diff mbox

[FFmpeg-devel,1/2] avdevice/decklink: addition of copyts option

Message ID 1515389203-30954-1-git-send-email-vdixit@akamai.com
State Superseded
Headers show

Commit Message

Dixit, Vishwanath Jan. 8, 2018, 5:26 a.m. UTC
From: Vishwanath Dixit <vdixit@akamai.com>

---
 doc/indevs.texi                 |  5 +++++
 libavdevice/decklink_common_c.h |  1 +
 libavdevice/decklink_dec.cpp    | 18 +++++++++++-------
 libavdevice/decklink_dec_c.c    |  1 +
 4 files changed, 18 insertions(+), 7 deletions(-)

Comments

Carl Eugen Hoyos Jan. 20, 2018, 6:12 p.m. UTC | #1
2018-01-08 6:26 GMT+01:00  <vdixit@akamai.com>:

> +@item decklink_copyts
> +If set to @option{true}, timestamps are forwarded as they are without removing
> +the initial offset.
> +Defaults to @option{false}.

Why is the offset ever removed?
I believe this is not done for any other demuxer or is it?

Carl Eugen
Marton Balint Jan. 20, 2018, 6:50 p.m. UTC | #2
On Sat, 20 Jan 2018, Carl Eugen Hoyos wrote:

> 2018-01-08 6:26 GMT+01:00  <vdixit@akamai.com>:
>
>> +@item decklink_copyts
>> +If set to @option{true}, timestamps are forwarded as they are without removing
>> +the initial offset.
>> +Defaults to @option{false}.
>
> Why is the offset ever removed?
> I believe this is not done for any other demuxer or is it?

The code keeps track separate audio and video initial offset, so even if 
you are using different clock sources for audio and video, you will get 
comparable output. Probably that is the reason.

Regards,
Marton
Carl Eugen Hoyos Jan. 20, 2018, 7:06 p.m. UTC | #3
2018-01-20 19:50 GMT+01:00 Marton Balint <cus@passwd.hu>:
>
> On Sat, 20 Jan 2018, Carl Eugen Hoyos wrote:
>
>> 2018-01-08 6:26 GMT+01:00  <vdixit@akamai.com>:
>>
>>> +@item decklink_copyts
>>> +If set to @option{true}, timestamps are forwarded as they are without
>>> removing
>>> +the initial offset.
>>> +Defaults to @option{false}.
>>
>>
>> Why is the offset ever removed?
>> I believe this is not done for any other demuxer or is it?
>
> The code keeps track separate audio and video initial offset

But they can't be independent or can they?
And if they can, how is A/V sync assured?

I am just surprised because I don't remember another
device input that removes the time offset on initialization
and demuxers like mpegts don't do it either.

Carl Eugen
Marton Balint Jan. 20, 2018, 7:47 p.m. UTC | #4
On Sat, 20 Jan 2018, Carl Eugen Hoyos wrote:

> 2018-01-20 19:50 GMT+01:00 Marton Balint <cus@passwd.hu>:
>>
>> On Sat, 20 Jan 2018, Carl Eugen Hoyos wrote:
>>
>>> 2018-01-08 6:26 GMT+01:00  <vdixit@akamai.com>:
>>>
>>>> +@item decklink_copyts
>>>> +If set to @option{true}, timestamps are forwarded as they are without
>>>> removing
>>>> +the initial offset.
>>>> +Defaults to @option{false}.
>>>
>>>
>>> Why is the offset ever removed?
>>> I believe this is not done for any other demuxer or is it?
>>
>> The code keeps track separate audio and video initial offset
>
> But they can't be independent or can they?
> And if they can, how is A/V sync assured?

Well, I am not entirely sure. Decklink has always had problems with 
timestamps and A-V desync was not uncommon when using the default clock 
sources. Probably a bug in the SDK.

The whole offset removal is is originated from this commit:

https://github.com/lu-zero/bmdtools/commit/b51ce3841ea763f27431ed9c77b99ae5404180c6

Regards,
Marton
Carl Eugen Hoyos Jan. 20, 2018, 8:10 p.m. UTC | #5
2018-01-20 20:47 GMT+01:00 Marton Balint <cus@passwd.hu>:
>
> On Sat, 20 Jan 2018, Carl Eugen Hoyos wrote:
>
>> 2018-01-20 19:50 GMT+01:00 Marton Balint <cus@passwd.hu>:
>>>
>>> On Sat, 20 Jan 2018, Carl Eugen Hoyos wrote:
>>>
>>>> 2018-01-08 6:26 GMT+01:00  <vdixit@akamai.com>:
>>>>
>>>>> +@item decklink_copyts
>>>>> +If set to @option{true}, timestamps are forwarded as they are without
>>>>> removing
>>>>> +the initial offset.
>>>>> +Defaults to @option{false}.
>>>>
>>>> Why is the offset ever removed?
>>>> I believe this is not done for any other demuxer or is it?
>>>
>>> The code keeps track separate audio and video initial offset
>>
>> But they can't be independent or can they?
>> And if they can, how is A/V sync assured?
>
> Well, I am not entirely sure. Decklink has always had problems with
> timestamps and A-V desync was not uncommon when using the default clock
> sources. Probably a bug in the SDK.
>
> The whole offset removal is is originated from this commit:
>
> https://github.com/lu-zero/bmdtools/commit/b51ce3841ea763f27431ed9c77b99ae5404180c6

