From 4a45e722e373c85cae7b5d57fb6bb7e295e664e0 Mon Sep 17 00:00:00 2001 From: Marton Balint Date: Sun, 26 Aug 2012 15:18:00 +0200 Subject: [PATCH 1/3] ffplay: requeue last picture on forced video refresh Fixes ticket #1609. Signed-off-by: Marton Balint --- ffplay.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/ffplay.c b/ffplay.c index 9efc93ae3d..2157131f21 100644 --- a/ffplay.c +++ b/ffplay.c @@ -95,7 +95,7 @@ typedef struct PacketQueue { SDL_cond *cond; } PacketQueue; -#define VIDEO_PICTURE_QUEUE_SIZE 2 +#define VIDEO_PICTURE_QUEUE_SIZE 3 #define SUBPICTURE_QUEUE_SIZE 4 typedef struct VideoPicture { @@ -1132,6 +1132,22 @@ static void pictq_next_picture(VideoState *is) { SDL_UnlockMutex(is->pictq_mutex); } +static void pictq_prev_picture(VideoState *is) { + VideoPicture *prevvp; + /* update queue size and signal for the previous picture */ + prevvp = &is->pictq[(is->pictq_rindex + VIDEO_PICTURE_QUEUE_SIZE - 1) % VIDEO_PICTURE_QUEUE_SIZE]; + if (prevvp->allocated && !prevvp->skip) { + SDL_LockMutex(is->pictq_mutex); + if (is->pictq_size < VIDEO_PICTURE_QUEUE_SIZE) { + if (--is->pictq_rindex == -1) + is->pictq_rindex = VIDEO_PICTURE_QUEUE_SIZE - 1; + is->pictq_size++; + } + SDL_CondSignal(is->pictq_cond); + SDL_UnlockMutex(is->pictq_mutex); + } +} + static void update_video_pts(VideoState *is, double pts, int64_t pos) { double time = av_gettime() / 1000000.0; /* update current video pts */ @@ -1251,8 +1267,7 @@ display: if (!display_disable) video_display(is); - if (!is->paused) - pictq_next_picture(is); + pictq_next_picture(is); } } else if (is->audio_st) { /* draw the next audio frame */ @@ -1368,7 +1383,8 @@ static int queue_picture(VideoState *is, AVFrame *src_frame, double pts1, int64_ /* wait until we have space to put a new picture */ SDL_LockMutex(is->pictq_mutex); - while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE && + /* keep the last already displayed picture in the queue */ + while (is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE - 1 && !is->videoq.abort_request) { SDL_CondWait(is->pictq_cond, is->pictq_mutex); } @@ -2872,6 +2888,8 @@ static void event_loop(VideoState *cur_stream) alloc_picture(event.user.data1); break; case FF_REFRESH_EVENT: + if (cur_stream->force_refresh) + pictq_prev_picture(event.user.data1); video_refresh(event.user.data1); cur_stream->refresh = 0; break; From 0e5042be284182823223a8c3d0006298ba16c921 Mon Sep 17 00:00:00 2001 From: Marton Balint Date: Tue, 28 Aug 2012 00:00:15 +0200 Subject: [PATCH 2/3] ffplay: replace SDL_delay in read thread with SDL_CondWait When the audio queue was empty, it was not filled until the 10ms delay expired in the read thread. This patch changes the delay method with a condition wait, which reacts to an empty queue a lot faster, therefore the audio buffer underruns become less common especially after seeking. Signed-off-by: Marton Balint --- ffplay.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/ffplay.c b/ffplay.c index 2157131f21..861080c2b9 100644 --- a/ffplay.c +++ b/ffplay.c @@ -237,6 +237,8 @@ typedef struct VideoState { int refresh; int last_video_stream, last_audio_stream, last_subtitle_stream; + + SDL_cond *continue_read_thread; } VideoState; typedef struct AllocEventProps { @@ -919,6 +921,7 @@ static void stream_close(VideoState *is) SDL_DestroyCond(is->pictq_cond); SDL_DestroyMutex(is->subpq_mutex); SDL_DestroyCond(is->subpq_cond); + SDL_DestroyCond(is->continue_read_thread); #if !CONFIG_AVFILTER if (is->img_convert_ctx) sws_freeContext(is->img_convert_ctx); @@ -2055,6 +2058,9 @@ static int audio_decode_frame(VideoState *is, double *pts_ptr) return -1; } + if (is->audioq.nb_packets == 0) + SDL_CondSignal(is->continue_read_thread); + /* read next packet */ if ((new_packet = packet_queue_get(&is->audioq, pkt, 1)) < 0) return -1; @@ -2371,6 +2377,7 @@ static int read_thread(void *arg) AVDictionaryEntry *t; AVDictionary **opts; int orig_nb_streams; + SDL_mutex *wait_mutex = SDL_CreateMutex(); memset(st_index, -1, sizeof(st_index)); is->last_video_stream = is->video_stream = -1; @@ -2538,7 +2545,9 @@ static int read_thread(void *arg) && (is->videoq .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request) && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request)))) { /* wait 10 ms */ - SDL_Delay(10); + SDL_LockMutex(wait_mutex); + SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10); + SDL_UnlockMutex(wait_mutex); continue; } if (eof) { @@ -2619,6 +2628,7 @@ static int read_thread(void *arg) event.user.data1 = is; SDL_PushEvent(&event); } + SDL_DestroyMutex(wait_mutex); return 0; } @@ -2645,6 +2655,8 @@ static VideoState *stream_open(const char *filename, AVInputFormat *iformat) packet_queue_init(&is->audioq); packet_queue_init(&is->subtitleq); + is->continue_read_thread = SDL_CreateCond(); + is->av_sync_type = av_sync_type; is->read_tid = SDL_CreateThread(read_thread, is); if (!is->read_tid) { From 55594447d635a89d6b0183299f2bf8e27e8a0d42 Mon Sep 17 00:00:00 2001 From: Marton Balint Date: Wed, 29 Aug 2012 23:56:33 +0200 Subject: [PATCH 3/3] ffplay: only free vfilters on exit Freeing it in the end of the video thread is not a good idea, because we still may need the filter names for the next video thread, in order to apply the filters after chaning the video stream. Signed-off-by: Marton Balint --- ffplay.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ffplay.c b/ffplay.c index 861080c2b9..ce19378fd5 100644 --- a/ffplay.c +++ b/ffplay.c @@ -938,6 +938,7 @@ static void do_exit(VideoState *is) uninit_opts(); #if CONFIG_AVFILTER avfilter_uninit(); + av_freep(&vfilters); #endif avformat_network_deinit(); if (show_status) @@ -1790,7 +1791,6 @@ static int video_thread(void *arg) the_end: avcodec_flush_buffers(is->video_st->codec); #if CONFIG_AVFILTER - av_freep(&vfilters); avfilter_graph_free(&graph); #endif av_free_packet(&pkt);