FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/alacenc.c
Date: 2023-09-24 13:02:57
Exec Total Coverage
Lines: 290 310 93.5%
Functions: 14 14 100.0%
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 "codec_internal.h"
26 #include "encode.h"
27 #include "put_bits.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 int channels = s->avctx->ch_layout.nb_channels;
466 2353 const enum AlacRawDataBlockType *ch_elements = ff_alac_channel_elements[channels - 1];
467 2353 const uint8_t *ch_map = ff_alac_channel_layout_offsets[channels - 1];
468 int ch, element, sce, cpe;
469
470 2353 init_put_bits(pb, avpkt->data, avpkt->size);
471
472 2353 ch = element = sce = cpe = 0;
473
2/2
✓ Branch 0 taken 2353 times.
✓ Branch 1 taken 2353 times.
4706 while (ch < channels) {
474
1/2
✓ Branch 0 taken 2353 times.
✗ Branch 1 not taken.
2353 if (ch_elements[element] == TYPE_CPE) {
475 2353 write_element(s, TYPE_CPE, cpe, samples[ch_map[ch]],
476 2353 samples[ch_map[ch + 1]]);
477 2353 cpe++;
478 2353 ch += 2;
479 } else {
480 write_element(s, TYPE_SCE, sce, samples[ch_map[ch]], NULL);
481 sce++;
482 ch++;
483 }
484 2353 element++;
485 }
486
487 2353 put_bits(pb, 3, TYPE_END);
488 2353 flush_put_bits(pb);
489
490 2353 return put_bytes_output(pb);
491 }
492
493 18 static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps)
494 {
495
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 int header_bits = 23 + 32 * (frame_size < DEFAULT_FRAME_SIZE);
496 18 return FFALIGN(header_bits + bps * ch * frame_size + 3, 8) / 8;
497 }
498
499 9 static av_cold int alac_encode_close(AVCodecContext *avctx)
500 {
501 9 AlacEncodeContext *s = avctx->priv_data;
502 9 ff_lpc_end(&s->lpc_ctx);
503 9 return 0;
504 }
505
506 9 static av_cold int alac_encode_init(AVCodecContext *avctx)
507 {
508 9 AlacEncodeContext *s = avctx->priv_data;
509 int ret;
510 uint8_t *alac_extradata;
511
512 9 avctx->frame_size = s->frame_size = DEFAULT_FRAME_SIZE;
513
514
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
9 if (avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
515
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (avctx->bits_per_raw_sample != 24)
516 4 av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
517 4 avctx->bits_per_raw_sample = 24;
518 } else {
519 5 avctx->bits_per_raw_sample = 16;
520 5 s->extra_bits = 0;
521 }
522
523 // Set default compression level
524
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
9 if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
525 2 s->compression_level = 2;
526 else
527 7 s->compression_level = av_clip(avctx->compression_level, 0, 2);
528
529 // Initialize default Rice parameters
530 9 s->rc.history_mult = 40;
531 9 s->rc.initial_history = 10;
532 9 s->rc.k_modifier = 14;
533 9 s->rc.rice_modifier = 4;
534
535 9 s->max_coded_frame_size = get_max_frame_size(avctx->frame_size,
536 avctx->ch_layout.nb_channels,
537 avctx->bits_per_raw_sample);
538
539 9 avctx->extradata = av_mallocz(ALAC_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
540
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!avctx->extradata)
541 return AVERROR(ENOMEM);
542 9 avctx->extradata_size = ALAC_EXTRADATA_SIZE;
543
544 9 alac_extradata = avctx->extradata;
545 9 AV_WB32(alac_extradata, ALAC_EXTRADATA_SIZE);
546 9 AV_WB32(alac_extradata+4, MKBETAG('a','l','a','c'));
547 9 AV_WB32(alac_extradata+12, avctx->frame_size);
548 9 AV_WB8 (alac_extradata+17, avctx->bits_per_raw_sample);
549 9 AV_WB8 (alac_extradata+21, avctx->ch_layout.nb_channels);
550 9 AV_WB32(alac_extradata+24, s->max_coded_frame_size);
551 9 AV_WB32(alac_extradata+28,
552 avctx->sample_rate * avctx->ch_layout.nb_channels * avctx->bits_per_raw_sample); // average bitrate
553 9 AV_WB32(alac_extradata+32, avctx->sample_rate);
554
555 // Set relevant extradata fields
556
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
9 if (s->compression_level > 0) {
557 7 AV_WB8(alac_extradata+18, s->rc.history_mult);
558 7 AV_WB8(alac_extradata+19, s->rc.initial_history);
559 7 AV_WB8(alac_extradata+20, s->rc.k_modifier);
560 }
561
562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (s->max_prediction_order < s->min_prediction_order) {
563 av_log(avctx, AV_LOG_ERROR,
564 "invalid prediction orders: min=%d max=%d\n",
565 s->min_prediction_order, s->max_prediction_order);
566 return AVERROR(EINVAL);
567 }
568
569 9 s->avctx = avctx;
570
571
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if ((ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
572 s->max_prediction_order,
573 FF_LPC_TYPE_LEVINSON)) < 0) {
574 return ret;
575 }
576
577 9 return 0;
578 }
579
580 2353 static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
581 const AVFrame *frame, int *got_packet_ptr)
582 {
583 2353 AlacEncodeContext *s = avctx->priv_data;
584 int out_bytes, max_frame_size, ret;
585
586 2353 s->frame_size = frame->nb_samples;
587
588
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2344 times.
2353 if (frame->nb_samples < DEFAULT_FRAME_SIZE)
589 9 max_frame_size = get_max_frame_size(s->frame_size, avctx->ch_layout.nb_channels,
590 avctx->bits_per_raw_sample);
591 else
592 2344 max_frame_size = s->max_coded_frame_size;
593
594
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2353 times.
2353 if ((ret = ff_alloc_packet(avctx, avpkt, 4 * max_frame_size)) < 0)
595 return ret;
596
597 /* use verbatim mode for compression_level 0 */
598
2/2
✓ Branch 0 taken 1781 times.
✓ Branch 1 taken 572 times.
2353 if (s->compression_level) {
599 1781 s->verbatim = 0;
600 1781 s->extra_bits = avctx->bits_per_raw_sample - 16;
601 } else {
602 572 s->verbatim = 1;
603 572 s->extra_bits = 0;
604 }
605
606 2353 out_bytes = write_frame(s, avpkt, frame->extended_data);
607
608
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2353 times.
2353 if (out_bytes > max_frame_size) {
609 /* frame too large. use verbatim mode */
610 s->verbatim = 1;
611 s->extra_bits = 0;
612 out_bytes = write_frame(s, avpkt, frame->extended_data);
613 }
614
615 2353 avpkt->size = out_bytes;
616 2353 *got_packet_ptr = 1;
617 2353 return 0;
618 }
619
620 #if FF_API_OLD_CHANNEL_LAYOUT
621 static const uint64_t alac_channel_layouts[ALAC_MAX_CHANNELS + 1] = {
622 AV_CH_LAYOUT_MONO,
623 AV_CH_LAYOUT_STEREO,
624 AV_CH_LAYOUT_SURROUND,
625 AV_CH_LAYOUT_4POINT0,
626 AV_CH_LAYOUT_5POINT0_BACK,
627 AV_CH_LAYOUT_5POINT1_BACK,
628 AV_CH_LAYOUT_6POINT1_BACK,
629 AV_CH_LAYOUT_7POINT1_WIDE_BACK,
630 0
631 };
632 #endif
633
634
635 #define OFFSET(x) offsetof(AlacEncodeContext, x)
636 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
637 static const AVOption options[] = {
638 { "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 },
639 { "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 },
640
641 { NULL },
642 };
643
644 static const AVClass alacenc_class = {
645 .class_name = "alacenc",
646 .item_name = av_default_item_name,
647 .option = options,
648 .version = LIBAVUTIL_VERSION_INT,
649 };
650
651 const FFCodec ff_alac_encoder = {
652 .p.name = "alac",
653 CODEC_LONG_NAME("ALAC (Apple Lossless Audio Codec)"),
654 .p.type = AVMEDIA_TYPE_AUDIO,
655 .p.id = AV_CODEC_ID_ALAC,
656 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SMALL_LAST_FRAME |
657 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
658 .priv_data_size = sizeof(AlacEncodeContext),
659 .p.priv_class = &alacenc_class,
660 .init = alac_encode_init,
661 FF_CODEC_ENCODE_CB(alac_encode_frame),
662 .close = alac_encode_close,
663 CODEC_OLD_CHANNEL_LAYOUTS_ARRAY(alac_channel_layouts)
664 .p.ch_layouts = ff_alac_ch_layouts,
665 .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32P,
666 AV_SAMPLE_FMT_S16P,
667 AV_SAMPLE_FMT_NONE },
668 };
669