~ubuntu-branches/ubuntu/raring/flac/raring

« back to all changes in this revision

Viewing changes to src/flac/decode.c

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-12-06 16:57:20 UTC
  • mto: (8.1.1 lenny)
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: james.westby@ubuntu.com-20071206165720-4przr8grn6ha3e3a
Tags: upstream-1.2.1
ImportĀ upstreamĀ versionĀ 1.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
typedef struct {
46
46
#if FLAC__HAS_OGG
47
47
        FLAC__bool is_ogg;
 
48
        FLAC__bool use_first_serial_number;
 
49
        long serial_number;
48
50
#endif
49
51
 
50
52
        FLAC__bool is_aiff_out;
68
70
        utils__CueSpecification *cue_specification;
69
71
 
70
72
        const char *inbasefilename;
 
73
        const char *infilename;
71
74
        const char *outfilename;
72
75
 
73
76
        FLAC__uint64 samples_processed;
94
97
        FLAC__StreamDecoder *decoder;
95
98
 
96
99
        FILE *fout;
 
100
 
 
101
        foreign_metadata_t *foreign_metadata; /* NULL unless --keep-foreign-metadata requested */
 
102
        off_t fm_offset1, fm_offset2, fm_offset3;
97
103
} DecoderSession;
98
104
 
99
105
 
103
109
/*
104
110
 * local routines
105
111
 */
106
 
static FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool is_aiff_out, FLAC__bool is_wave_out, FLAC__bool treat_warnings_as_errors, FLAC__bool continue_through_decode_errors, FLAC__bool channel_map_none, replaygain_synthesis_spec_t replaygain_synthesis_spec, FLAC__bool analysis_mode, analysis_options aopts, utils__SkipUntilSpecification *skip_specification, utils__SkipUntilSpecification *until_specification, utils__CueSpecification *cue_specification, const char *infilename, const char *outfilename);
 
112
static FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool use_first_serial_number, long serial_number, FLAC__bool is_aiff_out, FLAC__bool is_wave_out, FLAC__bool treat_warnings_as_errors, FLAC__bool continue_through_decode_errors, FLAC__bool channel_map_none, replaygain_synthesis_spec_t replaygain_synthesis_spec, FLAC__bool analysis_mode, analysis_options aopts, utils__SkipUntilSpecification *skip_specification, utils__SkipUntilSpecification *until_specification, utils__CueSpecification *cue_specification, foreign_metadata_t *foreign_metadata, const char *infilename, const char *outfilename);
107
113
static void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred);
108
 
static FLAC__bool DecoderSession_init_decoder(DecoderSession *d, decode_options_t decode_options, const char *infilename);
 
114
static FLAC__bool DecoderSession_init_decoder(DecoderSession *d, const char *infilename);
109
115
static FLAC__bool DecoderSession_process(DecoderSession *d);
110
116
static int DecoderSession_finish_ok(DecoderSession *d);
111
117
static int DecoderSession_finish_error(DecoderSession *d);
112
118
static FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, unsigned sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input);
113
119
static FLAC__bool write_iff_headers(FILE *f, DecoderSession *decoder_session, FLAC__uint64 samples);
 
120
static FLAC__bool write_riff_wave_fmt_chunk(FILE *f, FLAC__bool is_waveformatextensible, unsigned bps, unsigned channels, unsigned sample_rate, FLAC__uint32 channel_mask);
 
121
static FLAC__bool write_aiff_form_comm_chunk(FILE *f, FLAC__uint64 samples, unsigned bps, unsigned channels, unsigned sample_rate);
114
122
static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val);
115
123
static FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val);
116
124
static FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val);
137
145
                        &decoder_session,
138
146
#if FLAC__HAS_OGG
139
147
                        options.common.is_ogg,
 
148
                        options.common.use_first_serial_number,
 
149
                        options.common.serial_number,
140
150
#else
141
151
                        /*is_ogg=*/false,
 
152
                        /*use_first_serial_number=*/false,
 
153
                        /*serial_number=*/0,
142
154
#endif
143
155
                        /*is_aiff_out=*/true,
144
156
                        /*is_wave_out=*/false,
151
163
                        &options.common.skip_specification,
152
164
                        &options.common.until_specification,
