| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * FLAC audio encoder | ||
| 3 | * Copyright (c) 2006 Justin Ruggles <justin.ruggles@gmail.com> | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "libavutil/avassert.h" | ||
| 23 | #include "libavutil/channel_layout.h" | ||
| 24 | #include "libavutil/crc.h" | ||
| 25 | #include "libavutil/intmath.h" | ||
| 26 | #include "libavutil/md5.h" | ||
| 27 | #include "libavutil/mem.h" | ||
| 28 | #include "libavutil/opt.h" | ||
| 29 | |||
| 30 | #include "avcodec.h" | ||
| 31 | #include "bswapdsp.h" | ||
| 32 | #include "codec_internal.h" | ||
| 33 | #include "encode.h" | ||
| 34 | #include "put_bits.h" | ||
| 35 | #include "lpc.h" | ||
| 36 | #include "flac.h" | ||
| 37 | #include "flacdata.h" | ||
| 38 | #include "flacencdsp.h" | ||
| 39 | |||
| 40 | #define FLAC_SUBFRAME_CONSTANT 0 | ||
| 41 | #define FLAC_SUBFRAME_VERBATIM 1 | ||
| 42 | #define FLAC_SUBFRAME_FIXED 8 | ||
| 43 | #define FLAC_SUBFRAME_LPC 32 | ||
| 44 | |||
| 45 | #define MAX_FIXED_ORDER 4 | ||
| 46 | #define MAX_PARTITION_ORDER 8 | ||
| 47 | #define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER) | ||
| 48 | #define MAX_LPC_PRECISION 15 | ||
| 49 | #define MIN_LPC_SHIFT 0 | ||
| 50 | #define MAX_LPC_SHIFT 15 | ||
| 51 | |||
| 52 | enum CodingMode { | ||
| 53 | CODING_MODE_RICE = 4, | ||
| 54 | CODING_MODE_RICE2 = 5, | ||
| 55 | }; | ||
| 56 | |||
| 57 | typedef struct CompressionOptions { | ||
| 58 | int compression_level; | ||
| 59 | int block_time_ms; | ||
| 60 | enum FFLPCType lpc_type; | ||
| 61 | int lpc_passes; | ||
| 62 | int lpc_coeff_precision; | ||
| 63 | int min_prediction_order; | ||
| 64 | int max_prediction_order; | ||
| 65 | int prediction_order_method; | ||
| 66 | int min_partition_order; | ||
| 67 | int max_partition_order; | ||
| 68 | int ch_mode; | ||
| 69 | int exact_rice_parameters; | ||
| 70 | int multi_dim_quant; | ||
| 71 | } CompressionOptions; | ||
| 72 | |||
| 73 | typedef struct RiceContext { | ||
| 74 | enum CodingMode coding_mode; | ||
| 75 | int porder; | ||
| 76 | int params[MAX_PARTITIONS]; | ||
| 77 | } RiceContext; | ||
| 78 | |||
| 79 | typedef struct FlacSubframe { | ||
| 80 | int type; | ||
| 81 | int type_code; | ||
| 82 | int obits; | ||
| 83 | int wasted; | ||
| 84 | int order; | ||
| 85 | int32_t coefs[MAX_LPC_ORDER]; | ||
| 86 | int shift; | ||
| 87 | |||
| 88 | RiceContext rc; | ||
| 89 | uint32_t rc_udata[FLAC_MAX_BLOCKSIZE]; | ||
| 90 | uint64_t rc_sums[32][MAX_PARTITIONS]; | ||
| 91 | |||
| 92 | int32_t samples[FLAC_MAX_BLOCKSIZE]; | ||
| 93 | int32_t residual[FLAC_MAX_BLOCKSIZE+11]; | ||
| 94 | } FlacSubframe; | ||
| 95 | |||
| 96 | typedef struct FlacFrame { | ||
| 97 | FlacSubframe subframes[FLAC_MAX_CHANNELS]; | ||
| 98 | int64_t samples_33bps[FLAC_MAX_BLOCKSIZE]; | ||
| 99 | int blocksize; | ||
| 100 | int bs_code[2]; | ||
| 101 | uint8_t crc8; | ||
| 102 | int ch_mode; | ||
| 103 | int verbatim_only; | ||
| 104 | } FlacFrame; | ||
| 105 | |||
| 106 | typedef struct FlacEncodeContext { | ||
| 107 | AVClass *class; | ||
| 108 | PutBitContext pb; | ||
| 109 | int channels; | ||
| 110 | int samplerate; | ||
| 111 | int sr_code[2]; | ||
| 112 | int bps_code; | ||
| 113 | int max_blocksize; | ||
| 114 | int min_framesize; | ||
| 115 | int max_framesize; | ||
| 116 | int max_encoded_framesize; | ||
| 117 | uint32_t frame_count; | ||
| 118 | uint64_t sample_count; | ||
| 119 | uint8_t md5sum[16]; | ||
| 120 | FlacFrame frame; | ||
| 121 | CompressionOptions options; | ||
| 122 | AVCodecContext *avctx; | ||
| 123 | LPCContext lpc_ctx; | ||
| 124 | struct AVMD5 *md5ctx; | ||
| 125 | uint8_t *md5_buffer; | ||
| 126 | unsigned int md5_buffer_size; | ||
| 127 | BswapDSPContext bdsp; | ||
| 128 | FLACEncDSPContext flac_dsp; | ||
| 129 | |||
| 130 | int flushed; | ||
| 131 | int64_t next_pts; | ||
| 132 | } FlacEncodeContext; | ||
| 133 | |||
| 134 | |||
| 135 | /** | ||
| 136 | * Write streaminfo metadata block to byte array. | ||
| 137 | */ | ||
| 138 | 238 | static void write_streaminfo(FlacEncodeContext *s, uint8_t *header) | |
| 139 | { | ||
| 140 | PutBitContext pb; | ||
| 141 | |||
| 142 | 238 | memset(header, 0, FLAC_STREAMINFO_SIZE); | |
| 143 | 238 | init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE); | |
| 144 | |||
| 145 | /* streaminfo metadata block */ | ||
| 146 | 238 | put_bits(&pb, 16, s->max_blocksize); | |
| 147 | 238 | put_bits(&pb, 16, s->max_blocksize); | |
| 148 | 238 | put_bits(&pb, 24, s->min_framesize); | |
| 149 | 238 | put_bits(&pb, 24, s->max_framesize); | |
| 150 | 238 | put_bits(&pb, 20, s->samplerate); | |
| 151 | 238 | put_bits(&pb, 3, s->channels-1); | |
| 152 | 238 | put_bits(&pb, 5, s->avctx->bits_per_raw_sample - 1); | |
| 153 | /* write 36-bit sample count in 2 put_bits() calls */ | ||
| 154 | 238 | put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12); | |
| 155 | 238 | put_bits(&pb, 12, s->sample_count & 0x000000FFFLL); | |
| 156 | 238 | flush_put_bits(&pb); | |
| 157 | 238 | memcpy(&header[18], s->md5sum, 16); | |
| 158 | 238 | } | |
| 159 | |||
| 160 | |||
| 161 | /** | ||
| 162 | * Calculate an estimate for the maximum frame size based on verbatim mode. | ||
| 163 | * @param blocksize block size, in samples | ||
| 164 | * @param ch number of channels | ||
| 165 | * @param bps bits-per-sample | ||
| 166 | */ | ||
| 167 | 161 | static int flac_get_max_frame_size(int blocksize, int ch, int bps) | |
| 168 | { | ||
| 169 | /* Technically, there is no limit to FLAC frame size, but an encoder | ||
| 170 | should not write a frame that is larger than if verbatim encoding mode | ||
| 171 | were to be used. */ | ||
| 172 | |||
| 173 | int count; | ||
| 174 | |||
| 175 | 161 | count = 16; /* frame header */ | |
| 176 | 161 | count += ch * ((7+bps+7)/8); /* subframe headers */ | |
| 177 |
2/2✓ Branch 0 taken 100 times.
✓ Branch 1 taken 61 times.
|
161 | if (ch == 2) { |
| 178 | /* for stereo, need to account for using decorrelation */ | ||
| 179 | 100 | count += (( 2*bps+1) * blocksize + 7) / 8; | |
| 180 | } else { | ||
| 181 | 61 | count += ( ch*bps * blocksize + 7) / 8; | |
| 182 | } | ||
| 183 | 161 | count += 2; /* frame footer */ | |
| 184 | |||
| 185 | 161 | return count; | |
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | /** | ||
| 190 | * Set blocksize based on samplerate. | ||
| 191 | * Choose the closest predefined blocksize >= BLOCK_TIME_MS milliseconds. | ||
| 192 | */ | ||
| 193 | 90 | static int select_blocksize(int samplerate, int block_time_ms) | |
| 194 | { | ||
| 195 | int i; | ||
| 196 | int target; | ||
| 197 | int blocksize; | ||
| 198 | |||
| 199 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
|
90 | av_assert0(samplerate > 0); |
| 200 | 90 | blocksize = ff_flac_blocksize_table[1]; | |
| 201 | 90 | target = (samplerate * block_time_ms) / 1000; | |
| 202 |
2/2✓ Branch 0 taken 1440 times.
✓ Branch 1 taken 90 times.
|
1530 | for (i = 0; i < 16; i++) { |
| 203 |
2/2✓ Branch 0 taken 1148 times.
✓ Branch 1 taken 292 times.
|
1440 | if (target >= ff_flac_blocksize_table[i] && |
| 204 |
2/2✓ Branch 0 taken 354 times.
✓ Branch 1 taken 794 times.
|
1148 | ff_flac_blocksize_table[i] > blocksize) { |
| 205 | 354 | blocksize = ff_flac_blocksize_table[i]; | |
| 206 | } | ||
| 207 | } | ||
| 208 | 90 | return blocksize; | |
| 209 | } | ||
| 210 | |||
| 211 | |||
| 212 | 90 | static av_cold void dprint_compression_options(FlacEncodeContext *s) | |
| 213 | { | ||
| 214 | 90 | AVCodecContext *avctx = s->avctx; | |
| 215 | 90 | CompressionOptions *opt = &s->options; | |
| 216 | |||
| 217 | 90 | av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", opt->compression_level); | |
| 218 | |||
| 219 |
3/5✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 86 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
90 | switch (opt->lpc_type) { |
| 220 | ✗ | case FF_LPC_TYPE_NONE: | |
| 221 | ✗ | av_log(avctx, AV_LOG_DEBUG, " lpc type: None\n"); | |
| 222 | ✗ | break; | |
| 223 | 3 | case FF_LPC_TYPE_FIXED: | |
| 224 | 3 | av_log(avctx, AV_LOG_DEBUG, " lpc type: Fixed pre-defined coefficients\n"); | |
| 225 | 3 | break; | |
| 226 | 86 | case FF_LPC_TYPE_LEVINSON: | |
| 227 | 86 | av_log(avctx, AV_LOG_DEBUG, " lpc type: Levinson-Durbin recursion with Welch window\n"); | |
| 228 | 86 | break; | |
| 229 | 1 | case FF_LPC_TYPE_CHOLESKY: | |
| 230 | 1 | av_log(avctx, AV_LOG_DEBUG, " lpc type: Cholesky factorization, %d pass%s\n", | |
| 231 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | opt->lpc_passes, opt->lpc_passes == 1 ? "" : "es"); |
| 232 | 1 | break; | |
| 233 | } | ||
| 234 | |||
| 235 | 90 | av_log(avctx, AV_LOG_DEBUG, " prediction order: %d, %d\n", | |
| 236 | opt->min_prediction_order, opt->max_prediction_order); | ||
| 237 | |||
| 238 |
2/7✓ Branch 0 taken 89 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
90 | switch (opt->prediction_order_method) { |
| 239 | 89 | case ORDER_METHOD_EST: | |
| 240 | 89 | av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "estimate"); | |
| 241 | 89 | break; | |
| 242 | ✗ | case ORDER_METHOD_2LEVEL: | |
| 243 | ✗ | av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "2-level"); | |
| 244 | ✗ | break; | |
| 245 | 1 | case ORDER_METHOD_4LEVEL: | |
| 246 | 1 | av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "4-level"); | |
| 247 | 1 | break; | |
| 248 | ✗ | case ORDER_METHOD_8LEVEL: | |
| 249 | ✗ | av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "8-level"); | |
| 250 | ✗ | break; | |
| 251 | ✗ | case ORDER_METHOD_SEARCH: | |
| 252 | ✗ | av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "full search"); | |
| 253 | ✗ | break; | |
| 254 | ✗ | case ORDER_METHOD_LOG: | |
| 255 | ✗ | av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "log search"); | |
| 256 | ✗ | break; | |
| 257 | } | ||
| 258 | |||
| 259 | |||
| 260 | 90 | av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n", | |
| 261 | opt->min_partition_order, opt->max_partition_order); | ||
| 262 | |||
| 263 | 90 | av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", avctx->frame_size); | |
| 264 | |||
| 265 | 90 | av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n", | |
| 266 | opt->lpc_coeff_precision); | ||
| 267 | 90 | } | |
| 268 | |||
| 269 | |||
| 270 | 90 | static av_cold int flac_encode_init(AVCodecContext *avctx) | |
| 271 | { | ||
| 272 | 90 | int freq = avctx->sample_rate; | |
| 273 | 90 | int channels = avctx->ch_layout.nb_channels; | |
| 274 | 90 | FlacEncodeContext *s = avctx->priv_data; | |
| 275 | int i, level, ret; | ||
| 276 | uint8_t *streaminfo; | ||
| 277 | |||
| 278 | 90 | s->avctx = avctx; | |
| 279 | |||
| 280 |
2/3✓ Branch 0 taken 88 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
90 | switch (avctx->sample_fmt) { |
| 281 | 88 | case AV_SAMPLE_FMT_S16: | |
| 282 | 88 | avctx->bits_per_raw_sample = 16; | |
| 283 | 88 | s->bps_code = 4; | |
| 284 | 88 | break; | |
| 285 | 2 | case AV_SAMPLE_FMT_S32: | |
| 286 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (avctx->bits_per_raw_sample <= 24) { |
| 287 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (avctx->bits_per_raw_sample < 24) |
| 288 | ✗ | av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n"); | |
| 289 | 1 | avctx->bits_per_raw_sample = 24; | |
| 290 | 1 | s->bps_code = 6; | |
| 291 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | } else if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) { |
| 292 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
| 293 | "encoding as 24 bits-per-sample, more is considered " | ||
| 294 | "experimental. Add -strict experimental if you want " | ||
| 295 | "to encode more than 24 bits-per-sample\n"); | ||
| 296 | ✗ | avctx->bits_per_raw_sample = 24; | |
| 297 | ✗ | s->bps_code = 6; | |
| 298 | } else { | ||
| 299 | 1 | avctx->bits_per_raw_sample = 32; | |
| 300 | 1 | s->bps_code = 7; | |
| 301 | } | ||
| 302 | 2 | break; | |
| 303 | } | ||
| 304 | |||
| 305 |
2/4✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 90 times.
|
90 | if (channels < 1 || channels > FLAC_MAX_CHANNELS) { |
| 306 | ✗ | av_log(avctx, AV_LOG_ERROR, "%d channels not supported (max %d)\n", | |
| 307 | channels, FLAC_MAX_CHANNELS); | ||
| 308 | ✗ | return AVERROR(EINVAL); | |
| 309 | } | ||
| 310 | 90 | s->channels = channels; | |
| 311 | |||
| 312 | /* find samplerate in table */ | ||
| 313 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
|
90 | if (freq < 1) |
| 314 | ✗ | return AVERROR(EINVAL); | |
| 315 |
1/2✓ Branch 0 taken 767 times.
✗ Branch 1 not taken.
|
767 | for (i = 1; i < 12; i++) { |
| 316 |
2/2✓ Branch 0 taken 90 times.
✓ Branch 1 taken 677 times.
|
767 | if (freq == ff_flac_sample_rate_table[i]) { |
| 317 | 90 | s->samplerate = ff_flac_sample_rate_table[i]; | |
| 318 | 90 | s->sr_code[0] = i; | |
| 319 | 90 | s->sr_code[1] = 0; | |
| 320 | 90 | break; | |
| 321 | } | ||
| 322 | } | ||
| 323 | /* if not in table, samplerate is non-standard */ | ||
| 324 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
|
90 | if (i == 12) { |
| 325 | ✗ | if (freq % 1000 == 0 && freq < 255000) { | |
| 326 | ✗ | s->sr_code[0] = 12; | |
| 327 | ✗ | s->sr_code[1] = freq / 1000; | |
| 328 | ✗ | } else if (freq % 10 == 0 && freq < 655350) { | |
| 329 | ✗ | s->sr_code[0] = 14; | |
| 330 | ✗ | s->sr_code[1] = freq / 10; | |
| 331 | ✗ | } else if (freq < 65535) { | |
| 332 | ✗ | s->sr_code[0] = 13; | |
| 333 | ✗ | s->sr_code[1] = freq; | |
| 334 | ✗ | } else if (freq < 1048576) { | |
| 335 | ✗ | s->sr_code[0] = 0; | |
| 336 | ✗ | s->sr_code[1] = 0; | |
| 337 | } else { | ||
| 338 | ✗ | av_log(avctx, AV_LOG_ERROR, "%d Hz not supported\n", freq); | |
| 339 | ✗ | return AVERROR(EINVAL); | |
| 340 | } | ||
| 341 | ✗ | s->samplerate = freq; | |
| 342 | } | ||
| 343 | |||
| 344 | /* set compression option defaults based on avctx->compression_level */ | ||
| 345 |
2/2✓ Branch 0 taken 87 times.
✓ Branch 1 taken 3 times.
|
90 | if (avctx->compression_level < 0) |
| 346 | 87 | s->options.compression_level = 5; | |
| 347 | else | ||
| 348 | 3 | s->options.compression_level = avctx->compression_level; | |
| 349 | |||
| 350 | 90 | level = s->options.compression_level; | |
| 351 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
|
90 | if (level > 12) { |
| 352 | ✗ | av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n", | |
| 353 | s->options.compression_level); | ||
| 354 | ✗ | return AVERROR(EINVAL); | |
| 355 | } | ||
| 356 | |||
| 357 | 90 | s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level]; | |
| 358 | |||
| 359 |
2/2✓ Branch 0 taken 87 times.
✓ Branch 1 taken 3 times.
|
90 | if (s->options.lpc_type == FF_LPC_TYPE_DEFAULT) |
| 360 | 87 | s->options.lpc_type = ((int[]){ FF_LPC_TYPE_FIXED, FF_LPC_TYPE_FIXED, FF_LPC_TYPE_FIXED, | |
| 361 | FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, | ||
| 362 | FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, | ||
| 363 | FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, | ||
| 364 | 87 | FF_LPC_TYPE_LEVINSON})[level]; | |
| 365 | |||
| 366 |
1/2✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
|
90 | if (s->options.min_prediction_order < 0) |
| 367 | 90 | s->options.min_prediction_order = ((int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level]; | |
| 368 |
1/2✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
|
90 | if (s->options.max_prediction_order < 0) |
| 369 | 90 | s->options.max_prediction_order = ((int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level]; | |
| 370 | |||
| 371 |
1/2✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
|
90 | if (s->options.prediction_order_method < 0) |
| 372 | 90 | s->options.prediction_order_method = ((int[]){ ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST, | |
| 373 | ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST, | ||
| 374 | ORDER_METHOD_4LEVEL, ORDER_METHOD_LOG, ORDER_METHOD_4LEVEL, | ||
| 375 | ORDER_METHOD_LOG, ORDER_METHOD_SEARCH, ORDER_METHOD_LOG, | ||
| 376 | 90 | ORDER_METHOD_SEARCH})[level]; | |
| 377 | |||
| 378 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
|
90 | if (s->options.min_partition_order > s->options.max_partition_order) { |
| 379 | ✗ | av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n", | |
| 380 | s->options.min_partition_order, s->options.max_partition_order); | ||
| 381 | ✗ | return AVERROR(EINVAL); | |
| 382 | } | ||
| 383 |
1/2✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
|
90 | if (s->options.min_partition_order < 0) |
| 384 | 90 | s->options.min_partition_order = ((int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level]; | |
| 385 |
1/2✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
|
90 | if (s->options.max_partition_order < 0) |
| 386 | 90 | s->options.max_partition_order = ((int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level]; | |
| 387 | |||
| 388 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
|
90 | if (s->options.lpc_type == FF_LPC_TYPE_NONE) { |
| 389 | ✗ | s->options.min_prediction_order = 0; | |
| 390 | ✗ | s->options.max_prediction_order = 0; | |
| 391 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 87 times.
|
90 | } else if (s->options.lpc_type == FF_LPC_TYPE_FIXED) { |
| 392 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (s->options.min_prediction_order > MAX_FIXED_ORDER) { |
| 393 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
| 394 | "invalid min prediction order %d, clamped to %d\n", | ||
| 395 | s->options.min_prediction_order, MAX_FIXED_ORDER); | ||
| 396 | ✗ | s->options.min_prediction_order = MAX_FIXED_ORDER; | |
| 397 | } | ||
| 398 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (s->options.max_prediction_order > MAX_FIXED_ORDER) { |
| 399 | 1 | av_log(avctx, AV_LOG_WARNING, | |
| 400 | "invalid max prediction order %d, clamped to %d\n", | ||
| 401 | s->options.max_prediction_order, MAX_FIXED_ORDER); | ||
| 402 | 1 | s->options.max_prediction_order = MAX_FIXED_ORDER; | |
| 403 | } | ||
| 404 | } | ||
| 405 | |||
| 406 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
|
90 | if (s->options.max_prediction_order < s->options.min_prediction_order) { |
| 407 | ✗ | av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n", | |
| 408 | s->options.min_prediction_order, s->options.max_prediction_order); | ||
| 409 | ✗ | return AVERROR(EINVAL); | |
| 410 | } | ||
| 411 | |||
| 412 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
|
90 | if (avctx->frame_size > 0) { |
| 413 | ✗ | if (avctx->frame_size < FLAC_MIN_BLOCKSIZE || | |
| 414 | ✗ | avctx->frame_size > FLAC_MAX_BLOCKSIZE) { | |
| 415 | ✗ | av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n", | |
| 416 | avctx->frame_size); | ||
| 417 | ✗ | return AVERROR(EINVAL); | |
| 418 | } | ||
| 419 | } else { | ||
| 420 | 90 | s->avctx->frame_size = select_blocksize(s->samplerate, s->options.block_time_ms); | |
| 421 | } | ||
| 422 | 90 | s->max_blocksize = s->avctx->frame_size; | |
| 423 | |||
| 424 | /* set maximum encoded frame size in verbatim mode */ | ||
| 425 | 180 | s->max_framesize = flac_get_max_frame_size(s->avctx->frame_size, | |
| 426 | s->channels, | ||
| 427 | 90 | s->avctx->bits_per_raw_sample); | |
| 428 | |||
| 429 | /* initialize MD5 context */ | ||
| 430 | 90 | s->md5ctx = av_md5_alloc(); | |
| 431 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
|
90 | if (!s->md5ctx) |
| 432 | ✗ | return AVERROR(ENOMEM); | |
| 433 | 90 | av_md5_init(s->md5ctx); | |
| 434 | |||
| 435 | 90 | streaminfo = av_malloc(FLAC_STREAMINFO_SIZE); | |
| 436 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
|
90 | if (!streaminfo) |
| 437 | ✗ | return AVERROR(ENOMEM); | |
| 438 | 90 | write_streaminfo(s, streaminfo); | |
| 439 | 90 | avctx->extradata = streaminfo; | |
| 440 | 90 | avctx->extradata_size = FLAC_STREAMINFO_SIZE; | |
| 441 | |||
| 442 | 90 | s->frame_count = 0; | |
| 443 | 90 | s->min_framesize = s->max_framesize; | |
| 444 | |||
| 445 |
3/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 86 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
94 | if ((channels == 3 && |
| 446 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
|
94 | av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_SURROUND)) || |
| 447 | ✗ | (channels == 4 && | |
| 448 | ✗ | av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_2_2) && | |
| 449 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
|
90 | av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_QUAD)) || |
| 450 | ✗ | (channels == 5 && | |
| 451 | ✗ | av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0) && | |
| 452 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 2 taken 83 times.
|
90 | av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0_BACK)) || |
| 453 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
|
7 | (channels == 6 && |
| 454 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
13 | av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1) && |
| 455 | 6 | av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK))) { | |
| 456 | ✗ | if (avctx->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) { | |
| 457 | ✗ | av_log(avctx, AV_LOG_ERROR, "Channel layout not supported by Flac, " | |
| 458 | "output stream will have incorrect " | ||
| 459 | "channel layout.\n"); | ||
| 460 | } else { | ||
| 461 | ✗ | av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The encoder " | |
| 462 | "will use Flac channel layout for " | ||
| 463 | "%d channels.\n", channels); | ||
| 464 | } | ||
| 465 | } | ||
| 466 | |||
| 467 | 90 | ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size, | |
| 468 | s->options.max_prediction_order, FF_LPC_TYPE_LEVINSON); | ||
| 469 | |||
| 470 | 90 | ff_bswapdsp_init(&s->bdsp); | |
| 471 | 90 | ff_flacencdsp_init(&s->flac_dsp); | |
| 472 | |||
| 473 | 90 | dprint_compression_options(s); | |
| 474 | |||
| 475 | 90 | return ret; | |
| 476 | } | ||
| 477 | |||
| 478 | |||
| 479 | 5054 | static void init_frame(FlacEncodeContext *s, int nb_samples) | |
| 480 | { | ||
| 481 | int i, ch; | ||
| 482 | FlacFrame *frame; | ||
| 483 | |||
| 484 | 5054 | frame = &s->frame; | |
| 485 | |||
| 486 |
2/2✓ Branch 0 taken 35971 times.
✓ Branch 1 taken 71 times.
|
36042 | for (i = 0; i < 16; i++) { |
| 487 |
2/2✓ Branch 0 taken 4983 times.
✓ Branch 1 taken 30988 times.
|
35971 | if (nb_samples == ff_flac_blocksize_table[i]) { |
| 488 | 4983 | frame->blocksize = ff_flac_blocksize_table[i]; | |
| 489 | 4983 | frame->bs_code[0] = i; | |
| 490 | 4983 | frame->bs_code[1] = 0; | |
| 491 | 4983 | break; | |
| 492 | } | ||
| 493 | } | ||
| 494 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 4983 times.
|
5054 | if (i == 16) { |
| 495 | 71 | frame->blocksize = nb_samples; | |
| 496 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (frame->blocksize <= 256) { |
| 497 | ✗ | frame->bs_code[0] = 6; | |
| 498 | ✗ | frame->bs_code[1] = frame->blocksize-1; | |
| 499 | } else { | ||
| 500 | 71 | frame->bs_code[0] = 7; | |
| 501 | 71 | frame->bs_code[1] = frame->blocksize-1; | |
| 502 | } | ||
| 503 | } | ||
| 504 | |||
| 505 |
2/2✓ Branch 0 taken 13954 times.
✓ Branch 1 taken 5054 times.
|
19008 | for (ch = 0; ch < s->channels; ch++) { |
| 506 | 13954 | FlacSubframe *sub = &frame->subframes[ch]; | |
| 507 | |||
| 508 | 13954 | sub->wasted = 0; | |
| 509 | 13954 | sub->obits = s->avctx->bits_per_raw_sample; | |
| 510 | |||
| 511 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 13698 times.
|
13954 | if (sub->obits > 16) |
| 512 | 256 | sub->rc.coding_mode = CODING_MODE_RICE2; | |
| 513 | else | ||
| 514 | 13698 | sub->rc.coding_mode = CODING_MODE_RICE; | |
| 515 | } | ||
| 516 | |||
| 517 | 5054 | frame->verbatim_only = 0; | |
| 518 | 5054 | } | |
| 519 | |||
| 520 | |||
| 521 | /** | ||
| 522 | * Copy channel-interleaved input samples into separate subframes. | ||
| 523 | */ | ||
| 524 | 5054 | static void copy_samples(FlacEncodeContext *s, const void *samples) | |
| 525 | { | ||
| 526 | int i, j, ch; | ||
| 527 | FlacFrame *frame; | ||
| 528 | |||
| 529 | #define COPY_SAMPLES(bits, shift0) do { \ | ||
| 530 | const int ## bits ## _t *samples0 = samples; \ | ||
| 531 | const int shift = shift0; \ | ||
| 532 | frame = &s->frame; \ | ||
| 533 | for (i = 0, j = 0; i < frame->blocksize; i++) \ | ||
| 534 | for (ch = 0; ch < s->channels; ch++, j++) \ | ||
| 535 | frame->subframes[ch].samples[i] = samples0[j] >> shift; \ | ||
| 536 | } while (0) | ||
| 537 | |||
| 538 |
2/2✓ Branch 0 taken 4926 times.
✓ Branch 1 taken 128 times.
|
5054 | if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S16) |
| 539 |
4/4✓ Branch 0 taken 79879650 times.
✓ Branch 1 taken 27181891 times.
✓ Branch 2 taken 27181891 times.
✓ Branch 3 taken 4926 times.
|
107066467 | COPY_SAMPLES(16, 0); |
| 540 | else | ||
| 541 |
4/4✓ Branch 0 taken 3928266 times.
✓ Branch 1 taken 1964133 times.
✓ Branch 2 taken 1964133 times.
✓ Branch 3 taken 128 times.
|
5892527 | COPY_SAMPLES(32, 32 - s->avctx->bits_per_raw_sample); |
| 542 | 5054 | } | |
| 543 | |||
| 544 | |||
| 545 | 99517 | static uint64_t rice_count_exact(const int32_t *res, int n, int k) | |
| 546 | { | ||
| 547 | int i; | ||
| 548 | 99517 | uint64_t count = 0; | |
| 549 | |||
| 550 |
2/2✓ Branch 0 taken 47351638 times.
✓ Branch 1 taken 99517 times.
|
47451155 | for (i = 0; i < n; i++) { |
| 551 | 47351638 | unsigned v = ((unsigned)(res[i]) << 1) ^ (res[i] >> 31); | |
| 552 | 47351638 | count += (v >> k) + 1 + k; | |
| 553 | } | ||
| 554 | 99517 | return count; | |
| 555 | } | ||
| 556 | |||
| 557 | |||
| 558 | 13954 | static uint64_t subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub, | |
| 559 | int pred_order) | ||
| 560 | { | ||
| 561 | int p, porder, psize; | ||
| 562 | int i, part_end; | ||
| 563 | 13954 | uint64_t count = 0; | |
| 564 | |||
| 565 | /* subframe header */ | ||
| 566 | 13954 | count += 8; | |
| 567 | |||
| 568 |
2/2✓ Branch 0 taken 5212 times.
✓ Branch 1 taken 8742 times.
|
13954 | if (sub->wasted) |
| 569 | 5212 | count += sub->wasted; | |
| 570 | |||
| 571 | /* subframe */ | ||
| 572 |
2/2✓ Branch 0 taken 5906 times.
✓ Branch 1 taken 8048 times.
|
13954 | if (sub->type == FLAC_SUBFRAME_CONSTANT) { |
| 573 | 5906 | count += sub->obits; | |
| 574 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8047 times.
|
8048 | } else if (sub->type == FLAC_SUBFRAME_VERBATIM) { |
| 575 | 1 | count += s->frame.blocksize * sub->obits; | |
| 576 | } else { | ||
| 577 | /* warm-up samples */ | ||
| 578 | 8047 | count += pred_order * sub->obits; | |
| 579 | |||
| 580 | /* LPC coefficients */ | ||
| 581 |
2/2✓ Branch 0 taken 7173 times.
✓ Branch 1 taken 874 times.
|
8047 | if (sub->type == FLAC_SUBFRAME_LPC) |
| 582 | 7173 | count += 4 + 5 + pred_order * s->options.lpc_coeff_precision; | |
| 583 | |||
| 584 | /* rice-encoded block */ | ||
| 585 | 8047 | count += 2; | |
| 586 | |||
| 587 | /* partition order */ | ||
| 588 | 8047 | porder = sub->rc.porder; | |
| 589 | 8047 | psize = s->frame.blocksize >> porder; | |
| 590 | 8047 | count += 4; | |
| 591 | |||
| 592 | /* residual */ | ||
| 593 | 8047 | i = pred_order; | |
| 594 | 8047 | part_end = psize; | |
| 595 |
2/2✓ Branch 0 taken 99517 times.
✓ Branch 1 taken 8047 times.
|
107564 | for (p = 0; p < 1 << porder; p++) { |
| 596 | 99517 | int k = sub->rc.params[p]; | |
| 597 | 99517 | count += sub->rc.coding_mode; | |
| 598 | 99517 | count += rice_count_exact(&sub->residual[i], part_end - i, k); | |
| 599 | 99517 | i = part_end; | |
| 600 | 99517 | part_end = FFMIN(s->frame.blocksize, part_end + psize); | |
| 601 | } | ||
| 602 | } | ||
| 603 | |||
| 604 | 13954 | return count; | |
| 605 | } | ||
| 606 | |||
| 607 | |||
| 608 | #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k))) | ||
| 609 | |||
| 610 | /** | ||
| 611 | * Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0. | ||
| 612 | */ | ||
| 613 | 31323794 | static int find_optimal_param(uint64_t sum, int n, int max_param) | |
| 614 | { | ||
| 615 | int k; | ||
| 616 | uint64_t sum2; | ||
| 617 | |||
| 618 |
2/2✓ Branch 0 taken 1394749 times.
✓ Branch 1 taken 29929045 times.
|
31323794 | if (sum <= n >> 1) |
| 619 | 1394749 | return 0; | |
| 620 | 29929045 | sum2 = sum - (n >> 1); | |
| 621 | 29929045 | k = av_log2(av_clipl_int32(sum2 / n)); | |
| 622 | 29929045 | return FFMIN(k, max_param); | |
| 623 | } | ||
| 624 | |||
| 625 | 27285 | static int find_optimal_param_exact(uint64_t sums[32][MAX_PARTITIONS], int i, int max_param) | |
| 626 | { | ||
| 627 | 27285 | int bestk = 0; | |
| 628 | 27285 | int64_t bestbits = INT64_MAX; | |
| 629 | int k; | ||
| 630 | |||
| 631 |
2/2✓ Branch 0 taken 409275 times.
✓ Branch 1 taken 27285 times.
|
436560 | for (k = 0; k <= max_param; k++) { |
| 632 | 409275 | int64_t bits = sums[k][i]; | |
| 633 |
2/2✓ Branch 0 taken 253571 times.
✓ Branch 1 taken 155704 times.
|
409275 | if (bits < bestbits) { |
| 634 | 253571 | bestbits = bits; | |
| 635 | 253571 | bestk = k; | |
| 636 | } | ||
| 637 | } | ||
| 638 | |||
| 639 | 27285 | return bestk; | |
| 640 | } | ||
| 641 | |||
| 642 | 670035 | static uint64_t calc_optimal_rice_params(RiceContext *rc, int porder, | |
| 643 | uint64_t sums[32][MAX_PARTITIONS], | ||
| 644 | int n, int pred_order, int max_param, int exact) | ||
| 645 | { | ||
| 646 | int i; | ||
| 647 | int k, cnt, part; | ||
| 648 | uint64_t all_bits; | ||
| 649 | |||
| 650 | 670035 | part = (1 << porder); | |
| 651 | 670035 | all_bits = 4 * part; | |
| 652 | |||
| 653 | 670035 | cnt = (n >> porder) - pred_order; | |
| 654 |
2/2✓ Branch 0 taken 31339815 times.
✓ Branch 1 taken 670035 times.
|
32009850 | for (i = 0; i < part; i++) { |
| 655 |
2/2✓ Branch 0 taken 27285 times.
✓ Branch 1 taken 31312530 times.
|
31339815 | if (exact) { |
| 656 | 27285 | k = find_optimal_param_exact(sums, i, max_param); | |
| 657 | 27285 | all_bits += sums[k][i]; | |
| 658 | } else { | ||
| 659 | 31312530 | k = find_optimal_param(sums[0][i], cnt, max_param); | |
| 660 | 31312530 | all_bits += rice_encode_count(sums[0][i], cnt, k); | |
| 661 | } | ||
| 662 | 31339815 | rc->params[i] = k; | |
| 663 | 31339815 | cnt = n >> porder; | |
| 664 | } | ||
| 665 | |||
| 666 | 670035 | rc->porder = porder; | |
| 667 | |||
| 668 | 670035 | return all_bits; | |
| 669 | } | ||
| 670 | |||
| 671 | |||
| 672 | 91401 | static void calc_sum_top(int pmax, int kmax, const uint32_t *data, int n, int pred_order, | |
| 673 | uint64_t sums[32][MAX_PARTITIONS]) | ||
| 674 | { | ||
| 675 | int i, k; | ||
| 676 | int parts; | ||
| 677 | const uint32_t *res, *res_end; | ||
| 678 | |||
| 679 | /* sums for highest level */ | ||
| 680 | 91401 | parts = (1 << pmax); | |
| 681 | |||
| 682 |
2/2✓ Branch 0 taken 116867 times.
✓ Branch 1 taken 91401 times.
|
208268 | for (k = 0; k <= kmax; k++) { |
| 683 | 116867 | res = &data[pred_order]; | |
| 684 | 116867 | res_end = &data[n >> pmax]; | |
| 685 |
2/2✓ Branch 0 taken 15919336 times.
✓ Branch 1 taken 116867 times.
|
16036203 | for (i = 0; i < parts; i++) { |
| 686 |
2/2✓ Branch 0 taken 218280 times.
✓ Branch 1 taken 15701056 times.
|
15919336 | if (kmax) { |
| 687 | 218280 | uint64_t sum = (1LL + k) * (res_end - res); | |
| 688 |
2/2✓ Branch 0 taken 31324725 times.
✓ Branch 1 taken 218280 times.
|
31543005 | while (res < res_end) |
| 689 | 31324725 | sum += *(res++) >> k; | |
| 690 | 218280 | sums[k][i] = sum; | |
| 691 | } else { | ||
| 692 | 15701056 | uint64_t sum = 0; | |
| 693 |
2/2✓ Branch 0 taken 408741588 times.
✓ Branch 1 taken 15701056 times.
|
424442644 | while (res < res_end) |
| 694 | 408741588 | sum += *(res++); | |
| 695 | 15701056 | sums[k][i] = sum; | |
| 696 | } | ||
| 697 | 15919336 | res_end += n >> pmax; | |
| 698 | } | ||
| 699 | } | ||
| 700 | 91401 | } | |
| 701 | |||
| 702 | 578634 | static void calc_sum_next(int level, uint64_t sums[32][MAX_PARTITIONS], int kmax) | |
| 703 | { | ||
| 704 | int i, k; | ||
| 705 | 578634 | int parts = (1 << level); | |
| 706 |
2/2✓ Branch 0 taken 15624207 times.
✓ Branch 1 taken 578634 times.
|
16202841 | for (i = 0; i < parts; i++) { |
| 707 |
2/2✓ Branch 0 taken 15802469 times.
✓ Branch 1 taken 15624207 times.
|
31426676 | for (k=0; k<=kmax; k++) |
| 708 | 15802469 | sums[k][i] = sums[k][2*i] + sums[k][2*i+1]; | |
| 709 | } | ||
| 710 | 578634 | } | |
| 711 | |||
| 712 | 91401 | static uint64_t calc_rice_params(RiceContext *rc, | |
| 713 | uint32_t udata[FLAC_MAX_BLOCKSIZE], | ||
| 714 | uint64_t sums[32][MAX_PARTITIONS], | ||
| 715 | int pmin, int pmax, | ||
| 716 | const int32_t *data, int n, int pred_order, int exact) | ||
| 717 | { | ||
| 718 | int i; | ||
| 719 | uint64_t bits[MAX_PARTITION_ORDER+1]; | ||
| 720 | int opt_porder; | ||
| 721 | RiceContext tmp_rc; | ||
| 722 | 91401 | int kmax = (1 << rc->coding_mode) - 2; | |
| 723 | |||
| 724 | av_assert1(pmin >= 0 && pmin <= MAX_PARTITION_ORDER); | ||
| 725 | av_assert1(pmax >= 0 && pmax <= MAX_PARTITION_ORDER); | ||
| 726 | av_assert1(pmin <= pmax); | ||
| 727 | |||
| 728 | 91401 | tmp_rc.coding_mode = rc->coding_mode; | |
| 729 | |||
| 730 |
2/2✓ Branch 0 taken 410829903 times.
✓ Branch 1 taken 91401 times.
|
410921304 | for (i = pred_order; i < n; i++) |
| 731 | 410829903 | udata[i] = ((unsigned)(data[i]) << 1) ^ (data[i] >> 31); | |
| 732 | |||
| 733 |
2/2✓ Branch 0 taken 1819 times.
✓ Branch 1 taken 89582 times.
|
91401 | calc_sum_top(pmax, exact ? kmax : 0, udata, n, pred_order, sums); |
| 734 | |||
| 735 | 91401 | opt_porder = pmin; | |
| 736 | 91401 | bits[pmin] = UINT32_MAX; | |
| 737 | 91401 | for (i = pmax; ; ) { | |
| 738 | 670035 | bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums, n, pred_order, kmax, exact); | |
| 739 |
4/4✓ Branch 0 taken 278141 times.
✓ Branch 1 taken 391894 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 278137 times.
|
670035 | if (bits[i] < bits[opt_porder] || pmax == pmin) { |
| 740 | 391898 | opt_porder = i; | |
| 741 | 391898 | *rc = tmp_rc; | |
| 742 | } | ||
| 743 |
2/2✓ Branch 0 taken 91401 times.
✓ Branch 1 taken 578634 times.
|
670035 | if (i == pmin) |
| 744 | 91401 | break; | |
| 745 |
2/2✓ Branch 0 taken 5457 times.
✓ Branch 1 taken 573177 times.
|
578634 | calc_sum_next(--i, sums, exact ? kmax : 0); |
| 746 | } | ||
| 747 | |||
| 748 | 91401 | return bits[opt_porder]; | |
| 749 | } | ||
| 750 | |||
| 751 | |||
| 752 | 182802 | static int get_max_p_order(int max_porder, int n, int order) | |
| 753 | { | ||
| 754 | 182802 | int porder = FFMIN(max_porder, av_log2(n^(n-1))); | |
| 755 |
2/2✓ Branch 0 taken 181222 times.
✓ Branch 1 taken 1580 times.
|
182802 | if (order > 0) |
| 756 | 181222 | porder = FFMIN(porder, av_log2(n/order)); | |
| 757 | 182802 | return porder; | |
| 758 | } | ||
| 759 | |||
| 760 | |||
| 761 | 91401 | static uint64_t find_subframe_rice_params(FlacEncodeContext *s, | |
| 762 | FlacSubframe *sub, int pred_order) | ||
| 763 | { | ||
| 764 | 91401 | int pmin = get_max_p_order(s->options.min_partition_order, | |
| 765 | s->frame.blocksize, pred_order); | ||
| 766 | 91401 | int pmax = get_max_p_order(s->options.max_partition_order, | |
| 767 | s->frame.blocksize, pred_order); | ||
| 768 | |||
| 769 | 91401 | uint64_t bits = 8 + pred_order * sub->obits + 2 + sub->rc.coding_mode; | |
| 770 |
2/2✓ Branch 0 taken 86849 times.
✓ Branch 1 taken 4552 times.
|
91401 | if (sub->type == FLAC_SUBFRAME_LPC) |
| 771 | 86849 | bits += 4 + 5 + pred_order * s->options.lpc_coeff_precision; | |
| 772 | 91401 | bits += calc_rice_params(&sub->rc, sub->rc_udata, sub->rc_sums, pmin, pmax, sub->residual, | |
| 773 | s->frame.blocksize, pred_order, s->options.exact_rice_parameters); | ||
| 774 | 91401 | return bits; | |
| 775 | } | ||
| 776 | |||
| 777 | |||
| 778 | 4552 | static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n, | |
| 779 | int order) | ||
| 780 | { | ||
| 781 | int i; | ||
| 782 | |||
| 783 |
2/2✓ Branch 0 taken 9292 times.
✓ Branch 1 taken 4552 times.
|
13844 | for (i = 0; i < order; i++) |
| 784 | 9292 | res[i] = smp[i]; | |
| 785 | |||
| 786 |
2/2✓ Branch 0 taken 790 times.
✓ Branch 1 taken 3762 times.
|
4552 | if (order == 0) { |
| 787 |
2/2✓ Branch 0 taken 908640 times.
✓ Branch 1 taken 790 times.
|
909430 | for (i = order; i < n; i++) |
| 788 | 908640 | res[i] = smp[i]; | |
| 789 |
2/2✓ Branch 0 taken 932 times.
✓ Branch 1 taken 2830 times.
|
3762 | } else if (order == 1) { |
| 790 |
2/2✓ Branch 0 taken 1899976 times.
✓ Branch 1 taken 932 times.
|
1900908 | for (i = order; i < n; i++) |
| 791 | 1899976 | res[i] = smp[i] - smp[i-1]; | |
| 792 |
2/2✓ Branch 0 taken 1004 times.
✓ Branch 1 taken 1826 times.
|
2830 | } else if (order == 2) { |
| 793 | 1004 | int a = smp[order-1] - smp[order-2]; | |
| 794 |
2/2✓ Branch 0 taken 1090768 times.
✓ Branch 1 taken 1004 times.
|
1091772 | for (i = order; i < n; i += 2) { |
| 795 | 1090768 | int b = smp[i ] - smp[i-1]; | |
| 796 | 1090768 | res[i] = b - a; | |
| 797 | 1090768 | a = smp[i+1] - smp[i ]; | |
| 798 | 1090768 | res[i+1] = a - b; | |
| 799 | } | ||
| 800 |
2/2✓ Branch 0 taken 952 times.
✓ Branch 1 taken 874 times.
|
1826 | } else if (order == 3) { |
| 801 | 952 | int a = smp[order-1] - smp[order-2]; | |
| 802 | 952 | int c = smp[order-1] - 2*smp[order-2] + smp[order-3]; | |
| 803 |
2/2✓ Branch 0 taken 874622 times.
✓ Branch 1 taken 952 times.
|
875574 | for (i = order; i < n; i += 2) { |
| 804 | 874622 | int b = smp[i ] - smp[i-1]; | |
| 805 | 874622 | int d = b - a; | |
| 806 | 874622 | res[i] = d - c; | |
| 807 | 874622 | a = smp[i+1] - smp[i ]; | |
| 808 | 874622 | c = a - b; | |
| 809 | 874622 | res[i+1] = c - d; | |
| 810 | } | ||
| 811 | } else { | ||
| 812 | 874 | int a = smp[order-1] - smp[order-2]; | |
| 813 | 874 | int c = smp[order-1] - 2*smp[order-2] + smp[order-3]; | |
| 814 | 874 | int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4]; | |
| 815 |
2/2✓ Branch 0 taken 815074 times.
✓ Branch 1 taken 874 times.
|
815948 | for (i = order; i < n; i += 2) { |
| 816 | 815074 | int b = smp[i ] - smp[i-1]; | |
| 817 | 815074 | int d = b - a; | |
| 818 | 815074 | int f = d - c; | |
| 819 | 815074 | res[i ] = f - e; | |
| 820 | 815074 | a = smp[i+1] - smp[i ]; | |
| 821 | 815074 | c = a - b; | |
| 822 | 815074 | e = c - d; | |
| 823 | 815074 | res[i+1] = e - f; | |
| 824 | } | ||
| 825 | } | ||
| 826 | 4552 | } | |
| 827 | |||
| 828 | |||
| 829 | /* These four functions check for every residual whether it can be | ||
| 830 | * contained in <INT32_MIN,INT32_MAX]. In case it doesn't, the | ||
| 831 | * function that called this function has to try something else. | ||
| 832 | * Each function is duplicated, once for int32_t input, once for | ||
| 833 | * int64_t input */ | ||
| 834 | #define ENCODE_RESIDUAL_FIXED_WITH_RESIDUAL_LIMIT() \ | ||
| 835 | { \ | ||
| 836 | for (int i = 0; i < order; i++) \ | ||
| 837 | res[i] = smp[i]; \ | ||
| 838 | if (order == 0) { \ | ||
| 839 | for (int i = order; i < n; i++) { \ | ||
| 840 | if (smp[i] == INT32_MIN) \ | ||
| 841 | return 1; \ | ||
| 842 | res[i] = smp[i]; \ | ||
| 843 | } \ | ||
| 844 | } else if (order == 1) { \ | ||
| 845 | for (int i = order; i < n; i++) { \ | ||
| 846 | int64_t res64 = (int64_t)smp[i] - smp[i-1]; \ | ||
| 847 | if (res64 <= INT32_MIN || res64 > INT32_MAX) \ | ||
| 848 | return 1; \ | ||
| 849 | res[i] = res64; \ | ||
| 850 | } \ | ||
| 851 | } else if (order == 2) { \ | ||
| 852 | for (int i = order; i < n; i++) { \ | ||
| 853 | int64_t res64 = (int64_t)smp[i] - 2*(int64_t)smp[i-1] + smp[i-2]; \ | ||
| 854 | if (res64 <= INT32_MIN || res64 > INT32_MAX) \ | ||
| 855 | return 1; \ | ||
| 856 | res[i] = res64; \ | ||
| 857 | } \ | ||
| 858 | } else if (order == 3) { \ | ||
| 859 | for (int i = order; i < n; i++) { \ | ||
| 860 | int64_t res64 = (int64_t)smp[i] - 3*(int64_t)smp[i-1] + 3*(int64_t)smp[i-2] - smp[i-3]; \ | ||
| 861 | if (res64 <= INT32_MIN || res64 > INT32_MAX) \ | ||
| 862 | return 1; \ | ||
| 863 | res[i] = res64; \ | ||
| 864 | } \ | ||
| 865 | } else { \ | ||
| 866 | for (int i = order; i < n; i++) { \ | ||
| 867 | int64_t res64 = (int64_t)smp[i] - 4*(int64_t)smp[i-1] + 6*(int64_t)smp[i-2] - 4*(int64_t)smp[i-3] + smp[i-4]; \ | ||
| 868 | if (res64 <= INT32_MIN || res64 > INT32_MAX) \ | ||
| 869 | return 1; \ | ||
| 870 | res[i] = res64; \ | ||
| 871 | } \ | ||
| 872 | } \ | ||
| 873 | return 0; \ | ||
| 874 | } | ||
| 875 | |||
| 876 | ✗ | static int encode_residual_fixed_with_residual_limit(int32_t *res, const int32_t *smp, | |
| 877 | int n, int order) | ||
| 878 | { | ||
| 879 | ✗ | ENCODE_RESIDUAL_FIXED_WITH_RESIDUAL_LIMIT(); | |
| 880 | } | ||
| 881 | |||
| 882 | |||
| 883 | ✗ | static int encode_residual_fixed_with_residual_limit_33bps(int32_t *res, const int64_t *smp, | |
| 884 | int n, int order) | ||
| 885 | { | ||
| 886 | ✗ | ENCODE_RESIDUAL_FIXED_WITH_RESIDUAL_LIMIT(); | |
| 887 | } | ||
| 888 | |||
| 889 | #define LPC_ENCODE_WITH_RESIDUAL_LIMIT() \ | ||
| 890 | { \ | ||
| 891 | for (int i = 0; i < order; i++) \ | ||
| 892 | res[i] = smp[i]; \ | ||
| 893 | for (int i = order; i < len; i++) { \ | ||
| 894 | int64_t p = 0, tmp; \ | ||
| 895 | for (int j = 0; j < order; j++) \ | ||
| 896 | p += (int64_t)coefs[j]*smp[(i-1)-j]; \ | ||
| 897 | p >>= shift; \ | ||
| 898 | tmp = smp[i] - p; \ | ||
| 899 | if (tmp <= INT32_MIN || tmp > INT32_MAX) \ | ||
| 900 | return 1; \ | ||
| 901 | res[i] = tmp; \ | ||
| 902 | } \ | ||
| 903 | return 0; \ | ||
| 904 | } | ||
| 905 | |||
| 906 | 13 | static int lpc_encode_with_residual_limit(int32_t *res, const int32_t *smp, int len, | |
| 907 | int order, int32_t *coefs, int shift) | ||
| 908 | { | ||
| 909 |
8/10✓ Branch 0 taken 94 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 401314 times.
✓ Branch 3 taken 55916 times.
✓ Branch 4 taken 55916 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 55916 times.
✓ Branch 8 taken 55916 times.
✓ Branch 9 taken 13 times.
|
457337 | LPC_ENCODE_WITH_RESIDUAL_LIMIT(); |
| 910 | } | ||
| 911 | |||
| 912 | 3 | static int lpc_encode_with_residual_limit_33bps(int32_t *res, const int64_t *smp, int len, | |
| 913 | int order, int32_t *coefs, int shift) | ||
| 914 | { | ||
| 915 |
9/10✓ Branch 0 taken 24 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 73840 times.
✓ Branch 3 taken 9230 times.
✓ Branch 4 taken 9230 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 9229 times.
✓ Branch 8 taken 9230 times.
✓ Branch 9 taken 2 times.
|
83096 | LPC_ENCODE_WITH_RESIDUAL_LIMIT(); |
| 916 | } | ||
| 917 | |||
| 918 | 86850 | static int lpc_encode_choose_datapath(FlacEncodeContext *s, int32_t bps, | |
| 919 | int32_t *res, const int32_t *smp, | ||
| 920 | const int64_t *smp_33bps, int len, | ||
| 921 | int order, int32_t *coefs, int shift) | ||
| 922 | { | ||
| 923 | 86850 | uint64_t max_residual_value = 0; | |
| 924 | 86850 | int64_t max_sample_value = ((int64_t)(1) << (bps-1)); | |
| 925 | /* This calculates the max size of any residual with the current | ||
| 926 | * predictor, so we know whether we need to check the residual */ | ||
| 927 |
2/2✓ Branch 0 taken 651429 times.
✓ Branch 1 taken 86850 times.
|
738279 | for (int i = 0; i < order; i++) |
| 928 | 651429 | max_residual_value += FFABS(max_sample_value * coefs[i]); | |
| 929 | 86850 | max_residual_value >>= shift; | |
| 930 | 86850 | max_residual_value += max_sample_value; | |
| 931 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 86847 times.
|
86850 | if (bps > 32) { |
| 932 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
|
3 | if (lpc_encode_with_residual_limit_33bps(res, smp_33bps, len, order, coefs, shift)) |
| 933 | 1 | return 1; | |
| 934 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 86834 times.
|
86847 | } else if (max_residual_value > INT32_MAX) { |
| 935 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
|
13 | if (lpc_encode_with_residual_limit(res, smp, len, order, coefs, shift)) |
| 936 | ✗ | return 1; | |
| 937 |
2/2✓ Branch 0 taken 5213 times.
✓ Branch 1 taken 81621 times.
|
86834 | } else if (bps + s->options.lpc_coeff_precision + av_log2(order) <= 32) { |
| 938 | 5213 | s->flac_dsp.lpc16_encode(res, smp, len, order, coefs, shift); | |
| 939 | } else { | ||
| 940 | 81621 | s->flac_dsp.lpc32_encode(res, smp, len, order, coefs, shift); | |
| 941 | } | ||
| 942 | 86849 | return 0; | |
| 943 | } | ||
| 944 | |||
| 945 | #define DEFAULT_TO_VERBATIM() \ | ||
| 946 | { \ | ||
| 947 | sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM; \ | ||
| 948 | if (sub->obits <= 32) \ | ||
| 949 | memcpy(res, smp, n * sizeof(int32_t)); \ | ||
| 950 | return subframe_count_exact(s, sub, 0); \ | ||
| 951 | } | ||
| 952 | |||
| 953 | 13954 | static int encode_residual_ch(FlacEncodeContext *s, int ch) | |
| 954 | { | ||
| 955 | int i, n; | ||
| 956 | int min_order, max_order, opt_order, omethod; | ||
| 957 | FlacFrame *frame; | ||
| 958 | FlacSubframe *sub; | ||
| 959 | int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER]; | ||
| 960 | int shift[MAX_LPC_ORDER]; | ||
| 961 | int32_t *res, *smp; | ||
| 962 | int64_t *smp_33bps; | ||
| 963 | |||
| 964 | 13954 | frame = &s->frame; | |
| 965 | 13954 | sub = &frame->subframes[ch]; | |
| 966 | 13954 | res = sub->residual; | |
| 967 | 13954 | smp = sub->samples; | |
| 968 | 13954 | smp_33bps = frame->samples_33bps; | |
| 969 | 13954 | n = frame->blocksize; | |
| 970 | |||
| 971 | /* CONSTANT */ | ||
| 972 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 13951 times.
|
13954 | if (sub->obits > 32) { |
| 973 |
1/2✓ Branch 0 taken 3764 times.
✗ Branch 1 not taken.
|
3764 | for (i = 1; i < n; i++) |
| 974 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3761 times.
|
3764 | if(smp_33bps[i] != smp_33bps[0]) |
| 975 | 3 | break; | |
| 976 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (i == n) { |
| 977 | ✗ | sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT; | |
| 978 | ✗ | return subframe_count_exact(s, sub, 0); | |
| 979 | } | ||
| 980 | } else { | ||
| 981 |
2/2✓ Branch 0 taken 36484283 times.
✓ Branch 1 taken 5906 times.
|
36490189 | for (i = 1; i < n; i++) |
| 982 |
2/2✓ Branch 0 taken 8045 times.
✓ Branch 1 taken 36476238 times.
|
36484283 | if(smp[i] != smp[0]) |
| 983 | 8045 | break; | |
| 984 |
2/2✓ Branch 0 taken 5906 times.
✓ Branch 1 taken 8045 times.
|
13951 | if (i == n) { |
| 985 | 5906 | sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT; | |
| 986 | 5906 | res[0] = smp[0]; | |
| 987 | 5906 | return subframe_count_exact(s, sub, 0); | |
| 988 | } | ||
| 989 | } | ||
| 990 | |||
| 991 | /* VERBATIM */ | ||
| 992 |
2/4✓ Branch 0 taken 8048 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8048 times.
|
8048 | if (frame->verbatim_only || n < 5) { |
| 993 | ✗ | DEFAULT_TO_VERBATIM(); | |
| 994 | } | ||
| 995 | |||
| 996 | 8048 | min_order = s->options.min_prediction_order; | |
| 997 | 8048 | max_order = s->options.max_prediction_order; | |
| 998 | 8048 | omethod = s->options.prediction_order_method; | |
| 999 | |||
| 1000 | /* FIXED */ | ||
| 1001 | 8048 | sub->type = FLAC_SUBFRAME_FIXED; | |
| 1002 |
1/2✓ Branch 0 taken 8048 times.
✗ Branch 1 not taken.
|
8048 | if (s->options.lpc_type == FF_LPC_TYPE_NONE || |
| 1003 |
3/4✓ Branch 0 taken 7174 times.
✓ Branch 1 taken 874 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7174 times.
|
8048 | s->options.lpc_type == FF_LPC_TYPE_FIXED || n <= max_order) { |
| 1004 | uint64_t bits[MAX_FIXED_ORDER+1]; | ||
| 1005 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 874 times.
|
874 | if (max_order > MAX_FIXED_ORDER) |
| 1006 | ✗ | max_order = MAX_FIXED_ORDER; | |
| 1007 | 874 | opt_order = 0; | |
| 1008 | 874 | bits[0] = UINT32_MAX; | |
| 1009 |
2/2✓ Branch 0 taken 4188 times.
✓ Branch 1 taken 874 times.
|
5062 | for (i = min_order; i <= max_order; i++) { |
| 1010 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4188 times.
|
4188 | if (sub->obits == 33) { |
| 1011 | ✗ | if (encode_residual_fixed_with_residual_limit_33bps(res, smp_33bps, n, i)) | |
| 1012 | ✗ | continue; | |
| 1013 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4188 times.
|
4188 | } else if (sub->obits + i >= 32) { |
| 1014 | ✗ | if (encode_residual_fixed_with_residual_limit(res, smp, n, i)) | |
| 1015 | ✗ | continue; | |
| 1016 | } else | ||
| 1017 | 4188 | encode_residual_fixed(res, smp, n, i); | |
| 1018 | 4188 | bits[i] = find_subframe_rice_params(s, sub, i); | |
| 1019 |
2/2✓ Branch 0 taken 2592 times.
✓ Branch 1 taken 1596 times.
|
4188 | if (bits[i] < bits[opt_order]) |
| 1020 | 2592 | opt_order = i; | |
| 1021 | } | ||
| 1022 |
3/4✓ Branch 0 taken 98 times.
✓ Branch 1 taken 776 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 98 times.
|
874 | if (opt_order == 0 && bits[0] == UINT32_MAX) { |
| 1023 | /* No predictor found with residuals within <INT32_MIN,INT32_MAX], | ||
| 1024 | * so encode a verbatim subframe instead */ | ||
| 1025 | ✗ | DEFAULT_TO_VERBATIM(); | |
| 1026 | } | ||
| 1027 | 874 | sub->order = opt_order; | |
| 1028 | 874 | sub->type_code = sub->type | sub->order; | |
| 1029 |
2/2✓ Branch 0 taken 364 times.
✓ Branch 1 taken 510 times.
|
874 | if (sub->order != max_order) { |
| 1030 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 364 times.
|
364 | if (sub->obits == 33) |
| 1031 | ✗ | encode_residual_fixed_with_residual_limit_33bps(res, smp_33bps, n, sub->order); | |
| 1032 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 364 times.
|
364 | else if (sub->obits + i >= 32) |
| 1033 | ✗ | encode_residual_fixed_with_residual_limit(res, smp, n, sub->order); | |
| 1034 | else | ||
| 1035 | 364 | encode_residual_fixed(res, smp, n, sub->order); | |
| 1036 | 364 | find_subframe_rice_params(s, sub, sub->order); | |
| 1037 | } | ||
| 1038 | 874 | return subframe_count_exact(s, sub, sub->order); | |
| 1039 | } | ||
| 1040 | |||
| 1041 | /* LPC */ | ||
| 1042 | 7174 | sub->type = FLAC_SUBFRAME_LPC; | |
| 1043 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7171 times.
|
7174 | if (sub->obits == 33) |
| 1044 | /* As ff_lpc_calc_coefs is shared with other codecs and the LSB | ||
| 1045 | * probably isn't predictable anyway, throw away LSB for analysis | ||
| 1046 | * so it fits 32 bit int and existing function can be used | ||
| 1047 | * unmodified */ | ||
| 1048 |
2/2✓ Branch 0 taken 13824 times.
✓ Branch 1 taken 3 times.
|
13827 | for (i = 0; i < n; i++) |
| 1049 | 13824 | smp[i] = smp_33bps[i] >> 1; | |
| 1050 | |||
| 1051 | 7174 | opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, smp, n, min_order, max_order, | |
| 1052 | s->options.lpc_coeff_precision, coefs, shift, s->options.lpc_type, | ||
| 1053 | s->options.lpc_passes, omethod, | ||
| 1054 | MIN_LPC_SHIFT, MAX_LPC_SHIFT, 0); | ||
| 1055 | |||
| 1056 |
3/4✓ Branch 0 taken 7174 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6938 times.
✓ Branch 3 taken 236 times.
|
7174 | if (omethod == ORDER_METHOD_2LEVEL || |
| 1057 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6938 times.
|
6938 | omethod == ORDER_METHOD_4LEVEL || |
| 1058 | 236 | omethod == ORDER_METHOD_8LEVEL) { | |
| 1059 | 236 | int levels = 1 << omethod; | |
| 1060 | uint64_t bits[1 << ORDER_METHOD_8LEVEL]; | ||
| 1061 | 236 | int order = -1; | |
| 1062 | 236 | int opt_index = levels-1; | |
| 1063 | 236 | opt_order = max_order-1; | |
| 1064 | 236 | bits[opt_index] = UINT32_MAX; | |
| 1065 |
2/2✓ Branch 0 taken 944 times.
✓ Branch 1 taken 236 times.
|
1180 | for (i = levels-1; i >= 0; i--) { |
| 1066 | 944 | int last_order = order; | |
| 1067 | 944 | order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1; | |
| 1068 | 944 | order = av_clip(order, min_order - 1, max_order - 1); | |
| 1069 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 944 times.
|
944 | if (order == last_order) |
| 1070 | ✗ | continue; | |
| 1071 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 944 times.
|
944 | if(lpc_encode_choose_datapath(s, sub->obits, res, smp, smp_33bps, n, order+1, coefs[order], shift[order])) |
| 1072 | ✗ | continue; | |
| 1073 | 944 | bits[i] = find_subframe_rice_params(s, sub, order+1); | |
| 1074 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 886 times.
|
944 | if (bits[i] < bits[opt_index]) { |
| 1075 | 58 | opt_index = i; | |
| 1076 | 58 | opt_order = order; | |
| 1077 | } | ||
| 1078 | } | ||
| 1079 | 236 | opt_order++; | |
| 1080 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6938 times.
|
6938 | } else if (omethod == ORDER_METHOD_SEARCH) { |
| 1081 | // brute-force optimal order search | ||
| 1082 | uint64_t bits[MAX_LPC_ORDER]; | ||
| 1083 | ✗ | opt_order = 0; | |
| 1084 | ✗ | bits[0] = UINT32_MAX; | |
| 1085 | ✗ | for (i = min_order-1; i < max_order; i++) { | |
| 1086 | ✗ | if(lpc_encode_choose_datapath(s, sub->obits, res, smp, smp_33bps, n, i+1, coefs[i], shift[i])) | |
| 1087 | ✗ | continue; | |
| 1088 | ✗ | bits[i] = find_subframe_rice_params(s, sub, i+1); | |
| 1089 | ✗ | if (bits[i] < bits[opt_order]) | |
| 1090 | ✗ | opt_order = i; | |
| 1091 | } | ||
| 1092 | ✗ | opt_order++; | |
| 1093 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6938 times.
|
6938 | } else if (omethod == ORDER_METHOD_LOG) { |
| 1094 | uint64_t bits[MAX_LPC_ORDER]; | ||
| 1095 | int step; | ||
| 1096 | |||
| 1097 | ✗ | opt_order = min_order - 1 + (max_order-min_order)/3; | |
| 1098 | ✗ | memset(bits, -1, sizeof(bits)); | |
| 1099 | |||
| 1100 | ✗ | for (step = 16; step; step >>= 1) { | |
| 1101 | ✗ | int last = opt_order; | |
| 1102 | ✗ | for (i = last-step; i <= last+step; i += step) { | |
| 1103 | ✗ | if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX) | |
| 1104 | ✗ | continue; | |
| 1105 | ✗ | if(lpc_encode_choose_datapath(s, sub->obits, res, smp, smp_33bps, n, i+1, coefs[i], shift[i])) | |
| 1106 | ✗ | continue; | |
| 1107 | ✗ | bits[i] = find_subframe_rice_params(s, sub, i+1); | |
| 1108 | ✗ | if (bits[i] < bits[opt_order]) | |
| 1109 | ✗ | opt_order = i; | |
| 1110 | } | ||
| 1111 | } | ||
| 1112 | ✗ | opt_order++; | |
| 1113 | } | ||
| 1114 | |||
| 1115 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 7168 times.
|
7174 | if (s->options.multi_dim_quant) { |
| 1116 | 6 | int allsteps = 1; | |
| 1117 | int i, step, improved; | ||
| 1118 | 6 | int64_t best_score = INT64_MAX; | |
| 1119 | int32_t qmax; | ||
| 1120 | |||
| 1121 | 6 | qmax = (1 << (s->options.lpc_coeff_precision - 1)) - 1; | |
| 1122 | |||
| 1123 |
2/2✓ Branch 0 taken 47 times.
✓ Branch 1 taken 6 times.
|
53 | for (i=0; i<opt_order; i++) |
| 1124 | 47 | allsteps *= 3; | |
| 1125 | |||
| 1126 | do { | ||
| 1127 | 14 | improved = 0; | |
| 1128 |
2/2✓ Branch 0 taken 78732 times.
✓ Branch 1 taken 14 times.
|
78746 | for (step = 0; step < allsteps; step++) { |
| 1129 | 78732 | int tmp = step; | |
| 1130 | int32_t lpc_try[MAX_LPC_ORDER]; | ||
| 1131 | 78732 | int64_t score = 0; | |
| 1132 | 78732 | int diffsum = 0; | |
| 1133 | |||
| 1134 |
2/2✓ Branch 0 taken 623295 times.
✓ Branch 1 taken 78732 times.
|
702027 | for (i=0; i<opt_order; i++) { |
| 1135 | 623295 | int diff = ((tmp + 1) % 3) - 1; | |
| 1136 | 623295 | lpc_try[i] = av_clip(coefs[opt_order - 1][i] + diff, -qmax, qmax); | |
| 1137 | 623295 | tmp /= 3; | |
| 1138 | 623295 | diffsum += !!diff; | |
| 1139 | } | ||
| 1140 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 78732 times.
|
78732 | if (diffsum >8) |
| 1141 | ✗ | continue; | |
| 1142 | |||
| 1143 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 78732 times.
|
78732 | if(lpc_encode_choose_datapath(s, sub->obits, res, smp, smp_33bps, n, opt_order, lpc_try, shift[opt_order-1])) |
| 1144 | ✗ | continue; | |
| 1145 | 78732 | score = find_subframe_rice_params(s, sub, opt_order); | |
| 1146 |
2/2✓ Branch 0 taken 467 times.
✓ Branch 1 taken 78265 times.
|
78732 | if (score < best_score) { |
| 1147 | 467 | best_score = score; | |
| 1148 | 467 | memcpy(coefs[opt_order-1], lpc_try, sizeof(*coefs)); | |
| 1149 | 467 | improved=1; | |
| 1150 | } | ||
| 1151 | } | ||
| 1152 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6 times.
|
14 | } while(improved); |
| 1153 | } | ||
| 1154 | |||
| 1155 | 7174 | sub->order = opt_order; | |
| 1156 | 7174 | sub->type_code = sub->type | (sub->order-1); | |
| 1157 | 7174 | sub->shift = shift[sub->order-1]; | |
| 1158 |
2/2✓ Branch 0 taken 20346 times.
✓ Branch 1 taken 7174 times.
|
27520 | for (i = 0; i < sub->order; i++) |
| 1159 | 20346 | sub->coefs[i] = coefs[sub->order-1][i]; | |
| 1160 | |||
| 1161 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 7173 times.
|
7174 | if(lpc_encode_choose_datapath(s, sub->obits, res, smp, smp_33bps, n, sub->order, sub->coefs, sub->shift)) { |
| 1162 | /* No predictor found with residuals within <INT32_MIN,INT32_MAX], | ||
| 1163 | * so encode a verbatim subframe instead */ | ||
| 1164 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | DEFAULT_TO_VERBATIM(); |
| 1165 | } | ||
| 1166 | |||
| 1167 | 7173 | find_subframe_rice_params(s, sub, sub->order); | |
| 1168 | |||
| 1169 | 7173 | return subframe_count_exact(s, sub, sub->order); | |
| 1170 | } | ||
| 1171 | |||
| 1172 | |||
| 1173 | 5054 | static int count_frame_header(FlacEncodeContext *s) | |
| 1174 | { | ||
| 1175 | av_unused uint8_t tmp; | ||
| 1176 | int count; | ||
| 1177 | |||
| 1178 | /* | ||
| 1179 | <14> Sync code | ||
| 1180 | <1> Reserved | ||
| 1181 | <1> Blocking strategy | ||
| 1182 | <4> Block size in inter-channel samples | ||
| 1183 | <4> Sample rate | ||
| 1184 | <4> Channel assignment | ||
| 1185 | <3> Sample size in bits | ||
| 1186 | <1> Reserved | ||
| 1187 | */ | ||
| 1188 | 5054 | count = 32; | |
| 1189 | |||
| 1190 | /* coded frame number */ | ||
| 1191 |
4/4✓ Branch 0 taken 3698 times.
✓ Branch 1 taken 1356 times.
✓ Branch 2 taken 1356 times.
✓ Branch 3 taken 1356 times.
|
6410 | PUT_UTF8(s->frame_count, tmp, count += 8;) |
| 1192 | |||
| 1193 | /* explicit block size */ | ||
| 1194 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5054 times.
|
5054 | if (s->frame.bs_code[0] == 6) |
| 1195 | ✗ | count += 8; | |
| 1196 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 4983 times.
|
5054 | else if (s->frame.bs_code[0] == 7) |
| 1197 | 71 | count += 16; | |
| 1198 | |||
| 1199 | /* explicit sample rate */ | ||
| 1200 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5054 times.
|
5054 | count += ((s->sr_code[0] == 12) + (s->sr_code[0] > 12) * 2) * 8; |
| 1201 | |||
| 1202 | /* frame header CRC-8 */ | ||
| 1203 | 5054 | count += 8; | |
| 1204 | |||
| 1205 | 5054 | return count; | |
| 1206 | } | ||
| 1207 | |||
| 1208 | |||
| 1209 | 5054 | static int encode_frame(FlacEncodeContext *s) | |
| 1210 | { | ||
| 1211 | int ch; | ||
| 1212 | uint64_t count; | ||
| 1213 | |||
| 1214 | 5054 | count = count_frame_header(s); | |
| 1215 | |||
| 1216 |
2/2✓ Branch 0 taken 13954 times.
✓ Branch 1 taken 5054 times.
|
19008 | for (ch = 0; ch < s->channels; ch++) |
| 1217 | 13954 | count += encode_residual_ch(s, ch); | |
| 1218 | |||
| 1219 | 5054 | count += (8 - (count & 7)) & 7; // byte alignment | |
| 1220 | 5054 | count += 16; // CRC-16 | |
| 1221 | |||
| 1222 | 5054 | count >>= 3; | |
| 1223 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5054 times.
|
5054 | if (count > INT_MAX) |
| 1224 | ✗ | return AVERROR_BUG; | |
| 1225 | 5054 | return count; | |
| 1226 | } | ||
| 1227 | |||
| 1228 | |||
| 1229 | 5054 | static void remove_wasted_bits(FlacEncodeContext *s) | |
| 1230 | { | ||
| 1231 | int ch, i, wasted_bits; | ||
| 1232 | |||
| 1233 |
2/2✓ Branch 0 taken 8660 times.
✓ Branch 1 taken 1606 times.
|
10266 | for (ch = 0; ch < s->channels; ch++) { |
| 1234 | 8660 | FlacSubframe *sub = &s->frame.subframes[ch]; | |
| 1235 | |||
| 1236 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8652 times.
|
8660 | if (sub->obits > 32) { |
| 1237 | 8 | int64_t v = 0; | |
| 1238 |
2/2✓ Branch 0 taken 29481 times.
✓ Branch 1 taken 6 times.
|
29487 | for (i = 0; i < s->frame.blocksize; i++) { |
| 1239 | 29481 | v |= s->frame.samples_33bps[i]; | |
| 1240 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 29479 times.
|
29481 | if (v & 1) |
| 1241 | 2 | break; | |
| 1242 | } | ||
| 1243 | |||
| 1244 |
3/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 6 times.
|
8 | if (!v || (v & 1)) |
| 1245 | 2 | return; | |
| 1246 | |||
| 1247 | 6 | v = ff_ctzll(v); | |
| 1248 | |||
| 1249 | /* If any wasted bits are found, samples are moved | ||
| 1250 | * from frame.samples_33bps to frame.subframes[ch] */ | ||
| 1251 |
2/2✓ Branch 0 taken 25701 times.
✓ Branch 1 taken 6 times.
|
25707 | for (i = 0; i < s->frame.blocksize; i++) |
| 1252 | 25701 | sub->samples[i] = s->frame.samples_33bps[i] >> v; | |
| 1253 | 6 | wasted_bits = v; | |
| 1254 | } else { | ||
| 1255 | 8652 | int32_t v = 0; | |
| 1256 |
2/2✓ Branch 0 taken 44650953 times.
✓ Branch 1 taken 6832 times.
|
44657785 | for (i = 0; i < s->frame.blocksize; i++) { |
| 1257 | 44650953 | v |= sub->samples[i]; | |
| 1258 |
2/2✓ Branch 0 taken 1820 times.
✓ Branch 1 taken 44649133 times.
|
44650953 | if (v & 1) |
| 1259 | 1820 | break; | |
| 1260 | } | ||
| 1261 | |||
| 1262 |
4/4✓ Branch 0 taken 7026 times.
✓ Branch 1 taken 1626 times.
✓ Branch 2 taken 1820 times.
✓ Branch 3 taken 5206 times.
|
8652 | if (!v || (v & 1)) |
| 1263 | 3446 | return; | |
| 1264 | |||
| 1265 | 5206 | v = ff_ctz(v); | |
| 1266 | |||
| 1267 |
2/2✓ Branch 0 taken 34054501 times.
✓ Branch 1 taken 5206 times.
|
34059707 | for (i = 0; i < s->frame.blocksize; i++) |
| 1268 | 34054501 | sub->samples[i] >>= v; | |
| 1269 | 5206 | wasted_bits = v; | |
| 1270 | } | ||
| 1271 | |||
| 1272 | 5212 | sub->wasted = wasted_bits; | |
| 1273 | 5212 | sub->obits -= wasted_bits; | |
| 1274 | |||
| 1275 | /* for 24-bit, check if removing wasted bits makes the range better | ||
| 1276 | * suited for using RICE instead of RICE2 for entropy coding */ | ||
| 1277 |
2/2✓ Branch 0 taken 5200 times.
✓ Branch 1 taken 12 times.
|
5212 | if (sub->obits <= 17) |
| 1278 | 5200 | sub->rc.coding_mode = CODING_MODE_RICE; | |
| 1279 | } | ||
| 1280 | } | ||
| 1281 | |||
| 1282 | |||
| 1283 | 2816 | static int estimate_stereo_mode(const int32_t *left_ch, const int32_t *right_ch, int n, | |
| 1284 | int max_rice_param, int bps) | ||
| 1285 | { | ||
| 1286 | int best; | ||
| 1287 | uint64_t sum[4]; | ||
| 1288 | uint64_t score[4]; | ||
| 1289 | int k; | ||
| 1290 | |||
| 1291 | /* calculate sum of 2nd order residual for each channel */ | ||
| 1292 | 2816 | sum[0] = sum[1] = sum[2] = sum[3] = 0; | |
| 1293 |
2/2✓ Branch 0 taken 2806 times.
✓ Branch 1 taken 10 times.
|
2816 | if(bps < 30) { |
| 1294 | int32_t lt, rt; | ||
| 1295 |
2/2✓ Branch 0 taken 15736987 times.
✓ Branch 1 taken 2806 times.
|
15739793 | for (int i = 2; i < n; i++) { |
| 1296 | 15736987 | lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2]; | |
| 1297 | 15736987 | rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2]; | |
| 1298 |
2/2✓ Branch 0 taken 12583917 times.
✓ Branch 1 taken 3153070 times.
|
15736987 | sum[2] += FFABS((lt + rt) >> 1); |
| 1299 |
2/2✓ Branch 0 taken 13445538 times.
✓ Branch 1 taken 2291449 times.
|
15736987 | sum[3] += FFABS(lt - rt); |
| 1300 |
2/2✓ Branch 0 taken 12590485 times.
✓ Branch 1 taken 3146502 times.
|
15736987 | sum[0] += FFABS(lt); |
| 1301 |
2/2✓ Branch 0 taken 12590383 times.
✓ Branch 1 taken 3146604 times.
|
15736987 | sum[1] += FFABS(rt); |
| 1302 | } | ||
| 1303 | } else { | ||
| 1304 | int64_t lt, rt; | ||
| 1305 |
2/2✓ Branch 0 taken 44113 times.
✓ Branch 1 taken 10 times.
|
44123 | for (int i = 2; i < n; i++) { |
| 1306 | 44113 | lt = (int64_t)left_ch[i] - 2*(int64_t)left_ch[i-1] + left_ch[i-2]; | |
| 1307 | 44113 | rt = (int64_t)right_ch[i] - 2*(int64_t)right_ch[i-1] + right_ch[i-2]; | |
| 1308 | 44113 | sum[2] += FFABS((lt + rt) >> 1); | |
| 1309 | 44113 | sum[3] += FFABS(lt - rt); | |
| 1310 | 44113 | sum[0] += FFABS(lt); | |
| 1311 | 44113 | sum[1] += FFABS(rt); | |
| 1312 | } | ||
| 1313 | } | ||
| 1314 | /* estimate bit counts */ | ||
| 1315 |
2/2✓ Branch 0 taken 11264 times.
✓ Branch 1 taken 2816 times.
|
14080 | for (int i = 0; i < 4; i++) { |
| 1316 | 11264 | k = find_optimal_param(2 * sum[i], n, max_rice_param); | |
| 1317 | 11264 | sum[i] = rice_encode_count( 2 * sum[i], n, k); | |
| 1318 | } | ||
| 1319 | |||
| 1320 | /* calculate score for each mode */ | ||
| 1321 | 2816 | score[0] = sum[0] + sum[1]; | |
| 1322 | 2816 | score[1] = sum[0] + sum[3]; | |
| 1323 | 2816 | score[2] = sum[1] + sum[3]; | |
| 1324 | 2816 | score[3] = sum[2] + sum[3]; | |
| 1325 | |||
| 1326 | /* return mode with lowest score */ | ||
| 1327 | 2816 | best = 0; | |
| 1328 |
2/2✓ Branch 0 taken 8448 times.
✓ Branch 1 taken 2816 times.
|
11264 | for (int i = 1; i < 4; i++) |
| 1329 |
2/2✓ Branch 0 taken 2096 times.
✓ Branch 1 taken 6352 times.
|
8448 | if (score[i] < score[best]) |
| 1330 | 2096 | best = i; | |
| 1331 | |||
| 1332 | 2816 | return best; | |
| 1333 | } | ||
| 1334 | |||
| 1335 | |||
| 1336 | /** | ||
| 1337 | * Perform stereo channel decorrelation. | ||
| 1338 | */ | ||
| 1339 | 5054 | static void channel_decorrelation(FlacEncodeContext *s) | |
| 1340 | { | ||
| 1341 | FlacFrame *frame; | ||
| 1342 | int32_t *left, *right; | ||
| 1343 | int64_t *side_33bps; | ||
| 1344 | int n; | ||
| 1345 | |||
| 1346 | 5054 | frame = &s->frame; | |
| 1347 | 5054 | n = frame->blocksize; | |
| 1348 | 5054 | left = frame->subframes[0].samples; | |
| 1349 | 5054 | right = frame->subframes[1].samples; | |
| 1350 | 5054 | side_33bps = frame->samples_33bps; | |
| 1351 | |||
| 1352 |
2/2✓ Branch 0 taken 1874 times.
✓ Branch 1 taken 3180 times.
|
5054 | if (s->channels != 2) { |
| 1353 | 1874 | frame->ch_mode = FLAC_CHMODE_INDEPENDENT; | |
| 1354 | 1874 | return; | |
| 1355 | } | ||
| 1356 | |||
| 1357 |
2/2✓ Branch 0 taken 2816 times.
✓ Branch 1 taken 364 times.
|
3180 | if (s->options.ch_mode < 0) { |
| 1358 | 2816 | int max_rice_param = (1 << frame->subframes[0].rc.coding_mode) - 2; | |
| 1359 | 2816 | frame->ch_mode = estimate_stereo_mode(left, right, n, max_rice_param, s->avctx->bits_per_raw_sample); | |
| 1360 | } else | ||
| 1361 | 364 | frame->ch_mode = s->options.ch_mode; | |
| 1362 | |||
| 1363 | /* perform decorrelation and adjust bits-per-sample */ | ||
| 1364 |
2/2✓ Branch 0 taken 1052 times.
✓ Branch 1 taken 2128 times.
|
3180 | if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT) |
| 1365 | 1052 | return; | |
| 1366 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2119 times.
|
2128 | if(s->avctx->bits_per_raw_sample == 32) { |
| 1367 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
|
9 | if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) { |
| 1368 | int64_t tmp; | ||
| 1369 |
2/2✓ Branch 0 taken 4608 times.
✓ Branch 1 taken 1 times.
|
4609 | for (int i = 0; i < n; i++) { |
| 1370 | 4608 | tmp = left[i]; | |
| 1371 | 4608 | left[i] = (tmp + right[i]) >> 1; | |
| 1372 | 4608 | side_33bps[i] = tmp - right[i]; | |
| 1373 | } | ||
| 1374 | 1 | frame->subframes[1].obits++; | |
| 1375 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) { |
| 1376 | ✗ | for (int i = 0; i < n; i++) | |
| 1377 | ✗ | side_33bps[i] = (int64_t)left[i] - right[i]; | |
| 1378 | ✗ | frame->subframes[1].obits++; | |
| 1379 | } else { | ||
| 1380 |
2/2✓ Branch 0 taken 34917 times.
✓ Branch 1 taken 8 times.
|
34925 | for (int i = 0; i < n; i++) |
| 1381 | 34917 | side_33bps[i] = (int64_t)left[i] - right[i]; | |
| 1382 | 8 | frame->subframes[0].obits++; | |
| 1383 | } | ||
| 1384 | } else { | ||
| 1385 |
2/2✓ Branch 0 taken 254 times.
✓ Branch 1 taken 1865 times.
|
2119 | if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) { |
| 1386 | int32_t tmp; | ||
| 1387 |
2/2✓ Branch 0 taken 1078297 times.
✓ Branch 1 taken 254 times.
|
1078551 | for (int i = 0; i < n; i++) { |
| 1388 | 1078297 | tmp = left[i]; | |
| 1389 | 1078297 | left[i] = (tmp + right[i]) >> 1; | |
| 1390 | 1078297 | right[i] = tmp - right[i]; | |
| 1391 | } | ||
| 1392 | 254 | frame->subframes[1].obits++; | |
| 1393 |
2/2✓ Branch 0 taken 1634 times.
✓ Branch 1 taken 231 times.
|
1865 | } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) { |
| 1394 |
2/2✓ Branch 0 taken 7968122 times.
✓ Branch 1 taken 1634 times.
|
7969756 | for (int i = 0; i < n; i++) |
| 1395 | 7968122 | right[i] = left[i] - right[i]; | |
| 1396 | 1634 | frame->subframes[1].obits++; | |
| 1397 | } else { | ||
| 1398 |
2/2✓ Branch 0 taken 787590 times.
✓ Branch 1 taken 231 times.
|
787821 | for (int i = 0; i < n; i++) |
| 1399 | 787590 | left[i] -= right[i]; | |
| 1400 | 231 | frame->subframes[0].obits++; | |
| 1401 | } | ||
| 1402 | } | ||
| 1403 | } | ||
| 1404 | |||
| 1405 | |||
| 1406 | 5054 | static void write_utf8(PutBitContext *pb, uint32_t val) | |
| 1407 | { | ||
| 1408 | uint8_t tmp; | ||
| 1409 |
4/4✓ Branch 0 taken 3698 times.
✓ Branch 1 taken 1356 times.
✓ Branch 5 taken 1356 times.
✓ Branch 6 taken 1356 times.
|
6410 | PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);) |
| 1410 | 5054 | } | |
| 1411 | |||
| 1412 | |||
| 1413 | 5054 | static void write_frame_header(FlacEncodeContext *s) | |
| 1414 | { | ||
| 1415 | FlacFrame *frame; | ||
| 1416 | int crc; | ||
| 1417 | |||
| 1418 | 5054 | frame = &s->frame; | |
| 1419 | |||
| 1420 | 5054 | put_bits(&s->pb, 16, 0xFFF8); | |
| 1421 | 5054 | put_bits(&s->pb, 4, frame->bs_code[0]); | |
| 1422 | 5054 | put_bits(&s->pb, 4, s->sr_code[0]); | |
| 1423 | |||
| 1424 |
2/2✓ Branch 0 taken 2926 times.
✓ Branch 1 taken 2128 times.
|
5054 | if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT) |
| 1425 | 2926 | put_bits(&s->pb, 4, s->channels-1); | |
| 1426 | else | ||
| 1427 | 2128 | put_bits(&s->pb, 4, frame->ch_mode + FLAC_MAX_CHANNELS - 1); | |
| 1428 | |||
| 1429 | 5054 | put_bits(&s->pb, 3, s->bps_code); | |
| 1430 | 5054 | put_bits(&s->pb, 1, 0); | |
| 1431 | 5054 | write_utf8(&s->pb, s->frame_count); | |
| 1432 | |||
| 1433 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5054 times.
|
5054 | if (frame->bs_code[0] == 6) |
| 1434 | ✗ | put_bits(&s->pb, 8, frame->bs_code[1]); | |
| 1435 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 4983 times.
|
5054 | else if (frame->bs_code[0] == 7) |
| 1436 | 71 | put_bits(&s->pb, 16, frame->bs_code[1]); | |
| 1437 | |||
| 1438 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5054 times.
|
5054 | if (s->sr_code[0] == 12) |
| 1439 | ✗ | put_bits(&s->pb, 8, s->sr_code[1]); | |
| 1440 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5054 times.
|
5054 | else if (s->sr_code[0] > 12) |
| 1441 | ✗ | put_bits(&s->pb, 16, s->sr_code[1]); | |
| 1442 | |||
| 1443 | 5054 | flush_put_bits(&s->pb); | |
| 1444 | 5054 | crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf, | |
| 1445 | 5054 | put_bytes_output(&s->pb)); | |
| 1446 | 5054 | put_bits(&s->pb, 8, crc); | |
| 1447 | 5054 | } | |
| 1448 | |||
| 1449 | |||
| 1450 | 47351638 | static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k) | |
| 1451 | { | ||
| 1452 | unsigned v, e; | ||
| 1453 | |||
| 1454 | 47351638 | v = ((unsigned)(i) << 1) ^ (i >> 31); | |
| 1455 | |||
| 1456 | 47351638 | e = (v >> k) + 1; | |
| 1457 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 47351638 times.
|
47351679 | while (e > 31) { |
| 1458 | 41 | put_bits(pb, 31, 0); | |
| 1459 | 41 | e -= 31; | |
| 1460 | } | ||
| 1461 | 47351638 | put_bits(pb, e, 1); | |
| 1462 |
2/2✓ Branch 0 taken 44724675 times.
✓ Branch 1 taken 2626963 times.
|
47351638 | if (k) { |
| 1463 | 44724675 | unsigned mask = UINT32_MAX >> (32-k); | |
| 1464 | 44724675 | put_bits(pb, k, v & mask); | |
| 1465 | } | ||
| 1466 | 47351638 | } | |
| 1467 | |||
| 1468 | |||
| 1469 | 5054 | static void write_subframes(FlacEncodeContext *s) | |
| 1470 | { | ||
| 1471 | int ch; | ||
| 1472 | |||
| 1473 |
2/2✓ Branch 0 taken 13954 times.
✓ Branch 1 taken 5054 times.
|
19008 | for (ch = 0; ch < s->channels; ch++) { |
| 1474 | 13954 | FlacSubframe *sub = &s->frame.subframes[ch]; | |
| 1475 | int p, porder, psize; | ||
| 1476 | int32_t *part_end; | ||
| 1477 | 13954 | int32_t *res = sub->residual; | |
| 1478 | 13954 | int32_t *frame_end = &sub->residual[s->frame.blocksize]; | |
| 1479 | |||
| 1480 | /* subframe header */ | ||
| 1481 | 13954 | put_bits(&s->pb, 1, 0); | |
| 1482 | 13954 | put_bits(&s->pb, 6, sub->type_code); | |
| 1483 | 13954 | put_bits(&s->pb, 1, !!sub->wasted); | |
| 1484 |
2/2✓ Branch 0 taken 5212 times.
✓ Branch 1 taken 8742 times.
|
13954 | if (sub->wasted) |
| 1485 | 5212 | put_bits(&s->pb, sub->wasted, 1); | |
| 1486 | |||
| 1487 | /* subframe */ | ||
| 1488 |
2/2✓ Branch 0 taken 5906 times.
✓ Branch 1 taken 8048 times.
|
13954 | if (sub->type == FLAC_SUBFRAME_CONSTANT) { |
| 1489 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5906 times.
|
5906 | if(sub->obits == 33) |
| 1490 | ✗ | put_sbits63(&s->pb, 33, s->frame.samples_33bps[0]); | |
| 1491 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5904 times.
|
5906 | else if(sub->obits == 32) |
| 1492 | 2 | put_bits32(&s->pb, res[0]); | |
| 1493 | else | ||
| 1494 | 5904 | put_sbits(&s->pb, sub->obits, res[0]); | |
| 1495 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8047 times.
|
8048 | } else if (sub->type == FLAC_SUBFRAME_VERBATIM) { |
| 1496 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (sub->obits == 33) { |
| 1497 | 1 | int64_t *res64 = s->frame.samples_33bps; | |
| 1498 | 1 | int64_t *frame_end64 = &s->frame.samples_33bps[s->frame.blocksize]; | |
| 1499 |
2/2✓ Branch 0 taken 4608 times.
✓ Branch 1 taken 1 times.
|
4609 | while (res64 < frame_end64) |
| 1500 | 4608 | put_sbits63(&s->pb, 33, (*res64++)); | |
| 1501 | ✗ | } else if (sub->obits == 32) { | |
| 1502 | ✗ | while (res < frame_end) | |
| 1503 | ✗ | put_bits32(&s->pb, *res++); | |
| 1504 | } else { | ||
| 1505 | ✗ | while (res < frame_end) | |
| 1506 | ✗ | put_sbits(&s->pb, sub->obits, *res++); | |
| 1507 | } | ||
| 1508 | } else { | ||
| 1509 | /* warm-up samples */ | ||
| 1510 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8045 times.
|
8047 | if (sub->obits == 33) { |
| 1511 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
|
18 | for (int i = 0; i < sub->order; i++) |
| 1512 | 16 | put_sbits63(&s->pb, 33, s->frame.samples_33bps[i]); | |
| 1513 | 2 | res += sub->order; | |
| 1514 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 8040 times.
|
8045 | } else if (sub->obits == 32) { |
| 1515 |
2/2✓ Branch 0 taken 33 times.
✓ Branch 1 taken 5 times.
|
38 | for (int i = 0; i < sub->order; i++) |
| 1516 | 33 | put_bits32(&s->pb, *res++); | |
| 1517 | } else { | ||
| 1518 |
2/2✓ Branch 0 taken 22881 times.
✓ Branch 1 taken 8040 times.
|
30921 | for (int i = 0; i < sub->order; i++) |
| 1519 | 22881 | put_sbits(&s->pb, sub->obits, *res++); | |
| 1520 | } | ||
| 1521 | |||
| 1522 | /* LPC coefficients */ | ||
| 1523 |
2/2✓ Branch 0 taken 7173 times.
✓ Branch 1 taken 874 times.
|
8047 | if (sub->type == FLAC_SUBFRAME_LPC) { |
| 1524 | 7173 | int cbits = s->options.lpc_coeff_precision; | |
| 1525 | 7173 | put_bits( &s->pb, 4, cbits-1); | |
| 1526 | 7173 | put_sbits(&s->pb, 5, sub->shift); | |
| 1527 |
2/2✓ Branch 0 taken 20338 times.
✓ Branch 1 taken 7173 times.
|
27511 | for (int i = 0; i < sub->order; i++) |
| 1528 | 20338 | put_sbits(&s->pb, cbits, sub->coefs[i]); | |
| 1529 | } | ||
| 1530 | |||
| 1531 | /* rice-encoded block */ | ||
| 1532 | 8047 | put_bits(&s->pb, 2, sub->rc.coding_mode - 4); | |
| 1533 | |||
| 1534 | /* partition order */ | ||
| 1535 | 8047 | porder = sub->rc.porder; | |
| 1536 | 8047 | psize = s->frame.blocksize >> porder; | |
| 1537 | 8047 | put_bits(&s->pb, 4, porder); | |
| 1538 | |||
| 1539 | /* residual */ | ||
| 1540 | 8047 | part_end = &sub->residual[psize]; | |
| 1541 |
2/2✓ Branch 0 taken 99517 times.
✓ Branch 1 taken 8047 times.
|
107564 | for (p = 0; p < 1 << porder; p++) { |
| 1542 | 99517 | int k = sub->rc.params[p]; | |
| 1543 | 99517 | put_bits(&s->pb, sub->rc.coding_mode, k); | |
| 1544 |
2/2✓ Branch 0 taken 47351638 times.
✓ Branch 1 taken 99517 times.
|
47451155 | while (res < part_end) |
| 1545 | 47351638 | set_sr_golomb_flac(&s->pb, *res++, k); | |
| 1546 | 99517 | part_end = FFMIN(frame_end, part_end + psize); | |
| 1547 | } | ||
| 1548 | } | ||
| 1549 | } | ||
| 1550 | 5054 | } | |
| 1551 | |||
| 1552 | |||
| 1553 | 5054 | static void write_frame_footer(FlacEncodeContext *s) | |
| 1554 | { | ||
| 1555 | int crc; | ||
| 1556 | 5054 | flush_put_bits(&s->pb); | |
| 1557 | 5054 | crc = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, s->pb.buf, | |
| 1558 | 5054 | put_bytes_output(&s->pb))); | |
| 1559 | 5054 | put_bits(&s->pb, 16, crc); | |
| 1560 | 5054 | flush_put_bits(&s->pb); | |
| 1561 | 5054 | } | |
| 1562 | |||
| 1563 | |||
| 1564 | 5054 | static int write_frame(FlacEncodeContext *s, AVPacket *avpkt) | |
| 1565 | { | ||
| 1566 | 5054 | init_put_bits(&s->pb, avpkt->data, avpkt->size); | |
| 1567 | 5054 | write_frame_header(s); | |
| 1568 | 5054 | write_subframes(s); | |
| 1569 | 5054 | write_frame_footer(s); | |
| 1570 | 5054 | return put_bytes_output(&s->pb); | |
| 1571 | } | ||
| 1572 | |||
| 1573 | |||
| 1574 | 5054 | static int update_md5_sum(FlacEncodeContext *s, const void *samples) | |
| 1575 | { | ||
| 1576 | const uint8_t *buf; | ||
| 1577 | 5054 | int buf_size = s->frame.blocksize * s->channels * | |
| 1578 | 5054 | ((s->avctx->bits_per_raw_sample + 7) / 8); | |
| 1579 | |||
| 1580 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 4926 times.
|
5054 | if (s->avctx->bits_per_raw_sample > 16 || HAVE_BIGENDIAN) { |
| 1581 | 128 | av_fast_malloc(&s->md5_buffer, &s->md5_buffer_size, buf_size); | |
| 1582 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
128 | if (!s->md5_buffer) |
| 1583 | ✗ | return AVERROR(ENOMEM); | |
| 1584 | } | ||
| 1585 | |||
| 1586 |
2/2✓ Branch 0 taken 4926 times.
✓ Branch 1 taken 128 times.
|
5054 | if (s->avctx->bits_per_raw_sample <= 16) { |
| 1587 | 4926 | buf = (const uint8_t *)samples; | |
| 1588 | #if HAVE_BIGENDIAN | ||
| 1589 | s->bdsp.bswap16_buf((uint16_t *) s->md5_buffer, | ||
| 1590 | (const uint16_t *) samples, buf_size / 2); | ||
| 1591 | buf = s->md5_buffer; | ||
| 1592 | #endif | ||
| 1593 |
2/2✓ Branch 0 taken 118 times.
✓ Branch 1 taken 10 times.
|
128 | } else if (s->avctx->bits_per_raw_sample <= 24) { |
| 1594 | int i; | ||
| 1595 | 118 | const int32_t *samples0 = samples; | |
| 1596 | 118 | uint8_t *tmp = s->md5_buffer; | |
| 1597 | |||
| 1598 |
2/2✓ Branch 0 taken 3840000 times.
✓ Branch 1 taken 118 times.
|
3840118 | for (i = 0; i < s->frame.blocksize * s->channels; i++) { |
| 1599 | 3840000 | int32_t v = samples0[i] >> 8; | |
| 1600 | 3840000 | AV_WL24(tmp + 3*i, v); | |
| 1601 | } | ||
| 1602 | 118 | buf = s->md5_buffer; | |
| 1603 | } else { | ||
| 1604 | /* s->avctx->bits_per_raw_sample <= 32 */ | ||
| 1605 | int i; | ||
| 1606 | 10 | const int32_t *samples0 = samples; | |
| 1607 | 10 | uint8_t *tmp = s->md5_buffer; | |
| 1608 | |||
| 1609 |
2/2✓ Branch 0 taken 88266 times.
✓ Branch 1 taken 10 times.
|
88276 | for (i = 0; i < s->frame.blocksize * s->channels; i++) |
| 1610 | 88266 | AV_WL32(tmp + 4*i, samples0[i]); | |
| 1611 | 10 | buf = s->md5_buffer; | |
| 1612 | } | ||
| 1613 | 5054 | av_md5_update(s->md5ctx, buf, buf_size); | |
| 1614 | |||
| 1615 | 5054 | return 0; | |
| 1616 | } | ||
| 1617 | |||
| 1618 | |||
| 1619 | 5202 | static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, | |
| 1620 | const AVFrame *frame, int *got_packet_ptr) | ||
| 1621 | { | ||
| 1622 | FlacEncodeContext *s; | ||
| 1623 | int frame_bytes, out_bytes, ret; | ||
| 1624 | |||
| 1625 | 5202 | s = avctx->priv_data; | |
| 1626 | |||
| 1627 | /* when the last block is reached, update the header in extradata */ | ||
| 1628 |
2/2✓ Branch 0 taken 148 times.
✓ Branch 1 taken 5054 times.
|
5202 | if (!frame) { |
| 1629 | 148 | s->max_framesize = s->max_encoded_framesize; | |
| 1630 | 148 | av_md5_final(s->md5ctx, s->md5sum); | |
| 1631 | 148 | write_streaminfo(s, avctx->extradata); | |
| 1632 | |||
| 1633 |
2/2✓ Branch 0 taken 74 times.
✓ Branch 1 taken 74 times.
|
148 | if (!s->flushed) { |
| 1634 | 74 | uint8_t *side_data = av_packet_new_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, | |
| 1635 | 74 | avctx->extradata_size); | |
| 1636 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
|
74 | if (!side_data) |
| 1637 | ✗ | return AVERROR(ENOMEM); | |
| 1638 | 74 | memcpy(side_data, avctx->extradata, avctx->extradata_size); | |
| 1639 | |||
| 1640 | 74 | avpkt->pts = s->next_pts; | |
| 1641 | |||
| 1642 | 74 | *got_packet_ptr = 1; | |
| 1643 | 74 | s->flushed = 1; | |
| 1644 | } | ||
| 1645 | |||
| 1646 | 148 | return 0; | |
| 1647 | } | ||
| 1648 | |||
| 1649 | /* change max_framesize for small final frame */ | ||
| 1650 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 4983 times.
|
5054 | if (frame->nb_samples < s->frame.blocksize) { |
| 1651 | 71 | s->max_framesize = flac_get_max_frame_size(frame->nb_samples, | |
| 1652 | s->channels, | ||
| 1653 | avctx->bits_per_raw_sample); | ||
| 1654 | } | ||
| 1655 | |||
| 1656 | 5054 | init_frame(s, frame->nb_samples); | |
| 1657 | |||
| 1658 | 5054 | copy_samples(s, frame->data[0]); | |
| 1659 | |||
| 1660 | 5054 | channel_decorrelation(s); | |
| 1661 | |||
| 1662 | 5054 | remove_wasted_bits(s); | |
| 1663 | |||
| 1664 | 5054 | frame_bytes = encode_frame(s); | |
| 1665 | |||
| 1666 | /* Fall back on verbatim mode if the compressed frame is larger than it | ||
| 1667 | would be if encoded uncompressed. */ | ||
| 1668 |
2/4✓ Branch 0 taken 5054 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5054 times.
|
5054 | if (frame_bytes < 0 || frame_bytes > s->max_framesize) { |
| 1669 | ✗ | s->frame.verbatim_only = 1; | |
| 1670 | ✗ | frame_bytes = encode_frame(s); | |
| 1671 | ✗ | if (frame_bytes < 0) { | |
| 1672 | ✗ | av_log(avctx, AV_LOG_ERROR, "Bad frame count\n"); | |
| 1673 | ✗ | return frame_bytes; | |
| 1674 | } | ||
| 1675 | } | ||
| 1676 | |||
| 1677 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 5054 times.
|
5054 | if ((ret = ff_get_encode_buffer(avctx, avpkt, frame_bytes, 0)) < 0) |
| 1678 | ✗ | return ret; | |
| 1679 | |||
| 1680 | 5054 | out_bytes = write_frame(s, avpkt); | |
| 1681 | |||
| 1682 | 5054 | s->frame_count++; | |
| 1683 | 5054 | s->sample_count += frame->nb_samples; | |
| 1684 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 5054 times.
|
5054 | if ((ret = update_md5_sum(s, frame->data[0])) < 0) { |
| 1685 | ✗ | av_log(avctx, AV_LOG_ERROR, "Error updating MD5 checksum\n"); | |
| 1686 | ✗ | return ret; | |
| 1687 | } | ||
| 1688 |
2/2✓ Branch 0 taken 444 times.
✓ Branch 1 taken 4610 times.
|
5054 | if (out_bytes > s->max_encoded_framesize) |
| 1689 | 444 | s->max_encoded_framesize = out_bytes; | |
| 1690 |
2/2✓ Branch 0 taken 219 times.
✓ Branch 1 taken 4835 times.
|
5054 | if (out_bytes < s->min_framesize) |
| 1691 | 219 | s->min_framesize = out_bytes; | |
| 1692 | |||
| 1693 | 5054 | s->next_pts = frame->pts + ff_samples_to_time_base(avctx, frame->nb_samples); | |
| 1694 | |||
| 1695 | 5054 | av_shrink_packet(avpkt, out_bytes); | |
| 1696 | |||
| 1697 | 5054 | *got_packet_ptr = 1; | |
| 1698 | 5054 | return 0; | |
| 1699 | } | ||
| 1700 | |||
| 1701 | |||
| 1702 | 90 | static av_cold int flac_encode_close(AVCodecContext *avctx) | |
| 1703 | { | ||
| 1704 | 90 | FlacEncodeContext *s = avctx->priv_data; | |
| 1705 | |||
| 1706 | 90 | av_freep(&s->md5ctx); | |
| 1707 | 90 | av_freep(&s->md5_buffer); | |
| 1708 | 90 | ff_lpc_end(&s->lpc_ctx); | |
| 1709 | 90 | return 0; | |
| 1710 | } | ||
| 1711 | |||
| 1712 | #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | ||
| 1713 | static const AVOption options[] = { | ||
| 1714 | { "lpc_coeff_precision", "LPC coefficient precision", offsetof(FlacEncodeContext, options.lpc_coeff_precision), AV_OPT_TYPE_INT, {.i64 = 15 }, 0, MAX_LPC_PRECISION, FLAGS }, | ||
| 1715 | { "lpc_type", "LPC algorithm", offsetof(FlacEncodeContext, options.lpc_type), AV_OPT_TYPE_INT, {.i64 = FF_LPC_TYPE_DEFAULT }, FF_LPC_TYPE_DEFAULT, FF_LPC_TYPE_NB-1, FLAGS, .unit = "lpc_type" }, | ||
| 1716 | { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_NONE }, INT_MIN, INT_MAX, FLAGS, .unit = "lpc_type" }, | ||
| 1717 | { "fixed", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_FIXED }, INT_MIN, INT_MAX, FLAGS, .unit = "lpc_type" }, | ||
| 1718 | { "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_LEVINSON }, INT_MIN, INT_MAX, FLAGS, .unit = "lpc_type" }, | ||
| 1719 | { "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_CHOLESKY }, INT_MIN, INT_MAX, FLAGS, .unit = "lpc_type" }, | ||
| 1720 | { "lpc_passes", "Number of passes to use for Cholesky factorization during LPC analysis", offsetof(FlacEncodeContext, options.lpc_passes), AV_OPT_TYPE_INT, {.i64 = 2 }, 1, INT_MAX, FLAGS }, | ||
| 1721 | { "min_partition_order", NULL, offsetof(FlacEncodeContext, options.min_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS }, | ||
| 1722 | { "max_partition_order", NULL, offsetof(FlacEncodeContext, options.max_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS }, | ||
| 1723 | { "prediction_order_method", "Search method for selecting prediction order", offsetof(FlacEncodeContext, options.prediction_order_method), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, ORDER_METHOD_LOG, FLAGS, .unit = "predm" }, | ||
| 1724 | { "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_EST }, INT_MIN, INT_MAX, FLAGS, .unit = "predm" }, | ||
| 1725 | { "2level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_2LEVEL }, INT_MIN, INT_MAX, FLAGS, .unit = "predm" }, | ||
| 1726 | { "4level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_4LEVEL }, INT_MIN, INT_MAX, FLAGS, .unit = "predm" }, | ||
| 1727 | { "8level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_8LEVEL }, INT_MIN, INT_MAX, FLAGS, .unit = "predm" }, | ||
| 1728 | { "search", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_SEARCH }, INT_MIN, INT_MAX, FLAGS, .unit = "predm" }, | ||
| 1729 | { "log", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_LOG }, INT_MIN, INT_MAX, FLAGS, .unit = "predm" }, | ||
| 1730 | { "ch_mode", "Stereo decorrelation mode", offsetof(FlacEncodeContext, options.ch_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, FLAC_CHMODE_MID_SIDE, FLAGS, .unit = "ch_mode" }, | ||
| 1731 | { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, FLAGS, .unit = "ch_mode" }, | ||
| 1732 | { "indep", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_INDEPENDENT }, INT_MIN, INT_MAX, FLAGS, .unit = "ch_mode" }, | ||
| 1733 | { "left_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_LEFT_SIDE }, INT_MIN, INT_MAX, FLAGS, .unit = "ch_mode" }, | ||
| 1734 | { "right_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_RIGHT_SIDE }, INT_MIN, INT_MAX, FLAGS, .unit = "ch_mode" }, | ||
| 1735 | { "mid_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_MID_SIDE }, INT_MIN, INT_MAX, FLAGS, .unit = "ch_mode" }, | ||
| 1736 | { "exact_rice_parameters", "Calculate rice parameters exactly", offsetof(FlacEncodeContext, options.exact_rice_parameters), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, | ||
| 1737 | { "multi_dim_quant", "Multi-dimensional quantization", offsetof(FlacEncodeContext, options.multi_dim_quant), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, | ||
| 1738 | { "min_prediction_order", NULL, offsetof(FlacEncodeContext, options.min_prediction_order), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, MAX_LPC_ORDER, FLAGS }, | ||
| 1739 | { "max_prediction_order", NULL, offsetof(FlacEncodeContext, options.max_prediction_order), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, MAX_LPC_ORDER, FLAGS }, | ||
| 1740 | |||
| 1741 | { NULL }, | ||
| 1742 | }; | ||
| 1743 | |||
| 1744 | static const AVClass flac_encoder_class = { | ||
| 1745 | .class_name = "FLAC encoder", | ||
| 1746 | .item_name = av_default_item_name, | ||
| 1747 | .option = options, | ||
| 1748 | .version = LIBAVUTIL_VERSION_INT, | ||
| 1749 | }; | ||
| 1750 | |||
| 1751 | const FFCodec ff_flac_encoder = { | ||
| 1752 | .p.name = "flac", | ||
| 1753 | CODEC_LONG_NAME("FLAC (Free Lossless Audio Codec)"), | ||
| 1754 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
| 1755 | .p.id = AV_CODEC_ID_FLAC, | ||
| 1756 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | | ||
| 1757 | AV_CODEC_CAP_SMALL_LAST_FRAME | | ||
| 1758 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
| 1759 | .priv_data_size = sizeof(FlacEncodeContext), | ||
| 1760 | .init = flac_encode_init, | ||
| 1761 | FF_CODEC_ENCODE_CB(flac_encode_frame), | ||
| 1762 | .close = flac_encode_close, | ||
| 1763 | CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), | ||
| 1764 | .p.priv_class = &flac_encoder_class, | ||
| 1765 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_EOF_FLUSH, | ||
| 1766 | }; | ||
| 1767 |