]> git.sesse.net Git - ffmpeg/blob - libavcodec/shorten.c
Merge commit 'bfe1cd80ebeab58cbc1c91ac766a96fce8e4ec1e'
[ffmpeg] / libavcodec / shorten.c
1 /*
2  * Shorten decoder
3  * Copyright (c) 2005 Jeff Muizelaar
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Shorten decoder
25  * @author Jeff Muizelaar
26  *
27  */
28
29 #include <limits.h>
30 #include "avcodec.h"
31 #include "bytestream.h"
32 #include "get_bits.h"
33 #include "golomb.h"
34 #include "internal.h"
35
36 #define MAX_CHANNELS 8
37 #define MAX_BLOCKSIZE 65535
38
39 #define OUT_BUFFER_SIZE 16384
40
41 #define ULONGSIZE 2
42
43 #define WAVE_FORMAT_PCM 0x0001
44
45 #define DEFAULT_BLOCK_SIZE 256
46
47 #define TYPESIZE 4
48 #define CHANSIZE 0
49 #define LPCQSIZE 2
50 #define ENERGYSIZE 3
51 #define BITSHIFTSIZE 2
52
53 #define TYPE_S8    1
54 #define TYPE_U8    2
55 #define TYPE_S16HL 3
56 #define TYPE_U16HL 4
57 #define TYPE_S16LH 5
58 #define TYPE_U16LH 6
59
60 #define NWRAP 3
61 #define NSKIPSIZE 1
62
63 #define LPCQUANT 5
64 #define V2LPCQOFFSET (1 << LPCQUANT)
65
66 #define FNSIZE 2
67 #define FN_DIFF0        0
68 #define FN_DIFF1        1
69 #define FN_DIFF2        2
70 #define FN_DIFF3        3
71 #define FN_QUIT         4
72 #define FN_BLOCKSIZE    5
73 #define FN_BITSHIFT     6
74 #define FN_QLPC         7
75 #define FN_ZERO         8
76 #define FN_VERBATIM     9
77
78 /** indicates if the FN_* command is audio or non-audio */
79 static const uint8_t is_audio_command[10] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0 };
80
81 #define VERBATIM_CKSIZE_SIZE 5
82 #define VERBATIM_BYTE_SIZE 8
83 #define CANONICAL_HEADER_SIZE 44
84
85 typedef struct ShortenContext {
86     AVCodecContext *avctx;
87     GetBitContext gb;
88
89     int min_framesize, max_framesize;
90     unsigned channels;
91
92     int32_t *decoded[MAX_CHANNELS];
93     int32_t *decoded_base[MAX_CHANNELS];
94     int32_t *offset[MAX_CHANNELS];
95     int *coeffs;
96     uint8_t *bitstream;
97     int bitstream_size;
98     int bitstream_index;
99     unsigned int allocated_bitstream_size;
100     int header_size;
101     uint8_t header[OUT_BUFFER_SIZE];
102     int version;
103     int cur_chan;
104     int bitshift;
105     int nmean;
106     int internal_ftype;
107     int nwrap;
108     int blocksize;
109     int bitindex;
110     int32_t lpcqoffset;
111     int got_header;
112     int got_quit_command;
113 } ShortenContext;
114
115 static av_cold int shorten_decode_init(AVCodecContext *avctx)
116 {
117     ShortenContext *s = avctx->priv_data;
118     s->avctx          = avctx;
119
120     return 0;
121 }
122
123 static int allocate_buffers(ShortenContext *s)
124 {
125     int i, chan, err;
126
127     for (chan = 0; chan < s->channels; chan++) {
128         if (FFMAX(1, s->nmean) >= UINT_MAX / sizeof(int32_t)) {
129             av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
130             return AVERROR_INVALIDDATA;
131         }
132         if (s->blocksize + (uint64_t)s->nwrap >= UINT_MAX / sizeof(int32_t)) {
133             av_log(s->avctx, AV_LOG_ERROR,
134                    "s->blocksize + s->nwrap too large\n");
135             return AVERROR_INVALIDDATA;
136         }
137
138         if ((err = av_reallocp_array(&s->offset[chan],
139                                sizeof(int32_t),
140                                FFMAX(1, s->nmean))) < 0)
141             return err;
142
143         if ((err = av_reallocp_array(&s->decoded_base[chan], (s->blocksize + s->nwrap),
144                                sizeof(s->decoded_base[0][0]))) < 0)
145             return err;
146         for (i = 0; i < s->nwrap; i++)
147             s->decoded_base[chan][i] = 0;
148         s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
149     }
150
151     if ((err = av_reallocp_array(&s->coeffs, s->nwrap, sizeof(*s->coeffs))) < 0)
152         return err;
153
154     return 0;
155 }
156
157 static inline unsigned int get_uint(ShortenContext *s, int k)
158 {
159     if (s->version != 0)
160         k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
161     return get_ur_golomb_shorten(&s->gb, k);
162 }
163
164 static void fix_bitshift(ShortenContext *s, int32_t *buffer)
165 {
166     int i;
167
168     if (s->bitshift != 0)
169         for (i = 0; i < s->blocksize; i++)
170             buffer[i] <<= s->bitshift;
171 }
172
173 static int init_offset(ShortenContext *s)
174 {
175     int32_t mean = 0;
176     int chan, i;
177     int nblock = FFMAX(1, s->nmean);
178     /* initialise offset */
179     switch (s->internal_ftype) {
180     case TYPE_U8:
181         s->avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
182         mean = 0x80;
183         break;
184     case TYPE_S16HL:
185     case TYPE_S16LH:
186         s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
187         break;
188     default:
189         av_log(s->avctx, AV_LOG_ERROR, "unknown audio type\n");
190         return AVERROR_PATCHWELCOME;
191     }
192
193     for (chan = 0; chan < s->channels; chan++)
194         for (i = 0; i < nblock; i++)
195             s->offset[chan][i] = mean;
196     return 0;
197 }
198
199 static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header,
200                               int header_size)
201 {
202     int len, bps;
203     short wave_format;
204     GetByteContext gb;
205
206     bytestream2_init(&gb, header, header_size);
207
208     if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
209         av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
210         return AVERROR_INVALIDDATA;
211     }
212
213     bytestream2_skip(&gb, 4); /* chunk size */
214
215     if (bytestream2_get_le32(&gb) != MKTAG('W', 'A', 'V', 'E')) {
216         av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
217         return AVERROR_INVALIDDATA;
218     }
219
220     while (bytestream2_get_le32(&gb) != MKTAG('f', 'm', 't', ' ')) {
221         len = bytestream2_get_le32(&gb);
222         bytestream2_skip(&gb, len);
223         if (len < 0 || bytestream2_get_bytes_left(&gb) < 16) {
224             av_log(avctx, AV_LOG_ERROR, "no fmt chunk found\n");
225             return AVERROR_INVALIDDATA;
226         }
227     }
228     len = bytestream2_get_le32(&gb);
229
230     if (len < 16) {
231         av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
232         return AVERROR_INVALIDDATA;
233     }
234
235     wave_format = bytestream2_get_le16(&gb);
236
237     switch (wave_format) {
238     case WAVE_FORMAT_PCM:
239         break;
240     default:
241         av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
242         return AVERROR(ENOSYS);
243     }
244
245     bytestream2_skip(&gb, 2); // skip channels    (already got from shorten header)
246     avctx->sample_rate = bytestream2_get_le32(&gb);
247     bytestream2_skip(&gb, 4); // skip bit rate    (represents original uncompressed bit rate)
248     bytestream2_skip(&gb, 2); // skip block align (not needed)
249     bps = bytestream2_get_le16(&gb);
250     avctx->bits_per_coded_sample = bps;
251
252     if (bps != 16 && bps != 8) {
253         av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample: %d\n", bps);
254         return AVERROR(ENOSYS);
255     }
256
257     len -= 16;
258     if (len > 0)
259         av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
260
261     return 0;
262 }
263
264 static const int fixed_coeffs[][3] = {
265     { 0,  0,  0 },
266     { 1,  0,  0 },
267     { 2, -1,  0 },
268     { 3, -3,  1 }
269 };
270
271 static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
272                                int residual_size, int32_t coffset)
273 {
274     int pred_order, sum, qshift, init_sum, i, j;
275     const int *coeffs;
276
277     if (command == FN_QLPC) {
278         /* read/validate prediction order */
279         pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
280         if ((unsigned)pred_order > s->nwrap) {
281             av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
282                    pred_order);
283             return AVERROR(EINVAL);
284         }
285         /* read LPC coefficients */
286         for (i = 0; i < pred_order; i++)
287             s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
288         coeffs = s->coeffs;
289
290         qshift = LPCQUANT;
291     } else {
292         /* fixed LPC coeffs */
293         pred_order = command;
294         if (pred_order >= FF_ARRAY_ELEMS(fixed_coeffs)) {
295             av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
296                    pred_order);
297             return AVERROR_INVALIDDATA;
298         }
299         coeffs     = fixed_coeffs[pred_order];
300         qshift     = 0;
301     }
302
303     /* subtract offset from previous samples to use in prediction */
304     if (command == FN_QLPC && coffset)
305         for (i = -pred_order; i < 0; i++)
306             s->decoded[channel][i] -= coffset;
307
308     /* decode residual and do LPC prediction */
309     init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
310     for (i = 0; i < s->blocksize; i++) {
311         sum = init_sum;
312         for (j = 0; j < pred_order; j++)
313             sum += coeffs[j] * s->decoded[channel][i - j - 1];
314         s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) +
315                                  (sum >> qshift);
316     }
317
318     /* add offset to current samples */
319     if (command == FN_QLPC && coffset)
320         for (i = 0; i < s->blocksize; i++)
321             s->decoded[channel][i] += coffset;
322
323     return 0;
324 }
325
326 static int read_header(ShortenContext *s)
327 {
328     int i, ret;
329     int maxnlpc = 0;
330     /* shorten signature */
331     if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
332         av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
333         return AVERROR_INVALIDDATA;
334     }
335
336     s->lpcqoffset     = 0;
337     s->blocksize      = DEFAULT_BLOCK_SIZE;
338     s->nmean          = -1;
339     s->version        = get_bits(&s->gb, 8);
340     s->internal_ftype = get_uint(s, TYPESIZE);
341
342     s->channels = get_uint(s, CHANSIZE);
343     if (!s->channels) {
344         av_log(s->avctx, AV_LOG_ERROR, "No channels reported\n");
345         return AVERROR_INVALIDDATA;
346     }
347     if (s->channels > MAX_CHANNELS) {
348         av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
349         s->channels = 0;
350         return AVERROR_INVALIDDATA;
351     }
352     s->avctx->channels = s->channels;
353
354     /* get blocksize if version > 0 */
355     if (s->version > 0) {
356         int skip_bytes;
357         unsigned blocksize;
358
359         blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
360         if (!blocksize || blocksize > MAX_BLOCKSIZE) {
361             av_log(s->avctx, AV_LOG_ERROR,
362                    "invalid or unsupported block size: %d\n",
363                    blocksize);
364             return AVERROR(EINVAL);
365         }
366         s->blocksize = blocksize;
367
368         maxnlpc  = get_uint(s, LPCQSIZE);
369         s->nmean = get_uint(s, 0);
370
371         skip_bytes = get_uint(s, NSKIPSIZE);
372         if ((unsigned)skip_bytes > get_bits_left(&s->gb)/8) {
373             av_log(s->avctx, AV_LOG_ERROR, "invalid skip_bytes: %d\n", skip_bytes);
374             return AVERROR_INVALIDDATA;
375         }
376
377         for (i = 0; i < skip_bytes; i++)
378             skip_bits(&s->gb, 8);
379     }
380     s->nwrap = FFMAX(NWRAP, maxnlpc);
381
382     if ((ret = allocate_buffers(s)) < 0)
383         return ret;
384
385     if ((ret = init_offset(s)) < 0)
386         return ret;
387
388     if (s->version > 1)
389         s->lpcqoffset = V2LPCQOFFSET;
390
391     if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
392         av_log(s->avctx, AV_LOG_ERROR,
393                "missing verbatim section at beginning of stream\n");
394         return AVERROR_INVALIDDATA;
395     }
396
397     s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
398     if (s->header_size >= OUT_BUFFER_SIZE ||
399         s->header_size < CANONICAL_HEADER_SIZE) {
400         av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n",
401                s->header_size);
402         return AVERROR_INVALIDDATA;
403     }
404
405     for (i = 0; i < s->header_size; i++)
406         s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
407
408     if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0)
409         return ret;
410
411     s->cur_chan = 0;
412     s->bitshift = 0;
413
414     s->got_header = 1;
415
416     return 0;
417 }
418
419 static int shorten_decode_frame(AVCodecContext *avctx, void *data,
420                                 int *got_frame_ptr, AVPacket *avpkt)
421 {
422     AVFrame *frame     = data;
423     const uint8_t *buf = avpkt->data;
424     int buf_size       = avpkt->size;
425     ShortenContext *s  = avctx->priv_data;
426     int i, input_buf_size = 0;
427     int ret;
428
429     /* allocate internal bitstream buffer */
430     if (s->max_framesize == 0) {
431         void *tmp_ptr;
432         s->max_framesize = 8192; // should hopefully be enough for the first header
433         tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
434                                   s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE);
435         if (!tmp_ptr) {
436             av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
437             return AVERROR(ENOMEM);
438         }
439         memset(tmp_ptr, 0, s->allocated_bitstream_size);
440         s->bitstream = tmp_ptr;
441     }
442
443     /* append current packet data to bitstream buffer */
444     if (1 && s->max_framesize) { //FIXME truncated
445         buf_size       = FFMIN(buf_size, s->max_framesize - s->bitstream_size);
446         input_buf_size = buf_size;
447
448         if (s->bitstream_index + s->bitstream_size + buf_size + AV_INPUT_BUFFER_PADDING_SIZE >
449             s->allocated_bitstream_size) {
450             memmove(s->bitstream, &s->bitstream[s->bitstream_index],
451                     s->bitstream_size);
452             s->bitstream_index = 0;
453         }
454         if (buf)
455             memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf,
456                    buf_size);
457         buf               = &s->bitstream[s->bitstream_index];
458         buf_size         += s->bitstream_size;
459         s->bitstream_size = buf_size;
460
461         /* do not decode until buffer has at least max_framesize bytes or
462          * the end of the file has been reached */
463         if (buf_size < s->max_framesize && avpkt->data) {
464             *got_frame_ptr = 0;
465             return input_buf_size;
466         }
467     }
468     /* init and position bitstream reader */
469     if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
470         return ret;
471     skip_bits(&s->gb, s->bitindex);
472
473     /* process header or next subblock */
474     if (!s->got_header) {
475         if ((ret = read_header(s)) < 0)
476             return ret;
477         *got_frame_ptr = 0;
478         goto finish_frame;
479     }
480
481     /* if quit command was read previously, don't decode anything */
482     if (s->got_quit_command) {
483         *got_frame_ptr = 0;
484         return avpkt->size;
485     }
486
487     s->cur_chan = 0;
488     while (s->cur_chan < s->channels) {
489         unsigned cmd;
490         int len;
491
492         if (get_bits_left(&s->gb) < 3 + FNSIZE) {
493             *got_frame_ptr = 0;
494             break;
495         }
496
497         cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
498
499         if (cmd > FN_VERBATIM) {
500             av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
501             *got_frame_ptr = 0;
502             break;
503         }
504
505         if (!is_audio_command[cmd]) {
506             /* process non-audio command */
507             switch (cmd) {
508             case FN_VERBATIM:
509                 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
510                 while (len--)
511                     get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
512                 break;
513             case FN_BITSHIFT: {
514                 unsigned bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
515                 if (bitshift > 31) {
516                     av_log(avctx, AV_LOG_ERROR, "bitshift %d is invalid\n",
517                            bitshift);
518                     return AVERROR_INVALIDDATA;
519                 }
520                 s->bitshift = bitshift;
521                 break;
522             }
523             case FN_BLOCKSIZE: {
524                 unsigned blocksize = get_uint(s, av_log2(s->blocksize));
525                 if (blocksize > s->blocksize) {
526                     av_log(avctx, AV_LOG_ERROR,
527                            "Increasing block size is not supported\n");
528                     return AVERROR_PATCHWELCOME;
529                 }
530                 if (!blocksize || blocksize > MAX_BLOCKSIZE) {
531                     av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
532                                                 "block size: %d\n", blocksize);
533                     return AVERROR(EINVAL);
534                 }
535                 s->blocksize = blocksize;
536                 break;
537             }
538             case FN_QUIT:
539                 s->got_quit_command = 1;
540                 break;
541             }
542             if (cmd == FN_BLOCKSIZE || cmd == FN_QUIT) {
543                 *got_frame_ptr = 0;
544                 break;
545             }
546         } else {
547             /* process audio command */
548             int residual_size = 0;
549             int channel = s->cur_chan;
550             int32_t coffset;
551
552             /* get Rice code for residual decoding */
553             if (cmd != FN_ZERO) {
554                 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
555                 /* This is a hack as version 0 differed in the definition
556                  * of get_sr_golomb_shorten(). */
557                 if (s->version == 0)
558                     residual_size--;
559             }
560
561             /* calculate sample offset using means from previous blocks */
562             if (s->nmean == 0)
563                 coffset = s->offset[channel][0];
564             else {
565                 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
566                 for (i = 0; i < s->nmean; i++)
567                     sum += s->offset[channel][i];
568                 coffset = sum / s->nmean;
569                 if (s->version >= 2)
570                     coffset = s->bitshift == 0 ? coffset : coffset >> s->bitshift - 1 >> 1;
571             }
572
573             /* decode samples for this channel */
574             if (cmd == FN_ZERO) {
575                 for (i = 0; i < s->blocksize; i++)
576                     s->decoded[channel][i] = 0;
577             } else {
578                 if ((ret = decode_subframe_lpc(s, cmd, channel,
579                                                residual_size, coffset)) < 0)
580                     return ret;
581             }
582
583             /* update means with info from the current block */
584             if (s->nmean > 0) {
585                 int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
586                 for (i = 0; i < s->blocksize; i++)
587                     sum += s->decoded[channel][i];
588
589                 for (i = 1; i < s->nmean; i++)
590                     s->offset[channel][i - 1] = s->offset[channel][i];
591
592                 if (s->version < 2)
593                     s->offset[channel][s->nmean - 1] = sum / s->blocksize;
594                 else
595                     s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
596             }
597
598             /* copy wrap samples for use with next block */
599             for (i = -s->nwrap; i < 0; i++)
600                 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
601
602             /* shift samples to add in unused zero bits which were removed
603              * during encoding */
604             fix_bitshift(s, s->decoded[channel]);
605
606             /* if this is the last channel in the block, output the samples */
607             s->cur_chan++;
608             if (s->cur_chan == s->channels) {
609                 uint8_t *samples_u8;
610                 int16_t *samples_s16;
611                 int chan;
612
613                 /* get output buffer */
614                 frame->nb_samples = s->blocksize;
615                 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
616                     return ret;
617
618                 for (chan = 0; chan < s->channels; chan++) {
619                     samples_u8  = ((uint8_t **)frame->extended_data)[chan];
620                     samples_s16 = ((int16_t **)frame->extended_data)[chan];
621                     for (i = 0; i < s->blocksize; i++) {
622                         switch (s->internal_ftype) {
623                         case TYPE_U8:
624                             *samples_u8++ = av_clip_uint8(s->decoded[chan][i]);
625                             break;
626                         case TYPE_S16HL:
627                         case TYPE_S16LH:
628                             *samples_s16++ = av_clip_int16(s->decoded[chan][i]);
629                             break;
630                         }
631                     }
632                 }
633
634                 *got_frame_ptr = 1;
635             }
636         }
637     }
638     if (s->cur_chan < s->channels)
639         *got_frame_ptr = 0;
640
641 finish_frame:
642     s->bitindex = get_bits_count(&s->gb) - 8 * (get_bits_count(&s->gb) / 8);
643     i           = get_bits_count(&s->gb) / 8;
644     if (i > buf_size) {
645         av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
646         s->bitstream_size  = 0;
647         s->bitstream_index = 0;
648         return AVERROR_INVALIDDATA;
649     }
650     if (s->bitstream_size) {
651         s->bitstream_index += i;
652         s->bitstream_size  -= i;
653         return input_buf_size;
654     } else
655         return i;
656 }
657
658 static av_cold int shorten_decode_close(AVCodecContext *avctx)
659 {
660     ShortenContext *s = avctx->priv_data;
661     int i;
662
663     for (i = 0; i < s->channels; i++) {
664         s->decoded[i] = NULL;
665         av_freep(&s->decoded_base[i]);
666         av_freep(&s->offset[i]);
667     }
668     av_freep(&s->bitstream);
669     av_freep(&s->coeffs);
670
671     return 0;
672 }
673
674 AVCodec ff_shorten_decoder = {
675     .name           = "shorten",
676     .long_name      = NULL_IF_CONFIG_SMALL("Shorten"),
677     .type           = AVMEDIA_TYPE_AUDIO,
678     .id             = AV_CODEC_ID_SHORTEN,
679     .priv_data_size = sizeof(ShortenContext),
680     .init           = shorten_decode_init,
681     .close          = shorten_decode_close,
682     .decode         = shorten_decode_frame,
683     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
684     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
685                                                       AV_SAMPLE_FMT_U8P,
686                                                       AV_SAMPLE_FMT_NONE },
687 };