153
165
                        options.common.has_cue_specification? &options.common.cue_specification : 0,
 
166
                        options.foreign_metadata,
154
167
                        infilename,
155
168
                        outfilename
156
169
                )
157
170
        )
158
171
                return 1;
159
172
 
160
 
        if(!DecoderSession_init_decoder(&decoder_session, options.common, infilename))
 
173
        if(!DecoderSession_init_decoder(&decoder_session, infilename))
161
174
                return DecoderSession_finish_error(&decoder_session);
162
175
 
163
176
        if(!DecoderSession_process(&decoder_session))
175
188
                        &decoder_session,
176
189
#if FLAC__HAS_OGG
177
190
                        options.common.is_ogg,
 
191
                        options.common.use_first_serial_number,
 
192
                        options.common.serial_number,
178
193
#else
179
194
                        /*is_ogg=*/false,
 
195
                        /*use_first_serial_number=*/false,
 
196
                        /*serial_number=*/0,
180
197
#endif
181
198
                        /*is_aiff_out=*/false,
182
199
                        /*is_wave_out=*/true,
189
206
                        &options.common.skip_specification,
190
207
                        &options.common.until_specification,
191
208
                        options.common.has_cue_specification? &options.common.cue_specification : 0,
 
209
                        options.foreign_metadata,
192
210
                        infilename,
193
211
                        outfilename
194
212
                )
195
213
        )
196
214
                return 1;
197
215
 
198
 
        if(!DecoderSession_init_decoder(&decoder_session, options.common, infilename))
 
216
        if(!DecoderSession_init_decoder(&decoder_session, infilename))
199
217
                return DecoderSession_finish_error(&decoder_session);
200
218
 
201
219
        if(!DecoderSession_process(&decoder_session))
216
234
                        &decoder_session,
217
235
#if FLAC__HAS_OGG
218
236
                        options.common.is_ogg,
 
237
                        options.common.use_first_serial_number,
 
238
                        options.common.serial_number,
219
239
#else
220
240
                        /*is_ogg=*/false,
 
241
                        /*use_first_serial_number=*/false,
 
242
                        /*serial_number=*/0,
221
243
#endif
222
244
                        /*is_aiff_out=*/false,
223
245
                        /*is_wave_out=*/false,
230
252
                        &options.common.skip_specification,
231
253
                        &options.common.until_specification,
232
254
                        options.common.has_cue_specification? &options.common.cue_specification : 0,
 
255
                        /*foreign_metadata=*/NULL,
233
256
                        infilename,
234
257
                        outfilename
235
258
                )
236
259
        )
237
260
                return 1;
238
261
 
239
 
        if(!DecoderSession_init_decoder(&decoder_session, options.common, infilename))
 
262
        if(!DecoderSession_init_decoder(&decoder_session, infilename))
240
263
                return DecoderSession_finish_error(&decoder_session);
241
264
 
242
265
        if(!DecoderSession_process(&decoder_session))
245
268
        return DecoderSession_finish_ok(&decoder_session);
246
269
}
247
270
 
248
 
FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool is_aiff_out, FLAC__bool is_wave_out, FLAC__bool treat_warnings_as_errors, FLAC__bool continue_through_decode_errors, FLAC__bool channel_map_none, replaygain_synthesis_spec_t replaygain_synthesis_spec, FLAC__bool analysis_mode, analysis_options aopts, utils__SkipUntilSpecification *skip_specification, utils__SkipUntilSpecification *until_specification, utils__CueSpecification *cue_specification, const char *infilename, const char *outfilename)
 
