[FFmpeg-devel] [PATCH 23/25] avcodec/v4l2_buffers: use correct timebase for encoder/decoder/filter

Aman Gupta ffmpeg at tmm1.net
Tue Sep 3 04:02:28 EEST 2019


From: Aman Gupta <aman at tmm1.net>

Signed-off-by: Aman Gupta <aman at tmm1.net>
---
 libavcodec/v4l2_buffers.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/libavcodec/v4l2_buffers.c b/libavcodec/v4l2_buffers.c
index 389b5dea8d..d8cec06cb3 100644
--- a/libavcodec/v4l2_buffers.c
+++ b/libavcodec/v4l2_buffers.c
@@ -55,40 +55,41 @@ static inline AVCodecContext *logger(V4L2Buffer *buf)
     return buf_to_m2mctx(buf)->avctx;
 }
 
+static inline AVRational v4l2_get_timebase(V4L2Buffer *avbuf)
+{
+    V4L2m2mContext *s = buf_to_m2mctx(avbuf);
+
+    if (s->filterctx)
+        return s->filterctx->inputs[0]->time_base;
+    if (s->avctx->pkt_timebase.num)
+        return s->avctx->pkt_timebase;
+    return s->avctx->time_base;
+}
+
 static inline void v4l2_set_pts(V4L2Buffer *out, int64_t pts)
 {
-    V4L2m2mContext *s = buf_to_m2mctx(out);
     AVRational v4l2_timebase = { 1, USEC_PER_SEC };
-    AVRational timebase;
     int64_t v4l2_pts;
 
     if (pts == AV_NOPTS_VALUE)
         pts = 0;
 
-    timebase = s->filterctx ? s->filterctx->inputs[0]->time_base
-                            : s->avctx->time_base;
-
     /* convert pts to v4l2 timebase */
-    v4l2_pts = av_rescale_q(pts, timebase, v4l2_timebase);
+    v4l2_pts = av_rescale_q(pts, v4l2_get_timebase(out), v4l2_timebase);
     out->buf.timestamp.tv_usec = v4l2_pts % USEC_PER_SEC;
     out->buf.timestamp.tv_sec = v4l2_pts / USEC_PER_SEC;
 }
 
 static inline int64_t v4l2_get_pts(V4L2Buffer *avbuf)
 {
-    V4L2m2mContext *s = buf_to_m2mctx(avbuf);
     AVRational v4l2_timebase = { 1, USEC_PER_SEC };
-    AVRational timebase;
     int64_t v4l2_pts;
 
-    timebase = s->filterctx ? s->filterctx->inputs[0]->time_base
-                            : s->avctx->time_base;
-
     /* convert pts back to encoder timebase */
     v4l2_pts = (int64_t)avbuf->buf.timestamp.tv_sec * USEC_PER_SEC +
                         avbuf->buf.timestamp.tv_usec;
 
-    return av_rescale_q(v4l2_pts, v4l2_timebase, timebase);
+    return av_rescale_q(v4l2_pts, v4l2_timebase, v4l2_get_timebase(avbuf));
 }
 
 static enum AVColorPrimaries v4l2_get_color_primaries(V4L2Buffer *buf)
-- 
2.20.1



More information about the ffmpeg-devel mailing list