FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/alacenc.c
Date: 2021-09-23 20:34:37
Exec Total Coverage
Lines: 288 309 93.2%
Branches: 137 154 89.0%

Line Branch Exec Source
1 /*
2 * ALAC audio encoder
3 * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
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/opt.h"
23
24 #include "avcodec.h"
25 #include "encode.h"
26 #include "put_bits.h"
27 #include "internal.h"
28 #include "lpc.h"
29 #include "mathops.h"
30 #include "alac_data.h"
31
32 #define DEFAULT_FRAME_SIZE 4096
33 #define ALAC_EXTRADATA_SIZE 36
34 #define ALAC_FRAME_HEADER_SIZE 55
35 #define ALAC_FRAME_FOOTER_SIZE 3
36
37 #define ALAC_ESCAPE_CODE 0x1FF
38 #define ALAC_MAX_LPC_ORDER 30
39 #define DEFAULT_MAX_PRED_ORDER 6
40 #define DEFAULT_MIN_PRED_ORDER 4
41 #define ALAC_MAX_LPC_PRECISION 9
42 #define ALAC_MIN_LPC_SHIFT 0
43 #define ALAC_MAX_LPC_SHIFT 9
44
45 #define ALAC_CHMODE_LEFT_RIGHT 0
46 #define ALAC_CHMODE_LEFT_SIDE 1
47 #define ALAC_CHMODE_RIGHT_SIDE 2
48 #define ALAC_CHMODE_MID_SIDE 3
49
50 typedef struct RiceContext {
51 int history_mult;
52 int initial_history;
53 int k_modifier;
54 int rice_modifier;
55 } RiceContext;
56
57 typedef struct AlacLPCContext {
58 int lpc_order;
59 int lpc_coeff[ALAC_MAX_LPC_ORDER+1];
60 int lpc_quant;
61 } AlacLPCContext;
62
63 typedef struct AlacEncodeContext {
64 const AVClass *class;
65 AVCodecContext *avctx;
66 int frame_size; /**< current frame size */
67 int verbatim; /**< current frame verbatim mode flag */
68 int compression_level;
69 int min_prediction_order;
70 int max_prediction_order;
71 int max_coded_frame_size;
72 int write_sample_size;
73 int extra_bits;
74 int32_t sample_buf[2][DEFAULT_FRAME_SIZE];
75 int32_t predictor_buf[2][DEFAULT_FRAME_SIZE];
76 int interlacing_shift;
77 int interlacing_leftweight;
78 PutBitContext pbctx;
79 RiceContext rc;
80 AlacLPCContext lpc[2];
81 LPCContext lpc_ctx;
82 } AlacEncodeContext;
83
84
85 1781 static void init_sample_buffers(AlacEncodeContext *s, int channels,
86 const uint8_t *samples[2])
87 {
88 int ch, i;
89 1781 int shift = av_get_bytes_per_sample(s->avctx->sample_fmt) * 8 -
90 1781 s->avctx->bits_per_raw_sample;
91
92 #define COPY_SAMPLES(type) do { \
93 for (ch = 0; ch < channels; ch++) { \
94 int32_t *bptr = s->sample_buf[ch]; \
95 const type *sptr = (const type *)samples[ch]; \
96 for (i = 0; i < s->frame_size; i++) \
97 bptr[i] = sptr[i] >> shift; \
98 } \
99 } while (0)
100
101
2/2
✓ Branch 0 taken 1407 times.
✓ Branch 1 taken 374 times.
1781 if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S32P)
102
4/4
✓ Branch 0 taken 11520000 times.
✓ Branch 1 taken 2814 times.
✓ Branch 2 taken 2814 times.
✓ Branch 3 taken 1407 times.
11524221 COPY_SAMPLES(int32_t);
103 else
104
4/4
✓ Branch 0 taken 3042900 times.
✓ Branch 1 taken 748 times.
✓ Branch 2 taken 748 times.
✓ Branch 3 taken 374 times.
3044022 COPY_SAMPLES(int16_t);
105 1781 }
106
107 14430194 static void encode_scalar(AlacEncodeContext *s, int x,
108 int k, int write_sample_size)
109 {
110 int divisor, q, r;
111
112 14430194 k = FFMIN(k, s->rc.k_modifier);
113 14430194 divisor = (1<<k) - 1;
114 14430194 q = x / divisor;
115 14430194 r = x % divisor;
116
117
2/2
✓ Branch 0 taken 25847 times.
✓ Branch 1 taken 14404347 times.
14430194 if (q > 8) {
118 // write escape code and sample value directly
119 25847 put_bits(&s->pbctx, 9, ALAC_ESCAPE_CODE);
120 25847 put_bits(&s->pbctx, write_sample_size, x);
121 } else {
122
2/2
✓ Branch 0 taken 7865003 times.
✓ Branch 1 taken 6539344 times.
14404347 if (q)
123 7865003 put_bits(&s->pbctx, q, (1<<q) - 1);
124 14404347 put_bits(&s->pbctx, 1, 0);
125
126
2/2
✓ Branch 0 taken 14401767 times.
✓ Branch 1 taken 2580 times.
14404347 if (k != 1) {
127
2/2
✓ Branch 0 taken 12874329 times.
✓ Branch 1 taken 1527438 times.
14401767 if (r > 0)
128 12874329 put_bits(&s->pbctx, k, r+1);
129 else
130 1527438 put_bits(&s->pbctx, k-1, 0);
131 }
132 }
133 14430194 }
134
135 2353 static void write_element_header(AlacEncodeContext *s,
136 enum AlacRawDataBlockType element,
137 int instance)
138 {
139 2353 int encode_fs = 0;
140
141
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2344 times.
2353 if (s->frame_size < DEFAULT_FRAME_SIZE)
142 9 encode_fs = 1;
143
144 2353 put_bits(&s->pbctx, 3, element); // element type
145 2353 put_bits(&s->pbctx, 4, instance); // element instance
146 2353 put_bits(&s->pbctx, 12, 0); // unused header bits
147 2353 put_bits(&s->pbctx, 1, encode_fs); // Sample count is in the header
148 2353 put_bits(&s->pbctx, 2, s->extra_bits >> 3); // Extra bytes (for 24-bit)
149 2353 put_bits(&s->pbctx, 1, s->verbatim); // Audio block is verbatim
150
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2344 times.
2353 if (encode_fs)
151 9 put_bits32(&s->pbctx, s->frame_size); // No. of samples in the frame
152 2353 }
153
154 3562 static void calc_predictor_params(AlacEncodeContext *s, int ch)
155 {
156 int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
157 int shift[MAX_LPC_ORDER];
158 int opt_order;
159
160
2/2
✓ Branch 0 taken 1274 times.
✓ Branch 1 taken 2288 times.
3562 if (s->compression_level == 1) {
161 1274 s->lpc[ch].lpc_order = 6;
162 1274 s->lpc[ch].lpc_quant = 6;
163 1274 s->lpc[ch].lpc_coeff[0] = 160;
164 1274 s->lpc[ch].lpc_coeff[1] = -190;
165 1274 s->lpc[ch].lpc_coeff[2] = 170;
166 1274 s->lpc[ch].lpc_coeff[3] = -130;
167 1274 s->lpc[ch].lpc_coeff[4] = 80;
168 1274 s->lpc[ch].lpc_coeff[5] = -25;
169 } else {
170 2288 opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, s->sample_buf[ch],
171 s->frame_size,
172 s->min_prediction_order,
173 s->max_prediction_order,
174 ALAC_MAX_LPC_PRECISION, coefs, shift,
175 FF_LPC_TYPE_LEVINSON, 0,
176 ORDER_METHOD_EST, ALAC_MIN_LPC_SHIFT,
177 ALAC_MAX_LPC_SHIFT, 1);
178
179 2288 s->lpc[ch].lpc_order = opt_order;
180 2288 s->lpc[ch].lpc_quant = shift[opt_order-1];
181 2288 memcpy(s->lpc[ch].lpc_coeff, coefs[opt_order-1], opt_order*sizeof(int));
182 }
183 3562 }
184
185 1781 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
186 {
187 int i, best;
188 int32_t lt, rt;
189 uint64_t sum[4];
190 uint64_t score[4];
191
192 /* calculate sum of 2nd order residual for each channel */
193 1781 sum[0] = sum[1] = sum[2] = sum[3] = 0;
194
2/2
✓ Branch 0 taken 7277888 times.
✓ Branch 1 taken 1781 times.
7279669 for (i = 2; i < n; i++) {
195 7277888 lt = left_ch[i] - 2 * left_ch[i - 1] + left_ch[i - 2];
196 7277888 rt = right_ch[i] - 2 * right_ch[i - 1] + right_ch[i - 2];
197
2/2
✓ Branch 0 taken 3674830 times.
✓ Branch 1 taken 3603058 times.
7277888 sum[2] += FFABS((lt + rt) >> 1);
198
2/2
✓ Branch 0 taken 3737436 times.
✓ Branch 1 taken 3540452 times.
7277888 sum[3] += FFABS(lt - rt);
199
2/2
✓ Branch 0 taken 3698084 times.
✓ Branch 1 taken 3579804 times.
7277888 sum[0] += FFABS(lt);
200
2/2
✓ Branch 0 taken 3681625 times.
✓ Branch 1 taken 3596263 times.
7277888 sum[1] += FFABS(rt);
201 }
202
203 /* calculate score for each mode */
204 1781 score[0] = sum[0] + sum[1];
205 1781 score[1] = sum[0] + sum[3];
206 1781 score[2] = sum[1] + sum[3];
207 1781 score[3] = sum[2] + sum[3];
208
209 /* return mode with lowest score */
210 1781 best = 0;
211
2/2
✓ Branch 0 taken 5343 times.
✓ Branch 1 taken 1781 times.
7124 for (i = 1; i < 4; i++) {
212
2/2
✓ Branch 0 taken 597 times.
✓ Branch 1 taken 4746 times.
5343 if (score[i] < score[best])
213 597 best = i;
214 }
215 1781 return best;
216 }
217
218 1781 static void alac_stereo_decorrelation(AlacEncodeContext *s)
219 {
220 1781 int32_t *left = s->sample_buf[0], *right = s->sample_buf[1];
221 1781 int i, mode, n = s->frame_size;
222 int32_t tmp;
223
224 1781 mode = estimate_stereo_mode(left, right, n);
225
226
4/4
✓ Branch 0 taken 1425 times.
✓ Branch 1 taken 145 times.
✓ Branch 2 taken 90 times.
✓ Branch 3 taken 121 times.
1781 switch (mode) {
227 1425 case ALAC_CHMODE_LEFT_RIGHT:
228 1425 s->interlacing_leftweight = 0;
229 1425 s->interlacing_shift = 0;
230 1425 break;
231 145 case ALAC_CHMODE_LEFT_SIDE:
232
2/2
✓ Branch 0 taken 592280 times.
✓ Branch 1 taken 145 times.
592425 for (i = 0; i < n; i++)
233 592280 right[i] = left[i] - right[i];
234 145 s->interlacing_leftweight = 1;
235 145 s->interlacing_shift = 0;
236 145 break;
237 90 case ALAC_CHMODE_RIGHT_SIDE:
238
2/2
✓ Branch 0 taken 368640 times.
✓ Branch 1 taken 90 times.
368730 for (i = 0; i < n; i++) {
239 368640 tmp = right[i];
240 368640 right[i] = left[i] - right[i];
241 368640 left[i] = tmp + (right[i] >> 31);
242 }
243 90 s->interlacing_leftweight = 1;
244 90 s->interlacing_shift = 31;
245 90 break;
246 121 default:
247
2/2
✓ Branch 0 taken 486802 times.
✓ Branch 1 taken 121 times.
486923 for (i = 0; i < n; i++) {
248 486802 tmp = left[i];
249 486802 left[i] = (tmp + right[i]) >> 1;
250 486802 right[i] = tmp - right[i];
251 }
252 121 s->interlacing_leftweight = 1;
253 121 s->interlacing_shift = 1;
254 121 break;
255 }
256 1781 }
257
258 3562 static void alac_linear_predictor(AlacEncodeContext *s, int ch)
259 {
260 int i;
261 3562 AlacLPCContext lpc = s->lpc[ch];
262 3562 int32_t *residual = s->predictor_buf[ch];
263
264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3562 times.
3562 if (lpc.lpc_order == 31) {
265 residual[0] = s->sample_buf[ch][0];
266
267 for (i = 1; i < s->frame_size; i++) {
268 residual[i] = s->sample_buf[ch][i ] -
269 s->sample_buf[ch][i - 1];
270 }
271
272 return;
273 }
274
275 // generalised linear predictor
276
277
1/2
✓ Branch 0 taken 3562 times.
✗ Branch 1 not taken.
3562 if (lpc.lpc_order > 0) {
278 3562 int32_t *samples = s->sample_buf[ch];
279
280 // generate warm-up samples
281 3562 residual[0] = samples[0];
282
2/2
✓ Branch 0 taken 31197 times.
✓ Branch 1 taken 3562 times.
34759 for (i = 1; i <= lpc.lpc_order; i++)
283 31197 residual[i] = sign_extend(samples[i] - samples[i-1], s->write_sample_size);
284
285 // perform lpc on remaining samples
286
2/2
✓ Branch 0 taken 14528141 times.
✓ Branch 1 taken 3562 times.
14531703 for (i = lpc.lpc_order + 1; i < s->frame_size; i++) {
287 14528141 int sum = 1 << (lpc.lpc_quant - 1), res_val, j;
288
289
2/2
✓ Branch 0 taken 127115964 times.
✓ Branch 1 taken 14528141 times.
141644105 for (j = 0; j < lpc.lpc_order; j++) {
290 127115964 sum += (samples[lpc.lpc_order-j] - samples[0]) *
291 127115964 lpc.lpc_coeff[j];
292 }
293
294 14528141 sum >>= lpc.lpc_quant;
295 14528141 sum += samples[0];
296 14528141 residual[i] = sign_extend(samples[lpc.lpc_order+1] - sum,
297 14528141 s->write_sample_size);
298 14528141 res_val = residual[i];
299
300
2/2
✓ Branch 0 taken 13747876 times.
✓ Branch 1 taken 780265 times.
14528141 if (res_val) {
301 13747876 int index = lpc.lpc_order - 1;
302 13747876 int neg = (res_val < 0);
303
304
8/8
✓ Branch 0 taken 69809376 times.
✓ Branch 1 taken 4402062 times.
✓ Branch 2 taken 34289798 times.
✓ Branch 3 taken 35519578 times.
✓ Branch 4 taken 28132311 times.
✓ Branch 5 taken 6157487 times.
✓ Branch 6 taken 32331251 times.
✓ Branch 7 taken 3188327 times.
74211438 while (index >= 0 && (neg ? (res_val < 0) : (res_val > 0))) {
305 60463562 int val = samples[0] - samples[lpc.lpc_order - index];
306
4/4
✓ Branch 0 taken 59881539 times.
✓ Branch 1 taken 582023 times.
✓ Branch 2 taken 29776850 times.
✓ Branch 3 taken 30104689 times.
60463562 int sign = (val ? FFSIGN(val) : 0);
307
308
2/2
✓ Branch 0 taken 28132311 times.
✓ Branch 1 taken 32331251 times.
60463562 if (neg)
309 28132311 sign *= -1;
310
311 60463562 lpc.lpc_coeff[index] -= sign;
312 60463562 val *= sign;
313 60463562 res_val -= (val >> lpc.lpc_quant) * (lpc.lpc_order - index);
314 60463562 index--;
315 }
316 }
317 14528141 samples++;
318 }
319 }
320 }
321
322 3562 static void alac_entropy_coder(AlacEncodeContext *s, int ch)
323 {
324 3562 unsigned int history = s->rc.initial_history;
325 3562 int sign_modifier = 0, i, k;
326 3562 int32_t *samples = s->predictor_buf[ch];
327
328
2/2
✓ Branch 0 taken 14430063 times.
✓ Branch 1 taken 3562 times.
14433625 for (i = 0; i < s->frame_size;) {
329 int x;
330
331 14430063 k = av_log2((history >> 9) + 3);
332
333 14430063 x = -2 * (*samples) -1;
334 14430063 x ^= x >> 31;
335
336 14430063 samples++;
337 14430063 i++;
338
339 14430063 encode_scalar(s, x - sign_modifier, k, s->write_sample_size);
340
341 14430063 history += x * s->rc.history_mult -
342 14430063 ((history * s->rc.history_mult) >> 9);
343
344 14430063 sign_modifier = 0;
345
2/2
✓ Branch 0 taken 11363 times.
✓ Branch 1 taken 14418700 times.
14430063 if (x > 0xFFFF)
346 11363 history = 0xFFFF;
347
348
3/4
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 14429932 times.
✓ Branch 2 taken 131 times.
✗ Branch 3 not taken.
14430063 if (history < 128 && i < s->frame_size) {
349 131 unsigned int block_size = 0;
350
351 131 k = 7 - av_log2(history) + ((history + 16) >> 6);
352
353
4/4
✓ Branch 0 taken 132869 times.
✓ Branch 1 taken 99 times.
✓ Branch 2 taken 132837 times.
✓ Branch 3 taken 32 times.
132968 while (*samples == 0 && i < s->frame_size) {
354 132837 samples++;
355 132837 i++;
356 132837 block_size++;
357 }
358 131 encode_scalar(s, block_size, k, 16);
359 131 sign_modifier = (block_size <= 0xFFFF);
360 131 history = 0;
361 }
362
363 }
364 3562 }
365
366 2353 static void write_element(AlacEncodeContext *s,
367 enum AlacRawDataBlockType element, int instance,
368 const uint8_t *samples0, const uint8_t *samples1)
369 {
370 2353 const uint8_t *samples[2] = { samples0, samples1 };
371 int i, j, channels;
372 2353 int prediction_type = 0;
373 2353 PutBitContext *pb = &s->pbctx;
374
375
1/2
✓ Branch 0 taken 2353 times.
✗ Branch 1 not taken.
2353 channels = element == TYPE_CPE ? 2 : 1;
376
377
2/2
✓ Branch 0 taken 572 times.
✓ Branch 1 taken 1781 times.
2353 if (s->verbatim) {
378 572 write_element_header(s, element, instance);
379 /* samples are channel-interleaved in verbatim mode */
380
2/2
✓ Branch 0 taken 469 times.
✓ Branch 1 taken 103 times.
572 if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
381 469 int shift = 32 - s->avctx->bits_per_raw_sample;
382 469 const int32_t *samples_s32[2] = { (const int32_t *)samples0,
383 (const int32_t *)samples1 };
384
2/2
✓ Branch 0 taken 1920000 times.
✓ Branch 1 taken 469 times.
1920469 for (i = 0; i < s->frame_size; i++)
385
2/2
✓ Branch 0 taken 3840000 times.
✓ Branch 1 taken 1920000 times.
5760000 for (j = 0; j < channels; j++)
386 3840000 put_sbits(pb, s->avctx->bits_per_raw_sample,
387 3840000 samples_s32[j][i] >> shift);
388 } else {
389 103 const int16_t *samples_s16[2] = { (const int16_t *)samples0,
390 (const int16_t *)samples1 };
391
2/2
✓ Branch 0 taken 418950 times.
✓ Branch 1 taken 103 times.
419053 for (i = 0; i < s->frame_size; i++)
392
2/2
✓ Branch 0 taken 837900 times.
✓ Branch 1 taken 418950 times.
1256850 for (j = 0; j < channels; j++)
393 837900 put_sbits(pb, s->avctx->bits_per_raw_sample,
394 837900 samples_s16[j][i]);
395 }
396 } else {
397 1781 s->write_sample_size = s->avctx->bits_per_raw_sample - s->extra_bits +
398 1781 channels - 1;
399
400 1781 init_sample_buffers(s, channels, samples);
401 1781 write_element_header(s, element, instance);
402
403 // extract extra bits if needed
404
2/2
✓ Branch 0 taken 1407 times.
✓ Branch 1 taken 374 times.
1781 if (s->extra_bits) {
405 1407 uint32_t mask = (1 << s->extra_bits) - 1;
406
2/2
✓ Branch 0 taken 2814 times.
✓ Branch 1 taken 1407 times.
4221 for (j = 0; j < channels; j++) {
407 2814 int32_t *extra = s->predictor_buf[j];
408 2814 int32_t *smp = s->sample_buf[j];
409
2/2
✓ Branch 0 taken 11520000 times.
✓ Branch 1 taken 2814 times.
11522814 for (i = 0; i < s->frame_size; i++) {
410 11520000 extra[i] = smp[i] & mask;
411 11520000 smp[i] >>= s->extra_bits;
412 }
413 }
414 }
415
416
1/2
✓ Branch 0 taken 1781 times.
✗ Branch 1 not taken.
1781 if (channels == 2)
417 1781 alac_stereo_decorrelation(s);
418 else
419 s->interlacing_shift = s->interlacing_leftweight = 0;
420 1781 put_bits(pb, 8, s->interlacing_shift);
421 1781 put_bits(pb, 8, s->interlacing_leftweight);
422
423
2/2
✓ Branch 0 taken 3562 times.
✓ Branch 1 taken 1781 times.
5343 for (i = 0; i < channels; i++) {
424 3562 calc_predictor_params(s, i);
425
426 3562 put_bits(pb, 4, prediction_type);
427 3562 put_bits(pb, 4, s->lpc[i].lpc_quant);
428
429 3562 put_bits(pb, 3, s->rc.rice_modifier);
430 3562 put_bits(pb, 5, s->lpc[i].lpc_order);
431 // predictor coeff. table
432
2/2
✓ Branch 0 taken 31197 times.
✓ Branch 1 taken 3562 times.
34759 for (j = 0; j < s->lpc[i].lpc_order; j++)
433 31197 put_sbits(pb, 16, s->lpc[i].lpc_coeff[j]);
434 }
435
436 // write extra bits if needed
437
2/2
✓ Branch 0 taken 1407 times.
✓ Branch 1 taken 374 times.
1781 if (s->extra_bits) {
438
2/2
✓ Branch 0 taken 5760000 times.
✓ Branch 1 taken 1407 times.
5761407 for (i = 0; i < s->frame_size; i++) {
439
2/2
✓ Branch 0 taken 11520000 times.
✓ Branch 1 taken 5760000 times.
17280000 for (j = 0; j < channels; j++) {
440 11520000 put_bits(pb, s->extra_bits, s->predictor_buf[j][i]);
441 }
442 }
443 }
444
445 // apply lpc and entropy coding to audio samples
446
2/2
✓ Branch 0 taken 3562 times.
✓ Branch 1 taken 1781 times.
5343 for (i = 0; i < channels; i++) {
447 3562 alac_linear_predictor(s, i);
448
449 // TODO: determine when this will actually help. for now it's not used.
450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3562 times.
3562 if (prediction_type == 15) {
451 // 2nd pass 1st order filter
452 int32_t *residual = s->predictor_buf[i];
453 for (j = s->frame_size - 1; j > 0; j--)
454 residual[j] -= residual[j - 1];
455 }
456 3562 alac_entropy_coder(s, i);
457 }
458 }
459 2353 }
460
461 2353 static int write_frame(AlacEncodeContext *s, AVPacket *avpkt,
462 uint8_t * const *samples)
463 {
464 2353 PutBitContext *pb = &s->pbctx;
465 2353 const enum AlacRawDataBlockType *ch_elements = ff_alac_channel_elements[s->avctx->channels - 1];
466 2353 const uint8_t *ch_map = ff_alac_channel_layout_offsets[s->avctx->channels - 1];
467 int ch, element, sce, cpe;
468
469 2353 init_put_bits(pb, avpkt->data, avpkt->size);
470
471 2353 ch = element = sce = cpe = 0;
472
2/2
✓ Branch 0 taken 2353 times.
✓ Branch 1 taken 2353 times.
4706 while (ch < s->avctx->channels) {
473
1/2
✓ Branch 0 taken 2353 times.
✗ Branch 1 not taken.
2353 if (ch_elements[element] == TYPE_CPE) {
474 2353 write_element(s, TYPE_CPE, cpe, samples[ch_map[ch]],
475 2353 samples[ch_map[ch + 1]]);
476 2353 cpe++;
477 2353 ch += 2;
478 } else {
479 write_element(s, TYPE_SCE, sce, samples[ch_map[ch]], NULL);
480 sce++;
481 ch++;
482 }
483 2353 element++;
484 }
485
486 2353 put_bits(pb, 3, TYPE_END);
487 2353 flush_put_bits(pb);
488
489 2353 return put_bytes_output(pb);
490 }
491
492 18 static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps)
493 {
494
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 int header_bits = 23 + 32 * (frame_size < DEFAULT_FRAME_SIZE);
495 18 return FFALIGN(header_bits + bps * ch * frame_size + 3, 8) / 8;
496 }
497
498 9 static av_cold int alac_encode_close(AVCodecContext *avctx)
499 {
500 9 AlacEncodeContext *s = avctx->priv_data;
501 9 ff_lpc_end(&s->lpc_ctx);
502 9 return 0;
503 }
504
505 9 static av_cold int alac_encode_init(AVCodecContext *avctx)
506 {
507 9 AlacEncodeContext *s = avctx->priv_data;
508 int ret;
509 uint8_t *alac_extradata;
510
511 9 avctx->frame_size = s->frame_size = DEFAULT_FRAME_SIZE;
512
513
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
9 if (avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (avctx->bits_per_raw_sample != 24)
515 av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
516 4 avctx->bits_per_raw_sample = 24;
517 } else {
518 5 avctx->bits_per_raw_sample = 16;
519 5 s->extra_bits = 0;
520 }
521
522 // Set default compression level
523
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
9 if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
524 2 s->compression_level = 2;
525 else
526 7 s->compression_level = av_clip(avctx->compression_level, 0, 2);
527
528 // Initialize default Rice parameters
529 9 s->rc.history_mult = 40;
530 9 s->rc.initial_history = 10;
531 9 s->rc.k_modifier = 14;
532 9 s->rc.rice_modifier = 4;
533
534 9 s->max_coded_frame_size = get_max_frame_size(avctx->frame_size,
535 avctx->channels,
536 avctx->bits_per_raw_sample);
537
538 9 avctx->extradata = av_mallocz(ALAC_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!avctx->extradata)
540 return AVERROR(ENOMEM);
541 9 avctx->extradata_size = ALAC_EXTRADATA_SIZE;
542
543 9 alac_extradata = avctx->extradata;
544 9 AV_WB32(alac_extradata, ALAC_EXTRADATA_SIZE);
545 9 AV_WB32(alac_extradata+4, MKBETAG('a','l','a','c'));
546 9 AV_WB32(alac_extradata+12, avctx->frame_size);
547 9 AV_WB8 (alac_extradata+17, avctx->bits_per_raw_sample);
548 9 AV_WB8 (alac_extradata+21, avctx->channels);
549 9 AV_WB32(alac_extradata+24, s->max_coded_frame_size);
550 9 AV_WB32(alac_extradata+28,
551 avctx->sample_rate * avctx->channels * avctx->bits_per_raw_sample); // average bitrate
552 9 AV_WB32(alac_extradata+32, avctx->sample_rate);
553
554 // Set relevant extradata fields
555
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
9 if (s->compression_level > 0) {
556 7 AV_WB8(alac_extradata+18, s->rc.history_mult);
557 7 AV_WB8(alac_extradata+19, s->rc.initial_history);
558 7 AV_WB8(alac_extradata+20, s->rc.k_modifier);
559 }
560
561
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (s->max_prediction_order < s->min_prediction_order) {
562 av_log(avctx, AV_LOG_ERROR,
563 "invalid prediction orders: min=%d max=%d\n",
564 s->min_prediction_order, s->max_prediction_order);
565 return AVERROR(EINVAL);
566 }
567
568 9 s->avctx = avctx;
569
570
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if ((ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
571 s->max_prediction_order,
572 FF_LPC_TYPE_LEVINSON)) < 0) {
573 return ret;
574 }
575
576 9 return 0;
577 }
578
579 2353 static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
580 const AVFrame *frame, int *got_packet_ptr)
581 {
582 2353 AlacEncodeContext *s = avctx->priv_data;
583 int out_bytes, max_frame_size, ret;
584
585 2353 s->frame_size = frame->nb_samples;
586
587
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2344 times.
2353 if (frame->nb_samples < DEFAULT_FRAME_SIZE)
588 9 max_frame_size = get_max_frame_size(s->frame_size, avctx->channels,
589 avctx->bits_per_raw_sample);
590 else
591 2344 max_frame_size = s->max_coded_frame_size;
592
593
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2353 times.
2353 if ((ret = ff_alloc_packet(avctx, avpkt, 4 * max_frame_size)) < 0)
594 return ret;
595
596 /* use verbatim mode for compression_level 0 */
597
2/2
✓ Branch 0 taken 1781 times.
✓ Branch 1 taken 572 times.
2353 if (s->compression_level) {
598 1781 s->verbatim = 0;
599 1781 s->extra_bits = avctx->bits_per_raw_sample - 16;
600 } else {
601 572 s->verbatim = 1;
602 572 s->extra_bits = 0;
603 }
604
605 2353 out_bytes = write_frame(s, avpkt, frame->extended_data);
606
607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2353 times.
2353 if (out_bytes > max_frame_size) {
608 /* frame too large. use verbatim mode */
609 s->verbatim = 1;
610 s->extra_bits = 0;
611 out_bytes = write_frame(s, avpkt, frame->extended_data);
612 }
613
614 2353 avpkt->size = out_bytes;
615 2353 *got_packet_ptr = 1;
616 2353 return 0;
617 }
618
619 #define OFFSET(x) offsetof(AlacEncodeContext, x)
620 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
621 static const AVOption options[] = {
622 { "min_prediction_order", NULL, OFFSET(min_prediction_order), AV_OPT_TYPE_INT, { .i64 = DEFAULT_MIN_PRED_ORDER }, MIN_LPC_ORDER, ALAC_MAX_LPC_ORDER, AE },
623 { "max_prediction_order", NULL, OFFSET(max_prediction_order), AV_OPT_TYPE_INT, { .i64 = DEFAULT_MAX_PRED_ORDER }, MIN_LPC_ORDER, ALAC_MAX_LPC_ORDER, AE },
624
625 { NULL },
626 };
627
628 static const AVClass alacenc_class = {
629 .class_name = "alacenc",
630 .item_name = av_default_item_name,
631 .option = options,
632 .version = LIBAVUTIL_VERSION_INT,
633 };
634
635 const AVCodec ff_alac_encoder = {
636 .name = "alac",
637 .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
638 .type = AVMEDIA_TYPE_AUDIO,
639 .id = AV_CODEC_ID_ALAC,
640 .priv_data_size = sizeof(AlacEncodeContext),
641 .priv_class = &alacenc_class,
642 .init = alac_encode_init,
643 .encode2 = alac_encode_frame,
644 .close = alac_encode_close,
645 .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
646 .channel_layouts = ff_alac_channel_layouts,
647 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32P,
648 AV_SAMPLE_FMT_S16P,
649 AV_SAMPLE_FMT_NONE },
650 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
651 };
652