271
FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool use_first_serial_number, long serial_number, FLAC__bool is_aiff_out, FLAC__bool is_wave_out, FLAC__bool treat_warnings_as_errors, FLAC__bool continue_through_decode_errors, FLAC__bool channel_map_none, replaygain_synthesis_spec_t replaygain_synthesis_spec, FLAC__bool analysis_mode, analysis_options aopts, utils__SkipUntilSpecification *skip_specification, utils__SkipUntilSpecification *until_specification, utils__CueSpecification *cue_specification, foreign_metadata_t *foreign_metadata, const char *infilename, const char *outfilename)
249
272
{
250
273
#if FLAC__HAS_OGG
251
274
        d->is_ogg = is_ogg;
 
275
        d->use_first_serial_number = use_first_serial_number;
 
276
        d->serial_number = serial_number;
252
277
#else
253
278
        (void)is_ogg;
 
279
        (void)use_first_serial_number;
 
280
        (void)serial_number;
254
281
#endif
255
282
 
256
283
        d->is_aiff_out = is_aiff_out;
270
297
        d->cue_specification = cue_specification;
271
298
 
272
299
        d->inbasefilename = grabbag__file_get_basename(infilename);
 
300
        d->infilename = infilename;
273
301
        d->outfilename = outfilename;
274
302
 
275
303
        d->samples_processed = 0;
294
322
 
295
323
        d->fout = 0; /* initialized with an open file later if necessary */
296
324
 
 
325
        d->foreign_metadata = foreign_metadata;
 
326
 
297
327
        FLAC__ASSERT(!(d->test_only && d->analysis_mode));
298
328
 
299
329
        if(!d->test_only) {
324
354
        }
325
355
}
326
356
 
327
 
FLAC__bool DecoderSession_init_decoder(DecoderSession *decoder_session, decode_options_t decode_options, const char *infilename)
 
