From 5f58ef3a4dab739b2575905d0ccb4f233071615b Mon Sep 17 00:00:00 2001 From: nick black Date: Tue, 14 Jan 2020 15:56:45 -0500 Subject: [PATCH] implement ncvisual timescaling #275 --- src/lib/internal.h | 1 + src/lib/libav.c | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lib/internal.h b/src/lib/internal.h index 231e93502..aa02bb613 100644 --- a/src/lib/internal.h +++ b/src/lib/internal.h @@ -80,6 +80,7 @@ typedef struct ncvisual { int packet_outstanding; int dstwidth, dstheight; int stream_index; // match against this following av_read_frame() + float timescale; // scale frame duration by this value ncplane* ncp; // if we're creating the plane based off the first frame's dimensions, these // describe where the plane ought be placed, and how it ought be sized. this diff --git a/src/lib/libav.c b/src/lib/libav.c index 2567cb3f1..0b2d9491e 100644 --- a/src/lib/libav.c +++ b/src/lib/libav.c @@ -39,12 +39,13 @@ bool notcurses_canopen(const notcurses* nc __attribute__ ((unused))){ } static ncvisual* -ncvisual_create(void){ +ncvisual_create(float timescale){ ncvisual* ret = malloc(sizeof(*ret)); if(ret == NULL){ return NULL; } memset(ret, 0, sizeof(*ret)); + ret->timescale = timescale; return ret; } @@ -195,7 +196,7 @@ AVFrame* ncvisual_decode(ncvisual* nc, int* averr){ static ncvisual* ncvisual_open(const char* filename, int* averr){ - ncvisual* ncv = ncvisual_create(); + ncvisual* ncv = ncvisual_create(1); if(ncv == NULL){ // fprintf(stderr, "Couldn't create %s (%s)\n", filename, strerror(errno)); *averr = AVERROR(ENOMEM); @@ -383,6 +384,7 @@ int ncvisual_render(const ncvisual* ncv, int begy, int begx, int leny, int lenx) int ncvisual_stream(notcurses* nc, ncvisual* ncv, int* averr, float timescale, streamcb streamer, void* curry){ int frame = 1; + ncv->timescale = timescale; AVFrame* avf; struct timespec begin; // time we started clock_gettime(CLOCK_MONOTONIC, &begin); @@ -415,13 +417,13 @@ int ncvisual_stream(notcurses* nc, ncvisual* ncv, int* averr, struct timespec interval; uint64_t duration = avf->pkt_duration * tbase * NANOSECS_IN_SEC; //fprintf(stderr, "use: %u dur: %ju ts: %ju cctx: %f fctx: %f\n", usets, duration, ts, av_q2d(ncv->codecctx->time_base), av_q2d(ncv->fmtctx->streams[ncv->stream_index]->time_base)); - sum_duration += duration; + sum_duration += (duration * ncv->timescale); double schedns = nsbegin; if(usets){ if(tbase == 0){ tbase = duration; } - schedns += ts * tbase * NANOSECS_IN_SEC; + schedns += ts * (tbase * ncv->timescale) * NANOSECS_IN_SEC; }else{ schedns += sum_duration; }