annotate src/flac-1.2.1/src/flac/decode.c @ 86:98c1576536ae

Bring in flac, ogg, vorbis
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 19 Mar 2013 17:37:49 +0000
parents
children
rev   line source
cannam@86 1 /* flac - Command-line FLAC encoder/decoder
cannam@86 2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson
cannam@86 3 *
cannam@86 4 * This program is free software; you can redistribute it and/or
cannam@86 5 * modify it under the terms of the GNU General Public License
cannam@86 6 * as published by the Free Software Foundation; either version 2
cannam@86 7 * of the License, or (at your option) any later version.
cannam@86 8 *
cannam@86 9 * This program is distributed in the hope that it will be useful,
cannam@86 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@86 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cannam@86 12 * GNU General Public License for more details.
cannam@86 13 *
cannam@86 14 * You should have received a copy of the GNU General Public License
cannam@86 15 * along with this program; if not, write to the Free Software
cannam@86 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
cannam@86 17 */
cannam@86 18
cannam@86 19 #if HAVE_CONFIG_H
cannam@86 20 # include <config.h>
cannam@86 21 #endif
cannam@86 22
cannam@86 23 #if defined _WIN32 && !defined __CYGWIN__
cannam@86 24 /* where MSVC puts unlink() */
cannam@86 25 # include <io.h>
cannam@86 26 #else
cannam@86 27 # include <unistd.h>
cannam@86 28 #endif
cannam@86 29 #if defined _MSC_VER || defined __MINGW32__
cannam@86 30 #include <sys/types.h> /* for off_t */
cannam@86 31 #if _MSC_VER <= 1600 /* @@@ [2G limit] */
cannam@86 32 #define fseeko fseek
cannam@86 33 #define ftello ftell
cannam@86 34 #endif
cannam@86 35 #endif
cannam@86 36 #include <errno.h>
cannam@86 37 #include <math.h> /* for floor() */
cannam@86 38 #include <stdio.h> /* for FILE etc. */
cannam@86 39 #include <string.h> /* for strcmp(), strerror() */
cannam@86 40 #include "FLAC/all.h"
cannam@86 41 #include "share/grabbag.h"
cannam@86 42 #include "share/replaygain_synthesis.h"
cannam@86 43 #include "decode.h"
cannam@86 44
cannam@86 45 typedef struct {
cannam@86 46 #if FLAC__HAS_OGG
cannam@86 47 FLAC__bool is_ogg;
cannam@86 48 FLAC__bool use_first_serial_number;
cannam@86 49 long serial_number;
cannam@86 50 #endif
cannam@86 51
cannam@86 52 FLAC__bool is_aiff_out;
cannam@86 53 FLAC__bool is_wave_out;
cannam@86 54 FLAC__bool treat_warnings_as_errors;
cannam@86 55 FLAC__bool continue_through_decode_errors;
cannam@86 56 FLAC__bool channel_map_none;
cannam@86 57
cannam@86 58 struct {
cannam@86 59 replaygain_synthesis_spec_t spec;
cannam@86 60 FLAC__bool apply; /* 'spec.apply' is just a request; this 'apply' means we actually parsed the RG tags and are ready to go */
cannam@86 61 double scale;
cannam@86 62 DitherContext dither_context;
cannam@86 63 } replaygain;
cannam@86 64
cannam@86 65 FLAC__bool test_only;
cannam@86 66 FLAC__bool analysis_mode;
cannam@86 67 analysis_options aopts;
cannam@86 68 utils__SkipUntilSpecification *skip_specification;
cannam@86 69 utils__SkipUntilSpecification *until_specification; /* a canonicalized value of 0 mean end-of-stream (i.e. --until=-0) */
cannam@86 70 utils__CueSpecification *cue_specification;
cannam@86 71
cannam@86 72 const char *inbasefilename;
cannam@86 73 const char *infilename;
cannam@86 74 const char *outfilename;
cannam@86 75
cannam@86 76 FLAC__uint64 samples_processed;
cannam@86 77 unsigned frame_counter;
cannam@86 78 FLAC__bool abort_flag;
cannam@86 79 FLAC__bool aborting_due_to_until; /* true if we intentionally abort decoding prematurely because we hit the --until point */
cannam@86 80 FLAC__bool aborting_due_to_unparseable; /* true if we abort decoding because we hit an unparseable frame */
cannam@86 81
cannam@86 82 FLAC__bool iff_headers_need_fixup;
cannam@86 83
cannam@86 84 FLAC__bool is_big_endian;
cannam@86 85 FLAC__bool is_unsigned_samples;
cannam@86 86 FLAC__bool got_stream_info;
cannam@86 87 FLAC__bool has_md5sum;
cannam@86 88 FLAC__uint64 total_samples;
cannam@86 89 unsigned bps;
cannam@86 90 unsigned channels;
cannam@86 91 unsigned sample_rate;
cannam@86 92 FLAC__uint32 channel_mask;
cannam@86 93
cannam@86 94 /* these are used only in analyze mode */
cannam@86 95 FLAC__uint64 decode_position;
cannam@86 96
cannam@86 97 FLAC__StreamDecoder *decoder;
cannam@86 98
cannam@86 99 FILE *fout;
cannam@86 100
cannam@86 101 foreign_metadata_t *foreign_metadata; /* NULL unless --keep-foreign-metadata requested */
cannam@86 102 off_t fm_offset1, fm_offset2, fm_offset3;
cannam@86 103 } DecoderSession;
cannam@86 104
cannam@86 105
cannam@86 106 static FLAC__bool is_big_endian_host_;
cannam@86 107
cannam@86 108
cannam@86 109 /*
cannam@86 110 * local routines
cannam@86 111 */
cannam@86 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);
cannam@86 113 static void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred);
cannam@86 114 static FLAC__bool DecoderSession_init_decoder(DecoderSession *d, const char *infilename);
cannam@86 115 static FLAC__bool DecoderSession_process(DecoderSession *d);
cannam@86 116 static int DecoderSession_finish_ok(DecoderSession *d);
cannam@86 117 static int DecoderSession_finish_error(DecoderSession *d);
cannam@86 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);
cannam@86 119 static FLAC__bool write_iff_headers(FILE *f, DecoderSession *decoder_session, FLAC__uint64 samples);
cannam@86 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);
cannam@86 121 static FLAC__bool write_aiff_form_comm_chunk(FILE *f, FLAC__uint64 samples, unsigned bps, unsigned channels, unsigned sample_rate);
cannam@86 122 static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val);
cannam@86 123 static FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val);
cannam@86 124 static FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val);
cannam@86 125 static FLAC__bool write_big_endian_uint32(FILE *f, FLAC__uint32 val);
cannam@86 126 static FLAC__bool write_sane_extended(FILE *f, unsigned val);
cannam@86 127 static FLAC__bool fixup_iff_headers(DecoderSession *d);
cannam@86 128 static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
cannam@86 129 static void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
cannam@86 130 static void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
cannam@86 131 static void print_error_with_init_status(const DecoderSession *d, const char *message, FLAC__StreamDecoderInitStatus init_status);
cannam@86 132 static void print_error_with_state(const DecoderSession *d, const char *message);
cannam@86 133 static void print_stats(const DecoderSession *decoder_session);
cannam@86 134
cannam@86 135
cannam@86 136 /*
cannam@86 137 * public routines
cannam@86 138 */
cannam@86 139 int flac__decode_aiff(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, wav_decode_options_t options)
cannam@86 140 {
cannam@86 141 DecoderSession decoder_session;
cannam@86 142
cannam@86 143 if(!
cannam@86 144 DecoderSession_construct(
cannam@86 145 &decoder_session,
cannam@86 146 #if FLAC__HAS_OGG
cannam@86 147 options.common.is_ogg,
cannam@86 148 options.common.use_first_serial_number,
cannam@86 149 options.common.serial_number,
cannam@86 150 #else
cannam@86 151 /*is_ogg=*/false,
cannam@86 152 /*use_first_serial_number=*/false,
cannam@86 153 /*serial_number=*/0,
cannam@86 154 #endif
cannam@86 155 /*is_aiff_out=*/true,
cannam@86 156 /*is_wave_out=*/false,
cannam@86 157 options.common.treat_warnings_as_errors,
cannam@86 158 options.common.continue_through_decode_errors,
cannam@86 159 options.common.channel_map_none,
cannam@86 160 options.common.replaygain_synthesis_spec,
cannam@86 161 analysis_mode,
cannam@86 162 aopts,
cannam@86 163 &options.common.skip_specification,
cannam@86 164 &options.common.until_specification,
cannam@86 165 options.common.has_cue_specification? &options.common.cue_specification : 0,
cannam@86 166 options.foreign_metadata,
cannam@86 167 infilename,
cannam@86 168 outfilename
cannam@86 169 )
cannam@86 170 )
cannam@86 171 return 1;
cannam@86 172
cannam@86 173 if(!DecoderSession_init_decoder(&decoder_session, infilename))
cannam@86 174 return DecoderSession_finish_error(&decoder_session);
cannam@86 175
cannam@86 176 if(!DecoderSession_process(&decoder_session))
cannam@86 177 return DecoderSession_finish_error(&decoder_session);
cannam@86 178
cannam@86 179 return DecoderSession_finish_ok(&decoder_session);
cannam@86 180 }
cannam@86 181
cannam@86 182 int flac__decode_wav(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, wav_decode_options_t options)
cannam@86 183 {
cannam@86 184 DecoderSession decoder_session;
cannam@86 185
cannam@86 186 if(!
cannam@86 187 DecoderSession_construct(
cannam@86 188 &decoder_session,
cannam@86 189 #if FLAC__HAS_OGG
cannam@86 190 options.common.is_ogg,
cannam@86 191 options.common.use_first_serial_number,
cannam@86 192 options.common.serial_number,
cannam@86 193 #else
cannam@86 194 /*is_ogg=*/false,
cannam@86 195 /*use_first_serial_number=*/false,
cannam@86 196 /*serial_number=*/0,
cannam@86 197 #endif
cannam@86 198 /*is_aiff_out=*/false,
cannam@86 199 /*is_wave_out=*/true,
cannam@86 200 options.common.treat_warnings_as_errors,
cannam@86 201 options.common.continue_through_decode_errors,
cannam@86 202 options.common.channel_map_none,
cannam@86 203 options.common.replaygain_synthesis_spec,
cannam@86 204 analysis_mode,
cannam@86 205 aopts,
cannam@86 206 &options.common.skip_specification,
cannam@86 207 &options.common.until_specification,
cannam@86 208 options.common.has_cue_specification? &options.common.cue_specification : 0,
cannam@86 209 options.foreign_metadata,
cannam@86 210 infilename,
cannam@86 211 outfilename
cannam@86 212 )
cannam@86 213 )
cannam@86 214 return 1;
cannam@86 215
cannam@86 216 if(!DecoderSession_init_decoder(&decoder_session, infilename))
cannam@86 217 return DecoderSession_finish_error(&decoder_session);
cannam@86 218
cannam@86 219 if(!DecoderSession_process(&decoder_session))
cannam@86 220 return DecoderSession_finish_error(&decoder_session);
cannam@86 221
cannam@86 222 return DecoderSession_finish_ok(&decoder_session);
cannam@86 223 }
cannam@86 224
cannam@86 225 int flac__decode_raw(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, raw_decode_options_t options)
cannam@86 226 {
cannam@86 227 DecoderSession decoder_session;
cannam@86 228
cannam@86 229 decoder_session.is_big_endian = options.is_big_endian;
cannam@86 230 decoder_session.is_unsigned_samples = options.is_unsigned_samples;
cannam@86 231
cannam@86 232 if(!
cannam@86 233 DecoderSession_construct(
cannam@86 234 &decoder_session,
cannam@86 235 #if FLAC__HAS_OGG
cannam@86 236 options.common.is_ogg,
cannam@86 237 options.common.use_first_serial_number,
cannam@86 238 options.common.serial_number,
cannam@86 239 #else
cannam@86 240 /*is_ogg=*/false,
cannam@86 241 /*use_first_serial_number=*/false,
cannam@86 242 /*serial_number=*/0,
cannam@86 243 #endif
cannam@86 244 /*is_aiff_out=*/false,
cannam@86 245 /*is_wave_out=*/false,
cannam@86 246 options.common.treat_warnings_as_errors,
cannam@86 247 options.common.continue_through_decode_errors,
cannam@86 248 options.common.channel_map_none,
cannam@86 249 options.common.replaygain_synthesis_spec,
cannam@86 250 analysis_mode,
cannam@86 251 aopts,
cannam@86 252 &options.common.skip_specification,
cannam@86 253 &options.common.until_specification,
cannam@86 254 options.common.has_cue_specification? &options.common.cue_specification : 0,
cannam@86 255 /*foreign_metadata=*/NULL,
cannam@86 256 infilename,
cannam@86 257 outfilename
cannam@86 258 )
cannam@86 259 )
cannam@86 260 return 1;
cannam@86 261
cannam@86 262 if(!DecoderSession_init_decoder(&decoder_session, infilename))
cannam@86 263 return DecoderSession_finish_error(&decoder_session);
cannam@86 264
cannam@86 265 if(!DecoderSession_process(&decoder_session))
cannam@86 266 return DecoderSession_finish_error(&decoder_session);
cannam@86 267
cannam@86 268 return DecoderSession_finish_ok(&decoder_session);
cannam@86 269 }
cannam@86 270
cannam@86 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)
cannam@86 272 {
cannam@86 273 #if FLAC__HAS_OGG
cannam@86 274 d->is_ogg = is_ogg;
cannam@86 275 d->use_first_serial_number = use_first_serial_number;
cannam@86 276 d->serial_number = serial_number;
cannam@86 277 #else
cannam@86 278 (void)is_ogg;
cannam@86 279 (void)use_first_serial_number;
cannam@86 280 (void)serial_number;
cannam@86 281 #endif
cannam@86 282
cannam@86 283 d->is_aiff_out = is_aiff_out;
cannam@86 284 d->is_wave_out = is_wave_out;
cannam@86 285 d->treat_warnings_as_errors = treat_warnings_as_errors;
cannam@86 286 d->continue_through_decode_errors = continue_through_decode_errors;
cannam@86 287 d->channel_map_none = channel_map_none;
cannam@86 288 d->replaygain.spec = replaygain_synthesis_spec;
cannam@86 289 d->replaygain.apply = false;
cannam@86 290 d->replaygain.scale = 0.0;
cannam@86 291 /* d->replaygain.dither_context gets initialized later once we know the sample resolution */
cannam@86 292 d->test_only = (0 == outfilename);
cannam@86 293 d->analysis_mode = analysis_mode;
cannam@86 294 d->aopts = aopts;
cannam@86 295 d->skip_specification = skip_specification;
cannam@86 296 d->until_specification = until_specification;
cannam@86 297 d->cue_specification = cue_specification;
cannam@86 298
cannam@86 299 d->inbasefilename = grabbag__file_get_basename(infilename);
cannam@86 300 d->infilename = infilename;
cannam@86 301 d->outfilename = outfilename;
cannam@86 302
cannam@86 303 d->samples_processed = 0;
cannam@86 304 d->frame_counter = 0;
cannam@86 305 d->abort_flag = false;
cannam@86 306 d->aborting_due_to_until = false;
cannam@86 307 d->aborting_due_to_unparseable = false;
cannam@86 308
cannam@86 309 d->iff_headers_need_fixup = false;
cannam@86 310
cannam@86 311 d->total_samples = 0;
cannam@86 312 d->got_stream_info = false;
cannam@86 313 d->has_md5sum = false;
cannam@86 314 d->bps = 0;
cannam@86 315 d->channels = 0;
cannam@86 316 d->sample_rate = 0;
cannam@86 317 d->channel_mask = 0;
cannam@86 318
cannam@86 319 d->decode_position = 0;
cannam@86 320
cannam@86 321 d->decoder = 0;
cannam@86 322
cannam@86 323 d->fout = 0; /* initialized with an open file later if necessary */
cannam@86 324
cannam@86 325 d->foreign_metadata = foreign_metadata;
cannam@86 326
cannam@86 327 FLAC__ASSERT(!(d->test_only && d->analysis_mode));
cannam@86 328
cannam@86 329 if(!d->test_only) {
cannam@86 330 if(0 == strcmp(outfilename, "-")) {
cannam@86 331 d->fout = grabbag__file_get_binary_stdout();
cannam@86 332 }
cannam@86 333 else {
cannam@86 334 if(0 == (d->fout = fopen(outfilename, "wb"))) {
cannam@86 335 flac__utils_printf(stderr, 1, "%s: ERROR: can't open output file %s: %s\n", d->inbasefilename, outfilename, strerror(errno));
cannam@86 336 DecoderSession_destroy(d, /*error_occurred=*/true);
cannam@86 337 return false;
cannam@86 338 }
cannam@86 339 }
cannam@86 340 }
cannam@86 341
cannam@86 342 if(analysis_mode)
cannam@86 343 flac__analyze_init(aopts);
cannam@86 344
cannam@86 345 return true;
cannam@86 346 }
cannam@86 347
cannam@86 348 void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred)
cannam@86 349 {
cannam@86 350 if(0 != d->fout && d->fout != stdout) {
cannam@86 351 fclose(d->fout);
cannam@86 352 if(error_occurred)
cannam@86 353 unlink(d->outfilename);
cannam@86 354 }
cannam@86 355 }
cannam@86 356
cannam@86 357 FLAC__bool DecoderSession_init_decoder(DecoderSession *decoder_session, const char *infilename)
cannam@86 358 {
cannam@86 359 FLAC__StreamDecoderInitStatus init_status;
cannam@86 360 FLAC__uint32 test = 1;
cannam@86 361
cannam@86 362 is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
cannam@86 363
cannam@86 364 if(!decoder_session->analysis_mode && !decoder_session->test_only && (decoder_session->is_wave_out || decoder_session->is_aiff_out)) {
cannam@86 365 if(decoder_session->foreign_metadata) {
cannam@86 366 const char *error;
cannam@86 367 if(!flac__foreign_metadata_read_from_flac(decoder_session->foreign_metadata, infilename, &error)) {
cannam@86 368 flac__utils_printf(stderr, 1, "%s: ERROR reading foreign metadata: %s\n", decoder_session->inbasefilename, error);
cannam@86 369 return false;
cannam@86 370 }
cannam@86 371 }
cannam@86 372 }
cannam@86 373
cannam@86 374 decoder_session->decoder = FLAC__stream_decoder_new();
cannam@86 375
cannam@86 376 if(0 == decoder_session->decoder) {
cannam@86 377 flac__utils_printf(stderr, 1, "%s: ERROR creating the decoder instance\n", decoder_session->inbasefilename);
cannam@86 378 return false;
cannam@86 379 }
cannam@86 380
cannam@86 381 FLAC__stream_decoder_set_md5_checking(decoder_session->decoder, true);
cannam@86 382 if (0 != decoder_session->cue_specification)
cannam@86 383 FLAC__stream_decoder_set_metadata_respond(decoder_session->decoder, FLAC__METADATA_TYPE_CUESHEET);
cannam@86 384 if (decoder_session->replaygain.spec.apply)
cannam@86 385 FLAC__stream_decoder_set_metadata_respond(decoder_session->decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
cannam@86 386
cannam@86 387 #if FLAC__HAS_OGG
cannam@86 388 if(decoder_session->is_ogg) {
cannam@86 389 if(!decoder_session->use_first_serial_number)
cannam@86 390 FLAC__stream_decoder_set_ogg_serial_number(decoder_session->decoder, decoder_session->serial_number);
cannam@86 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);
cannam@86 392 }
cannam@86 393 else
cannam@86 394 #endif
cannam@86 395 {
cannam@86 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);
cannam@86 397 }
cannam@86 398
cannam@86 399 if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
cannam@86 400 print_error_with_init_status(decoder_session, "ERROR initializing decoder", init_status);
cannam@86 401 return false;
cannam@86 402 }
cannam@86 403
cannam@86 404 return true;
cannam@86 405 }
cannam@86 406
cannam@86 407 FLAC__bool DecoderSession_process(DecoderSession *d)
cannam@86 408 {
cannam@86 409 if(!FLAC__stream_decoder_process_until_end_of_metadata(d->decoder)) {
cannam@86 410 flac__utils_printf(stderr, 2, "\n");
cannam@86 411 print_error_with_state(d, "ERROR while decoding metadata");
cannam@86 412 return false;
cannam@86 413 }
cannam@86 414 if(FLAC__stream_decoder_get_state(d->decoder) > FLAC__STREAM_DECODER_END_OF_STREAM) {
cannam@86 415 flac__utils_printf(stderr, 2, "\n");
cannam@86 416 print_error_with_state(d, "ERROR during metadata decoding");
cannam@86 417 if(!d->continue_through_decode_errors)
cannam@86 418 return false;
cannam@86 419 }
cannam@86 420
cannam@86 421 if(d->abort_flag)
cannam@86 422 return false;
cannam@86 423
cannam@86 424 /* set channel mapping */
cannam@86 425 if(!d->channel_map_none) {
cannam@86 426 /* currently FLAC order matches SMPTE/WAVEFORMATEXTENSIBLE order, so no reordering is necessary; see encode.c */
cannam@86 427 /* only the channel mask must be set if it was not already picked up from the WAVEFORMATEXTENSIBLE_CHANNEL_MASK tag */
cannam@86 428 if(d->channels == 1) {
cannam@86 429 if(d->channel_mask == 0)
cannam@86 430 d->channel_mask = 0x0001;
cannam@86 431 }
cannam@86 432 else if(d->channels == 2) {
cannam@86 433 if(d->channel_mask == 0)
cannam@86 434 d->channel_mask = 0x0003;
cannam@86 435 }
cannam@86 436 else if(d->channels == 3) {
cannam@86 437 if(d->channel_mask == 0)
cannam@86 438 d->channel_mask = 0x0007;
cannam@86 439 }
cannam@86 440 else if(d->channels == 4) {
cannam@86 441 if(d->channel_mask == 0)
cannam@86 442 d->channel_mask = 0x0033;
cannam@86 443 }
cannam@86 444 else if(d->channels == 5) {
cannam@86 445 if(d->channel_mask == 0)
cannam@86 446 d->channel_mask = 0x0607;
cannam@86 447 }
cannam@86 448 else if(d->channels == 6) {
cannam@86 449 if(d->channel_mask == 0)
cannam@86 450 d->channel_mask = 0x060f;
cannam@86 451 }
cannam@86 452 }
cannam@86 453
cannam@86 454 /* write the WAVE/AIFF headers if necessary */
cannam@86 455 if(!d->analysis_mode && !d->test_only && (d->is_wave_out || d->is_aiff_out)) {
cannam@86 456 if(!write_iff_headers(d->fout, d, d->total_samples)) {
cannam@86 457 d->abort_flag = true;
cannam@86 458 return false;
cannam@86 459 }
cannam@86 460 }
cannam@86 461
cannam@86 462 if(d->skip_specification->value.samples > 0) {
cannam@86 463 const FLAC__uint64 skip = (FLAC__uint64)d->skip_specification->value.samples;
cannam@86 464
cannam@86 465 if(!FLAC__stream_decoder_seek_absolute(d->decoder, skip)) {
cannam@86 466 print_error_with_state(d, "ERROR seeking while skipping bytes");
cannam@86 467 return false;
cannam@86 468 }
cannam@86 469 }
cannam@86 470 if(!FLAC__stream_decoder_process_until_end_of_stream(d->decoder) && !d->aborting_due_to_until) {
cannam@86 471 flac__utils_printf(stderr, 2, "\n");
cannam@86 472 print_error_with_state(d, "ERROR while decoding data");
cannam@86 473 if(!d->continue_through_decode_errors)
cannam@86 474 return false;
cannam@86 475 }
cannam@86 476 if(
cannam@86 477 (d->abort_flag && !(d->aborting_due_to_until || d->continue_through_decode_errors)) ||
cannam@86 478 (FLAC__stream_decoder_get_state(d->decoder) > FLAC__STREAM_DECODER_END_OF_STREAM && !d->aborting_due_to_until)
cannam@86 479 ) {
cannam@86 480 flac__utils_printf(stderr, 2, "\n");
cannam@86 481 print_error_with_state(d, "ERROR during decoding");
cannam@86 482 return false;
cannam@86 483 }
cannam@86 484
cannam@86 485 if(!d->analysis_mode && !d->test_only && (d->is_wave_out || d->is_aiff_out) && ((d->total_samples * d->channels * ((d->bps+7)/8)) & 1)) {
cannam@86 486 if(flac__utils_fwrite("\000", 1, 1, d->fout) != 1) {
cannam@86 487 print_error_with_state(d, d->is_wave_out?
cannam@86 488 "ERROR writing pad byte to WAVE data chunk" :
cannam@86 489 "ERROR writing pad byte to AIFF SSND chunk"
cannam@86 490 );
cannam@86 491 return false;
cannam@86 492 }
cannam@86 493 }
cannam@86 494
cannam@86 495 return true;
cannam@86 496 }
cannam@86 497
cannam@86 498 int DecoderSession_finish_ok(DecoderSession *d)
cannam@86 499 {
cannam@86 500 FLAC__bool ok = true, md5_failure = false;
cannam@86 501
cannam@86 502 if(d->decoder) {
cannam@86 503 md5_failure = !FLAC__stream_decoder_finish(d->decoder) && !d->aborting_due_to_until;
cannam@86 504 print_stats(d);
cannam@86 505 FLAC__stream_decoder_delete(d->decoder);
cannam@86 506 }
cannam@86 507 if(d->analysis_mode)
cannam@86 508 flac__analyze_finish(d->aopts);
cannam@86 509 if(md5_failure) {
cannam@86 510 flac__utils_printf(stderr, 1, "\r%s: ERROR, MD5 signature mismatch\n", d->inbasefilename);
cannam@86 511 ok = d->continue_through_decode_errors;
cannam@86 512 }
cannam@86 513 else {
cannam@86 514 if(!d->got_stream_info) {
cannam@86 515 flac__utils_printf(stderr, 1, "\r%s: WARNING, cannot check MD5 signature since there was no STREAMINFO\n", d->inbasefilename);
cannam@86 516 ok = !d->treat_warnings_as_errors;
cannam@86 517 }
cannam@86 518 else if(!d->has_md5sum) {
cannam@86 519 flac__utils_printf(stderr, 1, "\r%s: WARNING, cannot check MD5 signature since it was unset in the STREAMINFO\n", d->inbasefilename);
cannam@86 520 ok = !d->treat_warnings_as_errors;
cannam@86 521 }
cannam@86 522 flac__utils_printf(stderr, 2, "\r%s: %s \n", d->inbasefilename, d->test_only? "ok ":d->analysis_mode?"done ":"done");
cannam@86 523 }
cannam@86 524 DecoderSession_destroy(d, /*error_occurred=*/!ok);
cannam@86 525 if(!d->analysis_mode && !d->test_only && (d->is_wave_out || d->is_aiff_out)) {
cannam@86 526 if(d->iff_headers_need_fixup || (!d->got_stream_info && strcmp(d->outfilename, "-"))) {
cannam@86 527 if(!fixup_iff_headers(d))
cannam@86 528 return 1;
cannam@86 529 }
cannam@86 530 if(d->foreign_metadata) {
cannam@86 531 const char *error;
cannam@86 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)) {
cannam@86 533 flac__utils_printf(stderr, 1, "ERROR updating foreign metadata from %s to %s: %s\n", d->infilename, d->outfilename, error);
cannam@86 534 return 1;
cannam@86 535 }
cannam@86 536 }
cannam@86 537 }
cannam@86 538 return ok? 0 : 1;
cannam@86 539 }
cannam@86 540
cannam@86 541 int DecoderSession_finish_error(DecoderSession *d)
cannam@86 542 {
cannam@86 543 if(d->decoder) {
cannam@86 544 (void)FLAC__stream_decoder_finish(d->decoder);
cannam@86 545 FLAC__stream_decoder_delete(d->decoder);
cannam@86 546 }
cannam@86 547 if(d->analysis_mode)
cannam@86 548 flac__analyze_finish(d->aopts);
cannam@86 549 DecoderSession_destroy(d, /*error_occurred=*/true);
cannam@86 550 return 1;
cannam@86 551 }
cannam@86 552
cannam@86 553 FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, unsigned sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input)
cannam@86 554 {
cannam@86 555 /* convert from mm:ss.sss to sample number if necessary */
cannam@86 556 flac__utils_canonicalize_skip_until_specification(spec, sample_rate);
cannam@86 557
cannam@86 558 /* special case: if "--until=-0", use the special value '0' to mean "end-of-stream" */
cannam@86 559 if(spec->is_relative && spec->value.samples == 0) {
cannam@86 560 spec->is_relative = false;
cannam@86 561 return true;
cannam@86 562 }
cannam@86 563
cannam@86 564 /* in any other case the total samples in the input must be known */
cannam@86 565 if(total_samples_in_input == 0) {
cannam@86 566 flac__utils_printf(stderr, 1, "%s: ERROR, cannot use --until when FLAC metadata has total sample count of 0\n", inbasefilename);
cannam@86 567 return false;
cannam@86 568 }
cannam@86 569
cannam@86 570 FLAC__ASSERT(spec->value_is_samples);
cannam@86 571
cannam@86 572 /* convert relative specifications to absolute */
cannam@86 573 if(spec->is_relative) {
cannam@86 574 if(spec->value.samples <= 0)
cannam@86 575 spec->value.samples += (FLAC__int64)total_samples_in_input;
cannam@86 576 else
cannam@86 577 spec->value.samples += skip;
cannam@86 578 spec->is_relative = false;
cannam@86 579 }
cannam@86 580
cannam@86 581 /* error check */
cannam@86 582 if(spec->value.samples < 0) {
cannam@86 583 flac__utils_printf(stderr, 1, "%s: ERROR, --until value is before beginning of input\n", inbasefilename);
cannam@86 584 return false;
cannam@86 585 }
cannam@86 586 if((FLAC__uint64)spec->value.samples <= skip) {
cannam@86 587 flac__utils_printf(stderr, 1, "%s: ERROR, --until value is before --skip point\n", inbasefilename);
cannam@86 588 return false;
cannam@86 589 }
cannam@86 590 if((FLAC__uint64)spec->value.samples > total_samples_in_input) {
cannam@86 591 flac__utils_printf(stderr, 1, "%s: ERROR, --until value is after end of input\n", inbasefilename);
cannam@86 592 return false;
cannam@86 593 }
cannam@86 594
cannam@86 595 return true;
cannam@86 596 }
cannam@86 597
cannam@86 598 FLAC__bool write_iff_headers(FILE *f, DecoderSession *decoder_session, FLAC__uint64 samples)
cannam@86 599 {
cannam@86 600 const char *fmt_desc = decoder_session->is_wave_out? "WAVE" : "AIFF";
cannam@86 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);
cannam@86 602 FLAC__uint64 data_size = samples * decoder_session->channels * ((decoder_session->bps+7)/8);
cannam@86 603 const FLAC__uint32 aligned_data_size = (FLAC__uint32)((data_size+1) & (~1U)); /* we'll check for overflow later */
cannam@86 604
cannam@86 605 unsigned foreign_metadata_size = 0; /* size of all non-audio non-fmt/COMM foreign metadata chunks */
cannam@86 606 foreign_metadata_t *fm = decoder_session->foreign_metadata;
cannam@86 607 size_t i;
cannam@86 608
cannam@86 609 if(samples == 0) {
cannam@86 610 if(f == stdout) {
cannam@86 611 flac__utils_printf(stderr, 1, "%s: WARNING, don't have accurate sample count available for %s header.\n", decoder_session->inbasefilename, fmt_desc);
cannam@86 612 flac__utils_printf(stderr, 1, " Generated %s file will have a data chunk size of 0. Try\n", fmt_desc);
cannam@86 613 flac__utils_printf(stderr, 1, " decoding directly to a file instead.\n");
cannam@86 614 if(decoder_session->treat_warnings_as_errors)
cannam@86 615 return false;
cannam@86 616 }
cannam@86 617 else {
cannam@86 618 decoder_session->iff_headers_need_fixup = true;
cannam@86 619 }
cannam@86 620 }
cannam@86 621
cannam@86 622 if(fm) {
cannam@86 623 FLAC__ASSERT(fm->format_block);
cannam@86 624 FLAC__ASSERT(fm->audio_block);
cannam@86 625 FLAC__ASSERT(fm->format_block < fm->audio_block);
cannam@86 626 /* calc foreign metadata size; for RIFF/AIFF we always skip the first chunk, format chunk, and sound chunk since we write our own */
cannam@86 627 for(i = 1; i < fm->num_blocks; i++) {
cannam@86 628 if(i != fm->format_block && i != fm->audio_block)
cannam@86 629 foreign_metadata_size += fm->blocks[i].size;
cannam@86 630 }
cannam@86 631 }
cannam@86 632
cannam@86 633 if(data_size + foreign_metadata_size + 60/*worst-case*/ >= 0xFFFFFFF4) {
cannam@86 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);
cannam@86 635 return false;
cannam@86 636 }
cannam@86 637
cannam@86 638 if(decoder_session->is_wave_out) {
cannam@86 639 if(flac__utils_fwrite("RIFF", 1, 4, f) != 4)
cannam@86 640 return false;
cannam@86 641
cannam@86 642 if(!write_little_endian_uint32(f, foreign_metadata_size + aligned_data_size + (is_waveformatextensible?60:36))) /* filesize-8 */
cannam@86 643 return false;
cannam@86 644
cannam@86 645 if(flac__utils_fwrite("WAVE", 1, 4, f) != 4)
cannam@86 646 return false;
cannam@86 647
cannam@86 648 decoder_session->fm_offset1 = ftello(f);
cannam@86 649
cannam@86 650 if(fm) {
cannam@86 651 /* seek forward to {allocate} or {skip over already-written chunks} before "fmt " */
cannam@86 652 for(i = 1; i < fm->format_block; i++) {
cannam@86 653 if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
cannam@86 654 flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata before \"fmt \"\n", decoder_session->inbasefilename);
cannam@86 655 return false;
cannam@86 656 }
cannam@86 657 }
cannam@86 658 }
cannam@86 659
cannam@86 660 if(!write_riff_wave_fmt_chunk(f, is_waveformatextensible, decoder_session->bps, decoder_session->channels, decoder_session->sample_rate, decoder_session->channel_mask))
cannam@86 661 return false;
cannam@86 662
cannam@86 663 decoder_session->fm_offset2 = ftello(f);
cannam@86 664
cannam@86 665 if(fm) {
cannam@86 666 /* seek forward to {allocate} or {skip over already-written chunks} after "fmt " but before "data" */
cannam@86 667 for(i = fm->format_block+1; i < fm->audio_block; i++) {
cannam@86 668 if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
cannam@86 669 flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata after \"fmt \"\n", decoder_session->inbasefilename);
cannam@86 670 return false;
cannam@86 671 }
cannam@86 672 }
cannam@86 673 }
cannam@86 674
cannam@86 675 if(flac__utils_fwrite("data", 1, 4, f) != 4)
cannam@86 676 return false;
cannam@86 677
cannam@86 678 if(!write_little_endian_uint32(f, (FLAC__uint32)data_size)) /* data size */
cannam@86 679 return false;
cannam@86 680
cannam@86 681 decoder_session->fm_offset3 = ftello(f) + aligned_data_size;
cannam@86 682 }
cannam@86 683 else {
cannam@86 684 FLAC__uint32 ssnd_offset_size = (fm? fm->ssnd_offset_size : 0);
cannam@86 685
cannam@86 686 if(flac__utils_fwrite("FORM", 1, 4, f) != 4)
cannam@86 687 return false;
cannam@86 688
cannam@86 689 if(!write_big_endian_uint32(f, foreign_metadata_size + aligned_data_size + 46 + ssnd_offset_size)) /* filesize-8 */
cannam@86 690 return false;
cannam@86 691
cannam@86 692 if(flac__utils_fwrite("AIFF", 1, 4, f) != 4)
cannam@86 693 return false;
cannam@86 694
cannam@86 695 decoder_session->fm_offset1 = ftello(f);
cannam@86 696
cannam@86 697 if(fm) {
cannam@86 698 /* seek forward to {allocate} or {skip over already-written chunks} before "COMM" */
cannam@86 699 for(i = 1; i < fm->format_block; i++) {
cannam@86 700 if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
cannam@86 701 flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata before \"COMM\"\n", decoder_session->inbasefilename);
cannam@86 702 return false;
cannam@86 703 }
cannam@86 704 }
cannam@86 705 }
cannam@86 706
cannam@86 707 if(!write_aiff_form_comm_chunk(f, samples, decoder_session->bps, decoder_session->channels, decoder_session->sample_rate))
cannam@86 708 return false;
cannam@86 709
cannam@86 710 decoder_session->fm_offset2 = ftello(f);
cannam@86 711
cannam@86 712 if(fm) {
cannam@86 713 /* seek forward to {allocate} or {skip over already-written chunks} after "COMM" but before "SSND" */
cannam@86 714 for(i = fm->format_block+1; i < fm->audio_block; i++) {
cannam@86 715 if(fseeko(f, fm->blocks[i].size, SEEK_CUR) < 0) {
cannam@86 716 flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping foreign metadata after \"COMM\"\n", decoder_session->inbasefilename);
cannam@86 717 return false;
cannam@86 718 }
cannam@86 719 }
cannam@86 720 }
cannam@86 721
cannam@86 722 if(flac__utils_fwrite("SSND", 1, 4, f) != 4)
cannam@86 723 return false;
cannam@86 724
cannam@86 725 if(!write_big_endian_uint32(f, (FLAC__uint32)data_size + 8 + ssnd_offset_size)) /* data size */
cannam@86 726 return false;
cannam@86 727
cannam@86 728 if(!write_big_endian_uint32(f, ssnd_offset_size))
cannam@86 729 return false;
cannam@86 730
cannam@86 731 if(!write_big_endian_uint32(f, 0/*block_size*/))
cannam@86 732 return false;
cannam@86 733
cannam@86 734 if(ssnd_offset_size) {
cannam@86 735 /* seek forward to {allocate} or {skip over already-written} SSND offset */
cannam@86 736 if(fseeko(f, ssnd_offset_size, SEEK_CUR) < 0) {
cannam@86 737 flac__utils_printf(stderr, 1, "%s: ERROR: allocating/skipping \"SSND\" offset\n", decoder_session->inbasefilename);
cannam@86 738 return false;
cannam@86 739 }
cannam@86 740 }
cannam@86 741
cannam@86 742 decoder_session->fm_offset3 = ftello(f) + aligned_data_size;
cannam@86 743 }
cannam@86 744
cannam@86 745 return true;
cannam@86 746 }
cannam@86 747
cannam@86 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)
cannam@86 749 {
cannam@86 750 if(flac__utils_fwrite("fmt ", 1, 4, f) != 4)
cannam@86 751 return false;
cannam@86 752
cannam@86 753 if(!write_little_endian_uint32(f, is_waveformatextensible? 40 : 16)) /* chunk size */
cannam@86 754 return false;
cannam@86 755
cannam@86 756 if(!write_little_endian_uint16(f, (FLAC__uint16)(is_waveformatextensible? 65534 : 1))) /* compression code */
cannam@86 757 return false;
cannam@86 758
cannam@86 759 if(!write_little_endian_uint16(f, (FLAC__uint16)channels))
cannam@86 760 return false;
cannam@86 761
cannam@86 762 if(!write_little_endian_uint32(f, sample_rate))
cannam@86 763 return false;
cannam@86 764
cannam@86 765 if(!write_little_endian_uint32(f, sample_rate * channels * ((bps+7) / 8)))
cannam@86 766 return false;
cannam@86 767
cannam@86 768 if(!write_little_endian_uint16(f, (FLAC__uint16)(channels * ((bps+7) / 8)))) /* block align */
cannam@86 769 return false;
cannam@86 770
cannam@86 771 if(!write_little_endian_uint16(f, (FLAC__uint16)(((bps+7)/8)*8))) /* bits per sample */
cannam@86 772 return false;
cannam@86 773
cannam@86 774 if(is_waveformatextensible) {
cannam@86 775 if(!write_little_endian_uint16(f, (FLAC__uint16)22)) /* cbSize */
cannam@86 776 return false;
cannam@86 777
cannam@86 778 if(!write_little_endian_uint16(f, (FLAC__uint16)bps)) /* validBitsPerSample */
cannam@86 779 return false;
cannam@86 780
cannam@86 781 if(!write_little_endian_uint32(f, channel_mask))
cannam@86 782 return false;
cannam@86 783
cannam@86 784 /* GUID = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} */
cannam@86 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)
cannam@86 786 return false;
cannam@86 787 }
cannam@86 788
cannam@86 789 return true;
cannam@86 790 }
cannam@86 791
cannam@86 792 FLAC__bool write_aiff_form_comm_chunk(FILE *f, FLAC__uint64 samples, unsigned bps, unsigned channels, unsigned sample_rate)
cannam@86 793 {
cannam@86 794 FLAC__ASSERT(samples <= 0xffffffff);
cannam@86 795
cannam@86 796 if(flac__utils_fwrite("COMM", 1, 4, f) != 4)
cannam@86 797 return false;
cannam@86 798
cannam@86 799 if(!write_big_endian_uint32(f, 18)) /* chunk size = 18 */
cannam@86 800 return false;
cannam@86 801
cannam@86 802 if(!write_big_endian_uint16(f, (FLAC__uint16)channels))
cannam@86 803 return false;
cannam@86 804
cannam@86 805 if(!write_big_endian_uint32(f, (FLAC__uint32)samples))
cannam@86 806 return false;
cannam@86 807
cannam@86 808 if(!write_big_endian_uint16(f, (FLAC__uint16)bps))
cannam@86 809 return false;
cannam@86 810
cannam@86 811 if(!write_sane_extended(f, sample_rate))
cannam@86 812 return false;
cannam@86 813
cannam@86 814 return true;
cannam@86 815 }
cannam@86 816
cannam@86 817 FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val)
cannam@86 818 {
cannam@86 819 FLAC__byte *b = (FLAC__byte*)(&val);
cannam@86 820 if(is_big_endian_host_) {
cannam@86 821 FLAC__byte tmp;
cannam@86 822 tmp = b[1]; b[1] = b[0]; b[0] = tmp;
cannam@86 823 }
cannam@86 824 return flac__utils_fwrite(b, 1, 2, f) == 2;
cannam@86 825 }
cannam@86 826
cannam@86 827 FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val)
cannam@86 828 {
cannam@86 829 FLAC__byte *b = (FLAC__byte*)(&val);
cannam@86 830 if(is_big_endian_host_) {
cannam@86 831 FLAC__byte tmp;
cannam@86 832 tmp = b[3]; b[3] = b[0]; b[0] = tmp;
cannam@86 833 tmp = b[2]; b[2] = b[1]; b[1] = tmp;
cannam@86 834 }
cannam@86 835 return flac__utils_fwrite(b, 1, 4, f) == 4;
cannam@86 836 }
cannam@86 837
cannam@86 838 FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val)
cannam@86 839 {
cannam@86 840 FLAC__byte *b = (FLAC__byte*)(&val);
cannam@86 841 if(!is_big_endian_host_) {
cannam@86 842 FLAC__byte tmp;
cannam@86 843 tmp = b[1]; b[1] = b[0]; b[0] = tmp;
cannam@86 844 }
cannam@86 845 return flac__utils_fwrite(b, 1, 2, f) == 2;
cannam@86 846 }
cannam@86 847
cannam@86 848 FLAC__bool write_big_endian_uint32(FILE *f, FLAC__uint32 val)
cannam@86 849 {
cannam@86 850 FLAC__byte *b = (FLAC__byte*)(&val);
cannam@86 851 if(!is_big_endian_host_) {
cannam@86 852 FLAC__byte tmp;
cannam@86 853 tmp = b[3]; b[3] = b[0]; b[0] = tmp;
cannam@86 854 tmp = b[2]; b[2] = b[1]; b[1] = tmp;
cannam@86 855 }
cannam@86 856 return flac__utils_fwrite(b, 1, 4, f) == 4;
cannam@86 857 }
cannam@86 858
cannam@86 859 FLAC__bool write_sane_extended(FILE *f, unsigned val)
cannam@86 860 /* Write to 'f' a SANE extended representation of 'val'. Return false if
cannam@86 861 * the write succeeds; return true otherwise.
cannam@86 862 *
cannam@86 863 * SANE extended is an 80-bit IEEE-754 representation with sign bit, 15 bits
cannam@86 864 * of exponent, and 64 bits of significand (mantissa). Unlike most IEEE-754
cannam@86 865 * representations, it does not imply a 1 above the MSB of the significand.
cannam@86 866 *
cannam@86 867 * Preconditions:
cannam@86 868 * val!=0U
cannam@86 869 */
cannam@86 870 {
cannam@86 871 unsigned int shift, exponent;
cannam@86 872
cannam@86 873 FLAC__ASSERT(val!=0U); /* handling 0 would require a special case */
cannam@86 874
cannam@86 875 for(shift= 0U; (val>>(31-shift))==0U; ++shift)
cannam@86 876 ;
cannam@86 877 val<<= shift;
cannam@86 878 exponent= 63U-(shift+32U); /* add 32 for unused second word */
cannam@86 879
cannam@86 880 if(!write_big_endian_uint16(f, (FLAC__uint16)(exponent+0x3FFF)))
cannam@86 881 return false;
cannam@86 882 if(!write_big_endian_uint32(f, val))
cannam@86 883 return false;
cannam@86 884 if(!write_big_endian_uint32(f, 0)) /* unused second word */
cannam@86 885 return false;
cannam@86 886
cannam@86 887 return true;
cannam@86 888 }
cannam@86 889
cannam@86 890 FLAC__bool fixup_iff_headers(DecoderSession *d)
cannam@86 891 {
cannam@86 892 const char *fmt_desc = (d->is_wave_out? "WAVE" : "AIFF");
cannam@86 893 FILE *f = fopen(d->outfilename, "r+b"); /* stream is positioned at beginning of file */
cannam@86 894
cannam@86 895 if(0 == f) {
cannam@86 896 flac__utils_printf(stderr, 1, "ERROR, couldn't open file %s while fixing up %s chunk size: %s\n", d->outfilename, fmt_desc, strerror(errno));
cannam@86 897 return false;
cannam@86 898 }
cannam@86 899
cannam@86 900 if(!write_iff_headers(f, d, d->samples_processed)) {
cannam@86 901 fclose(f);
cannam@86 902 return false;
cannam@86 903 }
cannam@86 904
cannam@86 905 fclose(f);
cannam@86 906 return true;
cannam@86 907 }
cannam@86 908
cannam@86 909 FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
cannam@86 910 {
cannam@86 911 DecoderSession *decoder_session = (DecoderSession*)client_data;
cannam@86 912 FILE *fout = decoder_session->fout;
cannam@86 913 const unsigned bps = frame->header.bits_per_sample, channels = frame->header.channels;
cannam@86 914 const unsigned shift = ((decoder_session->is_wave_out || decoder_session->is_aiff_out) && (bps%8)? 8-(bps%8): 0);
cannam@86 915 FLAC__bool is_big_endian = (decoder_session->is_aiff_out? true : (decoder_session->is_wave_out? false : decoder_session->is_big_endian));
cannam@86 916 FLAC__bool is_unsigned_samples = (decoder_session->is_aiff_out? false : (decoder_session->is_wave_out? bps<=8 : decoder_session->is_unsigned_samples));
cannam@86 917 unsigned wide_samples = frame->header.blocksize, wide_sample, sample, channel, byte;
cannam@86 918 unsigned frame_bytes = 0;
cannam@86 919 static FLAC__int8 s8buffer[FLAC__MAX_BLOCK_SIZE * FLAC__MAX_CHANNELS * sizeof(FLAC__int32)]; /* WATCHOUT: can be up to 2 megs */
cannam@86 920 FLAC__uint8 *u8buffer = (FLAC__uint8 *)s8buffer;
cannam@86 921 FLAC__int16 *s16buffer = (FLAC__int16 *)s8buffer;
cannam@86 922 FLAC__uint16 *u16buffer = (FLAC__uint16 *)s8buffer;
cannam@86 923 FLAC__int32 *s32buffer = (FLAC__int32 *)s8buffer;
cannam@86 924 FLAC__uint32 *u32buffer = (FLAC__uint32 *)s8buffer;
cannam@86 925 size_t bytes_to_write = 0;
cannam@86 926
cannam@86 927 (void)decoder;
cannam@86 928
cannam@86 929 if(decoder_session->abort_flag)
cannam@86 930 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
cannam@86 931
cannam@86 932 /* sanity-check the bits-per-sample */
cannam@86 933 if(decoder_session->bps) {
cannam@86 934 if(bps != decoder_session->bps) {
cannam@86 935 if(decoder_session->got_stream_info)
cannam@86 936 flac__utils_printf(stderr, 1, "%s: ERROR, bits-per-sample is %u in frame but %u in STREAMINFO\n", decoder_session->inbasefilename, bps, decoder_session->bps);
cannam@86 937 else
cannam@86 938 flac__utils_printf(stderr, 1, "%s: ERROR, bits-per-sample is %u in this frame but %u in previous frames\n", decoder_session->inbasefilename, bps, decoder_session->bps);
cannam@86 939 if(!decoder_session->continue_through_decode_errors)
cannam@86 940 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
cannam@86 941 }
cannam@86 942 }
cannam@86 943 else {
cannam@86 944 /* must not have gotten STREAMINFO, save the bps from the frame header */
cannam@86 945 FLAC__ASSERT(!decoder_session->got_stream_info);
cannam@86 946 decoder_session->bps = bps;
cannam@86 947 }
cannam@86 948
cannam@86 949 /* sanity-check the #channels */
cannam@86 950 if(decoder_session->channels) {
cannam@86 951 if(channels != decoder_session->channels) {
cannam@86 952 if(decoder_session->got_stream_info)
cannam@86 953 flac__utils_printf(stderr, 1, "%s: ERROR, channels is %u in frame but %u in STREAMINFO\n", decoder_session->inbasefilename, channels, decoder_session->channels);
cannam@86 954 else
cannam@86 955 flac__utils_printf(stderr, 1, "%s: ERROR, channels is %u in this frame but %u in previous frames\n", decoder_session->inbasefilename, channels, decoder_session->channels);
cannam@86 956 if(!decoder_session->continue_through_decode_errors)
cannam@86 957 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
cannam@86 958 }
cannam@86 959 }
cannam@86 960 else {
cannam@86 961 /* must not have gotten STREAMINFO, save the #channels from the frame header */
cannam@86 962 FLAC__ASSERT(!decoder_session->got_stream_info);
cannam@86 963 decoder_session->channels = channels;
cannam@86 964 }
cannam@86 965
cannam@86 966 /* sanity-check the sample rate */
cannam@86 967 if(decoder_session->sample_rate) {
cannam@86 968 if(frame->header.sample_rate != decoder_session->sample_rate) {
cannam@86 969 if(decoder_session->got_stream_info)
cannam@86 970 flac__utils_printf(stderr, 1, "%s: ERROR, sample rate is %u in frame but %u in STREAMINFO\n", decoder_session->inbasefilename, frame->header.sample_rate, decoder_session->sample_rate);
cannam@86 971 else
cannam@86 972 flac__utils_printf(stderr, 1, "%s: ERROR, sample rate is %u in this frame but %u in previous frames\n", decoder_session->inbasefilename, frame->header.sample_rate, decoder_session->sample_rate);
cannam@86 973 if(!decoder_session->continue_through_decode_errors)
cannam@86 974 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
cannam@86 975 }
cannam@86 976 }
cannam@86 977 else {
cannam@86 978 /* must not have gotten STREAMINFO, save the sample rate from the frame header */
cannam@86 979 FLAC__ASSERT(!decoder_session->got_stream_info);
cannam@86 980 decoder_session->sample_rate = frame->header.sample_rate;
cannam@86 981 }
cannam@86 982
cannam@86 983 /*
cannam@86 984 * limit the number of samples to accept based on --until
cannam@86 985 */
cannam@86 986 FLAC__ASSERT(!decoder_session->skip_specification->is_relative);
cannam@86 987 /* if we never got the total_samples from the metadata, the skip and until specs would never have been canonicalized, so protect against that: */
cannam@86 988 if(decoder_session->skip_specification->is_relative) {
cannam@86 989 if(decoder_session->skip_specification->value.samples == 0) /* special case for when no --skip was given */
cannam@86 990 decoder_session->skip_specification->is_relative = false; /* convert to our meaning of beginning-of-stream */
cannam@86 991 else {
cannam@86 992 flac__utils_printf(stderr, 1, "%s: ERROR, cannot use --skip because the total sample count was not found in the metadata\n", decoder_session->inbasefilename);
cannam@86 993 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
cannam@86 994 }
cannam@86 995 }
cannam@86 996 if(decoder_session->until_specification->is_relative) {
cannam@86 997 if(decoder_session->until_specification->value.samples == 0) /* special case for when no --until was given */
cannam@86 998 decoder_session->until_specification->is_relative = false; /* convert to our meaning of end-of-stream */
cannam@86 999 else {
cannam@86 1000 flac__utils_printf(stderr, 1, "%s: ERROR, cannot use --until because the total sample count was not found in the metadata\n", decoder_session->inbasefilename);
cannam@86 1001 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
cannam@86 1002 }
cannam@86 1003 }
cannam@86 1004 FLAC__ASSERT(decoder_session->skip_specification->value.samples >= 0);
cannam@86 1005 FLAC__ASSERT(decoder_session->until_specification->value.samples >= 0);
cannam@86 1006 if(decoder_session->until_specification->value.samples > 0) {
cannam@86 1007 const FLAC__uint64 skip = (FLAC__uint64)decoder_session->skip_specification->value.samples;
cannam@86 1008 const FLAC__uint64 until = (FLAC__uint64)decoder_session->until_specification->value.samples;
cannam@86 1009 const FLAC__uint64 input_samples_passed = skip + decoder_session->samples_processed;
cannam@86 1010 FLAC__ASSERT(until >= input_samples_passed);
cannam@86 1011 if(input_samples_passed + wide_samples > until)
cannam@86 1012 wide_samples = (unsigned)(until - input_samples_passed);
cannam@86 1013 if (wide_samples == 0) {
cannam@86 1014 decoder_session->abort_flag = true;
cannam@86 1015 decoder_session->aborting_due_to_until = true;
cannam@86 1016 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
cannam@86 1017 }
cannam@86 1018 }
cannam@86 1019
cannam@86 1020 if(decoder_session->analysis_mode) {
cannam@86 1021 FLAC__uint64 dpos;
cannam@86 1022 FLAC__stream_decoder_get_decode_position(decoder_session->decoder, &dpos);
cannam@86 1023 frame_bytes = (unsigned)(dpos-decoder_session->decode_position);
cannam@86 1024 decoder_session->decode_position = dpos;
cannam@86 1025 }
cannam@86 1026
cannam@86 1027 if(wide_samples > 0) {
cannam@86 1028 decoder_session->samples_processed += wide_samples;
cannam@86 1029 decoder_session->frame_counter++;
cannam@86 1030
cannam@86 1031 if(!(decoder_session->frame_counter & 0x3f))
cannam@86 1032 print_stats(decoder_session);
cannam@86 1033
cannam@86 1034 if(decoder_session->analysis_mode) {
cannam@86 1035 flac__analyze_frame(frame, decoder_session->frame_counter-1, decoder_session->decode_position-frame_bytes, frame_bytes, decoder_session->aopts, fout);
cannam@86 1036 }
cannam@86 1037 else if(!decoder_session->test_only) {
cannam@86 1038 if(shift && !decoder_session->replaygain.apply) {
cannam@86 1039 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++)
cannam@86 1040 for(channel = 0; channel < channels; channel++)
cannam@86 1041 ((FLAC__int32**)buffer)[channel][wide_sample] <<= shift;/*@@@@@@un-const'ing the buffer is hacky but safe*/
cannam@86 1042 }
cannam@86 1043 if(decoder_session->replaygain.apply) {
cannam@86 1044 bytes_to_write = FLAC__replaygain_synthesis__apply_gain(
cannam@86 1045 u8buffer,
cannam@86 1046 !is_big_endian,
cannam@86 1047 is_unsigned_samples,
cannam@86 1048 buffer,
cannam@86 1049 wide_samples,
cannam@86 1050 channels,
cannam@86 1051 bps, /* source_bps */
cannam@86 1052 bps+shift, /* target_bps */
cannam@86 1053 decoder_session->replaygain.scale,
cannam@86 1054 decoder_session->replaygain.spec.limiter == RGSS_LIMIT__HARD, /* hard_limit */
cannam@86 1055 decoder_session->replaygain.spec.noise_shaping != NOISE_SHAPING_NONE, /* do_dithering */
cannam@86 1056 &decoder_session->replaygain.dither_context
cannam@86 1057 );
cannam@86 1058 }
cannam@86 1059 /* first some special code for common cases */
cannam@86 1060 else if(is_big_endian == is_big_endian_host_ && !is_unsigned_samples && channels == 2 && bps+shift == 16) {
cannam@86 1061 FLAC__int16 *buf1_ = s16buffer + 1;
cannam@86 1062 if(is_big_endian)
cannam@86 1063 memcpy(s16buffer, ((FLAC__byte*)(buffer[0]))+2, sizeof(FLAC__int32) * wide_samples - 2);
cannam@86 1064 else
cannam@86 1065 memcpy(s16buffer, buffer[0], sizeof(FLAC__int32) * wide_samples);
cannam@86 1066 for(sample = 0; sample < wide_samples; sample++, buf1_+=2)
cannam@86 1067 *buf1_ = (FLAC__int16)buffer[1][sample];
cannam@86 1068 bytes_to_write = 4 * sample;
cannam@86 1069 }
cannam@86 1070 else if(is_big_endian == is_big_endian_host_ && !is_unsigned_samples && channels == 1 && bps+shift == 16) {
cannam@86 1071 FLAC__int16 *buf1_ = s16buffer;
cannam@86 1072 for(sample = 0; sample < wide_samples; sample++)
cannam@86 1073 *buf1_++ = (FLAC__int16)buffer[0][sample];
cannam@86 1074 bytes_to_write = 2 * sample;
cannam@86 1075 }
cannam@86 1076 /* generic code for the rest */
cannam@86 1077 else if(bps+shift == 16) {
cannam@86 1078 if(is_unsigned_samples) {
cannam@86 1079 if(channels == 2) {
cannam@86 1080 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
cannam@86 1081 u16buffer[sample++] = (FLAC__uint16)(buffer[0][wide_sample] + 0x8000);
cannam@86 1082 u16buffer[sample++] = (FLAC__uint16)(buffer[1][wide_sample] + 0x8000);
cannam@86 1083 }
cannam@86 1084 }
cannam@86 1085 else if(channels == 1) {
cannam@86 1086 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
cannam@86 1087 u16buffer[sample++] = (FLAC__uint16)(buffer[0][wide_sample] + 0x8000);
cannam@86 1088 }
cannam@86 1089 else { /* works for any 'channels' but above flavors are faster for 1 and 2 */
cannam@86 1090 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
cannam@86 1091 for(channel = 0; channel < channels; channel++, sample++)
cannam@86 1092 u16buffer[sample] = (FLAC__uint16)(buffer[channel][wide_sample] + 0x8000);
cannam@86 1093 }
cannam@86 1094 }
cannam@86 1095 else {
cannam@86 1096 if(channels == 2) {
cannam@86 1097 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
cannam@86 1098 s16buffer[sample++] = (FLAC__int16)(buffer[0][wide_sample]);
cannam@86 1099 s16buffer[sample++] = (FLAC__int16)(buffer[1][wide_sample]);
cannam@86 1100 }
cannam@86 1101 }
cannam@86 1102 else if(channels == 1) {
cannam@86 1103 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
cannam@86 1104 s16buffer[sample++] = (FLAC__int16)(buffer[0][wide_sample]);
cannam@86 1105 }
cannam@86 1106 else { /* works for any 'channels' but above flavors are faster for 1 and 2 */
cannam@86 1107 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
cannam@86 1108 for(channel = 0; channel < channels; channel++, sample++)
cannam@86 1109 s16buffer[sample] = (FLAC__int16)(buffer[channel][wide_sample]);
cannam@86 1110 }
cannam@86 1111 }
cannam@86 1112 if(is_big_endian != is_big_endian_host_) {
cannam@86 1113 unsigned char tmp;
cannam@86 1114 const unsigned bytes = sample * 2;
cannam@86 1115 for(byte = 0; byte < bytes; byte += 2) {
cannam@86 1116 tmp = u8buffer[byte];
cannam@86 1117 u8buffer[byte] = u8buffer[byte+1];
cannam@86 1118 u8buffer[byte+1] = tmp;
cannam@86 1119 }
cannam@86 1120 }
cannam@86 1121 bytes_to_write = 2 * sample;
cannam@86 1122 }
cannam@86 1123 else if(bps+shift == 24) {
cannam@86 1124 if(is_unsigned_samples) {
cannam@86 1125 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
cannam@86 1126 for(channel = 0; channel < channels; channel++, sample++)
cannam@86 1127 u32buffer[sample] = buffer[channel][wide_sample] + 0x800000;
cannam@86 1128 }
cannam@86 1129 else {
cannam@86 1130 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
cannam@86 1131 for(channel = 0; channel < channels; channel++, sample++)
cannam@86 1132 s32buffer[sample] = buffer[channel][wide_sample];
cannam@86 1133 }
cannam@86 1134 if(is_big_endian != is_big_endian_host_) {
cannam@86 1135 unsigned char tmp;
cannam@86 1136 const unsigned bytes = sample * 4;
cannam@86 1137 for(byte = 0; byte < bytes; byte += 4) {
cannam@86 1138 tmp = u8buffer[byte];
cannam@86 1139 u8buffer[byte] = u8buffer[byte+3];
cannam@86 1140 u8buffer[byte+3] = tmp;
cannam@86 1141 tmp = u8buffer[byte+1];
cannam@86 1142 u8buffer[byte+1] = u8buffer[byte+2];
cannam@86 1143 u8buffer[byte+2] = tmp;
cannam@86 1144 }
cannam@86 1145 }
cannam@86 1146 if(is_big_endian) {
cannam@86 1147 unsigned lbyte;
cannam@86 1148 const unsigned bytes = sample * 4;
cannam@86 1149 for(lbyte = byte = 0; byte < bytes; ) {
cannam@86 1150 byte++;
cannam@86 1151 u8buffer[lbyte++] = u8buffer[byte++];
cannam@86 1152 u8buffer[lbyte++] = u8buffer[byte++];
cannam@86 1153 u8buffer[lbyte++] = u8buffer[byte++];
cannam@86 1154 }
cannam@86 1155 }
cannam@86 1156 else {
cannam@86 1157 unsigned lbyte;
cannam@86 1158 const unsigned bytes = sample * 4;
cannam@86 1159 for(lbyte = byte = 0; byte < bytes; ) {
cannam@86 1160 u8buffer[lbyte++] = u8buffer[byte++];
cannam@86 1161 u8buffer[lbyte++] = u8buffer[byte++];
cannam@86 1162 u8buffer[lbyte++] = u8buffer[byte++];
cannam@86 1163 byte++;
cannam@86 1164 }
cannam@86 1165 }
cannam@86 1166 bytes_to_write = 3 * sample;
cannam@86 1167 }
cannam@86 1168 else if(bps+shift == 8) {
cannam@86 1169 if(is_unsigned_samples) {
cannam@86 1170 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
cannam@86 1171 for(channel = 0; channel < channels; channel++, sample++)
cannam@86 1172 u8buffer[sample] = (FLAC__uint8)(buffer[channel][wide_sample] + 0x80);
cannam@86 1173 }
cannam@86 1174 else {
cannam@86 1175 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
cannam@86 1176 for(channel = 0; channel < channels; channel++, sample++)
cannam@86 1177 s8buffer[sample] = (FLAC__int8)(buffer[channel][wide_sample]);
cannam@86 1178 }
cannam@86 1179 bytes_to_write = sample;
cannam@86 1180 }
cannam@86 1181 else {
cannam@86 1182 FLAC__ASSERT(0);
cannam@86 1183 /* double protection */
cannam@86 1184 decoder_session->abort_flag = true;
cannam@86 1185 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
cannam@86 1186 }
cannam@86 1187 }
cannam@86 1188 }
cannam@86 1189 if(bytes_to_write > 0) {
cannam@86 1190 if(flac__utils_fwrite(u8buffer, 1, bytes_to_write, fout) != bytes_to_write) {
cannam@86 1191 /* if a pipe closed when writing to stdout, we let it go without an error message */
cannam@86 1192 if(errno == EPIPE && decoder_session->fout == stdout)
cannam@86 1193 decoder_session->aborting_due_to_until = true;
cannam@86 1194 decoder_session->abort_flag = true;
cannam@86 1195 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
cannam@86 1196 }
cannam@86 1197 }
cannam@86 1198 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
cannam@86 1199 }
cannam@86 1200
cannam@86 1201 void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
cannam@86 1202 {
cannam@86 1203 DecoderSession *decoder_session = (DecoderSession*)client_data;
cannam@86 1204
cannam@86 1205 if(decoder_session->analysis_mode)
cannam@86 1206 FLAC__stream_decoder_get_decode_position(decoder, &decoder_session->decode_position);
cannam@86 1207
cannam@86 1208 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
cannam@86 1209 FLAC__uint64 skip, until;
cannam@86 1210 decoder_session->got_stream_info = true;
cannam@86 1211 decoder_session->has_md5sum = memcmp(metadata->data.stream_info.md5sum, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16);
cannam@86 1212 decoder_session->bps = metadata->data.stream_info.bits_per_sample;
cannam@86 1213 decoder_session->channels = metadata->data.stream_info.channels;
cannam@86 1214 decoder_session->sample_rate = metadata->data.stream_info.sample_rate;
cannam@86 1215
cannam@86 1216 flac__utils_canonicalize_skip_until_specification(decoder_session->skip_specification, decoder_session->sample_rate);
cannam@86 1217 FLAC__ASSERT(decoder_session->skip_specification->value.samples >= 0);
cannam@86 1218 skip = (FLAC__uint64)decoder_session->skip_specification->value.samples;
cannam@86 1219
cannam@86 1220 /* remember, metadata->data.stream_info.total_samples can be 0, meaning 'unknown' */
cannam@86 1221 if(metadata->data.stream_info.total_samples > 0 && skip >= metadata->data.stream_info.total_samples) {
cannam@86 1222 flac__utils_printf(stderr, 1, "%s: ERROR trying to --skip more samples than in stream\n", decoder_session->inbasefilename);
cannam@86 1223 decoder_session->abort_flag = true;
cannam@86 1224 return;
cannam@86 1225 }
cannam@86 1226 else if(metadata->data.stream_info.total_samples == 0 && skip > 0) {
cannam@86 1227 flac__utils_printf(stderr, 1, "%s: ERROR, can't --skip when FLAC metadata has total sample count of 0\n", decoder_session->inbasefilename);
cannam@86 1228 decoder_session->abort_flag = true;
cannam@86 1229 return;
cannam@86 1230 }
cannam@86 1231 FLAC__ASSERT(skip == 0 || 0 == decoder_session->cue_specification);
cannam@86 1232 decoder_session->total_samples = metadata->data.stream_info.total_samples - skip;
cannam@86 1233
cannam@86 1234 /* note that we use metadata->data.stream_info.total_samples instead of decoder_session->total_samples */
cannam@86 1235 if(!canonicalize_until_specification(decoder_session->until_specification, decoder_session->inbasefilename, decoder_session->sample_rate, skip, metadata->data.stream_info.total_samples)) {
cannam@86 1236 decoder_session->abort_flag = true;
cannam@86 1237 return;
cannam@86 1238 }
cannam@86 1239 FLAC__ASSERT(decoder_session->until_specification->value.samples >= 0);
cannam@86 1240 until = (FLAC__uint64)decoder_session->until_specification->value.samples;
cannam@86 1241
cannam@86 1242 if(until > 0) {
cannam@86 1243 FLAC__ASSERT(decoder_session->total_samples != 0);
cannam@86 1244 FLAC__ASSERT(0 == decoder_session->cue_specification);
cannam@86 1245 decoder_session->total_samples -= (metadata->data.stream_info.total_samples - until);
cannam@86 1246 }
cannam@86 1247
cannam@86 1248 if(decoder_session->bps < 4 || decoder_session->bps > 24) {
cannam@86 1249 flac__utils_printf(stderr, 1, "%s: ERROR: bits per sample is %u, must be 4-24\n", decoder_session->inbasefilename, decoder_session->bps);
cannam@86 1250 decoder_session->abort_flag = true;
cannam@86 1251 return;
cannam@86 1252 }
cannam@86 1253 }
cannam@86 1254 else if(metadata->type == FLAC__METADATA_TYPE_CUESHEET) {
cannam@86 1255 /* remember, at this point, decoder_session->total_samples can be 0, meaning 'unknown' */
cannam@86 1256 if(decoder_session->total_samples == 0) {
cannam@86 1257 flac__utils_printf(stderr, 1, "%s: ERROR can't use --cue when FLAC metadata has total sample count of 0\n", decoder_session->inbasefilename);
cannam@86 1258 decoder_session->abort_flag = true;
cannam@86 1259 return;
cannam@86 1260 }
cannam@86 1261
cannam@86 1262 flac__utils_canonicalize_cue_specification(decoder_session->cue_specification, &metadata->data.cue_sheet, decoder_session->total_samples, decoder_session->skip_specification, decoder_session->until_specification);
cannam@86 1263
cannam@86 1264 FLAC__ASSERT(!decoder_session->skip_specification->is_relative);
cannam@86 1265 FLAC__ASSERT(decoder_session->skip_specification->value_is_samples);
cannam@86 1266
cannam@86 1267 FLAC__ASSERT(!decoder_session->until_specification->is_relative);
cannam@86 1268 FLAC__ASSERT(decoder_session->until_specification->value_is_samples);
cannam@86 1269
cannam@86 1270 FLAC__ASSERT(decoder_session->skip_specification->value.samples >= 0);
cannam@86 1271 FLAC__ASSERT(decoder_session->until_specification->value.samples >= 0);
cannam@86 1272 FLAC__ASSERT((FLAC__uint64)decoder_session->until_specification->value.samples <= decoder_session->total_samples);
cannam@86 1273 FLAC__ASSERT(decoder_session->skip_specification->value.samples <= decoder_session->until_specification->value.samples);
cannam@86 1274
cannam@86 1275 decoder_session->total_samples = decoder_session->until_specification->value.samples - decoder_session->skip_specification->value.samples;
cannam@86 1276 }
cannam@86 1277 else if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
cannam@86 1278 if (decoder_session->replaygain.spec.apply) {
cannam@86 1279 double reference, gain, peak;
cannam@86 1280 if (!(decoder_session->replaygain.apply = grabbag__replaygain_load_from_vorbiscomment(metadata, decoder_session->replaygain.spec.use_album_gain, /*strict=*/false, &reference, &gain, &peak))) {
cannam@86 1281 flac__utils_printf(stderr, 1, "%s: WARNING: can't get %s (or even %s) ReplayGain tags\n", decoder_session->inbasefilename, decoder_session->replaygain.spec.use_album_gain? "album":"track", decoder_session->replaygain.spec.use_album_gain? "track":"album");
cannam@86 1282 if(decoder_session->treat_warnings_as_errors) {
cannam@86 1283 decoder_session->abort_flag = true;
cannam@86 1284 return;
cannam@86 1285 }
cannam@86 1286 }
cannam@86 1287 else {
cannam@86 1288 const char *ls[] = { "no", "peak", "hard" };
cannam@86 1289 const char *ns[] = { "no", "low", "medium", "high" };
cannam@86 1290 decoder_session->replaygain.scale = grabbag__replaygain_compute_scale_factor(peak, gain, decoder_session->replaygain.spec.preamp, decoder_session->replaygain.spec.limiter == RGSS_LIMIT__PEAK);
cannam@86 1291 FLAC__ASSERT(decoder_session->bps > 0 && decoder_session->bps <= 32);
cannam@86 1292 FLAC__replaygain_synthesis__init_dither_context(&decoder_session->replaygain.dither_context, decoder_session->bps, decoder_session->replaygain.spec.noise_shaping);
cannam@86 1293 flac__utils_printf(stderr, 1, "%s: INFO: applying %s ReplayGain (gain=%0.2fdB+preamp=%0.1fdB, %s noise shaping, %s limiting) to output\n", decoder_session->inbasefilename, decoder_session->replaygain.spec.use_album_gain? "album":"track", gain, decoder_session->replaygain.spec.preamp, ns[decoder_session->replaygain.spec.noise_shaping], ls[decoder_session->replaygain.spec.limiter]);
cannam@86 1294 flac__utils_printf(stderr, 1, "%s: WARNING: applying ReplayGain is not lossless\n", decoder_session->inbasefilename);
cannam@86 1295 /* don't check if(decoder_session->treat_warnings_as_errors) because the user explicitly asked for it */
cannam@86 1296 }
cannam@86 1297 }
cannam@86 1298 (void)flac__utils_get_channel_mask_tag(metadata, &decoder_session->channel_mask);
cannam@86 1299 }
cannam@86 1300 }
cannam@86 1301
cannam@86 1302 void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
cannam@86 1303 {
cannam@86 1304 DecoderSession *decoder_session = (DecoderSession*)client_data;
cannam@86 1305 (void)decoder;
cannam@86 1306 flac__utils_printf(stderr, 1, "%s: *** Got error code %d:%s\n", decoder_session->inbasefilename, status, FLAC__StreamDecoderErrorStatusString[status]);
cannam@86 1307 if(!decoder_session->continue_through_decode_errors) {
cannam@86 1308 decoder_session->abort_flag = true;
cannam@86 1309 if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM)
cannam@86 1310 decoder_session->aborting_due_to_unparseable = true;
cannam@86 1311 }
cannam@86 1312 }
cannam@86 1313
cannam@86 1314 void print_error_with_init_status(const DecoderSession *d, const char *message, FLAC__StreamDecoderInitStatus init_status)
cannam@86 1315 {
cannam@86 1316 const int ilen = strlen(d->inbasefilename) + 1;
cannam@86 1317
cannam@86 1318 flac__utils_printf(stderr, 1, "\n%s: %s\n", d->inbasefilename, message);
cannam@86 1319
cannam@86 1320 flac__utils_printf(stderr, 1, "%*s init status = %s\n", ilen, "", FLAC__StreamDecoderInitStatusString[init_status]);
cannam@86 1321
cannam@86 1322 /* print out some more info for some errors: */
cannam@86 1323 if (init_status == FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE) {
cannam@86 1324 flac__utils_printf(stderr, 1,
cannam@86 1325 "\n"
cannam@86 1326 "An error occurred opening the input file; it is likely that it does not exist\n"
cannam@86 1327 "or is not readable.\n"
cannam@86 1328 );
cannam@86 1329 }
cannam@86 1330 }
cannam@86 1331
cannam@86 1332 void print_error_with_state(const DecoderSession *d, const char *message)
cannam@86 1333 {
cannam@86 1334 const int ilen = strlen(d->inbasefilename) + 1;
cannam@86 1335
cannam@86 1336 flac__utils_printf(stderr, 1, "\n%s: %s\n", d->inbasefilename, message);
cannam@86 1337 flac__utils_printf(stderr, 1, "%*s state = %s\n", ilen, "", FLAC__stream_decoder_get_resolved_state_string(d->decoder));
cannam@86 1338
cannam@86 1339 /* print out some more info for some errors: */
cannam@86 1340 if (d->aborting_due_to_unparseable) {
cannam@86 1341 flac__utils_printf(stderr, 1,
cannam@86 1342 "\n"
cannam@86 1343 "The FLAC stream may have been created by a more advanced encoder. Try\n"
cannam@86 1344 " metaflac --show-vendor-tag %s\n"
cannam@86 1345 "If the version number is greater than %s, this decoder is probably\n"
cannam@86 1346 "not able to decode the file. If the version number is not, the file\n"
cannam@86 1347 "may be corrupted, or you may have found a bug. In this case please\n"
cannam@86 1348 "submit a bug report to\n"
cannam@86 1349 " http://sourceforge.net/bugs/?func=addbug&group_id=13478\n"
cannam@86 1350 "Make sure to use the \"Monitor\" feature to monitor the bug status.\n",
cannam@86 1351 d->inbasefilename, FLAC__VERSION_STRING
cannam@86 1352 );
cannam@86 1353 }
cannam@86 1354 }
cannam@86 1355
cannam@86 1356 void print_stats(const DecoderSession *decoder_session)
cannam@86 1357 {
cannam@86 1358 if(flac__utils_verbosity_ >= 2) {
cannam@86 1359 #if defined _MSC_VER || defined __MINGW32__
cannam@86 1360 /* with MSVC you have to spoon feed it the casting */
cannam@86 1361 const double progress = (double)(FLAC__int64)decoder_session->samples_processed / (double)(FLAC__int64)decoder_session->total_samples * 100.0;
cannam@86 1362 #else
cannam@86 1363 const double progress = (double)decoder_session->samples_processed / (double)decoder_session->total_samples * 100.0;
cannam@86 1364 #endif
cannam@86 1365 if(decoder_session->total_samples > 0) {
cannam@86 1366 fprintf(stderr, "\r%s: %s%u%% complete",
cannam@86 1367 decoder_session->inbasefilename,
cannam@86 1368 decoder_session->test_only? "testing, " : decoder_session->analysis_mode? "analyzing, " : "",
cannam@86 1369 (unsigned)floor(progress + 0.5)
cannam@86 1370 );
cannam@86 1371 }
cannam@86 1372 else {
cannam@86 1373 fprintf(stderr, "\r%s: %s %u samples",
cannam@86 1374 decoder_session->inbasefilename,
cannam@86 1375 decoder_session->test_only? "tested" : decoder_session->analysis_mode? "analyzed" : "wrote",
cannam@86 1376 (unsigned)decoder_session->samples_processed
cannam@86 1377 );
cannam@86 1378 }
cannam@86 1379 }
cannam@86 1380 }