• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

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

Generated on Sat Feb 16 2013 09:23:13 for ffmpeg by  doxygen 1.7.1