FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/flacenc.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 599 724 82.7%
Branches: 293 411 71.3%

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