~ubuntu-branches/ubuntu/precise/flac/precise-updates

« back to all changes in this revision

Viewing changes to src/libFLAC/stream_decoder.c

  • Committer: Bazaar Package Importer
  • Author(s): Joshua Kwan
  • Date: 2007-05-29 22:56:36 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070529225636-ljeff8xxip09qaap
Tags: 1.1.4-1
* New upstream release. closes: #405167, #411311
  - libOggFLAC and libOggFLAC++ have been merged into libFLAC, so
    remove their corresponding packages.
  - Because of the API changes required to effect the above, there has
    been yet another soname bump. libflac7 -> libflac8 and
    libflac++5 -> libflac++6. Emails have been dispatched to the
    maintainers of dependent packages.
* Some notes on patches that were removed:
  - 02_stdin_stdout, 06_manpage_mention_utf8_convert: merged upstream
  - 08_manpage_warnings: Upstream has changed the manpage so it defintely
    can't fit in in 80 cols, so just forget about it. We'll live.
  - 05_eof_warnings_are_errors: Upstream decided to add a -w option to
    flac to treat all warnings as errors. I am going to defer to that
    for now, but if people think it's stupid let me know and I'll port
    the patch forward.
  - 04_stack_smasher: was a backport from 1.1.3, so it's obsolete.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* libFLAC - Free Lossless Audio Codec library
2
 
 * Copyright (C) 2000,2001,2002,2003,2004,2005  Josh Coalson
 
2
 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson
3
3
 *
4
4
 * Redistribution and use in source and binary forms, with or without
5
5
 * modification, are permitted provided that the following conditions
29
29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
30
 */
31
31
 
 
32
#if HAVE_CONFIG_H
 
33
#  include <config.h>
 
34
#endif
 
35
 
 
36
#if defined _MSC_VER || defined __MINGW32__
 
37
#include <io.h> /* for _setmode() */
 
38
#include <fcntl.h> /* for _O_BINARY */
 
39
#endif
 
40
#if defined __CYGWIN__ || defined __EMX__
 
41
#include <io.h> /* for setmode(), O_BINARY */
 
42
#include <fcntl.h> /* for _O_BINARY */
 
43
#endif
32
44
#include <stdio.h>
33
45
#include <stdlib.h> /* for malloc() */
34
46
#include <string.h> /* for memset/memcpy() */
 
47
#include <sys/stat.h> /* for stat() */
 
48
#include <sys/types.h> /* for off_t */
 
49
#if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__
 
50
#if _MSC_VER <= 1600 || defined __BORLANDC__ /* @@@ [2G limit] */
 
51
#define fseeko fseek
 
52
#define ftello ftell
 
53
#endif
 
54
#endif
35
55
#include "FLAC/assert.h"
36
56
#include "protected/stream_decoder.h"
37
 
#include "private/bitbuffer.h"
 
57
#include "private/bitreader.h"
38
58
#include "private/bitmath.h"
39
59
#include "private/cpu.h"
40
60
#include "private/crc.h"
41
61
#include "private/fixed.h"
42
62
#include "private/format.h"
43
63
#include "private/lpc.h"
 
64
#include "private/md5.h"
44
65
#include "private/memory.h"
45
66
 
46
 
#ifdef HAVE_CONFIG_H
47
 
#include <config.h>
48
 
#endif
49
 
 
50
67
#ifdef max
51
68
#undef max
52
69
#endif
59
76
#define FLAC__U64L(x) x##LLU
60
77
#endif
61
78
 
 
79
 
 
80
/* technically this should be in an "export.c" but this is convenient enough */
 
81
FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC =
 
82
#if FLAC__HAS_OGG
 
83
        1
 
84
#else
 
85
        0
 
86
#endif
 
87
;
 
88
 
 
89
 
62
90
/***********************************************************************
63
91
 *
64
92
 * Private static data
74
102
 ***********************************************************************/
75
103
 
76
104
static void set_defaults_(FLAC__StreamDecoder *decoder);
 
105
static FILE *get_binary_stdin_(void);
77
106
static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
78
107
static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
79
108
static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
82
111
static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
83
112
static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj);
84
113
static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
 
114
static FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj);
85
115
static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
86
116
static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
87
117
static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode);
93
123
static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
94
124
static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual);
95
125
static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
96
 
static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
 
126
static FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data);
 
127
#if FLAC__HAS_OGG
 
128
static FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes);
 
129
static FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
 
130
#endif
 
131
static FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
 
132
static void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status);
 
133
static FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
 
134
#if FLAC__HAS_OGG
 
135
static FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
 
136
#endif
 
137
static FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
 
138
static FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
 
139
static FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
 
140
static FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
 
141
static FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data);
97
142
 
98
143
/***********************************************************************
99
144
 *
102
147
 ***********************************************************************/
103
148
 
104
149
typedef struct FLAC__StreamDecoderPrivate {
 
150
#if FLAC__HAS_OGG
 
151
        FLAC__bool is_ogg;
 
152
#endif
105
153
        FLAC__StreamDecoderReadCallback read_callback;
 
154
        FLAC__StreamDecoderSeekCallback seek_callback;
 
155
        FLAC__StreamDecoderTellCallback tell_callback;
 
156
        FLAC__StreamDecoderLengthCallback length_callback;
 
157
        FLAC__StreamDecoderEofCallback eof_callback;
106
158
        FLAC__StreamDecoderWriteCallback write_callback;
107
159
        FLAC__StreamDecoderMetadataCallback metadata_callback;
108
160
        FLAC__StreamDecoderErrorCallback error_callback;
115
167
        /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit), AND order <= 8: */
116
168
        void (*local_lpc_restore_signal_16bit_order8)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
117
169
        void *client_data;
118
 
        FLAC__BitBuffer *input;
 
170
        FILE *file; /* only used if FLAC__stream_decoder_init_file()/FLAC__stream_decoder_init_file() called, else NULL */
 
171
        FLAC__BitReader *input;
119
172
        FLAC__int32 *output[FLAC__MAX_CHANNELS];
120
173
        FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */
121
174
        FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
136
189
        FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
137
190
        /* unaligned (original) pointers to allocated data */
138
191
        FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS];
 
192
        FLAC__bool do_md5_checking; /* initially gets protected_->md5_checking but is turned off after a seek or if the metadata has a zero MD5 */
 
193
        FLAC__bool internal_reset_hack; /* used only during init() so we can call reset to set up the decoder without rewinding the input */
 
194
        FLAC__bool is_seeking;
 
195
        struct FLAC__MD5Context md5context;
 
196
        FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */
 
197
        /* (the rest of these are only used for seeking) */
 
198
        FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */
 
199
        FLAC__uint64 first_frame_offset; /* hint to the seek routine of where in the stream the first audio frame starts */
 
200
        FLAC__uint64 target_sample;
 
201
        unsigned unparseable_frame_count; /* used to tell whether we're decoding a future version of FLAC or just got a bad sync */
 
202
#if FLAC__HAS_OGG
 
203
        FLAC__bool got_a_frame; /* hack needed in Ogg FLAC seek routine to check when process_single() actually writes a frame */
 
204
#endif
139
205
} FLAC__StreamDecoderPrivate;
140
206
 
141
207
/***********************************************************************
150
216
        "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
151
217
        "FLAC__STREAM_DECODER_READ_FRAME",
152
218
        "FLAC__STREAM_DECODER_END_OF_STREAM",
 
219
        "FLAC__STREAM_DECODER_OGG_ERROR",
 
220
        "FLAC__STREAM_DECODER_SEEK_ERROR",
153
221
        "FLAC__STREAM_DECODER_ABORTED",
154
 
        "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
155
222
        "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
156
 
        "FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
157
 
        "FLAC__STREAM_DECODER_INVALID_CALLBACK",
158
223
        "FLAC__STREAM_DECODER_UNINITIALIZED"
159
224
};
160
225
 
 
226
FLAC_API const char * const FLAC__StreamDecoderInitStatusString[] = {
 
227
        "FLAC__STREAM_DECODER_INIT_STATUS_OK",
 
228
        "FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
 
229
        "FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS",
 
230
        "FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR",
 
231
        "FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE",
 
232
        "FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED"
 
233
};
 
234
 
161
235
FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
162
236
        "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
163
237
        "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
164
238
        "FLAC__STREAM_DECODER_READ_STATUS_ABORT"
165
239
};
166
240
 
 
241
FLAC_API const char * const FLAC__StreamDecoderSeekStatusString[] = {
 
242
        "FLAC__STREAM_DECODER_SEEK_STATUS_OK",
 
243
        "FLAC__STREAM_DECODER_SEEK_STATUS_ERROR",
 
244
        "FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED"
 
245
};
 
246
 
 
247
FLAC_API const char * const FLAC__StreamDecoderTellStatusString[] = {
 
248
        "FLAC__STREAM_DECODER_TELL_STATUS_OK",
 
249
        "FLAC__STREAM_DECODER_TELL_STATUS_ERROR",
 
250
        "FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED"
 
251
};
 
252
 
 
253
FLAC_API const char * const FLAC__StreamDecoderLengthStatusString[] = {
 
254
        "FLAC__STREAM_DECODER_LENGTH_STATUS_OK",
 
255
        "FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR",
 
256
        "FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED"
 
257
};
 
258
 
167
259
FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
168
260
        "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
169
261
        "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
172
264
FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
173
265
        "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
174
266
        "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
175
 
        "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH"
 
267
        "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH",
 
268
        "FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM"
176
269
};
177
270
 
178
271
/***********************************************************************
180
273
 * Class constructor/destructor
181
274
 *
182
275
 ***********************************************************************/
183
 
FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new()
 
276
FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new(void)
184
277
{
185
278
        FLAC__StreamDecoder *decoder;
186
279
        unsigned i;
205
298
                return 0;
206
299
        }
207
300
 
208
 
        decoder->private_->input = FLAC__bitbuffer_new();
 
301
        decoder->private_->input = FLAC__bitreader_new();
