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