]> git.sesse.net Git - ffmpeg/blob - libavcodec/shorten.c
Merge commit '53618054b64ce4dab459d23a7efebe9d5afc4855'
[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 "bswapdsp.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     int swap;
114     BswapDSPContext bdsp;
115 } ShortenContext;
116
117 static av_cold int shorten_decode_init(AVCodecContext *avctx)
118 {
119     ShortenContext *s = avctx->priv_data;
120     s->avctx          = avctx;
121
122     ff_bswapdsp_init(&s->bdsp);
123
124     return 0;
125 }
126
127 static int allocate_buffers(ShortenContext *s)
128 {
129     int i, chan, err;
130
131     for (chan = 0; chan < s->channels; chan++) {
132         if (FFMAX(1, s->nmean) >= UINT_MAX / sizeof(int32_t)) {
133             av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
134             return AVERROR_INVALIDDATA;
135         }
136         if (s->blocksize + (uint64_t)s->nwrap >= UINT_MAX / sizeof(int32_t)) {
137             av_log(s->avctx, AV_LOG_ERROR,
138                    "s->blocksize + s->nwrap too large\n");
139             return AVERROR_INVALIDDATA;
140         }
141
142         if ((err = av_reallocp_array(&s->offset[chan],
143                                sizeof(int32_t),
144                                FFMAX(1, s->nmean))) < 0)
145             return err;
146
147         if ((err = av_reallocp_array(&s->decoded_base[chan], (s->blocksize + s->nwrap),
148                                sizeof(s->decoded_base[0][0]))) < 0)
149             return err;
150         for (i = 0; i < s->nwrap; i++)
151             s->decoded_base[chan][i] = 0;
152         s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
153     }
154
155     if ((err = av_reallocp_array(&s->coeffs, s->nwrap, sizeof(*s->coeffs))) < 0)
156         return err;
157
158     return 0;
159 }
160
161 static 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 static void fix_bitshift(ShortenContext *s, int32_t *buffer)
169 {
170     int i;
171
172     if (s->bitshift == 32) {
173         for (i = 0; i < s->blocksize; i++)
174             buffer[i] = 0;
175     } else if (s->bitshift != 0) {
176         for (i = 0; i < s->blocksize; i++)
177             buffer[i] <<= s->bitshift;
178     }
179 }
180
181 static int init_offset(ShortenContext *s)
182 {
183     int32_t mean = 0;
184     int chan, i;
185     int nblock = FFMAX(1, s->nmean);
186     /* initialise offset */
187     switch (s->internal_ftype) {
188     case TYPE_U8:
189         s->avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
190         mean = 0x80;
191         break;
192     case TYPE_S16HL:
193     case TYPE_S16LH:
194         s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
195         break;
196     default:
197         av_log(s->avctx, AV_LOG_ERROR, "unknown audio type\n");
198         return AVERROR_PATCHWELCOME;
199     }
200
201     for (chan = 0; chan < s->channels; chan++)
202         for (i = 0; i < nblock; i++)
203             s->offset[chan][i] = mean;
204     return 0;
205 }
206
207 static int decode_aiff_header(AVCodecContext *avctx, const uint8_t *header,
208                               int header_size)
209 {
210     ShortenContext *s = avctx->priv_data;
211     int len, bps, exp;
212     GetByteContext gb;
213     uint64_t val;
214     uint32_t tag;
215
216     bytestream2_init(&gb, header, header_size);
217
218     if (bytestream2_get_le32(&gb) != MKTAG('F', 'O', 'R', 'M')) {
219         av_log(avctx, AV_LOG_ERROR, "missing FORM tag\n");
220         return AVERROR_INVALIDDATA;
221     }
222
223     bytestream2_skip(&gb, 4); /* chunk size */
224
225     tag = bytestream2_get_le32(&gb);
226     if (tag != MKTAG('A', 'I', 'F', 'F') &&
227         tag != MKTAG('A', 'I', 'F', 'C')) {
228         av_log(avctx, AV_LOG_ERROR, "missing AIFF tag\n");
229         return AVERROR_INVALIDDATA;
230     }
231
232     while (bytestream2_get_le32(&gb) != MKTAG('C', 'O', 'M', 'M')) {
233         len = bytestream2_get_be32(&gb);
234         bytestream2_skip(&gb, len + (len & 1));
235         if (len < 0 || bytestream2_get_bytes_left(&gb) < 18) {
236             av_log(avctx, AV_LOG_ERROR, "no COMM chunk found\n");
237             return AVERROR_INVALIDDATA;
238         }
239     }
240     len = bytestream2_get_be32(&gb);
241
242     if (len < 18) {
243         av_log(avctx, AV_LOG_ERROR, "COMM chunk was too short\n");
244         return AVERROR_INVALIDDATA;
245     }
246
247     bytestream2_skip(&gb, 6);
248     bps = bytestream2_get_be16(&gb);
249     avctx->bits_per_coded_sample = bps;
250
251     s->swap = tag == MKTAG('A', 'I', 'F', 'C');
252
253     if (bps != 16 && bps != 8) {
254         av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample: %d\n", bps);
255         return AVERROR(ENOSYS);
256     }
257
258     exp = bytestream2_get_be16(&gb) - 16383 - 63;
259     val = bytestream2_get_be64(&gb);
260     if (exp < -63 || exp > 63) {
261         av_log(avctx, AV_LOG_ERROR, "exp %d is out of range\n", exp);
262         return AVERROR_INVALIDDATA;
263     }
264     if (exp >= 0)
265         avctx->sample_rate = val << exp;
266     else
267         avctx->sample_rate = (val + (1ULL<<(-exp-1))) >> -exp;
268     len -= 18;
269     if (len > 0)
270         av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
271
272     return 0;
273 }
274
275 static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header,
276                               int header_size)
277 {
278     int len, bps;
279     short wave_format;
280     GetByteContext gb;
281
282     bytestream2_init(&gb, header, header_size);
283
284     if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
285         av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
286         return AVERROR_INVALIDDATA;
287     }
288
289     bytestream2_skip(&gb, 4); /* chunk size */
290
291     if (bytestream2_get_le32(&gb) != MKTAG('W', 'A', 'V', 'E')) {
292         av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
293         return AVERROR_INVALIDDATA;
294     }
295
296     while (bytestream2_get_le32(&gb) != MKTAG('f', 'm', 't', ' ')) {
297         len = bytestream2_get_le32(&gb);
298         bytestream2_skip(&gb, len);
299         if (len < 0 || bytestream2_get_bytes_left(&gb) < 16) {
300             av_log(avctx, AV_LOG_ERROR, "no fmt chunk found\n");
301             return AVERROR_INVALIDDATA;
302         }
303     }
304     len = bytestream2_get_le32(&gb);
305
306     if (len < 16) {
307         av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
308         return AVERROR_INVALIDDATA;
309     }
310
311     wave_format = bytestream2_get_le16(&gb);
312
313     switch (wave_format) {
314     case WAVE_FORMAT_PCM:
315         break;
316     default:
317         av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
318         return AVERROR(ENOSYS);
319     }
320
321     bytestream2_skip(&gb, 2); // skip channels    (already got from shorten header)
322     avctx->sample_rate = bytestream2_get_le32(&gb);
323     bytestream2_skip(&gb, 4); // skip bit rate    (represents original uncompressed bit rate)
324     bytestream2_skip(&gb, 2); // skip block align (not needed)
325     bps = bytestream2_get_le16(&gb);
326     avctx->bits_per_coded_sample = bps;
327
328     if (bps != 16 && bps != 8) {
329         av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample: %d\n", bps);
330         return AVERROR(ENOSYS);
331     }
332
333     len -= 16;
334     if (len > 0)
335         av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
336
337     return 0;
338 }
339
340 static const int fixed_coeffs[][3] = {
341     { 0,  0,  0 },
342     { 1,  0,  0 },
343     { 2, -1,  0 },
344     { 3, -3,  1 }
345 };
346
347 static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
348                                int residual_size, int32_t coffset)
349 {
350     int pred_order, sum, qshift, init_sum, i, j;
351     const int *coeffs;
352
353     if (command == FN_QLPC) {
354         /* read/validate prediction order */
355         pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
356         if ((unsigned)pred_order > s->nwrap) {
357             av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
358                    pred_order);
359             return AVERROR(EINVAL);
360         }
361         /* read LPC coefficients */
362         for (i = 0; i < pred_order; i++)
363             s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
364         coeffs = s->coeffs;
365
366         qshift = LPCQUANT;
367     } else {
368         /* fixed LPC coeffs */
369         pred_order = command;
370         if (pred_order >= FF_ARRAY_ELEMS(fixed_coeffs)) {
371             av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
372                    pred_order);
373             return AVERROR_INVALIDDATA;
374         }
375         coeffs     = fixed_coeffs[pred_order];
376         qshift     = 0;
377     }
378
379     /* subtract offset from previous samples to use in prediction */
380     if (command == FN_QLPC && coffset)
381         for (i = -pred_order; i < 0; i++)
382             s->decoded[channel][i] -= coffset;
383
384     /* decode residual and do LPC prediction */
385     init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
386     for (i = 0; i < s->blocksize; i++) {
387         sum = init_sum;
388         for (j = 0; j < pred_order; j++)
389             sum += coeffs[j] * s->decoded[channel][i - j - 1];
390         s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) +
391                                  (sum >> qshift);
392     }
393
394     /* add offset to current samples */
395     if (command == FN_QLPC && coffset)
396         for (i = 0; i < s->blocksize; i++)
397             s->decoded[channel][i] += coffset;
398
399     return 0;
400 }
401
402 static int read_header(ShortenContext *s)
403 {
404     int i, ret;
405     int maxnlpc = 0;
406     /* shorten signature */
407     if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
408         av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
409         return AVERROR_INVALIDDATA;
410     }
411
412     s->lpcqoffset     = 0;
413     s->blocksize      = DEFAULT_BLOCK_SIZE;
414     s->nmean          = -1;
415     s->version        = get_bits(&s->gb, 8);
416     s->internal_ftype = get_uint(s, TYPESIZE);
417
418     s->channels = get_uint(s, CHANSIZE);
419     if (!s->channels) {
420         av_log(s->avctx, AV_LOG_ERROR, "No channels reported\n");
421         return AVERROR_INVALIDDATA;
422     }
423     if (s->channels > MAX_CHANNELS) {
424         av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
425         s->channels = 0;
426         return AVERROR_INVALIDDATA;
427     }
428     s->avctx->channels = s->channels;
429
430     /* get blocksize if version > 0 */
431     if (s->version > 0) {
432         int skip_bytes;
433         unsigned blocksize;
434
435         blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
436         if (!blocksize || blocksize > MAX_BLOCKSIZE) {
437             av_log(s->avctx, AV_LOG_ERROR,
438                    "invalid or unsupported block size: %d\n",
439                    blocksize);
440             return AVERROR(EINVAL);
441         }
442         s->blocksize = blocksize;
443
444         maxnlpc  = get_uint(s, LPCQSIZE);
445         s->nmean = get_uint(s, 0);
446
447         skip_bytes = get_uint(s, NSKIPSIZE);
448         if ((unsigned)skip_bytes > get_bits_left(&s->gb)/8) {
449             av_log(s->avctx, AV_LOG_ERROR, "invalid skip_bytes: %d\n", skip_bytes);
450             return AVERROR_INVALIDDATA;
451         }
452
453         for (i = 0; i < skip_bytes; i++)
454             skip_bits(&s->gb, 8);
455     }
456     s->nwrap = FFMAX(NWRAP, maxnlpc);
457
458     if ((ret = allocate_buffers(s)) < 0)
459         return ret;
460
461     if ((ret = init_offset(s)) < 0)
462         return ret;
463
464     if (s->version > 1)
465         s->lpcqoffset = V2LPCQOFFSET;
466
467     if (s->avctx->extradata_size > 0)
468         goto end;
469
470     if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
471         av_log(s->avctx, AV_LOG_ERROR,
472                "missing verbatim section at beginning of stream\n");
473         return AVERROR_INVALIDDATA;
474     }
475
476     s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
477     if (s->header_size >= OUT_BUFFER_SIZE ||
478         s->header_size < CANONICAL_HEADER_SIZE) {
479         av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n",
480                s->header_size);
481         return AVERROR_INVALIDDATA;
482     }
483
484     for (i = 0; i < s->header_size; i++)
485         s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
486
487     if (AV_RL32(s->header) == MKTAG('R','I','F','F')) {
488         if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0)
489             return ret;
490     } else if (AV_RL32(s->header) == MKTAG('F','O','R','M')) {
491         if ((ret = decode_aiff_header(s->avctx, s->header, s->header_size)) < 0)
492             return ret;
493     } else {
494         avpriv_report_missing_feature(s->avctx, "unsupported bit packing %"
495                                       PRIX32, AV_RL32(s->header));
496         return AVERROR_PATCHWELCOME;
497     }
498
499 end:
500     s->cur_chan = 0;
501     s->bitshift = 0;
502
503     s->got_header = 1;
504
505     return 0;
506 }
507
508 static int shorten_decode_frame(AVCodecContext *avctx, void *data,
509                                 int *got_frame_ptr, AVPacket *avpkt)
510 {
511     AVFrame *frame     = data;
512     const uint8_t *buf = avpkt->data;
513     int buf_size       = avpkt->size;
514     ShortenContext *s  = avctx->priv_data;
515     int i, input_buf_size = 0;
516     int ret;
517
518     /* allocate internal bitstream buffer */
519     if (s->max_framesize == 0) {
520         void *tmp_ptr;
521         s->max_framesize = 8192; // should hopefully be enough for the first header
522         tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
523                                   s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE);
524         if (!tmp_ptr) {
525             s->max_framesize = 0;
526             av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
527             return AVERROR(ENOMEM);
528         }
529         memset(tmp_ptr, 0, s->allocated_bitstream_size);
530         s->bitstream = tmp_ptr;
531     }
532
533     /* append current packet data to bitstream buffer */
534     buf_size       = FFMIN(buf_size, s->max_framesize - s->bitstream_size);
535     input_buf_size = buf_size;
536
537     if (s->bitstream_index + s->bitstream_size + buf_size + AV_INPUT_BUFFER_PADDING_SIZE >
538         s->allocated_bitstream_size) {
539         memmove(s->bitstream, &s->bitstream[s->bitstream_index],
540                 s->bitstream_size);
541         s->bitstream_index = 0;
542     }
543     if (buf)
544         memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf,
545                buf_size);
546     buf               = &s->bitstream[s->bitstream_index];
547     buf_size         += s->bitstream_size;
548     s->bitstream_size = buf_size;
549
550     /* do not decode until buffer has at least max_framesize bytes or
551      * the end of the file has been reached */
552     if (buf_size < s->max_framesize && avpkt->data) {
553         *got_frame_ptr = 0;
554         return input_buf_size;
555     }
556     /* init and position bitstream reader */
557     if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
558         return ret;
559     skip_bits(&s->gb, s->bitindex);
560
561     /* process header or next subblock */
562     if (!s->got_header) {
563
564         if ((ret = read_header(s)) < 0)
565             return ret;
566
567         if (avpkt->size) {
568             int max_framesize;
569             void *tmp_ptr;
570
571             max_framesize = FFMAX(s->max_framesize, s->blocksize * s->channels * 8);
572             tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
573                                       max_framesize + AV_INPUT_BUFFER_PADDING_SIZE);
574             if (!tmp_ptr) {
575                 av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
576                 return AVERROR(ENOMEM);
577             }
578             s->bitstream = tmp_ptr;
579             s->max_framesize = max_framesize;
580             *got_frame_ptr = 0;
581             goto finish_frame;
582         }
583     }
584
585     /* if quit command was read previously, don't decode anything */
586     if (s->got_quit_command) {
587         *got_frame_ptr = 0;
588         return avpkt->size;
589     }
590
591     s->cur_chan = 0;
592     while (s->cur_chan < s->channels) {
593         unsigned cmd;
594         int len;
595
596         if (get_bits_left(&s->gb) < 3 + FNSIZE) {
597             *got_frame_ptr = 0;
598             break;
599         }
600
601         cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
602
603         if (cmd > FN_VERBATIM) {
604             av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
605             *got_frame_ptr = 0;
606             break;
607         }
608
609         if (!is_audio_command[cmd]) {
610             /* process non-audio command */
611             switch (cmd) {
612             case FN_VERBATIM:
613                 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
614                 while (len--)
615                     get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
616                 break;
617             case FN_BITSHIFT: {
618                 unsigned bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
619                 if (bitshift > 32) {
620                     av_log(avctx, AV_LOG_ERROR, "bitshift %d is invalid\n",
621                            bitshift);
622                     return AVERROR_INVALIDDATA;
623                 }
624                 s->bitshift = bitshift;
625                 break;
626             }
627             case FN_BLOCKSIZE: {
628                 unsigned blocksize = get_uint(s, av_log2(s->blocksize));
629                 if (blocksize > s->blocksize) {
630                     avpriv_report_missing_feature(avctx,
631                                                   "Increasing block size");
632                     return AVERROR_PATCHWELCOME;
633                 }
634                 if (!blocksize || blocksize > MAX_BLOCKSIZE) {
635                     av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
636                                                 "block size: %d\n", blocksize);
637                     return AVERROR(EINVAL);
638                 }
639                 s->blocksize = blocksize;
640                 break;
641             }
642             case FN_QUIT:
643                 s->got_quit_command = 1;
644                 break;
645             }
646             if (cmd == FN_QUIT)
647                 break;
648         } else {
649             /* process audio command */
650             int residual_size = 0;
651             int channel = s->cur_chan;
652             int32_t coffset;
653
654             /* get Rice code for residual decoding */
655             if (cmd != FN_ZERO) {
656                 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
657                 /* This is a hack as version 0 differed in the definition
658                  * of get_sr_golomb_shorten(). */
659                 if (s->version == 0)
660                     residual_size--;
661             }
662
663             /* calculate sample offset using means from previous blocks */
664             if (s->nmean == 0)
665                 coffset = s->offset[channel][0];
666             else {
667                 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
668                 for (i = 0; i < s->nmean; i++)
669                     sum += s->offset[channel][i];
670                 coffset = sum / s->nmean;
671                 if (s->version >= 2)
672                     coffset = s->bitshift == 0 ? coffset : coffset >> s->bitshift - 1 >> 1;
673             }
674
675             /* decode samples for this channel */
676             if (cmd == FN_ZERO) {
677                 for (i = 0; i < s->blocksize; i++)
678                     s->decoded[channel][i] = 0;
679             } else {
680                 if ((ret = decode_subframe_lpc(s, cmd, channel,
681                                                residual_size, coffset)) < 0)
682                     return ret;
683             }
684
685             /* update means with info from the current block */
686             if (s->nmean > 0) {
687                 int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
688                 for (i = 0; i < s->blocksize; i++)
689                     sum += s->decoded[channel][i];
690
691                 for (i = 1; i < s->nmean; i++)
692                     s->offset[channel][i - 1] = s->offset[channel][i];
693
694                 if (s->version < 2)
695                     s->offset[channel][s->nmean - 1] = sum / s->blocksize;
696                 else
697                     s->offset[channel][s->nmean - 1] = s->bitshift == 32 ? 0 : (sum / s->blocksize) << s->bitshift;
698             }
699
700             /* copy wrap samples for use with next block */
701             for (i = -s->nwrap; i < 0; i++)
702                 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
703
704             /* shift samples to add in unused zero bits which were removed
705              * during encoding */
706             fix_bitshift(s, s->decoded[channel]);
707
708             /* if this is the last channel in the block, output the samples */
709             s->cur_chan++;
710             if (s->cur_chan == s->channels) {
711                 uint8_t *samples_u8;
712                 int16_t *samples_s16;
713                 int chan;
714
715                 /* get output buffer */
716                 frame->nb_samples = s->blocksize;
717                 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
718                     return ret;
719
720                 for (chan = 0; chan < s->channels; chan++) {
721                     samples_u8  = ((uint8_t **)frame->extended_data)[chan];
722                     samples_s16 = ((int16_t **)frame->extended_data)[chan];
723                     for (i = 0; i < s->blocksize; i++) {
724                         switch (s->internal_ftype) {
725                         case TYPE_U8:
726                             *samples_u8++ = av_clip_uint8(s->decoded[chan][i]);
727                             break;
728                         case TYPE_S16HL:
729                         case TYPE_S16LH:
730                             *samples_s16++ = av_clip_int16(s->decoded[chan][i]);
731                             break;
732                         }
733                     }
734                     if (s->swap && s->internal_ftype != TYPE_U8)
735                         s->bdsp.bswap16_buf(((uint16_t **)frame->extended_data)[chan],
736                                             ((uint16_t **)frame->extended_data)[chan],
737                                             s->blocksize);
738
739                 }
740
741                 *got_frame_ptr = 1;
742             }
743         }
744     }
745     if (s->cur_chan < s->channels)
746         *got_frame_ptr = 0;
747
748 finish_frame:
749     s->bitindex = get_bits_count(&s->gb) - 8 * (get_bits_count(&s->gb) / 8);
750     i           = get_bits_count(&s->gb) / 8;
751     if (i > buf_size) {
752         av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
753         s->bitstream_size  = 0;
754         s->bitstream_index = 0;
755         return AVERROR_INVALIDDATA;
756     }
757     if (s->bitstream_size) {
758         s->bitstream_index += i;
759         s->bitstream_size  -= i;
760         return input_buf_size;
761     } else
762         return i;
763 }
764
765 static av_cold int shorten_decode_close(AVCodecContext *avctx)
766 {
767     ShortenContext *s = avctx->priv_data;
768     int i;
769
770     for (i = 0; i < s->channels; i++) {
771         s->decoded[i] = NULL;
772         av_freep(&s->decoded_base[i]);
773         av_freep(&s->offset[i]);
774     }
775     av_freep(&s->bitstream);
776     av_freep(&s->coeffs);
777
778     return 0;
779 }
780
781 AVCodec ff_shorten_decoder = {
782     .name           = "shorten",
783     .long_name      = NULL_IF_CONFIG_SMALL("Shorten"),
784     .type           = AVMEDIA_TYPE_AUDIO,
785     .id             = AV_CODEC_ID_SHORTEN,
786     .priv_data_size = sizeof(ShortenContext),
787     .init           = shorten_decode_init,
788     .close          = shorten_decode_close,
789     .decode         = shorten_decode_frame,
790     .capabilities   = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
791     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
792                                                       AV_SAMPLE_FMT_U8P,
793                                                       AV_SAMPLE_FMT_NONE },
794 };