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