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