From e3db686e8746984cc580a347e7a4a8e4077cbf37 Mon Sep 17 00:00:00 2001 From: wm4 Date: Wed, 4 Nov 2015 16:50:16 +0100 Subject: [PATCH] af_lavcac3enc: simplify/fix AVPacket handling For some reason, the encoder didn't like that the AVPacket already had fields set. I'm not quite sure, but this might just be invalid API usage. Do it as it's recommended. --- audio/filter/af_lavcac3enc.c | 37 ++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/audio/filter/af_lavcac3enc.c b/audio/filter/af_lavcac3enc.c index c1e6b964ad..973e688aaa 100644 --- a/audio/filter/af_lavcac3enc.c +++ b/audio/filter/af_lavcac3enc.c @@ -49,7 +49,6 @@ const uint16_t ac3_bitrate_tab[19] = { typedef struct af_ac3enc_s { struct AVCodec *lavc_acodec; struct AVCodecContext *lavc_actx; - AVPacket pkt; int bit_rate; struct mp_audio *input; // frame passed to libavcodec struct mp_audio *pending; // unconsumed input data @@ -146,7 +145,6 @@ static void uninit(struct af_instance* af) af_ac3enc_t *s = af->priv; if (s) { - av_packet_unref(&s->pkt); if(s->lavc_actx) { avcodec_close(s->lavc_actx); av_free(s->lavc_actx); @@ -212,6 +210,8 @@ static int filter_out(struct af_instance *af) MP_FATAL(af, "Could not allocate memory \n"); return -1; } + int err = -1; + frame->nb_samples = s->in_samples; frame->format = s->lavc_actx->sample_fmt; frame->channel_layout = s->lavc_actx->channel_layout; @@ -221,30 +221,33 @@ static int filter_out(struct af_instance *af) frame->data[n] = s->input->planes[n]; frame->linesize[0] = s->input->samples * s->input->sstride; + AVPacket pkt = {0}; + av_init_packet(&pkt); + int ok; - int lavc_ret = avcodec_encode_audio2(s->lavc_actx, &s->pkt, frame, &ok); + int lavc_ret = avcodec_encode_audio2(s->lavc_actx, &pkt, frame, &ok); av_frame_free(&frame); s->input->samples = 0; if (lavc_ret < 0 || !ok) { MP_FATAL(af, "Encode failed.\n"); - return -1; + goto done; } MP_DBG(af, "avcodec_encode_audio got %d, pending %d.\n", - s->pkt.size, s->pending->samples); + pkt.size, s->pending->samples); struct mp_audio *out = mp_audio_pool_get(af->out_pool, af->data, s->out_samples); if (!out) - return -1; + goto done; mp_audio_copy_attributes(out, s->pending); - int frame_size = s->pkt.size; + int frame_size = pkt.size; int header_len = 0; char hdr[8]; - if (s->cfg_add_iec61937_header && s->pkt.size > 5) { - int bsmod = s->pkt.data[5] & 0x7; + if (s->cfg_add_iec61937_header && pkt.size > 5) { + int bsmod = pkt.data[5] & 0x7; int len = frame_size; frame_size = AC3_FRAME_SIZE * 2 * 2; @@ -262,14 +265,18 @@ static int filter_out(struct af_instance *af) char *buf = (char *)out->planes[0]; memcpy(buf, hdr, header_len); - memcpy(buf + header_len, s->pkt.data, s->pkt.size); - memset(buf + header_len + s->pkt.size, 0, - frame_size - (header_len + s->pkt.size)); - swap_16((uint16_t *)(buf + header_len), s->pkt.size / 2); + memcpy(buf + header_len, pkt.data, pkt.size); + memset(buf + header_len + pkt.size, 0, + frame_size - (header_len + pkt.size)); + swap_16((uint16_t *)(buf + header_len), pkt.size / 2); out->samples = frame_size / out->sstride; af_add_output_frame(af, out); update_delay(af); - return 0; + + err = 0; +done: + av_packet_unref(&pkt); + return err; } static int af_open(struct af_instance* af){ @@ -307,8 +314,6 @@ static int af_open(struct af_instance* af){ MP_VERBOSE(af, "[af_lavcac3enc]: in sample format: %s\n", af_fmt_to_str(s->in_sampleformat)); - av_init_packet(&s->pkt); - s->input = talloc_zero(s, struct mp_audio); if (s->cfg_bit_rate) {