Josh Coalson | 26560dd | 2001-02-08 00:38:41 +0000 | [diff] [blame] | 1 | /* libFLAC - Free Lossless Audio Codec library |
Josh Coalson | 305ae2e | 2002-01-26 17:36:39 +0000 | [diff] [blame] | 2 | * Copyright (C) 2000,2001,2002 Josh Coalson |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public |
| 15 | * License along with this library; if not, write to the |
| 16 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 | * Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> /* for malloc() */ |
| 22 | #include <string.h> /* for memcpy() */ |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 23 | #include "FLAC/assert.h" |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 24 | #include "protected/stream_encoder.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 25 | #include "private/bitbuffer.h" |
Josh Coalson | eef5670 | 2001-03-30 00:45:22 +0000 | [diff] [blame] | 26 | #include "private/bitmath.h" |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 27 | #include "private/crc.h" |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 28 | #include "private/cpu.h" |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 29 | #include "private/stream_encoder_framing.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 30 | #include "private/fixed.h" |
| 31 | #include "private/lpc.h" |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 32 | #include "private/md5.h" |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 33 | #include "private/memory.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 34 | |
| 35 | #ifdef min |
| 36 | #undef min |
| 37 | #endif |
| 38 | #define min(x,y) ((x)<(y)?(x):(y)) |
| 39 | |
| 40 | #ifdef max |
| 41 | #undef max |
| 42 | #endif |
| 43 | #define max(x,y) ((x)>(y)?(x):(y)) |
| 44 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 45 | /*********************************************************************** |
| 46 | * |
| 47 | * Private class method prototypes |
| 48 | * |
| 49 | ***********************************************************************/ |
| 50 | |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 51 | static void stream_encoder_set_defaults_(FLAC__StreamEncoder *encoder); |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 52 | static FLAC__bool stream_encoder_resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size); |
| 53 | static FLAC__bool stream_encoder_process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame); |
| 54 | static FLAC__bool stream_encoder_process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 55 | static FLAC__bool stream_encoder_process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__int32 integer_signal[], const FLAC__real real_signal[], FLAC__Subframe *subframe[2], FLAC__int32 *residual[2], unsigned *best_subframe, unsigned *best_bits); |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 56 | static FLAC__bool stream_encoder_add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame); |
| 57 | static unsigned stream_encoder_evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe); |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 58 | static unsigned stream_encoder_evaluate_fixed_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe); |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 59 | static unsigned stream_encoder_evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const FLAC__real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe); |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 60 | static unsigned stream_encoder_evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe); |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 61 | static unsigned stream_encoder_find_best_partition_order_(struct FLAC__StreamEncoderPrivate *private_, const FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[]); |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 62 | static void stream_encoder_precompute_partition_info_sums_(const FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 63 | static void stream_encoder_precompute_partition_info_escapes_(const FLAC__int32 residual[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order); |
| 64 | #ifdef DONT_ESTIMATE_RICE_BITS |
| 65 | static FLAC__bool stream_encoder_set_partitioned_rice_(const FLAC__uint32 abs_residual[], const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits); |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 66 | static FLAC__bool stream_encoder_set_partitioned_rice_with_precompute_(const FLAC__int32 residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 67 | #else |
| 68 | static FLAC__bool stream_encoder_set_partitioned_rice_(const FLAC__uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits); |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 69 | static FLAC__bool stream_encoder_set_partitioned_rice_with_precompute_(const FLAC__uint32 abs_residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 70 | #endif |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 71 | static unsigned stream_encoder_get_wasted_bits_(FLAC__int32 signal[], unsigned samples); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 72 | |
| 73 | /*********************************************************************** |
| 74 | * |
| 75 | * Private class data |
| 76 | * |
| 77 | ***********************************************************************/ |
| 78 | |
| 79 | typedef struct FLAC__StreamEncoderPrivate { |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 80 | unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 81 | FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */ |
| 82 | FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */ |
| 83 | FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */ |
| 84 | FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */ |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 85 | unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */ |
| 86 | unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 87 | FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */ |
| 88 | FLAC__int32 *residual_workspace_mid_side[2][2]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 89 | FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2]; |
| 90 | FLAC__Subframe subframe_workspace_mid_side[2][2]; |
| 91 | FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2]; |
| 92 | FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2]; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 93 | unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 94 | unsigned best_subframe_mid_side[2]; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 95 | unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 96 | unsigned best_subframe_bits_mid_side[2]; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 97 | FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */ |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 98 | FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */ |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 99 | unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 100 | FLAC__BitBuffer *frame; /* the current frame being worked on */ |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 101 | double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */ |
| 102 | unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */ |
| 103 | unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 104 | FLAC__ChannelAssignment last_channel_assignment; |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 105 | FLAC__StreamMetadata metadata; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 106 | unsigned current_sample_number; |
| 107 | unsigned current_frame_number; |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 108 | struct MD5Context md5context; |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 109 | FLAC__CPUInfo cpuinfo; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 110 | unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); |
| 111 | void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
| 112 | 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[]); |
| 113 | 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[]); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 114 | FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */ |
| 115 | FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */ |
| 116 | FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */ |
| 117 | FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 118 | FLAC__StreamEncoderWriteStatus (*write_callback)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data); |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 119 | void (*metadata_callback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 120 | void *client_data; |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 121 | /* unaligned (original) pointers to allocated data */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 122 | FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS]; |
| 123 | FLAC__int32 *integer_signal_mid_side_unaligned[2]; |
| 124 | FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS]; |
| 125 | FLAC__real *real_signal_mid_side_unaligned[2]; |
| 126 | FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2]; |
| 127 | FLAC__int32 *residual_workspace_mid_side_unaligned[2][2]; |
| 128 | FLAC__uint32 *abs_residual_unaligned; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 129 | FLAC__uint64 *abs_residual_partition_sums_unaligned; |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 130 | unsigned *raw_bits_per_partition_unaligned; |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 131 | /* |
| 132 | * These fields have been moved here from private function local |
| 133 | * declarations merely to save stack space during encoding. |
| 134 | */ |
| 135 | FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from stream_encoder_process_subframe_() */ |
| 136 | unsigned parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER], raw_bits[2][1 << FLAC__MAX_RICE_PARTITION_ORDER]; /* from stream_encoder_find_best_partition_order_() */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 137 | } FLAC__StreamEncoderPrivate; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 138 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 139 | /*********************************************************************** |
| 140 | * |
| 141 | * Public static class data |
| 142 | * |
| 143 | ***********************************************************************/ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 144 | |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 145 | const char * const FLAC__StreamEncoderStateString[] = { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 146 | "FLAC__STREAM_ENCODER_OK", |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 147 | "FLAC__STREAM_ENCODER_INVALID_CALLBACK", |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 148 | "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS", |
| 149 | "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE", |
| 150 | "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE", |
| 151 | "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE", |
| 152 | "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION", |
| 153 | "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH", |
| 154 | "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH", |
| 155 | "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE", |
| 156 | "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER", |
| 157 | "FLAC__STREAM_ENCODER_NOT_STREAMABLE", |
| 158 | "FLAC__STREAM_ENCODER_FRAMING_ERROR", |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 159 | "FLAC__STREAM_ENCODER_INVALID_METADATA", |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 160 | "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING", |
| 161 | "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING", |
| 162 | "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR", |
| 163 | "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED", |
| 164 | "FLAC__STREAM_ENCODER_UNINITIALIZED" |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 167 | const char * const FLAC__StreamEncoderWriteStatusString[] = { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 168 | "FLAC__STREAM_ENCODER_WRITE_OK", |
| 169 | "FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR" |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 172 | /*********************************************************************** |
| 173 | * |
| 174 | * Class constructor/destructor |
| 175 | * |
| 176 | ***********************************************************************/ |
| 177 | FLAC__StreamEncoder *FLAC__stream_encoder_new() |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 178 | { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 179 | FLAC__StreamEncoder *encoder; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 180 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 181 | FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 182 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 183 | encoder = (FLAC__StreamEncoder*)malloc(sizeof(FLAC__StreamEncoder)); |
| 184 | if(encoder == 0) { |
| 185 | return 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 186 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 187 | encoder->protected_ = (FLAC__StreamEncoderProtected*)malloc(sizeof(FLAC__StreamEncoderProtected)); |
| 188 | if(encoder->protected_ == 0) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 189 | free(encoder); |
| 190 | return 0; |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 191 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 192 | encoder->private_ = (FLAC__StreamEncoderPrivate*)malloc(sizeof(FLAC__StreamEncoderPrivate)); |
| 193 | if(encoder->private_ == 0) { |
| 194 | free(encoder->protected_); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 195 | free(encoder); |
| 196 | return 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 197 | } |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 198 | encoder->private_->frame = FLAC__bitbuffer_new(); |
| 199 | if(encoder->private_->frame == 0) { |
| 200 | free(encoder->private_); |
| 201 | free(encoder->protected_); |
| 202 | free(encoder); |
| 203 | return 0; |
| 204 | } |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 205 | |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 206 | stream_encoder_set_defaults_(encoder); |
| 207 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 208 | encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 209 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 210 | return encoder; |
| 211 | } |
| 212 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 213 | void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 214 | { |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 215 | FLAC__ASSERT(encoder != 0); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 216 | FLAC__ASSERT(encoder->protected_ != 0); |
| 217 | FLAC__ASSERT(encoder->private_ != 0); |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 218 | FLAC__ASSERT(encoder->private_->frame != 0); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 219 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 220 | FLAC__bitbuffer_delete(encoder->private_->frame); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 221 | free(encoder->private_); |
| 222 | free(encoder->protected_); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 223 | free(encoder); |
| 224 | } |
| 225 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 226 | /*********************************************************************** |
| 227 | * |
| 228 | * Public class methods |
| 229 | * |
| 230 | ***********************************************************************/ |
| 231 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 232 | FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 233 | { |
| 234 | unsigned i; |
| 235 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 236 | FLAC__ASSERT(encoder != 0); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 237 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 238 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 239 | return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 240 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 241 | encoder->protected_->state = FLAC__STREAM_ENCODER_OK; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 242 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 243 | if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback) |
| 244 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 245 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 246 | if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS) |
| 247 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS; |
Josh Coalson | 69f1ee0 | 2001-01-24 00:54:43 +0000 | [diff] [blame] | 248 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 249 | if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2) |
| 250 | return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH; |
Josh Coalson | d37d135 | 2001-05-30 23:09:31 +0000 | [diff] [blame] | 251 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 252 | if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo) |
| 253 | return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 254 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 255 | if(encoder->protected_->bits_per_sample >= 32) |
| 256 | encoder->protected_->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 257 | |
Josh Coalson | 76c68bc | 2002-05-17 06:22:02 +0000 | [diff] [blame] | 258 | if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 259 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 260 | |
Josh Coalson | 0833f34 | 2002-07-15 05:31:55 +0000 | [diff] [blame] | 261 | if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate)) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 262 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 263 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 264 | if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE) |
| 265 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 266 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 267 | if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order) |
| 268 | return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 269 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 270 | if(encoder->protected_->qlp_coeff_precision == 0) { |
| 271 | if(encoder->protected_->bits_per_sample < 16) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 272 | /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */ |
| 273 | /* @@@ until then we'll make a guess */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 274 | encoder->protected_->qlp_coeff_precision = max(5, 2 + encoder->protected_->bits_per_sample / 2); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 275 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 276 | else if(encoder->protected_->bits_per_sample == 16) { |
| 277 | if(encoder->protected_->blocksize <= 192) |
| 278 | encoder->protected_->qlp_coeff_precision = 7; |
| 279 | else if(encoder->protected_->blocksize <= 384) |
| 280 | encoder->protected_->qlp_coeff_precision = 8; |
| 281 | else if(encoder->protected_->blocksize <= 576) |
| 282 | encoder->protected_->qlp_coeff_precision = 9; |
| 283 | else if(encoder->protected_->blocksize <= 1152) |
| 284 | encoder->protected_->qlp_coeff_precision = 10; |
| 285 | else if(encoder->protected_->blocksize <= 2304) |
| 286 | encoder->protected_->qlp_coeff_precision = 11; |
| 287 | else if(encoder->protected_->blocksize <= 4608) |
| 288 | encoder->protected_->qlp_coeff_precision = 12; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 289 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 290 | encoder->protected_->qlp_coeff_precision = 13; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 291 | } |
| 292 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 293 | encoder->protected_->qlp_coeff_precision = min(13, 8*sizeof(FLAC__int32) - encoder->protected_->bits_per_sample - 1 - 2); /* @@@ -2 to keep things 32-bit safe */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 294 | } |
| 295 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 296 | else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision + encoder->protected_->bits_per_sample >= 8*sizeof(FLAC__uint32) || encoder->protected_->qlp_coeff_precision >= (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)) |
| 297 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 298 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 299 | if(encoder->protected_->streamable_subset) { |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 300 | /*@@@ add check for blocksize here */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 301 | if(encoder->protected_->bits_per_sample != 8 && encoder->protected_->bits_per_sample != 12 && encoder->protected_->bits_per_sample != 16 && encoder->protected_->bits_per_sample != 20 && encoder->protected_->bits_per_sample != 24) |
| 302 | return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE; |
| 303 | if(encoder->protected_->sample_rate > 655350) |
| 304 | return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 307 | if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN)) |
| 308 | encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1; |
| 309 | if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order) |
| 310 | encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 311 | |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 312 | /* validate metadata */ |
| 313 | if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) |
| 314 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA; |
| 315 | for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) { |
| 316 | if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO) |
| 317 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA; |
| 318 | else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) { |
Josh Coalson | 0833f34 | 2002-07-15 05:31:55 +0000 | [diff] [blame] | 319 | if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table)) |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 320 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA; |
| 321 | } |
| 322 | } |
| 323 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 324 | encoder->private_->input_capacity = 0; |
| 325 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 326 | encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0; |
| 327 | encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 328 | } |
| 329 | for(i = 0; i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 330 | encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0; |
| 331 | encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 332 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 333 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 334 | encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0; |
| 335 | encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0; |
| 336 | encoder->private_->best_subframe[i] = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 337 | } |
| 338 | for(i = 0; i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 339 | encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0; |
| 340 | encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0; |
| 341 | encoder->private_->best_subframe_mid_side[i] = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 342 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 343 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 344 | encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0]; |
| 345 | encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 346 | } |
| 347 | for(i = 0; i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 348 | encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0]; |
| 349 | encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 350 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 351 | encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0; |
| 352 | encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0; |
| 353 | encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0; |
| 354 | encoder->private_->loose_mid_side_stereo_frames_exact = (double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize; |
| 355 | encoder->private_->loose_mid_side_stereo_frames = (unsigned)(encoder->private_->loose_mid_side_stereo_frames_exact + 0.5); |
| 356 | if(encoder->private_->loose_mid_side_stereo_frames == 0) |
| 357 | encoder->private_->loose_mid_side_stereo_frames = 1; |
| 358 | encoder->private_->loose_mid_side_stereo_frame_count = 0; |
| 359 | encoder->private_->current_sample_number = 0; |
| 360 | encoder->private_->current_frame_number = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 361 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 362 | encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30); |
| 363 | 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? */ |
| 364 | encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */ |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 365 | |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 366 | /* |
| 367 | * get the CPU info and set the function pointers |
| 368 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 369 | FLAC__cpu_info(&encoder->private_->cpuinfo); |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 370 | /* first default to the non-asm routines */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 371 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation; |
| 372 | encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor; |
| 373 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients; |
| 374 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients; |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 375 | /* now override with asm where appropriate */ |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 376 | #ifndef FLAC__NO_ASM |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 377 | if(encoder->private_->cpuinfo.use_asm) { |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 378 | #ifdef FLAC__CPU_IA32 |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 379 | FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32); |
Josh Coalson | 034d38e | 2001-05-24 19:29:30 +0000 | [diff] [blame] | 380 | #ifdef FLAC__HAS_NASM |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 381 | if(0 && encoder->private_->cpuinfo.data.ia32.sse) { |
| 382 | if(encoder->protected_->max_lpc_order < 4) |
| 383 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4; |
| 384 | else if(encoder->protected_->max_lpc_order < 8) |
| 385 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8; |
| 386 | else if(encoder->protected_->max_lpc_order < 12) |
| 387 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12; |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 388 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 389 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32; |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 390 | } |
Josh Coalson | 395938e | 2001-11-15 21:53:25 +0000 | [diff] [blame] | 391 | else if(encoder->private_->cpuinfo.data.ia32._3dnow) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 392 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 393 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 394 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32; |
| 395 | if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov) |
| 396 | encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov; |
| 397 | if(encoder->private_->cpuinfo.data.ia32.mmx) { |
| 398 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32; |
| 399 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx; |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 400 | } |
| 401 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 402 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32; |
| 403 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32; |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 404 | } |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 405 | #endif |
Josh Coalson | 034d38e | 2001-05-24 19:29:30 +0000 | [diff] [blame] | 406 | #endif |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 407 | } |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 408 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 409 | /* finally override based on wide-ness if necessary */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 410 | if(encoder->private_->use_wide_by_block) { |
| 411 | encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 412 | } |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 413 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 414 | /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 415 | encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding; |
Josh Coalson | eef5670 | 2001-03-30 00:45:22 +0000 | [diff] [blame] | 416 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 417 | if(!stream_encoder_resize_buffers_(encoder, encoder->protected_->blocksize)) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 418 | /* the above function sets the state for us in case of an error */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 419 | return encoder->protected_->state; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 420 | } |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 421 | |
| 422 | if(!FLAC__bitbuffer_init(encoder->private_->frame)) |
| 423 | return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 424 | |
| 425 | /* |
| 426 | * write the stream header |
| 427 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 428 | if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 429 | return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 430 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 431 | encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO; |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 432 | encoder->private_->metadata.is_last = (encoder->protected_->num_metadata_blocks == 0); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 433 | encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH; |
| 434 | encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */ |
| 435 | encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize; |
| 436 | encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 437 | encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 438 | encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate; |
| 439 | encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels; |
| 440 | encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample; |
| 441 | encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */ |
| 442 | memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */ |
| 443 | MD5Init(&encoder->private_->md5context); |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 444 | if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame)) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 445 | return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 446 | |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 447 | for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) { |
| 448 | encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1); |
| 449 | if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 450 | return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | c692d38 | 2001-02-23 21:05:05 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 453 | FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame)); |
| 454 | { |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 455 | const FLAC__byte *buffer; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 456 | unsigned bytes; |
| 457 | |
| 458 | FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes); |
| 459 | |
| 460 | if(encoder->private_->write_callback(encoder, buffer, bytes, 0, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_OK) |
| 461 | return encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING; |
| 462 | |
| 463 | FLAC__bitbuffer_release_buffer(encoder->private_->frame); |
| 464 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 465 | |
Josh Coalson | cbbbb5f | 2001-01-23 00:41:48 +0000 | [diff] [blame] | 466 | /* now that the metadata block is written, we can init this to an absurdly-high value... */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 467 | encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1; |
Josh Coalson | cbbbb5f | 2001-01-23 00:41:48 +0000 | [diff] [blame] | 468 | /* ... and clear this to 0 */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 469 | encoder->private_->metadata.data.stream_info.total_samples = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 470 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 471 | return encoder->protected_->state; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 474 | void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 475 | { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 476 | unsigned i, channel; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 477 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 478 | FLAC__ASSERT(encoder != 0); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 479 | if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 480 | return; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 481 | if(encoder->private_->current_sample_number != 0) { |
| 482 | encoder->protected_->blocksize = encoder->private_->current_sample_number; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 483 | stream_encoder_process_frame_(encoder, true); /* true => is last frame */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 484 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 485 | MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context); |
| 486 | encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data); |
| 487 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 488 | if(encoder->private_->integer_signal_unaligned[i] != 0) { |
| 489 | free(encoder->private_->integer_signal_unaligned[i]); |
| 490 | encoder->private_->integer_signal_unaligned[i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 491 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 492 | if(encoder->private_->real_signal_unaligned[i] != 0) { |
| 493 | free(encoder->private_->real_signal_unaligned[i]); |
| 494 | encoder->private_->real_signal_unaligned[i] = 0; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | for(i = 0; i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 498 | if(encoder->private_->integer_signal_mid_side_unaligned[i] != 0) { |
| 499 | free(encoder->private_->integer_signal_mid_side_unaligned[i]); |
| 500 | encoder->private_->integer_signal_mid_side_unaligned[i] = 0; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 501 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 502 | if(encoder->private_->real_signal_mid_side_unaligned[i] != 0) { |
| 503 | free(encoder->private_->real_signal_mid_side_unaligned[i]); |
| 504 | encoder->private_->real_signal_mid_side_unaligned[i] = 0; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 505 | } |
| 506 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 507 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 508 | for(i = 0; i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 509 | if(encoder->private_->residual_workspace_unaligned[channel][i] != 0) { |
| 510 | free(encoder->private_->residual_workspace_unaligned[channel][i]); |
| 511 | encoder->private_->residual_workspace_unaligned[channel][i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 512 | } |
| 513 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 514 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 515 | for(channel = 0; channel < 2; channel++) { |
| 516 | for(i = 0; i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 517 | if(encoder->private_->residual_workspace_mid_side_unaligned[channel][i] != 0) { |
| 518 | free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]); |
| 519 | encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 523 | if(encoder->private_->abs_residual_unaligned != 0) { |
| 524 | free(encoder->private_->abs_residual_unaligned); |
| 525 | encoder->private_->abs_residual_unaligned = 0; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 526 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 527 | if(encoder->private_->abs_residual_partition_sums_unaligned != 0) { |
| 528 | free(encoder->private_->abs_residual_partition_sums_unaligned); |
| 529 | encoder->private_->abs_residual_partition_sums_unaligned = 0; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 530 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 531 | if(encoder->private_->raw_bits_per_partition_unaligned != 0) { |
| 532 | free(encoder->private_->raw_bits_per_partition_unaligned); |
| 533 | encoder->private_->raw_bits_per_partition_unaligned = 0; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 534 | } |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 535 | FLAC__bitbuffer_free(encoder->private_->frame); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 536 | |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 537 | stream_encoder_set_defaults_(encoder); |
| 538 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 539 | encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 542 | FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 543 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 544 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 545 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 546 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 547 | encoder->protected_->streamable_subset = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 548 | return true; |
| 549 | } |
| 550 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 551 | FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 552 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 553 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 554 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 555 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 556 | encoder->protected_->do_mid_side_stereo = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 557 | return true; |
| 558 | } |
| 559 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 560 | FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 561 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 562 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 563 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 564 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 565 | encoder->protected_->loose_mid_side_stereo = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 566 | return true; |
| 567 | } |
| 568 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 569 | FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 570 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 571 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 572 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 573 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 574 | encoder->protected_->channels = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 575 | return true; |
| 576 | } |
| 577 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 578 | FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 579 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 580 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 581 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 582 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 583 | encoder->protected_->bits_per_sample = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 584 | return true; |
| 585 | } |
| 586 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 587 | FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 588 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 589 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 590 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 591 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 592 | encoder->protected_->sample_rate = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 593 | return true; |
| 594 | } |
| 595 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 596 | FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 597 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 598 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 599 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 600 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 601 | encoder->protected_->blocksize = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 602 | return true; |
| 603 | } |
| 604 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 605 | FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 606 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 607 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 608 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 609 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 610 | encoder->protected_->max_lpc_order = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 611 | return true; |
| 612 | } |
| 613 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 614 | FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 615 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 616 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 617 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 618 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 619 | encoder->protected_->qlp_coeff_precision = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 620 | return true; |
| 621 | } |
| 622 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 623 | FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 624 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 625 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 626 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 627 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 628 | encoder->protected_->do_qlp_coeff_prec_search = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 629 | return true; |
| 630 | } |
| 631 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 632 | FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 633 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 634 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 635 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 636 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 637 | encoder->protected_->do_escape_coding = value; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 638 | return true; |
| 639 | } |
| 640 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 641 | FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 642 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 643 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 644 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 645 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 646 | encoder->protected_->do_exhaustive_model_search = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 647 | return true; |
| 648 | } |
| 649 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 650 | FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 651 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 652 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 653 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 654 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 655 | encoder->protected_->min_residual_partition_order = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 656 | return true; |
| 657 | } |
| 658 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 659 | FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 660 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 661 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 662 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 663 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 664 | encoder->protected_->max_residual_partition_order = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 665 | return true; |
| 666 | } |
| 667 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 668 | FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 669 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 670 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 671 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 672 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 673 | encoder->protected_->rice_parameter_search_dist = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 674 | return true; |
| 675 | } |
| 676 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 677 | FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 678 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 679 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 680 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 681 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 682 | encoder->protected_->total_samples_estimate = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 683 | return true; |
| 684 | } |
| 685 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 686 | FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 687 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 688 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 689 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 690 | return false; |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 691 | encoder->protected_->metadata = metadata; |
| 692 | encoder->protected_->num_metadata_blocks = num_blocks; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 693 | return true; |
| 694 | } |
| 695 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 696 | FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteStatus (*value)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 697 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 698 | FLAC__ASSERT(0 != encoder); |
| 699 | FLAC__ASSERT(0 != value); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 700 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 701 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 702 | encoder->private_->write_callback = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 703 | return true; |
| 704 | } |
| 705 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 706 | FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, void (*value)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 707 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 708 | FLAC__ASSERT(0 != encoder); |
| 709 | FLAC__ASSERT(0 != value); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 710 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 711 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 712 | encoder->private_->metadata_callback = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 713 | return true; |
| 714 | } |
| 715 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 716 | FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 717 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 718 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 719 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 720 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 721 | encoder->private_->client_data = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 722 | return true; |
| 723 | } |
| 724 | |
| 725 | FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 726 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 727 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 728 | return encoder->protected_->state; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 731 | FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 732 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 733 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 734 | return encoder->protected_->streamable_subset; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 737 | FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 738 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 739 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 740 | return encoder->protected_->do_mid_side_stereo; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 743 | FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 744 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 745 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 746 | return encoder->protected_->loose_mid_side_stereo; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 749 | unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 750 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 751 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 752 | return encoder->protected_->channels; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 755 | unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 756 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 757 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 758 | return encoder->protected_->bits_per_sample; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 761 | unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 762 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 763 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 764 | return encoder->protected_->sample_rate; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 767 | unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 768 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 769 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 770 | return encoder->protected_->blocksize; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 773 | unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 774 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 775 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 776 | return encoder->protected_->max_lpc_order; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 779 | unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 780 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 781 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 782 | return encoder->protected_->qlp_coeff_precision; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 785 | FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 786 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 787 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 788 | return encoder->protected_->do_qlp_coeff_prec_search; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 791 | FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder) |
| 792 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 793 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 794 | return encoder->protected_->do_escape_coding; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 797 | FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 798 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 799 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 800 | return encoder->protected_->do_exhaustive_model_search; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 803 | unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 804 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 805 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 806 | return encoder->protected_->min_residual_partition_order; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 809 | unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 810 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 811 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 812 | return encoder->protected_->max_residual_partition_order; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 815 | unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 816 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 817 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 818 | return encoder->protected_->rice_parameter_search_dist; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 821 | FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 822 | { |
| 823 | unsigned i, j, channel; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 824 | FLAC__int32 x, mid, side; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 825 | const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 826 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 827 | FLAC__ASSERT(encoder != 0); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 828 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 829 | |
| 830 | j = 0; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 831 | if(encoder->protected_->do_mid_side_stereo && channels == 2) { |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 832 | do { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 833 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 834 | x = mid = side = buffer[0][j]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 835 | encoder->private_->integer_signal[0][i] = x; |
| 836 | encoder->private_->real_signal[0][i] = (FLAC__real)x; |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 837 | x = buffer[1][j]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 838 | encoder->private_->integer_signal[1][i] = x; |
| 839 | encoder->private_->real_signal[1][i] = (FLAC__real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 840 | mid += x; |
| 841 | side -= x; |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 842 | mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 843 | encoder->private_->integer_signal_mid_side[1][i] = side; |
| 844 | encoder->private_->integer_signal_mid_side[0][i] = mid; |
| 845 | encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side; |
| 846 | encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid; |
| 847 | encoder->private_->current_sample_number++; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 848 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 849 | if(i == blocksize) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 850 | if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 851 | return false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 852 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 853 | } while(j < samples); |
| 854 | } |
| 855 | else { |
| 856 | do { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 857 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 858 | for(channel = 0; channel < channels; channel++) { |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 859 | x = buffer[channel][j]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 860 | encoder->private_->integer_signal[channel][i] = x; |
| 861 | encoder->private_->real_signal[channel][i] = (FLAC__real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 862 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 863 | encoder->private_->current_sample_number++; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 864 | } |
| 865 | if(i == blocksize) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 866 | if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 867 | return false; |
| 868 | } |
| 869 | } while(j < samples); |
| 870 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 871 | |
| 872 | return true; |
| 873 | } |
| 874 | |
| 875 | /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */ |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 876 | FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 877 | { |
| 878 | unsigned i, j, k, channel; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 879 | FLAC__int32 x, mid, side; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 880 | const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 881 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 882 | FLAC__ASSERT(encoder != 0); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 883 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 884 | |
| 885 | j = k = 0; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 886 | if(encoder->protected_->do_mid_side_stereo && channels == 2) { |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 887 | do { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 888 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 889 | x = mid = side = buffer[k++]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 890 | encoder->private_->integer_signal[0][i] = x; |
| 891 | encoder->private_->real_signal[0][i] = (FLAC__real)x; |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 892 | x = buffer[k++]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 893 | encoder->private_->integer_signal[1][i] = x; |
| 894 | encoder->private_->real_signal[1][i] = (FLAC__real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 895 | mid += x; |
| 896 | side -= x; |
| 897 | mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 898 | encoder->private_->integer_signal_mid_side[1][i] = side; |
| 899 | encoder->private_->integer_signal_mid_side[0][i] = mid; |
| 900 | encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side; |
| 901 | encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid; |
| 902 | encoder->private_->current_sample_number++; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 903 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 904 | if(i == blocksize) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 905 | if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 906 | return false; |
| 907 | } |
| 908 | } while(j < samples); |
| 909 | } |
| 910 | else { |
| 911 | do { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 912 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 913 | for(channel = 0; channel < channels; channel++) { |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 914 | x = buffer[k++]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 915 | encoder->private_->integer_signal[channel][i] = x; |
| 916 | encoder->private_->real_signal[channel][i] = (FLAC__real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 917 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 918 | encoder->private_->current_sample_number++; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 919 | } |
| 920 | if(i == blocksize) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 921 | if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 922 | return false; |
| 923 | } |
| 924 | } while(j < samples); |
| 925 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 926 | |
| 927 | return true; |
| 928 | } |
| 929 | |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame^] | 930 | void stream_encoder_set_defaults_(FLAC__StreamEncoder *encoder) |
| 931 | { |
| 932 | FLAC__ASSERT(0 != encoder); |
| 933 | |
| 934 | encoder->protected_->streamable_subset = true; |
| 935 | encoder->protected_->do_mid_side_stereo = false; |
| 936 | encoder->protected_->loose_mid_side_stereo = false; |
| 937 | encoder->protected_->channels = 2; |
| 938 | encoder->protected_->bits_per_sample = 16; |
| 939 | encoder->protected_->sample_rate = 44100; |
| 940 | encoder->protected_->blocksize = 1152; |
| 941 | encoder->protected_->max_lpc_order = 0; |
| 942 | encoder->protected_->qlp_coeff_precision = 0; |
| 943 | encoder->protected_->do_qlp_coeff_prec_search = false; |
| 944 | encoder->protected_->do_exhaustive_model_search = false; |
| 945 | encoder->protected_->do_escape_coding = false; |
| 946 | encoder->protected_->min_residual_partition_order = 0; |
| 947 | encoder->protected_->max_residual_partition_order = 0; |
| 948 | encoder->protected_->rice_parameter_search_dist = 0; |
| 949 | encoder->protected_->total_samples_estimate = 0; |
| 950 | encoder->protected_->metadata = 0; |
| 951 | encoder->protected_->num_metadata_blocks = 0; |
| 952 | |
| 953 | encoder->private_->write_callback = 0; |
| 954 | encoder->private_->metadata_callback = 0; |
| 955 | encoder->private_->client_data = 0; |
| 956 | } |
| 957 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 958 | FLAC__bool stream_encoder_resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 959 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 960 | FLAC__bool ok; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 961 | unsigned i, channel; |
| 962 | |
| 963 | FLAC__ASSERT(new_size > 0); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 964 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
| 965 | FLAC__ASSERT(encoder->private_->current_sample_number == 0); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 966 | |
| 967 | /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 968 | if(new_size <= encoder->private_->input_capacity) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 969 | return true; |
| 970 | |
| 971 | ok = true; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 972 | |
| 973 | /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx() requires that the input arrays (in our case the integer signals) have a buffer of up to 3 zeroes in front (at negative indices) for alignment purposes; we use 4 to keep the data well-aligned. */ |
| 974 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 975 | for(i = 0; ok && i < encoder->protected_->channels; i++) { |
| 976 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]); |
| 977 | ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]); |
| 978 | memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4); |
| 979 | encoder->private_->integer_signal[i] += 4; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 980 | } |
| 981 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 982 | 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]); |
| 983 | 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]); |
| 984 | memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4); |
| 985 | encoder->private_->integer_signal_mid_side[i] += 4; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 986 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 987 | for(channel = 0; ok && channel < encoder->protected_->channels; channel++) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 988 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 989 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | for(channel = 0; ok && channel < 2; channel++) { |
| 993 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 994 | 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]); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 995 | } |
| 996 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 997 | ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual); |
| 998 | 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 */ |
| 999 | ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums); |
| 1000 | if(encoder->protected_->do_escape_coding) |
| 1001 | ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1002 | |
| 1003 | if(ok) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1004 | encoder->private_->input_capacity = new_size; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1005 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1006 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1007 | |
| 1008 | return ok; |
| 1009 | } |
| 1010 | |
| 1011 | /*********************************************************************** |
| 1012 | * |
| 1013 | * Private class methods |
| 1014 | * |
| 1015 | ***********************************************************************/ |
| 1016 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1017 | FLAC__bool stream_encoder_process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1018 | { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1019 | const FLAC__byte *buffer; |
| 1020 | unsigned bytes; |
| 1021 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1022 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1023 | |
| 1024 | /* |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 1025 | * Accumulate raw signal to the MD5 signature |
| 1026 | */ |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1027 | 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)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1028 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 1029 | return false; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1033 | * Process the frame header and subframes into the frame bitbuffer |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1034 | */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1035 | if(!stream_encoder_process_subframes_(encoder, is_last_frame)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1036 | /* the above function sets the state for us in case of an error */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1037 | return false; |
| 1038 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1039 | |
| 1040 | /* |
| 1041 | * Zero-pad the frame to a byte_boundary |
| 1042 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1043 | if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1044 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1045 | return false; |
| 1046 | } |
| 1047 | |
| 1048 | /* |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1049 | * CRC-16 the whole thing |
| 1050 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1051 | FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame)); |
| 1052 | FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN); |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1053 | |
| 1054 | /* |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1055 | * Write it |
| 1056 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1057 | FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes); |
| 1058 | if(encoder->private_->write_callback(encoder, buffer, bytes, encoder->protected_->blocksize, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_OK) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1059 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1060 | return false; |
| 1061 | } |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1062 | FLAC__bitbuffer_release_buffer(encoder->private_->frame); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1063 | |
| 1064 | /* |
| 1065 | * Get ready for the next frame |
| 1066 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1067 | encoder->private_->current_sample_number = 0; |
| 1068 | encoder->private_->current_frame_number++; |
| 1069 | encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1070 | encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize); |
| 1071 | encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1072 | |
| 1073 | return true; |
| 1074 | } |
| 1075 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1076 | FLAC__bool stream_encoder_process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1077 | { |
| 1078 | FLAC__FrameHeader frame_header; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1079 | unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1080 | FLAC__bool do_independent, do_mid_side, precompute_partition_sums; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1081 | |
| 1082 | /* |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1083 | * Calculate the min,max Rice partition orders |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1084 | */ |
| 1085 | if(is_last_frame) { |
| 1086 | max_partition_order = 0; |
| 1087 | } |
| 1088 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1089 | unsigned limit = 0, b = encoder->protected_->blocksize; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1090 | while(!(b & 1)) { |
| 1091 | limit++; |
| 1092 | b >>= 1; |
| 1093 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1094 | max_partition_order = min(encoder->protected_->max_residual_partition_order, limit); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1095 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1096 | min_partition_order = min(min_partition_order, max_partition_order); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1097 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1098 | precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1099 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1100 | /* |
| 1101 | * Setup the frame |
| 1102 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1103 | if(!FLAC__bitbuffer_clear(encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1104 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1105 | return false; |
| 1106 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1107 | frame_header.blocksize = encoder->protected_->blocksize; |
| 1108 | frame_header.sample_rate = encoder->protected_->sample_rate; |
| 1109 | frame_header.channels = encoder->protected_->channels; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1110 | frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1111 | frame_header.bits_per_sample = encoder->protected_->bits_per_sample; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1112 | frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1113 | frame_header.number.frame_number = encoder->private_->current_frame_number; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1114 | |
| 1115 | /* |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1116 | * Figure out what channel assignments to try |
| 1117 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1118 | if(encoder->protected_->do_mid_side_stereo) { |
| 1119 | if(encoder->protected_->loose_mid_side_stereo) { |
| 1120 | if(encoder->private_->loose_mid_side_stereo_frame_count == 0) { |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1121 | do_independent = true; |
| 1122 | do_mid_side = true; |
| 1123 | } |
| 1124 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1125 | do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1126 | do_mid_side = !do_independent; |
| 1127 | } |
| 1128 | } |
| 1129 | else { |
| 1130 | do_independent = true; |
| 1131 | do_mid_side = true; |
| 1132 | } |
| 1133 | } |
| 1134 | else { |
| 1135 | do_independent = true; |
| 1136 | do_mid_side = false; |
| 1137 | } |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1138 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1139 | FLAC__ASSERT(do_independent || do_mid_side); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1140 | |
| 1141 | /* |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1142 | * Check for wasted bits; set effective bps for each subframe |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1143 | */ |
| 1144 | if(do_independent) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1145 | unsigned w; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1146 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
| 1147 | w = stream_encoder_get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize); |
| 1148 | encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w; |
| 1149 | encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1150 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1151 | } |
| 1152 | if(do_mid_side) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1153 | unsigned w; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1154 | FLAC__ASSERT(encoder->protected_->channels == 2); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1155 | for(channel = 0; channel < 2; channel++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1156 | w = stream_encoder_get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize); |
| 1157 | encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w; |
| 1158 | encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1159 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | /* |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1163 | * First do a normal encoding pass of each independent channel |
| 1164 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1165 | if(do_independent) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1166 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
| 1167 | if(!stream_encoder_process_subframe_(encoder, min_partition_order, max_partition_order, precompute_partition_sums, false, &frame_header, encoder->private_->subframe_bps[channel], encoder->private_->integer_signal[channel], encoder->private_->real_signal[channel], encoder->private_->subframe_workspace_ptr[channel], encoder->private_->residual_workspace[channel], encoder->private_->best_subframe+channel, encoder->private_->best_subframe_bits+channel)) |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1168 | return false; |
| 1169 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | /* |
| 1173 | * Now do mid and side channels if requested |
| 1174 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1175 | if(do_mid_side) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1176 | FLAC__ASSERT(encoder->protected_->channels == 2); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1177 | |
| 1178 | for(channel = 0; channel < 2; channel++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1179 | if(!stream_encoder_process_subframe_(encoder, min_partition_order, max_partition_order, precompute_partition_sums, false, &frame_header, encoder->private_->subframe_bps_mid_side[channel], encoder->private_->integer_signal_mid_side[channel], encoder->private_->real_signal_mid_side[channel], encoder->private_->subframe_workspace_ptr_mid_side[channel], encoder->private_->residual_workspace_mid_side[channel], encoder->private_->best_subframe_mid_side+channel, encoder->private_->best_subframe_bits_mid_side+channel)) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1180 | return false; |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | /* |
| 1185 | * Compose the frame bitbuffer |
| 1186 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1187 | if(do_mid_side) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1188 | unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */ |
| 1189 | FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1190 | FLAC__ChannelAssignment channel_assignment; |
| 1191 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1192 | FLAC__ASSERT(encoder->protected_->channels == 2); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1193 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1194 | if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) { |
| 1195 | channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1196 | } |
| 1197 | else { |
| 1198 | unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */ |
| 1199 | unsigned min_bits; |
| 1200 | FLAC__ChannelAssignment ca; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1201 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1202 | FLAC__ASSERT(do_independent && do_mid_side); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1203 | |
| 1204 | /* We have to figure out which channel assignent results in the smallest frame */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1205 | bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1]; |
| 1206 | bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1]; |
| 1207 | bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1]; |
| 1208 | bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1]; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1209 | |
| 1210 | for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) { |
| 1211 | if(bits[ca] < min_bits) { |
| 1212 | min_bits = bits[ca]; |
| 1213 | channel_assignment = ca; |
| 1214 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1215 | } |
| 1216 | } |
| 1217 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1218 | frame_header.channel_assignment = channel_assignment; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1219 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1220 | if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1221 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1222 | return false; |
| 1223 | } |
| 1224 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1225 | switch(channel_assignment) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1226 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1227 | left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]]; |
| 1228 | right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1229 | break; |
| 1230 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1231 | left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]]; |
| 1232 | right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1233 | break; |
| 1234 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1235 | left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]]; |
| 1236 | right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1237 | break; |
| 1238 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1239 | left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]]; |
| 1240 | right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1241 | break; |
| 1242 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1243 | FLAC__ASSERT(0); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1244 | } |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1245 | |
| 1246 | switch(channel_assignment) { |
| 1247 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1248 | left_bps = encoder->private_->subframe_bps [0]; |
| 1249 | right_bps = encoder->private_->subframe_bps [1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1250 | break; |
| 1251 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1252 | left_bps = encoder->private_->subframe_bps [0]; |
| 1253 | right_bps = encoder->private_->subframe_bps_mid_side[1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1254 | break; |
| 1255 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1256 | left_bps = encoder->private_->subframe_bps_mid_side[1]; |
| 1257 | right_bps = encoder->private_->subframe_bps [1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1258 | break; |
| 1259 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1260 | left_bps = encoder->private_->subframe_bps_mid_side[0]; |
| 1261 | right_bps = encoder->private_->subframe_bps_mid_side[1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1262 | break; |
| 1263 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1264 | FLAC__ASSERT(0); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | /* note that encoder_add_subframe_ sets the state for us in case of an error */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1268 | if(!stream_encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame)) |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1269 | return false; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1270 | if(!stream_encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame)) |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1271 | return false; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1272 | } |
| 1273 | else { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1274 | if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1275 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1276 | return false; |
| 1277 | } |
| 1278 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1279 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1280 | if(!stream_encoder_add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1281 | /* the above function sets the state for us in case of an error */ |
| 1282 | return false; |
| 1283 | } |
| 1284 | } |
| 1285 | } |
| 1286 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1287 | if(encoder->protected_->loose_mid_side_stereo) { |
| 1288 | encoder->private_->loose_mid_side_stereo_frame_count++; |
| 1289 | if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames) |
| 1290 | encoder->private_->loose_mid_side_stereo_frame_count = 0; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1293 | encoder->private_->last_channel_assignment = frame_header.channel_assignment; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1294 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1295 | return true; |
| 1296 | } |
| 1297 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1298 | FLAC__bool stream_encoder_process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__int32 integer_signal[], const FLAC__real real_signal[], FLAC__Subframe *subframe[2], FLAC__int32 *residual[2], unsigned *best_subframe, unsigned *best_bits) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1299 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1300 | FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]; |
| 1301 | FLAC__real lpc_residual_bits_per_sample; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1302 | 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 */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1303 | FLAC__real lpc_error[FLAC__MAX_LPC_ORDER]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1304 | unsigned min_lpc_order, max_lpc_order, lpc_order; |
| 1305 | unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order; |
| 1306 | unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision; |
| 1307 | unsigned rice_parameter; |
| 1308 | unsigned _candidate_bits, _best_bits; |
| 1309 | unsigned _best_subframe; |
| 1310 | |
| 1311 | /* verbatim subframe is the baseline against which we measure other compressed subframes */ |
| 1312 | _best_subframe = 0; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1313 | _best_bits = stream_encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1314 | |
| 1315 | if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) { |
| 1316 | /* check for constant subframe */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1317 | 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); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1318 | if(fixed_residual_bits_per_sample[1] == 0.0) { |
| 1319 | /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */ |
| 1320 | unsigned i, signal_is_constant = true; |
| 1321 | for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) { |
| 1322 | if(integer_signal[0] != integer_signal[i]) { |
| 1323 | signal_is_constant = false; |
| 1324 | break; |
| 1325 | } |
| 1326 | } |
| 1327 | if(signal_is_constant) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1328 | _candidate_bits = stream_encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1329 | if(_candidate_bits < _best_bits) { |
| 1330 | _best_subframe = !_best_subframe; |
| 1331 | _best_bits = _candidate_bits; |
| 1332 | } |
| 1333 | } |
| 1334 | } |
| 1335 | else { |
| 1336 | /* encode fixed */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1337 | if(encoder->protected_->do_exhaustive_model_search) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1338 | min_fixed_order = 0; |
| 1339 | max_fixed_order = FLAC__MAX_FIXED_ORDER; |
| 1340 | } |
| 1341 | else { |
| 1342 | min_fixed_order = max_fixed_order = guess_fixed_order; |
| 1343 | } |
| 1344 | for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1345 | if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1346 | continue; /* don't even try */ |
Josh Coalson | 46f2ae8 | 2001-02-08 00:27:21 +0000 | [diff] [blame] | 1347 | 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 */ |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1348 | #ifndef FLAC__SYMMETRIC_RICE |
Josh Coalson | 46f2ae8 | 2001-02-08 00:27:21 +0000 | [diff] [blame] | 1349 | rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */ |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1350 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1351 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 1352 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1353 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 1354 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1355 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1356 | } |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1357 | _candidate_bits = stream_encoder_evaluate_fixed_subframe_(encoder, integer_signal, residual[!_best_subframe], encoder->private_->abs_residual, encoder->private_->abs_residual_partition_sums, encoder->private_->raw_bits_per_partition, frame_header->blocksize, subframe_bps, fixed_order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, encoder->protected_->do_escape_coding, encoder->protected_->rice_parameter_search_dist, subframe[!_best_subframe]); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1358 | if(_candidate_bits < _best_bits) { |
| 1359 | _best_subframe = !_best_subframe; |
| 1360 | _best_bits = _candidate_bits; |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | /* encode lpc */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1365 | if(encoder->protected_->max_lpc_order > 0) { |
| 1366 | if(encoder->protected_->max_lpc_order >= frame_header->blocksize) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1367 | max_lpc_order = frame_header->blocksize-1; |
| 1368 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1369 | max_lpc_order = encoder->protected_->max_lpc_order; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1370 | if(max_lpc_order > 0) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1371 | encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc); |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1372 | /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */ |
| 1373 | if(autoc[0] != 0.0) { |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1374 | FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1375 | if(encoder->protected_->do_exhaustive_model_search) { |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1376 | min_lpc_order = 1; |
| 1377 | } |
| 1378 | else { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1379 | unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps); |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1380 | min_lpc_order = max_lpc_order = guess_lpc_order; |
| 1381 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1382 | if(encoder->protected_->do_qlp_coeff_prec_search) { |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1383 | min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1384 | max_qlp_coeff_precision = min(8*sizeof(FLAC__int32) - subframe_bps - 1 - 2, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1); /* -2 to keep things 32-bit safe */ |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1385 | } |
| 1386 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1387 | min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision; |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1388 | } |
| 1389 | for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) { |
| 1390 | lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order); |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1391 | if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps) |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1392 | continue; /* don't even try */ |
| 1393 | rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */ |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1394 | #ifndef FLAC__SYMMETRIC_RICE |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1395 | rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */ |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1396 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1397 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 1398 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1399 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 1400 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1401 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1402 | } |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1403 | for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) { |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1404 | _candidate_bits = stream_encoder_evaluate_lpc_subframe_(encoder, integer_signal, residual[!_best_subframe], encoder->private_->abs_residual, encoder->private_->abs_residual_partition_sums, encoder->private_->raw_bits_per_partition, encoder->private_->lp_coeff[lpc_order-1], frame_header->blocksize, subframe_bps, lpc_order, qlp_coeff_precision, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, encoder->protected_->do_escape_coding, encoder->protected_->rice_parameter_search_dist, subframe[!_best_subframe]); |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1405 | if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */ |
| 1406 | if(_candidate_bits < _best_bits) { |
| 1407 | _best_subframe = !_best_subframe; |
| 1408 | _best_bits = _candidate_bits; |
| 1409 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | } |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | *best_subframe = _best_subframe; |
| 1420 | *best_bits = _best_bits; |
| 1421 | |
| 1422 | return true; |
| 1423 | } |
| 1424 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1425 | FLAC__bool stream_encoder_add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1426 | { |
| 1427 | switch(subframe->type) { |
| 1428 | case FLAC__SUBFRAME_TYPE_CONSTANT: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1429 | if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1430 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1431 | return false; |
| 1432 | } |
| 1433 | break; |
| 1434 | case FLAC__SUBFRAME_TYPE_FIXED: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1435 | if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1436 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1437 | return false; |
| 1438 | } |
| 1439 | break; |
| 1440 | case FLAC__SUBFRAME_TYPE_LPC: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1441 | if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1442 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1443 | return false; |
| 1444 | } |
| 1445 | break; |
| 1446 | case FLAC__SUBFRAME_TYPE_VERBATIM: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1447 | if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1448 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1449 | return false; |
| 1450 | } |
| 1451 | break; |
| 1452 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1453 | FLAC__ASSERT(0); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | return true; |
| 1457 | } |
| 1458 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1459 | unsigned stream_encoder_evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1460 | { |
| 1461 | subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT; |
| 1462 | subframe->data.constant.value = signal; |
| 1463 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1464 | return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1467 | unsigned stream_encoder_evaluate_fixed_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1468 | { |
| 1469 | unsigned i, residual_bits; |
| 1470 | const unsigned residual_samples = blocksize - order; |
| 1471 | |
| 1472 | FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual); |
| 1473 | |
| 1474 | subframe->type = FLAC__SUBFRAME_TYPE_FIXED; |
| 1475 | |
| 1476 | subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
| 1477 | subframe->data.fixed.residual = residual; |
| 1478 | |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1479 | residual_bits = stream_encoder_find_best_partition_order_(encoder->private_, residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, do_escape_coding, rice_parameter_search_dist, &subframe->data.fixed.entropy_coding_method.data.partitioned_rice.order, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.raw_bits); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1480 | |
| 1481 | subframe->data.fixed.order = order; |
| 1482 | for(i = 0; i < order; i++) |
| 1483 | subframe->data.fixed.warmup[i] = signal[i]; |
| 1484 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1485 | return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1488 | unsigned stream_encoder_evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const FLAC__real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1489 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1490 | FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1491 | unsigned i, residual_bits; |
| 1492 | int quantization, ret; |
| 1493 | const unsigned residual_samples = blocksize - order; |
| 1494 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1495 | ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1496 | if(ret != 0) |
| 1497 | return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */ |
| 1498 | |
Josh Coalson | 92d4240 | 2001-05-31 20:53:19 +0000 | [diff] [blame] | 1499 | if(subframe_bps <= 16 && qlp_coeff_precision <= 16) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1500 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual); |
Josh Coalson | 92d4240 | 2001-05-31 20:53:19 +0000 | [diff] [blame] | 1501 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1502 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1503 | |
| 1504 | subframe->type = FLAC__SUBFRAME_TYPE_LPC; |
| 1505 | |
| 1506 | subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
| 1507 | subframe->data.lpc.residual = residual; |
| 1508 | |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1509 | residual_bits = stream_encoder_find_best_partition_order_(encoder->private_, residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, do_escape_coding, rice_parameter_search_dist, &subframe->data.lpc.entropy_coding_method.data.partitioned_rice.order, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.raw_bits); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1510 | |
| 1511 | subframe->data.lpc.order = order; |
| 1512 | subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision; |
| 1513 | subframe->data.lpc.quantization_level = quantization; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1514 | memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1515 | for(i = 0; i < order; i++) |
| 1516 | subframe->data.lpc.warmup[i] = signal[i]; |
| 1517 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1518 | 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; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1521 | unsigned stream_encoder_evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1522 | { |
| 1523 | subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM; |
| 1524 | |
| 1525 | subframe->data.verbatim.data = signal; |
| 1526 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1527 | return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1530 | unsigned stream_encoder_find_best_partition_order_(FLAC__StreamEncoderPrivate *private_, const FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[]) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1531 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1532 | FLAC__int32 r; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1533 | unsigned residual_bits, best_residual_bits = 0; |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1534 | unsigned residual_sample; |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1535 | unsigned best_parameters_index = 0; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1536 | const unsigned blocksize = residual_samples + predictor_order; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1537 | |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1538 | /* compute abs(residual) for use later */ |
| 1539 | for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) { |
| 1540 | r = residual[residual_sample]; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1541 | abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r); |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1544 | while(max_partition_order > 0 && blocksize >> max_partition_order <= predictor_order) |
| 1545 | max_partition_order--; |
| 1546 | FLAC__ASSERT(blocksize >> max_partition_order > predictor_order); |
| 1547 | min_partition_order = min(min_partition_order, max_partition_order); |
| 1548 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1549 | if(precompute_partition_sums) { |
| 1550 | int partition_order; |
| 1551 | unsigned sum; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1552 | |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1553 | stream_encoder_precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1554 | |
| 1555 | if(do_escape_coding) |
| 1556 | stream_encoder_precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order); |
| 1557 | |
| 1558 | for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) { |
| 1559 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1560 | if(!stream_encoder_set_partitioned_rice_with_precompute_(residual, abs_residual_partition_sums+sum, raw_bits_per_partition+sum, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, (unsigned)partition_order, do_escape_coding, private_->parameters[!best_parameters_index], private_->raw_bits[!best_parameters_index], &residual_bits)) |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1561 | #else |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1562 | if(!stream_encoder_set_partitioned_rice_with_precompute_(abs_residual, abs_residual_partition_sums+sum, raw_bits_per_partition+sum, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, (unsigned)partition_order, do_escape_coding, private_->parameters[!best_parameters_index], private_->raw_bits[!best_parameters_index], &residual_bits)) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1563 | #endif |
| 1564 | { |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1565 | FLAC__ASSERT(best_residual_bits != 0); |
| 1566 | break; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1567 | } |
| 1568 | sum += 1u << partition_order; |
| 1569 | if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 1570 | best_residual_bits = residual_bits; |
| 1571 | *best_partition_order = partition_order; |
| 1572 | best_parameters_index = !best_parameters_index; |
| 1573 | } |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1574 | } |
| 1575 | } |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1576 | else { |
| 1577 | unsigned partition_order; |
| 1578 | for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) { |
| 1579 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1580 | if(!stream_encoder_set_partitioned_rice_(abs_residual, residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, private_->parameters[!best_parameters_index], &residual_bits)) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1581 | #else |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1582 | if(!stream_encoder_set_partitioned_rice_(abs_residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, private_->parameters[!best_parameters_index], &residual_bits)) |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1583 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1584 | { |
| 1585 | FLAC__ASSERT(best_residual_bits != 0); |
| 1586 | break; |
| 1587 | } |
| 1588 | if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 1589 | best_residual_bits = residual_bits; |
| 1590 | *best_partition_order = partition_order; |
| 1591 | best_parameters_index = !best_parameters_index; |
| 1592 | } |
| 1593 | } |
| 1594 | } |
| 1595 | |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1596 | memcpy(best_parameters, private_->parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order))); |
| 1597 | memcpy(best_raw_bits, private_->raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order))); |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1598 | |
| 1599 | return best_residual_bits; |
| 1600 | } |
| 1601 | |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1602 | void stream_encoder_precompute_partition_info_sums_(const FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order) |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1603 | { |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1604 | int partition_order; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1605 | unsigned from_partition, to_partition = 0; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1606 | const unsigned blocksize = residual_samples + predictor_order; |
| 1607 | |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1608 | /* first do max_partition_order */ |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1609 | for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) { |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1610 | FLAC__uint64 abs_residual_partition_sum; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1611 | FLAC__uint32 abs_r; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1612 | unsigned partition, partition_sample, partition_samples, residual_sample; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1613 | const unsigned partitions = 1u << partition_order; |
| 1614 | const unsigned default_partition_samples = blocksize >> partition_order; |
| 1615 | |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1616 | FLAC__ASSERT(default_partition_samples > predictor_order); |
| 1617 | |
| 1618 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 1619 | partition_samples = default_partition_samples; |
| 1620 | if(partition == 0) |
| 1621 | partition_samples -= predictor_order; |
| 1622 | abs_residual_partition_sum = 0; |
| 1623 | for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) { |
| 1624 | abs_r = abs_residual[residual_sample]; |
| 1625 | abs_residual_partition_sum += abs_r; |
| 1626 | residual_sample++; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1627 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1628 | abs_residual_partition_sums[partition] = abs_residual_partition_sum; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1629 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1630 | to_partition = partitions; |
| 1631 | break; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1632 | } |
Josh Coalson | f76a361 | 2001-04-18 02:28:11 +0000 | [diff] [blame] | 1633 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1634 | /* now merge partitions for lower orders */ |
Josh Coalson | 6bd1757 | 2001-05-25 19:02:01 +0000 | [diff] [blame] | 1635 | for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) { |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1636 | FLAC__uint64 s; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1637 | unsigned i; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1638 | const unsigned partitions = 1u << partition_order; |
| 1639 | for(i = 0; i < partitions; i++) { |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1640 | s = abs_residual_partition_sums[from_partition]; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1641 | from_partition++; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1642 | abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition]; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1643 | from_partition++; |
| 1644 | to_partition++; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1645 | } |
| 1646 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1647 | } |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1648 | |
| 1649 | void stream_encoder_precompute_partition_info_escapes_(const FLAC__int32 residual[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order) |
| 1650 | { |
| 1651 | int partition_order; |
| 1652 | unsigned from_partition, to_partition = 0; |
| 1653 | const unsigned blocksize = residual_samples + predictor_order; |
| 1654 | |
| 1655 | /* first do max_partition_order */ |
| 1656 | for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) { |
| 1657 | FLAC__int32 r, residual_partition_min, residual_partition_max; |
| 1658 | unsigned silog2_min, silog2_max; |
| 1659 | unsigned partition, partition_sample, partition_samples, residual_sample; |
| 1660 | const unsigned partitions = 1u << partition_order; |
| 1661 | const unsigned default_partition_samples = blocksize >> partition_order; |
| 1662 | |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1663 | FLAC__ASSERT(default_partition_samples > predictor_order); |
| 1664 | |
| 1665 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 1666 | partition_samples = default_partition_samples; |
| 1667 | if(partition == 0) |
| 1668 | partition_samples -= predictor_order; |
| 1669 | residual_partition_min = residual_partition_max = 0; |
| 1670 | for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) { |
| 1671 | r = residual[residual_sample]; |
| 1672 | if(r < residual_partition_min) |
| 1673 | residual_partition_min = r; |
| 1674 | else if(r > residual_partition_max) |
| 1675 | residual_partition_max = r; |
| 1676 | residual_sample++; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1677 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1678 | silog2_min = FLAC__bitmath_silog2(residual_partition_min); |
| 1679 | silog2_max = FLAC__bitmath_silog2(residual_partition_max); |
| 1680 | raw_bits_per_partition[partition] = max(silog2_min, silog2_max); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1681 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1682 | to_partition = partitions; |
| 1683 | break; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | /* now merge partitions for lower orders */ |
| 1687 | for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) { |
| 1688 | unsigned m; |
| 1689 | unsigned i; |
| 1690 | const unsigned partitions = 1u << partition_order; |
| 1691 | for(i = 0; i < partitions; i++) { |
| 1692 | m = raw_bits_per_partition[from_partition]; |
| 1693 | from_partition++; |
| 1694 | raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]); |
| 1695 | from_partition++; |
| 1696 | to_partition++; |
| 1697 | } |
| 1698 | } |
| 1699 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1700 | |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1701 | #ifdef VARIABLE_RICE_BITS |
| 1702 | #undef VARIABLE_RICE_BITS |
| 1703 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1704 | #ifndef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1705 | #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter)) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1706 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1707 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1708 | #ifdef DONT_ESTIMATE_RICE_BITS |
| 1709 | FLAC__bool stream_encoder_set_partitioned_rice_(const FLAC__uint32 abs_residual[], const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits) |
| 1710 | #else |
| 1711 | FLAC__bool stream_encoder_set_partitioned_rice_(const FLAC__uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits) |
| 1712 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1713 | { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1714 | unsigned rice_parameter, partition_bits; |
| 1715 | #ifndef NO_RICE_SEARCH |
| 1716 | unsigned best_partition_bits; |
| 1717 | unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0; |
| 1718 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1719 | unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; |
| 1720 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1721 | FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER); |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1722 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1723 | if(partition_order == 0) { |
| 1724 | unsigned i; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1725 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1726 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1727 | if(rice_parameter_search_dist) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1728 | if(suggested_rice_parameter < rice_parameter_search_dist) |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1729 | min_rice_parameter = 0; |
| 1730 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1731 | min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist; |
| 1732 | max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1733 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 1734 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1735 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 1736 | #endif |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1737 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1738 | } |
| 1739 | } |
| 1740 | else |
| 1741 | min_rice_parameter = max_rice_parameter = suggested_rice_parameter; |
| 1742 | |
| 1743 | best_partition_bits = 0xffffffff; |
| 1744 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 1745 | #endif |
| 1746 | #ifdef VARIABLE_RICE_BITS |
| 1747 | #ifdef FLAC__SYMMETRIC_RICE |
| 1748 | partition_bits = (2+rice_parameter) * residual_samples; |
| 1749 | #else |
| 1750 | const unsigned rice_parameter_estimate = rice_parameter-1; |
| 1751 | partition_bits = (1+rice_parameter) * residual_samples; |
| 1752 | #endif |
| 1753 | #else |
| 1754 | partition_bits = 0; |
| 1755 | #endif |
| 1756 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
| 1757 | for(i = 0; i < residual_samples; i++) { |
| 1758 | #ifdef VARIABLE_RICE_BITS |
| 1759 | #ifdef FLAC__SYMMETRIC_RICE |
| 1760 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter); |
| 1761 | #else |
| 1762 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate); |
| 1763 | #endif |
| 1764 | #else |
| 1765 | partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */ |
| 1766 | #endif |
| 1767 | } |
| 1768 | #ifndef NO_RICE_SEARCH |
| 1769 | if(partition_bits < best_partition_bits) { |
| 1770 | best_rice_parameter = rice_parameter; |
| 1771 | best_partition_bits = partition_bits; |
| 1772 | } |
| 1773 | } |
| 1774 | #endif |
| 1775 | parameters[0] = best_rice_parameter; |
| 1776 | bits_ += best_partition_bits; |
| 1777 | } |
| 1778 | else { |
| 1779 | unsigned partition, residual_sample, save_residual_sample, partition_sample; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1780 | unsigned partition_samples; |
| 1781 | FLAC__uint64 mean, k; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1782 | const unsigned partitions = 1u << partition_order; |
| 1783 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 1784 | partition_samples = (residual_samples+predictor_order) >> partition_order; |
| 1785 | if(partition == 0) { |
| 1786 | if(partition_samples <= predictor_order) |
| 1787 | return false; |
| 1788 | else |
| 1789 | partition_samples -= predictor_order; |
| 1790 | } |
| 1791 | mean = 0; |
| 1792 | save_residual_sample = residual_sample; |
| 1793 | for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1794 | mean += abs_residual[residual_sample]; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1795 | residual_sample = save_residual_sample; |
| 1796 | #ifdef FLAC__SYMMETRIC_RICE |
| 1797 | mean += partition_samples >> 1; /* for rounding effect */ |
| 1798 | mean /= partition_samples; |
| 1799 | |
| 1800 | /* calc rice_parameter = floor(log2(mean)) */ |
| 1801 | rice_parameter = 0; |
| 1802 | mean>>=1; |
| 1803 | while(mean) { |
| 1804 | rice_parameter++; |
| 1805 | mean >>= 1; |
| 1806 | } |
| 1807 | #else |
| 1808 | /* calc rice_parameter ala LOCO-I */ |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1809 | for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1810 | ; |
| 1811 | #endif |
| 1812 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 1813 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1814 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 1815 | #endif |
| 1816 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 1817 | } |
| 1818 | |
| 1819 | #ifndef NO_RICE_SEARCH |
| 1820 | if(rice_parameter_search_dist) { |
| 1821 | if(rice_parameter < rice_parameter_search_dist) |
| 1822 | min_rice_parameter = 0; |
| 1823 | else |
| 1824 | min_rice_parameter = rice_parameter - rice_parameter_search_dist; |
| 1825 | max_rice_parameter = rice_parameter + rice_parameter_search_dist; |
| 1826 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 1827 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1828 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 1829 | #endif |
| 1830 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 1831 | } |
| 1832 | } |
| 1833 | else |
| 1834 | min_rice_parameter = max_rice_parameter = rice_parameter; |
| 1835 | |
| 1836 | best_partition_bits = 0xffffffff; |
| 1837 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 1838 | #endif |
| 1839 | #ifdef VARIABLE_RICE_BITS |
| 1840 | #ifdef FLAC__SYMMETRIC_RICE |
| 1841 | partition_bits = (2+rice_parameter) * partition_samples; |
| 1842 | #else |
| 1843 | const unsigned rice_parameter_estimate = rice_parameter-1; |
| 1844 | partition_bits = (1+rice_parameter) * partition_samples; |
| 1845 | #endif |
| 1846 | #else |
| 1847 | partition_bits = 0; |
| 1848 | #endif |
| 1849 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
| 1850 | save_residual_sample = residual_sample; |
| 1851 | for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) { |
| 1852 | #ifdef VARIABLE_RICE_BITS |
| 1853 | #ifdef FLAC__SYMMETRIC_RICE |
| 1854 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter); |
| 1855 | #else |
| 1856 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate); |
| 1857 | #endif |
| 1858 | #else |
| 1859 | partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */ |
| 1860 | #endif |
| 1861 | } |
| 1862 | #ifndef NO_RICE_SEARCH |
| 1863 | if(rice_parameter != max_rice_parameter) |
| 1864 | residual_sample = save_residual_sample; |
| 1865 | if(partition_bits < best_partition_bits) { |
| 1866 | best_rice_parameter = rice_parameter; |
| 1867 | best_partition_bits = partition_bits; |
| 1868 | } |
| 1869 | } |
| 1870 | #endif |
| 1871 | parameters[partition] = best_rice_parameter; |
| 1872 | bits_ += best_partition_bits; |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | *bits = bits_; |
| 1877 | return true; |
| 1878 | } |
| 1879 | |
| 1880 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1881 | FLAC__bool stream_encoder_set_partitioned_rice_with_precompute_(const FLAC__int32 residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1882 | #else |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1883 | FLAC__bool stream_encoder_set_partitioned_rice_with_precompute_(const FLAC__uint32 abs_residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1884 | #endif |
| 1885 | { |
| 1886 | unsigned rice_parameter, partition_bits; |
| 1887 | #ifndef NO_RICE_SEARCH |
| 1888 | unsigned best_partition_bits; |
| 1889 | unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0; |
| 1890 | #endif |
| 1891 | unsigned flat_bits; |
| 1892 | unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; |
| 1893 | |
| 1894 | FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER); |
| 1895 | |
| 1896 | if(partition_order == 0) { |
| 1897 | unsigned i; |
| 1898 | |
| 1899 | #ifndef NO_RICE_SEARCH |
| 1900 | if(rice_parameter_search_dist) { |
| 1901 | if(suggested_rice_parameter < rice_parameter_search_dist) |
| 1902 | min_rice_parameter = 0; |
| 1903 | else |
| 1904 | min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist; |
| 1905 | max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist; |
| 1906 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 1907 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1908 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 1909 | #endif |
| 1910 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 1911 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1912 | } |
| 1913 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1914 | min_rice_parameter = max_rice_parameter = suggested_rice_parameter; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1915 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1916 | best_partition_bits = 0xffffffff; |
| 1917 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 1918 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1919 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1920 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1921 | partition_bits = (2+rice_parameter) * residual_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1922 | #else |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1923 | const unsigned rice_parameter_estimate = rice_parameter-1; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1924 | partition_bits = (1+rice_parameter) * residual_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1925 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1926 | #else |
| 1927 | partition_bits = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1928 | #endif |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1929 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1930 | for(i = 0; i < residual_samples; i++) { |
| 1931 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1932 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1933 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1934 | #else |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1935 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate); |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1936 | #endif |
| 1937 | #else |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1938 | partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1939 | #endif |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1940 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1941 | #ifndef NO_RICE_SEARCH |
| 1942 | if(partition_bits < best_partition_bits) { |
| 1943 | best_rice_parameter = rice_parameter; |
| 1944 | best_partition_bits = partition_bits; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1945 | } |
| 1946 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1947 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1948 | if(search_for_escapes) { |
| 1949 | 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; |
| 1950 | if(flat_bits <= best_partition_bits) { |
| 1951 | raw_bits[0] = raw_bits_per_partition[0]; |
| 1952 | best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 1953 | best_partition_bits = flat_bits; |
| 1954 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1955 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1956 | parameters[0] = best_rice_parameter; |
| 1957 | bits_ += best_partition_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1958 | } |
| 1959 | else { |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 1960 | unsigned partition, residual_sample, save_residual_sample, partition_sample; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1961 | unsigned partition_samples; |
| 1962 | FLAC__uint64 mean, k; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1963 | const unsigned partitions = 1u << partition_order; |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 1964 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1965 | partition_samples = (residual_samples+predictor_order) >> partition_order; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1966 | if(partition == 0) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1967 | if(partition_samples <= predictor_order) |
| 1968 | return false; |
| 1969 | else |
| 1970 | partition_samples -= predictor_order; |
| 1971 | } |
Josh Coalson | 05d2079 | 2001-06-29 23:12:26 +0000 | [diff] [blame] | 1972 | mean = abs_residual_partition_sums[partition]; |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1973 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 05d2079 | 2001-06-29 23:12:26 +0000 | [diff] [blame] | 1974 | mean += partition_samples >> 1; /* for rounding effect */ |
| 1975 | mean /= partition_samples; |
| 1976 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1977 | /* calc rice_parameter = floor(log2(mean)) */ |
| 1978 | rice_parameter = 0; |
| 1979 | mean>>=1; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1980 | while(mean) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1981 | rice_parameter++; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1982 | mean >>= 1; |
| 1983 | } |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1984 | #else |
Josh Coalson | 05d2079 | 2001-06-29 23:12:26 +0000 | [diff] [blame] | 1985 | /* calc rice_parameter ala LOCO-I */ |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1986 | for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1) |
Josh Coalson | 05d2079 | 2001-06-29 23:12:26 +0000 | [diff] [blame] | 1987 | ; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1988 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1989 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 1990 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1991 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 1992 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1993 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1994 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1995 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1996 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1997 | if(rice_parameter_search_dist) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1998 | if(rice_parameter < rice_parameter_search_dist) |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1999 | min_rice_parameter = 0; |
| 2000 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2001 | min_rice_parameter = rice_parameter - rice_parameter_search_dist; |
| 2002 | max_rice_parameter = rice_parameter + rice_parameter_search_dist; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2003 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 2004 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2005 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 2006 | #endif |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2007 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2008 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2009 | } |
| 2010 | else |
| 2011 | min_rice_parameter = max_rice_parameter = rice_parameter; |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2012 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2013 | best_partition_bits = 0xffffffff; |
| 2014 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 2015 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2016 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 2017 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2018 | partition_bits = (2+rice_parameter) * partition_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2019 | #else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2020 | const unsigned rice_parameter_estimate = rice_parameter-1; |
| 2021 | partition_bits = (1+rice_parameter) * partition_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2022 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2023 | #else |
| 2024 | partition_bits = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2025 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2026 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2027 | save_residual_sample = residual_sample; |
| 2028 | for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) { |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2029 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 2030 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2031 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2032 | #else |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2033 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate); |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2034 | #endif |
| 2035 | #else |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2036 | partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2037 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2038 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2039 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2040 | if(rice_parameter != max_rice_parameter) |
| 2041 | residual_sample = save_residual_sample; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2042 | if(partition_bits < best_partition_bits) { |
| 2043 | best_rice_parameter = rice_parameter; |
| 2044 | best_partition_bits = partition_bits; |
| 2045 | } |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2046 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2047 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2048 | if(search_for_escapes) { |
| 2049 | 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; |
| 2050 | if(flat_bits <= best_partition_bits) { |
| 2051 | raw_bits[partition] = raw_bits_per_partition[partition]; |
| 2052 | best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 2053 | best_partition_bits = flat_bits; |
| 2054 | } |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2055 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2056 | parameters[partition] = best_rice_parameter; |
| 2057 | bits_ += best_partition_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2058 | } |
| 2059 | } |
| 2060 | |
| 2061 | *bits = bits_; |
| 2062 | return true; |
| 2063 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 2064 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2065 | unsigned stream_encoder_get_wasted_bits_(FLAC__int32 signal[], unsigned samples) |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 2066 | { |
| 2067 | unsigned i, shift; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2068 | FLAC__int32 x = 0; |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 2069 | |
| 2070 | for(i = 0; i < samples && !(x&1); i++) |
| 2071 | x |= signal[i]; |
| 2072 | |
| 2073 | if(x == 0) { |
| 2074 | shift = 0; |
| 2075 | } |
| 2076 | else { |
| 2077 | for(shift = 0; !(x&1); shift++) |
| 2078 | x >>= 1; |
| 2079 | } |
| 2080 | |
| 2081 | if(shift > 0) { |
| 2082 | for(i = 0; i < samples; i++) |
| 2083 | signal[i] >>= shift; |
| 2084 | } |
| 2085 | |
| 2086 | return shift; |
| 2087 | } |