X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fshorten.c;h=1dc010f441d57de1f2f97b2ee9cca53fbd56c103;hb=dda20a6e2c0c3d7985a93c60f5897975549d53b0;hp=5ffd634e245fa8dcb1257db87d02f5edf56f9dcc;hpb=069ada46c1445babdec543210b93d98f37ef618e;p=ffmpeg diff --git a/libavcodec/shorten.c b/libavcodec/shorten.c index 5ffd634e245..1dc010f441d 100644 --- a/libavcodec/shorten.c +++ b/libavcodec/shorten.c @@ -31,6 +31,7 @@ #include "bytestream.h" #include "get_bits.h" #include "golomb.h" +#include "internal.h" #define MAX_CHANNELS 8 #define MAX_BLOCKSIZE 65535 @@ -79,12 +80,14 @@ static const uint8_t is_audio_command[10] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0 }; typedef struct ShortenContext { AVCodecContext *avctx; + AVFrame frame; GetBitContext gb; int min_framesize, max_framesize; int channels; int32_t *decoded[MAX_CHANNELS]; + int32_t *decoded_base[MAX_CHANNELS]; int32_t *offset[MAX_CHANNELS]; int *coeffs; uint8_t *bitstream; @@ -102,13 +105,18 @@ typedef struct ShortenContext { int blocksize; int bitindex; int32_t lpcqoffset; + int got_header; + int got_quit_command; } ShortenContext; static av_cold int shorten_decode_init(AVCodecContext * avctx) { ShortenContext *s = avctx->priv_data; s->avctx = avctx; - avctx->sample_fmt = AV_SAMPLE_FMT_S16; + avctx->sample_fmt = AV_SAMPLE_FMT_S16P; + + avcodec_get_frame_defaults(&s->frame); + avctx->coded_frame = &s->frame; return 0; } @@ -134,13 +142,14 @@ static int allocate_buffers(ShortenContext *s) return AVERROR(ENOMEM); s->offset[chan] = tmp_ptr; - tmp_ptr = av_realloc(s->decoded[chan], sizeof(int32_t)*(s->blocksize + s->nwrap)); + tmp_ptr = av_realloc(s->decoded_base[chan], (s->blocksize + s->nwrap) * + sizeof(s->decoded_base[0][0])); if (!tmp_ptr) return AVERROR(ENOMEM); - s->decoded[chan] = tmp_ptr; + s->decoded_base[chan] = tmp_ptr; for (i=0; inwrap; i++) - s->decoded[chan][i] = 0; - s->decoded[chan] += s->nwrap; + s->decoded_base[chan][i] = 0; + s->decoded[chan] = s->decoded_base[chan] + s->nwrap; } coeffs = av_realloc(s->coeffs, s->nwrap * sizeof(*s->coeffs)); @@ -170,7 +179,7 @@ static void fix_bitshift(ShortenContext *s, int32_t *buffer) } -static void init_offset(ShortenContext *s) +static int init_offset(ShortenContext *s) { int32_t mean = 0; int chan, i; @@ -184,12 +193,13 @@ static void init_offset(ShortenContext *s) break; default: av_log(s->avctx, AV_LOG_ERROR, "unknown audio type"); - abort(); + return AVERROR_INVALIDDATA; } for (chan = 0; chan < s->channels; chan++) for (i = 0; i < nblock; i++) s->offset[chan][i] = mean; + return 0; } static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header, @@ -250,12 +260,16 @@ static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header, return 0; } -static int16_t * interleave_buffer(int16_t *samples, int nchan, int blocksize, int32_t **buffer) { - int i, chan; - for (i=0; ilpcqoffset = 0; s->blocksize = DEFAULT_BLOCK_SIZE; - s->channels = 1; s->nmean = -1; s->version = get_bits(&s->gb, 8); s->internal_ftype = get_uint(s, TYPESIZE); s->channels = get_uint(s, CHANSIZE); - if (s->channels > MAX_CHANNELS) { + if (s->channels <= 0 || s->channels > MAX_CHANNELS) { av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels); return -1; } @@ -361,7 +374,8 @@ static int read_header(ShortenContext *s) if ((ret = allocate_buffers(s)) < 0) return ret; - init_offset(s); + if ((ret = init_offset(s)) < 0) + return ret; if (s->version > 1) s->lpcqoffset = V2LPCQOFFSET; @@ -386,18 +400,18 @@ static int read_header(ShortenContext *s) s->cur_chan = 0; s->bitshift = 0; + s->got_header = 1; + return 0; } -static int shorten_decode_frame(AVCodecContext *avctx, - void *data, int *data_size, - AVPacket *avpkt) +static int shorten_decode_frame(AVCodecContext *avctx, void *data, + int *got_frame_ptr, AVPacket *avpkt) { const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; ShortenContext *s = avctx->priv_data; int i, input_buf_size = 0; - int16_t *samples = data; int ret; /* allocate internal bitstream buffer */ @@ -422,14 +436,16 @@ static int shorten_decode_frame(AVCodecContext *avctx, memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size); s->bitstream_index=0; } - memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size); + if (buf) + memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size); buf= &s->bitstream[s->bitstream_index]; buf_size += s->bitstream_size; s->bitstream_size= buf_size; - /* do not decode until buffer has at least max_framesize bytes */ - if(buf_size < s->max_framesize){ - *data_size = 0; + /* do not decode until buffer has at least max_framesize bytes or + the end of the file has been reached */ + if (buf_size < s->max_framesize && avpkt->data) { + *got_frame_ptr = 0; return input_buf_size; } } @@ -438,25 +454,35 @@ static int shorten_decode_frame(AVCodecContext *avctx, skip_bits(&s->gb, s->bitindex); /* process header or next subblock */ - if (!s->blocksize) - { + if (!s->got_header) { if ((ret = read_header(s)) < 0) return ret; - *data_size = 0; + *got_frame_ptr = 0; + goto finish_frame; } - else - { + + /* if quit command was read previously, don't decode anything */ + if (s->got_quit_command) { + *got_frame_ptr = 0; + return avpkt->size; + } + + s->cur_chan = 0; + while (s->cur_chan < s->channels) { int cmd; int len; + + if (get_bits_left(&s->gb) < 3+FNSIZE) { + *got_frame_ptr = 0; + break; + } + cmd = get_ur_golomb_shorten(&s->gb, FNSIZE); if (cmd > FN_VERBATIM) { av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd); - if (s->bitstream_size > 0) { - s->bitstream_index++; - s->bitstream_size--; - } - return -1; + *got_frame_ptr = 0; + break; } if (!is_audio_command[cmd]) { @@ -486,9 +512,13 @@ static int shorten_decode_frame(AVCodecContext *avctx, break; } case FN_QUIT: + s->got_quit_command = 1; break; } - *data_size = 0; + if (cmd == FN_BLOCKSIZE || cmd == FN_QUIT) { + *got_frame_ptr = 0; + break; + } } else { /* process audio command */ int residual_size = 0; @@ -498,7 +528,8 @@ static int shorten_decode_frame(AVCodecContext *avctx, /* get Rice code for residual decoding */ if (cmd != FN_ZERO) { residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE); - /* this is a hack as version 0 differed in defintion of get_sr_golomb_shorten */ + /* This is a hack as version 0 differed in the definition + * of get_sr_golomb_shorten(). */ if (s->version == 0) residual_size--; } @@ -550,15 +581,25 @@ static int shorten_decode_frame(AVCodecContext *avctx, /* if this is the last channel in the block, output the samples */ s->cur_chan++; if (s->cur_chan == s->channels) { - samples = interleave_buffer(samples, s->channels, s->blocksize, s->decoded); - s->cur_chan = 0; - *data_size = (int8_t *)samples - (int8_t *)data; - } else { - *data_size = 0; + /* get output buffer */ + s->frame.nb_samples = s->blocksize; + if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) { + av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + return ret; + } + /* interleave output */ + output_buffer((int16_t **)s->frame.extended_data, s->channels, + s->blocksize, s->decoded); + + *got_frame_ptr = 1; + *(AVFrame *)data = s->frame; } } } + if (s->cur_chan < s->channels) + *got_frame_ptr = 0; +finish_frame: s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8); i= (get_bits_count(&s->gb))/8; if (i > buf_size) { @@ -581,22 +622,26 @@ static av_cold int shorten_decode_close(AVCodecContext *avctx) int i; for (i = 0; i < s->channels; i++) { - s->decoded[i] -= s->nwrap; - av_freep(&s->decoded[i]); + s->decoded[i] = NULL; + av_freep(&s->decoded_base[i]); av_freep(&s->offset[i]); } av_freep(&s->bitstream); av_freep(&s->coeffs); + return 0; } AVCodec ff_shorten_decoder = { .name = "shorten", .type = AVMEDIA_TYPE_AUDIO, - .id = CODEC_ID_SHORTEN, + .id = AV_CODEC_ID_SHORTEN, .priv_data_size = sizeof(ShortenContext), .init = shorten_decode_init, .close = shorten_decode_close, .decode = shorten_decode_frame, - .long_name= NULL_IF_CONFIG_SMALL("Shorten"), + .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1, + .long_name = NULL_IF_CONFIG_SMALL("Shorten"), + .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P, + AV_SAMPLE_FMT_NONE }, };