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

Windows CE

开发平台:

C/C++

  1. /* libFLAC - Free Lossless Audio Codec library
  2.  * Copyright (C) 2000,2001,2002,2003,2004,2005  Josh Coalson
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * - Redistributions of source code must retain the above copyright
  9.  * notice, this list of conditions and the following disclaimer.
  10.  *
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  * notice, this list of conditions and the following disclaimer in the
  13.  * documentation and/or other materials provided with the distribution.
  14.  *
  15.  * - Neither the name of the Xiph.org Foundation nor the names of its
  16.  * contributors may be used to endorse or promote products derived from
  17.  * this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20.  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22.  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31. #include <limits.h>
  32. #include <stdio.h>
  33. #include <stdlib.h> /* for malloc() */
  34. #include <string.h> /* for memcpy() */
  35. #include "FLAC/assert.h"
  36. #include "FLAC/stream_decoder.h"
  37. #include "protected/stream_encoder.h"
  38. #include "private/bitbuffer.h"
  39. #include "private/bitmath.h"
  40. #include "private/crc.h"
  41. #include "private/cpu.h"
  42. #include "private/fixed.h"
  43. #include "private/format.h"
  44. #include "private/lpc.h"
  45. #include "private/md5.h"
  46. #include "private/memory.h"
  47. #include "private/stream_encoder_framing.h"
  48. #ifdef HAVE_CONFIG_H
  49. #include <config.h>
  50. #endif
  51. #ifdef min
  52. #undef min
  53. #endif
  54. #define min(x,y) ((x)<(y)?(x):(y))
  55. #ifdef max
  56. #undef max
  57. #endif
  58. #define max(x,y) ((x)>(y)?(x):(y))
  59. typedef struct {
  60. FLAC__int32 *data[FLAC__MAX_CHANNELS];
  61. unsigned size; /* of each data[] in samples */
  62. unsigned tail;
  63. } verify_input_fifo;
  64. typedef struct {
  65. const FLAC__byte *data;
  66. unsigned capacity;
  67. unsigned bytes;
  68. } verify_output;
  69. typedef enum {
  70. ENCODER_IN_MAGIC = 0,
  71. ENCODER_IN_METADATA = 1,
  72. ENCODER_IN_AUDIO = 2
  73. } EncoderStateHint;
  74. /***********************************************************************
  75.  *
  76.  * Private class method prototypes
  77.  *
  78.  ***********************************************************************/
  79. static void set_defaults_(FLAC__StreamEncoder *encoder);
  80. static void free_(FLAC__StreamEncoder *encoder);
  81. static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
  82. static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
  83. static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
  84. static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
  85. static FLAC__bool process_subframe_(
  86. FLAC__StreamEncoder *encoder,
  87. unsigned min_partition_order,
  88. unsigned max_partition_order,
  89. FLAC__bool precompute_partition_sums,
  90. const FLAC__FrameHeader *frame_header,
  91. unsigned subframe_bps,
  92. const FLAC__int32 integer_signal[],
  93. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  94. const FLAC__real real_signal[],
  95. #endif
  96. FLAC__Subframe *subframe[2],
  97. FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
  98. FLAC__int32 *residual[2],
  99. unsigned *best_subframe,
  100. unsigned *best_bits
  101. );
  102. static FLAC__bool add_subframe_(
  103. FLAC__StreamEncoder *encoder,
  104. const FLAC__FrameHeader *frame_header,
  105. unsigned subframe_bps,
  106. const FLAC__Subframe *subframe,
  107. FLAC__BitBuffer *frame
  108. );
  109. static unsigned evaluate_constant_subframe_(
  110. const FLAC__int32 signal,
  111. unsigned subframe_bps,
  112. FLAC__Subframe *subframe
  113. );
  114. static unsigned evaluate_fixed_subframe_(
  115. FLAC__StreamEncoder *encoder,
  116. const FLAC__int32 signal[],
  117. FLAC__int32 residual[],
  118. FLAC__uint32 abs_residual[],
  119. FLAC__uint64 abs_residual_partition_sums[],
  120. unsigned raw_bits_per_partition[],
  121. unsigned blocksize,
  122. unsigned subframe_bps,
  123. unsigned order,
  124. unsigned rice_parameter,
  125. unsigned min_partition_order,
  126. unsigned max_partition_order,
  127. FLAC__bool precompute_partition_sums,
  128. FLAC__bool do_escape_coding,
  129. unsigned rice_parameter_search_dist,
  130. FLAC__Subframe *subframe,
  131. FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
  132. );
  133. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  134. static unsigned evaluate_lpc_subframe_(
  135. FLAC__StreamEncoder *encoder,
  136. const FLAC__int32 signal[],
  137. FLAC__int32 residual[],
  138. FLAC__uint32 abs_residual[],
  139. FLAC__uint64 abs_residual_partition_sums[],
  140. unsigned raw_bits_per_partition[],
  141. const FLAC__real lp_coeff[],
  142. unsigned blocksize,
  143. unsigned subframe_bps,
  144. unsigned order,
  145. unsigned qlp_coeff_precision,
  146. unsigned rice_parameter,
  147. unsigned min_partition_order,
  148. unsigned max_partition_order,
  149. FLAC__bool precompute_partition_sums,
  150. FLAC__bool do_escape_coding,
  151. unsigned rice_parameter_search_dist,
  152. FLAC__Subframe *subframe,
  153. FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
  154. );
  155. #endif
  156. static unsigned evaluate_verbatim_subframe_(
  157. const FLAC__int32 signal[],
  158. unsigned blocksize,
  159. unsigned subframe_bps,
  160. FLAC__Subframe *subframe
  161. );
  162. static unsigned find_best_partition_order_(
  163. struct FLAC__StreamEncoderPrivate *private_,
  164. const FLAC__int32 residual[],
  165. FLAC__uint32 abs_residual[],
  166. FLAC__uint64 abs_residual_partition_sums[],
  167. unsigned raw_bits_per_partition[],
  168. unsigned residual_samples,
  169. unsigned predictor_order,
  170. unsigned rice_parameter,
  171. unsigned min_partition_order,
  172. unsigned max_partition_order,
  173. FLAC__bool precompute_partition_sums,
  174. FLAC__bool do_escape_coding,
  175. unsigned rice_parameter_search_dist,
  176. FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
  177. );
  178. static void precompute_partition_info_sums_(
  179. const FLAC__uint32 abs_residual[],
  180. FLAC__uint64 abs_residual_partition_sums[],
  181. unsigned residual_samples,
  182. unsigned predictor_order,
  183. unsigned min_partition_order,
  184. unsigned max_partition_order
  185. );
  186. static void precompute_partition_info_escapes_(
  187. const FLAC__int32 residual[],
  188. unsigned raw_bits_per_partition[],
  189. unsigned residual_samples,
  190. unsigned predictor_order,
  191. unsigned min_partition_order,
  192. unsigned max_partition_order
  193. );
  194. #ifdef DONT_ESTIMATE_RICE_BITS
  195. static FLAC__bool set_partitioned_rice_(
  196. const FLAC__uint32 abs_residual[],
  197. const FLAC__int32 residual[],
  198. const unsigned residual_samples,
  199. const unsigned predictor_order,
  200. const unsigned suggested_rice_parameter,
  201. const unsigned rice_parameter_search_dist,
  202. const unsigned partition_order,
  203. FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
  204. unsigned *bits
  205. );
  206. static FLAC__bool set_partitioned_rice_with_precompute_(
  207. const FLAC__int32 residual[],
  208. const FLAC__uint64 abs_residual_partition_sums[],
  209. const unsigned raw_bits_per_partition[],
  210. const unsigned residual_samples,
  211. const unsigned predictor_order,
  212. const unsigned suggested_rice_parameter,
  213. const unsigned rice_parameter_search_dist,
  214. const unsigned partition_order,
  215. const FLAC__bool search_for_escapes,
  216. FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
  217. unsigned *bits
  218. );
  219. #else
  220. static FLAC__bool set_partitioned_rice_(
  221. const FLAC__uint32 abs_residual[],
  222. const unsigned residual_samples,
  223. const unsigned predictor_order,
  224. const unsigned suggested_rice_parameter,
  225. const unsigned rice_parameter_search_dist,
  226. const unsigned partition_order,
  227. FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
  228. unsigned *bits
  229. );
  230. static FLAC__bool set_partitioned_rice_with_precompute_(
  231. const FLAC__uint32 abs_residual[],
  232. const FLAC__uint64 abs_residual_partition_sums[],
  233. const unsigned raw_bits_per_partition[],
  234. const unsigned residual_samples,
  235. const unsigned predictor_order,
  236. const unsigned suggested_rice_parameter,
  237. const unsigned rice_parameter_search_dist,
  238. const unsigned partition_order,
  239. const FLAC__bool search_for_escapes,
  240. FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
  241. unsigned *bits
  242. );
  243. #endif
  244. static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
  245. /* verify-related routines: */
  246. static void append_to_verify_fifo_(
  247. verify_input_fifo *fifo,
  248. const FLAC__int32 * const input[],
  249. unsigned input_offset,
  250. unsigned channels,
  251. unsigned wide_samples
  252. );
  253. static void append_to_verify_fifo_interleaved_(
  254. verify_input_fifo *fifo,
  255. const FLAC__int32 input[],
  256. unsigned input_offset,
  257. unsigned channels,
  258. unsigned wide_samples
  259. );
  260. static FLAC__StreamDecoderReadStatus verify_read_callback_(
  261. const FLAC__StreamDecoder *decoder,
  262. FLAC__byte buffer[],
  263. unsigned *bytes,
  264. void *client_data
  265. );
  266. static FLAC__StreamDecoderWriteStatus verify_write_callback_(
  267. const FLAC__StreamDecoder *decoder,
  268. const FLAC__Frame *frame,
  269. const FLAC__int32 * const buffer[],
  270. void *client_data
  271. );
  272. static void verify_metadata_callback_(
  273. const FLAC__StreamDecoder *decoder,
  274. const FLAC__StreamMetadata *metadata,
  275. void *client_data
  276. );
  277. static void verify_error_callback_(
  278. const FLAC__StreamDecoder *decoder,
  279. FLAC__StreamDecoderErrorStatus status,
  280. void *client_data
  281. );
  282. /***********************************************************************
  283.  *
  284.  * Private class data
  285.  *
  286.  ***********************************************************************/
  287. typedef struct FLAC__StreamEncoderPrivate {
  288. unsigned input_capacity;                          /* current size (in samples) of the signal and residual buffers */
  289. FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS];  /* the integer version of the input signal */
  290. FLAC__int32 *integer_signal_mid_side[2];          /* the integer version of the mid-side input signal (stereo only) */
  291. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  292. FLAC__real *real_signal[FLAC__MAX_CHANNELS];      /* the floating-point version of the input signal */
  293. FLAC__real *real_signal_mid_side[2];              /* the floating-point version of the mid-side input signal (stereo only) */
  294. #endif
  295. unsigned subframe_bps[FLAC__MAX_CHANNELS];        /* the effective bits per sample of the input signal (stream bps - wasted bits) */
  296. unsigned subframe_bps_mid_side[2];                /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
  297. FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
  298. FLAC__int32 *residual_workspace_mid_side[2][2];
  299. FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
  300. FLAC__Subframe subframe_workspace_mid_side[2][2];
  301. FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
  302. FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
  303. FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
  304. FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
  305. FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
  306. FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
  307. unsigned best_subframe[FLAC__MAX_CHANNELS];       /* index into the above workspaces */
  308. unsigned best_subframe_mid_side[2];
  309. unsigned best_subframe_bits[FLAC__MAX_CHANNELS];  /* size in bits of the best subframe for each channel */
  310. unsigned best_subframe_bits_mid_side[2];
  311. FLAC__uint32 *abs_residual;                       /* workspace where abs(candidate residual) is stored */
  312. FLAC__uint64 *abs_residual_partition_sums;        /* workspace where the sum of abs(candidate residual) for each partition is stored */
  313. unsigned *raw_bits_per_partition;                 /* workspace where the sum of silog2(candidate residual) for each partition is stored */
  314. FLAC__BitBuffer *frame;                           /* the current frame being worked on */
  315. unsigned loose_mid_side_stereo_frames;            /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
  316. unsigned loose_mid_side_stereo_frame_count;       /* number of frames using the current channel assignment */
  317. FLAC__ChannelAssignment last_channel_assignment;
  318. FLAC__StreamMetadata metadata;
  319. unsigned current_sample_number;
  320. unsigned current_frame_number;
  321. struct FLAC__MD5Context md5context;
  322. FLAC__CPUInfo cpuinfo;
  323. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  324. unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
  325. #else
  326. unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
  327. #endif
  328. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  329. void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
  330. void (*local_lpc_compute_residual_from_qlp_coefficients)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
  331. void (*local_lpc_compute_residual_from_qlp_coefficients_64bit)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
  332. void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
  333. #endif
  334. FLAC__bool use_wide_by_block;          /* use slow 64-bit versions of some functions because of the block size */
  335. FLAC__bool use_wide_by_partition;      /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
  336. FLAC__bool use_wide_by_order;          /* use slow 64-bit versions of some functions because of the lpc order */
  337. FLAC__bool precompute_partition_sums;  /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
  338. FLAC__bool disable_constant_subframes;
  339. FLAC__bool disable_fixed_subframes;
  340. FLAC__bool disable_verbatim_subframes;
  341. FLAC__StreamEncoderWriteCallback write_callback;
  342. FLAC__StreamEncoderMetadataCallback metadata_callback;
  343. void *client_data;
  344. /* unaligned (original) pointers to allocated data */
  345. FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
  346. FLAC__int32 *integer_signal_mid_side_unaligned[2];
  347. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  348. FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
  349. FLAC__real *real_signal_mid_side_unaligned[2];
  350. #endif
  351. FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
  352. FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
  353. FLAC__uint32 *abs_residual_unaligned;
  354. FLAC__uint64 *abs_residual_partition_sums_unaligned;
  355. unsigned *raw_bits_per_partition_unaligned;
  356. /*
  357.  * These fields have been moved here from private function local
  358.  * declarations merely to save stack space during encoding.
  359.  */
  360. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  361. FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
  362. #endif
  363. FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
  364. /*
  365.  * The data for the verify section
  366.  */
  367. struct {
  368. FLAC__StreamDecoder *decoder;
  369. EncoderStateHint state_hint;
  370. FLAC__bool needs_magic_hack;
  371. verify_input_fifo input_fifo;
  372. verify_output output;
  373. struct {
  374. FLAC__uint64 absolute_sample;
  375. unsigned frame_number;
  376. unsigned channel;
  377. unsigned sample;
  378. FLAC__int32 expected;
  379. FLAC__int32 got;
  380. } error_stats;
  381. } verify;
  382. FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
  383. } FLAC__StreamEncoderPrivate;
  384. /***********************************************************************
  385.  *
  386.  * Public static class data
  387.  *
  388.  ***********************************************************************/
  389. FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
  390. "FLAC__STREAM_ENCODER_OK",
  391. "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
  392. "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
  393. "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
  394. "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
  395. "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
  396. "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
  397. "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
  398. "FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER",
  399. "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
  400. "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
  401. "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
  402. "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
  403. "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
  404. "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
  405. "FLAC__STREAM_ENCODER_FRAMING_ERROR",
  406. "FLAC__STREAM_ENCODER_INVALID_METADATA",
  407. "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
  408. "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
  409. "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
  410. "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
  411. "FLAC__STREAM_ENCODER_UNINITIALIZED"
  412. };
  413. FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
  414. "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
  415. "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
  416. };
  417. /***********************************************************************
  418.  *
  419.  * Class constructor/destructor
  420.  *
  421.  */
  422. FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
  423. {
  424. FLAC__StreamEncoder *encoder;
  425. unsigned i;
  426. FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
  427. encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
  428. if(encoder == 0) {
  429. return 0;
  430. }
  431. encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
  432. if(encoder->protected_ == 0) {
  433. free(encoder);
  434. return 0;
  435. }
  436. encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
  437. if(encoder->private_ == 0) {
  438. free(encoder->protected_);
  439. free(encoder);
  440. return 0;
  441. }
  442. encoder->private_->frame = FLAC__bitbuffer_new();
  443. if(encoder->private_->frame == 0) {
  444. free(encoder->private_);
  445. free(encoder->protected_);
  446. free(encoder);
  447. return 0;
  448. }
  449. set_defaults_(encoder);
  450. encoder->private_->is_being_deleted = false;
  451. for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
  452. encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
  453. encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
  454. }
  455. for(i = 0; i < 2; i++) {
  456. encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
  457. encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
  458. }
  459. for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
  460. encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
  461. encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
  462. }
  463. for(i = 0; i < 2; i++) {
  464. encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
  465. encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
  466. }
  467. for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
  468. FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
  469. FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
  470. }
  471. for(i = 0; i < 2; i++) {
  472. FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
  473. FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
  474. }
  475. for(i = 0; i < 2; i++)
  476. FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
  477. encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
  478. return encoder;
  479. }
  480. FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
  481. {
  482. unsigned i;
  483. FLAC__ASSERT(0 != encoder);
  484. FLAC__ASSERT(0 != encoder->protected_);
  485. FLAC__ASSERT(0 != encoder->private_);
  486. FLAC__ASSERT(0 != encoder->private_->frame);
  487. encoder->private_->is_being_deleted = true;
  488. FLAC__stream_encoder_finish(encoder);
  489. if(0 != encoder->private_->verify.decoder)
  490. FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
  491. for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
  492. FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
  493. FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
  494. }
  495. for(i = 0; i < 2; i++) {
  496. FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
  497. FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
  498. }
  499. for(i = 0; i < 2; i++)
  500. FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
  501. FLAC__bitbuffer_delete(encoder->private_->frame);
  502. free(encoder->private_);
  503. free(encoder->protected_);
  504. free(encoder);
  505. }
  506. /***********************************************************************
  507.  *
  508.  * Public class methods
  509.  *
  510.  ***********************************************************************/
  511. FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
  512. {
  513. unsigned i;
  514. FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment;
  515. FLAC__ASSERT(0 != encoder);
  516. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  517. return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
  518. encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
  519. if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
  520. return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
  521. if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
  522. return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
  523. if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
  524. return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
  525. if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
  526. return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
  527. if(encoder->protected_->bits_per_sample >= 32)
  528. encoder->protected_->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
  529. if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
  530. return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
  531. if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
  532. return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
  533. if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
  534. return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
  535. if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
  536. return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER;
  537. if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
  538. return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
  539. if(encoder->protected_->qlp_coeff_precision == 0) {
  540. if(encoder->protected_->bits_per_sample < 16) {
  541. /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
  542. /* @@@ until then we'll make a guess */
  543. encoder->protected_->qlp_coeff_precision = max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
  544. }
  545. else if(encoder->protected_->bits_per_sample == 16) {
  546. if(encoder->protected_->blocksize <= 192)
  547. encoder->protected_->qlp_coeff_precision = 7;
  548. else if(encoder->protected_->blocksize <= 384)
  549. encoder->protected_->qlp_coeff_precision = 8;
  550. else if(encoder->protected_->blocksize <= 576)
  551. encoder->protected_->qlp_coeff_precision = 9;
  552. else if(encoder->protected_->blocksize <= 1152)
  553. encoder->protected_->qlp_coeff_precision = 10;
  554. else if(encoder->protected_->blocksize <= 2304)
  555. encoder->protected_->qlp_coeff_precision = 11;
  556. else if(encoder->protected_->blocksize <= 4608)
  557. encoder->protected_->qlp_coeff_precision = 12;
  558. else
  559. encoder->protected_->qlp_coeff_precision = 13;
  560. }
  561. else {
  562. if(encoder->protected_->blocksize <= 384)
  563. encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
  564. else if(encoder->protected_->blocksize <= 1152)
  565. encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
  566. else
  567. encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
  568. }
  569. FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
  570. }
  571. else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
  572. return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
  573. if(encoder->protected_->streamable_subset) {
  574. if(
  575. encoder->protected_->blocksize != 192 &&
  576. encoder->protected_->blocksize != 576 &&
  577. encoder->protected_->blocksize != 1152 &&
  578. encoder->protected_->blocksize != 2304 &&
  579. encoder->protected_->blocksize != 4608 &&
  580. encoder->protected_->blocksize != 256 &&
  581. encoder->protected_->blocksize != 512 &&
  582. encoder->protected_->blocksize != 1024 &&
  583. encoder->protected_->blocksize != 2048 &&
  584. encoder->protected_->blocksize != 4096 &&
  585. encoder->protected_->blocksize != 8192 &&
  586. encoder->protected_->blocksize != 16384
  587. )
  588. return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
  589. if(
  590. encoder->protected_->sample_rate != 8000 &&
  591. encoder->protected_->sample_rate != 16000 &&
  592. encoder->protected_->sample_rate != 22050 &&
  593. encoder->protected_->sample_rate != 24000 &&
  594. encoder->protected_->sample_rate != 32000 &&
  595. encoder->protected_->sample_rate != 44100 &&
  596. encoder->protected_->sample_rate != 48000 &&
  597. encoder->protected_->sample_rate != 96000
  598. )
  599. return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
  600. if(
  601. encoder->protected_->bits_per_sample != 8 &&
  602. encoder->protected_->bits_per_sample != 12 &&
  603. encoder->protected_->bits_per_sample != 16 &&
  604. encoder->protected_->bits_per_sample != 20 &&
  605. encoder->protected_->bits_per_sample != 24
  606. )
  607. return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
  608. if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
  609. return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
  610. }
  611. if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
  612. encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
  613. if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
  614. encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
  615. /* validate metadata */
  616. if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
  617. return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
  618. metadata_has_seektable = false;
  619. metadata_has_vorbis_comment = false;
  620. for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
  621. if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
  622. return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
  623. else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
  624. if(metadata_has_seektable) /* only one is allowed */
  625. return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
  626. metadata_has_seektable = true;
  627. if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
  628. return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
  629. }
  630. else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
  631. if(metadata_has_vorbis_comment) /* only one is allowed */
  632. return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
  633. metadata_has_vorbis_comment = true;
  634. }
  635. else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_CUESHEET) {
  636. if(!FLAC__format_cuesheet_is_legal(&encoder->protected_->metadata[i]->data.cue_sheet, encoder->protected_->metadata[i]->data.cue_sheet.is_cd, /*violation=*/0))
  637. return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
  638. }
  639. }
  640. encoder->private_->input_capacity = 0;
  641. for(i = 0; i < encoder->protected_->channels; i++) {
  642. encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
  643. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  644. encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
  645. #endif
  646. }
  647. for(i = 0; i < 2; i++) {
  648. encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
  649. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  650. encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
  651. #endif
  652. }
  653. for(i = 0; i < encoder->protected_->channels; i++) {
  654. encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
  655. encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
  656. encoder->private_->best_subframe[i] = 0;
  657. }
  658. for(i = 0; i < 2; i++) {
  659. encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
  660. encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
  661. encoder->private_->best_subframe_mid_side[i] = 0;
  662. }
  663. encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
  664. encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
  665. encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
  666. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  667. encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
  668. #else
  669. /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
  670. /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply&divide by hand */
  671. FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
  672. FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
  673. FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
  674. FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
  675. encoder->private_->loose_mid_side_stereo_frames = (unsigned)FLAC__fixedpoint_trunc((((FLAC__uint64)(encoder->protected_->sample_rate) * (FLAC__uint64)(26214)) << 16) / (encoder->protected_->blocksize<<16) + FLAC__FP_ONE_HALF);
  676. #endif
  677. if(encoder->private_->loose_mid_side_stereo_frames == 0)
  678. encoder->private_->loose_mid_side_stereo_frames = 1;
  679. encoder->private_->loose_mid_side_stereo_frame_count = 0;
  680. encoder->private_->current_sample_number = 0;
  681. encoder->private_->current_frame_number = 0;
  682. encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
  683. encoder->private_->use_wide_by_order = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(max(encoder->protected_->max_lpc_order, FLAC__MAX_FIXED_ORDER))+1 > 30); /*@@@ need to use this? */
  684. encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
  685. /*
  686.  * get the CPU info and set the function pointers
  687.  */
  688. FLAC__cpu_info(&encoder->private_->cpuinfo);
  689. /* first default to the non-asm routines */
  690. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  691. encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
  692. #endif
  693. encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
  694. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  695. encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
  696. encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
  697. encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
  698. #endif
  699. /* now override with asm where appropriate */
  700. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  701. # ifndef FLAC__NO_ASM
  702. if(encoder->private_->cpuinfo.use_asm) {
  703. #  ifdef FLAC__CPU_IA32
  704. FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
  705. #   ifdef FLAC__HAS_NASM
  706. #    ifdef FLAC__SSE_OS
  707. if(encoder->private_->cpuinfo.data.ia32.sse) {
  708. if(encoder->protected_->max_lpc_order < 4)
  709. encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
  710. else if(encoder->protected_->max_lpc_order < 8)
  711. encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
  712. else if(encoder->protected_->max_lpc_order < 12)
  713. encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
  714. else
  715. encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
  716. }
  717. else
  718. #    endif /* FLAC__SSE_OS */
  719. if(encoder->private_->cpuinfo.data.ia32._3dnow)
  720. encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
  721. else
  722. encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
  723. if(encoder->private_->cpuinfo.data.ia32.mmx) {
  724. encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
  725. encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
  726. }
  727. else {
  728. encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
  729. encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
  730. }
  731. if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
  732. encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
  733. #   endif /* FLAC__HAS_NASM */
  734. #  endif /* FLAC__CPU_IA32 */
  735. }
  736. # endif /* !FLAC__NO_ASM */
  737. #endif /* !FLAC__INTEGER_ONLY_LIBRARY */
  738. /* finally override based on wide-ness if necessary */
  739. if(encoder->private_->use_wide_by_block) {
  740. encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
  741. }
  742. /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
  743. encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
  744. if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
  745. /* the above function sets the state for us in case of an error */
  746. return encoder->protected_->state;
  747. }
  748. if(!FLAC__bitbuffer_init(encoder->private_->frame))
  749. return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
  750. /*
  751.  * Set up the verify stuff if necessary
  752.  */
  753. if(encoder->protected_->verify) {
  754. /*
  755.  * First, set up the fifo which will hold the
  756.  * original signal to compare against
  757.  */
  758. encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
  759. for(i = 0; i < encoder->protected_->channels; i++) {
  760. if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size)))
  761. return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
  762. }
  763. encoder->private_->verify.input_fifo.tail = 0;
  764. /*
  765.  * Now set up a stream decoder for verification
  766.  */
  767. encoder->private_->verify.decoder = FLAC__stream_decoder_new();
  768. if(0 == encoder->private_->verify.decoder)
  769. return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
  770. FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_);
  771. FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_);
  772. FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_);
  773. FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_);
  774. FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder);
  775. if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
  776. return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
  777. }
  778. encoder->private_->verify.error_stats.absolute_sample = 0;
  779. encoder->private_->verify.error_stats.frame_number = 0;
  780. encoder->private_->verify.error_stats.channel = 0;
  781. encoder->private_->verify.error_stats.sample = 0;
  782. encoder->private_->verify.error_stats.expected = 0;
  783. encoder->private_->verify.error_stats.got = 0;
  784. /*
  785.  * write the stream header
  786.  */
  787. if(encoder->protected_->verify)
  788. encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
  789. if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
  790. return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
  791. if(!write_bitbuffer_(encoder, 0)) {
  792. /* the above function sets the state for us in case of an error */
  793. return encoder->protected_->state;
  794. }
  795. /*
  796.  * write the STREAMINFO metadata block
  797.  */
  798. if(encoder->protected_->verify)
  799. encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
  800. encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
  801. encoder->private_->metadata.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
  802. encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
  803. encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
  804. encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
  805. encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
  806. encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
  807. encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
  808. encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
  809. encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
  810. encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
  811. memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
  812. FLAC__MD5Init(&encoder->private_->md5context);
  813. if(!FLAC__bitbuffer_clear(encoder->private_->frame))
  814. return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
  815. if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
  816. return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
  817. if(!write_bitbuffer_(encoder, 0)) {
  818. /* the above function sets the state for us in case of an error */
  819. return encoder->protected_->state;
  820. }
  821. /*
  822.  * Now that the STREAMINFO block is written, we can init this to an
  823.  * absurdly-high value...
  824.  */
  825. encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
  826. /* ... and clear this to 0 */
  827. encoder->private_->metadata.data.stream_info.total_samples = 0;
  828. /*
  829.  * Check to see if the supplied metadata contains a VORBIS_COMMENT;
  830.  * if not, we will write an empty one (FLAC__add_metadata_block()
  831.  * automatically supplies the vendor string).
  832.  *
  833.  * WATCHOUT: libOggFLAC depends on us to write this block after the
  834.  * STREAMINFO since that's what the mapping requires.  (In the case
  835.  * that metadata_has_vorbis_comment is true it will have already
  836.  * insured that the metadata list is properly ordered.)
  837.  */
  838. if(!metadata_has_vorbis_comment) {
  839. FLAC__StreamMetadata vorbis_comment;
  840. vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
  841. vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
  842. vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
  843. vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
  844. vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
  845. vorbis_comment.data.vorbis_comment.num_comments = 0;
  846. vorbis_comment.data.vorbis_comment.comments = 0;
  847. if(!FLAC__bitbuffer_clear(encoder->private_->frame))
  848. return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
  849. if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame))
  850. return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
  851. if(!write_bitbuffer_(encoder, 0)) {
  852. /* the above function sets the state for us in case of an error */
  853. return encoder->protected_->state;
  854. }
  855. }
  856. /*
  857.  * write the user's metadata blocks
  858.  */
  859. for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
  860. encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
  861. if(!FLAC__bitbuffer_clear(encoder->private_->frame))
  862. return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
  863. if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
  864. return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
  865. if(!write_bitbuffer_(encoder, 0)) {
  866. /* the above function sets the state for us in case of an error */
  867. return encoder->protected_->state;
  868. }
  869. }
  870. if(encoder->protected_->verify)
  871. encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
  872. return encoder->protected_->state;
  873. }
  874. FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
  875. {
  876. FLAC__ASSERT(0 != encoder);
  877. if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
  878. return;
  879. if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
  880. if(encoder->private_->current_sample_number != 0) {
  881. encoder->protected_->blocksize = encoder->private_->current_sample_number;
  882. process_frame_(encoder, true); /* true => is last frame */
  883. }
  884. }
  885. FLAC__MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
  886. if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
  887. encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
  888. }
  889. if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
  890. FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
  891. free_(encoder);
  892. set_defaults_(encoder);
  893. encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
  894. }
  895. FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
  896. {
  897. FLAC__ASSERT(0 != encoder);
  898. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  899. return false;
  900. #ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
  901. encoder->protected_->verify = value;
  902. #endif
  903. return true;
  904. }
  905. FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
  906. {
  907. FLAC__ASSERT(0 != encoder);
  908. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  909. return false;
  910. encoder->protected_->streamable_subset = value;
  911. return true;
  912. }
  913. FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
  914. {
  915. FLAC__ASSERT(0 != encoder);
  916. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  917. return false;
  918. encoder->protected_->do_mid_side_stereo = value;
  919. return true;
  920. }
  921. FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
  922. {
  923. FLAC__ASSERT(0 != encoder);
  924. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  925. return false;
  926. encoder->protected_->loose_mid_side_stereo = value;
  927. return true;
  928. }
  929. FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
  930. {
  931. FLAC__ASSERT(0 != encoder);
  932. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  933. return false;
  934. encoder->protected_->channels = value;
  935. return true;
  936. }
  937. FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
  938. {
  939. FLAC__ASSERT(0 != encoder);
  940. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  941. return false;
  942. encoder->protected_->bits_per_sample = value;
  943. return true;
  944. }
  945. FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
  946. {
  947. FLAC__ASSERT(0 != encoder);
  948. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  949. return false;
  950. encoder->protected_->sample_rate = value;
  951. return true;
  952. }
  953. FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
  954. {
  955. FLAC__ASSERT(0 != encoder);
  956. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  957. return false;
  958. encoder->protected_->blocksize = value;
  959. return true;
  960. }
  961. FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
  962. {
  963. FLAC__ASSERT(0 != encoder);
  964. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  965. return false;
  966. encoder->protected_->max_lpc_order = value;
  967. return true;
  968. }
  969. FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
  970. {
  971. FLAC__ASSERT(0 != encoder);
  972. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  973. return false;
  974. encoder->protected_->qlp_coeff_precision = value;
  975. return true;
  976. }
  977. FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
  978. {
  979. FLAC__ASSERT(0 != encoder);
  980. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  981. return false;
  982. encoder->protected_->do_qlp_coeff_prec_search = value;
  983. return true;
  984. }
  985. FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
  986. {
  987. FLAC__ASSERT(0 != encoder);
  988. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  989. return false;
  990. #if 0
  991. /*@@@ deprecated: */
  992. encoder->protected_->do_escape_coding = value;
  993. #else
  994. (void)value;
  995. #endif
  996. return true;
  997. }
  998. FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
  999. {
  1000. FLAC__ASSERT(0 != encoder);
  1001. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  1002. return false;
  1003. encoder->protected_->do_exhaustive_model_search = value;
  1004. return true;
  1005. }
  1006. FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
  1007. {
  1008. FLAC__ASSERT(0 != encoder);
  1009. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  1010. return false;
  1011. encoder->protected_->min_residual_partition_order = value;
  1012. return true;
  1013. }
  1014. FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
  1015. {
  1016. FLAC__ASSERT(0 != encoder);
  1017. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  1018. return false;
  1019. encoder->protected_->max_residual_partition_order = value;
  1020. return true;
  1021. }
  1022. FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
  1023. {
  1024. FLAC__ASSERT(0 != encoder);
  1025. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  1026. return false;
  1027. #if 0
  1028. /*@@@ deprecated: */
  1029. encoder->protected_->rice_parameter_search_dist = value;
  1030. #else
  1031. (void)value;
  1032. #endif
  1033. return true;
  1034. }
  1035. FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
  1036. {
  1037. FLAC__ASSERT(0 != encoder);
  1038. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  1039. return false;
  1040. encoder->protected_->total_samples_estimate = value;
  1041. return true;
  1042. }
  1043. FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
  1044. {
  1045. FLAC__ASSERT(0 != encoder);
  1046. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  1047. return false;
  1048. encoder->protected_->metadata = metadata;
  1049. encoder->protected_->num_metadata_blocks = num_blocks;
  1050. return true;
  1051. }
  1052. FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
  1053. {
  1054. FLAC__ASSERT(0 != encoder);
  1055. FLAC__ASSERT(0 != value);
  1056. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  1057. return false;
  1058. encoder->private_->write_callback = value;
  1059. return true;
  1060. }
  1061. FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
  1062. {
  1063. FLAC__ASSERT(0 != encoder);
  1064. FLAC__ASSERT(0 != value);
  1065. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  1066. return false;
  1067. encoder->private_->metadata_callback = value;
  1068. return true;
  1069. }
  1070. FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
  1071. {
  1072. FLAC__ASSERT(0 != encoder);
  1073. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  1074. return false;
  1075. encoder->private_->client_data = value;
  1076. return true;
  1077. }
  1078. /*
  1079.  * These three functions are not static, but not publically exposed in
  1080.  * include/FLAC/ either.  They are used by the test suite.
  1081.  */
  1082. FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
  1083. {
  1084. FLAC__ASSERT(0 != encoder);
  1085. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  1086. return false;
  1087. encoder->private_->disable_constant_subframes = value;
  1088. return true;
  1089. }
  1090. FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
  1091. {
  1092. FLAC__ASSERT(0 != encoder);
  1093. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  1094. return false;
  1095. encoder->private_->disable_fixed_subframes = value;
  1096. return true;
  1097. }
  1098. FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
  1099. {
  1100. FLAC__ASSERT(0 != encoder);
  1101. if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
  1102. return false;
  1103. encoder->private_->disable_verbatim_subframes = value;
  1104. return true;
  1105. }
  1106. FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
  1107. {
  1108. FLAC__ASSERT(0 != encoder);
  1109. return encoder->protected_->state;
  1110. }
  1111. FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
  1112. {
  1113. FLAC__ASSERT(0 != encoder);
  1114. if(encoder->protected_->verify)
  1115. return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
  1116. else
  1117. return FLAC__STREAM_DECODER_UNINITIALIZED;
  1118. }
  1119. FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
  1120. {
  1121. if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
  1122. return FLAC__StreamEncoderStateString[encoder->protected_->state];
  1123. else
  1124. return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
  1125. }
  1126. FLAC_API void FLAC__stream_encoder_get_verify_decoder_error_stats(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
  1127. {
  1128. FLAC__ASSERT(0 != encoder);
  1129. if(0 != absolute_sample)
  1130. *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
  1131. if(0 != frame_number)
  1132. *frame_number = encoder->private_->verify.error_stats.frame_number;
  1133. if(0 != channel)
  1134. *channel = encoder->private_->verify.error_stats.channel;
  1135. if(0 != sample)
  1136. *sample = encoder->private_->verify.error_stats.sample;
  1137. if(0 != expected)
  1138. *expected = encoder->private_->verify.error_stats.expected;
  1139. if(0 != got)
  1140. *got = encoder->private_->verify.error_stats.got;
  1141. }
  1142. FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
  1143. {
  1144. FLAC__ASSERT(0 != encoder);
  1145. return encoder->protected_->verify;
  1146. }
  1147. FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
  1148. {
  1149. FLAC__ASSERT(0 != encoder);
  1150. return encoder->protected_->streamable_subset;
  1151. }
  1152. FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
  1153. {
  1154. FLAC__ASSERT(0 != encoder);
  1155. return encoder->protected_->do_mid_side_stereo;
  1156. }
  1157. FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
  1158. {
  1159. FLAC__ASSERT(0 != encoder);
  1160. return encoder->protected_->loose_mid_side_stereo;
  1161. }
  1162. FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
  1163. {
  1164. FLAC__ASSERT(0 != encoder);
  1165. return encoder->protected_->channels;
  1166. }
  1167. FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
  1168. {
  1169. FLAC__ASSERT(0 != encoder);
  1170. return encoder->protected_->bits_per_sample;
  1171. }
  1172. FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
  1173. {
  1174. FLAC__ASSERT(0 != encoder);
  1175. return encoder->protected_->sample_rate;
  1176. }
  1177. FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
  1178. {
  1179. FLAC__ASSERT(0 != encoder);
  1180. return encoder->protected_->blocksize;
  1181. }
  1182. FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
  1183. {
  1184. FLAC__ASSERT(0 != encoder);
  1185. return encoder->protected_->max_lpc_order;
  1186. }
  1187. FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
  1188. {
  1189. FLAC__ASSERT(0 != encoder);
  1190. return encoder->protected_->qlp_coeff_precision;
  1191. }
  1192. FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
  1193. {
  1194. FLAC__ASSERT(0 != encoder);
  1195. return encoder->protected_->do_qlp_coeff_prec_search;
  1196. }
  1197. FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
  1198. {
  1199. FLAC__ASSERT(0 != encoder);
  1200. return encoder->protected_->do_escape_coding;
  1201. }
  1202. FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
  1203. {
  1204. FLAC__ASSERT(0 != encoder);
  1205. return encoder->protected_->do_exhaustive_model_search;
  1206. }
  1207. FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
  1208. {
  1209. FLAC__ASSERT(0 != encoder);
  1210. return encoder->protected_->min_residual_partition_order;
  1211. }
  1212. FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
  1213. {
  1214. FLAC__ASSERT(0 != encoder);
  1215. return encoder->protected_->max_residual_partition_order;
  1216. }
  1217. FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
  1218. {
  1219. FLAC__ASSERT(0 != encoder);
  1220. return encoder->protected_->rice_parameter_search_dist;
  1221. }
  1222. FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
  1223. {
  1224. FLAC__ASSERT(0 != encoder);
  1225. return encoder->protected_->total_samples_estimate;
  1226. }
  1227. FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
  1228. {
  1229. unsigned i, j, channel;
  1230. FLAC__int32 x, mid, side;
  1231. const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
  1232. FLAC__ASSERT(0 != encoder);
  1233. FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
  1234. j = 0;
  1235. /*
  1236.  * we have several flavors of the same basic loop, optimized for
  1237.  * different conditions:
  1238.  */
  1239. if(encoder->protected_->max_lpc_order > 0) {
  1240. if(encoder->protected_->do_mid_side_stereo && channels == 2) {
  1241. /*
  1242.  * stereo coding: unroll channel loop
  1243.  * with LPC: calculate floating point version of signal
  1244.  */
  1245. do {
  1246. if(encoder->protected_->verify)
  1247. append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
  1248. for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
  1249. x = mid = side = buffer[0][j];
  1250. encoder->private_->integer_signal[0][i] = x;
  1251. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  1252. encoder->private_->real_signal[0][i] = (FLAC__real)x;
  1253. #endif
  1254. x = buffer[1][j];
  1255. encoder->private_->integer_signal[1][i] = x;
  1256. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  1257. encoder->private_->real_signal[1][i] = (FLAC__real)x;
  1258. #endif
  1259. mid += x;
  1260. side -= x;
  1261. mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
  1262. encoder->private_->integer_signal_mid_side[1][i] = side;
  1263. encoder->private_->integer_signal_mid_side[0][i] = mid;
  1264. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  1265. encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
  1266. encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
  1267. #endif
  1268. encoder->private_->current_sample_number++;
  1269. }
  1270. if(i == blocksize) {
  1271. if(!process_frame_(encoder, false)) /* false => not last frame */
  1272. return false;
  1273. }
  1274. } while(j < samples);
  1275. }
  1276. else {
  1277. /*
  1278.  * independent channel coding: buffer each channel in inner loop
  1279.  * with LPC: calculate floating point version of signal
  1280.  */
  1281. do {
  1282. if(encoder->protected_->verify)
  1283. append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
  1284. for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
  1285. for(channel = 0; channel < channels; channel++) {
  1286. x = buffer[channel][j];
  1287. encoder->private_->integer_signal[channel][i] = x;
  1288. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  1289. encoder->private_->real_signal[channel][i] = (FLAC__real)x;
  1290. #endif
  1291. }
  1292. encoder->private_->current_sample_number++;
  1293. }
  1294. if(i == blocksize) {
  1295. if(!process_frame_(encoder, false)) /* false => not last frame */
  1296. return false;
  1297. }
  1298. } while(j < samples);
  1299. }
  1300. }
  1301. else {
  1302. if(encoder->protected_->do_mid_side_stereo && channels == 2) {
  1303. /*
  1304.  * stereo coding: unroll channel loop
  1305.  * without LPC: no need to calculate floating point version of signal
  1306.  */
  1307. do {
  1308. if(encoder->protected_->verify)
  1309. append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
  1310. for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
  1311. encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
  1312. x = buffer[1][j];
  1313. encoder->private_->integer_signal[1][i] = x;
  1314. mid += x;
  1315. side -= x;
  1316. mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
  1317. encoder->private_->integer_signal_mid_side[1][i] = side;
  1318. encoder->private_->integer_signal_mid_side[0][i] = mid;
  1319. encoder->private_->current_sample_number++;
  1320. }
  1321. if(i == blocksize) {
  1322. if(!process_frame_(encoder, false)) /* false => not last frame */
  1323. return false;
  1324. }
  1325. } while(j < samples);
  1326. }
  1327. else {
  1328. /*
  1329.  * independent channel coding: buffer each channel in inner loop
  1330.  * without LPC: no need to calculate floating point version of signal
  1331.  */
  1332. do {
  1333. if(encoder->protected_->verify)
  1334. append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
  1335. for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
  1336. for(channel = 0; channel < channels; channel++)
  1337. encoder->private_->integer_signal[channel][i] = buffer[channel][j];
  1338. encoder->private_->current_sample_number++;
  1339. }
  1340. if(i == blocksize) {
  1341. if(!process_frame_(encoder, false)) /* false => not last frame */
  1342. return false;
  1343. }
  1344. } while(j < samples);
  1345. }
  1346. }
  1347. return true;
  1348. }
  1349. FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
  1350. {
  1351. unsigned i, j, k, channel;
  1352. FLAC__int32 x, mid, side;
  1353. const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
  1354. FLAC__ASSERT(0 != encoder);
  1355. FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
  1356. j = k = 0;
  1357. /*
  1358.  * we have several flavors of the same basic loop, optimized for
  1359.  * different conditions:
  1360.  */
  1361. if(encoder->protected_->max_lpc_order > 0) {
  1362. if(encoder->protected_->do_mid_side_stereo && channels == 2) {
  1363. /*
  1364.  * stereo coding: unroll channel loop
  1365.  * with LPC: calculate floating point version of signal
  1366.  */
  1367. do {
  1368. if(encoder->protected_->verify)
  1369. append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
  1370. for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
  1371. x = mid = side = buffer[k++];
  1372. encoder->private_->integer_signal[0][i] = x;
  1373. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  1374. encoder->private_->real_signal[0][i] = (FLAC__real)x;
  1375. #endif
  1376. x = buffer[k++];
  1377. encoder->private_->integer_signal[1][i] = x;
  1378. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  1379. encoder->private_->real_signal[1][i] = (FLAC__real)x;
  1380. #endif
  1381. mid += x;
  1382. side -= x;
  1383. mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
  1384. encoder->private_->integer_signal_mid_side[1][i] = side;
  1385. encoder->private_->integer_signal_mid_side[0][i] = mid;
  1386. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  1387. encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
  1388. encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
  1389. #endif
  1390. encoder->private_->current_sample_number++;
  1391. }
  1392. if(i == blocksize) {
  1393. if(!process_frame_(encoder, false)) /* false => not last frame */
  1394. return false;
  1395. }
  1396. } while(j < samples);
  1397. }
  1398. else {
  1399. /*
  1400.  * independent channel coding: buffer each channel in inner loop
  1401.  * with LPC: calculate floating point version of signal
  1402.  */
  1403. do {
  1404. if(encoder->protected_->verify)
  1405. append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
  1406. for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
  1407. for(channel = 0; channel < channels; channel++) {
  1408. x = buffer[k++];
  1409. encoder->private_->integer_signal[channel][i] = x;
  1410. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  1411. encoder->private_->real_signal[channel][i] = (FLAC__real)x;
  1412. #endif
  1413. }
  1414. encoder->private_->current_sample_number++;
  1415. }
  1416. if(i == blocksize) {
  1417. if(!process_frame_(encoder, false)) /* false => not last frame */
  1418. return false;
  1419. }
  1420. } while(j < samples);
  1421. }
  1422. }
  1423. else {
  1424. if(encoder->protected_->do_mid_side_stereo && channels == 2) {
  1425. /*
  1426.  * stereo coding: unroll channel loop
  1427.  * without LPC: no need to calculate floating point version of signal
  1428.  */
  1429. do {
  1430. if(encoder->protected_->verify)
  1431. append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
  1432. for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
  1433. encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
  1434. x = buffer[k++];
  1435. encoder->private_->integer_signal[1][i] = x;
  1436. mid += x;
  1437. side -= x;
  1438. mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
  1439. encoder->private_->integer_signal_mid_side[1][i] = side;
  1440. encoder->private_->integer_signal_mid_side[0][i] = mid;
  1441. encoder->private_->current_sample_number++;
  1442. }
  1443. if(i == blocksize) {
  1444. if(!process_frame_(encoder, false)) /* false => not last frame */
  1445. return false;
  1446. }
  1447. } while(j < samples);
  1448. }
  1449. else {
  1450. /*
  1451.  * independent channel coding: buffer each channel in inner loop
  1452.  * without LPC: no need to calculate floating point version of signal
  1453.  */
  1454. do {
  1455. if(encoder->protected_->verify)
  1456. append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
  1457. for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
  1458. for(channel = 0; channel < channels; channel++)
  1459. encoder->private_->integer_signal[channel][i] = buffer[k++];
  1460. encoder->private_->current_sample_number++;
  1461. }
  1462. if(i == blocksize) {
  1463. if(!process_frame_(encoder, false)) /* false => not last frame */
  1464. return false;
  1465. }
  1466. } while(j < samples);
  1467. }
  1468. }
  1469. return true;
  1470. }
  1471. /***********************************************************************
  1472.  *
  1473.  * Private class methods
  1474.  *
  1475.  ***********************************************************************/
  1476. void set_defaults_(FLAC__StreamEncoder *encoder)
  1477. {
  1478. FLAC__ASSERT(0 != encoder);
  1479. #ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
  1480. encoder->protected_->verify = true;
  1481. #else
  1482. encoder->protected_->verify = false;
  1483. #endif
  1484. encoder->protected_->streamable_subset = true;
  1485. encoder->protected_->do_mid_side_stereo = false;
  1486. encoder->protected_->loose_mid_side_stereo = false;
  1487. encoder->protected_->channels = 2;
  1488. encoder->protected_->bits_per_sample = 16;
  1489. encoder->protected_->sample_rate = 44100;
  1490. encoder->protected_->blocksize = 1152;
  1491. encoder->protected_->max_lpc_order = 0;
  1492. encoder->protected_->qlp_coeff_precision = 0;
  1493. encoder->protected_->do_qlp_coeff_prec_search = false;
  1494. encoder->protected_->do_exhaustive_model_search = false;
  1495. encoder->protected_->do_escape_coding = false;
  1496. encoder->protected_->min_residual_partition_order = 0;
  1497. encoder->protected_->max_residual_partition_order = 0;
  1498. encoder->protected_->rice_parameter_search_dist = 0;
  1499. encoder->protected_->total_samples_estimate = 0;
  1500. encoder->protected_->metadata = 0;
  1501. encoder->protected_->num_metadata_blocks = 0;
  1502. encoder->private_->disable_constant_subframes = false;
  1503. encoder->private_->disable_fixed_subframes = false;
  1504. encoder->private_->disable_verbatim_subframes = false;
  1505. encoder->private_->write_callback = 0;
  1506. encoder->private_->metadata_callback = 0;
  1507. encoder->private_->client_data = 0;
  1508. }
  1509. void free_(FLAC__StreamEncoder *encoder)
  1510. {
  1511. unsigned i, channel;
  1512. FLAC__ASSERT(0 != encoder);
  1513. for(i = 0; i < encoder->protected_->channels; i++) {
  1514. if(0 != encoder->private_->integer_signal_unaligned[i]) {
  1515. free(encoder->private_->integer_signal_unaligned[i]);
  1516. encoder->private_->integer_signal_unaligned[i] = 0;
  1517. }
  1518. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  1519. if(0 != encoder->private_->real_signal_unaligned[i]) {
  1520. free(encoder->private_->real_signal_unaligned[i]);
  1521. encoder->private_->real_signal_unaligned[i] = 0;
  1522. }
  1523. #endif
  1524. }
  1525. for(i = 0; i < 2; i++) {
  1526. if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
  1527. free(encoder->private_->integer_signal_mid_side_unaligned[i]);
  1528. encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
  1529. }
  1530. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  1531. if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
  1532. free(encoder->private_->real_signal_mid_side_unaligned[i]);
  1533. encoder->private_->real_signal_mid_side_unaligned[i] = 0;
  1534. }
  1535. #endif