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