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