00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00029 #include <limits.h>
00030 #include "avcodec.h"
00031 #include "bytestream.h"
00032 #include "get_bits.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_S8 1
00053 #define TYPE_U8 2
00054 #define TYPE_S16HL 3
00055 #define TYPE_U16HL 4
00056 #define TYPE_S16LH 5
00057 #define TYPE_U16LH 6
00058
00059 #define NWRAP 3
00060 #define NSKIPSIZE 1
00061
00062 #define LPCQUANT 5
00063 #define V2LPCQOFFSET (1 << LPCQUANT)
00064
00065 #define FNSIZE 2
00066 #define FN_DIFF0 0
00067 #define FN_DIFF1 1
00068 #define FN_DIFF2 2
00069 #define FN_DIFF3 3
00070 #define FN_QUIT 4
00071 #define FN_BLOCKSIZE 5
00072 #define FN_BITSHIFT 6
00073 #define FN_QLPC 7
00074 #define FN_ZERO 8
00075 #define FN_VERBATIM 9
00076
00078 static const uint8_t is_audio_command[10] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0 };
00079
00080 #define VERBATIM_CKSIZE_SIZE 5
00081 #define VERBATIM_BYTE_SIZE 8
00082 #define CANONICAL_HEADER_SIZE 44
00083
00084 typedef struct ShortenContext {
00085 AVCodecContext *avctx;
00086 AVFrame frame;
00087 GetBitContext gb;
00088
00089 int min_framesize, max_framesize;
00090 int channels;
00091
00092 int32_t *decoded[MAX_CHANNELS];
00093 int32_t *decoded_base[MAX_CHANNELS];
00094 int32_t *offset[MAX_CHANNELS];
00095 int *coeffs;
00096 uint8_t *bitstream;
00097 int bitstream_size;
00098 int bitstream_index;
00099 unsigned int allocated_bitstream_size;
00100 int header_size;
00101 uint8_t header[OUT_BUFFER_SIZE];
00102 int version;
00103 int cur_chan;
00104 int bitshift;
00105 int nmean;
00106 int internal_ftype;
00107 int nwrap;
00108 int blocksize;
00109 int bitindex;
00110 int32_t lpcqoffset;
00111 int got_header;
00112 int got_quit_command;
00113 } ShortenContext;
00114
00115 static av_cold int shorten_decode_init(AVCodecContext * avctx)
00116 {
00117 ShortenContext *s = avctx->priv_data;
00118 s->avctx = avctx;
00119
00120 avcodec_get_frame_defaults(&s->frame);
00121 avctx->coded_frame = &s->frame;
00122
00123 return 0;
00124 }
00125
00126 static int allocate_buffers(ShortenContext *s)
00127 {
00128 int i, chan;
00129 int *coeffs;
00130 void *tmp_ptr;
00131
00132 for (chan=0; chan<s->channels; chan++) {
00133 if(FFMAX(1, s->nmean) >= UINT_MAX/sizeof(int32_t)){
00134 av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
00135 return -1;
00136 }
00137 if(s->blocksize + s->nwrap >= UINT_MAX/sizeof(int32_t) || s->blocksize + s->nwrap <= (unsigned)s->nwrap){
00138 av_log(s->avctx, AV_LOG_ERROR, "s->blocksize + s->nwrap too large\n");
00139 return -1;
00140 }
00141
00142 tmp_ptr = av_realloc(s->offset[chan], sizeof(int32_t)*FFMAX(1, s->nmean));
00143 if (!tmp_ptr)
00144 return AVERROR(ENOMEM);
00145 s->offset[chan] = tmp_ptr;
00146
00147 tmp_ptr = av_realloc(s->decoded_base[chan], (s->blocksize + s->nwrap) *
00148 sizeof(s->decoded_base[0][0]));
00149 if (!tmp_ptr)
00150 return AVERROR(ENOMEM);
00151 s->decoded_base[chan] = tmp_ptr;
00152 for (i=0; i<s->nwrap; i++)
00153 s->decoded_base[chan][i] = 0;
00154 s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
00155 }
00156
00157 coeffs = av_realloc(s->coeffs, s->nwrap * sizeof(*s->coeffs));
00158 if (!coeffs)
00159 return AVERROR(ENOMEM);
00160 s->coeffs = coeffs;
00161
00162 return 0;
00163 }
00164
00165
00166 static inline unsigned int get_uint(ShortenContext *s, int k)
00167 {
00168 if (s->version != 0)
00169 k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
00170 return get_ur_golomb_shorten(&s->gb, k);
00171 }
00172
00173
00174 static void fix_bitshift(ShortenContext *s, int32_t *buffer)
00175 {
00176 int i;
00177
00178 if (s->bitshift != 0)
00179 for (i = 0; i < s->blocksize; i++)
00180 buffer[i] <<= s->bitshift;
00181 }
00182
00183
00184 static int init_offset(ShortenContext *s)
00185 {
00186 int32_t mean = 0;
00187 int chan, i;
00188 int nblock = FFMAX(1, s->nmean);
00189
00190 switch (s->internal_ftype)
00191 {
00192 case TYPE_U8:
00193 s->avctx->sample_fmt = AV_SAMPLE_FMT_U8;
00194 mean = 0x80;
00195 break;
00196 case TYPE_S16HL:
00197 case TYPE_S16LH:
00198 s->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00199 break;
00200 default:
00201 av_log(s->avctx, AV_LOG_ERROR, "unknown audio type\n");
00202 return AVERROR_INVALIDDATA;
00203 }
00204
00205 for (chan = 0; chan < s->channels; chan++)
00206 for (i = 0; i < nblock; i++)
00207 s->offset[chan][i] = mean;
00208 return 0;
00209 }
00210
00211 static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header,
00212 int header_size)
00213 {
00214 int len, bps;
00215 short wave_format;
00216 const uint8_t *end= header + header_size;
00217
00218 if (bytestream_get_le32(&header) != MKTAG('R','I','F','F')) {
00219 av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
00220 return -1;
00221 }
00222
00223 header += 4; ;
00224
00225 if (bytestream_get_le32(&header) != MKTAG('W','A','V','E')) {
00226 av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
00227 return -1;
00228 }
00229
00230 while (bytestream_get_le32(&header) != MKTAG('f','m','t',' ')) {
00231 len = bytestream_get_le32(&header);
00232 if(len<0 || end - header - 8 < len)
00233 return AVERROR_INVALIDDATA;
00234 header += len;
00235 }
00236 len = bytestream_get_le32(&header);
00237
00238 if (len < 16) {
00239 av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
00240 return -1;
00241 }
00242
00243 wave_format = bytestream_get_le16(&header);
00244
00245 switch (wave_format) {
00246 case WAVE_FORMAT_PCM:
00247 break;
00248 default:
00249 av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
00250 return -1;
00251 }
00252
00253 header += 2;
00254 avctx->sample_rate = bytestream_get_le32(&header);
00255 header += 4;
00256 header += 2;
00257 bps = bytestream_get_le16(&header);
00258 avctx->bits_per_coded_sample = bps;
00259
00260 if (bps != 16 && bps != 8) {
00261 av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample: %d\n", bps);
00262 return -1;
00263 }
00264
00265 len -= 16;
00266 if (len > 0)
00267 av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
00268
00269 return 0;
00270 }
00271
00272 static const int fixed_coeffs[3][3] = {
00273 { 1, 0, 0 },
00274 { 2, -1, 0 },
00275 { 3, -3, 1 }
00276 };
00277
00278 static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
00279 int residual_size, int32_t coffset)
00280 {
00281 int pred_order, sum, qshift, init_sum, i, j;
00282 const int *coeffs;
00283
00284 if (command == FN_QLPC) {
00285
00286 pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
00287 if (pred_order > s->nwrap) {
00288 av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n", pred_order);
00289 return AVERROR(EINVAL);
00290 }
00291
00292 for (i=0; i<pred_order; i++)
00293 s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
00294 coeffs = s->coeffs;
00295
00296 qshift = LPCQUANT;
00297 } else {
00298
00299 pred_order = command;
00300 coeffs = fixed_coeffs[pred_order-1];
00301 qshift = 0;
00302 }
00303
00304
00305 if (command == FN_QLPC && coffset)
00306 for (i = -pred_order; i < 0; i++)
00307 s->decoded[channel][i] -= coffset;
00308
00309
00310 init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
00311 for (i=0; i < s->blocksize; i++) {
00312 sum = init_sum;
00313 for (j=0; j<pred_order; j++)
00314 sum += coeffs[j] * s->decoded[channel][i-j-1];
00315 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + (sum >> qshift);
00316 }
00317
00318
00319 if (command == FN_QLPC && coffset)
00320 for (i = 0; i < s->blocksize; i++)
00321 s->decoded[channel][i] += coffset;
00322
00323 return 0;
00324 }
00325
00326 static int read_header(ShortenContext *s)
00327 {
00328 int i, ret;
00329 int maxnlpc = 0;
00330
00331 if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
00332 av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
00333 return -1;
00334 }
00335
00336 s->lpcqoffset = 0;
00337 s->blocksize = DEFAULT_BLOCK_SIZE;
00338 s->nmean = -1;
00339 s->version = get_bits(&s->gb, 8);
00340 s->internal_ftype = get_uint(s, TYPESIZE);
00341
00342 s->channels = get_uint(s, CHANSIZE);
00343 if (s->channels > MAX_CHANNELS) {
00344 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
00345 return -1;
00346 }
00347 s->avctx->channels = s->channels;
00348
00349
00350 if (s->version > 0) {
00351 int skip_bytes, blocksize;
00352
00353 blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
00354 if (!blocksize || blocksize > MAX_BLOCKSIZE) {
00355 av_log(s->avctx, AV_LOG_ERROR, "invalid or unsupported block size: %d\n",
00356 blocksize);
00357 return AVERROR(EINVAL);
00358 }
00359 s->blocksize = blocksize;
00360
00361 maxnlpc = get_uint(s, LPCQSIZE);
00362 s->nmean = get_uint(s, 0);
00363
00364 skip_bytes = get_uint(s, NSKIPSIZE);
00365 for (i=0; i<skip_bytes; i++) {
00366 skip_bits(&s->gb, 8);
00367 }
00368 }
00369 s->nwrap = FFMAX(NWRAP, maxnlpc);
00370
00371 if ((ret = allocate_buffers(s)) < 0)
00372 return ret;
00373
00374 if ((ret = init_offset(s)) < 0)
00375 return ret;
00376
00377 if (s->version > 1)
00378 s->lpcqoffset = V2LPCQOFFSET;
00379
00380 if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
00381 av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at beginning of stream\n");
00382 return -1;
00383 }
00384
00385 s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
00386 if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
00387 av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
00388 return -1;
00389 }
00390
00391 for (i=0; i<s->header_size; i++)
00392 s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
00393
00394 if (decode_wave_header(s->avctx, s->header, s->header_size) < 0)
00395 return -1;
00396
00397 s->cur_chan = 0;
00398 s->bitshift = 0;
00399
00400 s->got_header = 1;
00401
00402 return 0;
00403 }
00404
00405 static int shorten_decode_frame(AVCodecContext *avctx, void *data,
00406 int *got_frame_ptr, AVPacket *avpkt)
00407 {
00408 const uint8_t *buf = avpkt->data;
00409 int buf_size = avpkt->size;
00410 ShortenContext *s = avctx->priv_data;
00411 int i, input_buf_size = 0;
00412 int ret;
00413
00414
00415 if(s->max_framesize == 0){
00416 void *tmp_ptr;
00417 s->max_framesize= 8192;
00418 tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
00419 s->max_framesize);
00420 if (!tmp_ptr) {
00421 av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
00422 return AVERROR(ENOMEM);
00423 }
00424 s->bitstream = tmp_ptr;
00425 }
00426
00427
00428 if(1 && s->max_framesize){
00429 buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
00430 input_buf_size= buf_size;
00431
00432 if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
00433 memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
00434 s->bitstream_index=0;
00435 }
00436 if (buf)
00437 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
00438 buf= &s->bitstream[s->bitstream_index];
00439 buf_size += s->bitstream_size;
00440 s->bitstream_size= buf_size;
00441
00442
00443
00444 if (buf_size < s->max_framesize && avpkt->data) {
00445 *got_frame_ptr = 0;
00446 return input_buf_size;
00447 }
00448 }
00449
00450 init_get_bits(&s->gb, buf, buf_size*8);
00451 skip_bits(&s->gb, s->bitindex);
00452
00453
00454 if (!s->got_header) {
00455 if ((ret = read_header(s)) < 0)
00456 return ret;
00457 *got_frame_ptr = 0;
00458 goto finish_frame;
00459 }
00460
00461
00462 if (s->got_quit_command) {
00463 *got_frame_ptr = 0;
00464 return avpkt->size;
00465 }
00466
00467 s->cur_chan = 0;
00468 while (s->cur_chan < s->channels) {
00469 unsigned int cmd;
00470 int len;
00471
00472 if (get_bits_left(&s->gb) < 3+FNSIZE) {
00473 *got_frame_ptr = 0;
00474 break;
00475 }
00476
00477 cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
00478
00479 if (cmd > FN_VERBATIM) {
00480 av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
00481 *got_frame_ptr = 0;
00482 break;
00483 }
00484
00485 if (!is_audio_command[cmd]) {
00486
00487 switch (cmd) {
00488 case FN_VERBATIM:
00489 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
00490 while (len--) {
00491 get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
00492 }
00493 break;
00494 case FN_BITSHIFT:
00495 s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
00496 break;
00497 case FN_BLOCKSIZE: {
00498 int blocksize = get_uint(s, av_log2(s->blocksize));
00499 if (blocksize > s->blocksize) {
00500 av_log(avctx, AV_LOG_ERROR, "Increasing block size is not supported\n");
00501 return AVERROR_PATCHWELCOME;
00502 }
00503 if (!blocksize || blocksize > MAX_BLOCKSIZE) {
00504 av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
00505 "block size: %d\n", blocksize);
00506 return AVERROR(EINVAL);
00507 }
00508 s->blocksize = blocksize;
00509 break;
00510 }
00511 case FN_QUIT:
00512 s->got_quit_command = 1;
00513 break;
00514 }
00515 if (cmd == FN_BLOCKSIZE || cmd == FN_QUIT) {
00516 *got_frame_ptr = 0;
00517 break;
00518 }
00519 } else {
00520
00521 int residual_size = 0;
00522 int channel = s->cur_chan;
00523 int32_t coffset;
00524
00525
00526 if (cmd != FN_ZERO) {
00527 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
00528
00529 if (s->version == 0)
00530 residual_size--;
00531 }
00532
00533
00534 if (s->nmean == 0)
00535 coffset = s->offset[channel][0];
00536 else {
00537 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
00538 for (i=0; i<s->nmean; i++)
00539 sum += s->offset[channel][i];
00540 coffset = sum / s->nmean;
00541 if (s->version >= 2)
00542 coffset = s->bitshift == 0 ? coffset : coffset >> s->bitshift - 1 >> 1;
00543 }
00544
00545
00546 if (cmd == FN_ZERO) {
00547 for (i=0; i<s->blocksize; i++)
00548 s->decoded[channel][i] = 0;
00549 } else {
00550 if ((ret = decode_subframe_lpc(s, cmd, channel, residual_size, coffset)) < 0)
00551 return ret;
00552 }
00553
00554
00555 if (s->nmean > 0) {
00556 int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
00557 for (i=0; i<s->blocksize; i++)
00558 sum += s->decoded[channel][i];
00559
00560 for (i=1; i<s->nmean; i++)
00561 s->offset[channel][i-1] = s->offset[channel][i];
00562
00563 if (s->version < 2)
00564 s->offset[channel][s->nmean - 1] = sum / s->blocksize;
00565 else
00566 s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
00567 }
00568
00569
00570 for (i=-s->nwrap; i<0; i++)
00571 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
00572
00573
00574
00575 fix_bitshift(s, s->decoded[channel]);
00576
00577
00578 s->cur_chan++;
00579 if (s->cur_chan == s->channels) {
00580 uint8_t *samples_u8;
00581 int16_t *samples_s16;
00582 int chan;
00583
00584
00585 s->frame.nb_samples = s->blocksize;
00586 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00587 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00588 return ret;
00589 }
00590 samples_u8 = (uint8_t *)s->frame.data[0];
00591 samples_s16 = (int16_t *)s->frame.data[0];
00592
00593 for (i = 0; i < s->blocksize; i++) {
00594 for (chan = 0; chan < s->channels; chan++) {
00595 switch (s->internal_ftype) {
00596 case TYPE_U8:
00597 *samples_u8++ = av_clip_uint8(s->decoded[chan][i]);
00598 break;
00599 case TYPE_S16HL:
00600 case TYPE_S16LH:
00601 *samples_s16++ = av_clip_int16(s->decoded[chan][i]);
00602 break;
00603 }
00604 }
00605 }
00606
00607 *got_frame_ptr = 1;
00608 *(AVFrame *)data = s->frame;
00609 }
00610 }
00611 }
00612 if (s->cur_chan < s->channels)
00613 *got_frame_ptr = 0;
00614
00615 finish_frame:
00616 s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
00617 i= (get_bits_count(&s->gb))/8;
00618 if (i > buf_size) {
00619 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
00620 s->bitstream_size=0;
00621 s->bitstream_index=0;
00622 return -1;
00623 }
00624 if (s->bitstream_size) {
00625 s->bitstream_index += i;
00626 s->bitstream_size -= i;
00627 return input_buf_size;
00628 } else
00629 return i;
00630 }
00631
00632 static av_cold int shorten_decode_close(AVCodecContext *avctx)
00633 {
00634 ShortenContext *s = avctx->priv_data;
00635 int i;
00636
00637 for (i = 0; i < s->channels; i++) {
00638 s->decoded[i] = NULL;
00639 av_freep(&s->decoded_base[i]);
00640 av_freep(&s->offset[i]);
00641 }
00642 av_freep(&s->bitstream);
00643 av_freep(&s->coeffs);
00644
00645 return 0;
00646 }
00647
00648 AVCodec ff_shorten_decoder = {
00649 .name = "shorten",
00650 .type = AVMEDIA_TYPE_AUDIO,
00651 .id = AV_CODEC_ID_SHORTEN,
00652 .priv_data_size = sizeof(ShortenContext),
00653 .init = shorten_decode_init,
00654 .close = shorten_decode_close,
00655 .decode = shorten_decode_frame,
00656 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
00657 .long_name = NULL_IF_CONFIG_SMALL("Shorten"),
00658 };