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