]> git.sesse.net Git - ffmpeg/blob - libavcodec/alac.c
Merge commit 'e57c4706e969afa1f2384481b955ccd9494cddb5'
[ffmpeg] / libavcodec / alac.c
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  * 32bit  atom size
33  * 32bit  tag                  ("alac")
34  * 32bit  tag version          (0)
35  * 32bit  samples per frame    (used when not set explicitly in the frames)
36  *  8bit  compatible version   (0)
37  *  8bit  sample size
38  *  8bit  history mult         (40)
39  *  8bit  initial history      (14)
40  *  8bit  rice param limit     (10)
41  *  8bit  channels
42  * 16bit  maxRun               (255)
43  * 32bit  max coded frame size (0 means unknown)
44  * 32bit  average bitrate      (0 means unknown)
45  * 32bit  samplerate
46  */
47
48 #include "libavutil/channel_layout.h"
49 #include "avcodec.h"
50 #include "get_bits.h"
51 #include "bytestream.h"
52 #include "internal.h"
53 #include "unary.h"
54 #include "mathops.h"
55
56 #define ALAC_EXTRADATA_SIZE 36
57 #define MAX_CHANNELS 8
58
59 typedef struct {
60     AVCodecContext *avctx;
61     AVFrame frame;
62     GetBitContext gb;
63     int channels;
64
65     int32_t *predict_error_buffer[2];
66     int32_t *output_samples_buffer[2];
67     int32_t *extra_bits_buffer[2];
68
69     uint32_t max_samples_per_frame;
70     uint8_t  sample_size;
71     uint8_t  rice_history_mult;
72     uint8_t  rice_initial_history;
73     uint8_t  rice_limit;
74
75     int extra_bits;     /**< number of extra bits beyond 16-bit */
76     int nb_samples;     /**< number of samples in the current frame */
77
78     int direct_output;
79 } ALACContext;
80
81 enum RawDataBlockType {
82     /* At the moment, only SCE, CPE, LFE, and END are recognized. */
83     TYPE_SCE,
84     TYPE_CPE,
85     TYPE_CCE,
86     TYPE_LFE,
87     TYPE_DSE,
88     TYPE_PCE,
89     TYPE_FIL,
90     TYPE_END
91 };
92
93 static const uint8_t alac_channel_layout_offsets[8][8] = {
94     { 0 },
95     { 0, 1 },
96     { 2, 0, 1 },
97     { 2, 0, 1, 3 },
98     { 2, 0, 1, 3, 4 },
99     { 2, 0, 1, 4, 5, 3 },
100     { 2, 0, 1, 4, 5, 6, 3 },
101     { 2, 6, 7, 0, 1, 4, 5, 3 }
102 };
103
104 static const uint16_t alac_channel_layouts[8] = {
105     AV_CH_LAYOUT_MONO,
106     AV_CH_LAYOUT_STEREO,
107     AV_CH_LAYOUT_SURROUND,
108     AV_CH_LAYOUT_4POINT0,
109     AV_CH_LAYOUT_5POINT0_BACK,
110     AV_CH_LAYOUT_5POINT1_BACK,
111     AV_CH_LAYOUT_6POINT1_BACK,
112     AV_CH_LAYOUT_7POINT1_WIDE_BACK
113 };
114
115 static inline unsigned int decode_scalar(GetBitContext *gb, int k, int bps)
116 {
117     unsigned int x = get_unary_0_9(gb);
118
119     if (x > 8) { /* RICE THRESHOLD */
120         /* use alternative encoding */
121         x = get_bits_long(gb, bps);
122     } else if (k != 1) {
123         int extrabits = show_bits(gb, k);
124
125         /* multiply x by 2^k - 1, as part of their strange algorithm */
126         x = (x << k) - x;
127
128         if (extrabits > 1) {
129             x += extrabits - 1;
130             skip_bits(gb, k);
131         } else
132             skip_bits(gb, k - 1);
133     }
134     return x;
135 }
136
137 static int rice_decompress(ALACContext *alac, int32_t *output_buffer,
138                             int nb_samples, int bps, int rice_history_mult)
139 {
140     int i;
141     unsigned int history = alac->rice_initial_history;
142     int sign_modifier = 0;
143
144     for (i = 0; i < nb_samples; i++) {
145         int k;
146         unsigned int x;
147
148         if(get_bits_left(&alac->gb) <= 0)
149             return -1;
150
151         /* calculate rice param and decode next value */
152         k = av_log2((history >> 9) + 3);
153         k = FFMIN(k, alac->rice_limit);
154         x = decode_scalar(&alac->gb, k, bps);
155         x += sign_modifier;
156         sign_modifier = 0;
157         output_buffer[i] = (x >> 1) ^ -(x & 1);
158
159         /* update the history */
160         if (x > 0xffff)
161             history = 0xffff;
162         else
163             history +=         x * rice_history_mult -
164                        ((history * rice_history_mult) >> 9);
165
166         /* special case: there may be compressed blocks of 0 */
167         if ((history < 128) && (i + 1 < nb_samples)) {
168             int block_size;
169
170             /* calculate rice param and decode block size */
171             k = 7 - av_log2(history) + ((history + 16) >> 6);
172             k = FFMIN(k, alac->rice_limit);
173             block_size = decode_scalar(&alac->gb, k, 16);
174
175             if (block_size > 0) {
176                 if (block_size >= nb_samples - i) {
177                     av_log(alac->avctx, AV_LOG_ERROR,
178                            "invalid zero block size of %d %d %d\n", block_size,
179                            nb_samples, i);
180                     block_size = nb_samples - i - 1;
181                 }
182                 memset(&output_buffer[i + 1], 0,
183                        block_size * sizeof(*output_buffer));
184                 i += block_size;
185             }
186             if (block_size <= 0xffff)
187                 sign_modifier = 1;
188             history = 0;
189         }
190     }
191     return 0;
192 }
193
194 static inline int sign_only(int v)
195 {
196     return v ? FFSIGN(v) : 0;
197 }
198
199 static void lpc_prediction(int32_t *error_buffer, int32_t *buffer_out,
200                            int nb_samples, int bps, int16_t *lpc_coefs,
201                            int lpc_order, int lpc_quant)
202 {
203     int i;
204     int32_t *pred = buffer_out;
205
206     /* first sample always copies */
207     *buffer_out = *error_buffer;
208
209     if (nb_samples <= 1)
210         return;
211
212     if (!lpc_order) {
213         memcpy(&buffer_out[1], &error_buffer[1],
214                (nb_samples - 1) * sizeof(*buffer_out));
215         return;
216     }
217
218     if (lpc_order == 31) {
219         /* simple 1st-order prediction */
220         for (i = 1; i < nb_samples; i++) {
221             buffer_out[i] = sign_extend(buffer_out[i - 1] + error_buffer[i],
222                                         bps);
223         }
224         return;
225     }
226
227     /* read warm-up samples */
228     for (i = 1; i <= lpc_order && i < nb_samples; i++)
229         buffer_out[i] = sign_extend(buffer_out[i - 1] + error_buffer[i], bps);
230
231     /* NOTE: 4 and 8 are very common cases that could be optimized. */
232
233     for (; i < nb_samples; i++) {
234         int j;
235         int val = 0;
236         int error_val = error_buffer[i];
237         int error_sign;
238         int d = *pred++;
239
240         /* LPC prediction */
241         for (j = 0; j < lpc_order; j++)
242             val += (pred[j] - d) * lpc_coefs[j];
243         val = (val + (1 << (lpc_quant - 1))) >> lpc_quant;
244         val += d + error_val;
245         buffer_out[i] = sign_extend(val, bps);
246
247         /* adapt LPC coefficients */
248         error_sign = sign_only(error_val);
249         if (error_sign) {
250             for (j = 0; j < lpc_order && error_val * error_sign > 0; j++) {
251                 int sign;
252                 val  = d - pred[j];
253                 sign = sign_only(val) * error_sign;
254                 lpc_coefs[j] -= sign;
255                 val *= sign;
256                 error_val -= (val >> lpc_quant) * (j + 1);
257             }
258         }
259     }
260 }
261
262 static void decorrelate_stereo(int32_t *buffer[2], int nb_samples,
263                                int decorr_shift, int decorr_left_weight)
264 {
265     int i;
266
267     for (i = 0; i < nb_samples; i++) {
268         int32_t a, b;
269
270         a = buffer[0][i];
271         b = buffer[1][i];
272
273         a -= (b * decorr_left_weight) >> decorr_shift;
274         b += a;
275
276         buffer[0][i] = b;
277         buffer[1][i] = a;
278     }
279 }
280
281 static void append_extra_bits(int32_t *buffer[2], int32_t *extra_bits_buffer[2],
282                               int extra_bits, int channels, int nb_samples)
283 {
284     int i, ch;
285
286     for (ch = 0; ch < channels; ch++)
287         for (i = 0; i < nb_samples; i++)
288             buffer[ch][i] = (buffer[ch][i] << extra_bits) | extra_bits_buffer[ch][i];
289 }
290
291 static int decode_element(AVCodecContext *avctx, void *data, int ch_index,
292                           int channels)
293 {
294     ALACContext *alac = avctx->priv_data;
295     int has_size, bps, is_compressed, decorr_shift, decorr_left_weight, ret;
296     uint32_t output_samples;
297     int i, ch;
298
299     skip_bits(&alac->gb, 4);  /* element instance tag */
300     skip_bits(&alac->gb, 12); /* unused header bits */
301
302     /* the number of output samples is stored in the frame */
303     has_size = get_bits1(&alac->gb);
304
305     alac->extra_bits = get_bits(&alac->gb, 2) << 3;
306     bps = alac->sample_size - alac->extra_bits + channels - 1;
307     if (bps > 32U) {
308         av_log(avctx, AV_LOG_ERROR, "bps is unsupported: %d\n", bps);
309         return AVERROR_PATCHWELCOME;
310     }
311
312     /* whether the frame is compressed */
313     is_compressed = !get_bits1(&alac->gb);
314
315     if (has_size)
316         output_samples = get_bits_long(&alac->gb, 32);
317     else
318         output_samples = alac->max_samples_per_frame;
319     if (!output_samples || output_samples > alac->max_samples_per_frame) {
320         av_log(avctx, AV_LOG_ERROR, "invalid samples per frame: %d\n",
321                output_samples);
322         return AVERROR_INVALIDDATA;
323     }
324     if (!alac->nb_samples) {
325         /* get output buffer */
326         alac->frame.nb_samples = output_samples;
327         if ((ret = ff_get_buffer(avctx, &alac->frame)) < 0) {
328             av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
329             return ret;
330         }
331     } else if (output_samples != alac->nb_samples) {
332         av_log(avctx, AV_LOG_ERROR, "sample count mismatch: %u != %d\n",
333                output_samples, alac->nb_samples);
334         return AVERROR_INVALIDDATA;
335     }
336     alac->nb_samples = output_samples;
337     if (alac->direct_output) {
338         for (ch = 0; ch < channels; ch++)
339             alac->output_samples_buffer[ch] = (int32_t *)alac->frame.extended_data[ch_index + ch];
340     }
341
342     if (is_compressed) {
343         int16_t lpc_coefs[2][32];
344         int lpc_order[2];
345         int prediction_type[2];
346         int lpc_quant[2];
347         int rice_history_mult[2];
348
349         decorr_shift       = get_bits(&alac->gb, 8);
350         decorr_left_weight = get_bits(&alac->gb, 8);
351
352         for (ch = 0; ch < channels; ch++) {
353             prediction_type[ch]   = get_bits(&alac->gb, 4);
354             lpc_quant[ch]         = get_bits(&alac->gb, 4);
355             rice_history_mult[ch] = get_bits(&alac->gb, 3);
356             lpc_order[ch]         = get_bits(&alac->gb, 5);
357
358             /* read the predictor table */
359             for (i = lpc_order[ch] - 1; i >= 0; i--)
360                 lpc_coefs[ch][i] = get_sbits(&alac->gb, 16);
361         }
362
363         if (alac->extra_bits) {
364             for (i = 0; i < alac->nb_samples; i++) {
365                 if(get_bits_left(&alac->gb) <= 0)
366                     return -1;
367                 for (ch = 0; ch < channels; ch++)
368                     alac->extra_bits_buffer[ch][i] = get_bits(&alac->gb, alac->extra_bits);
369             }
370         }
371         for (ch = 0; ch < channels; ch++) {
372             int ret=rice_decompress(alac, alac->predict_error_buffer[ch],
373                             alac->nb_samples, bps,
374                             rice_history_mult[ch] * alac->rice_history_mult / 4);
375             if(ret<0)
376                 return ret;
377
378             /* adaptive FIR filter */
379             if (prediction_type[ch] == 15) {
380                 /* Prediction type 15 runs the adaptive FIR twice.
381                  * The first pass uses the special-case coef_num = 31, while
382                  * the second pass uses the coefs from the bitstream.
383                  *
384                  * However, this prediction type is not currently used by the
385                  * reference encoder.
386                  */
387                 lpc_prediction(alac->predict_error_buffer[ch],
388                                alac->predict_error_buffer[ch],
389                                alac->nb_samples, bps, NULL, 31, 0);
390             } else if (prediction_type[ch] > 0) {
391                 av_log(avctx, AV_LOG_WARNING, "unknown prediction type: %i\n",
392                        prediction_type[ch]);
393             }
394             lpc_prediction(alac->predict_error_buffer[ch],
395                            alac->output_samples_buffer[ch], alac->nb_samples,
396                            bps, lpc_coefs[ch], lpc_order[ch], lpc_quant[ch]);
397         }
398     } else {
399         /* not compressed, easy case */
400         for (i = 0; i < alac->nb_samples; i++) {
401             if(get_bits_left(&alac->gb) <= 0)
402                 return -1;
403             for (ch = 0; ch < channels; ch++) {
404                 alac->output_samples_buffer[ch][i] =
405                          get_sbits_long(&alac->gb, alac->sample_size);
406             }
407         }
408         alac->extra_bits   = 0;
409         decorr_shift       = 0;
410         decorr_left_weight = 0;
411     }
412
413     if (channels == 2 && decorr_left_weight) {
414         decorrelate_stereo(alac->output_samples_buffer, alac->nb_samples,
415                            decorr_shift, decorr_left_weight);
416     }
417
418     if (alac->extra_bits) {
419         append_extra_bits(alac->output_samples_buffer, alac->extra_bits_buffer,
420                           alac->extra_bits, channels, alac->nb_samples);
421     }
422
423     if(av_sample_fmt_is_planar(avctx->sample_fmt)) {
424     switch(alac->sample_size) {
425     case 16: {
426         for (ch = 0; ch < channels; ch++) {
427             int16_t *outbuffer = (int16_t *)alac->frame.extended_data[ch_index + ch];
428             for (i = 0; i < alac->nb_samples; i++)
429                 *outbuffer++ = alac->output_samples_buffer[ch][i];
430         }}
431         break;
432     case 24: {
433         for (ch = 0; ch < channels; ch++) {
434             for (i = 0; i < alac->nb_samples; i++)
435                 alac->output_samples_buffer[ch][i] <<= 8;
436         }}
437         break;
438     }
439     }else{
440         switch(alac->sample_size) {
441         case 16: {
442             int16_t *outbuffer = ((int16_t *)alac->frame.extended_data[0]) + ch_index;
443             for (i = 0; i < alac->nb_samples; i++) {
444                 for (ch = 0; ch < channels; ch++)
445                     *outbuffer++ = alac->output_samples_buffer[ch][i];
446                 outbuffer += alac->channels - channels;
447             }
448             }
449             break;
450         case 24: {
451             int32_t *outbuffer = ((int32_t *)alac->frame.extended_data[0]) + ch_index;
452             for (i = 0; i < alac->nb_samples; i++) {
453                 for (ch = 0; ch < channels; ch++)
454                     *outbuffer++ = alac->output_samples_buffer[ch][i] << 8;
455                 outbuffer += alac->channels - channels;
456             }
457             }
458             break;
459         case 32: {
460             int32_t *outbuffer = ((int32_t *)alac->frame.extended_data[0]) + ch_index;
461             for (i = 0; i < alac->nb_samples; i++) {
462                 for (ch = 0; ch < channels; ch++)
463                     *outbuffer++ = alac->output_samples_buffer[ch][i];
464                 outbuffer += alac->channels - channels;
465             }
466             }
467             break;
468         }
469     }
470
471     return 0;
472 }
473
474 static int alac_decode_frame(AVCodecContext *avctx, void *data,
475                              int *got_frame_ptr, AVPacket *avpkt)
476 {
477     ALACContext *alac = avctx->priv_data;
478     enum RawDataBlockType element;
479     int channels;
480     int ch, ret, got_end;
481
482     init_get_bits(&alac->gb, avpkt->data, avpkt->size * 8);
483
484     got_end = 0;
485     alac->nb_samples = 0;
486     ch = 0;
487     while (get_bits_left(&alac->gb) >= 3) {
488         element = get_bits(&alac->gb, 3);
489         if (element == TYPE_END) {
490             got_end = 1;
491             break;
492         }
493         if (element > TYPE_CPE && element != TYPE_LFE) {
494             av_log(avctx, AV_LOG_ERROR, "syntax element unsupported: %d\n", element);
495             return AVERROR_PATCHWELCOME;
496         }
497
498         channels = (element == TYPE_CPE) ? 2 : 1;
499         if (   ch + channels > alac->channels
500             || alac_channel_layout_offsets[alac->channels - 1][ch] + channels > alac->channels
501         ) {
502             av_log(avctx, AV_LOG_ERROR, "invalid element channel count\n");
503             return AVERROR_INVALIDDATA;
504         }
505
506         ret = decode_element(avctx, data,
507                              alac_channel_layout_offsets[alac->channels - 1][ch],
508                              channels);
509         if (ret < 0 && get_bits_left(&alac->gb))
510             return ret;
511
512         ch += channels;
513     }
514     if (!got_end) {
515         av_log(avctx, AV_LOG_ERROR, "no end tag found. incomplete packet.\n");
516         return AVERROR_INVALIDDATA;
517     }
518
519     if (avpkt->size * 8 - get_bits_count(&alac->gb) > 8) {
520         av_log(avctx, AV_LOG_ERROR, "Error : %d bits left\n",
521                avpkt->size * 8 - get_bits_count(&alac->gb));
522     }
523
524     *got_frame_ptr   = 1;
525     *(AVFrame *)data = alac->frame;
526
527     return avpkt->size;
528 }
529
530 static av_cold int alac_decode_close(AVCodecContext *avctx)
531 {
532     ALACContext *alac = avctx->priv_data;
533
534     int ch;
535     for (ch = 0; ch < FFMIN(alac->channels, 2); ch++) {
536         av_freep(&alac->predict_error_buffer[ch]);
537         if (!alac->direct_output)
538             av_freep(&alac->output_samples_buffer[ch]);
539         av_freep(&alac->extra_bits_buffer[ch]);
540     }
541
542     return 0;
543 }
544
545 static int allocate_buffers(ALACContext *alac)
546 {
547     int ch;
548     int buf_size;
549
550     if (alac->max_samples_per_frame > INT_MAX / sizeof(int32_t))
551         goto buf_alloc_fail;
552     buf_size = alac->max_samples_per_frame * sizeof(int32_t);
553
554     for (ch = 0; ch < FFMIN(alac->channels, 2); ch++) {
555         FF_ALLOC_OR_GOTO(alac->avctx, alac->predict_error_buffer[ch],
556                          buf_size, buf_alloc_fail);
557
558         alac->direct_output = alac->sample_size > 16 && av_sample_fmt_is_planar(alac->avctx->sample_fmt);
559         if (!alac->direct_output) {
560             FF_ALLOC_OR_GOTO(alac->avctx, alac->output_samples_buffer[ch],
561                              buf_size, buf_alloc_fail);
562         }
563
564         FF_ALLOC_OR_GOTO(alac->avctx, alac->extra_bits_buffer[ch],
565                          buf_size, buf_alloc_fail);
566     }
567     return 0;
568 buf_alloc_fail:
569     alac_decode_close(alac->avctx);
570     return AVERROR(ENOMEM);
571 }
572
573 static int alac_set_info(ALACContext *alac)
574 {
575     GetByteContext gb;
576
577     bytestream2_init(&gb, alac->avctx->extradata,
578                      alac->avctx->extradata_size);
579
580     bytestream2_skipu(&gb, 12); // size:4, alac:4, version:4
581
582     alac->max_samples_per_frame = bytestream2_get_be32u(&gb);
583     if (!alac->max_samples_per_frame || alac->max_samples_per_frame > INT_MAX) {
584         av_log(alac->avctx, AV_LOG_ERROR, "max samples per frame invalid: %u\n",
585                alac->max_samples_per_frame);
586         return AVERROR_INVALIDDATA;
587     }
588     bytestream2_skipu(&gb, 1);  // compatible version
589     alac->sample_size          = bytestream2_get_byteu(&gb);
590     alac->rice_history_mult    = bytestream2_get_byteu(&gb);
591     alac->rice_initial_history = bytestream2_get_byteu(&gb);
592     alac->rice_limit           = bytestream2_get_byteu(&gb);
593     alac->channels             = bytestream2_get_byteu(&gb);
594     bytestream2_get_be16u(&gb); // maxRun
595     bytestream2_get_be32u(&gb); // max coded frame size
596     bytestream2_get_be32u(&gb); // average bitrate
597     bytestream2_get_be32u(&gb); // samplerate
598
599     return 0;
600 }
601
602 static av_cold int alac_decode_init(AVCodecContext * avctx)
603 {
604     int ret;
605     int req_packed;
606     ALACContext *alac = avctx->priv_data;
607     alac->avctx = avctx;
608
609     /* initialize from the extradata */
610     if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
611         av_log(avctx, AV_LOG_ERROR, "expected %d extradata bytes\n",
612             ALAC_EXTRADATA_SIZE);
613         return -1;
614     }
615     if (alac_set_info(alac)) {
616         av_log(avctx, AV_LOG_ERROR, "set_info failed\n");
617         return -1;
618     }
619
620     req_packed = LIBAVCODEC_VERSION_MAJOR < 55 && !av_sample_fmt_is_planar(avctx->request_sample_fmt);
621     switch (alac->sample_size) {
622     case 16: avctx->sample_fmt = req_packed ? AV_SAMPLE_FMT_S16 : AV_SAMPLE_FMT_S16P;
623              break;
624     case 24:
625     case 32: avctx->sample_fmt = req_packed ? AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S32P;
626              break;
627     default: av_log_ask_for_sample(avctx, "Sample depth %d is not supported.\n",
628                                    alac->sample_size);
629              return AVERROR_PATCHWELCOME;
630     }
631     avctx->bits_per_raw_sample = alac->sample_size;
632
633     if (alac->channels < 1) {
634         av_log(avctx, AV_LOG_WARNING, "Invalid channel count\n");
635         alac->channels = avctx->channels;
636     } else {
637         if (alac->channels > MAX_CHANNELS)
638             alac->channels = avctx->channels;
639         else
640             avctx->channels = alac->channels;
641     }
642     if (avctx->channels > MAX_CHANNELS || avctx->channels <= 0 ) {
643         av_log(avctx, AV_LOG_ERROR, "Unsupported channel count: %d\n",
644                avctx->channels);
645         return AVERROR_PATCHWELCOME;
646     }
647     avctx->channel_layout = alac_channel_layouts[alac->channels - 1];
648
649     if ((ret = allocate_buffers(alac)) < 0) {
650         av_log(avctx, AV_LOG_ERROR, "Error allocating buffers\n");
651         return ret;
652     }
653
654     avcodec_get_frame_defaults(&alac->frame);
655     avctx->coded_frame = &alac->frame;
656
657     return 0;
658 }
659
660 AVCodec ff_alac_decoder = {
661     .name           = "alac",
662     .type           = AVMEDIA_TYPE_AUDIO,
663     .id             = AV_CODEC_ID_ALAC,
664     .priv_data_size = sizeof(ALACContext),
665     .init           = alac_decode_init,
666     .close          = alac_decode_close,
667     .decode         = alac_decode_frame,
668     .capabilities   = CODEC_CAP_DR1,
669     .long_name      = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
670 };