163
182
class StreamDecoder : public FLAC::Decoder::Stream, public DecoderCommon {
165
StreamDecoder(): FLAC::Decoder::Stream(), DecoderCommon() { }
186
StreamDecoder(Layer layer): FLAC::Decoder::Stream(), DecoderCommon(layer), file_(0) { }
166
187
~StreamDecoder() { }
168
189
// from FLAC::Decoder::Stream
169
::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], unsigned *bytes);
170
::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
171
void metadata_callback(const ::FLAC__StreamMetadata *metadata);
172
void error_callback(::FLAC__StreamDecoderErrorStatus status);
174
bool die(const char *msg = 0) const;
179
::FLAC__StreamDecoderReadStatus StreamDecoder::read_callback(FLAC__byte buffer[], unsigned *bytes)
181
return common_read_callback_(buffer, bytes);
184
::FLAC__StreamDecoderWriteStatus StreamDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
188
return common_write_callback_(frame);
191
void StreamDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
193
common_metadata_callback_(metadata);
196
void StreamDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
198
common_error_callback_(status);
201
bool StreamDecoder::die(const char *msg) const
203
State state = get_state();
206
printf("FAILED, %s", msg);
210
printf(", state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)state), state.as_cstring());
215
bool StreamDecoder::test_respond()
217
printf("testing init()... ");
218
if(init() != ::FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
222
current_metadata_number_ = 0;
224
if(::fseek(file_, 0, SEEK_SET) < 0) {
225
printf("FAILED rewinding input, errno = %d\n", errno);
229
printf("testing process_until_end_of_stream()... ");
230
if(!process_until_end_of_stream()) {
231
State state = get_state();
232
printf("FAILED, returned false, state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)state), state.as_cstring());
237
printf("testing finish()... ");
244
static bool test_stream_decoder()
246
StreamDecoder *decoder;
248
printf("\n+++ libFLAC++ unit test: FLAC::Decoder::Stream\n\n");
251
// test new -> delete
253
printf("allocating decoder instance... ");
254
decoder = new StreamDecoder();
256
printf("FAILED, new returned NULL\n");
261
printf("testing is_valid()... ");
262
if(!decoder->is_valid()) {
263
printf("FAILED, returned false\n");
268
printf("freeing decoder instance... ");
273
// test new -> init -> delete
275
printf("allocating decoder instance... ");
276
decoder = new StreamDecoder();
278
printf("FAILED, new returned NULL\n");
283
printf("testing is_valid()... ");
284
if(!decoder->is_valid()) {
285
printf("FAILED, returned false\n");
290
printf("testing init()... ");
291
if(decoder->init() != ::FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
292
return decoder->die();
295
printf("freeing decoder instance... ");
303
expected_metadata_sequence_[num_expected_++] = &streaminfo_;
305
printf("allocating decoder instance... ");
306
decoder = new StreamDecoder();
308
printf("FAILED, new returned NULL\n");
313
printf("testing is_valid()... ");
314
if(!decoder->is_valid()) {
315
printf("FAILED, returned false\n");
320
printf("testing init()... ");
321
if(decoder->init() != ::FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
322
return decoder->die();
325
printf("testing get_state()... ");
326
FLAC::Decoder::Stream::State state = decoder->get_state();
327
printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamDecoderState)state), state.as_cstring());
329
decoder->current_metadata_number_ = 0;
330
decoder->ignore_errors_ = false;
331
decoder->error_occurred_ = false;
333
printf("opening FLAC file... ");
334
decoder->file_ = ::fopen(flacfilename_, "rb");
335
if(0 == decoder->file_) {
341
printf("testing process_until_end_of_metadata()... ");
342
if(!decoder->process_until_end_of_metadata())
343
return decoder->die("returned false");
346
printf("testing process_single()... ");
347
if(!decoder->process_single())
348
return decoder->die("returned false");
351
printf("testing skip_single_frame()... ");
352
if(!decoder->skip_single_frame())
353
return decoder->die("returned false");
356
printf("testing flush()... ");
357
if(!decoder->flush())
358
return decoder->die("returned false");
361
decoder->ignore_errors_ = true;
362
printf("testing process_single()... ");
363
if(!decoder->process_single())
364
return decoder->die("returned false");
366
decoder->ignore_errors_ = false;
368
printf("testing process_until_end_of_stream()... ");
369
if(!decoder->process_until_end_of_stream())
370
return decoder->die("returned false");
373
printf("testing get_channels()... ");
375
unsigned channels = decoder->get_channels();
376
if(channels != streaminfo_.data.stream_info.channels) {
377
printf("FAILED, returned %u, expected %u\n", channels, streaminfo_.data.stream_info.channels);
383
printf("testing get_bits_per_sample()... ");
385
unsigned bits_per_sample = decoder->get_bits_per_sample();
386
if(bits_per_sample != streaminfo_.data.stream_info.bits_per_sample) {
387
printf("FAILED, returned %u, expected %u\n", bits_per_sample, streaminfo_.data.stream_info.bits_per_sample);
393
printf("testing get_sample_rate()... ");
395
unsigned sample_rate = decoder->get_sample_rate();
396
if(sample_rate != streaminfo_.data.stream_info.sample_rate) {
397
printf("FAILED, returned %u, expected %u\n", sample_rate, streaminfo_.data.stream_info.sample_rate);
403
printf("testing get_blocksize()... ");
405
unsigned blocksize = decoder->get_blocksize();
406
/* value could be anything since we're at the last block, so accept any answer */
407
printf("returned %u... OK\n", blocksize);
410
printf("testing get_channel_assignment()... ");
412
::FLAC__ChannelAssignment ca = decoder->get_channel_assignment();
413
printf("returned %u (%s)... OK\n", (unsigned)ca, ::FLAC__ChannelAssignmentString[ca]);
416
printf("testing reset()... ");
417
if(!decoder->reset())
418
return decoder->die("returned false");
421
decoder->current_metadata_number_ = 0;
423
printf("rewinding input... ");
424
if(::fseek(decoder->file_, 0, SEEK_SET) < 0) {
425
printf("FAILED, errno = %d\n", errno);
430
printf("testing process_until_end_of_stream()... ");
431
if(!decoder->process_until_end_of_stream())
432
return decoder->die("returned false");
435
printf("testing finish()... ");
443
printf("testing set_metadata_respond_all()... ");
444
if(!decoder->set_metadata_respond_all()) {
445
printf("FAILED, returned false\n");
451
expected_metadata_sequence_[num_expected_++] = &streaminfo_;
452
expected_metadata_sequence_[num_expected_++] = &padding_;
453
expected_metadata_sequence_[num_expected_++] = &seektable_;
454
expected_metadata_sequence_[num_expected_++] = &application1_;
455
expected_metadata_sequence_[num_expected_++] = &application2_;
456
expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
457
expected_metadata_sequence_[num_expected_++] = &cuesheet_;
458
expected_metadata_sequence_[num_expected_++] = &unknown_;
460
if(!decoder->test_respond())
467
printf("testing set_metadata_ignore_all()... ");
468
if(!decoder->set_metadata_ignore_all()) {
469
printf("FAILED, returned false\n");
476
if(!decoder->test_respond())
480
* respond all, ignore VORBIS_COMMENT
483
printf("testing set_metadata_respond_all()... ");
484
if(!decoder->set_metadata_respond_all()) {
485
printf("FAILED, returned false\n");
490
printf("testing set_metadata_ignore(VORBIS_COMMENT)... ");
491
if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_VORBIS_COMMENT)) {
492
printf("FAILED, returned false\n");
498
expected_metadata_sequence_[num_expected_++] = &streaminfo_;
499
expected_metadata_sequence_[num_expected_++] = &padding_;
500
expected_metadata_sequence_[num_expected_++] = &seektable_;
501
expected_metadata_sequence_[num_expected_++] = &application1_;
502
expected_metadata_sequence_[num_expected_++] = &application2_;
503
expected_metadata_sequence_[num_expected_++] = &cuesheet_;
504
expected_metadata_sequence_[num_expected_++] = &unknown_;
506
if(!decoder->test_respond())
510
* respond all, ignore APPLICATION
513
printf("testing set_metadata_respond_all()... ");
514
if(!decoder->set_metadata_respond_all()) {
515
printf("FAILED, returned false\n");
520
printf("testing set_metadata_ignore(APPLICATION)... ");
521
if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
522
printf("FAILED, returned false\n");
528
expected_metadata_sequence_[num_expected_++] = &streaminfo_;
529
expected_metadata_sequence_[num_expected_++] = &padding_;
530
expected_metadata_sequence_[num_expected_++] = &seektable_;
531
expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
532
expected_metadata_sequence_[num_expected_++] = &cuesheet_;
533
expected_metadata_sequence_[num_expected_++] = &unknown_;
535
if(!decoder->test_respond())
539
* respond all, ignore APPLICATION id of app#1
542
printf("testing set_metadata_respond_all()... ");
543
if(!decoder->set_metadata_respond_all()) {
544
printf("FAILED, returned false\n");
549
printf("testing set_metadata_ignore_application(of app block #1)... ");
550
if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
551
printf("FAILED, returned false\n");
557
expected_metadata_sequence_[num_expected_++] = &streaminfo_;
558
expected_metadata_sequence_[num_expected_++] = &padding_;
559
expected_metadata_sequence_[num_expected_++] = &seektable_;
560
expected_metadata_sequence_[num_expected_++] = &application2_;
561
expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
562
expected_metadata_sequence_[num_expected_++] = &cuesheet_;
563
expected_metadata_sequence_[num_expected_++] = &unknown_;
565
if(!decoder->test_respond())
569
* respond all, ignore APPLICATION id of app#1 & app#2
572
printf("testing set_metadata_respond_all()... ");
573
if(!decoder->set_metadata_respond_all()) {
574
printf("FAILED, returned false\n");
579
printf("testing set_metadata_ignore_application(of app block #1)... ");
580
if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
581
printf("FAILED, returned false\n");
586
printf("testing set_metadata_ignore_application(of app block #2)... ");
587
if(!decoder->set_metadata_ignore_application(application2_.data.application.id)) {
588
printf("FAILED, returned false\n");
594
expected_metadata_sequence_[num_expected_++] = &streaminfo_;
595
expected_metadata_sequence_[num_expected_++] = &padding_;
596
expected_metadata_sequence_[num_expected_++] = &seektable_;
597
expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
598
expected_metadata_sequence_[num_expected_++] = &cuesheet_;
599
expected_metadata_sequence_[num_expected_++] = &unknown_;
601
if(!decoder->test_respond())
605
* ignore all, respond VORBIS_COMMENT
608
printf("testing set_metadata_ignore_all()... ");
609
if(!decoder->set_metadata_ignore_all()) {
610
printf("FAILED, returned false\n");
615
printf("testing set_metadata_respond(VORBIS_COMMENT)... ");
616
if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_VORBIS_COMMENT)) {
617
printf("FAILED, returned false\n");
623
expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
625
if(!decoder->test_respond())
629
* ignore all, respond APPLICATION
632
printf("testing set_metadata_ignore_all()... ");
633
if(!decoder->set_metadata_ignore_all()) {
634
printf("FAILED, returned false\n");
639
printf("testing set_metadata_respond(APPLICATION)... ");
640
if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
641
printf("FAILED, returned false\n");
647
expected_metadata_sequence_[num_expected_++] = &application1_;
648
expected_metadata_sequence_[num_expected_++] = &application2_;
650
if(!decoder->test_respond())
654
* ignore all, respond APPLICATION id of app#1
657
printf("testing set_metadata_ignore_all()... ");
658
if(!decoder->set_metadata_ignore_all()) {
659
printf("FAILED, returned false\n");
664
printf("testing set_metadata_respond_application(of app block #1)... ");
665
if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
666
printf("FAILED, returned false\n");
672
expected_metadata_sequence_[num_expected_++] = &application1_;
674
if(!decoder->test_respond())
678
* ignore all, respond APPLICATION id of app#1 & app#2
681
printf("testing set_metadata_ignore_all()... ");
682
if(!decoder->set_metadata_ignore_all()) {
683
printf("FAILED, returned false\n");
688
printf("testing set_metadata_respond_application(of app block #1)... ");
689
if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
690
printf("FAILED, returned false\n");
695
printf("testing set_metadata_respond_application(of app block #2)... ");
696
if(!decoder->set_metadata_respond_application(application2_.data.application.id)) {
697
printf("FAILED, returned false\n");
703
expected_metadata_sequence_[num_expected_++] = &application1_;
704
expected_metadata_sequence_[num_expected_++] = &application2_;
706
if(!decoder->test_respond())
710
* respond all, ignore APPLICATION, respond APPLICATION id of app#1
713
printf("testing set_metadata_respond_all()... ");
714
if(!decoder->set_metadata_respond_all()) {
715
printf("FAILED, returned false\n");
720
printf("testing set_metadata_ignore(APPLICATION)... ");
721
if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
722
printf("FAILED, returned false\n");
727
printf("testing set_metadata_respond_application(of app block #1)... ");
728
if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
729
printf("FAILED, returned false\n");
735
expected_metadata_sequence_[num_expected_++] = &streaminfo_;
736
expected_metadata_sequence_[num_expected_++] = &padding_;
737
expected_metadata_sequence_[num_expected_++] = &seektable_;
738
expected_metadata_sequence_[num_expected_++] = &application1_;
739
expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
740
expected_metadata_sequence_[num_expected_++] = &cuesheet_;
741
expected_metadata_sequence_[num_expected_++] = &unknown_;
743
if(!decoder->test_respond())
747
* ignore all, respond APPLICATION, ignore APPLICATION id of app#1
750
printf("testing set_metadata_ignore_all()... ");
751
if(!decoder->set_metadata_ignore_all()) {
752
printf("FAILED, returned false\n");
757
printf("testing set_metadata_respond(APPLICATION)... ");
758
if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
759
printf("FAILED, returned false\n");
764
printf("testing set_metadata_ignore_application(of app block #1)... ");
765
if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
766
printf("FAILED, returned false\n");
772
expected_metadata_sequence_[num_expected_++] = &application2_;
774
if(!decoder->test_respond())
777
/* done, now leave the sequence the way we found it... */
779
expected_metadata_sequence_[num_expected_++] = &streaminfo_;
780
expected_metadata_sequence_[num_expected_++] = &padding_;
781
expected_metadata_sequence_[num_expected_++] = &seektable_;
782
expected_metadata_sequence_[num_expected_++] = &application1_;
783
expected_metadata_sequence_[num_expected_++] = &application2_;
784
expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
785
expected_metadata_sequence_[num_expected_++] = &cuesheet_;
786
expected_metadata_sequence_[num_expected_++] = &unknown_;
788
::fclose(decoder->file_);
790
printf("freeing decoder instance... ");
794
printf("\nPASSED!\n");
799
class SeekableStreamDecoder : public FLAC::Decoder::SeekableStream, public DecoderCommon {
801
SeekableStreamDecoder(): FLAC::Decoder::SeekableStream(), DecoderCommon() { }
802
~SeekableStreamDecoder() { }
804
// from FLAC::Decoder::SeekableStream
805
::FLAC__SeekableStreamDecoderReadStatus read_callback(FLAC__byte buffer[], unsigned *bytes);
806
::FLAC__SeekableStreamDecoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset);
807
::FLAC__SeekableStreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset);
808
::FLAC__SeekableStreamDecoderLengthStatus length_callback(FLAC__uint64 *stream_length);
190
::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes);
191
::FLAC__StreamDecoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset);
192
::FLAC__StreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset);
193
::FLAC__StreamDecoderLengthStatus length_callback(FLAC__uint64 *stream_length);
809
194
bool eof_callback();
810
195
::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
811
196
void metadata_callback(const ::FLAC__StreamMetadata *metadata);
812
197
void error_callback(::FLAC__StreamDecoderErrorStatus status);
814
bool die(const char *msg = 0) const;
199
bool test_respond(bool is_ogg);
819
::FLAC__SeekableStreamDecoderReadStatus SeekableStreamDecoder::read_callback(FLAC__byte buffer[], unsigned *bytes)
202
::FLAC__StreamDecoderReadStatus StreamDecoder::read_callback(FLAC__byte buffer[], size_t *bytes)
821
switch(common_read_callback_(buffer, bytes)) {
822
case ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE:
823
return ::FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK;
824
case ::FLAC__STREAM_DECODER_READ_STATUS_ABORT:
825
case ::FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM:
826
return ::FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;
829
return ::FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;
204
const size_t requested_bytes = *bytes;
207
return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT;
211
return ::FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
213
else if(requested_bytes > 0) {
214
*bytes = ::fread(buffer, 1, requested_bytes, file_);
217
return ::FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
219
return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
222
return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
226
return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
833
::FLAC__SeekableStreamDecoderSeekStatus SeekableStreamDecoder::seek_callback(FLAC__uint64 absolute_byte_offset)
229
::FLAC__StreamDecoderSeekStatus StreamDecoder::seek_callback(FLAC__uint64 absolute_byte_offset)
231
if(layer_ == LAYER_STREAM)
232
return ::FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
835
234
if(error_occurred_)
836
return ::FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
235
return ::FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
838
if(::fseek(file_, (long)absolute_byte_offset, SEEK_SET) < 0) {
237
if(fseeko(file_, (off_t)absolute_byte_offset, SEEK_SET) < 0) {
839
238
error_occurred_ = true;
840
return ::FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
239
return ::FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
843
return ::FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;
242
return ::FLAC__STREAM_DECODER_SEEK_STATUS_OK;
846
::FLAC__SeekableStreamDecoderTellStatus SeekableStreamDecoder::tell_callback(FLAC__uint64 *absolute_byte_offset)
245
::FLAC__StreamDecoderTellStatus StreamDecoder::tell_callback(FLAC__uint64 *absolute_byte_offset)
247
if(layer_ == LAYER_STREAM)
248
return ::FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
848
250
if(error_occurred_)
849
return ::FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR;
251
return ::FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
851
long offset = ::ftell(file_);
253
off_t offset = ftello(file_);
852
254
*absolute_byte_offset = (FLAC__uint64)offset;
855
257
error_occurred_ = true;
856
return ::FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR;
258
return ::FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
859
return ::FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;
261
return ::FLAC__STREAM_DECODER_TELL_STATUS_OK;
862
::FLAC__SeekableStreamDecoderLengthStatus SeekableStreamDecoder::length_callback(FLAC__uint64 *stream_length)
264
::FLAC__StreamDecoderLengthStatus StreamDecoder::length_callback(FLAC__uint64 *stream_length)
266
if(layer_ == LAYER_STREAM)
267
return ::FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
864
269
if(error_occurred_)
865
return ::FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR;
270
return ::FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
867
272
*stream_length = (FLAC__uint64)flacfilesize_;
868
return ::FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;
273
return ::FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
871
bool SeekableStreamDecoder::eof_callback()
276
bool StreamDecoder::eof_callback()
278
if(layer_ == LAYER_STREAM)
873
281
if(error_occurred_)
876
284
return (bool)feof(file_);
879
::FLAC__StreamDecoderWriteStatus SeekableStreamDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
287
::FLAC__StreamDecoderWriteStatus StreamDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
883
291
return common_write_callback_(frame);
886
void SeekableStreamDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
294
void StreamDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
888
296
common_metadata_callback_(metadata);
891
void SeekableStreamDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
299
void StreamDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
893
301
common_error_callback_(status);
896
bool SeekableStreamDecoder::die(const char *msg) const
898
State state = get_state();
901
printf("FAILED, %s", msg);
905
printf(", state = %u (%s)\n", (unsigned)((::FLAC__SeekableStreamDecoderState)state), state.as_cstring());
906
if(state == ::FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR) {
907
FLAC::Decoder::Stream::State state_ = get_stream_decoder_state();
908
printf(" stream decoder state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)state_), state_.as_cstring());
914
bool SeekableStreamDecoder::test_respond()
304
bool StreamDecoder::test_respond(bool is_ogg)
306
::FLAC__StreamDecoderInitStatus init_status;
916
308
if(!set_md5_checking(true)) {
917
309
printf("FAILED at set_md5_checking(), returned false\n");
921
printf("testing init()... ");
922
if(init() != ::FLAC__SEEKABLE_STREAM_DECODER_OK)
313
printf("testing init%s()... ", is_ogg? "_ogg":"");
314
init_status = is_ogg? init_ogg() : init();
315
if(init_status != ::FLAC__STREAM_DECODER_INIT_STATUS_OK)
316
return die_s_(0, this);
926
319
current_metadata_number_ = 0;
928
if(::fseek(file_, 0, SEEK_SET) < 0) {
321
if(fseeko(file_, 0, SEEK_SET) < 0) {
929
322
printf("FAILED rewinding input, errno = %d\n", errno);
933
326
printf("testing process_until_end_of_stream()... ");
934
327
if(!process_until_end_of_stream()) {
935
328
State state = get_state();
936
printf("FAILED, returned false, state = %u (%s)\n", (unsigned)((::FLAC__SeekableStreamDecoderState)state), state.as_cstring());
941
printf("testing finish()... ");
948
static bool test_seekable_stream_decoder()
950
SeekableStreamDecoder *decoder;
952
printf("\n+++ libFLAC++ unit test: FLAC::Decoder::SeekableStream\n\n");
955
// test new -> delete
957
printf("allocating decoder instance... ");
958
decoder = new SeekableStreamDecoder();
960
printf("FAILED, new returned NULL\n");
965
printf("testing is_valid()... ");
966
if(!decoder->is_valid()) {
967
printf("FAILED, returned false\n");
972
printf("freeing decoder instance... ");
977
// test new -> init -> delete
979
printf("allocating decoder instance... ");
980
decoder = new SeekableStreamDecoder();
982
printf("FAILED, new returned NULL\n");
987
printf("testing is_valid()... ");
988
if(!decoder->is_valid()) {
989
printf("FAILED, returned false\n");
994
printf("testing init()... ");
995
if(decoder->init() != ::FLAC__SEEKABLE_STREAM_DECODER_OK)
996
return decoder->die();
999
printf("freeing decoder instance... ");
1004
// test normal usage
1007
expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1009
printf("allocating decoder instance... ");
1010
decoder = new SeekableStreamDecoder();
1012
printf("FAILED, new returned NULL\n");
1017
printf("testing is_valid()... ");
1018
if(!decoder->is_valid()) {
1019
printf("FAILED, returned false\n");
1024
printf("testing set_md5_checking()... ");
1025
if(!decoder->set_md5_checking(true)) {
1026
printf("FAILED, returned false\n");
1031
printf("testing init()... ");
1032
if(decoder->init() != ::FLAC__SEEKABLE_STREAM_DECODER_OK)
1033
return decoder->die();
1036
printf("testing get_state()... ");
1037
FLAC::Decoder::SeekableStream::State state = decoder->get_state();
1038
printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__SeekableStreamDecoderState)state), state.as_cstring());
1040
printf("testing get_stream_decoder_state()... ");
1041
FLAC::Decoder::Stream::State state_ = decoder->get_stream_decoder_state();
1042
printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamDecoderState)state_), state_.as_cstring());
1044
decoder->current_metadata_number_ = 0;
1045
decoder->ignore_errors_ = false;
1046
decoder->error_occurred_ = false;
1048
printf("opening FLAC file... ");
1049
decoder->file_ = ::fopen(flacfilename_, "rb");
1050
if(0 == decoder->file_) {
1056
printf("testing get_md5_checking()... ");
1057
if(!decoder->get_md5_checking()) {
1058
printf("FAILED, returned false, expected true\n");
1063
printf("testing process_until_end_of_metadata()... ");
1064
if(!decoder->process_until_end_of_metadata())
1065
return decoder->die("returned false");
1068
printf("testing process_single()... ");
1069
if(!decoder->process_single())
1070
return decoder->die("returned false");
1073
printf("testing skip_single_frame()... ");
1074
if(!decoder->skip_single_frame())
1075
return decoder->die("returned false");
1078
printf("testing flush()... ");
1079
if(!decoder->flush())
1080
return decoder->die("returned false");
1083
decoder->ignore_errors_ = true;
1084
printf("testing process_single()... ");
1085
if(!decoder->process_single())
1086
return decoder->die("returned false");
1088
decoder->ignore_errors_ = false;
1090
printf("testing seek_absolute()... ");
1091
if(!decoder->seek_absolute(0))
1092
return decoder->die("returned false");
1095
printf("testing process_until_end_of_stream()... ");
1096
if(!decoder->process_until_end_of_stream())
1097
return decoder->die("returned false");
1100
printf("testing get_channels()... ");
1102
unsigned channels = decoder->get_channels();
1103
if(channels != streaminfo_.data.stream_info.channels) {
1104
printf("FAILED, returned %u, expected %u\n", channels, streaminfo_.data.stream_info.channels);
1110
printf("testing get_bits_per_sample()... ");
1112
unsigned bits_per_sample = decoder->get_bits_per_sample();
1113
if(bits_per_sample != streaminfo_.data.stream_info.bits_per_sample) {
1114
printf("FAILED, returned %u, expected %u\n", bits_per_sample, streaminfo_.data.stream_info.bits_per_sample);
1120
printf("testing get_sample_rate()... ");
1122
unsigned sample_rate = decoder->get_sample_rate();
1123
if(sample_rate != streaminfo_.data.stream_info.sample_rate) {
1124
printf("FAILED, returned %u, expected %u\n", sample_rate, streaminfo_.data.stream_info.sample_rate);
1130
printf("testing get_blocksize()... ");
1132
unsigned blocksize = decoder->get_blocksize();
1133
/* value could be anything since we're at the last block, so accept any answer */
1134
printf("returned %u... OK\n", blocksize);
1137
printf("testing get_channel_assignment()... ");
1139
::FLAC__ChannelAssignment ca = decoder->get_channel_assignment();
1140
printf("returned %u (%s)... OK\n", (unsigned)ca, ::FLAC__ChannelAssignmentString[ca]);
1143
printf("testing reset()... ");
1144
if(!decoder->reset())
1145
return decoder->die("returned false");
1148
decoder->current_metadata_number_ = 0;
1150
printf("rewinding input... ");
1151
if(::fseek(decoder->file_, 0, SEEK_SET) < 0) {
1152
printf("FAILED, errno = %d\n", errno);
1157
printf("testing process_until_end_of_stream()... ");
1158
if(!decoder->process_until_end_of_stream())
1159
return decoder->die("returned false");
1162
printf("testing finish()... ");
1170
printf("testing set_metadata_respond_all()... ");
1171
if(!decoder->set_metadata_respond_all()) {
1172
printf("FAILED, returned false\n");
1178
expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1179
expected_metadata_sequence_[num_expected_++] = &padding_;
1180
expected_metadata_sequence_[num_expected_++] = &seektable_;
1181
expected_metadata_sequence_[num_expected_++] = &application1_;
1182
expected_metadata_sequence_[num_expected_++] = &application2_;
1183
expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1184
expected_metadata_sequence_[num_expected_++] = &cuesheet_;
1185
expected_metadata_sequence_[num_expected_++] = &unknown_;
1187
if(!decoder->test_respond())
1194
printf("testing set_metadata_ignore_all()... ");
1195
if(!decoder->set_metadata_ignore_all()) {
1196
printf("FAILED, returned false\n");
1203
if(!decoder->test_respond())
1207
* respond all, ignore VORBIS_COMMENT
1210
printf("testing set_metadata_respond_all()... ");
1211
if(!decoder->set_metadata_respond_all()) {
1212
printf("FAILED, returned false\n");
1217
printf("testing set_metadata_ignore(VORBIS_COMMENT)... ");
1218
if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_VORBIS_COMMENT)) {
1219
printf("FAILED, returned false\n");
1225
expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1226
expected_metadata_sequence_[num_expected_++] = &padding_;
1227
expected_metadata_sequence_[num_expected_++] = &seektable_;
1228
expected_metadata_sequence_[num_expected_++] = &application1_;
1229
expected_metadata_sequence_[num_expected_++] = &application2_;
1230
expected_metadata_sequence_[num_expected_++] = &cuesheet_;
1231
expected_metadata_sequence_[num_expected_++] = &unknown_;
1233
if(!decoder->test_respond())
1237
* respond all, ignore APPLICATION
1240
printf("testing set_metadata_respond_all()... ");
1241
if(!decoder->set_metadata_respond_all()) {
1242
printf("FAILED, returned false\n");
1247
printf("testing set_metadata_ignore(APPLICATION)... ");
1248
if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
1249
printf("FAILED, returned false\n");
1255
expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1256
expected_metadata_sequence_[num_expected_++] = &padding_;
1257
expected_metadata_sequence_[num_expected_++] = &seektable_;
1258
expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1259
expected_metadata_sequence_[num_expected_++] = &cuesheet_;
1260
expected_metadata_sequence_[num_expected_++] = &unknown_;
1262
if(!decoder->test_respond())
1266
* respond all, ignore APPLICATION id of app#1
1269
printf("testing set_metadata_respond_all()... ");
1270
if(!decoder->set_metadata_respond_all()) {
1271
printf("FAILED, returned false\n");
1276
printf("testing set_metadata_ignore_application(of app block #1)... ");
1277
if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
1278
printf("FAILED, returned false\n");
1284
expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1285
expected_metadata_sequence_[num_expected_++] = &padding_;
1286
expected_metadata_sequence_[num_expected_++] = &seektable_;
1287
expected_metadata_sequence_[num_expected_++] = &application2_;
1288
expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1289
expected_metadata_sequence_[num_expected_++] = &cuesheet_;
1290
expected_metadata_sequence_[num_expected_++] = &unknown_;
1292
if(!decoder->test_respond())
1296
* respond all, ignore APPLICATION id of app#1 & app#2
1299
printf("testing set_metadata_respond_all()... ");
1300
if(!decoder->set_metadata_respond_all()) {
1301
printf("FAILED, returned false\n");
1306
printf("testing set_metadata_ignore_application(of app block #1)... ");
1307
if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
1308
printf("FAILED, returned false\n");
1313
printf("testing set_metadata_ignore_application(of app block #2)... ");
1314
if(!decoder->set_metadata_ignore_application(application2_.data.application.id)) {
1315
printf("FAILED, returned false\n");
1321
expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1322
expected_metadata_sequence_[num_expected_++] = &padding_;
1323
expected_metadata_sequence_[num_expected_++] = &seektable_;
1324
expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1325
expected_metadata_sequence_[num_expected_++] = &cuesheet_;
1326
expected_metadata_sequence_[num_expected_++] = &unknown_;
1328
if(!decoder->test_respond())
1332
* ignore all, respond VORBIS_COMMENT
1335
printf("testing set_metadata_ignore_all()... ");
1336
if(!decoder->set_metadata_ignore_all()) {
1337
printf("FAILED, returned false\n");
1342
printf("testing set_metadata_respond(VORBIS_COMMENT)... ");
1343
if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_VORBIS_COMMENT)) {
1344
printf("FAILED, returned false\n");
1350
expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1352
if(!decoder->test_respond())
1356
* ignore all, respond APPLICATION
1359
printf("testing set_metadata_ignore_all()... ");
1360
if(!decoder->set_metadata_ignore_all()) {
1361
printf("FAILED, returned false\n");
1366
printf("testing set_metadata_respond(APPLICATION)... ");
1367
if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
1368
printf("FAILED, returned false\n");
1374
expected_metadata_sequence_[num_expected_++] = &application1_;
1375
expected_metadata_sequence_[num_expected_++] = &application2_;
1377
if(!decoder->test_respond())
1381
* ignore all, respond APPLICATION id of app#1
1384
printf("testing set_metadata_ignore_all()... ");
1385
if(!decoder->set_metadata_ignore_all()) {
1386
printf("FAILED, returned false\n");
1391
printf("testing set_metadata_respond_application(of app block #1)... ");
1392
if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
1393
printf("FAILED, returned false\n");
1399
expected_metadata_sequence_[num_expected_++] = &application1_;
1401
if(!decoder->test_respond())
1405
* ignore all, respond APPLICATION id of app#1 & app#2
1408
printf("testing set_metadata_ignore_all()... ");
1409
if(!decoder->set_metadata_ignore_all()) {
1410
printf("FAILED, returned false\n");
1415
printf("testing set_metadata_respond_application(of app block #1)... ");
1416
if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
1417
printf("FAILED, returned false\n");
1422
printf("testing set_metadata_respond_application(of app block #2)... ");
1423
if(!decoder->set_metadata_respond_application(application2_.data.application.id)) {
1424
printf("FAILED, returned false\n");
1430
expected_metadata_sequence_[num_expected_++] = &application1_;
1431
expected_metadata_sequence_[num_expected_++] = &application2_;
1433
if(!decoder->test_respond())
1437
* respond all, ignore APPLICATION, respond APPLICATION id of app#1
1440
printf("testing set_metadata_respond_all()... ");
1441
if(!decoder->set_metadata_respond_all()) {
1442
printf("FAILED, returned false\n");
1447
printf("testing set_metadata_ignore(APPLICATION)... ");
1448
if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
1449
printf("FAILED, returned false\n");
1454
printf("testing set_metadata_respond_application(of app block #1)... ");
1455
if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
1456
printf("FAILED, returned false\n");
1462
expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1463
expected_metadata_sequence_[num_expected_++] = &padding_;
1464
expected_metadata_sequence_[num_expected_++] = &seektable_;
1465
expected_metadata_sequence_[num_expected_++] = &application1_;
1466
expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1467
expected_metadata_sequence_[num_expected_++] = &cuesheet_;
1468
expected_metadata_sequence_[num_expected_++] = &unknown_;
1470
if(!decoder->test_respond())
1474
* ignore all, respond APPLICATION, ignore APPLICATION id of app#1
1477
printf("testing set_metadata_ignore_all()... ");
1478
if(!decoder->set_metadata_ignore_all()) {
1479
printf("FAILED, returned false\n");
1484
printf("testing set_metadata_respond(APPLICATION)... ");
1485
if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
1486
printf("FAILED, returned false\n");
1491
printf("testing set_metadata_ignore_application(of app block #1)... ");
1492
if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
1493
printf("FAILED, returned false\n");
1499
expected_metadata_sequence_[num_expected_++] = &application2_;
1501
if(!decoder->test_respond())
1504
/* done, now leave the sequence the way we found it... */
1506
expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1507
expected_metadata_sequence_[num_expected_++] = &padding_;
1508
expected_metadata_sequence_[num_expected_++] = &seektable_;
1509
expected_metadata_sequence_[num_expected_++] = &application1_;
1510
expected_metadata_sequence_[num_expected_++] = &application2_;
1511
expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1512
expected_metadata_sequence_[num_expected_++] = &cuesheet_;
1513
expected_metadata_sequence_[num_expected_++] = &unknown_;
1515
::fclose(decoder->file_);
1517
printf("freeing decoder instance... ");
1521
printf("\nPASSED!\n");
329
printf("FAILED, returned false, state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)state), state.as_cstring());
334
printf("testing finish()... ");
336
State state = get_state();
337
printf("FAILED, returned false, state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)state), state.as_cstring());
1526
345
class FileDecoder : public FLAC::Decoder::File, public DecoderCommon {
1528
FileDecoder(): FLAC::Decoder::File(), DecoderCommon() { }
347
FileDecoder(Layer layer): FLAC::Decoder::File(), DecoderCommon(layer) { }
1529
348
~FileDecoder() { }
1531
// from FLAC::Decoder::File
350
// from FLAC::Decoder::Stream
1532
351
::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
1533
352
void metadata_callback(const ::FLAC__StreamMetadata *metadata);
1534
353
void error_callback(::FLAC__StreamDecoderErrorStatus status);
1536
bool die(const char *msg = 0) const;
1538
bool test_respond();
355
bool test_respond(bool is_ogg);
1541
358
::FLAC__StreamDecoderWriteStatus FileDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])