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
34 #define MAX_CHANNELS 8
35 #define MAX_BLOCKSIZE 65535
37 #define OUT_BUFFER_SIZE 16384
41 #define WAVE_FORMAT_PCM 0x0001
43 #define DEFAULT_BLOCK_SIZE 256
49 #define BITSHIFTSIZE 2
58 #define V2LPCQOFFSET (1 << LPCQUANT)
66 #define FN_BLOCKSIZE 5
72 #define VERBATIM_CKSIZE_SIZE 5
73 #define VERBATIM_BYTE_SIZE 8
74 #define CANONICAL_HEADER_SIZE 44
76 typedef struct ShortenContext {
77 AVCodecContext *avctx;
80 int min_framesize, max_framesize;
83 int32_t *decoded[MAX_CHANNELS];
84 int32_t *offset[MAX_CHANNELS];
89 unsigned int allocated_bitstream_size;
91 uint8_t header[OUT_BUFFER_SIZE];
103 static av_cold int shorten_decode_init(AVCodecContext * avctx)
105 ShortenContext *s = avctx->priv_data;
107 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
112 static int allocate_buffers(ShortenContext *s)
117 for (chan=0; chan<s->channels; chan++) {
118 if(FFMAX(1, s->nmean) >= UINT_MAX/sizeof(int32_t)){
119 av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
122 if(s->blocksize + s->nwrap >= UINT_MAX/sizeof(int32_t) || s->blocksize + s->nwrap <= (unsigned)s->nwrap){
123 av_log(s->avctx, AV_LOG_ERROR, "s->blocksize + s->nwrap too large\n");
127 s->offset[chan] = av_realloc(s->offset[chan], sizeof(int32_t)*FFMAX(1, s->nmean));
129 s->decoded[chan] = av_realloc(s->decoded[chan], sizeof(int32_t)*(s->blocksize + s->nwrap));
130 for (i=0; i<s->nwrap; i++)
131 s->decoded[chan][i] = 0;
132 s->decoded[chan] += s->nwrap;
135 coeffs = av_realloc(s->coeffs, s->nwrap * sizeof(*s->coeffs));
137 return AVERROR(ENOMEM);
144 static inline unsigned int get_uint(ShortenContext *s, int k)
147 k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
148 return get_ur_golomb_shorten(&s->gb, k);
152 static void fix_bitshift(ShortenContext *s, int32_t *buffer)
156 if (s->bitshift != 0)
157 for (i = 0; i < s->blocksize; i++)
158 buffer[s->nwrap + i] <<= s->bitshift;
162 static void init_offset(ShortenContext *s)
166 int nblock = FFMAX(1, s->nmean);
167 /* initialise offset */
168 switch (s->internal_ftype)
175 av_log(s->avctx, AV_LOG_ERROR, "unknown audio type");
179 for (chan = 0; chan < s->channels; chan++)
180 for (i = 0; i < nblock; i++)
181 s->offset[chan][i] = mean;
184 static inline int get_le32(GetBitContext *gb)
186 return av_bswap32(get_bits_long(gb, 32));
189 static inline short get_le16(GetBitContext *gb)
191 return av_bswap16(get_bits_long(gb, 16));
194 static int decode_wave_header(AVCodecContext *avctx, uint8_t *header, int header_size)
200 init_get_bits(&hb, header, header_size*8);
201 if (get_le32(&hb) != MKTAG('R','I','F','F')) {
202 av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
206 skip_bits_long(&hb, 32); /* chunk_size */
208 if (get_le32(&hb) != MKTAG('W','A','V','E')) {
209 av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
213 while (get_le32(&hb) != MKTAG('f','m','t',' ')) {
215 skip_bits(&hb, 8*len);
220 av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
224 wave_format = get_le16(&hb);
226 switch (wave_format) {
227 case WAVE_FORMAT_PCM:
230 av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
234 avctx->channels = get_le16(&hb);
235 avctx->sample_rate = get_le32(&hb);
236 avctx->bit_rate = get_le32(&hb) * 8;
237 avctx->block_align = get_le16(&hb);
238 avctx->bits_per_coded_sample = get_le16(&hb);
240 if (avctx->bits_per_coded_sample != 16) {
241 av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
247 av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
252 static int16_t * interleave_buffer(int16_t *samples, int nchan, int blocksize, int32_t **buffer) {
254 for (i=0; i<blocksize; i++)
255 for (chan=0; chan < nchan; chan++)
256 *samples++ = FFMIN(buffer[chan][i], 32768);
260 static void decode_subframe_lpc(ShortenContext *s, int channel, int residual_size, int pred_order)
263 int *coeffs = s->coeffs;
265 for (i=0; i<pred_order; i++)
266 coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
268 for (i=0; i < s->blocksize; i++) {
270 for (j=0; j<pred_order; j++)
271 sum += coeffs[j] * s->decoded[channel][i-j-1];
272 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + (sum >> LPCQUANT);
277 static int shorten_decode_frame(AVCodecContext *avctx,
278 void *data, int *data_size,
281 const uint8_t *buf = avpkt->data;
282 int buf_size = avpkt->size;
283 ShortenContext *s = avctx->priv_data;
284 int i, input_buf_size = 0;
285 int16_t *samples = data;
286 if(s->max_framesize == 0){
287 s->max_framesize= 1024; // should hopefully be enough for the first header
288 s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
291 if(1 && s->max_framesize){//FIXME truncated
292 buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
293 input_buf_size= buf_size;
295 if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
296 // printf("memmove\n");
297 memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
298 s->bitstream_index=0;
300 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
301 buf= &s->bitstream[s->bitstream_index];
302 buf_size += s->bitstream_size;
303 s->bitstream_size= buf_size;
305 if(buf_size < s->max_framesize){
307 return input_buf_size;
310 init_get_bits(&s->gb, buf, buf_size*8);
311 skip_bits(&s->gb, s->bitindex);
315 /* shorten signature */
316 if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
317 av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
322 s->blocksize = DEFAULT_BLOCK_SIZE;
325 s->version = get_bits(&s->gb, 8);
326 s->internal_ftype = get_uint(s, TYPESIZE);
328 s->channels = get_uint(s, CHANSIZE);
329 if (s->channels > MAX_CHANNELS) {
330 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
334 /* get blocksize if version > 0 */
335 if (s->version > 0) {
337 s->blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
338 maxnlpc = get_uint(s, LPCQSIZE);
339 s->nmean = get_uint(s, 0);
341 skip_bytes = get_uint(s, NSKIPSIZE);
342 for (i=0; i<skip_bytes; i++) {
343 skip_bits(&s->gb, 8);
346 s->nwrap = FFMAX(NWRAP, maxnlpc);
348 if (allocate_buffers(s))
354 s->lpcqoffset = V2LPCQOFFSET;
356 if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
357 av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at beginning of stream\n");
361 s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
362 if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
363 av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
367 for (i=0; i<s->header_size; i++)
368 s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
370 if (decode_wave_header(avctx, s->header, s->header_size) < 0)
380 cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
389 int residual_size = 0;
390 int channel = s->cur_chan;
392 if (cmd != FN_ZERO) {
393 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
394 /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
400 coffset = s->offset[channel][0];
402 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
403 for (i=0; i<s->nmean; i++)
404 sum += s->offset[channel][i];
405 coffset = sum / s->nmean;
407 coffset >>= FFMIN(1, s->bitshift);
411 for (i=0; i<s->blocksize; i++)
412 s->decoded[channel][i] = 0;
415 for (i=0; i<s->blocksize; i++)
416 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + coffset;
419 for (i=0; i<s->blocksize; i++)
420 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + s->decoded[channel][i - 1];
423 for (i=0; i<s->blocksize; i++)
424 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 2*s->decoded[channel][i-1]
425 - s->decoded[channel][i-2];
428 for (i=0; i<s->blocksize; i++)
429 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 3*s->decoded[channel][i-1]
430 - 3*s->decoded[channel][i-2]
431 + s->decoded[channel][i-3];
435 int pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
436 if (pred_order > s->nwrap) {
437 av_log(avctx, AV_LOG_ERROR,
438 "invalid pred_order %d\n",
442 for (i=0; i<pred_order; i++)
443 s->decoded[channel][i - pred_order] -= coffset;
444 decode_subframe_lpc(s, channel, residual_size, pred_order);
446 for (i=0; i < s->blocksize; i++)
447 s->decoded[channel][i] += coffset;
451 int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
452 for (i=0; i<s->blocksize; i++)
453 sum += s->decoded[channel][i];
455 for (i=1; i<s->nmean; i++)
456 s->offset[channel][i-1] = s->offset[channel][i];
459 s->offset[channel][s->nmean - 1] = sum / s->blocksize;
461 s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
463 for (i=-s->nwrap; i<0; i++)
464 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
466 fix_bitshift(s, s->decoded[channel]);
469 if (s->cur_chan == s->channels) {
470 samples = interleave_buffer(samples, s->channels, s->blocksize, s->decoded);
477 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
479 get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
483 s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
486 s->blocksize = get_uint(s, av_log2(s->blocksize));
492 av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
497 *data_size = (int8_t *)samples - (int8_t *)data;
499 // s->last_blocksize = s->blocksize;
500 s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
501 i= (get_bits_count(&s->gb))/8;
503 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
505 s->bitstream_index=0;
508 if (s->bitstream_size) {
509 s->bitstream_index += i;
510 s->bitstream_size -= i;
511 return input_buf_size;
516 static av_cold int shorten_decode_close(AVCodecContext *avctx)
518 ShortenContext *s = avctx->priv_data;
521 for (i = 0; i < s->channels; i++) {
522 s->decoded[i] -= s->nwrap;
523 av_freep(&s->decoded[i]);
524 av_freep(&s->offset[i]);
526 av_freep(&s->bitstream);
527 av_freep(&s->coeffs);
531 static void shorten_flush(AVCodecContext *avctx){
532 ShortenContext *s = avctx->priv_data;
535 s->bitstream_index= 0;
538 AVCodec ff_shorten_decoder = {
540 .type = AVMEDIA_TYPE_AUDIO,
541 .id = CODEC_ID_SHORTEN,
542 .priv_data_size = sizeof(ShortenContext),
543 .init = shorten_decode_init,
544 .close = shorten_decode_close,
545 .decode = shorten_decode_frame,
546 .flush= shorten_flush,
547 .long_name= NULL_IF_CONFIG_SMALL("Shorten"),