Skip to content

Commit

Permalink
avdevice/dshow: handle unknown sample time
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Roger Pack <[email protected]>
  • Loading branch information
dcnieho authored and GyanD committed Dec 24, 2021
1 parent 584b0fb commit 271e559
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions libavdevice/dshow_pin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 271e559

Please sign in to comment.