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