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

Unified · Split

6 files
  1. doc/indevs.texi +8 −0
  2. libavdevice/decklink_common.h +2 −0
  3. libavdevice/decklink_common_c.h +9 −0
  4. libavdevice/decklink_dec.cpp +48 −14
  5. libavdevice/decklink_dec_c.c +6 −0
  6. libavdevice/version.h +1 −1
doc/indevs.texi+8−0
@@ -265,6 +265,14 @@ Sets the audio input source. Must be @samp{unset}, @samp{embedded}, · 264 unmodified lines
265265 @samp{aes_ebu}, @samp{analog}, @samp{analog_xlr}, @samp{analog_rca} or
266266 @samp{microphone}. Defaults to @samp{unset}.
267267
268+@item video_pts
269+Sets the video packet timestamp source. Must be @samp{video}, @samp{audio},
270+@samp{reference} or @samp{wallclock}. Defaults to @samp{video}.
271+
272+@item audio_pts
273+Sets the audio packet timestamp source. Must be @samp{video}, @samp{audio},
274+@samp{reference} or @samp{wallclock}. Defaults to @samp{audio}.
275+
268276 @end table
269277
270278 @subsection Examples
libavdevice/decklink_common.h+2−0
@@ -82,6 +82,8 @@ struct decklink_ctx { · 81 unmodified lines
8282 int64_t teletext_lines;
8383 double preroll;
8484 int duplex_mode;
85+ DecklinkPtsSource audio_pts_source;
86+ DecklinkPtsSource video_pts_source;
8587
8688 int frames_preroll;
8789 int frames_buffer;
libavdevice/decklink_common_c.h+9−0
@@ -22,6 +22,13 @@ · 21 unmodified lines
2222 #ifndef AVDEVICE_DECKLINK_COMMON_C_H
2323 #define AVDEVICE_DECKLINK_COMMON_C_H
2424
25+typedef enum DecklinkPtsSource {
26+ PTS_SRC_AUDIO = 1,
27+ PTS_SRC_VIDEO = 2,
28+ PTS_SRC_REFERENCE = 3,
29+ PTS_SRC_WALLCLOCK = 4,
30+} DecklinkPtsSource;
31+
2532 struct decklink_cctx {
2633 const AVClass *cclass;
2734
@@ -35,6 +42,8 @@ struct decklink_cctx { · 7 unmodified lines
3542 int v210;
3643 int audio_channels;
3744 int duplex_mode;
45+ DecklinkPtsSource audio_pts_source;
46+ DecklinkPtsSource video_pts_source;
3847 int audio_input;
3948 int video_input;
4049 };
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
libavdevice/decklink_dec_c.c+6−0
@@ -56,6 +56,12 @@ static const AVOption options[] = { · 55 unmodified lines
5656 { "analog_xlr", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 4}, 0, 0, DEC, "audio_input"},
5757 { "analog_rca", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 5}, 0, 0, DEC, "audio_input"},
5858 { "microphone", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 6}, 0, 0, DEC, "audio_input"},
59+ { "audio_pts", "audio pts source", OFFSET(audio_pts_source), AV_OPT_TYPE_INT, { .i64 = PTS_SRC_AUDIO }, 1, 4, DEC, "pts_source"},
60+ { "video_pts", "video pts source", OFFSET(video_pts_source), AV_OPT_TYPE_INT, { .i64 = PTS_SRC_VIDEO }, 1, 4, DEC, "pts_source"},
61+ { "audio", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PTS_SRC_AUDIO }, 0, 0, DEC, "pts_source"},
62+ { "video", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PTS_SRC_VIDEO }, 0, 0, DEC, "pts_source"},
63+ { "reference", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PTS_SRC_REFERENCE}, 0, 0, DEC, "pts_source"},
64+ { "wallclock", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PTS_SRC_WALLCLOCK}, 0, 0, DEC, "pts_source"},
5965 { NULL },
6066 };
6167
libavdevice/version.h+1−1
@@ -29,7 +29,7 @@ · 28 unmodified lines
2929
3030 #define LIBAVDEVICE_VERSION_MAJOR 57
3131 #define LIBAVDEVICE_VERSION_MINOR 0
32−#define LIBAVDEVICE_VERSION_MICRO 101
32+#define LIBAVDEVICE_VERSION_MICRO 102
3333
3434 #define LIBAVDEVICE_VERSION_INT AV_VERSION_INT(LIBAVDEVICE_VERSION_MAJOR, \
3535 LIBAVDEVICE_VERSION_MINOR, \

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