FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/flacenc.c
Date: 2026-10-29 07:45:57
Exec Total Coverage
Lines: 726 854 85.0%
Functions: 36 38 94.7%
Branches: 405 619 65.4%

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 int /* 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 253 static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
139 {
140 PutBitContext pb;
141
142 253 memset(header, 0, FLAC_STREAMINFO_SIZE);
143 253 init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
144
145 /* streaminfo metadata block */
146 253 put_bits(&pb, 16, s->max_blocksize);
147 253 put_bits(&pb, 16, s->max_blocksize);
148 253 put_bits(&pb, 24, s->min_framesize);
149 253 put_bits(&pb, 24, s->max_framesize);
150 253 put_bits(&pb, 20, s->samplerate);
151 253 put_bits(&pb, 3, s->channels-1);
152 253 put_bits(&pb, 5, s->avctx->bits_per_raw_sample - 1);
153 /* write 36-bit sample count in 2 put_bits() calls */
154 253 put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12);
155 253 put_bits(&pb, 12, s->sample_count & 0x000000FFFLL);
156 253 flush_put_bits(&pb);
157 253 memcpy(&header[18], s->md5sum, 16);
158 253 }
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 111 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 111 count = 16; /* frame header */
176 111 count += ch * ((7+bps+7)/8); /* subframe headers */
177
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 38 times.
111 if (ch == 2) {
178 /* for stereo, need to account for using decorrelation */
179 73 count += (( 2*bps+1) * blocksize + 7) / 8;
180 } else {
181 38 count += ( ch*bps * blocksize + 7) / 8;
182 }
183 111 count += 2; /* frame footer */
184
185 111 return count;
186 }
187
188
189 /**
190 * Set blocksize based on samplerate.
191 * Choose the closest predefined blocksize >= BLOCK_TIME_MS milliseconds.
192 */
193 16 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 16 times.
16 av_assert0(samplerate > 0);
200 16 blocksize = ff_flac_blocksize_table[1];
201 16 target = (samplerate * block_time_ms) / 1000;
202
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 16 times.
272 for (i = 0; i < 16; i++) {
203
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 64 times.
256 if (target >= ff_flac_blocksize_table[i] &&
204
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 132 times.
192 ff_flac_blocksize_table[i] > blocksize) {
205 60 blocksize = ff_flac_blocksize_table[i];
206 }
207 }
208 16 return blocksize;
209 }
210
211
212 95 static av_cold void dprint_compression_options(FlacEncodeContext *s)
213 {
214 95 AVCodecContext *avctx = s->avctx;
215 95 CompressionOptions *opt = &s->options;
216
217 95 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 91 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
95 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 91 case FF_LPC_TYPE_LEVINSON:
227 91 av_log(avctx, AV_LOG_DEBUG, " lpc type: Levinson-Durbin recursion with Welch window\n");
228 91 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 95 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 94 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.
95 switch (opt->prediction_order_method) {
239 94 case ORDER_METHOD_EST:
240 94 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "estimate");
241 94 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 95 av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n",
261 opt->min_partition_order, opt->max_partition_order);
262
263 95 av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", avctx->frame_size);
264
265 95 av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n",
266 opt->lpc_coeff_precision);
267 95 }
268
269
270 95 static av_cold int flac_encode_init(AVCodecContext *avctx)
271 {
272 95 int freq = avctx->sample_rate;
273 95 int channels = avctx->ch_layout.nb_channels;
274 95 FlacEncodeContext *s = avctx->priv_data;
275 int i, level, ret;
276 uint8_t *streaminfo;
277
278 95 s->avctx = avctx;
279
280
2/3
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
95 switch (avctx->sample_fmt) {
281 92 case AV_SAMPLE_FMT_S16:
282 92 avctx->bits_per_raw_sample = 16;
283 92 s->bps_code = 4;
284 92 break;
285 3 case AV_SAMPLE_FMT_S32:
286
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 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 2 times.
2 } 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 2 avctx->bits_per_raw_sample = 32;
300 2 s->bps_code = 7;
301 }
302 3 break;
303 }
304
305
2/4
✓ Branch 0 taken 95 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 95 times.
95 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 95 s->channels = channels;
311
312 /* find samplerate in table */
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 if (freq < 1)
314 ✗ return AVERROR(EINVAL);
315
1/2
✓ Branch 0 taken 812 times.
✗ Branch 1 not taken.
812 for (i = 1; i < 12; i++) {
316
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 717 times.
812 if (freq == ff_flac_sample_rate_table[i]) {
317 95 s->samplerate = ff_flac_sample_rate_table[i];
318 95 s->sr_code[0] = i;
319 95 s->sr_code[1] = 0;
320 95 break;
321 }
322 }
323 /* if not in table, samplerate is non-standard */
324
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 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 92 times.
✓ Branch 1 taken 3 times.
95 if (avctx->compression_level < 0)
346 92 s->options.compression_level = 5;
347 else
348 3 s->options.compression_level = avctx->compression_level;
349
350 95 level = s->options.compression_level;
351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 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 95 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 92 times.
✓ Branch 1 taken 3 times.
95 if (s->options.lpc_type == FF_LPC_TYPE_DEFAULT)
360 92 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 92 FF_LPC_TYPE_LEVINSON})[level];
365
366
1/2
✓ Branch 0 taken 95 times.
✗ Branch 1 not taken.
95 if (s->options.min_prediction_order < 0)
367 95 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 95 times.
✗ Branch 1 not taken.
95 if (s->options.max_prediction_order < 0)
369 95 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 95 times.
✗ Branch 1 not taken.
95 if (s->options.prediction_order_method < 0)
372 95 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 95 ORDER_METHOD_SEARCH})[level];
377
378
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 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 95 times.
✗ Branch 1 not taken.
95 if (s->options.min_partition_order < 0)
384 95 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 95 times.
✗ Branch 1 not taken.
95 if (s->options.max_partition_order < 0)
386 95 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 95 times.
95 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 92 times.
95 } 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
92 } else if (s->options.min_prediction_order < MIN_LPC_ORDER) {
405 ✗ av_log(avctx, AV_LOG_WARNING,
406 "invalid min prediction order %d, clamped to %d\n",
407 s->options.min_prediction_order, MIN_LPC_ORDER);
408 ✗ s->options.min_prediction_order = MIN_LPC_ORDER;
409 }
410
411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 if (s->options.max_prediction_order < s->options.min_prediction_order) {
412 ✗ av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
413 s->options.min_prediction_order, s->options.max_prediction_order);
414 ✗ return AVERROR(EINVAL);
415 }
416
417
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 16 times.
95 if (avctx->frame_size > 0) {
418
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 if (avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 avctx->frame_size > FLAC_MAX_BLOCKSIZE) {
420 ✗ av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n",
421 avctx->frame_size);
422 ✗ return AVERROR(EINVAL);
423 }
424 } else {
425 16 s->avctx->frame_size = select_blocksize(s->samplerate, s->options.block_time_ms);
426 }
427 95 s->max_blocksize = s->avctx->frame_size;
428
429 /* set maximum encoded frame size in verbatim mode */
430 190 s->max_framesize = flac_get_max_frame_size(s->avctx->frame_size,
431 s->channels,
432 95 s->avctx->bits_per_raw_sample);
433
434 /* initialize MD5 context */
435 95 s->md5ctx = av_md5_alloc();
436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 if (!s->md5ctx)
437 ✗ return AVERROR(ENOMEM);
438 95 av_md5_init(s->md5ctx);
439
440 95 streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 if (!streaminfo)
442 ✗ return AVERROR(ENOMEM);
443 95 write_streaminfo(s, streaminfo);
444 95 avctx->extradata = streaminfo;
445 95 avctx->extradata_size = FLAC_STREAMINFO_SIZE;
446
447 95 s->frame_count = 0;
448 95 s->min_framesize = s->max_framesize;
449
450
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 91 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
99 if ((channels == 3 &&
451
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 95 times.
99 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_SURROUND)) ||
452 ✗ (channels == 4 &&
453 ✗ av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_2_2) &&
454
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 95 times.
95 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_QUAD)) ||
455 ✗ (channels == 5 &&
456 ✗ av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0) &&
457
2/2
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 88 times.
95 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0_BACK)) ||
458
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 (channels == 6 &&
459
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
13 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1) &&
460 6 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK))) {
461 ✗ if (avctx->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
462 ✗ av_log(avctx, AV_LOG_ERROR, "Channel layout not supported by Flac, "
463 "output stream will have incorrect "
464 "channel layout.\n");
465 } else {
466 ✗ av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The encoder "
467 "will use Flac channel layout for "
468 "%d channels.\n", channels);
469 }
470 }
471
472 95 ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
473 s->options.max_prediction_order, FF_LPC_TYPE_LEVINSON);
474
475 95 ff_bswapdsp_init(&s->bdsp);
476 95 ff_flacencdsp_init(&s->flac_dsp);
477
478 95 dprint_compression_options(s);
479
480 95 return ret;
481 }
482
483
484 4901 static void init_frame(FlacEncodeContext *s, int nb_samples)
485 {
486 int i, ch;
487 FlacFrame *frame;
488
489 4901 frame = &s->frame;
490
491
2/2
✓ Branch 0 taken 46316 times.
✓ Branch 1 taken 16 times.
46332 for (i = 0; i < 16; i++) {
492
2/2
✓ Branch 0 taken 4885 times.
✓ Branch 1 taken 41431 times.
46316 if (nb_samples == ff_flac_blocksize_table[i]) {
493 4885 frame->blocksize = ff_flac_blocksize_table[i];
494 4885 frame->bs_code[0] = i;
495 4885 frame->bs_code[1] = 0;
496 4885 break;
497 }
498 }
499
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4885 times.
4901 if (i == 16) {
500 16 frame->blocksize = nb_samples;
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (frame->blocksize <= 256) {
502 ✗ frame->bs_code[0] = 6;
503 ✗ frame->bs_code[1] = frame->blocksize-1;
504 } else {
505 16 frame->bs_code[0] = 7;
506 16 frame->bs_code[1] = frame->blocksize-1;
507 }
508 }
509
510
2/2
✓ Branch 0 taken 13624 times.
✓ Branch 1 taken 4901 times.
18525 for (ch = 0; ch < s->channels; ch++) {
511 13624 FlacSubframe *sub = &frame->subframes[ch];
512
513 13624 sub->wasted = 0;
514 13624 sub->obits = s->avctx->bits_per_raw_sample;
515
516
2/2
✓ Branch 0 taken 258 times.
✓ Branch 1 taken 13366 times.
13624 if (sub->obits > 16)
517 258 sub->rc.coding_mode = CODING_MODE_RICE2;
518 else
519 13366 sub->rc.coding_mode = CODING_MODE_RICE;
520 }
521
522 4901 frame->verbatim_only = 0;
523 4901 }
524
525
526 /**
527 * Copy channel-interleaved input samples into separate subframes.
528 */
529 4901 static void copy_samples(FlacEncodeContext *s, const void *samples)
530 {
531 int i, j, ch;
532 FlacFrame *frame;
533
534 #define COPY_SAMPLES(bits, shift0) do { \
535 const int ## bits ## _t *samples0 = samples; \
536 const int shift = shift0; \
537 frame = &s->frame; \
538 for (i = 0, j = 0; i < frame->blocksize; i++) \
539 for (ch = 0; ch < s->channels; ch++, j++) \
540 frame->subframes[ch].samples[i] = samples0[j] >> shift; \
541 } while (0)
542
543
2/2
✓ Branch 0 taken 4772 times.
✓ Branch 1 taken 129 times.
4901 if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S16)
544
4/4
✓ Branch 0 taken 80089826 times.
✓ Branch 1 taken 27323027 times.
✓ Branch 2 taken 27323027 times.
✓ Branch 3 taken 4772 times.
107417625 COPY_SAMPLES(16, 0);
545 else
546
4/4
✓ Branch 0 taken 3928266 times.
✓ Branch 1 taken 1964133 times.
✓ Branch 2 taken 1964133 times.
✓ Branch 3 taken 129 times.
5892528 COPY_SAMPLES(32, 32 - s->avctx->bits_per_raw_sample);
547 4901 }
548
549
550 107949 static uint64_t rice_count_exact(const int32_t *res, int n, int k)
551 {
552 int i;
553 107949 uint64_t count = 0;
554
555
2/2
✓ Branch 0 taken 47504444 times.
✓ Branch 1 taken 107949 times.
47612393 for (i = 0; i < n; i++) {
556 47504444 unsigned v = ((unsigned)(res[i]) << 1) ^ (res[i] >> 31);
557 47504444 count += (v >> k) + 1 + k;
558 }
559 107949 return count;
560 }
561
562
563 13624 static uint64_t subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub,
564 int pred_order)
565 {
566 int p, porder, psize;
567 int i, part_end;
568 13624 uint64_t count = 0;
569
570 /* subframe header */
571 13624 count += 8;
572
573
2/2
✓ Branch 0 taken 5214 times.
✓ Branch 1 taken 8410 times.
13624 if (sub->wasted)
574 5214 count += sub->wasted;
575
576 /* subframe */
577
2/2
✓ Branch 0 taken 5803 times.
✓ Branch 1 taken 7821 times.
13624 if (sub->type == FLAC_SUBFRAME_CONSTANT) {
578 5803 count += sub->obits;
579
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7820 times.
7821 } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
580 1 count += s->frame.blocksize * sub->obits;
581 } else {
582 /* warm-up samples */
583 7820 count += pred_order * sub->obits;
584
585 /* LPC coefficients */
586
2/2
✓ Branch 0 taken 7418 times.
✓ Branch 1 taken 402 times.
7820 if (sub->type == FLAC_SUBFRAME_LPC)
587 7418 count += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
588
589 /* rice-encoded block */
590 7820 count += 2;
591
592 /* partition order */
593 7820 porder = sub->rc.porder;
594 7820 psize = s->frame.blocksize >> porder;
595 7820 count += 4;
596
597 /* residual */
598 7820 i = pred_order;
599 7820 part_end = psize;
600
2/2
✓ Branch 0 taken 107949 times.
✓ Branch 1 taken 7820 times.
115769 for (p = 0; p < 1 << porder; p++) {
601 107949 int k = sub->rc.params[p];
602 107949 count += sub->rc.coding_mode;
603 107949 count += rice_count_exact(&sub->residual[i], part_end - i, k);
604 107949 i = part_end;
605 107949 part_end = FFMIN(s->frame.blocksize, part_end + psize);
606 }
607 }
608
609 13624 return count;
610 }
611
612
613 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
614
615 /**
616 * Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
617 */
618 37715363 static int find_optimal_param(uint64_t sum, int n, int max_param)
619 {
620 int k;
621 uint64_t sum2;
622
623
2/2
✓ Branch 0 taken 3653987 times.
✓ Branch 1 taken 34061376 times.
37715363 if (sum <= n >> 1)
624 3653987 return 0;
625 34061376 sum2 = sum - (n >> 1);
626 34061376 k = av_log2(av_clipl_int32(sum2 / n));
627 34061376 return FFMIN(k, max_param);
628 }
629
630 7560 static int find_optimal_param_exact(uint64_t sums[32][MAX_PARTITIONS], int i, int max_param)
631 {
632 7560 int bestk = 0;
633 7560 int64_t bestbits = INT64_MAX;
634 int k;
635
636
2/2
✓ Branch 0 taken 113400 times.
✓ Branch 1 taken 7560 times.
120960 for (k = 0; k <= max_param; k++) {
637 113400 int64_t bits = sums[k][i];
638
2/2
✓ Branch 0 taken 71661 times.
✓ Branch 1 taken 41739 times.
113400 if (bits < bestbits) {
639 71661 bestbits = bits;
640 71661 bestk = k;
641 }
642 }
643
644 7560 return bestk;
645 }
646
647 739633 static uint64_t calc_optimal_rice_params(RiceContext *rc, int porder,
648 uint64_t sums[32][MAX_PARTITIONS],
649 int n, int pred_order, int max_param, int exact)
650 {
651 int i;
652 int k, cnt, part;
653 uint64_t all_bits;
654
655 739633 part = (1 << porder);
656 739633 all_bits = 4 * part;
657
658 739633 cnt = (n >> porder) - pred_order;
659
2/2
✓ Branch 0 taken 37712559 times.
✓ Branch 1 taken 739633 times.
38452192 for (i = 0; i < part; i++) {
660
2/2
✓ Branch 0 taken 7560 times.
✓ Branch 1 taken 37704999 times.
37712559 if (exact) {
661 7560 k = find_optimal_param_exact(sums, i, max_param);
662 7560 all_bits += sums[k][i];
663 } else {
664 37704999 k = find_optimal_param(sums[0][i], cnt, max_param);
665 37704999 all_bits += rice_encode_count(sums[0][i], cnt, k);
666 }
667 37712559 rc->params[i] = k;
668 37712559 cnt = n >> porder;
669 }
670
671 739633 rc->porder = porder;
672
673 739633 return all_bits;
674 }
675
676
677 92777 static void calc_sum_top(int pmax, int kmax, const uint32_t *data, int n, int pred_order,
678 uint64_t sums[32][MAX_PARTITIONS])
679 {
680 int i, k;
681 int parts;
682 const uint32_t *res, *res_end;
683
684 /* sums for highest level */
685 92777 parts = (1 << pmax);
686
687
2/2
✓ Branch 0 taken 99833 times.
✓ Branch 1 taken 92777 times.
192610 for (k = 0; k <= kmax; k++) {
688 99833 res = &data[pred_order];
689 99833 res_end = &data[n >> pmax];
690
2/2
✓ Branch 0 taken 18959116 times.
✓ Branch 1 taken 99833 times.
19058949 for (i = 0; i < parts; i++) {
691
2/2
✓ Branch 0 taken 60480 times.
✓ Branch 1 taken 18898636 times.
18959116 if (kmax) {
692 60480 uint64_t sum = (1LL + k) * (res_end - res);
693
2/2
✓ Branch 0 taken 30705015 times.
✓ Branch 1 taken 60480 times.
30765495 while (res < res_end)
694 30705015 sum += *(res++) >> k;
695 60480 sums[k][i] = sum;
696 } else {
697 18898636 uint64_t sum = 0;
698
2/2
✓ Branch 0 taken 354537819 times.
✓ Branch 1 taken 18898636 times.
373436455 while (res < res_end)
699 354537819 sum += *(res++);
700 18898636 sums[k][i] = sum;
701 }
702 18959116 res_end += n >> pmax;
703 }
704 }
705 92777 }
706
707 646856 static void calc_sum_next(int level, uint64_t sums[32][MAX_PARTITIONS], int kmax)
708 {
709 int i, k;
710 646856 int parts = (1 << level);
711
2/2
✓ Branch 0 taken 18809891 times.
✓ Branch 1 taken 646856 times.
19456747 for (i = 0; i < parts; i++) {
712
2/2
✓ Branch 0 taken 18859283 times.
✓ Branch 1 taken 18809891 times.
37669174 for (k=0; k<=kmax; k++)
713 18859283 sums[k][i] = sums[k][2*i] + sums[k][2*i+1];
714 }
715 646856 }
716
717 92777 static uint64_t calc_rice_params(RiceContext *rc,
718 uint32_t udata[FLAC_MAX_BLOCKSIZE],
719 uint64_t sums[32][MAX_PARTITIONS],
720 int pmin, int pmax,
721 const int32_t *data, int n, int pred_order, int exact)
722 {
723 int i;
724 uint64_t bits[MAX_PARTITION_ORDER+1];
725 int opt_porder;
726 RiceContext tmp_rc;
727 92777 int kmax = (1 << rc->coding_mode) - 2;
728
729 av_assert1(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
730 av_assert1(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
731 av_assert1(pmin <= pmax);
732
733 92777 tmp_rc.coding_mode = rc->coding_mode;
734
735
2/2
✓ Branch 0 taken 356584820 times.
✓ Branch 1 taken 92777 times.
356677597 for (i = pred_order; i < n; i++)
736 356584820 udata[i] = ((unsigned)(data[i]) << 1) ^ (data[i] >> 31);
737
738
2/2
✓ Branch 0 taken 504 times.
✓ Branch 1 taken 92273 times.
92777 calc_sum_top(pmax, exact ? kmax : 0, udata, n, pred_order, sums);
739
740 92777 opt_porder = pmin;
741 92777 bits[pmin] = UINT32_MAX;
742 92777 for (i = pmax; ; ) {
743 739633 bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums, n, pred_order, kmax, exact);
744
4/4
✓ Branch 0 taken 312131 times.
✓ Branch 1 taken 427502 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 312127 times.
739633 if (bits[i] < bits[opt_porder] || pmax == pmin) {
745 427506 opt_porder = i;
746 427506 *rc = tmp_rc;
747 }
748
2/2
✓ Branch 0 taken 92777 times.
✓ Branch 1 taken 646856 times.
739633 if (i == pmin)
749 92777 break;
750
2/2
✓ Branch 0 taken 1512 times.
✓ Branch 1 taken 645344 times.
646856 calc_sum_next(--i, sums, exact ? kmax : 0);
751 }
752
753 92777 return bits[opt_porder];
754 }
755
756
757 185554 static int get_max_p_order(int max_porder, int n, int order)
758 {
759 185554 int porder = FFMIN(max_porder, av_log2(n^(n-1)));
760
2/2
✓ Branch 0 taken 185110 times.
✓ Branch 1 taken 444 times.
185554 if (order > 0)
761 185110 porder = FFMIN(porder, av_log2(n/order));
762 185554 return porder;
763 }
764
765
766 92777 static uint64_t find_subframe_rice_params(FlacEncodeContext *s,
767 FlacSubframe *sub, int pred_order)
768 {
769 92777 int pmin = get_max_p_order(s->options.min_partition_order,
770 s->frame.blocksize, pred_order);
771 92777 int pmax = get_max_p_order(s->options.max_partition_order,
772 s->frame.blocksize, pred_order);
773
774 92777 uint64_t bits = 8 + pred_order * sub->obits + 2 + sub->rc.coding_mode;
775
2/2
✓ Branch 0 taken 90739 times.
✓ Branch 1 taken 2038 times.
92777 if (sub->type == FLAC_SUBFRAME_LPC)
776 90739 bits += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
777 92777 bits += calc_rice_params(&sub->rc, sub->rc_udata, sub->rc_sums, pmin, pmax, sub->residual,
778 s->frame.blocksize, pred_order, s->options.exact_rice_parameters);
779 92777 return bits;
780 }
781
782
783 2038 static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
784 int order)
785 {
786 int i;
787
788
2/2
✓ Branch 0 taken 4380 times.
✓ Branch 1 taken 2038 times.
6418 for (i = 0; i < order; i++)
789 4380 res[i] = smp[i];
790
791
2/2
✓ Branch 0 taken 222 times.
✓ Branch 1 taken 1816 times.
2038 if (order == 0) {
792
2/2
✓ Branch 0 taken 902752 times.
✓ Branch 1 taken 222 times.
902974 for (i = order; i < n; i++)
793 902752 res[i] = smp[i];
794
2/2
✓ Branch 0 taken 470 times.
✓ Branch 1 taken 1346 times.
1816 } else if (order == 1) {
795
2/2
✓ Branch 0 taken 1912214 times.
✓ Branch 1 taken 470 times.
1912684 for (i = order; i < n; i++)
796 1912214 res[i] = smp[i] - smp[i-1];
797
2/2
✓ Branch 0 taken 530 times.
✓ Branch 1 taken 816 times.
1346 } else if (order == 2) {
798 530 int a = smp[order-1] - smp[order-2];
799
2/2
✓ Branch 0 taken 1075754 times.
✓ Branch 1 taken 530 times.
1076284 for (i = order; i < n; i += 2) {
800 1075754 int b = smp[i ] - smp[i-1];
801 1075754 res[i] = b - a;
802 1075754 a = smp[i+1] - smp[i ];
803 1075754 res[i+1] = a - b;
804 }
805
2/2
✓ Branch 0 taken 414 times.
✓ Branch 1 taken 402 times.
816 } else if (order == 3) {
806 414 int a = smp[order-1] - smp[order-2];
807 414 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
808
2/2
✓ Branch 0 taken 841240 times.
✓ Branch 1 taken 414 times.
841654 for (i = order; i < n; i += 2) {
809 841240 int b = smp[i ] - smp[i-1];
810 841240 int d = b - a;
811 841240 res[i] = d - c;
812 841240 a = smp[i+1] - smp[i ];
813 841240 c = a - b;
814 841240 res[i+1] = c - d;
815 }
816 } else {
817 402 int a = smp[order-1] - smp[order-2];
818 402 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
819 402 int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
820
2/2
✓ Branch 0 taken 816274 times.
✓ Branch 1 taken 402 times.
816676 for (i = order; i < n; i += 2) {
821 816274 int b = smp[i ] - smp[i-1];
822 816274 int d = b - a;
823 816274 int f = d - c;
824 816274 res[i ] = f - e;
825 816274 a = smp[i+1] - smp[i ];
826 816274 c = a - b;
827 816274 e = c - d;
828 816274 res[i+1] = e - f;
829 }
830 }
831 2038 }
832
833
834 /* These four functions check for every residual whether it can be
835 * contained in <INT32_MIN,INT32_MAX]. In case it doesn't, the
836 * function that called this function has to try something else.
837 * Each function is duplicated, once for int32_t input, once for
838 * int64_t input */
839 #define ENCODE_RESIDUAL_FIXED_WITH_RESIDUAL_LIMIT() \
840 { \
841 for (int i = 0; i < order; i++) \
842 res[i] = smp[i]; \
843 if (order == 0) { \
844 for (int i = order; i < n; i++) { \
845 if (smp[i] == INT32_MIN) \
846 return 1; \
847 res[i] = smp[i]; \
848 } \
849 } else if (order == 1) { \
850 for (int i = order; i < n; i++) { \
851 int64_t res64 = (int64_t)smp[i] - smp[i-1]; \
852 if (res64 <= INT32_MIN || res64 > INT32_MAX) \
853 return 1; \
854 res[i] = res64; \
855 } \
856 } else if (order == 2) { \
857 for (int i = order; i < n; i++) { \
858 int64_t res64 = (int64_t)smp[i] - 2*(int64_t)smp[i-1] + smp[i-2]; \
859 if (res64 <= INT32_MIN || res64 > INT32_MAX) \
860 return 1; \
861 res[i] = res64; \
862 } \
863 } else if (order == 3) { \
864 for (int i = order; i < n; i++) { \
865 int64_t res64 = (int64_t)smp[i] - 3*(int64_t)smp[i-1] + 3*(int64_t)smp[i-2] - smp[i-3]; \
866 if (res64 <= INT32_MIN || res64 > INT32_MAX) \
867 return 1; \
868 res[i] = res64; \
869 } \
870 } else { \
871 for (int i = order; i < n; i++) { \
872 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]; \
873 if (res64 <= INT32_MIN || res64 > INT32_MAX) \
874 return 1; \
875 res[i] = res64; \
876 } \
877 } \
878 return 0; \
879 }
880
881 ✗ static int encode_residual_fixed_with_residual_limit(int32_t *res, const int32_t *smp,
882 int n, int order)
883 {
884 ✗ ENCODE_RESIDUAL_FIXED_WITH_RESIDUAL_LIMIT();
885 }
886
887
888 ✗ static int encode_residual_fixed_with_residual_limit_33bps(int32_t *res, const int64_t *smp,
889 int n, int order)
890 {
891 ✗ ENCODE_RESIDUAL_FIXED_WITH_RESIDUAL_LIMIT();
892 }
893
894 #define LPC_ENCODE_WITH_RESIDUAL_LIMIT() \
895 { \
896 for (int i = 0; i < order; i++) \
897 res[i] = smp[i]; \
898 for (int i = order; i < len; i++) { \
899 int64_t p = 0, tmp; \
900 for (int j = 0; j < order; j++) \
901 p += (int64_t)coefs[j]*smp[(i-1)-j]; \
902 p >>= shift; \
903 tmp = smp[i] - p; \
904 if (tmp <= INT32_MIN || tmp > INT32_MAX) \
905 return 1; \
906 res[i] = tmp; \
907 } \
908 return 0; \
909 }
910
911 15 static int lpc_encode_with_residual_limit(int32_t *res, const int32_t *smp, int len,
912 int order, int32_t *coefs, int shift)
913 {
914
8/10
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 422717 times.
✓ Branch 3 taken 59487 times.
✓ Branch 4 taken 59487 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 59487 times.
✓ Branch 8 taken 59487 times.
✓ Branch 9 taken 15 times.
482326 LPC_ENCODE_WITH_RESIDUAL_LIMIT();
915 }
916
917 2 static int lpc_encode_with_residual_limit_33bps(int32_t *res, const int64_t *smp, int len,
918 int order, int32_t *coefs, int shift)
919 {
920
9/10
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 49328 times.
✓ Branch 3 taken 6166 times.
✓ Branch 4 taken 6166 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 6165 times.
✓ Branch 8 taken 6166 times.
✓ Branch 9 taken 1 times.
55511 LPC_ENCODE_WITH_RESIDUAL_LIMIT();
921 }
922
923 90740 static int lpc_encode_choose_datapath(FlacEncodeContext *s, int32_t bps,
924 int32_t *res, const int32_t *smp,
925 const int64_t *smp_33bps, int len,
926 int order, int32_t *coefs, int shift)
927 {
928 90740 uint64_t max_residual_value = 0;
929 90740 int64_t max_sample_value = ((int64_t)(1) << (bps-1));
930 /* This calculates the max size of any residual with the current
931 * predictor, so we know whether we need to check the residual */
932
2/2
✓ Branch 0 taken 677268 times.
✓ Branch 1 taken 90740 times.
768008 for (int i = 0; i < order; i++)
933 677268 max_residual_value += FFABS(max_sample_value * coefs[i]);
934 90740 max_residual_value >>= shift;
935 90740 max_residual_value += max_sample_value;
936
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 90738 times.
90740 if (bps > 32) {
937
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 if (lpc_encode_with_residual_limit_33bps(res, smp_33bps, len, order, coefs, shift))
938 1 return 1;
939
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 90723 times.
90738 } else if (max_residual_value > INT32_MAX) {
940
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (lpc_encode_with_residual_limit(res, smp, len, order, coefs, shift))
941 ✗ return 1;
942
2/2
✓ Branch 0 taken 5220 times.
✓ Branch 1 taken 85503 times.
90723 } else if (bps + s->options.lpc_coeff_precision + av_log2(order) <= 32) {
943 5220 s->flac_dsp.lpc16_encode(res, smp, len, order, coefs, shift);
944 } else {
945 85503 s->flac_dsp.lpc32_encode(res, smp, len, order, coefs, shift);
946 }
947 90739 return 0;
948 }
949
950 #define DEFAULT_TO_VERBATIM() \
951 { \
952 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM; \
953 if (sub->obits <= 32) \
954 memcpy(res, smp, n * sizeof(int32_t)); \
955 return subframe_count_exact(s, sub, 0); \
956 }
957
958 13624 static int encode_residual_ch(FlacEncodeContext *s, int ch)
959 {
960 int i, n;
961 int min_order, max_order, opt_order, omethod;
962 FlacFrame *frame;
963 FlacSubframe *sub;
964 int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
965 int shift[MAX_LPC_ORDER];
966 int32_t *res, *smp;
967 int64_t *smp_33bps;
968
969 13624 frame = &s->frame;
970 13624 sub = &frame->subframes[ch];
971 13624 res = sub->residual;
972 13624 smp = sub->samples;
973 13624 smp_33bps = frame->samples_33bps;
974 13624 n = frame->blocksize;
975
976 /* CONSTANT */
977
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13622 times.
13624 if (sub->obits > 32) {
978
1/2
✓ Branch 0 taken 179 times.
✗ Branch 1 not taken.
179 for (i = 1; i < n; i++)
979
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 177 times.
179 if(smp_33bps[i] != smp_33bps[0])
980 2 break;
981
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (i == n) {
982 ✗ sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
983 ✗ return subframe_count_exact(s, sub, 0);
984 }
985 } else {
986
2/2
✓ Branch 0 taken 36543384 times.
✓ Branch 1 taken 5803 times.
36549187 for (i = 1; i < n; i++)
987
2/2
✓ Branch 0 taken 7819 times.
✓ Branch 1 taken 36535565 times.
36543384 if(smp[i] != smp[0])
988 7819 break;
989
2/2
✓ Branch 0 taken 5803 times.
✓ Branch 1 taken 7819 times.
13622 if (i == n) {
990 5803 sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
991 5803 res[0] = smp[0];
992 5803 return subframe_count_exact(s, sub, 0);
993 }
994 }
995
996 /* VERBATIM */
997
2/4
✓ Branch 0 taken 7821 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7821 times.
7821 if (frame->verbatim_only || n < 5) {
998 ✗ DEFAULT_TO_VERBATIM();
999 }
1000
1001 7821 min_order = s->options.min_prediction_order;
1002 7821 max_order = s->options.max_prediction_order;
1003 7821 omethod = s->options.prediction_order_method;
1004
1005 /* FIXED */
1006 7821 sub->type = FLAC_SUBFRAME_FIXED;
1007
1/2
✓ Branch 0 taken 7821 times.
✗ Branch 1 not taken.
7821 if (s->options.lpc_type == FF_LPC_TYPE_NONE ||
1008
3/4
✓ Branch 0 taken 7419 times.
✓ Branch 1 taken 402 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7419 times.
7821 s->options.lpc_type == FF_LPC_TYPE_FIXED || n <= max_order) {
1009 uint64_t bits[MAX_FIXED_ORDER+1];
1010
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 402 times.
402 if (max_order > MAX_FIXED_ORDER)
1011 ✗ max_order = MAX_FIXED_ORDER;
1012 402 opt_order = 0;
1013 402 bits[0] = UINT32_MAX;
1014
2/2
✓ Branch 0 taken 1804 times.
✓ Branch 1 taken 402 times.
2206 for (i = min_order; i <= max_order; i++) {
1015
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1804 times.
1804 if (sub->obits == 33) {
1016 ✗ if (encode_residual_fixed_with_residual_limit_33bps(res, smp_33bps, n, i))
1017 ✗ continue;
1018
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1804 times.
1804 } else if (sub->obits + i >= 32) {
1019 ✗ if (encode_residual_fixed_with_residual_limit(res, smp, n, i))
1020 ✗ continue;
1021 } else
1022 1804 encode_residual_fixed(res, smp, n, i);
1023 1804 bits[i] = find_subframe_rice_params(s, sub, i);
1024
2/2
✓ Branch 0 taken 1032 times.
✓ Branch 1 taken 772 times.
1804 if (bits[i] < bits[opt_order])
1025 1032 opt_order = i;
1026 }
1027
3/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 376 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
402 if (opt_order == 0 && bits[0] == UINT32_MAX) {
1028 /* No predictor found with residuals within <INT32_MIN,INT32_MAX],
1029 * so encode a verbatim subframe instead */
1030 ✗ DEFAULT_TO_VERBATIM();
1031 }
1032 402 sub->order = opt_order;
1033 402 sub->type_code = sub->type | sub->order;
1034
2/2
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 168 times.
402 if (sub->order != max_order) {
1035
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 234 times.
234 if (sub->obits == 33)
1036 ✗ encode_residual_fixed_with_residual_limit_33bps(res, smp_33bps, n, sub->order);
1037
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 234 times.
234 else if (sub->obits + i >= 32)
1038 ✗ encode_residual_fixed_with_residual_limit(res, smp, n, sub->order);
1039 else
1040 234 encode_residual_fixed(res, smp, n, sub->order);
1041 234 find_subframe_rice_params(s, sub, sub->order);
1042 }
1043 402 return subframe_count_exact(s, sub, sub->order);
1044 }
1045
1046 /* LPC */
1047 7419 sub->type = FLAC_SUBFRAME_LPC;
1048
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7417 times.
7419 if (sub->obits == 33)
1049 /* As ff_lpc_calc_coefs is shared with other codecs and the LSB
1050 * probably isn't predictable anyway, throw away LSB for analysis
1051 * so it fits 32 bit int and existing function can be used
1052 * unmodified */
1053
2/2
✓ Branch 0 taken 8192 times.
✓ Branch 1 taken 2 times.
8194 for (i = 0; i < n; i++)
1054 8192 smp[i] = smp_33bps[i] >> 1;
1055
1056 7419 opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, smp, n, min_order, max_order,
1057 7419 s->options.lpc_coeff_precision, coefs, shift, s->options.lpc_type,
1058 s->options.lpc_passes, omethod,
1059 MIN_LPC_SHIFT, MAX_LPC_SHIFT, 0);
1060
1061
3/4
✓ Branch 0 taken 7419 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7183 times.
✓ Branch 3 taken 236 times.
7419 if (omethod == ORDER_METHOD_2LEVEL ||
1062
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7183 times.
7183 omethod == ORDER_METHOD_4LEVEL ||
1063 236 omethod == ORDER_METHOD_8LEVEL) {
1064 236 int levels = 1 << omethod;
1065 uint64_t bits[1 << ORDER_METHOD_8LEVEL];
1066 236 int order = -1;
1067 236 int opt_index = levels-1;
1068 236 opt_order = max_order-1;
1069 236 bits[opt_index] = UINT32_MAX;
1070
2/2
✓ Branch 0 taken 944 times.
✓ Branch 1 taken 236 times.
1180 for (i = levels-1; i >= 0; i--) {
1071 944 int last_order = order;
1072 944 order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
1073 944 order = av_clip(order, min_order - 1, max_order - 1);
1074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 944 times.
944 if (order == last_order)
1075 ✗ continue;
1076
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]))
1077 ✗ continue;
1078 944 bits[i] = find_subframe_rice_params(s, sub, order+1);
1079
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 886 times.
944 if (bits[i] < bits[opt_index]) {
1080 58 opt_index = i;
1081 58 opt_order = order;
1082 }
1083 }
1084 236 opt_order++;
1085
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7183 times.
7183 } else if (omethod == ORDER_METHOD_SEARCH) {
1086 // brute-force optimal order search
1087 uint64_t bits[MAX_LPC_ORDER];
1088 ✗ opt_order = 0;
1089 ✗ bits[0] = UINT32_MAX;
1090 ✗ for (i = min_order-1; i < max_order; i++) {
1091 ✗ if(lpc_encode_choose_datapath(s, sub->obits, res, smp, smp_33bps, n, i+1, coefs[i], shift[i]))
1092 ✗ continue;
1093 ✗ bits[i] = find_subframe_rice_params(s, sub, i+1);
1094 ✗ if (bits[i] < bits[opt_order])
1095 ✗ opt_order = i;
1096 }
1097 ✗ opt_order++;
1098
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7183 times.
7183 } else if (omethod == ORDER_METHOD_LOG) {
1099 uint64_t bits[MAX_LPC_ORDER];
1100 int step;
1101
1102 ✗ opt_order = min_order - 1 + (max_order-min_order)/3;
1103 ✗ memset(bits, -1, sizeof(bits));
1104
1105 ✗ for (step = 16; step; step >>= 1) {
1106 ✗ int last = opt_order;
1107 ✗ for (i = last-step; i <= last+step; i += step) {
1108 ✗ if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
1109 ✗ continue;
1110 ✗ if(lpc_encode_choose_datapath(s, sub->obits, res, smp, smp_33bps, n, i+1, coefs[i], shift[i]))
1111 ✗ continue;
1112 ✗ bits[i] = find_subframe_rice_params(s, sub, i+1);
1113 ✗ if (bits[i] < bits[opt_order])
1114 ✗ opt_order = i;
1115 }
1116 }
1117 ✗ opt_order++;
1118 }
1119
1120
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7411 times.
7419 if (s->options.multi_dim_quant) {
1121 8 int allsteps = 1;
1122 int i, step, improved;
1123 8 int64_t best_score = INT64_MAX;
1124 int32_t qmax;
1125
1126 8 qmax = (1 << (s->options.lpc_coeff_precision - 1)) - 1;
1127
1128
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 8 times.
68 for (i=0; i<opt_order; i++)
1129 60 allsteps *= 3;
1130
1131 do {
1132 17 improved = 0;
1133
2/2
✓ Branch 0 taken 82377 times.
✓ Branch 1 taken 17 times.
82394 for (step = 0; step < allsteps; step++) {
1134 82377 int tmp = step;
1135 int32_t lpc_try[MAX_LPC_ORDER];
1136 82377 int64_t score = 0;
1137 82377 int diffsum = 0;
1138
1139
2/2
✓ Branch 0 taken 647352 times.
✓ Branch 1 taken 82377 times.
729729 for (i=0; i<opt_order; i++) {
1140 647352 int diff = ((tmp + 1) % 3) - 1;
1141 647352 lpc_try[i] = av_clip(coefs[opt_order - 1][i] + diff, -qmax, qmax);
1142 647352 tmp /= 3;
1143 647352 diffsum += !!diff;
1144 }
1145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82377 times.
82377 if (diffsum >8)
1146 ✗ continue;
1147
1148
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 82377 times.
82377 if(lpc_encode_choose_datapath(s, sub->obits, res, smp, smp_33bps, n, opt_order, lpc_try, shift[opt_order-1]))
1149 ✗ continue;
1150 82377 score = find_subframe_rice_params(s, sub, opt_order);
1151
2/2
✓ Branch 0 taken 378 times.
✓ Branch 1 taken 81999 times.
82377 if (score < best_score) {
1152 378 best_score = score;
1153 378 memcpy(coefs[opt_order-1], lpc_try, sizeof(*coefs));
1154 378 improved=1;
1155 }
1156 }
1157
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 8 times.
17 } while(improved);
1158 }
1159
1160 7419 sub->order = opt_order;
1161 7419 sub->type_code = sub->type | (sub->order-1);
1162 7419 sub->shift = shift[sub->order-1];
1163
2/2
✓ Branch 0 taken 22128 times.
✓ Branch 1 taken 7419 times.
29547 for (i = 0; i < sub->order; i++)
1164 22128 sub->coefs[i] = coefs[sub->order-1][i];
1165
1166
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 7418 times.
7419 if(lpc_encode_choose_datapath(s, sub->obits, res, smp, smp_33bps, n, sub->order, sub->coefs, sub->shift)) {
1167 /* No predictor found with residuals within <INT32_MIN,INT32_MAX],
1168 * so encode a verbatim subframe instead */
1169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 DEFAULT_TO_VERBATIM();
1170 }
1171
1172 7418 find_subframe_rice_params(s, sub, sub->order);
1173
1174 7418 return subframe_count_exact(s, sub, sub->order);
1175 }
1176
1177
1178 4901 static int count_frame_header(FlacEncodeContext *s)
1179 {
1180 av_unused uint8_t tmp;
1181 int count;
1182
1183 /*
1184 <14> Sync code
1185 <1> Reserved
1186 <1> Blocking strategy
1187 <4> Block size in inter-channel samples
1188 <4> Sample rate
1189 <4> Channel assignment
1190 <3> Sample size in bits
1191 <1> Reserved
1192 */
1193 4901 count = 32;
1194
1195 /* coded frame number */
1196
4/4
✓ Branch 0 taken 3749 times.
✓ Branch 1 taken 1152 times.
✓ Branch 2 taken 1152 times.
✓ Branch 3 taken 1152 times.
6053 PUT_UTF8(s->frame_count, tmp, count += 8;)
1197
1198 /* explicit block size */
1199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4901 times.
4901 if (s->frame.bs_code[0] == 6)
1200 ✗ count += 8;
1201
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4885 times.
4901 else if (s->frame.bs_code[0] == 7)
1202 16 count += 16;
1203
1204 /* explicit sample rate */
1205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4901 times.
4901 count += ((s->sr_code[0] == 12) + (s->sr_code[0] > 12) * 2) * 8;
1206
1207 /* frame header CRC-8 */
1208 4901 count += 8;
1209
1210 4901 return count;
1211 }
1212
1213
1214 4901 static int encode_frame(FlacEncodeContext *s)
1215 {
1216 int ch;
1217 uint64_t count;
1218
1219 4901 count = count_frame_header(s);
1220
1221
2/2
✓ Branch 0 taken 13624 times.
✓ Branch 1 taken 4901 times.
18525 for (ch = 0; ch < s->channels; ch++)
1222 13624 count += encode_residual_ch(s, ch);
1223
1224 4901 count += (8 - (count & 7)) & 7; // byte alignment
1225 4901 count += 16; // CRC-16
1226
1227 4901 count >>= 3;
1228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4901 times.
4901 if (count > INT_MAX)
1229 ✗ return AVERROR_BUG;
1230 4901 return count;
1231 }
1232
1233
1234 4901 static void remove_wasted_bits(FlacEncodeContext *s)
1235 {
1236 int ch, i, wasted_bits;
1237
1238
2/2
✓ Branch 0 taken 8508 times.
✓ Branch 1 taken 1607 times.
10115 for (ch = 0; ch < s->channels; ch++) {
1239 8508 FlacSubframe *sub = &s->frame.subframes[ch];
1240
1241
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8500 times.
8508 if (sub->obits > 32) {
1242 8 int64_t v = 0;
1243
2/2
✓ Branch 0 taken 27928 times.
✓ Branch 1 taken 7 times.
27935 for (i = 0; i < s->frame.blocksize; i++) {
1244 27928 v |= s->frame.samples_33bps[i];
1245
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 27927 times.
27928 if (v & 1)
1246 1 break;
1247 }
1248
1249
3/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 7 times.
8 if (!v || (v & 1))
1250 1 return;
1251
1252 7 v = ff_ctzll(v);
1253
1254 /* If any wasted bits are found, samples are moved
1255 * from frame.samples_33bps to frame.subframes[ch] */
1256
2/2
✓ Branch 0 taken 27749 times.
✓ Branch 1 taken 7 times.
27756 for (i = 0; i < s->frame.blocksize; i++)
1257 27749 sub->samples[i] = s->frame.samples_33bps[i] >> v;
1258 7 wasted_bits = v;
1259 } else {
1260 8500 int32_t v = 0;
1261
2/2
✓ Branch 0 taken 44648525 times.
✓ Branch 1 taken 6836 times.
44655361 for (i = 0; i < s->frame.blocksize; i++) {
1262 44648525 v |= sub->samples[i];
1263
2/2
✓ Branch 0 taken 1664 times.
✓ Branch 1 taken 44646861 times.
44648525 if (v & 1)
1264 1664 break;
1265 }
1266
1267
4/4
✓ Branch 0 taken 6871 times.
✓ Branch 1 taken 1629 times.
✓ Branch 2 taken 1664 times.
✓ Branch 3 taken 5207 times.
8500 if (!v || (v & 1))
1268 3293 return;
1269
1270 5207 v = ff_ctz(v);
1271
1272
2/2
✓ Branch 0 taken 34056549 times.
✓ Branch 1 taken 5207 times.
34061756 for (i = 0; i < s->frame.blocksize; i++)
1273 34056549 sub->samples[i] >>= v;
1274 5207 wasted_bits = v;
1275 }
1276
1277 5214 sub->wasted = wasted_bits;
1278 5214 sub->obits -= wasted_bits;
1279
1280 /* for 24-bit, check if removing wasted bits makes the range better
1281 * suited for using RICE instead of RICE2 for entropy coding */
1282
2/2
✓ Branch 0 taken 5200 times.
✓ Branch 1 taken 14 times.
5214 if (sub->obits <= 17)
1283 5200 sub->rc.coding_mode = CODING_MODE_RICE;
1284 }
1285 }
1286
1287
1288 2591 static int estimate_stereo_mode(const int32_t *left_ch, const int32_t *right_ch, int n,
1289 int max_rice_param, int bps)
1290 {
1291 int best;
1292 uint64_t sum[4];
1293 uint64_t score[4];
1294 int k;
1295
1296 /* calculate sum of 2nd order residual for each channel */
1297 2591 sum[0] = sum[1] = sum[2] = sum[3] = 0;
1298
2/2
✓ Branch 0 taken 2580 times.
✓ Branch 1 taken 11 times.
2591 if(bps < 30) {
1299 int32_t lt, rt;
1300
2/2
✓ Branch 0 taken 15867919 times.
✓ Branch 1 taken 2580 times.
15870499 for (int i = 2; i < n; i++) {
1301 15867919 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
1302 15867919 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
1303
2/2
✓ Branch 0 taken 12668280 times.
✓ Branch 1 taken 3199639 times.
15867919 sum[2] += FFABS((lt + rt) >> 1);
1304
2/2
✓ Branch 0 taken 13573867 times.
✓ Branch 1 taken 2294052 times.
15867919 sum[3] += FFABS(lt - rt);
1305
2/2
✓ Branch 0 taken 12674857 times.
✓ Branch 1 taken 3193062 times.
15867919 sum[0] += FFABS(lt);
1306
2/2
✓ Branch 0 taken 12674786 times.
✓ Branch 1 taken 3193133 times.
15867919 sum[1] += FFABS(rt);
1307 }
1308 } else {
1309 int64_t lt, rt;
1310
2/2
✓ Branch 0 taken 44111 times.
✓ Branch 1 taken 11 times.
44122 for (int i = 2; i < n; i++) {
1311 44111 lt = (int64_t)left_ch[i] - 2*(int64_t)left_ch[i-1] + left_ch[i-2];
1312 44111 rt = (int64_t)right_ch[i] - 2*(int64_t)right_ch[i-1] + right_ch[i-2];
1313 44111 sum[2] += FFABS((lt + rt) >> 1);
1314 44111 sum[3] += FFABS(lt - rt);
1315 44111 sum[0] += FFABS(lt);
1316 44111 sum[1] += FFABS(rt);
1317 }
1318 }
1319 /* estimate bit counts */
1320
2/2
✓ Branch 0 taken 10364 times.
✓ Branch 1 taken 2591 times.
12955 for (int i = 0; i < 4; i++) {
1321 10364 k = find_optimal_param(2 * sum[i], n, max_rice_param);
1322 10364 sum[i] = rice_encode_count( 2 * sum[i], n, k);
1323 }
1324
1325 /* calculate score for each mode */
1326 2591 score[0] = sum[0] + sum[1];
1327 2591 score[1] = sum[0] + sum[3];
1328 2591 score[2] = sum[1] + sum[3];
1329 2591 score[3] = sum[2] + sum[3];
1330
1331 /* return mode with lowest score */
1332 2591 best = 0;
1333
2/2
✓ Branch 0 taken 7773 times.
✓ Branch 1 taken 2591 times.
10364 for (int i = 1; i < 4; i++)
1334
2/2
✓ Branch 0 taken 1920 times.
✓ Branch 1 taken 5853 times.
7773 if (score[i] < score[best])
1335 1920 best = i;
1336
1337 2591 return best;
1338 }
1339
1340
1341 /**
1342 * Perform stereo channel decorrelation.
1343 */
1344 4901 static void channel_decorrelation(FlacEncodeContext *s)
1345 {
1346 FlacFrame *frame;
1347 int32_t *left, *right;
1348 int64_t *side_33bps;
1349 int n;
1350
1351 4901 frame = &s->frame;
1352 4901 n = frame->blocksize;
1353 4901 left = frame->subframes[0].samples;
1354 4901 right = frame->subframes[1].samples;
1355 4901 side_33bps = frame->samples_33bps;
1356
1357
2/2
✓ Branch 0 taken 1898 times.
✓ Branch 1 taken 3003 times.
4901 if (s->channels != 2) {
1358 1898 frame->ch_mode = FLAC_CHMODE_INDEPENDENT;
1359 1898 return;
1360 }
1361
1362
2/2
✓ Branch 0 taken 2591 times.
✓ Branch 1 taken 412 times.
3003 if (s->options.ch_mode < 0) {
1363 2591 int max_rice_param = (1 << frame->subframes[0].rc.coding_mode) - 2;
1364 2591 frame->ch_mode = estimate_stereo_mode(left, right, n, max_rice_param, s->avctx->bits_per_raw_sample);
1365 } else
1366 412 frame->ch_mode = s->options.ch_mode;
1367
1368 /* perform decorrelation and adjust bits-per-sample */
1369
2/2
✓ Branch 0 taken 1047 times.
✓ Branch 1 taken 1956 times.
3003 if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1370 1047 return;
1371
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1947 times.
1956 if(s->avctx->bits_per_raw_sample == 32) {
1372
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
9 if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
1373 int64_t tmp;
1374
2/2
✓ Branch 0 taken 4096 times.
✓ Branch 1 taken 1 times.
4097 for (int i = 0; i < n; i++) {
1375 4096 tmp = left[i];
1376 4096 left[i] = (tmp + right[i]) >> 1;
1377 4096 side_33bps[i] = tmp - right[i];
1378 }
1379 1 frame->subframes[1].obits++;
1380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
1381 ✗ for (int i = 0; i < n; i++)
1382 ✗ side_33bps[i] = (int64_t)left[i] - right[i];
1383 ✗ frame->subframes[1].obits++;
1384 } else {
1385
2/2
✓ Branch 0 taken 31845 times.
✓ Branch 1 taken 8 times.
31853 for (int i = 0; i < n; i++)
1386 31845 side_33bps[i] = (int64_t)left[i] - right[i];
1387 8 frame->subframes[0].obits++;
1388 }
1389 } else {
1390
2/2
✓ Branch 0 taken 260 times.
✓ Branch 1 taken 1687 times.
1947 if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
1391 int32_t tmp;
1392
2/2
✓ Branch 0 taken 1110809 times.
✓ Branch 1 taken 260 times.
1111069 for (int i = 0; i < n; i++) {
1393 1110809 tmp = left[i];
1394 1110809 left[i] = (tmp + right[i]) >> 1;
1395 1110809 right[i] = tmp - right[i];
1396 }
1397 260 frame->subframes[1].obits++;
1398
2/2
✓ Branch 0 taken 1482 times.
✓ Branch 1 taken 205 times.
1687 } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
1399
2/2
✓ Branch 0 taken 8002602 times.
✓ Branch 1 taken 1482 times.
8004084 for (int i = 0; i < n; i++)
1400 8002602 right[i] = left[i] - right[i];
1401 1482 frame->subframes[1].obits++;
1402 } else {
1403
2/2
✓ Branch 0 taken 836742 times.
✓ Branch 1 taken 205 times.
836947 for (int i = 0; i < n; i++)
1404 836742 left[i] -= right[i];
1405 205 frame->subframes[0].obits++;
1406 }
1407 }
1408 }
1409
1410
1411 4901 static void write_utf8(PutBitContext *pb, uint32_t val)
1412 {
1413 uint8_t tmp;
1414
4/4
✓ Branch 0 taken 3749 times.
✓ Branch 1 taken 1152 times.
✓ Branch 5 taken 1152 times.
✓ Branch 6 taken 1152 times.
6053 PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
1415 4901 }
1416
1417
1418 4901 static void write_frame_header(FlacEncodeContext *s)
1419 {
1420 FlacFrame *frame;
1421 int crc;
1422
1423 4901 frame = &s->frame;
1424
1425 4901 put_bits(&s->pb, 16, 0xFFF8);
1426 4901 put_bits(&s->pb, 4, frame->bs_code[0]);
1427 4901 put_bits(&s->pb, 4, s->sr_code[0]);
1428
1429
2/2
✓ Branch 0 taken 2945 times.
✓ Branch 1 taken 1956 times.
4901 if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1430 2945 put_bits(&s->pb, 4, s->channels-1);
1431 else
1432 1956 put_bits(&s->pb, 4, frame->ch_mode + FLAC_MAX_CHANNELS - 1);
1433
1434 4901 put_bits(&s->pb, 3, s->bps_code);
1435 4901 put_bits(&s->pb, 1, 0);
1436 4901 write_utf8(&s->pb, s->frame_count);
1437
1438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4901 times.
4901 if (frame->bs_code[0] == 6)
1439 ✗ put_bits(&s->pb, 8, frame->bs_code[1]);
1440
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4885 times.
4901 else if (frame->bs_code[0] == 7)
1441 16 put_bits(&s->pb, 16, frame->bs_code[1]);
1442
1443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4901 times.
4901 if (s->sr_code[0] == 12)
1444 ✗ put_bits(&s->pb, 8, s->sr_code[1]);
1445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4901 times.
4901 else if (s->sr_code[0] > 12)
1446 ✗ put_bits(&s->pb, 16, s->sr_code[1]);
1447
1448 4901 flush_put_bits(&s->pb);
1449 4901 crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf,
1450 4901 put_bytes_output(&s->pb));
1451 4901 put_bits(&s->pb, 8, crc);
1452 4901 }
1453
1454
1455 47504444 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k)
1456 {
1457 unsigned v, e;
1458
1459 47504444 v = ((unsigned)(i) << 1) ^ (i >> 31);
1460
1461 47504444 e = (v >> k) + 1;
1462
2/2
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 47504444 times.
47504577 while (e > 31) {
1463 133 put_bits(pb, 31, 0);
1464 133 e -= 31;
1465 }
1466 47504444 put_bits(pb, e, 1);
1467
2/2
✓ Branch 0 taken 44758801 times.
✓ Branch 1 taken 2745643 times.
47504444 if (k) {
1468 44758801 unsigned mask = UINT32_MAX >> (32-k);
1469 44758801 put_bits(pb, k, v & mask);
1470 }
1471 47504444 }
1472
1473
1474 4901 static void write_subframes(FlacEncodeContext *s)
1475 {
1476 int ch;
1477
1478
2/2
✓ Branch 0 taken 13624 times.
✓ Branch 1 taken 4901 times.
18525 for (ch = 0; ch < s->channels; ch++) {
1479 13624 FlacSubframe *sub = &s->frame.subframes[ch];
1480 int p, porder, psize;
1481 int32_t *part_end;
1482 13624 int32_t *res = sub->residual;
1483 13624 int32_t *frame_end = &sub->residual[s->frame.blocksize];
1484
1485 /* subframe header */
1486 13624 put_bits(&s->pb, 1, 0);
1487 13624 put_bits(&s->pb, 6, sub->type_code);
1488 13624 put_bits(&s->pb, 1, !!sub->wasted);
1489
2/2
✓ Branch 0 taken 5214 times.
✓ Branch 1 taken 8410 times.
13624 if (sub->wasted)
1490 5214 put_bits(&s->pb, sub->wasted, 1);
1491
1492 /* subframe */
1493
2/2
✓ Branch 0 taken 5803 times.
✓ Branch 1 taken 7821 times.
13624 if (sub->type == FLAC_SUBFRAME_CONSTANT) {
1494
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5803 times.
5803 if(sub->obits == 33)
1495 ✗ put_sbits63(&s->pb, 33, s->frame.samples_33bps[0]);
1496
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5799 times.
5803 else if(sub->obits == 32)
1497 4 put_bits32(&s->pb, res[0]);
1498 else
1499 5799 put_sbits(&s->pb, sub->obits, res[0]);
1500
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7820 times.
7821 } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
1501
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (sub->obits == 33) {
1502 1 int64_t *res64 = s->frame.samples_33bps;
1503 1 int64_t *frame_end64 = &s->frame.samples_33bps[s->frame.blocksize];
1504
2/2
✓ Branch 0 taken 4096 times.
✓ Branch 1 taken 1 times.
4097 while (res64 < frame_end64)
1505 4096 put_sbits63(&s->pb, 33, (*res64++));
1506 ✗ } else if (sub->obits == 32) {
1507 ✗ while (res < frame_end)
1508 ✗ put_bits32(&s->pb, *res++);
1509 } else {
1510 ✗ while (res < frame_end)
1511 ✗ put_sbits(&s->pb, sub->obits, *res++);
1512 }
1513 } else {
1514 /* warm-up samples */
1515
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7819 times.
7820 if (sub->obits == 33) {
1516
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (int i = 0; i < sub->order; i++)
1517 8 put_sbits63(&s->pb, 33, s->frame.samples_33bps[i]);
1518 1 res += sub->order;
1519
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 7814 times.
7819 } else if (sub->obits == 32) {
1520
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 5 times.
36 for (int i = 0; i < sub->order; i++)
1521 31 put_bits32(&s->pb, *res++);
1522 } else {
1523
2/2
✓ Branch 0 taken 23113 times.
✓ Branch 1 taken 7814 times.
30927 for (int i = 0; i < sub->order; i++)
1524 23113 put_sbits(&s->pb, sub->obits, *res++);
1525 }
1526
1527 /* LPC coefficients */
1528
2/2
✓ Branch 0 taken 7418 times.
✓ Branch 1 taken 402 times.
7820 if (sub->type == FLAC_SUBFRAME_LPC) {
1529 7418 int cbits = s->options.lpc_coeff_precision;
1530 7418 put_bits( &s->pb, 4, cbits-1);
1531 7418 put_sbits(&s->pb, 5, sub->shift);
1532
2/2
✓ Branch 0 taken 22120 times.
✓ Branch 1 taken 7418 times.
29538 for (int i = 0; i < sub->order; i++)
1533 22120 put_sbits(&s->pb, cbits, sub->coefs[i]);
1534 }
1535
1536 /* rice-encoded block */
1537 7820 put_bits(&s->pb, 2, sub->rc.coding_mode - 4);
1538
1539 /* partition order */
1540 7820 porder = sub->rc.porder;
1541 7820 psize = s->frame.blocksize >> porder;
1542 7820 put_bits(&s->pb, 4, porder);
1543
1544 /* residual */
1545 7820 part_end = &sub->residual[psize];
1546
2/2
✓ Branch 0 taken 107949 times.
✓ Branch 1 taken 7820 times.
115769 for (p = 0; p < 1 << porder; p++) {
1547 107949 int k = sub->rc.params[p];
1548 107949 put_bits(&s->pb, sub->rc.coding_mode, k);
1549
2/2
✓ Branch 0 taken 47504444 times.
✓ Branch 1 taken 107949 times.
47612393 while (res < part_end)
1550 47504444 set_sr_golomb_flac(&s->pb, *res++, k);
1551 107949 part_end = FFMIN(frame_end, part_end + psize);
1552 }
1553 }
1554 }
1555 4901 }
1556
1557
1558 4901 static void write_frame_footer(FlacEncodeContext *s)
1559 {
1560 int crc;
1561 4901 flush_put_bits(&s->pb);
1562 4901 crc = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, s->pb.buf,
1563 4901 put_bytes_output(&s->pb)));
1564 4901 put_bits(&s->pb, 16, crc);
1565 4901 flush_put_bits(&s->pb);
1566 4901 }
1567
1568
1569 4901 static int write_frame(FlacEncodeContext *s, AVPacket *avpkt)
1570 {
1571 4901 init_put_bits(&s->pb, avpkt->data, avpkt->size);
1572 4901 write_frame_header(s);
1573 4901 write_subframes(s);
1574 4901 write_frame_footer(s);
1575 4901 return put_bytes_output(&s->pb);
1576 }
1577
1578
1579 4901 static int update_md5_sum(FlacEncodeContext *s, const void *samples)
1580 {
1581 const uint8_t *buf;
1582 4901 int buf_size = s->frame.blocksize * s->channels *
1583 4901 ((s->avctx->bits_per_raw_sample + 7) / 8);
1584
1585
2/2
✓ Branch 0 taken 129 times.
✓ Branch 1 taken 4772 times.
4901 if (s->avctx->bits_per_raw_sample > 16 || HAVE_BIGENDIAN) {
1586 129 av_fast_malloc(&s->md5_buffer, &s->md5_buffer_size, buf_size);
1587
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129 times.
129 if (!s->md5_buffer)
1588 ✗ return AVERROR(ENOMEM);
1589 }
1590
1591
2/2
✓ Branch 0 taken 4772 times.
✓ Branch 1 taken 129 times.
4901 if (s->avctx->bits_per_raw_sample <= 16) {
1592 4772 buf = (const uint8_t *)samples;
1593 #if HAVE_BIGENDIAN
1594 s->bdsp.bswap16_buf((uint16_t *) s->md5_buffer,
1595 (const uint16_t *) samples, buf_size / 2);
1596 buf = s->md5_buffer;
1597 #endif
1598
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 11 times.
129 } else if (s->avctx->bits_per_raw_sample <= 24) {
1599 int i;
1600 118 const int32_t *samples0 = samples;
1601 118 uint8_t *tmp = s->md5_buffer;
1602
1603
2/2
✓ Branch 0 taken 3840000 times.
✓ Branch 1 taken 118 times.
3840118 for (i = 0; i < s->frame.blocksize * s->channels; i++) {
1604 3840000 int32_t v = samples0[i] >> 8;
1605 3840000 AV_WL24(tmp + 3*i, v);
1606 }
1607 118 buf = s->md5_buffer;
1608 } else {
1609 /* s->avctx->bits_per_raw_sample <= 32 */
1610 int i;
1611 11 const int32_t *samples0 = samples;
1612 11 uint8_t *tmp = s->md5_buffer;
1613
1614
2/2
✓ Branch 0 taken 88266 times.
✓ Branch 1 taken 11 times.
88277 for (i = 0; i < s->frame.blocksize * s->channels; i++)
1615 88266 AV_WL32(tmp + 4*i, samples0[i]);
1616 11 buf = s->md5_buffer;
1617 }
1618 4901 av_md5_update(s->md5ctx, buf, buf_size);
1619
1620 4901 return 0;
1621 }
1622
1623
1624 5059 static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1625 const AVFrame *frame, int *got_packet_ptr)
1626 {
1627 FlacEncodeContext *s;
1628 int frame_bytes, out_bytes, ret;
1629
1630 5059 s = avctx->priv_data;
1631
1632 /* when the last block is reached, update the header in extradata */
1633
2/2
✓ Branch 0 taken 158 times.
✓ Branch 1 taken 4901 times.
5059 if (!frame) {
1634 158 s->max_framesize = s->max_encoded_framesize;
1635 158 av_md5_final(s->md5ctx, s->md5sum);
1636 158 write_streaminfo(s, avctx->extradata);
1637
1638
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 79 times.
158 if (!s->flushed) {
1639 79 uint8_t *side_data = av_packet_new_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
1640 79 avctx->extradata_size);
1641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 if (!side_data)
1642 ✗ return AVERROR(ENOMEM);
1643 79 memcpy(side_data, avctx->extradata, avctx->extradata_size);
1644
1645 79 avpkt->pts = s->next_pts;
1646
1647 79 *got_packet_ptr = 1;
1648 79 s->flushed = 1;
1649 }
1650
1651 158 return 0;
1652 }
1653
1654 /* change max_framesize for small final frame */
1655
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4885 times.
4901 if (frame->nb_samples < s->frame.blocksize) {
1656 16 s->max_framesize = flac_get_max_frame_size(frame->nb_samples,
1657 s->channels,
1658 avctx->bits_per_raw_sample);
1659 }
1660
1661 4901 init_frame(s, frame->nb_samples);
1662
1663 4901 copy_samples(s, frame->data[0]);
1664
1665 4901 channel_decorrelation(s);
1666
1667 4901 remove_wasted_bits(s);
1668
1669 4901 frame_bytes = encode_frame(s);
1670
1671 /* Fall back on verbatim mode if the compressed frame is larger than it
1672 would be if encoded uncompressed. */
1673
2/4
✓ Branch 0 taken 4901 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4901 times.
4901 if (frame_bytes < 0 || frame_bytes > s->max_framesize) {
1674 ✗ s->frame.verbatim_only = 1;
1675 ✗ frame_bytes = encode_frame(s);
1676 ✗ if (frame_bytes < 0) {
1677 ✗ av_log(avctx, AV_LOG_ERROR, "Bad frame count\n");
1678 ✗ return frame_bytes;
1679 }
1680 }
1681
1682
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4901 times.
4901 if ((ret = ff_get_encode_buffer(avctx, avpkt, frame_bytes, 0)) < 0)
1683 ✗ return ret;
1684
1685 4901 out_bytes = write_frame(s, avpkt);
1686
1687 4901 s->frame_count++;
1688 4901 s->sample_count += frame->nb_samples;
1689
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4901 times.
4901 if ((ret = update_md5_sum(s, frame->data[0])) < 0) {
1690 ✗ av_log(avctx, AV_LOG_ERROR, "Error updating MD5 checksum\n");
1691 ✗ return ret;
1692 }
1693
2/2
✓ Branch 0 taken 296 times.
✓ Branch 1 taken 4605 times.
4901 if (out_bytes > s->max_encoded_framesize)
1694 296 s->max_encoded_framesize = out_bytes;
1695
2/2
✓ Branch 0 taken 233 times.
✓ Branch 1 taken 4668 times.
4901 if (out_bytes < s->min_framesize)
1696 233 s->min_framesize = out_bytes;
1697
1698 4901 s->next_pts = frame->pts + ff_samples_to_time_base(avctx, frame->nb_samples);
1699
1700 4901 av_shrink_packet(avpkt, out_bytes);
1701
1702 4901 *got_packet_ptr = 1;
1703 4901 return 0;
1704 }
1705
1706
1707 95 static av_cold int flac_encode_close(AVCodecContext *avctx)
1708 {
1709 95 FlacEncodeContext *s = avctx->priv_data;
1710
1711 95 av_freep(&s->md5ctx);
1712 95 av_freep(&s->md5_buffer);
1713 95 ff_lpc_end(&s->lpc_ctx);
1714 95 return 0;
1715 }
1716
1717 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1718 static const AVOption options[] = {
1719 { "lpc_coeff_precision", "LPC coefficient precision", offsetof(FlacEncodeContext, options.lpc_coeff_precision), AV_OPT_TYPE_INT, {.i64 = 15 }, 0, MAX_LPC_PRECISION, FLAGS },
1720 { "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" },
1721 { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_NONE }, INT_MIN, INT_MAX, FLAGS, .unit = "lpc_type" },
1722 { "fixed", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_FIXED }, INT_MIN, INT_MAX, FLAGS, .unit = "lpc_type" },
1723 { "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_LEVINSON }, INT_MIN, INT_MAX, FLAGS, .unit = "lpc_type" },
1724 { "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_CHOLESKY }, INT_MIN, INT_MAX, FLAGS, .unit = "lpc_type" },
1725 { "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 },
1726 { "min_partition_order", NULL, offsetof(FlacEncodeContext, options.min_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
1727 { "max_partition_order", NULL, offsetof(FlacEncodeContext, options.max_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
1728 { "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" },
1729 { "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_EST }, INT_MIN, INT_MAX, FLAGS, .unit = "predm" },
1730 { "2level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_2LEVEL }, INT_MIN, INT_MAX, FLAGS, .unit = "predm" },
1731 { "4level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_4LEVEL }, INT_MIN, INT_MAX, FLAGS, .unit = "predm" },
1732 { "8level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_8LEVEL }, INT_MIN, INT_MAX, FLAGS, .unit = "predm" },
1733 { "search", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_SEARCH }, INT_MIN, INT_MAX, FLAGS, .unit = "predm" },
1734 { "log", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_LOG }, INT_MIN, INT_MAX, FLAGS, .unit = "predm" },
1735 { "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" },
1736 { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, FLAGS, .unit = "ch_mode" },
1737 { "indep", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_INDEPENDENT }, INT_MIN, INT_MAX, FLAGS, .unit = "ch_mode" },
1738 { "left_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_LEFT_SIDE }, INT_MIN, INT_MAX, FLAGS, .unit = "ch_mode" },
1739 { "right_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_RIGHT_SIDE }, INT_MIN, INT_MAX, FLAGS, .unit = "ch_mode" },
1740 { "mid_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_MID_SIDE }, INT_MIN, INT_MAX, FLAGS, .unit = "ch_mode" },
1741 { "exact_rice_parameters", "Calculate rice parameters exactly", offsetof(FlacEncodeContext, options.exact_rice_parameters), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1742 { "multi_dim_quant", "Multi-dimensional quantization", offsetof(FlacEncodeContext, options.multi_dim_quant), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1743 { "min_prediction_order", NULL, offsetof(FlacEncodeContext, options.min_prediction_order), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, MAX_LPC_ORDER, FLAGS },
1744 { "max_prediction_order", NULL, offsetof(FlacEncodeContext, options.max_prediction_order), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, MAX_LPC_ORDER, FLAGS },
1745
1746 { NULL },
1747 };
1748
1749 static const AVClass flac_encoder_class = {
1750 .class_name = "FLAC encoder",
1751 .item_name = av_default_item_name,
1752 .option = options,
1753 .version = LIBAVUTIL_VERSION_INT,
1754 };
1755
1756 const FFCodec ff_flac_encoder = {
1757 .p.name = "flac",
1758 CODEC_LONG_NAME("FLAC (Free Lossless Audio Codec)"),
1759 .p.type = AVMEDIA_TYPE_AUDIO,
1760 .p.id = AV_CODEC_ID_FLAC,
1761 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1762 AV_CODEC_CAP_SMALL_LAST_FRAME |
1763 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1764 .priv_data_size = sizeof(FlacEncodeContext),
1765 .init = flac_encode_init,
1766 FF_CODEC_ENCODE_CB(flac_encode_frame),
1767 .close = flac_encode_close,
1768 CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32),
1769 .p.priv_class = &flac_encoder_class,
1770 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_EOF_FLUSH,
1771 };
1772