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
25 * @author Jeff Muizelaar
31 #include "bytestream.h"
36 #define MAX_CHANNELS 8
37 #define MAX_BLOCKSIZE 65535
39 #define OUT_BUFFER_SIZE 16384
43 #define WAVE_FORMAT_PCM 0x0001
45 #define DEFAULT_BLOCK_SIZE 256
51 #define BITSHIFTSIZE 2
60 #define V2LPCQOFFSET (1 << LPCQUANT)
68 #define FN_BLOCKSIZE 5
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;
86 int min_framesize, max_framesize;
89 int32_t *decoded[MAX_CHANNELS];
90 int32_t *decoded_base[MAX_CHANNELS];
91 int32_t *offset[MAX_CHANNELS];
96 unsigned int allocated_bitstream_size;
98 uint8_t header[OUT_BUFFER_SIZE];
109 int got_quit_command;
112 static av_cold int shorten_decode_init(AVCodecContext * avctx)
114 ShortenContext *s = avctx->priv_data;
116 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
118 avcodec_get_frame_defaults(&s->frame);
119 avctx->coded_frame = &s->frame;
124 static int allocate_buffers(ShortenContext *s)
130 for (chan=0; chan<s->channels; chan++) {
131 if(FFMAX(1, s->nmean) >= UINT_MAX/sizeof(int32_t)){
132 av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
135 if(s->blocksize + s->nwrap >= UINT_MAX/sizeof(int32_t) || s->blocksize + s->nwrap <= (unsigned)s->nwrap){
136 av_log(s->avctx, AV_LOG_ERROR, "s->blocksize + s->nwrap too large\n");
140 tmp_ptr = av_realloc(s->offset[chan], sizeof(int32_t)*FFMAX(1, s->nmean));
142 return AVERROR(ENOMEM);
143 s->offset[chan] = tmp_ptr;
145 tmp_ptr = av_realloc(s->decoded_base[chan], (s->blocksize + s->nwrap) *
146 sizeof(s->decoded_base[0][0]));
148 return AVERROR(ENOMEM);
149 s->decoded_base[chan] = tmp_ptr;
150 for (i=0; i<s->nwrap; i++)
151 s->decoded_base[chan][i] = 0;
152 s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
155 coeffs = av_realloc(s->coeffs, s->nwrap * sizeof(*s->coeffs));
157 return AVERROR(ENOMEM);
164 static inline unsigned int get_uint(ShortenContext *s, int k)
167 k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
168 return get_ur_golomb_shorten(&s->gb, k);
172 static void fix_bitshift(ShortenContext *s, int32_t *buffer)
176 if (s->bitshift != 0)
177 for (i = 0; i < s->blocksize; i++)
178 buffer[i] <<= s->bitshift;
182 static int init_offset(ShortenContext *s)
186 int nblock = FFMAX(1, s->nmean);
187 /* initialise offset */
188 switch (s->internal_ftype)
195 av_log(s->avctx, AV_LOG_ERROR, "unknown audio type");
196 return AVERROR_INVALIDDATA;
199 for (chan = 0; chan < s->channels; chan++)
200 for (i = 0; i < nblock; i++)
201 s->offset[chan][i] = mean;
205 static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header,
212 if (bytestream_get_le32(&header) != MKTAG('R','I','F','F')) {
213 av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
217 header += 4; /* chunk size */;
219 if (bytestream_get_le32(&header) != MKTAG('W','A','V','E')) {
220 av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
224 while (bytestream_get_le32(&header) != MKTAG('f','m','t',' ')) {
225 len = bytestream_get_le32(&header);
228 len = bytestream_get_le32(&header);
231 av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
235 wave_format = bytestream_get_le16(&header);
237 switch (wave_format) {
238 case WAVE_FORMAT_PCM:
241 av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
245 header += 2; // skip channels (already got from shorten header)
246 avctx->sample_rate = bytestream_get_le32(&header);
247 header += 4; // skip bit rate (represents original uncompressed bit rate)
248 header += 2; // skip block align (not needed)
249 avctx->bits_per_coded_sample = bytestream_get_le16(&header);
251 if (avctx->bits_per_coded_sample != 16) {
252 av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
258 av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
263 static void output_buffer(int16_t **samples, int nchan, int blocksize,
267 for (ch = 0; ch < nchan; ch++) {
268 int32_t *in = buffer[ch];
269 int16_t *out = samples[ch];
270 for (i = 0; i < blocksize; i++)
271 out[i] = av_clip_int16(in[i]);
275 static const int fixed_coeffs[3][3] = {
281 static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
282 int residual_size, int32_t coffset)
284 int pred_order, sum, qshift, init_sum, i, j;
287 if (command == FN_QLPC) {
288 /* read/validate prediction order */
289 pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
290 if (pred_order > s->nwrap) {
291 av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n", pred_order);
292 return AVERROR(EINVAL);
294 /* read LPC coefficients */
295 for (i=0; i<pred_order; i++)
296 s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
301 /* fixed LPC coeffs */
302 pred_order = command;
303 coeffs = fixed_coeffs[pred_order-1];
307 /* subtract offset from previous samples to use in prediction */
308 if (command == FN_QLPC && coffset)
309 for (i = -pred_order; i < 0; i++)
310 s->decoded[channel][i] -= coffset;
312 /* decode residual and do LPC prediction */
313 init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
314 for (i=0; i < s->blocksize; i++) {
316 for (j=0; j<pred_order; j++)
317 sum += coeffs[j] * s->decoded[channel][i-j-1];
318 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + (sum >> qshift);
321 /* add offset to current samples */
322 if (command == FN_QLPC && coffset)
323 for (i = 0; i < s->blocksize; i++)
324 s->decoded[channel][i] += coffset;
329 static int read_header(ShortenContext *s)
333 /* shorten signature */
334 if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
335 av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
340 s->blocksize = DEFAULT_BLOCK_SIZE;
342 s->version = get_bits(&s->gb, 8);
343 s->internal_ftype = get_uint(s, TYPESIZE);
345 s->channels = get_uint(s, CHANSIZE);
346 if (s->channels <= 0 || s->channels > MAX_CHANNELS) {
347 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
350 s->avctx->channels = s->channels;
352 /* get blocksize if version > 0 */
353 if (s->version > 0) {
354 int skip_bytes, blocksize;
356 blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
357 if (!blocksize || blocksize > MAX_BLOCKSIZE) {
358 av_log(s->avctx, AV_LOG_ERROR, "invalid or unsupported block size: %d\n",
360 return AVERROR(EINVAL);
362 s->blocksize = blocksize;
364 maxnlpc = get_uint(s, LPCQSIZE);
365 s->nmean = get_uint(s, 0);
367 skip_bytes = get_uint(s, NSKIPSIZE);
368 for (i=0; i<skip_bytes; i++) {
369 skip_bits(&s->gb, 8);
372 s->nwrap = FFMAX(NWRAP, maxnlpc);
374 if ((ret = allocate_buffers(s)) < 0)
377 if ((ret = init_offset(s)) < 0)
381 s->lpcqoffset = V2LPCQOFFSET;
383 if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
384 av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at beginning of stream\n");
388 s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
389 if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
390 av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
394 for (i=0; i<s->header_size; i++)
395 s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
397 if (decode_wave_header(s->avctx, s->header, s->header_size) < 0)
408 static int shorten_decode_frame(AVCodecContext *avctx, void *data,
409 int *got_frame_ptr, AVPacket *avpkt)
411 const uint8_t *buf = avpkt->data;
412 int buf_size = avpkt->size;
413 ShortenContext *s = avctx->priv_data;
414 int i, input_buf_size = 0;
417 /* allocate internal bitstream buffer */
418 if(s->max_framesize == 0){
420 s->max_framesize= 1024; // should hopefully be enough for the first header
421 tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
424 av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
425 return AVERROR(ENOMEM);
427 s->bitstream = tmp_ptr;
430 /* append current packet data to bitstream buffer */
431 if(1 && s->max_framesize){//FIXME truncated
432 buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
433 input_buf_size= buf_size;
435 if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
436 memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
437 s->bitstream_index=0;
440 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
441 buf= &s->bitstream[s->bitstream_index];
442 buf_size += s->bitstream_size;
443 s->bitstream_size= buf_size;
445 /* do not decode until buffer has at least max_framesize bytes or
446 the end of the file has been reached */
447 if (buf_size < s->max_framesize && avpkt->data) {
449 return input_buf_size;
452 /* init and position bitstream reader */
453 init_get_bits(&s->gb, buf, buf_size*8);
454 skip_bits(&s->gb, s->bitindex);
456 /* process header or next subblock */
457 if (!s->got_header) {
458 if ((ret = read_header(s)) < 0)
464 /* if quit command was read previously, don't decode anything */
465 if (s->got_quit_command) {
471 while (s->cur_chan < s->channels) {
475 if (get_bits_left(&s->gb) < 3+FNSIZE) {
480 cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
482 if (cmd > FN_VERBATIM) {
483 av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
488 if (!is_audio_command[cmd]) {
489 /* process non-audio command */
492 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
494 get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
498 s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
501 int blocksize = get_uint(s, av_log2(s->blocksize));
502 if (blocksize > s->blocksize) {
503 av_log(avctx, AV_LOG_ERROR, "Increasing block size is not supported\n");
504 return AVERROR_PATCHWELCOME;
506 if (!blocksize || blocksize > MAX_BLOCKSIZE) {
507 av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
508 "block size: %d\n", blocksize);
509 return AVERROR(EINVAL);
511 s->blocksize = blocksize;
515 s->got_quit_command = 1;
518 if (cmd == FN_BLOCKSIZE || cmd == FN_QUIT) {
523 /* process audio command */
524 int residual_size = 0;
525 int channel = s->cur_chan;
528 /* get Rice code for residual decoding */
529 if (cmd != FN_ZERO) {
530 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
531 /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
536 /* calculate sample offset using means from previous blocks */
538 coffset = s->offset[channel][0];
540 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
541 for (i=0; i<s->nmean; i++)
542 sum += s->offset[channel][i];
543 coffset = sum / s->nmean;
545 coffset >>= FFMIN(1, s->bitshift);
548 /* decode samples for this channel */
549 if (cmd == FN_ZERO) {
550 for (i=0; i<s->blocksize; i++)
551 s->decoded[channel][i] = 0;
553 if ((ret = decode_subframe_lpc(s, cmd, channel, residual_size, coffset)) < 0)
557 /* update means with info from the current block */
559 int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
560 for (i=0; i<s->blocksize; i++)
561 sum += s->decoded[channel][i];
563 for (i=1; i<s->nmean; i++)
564 s->offset[channel][i-1] = s->offset[channel][i];
567 s->offset[channel][s->nmean - 1] = sum / s->blocksize;
569 s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
572 /* copy wrap samples for use with next block */
573 for (i=-s->nwrap; i<0; i++)
574 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
576 /* shift samples to add in unused zero bits which were removed
578 fix_bitshift(s, s->decoded[channel]);
580 /* if this is the last channel in the block, output the samples */
582 if (s->cur_chan == s->channels) {
583 /* get output buffer */
584 s->frame.nb_samples = s->blocksize;
585 if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
586 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
589 /* interleave output */
590 output_buffer((int16_t **)s->frame.extended_data, s->channels,
591 s->blocksize, s->decoded);
594 *(AVFrame *)data = s->frame;
598 if (s->cur_chan < s->channels)
602 s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
603 i= (get_bits_count(&s->gb))/8;
605 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
607 s->bitstream_index=0;
610 if (s->bitstream_size) {
611 s->bitstream_index += i;
612 s->bitstream_size -= i;
613 return input_buf_size;
618 static av_cold int shorten_decode_close(AVCodecContext *avctx)
620 ShortenContext *s = avctx->priv_data;
623 for (i = 0; i < s->channels; i++) {
624 s->decoded[i] = NULL;
625 av_freep(&s->decoded_base[i]);
626 av_freep(&s->offset[i]);
628 av_freep(&s->bitstream);
629 av_freep(&s->coeffs);
634 AVCodec ff_shorten_decoder = {
636 .type = AVMEDIA_TYPE_AUDIO,
637 .id = AV_CODEC_ID_SHORTEN,
638 .priv_data_size = sizeof(ShortenContext),
639 .init = shorten_decode_init,
640 .close = shorten_decode_close,
641 .decode = shorten_decode_frame,
642 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
643 .long_name = NULL_IF_CONFIG_SMALL("Shorten"),
644 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
645 AV_SAMPLE_FMT_NONE },