flacdec: change frame bps validation to return an error value if bps
[FFMpeg-mirror/lagarith.git] / libavcodec / alacenc.c
blob886b6c68ba7f54234b099b90159e296a91d54386
1 /**
2 * ALAC audio encoder
3 * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "avcodec.h"
23 #include "bitstream.h"
24 #include "dsputil.h"
25 #include "lpc.h"
26 #include "mathops.h"
28 #define DEFAULT_FRAME_SIZE 4096
29 #define DEFAULT_SAMPLE_SIZE 16
30 #define MAX_CHANNELS 8
31 #define ALAC_EXTRADATA_SIZE 36
32 #define ALAC_FRAME_HEADER_SIZE 55
33 #define ALAC_FRAME_FOOTER_SIZE 3
35 #define ALAC_ESCAPE_CODE 0x1FF
36 #define ALAC_MAX_LPC_ORDER 30
37 #define DEFAULT_MAX_PRED_ORDER 6
38 #define DEFAULT_MIN_PRED_ORDER 4
39 #define ALAC_MAX_LPC_PRECISION 9
40 #define ALAC_MAX_LPC_SHIFT 9
42 #define ALAC_CHMODE_LEFT_RIGHT 0
43 #define ALAC_CHMODE_LEFT_SIDE 1
44 #define ALAC_CHMODE_RIGHT_SIDE 2
45 #define ALAC_CHMODE_MID_SIDE 3
47 typedef struct RiceContext {
48 int history_mult;
49 int initial_history;
50 int k_modifier;
51 int rice_modifier;
52 } RiceContext;
54 typedef struct LPCContext {
55 int lpc_order;
56 int lpc_coeff[ALAC_MAX_LPC_ORDER+1];
57 int lpc_quant;
58 } LPCContext;
60 typedef struct AlacEncodeContext {
61 int compression_level;
62 int min_prediction_order;
63 int max_prediction_order;
64 int max_coded_frame_size;
65 int write_sample_size;
66 int32_t sample_buf[MAX_CHANNELS][DEFAULT_FRAME_SIZE];
67 int32_t predictor_buf[DEFAULT_FRAME_SIZE];
68 int interlacing_shift;
69 int interlacing_leftweight;
70 PutBitContext pbctx;
71 RiceContext rc;
72 LPCContext lpc[MAX_CHANNELS];
73 DSPContext dspctx;
74 AVCodecContext *avctx;
75 } AlacEncodeContext;
78 static void init_sample_buffers(AlacEncodeContext *s, int16_t *input_samples)
80 int ch, i;
82 for(ch=0;ch<s->avctx->channels;ch++) {
83 int16_t *sptr = input_samples + ch;
84 for(i=0;i<s->avctx->frame_size;i++) {
85 s->sample_buf[ch][i] = *sptr;
86 sptr += s->avctx->channels;
91 static void encode_scalar(AlacEncodeContext *s, int x, int k, int write_sample_size)
93 int divisor, q, r;
95 k = FFMIN(k, s->rc.k_modifier);
96 divisor = (1<<k) - 1;
97 q = x / divisor;
98 r = x % divisor;
100 if(q > 8) {
101 // write escape code and sample value directly
102 put_bits(&s->pbctx, 9, ALAC_ESCAPE_CODE);
103 put_bits(&s->pbctx, write_sample_size, x);
104 } else {
105 if(q)
106 put_bits(&s->pbctx, q, (1<<q) - 1);
107 put_bits(&s->pbctx, 1, 0);
109 if(k != 1) {
110 if(r > 0)
111 put_bits(&s->pbctx, k, r+1);
112 else
113 put_bits(&s->pbctx, k-1, 0);
118 static void write_frame_header(AlacEncodeContext *s, int is_verbatim)
120 put_bits(&s->pbctx, 3, s->avctx->channels-1); // No. of channels -1
121 put_bits(&s->pbctx, 16, 0); // Seems to be zero
122 put_bits(&s->pbctx, 1, 1); // Sample count is in the header
123 put_bits(&s->pbctx, 2, 0); // FIXME: Wasted bytes field
124 put_bits(&s->pbctx, 1, is_verbatim); // Audio block is verbatim
125 put_bits(&s->pbctx, 32, s->avctx->frame_size); // No. of samples in the frame
128 static void calc_predictor_params(AlacEncodeContext *s, int ch)
130 int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
131 int shift[MAX_LPC_ORDER];
132 int opt_order;
134 opt_order = ff_lpc_calc_coefs(&s->dspctx, s->sample_buf[ch], s->avctx->frame_size, s->min_prediction_order, s->max_prediction_order,
135 ALAC_MAX_LPC_PRECISION, coefs, shift, 1, ORDER_METHOD_EST, ALAC_MAX_LPC_SHIFT, 1);
137 s->lpc[ch].lpc_order = opt_order;
138 s->lpc[ch].lpc_quant = shift[opt_order-1];
139 memcpy(s->lpc[ch].lpc_coeff, coefs[opt_order-1], opt_order*sizeof(int));
142 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
144 int i, best;
145 int32_t lt, rt;
146 uint64_t sum[4];
147 uint64_t score[4];
149 /* calculate sum of 2nd order residual for each channel */
150 sum[0] = sum[1] = sum[2] = sum[3] = 0;
151 for(i=2; i<n; i++) {
152 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
153 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
154 sum[2] += FFABS((lt + rt) >> 1);
155 sum[3] += FFABS(lt - rt);
156 sum[0] += FFABS(lt);
157 sum[1] += FFABS(rt);
160 /* calculate score for each mode */
161 score[0] = sum[0] + sum[1];
162 score[1] = sum[0] + sum[3];
163 score[2] = sum[1] + sum[3];
164 score[3] = sum[2] + sum[3];
166 /* return mode with lowest score */
167 best = 0;
168 for(i=1; i<4; i++) {
169 if(score[i] < score[best]) {
170 best = i;
173 return best;
176 static void alac_stereo_decorrelation(AlacEncodeContext *s)
178 int32_t *left = s->sample_buf[0], *right = s->sample_buf[1];
179 int i, mode, n = s->avctx->frame_size;
180 int32_t tmp;
182 mode = estimate_stereo_mode(left, right, n);
184 switch(mode)
186 case ALAC_CHMODE_LEFT_RIGHT:
187 s->interlacing_leftweight = 0;
188 s->interlacing_shift = 0;
189 break;
191 case ALAC_CHMODE_LEFT_SIDE:
192 for(i=0; i<n; i++) {
193 right[i] = left[i] - right[i];
195 s->interlacing_leftweight = 1;
196 s->interlacing_shift = 0;
197 break;
199 case ALAC_CHMODE_RIGHT_SIDE:
200 for(i=0; i<n; i++) {
201 tmp = right[i];
202 right[i] = left[i] - right[i];
203 left[i] = tmp + (right[i] >> 31);
205 s->interlacing_leftweight = 1;
206 s->interlacing_shift = 31;
207 break;
209 default:
210 for(i=0; i<n; i++) {
211 tmp = left[i];
212 left[i] = (tmp + right[i]) >> 1;
213 right[i] = tmp - right[i];
215 s->interlacing_leftweight = 1;
216 s->interlacing_shift = 1;
217 break;
221 static void alac_linear_predictor(AlacEncodeContext *s, int ch)
223 int i;
224 LPCContext lpc = s->lpc[ch];
226 if(lpc.lpc_order == 31) {
227 s->predictor_buf[0] = s->sample_buf[ch][0];
229 for(i=1; i<s->avctx->frame_size; i++)
230 s->predictor_buf[i] = s->sample_buf[ch][i] - s->sample_buf[ch][i-1];
232 return;
235 // generalised linear predictor
237 if(lpc.lpc_order > 0) {
238 int32_t *samples = s->sample_buf[ch];
239 int32_t *residual = s->predictor_buf;
241 // generate warm-up samples
242 residual[0] = samples[0];
243 for(i=1;i<=lpc.lpc_order;i++)
244 residual[i] = samples[i] - samples[i-1];
246 // perform lpc on remaining samples
247 for(i = lpc.lpc_order + 1; i < s->avctx->frame_size; i++) {
248 int sum = 1 << (lpc.lpc_quant - 1), res_val, j;
250 for (j = 0; j < lpc.lpc_order; j++) {
251 sum += (samples[lpc.lpc_order-j] - samples[0]) *
252 lpc.lpc_coeff[j];
255 sum >>= lpc.lpc_quant;
256 sum += samples[0];
257 residual[i] = sign_extend(samples[lpc.lpc_order+1] - sum,
258 s->write_sample_size);
259 res_val = residual[i];
261 if(res_val) {
262 int index = lpc.lpc_order - 1;
263 int neg = (res_val < 0);
265 while(index >= 0 && (neg ? (res_val < 0):(res_val > 0))) {
266 int val = samples[0] - samples[lpc.lpc_order - index];
267 int sign = (val ? FFSIGN(val) : 0);
269 if(neg)
270 sign*=-1;
272 lpc.lpc_coeff[index] -= sign;
273 val *= sign;
274 res_val -= ((val >> lpc.lpc_quant) *
275 (lpc.lpc_order - index));
276 index--;
279 samples++;
284 static void alac_entropy_coder(AlacEncodeContext *s)
286 unsigned int history = s->rc.initial_history;
287 int sign_modifier = 0, i, k;
288 int32_t *samples = s->predictor_buf;
290 for(i=0;i < s->avctx->frame_size;) {
291 int x;
293 k = av_log2((history >> 9) + 3);
295 x = -2*(*samples)-1;
296 x ^= (x>>31);
298 samples++;
299 i++;
301 encode_scalar(s, x - sign_modifier, k, s->write_sample_size);
303 history += x * s->rc.history_mult
304 - ((history * s->rc.history_mult) >> 9);
306 sign_modifier = 0;
307 if(x > 0xFFFF)
308 history = 0xFFFF;
310 if((history < 128) && (i < s->avctx->frame_size)) {
311 unsigned int block_size = 0;
313 k = 7 - av_log2(history) + ((history + 16) >> 6);
315 while((*samples == 0) && (i < s->avctx->frame_size)) {
316 samples++;
317 i++;
318 block_size++;
320 encode_scalar(s, block_size, k, 16);
322 sign_modifier = (block_size <= 0xFFFF);
324 history = 0;
330 static void write_compressed_frame(AlacEncodeContext *s)
332 int i, j;
334 /* only simple mid/side decorrelation supported as of now */
335 if(s->avctx->channels == 2)
336 alac_stereo_decorrelation(s);
337 put_bits(&s->pbctx, 8, s->interlacing_shift);
338 put_bits(&s->pbctx, 8, s->interlacing_leftweight);
340 for(i=0;i<s->avctx->channels;i++) {
342 calc_predictor_params(s, i);
344 put_bits(&s->pbctx, 4, 0); // prediction type : currently only type 0 has been RE'd
345 put_bits(&s->pbctx, 4, s->lpc[i].lpc_quant);
347 put_bits(&s->pbctx, 3, s->rc.rice_modifier);
348 put_bits(&s->pbctx, 5, s->lpc[i].lpc_order);
349 // predictor coeff. table
350 for(j=0;j<s->lpc[i].lpc_order;j++) {
351 put_sbits(&s->pbctx, 16, s->lpc[i].lpc_coeff[j]);
355 // apply lpc and entropy coding to audio samples
357 for(i=0;i<s->avctx->channels;i++) {
358 alac_linear_predictor(s, i);
359 alac_entropy_coder(s);
363 static av_cold int alac_encode_init(AVCodecContext *avctx)
365 AlacEncodeContext *s = avctx->priv_data;
366 uint8_t *alac_extradata = av_mallocz(ALAC_EXTRADATA_SIZE+1);
368 avctx->frame_size = DEFAULT_FRAME_SIZE;
369 avctx->bits_per_coded_sample = DEFAULT_SAMPLE_SIZE;
371 if(avctx->sample_fmt != SAMPLE_FMT_S16) {
372 av_log(avctx, AV_LOG_ERROR, "only pcm_s16 input samples are supported\n");
373 return -1;
376 // Set default compression level
377 if(avctx->compression_level == FF_COMPRESSION_DEFAULT)
378 s->compression_level = 1;
379 else
380 s->compression_level = av_clip(avctx->compression_level, 0, 1);
382 // Initialize default Rice parameters
383 s->rc.history_mult = 40;
384 s->rc.initial_history = 10;
385 s->rc.k_modifier = 14;
386 s->rc.rice_modifier = 4;
388 s->max_coded_frame_size = (ALAC_FRAME_HEADER_SIZE + ALAC_FRAME_FOOTER_SIZE +
389 avctx->frame_size*avctx->channels*avctx->bits_per_coded_sample)>>3;
391 s->write_sample_size = avctx->bits_per_coded_sample + avctx->channels - 1; // FIXME: consider wasted_bytes
393 AV_WB32(alac_extradata, ALAC_EXTRADATA_SIZE);
394 AV_WB32(alac_extradata+4, MKBETAG('a','l','a','c'));
395 AV_WB32(alac_extradata+12, avctx->frame_size);
396 AV_WB8 (alac_extradata+17, avctx->bits_per_coded_sample);
397 AV_WB8 (alac_extradata+21, avctx->channels);
398 AV_WB32(alac_extradata+24, s->max_coded_frame_size);
399 AV_WB32(alac_extradata+28, avctx->sample_rate*avctx->channels*avctx->bits_per_coded_sample); // average bitrate
400 AV_WB32(alac_extradata+32, avctx->sample_rate);
402 // Set relevant extradata fields
403 if(s->compression_level > 0) {
404 AV_WB8(alac_extradata+18, s->rc.history_mult);
405 AV_WB8(alac_extradata+19, s->rc.initial_history);
406 AV_WB8(alac_extradata+20, s->rc.k_modifier);
409 s->min_prediction_order = DEFAULT_MIN_PRED_ORDER;
410 if(avctx->min_prediction_order >= 0) {
411 if(avctx->min_prediction_order < MIN_LPC_ORDER ||
412 avctx->min_prediction_order > ALAC_MAX_LPC_ORDER) {
413 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n", avctx->min_prediction_order);
414 return -1;
417 s->min_prediction_order = avctx->min_prediction_order;
420 s->max_prediction_order = DEFAULT_MAX_PRED_ORDER;
421 if(avctx->max_prediction_order >= 0) {
422 if(avctx->max_prediction_order < MIN_LPC_ORDER ||
423 avctx->max_prediction_order > ALAC_MAX_LPC_ORDER) {
424 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n", avctx->max_prediction_order);
425 return -1;
428 s->max_prediction_order = avctx->max_prediction_order;
431 if(s->max_prediction_order < s->min_prediction_order) {
432 av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
433 s->min_prediction_order, s->max_prediction_order);
434 return -1;
437 avctx->extradata = alac_extradata;
438 avctx->extradata_size = ALAC_EXTRADATA_SIZE;
440 avctx->coded_frame = avcodec_alloc_frame();
441 avctx->coded_frame->key_frame = 1;
443 s->avctx = avctx;
444 dsputil_init(&s->dspctx, avctx);
446 return 0;
449 static int alac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
450 int buf_size, void *data)
452 AlacEncodeContext *s = avctx->priv_data;
453 PutBitContext *pb = &s->pbctx;
454 int i, out_bytes, verbatim_flag = 0;
456 if(avctx->frame_size > DEFAULT_FRAME_SIZE) {
457 av_log(avctx, AV_LOG_ERROR, "input frame size exceeded\n");
458 return -1;
461 if(buf_size < 2*s->max_coded_frame_size) {
462 av_log(avctx, AV_LOG_ERROR, "buffer size is too small\n");
463 return -1;
466 verbatim:
467 init_put_bits(pb, frame, buf_size);
469 if((s->compression_level == 0) || verbatim_flag) {
470 // Verbatim mode
471 int16_t *samples = data;
472 write_frame_header(s, 1);
473 for(i=0; i<avctx->frame_size*avctx->channels; i++) {
474 put_sbits(pb, 16, *samples++);
476 } else {
477 init_sample_buffers(s, data);
478 write_frame_header(s, 0);
479 write_compressed_frame(s);
482 put_bits(pb, 3, 7);
483 flush_put_bits(pb);
484 out_bytes = put_bits_count(pb) >> 3;
486 if(out_bytes > s->max_coded_frame_size) {
487 /* frame too large. use verbatim mode */
488 if(verbatim_flag || (s->compression_level == 0)) {
489 /* still too large. must be an error. */
490 av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
491 return -1;
493 verbatim_flag = 1;
494 goto verbatim;
497 return out_bytes;
500 static av_cold int alac_encode_close(AVCodecContext *avctx)
502 av_freep(&avctx->extradata);
503 avctx->extradata_size = 0;
504 av_freep(&avctx->coded_frame);
505 return 0;
508 AVCodec alac_encoder = {
509 "alac",
510 CODEC_TYPE_AUDIO,
511 CODEC_ID_ALAC,
512 sizeof(AlacEncodeContext),
513 alac_encode_init,
514 alac_encode_frame,
515 alac_encode_close,
516 .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
517 .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),