3 * Copyright (c) 2005 Jeff Muizelaar
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
25 * @author Jeff Muizelaar
31 #include "bytestream.h"
35 #define MAX_CHANNELS 8
36 #define MAX_BLOCKSIZE 65535
38 #define OUT_BUFFER_SIZE 16384
42 #define WAVE_FORMAT_PCM 0x0001
44 #define DEFAULT_BLOCK_SIZE 256
50 #define BITSHIFTSIZE 2
59 #define V2LPCQOFFSET (1 << LPCQUANT)
67 #define FN_BLOCKSIZE 5
73 /** indicates if the FN_* command is audio or non-audio */
74 static const uint8_t is_audio_command[10] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0 };
76 #define VERBATIM_CKSIZE_SIZE 5
77 #define VERBATIM_BYTE_SIZE 8
78 #define CANONICAL_HEADER_SIZE 44
80 typedef struct ShortenContext {
81 AVCodecContext *avctx;
85 int min_framesize, max_framesize;
88 int32_t *decoded[MAX_CHANNELS];
89 int32_t *offset[MAX_CHANNELS];
94 unsigned int allocated_bitstream_size;
96 uint8_t header[OUT_BUFFER_SIZE];
107 int got_quit_command;
110 static av_cold int shorten_decode_init(AVCodecContext * avctx)
112 ShortenContext *s = avctx->priv_data;
114 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
116 avcodec_get_frame_defaults(&s->frame);
117 avctx->coded_frame = &s->frame;
122 static int allocate_buffers(ShortenContext *s)
128 for (chan=0; chan<s->channels; chan++) {
129 if(FFMAX(1, s->nmean) >= UINT_MAX/sizeof(int32_t)){
130 av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
133 if(s->blocksize + s->nwrap >= UINT_MAX/sizeof(int32_t) || s->blocksize + s->nwrap <= (unsigned)s->nwrap){
134 av_log(s->avctx, AV_LOG_ERROR, "s->blocksize + s->nwrap too large\n");
138 tmp_ptr = av_realloc(s->offset[chan], sizeof(int32_t)*FFMAX(1, s->nmean));
140 return AVERROR(ENOMEM);
141 s->offset[chan] = tmp_ptr;
143 tmp_ptr = av_realloc(s->decoded[chan], sizeof(int32_t)*(s->blocksize + s->nwrap));
145 return AVERROR(ENOMEM);
146 s->decoded[chan] = tmp_ptr;
147 for (i=0; i<s->nwrap; i++)
148 s->decoded[chan][i] = 0;
149 s->decoded[chan] += s->nwrap;
152 coeffs = av_realloc(s->coeffs, s->nwrap * sizeof(*s->coeffs));
154 return AVERROR(ENOMEM);
161 static inline unsigned int get_uint(ShortenContext *s, int k)
164 k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
165 return get_ur_golomb_shorten(&s->gb, k);
169 static void fix_bitshift(ShortenContext *s, int32_t *buffer)
173 if (s->bitshift != 0)
174 for (i = 0; i < s->blocksize; i++)
175 buffer[i] <<= s->bitshift;
179 static int init_offset(ShortenContext *s)
183 int nblock = FFMAX(1, s->nmean);
184 /* initialise offset */
185 switch (s->internal_ftype)
192 av_log(s->avctx, AV_LOG_ERROR, "unknown audio type");
193 return AVERROR_INVALIDDATA;
196 for (chan = 0; chan < s->channels; chan++)
197 for (i = 0; i < nblock; i++)
198 s->offset[chan][i] = mean;
202 static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header,
209 if (bytestream_get_le32(&header) != MKTAG('R','I','F','F')) {
210 av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
214 header += 4; /* chunk size */;
216 if (bytestream_get_le32(&header) != MKTAG('W','A','V','E')) {
217 av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
221 while (bytestream_get_le32(&header) != MKTAG('f','m','t',' ')) {
222 len = bytestream_get_le32(&header);
225 len = bytestream_get_le32(&header);
228 av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
232 wave_format = bytestream_get_le16(&header);
234 switch (wave_format) {
235 case WAVE_FORMAT_PCM:
238 av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
242 header += 2; // skip channels (already got from shorten header)
243 avctx->sample_rate = bytestream_get_le32(&header);
244 header += 4; // skip bit rate (represents original uncompressed bit rate)
245 header += 2; // skip block align (not needed)
246 avctx->bits_per_coded_sample = bytestream_get_le16(&header);
248 if (avctx->bits_per_coded_sample != 16) {
249 av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
255 av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
260 static void interleave_buffer(int16_t *samples, int nchan, int blocksize,
264 for (i=0; i<blocksize; i++)
265 for (chan=0; chan < nchan; chan++)
266 *samples++ = av_clip_int16(buffer[chan][i]);
269 static const int fixed_coeffs[3][3] = {
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;
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", pred_order);
286 return AVERROR(EINVAL);
288 /* read LPC coefficients */
289 for (i=0; i<pred_order; i++)
290 s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
295 /* fixed LPC coeffs */
296 pred_order = command;
297 coeffs = fixed_coeffs[pred_order-1];
301 /* subtract offset from previous samples to use in prediction */
302 if (command == FN_QLPC && coffset)
303 for (i = -pred_order; i < 0; i++)
304 s->decoded[channel][i] -= coffset;
306 /* decode residual and do LPC prediction */
307 init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
308 for (i=0; i < s->blocksize; i++) {
310 for (j=0; j<pred_order; j++)
311 sum += coeffs[j] * s->decoded[channel][i-j-1];
312 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + (sum >> qshift);
315 /* add offset to current samples */
316 if (command == FN_QLPC && coffset)
317 for (i = 0; i < s->blocksize; i++)
318 s->decoded[channel][i] += coffset;
323 static int read_header(ShortenContext *s)
327 /* shorten signature */
328 if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
329 av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
334 s->blocksize = DEFAULT_BLOCK_SIZE;
336 s->version = get_bits(&s->gb, 8);
337 s->internal_ftype = get_uint(s, TYPESIZE);
339 s->channels = get_uint(s, CHANSIZE);
340 if (s->channels > MAX_CHANNELS) {
341 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
344 s->avctx->channels = s->channels;
346 /* get blocksize if version > 0 */
347 if (s->version > 0) {
348 int skip_bytes, blocksize;
350 blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
351 if (!blocksize || blocksize > MAX_BLOCKSIZE) {
352 av_log(s->avctx, AV_LOG_ERROR, "invalid or unsupported block size: %d\n",
354 return AVERROR(EINVAL);
356 s->blocksize = blocksize;
358 maxnlpc = get_uint(s, LPCQSIZE);
359 s->nmean = get_uint(s, 0);
361 skip_bytes = get_uint(s, NSKIPSIZE);
362 for (i=0; i<skip_bytes; i++) {
363 skip_bits(&s->gb, 8);
366 s->nwrap = FFMAX(NWRAP, maxnlpc);
368 if ((ret = allocate_buffers(s)) < 0)
371 if ((ret = init_offset(s)) < 0)
375 s->lpcqoffset = V2LPCQOFFSET;
377 if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
378 av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at beginning of stream\n");
382 s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
383 if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
384 av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
388 for (i=0; i<s->header_size; i++)
389 s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
391 if (decode_wave_header(s->avctx, s->header, s->header_size) < 0)
402 static int shorten_decode_frame(AVCodecContext *avctx, void *data,
403 int *got_frame_ptr, AVPacket *avpkt)
405 const uint8_t *buf = avpkt->data;
406 int buf_size = avpkt->size;
407 ShortenContext *s = avctx->priv_data;
408 int i, input_buf_size = 0;
411 /* allocate internal bitstream buffer */
412 if(s->max_framesize == 0){
414 s->max_framesize= 1024; // should hopefully be enough for the first header
415 tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
418 av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
419 return AVERROR(ENOMEM);
421 s->bitstream = tmp_ptr;
424 /* append current packet data to bitstream buffer */
425 if(1 && s->max_framesize){//FIXME truncated
426 buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
427 input_buf_size= buf_size;
429 if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
430 memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
431 s->bitstream_index=0;
434 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
435 buf= &s->bitstream[s->bitstream_index];
436 buf_size += s->bitstream_size;
437 s->bitstream_size= buf_size;
439 /* do not decode until buffer has at least max_framesize bytes or
440 the end of the file has been reached */
441 if (buf_size < s->max_framesize && avpkt->data) {
443 return input_buf_size;
446 /* init and position bitstream reader */
447 init_get_bits(&s->gb, buf, buf_size*8);
448 skip_bits(&s->gb, s->bitindex);
450 /* process header or next subblock */
451 if (!s->got_header) {
452 if ((ret = read_header(s)) < 0)
458 /* if quit command was read previously, don't decode anything */
459 if (s->got_quit_command) {
465 while (s->cur_chan < s->channels) {
469 if (get_bits_left(&s->gb) < 3+FNSIZE) {
474 cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
476 if (cmd > FN_VERBATIM) {
477 av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
482 if (!is_audio_command[cmd]) {
483 /* process non-audio command */
486 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
488 get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
492 s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
495 int blocksize = get_uint(s, av_log2(s->blocksize));
496 if (blocksize > s->blocksize) {
497 av_log(avctx, AV_LOG_ERROR, "Increasing block size is not supported\n");
498 return AVERROR_PATCHWELCOME;
500 if (!blocksize || blocksize > MAX_BLOCKSIZE) {
501 av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
502 "block size: %d\n", blocksize);
503 return AVERROR(EINVAL);
505 s->blocksize = blocksize;
509 s->got_quit_command = 1;
512 if (cmd == FN_BLOCKSIZE || cmd == FN_QUIT) {
517 /* process audio command */
518 int residual_size = 0;
519 int channel = s->cur_chan;
522 /* get Rice code for residual decoding */
523 if (cmd != FN_ZERO) {
524 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
525 /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
530 /* calculate sample offset using means from previous blocks */
532 coffset = s->offset[channel][0];
534 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
535 for (i=0; i<s->nmean; i++)
536 sum += s->offset[channel][i];
537 coffset = sum / s->nmean;
539 coffset >>= FFMIN(1, s->bitshift);
542 /* decode samples for this channel */
543 if (cmd == FN_ZERO) {
544 for (i=0; i<s->blocksize; i++)
545 s->decoded[channel][i] = 0;
547 if ((ret = decode_subframe_lpc(s, cmd, channel, residual_size, coffset)) < 0)
551 /* update means with info from the current block */
553 int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
554 for (i=0; i<s->blocksize; i++)
555 sum += s->decoded[channel][i];
557 for (i=1; i<s->nmean; i++)
558 s->offset[channel][i-1] = s->offset[channel][i];
561 s->offset[channel][s->nmean - 1] = sum / s->blocksize;
563 s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
566 /* copy wrap samples for use with next block */
567 for (i=-s->nwrap; i<0; i++)
568 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
570 /* shift samples to add in unused zero bits which were removed
572 fix_bitshift(s, s->decoded[channel]);
574 /* if this is the last channel in the block, output the samples */
576 if (s->cur_chan == s->channels) {
577 /* get output buffer */
578 s->frame.nb_samples = s->blocksize;
579 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
580 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
583 /* interleave output */
584 interleave_buffer((int16_t *)s->frame.data[0], s->channels,
585 s->blocksize, s->decoded);
588 *(AVFrame *)data = s->frame;
592 if (s->cur_chan < s->channels)
596 s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
597 i= (get_bits_count(&s->gb))/8;
599 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
601 s->bitstream_index=0;
604 if (s->bitstream_size) {
605 s->bitstream_index += i;
606 s->bitstream_size -= i;
607 return input_buf_size;
612 static av_cold int shorten_decode_close(AVCodecContext *avctx)
614 ShortenContext *s = avctx->priv_data;
617 for (i = 0; i < s->channels; i++) {
618 s->decoded[i] -= s->nwrap;
619 av_freep(&s->decoded[i]);
620 av_freep(&s->offset[i]);
622 av_freep(&s->bitstream);
623 av_freep(&s->coeffs);
628 AVCodec ff_shorten_decoder = {
630 .type = AVMEDIA_TYPE_AUDIO,
631 .id = CODEC_ID_SHORTEN,
632 .priv_data_size = sizeof(ShortenContext),
633 .init = shorten_decode_init,
634 .close = shorten_decode_close,
635 .decode = shorten_decode_frame,
636 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
637 .long_name= NULL_IF_CONFIG_SMALL("Shorten"),