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