]> git.sesse.net Git - ffmpeg/blob - libavcodec/shorten.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / 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
24  * Shorten decoder
25  * @author Jeff Muizelaar
26  *
27  */
28
29 #include <limits.h>
30 #include "avcodec.h"
31 #include "bytestream.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 /** indicates if the FN_* command is audio or non-audio */
74 static const uint8_t is_audio_command[10] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0 };
75
76 #define VERBATIM_CKSIZE_SIZE 5
77 #define VERBATIM_BYTE_SIZE 8
78 #define CANONICAL_HEADER_SIZE 44
79
80 typedef struct ShortenContext {
81     AVCodecContext *avctx;
82     GetBitContext gb;
83
84     int min_framesize, max_framesize;
85     int channels;
86
87     int32_t *decoded[MAX_CHANNELS];
88     int32_t *offset[MAX_CHANNELS];
89     int *coeffs;
90     uint8_t *bitstream;
91     int bitstream_size;
92     int bitstream_index;
93     unsigned int allocated_bitstream_size;
94     int header_size;
95     uint8_t header[OUT_BUFFER_SIZE];
96     int version;
97     int cur_chan;
98     int bitshift;
99     int nmean;
100     int internal_ftype;
101     int nwrap;
102     int blocksize;
103     int bitindex;
104     int32_t lpcqoffset;
105     int got_header;
106     int got_quit_command;
107 } ShortenContext;
108
109 static av_cold int shorten_decode_init(AVCodecContext * avctx)
110 {
111     ShortenContext *s = avctx->priv_data;
112     s->avctx = avctx;
113     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
114
115     return 0;
116 }
117
118 static int allocate_buffers(ShortenContext *s)
119 {
120     int i, chan;
121     int *coeffs;
122     void *tmp_ptr;
123
124     for (chan=0; chan<s->channels; chan++) {
125         if(FFMAX(1, s->nmean) >= UINT_MAX/sizeof(int32_t)){
126             av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
127             return -1;
128         }
129         if(s->blocksize + s->nwrap >= UINT_MAX/sizeof(int32_t) || s->blocksize + s->nwrap <= (unsigned)s->nwrap){
130             av_log(s->avctx, AV_LOG_ERROR, "s->blocksize + s->nwrap too large\n");
131             return -1;
132         }
133
134         tmp_ptr = av_realloc(s->offset[chan], sizeof(int32_t)*FFMAX(1, s->nmean));
135         if (!tmp_ptr)
136             return AVERROR(ENOMEM);
137         s->offset[chan] = tmp_ptr;
138
139         tmp_ptr = av_realloc(s->decoded[chan], sizeof(int32_t)*(s->blocksize + s->nwrap));
140         if (!tmp_ptr)
141             return AVERROR(ENOMEM);
142         s->decoded[chan] = tmp_ptr;
143         for (i=0; i<s->nwrap; i++)
144             s->decoded[chan][i] = 0;
145         s->decoded[chan] += s->nwrap;
146     }
147
148     coeffs = av_realloc(s->coeffs, s->nwrap * sizeof(*s->coeffs));
149     if (!coeffs)
150         return AVERROR(ENOMEM);
151     s->coeffs = coeffs;
152
153     return 0;
154 }
155
156
157 static inline unsigned int get_uint(ShortenContext *s, int k)
158 {
159     if (s->version != 0)
160         k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
161     return get_ur_golomb_shorten(&s->gb, k);
162 }
163
164
165 static void fix_bitshift(ShortenContext *s, int32_t *buffer)
166 {
167     int i;
168
169     if (s->bitshift != 0)
170         for (i = 0; i < s->blocksize; i++)
171             buffer[i] <<= s->bitshift;
172 }
173
174
175 static void init_offset(ShortenContext *s)
176 {
177     int32_t mean = 0;
178     int  chan, i;
179     int nblock = FFMAX(1, s->nmean);
180     /* initialise offset */
181     switch (s->internal_ftype)
182     {
183         case TYPE_S16HL:
184         case TYPE_S16LH:
185             mean = 0;
186             break;
187         default:
188             av_log(s->avctx, AV_LOG_ERROR, "unknown audio type");
189             abort();
190     }
191
192     for (chan = 0; chan < s->channels; chan++)
193         for (i = 0; i < nblock; i++)
194             s->offset[chan][i] = mean;
195 }
196
197 static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header,
198                               int header_size)
199 {
200     int len;
201     short wave_format;
202
203
204     if (bytestream_get_le32(&header) != MKTAG('R','I','F','F')) {
205         av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
206         return -1;
207     }
208
209     header += 4; /* chunk size */;
210
211     if (bytestream_get_le32(&header) != MKTAG('W','A','V','E')) {
212         av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
213         return -1;
214     }
215
216     while (bytestream_get_le32(&header) != MKTAG('f','m','t',' ')) {
217         len = bytestream_get_le32(&header);
218         header += len;
219     }
220     len = bytestream_get_le32(&header);
221
222     if (len < 16) {
223         av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
224         return -1;
225     }
226
227     wave_format = bytestream_get_le16(&header);
228
229     switch (wave_format) {
230         case WAVE_FORMAT_PCM:
231             break;
232         default:
233             av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
234             return -1;
235     }
236
237     header += 2;        // skip channels    (already got from shorten header)
238     avctx->sample_rate = bytestream_get_le32(&header);
239     header += 4;        // skip bit rate    (represents original uncompressed bit rate)
240     header += 2;        // skip block align (not needed)
241     avctx->bits_per_coded_sample = bytestream_get_le16(&header);
242
243     if (avctx->bits_per_coded_sample != 16) {
244         av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n");
245         return -1;
246     }
247
248     len -= 16;
249     if (len > 0)
250         av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
251
252     return 0;
253 }
254
255 static void interleave_buffer(int16_t *samples, int nchan, int blocksize,
256                               int32_t **buffer)
257 {
258     int i, chan;
259     for (i=0; i<blocksize; i++)
260         for (chan=0; chan < nchan; chan++)
261             *samples++ = av_clip_int16(buffer[chan][i]);
262 }
263
264 static const int fixed_coeffs[3][3] = {
265     { 1,  0,  0 },
266     { 2, -1,  0 },
267     { 3, -3,  1 }
268 };
269
270 static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
271                                int residual_size, int32_t coffset)
272 {
273     int pred_order, sum, qshift, init_sum, i, j;
274     const int *coeffs;
275
276     if (command == FN_QLPC) {
277         /* read/validate prediction order */
278         pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
279         if (pred_order > s->nwrap) {
280             av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n", pred_order);
281             return AVERROR(EINVAL);
282         }
283         /* read LPC coefficients */
284         for (i=0; i<pred_order; i++)
285             s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
286         coeffs = s->coeffs;
287
288         qshift = LPCQUANT;
289     } else {
290         /* fixed LPC coeffs */
291         pred_order = command;
292         coeffs     = fixed_coeffs[pred_order-1];
293         qshift     = 0;
294     }
295
296     /* subtract offset from previous samples to use in prediction */
297     if (command == FN_QLPC && coffset)
298         for (i = -pred_order; i < 0; i++)
299             s->decoded[channel][i] -= coffset;
300
301     /* decode residual and do LPC prediction */
302     init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
303     for (i=0; i < s->blocksize; i++) {
304         sum = init_sum;
305         for (j=0; j<pred_order; j++)
306             sum += coeffs[j] * s->decoded[channel][i-j-1];
307         s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + (sum >> qshift);
308     }
309
310     /* add offset to current samples */
311     if (command == FN_QLPC && coffset)
312         for (i = 0; i < s->blocksize; i++)
313             s->decoded[channel][i] += coffset;
314
315     return 0;
316 }
317
318 static int read_header(ShortenContext *s)
319 {
320     int i, ret;
321     int maxnlpc = 0;
322     /* shorten signature */
323     if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
324         av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
325         return -1;
326     }
327
328     s->lpcqoffset = 0;
329     s->blocksize = DEFAULT_BLOCK_SIZE;
330     s->channels = 1;
331     s->nmean = -1;
332     s->version = get_bits(&s->gb, 8);
333     s->internal_ftype = get_uint(s, TYPESIZE);
334
335     s->channels = get_uint(s, CHANSIZE);
336     if (s->channels > MAX_CHANNELS) {
337         av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
338         return -1;
339     }
340     s->avctx->channels = s->channels;
341
342     /* get blocksize if version > 0 */
343     if (s->version > 0) {
344         int skip_bytes, blocksize;
345
346         blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
347         if (!blocksize || blocksize > MAX_BLOCKSIZE) {
348             av_log(s->avctx, AV_LOG_ERROR, "invalid or unsupported block size: %d\n",
349                    blocksize);
350             return AVERROR(EINVAL);
351         }
352         s->blocksize = blocksize;
353
354         maxnlpc = get_uint(s, LPCQSIZE);
355         s->nmean = get_uint(s, 0);
356
357         skip_bytes = get_uint(s, NSKIPSIZE);
358         for (i=0; i<skip_bytes; i++) {
359             skip_bits(&s->gb, 8);
360         }
361     }
362     s->nwrap = FFMAX(NWRAP, maxnlpc);
363
364     if ((ret = allocate_buffers(s)) < 0)
365         return ret;
366
367     init_offset(s);
368
369     if (s->version > 1)
370         s->lpcqoffset = V2LPCQOFFSET;
371
372     if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
373         av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at beginning of stream\n");
374         return -1;
375     }
376
377     s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
378     if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
379         av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
380         return -1;
381     }
382
383     for (i=0; i<s->header_size; i++)
384         s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
385
386     if (decode_wave_header(s->avctx, s->header, s->header_size) < 0)
387         return -1;
388
389     s->cur_chan = 0;
390     s->bitshift = 0;
391
392     s->got_header = 1;
393
394     return 0;
395 }
396
397 static int shorten_decode_frame(AVCodecContext *avctx,
398         void *data, int *data_size,
399         AVPacket *avpkt)
400 {
401     const uint8_t *buf = avpkt->data;
402     int buf_size = avpkt->size;
403     ShortenContext *s = avctx->priv_data;
404     int i, input_buf_size = 0;
405     int16_t *samples = data;
406     int ret;
407
408     /* allocate internal bitstream buffer */
409     if(s->max_framesize == 0){
410         void *tmp_ptr;
411         s->max_framesize= 1024; // should hopefully be enough for the first header
412         tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
413                                   s->max_framesize);
414         if (!tmp_ptr) {
415             av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
416             return AVERROR(ENOMEM);
417         }
418         s->bitstream = tmp_ptr;
419     }
420
421     /* append current packet data to bitstream buffer */
422     if(1 && s->max_framesize){//FIXME truncated
423         buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
424         input_buf_size= buf_size;
425
426         if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
427             memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
428             s->bitstream_index=0;
429         }
430         if (buf)
431             memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
432         buf= &s->bitstream[s->bitstream_index];
433         buf_size += s->bitstream_size;
434         s->bitstream_size= buf_size;
435
436         /* do not decode until buffer has at least max_framesize bytes or
437            the end of the file has been reached */
438         if (buf_size < s->max_framesize && avpkt->data) {
439             *data_size = 0;
440             return input_buf_size;
441         }
442     }
443     /* init and position bitstream reader */
444     init_get_bits(&s->gb, buf, buf_size*8);
445     skip_bits(&s->gb, s->bitindex);
446
447     /* process header or next subblock */
448     if (!s->got_header) {
449         if ((ret = read_header(s)) < 0)
450             return ret;
451         *data_size = 0;
452         goto finish_frame;
453     }
454
455     /* if quit command was read previously, don't decode anything */
456     if (s->got_quit_command) {
457         *data_size = 0;
458         return avpkt->size;
459     }
460
461     s->cur_chan = 0;
462     while (s->cur_chan < s->channels) {
463         int cmd;
464         int len;
465
466         if (get_bits_left(&s->gb) < 3+FNSIZE) {
467             *data_size = 0;
468             break;
469         }
470
471         cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
472
473         if (cmd > FN_VERBATIM) {
474             av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
475             *data_size = 0;
476             break;
477         }
478
479         if (!is_audio_command[cmd]) {
480             /* process non-audio command */
481             switch (cmd) {
482                 case FN_VERBATIM:
483                     len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
484                     while (len--) {
485                         get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
486                     }
487                     break;
488                 case FN_BITSHIFT:
489                     s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
490                     break;
491                 case FN_BLOCKSIZE: {
492                     int blocksize = get_uint(s, av_log2(s->blocksize));
493                     if (blocksize > s->blocksize) {
494                         av_log(avctx, AV_LOG_ERROR, "Increasing block size is not supported\n");
495                         return AVERROR_PATCHWELCOME;
496                     }
497                     if (!blocksize || blocksize > MAX_BLOCKSIZE) {
498                         av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
499                                "block size: %d\n", blocksize);
500                         return AVERROR(EINVAL);
501                     }
502                     s->blocksize = blocksize;
503                     break;
504                 }
505                 case FN_QUIT:
506                     s->got_quit_command = 1;
507                     break;
508             }
509             if (cmd == FN_BLOCKSIZE || cmd == FN_QUIT) {
510                 *data_size = 0;
511                 break;
512             }
513         } else {
514             /* process audio command */
515             int residual_size = 0;
516             int channel = s->cur_chan;
517             int32_t coffset;
518
519             /* get Rice code for residual decoding */
520             if (cmd != FN_ZERO) {
521                 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
522                 /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */
523                 if (s->version == 0)
524                     residual_size--;
525             }
526
527             /* calculate sample offset using means from previous blocks */
528             if (s->nmean == 0)
529                 coffset = s->offset[channel][0];
530             else {
531                 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
532                 for (i=0; i<s->nmean; i++)
533                     sum += s->offset[channel][i];
534                 coffset = sum / s->nmean;
535                 if (s->version >= 2)
536                     coffset >>= FFMIN(1, s->bitshift);
537             }
538
539             /* decode samples for this channel */
540             if (cmd == FN_ZERO) {
541                 for (i=0; i<s->blocksize; i++)
542                     s->decoded[channel][i] = 0;
543             } else {
544                 if ((ret = decode_subframe_lpc(s, cmd, channel, residual_size, coffset)) < 0)
545                     return ret;
546             }
547
548             /* update means with info from the current block */
549             if (s->nmean > 0) {
550                 int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
551                 for (i=0; i<s->blocksize; i++)
552                     sum += s->decoded[channel][i];
553
554                 for (i=1; i<s->nmean; i++)
555                     s->offset[channel][i-1] = s->offset[channel][i];
556
557                 if (s->version < 2)
558                     s->offset[channel][s->nmean - 1] = sum / s->blocksize;
559                 else
560                     s->offset[channel][s->nmean - 1] = (sum / s->blocksize) << s->bitshift;
561             }
562
563             /* copy wrap samples for use with next block */
564             for (i=-s->nwrap; i<0; i++)
565                 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
566
567             /* shift samples to add in unused zero bits which were removed
568                during encoding */
569             fix_bitshift(s, s->decoded[channel]);
570
571             /* if this is the last channel in the block, output the samples */
572             s->cur_chan++;
573             if (s->cur_chan == s->channels) {
574                 int out_size = s->blocksize * s->channels *
575                                av_get_bytes_per_sample(avctx->sample_fmt);
576                 if (*data_size < out_size) {
577                     av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n");
578                     return AVERROR(EINVAL);
579                 }
580                 interleave_buffer(samples, s->channels, s->blocksize, s->decoded);
581                 *data_size = out_size;
582             }
583         }
584     }
585     if (s->cur_chan < s->channels)
586         *data_size = 0;
587
588 finish_frame:
589     s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
590     i= (get_bits_count(&s->gb))/8;
591     if (i > buf_size) {
592         av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
593         s->bitstream_size=0;
594         s->bitstream_index=0;
595         return -1;
596     }
597     if (s->bitstream_size) {
598         s->bitstream_index += i;
599         s->bitstream_size  -= i;
600         return input_buf_size;
601     } else
602         return i;
603 }
604
605 static av_cold int shorten_decode_close(AVCodecContext *avctx)
606 {
607     ShortenContext *s = avctx->priv_data;
608     int i;
609
610     for (i = 0; i < s->channels; i++) {
611         s->decoded[i] -= s->nwrap;
612         av_freep(&s->decoded[i]);
613         av_freep(&s->offset[i]);
614     }
615     av_freep(&s->bitstream);
616     av_freep(&s->coeffs);
617     return 0;
618 }
619
620 AVCodec ff_shorten_decoder = {
621     .name           = "shorten",
622     .type           = AVMEDIA_TYPE_AUDIO,
623     .id             = CODEC_ID_SHORTEN,
624     .priv_data_size = sizeof(ShortenContext),
625     .init           = shorten_decode_init,
626     .close          = shorten_decode_close,
627     .decode         = shorten_decode_frame,
628     .capabilities   = CODEC_CAP_DELAY,
629     .long_name= NULL_IF_CONFIG_SMALL("Shorten"),
630 };