shorten.c

Go to the documentation of this file.
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 
00022 /**
00023  * @file shorten.c
00024  * Shorten decoder
00025  * @author Jeff Muizelaar
00026  *
00027  */
00028 
00029 #define DEBUG
00030 #include <limits.h>
00031 #include "avcodec.h"
00032 #include "bitstream.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 *offset[MAX_CHANNELS];
00086     uint8_t *bitstream;
00087     int bitstream_size;
00088     int bitstream_index;
00089     unsigned int allocated_bitstream_size;
00090     int header_size;
00091     uint8_t header[OUT_BUFFER_SIZE];
00092     int version;
00093     int cur_chan;
00094     int bitshift;
00095     int nmean;
00096     int internal_ftype;
00097     int nwrap;
00098     int blocksize;
00099     int bitindex;
00100     int32_t lpcqoffset;
00101 } ShortenContext;
00102 
00103 static av_cold int shorten_decode_init(AVCodecContext * avctx)
00104 {
00105     ShortenContext *s = avctx->priv_data;
00106     s->avctx = avctx;
00107 
00108     return 0;
00109 }
00110 
00111 static int allocate_buffers(ShortenContext *s)
00112 {
00113     int i, chan;
00114     for (chan=0; chan<s->channels; chan++) {
00115         if(FFMAX(1, s->nmean) >= UINT_MAX/sizeof(int32_t)){
00116             av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
00117             return -1;
00118         }
00119         if(s->blocksize + s->nwrap >= UINT_MAX/sizeof(int32_t) || s->blocksize + s->nwrap <= (unsigned)s->nwrap){
00120             av_log(s->avctx, AV_LOG_ERROR, "s->blocksize + s->nwrap too large\n");
00121             return -1;
00122         }
00123 
00124         s->offset[chan] = av_realloc(s->offset[chan], sizeof(int32_t)*FFMAX(1, s->nmean));
00125 
00126         s->decoded[chan] = av_realloc(s->decoded[chan], sizeof(int32_t)*(s->blocksize + s->nwrap));
00127         for (i=0; i<s->nwrap; i++)
00128             s->decoded[chan][i] = 0;
00129         s->decoded[chan] += s->nwrap;
00130     }
00131     return 0;
00132 }
00133 
00134 
00135 static inline unsigned int get_uint(ShortenContext *s, int k)
00136 {
00137     if (s->version != 0)
00138         k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
00139     return get_ur_golomb_shorten(&s->gb, k);
00140 }
00141 
00142 
00143 static void fix_bitshift(ShortenContext *s, int32_t *buffer)
00144 {
00145     int i;
00146 
00147     if (s->bitshift != 0)
00148         for (i = 0; i < s->blocksize; i++)
00149             buffer[s->nwrap + i] <<= s->bitshift;
00150 }
00151 
00152 
00153 static void init_offset(ShortenContext *s)
00154 {
00155     int32_t mean = 0;
00156     int  chan, i;
00157     int nblock = FFMAX(1, s->nmean);
00158     /* initialise offset */
00159     switch (s->internal_ftype)
00160     {
00161         case TYPE_S16HL:
00162         case TYPE_S16LH:
00163             mean = 0;
00164             break;
00165         default:
00166             av_log(s->avctx, AV_LOG_ERROR, "unknown audio type");
00167             abort();
00168     }
00169 
00170     for (chan = 0; chan < s->channels; chan++)
00171         for (i = 0; i < nblock; i++)
00172             s->offset[chan][i] = mean;
00173 }
00174 
00175 static inline int get_le32(GetBitContext *gb)
00176 {
00177     return bswap_32(get_bits_long(gb, 32));
00178 }
00179 
00180 static inline short get_le16(GetBitContext *gb)
00181 {
00182     return bswap_16(get_bits_long(gb, 16));
00183 }
00184 
00185 static int decode_wave_header(AVCodecContext *avctx, uint8_t *header, int header_size)
00186 {
00187     GetBitContext hb;
00188     int len;
00189     int chunk_size;
00190     short wave_format;
00191 
00192     init_get_bits(&hb, header, header_size*8);
00193     if (get_le32(&hb) != MKTAG('R','I','F','F')) {
00194         av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
00195         return -1;
00196     }
00197 
00198     chunk_size = get_le32(&hb);
00199 
00200     if (get_le32(&hb) != MKTAG('W','A','V','E')) {
00201         av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
00202         return -1;
00203     }
00204 
00205     while (get_le32(&hb) != MKTAG('f','m','t',' ')) {
00206         len = get_le32(&hb);
00207         skip_bits(&hb, 8*len);
00208     }
00209     len = get_le32(&hb);
00210 
00211     if (len < 16) {
00212         av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
00213         return -1;
00214     }
00215 
00216     wave_format = get_le16(&hb);
00217 
00218     switch (wave_format) {
00219         case WAVE_FORMAT_PCM:
00220             break;
00221         default:
00222             av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
00223             return -1;
00224     }
00225 
00226     avctx->channels = get_le16(&hb);
00227     avctx->sample_rate = get_le32(&hb);
00228     avctx->bit_rate = get_le32(&hb) * 8;
00229     avctx->block_align = get_le16(&hb);
00230     avctx->bits_per_sample = get_le16(&hb);
00231 
00232     if (avctx->bits_per_sample != 16) {
00233         av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
00234         return -1;
00235     }
00236 
00237     len -= 16;
00238     if (len > 0)
00239         av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
00240 
00241     return 0;
00242 }
00243 
00244 static int16_t * interleave_buffer(int16_t *samples, int nchan, int blocksize, int32_t **buffer) {
00245     int i, chan;
00246     for (i=0; i<blocksize; i++)
00247         for (chan=0; chan < nchan; chan++)
00248             *samples++ = FFMIN(buffer[chan][i], 32768);
00249     return samples;
00250 }
00251 
00252 static void decode_subframe_lpc(ShortenContext *s, int channel, int residual_size, int pred_order)
00253 {
00254     int sum, i, j;
00255     int coeffs[pred_order];
00256 
00257     for (i=0; i<pred_order; i++)
00258         coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
00259 
00260     for (i=0; i < s->blocksize; i++) {
00261         sum = s->lpcqoffset;
00262         for (j=0; j<pred_order; j++)
00263             sum += coeffs[j] * s->decoded[channel][i-j-1];
00264         s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + (sum >> LPCQUANT);
00265     }
00266 }
00267 
00268 
00269 static int shorten_decode_frame(AVCodecContext *avctx,
00270         void *data, int *data_size,
00271         const uint8_t *buf, int buf_size)
00272 {
00273     ShortenContext *s = avctx->priv_data;
00274     int i, input_buf_size = 0;
00275     int16_t *samples = data;
00276     if(s->max_framesize == 0){
00277         s->max_framesize= 1024; // should hopefully be enough for the first header
00278         s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
00279     }
00280 
00281     if(1 && s->max_framesize){//FIXME truncated
00282         buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
00283         input_buf_size= buf_size;
00284 
00285         if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
00286             //                printf("memmove\n");
00287             memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
00288             s->bitstream_index=0;
00289         }
00290         memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
00291         buf= &s->bitstream[s->bitstream_index];
00292         buf_size += s->bitstream_size;
00293         s->bitstream_size= buf_size;
00294 
00295         if(buf_size < s->max_framesize){
00296             //dprintf(avctx, "wanna more data ... %d\n", buf_size);
00297             *data_size = 0;
00298             return input_buf_size;
00299         }
00300     }
00301     init_get_bits(&s->gb, buf, buf_size*8);
00302     skip_bits(&s->gb, s->bitindex);
00303     if (!s->blocksize)
00304     {
00305         int maxnlpc = 0;
00306         /* shorten signature */
00307         if (get_bits_long(&s->gb, 32) != bswap_32(ff_get_fourcc("ajkg"))) {
00308             av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
00309             return -1;
00310         }
00311 
00312         s->lpcqoffset = 0;
00313         s->blocksize = DEFAULT_BLOCK_SIZE;
00314         s->channels = 1;
00315         s->nmean = -1;
00316         s->version = get_bits(&s->gb, 8);
00317         s->internal_ftype = get_uint(s, TYPESIZE);
00318 
00319         s->channels = get_uint(s, CHANSIZE);
00320         if (s->channels > MAX_CHANNELS) {
00321             av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
00322             return -1;
00323         }
00324 
00325         /* get blocksize if version > 0 */
00326         if (s->version > 0) {
00327             int skip_bytes;
00328             s->blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
00329             maxnlpc = get_uint(s, LPCQSIZE);
00330             s->nmean = get_uint(s, 0);
00331 
00332             skip_bytes = get_uint(s, NSKIPSIZE);
00333             for (i=0; i<skip_bytes; i++) {
00334                 skip_bits(&s->gb, 8);
00335             }
00336         }
00337         s->nwrap = FFMAX(NWRAP, maxnlpc);
00338 
00339         if (allocate_buffers(s))
00340             return -1;
00341 
00342         init_offset(s);
00343 
00344         if (s->version > 1)
00345             s->lpcqoffset = V2LPCQOFFSET;
00346 
00347         if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
00348             av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at beginning of stream\n");
00349             return -1;
00350         }
00351 
00352         s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
00353         if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
00354             av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
00355             return -1;
00356         }
00357 
00358         for (i=0; i<s->header_size; i++)
00359             s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
00360 
00361         if (decode_wave_header(avctx, s->header, s->header_size) < 0)
00362             return -1;
00363 
00364         s->cur_chan = 0;
00365         s->bitshift = 0;
00366     }
00367     else
00368     {
00369         int cmd;
00370         int len;
00371         cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
00372         switch (cmd) {
00373             case FN_ZERO:
00374             case FN_DIFF0:
00375             case FN_DIFF1:
00376             case FN_DIFF2:
00377             case FN_DIFF3:
00378             case FN_QLPC:
00379                 {
00380                     int residual_size = 0;
00381                     int channel = s->cur_chan;
00382                     int32_t coffset;
00383                     if (cmd != FN_ZERO) {
00384                         residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
00385                         /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
00386                         if (s->version == 0)
00387                             residual_size--;
00388                     }
00389 
00390                     if (s->nmean == 0)
00391                         coffset = s->offset[channel][0];
00392                     else {
00393                         int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
00394                         for (i=0; i<s->nmean; i++)
00395                             sum += s->offset[channel][i];
00396                         coffset = sum / s->nmean;
00397                         if (s->version >= 2)
00398                             coffset >>= FFMIN(1, s->bitshift);
00399                     }
00400                     switch (cmd) {
00401                         case FN_ZERO:
00402                             for (i=0; i<s->blocksize; i++)
00403                                 s->decoded[channel][i] = 0;
00404                             break;
00405                         case FN_DIFF0:
00406                             for (i=0; i<s->blocksize; i++)
00407                                 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + coffset;
00408                             break;
00409                         case FN_DIFF1:
00410                             for (i=0; i<s->blocksize; i++)
00411                                 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + s->decoded[channel][i - 1];
00412                             break;
00413                         case FN_DIFF2:
00414                             for (i=0; i<s->blocksize; i++)
00415                                 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 2*s->decoded[channel][i-1]
00416                                                                                                       -   s->decoded[channel][i-2];
00417                             break;
00418                         case FN_DIFF3:
00419                             for (i=0; i<s->blocksize; i++)
00420                                 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 3*s->decoded[channel][i-1]
00421                                                                                                       - 3*s->decoded[channel][i-2]
00422                                                                                                       +   s->decoded[channel][i-3];
00423                             break;
00424                         case FN_QLPC:
00425                             {
00426                                 int pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
00427                                 for (i=0; i<pred_order; i++)
00428                                     s->decoded[channel][i - pred_order] -= coffset;
00429                                 decode_subframe_lpc(s, channel, residual_size, pred_order);
00430                                 if (coffset != 0)
00431                                     for (i=0; i < s->blocksize; i++)
00432                                         s->decoded[channel][i] += coffset;
00433                             }
00434                     }
00435                     if (s->nmean > 0) {
00436                         int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
00437                         for (i=0; i<s->blocksize; i++)
00438                             sum += s->decoded[channel][i];
00439 
00440                         for (i=1; i<s->nmean; i++)
00441                             s->offset[channel][i-1] = s->offset[channel][i];
00442 
00443                         if (s->version < 2)
00444                             s->offset[channel][s->nmean - 1] = sum / s->blocksize;
00445                         else
00446                             s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
00447                     }
00448                     for (i=-s->nwrap; i<0; i++)
00449                         s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
00450 
00451                     fix_bitshift(s, s->decoded[channel]);
00452 
00453                     s->cur_chan++;
00454                     if (s->cur_chan == s->channels) {
00455                         samples = interleave_buffer(samples, s->channels, s->blocksize, s->decoded);
00456                         s->cur_chan = 0;
00457                         goto frame_done;
00458                     }
00459                     break;
00460                 }
00461                 break;
00462             case FN_VERBATIM:
00463                 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
00464                 while (len--) {
00465                     get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
00466                 }
00467                 break;
00468             case FN_BITSHIFT:
00469                 s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
00470                 break;
00471             case FN_BLOCKSIZE:
00472                 s->blocksize = get_uint(s, av_log2(s->blocksize));
00473                 break;
00474             case FN_QUIT:
00475                 *data_size = 0;
00476                 return buf_size;
00477                 break;
00478             default:
00479                 av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
00480                 return -1;
00481                 break;
00482         }
00483     }
00484 frame_done:
00485     *data_size = (int8_t *)samples - (int8_t *)data;
00486 
00487     //    s->last_blocksize = s->blocksize;
00488     s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
00489     i= (get_bits_count(&s->gb))/8;
00490     if (i > buf_size) {
00491         av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
00492         s->bitstream_size=0;
00493         s->bitstream_index=0;
00494         return -1;
00495     }
00496     if (s->bitstream_size) {
00497         s->bitstream_index += i;
00498         s->bitstream_size  -= i;
00499         return input_buf_size;
00500     } else
00501         return i;
00502 }
00503 
00504 static av_cold int shorten_decode_close(AVCodecContext *avctx)
00505 {
00506     ShortenContext *s = avctx->priv_data;
00507     int i;
00508 
00509     for (i = 0; i < s->channels; i++) {
00510         s->decoded[i] -= s->nwrap;
00511         av_freep(&s->decoded[i]);
00512         av_freep(&s->offset[i]);
00513     }
00514     av_freep(&s->bitstream);
00515     return 0;
00516 }
00517 
00518 static void shorten_flush(AVCodecContext *avctx){
00519     ShortenContext *s = avctx->priv_data;
00520 
00521     s->bitstream_size=
00522         s->bitstream_index= 0;
00523 }
00524 
00525 AVCodec shorten_decoder = {
00526     "shorten",
00527     CODEC_TYPE_AUDIO,
00528     CODEC_ID_SHORTEN,
00529     sizeof(ShortenContext),
00530     shorten_decode_init,
00531     NULL,
00532     shorten_decode_close,
00533     shorten_decode_frame,
00534     .flush= shorten_flush,
00535     .long_name= NULL_IF_CONFIG_SMALL("Shorten"),
00536 };

Generated on Thu Nov 20 07:44:52 2008 for libextractor by  doxygen 1.5.1