357
FLAC__bool DecoderSession_init_decoder(DecoderSession *decoder_session, const char *infilename)
328
358
{
329
359
        FLAC__StreamDecoderInitStatus init_status;
330
360
        FLAC__uint32 test = 1;
331
361
 
332
362
        is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
333
363
 
 
364
        if(!decoder_session->analysis_mode && !decoder_session->test_only && (decoder_session->is_wave_out || decoder_session->is_aiff_out)) {
 
365
                if(decoder_session->foreign_metadata) {
 
366
                        const char *error;
 
367
                        if(!flac__foreign_metadata_read_from_flac(decoder_session->foreign_metadata, infilename, &error)) {
 
368
                                flac__utils_printf(stderr, 1, "%s: ERROR reading foreign metadata: %s\n", decoder_session->inbasefilename, error);
 
369
                                return false;
 
370
                        }
 
371
                }
 
372
        }
 
373
 
334
374
        decoder_session->decoder = FLAC__stream_decoder_new();
335
375
 
336
376
        if(0 == decoder_session->decoder) {
346
386
 
347
387
#if FLAC__HAS_OGG
348
388
        if(decoder_session->is_ogg) {
349
 
                if(!decode_options.use_first_serial_number)
350
 
                        FLAC__stream_decoder_set_ogg_serial_number(decoder_session->decoder, decode_options.serial_number);
 
389
                if(!decoder_session->use_first_serial_number)
 
390
                        FLAC__stream_decoder_set_ogg_serial_number(decoder_session->decoder, decoder_session->serial_number);
351
391
                init_status = FLAC__stream_decoder_init_ogg_file(decoder_session->decoder, strcmp(infilename, "-")? infilename : 0, write_callback, metadata_callback, error_callback, /*client_data=*/decoder_session);
352
392
        }
353
393
        else
354
 
#else
355
 
        (void)decode_options;
356
394
#endif
357
395
        {
358
396
                init_status = FLAC__stream_decoder_init_file(decoder_session->decoder, strcmp(infilename, "-")? infilename : 0, write_callback, metadata_callback, error_callback, /*client_data=*/decoder_session);
484
522
                flac__utils_printf(stderr, 2, "\r%s: %s         \n", d->inbasefilename, d->test_only? "ok           ":d->analysis_mode?"done           ":"done");
485
523
        }
486
524
        DecoderSession_destroy(d, /*error_occurred=*/!ok);
487
 
        if(!d->test_only && (d->is_wave_out || d->is_aiff_out) && (d->iff_headers_need_fixup || (!d->got_stream_info && strcmp(d->outfilename, "-"))))
488
 
                if(!fixup_iff_headers(d))
489
 
                        return 1;
 
525
        if(!d->analysis_mode && !d->test_only && (d->is_wave_out || d->is_aiff_out)) {
 
526
                if(d->iff_headers_need_fixup || (!d->got_stream_info && strcmp(d->outfilename, "-"))) {
 
527
                        if(!fixup_iff_headers(d))
 
528
                                return 1;
 
529
                }
 
530
                if(d->foreign_metadata) {
 
531
                        const char *error;
 
532
                        if(!flac__foreign_metadata_write_to_iff(d->foreign_metadata, d->infilename, d->outfilename, d->fm_offset1, d->fm_offset2, d->fm_offset3, &error)) {
 
533
                                flac__utils_printf(stderr, 1, "ERROR updating foreign metadata from %s to %s: %s\n", d->infilename, d->outfilename, error);
 
534
                                return 1;
 
535
                        }
 
536
                }
 
537
        }
490
538
        return ok? 0 : 1;
491
539
}
492
540
 
553
601
        const FLAC__bool is_waveformatextensible = decoder_session->is_wave_out && (decoder_session->channel_mask == 2 || decoder_session->channel_mask > 3 || decoder_session->bps%8 || decoder_session->channels > 2);
554
602
        FLAC__uint64 data_size = samples * decoder_session->channels * ((decoder_session->bps+7)/8);
555
603
        const FLAC__uint32 aligned_data_size = (FLAC__uint32)((data_size+1) & (~1U)); /* we'll check for overflow later */
 
604
 
 
605
        unsigned foreign_metadata_size = 0; /* size of all non-audio non-fmt/COMM foreign metadata chunks */
 
606
        foreign_metadata_t *fm = decoder_session->foreign_metadata;
 
607
        size_t i;
 
608
 
556
609
        if(samples == 0) {
557
610
                if(f == stdout) {
558
611
                        flac__utils_printf(stderr, 1, "%s: WARNING, don't have accurate sample count available for %s header.\n", decoder_session->inbasefilename, fmt_desc);
565
618
                        decoder_session->iff_headers_need_fixup = true;
566
619
                }
567
620
        }
568
 
        if(data_size >= 0xFFFFFFDC) {
569
 
                flac__utils_printf(stderr, 1, "%s: ERROR: stream is too big to fit in a single %s file chunk\n", decoder_session->inbasefilename, fmt_desc);
 
621
 
 
622
        if(fm) {
 
623
                FLAC__ASSERT(fm->format_block);
 
624
                FLAC__ASSERT(fm->audio_block);
 
625
                FLAC__ASSERT(fm->format_block < fm->audio_block);
 
626
                /* calc foreign metadata size; for RIFF/AIFF we always skip the first chunk, format chunk, and sound chunk since we write our own */
 
627
                for(i = 1; i < fm->num_blocks; i++) {
 
628
                        if(i != fm->format_block && i != fm->audio_block)
 
629
                                foreign_metadata_size += fm->blocks[i].size;
 
630
                }
 
631
        }
 
632
 
 
633
        if(data_size + foreign_metadata_size + 60/*worst-case*/ >= 0xFFFFFFF4) {
 
634
                flac__utils_printf(stderr, 1, "%s: ERROR: stream is too big to fit in a single %s file\n", decoder_session->inbasefilename, fmt_desc);
570
635
                return false;
571
636
        }
 
637
 
572
638
        if(decoder_session->is_wave_out) {
573
639
                if(flac__utils_fwrite("RIFF", 1, 4, f) != 4)
574
640
                        return false;
575
641
 
576
 
                if(!write_little_endian_uint32(f, aligned_data_size+(is_waveformatextensible?60:36))) /* filesize-8 */
577
 
                        return false;
578
 
 
579
 
                if(flac__utils_fwrite("WAVEfmt ", 1, 8, f) != 8)
580
 
                        return false;
581
 
 
582
 
                if(!write_little_endian_uint32(f, is_waveformatextensible? 40 : 16)) /* chunk size */
583
 
                        return false;
584
 
 
585
 
                if(!write_little_endian_uint16(f, (FLAC__uint16)(is_waveformatextensible? 65534 : 1))) /* compression code */
586
 
                        return false;
587
 
 
588
 
                if(!write_little_endian_uint16(f, (FLAC__uint16)(decoder_session->channels)))
589
 
                        return false;
590
 
 
591
 
                if(!write_little_endian_uint32(f, decoder_session->sample_rate))
592
 
                        return false;
593
 
 
594
 
                if(!write_little_endian_uint32(f, decoder_session->sample_rate * decoder_session->channels * ((decoder_session->bps+7) / 8)))
595
 
                        return false;
596
 
 
597
 
                if(!write_little_endian_uint16(f, (FLAC__uint16)(decoder_session->channels * ((decoder_session->bps+7) / 8)))) /* block align */
598
 
                        return false;
599
 
 
600
 
                if(!write_little_endian_uint16(f, (FLAC__uint16)(((decoder_session->bps+7)/8)*8))) /* bits per sample */
601
 
                        return false;
602
 
 
603
 
                if(is_waveformatextensible) {
604
 
                        if(!write_little_endian_uint16(f, (FLAC__uint16)22)) /* cbSize */
605
 
                                return false;
606
 
 
607
 
                        if(!write_little_endian_uint16(f, (FLAC__uint16)decoder_session->bps)) /* validBitsPerSample */
608
 
                                return false;
609
 
 
610
 
                        if(!write_little_endian_uint32(f, decoder_session->channel_mask))
611
 
                                return false;
612
 
 
613
 
                        /* GUID = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} */
614
 
                        if(flac__utils_fwrite("\x01\x00\x00\x00\x00\x00\x10\x00\x80\x00\x00\xaa\x00\x38\x9b\x71", 1, 16, f) != 16)
615
 
                                return false;
 
642
                if(!write_little_endian_uint32(f, foreign_metadata_size + aligned_data_size + (is_waveformatextensible?60:36))) /* filesize-8 */
 
643
                        return false;
 
644
 
 
645
                if(flac__utils_fwrite("WAVE", 1, 4, f) != 4)
 
646
                        return false;
 
647
 
 
648
                decoder_session->fm_offset1 = ftello(f);
 
649
 
 
650
                if(fm) {
 
651
                        /* seek forward to {allocate} or {skip over already-written chunks} before "fmt " */
 
652
                        for(i = 1; i < fm->format_block; i++) {
 
653
                                if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
 
654
                                        flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata before \"fmt \"\n", decoder_session->inbasefilename);
 
655
                                        return false;
 
656
                                }
 
657
                        }
 
658
                }
 
659
 
 
660
                if(!write_riff_wave_fmt_chunk(f, is_waveformatextensible, decoder_session->bps, decoder_session->channels, decoder_session->sample_rate, decoder_session->channel_mask))
 
661
                        return false;
 
662
 
 
663
                decoder_session->fm_offset2 = ftello(f);
 
664
 
 
665
                if(fm) {
 
666
                        /* seek forward to {allocate} or {skip over already-written chunks} after "fmt " but before "data" */
 
667
                        for(i = fm->format_block+1; i < fm->audio_block; i++) {
 
668
                                if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
 
669
                                        flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata after \"fmt \"\n", decoder_session->inbasefilename);
 
670
                                        return false;
 
671
                                }
 
672
                        }
616
673
                }
