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

Reviewed-by: Deti Fliegl <deti@fliegl.de>
Signed-off-by: Marton Balint <cus@passwd.hu>
This commit is contained in:
Marton Balint 2016-06-19 00:35:44 +02:00
parent da89c6e37c
commit cf0a05668b
6 changed files with 74 additions and 15 deletions

View File

@ -265,6 +265,14 @@ Sets the audio input source. Must be @samp{unset}, @samp{embedded},
@samp{aes_ebu}, @samp{analog}, @samp{analog_xlr}, @samp{analog_rca} or
@samp{microphone}. Defaults to @samp{unset}.
@item video_pts
Sets the video packet timestamp source. Must be @samp{video}, @samp{audio},
@samp{reference} or @samp{wallclock}. Defaults to @samp{video}.
@item audio_pts
Sets the audio packet timestamp source. Must be @samp{video}, @samp{audio},
@samp{reference} or @samp{wallclock}. Defaults to @samp{audio}.
@end table
@subsection Examples

View File

@ -82,6 +82,8 @@ struct decklink_ctx {
int64_t teletext_lines;
double preroll;
int duplex_mode;
DecklinkPtsSource audio_pts_source;
DecklinkPtsSource video_pts_source;
int frames_preroll;
int frames_buffer;

View File

@ -22,6 +22,13 @@
#ifndef AVDEVICE_DECKLINK_COMMON_C_H
#define AVDEVICE_DECKLINK_COMMON_C_H
typedef enum DecklinkPtsSource {
PTS_SRC_AUDIO = 1,
PTS_SRC_VIDEO = 2,
PTS_SRC_REFERENCE = 3,
PTS_SRC_WALLCLOCK = 4,
} DecklinkPtsSource;
struct decklink_cctx {
const AVClass *cclass;
@ -35,6 +42,8 @@ struct decklink_cctx {
int v210;
int audio_channels;
int duplex_mode;
DecklinkPtsSource audio_pts_source;
DecklinkPtsSource video_pts_source;
int audio_input;
int video_input;
};

View 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

View File

@ -56,6 +56,12 @@ static const AVOption options[] = {
{ "analog_xlr", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 4}, 0, 0, DEC, "audio_input"},
{ "analog_rca", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 5}, 0, 0, DEC, "audio_input"},
{ "microphone", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 6}, 0, 0, DEC, "audio_input"},
{ "audio_pts", "audio pts source", OFFSET(audio_pts_source), AV_OPT_TYPE_INT, { .i64 = PTS_SRC_AUDIO }, 1, 4, DEC, "pts_source"},
{ "video_pts", "video pts source", OFFSET(video_pts_source), AV_OPT_TYPE_INT, { .i64 = PTS_SRC_VIDEO }, 1, 4, DEC, "pts_source"},
{ "audio", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PTS_SRC_AUDIO }, 0, 0, DEC, "pts_source"},
{ "video", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PTS_SRC_VIDEO }, 0, 0, DEC, "pts_source"},
{ "reference", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PTS_SRC_REFERENCE}, 0, 0, DEC, "pts_source"},
{ "wallclock", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PTS_SRC_WALLCLOCK}, 0, 0, DEC, "pts_source"},
{ NULL },
};

View File

@ -29,7 +29,7 @@
#define LIBAVDEVICE_VERSION_MAJOR 57
#define LIBAVDEVICE_VERSION_MINOR 0
#define LIBAVDEVICE_VERSION_MICRO 101
#define LIBAVDEVICE_VERSION_MICRO 102
#define LIBAVDEVICE_VERSION_INT AV_VERSION_INT(LIBAVDEVICE_VERSION_MAJOR, \
LIBAVDEVICE_VERSION_MINOR, \