209
302
        if(decoder->private_->input == 0) {
210
303
                free(decoder->private_);
211
304
                free(decoder->protected_);
215
308
 
216
309
        decoder->private_->metadata_filter_ids_capacity = 16;
217
310
        if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
218
 
                FLAC__bitbuffer_delete(decoder->private_->input);
 
311
                FLAC__bitreader_delete(decoder->private_->input);
219
312
                free(decoder->private_);
220
313
                free(decoder->protected_);
221
314
                free(decoder);
234
327
        for(i = 0; i < FLAC__MAX_CHANNELS; i++)
235
328
                FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
236
329
 
 
330
        decoder->private_->file = 0;
 
331
 
237
332
        set_defaults_(decoder);
238
333
 
239
334
        decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
250
345
        FLAC__ASSERT(0 != decoder->private_);
251
346
        FLAC__ASSERT(0 != decoder->private_->input);
252
347
 
253
 
        FLAC__stream_decoder_finish(decoder);
 
348
        (void)FLAC__stream_decoder_finish(decoder);
254
349
 
255
350
        if(0 != decoder->private_->metadata_filter_ids)
256
351
                free(decoder->private_->metadata_filter_ids);
257
352
 
258
 
        FLAC__bitbuffer_delete(decoder->private_->input);
 
353
        FLAC__bitreader_delete(decoder->private_->input);
259
354
 
260
355
        for(i = 0; i < FLAC__MAX_CHANNELS; i++)
261
356
                FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
271
366
 *
272
367
 ***********************************************************************/
273
368
 
274
 
FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
 
369
static FLAC__StreamDecoderInitStatus init_stream_internal_(
 
370
        FLAC__StreamDecoder *decoder,
 
371
        FLAC__StreamDecoderReadCallback read_callback,
 
372
        FLAC__StreamDecoderSeekCallback seek_callback,
 
373
        FLAC__StreamDecoderTellCallback tell_callback,
 
374
        FLAC__StreamDecoderLengthCallback length_callback,
 
375
        FLAC__StreamDecoderEofCallback eof_callback,
 
376
        FLAC__StreamDecoderWriteCallback write_callback,
 
377
        FLAC__StreamDecoderMetadataCallback metadata_callback,
 
378
        FLAC__StreamDecoderErrorCallback error_callback,
 
379
        void *client_data,
 
380
        FLAC__bool is_ogg
 
381
)
275
382
{
276
383
        FLAC__ASSERT(0 != decoder);
277
384
 
278
385
        if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
279
 
                return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
280
 
 
281
 
        if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
282
 
                return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
283
 
 
284
 
        if(!FLAC__bitbuffer_init(decoder->private_->input))
285
 
                return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
286
 
 
287
 
        decoder->private_->last_frame_number = 0;
288
 
        decoder->private_->last_block_size = 0;
289
 
        decoder->private_->samples_decoded = 0;
290
 
        decoder->private_->has_stream_info = false;
291
 
        decoder->private_->cached = false;
 
386
                return FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
 
387
 
 
388
#if !FLAC__HAS_OGG
 
389
        if(is_ogg)
 
390
                return FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
 
391
#endif
 
392
 
 
393
        if(
 
394
                0 == read_callback ||
 
395
                0 == write_callback ||
 
396
                0 == error_callback ||
 
397
                (seek_callback && (0 == tell_callback || 0 == length_callback || 0 == eof_callback))
 
398
        )
 
399
                return FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
 
400
 
 
401
#if FLAC__HAS_OGG
 
402
        decoder->private_->is_ogg = is_ogg;
 
403
        if(is_ogg && !FLAC__ogg_decoder_aspect_init(&decoder->protected_->ogg_decoder_aspect))
 
404
                return decoder->protected_->state = FLAC__STREAM_DECODER_OGG_ERROR;
 
405
#endif
292
406
 
293
407
        /*
294
408
         * get the CPU info and set the function pointers
326
440
        }
327
441
#endif
328
442
 
329
 
        if(!FLAC__stream_decoder_reset(decoder))
330
 
                return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
331
 
 
332
 
        return decoder->protected_->state;
333
 
}
334
 
 
335
 
FLAC_API void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
336
 
{
 
443
        /* from here on, errors are fatal */
 
444
 
 
445
        if(!FLAC__bitreader_init(decoder->private_->input, read_callback_, decoder)) {
 
446
                decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
 
447
                return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
 
448
        }
 
449
 
 
450
        decoder->private_->read_callback = read_callback;
 
451
        decoder->private_->seek_callback = seek_callback;
 
452
        decoder->private_->tell_callback = tell_callback;
 
453
        decoder->private_->length_callback = length_callback;
 
454
        decoder->private_->eof_callback = eof_callback;
 
455
        decoder->private_->write_callback = write_callback;
 
456
        decoder->private_->metadata_callback = metadata_callback;
 
457
        decoder->private_->error_callback = error_callback;
 
458
        decoder->private_->client_data = client_data;
 
459
        decoder->private_->last_frame_number = 0;
 
460
        decoder->private_->last_block_size = 0;
 
461
        decoder->private_->samples_decoded = 0;
 
462
        decoder->private_->has_stream_info = false;
 
463
        decoder->private_->cached = false;
 
464
 
 
465
        decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
 
466
        decoder->private_->is_seeking = false;
 
467
 
 
468
        decoder->private_->internal_reset_hack = true; /* so the following reset does not try to rewind the input */
 
469
        if(!FLAC__stream_decoder_reset(decoder)) {
 
470
                /* above call sets the state for us */
 
471
                return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
 
472
        }
 
473
 
 
474
        return FLAC__STREAM_DECODER_INIT_STATUS_OK;
 
475
}
 
476
 
 
477
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream(
 
478
        FLAC__StreamDecoder *decoder,
 
479
        FLAC__StreamDecoderReadCallback read_callback,
 
480
        FLAC__StreamDecoderSeekCallback seek_callback,
 
481
        FLAC__StreamDecoderTellCallback tell_callback,
 
482
        FLAC__StreamDecoderLengthCallback length_callback,
 
483
        FLAC__StreamDecoderEofCallback eof_callback,
 
484
        FLAC__StreamDecoderWriteCallback write_callback,
 
485
        FLAC__StreamDecoderMetadataCallback metadata_callback,
 
486
        FLAC__StreamDecoderErrorCallback error_callback,
 
487
        void *client_data
 
488
)
 
489
{
 
490
        return init_stream_internal_(
 
491
                decoder,
 
492
                read_callback,
 
493
                seek_callback,
 
494
                tell_callback,
 
495
                length_callback,
 
496
                eof_callback,
 
497
                write_callback,
 
498
                metadata_callback,
 
499
                error_callback,
 
500
                client_data,
 
501
                /*is_ogg=*/false
 
502
        );
 
503
}
 
504
 
 
505
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_stream(
 
506
        FLAC__StreamDecoder *decoder,
 
507
        FLAC__StreamDecoderReadCallback read_callback,
 
508
        FLAC__StreamDecoderSeekCallback seek_callback,
 
509
        FLAC__StreamDecoderTellCallback tell_callback,
 
510
        FLAC__StreamDecoderLengthCallback length_callback,
 
511
        FLAC__StreamDecoderEofCallback eof_callback,
 
512
        FLAC__StreamDecoderWriteCallback write_callback,
 
513
        FLAC__StreamDecoderMetadataCallback metadata_callback,
 
514
        FLAC__StreamDecoderErrorCallback error_callback,
 
515
        void *client_data
 
516
)
 
517
{
 
518
        return init_stream_internal_(
 
519
                decoder,
 
520
                read_callback,
 
521
                seek_callback,
 
522
                tell_callback,
 
523
                length_callback,
 
524
                eof_callback,
 
525
                write_callback,
 
526
                metadata_callback,
 
527
                error_callback,
 
528
                client_data,
 
529
                /*is_ogg=*/true
 
530
        );
 
531
}
 
532
 
 
533
static FLAC__StreamDecoderInitStatus init_FILE_internal_(
 
534
        FLAC__StreamDecoder *decoder,
 
535
        FILE *file,
 
536
        FLAC__StreamDecoderWriteCallback write_callback,
 
537
        FLAC__StreamDecoderMetadataCallback metadata_callback,
 
538
        FLAC__StreamDecoderErrorCallback error_callback,
 
539
        void *client_data,
 
540
        FLAC__bool is_ogg
 
541
)
 
542
{
 
543
        FLAC__ASSERT(0 != decoder);
 
544
        FLAC__ASSERT(0 != file);
 
545
 
 
546
        if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
 
547
                return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
 
548
 
 
549
        if(0 == write_callback || 0 == error_callback)
 
550
                return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
 
551
 
 
552
        /*
 
553
         * To make sure that our file does not go unclosed after an error, we
 
554
         * must assign the FILE pointer before any further error can occur in
 
555
         * this routine.
 
556
         */
 
557
        if(file == stdin)
 
558
                file = get_binary_stdin_(); /* just to be safe */
 
559
 
 
560
        decoder->private_->file = file;
 
561
 
 
562
        return init_stream_internal_(
 
563
                decoder,
 
564
                file_read_callback_,
 
565
                decoder->private_->file == stdin? 0: file_seek_callback_,
 
566
                decoder->private_->file == stdin? 0: file_tell_callback_,
 
567
                decoder->private_->file == stdin? 0: file_length_callback_,
 
568
                file_eof_callback_,
 
569
                write_callback,
 
570
                metadata_callback,
 
571
                error_callback,
 
572
                client_data,
 
573
                is_ogg
 
574
        );
 
575
}
 
576
 
 
577
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE(
 
578
        FLAC__StreamDecoder *decoder,
 
579
        FILE *file,
 
580
        FLAC__StreamDecoderWriteCallback write_callback,
 
581
        FLAC__StreamDecoderMetadataCallback metadata_callback,
 
582
        FLAC__StreamDecoderErrorCallback error_callback,
 
583
        void *client_data
 
584
)
 
585
{
 
586
        return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
 
587
}
 
588
 
 
589
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE(
 
590
        FLAC__StreamDecoder *decoder,
 
591
        FILE *file,
 
592
        FLAC__StreamDecoderWriteCallback write_callback,
 
593
        FLAC__StreamDecoderMetadataCallback metadata_callback,
 
594
        FLAC__StreamDecoderErrorCallback error_callback,
 
595
        void *client_data
 
596
)
 
597
{
 
598
        return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
 
599
}
 
600
 
 
601
static FLAC__StreamDecoderInitStatus init_file_internal_(
 
602
        FLAC__StreamDecoder *decoder,
 
603
        const char *filename,
 
604
        FLAC__StreamDecoderWriteCallback write_callback,
 
605
        FLAC__StreamDecoderMetadataCallback metadata_callback,
 
606
        FLAC__StreamDecoderErrorCallback error_callback,
 
607
        void *client_data,
 
608
        FLAC__bool is_ogg
 
609
)
 
610
{
 
611
        FILE *file;
 
612
 
 
613
        FLAC__ASSERT(0 != decoder);
 
614
 
 
615
        /*
 
616
         * To make sure that our file does not go unclosed after an error, we
 
617
         * have to do the same entrance checks here that are later performed
 
618
         * in FLAC__stream_decoder_init_FILE() before the FILE* is assigned.
 
619
         */
 
620
        if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
 
621
                return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
 
622
 
 
623
        if(0 == write_callback || 0 == error_callback)
 
624
                return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
 
625
 
 
626
        file = filename? fopen(filename, "rb") : stdin;
 
627
 
 
628
        if(0 == file)
 
629
                return FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE;
 
630
 
 
631
        return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, is_ogg);
 
632
}
 
633
 
 
634
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file(
 
635
        FLAC__StreamDecoder *decoder,
 
636
        const char *filename,
 
637
        FLAC__StreamDecoderWriteCallback write_callback,
 
638
        FLAC__StreamDecoderMetadataCallback metadata_callback,
 
639
        FLAC__StreamDecoderErrorCallback error_callback,
 
640
        void *client_data
 
641
)
 
642
{
 
643
        return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
 
644
}
 
645
 
 
646
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_file(
 
647
        FLAC__StreamDecoder *decoder,
 
648
        const char *filename,
 
649
        FLAC__StreamDecoderWriteCallback write_callback,
 
650
        FLAC__StreamDecoderMetadataCallback metadata_callback,
 
651
        FLAC__StreamDecoderErrorCallback error_callback,
 
652
        void *client_data
 
653
)
 
654
{
 
655
        return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
 
656
}
 
657
 
 
658
FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
 
659
{
 
660
        FLAC__bool md5_failed = false;
337
661
        unsigned i;
 
662
 
338
663
        FLAC__ASSERT(0 != decoder);
 
664
        FLAC__ASSERT(0 != decoder->private_);
 
665
        FLAC__ASSERT(0 != decoder->protected_);
 
666
 
339
667
        if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
340
 
                return;
341
 
        if(0 != decoder->private_->seek_table.data.seek_table.points) {
 
668
                return true;
 
669
 
 
670
        /* see the comment in FLAC__seekable_stream_decoder_reset() as to why we
 
671
         * always call FLAC__MD5Final()
 
672
         */
 
673
        FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
 
674
 
 
675
        if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
342
676
                free(decoder->private_->seek_table.data.seek_table.points);
343
677
                decoder->private_->seek_table.data.seek_table.points = 0;
344
678
                decoder->private_->has_seek_table = false;
345
679
        }
346
 
        FLAC__bitbuffer_free(decoder->private_->input);
 
680
        FLAC__bitreader_free(decoder->private_->input);
347
681
        for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
348
682
                /* WATCHOUT:
349
683
                 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
363
697
        decoder->private_->output_capacity = 0;
364
698
        decoder->private_->output_channels = 0;
365
699
 
 
700
#if FLAC__HAS_OGG
 
701
        if(decoder->private_->is_ogg)
 
702
                FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect);
 
703
#endif
 
704
 
 
705
        if(0 != decoder->private_->file) {
 
706
                if(decoder->private_->file != stdin)
 
707
                        fclose(decoder->private_->file);
 
708
                decoder->private_->file = 0;
 
709
        }
 
710
 
 
711
        if(decoder->private_->do_md5_checking) {
 
712
                if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16))
 
713
                        md5_failed = true;
 
714
        }
 
715
        decoder->private_->is_seeking = false;
 
716
 
366
717
        set_defaults_(decoder);
367
718
 
368
719
        decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
369
 
}
370
 
 
371
 
FLAC_API FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
372
 
{
373
 
        FLAC__ASSERT(0 != decoder);
374
 
        FLAC__ASSERT(0 != decoder->private_);
375
 
        FLAC__ASSERT(0 != decoder->protected_);
376
 
        if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
377
 
                return false;
378
 
        decoder->private_->read_callback = value;
379
 
        return true;
380
 
}
381
 
 
382
 
FLAC_API FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
383
 
{
384
 
        FLAC__ASSERT(0 != decoder);
385
 
        FLAC__ASSERT(0 != decoder->private_);
386
 
        FLAC__ASSERT(0 != decoder->protected_);
387
 
        if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
388
 
                return false;
389
 
        decoder->private_->write_callback = value;
390
 
        return true;
391
 
}
392
 
 
393
 
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value)
394
 
{
395
 
        FLAC__ASSERT(0 != decoder);
396
 
        FLAC__ASSERT(0 != decoder->private_);
397
 
        FLAC__ASSERT(0 != decoder->protected_);
398
 
        if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
399
 
                return false;
400
 
        decoder->private_->metadata_callback = value;
401
 
        return true;
402
 
}
403
 
 
404
 
FLAC_API FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorCallback value)
405
 
{
406
 
        FLAC__ASSERT(0 != decoder);
407
 
        FLAC__ASSERT(0 != decoder->private_);
408
 
        FLAC__ASSERT(0 != decoder->protected_);
409
 
        if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
410
 
                return false;
411
 
        decoder->private_->error_callback = value;
412
 
        return true;
413
 
}
414
 
 
415
 
FLAC_API FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value)
416
 
{
417
 
        FLAC__ASSERT(0 != decoder);
418
 
        FLAC__ASSERT(0 != decoder->private_);
419
 
        FLAC__ASSERT(0 != decoder->protected_);
420
 
        if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
421
 
                return false;
422
 
        decoder->private_->client_data = value;
 
720
 
 
721
        return !md5_failed;
 
722
}
 
723
 
 
724
FLAC_API FLAC__bool FLAC__stream_decoder_set_ogg_serial_number(FLAC__StreamDecoder *decoder, long value)
 
725
{
 
726
        FLAC__ASSERT(0 != decoder);
 
727
        FLAC__ASSERT(0 != decoder->private_);
 
728
        FLAC__ASSERT(0 != decoder->protected_);
 
729
        if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
 
730
                return false;
 
731
#if FLAC__HAS_OGG
 
732
        /* can't check decoder->private_->is_ogg since that's not set until init time */
 
733
        FLAC__ogg_decoder_aspect_set_serial_number(&decoder->protected_->ogg_decoder_aspect, value);
 
734
        return true;
 
735
#else
 
736
        (void)value;
 
737
        return false;
 
738
#endif
 
739
}
 