617
674
 
618
675
                if(flac__utils_fwrite("data", 1, 4, f) != 4)
620
677
 
621
678
                if(!write_little_endian_uint32(f, (FLAC__uint32)data_size)) /* data size */
622
679
                        return false;
 
680
 
 
681
                decoder_session->fm_offset3 = ftello(f) + aligned_data_size;
623
682
        }
624
683
        else {
 
684
                FLAC__uint32 ssnd_offset_size = (fm? fm->ssnd_offset_size : 0);
 
685
 
625
686
                if(flac__utils_fwrite("FORM", 1, 4, f) != 4)
626
687
                        return false;
627
688
 
628
 
                if(!write_big_endian_uint32(f, aligned_data_size+46)) /* filesize-8 */
629
 
                        return false;
630
 
 
631
 
                if(flac__utils_fwrite("AIFFCOMM", 1, 8, f) != 8)
632
 
                        return false;
633
 
 
634
 
                if(flac__utils_fwrite("\000\000\000\022", 1, 4, f) != 4) /* chunk size = 18 */
635
 
                        return false;
636
 
 
637
 
                if(!write_big_endian_uint16(f, (FLAC__uint16)(decoder_session->channels)))
638
 
                        return false;
639
 
 
640
 
                if(!write_big_endian_uint32(f, (FLAC__uint32)samples))
641
 
                        return false;
642
 
 
643
 
                if(!write_big_endian_uint16(f, (FLAC__uint16)(decoder_session->bps)))
644
 
                        return false;
645
 
 
646
 
                if(!write_sane_extended(f, decoder_session->sample_rate))
647
 
                        return false;
 
689
                if(!write_big_endian_uint32(f, foreign_metadata_size + aligned_data_size + 46 + ssnd_offset_size)) /* filesize-8 */
 
690
                        return false;
 
691
 
 
692
                if(flac__utils_fwrite("AIFF", 1, 4, f) != 4)
 
693
                        return false;
 
694
 
 
695
                decoder_session->fm_offset1 = ftello(f);
 
696
 
 
697
                if(fm) {
 
698
                        /* seek forward to {allocate} or {skip over already-written chunks} before "COMM" */
 
699
                        for(i = 1; i < fm->format_block; i++) {
 
700
                                if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
 
701
                                        flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata before \"COMM\"\n", decoder_session->inbasefilename);
 
702
                                        return false;
 
703
                                }
 
704
                        }
 
