Libav
|
00001 /* 00002 * Shorten decoder 00003 * Copyright (c) 2005 Jeff Muizelaar 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00029 #define DEBUG 00030 #include <limits.h> 00031 #include "avcodec.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 00073 #define VERBATIM_CKSIZE_SIZE 5 00074 #define VERBATIM_BYTE_SIZE 8 00075 #define CANONICAL_HEADER_SIZE 44 00076 00077 typedef struct ShortenContext { 00078 AVCodecContext *avctx; 00079 GetBitContext gb; 00080 00081 int min_framesize, max_framesize; 00082 int channels; 00083 00084 int32_t *decoded[MAX_CHANNELS]; 00085 int32_t *decoded_base[MAX_CHANNELS]; 00086 int32_t *offset[MAX_CHANNELS]; 00087 int *coeffs; 00088 uint8_t *bitstream; 00089 int bitstream_size; 00090 int bitstream_index; 00091 unsigned int allocated_bitstream_size; 00092 int header_size; 00093 uint8_t header[OUT_BUFFER_SIZE]; 00094 int version; 00095 int cur_chan; 00096 int bitshift; 00097 int nmean; 00098 int internal_ftype; 00099 int nwrap; 00100 int blocksize; 00101 int bitindex; 00102 int32_t lpcqoffset; 00103 } ShortenContext; 00104 00105 static av_cold int shorten_decode_init(AVCodecContext * avctx) 00106 { 00107 ShortenContext *s = avctx->priv_data; 00108 s->avctx = avctx; 00109 avctx->sample_fmt = SAMPLE_FMT_S16; 00110 00111 return 0; 00112 } 00113 00114 static int allocate_buffers(ShortenContext *s) 00115 { 00116 int i, chan; 00117 int *coeffs; 00118 void *tmp_ptr; 00119 00120 for (chan=0; chan<s->channels; chan++) { 00121 if(FFMAX(1, s->nmean) >= UINT_MAX/sizeof(int32_t)){ 00122 av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n"); 00123 return -1; 00124 } 00125 if(s->blocksize + s->nwrap >= UINT_MAX/sizeof(int32_t) || s->blocksize + s->nwrap <= (unsigned)s->nwrap){ 00126 av_log(s->avctx, AV_LOG_ERROR, "s->blocksize + s->nwrap too large\n"); 00127 return -1; 00128 } 00129 00130 tmp_ptr = av_realloc(s->offset[chan], sizeof(int32_t)*FFMAX(1, s->nmean)); 00131 if (!tmp_ptr) 00132 return AVERROR(ENOMEM); 00133 s->offset[chan] = tmp_ptr; 00134 00135 tmp_ptr = av_realloc(s->decoded_base[chan], (s->blocksize + s->nwrap) * 00136 sizeof(s->decoded_base[0][0])); 00137 if (!tmp_ptr) 00138 return AVERROR(ENOMEM); 00139 s->decoded_base[chan] = tmp_ptr; 00140 for (i=0; i<s->nwrap; i++) 00141 s->decoded_base[chan][i] = 0; 00142 s->decoded[chan] = s->decoded_base[chan] + s->nwrap; 00143 } 00144 00145 coeffs = av_realloc(s->coeffs, s->nwrap * sizeof(*s->coeffs)); 00146 if (!coeffs) 00147 return AVERROR(ENOMEM); 00148 s->coeffs = coeffs; 00149 00150 return 0; 00151 } 00152 00153 00154 static inline unsigned int get_uint(ShortenContext *s, int k) 00155 { 00156 if (s->version != 0) 00157 k = get_ur_golomb_shorten(&s->gb, ULONGSIZE); 00158 return get_ur_golomb_shorten(&s->gb, k); 00159 } 00160 00161 00162 static void fix_bitshift(ShortenContext *s, int32_t *buffer) 00163 { 00164 int i; 00165 00166 if (s->bitshift != 0) 00167 for (i = 0; i < s->blocksize; i++) 00168 buffer[i] <<= s->bitshift; 00169 } 00170 00171 00172 static void init_offset(ShortenContext *s) 00173 { 00174 int32_t mean = 0; 00175 int chan, i; 00176 int nblock = FFMAX(1, s->nmean); 00177 /* initialise offset */ 00178 switch (s->internal_ftype) 00179 { 00180 case TYPE_S16HL: 00181 case TYPE_S16LH: 00182 mean = 0; 00183 break; 00184 default: 00185 av_log(s->avctx, AV_LOG_ERROR, "unknown audio type"); 00186 abort(); 00187 } 00188 00189 for (chan = 0; chan < s->channels; chan++) 00190 for (i = 0; i < nblock; i++) 00191 s->offset[chan][i] = mean; 00192 } 00193 00194 static inline int get_le32(GetBitContext *gb) 00195 { 00196 return bswap_32(get_bits_long(gb, 32)); 00197 } 00198 00199 static inline short get_le16(GetBitContext *gb) 00200 { 00201 return bswap_16(get_bits_long(gb, 16)); 00202 } 00203 00204 static int decode_wave_header(AVCodecContext *avctx, uint8_t *header, int header_size) 00205 { 00206 GetBitContext hb; 00207 int len; 00208 int chunk_size; 00209 short wave_format; 00210 00211 init_get_bits(&hb, header, header_size*8); 00212 if (get_le32(&hb) != MKTAG('R','I','F','F')) { 00213 av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n"); 00214 return -1; 00215 } 00216 00217 chunk_size = get_le32(&hb); 00218 00219 if (get_le32(&hb) != MKTAG('W','A','V','E')) { 00220 av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n"); 00221 return -1; 00222 } 00223 00224 while (get_le32(&hb) != MKTAG('f','m','t',' ')) { 00225 len = get_le32(&hb); 00226 skip_bits(&hb, 8*len); 00227 } 00228 len = get_le32(&hb); 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 = get_le16(&hb); 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 avctx->channels = get_le16(&hb); 00246 avctx->sample_rate = get_le32(&hb); 00247 avctx->bit_rate = get_le32(&hb) * 8; 00248 avctx->block_align = get_le16(&hb); 00249 avctx->bits_per_coded_sample = get_le16(&hb); 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 int16_t * interleave_buffer(int16_t *samples, int nchan, int blocksize, int32_t **buffer) { 00264 int i, chan; 00265 for (i=0; i<blocksize; i++) 00266 for (chan=0; chan < nchan; chan++) 00267 *samples++ = FFMIN(buffer[chan][i], 32768); 00268 return samples; 00269 } 00270 00271 static void decode_subframe_lpc(ShortenContext *s, int channel, int residual_size, int pred_order) 00272 { 00273 int sum, i, j; 00274 int *coeffs = s->coeffs; 00275 00276 for (i=0; i<pred_order; i++) 00277 coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT); 00278 00279 for (i=0; i < s->blocksize; i++) { 00280 sum = s->lpcqoffset; 00281 for (j=0; j<pred_order; j++) 00282 sum += coeffs[j] * s->decoded[channel][i-j-1]; 00283 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + (sum >> LPCQUANT); 00284 } 00285 } 00286 00287 00288 static int shorten_decode_frame(AVCodecContext *avctx, 00289 void *data, int *data_size, 00290 AVPacket *avpkt) 00291 { 00292 const uint8_t *buf = avpkt->data; 00293 int buf_size = avpkt->size; 00294 ShortenContext *s = avctx->priv_data; 00295 int i, input_buf_size = 0; 00296 int16_t *samples = data; 00297 if(s->max_framesize == 0){ 00298 void *tmp_ptr; 00299 s->max_framesize= 1024; // should hopefully be enough for the first header 00300 tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, 00301 s->max_framesize); 00302 if (!tmp_ptr) { 00303 av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n"); 00304 return AVERROR(ENOMEM); 00305 } 00306 s->bitstream = tmp_ptr; 00307 } 00308 00309 if(1 && s->max_framesize){//FIXME truncated 00310 buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size); 00311 input_buf_size= buf_size; 00312 00313 if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){ 00314 // printf("memmove\n"); 00315 memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size); 00316 s->bitstream_index=0; 00317 } 00318 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size); 00319 buf= &s->bitstream[s->bitstream_index]; 00320 buf_size += s->bitstream_size; 00321 s->bitstream_size= buf_size; 00322 00323 if(buf_size < s->max_framesize){ 00324 //dprintf(avctx, "wanna more data ... %d\n", buf_size); 00325 *data_size = 0; 00326 return input_buf_size; 00327 } 00328 } 00329 init_get_bits(&s->gb, buf, buf_size*8); 00330 skip_bits(&s->gb, s->bitindex); 00331 if (!s->blocksize) 00332 { 00333 int maxnlpc = 0; 00334 /* shorten signature */ 00335 if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) { 00336 av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n"); 00337 return -1; 00338 } 00339 00340 s->lpcqoffset = 0; 00341 s->blocksize = DEFAULT_BLOCK_SIZE; 00342 s->channels = 1; 00343 s->nmean = -1; 00344 s->version = get_bits(&s->gb, 8); 00345 s->internal_ftype = get_uint(s, TYPESIZE); 00346 00347 s->channels = get_uint(s, CHANSIZE); 00348 if (s->channels > MAX_CHANNELS) { 00349 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels); 00350 return -1; 00351 } 00352 00353 /* get blocksize if version > 0 */ 00354 if (s->version > 0) { 00355 int skip_bytes; 00356 s->blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE)); 00357 maxnlpc = get_uint(s, LPCQSIZE); 00358 s->nmean = get_uint(s, 0); 00359 00360 skip_bytes = get_uint(s, NSKIPSIZE); 00361 for (i=0; i<skip_bytes; i++) { 00362 skip_bits(&s->gb, 8); 00363 } 00364 } 00365 s->nwrap = FFMAX(NWRAP, maxnlpc); 00366 00367 if (allocate_buffers(s)) 00368 return -1; 00369 00370 init_offset(s); 00371 00372 if (s->version > 1) 00373 s->lpcqoffset = V2LPCQOFFSET; 00374 00375 if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) { 00376 av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at beginning of stream\n"); 00377 return -1; 00378 } 00379 00380 s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE); 00381 if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) { 00382 av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size); 00383 return -1; 00384 } 00385 00386 for (i=0; i<s->header_size; i++) 00387 s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE); 00388 00389 if (decode_wave_header(avctx, s->header, s->header_size) < 0) 00390 return -1; 00391 00392 s->cur_chan = 0; 00393 s->bitshift = 0; 00394 } 00395 else 00396 { 00397 int cmd; 00398 int len; 00399 cmd = get_ur_golomb_shorten(&s->gb, FNSIZE); 00400 switch (cmd) { 00401 case FN_ZERO: 00402 case FN_DIFF0: 00403 case FN_DIFF1: 00404 case FN_DIFF2: 00405 case FN_DIFF3: 00406 case FN_QLPC: 00407 { 00408 int residual_size = 0; 00409 int channel = s->cur_chan; 00410 int32_t coffset; 00411 if (cmd != FN_ZERO) { 00412 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE); 00413 /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */ 00414 if (s->version == 0) 00415 residual_size--; 00416 } 00417 00418 if (s->nmean == 0) 00419 coffset = s->offset[channel][0]; 00420 else { 00421 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2; 00422 for (i=0; i<s->nmean; i++) 00423 sum += s->offset[channel][i]; 00424 coffset = sum / s->nmean; 00425 if (s->version >= 2) 00426 coffset >>= FFMIN(1, s->bitshift); 00427 } 00428 switch (cmd) { 00429 case FN_ZERO: 00430 for (i=0; i<s->blocksize; i++) 00431 s->decoded[channel][i] = 0; 00432 break; 00433 case FN_DIFF0: 00434 for (i=0; i<s->blocksize; i++) 00435 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + coffset; 00436 break; 00437 case FN_DIFF1: 00438 for (i=0; i<s->blocksize; i++) 00439 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + s->decoded[channel][i - 1]; 00440 break; 00441 case FN_DIFF2: 00442 for (i=0; i<s->blocksize; i++) 00443 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 2*s->decoded[channel][i-1] 00444 - s->decoded[channel][i-2]; 00445 break; 00446 case FN_DIFF3: 00447 for (i=0; i<s->blocksize; i++) 00448 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 3*s->decoded[channel][i-1] 00449 - 3*s->decoded[channel][i-2] 00450 + s->decoded[channel][i-3]; 00451 break; 00452 case FN_QLPC: 00453 { 00454 int pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE); 00455 if (pred_order > s->nwrap) { 00456 av_log(avctx, AV_LOG_ERROR, 00457 "invalid pred_order %d\n", 00458 pred_order); 00459 return -1; 00460 } 00461 for (i=0; i<pred_order; i++) 00462 s->decoded[channel][i - pred_order] -= coffset; 00463 decode_subframe_lpc(s, channel, residual_size, pred_order); 00464 if (coffset != 0) 00465 for (i=0; i < s->blocksize; i++) 00466 s->decoded[channel][i] += coffset; 00467 } 00468 } 00469 if (s->nmean > 0) { 00470 int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2; 00471 for (i=0; i<s->blocksize; i++) 00472 sum += s->decoded[channel][i]; 00473 00474 for (i=1; i<s->nmean; i++) 00475 s->offset[channel][i-1] = s->offset[channel][i]; 00476 00477 if (s->version < 2) 00478 s->offset[channel][s->nmean - 1] = sum / s->blocksize; 00479 else 00480 s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift; 00481 } 00482 for (i=-s->nwrap; i<0; i++) 00483 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize]; 00484 00485 fix_bitshift(s, s->decoded[channel]); 00486 00487 s->cur_chan++; 00488 if (s->cur_chan == s->channels) { 00489 samples = interleave_buffer(samples, s->channels, s->blocksize, s->decoded); 00490 s->cur_chan = 0; 00491 goto frame_done; 00492 } 00493 break; 00494 } 00495 break; 00496 case FN_VERBATIM: 00497 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE); 00498 while (len--) { 00499 get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE); 00500 } 00501 break; 00502 case FN_BITSHIFT: 00503 s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE); 00504 break; 00505 case FN_BLOCKSIZE: { 00506 int blocksize = get_uint(s, av_log2(s->blocksize)); 00507 if (blocksize > s->blocksize) { 00508 av_log(avctx, AV_LOG_ERROR, "Increasing block size is not supported\n"); 00509 return AVERROR_PATCHWELCOME; 00510 } 00511 s->blocksize = blocksize; 00512 break; 00513 } 00514 case FN_QUIT: 00515 *data_size = 0; 00516 return buf_size; 00517 break; 00518 default: 00519 av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd); 00520 return -1; 00521 break; 00522 } 00523 } 00524 frame_done: 00525 *data_size = (int8_t *)samples - (int8_t *)data; 00526 00527 // s->last_blocksize = s->blocksize; 00528 s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8); 00529 i= (get_bits_count(&s->gb))/8; 00530 if (i > buf_size) { 00531 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size); 00532 s->bitstream_size=0; 00533 s->bitstream_index=0; 00534 return -1; 00535 } 00536 if (s->bitstream_size) { 00537 s->bitstream_index += i; 00538 s->bitstream_size -= i; 00539 return input_buf_size; 00540 } else 00541 return i; 00542 } 00543 00544 static av_cold int shorten_decode_close(AVCodecContext *avctx) 00545 { 00546 ShortenContext *s = avctx->priv_data; 00547 int i; 00548 00549 for (i = 0; i < s->channels; i++) { 00550 s->decoded[i] = NULL; 00551 av_freep(&s->decoded_base[i]); 00552 av_freep(&s->offset[i]); 00553 } 00554 av_freep(&s->bitstream); 00555 av_freep(&s->coeffs); 00556 return 0; 00557 } 00558 00559 static void shorten_flush(AVCodecContext *avctx){ 00560 ShortenContext *s = avctx->priv_data; 00561 00562 s->bitstream_size= 00563 s->bitstream_index= 0; 00564 } 00565 00566 AVCodec shorten_decoder = { 00567 "shorten", 00568 AVMEDIA_TYPE_AUDIO, 00569 CODEC_ID_SHORTEN, 00570 sizeof(ShortenContext), 00571 shorten_decode_init, 00572 NULL, 00573 shorten_decode_close, 00574 shorten_decode_frame, 00575 .flush= shorten_flush, 00576 .long_name= NULL_IF_CONFIG_SMALL("Shorten"), 00577 };