740
 
 
741
FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value)
 
742
{
 
743
        FLAC__ASSERT(0 != decoder);
 
744
        FLAC__ASSERT(0 != decoder->protected_);
 
745
        if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
 
746
                return false;
 
747
        decoder->protected_->md5_checking = value;
423
748
        return true;
424
749
}
425
750
 
455
780
        FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
456
781
 
457
782
        if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
458
 
                if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
459
 
                        return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
 
783
                if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) {
 
784
                        decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
 
785
                        return false;
 
786
                }
460
787
                decoder->private_->metadata_filter_ids_capacity *= 2;
461
788
        }
462
789
 
512
839
        FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
513
840
 
514
841
        if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
515
 
                if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
516
 
                        return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
 
842
                if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) {
 
843
                        decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
 
844
                        return false;
 
845
                }
517
846
                decoder->private_->metadata_filter_ids_capacity *= 2;
518
847
        }
519
848
 
547
876
        return FLAC__StreamDecoderStateString[decoder->protected_->state];
548
877
}
549
878
 
 
879
FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder)
 
880
{
 
881
        FLAC__ASSERT(0 != decoder);
 
882
        FLAC__ASSERT(0 != decoder->protected_);
 
883
        return decoder->protected_->md5_checking;
 
884
}
 
885
 
 
886
FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder)
 
887
{
 
888
        FLAC__ASSERT(0 != decoder);
 
889
        FLAC__ASSERT(0 != decoder->protected_);
 
890
        return decoder->private_->has_stream_info? decoder->private_->stream_info.data.stream_info.total_samples : 0;
 
891
}
 
892
 
550
893
FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
551
894
{
552
895
        FLAC__ASSERT(0 != decoder);
582
925
        return decoder->protected_->blocksize;
583
926
}
584
927
 
 
928
FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position)
 
929
{
 
930
        FLAC__ASSERT(0 != decoder);
 
931
        FLAC__ASSERT(0 != decoder->private_);
 
932
        FLAC__ASSERT(0 != position);
 
933
 
 
934
#if FLAC__HAS_OGG
 
935
        if(decoder->private_->is_ogg)
 
936
                return false;
 
937
#endif
 
938
        if(0 == decoder->private_->tell_callback)
 
939
                return false;
 
940
        if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK)
 
941
                return false;
 
942
        /* should never happen since all FLAC frames and metadata blocks are byte aligned, but check just in case */
 
943
        if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input))
 
944
                return false;
 
945
        FLAC__ASSERT(*position >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder));
 
946
        *position -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder);
 
947
        return true;
 
948
}
 
949
 
585
950
FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
586
951
{
587
952
        FLAC__ASSERT(0 != decoder);
588
953
        FLAC__ASSERT(0 != decoder->private_);
589
954
        FLAC__ASSERT(0 != decoder->protected_);
590
955
 
591
 
        if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
 
956
        decoder->private_->samples_decoded = 0;
 
957
        decoder->private_->do_md5_checking = false;
 
958
 
 
959
#if FLAC__HAS_OGG
 
960
        if(decoder->private_->is_ogg)
 
961
                FLAC__ogg_decoder_aspect_flush(&decoder->protected_->ogg_decoder_aspect);
 
962
#endif
 
963
 
 
964
        if(!FLAC__bitreader_clear(decoder->private_->input)) {
592
965
                decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
593
966
                return false;
594
967
        }
606
979
        FLAC__ASSERT(0 != decoder->protected_);
607
980
 
608
981
        if(!FLAC__stream_decoder_flush(decoder)) {
609
 
                decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
 
982
                /* above call sets the state for us */
610
983
                return false;
611
984
        }
 
985
 
 
986
#if FLAC__HAS_OGG
 
987
        /*@@@ could go in !internal_reset_hack block below */
 
988
        if(decoder->private_->is_ogg)
 
989
                FLAC__ogg_decoder_aspect_reset(&decoder->protected_->ogg_decoder_aspect);
 
990
#endif
 
991
 
 
992
        /* Rewind if necessary.  If FLAC__stream_decoder_init() is calling us,
 
993
         * (internal_reset_hack) don't try to rewind since we are already at
 
994
         * the beginning of the stream and don't want to fail if the input is
 
995
         * not seekable.
 
996
         */
 
997
        if(!decoder->private_->internal_reset_hack) {
 
998
                if(decoder->private_->file == stdin)
 
999
                        return false; /* can't rewind stdin, reset fails */
 
1000
                if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR)
 
1001
                        return false; /* seekable and seek fails, reset fails */
 
1002
        }
 
1003
        else
 
1004
                decoder->private_->internal_reset_hack = false;
 
1005
 
612
1006
        decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
613
1007
 
614
 
        decoder->private_->samples_decoded = 0;
 
1008
        decoder->private_->has_stream_info = false;
 
1009
        if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
 
1010
                free(decoder->private_->seek_table.data.seek_table.points);
 
1011
                decoder->private_->seek_table.data.seek_table.points = 0;
 
1012
                decoder->private_->has_seek_table = false;
 
1013
        }
 
1014
        decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
 
1015
 
 
1016
        /* We initialize the FLAC__MD5Context even though we may never use it.  This
 
1017
         * is because md5 checking may be turned on to start and then turned off if
 
1018
         * a seek occurs.  So we init the context here and finalize it in
 
1019
         * FLAC__stream_decoder_finish() to make sure things are always cleaned up
 
1020
         * properly.
 
1021
         */
 
1022
        FLAC__MD5Init(&decoder->private_->md5context);
 
1023
 
 
1024
        decoder->private_->first_frame_offset = 0;
 
1025
        decoder->private_->unparseable_frame_count = 0;
615
1026
 
616
1027
        return true;
617
1028
}
745
1156
        }
746
1157
}
747
1158
 
 
1159
FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample)
 
1160
{
 
1161
        FLAC__uint64 length;
 
1162
 
 
1163
        FLAC__ASSERT(0 != decoder);
 
1164
 
 
1165
        if(
 
1166
                decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA &&
 
1167
                decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA &&
 
1168
                decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC &&
 
1169
                decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME &&
 
1170
                decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM
 
1171
        )
 
1172
                return false;
 
1173
 
 
1174
        if(0 == decoder->private_->seek_callback)
 
1175
                return false;
 
1176
 
 
1177
        FLAC__ASSERT(decoder->private_->seek_callback);
 
1178
        FLAC__ASSERT(decoder->private_->tell_callback);
 
1179
        FLAC__ASSERT(decoder->private_->length_callback);
 
1180
        FLAC__ASSERT(decoder->private_->eof_callback);
 
1181
 
 
1182
        if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder))
 
1183
                return false;
 
1184
 
 
1185
        decoder->private_->is_seeking = true;
 
1186
 
 
1187
        /* turn off md5 checking if a seek is attempted */
 
1188
        decoder->private_->do_md5_checking = false;
 
1189
 
 
1190
        /* get the file length (currently our algorithm needs to know the length so it's also an error to get FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED) */
 
1191
        if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) {
 
1192
                decoder->private_->is_seeking = false;
 
1193
                return false;
 
1194
        }
 
1195
 
 
1196
        /* if we haven't finished processing the metadata yet, do that so we have the STREAMINFO, SEEK_TABLE, and first_frame_offset */
 
1197
        if(
 
1198
                decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA ||
 
1199
                decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA
 
1200
        ) {
 
1201
                if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) {
 
1202
                        /* above call sets the state for us */
 
1203
                        decoder->private_->is_seeking = false;
 
1204
                        return false;
 
1205
                }
 
1206
                /* check this again in case we didn't know total_samples the first time */
 
1207
                if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) {
 
1208
                        decoder->private_->is_seeking = false;
 
1209
                        return false;
 
1210
                }
 
1211
        }
 
1212
 
 
1213
        {
 
1214
                const FLAC__bool ok =
 
1215
#if FLAC__HAS_OGG
 
1216
                        decoder->private_->is_ogg?
 
1217
                        seek_to_absolute_sample_ogg_(decoder, length, sample) :
 
1218
#endif
 
1219
                        seek_to_absolute_sample_(decoder, length, sample)
 
1220
                ;
 
1221
                decoder->private_->is_seeking = false;
 
1222
                return ok;
 
1223
        }
 
1224
}
 
1225
 
748
1226
/***********************************************************************
749
1227
 *
750
1228
 * Protected class methods
754
1232
unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
755
1233
{
756
1234
        FLAC__ASSERT(0 != decoder);
757
 
        return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
 
1235
        FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
 
1236
        FLAC__ASSERT(!(FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) & 7));
 
1237
        return FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) / 8;
758
1238
}
759
1239
 
760
1240
/***********************************************************************
765
1245
 
766
1246
void set_defaults_(FLAC__StreamDecoder *decoder)
767
1247
{
 
1248
#if FLAC__HAS_OGG
 
1249
        decoder->private_->is_ogg = false;
 
1250
#endif
768
1251
        decoder->private_->read_callback = 0;
 
1252
        decoder->private_->seek_callback = 0;
 
1253
        decoder->private_->tell_callback = 0;
 
1254
        decoder->private_->length_callback = 0;
 
1255
        decoder->private_->eof_callback = 0;
769
1256
        decoder->private_->write_callback = 0;
770
1257
        decoder->private_->metadata_callback = 0;
771
1258
        decoder->private_->error_callback = 0;
774
1261
        memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
775
1262
        decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
776
1263
        decoder->private_->metadata_filter_ids_count = 0;
 
1264
 
 
1265
        decoder->protected_->md5_checking = false;
 
1266
 
 
1267
#if FLAC__HAS_OGG
 
1268
        FLAC__ogg_decoder_aspect_set_defaults(&decoder->protected_->ogg_decoder_aspect);
 
1269
#endif
 
1270
}
 
1271
 
 
1272
/*
 
1273
 * This will forcibly set stdin to binary mode (for OSes that require it)
 
1274
 */
 
1275
FILE *get_binary_stdin_(void)
 
1276
{
 
1277
        /* if something breaks here it is probably due to the presence or
 
1278
         * absence of an underscore before the identifiers 'setmode',
 
1279
         * 'fileno', and/or 'O_BINARY'; check your system header files.
 
1280
         */
 
1281
#if defined _MSC_VER || defined __MINGW32__
 
1282
        _setmode(_fileno(stdin), _O_BINARY);
 
1283
#elif defined __CYGWIN__ || defined __EMX__
 
1284
        /* almost certainly not needed for any modern Cygwin, but let's be safe... */
 
1285
        setmode(_fileno(stdin), _O_BINARY);
 
1286
#endif
 
1287
 
 
1288
        return stdin;
777
1289
}
778
1290
 
779
1291
FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
847
1359
        unsigned i, id;
848
1360
        FLAC__bool first = true;
849
1361
 
850
 
        FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
 
1362
        FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
851
1363
 
852
1364
        for(i = id = 0; i < 4; ) {
853
1365
                if(decoder->private_->cached) {
855
1367
                        decoder->private_->cached = false;
856
1368
                }
857
1369
                else {
858
 
                        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
859
 
                                return false; /* the read_callback_ sets the state for us */
 
1370
                        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
 
1371
                                return false; /* read_callback_ sets the state for us */
860
1372
                }
861
1373
                if(x == FLAC__STREAM_SYNC_STRING[i]) {
862
1374
                        first = true;
869
1381
                        i = 0;
870
1382
                        if(id == 3) {
871
1383
                                if(!skip_id3v2_tag_(decoder))
872
 
                                        return false; /* the read_callback_ sets the state for us */
 
1384
                                        return false; /* skip_id3v2_tag_ sets the state for us */
873
1385
                        }
874
1386
                        continue;
875
1387
                }
 
1388
                id = 0;
876
1389
                if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
877
1390
                        decoder->private_->header_warmup[0] = (FLAC__byte)x;
878
 
                        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
879
 
                                return false; /* the read_callback_ sets the state for us */
 
1391
                        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
 
1392
                                return false; /* read_callback_ sets the state for us */
880
1393
 
881
1394
                        /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
882
1395
                        /* else we have to check if the second byte is the end of a sync code */
892
1405
                }
893
1406
                i = 0;
894
1407
                if(first) {
895
 
                        decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
 
1408
                        send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
896
1409
                        first = false;
897
1410
                }
898
1411
        }
906
1419
        FLAC__bool is_last;
907
1420
        FLAC__uint32 i, x, type, length;
908
1421
 
909
 
        FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
 
1422
        FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
