decode.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:46k
源码类别:

Windows CE

开发平台:

C/C++

  1. /* flac - Command-line FLAC encoder/decoder
  2.  * Copyright (C) 2000,2001,2002,2003,2004,2005  Josh Coalson
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #if defined _WIN32 && !defined __CYGWIN__
  22. /* where MSVC puts unlink() */
  23. # include <io.h>
  24. #else
  25. # include <unistd.h>
  26. #endif
  27. #include <errno.h>
  28. #include <math.h> /* for floor() */
  29. #include <stdio.h> /* for FILE et al. */
  30. #include <string.h> /* for strcmp() */
  31. #include "FLAC/all.h"
  32. #include "share/grabbag.h"
  33. #include "share/replaygain_synthesis.h"
  34. #include "decode.h"
  35. #ifdef FLAC__HAS_OGG
  36. #include "OggFLAC/file_decoder.h"
  37. #endif
  38. typedef struct {
  39. #ifdef FLAC__HAS_OGG
  40. FLAC__bool is_ogg;
  41. #endif
  42. FLAC__bool is_aiff_out;
  43. FLAC__bool is_wave_out;
  44. FLAC__bool continue_through_decode_errors;
  45. struct {
  46. replaygain_synthesis_spec_t spec;
  47. FLAC__bool apply; /* 'spec.apply' is just a request; this 'apply' means we actually parsed the RG tags and are ready to go */
  48. double scale;
  49. DitherContext dither_context;
  50. } replaygain;
  51. FLAC__bool test_only;
  52. FLAC__bool analysis_mode;
  53. analysis_options aopts;
  54. utils__SkipUntilSpecification *skip_specification;
  55. utils__SkipUntilSpecification *until_specification; /* a canonicalized value of 0 mean end-of-stream (i.e. --until=-0) */
  56. utils__CueSpecification *cue_specification;
  57. const char *inbasefilename;
  58. const char *outfilename;
  59. FLAC__uint64 samples_processed;
  60. unsigned frame_counter;
  61. FLAC__bool abort_flag;
  62. FLAC__bool aborting_due_to_until; /* true if we intentionally abort decoding prematurely because we hit the --until point */
  63. struct {
  64. FLAC__bool needs_fixup;
  65. unsigned riff_offset; /* or FORM offset for AIFF */
  66. unsigned data_offset; /* or SSND offset for AIFF */
  67. unsigned frames_offset; /* AIFF only */
  68. } wave_chunk_size_fixup;
  69. FLAC__bool is_big_endian;
  70. FLAC__bool is_unsigned_samples;
  71. FLAC__uint64 total_samples;
  72. unsigned bps;
  73. unsigned channels;
  74. unsigned sample_rate;
  75. union {
  76. union {
  77. FLAC__FileDecoder *file;
  78. } flac;
  79. #ifdef FLAC__HAS_OGG
  80. union {
  81. OggFLAC__FileDecoder *file;
  82. } ogg;
  83. #endif
  84. } decoder;
  85. FILE *fout;
  86. } DecoderSession;
  87. static FLAC__bool is_big_endian_host_;
  88. /*
  89.  * local routines
  90.  */
  91. static FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool is_aiff_out, FLAC__bool is_wave_out, FLAC__bool continue_through_decode_errors, 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);
  92. static void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred);
  93. static FLAC__bool DecoderSession_init_decoder(DecoderSession *d, decode_options_t decode_options, const char *infilename);
  94. static FLAC__bool DecoderSession_process(DecoderSession *d);
  95. static int DecoderSession_finish_ok(DecoderSession *d);
  96. static int DecoderSession_finish_error(DecoderSession *d);
  97. static FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, unsigned sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input);
  98. static FLAC__bool write_necessary_headers(DecoderSession *decoder_session);
  99. static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val);
  100. static FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val);
  101. static FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val);
  102. static FLAC__bool write_big_endian_uint32(FILE *f, FLAC__uint32 val);
  103. static FLAC__bool write_sane_extended(FILE *f, unsigned val);
  104. static FLAC__bool fixup_wave_chunk_size(const char *outfilename, FLAC__bool is_wave_out, unsigned riff_offset, unsigned data_offset, unsigned frames_offset, FLAC__uint32 total_samples, unsigned channels, unsigned bps);
  105. /*
  106.  * We use 'void *' so that we can use the same callbacks for the
  107.  * FLAC__StreamDecoder and FLAC__FileDecoder.  The 'decoder' argument is
  108.  * actually never used in the callbacks.
  109.  */
  110. static FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
  111. static void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
  112. static void error_callback(const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
  113. static void print_error_with_state(const DecoderSession *d, const char *message);
  114. static void print_stats(const DecoderSession *decoder_session);
  115. /*
  116.  * public routines
  117.  */
  118. int flac__decode_aiff(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, wav_decode_options_t options)
  119. {
  120. DecoderSession decoder_session;
  121. if(!
  122. DecoderSession_construct(
  123. &decoder_session,
  124. #ifdef FLAC__HAS_OGG
  125. options.common.is_ogg,
  126. #else
  127. /*is_ogg=*/false,
  128. #endif
  129. /*is_aiff_out=*/true,
  130. /*is_wave_out=*/false,
  131. options.common.continue_through_decode_errors,
  132. options.common.replaygain_synthesis_spec,
  133. analysis_mode,
  134. aopts,
  135. &options.common.skip_specification,
  136. &options.common.until_specification,
  137. options.common.has_cue_specification? &options.common.cue_specification : 0,
  138. infilename,
  139. outfilename
  140. )
  141. )
  142. return 1;
  143. if(!DecoderSession_init_decoder(&decoder_session, options.common, infilename))
  144. return DecoderSession_finish_error(&decoder_session);
  145. if(!DecoderSession_process(&decoder_session))
  146. return DecoderSession_finish_error(&decoder_session);
  147. return DecoderSession_finish_ok(&decoder_session);
  148. }
  149. int flac__decode_wav(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, wav_decode_options_t options)
  150. {
  151. DecoderSession decoder_session;
  152. if(!
  153. DecoderSession_construct(
  154. &decoder_session,
  155. #ifdef FLAC__HAS_OGG
  156. options.common.is_ogg,
  157. #else
  158. /*is_ogg=*/false,
  159. #endif
  160. /*is_aiff_out=*/false,
  161. /*is_wave_out=*/true,
  162. options.common.continue_through_decode_errors,
  163. options.common.replaygain_synthesis_spec,
  164. analysis_mode,
  165. aopts,
  166. &options.common.skip_specification,
  167. &options.common.until_specification,
  168. options.common.has_cue_specification? &options.common.cue_specification : 0,
  169. infilename,
  170. outfilename
  171. )
  172. )
  173. return 1;
  174. if(!DecoderSession_init_decoder(&decoder_session, options.common, infilename))
  175. return DecoderSession_finish_error(&decoder_session);
  176. if(!DecoderSession_process(&decoder_session))
  177. return DecoderSession_finish_error(&decoder_session);
  178. return DecoderSession_finish_ok(&decoder_session);
  179. }
  180. int flac__decode_raw(const char *infilename, const char *outfilename, FLAC__bool analysis_mode, analysis_options aopts, raw_decode_options_t options)
  181. {
  182. DecoderSession decoder_session;
  183. decoder_session.is_big_endian = options.is_big_endian;
  184. decoder_session.is_unsigned_samples = options.is_unsigned_samples;
  185. if(!
  186. DecoderSession_construct(
  187. &decoder_session,
  188. #ifdef FLAC__HAS_OGG
  189. options.common.is_ogg,
  190. #else
  191. /*is_ogg=*/false,
  192. #endif
  193. /*is_aiff_out=*/false,
  194. /*is_wave_out=*/false,
  195. options.common.continue_through_decode_errors,
  196. options.common.replaygain_synthesis_spec,
  197. analysis_mode,
  198. aopts,
  199. &options.common.skip_specification,
  200. &options.common.until_specification,
  201. options.common.has_cue_specification? &options.common.cue_specification : 0,
  202. infilename,
  203. outfilename
  204. )
  205. )
  206. return 1;
  207. if(!DecoderSession_init_decoder(&decoder_session, options.common, infilename))
  208. return DecoderSession_finish_error(&decoder_session);
  209. if(!DecoderSession_process(&decoder_session))
  210. return DecoderSession_finish_error(&decoder_session);
  211. return DecoderSession_finish_ok(&decoder_session);
  212. }
  213. FLAC__bool DecoderSession_construct(DecoderSession *d, FLAC__bool is_ogg, FLAC__bool is_aiff_out, FLAC__bool is_wave_out, FLAC__bool continue_through_decode_errors, 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)
  214. {
  215. #ifdef FLAC__HAS_OGG
  216. d->is_ogg = is_ogg;
  217. #else
  218. (void)is_ogg;
  219. #endif
  220. d->is_aiff_out = is_aiff_out;
  221. d->is_wave_out = is_wave_out;
  222. d->continue_through_decode_errors = continue_through_decode_errors;
  223. d->replaygain.spec = replaygain_synthesis_spec;
  224. d->replaygain.apply = false;
  225. d->replaygain.scale = 0.0;
  226. /* d->replaygain.dither_context gets initialized later once we know the sample resolution */
  227. d->test_only = (0 == outfilename);
  228. d->analysis_mode = analysis_mode;
  229. d->aopts = aopts;
  230. d->skip_specification = skip_specification;
  231. d->until_specification = until_specification;
  232. d->cue_specification = cue_specification;
  233. d->inbasefilename = grabbag__file_get_basename(infilename);
  234. d->outfilename = outfilename;
  235. d->samples_processed = 0;
  236. d->frame_counter = 0;
  237. d->abort_flag = false;
  238. d->aborting_due_to_until = false;
  239. d->wave_chunk_size_fixup.needs_fixup = false;
  240. d->decoder.flac.file = 0;
  241. #ifdef FLAC__HAS_OGG
  242. d->decoder.ogg.file = 0;
  243. #endif
  244. d->fout = 0; /* initialized with an open file later if necessary */
  245. FLAC__ASSERT(!(d->test_only && d->analysis_mode));
  246. if(!d->test_only) {
  247. if(0 == strcmp(outfilename, "-")) {
  248. d->fout = grabbag__file_get_binary_stdout();
  249. }
  250. else {
  251. if(0 == (d->fout = fopen(outfilename, "wb"))) {
  252. flac__utils_printf(stderr, 1, "%s: ERROR: can't open output file %sn", d->inbasefilename, outfilename);
  253. DecoderSession_destroy(d, /*error_occurred=*/true);
  254. return false;
  255. }
  256. }
  257. }
  258. if(analysis_mode)
  259. flac__analyze_init(aopts);
  260. return true;
  261. }
  262. void DecoderSession_destroy(DecoderSession *d, FLAC__bool error_occurred)
  263. {
  264. if(0 != d->fout && d->fout != stdout) {
  265. fclose(d->fout);
  266. if(error_occurred)
  267. unlink(d->outfilename);
  268. }
  269. }
  270. FLAC__bool DecoderSession_init_decoder(DecoderSession *decoder_session, decode_options_t decode_options, const char *infilename)
  271. {
  272. FLAC__uint32 test = 1;
  273. is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
  274. #ifdef FLAC__HAS_OGG
  275. if(decoder_session->is_ogg) {
  276. decoder_session->decoder.ogg.file = OggFLAC__file_decoder_new();
  277. if(0 == decoder_session->decoder.ogg.file) {
  278. flac__utils_printf(stderr, 1, "%s: ERROR creating the decoder instancen", decoder_session->inbasefilename);
  279. return false;
  280. }
  281. OggFLAC__file_decoder_set_md5_checking(decoder_session->decoder.ogg.file, true);
  282. OggFLAC__file_decoder_set_filename(decoder_session->decoder.ogg.file, infilename);
  283. if(!decode_options.use_first_serial_number)
  284. OggFLAC__file_decoder_set_serial_number(decoder_session->decoder.ogg.file, decode_options.serial_number);
  285. if (0 != decoder_session->cue_specification)
  286. OggFLAC__file_decoder_set_metadata_respond(decoder_session->decoder.ogg.file, FLAC__METADATA_TYPE_CUESHEET);
  287. if (decoder_session->replaygain.spec.apply)
  288. OggFLAC__file_decoder_set_metadata_respond(decoder_session->decoder.ogg.file, FLAC__METADATA_TYPE_VORBIS_COMMENT);
  289. /*
  290.  * The three ugly casts here are to 'downcast' the 'void *' argument of
  291.  * the callback down to 'OggFLAC__FileDecoder *'.  In C++ this would be
  292.  * unnecessary but here the cast makes the C compiler happy.
  293.  */
  294. OggFLAC__file_decoder_set_write_callback(decoder_session->decoder.ogg.file, (FLAC__StreamDecoderWriteStatus (*)(const OggFLAC__FileDecoder *, const FLAC__Frame *, const FLAC__int32 * const [], void *))write_callback);
  295. OggFLAC__file_decoder_set_metadata_callback(decoder_session->decoder.ogg.file, (void (*)(const OggFLAC__FileDecoder *, const FLAC__StreamMetadata *, void *))metadata_callback);
  296. OggFLAC__file_decoder_set_error_callback(decoder_session->decoder.ogg.file, (void (*)(const OggFLAC__FileDecoder *, FLAC__StreamDecoderErrorStatus, void *))error_callback);
  297. OggFLAC__file_decoder_set_client_data(decoder_session->decoder.ogg.file, decoder_session);
  298. if(OggFLAC__file_decoder_init(decoder_session->decoder.ogg.file) != OggFLAC__FILE_DECODER_OK) {
  299. print_error_with_state(decoder_session, "ERROR initializing decoder");
  300. return false;
  301. }
  302. }
  303. else
  304. #else
  305. (void)decode_options;
  306. #endif
  307. {
  308. decoder_session->decoder.flac.file = FLAC__file_decoder_new();
  309. if(0 == decoder_session->decoder.flac.file) {
  310. flac__utils_printf(stderr, 1, "%s: ERROR creating the decoder instancen", decoder_session->inbasefilename);
  311. return false;
  312. }
  313. FLAC__file_decoder_set_md5_checking(decoder_session->decoder.flac.file, true);
  314. FLAC__file_decoder_set_filename(decoder_session->decoder.flac.file, infilename);
  315. if (0 != decoder_session->cue_specification)
  316. FLAC__file_decoder_set_metadata_respond(decoder_session->decoder.flac.file, FLAC__METADATA_TYPE_CUESHEET);
  317. if (decoder_session->replaygain.spec.apply)
  318. FLAC__file_decoder_set_metadata_respond(decoder_session->decoder.flac.file, FLAC__METADATA_TYPE_VORBIS_COMMENT);
  319. /*
  320.  * The three ugly casts here are to 'downcast' the 'void *' argument of
  321.  * the callback down to 'FLAC__FileDecoder *'.
  322.  */
  323. FLAC__file_decoder_set_write_callback(decoder_session->decoder.flac.file, (FLAC__StreamDecoderWriteStatus (*)(const FLAC__FileDecoder *, const FLAC__Frame *, const FLAC__int32 * const [], void *))write_callback);
  324. FLAC__file_decoder_set_metadata_callback(decoder_session->decoder.flac.file, (void (*)(const FLAC__FileDecoder *, const FLAC__StreamMetadata *, void *))metadata_callback);
  325. FLAC__file_decoder_set_error_callback(decoder_session->decoder.flac.file, (void (*)(const FLAC__FileDecoder *, FLAC__StreamDecoderErrorStatus, void *))error_callback);
  326. FLAC__file_decoder_set_client_data(decoder_session->decoder.flac.file, decoder_session);
  327. if(FLAC__file_decoder_init(decoder_session->decoder.flac.file) != FLAC__FILE_DECODER_OK) {
  328. print_error_with_state(decoder_session, "ERROR initializing decoder");
  329. return false;
  330. }
  331. }
  332. return true;
  333. }
  334. FLAC__bool DecoderSession_process(DecoderSession *d)
  335. {
  336. #ifdef FLAC__HAS_OGG
  337. if(d->is_ogg) {
  338. if(!OggFLAC__file_decoder_process_until_end_of_metadata(d->decoder.ogg.file)) {
  339. flac__utils_printf(stderr, 2, "n");
  340. print_error_with_state(d, "ERROR while decoding metadata");
  341. return false;
  342. }
  343. if(OggFLAC__file_decoder_get_state(d->decoder.ogg.file) != OggFLAC__FILE_DECODER_OK && OggFLAC__file_decoder_get_state(d->decoder.ogg.file) != OggFLAC__FILE_DECODER_END_OF_FILE) {
  344. flac__utils_printf(stderr, 2, "n");
  345. print_error_with_state(d, "ERROR during metadata decoding");
  346. return false;
  347. }
  348. }
  349. else
  350. #endif
  351. {
  352. if(!FLAC__file_decoder_process_until_end_of_metadata(d->decoder.flac.file)) {
  353. flac__utils_printf(stderr, 2, "n");
  354. print_error_with_state(d, "ERROR while decoding metadata");
  355. return false;
  356. }
  357. if(FLAC__file_decoder_get_state(d->decoder.flac.file) != FLAC__FILE_DECODER_OK && FLAC__file_decoder_get_state(d->decoder.flac.file) != FLAC__FILE_DECODER_END_OF_FILE) {
  358. flac__utils_printf(stderr, 2, "n");
  359. print_error_with_state(d, "ERROR during metadata decoding");
  360. return false;
  361. }
  362. }
  363. if(d->abort_flag)
  364. return false;
  365. /* write the WAVE/AIFF headers if necessary */
  366. if(!write_necessary_headers(d)) {
  367. d->abort_flag = true;
  368. return false;
  369. }
  370. if(d->skip_specification->value.samples > 0) {
  371. const FLAC__uint64 skip = (FLAC__uint64)d->skip_specification->value.samples;
  372. #ifdef FLAC__HAS_OGG
  373. if(d->is_ogg) {
  374. if(!OggFLAC__file_decoder_seek_absolute(d->decoder.ogg.file, skip)) {
  375. print_error_with_state(d, "ERROR seeking while skipping bytes");
  376. return false;
  377. }
  378. if(!OggFLAC__file_decoder_process_until_end_of_file(d->decoder.ogg.file) && !d->aborting_due_to_until) {
  379. flac__utils_printf(stderr, 2, "n");
  380. print_error_with_state(d, "ERROR while decoding frames");
  381. return false;
  382. }
  383. if(OggFLAC__file_decoder_get_state(d->decoder.ogg.file) != OggFLAC__FILE_DECODER_OK && OggFLAC__file_decoder_get_state(d->decoder.ogg.file) != OggFLAC__FILE_DECODER_END_OF_FILE && !d->aborting_due_to_until) {
  384. flac__utils_printf(stderr, 2, "n");
  385. print_error_with_state(d, "ERROR during decoding");
  386. return false;
  387. }
  388. }
  389. else
  390. #endif
  391. {
  392. if(!FLAC__file_decoder_seek_absolute(d->decoder.flac.file, skip)) {
  393. print_error_with_state(d, "ERROR seeking while skipping bytes");
  394. return false;
  395. }
  396. if(!FLAC__file_decoder_process_until_end_of_file(d->decoder.flac.file) && !d->aborting_due_to_until) {
  397. flac__utils_printf(stderr, 2, "n");
  398. print_error_with_state(d, "ERROR while decoding frames");
  399. return false;
  400. }
  401. if(FLAC__file_decoder_get_state(d->decoder.flac.file) != FLAC__FILE_DECODER_OK && FLAC__file_decoder_get_state(d->decoder.flac.file) != FLAC__FILE_DECODER_END_OF_FILE && !d->aborting_due_to_until) {
  402. flac__utils_printf(stderr, 2, "n");
  403. print_error_with_state(d, "ERROR during decoding");
  404. return false;
  405. }
  406. }
  407. }
  408. else {
  409. #ifdef FLAC__HAS_OGG
  410. if(d->is_ogg) {
  411. if(!OggFLAC__file_decoder_process_until_end_of_file(d->decoder.ogg.file) && !d->aborting_due_to_until) {
  412. flac__utils_printf(stderr, 2, "n");
  413. print_error_with_state(d, "ERROR while decoding data");
  414. return false;
  415. }
  416. if(OggFLAC__file_decoder_get_state(d->decoder.ogg.file) != OggFLAC__FILE_DECODER_OK && OggFLAC__file_decoder_get_state(d->decoder.ogg.file) != OggFLAC__FILE_DECODER_END_OF_FILE && !d->aborting_due_to_until) {
  417. flac__utils_printf(stderr, 2, "n");
  418. print_error_with_state(d, "ERROR during decoding");
  419. return false;
  420. }
  421. }
  422. else
  423. #endif
  424. {
  425. if(!FLAC__file_decoder_process_until_end_of_file(d->decoder.flac.file) && !d->aborting_due_to_until) {
  426. flac__utils_printf(stderr, 2, "n");
  427. print_error_with_state(d, "ERROR while decoding data");
  428. return false;
  429. }
  430. if(FLAC__file_decoder_get_state(d->decoder.flac.file) != FLAC__FILE_DECODER_OK && FLAC__file_decoder_get_state(d->decoder.flac.file) != FLAC__FILE_DECODER_END_OF_FILE && !d->aborting_due_to_until) {
  431. flac__utils_printf(stderr, 2, "n");
  432. print_error_with_state(d, "ERROR during decoding");
  433. return false;
  434. }
  435. }
  436. }
  437. if((d->is_wave_out || d->is_aiff_out) && ((d->total_samples * d->channels * ((d->bps+7)/8)) & 1)) {
  438. if(flac__utils_fwrite("00", 1, 1, d->fout) != 1) {
  439. print_error_with_state(d, d->is_wave_out?
  440. "ERROR writing pad byte to WAVE data chunk" :
  441. "ERROR writing pad byte to AIFF SSND chunk"
  442. );
  443. return false;
  444. }
  445. }
  446. return true;
  447. }
  448. int DecoderSession_finish_ok(DecoderSession *d)
  449. {
  450. FLAC__bool md5_failure = false;
  451. #ifdef FLAC__HAS_OGG
  452. if(d->is_ogg) {
  453. if(d->decoder.ogg.file) {
  454. md5_failure = !OggFLAC__file_decoder_finish(d->decoder.ogg.file) && !d->aborting_due_to_until;
  455. print_stats(d);
  456. OggFLAC__file_decoder_delete(d->decoder.ogg.file);
  457. }
  458. }
  459. else
  460. #endif
  461. {
  462. if(d->decoder.flac.file) {
  463. md5_failure = !FLAC__file_decoder_finish(d->decoder.flac.file) && !d->aborting_due_to_until;
  464. print_stats(d);
  465. FLAC__file_decoder_delete(d->decoder.flac.file);
  466. }
  467. }
  468. if(d->analysis_mode)
  469. flac__analyze_finish(d->aopts);
  470. if(md5_failure) {
  471. flac__utils_printf(stderr, 1, "r%s: WARNING, MD5 signature mismatchn", d->inbasefilename);
  472. }
  473. else {
  474. flac__utils_printf(stderr, 2, "r%s: %s         n", d->inbasefilename, d->test_only? "ok           ":d->analysis_mode?"done           ":"done");
  475. }
  476. DecoderSession_destroy(d, /*error_occurred=*/false);
  477. if((d->is_wave_out || d->is_aiff_out) && d->wave_chunk_size_fixup.needs_fixup)
  478. if(!fixup_wave_chunk_size(d->outfilename, d->is_wave_out, d->wave_chunk_size_fixup.riff_offset, d->wave_chunk_size_fixup.data_offset, d->wave_chunk_size_fixup.frames_offset, (FLAC__uint32)d->samples_processed, d->channels, d->bps))
  479. return 1;
  480. return 0;
  481. }
  482. int DecoderSession_finish_error(DecoderSession *d)
  483. {
  484. #ifdef FLAC__HAS_OGG
  485. if(d->is_ogg) {
  486. if(d->decoder.ogg.file) {
  487. OggFLAC__file_decoder_finish(d->decoder.ogg.file);
  488. OggFLAC__file_decoder_delete(d->decoder.ogg.file);
  489. }
  490. }
  491. else
  492. #endif
  493. {
  494. if(d->decoder.flac.file) {
  495. FLAC__file_decoder_finish(d->decoder.flac.file);
  496. FLAC__file_decoder_delete(d->decoder.flac.file);
  497. }
  498. }
  499. if(d->analysis_mode)
  500. flac__analyze_finish(d->aopts);
  501. DecoderSession_destroy(d, /*error_occurred=*/true);
  502. return 1;
  503. }
  504. FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, unsigned sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input)
  505. {
  506. /* convert from mm:ss.sss to sample number if necessary */
  507. flac__utils_canonicalize_skip_until_specification(spec, sample_rate);
  508. /* special case: if "--until=-0", use the special value '0' to mean "end-of-stream" */
  509. if(spec->is_relative && spec->value.samples == 0) {
  510. spec->is_relative = false;
  511. return true;
  512. }
  513. /* in any other case the total samples in the input must be known */
  514. if(total_samples_in_input == 0) {
  515. flac__utils_printf(stderr, 1, "%s: ERROR, cannot use --until when FLAC metadata has total sample count of 0n", inbasefilename);
  516. return false;
  517. }
  518. FLAC__ASSERT(spec->value_is_samples);
  519. /* convert relative specifications to absolute */
  520. if(spec->is_relative) {
  521. if(spec->value.samples <= 0)
  522. spec->value.samples += (FLAC__int64)total_samples_in_input;
  523. else
  524. spec->value.samples += skip;
  525. spec->is_relative = false;
  526. }
  527. /* error check */
  528. if(spec->value.samples < 0) {
  529. flac__utils_printf(stderr, 1, "%s: ERROR, --until value is before beginning of inputn", inbasefilename);
  530. return false;
  531. }
  532. if((FLAC__uint64)spec->value.samples <= skip) {
  533. flac__utils_printf(stderr, 1, "%s: ERROR, --until value is before --skip pointn", inbasefilename);
  534. return false;
  535. }
  536. if((FLAC__uint64)spec->value.samples > total_samples_in_input) {
  537. flac__utils_printf(stderr, 1, "%s: ERROR, --until value is after end of inputn", inbasefilename);
  538. return false;
  539. }
  540. return true;
  541. }
  542. FLAC__bool write_necessary_headers(DecoderSession *decoder_session)
  543. {
  544. /* write the WAVE/AIFF headers if necessary */
  545. if(!decoder_session->analysis_mode && !decoder_session->test_only && (decoder_session->is_wave_out || decoder_session->is_aiff_out)) {
  546. const char *fmt_desc = decoder_session->is_wave_out? "WAVE" : "AIFF";
  547. FLAC__uint64 data_size = decoder_session->total_samples * decoder_session->channels * ((decoder_session->bps+7)/8);
  548. const FLAC__uint32 aligned_data_size = (FLAC__uint32)((data_size+1) & (~1U)); /* we'll check for overflow later */
  549. if(decoder_session->total_samples == 0) {
  550. if(decoder_session->fout == stdout) {
  551. flac__utils_printf(stderr, 1, "%s: WARNING, don't have accurate sample count available for %s header.n", decoder_session->inbasefilename, fmt_desc);
  552. flac__utils_printf(stderr, 1, "             Generated %s file will have a data chunk size of 0.  Tryn", fmt_desc);
  553. flac__utils_printf(stderr, 1, "             decoding directly to a file instead.n");
  554. }
  555. else {
  556. decoder_session->wave_chunk_size_fixup.needs_fixup = true;
  557. }
  558. }
  559. if(data_size >= 0xFFFFFFDC) {
  560. flac__utils_printf(stderr, 1, "%s: ERROR: stream is too big to fit in a single %s file chunkn", decoder_session->inbasefilename, fmt_desc);
  561. return false;
  562. }
  563. if(decoder_session->is_wave_out) {
  564. if(flac__utils_fwrite("RIFF", 1, 4, decoder_session->fout) != 4)
  565. return false;
  566. if(decoder_session->wave_chunk_size_fixup.needs_fixup)
  567. decoder_session->wave_chunk_size_fixup.riff_offset = ftell(decoder_session->fout);
  568. if(!write_little_endian_uint32(decoder_session->fout, aligned_data_size+36)) /* filesize-8 */
  569. return false;
  570. if(flac__utils_fwrite("WAVEfmt ", 1, 8, decoder_session->fout) != 8)
  571. return false;
  572. if(flac__utils_fwrite("20000000", 1, 4, decoder_session->fout) != 4) /* chunk size = 16 */
  573. return false;
  574. if(flac__utils_fwrite("0100", 1, 2, decoder_session->fout) != 2) /* compression code == 1 */
  575. return false;
  576. if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->channels)))
  577. return false;
  578. if(!write_little_endian_uint32(decoder_session->fout, decoder_session->sample_rate))
  579. return false;
  580. if(!write_little_endian_uint32(decoder_session->fout, decoder_session->sample_rate * decoder_session->channels * ((decoder_session->bps+7) / 8))) /* @@@ or is it (sample_rate*channels*bps) / 8 ??? */
  581. return false;
  582. if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->channels * ((decoder_session->bps+7) / 8)))) /* block align */
  583. return false;
  584. if(!write_little_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->bps))) /* bits per sample */
  585. return false;
  586. if(flac__utils_fwrite("data", 1, 4, decoder_session->fout) != 4)
  587. return false;
  588. if(decoder_session->wave_chunk_size_fixup.needs_fixup)
  589. decoder_session->wave_chunk_size_fixup.data_offset = ftell(decoder_session->fout);
  590. if(!write_little_endian_uint32(decoder_session->fout, (FLAC__uint32)data_size)) /* data size */
  591. return false;
  592. }
  593. else {
  594. if(flac__utils_fwrite("FORM", 1, 4, decoder_session->fout) != 4)
  595. return false;
  596. if(decoder_session->wave_chunk_size_fixup.needs_fixup)
  597. decoder_session->wave_chunk_size_fixup.riff_offset = ftell(decoder_session->fout);
  598. if(!write_big_endian_uint32(decoder_session->fout, aligned_data_size+46)) /* filesize-8 */
  599. return false;
  600. if(flac__utils_fwrite("AIFFCOMM", 1, 8, decoder_session->fout) != 8)
  601. return false;
  602. if(flac__utils_fwrite("00000022", 1, 4, decoder_session->fout) != 4) /* chunk size = 18 */
  603. return false;
  604. if(!write_big_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->channels)))
  605. return false;
  606. if(decoder_session->wave_chunk_size_fixup.needs_fixup)
  607. decoder_session->wave_chunk_size_fixup.frames_offset = ftell(decoder_session->fout);
  608. if(!write_big_endian_uint32(decoder_session->fout, (FLAC__uint32)decoder_session->total_samples))
  609. return false;
  610. if(!write_big_endian_uint16(decoder_session->fout, (FLAC__uint16)(decoder_session->bps)))
  611. return false;
  612. if(!write_sane_extended(decoder_session->fout, decoder_session->sample_rate))
  613. return false;
  614. if(flac__utils_fwrite("SSND", 1, 4, decoder_session->fout) != 4)
  615. return false;
  616. if(decoder_session->wave_chunk_size_fixup.needs_fixup)
  617. decoder_session->wave_chunk_size_fixup.data_offset = ftell(decoder_session->fout);
  618. if(!write_big_endian_uint32(decoder_session->fout, (FLAC__uint32)data_size+8)) /* data size */
  619. return false;
  620. if(!write_big_endian_uint32(decoder_session->fout, 0/*offset*/))
  621. return false;
  622. if(!write_big_endian_uint32(decoder_session->fout, 0/*block_size*/))
  623. return false;
  624. }
  625. }
  626. return true;
  627. }
  628. FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 val)
  629. {
  630. FLAC__byte *b = (FLAC__byte*)(&val);
  631. if(is_big_endian_host_) {
  632. FLAC__byte tmp;
  633. tmp = b[1]; b[1] = b[0]; b[0] = tmp;
  634. }
  635. return flac__utils_fwrite(b, 1, 2, f) == 2;
  636. }
  637. FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 val)
  638. {
  639. FLAC__byte *b = (FLAC__byte*)(&val);
  640. if(is_big_endian_host_) {
  641. FLAC__byte tmp;
  642. tmp = b[3]; b[3] = b[0]; b[0] = tmp;
  643. tmp = b[2]; b[2] = b[1]; b[1] = tmp;
  644. }
  645. return flac__utils_fwrite(b, 1, 4, f) == 4;
  646. }
  647. FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val)
  648. {
  649. FLAC__byte *b = (FLAC__byte*)(&val);
  650. if(!is_big_endian_host_) {
  651. FLAC__byte tmp;
  652. tmp = b[1]; b[1] = b[0]; b[0] = tmp;
  653. }
  654. return flac__utils_fwrite(b, 1, 2, f) == 2;
  655. }
  656. FLAC__bool write_big_endian_uint32(FILE *f, FLAC__uint32 val)
  657. {
  658. FLAC__byte *b = (FLAC__byte*)(&val);
  659. if(!is_big_endian_host_) {
  660. FLAC__byte tmp;
  661. tmp = b[3]; b[3] = b[0]; b[0] = tmp;
  662. tmp = b[2]; b[2] = b[1]; b[1] = tmp;
  663. }
  664. return flac__utils_fwrite(b, 1, 4, f) == 4;
  665. }
  666. FLAC__bool write_sane_extended(FILE *f, unsigned val)
  667. /* Write to 'f' a SANE extended representation of 'val'.  Return false if
  668. * the write succeeds; return true otherwise.
  669. *
  670. * SANE extended is an 80-bit IEEE-754 representation with sign bit, 15 bits
  671. * of exponent, and 64 bits of significand (mantissa).  Unlike most IEEE-754
  672. * representations, it does not imply a 1 above the MSB of the significand.
  673. *
  674. * Preconditions:
  675. *  val!=0U
  676. */
  677. {
  678. unsigned int shift, exponent;
  679. FLAC__ASSERT(val!=0U); /* handling 0 would require a special case */
  680. for(shift= 0U; (val>>(31-shift))==0U; ++shift)
  681. ;
  682. val<<= shift;
  683. exponent= 63U-(shift+32U); /* add 32 for unused second word */
  684. if(!write_big_endian_uint16(f, (FLAC__uint16)(exponent+0x3FFF)))
  685. return false;
  686. if(!write_big_endian_uint32(f, val))
  687. return false;
  688. if(!write_big_endian_uint32(f, 0)) /* unused second word */
  689. return false;
  690. return true;
  691. }
  692. FLAC__bool fixup_wave_chunk_size(const char *outfilename, FLAC__bool is_wave_out, unsigned riff_offset, unsigned data_offset, unsigned frames_offset, FLAC__uint32 total_samples, unsigned channels, unsigned bps)
  693. {
  694. const char *fmt_desc = (is_wave_out? "WAVE" : "AIFF");
  695. FLAC__bool (*write_it)(FILE *, FLAC__uint32) = (is_wave_out? write_little_endian_uint32 : write_big_endian_uint32);
  696. FILE *f = fopen(outfilename, "r+b");
  697. FLAC__uint32 data_size, aligned_data_size;
  698. if(0 == f) {
  699. flac__utils_printf(stderr, 1, "ERROR, couldn't open file %s while fixing up %s chunk sizen", outfilename, fmt_desc);
  700. return false;
  701. }
  702. data_size = aligned_data_size = total_samples * channels * ((bps+7)/8);
  703. if(aligned_data_size & 1)
  704. aligned_data_size++;
  705. if(fseek(f, riff_offset, SEEK_SET) < 0) {
  706. flac__utils_printf(stderr, 1, "ERROR, couldn't seek in file %s while fixing up %s chunk sizen", outfilename, fmt_desc);
  707. fclose(f);
  708. return false;
  709. }
  710. if(!write_it(f, aligned_data_size + (is_wave_out? 36 : 46))) {
  711. flac__utils_printf(stderr, 1, "ERROR, couldn't write size in file %s while fixing up %s chunk sizen", outfilename, fmt_desc);
  712. fclose(f);
  713. return false;
  714. }
  715. if(!is_wave_out) {
  716. if(fseek(f, frames_offset, SEEK_SET) < 0) {
  717. flac__utils_printf(stderr, 1, "ERROR, couldn't seek in file %s while fixing up %s chunk sizen", outfilename, fmt_desc);
  718. fclose(f);
  719. return false;
  720. }
  721. if(!write_it(f, total_samples)) {
  722. flac__utils_printf(stderr, 1, "ERROR, couldn't write size in file %s while fixing up %s chunk sizen", outfilename, fmt_desc);
  723. fclose(f);
  724. return false;
  725. }
  726. }
  727. if(fseek(f, data_offset, SEEK_SET) < 0) {
  728. flac__utils_printf(stderr, 1, "ERROR, couldn't seek in file %s while fixing up %s chunk sizen", outfilename, fmt_desc);
  729. fclose(f);
  730. return false;
  731. }
  732. if(!write_it(f, data_size + (is_wave_out? 0 : 8))) {
  733. flac__utils_printf(stderr, 1, "ERROR, couldn't write size in file %s while fixing up %s chunk sizen", outfilename, fmt_desc);
  734. fclose(f);
  735. return false;
  736. }
  737. fclose(f);
  738. return true;
  739. }
  740. FLAC__StreamDecoderWriteStatus write_callback(const void *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
  741. {
  742. DecoderSession *decoder_session = (DecoderSession*)client_data;
  743. FILE *fout = decoder_session->fout;
  744. const unsigned bps = frame->header.bits_per_sample, channels = frame->header.channels;
  745. FLAC__bool is_big_endian = (decoder_session->is_aiff_out? true : (decoder_session->is_wave_out? false : decoder_session->is_big_endian));
  746. FLAC__bool is_unsigned_samples = (decoder_session->is_aiff_out? false : (decoder_session->is_wave_out? bps<=8 : decoder_session->is_unsigned_samples));
  747. unsigned wide_samples = frame->header.blocksize, wide_sample, sample, channel, byte;
  748. static FLAC__int8 s8buffer[FLAC__MAX_BLOCK_SIZE * FLAC__MAX_CHANNELS * sizeof(FLAC__int32)]; /* WATCHOUT: can be up to 2 megs */
  749. FLAC__uint8  *u8buffer  = (FLAC__uint8  *)s8buffer;
  750. FLAC__int16  *s16buffer = (FLAC__int16  *)s8buffer;
  751. FLAC__uint16 *u16buffer = (FLAC__uint16 *)s8buffer;
  752. FLAC__int32  *s32buffer = (FLAC__int32  *)s8buffer;
  753. FLAC__uint32 *u32buffer = (FLAC__uint32 *)s8buffer;
  754. size_t bytes_to_write = 0;
  755. (void)decoder;
  756. if(decoder_session->abort_flag)
  757. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  758. if(bps != decoder_session->bps) {
  759. flac__utils_printf(stderr, 1, "%s: ERROR, bits-per-sample is %u in frame but %u in STREAMINFOn", decoder_session->inbasefilename, bps, decoder_session->bps);
  760. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  761. }
  762. if(channels != decoder_session->channels) {
  763. flac__utils_printf(stderr, 1, "%s: ERROR, channels is %u in frame but %u in STREAMINFOn", decoder_session->inbasefilename, channels, decoder_session->channels);
  764. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  765. }
  766. if(frame->header.sample_rate != decoder_session->sample_rate) {
  767. flac__utils_printf(stderr, 1, "%s: ERROR, sample rate is %u in frame but %u in STREAMINFOn", decoder_session->inbasefilename, frame->header.sample_rate, decoder_session->sample_rate);
  768. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  769. }
  770. /*
  771.  * limit the number of samples to accept based on --until
  772.  */
  773. FLAC__ASSERT(!decoder_session->skip_specification->is_relative);
  774. FLAC__ASSERT(decoder_session->skip_specification->value.samples >= 0);
  775. FLAC__ASSERT(!decoder_session->until_specification->is_relative);
  776. FLAC__ASSERT(decoder_session->until_specification->value.samples >= 0);
  777. if(decoder_session->until_specification->value.samples > 0) {
  778. const FLAC__uint64 skip = (FLAC__uint64)decoder_session->skip_specification->value.samples;
  779. const FLAC__uint64 until = (FLAC__uint64)decoder_session->until_specification->value.samples;
  780. const FLAC__uint64 input_samples_passed = skip + decoder_session->samples_processed;
  781. FLAC__ASSERT(until >= input_samples_passed);
  782. if(input_samples_passed + wide_samples > until)
  783. wide_samples = (unsigned)(until - input_samples_passed);
  784. if (wide_samples == 0) {
  785. decoder_session->abort_flag = true;
  786. decoder_session->aborting_due_to_until = true;
  787. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  788. }
  789. }
  790. if(wide_samples > 0) {
  791. decoder_session->samples_processed += wide_samples;
  792. decoder_session->frame_counter++;
  793. if(!(decoder_session->frame_counter & 0x3f))
  794. print_stats(decoder_session);
  795. if(decoder_session->analysis_mode) {
  796. flac__analyze_frame(frame, decoder_session->frame_counter-1, decoder_session->aopts, fout);
  797. }
  798. else if(!decoder_session->test_only) {
  799. if (decoder_session->replaygain.apply) {
  800. bytes_to_write = FLAC__replaygain_synthesis__apply_gain(
  801. u8buffer,
  802. !is_big_endian,
  803. is_unsigned_samples,
  804. buffer,
  805. wide_samples,
  806. channels,
  807. bps, /* source_bps */
  808. bps, /* target_bps */
  809. decoder_session->replaygain.scale,
  810. decoder_session->replaygain.spec.limiter == RGSS_LIMIT__HARD, /* hard_limit */
  811. decoder_session->replaygain.spec.noise_shaping != NOISE_SHAPING_NONE, /* do_dithering */
  812. &decoder_session->replaygain.dither_context
  813. );
  814. }
  815. else if(bps == 8) {
  816. if(is_unsigned_samples) {
  817. for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
  818. for(channel = 0; channel < channels; channel++, sample++)
  819. u8buffer[sample] = (FLAC__uint8)(buffer[channel][wide_sample] + 0x80);
  820. }
  821. else {
  822. for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
  823. for(channel = 0; channel < channels; channel++, sample++)
  824. s8buffer[sample] = (FLAC__int8)(buffer[channel][wide_sample]);
  825. }
  826. bytes_to_write = sample;
  827. }
  828. else if(bps == 16) {
  829. if(is_unsigned_samples) {
  830. for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
  831. for(channel = 0; channel < channels; channel++, sample++)
  832. u16buffer[sample] = (FLAC__uint16)(buffer[channel][wide_sample] + 0x8000);
  833. }
  834. else {
  835. for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
  836. for(channel = 0; channel < channels; channel++, sample++)
  837. s16buffer[sample] = (FLAC__int16)(buffer[channel][wide_sample]);
  838. }
  839. if(is_big_endian != is_big_endian_host_) {
  840. unsigned char tmp;
  841. const unsigned bytes = sample * 2;
  842. for(byte = 0; byte < bytes; byte += 2) {
  843. tmp = u8buffer[byte];
  844. u8buffer[byte] = u8buffer[byte+1];
  845. u8buffer[byte+1] = tmp;
  846. }
  847. }
  848. bytes_to_write = 2 * sample;
  849. }
  850. else if(bps == 24) {
  851. if(is_unsigned_samples) {
  852. for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
  853. for(channel = 0; channel < channels; channel++, sample++)
  854. u32buffer[sample] = buffer[channel][wide_sample] + 0x800000;
  855. }
  856. else {
  857. for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
  858. for(channel = 0; channel < channels; channel++, sample++)
  859. s32buffer[sample] = buffer[channel][wide_sample];
  860. }
  861. if(is_big_endian != is_big_endian_host_) {
  862. unsigned char tmp;
  863. const unsigned bytes = sample * 4;
  864. for(byte = 0; byte < bytes; byte += 4) {
  865. tmp = u8buffer[byte];
  866. u8buffer[byte] = u8buffer[byte+3];
  867. u8buffer[byte+3] = tmp;
  868. tmp = u8buffer[byte+1];
  869. u8buffer[byte+1] = u8buffer[byte+2];
  870. u8buffer[byte+2] = tmp;
  871. }
  872. }
  873. if(is_big_endian) {
  874. unsigned lbyte;
  875. const unsigned bytes = sample * 4;
  876. for(lbyte = byte = 0; byte < bytes; ) {
  877. byte++;
  878. u8buffer[lbyte++] = u8buffer[byte++];
  879. u8buffer[lbyte++] = u8buffer[byte++];
  880. u8buffer[lbyte++] = u8buffer[byte++];
  881. }
  882. }
  883. else {
  884. unsigned lbyte;
  885. const unsigned bytes = sample * 4;
  886. for(lbyte = byte = 0; byte < bytes; ) {
  887. u8buffer[lbyte++] = u8buffer[byte++];
  888. u8buffer[lbyte++] = u8buffer[byte++];
  889. u8buffer[lbyte++] = u8buffer[byte++];
  890. byte++;
  891. }
  892. }
  893. bytes_to_write = 3 * sample;
  894. }
  895. else {
  896. FLAC__ASSERT(0);
  897. }
  898. }
  899. }
  900. if(bytes_to_write > 0) {
  901. if(flac__utils_fwrite(u8buffer, 1, bytes_to_write, fout) != bytes_to_write) {
  902. /* if a pipe closed when writing to stdout, we let it go without an error message */
  903. if(errno == EPIPE && decoder_session->fout == stdout)
  904. decoder_session->aborting_due_to_until = true;
  905. decoder_session->abort_flag = true;
  906. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  907. }
  908. }
  909. return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  910. }
  911. void metadata_callback(const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
  912. {
  913. DecoderSession *decoder_session = (DecoderSession*)client_data;
  914. (void)decoder;
  915. if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
  916. FLAC__uint64 skip, until;
  917. decoder_session->bps = metadata->data.stream_info.bits_per_sample;
  918. decoder_session->channels = metadata->data.stream_info.channels;
  919. decoder_session->sample_rate = metadata->data.stream_info.sample_rate;
  920. flac__utils_canonicalize_skip_until_specification(decoder_session->skip_specification, decoder_session->sample_rate);
  921. FLAC__ASSERT(decoder_session->skip_specification->value.samples >= 0);
  922. skip = (FLAC__uint64)decoder_session->skip_specification->value.samples;
  923. /* remember, metadata->data.stream_info.total_samples can be 0, meaning 'unknown' */
  924. if(metadata->data.stream_info.total_samples > 0 && skip >= metadata->data.stream_info.total_samples) {
  925. flac__utils_printf(stderr, 1, "%s: ERROR trying to --skip more samples than in streamn", decoder_session->inbasefilename);
  926. decoder_session->abort_flag = true;
  927. return;
  928. }
  929. else if(metadata->data.stream_info.total_samples == 0 && skip > 0) {
  930. flac__utils_printf(stderr, 1, "%s: ERROR, can't --skip when FLAC metadata has total sample count of 0n", decoder_session->inbasefilename);
  931. decoder_session->abort_flag = true;
  932. return;
  933. }
  934. FLAC__ASSERT(skip == 0 || 0 == decoder_session->cue_specification);
  935. decoder_session->total_samples = metadata->data.stream_info.total_samples - skip;
  936. /* note that we use metadata->data.stream_info.total_samples instead of decoder_session->total_samples */
  937. if(!canonicalize_until_specification(decoder_session->until_specification, decoder_session->inbasefilename, decoder_session->sample_rate, skip, metadata->data.stream_info.total_samples)) {
  938. decoder_session->abort_flag = true;
  939. return;
  940. }
  941. FLAC__ASSERT(decoder_session->until_specification->value.samples >= 0);
  942. until = (FLAC__uint64)decoder_session->until_specification->value.samples;
  943. if(until > 0) {
  944. FLAC__ASSERT(decoder_session->total_samples != 0);
  945. FLAC__ASSERT(0 == decoder_session->cue_specification);
  946. decoder_session->total_samples -= (metadata->data.stream_info.total_samples - until);
  947. }
  948. if(decoder_session->bps != 8 && decoder_session->bps != 16 && decoder_session->bps != 24) {
  949. flac__utils_printf(stderr, 1, "%s: ERROR: bits per sample is not 8/16/24n", decoder_session->inbasefilename);
  950. decoder_session->abort_flag = true;
  951. return;
  952. }
  953. }
  954. else if(metadata->type == FLAC__METADATA_TYPE_CUESHEET) {
  955. /* remember, at this point, decoder_session->total_samples can be 0, meaning 'unknown' */
  956. if(decoder_session->total_samples == 0) {
  957. flac__utils_printf(stderr, 1, "%s: ERROR can't use --cue when FLAC metadata has total sample count of 0n", decoder_session->inbasefilename);
  958. decoder_session->abort_flag = true;
  959. return;
  960. }
  961. 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);
  962. FLAC__ASSERT(!decoder_session->skip_specification->is_relative);
  963. FLAC__ASSERT(decoder_session->skip_specification->value_is_samples);
  964. FLAC__ASSERT(!decoder_session->until_specification->is_relative);
  965. FLAC__ASSERT(decoder_session->until_specification->value_is_samples);
  966. FLAC__ASSERT(decoder_session->skip_specification->value.samples >= 0);
  967. FLAC__ASSERT(decoder_session->until_specification->value.samples >= 0);
  968. FLAC__ASSERT((FLAC__uint64)decoder_session->until_specification->value.samples <= decoder_session->total_samples);
  969. FLAC__ASSERT(decoder_session->skip_specification->value.samples <= decoder_session->until_specification->value.samples);
  970. decoder_session->total_samples = decoder_session->until_specification->value.samples - decoder_session->skip_specification->value.samples;
  971. }
  972. else if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
  973. if (decoder_session->replaygain.spec.apply) {
  974. double gain, peak;
  975. if (!(decoder_session->replaygain.apply = grabbag__replaygain_load_from_vorbiscomment(metadata, decoder_session->replaygain.spec.use_album_gain, &gain, &peak))) {
  976. flac__utils_printf(stderr, 1, "%s: WARNING: can't get %s ReplayGain tagn", decoder_session->inbasefilename, decoder_session->replaygain.spec.use_album_gain? "album":"track");
  977. }
  978. else {
  979. const char *ls[] = { "no", "peak", "hard" };
  980. const char *ns[] = { "no", "low", "medium", "high" };
  981. decoder_session->replaygain.scale = grabbag__replaygain_compute_scale_factor(peak, gain, decoder_session->replaygain.spec.preamp, decoder_session->replaygain.spec.limiter == RGSS_LIMIT__PEAK);
  982. FLAC__ASSERT(decoder_session->bps > 0 && decoder_session->bps <= 32);
  983. FLAC__replaygain_synthesis__init_dither_context(&decoder_session->replaygain.dither_context, decoder_session->bps, decoder_session->replaygain.spec.noise_shaping);
  984. flac__utils_printf(stderr, 1, "%s: INFO: applying %s ReplayGain (gain=%0.2fdB+preamp=%0.1fdB, %s noise shaping, %s limiting) to outputn", 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]);
  985. flac__utils_printf(stderr, 1, "%s: WARNING: applying ReplayGain is not losslessn", decoder_session->inbasefilename);
  986. }
  987. }
  988. }
  989. }
  990. void error_callback(const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
  991. {
  992. DecoderSession *decoder_session = (DecoderSession*)client_data;
  993. (void)decoder;
  994. flac__utils_printf(stderr, 1, "%s: *** Got error code %d:%sn", decoder_session->inbasefilename, status, FLAC__StreamDecoderErrorStatusString[status]);
  995. if(!decoder_session->continue_through_decode_errors)
  996. decoder_session->abort_flag = true;
  997. }
  998. void print_error_with_state(const DecoderSession *d, const char *message)
  999. {
  1000. const int ilen = strlen(d->inbasefilename) + 1;
  1001. const char *state_string;
  1002. flac__utils_printf(stderr, 1, "n%s: %sn", d->inbasefilename, message);
  1003. #ifdef FLAC__HAS_OGG
  1004. if(d->is_ogg) {
  1005. state_string = OggFLAC__file_decoder_get_resolved_state_string(d->decoder.ogg.file);
  1006. }
  1007. else
  1008. #endif
  1009. {
  1010. state_string = FLAC__file_decoder_get_resolved_state_string(d->decoder.flac.file);
  1011. }
  1012. flac__utils_printf(stderr, 1, "%*s state = %sn", ilen, "", state_string);
  1013. /* print out some more info for some errors: */
  1014. if (0 == strcmp(state_string, FLAC__StreamDecoderStateString[FLAC__STREAM_DECODER_UNPARSEABLE_STREAM])) {
  1015. flac__utils_printf(stderr, 1,
  1016. "n"
  1017. "The FLAC stream may have been created by a more advanced encoder.  Tryn"
  1018. "  metaflac --show-vendor-tag %sn"
  1019. "If the version number is greater than %s, this decoder is probablyn"
  1020. "not able to decode the file.  If the version number is not, the filen"
  1021. "may be corrupted, or you may have found a bug.  In this case pleasen"
  1022. "submit a bug report ton"
  1023. "    http://sourceforge.net/bugs/?func=addbug&group_id=13478n"
  1024. "Make sure to include an email contact in the comment and/or use then"
  1025. ""Monitor" feature to monitor the bug status.n",
  1026. d->inbasefilename, FLAC__VERSION_STRING
  1027. );
  1028. }
  1029. else if (
  1030. 0 == strcmp(state_string, FLAC__FileDecoderStateString[FLAC__FILE_DECODER_ERROR_OPENING_FILE])
  1031. #ifdef FLAC__HAS_OGG
  1032. || 0 == strcmp(state_string, OggFLAC__FileDecoderStateString[OggFLAC__FILE_DECODER_ERROR_OPENING_FILE])
  1033. #endif
  1034. ) {
  1035. flac__utils_printf(stderr, 1,
  1036. "n"
  1037. "An error occurred opening the input file; it is likely that it does not existn"
  1038. "or is not readable.n"
  1039. );
  1040. }
  1041. }
  1042. void print_stats(const DecoderSession *decoder_session)
  1043. {
  1044. if(flac__utils_verbosity_ >= 2) {
  1045. #if defined _MSC_VER || defined __MINGW32__
  1046. /* with MSVC you have to spoon feed it the casting */
  1047. const double progress = (double)(FLAC__int64)decoder_session->samples_processed / (double)(FLAC__int64)decoder_session->total_samples * 100.0;
  1048. #else
  1049. const double progress = (double)decoder_session->samples_processed / (double)decoder_session->total_samples * 100.0;
  1050. #endif
  1051. if(decoder_session->total_samples > 0) {
  1052. fprintf(stderr, "r%s: %s%u%% complete",
  1053. decoder_session->inbasefilename,
  1054. decoder_session->test_only? "testing, " : decoder_session->analysis_mode? "analyzing, " : "",
  1055. (unsigned)floor(progress + 0.5)
  1056. );
  1057. }
  1058. else {
  1059. fprintf(stderr, "r%s: %s %u samples",
  1060. decoder_session->inbasefilename,
  1061. decoder_session->test_only? "tested" : decoder_session->analysis_mode? "analyzed" : "wrote",
  1062. (unsigned)decoder_session->samples_processed
  1063. );
  1064. }
  1065. }
  1066. }