lavfi: switch to AVFrame.
[FFMpeg-mirror/mplayer-patches.git] / libavcodec / shorten.c
blobd6c9386c8aae882497c7c399a5a924abd14d0e44
1 /*
2 * Shorten decoder
3 * Copyright (c) 2005 Jeff Muizelaar
5 * This file is part of Libav.
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.
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.
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
22 /**
23 * @file
24 * Shorten decoder
25 * @author Jeff Muizelaar
29 #include <limits.h>
30 #include "avcodec.h"
31 #include "bytestream.h"
32 #include "get_bits.h"
33 #include "golomb.h"
34 #include "internal.h"
36 #define MAX_CHANNELS 8
37 #define MAX_BLOCKSIZE 65535
39 #define OUT_BUFFER_SIZE 16384
41 #define ULONGSIZE 2
43 #define WAVE_FORMAT_PCM 0x0001
45 #define DEFAULT_BLOCK_SIZE 256
47 #define TYPESIZE 4
48 #define CHANSIZE 0
49 #define LPCQSIZE 2
50 #define ENERGYSIZE 3
51 #define BITSHIFTSIZE 2
53 #define TYPE_S16HL 3
54 #define TYPE_S16LH 5
56 #define NWRAP 3
57 #define NSKIPSIZE 1
59 #define LPCQUANT 5
60 #define V2LPCQOFFSET (1 << LPCQUANT)
62 #define FNSIZE 2
63 #define FN_DIFF0 0
64 #define FN_DIFF1 1
65 #define FN_DIFF2 2
66 #define FN_DIFF3 3
67 #define FN_QUIT 4
68 #define FN_BLOCKSIZE 5
69 #define FN_BITSHIFT 6
70 #define FN_QLPC 7
71 #define FN_ZERO 8
72 #define FN_VERBATIM 9
74 /** indicates if the FN_* command is audio or non-audio */
75 static const uint8_t is_audio_command[10] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0 };
77 #define VERBATIM_CKSIZE_SIZE 5
78 #define VERBATIM_BYTE_SIZE 8
79 #define CANONICAL_HEADER_SIZE 44
81 typedef struct ShortenContext {
82 AVCodecContext *avctx;
83 GetBitContext gb;
85 int min_framesize, max_framesize;
86 unsigned channels;
88 int32_t *decoded[MAX_CHANNELS];
89 int32_t *decoded_base[MAX_CHANNELS];
90 int32_t *offset[MAX_CHANNELS];
91 int *coeffs;
92 uint8_t *bitstream;
93 int bitstream_size;
94 int bitstream_index;
95 unsigned int allocated_bitstream_size;
96 int header_size;
97 uint8_t header[OUT_BUFFER_SIZE];
98 int version;
99 int cur_chan;
100 int bitshift;
101 int nmean;
102 int internal_ftype;
103 int nwrap;
104 int blocksize;
105 int bitindex;
106 int32_t lpcqoffset;
107 int got_header;
108 int got_quit_command;
109 } ShortenContext;
111 static av_cold int shorten_decode_init(AVCodecContext *avctx)
113 ShortenContext *s = avctx->priv_data;
114 s->avctx = avctx;
115 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
117 return 0;
120 static int allocate_buffers(ShortenContext *s)
122 int i, chan;
123 int *coeffs;
124 void *tmp_ptr;
126 for (chan = 0; chan < s->channels; chan++) {
127 if (FFMAX(1, s->nmean) >= UINT_MAX / sizeof(int32_t)) {
128 av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
129 return AVERROR_INVALIDDATA;
131 if (s->blocksize + s->nwrap >= UINT_MAX / sizeof(int32_t) ||
132 s->blocksize + s->nwrap <= (unsigned)s->nwrap) {
133 av_log(s->avctx, AV_LOG_ERROR,
134 "s->blocksize + s->nwrap too large\n");
135 return AVERROR_INVALIDDATA;
138 tmp_ptr =
139 av_realloc(s->offset[chan], sizeof(int32_t) * FFMAX(1, s->nmean));
140 if (!tmp_ptr)
141 return AVERROR(ENOMEM);
142 s->offset[chan] = tmp_ptr;
144 tmp_ptr = av_realloc(s->decoded_base[chan], (s->blocksize + s->nwrap) *
145 sizeof(s->decoded_base[0][0]));
146 if (!tmp_ptr)
147 return AVERROR(ENOMEM);
148 s->decoded_base[chan] = tmp_ptr;
149 for (i = 0; i < s->nwrap; i++)
150 s->decoded_base[chan][i] = 0;
151 s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
154 coeffs = av_realloc(s->coeffs, s->nwrap * sizeof(*s->coeffs));
155 if (!coeffs)
156 return AVERROR(ENOMEM);
157 s->coeffs = coeffs;
159 return 0;
162 static inline unsigned int get_uint(ShortenContext *s, int k)
164 if (s->version != 0)
165 k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
166 return get_ur_golomb_shorten(&s->gb, k);
169 static void fix_bitshift(ShortenContext *s, int32_t *buffer)
171 int i;
173 if (s->bitshift != 0)
174 for (i = 0; i < s->blocksize; i++)
175 buffer[i] <<= s->bitshift;
178 static int init_offset(ShortenContext *s)
180 int32_t mean = 0;
181 int chan, i;
182 int nblock = FFMAX(1, s->nmean);
183 /* initialise offset */
184 switch (s->internal_ftype) {
185 case TYPE_S16HL:
186 case TYPE_S16LH:
187 mean = 0;
188 break;
189 default:
190 av_log(s->avctx, AV_LOG_ERROR, "unknown audio type");
191 return AVERROR_INVALIDDATA;
194 for (chan = 0; chan < s->channels; chan++)
195 for (i = 0; i < nblock; i++)
196 s->offset[chan][i] = mean;
197 return 0;
200 static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header,
201 int header_size)
203 int len;
204 short wave_format;
206 if (bytestream_get_le32(&header) != MKTAG('R', 'I', 'F', 'F')) {
207 av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
208 return AVERROR_INVALIDDATA;
211 header += 4; /* chunk size */
213 if (bytestream_get_le32(&header) != MKTAG('W', 'A', 'V', 'E')) {
214 av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
215 return AVERROR_INVALIDDATA;
218 while (bytestream_get_le32(&header) != MKTAG('f', 'm', 't', ' ')) {
219 len = bytestream_get_le32(&header);
220 header += len;
222 len = bytestream_get_le32(&header);
224 if (len < 16) {
225 av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
226 return AVERROR_INVALIDDATA;
229 wave_format = bytestream_get_le16(&header);
231 switch (wave_format) {
232 case WAVE_FORMAT_PCM:
233 break;
234 default:
235 av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
236 return AVERROR(ENOSYS);
239 header += 2; // skip channels (already got from shorten header)
240 avctx->sample_rate = bytestream_get_le32(&header);
241 header += 4; // skip bit rate (represents original uncompressed bit rate)
242 header += 2; // skip block align (not needed)
243 avctx->bits_per_coded_sample = bytestream_get_le16(&header);
245 if (avctx->bits_per_coded_sample != 16) {
246 av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
247 return AVERROR(ENOSYS);
250 len -= 16;
251 if (len > 0)
252 av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
254 return 0;
257 static void output_buffer(int16_t **samples, int nchan, int blocksize,
258 int32_t **buffer)
260 int i, ch;
261 for (ch = 0; ch < nchan; ch++) {
262 int32_t *in = buffer[ch];
263 int16_t *out = samples[ch];
264 for (i = 0; i < blocksize; i++)
265 out[i] = av_clip_int16(in[i]);
269 static const int fixed_coeffs[3][3] = {
270 { 1, 0, 0 },
271 { 2, -1, 0 },
272 { 3, -3, 1 }
275 static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
276 int residual_size, int32_t coffset)
278 int pred_order, sum, qshift, init_sum, i, j;
279 const int *coeffs;
281 if (command == FN_QLPC) {
282 /* read/validate prediction order */
283 pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
284 if (pred_order > s->nwrap) {
285 av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
286 pred_order);
287 return AVERROR(EINVAL);
289 /* read LPC coefficients */
290 for (i = 0; i < pred_order; i++)
291 s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
292 coeffs = s->coeffs;
294 qshift = LPCQUANT;
295 } else {
296 /* fixed LPC coeffs */
297 pred_order = command;
298 coeffs = fixed_coeffs[pred_order - 1];
299 qshift = 0;
302 /* subtract offset from previous samples to use in prediction */
303 if (command == FN_QLPC && coffset)
304 for (i = -pred_order; i < 0; i++)
305 s->decoded[channel][i] -= coffset;
307 /* decode residual and do LPC prediction */
308 init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
309 for (i = 0; i < s->blocksize; i++) {
310 sum = init_sum;
311 for (j = 0; j < pred_order; j++)
312 sum += coeffs[j] * s->decoded[channel][i - j - 1];
313 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) +
314 (sum >> qshift);
317 /* add offset to current samples */
318 if (command == FN_QLPC && coffset)
319 for (i = 0; i < s->blocksize; i++)
320 s->decoded[channel][i] += coffset;
322 return 0;
325 static int read_header(ShortenContext *s)
327 int i, ret;
328 int maxnlpc = 0;
329 /* shorten signature */
330 if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
331 av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
332 return AVERROR_INVALIDDATA;
335 s->lpcqoffset = 0;
336 s->blocksize = DEFAULT_BLOCK_SIZE;
337 s->nmean = -1;
338 s->version = get_bits(&s->gb, 8);
339 s->internal_ftype = get_uint(s, TYPESIZE);
341 s->channels = get_uint(s, CHANSIZE);
342 if (!s->channels) {
343 av_log(s->avctx, AV_LOG_ERROR, "No channels reported\n");
344 return AVERROR_INVALIDDATA;
346 if (s->channels > MAX_CHANNELS) {
347 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
348 s->channels = 0;
349 return AVERROR_INVALIDDATA;
351 s->avctx->channels = s->channels;
353 /* get blocksize if version > 0 */
354 if (s->version > 0) {
355 int skip_bytes;
356 unsigned blocksize;
358 blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
359 if (!blocksize || blocksize > MAX_BLOCKSIZE) {
360 av_log(s->avctx, AV_LOG_ERROR,
361 "invalid or unsupported block size: %d\n",
362 blocksize);
363 return AVERROR(EINVAL);
365 s->blocksize = blocksize;
367 maxnlpc = get_uint(s, LPCQSIZE);
368 s->nmean = get_uint(s, 0);
370 skip_bytes = get_uint(s, NSKIPSIZE);
371 for (i = 0; i < skip_bytes; i++)
372 skip_bits(&s->gb, 8);
374 s->nwrap = FFMAX(NWRAP, maxnlpc);
376 if ((ret = allocate_buffers(s)) < 0)
377 return ret;
379 if ((ret = init_offset(s)) < 0)
380 return ret;
382 if (s->version > 1)
383 s->lpcqoffset = V2LPCQOFFSET;
385 if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
386 av_log(s->avctx, AV_LOG_ERROR,
387 "missing verbatim section at beginning of stream\n");
388 return AVERROR_INVALIDDATA;
391 s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
392 if (s->header_size >= OUT_BUFFER_SIZE ||
393 s->header_size < CANONICAL_HEADER_SIZE) {
394 av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n",
395 s->header_size);
396 return AVERROR_INVALIDDATA;
399 for (i = 0; i < s->header_size; i++)
400 s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
402 if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0)
403 return ret;
405 s->cur_chan = 0;
406 s->bitshift = 0;
408 s->got_header = 1;
410 return 0;
413 static int shorten_decode_frame(AVCodecContext *avctx, void *data,
414 int *got_frame_ptr, AVPacket *avpkt)
416 AVFrame *frame = data;
417 const uint8_t *buf = avpkt->data;
418 int buf_size = avpkt->size;
419 ShortenContext *s = avctx->priv_data;
420 int i, input_buf_size = 0;
421 int ret;
423 /* allocate internal bitstream buffer */
424 if (s->max_framesize == 0) {
425 void *tmp_ptr;
426 s->max_framesize = 1024; // should hopefully be enough for the first header
427 tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
428 s->max_framesize);
429 if (!tmp_ptr) {
430 av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
431 return AVERROR(ENOMEM);
433 s->bitstream = tmp_ptr;
436 /* append current packet data to bitstream buffer */
437 if (1 && s->max_framesize) { //FIXME truncated
438 buf_size = FFMIN(buf_size, s->max_framesize - s->bitstream_size);
439 input_buf_size = buf_size;
441 if (s->bitstream_index + s->bitstream_size + buf_size >
442 s->allocated_bitstream_size) {
443 memmove(s->bitstream, &s->bitstream[s->bitstream_index],
444 s->bitstream_size);
445 s->bitstream_index = 0;
447 if (buf)
448 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf,
449 buf_size);
450 buf = &s->bitstream[s->bitstream_index];
451 buf_size += s->bitstream_size;
452 s->bitstream_size = buf_size;
454 /* do not decode until buffer has at least max_framesize bytes or
455 * the end of the file has been reached */
456 if (buf_size < s->max_framesize && avpkt->data) {
457 *got_frame_ptr = 0;
458 return input_buf_size;
461 /* init and position bitstream reader */
462 init_get_bits(&s->gb, buf, buf_size * 8);
463 skip_bits(&s->gb, s->bitindex);
465 /* process header or next subblock */
466 if (!s->got_header) {
467 if ((ret = read_header(s)) < 0)
468 return ret;
469 *got_frame_ptr = 0;
470 goto finish_frame;
473 /* if quit command was read previously, don't decode anything */
474 if (s->got_quit_command) {
475 *got_frame_ptr = 0;
476 return avpkt->size;
479 s->cur_chan = 0;
480 while (s->cur_chan < s->channels) {
481 unsigned cmd;
482 int len;
484 if (get_bits_left(&s->gb) < 3 + FNSIZE) {
485 *got_frame_ptr = 0;
486 break;
489 cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
491 if (cmd > FN_VERBATIM) {
492 av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
493 *got_frame_ptr = 0;
494 break;
497 if (!is_audio_command[cmd]) {
498 /* process non-audio command */
499 switch (cmd) {
500 case FN_VERBATIM:
501 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
502 while (len--)
503 get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
504 break;
505 case FN_BITSHIFT:
506 s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
507 break;
508 case FN_BLOCKSIZE: {
509 unsigned blocksize = get_uint(s, av_log2(s->blocksize));
510 if (blocksize > s->blocksize) {
511 av_log(avctx, AV_LOG_ERROR,
512 "Increasing block size is not supported\n");
513 return AVERROR_PATCHWELCOME;
515 if (!blocksize || blocksize > MAX_BLOCKSIZE) {
516 av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
517 "block size: %d\n", blocksize);
518 return AVERROR(EINVAL);
520 s->blocksize = blocksize;
521 break;
523 case FN_QUIT:
524 s->got_quit_command = 1;
525 break;
527 if (cmd == FN_BLOCKSIZE || cmd == FN_QUIT) {
528 *got_frame_ptr = 0;
529 break;
531 } else {
532 /* process audio command */
533 int residual_size = 0;
534 int channel = s->cur_chan;
535 int32_t coffset;
537 /* get Rice code for residual decoding */
538 if (cmd != FN_ZERO) {
539 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
540 /* This is a hack as version 0 differed in the definition
541 * of get_sr_golomb_shorten(). */
542 if (s->version == 0)
543 residual_size--;
546 /* calculate sample offset using means from previous blocks */
547 if (s->nmean == 0)
548 coffset = s->offset[channel][0];
549 else {
550 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
551 for (i = 0; i < s->nmean; i++)
552 sum += s->offset[channel][i];
553 coffset = sum / s->nmean;
554 if (s->version >= 2)
555 coffset >>= FFMIN(1, s->bitshift);
558 /* decode samples for this channel */
559 if (cmd == FN_ZERO) {
560 for (i = 0; i < s->blocksize; i++)
561 s->decoded[channel][i] = 0;
562 } else {
563 if ((ret = decode_subframe_lpc(s, cmd, channel,
564 residual_size, coffset)) < 0)
565 return ret;
568 /* update means with info from the current block */
569 if (s->nmean > 0) {
570 int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
571 for (i = 0; i < s->blocksize; i++)
572 sum += s->decoded[channel][i];
574 for (i = 1; i < s->nmean; i++)
575 s->offset[channel][i - 1] = s->offset[channel][i];
577 if (s->version < 2)
578 s->offset[channel][s->nmean - 1] = sum / s->blocksize;
579 else
580 s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
583 /* copy wrap samples for use with next block */
584 for (i = -s->nwrap; i < 0; i++)
585 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
587 /* shift samples to add in unused zero bits which were removed
588 * during encoding */
589 fix_bitshift(s, s->decoded[channel]);
591 /* if this is the last channel in the block, output the samples */
592 s->cur_chan++;
593 if (s->cur_chan == s->channels) {
594 /* get output buffer */
595 frame->nb_samples = s->blocksize;
596 if ((ret = ff_get_buffer(avctx, frame)) < 0) {
597 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
598 return ret;
600 /* interleave output */
601 output_buffer((int16_t **)frame->extended_data, s->channels,
602 s->blocksize, s->decoded);
604 *got_frame_ptr = 1;
608 if (s->cur_chan < s->channels)
609 *got_frame_ptr = 0;
611 finish_frame:
612 s->bitindex = get_bits_count(&s->gb) - 8 * (get_bits_count(&s->gb) / 8);
613 i = get_bits_count(&s->gb) / 8;
614 if (i > buf_size) {
615 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
616 s->bitstream_size = 0;
617 s->bitstream_index = 0;
618 return AVERROR_INVALIDDATA;
620 if (s->bitstream_size) {
621 s->bitstream_index += i;
622 s->bitstream_size -= i;
623 return input_buf_size;
624 } else
625 return i;
628 static av_cold int shorten_decode_close(AVCodecContext *avctx)
630 ShortenContext *s = avctx->priv_data;
631 int i;
633 for (i = 0; i < s->channels; i++) {
634 s->decoded[i] = NULL;
635 av_freep(&s->decoded_base[i]);
636 av_freep(&s->offset[i]);
638 av_freep(&s->bitstream);
639 av_freep(&s->coeffs);
641 return 0;
644 AVCodec ff_shorten_decoder = {
645 .name = "shorten",
646 .type = AVMEDIA_TYPE_AUDIO,
647 .id = AV_CODEC_ID_SHORTEN,
648 .priv_data_size = sizeof(ShortenContext),
649 .init = shorten_decode_init,
650 .close = shorten_decode_close,
651 .decode = shorten_decode_frame,
652 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
653 .long_name = NULL_IF_CONFIG_SMALL("Shorten"),
654 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
655 AV_SAMPLE_FMT_NONE },