910
1423
 
911
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
912
 
                return false; /* the read_callback_ sets the state for us */
 
1424
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN))
 
1425
                return false; /* read_callback_ sets the state for us */
913
1426
        is_last = x? true : false;
914
1427
 
915
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
916
 
                return false; /* the read_callback_ sets the state for us */
 
1428
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN))
 
1429
                return false; /* read_callback_ sets the state for us */
917
1430
 
918
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
919
 
                return false; /* the read_callback_ sets the state for us */
 
1431
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN))
 
1432
                return false; /* read_callback_ sets the state for us */
920
1433
 
921
1434
        if(type == FLAC__METADATA_TYPE_STREAMINFO) {
922
1435
                if(!read_metadata_streaminfo_(decoder, is_last, length))
923
1436
                        return false;
924
1437
 
925
1438
                decoder->private_->has_stream_info = true;
926
 
                if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
 
1439
                if(0 == memcmp(decoder->private_->stream_info.data.stream_info.md5sum, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))
 
1440
                        decoder->private_->do_md5_checking = false;
 
1441
                if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] && decoder->private_->metadata_callback)
927
1442
                        decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
928
1443
        }
929
1444
        else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
931
1446
                        return false;
932
1447
 
933
1448
                decoder->private_->has_seek_table = true;
934
 
                if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE])
 
1449
                if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE] && decoder->private_->metadata_callback)
935
1450
                        decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
936
1451
        }
937
1452
        else {
944
1459
                block.length = length;
945
1460
 
946
1461
                if(type == FLAC__METADATA_TYPE_APPLICATION) {
947
 
                        if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8, read_callback_, decoder))
948
 
                                return false; /* the read_callback_ sets the state for us */
 
1462
                        if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))
 
1463
                                return false; /* read_callback_ sets the state for us */
949
1464
 
950
1465
                        real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
951
1466
 
954
1469
                }
955
1470
 
956
1471
                if(skip_it) {
957
 
                        if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
958
 
                                return false; /* the read_callback_ sets the state for us */
 
1472
                        if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
 
1473
                                return false; /* read_callback_ sets the state for us */
959
1474
                }
960
1475
                else {
961
1476
                        switch(type) {
962
1477
                                case FLAC__METADATA_TYPE_PADDING:
963
1478
                                        /* skip the padding bytes */
964
 
                                        if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
965
 
                                                return false; /* the read_callback_ sets the state for us */
 
1479
                                        if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
 
1480
                                                return false; /* read_callback_ sets the state for us */
966
1481
                                        break;
967
1482
                                case FLAC__METADATA_TYPE_APPLICATION:
968
1483
                                        /* remember, we read the ID already */
971
1486
                                                        decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
972
1487
                                                        return false;
973
1488
                                                }
974
 
                                                if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
975
 
                                                        return false; /* the read_callback_ sets the state for us */
 
1489
                                                if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length))
 
1490
                                                        return false; /* read_callback_ sets the state for us */
976
1491
                                        }
977
1492
                                        else
978
1493
                                                block.data.application.data = 0;
985
1500
                                        if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
986
1501
                                                return false;
987
1502
                                        break;
 
1503
                                case FLAC__METADATA_TYPE_PICTURE:
 
1504
                                        if(!read_metadata_picture_(decoder, &block.data.picture))
 
1505
                                                return false;
 
1506
                                        break;
988
1507
                                case FLAC__METADATA_TYPE_STREAMINFO:
989
1508
                                case FLAC__METADATA_TYPE_SEEKTABLE:
990
1509
                                        FLAC__ASSERT(0);
995
1514
                                                        decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
996
1515
                                                        return false;
997
1516
                                                }
998
 
                                                if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length, read_callback_, decoder))
999
 
                                                        return false; /* the read_callback_ sets the state for us */
 
1517
                                                if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length))
 
1518
                                                        return false; /* read_callback_ sets the state for us */
1000
1519
                                        }
1001
1520
                                        else
1002
1521
                                                block.data.unknown.data = 0;
1003
1522
                                        break;
1004
1523
                        }
1005
 
                        decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
 
1524
                        if(!decoder->private_->is_seeking && decoder->private_->metadata_callback)
 
1525
                                decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
1006
1526
 
1007
1527
                        /* now we have to free any malloc'ed data in the block */
1008
1528
                        switch(type) {
1030
1550
                                        if(0 != block.data.cue_sheet.tracks)
1031
1551
                                                free(block.data.cue_sheet.tracks);
1032
1552
                                        break;
 
1553
                                case FLAC__METADATA_TYPE_PICTURE:
 
1554
                                        if(0 != block.data.picture.mime_type)
 
1555
                                                free(block.data.picture.mime_type);
 
1556
                                        if(0 != block.data.picture.description)
 
1557
                                                free(block.data.picture.description);
 
1558
                                        if(0 != block.data.picture.data)
 
1559
                                                free(block.data.picture.data);
 
1560
                                        break;
1033
1561
                                case FLAC__METADATA_TYPE_STREAMINFO:
1034
1562
                                case FLAC__METADATA_TYPE_SEEKTABLE:
1035
1563
                                        FLAC__ASSERT(0);
1041
1569
                }
1042
1570
        }
1043
1571
 
1044
 
        if(is_last)
 
1572
        if(is_last) {
 
1573
                /* if this fails, it's OK, it's just a hint for the seek routine */
 
1574
                if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->first_frame_offset))
 
1575
                        decoder->private_->first_frame_offset = 0;
1045
1576
                decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
 
1577
        }
1046
1578
 
1047
1579
        return true;
1048
1580
}
1052
1584
        FLAC__uint32 x;
1053
1585
        unsigned bits, used_bits = 0;
1054
1586
 
1055
 
        FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
 
1587
        FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1056
1588
 
1057
1589
        decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
1058
1590
        decoder->private_->stream_info.is_last = is_last;
1059
1591
        decoder->private_->stream_info.length = length;
1060
1592
 
1061
1593
        bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
1062
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, bits, read_callback_, decoder))
1063
 
                return false; /* the read_callback_ sets the state for us */
 
1594
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, bits))
 
1595
                return false; /* read_callback_ sets the state for us */
1064
1596
        decoder->private_->stream_info.data.stream_info.min_blocksize = x;
1065
1597
        used_bits += bits;
1066
1598
 
1067
1599
        bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
1068
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
1069
 
                return false; /* the read_callback_ sets the state for us */
 
1600
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
 
1601
                return false; /* read_callback_ sets the state for us */
1070
1602
        decoder->private_->stream_info.data.stream_info.max_blocksize = x;
1071
1603
        used_bits += bits;
1072
1604
 
1073
1605
        bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
1074
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
1075
 
                return false; /* the read_callback_ sets the state for us */
 
1606
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
 
1607
                return false; /* read_callback_ sets the state for us */
1076
1608
        decoder->private_->stream_info.data.stream_info.min_framesize = x;
1077
1609
        used_bits += bits;
1078
1610
 
1079
1611
        bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
1080
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
1081
 
                return false; /* the read_callback_ sets the state for us */
 
1612
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
 
1613
                return false; /* read_callback_ sets the state for us */
1082
1614
        decoder->private_->stream_info.data.stream_info.max_framesize = x;
1083
1615
        used_bits += bits;
1084
1616
 
1085
1617
        bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
1086
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
1087
 
                return false; /* the read_callback_ sets the state for us */
 
1618
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
 
1619
                return false; /* read_callback_ sets the state for us */
1088
1620
        decoder->private_->stream_info.data.stream_info.sample_rate = x;
1089
1621
        used_bits += bits;
1090
1622
 
1091
1623
        bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
1092
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
1093
 
                return false; /* the read_callback_ sets the state for us */
 
1624
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
 
1625
                return false; /* read_callback_ sets the state for us */
1094
1626
        decoder->private_->stream_info.data.stream_info.channels = x+1;
1095
1627
        used_bits += bits;
1096
1628
 
1097
1629
        bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
1098
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
1099
 
                return false; /* the read_callback_ sets the state for us */
 
1630
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
 
1631
                return false; /* read_callback_ sets the state for us */
1100
1632
        decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
1101
1633
        used_bits += bits;
1102
1634
 
1103
1635
        bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
1104
 
        if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder))
1105
 
                return false; /* the read_callback_ sets the state for us */
 
1636
        if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN))
 
1637
                return false; /* read_callback_ sets the state for us */
1106
1638
        used_bits += bits;
1107
1639
 
1108
 
        if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
1109
 
                return false; /* the read_callback_ sets the state for us */
 
1640
        if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16))
 
1641
                return false; /* read_callback_ sets the state for us */
1110
1642
        used_bits += 16*8;
1111
1643
 
1112
1644
        /* skip the rest of the block */
1113
1645
        FLAC__ASSERT(used_bits % 8 == 0);
1114
1646
        length -= (used_bits / 8);
1115
 
        if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
1116
 
                return false; /* the read_callback_ sets the state for us */
 
1647
        if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
 
1648
                return false; /* read_callback_ sets the state for us */
1117
1649
 
1118
1650
        return true;
1119
1651
}
1123
1655
        FLAC__uint32 i, x;
1124
1656
        FLAC__uint64 xx;
1125
1657
 
1126
 
        FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
 
1658
        FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1127
1659
 
1128
1660
        decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
1129
1661
        decoder->private_->seek_table.is_last = is_last;
1137
1669
                return false;
1138
1670
        }
1139
1671
        for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
1140
 
                if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
1141
 
                        return false; /* the read_callback_ sets the state for us */
 
1672
                if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
 
1673
                        return false; /* read_callback_ sets the state for us */
1142
1674
                decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
1143
1675
 
1144
 
                if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
1145
 
                        return false; /* the read_callback_ sets the state for us */
 
1676
                if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
 
1677
                        return false; /* read_callback_ sets the state for us */
1146
1678
                decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
1147
1679
 
1148
 
                if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
1149
 
                        return false; /* the read_callback_ sets the state for us */
 
1680
                if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
 
1681
                        return false; /* read_callback_ sets the state for us */
1150
1682
                decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
1151
1683
        }
1152
1684
        length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
1153
1685
        /* if there is a partial point left, skip over it */
1154
1686
        if(length > 0) {
1155
 
                /*@@@ do an error_callback() here?  there's an argument for either way */
1156
 
                if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
1157
 
                        return false; /* the read_callback_ sets the state for us */
 
1687
                /*@@@ do a send_error_to_client_() here?  there's an argument for either way */
 
1688
                if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
 
1689
                        return false; /* read_callback_ sets the state for us */
1158
1690
        }
1159
1691
 
1160
1692
        return true;
1164
1696
{
1165
1697
        FLAC__uint32 i;
1166
1698
 
1167
 
        FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
 
1699
        FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1168
1700
 
1169
1701
        /* read vendor string */
1170
1702
        FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1171
 
        if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length, read_callback_, decoder))
1172
 
                return false; /* the read_callback_ sets the state for us */
 
1703
        if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length))
 
1704
                return false; /* read_callback_ sets the state for us */
1173
1705
        if(obj->vendor_string.length > 0) {
1174
1706
                if(0 == (obj->vendor_string.entry = (FLAC__byte*)malloc(obj->vendor_string.length+1))) {
1175
1707
                        decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1176
1708
                        return false;
1177
1709
                }
1178
 
                if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length, read_callback_, decoder))
1179
 
                        return false; /* the read_callback_ sets the state for us */
 
1710
                if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length))
 
1711
                        return false; /* read_callback_ sets the state for us */
1180
1712
                obj->vendor_string.entry[obj->vendor_string.length] = '\0';
1181
1713
        }
1182
1714
        else
1184
1716
 
1185
1717
        /* read num comments */
1186
1718
        FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
1187
 
        if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->num_comments, read_callback_, decoder))
1188
 
                return false; /* the read_callback_ sets the state for us */
 
1719
        if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->num_comments))
 
1720
                return false; /* read_callback_ sets the state for us */
1189
1721
 
1190
1722
        /* read comments */
1191
1723
        if(obj->num_comments > 0) {
1195
1727
                }
1196
1728
                for(i = 0; i < obj->num_comments; i++) {
1197
1729
                        FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1198
 
                        if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->comments[i].length, read_callback_, decoder))
1199
 
                                return false; /* the read_callback_ sets the state for us */
 
1730
                        if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->comments[i].length))
 
1731
                                return false; /* read_callback_ sets the state for us */
1200
1732
                        if(obj->comments[i].length > 0) {
1201
1733
                                if(0 == (obj->comments[i].entry = (FLAC__byte*)malloc(obj->comments[i].length+1))) {
1202
1734
                                        decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1203
1735
                                        return false;
1204
1736
                                }
1205
 
                                if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length, read_callback_, decoder))
1206
 
                                        return false; /* the read_callback_ sets the state for us */
 
1737
                                if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length))
 
1738
                                        return false; /* read_callback_ sets the state for us */
1207
1739
                                obj->comments[i].entry[obj->comments[i].length] = '\0';
1208
1740
                        }
1209
1741
                        else
1221
1753
{
1222
1754
        FLAC__uint32 i, j, x;
1223
1755
 
1224
 
        FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
 
1756
        FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1225
1757
 
1226
1758
        memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
1227
1759
 
1228
1760
        FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
1229
 
        if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8, read_callback_, decoder))
