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#define DEBUG
30#include <limits.h>
31#include "avcodec.h"
32#include "get_bits.h"
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
73#define VERBATIM_CKSIZE_SIZE 5
74#define VERBATIM_BYTE_SIZE 8
75#define CANONICAL_HEADER_SIZE 44
76
77typedef struct ShortenContext {
78    AVCodecContext *avctx;
79    GetBitContext gb;
80
81    int min_framesize, max_framesize;
82    int channels;
83
84    int32_t *decoded[MAX_CHANNELS];
85    int32_t *offset[MAX_CHANNELS];
86    uint8_t *bitstream;
87    int bitstream_size;
88    int bitstream_index;
89    unsigned int allocated_bitstream_size;
90    int header_size;
91    uint8_t header[OUT_BUFFER_SIZE];
92    int version;
93    int cur_chan;
94    int bitshift;
95    int nmean;
96    int internal_ftype;
97    int nwrap;
98    int blocksize;
99    int bitindex;
100    int32_t lpcqoffset;
101} ShortenContext;
102
103static av_cold int shorten_decode_init(AVCodecContext * avctx)
104{
105    ShortenContext *s = avctx->priv_data;
106    s->avctx = avctx;
107    avctx->sample_fmt = SAMPLE_FMT_S16;
108
109    return 0;
110}
111
112static int allocate_buffers(ShortenContext *s)
113{
114    int i, chan;
115    for (chan=0; chan<s->channels; chan++) {
116        if(FFMAX(1, s->nmean) >= UINT_MAX/sizeof(int32_t)){
117            av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
118            return -1;
119        }
120        if(s->blocksize + s->nwrap >= UINT_MAX/sizeof(int32_t) || s->blocksize + s->nwrap <= (unsigned)s->nwrap){
121            av_log(s->avctx, AV_LOG_ERROR, "s->blocksize + s->nwrap too large\n");
122            return -1;
123        }
124
125        s->offset[chan] = av_realloc(s->offset[chan], sizeof(int32_t)*FFMAX(1, s->nmean));
126
127        s->decoded[chan] = av_realloc(s->decoded[chan], sizeof(int32_t)*(s->blocksize + s->nwrap));
128        for (i=0; i<s->nwrap; i++)
129            s->decoded[chan][i] = 0;
130        s->decoded[chan] += s->nwrap;
131    }
132    return 0;
133}
134
135
136static inline unsigned int get_uint(ShortenContext *s, int k)
137{
138    if (s->version != 0)
139        k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
140    return get_ur_golomb_shorten(&s->gb, k);
141}
142
143
144static void fix_bitshift(ShortenContext *s, int32_t *buffer)
145{
146    int i;
147
148    if (s->bitshift != 0)
149        for (i = 0; i < s->blocksize; i++)
150            buffer[s->nwrap + i] <<= s->bitshift;
151}
152
153
154static void init_offset(ShortenContext *s)
155{
156    int32_t mean = 0;
157    int  chan, i;
158    int nblock = FFMAX(1, s->nmean);
159    /* initialise offset */
160    switch (s->internal_ftype)
161    {
162        case TYPE_S16HL:
163        case TYPE_S16LH:
164            mean = 0;
165            break;
166        default:
167            av_log(s->avctx, AV_LOG_ERROR, "unknown audio type");
168            abort();
169    }
170
171    for (chan = 0; chan < s->channels; chan++)
172        for (i = 0; i < nblock; i++)
173            s->offset[chan][i] = mean;
174}
175
176static inline int get_le32(GetBitContext *gb)
177{
178    return bswap_32(get_bits_long(gb, 32));
179}
180
181static inline short get_le16(GetBitContext *gb)
182{
183    return bswap_16(get_bits_long(gb, 16));
184}
185
186static int decode_wave_header(AVCodecContext *avctx, uint8_t *header, int header_size)
187{
188    GetBitContext hb;
189    int len;
190    int chunk_size;
191    short wave_format;
192
193    init_get_bits(&hb, header, header_size*8);
194    if (get_le32(&hb) != MKTAG('R','I','F','F')) {
195        av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
196        return -1;
197    }
198
199    chunk_size = get_le32(&hb);
200
201    if (get_le32(&hb) != MKTAG('W','A','V','E')) {
202        av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
203        return -1;
204    }
205
206    while (get_le32(&hb) != MKTAG('f','m','t',' ')) {
207        len = get_le32(&hb);
208        skip_bits(&hb, 8*len);
209    }
210    len = get_le32(&hb);
211
212    if (len < 16) {
213        av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
214        return -1;
215    }
216
217    wave_format = get_le16(&hb);
218
219    switch (wave_format) {
220        case WAVE_FORMAT_PCM:
221            break;
222        default:
223            av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
224            return -1;
225    }
226
227    avctx->channels = get_le16(&hb);
228    avctx->sample_rate = get_le32(&hb);
229    avctx->bit_rate = get_le32(&hb) * 8;
230    avctx->block_align = get_le16(&hb);
231    avctx->bits_per_coded_sample = get_le16(&hb);
232
233    if (avctx->bits_per_coded_sample != 16) {
234        av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
235        return -1;
236    }
237
238    len -= 16;
239    if (len > 0)
240        av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
241
242    return 0;
243}
244
245static int16_t * interleave_buffer(int16_t *samples, int nchan, int blocksize, int32_t **buffer) {
246    int i, chan;
247    for (i=0; i<blocksize; i++)
248        for (chan=0; chan < nchan; chan++)
249            *samples++ = FFMIN(buffer[chan][i], 32768);
250    return samples;
251}
252
253static void decode_subframe_lpc(ShortenContext *s, int channel, int residual_size, int pred_order)
254{
255    int sum, i, j;
256    int coeffs[pred_order];
257
258    for (i=0; i<pred_order; i++)
259        coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
260
261    for (i=0; i < s->blocksize; i++) {
262        sum = s->lpcqoffset;
263        for (j=0; j<pred_order; j++)
264            sum += coeffs[j] * s->decoded[channel][i-j-1];
265        s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + (sum >> LPCQUANT);
266    }
267}
268
269
270static int shorten_decode_frame(AVCodecContext *avctx,
271        void *data, int *data_size,
272        AVPacket *avpkt)
273{
274    const uint8_t *buf = avpkt->data;
275    int buf_size = avpkt->size;
276    ShortenContext *s = avctx->priv_data;
277    int i, input_buf_size = 0;
278    int16_t *samples = data;
279    if(s->max_framesize == 0){
280        s->max_framesize= 1024; // should hopefully be enough for the first header
281        s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
282    }
283
284    if(1 && s->max_framesize){//FIXME truncated
285        buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
286        input_buf_size= buf_size;
287
288        if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
289            //                printf("memmove\n");
290            memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
291            s->bitstream_index=0;
292        }
293        memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
294        buf= &s->bitstream[s->bitstream_index];
295        buf_size += s->bitstream_size;
296        s->bitstream_size= buf_size;
297
298        if(buf_size < s->max_framesize){
299            //dprintf(avctx, "wanna more data ... %d\n", buf_size);
300            *data_size = 0;
301            return input_buf_size;
302        }
303    }
304    init_get_bits(&s->gb, buf, buf_size*8);
305    skip_bits(&s->gb, s->bitindex);
306    if (!s->blocksize)
307    {
308        int maxnlpc = 0;
309        /* shorten signature */
310        if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
311            av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
312            return -1;
313        }
314
315        s->lpcqoffset = 0;
316        s->blocksize = DEFAULT_BLOCK_SIZE;
317        s->channels = 1;
318        s->nmean = -1;
319        s->version = get_bits(&s->gb, 8);
320        s->internal_ftype = get_uint(s, TYPESIZE);
321
322        s->channels = get_uint(s, CHANSIZE);
323        if (s->channels > MAX_CHANNELS) {
324            av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
325            return -1;
326        }
327
328        /* get blocksize if version > 0 */
329        if (s->version > 0) {
330            int skip_bytes;
331            s->blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
332            maxnlpc = get_uint(s, LPCQSIZE);
333            s->nmean = get_uint(s, 0);
334
335            skip_bytes = get_uint(s, NSKIPSIZE);
336            for (i=0; i<skip_bytes; i++) {
337                skip_bits(&s->gb, 8);
338            }
339        }
340        s->nwrap = FFMAX(NWRAP, maxnlpc);
341
342        if (allocate_buffers(s))
343            return -1;
344
345        init_offset(s);
346
347        if (s->version > 1)
348            s->lpcqoffset = V2LPCQOFFSET;
349
350        if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
351            av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at beginning of stream\n");
352            return -1;
353        }
354
355        s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
356        if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
357            av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
358            return -1;
359        }
360
361        for (i=0; i<s->header_size; i++)
362            s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
363
364        if (decode_wave_header(avctx, s->header, s->header_size) < 0)
365            return -1;
366
367        s->cur_chan = 0;
368        s->bitshift = 0;
369    }
370    else
371    {
372        int cmd;
373        int len;
374        cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
375        switch (cmd) {
376            case FN_ZERO:
377            case FN_DIFF0:
378            case FN_DIFF1:
379            case FN_DIFF2:
380            case FN_DIFF3:
381            case FN_QLPC:
382                {
383                    int residual_size = 0;
384                    int channel = s->cur_chan;
385                    int32_t coffset;
386                    if (cmd != FN_ZERO) {
387                        residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
388                        /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
389                        if (s->version == 0)
390                            residual_size--;
391                    }
392
393                    if (s->nmean == 0)
394                        coffset = s->offset[channel][0];
395                    else {
396                        int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
397                        for (i=0; i<s->nmean; i++)
398                            sum += s->offset[channel][i];
399                        coffset = sum / s->nmean;
400                        if (s->version >= 2)
401                            coffset >>= FFMIN(1, s->bitshift);
402                    }
403                    switch (cmd) {
404                        case FN_ZERO:
405                            for (i=0; i<s->blocksize; i++)
406                                s->decoded[channel][i] = 0;
407                            break;
408                        case FN_DIFF0:
409                            for (i=0; i<s->blocksize; i++)
410                                s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + coffset;
411                            break;
412                        case FN_DIFF1:
413                            for (i=0; i<s->blocksize; i++)
414                                s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + s->decoded[channel][i - 1];
415                            break;
416                        case FN_DIFF2:
417                            for (i=0; i<s->blocksize; i++)
418                                s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 2*s->decoded[channel][i-1]
419                                                                                                      -   s->decoded[channel][i-2];
420                            break;
421                        case FN_DIFF3:
422                            for (i=0; i<s->blocksize; i++)
423                                s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 3*s->decoded[channel][i-1]
424                                                                                                      - 3*s->decoded[channel][i-2]
425                                                                                                      +   s->decoded[channel][i-3];
426                            break;
427                        case FN_QLPC:
428                            {
429                                int pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
430                                for (i=0; i<pred_order; i++)
431                                    s->decoded[channel][i - pred_order] -= coffset;
432                                decode_subframe_lpc(s, channel, residual_size, pred_order);
433                                if (coffset != 0)
434                                    for (i=0; i < s->blocksize; i++)
435                                        s->decoded[channel][i] += coffset;
436                            }
437                    }
438                    if (s->nmean > 0) {
439                        int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
440                        for (i=0; i<s->blocksize; i++)
441                            sum += s->decoded[channel][i];
442
443                        for (i=1; i<s->nmean; i++)
444                            s->offset[channel][i-1] = s->offset[channel][i];
445
446                        if (s->version < 2)
447                            s->offset[channel][s->nmean - 1] = sum / s->blocksize;
448                        else
449                            s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
450                    }
451                    for (i=-s->nwrap; i<0; i++)
452                        s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
453
454                    fix_bitshift(s, s->decoded[channel]);
455
456                    s->cur_chan++;
457                    if (s->cur_chan == s->channels) {
458                        samples = interleave_buffer(samples, s->channels, s->blocksize, s->decoded);
459                        s->cur_chan = 0;
460                        goto frame_done;
461                    }
462                    break;
463                }
464                break;
465            case FN_VERBATIM:
466                len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
467                while (len--) {
468                    get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
469                }
470                break;
471            case FN_BITSHIFT:
472                s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
473                break;
474            case FN_BLOCKSIZE:
475                s->blocksize = get_uint(s, av_log2(s->blocksize));
476                break;
477            case FN_QUIT:
478                *data_size = 0;
479                return buf_size;
480                break;
481            default:
482                av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
483                return -1;
484                break;
485        }
486    }
487frame_done:
488    *data_size = (int8_t *)samples - (int8_t *)data;
489
490    //    s->last_blocksize = s->blocksize;
491    s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
492    i= (get_bits_count(&s->gb))/8;
493    if (i > buf_size) {
494        av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
495        s->bitstream_size=0;
496        s->bitstream_index=0;
497        return -1;
498    }
499    if (s->bitstream_size) {
500        s->bitstream_index += i;
501        s->bitstream_size  -= i;
502        return input_buf_size;
503    } else
504        return i;
505}
506
507static av_cold int shorten_decode_close(AVCodecContext *avctx)
508{
509    ShortenContext *s = avctx->priv_data;
510    int i;
511
512    for (i = 0; i < s->channels; i++) {
513        s->decoded[i] -= s->nwrap;
514        av_freep(&s->decoded[i]);
515        av_freep(&s->offset[i]);
516    }
517    av_freep(&s->bitstream);
518    return 0;
519}
520
521static void shorten_flush(AVCodecContext *avctx){
522    ShortenContext *s = avctx->priv_data;
523
524    s->bitstream_size=
525        s->bitstream_index= 0;
526}
527
528AVCodec shorten_decoder = {
529    "shorten",
530    AVMEDIA_TYPE_AUDIO,
531    CODEC_ID_SHORTEN,
532    sizeof(ShortenContext),
533    shorten_decode_init,
534    NULL,
535    shorten_decode_close,
536    shorten_decode_frame,
537    .flush= shorten_flush,
538    .long_name= NULL_IF_CONFIG_SMALL("Shorten"),
539};
540