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