FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/flacenc.c
Date: 2022-07-05 19:52:29
Exec Total Coverage
Lines: 623 724 86.0%
Branches: 306 411 74.5%

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/opt.h"
28
29 #include "avcodec.h"
30 #include "bswapdsp.h"
31 #include "codec_internal.h"
32 #include "encode.h"
33 #include "put_bits.h"
34 #include "put_golomb.h"
35 #include "internal.h"
36 #include "lpc.h"
37 #include "flac.h"
38 #include "flacdata.h"
39 #include "flacdsp.h"
40
41 #define FLAC_SUBFRAME_CONSTANT 0
42 #define FLAC_SUBFRAME_VERBATIM 1
43 #define FLAC_SUBFRAME_FIXED 8
44 #define FLAC_SUBFRAME_LPC 32
45
46 #define MAX_FIXED_ORDER 4
47 #define MAX_PARTITION_ORDER 8
48 #define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER)
49 #define MAX_LPC_PRECISION 15
50 #define MIN_LPC_SHIFT 0
51 #define MAX_LPC_SHIFT 15
52
53 enum CodingMode {
54 CODING_MODE_RICE = 4,
55 CODING_MODE_RICE2 = 5,
56 };
57
58 typedef struct CompressionOptions {
59 int compression_level;
60 int block_time_ms;
61 enum FFLPCType lpc_type;
62 int lpc_passes;
63 int lpc_coeff_precision;
64 int min_prediction_order;
65 int max_prediction_order;
66 int prediction_order_method;
67 int min_partition_order;
68 int max_partition_order;
69 int ch_mode;
70 int exact_rice_parameters;
71 int multi_dim_quant;
72 } CompressionOptions;
73
74 typedef struct RiceContext {
75 enum CodingMode coding_mode;
76 int porder;
77 int params[MAX_PARTITIONS];
78 } RiceContext;
79
80 typedef struct FlacSubframe {
81 int type;
82 int type_code;
83 int obits;
84 int wasted;
85 int order;
86 int32_t coefs[MAX_LPC_ORDER];
87 int shift;
88
89 RiceContext rc;
90 uint32_t rc_udata[FLAC_MAX_BLOCKSIZE];
91 uint64_t rc_sums[32][MAX_PARTITIONS];
92
93 int32_t samples[FLAC_MAX_BLOCKSIZE];
94 int32_t residual[FLAC_MAX_BLOCKSIZE+11];
95 } FlacSubframe;
96
97 typedef struct FlacFrame {
98 FlacSubframe subframes[FLAC_MAX_CHANNELS];
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 FLACDSPContext 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 64 static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
139 {
140 PutBitContext pb;
141
142 64 memset(header, 0, FLAC_STREAMINFO_SIZE);
143 64 init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
144
145 /* streaminfo metadata block */
146 64 put_bits(&pb, 16, s->max_blocksize);
147 64 put_bits(&pb, 16, s->max_blocksize);
148 64 put_bits(&pb, 24, s->min_framesize);
149 64 put_bits(&pb, 24, s->max_framesize);
150 64 put_bits(&pb, 20, s->samplerate);
151 64 put_bits(&pb, 3, s->channels-1);
152 64 put_bits(&pb, 5, s->avctx->bits_per_raw_sample - 1);
153 /* write 36-bit sample count in 2 put_bits() calls */
154 64 put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12);
155 64 put_bits(&pb, 12, s->sample_count & 0x000000FFFLL);
156 64 flush_put_bits(&pb);
157 64 memcpy(&header[18], s->md5sum, 16);
158 64 }
159
160
161 /**
162 * Set blocksize based on samplerate.
163 * Choose the closest predefined blocksize >= BLOCK_TIME_MS milliseconds.
164 */
165 32 static int select_blocksize(int samplerate, int block_time_ms)
166 {
167 int i;
168 int target;
169 int blocksize;
170
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 av_assert0(samplerate > 0);
172 32 blocksize = ff_flac_blocksize_table[1];
173 32 target = (samplerate * block_time_ms) / 1000;
174
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 32 times.
544 for (i = 0; i < 16; i++) {
175
2/2
✓ Branch 0 taken 394 times.
✓ Branch 1 taken 118 times.
512 if (target >= ff_flac_blocksize_table[i] &&
176
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 272 times.
394 ff_flac_blocksize_table[i] > blocksize) {
177 122 blocksize = ff_flac_blocksize_table[i];
178 }
179 }
180 32 return blocksize;
181 }
182
183
184 32 static av_cold void dprint_compression_options(FlacEncodeContext *s)
185 {
186 32 AVCodecContext *avctx = s->avctx;
187 32 CompressionOptions *opt = &s->options;
188
189 32 av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", opt->compression_level);
190
191
3/5
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
32 switch (opt->lpc_type) {
192 case FF_LPC_TYPE_NONE:
193 av_log(avctx, AV_LOG_DEBUG, " lpc type: None\n");
194 break;
195 3 case FF_LPC_TYPE_FIXED:
196 3 av_log(avctx, AV_LOG_DEBUG, " lpc type: Fixed pre-defined coefficients\n");
197 3 break;
198 28 case FF_LPC_TYPE_LEVINSON:
199 28 av_log(avctx, AV_LOG_DEBUG, " lpc type: Levinson-Durbin recursion with Welch window\n");
200 28 break;
201 1 case FF_LPC_TYPE_CHOLESKY:
202 1 av_log(avctx, AV_LOG_DEBUG, " lpc type: Cholesky factorization, %d pass%s\n",
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 opt->lpc_passes, opt->lpc_passes == 1 ? "" : "es");
204 1 break;
205 }
206
207 32 av_log(avctx, AV_LOG_DEBUG, " prediction order: %d, %d\n",
208 opt->min_prediction_order, opt->max_prediction_order);
209
210
2/7
✓ Branch 0 taken 31 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.
32 switch (opt->prediction_order_method) {
211 31 case ORDER_METHOD_EST:
212 31 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "estimate");
213 31 break;
214 case ORDER_METHOD_2LEVEL:
215 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "2-level");
216 break;
217 1 case ORDER_METHOD_4LEVEL:
218 1 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "4-level");
219 1 break;
220 case ORDER_METHOD_8LEVEL:
221 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "8-level");
222 break;
223 case ORDER_METHOD_SEARCH:
224 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "full search");
225 break;
226 case ORDER_METHOD_LOG:
227 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "log search");
228 break;
229 }
230
231
232 32 av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n",
233 opt->min_partition_order, opt->max_partition_order);
234
235 32 av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", avctx->frame_size);
236
237 32 av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n",
238 opt->lpc_coeff_precision);
239 32 }
240
241
242 32 static av_cold int flac_encode_init(AVCodecContext *avctx)
243 {
244 32 int freq = avctx->sample_rate;
245 32 int channels = avctx->ch_layout.nb_channels;
246 32 FlacEncodeContext *s = avctx->priv_data;
247 int i, level, ret;
248 uint8_t *streaminfo;
249
250 32 s->avctx = avctx;
251
252
2/3
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
32 switch (avctx->sample_fmt) {
253 31 case AV_SAMPLE_FMT_S16:
254 31 avctx->bits_per_raw_sample = 16;
255 31 s->bps_code = 4;
256 31 break;
257 1 case AV_SAMPLE_FMT_S32:
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avctx->bits_per_raw_sample != 24)
259 av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
260 1 avctx->bits_per_raw_sample = 24;
261 1 s->bps_code = 6;
262 1 break;
263 }
264
265
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 32 times.
32 if (channels < 1 || channels > FLAC_MAX_CHANNELS) {
266 av_log(avctx, AV_LOG_ERROR, "%d channels not supported (max %d)\n",
267 channels, FLAC_MAX_CHANNELS);
268 return AVERROR(EINVAL);
269 }
270 32 s->channels = channels;
271
272 /* find samplerate in table */
273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (freq < 1)
274 return AVERROR(EINVAL);
275
2/2
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 5 times.
194 for (i = 4; i < 12; i++) {
276
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 162 times.
189 if (freq == ff_flac_sample_rate_table[i]) {
277 27 s->samplerate = ff_flac_sample_rate_table[i];
278 27 s->sr_code[0] = i;
279 27 s->sr_code[1] = 0;
280 27 break;
281 }
282 }
283 /* if not in table, samplerate is non-standard */
284
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 27 times.
32 if (i == 12) {
285
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 if (freq % 1000 == 0 && freq < 255000) {
286 5 s->sr_code[0] = 12;
287 5 s->sr_code[1] = freq / 1000;
288 } else if (freq % 10 == 0 && freq < 655350) {
289 s->sr_code[0] = 14;
290 s->sr_code[1] = freq / 10;
291 } else if (freq < 65535) {
292 s->sr_code[0] = 13;
293 s->sr_code[1] = freq;
294 } else {
295 av_log(avctx, AV_LOG_ERROR, "%d Hz not supported\n", freq);
296 return AVERROR(EINVAL);
297 }
298 5 s->samplerate = freq;
299 }
300
301 /* set compression option defaults based on avctx->compression_level */
302
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 3 times.
32 if (avctx->compression_level < 0)
303 29 s->options.compression_level = 5;
304 else
305 3 s->options.compression_level = avctx->compression_level;
306
307 32 level = s->options.compression_level;
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (level > 12) {
309 av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n",
310 s->options.compression_level);
311 return AVERROR(EINVAL);
312 }
313
314 32 s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
315
316
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 3 times.
32 if (s->options.lpc_type == FF_LPC_TYPE_DEFAULT)
317 29 s->options.lpc_type = ((int[]){ FF_LPC_TYPE_FIXED, FF_LPC_TYPE_FIXED, FF_LPC_TYPE_FIXED,
318 FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON,
319 FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON,
320 FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON,
321 29 FF_LPC_TYPE_LEVINSON})[level];
322
323
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (s->options.min_prediction_order < 0)
324 32 s->options.min_prediction_order = ((int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
325
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (s->options.max_prediction_order < 0)
326 32 s->options.max_prediction_order = ((int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level];
327
328
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (s->options.prediction_order_method < 0)
329 32 s->options.prediction_order_method = ((int[]){ ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST,
330 ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST,
331 ORDER_METHOD_4LEVEL, ORDER_METHOD_LOG, ORDER_METHOD_4LEVEL,
332 ORDER_METHOD_LOG, ORDER_METHOD_SEARCH, ORDER_METHOD_LOG,
333 32 ORDER_METHOD_SEARCH})[level];
334
335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (s->options.min_partition_order > s->options.max_partition_order) {
336 av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n",
337 s->options.min_partition_order, s->options.max_partition_order);
338 return AVERROR(EINVAL);
339 }
340
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (s->options.min_partition_order < 0)
341 32 s->options.min_partition_order = ((int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level];
342
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (s->options.max_partition_order < 0)
343 32 s->options.max_partition_order = ((int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level];
344
345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (s->options.lpc_type == FF_LPC_TYPE_NONE) {
346 s->options.min_prediction_order = 0;
347 s->options.max_prediction_order = 0;
348
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 29 times.
32 } else if (s->options.lpc_type == FF_LPC_TYPE_FIXED) {
349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (s->options.min_prediction_order > MAX_FIXED_ORDER) {
350 av_log(avctx, AV_LOG_WARNING,
351 "invalid min prediction order %d, clamped to %d\n",
352 s->options.min_prediction_order, MAX_FIXED_ORDER);
353 s->options.min_prediction_order = MAX_FIXED_ORDER;
354 }
355
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (s->options.max_prediction_order > MAX_FIXED_ORDER) {
356 1 av_log(avctx, AV_LOG_WARNING,
357 "invalid max prediction order %d, clamped to %d\n",
358 s->options.max_prediction_order, MAX_FIXED_ORDER);
359 1 s->options.max_prediction_order = MAX_FIXED_ORDER;
360 }
361 }
362
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (s->options.max_prediction_order < s->options.min_prediction_order) {
364 av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
365 s->options.min_prediction_order, s->options.max_prediction_order);
366 return AVERROR(EINVAL);
367 }
368
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (avctx->frame_size > 0) {
370 if (avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
371 avctx->frame_size > FLAC_MAX_BLOCKSIZE) {
372 av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n",
373 avctx->frame_size);
374 return AVERROR(EINVAL);
375 }
376 } else {
377 32 s->avctx->frame_size = select_blocksize(s->samplerate, s->options.block_time_ms);
378 }
379 32 s->max_blocksize = s->avctx->frame_size;
380
381 /* set maximum encoded frame size in verbatim mode */
382 64 s->max_framesize = ff_flac_get_max_frame_size(s->avctx->frame_size,
383 s->channels,
384 32 s->avctx->bits_per_raw_sample);
385
386 /* initialize MD5 context */
387 32 s->md5ctx = av_md5_alloc();
388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (!s->md5ctx)
389 return AVERROR(ENOMEM);
390 32 av_md5_init(s->md5ctx);
391
392 32 streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (!streaminfo)
394 return AVERROR(ENOMEM);
395 32 write_streaminfo(s, streaminfo);
396 32 avctx->extradata = streaminfo;
397 32 avctx->extradata_size = FLAC_STREAMINFO_SIZE;
398
399 32 s->frame_count = 0;
400 32 s->min_framesize = s->max_framesize;
401
402
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
36 if ((channels == 3 &&
403
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
36 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_SURROUND)) ||
404 (channels == 4 &&
405 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_2_2) &&
406
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
32 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_QUAD)) ||
407 (channels == 5 &&
408 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0) &&
409
2/2
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 25 times.
32 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0_BACK)) ||
410
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 (channels == 6 &&
411
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
13 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1) &&
412 6 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK))) {
413 if (avctx->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
414 av_log(avctx, AV_LOG_ERROR, "Channel layout not supported by Flac, "
415 "output stream will have incorrect "
416 "channel layout.\n");
417 } else {
418 av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The encoder "
419 "will use Flac channel layout for "
420 "%d channels.\n", channels);
421 }
422 }
423
424 32 ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
425 s->options.max_prediction_order, FF_LPC_TYPE_LEVINSON);
426
427 32 ff_bswapdsp_init(&s->bdsp);
428 32 ff_flacdsp_init(&s->flac_dsp, avctx->sample_fmt, channels,
429 avctx->bits_per_raw_sample);
430
431 32 dprint_compression_options(s);
432
433 32 return ret;
434 }
435
436
437 4483 static void init_frame(FlacEncodeContext *s, int nb_samples)
438 {
439 int i, ch;
440 FlacFrame *frame;
441
442 4483 frame = &s->frame;
443
444
2/2
✓ Branch 0 taken 31983 times.
✓ Branch 1 taken 13 times.
31996 for (i = 0; i < 16; i++) {
445
2/2
✓ Branch 0 taken 4470 times.
✓ Branch 1 taken 27513 times.
31983 if (nb_samples == ff_flac_blocksize_table[i]) {
446 4470 frame->blocksize = ff_flac_blocksize_table[i];
447 4470 frame->bs_code[0] = i;
448 4470 frame->bs_code[1] = 0;
449 4470 break;
450 }
451 }
452
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 4470 times.
4483 if (i == 16) {
453 13 frame->blocksize = nb_samples;
454
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (frame->blocksize <= 256) {
455 frame->bs_code[0] = 6;
456 frame->bs_code[1] = frame->blocksize-1;
457 } else {
458 13 frame->bs_code[0] = 7;
459 13 frame->bs_code[1] = frame->blocksize-1;
460 }
461 }
462
463
2/2
✓ Branch 0 taken 13088 times.
✓ Branch 1 taken 4483 times.
17571 for (ch = 0; ch < s->channels; ch++) {
464 13088 FlacSubframe *sub = &frame->subframes[ch];
465
466 13088 sub->wasted = 0;
467 13088 sub->obits = s->avctx->bits_per_raw_sample;
468
469
2/2
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 12852 times.
13088 if (sub->obits > 16)
470 236 sub->rc.coding_mode = CODING_MODE_RICE2;
471 else
472 12852 sub->rc.coding_mode = CODING_MODE_RICE;
473 }
474
475 4483 frame->verbatim_only = 0;
476 4483 }
477
478
479 /**
480 * Copy channel-interleaved input samples into separate subframes.
481 */
482 4483 static void copy_samples(FlacEncodeContext *s, const void *samples)
483 {
484 int i, j, ch;
485 FlacFrame *frame;
486 4483 int shift = av_get_bytes_per_sample(s->avctx->sample_fmt) * 8 -
487 4483 s->avctx->bits_per_raw_sample;
488
489 #define COPY_SAMPLES(bits) do { \
490 const int ## bits ## _t *samples0 = samples; \
491 frame = &s->frame; \
492 for (i = 0, j = 0; i < frame->blocksize; i++) \
493 for (ch = 0; ch < s->channels; ch++, j++) \
494 frame->subframes[ch].samples[i] = samples0[j] >> shift; \
495 } while (0)
496
497
2/2
✓ Branch 0 taken 4365 times.
✓ Branch 1 taken 118 times.
4483 if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S16)
498
4/4
✓ Branch 0 taken 76113402 times.
✓ Branch 1 taken 24701983 times.
✓ Branch 2 taken 24701983 times.
✓ Branch 3 taken 4365 times.
100819750 COPY_SAMPLES(16);
499 else
500
4/4
✓ Branch 0 taken 3840000 times.
✓ Branch 1 taken 1920000 times.
✓ Branch 2 taken 1920000 times.
✓ Branch 3 taken 118 times.
5760118 COPY_SAMPLES(32);
501 4483 }
502
503
504 98660 static uint64_t rice_count_exact(const int32_t *res, int n, int k)
505 {
506 int i;
507 98660 uint64_t count = 0;
508
509
2/2
✓ Branch 0 taken 44768181 times.
✓ Branch 1 taken 98660 times.
44866841 for (i = 0; i < n; i++) {
510 44768181 int32_t v = -2 * res[i] - 1;
511 44768181 v ^= v >> 31;
512 44768181 count += (v >> k) + 1 + k;
513 }
514 98660 return count;
515 }
516
517
518 13088 static uint64_t subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub,
519 int pred_order)
520 {
521 int p, porder, psize;
522 int i, part_end;
523 13088 uint64_t count = 0;
524
525 /* subframe header */
526 13088 count += 8;
527
528
2/2
✓ Branch 0 taken 5200 times.
✓ Branch 1 taken 7888 times.
13088 if (sub->wasted)
529 5200 count += sub->wasted;
530
531 /* subframe */
532
2/2
✓ Branch 0 taken 5628 times.
✓ Branch 1 taken 7460 times.
13088 if (sub->type == FLAC_SUBFRAME_CONSTANT) {
533 5628 count += sub->obits;
534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7460 times.
7460 } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
535 count += s->frame.blocksize * sub->obits;
536 } else {
537 /* warm-up samples */
538 7460 count += pred_order * sub->obits;
539
540 /* LPC coefficients */
541
2/2
✓ Branch 0 taken 6586 times.
✓ Branch 1 taken 874 times.
7460 if (sub->type == FLAC_SUBFRAME_LPC)
542 6586 count += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
543
544 /* rice-encoded block */
545 7460 count += 2;
546
547 /* partition order */
548 7460 porder = sub->rc.porder;
549 7460 psize = s->frame.blocksize >> porder;
550 7460 count += 4;
551
552 /* residual */
553 7460 i = pred_order;
554 7460 part_end = psize;
555
2/2
✓ Branch 0 taken 98660 times.
✓ Branch 1 taken 7460 times.
106120 for (p = 0; p < 1 << porder; p++) {
556 98660 int k = sub->rc.params[p];
557 98660 count += sub->rc.coding_mode;
558 98660 count += rice_count_exact(&sub->residual[i], part_end - i, k);
559 98660 i = part_end;
560 98660 part_end = FFMIN(s->frame.blocksize, part_end + psize);
561 }
562 }
563
564 13088 return count;
565 }
566
567
568 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
569
570 /**
571 * Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
572 */
573 31052225 static int find_optimal_param(uint64_t sum, int n, int max_param)
574 {
575 int k;
576 uint64_t sum2;
577
578
2/2
✓ Branch 0 taken 1393359 times.
✓ Branch 1 taken 29658866 times.
31052225 if (sum <= n >> 1)
579 1393359 return 0;
580 29658866 sum2 = sum - (n >> 1);
581 29658866 k = av_log2(av_clipl_int32(sum2 / n));
582 29658866 return FFMIN(k, max_param);
583 }
584
585 27285 static int find_optimal_param_exact(uint64_t sums[32][MAX_PARTITIONS], int i, int max_param)
586 {
587 27285 int bestk = 0;
588 27285 int64_t bestbits = INT64_MAX;
589 int k;
590
591
2/2
✓ Branch 0 taken 409275 times.
✓ Branch 1 taken 27285 times.
436560 for (k = 0; k <= max_param; k++) {
592 409275 int64_t bits = sums[k][i];
593
2/2
✓ Branch 0 taken 253571 times.
✓ Branch 1 taken 155704 times.
409275 if (bits < bestbits) {
594 253571 bestbits = bits;
595 253571 bestk = k;
596 }
597 }
598
599 27285 return bestk;
600 }
601
602 665110 static uint64_t calc_optimal_rice_params(RiceContext *rc, int porder,
603 uint64_t sums[32][MAX_PARTITIONS],
604 int n, int pred_order, int max_param, int exact)
605 {
606 int i;
607 int k, cnt, part;
608 uint64_t all_bits;
609
610 665110 part = (1 << porder);
611 665110 all_bits = 4 * part;
612
613 665110 cnt = (n >> porder) - pred_order;
614
2/2
✓ Branch 0 taken 31069606 times.
✓ Branch 1 taken 665110 times.
31734716 for (i = 0; i < part; i++) {
615
2/2
✓ Branch 0 taken 27285 times.
✓ Branch 1 taken 31042321 times.
31069606 if (exact) {
616 27285 k = find_optimal_param_exact(sums, i, max_param);
617 27285 all_bits += sums[k][i];
618 } else {
619 31042321 k = find_optimal_param(sums[0][i], cnt, max_param);
620 31042321 all_bits += rice_encode_count(sums[0][i], cnt, k);
621 }
622 31069606 rc->params[i] = k;
623 31069606 cnt = n >> porder;
624 }
625
626 665110 rc->porder = porder;
627
628 665110 return all_bits;
629 }
630
631
632 90814 static void calc_sum_top(int pmax, int kmax, const uint32_t *data, int n, int pred_order,
633 uint64_t sums[32][MAX_PARTITIONS])
634 {
635 int i, k;
636 int parts;
637 const uint32_t *res, *res_end;
638
639 /* sums for highest level */
640 90814 parts = (1 << pmax);
641
642
2/2
✓ Branch 0 taken 116280 times.
✓ Branch 1 taken 90814 times.
207094 for (k = 0; k <= kmax; k++) {
643 116280 res = &data[pred_order];
644 116280 res_end = &data[n >> pmax];
645
2/2
✓ Branch 0 taken 15783938 times.
✓ Branch 1 taken 116280 times.
15900218 for (i = 0; i < parts; i++) {
646
2/2
✓ Branch 0 taken 218280 times.
✓ Branch 1 taken 15565658 times.
15783938 if (kmax) {
647 218280 uint64_t sum = (1LL + k) * (res_end - res);
648
2/2
✓ Branch 0 taken 31324725 times.
✓ Branch 1 taken 218280 times.
31543005 while (res < res_end)
649 31324725 sum += *(res++) >> k;
650 218280 sums[k][i] = sum;
651 } else {
652 15565658 uint64_t sum = 0;
653
2/2
✓ Branch 0 taken 406158131 times.
✓ Branch 1 taken 15565658 times.
421723789 while (res < res_end)
654 406158131 sum += *(res++);
655 15565658 sums[k][i] = sum;
656 }
657 15783938 res_end += n >> pmax;
658 }
659 }
660 90814 }
661
662 574296 static void calc_sum_next(int level, uint64_t sums[32][MAX_PARTITIONS], int kmax)
663 {
664 int i, k;
665 574296 int parts = (1 << level);
666
2/2
✓ Branch 0 taken 15489396 times.
✓ Branch 1 taken 574296 times.
16063692 for (i = 0; i < parts; i++) {
667
2/2
✓ Branch 0 taken 15667658 times.
✓ Branch 1 taken 15489396 times.
31157054 for (k=0; k<=kmax; k++)
668 15667658 sums[k][i] = sums[k][2*i] + sums[k][2*i+1];
669 }
670 574296 }
671
672 90814 static uint64_t calc_rice_params(RiceContext *rc,
673 uint32_t udata[FLAC_MAX_BLOCKSIZE],
674 uint64_t sums[32][MAX_PARTITIONS],
675 int pmin, int pmax,
676 const int32_t *data, int n, int pred_order, int exact)
677 {
678 int i;
679 uint64_t bits[MAX_PARTITION_ORDER+1];
680 int opt_porder;
681 RiceContext tmp_rc;
682 90814 int kmax = (1 << rc->coding_mode) - 2;
683
684 av_assert1(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
685 av_assert1(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
686 av_assert1(pmin <= pmax);
687
688 90814 tmp_rc.coding_mode = rc->coding_mode;
689
690
2/2
✓ Branch 0 taken 408902474 times.
✓ Branch 1 taken 90814 times.
408993288 for (i = 0; i < n; i++)
691 408902474 udata[i] = (2 * data[i]) ^ (data[i] >> 31);
692
693
2/2
✓ Branch 0 taken 1819 times.
✓ Branch 1 taken 88995 times.
90814 calc_sum_top(pmax, exact ? kmax : 0, udata, n, pred_order, sums);
694
695 90814 opt_porder = pmin;
696 90814 bits[pmin] = UINT32_MAX;
697 90814 for (i = pmax; ; ) {
698 665110 bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums, n, pred_order, kmax, exact);
699
4/4
✓ Branch 0 taken 278091 times.
✓ Branch 1 taken 387019 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 278089 times.
665110 if (bits[i] < bits[opt_porder] || pmax == pmin) {
700 387021 opt_porder = i;
701 387021 *rc = tmp_rc;
702 }
703
2/2
✓ Branch 0 taken 90814 times.
✓ Branch 1 taken 574296 times.
665110 if (i == pmin)
704 90814 break;
705
2/2
✓ Branch 0 taken 5457 times.
✓ Branch 1 taken 568839 times.
574296 calc_sum_next(--i, sums, exact ? kmax : 0);
706 }
707
708 90814 return bits[opt_porder];
709 }
710
711
712 181628 static int get_max_p_order(int max_porder, int n, int order)
713 {
714 181628 int porder = FFMIN(max_porder, av_log2(n^(n-1)));
715
2/2
✓ Branch 0 taken 180048 times.
✓ Branch 1 taken 1580 times.
181628 if (order > 0)
716 180048 porder = FFMIN(porder, av_log2(n/order));
717 181628 return porder;
718 }
719
720
721 90814 static uint64_t find_subframe_rice_params(FlacEncodeContext *s,
722 FlacSubframe *sub, int pred_order)
723 {
724 90814 int pmin = get_max_p_order(s->options.min_partition_order,
725 s->frame.blocksize, pred_order);
726 90814 int pmax = get_max_p_order(s->options.max_partition_order,
727 s->frame.blocksize, pred_order);
728
729 90814 uint64_t bits = 8 + pred_order * sub->obits + 2 + sub->rc.coding_mode;
730
2/2
✓ Branch 0 taken 86262 times.
✓ Branch 1 taken 4552 times.
90814 if (sub->type == FLAC_SUBFRAME_LPC)
731 86262 bits += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
732 90814 bits += calc_rice_params(&sub->rc, sub->rc_udata, sub->rc_sums, pmin, pmax, sub->residual,
733 s->frame.blocksize, pred_order, s->options.exact_rice_parameters);
734 90814 return bits;
735 }
736
737
738 4552 static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
739 int order)
740 {
741 int i;
742
743
2/2
✓ Branch 0 taken 9292 times.
✓ Branch 1 taken 4552 times.
13844 for (i = 0; i < order; i++)
744 9292 res[i] = smp[i];
745
746
2/2
✓ Branch 0 taken 790 times.
✓ Branch 1 taken 3762 times.
4552 if (order == 0) {
747
2/2
✓ Branch 0 taken 908640 times.
✓ Branch 1 taken 790 times.
909430 for (i = order; i < n; i++)
748 908640 res[i] = smp[i];
749
2/2
✓ Branch 0 taken 932 times.
✓ Branch 1 taken 2830 times.
3762 } else if (order == 1) {
750
2/2
✓ Branch 0 taken 1899976 times.
✓ Branch 1 taken 932 times.
1900908 for (i = order; i < n; i++)
751 1899976 res[i] = smp[i] - smp[i-1];
752
2/2
✓ Branch 0 taken 1004 times.
✓ Branch 1 taken 1826 times.
2830 } else if (order == 2) {
753 1004 int a = smp[order-1] - smp[order-2];
754
2/2
✓ Branch 0 taken 1090768 times.
✓ Branch 1 taken 1004 times.
1091772 for (i = order; i < n; i += 2) {
755 1090768 int b = smp[i ] - smp[i-1];
756 1090768 res[i] = b - a;
757 1090768 a = smp[i+1] - smp[i ];
758 1090768 res[i+1] = a - b;
759 }
760
2/2
✓ Branch 0 taken 952 times.
✓ Branch 1 taken 874 times.
1826 } else if (order == 3) {
761 952 int a = smp[order-1] - smp[order-2];
762 952 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
763
2/2
✓ Branch 0 taken 874622 times.
✓ Branch 1 taken 952 times.
875574 for (i = order; i < n; i += 2) {
764 874622 int b = smp[i ] - smp[i-1];
765 874622 int d = b - a;
766 874622 res[i] = d - c;
767 874622 a = smp[i+1] - smp[i ];
768 874622 c = a - b;
769 874622 res[i+1] = c - d;
770 }
771 } else {
772 874 int a = smp[order-1] - smp[order-2];
773 874 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
774 874 int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
775
2/2
✓ Branch 0 taken 815074 times.
✓ Branch 1 taken 874 times.
815948 for (i = order; i < n; i += 2) {
776 815074 int b = smp[i ] - smp[i-1];
777 815074 int d = b - a;
778 815074 int f = d - c;
779 815074 res[i ] = f - e;
780 815074 a = smp[i+1] - smp[i ];
781 815074 c = a - b;
782 815074 e = c - d;
783 815074 res[i+1] = e - f;
784 }
785 }
786 4552 }
787
788
789 13088 static int encode_residual_ch(FlacEncodeContext *s, int ch)
790 {
791 int i, n;
792 int min_order, max_order, opt_order, omethod;
793 FlacFrame *frame;
794 FlacSubframe *sub;
795 int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
796 int shift[MAX_LPC_ORDER];
797 int32_t *res, *smp;
798
799 13088 frame = &s->frame;
800 13088 sub = &frame->subframes[ch];
801 13088 res = sub->residual;
802 13088 smp = sub->samples;
803 13088 n = frame->blocksize;
804
805 /* CONSTANT */
806
2/2
✓ Branch 0 taken 35218452 times.
✓ Branch 1 taken 5628 times.
35224080 for (i = 1; i < n; i++)
807
2/2
✓ Branch 0 taken 7460 times.
✓ Branch 1 taken 35210992 times.
35218452 if(smp[i] != smp[0])
808 7460 break;
809
2/2
✓ Branch 0 taken 5628 times.
✓ Branch 1 taken 7460 times.
13088 if (i == n) {
810 5628 sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
811 5628 res[0] = smp[0];
812 5628 return subframe_count_exact(s, sub, 0);
813 }
814
815 /* VERBATIM */
816
2/4
✓ Branch 0 taken 7460 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7460 times.
7460 if (frame->verbatim_only || n < 5) {
817 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
818 memcpy(res, smp, n * sizeof(int32_t));
819 return subframe_count_exact(s, sub, 0);
820 }
821
822 7460 min_order = s->options.min_prediction_order;
823 7460 max_order = s->options.max_prediction_order;
824 7460 omethod = s->options.prediction_order_method;
825
826 /* FIXED */
827 7460 sub->type = FLAC_SUBFRAME_FIXED;
828
1/2
✓ Branch 0 taken 7460 times.
✗ Branch 1 not taken.
7460 if (s->options.lpc_type == FF_LPC_TYPE_NONE ||
829
3/4
✓ Branch 0 taken 6586 times.
✓ Branch 1 taken 874 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6586 times.
7460 s->options.lpc_type == FF_LPC_TYPE_FIXED || n <= max_order) {
830 uint64_t bits[MAX_FIXED_ORDER+1];
831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 874 times.
874 if (max_order > MAX_FIXED_ORDER)
832 max_order = MAX_FIXED_ORDER;
833 874 opt_order = 0;
834 874 bits[0] = UINT32_MAX;
835
2/2
✓ Branch 0 taken 4188 times.
✓ Branch 1 taken 874 times.
5062 for (i = min_order; i <= max_order; i++) {
836 4188 encode_residual_fixed(res, smp, n, i);
837 4188 bits[i] = find_subframe_rice_params(s, sub, i);
838
2/2
✓ Branch 0 taken 2592 times.
✓ Branch 1 taken 1596 times.
4188 if (bits[i] < bits[opt_order])
839 2592 opt_order = i;
840 }
841 874 sub->order = opt_order;
842 874 sub->type_code = sub->type | sub->order;
843
2/2
✓ Branch 0 taken 364 times.
✓ Branch 1 taken 510 times.
874 if (sub->order != max_order) {
844 364 encode_residual_fixed(res, smp, n, sub->order);
845 364 find_subframe_rice_params(s, sub, sub->order);
846 }
847 874 return subframe_count_exact(s, sub, sub->order);
848 }
849
850 /* LPC */
851 6586 sub->type = FLAC_SUBFRAME_LPC;
852 6586 opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, smp, n, min_order, max_order,
853 s->options.lpc_coeff_precision, coefs, shift, s->options.lpc_type,
854 s->options.lpc_passes, omethod,
855 MIN_LPC_SHIFT, MAX_LPC_SHIFT, 0);
856
857
3/4
✓ Branch 0 taken 6586 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6350 times.
✓ Branch 3 taken 236 times.
6586 if (omethod == ORDER_METHOD_2LEVEL ||
858
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6350 times.
6350 omethod == ORDER_METHOD_4LEVEL ||
859 236 omethod == ORDER_METHOD_8LEVEL) {
860 236 int levels = 1 << omethod;
861 uint64_t bits[1 << ORDER_METHOD_8LEVEL];
862 236 int order = -1;
863 236 int opt_index = levels-1;
864 236 opt_order = max_order-1;
865 236 bits[opt_index] = UINT32_MAX;
866
2/2
✓ Branch 0 taken 944 times.
✓ Branch 1 taken 236 times.
1180 for (i = levels-1; i >= 0; i--) {
867 944 int last_order = order;
868 944 order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
869 944 order = av_clip(order, min_order - 1, max_order - 1);
870
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 944 times.
944 if (order == last_order)
871 continue;
872
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 944 times.
944 if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(order) <= 32) {
873 s->flac_dsp.lpc16_encode(res, smp, n, order+1, coefs[order],
874 shift[order]);
875 } else {
876 944 s->flac_dsp.lpc32_encode(res, smp, n, order+1, coefs[order],
877 shift[order]);
878 }
879 944 bits[i] = find_subframe_rice_params(s, sub, order+1);
880
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 886 times.
944 if (bits[i] < bits[opt_index]) {
881 58 opt_index = i;
882 58 opt_order = order;
883 }
884 }
885 236 opt_order++;
886
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6350 times.
6350 } else if (omethod == ORDER_METHOD_SEARCH) {
887 // brute-force optimal order search
888 uint64_t bits[MAX_LPC_ORDER];
889 opt_order = 0;
890 bits[0] = UINT32_MAX;
891 for (i = min_order-1; i < max_order; i++) {
892 if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(i) <= 32) {
893 s->flac_dsp.lpc16_encode(res, smp, n, i+1, coefs[i], shift[i]);
894 } else {
895 s->flac_dsp.lpc32_encode(res, smp, n, i+1, coefs[i], shift[i]);
896 }
897 bits[i] = find_subframe_rice_params(s, sub, i+1);
898 if (bits[i] < bits[opt_order])
899 opt_order = i;
900 }
901 opt_order++;
902
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6350 times.
6350 } else if (omethod == ORDER_METHOD_LOG) {
903 uint64_t bits[MAX_LPC_ORDER];
904 int step;
905
906 opt_order = min_order - 1 + (max_order-min_order)/3;
907 memset(bits, -1, sizeof(bits));
908
909 for (step = 16; step; step >>= 1) {
910 int last = opt_order;
911 for (i = last-step; i <= last+step; i += step) {
912 if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
913 continue;
914 if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(i) <= 32) {
915 s->flac_dsp.lpc32_encode(res, smp, n, i+1, coefs[i], shift[i]);
916 } else {
917 s->flac_dsp.lpc16_encode(res, smp, n, i+1, coefs[i], shift[i]);
918 }
919 bits[i] = find_subframe_rice_params(s, sub, i+1);
920 if (bits[i] < bits[opt_order])
921 opt_order = i;
922 }
923 }
924 opt_order++;
925 }
926
927
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6580 times.
6586 if (s->options.multi_dim_quant) {
928 6 int allsteps = 1;
929 int i, step, improved;
930 6 int64_t best_score = INT64_MAX;
931 int32_t qmax;
932
933 6 qmax = (1 << (s->options.lpc_coeff_precision - 1)) - 1;
934
935
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 6 times.
53 for (i=0; i<opt_order; i++)
936 47 allsteps *= 3;
937
938 do {
939 14 improved = 0;
940
2/2
✓ Branch 0 taken 78732 times.
✓ Branch 1 taken 14 times.
78746 for (step = 0; step < allsteps; step++) {
941 78732 int tmp = step;
942 int32_t lpc_try[MAX_LPC_ORDER];
943 78732 int64_t score = 0;
944 78732 int diffsum = 0;
945
946
2/2
✓ Branch 0 taken 623295 times.
✓ Branch 1 taken 78732 times.
702027 for (i=0; i<opt_order; i++) {
947 623295 int diff = ((tmp + 1) % 3) - 1;
948 623295 lpc_try[i] = av_clip(coefs[opt_order - 1][i] + diff, -qmax, qmax);
949 623295 tmp /= 3;
950 623295 diffsum += !!diff;
951 }
952
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78732 times.
78732 if (diffsum >8)
953 continue;
954
955
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78732 times.
78732 if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(opt_order - 1) <= 32) {
956 s->flac_dsp.lpc16_encode(res, smp, n, opt_order, lpc_try, shift[opt_order-1]);
957 } else {
958 78732 s->flac_dsp.lpc32_encode(res, smp, n, opt_order, lpc_try, shift[opt_order-1]);
959 }
960 78732 score = find_subframe_rice_params(s, sub, opt_order);
961
2/2
✓ Branch 0 taken 467 times.
✓ Branch 1 taken 78265 times.
78732 if (score < best_score) {
962 467 best_score = score;
963 467 memcpy(coefs[opt_order-1], lpc_try, sizeof(*coefs));
964 467 improved=1;
965 }
966 }
967
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6 times.
14 } while(improved);
968 }
969
970 6586 sub->order = opt_order;
971 6586 sub->type_code = sub->type | (sub->order-1);
972 6586 sub->shift = shift[sub->order-1];
973
2/2
✓ Branch 0 taken 15653 times.
✓ Branch 1 taken 6586 times.
22239 for (i = 0; i < sub->order; i++)
974 15653 sub->coefs[i] = coefs[sub->order-1][i];
975
976
2/2
✓ Branch 0 taken 5233 times.
✓ Branch 1 taken 1353 times.
6586 if (s->bps_code * 4 + s->options.lpc_coeff_precision + av_log2(opt_order) <= 32) {
977 5233 s->flac_dsp.lpc16_encode(res, smp, n, sub->order, sub->coefs, sub->shift);
978 } else {
979 1353 s->flac_dsp.lpc32_encode(res, smp, n, sub->order, sub->coefs, sub->shift);
980 }
981
982 6586 find_subframe_rice_params(s, sub, sub->order);
983
984 6586 return subframe_count_exact(s, sub, sub->order);
985 }
986
987
988 4483 static int count_frame_header(FlacEncodeContext *s)
989 {
990 uint8_t av_unused tmp;
991 int count;
992
993 /*
994 <14> Sync code
995 <1> Reserved
996 <1> Blocking strategy
997 <4> Block size in inter-channel samples
998 <4> Sample rate
999 <4> Channel assignment
1000 <3> Sample size in bits
1001 <1> Reserved
1002 */
1003 4483 count = 32;
1004
1005 /* coded frame number */
1006
4/4
✓ Branch 0 taken 3127 times.
✓ Branch 1 taken 1356 times.
✓ Branch 2 taken 1356 times.
✓ Branch 3 taken 1356 times.
5839 PUT_UTF8(s->frame_count, tmp, count += 8;)
1007
1008 /* explicit block size */
1009
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4483 times.
4483 if (s->frame.bs_code[0] == 6)
1010 count += 8;
1011
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 4470 times.
4483 else if (s->frame.bs_code[0] == 7)
1012 13 count += 16;
1013
1014 /* explicit sample rate */
1015
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4483 times.
4483 count += ((s->sr_code[0] == 12) + (s->sr_code[0] > 12) * 2) * 8;
1016
1017 /* frame header CRC-8 */
1018 4483 count += 8;
1019
1020 4483 return count;
1021 }
1022
1023
1024 4483 static int encode_frame(FlacEncodeContext *s)
1025 {
1026 int ch;
1027 uint64_t count;
1028
1029 4483 count = count_frame_header(s);
1030
1031
2/2
✓ Branch 0 taken 13088 times.
✓ Branch 1 taken 4483 times.
17571 for (ch = 0; ch < s->channels; ch++)
1032 13088 count += encode_residual_ch(s, ch);
1033
1034 4483 count += (8 - (count & 7)) & 7; // byte alignment
1035 4483 count += 16; // CRC-16
1036
1037 4483 count >>= 3;
1038
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4483 times.
4483 if (count > INT_MAX)
1039 return AVERROR_BUG;
1040 4483 return count;
1041 }
1042
1043
1044 4483 static void remove_wasted_bits(FlacEncodeContext *s)
1045 {
1046 int ch, i;
1047
1048
2/2
✓ Branch 0 taken 13088 times.
✓ Branch 1 taken 4483 times.
17571 for (ch = 0; ch < s->channels; ch++) {
1049 13088 FlacSubframe *sub = &s->frame.subframes[ch];
1050 13088 int32_t v = 0;
1051
1052
2/2
✓ Branch 0 taken 69203100 times.
✓ Branch 1 taken 10828 times.
69213928 for (i = 0; i < s->frame.blocksize; i++) {
1053 69203100 v |= sub->samples[i];
1054
2/2
✓ Branch 0 taken 2260 times.
✓ Branch 1 taken 69200840 times.
69203100 if (v & 1)
1055 2260 break;
1056 }
1057
1058
4/4
✓ Branch 0 taken 7460 times.
✓ Branch 1 taken 5628 times.
✓ Branch 2 taken 5200 times.
✓ Branch 3 taken 2260 times.
13088 if (v && !(v & 1)) {
1059 5200 v = ff_ctz(v);
1060
1061
2/2
✓ Branch 0 taken 34028800 times.
✓ Branch 1 taken 5200 times.
34034000 for (i = 0; i < s->frame.blocksize; i++)
1062 34028800 sub->samples[i] >>= v;
1063
1064 5200 sub->wasted = v;
1065 5200 sub->obits -= v;
1066
1067 /* for 24-bit, check if removing wasted bits makes the range better
1068 suited for using RICE instead of RICE2 for entropy coding */
1069
1/2
✓ Branch 0 taken 5200 times.
✗ Branch 1 not taken.
5200 if (sub->obits <= 17)
1070 5200 sub->rc.coding_mode = CODING_MODE_RICE;
1071 }
1072 }
1073 4483 }
1074
1075
1076 2476 static int estimate_stereo_mode(const int32_t *left_ch, const int32_t *right_ch, int n,
1077 int max_rice_param)
1078 {
1079 int i, best;
1080 int32_t lt, rt;
1081 uint64_t sum[4];
1082 uint64_t score[4];
1083 int k;
1084
1085 /* calculate sum of 2nd order residual for each channel */
1086 2476 sum[0] = sum[1] = sum[2] = sum[3] = 0;
1087
2/2
✓ Branch 0 taken 14282347 times.
✓ Branch 1 taken 2476 times.
14284823 for (i = 2; i < n; i++) {
1088 14282347 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
1089 14282347 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
1090
2/2
✓ Branch 0 taken 11855013 times.
✓ Branch 1 taken 2427334 times.
14282347 sum[2] += FFABS((lt + rt) >> 1);
1091
2/2
✓ Branch 0 taken 11990898 times.
✓ Branch 1 taken 2291449 times.
14282347 sum[3] += FFABS(lt - rt);
1092
2/2
✓ Branch 0 taken 11861581 times.
✓ Branch 1 taken 2420766 times.
14282347 sum[0] += FFABS(lt);
1093
2/2
✓ Branch 0 taken 11861479 times.
✓ Branch 1 taken 2420868 times.
14282347 sum[1] += FFABS(rt);
1094 }
1095 /* estimate bit counts */
1096
2/2
✓ Branch 0 taken 9904 times.
✓ Branch 1 taken 2476 times.
12380 for (i = 0; i < 4; i++) {
1097 9904 k = find_optimal_param(2 * sum[i], n, max_rice_param);
1098 9904 sum[i] = rice_encode_count( 2 * sum[i], n, k);
1099 }
1100
1101 /* calculate score for each mode */
1102 2476 score[0] = sum[0] + sum[1];
1103 2476 score[1] = sum[0] + sum[3];
1104 2476 score[2] = sum[1] + sum[3];
1105 2476 score[3] = sum[2] + sum[3];
1106
1107 /* return mode with lowest score */
1108 2476 best = 0;
1109
2/2
✓ Branch 0 taken 7428 times.
✓ Branch 1 taken 2476 times.
9904 for (i = 1; i < 4; i++)
1110
2/2
✓ Branch 0 taken 1749 times.
✓ Branch 1 taken 5679 times.
7428 if (score[i] < score[best])
1111 1749 best = i;
1112
1113 2476 return best;
1114 }
1115
1116
1117 /**
1118 * Perform stereo channel decorrelation.
1119 */
1120 4483 static void channel_decorrelation(FlacEncodeContext *s)
1121 {
1122 FlacFrame *frame;
1123 int32_t *left, *right;
1124 int i, n;
1125
1126 4483 frame = &s->frame;
1127 4483 n = frame->blocksize;
1128 4483 left = frame->subframes[0].samples;
1129 4483 right = frame->subframes[1].samples;
1130
1131
2/2
✓ Branch 0 taken 1643 times.
✓ Branch 1 taken 2840 times.
4483 if (s->channels != 2) {
1132 1643 frame->ch_mode = FLAC_CHMODE_INDEPENDENT;
1133 1643 return;
1134 }
1135
1136
2/2
✓ Branch 0 taken 2476 times.
✓ Branch 1 taken 364 times.
2840 if (s->options.ch_mode < 0) {
1137 2476 int max_rice_param = (1 << frame->subframes[0].rc.coding_mode) - 2;
1138 2476 frame->ch_mode = estimate_stereo_mode(left, right, n, max_rice_param);
1139 } else
1140 364 frame->ch_mode = s->options.ch_mode;
1141
1142 /* perform decorrelation and adjust bits-per-sample */
1143
2/2
✓ Branch 0 taken 1051 times.
✓ Branch 1 taken 1789 times.
2840 if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1144 1051 return;
1145
2/2
✓ Branch 0 taken 254 times.
✓ Branch 1 taken 1535 times.
1789 if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
1146 int32_t tmp;
1147
2/2
✓ Branch 0 taken 1078297 times.
✓ Branch 1 taken 254 times.
1078551 for (i = 0; i < n; i++) {
1148 1078297 tmp = left[i];
1149 1078297 left[i] = (tmp + right[i]) >> 1;
1150 1078297 right[i] = tmp - right[i];
1151 }
1152 254 frame->subframes[1].obits++;
1153
2/2
✓ Branch 0 taken 1304 times.
✓ Branch 1 taken 231 times.
1535 } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
1154
2/2
✓ Branch 0 taken 6512822 times.
✓ Branch 1 taken 1304 times.
6514126 for (i = 0; i < n; i++)
1155 6512822 right[i] = left[i] - right[i];
1156 1304 frame->subframes[1].obits++;
1157 } else {
1158
2/2
✓ Branch 0 taken 787590 times.
✓ Branch 1 taken 231 times.
787821 for (i = 0; i < n; i++)
1159 787590 left[i] -= right[i];
1160 231 frame->subframes[0].obits++;
1161 }
1162 }
1163
1164
1165 4483 static void write_utf8(PutBitContext *pb, uint32_t val)
1166 {
1167 uint8_t tmp;
1168
4/4
✓ Branch 0 taken 3127 times.
✓ Branch 1 taken 1356 times.
✓ Branch 5 taken 1356 times.
✓ Branch 6 taken 1356 times.
5839 PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
1169 4483 }
1170
1171
1172 4483 static void write_frame_header(FlacEncodeContext *s)
1173 {
1174 FlacFrame *frame;
1175 int crc;
1176
1177 4483 frame = &s->frame;
1178
1179 4483 put_bits(&s->pb, 16, 0xFFF8);
1180 4483 put_bits(&s->pb, 4, frame->bs_code[0]);
1181 4483 put_bits(&s->pb, 4, s->sr_code[0]);
1182
1183
2/2
✓ Branch 0 taken 2694 times.
✓ Branch 1 taken 1789 times.
4483 if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
1184 2694 put_bits(&s->pb, 4, s->channels-1);
1185 else
1186 1789 put_bits(&s->pb, 4, frame->ch_mode + FLAC_MAX_CHANNELS - 1);
1187
1188 4483 put_bits(&s->pb, 3, s->bps_code);
1189 4483 put_bits(&s->pb, 1, 0);
1190 4483 write_utf8(&s->pb, s->frame_count);
1191
1192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4483 times.
4483 if (frame->bs_code[0] == 6)
1193 put_bits(&s->pb, 8, frame->bs_code[1]);
1194
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 4470 times.
4483 else if (frame->bs_code[0] == 7)
1195 13 put_bits(&s->pb, 16, frame->bs_code[1]);
1196
1197
2/2
✓ Branch 0 taken 918 times.
✓ Branch 1 taken 3565 times.
4483 if (s->sr_code[0] == 12)
1198 918 put_bits(&s->pb, 8, s->sr_code[1]);
1199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3565 times.
3565 else if (s->sr_code[0] > 12)
1200 put_bits(&s->pb, 16, s->sr_code[1]);
1201
1202 4483 flush_put_bits(&s->pb);
1203 4483 crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf,
1204 4483 put_bytes_output(&s->pb));
1205 4483 put_bits(&s->pb, 8, crc);
1206 4483 }
1207
1208
1209 4483 static void write_subframes(FlacEncodeContext *s)
1210 {
1211 int ch;
1212
1213
2/2
✓ Branch 0 taken 13088 times.
✓ Branch 1 taken 4483 times.
17571 for (ch = 0; ch < s->channels; ch++) {
1214 13088 FlacSubframe *sub = &s->frame.subframes[ch];
1215 int i, p, porder, psize;
1216 int32_t *part_end;
1217 13088 int32_t *res = sub->residual;
1218 13088 int32_t *frame_end = &sub->residual[s->frame.blocksize];
1219
1220 /* subframe header */
1221 13088 put_bits(&s->pb, 1, 0);
1222 13088 put_bits(&s->pb, 6, sub->type_code);
1223 13088 put_bits(&s->pb, 1, !!sub->wasted);
1224
2/2
✓ Branch 0 taken 5200 times.
✓ Branch 1 taken 7888 times.
13088 if (sub->wasted)
1225 5200 put_bits(&s->pb, sub->wasted, 1);
1226
1227 /* subframe */
1228
2/2
✓ Branch 0 taken 5628 times.
✓ Branch 1 taken 7460 times.
13088 if (sub->type == FLAC_SUBFRAME_CONSTANT) {
1229 5628 put_sbits(&s->pb, sub->obits, res[0]);
1230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7460 times.
7460 } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
1231 while (res < frame_end)
1232 put_sbits(&s->pb, sub->obits, *res++);
1233 } else {
1234 /* warm-up samples */
1235
2/2
✓ Branch 0 taken 18245 times.
✓ Branch 1 taken 7460 times.
25705 for (i = 0; i < sub->order; i++)
1236 18245 put_sbits(&s->pb, sub->obits, *res++);
1237
1238 /* LPC coefficients */
1239
2/2
✓ Branch 0 taken 6586 times.
✓ Branch 1 taken 874 times.
7460 if (sub->type == FLAC_SUBFRAME_LPC) {
1240 6586 int cbits = s->options.lpc_coeff_precision;
1241 6586 put_bits( &s->pb, 4, cbits-1);
1242 6586 put_sbits(&s->pb, 5, sub->shift);
1243
2/2
✓ Branch 0 taken 15653 times.
✓ Branch 1 taken 6586 times.
22239 for (i = 0; i < sub->order; i++)
1244 15653 put_sbits(&s->pb, cbits, sub->coefs[i]);
1245 }
1246
1247 /* rice-encoded block */
1248 7460 put_bits(&s->pb, 2, sub->rc.coding_mode - 4);
1249
1250 /* partition order */
1251 7460 porder = sub->rc.porder;
1252 7460 psize = s->frame.blocksize >> porder;
1253 7460 put_bits(&s->pb, 4, porder);
1254
1255 /* residual */
1256 7460 part_end = &sub->residual[psize];
1257
2/2
✓ Branch 0 taken 98660 times.
✓ Branch 1 taken 7460 times.
106120 for (p = 0; p < 1 << porder; p++) {
1258 98660 int k = sub->rc.params[p];
1259 98660 put_bits(&s->pb, sub->rc.coding_mode, k);
1260
2/2
✓ Branch 0 taken 44768181 times.
✓ Branch 1 taken 98660 times.
44866841 while (res < part_end)
1261 44768181 set_sr_golomb_flac(&s->pb, *res++, k, INT32_MAX, 0);
1262 98660 part_end = FFMIN(frame_end, part_end + psize);
1263 }
1264 }
1265 }
1266 4483 }
1267
1268
1269 4483 static void write_frame_footer(FlacEncodeContext *s)
1270 {
1271 int crc;
1272 4483 flush_put_bits(&s->pb);
1273 4483 crc = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, s->pb.buf,
1274 4483 put_bytes_output(&s->pb)));
1275 4483 put_bits(&s->pb, 16, crc);
1276 4483 flush_put_bits(&s->pb);
1277 4483 }
1278
1279
1280 4483 static int write_frame(FlacEncodeContext *s, AVPacket *avpkt)
1281 {
1282 4483 init_put_bits(&s->pb, avpkt->data, avpkt->size);
1283 4483 write_frame_header(s);
1284 4483 write_subframes(s);
1285 4483 write_frame_footer(s);
1286 4483 return put_bytes_output(&s->pb);
1287 }
1288
1289
1290 4483 static int update_md5_sum(FlacEncodeContext *s, const void *samples)
1291 {
1292 const uint8_t *buf;
1293 4483 int buf_size = s->frame.blocksize * s->channels *
1294 4483 ((s->avctx->bits_per_raw_sample + 7) / 8);
1295
1296
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 4365 times.
4483 if (s->avctx->bits_per_raw_sample > 16 || HAVE_BIGENDIAN) {
1297 118 av_fast_malloc(&s->md5_buffer, &s->md5_buffer_size, buf_size);
1298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 118 times.
118 if (!s->md5_buffer)
1299 return AVERROR(ENOMEM);
1300 }
1301
1302
2/2
✓ Branch 0 taken 4365 times.
✓ Branch 1 taken 118 times.
4483 if (s->avctx->bits_per_raw_sample <= 16) {
1303 4365 buf = (const uint8_t *)samples;
1304 #if HAVE_BIGENDIAN
1305 s->bdsp.bswap16_buf((uint16_t *) s->md5_buffer,
1306 (const uint16_t *) samples, buf_size / 2);
1307 buf = s->md5_buffer;
1308 #endif
1309 } else {
1310 int i;
1311 118 const int32_t *samples0 = samples;
1312 118 uint8_t *tmp = s->md5_buffer;
1313
1314
2/2
✓ Branch 0 taken 3840000 times.
✓ Branch 1 taken 118 times.
3840118 for (i = 0; i < s->frame.blocksize * s->channels; i++) {
1315 3840000 int32_t v = samples0[i] >> 8;
1316 3840000 AV_WL24(tmp + 3*i, v);
1317 }
1318 118 buf = s->md5_buffer;
1319 }
1320 4483 av_md5_update(s->md5ctx, buf, buf_size);
1321
1322 4483 return 0;
1323 }
1324
1325
1326 4515 static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1327 const AVFrame *frame, int *got_packet_ptr)
1328 {
1329 FlacEncodeContext *s;
1330 int frame_bytes, out_bytes, ret;
1331
1332 4515 s = avctx->priv_data;
1333
1334 /* when the last block is reached, update the header in extradata */
1335
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 4483 times.
4515 if (!frame) {
1336 32 s->max_framesize = s->max_encoded_framesize;
1337 32 av_md5_final(s->md5ctx, s->md5sum);
1338 32 write_streaminfo(s, avctx->extradata);
1339
1340
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 if (!s->flushed) {
1341 16 uint8_t *side_data = av_packet_new_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
1342 16 avctx->extradata_size);
1343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (!side_data)
1344 return AVERROR(ENOMEM);
1345 16 memcpy(side_data, avctx->extradata, avctx->extradata_size);
1346
1347 16 avpkt->pts = s->next_pts;
1348
1349 16 *got_packet_ptr = 1;
1350 16 s->flushed = 1;
1351 }
1352
1353 32 return 0;
1354 }
1355
1356 /* change max_framesize for small final frame */
1357
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4467 times.
4483 if (frame->nb_samples < s->frame.blocksize) {
1358 16 s->max_framesize = ff_flac_get_max_frame_size(frame->nb_samples,
1359 s->channels,
1360 avctx->bits_per_raw_sample);
1361 }
1362
1363 4483 init_frame(s, frame->nb_samples);
1364
1365 4483 copy_samples(s, frame->data[0]);
1366
1367 4483 channel_decorrelation(s);
1368
1369 4483 remove_wasted_bits(s);
1370
1371 4483 frame_bytes = encode_frame(s);
1372
1373 /* Fall back on verbatim mode if the compressed frame is larger than it
1374 would be if encoded uncompressed. */
1375
2/4
✓ Branch 0 taken 4483 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4483 times.
4483 if (frame_bytes < 0 || frame_bytes > s->max_framesize) {
1376 s->frame.verbatim_only = 1;
1377 frame_bytes = encode_frame(s);
1378 if (frame_bytes < 0) {
1379 av_log(avctx, AV_LOG_ERROR, "Bad frame count\n");
1380 return frame_bytes;
1381 }
1382 }
1383
1384
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4483 times.
4483 if ((ret = ff_get_encode_buffer(avctx, avpkt, frame_bytes, 0)) < 0)
1385 return ret;
1386
1387 4483 out_bytes = write_frame(s, avpkt);
1388
1389 4483 s->frame_count++;
1390 4483 s->sample_count += frame->nb_samples;
1391
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4483 times.
4483 if ((ret = update_md5_sum(s, frame->data[0])) < 0) {
1392 av_log(avctx, AV_LOG_ERROR, "Error updating MD5 checksum\n");
1393 return ret;
1394 }
1395
2/2
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 4271 times.
4483 if (out_bytes > s->max_encoded_framesize)
1396 212 s->max_encoded_framesize = out_bytes;
1397
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 4436 times.
4483 if (out_bytes < s->min_framesize)
1398 47 s->min_framesize = out_bytes;
1399
1400 4483 avpkt->pts = frame->pts;
1401 4483 avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
1402
1403 4483 s->next_pts = avpkt->pts + avpkt->duration;
1404
1405 4483 av_shrink_packet(avpkt, out_bytes);
1406
1407 4483 *got_packet_ptr = 1;
1408 4483 return 0;
1409 }
1410
1411
1412 32 static av_cold int flac_encode_close(AVCodecContext *avctx)
1413 {
1414 32 FlacEncodeContext *s = avctx->priv_data;
1415
1416 32 av_freep(&s->md5ctx);
1417 32 av_freep(&s->md5_buffer);
1418 32 ff_lpc_end(&s->lpc_ctx);
1419 32 return 0;
1420 }
1421
1422 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1423 static const AVOption options[] = {
1424 { "lpc_coeff_precision", "LPC coefficient precision", offsetof(FlacEncodeContext, options.lpc_coeff_precision), AV_OPT_TYPE_INT, {.i64 = 15 }, 0, MAX_LPC_PRECISION, FLAGS },
1425 { "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, "lpc_type" },
1426 { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_NONE }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1427 { "fixed", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_FIXED }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1428 { "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_LEVINSON }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1429 { "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_CHOLESKY }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
1430 { "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 },
1431 { "min_partition_order", NULL, offsetof(FlacEncodeContext, options.min_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
1432 { "max_partition_order", NULL, offsetof(FlacEncodeContext, options.max_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
1433 { "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, "predm" },
1434 { "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_EST }, INT_MIN, INT_MAX, FLAGS, "predm" },
1435 { "2level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_2LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1436 { "4level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_4LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1437 { "8level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_8LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
1438 { "search", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_SEARCH }, INT_MIN, INT_MAX, FLAGS, "predm" },
1439 { "log", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_LOG }, INT_MIN, INT_MAX, FLAGS, "predm" },
1440 { "ch_mode", "Stereo decorrelation mode", offsetof(FlacEncodeContext, options.ch_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, FLAC_CHMODE_MID_SIDE, FLAGS, "ch_mode" },
1441 { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1442 { "indep", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_INDEPENDENT }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1443 { "left_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_LEFT_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1444 { "right_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_RIGHT_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1445 { "mid_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_MID_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
1446 { "exact_rice_parameters", "Calculate rice parameters exactly", offsetof(FlacEncodeContext, options.exact_rice_parameters), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1447 { "multi_dim_quant", "Multi-dimensional quantization", offsetof(FlacEncodeContext, options.multi_dim_quant), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1448 { "min_prediction_order", NULL, offsetof(FlacEncodeContext, options.min_prediction_order), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, MAX_LPC_ORDER, FLAGS },
1449 { "max_prediction_order", NULL, offsetof(FlacEncodeContext, options.max_prediction_order), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, MAX_LPC_ORDER, FLAGS },
1450
1451 { NULL },
1452 };
1453
1454 static const AVClass flac_encoder_class = {
1455 .class_name = "FLAC encoder",
1456 .item_name = av_default_item_name,
1457 .option = options,
1458 .version = LIBAVUTIL_VERSION_INT,
1459 };
1460
1461 const FFCodec ff_flac_encoder = {
1462 .p.name = "flac",
1463 .p.long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
1464 .p.type = AVMEDIA_TYPE_AUDIO,
1465 .p.id = AV_CODEC_ID_FLAC,
1466 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1467 AV_CODEC_CAP_SMALL_LAST_FRAME,
1468 .priv_data_size = sizeof(FlacEncodeContext),
1469 .init = flac_encode_init,
1470 FF_CODEC_ENCODE_CB(flac_encode_frame),
1471 .close = flac_encode_close,
1472 .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
1473 AV_SAMPLE_FMT_S32,
1474 AV_SAMPLE_FMT_NONE },
1475 .p.priv_class = &flac_encoder_class,
1476 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1477 };
1478