16740c082f3142932d58a8b78a64aa04c87c06d3
[flac.git] / src / libFLAC / seekable_stream_encoder.c
1 /* libFLAC - Free Lossless Audio Codec 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 malloc() */
22 #include <string.h> /* for memcpy() */
23 #include "FLAC/assert.h"
24 #include "protected/seekable_stream_encoder.h"
25 #include "protected/stream_encoder.h"
26
27 #ifdef max
28 #undef max
29 #endif
30 #define max(a,b) ((a)>(b)?(a):(b))
31
32 /***********************************************************************
33  *
34  * Private class method prototypes
35  *
36  ***********************************************************************/
37
38 /* unpublished debug routines */
39 extern FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value);
40 extern FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value);
41 extern FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value);
42
43 static void set_defaults_(FLAC__SeekableStreamEncoder *encoder);
44 static FLAC__StreamEncoderWriteStatus write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
45 static void metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data);
46
47 /***********************************************************************
48  *
49  * Private class data
50  *
51  ***********************************************************************/
52
53 typedef struct FLAC__SeekableStreamEncoderPrivate {
54         FLAC__SeekableStreamEncoderSeekCallback seek_callback;
55         FLAC__SeekableStreamEncoderWriteCallback write_callback;
56         void *client_data;
57         FLAC__StreamEncoder *stream_encoder;
58         FLAC__StreamMetadata_SeekTable *seek_table;
59         /* internal vars (all the above are class settings) */
60         unsigned first_seekpoint_to_check;
61         FLAC__uint64 stream_offset, seektable_offset;
62         FLAC__uint64 bytes_written;
63 } FLAC__SeekableStreamEncoderPrivate;
64
65 /***********************************************************************
66  *
67  * Public static class data
68  *
69  ***********************************************************************/
70
71 FLAC_API const char * const FLAC__SeekableStreamEncoderStateString[] = {
72         "FLAC__SEEKABLE_STREAM_ENCODER_OK",
73         "FLAC__SEEKABLE_STREAM_ENCODER_STREAM_ENCODER_ERROR",
74         "FLAC__SEEKABLE_STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
75         "FLAC__SEEKABLE_STREAM_ENCODER_WRITE_ERROR",
76         "FLAC__SEEKABLE_STREAM_ENCODER_READ_ERROR",
77         "FLAC__SEEKABLE_STREAM_ENCODER_SEEK_ERROR",
78         "FLAC__SEEKABLE_STREAM_ENCODER_ALREADY_INITIALIZED",
79         "FLAC__SEEKABLE_STREAM_ENCODER_INVALID_CALLBACK",
80         "FLAC__SEEKABLE_STREAM_ENCODER_INVALID_SEEKTABLE",
81         "FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED"
82 };
83
84 FLAC_API const char * const FLAC__SeekableStreamEncoderSeekStatusString[] = {
85         "FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK",
86         "FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_ERROR"
87 };
88
89
90 /***********************************************************************
91  *
92  * Class constructor/destructor
93  *
94  ***********************************************************************/
95
96 FLAC_API FLAC__SeekableStreamEncoder *FLAC__seekable_stream_encoder_new()
97 {
98         FLAC__SeekableStreamEncoder *encoder;
99
100         FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
101
102         encoder = (FLAC__SeekableStreamEncoder*)malloc(sizeof(FLAC__SeekableStreamEncoder));
103         if(encoder == 0) {
104                 return 0;
105         }
106         memset(encoder, 0, sizeof(FLAC__SeekableStreamEncoder));
107
108         encoder->protected_ = (FLAC__SeekableStreamEncoderProtected*)malloc(sizeof(FLAC__SeekableStreamEncoderProtected));
109         if(encoder->protected_ == 0) {
110                 free(encoder);
111                 return 0;
112         }
113         memset(encoder->protected_, 0, sizeof(FLAC__SeekableStreamEncoderProtected));
114
115         encoder->private_ = (FLAC__SeekableStreamEncoderPrivate*)malloc(sizeof(FLAC__SeekableStreamEncoderPrivate));
116         if(encoder->private_ == 0) {
117                 free(encoder->protected_);
118                 free(encoder);
119                 return 0;
120         }
121         memset(encoder->private_, 0, sizeof(FLAC__SeekableStreamEncoderPrivate));
122
123         encoder->private_->stream_encoder = FLAC__stream_encoder_new();
124         if(0 == encoder->private_->stream_encoder) {
125                 free(encoder->private_);
126                 free(encoder->protected_);
127                 free(encoder);
128                 return 0;
129         }
130
131         set_defaults_(encoder);
132
133         encoder->protected_->state = FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED;
134
135         return encoder;
136 }
137
138 FLAC_API void FLAC__seekable_stream_encoder_delete(FLAC__SeekableStreamEncoder *encoder)
139 {
140         FLAC__ASSERT(0 != encoder);
141         FLAC__ASSERT(0 != encoder->protected_);
142         FLAC__ASSERT(0 != encoder->private_);
143         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
144
145         (void)FLAC__seekable_stream_encoder_finish(encoder);
146
147         FLAC__stream_encoder_delete(encoder->private_->stream_encoder);
148
149         free(encoder->private_);
150         free(encoder->protected_);
151         free(encoder);
152 }
153
154
155 /***********************************************************************
156  *
157  * Public class methods
158  *
159  ***********************************************************************/
160
161 FLAC_API FLAC__SeekableStreamEncoderState FLAC__seekable_stream_encoder_init(FLAC__SeekableStreamEncoder *encoder)
162 {
163         FLAC__ASSERT(0 != encoder);
164
165         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
166                 return encoder->protected_->state = FLAC__SEEKABLE_STREAM_ENCODER_ALREADY_INITIALIZED;
167
168         if(0 == encoder->private_->seek_callback || 0 == encoder->private_->write_callback)
169                 return encoder->protected_->state = FLAC__SEEKABLE_STREAM_ENCODER_INVALID_CALLBACK;
170
171         if(0 != encoder->private_->seek_table && !FLAC__format_seektable_is_legal(encoder->private_->seek_table))
172                 return encoder->protected_->state = FLAC__SEEKABLE_STREAM_ENCODER_INVALID_SEEKTABLE;
173
174         /*
175          * This must be done before we init the stream encoder because that
176          * calls the write_callback, which uses these values.
177          */
178         encoder->private_->first_seekpoint_to_check = 0;
179         encoder->private_->stream_offset = 0;
180         encoder->private_->seektable_offset = 0;
181         encoder->private_->bytes_written = 0;
182
183         FLAC__stream_encoder_set_write_callback(encoder->private_->stream_encoder, write_callback_);
184         FLAC__stream_encoder_set_metadata_callback(encoder->private_->stream_encoder, metadata_callback_);
185         FLAC__stream_encoder_set_client_data(encoder->private_->stream_encoder, encoder);
186
187         if(FLAC__stream_encoder_init(encoder->private_->stream_encoder) != FLAC__STREAM_ENCODER_OK)
188                 return encoder->protected_->state = FLAC__SEEKABLE_STREAM_ENCODER_STREAM_ENCODER_ERROR;
189
190         /*
191          * Initializing the stream encoder writes all the metadata, so we
192          * save the stream offset now.
193          */
194         encoder->private_->stream_offset = encoder->private_->bytes_written;
195
196         return encoder->protected_->state = FLAC__SEEKABLE_STREAM_ENCODER_OK;
197 }
198
199 FLAC_API void FLAC__seekable_stream_encoder_finish(FLAC__SeekableStreamEncoder *encoder)
200 {
201         FLAC__ASSERT(0 != encoder);
202         FLAC__ASSERT(0 != encoder->private_);
203         FLAC__ASSERT(0 != encoder->protected_);
204
205         if(encoder->protected_->state == FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
206                 return;
207
208         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
209
210         FLAC__stream_encoder_finish(encoder->private_->stream_encoder);
211
212         set_defaults_(encoder);
213
214         encoder->protected_->state = FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED;
215 }
216
217 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_verify(FLAC__SeekableStreamEncoder *encoder, FLAC__bool value)
218 {
219         FLAC__ASSERT(0 != encoder);
220         FLAC__ASSERT(0 != encoder->private_);
221         FLAC__ASSERT(0 != encoder->protected_);
222         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
223         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
224                 return false;
225         return FLAC__stream_encoder_set_verify(encoder->private_->stream_encoder, value);
226 }
227
228 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_streamable_subset(FLAC__SeekableStreamEncoder *encoder, FLAC__bool value)
229 {
230         FLAC__ASSERT(0 != encoder);
231         FLAC__ASSERT(0 != encoder->private_);
232         FLAC__ASSERT(0 != encoder->protected_);
233         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
234         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
235                 return false;
236         return FLAC__stream_encoder_set_streamable_subset(encoder->private_->stream_encoder, value);
237 }
238
239 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_do_mid_side_stereo(FLAC__SeekableStreamEncoder *encoder, FLAC__bool value)
240 {
241         FLAC__ASSERT(0 != encoder);
242         FLAC__ASSERT(0 != encoder->private_);
243         FLAC__ASSERT(0 != encoder->protected_);
244         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
245         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
246                 return false;
247         return FLAC__stream_encoder_set_do_mid_side_stereo(encoder->private_->stream_encoder, value);
248 }
249
250 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_loose_mid_side_stereo(FLAC__SeekableStreamEncoder *encoder, FLAC__bool value)
251 {
252         FLAC__ASSERT(0 != encoder);
253         FLAC__ASSERT(0 != encoder->private_);
254         FLAC__ASSERT(0 != encoder->protected_);
255         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
256         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
257                 return false;
258         return FLAC__stream_encoder_set_loose_mid_side_stereo(encoder->private_->stream_encoder, value);
259 }
260
261 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_channels(FLAC__SeekableStreamEncoder *encoder, unsigned value)
262 {
263         FLAC__ASSERT(0 != encoder);
264         FLAC__ASSERT(0 != encoder->private_);
265         FLAC__ASSERT(0 != encoder->protected_);
266         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
267         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
268                 return false;
269         return FLAC__stream_encoder_set_channels(encoder->private_->stream_encoder, value);
270 }
271
272 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_bits_per_sample(FLAC__SeekableStreamEncoder *encoder, unsigned value)
273 {
274         FLAC__ASSERT(0 != encoder);
275         FLAC__ASSERT(0 != encoder->private_);
276         FLAC__ASSERT(0 != encoder->protected_);
277         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
278         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
279                 return false;
280         return FLAC__stream_encoder_set_bits_per_sample(encoder->private_->stream_encoder, value);
281 }
282
283 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_sample_rate(FLAC__SeekableStreamEncoder *encoder, unsigned value)
284 {
285         FLAC__ASSERT(0 != encoder);
286         FLAC__ASSERT(0 != encoder->private_);
287         FLAC__ASSERT(0 != encoder->protected_);
288         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
289         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
290                 return false;
291         return FLAC__stream_encoder_set_sample_rate(encoder->private_->stream_encoder, value);
292 }
293
294 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_blocksize(FLAC__SeekableStreamEncoder *encoder, unsigned value)
295 {
296         FLAC__ASSERT(0 != encoder);
297         FLAC__ASSERT(0 != encoder->private_);
298         FLAC__ASSERT(0 != encoder->protected_);
299         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
300         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
301                 return false;
302         return FLAC__stream_encoder_set_blocksize(encoder->private_->stream_encoder, value);
303 }
304
305 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_max_lpc_order(FLAC__SeekableStreamEncoder *encoder, unsigned value)
306 {
307         FLAC__ASSERT(0 != encoder);
308         FLAC__ASSERT(0 != encoder->private_);
309         FLAC__ASSERT(0 != encoder->protected_);
310         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
311         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
312                 return false;
313         return FLAC__stream_encoder_set_max_lpc_order(encoder->private_->stream_encoder, value);
314 }
315
316 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_qlp_coeff_precision(FLAC__SeekableStreamEncoder *encoder, unsigned value)
317 {
318         FLAC__ASSERT(0 != encoder);
319         FLAC__ASSERT(0 != encoder->private_);
320         FLAC__ASSERT(0 != encoder->protected_);
321         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
322         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
323                 return false;
324         return FLAC__stream_encoder_set_qlp_coeff_precision(encoder->private_->stream_encoder, value);
325 }
326
327 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_do_qlp_coeff_prec_search(FLAC__SeekableStreamEncoder *encoder, FLAC__bool value)
328 {
329         FLAC__ASSERT(0 != encoder);
330         FLAC__ASSERT(0 != encoder->private_);
331         FLAC__ASSERT(0 != encoder->protected_);
332         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
333         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
334                 return false;
335         return FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder->private_->stream_encoder, value);
336 }
337
338 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_do_escape_coding(FLAC__SeekableStreamEncoder *encoder, FLAC__bool value)
339 {
340         FLAC__ASSERT(0 != encoder);
341         FLAC__ASSERT(0 != encoder->private_);
342         FLAC__ASSERT(0 != encoder->protected_);
343         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
344         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
345                 return false;
346         return FLAC__stream_encoder_set_do_escape_coding(encoder->private_->stream_encoder, value);
347 }
348
349 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_do_exhaustive_model_search(FLAC__SeekableStreamEncoder *encoder, FLAC__bool value)
350 {
351         FLAC__ASSERT(0 != encoder);
352         FLAC__ASSERT(0 != encoder->private_);
353         FLAC__ASSERT(0 != encoder->protected_);
354         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
355         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
356                 return false;
357         return FLAC__stream_encoder_set_do_exhaustive_model_search(encoder->private_->stream_encoder, value);
358 }
359
360 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_min_residual_partition_order(FLAC__SeekableStreamEncoder *encoder, unsigned value)
361 {
362         FLAC__ASSERT(0 != encoder);
363         FLAC__ASSERT(0 != encoder->private_);
364         FLAC__ASSERT(0 != encoder->protected_);
365         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
366         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
367                 return false;
368         return FLAC__stream_encoder_set_min_residual_partition_order(encoder->private_->stream_encoder, value);
369 }
370
371 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_max_residual_partition_order(FLAC__SeekableStreamEncoder *encoder, unsigned value)
372 {
373         FLAC__ASSERT(0 != encoder);
374         FLAC__ASSERT(0 != encoder->private_);
375         FLAC__ASSERT(0 != encoder->protected_);
376         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
377         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
378                 return false;
379         return FLAC__stream_encoder_set_max_residual_partition_order(encoder->private_->stream_encoder, value);
380 }
381
382 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_rice_parameter_search_dist(FLAC__SeekableStreamEncoder *encoder, unsigned value)
383 {
384         FLAC__ASSERT(0 != encoder);
385         FLAC__ASSERT(0 != encoder->private_);
386         FLAC__ASSERT(0 != encoder->protected_);
387         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
388         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
389                 return false;
390         return FLAC__stream_encoder_set_rice_parameter_search_dist(encoder->private_->stream_encoder, value);
391 }
392
393 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_total_samples_estimate(FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 value)
394 {
395         FLAC__ASSERT(0 != encoder);
396         FLAC__ASSERT(0 != encoder->private_);
397         FLAC__ASSERT(0 != encoder->protected_);
398         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
399         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
400                 return false;
401         return FLAC__stream_encoder_set_total_samples_estimate(encoder->private_->stream_encoder, value);
402 }
403
404 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_metadata(FLAC__SeekableStreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
405 {
406         FLAC__ASSERT(0 != encoder);
407         FLAC__ASSERT(0 != encoder->private_);
408         FLAC__ASSERT(0 != encoder->protected_);
409         FLAC__ASSERT(0 != encoder->private_->stream_encoder);
410         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
411                 return false;
412         if(0 != metadata && num_blocks > 0) {
413                 unsigned i;
414                 for(i = 0; i < num_blocks; i++) {
415                         if(0 != metadata[i] && metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
416                                 encoder->private_->seek_table = &metadata[i]->data.seek_table;
417                                 break; /* take only the first one */
418                         }
419                 }
420         }
421         return FLAC__stream_encoder_set_metadata(encoder->private_->stream_encoder, metadata, num_blocks);
422 }
423
424 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_seek_callback(FLAC__SeekableStreamEncoder *encoder, FLAC__SeekableStreamEncoderSeekCallback value)
425 {
426         FLAC__ASSERT(0 != encoder);
427         FLAC__ASSERT(0 != encoder->private_);
428         FLAC__ASSERT(0 != encoder->protected_);
429         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
430                 return false;
431         encoder->private_->seek_callback = value;
432         return true;
433 }
434
435 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_write_callback(FLAC__SeekableStreamEncoder *encoder, FLAC__SeekableStreamEncoderWriteCallback value)
436 {
437         FLAC__ASSERT(0 != encoder);
438         FLAC__ASSERT(0 != encoder->private_);
439         FLAC__ASSERT(0 != encoder->protected_);
440         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
441                 return false;
442         encoder->private_->write_callback = value;
443         return true;
444 }
445
446 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_set_client_data(FLAC__SeekableStreamEncoder *encoder, void *value)
447 {
448         FLAC__ASSERT(0 != encoder);
449         FLAC__ASSERT(0 != encoder->private_);
450         FLAC__ASSERT(0 != encoder->protected_);
451         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
452                 return false;
453         encoder->private_->client_data = value;
454         return true;
455 }
456
457 /*
458  * These three functions are not static, but not publically exposed in
459  * include/FLAC/ either.  They are used by the test suite.
460  */
461 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_disable_constant_subframes(FLAC__SeekableStreamEncoder *encoder, FLAC__bool value)
462 {
463         FLAC__ASSERT(0 != encoder);
464         FLAC__ASSERT(0 != encoder->private_);
465         FLAC__ASSERT(0 != encoder->protected_);
466         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
467                 return false;
468         return FLAC__stream_encoder_disable_constant_subframes(encoder->private_->stream_encoder, value);
469 }
470
471 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_disable_fixed_subframes(FLAC__SeekableStreamEncoder *encoder, FLAC__bool value)
472 {
473         FLAC__ASSERT(0 != encoder);
474         FLAC__ASSERT(0 != encoder->private_);
475         FLAC__ASSERT(0 != encoder->protected_);
476         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
477                 return false;
478         return FLAC__stream_encoder_disable_fixed_subframes(encoder->private_->stream_encoder, value);
479 }
480
481 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_disable_verbatim_subframes(FLAC__SeekableStreamEncoder *encoder, FLAC__bool value)
482 {
483         FLAC__ASSERT(0 != encoder);
484         FLAC__ASSERT(0 != encoder->private_);
485         FLAC__ASSERT(0 != encoder->protected_);
486         if(encoder->protected_->state != FLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED)
487                 return false;
488         return FLAC__stream_encoder_disable_verbatim_subframes(encoder->private_->stream_encoder, value);
489 }
490
491 FLAC_API FLAC__SeekableStreamEncoderState FLAC__seekable_stream_encoder_get_state(const FLAC__SeekableStreamEncoder *encoder)
492 {
493         FLAC__ASSERT(0 != encoder);
494         FLAC__ASSERT(0 != encoder->protected_);
495         return encoder->protected_->state;
496 }
497
498 FLAC_API FLAC__StreamEncoderState FLAC__seekable_stream_encoder_get_stream_encoder_state(const FLAC__SeekableStreamEncoder *encoder)
499 {
500         FLAC__ASSERT(0 != encoder);
501         FLAC__ASSERT(0 != encoder->private_);
502         return FLAC__stream_encoder_get_state(encoder->private_->stream_encoder);
503 }
504
505 FLAC_API FLAC__StreamDecoderState FLAC__seekable_stream_encoder_get_verify_decoder_state(const FLAC__SeekableStreamEncoder *encoder)
506 {
507         FLAC__ASSERT(0 != encoder);
508         FLAC__ASSERT(0 != encoder->private_);
509         return FLAC__stream_encoder_get_verify_decoder_state(encoder->private_->stream_encoder);
510 }
511
512 FLAC_API void FLAC__seekable_stream_encoder_get_verify_decoder_error_stats(const FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
513 {
514         FLAC__ASSERT(0 != encoder);
515         FLAC__ASSERT(0 != encoder->private_);
516         FLAC__stream_encoder_get_verify_decoder_error_stats(encoder->private_->stream_encoder, absolute_sample, frame_number, channel, sample, expected, got);
517 }
518
519 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_get_verify(const FLAC__SeekableStreamEncoder *encoder)
520 {
521         FLAC__ASSERT(0 != encoder);
522         FLAC__ASSERT(0 != encoder->private_);
523         return FLAC__stream_encoder_get_verify(encoder->private_->stream_encoder);
524 }
525
526 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_get_streamable_subset(const FLAC__SeekableStreamEncoder *encoder)
527 {
528         FLAC__ASSERT(0 != encoder);
529         FLAC__ASSERT(0 != encoder->private_);
530         return FLAC__stream_encoder_get_streamable_subset(encoder->private_->stream_encoder);
531 }
532
533 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_get_do_mid_side_stereo(const FLAC__SeekableStreamEncoder *encoder)
534 {
535         FLAC__ASSERT(0 != encoder);
536         FLAC__ASSERT(0 != encoder->private_);
537         return FLAC__stream_encoder_get_do_mid_side_stereo(encoder->private_->stream_encoder);
538 }
539
540 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_get_loose_mid_side_stereo(const FLAC__SeekableStreamEncoder *encoder)
541 {
542         FLAC__ASSERT(0 != encoder);
543         FLAC__ASSERT(0 != encoder->private_);
544         return FLAC__stream_encoder_get_loose_mid_side_stereo(encoder->private_->stream_encoder);
545 }
546
547 FLAC_API unsigned FLAC__seekable_stream_encoder_get_channels(const FLAC__SeekableStreamEncoder *encoder)
548 {
549         FLAC__ASSERT(0 != encoder);
550         FLAC__ASSERT(0 != encoder->private_);
551         return FLAC__stream_encoder_get_channels(encoder->private_->stream_encoder);
552 }
553
554 FLAC_API unsigned FLAC__seekable_stream_encoder_get_bits_per_sample(const FLAC__SeekableStreamEncoder *encoder)
555 {
556         FLAC__ASSERT(0 != encoder);
557         FLAC__ASSERT(0 != encoder->private_);
558         return FLAC__stream_encoder_get_bits_per_sample(encoder->private_->stream_encoder);
559 }
560
561 FLAC_API unsigned FLAC__seekable_stream_encoder_get_sample_rate(const FLAC__SeekableStreamEncoder *encoder)
562 {
563         FLAC__ASSERT(0 != encoder);
564         FLAC__ASSERT(0 != encoder->private_);
565         return FLAC__stream_encoder_get_sample_rate(encoder->private_->stream_encoder);
566 }
567
568 FLAC_API unsigned FLAC__seekable_stream_encoder_get_blocksize(const FLAC__SeekableStreamEncoder *encoder)
569 {
570         FLAC__ASSERT(0 != encoder);
571         FLAC__ASSERT(0 != encoder->private_);
572         return FLAC__stream_encoder_get_blocksize(encoder->private_->stream_encoder);
573 }
574
575 FLAC_API unsigned FLAC__seekable_stream_encoder_get_max_lpc_order(const FLAC__SeekableStreamEncoder *encoder)
576 {
577         FLAC__ASSERT(0 != encoder);
578         FLAC__ASSERT(0 != encoder->private_);
579         return FLAC__stream_encoder_get_max_lpc_order(encoder->private_->stream_encoder);
580 }
581
582 FLAC_API unsigned FLAC__seekable_stream_encoder_get_qlp_coeff_precision(const FLAC__SeekableStreamEncoder *encoder)
583 {
584         FLAC__ASSERT(0 != encoder);
585         FLAC__ASSERT(0 != encoder->private_);
586         return FLAC__stream_encoder_get_qlp_coeff_precision(encoder->private_->stream_encoder);
587 }
588
589 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__SeekableStreamEncoder *encoder)
590 {
591         FLAC__ASSERT(0 != encoder);
592         FLAC__ASSERT(0 != encoder->private_);
593         return FLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder->private_->stream_encoder);
594 }
595
596 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_get_do_escape_coding(const FLAC__SeekableStreamEncoder *encoder)
597 {
598         FLAC__ASSERT(0 != encoder);
599         FLAC__ASSERT(0 != encoder->private_);
600         return FLAC__stream_encoder_get_do_escape_coding(encoder->private_->stream_encoder);
601 }
602
603 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_get_do_exhaustive_model_search(const FLAC__SeekableStreamEncoder *encoder)
604 {
605         FLAC__ASSERT(0 != encoder);
606         FLAC__ASSERT(0 != encoder->private_);
607         return FLAC__stream_encoder_get_do_exhaustive_model_search(encoder->private_->stream_encoder);
608 }
609
610 FLAC_API unsigned FLAC__seekable_stream_encoder_get_min_residual_partition_order(const FLAC__SeekableStreamEncoder *encoder)
611 {
612         FLAC__ASSERT(0 != encoder);
613         FLAC__ASSERT(0 != encoder->private_);
614         return FLAC__stream_encoder_get_min_residual_partition_order(encoder->private_->stream_encoder);
615 }
616
617 FLAC_API unsigned FLAC__seekable_stream_encoder_get_max_residual_partition_order(const FLAC__SeekableStreamEncoder *encoder)
618 {
619         FLAC__ASSERT(0 != encoder);
620         FLAC__ASSERT(0 != encoder->private_);
621         return FLAC__stream_encoder_get_max_residual_partition_order(encoder->private_->stream_encoder);
622 }
623
624 FLAC_API unsigned FLAC__seekable_stream_encoder_get_rice_parameter_search_dist(const FLAC__SeekableStreamEncoder *encoder)
625 {
626         FLAC__ASSERT(0 != encoder);
627         FLAC__ASSERT(0 != encoder->private_);
628         return FLAC__stream_encoder_get_rice_parameter_search_dist(encoder->private_->stream_encoder);
629 }
630
631 FLAC_API FLAC__uint64 FLAC__seekable_stream_encoder_get_total_samples_estimate(const FLAC__SeekableStreamEncoder *encoder)
632 {
633         FLAC__ASSERT(0 != encoder);
634         FLAC__ASSERT(0 != encoder->private_);
635         return FLAC__stream_encoder_get_total_samples_estimate(encoder->private_->stream_encoder);
636 }
637
638 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_process(FLAC__SeekableStreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
639 {
640         FLAC__ASSERT(0 != encoder);
641         FLAC__ASSERT(0 != encoder->private_);
642         if(!FLAC__stream_encoder_process(encoder->private_->stream_encoder, buffer, samples)) {
643                 encoder->protected_->state = FLAC__SEEKABLE_STREAM_ENCODER_STREAM_ENCODER_ERROR;
644                 return false;
645         }
646         else
647                 return true;
648 }
649
650 /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
651 FLAC_API FLAC__bool FLAC__seekable_stream_encoder_process_interleaved(FLAC__SeekableStreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
652 {
653         FLAC__ASSERT(0 != encoder);
654         FLAC__ASSERT(0 != encoder->private_);
655         if(!FLAC__stream_encoder_process_interleaved(encoder->private_->stream_encoder, buffer, samples)) {
656                 encoder->protected_->state = FLAC__SEEKABLE_STREAM_ENCODER_STREAM_ENCODER_ERROR;
657                 return false;
658         }
659         else
660                 return true;
661 }
662
663 /***********************************************************************
664  *
665  * Private class methods
666  *
667  ***********************************************************************/
668
669 void set_defaults_(FLAC__SeekableStreamEncoder *encoder)
670 {
671         FLAC__ASSERT(0 != encoder);
672         FLAC__ASSERT(0 != encoder->private_);
673         FLAC__ASSERT(0 != encoder->protected_);
674
675         encoder->private_->seek_callback = 0;
676         encoder->private_->write_callback = 0;
677         encoder->private_->client_data = 0;
678
679         encoder->private_->seek_table = 0;
680 }
681
682 FLAC__StreamEncoderWriteStatus write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
683 {
684         FLAC__SeekableStreamEncoder *seekable_stream_encoder = (FLAC__SeekableStreamEncoder*)client_data;
685         FLAC__StreamEncoderWriteStatus status;
686
687         /*
688          * Watch for the first SEEKTABLE block to go by and store its offset.
689          */
690         if(samples == 0 && (buffer[0] & 0x7f) == FLAC__METADATA_TYPE_SEEKTABLE)
691                 seekable_stream_encoder->private_->seektable_offset = seekable_stream_encoder->private_->bytes_written;
692
693         /*
694          * Mark the current seek point if hit (if stream_offset == 0 that
695          * means we're still writing metadata and haven't hit the first
696          * frame yet)
697          */
698         if(0 != seekable_stream_encoder->private_->seek_table && seekable_stream_encoder->private_->stream_offset > 0 && seekable_stream_encoder->private_->seek_table->num_points > 0) {
699                 /*@@@ WATCHOUT: assumes the encoder is fixed-blocksize, which will be true indefinitely: */
700                 const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
701                 const FLAC__uint64 frame_first_sample = (FLAC__uint64)current_frame * (FLAC__uint64)blocksize;
702                 const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
703                 FLAC__uint64 test_sample;
704                 unsigned i;
705                 for(i = seekable_stream_encoder->private_->first_seekpoint_to_check; i < seekable_stream_encoder->private_->seek_table->num_points; i++) {
706                         test_sample = seekable_stream_encoder->private_->seek_table->points[i].sample_number;
707                         if(test_sample > frame_last_sample) {
708                                 break;
709                         }
710                         else if(test_sample >= frame_first_sample) {
711                                 seekable_stream_encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
712                                 seekable_stream_encoder->private_->seek_table->points[i].stream_offset = seekable_stream_encoder->private_->bytes_written - seekable_stream_encoder->private_->stream_offset;
713                                 seekable_stream_encoder->private_->seek_table->points[i].frame_samples = blocksize;
714                                 seekable_stream_encoder->private_->first_seekpoint_to_check++;
715                                 /* DO NOT: "break;" and here's why:
716                                  * The seektable template may contain more than one target
717                                  * sample for any given frame; we will keep looping, generating
718                                  * duplicate seekpoints for them, and we'll clean it up later,
719                                  * just before writing the seektable back to the metadata.
720                                  */
721                         }
722                         else {
723                                 seekable_stream_encoder->private_->first_seekpoint_to_check++;
724                         }
725                 }
726         }
727
728         status = seekable_stream_encoder->private_->write_callback(seekable_stream_encoder, buffer, bytes, samples, current_frame, seekable_stream_encoder->private_->client_data);
729
730         if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK)
731                 seekable_stream_encoder->private_->bytes_written += bytes;
732         else
733                 seekable_stream_encoder->protected_->state = FLAC__SEEKABLE_STREAM_ENCODER_WRITE_ERROR;
734
735         return status;
736 }
737
738 void metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
739 {
740         FLAC__SeekableStreamEncoder *seekable_stream_encoder = (FLAC__SeekableStreamEncoder*)client_data;
741         FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
742         const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
743         const unsigned min_framesize = metadata->data.stream_info.min_framesize;
744         const unsigned max_framesize = metadata->data.stream_info.max_framesize;
745         const unsigned bps = metadata->data.stream_info.bits_per_sample;
746
747         FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
748
749         /* We get called by the stream encoder when the encoding process
750          * has finished so that we can update the STREAMINFO and SEEKTABLE
751          * blocks.
752          */
753
754         (void)encoder; /* silence compiler warning about unused parameter */
755
756         /*@@@ reopen callback here?  The docs currently require user to open files in update mode from the start */
757
758         /* All this is based on intimate knowledge of the stream header
759          * layout, but a change to the header format that would break this
760          * would also break all streams encoded in the previous format.
761          */
762
763         /*
764          * Write MD5 signature
765          */
766         if(seekable_stream_encoder->private_->seek_callback(seekable_stream_encoder, 26, seekable_stream_encoder->private_->client_data) != FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK) {
767                 seekable_stream_encoder->protected_->state = FLAC__SEEKABLE_STREAM_ENCODER_SEEK_ERROR;
768                 return;
769         }
770         if(seekable_stream_encoder->private_->write_callback(seekable_stream_encoder, metadata->data.stream_info.md5sum, 16, 0, 0, seekable_stream_encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
771                 seekable_stream_encoder->protected_->state = FLAC__SEEKABLE_STREAM_ENCODER_WRITE_ERROR;
772                 return;
773         }
774
775         /*
776          * Write total samples
777          */
778         b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
779         b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
780         b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
781         b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
782         b[4] = (FLAC__byte)(samples & 0xFF);
783         if(seekable_stream_encoder->private_->seek_callback(seekable_stream_encoder, 21, seekable_stream_encoder->private_->client_data) != FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK) {
784                 seekable_stream_encoder->protected_->state = FLAC__SEEKABLE_STREAM_ENCODER_SEEK_ERROR;
785                 return;
786         }
787         if(seekable_stream_encoder->private_->write_callback(seekable_stream_encoder, b, 5, 0, 0, seekable_stream_encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
788                 seekable_stream_encoder->protected_->state = FLAC__SEEKABLE_STREAM_ENCODER_WRITE_ERROR;
789                 return;
790         }
791
792         /*
793          * Write min/max framesize
794          */
795         b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
796         b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
797         b[2] = (FLAC__byte)(min_framesize & 0xFF);
798         b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
799         b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
800         b[5] = (FLAC__byte)(max_framesize & 0xFF);
801         if(seekable_stream_encoder->private_->seek_callback(seekable_stream_encoder, 12, seekable_stream_encoder->private_->client_data) != FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK) {
802                 seekable_stream_encoder->protected_->state = FLAC__SEEKABLE_STREAM_ENCODER_SEEK_ERROR;
803                 return;
804         }
805         if(seekable_stream_encoder->private_->write_callback(seekable_stream_encoder, b, 6, 0, 0, seekable_stream_encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
806                 seekable_stream_encoder->protected_->state = FLAC__SEEKABLE_STREAM_ENCODER_WRITE_ERROR;
807                 return;
808         }
809
810         /*
811          * Write seektable
812          */
813         if(0 != seekable_stream_encoder->private_->seek_table && seekable_stream_encoder->private_->seek_table->num_points > 0 && seekable_stream_encoder->private_->seektable_offset > 0) {
814                 unsigned i;
815
816                 FLAC__format_seektable_sort(seekable_stream_encoder->private_->seek_table);
817
818                 FLAC__ASSERT(FLAC__format_seektable_is_legal(seekable_stream_encoder->private_->seek_table));
819
820                 if(seekable_stream_encoder->private_->seek_callback(seekable_stream_encoder, seekable_stream_encoder->private_->seektable_offset + FLAC__STREAM_METADATA_HEADER_LENGTH, seekable_stream_encoder->private_->client_data) != FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK) {
821                         seekable_stream_encoder->protected_->state = FLAC__SEEKABLE_STREAM_ENCODER_SEEK_ERROR;
822                         return;
823                 }
824
825                 for(i = 0; i < seekable_stream_encoder->private_->seek_table->num_points; i++) {
826                         FLAC__uint64 xx;
827                         unsigned x;
828                         xx = seekable_stream_encoder->private_->seek_table->points[i].sample_number;
829                         b[7] = (FLAC__byte)xx; xx >>= 8;
830                         b[6] = (FLAC__byte)xx; xx >>= 8;
831                         b[5] = (FLAC__byte)xx; xx >>= 8;
832                         b[4] = (FLAC__byte)xx; xx >>= 8;
833                         b[3] = (FLAC__byte)xx; xx >>= 8;
834                         b[2] = (FLAC__byte)xx; xx >>= 8;
835                         b[1] = (FLAC__byte)xx; xx >>= 8;
836                         b[0] = (FLAC__byte)xx; xx >>= 8;
837                         xx = seekable_stream_encoder->private_->seek_table->points[i].stream_offset;
838                         b[15] = (FLAC__byte)xx; xx >>= 8;
839                         b[14] = (FLAC__byte)xx; xx >>= 8;
840                         b[13] = (FLAC__byte)xx; xx >>= 8;
841                         b[12] = (FLAC__byte)xx; xx >>= 8;
842                         b[11] = (FLAC__byte)xx; xx >>= 8;
843                         b[10] = (FLAC__byte)xx; xx >>= 8;
844                         b[9] = (FLAC__byte)xx; xx >>= 8;
845                         b[8] = (FLAC__byte)xx; xx >>= 8;
846                         x = seekable_stream_encoder->private_->seek_table->points[i].frame_samples;
847                         b[17] = (FLAC__byte)x; x >>= 8;
848                         b[16] = (FLAC__byte)x; x >>= 8;
849                         if(seekable_stream_encoder->private_->write_callback(seekable_stream_encoder, b, 18, 0, 0, seekable_stream_encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
850                                 seekable_stream_encoder->protected_->state = FLAC__SEEKABLE_STREAM_ENCODER_WRITE_ERROR;
851                                 return;
852                         }
853                 }
854         }
855 }