3 * Copyright (c) 2005 Jeff Muizelaar
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * @author Jeff Muizelaar
30 #include "bitstream.h"
33 #define MAX_CHANNELS 8
34 #define MAX_BLOCKSIZE 65535
36 #define OUT_BUFFER_SIZE 16384
40 #define WAVE_FORMAT_PCM 0x0001
42 #define DEFAULT_BLOCK_SIZE 256
48 #define BITSHIFTSIZE 2
57 #define V2LPCQOFFSET (1 << LPCQUANT)
65 #define FN_BLOCKSIZE 5
71 #define VERBATIM_CKSIZE_SIZE 5
72 #define VERBATIM_BYTE_SIZE 8
73 #define CANONICAL_HEADER_SIZE 44
75 typedef struct ShortenContext {
76 AVCodecContext *avctx;
79 int min_framesize, max_framesize;
82 int32_t *decoded[MAX_CHANNELS];
83 int32_t *offset[MAX_CHANNELS];
87 unsigned int allocated_bitstream_size;
89 uint8_t header[OUT_BUFFER_SIZE];
101 static int shorten_decode_init(AVCodecContext * avctx)
103 ShortenContext *s = avctx->priv_data;
109 static int allocate_buffers(ShortenContext *s)
112 for (chan=0; chan<s->channels; chan++) {
113 if(FFMAX(1, s->nmean) >= UINT_MAX/sizeof(int32_t)){
114 av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
117 if(s->blocksize + s->nwrap >= UINT_MAX/sizeof(int32_t) || s->blocksize + s->nwrap <= (unsigned)s->nwrap){
118 av_log(s->avctx, AV_LOG_ERROR, "s->blocksize + s->nwrap too large\n");
122 s->offset[chan] = av_realloc(s->offset[chan], sizeof(int32_t)*FFMAX(1, s->nmean));
124 s->decoded[chan] = av_realloc(s->decoded[chan], sizeof(int32_t)*(s->blocksize + s->nwrap));
125 for (i=0; i<s->nwrap; i++)
126 s->decoded[chan][i] = 0;
127 s->decoded[chan] += s->nwrap;
133 static inline unsigned int get_uint(ShortenContext *s, int k)
136 k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
137 return get_ur_golomb_shorten(&s->gb, k);
141 static void fix_bitshift(ShortenContext *s, int32_t *buffer)
145 if (s->bitshift != 0)
146 for (i = 0; i < s->blocksize; i++)
147 buffer[s->nwrap + i] <<= s->bitshift;
151 static void init_offset(ShortenContext *s)
155 int nblock = FFMAX(1, s->nmean);
156 /* initialise offset */
157 switch (s->internal_ftype)
164 av_log(s->avctx, AV_LOG_ERROR, "unknown audio type");
168 for (chan = 0; chan < s->channels; chan++)
169 for (i = 0; i < nblock; i++)
170 s->offset[chan][i] = mean;
173 static int inline get_le32(GetBitContext *gb)
175 return bswap_32(get_bits_long(gb, 32));
178 static short inline get_le16(GetBitContext *gb)
180 return bswap_16(get_bits_long(gb, 16));
183 static int decode_wave_header(AVCodecContext *avctx, uint8_t *header, int header_size)
190 init_get_bits(&hb, header, header_size*8);
191 if (get_le32(&hb) != MKTAG('R','I','F','F')) {
192 av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
196 chunk_size = get_le32(&hb);
198 if (get_le32(&hb) != MKTAG('W','A','V','E')) {
199 av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
203 while (get_le32(&hb) != MKTAG('f','m','t',' ')) {
205 skip_bits(&hb, 8*len);
210 av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
214 wave_format = get_le16(&hb);
216 switch (wave_format) {
217 case WAVE_FORMAT_PCM:
220 av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
224 avctx->channels = get_le16(&hb);
225 avctx->sample_rate = get_le32(&hb);
226 avctx->bit_rate = get_le32(&hb) * 8;
227 avctx->block_align = get_le16(&hb);
228 avctx->bits_per_sample = get_le16(&hb);
230 if (avctx->bits_per_sample != 16) {
231 av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
237 av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
242 static int16_t * interleave_buffer(int16_t *samples, int nchan, int blocksize, int32_t **buffer) {
244 for (i=0; i<blocksize; i++)
245 for (chan=0; chan < nchan; chan++)
246 *samples++ = FFMIN(buffer[chan][i], 32768);
250 static void decode_subframe_lpc(ShortenContext *s, int channel, int residual_size, int pred_order)
253 int coeffs[pred_order];
255 for (i=0; i<pred_order; i++)
256 coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
258 for (i=0; i < s->blocksize; i++) {
260 for (j=0; j<pred_order; j++)
261 sum += coeffs[j] * s->decoded[channel][i-j-1];
262 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + (sum >> LPCQUANT);
267 static int shorten_decode_frame(AVCodecContext *avctx,
268 void *data, int *data_size,
269 uint8_t *buf, int buf_size)
271 ShortenContext *s = avctx->priv_data;
272 int i, input_buf_size = 0;
273 int16_t *samples = data;
274 if(s->max_framesize == 0){
275 s->max_framesize= 1024; // should hopefully be enough for the first header
276 s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
279 if(1 && s->max_framesize){//FIXME truncated
280 buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
281 input_buf_size= buf_size;
283 if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
284 // printf("memmove\n");
285 memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
286 s->bitstream_index=0;
288 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
289 buf= &s->bitstream[s->bitstream_index];
290 buf_size += s->bitstream_size;
291 s->bitstream_size= buf_size;
293 if(buf_size < s->max_framesize){
294 //dprintf("wanna more data ... %d\n", buf_size);
295 return input_buf_size;
298 init_get_bits(&s->gb, buf, buf_size*8);
299 get_bits(&s->gb, s->bitindex);
303 /* shorten signature */
304 if (get_bits_long(&s->gb, 32) != bswap_32(ff_get_fourcc("ajkg"))) {
305 av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
310 s->blocksize = DEFAULT_BLOCK_SIZE;
313 s->version = get_bits(&s->gb, 8);
314 s->internal_ftype = get_uint(s, TYPESIZE);
316 s->channels = get_uint(s, CHANSIZE);
317 if (s->channels > MAX_CHANNELS) {
318 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
322 /* get blocksize if version > 0 */
323 if (s->version > 0) {
325 s->blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
326 maxnlpc = get_uint(s, LPCQSIZE);
327 s->nmean = get_uint(s, 0);
329 skip_bytes = get_uint(s, NSKIPSIZE);
330 for (i=0; i<skip_bytes; i++) {
331 skip_bits(&s->gb, 8);
334 s->nwrap = FFMAX(NWRAP, maxnlpc);
341 s->lpcqoffset = V2LPCQOFFSET;
343 if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
344 av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at begining of stream\n");
348 s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
349 if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
350 av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
354 for (i=0; i<s->header_size; i++)
355 s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
357 if (decode_wave_header(avctx, s->header, s->header_size) < 0)
367 cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
376 int residual_size = 0;
377 int channel = s->cur_chan;
379 if (cmd != FN_ZERO) {
380 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
381 /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
387 coffset = s->offset[channel][0];
389 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
390 for (i=0; i<s->nmean; i++)
391 sum += s->offset[channel][i];
392 coffset = sum / s->nmean;
394 coffset >>= FFMIN(1, s->bitshift);
398 for (i=0; i<s->blocksize; i++)
399 s->decoded[channel][i] = 0;
402 for (i=0; i<s->blocksize; i++)
403 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + coffset;
406 for (i=0; i<s->blocksize; i++)
407 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + s->decoded[channel][i - 1];
410 for (i=0; i<s->blocksize; i++)
411 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 2*s->decoded[channel][i-1]
412 - s->decoded[channel][i-2];
415 for (i=0; i<s->blocksize; i++)
416 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 3*s->decoded[channel][i-1]
417 - 3*s->decoded[channel][i-2]
418 + s->decoded[channel][i-3];
422 int pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
423 for (i=0; i<pred_order; i++)
424 s->decoded[channel][i - pred_order] -= coffset;
425 decode_subframe_lpc(s, channel, residual_size, pred_order);
427 for (i=0; i < s->blocksize; i++)
428 s->decoded[channel][i] += coffset;
432 int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
433 for (i=0; i<s->blocksize; i++)
434 sum += s->decoded[channel][i];
436 for (i=1; i<s->nmean; i++)
437 s->offset[channel][i-1] = s->offset[channel][i];
440 s->offset[channel][s->nmean - 1] = sum / s->blocksize;
442 s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
444 for (i=-s->nwrap; i<0; i++)
445 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
447 fix_bitshift(s, s->decoded[channel]);
450 if (s->cur_chan == s->channels) {
451 samples = interleave_buffer(samples, s->channels, s->blocksize, s->decoded);
459 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
461 get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
465 s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
468 s->blocksize = get_uint(s, av_log2(s->blocksize));
474 av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
480 *data_size = (int8_t *)samples - (int8_t *)data;
482 // s->last_blocksize = s->blocksize;
483 s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
484 i= (get_bits_count(&s->gb))/8;
486 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
488 s->bitstream_index=0;
491 if (s->bitstream_size) {
492 s->bitstream_index += i;
493 s->bitstream_size -= i;
494 return input_buf_size;
499 static int shorten_decode_close(AVCodecContext *avctx)
501 ShortenContext *s = avctx->priv_data;
504 for (i = 0; i < s->channels; i++) {
505 s->decoded[i] -= s->nwrap;
506 av_freep(&s->decoded[i]);
507 av_freep(&s->offset[i]);
509 av_freep(&s->bitstream);
513 static void shorten_flush(AVCodecContext *avctx){
514 ShortenContext *s = avctx->priv_data;
517 s->bitstream_index= 0;
520 AVCodec shorten_decoder = {
524 sizeof(ShortenContext),
527 shorten_decode_close,
528 shorten_decode_frame,
529 .flush= shorten_flush,