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