[FFmpeg-devel] [RFC] [PATCH] ffmpeg: more accurate -t handling for audio.

Nicolas George nicolas.george at normalesup.org
Tue Jul 10 22:07:51 CEST 2012


If the encoder is capable of variable frame size,
truncate the last encoded frame to the exact sample.

Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
 ffmpeg.c |   13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)


It breaks a lot of FATE tests, and that is normal for all of them, except dv.

For dv, there is framing at the muxer level, and truncating the last frame
also drops the corresponding video frame. This is a serious break from the
"muxer abstraction", but I have to do with. Any advice?


diff --git a/ffmpeg.c b/ffmpeg.c
index cf53b91..7715317 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -1565,7 +1565,7 @@ static void do_audio_out(AVFormatContext *s, OutputStream *ost,
 {
     AVCodecContext *enc = ost->st->codec;
     AVPacket pkt;
-    int got_packet = 0;
+    int got_packet = 0, trim;
 
     av_init_packet(&pkt);
     pkt.data = NULL;
@@ -1578,6 +1578,17 @@ static void do_audio_out(AVFormatContext *s, OutputStream *ost,
         frame->pts = ost->sync_opts;
     ost->sync_opts = frame->pts + frame->nb_samples;
 
+    if ((ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) &&
+        !check_recording_time(ost)) {
+        OutputFile *of = output_files[ost->file_index];
+        trim = ost->sync_opts - ost->first_pts -
+               av_rescale_q(of->recording_time, AV_TIME_BASE_Q, enc->time_base);
+        trim = av_clip(trim, 0, frame->nb_samples - 1);
+        av_log(enc, AV_LOG_VERBOSE, "Removing %d/%d samples from last frame.\n",
+               trim, frame->nb_samples);
+        frame->nb_samples -= trim;
+    }
+
     av_assert0(pkt.size || !pkt.data);
     update_benchmark(NULL);
     if (avcodec_encode_audio2(enc, &pkt, frame, &got_packet) < 0) {
-- 
1.7.10.4



More information about the ffmpeg-devel mailing list