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], sizeof(int32_t)*(s->blocksize + s->nwrap));
00145 if (!tmp_ptr)
00146 return AVERROR(ENOMEM);
00147 s->decoded_base[chan] = tmp_ptr;
00148 for (i=0; i<s->nwrap; i++)
00149 s->decoded_base[chan][i] = 0;
00150 s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
00151 }
00152
00153 coeffs = av_realloc(s->coeffs, s->nwrap * sizeof(*s->coeffs));
00154 if (!coeffs)
00155 return AVERROR(ENOMEM);
00156 s->coeffs = coeffs;
00157
00158 return 0;
00159 }
00160
00161
00162 static inline unsigned int get_uint(ShortenContext *s, int k)
00163 {
00164 if (s->version != 0)
00165 k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
00166 return get_ur_golomb_shorten(&s->gb, k);
00167 }
00168
00169
00170 static void fix_bitshift(ShortenContext *s, int32_t *buffer)
00171 {
00172 int i;
00173
00174 if (s->bitshift != 0)
00175 for (i = 0; i < s->blocksize; i++)
00176 buffer[i] <<= s->bitshift;
00177 }
00178
00179
00180 static int init_offset(ShortenContext *s)
00181 {
00182 int32_t mean = 0;
00183 int chan, i;
00184 int nblock = FFMAX(1, s->nmean);
00185
00186 switch (s->internal_ftype)
00187 {
00188 case TYPE_S16HL:
00189 case TYPE_S16LH:
00190 mean = 0;
00191 break;
00192 default:
00193 av_log(s->avctx, AV_LOG_ERROR, "unknown audio type");
00194 return AVERROR_INVALIDDATA;
00195 }
00196
00197 for (chan = 0; chan < s->channels; chan++)
00198 for (i = 0; i < nblock; i++)
00199 s->offset[chan][i] = mean;
00200 return 0;
00201 }
00202
00203 static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header,
00204 int header_size)
00205 {
00206 int len;
00207 short wave_format;
00208 const uint8_t *end= header + header_size;
00209
00210 if (bytestream_get_le32(&header) != MKTAG('R','I','F','F')) {
00211 av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
00212 return -1;
00213 }
00214
00215 header += 4; ;
00216
00217 if (bytestream_get_le32(&header) != MKTAG('W','A','V','E')) {
00218 av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
00219 return -1;
00220 }
00221
00222 while (bytestream_get_le32(&header) != MKTAG('f','m','t',' ')) {
00223 len = bytestream_get_le32(&header);
00224 if(len<0 || end - header - 8 < len)
00225 return AVERROR_INVALIDDATA;
00226 header += len;
00227 }
00228 len = bytestream_get_le32(&header);
00229
00230 if (len < 16) {
00231 av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
00232 return -1;
00233 }
00234
00235 wave_format = bytestream_get_le16(&header);
00236
00237 switch (wave_format) {
00238 case WAVE_FORMAT_PCM:
00239 break;
00240 default:
00241 av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
00242 return -1;
00243 }
00244
00245 header += 2;
00246 avctx->sample_rate = bytestream_get_le32(&header);
00247 header += 4;
00248 header += 2;
00249 avctx->bits_per_coded_sample = bytestream_get_le16(&header);
00250
00251 if (avctx->bits_per_coded_sample != 16) {
00252 av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
00253 return -1;
00254 }
00255
00256 len -= 16;
00257 if (len > 0)
00258 av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
00259
00260 return 0;
00261 }
00262
00263 static void interleave_buffer(int16_t *samples, int nchan, int blocksize,
00264 int32_t **buffer)
00265 {
00266 int i, chan;
00267 for (i=0; i<blocksize; i++)
00268 for (chan=0; chan < nchan; chan++)
00269 *samples++ = av_clip_int16(buffer[chan][i]);
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= 1024;
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 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 >>= FFMIN(1, s->bitshift);
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
00581 s->frame.nb_samples = s->blocksize;
00582 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00583 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00584 return ret;
00585 }
00586
00587 interleave_buffer((int16_t *)s->frame.data[0], s->channels,
00588 s->blocksize, s->decoded);
00589
00590 *got_frame_ptr = 1;
00591 *(AVFrame *)data = s->frame;
00592 }
00593 }
00594 }
00595 if (s->cur_chan < s->channels)
00596 *got_frame_ptr = 0;
00597
00598 finish_frame:
00599 s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
00600 i= (get_bits_count(&s->gb))/8;
00601 if (i > buf_size) {
00602 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
00603 s->bitstream_size=0;
00604 s->bitstream_index=0;
00605 return -1;
00606 }
00607 if (s->bitstream_size) {
00608 s->bitstream_index += i;
00609 s->bitstream_size -= i;
00610 return input_buf_size;
00611 } else
00612 return i;
00613 }
00614
00615 static av_cold int shorten_decode_close(AVCodecContext *avctx)
00616 {
00617 ShortenContext *s = avctx->priv_data;
00618 int i;
00619
00620 for (i = 0; i < s->channels; i++) {
00621 s->decoded[i] = NULL;
00622 av_freep(&s->decoded_base[i]);
00623 av_freep(&s->offset[i]);
00624 }
00625 av_freep(&s->bitstream);
00626 av_freep(&s->coeffs);
00627
00628 return 0;
00629 }
00630
00631 AVCodec ff_shorten_decoder = {
00632 .name = "shorten",
00633 .type = AVMEDIA_TYPE_AUDIO,
00634 .id = CODEC_ID_SHORTEN,
00635 .priv_data_size = sizeof(ShortenContext),
00636 .init = shorten_decode_init,
00637 .close = shorten_decode_close,
00638 .decode = shorten_decode_frame,
00639 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
00640 .long_name= NULL_IF_CONFIG_SMALL("Shorten"),
00641 };