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