diff options
author | Rudolf Polzer <rpolzer@google.com> | 2016-06-24 14:20:32 -0400 |
---|---|---|
committer | Rudolf Polzer <rpolzer@google.com> | 2016-06-27 08:33:12 -0400 |
commit | acb74236ac9e48ccc653207a22428d3811b0a2cd (patch) | |
tree | 13b0d2ef054f493d94948bd46b61e487419ba42c /audio | |
parent | c5094206ce6ff1a557540ed6e0d8505bc6db0031 (diff) |
ao_lavc, vo_lavc: Migrate to new encoding API.
Also marked some places for possible later refactoring, as they became
quite similar in this commit.
Diffstat (limited to 'audio')
-rw-r--r-- | audio/out/ao_lavc.c | 197 |
1 files changed, 121 insertions, 76 deletions
diff --git a/audio/out/ao_lavc.c b/audio/out/ao_lavc.c index 572874d27c..6b4279ca87 100644 --- a/audio/out/ao_lavc.c +++ b/audio/out/ao_lavc.c @@ -39,8 +39,6 @@ #include "common/encode_lavc.h" struct priv { - uint8_t *buffer; - size_t buffer_size; AVStream *stream; AVCodecContext *codec; int pcmhack; @@ -146,18 +144,10 @@ static int init(struct ao *ao) if (ac->codec->frame_size <= 1) ac->pcmhack = av_get_bits_per_sample(ac->codec->codec_id) / 8; - if (ac->pcmhack) { + if (ac->pcmhack) ac->aframesize = 16384; // "enough" - ac->buffer_size = - ac->aframesize * ac->pcmhack * ao->channels.num * 2 + 200; - } else { + else ac->aframesize = ac->codec->frame_size; - ac->buffer_size = - ac->aframesize * ac->sample_size * ao->channels.num * 2 + 200; - } - if (ac->buffer_size < FF_MIN_BUFFER_SIZE) - ac->buffer_size = FF_MIN_BUFFER_SIZE; - ac->buffer = talloc_size(ac, ac->buffer_size); // enough frames for at least 0.25 seconds ac->framecount = ceil(ao->samplerate * 0.25 / ac->aframesize); @@ -182,7 +172,7 @@ fail: } // close audio device -static int encode(struct ao *ao, double apts, void **data); +static void encode(struct ao *ao, double apts, void **data); static void uninit(struct ao *ao) { struct priv *ac = ao->priv; @@ -199,12 +189,12 @@ static void uninit(struct ao *ao) return; } - if (ac->buffer) { + if (ac->stream) { double outpts = ac->expected_next_pts; if (!ectx->options->rawts && ectx->options->copyts) outpts += ectx->discontinuity_pts_offset; outpts += encode_lavc_getoffset(ectx, ac->codec); - while (encode(ao, outpts, NULL) > 0) ; + encode(ao, outpts, NULL); } pthread_mutex_unlock(&ectx->lock); @@ -220,24 +210,130 @@ static int get_space(struct ao *ao) return ac->aframesize * ac->framecount; } +static void write_packet(struct ao *ao, AVPacket *packet) +{ + // TODO: Can we unify this with the equivalent video code path? + struct priv *ac = ao->priv; + + packet->stream_index = ac->stream->index; + if (packet->pts != AV_NOPTS_VALUE) { + packet->pts = av_rescale_q(packet->pts, + ac->codec->time_base, + ac->stream->time_base); + } else { + // Do we need this at all? Better be safe than sorry... + MP_WARN(ao, "encoder lost pts, why?\n"); + if (ac->savepts != MP_NOPTS_VALUE) { + packet->pts = av_rescale_q(ac->savepts, + ac->codec->time_base, + ac->stream->time_base); + } + } + if (packet->dts != AV_NOPTS_VALUE) { + packet->dts = av_rescale_q(packet->dts, + ac->codec->time_base, + ac->stream->time_base); + } + if (packet->duration > 0) { + packet->duration = av_rescale_q(packet->duration, + ac->codec->time_base, + ac->stream->time_base); + } + + ac->savepts = AV_NOPTS_VALUE; + + if (encode_lavc_write_frame(ao->encode_lavc_ctx, + ac->stream, packet) < 0) { + MP_ERR(ao, "error writing at %d %d/%d\n", + (int) packet->pts, + ac->stream->time_base.num, + ac->stream->time_base.den); + return; + } +} + +static void encode_audio_and_write(struct ao *ao, AVFrame *frame) +{ + // TODO: Can we unify this with the equivalent video code path? + struct priv *ac = ao->priv; + AVPacket packet = {0}; + +#if HAVE_AVCODEC_NEW_CODEC_API + int status = avcodec_send_frame(ac->codec, frame); + if (status < 0) { + MP_ERR(ao, "error encoding at %d %d/%d\n", + frame ? (int) frame->pts : -1, + ac->codec->time_base.num, + ac->codec->time_base.den); + return; + } + for (;;) { + av_init_packet(&packet); + status = avcodec_receive_packet(ac->codec, &packet); + if (status == AVERROR(EAGAIN)) { // No more packets for now. + if (frame == NULL) { + MP_ERR(ao, "sent flush frame, got EAGAIN"); + } + break; + } + if (status == AVERROR_EOF) { // No more packets, ever. + if (frame != NULL) { + MP_ERR(ao, "sent audio frame, got EOF"); + } + break; + } + if (status < 0) { + MP_ERR(ao, "error encoding at %d %d/%d\n", + frame ? (int) frame->pts : -1, + ac->codec->time_base.num, + ac->codec->time_base.den); + break; + } + if (frame) { + if (ac->savepts == AV_NOPTS_VALUE) + ac->savepts = frame->pts; + } + encode_lavc_write_stats(ao->encode_lavc_ctx, ac->codec); + write_packet(ao, &packet); + av_packet_unref(&packet); + } +#else + av_init_packet(&packet); + int got_packet = 0; + int status = avcodec_encode_audio2(ac->codec, &packet, frame, &got_packet); + if (status < 0) { + MP_ERR(ao, "error encoding at %d %d/%d\n", + frame ? (int) frame->pts : -1, + ac->codec->time_base.num, + ac->codec->time_base.den); + return; + } + if (!got_packet) { + return; + } + if (frame) { + if (ac->savepts == AV_NOPTS_VALUE) + ac->savepts = frame->pts; + } + encode_lavc_write_stats(ao->encode_lavc_ctx, ac->codec); + write_packet(ao, &packet); + av_packet_unref(&packet); +#endif +} + // must get exactly ac->aframesize amount of data -static int encode(struct ao *ao, double apts, void **data) +static void encode(struct ao *ao, double apts, void **data) { - AVPacket packet; struct priv *ac = ao->priv; struct encode_lavc_context *ectx = ao->encode_lavc_ctx; double realapts = ac->aframecount * (double) ac->aframesize / ao->samplerate; - int status, gotpacket; ac->aframecount++; if (data) ectx->audio_pts_offset = realapts - apts; - av_init_packet(&packet); - packet.data = ac->buffer; - packet.size = ac->buffer_size; if(data) { AVFrame *frame = av_frame_alloc(); frame->format = af_to_avformat(ao->format); @@ -270,64 +366,11 @@ static int encode(struct ao *ao, double apts, void **data) ac->lastpts = frame_pts; frame->quality = ac->codec->global_quality; - status = avcodec_encode_audio2(ac->codec, &packet, frame, &gotpacket); - - if (!status) { - if (ac->savepts == AV_NOPTS_VALUE) - ac->savepts = frame->pts; - } - + encode_audio_and_write(ao, frame); av_frame_free(&frame); } else - { - status = avcodec_encode_audio2(ac->codec, &packet, NULL, &gotpacket); - } - - if(status) { - MP_ERR(ao, "error encoding\n"); - return -1; - } - - if(!gotpacket) - return 0; - - MP_DBG(ao, "got pts %f (playback time: %f); out size: %d\n", - apts, realapts, packet.size); - - encode_lavc_write_stats(ao->encode_lavc_ctx, ac->codec); - - packet.stream_index = ac->stream->index; - - // Do we need this at all? Better be safe than sorry... - if (packet.pts == AV_NOPTS_VALUE) { - MP_WARN(ao, "encoder lost pts, why?\n"); - if (ac->savepts != MP_NOPTS_VALUE) - packet.pts = ac->savepts; - } - - if (packet.pts != AV_NOPTS_VALUE) - packet.pts = av_rescale_q(packet.pts, ac->codec->time_base, - ac->stream->time_base); - - if (packet.dts != AV_NOPTS_VALUE) - packet.dts = av_rescale_q(packet.dts, ac->codec->time_base, - ac->stream->time_base); - - if(packet.duration > 0) - packet.duration = av_rescale_q(packet.duration, ac->codec->time_base, - ac->stream->time_base); - - ac->savepts = AV_NOPTS_VALUE; - - if (encode_lavc_write_frame(ao->encode_lavc_ctx, ac->stream, &packet) < 0) { - MP_ERR(ao, "error writing at %f %f/%f\n", - realapts, (double) ac->stream->time_base.num, - (double) ac->stream->time_base.den); - return -1; - } - - return packet.size; + encode_audio_and_write(ao, NULL); } // this should round samples down to frame sizes @@ -492,3 +535,5 @@ const struct ao_driver audio_out_lavc = { .play = play, .drain = drain, }; + +// vim: sw=4 ts=4 et tw=80 |