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
32 #include "bitstream.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 #define VERBATIM_CKSIZE_SIZE 5
74 #define VERBATIM_BYTE_SIZE 8
75 #define CANONICAL_HEADER_SIZE 44
77 typedef struct ShortenContext {
78 AVCodecContext *avctx;
81 int min_framesize, max_framesize;
84 int32_t *decoded[MAX_CHANNELS];
85 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;
111 static int allocate_buffers(ShortenContext *s)
114 for (chan=0; chan<s->channels; chan++) {
115 if(FFMAX(1, s->nmean) >= UINT_MAX/sizeof(int32_t)){
116 av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
119 if(s->blocksize + s->nwrap >= UINT_MAX/sizeof(int32_t) || s->blocksize + s->nwrap <= (unsigned)s->nwrap){
120 av_log(s->avctx, AV_LOG_ERROR, "s->blocksize + s->nwrap too large\n");
124 s->offset[chan] = av_realloc(s->offset[chan], sizeof(int32_t)*FFMAX(1, s->nmean));
126 s->decoded[chan] = av_realloc(s->decoded[chan], sizeof(int32_t)*(s->blocksize + s->nwrap));
127 for (i=0; i<s->nwrap; i++)
128 s->decoded[chan][i] = 0;
129 s->decoded[chan] += s->nwrap;
135 static inline unsigned int get_uint(ShortenContext *s, int k)
138 k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
139 return get_ur_golomb_shorten(&s->gb, k);
143 static void fix_bitshift(ShortenContext *s, int32_t *buffer)
147 if (s->bitshift != 0)
148 for (i = 0; i < s->blocksize; i++)
149 buffer[s->nwrap + i] <<= s->bitshift;
153 static void init_offset(ShortenContext *s)
157 int nblock = FFMAX(1, s->nmean);
158 /* initialise offset */
159 switch (s->internal_ftype)
166 av_log(s->avctx, AV_LOG_ERROR, "unknown audio type");
170 for (chan = 0; chan < s->channels; chan++)
171 for (i = 0; i < nblock; i++)
172 s->offset[chan][i] = mean;
175 static inline int get_le32(GetBitContext *gb)
177 return bswap_32(get_bits_long(gb, 32));
180 static inline short get_le16(GetBitContext *gb)
182 return bswap_16(get_bits_long(gb, 16));
185 static int decode_wave_header(AVCodecContext *avctx, uint8_t *header, int header_size)
192 init_get_bits(&hb, header, header_size*8);
193 if (get_le32(&hb) != MKTAG('R','I','F','F')) {
194 av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
198 chunk_size = get_le32(&hb);
200 if (get_le32(&hb) != MKTAG('W','A','V','E')) {
201 av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
205 while (get_le32(&hb) != MKTAG('f','m','t',' ')) {
207 skip_bits(&hb, 8*len);
212 av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
216 wave_format = get_le16(&hb);
218 switch (wave_format) {
219 case WAVE_FORMAT_PCM:
222 av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
226 avctx->channels = get_le16(&hb);
227 avctx->sample_rate = get_le32(&hb);
228 avctx->bit_rate = get_le32(&hb) * 8;
229 avctx->block_align = get_le16(&hb);
230 avctx->bits_per_sample = get_le16(&hb);
232 if (avctx->bits_per_sample != 16) {
233 av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
239 av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
244 static int16_t * interleave_buffer(int16_t *samples, int nchan, int blocksize, int32_t **buffer) {
246 for (i=0; i<blocksize; i++)
247 for (chan=0; chan < nchan; chan++)
248 *samples++ = FFMIN(buffer[chan][i], 32768);
252 static void decode_subframe_lpc(ShortenContext *s, int channel, int residual_size, int pred_order)
255 int coeffs[pred_order];
257 for (i=0; i<pred_order; i++)
258 coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
260 for (i=0; i < s->blocksize; i++) {
262 for (j=0; j<pred_order; j++)
263 sum += coeffs[j] * s->decoded[channel][i-j-1];
264 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + (sum >> LPCQUANT);
269 static int shorten_decode_frame(AVCodecContext *avctx,
270 void *data, int *data_size,
271 const uint8_t *buf, int buf_size)
273 ShortenContext *s = avctx->priv_data;
274 int i, input_buf_size = 0;
275 int16_t *samples = data;
276 if(s->max_framesize == 0){
277 s->max_framesize= 1024; // should hopefully be enough for the first header
278 s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
281 if(1 && s->max_framesize){//FIXME truncated
282 buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
283 input_buf_size= buf_size;
285 if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
286 // printf("memmove\n");
287 memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
288 s->bitstream_index=0;
290 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
291 buf= &s->bitstream[s->bitstream_index];
292 buf_size += s->bitstream_size;
293 s->bitstream_size= buf_size;
295 if(buf_size < s->max_framesize){
296 //dprintf(avctx, "wanna more data ... %d\n", buf_size);
298 return input_buf_size;
301 init_get_bits(&s->gb, buf, buf_size*8);
302 skip_bits(&s->gb, s->bitindex);
306 /* shorten signature */
307 if (get_bits_long(&s->gb, 32) != bswap_32(ff_get_fourcc("ajkg"))) {
308 av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
313 s->blocksize = DEFAULT_BLOCK_SIZE;
316 s->version = get_bits(&s->gb, 8);
317 s->internal_ftype = get_uint(s, TYPESIZE);
319 s->channels = get_uint(s, CHANSIZE);
320 if (s->channels > MAX_CHANNELS) {
321 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
325 /* get blocksize if version > 0 */
326 if (s->version > 0) {
328 s->blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
329 maxnlpc = get_uint(s, LPCQSIZE);
330 s->nmean = get_uint(s, 0);
332 skip_bytes = get_uint(s, NSKIPSIZE);
333 for (i=0; i<skip_bytes; i++) {
334 skip_bits(&s->gb, 8);
337 s->nwrap = FFMAX(NWRAP, maxnlpc);
339 if (allocate_buffers(s))
345 s->lpcqoffset = V2LPCQOFFSET;
347 if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
348 av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at beginning of stream\n");
352 s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
353 if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
354 av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
358 for (i=0; i<s->header_size; i++)
359 s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
361 if (decode_wave_header(avctx, s->header, s->header_size) < 0)
371 cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
380 int residual_size = 0;
381 int channel = s->cur_chan;
383 if (cmd != FN_ZERO) {
384 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
385 /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
391 coffset = s->offset[channel][0];
393 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
394 for (i=0; i<s->nmean; i++)
395 sum += s->offset[channel][i];
396 coffset = sum / s->nmean;
398 coffset >>= FFMIN(1, s->bitshift);
402 for (i=0; i<s->blocksize; i++)
403 s->decoded[channel][i] = 0;
406 for (i=0; i<s->blocksize; i++)
407 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + coffset;
410 for (i=0; i<s->blocksize; i++)
411 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + s->decoded[channel][i - 1];
414 for (i=0; i<s->blocksize; i++)
415 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 2*s->decoded[channel][i-1]
416 - s->decoded[channel][i-2];
419 for (i=0; i<s->blocksize; i++)
420 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 3*s->decoded[channel][i-1]
421 - 3*s->decoded[channel][i-2]
422 + s->decoded[channel][i-3];
426 int pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
427 for (i=0; i<pred_order; i++)
428 s->decoded[channel][i - pred_order] -= coffset;
429 decode_subframe_lpc(s, channel, residual_size, pred_order);
431 for (i=0; i < s->blocksize; i++)
432 s->decoded[channel][i] += coffset;
436 int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
437 for (i=0; i<s->blocksize; i++)
438 sum += s->decoded[channel][i];
440 for (i=1; i<s->nmean; i++)
441 s->offset[channel][i-1] = s->offset[channel][i];
444 s->offset[channel][s->nmean - 1] = sum / s->blocksize;
446 s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
448 for (i=-s->nwrap; i<0; i++)
449 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
451 fix_bitshift(s, s->decoded[channel]);
454 if (s->cur_chan == s->channels) {
455 samples = interleave_buffer(samples, s->channels, s->blocksize, s->decoded);
463 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
465 get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
469 s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
472 s->blocksize = get_uint(s, av_log2(s->blocksize));
479 av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
485 *data_size = (int8_t *)samples - (int8_t *)data;
487 // s->last_blocksize = s->blocksize;
488 s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
489 i= (get_bits_count(&s->gb))/8;
491 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
493 s->bitstream_index=0;
496 if (s->bitstream_size) {
497 s->bitstream_index += i;
498 s->bitstream_size -= i;
499 return input_buf_size;
504 static av_cold int shorten_decode_close(AVCodecContext *avctx)
506 ShortenContext *s = avctx->priv_data;
509 for (i = 0; i < s->channels; i++) {
510 s->decoded[i] -= s->nwrap;
511 av_freep(&s->decoded[i]);
512 av_freep(&s->offset[i]);
514 av_freep(&s->bitstream);
518 static void shorten_flush(AVCodecContext *avctx){
519 ShortenContext *s = avctx->priv_data;
522 s->bitstream_index= 0;
525 AVCodec shorten_decoder = {
529 sizeof(ShortenContext),
532 shorten_decode_close,
533 shorten_decode_frame,
534 .flush= shorten_flush,