remove ff_get_fourcc() and use AV_RL32() instead
[ffmpeg.git] / 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 shorten.c
24  * Shorten decoder
25  * @author Jeff Muizelaar
26  *
27  */
28
29 #define DEBUG
30 #include <limits.h>
31 #include "avcodec.h"
32 #include "bitstream.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
77 typedef 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
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 = SAMPLE_FMT_S16;
108
109     return 0;
110 }
111
112 static 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
136 static 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
144 static 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
154 static 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
176 static inline int get_le32(GetBitContext *gb)
177 {
178     return bswap_32(get_bits_long(gb, 32));
179 }
180
181 static inline short get_le16(GetBitContext *gb)
182 {
183     return bswap_16(get_bits_long(gb, 16));
184 }
185
186 static 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
245 static 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
253 static 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
270 static int shorten_decode_frame(AVCodecContext *avctx,
271         void *data, int *data_size,
272         const uint8_t *buf, int buf_size)
273 {
274     ShortenContext *s = avctx->priv_data;
275     int i, input_buf_size = 0;
276     int16_t *samples = data;
277     if(s->max_framesize == 0){
278         s->max_framesize= 1024; // should hopefully be enough for the first header
279         s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
280     }
281
282     if(1 && s->max_framesize){//FIXME truncated
283         buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
284         input_buf_size= buf_size;
285
286         if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
287             //                printf("memmove\n");
288             memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
289             s->bitstream_index=0;
290         }
291         memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
292         buf= &s->bitstream[s->bitstream_index];
293         buf_size += s->bitstream_size;
294         s->bitstream_size= buf_size;
295
296         if(buf_size < s->max_framesize){
297             //dprintf(avctx, "wanna more data ... %d\n", buf_size);
298             *data_size = 0;
299             return input_buf_size;
300         }
301     }
302     init_get_bits(&s->gb, buf, buf_size*8);
303     skip_bits(&s->gb, s->bitindex);
304     if (!s->blocksize)
305     {
306         int maxnlpc = 0;
307         /* shorten signature */
308         if (get_bits_long(&s->gb, 32) != bswap_32(AV_RL32("ajkg"))) {
309             av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
310             return -1;
311         }
312
313         s->lpcqoffset = 0;
314         s->blocksize = DEFAULT_BLOCK_SIZE;
315         s->channels = 1;
316         s->nmean = -1;
317         s->version = get_bits(&s->gb, 8);
318         s->internal_ftype = get_uint(s, TYPESIZE);
319
320         s->channels = get_uint(s, CHANSIZE);
321         if (s->channels > MAX_CHANNELS) {
322             av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
323             return -1;
324         }
325
326         /* get blocksize if version > 0 */
327         if (s->version > 0) {
328             int skip_bytes;
329             s->blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
330             maxnlpc = get_uint(s, LPCQSIZE);
331             s->nmean = get_uint(s, 0);
332
333             skip_bytes = get_uint(s, NSKIPSIZE);
334             for (i=0; i<skip_bytes; i++) {
335                 skip_bits(&s->gb, 8);
336             }
337         }
338         s->nwrap = FFMAX(NWRAP, maxnlpc);
339
340         if (allocate_buffers(s))
341             return -1;
342
343         init_offset(s);
344
345         if (s->version > 1)
346             s->lpcqoffset = V2LPCQOFFSET;
347
348         if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
349             av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at beginning of stream\n");
350             return -1;
351         }
352
353         s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
354         if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
355             av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
356             return -1;
357         }
358
359         for (i=0; i<s->header_size; i++)
360             s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
361
362         if (decode_wave_header(avctx, s->header, s->header_size) < 0)
363             return -1;
364
365         s->cur_chan = 0;
366         s->bitshift = 0;
367     }
368     else
369     {
370         int cmd;
371         int len;
372         cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
373         switch (cmd) {
374             case FN_ZERO:
375             case FN_DIFF0:
376             case FN_DIFF1:
377             case FN_DIFF2:
378             case FN_DIFF3:
379             case FN_QLPC:
380                 {
381                     int residual_size = 0;
382                     int channel = s->cur_chan;
383                     int32_t coffset;
384                     if (cmd != FN_ZERO) {
385                         residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
386                         /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
387                         if (s->version == 0)
388                             residual_size--;
389                     }
390
391                     if (s->nmean == 0)
392                         coffset = s->offset[channel][0];
393                     else {
394                         int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
395                         for (i=0; i<s->nmean; i++)
396                             sum += s->offset[channel][i];
397                         coffset = sum / s->nmean;
398                         if (s->version >= 2)
399                             coffset >>= FFMIN(1, s->bitshift);
400                     }
401                     switch (cmd) {
402                         case FN_ZERO:
403                             for (i=0; i<s->blocksize; i++)
404                                 s->decoded[channel][i] = 0;
405                             break;
406                         case FN_DIFF0:
407                             for (i=0; i<s->blocksize; i++)
408                                 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + coffset;
409                             break;
410                         case FN_DIFF1:
411                             for (i=0; i<s->blocksize; i++)
412                                 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + s->decoded[channel][i - 1];
413                             break;
414                         case FN_DIFF2:
415                             for (i=0; i<s->blocksize; i++)
416                                 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 2*s->decoded[channel][i-1]
417                                                                                                       -   s->decoded[channel][i-2];
418                             break;
419                         case FN_DIFF3:
420                             for (i=0; i<s->blocksize; i++)
421                                 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + 3*s->decoded[channel][i-1]
422                                                                                                       - 3*s->decoded[channel][i-2]
423                                                                                                       +   s->decoded[channel][i-3];
424                             break;
425                         case FN_QLPC:
426                             {
427                                 int pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
428                                 for (i=0; i<pred_order; i++)
429                                     s->decoded[channel][i - pred_order] -= coffset;
430                                 decode_subframe_lpc(s, channel, residual_size, pred_order);
431                                 if (coffset != 0)
432                                     for (i=0; i < s->blocksize; i++)
433                                         s->decoded[channel][i] += coffset;
434                             }
435                     }
436                     if (s->nmean > 0) {
437                         int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
438                         for (i=0; i<s->blocksize; i++)
439                             sum += s->decoded[channel][i];
440
441                         for (i=1; i<s->nmean; i++)
442                             s->offset[channel][i-1] = s->offset[channel][i];
443
444                         if (s->version < 2)
445                             s->offset[channel][s->nmean - 1] = sum / s->blocksize;
446                         else
447                             s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
448                     }
449                     for (i=-s->nwrap; i<0; i++)
450                         s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
451
452                     fix_bitshift(s, s->decoded[channel]);
453
454                     s->cur_chan++;
455                     if (s->cur_chan == s->channels) {
456                         samples = interleave_buffer(samples, s->channels, s->blocksize, s->decoded);
457                         s->cur_chan = 0;
458                         goto frame_done;
459                     }
460                     break;
461                 }
462                 break;
463             case FN_VERBATIM:
464                 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
465                 while (len--) {
466                     get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
467                 }
468                 break;
469             case FN_BITSHIFT:
470                 s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
471                 break;
472             case FN_BLOCKSIZE:
473                 s->blocksize = get_uint(s, av_log2(s->blocksize));
474                 break;
475             case FN_QUIT:
476                 *data_size = 0;
477                 return buf_size;
478                 break;
479             default:
480                 av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
481                 return -1;
482                 break;
483         }
484     }
485 frame_done:
486     *data_size = (int8_t *)samples - (int8_t *)data;
487
488     //    s->last_blocksize = s->blocksize;
489     s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
490     i= (get_bits_count(&s->gb))/8;
491     if (i > buf_size) {
492         av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
493         s->bitstream_size=0;
494         s->bitstream_index=0;
495         return -1;
496     }
497     if (s->bitstream_size) {
498         s->bitstream_index += i;
499         s->bitstream_size  -= i;
500         return input_buf_size;
501     } else
502         return i;
503 }
504
505 static av_cold int shorten_decode_close(AVCodecContext *avctx)
506 {
507     ShortenContext *s = avctx->priv_data;
508     int i;
509
510     for (i = 0; i < s->channels; i++) {
511         s->decoded[i] -= s->nwrap;
512         av_freep(&s->decoded[i]);
513         av_freep(&s->offset[i]);
514     }
515     av_freep(&s->bitstream);
516     return 0;
517 }
518
519 static void shorten_flush(AVCodecContext *avctx){
520     ShortenContext *s = avctx->priv_data;
521
522     s->bitstream_size=
523         s->bitstream_index= 0;
524 }
525
526 AVCodec shorten_decoder = {
527     "shorten",
528     CODEC_TYPE_AUDIO,
529     CODEC_ID_SHORTEN,
530     sizeof(ShortenContext),
531     shorten_decode_init,
532     NULL,
533     shorten_decode_close,
534     shorten_decode_frame,
535     .flush= shorten_flush,
536     .long_name= NULL_IF_CONFIG_SMALL("Shorten"),
537 };