• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
init_sample_buffers(AlacEncodeContext * s,int channels,const uint8_t * samples[2])85 static void init_sample_buffers(AlacEncodeContext *s, int channels,
86                                 const uint8_t *samples[2])
87 {
88     int ch, i;
89     int shift = av_get_bytes_per_sample(s->avctx->sample_fmt) * 8 -
90                 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     if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S32P)
102         COPY_SAMPLES(int32_t);
103     else
104         COPY_SAMPLES(int16_t);
105 }
106 
encode_scalar(AlacEncodeContext * s,int x,int k,int write_sample_size)107 static void encode_scalar(AlacEncodeContext *s, int x,
108                           int k, int write_sample_size)
109 {
110     int divisor, q, r;
111 
112     k = FFMIN(k, s->rc.k_modifier);
113     divisor = (1<<k) - 1;
114     q = x / divisor;
115     r = x % divisor;
116 
117     if (q > 8) {
118         // write escape code and sample value directly
119         put_bits(&s->pbctx, 9, ALAC_ESCAPE_CODE);
120         put_bits(&s->pbctx, write_sample_size, x);
121     } else {
122         if (q)
123             put_bits(&s->pbctx, q, (1<<q) - 1);
124         put_bits(&s->pbctx, 1, 0);
125 
126         if (k != 1) {
127             if (r > 0)
128                 put_bits(&s->pbctx, k, r+1);
129             else
130                 put_bits(&s->pbctx, k-1, 0);
131         }
132     }
133 }
134 
write_element_header(AlacEncodeContext * s,enum AlacRawDataBlockType element,int instance)135 static void write_element_header(AlacEncodeContext *s,
136                                  enum AlacRawDataBlockType element,
137                                  int instance)
138 {
139     int encode_fs = 0;
140 
141     if (s->frame_size < DEFAULT_FRAME_SIZE)
142         encode_fs = 1;
143 
144     put_bits(&s->pbctx, 3,  element);               // element type
145     put_bits(&s->pbctx, 4,  instance);              // element instance
146     put_bits(&s->pbctx, 12, 0);                     // unused header bits
147     put_bits(&s->pbctx, 1,  encode_fs);             // Sample count is in the header
148     put_bits(&s->pbctx, 2,  s->extra_bits >> 3);    // Extra bytes (for 24-bit)
149     put_bits(&s->pbctx, 1,  s->verbatim);           // Audio block is verbatim
150     if (encode_fs)
151         put_bits32(&s->pbctx, s->frame_size);       // No. of samples in the frame
152 }
153 
calc_predictor_params(AlacEncodeContext * s,int ch)154 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     if (s->compression_level == 1) {
161         s->lpc[ch].lpc_order = 6;
162         s->lpc[ch].lpc_quant = 6;
163         s->lpc[ch].lpc_coeff[0] =  160;
164         s->lpc[ch].lpc_coeff[1] = -190;
165         s->lpc[ch].lpc_coeff[2] =  170;
166         s->lpc[ch].lpc_coeff[3] = -130;
167         s->lpc[ch].lpc_coeff[4] =   80;
168         s->lpc[ch].lpc_coeff[5] =  -25;
169     } else {
170         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         s->lpc[ch].lpc_order = opt_order;
180         s->lpc[ch].lpc_quant = shift[opt_order-1];
181         memcpy(s->lpc[ch].lpc_coeff, coefs[opt_order-1], opt_order*sizeof(int));
182     }
183 }
184 
estimate_stereo_mode(int32_t * left_ch,int32_t * right_ch,int n)185 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     sum[0] = sum[1] = sum[2] = sum[3] = 0;
194     for (i = 2; i < n; i++) {
195         lt =  left_ch[i] - 2 *  left_ch[i - 1] +  left_ch[i - 2];
196         rt = right_ch[i] - 2 * right_ch[i - 1] + right_ch[i - 2];
197         sum[2] += FFABS((lt + rt) >> 1);
198         sum[3] += FFABS(lt - rt);
199         sum[0] += FFABS(lt);
200         sum[1] += FFABS(rt);
201     }
202 
203     /* calculate score for each mode */
204     score[0] = sum[0] + sum[1];
205     score[1] = sum[0] + sum[3];
206     score[2] = sum[1] + sum[3];
207     score[3] = sum[2] + sum[3];
208 
209     /* return mode with lowest score */
210     best = 0;
211     for (i = 1; i < 4; i++) {
212         if (score[i] < score[best])
213             best = i;
214     }
215     return best;
216 }
217 
alac_stereo_decorrelation(AlacEncodeContext * s)218 static void alac_stereo_decorrelation(AlacEncodeContext *s)
219 {
220     int32_t *left = s->sample_buf[0], *right = s->sample_buf[1];
221     int i, mode, n = s->frame_size;
222     int32_t tmp;
223 
224     mode = estimate_stereo_mode(left, right, n);
225 
226     switch (mode) {
227     case ALAC_CHMODE_LEFT_RIGHT:
228         s->interlacing_leftweight = 0;
229         s->interlacing_shift      = 0;
230         break;
231     case ALAC_CHMODE_LEFT_SIDE:
232         for (i = 0; i < n; i++)
233             right[i] = left[i] - right[i];
234         s->interlacing_leftweight = 1;
235         s->interlacing_shift      = 0;
236         break;
237     case ALAC_CHMODE_RIGHT_SIDE:
238         for (i = 0; i < n; i++) {
239             tmp = right[i];
240             right[i] = left[i] - right[i];
241             left[i]  = tmp + (right[i] >> 31);
242         }
243         s->interlacing_leftweight = 1;
244         s->interlacing_shift      = 31;
245         break;
246     default:
247         for (i = 0; i < n; i++) {
248             tmp = left[i];
249             left[i]  = (tmp + right[i]) >> 1;
250             right[i] =  tmp - right[i];
251         }
252         s->interlacing_leftweight = 1;
253         s->interlacing_shift      = 1;
254         break;
255     }
256 }
257 
alac_linear_predictor(AlacEncodeContext * s,int ch)258 static void alac_linear_predictor(AlacEncodeContext *s, int ch)
259 {
260     int i;
261     AlacLPCContext lpc = s->lpc[ch];
262     int32_t *residual = s->predictor_buf[ch];
263 
264     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     if (lpc.lpc_order > 0) {
278         int32_t *samples  = s->sample_buf[ch];
279 
280         // generate warm-up samples
281         residual[0] = samples[0];
282         for (i = 1; i <= lpc.lpc_order; i++)
283             residual[i] = sign_extend(samples[i] - samples[i-1], s->write_sample_size);
284 
285         // perform lpc on remaining samples
286         for (i = lpc.lpc_order + 1; i < s->frame_size; i++) {
287             int sum = 1 << (lpc.lpc_quant - 1), res_val, j;
288 
289             for (j = 0; j < lpc.lpc_order; j++) {
290                 sum += (samples[lpc.lpc_order-j] - samples[0]) *
291                        lpc.lpc_coeff[j];
292             }
293 
294             sum >>= lpc.lpc_quant;
295             sum += samples[0];
296             residual[i] = sign_extend(samples[lpc.lpc_order+1] - sum,
297                                       s->write_sample_size);
298             res_val = residual[i];
299 
300             if (res_val) {
301                 int index = lpc.lpc_order - 1;
302                 int neg = (res_val < 0);
303 
304                 while (index >= 0 && (neg ? (res_val < 0) : (res_val > 0))) {
305                     int val  = samples[0] - samples[lpc.lpc_order - index];
306                     int sign = (val ? FFSIGN(val) : 0);
307 
308                     if (neg)
309                         sign *= -1;
310 
311                     lpc.lpc_coeff[index] -= sign;
312                     val *= sign;
313                     res_val -= (val >> lpc.lpc_quant) * (lpc.lpc_order - index);
314                     index--;
315                 }
316             }
317             samples++;
318         }
319     }
320 }
321 
alac_entropy_coder(AlacEncodeContext * s,int ch)322 static void alac_entropy_coder(AlacEncodeContext *s, int ch)
323 {
324     unsigned int history = s->rc.initial_history;
325     int sign_modifier = 0, i, k;
326     int32_t *samples = s->predictor_buf[ch];
327 
328     for (i = 0; i < s->frame_size;) {
329         int x;
330 
331         k = av_log2((history >> 9) + 3);
332 
333         x  = -2 * (*samples) -1;
334         x ^= x >> 31;
335 
336         samples++;
337         i++;
338 
339         encode_scalar(s, x - sign_modifier, k, s->write_sample_size);
340 
341         history += x * s->rc.history_mult -
342                    ((history * s->rc.history_mult) >> 9);
343 
344         sign_modifier = 0;
345         if (x > 0xFFFF)
346             history = 0xFFFF;
347 
348         if (history < 128 && i < s->frame_size) {
349             unsigned int block_size = 0;
350 
351             k = 7 - av_log2(history) + ((history + 16) >> 6);
352 
353             while (*samples == 0 && i < s->frame_size) {
354                 samples++;
355                 i++;
356                 block_size++;
357             }
358             encode_scalar(s, block_size, k, 16);
359             sign_modifier = (block_size <= 0xFFFF);
360             history = 0;
361         }
362 
363     }
364 }
365 
write_element(AlacEncodeContext * s,enum AlacRawDataBlockType element,int instance,const uint8_t * samples0,const uint8_t * samples1)366 static void write_element(AlacEncodeContext *s,
367                           enum AlacRawDataBlockType element, int instance,
368                           const uint8_t *samples0, const uint8_t *samples1)
369 {
370     const uint8_t *samples[2] = { samples0, samples1 };
371     int i, j, channels;
372     int prediction_type = 0;
373     PutBitContext *pb = &s->pbctx;
374 
375     channels = element == TYPE_CPE ? 2 : 1;
376 
377     if (s->verbatim) {
378         write_element_header(s, element, instance);
379         /* samples are channel-interleaved in verbatim mode */
380         if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
381             int shift = 32 - s->avctx->bits_per_raw_sample;
382             const int32_t *samples_s32[2] = { (const int32_t *)samples0,
383                                               (const int32_t *)samples1 };
384             for (i = 0; i < s->frame_size; i++)
385                 for (j = 0; j < channels; j++)
386                     put_sbits(pb, s->avctx->bits_per_raw_sample,
387                               samples_s32[j][i] >> shift);
388         } else {
389             const int16_t *samples_s16[2] = { (const int16_t *)samples0,
390                                               (const int16_t *)samples1 };
391             for (i = 0; i < s->frame_size; i++)
392                 for (j = 0; j < channels; j++)
393                     put_sbits(pb, s->avctx->bits_per_raw_sample,
394                               samples_s16[j][i]);
395         }
396     } else {
397         s->write_sample_size = s->avctx->bits_per_raw_sample - s->extra_bits +
398                                channels - 1;
399 
400         init_sample_buffers(s, channels, samples);
401         write_element_header(s, element, instance);
402 
403         // extract extra bits if needed
404         if (s->extra_bits) {
405             uint32_t mask = (1 << s->extra_bits) - 1;
406             for (j = 0; j < channels; j++) {
407                 int32_t *extra = s->predictor_buf[j];
408                 int32_t *smp   = s->sample_buf[j];
409                 for (i = 0; i < s->frame_size; i++) {
410                     extra[i] = smp[i] & mask;
411                     smp[i] >>= s->extra_bits;
412                 }
413             }
414         }
415 
416         if (channels == 2)
417             alac_stereo_decorrelation(s);
418         else
419             s->interlacing_shift = s->interlacing_leftweight = 0;
420         put_bits(pb, 8, s->interlacing_shift);
421         put_bits(pb, 8, s->interlacing_leftweight);
422 
423         for (i = 0; i < channels; i++) {
424             calc_predictor_params(s, i);
425 
426             put_bits(pb, 4, prediction_type);
427             put_bits(pb, 4, s->lpc[i].lpc_quant);
428 
429             put_bits(pb, 3, s->rc.rice_modifier);
430             put_bits(pb, 5, s->lpc[i].lpc_order);
431             // predictor coeff. table
432             for (j = 0; j < s->lpc[i].lpc_order; j++)
433                 put_sbits(pb, 16, s->lpc[i].lpc_coeff[j]);
434         }
435 
436         // write extra bits if needed
437         if (s->extra_bits) {
438             for (i = 0; i < s->frame_size; i++) {
439                 for (j = 0; j < channels; j++) {
440                     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         for (i = 0; i < channels; i++) {
447             alac_linear_predictor(s, i);
448 
449             // TODO: determine when this will actually help. for now it's not used.
450             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             alac_entropy_coder(s, i);
457         }
458     }
459 }
460 
write_frame(AlacEncodeContext * s,AVPacket * avpkt,uint8_t * const * samples)461 static int write_frame(AlacEncodeContext *s, AVPacket *avpkt,
462                        uint8_t * const *samples)
463 {
464     PutBitContext *pb = &s->pbctx;
465     int channels = s->avctx->ch_layout.nb_channels;
466     const enum AlacRawDataBlockType *ch_elements = ff_alac_channel_elements[channels - 1];
467     const uint8_t *ch_map = ff_alac_channel_layout_offsets[channels - 1];
468     int ch, element, sce, cpe;
469 
470     init_put_bits(pb, avpkt->data, avpkt->size);
471 
472     ch = element = sce = cpe = 0;
473     while (ch < channels) {
474         if (ch_elements[element] == TYPE_CPE) {
475             write_element(s, TYPE_CPE, cpe, samples[ch_map[ch]],
476                           samples[ch_map[ch + 1]]);
477             cpe++;
478             ch += 2;
479         } else {
480             write_element(s, TYPE_SCE, sce, samples[ch_map[ch]], NULL);
481             sce++;
482             ch++;
483         }
484         element++;
485     }
486 
487     put_bits(pb, 3, TYPE_END);
488     flush_put_bits(pb);
489 
490     return put_bytes_output(pb);
491 }
492 
get_max_frame_size(int frame_size,int ch,int bps)493 static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps)
494 {
495     int header_bits = 23 + 32 * (frame_size < DEFAULT_FRAME_SIZE);
496     return FFALIGN(header_bits + bps * ch * frame_size + 3, 8) / 8;
497 }
498 
alac_encode_close(AVCodecContext * avctx)499 static av_cold int alac_encode_close(AVCodecContext *avctx)
500 {
501     AlacEncodeContext *s = avctx->priv_data;
502     ff_lpc_end(&s->lpc_ctx);
503     return 0;
504 }
505 
alac_encode_init(AVCodecContext * avctx)506 static av_cold int alac_encode_init(AVCodecContext *avctx)
507 {
508     AlacEncodeContext *s = avctx->priv_data;
509     int ret;
510     uint8_t *alac_extradata;
511 
512     avctx->frame_size = s->frame_size = DEFAULT_FRAME_SIZE;
513 
514     if (avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
515         if (avctx->bits_per_raw_sample != 24)
516             av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
517         avctx->bits_per_raw_sample = 24;
518     } else {
519         avctx->bits_per_raw_sample = 16;
520         s->extra_bits              = 0;
521     }
522 
523     // Set default compression level
524     if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
525         s->compression_level = 2;
526     else
527         s->compression_level = av_clip(avctx->compression_level, 0, 2);
528 
529     // Initialize default Rice parameters
530     s->rc.history_mult    = 40;
531     s->rc.initial_history = 10;
532     s->rc.k_modifier      = 14;
533     s->rc.rice_modifier   = 4;
534 
535     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     avctx->extradata = av_mallocz(ALAC_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
540     if (!avctx->extradata)
541         return AVERROR(ENOMEM);
542     avctx->extradata_size = ALAC_EXTRADATA_SIZE;
543 
544     alac_extradata = avctx->extradata;
545     AV_WB32(alac_extradata,    ALAC_EXTRADATA_SIZE);
546     AV_WB32(alac_extradata+4,  MKBETAG('a','l','a','c'));
547     AV_WB32(alac_extradata+12, avctx->frame_size);
548     AV_WB8 (alac_extradata+17, avctx->bits_per_raw_sample);
549     AV_WB8 (alac_extradata+21, avctx->ch_layout.nb_channels);
550     AV_WB32(alac_extradata+24, s->max_coded_frame_size);
551     AV_WB32(alac_extradata+28,
552             avctx->sample_rate * avctx->ch_layout.nb_channels * avctx->bits_per_raw_sample); // average bitrate
553     AV_WB32(alac_extradata+32, avctx->sample_rate);
554 
555     // Set relevant extradata fields
556     if (s->compression_level > 0) {
557         AV_WB8(alac_extradata+18, s->rc.history_mult);
558         AV_WB8(alac_extradata+19, s->rc.initial_history);
559         AV_WB8(alac_extradata+20, s->rc.k_modifier);
560     }
561 
562     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     s->avctx = avctx;
570 
571     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     return 0;
578 }
579 
alac_encode_frame(AVCodecContext * avctx,AVPacket * avpkt,const AVFrame * frame,int * got_packet_ptr)580 static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
581                              const AVFrame *frame, int *got_packet_ptr)
582 {
583     AlacEncodeContext *s = avctx->priv_data;
584     int out_bytes, max_frame_size, ret;
585 
586     s->frame_size = frame->nb_samples;
587 
588     if (frame->nb_samples < DEFAULT_FRAME_SIZE)
589         max_frame_size = get_max_frame_size(s->frame_size, avctx->ch_layout.nb_channels,
590                                             avctx->bits_per_raw_sample);
591     else
592         max_frame_size = s->max_coded_frame_size;
593 
594     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     if (s->compression_level) {
599         s->verbatim   = 0;
600         s->extra_bits = avctx->bits_per_raw_sample - 16;
601     } else {
602         s->verbatim   = 1;
603         s->extra_bits = 0;
604     }
605 
606     out_bytes = write_frame(s, avpkt, frame->extended_data);
607 
608     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     avpkt->size = out_bytes;
616     *got_packet_ptr = 1;
617     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 FF_DISABLE_DEPRECATION_WARNINGS
652 const FFCodec ff_alac_encoder = {
653     .p.name         = "alac",
654     .p.long_name    = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
655     .p.type         = AVMEDIA_TYPE_AUDIO,
656     .p.id           = AV_CODEC_ID_ALAC,
657     .priv_data_size = sizeof(AlacEncodeContext),
658     .p.priv_class   = &alacenc_class,
659     .init           = alac_encode_init,
660     FF_CODEC_ENCODE_CB(alac_encode_frame),
661     .close          = alac_encode_close,
662     .p.capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
663 #if FF_API_OLD_CHANNEL_LAYOUT
664     .p.channel_layouts = alac_channel_layouts,
665 #endif
666     .p.ch_layouts   = ff_alac_ch_layouts,
667     .p.sample_fmts  = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32P,
668                                                      AV_SAMPLE_FMT_S16P,
669                                                      AV_SAMPLE_FMT_NONE },
670     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
671 };
672 FF_ENABLE_DEPRECATION_WARNINGS
673