From db040d4ea167f23139d571662e79a4478700025e Mon Sep 17 00:00:00 2001 From: "Alfred E. Heggestad" Date: Mon, 26 Dec 2016 16:13:33 +0100 Subject: avformat: sleep frame duration (refs #163) --- modules/avformat/avformat.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'modules') diff --git a/modules/avformat/avformat.c b/modules/avformat/avformat.c index 12c6b2d..9767303 100644 --- a/modules/avformat/avformat.c +++ b/modules/avformat/avformat.c @@ -58,11 +58,11 @@ struct vidsrc_st { AVFormatContext *ic; AVCodec *codec; AVCodecContext *ctx; + AVRational time_base; struct vidsz sz; vidsrc_frame_h *frameh; void *arg; int sindex; - int fps; }; @@ -97,6 +97,7 @@ static void handle_packet(struct vidsrc_st *st, AVPacket *pkt) struct vidframe vf; struct vidsz sz; unsigned i; + double dur; if (st->codec) { int got_pict, ret; @@ -166,6 +167,10 @@ static void handle_packet(struct vidsrc_st *st, AVPacket *pkt) st->frameh(&vf, st->arg); + /* simulate framerate (NOTE: not accurate) */ + dur = 1.0 * av_frame_get_pkt_duration(frame) * av_q2d(st->time_base); + sys_msleep(1000.0 * dur); + out: if (frame) { #if LIBAVUTIL_VERSION_INT >= ((52<<16)+(20<<8)+100) @@ -200,9 +205,6 @@ static void *read_thread(void *data) handle_packet(st, &pkt); - /* simulate framerate */ - sys_msleep(1000/st->fps); - out: #if LIBAVCODEC_VERSION_INT >= ((57<<16)+(12<<8)+100) av_packet_unref(&pkt); @@ -243,7 +245,6 @@ static int alloc(struct vidsrc_st **stp, const struct vidsrc *vs, st->sz = *size; st->frameh = frameh; st->arg = arg; - st->fps = prm->fps; /* * avformat_open_input() was added in lavf 53.2.0 according to @@ -258,7 +259,7 @@ static int alloc(struct vidsrc_st **stp, const struct vidsrc *vs, /* Params */ memset(&prms, 0, sizeof(prms)); - prms.time_base = (AVRational){1, st->fps}; + prms.time_base = (AVRational){1, prm->fps}; prms.channels = 1; prms.width = size->w; prms.height = size->h; @@ -325,6 +326,7 @@ static int alloc(struct vidsrc_st **stp, const struct vidsrc *vs, st->sz.h = ctx->height; st->ctx = ctx; st->sindex = strm->index; + st->time_base = strm->time_base; if (ctx->codec_id != AV_CODEC_ID_NONE) { -- cgit v1.2.3