00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00027 #define DEBUG
00028 #include <limits.h>
00029 #include "avcodec.h"
00030 #include "bitstream.h"
00031 #include "golomb.h"
00032
00033 #define MAX_CHANNELS 8
00034 #define MAX_BLOCKSIZE 65535
00035
00036 #define OUT_BUFFER_SIZE 16384
00037
00038 #define ULONGSIZE 2
00039
00040 #define WAVE_FORMAT_PCM 0x0001
00041
00042 #define DEFAULT_BLOCK_SIZE 256
00043
00044 #define TYPESIZE 4
00045 #define CHANSIZE 0
00046 #define LPCQSIZE 2
00047 #define ENERGYSIZE 3
00048 #define BITSHIFTSIZE 2
00049
00050 #define TYPE_S16HL 3
00051 #define TYPE_S16LH 5
00052
00053 #define NWRAP 3
00054 #define NSKIPSIZE 1
00055
00056 #define LPCQUANT 5
00057 #define V2LPCQOFFSET (1 << LPCQUANT)
00058
00059 #define FNSIZE 2
00060 #define FN_DIFF0 0
00061 #define FN_DIFF1 1
00062 #define FN_DIFF2 2
00063 #define FN_DIFF3 3
00064 #define FN_QUIT 4
00065 #define FN_BLOCKSIZE 5
00066 #define FN_BITSHIFT 6
00067 #define FN_QLPC 7
00068 #define FN_ZERO 8
00069 #define FN_VERBATIM 9
00070
00071 #define VERBATIM_CKSIZE_SIZE 5
00072 #define VERBATIM_BYTE_SIZE 8
00073 #define CANONICAL_HEADER_SIZE 44
00074
00075 typedef struct ShortenContext {
00076 AVCodecContext *avctx;
00077 GetBitContext gb;
00078
00079 int min_framesize, max_framesize;
00080 int channels;
00081
00082 int32_t *decoded[MAX_CHANNELS];
00083 int32_t *offset[MAX_CHANNELS];
00084 uint8_t *bitstream;
00085 int bitstream_size;
00086 int bitstream_index;
00087 int allocated_bitstream_size;
00088 int header_size;
00089 uint8_t header[OUT_BUFFER_SIZE];
00090 int version;
00091 int cur_chan;
00092 int bitshift;
00093 int nmean;
00094 int internal_ftype;
00095 int nwrap;
00096 int blocksize;
00097 int bitindex;
00098 int32_t lpcqoffset;
00099 } ShortenContext;
00100
00101 static int shorten_decode_init(AVCodecContext * avctx)
00102 {
00103 ShortenContext *s = avctx->priv_data;
00104 s->avctx = avctx;
00105
00106 return 0;
00107 }
00108
00109 static void allocate_buffers(ShortenContext *s)
00110 {
00111 int i, chan;
00112 for (chan=0; chan<s->channels; chan++) {
00113 s->offset[chan] = av_realloc(s->offset[chan], sizeof(int32_t)*FFMAX(1, s->nmean));
00114
00115 s->decoded[chan] = av_realloc(s->decoded[chan], sizeof(int32_t)*(s->blocksize + s->nwrap));
00116 for (i=0; i<s->nwrap; i++)
00117 s->decoded[chan][i] = 0;
00118 s->decoded[chan] += s->nwrap;
00119
00120 }
00121 }
00122
00123
00124 static inline unsigned int get_uint(ShortenContext *s, int k)
00125 {
00126 if (s->version != 0)
00127 k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
00128 return get_ur_golomb_shorten(&s->gb, k);
00129 }
00130
00131
00132 static void fix_bitshift(ShortenContext *s, int32_t *buffer)
00133 {
00134 int i;
00135
00136 if (s->bitshift != 0)
00137 for (i = 0; i < s->blocksize; i++)
00138 buffer[s->nwrap + i] <<= s->bitshift;
00139 }
00140
00141
00142 static void init_offset(ShortenContext *s)
00143 {
00144 int32_t mean = 0;
00145 int chan, i;
00146 int nblock = FFMAX(1, s->nmean);
00147
00148 switch (s->internal_ftype)
00149 {
00150 case TYPE_S16HL:
00151 case TYPE_S16LH:
00152 mean = 0;
00153 break;
00154 default:
00155 av_log(s->avctx, AV_LOG_ERROR, "unknown audio type");
00156 abort();
00157 }
00158
00159 for (chan = 0; chan < s->channels; chan++)
00160 for (i = 0; i < nblock; i++)
00161 s->offset[chan][i] = mean;
00162 }
00163
00164 static int inline get_le32(GetBitContext *gb)
00165 {
00166 return bswap_32(get_bits_long(gb, 32));
00167 }
00168
00169 static short inline get_le16(GetBitContext *gb)
00170 {
00171 return bswap_16(get_bits_long(gb, 16));
00172 }
00173
00174 static int decode_wave_header(AVCodecContext *avctx, uint8_t *header, int header_size)
00175 {
00176 GetBitContext hb;
00177 int len;
00178 int chunk_size;
00179 short wave_format;
00180
00181 init_get_bits(&hb, header, header_size*8);
00182 if (get_le32(&hb) != MKTAG('R','I','F','F')) {
00183 av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
00184 return -1;
00185 }
00186
00187 chunk_size = get_le32(&hb);
00188
00189 if (get_le32(&hb) != MKTAG('W','A','V','E')) {
00190 av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
00191 return -1;
00192 }
00193
00194 while (get_le32(&hb) != MKTAG('f','m','t',' ')) {
00195 len = get_le32(&hb);
00196 skip_bits(&hb, 8*len);
00197 }
00198 len = get_le32(&hb);
00199
00200 if (len < 16) {
00201 av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
00202 return -1;
00203 }
00204
00205 wave_format = get_le16(&hb);
00206
00207 switch (wave_format) {
00208 case WAVE_FORMAT_PCM:
00209 break;
00210 default:
00211 av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
00212 return -1;
00213 }
00214
00215 avctx->channels = get_le16(&hb);
00216 avctx->sample_rate = get_le32(&hb);
00217 avctx->bit_rate = get_le32(&hb) * 8;
00218 avctx->block_align = get_le16(&hb);
00219 avctx->bits_per_sample = get_le16(&hb);
00220
00221 if (avctx->bits_per_sample != 16) {
00222 av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
00223 return -1;
00224 }
00225
00226 len -= 16;
00227 if (len > 0)
00228 av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
00229
00230 return 0;
00231 }
00232
00233 static int16_t * interleave_buffer(int16_t *samples, int nchan, int blocksize, int32_t **buffer) {
00234 int i, chan;
00235 for (i=0; i<blocksize; i++)
00236 for (chan=0; chan < nchan; chan++)
00237 *samples++ = FFMIN(buffer[chan][i], 32768);
00238 return samples;
00239 }
00240
00241 static void decode_subframe_lpc(ShortenContext *s, int channel, int residual_size, int pred_order)
00242 {
00243 int sum, i, j;
00244 int coeffs[pred_order];
00245
00246 for (i=0; i<pred_order; i++)
00247 coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
00248
00249 for (i=0; i < s->blocksize; i++) {
00250 sum = s->lpcqoffset;
00251 for (j=0; j<pred_order; j++)
00252 sum += coeffs[j] * s->decoded[channel][i-j-1];
00253 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + (sum >> LPCQUANT);
00254 }
00255 }
00256
00257
00258 static int shorten_decode_frame(AVCodecContext *avctx,
00259 void *data, int *data_size,
00260 uint8_t *buf, int buf_size)
00261 {
00262 ShortenContext *s = avctx->priv_data;
00263 int i, input_buf_size = 0;
00264 int16_t *samples = data;
00265 if(s->max_framesize == 0){
00266 s->max_framesize= 1024;
00267 s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
00268 }
00269
00270 if(1 && s->max_framesize){
00271 buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
00272 input_buf_size= buf_size;
00273
00274 if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
00275
00276 memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
00277 s->bitstream_index=0;
00278 }
00279 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
00280 buf= &s->bitstream[s->bitstream_index];
00281 buf_size += s->bitstream_size;
00282 s->bitstream_size= buf_size;
00283
00284 if(buf_size < s->max_framesize){
00285
00286 return input_buf_size;
00287 }
00288 }
00289 init_get_bits(&s->gb, buf, buf_size*8);
00290 get_bits(&s->gb, s->bitindex);
00291 if (!s->blocksize)
00292 {
00293 int maxnlpc = 0;
00294
00295 if (get_bits_long(&s->gb, 32) != bswap_32(ff_get_fourcc("ajkg"))) {
00296 av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
00297 return -1;
00298 }
00299
00300 s->lpcqoffset = 0;
00301 s->blocksize = DEFAULT_BLOCK_SIZE;
00302 s->channels = 1;
00303 s->nmean = -1;
00304 s->version = get_bits(&s->gb, 8);
00305 s->internal_ftype = get_uint(s, TYPESIZE);
00306
00307 s->channels = get_uint(s, CHANSIZE);
00308 if (s->channels > MAX_CHANNELS) {
00309 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
00310 return -1;
00311 }
00312
00313
00314 if (s->version > 0) {
00315 int skip_bytes;
00316 s->blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
00317 maxnlpc = get_uint(s, LPCQSIZE);
00318 s->nmean = get_uint(s, 0);
00319
00320 skip_bytes = get_uint(s, NSKIPSIZE);
00321 for (i=0; i<skip_bytes; i++) {
00322 skip_bits(&s->gb, 8);
00323 }
00324 }
00325 s->nwrap = FFMAX(NWRAP, maxnlpc);
00326
00327 allocate_buffers(s);
00328
00329 init_offset(s);
00330
00331 if (s->version > 1)
00332 s->lpcqoffset = V2LPCQOFFSET;
00333
00334 if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
00335 av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at begining of stream\n");
00336 return -1;
00337 }
00338
00339 s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
00340 if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
00341 av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
00342 return -1;
00343 }
00344
00345 for (i=0; i<s->header_size; i++)
00346 s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
00347
00348 if (decode_wave_header(avctx, s->header, s->header_size) < 0)
00349 return -1;
00350
00351 s->cur_chan = 0;
00352 s->bitshift = 0;
00353 }
00354 else
00355 {
00356 int cmd;
00357 int len;
00358 cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
00359 switch (cmd) {
00360 case FN_ZERO:
00361 case FN_DIFF0:
00362 case FN_DIFF1:
00363 case FN_DIFF2:
00364 case FN_DIFF3:
00365 case FN_QLPC:
00366 {
00367 int residual_size = 0;
00368 int channel = s->cur_chan;
00369 int32_t coffset;
00370 if (cmd != FN_ZERO) {
00371 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
00372
00373 if (s->version == 0)
00374 residual_size--;
00375 }
00376
00377 if (s->nmean == 0)
00378 coffset = s->offset[channel][0];
00379 else {
00380 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
00381 for (i=0; i<s->nmean; i++)
00382 sum += s->offset[channel][i];
00383 coffset = sum / s->nmean;
00384 if (s->version >= 2)
00385 coffset >>= FFMIN(1, s->bitshift);
00386 }
00387 switch (cmd) {
00388 case FN_ZERO:
00389 for (i=0; i<s->blocksize; i++)
00390 s->decoded[channel][i] = 0;
00391 break;
00392 case FN_DIFF0:
00393 for (i=0; i<s->blocksize; i++)
00394 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + coffset;
00395 break;
00396 case FN_DIFF1:
00397 for (i=0; i<s->blocksize; i++)
00398 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + s->decoded[channel][i - 1];
00399 break;
00400 case FN_DIFF2:
00401 for (i=0; i<s->blocksize; i++)
00402 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 2*s->decoded[channel][i-1]
00403 - s->decoded[channel][i-2];
00404 break;
00405 case FN_DIFF3:
00406 for (i=0; i<s->blocksize; i++)
00407 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 3*s->decoded[channel][i-1]
00408 - 3*s->decoded[channel][i-2]
00409 + s->decoded[channel][i-3];
00410 break;
00411 case FN_QLPC:
00412 {
00413 int pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
00414 for (i=0; i<pred_order; i++)
00415 s->decoded[channel][i - pred_order] -= coffset;
00416 decode_subframe_lpc(s, channel, residual_size, pred_order);
00417 if (coffset != 0)
00418 for (i=0; i < s->blocksize; i++)
00419 s->decoded[channel][i] += coffset;
00420 }
00421 }
00422 if (s->nmean > 0) {
00423 int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
00424 for (i=0; i<s->blocksize; i++)
00425 sum += s->decoded[channel][i];
00426
00427 for (i=1; i<s->nmean; i++)
00428 s->offset[channel][i-1] = s->offset[channel][i];
00429
00430 if (s->version < 2)
00431 s->offset[channel][s->nmean - 1] = sum / s->blocksize;
00432 else
00433 s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
00434 }
00435 for (i=-s->nwrap; i<0; i++)
00436 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
00437
00438 fix_bitshift(s, s->decoded[channel]);
00439
00440 s->cur_chan++;
00441 if (s->cur_chan == s->channels) {
00442 samples = interleave_buffer(samples, s->channels, s->blocksize, s->decoded);
00443 s->cur_chan = 0;
00444 goto frame_done;
00445 }
00446 break;
00447 }
00448 break;
00449 case FN_VERBATIM:
00450 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
00451 while (len--) {
00452 get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
00453 }
00454 break;
00455 case FN_BITSHIFT:
00456 s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
00457 break;
00458 case FN_BLOCKSIZE:
00459 s->blocksize = get_uint(s, av_log2(s->blocksize));
00460 break;
00461 case FN_QUIT:
00462 return buf_size;
00463 break;
00464 default:
00465 av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
00466 return -1;
00467 break;
00468 }
00469 }
00470 frame_done:
00471 *data_size = (int8_t *)samples - (int8_t *)data;
00472
00473
00474 s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
00475 i= (get_bits_count(&s->gb))/8;
00476 if (i > buf_size) {
00477 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
00478 s->bitstream_size=0;
00479 s->bitstream_index=0;
00480 return -1;
00481 }
00482 if (s->bitstream_size) {
00483 s->bitstream_index += i;
00484 s->bitstream_size -= i;
00485 return input_buf_size;
00486 } else
00487 return i;
00488 }
00489
00490 static int shorten_decode_close(AVCodecContext *avctx)
00491 {
00492 ShortenContext *s = avctx->priv_data;
00493 int i;
00494
00495 for (i = 0; i < s->channels; i++) {
00496 s->decoded[i] -= s->nwrap;
00497 av_freep(&s->decoded[i]);
00498 av_freep(&s->offset[i]);
00499 }
00500 av_freep(&s->bitstream);
00501 return 0;
00502 }
00503
00504 static void shorten_flush(AVCodecContext *avctx){
00505 ShortenContext *s = avctx->priv_data;
00506
00507 s->bitstream_size=
00508 s->bitstream_index= 0;
00509 }
00510
00511 AVCodec shorten_decoder = {
00512 "shorten",
00513 CODEC_TYPE_AUDIO,
00514 CODEC_ID_SHORTEN,
00515 sizeof(ShortenContext),
00516 shorten_decode_init,
00517 NULL,
00518 shorten_decode_close,
00519 shorten_decode_frame,
00520 .flush= shorten_flush,
00521 };