705
                }
 
706
 
 
707
                if(!write_aiff_form_comm_chunk(f, samples, decoder_session->bps, decoder_session->channels, decoder_session->sample_rate))
 
708
                        return false;
 
709
 
 
710
                decoder_session->fm_offset2 = ftello(f);
 
711
 
 
712
                if(fm) {
 
713
                        /* seek forward to {allocate} or {skip over already-written chunks} after "COMM" but before "SSND" */
 
714
                        for(i = fm->format_block+1; i < fm->audio_block; i++) {
 
715
                                if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
 
716
                                        flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata after \"COMM\"\n", decoder_session->inbasefilename);
 
717
                                        return false;
 
718
                                }
 
719
                        }
 
720
                }
648
721
 
649
722
                if(flac__utils_fwrite("SSND", 1, 4, f) != 4)
650
723
                        return false;
651
724
 
652
 
                if(!write_big_endian_uint32(f, (FLAC__uint32)data_size+8)) /* data size */
 
725
                if(!write_big_endian_uint32(f, (FLAC__uint32)data_size + 8 + ssnd_offset_size)) /* data size */
653
726
                        return false;
654
727
 
655
 
                if(!write_big_endian_uint32(f, 0/*offset*/))
 
728
                if(!write_big_endian_uint32(f, ssnd_offset_size))
656
729
                        return false;
657
730
 
658
731
                if(!write_big_endian_uint32(f, 0/*block_size*/))
659
732
                        return false;
660
 
        }
 
733
 
 
734
                if(ssnd_offset_size) {
 
735
                        /* seek forward to {allocate} or {skip over already-written} SSND offset */
 
736
                        if(fseeko(f, ssnd_offset_size, SEEK_CUR) < 0) {
 
737
                                flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping \"SSND\" offset\n", decoder_session->inbasefilename);
 
738
                                return false;
 
739
                        }
 
740
                }
 
741
 
 
742
                decoder_session->fm_offset3 = ftello(f) + aligned_data_size;
 
743
        }
 
744
 
 
745
        return true;
 
746
}
 
747
 
 
748
FLAC__bool write_riff_wave_fmt_chunk(FILE *f, FLAC__bool is_waveformatextensible, unsigned bps, unsigned channels, unsigned sample_rate, FLAC__uint32 channel_mask)
 
