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 const uint8_t *end= header + header_size;
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 if(len<0 || end - header - 8 < len)
00226 return AVERROR_INVALIDDATA;
00227 header += len;
00228 }
00229 len = bytestream_get_le32(&header);
00230
00231 if (len < 16) {
00232 av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
00233 return -1;
00234 }
00235
00236 wave_format = bytestream_get_le16(&header);
00237
00238 switch (wave_format) {
00239 case WAVE_FORMAT_PCM:
00240 break;
00241 default:
00242 av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
00243 return -1;
00244 }
00245
00246 header += 2;
00247 avctx->sample_rate = bytestream_get_le32(&header);
00248 header += 4;
00249 header += 2;
00250 avctx->bits_per_coded_sample = bytestream_get_le16(&header);
00251
00252 if (avctx->bits_per_coded_sample != 16) {
00253 av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
00254 return -1;
00255 }
00256
00257 len -= 16;
00258 if (len > 0)
00259 av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
00260
00261 return 0;
00262 }
00263
00264 static void interleave_buffer(int16_t *samples, int nchan, int blocksize,
00265 int32_t **buffer)
00266 {
00267 int i, chan;
00268 for (i=0; i<blocksize; i++)
00269 for (chan=0; chan < nchan; chan++)
00270 *samples++ = av_clip_int16(buffer[chan][i]);
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 -1;
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 > MAX_CHANNELS) {
00345 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
00346 return -1;
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 -1;
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 -1;
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 (decode_wave_header(s->avctx, s->header, s->header_size) < 0)
00396 return -1;
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= 1024;
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 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 >>= FFMIN(1, s->bitshift);
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
00582 s->frame.nb_samples = s->blocksize;
00583 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00584 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00585 return ret;
00586 }
00587
00588 interleave_buffer((int16_t *)s->frame.data[0], s->channels,
00589 s->blocksize, s->decoded);
00590
00591 *got_frame_ptr = 1;
00592 *(AVFrame *)data = s->frame;
00593 }
00594 }
00595 }
00596 if (s->cur_chan < s->channels)
00597 *got_frame_ptr = 0;
00598
00599 finish_frame:
00600 s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
00601 i= (get_bits_count(&s->gb))/8;
00602 if (i > buf_size) {
00603 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
00604 s->bitstream_size=0;
00605 s->bitstream_index=0;
00606 return -1;
00607 }
00608 if (s->bitstream_size) {
00609 s->bitstream_index += i;
00610 s->bitstream_size -= i;
00611 return input_buf_size;
00612 } else
00613 return i;
00614 }
00615
00616 static av_cold int shorten_decode_close(AVCodecContext *avctx)
00617 {
00618 ShortenContext *s = avctx->priv_data;
00619 int i;
00620
00621 for (i = 0; i < s->channels; i++) {
00622 s->decoded[i] = NULL;
00623 av_freep(&s->decoded_base[i]);
00624 av_freep(&s->offset[i]);
00625 }
00626 av_freep(&s->bitstream);
00627 av_freep(&s->coeffs);
00628
00629 return 0;
00630 }
00631
00632 AVCodec ff_shorten_decoder = {
00633 .name = "shorten",
00634 .type = AVMEDIA_TYPE_AUDIO,
00635 .id = CODEC_ID_SHORTEN,
00636 .priv_data_size = sizeof(ShortenContext),
00637 .init = shorten_decode_init,
00638 .close = shorten_decode_close,
00639 .decode = shorten_decode_frame,
00640 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
00641 .long_name= NULL_IF_CONFIG_SMALL("Shorten"),
00642 };