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

Generated on Fri Sep 16 2011 17:17:43 for FFmpeg by  doxygen 1.7.1