1230
 
                return false; /* the read_callback_ sets the state for us */
1231
 
 
1232
 
        if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN, read_callback_, decoder))
1233
 
                return false; /* the read_callback_ sets the state for us */
1234
 
 
1235
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN, read_callback_, decoder))
1236
 
                return false; /* the read_callback_ sets the state for us */
 
1761
        if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8))
 
1762
                return false; /* read_callback_ sets the state for us */
 
1763
 
 
1764
        if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
 
1765
                return false; /* read_callback_ sets the state for us */
 
1766
 
 
1767
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
 
1768
                return false; /* read_callback_ sets the state for us */
1237
1769
        obj->is_cd = x? true : false;
1238
1770
 
1239
 
        if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN, read_callback_, decoder))
1240
 
                return false; /* the read_callback_ sets the state for us */
 
1771
        if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN))
 
1772
                return false; /* read_callback_ sets the state for us */
1241
1773
 
1242
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN, read_callback_, decoder))
1243
 
                return false; /* the read_callback_ sets the state for us */
 
1774
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN))
 
1775
                return false; /* read_callback_ sets the state for us */
1244
1776
        obj->num_tracks = x;
1245
1777
 
1246
1778
        if(obj->num_tracks > 0) {
1250
1782
                }
1251
1783
                for(i = 0; i < obj->num_tracks; i++) {
1252
1784
                        FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
1253
 
                        if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN, read_callback_, decoder))
1254
 
                                return false; /* the read_callback_ sets the state for us */
 
1785
                        if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
 
1786
                                return false; /* read_callback_ sets the state for us */
1255
1787
 
1256
 
                        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN, read_callback_, decoder))
1257
 
                                return false; /* the read_callback_ sets the state for us */
 
1788
                        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN))
 
1789
                                return false; /* read_callback_ sets the state for us */
1258
1790
                        track->number = (FLAC__byte)x;
1259
1791
 
1260
1792
                        FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
1261
 
                        if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8, read_callback_, decoder))
1262
 
                                return false; /* the read_callback_ sets the state for us */
 
1793
                        if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8))
 
1794
                                return false; /* read_callback_ sets the state for us */
1263
1795
 
1264
 
                        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN, read_callback_, decoder))
1265
 
                                return false; /* the read_callback_ sets the state for us */
 
1796
                        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
 
1797
                                return false; /* read_callback_ sets the state for us */
1266
1798
                        track->type = x;
1267
1799
 
1268
 
                        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN, read_callback_, decoder))
1269
 
                                return false; /* the read_callback_ sets the state for us */
 
1800
                        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN))
 
1801
                                return false; /* read_callback_ sets the state for us */
1270
1802
                        track->pre_emphasis = x;
1271
1803
 
1272
 
                        if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN, read_callback_, decoder))
1273
 
                                return false; /* the read_callback_ sets the state for us */
 
1804
                        if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN))
 
1805
                                return false; /* read_callback_ sets the state for us */
1274
1806
 
1275
 
                        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN, read_callback_, decoder))
1276
 
                                return false; /* the read_callback_ sets the state for us */
 
1807
                        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN))
 
1808
                                return false; /* read_callback_ sets the state for us */
1277
1809
                        track->num_indices = (FLAC__byte)x;
1278
1810
 
1279
1811
                        if(track->num_indices > 0) {
1283
1815
                                }
1284
1816
                                for(j = 0; j < track->num_indices; j++) {
1285
1817
                                        FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
1286
 
                                        if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN, read_callback_, decoder))
1287
 
                                                return false; /* the read_callback_ sets the state for us */
 
1818
                                        if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
 
1819
                                                return false; /* read_callback_ sets the state for us */
1288
1820
 
1289
 
                                        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN, read_callback_, decoder))
1290
 
                                                return false; /* the read_callback_ sets the state for us */
 
1821
                                        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
 
1822
                                                return false; /* read_callback_ sets the state for us */
1291
1823
                                        index->number = (FLAC__byte)x;
1292
1824
 
1293
 
                                        if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN, read_callback_, decoder))
1294
 
                                                return false; /* the read_callback_ sets the state for us */
 
1825
                                        if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
 
1826
                                                return false; /* read_callback_ sets the state for us */
1295
1827
                                }
1296
1828
                        }
1297
1829
                }
1300
1832
        return true;
1301
1833
}
1302
1834
 
 
1835
FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj)
 
1836
{
 
1837
        FLAC__uint32 len;
 
1838
 
 
1839
        FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
 
1840
 
 
1841
        /* read type */
 
1842
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->type, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN))
 
1843
                return false; /* read_callback_ sets the state for us */
 
1844
 
 
1845
        /* read MIME type */
 
1846
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &len, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN))
 
1847
                return false; /* read_callback_ sets the state for us */
 
1848
        if(0 == (obj->mime_type = (char*)malloc(len+1))) {
 
1849
                decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
 
1850
                return false;
 
1851
        }
 
1852
        if(len > 0) {
 
1853
                if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, len))
 
1854
                        return false; /* read_callback_ sets the state for us */
 
1855
        }
 
1856
        obj->mime_type[len] = '\0';
 
1857
 
 
1858
        /* read description */
 
1859
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &len, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN))
 
1860
                return false; /* read_callback_ sets the state for us */
 
1861
        if(0 == (obj->description = (FLAC__byte*)malloc(len+1))) {
 
1862
                decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
 
1863
                return false;
 
1864
        }
 
1865
        if(len > 0) {
 
1866
                if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, len))
 
1867
                        return false; /* read_callback_ sets the state for us */
 
1868
        }
 
1869
        obj->description[len] = '\0';
 
1870
 
 
1871
        /* read width */
 
1872
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN))
 
1873
                return false; /* read_callback_ sets the state for us */
 
1874
 
 
1875
        /* read height */
 
1876
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN))
 
1877
                return false; /* read_callback_ sets the state for us */
 
1878
 
 
1879
        /* read depth */
 
1880
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN))
 
1881
                return false; /* read_callback_ sets the state for us */
 
1882
 
 
1883
        /* read colors */
 
1884
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN))
 
1885
                return false; /* read_callback_ sets the state for us */
 
1886
 
 
1887
        /* read data */
 
1888
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN))
 
1889
                return false; /* read_callback_ sets the state for us */
 
1890
        if(0 == (obj->data = (FLAC__byte*)malloc(obj->data_length))) {
 
1891
                decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
 
1892
                return false;
 
1893
        }
 
1894
        if(obj->data_length > 0) {
 
1895
                if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length))
 
1896
                        return false; /* read_callback_ sets the state for us */
 
1897
        }
 
1898
 
 
1899
        return true;
 
1900
}
 
1901
 
1303
1902
FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
1304
1903
{
1305
1904
        FLAC__uint32 x;
1306
1905
        unsigned i, skip;
1307
1906
 
1308
1907
        /* skip the version and flags bytes */
1309
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
1310
 
                return false; /* the read_callback_ sets the state for us */
 
1908
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 24))
 
1909
                return false; /* read_callback_ sets the state for us */
1311
1910
        /* get the size (in bytes) to skip */
1312
1911
        skip = 0;
1313
1912
        for(i = 0; i < 4; i++) {
1314
 
                if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1315
 
                        return false; /* the read_callback_ sets the state for us */
 
1913
                if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
 
1914
                        return false; /* read_callback_ sets the state for us */
1316
1915
                skip <<= 7;
1317
1916
                skip |= (x & 0x7f);
1318
1917
        }
1319
1918
        /* skip the rest of the tag */
1320
 
        if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, skip, read_callback_, decoder))
1321
 
                return false; /* the read_callback_ sets the state for us */
 
1919
        if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, skip))
 
1920
                return false; /* read_callback_ sets the state for us */
1322
1921
        return true;
1323
1922
}
1324
1923
 
1329
1928
 
1330
1929
        /* If we know the total number of samples in the stream, stop if we've read that many. */
1331
1930
        /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
1332
 
        if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
1333
 
                if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
 
1931
        if(FLAC__stream_decoder_get_total_samples(decoder) > 0) {
 
1932
                if(decoder->private_->samples_decoded >= FLAC__stream_decoder_get_total_samples(decoder)) {
1334
1933
                        decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1335
1934
                        return true;
1336
1935
                }
1337
1936
        }
1338
1937
 
1339
1938
        /* make sure we're byte aligned */
1340
 
        if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1341
 
                if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
1342
 
                        return false; /* the read_callback_ sets the state for us */
 
1939
        if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
 
1940
                if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
 
1941
                        return false; /* read_callback_ sets the state for us */
1343
1942
        }
1344
1943
 
1345
1944
        while(1) {
1348
1947
                        decoder->private_->cached = false;
1349
1948
                }
1350
1949
                else {
1351
 
                        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1352
 
                                return false; /* the read_callback_ sets the state for us */
 
1950
                        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
 
1951
                                return false; /* read_callback_ sets the state for us */
1353
1952
                }
1354
1953
                if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1355
1954
                        decoder->private_->header_warmup[0] = (FLAC__byte)x;
1356
 
                        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1357
 
                                return false; /* the read_callback_ sets the state for us */
 
1955
                        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
 
1956
                                return false; /* read_callback_ sets the state for us */
1358
1957
 
1359
1958
                        /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
1360
1959
                        /* else we have to check if the second byte is the end of a sync code */
1369
1968
                        }
1370
1969
                }
1371
1970
                if(first) {
1372
 
                        decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
 
1971
                        send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
1373
1972
                        first = false;
1374
1973
                }
1375
1974
        }
1382
1981
        unsigned channel;
1383
1982
        unsigned i;
1384
1983
        FLAC__int32 mid, side, left, right;
1385
 
        FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
 
1984
        unsigned frame_crc; /* the one we calculate from the input stream */
1386
1985
        FLAC__uint32 x;
1387
1986
 
1388
1987
        *got_a_frame = false;
1389
1988
 
1390
1989
        /* init the CRC */
1391
1990
        frame_crc = 0;
1392
 
        FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1393
 
        FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
1394
 
        FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
 
1991
        frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
 
1992
        frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
 
1993
        FLAC__bitreader_reset_read_crc16(decoder->private_->input, (FLAC__uint16)frame_crc);
1395
1994
 
1396
1995
        if(!read_frame_header_(decoder))
1397
1996
                return false;
1398
 
        if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
 
1997
        if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means we didn't sync on a valid header */
1399
1998
                return true;
1400
1999
        if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
1401
2000
                return false;
1431
2030
                 */
1432
2031
                if(!read_subframe_(decoder, channel, bps, do_full_decode))
1433
2032
                        return false;
1434
 
                if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
1435
 
                        decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
 
2033
                if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
1436
2034
                        return true;
1437
 
                }
1438
2035
        }
1439
2036
        if(!read_zero_padding_(decoder))
1440
2037
                return false;
1442
2039
        /*
1443
2040
         * Read the frame CRC-16 from the footer and check
1444
2041
         */
1445
 
        frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
1446
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
1447
 
                return false; /* the read_callback_ sets the state for us */
1448
 
        if(frame_crc == (FLAC__uint16)x) {
 
2042
        frame_crc = FLAC__bitreader_get_read_crc16(decoder->private_->input);
 
2043
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN))
 
2044
                return false; /* read_callback_ sets the state for us */
 
2045
        if(frame_crc == x) {
1449
2046
                if(do_full_decode) {
1450
2047
                        /* Undo any special channel coding */
1451
2048
                        switch(decoder->private_->frame.header.channel_assignment) {
1484
2081
        }
1485
2082
        else {
1486
2083
                /* Bad frame, emit error and zero the output signal */
1487
 
                decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, decoder->private_->client_data);
 
2084
                send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH);
1488
2085
                if(do_full_decode) {
1489
2086
                        for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1490
2087
                                memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1506
2103
 
1507
2104
        /* write it */
1508
2105
        if(do_full_decode) {
1509
 
                if(decoder->private_->write_callback(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output, decoder->private_->client_data) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE)
 
2106
                if(write_audio_frame_to_client_(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE)
1510
2107
                        return false;
1511
2108
        }
1512
2109
 
1525
2122
        const FLAC__bool is_known_variable_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize);
1526
2123
        const FLAC__bool is_known_fixed_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize);
1527
2124
 
1528
 
        FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
 
2125
        FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1529
2126
 
1530
2127
        /* init the raw header with the saved bits from synchronization */
1531
2128
        raw_header[0] = decoder->private_->header_warmup[0];
1549
2146
         * read in the raw header as bytes so we can CRC it, and parse it on the way
1550
2147
         */
1551
2148
        for(i = 0; i < 2; i++) {
1552
 
                if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1553
 
                        return false; /* the read_callback_ sets the state for us */
 
2149
                if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
 
2150
                        return false; /* read_callback_ sets the state for us */
1554
2151
                if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1555
2152
                        /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
1556
2153
                        decoder->private_->lookahead = (FLAC__byte)x;
1557
2154
                        decoder->private_->cached = true;
1558
 
                        decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
 
2155
                        send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
1559
2156
                        decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1560
2157
                        return true;
1561
2158
                }
1639
2236
                        sample_rate_hint = x;
1640
2237
                        break;
1641
2238
                case 15:
1642
 
                        decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
 
2239
                        send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
1643
2240
                        decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1644
2241
                        return true;
1645
2242
                default:
1701
2298
        }
