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

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. #if defined _WIN32 && !defined __CYGWIN__
  19. /* where MSVC puts unlink() */
  20. # include <io.h>
  21. #else
  22. # include <unistd.h>
  23. #endif
  24. #include <limits.h> /* for LONG_MAX */
  25. #include <math.h> /* for floor() */
  26. #include <stdio.h> /* for FILE etc. */
  27. #include <stdlib.h> /* for malloc */
  28. #include <string.h> /* for strcmp() */
  29. #include "FLAC/all.h"
  30. #include "share/grabbag.h"
  31. #include "encode.h"
  32. #ifdef HAVE_CONFIG_H
  33. #include <config.h>
  34. #endif
  35. #ifdef FLAC__HAS_OGG
  36. #include "OggFLAC/stream_encoder.h"
  37. #include "OggFLAC/file_encoder.h"
  38. #endif
  39. #ifdef min
  40. #undef min
  41. #endif
  42. #define min(x,y) ((x)<(y)?(x):(y))
  43. #ifdef max
  44. #undef max
  45. #endif
  46. #define max(x,y) ((x)>(y)?(x):(y))
  47. /* this MUST be >= 588 so that sector aligning can take place with one read */
  48. #define CHUNK_OF_SAMPLES 2048
  49. typedef struct {
  50. #ifdef FLAC__HAS_OGG
  51. FLAC__bool use_ogg;
  52. #endif
  53. FLAC__bool verify;
  54. FLAC__bool is_stdout;
  55. const char *inbasefilename;
  56. const char *outfilename;
  57. FLAC__uint64 skip;
  58. FLAC__uint64 until; /* a value of 0 mean end-of-stream (i.e. --until=-0) */
  59. FLAC__bool replay_gain;
  60. unsigned channels;
  61. unsigned bits_per_sample;
  62. unsigned sample_rate;
  63. FLAC__uint64 unencoded_size;
  64. FLAC__uint64 total_samples_to_encode;
  65. FLAC__uint64 bytes_written;
  66. FLAC__uint64 samples_written;
  67. unsigned blocksize;
  68. unsigned stats_mask;
  69. /*
  70.  * We use *.stream for encoding to stdout
  71.  * We use *.file for encoding to a regular file
  72.  */
  73. union {
  74. union {
  75. FLAC__StreamEncoder *stream;
  76. FLAC__FileEncoder *file;
  77. } flac;
  78. #ifdef FLAC__HAS_OGG
  79. union {
  80. OggFLAC__StreamEncoder *stream;
  81. OggFLAC__FileEncoder *file;
  82. } ogg;
  83. #endif
  84. } encoder;
  85. FILE *fin;
  86. FILE *fout;
  87. FLAC__StreamMetadata *seek_table_template;
  88. } EncoderSession;
  89. static FLAC__bool is_big_endian_host_;
  90. static unsigned char ucbuffer_[CHUNK_OF_SAMPLES*FLAC__MAX_CHANNELS*((FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE+7)/8)];
  91. static signed char *scbuffer_ = (signed char *)ucbuffer_;
  92. static FLAC__uint16 *usbuffer_ = (FLAC__uint16 *)ucbuffer_;
  93. static FLAC__int16 *ssbuffer_ = (FLAC__int16 *)ucbuffer_;
  94. static FLAC__int32 in_[FLAC__MAX_CHANNELS][CHUNK_OF_SAMPLES];
  95. static FLAC__int32 *input_[FLAC__MAX_CHANNELS];
  96. /*
  97.  * unpublished debug routines from the FLAC libs
  98.  */
  99. extern FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value);
  100. extern FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value);
  101. extern FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value);
  102. extern FLAC__bool FLAC__file_encoder_disable_constant_subframes(FLAC__FileEncoder *encoder, FLAC__bool value);
  103. extern FLAC__bool FLAC__file_encoder_disable_fixed_subframes(FLAC__FileEncoder *encoder, FLAC__bool value);
  104. extern FLAC__bool FLAC__file_encoder_disable_verbatim_subframes(FLAC__FileEncoder *encoder, FLAC__bool value);
  105. #ifdef FLAC__HAS_OGG
  106. extern FLAC__bool OggFLAC__stream_encoder_disable_constant_subframes(OggFLAC__StreamEncoder *encoder, FLAC__bool value);
  107. extern FLAC__bool OggFLAC__stream_encoder_disable_fixed_subframes(OggFLAC__StreamEncoder *encoder, FLAC__bool value);
  108. extern FLAC__bool OggFLAC__stream_encoder_disable_verbatim_subframes(OggFLAC__StreamEncoder *encoder, FLAC__bool value);
  109. extern FLAC__bool OggFLAC__file_encoder_disable_constant_subframes(OggFLAC__FileEncoder *encoder, FLAC__bool value);
  110. extern FLAC__bool OggFLAC__file_encoder_disable_fixed_subframes(OggFLAC__FileEncoder *encoder, FLAC__bool value);
  111. extern FLAC__bool OggFLAC__file_encoder_disable_verbatim_subframes(OggFLAC__FileEncoder *encoder, FLAC__bool value);
  112. #endif
  113. /*
  114.  * local routines
  115.  */
  116. static FLAC__bool EncoderSession_construct(EncoderSession *e, FLAC__bool use_ogg, FLAC__bool verify, FILE *infile, const char *infilename, const char *outfilename);
  117. static void EncoderSession_destroy(EncoderSession *e);
  118. static int EncoderSession_finish_ok(EncoderSession *e, int info_align_carry, int info_align_zero);
  119. static int EncoderSession_finish_error(EncoderSession *e);
  120. static FLAC__bool EncoderSession_init_encoder(EncoderSession *e, encode_options_t options, unsigned channels, unsigned bps, unsigned sample_rate);
  121. static FLAC__bool EncoderSession_process(EncoderSession *e, const FLAC__int32 * const buffer[], unsigned samples);
  122. static FLAC__bool convert_to_seek_table_template(const char *requested_seek_points, int num_requested_seek_points, FLAC__StreamMetadata *cuesheet, EncoderSession *e);
  123. static FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, unsigned sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input);
  124. static void format_input(FLAC__int32 *dest[], unsigned wide_samples, FLAC__bool is_big_endian, FLAC__bool is_unsigned_samples, unsigned channels, unsigned bps);
  125. #ifdef FLAC__HAS_OGG
  126. static FLAC__StreamEncoderWriteStatus ogg_stream_encoder_write_callback(const OggFLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
  127. static void ogg_stream_encoder_metadata_callback(const OggFLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data);
  128. static void ogg_file_encoder_progress_callback(const OggFLAC__FileEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data);
  129. #endif
  130. static FLAC__StreamEncoderWriteStatus flac_stream_encoder_write_callback(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
  131. static void flac_stream_encoder_metadata_callback(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data);
  132. static void flac_file_encoder_progress_callback(const FLAC__FileEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data);
  133. static FLAC__bool parse_cuesheet_(FLAC__StreamMetadata **cuesheet, const char *cuesheet_filename, const char *inbasefilename, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset);
  134. static void print_stats(const EncoderSession *encoder_session);
  135. static void print_error_with_state(const EncoderSession *e, const char *message);
  136. static void print_verify_error(EncoderSession *e);
  137. static FLAC__bool read_little_endian_uint16(FILE *f, FLAC__uint16 *val, FLAC__bool eof_ok, const char *fn);
  138. static FLAC__bool read_little_endian_uint32(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn);
  139. static FLAC__bool read_big_endian_uint16(FILE *f, FLAC__uint16 *val, FLAC__bool eof_ok, const char *fn);
  140. static FLAC__bool read_big_endian_uint32(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn);
  141. static FLAC__bool read_sane_extended(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn);
  142. static FLAC__bool fskip_ahead(FILE *f, FLAC__uint64 offset);
  143. /*
  144.  * public routines
  145.  */
  146. int
  147. flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const char *outfilename,
  148. const FLAC__byte *lookahead, unsigned lookahead_length, wav_encode_options_t options)
  149. {
  150. EncoderSession encoder_session;
  151. FLAC__uint16 x;
  152. FLAC__uint32 xx;
  153. unsigned int channels= 0U, bps= 0U, sample_rate= 0U, sample_frames= 0U;
  154. FLAC__bool got_comm_chunk= false, got_ssnd_chunk= false;
  155. int info_align_carry= -1, info_align_zero= -1;
  156. (void)infilesize; /* silence compiler warning about unused parameter */
  157. (void)lookahead; /* silence compiler warning about unused parameter */
  158. (void)lookahead_length; /* silence compiler warning about unused parameter */
  159. if(!
  160. EncoderSession_construct(
  161. &encoder_session,
  162. #ifdef FLAC__HAS_OGG
  163. options.common.use_ogg,
  164. #else
  165. /*use_ogg=*/false,
  166. #endif
  167. options.common.verify,
  168. infile,
  169. infilename,
  170. outfilename
  171. )
  172. )
  173. return 1;
  174. /* lookahead[] already has "FORMxxxxAIFF", do sub-chunks */
  175. while(1) {
  176. size_t c= 0U;
  177. char chunk_id[4];
  178. /* chunk identifier; really conservative about behavior of fread() and feof() */
  179. if(feof(infile) || ((c= fread(chunk_id, 1U, 4U, infile)), c==0U && feof(infile)))
  180. break;
  181. else if(c<4U || feof(infile)) {
  182. flac__utils_printf(stderr, 1, "%s: ERROR: incomplete chunk identifiern", encoder_session.inbasefilename);
  183. return EncoderSession_finish_error(&encoder_session);
  184. }
  185. if(got_comm_chunk==false && !strncmp(chunk_id, "COMM", 4)) { /* common chunk */
  186. unsigned long skip;
  187. /* COMM chunk size */
  188. if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
  189. return EncoderSession_finish_error(&encoder_session);
  190. else if(xx<18U) {
  191. flac__utils_printf(stderr, 1, "%s: ERROR: non-standard 'COMM' chunk has length = %un", encoder_session.inbasefilename, (unsigned int)xx);
  192. return EncoderSession_finish_error(&encoder_session);
  193. }
  194. else if(xx!=18U) {
  195. flac__utils_printf(stderr, 1, "%s: WARNING: non-standard 'COMM' chunk has length = %un", encoder_session.inbasefilename, (unsigned int)xx);
  196. }
  197. skip= (xx-18U)+(xx & 1U);
  198. /* number of channels */
  199. if(!read_big_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
  200. return EncoderSession_finish_error(&encoder_session);
  201. else if(x==0U || x>FLAC__MAX_CHANNELS) {
  202. flac__utils_printf(stderr, 1, "%s: ERROR: unsupported number channels %un", encoder_session.inbasefilename, (unsigned int)x);
  203. return EncoderSession_finish_error(&encoder_session);
  204. }
  205. else if(options.common.sector_align && x!=2U) {
  206. flac__utils_printf(stderr, 1, "%s: ERROR: file has %u channels, must be 2 for --sector-alignn", encoder_session.inbasefilename, (unsigned int)x);
  207. return EncoderSession_finish_error(&encoder_session);
  208. }
  209. channels= x;
  210. /* number of sample frames */
  211. if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
  212. return EncoderSession_finish_error(&encoder_session);
  213. sample_frames= xx;
  214. /* bits per sample */
  215. if(!read_big_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
  216. return EncoderSession_finish_error(&encoder_session);
  217. else if(x!=8U && x!=16U && x!=24U) {
  218. flac__utils_printf(stderr, 1, "%s: ERROR: unsupported bits per sample %un", encoder_session.inbasefilename, (unsigned int)x);
  219. return EncoderSession_finish_error(&encoder_session);
  220. }
  221. else if(options.common.sector_align && x!=16U) {
  222. flac__utils_printf(stderr, 1, "%s: ERROR: file has %u bits per sample, must be 16 for --sector-alignn", encoder_session.inbasefilename, (unsigned int)x);
  223. return EncoderSession_finish_error(&encoder_session);
  224. }
  225. bps= x;
  226. /* sample rate */
  227. if(!read_sane_extended(infile, &xx, false, encoder_session.inbasefilename))
  228. return EncoderSession_finish_error(&encoder_session);
  229. else if(!FLAC__format_sample_rate_is_valid(xx)) {
  230. flac__utils_printf(stderr, 1, "%s: ERROR: unsupported sample rate %un", encoder_session.inbasefilename, (unsigned int)xx);
  231. return EncoderSession_finish_error(&encoder_session);
  232. }
  233. else if(options.common.sector_align && xx!=44100U) {
  234. flac__utils_printf(stderr, 1, "%s: ERROR: file's sample rate is %u, must be 44100 for --sector-alignn", encoder_session.inbasefilename, (unsigned int)xx);
  235. return EncoderSession_finish_error(&encoder_session);
  236. }
  237. sample_rate= xx;
  238. /* skip any extra data in the COMM chunk */
  239. if(!fskip_ahead(infile, skip)) {
  240. flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping extra COMM datan", encoder_session.inbasefilename);
  241. return EncoderSession_finish_error(&encoder_session);
  242. }
  243. /*
  244.  * now that we know the sample rate, canonicalize the
  245.  * --skip string to a number of samples:
  246.  */
  247. flac__utils_canonicalize_skip_until_specification(&options.common.skip_specification, sample_rate);
  248. FLAC__ASSERT(options.common.skip_specification.value.samples >= 0);
  249. encoder_session.skip = (FLAC__uint64)options.common.skip_specification.value.samples;
  250. FLAC__ASSERT(!options.common.sector_align || encoder_session.skip == 0);
  251. got_comm_chunk= true;
  252. }
  253. else if(got_ssnd_chunk==false && !strncmp(chunk_id, "SSND", 4)) { /* sound data chunk */
  254. unsigned int offset= 0U, block_size= 0U, align_remainder= 0U, data_bytes;
  255. size_t bytes_per_frame= channels*(bps>>3);
  256. FLAC__uint64 total_samples_in_input, trim = 0;
  257. FLAC__bool pad= false;
  258. if(got_comm_chunk==false) {
  259. flac__utils_printf(stderr, 1, "%s: ERROR: got 'SSND' chunk before 'COMM' chunkn", encoder_session.inbasefilename);
  260. return EncoderSession_finish_error(&encoder_session);
  261. }
  262. /* SSND chunk size */
  263. if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
  264. return EncoderSession_finish_error(&encoder_session);
  265. data_bytes= xx;
  266. pad= (data_bytes & 1U) ? true : false;
  267. data_bytes-= 8U; /* discount the offset and block size fields */
  268. /* offset */
  269. if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
  270. return EncoderSession_finish_error(&encoder_session);
  271. offset= xx;
  272. data_bytes-= offset;
  273. /* block size */
  274. if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
  275. return EncoderSession_finish_error(&encoder_session);
  276. else if(xx!=0U) {
  277. flac__utils_printf(stderr, 1, "%s: ERROR: block size is %u; must be 0n", encoder_session.inbasefilename, (unsigned int)xx);
  278. return EncoderSession_finish_error(&encoder_session);
  279. }
  280. block_size= xx;
  281. /* skip any SSND offset bytes */
  282. FLAC__ASSERT(offset<=LONG_MAX);
  283. if(!fskip_ahead(infile, offset)) {
  284. flac__utils_printf(stderr, 1, "%s: ERROR: skipping offset in SSND chunkn", encoder_session.inbasefilename);
  285. return EncoderSession_finish_error(&encoder_session);
  286. }
  287. if(data_bytes!=(sample_frames*bytes_per_frame)) {
  288. flac__utils_printf(stderr, 1, "%s: ERROR: SSND chunk size inconsistent with sample frame countn", encoder_session.inbasefilename);
  289. return EncoderSession_finish_error(&encoder_session);
  290. }
  291. /* *options.common.align_reservoir_samples will be 0 unless --sector-align is used */
  292. FLAC__ASSERT(options.common.sector_align || *options.common.align_reservoir_samples == 0);
  293. total_samples_in_input = data_bytes / bytes_per_frame + *options.common.align_reservoir_samples;
  294. /*
  295.  * now that we know the input size, canonicalize the
  296.  * --until string to an absolute sample number:
  297.  */
  298. if(!canonicalize_until_specification(&options.common.until_specification, encoder_session.inbasefilename, sample_rate, encoder_session.skip, total_samples_in_input))
  299. return EncoderSession_finish_error(&encoder_session);
  300. encoder_session.until = (FLAC__uint64)options.common.until_specification.value.samples;
  301. FLAC__ASSERT(!options.common.sector_align || encoder_session.until == 0);
  302. if(encoder_session.skip>0U) {
  303. if(!fskip_ahead(infile, encoder_session.skip*bytes_per_frame)) {
  304. flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samplesn", encoder_session.inbasefilename);
  305. return EncoderSession_finish_error(&encoder_session);
  306. }
  307. }
  308. data_bytes-= (unsigned int)encoder_session.skip*bytes_per_frame; /*@@@ WATCHOUT: 4GB limit */
  309. encoder_session.total_samples_to_encode= total_samples_in_input - encoder_session.skip;
  310. if(encoder_session.until > 0) {
  311. trim = total_samples_in_input - encoder_session.until;
  312. FLAC__ASSERT(total_samples_in_input > 0);
  313. FLAC__ASSERT(!options.common.sector_align);
  314. data_bytes-= (unsigned int)trim*bytes_per_frame;
  315. encoder_session.total_samples_to_encode-= trim;
  316. }
  317. if(options.common.sector_align) {
  318. align_remainder= (unsigned int)(encoder_session.total_samples_to_encode % 588U);
  319. if(options.common.is_last_file)
  320. encoder_session.total_samples_to_encode+= (588U-align_remainder); /* will pad with zeroes */
  321. else
  322. encoder_session.total_samples_to_encode-= align_remainder; /* will stop short and carry over to next file */
  323. }
  324. /* +54 for the size of the AIFF headers; this is just an estimate for the progress indicator and doesn't need to be exact */
  325. encoder_session.unencoded_size= encoder_session.total_samples_to_encode*bytes_per_frame+54;
  326. if(!EncoderSession_init_encoder(&encoder_session, options.common, channels, bps, sample_rate))
  327. return EncoderSession_finish_error(&encoder_session);
  328. /* first do any samples in the reservoir */
  329. if(options.common.sector_align && *options.common.align_reservoir_samples>0U) {
  330. if(!EncoderSession_process(&encoder_session, (const FLAC__int32 *const *)options.common.align_reservoir, *options.common.align_reservoir_samples)) {
  331. print_error_with_state(&encoder_session, "ERROR during encoding");
  332. return EncoderSession_finish_error(&encoder_session);
  333. }
  334. }
  335. /* decrement the data_bytes counter if we need to align the file */
  336. if(options.common.sector_align) {
  337. if(options.common.is_last_file)
  338. *options.common.align_reservoir_samples= 0U;
  339. else {
  340. *options.common.align_reservoir_samples= align_remainder;
  341. data_bytes-= (*options.common.align_reservoir_samples)*bytes_per_frame;
  342. }
  343. }
  344. /* now do from the file */
  345. while(data_bytes>0) {
  346. size_t bytes_read= fread(ucbuffer_, 1U, min(data_bytes, CHUNK_OF_SAMPLES*bytes_per_frame), infile);
  347. if(bytes_read==0U) {
  348. if(ferror(infile)) {
  349. flac__utils_printf(stderr, 1, "%s: ERROR during readn", encoder_session.inbasefilename);
  350. return EncoderSession_finish_error(&encoder_session);
  351. }
  352. else if(feof(infile)) {
  353. flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; expected %u samples, got %u samplesn", encoder_session.inbasefilename, (unsigned int)encoder_session.total_samples_to_encode, (unsigned int)encoder_session.samples_written);
  354. data_bytes= 0;
  355. }
  356. }
  357. else {
  358. if(bytes_read % bytes_per_frame != 0U) {
  359. flac__utils_printf(stderr, 1, "%s: ERROR: got partial samplen", encoder_session.inbasefilename);
  360. return EncoderSession_finish_error(&encoder_session);
  361. }
  362. else {
  363. unsigned int frames= bytes_read/bytes_per_frame;
  364. format_input(input_, frames, true, false, channels, bps);
  365. if(!EncoderSession_process(&encoder_session, (const FLAC__int32 *const *)input_, frames)) {
  366. print_error_with_state(&encoder_session, "ERROR during encoding");
  367. return EncoderSession_finish_error(&encoder_session);
  368. }
  369. else
  370. data_bytes-= bytes_read;
  371. }
  372. }
  373. }
  374. if(trim>0) {
  375. FLAC__ASSERT(!options.common.sector_align);
  376. if(!fskip_ahead(infile, trim*bytes_per_frame)) {
  377. flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samplesn", encoder_session.inbasefilename);
  378. return EncoderSession_finish_error(&encoder_session);
  379. }
  380. }
  381. /* now read unaligned samples into reservoir or pad with zeroes if necessary */
  382. if(options.common.sector_align) {
  383. if(options.common.is_last_file) {
  384. unsigned int pad_frames= 588U-align_remainder;
  385. if(pad_frames<588U) {
  386. unsigned int i;
  387. info_align_zero= pad_frames;
  388. for(i= 0U; i<channels; ++i)
  389. memset(input_[i], 0, pad_frames*(bps>>3));
  390. if(!EncoderSession_process(&encoder_session, (const FLAC__int32 *const *)input_, pad_frames)) {
  391. print_error_with_state(&encoder_session, "ERROR during encoding");
  392. return EncoderSession_finish_error(&encoder_session);
  393. }
  394. }
  395. }
  396. else {
  397. if(*options.common.align_reservoir_samples > 0) {
  398. size_t bytes_read= fread(ucbuffer_, 1U, (*options.common.align_reservoir_samples)*bytes_per_frame, infile);
  399. FLAC__ASSERT(CHUNK_OF_SAMPLES>=588U);
  400. if(bytes_read==0U && ferror(infile)) {
  401. flac__utils_printf(stderr, 1, "%s: ERROR during readn", encoder_session.inbasefilename);
  402. return EncoderSession_finish_error(&encoder_session);
  403. }
  404. else if(bytes_read != (*options.common.align_reservoir_samples) * bytes_per_frame) {
  405. flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; read %u bytes; expected %u samples, got %u samplesn", encoder_session.inbasefilename, (unsigned int)bytes_read, (unsigned int)encoder_session.total_samples_to_encode, (unsigned int)encoder_session.samples_written);
  406. }
  407. else {
  408. info_align_carry= *options.common.align_reservoir_samples;
  409. format_input(options.common.align_reservoir, *options.common.align_reservoir_samples, true, false, channels, bps);
  410. }
  411. }
  412. }
  413. }
  414. if(pad==true) {
  415. unsigned char tmp;
  416. if(fread(&tmp, 1U, 1U, infile)<1U) {
  417. flac__utils_printf(stderr, 1, "%s: ERROR during read of SSND pad byten", encoder_session.inbasefilename);
  418. return EncoderSession_finish_error(&encoder_session);
  419. }
  420. }
  421. got_ssnd_chunk= true;
  422. }
  423. else { /* other chunk */
  424. if(!strncmp(chunk_id, "COMM", 4)) {
  425. flac__utils_printf(stderr, 1, "%s: WARNING: skipping extra 'COMM' chunkn", encoder_session.inbasefilename);
  426. }
  427. else if(!strncmp(chunk_id, "SSND", 4)) {
  428. flac__utils_printf(stderr, 1, "%s: WARNING: skipping extra 'SSND' chunkn", encoder_session.inbasefilename);
  429. }
  430. else {
  431. flac__utils_printf(stderr, 1, "%s: WARNING: skipping unknown chunk '%s'n", encoder_session.inbasefilename, chunk_id);
  432. }
  433. /* chunk size */
  434. if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
  435. return EncoderSession_finish_error(&encoder_session);
  436. else {
  437. unsigned long skip= xx+(xx & 1U);
  438. FLAC__ASSERT(skip<=LONG_MAX);
  439. if(!fskip_ahead(infile, skip)) {
  440. fprintf(stderr, "%s: ERROR during read while skipping unknown chunkn", encoder_session.inbasefilename);
  441. return EncoderSession_finish_error(&encoder_session);
  442. }
  443. }
  444. }
  445. }
  446. if(got_ssnd_chunk==false && sample_frames!=0U) {
  447. flac__utils_printf(stderr, 1, "%s: ERROR: missing SSND chunkn", encoder_session.inbasefilename);
  448. return EncoderSession_finish_error(&encoder_session);
  449. }
  450. return EncoderSession_finish_ok(&encoder_session, info_align_carry, info_align_zero);
  451. }
  452. int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, wav_encode_options_t options)
  453. {
  454. EncoderSession encoder_session;
  455. FLAC__bool is_unsigned_samples = false;
  456. unsigned channels = 0, bps = 0, sample_rate = 0, data_bytes;
  457. size_t bytes_per_wide_sample, bytes_read;
  458. FLAC__uint16 x;
  459. FLAC__uint32 xx;
  460. FLAC__bool got_fmt_chunk = false, got_data_chunk = false;
  461. unsigned align_remainder = 0;
  462. int info_align_carry = -1, info_align_zero = -1;
  463. (void)infilesize;
  464. (void)lookahead;
  465. (void)lookahead_length;
  466. if(!
  467. EncoderSession_construct(
  468. &encoder_session,
  469. #ifdef FLAC__HAS_OGG
  470. options.common.use_ogg,
  471. #else
  472. /*use_ogg=*/false,
  473. #endif
  474. options.common.verify,
  475. infile,
  476. infilename,
  477. outfilename
  478. )
  479. )
  480. return 1;
  481. /*
  482.  * lookahead[] already has "RIFFxxxxWAVE", do sub-chunks
  483.  */
  484. while(!feof(infile)) {
  485. if(!read_little_endian_uint32(infile, &xx, true, encoder_session.inbasefilename))
  486. return EncoderSession_finish_error(&encoder_session);
  487. if(feof(infile))
  488. break;
  489. if(xx == 0x20746d66 && !got_fmt_chunk) { /* "fmt " */
  490. unsigned block_align;
  491. /* fmt sub-chunk size */
  492. if(!read_little_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
  493. return EncoderSession_finish_error(&encoder_session);
  494. if(xx < 16) {
  495. flac__utils_printf(stderr, 1, "%s: ERROR: found non-standard 'fmt ' sub-chunk which has length = %un", encoder_session.inbasefilename, (unsigned)xx);
  496. return EncoderSession_finish_error(&encoder_session);
  497. }
  498. else if(xx != 16 && xx != 18) {
  499. flac__utils_printf(stderr, 1, "%s: WARNING: found non-standard 'fmt ' sub-chunk which has length = %un", encoder_session.inbasefilename, (unsigned)xx);
  500. }
  501. data_bytes = xx;
  502. /* compression code */
  503. if(!read_little_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
  504. return EncoderSession_finish_error(&encoder_session);
  505. if(x != 1) {
  506. flac__utils_printf(stderr, 1, "%s: ERROR: unsupported compression type %un", encoder_session.inbasefilename, (unsigned)x);
  507. return EncoderSession_finish_error(&encoder_session);
  508. }
  509. /* number of channels */
  510. if(!read_little_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
  511. return EncoderSession_finish_error(&encoder_session);
  512. if(x == 0 || x > FLAC__MAX_CHANNELS) {
  513. flac__utils_printf(stderr, 1, "%s: ERROR: unsupported number channels %un", encoder_session.inbasefilename, (unsigned)x);
  514. return EncoderSession_finish_error(&encoder_session);
  515. }
  516. else if(options.common.sector_align && x != 2) {
  517. flac__utils_printf(stderr, 1, "%s: ERROR: file has %u channels, must be 2 for --sector-alignn", encoder_session.inbasefilename, (unsigned)x);
  518. return EncoderSession_finish_error(&encoder_session);
  519. }
  520. channels = x;
  521. /* sample rate */
  522. if(!read_little_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
  523. return EncoderSession_finish_error(&encoder_session);
  524. if(!FLAC__format_sample_rate_is_valid(xx)) {
  525. flac__utils_printf(stderr, 1, "%s: ERROR: unsupported sample rate %un", encoder_session.inbasefilename, (unsigned)xx);
  526. return EncoderSession_finish_error(&encoder_session);
  527. }
  528. else if(options.common.sector_align && xx != 44100) {
  529. flac__utils_printf(stderr, 1, "%s: ERROR: file's sample rate is %u, must be 44100 for --sector-alignn", encoder_session.inbasefilename, (unsigned)xx);
  530. return EncoderSession_finish_error(&encoder_session);
  531. }
  532. sample_rate = xx;
  533. /* avg bytes per second (ignored) */
  534. if(!read_little_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
  535. return EncoderSession_finish_error(&encoder_session);
  536. /* block align */
  537. if(!read_little_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
  538. return EncoderSession_finish_error(&encoder_session);
  539. block_align = x;
  540. /* bits per sample */
  541. if(!read_little_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
  542. return EncoderSession_finish_error(&encoder_session);
  543. if(x != 8 && x != 16 && x != 24) {
  544. flac__utils_printf(stderr, 1, "%s: ERROR: unsupported bits-per-sample %un", encoder_session.inbasefilename, (unsigned)x);
  545. return EncoderSession_finish_error(&encoder_session);
  546. }
  547. else if(options.common.sector_align && x != 16) {
  548. flac__utils_printf(stderr, 1, "%s: ERROR: file has %u bits per sample, must be 16 for --sector-alignn", encoder_session.inbasefilename, (unsigned)x);
  549. return EncoderSession_finish_error(&encoder_session);
  550. }
  551. bps = x;
  552. if(bps * channels != block_align * 8) {
  553. flac__utils_printf(stderr, 1, "%s: ERROR: unsupported block alignment (%u), for bits-per-sample=%u, channels=%un", encoder_session.inbasefilename, block_align, bps, channels);
  554. return EncoderSession_finish_error(&encoder_session);
  555. }
  556. is_unsigned_samples = (x == 8);
  557. /* skip any extra data in the fmt sub-chunk */
  558. data_bytes -= 16;
  559. if(data_bytes > 0) {
  560. unsigned left, need;
  561. for(left = data_bytes; left > 0; ) {
  562. need = min(left, CHUNK_OF_SAMPLES);
  563. if(fread(ucbuffer_, 1U, need, infile) < need) {
  564. flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samplesn", encoder_session.inbasefilename);
  565. return EncoderSession_finish_error(&encoder_session);
  566. }
  567. left -= need;
  568. }
  569. }
  570. /*
  571.  * now that we know the sample rate, canonicalize the
  572.  * --skip string to a number of samples:
  573.  */
  574. flac__utils_canonicalize_skip_until_specification(&options.common.skip_specification, sample_rate);
  575. FLAC__ASSERT(options.common.skip_specification.value.samples >= 0);
  576. encoder_session.skip = (FLAC__uint64)options.common.skip_specification.value.samples;
  577. FLAC__ASSERT(!options.common.sector_align || encoder_session.skip == 0);
  578. got_fmt_chunk = true;
  579. }
  580. else if(xx == 0x61746164 && !got_data_chunk && got_fmt_chunk) { /* "data" */
  581. FLAC__uint64 total_samples_in_input, trim = 0;
  582. FLAC__bool pad = false;
  583. /* data size */
  584. if(!read_little_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
  585. return EncoderSession_finish_error(&encoder_session);
  586. data_bytes = xx;
  587. pad = (data_bytes & 1U) ? true : false;
  588. bytes_per_wide_sample = channels * (bps >> 3);
  589. /* *options.common.align_reservoir_samples will be 0 unless --sector-align is used */
  590. FLAC__ASSERT(options.common.sector_align || *options.common.align_reservoir_samples == 0);
  591. total_samples_in_input = data_bytes / bytes_per_wide_sample + *options.common.align_reservoir_samples;
  592. /*
  593.  * now that we know the input size, canonicalize the
  594.  * --until string to an absolute sample number:
  595.  */
  596. if(!canonicalize_until_specification(&options.common.until_specification, encoder_session.inbasefilename, sample_rate, encoder_session.skip, total_samples_in_input))
  597. return EncoderSession_finish_error(&encoder_session);
  598. encoder_session.until = (FLAC__uint64)options.common.until_specification.value.samples;
  599. FLAC__ASSERT(!options.common.sector_align || encoder_session.until == 0);
  600. if(encoder_session.skip > 0) {
  601. if(!fskip_ahead(infile, encoder_session.skip * bytes_per_wide_sample)) {
  602. flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samplesn", encoder_session.inbasefilename);
  603. return EncoderSession_finish_error(&encoder_session);
  604. }
  605. }
  606. data_bytes -= (unsigned)encoder_session.skip * bytes_per_wide_sample; /*@@@ WATCHOUT: 4GB limit */
  607. encoder_session.total_samples_to_encode = total_samples_in_input - encoder_session.skip;
  608. if(encoder_session.until > 0) {
  609. trim = total_samples_in_input - encoder_session.until;
  610. FLAC__ASSERT(total_samples_in_input > 0);
  611. FLAC__ASSERT(!options.common.sector_align);
  612. data_bytes -= (unsigned int)trim * bytes_per_wide_sample;
  613. encoder_session.total_samples_to_encode -= trim;
  614. }
  615. if(options.common.sector_align) {
  616. align_remainder = (unsigned)(encoder_session.total_samples_to_encode % 588);
  617. if(options.common.is_last_file)
  618. encoder_session.total_samples_to_encode += (588-align_remainder); /* will pad with zeroes */
  619. else
  620. encoder_session.total_samples_to_encode -= align_remainder; /* will stop short and carry over to next file */
  621. }
  622. /* +44 for the size of the WAV headers; this is just an estimate for the progress indicator and doesn't need to be exact */
  623. encoder_session.unencoded_size = encoder_session.total_samples_to_encode * bytes_per_wide_sample + 44;
  624. if(!EncoderSession_init_encoder(&encoder_session, options.common, channels, bps, sample_rate))
  625. return EncoderSession_finish_error(&encoder_session);
  626. /*
  627.  * first do any samples in the reservoir
  628.  */
  629. if(options.common.sector_align && *options.common.align_reservoir_samples > 0) {
  630. if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)options.common.align_reservoir, *options.common.align_reservoir_samples)) {
  631. print_error_with_state(&encoder_session, "ERROR during encoding");
  632. return EncoderSession_finish_error(&encoder_session);
  633. }
  634. }
  635. /*
  636.  * decrement the data_bytes counter if we need to align the file
  637.  */
  638. if(options.common.sector_align) {
  639. if(options.common.is_last_file) {
  640. *options.common.align_reservoir_samples = 0;
  641. }
  642. else {
  643. *options.common.align_reservoir_samples = align_remainder;
  644. data_bytes -= (*options.common.align_reservoir_samples) * bytes_per_wide_sample;
  645. }
  646. }
  647. /*
  648.  * now do from the file
  649.  */
  650. while(data_bytes > 0) {
  651. bytes_read = fread(ucbuffer_, sizeof(unsigned char), min(data_bytes, CHUNK_OF_SAMPLES * bytes_per_wide_sample), infile);
  652. if(bytes_read == 0) {
  653. if(ferror(infile)) {
  654. flac__utils_printf(stderr, 1, "%s: ERROR during readn", encoder_session.inbasefilename);
  655. return EncoderSession_finish_error(&encoder_session);
  656. }
  657. else if(feof(infile)) {
  658. flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; expected %u samples, got %u samplesn", encoder_session.inbasefilename, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
  659. data_bytes = 0;
  660. }
  661. }
  662. else {
  663. if(bytes_read % bytes_per_wide_sample != 0) {
  664. flac__utils_printf(stderr, 1, "%s: ERROR: got partial samplen", encoder_session.inbasefilename);
  665. return EncoderSession_finish_error(&encoder_session);
  666. }
  667. else {
  668. unsigned wide_samples = bytes_read / bytes_per_wide_sample;
  669. format_input(input_, wide_samples, false, is_unsigned_samples, channels, bps);
  670. if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
  671. print_error_with_state(&encoder_session, "ERROR during encoding");
  672. return EncoderSession_finish_error(&encoder_session);
  673. }
  674. data_bytes -= bytes_read;
  675. }
  676. }
  677. }
  678. if(trim > 0) {
  679. FLAC__ASSERT(!options.common.sector_align);
  680. if(!fskip_ahead(infile, trim * bytes_per_wide_sample)) {
  681. flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samplesn", encoder_session.inbasefilename);
  682. return EncoderSession_finish_error(&encoder_session);
  683. }
  684. }
  685. /*
  686.  * now read unaligned samples into reservoir or pad with zeroes if necessary
  687.  */
  688. if(options.common.sector_align) {
  689. if(options.common.is_last_file) {
  690. unsigned wide_samples = 588 - align_remainder;
  691. if(wide_samples < 588) {
  692. unsigned channel;
  693. info_align_zero = wide_samples;
  694. data_bytes = wide_samples * (bps >> 3);
  695. for(channel = 0; channel < channels; channel++)
  696. memset(input_[channel], 0, data_bytes);
  697. if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
  698. print_error_with_state(&encoder_session, "ERROR during encoding");
  699. return EncoderSession_finish_error(&encoder_session);
  700. }
  701. }
  702. }
  703. else {
  704. if(*options.common.align_reservoir_samples > 0) {
  705. FLAC__ASSERT(CHUNK_OF_SAMPLES >= 588);
  706. bytes_read = fread(ucbuffer_, sizeof(unsigned char), (*options.common.align_reservoir_samples) * bytes_per_wide_sample, infile);
  707. if(bytes_read == 0 && ferror(infile)) {
  708. flac__utils_printf(stderr, 1, "%s: ERROR during readn", encoder_session.inbasefilename);
  709. return EncoderSession_finish_error(&encoder_session);
  710. }
  711. else if(bytes_read != (*options.common.align_reservoir_samples) * bytes_per_wide_sample) {
  712. flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; read %u bytes; expected %u samples, got %u samplesn", encoder_session.inbasefilename, (unsigned)bytes_read, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
  713. data_bytes = 0;
  714. }
  715. else {
  716. info_align_carry = *options.common.align_reservoir_samples;
  717. format_input(options.common.align_reservoir, *options.common.align_reservoir_samples, false, is_unsigned_samples, channels, bps);
  718. }
  719. }
  720. }
  721. }
  722. if(pad == true) {
  723. unsigned char tmp;
  724. if(fread(&tmp, 1U, 1U, infile) < 1U) {
  725. flac__utils_printf(stderr, 1, "%s: ERROR during read of data pad byten", encoder_session.inbasefilename);
  726. return EncoderSession_finish_error(&encoder_session);
  727. }
  728. }
  729. got_data_chunk = true;
  730. }
  731. else {
  732. if(xx == 0x20746d66 && got_fmt_chunk) { /* "fmt " */
  733. flac__utils_printf(stderr, 1, "%s: WARNING: skipping extra 'fmt ' sub-chunkn", encoder_session.inbasefilename);
  734. }
  735. else if(xx == 0x61746164) { /* "data" */
  736. if(got_data_chunk) {
  737. flac__utils_printf(stderr, 1, "%s: WARNING: skipping extra 'data' sub-chunkn", encoder_session.inbasefilename);
  738. }
  739. else if(!got_fmt_chunk) {
  740. flac__utils_printf(stderr, 1, "%s: ERROR: got 'data' sub-chunk before 'fmt' sub-chunkn", encoder_session.inbasefilename);
  741. return EncoderSession_finish_error(&encoder_session);
  742. }
  743. else {
  744. FLAC__ASSERT(0);
  745. }
  746. }
  747. else {
  748. flac__utils_printf(stderr, 1, "%s: WARNING: skipping unknown sub-chunk '%c%c%c%c'n", encoder_session.inbasefilename, (char)(xx&255), (char)((xx>>8)&255), (char)((xx>>16)&255), (char)(xx>>24));
  749. }
  750. /* sub-chunk size */
  751. if(!read_little_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
  752. return EncoderSession_finish_error(&encoder_session);
  753. else {
  754. unsigned long skip = xx+(xx & 1U);
  755. FLAC__ASSERT(skip<=LONG_MAX);
  756. if(!fskip_ahead(infile, skip)) {
  757. flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping unsupported sub-chunkn", encoder_session.inbasefilename);
  758. return EncoderSession_finish_error(&encoder_session);
  759. }
  760. }
  761. }
  762. }
  763. return EncoderSession_finish_ok(&encoder_session, info_align_carry, info_align_zero);
  764. }
  765. int flac__encode_raw(FILE *infile, long infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, raw_encode_options_t options)
  766. {
  767. EncoderSession encoder_session;
  768. size_t bytes_read;
  769. const size_t bytes_per_wide_sample = options.channels * (options.bps >> 3);
  770. unsigned align_remainder = 0;
  771. int info_align_carry = -1, info_align_zero = -1;
  772. FLAC__uint64 total_samples_in_input = 0;;
  773. FLAC__ASSERT(!options.common.sector_align || options.channels == 2);
  774. FLAC__ASSERT(!options.common.sector_align || options.bps == 16);
  775. FLAC__ASSERT(!options.common.sector_align || options.sample_rate == 44100);
  776. FLAC__ASSERT(!options.common.sector_align || infilesize >= 0);
  777. FLAC__ASSERT(!options.common.replay_gain || options.channels <= 2);
  778. FLAC__ASSERT(!options.common.replay_gain || grabbag__replaygain_is_valid_sample_frequency(options.sample_rate));
  779. if(!
  780. EncoderSession_construct(
  781. &encoder_session,
  782. #ifdef FLAC__HAS_OGG
  783. options.common.use_ogg,
  784. #else
  785. /*use_ogg=*/false,
  786. #endif
  787. options.common.verify,
  788. infile,
  789. infilename,
  790. outfilename
  791. )
  792. )
  793. return 1;
  794. /*
  795.  * now that we know the sample rate, canonicalize the
  796.  * --skip string to a number of samples:
  797.  */
  798. flac__utils_canonicalize_skip_until_specification(&options.common.skip_specification, options.sample_rate);
  799. FLAC__ASSERT(options.common.skip_specification.value.samples >= 0);
  800. encoder_session.skip = (FLAC__uint64)options.common.skip_specification.value.samples;
  801. FLAC__ASSERT(!options.common.sector_align || encoder_session.skip == 0);
  802. if(infilesize < 0)
  803. total_samples_in_input = 0;
  804. else {
  805. /* *options.common.align_reservoir_samples will be 0 unless --sector-align is used */
  806. FLAC__ASSERT(options.common.sector_align || *options.common.align_reservoir_samples == 0);
  807. total_samples_in_input = (unsigned)infilesize / bytes_per_wide_sample + *options.common.align_reservoir_samples;
  808. }
  809. /*
  810.  * now that we know the input size, canonicalize the
  811.  * --until strings to a number of samples:
  812.  */
  813. if(!canonicalize_until_specification(&options.common.until_specification, encoder_session.inbasefilename, options.sample_rate, encoder_session.skip, total_samples_in_input))
  814. return EncoderSession_finish_error(&encoder_session);
  815. encoder_session.until = (FLAC__uint64)options.common.until_specification.value.samples;
  816. FLAC__ASSERT(!options.common.sector_align || encoder_session.until == 0);
  817. encoder_session.total_samples_to_encode = total_samples_in_input - encoder_session.skip;
  818. if(encoder_session.until > 0) {
  819. const FLAC__uint64 trim = total_samples_in_input - encoder_session.until;
  820. FLAC__ASSERT(total_samples_in_input > 0);
  821. FLAC__ASSERT(!options.common.sector_align);
  822. encoder_session.total_samples_to_encode -= trim;
  823. }
  824. if(infilesize >= 0 && options.common.sector_align) {
  825. FLAC__ASSERT(encoder_session.skip == 0);
  826. align_remainder = (unsigned)(encoder_session.total_samples_to_encode % 588);
  827. if(options.common.is_last_file)
  828. encoder_session.total_samples_to_encode += (588-align_remainder); /* will pad with zeroes */
  829. else
  830. encoder_session.total_samples_to_encode -= align_remainder; /* will stop short and carry over to next file */
  831. }
  832. encoder_session.unencoded_size = encoder_session.total_samples_to_encode * bytes_per_wide_sample;
  833. if(encoder_session.total_samples_to_encode <= 0)
  834. flac__utils_printf(stderr, 2, "(No runtime statistics possible; please wait for encoding to finish...)n");
  835. if(encoder_session.skip > 0) {
  836. unsigned skip_bytes = bytes_per_wide_sample * (unsigned)encoder_session.skip;
  837. if(skip_bytes > lookahead_length) {
  838. skip_bytes -= lookahead_length;
  839. lookahead_length = 0;
  840. if(!fskip_ahead(infile, skip_bytes)) {
  841. flac__utils_printf(stderr, 1, "%s: ERROR during read while skipping samplesn", encoder_session.inbasefilename);
  842. return EncoderSession_finish_error(&encoder_session);
  843. }
  844. }
  845. else {
  846. lookahead += skip_bytes;
  847. lookahead_length -= skip_bytes;
  848. }
  849. }
  850. if(!EncoderSession_init_encoder(&encoder_session, options.common, options.channels, options.bps, options.sample_rate))
  851. return EncoderSession_finish_error(&encoder_session);
  852. /*
  853.  * first do any samples in the reservoir
  854.  */
  855. if(options.common.sector_align && *options.common.align_reservoir_samples > 0) {
  856. if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)options.common.align_reservoir, *options.common.align_reservoir_samples)) {
  857. print_error_with_state(&encoder_session, "ERROR during encoding");
  858. return EncoderSession_finish_error(&encoder_session);
  859. }
  860. }
  861. /*
  862.  * decrement infilesize if we need to align the file
  863.  */
  864. if(options.common.sector_align) {
  865. FLAC__ASSERT(infilesize >= 0);
  866. if(options.common.is_last_file) {
  867. *options.common.align_reservoir_samples = 0;
  868. }
  869. else {
  870. *options.common.align_reservoir_samples = align_remainder;
  871. infilesize -= (long)((*options.common.align_reservoir_samples) * bytes_per_wide_sample);
  872. FLAC__ASSERT(infilesize >= 0);
  873. }
  874. }
  875. /*
  876.  * now do from the file
  877.  */
  878. if(infilesize < 0) {
  879. while(!feof(infile)) {
  880. if(lookahead_length > 0) {
  881. FLAC__ASSERT(lookahead_length < CHUNK_OF_SAMPLES * bytes_per_wide_sample);
  882. memcpy(ucbuffer_, lookahead, lookahead_length);
  883. bytes_read = fread(ucbuffer_+lookahead_length, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample - lookahead_length, infile) + lookahead_length;
  884. if(ferror(infile)) {
  885. flac__utils_printf(stderr, 1, "%s: ERROR during readn", encoder_session.inbasefilename);
  886. return EncoderSession_finish_error(&encoder_session);
  887. }
  888. lookahead_length = 0;
  889. }
  890. else
  891. bytes_read = fread(ucbuffer_, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample, infile);
  892. if(bytes_read == 0) {
  893. if(ferror(infile)) {
  894. flac__utils_printf(stderr, 1, "%s: ERROR during readn", encoder_session.inbasefilename);
  895. return EncoderSession_finish_error(&encoder_session);
  896. }
  897. }
  898. else if(bytes_read % bytes_per_wide_sample != 0) {
  899. flac__utils_printf(stderr, 1, "%s: ERROR: got partial samplen", encoder_session.inbasefilename);
  900. return EncoderSession_finish_error(&encoder_session);
  901. }
  902. else {
  903. unsigned wide_samples = bytes_read / bytes_per_wide_sample;
  904. format_input(input_, wide_samples, options.is_big_endian, options.is_unsigned_samples, options.channels, options.bps);
  905. if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
  906. print_error_with_state(&encoder_session, "ERROR during encoding");
  907. return EncoderSession_finish_error(&encoder_session);
  908. }
  909. }
  910. }
  911. }
  912. else {
  913. const FLAC__uint64 max_input_bytes = encoder_session.total_samples_to_encode * bytes_per_wide_sample;
  914. FLAC__uint64 total_input_bytes_read = 0;
  915. while(total_input_bytes_read < max_input_bytes) {
  916. {
  917. size_t wanted = (CHUNK_OF_SAMPLES * bytes_per_wide_sample);
  918. wanted = min(wanted, (size_t)(max_input_bytes - total_input_bytes_read));
  919. if(lookahead_length > 0) {
  920. FLAC__ASSERT(lookahead_length <= wanted);
  921. memcpy(ucbuffer_, lookahead, lookahead_length);
  922. wanted -= lookahead_length;
  923. bytes_read = lookahead_length;
  924. if(wanted > 0) {
  925. bytes_read += fread(ucbuffer_+lookahead_length, sizeof(unsigned char), wanted, infile);
  926. if(ferror(infile)) {
  927. flac__utils_printf(stderr, 1, "%s: ERROR during readn", encoder_session.inbasefilename);
  928. return EncoderSession_finish_error(&encoder_session);
  929. }
  930. }
  931. lookahead_length = 0;
  932. }
  933. else
  934. bytes_read = fread(ucbuffer_, sizeof(unsigned char), wanted, infile);
  935. }
  936. if(bytes_read == 0) {
  937. if(ferror(infile)) {
  938. flac__utils_printf(stderr, 1, "%s: ERROR during readn", encoder_session.inbasefilename);
  939. return EncoderSession_finish_error(&encoder_session);
  940. }
  941. else if(feof(infile)) {
  942. flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; expected %u samples, got %u samplesn", encoder_session.inbasefilename, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
  943. total_input_bytes_read = max_input_bytes;
  944. }
  945. }
  946. else {
  947. if(bytes_read % bytes_per_wide_sample != 0) {
  948. flac__utils_printf(stderr, 1, "%s: ERROR: got partial samplen", encoder_session.inbasefilename);
  949. return EncoderSession_finish_error(&encoder_session);
  950. }
  951. else {
  952. unsigned wide_samples = bytes_read / bytes_per_wide_sample;
  953. format_input(input_, wide_samples, options.is_big_endian, options.is_unsigned_samples, options.channels, options.bps);
  954. if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
  955. print_error_with_state(&encoder_session, "ERROR during encoding");
  956. return EncoderSession_finish_error(&encoder_session);
  957. }
  958. total_input_bytes_read += bytes_read;
  959. }
  960. }
  961. }
  962. }
  963. /*
  964.  * now read unaligned samples into reservoir or pad with zeroes if necessary
  965.  */
  966. if(options.common.sector_align) {
  967. if(options.common.is_last_file) {
  968. unsigned wide_samples = 588 - align_remainder;
  969. if(wide_samples < 588) {
  970. unsigned channel, data_bytes;
  971. info_align_zero = wide_samples;
  972. data_bytes = wide_samples * (options.bps >> 3);
  973. for(channel = 0; channel < options.channels; channel++)
  974. memset(input_[channel], 0, data_bytes);
  975. if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
  976. print_error_with_state(&encoder_session, "ERROR during encoding");
  977. return EncoderSession_finish_error(&encoder_session);
  978. }
  979. }
  980. }
  981. else {
  982. if(*options.common.align_reservoir_samples > 0) {
  983. FLAC__ASSERT(CHUNK_OF_SAMPLES >= 588);
  984. bytes_read = fread(ucbuffer_, sizeof(unsigned char), (*options.common.align_reservoir_samples) * bytes_per_wide_sample, infile);
  985. if(bytes_read == 0 && ferror(infile)) {
  986. flac__utils_printf(stderr, 1, "%s: ERROR during readn", encoder_session.inbasefilename);
  987. return EncoderSession_finish_error(&encoder_session);
  988. }
  989. else if(bytes_read != (*options.common.align_reservoir_samples) * bytes_per_wide_sample) {
  990. flac__utils_printf(stderr, 1, "%s: WARNING: unexpected EOF; read %u bytes; expected %u samples, got %u samplesn", encoder_session.inbasefilename, (unsigned)bytes_read, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
  991. }
  992. else {
  993. info_align_carry = *options.common.align_reservoir_samples;
  994. format_input(options.common.align_reservoir, *options.common.align_reservoir_samples, false, options.is_unsigned_samples, options.channels, options.bps);
  995. }
  996. }
  997. }
  998. }
  999. return EncoderSession_finish_ok(&encoder_session, info_align_carry, info_align_zero);
  1000. }
  1001. FLAC__bool EncoderSession_construct(EncoderSession *e, FLAC__bool use_ogg, FLAC__bool verify, FILE *infile, const char *infilename, const char *outfilename)
  1002. {
  1003. unsigned i;
  1004. FLAC__uint32 test = 1;
  1005. /*
  1006.  * initialize globals
  1007.  */
  1008. is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
  1009. for(i = 0; i < FLAC__MAX_CHANNELS; i++)
  1010. input_[i] = &(in_[i][0]);
  1011. /*
  1012.  * initialize instance
  1013.  */
  1014. #ifdef FLAC__HAS_OGG
  1015. e->use_ogg = use_ogg;
  1016. #else
  1017. (void)use_ogg;
  1018. #endif
  1019. e->verify = verify;
  1020. e->is_stdout = (0 == strcmp(outfilename, "-"));
  1021. e->inbasefilename = grabbag__file_get_basename(infilename);
  1022. e->outfilename = outfilename;
  1023. e->skip = 0; /* filled in later after the sample_rate is known */
  1024. e->unencoded_size = 0;
  1025. e->total_samples_to_encode = 0;
  1026. e->bytes_written = 0;
  1027. e->samples_written = 0;
  1028. e->blocksize = 0;
  1029. e->stats_mask = 0;
  1030. e->encoder.flac.stream = 0;
  1031. e->encoder.flac.file = 0;
  1032. #ifdef FLAC__HAS_OGG
  1033. e->encoder.ogg.stream = 0;
  1034. e->encoder.ogg.file = 0;
  1035. #endif
  1036. e->fin = infile;
  1037. e->fout = 0;
  1038. e->seek_table_template = 0;
  1039. if(e->is_stdout) {
  1040. e->fout = grabbag__file_get_binary_stdout();
  1041. }
  1042. if(0 == (e->seek_table_template = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE))) {
  1043. flac__utils_printf(stderr, 1, "%s: ERROR allocating memory for seek tablen", e->inbasefilename);
  1044. return false;
  1045. }
  1046. #ifdef FLAC__HAS_OGG
  1047. if(e->use_ogg) {
  1048. if(e->is_stdout) {
  1049. e->encoder.ogg.stream = OggFLAC__stream_encoder_new();
  1050. if(0 == e->encoder.ogg.stream) {
  1051. flac__utils_printf(stderr, 1, "%s: ERROR creating the encoder instancen", e->inbasefilename);
  1052. EncoderSession_destroy(e);
  1053. return false;
  1054. }
  1055. }
  1056. else {
  1057. e->encoder.ogg.file = OggFLAC__file_encoder_new();
  1058. if(0 == e->encoder.ogg.file) {
  1059. flac__utils_printf(stderr, 1, "%s: ERROR creating the encoder instancen", e->inbasefilename);
  1060. EncoderSession_destroy(e);
  1061. return false;
  1062. }
  1063. }
  1064. }
  1065. else
  1066. #endif
  1067. if(e->is_stdout) {
  1068. e->encoder.flac.stream = FLAC__stream_encoder_new();
  1069. if(0 == e->encoder.flac.stream) {
  1070. flac__utils_printf(stderr, 1, "%s: ERROR creating the encoder instancen", e->inbasefilename);
  1071. EncoderSession_destroy(e);
  1072. return false;
  1073. }
  1074. }
  1075. else {
  1076. e->encoder.flac.file = FLAC__file_encoder_new();
  1077. if(0 == e->encoder.flac.file) {
  1078. flac__utils_printf(stderr, 1, "%s: ERROR creating the encoder instancen", e->inbasefilename);
  1079. EncoderSession_destroy(e);
  1080. return false;
  1081. }
  1082. }
  1083. return true;
  1084. }
  1085. void EncoderSession_destroy(EncoderSession *e)
  1086. {
  1087. if(e->fin != stdin)
  1088. fclose(e->fin);
  1089. if(0 != e->fout && e->fout != stdout)
  1090. fclose(e->fout);
  1091. #ifdef FLAC__HAS_OGG
  1092. if(e->use_ogg) {
  1093. if(e->is_stdout) {
  1094. if(0 != e->encoder.ogg.stream) {
  1095. OggFLAC__stream_encoder_delete(e->encoder.ogg.stream);
  1096. e->encoder.ogg.stream = 0;
  1097. }
  1098. }
  1099. else {
  1100. if(0 != e->encoder.ogg.file) {
  1101. OggFLAC__file_encoder_delete(e->encoder.ogg.file);
  1102. e->encoder.ogg.file = 0;
  1103. }
  1104. }
  1105. }
  1106. else
  1107. #endif
  1108. if(e->is_stdout) {
  1109. if(0 != e->encoder.flac.stream) {
  1110. FLAC__stream_encoder_delete(e->encoder.flac.stream);
  1111. e->encoder.flac.stream = 0;
  1112. }
  1113. }
  1114. else {
  1115. if(0 != e->encoder.flac.file) {
  1116. FLAC__file_encoder_delete(e->encoder.flac.file);
  1117. e->encoder.flac.file = 0;
  1118. }
  1119. }
  1120. if(0 != e->seek_table_template) {
  1121. FLAC__metadata_object_delete(e->seek_table_template);
  1122. e->seek_table_template = 0;
  1123. }
  1124. }
  1125. int EncoderSession_finish_ok(EncoderSession *e, int info_align_carry, int info_align_zero)
  1126. {
  1127. FLAC__StreamEncoderState fse_state = FLAC__STREAM_ENCODER_OK;
  1128. int ret = 0;
  1129. #ifdef FLAC__HAS_OGG
  1130. if(e->use_ogg) {
  1131. if(e->is_stdout) {
  1132. if(e->encoder.ogg.stream) {
  1133. fse_state = OggFLAC__stream_encoder_get_FLAC_stream_encoder_state(e->encoder.ogg.stream);
  1134. OggFLAC__stream_encoder_finish(e->encoder.ogg.stream);
  1135. }
  1136. }
  1137. else {
  1138. if(e->encoder.ogg.file) {
  1139. fse_state = OggFLAC__file_encoder_get_FLAC_stream_encoder_state(e->encoder.ogg.file);
  1140. OggFLAC__file_encoder_finish(e->encoder.ogg.file);
  1141. }
  1142. }
  1143. }
  1144. else
  1145. #endif
  1146. if(e->is_stdout) {
  1147. if(e->encoder.flac.stream) {
  1148. fse_state = FLAC__stream_encoder_get_state(e->encoder.flac.stream);
  1149. FLAC__stream_encoder_finish(e->encoder.flac.stream);
  1150. }
  1151. }
  1152. else {
  1153. if(e->encoder.flac.file) {
  1154. fse_state = FLAC__file_encoder_get_stream_encoder_state(e->encoder.flac.file);
  1155. FLAC__file_encoder_finish(e->encoder.flac.file);
  1156. }
  1157. }
  1158. if(e->total_samples_to_encode > 0) {
  1159. print_stats(e);
  1160. flac__utils_printf(stderr, 2, "n");
  1161. }
  1162. if(fse_state == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA) {
  1163. print_verify_error(e);
  1164. ret = 1;
  1165. }
  1166. else {
  1167. if(info_align_carry >= 0) {
  1168. flac__utils_printf(stderr, 1, "%s: INFO: sector alignment causing %d samples to be carried overn", e->inbasefilename, info_align_carry);
  1169. }
  1170. if(info_align_zero >= 0) {
  1171. flac__utils_printf(stderr, 1, "%s: INFO: sector alignment causing %d zero samples to be appendedn", e->inbasefilename, info_align_zero);
  1172. }
  1173. }
  1174. EncoderSession_destroy(e);
  1175. return ret;
  1176. }
  1177. int EncoderSession_finish_error(EncoderSession *e)
  1178. {
  1179. FLAC__StreamEncoderState fse_state;
  1180. if(e->total_samples_to_encode > 0)
  1181. flac__utils_printf(stderr, 2, "n");
  1182. #ifdef FLAC__HAS_OGG
  1183. if(e->use_ogg) {
  1184. if(e->is_stdout) {
  1185. fse_state = OggFLAC__stream_encoder_get_FLAC_stream_encoder_state(e->encoder.ogg.stream);
  1186. }
  1187. else {
  1188. fse_state = OggFLAC__file_encoder_get_FLAC_stream_encoder_state(e->encoder.ogg.file);
  1189. }
  1190. }
  1191. else
  1192. #endif
  1193. if(e->is_stdout) {
  1194. fse_state = FLAC__stream_encoder_get_state(e->encoder.flac.stream);
  1195. }
  1196. else {
  1197. fse_state = FLAC__file_encoder_get_stream_encoder_state(e->encoder.flac.file);
  1198. }
  1199. if(fse_state == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
  1200. print_verify_error(e);
  1201. else
  1202. unlink(e->outfilename);
  1203. EncoderSession_destroy(e);
  1204. return 1;
  1205. }
  1206. FLAC__bool EncoderSession_init_encoder(EncoderSession *e, encode_options_t options, unsigned channels, unsigned bps, unsigned sample_rate)
  1207. {
  1208. unsigned num_metadata;
  1209. FLAC__StreamMetadata padding, *cuesheet = 0;
  1210. FLAC__StreamMetadata *metadata[4];
  1211. const FLAC__bool is_cdda = (channels == 1 || channels == 2) && (bps == 16) && (sample_rate == 44100);
  1212. e->replay_gain = options.replay_gain;
  1213. e->channels = channels;
  1214. e->bits_per_sample = bps;
  1215. e->sample_rate = sample_rate;
  1216. if(e->replay_gain) {
  1217. if(channels != 1 && channels != 2) {
  1218. flac__utils_printf(stderr, 1, "%s: ERROR, number of channels (%u) must be 1 or 2 for --replay-gainn", e->inbasefilename, channels);
  1219. return false;
  1220. }
  1221. if(!grabbag__replaygain_is_valid_sample_frequency(sample_rate)) {
  1222. flac__utils_printf(stderr, 1, "%s: ERROR, invalid sample rate (%u) for --replay-gainn", e->inbasefilename, sample_rate);
  1223. return false;
  1224. }
  1225. if(options.is_first_file) {
  1226. if(!grabbag__replaygain_init(sample_rate)) {
  1227. flac__utils_printf(stderr, 1, "%s: ERROR initializing ReplayGain stagen", e->inbasefilename);
  1228. return false;
  1229. }
  1230. }
  1231. }
  1232. if(channels != 2)
  1233. options.do_mid_side = options.loose_mid_side = false;
  1234. if(!parse_cuesheet_(&cuesheet, options.cuesheet_filename, e->inbasefilename, is_cdda, e->total_samples_to_encode))
  1235. return false;
  1236. if(!convert_to_seek_table_template(options.requested_seek_points, options.num_requested_seek_points, options.cued_seekpoints? cuesheet : 0, e)) {
  1237. flac__utils_printf(stderr, 1, "%s: ERROR allocating memory for seek tablen", e->inbasefilename);
  1238. if(0 != cuesheet)
  1239. FLAC__metadata_object_delete(cuesheet);
  1240. return false;
  1241. }
  1242. num_metadata = 0;
  1243. if(e->seek_table_template->data.seek_table.num_points > 0) {
  1244. e->seek_table_template->is_last = false; /* the encoder will set this for us */
  1245. metadata[num_metadata++] = e->seek_table_template;
  1246. }
  1247. if(0 != cuesheet)
  1248. metadata[num_metadata++] = cuesheet;
  1249. metadata[num_metadata++] = options.vorbis_comment;
  1250. if(options.padding > 0) {
  1251. padding.is_last = false; /* the encoder will set this for us */
  1252. padding.type = FLAC__METADATA_TYPE_PADDING;
  1253. padding.length = (unsigned)options.padding;
  1254. metadata[num_metadata++] = &padding;
  1255. }
  1256. e->blocksize = options.blocksize;
  1257. e->stats_mask = (options.do_exhaustive_model_search || options.do_qlp_coeff_prec_search)? 0x0f : 0x3f;
  1258. #ifdef FLAC__HAS_OGG
  1259. if(e->use_ogg) {
  1260. if(e->is_stdout) {
  1261. OggFLAC__stream_encoder_set_serial_number(e->encoder.ogg.stream, options.serial_number);
  1262. OggFLAC__stream_encoder_set_verify(e->encoder.ogg.stream, options.verify);
  1263. OggFLAC__stream_encoder_set_streamable_subset(e->encoder.ogg.stream, !options.lax);
  1264. OggFLAC__stream_encoder_set_do_mid_side_stereo(e->encoder.ogg.stream, options.do_mid_side);
  1265. OggFLAC__stream_encoder_set_loose_mid_side_stereo(e->encoder.ogg.stream, options.loose_mid_side);
  1266. OggFLAC__stream_encoder_set_channels(e->encoder.ogg.stream, channels);
  1267. OggFLAC__stream_encoder_set_bits_per_sample(e->encoder.ogg.stream, bps);
  1268. OggFLAC__stream_encoder_set_sample_rate(e->encoder.ogg.stream, sample_rate);
  1269. OggFLAC__stream_encoder_set_blocksize(e->encoder.ogg.stream, options.blocksize);
  1270. OggFLAC__stream_encoder_set_max_lpc_order(e->encoder.ogg.stream, options.max_lpc_order);
  1271. OggFLAC__stream_encoder_set_qlp_coeff_precision(e->encoder.ogg.stream, options.qlp_coeff_precision);
  1272. OggFLAC__stream_encoder_set_do_qlp_coeff_prec_search(e->encoder.ogg.stream, options.do_qlp_coeff_prec_search);
  1273. OggFLAC__stream_encoder_set_do_escape_coding(e->encoder.ogg.stream, options.do_escape_coding);
  1274. OggFLAC__stream_encoder_set_do_exhaustive_model_search(e->encoder.ogg.stream, options.do_exhaustive_model_search);
  1275. OggFLAC__stream_encoder_set_min_residual_partition_order(e->encoder.ogg.stream, options.min_residual_partition_order);
  1276. OggFLAC__stream_encoder_set_max_residual_partition_order(e->encoder.ogg.stream, options.max_residual_partition_order);
  1277. OggFLAC__stream_encoder_set_rice_parameter_search_dist(e->encoder.ogg.stream, options.rice_parameter_search_dist);
  1278. OggFLAC__stream_encoder_set_total_samples_estimate(e->encoder.ogg.stream, e->total_samples_to_encode);
  1279. OggFLAC__stream_encoder_set_metadata(e->encoder.ogg.stream, (num_metadata > 0)? metadata : 0, num_metadata);
  1280. OggFLAC__stream_encoder_set_write_callback(e->encoder.ogg.stream, ogg_stream_encoder_write_callback);
  1281. OggFLAC__stream_encoder_set_metadata_callback(e->encoder.ogg.stream, ogg_stream_encoder_metadata_callback);
  1282. OggFLAC__stream_encoder_set_client_data(e->encoder.ogg.stream, e);
  1283. OggFLAC__stream_encoder_disable_constant_subframes(e->encoder.ogg.stream, options.debug.disable_constant_subframes);
  1284. OggFLAC__stream_encoder_disable_fixed_subframes(e->encoder.ogg.stream, options.debug.disable_fixed_subframes);
  1285. OggFLAC__stream_encoder_disable_verbatim_subframes(e->encoder.ogg.stream, options.debug.disable_verbatim_subframes);
  1286. if(OggFLAC__stream_encoder_init(e->encoder.ogg.stream) != FLAC__STREAM_ENCODER_OK) {
  1287. print_error_with_state(e, "ERROR initializing encoder");
  1288. if(0 != cuesheet)
  1289. FLAC__metadata_object_delete(cuesheet);
  1290. return false;
  1291. }
  1292. }
  1293. else {
  1294. OggFLAC__file_encoder_set_serial_number(e->encoder.ogg.file, options.serial_number);
  1295. OggFLAC__file_encoder_set_filename(e->encoder.ogg.file, e->outfilename);
  1296. OggFLAC__file_encoder_set_verify(e->encoder.ogg.file, options.verify);
  1297. OggFLAC__file_encoder_set_streamable_subset(e->encoder.ogg.file, !options.lax);
  1298. OggFLAC__file_encoder_set_do_mid_side_stereo(e->encoder.ogg.file, options.do_mid_side);
  1299. OggFLAC__file_encoder_set_loose_mid_side_stereo(e->encoder.ogg.file, options.loose_mid_side);
  1300. OggFLAC__file_encoder_set_channels(e->encoder.ogg.file, channels);
  1301. OggFLAC__file_encoder_set_bits_per_sample(e->encoder.ogg.file, bps);
  1302. OggFLAC__file_encoder_set_sample_rate(e->encoder.ogg.file, sample_rate);
  1303. OggFLAC__file_encoder_set_blocksize(e->encoder.ogg.file, options.blocksize);
  1304. OggFLAC__file_encoder_set_max_lpc_order(e->encoder.ogg.file, options.max_lpc_order);
  1305. OggFLAC__file_encoder_set_qlp_coeff_precision(e->encoder.ogg.file, options.qlp_coeff_precision);
  1306. OggFLAC__file_encoder_set_do_qlp_coeff_prec_search(e->encoder.ogg.file, options.do_qlp_coeff_prec_search);
  1307. OggFLAC__file_encoder_set_do_escape_coding(e->encoder.ogg.file, options.do_escape_coding);
  1308. OggFLAC__file_encoder_set_do_exhaustive_model_search(e->encoder.ogg.file, options.do_exhaustive_model_search);
  1309. OggFLAC__file_encoder_set_min_residual_partition_order(e->encoder.ogg.file, options.min_residual_partition_order);
  1310. OggFLAC__file_encoder_set_max_residual_partition_order(e->encoder.ogg.file, options.max_residual_partition_order);
  1311. OggFLAC__file_encoder_set_rice_parameter_search_dist(e->encoder.ogg.file, options.rice_parameter_search_dist);
  1312. OggFLAC__file_encoder_set_total_samples_estimate(e->encoder.ogg.file, e->total_samples_to_encode);
  1313. OggFLAC__file_encoder_set_metadata(e->encoder.ogg.file, (num_metadata > 0)? metadata : 0, num_metadata);
  1314. OggFLAC__file_encoder_set_progress_callback(e->encoder.ogg.file, ogg_file_encoder_progress_callback);
  1315. OggFLAC__file_encoder_set_client_data(e->encoder.ogg.file, e);
  1316. OggFLAC__file_encoder_disable_constant_subframes(e->encoder.ogg.file, options.debug.disable_constant_subframes);
  1317. OggFLAC__file_encoder_disable_fixed_subframes(e->encoder.ogg.file, options.debug.disable_fixed_subframes);
  1318. OggFLAC__file_encoder_disable_verbatim_subframes(e->encoder.ogg.file, options.debug.disable_verbatim_subframes);
  1319. if(OggFLAC__file_encoder_init(e->encoder.ogg.file) != OggFLAC__FILE_ENCODER_OK) {
  1320. print_error_with_state(e, "ERROR initializing encoder");
  1321. if(0 != cuesheet)
  1322. FLAC__metadata_object_delete(cuesheet);
  1323. return false;
  1324. }
  1325. }
  1326. }
  1327. else
  1328. #endif
  1329. if(e->is_stdout) {
  1330. FLAC__stream_encoder_set_verify(e->encoder.flac.stream, options.verify);
  1331. FLAC__stream_encoder_set_streamable_subset(e->encoder.flac.stream, !options.lax);
  1332. FLAC__stream_encoder_set_do_mid_side_stereo(e->encoder.flac.stream, options.do_mid_side);
  1333. FLAC__stream_encoder_set_loose_mid_side_stereo(e->encoder.flac.stream, options.loose_mid_side);
  1334. FLAC__stream_encoder_set_channels(e->encoder.flac.stream, channels);
  1335. FLAC__stream_encoder_set_bits_per_sample(e->encoder.flac.stream, bps);
  1336. FLAC__stream_encoder_set_sample_rate(e->encoder.flac.stream, sample_rate);
  1337. FLAC__stream_encoder_set_blocksize(e->encoder.flac.stream, options.blocksize);
  1338. FLAC__stream_encoder_set_max_lpc_order(e->encoder.flac.stream, options.max_lpc_order);
  1339. FLAC__stream_encoder_set_qlp_coeff_precision(e->encoder.flac.stream, options.qlp_coeff_precision);
  1340. FLAC__stream_encoder_set_do_qlp_coeff_prec_search(e->encoder.flac.stream, options.do_qlp_coeff_prec_search);
  1341. FLAC__stream_encoder_set_do_escape_coding(e->encoder.flac.stream, options.do_escape_coding);
  1342. FLAC__stream_encoder_set_do_exhaustive_model_search(e->encoder.flac.stream, options.do_exhaustive_model_search);
  1343. FLAC__stream_encoder_set_min_residual_partition_order(e->encoder.flac.stream, options.min_residual_partition_order);
  1344. FLAC__stream_encoder_set_max_residual_partition_order(e->encoder.flac.stream, options.max_residual_partition_order);
  1345. FLAC__stream_encoder_set_rice_parameter_search_dist(e->encoder.flac.stream, options.rice_parameter_search_dist);
  1346. FLAC__stream_encoder_set_total_samples_estimate(e->encoder.flac.stream, e->total_samples_to_encode);
  1347. FLAC__stream_encoder_set_metadata(e->encoder.flac.stream, (num_metadata > 0)? metadata : 0, num_metadata);
  1348. FLAC__stream_encoder_set_write_callback(e->encoder.flac.stream, flac_stream_encoder_write_callback);
  1349. FLAC__stream_encoder_set_metadata_callback(e->encoder.flac.stream, flac_stream_encoder_metadata_callback);
  1350. FLAC__stream_encoder_set_client_data(e->encoder.flac.stream, e);
  1351. FLAC__stream_encoder_disable_constant_subframes(e->encoder.flac.stream, options.debug.disable_constant_subframes);
  1352. FLAC__stream_encoder_disable_fixed_subframes(e->encoder.flac.stream, options.debug.disable_fixed_subframes);
  1353. FLAC__stream_encoder_disable_verbatim_subframes(e->encoder.flac.stream, options.debug.disable_verbatim_subframes);
  1354. if(FLAC__stream_encoder_init(e->encoder.flac.stream) != FLAC__STREAM_ENCODER_OK) {
  1355. print_error_with_state(e, "ERROR initializing encoder");
  1356. if(0 != cuesheet)
  1357. FLAC__metadata_object_delete(cuesheet);
  1358. return false;
  1359. }
  1360. }
  1361. else {
  1362. FLAC__file_encoder_set_filename(e->encoder.flac.file, e->outfilename);
  1363. FLAC__file_encoder_set_verify(e->encoder.flac.file, options.verify);
  1364. FLAC__file_encoder_set_streamable_subset(e->encoder.flac.file, !options.lax);
  1365. FLAC__file_encoder_set_do_mid_side_stereo(e->encoder.flac.file, options.do_mid_side);
  1366. FLAC__file_encoder_set_loose_mid_side_stereo(e->encoder.flac.file, options.loose_mid_side);
  1367. FLAC__file_encoder_set_channels(e->encoder.flac.file, channels);
  1368. FLAC__file_encoder_set_bits_per_sample(e->encoder.flac.file, bps);
  1369. FLAC__file_encoder_set_sample_rate(e->encoder.flac.file, sample_rate);
  1370. FLAC__file_encoder_set_blocksize(e->encoder.flac.file, options.blocksize);
  1371. FLAC__file_encoder_set_max_lpc_order(e->encoder.flac.file, options.max_lpc_order);
  1372. FLAC__file_encoder_set_qlp_coeff_precision(e->encoder.flac.file, options.qlp_coeff_precision);
  1373. FLAC__file_encoder_set_do_qlp_coeff_prec_search(e->encoder.flac.file, options.do_qlp_coeff_prec_search);
  1374. FLAC__file_encoder_set_do_escape_coding(e->encoder.flac.file, options.do_escape_coding);
  1375. FLAC__file_encoder_set_do_exhaustive_model_search(e->encoder.flac.file, options.do_exhaustive_model_search);
  1376. FLAC__file_encoder_set_min_residual_partition_order(e->encoder.flac.file, options.min_residual_partition_order);
  1377. FLAC__file_encoder_set_max_residual_partition_order(e->encoder.flac.file, options.max_residual_partition_order);
  1378. FLAC__file_encoder_set_rice_parameter_search_dist(e->encoder.flac.file, options.rice_parameter_search_dist);
  1379. FLAC__file_encoder_set_total_samples_estimate(e->encoder.flac.file, e->total_samples_to_encode);
  1380. FLAC__file_encoder_set_metadata(e->encoder.flac.file, (num_metadata > 0)? metadata : 0, num_metadata);
  1381. FLAC__file_encoder_set_progress_callback(e->encoder.flac.file, flac_file_encoder_progress_callback);
  1382. FLAC__file_encoder_set_client_data(e->encoder.flac.file, e);
  1383. FLAC__file_encoder_disable_constant_subframes(e->encoder.flac.file, options.debug.disable_constant_subframes);
  1384. FLAC__file_encoder_disable_fixed_subframes(e->encoder.flac.file, options.debug.disable_fixed_subframes);
  1385. FLAC__file_encoder_disable_verbatim_subframes(e->encoder.flac.file, options.debug.disable_verbatim_subframes);
  1386. if(FLAC__file_encoder_init(e->encoder.flac.file) != FLAC__FILE_ENCODER_OK) {
  1387. print_error_with_state(e, "ERROR initializing encoder");
  1388. if(0 != cuesheet)
  1389. FLAC__metadata_object_delete(cuesheet);
  1390. return false;
  1391. }
  1392. }
  1393. if(0 != cuesheet)
  1394. FLAC__metadata_object_delete(cuesheet);
  1395. return true;
  1396. }
  1397. FLAC__bool EncoderSession_process(EncoderSession *e, const FLAC__int32 * const buffer[], unsigned samples)
  1398. {
  1399. if(e->replay_gain) {
  1400. if(!grabbag__replaygain_analyze(buffer, e->channels==2, e->bits_per_sample, samples)) {
  1401. flac__utils_printf(stderr, 1, "%s: WARNING, error while calculating ReplayGainn", e->inbasefilename);
  1402. }
  1403. }
  1404. #ifdef FLAC__HAS_OGG
  1405. if(e->use_ogg) {
  1406. if(e->is_stdout) {
  1407. return OggFLAC__stream_encoder_process(e->encoder.ogg.stream, buffer, samples);
  1408. }
  1409. else {
  1410. return OggFLAC__file_encoder_process(e->encoder.ogg.file, buffer, samples);
  1411. }
  1412. }
  1413. else
  1414. #endif
  1415. if(e->is_stdout) {
  1416. return FLAC__stream_encoder_process(e->encoder.flac.stream, buffer, samples);
  1417. }
  1418. else {
  1419. return FLAC__file_encoder_process(e->encoder.flac.file, buffer, samples);
  1420. }
  1421. }
  1422. FLAC__bool convert_to_seek_table_template(const char *requested_seek_points, int num_requested_seek_points, FLAC__StreamMetadata *cuesheet, EncoderSession *e)
  1423. {
  1424. const FLAC__bool only_placeholders = e->is_stdout;
  1425. FLAC__bool has_real_points;
  1426. if(num_requested_seek_points == 0 && 0 == cuesheet)
  1427. return true;
  1428. if(num_requested_seek_points < 0) {
  1429. requested_seek_points = "10s;";
  1430. num_requested_seek_points = 1;
  1431. }
  1432. if(num_requested_seek_points > 0) {
  1433. if(!grabbag__seektable_convert_specification_to_template(requested_seek_points, only_placeholders, e->total_samples_to_encode, e->sample_rate, e->seek_table_template, &has_real_points))
  1434. return false;
  1435. }
  1436. if(0 != cuesheet) {
  1437. unsigned i, j;
  1438. const FLAC__StreamMetadata_CueSheet *cs = &cuesheet->data.cue_sheet;
  1439. for(i = 0; i < cs->num_tracks; i++) {
  1440. const FLAC__StreamMetadata_CueSheet_Track *tr = cs->tracks+i;
  1441. for(j = 0; j < tr->num_indices; j++) {
  1442. if(!FLAC__metadata_object_seektable_template_append_point(e->seek_table_template, tr->offset + tr->indices[j].offset))
  1443. return false;
  1444. has_real_points = true;
  1445. }
  1446. }
  1447. if(has_real_points)
  1448. if(!FLAC__metadata_object_seektable_template_sort(e->seek_table_template, /*compact=*/true))
  1449. return false;
  1450. }
  1451. if(has_real_points) {
  1452. if(e->is_stdout) {
  1453. flac__utils_printf(stderr, 1, "%s: WARNING, cannot write back seekpoints when encoding to stdoutn", e->inbasefilename);
  1454. }
  1455. }
  1456. return true;
  1457. }
  1458. FLAC__bool canonicalize_until_specification(utils__SkipUntilSpecification *spec, const char *inbasefilename, unsigned sample_rate, FLAC__uint64 skip, FLAC__uint64 total_samples_in_input)
  1459. {
  1460. /* convert from mm:ss.sss to sample number if necessary */
  1461. flac__utils_canonicalize_skip_until_specification(spec, sample_rate);
  1462. /* special case: if "--until=-0", use the special value '0' to mean "end-of-stream" */
  1463. if(spec->is_relative && spec->value.samples == 0) {
  1464. spec->is_relative = false;
  1465. return true;
  1466. }
  1467. /* in any other case the total samples in the input must be known */
  1468. if(total_samples_in_input == 0) {
  1469. flac__utils_printf(stderr, 1, "%s: ERROR, cannot use --until when input length is unknownn", inbasefilename);
  1470. return false;
  1471. }
  1472. FLAC__ASSERT(spec->value_is_samples);
  1473. /* convert relative specifications to absolute */
  1474. if(spec->is_relative) {
  1475. if(spec->value.samples <= 0)
  1476. spec->value.samples += (FLAC__int64)total_samples_in_input;
  1477. else
  1478. spec->value.samples += skip;
  1479. spec->is_relative = false;
  1480. }
  1481. /* error check */
  1482. if(spec->value.samples < 0) {
  1483. flac__utils_printf(stderr, 1, "%s: ERROR, --until value is before beginning of inputn", inbasefilename);
  1484. return false;
  1485. }
  1486. if((FLAC__uint64)spec->value.samples <= skip) {
  1487. flac__utils_printf(stderr, 1, "%s: ERROR, --until value is before --skip pointn", inbasefilename);
  1488. return false;
  1489. }
  1490. if((FLAC__uint64)spec->value.samples > total_samples_in_input) {
  1491. flac__utils_printf(stderr, 1, "%s: ERROR, --until value is after end of inputn", inbasefilename);
  1492. return false;
  1493. }
  1494. return true;
  1495. }
  1496. void format_input(FLAC__int32 *dest[], unsigned wide_samples, FLAC__bool is_big_endian, FLAC__bool is_unsigned_samples, unsigned channels, unsigned bps)
  1497. {
  1498. unsigned wide_sample, sample, channel, byte;
  1499. if(bps == 8) {
  1500. if(is_unsigned_samples) {
  1501. for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
  1502. for(channel = 0; channel < channels; channel++, sample++)
  1503. dest[channel][wide_sample] = (FLAC__int32)ucbuffer_[sample] - 0x80;
  1504. }
  1505. else {
  1506. for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
  1507. for(channel = 0; channel < channels; channel++, sample++)
  1508. dest[channel][wide_sample] = (FLAC__int32)scbuffer_[sample];
  1509. }
  1510. }
  1511. else if(bps == 16) {
  1512. if(is_big_endian != is_big_endian_host_) {
  1513. unsigned char tmp;
  1514. const unsigned bytes = wide_samples * channels * (bps >> 3);
  1515. for(byte = 0; byte < bytes; byte += 2) {
  1516. tmp = ucbuffer_[byte];
  1517. ucbuffer_[byte] = ucbuffer_[byte+1];
  1518. ucbuffer_[byte+1] = tmp;
  1519. }
  1520. }
  1521. if(is_unsigned_samples) {
  1522. for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
  1523. for(channel = 0; channel < channels; channel++, sample++)
  1524. dest[channel][wide_sample] = (FLAC__int32)usbuffer_[sample] - 0x8000;
  1525. }
  1526. else {
  1527. for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
  1528. for(channel = 0; channel < channels; channel++, sample++)
  1529. dest[channel][wide_sample] = (FLAC__int32)ssbuffer_[sample];
  1530. }
  1531. }
  1532. else if(bps == 24) {
  1533. if(!is_big_endian) {
  1534. unsigned char tmp;
  1535. const unsigned bytes = wide_samples * channels * (bps >> 3);
  1536. for(byte = 0; byte < bytes; byte += 3) {
  1537. tmp = ucbuffer_[byte];
  1538. ucbuffer_[byte] = ucbuffer_[byte+2];
  1539. ucbuffer_[byte+2] = tmp;
  1540. }
  1541. }
  1542. if(is_unsigned_samples) {
  1543. for(byte = sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
  1544. for(channel = 0; channel < channels; channel++, sample++) {
  1545. dest[channel][wide_sample]  = ucbuffer_[byte++]; dest[channel][wide_sample] <<= 8;
  1546. dest[channel][wide_sample] |= ucbuffer_[byte++]; dest[channel][wide_sample] <<= 8;
  1547. dest[channel][wide_sample] |= ucbuffer_[byte++];
  1548. dest[channel][wide_sample] -= 0x800000;
  1549. }
  1550. }
  1551. else {
  1552. for(byte = sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
  1553. for(channel = 0; channel < channels; channel++, sample++) {
  1554. dest[channel][wide_sample]  = scbuffer_[byte++]; dest[channel][wide_sample] <<= 8;
  1555. dest[channel][wide_sample] |= ucbuffer_[byte++]; dest[channel][wide_sample] <<= 8;
  1556. dest[channel][wide_sample] |= ucbuffer_[byte++];
  1557. }
  1558. }
  1559. }
  1560. else {
  1561. FLAC__ASSERT(0);
  1562. }
  1563. }
  1564. #ifdef FLAC__HAS_OGG
  1565. FLAC__StreamEncoderWriteStatus ogg_stream_encoder_write_callback(const OggFLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
  1566. {
  1567. EncoderSession *encoder_session = (EncoderSession*)client_data;
  1568. (void)encoder;
  1569. encoder_session->bytes_written += bytes;
  1570. /*
  1571.  * With Ogg FLAC we don't get one write callback per frame and
  1572.  * we don't have a good number for 'samples', so we estimate based
  1573.  * on the frame number and the knowledge that all blocks (except
  1574.  * the last) are the same size.
  1575.  */
  1576. (void)samples;
  1577. encoder_session->samples_written = (current_frame+1) * encoder_session->blocksize;
  1578. if(encoder_session->total_samples_to_encode > 0 && !(current_frame & encoder_session->stats_mask))
  1579. print_stats(encoder_session);
  1580. if(flac__utils_fwrite(buffer, sizeof(FLAC__byte), bytes, encoder_session->fout) == bytes)
  1581. return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
  1582. else
  1583. return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
  1584. }
  1585. void ogg_stream_encoder_metadata_callback(const OggFLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
  1586. {
  1587. // do nothing, for compatibilty.  soon we will be using the ogg file encoder anyway.
  1588. (void)encoder, (void)metadata, (void)client_data;
  1589. }
  1590. void ogg_file_encoder_progress_callback(const OggFLAC__FileEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data)
  1591. {
  1592. EncoderSession *encoder_session = (EncoderSession*)client_data;
  1593. (void)encoder;
  1594. /*
  1595.  * With Ogg FLAC we don't get a value for 'samples_written', so we
  1596.  * estimate based on the frames written and the knowledge that all
  1597.  * blocks (except the last) are the same size.
  1598.  */
  1599. samples_written = frames_written * encoder_session->blocksize;
  1600. flac_file_encoder_progress_callback(0, bytes_written, samples_written, frames_written, total_frames_estimate, client_data);
  1601. }
  1602. #endif
  1603. FLAC__StreamEncoderWriteStatus flac_stream_encoder_write_callback(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
  1604. {
  1605. EncoderSession *encoder_session = (EncoderSession*)client_data;
  1606. (void)encoder;
  1607. encoder_session->bytes_written += bytes;
  1608. encoder_session->samples_written += samples;
  1609. if(samples && encoder_session->total_samples_to_encode > 0 && !(current_frame & encoder_session->stats_mask))
  1610. print_stats(encoder_session);
  1611. if(flac__utils_fwrite(buffer, sizeof(FLAC__byte), bytes, encoder_session->fout) == bytes)
  1612. return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
  1613. else
  1614. return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
  1615. }
  1616. void flac_stream_encoder_metadata_callback(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
  1617. {
  1618. /*
  1619.  * Nothing to do; if we get here, we're decoding to stdout, in
  1620.  * which case we can't seek backwards to write new metadata.
  1621.  */
  1622. (void)encoder, (void)metadata, (void)client_data;
  1623. }
  1624. void flac_file_encoder_progress_callback(const FLAC__FileEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data)
  1625. {
  1626. EncoderSession *encoder_session = (EncoderSession*)client_data;
  1627. (void)encoder, (void)total_frames_estimate;
  1628. encoder_session->bytes_written = bytes_written;
  1629. encoder_session->samples_written = samples_written;
  1630. if(encoder_session->total_samples_to_encode > 0 && !((frames_written-1) & encoder_session->stats_mask))
  1631. print_stats(encoder_session);
  1632. }
  1633. FLAC__bool parse_cuesheet_(FLAC__StreamMetadata **cuesheet, const char *cuesheet_filename, const char *inbasefilename, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset)
  1634. {
  1635. FILE *f;
  1636. unsigned last_line_read;
  1637. const char *error_message;
  1638. if(0 == cuesheet_filename)
  1639. return true;
  1640. if(lead_out_offset == 0) {
  1641. flac__utils_printf(stderr, 1, "%s: ERROR cannot import cuesheet when the number of input samples to encode is unknownn", inbasefilename);
  1642. return false;
  1643. }
  1644. if(0 == (f = fopen(cuesheet_filename, "r"))) {
  1645. flac__utils_printf(stderr, 1, "%s: ERROR opening cuesheet "%s" for readingn", inbasefilename, cuesheet_filename);
  1646. return false;
  1647. }
  1648. *cuesheet = grabbag__cuesheet_parse(f, &error_message, &last_line_read, is_cdda, lead_out_offset);
  1649. fclose(f);
  1650. if(0 == *cuesheet) {
  1651. flac__utils_printf(stderr, 1, "%s: ERROR parsing cuesheet "%s" on line %u: %sn", inbasefilename, cuesheet_filename, last_line_read, error_message);
  1652. return false;
  1653. }
  1654. return true;
  1655. }
  1656. void print_stats(const EncoderSession *encoder_session)
  1657. {
  1658. const FLAC__uint64 samples_written = min(encoder_session->total_samples_to_encode, encoder_session->samples_written);
  1659. #if defined _MSC_VER || defined __MINGW32__
  1660. /* with MSVC you have to spoon feed it the casting */
  1661. const double progress = (double)(FLAC__int64)samples_written / (double)(FLAC__int64)encoder_session->total_samples_to_encode;
  1662. const double ratio = (double)(FLAC__int64)encoder_session->bytes_written / ((double)(FLAC__int64)encoder_session->unencoded_size * min(1.0, progress));
  1663. #else
  1664. const double progress = (double)samples_written / (double)encoder_session->total_samples_to_encode;
  1665. const double ratio = (double)encoder_session->bytes_written / ((double)encoder_session->unencoded_size * min(1.0, progress));
  1666. #endif
  1667. if(samples_written == encoder_session->total_samples_to_encode) {
  1668. flac__utils_printf(stderr, 2, "r%s:%s wrote %u bytes, ratio=%0.3f",
  1669. encoder_session->inbasefilename,
  1670. encoder_session->verify? " Verify OK," : "",
  1671. (unsigned)encoder_session->bytes_written,
  1672. ratio
  1673. );
  1674. }
  1675. else {
  1676. flac__utils_printf(stderr, 2, "r%s: %u%% complete, ratio=%0.3f", encoder_session->inbasefilename, (unsigned)floor(progress * 100.0 + 0.5), ratio);
  1677. }
  1678. }
  1679. void print_error_with_state(const EncoderSession *e, const char *message)
  1680. {
  1681. const int ilen = strlen(e->inbasefilename) + 1;
  1682. const char *state_string;
  1683. flac__utils_printf(stderr, 1, "n%s: %sn", e->inbasefilename, message);
  1684. #ifdef FLAC__HAS_OGG
  1685. if(e->use_ogg) {
  1686. if(e->is_stdout) {
  1687. state_string = OggFLAC__stream_encoder_get_resolved_state_string(e->encoder.ogg.stream);
  1688. }
  1689. else {
  1690. state_string = OggFLAC__file_encoder_get_resolved_state_string(e->encoder.ogg.file);
  1691. }
  1692. }
  1693. else
  1694. #endif
  1695. if(e->is_stdout) {
  1696. state_string = FLAC__stream_encoder_get_resolved_state_string(e->encoder.flac.stream);
  1697. }
  1698. else {
  1699. state_string = FLAC__file_encoder_get_resolved_state_string(e->encoder.flac.file);
  1700. }
  1701. flac__utils_printf(stderr, 1, "%*s state = %sn", ilen, "", state_string);
  1702. /* print out some more info for some errors: */
  1703. if(0 == strcmp(state_string, FLAC__StreamEncoderStateString[FLAC__STREAM_ENCODER_NOT_STREAMABLE])) {
  1704. flac__utils_printf(stderr, 1,
  1705. "n"
  1706. "The encoding parameters specified do not conform to the FLAC Subset and may notn"
  1707. "be streamable or playable in hardware devices.  Add --lax to the command-linen"
  1708. "options to encode with these parameters.n"
  1709. );
  1710. }
  1711. else if(
  1712. 0 == strcmp(state_string, FLAC__FileEncoderStateString[FLAC__FILE_ENCODER_FATAL_ERROR_WHILE_WRITING])
  1713. #ifdef FLAC__HAS_OGG
  1714. || 0 == strcmp(state_string, OggFLAC__FileEncoderStateString[OggFLAC__FILE_ENCODER_FATAL_ERROR_WHILE_WRITING])
  1715. #endif
  1716. ) {
  1717. flac__utils_printf(stderr, 1,
  1718. "n"
  1719. "An error occurred while writing; the most common cause is that the disk is full.n"
  1720. );
  1721. }
  1722. else if(
  1723. 0 == strcmp(state_string, FLAC__FileEncoderStateString[FLAC__FILE_ENCODER_ERROR_OPENING_FILE])
  1724. #ifdef FLAC__HAS_OGG
  1725. || 0 == strcmp(state_string, OggFLAC__FileEncoderStateString[OggFLAC__FILE_ENCODER_ERROR_OPENING_FILE])
  1726. #endif
  1727. ) {
  1728. flac__utils_printf(stderr, 1,
  1729. "n"
  1730. "An error occurred opening the output file; it is likely that the outputn"
  1731. "directory does not exist or is not writable, the output file already exists andn"
  1732. "is not writable, or the disk is full.n"
  1733. );
  1734. }
  1735. }
  1736. void print_verify_error(EncoderSession *e)
  1737. {
  1738. FLAC__uint64 absolute_sample;
  1739. unsigned frame_number;
  1740. unsigned channel;
  1741. unsigned sample;
  1742. FLAC__int32 expected;
  1743. FLAC__int32 got;
  1744. #ifdef FLAC__HAS_OGG
  1745. if(e->use_ogg) {
  1746. if(e->is_stdout) {
  1747. OggFLAC__stream_encoder_get_verify_decoder_error_stats(e->encoder.ogg.stream, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
  1748. }
  1749. else {
  1750. OggFLAC__file_encoder_get_verify_decoder_error_stats(e->encoder.ogg.file, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
  1751. }
  1752. }
  1753. else
  1754. #endif
  1755. if(e->is_stdout) {
  1756. FLAC__stream_encoder_get_verify_decoder_error_stats(e->encoder.flac.stream, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
  1757. }
  1758. else {
  1759. FLAC__file_encoder_get_verify_decoder_error_stats(e->encoder.flac.file, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
  1760. }
  1761. flac__utils_printf(stderr, 1, "%s: ERROR: mismatch in decoded data, verify FAILED!n", e->inbasefilename);
  1762. flac__utils_printf(stderr, 1, "       Absolute sample=%u, frame=%u, channel=%u, sample=%u, expected %d, got %dn", (unsigned)absolute_sample, frame_number, channel, sample, expected, got);
  1763. flac__utils_printf(stderr, 1, "       In all known cases, verify errors are caused by hardware problems,n");
  1764. flac__utils_printf(stderr, 1, "       usually overclocking or bad RAM.  Delete %sn", e->inbasefilename);
  1765. flac__utils_printf(stderr, 1, "       and repeat the flac command exactly as before.  If it does not give an");
  1766. flac__utils_printf(stderr, 1, "       verify error in the exact same place each time you try it, then there isn");
  1767. flac__utils_printf(stderr, 1, "       a problem with your hardware.  If it does, keep the bad FLAC file andn");
  1768. flac__utils_printf(stderr, 1, "       submit a bug report to:n");
  1769. flac__utils_printf(stderr, 1, "           http://sourceforge.net/bugs/?func=addbug&group_id=13478n");
  1770. flac__utils_printf(stderr, 1, "       Make sure to include an email contact in the comment and/or use then");
  1771. flac__utils_printf(stderr, 1, "       "Monitor" feature to monitor the bug status.n");
  1772. flac__utils_printf(stderr, 1, "Verify FAILED!  Do not trust %sn", e->outfilename);
  1773. }
  1774. FLAC__bool read_little_endian_uint16(FILE *f, FLAC__uint16 *val, FLAC__bool eof_ok, const char *fn)
  1775. {
  1776. size_t bytes_read = fread(val, 1, 2, f);
  1777. if(bytes_read == 0) {
  1778. if(!eof_ok) {
  1779. flac__utils_printf(stderr, 1, "%s: ERROR: unexpected EOFn", fn);
  1780. return false;
  1781. }
  1782. else
  1783. return true;
  1784. }
  1785. else if(bytes_read < 2) {
  1786. flac__utils_printf(stderr, 1, "%s: ERROR: unexpected EOFn", fn);
  1787. return false;
  1788. }
  1789. else {
  1790. if(is_big_endian_host_) {
  1791. FLAC__byte tmp, *b = (FLAC__byte*)val;
  1792. tmp = b[1]; b[1] = b[0]; b[0] = tmp;
  1793. }
  1794. return true;
  1795. }
  1796. }
  1797. FLAC__bool read_little_endian_uint32(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn)
  1798. {
  1799. size_t bytes_read = fread(val, 1, 4, f);
  1800. if(bytes_read == 0) {
  1801. if(!eof_ok) {
  1802. flac__utils_printf(stderr, 1, "%s: ERROR: unexpected EOFn", fn);
  1803. return false;
  1804. }
  1805. else
  1806. return true;
  1807. }
  1808. else if(bytes_read < 4) {
  1809. flac__utils_printf(stderr, 1, "%s: ERROR: unexpected EOFn", fn);
  1810. return false;
  1811. }
  1812. else {
  1813. if(is_big_endian_host_) {
  1814. FLAC__byte tmp, *b = (FLAC__byte*)val;
  1815. tmp = b[3]; b[3] = b[0]; b[0] = tmp;
  1816. tmp = b[2]; b[2] = b[1]; b[1] = tmp;
  1817. }
  1818. return true;
  1819. }
  1820. }
  1821. FLAC__bool read_big_endian_uint16(FILE *f, FLAC__uint16 *val, FLAC__bool eof_ok, const char *fn)
  1822. {
  1823. unsigned char buf[4];
  1824. size_t bytes_read= fread(buf, 1, 2, f);
  1825. if(bytes_read==0U && eof_ok)
  1826. return true;
  1827. else if(bytes_read<2U) {
  1828. flac__utils_printf(stderr, 1, "%s: ERROR: unexpected EOFn", fn);
  1829. return false;
  1830. }
  1831. /* this is independent of host endianness */
  1832. *val= (FLAC__uint16)(buf[0])<<8 | buf[1];
  1833. return true;
  1834. }
  1835. FLAC__bool read_big_endian_uint32(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn)
  1836. {
  1837. unsigned char buf[4];
  1838. size_t bytes_read= fread(buf, 1, 4, f);
  1839. if(bytes_read==0U && eof_ok)
  1840. return true;
  1841. else if(bytes_read<4U) {
  1842. flac__utils_printf(stderr, 1, "%s: ERROR: unexpected EOFn", fn);
  1843. return false;
  1844. }
  1845. /* this is independent of host endianness */
  1846. *val= (FLAC__uint32)(buf[0])<<24 | (FLAC__uint32)(buf[1])<<16 |
  1847. (FLAC__uint32)(buf[2])<<8 | buf[3];
  1848. return true;
  1849. }
  1850. FLAC__bool read_sane_extended(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn)
  1851. /* Read an IEEE 754 80-bit (aka SANE) extended floating point value from 'f',
  1852.  * convert it into an integral value and store in 'val'.  Return false if only
  1853.  * between 1 and 9 bytes remain in 'f', if 0 bytes remain in 'f' and 'eof_ok' is
  1854.  * false, or if the value is negative, between zero and one, or too large to be
  1855.  * represented by 'val'; return true otherwise.
  1856.  */
  1857. {
  1858. unsigned int i;
  1859. unsigned char buf[10];
  1860. size_t bytes_read= fread(buf, 1U, 10U, f);
  1861. FLAC__int16 e= ((FLAC__uint16)(buf[0])<<8 | (FLAC__uint16)(buf[1]))-0x3FFF;
  1862. FLAC__int16 shift= 63-e;
  1863. FLAC__uint64 p= 0U;
  1864. if(bytes_read==0U && eof_ok)
  1865. return true;
  1866. else if(bytes_read<10U) {
  1867. flac__utils_printf(stderr, 1, "%s: ERROR: unexpected EOFn", fn);
  1868. return false;
  1869. }
  1870. else if((buf[0]>>7)==1U || e<0 || e>63) {
  1871. flac__utils_printf(stderr, 1, "%s: ERROR: invalid floating-point valuen", fn);
  1872. return false;
  1873. }
  1874. for(i= 0U; i<8U; ++i)
  1875. p|= (FLAC__uint64)(buf[i+2])<<(56U-i*8);
  1876. *val= (FLAC__uint32)((p>>shift)+(p>>(shift-1) & 0x1));
  1877. return true;
  1878. }
  1879. FLAC__bool fskip_ahead(FILE *f, FLAC__uint64 offset)
  1880. {
  1881. static unsigned char dump[8192];
  1882. while(offset > 0) {
  1883. long need = (long)min(offset, LONG_MAX);
  1884.     if(fseek(f, need, SEEK_CUR) < 0) {
  1885. need = (long)min(offset, sizeof(dump));
  1886. if(fread(dump, need, 1, f) < 1)
  1887. return false;
  1888. }
  1889. offset -= need;
  1890. }
  1891. return true;
  1892. }