#undef main /* We don't want SDL to override our main() */
#endif
+#include <unistd.h>
+#include <assert.h>
+
const char program_name[] = "FFplay";
const int program_birth_year = 2003;
/* no AV correction is done if too big error */
#define AV_NOSYNC_THRESHOLD 10.0
+#define FRAME_SKIP_FACTOR 0.05
+
/* maximum audio speed change to get correct sync */
#define SAMPLE_CORRECTION_PERCENT_MAX 10
typedef struct VideoPicture {
double pts; ///<presentation time stamp for this picture
+ double target_clock; ///<av_gettime() time at which this should be displayed ideally
int64_t pos; ///<byte position in file
SDL_Overlay *bmp;
int width, height; /* source height & width */
int allocated;
- SDL_TimerID timer_id;
enum PixelFormat pix_fmt;
#if CONFIG_AVFILTER
typedef struct VideoState {
SDL_Thread *parse_tid;
SDL_Thread *video_tid;
+ SDL_Thread *refresh_tid;
AVInputFormat *iformat;
int no_background;
int abort_request;
#if CONFIG_AVFILTER
AVFilterContext *out_video_filter; ///<the last filter in the video chain
#endif
+
+ float skip_frames;
+ float skip_frames_index;
+ int refresh;
} VideoState;
static void show_help(void);
static int error_concealment = 3;
static int decoder_reorder_pts= -1;
static int autoexit;
+static int framedrop=1;
+
+static int rdftspeed=20;
#if CONFIG_AVFILTER
static char *vfilters = NULL;
#endif
video_image_display(is);
}
-static Uint32 sdl_refresh_timer_cb(Uint32 interval, void *opaque)
+static int refresh_thread(void *opaque)
{
+ VideoState *is= opaque;
+ while(!is->abort_request){
SDL_Event event;
event.type = FF_REFRESH_EVENT;
event.user.data1 = opaque;
+ if(!is->refresh){
+ is->refresh=1;
SDL_PushEvent(&event);
- return 0; /* 0 means stop timer */
-}
-
-/* schedule a video refresh in 'delay' ms */
-static SDL_TimerID schedule_refresh(VideoState *is, int delay)
-{
- if(!delay) delay=1; //SDL seems to be buggy when the delay is 0
- return SDL_AddTimer(delay, sdl_refresh_timer_cb, is);
+ }
+ usleep(is->audio_st && is->show_audio ? rdftspeed*1000 : 5000); //FIXME ideally we should wait the correct time but SDLs event passing is so slow it would be silly
+ }
+ return 0;
}
/* get the current audio clock value */
is->paused = !is->paused;
}
-static double compute_frame_delay(double frame_current_pts, VideoState *is)
+static double compute_target_time(double frame_current_pts, VideoState *is)
{
- double actual_delay, delay, sync_threshold, diff;
+ double delay, sync_threshold, diff;
/* compute nominal delay */
delay = frame_current_pts - is->frame_last_pts;
delay = 2 * delay;
}
}
-
is->frame_timer += delay;
- /* compute the REAL delay (we need to do that to avoid
- long term errors */
- actual_delay = is->frame_timer - (av_gettime() / 1000000.0);
- if (actual_delay < 0.010) {
- /* XXX: should skip picture */
- actual_delay = 0.010;
- }
-
#if defined(DEBUG_SYNC)
printf("video: delay=%0.3f actual_delay=%0.3f pts=%0.3f A-V=%f\n",
delay, actual_delay, frame_current_pts, -diff);
#endif
- return actual_delay;
+ return is->frame_timer;
}
/* called to display each frame */
SubPicture *sp, *sp2;
if (is->video_st) {
+retry:
if (is->pictq_size == 0) {
- fprintf(stderr, "Internal error detected in the SDL timer\n");
+ //nothing to do, no picture to display in the que
} else {
+ double time= av_gettime()/1000000.0;
+ double next_target;
/* dequeue the picture */
vp = &is->pictq[is->pictq_rindex];
+ if(time < vp->target_clock)
+ return;
/* update current video pts */
is->video_current_pts = vp->pts;
- is->video_current_pts_drift = is->video_current_pts - av_gettime() / 1000000.0;
+ is->video_current_pts_drift = is->video_current_pts - time;
is->video_current_pos = vp->pos;
+ if(is->pictq_size > 1){
+ VideoPicture *nextvp= &is->pictq[(is->pictq_rindex+1)%VIDEO_PICTURE_QUEUE_SIZE];
+ assert(nextvp->target_clock >= vp->target_clock);
+ next_target= nextvp->target_clock;
+ }else{
+ next_target= vp->target_clock + is->video_clock - vp->pts; //FIXME pass durations cleanly
+ }
+ if(framedrop && time > next_target){
+ is->skip_frames *= 1.0 + FRAME_SKIP_FACTOR;
+ if(is->pictq_size > 1 || time > next_target + 0.5){
+ /* update queue size and signal for next picture */
+ if (++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE)
+ is->pictq_rindex = 0;
+
+ SDL_LockMutex(is->pictq_mutex);
+ is->pictq_size--;
+ SDL_CondSignal(is->pictq_cond);
+ SDL_UnlockMutex(is->pictq_mutex);
+ goto retry;
+ }
+ }
if(is->subtitle_st) {
if (is->subtitle_stream_changed) {
is->pictq_rindex = 0;
SDL_LockMutex(is->pictq_mutex);
- vp->timer_id= 0;
is->pictq_size--;
SDL_CondSignal(is->pictq_cond);
SDL_UnlockMutex(is->pictq_mutex);
} else if (is->audio_st) {
/* draw the next audio frame */
- schedule_refresh(is, 40);
-
/* if only audio stream, then display the audio bars (better
than nothing, just to test the implementation */
/* display picture */
video_display(is);
- } else {
- schedule_refresh(is, 100);
}
if (show_status) {
static int64_t last_time;
av_diff = 0;
if (is->audio_st && is->video_st)
av_diff = get_audio_clock(is) - get_video_clock(is);
- printf("%7.2f A-V:%7.3f aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r",
- get_master_clock(is), av_diff, aqsize / 1024, vqsize / 1024, sqsize, is->faulty_dts, is->faulty_pts);
+ printf("%7.2f A-V:%7.3f s:%3.1f aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r",
+ get_master_clock(is), av_diff, FFMAX(is->skip_frames-1, 0), aqsize / 1024, vqsize / 1024, sqsize, is->faulty_dts, is->faulty_pts);
fflush(stdout);
last_time = cur_time;
}
#endif
/* wait until we have space to put a new picture */
SDL_LockMutex(is->pictq_mutex);
+
+ if(is->pictq_size>=VIDEO_PICTURE_QUEUE_SIZE && !is->refresh)
+ is->skip_frames= FFMAX(1.0 - FRAME_SKIP_FACTOR, is->skip_frames * (1.0-FRAME_SKIP_FACTOR));
+
while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE &&
!is->videoq.abort_request) {
SDL_CondWait(is->pictq_cond, is->pictq_mutex);
if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
is->pictq_windex = 0;
SDL_LockMutex(is->pictq_mutex);
+ vp->target_clock= compute_target_time(vp->pts, is);
+
is->pictq_size++;
- //We must schedule in a mutex as we must store the timer id before the timer dies or might end up freeing a alraedy freed id
- vp->timer_id= schedule_refresh(is, (int)(compute_frame_delay(vp->pts, is) * 1000 + 0.5));
SDL_UnlockMutex(is->pictq_mutex);
}
return 0;
SDL_LockMutex(is->pictq_mutex);
//Make sure there are no long delay timers (ideally we should just flush the que but thats harder)
for(i=0; i<VIDEO_PICTURE_QUEUE_SIZE; i++){
- if(is->pictq[i].timer_id){
- if(SDL_RemoveTimer(is->pictq[i].timer_id)){
- is->pictq[i].timer_id=0;
- schedule_refresh(is, 1);
- }
- }
+ is->pictq[i].target_clock= 0;
}
while (is->pictq_size && !is->videoq.abort_request) {
SDL_CondWait(is->pictq_cond, is->pictq_mutex);
is->frame_last_pts= AV_NOPTS_VALUE;
is->frame_last_delay = 0;
is->frame_timer = (double)av_gettime() / 1000000.0;
-
+ is->skip_frames= 1;
+ is->skip_frames_index= 0;
return 0;
}
// if (len1 < 0)
// break;
- if (got_picture)
- return 1;
+ if (got_picture){
+ is->skip_frames_index += 1;
+ if(is->skip_frames_index >= is->skip_frames){
+ is->skip_frames_index -= FFMAX(is->skip_frames, 1.0);
+ return 1;
+ }
+
+ }
return 0;
}
av_free_packet(&pkt);
picref->pts = pts;
+ picref->pos = pkt.pos;
picref->pixel_aspect = priv->is->video_st->codec->sample_aspect_ratio;
avfilter_start_frame(link, avfilter_ref_pic(picref, ~0));
avfilter_draw_slice(link, 0, link->h, 1);
}
static int get_filtered_video_frame(AVFilterContext *ctx, AVFrame *frame,
- int64_t *pts)
+ int64_t *pts, int64_t *pos)
{
AVFilterPicRef *pic;
frame->opaque = pic;
*pts = pic->pts;
+ *pos = pic->pos;
memcpy(frame->data, pic->data, sizeof(frame->data));
memcpy(frame->linesize, pic->linesize, sizeof(frame->linesize));
{
VideoState *is = arg;
AVFrame *frame= avcodec_alloc_frame();
- int64_t pts_int;
+ int64_t pts_int, pos;
double pts;
int ret;
while (is->paused && !is->videoq.abort_request)
SDL_Delay(10);
#if CONFIG_AVFILTER
- ret = get_filtered_video_frame(filt_out, frame, &pts_int);
+ ret = get_filtered_video_frame(filt_out, frame, &pts_int, &pos);
#else
ret = get_video_frame(is, frame, &pts_int, &pkt);
#endif
pts = pts_int*av_q2d(is->video_st->time_base);
#if CONFIG_AVFILTER
- ret = output_picture2(is, frame, pts, -1); /* fixme: unknown pos */
+ ret = output_picture2(is, frame, pts, pos);
#else
ret = output_picture2(is, frame, pts, pkt.pos);
av_free_packet(&pkt);
if (st_index[CODEC_TYPE_VIDEO] >= 0) {
ret= stream_component_open(is, st_index[CODEC_TYPE_VIDEO]);
}
+ is->refresh_tid = SDL_CreateThread(refresh_thread, is);
if(ret<0) {
- /* add the refresh timer to draw the picture */
- schedule_refresh(is, 40);
-
if (!display_disable)
is->show_audio = 2;
}
/* XXX: use a special url_shutdown call to abort parse cleanly */
is->abort_request = 1;
SDL_WaitThread(is->parse_tid, NULL);
+ SDL_WaitThread(is->refresh_tid, NULL);
/* free all pictures */
for(i=0;i<VIDEO_PICTURE_QUEUE_SIZE; i++) {
break;
case FF_REFRESH_EVENT:
video_refresh_timer(event.user.data1);
+ cur_stream->refresh=0;
break;
default:
break;
{ "sync", HAS_ARG | OPT_FUNC2 | OPT_EXPERT, {(void*)opt_sync}, "set audio-video sync. type (type=audio/video/ext)", "type" },
{ "threads", HAS_ARG | OPT_FUNC2 | OPT_EXPERT, {(void*)opt_thread_count}, "thread count", "count" },
{ "autoexit", OPT_BOOL | OPT_EXPERT, {(void*)&autoexit}, "exit at the end", "" },
+ { "framedrop", OPT_BOOL | OPT_EXPERT, {(void*)&framedrop}, "drop frames when cpu is too slow", "" },
#if CONFIG_AVFILTER
{ "vfilters", OPT_STRING | HAS_ARG, {(void*)&vfilters}, "video filters", "filter list" },
#endif
+ { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, {(void*)&rdftspeed}, "rdft speed", "msecs" },
{ "default", OPT_FUNC2 | HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
{ NULL, },
};