]> git.sesse.net Git - ffmpeg/blobdiff - libavdevice/decklink_dec.cpp
af_hdcd: don't log full HDCD stats if HDCD was not detected
[ffmpeg] / libavdevice / decklink_dec.cpp
index 7f452243080801337b737755edf5e3418eb5ccea..fcb024e5b3173cde93901a5b597ca9d145193657 100644 (file)
@@ -28,8 +28,11 @@ extern "C" {
 #include "config.h"
 #include "libavformat/avformat.h"
 #include "libavformat/internal.h"
+#include "libavutil/avutil.h"
 #include "libavutil/common.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/time.h"
+#include "libavutil/mathematics.h"
 #if CONFIG_LIBZVBI
 #include <libzvbi.h>
 #endif
@@ -237,6 +240,44 @@ ULONG decklink_input_callback::Release(void)
     return (ULONG)m_refCount;
 }
 
+static int64_t get_pkt_pts(IDeckLinkVideoInputFrame *videoFrame,
+                           IDeckLinkAudioInputPacket *audioFrame,
+                           int64_t wallclock,
+                           DecklinkPtsSource pts_src,
+                           AVRational time_base, int64_t *initial_pts)
+{
+    int64_t pts = AV_NOPTS_VALUE;
+    BMDTimeValue bmd_pts;
+    BMDTimeValue bmd_duration;
+    HRESULT res = E_INVALIDARG;
+    switch (pts_src) {
+        case PTS_SRC_AUDIO:
+            if (audioFrame)
+                res = audioFrame->GetPacketTime(&bmd_pts, time_base.den);
+            break;
+        case PTS_SRC_VIDEO:
+            if (videoFrame)
+                res = videoFrame->GetStreamTime(&bmd_pts, &bmd_duration, time_base.den);
+            break;
+        case PTS_SRC_REFERENCE:
+            if (videoFrame)
+                res = videoFrame->GetHardwareReferenceTimestamp(time_base.den, &bmd_pts, &bmd_duration);
+            break;
+        case PTS_SRC_WALLCLOCK:
+            pts = av_rescale_q(wallclock, AV_TIME_BASE_Q, time_base);
+            break;
+    }
+    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;
+
+    return pts;
+}
+
 HRESULT decklink_input_callback::VideoInputFrameArrived(
     IDeckLinkVideoInputFrame *videoFrame, IDeckLinkAudioInputPacket *audioFrame)
 {
@@ -244,8 +285,11 @@ HRESULT decklink_input_callback::VideoInputFrameArrived(
     void *audioFrameBytes;
     BMDTimeValue frameTime;
     BMDTimeValue frameDuration;
+    int64_t wallclock = 0;
 
     ctx->frameCount++;
+    if (ctx->audio_pts_source == PTS_SRC_WALLCLOCK || ctx->video_pts_source == PTS_SRC_WALLCLOCK)
+        wallclock = av_gettime_relative();
 
     // Handle Video Frame
     if (videoFrame) {
@@ -292,13 +336,7 @@ HRESULT decklink_input_callback::VideoInputFrameArrived(
             no_video = 0;
         }
 
-        pkt.pts = frameTime / ctx->video_st->time_base.num;
-
-        if (initial_video_pts == AV_NOPTS_VALUE) {
-            initial_video_pts = pkt.pts;
-        }
-
-        pkt.pts -= initial_video_pts;
+        pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, ctx->video_pts_source, ctx->video_st->time_base, &initial_video_pts);
         pkt.dts = pkt.pts;
 
         pkt.duration = frameDuration;
@@ -368,13 +406,7 @@ HRESULT decklink_input_callback::VideoInputFrameArrived(
         pkt.size = audioFrame->GetSampleFrameCount() * ctx->audio_st->codecpar->channels * (16 / 8);
         audioFrame->GetBytes(&audioFrameBytes);
         audioFrame->GetPacketTime(&audio_pts, ctx->audio_st->time_base.den);
-        pkt.pts = audio_pts / ctx->audio_st->time_base.num;
-
-        if (initial_audio_pts == AV_NOPTS_VALUE) {
-            initial_audio_pts = pkt.pts;
-        }
-
-        pkt.pts -= initial_audio_pts;
+        pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, ctx->audio_pts_source, ctx->audio_st->time_base, &initial_audio_pts);
         pkt.dts = pkt.pts;
 
         //fprintf(stderr,"Audio Frame size %d ts %d\n", pkt.size, pkt.pts);
@@ -451,6 +483,8 @@ av_cold int ff_decklink_read_header(AVFormatContext *avctx)
         ctx->video_input = decklink_video_connection_map[cctx->video_input];
     if (cctx->audio_input > 0 && (unsigned int)cctx->audio_input < FF_ARRAY_ELEMS(decklink_audio_connection_map))
         ctx->audio_input = decklink_audio_connection_map[cctx->audio_input];
+    ctx->audio_pts_source = cctx->audio_pts_source;
+    ctx->video_pts_source = cctx->video_pts_source;
     cctx->ctx = ctx;
 
 #if !CONFIG_LIBZVBI