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