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