switch malloc+memset(0) to calloc()
[flac.git] / src / libOggFLAC / stream_encoder.c
1 /* libOggFLAC - Free Lossless Audio Codec + Ogg library
2  * Copyright (C) 2002  Josh Coalson
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA  02111-1307, USA.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h> /* for calloc() */
22 #include <string.h> /* for memcpy() */
23 #include "ogg/ogg.h"
24 #include "FLAC/assert.h"
25 #include "OggFLAC/stream_decoder.h"
26 #include "protected/stream_encoder.h"
27
28 /***********************************************************************
29  *
30  * Private class method prototypes
31  *
32  ***********************************************************************/
33
34 /* unpublished debug routines */
35 extern FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value);
36 extern FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value);
37 extern FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value);
38
39 static void set_defaults_(OggFLAC__StreamEncoder *encoder);
40 static FLAC__StreamEncoderWriteStatus write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
41 static void metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data);
42
43
44 /***********************************************************************
45  *
46  * Private class data
47  *
48  ***********************************************************************/
49
50 typedef struct OggFLAC__StreamEncoderPrivate {
51         OggFLAC__StreamEncoderWriteCallback write_callback;
52         void *client_data;
53         FLAC__StreamEncoder *FLAC_stream_encoder;
54         /* internal vars (all the above are class settings) */
55         FLAC__bool is_first_packet;
56         FLAC__uint64 samples_written;
57         struct {
58                 ogg_stream_state stream_state;
59                 ogg_page page;
60         } ogg;
61 } OggFLAC__StreamEncoderPrivate;
62
63
64 /***********************************************************************
65  *
66  * Public static class data
67  *
68  ***********************************************************************/
69
70 const OggFLAC_API char * const OggFLAC__StreamEncoderStateString[] = {
71         "OggFLAC__STREAM_ENCODER_OK",
72         "OggFLAC__STREAM_ENCODER_OGG_ERROR",
73         "OggFLAC__STREAM_ENCODER_FLAC_STREAM_ENCODER_ERROR",
74         "OggFLAC__STREAM_ENCODER_INVALID_CALLBACK",
75         "OggFLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
76         "OggFLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
77         "OggFLAC__STREAM_ENCODER_UNINITIALIZED"
78 };
79
80
81 /***********************************************************************
82  *
83  * Class constructor/destructor
84  *
85  */
86 OggFLAC_API OggFLAC__StreamEncoder *OggFLAC__stream_encoder_new()
87 {
88         OggFLAC__StreamEncoder *encoder;
89
90         encoder = (OggFLAC__StreamEncoder*)calloc(1, sizeof(OggFLAC__StreamEncoder));
91         if(encoder == 0) {
92                 return 0;
93         }
94
95         encoder->protected_ = (OggFLAC__StreamEncoderProtected*)calloc(1, sizeof(OggFLAC__StreamEncoderProtected));
96         if(encoder->protected_ == 0) {
97                 free(encoder);
98                 return 0;
99         }
100
101         encoder->private_ = (OggFLAC__StreamEncoderPrivate*)calloc(1, sizeof(OggFLAC__StreamEncoderPrivate));
102         if(encoder->private_ == 0) {
103                 free(encoder->protected_);
104                 free(encoder);
105                 return 0;
106         }
107
108         encoder->private_->FLAC_stream_encoder = FLAC__stream_encoder_new();
109         if(0 == encoder->private_->FLAC_stream_encoder) {
110                 free(encoder->private_);
111                 free(encoder->protected_);
112                 free(encoder);
113                 return 0;
114         }
115
116         set_defaults_(encoder);
117
118         encoder->protected_->state = OggFLAC__STREAM_ENCODER_UNINITIALIZED;
119
120         return encoder;
121 }
122
123 OggFLAC_API void OggFLAC__stream_encoder_delete(OggFLAC__StreamEncoder *encoder)
124 {
125         FLAC__ASSERT(0 != encoder);
126         FLAC__ASSERT(0 != encoder->protected_);
127         FLAC__ASSERT(0 != encoder->private_);
128         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
129
130         (void)OggFLAC__stream_encoder_finish(encoder);
131
132         FLAC__stream_encoder_delete(encoder->private_->FLAC_stream_encoder);
133
134         free(encoder->private_);
135         free(encoder->protected_);
136         free(encoder);
137 }
138
139
140 /***********************************************************************
141  *
142  * Public class methods
143  *
144  ***********************************************************************/
145
146 OggFLAC_API OggFLAC__StreamEncoderState OggFLAC__stream_encoder_init(OggFLAC__StreamEncoder *encoder)
147 {
148         FLAC__ASSERT(0 != encoder);
149
150         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
151                 return encoder->protected_->state = OggFLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
152
153         if(0 == encoder->private_->write_callback)
154                 return encoder->protected_->state = OggFLAC__STREAM_ENCODER_INVALID_CALLBACK;
155
156         if(ogg_stream_init(&encoder->private_->ogg.stream_state, encoder->protected_->serial_number) != 0)
157                 return encoder->protected_->state = OggFLAC__STREAM_ENCODER_OGG_ERROR;
158
159         FLAC__stream_encoder_set_write_callback(encoder->private_->FLAC_stream_encoder, write_callback_);
160         FLAC__stream_encoder_set_metadata_callback(encoder->private_->FLAC_stream_encoder, metadata_callback_);
161         FLAC__stream_encoder_set_client_data(encoder->private_->FLAC_stream_encoder, encoder);
162
163         if(FLAC__stream_encoder_init(encoder->private_->FLAC_stream_encoder) != FLAC__STREAM_ENCODER_OK)
164                 return encoder->protected_->state = OggFLAC__STREAM_ENCODER_FLAC_STREAM_ENCODER_ERROR;
165
166         encoder->private_->is_first_packet = true;
167         encoder->private_->samples_written = 0;
168
169         return encoder->protected_->state = OggFLAC__STREAM_ENCODER_OK;
170 }
171
172 OggFLAC_API void OggFLAC__stream_encoder_finish(OggFLAC__StreamEncoder *encoder)
173 {
174         FLAC__ASSERT(0 != encoder);
175         FLAC__ASSERT(0 != encoder->private_);
176         FLAC__ASSERT(0 != encoder->protected_);
177
178         if(encoder->protected_->state == OggFLAC__STREAM_ENCODER_UNINITIALIZED)
179                 return;
180
181         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
182
183         FLAC__stream_encoder_finish(encoder->private_->FLAC_stream_encoder);
184
185         (void)ogg_stream_clear(&encoder->private_->ogg.stream_state);
186
187         set_defaults_(encoder);
188
189         encoder->protected_->state = OggFLAC__STREAM_ENCODER_UNINITIALIZED;
190 }
191
192 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_serial_number(OggFLAC__StreamEncoder *encoder, long value)
193 {
194         FLAC__ASSERT(0 != encoder);
195         FLAC__ASSERT(0 != encoder->private_);
196         FLAC__ASSERT(0 != encoder->protected_);
197         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
198         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
199                 return false;
200         encoder->protected_->serial_number = value;
201         return true;
202 }
203
204 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_verify(OggFLAC__StreamEncoder *encoder, FLAC__bool value)
205 {
206         FLAC__ASSERT(0 != encoder);
207         FLAC__ASSERT(0 != encoder->private_);
208         FLAC__ASSERT(0 != encoder->protected_);
209         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
210         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
211                 return false;
212         return FLAC__stream_encoder_set_verify(encoder->private_->FLAC_stream_encoder, value);
213 }
214
215 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_streamable_subset(OggFLAC__StreamEncoder *encoder, FLAC__bool value)
216 {
217         FLAC__ASSERT(0 != encoder);
218         FLAC__ASSERT(0 != encoder->private_);
219         FLAC__ASSERT(0 != encoder->protected_);
220         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
221         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
222                 return false;
223         return FLAC__stream_encoder_set_streamable_subset(encoder->private_->FLAC_stream_encoder, value);
224 }
225
226 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_do_mid_side_stereo(OggFLAC__StreamEncoder *encoder, FLAC__bool value)
227 {
228         FLAC__ASSERT(0 != encoder);
229         FLAC__ASSERT(0 != encoder->private_);
230         FLAC__ASSERT(0 != encoder->protected_);
231         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
232         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
233                 return false;
234         return FLAC__stream_encoder_set_do_mid_side_stereo(encoder->private_->FLAC_stream_encoder, value);
235 }
236
237 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_loose_mid_side_stereo(OggFLAC__StreamEncoder *encoder, FLAC__bool value)
238 {
239         FLAC__ASSERT(0 != encoder);
240         FLAC__ASSERT(0 != encoder->private_);
241         FLAC__ASSERT(0 != encoder->protected_);
242         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
243         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
244                 return false;
245         return FLAC__stream_encoder_set_loose_mid_side_stereo(encoder->private_->FLAC_stream_encoder, value);
246 }
247
248 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_channels(OggFLAC__StreamEncoder *encoder, unsigned value)
249 {
250         FLAC__ASSERT(0 != encoder);
251         FLAC__ASSERT(0 != encoder->private_);
252         FLAC__ASSERT(0 != encoder->protected_);
253         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
254         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
255                 return false;
256         return FLAC__stream_encoder_set_channels(encoder->private_->FLAC_stream_encoder, value);
257 }
258
259 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_bits_per_sample(OggFLAC__StreamEncoder *encoder, unsigned value)
260 {
261         FLAC__ASSERT(0 != encoder);
262         FLAC__ASSERT(0 != encoder->private_);
263         FLAC__ASSERT(0 != encoder->protected_);
264         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
265         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
266                 return false;
267         return FLAC__stream_encoder_set_bits_per_sample(encoder->private_->FLAC_stream_encoder, value);
268 }
269
270 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_sample_rate(OggFLAC__StreamEncoder *encoder, unsigned value)
271 {
272         FLAC__ASSERT(0 != encoder);
273         FLAC__ASSERT(0 != encoder->private_);
274         FLAC__ASSERT(0 != encoder->protected_);
275         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
276         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
277                 return false;
278         return FLAC__stream_encoder_set_sample_rate(encoder->private_->FLAC_stream_encoder, value);
279 }
280
281 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_blocksize(OggFLAC__StreamEncoder *encoder, unsigned value)
282 {
283         FLAC__ASSERT(0 != encoder);
284         FLAC__ASSERT(0 != encoder->private_);
285         FLAC__ASSERT(0 != encoder->protected_);
286         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
287         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
288                 return false;
289         return FLAC__stream_encoder_set_blocksize(encoder->private_->FLAC_stream_encoder, value);
290 }
291
292 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_max_lpc_order(OggFLAC__StreamEncoder *encoder, unsigned value)
293 {
294         FLAC__ASSERT(0 != encoder);
295         FLAC__ASSERT(0 != encoder->private_);
296         FLAC__ASSERT(0 != encoder->protected_);
297         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
298         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
299                 return false;
300         return FLAC__stream_encoder_set_max_lpc_order(encoder->private_->FLAC_stream_encoder, value);
301 }
302
303 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_qlp_coeff_precision(OggFLAC__StreamEncoder *encoder, unsigned value)
304 {
305         FLAC__ASSERT(0 != encoder);
306         FLAC__ASSERT(0 != encoder->private_);
307         FLAC__ASSERT(0 != encoder->protected_);
308         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
309         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
310                 return false;
311         return FLAC__stream_encoder_set_qlp_coeff_precision(encoder->private_->FLAC_stream_encoder, value);
312 }
313
314 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_do_qlp_coeff_prec_search(OggFLAC__StreamEncoder *encoder, FLAC__bool value)
315 {
316         FLAC__ASSERT(0 != encoder);
317         FLAC__ASSERT(0 != encoder->private_);
318         FLAC__ASSERT(0 != encoder->protected_);
319         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
320         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
321                 return false;
322         return FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder->private_->FLAC_stream_encoder, value);
323 }
324
325 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_do_escape_coding(OggFLAC__StreamEncoder *encoder, FLAC__bool value)
326 {
327         FLAC__ASSERT(0 != encoder);
328         FLAC__ASSERT(0 != encoder->private_);
329         FLAC__ASSERT(0 != encoder->protected_);
330         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
331         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
332                 return false;
333         return FLAC__stream_encoder_set_do_escape_coding(encoder->private_->FLAC_stream_encoder, value);
334 }
335
336 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_do_exhaustive_model_search(OggFLAC__StreamEncoder *encoder, FLAC__bool value)
337 {
338         FLAC__ASSERT(0 != encoder);
339         FLAC__ASSERT(0 != encoder->private_);
340         FLAC__ASSERT(0 != encoder->protected_);
341         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
342         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
343                 return false;
344         return FLAC__stream_encoder_set_do_exhaustive_model_search(encoder->private_->FLAC_stream_encoder, value);
345 }
346
347 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_min_residual_partition_order(OggFLAC__StreamEncoder *encoder, unsigned value)
348 {
349         FLAC__ASSERT(0 != encoder);
350         FLAC__ASSERT(0 != encoder->private_);
351         FLAC__ASSERT(0 != encoder->protected_);
352         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
353         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
354                 return false;
355         return FLAC__stream_encoder_set_min_residual_partition_order(encoder->private_->FLAC_stream_encoder, value);
356 }
357
358 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_max_residual_partition_order(OggFLAC__StreamEncoder *encoder, unsigned value)
359 {
360         FLAC__ASSERT(0 != encoder);
361         FLAC__ASSERT(0 != encoder->private_);
362         FLAC__ASSERT(0 != encoder->protected_);
363         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
364         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
365                 return false;
366         return FLAC__stream_encoder_set_max_residual_partition_order(encoder->private_->FLAC_stream_encoder, value);
367 }
368
369 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_rice_parameter_search_dist(OggFLAC__StreamEncoder *encoder, unsigned value)
370 {
371         FLAC__ASSERT(0 != encoder);
372         FLAC__ASSERT(0 != encoder->private_);
373         FLAC__ASSERT(0 != encoder->protected_);
374         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
375         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
376                 return false;
377         return FLAC__stream_encoder_set_rice_parameter_search_dist(encoder->private_->FLAC_stream_encoder, value);
378 }
379
380 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_total_samples_estimate(OggFLAC__StreamEncoder *encoder, FLAC__uint64 value)
381 {
382         FLAC__ASSERT(0 != encoder);
383         FLAC__ASSERT(0 != encoder->private_);
384         FLAC__ASSERT(0 != encoder->protected_);
385         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
386         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
387                 return false;
388         return FLAC__stream_encoder_set_total_samples_estimate(encoder->private_->FLAC_stream_encoder, value);
389 }
390
391 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_metadata(OggFLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
392 {
393         FLAC__ASSERT(0 != encoder);
394         FLAC__ASSERT(0 != encoder->private_);
395         FLAC__ASSERT(0 != encoder->protected_);
396         FLAC__ASSERT(0 != encoder->private_->FLAC_stream_encoder);
397         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
398                 return false;
399         return FLAC__stream_encoder_set_metadata(encoder->private_->FLAC_stream_encoder, metadata, num_blocks);
400 }
401
402 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_write_callback(OggFLAC__StreamEncoder *encoder, OggFLAC__StreamEncoderWriteCallback value)
403 {
404         FLAC__ASSERT(0 != encoder);
405         FLAC__ASSERT(0 != encoder->private_);
406         FLAC__ASSERT(0 != encoder->protected_);
407         FLAC__ASSERT(0 != value);
408         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
409                 return false;
410         encoder->private_->write_callback = value;
411         return true;
412 }
413
414 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_set_client_data(OggFLAC__StreamEncoder *encoder, void *value)
415 {
416         FLAC__ASSERT(0 != encoder);
417         FLAC__ASSERT(0 != encoder->private_);
418         FLAC__ASSERT(0 != encoder->protected_);
419         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
420                 return false;
421         encoder->private_->client_data = value;
422         return true;
423 }
424
425 /*
426  * These three functions are not static, but not publically exposed in
427  * include/FLAC/ either.  They are used by the test suite.
428  */
429 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_disable_constant_subframes(OggFLAC__StreamEncoder *encoder, FLAC__bool value)
430 {
431         FLAC__ASSERT(0 != encoder);
432         FLAC__ASSERT(0 != encoder->private_);
433         FLAC__ASSERT(0 != encoder->protected_);
434         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
435                 return false;
436         return FLAC__stream_encoder_disable_constant_subframes(encoder->private_->FLAC_stream_encoder, value);
437 }
438
439 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_disable_fixed_subframes(OggFLAC__StreamEncoder *encoder, FLAC__bool value)
440 {
441         FLAC__ASSERT(0 != encoder);
442         FLAC__ASSERT(0 != encoder->private_);
443         FLAC__ASSERT(0 != encoder->protected_);
444         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
445                 return false;
446         return FLAC__stream_encoder_disable_fixed_subframes(encoder->private_->FLAC_stream_encoder, value);
447 }
448
449 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_disable_verbatim_subframes(OggFLAC__StreamEncoder *encoder, FLAC__bool value)
450 {
451         FLAC__ASSERT(0 != encoder);
452         FLAC__ASSERT(0 != encoder->private_);
453         FLAC__ASSERT(0 != encoder->protected_);
454         if(encoder->protected_->state != OggFLAC__STREAM_ENCODER_UNINITIALIZED)
455                 return false;
456         return FLAC__stream_encoder_disable_verbatim_subframes(encoder->private_->FLAC_stream_encoder, value);
457 }
458
459 OggFLAC_API OggFLAC__StreamEncoderState OggFLAC__stream_encoder_get_state(const OggFLAC__StreamEncoder *encoder)
460 {
461         FLAC__ASSERT(0 != encoder);
462         FLAC__ASSERT(0 != encoder->private_);
463         FLAC__ASSERT(0 != encoder->protected_);
464         return encoder->protected_->state;
465 }
466
467 OggFLAC_API FLAC__StreamEncoderState OggFLAC__stream_encoder_get_FLAC_stream_encoder_state(const OggFLAC__StreamEncoder *encoder)
468 {
469         FLAC__ASSERT(0 != encoder);
470         FLAC__ASSERT(0 != encoder->private_);
471         FLAC__ASSERT(0 != encoder->protected_);
472         return FLAC__stream_encoder_get_state(encoder->private_->FLAC_stream_encoder);
473 }
474
475 OggFLAC_API FLAC__StreamDecoderState OggFLAC__stream_encoder_get_verify_decoder_state(const OggFLAC__StreamEncoder *encoder)
476 {
477         FLAC__ASSERT(0 != encoder);
478         FLAC__ASSERT(0 != encoder->private_);
479         FLAC__ASSERT(0 != encoder->protected_);
480         return FLAC__stream_encoder_get_verify_decoder_state(encoder->private_->FLAC_stream_encoder);
481 }
482
483 OggFLAC_API void OggFLAC__stream_encoder_get_verify_decoder_error_stats(const OggFLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
484 {
485         FLAC__ASSERT(0 != encoder);
486         FLAC__ASSERT(0 != encoder->private_);
487         FLAC__stream_encoder_get_verify_decoder_error_stats(encoder->private_->FLAC_stream_encoder, absolute_sample, frame_number, channel, sample, expected, got);
488 }
489
490 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_get_verify(const OggFLAC__StreamEncoder *encoder)
491 {
492         FLAC__ASSERT(0 != encoder);
493         FLAC__ASSERT(0 != encoder->private_);
494         FLAC__ASSERT(0 != encoder->protected_);
495         return FLAC__stream_encoder_get_verify(encoder->private_->FLAC_stream_encoder);
496 }
497
498 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_get_streamable_subset(const OggFLAC__StreamEncoder *encoder)
499 {
500         FLAC__ASSERT(0 != encoder);
501         FLAC__ASSERT(0 != encoder->private_);
502         FLAC__ASSERT(0 != encoder->protected_);
503         return FLAC__stream_encoder_get_streamable_subset(encoder->private_->FLAC_stream_encoder);
504 }
505
506 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_get_do_mid_side_stereo(const OggFLAC__StreamEncoder *encoder)
507 {
508         FLAC__ASSERT(0 != encoder);
509         FLAC__ASSERT(0 != encoder->private_);
510         FLAC__ASSERT(0 != encoder->protected_);
511         return FLAC__stream_encoder_get_do_mid_side_stereo(encoder->private_->FLAC_stream_encoder);
512 }
513
514 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_get_loose_mid_side_stereo(const OggFLAC__StreamEncoder *encoder)
515 {
516         FLAC__ASSERT(0 != encoder);
517         FLAC__ASSERT(0 != encoder->private_);
518         FLAC__ASSERT(0 != encoder->protected_);
519         return FLAC__stream_encoder_get_loose_mid_side_stereo(encoder->private_->FLAC_stream_encoder);
520 }
521
522 OggFLAC_API unsigned OggFLAC__stream_encoder_get_channels(const OggFLAC__StreamEncoder *encoder)
523 {
524         FLAC__ASSERT(0 != encoder);
525         FLAC__ASSERT(0 != encoder->private_);
526         FLAC__ASSERT(0 != encoder->protected_);
527         return FLAC__stream_encoder_get_channels(encoder->private_->FLAC_stream_encoder);
528 }
529
530 OggFLAC_API unsigned OggFLAC__stream_encoder_get_bits_per_sample(const OggFLAC__StreamEncoder *encoder)
531 {
532         FLAC__ASSERT(0 != encoder);
533         FLAC__ASSERT(0 != encoder->private_);
534         FLAC__ASSERT(0 != encoder->protected_);
535         return FLAC__stream_encoder_get_bits_per_sample(encoder->private_->FLAC_stream_encoder);
536 }
537
538 OggFLAC_API unsigned OggFLAC__stream_encoder_get_sample_rate(const OggFLAC__StreamEncoder *encoder)
539 {
540         FLAC__ASSERT(0 != encoder);
541         FLAC__ASSERT(0 != encoder->private_);
542         FLAC__ASSERT(0 != encoder->protected_);
543         return FLAC__stream_encoder_get_sample_rate(encoder->private_->FLAC_stream_encoder);
544 }
545
546 OggFLAC_API unsigned OggFLAC__stream_encoder_get_blocksize(const OggFLAC__StreamEncoder *encoder)
547 {
548         FLAC__ASSERT(0 != encoder);
549         FLAC__ASSERT(0 != encoder->private_);
550         FLAC__ASSERT(0 != encoder->protected_);
551         return FLAC__stream_encoder_get_blocksize(encoder->private_->FLAC_stream_encoder);
552 }
553
554 OggFLAC_API unsigned OggFLAC__stream_encoder_get_max_lpc_order(const OggFLAC__StreamEncoder *encoder)
555 {
556         FLAC__ASSERT(0 != encoder);
557         FLAC__ASSERT(0 != encoder->private_);
558         FLAC__ASSERT(0 != encoder->protected_);
559         return FLAC__stream_encoder_get_max_lpc_order(encoder->private_->FLAC_stream_encoder);
560 }
561
562 OggFLAC_API unsigned OggFLAC__stream_encoder_get_qlp_coeff_precision(const OggFLAC__StreamEncoder *encoder)
563 {
564         FLAC__ASSERT(0 != encoder);
565         FLAC__ASSERT(0 != encoder->private_);
566         FLAC__ASSERT(0 != encoder->protected_);
567         return FLAC__stream_encoder_get_qlp_coeff_precision(encoder->private_->FLAC_stream_encoder);
568 }
569
570 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_get_do_qlp_coeff_prec_search(const OggFLAC__StreamEncoder *encoder)
571 {
572         FLAC__ASSERT(0 != encoder);
573         FLAC__ASSERT(0 != encoder->private_);
574         FLAC__ASSERT(0 != encoder->protected_);
575         return FLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder->private_->FLAC_stream_encoder);
576 }
577
578 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_get_do_escape_coding(const OggFLAC__StreamEncoder *encoder)
579 {
580         FLAC__ASSERT(0 != encoder);
581         FLAC__ASSERT(0 != encoder->private_);
582         FLAC__ASSERT(0 != encoder->protected_);
583         return FLAC__stream_encoder_get_do_escape_coding(encoder->private_->FLAC_stream_encoder);
584 }
585
586 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_get_do_exhaustive_model_search(const OggFLAC__StreamEncoder *encoder)
587 {
588         FLAC__ASSERT(0 != encoder);
589         FLAC__ASSERT(0 != encoder->private_);
590         FLAC__ASSERT(0 != encoder->protected_);
591         return FLAC__stream_encoder_get_do_exhaustive_model_search(encoder->private_->FLAC_stream_encoder);
592 }
593
594 OggFLAC_API unsigned OggFLAC__stream_encoder_get_min_residual_partition_order(const OggFLAC__StreamEncoder *encoder)
595 {
596         FLAC__ASSERT(0 != encoder);
597         FLAC__ASSERT(0 != encoder->private_);
598         FLAC__ASSERT(0 != encoder->protected_);
599         return FLAC__stream_encoder_get_min_residual_partition_order(encoder->private_->FLAC_stream_encoder);
600 }
601
602 OggFLAC_API unsigned OggFLAC__stream_encoder_get_max_residual_partition_order(const OggFLAC__StreamEncoder *encoder)
603 {
604         FLAC__ASSERT(0 != encoder);
605         FLAC__ASSERT(0 != encoder->private_);
606         FLAC__ASSERT(0 != encoder->protected_);
607         return FLAC__stream_encoder_get_max_residual_partition_order(encoder->private_->FLAC_stream_encoder);
608 }
609
610 OggFLAC_API unsigned OggFLAC__stream_encoder_get_rice_parameter_search_dist(const OggFLAC__StreamEncoder *encoder)
611 {
612         FLAC__ASSERT(0 != encoder);
613         FLAC__ASSERT(0 != encoder->private_);
614         FLAC__ASSERT(0 != encoder->protected_);
615         return FLAC__stream_encoder_get_rice_parameter_search_dist(encoder->private_->FLAC_stream_encoder);
616 }
617
618 OggFLAC_API FLAC__uint64 OggFLAC__stream_encoder_get_total_samples_estimate(const OggFLAC__StreamEncoder *encoder)
619 {
620         FLAC__ASSERT(0 != encoder);
621         FLAC__ASSERT(0 != encoder->private_);
622         FLAC__ASSERT(0 != encoder->protected_);
623         return FLAC__stream_encoder_get_total_samples_estimate(encoder->private_->FLAC_stream_encoder);
624 }
625
626 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_process(OggFLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
627 {
628         FLAC__ASSERT(0 != encoder);
629         FLAC__ASSERT(0 != encoder->private_);
630         FLAC__ASSERT(0 != encoder->protected_);
631         return FLAC__stream_encoder_process(encoder->private_->FLAC_stream_encoder, buffer, samples);
632 }
633
634 OggFLAC_API FLAC__bool OggFLAC__stream_encoder_process_interleaved(OggFLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
635 {
636         FLAC__ASSERT(0 != encoder);
637         FLAC__ASSERT(0 != encoder->private_);
638         FLAC__ASSERT(0 != encoder->protected_);
639         return FLAC__stream_encoder_process_interleaved(encoder->private_->FLAC_stream_encoder, buffer, samples);
640 }
641
642 /***********************************************************************
643  *
644  * Private class methods
645  *
646  ***********************************************************************/
647
648 void set_defaults_(OggFLAC__StreamEncoder *encoder)
649 {
650         FLAC__ASSERT(0 != encoder);
651
652         encoder->private_->write_callback = 0;
653         encoder->private_->client_data = 0;
654         encoder->protected_->serial_number = 0;
655 }
656
657 FLAC__StreamEncoderWriteStatus write_callback_(const FLAC__StreamEncoder *unused, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
658 {
659         OggFLAC__StreamEncoder *encoder = (OggFLAC__StreamEncoder*)client_data;
660         ogg_packet packet;
661         const FLAC__uint64 total_samples_estimate = FLAC__stream_encoder_get_total_samples_estimate(encoder->private_->FLAC_stream_encoder);
662         FLAC__ASSERT(encoder->private_->FLAC_stream_encoder == unused);
663
664         encoder->private_->samples_written += samples;
665
666         memset(&packet, 0, sizeof(packet));
667         packet.packet = (unsigned char *)buffer;
668         packet.granulepos = encoder->private_->samples_written;
669         /* WATCHOUT:
670          * This depends on the behavior of FLAC__StreamEncoder that 'samples'
671          * will be 0 for metadata writes.
672          */
673         packet.packetno = (samples == 0? -1 : (int)current_frame);
674         packet.bytes = bytes;
675
676         if(encoder->private_->is_first_packet) {
677                 packet.b_o_s = 1;
678                 encoder->private_->is_first_packet = false;
679         }
680
681         if(total_samples_estimate > 0 && total_samples_estimate == encoder->private_->samples_written)
682                 packet.e_o_s = 1;
683
684         if(ogg_stream_packetin(&encoder->private_->ogg.stream_state, &packet) != 0)
685                 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
686
687         while(ogg_stream_pageout(&encoder->private_->ogg.stream_state, &encoder->private_->ogg.page) != 0) {
688                 if(encoder->private_->write_callback(encoder, encoder->private_->ogg.page.header, encoder->private_->ogg.page.header_len, 0, current_frame, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK)
689                         return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
690
691                 if(encoder->private_->write_callback(encoder, encoder->private_->ogg.page.body, encoder->private_->ogg.page.body_len, 0, current_frame, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK)
692                         return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
693         }
694
695         return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
696 }
697
698 void metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
699 {
700         /*
701          * We don't try to go back an update metadata blocks by mucking
702          * around inside the Ogg layer.  Maybe someday we will care to
703          * and an OggFLAC__SeekableStreamEncoder and OggFLAC__FileEncoder
704          * will be possible but it may just never be useful.
705          */
706         (void)encoder, (void)metadata, (void)client_data;
707 }