From patchwork Fri Jul 29 13:35:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Jan_Ekstr=C3=B6m?= X-Patchwork-Id: 37039 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:1649:b0:8b:613a:194d with SMTP id no9csp892348pzb; Fri, 29 Jul 2022 06:36:04 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sUK82JeeIXs+ddjztR7VJiwukSoet8o5rL+YkIJB95RH8SVUc13tH++prfowxsMTPsoAKi X-Received: by 2002:a17:906:8a45:b0:72b:31d4:d537 with SMTP id gx5-20020a1709068a4500b0072b31d4d537mr2930914ejc.170.1659101764673; Fri, 29 Jul 2022 06:36:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659101764; cv=none; d=google.com; s=arc-20160816; b=yMz+zb9fG3ljZHcwnw+ik4udLDu1T/tZ76wSGAGuFytEPvEa11OJtIfVgSJyHJWgoe IoW/J0HS/KCyXzP6Z6IiRZH/Nc196SFUUImMViQOQMD41KrA84lz27cKWmBdT67utuhz abBPUYhCX30B9WjQplBYG7tIcHlUjp85XhVirRnob2IYsnkvLVnHNwTH5ePVlu1Ixb2C r7Z6mI2PxsR/Ies8ISYv1POLZDe9cBpzVeFxXesDrde6ARqc111lVNeL6NPyV6pAqLuQ 40+tAI4MUJ6Iees9J4OIVEuwBoipVBGAOfQ5iOC/FV+Ucp/E8SolRQNF4uWKSiNDgDNz kj8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=S8cwO8LTaDGXiDHKKtRdqmfOWPzK9mjmK6R4j78KJFg=; b=Z+M/wMMWxxec+fFZHe0vBEZbbrYxdUQ33isA3p6UqptdOqhb2CMHTqAlHSOJP4F20X JvcBMDg9W8sYmsHsRCJxMH0KNlKdC+RN/rIvQLVXo0jV92LRf65wupN2igj9oDtPRkWQ ZUB2DtsBH8KFSZKk2FcvZwTvT3B2njCeHvRHHWsv1tdzZPOD4i4G8rKcPZc/0P5ewCjT t+Glpwr+tOmJ5uQbJyHWePUgC3kHwGsQjh823wdKLN7B6g832fzAKzTA2C/l3ZzufOS1 LzgTwkwlwDFyTWF1oCL/hnuoolOYekmOsKDnEwx4uc1qFZUSsAkxR13DFhzSX2vQDPCX 9bqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=YCf10Bis; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id z60-20020a509e42000000b0043a4bc94f49si2247941ede.196.2022.07.29.06.36.04; Fri, 29 Jul 2022 06:36:04 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=YCf10Bis; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8141D68BA07; Fri, 29 Jul 2022 16:35:52 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lj1-f181.google.com (mail-lj1-f181.google.com [209.85.208.181]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9759568B985 for ; Fri, 29 Jul 2022 16:35:44 +0300 (EEST) Received: by mail-lj1-f181.google.com with SMTP id b21so5187313ljk.8 for ; Fri, 29 Jul 2022 06:35:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=CvU39fOf2gqx0PZQr7d/MyS7rdejoHNF32Q2RbzNKgo=; b=YCf10Bise6O/ZjropdTNs0uOzUNDZJy+rA3X3/Tl5BHkgCY9Hhb7IDy6kSf/aqORpv RDOofi/3OAjqTBszhI/ve8wfabByLt6SXyO1ixlLoCamEC/e3AT9Jwj9kzk3m6alhkzR dXc3jF4u1GPf+rTWsXBiKYxPHKHcmpaprxStHVRAGvKARYJCmPKV+Wpzs/B8rt+Pghii rYRgbsFY87+QBwQlJIWU/7jaCa6hIx7iamolYlbndzYPE9nPt4yCy/ZF2gE4HXBAsrg0 OJIqkVAX2/B7hMXqq0qDoMEj5RWd2sRNhDW7SK8ctdG+PqkH6u9SZVmYDle2HetTwrxR XSSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=CvU39fOf2gqx0PZQr7d/MyS7rdejoHNF32Q2RbzNKgo=; b=LR4fK5ygp5jSDmXJueJvRayiOLwqF0AfbE1t/GXRn0rvRe/rb/spcFxYsFxouCZi5V 8Pf94SjpmBa035arwcHjDyZ1lGgMSNxP+E0PDaKDIy5+LXZJLhsXfagMCD/MsHP5+alP V1Ors/h0HEDiXtYpEmrxpRi9I6DNJKjuZjjCG4tlxc+RTK0wLxVWhUPX4dtFhlLhQL2P 2/06zFwAryE8THZVe+V0HyVz/BHnm8gJSNaccGc62/RiENOO3z7cBo46B479aFdBLem3 KGQ5EDdKZESb9Epy7Cf87nf8+mwN/sEMVWp7loSrrzIH8G+ZUuC9DNuWz8vrGkt2p0aa 1wPA== X-Gm-Message-State: AJIora+FGglOcERRmpjVMbjR3CXnuz+Dbuv8XLJ9S+oZpNTIUEtZFwnL 8K/Xc+bXg3uJyGaTwtB8T+wUO5nY7FM= X-Received: by 2002:a2e:9d59:0:b0:25e:1eda:86f6 with SMTP id y25-20020a2e9d59000000b0025e1eda86f6mr1178486ljj.315.1659101743778; Fri, 29 Jul 2022 06:35:43 -0700 (PDT) Received: from localhost.localdomain (91-153-198-187.elisa-laajakaista.fi. [91.153.198.187]) by smtp.gmail.com with ESMTPSA id a3-20020a19e303000000b0048a68fe3745sm667589lfh.34.2022.07.29.06.35.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Jul 2022 06:35:43 -0700 (PDT) From: =?utf-8?q?Jan_Ekstr=C3=B6m?= To: ffmpeg-devel@ffmpeg.org Date: Fri, 29 Jul 2022 16:35:37 +0300 Message-Id: <20220729133537.53269-2-jeebjp@gmail.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220729133537.53269-1-jeebjp@gmail.com> References: <20220722134808.16692-1-jeebjp@gmail.com> <20220729133537.53269-1-jeebjp@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 2/2] ffmpeg: add video heartbeat capability to fix_sub_duration X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: SGoyEDbDkj/V From: Jan Ekström Splits the currently handled subtitle at random access point packets that can be configured to follow a specific output stream. This way the subtitle - which is known to be shown at this time can be split and passed to muxer before its full duration is yet known. Co-authored-by: Andrzej Nadachowski Co-authored-by: Bernard Boulay Signed-off-by: Jan Ekström --- doc/ffmpeg.texi | 11 ++ fftools/ffmpeg.c | 142 ++++++++++++++++++ fftools/ffmpeg.h | 8 + fftools/ffmpeg_opt.c | 9 ++ tests/fate/ffmpeg.mak | 14 ++ .../fate/ffmpeg-fix_sub_duration_heartbeat | 48 ++++++ 6 files changed, 232 insertions(+) create mode 100644 tests/ref/fate/ffmpeg-fix_sub_duration_heartbeat diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi index 2fb0bc8ffa..e3ace8ee65 100644 --- a/doc/ffmpeg.texi +++ b/doc/ffmpeg.texi @@ -1313,6 +1313,17 @@ List all hardware acceleration components enabled in this build of ffmpeg. Actual runtime availability depends on the hardware and its suitable driver being installed. +@item -fix_sub_duration_heartbeat[:@var{stream_specifier}] +Set a specific output video stream as the heartbeat stream according to which +to split and push through currently in-progress subtitle upon receipt of a +random access packet. + +This lowers the latency of subtitles for which the end packet or the following +subtitle has not yet been received. + +Requires @option{-fix_sub_duration} to be set for the relevant input subtitle +stream for this to have any effect. + @end table @section Audio Options diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index adcab43a30..e227c9d19e 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -128,6 +128,7 @@ typedef struct BenchmarkTimeStamps { int64_t sys_usec; } BenchmarkTimeStamps; +static int trigger_fix_sub_duration_heartbeat(OutputStream *ost, const AVPacket *pkt); static BenchmarkTimeStamps get_benchmark_time_stamps(void); static int64_t getmaxrss(void); static int ifilter_has_all_input_formats(FilterGraph *fg); @@ -981,6 +982,13 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame) av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base)); } + if ((ret = trigger_fix_sub_duration_heartbeat(ost, pkt)) < 0) { + av_log(NULL, AV_LOG_ERROR, + "Subtitle heartbeat logic failed in %s! (%s)\n", + __func__, av_err2str(ret)); + exit_program(1); + } + if (enc->codec_type == AVMEDIA_TYPE_VIDEO) update_video_stats(ost, pkt, !!vstats_filename); @@ -1931,6 +1939,16 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p ost->sync_opts += opkt->duration; + { + int ret = trigger_fix_sub_duration_heartbeat(ost, pkt); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, + "Subtitle heartbeat logic failed in %s! (%s)\n", + __func__, av_err2str(ret)); + exit_program(1); + } + } + output_packet(of, opkt, ost, 0); ost->streamcopy_started = 1; @@ -2365,6 +2383,130 @@ out: return ret; } +static int copy_av_subtitle(AVSubtitle *dst, AVSubtitle *src) +{ + int ret = AVERROR_BUG; + AVSubtitle tmp = { + .format = src->format, + .start_display_time = src->start_display_time, + .end_display_time = src->end_display_time, + .num_rects = 0, + .rects = NULL, + .pts = src->pts + }; + + if (!src->num_rects) + goto success; + + if (!(tmp.rects = av_calloc(src->num_rects, sizeof(*tmp.rects)))) + return AVERROR(ENOMEM); + + for (int i = 0; i < src->num_rects; i++) { + AVSubtitleRect *src_rect = src->rects[i]; + AVSubtitleRect *dst_rect; + + if (!(dst_rect = tmp.rects[i] = av_mallocz(sizeof(*tmp.rects[0])))) { + ret = AVERROR(ENOMEM); + goto cleanup; + } + + tmp.num_rects++; + + dst_rect->type = src_rect->type; + dst_rect->flags = src_rect->flags; + + dst_rect->x = src_rect->x; + dst_rect->y = src_rect->y; + dst_rect->w = src_rect->w; + dst_rect->h = src_rect->h; + dst_rect->nb_colors = src_rect->nb_colors; + + if (src_rect->text) + if (!(dst_rect->text = av_strdup(src_rect->text))) { + ret = AVERROR(ENOMEM); + goto cleanup; + } + + if (src_rect->ass) + if (!(dst_rect->ass = av_strdup(src_rect->ass))) { + ret = AVERROR(ENOMEM); + goto cleanup; + } + + for (int j = 0; j < 4; j++) { + // SUBTITLE_BITMAP images are special in the sense that they + // are like PAL8 images. first pointer to data, second to + // palette. This makes the size calculation match this. + size_t buf_size = src_rect->type == SUBTITLE_BITMAP && j == 1 ? + AVPALETTE_SIZE : + src_rect->h * src_rect->linesize[j]; + + if (!src_rect->data[j]) + continue; + + if (!(dst_rect->data[j] = av_memdup(src_rect->data[j], buf_size))) { + ret = AVERROR(ENOMEM); + goto cleanup; + } + dst_rect->linesize[j] = src_rect->linesize[j]; + } + } + +success: + *dst = tmp; + + return 0; + +cleanup: + avsubtitle_free(&tmp); + + return ret; +} + +static int fix_sub_duration_heartbeat(InputStream *ist, int64_t signal_pts) +{ + int ret = AVERROR_BUG; + int got_output = 1; + AVSubtitle *prev_subtitle = &ist->prev_sub.subtitle; + AVSubtitle subtitle; + + if (!ist->fix_sub_duration || !prev_subtitle->num_rects || + signal_pts <= prev_subtitle->pts) + return 0; + + if ((ret = copy_av_subtitle(&subtitle, prev_subtitle)) < 0) + return ret; + + subtitle.pts = signal_pts; + + return encode_mux_subtitles(ist, &subtitle, &got_output); +} + +static int trigger_fix_sub_duration_heartbeat(OutputStream *ost, const AVPacket *pkt) +{ + int64_t signal_pts = av_rescale_q(pkt->pts, ost->mux_timebase, + AV_TIME_BASE_Q); + + if (!ost->fix_sub_duration_heartbeat || !(pkt->flags & AV_PKT_FLAG_KEY)) + // we are only interested in heartbeats on streams configured, and + // only on random access points. + return 0; + + for (int index = 0; index < nb_input_streams; index++) { + InputStream *subtitle_ist = input_streams[index]; + int ret = AVERROR_BUG; + + if (!subtitle_ist->decoding_needed || + subtitle_ist->dec_ctx->codec_type != AVMEDIA_TYPE_SUBTITLE) + continue; + + if ((ret = fix_sub_duration_heartbeat(subtitle_ist, signal_pts)) < 0) + return ret; + } + + return 0; +} + static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output, int *decode_failed) { diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 6417db03bd..89c97f102f 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -220,6 +220,8 @@ typedef struct OptionsContext { int nb_reinit_filters; SpecifierOpt *fix_sub_duration; int nb_fix_sub_duration; + SpecifierOpt *fix_sub_duration_heartbeat; + int nb_fix_sub_duration_heartbeat; SpecifierOpt *canvas_sizes; int nb_canvas_sizes; SpecifierOpt *pass; @@ -584,6 +586,12 @@ typedef struct OutputStream { int sq_idx_encode; int sq_idx_mux; + + /* + * bool on whether this stream should be utilized for splitting + * subtitles utilizing fix_sub_duration at random access points. + */ + unsigned int fix_sub_duration_heartbeat; } OutputStream; typedef struct Muxer Muxer; diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index d7049069f4..d4c8a2568b 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -98,6 +98,7 @@ static const char *const opt_name_filters[] = {"filter", "af", static const char *const opt_name_filter_scripts[] = {"filter_script", NULL}; static const char *const opt_name_reinit_filters[] = {"reinit_filter", NULL}; static const char *const opt_name_fix_sub_duration[] = {"fix_sub_duration", NULL}; +static const char *const opt_name_fix_sub_duration_heartbeat[] = {"fix_sub_duration_heartbeat", NULL}; static const char *const opt_name_canvas_sizes[] = {"canvas_size", NULL}; static const char *const opt_name_pass[] = {"pass", NULL}; static const char *const opt_name_passlogfiles[] = {"passlogfile", NULL}; @@ -1730,6 +1731,9 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e MATCH_PER_STREAM_OPT(bits_per_raw_sample, i, ost->bits_per_raw_sample, oc, st); + MATCH_PER_STREAM_OPT(fix_sub_duration_heartbeat, i, ost->fix_sub_duration_heartbeat, + oc, st); + if (oc->oformat->flags & AVFMT_GLOBALHEADER && ost->enc_ctx) ost->enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; @@ -4074,6 +4078,11 @@ const OptionDef options[] = { { "autoscale", HAS_ARG | OPT_BOOL | OPT_SPEC | OPT_EXPERT | OPT_OUTPUT, { .off = OFFSET(autoscale) }, "automatically insert a scale filter at the end of the filter graph" }, + { "fix_sub_duration_heartbeat", OPT_VIDEO | OPT_BOOL | OPT_EXPERT | + OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(fix_sub_duration_heartbeat) }, + "set this video output stream to be a heartbeat stream for " + "fix_sub_duration, according to which subtitles should be split at " + "random access points" }, /* audio options */ { "aframes", OPT_AUDIO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_audio_frames }, diff --git a/tests/fate/ffmpeg.mak b/tests/fate/ffmpeg.mak index 38a1ae7ed5..39eb26f295 100644 --- a/tests/fate/ffmpeg.mak +++ b/tests/fate/ffmpeg.mak @@ -117,6 +117,20 @@ fate-ffmpeg-fix_sub_duration: CMD = fmtstdout srt -fix_sub_duration \ -real_time 1 -f lavfi \ -i "movie=$(TARGET_SAMPLES)/sub/Closedcaption_rollup.m2v[out0+subcc]" +# Basic test for fix_sub_duration_heartbeat, which causes a buffered subtitle +# to be pushed out when a video keyframe is received from an encoder. +FATE_SAMPLES_FFMPEG-$(call FILTERDEMDECENCMUX, MOVIE, MPEGVIDEO, \ + MPEG2VIDEO, SUBRIP, SRT, LAVFI_INDEV \ + MPEGVIDEO_PARSER CCAPTION_DECODER \ + MPEG2VIDEO_ENCODER NULL_MUXER PIPE_PROTOCOL) \ + += fate-ffmpeg-fix_sub_duration_heartbeat +fate-ffmpeg-fix_sub_duration_heartbeat: CMD = fmtstdout srt -fix_sub_duration \ + -real_time 1 -f lavfi \ + -i "movie=$(TARGET_SAMPLES)/sub/Closedcaption_rollup.m2v[out0+subcc]" \ + -map 0:v -fix_sub_duration_heartbeat:v:0 \ + -c mpeg2video -b:v 2M -g 30 -sc_threshold 1000000000 \ + -f null - + FATE_STREAMCOPY-$(call REMUX, MP4 MOV, EAC3_DEMUXER) += fate-copy-trac3074 fate-copy-trac3074: CMD = transcode eac3 $(TARGET_SAMPLES)/eac3/csi_miami_stereo_128_spx.eac3\ mp4 "-codec copy -map 0" "-codec copy" diff --git a/tests/ref/fate/ffmpeg-fix_sub_duration_heartbeat b/tests/ref/fate/ffmpeg-fix_sub_duration_heartbeat new file mode 100644 index 0000000000..957a410921 --- /dev/null +++ b/tests/ref/fate/ffmpeg-fix_sub_duration_heartbeat @@ -0,0 +1,48 @@ +1 +00:00:00,968 --> 00:00:01,001 +{\an7}( + +2 +00:00:01,001 --> 00:00:01,168 +{\an7}( + +3 +00:00:01,168 --> 00:00:01,368 +{\an7}( inaudibl + +4 +00:00:01,368 --> 00:00:01,568 +{\an7}( inaudible radio chat + +5 +00:00:01,568 --> 00:00:02,002 +{\an7}( inaudible radio chatter ) + +6 +00:00:02,002 --> 00:00:03,003 +{\an7}( inaudible radio chatter ) + +7 +00:00:03,003 --> 00:00:03,103 +{\an7}( inaudible radio chatter ) + +8 +00:00:03,103 --> 00:00:03,303 +{\an7}( inaudible radio chatter ) +>> + +9 +00:00:03,303 --> 00:00:03,503 +{\an7}( inaudible radio chatter ) +>> Safety rema + +10 +00:00:03,504 --> 00:00:03,704 +{\an7}( inaudible radio chatter ) +>> Safety remains our numb + +11 +00:00:03,704 --> 00:00:04,004 +{\an7}( inaudible radio chatter ) +>> Safety remains our number one +