/* maximum audio speed change to get correct sync */
#define SAMPLE_CORRECTION_PERCENT_MAX 10
+/* external clock speed adjustment constants for realtime sources based on buffer fullness */
+#define EXTERNAL_CLOCK_SPEED_MIN 0.900
+#define EXTERNAL_CLOCK_SPEED_MAX 1.010
+#define EXTERNAL_CLOCK_SPEED_STEP 0.001
+
/* we use about AUDIO_DIFF_AVG_NB A-V differences to make the average */
#define AUDIO_DIFF_AVG_NB 20
int64_t seek_rel;
int read_pause_return;
AVFormatContext *ic;
+ int realtime;
int audio_stream;
double external_clock; ///< external clock base
double external_clock_drift; ///< external clock base - time (av_gettime) at which we updated external_clock
int64_t external_clock_time; ///< last reference time
+ double external_clock_speed; ///< speed of the external clock
double audio_clock;
double audio_diff_cum; /* used for AV difference average computation */
double video_current_pts; // current displayed pts (different from video_clock if frame fifos are used)
double video_current_pts_drift; // video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
int64_t video_current_pos; // current displayed file pos
+ double max_frame_duration; // maximum duration of a frame - above this, we consider the jump a timestamp discontinuity
VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
int pictq_size, pictq_rindex, pictq_windex;
SDL_mutex *pictq_mutex;
if (is->paused) {
return is->external_clock;
} else {
- return is->external_clock_drift + av_gettime() / 1000000.0;
+ double time = av_gettime() / 1000000.0;
+ return is->external_clock_drift + time - (time - is->external_clock_time / 1000000.0) * (1.0 - is->external_clock_speed);
}
}
}
}
+static void update_external_clock_speed(VideoState *is, double speed) {
+ update_external_clock_pts(is, get_external_clock(is));
+ is->external_clock_speed = speed;
+}
+
+static void check_external_clock_speed(VideoState *is) {
+ if (is->video_stream >= 0 && is->videoq.nb_packets <= MIN_FRAMES / 2 ||
+ is->audio_stream >= 0 && is->audioq.nb_packets <= MIN_FRAMES / 2) {
+ update_external_clock_speed(is, FFMAX(EXTERNAL_CLOCK_SPEED_MIN, is->external_clock_speed - EXTERNAL_CLOCK_SPEED_STEP));
+ } else if ((is->video_stream < 0 || is->videoq.nb_packets > MIN_FRAMES * 2) &&
+ (is->audio_stream < 0 || is->audioq.nb_packets > MIN_FRAMES * 2)) {
+ update_external_clock_speed(is, FFMIN(EXTERNAL_CLOCK_SPEED_MAX, is->external_clock_speed + EXTERNAL_CLOCK_SPEED_STEP));
+ } else {
+ double speed = is->external_clock_speed;
+ if (speed != 1.0)
+ update_external_clock_speed(is, speed + EXTERNAL_CLOCK_SPEED_STEP * (1.0 - speed) / fabs(1.0 - speed));
+ }
+}
+
/* seek in the stream */
static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
{
SubPicture *sp, *sp2;
+ if (!is->paused && get_master_sync_type(is) == AV_SYNC_EXTERNAL_CLOCK && is->realtime)
+ check_external_clock_speed(is);
+
if (is->video_st) {
if (is->force_refresh)
pictq_prev_picture(is);
/* compute nominal last_duration */
last_duration = vp->pts - is->frame_last_pts;
- if (last_duration > 0 && last_duration < 10.0) {
+ if (last_duration > 0 && last_duration < is->max_frame_duration) {
/* if duration of the last frame was sane, update last_duration in video state */
is->frame_last_duration = last_duration;
}
if (seek_by_bytes < 0)
seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT);
+ is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
+
/* if seeking requested, we execute it */
if (start_time != AV_NOPTS_VALUE) {
int64_t timestamp;
}
}
+ is->realtime = is_realtime(ic);
+
for (i = 0; i < ic->nb_streams; i++)
ic->streams[i]->discard = AVDISCARD_ALL;
if (!video_disable)
goto fail;
}
- if (infinite_buffer < 0 && is_realtime(ic))
+ if (infinite_buffer < 0 && is->realtime)
infinite_buffer = 1;
for (;;) {
packet_queue_flush(&is->videoq);
packet_queue_put(&is->videoq, &flush_pkt);
}
+ if (is->seek_flags & AVSEEK_FLAG_BYTE) {
+ //FIXME: use a cleaner way to signal obsolete external clock...
+ update_external_clock_pts(is, (double)AV_NOPTS_VALUE);
+ } else {
+ update_external_clock_pts(is, seek_target / (double)AV_TIME_BASE);
+ }
}
- update_external_clock_pts(is, (seek_target + ic->start_time) / (double)AV_TIME_BASE);
is->seek_req = 0;
eof = 0;
}
is->continue_read_thread = SDL_CreateCond();
update_external_clock_pts(is, 0.0);
+ update_external_clock_speed(is, 1.0);
is->audio_current_pts_drift = -av_gettime() / 1000000.0;
is->video_current_pts_drift = is->audio_current_pts_drift;
is->av_sync_type = av_sync_type;
} else {
pos = get_master_clock(cur_stream);
pos += incr;
+ if (cur_stream->ic->start_time != AV_NOPTS_VALUE && pos < cur_stream->ic->start_time / (double)AV_TIME_BASE)
+ pos = cur_stream->ic->start_time / (double)AV_TIME_BASE;
stream_seek(cur_stream, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE), 0);
}
break;