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 unsigned 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 AVERROR_INVALIDDATA;
00133 }
00134 if (s->blocksize + s->nwrap >= UINT_MAX / sizeof(int32_t) ||
00135 s->blocksize + s->nwrap <= (unsigned)s->nwrap) {
00136 av_log(s->avctx, AV_LOG_ERROR,
00137 "s->blocksize + s->nwrap too large\n");
00138 return AVERROR_INVALIDDATA;
00139 }
00140
00141 tmp_ptr =
00142 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 static inline unsigned int get_uint(ShortenContext *s, int k)
00166 {
00167 if (s->version != 0)
00168 k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
00169 return get_ur_golomb_shorten(&s->gb, k);
00170 }
00171
00172 static void fix_bitshift(ShortenContext *s, int32_t *buffer)
00173 {
00174 int i;
00175
00176 if (s->bitshift != 0)
00177 for (i = 0; i < s->blocksize; i++)
00178 buffer[i] <<= s->bitshift;
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 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 GetByteContext gb;
00209
00210 bytestream2_init(&gb, header, header_size);
00211
00212 if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
00213 av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
00214 return AVERROR_INVALIDDATA;
00215 }
00216
00217 bytestream2_skip(&gb, 4);
00218
00219 if (bytestream2_get_le32(&gb) != MKTAG('W', 'A', 'V', 'E')) {
00220 av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
00221 return AVERROR_INVALIDDATA;
00222 }
00223
00224 while (bytestream2_get_le32(&gb) != MKTAG('f', 'm', 't', ' ')) {
00225 len = bytestream2_get_le32(&gb);
00226 bytestream2_skip(&gb, len);
00227 if (bytestream2_get_bytes_left(&gb) < 16) {
00228 av_log(avctx, AV_LOG_ERROR, "no fmt chunk found\n");
00229 return AVERROR_INVALIDDATA;
00230 }
00231 }
00232 len = bytestream2_get_le32(&gb);
00233
00234 if (len < 16) {
00235 av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
00236 return AVERROR_INVALIDDATA;
00237 }
00238
00239 wave_format = bytestream2_get_le16(&gb);
00240
00241 switch (wave_format) {
00242 case WAVE_FORMAT_PCM:
00243 break;
00244 default:
00245 av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
00246 return AVERROR(ENOSYS);
00247 }
00248
00249 bytestream2_skip(&gb, 2);
00250 avctx->sample_rate = bytestream2_get_le32(&gb);
00251 bytestream2_skip(&gb, 4);
00252 bytestream2_skip(&gb, 2);
00253 avctx->bits_per_coded_sample = bytestream2_get_le16(&gb);
00254
00255 if (avctx->bits_per_coded_sample != 16) {
00256 av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
00257 return AVERROR(ENOSYS);
00258 }
00259
00260 len -= 16;
00261 if (len > 0)
00262 av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
00263
00264 return 0;
00265 }
00266
00267 static void interleave_buffer(int16_t *samples, int nchan, int blocksize,
00268 int32_t **buffer)
00269 {
00270 int i, chan;
00271 for (i=0; i<blocksize; i++)
00272 for (chan=0; chan < nchan; chan++)
00273 *samples++ = av_clip_int16(buffer[chan][i]);
00274 }
00275
00276 static const int fixed_coeffs[3][3] = {
00277 { 1, 0, 0 },
00278 { 2, -1, 0 },
00279 { 3, -3, 1 }
00280 };
00281
00282 static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
00283 int residual_size, int32_t coffset)
00284 {
00285 int pred_order, sum, qshift, init_sum, i, j;
00286 const int *coeffs;
00287
00288 if (command == FN_QLPC) {
00289
00290 pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
00291 if (pred_order > s->nwrap) {
00292 av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
00293 pred_order);
00294 return AVERROR(EINVAL);
00295 }
00296
00297 for (i = 0; i < pred_order; i++)
00298 s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
00299 coeffs = s->coeffs;
00300
00301 qshift = LPCQUANT;
00302 } else {
00303
00304 pred_order = command;
00305 coeffs = fixed_coeffs[pred_order - 1];
00306 qshift = 0;
00307 }
00308
00309
00310 if (command == FN_QLPC && coffset)
00311 for (i = -pred_order; i < 0; i++)
00312 s->decoded[channel][i] -= coffset;
00313
00314
00315 init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
00316 for (i = 0; i < s->blocksize; i++) {
00317 sum = init_sum;
00318 for (j = 0; j < pred_order; j++)
00319 sum += coeffs[j] * s->decoded[channel][i - j - 1];
00320 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) +
00321 (sum >> qshift);
00322 }
00323
00324
00325 if (command == FN_QLPC && coffset)
00326 for (i = 0; i < s->blocksize; i++)
00327 s->decoded[channel][i] += coffset;
00328
00329 return 0;
00330 }
00331
00332 static int read_header(ShortenContext *s)
00333 {
00334 int i, ret;
00335 int maxnlpc = 0;
00336
00337 if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
00338 av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
00339 return AVERROR_INVALIDDATA;
00340 }
00341
00342 s->lpcqoffset = 0;
00343 s->blocksize = DEFAULT_BLOCK_SIZE;
00344 s->nmean = -1;
00345 s->version = get_bits(&s->gb, 8);
00346 s->internal_ftype = get_uint(s, TYPESIZE);
00347
00348 s->channels = get_uint(s, CHANSIZE);
00349 if (!s->channels) {
00350 av_log(s->avctx, AV_LOG_ERROR, "No channels reported\n");
00351 return AVERROR_INVALIDDATA;
00352 }
00353 if (s->channels > MAX_CHANNELS) {
00354 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
00355 s->channels = 0;
00356 return AVERROR_INVALIDDATA;
00357 }
00358 s->avctx->channels = s->channels;
00359
00360
00361 if (s->version > 0) {
00362 int skip_bytes;
00363 unsigned blocksize;
00364
00365 blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
00366 if (!blocksize || blocksize > MAX_BLOCKSIZE) {
00367 av_log(s->avctx, AV_LOG_ERROR,
00368 "invalid or unsupported block size: %d\n",
00369 blocksize);
00370 return AVERROR(EINVAL);
00371 }
00372 s->blocksize = blocksize;
00373
00374 maxnlpc = get_uint(s, LPCQSIZE);
00375 s->nmean = get_uint(s, 0);
00376
00377 skip_bytes = get_uint(s, NSKIPSIZE);
00378 for (i = 0; i < skip_bytes; i++)
00379 skip_bits(&s->gb, 8);
00380 }
00381 s->nwrap = FFMAX(NWRAP, maxnlpc);
00382
00383 if ((ret = allocate_buffers(s)) < 0)
00384 return ret;
00385
00386 if ((ret = init_offset(s)) < 0)
00387 return ret;
00388
00389 if (s->version > 1)
00390 s->lpcqoffset = V2LPCQOFFSET;
00391
00392 if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
00393 av_log(s->avctx, AV_LOG_ERROR,
00394 "missing verbatim section at beginning of stream\n");
00395 return AVERROR_INVALIDDATA;
00396 }
00397
00398 s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
00399 if (s->header_size >= OUT_BUFFER_SIZE ||
00400 s->header_size < CANONICAL_HEADER_SIZE) {
00401 av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n",
00402 s->header_size);
00403 return AVERROR_INVALIDDATA;
00404 }
00405
00406 for (i = 0; i < s->header_size; i++)
00407 s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
00408
00409 if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0)
00410 return ret;
00411
00412 s->cur_chan = 0;
00413 s->bitshift = 0;
00414
00415 s->got_header = 1;
00416
00417 return 0;
00418 }
00419
00420 static int shorten_decode_frame(AVCodecContext *avctx, void *data,
00421 int *got_frame_ptr, AVPacket *avpkt)
00422 {
00423 const uint8_t *buf = avpkt->data;
00424 int buf_size = avpkt->size;
00425 ShortenContext *s = avctx->priv_data;
00426 int i, input_buf_size = 0;
00427 int ret;
00428
00429
00430 if (s->max_framesize == 0) {
00431 void *tmp_ptr;
00432 s->max_framesize = 1024;
00433 tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
00434 s->max_framesize + FF_INPUT_BUFFER_PADDING_SIZE);
00435 if (!tmp_ptr) {
00436 av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
00437 return AVERROR(ENOMEM);
00438 }
00439 s->bitstream = tmp_ptr;
00440 }
00441
00442
00443 if (1 && s->max_framesize) {
00444 buf_size = FFMIN(buf_size, s->max_framesize - s->bitstream_size);
00445 input_buf_size = buf_size;
00446
00447 if (s->bitstream_index + s->bitstream_size + buf_size >
00448 s->allocated_bitstream_size) {
00449 memmove(s->bitstream, &s->bitstream[s->bitstream_index],
00450 s->bitstream_size);
00451 s->bitstream_index = 0;
00452 }
00453 if (buf)
00454 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf,
00455 buf_size);
00456 buf = &s->bitstream[s->bitstream_index];
00457 buf_size += s->bitstream_size;
00458 s->bitstream_size = buf_size;
00459
00460
00461
00462 if (buf_size < s->max_framesize && avpkt->data) {
00463 *got_frame_ptr = 0;
00464 return input_buf_size;
00465 }
00466 }
00467
00468 init_get_bits(&s->gb, buf, buf_size * 8);
00469 skip_bits(&s->gb, s->bitindex);
00470
00471
00472 if (!s->got_header) {
00473 if ((ret = read_header(s)) < 0)
00474 return ret;
00475 *got_frame_ptr = 0;
00476 goto finish_frame;
00477 }
00478
00479
00480 if (s->got_quit_command) {
00481 *got_frame_ptr = 0;
00482 return avpkt->size;
00483 }
00484
00485 s->cur_chan = 0;
00486 while (s->cur_chan < s->channels) {
00487 int cmd;
00488 int len;
00489
00490 if (get_bits_left(&s->gb) < 3 + FNSIZE) {
00491 *got_frame_ptr = 0;
00492 break;
00493 }
00494
00495 cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
00496
00497 if (cmd > FN_VERBATIM) {
00498 av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
00499 *got_frame_ptr = 0;
00500 break;
00501 }
00502
00503 if (!is_audio_command[cmd]) {
00504
00505 switch (cmd) {
00506 case FN_VERBATIM:
00507 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
00508 while (len--)
00509 get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
00510 break;
00511 case FN_BITSHIFT:
00512 s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
00513 break;
00514 case FN_BLOCKSIZE: {
00515 unsigned blocksize = get_uint(s, av_log2(s->blocksize));
00516 if (blocksize > s->blocksize) {
00517 av_log(avctx, AV_LOG_ERROR,
00518 "Increasing block size is not supported\n");
00519 return AVERROR_PATCHWELCOME;
00520 }
00521 if (!blocksize || blocksize > MAX_BLOCKSIZE) {
00522 av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
00523 "block size: %d\n", blocksize);
00524 return AVERROR(EINVAL);
00525 }
00526 s->blocksize = blocksize;
00527 break;
00528 }
00529 case FN_QUIT:
00530 s->got_quit_command = 1;
00531 break;
00532 }
00533 if (cmd == FN_BLOCKSIZE || cmd == FN_QUIT) {
00534 *got_frame_ptr = 0;
00535 break;
00536 }
00537 } else {
00538
00539 int residual_size = 0;
00540 int channel = s->cur_chan;
00541 int32_t coffset;
00542
00543
00544 if (cmd != FN_ZERO) {
00545 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
00546
00547 if (s->version == 0)
00548 residual_size--;
00549 }
00550
00551
00552 if (s->nmean == 0)
00553 coffset = s->offset[channel][0];
00554 else {
00555 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
00556 for (i = 0; i < s->nmean; i++)
00557 sum += s->offset[channel][i];
00558 coffset = sum / s->nmean;
00559 if (s->version >= 2)
00560 coffset >>= FFMIN(1, s->bitshift);
00561 }
00562
00563
00564 if (cmd == FN_ZERO) {
00565 for (i = 0; i < s->blocksize; i++)
00566 s->decoded[channel][i] = 0;
00567 } else {
00568 if ((ret = decode_subframe_lpc(s, cmd, channel,
00569 residual_size, coffset)) < 0)
00570 return ret;
00571 }
00572
00573
00574 if (s->nmean > 0) {
00575 int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
00576 for (i = 0; i < s->blocksize; i++)
00577 sum += s->decoded[channel][i];
00578
00579 for (i = 1; i < s->nmean; i++)
00580 s->offset[channel][i - 1] = s->offset[channel][i];
00581
00582 if (s->version < 2)
00583 s->offset[channel][s->nmean - 1] = sum / s->blocksize;
00584 else
00585 s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
00586 }
00587
00588
00589 for (i = -s->nwrap; i < 0; i++)
00590 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
00591
00592
00593
00594 fix_bitshift(s, s->decoded[channel]);
00595
00596
00597 s->cur_chan++;
00598 if (s->cur_chan == s->channels) {
00599
00600 s->frame.nb_samples = s->blocksize;
00601 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00602 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00603 return ret;
00604 }
00605
00606 interleave_buffer((int16_t *)s->frame.data[0], s->channels,
00607 s->blocksize, s->decoded);
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 = 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 };