1/*
2 * Shorten decoder
3 * Copyright (c) 2005 Jeff Muizelaar
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; 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
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/** 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
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;
82    AVFrame frame;
83    GetBitContext gb;
84
85    int min_framesize, max_framesize;
86    unsigned channels;
87
88    int32_t *decoded[MAX_CHANNELS];
89    int32_t *decoded_base[MAX_CHANNELS];
90    int32_t *offset[MAX_CHANNELS];
91    int *coeffs;
92    uint8_t *bitstream;
93    int bitstream_size;
94    int bitstream_index;
95    unsigned int allocated_bitstream_size;
96    int header_size;
97    uint8_t header[OUT_BUFFER_SIZE];
98    int version;
99    int cur_chan;
100    int bitshift;
101    int nmean;
102    int internal_ftype;
103    int nwrap;
104    int blocksize;
105    int bitindex;
106    int32_t lpcqoffset;
107    int got_header;
108    int got_quit_command;
109} ShortenContext;
110
111static av_cold int shorten_decode_init(AVCodecContext *avctx)
112{
113    ShortenContext *s = avctx->priv_data;
114    s->avctx          = avctx;
115    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
116
117    avcodec_get_frame_defaults(&s->frame);
118    avctx->coded_frame = &s->frame;
119
120    return 0;
121}
122
123static int allocate_buffers(ShortenContext *s)
124{
125    int i, chan;
126    int *coeffs;
127    void *tmp_ptr;
128
129    for (chan = 0; chan < s->channels; chan++) {
130        if (FFMAX(1, s->nmean) >= UINT_MAX / sizeof(int32_t)) {
131            av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
132            return AVERROR_INVALIDDATA;
133        }
134        if (s->blocksize + s->nwrap >= UINT_MAX / sizeof(int32_t) ||
135            s->blocksize + s->nwrap <= (unsigned)s->nwrap) {
136            av_log(s->avctx, AV_LOG_ERROR,
137                   "s->blocksize + s->nwrap too large\n");
138            return AVERROR_INVALIDDATA;
139        }
140
141        tmp_ptr =
142            av_realloc(s->offset[chan], sizeof(int32_t) * FFMAX(1, s->nmean));
143        if (!tmp_ptr)
144            return AVERROR(ENOMEM);
145        s->offset[chan] = tmp_ptr;
146
147        tmp_ptr = av_realloc(s->decoded_base[chan], (s->blocksize + s->nwrap) *
148                             sizeof(s->decoded_base[0][0]));
149        if (!tmp_ptr)
150            return AVERROR(ENOMEM);
151        s->decoded_base[chan] = tmp_ptr;
152        for (i = 0; i < s->nwrap; i++)
153            s->decoded_base[chan][i] = 0;
154        s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
155    }
156
157    coeffs = av_realloc(s->coeffs, s->nwrap * sizeof(*s->coeffs));
158    if (!coeffs)
159        return AVERROR(ENOMEM);
160    s->coeffs = coeffs;
161
162    return 0;
163}
164
165static inline unsigned int get_uint(ShortenContext *s, int k)
166{
167    if (s->version != 0)
168        k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
169    return get_ur_golomb_shorten(&s->gb, k);
170}
171
172static void fix_bitshift(ShortenContext *s, int32_t *buffer)
173{
174    int i;
175
176    if (s->bitshift != 0)
177        for (i = 0; i < s->blocksize; i++)
178            buffer[i] <<= s->bitshift;
179}
180
181static 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_S16HL:
189    case TYPE_S16LH:
190        mean = 0;
191        break;
192    default:
193        av_log(s->avctx, AV_LOG_ERROR, "unknown audio type");
194        return AVERROR_INVALIDDATA;
195    }
196
197    for (chan = 0; chan < s->channels; chan++)
198        for (i = 0; i < nblock; i++)
199            s->offset[chan][i] = mean;
200    return 0;
201}
202
203static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header,
204                              int header_size)
205{
206    int len;
207    short wave_format;
208
209    if (bytestream_get_le32(&header) != MKTAG('R', 'I', 'F', 'F')) {
210        av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
211        return AVERROR_INVALIDDATA;
212    }
213
214    header += 4; /* chunk size */
215
216    if (bytestream_get_le32(&header) != MKTAG('W', 'A', 'V', 'E')) {
217        av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
218        return AVERROR_INVALIDDATA;
219    }
220
221    while (bytestream_get_le32(&header) != MKTAG('f', 'm', 't', ' ')) {
222        len     = bytestream_get_le32(&header);
223        header += len;
224    }
225    len = bytestream_get_le32(&header);
226
227    if (len < 16) {
228        av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
229        return AVERROR_INVALIDDATA;
230    }
231
232    wave_format = bytestream_get_le16(&header);
233
234    switch (wave_format) {
235    case WAVE_FORMAT_PCM:
236        break;
237    default:
238        av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
239        return AVERROR(ENOSYS);
240    }
241
242    header += 2;        // skip channels    (already got from shorten header)
243    avctx->sample_rate = bytestream_get_le32(&header);
244    header += 4;        // skip bit rate    (represents original uncompressed bit rate)
245    header += 2;        // skip block align (not needed)
246    avctx->bits_per_coded_sample = bytestream_get_le16(&header);
247
248    if (avctx->bits_per_coded_sample != 16) {
249        av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
250        return AVERROR(ENOSYS);
251    }
252
253    len -= 16;
254    if (len > 0)
255        av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
256
257    return 0;
258}
259
260static void interleave_buffer(int16_t *samples, int nchan, int blocksize,
261                              int32_t **buffer)
262{
263    int i, chan;
264    for (i=0; i<blocksize; i++)
265        for (chan=0; chan < nchan; chan++)
266            *samples++ = av_clip_int16(buffer[chan][i]);
267}
268
269static const int fixed_coeffs[3][3] = {
270    { 1,  0,  0 },
271    { 2, -1,  0 },
272    { 3, -3,  1 }
273};
274
275static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
276                               int residual_size, int32_t coffset)
277{
278    int pred_order, sum, qshift, init_sum, i, j;
279    const int *coeffs;
280
281    if (command == FN_QLPC) {
282        /* read/validate prediction order */
283        pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
284        if (pred_order > s->nwrap) {
285            av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
286                   pred_order);
287            return AVERROR(EINVAL);
288        }
289        /* read LPC coefficients */
290        for (i = 0; i < pred_order; i++)
291            s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
292        coeffs = s->coeffs;
293
294        qshift = LPCQUANT;
295    } else {
296        /* fixed LPC coeffs */
297        pred_order = command;
298        coeffs     = fixed_coeffs[pred_order - 1];
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
325static 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        for (i = 0; i < skip_bytes; i++)
372            skip_bits(&s->gb, 8);
373    }
374    s->nwrap = FFMAX(NWRAP, maxnlpc);
375
376    if ((ret = allocate_buffers(s)) < 0)
377        return ret;
378
379    if ((ret = init_offset(s)) < 0)
380        return ret;
381
382    if (s->version > 1)
383        s->lpcqoffset = V2LPCQOFFSET;
384
385    if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
386        av_log(s->avctx, AV_LOG_ERROR,
387               "missing verbatim section at beginning of stream\n");
388        return AVERROR_INVALIDDATA;
389    }
390
391    s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
392    if (s->header_size >= OUT_BUFFER_SIZE ||
393        s->header_size < CANONICAL_HEADER_SIZE) {
394        av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n",
395               s->header_size);
396        return AVERROR_INVALIDDATA;
397    }
398
399    for (i = 0; i < s->header_size; i++)
400        s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
401
402    if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0)
403        return ret;
404
405    s->cur_chan = 0;
406    s->bitshift = 0;
407
408    s->got_header = 1;
409
410    return 0;
411}
412
413static int shorten_decode_frame(AVCodecContext *avctx, void *data,
414                                int *got_frame_ptr, AVPacket *avpkt)
415{
416    const uint8_t *buf = avpkt->data;
417    int buf_size       = avpkt->size;
418    ShortenContext *s  = avctx->priv_data;
419    int i, input_buf_size = 0;
420    int ret;
421
422    /* allocate internal bitstream buffer */
423    if (s->max_framesize == 0) {
424        void *tmp_ptr;
425        s->max_framesize = 1024; // should hopefully be enough for the first header
426        tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
427                                  s->max_framesize);
428        if (!tmp_ptr) {
429            av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
430            return AVERROR(ENOMEM);
431        }
432        s->bitstream = tmp_ptr;
433    }
434
435    /* append current packet data to bitstream buffer */
436    if (1 && s->max_framesize) { //FIXME truncated
437        buf_size       = FFMIN(buf_size, s->max_framesize - s->bitstream_size);
438        input_buf_size = buf_size;
439
440        if (s->bitstream_index + s->bitstream_size + buf_size >
441            s->allocated_bitstream_size) {
442            memmove(s->bitstream, &s->bitstream[s->bitstream_index],
443                    s->bitstream_size);
444            s->bitstream_index = 0;
445        }
446        if (buf)
447            memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf,
448                   buf_size);
449        buf               = &s->bitstream[s->bitstream_index];
450        buf_size         += s->bitstream_size;
451        s->bitstream_size = buf_size;
452
453        /* do not decode until buffer has at least max_framesize bytes or
454         * the end of the file has been reached */
455        if (buf_size < s->max_framesize && avpkt->data) {
456            *got_frame_ptr = 0;
457            return input_buf_size;
458        }
459    }
460    /* init and position bitstream reader */
461    init_get_bits(&s->gb, buf, buf_size * 8);
462    skip_bits(&s->gb, s->bitindex);
463
464    /* process header or next subblock */
465    if (!s->got_header) {
466        if ((ret = read_header(s)) < 0)
467            return ret;
468        *got_frame_ptr = 0;
469        goto finish_frame;
470    }
471
472    /* if quit command was read previously, don't decode anything */
473    if (s->got_quit_command) {
474        *got_frame_ptr = 0;
475        return avpkt->size;
476    }
477
478    s->cur_chan = 0;
479    while (s->cur_chan < s->channels) {
480        int cmd;
481        int len;
482
483        if (get_bits_left(&s->gb) < 3 + FNSIZE) {
484            *got_frame_ptr = 0;
485            break;
486        }
487
488        cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
489
490        if (cmd > FN_VERBATIM) {
491            av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
492            *got_frame_ptr = 0;
493            break;
494        }
495
496        if (!is_audio_command[cmd]) {
497            /* process non-audio command */
498            switch (cmd) {
499            case FN_VERBATIM:
500                len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
501                while (len--)
502                    get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
503                break;
504            case FN_BITSHIFT:
505                s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
506                break;
507            case FN_BLOCKSIZE: {
508                unsigned blocksize = get_uint(s, av_log2(s->blocksize));
509                if (blocksize > s->blocksize) {
510                    av_log(avctx, AV_LOG_ERROR,
511                           "Increasing block size is not supported\n");
512                    return AVERROR_PATCHWELCOME;
513                }
514                if (!blocksize || blocksize > MAX_BLOCKSIZE) {
515                    av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
516                                                "block size: %d\n", blocksize);
517                    return AVERROR(EINVAL);
518                }
519                s->blocksize = blocksize;
520                break;
521            }
522            case FN_QUIT:
523                s->got_quit_command = 1;
524                break;
525            }
526            if (cmd == FN_BLOCKSIZE || cmd == FN_QUIT) {
527                *got_frame_ptr = 0;
528                break;
529            }
530        } else {
531            /* process audio command */
532            int residual_size = 0;
533            int channel = s->cur_chan;
534            int32_t coffset;
535
536            /* get Rice code for residual decoding */
537            if (cmd != FN_ZERO) {
538                residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
539                /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
540                if (s->version == 0)
541                    residual_size--;
542            }
543
544            /* calculate sample offset using means from previous blocks */
545            if (s->nmean == 0)
546                coffset = s->offset[channel][0];
547            else {
548                int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
549                for (i = 0; i < s->nmean; i++)
550                    sum += s->offset[channel][i];
551                coffset = sum / s->nmean;
552                if (s->version >= 2)
553                    coffset >>= FFMIN(1, s->bitshift);
554            }
555
556            /* decode samples for this channel */
557            if (cmd == FN_ZERO) {
558                for (i = 0; i < s->blocksize; i++)
559                    s->decoded[channel][i] = 0;
560            } else {
561                if ((ret = decode_subframe_lpc(s, cmd, channel,
562                                               residual_size, coffset)) < 0)
563                    return ret;
564            }
565
566            /* update means with info from the current block */
567            if (s->nmean > 0) {
568                int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
569                for (i = 0; i < s->blocksize; i++)
570                    sum += s->decoded[channel][i];
571
572                for (i = 1; i < s->nmean; i++)
573                    s->offset[channel][i - 1] = s->offset[channel][i];
574
575                if (s->version < 2)
576                    s->offset[channel][s->nmean - 1] = sum / s->blocksize;
577                else
578                    s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
579            }
580
581            /* copy wrap samples for use with next block */
582            for (i = -s->nwrap; i < 0; i++)
583                s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
584
585            /* shift samples to add in unused zero bits which were removed
586             * during encoding */
587            fix_bitshift(s, s->decoded[channel]);
588
589            /* if this is the last channel in the block, output the samples */
590            s->cur_chan++;
591            if (s->cur_chan == s->channels) {
592                /* get output buffer */
593                s->frame.nb_samples = s->blocksize;
594                if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
595                    av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
596                    return ret;
597                }
598                /* interleave output */
599                interleave_buffer((int16_t *)s->frame.data[0], s->channels,
600                                  s->blocksize, s->decoded);
601
602                *got_frame_ptr   = 1;
603                *(AVFrame *)data = s->frame;
604            }
605        }
606    }
607    if (s->cur_chan < s->channels)
608        *got_frame_ptr = 0;
609
610finish_frame:
611    s->bitindex = get_bits_count(&s->gb) - 8 * (get_bits_count(&s->gb) / 8);
612    i           = get_bits_count(&s->gb) / 8;
613    if (i > buf_size) {
614        av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
615        s->bitstream_size  = 0;
616        s->bitstream_index = 0;
617        return AVERROR_INVALIDDATA;
618    }
619    if (s->bitstream_size) {
620        s->bitstream_index += i;
621        s->bitstream_size  -= i;
622        return input_buf_size;
623    } else
624        return i;
625}
626
627static av_cold int shorten_decode_close(AVCodecContext *avctx)
628{
629    ShortenContext *s = avctx->priv_data;
630    int i;
631
632    for (i = 0; i < s->channels; i++) {
633        s->decoded[i] = NULL;
634        av_freep(&s->decoded_base[i]);
635        av_freep(&s->offset[i]);
636    }
637    av_freep(&s->bitstream);
638    av_freep(&s->coeffs);
639
640    return 0;
641}
642
643AVCodec ff_shorten_decoder = {
644    .name           = "shorten",
645    .type           = AVMEDIA_TYPE_AUDIO,
646    .id             = CODEC_ID_SHORTEN,
647    .priv_data_size = sizeof(ShortenContext),
648    .init           = shorten_decode_init,
649    .close          = shorten_decode_close,
650    .decode         = shorten_decode_frame,
651    .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_DR1,
652    .long_name= NULL_IF_CONFIG_SMALL("Shorten"),
653};
654