FFmpeg
Loading...
Searching...
No Matches
shorten.c
Go to the documentation of this file.
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#include <limits.h>
29#include "libavutil/mem.h"
30#include "avcodec.h"
31#include "bswapdsp.h"
32#include "bytestream.h"
33#include "codec_internal.h"
34#include "decode.h"
35#include "get_bits.h"
36#include "golomb.h"
37
38#define MAX_CHANNELS 8
39#define MAX_BLOCKSIZE 65535
40
41#define OUT_BUFFER_SIZE 16384
42
43#define ULONGSIZE 2
44
45#define WAVE_FORMAT_PCM 0x0001
46
47#define DEFAULT_BLOCK_SIZE 256
48
49#define TYPESIZE 4
50#define CHANSIZE 0
51#define LPCQSIZE 2
52#define ENERGYSIZE 3
53#define BITSHIFTSIZE 2
54
55#define TYPE_S8 1
56#define TYPE_U8 2
57#define TYPE_S16HL 3
58#define TYPE_U16HL 4
59#define TYPE_S16LH 5
60#define TYPE_U16LH 6
61
62#define NWRAP 3
63#define NSKIPSIZE 1
64
65#define LPCQUANT 5
66#define V2LPCQOFFSET (1 << LPCQUANT)
67
68#define FNSIZE 2
69#define FN_DIFF0 0
70#define FN_DIFF1 1
71#define FN_DIFF2 2
72#define FN_DIFF3 3
73#define FN_QUIT 4
74#define FN_BLOCKSIZE 5
75#define FN_BITSHIFT 6
76#define FN_QLPC 7
77#define FN_ZERO 8
78#define FN_VERBATIM 9
79
80/** indicates if the FN_* command is audio or non-audio */
81static const uint8_t is_audio_command[10] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0 };
82
83#define VERBATIM_CKSIZE_SIZE 5
84#define VERBATIM_BYTE_SIZE 8
85#define CANONICAL_HEADER_SIZE 44
86
118
120{
121 ShortenContext *s = avctx->priv_data;
122 s->avctx = avctx;
123
124 ff_bswapdsp_init(&s->bdsp);
125
126 return 0;
127}
128
130{
131 int i, chan, err;
132
133 for (chan = 0; chan < s->channels; chan++) {
134 if (FFMAX(1, s->nmean) >= UINT_MAX / sizeof(int32_t)) {
135 av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
136 return AVERROR_INVALIDDATA;
137 }
138 if (s->blocksize + (uint64_t)s->nwrap >= UINT_MAX / sizeof(int32_t)) {
139 av_log(s->avctx, AV_LOG_ERROR,
140 "s->blocksize + s->nwrap too large\n");
141 return AVERROR_INVALIDDATA;
142 }
143
144 if ((err = av_reallocp_array(&s->offset[chan],
145 sizeof(int32_t),
146 FFMAX(1, s->nmean))) < 0)
147 return err;
148
149 if ((err = av_reallocp_array(&s->decoded_base[chan], (s->blocksize + s->nwrap),
150 sizeof(s->decoded_base[0][0]))) < 0)
151 return err;
152 for (i = 0; i < s->nwrap; i++)
153 s->decoded_base[chan][i] = 0;
154 s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
155 }
156
157 if ((err = av_reallocp_array(&s->coeffs, s->nwrap, sizeof(*s->coeffs))) < 0)
158 return err;
159
160 return 0;
161}
162
163static inline unsigned int get_uint(ShortenContext *s, int k)
164{
165 if (s->version != 0) {
167 if (k > 31U)
168 return AVERROR_INVALIDDATA;
169 }
170 return get_ur_golomb_shorten(&s->gb, k);
171}
172
174{
175 int i;
176
177 if (s->bitshift == 32) {
178 for (i = 0; i < s->blocksize; i++)
179 buffer[i] = 0;
180 } else if (s->bitshift != 0) {
181 for (i = 0; i < s->blocksize; i++)
182 buffer[i] *= 1U << s->bitshift;
183 }
184}
185
187{
188 int32_t mean = 0;
189 int chan, i;
190 int nblock = FFMAX(1, s->nmean);
191 /* initialise offset */
192 switch (s->internal_ftype) {
193 case TYPE_U8:
194 s->avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
195 mean = 0x80;
196 break;
197 case TYPE_S16HL:
198 case TYPE_S16LH:
199 s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
200 break;
201 default:
202 av_log(s->avctx, AV_LOG_ERROR, "unknown audio type\n");
204 }
205
206 for (chan = 0; chan < s->channels; chan++)
207 for (i = 0; i < nblock; i++)
208 s->offset[chan][i] = mean;
209 return 0;
210}
211
212static int decode_aiff_header(AVCodecContext *avctx, const uint8_t *header,
213 int header_size)
214{
215 ShortenContext *s = avctx->priv_data;
216 int len, bps, exp;
218 uint64_t val;
219 uint32_t tag;
220
221 bytestream2_init(&gb, header, header_size);
222
223 if (bytestream2_get_le32(&gb) != MKTAG('F', 'O', 'R', 'M')) {
224 av_log(avctx, AV_LOG_ERROR, "missing FORM tag\n");
225 return AVERROR_INVALIDDATA;
226 }
227
228 bytestream2_skip(&gb, 4); /* chunk size */
229
230 tag = bytestream2_get_le32(&gb);
231 if (tag != MKTAG('A', 'I', 'F', 'F') &&
232 tag != MKTAG('A', 'I', 'F', 'C')) {
233 av_log(avctx, AV_LOG_ERROR, "missing AIFF tag\n");
234 return AVERROR_INVALIDDATA;
235 }
236
237 while (bytestream2_get_le32(&gb) != MKTAG('C', 'O', 'M', 'M')) {
238 len = bytestream2_get_be32(&gb);
239 if (len < 0 || bytestream2_get_bytes_left(&gb) < 18LL + len + (len&1)) {
240 av_log(avctx, AV_LOG_ERROR, "no COMM chunk found\n");
241 return AVERROR_INVALIDDATA;
242 }
243 bytestream2_skip(&gb, len + (len & 1));
244 }
245 len = bytestream2_get_be32(&gb);
246
247 if (len < 18) {
248 av_log(avctx, AV_LOG_ERROR, "COMM chunk was too short\n");
249 return AVERROR_INVALIDDATA;
250 }
251
252 bytestream2_skip(&gb, 6);
253 bps = bytestream2_get_be16(&gb);
254 avctx->bits_per_coded_sample = bps;
255
256 s->swap = tag == MKTAG('A', 'I', 'F', 'C');
257
258 if (bps != 16 && bps != 8) {
259 av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample: %d\n", bps);
260 return AVERROR(ENOSYS);
261 }
262
263 exp = bytestream2_get_be16(&gb) - 16383 - 63;
264 val = bytestream2_get_be64(&gb);
265 if (exp < -63 || exp > 63) {
266 av_log(avctx, AV_LOG_ERROR, "exp %d is out of range\n", exp);
267 return AVERROR_INVALIDDATA;
268 }
269 if (exp >= 0)
270 avctx->sample_rate = val << exp;
271 else
272 avctx->sample_rate = (val + (1ULL<<(-exp-1))) >> -exp;
273 len -= 18;
274 if (len > 0)
275 av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
276
277 return 0;
278}
279
280static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header,
281 int header_size)
282{
283 int len, bps;
284 uint16_t wave_format;
286
287 bytestream2_init(&gb, header, header_size);
288
289 if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
290 av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
291 return AVERROR_INVALIDDATA;
292 }
293
294 bytestream2_skip(&gb, 4); /* chunk size */
295
296 if (bytestream2_get_le32(&gb) != MKTAG('W', 'A', 'V', 'E')) {
297 av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
298 return AVERROR_INVALIDDATA;
299 }
300
301 while (bytestream2_get_le32(&gb) != MKTAG('f', 'm', 't', ' ')) {
302 len = bytestream2_get_le32(&gb);
303 bytestream2_skip(&gb, len);
304 if (len < 0 || bytestream2_get_bytes_left(&gb) < 16) {
305 av_log(avctx, AV_LOG_ERROR, "no fmt chunk found\n");
306 return AVERROR_INVALIDDATA;
307 }
308 }
309 len = bytestream2_get_le32(&gb);
310
311 if (len < 16) {
312 av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
313 return AVERROR_INVALIDDATA;
314 }
315
316 wave_format = bytestream2_get_le16(&gb);
317
318 switch (wave_format) {
319 case WAVE_FORMAT_PCM:
320 break;
321 default:
322 av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
323 return AVERROR(ENOSYS);
324 }
325
326 bytestream2_skip(&gb, 2); // skip channels (already got from shorten header)
327 avctx->sample_rate = bytestream2_get_le32(&gb);
328 bytestream2_skip(&gb, 4); // skip bit rate (represents original uncompressed bit rate)
329 bytestream2_skip(&gb, 2); // skip block align (not needed)
330 bps = bytestream2_get_le16(&gb);
331 avctx->bits_per_coded_sample = bps;
332
333 if (bps != 16 && bps != 8) {
334 av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample: %d\n", bps);
335 return AVERROR(ENOSYS);
336 }
337
338 len -= 16;
339 if (len > 0)
340 av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
341
342 return 0;
343}
344
345static const int fixed_coeffs[][3] = {
346 { 0, 0, 0 },
347 { 1, 0, 0 },
348 { 2, -1, 0 },
349 { 3, -3, 1 }
350};
351
353 int residual_size, int32_t coffset)
354{
355 int pred_order, sum, qshift, init_sum, i, j;
356 const int *coeffs;
357
358 if (command == FN_QLPC) {
359 /* read/validate prediction order */
360 pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
361 if ((unsigned)pred_order > s->nwrap) {
362 av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
363 pred_order);
364 return AVERROR(EINVAL);
365 }
366 /* read LPC coefficients */
367 for (i = 0; i < pred_order; i++)
368 s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
369 coeffs = s->coeffs;
370
371 qshift = LPCQUANT;
372 } else {
373 /* fixed LPC coeffs */
374 pred_order = command;
375 if (pred_order >= FF_ARRAY_ELEMS(fixed_coeffs)) {
376 av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
377 pred_order);
378 return AVERROR_INVALIDDATA;
379 }
380 coeffs = fixed_coeffs[pred_order];
381 qshift = 0;
382 }
383
384 /* subtract offset from previous samples to use in prediction */
385 if (command == FN_QLPC && coffset)
386 for (i = -pred_order; i < 0; i++)
387 s->decoded[channel][i] -= (unsigned)coffset;
388
389 /* decode residual and do LPC prediction */
390 init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
391 for (i = 0; i < s->blocksize; i++) {
392 sum = init_sum;
393 for (j = 0; j < pred_order; j++)
394 sum += coeffs[j] * (unsigned)s->decoded[channel][i - j - 1];
395 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) +
396 (unsigned)(sum >> qshift);
397 }
398
399 /* add offset to current samples */
400 if (command == FN_QLPC && coffset)
401 for (i = 0; i < s->blocksize; i++)
402 s->decoded[channel][i] += (unsigned)coffset;
403
404 return 0;
405}
406
408{
409 int i, ret;
410 int maxnlpc = 0;
411 /* shorten signature */
412 if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
413 av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
414 return AVERROR_INVALIDDATA;
415 }
416
417 s->lpcqoffset = 0;
418 s->blocksize = DEFAULT_BLOCK_SIZE;
419 s->nmean = -1;
420 s->version = get_bits(&s->gb, 8);
421 s->internal_ftype = get_uint(s, TYPESIZE);
422
423 s->channels = get_uint(s, CHANSIZE);
424 if (!s->channels) {
425 av_log(s->avctx, AV_LOG_ERROR, "No channels reported\n");
426 return AVERROR_INVALIDDATA;
427 }
428 if (s->channels > MAX_CHANNELS) {
429 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
430 s->channels = 0;
431 return AVERROR_INVALIDDATA;
432 }
433 if (s->avctx->ch_layout.nb_channels != s->channels) {
434 av_channel_layout_uninit(&s->avctx->ch_layout);
435 s->avctx->ch_layout.nb_channels = s->channels;
436 s->avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
437 }
438
439 /* get blocksize if version > 0 */
440 if (s->version > 0) {
441 int skip_bytes;
442 unsigned blocksize;
443
444 blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
445 if (!blocksize || blocksize > MAX_BLOCKSIZE) {
446 av_log(s->avctx, AV_LOG_ERROR,
447 "invalid or unsupported block size: %d\n",
448 blocksize);
449 return AVERROR(EINVAL);
450 }
451 s->blocksize = blocksize;
452
453 maxnlpc = get_uint(s, LPCQSIZE);
454 if (maxnlpc > 1024U) {
455 av_log(s->avctx, AV_LOG_ERROR, "maxnlpc is: %d\n", maxnlpc);
456 return AVERROR_INVALIDDATA;
457 }
458 s->nmean = get_uint(s, 0);
459 if (s->nmean > 32768U) {
460 av_log(s->avctx, AV_LOG_ERROR, "nmean is: %d\n", s->nmean);
461 return AVERROR_INVALIDDATA;
462 }
463
465 if ((unsigned)skip_bytes > FFMAX(get_bits_left(&s->gb), 0)/8) {
466 av_log(s->avctx, AV_LOG_ERROR, "invalid skip_bytes: %d\n", skip_bytes);
467 return AVERROR_INVALIDDATA;
468 }
469
470 for (i = 0; i < skip_bytes; i++)
471 skip_bits(&s->gb, 8);
472 }
473 s->nwrap = FFMAX(NWRAP, maxnlpc);
474
475 if (s->version > 1)
476 s->lpcqoffset = V2LPCQOFFSET;
477
478 if (s->avctx->extradata_size > 0)
479 goto end;
480
482 av_log(s->avctx, AV_LOG_ERROR,
483 "missing verbatim section at beginning of stream\n");
484 return AVERROR_INVALIDDATA;
485 }
486
487 s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
488 if (s->header_size >= OUT_BUFFER_SIZE ||
489 s->header_size < CANONICAL_HEADER_SIZE) {
490 av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n",
491 s->header_size);
492 return AVERROR_INVALIDDATA;
493 }
494
495 for (i = 0; i < s->header_size; i++)
496 s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
497
498 if (AV_RL32(s->header) == MKTAG('R','I','F','F')) {
499 if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0)
500 return ret;
501 } else if (AV_RL32(s->header) == MKTAG('F','O','R','M')) {
502 if ((ret = decode_aiff_header(s->avctx, s->header, s->header_size)) < 0)
503 return ret;
504 } else {
505 avpriv_report_missing_feature(s->avctx, "unsupported bit packing %"
506 PRIX32, AV_RL32(s->header));
508 }
509
510end:
511
512 if ((ret = allocate_buffers(s)) < 0)
513 return ret;
514
515 if ((ret = init_offset(s)) < 0)
516 return ret;
517
518 s->cur_chan = 0;
519 s->bitshift = 0;
520
521 s->got_header = 1;
522
523 return 0;
524}
525
527 int *got_frame_ptr, AVPacket *avpkt)
528{
529 const uint8_t *buf = avpkt->data;
530 int buf_size = avpkt->size;
531 ShortenContext *s = avctx->priv_data;
532 int i, input_buf_size = 0;
533 int ret;
534
535 /* allocate internal bitstream buffer */
536 if (s->max_framesize == 0) {
537 void *tmp_ptr;
538 s->max_framesize = 8192; // should hopefully be enough for the first header
539 tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
540 s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE);
541 if (!tmp_ptr) {
542 s->max_framesize = 0;
543 av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
544 return AVERROR(ENOMEM);
545 }
546 memset(tmp_ptr, 0, s->allocated_bitstream_size);
547 s->bitstream = tmp_ptr;
548 }
549
550 /* append current packet data to bitstream buffer */
551 buf_size = FFMIN(buf_size, s->max_framesize - s->bitstream_size);
552 input_buf_size = buf_size;
553
554 if (s->bitstream_index + s->bitstream_size + buf_size + AV_INPUT_BUFFER_PADDING_SIZE >
555 s->allocated_bitstream_size) {
556 memmove(s->bitstream, &s->bitstream[s->bitstream_index],
557 s->bitstream_size);
558 s->bitstream_index = 0;
559 }
560 if (buf)
561 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf,
562 buf_size);
563 buf = &s->bitstream[s->bitstream_index];
564 buf_size += s->bitstream_size;
565 s->bitstream_size = buf_size;
566
567 /* do not decode until buffer has at least max_framesize bytes or
568 * the end of the file has been reached */
569 if (buf_size < s->max_framesize && avpkt->data) {
570 *got_frame_ptr = 0;
571 return input_buf_size;
572 }
573 /* init and position bitstream reader */
574 if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
575 return ret;
576 skip_bits(&s->gb, s->bitindex);
577
578 /* process header or next subblock */
579 if (!s->got_header) {
580
581 if ((ret = read_header(s)) < 0)
582 return ret;
583
584 if (avpkt->size) {
585 int max_framesize = s->blocksize * s->channels * 8;
586 void *tmp_ptr;
587 unsigned old_allocated_bitstream_size = s->allocated_bitstream_size;
588 tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
589 max_framesize + AV_INPUT_BUFFER_PADDING_SIZE);
590 if (!tmp_ptr) {
591 av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
592 return AVERROR(ENOMEM);
593 }
594 s->bitstream = tmp_ptr;
595 if (s->allocated_bitstream_size > old_allocated_bitstream_size)
596 memset(s->bitstream + old_allocated_bitstream_size, 0, s->allocated_bitstream_size - old_allocated_bitstream_size);
597 s->max_framesize = FFMAX(s->max_framesize, max_framesize);
598 *got_frame_ptr = 0;
599 goto finish_frame;
600 }
601 }
602
603 /* if quit command was read previously, don't decode anything */
604 if (s->got_quit_command) {
605 *got_frame_ptr = 0;
606 return avpkt->size;
607 }
608
609 s->cur_chan = 0;
610 while (s->cur_chan < s->channels) {
611 unsigned cmd;
612 int len;
613
614 if (get_bits_left(&s->gb) < 3 + FNSIZE) {
615 *got_frame_ptr = 0;
616 break;
617 }
618
619 cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
620
621 if (cmd > FN_VERBATIM) {
622 av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
623 *got_frame_ptr = 0;
624 break;
625 }
626
627 if (!is_audio_command[cmd]) {
628 /* process non-audio command */
629 switch (cmd) {
630 case FN_VERBATIM:
632 if (len < 0 || len > get_bits_left(&s->gb)) {
633 av_log(avctx, AV_LOG_ERROR, "verbatim length %d invalid\n",
634 len);
635 return AVERROR_INVALIDDATA;
636 }
637 while (len--)
639 break;
640 case FN_BITSHIFT: {
641 unsigned bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
642 if (bitshift > 32) {
643 av_log(avctx, AV_LOG_ERROR, "bitshift %d is invalid\n",
644 bitshift);
645 return AVERROR_INVALIDDATA;
646 }
647 s->bitshift = bitshift;
648 break;
649 }
650 case FN_BLOCKSIZE: {
651 unsigned blocksize = get_uint(s, av_log2(s->blocksize));
652 if (blocksize > s->blocksize) {
654 "Increasing block size");
656 }
657 if (!blocksize || blocksize > MAX_BLOCKSIZE) {
658 av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
659 "block size: %d\n", blocksize);
660 return AVERROR(EINVAL);
661 }
662 s->blocksize = blocksize;
663 break;
664 }
665 case FN_QUIT:
666 s->got_quit_command = 1;
667 break;
668 }
669 if (cmd == FN_QUIT)
670 break;
671 } else {
672 /* process audio command */
673 int residual_size = 0;
674 int channel = s->cur_chan;
675 int32_t coffset;
676
677 /* get Rice code for residual decoding */
678 if (cmd != FN_ZERO) {
679 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
680 /* This is a hack as version 0 differed in the definition
681 * of get_sr_golomb_shorten(). */
682 if (s->version == 0)
683 residual_size--;
684 if (residual_size > 30U) {
685 av_log(avctx, AV_LOG_ERROR, "residual size unsupportd: %d\n", residual_size);
686 return AVERROR_INVALIDDATA;
687 }
688 }
689
690 /* calculate sample offset using means from previous blocks */
691 if (s->nmean == 0)
692 coffset = s->offset[channel][0];
693 else {
694 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
695 for (i = 0; i < s->nmean; i++)
696 sum += (unsigned)s->offset[channel][i];
697 coffset = sum / s->nmean;
698 if (s->version >= 2)
699 coffset = s->bitshift == 0 ? coffset : coffset >> s->bitshift - 1 >> 1;
700 }
701
702 /* decode samples for this channel */
703 if (cmd == FN_ZERO) {
704 for (i = 0; i < s->blocksize; i++)
705 s->decoded[channel][i] = 0;
706 } else {
707 if ((ret = decode_subframe_lpc(s, cmd, channel,
708 residual_size, coffset)) < 0)
709 return ret;
710 }
711
712 /* update means with info from the current block */
713 if (s->nmean > 0) {
714 int64_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
715 for (i = 0; i < s->blocksize; i++)
716 sum += s->decoded[channel][i];
717
718 for (i = 1; i < s->nmean; i++)
719 s->offset[channel][i - 1] = s->offset[channel][i];
720
721 if (s->version < 2)
722 s->offset[channel][s->nmean - 1] = sum / s->blocksize;
723 else
724 s->offset[channel][s->nmean - 1] = s->bitshift == 32 ? 0 : (sum / s->blocksize) * (1LL << s->bitshift);
725 }
726
727 /* copy wrap samples for use with next block */
728 for (i = -s->nwrap; i < 0; i++)
729 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
730
731 /* shift samples to add in unused zero bits which were removed
732 * during encoding */
733 fix_bitshift(s, s->decoded[channel]);
734
735 /* if this is the last channel in the block, output the samples */
736 s->cur_chan++;
737 if (s->cur_chan == s->channels) {
738 uint8_t *samples_u8;
739 int16_t *samples_s16;
740 int chan;
741
742 /* get output buffer */
743 frame->nb_samples = s->blocksize;
744 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
745 return ret;
746
747 for (chan = 0; chan < s->channels; chan++) {
748 samples_u8 = ((uint8_t **)frame->extended_data)[chan];
749 samples_s16 = ((int16_t **)frame->extended_data)[chan];
750 for (i = 0; i < s->blocksize; i++) {
751 switch (s->internal_ftype) {
752 case TYPE_U8:
753 *samples_u8++ = av_clip_uint8(s->decoded[chan][i]);
754 break;
755 case TYPE_S16HL:
756 case TYPE_S16LH:
757 *samples_s16++ = av_clip_int16(s->decoded[chan][i]);
758 break;
759 }
760 }
761 if (s->swap && s->internal_ftype != TYPE_U8)
762 s->bdsp.bswap16_buf(((uint16_t **)frame->extended_data)[chan],
763 ((uint16_t **)frame->extended_data)[chan],
764 s->blocksize);
765
766 }
767
768 *got_frame_ptr = 1;
769 }
770 }
771 }
772 if (s->cur_chan < s->channels)
773 *got_frame_ptr = 0;
774
776 s->bitindex = get_bits_count(&s->gb) - 8 * (get_bits_count(&s->gb) / 8);
777 i = get_bits_count(&s->gb) / 8;
778 if (i > buf_size) {
779 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
780 s->bitstream_size = 0;
781 s->bitstream_index = 0;
782 return AVERROR_INVALIDDATA;
783 }
784 if (s->bitstream_size) {
785 s->bitstream_index += i;
786 s->bitstream_size -= i;
787 return input_buf_size;
788 } else
789 return i;
790}
791
793{
794 ShortenContext *s = avctx->priv_data;
795 int i;
796
797 for (i = 0; i < s->channels; i++) {
798 s->decoded[i] = NULL;
799 av_freep(&s->decoded_base[i]);
800 av_freep(&s->offset[i]);
801 }
802 av_freep(&s->bitstream);
803 av_freep(&s->coeffs);
804
805 return 0;
806}
807
809 .p.name = "shorten",
810 CODEC_LONG_NAME("Shorten"),
811 .p.type = AVMEDIA_TYPE_AUDIO,
812 .p.id = AV_CODEC_ID_SHORTEN,
813 .priv_data_size = sizeof(ShortenContext),
817 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
820};
#define MAX_CHANNELS
Definition aac.h:33
static double val(void *priv, double ch)
Definition aeval.c:77
const FFCodec ff_shorten_decoder
Definition shorten.c:808
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
int32_t
Libavcodec external API header.
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
static av_unused const uint8_t * skip_bytes(CABACContext *c, int n)
Skip n bytes and reset the decoder.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define av_clip_int16
Definition common.h:115
#define av_clip_uint8
Definition common.h:106
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition ebur128.h:39
int8_t exp
Definition eval.c:76
bitstream reader API header.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition get_bits.h:424
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
exp golomb vlc stuff
static int get_sr_golomb_shorten(GetBitContext *gb, int k)
read signed golomb rice code (shorten).
Definition golomb.h:562
static unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k)
read unsigned golomb rice code (shorten).
Definition golomb.h:554
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_SHORTEN
Definition codec_id.h:468
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition mem.c:495
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate an array through a pointer to a pointer.
Definition mem.c:225
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition samplefmt.h:64
@ AV_SAMPLE_FMT_U8P
unsigned 8 bits, planar
Definition samplefmt.h:63
#define av_log2
Definition intmath.h:84
#define AV_RL32(p)
#define AV_RB32(p)
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition bswapdsp.c:37
#define MAX_BLOCKSIZE
Definition diracdec.c:56
#define av_cold
Definition attributes.h:117
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
uint32_t tag
Definition movenc.c:2073
unsigned bps
Definition movenc.c:2074
static int finish_frame(AVCodecContext *avctx, AVFrame *pict)
Definition rv34.c:1590
static const uint8_t header[24]
Definition sdr2.c:68
#define OUT_BUFFER_SIZE
Definition shorten.c:41
#define V2LPCQOFFSET
Definition shorten.c:66
#define FN_ZERO
Definition shorten.c:77
static int decode_aiff_header(AVCodecContext *avctx, const uint8_t *header, int header_size)
Definition shorten.c:212
#define FN_BITSHIFT
Definition shorten.c:75
#define ENERGYSIZE
Definition shorten.c:52
#define LPCQUANT
Definition shorten.c:65
static int decode_subframe_lpc(ShortenContext *s, int command, int channel, int residual_size, int32_t coffset)
Definition shorten.c:352
#define FN_BLOCKSIZE
Definition shorten.c:74
#define ULONGSIZE
Definition shorten.c:43
static av_cold int shorten_decode_init(AVCodecContext *avctx)
Definition shorten.c:119
#define TYPE_S16HL
Definition shorten.c:57
static int init_offset(ShortenContext *s)
Definition shorten.c:186
#define BITSHIFTSIZE
Definition shorten.c:53
#define TYPESIZE
Definition shorten.c:49
#define VERBATIM_BYTE_SIZE
Definition shorten.c:84
#define FN_VERBATIM
Definition shorten.c:78
#define FNSIZE
Definition shorten.c:68
#define TYPE_U8
Definition shorten.c:56
#define VERBATIM_CKSIZE_SIZE
Definition shorten.c:83
static int read_header(ShortenContext *s)
Definition shorten.c:407
static unsigned int get_uint(ShortenContext *s, int k)
Definition shorten.c:163
static int shorten_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition shorten.c:526
#define CHANSIZE
Definition shorten.c:50
static int allocate_buffers(ShortenContext *s)
Definition shorten.c:129
static const int fixed_coeffs[][3]
Definition shorten.c:345
#define NSKIPSIZE
Definition shorten.c:63
#define CANONICAL_HEADER_SIZE
Definition shorten.c:85
#define WAVE_FORMAT_PCM
Definition shorten.c:45
#define FN_QLPC
Definition shorten.c:76
#define DEFAULT_BLOCK_SIZE
Definition shorten.c:47
#define MAX_CHANNELS
Definition shorten.c:38
#define FN_QUIT
Definition shorten.c:73
static const uint8_t is_audio_command[10]
indicates if the FN_* command is audio or non-audio
Definition shorten.c:81
#define TYPE_S16LH
Definition shorten.c:59
static void fix_bitshift(ShortenContext *s, int32_t *buffer)
Definition shorten.c:173
static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header, int header_size)
Definition shorten.c:280
static av_cold int shorten_decode_close(AVCodecContext *avctx)
Definition shorten.c:792
#define NWRAP
Definition shorten.c:62
#define LPCQSIZE
Definition shorten.c:51
#define FF_ARRAY_ELEMS(a)
main external API structure.
Definition avcodec.h:443
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition avcodec.h:1564
int sample_rate
samples per second
Definition avcodec.h:1040
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
int32_t * decoded[MAX_CHANNELS]
Definition shorten.c:94
AVCodecContext * avctx
Definition shorten.c:88
int32_t * offset[MAX_CHANNELS]
Definition shorten.c:96
int got_quit_command
Definition shorten.c:114
uint8_t header[OUT_BUFFER_SIZE]
Definition shorten.c:103
uint8_t * bitstream
Definition shorten.c:98
GetBitContext gb
Definition shorten.c:89
int bitstream_size
Definition shorten.c:99
int internal_ftype
Definition shorten.c:108
unsigned channels
Definition shorten.c:92
int max_framesize
Definition shorten.c:91
int bitstream_index
Definition shorten.c:100
int32_t * decoded_base[MAX_CHANNELS]
Definition shorten.c:95
int min_framesize
Definition shorten.c:91
int * coeffs
Definition shorten.c:97
unsigned int allocated_bitstream_size
Definition shorten.c:101
int32_t lpcqoffset
Definition shorten.c:112
BswapDSPContext bdsp
Definition shorten.c:116
#define av_freep(p)
#define av_log(a,...)
static char buffer[20]
Definition seek.c:32
static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
static float mean(const float *input, int size)
Definition vf_nnedi.c:861
int len