749
{
 
750
        if(flac__utils_fwrite("fmt ", 1, 4, f) != 4)
 
751
                return false;
 
752
 
 
753
        if(!write_little_endian_uint32(f, is_waveformatextensible? 40 : 16)) /* chunk size */
 
754
                return false;
 
755
 
 
756
        if(!write_little_endian_uint16(f, (FLAC__uint16)(is_waveformatextensible? 65534 : 1))) /* compression code */
 
757
                return false;
 
758
 
 
759
        if(!write_little_endian_uint16(f, (FLAC__uint16)channels))
 
760
                return false;
 
761
 
 
762
        if(!write_little_endian_uint32(f, sample_rate))
 
763
                return false;
 
764
 
 
765
        if(!write_little_endian_uint32(f, sample_rate * channels * ((bps+7) / 8)))
 
766
                return false;
 
767
 
 
768
        if(!write_little_endian_uint16(f, (FLAC__uint16)(channels * ((bps+7) / 8)))) /* block align */
 
769
                return false;
 
770
 
 
771
        if(!write_little_endian_uint16(f, (FLAC__uint16)(((bps+7)/8)*8))) /* bits per sample */
 
772
                return false;
 
773
 
 
774
        if(is_waveformatextensible) {
 
775
                if(!write_little_endian_uint16(f, (FLAC__uint16)22)) /* cbSize */
 
776
                        return false;
 
777
 
 
778
                if(!write_little_endian_uint16(f, (FLAC__uint16)bps)) /* validBitsPerSample */
 
779
                        return false;
 
780
 
 
781
                if(!write_little_endian_uint32(f, channel_mask))
 
782
                        return false;
 
783
 
 
784
                /* GUID = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} */
 
785
                if(flac__utils_fwrite("\x01\x00\x00\x00\x00\x00\x10\x00\x80\x00\x00\xaa\x00\x38\x9b\x71", 1, 16, f) != 16)
 
786
                        return false;
 
787
        }
 
788
 
 
789
        return true;
 
790
}
 
791
 
 
792
FLAC__bool write_aiff_form_comm_chunk(FILE *f, FLAC__uint64 samples, unsigned bps, unsigned channels, unsigned sample_rate)
 
793
{
 
794
        FLAC__ASSERT(samples <= 0xffffffff);
 
795
 
 
796
        if(flac__utils_fwrite("COMM", 1, 4, f) != 4)
 
797
                return false;
 
798
 
 
799
        if(!write_big_endian_uint32(f, 18)) /* chunk size = 18 */
 
800
                return false;
 
801
 
 
802
        if(!write_big_endian_uint16(f, (FLAC__uint16)channels))
 
803
                return false;
 
804
 
 
805
        if(!write_big_endian_uint32(f, (FLAC__uint32)samples))
 
806
                return false;
 
807
 
 
808
        if(!write_big_endian_uint16(f, (FLAC__uint16)bps))
 
809
                return false;
 
810
 
 
811
        if(!write_sane_extended(f, sample_rate))
 
812
                return false;
661
813
 
662
814
        return true;
663
815
}
880
1032
                        print_stats(decoder_session);
881
1033
 
882
1034
                if(decoder_session->analysis_mode) {
883
 
                        flac__analyze_frame(frame, decoder_session->frame_counter-1, frame_bytes, decoder_session->aopts, fout);
 
1035
                        flac__analyze_frame(frame, decoder_session->frame_counter-1, decoder_session->decode_position-frame_bytes, frame_bytes, decoder_session->aopts, fout);
884
1036
                }
885
1037
                else if(!decoder_session->test_only) {
886
1038
                        if(shift && !decoder_session->replaygain.apply) {
904
1056
                                        &decoder_session->replaygain.dither_context
905
1057
                                );
906
1058
                        }
907
 
                        else if(bps+shift == 8) {
908
 
                                if(is_unsigned_samples) {
909
 
                                        for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
910
 
                                                for(channel = 0; channel < channels; channel++, sample++)
911
 
                                                        u8buffer[sample] = (FLAC__uint8)(buffer[channel][wide_sample] + 0x80);
912
 
                                }
913
 
                                else {
914
 
                                        for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
915
 
                                                for(channel = 0; channel < channels; channel++, sample++)
916
 
                                                        s8buffer[sample] = (FLAC__int8)(buffer[channel][wide_sample]);
917
 
                                }
918
 
                                bytes_to_write = sample;
919
 
                        }
 
1059
                        /* first some special code for common cases */
 
1060
                        else if(is_big_endian == is_big_endian_host_ && !is_unsigned_samples && channels == 2 && bps+shift == 16) {
 
1061
                                FLAC__int16 *buf1_ = s16buffer + 1;
 
1062
                                if(is_big_endian)
 
1063
                                        memcpy(s16buffer, ((FLAC__byte*)(buffer[0]))+2, sizeof(FLAC__int32) * wide_samples - 2);
 
1064
                                else
 
1065
                                        memcpy(s16buffer, buffer[0], sizeof(FLAC__int32) * wide_samples);
 
1066
                                for(sample = 0; sample < wide_samples; sample++, buf1_+=2)
 
1067
                                        *buf1_ = (FLAC__int16)buffer[1][sample];
 
1068
                                bytes_to_write = 4 * sample;
 
1069
                        }
 
