libavcodec: Add missing AVClass pointers
[libav.git] / libavcodec / alacenc.c
CommitLineData
32f3c541 1/*
6b931476
RP
2 * ALAC audio encoder
3 * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
4 *
2912e87a 5 * This file is part of Libav.
6b931476 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
6b931476
RP
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
6b931476
RP
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
6b931476
RP
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
243df135
VG
22#include "libavutil/opt.h"
23
6b931476 24#include "avcodec.h"
b2755007 25#include "put_bits.h"
764852d6 26#include "internal.h"
6b931476 27#include "lpc.h"
4f46099b 28#include "mathops.h"
5e1bbb8c 29#include "alac_data.h"
6b931476
RP
30
31#define DEFAULT_FRAME_SIZE 4096
6b931476
RP
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
ca048266
RP
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
0d962ecb
RP
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
ca048266
RP
48typedef struct RiceContext {
49 int history_mult;
50 int initial_history;
51 int k_modifier;
52 int rice_modifier;
53} RiceContext;
54
56f8952b 55typedef struct AlacLPCContext {
ca048266
RP
56 int lpc_order;
57 int lpc_coeff[ALAC_MAX_LPC_ORDER+1];
58 int lpc_quant;
56f8952b 59} AlacLPCContext;
ca048266
RP
60
61typedef struct AlacEncodeContext {
87a814fd 62 const AVClass *class;
243df135 63 AVCodecContext *avctx;
ba821b09 64 int frame_size; /**< current frame size */
b6e8ff72 65 int verbatim; /**< current frame verbatim mode flag */
ca048266 66 int compression_level;
0d962ecb
RP
67 int min_prediction_order;
68 int max_prediction_order;
ca048266
RP
69 int max_coded_frame_size;
70 int write_sample_size;
7c278d2a 71 int extra_bits;
5e1bbb8c 72 int32_t sample_buf[2][DEFAULT_FRAME_SIZE];
0d962ecb 73 int32_t predictor_buf[DEFAULT_FRAME_SIZE];
6b931476
RP
74 int interlacing_shift;
75 int interlacing_leftweight;
76 PutBitContext pbctx;
ca048266 77 RiceContext rc;
5e1bbb8c 78 AlacLPCContext lpc[2];
56f8952b 79 LPCContext lpc_ctx;
6b931476
RP
80} AlacEncodeContext;
81
82
5e1bbb8c
JR
83static void init_sample_buffers(AlacEncodeContext *s, int channels,
84 uint8_t const *samples[2])
ca048266
RP
85{
86 int ch, i;
7c278d2a
JR
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 { \
5e1bbb8c 91 for (ch = 0; ch < channels; ch++) { \
7c278d2a
JR
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);
ca048266
RP
103}
104
d0fd6fc2
NAM
105static void encode_scalar(AlacEncodeContext *s, int x,
106 int k, int write_sample_size)
6b931476
RP
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
d0fd6fc2 115 if (q > 8) {
6b931476
RP
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 {
d0fd6fc2 120 if (q)
6b931476
RP
121 put_bits(&s->pbctx, q, (1<<q) - 1);
122 put_bits(&s->pbctx, 1, 0);
123
d0fd6fc2
NAM
124 if (k != 1) {
125 if (r > 0)
6b931476
RP
126 put_bits(&s->pbctx, k, r+1);
127 else
128 put_bits(&s->pbctx, k-1, 0);
129 }
130 }
131}
132
5e1bbb8c
JR
133static void write_element_header(AlacEncodeContext *s,
134 enum AlacRawDataBlockType element,
135 int instance)
6b931476 136{
b590f3a7
JR
137 int encode_fs = 0;
138
139 if (s->frame_size < DEFAULT_FRAME_SIZE)
140 encode_fs = 1;
141
5e1bbb8c
JR
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
b590f3a7 145 put_bits(&s->pbctx, 1, encode_fs); // Sample count is in the header
7c278d2a 146 put_bits(&s->pbctx, 2, s->extra_bits >> 3); // Extra bytes (for 24-bit)
b6e8ff72 147 put_bits(&s->pbctx, 1, s->verbatim); // Audio block is verbatim
b590f3a7
JR
148 if (encode_fs)
149 put_bits32(&s->pbctx, s->frame_size); // No. of samples in the frame
6b931476
RP
150}
151
0d962ecb
RP
152static 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
215dab5f 158 if (s->compression_level == 1) {
a0992374
JM
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 {
56f8952b 168 opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, s->sample_buf[ch],
ba821b09 169 s->frame_size,
9a9964c5
JR
170 s->min_prediction_order,
171 s->max_prediction_order,
23940f14 172 ALAC_MAX_LPC_PRECISION, coefs, shift,
188dea1d 173 FF_LPC_TYPE_LEVINSON, 0,
9a9964c5
JR
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));
d6eee9f3 179 }
0d962ecb
RP
180}
181
ca048266
RP
182static 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;
d0fd6fc2 191 for (i = 2; i < n; i++) {
fc9cf0b2
JR
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];
ca048266
RP
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;
d0fd6fc2 208 for (i = 1; i < 4; i++) {
fc9cf0b2 209 if (score[i] < score[best])
ca048266 210 best = i;
ca048266 211 }
0d962ecb
RP
212 return best;
213}
214
215static void alac_stereo_decorrelation(AlacEncodeContext *s)
216{
217 int32_t *left = s->sample_buf[0], *right = s->sample_buf[1];
ba821b09 218 int i, mode, n = s->frame_size;
0d962ecb
RP
219 int32_t tmp;
220
221 mode = estimate_stereo_mode(left, right, n);
222
fc9cf0b2
JR
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;
0d962ecb
RP
252 }
253}
ca048266 254
e13894e8
JM
255static void alac_linear_predictor(AlacEncodeContext *s, int ch)
256{
257 int i;
56f8952b 258 AlacLPCContext lpc = s->lpc[ch];
e13894e8 259
d0fd6fc2 260 if (lpc.lpc_order == 31) {
e13894e8
JM
261 s->predictor_buf[0] = s->sample_buf[ch][0];
262
ba821b09 263 for (i = 1; i < s->frame_size; i++) {
fc9cf0b2
JR
264 s->predictor_buf[i] = s->sample_buf[ch][i ] -
265 s->sample_buf[ch][i - 1];
266 }
e13894e8
JM
267
268 return;
269 }
270
271 // generalised linear predictor
272
d0fd6fc2 273 if (lpc.lpc_order > 0) {
e13894e8
JM
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];
d0fd6fc2 279 for (i = 1; i <= lpc.lpc_order; i++)
e13894e8
JM
280 residual[i] = samples[i] - samples[i-1];
281
282 // perform lpc on remaining samples
ba821b09 283 for (i = lpc.lpc_order + 1; i < s->frame_size; i++) {
e13894e8
JM
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]) *
fc9cf0b2 288 lpc.lpc_coeff[j];
e13894e8
JM
289 }
290
291 sum >>= lpc.lpc_quant;
292 sum += samples[0];
4f46099b
BF
293 residual[i] = sign_extend(samples[lpc.lpc_order+1] - sum,
294 s->write_sample_size);
e13894e8
JM
295 res_val = residual[i];
296
fc9cf0b2 297 if (res_val) {
e13894e8
JM
298 int index = lpc.lpc_order - 1;
299 int neg = (res_val < 0);
300
fc9cf0b2
JR
301 while (index >= 0 && (neg ? (res_val < 0) : (res_val > 0))) {
302 int val = samples[0] - samples[lpc.lpc_order - index];
e13894e8
JM
303 int sign = (val ? FFSIGN(val) : 0);
304
fc9cf0b2
JR
305 if (neg)
306 sign *= -1;
e13894e8
JM
307
308 lpc.lpc_coeff[index] -= sign;
309 val *= sign;
fc9cf0b2 310 res_val -= (val >> lpc.lpc_quant) * (lpc.lpc_order - index);
e13894e8
JM
311 index--;
312 }
313 }
314 samples++;
315 }
316 }
317}
318
319static 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
ba821b09 325 for (i = 0; i < s->frame_size;) {
e13894e8
JM
326 int x;
327
328 k = av_log2((history >> 9) + 3);
329
fc9cf0b2
JR
330 x = -2 * (*samples) -1;
331 x ^= x >> 31;
e13894e8
JM
332
333 samples++;
334 i++;
335
336 encode_scalar(s, x - sign_modifier, k, s->write_sample_size);
337
fc9cf0b2
JR
338 history += x * s->rc.history_mult -
339 ((history * s->rc.history_mult) >> 9);
e13894e8
JM
340
341 sign_modifier = 0;
d0fd6fc2 342 if (x > 0xFFFF)
e13894e8
JM
343 history = 0xFFFF;
344
ba821b09 345 if (history < 128 && i < s->frame_size) {
e13894e8
JM
346 unsigned int block_size = 0;
347
348 k = 7 - av_log2(history) + ((history + 16) >> 6);
349
ba821b09 350 while (*samples == 0 && i < s->frame_size) {
e13894e8
JM
351 samples++;
352 i++;
353 block_size++;
354 }
355 encode_scalar(s, block_size, k, 16);
e13894e8 356 sign_modifier = (block_size <= 0xFFFF);
e13894e8
JM
357 history = 0;
358 }
359
360 }
361}
362
5e1bbb8c
JR
363static void write_element(AlacEncodeContext *s,
364 enum AlacRawDataBlockType element, int instance,
365 const uint8_t *samples0, const uint8_t *samples1)
6b931476 366{
5e1bbb8c
JR
367 uint8_t const *samples[2] = { samples0, samples1 };
368 int i, j, channels;
bb63475a 369 int prediction_type = 0;
b6e8ff72
JR
370 PutBitContext *pb = &s->pbctx;
371
5e1bbb8c 372 channels = element == TYPE_CPE ? 2 : 1;
b6e8ff72
JR
373
374 if (s->verbatim) {
5e1bbb8c 375 write_element_header(s, element, instance);
f24cc1b2 376 /* samples are channel-interleaved in verbatim mode */
7c278d2a
JR
377 if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
378 int shift = 32 - s->avctx->bits_per_raw_sample;
5e1bbb8c
JR
379 int32_t const *samples_s32[2] = { (const int32_t *)samples0,
380 (const int32_t *)samples1 };
7c278d2a 381 for (i = 0; i < s->frame_size; i++)
5e1bbb8c 382 for (j = 0; j < channels; j++)
7c278d2a
JR
383 put_sbits(pb, s->avctx->bits_per_raw_sample,
384 samples_s32[j][i] >> shift);
385 } else {
5e1bbb8c
JR
386 int16_t const *samples_s16[2] = { (const int16_t *)samples0,
387 (const int16_t *)samples1 };
7c278d2a 388 for (i = 0; i < s->frame_size; i++)
5e1bbb8c 389 for (j = 0; j < channels; j++)
7c278d2a
JR
390 put_sbits(pb, s->avctx->bits_per_raw_sample,
391 samples_s16[j][i]);
392 }
b6e8ff72 393 } else {
5e1bbb8c
JR
394 s->write_sample_size = s->avctx->bits_per_raw_sample - s->extra_bits +
395 channels - 1;
6b931476 396
5e1bbb8c
JR
397 init_sample_buffers(s, channels, samples);
398 write_element_header(s, element, instance);
399
400 if (channels == 2)
bee80054 401 alac_stereo_decorrelation(s);
5e1bbb8c
JR
402 else
403 s->interlacing_shift = s->interlacing_leftweight = 0;
bee80054
JR
404 put_bits(pb, 8, s->interlacing_shift);
405 put_bits(pb, 8, s->interlacing_leftweight);
6b931476 406
5e1bbb8c 407 for (i = 0; i < channels; i++) {
bee80054 408 calc_predictor_params(s, i);
6b931476 409
bee80054
JR
410 put_bits(pb, 4, prediction_type);
411 put_bits(pb, 4, s->lpc[i].lpc_quant);
6b931476 412
bee80054
JR
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 }
6b931476 419
7c278d2a
JR
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++) {
5e1bbb8c 424 for (j = 0; j < channels; j++) {
7c278d2a
JR
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
bee80054 431 // apply lpc and entropy coding to audio samples
5e1bbb8c 432 for (i = 0; i < channels; i++) {
bee80054 433 alac_linear_predictor(s, i);
bb63475a 434
bee80054
JR
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 }
bee80054
JR
441 alac_entropy_coder(s);
442 }
b6e8ff72 443 }
5e1bbb8c
JR
444}
445
446static 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);
b6e8ff72 472 flush_put_bits(pb);
5e1bbb8c 473
b6e8ff72 474 return put_bits_count(pb) >> 3;
6b931476
RP
475}
476
302daf58
JR
477static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps)
478{
b590f3a7
JR
479 int header_bits = 23 + 32 * (frame_size < DEFAULT_FRAME_SIZE);
480 return FFALIGN(header_bits + bps * ch * frame_size + 3, 8) / 8;
302daf58
JR
481}
482
51c24838
JR
483static 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;
51c24838
JR
489 return 0;
490}
491
6b931476
RP
492static av_cold int alac_encode_init(AVCodecContext *avctx)
493{
fc9cf0b2 494 AlacEncodeContext *s = avctx->priv_data;
77a78e9b 495 int ret;
51c24838 496 uint8_t *alac_extradata;
6b931476 497
ba821b09 498 avctx->frame_size = s->frame_size = DEFAULT_FRAME_SIZE;
6b931476 499
7c278d2a
JR
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
6b931476 509 // Set default compression level
d0fd6fc2 510 if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
d6eee9f3 511 s->compression_level = 2;
6b931476 512 else
d6eee9f3 513 s->compression_level = av_clip(avctx->compression_level, 0, 2);
6b931476
RP
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
302daf58
JR
521 s->max_coded_frame_size = get_max_frame_size(avctx->frame_size,
522 avctx->channels,
7c278d2a 523 avctx->bits_per_raw_sample);
6b931476 524
059a9348 525 avctx->extradata = av_mallocz(ALAC_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
51c24838
JR
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;
6b931476
RP
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);
7c278d2a 536 AV_WB8 (alac_extradata+17, avctx->bits_per_raw_sample);
ca048266 537 AV_WB8 (alac_extradata+21, avctx->channels);
6b931476 538 AV_WB32(alac_extradata+24, s->max_coded_frame_size);
d0fd6fc2 539 AV_WB32(alac_extradata+28,
7c278d2a 540 avctx->sample_rate * avctx->channels * avctx->bits_per_raw_sample); // average bitrate
ca048266 541 AV_WB32(alac_extradata+32, avctx->sample_rate);
6b931476
RP
542
543 // Set relevant extradata fields
d0fd6fc2 544 if (s->compression_level > 0) {
6b931476
RP
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
243df135
VG
550#if FF_API_PRIVATE_OPT
551FF_DISABLE_DEPRECATION_WARNINGS
d0fd6fc2
NAM
552 if (avctx->min_prediction_order >= 0) {
553 if (avctx->min_prediction_order < MIN_LPC_ORDER ||
a1ab56c5 554 avctx->min_prediction_order > ALAC_MAX_LPC_ORDER) {
d0fd6fc2
NAM
555 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
556 avctx->min_prediction_order);
51c24838
JR
557 ret = AVERROR(EINVAL);
558 goto error;
0d962ecb
RP
559 }
560
561 s->min_prediction_order = avctx->min_prediction_order;
562 }
563
d0fd6fc2
NAM
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);
51c24838
JR
569 ret = AVERROR(EINVAL);
570 goto error;
0d962ecb
RP
571 }
572
573 s->max_prediction_order = avctx->max_prediction_order;
574 }
243df135
VG
575FF_ENABLE_DEPRECATION_WARNINGS
576#endif
0d962ecb 577
d0fd6fc2
NAM
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",
0d962ecb 581 s->min_prediction_order, s->max_prediction_order);
51c24838
JR
582 ret = AVERROR(EINVAL);
583 goto error;
0d962ecb
RP
584 }
585
6b931476 586 s->avctx = avctx;
6b931476 587
51c24838
JR
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;
595error:
596 alac_encode_close(avctx);
77a78e9b 597 return ret;
6b931476
RP
598}
599
764852d6
JR
600static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
601 const AVFrame *frame, int *got_packet_ptr)
6b931476
RP
602{
603 AlacEncodeContext *s = avctx->priv_data;
764852d6 604 int out_bytes, max_frame_size, ret;
302daf58 605
764852d6 606 s->frame_size = frame->nb_samples;
ba821b09 607
ec7a212f 608 if (frame->nb_samples < DEFAULT_FRAME_SIZE)
ba821b09 609 max_frame_size = get_max_frame_size(s->frame_size, avctx->channels,
7c278d2a 610 avctx->bits_per_raw_sample);
302daf58
JR
611 else
612 max_frame_size = s->max_coded_frame_size;
6b931476 613
764852d6
JR
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;
ca048266
RP
617 }
618
b6e8ff72 619 /* use verbatim mode for compression_level 0 */
7c278d2a
JR
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 }
0d962ecb 627
7c278d2a 628 out_bytes = write_frame(s, avpkt, frame->extended_data);
ca048266 629
302daf58 630 if (out_bytes > max_frame_size) {
ca048266 631 /* frame too large. use verbatim mode */
b6e8ff72 632 s->verbatim = 1;
7c278d2a 633 s->extra_bits = 0;
7c278d2a 634 out_bytes = write_frame(s, avpkt, frame->extended_data);
ca048266
RP
635 }
636
764852d6
JR
637 avpkt->size = out_bytes;
638 *got_packet_ptr = 1;
639 return 0;
ca048266
RP
640}
641
243df135
VG
642#define OFFSET(x) offsetof(AlacEncodeContext, x)
643#define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
644static 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
651static 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
d36beb3f 658AVCodec ff_alac_encoder = {
ec6402b7 659 .name = "alac",
b2bed932 660 .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
ec6402b7 661 .type = AVMEDIA_TYPE_AUDIO,
36ef5369 662 .id = AV_CODEC_ID_ALAC,
ec6402b7 663 .priv_data_size = sizeof(AlacEncodeContext),
243df135 664 .priv_class = &alacenc_class,
ec6402b7 665 .init = alac_encode_init,
764852d6 666 .encode2 = alac_encode_frame,
ec6402b7 667 .close = alac_encode_close,
def97856 668 .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
5e1bbb8c 669 .channel_layouts = ff_alac_channel_layouts,
7c278d2a
JR
670 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32P,
671 AV_SAMPLE_FMT_S16P,
fc9cf0b2 672 AV_SAMPLE_FMT_NONE },
6b931476 673};