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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 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 void allocate_buffers(ShortenContext *s)
112 for (chan=0; chan<s->channels; chan++) {
113 s->offset[chan] = av_realloc(s->offset[chan], sizeof(int32_t)*FFMAX(1, s->nmean));
115 s->decoded[chan] = av_realloc(s->decoded[chan], sizeof(int32_t)*(s->blocksize + s->nwrap));
116 for (i=0; i<s->nwrap; i++)
117 s->decoded[chan][i] = 0;
118 s->decoded[chan] += s->nwrap;
124 static inline unsigned int get_uint(ShortenContext *s, int k)
127 k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
128 return get_ur_golomb_shorten(&s->gb, k);
132 static void fix_bitshift(ShortenContext *s, int32_t *buffer)
136 if (s->bitshift != 0)
137 for (i = 0; i < s->blocksize; i++)
138 buffer[s->nwrap + i] <<= s->bitshift;
142 static void init_offset(ShortenContext *s)
146 int nblock = FFMAX(1, s->nmean);
147 /* initialise offset */
148 switch (s->internal_ftype)
155 av_log(s->avctx, AV_LOG_ERROR, "unknown audio type");
159 for (chan = 0; chan < s->channels; chan++)
160 for (i = 0; i < nblock; i++)
161 s->offset[chan][i] = mean;
164 static int inline get_le32(GetBitContext *gb)
166 return bswap_32(get_bits_long(gb, 32));
169 static short inline get_le16(GetBitContext *gb)
171 return bswap_16(get_bits_long(gb, 16));
174 static int decode_wave_header(AVCodecContext *avctx, uint8_t *header, int header_size)
181 init_get_bits(&hb, header, header_size*8);
182 if (get_le32(&hb) != MKTAG('R','I','F','F')) {
183 av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
187 chunk_size = get_le32(&hb);
189 if (get_le32(&hb) != MKTAG('W','A','V','E')) {
190 av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
194 while (get_le32(&hb) != MKTAG('f','m','t',' ')) {
196 skip_bits(&hb, 8*len);
201 av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
205 wave_format = get_le16(&hb);
207 switch (wave_format) {
208 case WAVE_FORMAT_PCM:
211 av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
215 avctx->channels = get_le16(&hb);
216 avctx->sample_rate = get_le32(&hb);
217 avctx->bit_rate = get_le32(&hb) * 8;
218 avctx->block_align = get_le16(&hb);
219 avctx->bits_per_sample = get_le16(&hb);
221 if (avctx->bits_per_sample != 16) {
222 av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
228 av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
233 static int16_t * interleave_buffer(int16_t *samples, int nchan, int blocksize, int32_t **buffer) {
235 for (i=0; i<blocksize; i++)
236 for (chan=0; chan < nchan; chan++)
237 *samples++ = FFMIN(buffer[chan][i], 32768);
241 static void decode_subframe_lpc(ShortenContext *s, int channel, int residual_size, int pred_order)
244 int coeffs[pred_order];
246 for (i=0; i<pred_order; i++)
247 coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
249 for (i=0; i < s->blocksize; i++) {
251 for (j=0; j<pred_order; j++)
252 sum += coeffs[j] * s->decoded[channel][i-j-1];
253 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + (sum >> LPCQUANT);
258 static int shorten_decode_frame(AVCodecContext *avctx,
259 void *data, int *data_size,
260 uint8_t *buf, int buf_size)
262 ShortenContext *s = avctx->priv_data;
263 int i, input_buf_size = 0;
264 int16_t *samples = data;
265 if(s->max_framesize == 0){
266 s->max_framesize= 1024; // should hopefully be enough for the first header
267 s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
270 if(1 && s->max_framesize){//FIXME truncated
271 buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
272 input_buf_size= buf_size;
274 if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
275 // printf("memmove\n");
276 memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
277 s->bitstream_index=0;
279 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
280 buf= &s->bitstream[s->bitstream_index];
281 buf_size += s->bitstream_size;
282 s->bitstream_size= buf_size;
284 if(buf_size < s->max_framesize){
285 //dprintf("wanna more data ... %d\n", buf_size);
286 return input_buf_size;
289 init_get_bits(&s->gb, buf, buf_size*8);
290 get_bits(&s->gb, s->bitindex);
294 /* shorten signature */
295 if (get_bits_long(&s->gb, 32) != bswap_32(ff_get_fourcc("ajkg"))) {
296 av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
301 s->blocksize = DEFAULT_BLOCK_SIZE;
304 s->version = get_bits(&s->gb, 8);
305 s->internal_ftype = get_uint(s, TYPESIZE);
307 s->channels = get_uint(s, CHANSIZE);
308 if (s->channels > MAX_CHANNELS) {
309 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
313 /* get blocksize if version > 0 */
314 if (s->version > 0) {
316 s->blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
317 maxnlpc = get_uint(s, LPCQSIZE);
318 s->nmean = get_uint(s, 0);
320 skip_bytes = get_uint(s, NSKIPSIZE);
321 for (i=0; i<skip_bytes; i++) {
322 skip_bits(&s->gb, 8);
325 s->nwrap = FFMAX(NWRAP, maxnlpc);
332 s->lpcqoffset = V2LPCQOFFSET;
334 if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
335 av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at begining of stream\n");
339 s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
340 if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
341 av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
345 for (i=0; i<s->header_size; i++)
346 s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
348 if (decode_wave_header(avctx, s->header, s->header_size) < 0)
358 cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
367 int residual_size = 0;
368 int channel = s->cur_chan;
370 if (cmd != FN_ZERO) {
371 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
372 /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
378 coffset = s->offset[channel][0];
380 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
381 for (i=0; i<s->nmean; i++)
382 sum += s->offset[channel][i];
383 coffset = sum / s->nmean;
385 coffset >>= FFMIN(1, s->bitshift);
389 for (i=0; i<s->blocksize; i++)
390 s->decoded[channel][i] = 0;
393 for (i=0; i<s->blocksize; i++)
394 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + coffset;
397 for (i=0; i<s->blocksize; i++)
398 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + s->decoded[channel][i - 1];
401 for (i=0; i<s->blocksize; i++)
402 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 2*s->decoded[channel][i-1]
403 - s->decoded[channel][i-2];
406 for (i=0; i<s->blocksize; i++)
407 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 3*s->decoded[channel][i-1]
408 - 3*s->decoded[channel][i-2]
409 + s->decoded[channel][i-3];
413 int pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
414 for (i=0; i<pred_order; i++)
415 s->decoded[channel][i - pred_order] -= coffset;
416 decode_subframe_lpc(s, channel, residual_size, pred_order);
418 for (i=0; i < s->blocksize; i++)
419 s->decoded[channel][i] += coffset;
423 int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
424 for (i=0; i<s->blocksize; i++)
425 sum += s->decoded[channel][i];
427 for (i=1; i<s->nmean; i++)
428 s->offset[channel][i-1] = s->offset[channel][i];
431 s->offset[channel][s->nmean - 1] = sum / s->blocksize;
433 s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
435 for (i=-s->nwrap; i<0; i++)
436 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
438 fix_bitshift(s, s->decoded[channel]);
441 if (s->cur_chan == s->channels) {
442 samples = interleave_buffer(samples, s->channels, s->blocksize, s->decoded);
450 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
452 get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
456 s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
459 s->blocksize = get_uint(s, av_log2(s->blocksize));
465 av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
471 *data_size = (int8_t *)samples - (int8_t *)data;
473 // s->last_blocksize = s->blocksize;
474 s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
475 i= (get_bits_count(&s->gb))/8;
477 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
479 s->bitstream_index=0;
482 if (s->bitstream_size) {
483 s->bitstream_index += i;
484 s->bitstream_size -= i;
485 return input_buf_size;
490 static int shorten_decode_close(AVCodecContext *avctx)
492 ShortenContext *s = avctx->priv_data;
495 for (i = 0; i < s->channels; i++) {
496 s->decoded[i] -= s->nwrap;
497 av_freep(&s->decoded[i]);
498 av_freep(&s->offset[i]);
500 av_freep(&s->bitstream);
504 static void shorten_flush(AVCodecContext *avctx){
505 ShortenContext *s = avctx->priv_data;
508 s->bitstream_index= 0;
511 AVCodec shorten_decoder = {
515 sizeof(ShortenContext),
518 shorten_decode_close,
519 shorten_decode_frame,
520 .flush= shorten_flush,