1070
                        else if(is_big_endian == is_big_endian_host_ && !is_unsigned_samples && channels == 1 && bps+shift == 16) {
 
1071
                                FLAC__int16 *buf1_ = s16buffer;
 
1072
                                for(sample = 0; sample < wide_samples; sample++)
 
1073
                                        *buf1_++ = (FLAC__int16)buffer[0][sample];
 
1074
                                bytes_to_write = 2 * sample;
 
1075
                        }
 
1076
                        /* generic code for the rest */
920
1077
                        else if(bps+shift == 16) {
921
1078
                                if(is_unsigned_samples) {
922
 
                                        for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
923
 
                                                for(channel = 0; channel < channels; channel++, sample++)
924
 
                                                        u16buffer[sample] = (FLAC__uint16)(buffer[channel][wide_sample] + 0x8000);
 
1079
                                        if(channels == 2) {
 
1080
                                                for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
 
1081
                                                        u16buffer[sample++] = (FLAC__uint16)(buffer[0][wide_sample] + 0x8000);
 
1082
                                                        u16buffer[sample++] = (FLAC__uint16)(buffer[1][wide_sample] + 0x8000);
 
1083
                                                }
 
1084
                                        }
 
1085
                                        else if(channels == 1) {
 
1086
                                                for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
 
1087
                                                        u16buffer[sample++] = (FLAC__uint16)(buffer[0][wide_sample] + 0x8000);
 
1088
                                        }
 
1089
                                        else { /* works for any 'channels' but above flavors are faster for 1 and 2 */
 
1090
                                                for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
 
1091
                                                        for(channel = 0; channel < channels; channel++, sample++)
 
1092
                                                                u16buffer[sample] = (FLAC__uint16)(buffer[channel][wide_sample] + 0x8000);
 
1093
                                        }
925
1094
                                }
926
1095
                                else {
927
 
                                        for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
928
 
                                                for(channel = 0; channel < channels; channel++, sample++)
929
 
                                                        s16buffer[sample] = (FLAC__int16)(buffer[channel][wide_sample]);
 
1096
                                        if(channels == 2) {
 
1097
                                                for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
 
1098
                                                        s16buffer[sample++] = (FLAC__int16)(buffer[0][wide_sample]);
 
1099
                                                        s16buffer[sample++] = (FLAC__int16)(buffer[1][wide_sample]);
 
1100
                                                }
 
1101
                                        }
 
1102
                                        else if(channels == 1) {
 
1103
                                                for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
 
1104
                                                        s16buffer[sample++] = (FLAC__int16)(buffer[0][wide_sample]);
 
1105
                                        }
 
1106
                                        else { /* works for any 'channels' but above flavors are faster for 1 and 2 */
 
1107
                                                for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
 
1108
                                                        for(channel = 0; channel < channels; channel++, sample++)
 
1109
                                                                s16buffer[sample] = (FLAC__int16)(buffer[channel][wide_sample]);
 
1110
                                        }
930
1111
                                }
931
1112
                                if(is_big_endian != is_big_endian_host_) {
932
1113
                                        unsigned char tmp;
984
1165
                                }
985
1166
                                bytes_to_write = 3 * sample;
986
1167
                        }
 
1168
                        else if(bps+shift == 8) {
 
1169
                                if(is_unsigned_samples) {
 
1170
                                        for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
 
1171
                                                for(channel = 0; channel < channels; channel++, sample++)
 
1172
                                                        u8buffer[sample] = (FLAC__uint8)(buffer[channel][wide_sample] + 0x80);
 
1173
                                }
 
1174
                                else {
 
1175
                                        for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
 
1176
                                                for(channel = 0; channel < channels; channel++, sample++)
 
1177
                                                        s8buffer[sample] = (FLAC__int8)(buffer[channel][wide_sample]);
 
1178
                                }
 
1179
                                bytes_to_write = sample;
 
1180
                        }
987
1181
                        else {
988
1182
                                FLAC__ASSERT(0);
989
1183
                                /* double protection */