shorten: skip some fields in the WAV header embedded in the shorten header.
[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     skip_bits(&hb, 16); // skip channels    (already got from shorten header)
235     avctx->sample_rate = get_le32(&hb);
236     skip_bits(&hb, 32); // skip bit rate    (represents original uncompressed bit rate)
237     skip_bits(&hb, 16); // skip block align (not needed)
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     s->avctx->channels = s->channels;
299
300     /* get blocksize if version > 0 */
301     if (s->version > 0) {
302         int skip_bytes;
303         s->blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
304         maxnlpc = get_uint(s, LPCQSIZE);
305         s->nmean = get_uint(s, 0);
306
307         skip_bytes = get_uint(s, NSKIPSIZE);
308         for (i=0; i<skip_bytes; i++) {
309             skip_bits(&s->gb, 8);
310         }
311     }
312     s->nwrap = FFMAX(NWRAP, maxnlpc);
313
314     if (allocate_buffers(s))
315         return -1;
316
317     init_offset(s);
318
319     if (s->version > 1)
320         s->lpcqoffset = V2LPCQOFFSET;
321
322     if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
323         av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at beginning of stream\n");
324         return -1;
325     }
326
327     s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
328     if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
329         av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
330         return -1;
331     }
332
333     for (i=0; i<s->header_size; i++)
334         s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
335
336     if (decode_wave_header(s->avctx, s->header, s->header_size) < 0)
337         return -1;
338
339     s->cur_chan = 0;
340     s->bitshift = 0;
341
342     return 0;
343 }
344
345 static int shorten_decode_frame(AVCodecContext *avctx,
346         void *data, int *data_size,
347         AVPacket *avpkt)
348 {
349     const uint8_t *buf = avpkt->data;
350     int buf_size = avpkt->size;
351     ShortenContext *s = avctx->priv_data;
352     int i, input_buf_size = 0;
353     int16_t *samples = data;
354     if(s->max_framesize == 0){
355         s->max_framesize= 1024; // should hopefully be enough for the first header
356         s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
357     }
358
359     if(1 && s->max_framesize){//FIXME truncated
360         buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
361         input_buf_size= buf_size;
362
363         if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
364             //                printf("memmove\n");
365             memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
366             s->bitstream_index=0;
367         }
368         memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
369         buf= &s->bitstream[s->bitstream_index];
370         buf_size += s->bitstream_size;
371         s->bitstream_size= buf_size;
372
373         if(buf_size < s->max_framesize){
374             *data_size = 0;
375             return input_buf_size;
376         }
377     }
378     init_get_bits(&s->gb, buf, buf_size*8);
379     skip_bits(&s->gb, s->bitindex);
380     if (!s->blocksize)
381     {
382         int ret;
383         if ((ret = read_header(s)) < 0)
384             return ret;
385     }
386     else
387     {
388         int cmd;
389         int len;
390         cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
391         switch (cmd) {
392             case FN_ZERO:
393             case FN_DIFF0:
394             case FN_DIFF1:
395             case FN_DIFF2:
396             case FN_DIFF3:
397             case FN_QLPC:
398                 {
399                     int residual_size = 0;
400                     int channel = s->cur_chan;
401                     int32_t coffset;
402                     if (cmd != FN_ZERO) {
403                         residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
404                         /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
405                         if (s->version == 0)
406                             residual_size--;
407                     }
408
409                     if (s->nmean == 0)
410                         coffset = s->offset[channel][0];
411                     else {
412                         int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
413                         for (i=0; i<s->nmean; i++)
414                             sum += s->offset[channel][i];
415                         coffset = sum / s->nmean;
416                         if (s->version >= 2)
417                             coffset >>= FFMIN(1, s->bitshift);
418                     }
419                     switch (cmd) {
420                         case FN_ZERO:
421                             for (i=0; i<s->blocksize; i++)
422                                 s->decoded[channel][i] = 0;
423                             break;
424                         case FN_DIFF0:
425                             for (i=0; i<s->blocksize; i++)
426                                 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + coffset;
427                             break;
428                         case FN_DIFF1:
429                             for (i=0; i<s->blocksize; i++)
430                                 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + s->decoded[channel][i - 1];
431                             break;
432                         case FN_DIFF2:
433                             for (i=0; i<s->blocksize; i++)
434                                 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 2*s->decoded[channel][i-1]
435                                                                                                       -   s->decoded[channel][i-2];
436                             break;
437                         case FN_DIFF3:
438                             for (i=0; i<s->blocksize; i++)
439                                 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 3*s->decoded[channel][i-1]
440                                                                                                       - 3*s->decoded[channel][i-2]
441                                                                                                       +   s->decoded[channel][i-3];
442                             break;
443                         case FN_QLPC:
444                             {
445                                 int pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
446                                 if (pred_order > s->nwrap) {
447                                     av_log(avctx, AV_LOG_ERROR,
448                                            "invalid pred_order %d\n",
449                                            pred_order);
450                                     return -1;
451                                 }
452                                 for (i=0; i<pred_order; i++)
453                                     s->decoded[channel][i - pred_order] -= coffset;
454                                 decode_subframe_lpc(s, channel, residual_size, pred_order);
455                                 if (coffset != 0)
456                                     for (i=0; i < s->blocksize; i++)
457                                         s->decoded[channel][i] += coffset;
458                             }
459                     }
460                     if (s->nmean > 0) {
461                         int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
462                         for (i=0; i<s->blocksize; i++)
463                             sum += s->decoded[channel][i];
464
465                         for (i=1; i<s->nmean; i++)
466                             s->offset[channel][i-1] = s->offset[channel][i];
467
468                         if (s->version < 2)
469                             s->offset[channel][s->nmean - 1] = sum / s->blocksize;
470                         else
471                             s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
472                     }
473                     for (i=-s->nwrap; i<0; i++)
474                         s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
475
476                     fix_bitshift(s, s->decoded[channel]);
477
478                     s->cur_chan++;
479                     if (s->cur_chan == s->channels) {
480                         samples = interleave_buffer(samples, s->channels, s->blocksize, s->decoded);
481                         s->cur_chan = 0;
482                         goto frame_done;
483                     }
484                 }
485                 break;
486             case FN_VERBATIM:
487                 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
488                 while (len--) {
489                     get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
490                 }
491                 break;
492             case FN_BITSHIFT:
493                 s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
494                 break;
495             case FN_BLOCKSIZE: {
496                 int blocksize = get_uint(s, av_log2(s->blocksize));
497                 if (blocksize > s->blocksize) {
498                     av_log(avctx, AV_LOG_ERROR, "Increasing block size is not supported\n");
499                     return AVERROR_PATCHWELCOME;
500                 }
501                 s->blocksize = blocksize;
502                 break;
503             }
504             case FN_QUIT:
505                 *data_size = 0;
506                 return buf_size;
507             default:
508                 av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
509                 return -1;
510         }
511     }
512 frame_done:
513     *data_size = (int8_t *)samples - (int8_t *)data;
514
515     //    s->last_blocksize = s->blocksize;
516     s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
517     i= (get_bits_count(&s->gb))/8;
518     if (i > buf_size) {
519         av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
520         s->bitstream_size=0;
521         s->bitstream_index=0;
522         return -1;
523     }
524     if (s->bitstream_size) {
525         s->bitstream_index += i;
526         s->bitstream_size  -= i;
527         return input_buf_size;
528     } else
529         return i;
530 }
531
532 static av_cold int shorten_decode_close(AVCodecContext *avctx)
533 {
534     ShortenContext *s = avctx->priv_data;
535     int i;
536
537     for (i = 0; i < s->channels; i++) {
538         s->decoded[i] -= s->nwrap;
539         av_freep(&s->decoded[i]);
540         av_freep(&s->offset[i]);
541     }
542     av_freep(&s->bitstream);
543     av_freep(&s->coeffs);
544     return 0;
545 }
546
547 AVCodec ff_shorten_decoder = {
548     .name           = "shorten",
549     .type           = AVMEDIA_TYPE_AUDIO,
550     .id             = CODEC_ID_SHORTEN,
551     .priv_data_size = sizeof(ShortenContext),
552     .init           = shorten_decode_init,
553     .close          = shorten_decode_close,
554     .decode         = shorten_decode_frame,
555     .long_name= NULL_IF_CONFIG_SMALL("Shorten"),
556 };