From: Anton Khirnov <anton@khirnov.net> To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH 20/21] fftools/ffmpeg: use AVFrame to pass subtitles from decoders to filters Date: Wed, 14 Jun 2023 18:49:07 +0200 Message-ID: <20230614164908.28712-20-anton@khirnov.net> (raw) In-Reply-To: <20230614164908.28712-1-anton@khirnov.net> Allows to use the same buffering code for all media types. Will also be important for the following commit. --- fftools/ffmpeg.h | 2 +- fftools/ffmpeg_dec.c | 88 ++++++++++++++++++++++++++--------------- fftools/ffmpeg_filter.c | 51 ++++++------------------ 3 files changed, 70 insertions(+), 71 deletions(-) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 3da5bc4f7c..316cd2b7a6 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -736,7 +736,7 @@ FrameData *frame_data(AVFrame *frame); int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame, int keep_reference); int ifilter_send_eof(InputFilter *ifilter, int64_t pts, AVRational tb); -int ifilter_sub2video(InputFilter *ifilter, const AVSubtitle *sub); +int ifilter_sub2video(InputFilter *ifilter, const AVFrame *frame); void ifilter_sub2video_heartbeat(InputFilter *ifilter, int64_t pts, AVRational tb); /** diff --git a/fftools/ffmpeg_dec.c b/fftools/ffmpeg_dec.c index 36e28aaa07..e0c0038f50 100644 --- a/fftools/ffmpeg_dec.c +++ b/fftools/ffmpeg_dec.c @@ -47,11 +47,9 @@ struct Decoder { int64_t last_filter_in_rescale_delta; int last_frame_sample_rate; - /* previous decoded subtitle and related variables */ - struct { - int got_output; - AVSubtitle subtitle; - } prev_sub; + /* previous decoded subtitles */ + AVFrame *sub_prev[2]; + AVFrame *sub_heartbeat; pthread_t thread; /** @@ -108,7 +106,9 @@ void dec_free(Decoder **pdec) av_frame_free(&dec->frame); av_packet_free(&dec->pkt); - avsubtitle_free(&dec->prev_sub.subtitle); + for (int i = 0; i < FF_ARRAY_ELEMS(dec->sub_prev); i++) + av_frame_free(&dec->sub_prev[i]); + av_frame_free(&dec->sub_heartbeat); av_freep(pdec); } @@ -384,45 +384,55 @@ static void sub2video_flush(InputStream *ist) } } -static int process_subtitle(InputStream *ist, AVSubtitle *subtitle) +static int process_subtitle(InputStream *ist, AVFrame *frame) { Decoder *d = ist->decoder; - int got_output = 1; + const AVSubtitle *subtitle = (AVSubtitle*)frame->buf[0]->data; int ret = 0; if (ist->fix_sub_duration) { + AVSubtitle *sub_prev = d->sub_prev[0]->buf[0] ? + (AVSubtitle*)d->sub_prev[0]->buf[0]->data : NULL; int end = 1; - if (d->prev_sub.got_output) { - end = av_rescale(subtitle->pts - d->prev_sub.subtitle.pts, + if (sub_prev) { + end = av_rescale(subtitle->pts - sub_prev->pts, 1000, AV_TIME_BASE); - if (end < d->prev_sub.subtitle.end_display_time) { + if (end < sub_prev->end_display_time) { av_log(NULL, AV_LOG_DEBUG, "Subtitle duration reduced from %"PRId32" to %d%s\n", - d->prev_sub.subtitle.end_display_time, end, + sub_prev->end_display_time, end, end <= 0 ? ", dropping it" : ""); - d->prev_sub.subtitle.end_display_time = end; + sub_prev->end_display_time = end; } } - FFSWAP(int, got_output, d->prev_sub.got_output); - FFSWAP(AVSubtitle, *subtitle, d->prev_sub.subtitle); + + av_frame_unref(d->sub_prev[1]); + av_frame_move_ref(d->sub_prev[1], frame); + + frame = d->sub_prev[0]; + subtitle = frame->buf[0] ? (AVSubtitle*)frame->buf[0]->data : NULL; + + FFSWAP(AVFrame*, d->sub_prev[0], d->sub_prev[1]); + if (end <= 0) - goto out; + return 0; } - if (!got_output) + if (!subtitle) return 0; for (int i = 0; i < ist->nb_filters; i++) { - ret = ifilter_sub2video(ist->filters[i], subtitle); + ret = ifilter_sub2video(ist->filters[i], frame); if (ret < 0) { av_log(ist, AV_LOG_ERROR, "Error sending a subtitle for filtering: %s\n", av_err2str(ret)); - goto out; + return ret; } } + subtitle = (AVSubtitle*)frame->buf[0]->data; if (!subtitle->num_rects) - goto out; + return 0; for (int oidx = 0; oidx < ist->nb_outputs; oidx++) { OutputStream *ost = ist->outputs[oidx]; @@ -432,28 +442,30 @@ static int process_subtitle(InputStream *ist, AVSubtitle *subtitle) enc_subtitle(output_files[ost->file_index], ost, subtitle); } -out: - avsubtitle_free(subtitle); - return ret; + return 0; } int fix_sub_duration_heartbeat(InputStream *ist, int64_t signal_pts) { Decoder *d = ist->decoder; int ret = AVERROR_BUG; - AVSubtitle *prev_subtitle = &d->prev_sub.subtitle; - AVSubtitle subtitle; + AVSubtitle *prev_subtitle = d->sub_prev[0]->buf[0] ? + (AVSubtitle*)d->sub_prev[0]->buf[0]->data : NULL; + AVSubtitle *subtitle; - if (!ist->fix_sub_duration || !prev_subtitle->num_rects || - signal_pts <= prev_subtitle->pts) + if (!ist->fix_sub_duration || !prev_subtitle || + !prev_subtitle->num_rects || signal_pts <= prev_subtitle->pts) return 0; - if ((ret = copy_av_subtitle(&subtitle, prev_subtitle)) < 0) + av_frame_unref(d->sub_heartbeat); + ret = subtitle_wrap_frame(d->sub_heartbeat, prev_subtitle, 1); + if (ret < 0) return ret; - subtitle.pts = signal_pts; + subtitle = (AVSubtitle*)d->sub_heartbeat->buf[0]->data; + subtitle->pts = signal_pts; - return process_subtitle(ist, &subtitle); + return process_subtitle(ist, d->sub_heartbeat); } static int transcode_subtitles(InputStream *ist, const AVPacket *pkt, @@ -825,8 +837,7 @@ int dec_packet(InputStream *ist, const AVPacket *pkt, int no_eof) // process the decoded frame if (ist->dec->type == AVMEDIA_TYPE_SUBTITLE) { - AVSubtitle *sub = (AVSubtitle*)d->frame->buf[0]->data; - ret = process_subtitle(ist, sub); + ret = process_subtitle(ist, d->frame); } else { ret = send_frame_to_filters(ist, d->frame); } @@ -1043,6 +1054,7 @@ static int hw_device_setup_for_decode(InputStream *ist) int dec_open(InputStream *ist) { + Decoder *d; const AVCodec *codec = ist->dec; int ret; @@ -1056,6 +1068,18 @@ int dec_open(InputStream *ist) ret = dec_alloc(&ist->decoder); if (ret < 0) return ret; + d = ist->decoder; + + if (codec->type == AVMEDIA_TYPE_SUBTITLE && ist->fix_sub_duration) { + for (int i = 0; i < FF_ARRAY_ELEMS(d->sub_prev); i++) { + d->sub_prev[i] = av_frame_alloc(); + if (!d->sub_prev[i]) + return AVERROR(ENOMEM); + } + d->sub_heartbeat = av_frame_alloc(); + if (!d->sub_heartbeat) + return AVERROR(ENOMEM); + } ist->dec_ctx->opaque = ist; ist->dec_ctx->get_format = get_format; diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c index 9e6883ccdd..acc8596836 100644 --- a/fftools/ffmpeg_filter.c +++ b/fftools/ffmpeg_filter.c @@ -118,9 +118,6 @@ typedef struct InputFilterPriv { } fallback; struct { - ///< queue of AVSubtitle* before filter init - AVFifo *queue; - AVFrame *frame; int64_t last_pts; @@ -749,12 +746,6 @@ void fg_free(FilterGraph **pfg) av_frame_free(&frame); av_fifo_freep2(&ifp->frame_queue); } - if (ifp->sub2video.queue) { - AVSubtitle sub; - while (av_fifo_read(ifp->sub2video.queue, &sub, 1) >= 0) - avsubtitle_free(&sub); - av_fifo_freep2(&ifp->sub2video.queue); - } av_frame_free(&ifp->sub2video.frame); av_channel_layout_uninit(&ifp->fallback.ch_layout); @@ -1593,7 +1584,11 @@ static int configure_filtergraph(FilterGraph *fg) InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]); AVFrame *tmp; while (av_fifo_read(ifp->frame_queue, &tmp, 1) >= 0) { - ret = av_buffersrc_add_frame(ifp->filter, tmp); + if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE) { + sub2video_update(ifp, INT64_MIN, (const AVSubtitle*)tmp->buf[0]->data); + } else { + ret = av_buffersrc_add_frame(ifp->filter, tmp); + } av_frame_free(&tmp); if (ret < 0) goto fail; @@ -1610,20 +1605,6 @@ static int configure_filtergraph(FilterGraph *fg) } } - /* process queued up subtitle packets */ - for (i = 0; i < fg->nb_inputs; i++) { - InputFilter *ifilter = fg->inputs[i]; - InputFilterPriv *ifp = ifp_from_ifilter(ifilter); - - if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE && ifp->sub2video.queue) { - AVSubtitle tmp; - while (av_fifo_read(ifp->sub2video.queue, &tmp, 1) >= 0) { - sub2video_update(ifp, INT64_MIN, &tmp); - avsubtitle_free(&tmp); - } - } - } - return 0; fail: @@ -1797,35 +1778,29 @@ void ifilter_sub2video_heartbeat(InputFilter *ifilter, int64_t pts, AVRational t sub2video_push_ref(ifp, pts2); } -int ifilter_sub2video(InputFilter *ifilter, const AVSubtitle *subtitle) +int ifilter_sub2video(InputFilter *ifilter, const AVFrame *frame) { InputFilterPriv *ifp = ifp_from_ifilter(ifilter); int ret; if (ifilter->graph->graph) { - if (!subtitle) { + if (!frame) { if (ifp->sub2video.end_pts < INT64_MAX) sub2video_update(ifp, INT64_MAX, NULL); return av_buffersrc_add_frame(ifp->filter, NULL); } - sub2video_update(ifp, INT64_MIN, subtitle); - } else if (subtitle) { - AVSubtitle sub; + sub2video_update(ifp, INT64_MIN, (const AVSubtitle*)frame->buf[0]->data); + } else if (frame) { + AVFrame *tmp = av_frame_clone(frame); - if (!ifp->sub2video.queue) - ifp->sub2video.queue = av_fifo_alloc2(8, sizeof(AVSubtitle), AV_FIFO_FLAG_AUTO_GROW); - if (!ifp->sub2video.queue) + if (!tmp) return AVERROR(ENOMEM); - ret = copy_av_subtitle(&sub, subtitle); - if (ret < 0) - return ret; - - ret = av_fifo_write(ifp->sub2video.queue, &sub, 1); + ret = av_fifo_write(ifp->frame_queue, &tmp, 1); if (ret < 0) { - avsubtitle_free(&sub); + av_frame_free(&tmp); return ret; } } -- 2.40.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply other threads:[~2023-06-14 16:51 UTC|newest] Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-06-14 16:48 [FFmpeg-devel] [PATCH 01/21] fftools/ffmpeg_dec: drop always-0 InputStream.prev_sub.ret Anton Khirnov 2023-06-14 16:48 ` [FFmpeg-devel] [PATCH 02/21] fftools/ffmpeg_dec: simplify process_subtitle() Anton Khirnov 2023-06-14 16:48 ` [FFmpeg-devel] [PATCH 03/21] tests/fate: rename ffmpeg-streamloop to ffmpeg-streamloop-copy Anton Khirnov 2023-06-14 16:48 ` [FFmpeg-devel] [PATCH 04/21] tests/fate: add a test for -streamloop with transcoding video+audio Anton Khirnov 2023-06-14 16:57 ` Andreas Rheinhardt 2023-06-16 6:01 ` [FFmpeg-devel] [PATCH 04/21 v2] " Anton Khirnov 2023-06-19 10:52 ` "zhilizhao(赵志立)" 2023-06-19 12:29 ` James Almer 2023-06-19 12:34 ` "zhilizhao(赵志立)" 2023-06-19 13:22 ` James Almer 2023-06-19 13:39 ` Zhao Zhili 2023-06-19 15:48 ` Lynne 2023-06-19 20:33 ` Martin Storsjö 2023-06-14 16:48 ` [FFmpeg-devel] [PATCH 05/21] fftools/ffmpeg_demux: move the loop out of add_input_streams() Anton Khirnov 2023-06-14 16:48 ` [FFmpeg-devel] [PATCH 06/21] fftools/ffmpeg_demux: reindent after previous commit Anton Khirnov 2023-06-14 16:48 ` [FFmpeg-devel] [PATCH 07/21] fftools/ffmpeg_hw: inline hwaccel_decode_init() into its caller Anton Khirnov 2023-06-14 16:48 ` [FFmpeg-devel] [PATCH 08/21] fftools/ffmpeg_dec: remove pointless InputStream.hwaccel_retrieve_data Anton Khirnov 2023-06-14 16:48 ` [FFmpeg-devel] [PATCH 09/21] fftools/ffmpeg_dec: move InputStream.hwaccel_pix_fmt to Decoder Anton Khirnov 2023-06-14 16:48 ` [FFmpeg-devel] [PATCH 10/21] fftools/ffmpeg_enc: move dup_warning global variable to Encoder Anton Khirnov 2023-06-14 16:48 ` [FFmpeg-devel] [PATCH 11/21] fftools/ffmpeg_filter: add an AVClass to FilterGraph Anton Khirnov 2023-06-14 16:48 ` [FFmpeg-devel] [PATCH 12/21] fftools/ffmpeg_filter: reject filtergraphs with zero outputs Anton Khirnov 2023-06-14 16:53 ` Paul B Mahol 2023-06-14 17:00 ` Anton Khirnov 2023-06-14 17:11 ` Paul B Mahol 2023-06-14 17:14 ` Anton Khirnov 2023-06-14 16:49 ` [FFmpeg-devel] [PATCH 13/21] fftools/ffmpeg_filter: make configure_filtergraph() static Anton Khirnov 2023-06-14 16:49 ` [FFmpeg-devel] [PATCH 14/21] fftools/ffmpeg_dec: stop using Decoder.pkt Anton Khirnov 2023-06-14 16:49 ` [FFmpeg-devel] [PATCH 15/21] fftools/ffmpeg: attach bits_per_raw_sample information to frames Anton Khirnov 2023-06-14 16:49 ` [FFmpeg-devel] [PATCH 16/21] fftools/ffmpeg_dec: move decoding to a separate thread Anton Khirnov 2023-06-16 20:58 ` Michael Niedermayer 2023-06-17 2:55 ` Anton Khirnov 2023-06-14 16:49 ` [FFmpeg-devel] [PATCH 17/21] fftools/ffmpeg: move fix_sub_duration_heartbeat() to ffmpeg_dec Anton Khirnov 2023-06-14 16:49 ` [FFmpeg-devel] [PATCH 18/21] fftools/ffmpeg_dec: move InputStream.prev_sub to Decoder Anton Khirnov 2023-06-14 16:49 ` [FFmpeg-devel] [PATCH 19/21] fftools/ffmpeg_enc: constify the subtitle passed to enc_subtitle() Anton Khirnov 2023-06-14 16:49 ` Anton Khirnov [this message] 2023-06-14 16:49 ` [FFmpeg-devel] [PATCH 21/21] fftools/ffmpeg: pass subtitle decoder dimensions to sub2video Anton Khirnov 2023-06-18 8:35 ` [FFmpeg-devel] [PATCH 01/21] fftools/ffmpeg_dec: drop always-0 InputStream.prev_sub.ret Anton Khirnov
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20230614164908.28712-20-anton@khirnov.net \ --to=anton@khirnov.net \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git