1702
2299
 
1703
2300
        if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1704
 
                decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
 
2301
                send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
1705
2302
                decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1706
2303
                return true;
1707
2304
        }
1720
2317
         */
1721
2318
        if(is_known_variable_blocksize_stream) {
1722
2319
                if(blocksize_hint) {
1723
 
                        if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1724
 
                                return false; /* the read_callback_ sets the state for us */
 
2320
                        if(!FLAC__bitreader_read_utf8_uint64(decoder->private_->input, &xx, raw_header, &raw_header_len))
 
2321
                                return false; /* read_callback_ sets the state for us */
1725
2322
                        if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */
1726
2323
                                decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1727
2324
                                decoder->private_->cached = true;
1728
 
                                decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
 
2325
                                send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
1729
2326
                                decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1730
2327
                                return true;
1731
2328
                        }
1736
2333
                        is_unparseable = true;
1737
2334
        }
1738
2335
        else {
1739
 
                if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1740
 
                        return false; /* the read_callback_ sets the state for us */
 
2336
                if(!FLAC__bitreader_read_utf8_uint32(decoder->private_->input, &x, raw_header, &raw_header_len))
 
2337
                        return false; /* read_callback_ sets the state for us */
1741
2338
                if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1742
2339
                        decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1743
2340
                        decoder->private_->cached = true;
1744
 
                        decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
 
2341
                        send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
1745
2342
                        decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1746
2343
                        return true;
1747
2344
                }
1765
2362
        }
1766
2363
 
1767
2364
        if(blocksize_hint) {
1768
 
                if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1769
 
                        return false; /* the read_callback_ sets the state for us */
 
2365
                if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
 
2366
                        return false; /* read_callback_ sets the state for us */
1770
2367
                raw_header[raw_header_len++] = (FLAC__byte)x;
1771
2368
                if(blocksize_hint == 7) {
1772
2369
                        FLAC__uint32 _x;
1773
 
                        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1774
 
                                return false; /* the read_callback_ sets the state for us */
 
2370
                        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
 
2371
                                return false; /* read_callback_ sets the state for us */
1775
2372
                        raw_header[raw_header_len++] = (FLAC__byte)_x;
1776
2373
                        x = (x << 8) | _x;
1777
2374
                }
1779
2376
        }
1780
2377
 
1781
2378
        if(sample_rate_hint) {
1782
 
                if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1783
 
                        return false; /* the read_callback_ sets the state for us */
 
2379
                if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
 
2380
                        return false; /* read_callback_ sets the state for us */
1784
2381
                raw_header[raw_header_len++] = (FLAC__byte)x;
1785
2382
                if(sample_rate_hint != 12) {
1786
2383
                        FLAC__uint32 _x;
1787
 
                        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
1788
 
                                return false; /* the read_callback_ sets the state for us */
 
2384
                        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
 
2385
                                return false; /* read_callback_ sets the state for us */
1789
2386
                        raw_header[raw_header_len++] = (FLAC__byte)_x;
1790
2387
                        x = (x << 8) | _x;
1791
2388
                }
1798
2395
        }
1799
2396
 
1800
2397
        /* read the CRC-8 byte */
1801
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
1802
 
                return false; /* the read_callback_ sets the state for us */
 
2398
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
 
2399
                return false; /* read_callback_ sets the state for us */
1803
2400
        crc8 = (FLAC__byte)x;
1804
2401
 
1805
2402
        if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1806
 
                decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
 
2403
                send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
1807
2404
                decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1808
2405
                return true;
1809
2406
        }
1810
2407
 
1811
2408
        if(is_unparseable) {
1812
 
                decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1813
 
                return false;
 
2409
                send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
 
2410
                decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
 
2411
                return true;
1814
2412
        }
1815
2413
 
1816
2414
        return true;
1820
2418
{
1821
2419
        FLAC__uint32 x;
1822
2420
        FLAC__bool wasted_bits;
 
2421
        unsigned i;
1823
2422
 
1824
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1825
 
                return false; /* the read_callback_ sets the state for us */
 
2423
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) /* MAGIC NUMBER */
 
2424
                return false; /* read_callback_ sets the state for us */
1826
2425
 
1827
2426
        wasted_bits = (x & 1);
1828
2427
        x &= 0xfe;
1829
2428
 
1830
2429
        if(wasted_bits) {
1831
2430
                unsigned u;
1832
 
                if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
1833
 
                        return false; /* the read_callback_ sets the state for us */
 
2431
                if(!FLAC__bitreader_read_unary_unsigned(decoder->private_->input, &u))
 
2432
                        return false; /* read_callback_ sets the state for us */
1834
2433
                decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1835
2434
                bps -= decoder->private_->frame.subframes[channel].wasted_bits;
1836
2435
        }
1841
2440
         * Lots of magic numbers here
1842
2441
         */
1843
2442
        if(x & 0x80) {
1844
 
                decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
 
2443
                send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
1845
2444
                decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1846
2445
                return true;
1847
2446
        }
1854
2453
                        return false;
1855
2454
        }
1856
2455
        else if(x < 16) {
1857
 
                decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1858
 
                return false;
 
2456
                send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
 
2457
                decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
 
2458
                return true;
1859
2459
        }
1860
2460
        else if(x <= 24) {
1861
2461
                if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode))
1862
2462
                        return false;
 
2463
                if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
 
2464
                        return true;
1863
2465
        }
1864
2466
        else if(x < 64) {
1865
 
                decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1866
 
                return false;
 
2467
                send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
 
2468
                decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
 
2469
                return true;
1867
2470
        }
1868
2471
        else {
1869
2472
                if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode))
1870
2473
                        return false;
 
2474
                if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
 
2475
                        return true;
1871
2476
        }
1872
2477
 
1873
2478
        if(wasted_bits && do_full_decode) {
1874
 
                unsigned i;
1875
2479
                x = decoder->private_->frame.subframes[channel].wasted_bits;
1876
2480
                for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1877
2481
                        decoder->private_->output[channel][i] <<= x;
1889
2493
 
1890
2494
        decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1891
2495
 
1892
 
        if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
1893
 
                return false; /* the read_callback_ sets the state for us */
 
2496
        if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
 
2497
                return false; /* read_callback_ sets the state for us */
1894
2498
 
1895
2499
        subframe->value = x;
1896
2500
 
1917
2521
 
1918
2522
        /* read warm-up samples */
1919
2523
        for(u = 0; u < order; u++) {
1920
 
                if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1921
 
                        return false; /* the read_callback_ sets the state for us */
 
2524
                if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
 
2525
                        return false; /* read_callback_ sets the state for us */
1922
2526
                subframe->warmup[u] = i32;
1923
2527
        }
1924
2528
 
1925
2529
        /* read entropy coding method info */
1926
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1927
 
                return false; /* the read_callback_ sets the state for us */
 
2530
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
 
2531
                return false; /* read_callback_ sets the state for us */
1928
2532
        subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
1929
2533
        switch(subframe->entropy_coding_method.type) {
1930
2534
                case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1931
 
                        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1932
 
                                return false; /* the read_callback_ sets the state for us */
 
2535
                        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
 
2536
                                return false; /* read_callback_ sets the state for us */
1933
2537
                        subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1934
2538
                        subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
1935
2539
                        break;
1936
2540
                default:
1937
 
                        decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1938
 
                        return false;
 
2541
                        send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
 
2542
                        decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
 
2543
                        return true;
1939
2544
        }
1940
2545
 
1941
2546
        /* read residual */
1971
2576
 
1972
2577
        /* read warm-up samples */
1973
2578
        for(u = 0; u < order; u++) {
1974
 
                if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
1975
 
                        return false; /* the read_callback_ sets the state for us */
 
2579
                if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
 
2580
                        return false; /* read_callback_ sets the state for us */
1976
2581
                subframe->warmup[u] = i32;
1977
2582
        }
1978
2583
 
1979
2584
        /* read qlp coeff precision */
1980
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1981
 
                return false; /* the read_callback_ sets the state for us */
 
2585
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
 
2586
                return false; /* read_callback_ sets the state for us */
1982
2587
        if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1983
 
                decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
 
2588
                send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
1984
2589
                decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1985
2590
                return true;
1986
2591
        }
1987
2592
        subframe->qlp_coeff_precision = u32+1;
1988
2593
 
1989
2594
        /* read qlp shift */
1990
 
        if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1991
 
                return false; /* the read_callback_ sets the state for us */
 
2595
        if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
 
2596
                return false; /* read_callback_ sets the state for us */
1992
2597
        subframe->quantization_level = i32;
1993
2598
 
1994
2599
        /* read quantized lp coefficiencts */
1995
2600
        for(u = 0; u < order; u++) {
1996
 
                if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1997
 
                        return false; /* the read_callback_ sets the state for us */
 
2601
                if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision))
 
2602
                        return false; /* read_callback_ sets the state for us */
1998
2603
                subframe->qlp_coeff[u] = i32;
1999
2604
        }
2000
2605
 
2001
2606
        /* read entropy coding method info */
2002
 
        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
2003
 
                return false; /* the read_callback_ sets the state for us */
 
2607
        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
 
2608
                return false; /* read_callback_ sets the state for us */
2004
2609
        subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
2005
2610
        switch(subframe->entropy_coding_method.type) {
2006
2611
                case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
2007
 
                        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
2008
 
                                return false; /* the read_callback_ sets the state for us */
 
2612
                        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
 
2613
                                return false; /* read_callback_ sets the state for us */
2009
2614
                        subframe->entropy_coding_method.data.partitioned_rice.order = u32;
2010
2615
                        subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
2011
2616
                        break;
2012
2617
                default:
2013
 
                        decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
2014
 
                        return false;
 
2618
                        send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
 
2619
                        decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
 
2620
                        return true;
2015
2621
        }
2016
2622
 
2017
2623
        /* read residual */
2054
2660
        subframe->data = residual;
2055
2661
 
2056
2662
        for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
2057
 
                if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
2058
 
                        return false; /* the read_callback_ sets the state for us */
 
2663
                if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
 
2664
                        return false; /* read_callback_ sets the state for us */
2059
2665
                residual[i] = x;
2060
2666
        }
2061
2667
 
2077
2683
        /* sanity checks */
2078
2684
        if(partition_order == 0) {
2079
2685
                if(decoder->private_->frame.header.blocksize < predictor_order) {
2080
 
                        decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
 
2686
                        send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2081
2687
                        decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2082
2688
                        return true;
2083
2689
                }
2084
2690
        }
2085
2691
        else {
2086
2692
                if(partition_samples < predictor_order) {
2087
 
                        decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
 
2693
                        send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2088
2694
                        decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2089
2695
                        return true;
2090
2696
                }
2097
2703
 
2098
2704
        sample = 0;
2099
2705
        for(partition = 0; partition < partitions; partition++) {
2100
 
                if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
2101
 
                        return false; /* the read_callback_ sets the state for us */
 
2706
                if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
 
2707
                        return false; /* read_callback_ sets the state for us */
2102
2708
                partitioned_rice_contents->parameters[partition] = rice_parameter;
2103
2709
                if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2104
 
#ifdef FLAC__SYMMETRIC_RICE
2105
 
                        for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
2106
 
                                if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
2107
 
                                        return false; /* the read_callback_ sets the state for us */
2108
 
                                residual[sample] = i;
2109
 
                        }
2110
 
#else
2111
2710
                        u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
2112
 
                        if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
2113
 
                                return false; /* the read_callback_ sets the state for us */
 
2711
                        if(!FLAC__bitreader_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter))
 
2712
                                return false; /* read_callback_ sets the state for us */
2114
2713
                        sample += u;
2115
 
#endif
2116
2714
                }
2117
2715
                else {
2118
 
                        if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
2119
 
                                return false; /* the read_callback_ sets the state for us */
 
2716
                        if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
 
2717
                                return false; /* read_callback_ sets the state for us */
2120
2718
                        partitioned_rice_contents->raw_bits[partition] = rice_parameter;
2121
2719
                        for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
2122
 
                                if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
2123
 
                                        return false; /* the read_callback_ sets the state for us */
 
2720
                                if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i, rice_parameter))
 
2721
                                        return false; /* read_callback_ sets the state for us */
2124
2722
                                residual[sample] = i;
2125
2723
                        }
2126
2724
                }
2131
2729
 
2132
2730
FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
2133
2731
{
2134
 
        if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
 
2732
        if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
2135
2733
                FLAC__uint32 zero = 0;
2136
 
                if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
2137
 
                        return false; /* the read_callback_ sets the state for us */
 
2734
                if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
 
2735
                        return false; /* read_callback_ sets the state for us */
2138
2736
                if(zero != 0) {
2139
 
                        decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
 
2737
                        send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2140
2738
                        decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2141
2739
                }
2142
2740
        }
2143
2741
        return true;
2144
2742
}
2145
2743
 
2146
 
FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
 
2744
FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data)
2147
2745
{
2148
2746
        FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
2149
 
        FLAC__StreamDecoderReadStatus status;
2150
2747
 
2151
 
        status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
2152
 
        if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM)
 
2748
        if(
 
2749
#if FLAC__HAS_OGG
 
2750
                /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
 
2751
                !decoder->private_->is_ogg &&
 
2752
#endif
 
2753
                decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
 
2754
        ) {
 
2755
                *bytes = 0;
2153
2756
                decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
2154
 
        else if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT)
 
2757
                return false;
 
2758
        }
 
2759
        else if(*bytes > 0) {
 
2760
                /* While seeking, it is possible for our seek to land in the
 
2761
                 * middle of audio data that looks exactly like a frame header
 
2762
                 * from a future version of an encoder.  When that happens, our
 
2763
                 * error callback will get an
 
2764
                 * FLAC__STREAM_DECODER_UNPARSEABLE_STREAM and increment its
 
2765
                 * unparseable_frame_count.  But there is a remote possibility
 
2766
                 * that it is properly synced at such a "future-codec frame",
 
2767
                 * so to make sure, we wait to see many "unparseable" errors in
 
2768
                 * a row before bailing out.
 
2769
                 */
 
2770
                if(decoder->private_->is_seeking && decoder->private_->unparseable_frame_count > 20) {
 
2771
                        decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
 
2772
                        return false;
 
2773
                }
 
2774
                else {
 
2775
                        const FLAC__StreamDecoderReadStatus status =
 
2776
#if FLAC__HAS_OGG
 
2777
                                decoder->private_->is_ogg?
 
2778
                                read_callback_ogg_aspect_(decoder, buffer, bytes) :
 
2779
#endif
 
2780
                                decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data)
 
2781
                        ;
 
2782
                        if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) {
 
2783
                                decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
 
2784
                                return false;
 
2785
                        }
 
2786
                        else if(*bytes == 0) {
 
2787
                                if(
 
2788
                                        status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM ||
 
2789
                                        (
 
2790
#if FLAC__HAS_OGG
 
2791
                                                /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
 
2792
                                                !decoder->private_->is_ogg &&
 
2793
#endif
 
2794
                                                decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
 
2795
                                        )
 
2796
                                ) {
 
2797
                                        decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
 
2798
                                        return false;
 
2799
                                }
 
2800
                                else
 
2801
                                        return true;
 
2802
                        }
 
2803
                        else
 
2804
                                return true;
 
2805
                }
 
2806
        }
 
2807
        else {
 
2808
                /* abort to avoid a deadlock */
2155
2809
                decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2156
 
        return status == FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
 
2810
                return false;
 
2811
        }
 
2812
        /* [1] @@@ HACK NOTE: The end-of-stream checking has to be hacked around
 
2813
         * for Ogg FLAC.  This is because the ogg decoder aspect can lose sync
 
2814
         * and at the same time hit the end of the stream (for example, seeking
 
2815
         * to a point that is after the beginning of the last Ogg page).  There
 
2816
         * is no way to report an Ogg sync loss through the callbacks (see note
 
2817
         * in read_callback_ogg_aspect_()) so it returns CONTINUE with *bytes==0.
 
2818
         * So to keep the decoder from stopping at this point we gate the call
 
2819
         * to the eof_callback and let the Ogg decoder aspect set the
 
2820
         * end-of-stream state when it is needed.
 
2821
         */
 
2822
}
 
2823
 
 
2824
#if FLAC__HAS_OGG
 
2825
FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes)
 
2826
{
 
2827
        switch(FLAC__ogg_decoder_aspect_read_callback_wrapper(&decoder->protected_->ogg_decoder_aspect, buffer, bytes, read_callback_proxy_, decoder, decoder->private_->client_data)) {
 
2828
                case FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK:
 
2829
                        return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
 
2830
                /* we don't really have a way to handle lost sync via read
 
2831
                 * callback so we'll let it pass and let the underlying
 
2832
                 * FLAC decoder catch the error
 
2833
                 */
 
2834
                case FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC:
 
2835
                        return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
 
2836
                case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM:
 
2837
                        return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
 
2838
                case FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC:
 
2839
                case FLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION:
 
2840
                case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT:
 
2841
                case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR:
 
2842
                case FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR:
 
2843
                        return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
 
2844
                default:
 
2845
                        FLAC__ASSERT(0);
 
2846
                        /* double protection */
 
2847
                        return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
 
2848
        }
 
2849
}
 
2850
 
 
2851
FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
 
2852
{
 
2853
        FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)void_decoder;
 
2854
 
 
2855
        switch(decoder->private_->read_callback(decoder, buffer, bytes, client_data)) {
 
2856
                case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE:
 
2857
                        return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK;
 
2858
                case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM:
 
2859
                        return FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM;
 
2860
                case FLAC__STREAM_DECODER_READ_STATUS_ABORT:
 
2861
                        return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
 
2862
                default:
 
2863
                        /* double protection: */
 
2864
                        FLAC__ASSERT(0);
 
2865
                        return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
 
2866
        }
 
2867
}
 
2868
#endif
 
2869
 
 
2870
FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[])
 
2871
{
 
2872
        if(decoder->private_->is_seeking) {
 
2873
                FLAC__uint64 this_frame_sample = frame->header.number.sample_number;
 
2874
                FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;
 
2875
                FLAC__uint64 target_sample = decoder->private_->target_sample;
 
2876
 
 
2877
                FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
 
2878
 
 
2879
#if FLAC__HAS_OGG
 
2880
                decoder->private_->got_a_frame = true;
 
2881
#endif
 
2882
                decoder->private_->last_frame = *frame; /* save the frame */
 
2883
                if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
 
2884
                        unsigned delta = (unsigned)(target_sample - this_frame_sample);
 
2885
                        /* kick out of seek mode */
 
2886
                        decoder->private_->is_seeking = false;
 
2887
                        /* shift out the samples before target_sample */
 
2888
                        if(delta > 0) {
 
2889
                                unsigned channel;
 
2890
                                const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];
 
2891
                                for(channel = 0; channel < frame->header.channels; channel++)
 
2892
                                        newbuffer[channel] = buffer[channel] + delta;
 
2893
                                decoder->private_->last_frame.header.blocksize -= delta;
 
2894
                                decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
 
2895
                                /* write the relevant samples */
 
2896
                                return decoder->private_->write_callback(decoder, &decoder->private_->last_frame, newbuffer, decoder->private_->client_data);
 
2897
                        }
 
2898
                        else {
 
2899
                                /* write the relevant samples */
 
2900
                                return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
 
2901
                        }
 
2902
                }
 
2903
                else {
 
2904
                        return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
 
2905
                }
 
2906
        }
 
2907
        else {
 
2908
                /*
 
2909
                 * If we never got STREAMINFO, turn off MD5 checking to save
 
2910
                 * cycles since we don't have a sum to compare to anyway
 
2911
                 */
 
2912
                if(!decoder->private_->has_stream_info)
 
2913
                        decoder->private_->do_md5_checking = false;
 
2914
                if(decoder->private_->do_md5_checking) {
 
2915
                        if(!FLAC__MD5Accumulate(&decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
 
2916
                                return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
 
2917
                }
 
2918
                return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
 
2919
        }
 
2920
}
 
2921
 
 
2922
void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status)
 
2923
{
 
2924
        if(!decoder->private_->is_seeking)
 
2925
                decoder->private_->error_callback(decoder, status, decoder->private_->client_data);
 
2926
        else if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM)
 
2927
                decoder->private_->unparseable_frame_count++;
 
2928
}
 
2929
 
 
2930
FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
 
2931
{
 
2932
        FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound, lower_bound_sample, upper_bound_sample, this_frame_sample;
 
2933
        FLAC__int64 pos = -1;
 
2934
        int i;
 
2935
        unsigned approx_bytes_per_frame;
 
2936
        FLAC__bool first_seek = true;
 
2937
        const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder);
 
2938
        const unsigned min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
 
2939
        const unsigned max_blocksize = decoder->private_->stream_info.data.stream_info.max_blocksize;
 
2940
        const unsigned max_framesize = decoder->private_->stream_info.data.stream_info.max_framesize;
 
2941
        const unsigned min_framesize = decoder->private_->stream_info.data.stream_info.min_framesize;
 
2942
        /* take these from the current frame in case they've changed mid-stream */
 
2943
        unsigned channels = FLAC__stream_decoder_get_channels(decoder);
 
2944
        unsigned bps = FLAC__stream_decoder_get_bits_per_sample(decoder);
 
2945
        const FLAC__StreamMetadata_SeekTable *seek_table = decoder->private_->has_seek_table? &decoder->private_->seek_table.data.seek_table : 0;
 
2946
 
 
2947
        /* use values from stream info if we didn't decode a frame */
 
2948
        if(channels == 0)
 
2949
                channels = decoder->private_->stream_info.data.stream_info.channels;
 
2950
        if(bps == 0)
 
2951
                bps = decoder->private_->stream_info.data.stream_info.bits_per_sample;
 
2952
 
 
2953
        /* we are just guessing here */
 
2954
        if(max_framesize > 0)
 
2955
                approx_bytes_per_frame = (max_framesize + min_framesize) / 2 + 1;
 
2956
 
 
2957
        /*
 
2958
         * Check if it's a known fixed-blocksize stream.  Note that though
 
2959
         * the spec doesn't allow zeroes in the STREAMINFO block, we may
 
2960
         * never get a STREAMINFO block when decoding so the value of
 
2961
         * min_blocksize might be zero.
 
2962
         */
 
2963
        else if(min_blocksize == max_blocksize && min_blocksize > 0) {
 
2964
                /* note there are no () around 'bps/8' to keep precision up since it's an integer calulation */
 
2965
                approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64;
 
2966
        }
 
2967
        else
 
2968
                approx_bytes_per_frame = 4096 * channels * bps/8 + 64;
 
2969
 
 
2970
        /*
 
2971
         * First, we set an upper and lower bound on where in the
 
2972
         * stream we will search.  For now we assume the worst case
 
2973
         * scenario, which is our best guess at the beginning of
 
2974
         * the first frame and end of the stream.
 
2975
         */
 
2976
        lower_bound = first_frame_offset;
 
2977
        lower_bound_sample = 0;
 
2978
        upper_bound = stream_length;
 
2979
        upper_bound_sample = total_samples > 0 ? total_samples : target_sample;
 
2980
        if(upper_bound_sample == 0)
 
2981
                upper_bound_sample = 1;
 
2982
 
 
2983
        /*
 
2984
         * Now we refine the bounds if we have a seektable with
 
2985
         * suitable points.  Note that according to the spec they
 
2986
         * must be ordered by ascending sample number.
 
2987
         */
 
2988
        if(seek_table) {
 
2989
                /* find the closest seek point <= target_sample, if it exists */
 
2990
                for(i = (int)seek_table->num_points - 1; i >= 0; i--) {
 
2991
                        if(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && seek_table->points[i].sample_number <= target_sample)
 
2992
                                break;
 
2993
                }
 
2994
                if(i >= 0) { /* i.e. we found a suitable seek point... */
 
2995
                        lower_bound = first_frame_offset + seek_table->points[i].stream_offset;
 
2996
                        lower_bound_sample = seek_table->points[i].sample_number;
 
2997
                }
 
2998
 
 
2999
                /* find the closest seek point > target_sample, if it exists */
 
3000
                for(i = 0; i < (int)seek_table->num_points; i++) {
 
3001
                        if(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && seek_table->points[i].sample_number > target_sample)
 
3002
                                break;
 
3003
                }
 
3004
                if(i < (int)seek_table->num_points) { /* i.e. we found a suitable seek point... */
 
3005
                        upper_bound = first_frame_offset + seek_table->points[i].stream_offset;
 
3006
                        upper_bound_sample = seek_table->points[i].sample_number;
 
3007
                }
 
3008
        }
 
3009
 
 
3010
        decoder->private_->target_sample = target_sample;
 
3011
        while(1) {
 
3012
                /* check if the bounds are still ok */
 
3013
                if (lower_bound_sample >= upper_bound_sample || lower_bound > upper_bound) {
 
3014
                        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
 
3015
                        return false;
 
3016
                }
 
3017
#ifndef FLAC__INTEGER_ONLY_LIBRARY
 
3018
#if defined _MSC_VER || defined __MINGW32__
 
3019
                /* with VC++ you have to spoon feed it the casting */
 
3020
                pos = (FLAC__int64)lower_bound + (FLAC__int64)((FLAC__double)(FLAC__int64)(target_sample - lower_bound_sample) / (FLAC__double)(FLAC__int64)(upper_bound_sample - lower_bound_sample) * (FLAC__double)(FLAC__int64)(upper_bound - lower_bound)) - approx_bytes_per_frame;
 
3021
#else
 
3022
                pos = (FLAC__int64)lower_bound + (FLAC__int64)((FLAC__double)(target_sample - lower_bound_sample) / (FLAC__double)(upper_bound_sample - lower_bound_sample) * (FLAC__double)(upper_bound - lower_bound)) - approx_bytes_per_frame;
 
3023
#endif
 
3024
#else
 
3025
                /* a little less accurate: */
 
3026
                if(upper_bound - lower_bound < 0xffffffff)
 
3027
                        pos = (FLAC__int64)lower_bound + (FLAC__int64)(((target_sample - lower_bound_sample) * (upper_bound - lower_bound)) / (upper_bound_sample - lower_bound_sample)) - approx_bytes_per_frame;
 
3028
                else /* @@@ WATCHOUT, ~2TB limit */
 
3029
                        pos = (FLAC__int64)lower_bound + (FLAC__int64)((((target_sample - lower_bound_sample)>>8) * ((upper_bound - lower_bound)>>8)) / ((upper_bound_sample - lower_bound_sample)>>16)) - approx_bytes_per_frame;
 
3030
#endif
 
3031
                if(pos >= (FLAC__int64)upper_bound)
 
3032
                        pos = (FLAC__int64)upper_bound - 1;
 
3033
                if(pos < (FLAC__int64)lower_bound)
 
3034
                        pos = (FLAC__int64)lower_bound;
 
3035
                if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
 
3036
                        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
 
3037
                        return false;
 
3038
                }
 
