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