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 | |
| 20 | #include <assert.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> /* for malloc() */ |
| 23 | #include <string.h> /* for memcpy() */ |
| 24 | #include "FLAC/encoder.h" |
Josh Coalson | 0a72e18 | 2001-04-13 19:17:16 +0000 | [diff] [blame] | 25 | #include "FLAC/seek_table.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 | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 30 | #include "private/encoder_framing.h" |
| 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 | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 46 | typedef struct FLAC__EncoderPrivate { |
| 47 | unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */ |
| 48 | int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */ |
| 49 | int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */ |
| 50 | real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */ |
| 51 | real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */ |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 52 | unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */ |
Josh Coalson | ac4c158 | 2001-03-28 23:10:22 +0000 | [diff] [blame] | 53 | 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 | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 54 | int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */ |
| 55 | int32 *residual_workspace_mid_side[2][2]; |
| 56 | FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2]; |
| 57 | FLAC__Subframe subframe_workspace_mid_side[2][2]; |
| 58 | FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2]; |
| 59 | FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2]; |
| 60 | unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */ |
| 61 | unsigned best_subframe_mid_side[2]; |
| 62 | unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */ |
| 63 | unsigned best_subframe_bits_mid_side[2]; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 64 | uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */ |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 65 | uint32 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */ |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 66 | unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 67 | FLAC__BitBuffer frame; /* the current frame being worked on */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 68 | bool current_frame_can_do_mid_side; /* encoder sets this false when any given sample of a frame's side channel exceeds 16 bits */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 69 | double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */ |
| 70 | unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */ |
| 71 | unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */ |
| 72 | FLAC__ChannelAssignment last_channel_assignment; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 73 | FLAC__StreamMetaData metadata; |
| 74 | unsigned current_sample_number; |
| 75 | unsigned current_frame_number; |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 76 | struct MD5Context md5context; |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 77 | FLAC__CPUInfo cpuinfo; |
| 78 | unsigned (*local_fixed_compute_best_predictor)(const int32 data[], unsigned data_len, real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); |
| 79 | void (*local_lpc_compute_autocorrelation)(const real data[], unsigned data_len, unsigned lag, real autoc[]); |
Josh Coalson | eef5670 | 2001-03-30 00:45:22 +0000 | [diff] [blame] | 80 | bool use_slow; /* use slow 64-bit versions of some functions */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 81 | FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data); |
| 82 | void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data); |
| 83 | void *client_data; |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 84 | /* unaligned (original) pointers to allocated data */ |
| 85 | int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS]; |
| 86 | int32 *integer_signal_mid_side_unaligned[2]; |
| 87 | real *real_signal_unaligned[FLAC__MAX_CHANNELS]; |
| 88 | real *real_signal_mid_side_unaligned[2]; |
| 89 | int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2]; |
| 90 | int32 *residual_workspace_mid_side_unaligned[2][2]; |
| 91 | uint32 *abs_residual_unaligned; |
| 92 | uint32 *abs_residual_partition_sums_unaligned; |
| 93 | unsigned *raw_bits_per_partition_unaligned; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 94 | } FLAC__EncoderPrivate; |
| 95 | |
| 96 | static bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size); |
| 97 | static bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 98 | static bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame); |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 99 | static bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned min_partition_order, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 100 | static bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame); |
| 101 | static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe); |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 102 | static unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 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, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe); |
| 103 | static unsigned encoder_evaluate_lpc_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const 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, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 104 | static unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe); |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 105 | static unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], uint32 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, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[]); |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 106 | #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES) |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 107 | static unsigned encoder_precompute_partition_info_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order); |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 108 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 109 | static bool encoder_set_partitioned_rice_(const uint32 abs_residual[], const uint32 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, unsigned parameters[], unsigned raw_bits[], unsigned *bits); |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 110 | static unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 111 | |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 112 | const char *FLAC__EncoderWriteStatusString[] = { |
| 113 | "FLAC__ENCODER_WRITE_OK", |
| 114 | "FLAC__ENCODER_WRITE_FATAL_ERROR" |
| 115 | }; |
| 116 | |
| 117 | const char *FLAC__EncoderStateString[] = { |
| 118 | "FLAC__ENCODER_OK", |
| 119 | "FLAC__ENCODER_UNINITIALIZED", |
| 120 | "FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS", |
| 121 | "FLAC__ENCODER_INVALID_BITS_PER_SAMPLE", |
| 122 | "FLAC__ENCODER_INVALID_SAMPLE_RATE", |
| 123 | "FLAC__ENCODER_INVALID_BLOCK_SIZE", |
| 124 | "FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION", |
| 125 | "FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH", |
| 126 | "FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH", |
Josh Coalson | 69f1ee0 | 2001-01-24 00:54:43 +0000 | [diff] [blame] | 127 | "FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE", |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 128 | "FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER", |
| 129 | "FLAC__ENCODER_NOT_STREAMABLE", |
| 130 | "FLAC__ENCODER_FRAMING_ERROR", |
| 131 | "FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING", |
| 132 | "FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING", |
| 133 | "FLAC__ENCODER_MEMORY_ALLOCATION_ERROR" |
| 134 | }; |
| 135 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 136 | |
| 137 | bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size) |
| 138 | { |
| 139 | bool ok; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 140 | unsigned i, channel; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 141 | |
| 142 | assert(new_size > 0); |
| 143 | assert(encoder->state == FLAC__ENCODER_OK); |
| 144 | assert(encoder->guts->current_sample_number == 0); |
| 145 | |
| 146 | /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */ |
| 147 | if(new_size <= encoder->guts->input_capacity) |
| 148 | return true; |
| 149 | |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 150 | ok = true; |
| 151 | for(i = 0; ok && i < encoder->channels; i++) { |
| 152 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->integer_signal_unaligned[i], &encoder->guts->integer_signal[i]); |
| 153 | ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->guts->real_signal_unaligned[i], &encoder->guts->real_signal[i]); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 154 | } |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 155 | for(i = 0; ok && i < 2; i++) { |
| 156 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->integer_signal_mid_side_unaligned[i], &encoder->guts->integer_signal_mid_side[i]); |
| 157 | ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->guts->real_signal_mid_side_unaligned[i], &encoder->guts->real_signal_mid_side[i]); |
| 158 | } |
| 159 | for(channel = 0; ok && channel < encoder->channels; channel++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 160 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 161 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->residual_workspace_unaligned[channel][i], &encoder->guts->residual_workspace[channel][i]); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 164 | for(channel = 0; ok && channel < 2; channel++) { |
| 165 | for(i = 0; ok && i < 2; i++) { |
| 166 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->residual_workspace_mid_side_unaligned[channel][i], &encoder->guts->residual_workspace_mid_side[channel][i]); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 167 | } |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 168 | } |
| 169 | ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->guts->abs_residual_unaligned, &encoder->guts->abs_residual); |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 170 | #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 171 | ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size * 2, &encoder->guts->abs_residual_partition_sums_unaligned, &encoder->guts->abs_residual_partition_sums); |
Josh Coalson | 9661108 | 2001-04-24 01:30:10 +0000 | [diff] [blame] | 172 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 173 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 174 | ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->guts->raw_bits_per_partition_unaligned, &encoder->guts->raw_bits_per_partition); |
Josh Coalson | 9661108 | 2001-04-24 01:30:10 +0000 | [diff] [blame] | 175 | #endif |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 176 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 177 | if(ok) |
| 178 | encoder->guts->input_capacity = new_size; |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 179 | else |
| 180 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 181 | |
| 182 | return ok; |
| 183 | } |
| 184 | |
| 185 | FLAC__Encoder *FLAC__encoder_get_new_instance() |
| 186 | { |
| 187 | FLAC__Encoder *encoder = (FLAC__Encoder*)malloc(sizeof(FLAC__Encoder)); |
| 188 | if(encoder != 0) { |
| 189 | encoder->state = FLAC__ENCODER_UNINITIALIZED; |
| 190 | encoder->guts = 0; |
| 191 | } |
| 192 | return encoder; |
| 193 | } |
| 194 | |
| 195 | void FLAC__encoder_free_instance(FLAC__Encoder *encoder) |
| 196 | { |
| 197 | assert(encoder != 0); |
| 198 | free(encoder); |
| 199 | } |
| 200 | |
| 201 | FLAC__EncoderState FLAC__encoder_init(FLAC__Encoder *encoder, FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data), void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data), void *client_data) |
| 202 | { |
| 203 | unsigned i; |
Josh Coalson | c692d38 | 2001-02-23 21:05:05 +0000 | [diff] [blame] | 204 | FLAC__StreamMetaData padding; |
Josh Coalson | 0a72e18 | 2001-04-13 19:17:16 +0000 | [diff] [blame] | 205 | FLAC__StreamMetaData seek_table; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 206 | |
| 207 | assert(sizeof(int) >= 4); /* we want to die right away if this is not true */ |
| 208 | assert(encoder != 0); |
| 209 | assert(write_callback != 0); |
| 210 | assert(metadata_callback != 0); |
| 211 | assert(encoder->state == FLAC__ENCODER_UNINITIALIZED); |
| 212 | assert(encoder->guts == 0); |
| 213 | |
| 214 | encoder->state = FLAC__ENCODER_OK; |
| 215 | |
| 216 | if(encoder->channels == 0 || encoder->channels > FLAC__MAX_CHANNELS) |
| 217 | return encoder->state = FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS; |
| 218 | |
| 219 | if(encoder->do_mid_side_stereo && encoder->channels != 2) |
| 220 | return encoder->state = FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH; |
| 221 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 222 | if(encoder->loose_mid_side_stereo && !encoder->do_mid_side_stereo) |
Josh Coalson | 69f1ee0 | 2001-01-24 00:54:43 +0000 | [diff] [blame] | 223 | return encoder->state = FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE; |
| 224 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 225 | if(encoder->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 226 | return encoder->state = FLAC__ENCODER_INVALID_BITS_PER_SAMPLE; |
| 227 | |
| 228 | if(encoder->sample_rate == 0 || encoder->sample_rate > FLAC__MAX_SAMPLE_RATE) |
| 229 | return encoder->state = FLAC__ENCODER_INVALID_SAMPLE_RATE; |
| 230 | |
| 231 | if(encoder->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->blocksize > FLAC__MAX_BLOCK_SIZE) |
| 232 | return encoder->state = FLAC__ENCODER_INVALID_BLOCK_SIZE; |
| 233 | |
| 234 | if(encoder->blocksize < encoder->max_lpc_order) |
| 235 | return encoder->state = FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER; |
| 236 | |
| 237 | if(encoder->qlp_coeff_precision == 0) { |
| 238 | if(encoder->bits_per_sample < 16) { |
| 239 | /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */ |
| 240 | /* @@@ until then we'll make a guess */ |
| 241 | encoder->qlp_coeff_precision = max(5, 2 + encoder->bits_per_sample / 2); |
| 242 | } |
| 243 | else if(encoder->bits_per_sample == 16) { |
| 244 | if(encoder->blocksize <= 192) |
| 245 | encoder->qlp_coeff_precision = 7; |
| 246 | else if(encoder->blocksize <= 384) |
| 247 | encoder->qlp_coeff_precision = 8; |
| 248 | else if(encoder->blocksize <= 576) |
| 249 | encoder->qlp_coeff_precision = 9; |
| 250 | else if(encoder->blocksize <= 1152) |
| 251 | encoder->qlp_coeff_precision = 10; |
| 252 | else if(encoder->blocksize <= 2304) |
| 253 | encoder->qlp_coeff_precision = 11; |
| 254 | else if(encoder->blocksize <= 4608) |
| 255 | encoder->qlp_coeff_precision = 12; |
| 256 | else |
| 257 | encoder->qlp_coeff_precision = 13; |
| 258 | } |
| 259 | else { |
| 260 | encoder->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->bits_per_sample - 1); |
| 261 | } |
| 262 | } |
Josh Coalson | 0552fdf | 2001-02-26 20:29:56 +0000 | [diff] [blame] | 263 | else if(encoder->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->qlp_coeff_precision + encoder->bits_per_sample >= 8*sizeof(uint32) || encoder->qlp_coeff_precision >= (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 264 | return encoder->state = FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION; |
| 265 | |
| 266 | if(encoder->streamable_subset) { |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 267 | //@@@ add check for blocksize here |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 268 | if(encoder->bits_per_sample != 8 && encoder->bits_per_sample != 12 && encoder->bits_per_sample != 16 && encoder->bits_per_sample != 20 && encoder->bits_per_sample != 24) |
| 269 | return encoder->state = FLAC__ENCODER_NOT_STREAMABLE; |
| 270 | if(encoder->sample_rate > 655350) |
| 271 | return encoder->state = FLAC__ENCODER_NOT_STREAMABLE; |
| 272 | } |
| 273 | |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 274 | if(encoder->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN)) |
| 275 | encoder->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1; |
| 276 | if(encoder->min_residual_partition_order >= encoder->max_residual_partition_order) |
| 277 | encoder->min_residual_partition_order = encoder->max_residual_partition_order; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 278 | |
| 279 | encoder->guts = (FLAC__EncoderPrivate*)malloc(sizeof(FLAC__EncoderPrivate)); |
| 280 | if(encoder->guts == 0) |
| 281 | return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 282 | |
| 283 | encoder->guts->input_capacity = 0; |
| 284 | for(i = 0; i < encoder->channels; i++) { |
Josh Coalson | 6bd1757 | 2001-05-25 19:02:01 +0000 | [diff] [blame^] | 285 | encoder->guts->integer_signal_unaligned[i] = encoder->guts->integer_signal[i] = 0; |
| 286 | encoder->guts->real_signal_unaligned[i] = encoder->guts->real_signal[i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 287 | } |
| 288 | for(i = 0; i < 2; i++) { |
Josh Coalson | 6bd1757 | 2001-05-25 19:02:01 +0000 | [diff] [blame^] | 289 | encoder->guts->integer_signal_mid_side_unaligned[i] = encoder->guts->integer_signal_mid_side[i] = 0; |
| 290 | encoder->guts->real_signal_mid_side_unaligned[i] = encoder->guts->real_signal_mid_side[i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 291 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 292 | for(i = 0; i < encoder->channels; i++) { |
Josh Coalson | 6bd1757 | 2001-05-25 19:02:01 +0000 | [diff] [blame^] | 293 | encoder->guts->residual_workspace_unaligned[i][0] = encoder->guts->residual_workspace[i][0] = 0; |
| 294 | encoder->guts->residual_workspace_unaligned[i][1] = encoder->guts->residual_workspace[i][1] = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 295 | encoder->guts->best_subframe[i] = 0; |
| 296 | } |
| 297 | for(i = 0; i < 2; i++) { |
Josh Coalson | 6bd1757 | 2001-05-25 19:02:01 +0000 | [diff] [blame^] | 298 | encoder->guts->residual_workspace_mid_side_unaligned[i][0] = encoder->guts->residual_workspace_mid_side[i][0] = 0; |
| 299 | encoder->guts->residual_workspace_mid_side_unaligned[i][1] = encoder->guts->residual_workspace_mid_side[i][1] = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 300 | encoder->guts->best_subframe_mid_side[i] = 0; |
| 301 | } |
| 302 | for(i = 0; i < encoder->channels; i++) { |
| 303 | encoder->guts->subframe_workspace_ptr[i][0] = &encoder->guts->subframe_workspace[i][0]; |
| 304 | encoder->guts->subframe_workspace_ptr[i][1] = &encoder->guts->subframe_workspace[i][1]; |
| 305 | } |
| 306 | for(i = 0; i < 2; i++) { |
| 307 | encoder->guts->subframe_workspace_ptr_mid_side[i][0] = &encoder->guts->subframe_workspace_mid_side[i][0]; |
| 308 | encoder->guts->subframe_workspace_ptr_mid_side[i][1] = &encoder->guts->subframe_workspace_mid_side[i][1]; |
| 309 | } |
Josh Coalson | 6bd1757 | 2001-05-25 19:02:01 +0000 | [diff] [blame^] | 310 | encoder->guts->abs_residual_unaligned = encoder->guts->abs_residual = 0; |
| 311 | encoder->guts->abs_residual_partition_sums_unaligned = encoder->guts->abs_residual_partition_sums = 0; |
| 312 | encoder->guts->raw_bits_per_partition_unaligned = encoder->guts->raw_bits_per_partition = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 313 | encoder->guts->current_frame_can_do_mid_side = true; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 314 | encoder->guts->loose_mid_side_stereo_frames_exact = (double)encoder->sample_rate * 0.4 / (double)encoder->blocksize; |
| 315 | encoder->guts->loose_mid_side_stereo_frames = (unsigned)(encoder->guts->loose_mid_side_stereo_frames_exact + 0.5); |
| 316 | if(encoder->guts->loose_mid_side_stereo_frames == 0) |
| 317 | encoder->guts->loose_mid_side_stereo_frames = 1; |
| 318 | encoder->guts->loose_mid_side_stereo_frame_count = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 319 | encoder->guts->current_sample_number = 0; |
| 320 | encoder->guts->current_frame_number = 0; |
| 321 | |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 322 | /* |
| 323 | * get the CPU info and set the function pointers |
| 324 | */ |
| 325 | FLAC__cpu_info(&encoder->guts->cpuinfo); |
| 326 | /* first default to the non-asm routines */ |
| 327 | encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation; |
| 328 | encoder->guts->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor; |
| 329 | /* now override with asm where appropriate */ |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 330 | #ifndef FLAC__NO_ASM |
| 331 | assert(encoder->guts->cpuinfo.use_asm); |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 332 | #ifdef FLAC__CPU_IA32 |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 333 | assert(encoder->guts->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32); |
Josh Coalson | 034d38e | 2001-05-24 19:29:30 +0000 | [diff] [blame] | 334 | #ifdef FLAC__HAS_NASM |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 335 | #if 0 |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 336 | /* @@@ SSE version not working yet */ |
| 337 | if(encoder->guts->cpuinfo.data.ia32.sse) |
| 338 | encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse; |
| 339 | else |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 340 | #endif |
| 341 | fprintf(stderr,"@@@ got _asm_i386 of lpc_compute_autocorrelation()\n"); |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 342 | encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386; |
| 343 | if(encoder->guts->cpuinfo.data.ia32.mmx && encoder->guts->cpuinfo.data.ia32.cmov) |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 344 | { |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 345 | encoder->guts->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_i386_mmx_cmov; |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 346 | fprintf(stderr,"@@@ got _asm_i386_mmx_cmov of fixed_compute_best_predictor()\n");} |
| 347 | #endif |
Josh Coalson | 034d38e | 2001-05-24 19:29:30 +0000 | [diff] [blame] | 348 | #endif |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 349 | #endif |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 350 | |
Josh Coalson | eef5670 | 2001-03-30 00:45:22 +0000 | [diff] [blame] | 351 | if(encoder->bits_per_sample + FLAC__bitmath_ilog2(encoder->blocksize)+1 > 30) |
| 352 | encoder->guts->use_slow = true; |
| 353 | else |
| 354 | encoder->guts->use_slow = false; |
| 355 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 356 | if(!encoder_resize_buffers_(encoder, encoder->blocksize)) { |
| 357 | /* the above function sets the state for us in case of an error */ |
| 358 | return encoder->state; |
| 359 | } |
| 360 | FLAC__bitbuffer_init(&encoder->guts->frame); |
| 361 | encoder->guts->write_callback = write_callback; |
| 362 | encoder->guts->metadata_callback = metadata_callback; |
| 363 | encoder->guts->client_data = client_data; |
| 364 | |
| 365 | /* |
| 366 | * write the stream header |
| 367 | */ |
| 368 | if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) |
| 369 | return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 370 | |
| 371 | if(!FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) |
| 372 | return encoder->state = FLAC__ENCODER_FRAMING_ERROR; |
| 373 | |
Josh Coalson | c692d38 | 2001-02-23 21:05:05 +0000 | [diff] [blame] | 374 | encoder->guts->metadata.type = FLAC__METADATA_TYPE_STREAMINFO; |
Josh Coalson | 0a72e18 | 2001-04-13 19:17:16 +0000 | [diff] [blame] | 375 | encoder->guts->metadata.is_last = (encoder->seek_table == 0 && encoder->padding == 0); |
Josh Coalson | c692d38 | 2001-02-23 21:05:05 +0000 | [diff] [blame] | 376 | encoder->guts->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH; |
| 377 | encoder->guts->metadata.data.stream_info.min_blocksize = encoder->blocksize; /* this encoder uses the same blocksize for the whole stream */ |
| 378 | encoder->guts->metadata.data.stream_info.max_blocksize = encoder->blocksize; |
| 379 | encoder->guts->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 380 | encoder->guts->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 381 | encoder->guts->metadata.data.stream_info.sample_rate = encoder->sample_rate; |
| 382 | encoder->guts->metadata.data.stream_info.channels = encoder->channels; |
| 383 | encoder->guts->metadata.data.stream_info.bits_per_sample = encoder->bits_per_sample; |
| 384 | encoder->guts->metadata.data.stream_info.total_samples = encoder->total_samples_estimate; /* we will replace this later with the real total */ |
| 385 | memset(encoder->guts->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */ |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 386 | MD5Init(&encoder->guts->md5context); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 387 | if(!FLAC__add_metadata_block(&encoder->guts->metadata, &encoder->guts->frame)) |
| 388 | return encoder->state = FLAC__ENCODER_FRAMING_ERROR; |
| 389 | |
Josh Coalson | 0a72e18 | 2001-04-13 19:17:16 +0000 | [diff] [blame] | 390 | if(0 != encoder->seek_table) { |
| 391 | if(!FLAC__seek_table_is_valid(encoder->seek_table)) |
| 392 | return encoder->state = FLAC__ENCODER_INVALID_SEEK_TABLE; |
| 393 | seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE; |
| 394 | seek_table.is_last = (encoder->padding == 0); |
| 395 | seek_table.length = encoder->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN; |
| 396 | seek_table.data.seek_table = *encoder->seek_table; |
| 397 | if(!FLAC__add_metadata_block(&seek_table, &encoder->guts->frame)) |
| 398 | return encoder->state = FLAC__ENCODER_FRAMING_ERROR; |
| 399 | } |
| 400 | |
Josh Coalson | c692d38 | 2001-02-23 21:05:05 +0000 | [diff] [blame] | 401 | /* add a PADDING block if requested */ |
| 402 | if(encoder->padding > 0) { |
| 403 | padding.type = FLAC__METADATA_TYPE_PADDING; |
| 404 | padding.is_last = true; |
| 405 | padding.length = encoder->padding; |
| 406 | if(!FLAC__add_metadata_block(&padding, &encoder->guts->frame)) |
| 407 | return encoder->state = FLAC__ENCODER_FRAMING_ERROR; |
| 408 | } |
| 409 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 410 | assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */ |
| 411 | assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */ |
| 412 | if(encoder->guts->write_callback(encoder, encoder->guts->frame.buffer, encoder->guts->frame.bytes, 0, encoder->guts->current_frame_number, encoder->guts->client_data) != FLAC__ENCODER_WRITE_OK) |
| 413 | return encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING; |
| 414 | |
Josh Coalson | cbbbb5f | 2001-01-23 00:41:48 +0000 | [diff] [blame] | 415 | /* now that the metadata block is written, we can init this to an absurdly-high value... */ |
Josh Coalson | c692d38 | 2001-02-23 21:05:05 +0000 | [diff] [blame] | 416 | encoder->guts->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] | 417 | /* ... and clear this to 0 */ |
Josh Coalson | c692d38 | 2001-02-23 21:05:05 +0000 | [diff] [blame] | 418 | encoder->guts->metadata.data.stream_info.total_samples = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 419 | |
| 420 | return encoder->state; |
| 421 | } |
| 422 | |
| 423 | void FLAC__encoder_finish(FLAC__Encoder *encoder) |
| 424 | { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 425 | unsigned i, channel; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 426 | |
| 427 | assert(encoder != 0); |
| 428 | if(encoder->state == FLAC__ENCODER_UNINITIALIZED) |
| 429 | return; |
| 430 | if(encoder->guts->current_sample_number != 0) { |
| 431 | encoder->blocksize = encoder->guts->current_sample_number; |
| 432 | encoder_process_frame_(encoder, true); /* true => is last frame */ |
| 433 | } |
Josh Coalson | c692d38 | 2001-02-23 21:05:05 +0000 | [diff] [blame] | 434 | MD5Final(encoder->guts->metadata.data.stream_info.md5sum, &encoder->guts->md5context); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 435 | encoder->guts->metadata_callback(encoder, &encoder->guts->metadata, encoder->guts->client_data); |
| 436 | if(encoder->guts != 0) { |
| 437 | for(i = 0; i < encoder->channels; i++) { |
Josh Coalson | 1830194 | 2001-05-16 19:25:33 +0000 | [diff] [blame] | 438 | if(encoder->guts->integer_signal_unaligned[i] != 0) { |
| 439 | free(encoder->guts->integer_signal_unaligned[i]); |
| 440 | encoder->guts->integer_signal_unaligned[i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 441 | } |
Josh Coalson | 1830194 | 2001-05-16 19:25:33 +0000 | [diff] [blame] | 442 | if(encoder->guts->real_signal_unaligned[i] != 0) { |
| 443 | free(encoder->guts->real_signal_unaligned[i]); |
| 444 | encoder->guts->real_signal_unaligned[i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | for(i = 0; i < 2; i++) { |
Josh Coalson | 1830194 | 2001-05-16 19:25:33 +0000 | [diff] [blame] | 448 | if(encoder->guts->integer_signal_mid_side_unaligned[i] != 0) { |
| 449 | free(encoder->guts->integer_signal_mid_side_unaligned[i]); |
| 450 | encoder->guts->integer_signal_mid_side_unaligned[i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 451 | } |
Josh Coalson | 1830194 | 2001-05-16 19:25:33 +0000 | [diff] [blame] | 452 | if(encoder->guts->real_signal_mid_side_unaligned[i] != 0) { |
| 453 | free(encoder->guts->real_signal_mid_side_unaligned[i]); |
| 454 | encoder->guts->real_signal_mid_side_unaligned[i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 457 | for(channel = 0; channel < encoder->channels; channel++) { |
| 458 | for(i = 0; i < 2; i++) { |
Josh Coalson | 1830194 | 2001-05-16 19:25:33 +0000 | [diff] [blame] | 459 | if(encoder->guts->residual_workspace_unaligned[channel][i] != 0) { |
| 460 | free(encoder->guts->residual_workspace_unaligned[channel][i]); |
| 461 | encoder->guts->residual_workspace_unaligned[channel][i] = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | } |
| 465 | for(channel = 0; channel < 2; channel++) { |
| 466 | for(i = 0; i < 2; i++) { |
Josh Coalson | 1830194 | 2001-05-16 19:25:33 +0000 | [diff] [blame] | 467 | if(encoder->guts->residual_workspace_mid_side_unaligned[channel][i] != 0) { |
| 468 | free(encoder->guts->residual_workspace_mid_side_unaligned[channel][i]); |
| 469 | encoder->guts->residual_workspace_mid_side_unaligned[channel][i] = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 470 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
Josh Coalson | 1830194 | 2001-05-16 19:25:33 +0000 | [diff] [blame] | 473 | if(encoder->guts->abs_residual_unaligned != 0) { |
| 474 | free(encoder->guts->abs_residual_unaligned); |
| 475 | encoder->guts->abs_residual_unaligned = 0; |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 476 | } |
Josh Coalson | 1830194 | 2001-05-16 19:25:33 +0000 | [diff] [blame] | 477 | if(encoder->guts->abs_residual_partition_sums_unaligned != 0) { |
| 478 | free(encoder->guts->abs_residual_partition_sums_unaligned); |
| 479 | encoder->guts->abs_residual_partition_sums_unaligned = 0; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 480 | } |
Josh Coalson | 1830194 | 2001-05-16 19:25:33 +0000 | [diff] [blame] | 481 | if(encoder->guts->raw_bits_per_partition_unaligned != 0) { |
| 482 | free(encoder->guts->raw_bits_per_partition_unaligned); |
| 483 | encoder->guts->raw_bits_per_partition_unaligned = 0; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 484 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 485 | FLAC__bitbuffer_free(&encoder->guts->frame); |
| 486 | free(encoder->guts); |
| 487 | encoder->guts = 0; |
| 488 | } |
| 489 | encoder->state = FLAC__ENCODER_UNINITIALIZED; |
| 490 | } |
| 491 | |
| 492 | bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned samples) |
| 493 | { |
| 494 | unsigned i, j, channel; |
| 495 | int32 x, mid, side; |
| 496 | const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2; |
| 497 | const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1)); |
| 498 | const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1; |
| 499 | |
| 500 | assert(encoder != 0); |
| 501 | assert(encoder->state == FLAC__ENCODER_OK); |
| 502 | |
| 503 | j = 0; |
| 504 | do { |
| 505 | for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++) { |
| 506 | for(channel = 0; channel < encoder->channels; channel++) { |
| 507 | x = buf[channel][j]; |
| 508 | encoder->guts->integer_signal[channel][i] = x; |
| 509 | encoder->guts->real_signal[channel][i] = (real)x; |
| 510 | } |
| 511 | if(ms && encoder->guts->current_frame_can_do_mid_side) { |
| 512 | side = buf[0][j] - buf[1][j]; |
| 513 | if(side < min_side || side > max_side) { |
| 514 | encoder->guts->current_frame_can_do_mid_side = false; |
| 515 | } |
| 516 | else { |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 517 | mid = (buf[0][j] + buf[1][j]) >> 1; /* NOTE: not the same as 'mid = (buf[0][j] + buf[1][j]) / 2' ! */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 518 | encoder->guts->integer_signal_mid_side[0][i] = mid; |
| 519 | encoder->guts->integer_signal_mid_side[1][i] = side; |
| 520 | encoder->guts->real_signal_mid_side[0][i] = (real)mid; |
| 521 | encoder->guts->real_signal_mid_side[1][i] = (real)side; |
| 522 | } |
| 523 | } |
| 524 | encoder->guts->current_sample_number++; |
| 525 | } |
| 526 | if(i == encoder->blocksize) { |
| 527 | if(!encoder_process_frame_(encoder, false)) /* false => not last frame */ |
| 528 | return false; |
| 529 | } |
| 530 | } while(j < samples); |
| 531 | |
| 532 | return true; |
| 533 | } |
| 534 | |
| 535 | /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */ |
| 536 | bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[], unsigned samples) |
| 537 | { |
| 538 | unsigned i, j, k, channel; |
| 539 | int32 x, left = 0, mid, side; |
| 540 | const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2; |
| 541 | const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1)); |
| 542 | const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1; |
| 543 | |
| 544 | assert(encoder != 0); |
| 545 | assert(encoder->state == FLAC__ENCODER_OK); |
| 546 | |
| 547 | j = k = 0; |
| 548 | do { |
| 549 | for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++, k++) { |
| 550 | for(channel = 0; channel < encoder->channels; channel++, k++) { |
| 551 | x = buf[k]; |
| 552 | encoder->guts->integer_signal[channel][i] = x; |
| 553 | encoder->guts->real_signal[channel][i] = (real)x; |
| 554 | if(ms && encoder->guts->current_frame_can_do_mid_side) { |
| 555 | if(channel == 0) { |
| 556 | left = x; |
| 557 | } |
| 558 | else { |
| 559 | side = left - x; |
| 560 | if(side < min_side || side > max_side) { |
| 561 | encoder->guts->current_frame_can_do_mid_side = false; |
| 562 | } |
| 563 | else { |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 564 | mid = (left + x) >> 1; /* NOTE: not the same as 'mid = (left + x) / 2' ! */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 565 | encoder->guts->integer_signal_mid_side[0][i] = mid; |
| 566 | encoder->guts->integer_signal_mid_side[1][i] = side; |
| 567 | encoder->guts->real_signal_mid_side[0][i] = (real)mid; |
| 568 | encoder->guts->real_signal_mid_side[1][i] = (real)side; |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | encoder->guts->current_sample_number++; |
| 574 | } |
| 575 | if(i == encoder->blocksize) { |
| 576 | if(!encoder_process_frame_(encoder, false)) /* false => not last frame */ |
| 577 | return false; |
| 578 | } |
| 579 | } while(j < samples); |
| 580 | |
| 581 | return true; |
| 582 | } |
| 583 | |
| 584 | bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame) |
| 585 | { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 586 | assert(encoder->state == FLAC__ENCODER_OK); |
| 587 | |
| 588 | /* |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 589 | * Accumulate raw signal to the MD5 signature |
| 590 | */ |
Josh Coalson | eae4dde | 2001-04-16 05:33:22 +0000 | [diff] [blame] | 591 | /* 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 | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 592 | if(!FLAC__MD5Accumulate(&encoder->guts->md5context, encoder->guts->integer_signal, encoder->channels, encoder->blocksize, (encoder->bits_per_sample+7) / 8)) { |
| 593 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 594 | return false; |
| 595 | } |
| 596 | |
| 597 | /* |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 598 | * Process the frame header and subframes into the frame bitbuffer |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 599 | */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 600 | if(!encoder_process_subframes_(encoder, is_last_frame)) { |
| 601 | /* 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] | 602 | return false; |
| 603 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 604 | |
| 605 | /* |
| 606 | * Zero-pad the frame to a byte_boundary |
| 607 | */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 608 | if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->guts->frame)) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 609 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 610 | return false; |
| 611 | } |
| 612 | |
| 613 | /* |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 614 | * CRC-16 the whole thing |
| 615 | */ |
| 616 | assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned */ |
| 617 | assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */ |
| 618 | FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__crc16(encoder->guts->frame.buffer, encoder->guts->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN); |
| 619 | |
| 620 | /* |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 621 | * Write it |
| 622 | */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 623 | if(encoder->guts->write_callback(encoder, encoder->guts->frame.buffer, encoder->guts->frame.bytes, encoder->blocksize, encoder->guts->current_frame_number, encoder->guts->client_data) != FLAC__ENCODER_WRITE_OK) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 624 | encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING; |
| 625 | return false; |
| 626 | } |
| 627 | |
| 628 | /* |
| 629 | * Get ready for the next frame |
| 630 | */ |
| 631 | encoder->guts->current_frame_can_do_mid_side = true; |
| 632 | encoder->guts->current_sample_number = 0; |
| 633 | encoder->guts->current_frame_number++; |
Josh Coalson | c692d38 | 2001-02-23 21:05:05 +0000 | [diff] [blame] | 634 | encoder->guts->metadata.data.stream_info.total_samples += (uint64)encoder->blocksize; |
| 635 | encoder->guts->metadata.data.stream_info.min_framesize = min(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.min_framesize); |
| 636 | encoder->guts->metadata.data.stream_info.max_framesize = max(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.max_framesize); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 637 | |
| 638 | return true; |
| 639 | } |
| 640 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 641 | bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame) |
| 642 | { |
| 643 | FLAC__FrameHeader frame_header; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 644 | unsigned channel, min_partition_order = encoder->min_residual_partition_order, max_partition_order; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 645 | bool do_independent, do_mid_side; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 646 | |
| 647 | /* |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 648 | * Calculate the min,max Rice partition orders |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 649 | */ |
| 650 | if(is_last_frame) { |
| 651 | max_partition_order = 0; |
| 652 | } |
| 653 | else { |
| 654 | unsigned limit = 0, b = encoder->blocksize; |
| 655 | while(!(b & 1)) { |
| 656 | limit++; |
| 657 | b >>= 1; |
| 658 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 659 | max_partition_order = min(encoder->max_residual_partition_order, limit); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 660 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 661 | min_partition_order = min(min_partition_order, max_partition_order); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 662 | |
| 663 | /* |
| 664 | * Setup the frame |
| 665 | */ |
| 666 | if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) { |
| 667 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 668 | return false; |
| 669 | } |
| 670 | frame_header.blocksize = encoder->blocksize; |
| 671 | frame_header.sample_rate = encoder->sample_rate; |
| 672 | frame_header.channels = encoder->channels; |
| 673 | frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */ |
| 674 | frame_header.bits_per_sample = encoder->bits_per_sample; |
| 675 | frame_header.number.frame_number = encoder->guts->current_frame_number; |
| 676 | |
| 677 | /* |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 678 | * Figure out what channel assignments to try |
| 679 | */ |
| 680 | if(encoder->do_mid_side_stereo) { |
| 681 | if(encoder->loose_mid_side_stereo) { |
| 682 | if(encoder->guts->loose_mid_side_stereo_frame_count == 0) { |
| 683 | do_independent = true; |
| 684 | do_mid_side = true; |
| 685 | } |
| 686 | else { |
| 687 | do_independent = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT); |
| 688 | do_mid_side = !do_independent; |
| 689 | } |
| 690 | } |
| 691 | else { |
| 692 | do_independent = true; |
| 693 | do_mid_side = true; |
| 694 | } |
| 695 | } |
| 696 | else { |
| 697 | do_independent = true; |
| 698 | do_mid_side = false; |
| 699 | } |
| 700 | if(do_mid_side && !encoder->guts->current_frame_can_do_mid_side) { |
| 701 | do_independent = true; |
| 702 | do_mid_side = false; |
| 703 | } |
| 704 | |
| 705 | assert(do_independent || do_mid_side); |
| 706 | |
| 707 | /* |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 708 | * Check for wasted bits; set effective bps for each subframe |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 709 | */ |
| 710 | if(do_independent) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 711 | unsigned w; |
| 712 | for(channel = 0; channel < encoder->channels; channel++) { |
| 713 | w = encoder_get_wasted_bits_(encoder->guts->integer_signal[channel], encoder->blocksize); |
| 714 | encoder->guts->subframe_workspace[channel][0].wasted_bits = encoder->guts->subframe_workspace[channel][1].wasted_bits = w; |
| 715 | encoder->guts->subframe_bps[channel] = encoder->bits_per_sample - w; |
| 716 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 717 | } |
| 718 | if(do_mid_side) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 719 | unsigned w; |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 720 | assert(encoder->channels == 2); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 721 | for(channel = 0; channel < 2; channel++) { |
| 722 | w = encoder_get_wasted_bits_(encoder->guts->integer_signal_mid_side[channel], encoder->blocksize); |
| 723 | encoder->guts->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->guts->subframe_workspace_mid_side[channel][1].wasted_bits = w; |
| 724 | encoder->guts->subframe_bps_mid_side[channel] = encoder->bits_per_sample - w + (channel==0? 0:1); |
| 725 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /* |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 729 | * First do a normal encoding pass of each independent channel |
| 730 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 731 | if(do_independent) { |
| 732 | for(channel = 0; channel < encoder->channels; channel++) { |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 733 | if(!encoder_process_subframe_(encoder, min_partition_order, max_partition_order, false, &frame_header, encoder->guts->subframe_bps[channel], encoder->guts->integer_signal[channel], encoder->guts->real_signal[channel], encoder->guts->subframe_workspace_ptr[channel], encoder->guts->residual_workspace[channel], encoder->guts->best_subframe+channel, encoder->guts->best_subframe_bits+channel)) |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 734 | return false; |
| 735 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /* |
| 739 | * Now do mid and side channels if requested |
| 740 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 741 | if(do_mid_side) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 742 | assert(encoder->channels == 2); |
| 743 | |
| 744 | for(channel = 0; channel < 2; channel++) { |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 745 | if(!encoder_process_subframe_(encoder, min_partition_order, max_partition_order, false, &frame_header, encoder->guts->subframe_bps_mid_side[channel], encoder->guts->integer_signal_mid_side[channel], encoder->guts->real_signal_mid_side[channel], encoder->guts->subframe_workspace_ptr_mid_side[channel], encoder->guts->residual_workspace_mid_side[channel], encoder->guts->best_subframe_mid_side+channel, encoder->guts->best_subframe_bits_mid_side+channel)) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 746 | return false; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | /* |
| 751 | * Compose the frame bitbuffer |
| 752 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 753 | if(do_mid_side) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 754 | unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */ |
| 755 | 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] | 756 | FLAC__ChannelAssignment channel_assignment; |
| 757 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 758 | assert(encoder->channels == 2); |
| 759 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 760 | if(encoder->loose_mid_side_stereo && encoder->guts->loose_mid_side_stereo_frame_count > 0) { |
| 761 | channel_assignment = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE); |
| 762 | } |
| 763 | else { |
| 764 | unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */ |
| 765 | unsigned min_bits; |
| 766 | FLAC__ChannelAssignment ca; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 767 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 768 | assert(do_independent && do_mid_side); |
| 769 | |
| 770 | /* We have to figure out which channel assignent results in the smallest frame */ |
| 771 | bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits [1]; |
| 772 | bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits_mid_side[1]; |
| 773 | bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->guts->best_subframe_bits [1] + encoder->guts->best_subframe_bits_mid_side[1]; |
| 774 | bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->guts->best_subframe_bits_mid_side[0] + encoder->guts->best_subframe_bits_mid_side[1]; |
| 775 | |
| 776 | for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) { |
| 777 | if(bits[ca] < min_bits) { |
| 778 | min_bits = bits[ca]; |
| 779 | channel_assignment = ca; |
| 780 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 784 | frame_header.channel_assignment = channel_assignment; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 785 | |
| 786 | if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) { |
| 787 | encoder->state = FLAC__ENCODER_FRAMING_ERROR; |
| 788 | return false; |
| 789 | } |
| 790 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 791 | switch(channel_assignment) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 792 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 793 | left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]]; |
| 794 | right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 795 | break; |
| 796 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 797 | left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]]; |
| 798 | right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 799 | break; |
| 800 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 801 | left_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]]; |
| 802 | right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 803 | break; |
| 804 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 805 | left_subframe = &encoder->guts->subframe_workspace_mid_side[0][encoder->guts->best_subframe_mid_side[0]]; |
| 806 | right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 807 | break; |
| 808 | default: |
| 809 | assert(0); |
| 810 | } |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 811 | |
| 812 | switch(channel_assignment) { |
| 813 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
| 814 | left_bps = encoder->guts->subframe_bps [0]; |
| 815 | right_bps = encoder->guts->subframe_bps [1]; |
| 816 | break; |
| 817 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
| 818 | left_bps = encoder->guts->subframe_bps [0]; |
| 819 | right_bps = encoder->guts->subframe_bps_mid_side[1]; |
| 820 | break; |
| 821 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
| 822 | left_bps = encoder->guts->subframe_bps_mid_side[1]; |
| 823 | right_bps = encoder->guts->subframe_bps [1]; |
| 824 | break; |
| 825 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
| 826 | left_bps = encoder->guts->subframe_bps_mid_side[0]; |
| 827 | right_bps = encoder->guts->subframe_bps_mid_side[1]; |
| 828 | break; |
| 829 | default: |
| 830 | assert(0); |
| 831 | } |
| 832 | |
| 833 | /* note that encoder_add_subframe_ sets the state for us in case of an error */ |
| 834 | if(!encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->guts->frame)) |
| 835 | return false; |
| 836 | if(!encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->guts->frame)) |
| 837 | return false; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 838 | } |
| 839 | else { |
| 840 | if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) { |
| 841 | encoder->state = FLAC__ENCODER_FRAMING_ERROR; |
| 842 | return false; |
| 843 | } |
| 844 | |
| 845 | for(channel = 0; channel < encoder->channels; channel++) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 846 | if(!encoder_add_subframe_(encoder, &frame_header, encoder->guts->subframe_bps[channel], &encoder->guts->subframe_workspace[channel][encoder->guts->best_subframe[channel]], &encoder->guts->frame)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 847 | /* the above function sets the state for us in case of an error */ |
| 848 | return false; |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 853 | if(encoder->loose_mid_side_stereo) { |
| 854 | encoder->guts->loose_mid_side_stereo_frame_count++; |
| 855 | if(encoder->guts->loose_mid_side_stereo_frame_count >= encoder->guts->loose_mid_side_stereo_frames) |
| 856 | encoder->guts->loose_mid_side_stereo_frame_count = 0; |
| 857 | } |
| 858 | |
| 859 | encoder->guts->last_channel_assignment = frame_header.channel_assignment; |
| 860 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 861 | return true; |
| 862 | } |
| 863 | |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 864 | bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned min_partition_order, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 865 | { |
| 866 | real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]; |
| 867 | real lpc_residual_bits_per_sample; |
| 868 | real autoc[FLAC__MAX_LPC_ORDER+1]; |
| 869 | real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; |
| 870 | real lpc_error[FLAC__MAX_LPC_ORDER]; |
| 871 | unsigned min_lpc_order, max_lpc_order, lpc_order; |
| 872 | unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order; |
| 873 | unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision; |
| 874 | unsigned rice_parameter; |
| 875 | unsigned _candidate_bits, _best_bits; |
| 876 | unsigned _best_subframe; |
| 877 | |
| 878 | /* verbatim subframe is the baseline against which we measure other compressed subframes */ |
| 879 | _best_subframe = 0; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 880 | _best_bits = 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] | 881 | |
| 882 | if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) { |
| 883 | /* check for constant subframe */ |
Josh Coalson | eef5670 | 2001-03-30 00:45:22 +0000 | [diff] [blame] | 884 | if(encoder->guts->use_slow) |
| 885 | guess_fixed_order = FLAC__fixed_compute_best_predictor_slow(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample); |
| 886 | else |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 887 | guess_fixed_order = encoder->guts->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] | 888 | if(fixed_residual_bits_per_sample[1] == 0.0) { |
| 889 | /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */ |
| 890 | unsigned i, signal_is_constant = true; |
| 891 | for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) { |
| 892 | if(integer_signal[0] != integer_signal[i]) { |
| 893 | signal_is_constant = false; |
| 894 | break; |
| 895 | } |
| 896 | } |
| 897 | if(signal_is_constant) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 898 | _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 899 | if(_candidate_bits < _best_bits) { |
| 900 | _best_subframe = !_best_subframe; |
| 901 | _best_bits = _candidate_bits; |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | else { |
| 906 | /* encode fixed */ |
| 907 | if(encoder->do_exhaustive_model_search) { |
| 908 | min_fixed_order = 0; |
| 909 | max_fixed_order = FLAC__MAX_FIXED_ORDER; |
| 910 | } |
| 911 | else { |
| 912 | min_fixed_order = max_fixed_order = guess_fixed_order; |
| 913 | } |
| 914 | for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 915 | if(fixed_residual_bits_per_sample[fixed_order] >= (real)subframe_bps) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 916 | continue; /* don't even try */ |
Josh Coalson | 46f2ae8 | 2001-02-08 00:27:21 +0000 | [diff] [blame] | 917 | 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] | 918 | #ifndef FLAC__SYMMETRIC_RICE |
Josh Coalson | 46f2ae8 | 2001-02-08 00:27:21 +0000 | [diff] [blame] | 919 | rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */ |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 920 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 921 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) |
| 922 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 923 | _candidate_bits = encoder_evaluate_fixed_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, encoder->guts->abs_residual_partition_sums, encoder->guts->raw_bits_per_partition, frame_header->blocksize, subframe_bps, fixed_order, rice_parameter, min_partition_order, max_partition_order, encoder->rice_parameter_search_dist, subframe[!_best_subframe]); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 924 | if(_candidate_bits < _best_bits) { |
| 925 | _best_subframe = !_best_subframe; |
| 926 | _best_bits = _candidate_bits; |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | /* encode lpc */ |
| 931 | if(encoder->max_lpc_order > 0) { |
| 932 | if(encoder->max_lpc_order >= frame_header->blocksize) |
| 933 | max_lpc_order = frame_header->blocksize-1; |
| 934 | else |
| 935 | max_lpc_order = encoder->max_lpc_order; |
| 936 | if(max_lpc_order > 0) { |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 937 | encoder->guts->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] | 938 | /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */ |
| 939 | if(autoc[0] != 0.0) { |
| 940 | FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error); |
| 941 | if(encoder->do_exhaustive_model_search) { |
| 942 | min_lpc_order = 1; |
| 943 | } |
| 944 | else { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 945 | 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] | 946 | min_lpc_order = max_lpc_order = guess_lpc_order; |
| 947 | } |
| 948 | if(encoder->do_qlp_coeff_prec_search) { |
| 949 | min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 950 | max_qlp_coeff_precision = min(32 - subframe_bps - 1, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1); |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 951 | } |
| 952 | else { |
| 953 | min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision; |
| 954 | } |
| 955 | for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) { |
| 956 | 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 | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 957 | if(lpc_residual_bits_per_sample >= (real)subframe_bps) |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 958 | continue; /* don't even try */ |
| 959 | 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] | 960 | #ifndef FLAC__SYMMETRIC_RICE |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 961 | rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */ |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 962 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 963 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) |
| 964 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 965 | for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) { |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 966 | _candidate_bits = encoder_evaluate_lpc_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, encoder->guts->abs_residual_partition_sums, encoder->guts->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, encoder->rice_parameter_search_dist, subframe[!_best_subframe]); |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 967 | if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */ |
| 968 | if(_candidate_bits < _best_bits) { |
| 969 | _best_subframe = !_best_subframe; |
| 970 | _best_bits = _candidate_bits; |
| 971 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | *best_subframe = _best_subframe; |
| 982 | *best_bits = _best_bits; |
| 983 | |
| 984 | return true; |
| 985 | } |
| 986 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 987 | bool encoder_add_subframe_(FLAC__Encoder *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] | 988 | { |
| 989 | switch(subframe->type) { |
| 990 | case FLAC__SUBFRAME_TYPE_CONSTANT: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 991 | if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 992 | encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING; |
| 993 | return false; |
| 994 | } |
| 995 | break; |
| 996 | case FLAC__SUBFRAME_TYPE_FIXED: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 997 | if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 998 | encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING; |
| 999 | return false; |
| 1000 | } |
| 1001 | break; |
| 1002 | case FLAC__SUBFRAME_TYPE_LPC: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1003 | if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1004 | encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING; |
| 1005 | return false; |
| 1006 | } |
| 1007 | break; |
| 1008 | case FLAC__SUBFRAME_TYPE_VERBATIM: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1009 | if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1010 | encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING; |
| 1011 | return false; |
| 1012 | } |
| 1013 | break; |
| 1014 | default: |
| 1015 | assert(0); |
| 1016 | } |
| 1017 | |
| 1018 | return true; |
| 1019 | } |
| 1020 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1021 | unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1022 | { |
| 1023 | subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT; |
| 1024 | subframe->data.constant.value = signal; |
| 1025 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1026 | 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] | 1027 | } |
| 1028 | |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1029 | unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 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, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1030 | { |
| 1031 | unsigned i, residual_bits; |
| 1032 | const unsigned residual_samples = blocksize - order; |
| 1033 | |
| 1034 | FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual); |
| 1035 | |
| 1036 | subframe->type = FLAC__SUBFRAME_TYPE_FIXED; |
| 1037 | |
| 1038 | subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
| 1039 | subframe->data.fixed.residual = residual; |
| 1040 | |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1041 | residual_bits = 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, 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] | 1042 | |
| 1043 | subframe->data.fixed.order = order; |
| 1044 | for(i = 0; i < order; i++) |
| 1045 | subframe->data.fixed.warmup[i] = signal[i]; |
| 1046 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1047 | 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] | 1048 | } |
| 1049 | |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1050 | unsigned encoder_evaluate_lpc_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const 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, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1051 | { |
| 1052 | int32 qlp_coeff[FLAC__MAX_LPC_ORDER]; |
| 1053 | unsigned i, residual_bits; |
| 1054 | int quantization, ret; |
| 1055 | const unsigned residual_samples = blocksize - order; |
| 1056 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1057 | 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] | 1058 | if(ret != 0) |
| 1059 | return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */ |
| 1060 | |
| 1061 | FLAC__lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual); |
| 1062 | |
| 1063 | subframe->type = FLAC__SUBFRAME_TYPE_LPC; |
| 1064 | |
| 1065 | subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
| 1066 | subframe->data.lpc.residual = residual; |
| 1067 | |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1068 | residual_bits = 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, 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] | 1069 | |
| 1070 | subframe->data.lpc.order = order; |
| 1071 | subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision; |
| 1072 | subframe->data.lpc.quantization_level = quantization; |
| 1073 | memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(int32)*FLAC__MAX_LPC_ORDER); |
| 1074 | for(i = 0; i < order; i++) |
| 1075 | subframe->data.lpc.warmup[i] = signal[i]; |
| 1076 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1077 | 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] | 1078 | } |
| 1079 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1080 | unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1081 | { |
| 1082 | subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM; |
| 1083 | |
| 1084 | subframe->data.verbatim.data = signal; |
| 1085 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1086 | 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] | 1087 | } |
| 1088 | |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1089 | unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], uint32 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, 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] | 1090 | { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1091 | int32 r; |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1092 | #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES) |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1093 | unsigned sum; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1094 | int partition_order; |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1095 | #else |
| 1096 | unsigned partition_order; |
| 1097 | #endif |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1098 | unsigned residual_bits, best_residual_bits = 0; |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1099 | unsigned residual_sample; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1100 | unsigned best_parameters_index = 0, parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER], raw_bits[2][1 << FLAC__MAX_RICE_PARTITION_ORDER]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1101 | |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1102 | /* compute abs(residual) for use later */ |
| 1103 | for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) { |
| 1104 | r = residual[residual_sample]; |
| 1105 | abs_residual[residual_sample] = (uint32)(r<0? -r : r); |
| 1106 | } |
| 1107 | |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1108 | #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES) |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1109 | max_partition_order = encoder_precompute_partition_info_(residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order); |
| 1110 | min_partition_order = min(min_partition_order, max_partition_order); |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1111 | |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1112 | for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) { |
| 1113 | if(!encoder_set_partitioned_rice_(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, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits)) { |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1114 | assert(0); /* encoder_precompute_partition_info_ should keep this from ever happening */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1115 | } |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1116 | sum += 1u << partition_order; |
| 1117 | if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 1118 | best_residual_bits = residual_bits; |
| 1119 | *best_partition_order = partition_order; |
| 1120 | best_parameters_index = !best_parameters_index; |
| 1121 | } |
| 1122 | } |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1123 | #else |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1124 | for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) { |
| 1125 | if(!encoder_set_partitioned_rice_(abs_residual, 0, 0, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits)) { |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1126 | assert(best_residual_bits != 0); |
| 1127 | break; |
| 1128 | } |
| 1129 | if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 1130 | best_residual_bits = residual_bits; |
| 1131 | *best_partition_order = partition_order; |
| 1132 | best_parameters_index = !best_parameters_index; |
| 1133 | } |
| 1134 | } |
| 1135 | #endif |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1136 | memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order))); |
| 1137 | memcpy(best_raw_bits, raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order))); |
| 1138 | |
| 1139 | return best_residual_bits; |
| 1140 | } |
| 1141 | |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1142 | #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES) |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1143 | unsigned encoder_precompute_partition_info_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], 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] | 1144 | { |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1145 | int partition_order; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1146 | unsigned from_partition, to_partition = 0; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1147 | const unsigned blocksize = residual_samples + predictor_order; |
| 1148 | |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1149 | /* first do max_partition_order */ |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1150 | for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) { |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1151 | #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1152 | uint32 abs_residual_partition_sum; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1153 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1154 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1155 | uint32 abs_residual_partition_max; |
| 1156 | unsigned abs_residual_partition_max_index = 0; /* initialized to silence superfluous compiler warning */ |
| 1157 | #endif |
| 1158 | uint32 abs_r; |
| 1159 | unsigned partition, partition_sample, partition_samples, residual_sample; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1160 | const unsigned partitions = 1u << partition_order; |
| 1161 | const unsigned default_partition_samples = blocksize >> partition_order; |
| 1162 | |
| 1163 | if(default_partition_samples <= predictor_order) { |
| 1164 | assert(max_partition_order > 0); |
| 1165 | max_partition_order--; |
| 1166 | } |
| 1167 | else { |
| 1168 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 1169 | partition_samples = default_partition_samples; |
| 1170 | if(partition == 0) |
| 1171 | partition_samples -= predictor_order; |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1172 | #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1173 | abs_residual_partition_sum = 0; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1174 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1175 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1176 | abs_residual_partition_max = 0; |
| 1177 | #endif |
| 1178 | for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) { |
| 1179 | abs_r = abs_residual[residual_sample]; |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1180 | #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1181 | abs_residual_partition_sum += abs_r; /* @@@ this can overflow with small max_partition_order and (large blocksizes or bits-per-sample), FIX! */ |
| 1182 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1183 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1184 | if(abs_r > abs_residual_partition_max) { |
| 1185 | abs_residual_partition_max = abs_r; |
| 1186 | abs_residual_partition_max_index = residual_sample; |
| 1187 | } |
| 1188 | #endif |
| 1189 | residual_sample++; |
| 1190 | } |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1191 | #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1192 | abs_residual_partition_sums[partition] = abs_residual_partition_sum; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1193 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1194 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1195 | if(abs_residual_partition_max > 0) |
| 1196 | raw_bits_per_partition[partition] = FLAC__bitmath_silog2(residual[abs_residual_partition_max_index]); |
| 1197 | else |
| 1198 | raw_bits_per_partition[partition] = FLAC__bitmath_silog2(0); |
| 1199 | #endif |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1200 | } |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1201 | to_partition = partitions; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1202 | break; |
| 1203 | } |
| 1204 | } |
Josh Coalson | f76a361 | 2001-04-18 02:28:11 +0000 | [diff] [blame] | 1205 | |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1206 | /* now merge for lower orders */ |
Josh Coalson | 6bd1757 | 2001-05-25 19:02:01 +0000 | [diff] [blame^] | 1207 | for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) { |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1208 | #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1209 | uint32 s; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1210 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1211 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1212 | unsigned m; |
| 1213 | #endif |
| 1214 | unsigned i; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1215 | const unsigned partitions = 1u << partition_order; |
| 1216 | for(i = 0; i < partitions; i++) { |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1217 | #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1218 | s = abs_residual_partition_sums[from_partition]; |
| 1219 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1220 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1221 | m = raw_bits_per_partition[from_partition]; |
| 1222 | #endif |
| 1223 | from_partition++; |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1224 | #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1225 | abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition]; |
| 1226 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1227 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1228 | raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]); |
| 1229 | #endif |
| 1230 | from_partition++; |
| 1231 | to_partition++; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | |
Josh Coalson | f76a361 | 2001-04-18 02:28:11 +0000 | [diff] [blame] | 1235 | return max_partition_order; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1236 | } |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1237 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1238 | |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1239 | #ifdef VARIABLE_RICE_BITS |
| 1240 | #undef VARIABLE_RICE_BITS |
| 1241 | #endif |
| 1242 | #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter)) |
| 1243 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1244 | bool encoder_set_partitioned_rice_(const uint32 abs_residual[], const uint32 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, unsigned parameters[], unsigned raw_bits[], unsigned *bits) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1245 | { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1246 | unsigned rice_parameter, partition_bits; |
| 1247 | #ifndef NO_RICE_SEARCH |
| 1248 | unsigned best_partition_bits; |
| 1249 | unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0; |
| 1250 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1251 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1252 | unsigned flat_bits; |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1253 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1254 | unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; |
| 1255 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1256 | assert(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER); |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1257 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1258 | if(partition_order == 0) { |
| 1259 | unsigned i; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1260 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1261 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1262 | if(rice_parameter_search_dist) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1263 | if(suggested_rice_parameter < rice_parameter_search_dist) |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1264 | min_rice_parameter = 0; |
| 1265 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1266 | min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist; |
| 1267 | max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist; |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1268 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) |
| 1269 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 1270 | } |
| 1271 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1272 | min_rice_parameter = max_rice_parameter = suggested_rice_parameter; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1273 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1274 | best_partition_bits = 0xffffffff; |
| 1275 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 1276 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1277 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1278 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1279 | partition_bits = (2+rice_parameter) * residual_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1280 | #else |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1281 | const unsigned rice_parameter_estimate = rice_parameter-1; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1282 | partition_bits = (1+rice_parameter) * residual_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1283 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1284 | #else |
| 1285 | partition_bits = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1286 | #endif |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1287 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1288 | for(i = 0; i < residual_samples; i++) { |
| 1289 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1290 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1291 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1292 | #else |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1293 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate); |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1294 | #endif |
| 1295 | #else |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1296 | 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] | 1297 | #endif |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1298 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1299 | #ifndef NO_RICE_SEARCH |
| 1300 | if(partition_bits < best_partition_bits) { |
| 1301 | best_rice_parameter = rice_parameter; |
| 1302 | best_partition_bits = partition_bits; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1303 | } |
| 1304 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1305 | #endif |
| 1306 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
| 1307 | flat_bits = raw_bits_per_partition[0] * residual_samples; |
| 1308 | if(flat_bits <= best_partition_bits) { |
| 1309 | raw_bits[0] = raw_bits_per_partition[0]; |
| 1310 | best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 1311 | best_partition_bits = flat_bits; |
| 1312 | } |
| 1313 | #endif |
| 1314 | parameters[0] = best_rice_parameter; |
| 1315 | bits_ += best_partition_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1316 | } |
| 1317 | else { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1318 | unsigned partition, j, save_j, k; |
| 1319 | unsigned mean, partition_samples; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1320 | const unsigned partitions = 1u << partition_order; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1321 | for(partition = j = 0; partition < partitions; partition++) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1322 | partition_samples = (residual_samples+predictor_order) >> partition_order; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1323 | if(partition == 0) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1324 | if(partition_samples <= predictor_order) |
| 1325 | return false; |
| 1326 | else |
| 1327 | partition_samples -= predictor_order; |
| 1328 | } |
| 1329 | mean = partition_samples >> 1; |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1330 | #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1331 | mean += abs_residual_partition_sums[partition]; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1332 | #else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1333 | save_j = j; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1334 | for(k = 0; k < partition_samples; j++, k++) |
| 1335 | mean += abs_residual[j]; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1336 | j = save_j; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1337 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1338 | mean /= partition_samples; |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1339 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1340 | /* calc rice_parameter = floor(log2(mean)) */ |
| 1341 | rice_parameter = 0; |
| 1342 | mean>>=1; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1343 | while(mean) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1344 | rice_parameter++; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1345 | mean >>= 1; |
| 1346 | } |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1347 | #else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1348 | /* calc rice_parameter = floor(log2(mean)) + 1 */ |
| 1349 | rice_parameter = 0; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1350 | while(mean) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1351 | rice_parameter++; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1352 | mean >>= 1; |
| 1353 | } |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1354 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1355 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) |
| 1356 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1357 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1358 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1359 | if(rice_parameter_search_dist) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1360 | if(rice_parameter < rice_parameter_search_dist) |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1361 | min_rice_parameter = 0; |
| 1362 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1363 | min_rice_parameter = rice_parameter - rice_parameter_search_dist; |
| 1364 | max_rice_parameter = rice_parameter + rice_parameter_search_dist; |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1365 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) |
| 1366 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 1367 | } |
| 1368 | else |
| 1369 | min_rice_parameter = max_rice_parameter = rice_parameter; |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1370 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1371 | best_partition_bits = 0xffffffff; |
| 1372 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 1373 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1374 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1375 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1376 | partition_bits = (2+rice_parameter) * partition_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1377 | #else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1378 | const unsigned rice_parameter_estimate = rice_parameter-1; |
| 1379 | partition_bits = (1+rice_parameter) * partition_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1380 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1381 | #else |
| 1382 | partition_bits = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1383 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1384 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
| 1385 | save_j = j; |
| 1386 | for(k = 0; k < partition_samples; j++, k++) { |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1387 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1388 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1389 | partition_bits += VARIABLE_RICE_BITS(abs_residual[j], rice_parameter); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1390 | #else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1391 | partition_bits += VARIABLE_RICE_BITS(abs_residual[j], rice_parameter_estimate); |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1392 | #endif |
| 1393 | #else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1394 | partition_bits += FLAC__bitbuffer_rice_bits(residual[j], 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] | 1395 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1396 | } |
| 1397 | if(rice_parameter != max_rice_parameter) |
| 1398 | j = save_j; |
| 1399 | #ifndef NO_RICE_SEARCH |
| 1400 | if(partition_bits < best_partition_bits) { |
| 1401 | best_rice_parameter = rice_parameter; |
| 1402 | best_partition_bits = partition_bits; |
| 1403 | } |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1404 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1405 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1406 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1407 | flat_bits = raw_bits_per_partition[partition] * partition_samples; |
| 1408 | if(flat_bits <= best_partition_bits) { |
| 1409 | raw_bits[partition] = raw_bits_per_partition[partition]; |
| 1410 | best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 1411 | best_partition_bits = flat_bits; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1412 | } |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1413 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1414 | parameters[partition] = best_rice_parameter; |
| 1415 | bits_ += best_partition_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | *bits = bits_; |
| 1420 | return true; |
| 1421 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1422 | |
Josh Coalson | 8e92f21 | 2001-05-01 18:50:45 +0000 | [diff] [blame] | 1423 | unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples) |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1424 | { |
| 1425 | unsigned i, shift; |
| 1426 | int32 x = 0; |
| 1427 | |
| 1428 | for(i = 0; i < samples && !(x&1); i++) |
| 1429 | x |= signal[i]; |
| 1430 | |
| 1431 | if(x == 0) { |
| 1432 | shift = 0; |
| 1433 | } |
| 1434 | else { |
| 1435 | for(shift = 0; !(x&1); shift++) |
| 1436 | x >>= 1; |
| 1437 | } |
| 1438 | |
| 1439 | if(shift > 0) { |
| 1440 | for(i = 0; i < samples; i++) |
| 1441 | signal[i] >>= shift; |
| 1442 | } |
| 1443 | |
| 1444 | return shift; |
| 1445 | } |