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