| 1 |
/* libFLAC - Free Lossless Audio Codec library |
| 2 |
* Copyright (C) 2000-2009 Josh Coalson |
| 3 |
* Copyright (C) 2011-2025 Xiph.Org Foundation |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* |
| 9 |
* - Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* - Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* |
| 16 |
* - Neither the name of the Xiph.org Foundation nor the names of its |
| 17 |
* contributors may be used to endorse or promote products derived from |
| 18 |
* this software without specific prior written permission. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 |
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 24 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
*/ |
| 32 |
|
| 33 |
//ppgb #ifdef HAVE_CONFIG_H |
| 34 |
#include "flac_config.h" |
| 35 |
//ppgb #endif |
| 36 |
|
| 37 |
#include <limits.h> |
| 38 |
#include <stdio.h> |
| 39 |
#include <stdlib.h> /* for malloc() */ |
| 40 |
#include <string.h> /* for memcpy() */ |
| 41 |
#include <sys/types.h> /* for off_t */ |
| 42 |
#ifdef _WIN32 |
| 43 |
#include <windows.h> /* for GetFileType() */ |
| 44 |
#include <io.h> /* for _get_osfhandle() */ |
| 45 |
#endif |
| 46 |
#ifdef HAVE_PTHREAD |
| 47 |
#include <pthread.h> |
| 48 |
#endif |
| 49 |
#include "flac_share_compat.h" |
| 50 |
#include "flac_FLAC_assert.h" |
| 51 |
#include "flac_FLAC_stream_decoder.h" |
| 52 |
#include "flac_protected_stream_encoder.h" |
| 53 |
#include "flac_private_bitwriter.h" |
| 54 |
#include "flac_private_bitmath.h" |
| 55 |
#include "flac_private_crc.h" |
| 56 |
#include "flac_private_cpu.h" |
| 57 |
#include "flac_private_fixed.h" |
| 58 |
#include "flac_private_format.h" |
| 59 |
#include "flac_private_lpc.h" |
| 60 |
#include "flac_private_md5.h" |
| 61 |
#include "flac_private_memory.h" |
| 62 |
#include "flac_private_macros.h" |
| 63 |
#if FLAC__HAS_OGG |
| 64 |
#include "flac_private_ogg_helper.h" |
| 65 |
#include "flac_private_ogg_mapping.h" |
| 66 |
#endif |
| 67 |
#include "flac_private_stream_encoder.h" |
| 68 |
#include "flac_private_stream_encoder_framing.h" |
| 69 |
#include "flac_private_window.h" |
| 70 |
#include "flac_share_alloc.h" |
| 71 |
#include "flac_share_private.h" |
| 72 |
|
| 73 |
|
| 74 |
/* Exact Rice codeword length calculation is off by default. The simple |
| 75 |
* (and fast) estimation (of how many bits a residual value will be |
| 76 |
* encoded with) in this encoder is very good, almost always yielding |
| 77 |
* compression within 0.1% of exact calculation. |
| 78 |
*/ |
| 79 |
#undef EXACT_RICE_BITS_CALCULATION |
| 80 |
/* Rice parameter searching is off by default. The simple (and fast) |
| 81 |
* parameter estimation in this encoder is very good, almost always |
| 82 |
* yielding compression within 0.1% of the optimal parameters. |
| 83 |
*/ |
| 84 |
#undef ENABLE_RICE_PARAMETER_SEARCH |
| 85 |
|
| 86 |
#ifdef local_abs64 |
| 87 |
#undef local_abs64 |
| 88 |
#endif |
| 89 |
#define local_abs64(x) ((uint64_t)((x)<0? -(x) : (x))) |
| 90 |
|
| 91 |
|
| 92 |
typedef struct { |
| 93 |
FLAC__int32 *data[FLAC__MAX_CHANNELS]; |
| 94 |
uint32_t size; /* of each data[] in samples */ |
| 95 |
uint32_t tail; |
| 96 |
} verify_input_fifo; |
| 97 |
|
| 98 |
typedef struct { |
| 99 |
const FLAC__byte *data; |
| 100 |
uint32_t capacity; |
| 101 |
uint32_t bytes; |
| 102 |
} verify_output; |
| 103 |
|
| 104 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 105 |
typedef struct { |
| 106 |
uint32_t a, b, c; |
| 107 |
FLAC__ApodizationSpecification * current_apodization; |
| 108 |
double autoc_root[FLAC__MAX_LPC_ORDER+1]; |
| 109 |
double autoc[FLAC__MAX_LPC_ORDER+1]; |
| 110 |
} apply_apodization_state_struct; |
| 111 |
#endif |
| 112 |
|
| 113 |
typedef enum { |
| 114 |
ENCODER_IN_MAGIC = 0, |
| 115 |
ENCODER_IN_METADATA = 1, |
| 116 |
ENCODER_IN_AUDIO = 2 |
| 117 |
} EncoderStateHint; |
| 118 |
|
| 119 |
static const struct CompressionLevels { |
| 120 |
FLAC__bool do_mid_side_stereo; |
| 121 |
FLAC__bool loose_mid_side_stereo; |
| 122 |
uint32_t max_lpc_order; |
| 123 |
uint32_t qlp_coeff_precision; |
| 124 |
FLAC__bool do_qlp_coeff_prec_search; |
| 125 |
FLAC__bool do_escape_coding; |
| 126 |
FLAC__bool do_exhaustive_model_search; |
| 127 |
uint32_t min_residual_partition_order; |
| 128 |
uint32_t max_residual_partition_order; |
| 129 |
uint32_t rice_parameter_search_dist; |
| 130 |
const char *apodization; |
| 131 |
} compression_levels_[] = { |
| 132 |
{ false, false, 0, 0, false, false, false, 0, 3, 0, "tukey(5e-1)" }, |
| 133 |
{ true , true , 0, 0, false, false, false, 0, 3, 0, "tukey(5e-1)" }, |
| 134 |
{ true , false, 0, 0, false, false, false, 0, 3, 0, "tukey(5e-1)" }, |
| 135 |
{ false, false, 6, 0, false, false, false, 0, 4, 0, "tukey(5e-1)" }, |
| 136 |
{ true , true , 8, 0, false, false, false, 0, 4, 0, "tukey(5e-1)" }, |
| 137 |
{ true , false, 8, 0, false, false, false, 0, 5, 0, "tukey(5e-1)" }, |
| 138 |
{ true , false, 8, 0, false, false, false, 0, 6, 0, "subdivide_tukey(2)" }, |
| 139 |
{ true , false, 12, 0, false, false, false, 0, 6, 0, "subdivide_tukey(2)" }, |
| 140 |
{ true , false, 12, 0, false, false, false, 0, 6, 0, "subdivide_tukey(3)" } |
| 141 |
/* here we use locale-independent 5e-1 instead of 0.5 or 0,5 */ |
| 142 |
}; |
| 143 |
|
| 144 |
/*********************************************************************** |
| 145 |
* |
| 146 |
* Thread-private data |
| 147 |
* |
| 148 |
***********************************************************************/ |
| 149 |
|
| 150 |
|
| 151 |
typedef struct FLAC__StreamEncoderThreadTask { |
| 152 |
FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */ |
| 153 |
FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */ |
| 154 |
FLAC__int64 *integer_signal_33bit_side; /* 33-bit side for 32-bit stereo decorrelation */ |
| 155 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 156 |
FLAC__real *windowed_signal; /* the integer_signal[] * current window[] */ |
| 157 |
#endif |
| 158 |
uint32_t subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */ |
| 159 |
uint32_t subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */ |
| 160 |
FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */ |
| 161 |
FLAC__int32 *residual_workspace_mid_side[2][2]; |
| 162 |
FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2]; |
| 163 |
FLAC__Subframe subframe_workspace_mid_side[2][2]; |
| 164 |
FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2]; |
| 165 |
FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2]; |
| 166 |
FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2]; |
| 167 |
FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2]; |
| 168 |
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2]; |
| 169 |
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2]; |
| 170 |
uint32_t best_subframe[FLAC__MAX_CHANNELS]; /* index (0 or 1) into 2nd dimension of the above workspaces */ |
| 171 |
uint32_t best_subframe_mid_side[2]; |
| 172 |
uint32_t best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */ |
| 173 |
uint32_t best_subframe_bits_mid_side[2]; |
| 174 |
FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */ |
| 175 |
uint32_t *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */ |
| 176 |
FLAC__BitWriter *frame; /* the current frame being worked on */ |
| 177 |
uint32_t current_frame_number; |
| 178 |
/* unaligned (original) pointers to allocated data */ |
| 179 |
FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS]; |
| 180 |
FLAC__int32 *integer_signal_mid_side_unaligned[2]; |
| 181 |
FLAC__int64 *integer_signal_33bit_side_unaligned; |
| 182 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 183 |
FLAC__real *windowed_signal_unaligned; |
| 184 |
#endif |
| 185 |
FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2]; |
| 186 |
FLAC__int32 *residual_workspace_mid_side_unaligned[2][2]; |
| 187 |
FLAC__uint64 *abs_residual_partition_sums_unaligned; |
| 188 |
uint32_t *raw_bits_per_partition_unaligned; |
| 189 |
/* |
| 190 |
* These fields have been moved here from private function local |
| 191 |
* declarations merely to save stack space during encoding. |
| 192 |
*/ |
| 193 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 194 |
FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */ |
| 195 |
#endif |
| 196 |
FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */ |
| 197 |
FLAC__bool disable_constant_subframes; |
| 198 |
#ifdef HAVE_PTHREAD |
| 199 |
pthread_mutex_t mutex_this_task; /* To lock whole threadtask */ |
| 200 |
pthread_cond_t cond_task_done; |
| 201 |
FLAC__bool task_done; |
| 202 |
FLAC__bool returnvalue; |
| 203 |
#endif |
| 204 |
} FLAC__StreamEncoderThreadTask; |
| 205 |
|
| 206 |
/*********************************************************************** |
| 207 |
* |
| 208 |
* Private class method prototypes |
| 209 |
* |
| 210 |
***********************************************************************/ |
| 211 |
|
| 212 |
static void set_defaults_(FLAC__StreamEncoder *encoder); |
| 213 |
static void free_(FLAC__StreamEncoder *encoder); |
| 214 |
static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, uint32_t new_blocksize); |
| 215 |
static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderThreadTask *threadtask, uint32_t samples, FLAC__bool is_last_block); |
| 216 |
static FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, uint32_t samples, FLAC__bool is_last_block); |
| 217 |
static void update_metadata_(const FLAC__StreamEncoder *encoder); |
| 218 |
#if FLAC__HAS_OGG |
| 219 |
static void update_ogg_metadata_(FLAC__StreamEncoder *encoder); |
| 220 |
#endif |
| 221 |
static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_block); |
| 222 |
void * process_frame_thread_(void * encoder); |
| 223 |
FLAC__bool process_frame_thread_inner_(FLAC__StreamEncoder * encoder, FLAC__StreamEncoderThreadTask *threadtask); |
| 224 |
static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderThreadTask *threadtask); |
| 225 |
|
| 226 |
static FLAC__bool process_subframe_( |
| 227 |
FLAC__StreamEncoder *encoder, |
| 228 |
FLAC__StreamEncoderThreadTask *threadtask, |
| 229 |
uint32_t min_partition_order, |
| 230 |
uint32_t max_partition_order, |
| 231 |
const FLAC__FrameHeader *frame_header, |
| 232 |
uint32_t subframe_bps, |
| 233 |
const void *integer_signal, |
| 234 |
FLAC__Subframe *subframe[2], |
| 235 |
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2], |
| 236 |
FLAC__int32 *residual[2], |
| 237 |
uint32_t *best_subframe, |
| 238 |
uint32_t *best_bits |
| 239 |
); |
| 240 |
|
| 241 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 242 |
static FLAC__bool apply_apodization_( |
| 243 |
FLAC__StreamEncoder *encoder, |
| 244 |
FLAC__StreamEncoderThreadTask *threadtask, |
| 245 |
apply_apodization_state_struct *apply_apodization_state, |
| 246 |
uint32_t blocksize, |
| 247 |
double *lpc_error, |
| 248 |
uint32_t *max_lpc_order_this_apodization, |
| 249 |
uint32_t subframe_bps, |
| 250 |
const void *integer_signal, |
| 251 |
uint32_t *guess_lpc_order |
| 252 |
); |
| 253 |
#endif |
| 254 |
|
| 255 |
static FLAC__bool add_subframe_( |
| 256 |
FLAC__StreamEncoder *encoder, |
| 257 |
uint32_t blocksize, |
| 258 |
uint32_t subframe_bps, |
| 259 |
const FLAC__Subframe *subframe, |
| 260 |
FLAC__BitWriter *frame |
| 261 |
); |
| 262 |
|
| 263 |
static uint32_t evaluate_constant_subframe_( |
| 264 |
FLAC__StreamEncoder *encoder, |
| 265 |
const FLAC__int64 signal, |
| 266 |
uint32_t blocksize, |
| 267 |
uint32_t subframe_bps, |
| 268 |
FLAC__Subframe *subframe |
| 269 |
); |
| 270 |
|
| 271 |
static uint32_t evaluate_fixed_subframe_( |
| 272 |
FLAC__StreamEncoder *encoder, |
| 273 |
FLAC__StreamEncoderThreadTask *threadtask, |
| 274 |
const void *signal, |
| 275 |
FLAC__int32 residual[], |
| 276 |
FLAC__uint64 abs_residual_partition_sums[], |
| 277 |
uint32_t raw_bits_per_partition[], |
| 278 |
uint32_t blocksize, |
| 279 |
uint32_t subframe_bps, |
| 280 |
uint32_t order, |
| 281 |
uint32_t rice_parameter_limit, |
| 282 |
uint32_t min_partition_order, |
| 283 |
uint32_t max_partition_order, |
| 284 |
FLAC__bool do_escape_coding, |
| 285 |
uint32_t rice_parameter_search_dist, |
| 286 |
FLAC__Subframe *subframe, |
| 287 |
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
| 288 |
); |
| 289 |
|
| 290 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 291 |
static uint32_t evaluate_lpc_subframe_( |
| 292 |
FLAC__StreamEncoder *encoder, |
| 293 |
FLAC__StreamEncoderThreadTask *threadtask, |
| 294 |
const void *signal, |
| 295 |
FLAC__int32 residual[], |
| 296 |
FLAC__uint64 abs_residual_partition_sums[], |
| 297 |
uint32_t raw_bits_per_partition[], |
| 298 |
const FLAC__real lp_coeff[], |
| 299 |
uint32_t blocksize, |
| 300 |
uint32_t subframe_bps, |
| 301 |
uint32_t order, |
| 302 |
uint32_t qlp_coeff_precision, |
| 303 |
uint32_t rice_parameter_limit, |
| 304 |
uint32_t min_partition_order, |
| 305 |
uint32_t max_partition_order, |
| 306 |
FLAC__bool do_escape_coding, |
| 307 |
uint32_t rice_parameter_search_dist, |
| 308 |
FLAC__Subframe *subframe, |
| 309 |
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
| 310 |
); |
| 311 |
#endif |
| 312 |
|
| 313 |
static uint32_t evaluate_verbatim_subframe_( |
| 314 |
FLAC__StreamEncoder *encoder, |
| 315 |
const void *signal, |
| 316 |
uint32_t blocksize, |
| 317 |
uint32_t subframe_bps, |
| 318 |
FLAC__Subframe *subframe |
| 319 |
); |
| 320 |
|
| 321 |
static uint32_t find_best_partition_order_( |
| 322 |
struct FLAC__StreamEncoderPrivate *private_, |
| 323 |
FLAC__StreamEncoderThreadTask *threadtask, |
| 324 |
const FLAC__int32 residual[], |
| 325 |
FLAC__uint64 abs_residual_partition_sums[], |
| 326 |
uint32_t raw_bits_per_partition[], |
| 327 |
uint32_t residual_samples, |
| 328 |
uint32_t predictor_order, |
| 329 |
uint32_t rice_parameter_limit, |
| 330 |
uint32_t min_partition_order, |
| 331 |
uint32_t max_partition_order, |
| 332 |
uint32_t bps, |
| 333 |
FLAC__bool do_escape_coding, |
| 334 |
uint32_t rice_parameter_search_dist, |
| 335 |
FLAC__EntropyCodingMethod *best_ecm |
| 336 |
); |
| 337 |
|
| 338 |
static void precompute_partition_info_sums_( |
| 339 |
const FLAC__int32 residual[], |
| 340 |
FLAC__uint64 abs_residual_partition_sums[], |
| 341 |
uint32_t residual_samples, |
| 342 |
uint32_t predictor_order, |
| 343 |
uint32_t min_partition_order, |
| 344 |
uint32_t max_partition_order, |
| 345 |
uint32_t bps |
| 346 |
); |
| 347 |
|
| 348 |
static void precompute_partition_info_escapes_( |
| 349 |
const FLAC__int32 residual[], |
| 350 |
uint32_t raw_bits_per_partition[], |
| 351 |
uint32_t residual_samples, |
| 352 |
uint32_t predictor_order, |
| 353 |
uint32_t min_partition_order, |
| 354 |
uint32_t max_partition_order |
| 355 |
); |
| 356 |
|
| 357 |
static FLAC__bool set_partitioned_rice_( |
| 358 |
#ifdef EXACT_RICE_BITS_CALCULATION |
| 359 |
const FLAC__int32 residual[], |
| 360 |
#endif |
| 361 |
const FLAC__uint64 abs_residual_partition_sums[], |
| 362 |
const uint32_t raw_bits_per_partition[], |
| 363 |
const uint32_t residual_samples, |
| 364 |
const uint32_t predictor_order, |
| 365 |
const uint32_t rice_parameter_limit, |
| 366 |
const uint32_t rice_parameter_search_dist, |
| 367 |
const uint32_t partition_order, |
| 368 |
const FLAC__bool search_for_escapes, |
| 369 |
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 370 |
uint32_t *bits |
| 371 |
); |
| 372 |
|
| 373 |
static uint32_t get_wasted_bits_(FLAC__int32 signal[], uint32_t samples); |
| 374 |
static uint32_t get_wasted_bits_wide_(FLAC__int64 signal_wide[], FLAC__int32 signal[], uint32_t samples); |
| 375 |
|
| 376 |
/* verify-related routines: */ |
| 377 |
static void append_to_verify_fifo_( |
| 378 |
verify_input_fifo *fifo, |
| 379 |
const FLAC__int32 * const input[], |
| 380 |
uint32_t input_offset, |
| 381 |
uint32_t channels, |
| 382 |
uint32_t wide_samples |
| 383 |
); |
| 384 |
|
| 385 |
static void append_to_verify_fifo_interleaved_( |
| 386 |
verify_input_fifo *fifo, |
| 387 |
const FLAC__int32 input[], |
| 388 |
uint32_t input_offset, |
| 389 |
uint32_t channels, |
| 390 |
uint32_t wide_samples |
| 391 |
); |
| 392 |
|
| 393 |
static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data); |
| 394 |
static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data); |
| 395 |
static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data); |
| 396 |
static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data); |
| 397 |
|
| 398 |
static FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data); |
| 399 |
static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data); |
| 400 |
static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data); |
| 401 |
static FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, uint32_t samples, uint32_t current_frame, void *client_data); |
| 402 |
static FILE *get_binary_stdout_(void); |
| 403 |
|
| 404 |
|
| 405 |
/*********************************************************************** |
| 406 |
* |
| 407 |
* Private class data |
| 408 |
* |
| 409 |
***********************************************************************/ |
| 410 |
|
| 411 |
typedef struct FLAC__StreamEncoderPrivate { |
| 412 |
FLAC__StreamEncoderThreadTask * threadtask[FLAC__STREAM_ENCODER_MAX_THREADTASKS]; |
| 413 |
#ifdef HAVE_PTHREAD |
| 414 |
pthread_t thread[FLAC__STREAM_ENCODER_MAX_THREADS]; |
| 415 |
#endif |
| 416 |
uint32_t input_capacity; /* current size (in samples) of the signal and residual buffers */ |
| 417 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 418 |
FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */ |
| 419 |
FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS]; |
| 420 |
#endif |
| 421 |
FLAC__StreamMetadata streaminfo; /* scratchpad for STREAMINFO as it is built */ |
| 422 |
FLAC__StreamMetadata_SeekTable *seek_table; /* pointer into encoder->protected_->metadata_ where the seek table is */ |
| 423 |
uint32_t current_sample_number; |
| 424 |
uint32_t current_frame_number; |
| 425 |
FLAC__MD5Context md5context; |
| 426 |
FLAC__CPUInfo cpuinfo; |
| 427 |
void (*local_precompute_partition_info_sums)(const FLAC__int32 residual[], FLAC__uint64 abs_residual_partition_sums[], uint32_t residual_samples, uint32_t predictor_order, uint32_t min_partition_order, uint32_t max_partition_order, uint32_t bps); |
| 428 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 429 |
uint32_t (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], uint32_t data_len, float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); |
| 430 |
uint32_t (*local_fixed_compute_best_predictor_wide)(const FLAC__int32 data[], uint32_t data_len, float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); |
| 431 |
uint32_t (*local_fixed_compute_best_predictor_limit_residual)(const FLAC__int32 data[], uint32_t data_len, float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); |
| 432 |
#else |
| 433 |
uint32_t (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], uint32_t data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); |
| 434 |
uint32_t (*local_fixed_compute_best_predictor_wide)(const FLAC__int32 data[], uint32_t data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); |
| 435 |
uint32_t (*local_fixed_compute_best_predictor_limit_residual)(const FLAC__int32 data[], uint32_t data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); |
| 436 |
#endif |
| 437 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 438 |
void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]); |
| 439 |
void (*local_lpc_compute_residual_from_qlp_coefficients)(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]); |
| 440 |
void (*local_lpc_compute_residual_from_qlp_coefficients_64bit)(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]); |
| 441 |
void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]); |
| 442 |
#endif |
| 443 |
FLAC__bool disable_mmx; |
| 444 |
FLAC__bool disable_sse2; |
| 445 |
FLAC__bool disable_ssse3; |
| 446 |
FLAC__bool disable_sse41; |
| 447 |
FLAC__bool disable_sse42; |
| 448 |
FLAC__bool disable_avx2; |
| 449 |
FLAC__bool disable_fma; |
| 450 |
FLAC__bool disable_constant_subframes; |
| 451 |
FLAC__bool disable_fixed_subframes; |
| 452 |
FLAC__bool disable_verbatim_subframes; |
| 453 |
FLAC__bool is_ogg; |
| 454 |
FLAC__StreamEncoderReadCallback read_callback; /* currently only needed for Ogg FLAC */ |
| 455 |
FLAC__StreamEncoderSeekCallback seek_callback; |
| 456 |
FLAC__StreamEncoderTellCallback tell_callback; |
| 457 |
FLAC__StreamEncoderWriteCallback write_callback; |
| 458 |
FLAC__StreamEncoderMetadataCallback metadata_callback; |
| 459 |
FLAC__StreamEncoderProgressCallback progress_callback; |
| 460 |
void *client_data; |
| 461 |
uint32_t first_seekpoint_to_check; |
| 462 |
FILE *file; /* only used when encoding to a file */ |
| 463 |
FLAC__uint64 bytes_written; |
| 464 |
FLAC__uint64 samples_written; |
| 465 |
uint32_t frames_written; |
| 466 |
uint32_t total_frames_estimate; |
| 467 |
/* |
| 468 |
* The data for the verify section |
| 469 |
*/ |
| 470 |
struct { |
| 471 |
FLAC__StreamDecoder *decoder; |
| 472 |
EncoderStateHint state_hint; |
| 473 |
FLAC__bool needs_magic_hack; |
| 474 |
verify_input_fifo input_fifo; |
| 475 |
verify_output output; |
| 476 |
struct { |
| 477 |
FLAC__uint64 absolute_sample; |
| 478 |
uint32_t frame_number; |
| 479 |
uint32_t channel; |
| 480 |
uint32_t sample; |
| 481 |
FLAC__int32 expected; |
| 482 |
FLAC__int32 got; |
| 483 |
} error_stats; |
| 484 |
} verify; |
| 485 |
FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */ |
| 486 |
uint32_t num_threadtasks; |
| 487 |
#ifdef HAVE_PTHREAD |
| 488 |
uint32_t num_created_threads; |
| 489 |
uint32_t next_thread; /* This is the next thread that needs start, or needs to finish and be restarted */ |
| 490 |
uint32_t num_started_threadtasks; |
| 491 |
uint32_t num_available_threadtasks; /* Number of threadtasks that are available to work on */ |
| 492 |
uint32_t num_running_threads; |
| 493 |
uint32_t next_threadtask; /* Next threadtask that is available to work on */ |
| 494 |
pthread_mutex_t mutex_md5_fifo; |
| 495 |
pthread_mutex_t mutex_work_queue; /* To lock work related variables in this struct */ |
| 496 |
pthread_cond_t cond_md5_emptied; /* To signal to main thread that MD5 queue has been emptied */ |
| 497 |
pthread_cond_t cond_work_available; /* To signal to threads that work is available */ |
| 498 |
pthread_cond_t cond_wake_up_thread; /* To signal that one sleeping thread can wake up */ |
| 499 |
FLAC__bool md5_active; |
| 500 |
FLAC__bool finish_work_threads; |
| 501 |
int32_t overcommitted_indicator; |
| 502 |
verify_input_fifo md5_fifo; |
| 503 |
#endif |
| 504 |
} FLAC__StreamEncoderPrivate; |
| 505 |
|
| 506 |
/*********************************************************************** |
| 507 |
* |
| 508 |
* Public static class data |
| 509 |
* |
| 510 |
***********************************************************************/ |
| 511 |
|
| 512 |
FLAC_API const char * const FLAC__StreamEncoderStateString[] = { |
| 513 |
"FLAC__STREAM_ENCODER_OK", |
| 514 |
"FLAC__STREAM_ENCODER_UNINITIALIZED", |
| 515 |
"FLAC__STREAM_ENCODER_OGG_ERROR", |
| 516 |
"FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR", |
| 517 |
"FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA", |
| 518 |
"FLAC__STREAM_ENCODER_CLIENT_ERROR", |
| 519 |
"FLAC__STREAM_ENCODER_IO_ERROR", |
| 520 |
"FLAC__STREAM_ENCODER_FRAMING_ERROR", |
| 521 |
"FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR" |
| 522 |
}; |
| 523 |
|
| 524 |
FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = { |
| 525 |
"FLAC__STREAM_ENCODER_INIT_STATUS_OK", |
| 526 |
"FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR", |
| 527 |
"FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER", |
| 528 |
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS", |
| 529 |
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS", |
| 530 |
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE", |
| 531 |
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE", |
| 532 |
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE", |
| 533 |
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER", |
| 534 |
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION", |
| 535 |
"FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER", |
| 536 |
"FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE", |
| 537 |
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA", |
| 538 |
"FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED" |
| 539 |
}; |
| 540 |
|
| 541 |
FLAC_API const char * const FLAC__StreamEncoderReadStatusString[] = { |
| 542 |
"FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE", |
| 543 |
"FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM", |
| 544 |
"FLAC__STREAM_ENCODER_READ_STATUS_ABORT", |
| 545 |
"FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED" |
| 546 |
}; |
| 547 |
|
| 548 |
FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = { |
| 549 |
"FLAC__STREAM_ENCODER_WRITE_STATUS_OK", |
| 550 |
"FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR" |
| 551 |
}; |
| 552 |
|
| 553 |
FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = { |
| 554 |
"FLAC__STREAM_ENCODER_SEEK_STATUS_OK", |
| 555 |
"FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR", |
| 556 |
"FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED" |
| 557 |
}; |
| 558 |
|
| 559 |
FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = { |
| 560 |
"FLAC__STREAM_ENCODER_TELL_STATUS_OK", |
| 561 |
"FLAC__STREAM_ENCODER_TELL_STATUS_ERROR", |
| 562 |
"FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED" |
| 563 |
}; |
| 564 |
|
| 565 |
/* Number of samples that will be overread to watch for end of stream. By |
| 566 |
* 'overread', we mean that the FLAC__stream_encoder_process*() calls will |
| 567 |
* always try to read blocksize+1 samples before encoding a block, so that |
| 568 |
* even if the stream has a total sample count that is an integral multiple |
| 569 |
* of the blocksize, we will still notice when we are encoding the last |
| 570 |
* block. This is needed, for example, to correctly set the end-of-stream |
| 571 |
* marker in Ogg FLAC. |
| 572 |
* |
| 573 |
* WATCHOUT: some parts of the code assert that OVERREAD_ == 1 and there's |
| 574 |
* not really any reason to change it. |
| 575 |
*/ |
| 576 |
static const uint32_t OVERREAD_ = 1; |
| 577 |
|
| 578 |
/*********************************************************************** |
| 579 |
* |
| 580 |
* Class constructor/destructor |
| 581 |
* |
| 582 |
*/ |
| 583 |
FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new(void) |
| 584 |
{ |
| 585 |
FLAC__StreamEncoder *encoder; |
| 586 |
uint32_t i; |
| 587 |
|
| 588 |
FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */ |
| 589 |
|
| 590 |
encoder = safe_calloc_(1, sizeof(FLAC__StreamEncoder)); |
| 591 |
if(encoder == 0) { |
| 592 |
return 0; |
| 593 |
} |
| 594 |
|
| 595 |
encoder->protected_ = safe_calloc_(1, sizeof(FLAC__StreamEncoderProtected)); |
| 596 |
if(encoder->protected_ == 0) { |
| 597 |
free(encoder); |
| 598 |
return 0; |
| 599 |
} |
| 600 |
|
| 601 |
encoder->private_ = safe_calloc_(1, sizeof(FLAC__StreamEncoderPrivate)); |
| 602 |
if(encoder->private_ == 0) { |
| 603 |
free(encoder->protected_); |
| 604 |
free(encoder); |
| 605 |
return 0; |
| 606 |
} |
| 607 |
|
| 608 |
encoder->private_->threadtask[0] = safe_calloc_(1, sizeof(FLAC__StreamEncoderThreadTask)); |
| 609 |
if(encoder->private_->threadtask[0] == 0) { |
| 610 |
free(encoder->private_); |
| 611 |
free(encoder->protected_); |
| 612 |
free(encoder); |
| 613 |
return 0; |
| 614 |
} |
| 615 |
|
| 616 |
encoder->private_->threadtask[0]->frame = FLAC__bitwriter_new(); |
| 617 |
if(encoder->private_->threadtask[0]->frame == 0) { |
| 618 |
free(encoder->private_->threadtask[0]); |
| 619 |
free(encoder->private_); |
| 620 |
free(encoder->protected_); |
| 621 |
free(encoder); |
| 622 |
return 0; |
| 623 |
} |
| 624 |
|
| 625 |
encoder->private_->file = 0; |
| 626 |
|
| 627 |
encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED; |
| 628 |
|
| 629 |
set_defaults_(encoder); |
| 630 |
|
| 631 |
encoder->private_->is_being_deleted = false; |
| 632 |
|
| 633 |
for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 634 |
encoder->private_->threadtask[0]->subframe_workspace_ptr[i][0] = &encoder->private_->threadtask[0]->subframe_workspace[i][0]; |
| 635 |
encoder->private_->threadtask[0]->subframe_workspace_ptr[i][1] = &encoder->private_->threadtask[0]->subframe_workspace[i][1]; |
| 636 |
} |
| 637 |
for(i = 0; i < 2; i++) { |
| 638 |
encoder->private_->threadtask[0]->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->threadtask[0]->subframe_workspace_mid_side[i][0]; |
| 639 |
encoder->private_->threadtask[0]->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->threadtask[0]->subframe_workspace_mid_side[i][1]; |
| 640 |
} |
| 641 |
for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 642 |
encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->threadtask[0]->partitioned_rice_contents_workspace[i][0]; |
| 643 |
encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->threadtask[0]->partitioned_rice_contents_workspace[i][1]; |
| 644 |
} |
| 645 |
for(i = 0; i < 2; i++) { |
| 646 |
encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_mid_side[i][0]; |
| 647 |
encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_mid_side[i][1]; |
| 648 |
} |
| 649 |
|
| 650 |
for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 651 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->threadtask[0]->partitioned_rice_contents_workspace[i][0]); |
| 652 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->threadtask[0]->partitioned_rice_contents_workspace[i][1]); |
| 653 |
} |
| 654 |
for(i = 0; i < 2; i++) { |
| 655 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_mid_side[i][0]); |
| 656 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_mid_side[i][1]); |
| 657 |
} |
| 658 |
for(i = 0; i < 2; i++) |
| 659 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->threadtask[0]->partitioned_rice_contents_extra[i]); |
| 660 |
|
| 661 |
return encoder; |
| 662 |
} |
| 663 |
|
| 664 |
FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder) |
| 665 |
{ |
| 666 |
uint32_t i; |
| 667 |
|
| 668 |
if (encoder == NULL) |
| 669 |
return ; |
| 670 |
|
| 671 |
FLAC__ASSERT(0 != encoder->protected_); |
| 672 |
FLAC__ASSERT(0 != encoder->private_); |
| 673 |
FLAC__ASSERT(0 != encoder->private_->threadtask[0]); |
| 674 |
FLAC__ASSERT(0 != encoder->private_->threadtask[0]->frame); |
| 675 |
|
| 676 |
encoder->private_->is_being_deleted = true; |
| 677 |
|
| 678 |
(void)FLAC__stream_encoder_finish(encoder); |
| 679 |
|
| 680 |
if(0 != encoder->private_->verify.decoder) |
| 681 |
FLAC__stream_decoder_delete(encoder->private_->verify.decoder); |
| 682 |
|
| 683 |
for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 684 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->threadtask[0]->partitioned_rice_contents_workspace[i][0]); |
| 685 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->threadtask[0]->partitioned_rice_contents_workspace[i][1]); |
| 686 |
} |
| 687 |
for(i = 0; i < 2; i++) { |
| 688 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_mid_side[i][0]); |
| 689 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->threadtask[0]->partitioned_rice_contents_workspace_mid_side[i][1]); |
| 690 |
} |
| 691 |
for(i = 0; i < 2; i++) |
| 692 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->threadtask[0]->partitioned_rice_contents_extra[i]); |
| 693 |
|
| 694 |
FLAC__bitwriter_delete(encoder->private_->threadtask[0]->frame); |
| 695 |
free(encoder->private_->threadtask[0]); |
| 696 |
free(encoder->private_); |
| 697 |
free(encoder->protected_); |
| 698 |
free(encoder); |
| 699 |
} |
| 700 |
|
| 701 |
/*********************************************************************** |
| 702 |
* |
| 703 |
* Public class methods |
| 704 |
* |
| 705 |
***********************************************************************/ |
| 706 |
|
| 707 |
static FLAC__StreamEncoderInitStatus init_stream_internal_( |
| 708 |
FLAC__StreamEncoder *encoder, |
| 709 |
FLAC__StreamEncoderReadCallback read_callback, |
| 710 |
FLAC__StreamEncoderWriteCallback write_callback, |
| 711 |
FLAC__StreamEncoderSeekCallback seek_callback, |
| 712 |
FLAC__StreamEncoderTellCallback tell_callback, |
| 713 |
FLAC__StreamEncoderMetadataCallback metadata_callback, |
| 714 |
void *client_data, |
| 715 |
FLAC__bool is_ogg |
| 716 |
) |
| 717 |
{ |
| 718 |
uint32_t i, t; |
| 719 |
FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2; |
| 720 |
|
| 721 |
FLAC__ASSERT(0 != encoder); |
| 722 |
|
| 723 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 724 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED; |
| 725 |
|
| 726 |
if(FLAC__HAS_OGG == 0 && is_ogg) |
| 727 |
return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER; |
| 728 |
|
| 729 |
if(0 == write_callback || (seek_callback && 0 == tell_callback)) |
| 730 |
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS; |
| 731 |
|
| 732 |
if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS) |
| 733 |
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS; |
| 734 |
|
| 735 |
if(encoder->protected_->channels != 2) { |
| 736 |
encoder->protected_->do_mid_side_stereo = false; |
| 737 |
encoder->protected_->loose_mid_side_stereo = false; |
| 738 |
} |
| 739 |
else if(!encoder->protected_->do_mid_side_stereo) |
| 740 |
encoder->protected_->loose_mid_side_stereo = false; |
| 741 |
|
| 742 |
if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE) |
| 743 |
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE; |
| 744 |
|
| 745 |
if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate)) |
| 746 |
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE; |
| 747 |
|
| 748 |
if(encoder->protected_->blocksize == 0) { |
| 749 |
if(encoder->protected_->max_lpc_order == 0) |
| 750 |
encoder->protected_->blocksize = 1152; |
| 751 |
else |
| 752 |
encoder->protected_->blocksize = 4096; |
| 753 |
} |
| 754 |
|
| 755 |
if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE) |
| 756 |
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE; |
| 757 |
|
| 758 |
if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER) |
| 759 |
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER; |
| 760 |
|
| 761 |
if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order) |
| 762 |
return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER; |
| 763 |
|
| 764 |
if(encoder->protected_->qlp_coeff_precision == 0) { |
| 765 |
if(encoder->protected_->bits_per_sample < 16) { |
| 766 |
/* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */ |
| 767 |
/* @@@ until then we'll make a guess */ |
| 768 |
encoder->protected_->qlp_coeff_precision = flac_max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2); |
| 769 |
} |
| 770 |
else if(encoder->protected_->bits_per_sample == 16) { |
| 771 |
if(encoder->protected_->blocksize <= 192) |
| 772 |
encoder->protected_->qlp_coeff_precision = 7; |
| 773 |
else if(encoder->protected_->blocksize <= 384) |
| 774 |
encoder->protected_->qlp_coeff_precision = 8; |
| 775 |
else if(encoder->protected_->blocksize <= 576) |
| 776 |
encoder->protected_->qlp_coeff_precision = 9; |
| 777 |
else if(encoder->protected_->blocksize <= 1152) |
| 778 |
encoder->protected_->qlp_coeff_precision = 10; |
| 779 |
else if(encoder->protected_->blocksize <= 2304) |
| 780 |
encoder->protected_->qlp_coeff_precision = 11; |
| 781 |
else if(encoder->protected_->blocksize <= 4608) |
| 782 |
encoder->protected_->qlp_coeff_precision = 12; |
| 783 |
else |
| 784 |
encoder->protected_->qlp_coeff_precision = 13; |
| 785 |
} |
| 786 |
else { |
| 787 |
if(encoder->protected_->blocksize <= 384) |
| 788 |
encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2; |
| 789 |
else if(encoder->protected_->blocksize <= 1152) |
| 790 |
encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1; |
| 791 |
else |
| 792 |
encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION; |
| 793 |
} |
| 794 |
FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION); |
| 795 |
} |
| 796 |
else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION) |
| 797 |
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION; |
| 798 |
|
| 799 |
if(encoder->protected_->streamable_subset) { |
| 800 |
if(!FLAC__format_blocksize_is_subset(encoder->protected_->blocksize, encoder->protected_->sample_rate)) |
| 801 |
return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE; |
| 802 |
if(!FLAC__format_sample_rate_is_subset(encoder->protected_->sample_rate)) |
| 803 |
return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE; |
| 804 |
if( |
| 805 |
encoder->protected_->bits_per_sample != 8 && |
| 806 |
encoder->protected_->bits_per_sample != 12 && |
| 807 |
encoder->protected_->bits_per_sample != 16 && |
| 808 |
encoder->protected_->bits_per_sample != 20 && |
| 809 |
encoder->protected_->bits_per_sample != 24 && |
| 810 |
encoder->protected_->bits_per_sample != 32 |
| 811 |
) |
| 812 |
return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE; |
| 813 |
if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER) |
| 814 |
return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE; |
| 815 |
if( |
| 816 |
encoder->protected_->sample_rate <= 48000 && |
| 817 |
( |
| 818 |
encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ || |
| 819 |
encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ |
| 820 |
) |
| 821 |
) { |
| 822 |
return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE; |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN)) |
| 827 |
encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1; |
| 828 |
if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order) |
| 829 |
encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order; |
| 830 |
|
| 831 |
#if FLAC__HAS_OGG |
| 832 |
/* drop any seektable for ogg */ |
| 833 |
if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) { |
| 834 |
uint32_t i1; |
| 835 |
for(i1 = 0; i1 < encoder->protected_->num_metadata_blocks; i1++) { |
| 836 |
if(0 != encoder->protected_->metadata[i1] && encoder->protected_->metadata[i1]->type == FLAC__METADATA_TYPE_SEEKTABLE) { |
| 837 |
encoder->protected_->num_metadata_blocks--; |
| 838 |
for( ; i1 < encoder->protected_->num_metadata_blocks; i1++) |
| 839 |
encoder->protected_->metadata[i1] = encoder->protected_->metadata[i1+1]; |
| 840 |
break; |
| 841 |
} |
| 842 |
} |
| 843 |
} |
| 844 |
/* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */ |
| 845 |
if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) { |
| 846 |
uint32_t i1; |
| 847 |
for(i1 = 1; i1 < encoder->protected_->num_metadata_blocks; i1++) { |
| 848 |
if(0 != encoder->protected_->metadata[i1] && encoder->protected_->metadata[i1]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) { |
| 849 |
FLAC__StreamMetadata *vc = encoder->protected_->metadata[i1]; |
| 850 |
for( ; i1 > 0; i1--) |
| 851 |
encoder->protected_->metadata[i1] = encoder->protected_->metadata[i1-1]; |
| 852 |
encoder->protected_->metadata[0] = vc; |
| 853 |
break; |
| 854 |
} |
| 855 |
} |
| 856 |
} |
| 857 |
#endif |
| 858 |
/* keep track of any SEEKTABLE block */ |
| 859 |
if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) { |
| 860 |
uint32_t i2; |
| 861 |
for(i2 = 0; i2 < encoder->protected_->num_metadata_blocks; i2++) { |
| 862 |
if(0 != encoder->protected_->metadata[i2] && encoder->protected_->metadata[i2]->type == FLAC__METADATA_TYPE_SEEKTABLE) { |
| 863 |
encoder->private_->seek_table = &encoder->protected_->metadata[i2]->data.seek_table; |
| 864 |
break; /* take only the first one */ |
| 865 |
} |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
/* validate metadata */ |
| 870 |
if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) |
| 871 |
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 872 |
metadata_has_seektable = false; |
| 873 |
metadata_has_vorbis_comment = false; |
| 874 |
metadata_picture_has_type1 = false; |
| 875 |
metadata_picture_has_type2 = false; |
| 876 |
for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) { |
| 877 |
const FLAC__StreamMetadata *m = encoder->protected_->metadata[i]; |
| 878 |
if(m->type == FLAC__METADATA_TYPE_STREAMINFO) |
| 879 |
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 880 |
else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) { |
| 881 |
if(metadata_has_seektable) /* only one is allowed */ |
| 882 |
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 883 |
metadata_has_seektable = true; |
| 884 |
if(!FLAC__format_seektable_is_legal(&m->data.seek_table)) |
| 885 |
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 886 |
} |
| 887 |
else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) { |
| 888 |
if(metadata_has_vorbis_comment) /* only one is allowed */ |
| 889 |
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 890 |
metadata_has_vorbis_comment = true; |
| 891 |
} |
| 892 |
else if(m->type == FLAC__METADATA_TYPE_CUESHEET) { |
| 893 |
if(!FLAC__format_cuesheet_is_legal(&m->data.cue_sheet, m->data.cue_sheet.is_cd, /*violation=*/0)) |
| 894 |
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 895 |
} |
| 896 |
else if(m->type == FLAC__METADATA_TYPE_PICTURE) { |
| 897 |
if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0)) |
| 898 |
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 899 |
if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) { |
| 900 |
if(metadata_picture_has_type1) /* there should only be 1 per stream */ |
| 901 |
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 902 |
metadata_picture_has_type1 = true; |
| 903 |
/* standard icon must be 32x32 pixel PNG */ |
| 904 |
if( |
| 905 |
m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD && |
| 906 |
( |
| 907 |
(strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) || |
| 908 |
m->data.picture.width != 32 || |
| 909 |
m->data.picture.height != 32 |
| 910 |
) |
| 911 |
) |
| 912 |
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 913 |
} |
| 914 |
else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) { |
| 915 |
if(metadata_picture_has_type2) /* there should only be 1 per stream */ |
| 916 |
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
| 917 |
metadata_picture_has_type2 = true; |
| 918 |
} |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
encoder->private_->input_capacity = 0; |
| 923 |
encoder->private_->current_sample_number = 0; |
| 924 |
encoder->private_->current_frame_number = 0; |
| 925 |
|
| 926 |
/* |
| 927 |
* get the CPU info and set the function pointers |
| 928 |
*/ |
| 929 |
FLAC__cpu_info(&encoder->private_->cpuinfo); |
| 930 |
/* remove cpu info as requested by |
| 931 |
* FLAC__stream_encoder_disable_instruction_set */ |
| 932 |
if(encoder->private_->disable_mmx) |
| 933 |
encoder->private_->cpuinfo.x86.mmx = false; |
| 934 |
if(encoder->private_->disable_sse2) |
| 935 |
encoder->private_->cpuinfo.x86.sse2 = false; |
| 936 |
if(encoder->private_->disable_ssse3) |
| 937 |
encoder->private_->cpuinfo.x86.ssse3 = false; |
| 938 |
if(encoder->private_->disable_sse41) |
| 939 |
encoder->private_->cpuinfo.x86.sse41 = false; |
| 940 |
if(encoder->private_->disable_sse42) |
| 941 |
encoder->private_->cpuinfo.x86.sse42 = false; |
| 942 |
if(encoder->private_->disable_avx2) |
| 943 |
encoder->private_->cpuinfo.x86.avx2 = false; |
| 944 |
if(encoder->private_->disable_fma) |
| 945 |
encoder->private_->cpuinfo.x86.fma = false; |
| 946 |
/* first default to the non-asm routines */ |
| 947 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 948 |
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation; |
| 949 |
#endif |
| 950 |
encoder->private_->local_precompute_partition_info_sums = precompute_partition_info_sums_; |
| 951 |
encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor; |
| 952 |
encoder->private_->local_fixed_compute_best_predictor_wide = FLAC__fixed_compute_best_predictor_wide; |
| 953 |
encoder->private_->local_fixed_compute_best_predictor_limit_residual = FLAC__fixed_compute_best_predictor_limit_residual; |
| 954 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 955 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients; |
| 956 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide; |
| 957 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients; |
| 958 |
#endif |
| 959 |
/* now override with asm where appropriate */ |
| 960 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 961 |
# ifndef FLAC__NO_ASM |
| 962 |
#if defined FLAC__CPU_ARM64 && FLAC__HAS_NEONINTRIN |
| 963 |
#if FLAC__HAS_A64NEONINTRIN |
| 964 |
if(encoder->protected_->max_lpc_order < 8) |
| 965 |
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_neon_lag_8; |
| 966 |
else if(encoder->protected_->max_lpc_order < 10) |
| 967 |
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_neon_lag_10; |
| 968 |
else if(encoder->protected_->max_lpc_order < 14) |
| 969 |
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_neon_lag_14; |
| 970 |
else |
| 971 |
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation; |
| 972 |
#endif |
| 973 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_neon; |
| 974 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_neon; |
| 975 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide_intrin_neon; |
| 976 |
#endif /* defined FLAC__CPU_ARM64 && FLAC__HAS_NEONINTRIN */ |
| 977 |
|
| 978 |
if(encoder->private_->cpuinfo.use_asm) { |
| 979 |
# ifdef FLAC__CPU_IA32 |
| 980 |
FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32); |
| 981 |
# if FLAC__HAS_X86INTRIN |
| 982 |
# ifdef FLAC__SSE2_SUPPORTED |
| 983 |
if (encoder->private_->cpuinfo.x86.sse2) { |
| 984 |
if(encoder->protected_->max_lpc_order < 8) |
| 985 |
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse2_lag_8; |
| 986 |
else if(encoder->protected_->max_lpc_order < 10) |
| 987 |
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse2_lag_10; |
| 988 |
else if(encoder->protected_->max_lpc_order < 14) |
| 989 |
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse2_lag_14; |
| 990 |
|
| 991 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_sse2; |
| 992 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_16_intrin_sse2; |
| 993 |
} |
| 994 |
# endif |
| 995 |
# ifdef FLAC__SSE4_1_SUPPORTED |
| 996 |
if (encoder->private_->cpuinfo.x86.sse41) { |
| 997 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_sse41; |
| 998 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide_intrin_sse41; |
| 999 |
} |
| 1000 |
# endif |
| 1001 |
# ifdef FLAC__AVX2_SUPPORTED |
| 1002 |
if (encoder->private_->cpuinfo.x86.avx2) { |
| 1003 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_16_intrin_avx2; |
| 1004 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_avx2; |
| 1005 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide_intrin_avx2; |
| 1006 |
} |
| 1007 |
# endif |
| 1008 |
|
| 1009 |
# ifdef FLAC__SSE2_SUPPORTED |
| 1010 |
if (encoder->private_->cpuinfo.x86.sse2) { |
| 1011 |
encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_intrin_sse2; |
| 1012 |
} |
| 1013 |
# endif |
| 1014 |
# ifdef FLAC__SSSE3_SUPPORTED |
| 1015 |
if (encoder->private_->cpuinfo.x86.ssse3) { |
| 1016 |
encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_intrin_ssse3; |
| 1017 |
} |
| 1018 |
# endif |
| 1019 |
# ifdef FLAC__SSE4_2_SUPPORTED |
| 1020 |
if (encoder->private_->cpuinfo.x86.sse42) { |
| 1021 |
encoder->private_->local_fixed_compute_best_predictor_limit_residual = FLAC__fixed_compute_best_predictor_limit_residual_intrin_sse42; |
| 1022 |
} |
| 1023 |
# endif |
| 1024 |
# ifdef FLAC__AVX2_SUPPORTED |
| 1025 |
if (encoder->private_->cpuinfo.x86.avx2) { |
| 1026 |
encoder->private_->local_fixed_compute_best_predictor_wide = FLAC__fixed_compute_best_predictor_wide_intrin_avx2; |
| 1027 |
encoder->private_->local_fixed_compute_best_predictor_limit_residual = FLAC__fixed_compute_best_predictor_limit_residual_intrin_avx2; |
| 1028 |
} |
| 1029 |
# endif |
| 1030 |
# endif /* FLAC__HAS_X86INTRIN */ |
| 1031 |
# elif defined FLAC__CPU_X86_64 |
| 1032 |
FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_X86_64); |
| 1033 |
# if FLAC__HAS_X86INTRIN |
| 1034 |
# ifdef FLAC__SSE2_SUPPORTED |
| 1035 |
if(encoder->private_->cpuinfo.x86.sse2) { /* For fuzzing */ |
| 1036 |
if(encoder->protected_->max_lpc_order < 8) |
| 1037 |
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse2_lag_8; |
| 1038 |
else if(encoder->protected_->max_lpc_order < 10) |
| 1039 |
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse2_lag_10; |
| 1040 |
else if(encoder->protected_->max_lpc_order < 14) |
| 1041 |
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse2_lag_14; |
| 1042 |
|
| 1043 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_16_intrin_sse2; |
| 1044 |
} |
| 1045 |
# endif |
| 1046 |
# ifdef FLAC__SSE4_1_SUPPORTED |
| 1047 |
if(encoder->private_->cpuinfo.x86.sse41) { |
| 1048 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_sse41; |
| 1049 |
} |
| 1050 |
# endif |
| 1051 |
# ifdef FLAC__AVX2_SUPPORTED |
| 1052 |
if(encoder->private_->cpuinfo.x86.avx2) { |
| 1053 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_16_intrin_avx2; |
| 1054 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_avx2; |
| 1055 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide_intrin_avx2; |
| 1056 |
} |
| 1057 |
# endif |
| 1058 |
# ifdef FLAC__FMA_SUPPORTED |
| 1059 |
if(encoder->private_->cpuinfo.x86.fma) { |
| 1060 |
if(encoder->protected_->max_lpc_order < 8) |
| 1061 |
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_fma_lag_8; |
| 1062 |
else if(encoder->protected_->max_lpc_order < 12) |
| 1063 |
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_fma_lag_12; |
| 1064 |
else if(encoder->protected_->max_lpc_order < 16) |
| 1065 |
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_fma_lag_16; |
| 1066 |
} |
| 1067 |
# endif |
| 1068 |
|
| 1069 |
|
| 1070 |
# ifdef FLAC__SSE2_SUPPORTED |
| 1071 |
if(encoder->private_->cpuinfo.x86.sse2) { /* For fuzzing */ |
| 1072 |
encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_intrin_sse2; |
| 1073 |
} |
| 1074 |
# endif |
| 1075 |
# ifdef FLAC__SSSE3_SUPPORTED |
| 1076 |
if (encoder->private_->cpuinfo.x86.ssse3) { |
| 1077 |
encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_intrin_ssse3; |
| 1078 |
} |
| 1079 |
# endif |
| 1080 |
# ifdef FLAC__SSE4_2_SUPPORTED |
| 1081 |
if (encoder->private_->cpuinfo.x86.sse42) { |
| 1082 |
encoder->private_->local_fixed_compute_best_predictor_limit_residual = FLAC__fixed_compute_best_predictor_limit_residual_intrin_sse42; |
| 1083 |
} |
| 1084 |
# endif |
| 1085 |
# ifdef FLAC__AVX2_SUPPORTED |
| 1086 |
if (encoder->private_->cpuinfo.x86.avx2) { |
| 1087 |
encoder->private_->local_fixed_compute_best_predictor_wide = FLAC__fixed_compute_best_predictor_wide_intrin_avx2; |
| 1088 |
encoder->private_->local_fixed_compute_best_predictor_limit_residual = FLAC__fixed_compute_best_predictor_limit_residual_intrin_avx2; |
| 1089 |
} |
| 1090 |
# endif |
| 1091 |
# endif /* FLAC__HAS_X86INTRIN */ |
| 1092 |
# endif /* FLAC__CPU_... */ |
| 1093 |
} |
| 1094 |
# endif /* !FLAC__NO_ASM */ |
| 1095 |
|
| 1096 |
#endif /* !FLAC__INTEGER_ONLY_LIBRARY */ |
| 1097 |
#if !defined FLAC__NO_ASM && FLAC__HAS_X86INTRIN |
| 1098 |
if(encoder->private_->cpuinfo.use_asm) { |
| 1099 |
# if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) |
| 1100 |
# ifdef FLAC__SSE2_SUPPORTED |
| 1101 |
if (encoder->private_->cpuinfo.x86.sse2) |
| 1102 |
encoder->private_->local_precompute_partition_info_sums = FLAC__precompute_partition_info_sums_intrin_sse2; |
| 1103 |
# endif |
| 1104 |
# ifdef FLAC__SSSE3_SUPPORTED |
| 1105 |
if (encoder->private_->cpuinfo.x86.ssse3) |
| 1106 |
encoder->private_->local_precompute_partition_info_sums = FLAC__precompute_partition_info_sums_intrin_ssse3; |
| 1107 |
# endif |
| 1108 |
# ifdef FLAC__AVX2_SUPPORTED |
| 1109 |
if (encoder->private_->cpuinfo.x86.avx2) |
| 1110 |
encoder->private_->local_precompute_partition_info_sums = FLAC__precompute_partition_info_sums_intrin_avx2; |
| 1111 |
# endif |
| 1112 |
# endif /* FLAC__CPU_... */ |
| 1113 |
} |
| 1114 |
#endif /* !FLAC__NO_ASM && FLAC__HAS_X86INTRIN */ |
| 1115 |
|
| 1116 |
/* set state to OK; from here on, errors are fatal and we'll override the state then */ |
| 1117 |
encoder->protected_->state = FLAC__STREAM_ENCODER_OK; |
| 1118 |
|
| 1119 |
#if FLAC__HAS_OGG |
| 1120 |
encoder->private_->is_ogg = is_ogg; |
| 1121 |
if(is_ogg && !FLAC__ogg_encoder_aspect_init(&encoder->protected_->ogg_encoder_aspect)) { |
| 1122 |
encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR; |
| 1123 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1124 |
} |
| 1125 |
#endif |
| 1126 |
|
| 1127 |
encoder->private_->read_callback = read_callback; |
| 1128 |
encoder->private_->write_callback = write_callback; |
| 1129 |
encoder->private_->seek_callback = seek_callback; |
| 1130 |
encoder->private_->tell_callback = tell_callback; |
| 1131 |
encoder->private_->metadata_callback = metadata_callback; |
| 1132 |
encoder->private_->client_data = client_data; |
| 1133 |
|
| 1134 |
if(encoder->protected_->num_threads > 1) { |
| 1135 |
#ifdef HAVE_PTHREAD |
| 1136 |
encoder->private_->num_threadtasks = encoder->protected_->num_threads * 2 + 2; /* First threadtask is reserved for main thread */ |
| 1137 |
if(pthread_mutex_init(&encoder->private_->mutex_md5_fifo, NULL)) { |
| 1138 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 1139 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1140 |
} |
| 1141 |
if(pthread_mutex_init(&encoder->private_->mutex_work_queue, NULL)) { |
| 1142 |
pthread_mutex_destroy(&encoder->private_->mutex_md5_fifo); |
| 1143 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 1144 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1145 |
} |
| 1146 |
if(pthread_cond_init(&encoder->private_->cond_md5_emptied, NULL)) { |
| 1147 |
pthread_mutex_destroy(&encoder->private_->mutex_md5_fifo); |
| 1148 |
pthread_mutex_destroy(&encoder->private_->mutex_work_queue); |
| 1149 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 1150 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1151 |
} |
| 1152 |
if(pthread_cond_init(&encoder->private_->cond_work_available, NULL)) { |
| 1153 |
pthread_mutex_destroy(&encoder->private_->mutex_md5_fifo); |
| 1154 |
pthread_mutex_destroy(&encoder->private_->mutex_work_queue); |
| 1155 |
pthread_cond_destroy(&encoder->private_->cond_md5_emptied); |
| 1156 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 1157 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1158 |
} |
| 1159 |
if(pthread_cond_init(&encoder->private_->cond_wake_up_thread, NULL)) { |
| 1160 |
pthread_mutex_destroy(&encoder->private_->mutex_md5_fifo); |
| 1161 |
pthread_mutex_destroy(&encoder->private_->mutex_work_queue); |
| 1162 |
pthread_cond_destroy(&encoder->private_->cond_md5_emptied); |
| 1163 |
pthread_cond_destroy(&encoder->private_->cond_work_available); |
| 1164 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 1165 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1166 |
} |
| 1167 |
if(encoder->protected_->do_md5) { |
| 1168 |
encoder->private_->md5_fifo.size = (encoder->protected_->blocksize+OVERREAD_) * (encoder->private_->num_threadtasks + 2); |
| 1169 |
for(i = 0; i < encoder->protected_->channels; i++) { |
| 1170 |
if(0 == (encoder->private_->md5_fifo.data[i] = safe_malloc_mul_2op_p(sizeof(FLAC__int32), /*times*/encoder->private_->md5_fifo.size))) { |
| 1171 |
pthread_mutex_destroy(&encoder->private_->mutex_md5_fifo); |
| 1172 |
pthread_mutex_destroy(&encoder->private_->mutex_work_queue); |
| 1173 |
pthread_cond_destroy(&encoder->private_->cond_md5_emptied); |
| 1174 |
pthread_cond_destroy(&encoder->private_->cond_work_available); |
| 1175 |
pthread_cond_destroy(&encoder->private_->cond_wake_up_thread); |
| 1176 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 1177 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1178 |
} |
| 1179 |
} |
| 1180 |
} |
| 1181 |
encoder->private_->md5_fifo.tail = 0; |
| 1182 |
for(t = 1; t < encoder->private_->num_threadtasks; t++) { |
| 1183 |
encoder->private_->threadtask[t] = safe_calloc_(1, sizeof(FLAC__StreamEncoderThreadTask)); |
| 1184 |
if(encoder->private_->threadtask[t] == NULL) { |
| 1185 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 1186 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1187 |
} |
| 1188 |
encoder->private_->threadtask[t]->frame = FLAC__bitwriter_new(); |
| 1189 |
if(encoder->private_->threadtask[t]->frame == NULL) { |
| 1190 |
free(encoder->private_->threadtask[t]); |
| 1191 |
encoder->private_->threadtask[t] = 0; |
| 1192 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 1193 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1194 |
} |
| 1195 |
if(pthread_mutex_init(&encoder->private_->threadtask[t]->mutex_this_task, NULL)) { |
| 1196 |
FLAC__bitwriter_delete(encoder->private_->threadtask[t]->frame); |
| 1197 |
free(encoder->private_->threadtask[t]); |
| 1198 |
encoder->private_->threadtask[t] = 0; |
| 1199 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 1200 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1201 |
} |
| 1202 |
if(pthread_cond_init(&encoder->private_->threadtask[t]->cond_task_done, NULL)) { |
| 1203 |
pthread_mutex_destroy(&encoder->private_->threadtask[t]->mutex_this_task); |
| 1204 |
FLAC__bitwriter_delete(encoder->private_->threadtask[t]->frame); |
| 1205 |
free(encoder->private_->threadtask[t]); |
| 1206 |
encoder->private_->threadtask[t] = 0; |
| 1207 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 1208 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1209 |
} |
| 1210 |
|
| 1211 |
for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 1212 |
encoder->private_->threadtask[t]->subframe_workspace_ptr[i][0] = &encoder->private_->threadtask[t]->subframe_workspace[i][0]; |
| 1213 |
encoder->private_->threadtask[t]->subframe_workspace_ptr[i][1] = &encoder->private_->threadtask[t]->subframe_workspace[i][1]; |
| 1214 |
} |
| 1215 |
for(i = 0; i < 2; i++) { |
| 1216 |
encoder->private_->threadtask[t]->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->threadtask[t]->subframe_workspace_mid_side[i][0]; |
| 1217 |
encoder->private_->threadtask[t]->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->threadtask[t]->subframe_workspace_mid_side[i][1]; |
| 1218 |
} |
| 1219 |
for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 1220 |
encoder->private_->threadtask[t]->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->threadtask[t]->partitioned_rice_contents_workspace[i][0]; |
| 1221 |
encoder->private_->threadtask[t]->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->threadtask[t]->partitioned_rice_contents_workspace[i][1]; |
| 1222 |
} |
| 1223 |
for(i = 0; i < 2; i++) { |
| 1224 |
encoder->private_->threadtask[t]->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->threadtask[t]->partitioned_rice_contents_workspace_mid_side[i][0]; |
| 1225 |
encoder->private_->threadtask[t]->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->threadtask[t]->partitioned_rice_contents_workspace_mid_side[i][1]; |
| 1226 |
} |
| 1227 |
|
| 1228 |
for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 1229 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->threadtask[t]->partitioned_rice_contents_workspace[i][0]); |
| 1230 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->threadtask[t]->partitioned_rice_contents_workspace[i][1]); |
| 1231 |
} |
| 1232 |
for(i = 0; i < 2; i++) { |
| 1233 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->threadtask[t]->partitioned_rice_contents_workspace_mid_side[i][0]); |
| 1234 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->threadtask[t]->partitioned_rice_contents_workspace_mid_side[i][1]); |
| 1235 |
} |
| 1236 |
for(i = 0; i < 2; i++) |
| 1237 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->threadtask[t]->partitioned_rice_contents_extra[i]); |
| 1238 |
} |
| 1239 |
#else |
| 1240 |
FLAC__ASSERT(0); |
| 1241 |
#endif |
| 1242 |
} |
| 1243 |
|
| 1244 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 1245 |
for(i = 0; i < encoder->protected_->num_apodizations; i++) |
| 1246 |
encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0; |
| 1247 |
#endif |
| 1248 |
for(t = 0; t < encoder->private_->num_threadtasks; t++) { |
| 1249 |
for(i = 0; i < encoder->protected_->channels; i++) { |
| 1250 |
encoder->private_->threadtask[t]->integer_signal_unaligned[i] = encoder->private_->threadtask[t]->integer_signal[i] = 0; |
| 1251 |
} |
| 1252 |
for(i = 0; i < 2; i++) { |
| 1253 |
encoder->private_->threadtask[t]->integer_signal_mid_side_unaligned[i] = encoder->private_->threadtask[t]->integer_signal_mid_side[i] = 0; |
| 1254 |
} |
| 1255 |
encoder->private_->threadtask[t]->integer_signal_33bit_side_unaligned = encoder->private_->threadtask[t]->integer_signal_33bit_side = 0; |
| 1256 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 1257 |
encoder->private_->threadtask[t]->windowed_signal_unaligned = encoder->private_->threadtask[t]->windowed_signal = 0; |
| 1258 |
#endif |
| 1259 |
for(i = 0; i < encoder->protected_->channels; i++) { |
| 1260 |
encoder->private_->threadtask[t]->residual_workspace_unaligned[i][0] = encoder->private_->threadtask[t]->residual_workspace[i][0] = 0; |
| 1261 |
encoder->private_->threadtask[t]->residual_workspace_unaligned[i][1] = encoder->private_->threadtask[t]->residual_workspace[i][1] = 0; |
| 1262 |
encoder->private_->threadtask[t]->best_subframe[i] = 0; |
| 1263 |
} |
| 1264 |
for(i = 0; i < 2; i++) { |
| 1265 |
encoder->private_->threadtask[t]->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->threadtask[t]->residual_workspace_mid_side[i][0] = 0; |
| 1266 |
encoder->private_->threadtask[t]->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->threadtask[t]->residual_workspace_mid_side[i][1] = 0; |
| 1267 |
encoder->private_->threadtask[t]->best_subframe_mid_side[i] = 0; |
| 1268 |
} |
| 1269 |
encoder->private_->threadtask[t]->abs_residual_partition_sums_unaligned = encoder->private_->threadtask[t]->abs_residual_partition_sums = 0; |
| 1270 |
encoder->private_->threadtask[t]->raw_bits_per_partition_unaligned = encoder->private_->threadtask[t]->raw_bits_per_partition = 0; |
| 1271 |
} |
| 1272 |
|
| 1273 |
|
| 1274 |
if(!resize_buffers_(encoder, encoder->protected_->blocksize)) { |
| 1275 |
/* the above function sets the state for us in case of an error */ |
| 1276 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1277 |
} |
| 1278 |
|
| 1279 |
for(t = 0; t < encoder->private_->num_threadtasks; t++) { |
| 1280 |
if(!FLAC__bitwriter_init(encoder->private_->threadtask[t]->frame)) { |
| 1281 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 1282 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1283 |
} |
| 1284 |
} |
| 1285 |
|
| 1286 |
/* |
| 1287 |
* Set up the verify stuff if necessary |
| 1288 |
*/ |
| 1289 |
if(encoder->protected_->verify) { |
| 1290 |
/* |
| 1291 |
* First, set up the fifo which will hold the |
| 1292 |
* original signal to compare against |
| 1293 |
*/ |
| 1294 |
encoder->private_->verify.input_fifo.size = (encoder->protected_->blocksize+OVERREAD_) * encoder->private_->num_threadtasks; |
| 1295 |
for(i = 0; i < encoder->protected_->channels; i++) { |
| 1296 |
if(0 == (encoder->private_->verify.input_fifo.data[i] = safe_malloc_mul_2op_p(sizeof(FLAC__int32), /*times*/encoder->private_->verify.input_fifo.size))) { |
| 1297 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 1298 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1299 |
} |
| 1300 |
} |
| 1301 |
encoder->private_->verify.input_fifo.tail = 0; |
| 1302 |
|
| 1303 |
/* |
| 1304 |
* Now set up a stream decoder for verification |
| 1305 |
*/ |
| 1306 |
if(0 == encoder->private_->verify.decoder) { |
| 1307 |
encoder->private_->verify.decoder = FLAC__stream_decoder_new(); |
| 1308 |
if(0 == encoder->private_->verify.decoder) { |
| 1309 |
encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 1310 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1311 |
} |
| 1312 |
} |
| 1313 |
|
| 1314 |
if(FLAC__stream_decoder_init_stream(encoder->private_->verify.decoder, verify_read_callback_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, verify_write_callback_, verify_metadata_callback_, verify_error_callback_, /*client_data=*/encoder) != FLAC__STREAM_DECODER_INIT_STATUS_OK) { |
| 1315 |
encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 1316 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1317 |
} |
| 1318 |
} |
| 1319 |
encoder->private_->verify.error_stats.absolute_sample = 0; |
| 1320 |
encoder->private_->verify.error_stats.frame_number = 0; |
| 1321 |
encoder->private_->verify.error_stats.channel = 0; |
| 1322 |
encoder->private_->verify.error_stats.sample = 0; |
| 1323 |
encoder->private_->verify.error_stats.expected = 0; |
| 1324 |
encoder->private_->verify.error_stats.got = 0; |
| 1325 |
|
| 1326 |
/* |
| 1327 |
* These must be done before we write any metadata, because that |
| 1328 |
* calls the write_callback, which uses these values. |
| 1329 |
*/ |
| 1330 |
encoder->private_->first_seekpoint_to_check = 0; |
| 1331 |
encoder->private_->samples_written = 0; |
| 1332 |
encoder->protected_->streaminfo_offset = 0; |
| 1333 |
encoder->protected_->seektable_offset = 0; |
| 1334 |
encoder->protected_->audio_offset = 0; |
| 1335 |
|
| 1336 |
/* |
| 1337 |
* write the stream header |
| 1338 |
*/ |
| 1339 |
if(encoder->protected_->verify) |
| 1340 |
encoder->private_->verify.state_hint = ENCODER_IN_MAGIC; |
| 1341 |
if(!FLAC__bitwriter_write_raw_uint32(encoder->private_->threadtask[0]->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) { |
| 1342 |
encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 1343 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1344 |
} |
| 1345 |
if(!write_bitbuffer_(encoder, encoder->private_->threadtask[0], 0, /*is_last_block=*/false)) { |
| 1346 |
/* the above function sets the state for us in case of an error */ |
| 1347 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1348 |
} |
| 1349 |
|
| 1350 |
/* |
| 1351 |
* write the STREAMINFO metadata block |
| 1352 |
*/ |
| 1353 |
if(encoder->protected_->verify) |
| 1354 |
encoder->private_->verify.state_hint = ENCODER_IN_METADATA; |
| 1355 |
encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO; |
| 1356 |
encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */ |
| 1357 |
encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH; |
| 1358 |
encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */ |
| 1359 |
encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize; |
| 1360 |
encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 1361 |
encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 1362 |
encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate; |
| 1363 |
encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels; |
| 1364 |
encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample; |
| 1365 |
encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */ |
| 1366 |
memset(encoder->private_->streaminfo.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */ |
| 1367 |
if(encoder->protected_->do_md5) |
| 1368 |
FLAC__MD5Init(&encoder->private_->md5context); |
| 1369 |
if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->threadtask[0]->frame, true)) { |
| 1370 |
encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 1371 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1372 |
} |
| 1373 |
if(!write_bitbuffer_(encoder, encoder->private_->threadtask[0], 0, /*is_last_block=*/false)) { |
| 1374 |
/* the above function sets the state for us in case of an error */ |
| 1375 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1376 |
} |
| 1377 |
|
| 1378 |
/* |
| 1379 |
* Now that the STREAMINFO block is written, we can init this to an |
| 1380 |
* absurdly-high value... |
| 1381 |
*/ |
| 1382 |
encoder->private_->streaminfo.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1; |
| 1383 |
/* ... and clear this to 0 */ |
| 1384 |
encoder->private_->streaminfo.data.stream_info.total_samples = 0; |
| 1385 |
|
| 1386 |
/* |
| 1387 |
* Check to see if the supplied metadata contains a VORBIS_COMMENT; |
| 1388 |
* if not, we will write an empty one (FLAC__add_metadata_block() |
| 1389 |
* automatically supplies the vendor string). |
| 1390 |
* |
| 1391 |
* WATCHOUT: the Ogg FLAC mapping requires us to write this block after |
| 1392 |
* the STREAMINFO. (In the case that metadata_has_vorbis_comment is |
| 1393 |
* true it will have already insured that the metadata list is properly |
| 1394 |
* ordered.) |
| 1395 |
*/ |
| 1396 |
if(!metadata_has_vorbis_comment) { |
| 1397 |
FLAC__StreamMetadata vorbis_comment; |
| 1398 |
vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT; |
| 1399 |
vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0); |
| 1400 |
vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */ |
| 1401 |
vorbis_comment.data.vorbis_comment.vendor_string.length = 0; |
| 1402 |
vorbis_comment.data.vorbis_comment.vendor_string.entry = 0; |
| 1403 |
vorbis_comment.data.vorbis_comment.num_comments = 0; |
| 1404 |
vorbis_comment.data.vorbis_comment.comments = 0; |
| 1405 |
if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->threadtask[0]->frame, true)) { |
| 1406 |
encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 1407 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1408 |
} |
| 1409 |
if(!write_bitbuffer_(encoder, encoder->private_->threadtask[0], 0, /*is_last_block=*/false)) { |
| 1410 |
/* the above function sets the state for us in case of an error */ |
| 1411 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1412 |
} |
| 1413 |
} |
| 1414 |
|
| 1415 |
/* |
| 1416 |
* write the user's metadata blocks |
| 1417 |
*/ |
| 1418 |
for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) { |
| 1419 |
encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1); |
| 1420 |
if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->threadtask[0]->frame, true)) { |
| 1421 |
encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 1422 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1423 |
} |
| 1424 |
if(!write_bitbuffer_(encoder, encoder->private_->threadtask[0], 0, /*is_last_block=*/false)) { |
| 1425 |
/* the above function sets the state for us in case of an error */ |
| 1426 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1427 |
} |
| 1428 |
} |
| 1429 |
|
| 1430 |
/* now that all the metadata is written, we save the stream offset */ |
| 1431 |
if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &encoder->protected_->audio_offset, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) { /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */ |
| 1432 |
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 1433 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1434 |
} |
| 1435 |
|
| 1436 |
if(encoder->protected_->verify) |
| 1437 |
encoder->private_->verify.state_hint = ENCODER_IN_AUDIO; |
| 1438 |
|
| 1439 |
return FLAC__STREAM_ENCODER_INIT_STATUS_OK; |
| 1440 |
} |
| 1441 |
|
| 1442 |
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream( |
| 1443 |
FLAC__StreamEncoder *encoder, |
| 1444 |
FLAC__StreamEncoderWriteCallback write_callback, |
| 1445 |
FLAC__StreamEncoderSeekCallback seek_callback, |
| 1446 |
FLAC__StreamEncoderTellCallback tell_callback, |
| 1447 |
FLAC__StreamEncoderMetadataCallback metadata_callback, |
| 1448 |
void *client_data |
| 1449 |
) |
| 1450 |
{ |
| 1451 |
return init_stream_internal_( |
| 1452 |
encoder, |
| 1453 |
/*read_callback=*/0, |
| 1454 |
write_callback, |
| 1455 |
seek_callback, |
| 1456 |
tell_callback, |
| 1457 |
metadata_callback, |
| 1458 |
client_data, |
| 1459 |
/*is_ogg=*/false |
| 1460 |
); |
| 1461 |
} |
| 1462 |
|
| 1463 |
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream( |
| 1464 |
FLAC__StreamEncoder *encoder, |
| 1465 |
FLAC__StreamEncoderReadCallback read_callback, |
| 1466 |
FLAC__StreamEncoderWriteCallback write_callback, |
| 1467 |
FLAC__StreamEncoderSeekCallback seek_callback, |
| 1468 |
FLAC__StreamEncoderTellCallback tell_callback, |
| 1469 |
FLAC__StreamEncoderMetadataCallback metadata_callback, |
| 1470 |
void *client_data |
| 1471 |
) |
| 1472 |
{ |
| 1473 |
return init_stream_internal_( |
| 1474 |
encoder, |
| 1475 |
read_callback, |
| 1476 |
write_callback, |
| 1477 |
seek_callback, |
| 1478 |
tell_callback, |
| 1479 |
metadata_callback, |
| 1480 |
client_data, |
| 1481 |
/*is_ogg=*/true |
| 1482 |
); |
| 1483 |
} |
| 1484 |
|
| 1485 |
static FLAC__StreamEncoderInitStatus init_FILE_internal_( |
| 1486 |
FLAC__StreamEncoder *encoder, |
| 1487 |
FILE *file, |
| 1488 |
FLAC__StreamEncoderProgressCallback progress_callback, |
| 1489 |
void *client_data, |
| 1490 |
FLAC__bool is_ogg |
| 1491 |
) |
| 1492 |
{ |
| 1493 |
FLAC__StreamEncoderInitStatus init_status; |
| 1494 |
|
| 1495 |
FLAC__ASSERT(0 != encoder); |
| 1496 |
FLAC__ASSERT(0 != file); |
| 1497 |
|
| 1498 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1499 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED; |
| 1500 |
|
| 1501 |
/* double protection */ |
| 1502 |
if(file == 0) { |
| 1503 |
encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR; |
| 1504 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1505 |
} |
| 1506 |
|
| 1507 |
/* |
| 1508 |
* To make sure that our file does not go unclosed after an error, we |
| 1509 |
* must assign the FILE pointer before any further error can occur in |
| 1510 |
* this routine. |
| 1511 |
*/ |
| 1512 |
if(file == stdout) |
| 1513 |
file = get_binary_stdout_(); /* just to be safe */ |
| 1514 |
|
| 1515 |
#ifdef _WIN32 |
| 1516 |
/* |
| 1517 |
* Windows can suffer quite badly from disk fragmentation. This can be |
| 1518 |
* reduced significantly by setting the output buffer size to be 10MB. |
| 1519 |
*/ |
| 1520 |
if(GetFileType((HANDLE)_get_osfhandle(_fileno(file))) == FILE_TYPE_DISK) |
| 1521 |
setvbuf(file, NULL, _IOFBF, 10*1024*1024); |
| 1522 |
#endif |
| 1523 |
encoder->private_->file = file; |
| 1524 |
|
| 1525 |
encoder->private_->progress_callback = progress_callback; |
| 1526 |
encoder->private_->bytes_written = 0; |
| 1527 |
encoder->private_->samples_written = 0; |
| 1528 |
encoder->private_->frames_written = 0; |
| 1529 |
|
| 1530 |
init_status = init_stream_internal_( |
| 1531 |
encoder, |
| 1532 |
encoder->private_->file == stdout? 0 : is_ogg? file_read_callback_ : 0, |
| 1533 |
file_write_callback_, |
| 1534 |
encoder->private_->file == stdout? 0 : file_seek_callback_, |
| 1535 |
encoder->private_->file == stdout? 0 : file_tell_callback_, |
| 1536 |
/*metadata_callback=*/0, |
| 1537 |
client_data, |
| 1538 |
is_ogg |
| 1539 |
); |
| 1540 |
if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) { |
| 1541 |
/* the above function sets the state for us in case of an error */ |
| 1542 |
return init_status; |
| 1543 |
} |
| 1544 |
|
| 1545 |
{ |
| 1546 |
uint32_t blocksize = FLAC__stream_encoder_get_blocksize(encoder); |
| 1547 |
|
| 1548 |
FLAC__ASSERT(blocksize != 0); |
| 1549 |
encoder->private_->total_frames_estimate = (uint32_t)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize); |
| 1550 |
} |
| 1551 |
|
| 1552 |
return init_status; |
| 1553 |
} |
| 1554 |
|
| 1555 |
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE( |
| 1556 |
FLAC__StreamEncoder *encoder, |
| 1557 |
FILE *file, |
| 1558 |
FLAC__StreamEncoderProgressCallback progress_callback, |
| 1559 |
void *client_data |
| 1560 |
) |
| 1561 |
{ |
| 1562 |
return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/false); |
| 1563 |
} |
| 1564 |
|
| 1565 |
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE( |
| 1566 |
FLAC__StreamEncoder *encoder, |
| 1567 |
FILE *file, |
| 1568 |
FLAC__StreamEncoderProgressCallback progress_callback, |
| 1569 |
void *client_data |
| 1570 |
) |
| 1571 |
{ |
| 1572 |
return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/true); |
| 1573 |
} |
| 1574 |
|
| 1575 |
static FLAC__StreamEncoderInitStatus init_file_internal_( |
| 1576 |
FLAC__StreamEncoder *encoder, |
| 1577 |
const char *filename, |
| 1578 |
FLAC__StreamEncoderProgressCallback progress_callback, |
| 1579 |
void *client_data, |
| 1580 |
FLAC__bool is_ogg |
| 1581 |
) |
| 1582 |
{ |
| 1583 |
FILE *file; |
| 1584 |
|
| 1585 |
FLAC__ASSERT(0 != encoder); |
| 1586 |
|
| 1587 |
/* |
| 1588 |
* To make sure that our file does not go unclosed after an error, we |
| 1589 |
* have to do the same entrance checks here that are later performed |
| 1590 |
* in FLAC__stream_encoder_init_FILE() before the FILE* is assigned. |
| 1591 |
*/ |
| 1592 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1593 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED; |
| 1594 |
|
| 1595 |
file = filename? flac_fopen(filename, "w+b") : stdout; |
| 1596 |
|
| 1597 |
if(file == 0) { |
| 1598 |
encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR; |
| 1599 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1600 |
} |
| 1601 |
|
| 1602 |
return init_FILE_internal_(encoder, file, progress_callback, client_data, is_ogg); |
| 1603 |
} |
| 1604 |
|
| 1605 |
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file( |
| 1606 |
FLAC__StreamEncoder *encoder, |
| 1607 |
const char *filename, |
| 1608 |
FLAC__StreamEncoderProgressCallback progress_callback, |
| 1609 |
void *client_data |
| 1610 |
) |
| 1611 |
{ |
| 1612 |
return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/false); |
| 1613 |
} |
| 1614 |
|
| 1615 |
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file( |
| 1616 |
FLAC__StreamEncoder *encoder, |
| 1617 |
const char *filename, |
| 1618 |
FLAC__StreamEncoderProgressCallback progress_callback, |
| 1619 |
void *client_data |
| 1620 |
) |
| 1621 |
{ |
| 1622 |
return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/true); |
| 1623 |
} |
| 1624 |
|
| 1625 |
FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder) |
| 1626 |
{ |
| 1627 |
FLAC__bool error = false; |
| 1628 |
|
| 1629 |
if (encoder == NULL) |
| 1630 |
return false; |
| 1631 |
|
| 1632 |
FLAC__ASSERT(0 != encoder->private_); |
| 1633 |
FLAC__ASSERT(0 != encoder->protected_); |
| 1634 |
|
| 1635 |
if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED){ |
| 1636 |
if(encoder->protected_->metadata){ // True in case FLAC__stream_encoder_set_metadata was used but init failed |
| 1637 |
free(encoder->protected_->metadata); |
| 1638 |
encoder->protected_->metadata = 0; |
| 1639 |
encoder->protected_->num_metadata_blocks = 0; |
| 1640 |
} |
| 1641 |
if(0 != encoder->private_->file) { |
| 1642 |
if(encoder->private_->file != stdout) |
| 1643 |
fclose(encoder->private_->file); |
| 1644 |
encoder->private_->file = 0; |
| 1645 |
} |
| 1646 |
return true; |
| 1647 |
} |
| 1648 |
|
| 1649 |
if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) { |
| 1650 |
FLAC__bool ok = true; |
| 1651 |
/* first finish threads */ |
| 1652 |
if(encoder->protected_->num_threads > 1) { |
| 1653 |
#ifdef HAVE_PTHREAD |
| 1654 |
/* This is quite complicated, so here is an explanation on what is supposed to happen |
| 1655 |
* |
| 1656 |
* Thread no.0 and threadtask no.0 are reserved for non-threaded operation, so counting |
| 1657 |
* here starts at 1, which makes things slightly more complicated. |
| 1658 |
* |
| 1659 |
* If the file processed was very short compared to the requested number of threadtasks, |
| 1660 |
* not all threadtasks have been populated yet. Handling that is easy: threadtask no.1 needs |
| 1661 |
* to be processed first, monotonically increasing until the last populated threadtask is |
| 1662 |
* processed. This number is stored in encoder->private_->num_started_threadtasks |
| 1663 |
* |
| 1664 |
* If the file is longer, the next due frame chronologically might not be in threadtasks |
| 1665 |
* number 1, because the threadtasks work like a ringbuffer. To access this, the variable |
| 1666 |
* twrap starts counting at the next due frame, and the modulo operator (%) is used to |
| 1667 |
* "wrap" the number with the number of threadtasks. So, if the next due task is 3 |
| 1668 |
* and 4 tasks are started, twrap increases 3, 4, 5, 6, and t follows with values 3, 4, 1, 2. |
| 1669 |
*/ |
| 1670 |
uint32_t start, end, t, twrap; |
| 1671 |
if(encoder->private_->num_started_threadtasks < encoder->private_->num_threadtasks) { |
| 1672 |
start = 1; |
| 1673 |
end = encoder->private_->num_started_threadtasks; |
| 1674 |
} |
| 1675 |
else { |
| 1676 |
start = encoder->private_->next_thread; |
| 1677 |
end = encoder->private_->next_thread + encoder->private_->num_threadtasks - 1; |
| 1678 |
} |
| 1679 |
for(twrap = start; twrap < end; twrap++) { |
| 1680 |
FLAC__ASSERT(twrap > 0); |
| 1681 |
t = (twrap - 1) % (encoder->private_->num_threadtasks - 1) + 1; |
| 1682 |
/* Lock mutex, if task isn't done yet, wait for condition */ |
| 1683 |
pthread_mutex_lock(&encoder->private_->threadtask[t]->mutex_this_task); |
| 1684 |
while(!encoder->private_->threadtask[t]->task_done) |
| 1685 |
pthread_cond_wait(&encoder->private_->threadtask[t]->cond_task_done,&encoder->private_->threadtask[t]->mutex_this_task); |
| 1686 |
|
| 1687 |
if(!encoder->private_->threadtask[t]->returnvalue) |
| 1688 |
ok = false; |
| 1689 |
if(ok && !write_bitbuffer_(encoder, encoder->private_->threadtask[t], encoder->protected_->blocksize, 0)) |
| 1690 |
ok = false; |
| 1691 |
pthread_mutex_unlock(&encoder->private_->threadtask[t]->mutex_this_task); |
| 1692 |
} |
| 1693 |
/* Wait for MD5 calculation to finish */ |
| 1694 |
pthread_mutex_lock(&encoder->private_->mutex_work_queue); |
| 1695 |
while(encoder->private_->md5_active || encoder->private_->md5_fifo.tail > 0) { |
| 1696 |
pthread_cond_wait(&encoder->private_->cond_md5_emptied, &encoder->private_->mutex_work_queue); |
| 1697 |
} |
| 1698 |
pthread_mutex_unlock(&encoder->private_->mutex_work_queue); |
| 1699 |
#else |
| 1700 |
FLAC__ASSERT(0); |
| 1701 |
#endif |
| 1702 |
} |
| 1703 |
if(ok && encoder->private_->current_sample_number != 0) { |
| 1704 |
encoder->protected_->blocksize = encoder->private_->current_sample_number; |
| 1705 |
if(!resize_buffers_(encoder, encoder->protected_->blocksize)) { |
| 1706 |
/* the above function sets the state for us in case of an error */ |
| 1707 |
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1708 |
} |
| 1709 |
if(!process_frame_(encoder, /*is_last_block=*/true)) |
| 1710 |
error = true; |
| 1711 |
} |
| 1712 |
} |
| 1713 |
|
| 1714 |
if(encoder->protected_->num_threads > 1) { |
| 1715 |
#ifdef HAVE_PTHREAD |
| 1716 |
/* Properly finish all threads */ |
| 1717 |
uint32_t t; |
| 1718 |
pthread_mutex_lock(&encoder->private_->mutex_work_queue); |
| 1719 |
for(t = 1; t < encoder->private_->num_created_threads; t++) |
| 1720 |
encoder->private_->finish_work_threads = true; |
| 1721 |
pthread_cond_broadcast(&encoder->private_->cond_wake_up_thread); |
| 1722 |
pthread_cond_broadcast(&encoder->private_->cond_work_available); |
| 1723 |
pthread_mutex_unlock(&encoder->private_->mutex_work_queue); |
| 1724 |
|
| 1725 |
for(t = 1; t < encoder->private_->num_created_threads; t++) |
| 1726 |
pthread_join(encoder->private_->thread[t], NULL); |
| 1727 |
#else |
| 1728 |
FLAC__ASSERT(0); |
| 1729 |
#endif |
| 1730 |
} |
| 1731 |
|
| 1732 |
if(encoder->protected_->do_md5) |
| 1733 |
FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context); |
| 1734 |
|
| 1735 |
if(!encoder->private_->is_being_deleted) { |
| 1736 |
if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) { |
| 1737 |
if(encoder->private_->seek_callback) { |
| 1738 |
#if FLAC__HAS_OGG |
| 1739 |
if(encoder->private_->is_ogg) |
| 1740 |
update_ogg_metadata_(encoder); |
| 1741 |
else |
| 1742 |
#endif |
| 1743 |
update_metadata_(encoder); |
| 1744 |
|
| 1745 |
/* check if an error occurred while updating metadata */ |
| 1746 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_OK) |
| 1747 |
error = true; |
| 1748 |
} |
| 1749 |
if(encoder->private_->metadata_callback) |
| 1750 |
encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data); |
| 1751 |
} |
| 1752 |
|
| 1753 |
if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder && !FLAC__stream_decoder_finish(encoder->private_->verify.decoder)) { |
| 1754 |
if(!error) |
| 1755 |
encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA; |
| 1756 |
error = true; |
| 1757 |
} |
| 1758 |
} |
| 1759 |
|
| 1760 |
if(0 != encoder->private_->file) { |
| 1761 |
if(encoder->private_->file != stdout) |
| 1762 |
fclose(encoder->private_->file); |
| 1763 |
encoder->private_->file = 0; |
| 1764 |
} |
| 1765 |
|
| 1766 |
#if FLAC__HAS_OGG |
| 1767 |
if(encoder->private_->is_ogg) |
| 1768 |
FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect); |
| 1769 |
#endif |
| 1770 |
|
| 1771 |
free_(encoder); |
| 1772 |
set_defaults_(encoder); |
| 1773 |
|
| 1774 |
if(!error) |
| 1775 |
encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED; |
| 1776 |
|
| 1777 |
return !error; |
| 1778 |
} |
| 1779 |
|
| 1780 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value) |
| 1781 |
{ |
| 1782 |
FLAC__ASSERT(0 != encoder); |
| 1783 |
FLAC__ASSERT(0 != encoder->private_); |
| 1784 |
FLAC__ASSERT(0 != encoder->protected_); |
| 1785 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1786 |
return false; |
| 1787 |
#if FLAC__HAS_OGG |
| 1788 |
/* can't check encoder->private_->is_ogg since that's not set until init time */ |
| 1789 |
FLAC__ogg_encoder_aspect_set_serial_number(&encoder->protected_->ogg_encoder_aspect, value); |
| 1790 |
return true; |
| 1791 |
#else |
| 1792 |
(void)value; |
| 1793 |
return false; |
| 1794 |
#endif |
| 1795 |
} |
| 1796 |
|
| 1797 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 1798 |
{ |
| 1799 |
FLAC__ASSERT(0 != encoder); |
| 1800 |
FLAC__ASSERT(0 != encoder->private_); |
| 1801 |
FLAC__ASSERT(0 != encoder->protected_); |
| 1802 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1803 |
return false; |
| 1804 |
#ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING |
| 1805 |
encoder->protected_->verify = value; |
| 1806 |
#endif |
| 1807 |
return true; |
| 1808 |
} |
| 1809 |
|
| 1810 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 1811 |
{ |
| 1812 |
FLAC__ASSERT(0 != encoder); |
| 1813 |
FLAC__ASSERT(0 != encoder->private_); |
| 1814 |
FLAC__ASSERT(0 != encoder->protected_); |
| 1815 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1816 |
return false; |
| 1817 |
encoder->protected_->streamable_subset = value; |
| 1818 |
return true; |
| 1819 |
} |
| 1820 |
|
| 1821 |
/* |
| 1822 |
* The following routine was intended as debug routine and is not in the |
| 1823 |
* public headers, but SHOULD NOT CHANGE! It is known is is used in |
| 1824 |
* some non-audio projects needing every last bit of performance. |
| 1825 |
* See https://github.com/xiph/flac/issues/547 for details. These projects |
| 1826 |
* provide their own prototype, so changing the signature of this function |
| 1827 |
* would break building. |
| 1828 |
*/ |
| 1829 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_do_md5(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 1830 |
{ |
| 1831 |
FLAC__ASSERT(0 != encoder); |
| 1832 |
FLAC__ASSERT(0 != encoder->private_); |
| 1833 |
FLAC__ASSERT(0 != encoder->protected_); |
| 1834 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1835 |
return false; |
| 1836 |
encoder->protected_->do_md5 = value; |
| 1837 |
return true; |
| 1838 |
} |
| 1839 |
|
| 1840 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, uint32_t value) |
| 1841 |
{ |
| 1842 |
FLAC__ASSERT(0 != encoder); |
| 1843 |
FLAC__ASSERT(0 != encoder->private_); |
| 1844 |
FLAC__ASSERT(0 != encoder->protected_); |
| 1845 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1846 |
return false; |
| 1847 |
encoder->protected_->channels = value; |
| 1848 |
return true; |
| 1849 |
} |
| 1850 |
|
| 1851 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, uint32_t value) |
| 1852 |
{ |
| 1853 |
FLAC__ASSERT(0 != encoder); |
| 1854 |
FLAC__ASSERT(0 != encoder->private_); |
| 1855 |
FLAC__ASSERT(0 != encoder->protected_); |
| 1856 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1857 |
return false; |
| 1858 |
encoder->protected_->bits_per_sample = value; |
| 1859 |
return true; |
| 1860 |
} |
| 1861 |
|
| 1862 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, uint32_t value) |
| 1863 |
{ |
| 1864 |
FLAC__ASSERT(0 != encoder); |
| 1865 |
FLAC__ASSERT(0 != encoder->private_); |
| 1866 |
FLAC__ASSERT(0 != encoder->protected_); |
| 1867 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1868 |
return false; |
| 1869 |
encoder->protected_->sample_rate = value; |
| 1870 |
return true; |
| 1871 |
} |
| 1872 |
|
| 1873 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, uint32_t value) |
| 1874 |
{ |
| 1875 |
FLAC__bool ok = true; |
| 1876 |
FLAC__ASSERT(0 != encoder); |
| 1877 |
FLAC__ASSERT(0 != encoder->private_); |
| 1878 |
FLAC__ASSERT(0 != encoder->protected_); |
| 1879 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1880 |
return false; |
| 1881 |
if(value >= sizeof(compression_levels_)/sizeof(compression_levels_[0])) |
| 1882 |
value = sizeof(compression_levels_)/sizeof(compression_levels_[0]) - 1; |
| 1883 |
ok &= FLAC__stream_encoder_set_do_mid_side_stereo (encoder, compression_levels_[value].do_mid_side_stereo); |
| 1884 |
ok &= FLAC__stream_encoder_set_loose_mid_side_stereo (encoder, compression_levels_[value].loose_mid_side_stereo); |
| 1885 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 1886 |
#if 1 |
| 1887 |
ok &= FLAC__stream_encoder_set_apodization (encoder, compression_levels_[value].apodization); |
| 1888 |
#else |
| 1889 |
/* equivalent to -A tukey(0.5) */ |
| 1890 |
encoder->protected_->num_apodizations = 1; |
| 1891 |
encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY; |
| 1892 |
encoder->protected_->apodizations[0].parameters.tukey.p = 0.5; |
| 1893 |
#endif |
| 1894 |
#endif |
| 1895 |
ok &= FLAC__stream_encoder_set_max_lpc_order (encoder, compression_levels_[value].max_lpc_order); |
| 1896 |
ok &= FLAC__stream_encoder_set_qlp_coeff_precision (encoder, compression_levels_[value].qlp_coeff_precision); |
| 1897 |
ok &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search (encoder, compression_levels_[value].do_qlp_coeff_prec_search); |
| 1898 |
ok &= FLAC__stream_encoder_set_do_escape_coding (encoder, compression_levels_[value].do_escape_coding); |
| 1899 |
ok &= FLAC__stream_encoder_set_do_exhaustive_model_search (encoder, compression_levels_[value].do_exhaustive_model_search); |
| 1900 |
ok &= FLAC__stream_encoder_set_min_residual_partition_order(encoder, compression_levels_[value].min_residual_partition_order); |
| 1901 |
ok &= FLAC__stream_encoder_set_max_residual_partition_order(encoder, compression_levels_[value].max_residual_partition_order); |
| 1902 |
ok &= FLAC__stream_encoder_set_rice_parameter_search_dist (encoder, compression_levels_[value].rice_parameter_search_dist); |
| 1903 |
return ok; |
| 1904 |
} |
| 1905 |
|
| 1906 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, uint32_t value) |
| 1907 |
{ |
| 1908 |
FLAC__ASSERT(0 != encoder); |
| 1909 |
FLAC__ASSERT(0 != encoder->private_); |
| 1910 |
FLAC__ASSERT(0 != encoder->protected_); |
| 1911 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1912 |
return false; |
| 1913 |
encoder->protected_->blocksize = value; |
| 1914 |
return true; |
| 1915 |
} |
| 1916 |
|
| 1917 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 1918 |
{ |
| 1919 |
FLAC__ASSERT(0 != encoder); |
| 1920 |
FLAC__ASSERT(0 != encoder->private_); |
| 1921 |
FLAC__ASSERT(0 != encoder->protected_); |
| 1922 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1923 |
return false; |
| 1924 |
encoder->protected_->do_mid_side_stereo = value; |
| 1925 |
return true; |
| 1926 |
} |
| 1927 |
|
| 1928 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 1929 |
{ |
| 1930 |
FLAC__ASSERT(0 != encoder); |
| 1931 |
FLAC__ASSERT(0 != encoder->private_); |
| 1932 |
FLAC__ASSERT(0 != encoder->protected_); |
| 1933 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1934 |
return false; |
| 1935 |
encoder->protected_->loose_mid_side_stereo = value; |
| 1936 |
return true; |
| 1937 |
} |
| 1938 |
|
| 1939 |
/*@@@@add to tests*/ |
| 1940 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification) |
| 1941 |
{ |
| 1942 |
FLAC__ASSERT(0 != encoder); |
| 1943 |
FLAC__ASSERT(0 != encoder->private_); |
| 1944 |
FLAC__ASSERT(0 != encoder->protected_); |
| 1945 |
FLAC__ASSERT(0 != specification); |
| 1946 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1947 |
return false; |
| 1948 |
#ifdef FLAC__INTEGER_ONLY_LIBRARY |
| 1949 |
(void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */ |
| 1950 |
#else |
| 1951 |
encoder->protected_->num_apodizations = 0; |
| 1952 |
while(1) { |
| 1953 |
const char *s = strchr(specification, ';'); |
| 1954 |
const size_t n = s? (size_t)(s - specification) : strlen(specification); |
| 1955 |
if (n==8 && 0 == strncmp("bartlett" , specification, n)) |
| 1956 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT; |
| 1957 |
else if(n==13 && 0 == strncmp("bartlett_hann", specification, n)) |
| 1958 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN; |
| 1959 |
else if(n==8 && 0 == strncmp("blackman" , specification, n)) |
| 1960 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN; |
| 1961 |
else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n)) |
| 1962 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE; |
| 1963 |
else if(n==6 && 0 == strncmp("connes" , specification, n)) |
| 1964 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES; |
| 1965 |
else if(n==7 && 0 == strncmp("flattop" , specification, n)) |
| 1966 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP; |
| 1967 |
else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) { |
| 1968 |
FLAC__real stddev = (FLAC__real)strtod(specification+6, 0); |
| 1969 |
if (stddev > 0.0 && stddev <= 0.5) { |
| 1970 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev; |
| 1971 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS; |
| 1972 |
} |
| 1973 |
} |
| 1974 |
else if(n==7 && 0 == strncmp("hamming" , specification, n)) |
| 1975 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING; |
| 1976 |
else if(n==4 && 0 == strncmp("hann" , specification, n)) |
| 1977 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN; |
| 1978 |
else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n)) |
| 1979 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL; |
| 1980 |
else if(n==7 && 0 == strncmp("nuttall" , specification, n)) |
| 1981 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL; |
| 1982 |
else if(n==9 && 0 == strncmp("rectangle" , specification, n)) |
| 1983 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE; |
| 1984 |
else if(n==8 && 0 == strncmp("triangle" , specification, n)) |
| 1985 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE; |
| 1986 |
else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) { |
| 1987 |
FLAC__real p = (FLAC__real)strtod(specification+6, 0); |
| 1988 |
if (p >= 0.0 && p <= 1.0) { |
| 1989 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p; |
| 1990 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY; |
| 1991 |
} |
| 1992 |
} |
| 1993 |
else if(n>15 && 0 == strncmp("partial_tukey(", specification, 14)) { |
| 1994 |
FLAC__int32 tukey_parts = (FLAC__int32)strtod(specification+14, 0); |
| 1995 |
const char *si_1 = strchr(specification, '/'); |
| 1996 |
FLAC__real overlap = si_1?flac_min((FLAC__real)strtod(si_1+1, 0),0.99f):0.1f; |
| 1997 |
FLAC__real overlap_units = 1.0f/(1.0f - overlap) - 1.0f; |
| 1998 |
const char *si_2 = strchr((si_1?(si_1+1):specification), '/'); |
| 1999 |
FLAC__real tukey_p = si_2?(FLAC__real)strtod(si_2+1, 0):0.2f; |
| 2000 |
|
| 2001 |
if (tukey_parts <= 1) { |
| 2002 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = tukey_p; |
| 2003 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY; |
| 2004 |
}else if (encoder->protected_->num_apodizations + tukey_parts < 32){ |
| 2005 |
FLAC__int32 m; |
| 2006 |
for(m = 0; m < tukey_parts; m++){ |
| 2007 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.multiple_tukey.p = tukey_p; |
| 2008 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.multiple_tukey.start = m/(tukey_parts+overlap_units); |
| 2009 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.multiple_tukey.end = (m+1+overlap_units)/(tukey_parts+overlap_units); |
| 2010 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_PARTIAL_TUKEY; |
| 2011 |
} |
| 2012 |
} |
| 2013 |
} |
| 2014 |
else if(n>16 && 0 == strncmp("punchout_tukey(", specification, 15)) { |
| 2015 |
FLAC__int32 tukey_parts = (FLAC__int32)strtod(specification+15, 0); |
| 2016 |
const char *si_1 = strchr(specification, '/'); |
| 2017 |
FLAC__real overlap = si_1?flac_min((FLAC__real)strtod(si_1+1, 0),0.99f):0.2f; |
| 2018 |
FLAC__real overlap_units = 1.0f/(1.0f - overlap) - 1.0f; |
| 2019 |
const char *si_2 = strchr((si_1?(si_1+1):specification), '/'); |
| 2020 |
FLAC__real tukey_p = si_2?(FLAC__real)strtod(si_2+1, 0):0.2f; |
| 2021 |
|
| 2022 |
if (tukey_parts <= 1) { |
| 2023 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = tukey_p; |
| 2024 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY; |
| 2025 |
}else if (encoder->protected_->num_apodizations + tukey_parts < 32){ |
| 2026 |
FLAC__int32 m; |
| 2027 |
for(m = 0; m < tukey_parts; m++){ |
| 2028 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.multiple_tukey.p = tukey_p; |
| 2029 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.multiple_tukey.start = m/(tukey_parts+overlap_units); |
| 2030 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.multiple_tukey.end = (m+1+overlap_units)/(tukey_parts+overlap_units); |
| 2031 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_PUNCHOUT_TUKEY; |
| 2032 |
} |
| 2033 |
} |
| 2034 |
} |
| 2035 |
else if(n>17 && 0 == strncmp("subdivide_tukey(", specification, 16)){ |
| 2036 |
FLAC__int32 parts = (FLAC__int32)strtod(specification+16, 0); |
| 2037 |
if(parts > 1){ |
| 2038 |
const char *si_1 = strchr(specification, '/'); |
| 2039 |
FLAC__real p = si_1?(FLAC__real)strtod(si_1+1, 0):5e-1; |
| 2040 |
if(p > 1) |
| 2041 |
p = 1; |
| 2042 |
else if(p < 0) |
| 2043 |
p = 0; |
| 2044 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.subdivide_tukey.parts = parts; |
| 2045 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.subdivide_tukey.p = p/parts; |
| 2046 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_SUBDIVIDE_TUKEY; |
| 2047 |
} |
| 2048 |
} |
| 2049 |
else if(n==5 && 0 == strncmp("welch" , specification, n)) |
| 2050 |
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH; |
| 2051 |
if (encoder->protected_->num_apodizations == 32) |
| 2052 |
break; |
| 2053 |
if (s) |
| 2054 |
specification = s+1; |
| 2055 |
else |
| 2056 |
break; |
| 2057 |
} |
| 2058 |
if(encoder->protected_->num_apodizations == 0) { |
| 2059 |
encoder->protected_->num_apodizations = 1; |
| 2060 |
encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY; |
| 2061 |
encoder->protected_->apodizations[0].parameters.tukey.p = 0.5; |
| 2062 |
} |
| 2063 |
#endif |
| 2064 |
return true; |
| 2065 |
} |
| 2066 |
|
| 2067 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, uint32_t value) |
| 2068 |
{ |
| 2069 |
FLAC__ASSERT(0 != encoder); |
| 2070 |
FLAC__ASSERT(0 != encoder->private_); |
| 2071 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2072 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 2073 |
return false; |
| 2074 |
encoder->protected_->max_lpc_order = value; |
| 2075 |
return true; |
| 2076 |
} |
| 2077 |
|
| 2078 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, uint32_t value) |
| 2079 |
{ |
| 2080 |
FLAC__ASSERT(0 != encoder); |
| 2081 |
FLAC__ASSERT(0 != encoder->private_); |
| 2082 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2083 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 2084 |
return false; |
| 2085 |
encoder->protected_->qlp_coeff_precision = value; |
| 2086 |
return true; |
| 2087 |
} |
| 2088 |
|
| 2089 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 2090 |
{ |
| 2091 |
FLAC__ASSERT(0 != encoder); |
| 2092 |
FLAC__ASSERT(0 != encoder->private_); |
| 2093 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2094 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 2095 |
return false; |
| 2096 |
encoder->protected_->do_qlp_coeff_prec_search = value; |
| 2097 |
return true; |
| 2098 |
} |
| 2099 |
|
| 2100 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 2101 |
{ |
| 2102 |
FLAC__ASSERT(0 != encoder); |
| 2103 |
FLAC__ASSERT(0 != encoder->private_); |
| 2104 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2105 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 2106 |
return false; |
| 2107 |
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 2108 |
/* was deprecated since FLAC 1.0.4 (24-Sep-2002), but is needed for |
| 2109 |
* full spec coverage, so this should be reenabled at some point. |
| 2110 |
* For now only enable while fuzzing */ |
| 2111 |
encoder->protected_->do_escape_coding = value; |
| 2112 |
#else |
| 2113 |
(void)value; |
| 2114 |
#endif |
| 2115 |
return true; |
| 2116 |
} |
| 2117 |
|
| 2118 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 2119 |
{ |
| 2120 |
FLAC__ASSERT(0 != encoder); |
| 2121 |
FLAC__ASSERT(0 != encoder->private_); |
| 2122 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2123 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 2124 |
return false; |
| 2125 |
encoder->protected_->do_exhaustive_model_search = value; |
| 2126 |
return true; |
| 2127 |
} |
| 2128 |
|
| 2129 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, uint32_t value) |
| 2130 |
{ |
| 2131 |
FLAC__ASSERT(0 != encoder); |
| 2132 |
FLAC__ASSERT(0 != encoder->private_); |
| 2133 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2134 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 2135 |
return false; |
| 2136 |
encoder->protected_->min_residual_partition_order = value; |
| 2137 |
return true; |
| 2138 |
} |
| 2139 |
|
| 2140 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, uint32_t value) |
| 2141 |
{ |
| 2142 |
FLAC__ASSERT(0 != encoder); |
| 2143 |
FLAC__ASSERT(0 != encoder->private_); |
| 2144 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2145 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 2146 |
return false; |
| 2147 |
encoder->protected_->max_residual_partition_order = value; |
| 2148 |
return true; |
| 2149 |
} |
| 2150 |
|
| 2151 |
FLAC_API uint32_t FLAC__stream_encoder_set_num_threads(FLAC__StreamEncoder *encoder, uint32_t value) |
| 2152 |
{ |
| 2153 |
#ifdef HAVE_PTHREAD |
| 2154 |
FLAC__ASSERT(0 != encoder); |
| 2155 |
FLAC__ASSERT(0 != encoder->private_); |
| 2156 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2157 |
|
| 2158 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 2159 |
return FLAC__STREAM_ENCODER_SET_NUM_THREADS_ALREADY_INITIALIZED; |
| 2160 |
if(value > FLAC__STREAM_ENCODER_MAX_THREADS) |
| 2161 |
return FLAC__STREAM_ENCODER_SET_NUM_THREADS_TOO_MANY_THREADS; |
| 2162 |
if(value == 0) |
| 2163 |
encoder->protected_->num_threads = 1; |
| 2164 |
else |
| 2165 |
encoder->protected_->num_threads = value; |
| 2166 |
return FLAC__STREAM_ENCODER_SET_NUM_THREADS_OK; |
| 2167 |
#else |
| 2168 |
(void)encoder; |
| 2169 |
(void)value; |
| 2170 |
return FLAC__STREAM_ENCODER_SET_NUM_THREADS_NOT_COMPILED_WITH_MULTITHREADING_ENABLED; |
| 2171 |
#endif |
| 2172 |
} |
| 2173 |
|
| 2174 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, uint32_t value) |
| 2175 |
{ |
| 2176 |
FLAC__ASSERT(0 != encoder); |
| 2177 |
FLAC__ASSERT(0 != encoder->private_); |
| 2178 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2179 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 2180 |
return false; |
| 2181 |
#if 0 |
| 2182 |
/*@@@ deprecated: */ |
| 2183 |
encoder->protected_->rice_parameter_search_dist = value; |
| 2184 |
#else |
| 2185 |
(void)value; |
| 2186 |
#endif |
| 2187 |
return true; |
| 2188 |
} |
| 2189 |
|
| 2190 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value) |
| 2191 |
{ |
| 2192 |
FLAC__ASSERT(0 != encoder); |
| 2193 |
FLAC__ASSERT(0 != encoder->private_); |
| 2194 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2195 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 2196 |
return false; |
| 2197 |
value = flac_min(value, (FLAC__U64L(1) << FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN) - 1); |
| 2198 |
encoder->protected_->total_samples_estimate = value; |
| 2199 |
return true; |
| 2200 |
} |
| 2201 |
|
| 2202 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, uint32_t num_blocks) |
| 2203 |
{ |
| 2204 |
FLAC__ASSERT(0 != encoder); |
| 2205 |
FLAC__ASSERT(0 != encoder->private_); |
| 2206 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2207 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 2208 |
return false; |
| 2209 |
if(0 == metadata) |
| 2210 |
num_blocks = 0; |
| 2211 |
if(0 == num_blocks) |
| 2212 |
metadata = 0; |
| 2213 |
/* realloc() does not do exactly what we want so... */ |
| 2214 |
if(encoder->protected_->metadata) { |
| 2215 |
free(encoder->protected_->metadata); |
| 2216 |
encoder->protected_->metadata = 0; |
| 2217 |
encoder->protected_->num_metadata_blocks = 0; |
| 2218 |
} |
| 2219 |
if(num_blocks) { |
| 2220 |
FLAC__StreamMetadata **m; |
| 2221 |
if(0 == (m = safe_malloc_mul_2op_p(sizeof(m[0]), /*times*/num_blocks))) |
| 2222 |
return false; |
| 2223 |
memcpy(m, metadata, sizeof(m[0]) * num_blocks); |
| 2224 |
encoder->protected_->metadata = m; |
| 2225 |
encoder->protected_->num_metadata_blocks = num_blocks; |
| 2226 |
} |
| 2227 |
#if FLAC__HAS_OGG |
| 2228 |
if(!FLAC__ogg_encoder_aspect_set_num_metadata(&encoder->protected_->ogg_encoder_aspect, num_blocks)) |
| 2229 |
return false; |
| 2230 |
#endif |
| 2231 |
return true; |
| 2232 |
} |
| 2233 |
|
| 2234 |
FLAC_API FLAC__bool FLAC__stream_encoder_set_limit_min_bitrate(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 2235 |
{ |
| 2236 |
FLAC__ASSERT(0 != encoder); |
| 2237 |
FLAC__ASSERT(0 != encoder->private_); |
| 2238 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2239 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 2240 |
return false; |
| 2241 |
encoder->protected_->limit_min_bitrate = value; |
| 2242 |
return true; |
| 2243 |
} |
| 2244 |
|
| 2245 |
/* |
| 2246 |
* These four functions are not static, but not publicly exposed in |
| 2247 |
* include/FLAC/ either. They are used by the test suite and in fuzzing |
| 2248 |
*/ |
| 2249 |
FLAC_API FLAC__bool FLAC__stream_encoder_disable_instruction_set(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 2250 |
{ |
| 2251 |
FLAC__ASSERT(0 != encoder); |
| 2252 |
FLAC__ASSERT(0 != encoder->private_); |
| 2253 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2254 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 2255 |
return false; |
| 2256 |
encoder->private_->disable_mmx = value & 1; |
| 2257 |
encoder->private_->disable_sse2 = value & 2; |
| 2258 |
encoder->private_->disable_ssse3 = value & 4; |
| 2259 |
encoder->private_->disable_sse41 = value & 8; |
| 2260 |
encoder->private_->disable_avx2 = value & 16; |
| 2261 |
encoder->private_->disable_fma = value & 32; |
| 2262 |
encoder->private_->disable_sse42 = value & 64; |
| 2263 |
return true; |
| 2264 |
} |
| 2265 |
|
| 2266 |
FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 2267 |
{ |
| 2268 |
FLAC__ASSERT(0 != encoder); |
| 2269 |
FLAC__ASSERT(0 != encoder->private_); |
| 2270 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2271 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 2272 |
return false; |
| 2273 |
encoder->private_->disable_constant_subframes = value; |
| 2274 |
return true; |
| 2275 |
} |
| 2276 |
|
| 2277 |
FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 2278 |
{ |
| 2279 |
FLAC__ASSERT(0 != encoder); |
| 2280 |
FLAC__ASSERT(0 != encoder->private_); |
| 2281 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2282 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 2283 |
return false; |
| 2284 |
encoder->private_->disable_fixed_subframes = value; |
| 2285 |
return true; |
| 2286 |
} |
| 2287 |
|
| 2288 |
FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 2289 |
{ |
| 2290 |
FLAC__ASSERT(0 != encoder); |
| 2291 |
FLAC__ASSERT(0 != encoder->private_); |
| 2292 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2293 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 2294 |
return false; |
| 2295 |
encoder->private_->disable_verbatim_subframes = value; |
| 2296 |
return true; |
| 2297 |
} |
| 2298 |
|
| 2299 |
FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder) |
| 2300 |
{ |
| 2301 |
FLAC__ASSERT(0 != encoder); |
| 2302 |
FLAC__ASSERT(0 != encoder->private_); |
| 2303 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2304 |
return encoder->protected_->state; |
| 2305 |
} |
| 2306 |
|
| 2307 |
FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder) |
| 2308 |
{ |
| 2309 |
FLAC__ASSERT(0 != encoder); |
| 2310 |
FLAC__ASSERT(0 != encoder->private_); |
| 2311 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2312 |
if(encoder->protected_->verify) |
| 2313 |
if(encoder->private_->verify.decoder == NULL) |
| 2314 |
return FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 2315 |
else |
| 2316 |
return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder); |
| 2317 |
else |
| 2318 |
return FLAC__STREAM_DECODER_UNINITIALIZED; |
| 2319 |
} |
| 2320 |
|
| 2321 |
FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder) |
| 2322 |
{ |
| 2323 |
FLAC__ASSERT(0 != encoder); |
| 2324 |
FLAC__ASSERT(0 != encoder->private_); |
| 2325 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2326 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) |
| 2327 |
return FLAC__StreamEncoderStateString[encoder->protected_->state]; |
| 2328 |
else if(!encoder->private_->verify.decoder) |
| 2329 |
return FLAC__StreamEncoderStateString[FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR]; |
| 2330 |
else |
| 2331 |
return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder); |
| 2332 |
} |
| 2333 |
|
| 2334 |
FLAC_API void FLAC__stream_encoder_get_verify_decoder_error_stats(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_sample, uint32_t *frame_number, uint32_t *channel, uint32_t *sample, FLAC__int32 *expected, FLAC__int32 *got) |
| 2335 |
{ |
| 2336 |
FLAC__ASSERT(0 != encoder); |
| 2337 |
FLAC__ASSERT(0 != encoder->private_); |
| 2338 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2339 |
if(0 != absolute_sample) |
| 2340 |
*absolute_sample = encoder->private_->verify.error_stats.absolute_sample; |
| 2341 |
if(0 != frame_number) |
| 2342 |
*frame_number = encoder->private_->verify.error_stats.frame_number; |
| 2343 |
if(0 != channel) |
| 2344 |
*channel = encoder->private_->verify.error_stats.channel; |
| 2345 |
if(0 != sample) |
| 2346 |
*sample = encoder->private_->verify.error_stats.sample; |
| 2347 |
if(0 != expected) |
| 2348 |
*expected = encoder->private_->verify.error_stats.expected; |
| 2349 |
if(0 != got) |
| 2350 |
*got = encoder->private_->verify.error_stats.got; |
| 2351 |
} |
| 2352 |
|
| 2353 |
FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder) |
| 2354 |
{ |
| 2355 |
FLAC__ASSERT(0 != encoder); |
| 2356 |
FLAC__ASSERT(0 != encoder->private_); |
| 2357 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2358 |
return encoder->protected_->verify; |
| 2359 |
} |
| 2360 |
|
| 2361 |
FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder) |
| 2362 |
{ |
| 2363 |
FLAC__ASSERT(0 != encoder); |
| 2364 |
FLAC__ASSERT(0 != encoder->private_); |
| 2365 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2366 |
return encoder->protected_->streamable_subset; |
| 2367 |
} |
| 2368 |
|
| 2369 |
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_md5(const FLAC__StreamEncoder *encoder) |
| 2370 |
{ |
| 2371 |
FLAC__ASSERT(0 != encoder); |
| 2372 |
FLAC__ASSERT(0 != encoder->private_); |
| 2373 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2374 |
return encoder->protected_->do_md5; |
| 2375 |
} |
| 2376 |
|
| 2377 |
FLAC_API uint32_t FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder) |
| 2378 |
{ |
| 2379 |
FLAC__ASSERT(0 != encoder); |
| 2380 |
FLAC__ASSERT(0 != encoder->private_); |
| 2381 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2382 |
return encoder->protected_->channels; |
| 2383 |
} |
| 2384 |
|
| 2385 |
FLAC_API uint32_t FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder) |
| 2386 |
{ |
| 2387 |
FLAC__ASSERT(0 != encoder); |
| 2388 |
FLAC__ASSERT(0 != encoder->private_); |
| 2389 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2390 |
return encoder->protected_->bits_per_sample; |
| 2391 |
} |
| 2392 |
|
| 2393 |
FLAC_API uint32_t FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder) |
| 2394 |
{ |
| 2395 |
FLAC__ASSERT(0 != encoder); |
| 2396 |
FLAC__ASSERT(0 != encoder->private_); |
| 2397 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2398 |
return encoder->protected_->sample_rate; |
| 2399 |
} |
| 2400 |
|
| 2401 |
FLAC_API uint32_t FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder) |
| 2402 |
{ |
| 2403 |
FLAC__ASSERT(0 != encoder); |
| 2404 |
FLAC__ASSERT(0 != encoder->private_); |
| 2405 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2406 |
return encoder->protected_->blocksize; |
| 2407 |
} |
| 2408 |
|
| 2409 |
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder) |
| 2410 |
{ |
| 2411 |
FLAC__ASSERT(0 != encoder); |
| 2412 |
FLAC__ASSERT(0 != encoder->private_); |
| 2413 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2414 |
return encoder->protected_->do_mid_side_stereo; |
| 2415 |
} |
| 2416 |
|
| 2417 |
FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder) |
| 2418 |
{ |
| 2419 |
FLAC__ASSERT(0 != encoder); |
| 2420 |
FLAC__ASSERT(0 != encoder->private_); |
| 2421 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2422 |
return encoder->protected_->loose_mid_side_stereo; |
| 2423 |
} |
| 2424 |
|
| 2425 |
FLAC_API uint32_t FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder) |
| 2426 |
{ |
| 2427 |
FLAC__ASSERT(0 != encoder); |
| 2428 |
FLAC__ASSERT(0 != encoder->private_); |
| 2429 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2430 |
return encoder->protected_->max_lpc_order; |
| 2431 |
} |
| 2432 |
|
| 2433 |
FLAC_API uint32_t FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder) |
| 2434 |
{ |
| 2435 |
FLAC__ASSERT(0 != encoder); |
| 2436 |
FLAC__ASSERT(0 != encoder->private_); |
| 2437 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2438 |
return encoder->protected_->qlp_coeff_precision; |
| 2439 |
} |
| 2440 |
|
| 2441 |
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder) |
| 2442 |
{ |
| 2443 |
FLAC__ASSERT(0 != encoder); |
| 2444 |
FLAC__ASSERT(0 != encoder->private_); |
| 2445 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2446 |
return encoder->protected_->do_qlp_coeff_prec_search; |
| 2447 |
} |
| 2448 |
|
| 2449 |
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder) |
| 2450 |
{ |
| 2451 |
FLAC__ASSERT(0 != encoder); |
| 2452 |
FLAC__ASSERT(0 != encoder->private_); |
| 2453 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2454 |
return encoder->protected_->do_escape_coding; |
| 2455 |
} |
| 2456 |
|
| 2457 |
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder) |
| 2458 |
{ |
| 2459 |
FLAC__ASSERT(0 != encoder); |
| 2460 |
FLAC__ASSERT(0 != encoder->private_); |
| 2461 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2462 |
return encoder->protected_->do_exhaustive_model_search; |
| 2463 |
} |
| 2464 |
|
| 2465 |
FLAC_API uint32_t FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder) |
| 2466 |
{ |
| 2467 |
FLAC__ASSERT(0 != encoder); |
| 2468 |
FLAC__ASSERT(0 != encoder->private_); |
| 2469 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2470 |
return encoder->protected_->min_residual_partition_order; |
| 2471 |
} |
| 2472 |
|
| 2473 |
FLAC_API uint32_t FLAC__stream_encoder_get_num_threads(const FLAC__StreamEncoder *encoder) |
| 2474 |
{ |
| 2475 |
FLAC__ASSERT(0 != encoder); |
| 2476 |
FLAC__ASSERT(0 != encoder->private_); |
| 2477 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2478 |
return encoder->protected_->num_threads; |
| 2479 |
} |
| 2480 |
|
| 2481 |
FLAC_API uint32_t FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder) |
| 2482 |
{ |
| 2483 |
FLAC__ASSERT(0 != encoder); |
| 2484 |
FLAC__ASSERT(0 != encoder->private_); |
| 2485 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2486 |
return encoder->protected_->max_residual_partition_order; |
| 2487 |
} |
| 2488 |
|
| 2489 |
FLAC_API uint32_t FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder) |
| 2490 |
{ |
| 2491 |
FLAC__ASSERT(0 != encoder); |
| 2492 |
FLAC__ASSERT(0 != encoder->private_); |
| 2493 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2494 |
return encoder->protected_->rice_parameter_search_dist; |
| 2495 |
} |
| 2496 |
|
| 2497 |
FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder) |
| 2498 |
{ |
| 2499 |
FLAC__ASSERT(0 != encoder); |
| 2500 |
FLAC__ASSERT(0 != encoder->private_); |
| 2501 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2502 |
return encoder->protected_->total_samples_estimate; |
| 2503 |
} |
| 2504 |
|
| 2505 |
FLAC_API FLAC__bool FLAC__stream_encoder_get_limit_min_bitrate(const FLAC__StreamEncoder *encoder) |
| 2506 |
{ |
| 2507 |
FLAC__ASSERT(0 != encoder); |
| 2508 |
FLAC__ASSERT(0 != encoder->private_); |
| 2509 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2510 |
return encoder->protected_->limit_min_bitrate; |
| 2511 |
} |
| 2512 |
|
| 2513 |
FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], uint32_t samples) |
| 2514 |
{ |
| 2515 |
uint32_t i, j = 0, k = 0, channel; |
| 2516 |
const uint32_t channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize; |
| 2517 |
const FLAC__int32 sample_max = INT32_MAX >> (32 - encoder->protected_->bits_per_sample); |
| 2518 |
const FLAC__int32 sample_min = INT32_MIN >> (32 - encoder->protected_->bits_per_sample); |
| 2519 |
|
| 2520 |
FLAC__ASSERT(0 != encoder); |
| 2521 |
FLAC__ASSERT(0 != encoder->private_); |
| 2522 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2523 |
|
| 2524 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_OK) |
| 2525 |
return false; |
| 2526 |
|
| 2527 |
do { |
| 2528 |
const uint32_t n = flac_min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j); |
| 2529 |
|
| 2530 |
if(encoder->protected_->verify) |
| 2531 |
append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, n); |
| 2532 |
|
| 2533 |
for(channel = 0; channel < channels; channel++) { |
| 2534 |
if (buffer[channel] == NULL) { |
| 2535 |
return false; |
| 2536 |
} |
| 2537 |
for(i = encoder->private_->current_sample_number, k = j; i <= blocksize && k < samples; i++, k++) { |
| 2538 |
if(buffer[channel][k] < sample_min || buffer[channel][k] > sample_max){ |
| 2539 |
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 2540 |
return false; |
| 2541 |
} |
| 2542 |
} |
| 2543 |
memcpy(&encoder->private_->threadtask[0]->integer_signal[channel][encoder->private_->current_sample_number], &buffer[channel][j], sizeof(buffer[channel][0]) * n); |
| 2544 |
} |
| 2545 |
j += n; |
| 2546 |
encoder->private_->current_sample_number += n; |
| 2547 |
|
| 2548 |
/* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */ |
| 2549 |
if(encoder->private_->current_sample_number > blocksize) { |
| 2550 |
FLAC__ASSERT(encoder->private_->current_sample_number == blocksize+OVERREAD_); |
| 2551 |
FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */ |
| 2552 |
if(!process_frame_(encoder, /*is_last_block=*/false)) |
| 2553 |
return false; |
| 2554 |
/* move unprocessed overread samples to beginnings of arrays */ |
| 2555 |
for(channel = 0; channel < channels; channel++) |
| 2556 |
encoder->private_->threadtask[0]->integer_signal[channel][0] = encoder->private_->threadtask[0]->integer_signal[channel][blocksize]; |
| 2557 |
encoder->private_->current_sample_number = 1; |
| 2558 |
} |
| 2559 |
} while(j < samples); |
| 2560 |
|
| 2561 |
return true; |
| 2562 |
} |
| 2563 |
|
| 2564 |
FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], uint32_t samples) |
| 2565 |
{ |
| 2566 |
uint32_t i, j, k, channel; |
| 2567 |
const uint32_t channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize; |
| 2568 |
const FLAC__int32 sample_max = INT32_MAX >> (32 - encoder->protected_->bits_per_sample); |
| 2569 |
const FLAC__int32 sample_min = INT32_MIN >> (32 - encoder->protected_->bits_per_sample); |
| 2570 |
|
| 2571 |
FLAC__ASSERT(0 != encoder); |
| 2572 |
FLAC__ASSERT(0 != encoder->private_); |
| 2573 |
FLAC__ASSERT(0 != encoder->protected_); |
| 2574 |
|
| 2575 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_OK) |
| 2576 |
return false; |
| 2577 |
|
| 2578 |
j = k = 0; |
| 2579 |
do { |
| 2580 |
if(encoder->protected_->verify) |
| 2581 |
append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, flac_min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j)); |
| 2582 |
|
| 2583 |
/* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */ |
| 2584 |
for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) { |
| 2585 |
for(channel = 0; channel < channels; channel++){ |
| 2586 |
if(buffer[k] < sample_min || buffer[k] > sample_max){ |
| 2587 |
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 2588 |
return false; |
| 2589 |
} |
| 2590 |
encoder->private_->threadtask[0]->integer_signal[channel][i] = buffer[k++]; |
| 2591 |
} |
| 2592 |
} |
| 2593 |
encoder->private_->current_sample_number = i; |
| 2594 |
/* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */ |
| 2595 |
if(i > blocksize) { |
| 2596 |
if(!process_frame_(encoder, /*is_last_block=*/false)) |
| 2597 |
return false; |
| 2598 |
/* move unprocessed overread samples to beginnings of arrays */ |
| 2599 |
FLAC__ASSERT(i == blocksize+OVERREAD_); |
| 2600 |
FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */ |
| 2601 |
for(channel = 0; channel < channels; channel++) |
| 2602 |
encoder->private_->threadtask[0]->integer_signal[channel][0] = encoder->private_->threadtask[0]->integer_signal[channel][blocksize]; |
| 2603 |
encoder->private_->current_sample_number = 1; |
| 2604 |
} |
| 2605 |
} while(j < samples); |
| 2606 |
|
| 2607 |
return true; |
| 2608 |
} |
| 2609 |
|
| 2610 |
/*********************************************************************** |
| 2611 |
* |
| 2612 |
* Private class methods |
| 2613 |
* |
| 2614 |
***********************************************************************/ |
| 2615 |
|
| 2616 |
void set_defaults_(FLAC__StreamEncoder *encoder) |
| 2617 |
{ |
| 2618 |
FLAC__ASSERT(0 != encoder); |
| 2619 |
|
| 2620 |
#ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING |
| 2621 |
encoder->protected_->verify = true; |
| 2622 |
#else |
| 2623 |
encoder->protected_->verify = false; |
| 2624 |
#endif |
| 2625 |
encoder->protected_->streamable_subset = true; |
| 2626 |
encoder->protected_->do_md5 = true; |
| 2627 |
encoder->protected_->do_mid_side_stereo = false; |
| 2628 |
encoder->protected_->loose_mid_side_stereo = false; |
| 2629 |
encoder->protected_->channels = 2; |
| 2630 |
encoder->protected_->bits_per_sample = 16; |
| 2631 |
encoder->protected_->sample_rate = 44100; |
| 2632 |
encoder->protected_->blocksize = 0; |
| 2633 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 2634 |
encoder->protected_->num_apodizations = 1; |
| 2635 |
encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY; |
| 2636 |
encoder->protected_->apodizations[0].parameters.tukey.p = 0.5; |
| 2637 |
#endif |
| 2638 |
encoder->protected_->max_lpc_order = 0; |
| 2639 |
encoder->protected_->qlp_coeff_precision = 0; |
| 2640 |
encoder->protected_->do_qlp_coeff_prec_search = false; |
| 2641 |
encoder->protected_->do_exhaustive_model_search = false; |
| 2642 |
encoder->protected_->do_escape_coding = false; |
| 2643 |
encoder->protected_->min_residual_partition_order = 0; |
| 2644 |
encoder->protected_->max_residual_partition_order = 0; |
| 2645 |
encoder->protected_->rice_parameter_search_dist = 0; |
| 2646 |
encoder->protected_->total_samples_estimate = 0; |
| 2647 |
encoder->protected_->limit_min_bitrate = false; |
| 2648 |
encoder->protected_->metadata = 0; |
| 2649 |
encoder->protected_->num_metadata_blocks = 0; |
| 2650 |
encoder->protected_->num_threads = 1; |
| 2651 |
|
| 2652 |
encoder->private_->seek_table = 0; |
| 2653 |
encoder->private_->disable_mmx = false; |
| 2654 |
encoder->private_->disable_sse2 = false; |
| 2655 |
encoder->private_->disable_ssse3 = false; |
| 2656 |
encoder->private_->disable_sse41 = false; |
| 2657 |
encoder->private_->disable_sse42 = false; |
| 2658 |
encoder->private_->disable_avx2 = false; |
| 2659 |
encoder->private_->disable_constant_subframes = false; |
| 2660 |
encoder->private_->disable_fixed_subframes = false; |
| 2661 |
encoder->private_->disable_verbatim_subframes = false; |
| 2662 |
encoder->private_->is_ogg = false; |
| 2663 |
encoder->private_->read_callback = 0; |
| 2664 |
encoder->private_->write_callback = 0; |
| 2665 |
encoder->private_->seek_callback = 0; |
| 2666 |
encoder->private_->tell_callback = 0; |
| 2667 |
encoder->private_->metadata_callback = 0; |
| 2668 |
encoder->private_->progress_callback = 0; |
| 2669 |
encoder->private_->client_data = 0; |
| 2670 |
encoder->private_->num_threadtasks = 1; |
| 2671 |
#ifdef HAVE_PTHREAD |
| 2672 |
encoder->private_->num_created_threads = 1; |
| 2673 |
encoder->private_->next_thread = 1; |
| 2674 |
encoder->private_->num_running_threads = 1; |
| 2675 |
encoder->private_->num_started_threadtasks = 1; |
| 2676 |
encoder->private_->num_available_threadtasks = 0; |
| 2677 |
encoder->private_->overcommitted_indicator = 0; |
| 2678 |
encoder->private_->next_threadtask = 1; |
| 2679 |
encoder->private_->md5_active = false; |
| 2680 |
encoder->private_->finish_work_threads = false; |
| 2681 |
#endif |
| 2682 |
|
| 2683 |
#if FLAC__HAS_OGG |
| 2684 |
FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect); |
| 2685 |
#endif |
| 2686 |
|
| 2687 |
FLAC__stream_encoder_set_compression_level(encoder, 5); |
| 2688 |
} |
| 2689 |
|
| 2690 |
void free_(FLAC__StreamEncoder *encoder) |
| 2691 |
{ |
| 2692 |
uint32_t i, t, channel; |
| 2693 |
|
| 2694 |
FLAC__ASSERT(0 != encoder); |
| 2695 |
if(encoder->protected_->metadata) { |
| 2696 |
free(encoder->protected_->metadata); |
| 2697 |
encoder->protected_->metadata = 0; |
| 2698 |
encoder->protected_->num_metadata_blocks = 0; |
| 2699 |
} |
| 2700 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 2701 |
for(i = 0; i < encoder->protected_->num_apodizations; i++) { |
| 2702 |
if(0 != encoder->private_->window_unaligned[i]) { |
| 2703 |
free(encoder->private_->window_unaligned[i]); |
| 2704 |
encoder->private_->window_unaligned[i] = 0; |
| 2705 |
} |
| 2706 |
} |
| 2707 |
#endif |
| 2708 |
for(t = 0; t < encoder->private_->num_threadtasks; t++) { |
| 2709 |
if(0 == encoder->private_->threadtask[t]) |
| 2710 |
continue; |
| 2711 |
for(i = 0; i < encoder->protected_->channels; i++) { |
| 2712 |
if(0 != encoder->private_->threadtask[t]->integer_signal_unaligned[i]) { |
| 2713 |
free(encoder->private_->threadtask[t]->integer_signal_unaligned[i]); |
| 2714 |
encoder->private_->threadtask[t]->integer_signal_unaligned[i] = 0; |
| 2715 |
} |
| 2716 |
} |
| 2717 |
for(i = 0; i < 2; i++) { |
| 2718 |
if(0 != encoder->private_->threadtask[t]->integer_signal_mid_side_unaligned[i]) { |
| 2719 |
free(encoder->private_->threadtask[t]->integer_signal_mid_side_unaligned[i]); |
| 2720 |
encoder->private_->threadtask[t]->integer_signal_mid_side_unaligned[i] = 0; |
| 2721 |
} |
| 2722 |
} |
| 2723 |
if(0 != encoder->private_->threadtask[t]->integer_signal_33bit_side_unaligned){ |
| 2724 |
free(encoder->private_->threadtask[t]->integer_signal_33bit_side_unaligned); |
| 2725 |
encoder->private_->threadtask[t]->integer_signal_33bit_side_unaligned = 0; |
| 2726 |
} |
| 2727 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 2728 |
if(0 != encoder->private_->threadtask[t]->windowed_signal_unaligned) { |
| 2729 |
free(encoder->private_->threadtask[t]->windowed_signal_unaligned); |
| 2730 |
encoder->private_->threadtask[t]->windowed_signal_unaligned = 0; |
| 2731 |
} |
| 2732 |
#endif |
| 2733 |
for(channel = 0; channel < encoder->protected_->channels; channel++) { |
| 2734 |
for(i = 0; i < 2; i++) { |
| 2735 |
if(0 != encoder->private_->threadtask[t]->residual_workspace_unaligned[channel][i]) { |
| 2736 |
free(encoder->private_->threadtask[t]->residual_workspace_unaligned[channel][i]); |
| 2737 |
encoder->private_->threadtask[t]->residual_workspace_unaligned[channel][i] = 0; |
| 2738 |
} |
| 2739 |
} |
| 2740 |
} |
| 2741 |
for(channel = 0; channel < 2; channel++) { |
| 2742 |
for(i = 0; i < 2; i++) { |
| 2743 |
if(0 != encoder->private_->threadtask[t]->residual_workspace_mid_side_unaligned[channel][i]) { |
| 2744 |
free(encoder->private_->threadtask[t]->residual_workspace_mid_side_unaligned[channel][i]); |
| 2745 |
encoder->private_->threadtask[t]->residual_workspace_mid_side_unaligned[channel][i] = 0; |
| 2746 |
} |
| 2747 |
} |
| 2748 |
} |
| 2749 |
if(0 != encoder->private_->threadtask[t]->abs_residual_partition_sums_unaligned) { |
| 2750 |
free(encoder->private_->threadtask[t]->abs_residual_partition_sums_unaligned); |
| 2751 |
encoder->private_->threadtask[t]->abs_residual_partition_sums_unaligned = 0; |
| 2752 |
} |
| 2753 |
if(0 != encoder->private_->threadtask[t]->raw_bits_per_partition_unaligned) { |
| 2754 |
free(encoder->private_->threadtask[t]->raw_bits_per_partition_unaligned); |
| 2755 |
encoder->private_->threadtask[t]->raw_bits_per_partition_unaligned = 0; |
| 2756 |
} |
| 2757 |
for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 2758 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->threadtask[t]->partitioned_rice_contents_workspace[i][0]); |
| 2759 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->threadtask[t]->partitioned_rice_contents_workspace[i][1]); |
| 2760 |
} |
| 2761 |
for(i = 0; i < 2; i++) { |
| 2762 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->threadtask[t]->partitioned_rice_contents_workspace_mid_side[i][0]); |
| 2763 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->threadtask[t]->partitioned_rice_contents_workspace_mid_side[i][1]); |
| 2764 |
} |
| 2765 |
for(i = 0; i < 2; i++) |
| 2766 |
FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->threadtask[t]->partitioned_rice_contents_extra[i]); |
| 2767 |
if(t > 0) { |
| 2768 |
#ifdef HAVE_PTHREAD |
| 2769 |
FLAC__bitwriter_delete(encoder->private_->threadtask[t]->frame); |
| 2770 |
pthread_mutex_destroy(&encoder->private_->threadtask[t]->mutex_this_task); |
| 2771 |
pthread_cond_destroy(&encoder->private_->threadtask[t]->cond_task_done); |
| 2772 |
free(encoder->private_->threadtask[t]); |
| 2773 |
encoder->private_->threadtask[t] = 0; |
| 2774 |
#else |
| 2775 |
FLAC__ASSERT(0); |
| 2776 |
#endif |
| 2777 |
} |
| 2778 |
|
| 2779 |
} |
| 2780 |
#ifdef HAVE_PTHREAD |
| 2781 |
if(encoder->protected_->num_threads > 1) { |
| 2782 |
pthread_mutex_destroy(&encoder->private_->mutex_md5_fifo); |
| 2783 |
pthread_mutex_destroy(&encoder->private_->mutex_work_queue); |
| 2784 |
pthread_cond_destroy(&encoder->private_->cond_md5_emptied); |
| 2785 |
pthread_cond_destroy(&encoder->private_->cond_work_available); |
| 2786 |
pthread_cond_destroy(&encoder->private_->cond_wake_up_thread); |
| 2787 |
if(encoder->protected_->do_md5) { |
| 2788 |
for(i = 0; i < encoder->protected_->channels; i++) { |
| 2789 |
if(0 != encoder->private_->md5_fifo.data[i]) { |
| 2790 |
free(encoder->private_->md5_fifo.data[i]); |
| 2791 |
encoder->private_->md5_fifo.data[i] = 0; |
| 2792 |
} |
| 2793 |
} |
| 2794 |
} |
| 2795 |
|
| 2796 |
} |
| 2797 |
#endif |
| 2798 |
if(encoder->protected_->verify) { |
| 2799 |
for(i = 0; i < encoder->protected_->channels; i++) { |
| 2800 |
if(0 != encoder->private_->verify.input_fifo.data[i]) { |
| 2801 |
free(encoder->private_->verify.input_fifo.data[i]); |
| 2802 |
encoder->private_->verify.input_fifo.data[i] = 0; |
| 2803 |
} |
| 2804 |
} |
| 2805 |
} |
| 2806 |
} |
| 2807 |
|
| 2808 |
FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, uint32_t new_blocksize) |
| 2809 |
{ |
| 2810 |
FLAC__bool ok; |
| 2811 |
uint32_t i, t, channel; |
| 2812 |
|
| 2813 |
FLAC__ASSERT(new_blocksize > 0); |
| 2814 |
FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
| 2815 |
|
| 2816 |
ok = true; |
| 2817 |
|
| 2818 |
/* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */ |
| 2819 |
if(new_blocksize > encoder->private_->input_capacity) { |
| 2820 |
|
| 2821 |
/* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx() and ..._intrin_sse2() |
| 2822 |
* require that the input arrays (in our case the integer signals) |
| 2823 |
* have a buffer of up to 3 zeroes in front (at negative indices) for |
| 2824 |
* alignment purposes; we use 4 in front to keep the data well-aligned. |
| 2825 |
*/ |
| 2826 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 2827 |
if(ok && encoder->protected_->max_lpc_order > 0) { |
| 2828 |
for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) |
| 2829 |
ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]); |
| 2830 |
} |
| 2831 |
#endif |
| 2832 |
for(t = 0; t < encoder->private_->num_threadtasks; t++) { |
| 2833 |
for(i = 0; ok && i < encoder->protected_->channels; i++) { |
| 2834 |
ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->threadtask[t]->integer_signal_unaligned[i], &encoder->private_->threadtask[t]->integer_signal[i]); |
| 2835 |
if(ok) { |
| 2836 |
memset(encoder->private_->threadtask[t]->integer_signal[i], 0, sizeof(FLAC__int32)*4); |
| 2837 |
encoder->private_->threadtask[t]->integer_signal[i] += 4; |
| 2838 |
} |
| 2839 |
} |
| 2840 |
for(i = 0; ok && i < 2; i++) { |
| 2841 |
ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->threadtask[t]->integer_signal_mid_side_unaligned[i], &encoder->private_->threadtask[t]->integer_signal_mid_side[i]); |
| 2842 |
if(ok) { |
| 2843 |
memset(encoder->private_->threadtask[t]->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4); |
| 2844 |
encoder->private_->threadtask[t]->integer_signal_mid_side[i] += 4; |
| 2845 |
} |
| 2846 |
} |
| 2847 |
ok = ok && FLAC__memory_alloc_aligned_int64_array(new_blocksize+4+OVERREAD_, &encoder->private_->threadtask[t]->integer_signal_33bit_side_unaligned, &encoder->private_->threadtask[t]->integer_signal_33bit_side); |
| 2848 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 2849 |
if(ok && encoder->protected_->max_lpc_order > 0) { |
| 2850 |
ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->threadtask[t]->windowed_signal_unaligned, &encoder->private_->threadtask[t]->windowed_signal); |
| 2851 |
} |
| 2852 |
#endif |
| 2853 |
for(channel = 0; ok && channel < encoder->protected_->channels; channel++) { |
| 2854 |
for(i = 0; ok && i < 2; i++) { |
| 2855 |
ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->threadtask[t]->residual_workspace_unaligned[channel][i], &encoder->private_->threadtask[t]->residual_workspace[channel][i]); |
| 2856 |
} |
| 2857 |
} |
| 2858 |
|
| 2859 |
|
| 2860 |
for(channel = 0; ok && channel < encoder->protected_->channels; channel++) { |
| 2861 |
for(i = 0; ok && i < 2; i++) { |
| 2862 |
ok = ok && FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(&encoder->private_->threadtask[t]->partitioned_rice_contents_workspace[channel][i], encoder->protected_->max_residual_partition_order); |
| 2863 |
ok = ok && FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(&encoder->private_->threadtask[t]->partitioned_rice_contents_workspace[channel][i], encoder->protected_->max_residual_partition_order); |
| 2864 |
} |
| 2865 |
} |
| 2866 |
|
| 2867 |
for(channel = 0; ok && channel < 2; channel++) { |
| 2868 |
for(i = 0; ok && i < 2; i++) { |
| 2869 |
ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->threadtask[t]->residual_workspace_mid_side_unaligned[channel][i], &encoder->private_->threadtask[t]->residual_workspace_mid_side[channel][i]); |
| 2870 |
} |
| 2871 |
} |
| 2872 |
|
| 2873 |
for(channel = 0; ok && channel < 2; channel++) { |
| 2874 |
for(i = 0; ok && i < 2; i++) { |
| 2875 |
ok = ok && FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(&encoder->private_->threadtask[t]->partitioned_rice_contents_workspace_mid_side[channel][i], encoder->protected_->max_residual_partition_order); |
| 2876 |
} |
| 2877 |
} |
| 2878 |
|
| 2879 |
for(i = 0; ok && i < 2; i++) { |
| 2880 |
ok = ok && FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(&encoder->private_->threadtask[t]->partitioned_rice_contents_extra[i], encoder->protected_->max_residual_partition_order); |
| 2881 |
} |
| 2882 |
|
| 2883 |
|
| 2884 |
/* the *2 is an approximation to the series 1 + 1/2 + 1/4 + ... that sums tree occupies in a flat array */ |
| 2885 |
/*@@@ new_blocksize*2 is too pessimistic, but to fix, we need smarter logic because a smaller new_blocksize can actually increase the # of partitions; would require moving this out into a separate function, then checking its capacity against the need of the current blocksize&min/max_partition_order (and maybe predictor order) */ |
| 2886 |
ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_blocksize * 2, &encoder->private_->threadtask[t]->abs_residual_partition_sums_unaligned, &encoder->private_->threadtask[t]->abs_residual_partition_sums); |
| 2887 |
if(encoder->protected_->do_escape_coding) |
| 2888 |
ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_blocksize * 2, &encoder->private_->threadtask[t]->raw_bits_per_partition_unaligned, &encoder->private_->threadtask[t]->raw_bits_per_partition); |
| 2889 |
} |
| 2890 |
} |
| 2891 |
if(ok) |
| 2892 |
encoder->private_->input_capacity = new_blocksize; |
| 2893 |
else { |
| 2894 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 2895 |
return ok; |
| 2896 |
} |
| 2897 |
|
| 2898 |
|
| 2899 |
/* now adjust the windows if the blocksize has changed */ |
| 2900 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 2901 |
if(encoder->protected_->max_lpc_order > 0 && new_blocksize > 1) { |
| 2902 |
for(i = 0; i < encoder->protected_->num_apodizations; i++) { |
| 2903 |
switch(encoder->protected_->apodizations[i].type) { |
| 2904 |
case FLAC__APODIZATION_BARTLETT: |
| 2905 |
FLAC__window_bartlett(encoder->private_->window[i], new_blocksize); |
| 2906 |
break; |
| 2907 |
case FLAC__APODIZATION_BARTLETT_HANN: |
| 2908 |
FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize); |
| 2909 |
break; |
| 2910 |
case FLAC__APODIZATION_BLACKMAN: |
| 2911 |
FLAC__window_blackman(encoder->private_->window[i], new_blocksize); |
| 2912 |
break; |
| 2913 |
case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE: |
| 2914 |
FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize); |
| 2915 |
break; |
| 2916 |
case FLAC__APODIZATION_CONNES: |
| 2917 |
FLAC__window_connes(encoder->private_->window[i], new_blocksize); |
| 2918 |
break; |
| 2919 |
case FLAC__APODIZATION_FLATTOP: |
| 2920 |
FLAC__window_flattop(encoder->private_->window[i], new_blocksize); |
| 2921 |
break; |
| 2922 |
case FLAC__APODIZATION_GAUSS: |
| 2923 |
FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev); |
| 2924 |
break; |
| 2925 |
case FLAC__APODIZATION_HAMMING: |
| 2926 |
FLAC__window_hamming(encoder->private_->window[i], new_blocksize); |
| 2927 |
break; |
| 2928 |
case FLAC__APODIZATION_HANN: |
| 2929 |
FLAC__window_hann(encoder->private_->window[i], new_blocksize); |
| 2930 |
break; |
| 2931 |
case FLAC__APODIZATION_KAISER_BESSEL: |
| 2932 |
FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize); |
| 2933 |
break; |
| 2934 |
case FLAC__APODIZATION_NUTTALL: |
| 2935 |
FLAC__window_nuttall(encoder->private_->window[i], new_blocksize); |
| 2936 |
break; |
| 2937 |
case FLAC__APODIZATION_RECTANGLE: |
| 2938 |
FLAC__window_rectangle(encoder->private_->window[i], new_blocksize); |
| 2939 |
break; |
| 2940 |
case FLAC__APODIZATION_TRIANGLE: |
| 2941 |
FLAC__window_triangle(encoder->private_->window[i], new_blocksize); |
| 2942 |
break; |
| 2943 |
case FLAC__APODIZATION_TUKEY: |
| 2944 |
FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p); |
| 2945 |
break; |
| 2946 |
case FLAC__APODIZATION_PARTIAL_TUKEY: |
| 2947 |
FLAC__window_partial_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.multiple_tukey.p, encoder->protected_->apodizations[i].parameters.multiple_tukey.start, encoder->protected_->apodizations[i].parameters.multiple_tukey.end); |
| 2948 |
break; |
| 2949 |
case FLAC__APODIZATION_PUNCHOUT_TUKEY: |
| 2950 |
FLAC__window_punchout_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.multiple_tukey.p, encoder->protected_->apodizations[i].parameters.multiple_tukey.start, encoder->protected_->apodizations[i].parameters.multiple_tukey.end); |
| 2951 |
break; |
| 2952 |
case FLAC__APODIZATION_SUBDIVIDE_TUKEY: |
| 2953 |
FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p); |
| 2954 |
break; |
| 2955 |
case FLAC__APODIZATION_WELCH: |
| 2956 |
FLAC__window_welch(encoder->private_->window[i], new_blocksize); |
| 2957 |
break; |
| 2958 |
default: |
| 2959 |
FLAC__ASSERT(0); |
| 2960 |
/* double protection */ |
| 2961 |
FLAC__window_hann(encoder->private_->window[i], new_blocksize); |
| 2962 |
break; |
| 2963 |
} |
| 2964 |
} |
| 2965 |
} |
| 2966 |
if (new_blocksize <= FLAC__MAX_LPC_ORDER) { |
| 2967 |
/* intrinsics autocorrelation routines do not all handle cases in which lag might be |
| 2968 |
* larger than data_len. Lag is one larger than the LPC order */ |
| 2969 |
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation; |
| 2970 |
} |
| 2971 |
#endif |
| 2972 |
|
| 2973 |
return true; |
| 2974 |
} |
| 2975 |
|
| 2976 |
FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderThreadTask *threadtask, uint32_t samples, FLAC__bool is_last_block) |
| 2977 |
{ |
| 2978 |
const FLAC__byte *buffer; |
| 2979 |
size_t bytes; |
| 2980 |
|
| 2981 |
FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(threadtask->frame)); |
| 2982 |
|
| 2983 |
if(!FLAC__bitwriter_get_buffer(threadtask->frame, &buffer, &bytes)) { |
| 2984 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 2985 |
return false; |
| 2986 |
} |
| 2987 |
|
| 2988 |
if(encoder->protected_->verify) { |
| 2989 |
encoder->private_->verify.output.data = buffer; |
| 2990 |
encoder->private_->verify.output.bytes = bytes; |
| 2991 |
if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) { |
| 2992 |
encoder->private_->verify.needs_magic_hack = true; |
| 2993 |
} |
| 2994 |
else { |
| 2995 |
if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder) |
| 2996 |
|| (!is_last_block |
| 2997 |
&& (FLAC__stream_encoder_get_verify_decoder_state(encoder) == FLAC__STREAM_DECODER_END_OF_STREAM)) |
| 2998 |
|| encoder->protected_->state == FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR /* Happens when error callback was used */) { |
| 2999 |
FLAC__bitwriter_release_buffer(threadtask->frame); |
| 3000 |
FLAC__bitwriter_clear(threadtask->frame); |
| 3001 |
if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA) |
| 3002 |
encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 3003 |
return false; |
| 3004 |
} |
| 3005 |
} |
| 3006 |
} |
| 3007 |
|
| 3008 |
if(write_frame_(encoder, buffer, bytes, samples, is_last_block) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) { |
| 3009 |
FLAC__bitwriter_release_buffer(threadtask->frame); |
| 3010 |
FLAC__bitwriter_clear(threadtask->frame); |
| 3011 |
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 3012 |
return false; |
| 3013 |
} |
| 3014 |
|
| 3015 |
FLAC__bitwriter_release_buffer(threadtask->frame); |
| 3016 |
FLAC__bitwriter_clear(threadtask->frame); |
| 3017 |
|
| 3018 |
if(samples > 0) { |
| 3019 |
encoder->private_->streaminfo.data.stream_info.min_framesize = flac_min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize); |
| 3020 |
encoder->private_->streaminfo.data.stream_info.max_framesize = flac_max(bytes, encoder->private_->streaminfo.data.stream_info.max_framesize); |
| 3021 |
} |
| 3022 |
|
| 3023 |
return true; |
| 3024 |
} |
| 3025 |
|
| 3026 |
FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, uint32_t samples, FLAC__bool is_last_block) |
| 3027 |
{ |
| 3028 |
FLAC__StreamEncoderWriteStatus status; |
| 3029 |
FLAC__uint64 output_position = 0; |
| 3030 |
|
| 3031 |
#if FLAC__HAS_OGG == 0 |
| 3032 |
(void)is_last_block; |
| 3033 |
#endif |
| 3034 |
|
| 3035 |
/* |
| 3036 |
* Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets. |
| 3037 |
*/ |
| 3038 |
if(samples == 0) { |
| 3039 |
FLAC__MetadataType type = (buffer[0] & 0x7f); |
| 3040 |
|
| 3041 |
/* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */ |
| 3042 |
if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) { |
| 3043 |
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 3044 |
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR; |
| 3045 |
} |
| 3046 |
|
| 3047 |
if(type == FLAC__METADATA_TYPE_STREAMINFO) |
| 3048 |
encoder->protected_->streaminfo_offset = output_position; |
| 3049 |
else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0) |
| 3050 |
encoder->protected_->seektable_offset = output_position; |
| 3051 |
} |
| 3052 |
|
| 3053 |
/* |
| 3054 |
* Mark the current seek point if hit (if audio_offset == 0 that |
| 3055 |
* means we're still writing metadata and haven't hit the first |
| 3056 |
* frame yet) |
| 3057 |
*/ |
| 3058 |
if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) { |
| 3059 |
const uint32_t blocksize = FLAC__stream_encoder_get_blocksize(encoder); |
| 3060 |
const FLAC__uint64 frame_first_sample = encoder->private_->samples_written; |
| 3061 |
const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1; |
| 3062 |
FLAC__uint64 test_sample; |
| 3063 |
uint32_t i; |
| 3064 |
for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) { |
| 3065 |
test_sample = encoder->private_->seek_table->points[i].sample_number; |
| 3066 |
if(test_sample > frame_last_sample) { |
| 3067 |
break; |
| 3068 |
} |
| 3069 |
else if(test_sample >= frame_first_sample) { |
| 3070 |
/* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */ |
| 3071 |
if(output_position == 0 && encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) { |
| 3072 |
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 3073 |
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR; |
| 3074 |
} |
| 3075 |
|
| 3076 |
encoder->private_->seek_table->points[i].sample_number = frame_first_sample; |
| 3077 |
encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset; |
| 3078 |
encoder->private_->seek_table->points[i].frame_samples = blocksize; |
| 3079 |
encoder->private_->first_seekpoint_to_check++; |
| 3080 |
/* DO NOT: "break;" and here's why: |
| 3081 |
* The seektable template may contain more than one target |
| 3082 |
* sample for any given frame; we will keep looping, generating |
| 3083 |
* duplicate seekpoints for them, and we'll clean it up later, |
| 3084 |
* just before writing the seektable back to the metadata. |
| 3085 |
*/ |
| 3086 |
} |
| 3087 |
else { |
| 3088 |
encoder->private_->first_seekpoint_to_check++; |
| 3089 |
} |
| 3090 |
} |
| 3091 |
} |
| 3092 |
|
| 3093 |
#if FLAC__HAS_OGG |
| 3094 |
if(encoder->private_->is_ogg) { |
| 3095 |
status = FLAC__ogg_encoder_aspect_write_callback_wrapper( |
| 3096 |
&encoder->protected_->ogg_encoder_aspect, |
| 3097 |
buffer, |
| 3098 |
bytes, |
| 3099 |
samples, |
| 3100 |
encoder->private_->current_frame_number, |
| 3101 |
is_last_block, |
| 3102 |
(FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback, |
| 3103 |
encoder, |
| 3104 |
encoder->private_->client_data |
| 3105 |
); |
| 3106 |
} |
| 3107 |
else |
| 3108 |
#endif |
| 3109 |
status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data); |
| 3110 |
|
| 3111 |
if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) { |
| 3112 |
encoder->private_->bytes_written += bytes; |
| 3113 |
encoder->private_->samples_written += samples; |
| 3114 |
/* we keep a high watermark on the number of frames written because |
| 3115 |
* when the encoder goes back to write metadata, 'current_frame' |
| 3116 |
* will drop back to 0. |
| 3117 |
*/ |
| 3118 |
encoder->private_->frames_written = flac_max(encoder->private_->frames_written, encoder->private_->current_frame_number+1); |
| 3119 |
} |
| 3120 |
else |
| 3121 |
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 3122 |
|
| 3123 |
return status; |
| 3124 |
} |
| 3125 |
|
| 3126 |
/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */ |
| 3127 |
void update_metadata_(const FLAC__StreamEncoder *encoder) |
| 3128 |
{ |
| 3129 |
FLAC__byte b[flac_max(6u, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)]; |
| 3130 |
const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo; |
| 3131 |
FLAC__uint64 samples = metadata->data.stream_info.total_samples; |
| 3132 |
const uint32_t min_framesize = metadata->data.stream_info.min_framesize; |
| 3133 |
const uint32_t max_framesize = metadata->data.stream_info.max_framesize; |
| 3134 |
const uint32_t bps = metadata->data.stream_info.bits_per_sample; |
| 3135 |
FLAC__StreamEncoderSeekStatus seek_status; |
| 3136 |
|
| 3137 |
FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO); |
| 3138 |
|
| 3139 |
/* All this is based on intimate knowledge of the stream header |
| 3140 |
* layout, but a change to the header format that would break this |
| 3141 |
* would also break all streams encoded in the previous format. |
| 3142 |
*/ |
| 3143 |
|
| 3144 |
/* |
| 3145 |
* Write MD5 signature |
| 3146 |
*/ |
| 3147 |
{ |
| 3148 |
const uint32_t md5_offset = |
| 3149 |
FLAC__STREAM_METADATA_HEADER_LENGTH + |
| 3150 |
( |
| 3151 |
FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN + |
| 3152 |
FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN + |
| 3153 |
FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN + |
| 3154 |
FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN + |
| 3155 |
FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN + |
| 3156 |
FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN + |
| 3157 |
FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN + |
| 3158 |
FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN |
| 3159 |
) / 8; |
| 3160 |
|
| 3161 |
if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) { |
| 3162 |
if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR) |
| 3163 |
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 3164 |
return; |
| 3165 |
} |
| 3166 |
if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) { |
| 3167 |
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 3168 |
return; |
| 3169 |
} |
| 3170 |
} |
| 3171 |
|
| 3172 |
/* |
| 3173 |
* Write total samples |
| 3174 |
*/ |
| 3175 |
{ |
| 3176 |
const uint32_t total_samples_byte_offset = |
| 3177 |
FLAC__STREAM_METADATA_HEADER_LENGTH + |
| 3178 |
( |
| 3179 |
FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN + |
| 3180 |
FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN + |
| 3181 |
FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN + |
| 3182 |
FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN + |
| 3183 |
FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN + |
| 3184 |
FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN + |
| 3185 |
FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN |
| 3186 |
- 4 |
| 3187 |
) / 8; |
| 3188 |
FLAC__uint64 samples_uint36 = samples; |
| 3189 |
if(samples > (FLAC__U64L(1) << FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN)) |
| 3190 |
samples_uint36 = 0; |
| 3191 |
|
| 3192 |
b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples_uint36 >> 32) & 0x0F); |
| 3193 |
b[1] = (FLAC__byte)((samples_uint36 >> 24) & 0xFF); |
| 3194 |
b[2] = (FLAC__byte)((samples_uint36 >> 16) & 0xFF); |
| 3195 |
b[3] = (FLAC__byte)((samples_uint36 >> 8) & 0xFF); |
| 3196 |
b[4] = (FLAC__byte)(samples_uint36 & 0xFF); |
| 3197 |
if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + total_samples_byte_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) { |
| 3198 |
if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR) |
| 3199 |
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 3200 |
return; |
| 3201 |
} |
| 3202 |
if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) { |
| 3203 |
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 3204 |
return; |
| 3205 |
} |
| 3206 |
} |
| 3207 |
|
| 3208 |
/* |
| 3209 |
* Write min/max framesize |
| 3210 |
*/ |
| 3211 |
{ |
| 3212 |
const uint32_t min_framesize_offset = |
| 3213 |
FLAC__STREAM_METADATA_HEADER_LENGTH + |
| 3214 |
( |
| 3215 |
FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN + |
| 3216 |
FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN |
| 3217 |
) / 8; |
| 3218 |
|
| 3219 |
b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF); |
| 3220 |
b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF); |
| 3221 |
b[2] = (FLAC__byte)(min_framesize & 0xFF); |
| 3222 |
b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF); |
| 3223 |
b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF); |
| 3224 |
b[5] = (FLAC__byte)(max_framesize & 0xFF); |
| 3225 |
if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + min_framesize_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) { |
| 3226 |
if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR) |
| 3227 |
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 3228 |
return; |
| 3229 |
} |
| 3230 |
if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) { |
| 3231 |
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 3232 |
return; |
| 3233 |
} |
| 3234 |
} |
| 3235 |
|
| 3236 |
/* |
| 3237 |
* Write seektable |
| 3238 |
*/ |
| 3239 |
if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) { |
| 3240 |
uint32_t i; |
| 3241 |
|
| 3242 |
/* Convert unused seekpoints to placeholders */ |
| 3243 |
for(i = 0; i < encoder->private_->seek_table->num_points; i++) |
| 3244 |
if(encoder->private_->seek_table->points[i].sample_number > samples) |
| 3245 |
encoder->private_->seek_table->points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER; |
| 3246 |
|
| 3247 |
FLAC__format_seektable_sort(encoder->private_->seek_table); |
| 3248 |
|
| 3249 |
FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table)); |
| 3250 |
|
| 3251 |
if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->seektable_offset + FLAC__STREAM_METADATA_HEADER_LENGTH, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) { |
| 3252 |
if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR) |
| 3253 |
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 3254 |
return; |
| 3255 |
} |
| 3256 |
|
| 3257 |
for(i = 0; i < encoder->private_->seek_table->num_points; i++) { |
| 3258 |
FLAC__uint64 xx; |
| 3259 |
uint32_t x; |
| 3260 |
xx = encoder->private_->seek_table->points[i].sample_number; |
| 3261 |
b[7] = (FLAC__byte)xx; xx >>= 8; |
| 3262 |
b[6] = (FLAC__byte)xx; xx >>= 8; |
| 3263 |
b[5] = (FLAC__byte)xx; xx >>= 8; |
| 3264 |
b[4] = (FLAC__byte)xx; xx >>= 8; |
| 3265 |
b[3] = (FLAC__byte)xx; xx >>= 8; |
| 3266 |
b[2] = (FLAC__byte)xx; xx >>= 8; |
| 3267 |
b[1] = (FLAC__byte)xx; xx >>= 8; |
| 3268 |
b[0] = (FLAC__byte)xx; xx >>= 8; |
| 3269 |
xx = encoder->private_->seek_table->points[i].stream_offset; |
| 3270 |
b[15] = (FLAC__byte)xx; xx >>= 8; |
| 3271 |
b[14] = (FLAC__byte)xx; xx >>= 8; |
| 3272 |
b[13] = (FLAC__byte)xx; xx >>= 8; |
| 3273 |
b[12] = (FLAC__byte)xx; xx >>= 8; |
| 3274 |
b[11] = (FLAC__byte)xx; xx >>= 8; |
| 3275 |
b[10] = (FLAC__byte)xx; xx >>= 8; |
| 3276 |
b[9] = (FLAC__byte)xx; xx >>= 8; |
| 3277 |
b[8] = (FLAC__byte)xx; xx >>= 8; |
| 3278 |
x = encoder->private_->seek_table->points[i].frame_samples; |
| 3279 |
b[17] = (FLAC__byte)x; x >>= 8; |
| 3280 |
b[16] = (FLAC__byte)x; x >>= 8; |
| 3281 |
if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) { |
| 3282 |
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
| 3283 |
return; |
| 3284 |
} |
| 3285 |
} |
| 3286 |
} |
| 3287 |
} |
| 3288 |
|
| 3289 |
#if FLAC__HAS_OGG |
| 3290 |
/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */ |
| 3291 |
void update_ogg_metadata_(FLAC__StreamEncoder *encoder) |
| 3292 |
{ |
| 3293 |
/* the # of bytes in the 1st packet that precede the STREAMINFO */ |
| 3294 |
static const uint32_t FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH = |
| 3295 |
FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH + |
| 3296 |
FLAC__OGG_MAPPING_MAGIC_LENGTH + |
| 3297 |
FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH + |
| 3298 |
FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH + |
| 3299 |
FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH + |
| 3300 |
FLAC__STREAM_SYNC_LENGTH |
| 3301 |
; |
| 3302 |
FLAC__byte b[flac_max(6u, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)]; |
| 3303 |
const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo; |
| 3304 |
const FLAC__uint64 samples = metadata->data.stream_info.total_samples; |
| 3305 |
const uint32_t min_framesize = metadata->data.stream_info.min_framesize; |
| 3306 |
const uint32_t max_framesize = metadata->data.stream_info.max_framesize; |
| 3307 |
ogg_page page; |
| 3308 |
|
| 3309 |
FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO); |
| 3310 |
FLAC__ASSERT(0 != encoder->private_->seek_callback); |
| 3311 |
|
| 3312 |
/* Pre-check that client supports seeking, since we don't want the |
| 3313 |
* ogg_helper code to ever have to deal with this condition. |
| 3314 |
*/ |
| 3315 |
if(encoder->private_->seek_callback(encoder, 0, encoder->private_->client_data) == FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED) |
| 3316 |
return; |
| 3317 |
|
| 3318 |
/* All this is based on intimate knowledge of the stream header |
| 3319 |
* layout, but a change to the header format that would break this |
| 3320 |
* would also break all streams encoded in the previous format. |
| 3321 |
*/ |
| 3322 |
|
| 3323 |
/** |
| 3324 |
** Write STREAMINFO stats |
| 3325 |
**/ |
| 3326 |
simple_ogg_page__init(&page); |
| 3327 |
if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) { |
| 3328 |
simple_ogg_page__clear(&page); |
| 3329 |
return; /* state already set */ |
| 3330 |
} |
| 3331 |
|
| 3332 |
/* |
| 3333 |
* Write MD5 signature |
| 3334 |
*/ |
| 3335 |
{ |
| 3336 |
const uint32_t md5_offset = |
| 3337 |
FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH + |
| 3338 |
FLAC__STREAM_METADATA_HEADER_LENGTH + |
| 3339 |
( |
| 3340 |
FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN + |
| 3341 |
FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN + |
| 3342 |
FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN + |
| 3343 |
FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN + |
| 3344 |
FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN + |
| 3345 |
FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN + |
| 3346 |
FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN + |
| 3347 |
FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN |
| 3348 |
) / 8; |
| 3349 |
|
| 3350 |
if(md5_offset + 16 > (uint32_t)page.body_len) { |
| 3351 |
encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR; |
| 3352 |
simple_ogg_page__clear(&page); |
| 3353 |
return; |
| 3354 |
} |
| 3355 |
memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16); |
| 3356 |
} |
| 3357 |
|
| 3358 |
/* |
| 3359 |
* Write total samples |
| 3360 |
*/ |
| 3361 |
{ |
| 3362 |
const uint32_t total_samples_byte_offset = |
| 3363 |
FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH + |
| 3364 |
FLAC__STREAM_METADATA_HEADER_LENGTH + |
| 3365 |
( |
| 3366 |
FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN + |
| 3367 |
FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN + |
| 3368 |
FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN + |
| 3369 |
FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN + |
| 3370 |
FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN + |
| 3371 |
FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN + |
| 3372 |
FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN |
| 3373 |
- 4 |
| 3374 |
) / 8; |
| 3375 |
|
| 3376 |
if(total_samples_byte_offset + 5 > (uint32_t)page.body_len) { |
| 3377 |
encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR; |
| 3378 |
simple_ogg_page__clear(&page); |
| 3379 |
return; |
| 3380 |
} |
| 3381 |
b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0; |
| 3382 |
b[0] |= (FLAC__byte)((samples >> 32) & 0x0F); |
| 3383 |
b[1] = (FLAC__byte)((samples >> 24) & 0xFF); |
| 3384 |
b[2] = (FLAC__byte)((samples >> 16) & 0xFF); |
| 3385 |
b[3] = (FLAC__byte)((samples >> 8) & 0xFF); |
| 3386 |
b[4] = (FLAC__byte)(samples & 0xFF); |
| 3387 |
memcpy(page.body + total_samples_byte_offset, b, 5); |
| 3388 |
} |
| 3389 |
|
| 3390 |
/* |
| 3391 |
* Write min/max framesize |
| 3392 |
*/ |
| 3393 |
{ |
| 3394 |
const uint32_t min_framesize_offset = |
| 3395 |
FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH + |
| 3396 |
FLAC__STREAM_METADATA_HEADER_LENGTH + |
| 3397 |
( |
| 3398 |
FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN + |
| 3399 |
FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN |
| 3400 |
) / 8; |
| 3401 |
|
| 3402 |
if(min_framesize_offset + 6 > (uint32_t)page.body_len) { |
| 3403 |
encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR; |
| 3404 |
simple_ogg_page__clear(&page); |
| 3405 |
return; |
| 3406 |
} |
| 3407 |
b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF); |
| 3408 |
b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF); |
| 3409 |
b[2] = (FLAC__byte)(min_framesize & 0xFF); |
| 3410 |
b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF); |
| 3411 |
b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF); |
| 3412 |
b[5] = (FLAC__byte)(max_framesize & 0xFF); |
| 3413 |
memcpy(page.body + min_framesize_offset, b, 6); |
| 3414 |
} |
| 3415 |
if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) { |
| 3416 |
simple_ogg_page__clear(&page); |
| 3417 |
return; /* state already set */ |
| 3418 |
} |
| 3419 |
simple_ogg_page__clear(&page); |
| 3420 |
} |
| 3421 |
#endif |
| 3422 |
|
| 3423 |
FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_block) |
| 3424 |
{ |
| 3425 |
FLAC__uint16 crc; |
| 3426 |
#ifdef HAVE_PTHREAD |
| 3427 |
uint32_t i; |
| 3428 |
#endif |
| 3429 |
if(encoder->protected_->num_threads < 2 || is_last_block) { |
| 3430 |
|
| 3431 |
FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
| 3432 |
|
| 3433 |
/* |
| 3434 |
* Accumulate raw signal to the MD5 signature |
| 3435 |
*/ |
| 3436 |
if(encoder->protected_->do_md5 && !FLAC__MD5Accumulate(&encoder->private_->md5context, (const FLAC__int32 * const *)encoder->private_->threadtask[0]->integer_signal, encoder->protected_->channels, encoder->protected_->blocksize, (encoder->protected_->bits_per_sample+7) / 8)) { |
| 3437 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 3438 |
return false; |
| 3439 |
} |
| 3440 |
|
| 3441 |
/* |
| 3442 |
* Process the frame header and subframes into the frame bitbuffer |
| 3443 |
*/ |
| 3444 |
encoder->private_->threadtask[0]->current_frame_number = encoder->private_->current_frame_number; |
| 3445 |
if(!process_subframes_(encoder, encoder->private_->threadtask[0])) { |
| 3446 |
/* the above function sets the state for us in case of an error */ |
| 3447 |
return false; |
| 3448 |
} |
| 3449 |
|
| 3450 |
/* |
| 3451 |
* Zero-pad the frame to a byte_boundary |
| 3452 |
*/ |
| 3453 |
if(!FLAC__bitwriter_zero_pad_to_byte_boundary(encoder->private_->threadtask[0]->frame)) { |
| 3454 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 3455 |
return false; |
| 3456 |
} |
| 3457 |
|
| 3458 |
/* |
| 3459 |
* CRC-16 the whole thing |
| 3460 |
*/ |
| 3461 |
FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->threadtask[0]->frame)); |
| 3462 |
if( |
| 3463 |
!FLAC__bitwriter_get_write_crc16(encoder->private_->threadtask[0]->frame, &crc) || |
| 3464 |
!FLAC__bitwriter_write_raw_uint32(encoder->private_->threadtask[0]->frame, crc, FLAC__FRAME_FOOTER_CRC_LEN) |
| 3465 |
) { |
| 3466 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 3467 |
return false; |
| 3468 |
} |
| 3469 |
|
| 3470 |
/* |
| 3471 |
* Write it |
| 3472 |
*/ |
| 3473 |
if(!write_bitbuffer_(encoder, encoder->private_->threadtask[0], encoder->protected_->blocksize, is_last_block)) { |
| 3474 |
/* the above function sets the state for us in case of an error */ |
| 3475 |
return false; |
| 3476 |
} |
| 3477 |
} |
| 3478 |
else { |
| 3479 |
#ifdef HAVE_PTHREAD |
| 3480 |
/* This bit is quite complicated, so here are some pointers: |
| 3481 |
* |
| 3482 |
* When this bit of code is reached for the first time, new threads are spawned and |
| 3483 |
* threadtasks are populated until the total number of threads equals the requested number |
| 3484 |
* of threads. Next, threadtasks are populated until they there are no more available. |
| 3485 |
* Next, this main thread checks whether the threadtask that is due chronologically is |
| 3486 |
* done. If it is, the bitbuffer is written and the threadtask memory reused for the next |
| 3487 |
* frame. If it is not done, the main thread checks whether there is enough work left in the |
| 3488 |
* queue. If there is a lot of work left, the main thread starts on some of it too. |
| 3489 |
* If not a lot of work is left, the main thread goes to sleep until the frame due first is |
| 3490 |
* finished. |
| 3491 |
* |
| 3492 |
* - encoder->private_->next_thread is the number of the next thread to be created or, when |
| 3493 |
* the required number of threads is created, the next threadtask to be populated, |
| 3494 |
* or, when all threadtasks have been populated once, the next threadtask that needs |
| 3495 |
* to finish and thus reused. |
| 3496 |
* - encoder->private_->next_threadtask is the number of the next threadtask that a thread |
| 3497 |
* can start work on. |
| 3498 |
* |
| 3499 |
* So, in effect, next_thread is (after startup) a pointer considering the chronological |
| 3500 |
* order, so input/output isn't shuffled. next_threadtask is a pointer to the next task that |
| 3501 |
* hasn't been picked up by a thread yet. This distinction enables threads to work on frames |
| 3502 |
* in a non-chronological order |
| 3503 |
* |
| 3504 |
* encoder->protected_->num_threads is the max number of threads that can be spawned |
| 3505 |
* encoder->private_->num_created_threads is the number of threads that has been spawned |
| 3506 |
* encoder->private_->num_threadtasks keeps track of how many threadtasks are available |
| 3507 |
* encoder->private_->num_started_threadtasks keeps track of how many threadtasks have been populated |
| 3508 |
* |
| 3509 |
* NOTE: thread no. 0 and threadtask no. 0 are reserved for non-threaded operations, so next_thread |
| 3510 |
* and next_threadtask start at 1 |
| 3511 |
*/ |
| 3512 |
if(encoder->private_->num_created_threads < encoder->protected_->num_threads) { |
| 3513 |
/* Create a new thread */ |
| 3514 |
pthread_create(&encoder->private_->thread[encoder->private_->next_thread], |
| 3515 |
NULL, process_frame_thread_, encoder); |
| 3516 |
encoder->private_->num_created_threads++; |
| 3517 |
} |
| 3518 |
else if(encoder->private_->num_started_threadtasks == encoder->private_->num_threadtasks) { |
| 3519 |
/* If the first task in the queue is still running, check whether there is enough work |
| 3520 |
* left in the queue. If there is, start on some |
| 3521 |
* First, check whether the mutex for the next due task is locked or free. If it is free (and thus acquired now) and |
| 3522 |
* the task is done, proceed to the next bit (writing the bitbuffer). If it is either currently locked or not yet |
| 3523 |
* processed, choose between starting on some work (if there is enough work in the queue) or waiting for the task |
| 3524 |
* to finish. Either way, release the mutex first, so it doesn't get interlocked with the work queue mutex */ |
| 3525 |
int mutex_result = pthread_mutex_trylock(&encoder->private_->threadtask[encoder->private_->next_thread]->mutex_this_task); |
| 3526 |
while(mutex_result || !encoder->private_->threadtask[encoder->private_->next_thread]->task_done) { |
| 3527 |
if(!mutex_result) |
| 3528 |
pthread_mutex_unlock(&encoder->private_->threadtask[encoder->private_->next_thread]->mutex_this_task); |
| 3529 |
|
| 3530 |
pthread_mutex_lock(&encoder->private_->mutex_work_queue); |
| 3531 |
if(encoder->private_->num_available_threadtasks > (encoder->protected_->num_threads - 1)) { |
| 3532 |
FLAC__StreamEncoderThreadTask * task = NULL; |
| 3533 |
task = encoder->private_->threadtask[encoder->private_->next_threadtask]; |
| 3534 |
encoder->private_->num_available_threadtasks--; |
| 3535 |
encoder->private_->next_threadtask++; |
| 3536 |
if(encoder->private_->next_threadtask == encoder->private_->num_threadtasks) |
| 3537 |
encoder->private_->next_threadtask = 1; |
| 3538 |
pthread_mutex_unlock(&encoder->private_->mutex_work_queue); |
| 3539 |
pthread_mutex_lock(&task->mutex_this_task); |
| 3540 |
process_frame_thread_inner_(encoder, task); |
| 3541 |
mutex_result = pthread_mutex_trylock(&encoder->private_->threadtask[encoder->private_->next_thread]->mutex_this_task); |
| 3542 |
} |
| 3543 |
else { |
| 3544 |
pthread_mutex_unlock(&encoder->private_->mutex_work_queue); |
| 3545 |
pthread_mutex_lock(&encoder->private_->threadtask[encoder->private_->next_thread]->mutex_this_task); |
| 3546 |
while(!encoder->private_->threadtask[encoder->private_->next_thread]->task_done) |
| 3547 |
pthread_cond_wait(&encoder->private_->threadtask[encoder->private_->next_thread]->cond_task_done,&encoder->private_->threadtask[encoder->private_->next_thread]->mutex_this_task); |
| 3548 |
mutex_result = 0; |
| 3549 |
} |
| 3550 |
} |
| 3551 |
/* Task is finished, write bitbuffer */ |
| 3552 |
if(!encoder->private_->threadtask[encoder->private_->next_thread]->returnvalue) { |
| 3553 |
pthread_mutex_unlock(&encoder->private_->threadtask[encoder->private_->next_thread]->mutex_this_task); |
| 3554 |
return false; |
| 3555 |
} |
| 3556 |
if(!write_bitbuffer_(encoder, encoder->private_->threadtask[encoder->private_->next_thread], encoder->protected_->blocksize, is_last_block)) { |
| 3557 |
/* the above function sets the state for us in case of an error */ |
| 3558 |
pthread_mutex_unlock(&encoder->private_->threadtask[encoder->private_->next_thread]->mutex_this_task); |
| 3559 |
return false; |
| 3560 |
} |
| 3561 |
pthread_mutex_unlock(&encoder->private_->threadtask[encoder->private_->next_thread]->mutex_this_task); |
| 3562 |
} |
| 3563 |
/* Copy input data for MD5 calculation */ |
| 3564 |
if(encoder->protected_->do_md5) { |
| 3565 |
pthread_mutex_lock(&encoder->private_->mutex_work_queue); |
| 3566 |
while(encoder->private_->md5_fifo.tail + encoder->protected_->blocksize > encoder->private_->md5_fifo.size) { |
| 3567 |
pthread_cond_wait(&encoder->private_->cond_md5_emptied,&encoder->private_->mutex_work_queue); |
| 3568 |
} |
| 3569 |
pthread_mutex_unlock(&encoder->private_->mutex_work_queue); |
| 3570 |
pthread_mutex_lock(&encoder->private_->mutex_md5_fifo); |
| 3571 |
for(i = 0; i < encoder->protected_->channels; i++) |
| 3572 |
memcpy(encoder->private_->md5_fifo.data[i]+encoder->private_->md5_fifo.tail, encoder->private_->threadtask[0]->integer_signal[i], encoder->protected_->blocksize * sizeof(encoder->private_->threadtask[0]->integer_signal[i][0])); |
| 3573 |
pthread_mutex_lock(&encoder->private_->mutex_work_queue); |
| 3574 |
encoder->private_->md5_fifo.tail += encoder->protected_->blocksize; |
| 3575 |
pthread_cond_signal(&encoder->private_->cond_work_available); |
| 3576 |
pthread_mutex_unlock(&encoder->private_->mutex_work_queue); |
| 3577 |
pthread_mutex_unlock(&encoder->private_->mutex_md5_fifo); |
| 3578 |
} |
| 3579 |
|
| 3580 |
/* Copy input data for frame creation */ |
| 3581 |
pthread_mutex_lock(&encoder->private_->threadtask[encoder->private_->next_thread]->mutex_this_task); |
| 3582 |
for(i = 0; i < encoder->protected_->channels; i++) |
| 3583 |
memcpy(encoder->private_->threadtask[encoder->private_->next_thread]->integer_signal[i], encoder->private_->threadtask[0]->integer_signal[i], encoder->protected_->blocksize * sizeof(encoder->private_->threadtask[0]->integer_signal[i][0])); |
| 3584 |
|
| 3585 |
encoder->private_->threadtask[encoder->private_->next_thread]->current_frame_number = encoder->private_->current_frame_number; |
| 3586 |
pthread_mutex_unlock(&encoder->private_->threadtask[encoder->private_->next_thread]->mutex_this_task); |
| 3587 |
|
| 3588 |
pthread_mutex_lock(&encoder->private_->mutex_work_queue); |
| 3589 |
if(encoder->private_->num_started_threadtasks < encoder->private_->num_threadtasks) |
| 3590 |
encoder->private_->num_started_threadtasks++; |
| 3591 |
encoder->private_->num_available_threadtasks++; |
| 3592 |
encoder->private_->threadtask[encoder->private_->next_thread]->task_done = false; |
| 3593 |
pthread_cond_signal(&encoder->private_->cond_work_available); |
| 3594 |
pthread_mutex_unlock(&encoder->private_->mutex_work_queue); |
| 3595 |
|
| 3596 |
encoder->private_->next_thread++; |
| 3597 |
if(encoder->private_->next_thread == encoder->private_->num_threadtasks) |
| 3598 |
encoder->private_->next_thread = 1; |
| 3599 |
#else |
| 3600 |
FLAC__ASSERT(0); |
| 3601 |
#endif |
| 3602 |
} |
| 3603 |
|
| 3604 |
/* |
| 3605 |
* Get ready for the next frame |
| 3606 |
*/ |
| 3607 |
encoder->private_->current_sample_number = 0; |
| 3608 |
encoder->private_->current_frame_number++; |
| 3609 |
encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize; |
| 3610 |
|
| 3611 |
return true; |
| 3612 |
} |
| 3613 |
|
| 3614 |
#ifdef HAVE_PTHREAD |
| 3615 |
void * process_frame_thread_(void * args) { |
| 3616 |
FLAC__StreamEncoder * encoder = args; |
| 3617 |
uint32_t channel; |
| 3618 |
|
| 3619 |
pthread_mutex_lock(&encoder->private_->mutex_work_queue); |
| 3620 |
encoder->private_->num_running_threads++; |
| 3621 |
pthread_mutex_unlock(&encoder->private_->mutex_work_queue); |
| 3622 |
|
| 3623 |
while(1) { |
| 3624 |
pthread_mutex_lock(&encoder->private_->mutex_work_queue); |
| 3625 |
if(encoder->private_->finish_work_threads) { |
| 3626 |
pthread_mutex_unlock(&encoder->private_->mutex_work_queue); |
| 3627 |
return NULL; |
| 3628 |
} |
| 3629 |
/* The code below pauses and restarts threads if it is noticed threads are often put too sleep |
| 3630 |
* because of a lack of work. This reduces overhead when too many threads are active. The |
| 3631 |
* overcommited indicator is increased when no tasks are available, decreased when more tasks |
| 3632 |
* are available then threads are running, and reset when a thread is woken up or put to sleep */ |
| 3633 |
if(encoder->private_->num_available_threadtasks == 0) |
| 3634 |
encoder->private_->overcommitted_indicator++; |
| 3635 |
else if(encoder->private_->num_available_threadtasks > encoder->private_->num_running_threads) |
| 3636 |
encoder->private_->overcommitted_indicator--; |
| 3637 |
if(encoder->private_->overcommitted_indicator < -20) { |
| 3638 |
encoder->private_->overcommitted_indicator = 0; |
| 3639 |
pthread_cond_signal(&encoder->private_->cond_wake_up_thread); |
| 3640 |
} |
| 3641 |
else if(encoder->private_->overcommitted_indicator > 20 && encoder->private_->num_running_threads > 2) { |
| 3642 |
encoder->private_->overcommitted_indicator = 0; |
| 3643 |
encoder->private_->num_running_threads--; |
| 3644 |
pthread_cond_wait(&encoder->private_->cond_wake_up_thread, &encoder->private_->mutex_work_queue); |
| 3645 |
encoder->private_->num_running_threads++; |
| 3646 |
} |
| 3647 |
while(encoder->private_->num_available_threadtasks == 0 && (encoder->private_->md5_active || encoder->private_->md5_fifo.tail == 0)) { |
| 3648 |
if(encoder->private_->finish_work_threads) { |
| 3649 |
pthread_mutex_unlock(&encoder->private_->mutex_work_queue); |
| 3650 |
return NULL; |
| 3651 |
} |
| 3652 |
pthread_cond_wait(&encoder->private_->cond_work_available, &encoder->private_->mutex_work_queue); |
| 3653 |
} |
| 3654 |
if(encoder->protected_->do_md5 && !encoder->private_->md5_active && encoder->private_->md5_fifo.tail > 0) { |
| 3655 |
uint32_t length = 0; |
| 3656 |
encoder->private_->md5_active = true; |
| 3657 |
while(encoder->private_->md5_fifo.tail > 0) { |
| 3658 |
length = encoder->private_->md5_fifo.tail; |
| 3659 |
pthread_mutex_unlock(&encoder->private_->mutex_work_queue); |
| 3660 |
if(!FLAC__MD5Accumulate(&encoder->private_->md5context, (const FLAC__int32 * const *)encoder->private_->md5_fifo.data, encoder->protected_->channels, length, (encoder->protected_->bits_per_sample+7) / 8)) { |
| 3661 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 3662 |
return NULL; |
| 3663 |
} |
| 3664 |
pthread_mutex_lock(&encoder->private_->mutex_md5_fifo); |
| 3665 |
for(channel = 0; channel < encoder->protected_->channels; channel++) |
| 3666 |
memmove(&encoder->private_->md5_fifo.data[channel][0], &encoder->private_->md5_fifo.data[channel][length], (encoder->private_->md5_fifo.tail-length) * sizeof(encoder->private_->md5_fifo.data[0][0])); |
| 3667 |
pthread_mutex_lock(&encoder->private_->mutex_work_queue); |
| 3668 |
encoder->private_->md5_fifo.tail -= length; |
| 3669 |
pthread_cond_signal(&encoder->private_->cond_md5_emptied); |
| 3670 |
pthread_mutex_unlock(&encoder->private_->mutex_md5_fifo); |
| 3671 |
} |
| 3672 |
encoder->private_->md5_active = false; |
| 3673 |
pthread_mutex_unlock(&encoder->private_->mutex_work_queue); |
| 3674 |
} |
| 3675 |
else if(encoder->private_->num_available_threadtasks > 0) { |
| 3676 |
FLAC__StreamEncoderThreadTask * task = NULL; |
| 3677 |
task = encoder->private_->threadtask[encoder->private_->next_threadtask]; |
| 3678 |
encoder->private_->num_available_threadtasks--; |
| 3679 |
encoder->private_->next_threadtask++; |
| 3680 |
if(encoder->private_->next_threadtask == encoder->private_->num_threadtasks) |
| 3681 |
encoder->private_->next_threadtask = 1; |
| 3682 |
pthread_mutex_unlock(&encoder->private_->mutex_work_queue); |
| 3683 |
pthread_mutex_lock(&task->mutex_this_task); |
| 3684 |
if(!process_frame_thread_inner_(encoder, task)) |
| 3685 |
return NULL; |
| 3686 |
} |
| 3687 |
else { |
| 3688 |
pthread_mutex_unlock(&encoder->private_->mutex_work_queue); |
| 3689 |
} |
| 3690 |
} |
| 3691 |
} |
| 3692 |
|
| 3693 |
FLAC__bool process_frame_thread_inner_(FLAC__StreamEncoder * encoder, FLAC__StreamEncoderThreadTask * task) { |
| 3694 |
FLAC__bool ok = true; |
| 3695 |
FLAC__uint16 crc; |
| 3696 |
|
| 3697 |
/* |
| 3698 |
* Process the frame header and subframes into the frame bitbuffer |
| 3699 |
*/ |
| 3700 |
if(ok && !process_subframes_(encoder, task)) { |
| 3701 |
/* the above function sets the state for us in case of an error */ |
| 3702 |
ok = false; |
| 3703 |
} |
| 3704 |
|
| 3705 |
/* |
| 3706 |
* Zero-pad the frame to a byte_boundary |
| 3707 |
*/ |
| 3708 |
if(ok && !FLAC__bitwriter_zero_pad_to_byte_boundary(task->frame)) { |
| 3709 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 3710 |
ok = false; |
| 3711 |
} |
| 3712 |
|
| 3713 |
/* |
| 3714 |
* CRC-16 the whole thing |
| 3715 |
*/ |
| 3716 |
FLAC__ASSERT(!ok || FLAC__bitwriter_is_byte_aligned(task->frame)); |
| 3717 |
if( |
| 3718 |
ok && |
| 3719 |
( |
| 3720 |
!FLAC__bitwriter_get_write_crc16(task->frame, &crc) || |
| 3721 |
!FLAC__bitwriter_write_raw_uint32(task->frame, crc, FLAC__FRAME_FOOTER_CRC_LEN) |
| 3722 |
) |
| 3723 |
) { |
| 3724 |
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 3725 |
ok = false; |
| 3726 |
} |
| 3727 |
task->returnvalue = ok; |
| 3728 |
task->task_done = true; |
| 3729 |
pthread_cond_signal(&task->cond_task_done); |
| 3730 |
pthread_mutex_unlock(&task->mutex_this_task); |
| 3731 |
return true; |
| 3732 |
} |
| 3733 |
#endif |
| 3734 |
|
| 3735 |
FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderThreadTask * threadtask) |
| 3736 |
{ |
| 3737 |
FLAC__FrameHeader frame_header; |
| 3738 |
uint32_t channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order; |
| 3739 |
FLAC__bool do_independent, do_mid_side, all_subframes_constant = true; |
| 3740 |
|
| 3741 |
threadtask->disable_constant_subframes = encoder->private_->disable_constant_subframes; |
| 3742 |
|
| 3743 |
/* |
| 3744 |
* Calculate the min,max Rice partition orders |
| 3745 |
*/ |
| 3746 |
|
| 3747 |
max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize); |
| 3748 |
max_partition_order = flac_min(max_partition_order, encoder->protected_->max_residual_partition_order); |
| 3749 |
min_partition_order = flac_min(min_partition_order, max_partition_order); |
| 3750 |
|
| 3751 |
/* |
| 3752 |
* Setup the frame |
| 3753 |
*/ |
| 3754 |
frame_header.blocksize = encoder->protected_->blocksize; |
| 3755 |
frame_header.sample_rate = encoder->protected_->sample_rate; |
| 3756 |
frame_header.channels = encoder->protected_->channels; |
| 3757 |
frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */ |
| 3758 |
frame_header.bits_per_sample = encoder->protected_->bits_per_sample; |
| 3759 |
frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER; |
| 3760 |
frame_header.number.frame_number = threadtask->current_frame_number; |
| 3761 |
|
| 3762 |
/* |
| 3763 |
* Figure out what channel assignments to try |
| 3764 |
*/ |
| 3765 |
if(encoder->protected_->do_mid_side_stereo) { |
| 3766 |
if(encoder->protected_->loose_mid_side_stereo) { |
| 3767 |
uint64_t sumAbsLR = 0, sumAbsMS = 0; |
| 3768 |
uint32_t i; |
| 3769 |
if(encoder->protected_->bits_per_sample < 25) { |
| 3770 |
for(i = 1; i < encoder->protected_->blocksize; i++) { |
| 3771 |
int32_t predictionLeft = threadtask->integer_signal[0][i] - threadtask->integer_signal[0][i-1]; |
| 3772 |
int32_t predictionRight = threadtask->integer_signal[1][i] - threadtask->integer_signal[1][i-1]; |
| 3773 |
sumAbsLR += abs(predictionLeft) + abs(predictionRight); |
| 3774 |
sumAbsMS += abs((predictionLeft + predictionRight) >> 1) + abs(predictionLeft - predictionRight); |
| 3775 |
} |
| 3776 |
} |
| 3777 |
else { /* bps 25 or higher */ |
| 3778 |
for(i = 1; i < encoder->protected_->blocksize; i++) { |
| 3779 |
int64_t predictionLeft = (int64_t)threadtask->integer_signal[0][i] - (int64_t)threadtask->integer_signal[0][i-1]; |
| 3780 |
int64_t predictionRight = (int64_t)threadtask->integer_signal[1][i] - (int64_t)threadtask->integer_signal[1][i-1]; |
| 3781 |
sumAbsLR += local_abs64(predictionLeft) + local_abs64(predictionRight); |
| 3782 |
sumAbsMS += local_abs64((predictionLeft + predictionRight) >> 1) + local_abs64(predictionLeft - predictionRight); |
| 3783 |
} |
| 3784 |
} |
| 3785 |
if(sumAbsLR < sumAbsMS) { |
| 3786 |
do_independent = true; |
| 3787 |
do_mid_side = false; |
| 3788 |
frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; |
| 3789 |
} |
| 3790 |
else { |
| 3791 |
do_independent = false; |
| 3792 |
do_mid_side = true; |
| 3793 |
frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE; |
| 3794 |
} |
| 3795 |
} |
| 3796 |
else { |
| 3797 |
do_independent = true; |
| 3798 |
do_mid_side = true; |
| 3799 |
} |
| 3800 |
} |
| 3801 |
else { |
| 3802 |
do_independent = true; |
| 3803 |
do_mid_side = false; |
| 3804 |
} |
| 3805 |
|
| 3806 |
FLAC__ASSERT(do_independent || do_mid_side); |
| 3807 |
|
| 3808 |
/* |
| 3809 |
* Prepare mid-side signals if applicable |
| 3810 |
*/ |
| 3811 |
if(do_mid_side) { |
| 3812 |
uint32_t i; |
| 3813 |
FLAC__ASSERT(encoder->protected_->channels == 2); |
| 3814 |
if(encoder->protected_->bits_per_sample < 32) |
| 3815 |
for(i = 0; i < encoder->protected_->blocksize; i++) { |
| 3816 |
threadtask->integer_signal_mid_side[1][i] = threadtask->integer_signal[0][i] - threadtask->integer_signal[1][i]; |
| 3817 |
threadtask->integer_signal_mid_side[0][i] = (threadtask->integer_signal[0][i] + threadtask->integer_signal[1][i]) >> 1; /* NOTE: not the same as 'mid = (signal[0][j] + signal[1][j]) / 2' ! */ |
| 3818 |
} |
| 3819 |
else |
| 3820 |
for(i = 0; i <= encoder->protected_->blocksize; i++) { |
| 3821 |
threadtask->integer_signal_33bit_side[i] = (FLAC__int64)threadtask->integer_signal[0][i] - (FLAC__int64)threadtask->integer_signal[1][i]; |
| 3822 |
threadtask->integer_signal_mid_side[0][i] = ((FLAC__int64)threadtask->integer_signal[0][i] + (FLAC__int64)threadtask->integer_signal[1][i]) >> 1; /* NOTE: not the same as 'mid = (signal[0][j] + signal[1][j]) / 2' ! */ |
| 3823 |
} |
| 3824 |
} |
| 3825 |
|
| 3826 |
|
| 3827 |
/* |
| 3828 |
* Check for wasted bits; set effective bps for each subframe |
| 3829 |
*/ |
| 3830 |
if(do_independent) { |
| 3831 |
for(channel = 0; channel < encoder->protected_->channels; channel++) { |
| 3832 |
uint32_t w = get_wasted_bits_(threadtask->integer_signal[channel], encoder->protected_->blocksize); |
| 3833 |
if (w > encoder->protected_->bits_per_sample) { |
| 3834 |
w = encoder->protected_->bits_per_sample; |
| 3835 |
} |
| 3836 |
threadtask->subframe_workspace[channel][0].wasted_bits = threadtask->subframe_workspace[channel][1].wasted_bits = w; |
| 3837 |
threadtask->subframe_bps[channel] = encoder->protected_->bits_per_sample - w; |
| 3838 |
} |
| 3839 |
} |
| 3840 |
if(do_mid_side) { |
| 3841 |
FLAC__ASSERT(encoder->protected_->channels == 2); |
| 3842 |
for(channel = 0; channel < 2; channel++) { |
| 3843 |
uint32_t w; |
| 3844 |
if(encoder->protected_->bits_per_sample < 32 || channel == 0) |
| 3845 |
w = get_wasted_bits_(threadtask->integer_signal_mid_side[channel], encoder->protected_->blocksize); |
| 3846 |
else |
| 3847 |
w = get_wasted_bits_wide_(threadtask->integer_signal_33bit_side, threadtask->integer_signal_mid_side[channel], encoder->protected_->blocksize); |
| 3848 |
|
| 3849 |
if (w > encoder->protected_->bits_per_sample) { |
| 3850 |
w = encoder->protected_->bits_per_sample; |
| 3851 |
} |
| 3852 |
threadtask->subframe_workspace_mid_side[channel][0].wasted_bits = threadtask->subframe_workspace_mid_side[channel][1].wasted_bits = w; |
| 3853 |
threadtask->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1); |
| 3854 |
} |
| 3855 |
} |
| 3856 |
|
| 3857 |
/* |
| 3858 |
* First do a normal encoding pass of each independent channel |
| 3859 |
*/ |
| 3860 |
if(do_independent) { |
| 3861 |
for(channel = 0; channel < encoder->protected_->channels; channel++) { |
| 3862 |
if(encoder->protected_->limit_min_bitrate && all_subframes_constant && (channel + 1) == encoder->protected_->channels){ |
| 3863 |
/* This frame contains only constant subframes at this point. |
| 3864 |
* To prevent the frame from becoming too small, make sure |
| 3865 |
* the last subframe isn't constant */ |
| 3866 |
threadtask->disable_constant_subframes = true; |
| 3867 |
} |
| 3868 |
if(! |
| 3869 |
process_subframe_( |
| 3870 |
encoder, |
| 3871 |
threadtask, |
| 3872 |
min_partition_order, |
| 3873 |
max_partition_order, |
| 3874 |
&frame_header, |
| 3875 |
threadtask->subframe_bps[channel], |
| 3876 |
threadtask->integer_signal[channel], |
| 3877 |
threadtask->subframe_workspace_ptr[channel], |
| 3878 |
threadtask->partitioned_rice_contents_workspace_ptr[channel], |
| 3879 |
threadtask->residual_workspace[channel], |
| 3880 |
threadtask->best_subframe+channel, |
| 3881 |
threadtask->best_subframe_bits+channel |
| 3882 |
) |
| 3883 |
) |
| 3884 |
return false; |
| 3885 |
if(threadtask->subframe_workspace[channel][threadtask->best_subframe[channel]].type != FLAC__SUBFRAME_TYPE_CONSTANT) |
| 3886 |
all_subframes_constant = false; |
| 3887 |
} |
| 3888 |
} |
| 3889 |
|
| 3890 |
/* |
| 3891 |
* Now do mid and side channels if requested |
| 3892 |
*/ |
| 3893 |
if(do_mid_side) { |
| 3894 |
FLAC__ASSERT(encoder->protected_->channels == 2); |
| 3895 |
|
| 3896 |
for(channel = 0; channel < 2; channel++) { |
| 3897 |
void *integer_signal_; |
| 3898 |
if(threadtask->subframe_bps_mid_side[channel] <= 32) |
| 3899 |
integer_signal_ = threadtask->integer_signal_mid_side[channel]; |
| 3900 |
else |
| 3901 |
integer_signal_ = threadtask->integer_signal_33bit_side; |
| 3902 |
if(! |
| 3903 |
process_subframe_( |
| 3904 |
encoder, |
| 3905 |
threadtask, |
| 3906 |
min_partition_order, |
| 3907 |
max_partition_order, |
| 3908 |
&frame_header, |
| 3909 |
threadtask->subframe_bps_mid_side[channel], |
| 3910 |
integer_signal_, |
| 3911 |
threadtask->subframe_workspace_ptr_mid_side[channel], |
| 3912 |
threadtask->partitioned_rice_contents_workspace_ptr_mid_side[channel], |
| 3913 |
threadtask->residual_workspace_mid_side[channel], |
| 3914 |
threadtask->best_subframe_mid_side+channel, |
| 3915 |
threadtask->best_subframe_bits_mid_side+channel |
| 3916 |
) |
| 3917 |
) |
| 3918 |
return false; |
| 3919 |
} |
| 3920 |
} |
| 3921 |
|
| 3922 |
/* |
| 3923 |
* Compose the frame bitbuffer |
| 3924 |
*/ |
| 3925 |
if((do_independent && do_mid_side) || encoder->protected_->loose_mid_side_stereo) { |
| 3926 |
uint32_t left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */ |
| 3927 |
FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */ |
| 3928 |
FLAC__ChannelAssignment channel_assignment; |
| 3929 |
|
| 3930 |
FLAC__ASSERT(encoder->protected_->channels == 2); |
| 3931 |
|
| 3932 |
if(!encoder->protected_->loose_mid_side_stereo) { |
| 3933 |
uint32_t bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */ |
| 3934 |
uint32_t min_bits; |
| 3935 |
int ca; |
| 3936 |
|
| 3937 |
FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT == 0); |
| 3938 |
FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE == 1); |
| 3939 |
FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE == 2); |
| 3940 |
FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_MID_SIDE == 3); |
| 3941 |
|
| 3942 |
/* We have to figure out which channel assignent results in the smallest frame */ |
| 3943 |
bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = threadtask->best_subframe_bits [0] + threadtask->best_subframe_bits [1]; |
| 3944 |
bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = threadtask->best_subframe_bits [0] + threadtask->best_subframe_bits_mid_side[1]; |
| 3945 |
bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = threadtask->best_subframe_bits [1] + threadtask->best_subframe_bits_mid_side[1]; |
| 3946 |
bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = threadtask->best_subframe_bits_mid_side[0] + threadtask->best_subframe_bits_mid_side[1]; |
| 3947 |
|
| 3948 |
channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; |
| 3949 |
min_bits = bits[channel_assignment]; |
| 3950 |
|
| 3951 |
/* When doing loose mid-side stereo, ignore left-side |
| 3952 |
* and right-side options */ |
| 3953 |
for(ca = 1; ca <= 3; ca++) { |
| 3954 |
if(bits[ca] < min_bits) { |
| 3955 |
min_bits = bits[ca]; |
| 3956 |
channel_assignment = (FLAC__ChannelAssignment)ca; |
| 3957 |
} |
| 3958 |
} |
| 3959 |
frame_header.channel_assignment = channel_assignment; |
| 3960 |
} |
| 3961 |
|
| 3962 |
if(!FLAC__frame_add_header(&frame_header, threadtask->frame)) { |
| 3963 |
encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 3964 |
return false; |
| 3965 |
} |
| 3966 |
|
| 3967 |
switch(frame_header.channel_assignment) { |
| 3968 |
case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
| 3969 |
left_subframe = &threadtask->subframe_workspace [0][threadtask->best_subframe [0]]; |
| 3970 |
right_subframe = &threadtask->subframe_workspace [1][threadtask->best_subframe [1]]; |
| 3971 |
break; |
| 3972 |
case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
| 3973 |
left_subframe = &threadtask->subframe_workspace [0][threadtask->best_subframe [0]]; |
| 3974 |
right_subframe = &threadtask->subframe_workspace_mid_side[1][threadtask->best_subframe_mid_side[1]]; |
| 3975 |
break; |
| 3976 |
case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
| 3977 |
left_subframe = &threadtask->subframe_workspace_mid_side[1][threadtask->best_subframe_mid_side[1]]; |
| 3978 |
right_subframe = &threadtask->subframe_workspace [1][threadtask->best_subframe [1]]; |
| 3979 |
break; |
| 3980 |
case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
| 3981 |
left_subframe = &threadtask->subframe_workspace_mid_side[0][threadtask->best_subframe_mid_side[0]]; |
| 3982 |
right_subframe = &threadtask->subframe_workspace_mid_side[1][threadtask->best_subframe_mid_side[1]]; |
| 3983 |
break; |
| 3984 |
default: |
| 3985 |
FLAC__ASSERT(0); |
| 3986 |
} |
| 3987 |
|
| 3988 |
switch(frame_header.channel_assignment) { |
| 3989 |
case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
| 3990 |
left_bps = threadtask->subframe_bps [0]; |
| 3991 |
right_bps = threadtask->subframe_bps [1]; |
| 3992 |
break; |
| 3993 |
case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
| 3994 |
left_bps = threadtask->subframe_bps [0]; |
| 3995 |
right_bps = threadtask->subframe_bps_mid_side[1]; |
| 3996 |
break; |
| 3997 |
case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
| 3998 |
left_bps = threadtask->subframe_bps_mid_side[1]; |
| 3999 |
right_bps = threadtask->subframe_bps [1]; |
| 4000 |
break; |
| 4001 |
case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
| 4002 |
left_bps = threadtask->subframe_bps_mid_side[0]; |
| 4003 |
right_bps = threadtask->subframe_bps_mid_side[1]; |
| 4004 |
break; |
| 4005 |
default: |
| 4006 |
FLAC__ASSERT(0); |
| 4007 |
} |
| 4008 |
|
| 4009 |
/* note that encoder_add_subframe_ sets the state for us in case of an error */ |
| 4010 |
if(!add_subframe_(encoder, frame_header.blocksize, left_bps , left_subframe , threadtask->frame)) |
| 4011 |
return false; |
| 4012 |
if(!add_subframe_(encoder, frame_header.blocksize, right_bps, right_subframe, threadtask->frame)) |
| 4013 |
return false; |
| 4014 |
} |
| 4015 |
else { |
| 4016 |
FLAC__ASSERT(do_independent); |
| 4017 |
if(!FLAC__frame_add_header(&frame_header, threadtask->frame)) { |
| 4018 |
encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 4019 |
return false; |
| 4020 |
} |
| 4021 |
|
| 4022 |
for(channel = 0; channel < encoder->protected_->channels; channel++) { |
| 4023 |
if(!add_subframe_(encoder, frame_header.blocksize, threadtask->subframe_bps[channel], &threadtask->subframe_workspace[channel][threadtask->best_subframe[channel]], threadtask->frame)) { |
| 4024 |
/* the above function sets the state for us in case of an error */ |
| 4025 |
return false; |
| 4026 |
} |
| 4027 |
} |
| 4028 |
} |
| 4029 |
|
| 4030 |
return true; |
| 4031 |
} |
| 4032 |
|
| 4033 |
FLAC__bool process_subframe_( |
| 4034 |
FLAC__StreamEncoder *encoder, |
| 4035 |
FLAC__StreamEncoderThreadTask *threadtask, |
| 4036 |
uint32_t min_partition_order, |
| 4037 |
uint32_t max_partition_order, |
| 4038 |
const FLAC__FrameHeader *frame_header, |
| 4039 |
uint32_t subframe_bps, |
| 4040 |
const void *integer_signal, |
| 4041 |
FLAC__Subframe *subframe[2], |
| 4042 |
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2], |
| 4043 |
FLAC__int32 *residual[2], |
| 4044 |
uint32_t *best_subframe, |
| 4045 |
uint32_t *best_bits |
| 4046 |
) |
| 4047 |
{ |
| 4048 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 4049 |
float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]; |
| 4050 |
#else |
| 4051 |
FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]; |
| 4052 |
#endif |
| 4053 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 4054 |
double lpc_residual_bits_per_sample; |
| 4055 |
apply_apodization_state_struct apply_apodization_state; |
| 4056 |
double lpc_error[FLAC__MAX_LPC_ORDER]; |
| 4057 |
uint32_t min_lpc_order, max_lpc_order, lpc_order, guess_lpc_order; |
| 4058 |
uint32_t min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision; |
| 4059 |
#endif |
| 4060 |
uint32_t min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order; |
| 4061 |
uint32_t _candidate_bits, _best_bits; |
| 4062 |
uint32_t _best_subframe; |
| 4063 |
/* only use RICE2 partitions if stream bps > 16 */ |
| 4064 |
const uint32_t rice_parameter_limit = FLAC__stream_encoder_get_bits_per_sample(encoder) > 16? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 4065 |
|
| 4066 |
FLAC__ASSERT(frame_header->blocksize > 0); |
| 4067 |
|
| 4068 |
/* verbatim subframe is the baseline against which we measure other compressed subframes */ |
| 4069 |
_best_subframe = 0; |
| 4070 |
if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) |
| 4071 |
_best_bits = UINT32_MAX; |
| 4072 |
else |
| 4073 |
_best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]); |
| 4074 |
*best_bits = _best_bits; |
| 4075 |
|
| 4076 |
if(frame_header->blocksize > FLAC__MAX_FIXED_ORDER) { |
| 4077 |
uint32_t signal_is_constant = false; |
| 4078 |
/* The next formula determines when to use a 64-bit accumulator |
| 4079 |
* for the error of a fixed predictor, and when a 32-bit one. As |
| 4080 |
* the error of a 4th order predictor for a given sample is the |
| 4081 |
* sum of 17 sample values (1+4+6+4+1) and there are blocksize - |
| 4082 |
* order error values to be summed, the maximum total error is |
| 4083 |
* maximum_sample_value * (blocksize - order) * 17. As ilog2(x) |
| 4084 |
* calculates floor(2log(x)), the result must be 31 or lower |
| 4085 |
*/ |
| 4086 |
if(subframe_bps < 28){ |
| 4087 |
if(subframe_bps + FLAC__bitmath_ilog2((frame_header->blocksize-FLAC__MAX_FIXED_ORDER)*17) < 32) |
| 4088 |
guess_fixed_order = encoder->private_->local_fixed_compute_best_predictor(((FLAC__int32 *)integer_signal)+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample); |
| 4089 |
else |
| 4090 |
guess_fixed_order = encoder->private_->local_fixed_compute_best_predictor_wide(((FLAC__int32 *)integer_signal)+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample); |
| 4091 |
} |
| 4092 |
else |
| 4093 |
if(subframe_bps <= 32) |
| 4094 |
guess_fixed_order = encoder->private_->local_fixed_compute_best_predictor_limit_residual(((FLAC__int32 *)integer_signal+FLAC__MAX_FIXED_ORDER),frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample); |
| 4095 |
else |
| 4096 |
guess_fixed_order = FLAC__fixed_compute_best_predictor_limit_residual_33bit(((FLAC__int64 *)integer_signal+FLAC__MAX_FIXED_ORDER),frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample); |
| 4097 |
|
| 4098 |
/* check for constant subframe */ |
| 4099 |
if( |
| 4100 |
!threadtask->disable_constant_subframes && |
| 4101 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 4102 |
fixed_residual_bits_per_sample[1] == 0.0 |
| 4103 |
#else |
| 4104 |
fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO |
| 4105 |
#endif |
| 4106 |
) { |
| 4107 |
/* the above means it's possible all samples are the same value; now double-check it: */ |
| 4108 |
uint32_t i; |
| 4109 |
signal_is_constant = true; |
| 4110 |
if(subframe_bps <= 32){ |
| 4111 |
const FLAC__int32 *integer_signal_ = integer_signal; |
| 4112 |
for(i = 1; i < frame_header->blocksize; i++) { |
| 4113 |
if(integer_signal_[0] != integer_signal_[i]) { |
| 4114 |
signal_is_constant = false; |
| 4115 |
break; |
| 4116 |
} |
| 4117 |
} |
| 4118 |
} |
| 4119 |
else { |
| 4120 |
const FLAC__int64 *integer_signal_ = integer_signal; |
| 4121 |
for(i = 1; i < frame_header->blocksize; i++) { |
| 4122 |
if(integer_signal_[0] != integer_signal_[i]) { |
| 4123 |
signal_is_constant = false; |
| 4124 |
break; |
| 4125 |
} |
| 4126 |
} |
| 4127 |
} |
| 4128 |
} |
| 4129 |
if(signal_is_constant) { |
| 4130 |
if(subframe_bps <= 32) |
| 4131 |
_candidate_bits = evaluate_constant_subframe_(encoder, ((FLAC__int32 *)integer_signal)[0], frame_header->blocksize, subframe_bps, subframe[!_best_subframe]); |
| 4132 |
else |
| 4133 |
_candidate_bits = evaluate_constant_subframe_(encoder, ((FLAC__int64 *)integer_signal)[0], frame_header->blocksize, subframe_bps, subframe[!_best_subframe]); |
| 4134 |
|
| 4135 |
if(_candidate_bits < _best_bits) { |
| 4136 |
_best_subframe = !_best_subframe; |
| 4137 |
_best_bits = _candidate_bits; |
| 4138 |
} |
| 4139 |
} |
| 4140 |
else { |
| 4141 |
if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) { |
| 4142 |
/* encode fixed */ |
| 4143 |
if(encoder->protected_->do_exhaustive_model_search) { |
| 4144 |
min_fixed_order = 0; |
| 4145 |
max_fixed_order = FLAC__MAX_FIXED_ORDER; |
| 4146 |
} |
| 4147 |
else { |
| 4148 |
min_fixed_order = max_fixed_order = guess_fixed_order; |
| 4149 |
} |
| 4150 |
if(max_fixed_order >= frame_header->blocksize) |
| 4151 |
max_fixed_order = frame_header->blocksize - 1; |
| 4152 |
for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) { |
| 4153 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 4154 |
if(fixed_residual_bits_per_sample[fixed_order] >= (float)subframe_bps) |
| 4155 |
continue; /* don't even try */ |
| 4156 |
#else |
| 4157 |
if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps) |
| 4158 |
continue; /* don't even try */ |
| 4159 |
#endif |
| 4160 |
_candidate_bits = |
| 4161 |
evaluate_fixed_subframe_( |
| 4162 |
encoder, |
| 4163 |
threadtask, |
| 4164 |
integer_signal, |
| 4165 |
residual[!_best_subframe], |
| 4166 |
threadtask->abs_residual_partition_sums, |
| 4167 |
threadtask->raw_bits_per_partition, |
| 4168 |
frame_header->blocksize, |
| 4169 |
subframe_bps, |
| 4170 |
fixed_order, |
| 4171 |
rice_parameter_limit, |
| 4172 |
min_partition_order, |
| 4173 |
max_partition_order, |
| 4174 |
encoder->protected_->do_escape_coding, |
| 4175 |
encoder->protected_->rice_parameter_search_dist, |
| 4176 |
subframe[!_best_subframe], |
| 4177 |
partitioned_rice_contents[!_best_subframe] |
| 4178 |
); |
| 4179 |
if(_candidate_bits < _best_bits) { |
| 4180 |
_best_subframe = !_best_subframe; |
| 4181 |
_best_bits = _candidate_bits; |
| 4182 |
} |
| 4183 |
} |
| 4184 |
} |
| 4185 |
|
| 4186 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 4187 |
/* encode lpc */ |
| 4188 |
if(encoder->protected_->max_lpc_order > 0) { |
| 4189 |
if(encoder->protected_->max_lpc_order >= frame_header->blocksize) |
| 4190 |
max_lpc_order = frame_header->blocksize-1; |
| 4191 |
else |
| 4192 |
max_lpc_order = encoder->protected_->max_lpc_order; |
| 4193 |
if(max_lpc_order > 0) { |
| 4194 |
apply_apodization_state.a = 0; |
| 4195 |
apply_apodization_state.b = 1; |
| 4196 |
apply_apodization_state.c = 0; |
| 4197 |
while (apply_apodization_state.a < encoder->protected_->num_apodizations) { |
| 4198 |
uint32_t max_lpc_order_this_apodization = max_lpc_order; |
| 4199 |
|
| 4200 |
if(!apply_apodization_(encoder, threadtask, &apply_apodization_state, |
| 4201 |
frame_header->blocksize, lpc_error, |
| 4202 |
&max_lpc_order_this_apodization, |
| 4203 |
subframe_bps, integer_signal, |
| 4204 |
&guess_lpc_order)) |
| 4205 |
/* If apply_apodization_ fails, try next apodization */ |
| 4206 |
continue; |
| 4207 |
|
| 4208 |
if(encoder->protected_->do_exhaustive_model_search) { |
| 4209 |
min_lpc_order = 1; |
| 4210 |
} |
| 4211 |
else { |
| 4212 |
min_lpc_order = max_lpc_order_this_apodization = guess_lpc_order; |
| 4213 |
} |
| 4214 |
for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order_this_apodization; lpc_order++) { |
| 4215 |
lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order); |
| 4216 |
if(lpc_residual_bits_per_sample >= (double)subframe_bps) |
| 4217 |
continue; /* don't even try */ |
| 4218 |
if(encoder->protected_->do_qlp_coeff_prec_search) { |
| 4219 |
min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION; |
| 4220 |
/* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps(+1bps for side channel) streams */ |
| 4221 |
if(subframe_bps <= 17) { |
| 4222 |
max_qlp_coeff_precision = flac_min(32 - subframe_bps - FLAC__bitmath_ilog2(lpc_order), FLAC__MAX_QLP_COEFF_PRECISION); |
| 4223 |
max_qlp_coeff_precision = flac_max(max_qlp_coeff_precision, min_qlp_coeff_precision); |
| 4224 |
} |
| 4225 |
else |
| 4226 |
max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION; |
| 4227 |
} |
| 4228 |
else { |
| 4229 |
min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision; |
| 4230 |
} |
| 4231 |
for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) { |
| 4232 |
_candidate_bits = |
| 4233 |
evaluate_lpc_subframe_( |
| 4234 |
encoder, |
| 4235 |
threadtask, |
| 4236 |
integer_signal, |
| 4237 |
residual[!_best_subframe], |
| 4238 |
threadtask->abs_residual_partition_sums, |
| 4239 |
threadtask->raw_bits_per_partition, |
| 4240 |
threadtask->lp_coeff[lpc_order-1], |
| 4241 |
frame_header->blocksize, |
| 4242 |
subframe_bps, |
| 4243 |
lpc_order, |
| 4244 |
qlp_coeff_precision, |
| 4245 |
rice_parameter_limit, |
| 4246 |
min_partition_order, |
| 4247 |
max_partition_order, |
| 4248 |
encoder->protected_->do_escape_coding, |
| 4249 |
encoder->protected_->rice_parameter_search_dist, |
| 4250 |
subframe[!_best_subframe], |
| 4251 |
partitioned_rice_contents[!_best_subframe] |
| 4252 |
); |
| 4253 |
if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */ |
| 4254 |
if(_candidate_bits < _best_bits) { |
| 4255 |
_best_subframe = !_best_subframe; |
| 4256 |
_best_bits = _candidate_bits; |
| 4257 |
} |
| 4258 |
} |
| 4259 |
} |
| 4260 |
} |
| 4261 |
} |
| 4262 |
} |
| 4263 |
} |
| 4264 |
#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ |
| 4265 |
} |
| 4266 |
} |
| 4267 |
|
| 4268 |
/* under rare circumstances this can happen when all but lpc subframe types are disabled: */ |
| 4269 |
if(_best_bits == UINT32_MAX) { |
| 4270 |
FLAC__ASSERT(_best_subframe == 0); |
| 4271 |
_best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]); |
| 4272 |
} |
| 4273 |
|
| 4274 |
*best_subframe = _best_subframe; |
| 4275 |
*best_bits = _best_bits; |
| 4276 |
|
| 4277 |
return true; |
| 4278 |
} |
| 4279 |
|
| 4280 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 4281 |
static inline void set_next_subdivide_tukey(FLAC__int32 parts, uint32_t * apodizations, uint32_t * current_depth, uint32_t * current_part){ |
| 4282 |
// current_part is interleaved: even are partial, odd are punchout |
| 4283 |
if(*current_depth == 2){ |
| 4284 |
// For depth 2, we only do partial, no punchout as that is almost redundant |
| 4285 |
if(*current_part == 0){ |
| 4286 |
*current_part = 2; |
| 4287 |
}else{ /* *current_path == 2 */ |
| 4288 |
*current_part = 0; |
| 4289 |
(*current_depth)++; |
| 4290 |
} |
| 4291 |
}else if((*current_part) < (2*(*current_depth)-1)){ |
| 4292 |
(*current_part)++; |
| 4293 |
}else{ /* (*current_part) >= (2*(*current_depth)-1) */ |
| 4294 |
*current_part = 0; |
| 4295 |
(*current_depth)++; |
| 4296 |
} |
| 4297 |
|
| 4298 |
/* Now check if we are done with this SUBDIVIDE_TUKEY apodization */ |
| 4299 |
if(*current_depth > (uint32_t) parts){ |
| 4300 |
(*apodizations)++; |
| 4301 |
*current_depth = 1; |
| 4302 |
*current_part = 0; |
| 4303 |
} |
| 4304 |
} |
| 4305 |
|
| 4306 |
FLAC__bool apply_apodization_(FLAC__StreamEncoder *encoder, |
| 4307 |
FLAC__StreamEncoderThreadTask *threadtask, |
| 4308 |
apply_apodization_state_struct *apply_apodization_state, |
| 4309 |
uint32_t blocksize, |
| 4310 |
double *lpc_error, |
| 4311 |
uint32_t *max_lpc_order_this_apodization, |
| 4312 |
uint32_t subframe_bps, |
| 4313 |
const void *integer_signal, |
| 4314 |
uint32_t *guess_lpc_order) |
| 4315 |
{ |
| 4316 |
apply_apodization_state->current_apodization = &encoder->protected_->apodizations[apply_apodization_state->a]; |
| 4317 |
|
| 4318 |
if(apply_apodization_state->b == 1) { |
| 4319 |
/* window full subblock */ |
| 4320 |
if(subframe_bps <= 32) |
| 4321 |
FLAC__lpc_window_data(integer_signal, encoder->private_->window[apply_apodization_state->a], threadtask->windowed_signal, blocksize); |
| 4322 |
else |
| 4323 |
FLAC__lpc_window_data_wide(integer_signal, encoder->private_->window[apply_apodization_state->a], threadtask->windowed_signal, blocksize); |
| 4324 |
encoder->private_->local_lpc_compute_autocorrelation(threadtask->windowed_signal, blocksize, (*max_lpc_order_this_apodization)+1, apply_apodization_state->autoc); |
| 4325 |
if(apply_apodization_state->current_apodization->type == FLAC__APODIZATION_SUBDIVIDE_TUKEY){ |
| 4326 |
uint32_t i; |
| 4327 |
for(i = 0; i < *max_lpc_order_this_apodization; i++) |
| 4328 |
memcpy(apply_apodization_state->autoc_root, apply_apodization_state->autoc, *max_lpc_order_this_apodization*sizeof(apply_apodization_state->autoc[0])); |
| 4329 |
|
| 4330 |
(apply_apodization_state->b)++; |
| 4331 |
}else{ |
| 4332 |
(apply_apodization_state->a)++; |
| 4333 |
} |
| 4334 |
} |
| 4335 |
else { |
| 4336 |
/* window part of subblock */ |
| 4337 |
if(blocksize/apply_apodization_state->b <= FLAC__MAX_LPC_ORDER) { |
| 4338 |
/* intrinsics autocorrelation routines do not all handle cases in which lag might be |
| 4339 |
* larger than data_len, and some routines round lag up to the nearest multiple of 4 |
| 4340 |
* As little gain is expected from using LPC on part of a signal as small as 32 samples |
| 4341 |
* and to enable widening this rounding up to larger values in the future, windowing |
| 4342 |
* parts smaller than or equal to FLAC__MAX_LPC_ORDER (which is 32) samples is not supported */ |
| 4343 |
set_next_subdivide_tukey(apply_apodization_state->current_apodization->parameters.subdivide_tukey.parts, &apply_apodization_state->a, &apply_apodization_state->b, &apply_apodization_state->c); |
| 4344 |
return false; |
| 4345 |
} |
| 4346 |
if(!(apply_apodization_state->c % 2)) { |
| 4347 |
/* on even c, evaluate the (c/2)th partial window of size blocksize/b */ |
| 4348 |
if(subframe_bps <= 32) |
| 4349 |
FLAC__lpc_window_data_partial(integer_signal, encoder->private_->window[apply_apodization_state->a], threadtask->windowed_signal, blocksize, blocksize/apply_apodization_state->b/2, (apply_apodization_state->c/2*blocksize)/apply_apodization_state->b); |
| 4350 |
else |
| 4351 |
FLAC__lpc_window_data_partial_wide(integer_signal, encoder->private_->window[apply_apodization_state->a], threadtask->windowed_signal, blocksize, blocksize/apply_apodization_state->b/2, (apply_apodization_state->c/2*blocksize)/apply_apodization_state->b); |
| 4352 |
encoder->private_->local_lpc_compute_autocorrelation(threadtask->windowed_signal, blocksize/apply_apodization_state->b, (*max_lpc_order_this_apodization)+1, apply_apodization_state->autoc); |
| 4353 |
} |
| 4354 |
else { |
| 4355 |
/* on uneven c, evaluate the root window (over the whole block) minus the previous partial window |
| 4356 |
* similar to tukey_punchout apodization but more efficient */ |
| 4357 |
uint32_t i; |
| 4358 |
for(i = 0; i < *max_lpc_order_this_apodization; i++) |
| 4359 |
apply_apodization_state->autoc[i] = apply_apodization_state->autoc_root[i] - apply_apodization_state->autoc[i]; |
| 4360 |
} |
| 4361 |
/* Next function sets a, b and c appropriate for next iteration */ |
| 4362 |
set_next_subdivide_tukey(apply_apodization_state->current_apodization->parameters.subdivide_tukey.parts, &apply_apodization_state->a, &apply_apodization_state->b, &apply_apodization_state->c); |
| 4363 |
} |
| 4364 |
|
| 4365 |
if(apply_apodization_state->autoc[0] == 0.0) /* Signal seems to be constant, so we can't do lp. Constant detection is probably disabled */ |
| 4366 |
return false; |
| 4367 |
FLAC__lpc_compute_lp_coefficients(apply_apodization_state->autoc, max_lpc_order_this_apodization, threadtask->lp_coeff, lpc_error); |
| 4368 |
*guess_lpc_order = |
| 4369 |
FLAC__lpc_compute_best_order( |
| 4370 |
lpc_error, |
| 4371 |
*max_lpc_order_this_apodization, |
| 4372 |
blocksize, |
| 4373 |
subframe_bps + ( |
| 4374 |
encoder->protected_->do_qlp_coeff_prec_search? |
| 4375 |
FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */ |
| 4376 |
encoder->protected_->qlp_coeff_precision |
| 4377 |
) |
| 4378 |
); |
| 4379 |
return true; |
| 4380 |
} |
| 4381 |
#endif |
| 4382 |
|
| 4383 |
FLAC__bool add_subframe_( |
| 4384 |
FLAC__StreamEncoder *encoder, |
| 4385 |
uint32_t blocksize, |
| 4386 |
uint32_t subframe_bps, |
| 4387 |
const FLAC__Subframe *subframe, |
| 4388 |
FLAC__BitWriter *frame |
| 4389 |
) |
| 4390 |
{ |
| 4391 |
switch(subframe->type) { |
| 4392 |
case FLAC__SUBFRAME_TYPE_CONSTANT: |
| 4393 |
if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) { |
| 4394 |
encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 4395 |
return false; |
| 4396 |
} |
| 4397 |
break; |
| 4398 |
case FLAC__SUBFRAME_TYPE_FIXED: |
| 4399 |
if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) { |
| 4400 |
encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 4401 |
return false; |
| 4402 |
} |
| 4403 |
break; |
| 4404 |
case FLAC__SUBFRAME_TYPE_LPC: |
| 4405 |
if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) { |
| 4406 |
encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 4407 |
return false; |
| 4408 |
} |
| 4409 |
break; |
| 4410 |
case FLAC__SUBFRAME_TYPE_VERBATIM: |
| 4411 |
if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), blocksize, subframe_bps, subframe->wasted_bits, frame)) { |
| 4412 |
encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 4413 |
return false; |
| 4414 |
} |
| 4415 |
break; |
| 4416 |
default: |
| 4417 |
FLAC__ASSERT(0); |
| 4418 |
} |
| 4419 |
|
| 4420 |
return true; |
| 4421 |
} |
| 4422 |
|
| 4423 |
#define SPOTCHECK_ESTIMATE 0 |
| 4424 |
#if SPOTCHECK_ESTIMATE |
| 4425 |
static void spotcheck_subframe_estimate_( |
| 4426 |
FLAC__StreamEncoder *encoder, |
| 4427 |
uint32_t blocksize, |
| 4428 |
uint32_t subframe_bps, |
| 4429 |
const FLAC__Subframe *subframe, |
| 4430 |
uint32_t estimate |
| 4431 |
) |
| 4432 |
{ |
| 4433 |
FLAC__bool ret; |
| 4434 |
FLAC__BitWriter *frame = FLAC__bitwriter_new(); |
| 4435 |
if(frame == 0) { |
| 4436 |
flac_fprintf(stderr, "EST: can't allocate frame\n"); |
| 4437 |
return; |
| 4438 |
} |
| 4439 |
if(!FLAC__bitwriter_init(frame)) { |
| 4440 |
flac_fprintf(stderr, "EST: can't init frame\n"); |
| 4441 |
return; |
| 4442 |
} |
| 4443 |
ret = add_subframe_(encoder, blocksize, subframe_bps, subframe, frame); |
| 4444 |
FLAC__ASSERT(ret); |
| 4445 |
{ |
| 4446 |
const uint32_t actual = FLAC__bitwriter_get_input_bits_unconsumed(frame); |
| 4447 |
if(estimate != actual) |
| 4448 |
flac_fprintf(stderr, "EST: bad, frame#%u sub#%%d type=%8s est=%u, actual=%u, delta=%d\n", encoder->private_->current_frame_number, FLAC__SubframeTypeString[subframe->type], estimate, actual, (int)actual-(int)estimate); |
| 4449 |
} |
| 4450 |
FLAC__bitwriter_delete(frame); |
| 4451 |
} |
| 4452 |
#endif |
| 4453 |
|
| 4454 |
uint32_t evaluate_constant_subframe_( |
| 4455 |
FLAC__StreamEncoder *encoder, |
| 4456 |
const FLAC__int64 signal, |
| 4457 |
uint32_t blocksize, |
| 4458 |
uint32_t subframe_bps, |
| 4459 |
FLAC__Subframe *subframe |
| 4460 |
) |
| 4461 |
{ |
| 4462 |
uint32_t estimate; |
| 4463 |
subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT; |
| 4464 |
subframe->data.constant.value = signal; |
| 4465 |
|
| 4466 |
estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + subframe_bps; |
| 4467 |
|
| 4468 |
#if SPOTCHECK_ESTIMATE |
| 4469 |
spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate); |
| 4470 |
#else |
| 4471 |
(void)encoder, (void)blocksize; |
| 4472 |
#endif |
| 4473 |
|
| 4474 |
return estimate; |
| 4475 |
} |
| 4476 |
|
| 4477 |
uint32_t evaluate_fixed_subframe_( |
| 4478 |
FLAC__StreamEncoder *encoder, |
| 4479 |
FLAC__StreamEncoderThreadTask *threadtask, |
| 4480 |
const void *signal, |
| 4481 |
FLAC__int32 residual[], |
| 4482 |
FLAC__uint64 abs_residual_partition_sums[], |
| 4483 |
uint32_t raw_bits_per_partition[], |
| 4484 |
uint32_t blocksize, |
| 4485 |
uint32_t subframe_bps, |
| 4486 |
uint32_t order, |
| 4487 |
uint32_t rice_parameter_limit, |
| 4488 |
uint32_t min_partition_order, |
| 4489 |
uint32_t max_partition_order, |
| 4490 |
FLAC__bool do_escape_coding, |
| 4491 |
uint32_t rice_parameter_search_dist, |
| 4492 |
FLAC__Subframe *subframe, |
| 4493 |
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
| 4494 |
) |
| 4495 |
{ |
| 4496 |
uint32_t i, residual_bits, estimate; |
| 4497 |
const uint32_t residual_samples = blocksize - order; |
| 4498 |
|
| 4499 |
if((subframe_bps + order) <= 32) |
| 4500 |
FLAC__fixed_compute_residual(((FLAC__int32 *)signal)+order, residual_samples, order, residual); |
| 4501 |
else if(subframe_bps <= 32) |
| 4502 |
FLAC__fixed_compute_residual_wide(((FLAC__int32 *)signal)+order, residual_samples, order, residual); |
| 4503 |
else |
| 4504 |
FLAC__fixed_compute_residual_wide_33bit(((FLAC__int64 *)signal)+order, residual_samples, order, residual); |
| 4505 |
|
| 4506 |
subframe->type = FLAC__SUBFRAME_TYPE_FIXED; |
| 4507 |
|
| 4508 |
subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
| 4509 |
subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents; |
| 4510 |
subframe->data.fixed.residual = residual; |
| 4511 |
|
| 4512 |
residual_bits = |
| 4513 |
find_best_partition_order_( |
| 4514 |
encoder->private_, |
| 4515 |
threadtask, |
| 4516 |
residual, |
| 4517 |
abs_residual_partition_sums, |
| 4518 |
raw_bits_per_partition, |
| 4519 |
residual_samples, |
| 4520 |
order, |
| 4521 |
rice_parameter_limit, |
| 4522 |
min_partition_order, |
| 4523 |
max_partition_order, |
| 4524 |
subframe_bps, |
| 4525 |
do_escape_coding, |
| 4526 |
rice_parameter_search_dist, |
| 4527 |
&subframe->data.fixed.entropy_coding_method |
| 4528 |
); |
| 4529 |
|
| 4530 |
subframe->data.fixed.order = order; |
| 4531 |
if(subframe_bps <= 32) |
| 4532 |
for(i = 0; i < order; i++) |
| 4533 |
subframe->data.fixed.warmup[i] = ((FLAC__int32 *)signal)[i]; |
| 4534 |
else |
| 4535 |
for(i = 0; i < order; i++) |
| 4536 |
subframe->data.fixed.warmup[i] = ((FLAC__int64 *)signal)[i]; |
| 4537 |
|
| 4538 |
estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (order * subframe_bps); |
| 4539 |
if(residual_bits < UINT32_MAX - estimate) // To make sure estimate doesn't overflow |
| 4540 |
estimate += residual_bits; |
| 4541 |
else |
| 4542 |
estimate = UINT32_MAX; |
| 4543 |
|
| 4544 |
#if SPOTCHECK_ESTIMATE |
| 4545 |
spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate); |
| 4546 |
#endif |
| 4547 |
|
| 4548 |
return estimate; |
| 4549 |
} |
| 4550 |
|
| 4551 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 4552 |
uint32_t evaluate_lpc_subframe_( |
| 4553 |
FLAC__StreamEncoder *encoder, |
| 4554 |
FLAC__StreamEncoderThreadTask *threadtask, |
| 4555 |
const void *signal, |
| 4556 |
FLAC__int32 residual[], |
| 4557 |
FLAC__uint64 abs_residual_partition_sums[], |
| 4558 |
uint32_t raw_bits_per_partition[], |
| 4559 |
const FLAC__real lp_coeff[], |
| 4560 |
uint32_t blocksize, |
| 4561 |
uint32_t subframe_bps, |
| 4562 |
uint32_t order, |
| 4563 |
uint32_t qlp_coeff_precision, |
| 4564 |
uint32_t rice_parameter_limit, |
| 4565 |
uint32_t min_partition_order, |
| 4566 |
uint32_t max_partition_order, |
| 4567 |
FLAC__bool do_escape_coding, |
| 4568 |
uint32_t rice_parameter_search_dist, |
| 4569 |
FLAC__Subframe *subframe, |
| 4570 |
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
| 4571 |
) |
| 4572 |
{ |
| 4573 |
FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER]; /* WATCHOUT: the size is important; some x86 intrinsic routines need more than lpc order elements */ |
| 4574 |
uint32_t i, residual_bits, estimate; |
| 4575 |
int quantization, ret; |
| 4576 |
const uint32_t residual_samples = blocksize - order; |
| 4577 |
|
| 4578 |
/* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps(+1bps for side channel) streams */ |
| 4579 |
if(subframe_bps <= 17) { |
| 4580 |
FLAC__ASSERT(order > 0); |
| 4581 |
FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER); |
| 4582 |
qlp_coeff_precision = flac_min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order)); |
| 4583 |
} |
| 4584 |
|
| 4585 |
ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization); |
| 4586 |
if(ret != 0) |
| 4587 |
return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */ |
| 4588 |
|
| 4589 |
if(FLAC__lpc_max_residual_bps(subframe_bps, qlp_coeff, order, quantization) > 32) { |
| 4590 |
if(subframe_bps <= 32){ |
| 4591 |
if(!FLAC__lpc_compute_residual_from_qlp_coefficients_limit_residual(((FLAC__int32 *)signal)+order, residual_samples, qlp_coeff, order, quantization, residual)) |
| 4592 |
return 0; |
| 4593 |
} |
| 4594 |
else |
| 4595 |
if(!FLAC__lpc_compute_residual_from_qlp_coefficients_limit_residual_33bit(((FLAC__int64 *)signal)+order, residual_samples, qlp_coeff, order, quantization, residual)) |
| 4596 |
return 0; |
| 4597 |
} |
| 4598 |
else |
| 4599 |
if(FLAC__lpc_max_prediction_before_shift_bps(subframe_bps, qlp_coeff, order) <= 32) |
| 4600 |
if(subframe_bps <= 16 && qlp_coeff_precision <= 16) |
| 4601 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(((FLAC__int32 *)signal)+order, residual_samples, qlp_coeff, order, quantization, residual); |
| 4602 |
else |
| 4603 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(((FLAC__int32 *)signal)+order, residual_samples, qlp_coeff, order, quantization, residual); |
| 4604 |
else |
| 4605 |
encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(((FLAC__int32 *)signal)+order, residual_samples, qlp_coeff, order, quantization, residual); |
| 4606 |
|
| 4607 |
subframe->type = FLAC__SUBFRAME_TYPE_LPC; |
| 4608 |
|
| 4609 |
subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
| 4610 |
subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents; |
| 4611 |
subframe->data.lpc.residual = residual; |
| 4612 |
|
| 4613 |
residual_bits = |
| 4614 |
find_best_partition_order_( |
| 4615 |
encoder->private_, |
| 4616 |
threadtask, |
| 4617 |
residual, |
| 4618 |
abs_residual_partition_sums, |
| 4619 |
raw_bits_per_partition, |
| 4620 |
residual_samples, |
| 4621 |
order, |
| 4622 |
rice_parameter_limit, |
| 4623 |
min_partition_order, |
| 4624 |
max_partition_order, |
| 4625 |
subframe_bps, |
| 4626 |
do_escape_coding, |
| 4627 |
rice_parameter_search_dist, |
| 4628 |
&subframe->data.lpc.entropy_coding_method |
| 4629 |
); |
| 4630 |
|
| 4631 |
subframe->data.lpc.order = order; |
| 4632 |
subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision; |
| 4633 |
subframe->data.lpc.quantization_level = quantization; |
| 4634 |
memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER); |
| 4635 |
if(subframe_bps <= 32) |
| 4636 |
for(i = 0; i < order; i++) |
| 4637 |
subframe->data.lpc.warmup[i] = ((FLAC__int32 *)signal)[i]; |
| 4638 |
else |
| 4639 |
for(i = 0; i < order; i++) |
| 4640 |
subframe->data.lpc.warmup[i] = ((FLAC__int64 *)signal)[i]; |
| 4641 |
|
| 4642 |
|
| 4643 |
estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)); |
| 4644 |
if(residual_bits < UINT32_MAX - estimate) // To make sure estimate doesn't overflow |
| 4645 |
estimate += residual_bits; |
| 4646 |
else |
| 4647 |
estimate = UINT32_MAX; |
| 4648 |
|
| 4649 |
#if SPOTCHECK_ESTIMATE |
| 4650 |
spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate); |
| 4651 |
#endif |
| 4652 |
|
| 4653 |
return estimate; |
| 4654 |
} |
| 4655 |
#endif |
| 4656 |
|
| 4657 |
uint32_t evaluate_verbatim_subframe_( |
| 4658 |
FLAC__StreamEncoder *encoder, |
| 4659 |
const void *signal, |
| 4660 |
uint32_t blocksize, |
| 4661 |
uint32_t subframe_bps, |
| 4662 |
FLAC__Subframe *subframe |
| 4663 |
) |
| 4664 |
{ |
| 4665 |
uint32_t estimate; |
| 4666 |
|
| 4667 |
subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM; |
| 4668 |
|
| 4669 |
if(subframe_bps <= 32){ |
| 4670 |
subframe->data.verbatim.data_type = FLAC__VERBATIM_SUBFRAME_DATA_TYPE_INT32; |
| 4671 |
subframe->data.verbatim.data.int32 = signal; |
| 4672 |
} |
| 4673 |
else { |
| 4674 |
subframe->data.verbatim.data_type = FLAC__VERBATIM_SUBFRAME_DATA_TYPE_INT64; |
| 4675 |
subframe->data.verbatim.data.int64 = signal; |
| 4676 |
} |
| 4677 |
|
| 4678 |
estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (blocksize * subframe_bps); |
| 4679 |
|
| 4680 |
#if SPOTCHECK_ESTIMATE |
| 4681 |
spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate); |
| 4682 |
#else |
| 4683 |
(void)encoder; |
| 4684 |
#endif |
| 4685 |
|
| 4686 |
return estimate; |
| 4687 |
} |
| 4688 |
|
| 4689 |
uint32_t find_best_partition_order_( |
| 4690 |
FLAC__StreamEncoderPrivate *private_, |
| 4691 |
FLAC__StreamEncoderThreadTask *threadtask, |
| 4692 |
const FLAC__int32 residual[], |
| 4693 |
FLAC__uint64 abs_residual_partition_sums[], |
| 4694 |
uint32_t raw_bits_per_partition[], |
| 4695 |
uint32_t residual_samples, |
| 4696 |
uint32_t predictor_order, |
| 4697 |
uint32_t rice_parameter_limit, |
| 4698 |
uint32_t min_partition_order, |
| 4699 |
uint32_t max_partition_order, |
| 4700 |
uint32_t bps, |
| 4701 |
FLAC__bool do_escape_coding, |
| 4702 |
uint32_t rice_parameter_search_dist, |
| 4703 |
FLAC__EntropyCodingMethod *best_ecm |
| 4704 |
) |
| 4705 |
{ |
| 4706 |
uint32_t residual_bits, best_residual_bits = 0; |
| 4707 |
uint32_t best_parameters_index = 0; |
| 4708 |
uint32_t best_partition_order = 0; |
| 4709 |
const uint32_t blocksize = residual_samples + predictor_order; |
| 4710 |
|
| 4711 |
max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order); |
| 4712 |
min_partition_order = flac_min(min_partition_order, max_partition_order); |
| 4713 |
|
| 4714 |
private_->local_precompute_partition_info_sums(residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order, bps); |
| 4715 |
|
| 4716 |
if(do_escape_coding) |
| 4717 |
precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order); |
| 4718 |
|
| 4719 |
{ |
| 4720 |
int partition_order; |
| 4721 |
uint32_t sum; |
| 4722 |
|
| 4723 |
for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) { |
| 4724 |
FLAC__ASSERT(do_escape_coding != /* XOR */ (raw_bits_per_partition == NULL)); |
| 4725 |
if(! |
| 4726 |
set_partitioned_rice_( |
| 4727 |
#ifdef EXACT_RICE_BITS_CALCULATION |
| 4728 |
residual, |
| 4729 |
#endif |
| 4730 |
abs_residual_partition_sums+sum, |
| 4731 |
do_escape_coding ? raw_bits_per_partition+sum : NULL, |
| 4732 |
residual_samples, |
| 4733 |
predictor_order, |
| 4734 |
rice_parameter_limit, |
| 4735 |
rice_parameter_search_dist, |
| 4736 |
(uint32_t)partition_order, |
| 4737 |
do_escape_coding, |
| 4738 |
&threadtask->partitioned_rice_contents_extra[!best_parameters_index], |
| 4739 |
&residual_bits |
| 4740 |
) |
| 4741 |
) |
| 4742 |
{ |
| 4743 |
FLAC__ASSERT(best_residual_bits != 0); |
| 4744 |
break; |
| 4745 |
} |
| 4746 |
sum += 1u << partition_order; |
| 4747 |
if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 4748 |
best_residual_bits = residual_bits; |
| 4749 |
best_parameters_index = !best_parameters_index; |
| 4750 |
best_partition_order = partition_order; |
| 4751 |
} |
| 4752 |
} |
| 4753 |
} |
| 4754 |
|
| 4755 |
best_ecm->data.partitioned_rice.order = best_partition_order; |
| 4756 |
|
| 4757 |
{ |
| 4758 |
/* |
| 4759 |
* We are allowed to de-const the pointer based on our special |
| 4760 |
* knowledge; it is const to the outside world. |
| 4761 |
*/ |
| 4762 |
FLAC__EntropyCodingMethod_PartitionedRiceContents* prc = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_ecm->data.partitioned_rice.contents; |
| 4763 |
uint32_t partition; |
| 4764 |
|
| 4765 |
/* save best parameters and raw_bits */ |
| 4766 |
memcpy(prc->parameters, threadtask->partitioned_rice_contents_extra[best_parameters_index].parameters, (uint32_t)sizeof(uint32_t)*(1<<(best_partition_order))); |
| 4767 |
if(do_escape_coding) |
| 4768 |
memcpy(prc->raw_bits, threadtask->partitioned_rice_contents_extra[best_parameters_index].raw_bits, (uint32_t)sizeof(uint32_t)*(1<<(best_partition_order))); |
| 4769 |
/* |
| 4770 |
* Now need to check if the type should be changed to |
| 4771 |
* FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2 based on the |
| 4772 |
* size of the rice parameters. |
| 4773 |
*/ |
| 4774 |
for(partition = 0; partition < (1u<<best_partition_order); partition++) { |
| 4775 |
if(prc->parameters[partition] >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
| 4776 |
best_ecm->type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2; |
| 4777 |
break; |
| 4778 |
} |
| 4779 |
} |
| 4780 |
} |
| 4781 |
|
| 4782 |
return best_residual_bits; |
| 4783 |
} |
| 4784 |
|
| 4785 |
void precompute_partition_info_sums_( |
| 4786 |
const FLAC__int32 residual[], |
| 4787 |
FLAC__uint64 abs_residual_partition_sums[], |
| 4788 |
uint32_t residual_samples, |
| 4789 |
uint32_t predictor_order, |
| 4790 |
uint32_t min_partition_order, |
| 4791 |
uint32_t max_partition_order, |
| 4792 |
uint32_t bps |
| 4793 |
) |
| 4794 |
{ |
| 4795 |
const uint32_t default_partition_samples = (residual_samples + predictor_order) >> max_partition_order; |
| 4796 |
uint32_t partitions = 1u << max_partition_order; |
| 4797 |
|
| 4798 |
FLAC__ASSERT(default_partition_samples > predictor_order); |
| 4799 |
|
| 4800 |
/* first do max_partition_order */ |
| 4801 |
{ |
| 4802 |
const uint32_t threshold = 32 - FLAC__bitmath_ilog2(default_partition_samples); |
| 4803 |
uint32_t partition, residual_sample, end = (uint32_t)(-(int)predictor_order); |
| 4804 |
/* WATCHOUT: "bps + FLAC__MAX_EXTRA_RESIDUAL_BPS" is the maximum assumed size of the average residual magnitude */ |
| 4805 |
if(bps + FLAC__MAX_EXTRA_RESIDUAL_BPS < threshold) { |
| 4806 |
for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 4807 |
FLAC__uint32 abs_residual_partition_sum = 0; |
| 4808 |
end += default_partition_samples; |
| 4809 |
for( ; residual_sample < end; residual_sample++) |
| 4810 |
abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */ |
| 4811 |
abs_residual_partition_sums[partition] = abs_residual_partition_sum; |
| 4812 |
} |
| 4813 |
} |
| 4814 |
else { /* have to pessimistically use 64 bits for accumulator */ |
| 4815 |
for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 4816 |
FLAC__uint64 abs_residual_partition_sum64 = 0; |
| 4817 |
end += default_partition_samples; |
| 4818 |
for( ; residual_sample < end; residual_sample++) |
| 4819 |
abs_residual_partition_sum64 += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */ |
| 4820 |
abs_residual_partition_sums[partition] = abs_residual_partition_sum64; |
| 4821 |
} |
| 4822 |
} |
| 4823 |
} |
| 4824 |
|
| 4825 |
/* now merge partitions for lower orders */ |
| 4826 |
{ |
| 4827 |
uint32_t from_partition = 0, to_partition = partitions; |
| 4828 |
int partition_order; |
| 4829 |
for(partition_order = (int)max_partition_order - 1; partition_order >= (int)min_partition_order; partition_order--) { |
| 4830 |
uint32_t i; |
| 4831 |
partitions >>= 1; |
| 4832 |
for(i = 0; i < partitions; i++) { |
| 4833 |
abs_residual_partition_sums[to_partition++] = |
| 4834 |
abs_residual_partition_sums[from_partition ] + |
| 4835 |
abs_residual_partition_sums[from_partition+1]; |
| 4836 |
from_partition += 2; |
| 4837 |
} |
| 4838 |
} |
| 4839 |
} |
| 4840 |
} |
| 4841 |
|
| 4842 |
void precompute_partition_info_escapes_( |
| 4843 |
const FLAC__int32 residual[], |
| 4844 |
uint32_t raw_bits_per_partition[], |
| 4845 |
uint32_t residual_samples, |
| 4846 |
uint32_t predictor_order, |
| 4847 |
uint32_t min_partition_order, |
| 4848 |
uint32_t max_partition_order |
| 4849 |
) |
| 4850 |
{ |
| 4851 |
int partition_order; |
| 4852 |
uint32_t from_partition, to_partition = 0; |
| 4853 |
const uint32_t blocksize = residual_samples + predictor_order; |
| 4854 |
|
| 4855 |
/* first do max_partition_order */ |
| 4856 |
for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) { |
| 4857 |
FLAC__int32 r; |
| 4858 |
FLAC__uint32 rmax; |
| 4859 |
uint32_t partition, partition_sample, partition_samples, residual_sample; |
| 4860 |
const uint32_t partitions = 1u << partition_order; |
| 4861 |
const uint32_t default_partition_samples = blocksize >> partition_order; |
| 4862 |
|
| 4863 |
FLAC__ASSERT(default_partition_samples > predictor_order); |
| 4864 |
|
| 4865 |
for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 4866 |
partition_samples = default_partition_samples; |
| 4867 |
if(partition == 0) |
| 4868 |
partition_samples -= predictor_order; |
| 4869 |
rmax = 0; |
| 4870 |
for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) { |
| 4871 |
r = residual[residual_sample++]; |
| 4872 |
/* OPT: maybe faster: rmax |= r ^ (r>>31) */ |
| 4873 |
if(r < 0) |
| 4874 |
rmax |= ~r; |
| 4875 |
else |
| 4876 |
rmax |= r; |
| 4877 |
} |
| 4878 |
/* now we know all residual values are in the range [-rmax-1,rmax] */ |
| 4879 |
raw_bits_per_partition[partition] = rmax? FLAC__bitmath_ilog2(rmax) + 2 : 1; |
| 4880 |
} |
| 4881 |
to_partition = partitions; |
| 4882 |
break; /*@@@ yuck, should remove the 'for' loop instead */ |
| 4883 |
} |
| 4884 |
|
| 4885 |
/* now merge partitions for lower orders */ |
| 4886 |
for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) { |
| 4887 |
uint32_t m; |
| 4888 |
uint32_t i; |
| 4889 |
const uint32_t partitions = 1u << partition_order; |
| 4890 |
for(i = 0; i < partitions; i++) { |
| 4891 |
m = raw_bits_per_partition[from_partition]; |
| 4892 |
from_partition++; |
| 4893 |
raw_bits_per_partition[to_partition] = flac_max(m, raw_bits_per_partition[from_partition]); |
| 4894 |
from_partition++; |
| 4895 |
to_partition++; |
| 4896 |
} |
| 4897 |
} |
| 4898 |
} |
| 4899 |
|
| 4900 |
#ifdef EXACT_RICE_BITS_CALCULATION |
| 4901 |
static inline uint32_t count_rice_bits_in_partition_( |
| 4902 |
const uint32_t rice_parameter, |
| 4903 |
const uint32_t partition_samples, |
| 4904 |
const FLAC__int32 *residual |
| 4905 |
) |
| 4906 |
{ |
| 4907 |
uint32_t i; |
| 4908 |
uint64_t partition_bits = |
| 4909 |
FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + /* actually could end up being FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN but err on side of 16bps */ |
| 4910 |
(1+rice_parameter) * partition_samples /* 1 for unary stop bit + rice_parameter for the binary portion */ |
| 4911 |
; |
| 4912 |
for(i = 0; i < partition_samples; i++) |
| 4913 |
partition_bits += ( (FLAC__uint32)((residual[i]<<1)^(residual[i]>>31)) >> rice_parameter ); |
| 4914 |
return (uint32_t)(flac_min(partition_bits,UINT32_MAX)); // To make sure the return value doesn't overflow |
| 4915 |
} |
| 4916 |
#else |
| 4917 |
static inline uint32_t count_rice_bits_in_partition_( |
| 4918 |
const uint32_t rice_parameter, |
| 4919 |
const uint32_t partition_samples, |
| 4920 |
const FLAC__uint64 abs_residual_partition_sum |
| 4921 |
) |
| 4922 |
{ |
| 4923 |
return (uint32_t)(flac_min( // To make sure the return value doesn't overflow |
| 4924 |
FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + /* actually could end up being FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN but err on side of 16bps */ |
| 4925 |
(1+rice_parameter) * partition_samples + /* 1 for unary stop bit + rice_parameter for the binary portion */ |
| 4926 |
( |
| 4927 |
rice_parameter? |
| 4928 |
(abs_residual_partition_sum >> (rice_parameter-1)) /* rice_parameter-1 because the real coder sign-folds instead of using a sign bit */ |
| 4929 |
: (abs_residual_partition_sum << 1) /* can't shift by negative number, so reverse */ |
| 4930 |
) |
| 4931 |
- (partition_samples >> 1),UINT32_MAX)); |
| 4932 |
/* -(partition_samples>>1) to subtract out extra contributions to the abs_residual_partition_sum. |
| 4933 |
* The actual number of bits used is closer to the sum(for all i in the partition) of abs(residual[i])>>(rice_parameter-1) |
| 4934 |
* By using the abs_residual_partition sum, we also add in bits in the LSBs that would normally be shifted out. |
| 4935 |
* So the subtraction term tries to guess how many extra bits were contributed. |
| 4936 |
* If the LSBs are randomly distributed, this should average to 0.5 extra bits per sample. |
| 4937 |
*/ |
| 4938 |
; |
| 4939 |
} |
| 4940 |
#endif |
| 4941 |
|
| 4942 |
FLAC__bool set_partitioned_rice_( |
| 4943 |
#ifdef EXACT_RICE_BITS_CALCULATION |
| 4944 |
const FLAC__int32 residual[], |
| 4945 |
#endif |
| 4946 |
const FLAC__uint64 abs_residual_partition_sums[], |
| 4947 |
const uint32_t raw_bits_per_partition[], |
| 4948 |
const uint32_t residual_samples, |
| 4949 |
const uint32_t predictor_order, |
| 4950 |
const uint32_t rice_parameter_limit, |
| 4951 |
const uint32_t rice_parameter_search_dist, |
| 4952 |
const uint32_t partition_order, |
| 4953 |
const FLAC__bool search_for_escapes, |
| 4954 |
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 4955 |
uint32_t *bits |
| 4956 |
) |
| 4957 |
{ |
| 4958 |
uint32_t rice_parameter, partition_bits; |
| 4959 |
uint32_t best_partition_bits, best_rice_parameter = 0; |
| 4960 |
uint32_t bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; |
| 4961 |
uint32_t *parameters, *raw_bits; |
| 4962 |
uint32_t partition, residual_sample; |
| 4963 |
uint32_t partition_samples, partition_samples_base; |
| 4964 |
uint32_t partition_samples_fixed_point_divisor, partition_samples_fixed_point_divisor_base; |
| 4965 |
const uint32_t partitions = 1u << partition_order; |
| 4966 |
FLAC__uint64 mean; |
| 4967 |
#ifdef ENABLE_RICE_PARAMETER_SEARCH |
| 4968 |
uint32_t min_rice_parameter, max_rice_parameter; |
| 4969 |
#else |
| 4970 |
(void)rice_parameter_search_dist; |
| 4971 |
#endif |
| 4972 |
|
| 4973 |
FLAC__ASSERT(rice_parameter_limit <= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER); |
| 4974 |
|
| 4975 |
parameters = partitioned_rice_contents->parameters; |
| 4976 |
raw_bits = partitioned_rice_contents->raw_bits; |
| 4977 |
|
| 4978 |
partition_samples_base = (residual_samples+predictor_order) >> partition_order; |
| 4979 |
|
| 4980 |
/* Integer division is slow. To speed up things, precalculate a fixed point |
| 4981 |
* divisor, as all partitions except the first are the same size. 18 bits |
| 4982 |
* are taken because maximum block size is 65535, max partition size for |
| 4983 |
* partitions other than 0 is 32767 (15 bit), max abs residual is 2^31, |
| 4984 |
* which leaves 18 bit */ |
| 4985 |
partition_samples_fixed_point_divisor_base = 0x40000 / partition_samples_base; |
| 4986 |
|
| 4987 |
for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 4988 |
partition_samples = partition_samples_base; |
| 4989 |
if(partition > 0) { |
| 4990 |
partition_samples_fixed_point_divisor = partition_samples_fixed_point_divisor_base; |
| 4991 |
} |
| 4992 |
else { |
| 4993 |
if(partition_samples <= predictor_order) |
| 4994 |
return false; |
| 4995 |
else |
| 4996 |
partition_samples -= predictor_order; |
| 4997 |
partition_samples_fixed_point_divisor = 0x40000 / partition_samples; |
| 4998 |
} |
| 4999 |
mean = abs_residual_partition_sums[partition]; |
| 5000 |
/* 'mean' is not a good name for the variable, it is |
| 5001 |
* actually the sum of magnitudes of all residual values |
| 5002 |
* in the partition, so the actual mean is |
| 5003 |
* mean/partition_samples |
| 5004 |
*/ |
| 5005 |
if(mean < 2 || (((mean - 1)*partition_samples_fixed_point_divisor)>>18) == 0) |
| 5006 |
rice_parameter = 0; |
| 5007 |
else |
| 5008 |
rice_parameter = FLAC__bitmath_ilog2_wide(((mean - 1)*partition_samples_fixed_point_divisor)>>18) + 1; |
| 5009 |
|
| 5010 |
if(rice_parameter >= rice_parameter_limit) { |
| 5011 |
rice_parameter = rice_parameter_limit - 1; |
| 5012 |
} |
| 5013 |
|
| 5014 |
best_partition_bits = UINT32_MAX; |
| 5015 |
#ifdef ENABLE_RICE_PARAMETER_SEARCH |
| 5016 |
if(rice_parameter_search_dist) { |
| 5017 |
if(rice_parameter < rice_parameter_search_dist) |
| 5018 |
min_rice_parameter = 0; |
| 5019 |
else |
| 5020 |
min_rice_parameter = rice_parameter - rice_parameter_search_dist; |
| 5021 |
max_rice_parameter = rice_parameter + rice_parameter_search_dist; |
| 5022 |
if(max_rice_parameter >= rice_parameter_limit) { |
| 5023 |
max_rice_parameter = rice_parameter_limit - 1; |
| 5024 |
} |
| 5025 |
} |
| 5026 |
else |
| 5027 |
min_rice_parameter = max_rice_parameter = rice_parameter; |
| 5028 |
|
| 5029 |
for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 5030 |
#endif |
| 5031 |
#ifdef EXACT_RICE_BITS_CALCULATION |
| 5032 |
partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, residual+residual_sample); |
| 5033 |
#else |
| 5034 |
partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, abs_residual_partition_sums[partition]); |
| 5035 |
#endif |
| 5036 |
if(partition_bits < best_partition_bits) { |
| 5037 |
best_rice_parameter = rice_parameter; |
| 5038 |
best_partition_bits = partition_bits; |
| 5039 |
} |
| 5040 |
#ifdef ENABLE_RICE_PARAMETER_SEARCH |
| 5041 |
} |
| 5042 |
#endif |
| 5043 |
if(search_for_escapes) { |
| 5044 |
partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples; |
| 5045 |
if(partition_bits <= best_partition_bits && raw_bits_per_partition[partition] < 32) { |
| 5046 |
raw_bits[partition] = raw_bits_per_partition[partition]; |
| 5047 |
best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */ |
| 5048 |
best_partition_bits = partition_bits; |
| 5049 |
} |
| 5050 |
else |
| 5051 |
raw_bits[partition] = 0; |
| 5052 |
} |
| 5053 |
parameters[partition] = best_rice_parameter; |
| 5054 |
if(best_partition_bits < UINT32_MAX - bits_) // To make sure _bits doesn't overflow |
| 5055 |
bits_ += best_partition_bits; |
| 5056 |
else |
| 5057 |
bits_ = UINT32_MAX; |
| 5058 |
residual_sample += partition_samples; |
| 5059 |
} |
| 5060 |
|
| 5061 |
*bits = bits_; |
| 5062 |
return true; |
| 5063 |
} |
| 5064 |
|
| 5065 |
uint32_t get_wasted_bits_(FLAC__int32 signal[], uint32_t samples) |
| 5066 |
{ |
| 5067 |
uint32_t i, shift; |
| 5068 |
FLAC__int32 x = 0; |
| 5069 |
|
| 5070 |
for(i = 0; i < samples && !(x&1); i++) |
| 5071 |
x |= signal[i]; |
| 5072 |
|
| 5073 |
if(x == 0) { |
| 5074 |
shift = 0; |
| 5075 |
} |
| 5076 |
else { |
| 5077 |
for(shift = 0; !(x&1); shift++) |
| 5078 |
x >>= 1; |
| 5079 |
} |
| 5080 |
|
| 5081 |
if(shift > 0) { |
| 5082 |
for(i = 0; i < samples; i++) |
| 5083 |
signal[i] >>= shift; |
| 5084 |
} |
| 5085 |
|
| 5086 |
return shift; |
| 5087 |
} |
| 5088 |
|
| 5089 |
uint32_t get_wasted_bits_wide_(FLAC__int64 signal_wide[], FLAC__int32 signal[], uint32_t samples) |
| 5090 |
{ |
| 5091 |
uint32_t i, shift; |
| 5092 |
FLAC__int64 x = 0; |
| 5093 |
|
| 5094 |
for(i = 0; i < samples && !(x&1); i++) |
| 5095 |
x |= signal_wide[i]; |
| 5096 |
|
| 5097 |
if(x == 0) { |
| 5098 |
shift = 1; |
| 5099 |
} |
| 5100 |
else { |
| 5101 |
for(shift = 0; !(x&1); shift++) |
| 5102 |
x >>= 1; |
| 5103 |
} |
| 5104 |
|
| 5105 |
if(shift > 0) { |
| 5106 |
for(i = 0; i < samples; i++) |
| 5107 |
signal[i] = (FLAC__int32)(signal_wide[i] >> shift); |
| 5108 |
} |
| 5109 |
|
| 5110 |
return shift; |
| 5111 |
} |
| 5112 |
|
| 5113 |
|
| 5114 |
void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], uint32_t input_offset, uint32_t channels, uint32_t wide_samples) |
| 5115 |
{ |
| 5116 |
uint32_t channel; |
| 5117 |
|
| 5118 |
for(channel = 0; channel < channels; channel++) |
| 5119 |
memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples); |
| 5120 |
|
| 5121 |
fifo->tail += wide_samples; |
| 5122 |
|
| 5123 |
FLAC__ASSERT(fifo->tail <= fifo->size); |
| 5124 |
} |
| 5125 |
|
| 5126 |
void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], uint32_t input_offset, uint32_t channels, uint32_t wide_samples) |
| 5127 |
{ |
| 5128 |
uint32_t channel; |
| 5129 |
uint32_t sample, wide_sample; |
| 5130 |
uint32_t tail = fifo->tail; |
| 5131 |
|
| 5132 |
sample = input_offset * channels; |
| 5133 |
for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) { |
| 5134 |
for(channel = 0; channel < channels; channel++) |
| 5135 |
fifo->data[channel][tail] = input[sample++]; |
| 5136 |
tail++; |
| 5137 |
} |
| 5138 |
fifo->tail = tail; |
| 5139 |
|
| 5140 |
FLAC__ASSERT(fifo->tail <= fifo->size); |
| 5141 |
} |
| 5142 |
|
| 5143 |
FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) |
| 5144 |
{ |
| 5145 |
FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data; |
| 5146 |
const size_t encoded_bytes = encoder->private_->verify.output.bytes; |
| 5147 |
(void)decoder; |
| 5148 |
|
| 5149 |
if(encoder->private_->verify.needs_magic_hack) { |
| 5150 |
FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH); |
| 5151 |
*bytes = FLAC__STREAM_SYNC_LENGTH; |
| 5152 |
memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes); |
| 5153 |
encoder->private_->verify.needs_magic_hack = false; |
| 5154 |
} |
| 5155 |
else { |
| 5156 |
if(encoded_bytes == 0) { |
| 5157 |
/* |
| 5158 |
* If we get here, a FIFO underflow has occurred, |
| 5159 |
* which means there is a bug somewhere. |
| 5160 |
*/ |
| 5161 |
FLAC__ASSERT(0); |
| 5162 |
return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 5163 |
} |
| 5164 |
else if(encoded_bytes < *bytes) |
| 5165 |
*bytes = encoded_bytes; |
| 5166 |
memcpy(buffer, encoder->private_->verify.output.data, *bytes); |
| 5167 |
encoder->private_->verify.output.data += *bytes; |
| 5168 |
encoder->private_->verify.output.bytes -= *bytes; |
| 5169 |
} |
| 5170 |
|
| 5171 |
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 5172 |
} |
| 5173 |
|
| 5174 |
FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) |
| 5175 |
{ |
| 5176 |
FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data; |
| 5177 |
uint32_t channel; |
| 5178 |
const uint32_t channels = frame->header.channels; |
| 5179 |
const uint32_t blocksize = frame->header.blocksize; |
| 5180 |
const uint32_t bytes_per_block = sizeof(FLAC__int32) * blocksize; |
| 5181 |
|
| 5182 |
(void)decoder; |
| 5183 |
|
| 5184 |
if(encoder->protected_->state == FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) { |
| 5185 |
/* This is set when verify_error_callback_ was called */ |
| 5186 |
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; |
| 5187 |
} |
| 5188 |
|
| 5189 |
for(channel = 0; channel < channels; channel++) { |
| 5190 |
if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) { |
| 5191 |
uint32_t i, sample = 0; |
| 5192 |
FLAC__int32 expect = 0, got = 0; |
| 5193 |
|
| 5194 |
for(i = 0; i < blocksize; i++) { |
| 5195 |
if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) { |
| 5196 |
sample = i; |
| 5197 |
expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i]; |
| 5198 |
got = (FLAC__int32)buffer[channel][i]; |
| 5199 |
break; |
| 5200 |
} |
| 5201 |
} |
| 5202 |
FLAC__ASSERT(i < blocksize); |
| 5203 |
FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 5204 |
encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample; |
| 5205 |
encoder->private_->verify.error_stats.frame_number = (uint32_t)(frame->header.number.sample_number / blocksize); |
| 5206 |
encoder->private_->verify.error_stats.channel = channel; |
| 5207 |
encoder->private_->verify.error_stats.sample = sample; |
| 5208 |
encoder->private_->verify.error_stats.expected = expect; |
| 5209 |
encoder->private_->verify.error_stats.got = got; |
| 5210 |
encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA; |
| 5211 |
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; |
| 5212 |
} |
| 5213 |
} |
| 5214 |
/* dequeue the frame from the fifo */ |
| 5215 |
encoder->private_->verify.input_fifo.tail -= blocksize; |
| 5216 |
for(channel = 0; channel < channels; channel++) |
| 5217 |
memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail * sizeof(encoder->private_->verify.input_fifo.data[0][0])); |
| 5218 |
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
| 5219 |
} |
| 5220 |
|
| 5221 |
void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) |
| 5222 |
{ |
| 5223 |
(void)decoder, (void)metadata, (void)client_data; |
| 5224 |
} |
| 5225 |
|
| 5226 |
void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) |
| 5227 |
{ |
| 5228 |
FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data; |
| 5229 |
(void)decoder, (void)status; |
| 5230 |
encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 5231 |
} |
| 5232 |
|
| 5233 |
FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data) |
| 5234 |
{ |
| 5235 |
(void)client_data; |
| 5236 |
|
| 5237 |
*bytes = fread(buffer, 1, *bytes, encoder->private_->file); |
| 5238 |
if (*bytes == 0) { |
| 5239 |
if (feof(encoder->private_->file)) |
| 5240 |
return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM; |
| 5241 |
else if (ferror(encoder->private_->file)) |
| 5242 |
return FLAC__STREAM_ENCODER_READ_STATUS_ABORT; |
| 5243 |
} |
| 5244 |
return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE; |
| 5245 |
} |
| 5246 |
|
| 5247 |
FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data) |
| 5248 |
{ |
| 5249 |
(void)client_data; |
| 5250 |
|
| 5251 |
if(fseeko(encoder->private_->file, (FLAC__off_t)absolute_byte_offset, SEEK_SET) < 0) |
| 5252 |
return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR; |
| 5253 |
else |
| 5254 |
return FLAC__STREAM_ENCODER_SEEK_STATUS_OK; |
| 5255 |
} |
| 5256 |
|
| 5257 |
FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data) |
| 5258 |
{ |
| 5259 |
FLAC__off_t offset; |
| 5260 |
|
| 5261 |
(void)client_data; |
| 5262 |
|
| 5263 |
offset = ftello(encoder->private_->file); |
| 5264 |
|
| 5265 |
if(offset < 0) { |
| 5266 |
return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR; |
| 5267 |
} |
| 5268 |
else { |
| 5269 |
*absolute_byte_offset = (FLAC__uint64)offset; |
| 5270 |
return FLAC__STREAM_ENCODER_TELL_STATUS_OK; |
| 5271 |
} |
| 5272 |
} |
| 5273 |
|
| 5274 |
#ifdef FLAC__VALGRIND_TESTING |
| 5275 |
static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) |
| 5276 |
{ |
| 5277 |
size_t ret = fwrite(ptr, size, nmemb, stream); |
| 5278 |
if(!ferror(stream)) |
| 5279 |
fflush(stream); |
| 5280 |
return ret; |
| 5281 |
} |
| 5282 |
#else |
| 5283 |
#define local__fwrite fwrite |
| 5284 |
#endif |
| 5285 |
|
| 5286 |
FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, uint32_t samples, uint32_t current_frame, void *client_data) |
| 5287 |
{ |
| 5288 |
(void)client_data, (void)current_frame; |
| 5289 |
|
| 5290 |
if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) { |
| 5291 |
FLAC__bool call_it = 0 != encoder->private_->progress_callback && ( |
| 5292 |
#if FLAC__HAS_OGG |
| 5293 |
/* We would like to be able to use 'samples > 0' in the |
| 5294 |
* clause here but currently because of the nature of our |
| 5295 |
* Ogg writing implementation, 'samples' is always 0 (see |
| 5296 |
* ogg_encoder_aspect.c). The downside is extra progress |
| 5297 |
* callbacks. |
| 5298 |
*/ |
| 5299 |
encoder->private_->is_ogg? true : |
| 5300 |
#endif |
| 5301 |
samples > 0 |
| 5302 |
); |
| 5303 |
if(call_it) { |
| 5304 |
/* NOTE: We have to add +bytes, +samples, and +1 to the stats |
| 5305 |
* because at this point in the callback chain, the stats |
| 5306 |
* have not been updated. Only after we return and control |
| 5307 |
* gets back to write_frame_() are the stats updated |
| 5308 |
*/ |
| 5309 |
encoder->private_->progress_callback(encoder, encoder->private_->bytes_written+bytes, encoder->private_->samples_written+samples, encoder->private_->frames_written+(samples?1:0), encoder->private_->total_frames_estimate, encoder->private_->client_data); |
| 5310 |
} |
| 5311 |
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK; |
| 5312 |
} |
| 5313 |
else |
| 5314 |
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR; |
| 5315 |
} |
| 5316 |
|
| 5317 |
/* |
| 5318 |
* This will forcibly set stdout to binary mode (for OSes that require it) |
| 5319 |
*/ |
| 5320 |
FILE *get_binary_stdout_(void) |
| 5321 |
{ |
| 5322 |
/* if something breaks here it is probably due to the presence or |
| 5323 |
* absence of an underscore before the identifiers 'setmode', |
| 5324 |
* 'fileno', and/or 'O_BINARY'; check your system header files. |
| 5325 |
*/ |
| 5326 |
#if defined _MSC_VER || defined __MINGW32__ |
| 5327 |
_setmode(_fileno(stdout), _O_BINARY); |
| 5328 |
#elif defined __EMX__ |
| 5329 |
setmode(fileno(stdout), O_BINARY); |
| 5330 |
#endif |
| 5331 |
|
| 5332 |
return stdout; |
| 5333 |
} |