Thanks for the info, Carl Eugen
diff mbox

Patch

diff --git a/doc/indevs.texi b/doc/indevs.texi
index 56066bf..36aef49 100644
--- a/doc/indevs.texi
+++ b/doc/indevs.texi
@@ -317,6 +317,11 @@  Defaults to @samp{1073741824}.
 Sets the audio sample bit depth. Must be @samp{16} or @samp{32}.
 Defaults to @samp{16}.
 
+@item decklink_copyts
+If set to @option{true}, timestamps are forwarded as they are without removing
+the initial offset.
+Defaults to @option{false}.
+
 @end table
 
 @subsection Examples
diff --git a/libavdevice/decklink_common_c.h b/libavdevice/decklink_common_c.h
index 368ac25..ac6563a 100644
--- a/libavdevice/decklink_common_c.h
+++ b/libavdevice/decklink_common_c.h
@@ -52,6 +52,7 @@  struct decklink_cctx {
     char *format_code;
     int raw_format;
     int64_t queue_size;
+    int copyts;
 };
 
 #endif /* AVDEVICE_DECKLINK_COMMON_C_H */
diff --git a/libavdevice/decklink_dec.cpp b/libavdevice/decklink_dec.cpp
index 94dae26..1fd40ca 100644
--- a/libavdevice/decklink_dec.cpp
+++ b/libavdevice/decklink_dec.cpp
@@ -586,7 +586,8 @@  static int64_t get_pkt_pts(IDeckLinkVideoInputFrame *videoFrame,
                            IDeckLinkAudioInputPacket *audioFrame,
                            int64_t wallclock,
                            DecklinkPtsSource pts_src,
-                           AVRational time_base, int64_t *initial_pts)
+                           AVRational time_base, int64_t *initial_pts,
+                           int copyts)
 {
     int64_t pts = AV_NOPTS_VALUE;
     BMDTimeValue bmd_pts;
@@ -619,10 +620,12 @@  static int64_t get_pkt_pts(IDeckLinkVideoInputFrame *videoFrame,
     if (res == S_OK)
         pts = bmd_pts / time_base.num;
 
-    if (pts != AV_NOPTS_VALUE && *initial_pts == AV_NOPTS_VALUE)
-        *initial_pts = pts;
-    if (*initial_pts != AV_NOPTS_VALUE)
-        pts -= *initial_pts;
+    if (!copyts) {
+        if (pts != AV_NOPTS_VALUE && *initial_pts == AV_NOPTS_VALUE)
+            *initial_pts = pts;
+        if (*initial_pts != AV_NOPTS_VALUE)
+            pts -= *initial_pts;
+    }
 
     return pts;
 }
@@ -635,6 +638,7 @@  HRESULT decklink_input_callback::VideoInputFrameArrived(
     BMDTimeValue frameTime;
     BMDTimeValue frameDuration;
     int64_t wallclock = 0;
+    struct decklink_cctx *cctx = (struct decklink_cctx *) avctx->priv_data;
 
     if (ctx->autodetect) {
         if (videoFrame && !(videoFrame->GetFlags() & bmdFrameHasNoInputSource) &&
@@ -694,7 +698,7 @@  HRESULT decklink_input_callback::VideoInputFrameArrived(
             no_video = 0;
         }
 
-        pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, ctx->video_pts_source, ctx->video_st->time_base, &initial_video_pts);
+        pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, ctx->video_pts_source, ctx->video_st->time_base, &initial_video_pts, cctx->copyts);
         pkt.dts = pkt.pts;
 
         pkt.duration = frameDuration;
@@ -785,7 +789,7 @@  HRESULT decklink_input_callback::VideoInputFrameArrived(
         pkt.size = audioFrame->GetSampleFrameCount() * ctx->audio_st->codecpar->channels * (ctx->audio_depth / 8);
         audioFrame->GetBytes(&audioFrameBytes);
         audioFrame->GetPacketTime(&audio_pts, ctx->audio_st->time_base.den);
-        pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, ctx->audio_pts_source, ctx->audio_st->time_base, &initial_audio_pts);
+        pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, ctx->audio_pts_source, ctx->audio_st->time_base, &initial_audio_pts, cctx->copyts);
         pkt.dts = pkt.pts;
 
         //fprintf(stderr,"Audio Frame size %d ts %d\n", pkt.size, pkt.pts);
diff --git a/libavdevice/decklink_dec_c.c b/libavdevice/decklink_dec_c.c
index 1c6d826..6fb5ffe 100644
--- a/libavdevice/decklink_dec_c.c
+++ b/libavdevice/decklink_dec_c.c
@@ -73,6 +73,7 @@  static const AVOption options[] = {
     { "draw_bars",     "draw bars on signal loss" , OFFSET(draw_bars),    AV_OPT_TYPE_BOOL,  { .i64 = 1}, 0, 1, DEC },
     { "queue_size",    "input queue buffer size",   OFFSET(queue_size),   AV_OPT_TYPE_INT64, { .i64 = (1024 * 1024 * 1024)}, 0, INT64_MAX, DEC },
     { "audio_depth",   "audio bitdepth (16 or 32)", OFFSET(audio_depth),  AV_OPT_TYPE_INT,   { .i64 = 16}, 16, 32, DEC },
+    { "decklink_copyts", "copy timestamps, do not remove the initial offset", OFFSET(copyts), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
     { NULL },
 };