3039
                if(!FLAC__stream_decoder_flush(decoder)) {
 
3040
                        /* above call sets the state for us */
 
3041
                        return false;
 
3042
                }
 
3043
                /* Now we need to get a frame.  First we need to reset our
 
3044
                 * unparseable_frame_count; if we get too many unparseable
 
3045
                 * frames in a row, the read callback will return
 
3046
                 * FLAC__STREAM_DECODER_READ_STATUS_ABORT, causing
 
3047
                 * FLAC__stream_decoder_process_single() to return false.
 
3048
                 */
 
3049
                decoder->private_->unparseable_frame_count = 0;
 
3050
                if(!FLAC__stream_decoder_process_single(decoder)) {
 
3051
                        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
 
3052
                        return false;
 
3053
                }
 
3054
                /* our write callback will change the state when it gets to the target frame */
 
3055
                /* actually, we could have got_a_frame if our decoder is at FLAC__STREAM_DECODER_END_OF_STREAM so we need to check for that also */
 
3056
#if 0
 
3057
                /*@@@@@@ used to be the following; not clear if the check for end of stream is needed anymore */
 
3058
                if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM)
 
3059
                        break;
 
3060
#endif
 
3061
                if(!decoder->private_->is_seeking) {
 
3062
                        break;
 
3063
                }
 
3064
                this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
 
3065
 
 
3066
                if (!decoder->private_->samples_decoded || (this_frame_sample + decoder->private_->last_frame.header.blocksize >= upper_bound_sample && !first_seek)) {
 
3067
                        if (pos == (FLAC__int64)lower_bound) {
 
3068
                                decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
 
3069
                                return false;
 
3070
                        }
 
3071
                        /* our last move backwards wasn't big enough, try again */
 
3072
                        approx_bytes_per_frame *= 2;
 
3073
                        continue;       
 
3074
                }
 
3075
                /* allow one seek over upper bound, required for streams with unknown total_samples */
 
3076
                first_seek = false;
 
3077
                
 
3078
                /* make sure we are not seeking in corrupted stream */
 
3079
                if (this_frame_sample < lower_bound_sample) {
 
3080
                        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
 
3081
                        return false;
 
3082
                }
 
3083
 
 
3084
                FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
 
3085
 
 
3086
                /* we need to narrow the search */
 
3087
                if(target_sample < this_frame_sample) {
 
3088
                        upper_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
 
3089
                        if(!FLAC__stream_decoder_get_decode_position(decoder, &upper_bound)) {
 
3090
                                decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
 
3091
                                return false;
 
3092
                        }
 
3093
                        approx_bytes_per_frame = (unsigned)(2 * (upper_bound - pos) / 3 + 16);
 
3094
                }
 
3095
                else {
 
3096
                        /* target_sample >= this_frame_sample + this frame's blocksize */
 
3097
 
 
3098
                        lower_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
 
3099
                        if(!FLAC__stream_decoder_get_decode_position(decoder, &lower_bound)) {
 
3100
                                decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
 
3101
                                return false;
 
3102
                        }
 
3103
                        approx_bytes_per_frame = (unsigned)(2 * (lower_bound - pos) / 3 + 16);
 
3104
                }
 
3105
        }
 
3106
 
 
3107
        return true;
 
3108
}
 
3109
 
 
3110
#if FLAC__HAS_OGG
 
3111
FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
 
3112
{
 
3113
        FLAC__uint64 left_pos = 0, right_pos = stream_length;
 
3114
        FLAC__uint64 left_sample = 0, right_sample = FLAC__stream_decoder_get_total_samples(decoder);
 
3115
        FLAC__uint64 this_frame_sample = 0; /* only initialized to avoid compiler warning */
 
3116
        FLAC__uint64 pos = 0; /* only initialized to avoid compiler warning */
 
3117
        FLAC__bool did_a_seek;
 
3118
        unsigned iteration = 0;
 
3119
 
 
3120
        /* In the first iterations, we will calculate the target byte position 
 
3121
         * by the distance from the target sample to left_sample and
 
3122
         * right_sample (let's call it "proportional search").  After that, we
 
3123
         * will switch to binary search.
 
3124
         */
 
3125
        unsigned BINARY_SEARCH_AFTER_ITERATION = 2;
 
3126
 
 
3127
        /* We will switch to a linear search once our current sample is less
 
3128
         * than this number of samples ahead of the target sample
 
3129
         */
 
3130
        static const FLAC__uint64 LINEAR_SEARCH_WITHIN_SAMPLES = FLAC__MAX_BLOCK_SIZE * 2;
 
3131
 
 
3132
        /* If the total number of samples is unknown, use a large value, and
 
3133
         * force binary search immediately.
 
3134
         */
 
3135
        if(right_sample == 0) {
 
3136
                right_sample = (FLAC__uint64)(-1);
 
3137
                BINARY_SEARCH_AFTER_ITERATION = 0;
 
3138
        }
 
3139
 
 
3140
        decoder->private_->target_sample = target_sample;
 
3141
        for( ; ; iteration++) {
 
3142
                if (iteration == 0 || this_frame_sample > target_sample || target_sample - this_frame_sample > LINEAR_SEARCH_WITHIN_SAMPLES) {
 
3143
                        if (iteration >= BINARY_SEARCH_AFTER_ITERATION) {
 
3144
                                pos = (right_pos + left_pos) / 2;
 
3145
                        }
 
3146
                        else {
 
3147
#ifndef FLAC__INTEGER_ONLY_LIBRARY
 
3148
#if defined _MSC_VER || defined __MINGW32__
 
3149
                                /* with MSVC you have to spoon feed it the casting */
 
3150
                                pos = (FLAC__uint64)((FLAC__double)(FLAC__int64)(target_sample - left_sample) / (FLAC__double)(FLAC__int64)(right_sample - left_sample) * (FLAC__double)(FLAC__int64)(right_pos - left_pos));
 
3151
#else
 
3152
                                pos = (FLAC__uint64)((FLAC__double)(target_sample - left_sample) / (FLAC__double)(right_sample - left_sample) * (FLAC__double)(right_pos - left_pos));
 
3153
#endif
 
3154
#else
 
3155
                                /* a little less accurate: */
 
3156
                                if ((target_sample-left_sample <= 0xffffffff) && (right_pos-left_pos <= 0xffffffff))
 
3157
                                        pos = (FLAC__int64)(((target_sample-left_sample) * (right_pos-left_pos)) / (right_sample-left_sample));
 
3158
                                else /* @@@ WATCHOUT, ~2TB limit */
 
3159
                                        pos = (FLAC__int64)((((target_sample-left_sample)>>8) * ((right_pos-left_pos)>>8)) / ((right_sample-left_sample)>>16));
 
3160
#endif
 
3161
                                /* @@@ TODO: might want to limit pos to some distance
 
3162
                                 * before EOF, to make sure we land before the last frame,
 
3163
                                 * thereby getting a this_frame_sample and so having a better
 
3164
                                 * estimate.  @@@@@@DELETE:this would also mostly (or totally if we could
 
3165
                                 * be sure to land before the last frame) avoid the
 
3166
                                 * end-of-stream case we have to check later.
 
3167
                                 */
 
3168
                        }
 
3169
 
 
3170
                        /* physical seek */
 
3171
                        if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
 
3172
                                decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
 
3173
                                return false;
 
3174
                        }
 
3175
                        if(!FLAC__stream_decoder_flush(decoder)) {
 
3176
                                /* above call sets the state for us */
 
3177
                                return false;
 
3178
                        }
 
3179
                        did_a_seek = true;
 
3180
                }
 
3181
                else
 
3182
                        did_a_seek = false;
 
3183
 
 
3184
                decoder->private_->got_a_frame = false;
 
3185
                if(!FLAC__stream_decoder_process_single(decoder)) {
 
3186
                        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
 
3187
                        return false;
 
3188
                }
 
3189
                if(!decoder->private_->got_a_frame) {
 
3190
                        if(did_a_seek) {
 
3191
                                /* this can happen if we seek to a point after the last frame; we drop
 
3192
                                 * to binary search right away in this case to avoid any wasted
 
3193
                                 * iterations of proportional search.
 
3194
                                 */
 
3195
                                right_pos = pos;
 
3196
                                BINARY_SEARCH_AFTER_ITERATION = 0;
 
3197
                        }
 
3198
                        else {
 
3199
                                /* this can probably only happen if total_samples is unknown and the
 
3200
                                 * target_sample is past the end of the stream
 
3201
                                 */
 
3202
                                decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
 
3203
                                return false;
 
3204
                        }
 
3205
                }
 
3206
                /* our write callback will change the state when it gets to the target frame */
 
3207
                else if(!decoder->private_->is_seeking/*@@@@@@ && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM*/) {
 
3208
                        break;
 
3209
                }
 
3210
                else {
 
3211
                        this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
 
3212
                        FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
 
3213
 
 
3214
                        if (did_a_seek) {
 
3215
                                if (this_frame_sample <= target_sample) {
 
3216
                                        /* The 'equal' case should not happen, since
 
3217
                                         * FLAC__stream_decoder_process_single()
 
3218
                                         * should recognize that it has hit the
 
3219
                                         * target sample and we would exit through
 
3220
                                         * the 'break' above.
 
3221
                                         */
 
3222
                                        FLAC__ASSERT(this_frame_sample != target_sample);
 
3223
 
 
3224
                                        left_sample = this_frame_sample;
 
3225
                                        /* sanity check to avoid infinite loop */
 
3226
                                        if (left_pos == pos) {
 
3227
                                                decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
 
3228
                                                return false;
 
3229
                                        }
 
3230
                                        left_pos = pos;
 
3231
                                }
 
3232
                                else if(this_frame_sample > target_sample) {
 
3233
                                        right_sample = this_frame_sample;
 
3234
                                        /* sanity check to avoid infinite loop */
 
3235
                                        if (right_pos == pos) {
 
3236
                                                decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
 
3237
                                                return false;
 
3238
                                        }
 
3239
                                        right_pos = pos;
 
3240
                                }
 
3241
                        }
 
3242
                }
 
3243
        }
 
3244
 
 
3245
        return true;
 
3246
}
 
3247
#endif
 
3248
 
 
3249
FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
 
3250
{
 
3251
        (void)client_data;
 
3252
 
 
3253
        if(*bytes > 0) {
 
3254
                *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file);
 
3255
                if(ferror(decoder->private_->file))
 
3256
                        return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
 
3257
                else if(*bytes == 0)
 
3258
                        return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
 
3259
                else
 
3260
                        return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
 
3261
        }
 
3262
        else
 
3263
                return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
 
3264
}
 
3265
 
 
3266
FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
 
3267
{
 
3268
        (void)client_data;
 
3269
 
 
3270
        if(decoder->private_->file == stdin)
 
3271
                return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
 
3272
        else if(fseeko(decoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
 
3273
                return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
 
3274
        else
 
3275
                return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
 
3276
}
 
3277
 
 
3278
FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
 
3279
{
 
3280
        off_t pos;
 
3281
        (void)client_data;
 
3282
 
 
3283
        if(decoder->private_->file == stdin)
 
3284
                return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
 
3285
        else if((pos = ftello(decoder->private_->file)) < 0)
 
3286
                return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
 
3287
        else {
 
3288
                *absolute_byte_offset = (FLAC__uint64)pos;
 
3289
                return FLAC__STREAM_DECODER_TELL_STATUS_OK;
 
3290
        }
 
3291
}
 
3292
 
 
3293
FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
 
3294
{
 
3295
        struct stat filestats;
 
3296
        (void)client_data;
 
3297
 
 
3298
        if(decoder->private_->file == stdin)
 
3299
                return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
 
3300
        else if(fstat(fileno(decoder->private_->file), &filestats) != 0)
 
3301
                return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
 
3302
        else {
 
3303
                *stream_length = (FLAC__uint64)filestats.st_size;
 
3304
                return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
 
3305
        }
 
3306
}
 
3307
 
 
3308
FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data)
 
3309
{
 
3310
        (void)client_data;
 
3311
 
 
3312
        return feof(decoder->private_->file)? true : false;
2157
3313
}