From 325846aac0aefef177d5ef06d5a558c281ead348 Mon Sep 17 00:00:00 2001 From: Marton Balint Date: Sun, 31 Mar 2013 23:53:15 +0200 Subject: [PATCH 1/4] ffplay: set time_base of audio filter buffer src Fixes ticket #2408. Signed-off-by: Marton Balint --- ffplay.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/ffplay.c b/ffplay.c index eee9da3576..2aaef3590b 100644 --- a/ffplay.c +++ b/ffplay.c @@ -1828,9 +1828,10 @@ static int configure_audio_filters(VideoState *is, const char *afilters, int for return AVERROR(ENOMEM); ret = snprintf(asrc_args, sizeof(asrc_args), - "sample_rate=%d:sample_fmt=%s:channels=%d", + "sample_rate=%d:sample_fmt=%s:channels=%d:time_base=%d/%d", is->audio_filter_src.freq, av_get_sample_fmt_name(is->audio_filter_src.fmt), - is->audio_filter_src.channels); + is->audio_filter_src.channels, + 1, is->audio_filter_src.freq); if (is->audio_filter_src.channel_layout) snprintf(asrc_args + ret, sizeof(asrc_args) - ret, ":channel_layout=0x%"PRIx64, is->audio_filter_src.channel_layout); @@ -2190,11 +2191,13 @@ static int audio_decode_frame(VideoState *is) continue; } + tb = (AVRational){1, is->frame->sample_rate}; + if (is->frame->pts != AV_NOPTS_VALUE) + is->frame->pts = av_rescale_q(is->frame->pts, dec->time_base, tb); if (is->frame->pts == AV_NOPTS_VALUE && pkt_temp->pts != AV_NOPTS_VALUE) - is->frame->pts = av_rescale_q(pkt_temp->pts, is->audio_st->time_base, dec->time_base); + is->frame->pts = av_rescale_q(pkt_temp->pts, is->audio_st->time_base, tb); if (pkt_temp->pts != AV_NOPTS_VALUE) pkt_temp->pts += (double) is->frame->nb_samples / is->frame->sample_rate / av_q2d(is->audio_st->time_base); - tb = dec->time_base; #if CONFIG_AVFILTER { From 0b24e341ed75573d1abfe13dec6fb4b8a37e94d3 Mon Sep 17 00:00:00 2001 From: Marton Balint Date: Mon, 1 Apr 2013 20:26:22 +0200 Subject: [PATCH 2/4] ffplay: handle audio buffersink output properly with buffering filters Fixes cases when the audio filter generates less or more frames than the input. Signed-off-by: Marton Balint --- ffplay.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/ffplay.c b/ffplay.c index 2aaef3590b..fc8bc4e82d 100644 --- a/ffplay.c +++ b/ffplay.c @@ -188,6 +188,7 @@ typedef struct VideoState { unsigned int audio_buf1_size; int audio_buf_index; /* in bytes */ int audio_write_buf_size; + int audio_buf_frames_pending; AVPacket audio_pkt_temp; AVPacket audio_pkt; int audio_pkt_temp_serial; @@ -2153,10 +2154,12 @@ static int audio_decode_frame(VideoState *is) int flush_complete = 0; int wanted_nb_samples; AVRational tb; + int ret; + int reconfigure; for (;;) { /* NOTE: the audio packet can contain several frames */ - while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet)) { + while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet) || is->audio_buf_frames_pending) { if (!is->frame) { if (!(is->frame = avcodec_alloc_frame())) return AVERROR(ENOMEM); @@ -2171,6 +2174,8 @@ static int audio_decode_frame(VideoState *is) if (is->paused) return -1; + if (!is->audio_buf_frames_pending) { + if (flush_complete) break; new_packet = 0; @@ -2200,10 +2205,6 @@ static int audio_decode_frame(VideoState *is) pkt_temp->pts += (double) is->frame->nb_samples / is->frame->sample_rate / av_q2d(is->audio_st->time_base); #if CONFIG_AVFILTER - { - int ret; - int reconfigure; - dec_channel_layout = get_valid_channel_layout(is->frame->channel_layout, av_frame_get_channels(is->frame)); reconfigure = @@ -2235,10 +2236,18 @@ static int audio_decode_frame(VideoState *is) if ((ret = av_buffersrc_add_frame(is->in_audio_filter, is->frame)) < 0) return ret; av_frame_unref(is->frame); - if ((ret = av_buffersink_get_frame_flags(is->out_audio_filter, is->frame, 0)) < 0) - return ret; - tb = is->out_audio_filter->inputs[0]->time_base; +#endif + } +#if CONFIG_AVFILTER + if ((ret = av_buffersink_get_frame_flags(is->out_audio_filter, is->frame, 0)) < 0) { + if (ret == AVERROR(EAGAIN)) { + is->audio_buf_frames_pending = 0; + continue; + } + return ret; } + is->audio_buf_frames_pending = 1; + tb = is->out_audio_filter->inputs[0]->time_base; #endif data_size = av_samples_get_buffer_size(NULL, av_frame_get_channels(is->frame), @@ -2340,6 +2349,7 @@ static int audio_decode_frame(VideoState *is) if (pkt->data == flush_pkt.data) { avcodec_flush_buffers(dec); flush_complete = 0; + is->audio_buf_frames_pending = 0; } *pkt_temp = *pkt; From 5dacf7b1ab40276c4d56b86081cbad7552a85f70 Mon Sep 17 00:00:00 2001 From: Marton Balint Date: Mon, 1 Apr 2013 21:10:25 +0200 Subject: [PATCH 3/4] ffplay: fix indentation Signed-off-by: Marton Balint --- ffplay.c | 49 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/ffplay.c b/ffplay.c index fc8bc4e82d..dd530b6a24 100644 --- a/ffplay.c +++ b/ffplay.c @@ -2175,34 +2175,33 @@ static int audio_decode_frame(VideoState *is) return -1; if (!is->audio_buf_frames_pending) { + if (flush_complete) + break; + new_packet = 0; + len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp); + if (len1 < 0) { + /* if error, we skip the frame */ + pkt_temp->size = 0; + break; + } - if (flush_complete) - break; - new_packet = 0; - len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp); - if (len1 < 0) { - /* if error, we skip the frame */ - pkt_temp->size = 0; - break; - } - - pkt_temp->data += len1; - pkt_temp->size -= len1; + pkt_temp->data += len1; + pkt_temp->size -= len1; - if (!got_frame) { - /* stop sending empty packets if the decoder is finished */ - if (!pkt_temp->data && dec->codec->capabilities & CODEC_CAP_DELAY) - flush_complete = 1; - continue; - } + if (!got_frame) { + /* stop sending empty packets if the decoder is finished */ + if (!pkt_temp->data && dec->codec->capabilities & CODEC_CAP_DELAY) + flush_complete = 1; + continue; + } - tb = (AVRational){1, is->frame->sample_rate}; - if (is->frame->pts != AV_NOPTS_VALUE) - is->frame->pts = av_rescale_q(is->frame->pts, dec->time_base, tb); - if (is->frame->pts == AV_NOPTS_VALUE && pkt_temp->pts != AV_NOPTS_VALUE) - is->frame->pts = av_rescale_q(pkt_temp->pts, is->audio_st->time_base, tb); - if (pkt_temp->pts != AV_NOPTS_VALUE) - pkt_temp->pts += (double) is->frame->nb_samples / is->frame->sample_rate / av_q2d(is->audio_st->time_base); + tb = (AVRational){1, is->frame->sample_rate}; + if (is->frame->pts != AV_NOPTS_VALUE) + is->frame->pts = av_rescale_q(is->frame->pts, dec->time_base, tb); + if (is->frame->pts == AV_NOPTS_VALUE && pkt_temp->pts != AV_NOPTS_VALUE) + is->frame->pts = av_rescale_q(pkt_temp->pts, is->audio_st->time_base, tb); + if (pkt_temp->pts != AV_NOPTS_VALUE) + pkt_temp->pts += (double) is->frame->nb_samples / is->frame->sample_rate / av_q2d(is->audio_st->time_base); #if CONFIG_AVFILTER dec_channel_layout = get_valid_channel_layout(is->frame->channel_layout, av_frame_get_channels(is->frame)); From 85b9bf5693c821bcfec89ea0964f52a7eeff233f Mon Sep 17 00:00:00 2001 From: Marton Balint Date: Mon, 1 Apr 2013 15:04:14 +0200 Subject: [PATCH 4/4] ffplay: simplify video pts calculation Also use frame->pts instead of a standalone variable Signed-off-by: Marton Balint --- ffplay.c | 41 ++++++++++++----------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/ffplay.c b/ffplay.c index dd530b6a24..13125391bb 100644 --- a/ffplay.c +++ b/ffplay.c @@ -1648,7 +1648,7 @@ static int queue_picture(VideoState *is, AVFrame *src_frame, double pts, int64_t return 0; } -static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacket *pkt, int *serial) +static int get_video_frame(VideoState *is, AVFrame *frame, AVPacket *pkt, int *serial) { int got_picture; @@ -1679,22 +1679,22 @@ static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacke int ret = 1; if (decoder_reorder_pts == -1) { - *pts = av_frame_get_best_effort_timestamp(frame); + frame->pts = av_frame_get_best_effort_timestamp(frame); } else if (decoder_reorder_pts) { - *pts = frame->pkt_pts; + frame->pts = frame->pkt_pts; } else { - *pts = frame->pkt_dts; + frame->pts = frame->pkt_dts; } - if (*pts == AV_NOPTS_VALUE) { - *pts = 0; + if (frame->pts == AV_NOPTS_VALUE) { + frame->pts = 0; } if (framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) { SDL_LockMutex(is->pictq_mutex); - if (is->frame_last_pts != AV_NOPTS_VALUE && *pts) { + if (is->frame_last_pts != AV_NOPTS_VALUE && frame->pts) { double clockdiff = get_video_clock(is) - get_master_clock(is); - double dpts = av_q2d(is->video_st->time_base) * *pts; + double dpts = av_q2d(is->video_st->time_base) * frame->pts; double ptsdiff = dpts - is->frame_last_pts; if (!isnan(clockdiff) && fabs(clockdiff) < AV_NOSYNC_THRESHOLD && ptsdiff > 0 && ptsdiff < AV_NOSYNC_THRESHOLD && @@ -1886,7 +1886,6 @@ static int video_thread(void *arg) AVPacket pkt = { 0 }; VideoState *is = arg; AVFrame *frame = av_frame_alloc(); - int64_t pts_int = AV_NOPTS_VALUE, pos = -1; double pts; int ret; int serial = 0; @@ -1901,19 +1900,15 @@ static int video_thread(void *arg) #endif for (;;) { -#if CONFIG_AVFILTER - AVRational tb; -#endif while (is->paused && !is->videoq.abort_request) SDL_Delay(10); avcodec_get_frame_defaults(frame); av_free_packet(&pkt); - ret = get_video_frame(is, frame, &pts_int, &pkt, &serial); + ret = get_video_frame(is, frame, &pkt, &serial); if (ret < 0) goto the_end; - if (!ret) continue; @@ -1946,7 +1941,6 @@ static int video_thread(void *arg) last_serial = serial; } - frame->pts = pts_int; frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame); ret = av_buffersrc_add_frame(filt_in, frame); if (ret < 0) @@ -1968,23 +1962,12 @@ static int video_thread(void *arg) if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0) is->frame_last_filter_delay = 0; - pts_int = frame->pts; - tb = filt_out->inputs[0]->time_base; - pos = av_frame_get_pkt_pos(frame); - if (av_cmp_q(tb, is->video_st->time_base)) { - av_unused int64_t pts1 = pts_int; - pts_int = av_rescale_q(pts_int, tb, is->video_st->time_base); - av_dlog(NULL, "video_thread(): " - "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n", - tb.num, tb.den, pts1, - is->video_st->time_base.num, is->video_st->time_base.den, pts_int); - } - pts = pts_int * av_q2d(is->video_st->time_base); - ret = queue_picture(is, frame, pts, pos, serial); + pts = frame->pts * av_q2d(filt_out->inputs[0]->time_base); + ret = queue_picture(is, frame, pts, av_frame_get_pkt_pos(frame), serial); av_frame_unref(frame); } #else - pts = pts_int * av_q2d(is->video_st->time_base); + pts = frame->pts * av_q2d(is->video_st->time_base); ret = queue_picture(is, frame, pts, pkt.pos, serial); av_frame_unref(frame); #endif