X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fwmalosslessdec.c;h=2f341c01c45ced018a042448a461a2b7209256d7;hb=3d7c84747d4b68f3929c98a6e09efea8e53634dc;hp=8300b171844774ea7ee665d2506a897f49fce6a0;hpb=4a969030e4d10f3f07fa52436ed3d3c6689694e0;p=ffmpeg diff --git a/libavcodec/wmalosslessdec.c b/libavcodec/wmalosslessdec.c index 8300b171844..2f341c01c45 100644 --- a/libavcodec/wmalosslessdec.c +++ b/libavcodec/wmalosslessdec.c @@ -23,6 +23,8 @@ */ #include "libavutil/attributes.h" +#include "libavutil/avassert.h" + #include "avcodec.h" #include "internal.h" #include "get_bits.h" @@ -64,7 +66,7 @@ typedef struct { typedef struct WmallDecodeCtx { /* generic decoder variables */ AVCodecContext *avctx; - AVFrame frame; + AVFrame *frame; uint8_t frame_data[MAX_FRAMESIZE + FF_INPUT_BUFFER_PADDING_SIZE]; ///< compressed frame data PutBitContext pb; ///< context for filling the frame_data buffer @@ -127,8 +129,8 @@ typedef struct WmallDecodeCtx { int8_t mclms_scaling; int16_t mclms_coeffs[128]; int16_t mclms_coeffs_cur[4]; - int16_t mclms_prevvalues[64]; - int16_t mclms_updates[64]; + int16_t mclms_prevvalues[WMALL_MAX_CHANNELS * 2 * 32]; + int16_t mclms_updates[WMALL_MAX_CHANNELS * 2 * 32]; int mclms_recent; int movave_scaling; @@ -158,14 +160,14 @@ typedef struct WmallDecodeCtx { int ave_sum[2]; - int channel_residues[2][2048]; + int channel_residues[2][WMALL_BLOCK_MAX_SIZE]; int lpc_coefs[2][40]; int lpc_order; int lpc_scaling; int lpc_intbits; - int channel_coeffs[2][2048]; + int channel_coeffs[2][WMALL_BLOCK_MAX_SIZE]; } WmallDecodeCtx; @@ -184,10 +186,10 @@ static av_cold int decode_init(AVCodecContext *avctx) channel_mask = AV_RL32(edata_ptr + 2); s->bits_per_sample = AV_RL16(edata_ptr); if (s->bits_per_sample == 16) - avctx->sample_fmt = AV_SAMPLE_FMT_S16; + avctx->sample_fmt = AV_SAMPLE_FMT_S16P; else if (s->bits_per_sample == 24) { - avctx->sample_fmt = AV_SAMPLE_FMT_S32; - av_log_missing_feature(avctx, "bit-depth higher than 16", 0); + avctx->sample_fmt = AV_SAMPLE_FMT_S32P; + avpriv_report_missing_feature(avctx, "Bit-depth higher than 16"); return AVERROR_PATCHWELCOME; } else { av_log(avctx, AV_LOG_ERROR, "Unknown bit-depth: %d\n", @@ -200,8 +202,8 @@ static av_cold int decode_init(AVCodecContext *avctx) av_dlog(avctx, "\n"); } else { - av_log_ask_for_sample(avctx, "Unsupported extradata size\n"); - return AVERROR_INVALIDDATA; + avpriv_request_sample(avctx, "Unsupported extradata size"); + return AVERROR_PATCHWELCOME; } /* generic init */ @@ -215,6 +217,7 @@ static av_cold int decode_init(AVCodecContext *avctx) /* get frame len */ s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate, 3, s->decode_flags); + av_assert0(s->samples_per_frame <= WMALL_BLOCK_MAX_SIZE); /* init previous block len */ for (i = 0; i < avctx->channels; i++) @@ -253,12 +256,15 @@ static av_cold int decode_init(AVCodecContext *avctx) s->num_channels); return AVERROR_INVALIDDATA; } else if (s->num_channels > WMALL_MAX_CHANNELS) { - av_log_ask_for_sample(avctx, "unsupported number of channels\n"); + avpriv_request_sample(avctx, + "More than %d channels", WMALL_MAX_CHANNELS); return AVERROR_PATCHWELCOME; } - avcodec_get_frame_defaults(&s->frame); - avctx->coded_frame = &s->frame; + s->frame = av_frame_alloc(); + if (!s->frame) + return AVERROR(ENOMEM); + avctx->channel_layout = channel_mask; return 0; } @@ -403,7 +409,8 @@ static void decode_ac_filter(WmallDecodeCtx *s) s->acfilter_scaling = get_bits(&s->gb, 4); for (i = 0; i < s->acfilter_order; i++) - s->acfilter_coeffs[i] = get_bits(&s->gb, s->acfilter_scaling) + 1; + s->acfilter_coeffs[i] = (s->acfilter_scaling ? + get_bits(&s->gb, s->acfilter_scaling) : 0) + 1; } static void decode_mclms(WmallDecodeCtx *s) @@ -516,7 +523,7 @@ static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size) residue = quo; else { rem_bits = av_ceil_log2(ave_mean); - rem = rem_bits ? get_bits(&s->gb, rem_bits) : 0; + rem = rem_bits ? get_bits_long(&s->gb, rem_bits) : 0; residue = (quo << rem_bits) + rem; } @@ -881,7 +888,7 @@ static int decode_subframe(WmallDecodeCtx *s) s->do_arith_coding = get_bits1(&s->gb); if (s->do_arith_coding) { - av_log_missing_feature(s->avctx, "arithmetic coding", 1); + avpriv_request_sample(s->avctx, "Arithmetic coding"); return AVERROR_PATCHWELCOME; } s->do_ac_filter = get_bits1(&s->gb); @@ -903,7 +910,7 @@ static int decode_subframe(WmallDecodeCtx *s) } else if (!s->cdlms[0][0].order) { av_log(s->avctx, AV_LOG_DEBUG, "Waiting for seekable tile\n"); - s->frame.nb_samples = 0; + av_frame_unref(s->frame); return -1; } @@ -921,8 +928,8 @@ static int decode_subframe(WmallDecodeCtx *s) s->do_lpc = get_bits1(&s->gb); if (s->do_lpc) { decode_lpc(s); - av_log_ask_for_sample(s->avctx, "Inverse LPC filter not " - "implemented. Expect wrong output.\n"); + avpriv_request_sample(s->avctx, "Expect wrong output since " + "inverse LPC filter"); } } else s->do_lpc = 0; @@ -980,11 +987,9 @@ static int decode_subframe(WmallDecodeCtx *s) for (j = 0; j < subframe_len; j++) { if (s->bits_per_sample == 16) { - *s->samples_16[c] = (int16_t) s->channel_residues[c][j] << padding_zeroes; - s->samples_16[c] += s->num_channels; + *s->samples_16[c]++ = (int16_t) s->channel_residues[c][j] << padding_zeroes; } else { - *s->samples_32[c] = s->channel_residues[c][j] << padding_zeroes; - s->samples_32[c] += s->num_channels; + *s->samples_32[c]++ = s->channel_residues[c][j] << padding_zeroes; } } } @@ -1012,8 +1017,8 @@ static int decode_frame(WmallDecodeCtx *s) GetBitContext* gb = &s->gb; int more_frames = 0, len = 0, i, ret; - s->frame.nb_samples = s->samples_per_frame; - if ((ret = s->avctx->get_buffer(s->avctx, &s->frame)) < 0) { + s->frame->nb_samples = s->samples_per_frame; + if ((ret = ff_get_buffer(s->avctx, s->frame, 0)) < 0) { /* return an error if no frame could be decoded at all */ av_log(s->avctx, AV_LOG_ERROR, "not enough space for the output samples\n"); @@ -1021,8 +1026,8 @@ static int decode_frame(WmallDecodeCtx *s) return ret; } for (i = 0; i < s->num_channels; i++) { - s->samples_16[i] = (int16_t *)s->frame.data[0] + i; - s->samples_32[i] = (int32_t *)s->frame.data[0] + i; + s->samples_16[i] = (int16_t *)s->frame->extended_data[i]; + s->samples_32[i] = (int32_t *)s->frame->extended_data[i]; } /* get frame length */ @@ -1135,7 +1140,7 @@ static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len, buflen = (s->num_saved_bits + len + 8) >> 3; if (len <= 0 || buflen > MAX_FRAMESIZE) { - av_log_ask_for_sample(s->avctx, "input buffer too small\n"); + avpriv_request_sample(s->avctx, "Too small input buffer"); s->packet_loss = 1; return; } @@ -1169,7 +1174,7 @@ static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr, int buf_size = avpkt->size; int num_bits_prev_frame, packet_sequence_number, spliced_packet; - s->frame.nb_samples = 0; + s->frame->nb_samples = 0; if (s->packet_done || s->packet_loss) { s->packet_done = 0; @@ -1188,7 +1193,7 @@ static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr, skip_bits(gb, 1); // Skip seekable_frame_in_packet, currently ununused spliced_packet = get_bits1(gb); if (spliced_packet) - av_log_missing_feature(avctx, "Bitstream splicing", 1); + avpriv_request_sample(avctx, "Bitstream splicing"); /* get number of bits that need to be added to the previous frame */ num_bits_prev_frame = get_bits(gb, s->log2_frame_size); @@ -1226,6 +1231,7 @@ static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr, * to decode incomplete frames in the s->len_prefix == 0 case. */ s->num_saved_bits = 0; s->packet_loss = 0; + init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE); } } else { @@ -1261,8 +1267,9 @@ static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr, save_bits(s, gb, remaining_bits(s, gb), 0); } - *(AVFrame *)data = s->frame; - *got_frame_ptr = s->frame.nb_samples > 0; + *got_frame_ptr = s->frame->nb_samples > 0; + av_frame_move_ref(data, s->frame); + s->packet_offset = get_bits_count(gb) & 7; return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3; @@ -1277,17 +1284,31 @@ static void flush(AVCodecContext *avctx) s->frame_offset = 0; s->next_packet_start = 0; s->cdlms[0][0].order = 0; - s->frame.nb_samples = 0; + s->frame->nb_samples = 0; + init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE); +} + +static av_cold int decode_close(AVCodecContext *avctx) +{ + WmallDecodeCtx *s = avctx->priv_data; + + av_frame_free(&s->frame); + + return 0; } AVCodec ff_wmalossless_decoder = { .name = "wmalossless", + .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio Lossless"), .type = AVMEDIA_TYPE_AUDIO, .id = AV_CODEC_ID_WMALOSSLESS, .priv_data_size = sizeof(WmallDecodeCtx), .init = decode_init, + .close = decode_close, .decode = decode_packet, .flush = flush, .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1 | CODEC_CAP_DELAY, - .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio Lossless"), + .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P, + AV_SAMPLE_FMT_S32P, + AV_SAMPLE_FMT_NONE }, };