3 * Copyright (c) 2005 Jeff Muizelaar
5 * This file is part of FFmpeg.
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.
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.
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
25 * @author Jeff Muizelaar
31 #include "bytestream.h"
32 #include "codec_internal.h"
37 #define MAX_CHANNELS 8
38 #define MAX_BLOCKSIZE 65535
40 #define OUT_BUFFER_SIZE 16384
44 #define WAVE_FORMAT_PCM 0x0001
46 #define DEFAULT_BLOCK_SIZE 256
52 #define BITSHIFTSIZE 2
65 #define V2LPCQOFFSET (1 << LPCQUANT)
73 #define FN_BLOCKSIZE 5
79 /** indicates if the FN_* command is audio or non-audio */
80 static const uint8_t is_audio_command
[10] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0 };
82 #define VERBATIM_CKSIZE_SIZE 5
83 #define VERBATIM_BYTE_SIZE 8
84 #define CANONICAL_HEADER_SIZE 44
86 typedef struct ShortenContext
{
87 AVCodecContext
*avctx
;
90 int min_framesize
, max_framesize
;
93 int32_t *decoded
[MAX_CHANNELS
];
94 int32_t *decoded_base
[MAX_CHANNELS
];
95 int32_t *offset
[MAX_CHANNELS
];
100 unsigned int allocated_bitstream_size
;
102 uint8_t header
[OUT_BUFFER_SIZE
];
113 int got_quit_command
;
115 BswapDSPContext bdsp
;
118 static av_cold
int shorten_decode_init(AVCodecContext
*avctx
)
120 ShortenContext
*s
= avctx
->priv_data
;
123 ff_bswapdsp_init(&s
->bdsp
);
128 static int allocate_buffers(ShortenContext
*s
)
132 for (chan
= 0; chan
< s
->channels
; chan
++) {
133 if (FFMAX(1, s
->nmean
) >= UINT_MAX
/ sizeof(int32_t)) {
134 av_log(s
->avctx
, AV_LOG_ERROR
, "nmean too large\n");
135 return AVERROR_INVALIDDATA
;
137 if (s
->blocksize
+ (uint64_t)s
->nwrap
>= UINT_MAX
/ sizeof(int32_t)) {
138 av_log(s
->avctx
, AV_LOG_ERROR
,
139 "s->blocksize + s->nwrap too large\n");
140 return AVERROR_INVALIDDATA
;
143 if ((err
= av_reallocp_array(&s
->offset
[chan
],
145 FFMAX(1, s
->nmean
))) < 0)
148 if ((err
= av_reallocp_array(&s
->decoded_base
[chan
], (s
->blocksize
+ s
->nwrap
),
149 sizeof(s
->decoded_base
[0][0]))) < 0)
151 for (i
= 0; i
< s
->nwrap
; i
++)
152 s
->decoded_base
[chan
][i
] = 0;
153 s
->decoded
[chan
] = s
->decoded_base
[chan
] + s
->nwrap
;
156 if ((err
= av_reallocp_array(&s
->coeffs
, s
->nwrap
, sizeof(*s
->coeffs
))) < 0)
162 static inline unsigned int get_uint(ShortenContext
*s
, int k
)
164 if (s
->version
!= 0) {
165 k
= get_ur_golomb_shorten(&s
->gb
, ULONGSIZE
);
167 return AVERROR_INVALIDDATA
;
169 return get_ur_golomb_shorten(&s
->gb
, k
);
172 static void fix_bitshift(ShortenContext
*s
, int32_t *buffer
)
176 if (s
->bitshift
== 32) {
177 for (i
= 0; i
< s
->blocksize
; i
++)
179 } else if (s
->bitshift
!= 0) {
180 for (i
= 0; i
< s
->blocksize
; i
++)
181 buffer
[i
] *= 1U << s
->bitshift
;
185 static int init_offset(ShortenContext
*s
)
189 int nblock
= FFMAX(1, s
->nmean
);
190 /* initialise offset */
191 switch (s
->internal_ftype
) {
193 s
->avctx
->sample_fmt
= AV_SAMPLE_FMT_U8P
;
198 s
->avctx
->sample_fmt
= AV_SAMPLE_FMT_S16P
;
201 av_log(s
->avctx
, AV_LOG_ERROR
, "unknown audio type\n");
202 return AVERROR_PATCHWELCOME
;
205 for (chan
= 0; chan
< s
->channels
; chan
++)
206 for (i
= 0; i
< nblock
; i
++)
207 s
->offset
[chan
][i
] = mean
;
211 static int decode_aiff_header(AVCodecContext
*avctx
, const uint8_t *header
,
214 ShortenContext
*s
= avctx
->priv_data
;
220 bytestream2_init(&gb
, header
, header_size
);
222 if (bytestream2_get_le32(&gb
) != MKTAG('F', 'O', 'R', 'M')) {
223 av_log(avctx
, AV_LOG_ERROR
, "missing FORM tag\n");
224 return AVERROR_INVALIDDATA
;
227 bytestream2_skip(&gb
, 4); /* chunk size */
229 tag
= bytestream2_get_le32(&gb
);
230 if (tag
!= MKTAG('A', 'I', 'F', 'F') &&
231 tag
!= MKTAG('A', 'I', 'F', 'C')) {
232 av_log(avctx
, AV_LOG_ERROR
, "missing AIFF tag\n");
233 return AVERROR_INVALIDDATA
;
236 while (bytestream2_get_le32(&gb
) != MKTAG('C', 'O', 'M', 'M')) {
237 len
= bytestream2_get_be32(&gb
);
238 if (len
< 0 || bytestream2_get_bytes_left(&gb
) < 18LL + len
+ (len
&1)) {
239 av_log(avctx
, AV_LOG_ERROR
, "no COMM chunk found\n");
240 return AVERROR_INVALIDDATA
;
242 bytestream2_skip(&gb
, len
+ (len
& 1));
244 len
= bytestream2_get_be32(&gb
);
247 av_log(avctx
, AV_LOG_ERROR
, "COMM chunk was too short\n");
248 return AVERROR_INVALIDDATA
;
251 bytestream2_skip(&gb
, 6);
252 bps
= bytestream2_get_be16(&gb
);
253 avctx
->bits_per_coded_sample
= bps
;
255 s
->swap
= tag
== MKTAG('A', 'I', 'F', 'C');
257 if (bps
!= 16 && bps
!= 8) {
258 av_log(avctx
, AV_LOG_ERROR
, "unsupported number of bits per sample: %d\n", bps
);
259 return AVERROR(ENOSYS
);
262 exp
= bytestream2_get_be16(&gb
) - 16383 - 63;
263 val
= bytestream2_get_be64(&gb
);
264 if (exp
< -63 || exp
> 63) {
265 av_log(avctx
, AV_LOG_ERROR
, "exp %d is out of range\n", exp
);
266 return AVERROR_INVALIDDATA
;
269 avctx
->sample_rate
= val
<< exp
;
271 avctx
->sample_rate
= (val
+ (1ULL<<(-exp
-1))) >> -exp
;
274 av_log(avctx
, AV_LOG_INFO
, "%d header bytes unparsed\n", len
);
279 static int decode_wave_header(AVCodecContext
*avctx
, const uint8_t *header
,
286 bytestream2_init(&gb
, header
, header_size
);
288 if (bytestream2_get_le32(&gb
) != MKTAG('R', 'I', 'F', 'F')) {
289 av_log(avctx
, AV_LOG_ERROR
, "missing RIFF tag\n");
290 return AVERROR_INVALIDDATA
;
293 bytestream2_skip(&gb
, 4); /* chunk size */
295 if (bytestream2_get_le32(&gb
) != MKTAG('W', 'A', 'V', 'E')) {
296 av_log(avctx
, AV_LOG_ERROR
, "missing WAVE tag\n");
297 return AVERROR_INVALIDDATA
;
300 while (bytestream2_get_le32(&gb
) != MKTAG('f', 'm', 't', ' ')) {
301 len
= bytestream2_get_le32(&gb
);
302 bytestream2_skip(&gb
, len
);
303 if (len
< 0 || bytestream2_get_bytes_left(&gb
) < 16) {
304 av_log(avctx
, AV_LOG_ERROR
, "no fmt chunk found\n");
305 return AVERROR_INVALIDDATA
;
308 len
= bytestream2_get_le32(&gb
);
311 av_log(avctx
, AV_LOG_ERROR
, "fmt chunk was too short\n");
312 return AVERROR_INVALIDDATA
;
315 wave_format
= bytestream2_get_le16(&gb
);
317 switch (wave_format
) {
318 case WAVE_FORMAT_PCM
:
321 av_log(avctx
, AV_LOG_ERROR
, "unsupported wave format\n");
322 return AVERROR(ENOSYS
);
325 bytestream2_skip(&gb
, 2); // skip channels (already got from shorten header)
326 avctx
->sample_rate
= bytestream2_get_le32(&gb
);
327 bytestream2_skip(&gb
, 4); // skip bit rate (represents original uncompressed bit rate)
328 bytestream2_skip(&gb
, 2); // skip block align (not needed)
329 bps
= bytestream2_get_le16(&gb
);
330 avctx
->bits_per_coded_sample
= bps
;
332 if (bps
!= 16 && bps
!= 8) {
333 av_log(avctx
, AV_LOG_ERROR
, "unsupported number of bits per sample: %d\n", bps
);
334 return AVERROR(ENOSYS
);
339 av_log(avctx
, AV_LOG_INFO
, "%d header bytes unparsed\n", len
);
344 static const int fixed_coeffs
[][3] = {
351 static int decode_subframe_lpc(ShortenContext
*s
, int command
, int channel
,
352 int residual_size
, int32_t coffset
)
354 int pred_order
, sum
, qshift
, init_sum
, i
, j
;
357 if (command
== FN_QLPC
) {
358 /* read/validate prediction order */
359 pred_order
= get_ur_golomb_shorten(&s
->gb
, LPCQSIZE
);
360 if ((unsigned)pred_order
> s
->nwrap
) {
361 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid pred_order %d\n",
363 return AVERROR(EINVAL
);
365 /* read LPC coefficients */
366 for (i
= 0; i
< pred_order
; i
++)
367 s
->coeffs
[i
] = get_sr_golomb_shorten(&s
->gb
, LPCQUANT
);
372 /* fixed LPC coeffs */
373 pred_order
= command
;
374 if (pred_order
>= FF_ARRAY_ELEMS(fixed_coeffs
)) {
375 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid pred_order %d\n",
377 return AVERROR_INVALIDDATA
;
379 coeffs
= fixed_coeffs
[pred_order
];
383 /* subtract offset from previous samples to use in prediction */
384 if (command
== FN_QLPC
&& coffset
)
385 for (i
= -pred_order
; i
< 0; i
++)
386 s
->decoded
[channel
][i
] -= (unsigned)coffset
;
388 /* decode residual and do LPC prediction */
389 init_sum
= pred_order
? (command
== FN_QLPC
? s
->lpcqoffset
: 0) : coffset
;
390 for (i
= 0; i
< s
->blocksize
; i
++) {
392 for (j
= 0; j
< pred_order
; j
++)
393 sum
+= coeffs
[j
] * (unsigned)s
->decoded
[channel
][i
- j
- 1];
394 s
->decoded
[channel
][i
] = get_sr_golomb_shorten(&s
->gb
, residual_size
) +
395 (unsigned)(sum
>> qshift
);
398 /* add offset to current samples */
399 if (command
== FN_QLPC
&& coffset
)
400 for (i
= 0; i
< s
->blocksize
; i
++)
401 s
->decoded
[channel
][i
] += (unsigned)coffset
;
406 static int read_header(ShortenContext
*s
)
410 /* shorten signature */
411 if (get_bits_long(&s
->gb
, 32) != AV_RB32("ajkg")) {
412 av_log(s
->avctx
, AV_LOG_ERROR
, "missing shorten magic 'ajkg'\n");
413 return AVERROR_INVALIDDATA
;
417 s
->blocksize
= DEFAULT_BLOCK_SIZE
;
419 s
->version
= get_bits(&s
->gb
, 8);
420 s
->internal_ftype
= get_uint(s
, TYPESIZE
);
422 s
->channels
= get_uint(s
, CHANSIZE
);
424 av_log(s
->avctx
, AV_LOG_ERROR
, "No channels reported\n");
425 return AVERROR_INVALIDDATA
;
427 if (s
->channels
> MAX_CHANNELS
) {
428 av_log(s
->avctx
, AV_LOG_ERROR
, "too many channels: %d\n", s
->channels
);
430 return AVERROR_INVALIDDATA
;
432 if (s
->avctx
->ch_layout
.nb_channels
!= s
->channels
) {
433 av_channel_layout_uninit(&s
->avctx
->ch_layout
);
434 s
->avctx
->ch_layout
.nb_channels
= s
->channels
;
435 s
->avctx
->ch_layout
.order
= AV_CHANNEL_ORDER_UNSPEC
;
438 /* get blocksize if version > 0 */
439 if (s
->version
> 0) {
443 blocksize
= get_uint(s
, av_log2(DEFAULT_BLOCK_SIZE
));
444 if (!blocksize
|| blocksize
> MAX_BLOCKSIZE
) {
445 av_log(s
->avctx
, AV_LOG_ERROR
,
446 "invalid or unsupported block size: %d\n",
448 return AVERROR(EINVAL
);
450 s
->blocksize
= blocksize
;
452 maxnlpc
= get_uint(s
, LPCQSIZE
);
453 if (maxnlpc
> 1024U) {
454 av_log(s
->avctx
, AV_LOG_ERROR
, "maxnlpc is: %d\n", maxnlpc
);
455 return AVERROR_INVALIDDATA
;
457 s
->nmean
= get_uint(s
, 0);
458 if (s
->nmean
> 32768U) {
459 av_log(s
->avctx
, AV_LOG_ERROR
, "nmean is: %d\n", s
->nmean
);
460 return AVERROR_INVALIDDATA
;
463 skip_bytes
= get_uint(s
, NSKIPSIZE
);
464 if ((unsigned)skip_bytes
> FFMAX(get_bits_left(&s
->gb
), 0)/8) {
465 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid skip_bytes: %d\n", skip_bytes
);
466 return AVERROR_INVALIDDATA
;
469 for (i
= 0; i
< skip_bytes
; i
++)
470 skip_bits(&s
->gb
, 8);
472 s
->nwrap
= FFMAX(NWRAP
, maxnlpc
);
475 s
->lpcqoffset
= V2LPCQOFFSET
;
477 if (s
->avctx
->extradata_size
> 0)
480 if (get_ur_golomb_shorten(&s
->gb
, FNSIZE
) != FN_VERBATIM
) {
481 av_log(s
->avctx
, AV_LOG_ERROR
,
482 "missing verbatim section at beginning of stream\n");
483 return AVERROR_INVALIDDATA
;
486 s
->header_size
= get_ur_golomb_shorten(&s
->gb
, VERBATIM_CKSIZE_SIZE
);
487 if (s
->header_size
>= OUT_BUFFER_SIZE
||
488 s
->header_size
< CANONICAL_HEADER_SIZE
) {
489 av_log(s
->avctx
, AV_LOG_ERROR
, "header is wrong size: %d\n",
491 return AVERROR_INVALIDDATA
;
494 for (i
= 0; i
< s
->header_size
; i
++)
495 s
->header
[i
] = (char)get_ur_golomb_shorten(&s
->gb
, VERBATIM_BYTE_SIZE
);
497 if (AV_RL32(s
->header
) == MKTAG('R','I','F','F')) {
498 if ((ret
= decode_wave_header(s
->avctx
, s
->header
, s
->header_size
)) < 0)
500 } else if (AV_RL32(s
->header
) == MKTAG('F','O','R','M')) {
501 if ((ret
= decode_aiff_header(s
->avctx
, s
->header
, s
->header_size
)) < 0)
504 avpriv_report_missing_feature(s
->avctx
, "unsupported bit packing %"
505 PRIX32
, AV_RL32(s
->header
));
506 return AVERROR_PATCHWELCOME
;
511 if ((ret
= allocate_buffers(s
)) < 0)
514 if ((ret
= init_offset(s
)) < 0)
525 static int shorten_decode_frame(AVCodecContext
*avctx
, AVFrame
*frame
,
526 int *got_frame_ptr
, AVPacket
*avpkt
)
528 const uint8_t *buf
= avpkt
->data
;
529 int buf_size
= avpkt
->size
;
530 ShortenContext
*s
= avctx
->priv_data
;
531 int i
, input_buf_size
= 0;
534 /* allocate internal bitstream buffer */
535 if (s
->max_framesize
== 0) {
537 s
->max_framesize
= 8192; // should hopefully be enough for the first header
538 tmp_ptr
= av_fast_realloc(s
->bitstream
, &s
->allocated_bitstream_size
,
539 s
->max_framesize
+ AV_INPUT_BUFFER_PADDING_SIZE
);
541 s
->max_framesize
= 0;
542 av_log(avctx
, AV_LOG_ERROR
, "error allocating bitstream buffer\n");
543 return AVERROR(ENOMEM
);
545 memset(tmp_ptr
, 0, s
->allocated_bitstream_size
);
546 s
->bitstream
= tmp_ptr
;
549 /* append current packet data to bitstream buffer */
550 buf_size
= FFMIN(buf_size
, s
->max_framesize
- s
->bitstream_size
);
551 input_buf_size
= buf_size
;
553 if (s
->bitstream_index
+ s
->bitstream_size
+ buf_size
+ AV_INPUT_BUFFER_PADDING_SIZE
>
554 s
->allocated_bitstream_size
) {
555 memmove(s
->bitstream
, &s
->bitstream
[s
->bitstream_index
],
557 s
->bitstream_index
= 0;
560 memcpy(&s
->bitstream
[s
->bitstream_index
+ s
->bitstream_size
], buf
,
562 buf
= &s
->bitstream
[s
->bitstream_index
];
563 buf_size
+= s
->bitstream_size
;
564 s
->bitstream_size
= buf_size
;
565 memset(buf
+ buf_size
, 0, AV_INPUT_BUFFER_PADDING_SIZE
);
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
) {
571 return input_buf_size
;
573 /* init and position bitstream reader */
574 if ((ret
= init_get_bits8(&s
->gb
, buf
, buf_size
)) < 0)
576 skip_bits(&s
->gb
, s
->bitindex
);
578 /* process header or next subblock */
579 if (!s
->got_header
) {
581 if ((ret
= read_header(s
)) < 0)
588 max_framesize
= FFMAX(s
->max_framesize
, s
->blocksize
* s
->channels
* 8);
589 tmp_ptr
= av_fast_realloc(s
->bitstream
, &s
->allocated_bitstream_size
,
590 max_framesize
+ AV_INPUT_BUFFER_PADDING_SIZE
);
592 av_log(avctx
, AV_LOG_ERROR
, "error allocating bitstream buffer\n");
593 return AVERROR(ENOMEM
);
595 s
->bitstream
= tmp_ptr
;
596 s
->max_framesize
= max_framesize
;
602 /* if quit command was read previously, don't decode anything */
603 if (s
->got_quit_command
) {
609 while (s
->cur_chan
< s
->channels
) {
613 if (get_bits_left(&s
->gb
) < 3 + FNSIZE
) {
618 cmd
= get_ur_golomb_shorten(&s
->gb
, FNSIZE
);
620 if (cmd
> FN_VERBATIM
) {
621 av_log(avctx
, AV_LOG_ERROR
, "unknown shorten function %d\n", cmd
);
626 if (!is_audio_command
[cmd
]) {
627 /* process non-audio command */
630 len
= get_ur_golomb_shorten(&s
->gb
, VERBATIM_CKSIZE_SIZE
);
631 if (len
< 0 || len
> get_bits_left(&s
->gb
)) {
632 av_log(avctx
, AV_LOG_ERROR
, "verbatim length %d invalid\n",
634 return AVERROR_INVALIDDATA
;
637 get_ur_golomb_shorten(&s
->gb
, VERBATIM_BYTE_SIZE
);
640 unsigned bitshift
= get_ur_golomb_shorten(&s
->gb
, BITSHIFTSIZE
);
642 av_log(avctx
, AV_LOG_ERROR
, "bitshift %d is invalid\n",
644 return AVERROR_INVALIDDATA
;
646 s
->bitshift
= bitshift
;
650 unsigned blocksize
= get_uint(s
, av_log2(s
->blocksize
));
651 if (blocksize
> s
->blocksize
) {
652 avpriv_report_missing_feature(avctx
,
653 "Increasing block size");
654 return AVERROR_PATCHWELCOME
;
656 if (!blocksize
|| blocksize
> MAX_BLOCKSIZE
) {
657 av_log(avctx
, AV_LOG_ERROR
, "invalid or unsupported "
658 "block size: %d\n", blocksize
);
659 return AVERROR(EINVAL
);
661 s
->blocksize
= blocksize
;
665 s
->got_quit_command
= 1;
671 /* process audio command */
672 int residual_size
= 0;
673 int channel
= s
->cur_chan
;
676 /* get Rice code for residual decoding */
677 if (cmd
!= FN_ZERO
) {
678 residual_size
= get_ur_golomb_shorten(&s
->gb
, ENERGYSIZE
);
679 /* This is a hack as version 0 differed in the definition
680 * of get_sr_golomb_shorten(). */
683 if (residual_size
> 30U) {
684 av_log(avctx
, AV_LOG_ERROR
, "residual size unsupportd: %d\n", residual_size
);
685 return AVERROR_INVALIDDATA
;
689 /* calculate sample offset using means from previous blocks */
691 coffset
= s
->offset
[channel
][0];
693 int32_t sum
= (s
->version
< 2) ? 0 : s
->nmean
/ 2;
694 for (i
= 0; i
< s
->nmean
; i
++)
695 sum
+= (unsigned)s
->offset
[channel
][i
];
696 coffset
= sum
/ s
->nmean
;
698 coffset
= s
->bitshift
== 0 ? coffset
: coffset
>> s
->bitshift
- 1 >> 1;
701 /* decode samples for this channel */
702 if (cmd
== FN_ZERO
) {
703 for (i
= 0; i
< s
->blocksize
; i
++)
704 s
->decoded
[channel
][i
] = 0;
706 if ((ret
= decode_subframe_lpc(s
, cmd
, channel
,
707 residual_size
, coffset
)) < 0)
711 /* update means with info from the current block */
713 int64_t sum
= (s
->version
< 2) ? 0 : s
->blocksize
/ 2;
714 for (i
= 0; i
< s
->blocksize
; i
++)
715 sum
+= s
->decoded
[channel
][i
];
717 for (i
= 1; i
< s
->nmean
; i
++)
718 s
->offset
[channel
][i
- 1] = s
->offset
[channel
][i
];
721 s
->offset
[channel
][s
->nmean
- 1] = sum
/ s
->blocksize
;
723 s
->offset
[channel
][s
->nmean
- 1] = s
->bitshift
== 32 ? 0 : (sum
/ s
->blocksize
) * (1LL << s
->bitshift
);
726 /* copy wrap samples for use with next block */
727 for (i
= -s
->nwrap
; i
< 0; i
++)
728 s
->decoded
[channel
][i
] = s
->decoded
[channel
][i
+ s
->blocksize
];
730 /* shift samples to add in unused zero bits which were removed
732 fix_bitshift(s
, s
->decoded
[channel
]);
734 /* if this is the last channel in the block, output the samples */
736 if (s
->cur_chan
== s
->channels
) {
738 int16_t *samples_s16
;
741 /* get output buffer */
742 frame
->nb_samples
= s
->blocksize
;
743 if ((ret
= ff_get_buffer(avctx
, frame
, 0)) < 0)
746 for (chan
= 0; chan
< s
->channels
; chan
++) {
747 samples_u8
= ((uint8_t **)frame
->extended_data
)[chan
];
748 samples_s16
= ((int16_t **)frame
->extended_data
)[chan
];
749 for (i
= 0; i
< s
->blocksize
; i
++) {
750 switch (s
->internal_ftype
) {
752 *samples_u8
++ = av_clip_uint8(s
->decoded
[chan
][i
]);
756 *samples_s16
++ = av_clip_int16(s
->decoded
[chan
][i
]);
760 if (s
->swap
&& s
->internal_ftype
!= TYPE_U8
)
761 s
->bdsp
.bswap16_buf(((uint16_t **)frame
->extended_data
)[chan
],
762 ((uint16_t **)frame
->extended_data
)[chan
],
771 if (s
->cur_chan
< s
->channels
)
775 s
->bitindex
= get_bits_count(&s
->gb
) - 8 * (get_bits_count(&s
->gb
) / 8);
776 i
= get_bits_count(&s
->gb
) / 8;
778 av_log(s
->avctx
, AV_LOG_ERROR
, "overread: %d\n", i
- buf_size
);
779 s
->bitstream_size
= 0;
780 s
->bitstream_index
= 0;
781 return AVERROR_INVALIDDATA
;
783 if (s
->bitstream_size
) {
784 s
->bitstream_index
+= i
;
785 s
->bitstream_size
-= i
;
786 return input_buf_size
;
791 static av_cold
int shorten_decode_close(AVCodecContext
*avctx
)
793 ShortenContext
*s
= avctx
->priv_data
;
796 for (i
= 0; i
< s
->channels
; i
++) {
797 s
->decoded
[i
] = NULL
;
798 av_freep(&s
->decoded_base
[i
]);
799 av_freep(&s
->offset
[i
]);
801 av_freep(&s
->bitstream
);
802 av_freep(&s
->coeffs
);
807 const FFCodec ff_shorten_decoder
= {
809 .p
.long_name
= NULL_IF_CONFIG_SMALL("Shorten"),
810 .p
.type
= AVMEDIA_TYPE_AUDIO
,
811 .p
.id
= AV_CODEC_ID_SHORTEN
,
812 .priv_data_size
= sizeof(ShortenContext
),
813 .init
= shorten_decode_init
,
814 .close
= shorten_decode_close
,
815 FF_CODEC_DECODE_CB(shorten_decode_frame
),
816 .p
.capabilities
= AV_CODEC_CAP_CHANNEL_CONF
|
819 AV_CODEC_CAP_SUBFRAMES
,
820 .p
.sample_fmts
= (const enum AVSampleFormat
[]) { AV_SAMPLE_FMT_S16P
,
822 AV_SAMPLE_FMT_NONE
},
823 .caps_internal
= FF_CODEC_CAP_INIT_THREADSAFE
,