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

Windows CE

开发平台:

C/C++

  1. }
  2. for(channel = 0; channel < encoder->protected_->channels; channel++) {
  3. for(i = 0; i < 2; i++) {
  4. if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
  5. free(encoder->private_->residual_workspace_unaligned[channel][i]);
  6. encoder->private_->residual_workspace_unaligned[channel][i] = 0;
  7. }
  8. }
  9. }
  10. for(channel = 0; channel < 2; channel++) {
  11. for(i = 0; i < 2; i++) {
  12. if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
  13. free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
  14. encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
  15. }
  16. }
  17. }
  18. if(0 != encoder->private_->abs_residual_unaligned) {
  19. free(encoder->private_->abs_residual_unaligned);
  20. encoder->private_->abs_residual_unaligned = 0;
  21. }
  22. if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
  23. free(encoder->private_->abs_residual_partition_sums_unaligned);
  24. encoder->private_->abs_residual_partition_sums_unaligned = 0;
  25. }
  26. if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
  27. free(encoder->private_->raw_bits_per_partition_unaligned);
  28. encoder->private_->raw_bits_per_partition_unaligned = 0;
  29. }
  30. if(encoder->protected_->verify) {
  31. for(i = 0; i < encoder->protected_->channels; i++) {
  32. if(0 != encoder->private_->verify.input_fifo.data[i]) {
  33. free(encoder->private_->verify.input_fifo.data[i]);
  34. encoder->private_->verify.input_fifo.data[i] = 0;
  35. }
  36. }
  37. }
  38. FLAC__bitbuffer_free(encoder->private_->frame);
  39. }
  40. FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
  41. {
  42. FLAC__bool ok;
  43. unsigned i, channel;
  44. FLAC__ASSERT(new_size > 0);
  45. FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
  46. FLAC__ASSERT(encoder->private_->current_sample_number == 0);
  47. /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
  48. if(new_size <= encoder->private_->input_capacity)
  49. return true;
  50. ok = true;
  51. /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
  52.  * requires that the input arrays (in our case the integer signals)
  53.  * have a buffer of up to 3 zeroes in front (at negative indices) for
  54.  * alignment purposes; we use 4 to keep the data well-aligned.
  55.  */
  56. for(i = 0; ok && i < encoder->protected_->channels; i++) {
  57. ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
  58. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  59. if(encoder->protected_->max_lpc_order > 0)
  60. ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
  61. #endif
  62. memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
  63. encoder->private_->integer_signal[i] += 4;
  64. }
  65. for(i = 0; ok && i < 2; i++) {
  66. ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]);
  67. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  68. if(encoder->protected_->max_lpc_order > 0)
  69. ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]);
  70. #endif
  71. memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
  72. encoder->private_->integer_signal_mid_side[i] += 4;
  73. }
  74. for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
  75. for(i = 0; ok && i < 2; i++) {
  76. ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
  77. }
  78. }
  79. for(channel = 0; ok && channel < 2; channel++) {
  80. for(i = 0; ok && i < 2; i++) {
  81. ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_mid_side_unaligned[channel][i], &encoder->private_->residual_workspace_mid_side[channel][i]);
  82. }
  83. }
  84. ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
  85. if(encoder->private_->precompute_partition_sums || encoder->protected_->do_escape_coding) /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
  86. ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
  87. if(encoder->protected_->do_escape_coding)
  88. ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
  89. if(ok)
  90. encoder->private_->input_capacity = new_size;
  91. else
  92. encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
  93. return ok;
  94. }
  95. FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
  96. {
  97. const FLAC__byte *buffer;
  98. unsigned bytes;
  99. FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
  100. FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
  101. if(encoder->protected_->verify) {
  102. encoder->private_->verify.output.data = buffer;
  103. encoder->private_->verify.output.bytes = bytes;
  104. if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
  105. encoder->private_->verify.needs_magic_hack = true;
  106. }
  107. else {
  108. if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) {
  109. FLAC__bitbuffer_release_buffer(encoder->private_->frame);
  110. if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
  111. encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
  112. return false;
  113. }
  114. }
  115. }
  116. if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
  117. FLAC__bitbuffer_release_buffer(encoder->private_->frame);
  118. encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
  119. return false;
  120. }
  121. FLAC__bitbuffer_release_buffer(encoder->private_->frame);
  122. if(samples > 0) {
  123. encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
  124. encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
  125. }
  126. return true;
  127. }
  128. FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
  129. {
  130. FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
  131. /*
  132.  * Accumulate raw signal to the MD5 signature
  133.  */
  134. if(!FLAC__MD5Accumulate(&encoder->private_->md5context, (const FLAC__int32 * const *)encoder->private_->integer_signal, encoder->protected_->channels, encoder->protected_->blocksize, (encoder->protected_->bits_per_sample+7) / 8)) {
  135. encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
  136. return false;
  137. }
  138. /*
  139.  * Process the frame header and subframes into the frame bitbuffer
  140.  */
  141. if(!process_subframes_(encoder, is_last_frame)) {
  142. /* the above function sets the state for us in case of an error */
  143. return false;
  144. }
  145. /*
  146.  * Zero-pad the frame to a byte_boundary
  147.  */
  148. if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) {
  149. encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
  150. return false;
  151. }
  152. /*
  153.  * CRC-16 the whole thing
  154.  */
  155. FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
  156. FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN);
  157. /*
  158.  * Write it
  159.  */
  160. if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) {
  161. /* the above function sets the state for us in case of an error */
  162. return false;
  163. }
  164. /*
  165.  * Get ready for the next frame
  166.  */
  167. encoder->private_->current_sample_number = 0;
  168. encoder->private_->current_frame_number++;
  169. encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
  170. return true;
  171. }
  172. FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
  173. {
  174. FLAC__FrameHeader frame_header;
  175. unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
  176. FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
  177. /*
  178.  * Calculate the min,max Rice partition orders
  179.  */
  180. if(is_last_frame) {
  181. max_partition_order = 0;
  182. }
  183. else {
  184. max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
  185. max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
  186. }
  187. min_partition_order = min(min_partition_order, max_partition_order);
  188. precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding);
  189. /*
  190.  * Setup the frame
  191.  */
  192. if(!FLAC__bitbuffer_clear(encoder->private_->frame)) {
  193. encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
  194. return false;
  195. }
  196. frame_header.blocksize = encoder->protected_->blocksize;
  197. frame_header.sample_rate = encoder->protected_->sample_rate;
  198. frame_header.channels = encoder->protected_->channels;
  199. frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
  200. frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
  201. frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
  202. frame_header.number.frame_number = encoder->private_->current_frame_number;
  203. /*
  204.  * Figure out what channel assignments to try
  205.  */
  206. if(encoder->protected_->do_mid_side_stereo) {
  207. if(encoder->protected_->loose_mid_side_stereo) {
  208. if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
  209. do_independent = true;
  210. do_mid_side = true;
  211. }
  212. else {
  213. do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
  214. do_mid_side = !do_independent;
  215. }
  216. }
  217. else {
  218. do_independent = true;
  219. do_mid_side = true;
  220. }
  221. }
  222. else {
  223. do_independent = true;
  224. do_mid_side = false;
  225. }
  226. FLAC__ASSERT(do_independent || do_mid_side);
  227. /*
  228.  * Check for wasted bits; set effective bps for each subframe
  229.  */
  230. if(do_independent) {
  231. for(channel = 0; channel < encoder->protected_->channels; channel++) {
  232. const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
  233. encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
  234. encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
  235. }
  236. }
  237. if(do_mid_side) {
  238. FLAC__ASSERT(encoder->protected_->channels == 2);
  239. for(channel = 0; channel < 2; channel++) {
  240. const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
  241. encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
  242. encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
  243. }
  244. }
  245. /*
  246.  * First do a normal encoding pass of each independent channel
  247.  */
  248. if(do_independent) {
  249. for(channel = 0; channel < encoder->protected_->channels; channel++) {
  250. if(!
  251. process_subframe_(
  252. encoder,
  253. min_partition_order,
  254. max_partition_order,
  255. precompute_partition_sums,
  256. &frame_header,
  257. encoder->private_->subframe_bps[channel],
  258. encoder->private_->integer_signal[channel],
  259. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  260. encoder->private_->real_signal[channel],
  261. #endif
  262. encoder->private_->subframe_workspace_ptr[channel],
  263. encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
  264. encoder->private_->residual_workspace[channel],
  265. encoder->private_->best_subframe+channel,
  266. encoder->private_->best_subframe_bits+channel
  267. )
  268. )
  269. return false;
  270. }
  271. }
  272. /*
  273.  * Now do mid and side channels if requested
  274.  */
  275. if(do_mid_side) {
  276. FLAC__ASSERT(encoder->protected_->channels == 2);
  277. for(channel = 0; channel < 2; channel++) {
  278. if(!
  279. process_subframe_(
  280. encoder,
  281. min_partition_order,
  282. max_partition_order,
  283. precompute_partition_sums,
  284. &frame_header,
  285. encoder->private_->subframe_bps_mid_side[channel],
  286. encoder->private_->integer_signal_mid_side[channel],
  287. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  288. encoder->private_->real_signal_mid_side[channel],
  289. #endif
  290. encoder->private_->subframe_workspace_ptr_mid_side[channel],
  291. encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
  292. encoder->private_->residual_workspace_mid_side[channel],
  293. encoder->private_->best_subframe_mid_side+channel,
  294. encoder->private_->best_subframe_bits_mid_side+channel
  295. )
  296. )
  297. return false;
  298. }
  299. }
  300. /*
  301.  * Compose the frame bitbuffer
  302.  */
  303. if(do_mid_side) {
  304. unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
  305. FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
  306. FLAC__ChannelAssignment channel_assignment;
  307. FLAC__ASSERT(encoder->protected_->channels == 2);
  308. if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
  309. channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
  310. }
  311. else {
  312. unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
  313. unsigned min_bits;
  314. FLAC__ChannelAssignment ca;
  315. FLAC__ASSERT(do_independent && do_mid_side);
  316. /* We have to figure out which channel assignent results in the smallest frame */
  317. bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits         [0] + encoder->private_->best_subframe_bits         [1];
  318. bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE  ] = encoder->private_->best_subframe_bits         [0] + encoder->private_->best_subframe_bits_mid_side[1];
  319. bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits         [1] + encoder->private_->best_subframe_bits_mid_side[1];
  320. bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE   ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
  321. for(channel_assignment = (FLAC__ChannelAssignment)0, min_bits = bits[0], ca = (FLAC__ChannelAssignment)1; (int)ca <= 3; ca = (FLAC__ChannelAssignment)((int)ca + 1)) {
  322. if(bits[ca] < min_bits) {
  323. min_bits = bits[ca];
  324. channel_assignment = ca;
  325. }
  326. }
  327. }
  328. frame_header.channel_assignment = channel_assignment;
  329. if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, encoder->private_->frame)) {
  330. encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
  331. return false;
  332. }
  333. switch(channel_assignment) {
  334. case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
  335. left_subframe  = &encoder->private_->subframe_workspace         [0][encoder->private_->best_subframe         [0]];
  336. right_subframe = &encoder->private_->subframe_workspace         [1][encoder->private_->best_subframe         [1]];
  337. break;
  338. case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
  339. left_subframe  = &encoder->private_->subframe_workspace         [0][encoder->private_->best_subframe         [0]];
  340. right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
  341. break;
  342. case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
  343. left_subframe  = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
  344. right_subframe = &encoder->private_->subframe_workspace         [1][encoder->private_->best_subframe         [1]];
  345. break;
  346. case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
  347. left_subframe  = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
  348. right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
  349. break;
  350. default:
  351. FLAC__ASSERT(0);
  352. }
  353. switch(channel_assignment) {
  354. case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
  355. left_bps  = encoder->private_->subframe_bps         [0];
  356. right_bps = encoder->private_->subframe_bps         [1];
  357. break;
  358. case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
  359. left_bps  = encoder->private_->subframe_bps         [0];
  360. right_bps = encoder->private_->subframe_bps_mid_side[1];
  361. break;
  362. case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
  363. left_bps  = encoder->private_->subframe_bps_mid_side[1];
  364. right_bps = encoder->private_->subframe_bps         [1];
  365. break;
  366. case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
  367. left_bps  = encoder->private_->subframe_bps_mid_side[0];
  368. right_bps = encoder->private_->subframe_bps_mid_side[1];
  369. break;
  370. default:
  371. FLAC__ASSERT(0);
  372. }
  373. /* note that encoder_add_subframe_ sets the state for us in case of an error */
  374. if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame))
  375. return false;
  376. if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame))
  377. return false;
  378. }
  379. else {
  380. if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, encoder->private_->frame)) {
  381. encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
  382. return false;
  383. }
  384. for(channel = 0; channel < encoder->protected_->channels; channel++) {
  385. if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
  386. /* the above function sets the state for us in case of an error */
  387. return false;
  388. }
  389. }
  390. }
  391. if(encoder->protected_->loose_mid_side_stereo) {
  392. encoder->private_->loose_mid_side_stereo_frame_count++;
  393. if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
  394. encoder->private_->loose_mid_side_stereo_frame_count = 0;
  395. }
  396. encoder->private_->last_channel_assignment = frame_header.channel_assignment;
  397. return true;
  398. }
  399. FLAC__bool process_subframe_(
  400. FLAC__StreamEncoder *encoder,
  401. unsigned min_partition_order,
  402. unsigned max_partition_order,
  403. FLAC__bool precompute_partition_sums,
  404. const FLAC__FrameHeader *frame_header,
  405. unsigned subframe_bps,
  406. const FLAC__int32 integer_signal[],
  407. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  408. const FLAC__real real_signal[],
  409. #endif
  410. FLAC__Subframe *subframe[2],
  411. FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
  412. FLAC__int32 *residual[2],
  413. unsigned *best_subframe,
  414. unsigned *best_bits
  415. )
  416. {
  417. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  418. FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
  419. #else
  420. FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
  421. #endif
  422. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  423. FLAC__double lpc_residual_bits_per_sample;
  424. FLAC__real autoc[FLAC__MAX_LPC_ORDER+1]; /* WATCHOUT: the size is important even though encoder->protected_->max_lpc_order might be less; some asm routines need all the space */
  425. FLAC__double lpc_error[FLAC__MAX_LPC_ORDER];
  426. unsigned min_lpc_order, max_lpc_order, lpc_order;
  427. unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
  428. #endif
  429. unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
  430. unsigned rice_parameter;
  431. unsigned _candidate_bits, _best_bits;
  432. unsigned _best_subframe;
  433. /* verbatim subframe is the baseline against which we measure other compressed subframes */
  434. _best_subframe = 0;
  435. if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
  436. _best_bits = UINT_MAX;
  437. else
  438. _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
  439. if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
  440. unsigned signal_is_constant = false;
  441. guess_fixed_order = encoder->private_->local_fixed_compute_best_predictor(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample);
  442. /* check for constant subframe */
  443. if(
  444. !encoder->private_->disable_constant_subframes &&
  445. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  446. fixed_residual_bits_per_sample[1] == 0.0
  447. #else
  448. fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
  449. #endif
  450. ) {
  451. /* the above means it's possible all samples are the same value; now double-check it: */
  452. unsigned i;
  453. signal_is_constant = true;
  454. for(i = 1; i < frame_header->blocksize; i++) {
  455. if(integer_signal[0] != integer_signal[i]) {
  456. signal_is_constant = false;
  457. break;
  458. }
  459. }
  460. }
  461. if(signal_is_constant) {
  462. _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
  463. if(_candidate_bits < _best_bits) {
  464. _best_subframe = !_best_subframe;
  465. _best_bits = _candidate_bits;
  466. }
  467. }
  468. else {
  469. if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
  470. /* encode fixed */
  471. if(encoder->protected_->do_exhaustive_model_search) {
  472. min_fixed_order = 0;
  473. max_fixed_order = FLAC__MAX_FIXED_ORDER;
  474. }
  475. else {
  476. min_fixed_order = max_fixed_order = guess_fixed_order;
  477. }
  478. for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
  479. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  480. if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps)
  481. continue; /* don't even try */
  482. rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > 0.0)? (unsigned)(fixed_residual_bits_per_sample[fixed_order]+0.5) : 0; /* 0.5 is for rounding */
  483. #else
  484. if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
  485. continue; /* don't even try */
  486. rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > FLAC__FP_ZERO)? (unsigned)FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]+FLAC__FP_ONE_HALF) : 0; /* 0.5 is for rounding */
  487. #endif
  488. #ifndef FLAC__SYMMETRIC_RICE
  489. rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
  490. #endif
  491. if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
  492. #ifdef DEBUG_VERBOSE
  493. fprintf(stderr, "clipping rice_parameter (%u -> %u) @0n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
  494. #endif
  495. rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
  496. }
  497. _candidate_bits =
  498. evaluate_fixed_subframe_(
  499. encoder,
  500. integer_signal,
  501. residual[!_best_subframe],
  502. encoder->private_->abs_residual,
  503. encoder->private_->abs_residual_partition_sums,
  504. encoder->private_->raw_bits_per_partition,
  505. frame_header->blocksize,
  506. subframe_bps,
  507. fixed_order,
  508. rice_parameter,
  509. min_partition_order,
  510. max_partition_order,
  511. precompute_partition_sums,
  512. encoder->protected_->do_escape_coding,
  513. encoder->protected_->rice_parameter_search_dist,
  514. subframe[!_best_subframe],
  515. partitioned_rice_contents[!_best_subframe]
  516. );
  517. if(_candidate_bits < _best_bits) {
  518. _best_subframe = !_best_subframe;
  519. _best_bits = _candidate_bits;
  520. }
  521. }
  522. }
  523. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  524. /* encode lpc */
  525. if(encoder->protected_->max_lpc_order > 0) {
  526. if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
  527. max_lpc_order = frame_header->blocksize-1;
  528. else
  529. max_lpc_order = encoder->protected_->max_lpc_order;
  530. if(max_lpc_order > 0) {
  531. encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
  532. /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
  533. if(autoc[0] != 0.0) {
  534. FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
  535. if(encoder->protected_->do_exhaustive_model_search) {
  536. min_lpc_order = 1;
  537. }
  538. else {
  539. unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
  540. min_lpc_order = max_lpc_order = guess_lpc_order;
  541. }
  542. for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
  543. lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
  544. if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
  545. continue; /* don't even try */
  546. rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
  547. #ifndef FLAC__SYMMETRIC_RICE
  548. rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
  549. #endif
  550. if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
  551. #ifdef DEBUG_VERBOSE
  552. fprintf(stderr, "clipping rice_parameter (%u -> %u) @1n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
  553. #endif
  554. rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
  555. }
  556. if(encoder->protected_->do_qlp_coeff_prec_search) {
  557. min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
  558. /* ensure a 32-bit datapath throughout for 16bps or less */
  559. if(subframe_bps <= 16)
  560. max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
  561. else
  562. max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
  563. }
  564. else {
  565. min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
  566. }
  567. for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
  568. _candidate_bits =
  569. evaluate_lpc_subframe_(
  570. encoder,
  571. integer_signal,
  572. residual[!_best_subframe],
  573. encoder->private_->abs_residual,
  574. encoder->private_->abs_residual_partition_sums,
  575. encoder->private_->raw_bits_per_partition,
  576. encoder->private_->lp_coeff[lpc_order-1],
  577. frame_header->blocksize,
  578. subframe_bps,
  579. lpc_order,
  580. qlp_coeff_precision,
  581. rice_parameter,
  582. min_partition_order,
  583. max_partition_order,
  584. precompute_partition_sums,
  585. encoder->protected_->do_escape_coding,
  586. encoder->protected_->rice_parameter_search_dist,
  587. subframe[!_best_subframe],
  588. partitioned_rice_contents[!_best_subframe]
  589. );
  590. if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
  591. if(_candidate_bits < _best_bits) {
  592. _best_subframe = !_best_subframe;
  593. _best_bits = _candidate_bits;
  594. }
  595. }
  596. }
  597. }
  598. }
  599. }
  600. }
  601. #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
  602. }
  603. }
  604. /* under rare circumstances this can happen when all but lpc subframe types are disabled: */
  605. if(_best_bits == UINT_MAX) {
  606. FLAC__ASSERT(_best_subframe == 0);
  607. _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
  608. }
  609. *best_subframe = _best_subframe;
  610. *best_bits = _best_bits;
  611. return true;
  612. }
  613. FLAC__bool add_subframe_(
  614. FLAC__StreamEncoder *encoder,
  615. const FLAC__FrameHeader *frame_header,
  616. unsigned subframe_bps,
  617. const FLAC__Subframe *subframe,
  618. FLAC__BitBuffer *frame
  619. )
  620. {
  621. switch(subframe->type) {
  622. case FLAC__SUBFRAME_TYPE_CONSTANT:
  623. if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
  624. encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
  625. return false;
  626. }
  627. break;
  628. case FLAC__SUBFRAME_TYPE_FIXED:
  629. if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
  630. encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
  631. return false;
  632. }
  633. break;
  634. case FLAC__SUBFRAME_TYPE_LPC:
  635. if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
  636. encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
  637. return false;
  638. }
  639. break;
  640. case FLAC__SUBFRAME_TYPE_VERBATIM:
  641. if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
  642. encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
  643. return false;
  644. }
  645. break;
  646. default:
  647. FLAC__ASSERT(0);
  648. }
  649. return true;
  650. }
  651. unsigned evaluate_constant_subframe_(
  652. const FLAC__int32 signal,
  653. unsigned subframe_bps,
  654. FLAC__Subframe *subframe
  655. )
  656. {
  657. subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
  658. subframe->data.constant.value = signal;
  659. return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
  660. }
  661. unsigned evaluate_fixed_subframe_(
  662. FLAC__StreamEncoder *encoder,
  663. const FLAC__int32 signal[],
  664. FLAC__int32 residual[],
  665. FLAC__uint32 abs_residual[],
  666. FLAC__uint64 abs_residual_partition_sums[],
  667. unsigned raw_bits_per_partition[],
  668. unsigned blocksize,
  669. unsigned subframe_bps,
  670. unsigned order,
  671. unsigned rice_parameter,
  672. unsigned min_partition_order,
  673. unsigned max_partition_order,
  674. FLAC__bool precompute_partition_sums,
  675. FLAC__bool do_escape_coding,
  676. unsigned rice_parameter_search_dist,
  677. FLAC__Subframe *subframe,
  678. FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
  679. )
  680. {
  681. unsigned i, residual_bits;
  682. const unsigned residual_samples = blocksize - order;
  683. FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
  684. subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
  685. subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
  686. subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
  687. subframe->data.fixed.residual = residual;
  688. residual_bits =
  689. find_best_partition_order_(
  690. encoder->private_,
  691. residual,
  692. abs_residual,
  693. abs_residual_partition_sums,
  694. raw_bits_per_partition,
  695. residual_samples,
  696. order,
  697. rice_parameter,
  698. min_partition_order,
  699. max_partition_order,
  700. precompute_partition_sums,
  701. do_escape_coding,
  702. rice_parameter_search_dist,
  703. &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
  704. );
  705. subframe->data.fixed.order = order;
  706. for(i = 0; i < order; i++)
  707. subframe->data.fixed.warmup[i] = signal[i];
  708. return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
  709. }
  710. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  711. unsigned evaluate_lpc_subframe_(
  712. FLAC__StreamEncoder *encoder,
  713. const FLAC__int32 signal[],
  714. FLAC__int32 residual[],
  715. FLAC__uint32 abs_residual[],
  716. FLAC__uint64 abs_residual_partition_sums[],
  717. unsigned raw_bits_per_partition[],
  718. const FLAC__real lp_coeff[],
  719. unsigned blocksize,
  720. unsigned subframe_bps,
  721. unsigned order,
  722. unsigned qlp_coeff_precision,
  723. unsigned rice_parameter,
  724. unsigned min_partition_order,
  725. unsigned max_partition_order,
  726. FLAC__bool precompute_partition_sums,
  727. FLAC__bool do_escape_coding,
  728. unsigned rice_parameter_search_dist,
  729. FLAC__Subframe *subframe,
  730. FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
  731. )
  732. {
  733. FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
  734. unsigned i, residual_bits;
  735. int quantization, ret;
  736. const unsigned residual_samples = blocksize - order;
  737. /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */
  738. if(subframe_bps <= 16) {
  739. FLAC__ASSERT(order > 0);
  740. FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
  741. qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
  742. }
  743. ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
  744. if(ret != 0)
  745. return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
  746. if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
  747. if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
  748. encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
  749. else
  750. encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
  751. else
  752. encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
  753. subframe->type = FLAC__SUBFRAME_TYPE_LPC;
  754. subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
  755. subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
  756. subframe->data.lpc.residual = residual;
  757. residual_bits =
  758. find_best_partition_order_(
  759. encoder->private_,
  760. residual,
  761. abs_residual,
  762. abs_residual_partition_sums,
  763. raw_bits_per_partition,
  764. residual_samples,
  765. order,
  766. rice_parameter,
  767. min_partition_order,
  768. max_partition_order,
  769. precompute_partition_sums,
  770. do_escape_coding,
  771. rice_parameter_search_dist,
  772. &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
  773. );
  774. subframe->data.lpc.order = order;
  775. subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
  776. subframe->data.lpc.quantization_level = quantization;
  777. memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
  778. for(i = 0; i < order; i++)
  779. subframe->data.lpc.warmup[i] = signal[i];
  780. return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits;
  781. }
  782. #endif
  783. unsigned evaluate_verbatim_subframe_(
  784. const FLAC__int32 signal[],
  785. unsigned blocksize,
  786. unsigned subframe_bps,
  787. FLAC__Subframe *subframe
  788. )
  789. {
  790. subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
  791. subframe->data.verbatim.data = signal;
  792. return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
  793. }
  794. unsigned find_best_partition_order_(
  795. FLAC__StreamEncoderPrivate *private_,
  796. const FLAC__int32 residual[],
  797. FLAC__uint32 abs_residual[],
  798. FLAC__uint64 abs_residual_partition_sums[],
  799. unsigned raw_bits_per_partition[],
  800. unsigned residual_samples,
  801. unsigned predictor_order,
  802. unsigned rice_parameter,
  803. unsigned min_partition_order,
  804. unsigned max_partition_order,
  805. FLAC__bool precompute_partition_sums,
  806. FLAC__bool do_escape_coding,
  807. unsigned rice_parameter_search_dist,
  808. FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
  809. )
  810. {
  811. FLAC__int32 r;
  812. unsigned residual_bits, best_residual_bits = 0;
  813. unsigned residual_sample;
  814. unsigned best_parameters_index = 0;
  815. const unsigned blocksize = residual_samples + predictor_order;
  816. /* compute abs(residual) for use later */
  817. for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
  818. r = residual[residual_sample];
  819. abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
  820. }
  821. max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
  822. min_partition_order = min(min_partition_order, max_partition_order);
  823. if(precompute_partition_sums) {
  824. int partition_order;
  825. unsigned sum;
  826. precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
  827. if(do_escape_coding)
  828. precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
  829. for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
  830. #ifdef DONT_ESTIMATE_RICE_BITS
  831. if(!
  832. set_partitioned_rice_with_precompute_(
  833. residual,
  834. abs_residual_partition_sums+sum,
  835. raw_bits_per_partition+sum,
  836. residual_samples,
  837. predictor_order,
  838. rice_parameter,
  839. rice_parameter_search_dist,
  840. (unsigned)partition_order,
  841. do_escape_coding,
  842. &private_->partitioned_rice_contents_extra[!best_parameters_index],
  843. &residual_bits
  844. )
  845. )
  846. #else
  847. if(!
  848. set_partitioned_rice_with_precompute_(
  849. abs_residual,
  850. abs_residual_partition_sums+sum,
  851. raw_bits_per_partition+sum,
  852. residual_samples,
  853. predictor_order,
  854. rice_parameter,
  855. rice_parameter_search_dist,
  856. (unsigned)partition_order,
  857. do_escape_coding,
  858. &private_->partitioned_rice_contents_extra[!best_parameters_index],
  859. &residual_bits
  860. )
  861. )
  862. #endif
  863. {
  864. FLAC__ASSERT(best_residual_bits != 0);
  865. break;
  866. }
  867. sum += 1u << partition_order;
  868. if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
  869. best_residual_bits = residual_bits;
  870. best_parameters_index = !best_parameters_index;
  871. best_partitioned_rice->order = partition_order;
  872. }
  873. }
  874. }
  875. else {
  876. unsigned partition_order;
  877. for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
  878. #ifdef DONT_ESTIMATE_RICE_BITS
  879. if(!
  880. set_partitioned_rice_(
  881. abs_residual,
  882. residual,
  883. residual_samples,
  884. predictor_order,
  885. rice_parameter,
  886. rice_parameter_search_dist,
  887. partition_order,
  888. &private_->partitioned_rice_contents_extra[!best_parameters_index],
  889. &residual_bits
  890. )
  891. )
  892. #else
  893. if(!
  894. set_partitioned_rice_(
  895. abs_residual,
  896. residual_samples,
  897. predictor_order,
  898. rice_parameter,
  899. rice_parameter_search_dist,
  900. partition_order,
  901. &private_->partitioned_rice_contents_extra[!best_parameters_index],
  902. &residual_bits
  903. )
  904. )
  905. #endif
  906. {
  907. FLAC__ASSERT(best_residual_bits != 0);
  908. break;
  909. }
  910. if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
  911. best_residual_bits = residual_bits;
  912. best_parameters_index = !best_parameters_index;
  913. best_partitioned_rice->order = partition_order;
  914. }
  915. }
  916. }
  917. /*
  918.  * We are allowed to de-const the pointer based on our special knowledge;
  919.  * it is const to the outside world.
  920.  */
  921. {
  922. FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
  923. FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
  924. memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
  925. memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
  926. }
  927. return best_residual_bits;
  928. }
  929. void precompute_partition_info_sums_(
  930. const FLAC__uint32 abs_residual[],
  931. FLAC__uint64 abs_residual_partition_sums[],
  932. unsigned residual_samples,
  933. unsigned predictor_order,
  934. unsigned min_partition_order,
  935. unsigned max_partition_order
  936. )
  937. {
  938. int partition_order;
  939. unsigned from_partition, to_partition = 0;
  940. const unsigned blocksize = residual_samples + predictor_order;
  941. /* first do max_partition_order */
  942. for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
  943. FLAC__uint64 abs_residual_partition_sum;
  944. FLAC__uint32 abs_r;
  945. unsigned partition, partition_sample, partition_samples, residual_sample;
  946. const unsigned partitions = 1u << partition_order;
  947. const unsigned default_partition_samples = blocksize >> partition_order;
  948. FLAC__ASSERT(default_partition_samples > predictor_order);
  949. for(partition = residual_sample = 0; partition < partitions; partition++) {
  950. partition_samples = default_partition_samples;
  951. if(partition == 0)
  952. partition_samples -= predictor_order;
  953. abs_residual_partition_sum = 0;
  954. for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
  955. abs_r = abs_residual[residual_sample];
  956. abs_residual_partition_sum += abs_r;
  957. residual_sample++;
  958. }
  959. abs_residual_partition_sums[partition] = abs_residual_partition_sum;
  960. }
  961. to_partition = partitions;
  962. break;
  963. }
  964. /* now merge partitions for lower orders */
  965. for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
  966. FLAC__uint64 s;
  967. unsigned i;
  968. const unsigned partitions = 1u << partition_order;
  969. for(i = 0; i < partitions; i++) {
  970. s = abs_residual_partition_sums[from_partition];
  971. from_partition++;
  972. abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
  973. from_partition++;
  974. to_partition++;
  975. }
  976. }
  977. }
  978. void precompute_partition_info_escapes_(
  979. const FLAC__int32 residual[],
  980. unsigned raw_bits_per_partition[],
  981. unsigned residual_samples,
  982. unsigned predictor_order,
  983. unsigned min_partition_order,
  984. unsigned max_partition_order
  985. )
  986. {
  987. int partition_order;
  988. unsigned from_partition, to_partition = 0;
  989. const unsigned blocksize = residual_samples + predictor_order;
  990. /* first do max_partition_order */
  991. for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
  992. FLAC__int32 r, residual_partition_min, residual_partition_max;
  993. unsigned silog2_min, silog2_max;
  994. unsigned partition, partition_sample, partition_samples, residual_sample;
  995. const unsigned partitions = 1u << partition_order;
  996. const unsigned default_partition_samples = blocksize >> partition_order;
  997. FLAC__ASSERT(default_partition_samples > predictor_order);
  998. for(partition = residual_sample = 0; partition < partitions; partition++) {
  999. partition_samples = default_partition_samples;
  1000. if(partition == 0)
  1001. partition_samples -= predictor_order;
  1002. residual_partition_min = residual_partition_max = 0;
  1003. for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
  1004. r = residual[residual_sample];
  1005. if(r < residual_partition_min)
  1006. residual_partition_min = r;
  1007. else if(r > residual_partition_max)
  1008. residual_partition_max = r;
  1009. residual_sample++;
  1010. }
  1011. silog2_min = FLAC__bitmath_silog2(residual_partition_min);
  1012. silog2_max = FLAC__bitmath_silog2(residual_partition_max);
  1013. raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
  1014. }
  1015. to_partition = partitions;
  1016. break;
  1017. }
  1018. /* now merge partitions for lower orders */
  1019. for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
  1020. unsigned m;
  1021. unsigned i;
  1022. const unsigned partitions = 1u << partition_order;
  1023. for(i = 0; i < partitions; i++) {
  1024. m = raw_bits_per_partition[from_partition];
  1025. from_partition++;
  1026. raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
  1027. from_partition++;
  1028. to_partition++;
  1029. }
  1030. }
  1031. }
  1032. #ifdef VARIABLE_RICE_BITS
  1033. #undef VARIABLE_RICE_BITS
  1034. #endif
  1035. #ifndef DONT_ESTIMATE_RICE_BITS
  1036. #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
  1037. #endif
  1038. #ifdef DONT_ESTIMATE_RICE_BITS
  1039. FLAC__bool set_partitioned_rice_(
  1040. const FLAC__uint32 abs_residual[],
  1041. const FLAC__int32 residual[],
  1042. const unsigned residual_samples,
  1043. const unsigned predictor_order,
  1044. const unsigned suggested_rice_parameter,
  1045. const unsigned rice_parameter_search_dist,
  1046. const unsigned partition_order,
  1047. FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
  1048. unsigned *bits
  1049. )
  1050. #else
  1051. FLAC__bool set_partitioned_rice_(
  1052. const FLAC__uint32 abs_residual[],
  1053. const unsigned residual_samples,
  1054. const unsigned predictor_order,
  1055. const unsigned suggested_rice_parameter,
  1056. const unsigned rice_parameter_search_dist,
  1057. const unsigned partition_order,
  1058. FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
  1059. unsigned *bits
  1060. )
  1061. #endif
  1062. {
  1063. unsigned rice_parameter, partition_bits;
  1064. #ifndef NO_RICE_SEARCH
  1065. unsigned best_partition_bits;
  1066. unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
  1067. #endif
  1068. unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
  1069. unsigned *parameters;
  1070. FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
  1071. FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
  1072. parameters = partitioned_rice_contents->parameters;
  1073. if(partition_order == 0) {
  1074. unsigned i;
  1075. #ifndef NO_RICE_SEARCH
  1076. if(rice_parameter_search_dist) {
  1077. if(suggested_rice_parameter < rice_parameter_search_dist)
  1078. min_rice_parameter = 0;
  1079. else
  1080. min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
  1081. max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
  1082. if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
  1083. #ifdef DEBUG_VERBOSE
  1084. fprintf(stderr, "clipping rice_parameter (%u -> %u) @2n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
  1085. #endif
  1086. max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
  1087. }
  1088. }
  1089. else
  1090. min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
  1091. best_partition_bits = 0xffffffff;
  1092. for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
  1093. #endif
  1094. #ifdef VARIABLE_RICE_BITS
  1095. #ifdef FLAC__SYMMETRIC_RICE
  1096. partition_bits = (2+rice_parameter) * residual_samples;
  1097. #else
  1098. const unsigned rice_parameter_estimate = rice_parameter-1;
  1099. partition_bits = (1+rice_parameter) * residual_samples;
  1100. #endif
  1101. #else
  1102. partition_bits = 0;
  1103. #endif
  1104. partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
  1105. for(i = 0; i < residual_samples; i++) {
  1106. #ifdef VARIABLE_RICE_BITS
  1107. #ifdef FLAC__SYMMETRIC_RICE
  1108. partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
  1109. #else
  1110. partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
  1111. #endif
  1112. #else
  1113. partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
  1114. #endif
  1115. }
  1116. #ifndef NO_RICE_SEARCH
  1117. if(partition_bits < best_partition_bits) {
  1118. best_rice_parameter = rice_parameter;
  1119. best_partition_bits = partition_bits;
  1120. }
  1121. }
  1122. #endif
  1123. parameters[0] = best_rice_parameter;
  1124. bits_ += best_partition_bits;
  1125. }
  1126. else {
  1127. unsigned partition, residual_sample, save_residual_sample, partition_sample;
  1128. unsigned partition_samples;
  1129. FLAC__uint64 mean, k;
  1130. const unsigned partitions = 1u << partition_order;
  1131. for(partition = residual_sample = 0; partition < partitions; partition++) {
  1132. partition_samples = (residual_samples+predictor_order) >> partition_order;
  1133. if(partition == 0) {
  1134. if(partition_samples <= predictor_order)
  1135. return false;
  1136. else
  1137. partition_samples -= predictor_order;
  1138. }
  1139. mean = 0;
  1140. save_residual_sample = residual_sample;
  1141. for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
  1142. mean += abs_residual[residual_sample];
  1143. residual_sample = save_residual_sample;
  1144. #ifdef FLAC__SYMMETRIC_RICE
  1145. mean += partition_samples >> 1; /* for rounding effect */
  1146. mean /= partition_samples;
  1147. /* calc rice_parameter = floor(log2(mean)) */
  1148. rice_parameter = 0;
  1149. mean>>=1;
  1150. while(mean) {
  1151. rice_parameter++;
  1152. mean >>= 1;
  1153. }
  1154. #else
  1155. /* we are basically calculating the size in bits of the
  1156.  * average residual magnitude in the partition:
  1157.  *   rice_parameter = floor(log2(mean/partition_samples))
  1158.  * 'mean' is not a good name for the variable, it is
  1159.  * actually the sum of magnitudes of all residual values
  1160.  * in the partition, so the actual mean is
  1161.  * mean/partition_samples
  1162.  */
  1163. for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
  1164. ;
  1165. #endif
  1166. if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
  1167. #ifdef DEBUG_VERBOSE
  1168. fprintf(stderr, "clipping rice_parameter (%u -> %u) @3n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
  1169. #endif
  1170. rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
  1171. }
  1172. #ifndef NO_RICE_SEARCH
  1173. if(rice_parameter_search_dist) {
  1174. if(rice_parameter < rice_parameter_search_dist)
  1175. min_rice_parameter = 0;
  1176. else
  1177. min_rice_parameter = rice_parameter - rice_parameter_search_dist;
  1178. max_rice_parameter = rice_parameter + rice_parameter_search_dist;
  1179. if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
  1180. #ifdef DEBUG_VERBOSE
  1181. fprintf(stderr, "clipping rice_parameter (%u -> %u) @4n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
  1182. #endif
  1183. max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
  1184. }
  1185. }
  1186. else
  1187. min_rice_parameter = max_rice_parameter = rice_parameter;
  1188. best_partition_bits = 0xffffffff;
  1189. for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
  1190. #endif
  1191. #ifdef VARIABLE_RICE_BITS
  1192. #ifdef FLAC__SYMMETRIC_RICE
  1193. partition_bits = (2+rice_parameter) * partition_samples;
  1194. #else
  1195. const unsigned rice_parameter_estimate = rice_parameter-1;
  1196. partition_bits = (1+rice_parameter) * partition_samples;
  1197. #endif
  1198. #else
  1199. partition_bits = 0;
  1200. #endif
  1201. partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
  1202. save_residual_sample = residual_sample;
  1203. for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
  1204. #ifdef VARIABLE_RICE_BITS
  1205. #ifdef FLAC__SYMMETRIC_RICE
  1206. partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
  1207. #else
  1208. partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
  1209. #endif
  1210. #else
  1211. partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
  1212. #endif
  1213. }
  1214. #ifndef NO_RICE_SEARCH
  1215. if(rice_parameter != max_rice_parameter)
  1216. residual_sample = save_residual_sample;
  1217. if(partition_bits < best_partition_bits) {
  1218. best_rice_parameter = rice_parameter;
  1219. best_partition_bits = partition_bits;
  1220. }
  1221. }
  1222. #endif
  1223. parameters[partition] = best_rice_parameter;
  1224. bits_ += best_partition_bits;
  1225. }
  1226. }
  1227. *bits = bits_;
  1228. return true;
  1229. }
  1230. #ifdef DONT_ESTIMATE_RICE_BITS
  1231. FLAC__bool set_partitioned_rice_with_precompute_(
  1232. const FLAC__int32 residual[],
  1233. const FLAC__uint64 abs_residual_partition_sums[],
  1234. const unsigned raw_bits_per_partition[],
  1235. const unsigned residual_samples,
  1236. const unsigned predictor_order,
  1237. const unsigned suggested_rice_parameter,
  1238. const unsigned rice_parameter_search_dist,
  1239. const unsigned partition_order,
  1240. const FLAC__bool search_for_escapes,
  1241. FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
  1242. unsigned *bits
  1243. )
  1244. #else
  1245. FLAC__bool set_partitioned_rice_with_precompute_(
  1246. const FLAC__uint32 abs_residual[],
  1247. const FLAC__uint64 abs_residual_partition_sums[],
  1248. const unsigned raw_bits_per_partition[],
  1249. const unsigned residual_samples,
  1250. const unsigned predictor_order,
  1251. const unsigned suggested_rice_parameter,
  1252. const unsigned rice_parameter_search_dist,
  1253. const unsigned partition_order,
  1254. const FLAC__bool search_for_escapes,
  1255. FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
  1256. unsigned *bits
  1257. )
  1258. #endif
  1259. {
  1260. unsigned rice_parameter, partition_bits;
  1261. #ifndef NO_RICE_SEARCH
  1262. unsigned best_partition_bits;
  1263. unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
  1264. #endif
  1265. unsigned flat_bits;
  1266. unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
  1267. unsigned *parameters, *raw_bits;
  1268. FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
  1269. FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
  1270. parameters = partitioned_rice_contents->parameters;
  1271. raw_bits = partitioned_rice_contents->raw_bits;
  1272. if(partition_order == 0) {
  1273. unsigned i;
  1274. #ifndef NO_RICE_SEARCH
  1275. if(rice_parameter_search_dist) {
  1276. if(suggested_rice_parameter < rice_parameter_search_dist)
  1277. min_rice_parameter = 0;
  1278. else
  1279. min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
  1280. max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
  1281. if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
  1282. #ifdef DEBUG_VERBOSE
  1283. fprintf(stderr, "clipping rice_parameter (%u -> %u) @5n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
  1284. #endif
  1285. max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
  1286. }
  1287. }
  1288. else
  1289. min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
  1290. best_partition_bits = 0xffffffff;
  1291. for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
  1292. #endif
  1293. #ifdef VARIABLE_RICE_BITS
  1294. #ifdef FLAC__SYMMETRIC_RICE
  1295. partition_bits = (2+rice_parameter) * residual_samples;
  1296. #else
  1297. const unsigned rice_parameter_estimate = rice_parameter-1;
  1298. partition_bits = (1+rice_parameter) * residual_samples;
  1299. #endif
  1300. #else
  1301. partition_bits = 0;
  1302. #endif
  1303. partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
  1304. for(i = 0; i < residual_samples; i++) {
  1305. #ifdef VARIABLE_RICE_BITS
  1306. #ifdef FLAC__SYMMETRIC_RICE
  1307. partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
  1308. #else
  1309. partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
  1310. #endif
  1311. #else
  1312. partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
  1313. #endif
  1314. }
  1315. #ifndef NO_RICE_SEARCH
  1316. if(partition_bits < best_partition_bits) {
  1317. best_rice_parameter = rice_parameter;
  1318. best_partition_bits = partition_bits;
  1319. }
  1320. }
  1321. #endif
  1322. if(search_for_escapes) {
  1323. flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples;
  1324. if(flat_bits <= best_partition_bits) {
  1325. raw_bits[0] = raw_bits_per_partition[0];
  1326. best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
  1327. best_partition_bits = flat_bits;
  1328. }
  1329. }
  1330. parameters[0] = best_rice_parameter;
  1331. bits_ += best_partition_bits;
  1332. }
  1333. else {
  1334. unsigned partition, residual_sample, save_residual_sample, partition_sample;
  1335. unsigned partition_samples;
  1336. FLAC__uint64 mean, k;
  1337. const unsigned partitions = 1u << partition_order;
  1338. for(partition = residual_sample = 0; partition < partitions; partition++) {
  1339. partition_samples = (residual_samples+predictor_order) >> partition_order;
  1340. if(partition == 0) {
  1341. if(partition_samples <= predictor_order)
  1342. return false;
  1343. else
  1344. partition_samples -= predictor_order;
  1345. }
  1346. mean = abs_residual_partition_sums[partition];
  1347. #ifdef FLAC__SYMMETRIC_RICE
  1348. mean += partition_samples >> 1; /* for rounding effect */
  1349. mean /= partition_samples;
  1350. /* calc rice_parameter = floor(log2(mean)) */
  1351. rice_parameter = 0;
  1352. mean>>=1;
  1353. while(mean) {
  1354. rice_parameter++;
  1355. mean >>= 1;
  1356. }
  1357. #else
  1358. /* we are basically calculating the size in bits of the
  1359.  * average residual magnitude in the partition:
  1360.  *   rice_parameter = floor(log2(mean/partition_samples))
  1361.  * 'mean' is not a good name for the variable, it is
  1362.  * actually the sum of magnitudes of all residual values
  1363.  * in the partition, so the actual mean is
  1364.  * mean/partition_samples
  1365.  */
  1366. for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
  1367. ;
  1368. #endif
  1369. if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
  1370. #ifdef DEBUG_VERBOSE
  1371. fprintf(stderr, "clipping rice_parameter (%u -> %u) @6n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
  1372. #endif
  1373. rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
  1374. }
  1375. #ifndef NO_RICE_SEARCH
  1376. if(rice_parameter_search_dist) {
  1377. if(rice_parameter < rice_parameter_search_dist)
  1378. min_rice_parameter = 0;
  1379. else
  1380. min_rice_parameter = rice_parameter - rice_parameter_search_dist;
  1381. max_rice_parameter = rice_parameter + rice_parameter_search_dist;
  1382. if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
  1383. #ifdef DEBUG_VERBOSE
  1384. fprintf(stderr, "clipping rice_parameter (%u -> %u) @7n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
  1385. #endif
  1386. max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
  1387. }
  1388. }
  1389. else
  1390. min_rice_parameter = max_rice_parameter = rice_parameter;
  1391. best_partition_bits = 0xffffffff;
  1392. for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
  1393. #endif
  1394. #ifdef VARIABLE_RICE_BITS
  1395. #ifdef FLAC__SYMMETRIC_RICE
  1396. partition_bits = (2+rice_parameter) * partition_samples;
  1397. #else
  1398. const unsigned rice_parameter_estimate = rice_parameter-1;
  1399. partition_bits = (1+rice_parameter) * partition_samples;
  1400. #endif
  1401. #else
  1402. partition_bits = 0;
  1403. #endif
  1404. partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
  1405. save_residual_sample = residual_sample;
  1406. for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
  1407. #ifdef VARIABLE_RICE_BITS
  1408. #ifdef FLAC__SYMMETRIC_RICE
  1409. partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
  1410. #else
  1411. partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
  1412. #endif
  1413. #else
  1414. partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
  1415. #endif
  1416. }
  1417. #ifndef NO_RICE_SEARCH
  1418. if(rice_parameter != max_rice_parameter)
  1419. residual_sample = save_residual_sample;
  1420. if(partition_bits < best_partition_bits) {
  1421. best_rice_parameter = rice_parameter;
  1422. best_partition_bits = partition_bits;
  1423. }
  1424. }
  1425. #endif
  1426. if(search_for_escapes) {
  1427. flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples;
  1428. if(flat_bits <= best_partition_bits) {
  1429. raw_bits[partition] = raw_bits_per_partition[partition];
  1430. best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
  1431. best_partition_bits = flat_bits;
  1432. }
  1433. }
  1434. parameters[partition] = best_rice_parameter;
  1435. bits_ += best_partition_bits;
  1436. }
  1437. }
  1438. *bits = bits_;
  1439. return true;
  1440. }
  1441. unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
  1442. {
  1443. unsigned i, shift;
  1444. FLAC__int32 x = 0;
  1445. for(i = 0; i < samples && !(x&1); i++)
  1446. x |= signal[i];
  1447. if(x == 0) {
  1448. shift = 0;
  1449. }
  1450. else {
  1451. for(shift = 0; !(x&1); shift++)
  1452. x >>= 1;
  1453. }
  1454. if(shift > 0) {
  1455. for(i = 0; i < samples; i++)
  1456.  signal[i] >>= shift;
  1457. }
  1458. return shift;
  1459. }
  1460. void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
  1461. {
  1462. unsigned channel;
  1463. for(channel = 0; channel < channels; channel++)
  1464. memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
  1465. fifo->tail += wide_samples;
  1466. FLAC__ASSERT(fifo->tail <= fifo->size);
  1467. }
  1468. void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples)
  1469. {
  1470. unsigned channel;
  1471. unsigned sample, wide_sample;
  1472. unsigned tail = fifo->tail;
  1473. sample = input_offset * channels;
  1474. for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
  1475. for(channel = 0; channel < channels; channel++)
  1476. fifo->data[channel][tail] = input[sample++];
  1477. tail++;
  1478. }
  1479. fifo->tail = tail;
  1480. FLAC__ASSERT(fifo->tail <= fifo->size);
  1481. }
  1482. FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
  1483. {
  1484. FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
  1485. const unsigned encoded_bytes = encoder->private_->verify.output.bytes;
  1486. (void)decoder;
  1487. if(encoder->private_->verify.needs_magic_hack) {
  1488. FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
  1489. *bytes = FLAC__STREAM_SYNC_LENGTH;
  1490. memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
  1491. encoder->private_->verify.needs_magic_hack = false;
  1492. }
  1493. else {
  1494. if(encoded_bytes == 0) {
  1495. /*
  1496.  * If we get here, a FIFO underflow has occurred,
  1497.  * which means there is a bug somewhere.
  1498.  */
  1499. FLAC__ASSERT(0);
  1500. return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  1501. }
  1502. else if(encoded_bytes < *bytes)
  1503. *bytes = encoded_bytes;
  1504. memcpy(buffer, encoder->private_->verify.output.data, *bytes);
  1505. encoder->private_->verify.output.data += *bytes;
  1506. encoder->private_->verify.output.bytes -= *bytes;
  1507. }
  1508. return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  1509. }
  1510. FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
  1511. {
  1512. FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
  1513. unsigned channel;
  1514. const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
  1515. const unsigned blocksize = frame->header.blocksize;
  1516. const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize;
  1517. for(channel = 0; channel < channels; channel++) {
  1518. if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
  1519. unsigned i, sample = 0;
  1520. FLAC__int32 expect = 0, got = 0;
  1521. for(i = 0; i < blocksize; i++) {
  1522. if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
  1523. sample = i;
  1524. expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
  1525. got = (FLAC__int32)buffer[channel][i];
  1526. break;
  1527. }
  1528. }
  1529. FLAC__ASSERT(i < blocksize);
  1530. FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
  1531. encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
  1532. encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize);
  1533. encoder->private_->verify.error_stats.channel = channel;
  1534. encoder->private_->verify.error_stats.sample = sample;
  1535. encoder->private_->verify.error_stats.expected = expect;
  1536. encoder->private_->verify.error_stats.got = got;
  1537. encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
  1538. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  1539. }
  1540. }
  1541. /* dequeue the frame from the fifo */
  1542. for(channel = 0; channel < channels; channel++) {
  1543. memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize);
  1544. }
  1545. encoder->private_->verify.input_fifo.tail -= blocksize;
  1546. return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  1547. }
  1548. void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
  1549. {
  1550. (void)decoder, (void)metadata, (void)client_data;
  1551. }
  1552. void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
  1553. {
  1554. FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
  1555. (void)decoder, (void)status;
  1556. encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
  1557. }