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 }
134 "s->blocksize + s->nwrap too large\n");
136 }
137
141 return err;
142
145 return err;
146 for (i = 0; i < s->
nwrap; i++)
149 }
150
152 return err;
153
154 return 0;
155 }
156
158 {
162 }
163
165 {
166 int i;
167
171 }
172
174 {
176 int chan, i;
178 /* initialise offset */
182 mean = 0x80;
183 break;
187 break;
188 default:
191 }
192
193 for (chan = 0; chan < s->
channels; chan++)
194 for (i = 0; i < nblock; i++)
195 s->
offset[chan][i] = mean;
196 return 0;
197 }
198
200 int header_size)
201 {
203 short wave_format;
205
207
208 if (bytestream2_get_le32(&gb) !=
MKTAG(
'R',
'I',
'F',
'F')) {
211 }
212
214
215 if (bytestream2_get_le32(&gb) !=
MKTAG(
'W',
'A',
'V',
'E')) {
218 }
219
220 while (bytestream2_get_le32(&gb) !=
MKTAG(
'f',
'm',
't',
' ')) {
221 len = bytestream2_get_le32(&gb);
226 }
227 }
228 len = bytestream2_get_le32(&gb);
229
230 if (len < 16) {
233 }
234
235 wave_format = bytestream2_get_le16(&gb);
236
237 switch (wave_format) {
239 break;
240 default:
243 }
244
245 bytestream2_skip(&gb, 2);
// skip channels (already got from shorten header)
247 bytestream2_skip(&gb, 4);
// skip bit rate (represents original uncompressed bit rate)
249 bps = bytestream2_get_le16(&gb);
251
252 if (bps != 16 && bps != 8) {
255 }
256
257 len -= 16;
258 if (len > 0)
260
261 return 0;
262 }
263
265 { 0, 0, 0 },
266 { 1, 0, 0 },
267 { 2, -1, 0 },
268 { 3, -3, 1 }
269 };
270
272 int residual_size,
int32_t coffset)
273 {
274 int pred_order, sum, qshift, init_sum, i, j;
276
278 /* read/validate prediction order */
280 if ((
unsigned)pred_order > s->
nwrap) {
282 pred_order);
284 }
285 /* read LPC coefficients */
286 for (i = 0; i < pred_order; i++)
289
291 } else {
292 /* fixed LPC coeffs */
296 pred_order);
298 }
300 qshift = 0;
301 }
302
303 /* subtract offset from previous samples to use in prediction */
304 if (command ==
FN_QLPC && coffset)
305 for (i = -pred_order; i < 0; i++)
306 s->
decoded[channel][i] -= coffset;
307
308 /* decode residual and do LPC prediction */
311 sum = init_sum;
312 for (j = 0; j < pred_order; j++)
313 sum += coeffs[j] * s->
decoded[channel][i - j - 1];
315 (sum >> qshift);
316 }
317
318 /* add offset to current samples */
319 if (command ==
FN_QLPC && coffset)
321 s->
decoded[channel][i] += coffset;
322
323 return 0;
324 }
325
327 {
328 int i, ret;
329 int maxnlpc = 0;
330 /* shorten signature */
334 }
335
341
346 }
351 }
353
354 /* get blocksize if version > 0 */
357 unsigned blocksize;
358
362 "invalid or unsupported block size: %d\n",
363 blocksize);
365 }
367
370
375 }
376
379 }
381
383 return ret;
384
386 return ret;
387
390
393 "missing verbatim section at beginning of stream\n");
395 }
396
403 }
404
407
409 return ret;
410
413
415
416 return 0;
417 }
418
420 int *got_frame_ptr,
AVPacket *avpkt)
421 {
424 int buf_size = avpkt->
size;
426 int i, input_buf_size = 0;
427 int ret;
428
429 /* allocate internal bitstream buffer */
431 void *tmp_ptr;
432 s->
max_framesize = 8192;
// should hopefully be enough for the first header
435 if (!tmp_ptr) {
438 }
441 }
442
443 /* append current packet data to bitstream buffer */
446 input_buf_size = buf_size;
447
453 }
454 if (buf)
456 buf_size);
460
461 /* do not decode until buffer has at least max_framesize bytes or
462 * the end of the file has been reached */
463 if (buf_size < s->max_framesize && avpkt->
data) {
464 *got_frame_ptr = 0;
465 return input_buf_size;
466 }
467 }
468 /* init and position bitstream reader */
470 return ret;
472
473 /* process header or next subblock */
476 return ret;
477 *got_frame_ptr = 0;
479 }
480
481 /* if quit command was read previously, don't decode anything */
483 *got_frame_ptr = 0;
485 }
486
489 unsigned cmd;
491
493 *got_frame_ptr = 0;
494 break;
495 }
496
498
501 *got_frame_ptr = 0;
502 break;
503 }
504
506 /* process non-audio command */
507 switch (cmd) {
510 while (len--)
512 break;
515 if (bitshift > 31) {
517 bitshift);
519 }
521 break;
522 }
527 "Increasing block size is not supported\n");
529 }
532 "block size: %d\n", blocksize);
534 }
536 break;
537 }
540 break;
541 }
543 *got_frame_ptr = 0;
544 break;
545 }
546 } else {
547 /* process audio command */
548 int residual_size = 0;
551
552 /* get Rice code for residual decoding */
555 /* This is a hack as version 0 differed in the definition
556 * of get_sr_golomb_shorten(). */
558 residual_size--;
559 }
560
561 /* calculate sample offset using means from previous blocks */
563 coffset = s->
offset[channel][0];
564 else {
567 sum += s->
offset[channel][i];
568 coffset = sum / s->
nmean;
571 }
572
573 /* decode samples for this channel */
577 } else {
579 residual_size, coffset)) < 0)
580 return ret;
581 }
582
583 /* update means with info from the current block */
588
591
594 else
596 }
597
598 /* copy wrap samples for use with next block */
599 for (i = -s->
nwrap; i < 0; i++)
601
602 /* shift samples to add in unused zero bits which were removed
603 * during encoding */
605
606 /* if this is the last channel in the block, output the samples */
610 int16_t *samples_s16;
611 int chan;
612
613 /* get output buffer */
616 return ret;
617
618 for (chan = 0; chan < s->
channels; chan++) {
624 *samples_u8++ = av_clip_uint8(s->
decoded[chan][i]);
625 break;
628 *samples_s16++ = av_clip_int16(s->
decoded[chan][i]);
629 break;
630 }
631 }
632 }
633
634 *got_frame_ptr = 1;
635 }
636 }
637 }
639 *got_frame_ptr = 0;
640
644 if (i > buf_size) {
649 }
653 return input_buf_size;
654 } else
655 return i;
656 }
657
659 {
661 int i;
662
667 }
670
671 return 0;
672 }
673
687 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
#define VERBATIM_CKSIZE_SIZE
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static int init_offset(ShortenContext *s)
static av_cold int init(AVCodecContext *avctx)
unsigned int allocated_bitstream_size
int32_t * decoded[MAX_CHANNELS]
static const int fixed_coeffs[][3]
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
static int finish_frame(AVCodecContext *avctx, AVFrame *pict)
AVCodec ff_shorten_decoder
static av_cold int shorten_decode_init(AVCodecContext *avctx)
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
enum AVSampleFormat sample_fmt
audio sample format
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header, int header_size)
static int get_bits_count(const GetBitContext *s)
bitstream reader API header.
static const uint8_t header[24]
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
static av_unused const uint8_t * skip_bytes(CABACContext *c, int n)
Skip n bytes and reset the decoder.
int32_t * decoded_base[MAX_CHANNELS]
static int get_bits_left(GetBitContext *gb)
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
const char * name
Name of the codec implementation.
static int allocate_buffers(ShortenContext *s)
static void fix_bitshift(ShortenContext *s, int32_t *buffer)
#define VERBATIM_BYTE_SIZE
#define FF_ARRAY_ELEMS(a)
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AV_LOG_INFO
Standard information.
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
int sample_rate
samples per second
uint8_t header[OUT_BUFFER_SIZE]
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
#define CANONICAL_HEADER_SIZE
main external API structure.
static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
static av_cold int shorten_decode_close(AVCodecContext *avctx)
static void skip_bits(GetBitContext *s, int n)
static int decode_subframe_lpc(ShortenContext *s, int command, int channel, int residual_size, int32_t coffset)
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static unsigned int get_uint(ShortenContext *s, int k)
#define DEFAULT_BLOCK_SIZE
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
static int shorten_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
common internal api header.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
int32_t * offset[MAX_CHANNELS]
static const int16_t coeffs[]
int channels
number of audio channels
static enum AVSampleFormat sample_fmts[]
static const uint8_t is_audio_command[10]
indicates if the FN_* command is audio or non-audio
static int read_header(ShortenContext *s)
uint8_t ** extended_data
pointers to the data planes/channels.
static unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k)
read unsigned golomb rice code (shorten).
#define MKTAG(a, b, c, d)
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
static int get_sr_golomb_shorten(GetBitContext *gb, int k)
read signed golomb rice code (shorten).