History

avdevice/decklink: add support for setting input packet timestamp source

Marton Balint committed 10 years ago

Commit cf0a0561 parent da89c6e6 files · +74 −15Browse files

Showing the changes to libavdevice/decklink_dec.cpp (1 of 6 files). Show all

Unified · Split

libavdevice/decklink_dec.cpp+48−14
@@ -28,8 +28,11 @@ extern "C" { · 27 unmodified lines
2828 #include "config.h"
2929 #include "libavformat/avformat.h"
3030 #include "libavformat/internal.h"
31+#include "libavutil/avutil.h"
3132 #include "libavutil/common.h"
3233 #include "libavutil/imgutils.h"
34+#include "libavutil/time.h"
35+#include "libavutil/mathematics.h"
3336 #if CONFIG_LIBZVBI
3437 #include <libzvbi.h>
3538 #endif
@@ -237,6 +240,44 @@ ULONG decklink_input_callback::Release(void) · 201 unmodified lines
237240 return (ULONG)m_refCount;
238241 }
239242
243+static int64_t get_pkt_pts(IDeckLinkVideoInputFrame *videoFrame,
244+ IDeckLinkAudioInputPacket *audioFrame,
245+ int64_t wallclock,
246+ DecklinkPtsSource pts_src,
247+ AVRational time_base, int64_t *initial_pts)
248+{
249+ int64_t pts = AV_NOPTS_VALUE;
250+ BMDTimeValue bmd_pts;
251+ BMDTimeValue bmd_duration;
252+ HRESULT res = E_INVALIDARG;
253+ switch (pts_src) {
254+ case PTS_SRC_AUDIO:
255+ if (audioFrame)
256+ res = audioFrame->GetPacketTime(&bmd_pts, time_base.den);
257+ break;
258+ case PTS_SRC_VIDEO:
259+ if (videoFrame)
260+ res = videoFrame->GetStreamTime(&bmd_pts, &bmd_duration, time_base.den);
261+ break;
262+ case PTS_SRC_REFERENCE:
263+ if (videoFrame)
264+ res = videoFrame->GetHardwareReferenceTimestamp(time_base.den, &bmd_pts, &bmd_duration);
265+ break;
266+ case PTS_SRC_WALLCLOCK:
267+ pts = av_rescale_q(wallclock, AV_TIME_BASE_Q, time_base);
268+ break;
269+ }
270+ if (res == S_OK)
271+ pts = bmd_pts / time_base.num;
272+
273+ if (pts != AV_NOPTS_VALUE && *initial_pts == AV_NOPTS_VALUE)
274+ *initial_pts = pts;
275+ if (*initial_pts != AV_NOPTS_VALUE)
276+ pts -= *initial_pts;
277+
278+ return pts;
279+}
280+
240281 HRESULT decklink_input_callback::VideoInputFrameArrived(
241282 IDeckLinkVideoInputFrame *videoFrame, IDeckLinkAudioInputPacket *audioFrame)
242283 {
@@ -244,8 +285,11 @@ HRESULT decklink_input_callback::VideoInputFrameArrived( · 1 unmodified line
244285 void *audioFrameBytes;
245286 BMDTimeValue frameTime;
246287 BMDTimeValue frameDuration;
288+ int64_t wallclock = 0;
247289
248290 ctx->frameCount++;
291+ if (ctx->audio_pts_source == PTS_SRC_WALLCLOCK || ctx->video_pts_source == PTS_SRC_WALLCLOCK)
292+ wallclock = av_gettime_relative();
249293
250294 // Handle Video Frame
251295 if (videoFrame) {
@@ -292,13 +336,7 @@ HRESULT decklink_input_callback::VideoInputFrameArrived( · 40 unmodified lines
292336 no_video = 0;
293337 }
294338
295− pkt.pts = frameTime / ctx->video_st->time_base.num;
296−
297− if (initial_video_pts == AV_NOPTS_VALUE) {
298− initial_video_pts = pkt.pts;
299− }
300−
301− pkt.pts -= initial_video_pts;
339+ pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, ctx->video_pts_source, ctx->video_st->time_base, &initial_video_pts);
302340 pkt.dts = pkt.pts;
303341
304342 pkt.duration = frameDuration;
@@ -368,13 +406,7 @@ HRESULT decklink_input_callback::VideoInputFrameArrived( · 63 unmodified lines
368406 pkt.size = audioFrame->GetSampleFrameCount() * ctx->audio_st->codecpar->channels * (16 / 8);
369407 audioFrame->GetBytes(&audioFrameBytes);
370408 audioFrame->GetPacketTime(&audio_pts, ctx->audio_st->time_base.den);
371− pkt.pts = audio_pts / ctx->audio_st->time_base.num;
372−
373− if (initial_audio_pts == AV_NOPTS_VALUE) {
374− initial_audio_pts = pkt.pts;
375− }
376−
377− pkt.pts -= initial_audio_pts;
409+ pkt.pts = get_pkt_pts(videoFrame, audioFrame, wallclock, ctx->audio_pts_source, ctx->audio_st->time_base, &initial_audio_pts);
378410 pkt.dts = pkt.pts;
379411
380412 //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) · 70 unmodified lines
451483 ctx->video_input = decklink_video_connection_map[cctx->video_input];
452484 if (cctx->audio_input > 0 && (unsigned int)cctx->audio_input < FF_ARRAY_ELEMS(decklink_audio_connection_map))
453485 ctx->audio_input = decklink_audio_connection_map[cctx->audio_input];
486+ ctx->audio_pts_source = cctx->audio_pts_source;
487+ ctx->video_pts_source = cctx->video_pts_source;
454488 cctx->ctx = ctx;
455489
456490 #if !CONFIG_LIBZVBI

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t