[FFmpeg-devel] [PATCH v5 05/13] avdevice/dshow: handle unknown sample time

Diederick Niehorster dcnieho at gmail.com
Sun Dec 19 21:21:26 EET 2021


GetTime may return an error indication that the sample has not
timestamps, or may return a NULL start time. In those cases, fall back
to graph time. Emit log when that happens.
Improve logging in the frame receive function: now logged against
correct avclass instead of NULL.
Better debug message in case sample dropped: could now be audio or
video frame.

Signed-off-by: Diederick Niehorster <dcnieho at gmail.com>
---
 libavdevice/dshow_pin.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/libavdevice/dshow_pin.c b/libavdevice/dshow_pin.c
index 1d0e880480..2d1fa0e882 100644
--- a/libavdevice/dshow_pin.c
+++ b/libavdevice/dshow_pin.c
@@ -295,14 +295,15 @@ long ff_dshow_meminputpin_Receive(DShowMemInputPin *this, IMediaSample *sample)
     uint8_t *buf;
     int buf_size; /* todo should be a long? */
     int index;
-    int64_t chosentime;
-    int64_t sampletime;
-    int64_t graphtime;
+    int64_t chosentime = 0;
+    int64_t sampletime = 0;
+    int64_t graphtime = 0;
     int use_sample_time = 1;
     const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
     IReferenceClock *clock = pin->filter->clock;
     int64_t dummy;
     struct dshow_ctx *ctx;
+    HRESULT hr;
 
 
     dshowdebug("ff_dshow_meminputpin_Receive(%p)\n", this);
@@ -314,22 +315,28 @@ long ff_dshow_meminputpin_Receive(DShowMemInputPin *this, IMediaSample *sample)
     s = priv_data;
     ctx = s->priv_data;
 
-    IMediaSample_GetTime(sample, &sampletime, &dummy);
+    hr = IMediaSample_GetTime(sample, &sampletime, &dummy);
     IReferenceClock_GetTime(clock, &graphtime);
     if (devtype == VideoDevice && !ctx->use_video_device_timestamps) {
         /* PTS from video devices is unreliable. */
         chosentime = graphtime;
         use_sample_time = 0;
     } else {
-        if (sampletime > 400000000000000000LL) {
+        if (hr == VFW_E_SAMPLE_TIME_NOT_SET || sampletime == 0) {
+            chosentime = graphtime;
+            use_sample_time = 0;
+            av_log(s, AV_LOG_DEBUG,
+                "frame with missing sample timestamp encountered, falling back to graph timestamp\n");
+        }
+        else if (sampletime > 400000000000000000LL) {
             /* initial frames sometimes start < 0 (shown as a very large number here,
                like 437650244077016960 which FFmpeg doesn't like).
                TODO figure out math. For now just drop them. */
-            av_log(NULL, AV_LOG_DEBUG,
-                "dshow dropping initial (or ending) frame with odd PTS too high %"PRId64"\n", sampletime);
+            av_log(s, AV_LOG_DEBUG,
+                "dropping initial (or ending) sample with odd PTS too high %"PRId64"\n", sampletime);
             return S_OK;
-        }
-        chosentime = sampletime;
+        } else
+            chosentime = sampletime;
     }
     // media sample time is relative to graph start time
     sampletime += pin->filter->start_time;
@@ -340,7 +347,7 @@ long ff_dshow_meminputpin_Receive(DShowMemInputPin *this, IMediaSample *sample)
     IMediaSample_GetPointer(sample, &buf);
     index = pin->filter->stream_index;
 
-    av_log(NULL, AV_LOG_VERBOSE, "dshow passing through packet of type %s size %8d "
+    av_log(s, AV_LOG_VERBOSE, "passing through packet of type %s size %8d "
         "timestamp %"PRId64" orig timestamp %"PRId64" graph timestamp %"PRId64" diff %"PRId64" %s\n",
         devtypename, buf_size, chosentime, sampletime, graphtime, graphtime - sampletime, ctx->device_name[devtype]);
     pin->filter->callback(priv_data, index, buf, buf_size, chosentime, devtype);
-- 
2.28.0.windows.1



More information about the ffmpeg-devel mailing list