From 751acd16371f670348b121bf5550deb7b810110f Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Tue, 18 Sep 2018 13:55:52 -0700 Subject: [PATCH] FFmpeg: Fix encoding audio/video queue issues --- CHANGES | 1 + src/feature/ffmpeg/ffmpeg-encoder.c | 10 ++++++---- src/feature/ffmpeg/ffmpeg-encoder.h | 1 - 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index 995724252..e0f784b9a 100644 --- a/CHANGES +++ b/CHANGES @@ -55,6 +55,7 @@ Bugfixes: - GBA Memory: Fix Vast Fame support (taizou) (fixes mgba.io/i/1170) - GB, GBA Savedata: Fix unmasking savedata crash - GBA DMA: Fix temporal sorting of DMAs of different priorities + - FFmpeg: Fix encoding audio/video queue issues Misc: - GBA Timer: Use global cycles for timers - GBA: Extend oddly-sized ROMs to full address space (fixes mgba.io/i/722) diff --git a/src/feature/ffmpeg/ffmpeg-encoder.c b/src/feature/ffmpeg/ffmpeg-encoder.c index 2e53479d6..acf21dd8c 100644 --- a/src/feature/ffmpeg/ffmpeg-encoder.c +++ b/src/feature/ffmpeg/ffmpeg-encoder.c @@ -205,7 +205,6 @@ bool FFmpegEncoderOpen(struct FFmpegEncoder* encoder, const char* outfile) { encoder->currentAudioSample = 0; encoder->currentAudioFrame = 0; encoder->currentVideoFrame = 0; - encoder->nextAudioPts = 0; AVOutputFormat* oformat = av_guess_format(encoder->containerFormat, 0, 0); #ifndef USE_LIBAV @@ -301,6 +300,7 @@ bool FFmpegEncoderOpen(struct FFmpegEncoder* encoder, const char* outfile) { encoder->video->width = encoder->width; encoder->video->height = encoder->height; encoder->video->time_base = (AVRational) { VIDEO_TOTAL_LENGTH, GBA_ARM7TDMI_FREQUENCY }; + encoder->video->framerate = (AVRational) { GBA_ARM7TDMI_FREQUENCY, VIDEO_TOTAL_LENGTH }; encoder->video->pix_fmt = encoder->pixFormat; encoder->video->gop_size = 60; encoder->video->max_b_frames = 3; @@ -457,14 +457,13 @@ void _ffmpegPostAudioFrame(struct mAVStream* stream, int16_t left, int16_t right (const uint8_t**) &encoder->audioBuffer, encoder->audioBufferSize / 4); #endif - encoder->audioFrame->pts = av_rescale_q(encoder->currentAudioFrame, encoder->audio->time_base, encoder->audioStream->time_base); + encoder->audioFrame->pts = encoder->currentAudioFrame; encoder->currentAudioFrame += samples; AVPacket packet; av_init_packet(&packet); packet.data = 0; packet.size = 0; - packet.pts = encoder->audioFrame->pts; int gotData; #ifdef FFMPEG_USE_PACKETS @@ -474,6 +473,9 @@ void _ffmpegPostAudioFrame(struct mAVStream* stream, int16_t left, int16_t right #else avcodec_encode_audio2(encoder->audio, &packet, encoder->audioFrame, &gotData); #endif + packet.pts = av_rescale_q(encoder->audioFrame->pts, encoder->audio->time_base, encoder->audioStream->time_base); + packet.dts = packet.pts; + if (gotData) { if (encoder->absf) { AVPacket tempPacket; @@ -532,7 +534,6 @@ void _ffmpegPostVideoFrame(struct mAVStream* stream, const color_t* pixels, size av_frame_make_writable(encoder->videoFrame); #endif encoder->videoFrame->pts = av_rescale_q(encoder->currentVideoFrame, encoder->video->time_base, encoder->videoStream->time_base); - packet.pts = encoder->videoFrame->pts; ++encoder->currentVideoFrame; sws_scale(encoder->scaleContext, (const uint8_t* const*) &pixels, (const int*) &stride, 0, encoder->iheight, encoder->videoFrame->data, encoder->videoFrame->linesize); @@ -544,6 +545,7 @@ void _ffmpegPostVideoFrame(struct mAVStream* stream, const color_t* pixels, size #else avcodec_encode_video2(encoder->video, &packet, encoder->videoFrame, &gotData); #endif + packet.pts = encoder->videoFrame->pts; if (gotData) { #ifndef FFMPEG_USE_PACKET_UNREF if (encoder->video->coded_frame->key_frame) { diff --git a/src/feature/ffmpeg/ffmpeg-encoder.h b/src/feature/ffmpeg/ffmpeg-encoder.h index f8c5add36..a5215b8a5 100644 --- a/src/feature/ffmpeg/ffmpeg-encoder.h +++ b/src/feature/ffmpeg/ffmpeg-encoder.h @@ -56,7 +56,6 @@ struct FFmpegEncoder { AVFrame* audioFrame; size_t currentAudioSample; int64_t currentAudioFrame; - int64_t nextAudioPts; // TODO (0.6): Remove #ifdef USE_LIBAVRESAMPLE struct AVAudioResampleContext* resampleContext; #else