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 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 "internal.h"
00032 #include "bytestream.h"
00033 #include "get_bits.h"
00034 #include "golomb.h"
00035 
00036 #define MAX_CHANNELS 8
00037 #define MAX_BLOCKSIZE 65535
00038 
00039 #define OUT_BUFFER_SIZE 16384
00040 
00041 #define ULONGSIZE 2
00042 
00043 #define WAVE_FORMAT_PCM 0x0001
00044 
00045 #define DEFAULT_BLOCK_SIZE 256
00046 
00047 #define TYPESIZE 4
00048 #define CHANSIZE 0
00049 #define LPCQSIZE 2
00050 #define ENERGYSIZE 3
00051 #define BITSHIFTSIZE 2
00052 
00053 #define TYPE_S16HL 3
00054 #define TYPE_S16LH 5
00055 
00056 #define NWRAP 3
00057 #define NSKIPSIZE 1
00058 
00059 #define LPCQUANT 5
00060 #define V2LPCQOFFSET (1 << LPCQUANT)
00061 
00062 #define FNSIZE 2
00063 #define FN_DIFF0        0
00064 #define FN_DIFF1        1
00065 #define FN_DIFF2        2
00066 #define FN_DIFF3        3
00067 #define FN_QUIT         4
00068 #define FN_BLOCKSIZE    5
00069 #define FN_BITSHIFT     6
00070 #define FN_QLPC         7
00071 #define FN_ZERO         8
00072 #define FN_VERBATIM     9
00073 
00075 static const uint8_t is_audio_command[10] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0 };
00076 
00077 #define VERBATIM_CKSIZE_SIZE 5
00078 #define VERBATIM_BYTE_SIZE 8
00079 #define CANONICAL_HEADER_SIZE 44
00080 
00081 typedef struct ShortenContext {
00082     AVCodecContext *avctx;
00083     AVFrame frame;
00084     GetBitContext gb;
00085 
00086     int min_framesize, max_framesize;
00087     unsigned channels;
00088 
00089     int32_t *decoded[MAX_CHANNELS];
00090     int32_t *decoded_base[MAX_CHANNELS];
00091     int32_t *offset[MAX_CHANNELS];
00092     int *coeffs;
00093     uint8_t *bitstream;
00094     int bitstream_size;
00095     int bitstream_index;
00096     unsigned int allocated_bitstream_size;
00097     int header_size;
00098     uint8_t header[OUT_BUFFER_SIZE];
00099     int version;
00100     int cur_chan;
00101     int bitshift;
00102     int nmean;
00103     int internal_ftype;
00104     int nwrap;
00105     int blocksize;
00106     int bitindex;
00107     int32_t lpcqoffset;
00108     int got_header;
00109     int got_quit_command;
00110 } ShortenContext;
00111 
00112 static av_cold int shorten_decode_init(AVCodecContext *avctx)
00113 {
00114     ShortenContext *s = avctx->priv_data;
00115     s->avctx          = avctx;
00116     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00117 
00118     avcodec_get_frame_defaults(&s->frame);
00119     avctx->coded_frame = &s->frame;
00120 
00121     return 0;
00122 }
00123 
00124 static int allocate_buffers(ShortenContext *s)
00125 {
00126     int i, chan;
00127     int *coeffs;
00128     void *tmp_ptr;
00129 
00130     for (chan = 0; chan < s->channels; chan++) {
00131         if (FFMAX(1, s->nmean) >= UINT_MAX / sizeof(int32_t)) {
00132             av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
00133             return AVERROR_INVALIDDATA;
00134         }
00135         if (s->blocksize + s->nwrap >= UINT_MAX / sizeof(int32_t) ||
00136             s->blocksize + s->nwrap <= (unsigned)s->nwrap) {
00137             av_log(s->avctx, AV_LOG_ERROR,
00138                    "s->blocksize + s->nwrap too large\n");
00139             return AVERROR_INVALIDDATA;
00140         }
00141 
00142         tmp_ptr =
00143             av_realloc(s->offset[chan], sizeof(int32_t) * FFMAX(1, s->nmean));
00144         if (!tmp_ptr)
00145             return AVERROR(ENOMEM);
00146         s->offset[chan] = tmp_ptr;
00147 
00148         tmp_ptr = av_realloc(s->decoded_base[chan], (s->blocksize + s->nwrap) *
00149                              sizeof(s->decoded_base[0][0]));
00150         if (!tmp_ptr)
00151             return AVERROR(ENOMEM);
00152         s->decoded_base[chan] = tmp_ptr;
00153         for (i = 0; i < s->nwrap; i++)
00154             s->decoded_base[chan][i] = 0;
00155         s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
00156     }
00157 
00158     coeffs = av_realloc(s->coeffs, s->nwrap * sizeof(*s->coeffs));
00159     if (!coeffs)
00160         return AVERROR(ENOMEM);
00161     s->coeffs = coeffs;
00162 
00163     return 0;
00164 }
00165 
00166 static inline unsigned int get_uint(ShortenContext *s, int k)
00167 {
00168     if (s->version != 0)
00169         k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
00170     return get_ur_golomb_shorten(&s->gb, k);
00171 }
00172 
00173 static void fix_bitshift(ShortenContext *s, int32_t *buffer)
00174 {
00175     int i;
00176 
00177     if (s->bitshift != 0)
00178         for (i = 0; i < s->blocksize; i++)
00179             buffer[i] <<= s->bitshift;
00180 }
00181 
00182 static int init_offset(ShortenContext *s)
00183 {
00184     int32_t mean = 0;
00185     int chan, i;
00186     int nblock = FFMAX(1, s->nmean);
00187     /* initialise offset */
00188     switch (s->internal_ftype) {
00189     case TYPE_S16HL:
00190     case TYPE_S16LH:
00191         mean = 0;
00192         break;
00193     default:
00194         av_log(s->avctx, AV_LOG_ERROR, "unknown audio type");
00195         return AVERROR_INVALIDDATA;
00196     }
00197 
00198     for (chan = 0; chan < s->channels; chan++)
00199         for (i = 0; i < nblock; i++)
00200             s->offset[chan][i] = mean;
00201     return 0;
00202 }
00203 
00204 static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header,
00205                               int header_size)
00206 {
00207     int len;
00208     short wave_format;
00209     GetByteContext gb;
00210 
00211     bytestream2_init(&gb, header, header_size);
00212 
00213     if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
00214         av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
00215         return AVERROR_INVALIDDATA;
00216     }
00217 
00218     bytestream2_skip(&gb, 4); /* chunk size */
00219 
00220     if (bytestream2_get_le32(&gb) != MKTAG('W', 'A', 'V', 'E')) {
00221         av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
00222         return AVERROR_INVALIDDATA;
00223     }
00224 
00225     while (bytestream2_get_le32(&gb) != MKTAG('f', 'm', 't', ' ')) {
00226         len = bytestream2_get_le32(&gb);
00227         bytestream2_skip(&gb, len);
00228         if (bytestream2_get_bytes_left(&gb) < 16) {
00229             av_log(avctx, AV_LOG_ERROR, "no fmt chunk found\n");
00230             return AVERROR_INVALIDDATA;
00231         }
00232     }
00233     len = bytestream2_get_le32(&gb);
00234 
00235     if (len < 16) {
00236         av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
00237         return AVERROR_INVALIDDATA;
00238     }
00239 
00240     wave_format = bytestream2_get_le16(&gb);
00241 
00242     switch (wave_format) {
00243     case WAVE_FORMAT_PCM:
00244         break;
00245     default:
00246         av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
00247         return AVERROR(ENOSYS);
00248     }
00249 
00250     bytestream2_skip(&gb, 2); // skip channels    (already got from shorten header)
00251     avctx->sample_rate = bytestream2_get_le32(&gb);
00252     bytestream2_skip(&gb, 4); // skip bit rate    (represents original uncompressed bit rate)
00253     bytestream2_skip(&gb, 2); // skip block align (not needed)
00254     avctx->bits_per_coded_sample = bytestream2_get_le16(&gb);
00255 
00256     if (avctx->bits_per_coded_sample != 16) {
00257         av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
00258         return AVERROR(ENOSYS);
00259     }
00260 
00261     len -= 16;
00262     if (len > 0)
00263         av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
00264 
00265     return 0;
00266 }
00267 
00268 static void interleave_buffer(int16_t *samples, int nchan, int blocksize,
00269                               int32_t **buffer)
00270 {
00271     int i, chan;
00272     for (i=0; i<blocksize; i++)
00273         for (chan=0; chan < nchan; chan++)
00274             *samples++ = av_clip_int16(buffer[chan][i]);
00275 }
00276 
00277 static const int fixed_coeffs[3][3] = {
00278     { 1,  0,  0 },
00279     { 2, -1,  0 },
00280     { 3, -3,  1 }
00281 };
00282 
00283 static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
00284                                int residual_size, int32_t coffset)
00285 {
00286     int pred_order, sum, qshift, init_sum, i, j;
00287     const int *coeffs;
00288 
00289     if (command == FN_QLPC) {
00290         /* read/validate prediction order */
00291         pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
00292         if (pred_order > s->nwrap) {
00293             av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
00294                    pred_order);
00295             return AVERROR(EINVAL);
00296         }
00297         /* read LPC coefficients */
00298         for (i = 0; i < pred_order; i++)
00299             s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
00300         coeffs = s->coeffs;
00301 
00302         qshift = LPCQUANT;
00303     } else {
00304         /* fixed LPC coeffs */
00305         pred_order = command;
00306         coeffs     = fixed_coeffs[pred_order - 1];
00307         qshift     = 0;
00308     }
00309 
00310     /* subtract offset from previous samples to use in prediction */
00311     if (command == FN_QLPC && coffset)
00312         for (i = -pred_order; i < 0; i++)
00313             s->decoded[channel][i] -= coffset;
00314 
00315     /* decode residual and do LPC prediction */
00316     init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
00317     for (i = 0; i < s->blocksize; i++) {
00318         sum = init_sum;
00319         for (j = 0; j < pred_order; j++)
00320             sum += coeffs[j] * s->decoded[channel][i - j - 1];
00321         s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) +
00322                                  (sum >> qshift);
00323     }
00324 
00325     /* add offset to current samples */
00326     if (command == FN_QLPC && coffset)
00327         for (i = 0; i < s->blocksize; i++)
00328             s->decoded[channel][i] += coffset;
00329 
00330     return 0;
00331 }
00332 
00333 static int read_header(ShortenContext *s)
00334 {
00335     int i, ret;
00336     int maxnlpc = 0;
00337     /* shorten signature */
00338     if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
00339         av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
00340         return AVERROR_INVALIDDATA;
00341     }
00342 
00343     s->lpcqoffset     = 0;
00344     s->blocksize      = DEFAULT_BLOCK_SIZE;
00345     s->nmean          = -1;
00346     s->version        = get_bits(&s->gb, 8);
00347     s->internal_ftype = get_uint(s, TYPESIZE);
00348 
00349     s->channels = get_uint(s, CHANSIZE);
00350     if (!s->channels) {
00351         av_log(s->avctx, AV_LOG_ERROR, "No channels reported\n");
00352         return AVERROR_INVALIDDATA;
00353     }
00354     if (s->channels > MAX_CHANNELS) {
00355         av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
00356         s->channels = 0;
00357         return AVERROR_INVALIDDATA;
00358     }
00359     s->avctx->channels = s->channels;
00360 
00361     /* get blocksize if version > 0 */
00362     if (s->version > 0) {
00363         int skip_bytes;
00364         unsigned blocksize;
00365 
00366         blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
00367         if (!blocksize || blocksize > MAX_BLOCKSIZE) {
00368             av_log(s->avctx, AV_LOG_ERROR,
00369                    "invalid or unsupported block size: %d\n",
00370                    blocksize);
00371             return AVERROR(EINVAL);
00372         }
00373         s->blocksize = blocksize;
00374 
00375         maxnlpc  = get_uint(s, LPCQSIZE);
00376         s->nmean = get_uint(s, 0);
00377 
00378         skip_bytes = get_uint(s, NSKIPSIZE);
00379         for (i = 0; i < skip_bytes; i++)
00380             skip_bits(&s->gb, 8);
00381     }
00382     s->nwrap = FFMAX(NWRAP, maxnlpc);
00383 
00384     if ((ret = allocate_buffers(s)) < 0)
00385         return ret;
00386 
00387     if ((ret = init_offset(s)) < 0)
00388         return ret;
00389 
00390     if (s->version > 1)
00391         s->lpcqoffset = V2LPCQOFFSET;
00392 
00393     if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
00394         av_log(s->avctx, AV_LOG_ERROR,
00395                "missing verbatim section at beginning of stream\n");
00396         return AVERROR_INVALIDDATA;
00397     }
00398 
00399     s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
00400     if (s->header_size >= OUT_BUFFER_SIZE ||
00401         s->header_size < CANONICAL_HEADER_SIZE) {
00402         av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n",
00403                s->header_size);
00404         return AVERROR_INVALIDDATA;
00405     }
00406 
00407     for (i = 0; i < s->header_size; i++)
00408         s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
00409 
00410     if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0)
00411         return ret;
00412 
00413     s->cur_chan = 0;
00414     s->bitshift = 0;
00415 
00416     s->got_header = 1;
00417 
00418     return 0;
00419 }
00420 
00421 static int shorten_decode_frame(AVCodecContext *avctx, void *data,
00422                                 int *got_frame_ptr, AVPacket *avpkt)
00423 {
00424     const uint8_t *buf = avpkt->data;
00425     int buf_size       = avpkt->size;
00426     ShortenContext *s  = avctx->priv_data;
00427     int i, input_buf_size = 0;
00428     int ret;
00429 
00430     /* allocate internal bitstream buffer */
00431     if (s->max_framesize == 0) {
00432         void *tmp_ptr;
00433         s->max_framesize = 1024; // should hopefully be enough for the first header
00434         tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
00435                                   s->max_framesize + FF_INPUT_BUFFER_PADDING_SIZE);
00436         if (!tmp_ptr) {
00437             av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
00438             return AVERROR(ENOMEM);
00439         }
00440         s->bitstream = tmp_ptr;
00441     }
00442 
00443     /* append current packet data to bitstream buffer */
00444     if (1 && s->max_framesize) { //FIXME truncated
00445         buf_size       = FFMIN(buf_size, s->max_framesize - s->bitstream_size);
00446         input_buf_size = buf_size;
00447 
00448         if (s->bitstream_index + s->bitstream_size + buf_size >
00449             s->allocated_bitstream_size) {
00450             memmove(s->bitstream, &s->bitstream[s->bitstream_index],
00451                     s->bitstream_size);
00452             s->bitstream_index = 0;
00453         }
00454         if (buf)
00455             memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf,
00456                    buf_size);
00457         buf               = &s->bitstream[s->bitstream_index];
00458         buf_size         += s->bitstream_size;
00459         s->bitstream_size = buf_size;
00460 
00461         /* do not decode until buffer has at least max_framesize bytes or
00462          * the end of the file has been reached */
00463         if (buf_size < s->max_framesize && avpkt->data) {
00464             *got_frame_ptr = 0;
00465             return input_buf_size;
00466         }
00467     }
00468     /* init and position bitstream reader */
00469     init_get_bits(&s->gb, buf, buf_size * 8);
00470     skip_bits(&s->gb, s->bitindex);
00471 
00472     /* process header or next subblock */
00473     if (!s->got_header) {
00474         if ((ret = read_header(s)) < 0)
00475             return ret;
00476         *got_frame_ptr = 0;
00477         goto finish_frame;
00478     }
00479 
00480     /* if quit command was read previously, don't decode anything */
00481     if (s->got_quit_command) {
00482         *got_frame_ptr = 0;
00483         return avpkt->size;
00484     }
00485 
00486     s->cur_chan = 0;
00487     while (s->cur_chan < s->channels) {
00488         int cmd;
00489         int len;
00490 
00491         if (get_bits_left(&s->gb) < 3 + FNSIZE) {
00492             *got_frame_ptr = 0;
00493             break;
00494         }
00495 
00496         cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
00497 
00498         if (cmd > FN_VERBATIM) {
00499             av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
00500             *got_frame_ptr = 0;
00501             break;
00502         }
00503 
00504         if (!is_audio_command[cmd]) {
00505             /* process non-audio command */
00506             switch (cmd) {
00507             case FN_VERBATIM:
00508                 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
00509                 while (len--)
00510                     get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
00511                 break;
00512             case FN_BITSHIFT:
00513                 s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
00514                 break;
00515             case FN_BLOCKSIZE: {
00516                 unsigned blocksize = get_uint(s, av_log2(s->blocksize));
00517                 if (blocksize > s->blocksize) {
00518                     av_log(avctx, AV_LOG_ERROR,
00519                            "Increasing block size is not supported\n");
00520                     return AVERROR_PATCHWELCOME;
00521                 }
00522                 if (!blocksize || blocksize > MAX_BLOCKSIZE) {
00523                     av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
00524                                                 "block size: %d\n", blocksize);
00525                     return AVERROR(EINVAL);
00526                 }
00527                 s->blocksize = blocksize;
00528                 break;
00529             }
00530             case FN_QUIT:
00531                 s->got_quit_command = 1;
00532                 break;
00533             }
00534             if (cmd == FN_BLOCKSIZE || cmd == FN_QUIT) {
00535                 *got_frame_ptr = 0;
00536                 break;
00537             }
00538         } else {
00539             /* process audio command */
00540             int residual_size = 0;
00541             int channel = s->cur_chan;
00542             int32_t coffset;
00543 
00544             /* get Rice code for residual decoding */
00545             if (cmd != FN_ZERO) {
00546                 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
00547                 /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
00548                 if (s->version == 0)
00549                     residual_size--;
00550             }
00551 
00552             /* calculate sample offset using means from previous blocks */
00553             if (s->nmean == 0)
00554                 coffset = s->offset[channel][0];
00555             else {
00556                 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
00557                 for (i = 0; i < s->nmean; i++)
00558                     sum += s->offset[channel][i];
00559                 coffset = sum / s->nmean;
00560                 if (s->version >= 2)
00561                     coffset >>= FFMIN(1, s->bitshift);
00562             }
00563 
00564             /* decode samples for this channel */
00565             if (cmd == FN_ZERO) {
00566                 for (i = 0; i < s->blocksize; i++)
00567                     s->decoded[channel][i] = 0;
00568             } else {
00569                 if ((ret = decode_subframe_lpc(s, cmd, channel,
00570                                                residual_size, coffset)) < 0)
00571                     return ret;
00572             }
00573 
00574             /* update means with info from the current block */
00575             if (s->nmean > 0) {
00576                 int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
00577                 for (i = 0; i < s->blocksize; i++)
00578                     sum += s->decoded[channel][i];
00579 
00580                 for (i = 1; i < s->nmean; i++)
00581                     s->offset[channel][i - 1] = s->offset[channel][i];
00582 
00583                 if (s->version < 2)
00584                     s->offset[channel][s->nmean - 1] = sum / s->blocksize;
00585                 else
00586                     s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
00587             }
00588 
00589             /* copy wrap samples for use with next block */
00590             for (i = -s->nwrap; i < 0; i++)
00591                 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
00592 
00593             /* shift samples to add in unused zero bits which were removed
00594              * during encoding */
00595             fix_bitshift(s, s->decoded[channel]);
00596 
00597             /* if this is the last channel in the block, output the samples */
00598             s->cur_chan++;
00599             if (s->cur_chan == s->channels) {
00600                 /* get output buffer */
00601                 s->frame.nb_samples = s->blocksize;
00602                 if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
00603                     av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00604                     return ret;
00605                 }
00606                 /* interleave output */
00607                 interleave_buffer((int16_t *)s->frame.data[0], s->channels,
00608                                   s->blocksize, s->decoded);
00609 
00610                 *got_frame_ptr   = 1;
00611                 *(AVFrame *)data = s->frame;
00612             }
00613         }
00614     }
00615     if (s->cur_chan < s->channels)
00616         *got_frame_ptr = 0;
00617 
00618 finish_frame:
00619     s->bitindex = get_bits_count(&s->gb) - 8 * (get_bits_count(&s->gb) / 8);
00620     i           = get_bits_count(&s->gb) / 8;
00621     if (i > buf_size) {
00622         av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
00623         s->bitstream_size  = 0;
00624         s->bitstream_index = 0;
00625         return AVERROR_INVALIDDATA;
00626     }
00627     if (s->bitstream_size) {
00628         s->bitstream_index += i;
00629         s->bitstream_size  -= i;
00630         return input_buf_size;
00631     } else
00632         return i;
00633 }
00634 
00635 static av_cold int shorten_decode_close(AVCodecContext *avctx)
00636 {
00637     ShortenContext *s = avctx->priv_data;
00638     int i;
00639 
00640     for (i = 0; i < s->channels; i++) {
00641         s->decoded[i] = NULL;
00642         av_freep(&s->decoded_base[i]);
00643         av_freep(&s->offset[i]);
00644     }
00645     av_freep(&s->bitstream);
00646     av_freep(&s->coeffs);
00647 
00648     return 0;
00649 }
00650 
00651 AVCodec ff_shorten_decoder = {
00652     .name           = "shorten",
00653     .type           = AVMEDIA_TYPE_AUDIO,
00654     .id             = CODEC_ID_SHORTEN,
00655     .priv_data_size = sizeof(ShortenContext),
00656     .init           = shorten_decode_init,
00657     .close          = shorten_decode_close,
00658     .decode         = shorten_decode_frame,
00659     .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_DR1,
00660     .long_name= NULL_IF_CONFIG_SMALL("Shorten"),
00661 };