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
35
36 #define MAX_CHANNELS 8
37 #define MAX_BLOCKSIZE 65535
38
39 #define OUT_BUFFER_SIZE 16384
40
42
43 #define WAVE_FORMAT_PCM 0x0001
44
45 #define DEFAULT_BLOCK_SIZE 256
46
51 #define BITSHIFTSIZE 2
52
59
62
64 #define V2LPCQOFFSET (1 << LPCQUANT)
65
72 #define FN_BLOCKSIZE 5
77
78 /** indicates if the FN_* command is audio or non-audio */
80
81 #define VERBATIM_CKSIZE_SIZE 5
82 #define VERBATIM_BYTE_SIZE 8
83 #define CANONICAL_HEADER_SIZE 44
84
88
91
114
116 {
119
120 return 0;
121 }
122
124 {
125 int i, chan, err;
126
127 for (chan = 0; chan < s->
channels; chan++) {
131 }
135 "s->blocksize + s->nwrap too large\n");
137 }
138
142 return err;
143
146 return err;
147 for (i = 0; i < s->
nwrap; i++)
150 }
151
153 return err;
154
155 return 0;
156 }
157
159 {
163 }
164
166 {
167 int i;
168
172 }
173
175 {
177 int chan, i;
179 /* initialise offset */
183 mean = 0x80;
184 break;
188 break;
189 default:
192 }
193
194 for (chan = 0; chan < s->
channels; chan++)
195 for (i = 0; i < nblock; i++)
196 s->
offset[chan][i] = mean;
197 return 0;
198 }
199
201 int header_size)
202 {
204 short wave_format;
206
208
209 if (bytestream2_get_le32(&gb) !=
MKTAG(
'R',
'I',
'F',
'F')) {
212 }
213
215
216 if (bytestream2_get_le32(&gb) !=
MKTAG(
'W',
'A',
'V',
'E')) {
219 }
220
221 while (bytestream2_get_le32(&gb) !=
MKTAG(
'f',
'm',
't',
' ')) {
222 len = bytestream2_get_le32(&gb);
227 }
228 }
229 len = bytestream2_get_le32(&gb);
230
231 if (len < 16) {
234 }
235
236 wave_format = bytestream2_get_le16(&gb);
237
238 switch (wave_format) {
240 break;
241 default:
244 }
245
246 bytestream2_skip(&gb, 2);
// skip channels (already got from shorten header)
248 bytestream2_skip(&gb, 4);
// skip bit rate (represents original uncompressed bit rate)
250 bps = bytestream2_get_le16(&gb);
252
253 if (bps != 16 && bps != 8) {
256 }
257
258 len -= 16;
259 if (len > 0)
261
262 return 0;
263 }
264
266 { 0, 0, 0 },
267 { 1, 0, 0 },
268 { 2, -1, 0 },
269 { 3, -3, 1 }
270 };
271
273 int residual_size,
int32_t coffset)
274 {
275 int pred_order, sum, qshift, init_sum, i, j;
276 const int *coeffs;
277
279 /* read/validate prediction order */
281 if (pred_order > s->
nwrap) {
283 pred_order);
285 }
286 /* read LPC coefficients */
287 for (i = 0; i < pred_order; i++)
290
292 } else {
293 /* fixed LPC coeffs */
297 pred_order);
299 }
301 qshift = 0;
302 }
303
304 /* subtract offset from previous samples to use in prediction */
305 if (command ==
FN_QLPC && coffset)
306 for (i = -pred_order; i < 0; i++)
307 s->
decoded[channel][i] -= coffset;
308
309 /* decode residual and do LPC prediction */
312 sum = init_sum;
313 for (j = 0; j < pred_order; j++)
314 sum += coeffs[j] * s->
decoded[channel][i - j - 1];
316 (sum >> qshift);
317 }
318
319 /* add offset to current samples */
320 if (command ==
FN_QLPC && coffset)
322 s->
decoded[channel][i] += coffset;
323
324 return 0;
325 }
326
328 {
330 int maxnlpc = 0;
331 /* shorten signature */
335 }
336
342
347 }
352 }
354
355 /* get blocksize if version > 0 */
358 unsigned blocksize;
359
363 "invalid or unsupported block size: %d\n",
364 blocksize);
366 }
368
371
375 }
377
380
383
386
389 "missing verbatim section at beginning of stream\n");
391 }
392
399 }
400
403
406
409
411
412 return 0;
413 }
414
416 int *got_frame_ptr,
AVPacket *avpkt)
417 {
420 int buf_size = avpkt->
size;
422 int i, input_buf_size = 0;
424
425 /* allocate internal bitstream buffer */
427 void *tmp_ptr;
428 s->
max_framesize = 8192;
// should hopefully be enough for the first header
431 if (!tmp_ptr) {
434 }
437 }
438
439 /* append current packet data to bitstream buffer */
442 input_buf_size = buf_size;
443
449 }
450 if (buf)
452 buf_size);
456
457 /* do not decode until buffer has at least max_framesize bytes or
458 * the end of the file has been reached */
459 if (buf_size < s->max_framesize && avpkt->
data) {
460 *got_frame_ptr = 0;
461 return input_buf_size;
462 }
463 }
464 /* init and position bitstream reader */
467
468 /* process header or next subblock */
472 *got_frame_ptr = 0;
474 }
475
476 /* if quit command was read previously, don't decode anything */
478 *got_frame_ptr = 0;
480 }
481
484 unsigned cmd;
486
488 *got_frame_ptr = 0;
489 break;
490 }
491
493
496 *got_frame_ptr = 0;
497 break;
498 }
499
501 /* process non-audio command */
502 switch (cmd) {
505 while (len--)
507 break;
510 if (bitshift > 31) {
512 bitshift);
514 }
516 break;
517 }
522 "Increasing block size is not supported\n");
524 }
527 "block size: %d\n", blocksize);
529 }
531 break;
532 }
535 break;
536 }
538 *got_frame_ptr = 0;
539 break;
540 }
541 } else {
542 /* process audio command */
543 int residual_size = 0;
546
547 /* get Rice code for residual decoding */
550 /* This is a hack as version 0 differed in the definition
551 * of get_sr_golomb_shorten(). */
553 residual_size--;
554 }
555
556 /* calculate sample offset using means from previous blocks */
558 coffset = s->
offset[channel][0];
559 else {
562 sum += s->
offset[channel][i];
563 coffset = sum / s->
nmean;
566 }
567
568 /* decode samples for this channel */
572 } else {
574 residual_size, coffset)) < 0)
575 return ret;
576 }
577
578 /* update means with info from the current block */
583
586
589 else
591 }
592
593 /* copy wrap samples for use with next block */
594 for (i = -s->
nwrap; i < 0; i++)
596
597 /* shift samples to add in unused zero bits which were removed
598 * during encoding */
600
601 /* if this is the last channel in the block, output the samples */
605 int16_t *samples_s16;
606 int chan;
607
608 /* get output buffer */
612
613 for (chan = 0; chan < s->
channels; chan++) {
619 *samples_u8++ = av_clip_uint8(s->
decoded[chan][i]);
620 break;
623 *samples_s16++ = av_clip_int16(s->
decoded[chan][i]);
624 break;
625 }
626 }
627 }
628
629 *got_frame_ptr = 1;
630 }
631 }
632 }
634 *got_frame_ptr = 0;
635
639 if (i > buf_size) {
644 }
648 return input_buf_size;
649 } else
650 return i;
651 }
652
654 {
656 int i;
657
662 }
665
666 return 0;
667 }
668
682 };