FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/alac.c
Date: 2022-07-05 19:52:29
Exec Total Coverage
Lines: 227 290 78.3%
Branches: 142 203 70.0%

Line Branch Exec Source
1 /*
2 * ALAC (Apple Lossless Audio Codec) decoder
3 * Copyright (c) 2005 David Hammerton
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 /**
23 * @file
24 * ALAC (Apple Lossless Audio Codec) decoder
25 * @author 2005 David Hammerton
26 * @see http://crazney.net/programs/itunes/alac.html
27 *
28 * Note: This decoder expects a 36-byte QuickTime atom to be
29 * passed through the extradata[_size] fields. This atom is tacked onto
30 * the end of an 'alac' stsd atom and has the following format:
31 *
32 * 32 bits atom size
33 * 32 bits tag ("alac")
34 * 32 bits tag version (0)
35 * 32 bits samples per frame (used when not set explicitly in the frames)
36 * 8 bits compatible version (0)
37 * 8 bits sample size
38 * 8 bits history mult (40)
39 * 8 bits initial history (10)
40 * 8 bits rice param limit (14)
41 * 8 bits channels
42 * 16 bits maxRun (255)
43 * 32 bits max coded frame size (0 means unknown)
44 * 32 bits average bitrate (0 means unknown)
45 * 32 bits samplerate
46 */
47
48 #include <inttypes.h>
49
50 #include "libavutil/channel_layout.h"
51 #include "libavutil/opt.h"
52 #include "avcodec.h"
53 #include "get_bits.h"
54 #include "bytestream.h"
55 #include "codec_internal.h"
56 #include "thread.h"
57 #include "unary.h"
58 #include "mathops.h"
59 #include "alac_data.h"
60 #include "alacdsp.h"
61
62 #define ALAC_EXTRADATA_SIZE 36
63
64 typedef struct ALACContext {
65 AVClass *class;
66 AVCodecContext *avctx;
67 GetBitContext gb;
68 int channels;
69
70 int32_t *predict_error_buffer[2];
71 int32_t *output_samples_buffer[2];
72 int32_t *extra_bits_buffer[2];
73
74 uint32_t max_samples_per_frame;
75 uint8_t sample_size;
76 uint8_t rice_history_mult;
77 uint8_t rice_initial_history;
78 uint8_t rice_limit;
79 int sample_rate;
80
81 int extra_bits; /**< number of extra bits beyond 16-bit */
82 int nb_samples; /**< number of samples in the current frame */
83
84 int direct_output;
85 int extra_bit_bug;
86
87 ALACDSPContext dsp;
88 } ALACContext;
89
90 17627718 static inline unsigned int decode_scalar(GetBitContext *gb, int k, int bps)
91 {
92 17627718 unsigned int x = get_unary_0_9(gb);
93
94
2/2
✓ Branch 0 taken 27674 times.
✓ Branch 1 taken 17600044 times.
17627718 if (x > 8) { /* RICE THRESHOLD */
95 /* use alternative encoding */
96 27674 x = get_bits_long(gb, bps);
97
2/2
✓ Branch 0 taken 17597191 times.
✓ Branch 1 taken 2853 times.
17600044 } else if (k != 1) {
98 17597191 int extrabits = show_bits(gb, k);
99
100 /* multiply x by 2^k - 1, as part of their strange algorithm */
101 17597191 x = (x << k) - x;
102
103
2/2
✓ Branch 0 taken 15974312 times.
✓ Branch 1 taken 1622879 times.
17597191 if (extrabits > 1) {
104 15974312 x += extrabits - 1;
105 15974312 skip_bits(gb, k);
106 } else
107 1622879 skip_bits(gb, k - 1);
108 }
109 17627718 return x;
110 }
111
112 4364 static int rice_decompress(ALACContext *alac, int32_t *output_buffer,
113 int nb_samples, int bps, int rice_history_mult)
114 {
115 int i;
116 4364 unsigned int history = alac->rice_initial_history;
117 4364 int sign_modifier = 0;
118
119
2/2
✓ Branch 0 taken 17627499 times.
✓ Branch 1 taken 4364 times.
17631863 for (i = 0; i < nb_samples; i++) {
120 int k;
121 unsigned int x;
122
123
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17627499 times.
17627499 if(get_bits_left(&alac->gb) <= 0)
124 return AVERROR_INVALIDDATA;
125
126 /* calculate rice param and decode next value */
127 17627499 k = av_log2((history >> 9) + 3);
128 17627499 k = FFMIN(k, alac->rice_limit);
129 17627499 x = decode_scalar(&alac->gb, k, bps);
130 17627499 x += sign_modifier;
131 17627499 sign_modifier = 0;
132 17627499 output_buffer[i] = (x >> 1) ^ -(x & 1);
133
134 /* update the history */
135
2/2
✓ Branch 0 taken 11363 times.
✓ Branch 1 taken 17616136 times.
17627499 if (x > 0xffff)
136 11363 history = 0xffff;
137 else
138 17616136 history += x * rice_history_mult -
139 17616136 ((history * rice_history_mult) >> 9);
140
141 /* special case: there may be compressed blocks of 0 */
142
3/4
✓ Branch 0 taken 219 times.
✓ Branch 1 taken 17627280 times.
✓ Branch 2 taken 219 times.
✗ Branch 3 not taken.
17627499 if ((history < 128) && (i + 1 < nb_samples)) {
143 int block_size;
144
145 /* calculate rice param and decode block size */
146 219 k = 7 - av_log2(history) + ((history + 16) >> 6);
147 219 k = FFMIN(k, alac->rice_limit);
148 219 block_size = decode_scalar(&alac->gb, k, 16);
149
150
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 127 times.
219 if (block_size > 0) {
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
92 if (block_size >= nb_samples - i) {
152 av_log(alac->avctx, AV_LOG_ERROR,
153 "invalid zero block size of %d %d %d\n", block_size,
154 nb_samples, i);
155 block_size = nb_samples - i - 1;
156 }
157 92 memset(&output_buffer[i + 1], 0,
158 block_size * sizeof(*output_buffer));
159 92 i += block_size;
160 }
161
1/2
✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
219 if (block_size <= 0xffff)
162 219 sign_modifier = 1;
163 219 history = 0;
164 }
165 }
166 4364 return 0;
167 }
168
169 92489337 static inline int sign_only(int v)
170 {
171
4/4
✓ Branch 0 taken 90929340 times.
✓ Branch 1 taken 1559997 times.
✓ Branch 2 taken 44582190 times.
✓ Branch 3 taken 46347150 times.
92489337 return v ? FFSIGN(v) : 0;
172 }
173
174 4364 static void lpc_prediction(int32_t *error_buffer, uint32_t *buffer_out,
175 int nb_samples, int bps, int16_t *lpc_coefs,
176 int lpc_order, int lpc_quant)
177 {
178 int i;
179 4364 uint32_t *pred = buffer_out;
180
181 /* first sample always copies */
182 4364 *buffer_out = *error_buffer;
183
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4364 times.
4364 if (nb_samples <= 1)
185 return;
186
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4364 times.
4364 if (!lpc_order) {
188 memcpy(&buffer_out[1], &error_buffer[1],
189 (nb_samples - 1) * sizeof(*buffer_out));
190 return;
191 }
192
193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4364 times.
4364 if (lpc_order == 31) {
194 /* simple 1st-order prediction */
195 for (i = 1; i < nb_samples; i++) {
196 buffer_out[i] = sign_extend(buffer_out[i - 1] + error_buffer[i],
197 bps);
198 }
199 return;
200 }
201
202 /* read warm-up samples */
203
3/4
✓ Branch 0 taken 35020 times.
✓ Branch 1 taken 4364 times.
✓ Branch 2 taken 35020 times.
✗ Branch 3 not taken.
39384 for (i = 1; i <= lpc_order && i < nb_samples; i++)
204 35020 buffer_out[i] = sign_extend(buffer_out[i - 1] + error_buffer[i], bps);
205
206 /* NOTE: 4 and 8 are very common cases that could be optimized. */
207
208
2/2
✓ Branch 0 taken 17808442 times.
✓ Branch 1 taken 4364 times.
17812806 for (; i < nb_samples; i++) {
209 int j;
210 17808442 int val = 0;
211 17808442 unsigned error_val = error_buffer[i];
212 int error_sign;
213 17808442 int d = *pred++;
214
215 /* LPC prediction */
216
2/2
✓ Branch 0 taken 142750580 times.
✓ Branch 1 taken 17808442 times.
160559022 for (j = 0; j < lpc_order; j++)
217 142750580 val += (pred[j] - d) * lpc_coefs[j];
218 17808442 val = (val + (1LL << (lpc_quant - 1))) >> lpc_quant;
219 17808442 val += d + error_val;
220 17808442 buffer_out[i] = sign_extend(val, bps);
221
222 /* adapt LPC coefficients */
223 17808442 error_sign = sign_only(error_val);
224
2/2
✓ Branch 0 taken 16900905 times.
✓ Branch 1 taken 907537 times.
17808442 if (error_sign) {
225
4/4
✓ Branch 0 taken 84349336 times.
✓ Branch 1 taken 7232464 times.
✓ Branch 2 taken 74680895 times.
✓ Branch 3 taken 9668441 times.
91581800 for (j = 0; j < lpc_order && (int)(error_val * error_sign) > 0; j++) {
226 int sign;
227 74680895 val = d - pred[j];
228 74680895 sign = sign_only(val) * error_sign;
229 74680895 lpc_coefs[j] -= sign;
230 74680895 val *= (unsigned)sign;
231 74680895 error_val -= (val >> lpc_quant) * (j + 1U);
232 }
233 }
234 }
235 }
236
237 2757 static int decode_element(AVCodecContext *avctx, AVFrame *frame, int ch_index,
238 int channels)
239 {
240 2757 ALACContext *alac = avctx->priv_data;
241 int has_size, bps, is_compressed, decorr_shift, decorr_left_weight, ret;
242 uint32_t output_samples;
243 int i, ch;
244
245 2757 skip_bits(&alac->gb, 4); /* element instance tag */
246 2757 skip_bits(&alac->gb, 12); /* unused header bits */
247
248 /* the number of output samples is stored in the frame */
249 2757 has_size = get_bits1(&alac->gb);
250
251 2757 alac->extra_bits = get_bits(&alac->gb, 2) << 3;
252 2757 bps = alac->sample_size - alac->extra_bits + channels - 1;
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2757 times.
2757 if (bps > 32) {
254 avpriv_report_missing_feature(avctx, "bps %d", bps);
255 return AVERROR_PATCHWELCOME;
256 }
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2757 times.
2757 if (bps < 1)
258 return AVERROR_INVALIDDATA;
259
260 /* whether the frame is compressed */
261 2757 is_compressed = !get_bits1(&alac->gb);
262
263
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2745 times.
2757 if (has_size)
264 12 output_samples = get_bits_long(&alac->gb, 32);
265 else
266 2745 output_samples = alac->max_samples_per_frame;
267
2/4
✓ Branch 0 taken 2757 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2757 times.
2757 if (!output_samples || output_samples > alac->max_samples_per_frame) {
268 av_log(avctx, AV_LOG_ERROR, "invalid samples per frame: %"PRIu32"\n",
269 output_samples);
270 return AVERROR_INVALIDDATA;
271 }
272
2/2
✓ Branch 0 taken 2754 times.
✓ Branch 1 taken 3 times.
2757 if (!alac->nb_samples) {
273 /* get output buffer */
274 2754 frame->nb_samples = output_samples;
275
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2754 times.
2754 if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0)
276 return ret;
277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (output_samples != alac->nb_samples) {
278 av_log(avctx, AV_LOG_ERROR, "sample count mismatch: %"PRIu32" != %d\n",
279 output_samples, alac->nb_samples);
280 return AVERROR_INVALIDDATA;
281 }
282 2757 alac->nb_samples = output_samples;
283
2/2
✓ Branch 0 taken 1880 times.
✓ Branch 1 taken 877 times.
2757 if (alac->direct_output) {
284
2/2
✓ Branch 0 taken 3760 times.
✓ Branch 1 taken 1880 times.
5640 for (ch = 0; ch < channels; ch++)
285 3760 alac->output_samples_buffer[ch] = (int32_t *)frame->extended_data[ch_index + ch];
286 }
287
288
2/2
✓ Branch 0 taken 2183 times.
✓ Branch 1 taken 574 times.
2757 if (is_compressed) {
289 int16_t lpc_coefs[2][32];
290 int lpc_order[2];
291 int prediction_type[2];
292 int lpc_quant[2];
293 int rice_history_mult[2];
294
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2183 times.
2183 if (!alac->rice_limit) {
296 avpriv_request_sample(alac->avctx,
297 "Compression with rice limit 0");
298 return AVERROR(ENOSYS);
299 }
300
301 2183 decorr_shift = get_bits(&alac->gb, 8);
302 2183 decorr_left_weight = get_bits(&alac->gb, 8);
303
304
5/6
✓ Branch 0 taken 2181 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 720 times.
✓ Branch 3 taken 1461 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 720 times.
2183 if (channels == 2 && decorr_left_weight && decorr_shift > 31)
305 return AVERROR_INVALIDDATA;
306
307
2/2
✓ Branch 0 taken 4364 times.
✓ Branch 1 taken 2183 times.
6547 for (ch = 0; ch < channels; ch++) {
308 4364 prediction_type[ch] = get_bits(&alac->gb, 4);
309 4364 lpc_quant[ch] = get_bits(&alac->gb, 4);
310 4364 rice_history_mult[ch] = get_bits(&alac->gb, 3);
311 4364 lpc_order[ch] = get_bits(&alac->gb, 5);
312
313
2/4
✓ Branch 0 taken 4364 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4364 times.
4364 if (lpc_order[ch] >= alac->max_samples_per_frame || !lpc_quant[ch])
314 return AVERROR_INVALIDDATA;
315
316 /* read the predictor table */
317
2/2
✓ Branch 0 taken 35020 times.
✓ Branch 1 taken 4364 times.
39384 for (i = lpc_order[ch] - 1; i >= 0; i--)
318 35020 lpc_coefs[ch][i] = get_sbits(&alac->gb, 16);
319 }
320
321
2/2
✓ Branch 0 taken 1410 times.
✓ Branch 1 taken 773 times.
2183 if (alac->extra_bits) {
322
2/2
✓ Branch 0 taken 5772288 times.
✓ Branch 1 taken 1410 times.
5773698 for (i = 0; i < alac->nb_samples; i++) {
323
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5772288 times.
5772288 if(get_bits_left(&alac->gb) <= 0)
324 return AVERROR_INVALIDDATA;
325
2/2
✓ Branch 0 taken 11544576 times.
✓ Branch 1 taken 5772288 times.
17316864 for (ch = 0; ch < channels; ch++)
326 11544576 alac->extra_bits_buffer[ch][i] = get_bits(&alac->gb, alac->extra_bits);
327 }
328 }
329
2/2
✓ Branch 0 taken 4364 times.
✓ Branch 1 taken 2183 times.
6547 for (ch = 0; ch < channels; ch++) {
330 4364 int ret=rice_decompress(alac, alac->predict_error_buffer[ch],
331 alac->nb_samples, bps,
332 4364 rice_history_mult[ch] * alac->rice_history_mult / 4);
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4364 times.
4364 if(ret<0)
334 return ret;
335
336 /* adaptive FIR filter */
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4364 times.
4364 if (prediction_type[ch] == 15) {
338 /* Prediction type 15 runs the adaptive FIR twice.
339 * The first pass uses the special-case coef_num = 31, while
340 * the second pass uses the coefs from the bitstream.
341 *
342 * However, this prediction type is not currently used by the
343 * reference encoder.
344 */
345 lpc_prediction(alac->predict_error_buffer[ch],
346 alac->predict_error_buffer[ch],
347 alac->nb_samples, bps, NULL, 31, 0);
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4364 times.
4364 } else if (prediction_type[ch] > 0) {
349 av_log(avctx, AV_LOG_WARNING, "unknown prediction type: %i\n",
350 prediction_type[ch]);
351 }
352 4364 lpc_prediction(alac->predict_error_buffer[ch],
353 4364 alac->output_samples_buffer[ch], alac->nb_samples,
354 4364 bps, lpc_coefs[ch], lpc_order[ch], lpc_quant[ch]);
355 }
356 } else {
357 /* not compressed, easy case */
358
2/2
✓ Branch 0 taken 2347142 times.
✓ Branch 1 taken 574 times.
2347716 for (i = 0; i < alac->nb_samples; i++) {
359
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2347142 times.
2347142 if(get_bits_left(&alac->gb) <= 0)
360 return AVERROR_INVALIDDATA;
361
2/2
✓ Branch 0 taken 4694284 times.
✓ Branch 1 taken 2347142 times.
7041426 for (ch = 0; ch < channels; ch++) {
362 4694284 alac->output_samples_buffer[ch][i] =
363 4694284 get_sbits_long(&alac->gb, alac->sample_size);
364 }
365 }
366 574 alac->extra_bits = 0;
367 574 decorr_shift = 0;
368 574 decorr_left_weight = 0;
369 }
370
371
2/2
✓ Branch 0 taken 2755 times.
✓ Branch 1 taken 2 times.
2757 if (channels == 2) {
372
3/4
✓ Branch 0 taken 1410 times.
✓ Branch 1 taken 1345 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1410 times.
2755 if (alac->extra_bits && alac->extra_bit_bug) {
373 alac->dsp.append_extra_bits[1](alac->output_samples_buffer, alac->extra_bits_buffer,
374 alac->extra_bits, channels, alac->nb_samples);
375 }
376
377
2/2
✓ Branch 0 taken 720 times.
✓ Branch 1 taken 2035 times.
2755 if (decorr_left_weight) {
378 720 alac->dsp.decorrelate_stereo(alac->output_samples_buffer, alac->nb_samples,
379 decorr_shift, decorr_left_weight);
380 }
381
382
3/4
✓ Branch 0 taken 1410 times.
✓ Branch 1 taken 1345 times.
✓ Branch 2 taken 1410 times.
✗ Branch 3 not taken.
2755 if (alac->extra_bits && !alac->extra_bit_bug) {
383 1410 alac->dsp.append_extra_bits[1](alac->output_samples_buffer, alac->extra_bits_buffer,
384 alac->extra_bits, channels, alac->nb_samples);
385 }
386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } else if (alac->extra_bits) {
387 alac->dsp.append_extra_bits[0](alac->output_samples_buffer, alac->extra_bits_buffer,
388 alac->extra_bits, channels, alac->nb_samples);
389 }
390
391
2/4
✓ Branch 0 taken 877 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1880 times.
✗ Branch 3 not taken.
2757 switch(alac->sample_size) {
392 877 case 16: {
393
2/2
✓ Branch 0 taken 1752 times.
✓ Branch 1 taken 877 times.
2629 for (ch = 0; ch < channels; ch++) {
394 1752 int16_t *outbuffer = (int16_t *)frame->extended_data[ch_index + ch];
395
2/2
✓ Branch 0 taken 7149342 times.
✓ Branch 1 taken 1752 times.
7151094 for (i = 0; i < alac->nb_samples; i++)
396 7149342 *outbuffer++ = alac->output_samples_buffer[ch][i];
397 }}
398 877 break;
399 case 20: {
400 for (ch = 0; ch < channels; ch++) {
401 for (i = 0; i < alac->nb_samples; i++)
402 alac->output_samples_buffer[ch][i] *= 1U << 12;
403 }}
404 break;
405 1880 case 24: {
406
2/2
✓ Branch 0 taken 3760 times.
✓ Branch 1 taken 1880 times.
5640 for (ch = 0; ch < channels; ch++) {
407
2/2
✓ Branch 0 taken 15392768 times.
✓ Branch 1 taken 3760 times.
15396528 for (i = 0; i < alac->nb_samples; i++)
408 15392768 alac->output_samples_buffer[ch][i] *= 1U << 8;
409 }}
410 1880 break;
411 }
412
413 2757 return 0;
414 }
415
416 2754 static int alac_decode_frame(AVCodecContext *avctx, AVFrame *frame,
417 int *got_frame_ptr, AVPacket *avpkt)
418 {
419 2754 ALACContext *alac = avctx->priv_data;
420 enum AlacRawDataBlockType element;
421 int channels;
422 int ch, ret, got_end;
423
424
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2754 times.
2754 if ((ret = init_get_bits8(&alac->gb, avpkt->data, avpkt->size)) < 0)
425 return ret;
426
427 2754 got_end = 0;
428 2754 alac->nb_samples = 0;
429 2754 ch = 0;
430
1/2
✓ Branch 1 taken 5511 times.
✗ Branch 2 not taken.
5511 while (get_bits_left(&alac->gb) >= 3) {
431 5511 element = get_bits(&alac->gb, 3);
432
2/2
✓ Branch 0 taken 2754 times.
✓ Branch 1 taken 2757 times.
5511 if (element == TYPE_END) {
433 2754 got_end = 1;
434 2754 break;
435 }
436
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2757 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2757 if (element > TYPE_CPE && element != TYPE_LFE) {
437 avpriv_report_missing_feature(avctx, "Syntax element %d", element);
438 return AVERROR_PATCHWELCOME;
439 }
440
441
2/2
✓ Branch 0 taken 2755 times.
✓ Branch 1 taken 2 times.
2757 channels = (element == TYPE_CPE) ? 2 : 1;
442
1/2
✓ Branch 0 taken 2757 times.
✗ Branch 1 not taken.
2757 if (ch + channels > alac->channels ||
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2757 times.
2757 ff_alac_channel_layout_offsets[alac->channels - 1][ch] + channels > alac->channels) {
444 av_log(avctx, AV_LOG_ERROR, "invalid element channel count\n");
445 return AVERROR_INVALIDDATA;
446 }
447
448 2757 ret = decode_element(avctx, frame,
449 2757 ff_alac_channel_layout_offsets[alac->channels - 1][ch],
450 channels);
451
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2757 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
2757 if (ret < 0 && get_bits_left(&alac->gb))
452 return ret;
453
454 2757 ch += channels;
455 }
456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2754 times.
2754 if (!got_end) {
457 av_log(avctx, AV_LOG_ERROR, "no end tag found. incomplete packet.\n");
458 return AVERROR_INVALIDDATA;
459 }
460
461
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2754 times.
2754 if (avpkt->size * 8 - get_bits_count(&alac->gb) > 8) {
462 av_log(avctx, AV_LOG_ERROR, "Error : %d bits left\n",
463 avpkt->size * 8 - get_bits_count(&alac->gb));
464 }
465
466
2/4
✓ Branch 0 taken 2754 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2754 times.
✗ Branch 3 not taken.
2754 if (alac->channels == ch && alac->nb_samples)
467 2754 *got_frame_ptr = 1;
468 else
469 av_log(avctx, AV_LOG_WARNING, "Failed to decode all channels\n");
470
471 2754 return avpkt->size;
472 }
473
474 31 static av_cold int alac_decode_close(AVCodecContext *avctx)
475 {
476 31 ALACContext *alac = avctx->priv_data;
477
478 int ch;
479
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 31 times.
93 for (ch = 0; ch < FFMIN(alac->channels, 2); ch++) {
480 62 av_freep(&alac->predict_error_buffer[ch]);
481
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 16 times.
62 if (!alac->direct_output)
482 46 av_freep(&alac->output_samples_buffer[ch]);
483 62 av_freep(&alac->extra_bits_buffer[ch]);
484 }
485
486 31 return 0;
487 }
488
489 31 static int allocate_buffers(ALACContext *alac)
490 {
491 int ch;
492 31 unsigned buf_size = alac->max_samples_per_frame * sizeof(int32_t);
493 31 unsigned extra_buf_size = buf_size + AV_INPUT_BUFFER_PADDING_SIZE;
494
495
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 31 times.
93 for (ch = 0; ch < 2; ch++) {
496 62 alac->predict_error_buffer[ch] = NULL;
497 62 alac->output_samples_buffer[ch] = NULL;
498 62 alac->extra_bits_buffer[ch] = NULL;
499 }
500
501
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 31 times.
93 for (ch = 0; ch < FFMIN(alac->channels, 2); ch++) {
502
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
62 if (!(alac->predict_error_buffer[ch] = av_malloc(buf_size)))
503 return AVERROR(ENOMEM);
504
505 62 alac->direct_output = alac->sample_size > 16;
506
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 16 times.
62 if (!alac->direct_output) {
507
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
46 if (!(alac->output_samples_buffer[ch] = av_malloc(extra_buf_size)))
508 return AVERROR(ENOMEM);
509 }
510
511
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
62 if (!(alac->extra_bits_buffer[ch] = av_malloc(extra_buf_size)))
512 return AVERROR(ENOMEM);
513 }
514 31 return 0;
515 }
516
517 31 static int alac_set_info(ALACContext *alac)
518 {
519 GetByteContext gb;
520
521 31 bytestream2_init(&gb, alac->avctx->extradata,
522 31 alac->avctx->extradata_size);
523
524 31 bytestream2_skipu(&gb, 12); // size:4, alac:4, version:4
525
526 31 alac->max_samples_per_frame = bytestream2_get_be32u(&gb);
527
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if (!alac->max_samples_per_frame ||
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 alac->max_samples_per_frame > 4096 * 4096) {
529 av_log(alac->avctx, AV_LOG_ERROR,
530 "max samples per frame invalid: %"PRIu32"\n",
531 alac->max_samples_per_frame);
532 return AVERROR_INVALIDDATA;
533 }
534 31 bytestream2_skipu(&gb, 1); // compatible version
535 31 alac->sample_size = bytestream2_get_byteu(&gb);
536 31 alac->rice_history_mult = bytestream2_get_byteu(&gb);
537 31 alac->rice_initial_history = bytestream2_get_byteu(&gb);
538 31 alac->rice_limit = bytestream2_get_byteu(&gb);
539 31 alac->channels = bytestream2_get_byteu(&gb);
540 31 bytestream2_get_be16u(&gb); // maxRun
541 31 bytestream2_get_be32u(&gb); // max coded frame size
542 31 bytestream2_get_be32u(&gb); // average bitrate
543 31 alac->sample_rate = bytestream2_get_be32u(&gb);
544
545 31 return 0;
546 }
547
548 31 static av_cold int alac_decode_init(AVCodecContext * avctx)
549 {
550 int ret;
551 31 ALACContext *alac = avctx->priv_data;
552 31 alac->avctx = avctx;
553
554 /* initialize from the extradata */
555
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (alac->avctx->extradata_size < ALAC_EXTRADATA_SIZE) {
556 av_log(avctx, AV_LOG_ERROR, "extradata is too small\n");
557 return AVERROR_INVALIDDATA;
558 }
559
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
31 if ((ret = alac_set_info(alac)) < 0) {
560 av_log(avctx, AV_LOG_ERROR, "set_info failed\n");
561 return ret;
562 }
563
564
2/3
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
31 switch (alac->sample_size) {
565 23 case 16: avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
566 23 break;
567 8 case 20:
568 case 24:
569 8 case 32: avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
570 8 break;
571 default: avpriv_request_sample(avctx, "Sample depth %d", alac->sample_size);
572 return AVERROR_PATCHWELCOME;
573 }
574 31 avctx->bits_per_raw_sample = alac->sample_size;
575 31 avctx->sample_rate = alac->sample_rate;
576
577
2/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31 times.
31 if (alac->channels < 1 || alac->channels > ALAC_MAX_CHANNELS) {
578 av_log(avctx, AV_LOG_WARNING, "Invalid channel count\n");
579 alac->channels = avctx->ch_layout.nb_channels;
580 }
581
2/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31 times.
31 if (avctx->ch_layout.nb_channels > ALAC_MAX_CHANNELS || avctx->ch_layout.nb_channels <= 0 ) {
582 avpriv_report_missing_feature(avctx, "Channel count %d",
583 avctx->ch_layout.nb_channels);
584 return AVERROR_PATCHWELCOME;
585 }
586 31 av_channel_layout_uninit(&avctx->ch_layout);
587 31 avctx->ch_layout = ff_alac_ch_layouts[alac->channels - 1];
588
589
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
31 if ((ret = allocate_buffers(alac)) < 0) {
590 av_log(avctx, AV_LOG_ERROR, "Error allocating buffers\n");
591 return ret;
592 }
593
594 31 ff_alacdsp_init(&alac->dsp);
595
596 31 return 0;
597 }
598
599 static const AVOption options[] = {
600 { "extra_bits_bug", "Force non-standard decoding process",
601 offsetof(ALACContext, extra_bit_bug), AV_OPT_TYPE_BOOL, { .i64 = 0 },
602 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM },
603 { NULL },
604 };
605
606 static const AVClass alac_class = {
607 .class_name = "alac",
608 .item_name = av_default_item_name,
609 .option = options,
610 .version = LIBAVUTIL_VERSION_INT,
611 };
612
613 const FFCodec ff_alac_decoder = {
614 .p.name = "alac",
615 .p.long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
616 .p.type = AVMEDIA_TYPE_AUDIO,
617 .p.id = AV_CODEC_ID_ALAC,
618 .priv_data_size = sizeof(ALACContext),
619 .init = alac_decode_init,
620 .close = alac_decode_close,
621 FF_CODEC_DECODE_CB(alac_decode_frame),
622 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_CHANNEL_CONF,
623 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
624 .p.priv_class = &alac_class
625 };
626