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