1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
/*****************************************************************
* gmerlin - a general purpose multimedia framework and applications
*
* Copyright (c) 2001 - 2024 Members of the Gmerlin project
* http://github.com/bplaum
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* *****************************************************************/
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <gavl/gavl.h>
#include <gmerlin/frametimer.h>
#define TIME_SCALE 1000
struct bg_frame_timer_s
{
int frame_duration;
gavl_timer_t * timer;
int64_t next_pts;
/* Only used when bg_frame_timer_wait() is used */
gavl_time_t capture_start_time;
gavl_time_t last_capture_duration;
gavl_time_t last_time;
};
bg_frame_timer_t * bg_frame_timer_create(float framerate,
uint32_t * timescale)
{
bg_frame_timer_t * ret;
ret = calloc(1, sizeof(*ret));
ret->timer = gavl_timer_create();
ret->next_pts = GAVL_TIME_UNDEFINED;
ret->frame_duration = (int)(TIME_SCALE / framerate);
*timescale = TIME_SCALE;
return ret;
}
void bg_frame_timer_destroy(bg_frame_timer_t * t)
{
gavl_timer_destroy(t->timer);
free(t);
}
void bg_frame_timer_update(bg_frame_timer_t * t,
gavl_video_frame_t * frame)
{
int64_t diff;
gavl_time_t current_time;
int64_t real_duration;
current_time = gavl_timer_get(t->timer);
t->last_capture_duration = current_time - t->capture_start_time;
if(t->next_pts == GAVL_TIME_UNDEFINED)
{
frame->timestamp = 0;
frame->duration = t->frame_duration;
gavl_timer_start(t->timer);
t->next_pts = frame->duration;
t->last_time = 0;
return;
}
frame->timestamp = t->next_pts;
/*
* diff: True time minus guessed time
*
* Diff < 0 -> frame too early: Guessed duration was too large
* Diff > 0 -> frame too late: Guessed duration was too small
*/
diff = gavl_time_scale(TIME_SCALE, current_time) - t->next_pts;
/* Real duration of the last frame */
real_duration =
gavl_time_scale(TIME_SCALE, current_time - t->last_time);
/* Duration of this frame is real duration of the last frame
plus the error */
frame->duration = real_duration + diff;
// fprintf(stderr, "Cur: %"PRId64", Last: %"PRId64", diff: %"PRId64"\n",
// current_time, t->last_time, current_time - t->last_time);
if(frame->duration <= 0)
frame->duration = TIME_SCALE / 100; // 10 ms/100 fps
t->last_time = current_time;
t->next_pts += frame->duration;
}
void bg_frame_timer_wait(bg_frame_timer_t * t)
{
gavl_time_t cur, diff;
if(t->next_pts == GAVL_TIME_UNDEFINED)
return;
cur = gavl_timer_get(t->timer);
diff =
gavl_time_unscale(TIME_SCALE, t->frame_duration) - (cur - t->last_time) -
t->last_capture_duration;
if(diff > 0)
gavl_time_delay(&diff);
t->capture_start_time = gavl_timer_get(t->timer);
}
|