mbox

[FFmpeg-devel,v3,00/26] Subtitle Filtering 2022

Message ID pull.18.v3.ffstaging.FFmpeg.1642649134.ffmpegagent@gmail.com
Headers show

Message

Aman Karmani Jan. 20, 2022, 3:25 a.m. UTC
Subtitle Filtering 2022
=======================

This is a substantial update to the earlier subtitle filtering patch series.
A primary goal has been to address others' concerns as much as possible on
one side and to provide more clarity and control over the way things are
working. Clarity is is specifically important to allow for a better
understanding of the need for a subtitle start pts value that can be
different from the frame's pts value. This is done by refactoring the
subtitle timing fields in AVFrame, adding a frame field to indicate repeated
subtitle frames, and finally the full removal of the heartbeat
functionality, replaced by a new 'subfeed' filter that provides different
modes for arbitrating subtitle frames in a filter graph. Finally, each
subtitle filter's documentation has been amended by a section describing the
filter's timeline behavior (in v3 update).

The update also includes major improvements to graphicsub2text and lots of
other details.

Versioning is restarting at v1 due to the new submission procedure.


v3 - Rebase
===========

due to merge conflicts - apologies.


Changes in v2
=============

 * added .gitattributes file to enforce binary diffs for the test refs that
   cannot be applied when being sent via e-mail
 * perform filter graph re-init due to subtitle "frame size" change only
   when the size was unknown before and not set via -canvas_size
 * overlaytextsubs: Make sure to request frames on the subtitle input
 * avfilter/splitcc: Start parsing cc data on key frames only
 * avcodec/webvttenc: Don't encode ass drawing codes and empty lines
 * stripstyles: fix mem leak
 * gs2t: improve color detection
 * gs2t: empty frames must not be skipped
 * subfeed: fix name
 * textmod: preserve margins
 * added .gitattributes file to enforce binary diffs for the test refs that
   cannot be applied when being sent via e-mail
 * perform filter graph re-init due to subtitle "frame size" change only
   when the size was unknown before and not set via -canvas_size
 * avcodec/dvbsubdec: Fix conditions for fallback to default resolution
 * Made changes suggested by Andreas
 * Fixed failing command line reported by Michael

Changes from previous version v24:


AVFrame
=======

 * Removed sub_start_time The start time is now added to the subtitle
   start_pts during decoding The sub_end_time field is adjusted accordingly
 * Renamed sub_end_time to duration which it is effectively after removing
   the start_time
 * Added a sub-struct 'subtitle_timing' to av frame Contains subtitle_pts
   renamed to 'subtitle_timing.start_pts' and 'subtitle_timing.duration'
 * Change both fields to (fixed) time_base AV_TIMEBASE
 * add repeat_sub field provides a clear indication whether a subtitle frame
   is an actual subtitle event or a repeated subtitle frame in a filter
   graph


Heartbeat Removal
=================

 * completely removed the earlier heartbeat implementation
 * filtering arbitration is now implemented in a new filter: 'subfeed'
 * subfeed will be auto-inserted for compatiblity with sub2video command
   lines
 * the new behavior is not exactly identical to the earlier behavior, but it
   basically allows to achieve the same results


New 'subfeed' Filter
====================

 * a versatile filter for solving all kinds of problems with subtile frame
   flow in filter graphs
 * Can be inserted at any position in a graph
 * Auto-inserted for sub2video command lines (in repeat-mode)
 * Allows duration fixup delay input frames with unknown duration and infer
   duration from start of subsequent frame
 * Provides multiple modes of operation:
   * repeat mode (default) Queues input frames Outputs frames at a fixed
     (configurable) rate Either sends a matching input frame (repeatedly) or
     empty frames otherwise
   * scatter mode similar to repeat mode, but splits input frames by
     duration into small segments with same content
   * forward mode No fixed output rate Useful in combination with duration
     fixup or overlap fixup


ffmpeg Tool Changes
===================

 * delay subtitle output stream initialization (like for audio and video)
   This is needed for example when a format header depends on having
   received an initial frame to derive certain header values from
 * decoding: set subtitle frame size from decoding context
 * re-init graph when subtitle size changes
 * always insert subscale filter for sub2video command lines (to ensure
   correct scaling)


Subtitle Encoding
=================

 * ignore repeated frames for encoding based on repeat_sub field in AVFrame
 * support multi-area encoding for text subtitles Subtitle OCR can create
   multiple areas at different positions. Previously, the texts were always
   squashed into a single area ('subtitle rect'), which was not ideal.
   Multiple text areas are now generally supported:
   * ASS Encoder Changed to use the 'receive_packet' encoding API A single
     frame with multiple text areas will create multiple packets now
   * All other text subtitle encoders A newline is inserted between the text
     from multiple areas


graphicsub2text (OCR)
=====================

 * enhanced preprocessing
   * using elbg algorithm for color quantization
   * detection and removal of text outlines
   * map-based identification of colors per word (text, outline, background)
 * add option for duration fixup
 * add option to dump preprocessing bitmaps
 * Recognize formatting and apply as ASS inline styles
   * per word(!)
   * paragraph alignment
   * positioning
   * font names
   * font size
   * font style (italic, underline, bold)
   * text color, outline color


Other Filter Changes
====================

 * all: Make sure to forward all link properties (time base, frame rate, w,
   h) where appropriate
 * overlaytextsubs: request frames on the subtitle input
 * overlaytextsubs: disable read-order checking
 * overlaytextsubs: improve implementation of render_latest_only
 * overlaytextsubs: ensure equal in/out video formats
 * splitcc: derive framerate from realtime_latency
 * graphicsub2video: implement caching of converted frames
 * graphicsub2video: use 1x1 output frame size as long as subtitle size is
   unknown (0x0)

Plus a dozen of things I forgot..

softworkz (26):
  avcodec,avutil: Move enum AVSubtitleType to avutil, add new and
    deprecate old values
  avutil/frame: Prepare AVFrame for subtitle handling
  avcodec/subtitles: Introduce new frame-based subtitle decoding API
  avfilter/subtitles: Update vf_subtitles to use new decoding api
  avcodec,avutil: Move ass helper functions to avutil as avpriv_ and
    extend ass dialog parsing
  avcodec/subtitles: Replace deprecated enum values
  fftools/play,probe: Adjust for subtitle changes
  avfilter/subtitles: Add subtitles.c for subtitle frame allocation
  avfilter/avfilter: Handle subtitle frames
  avfilter/avfilter: Fix hardcoded input index
  avfilter/sbuffer: Add sbuffersrc and sbuffersink filters
  avfilter/overlaygraphicsubs: Add overlaygraphicsubs and
    graphicsub2video filters
  avfilter/overlaytextsubs: Add overlaytextsubs and textsubs2video
    filters
  avfilter/textmod: Add textmod, censor and show_speaker filters
  avfilter/stripstyles: Add stripstyles filter
  avfilter/splitcc: Add splitcc filter for closed caption handling
  avfilter/graphicsub2text: Add new graphicsub2text filter (OCR)
  avfilter/subscale: Add filter for scaling and/or re-arranging
    graphical subtitles
  avfilter/subfeed: add subtitle feed filter
  avcodec/subtitles: Migrate subtitle encoders to frame-based API
  fftools/ffmpeg: Introduce subtitle filtering and new frame-based
    subtitle encoding
  avutil/ass_split: Add parsing of hard-space tags (\h)
  avcodec/webvttenc: convert hard-space tags to  
  doc/APIchanges: update for subtitle filtering changes
  avcodec/webvttenc: Don't encode drawing codes and empty lines
  avcodec/dvbsubdec: Fix conditions for fallback to default resolution

 configure                                     |    7 +-
 doc/APIchanges                                |   24 +
 doc/filters.texi                              |  756 ++++++++++
 fftools/ffmpeg.c                              |  501 +++----
 fftools/ffmpeg.h                              |   13 +-
 fftools/ffmpeg_filter.c                       |  235 +++-
 fftools/ffmpeg_hw.c                           |    2 +-
 fftools/ffmpeg_opt.c                          |    3 +-
 fftools/ffplay.c                              |  102 +-
 fftools/ffprobe.c                             |   47 +-
 libavcodec/Makefile                           |   56 +-
 libavcodec/ass.h                              |  144 +-
 libavcodec/assdec.c                           |    4 +-
 libavcodec/assenc.c                           |  191 ++-
 libavcodec/avcodec.h                          |   32 +-
 libavcodec/ccaption_dec.c                     |   19 +-
 libavcodec/codec_desc.c                       |   11 +
 libavcodec/codec_desc.h                       |    8 +
 libavcodec/decode.c                           |   60 +-
 libavcodec/dvbsubdec.c                        |   53 +-
 libavcodec/dvbsubenc.c                        |   96 +-
 libavcodec/dvdsubdec.c                        |    2 +-
 libavcodec/dvdsubenc.c                        |  102 +-
 libavcodec/encode.c                           |   61 +-
 libavcodec/internal.h                         |   16 +
 libavcodec/jacosubdec.c                       |    2 +-
 libavcodec/libaribb24.c                       |    2 +-
 libavcodec/libzvbi-teletextdec.c              |   14 +-
 libavcodec/microdvddec.c                      |    7 +-
 libavcodec/movtextdec.c                       |    3 +-
 libavcodec/movtextenc.c                       |  126 +-
 libavcodec/mpl2dec.c                          |    2 +-
 libavcodec/pgssubdec.c                        |    2 +-
 libavcodec/realtextdec.c                      |    2 +-
 libavcodec/samidec.c                          |    2 +-
 libavcodec/srtdec.c                           |    2 +-
 libavcodec/srtenc.c                           |  116 +-
 libavcodec/subviewerdec.c                     |    2 +-
 libavcodec/tests/avcodec.c                    |    2 -
 libavcodec/textdec.c                          |    4 +-
 libavcodec/ttmlenc.c                          |  114 +-
 libavcodec/utils.c                            |  184 +++
 libavcodec/version.h                          |    2 +-
 libavcodec/webvttdec.c                        |    2 +-
 libavcodec/webvttenc.c                        |  127 +-
 libavcodec/xsubdec.c                          |    2 +-
 libavcodec/xsubenc.c                          |   88 +-
 libavfilter/Makefile                          |   16 +
 libavfilter/allfilters.c                      |   16 +-
 libavfilter/avfilter.c                        |   30 +-
 libavfilter/avfilter.h                        |   11 +
 libavfilter/avfiltergraph.c                   |    5 +
 libavfilter/buffersink.c                      |   54 +
 libavfilter/buffersink.h                      |    7 +
 libavfilter/buffersrc.c                       |   72 +
 libavfilter/buffersrc.h                       |    1 +
 libavfilter/formats.c                         |   22 +
 libavfilter/formats.h                         |    3 +
 libavfilter/internal.h                        |   19 +-
 libavfilter/sf_graphicsub2text.c              | 1132 +++++++++++++++
 libavfilter/sf_splitcc.c                      |  395 ++++++
 libavfilter/sf_stripstyles.c                  |  211 +++
 libavfilter/sf_subfeed.c                      |  366 +++++
 libavfilter/sf_subscale.c                     |  884 ++++++++++++
 libavfilter/sf_textmod.c                      |  710 ++++++++++
 libavfilter/subtitles.c                       |   63 +
 libavfilter/subtitles.h                       |   44 +
 libavfilter/vf_overlaygraphicsubs.c           |  765 ++++++++++
 libavfilter/vf_overlaytextsubs.c              |  678 +++++++++
 libavfilter/vf_subtitles.c                    |   56 +-
 libavutil/Makefile                            |    4 +
 {libavcodec => libavutil}/ass.c               |   91 +-
 libavutil/ass_internal.h                      |  135 ++
 {libavcodec => libavutil}/ass_split.c         |   37 +-
 .../ass_split_internal.h                      |   32 +-
 libavutil/frame.c                             |  211 ++-
 libavutil/frame.h                             |   85 +-
 libavutil/subfmt.c                            |   45 +
 libavutil/subfmt.h                            |  115 ++
 libavutil/version.h                           |    3 +-
 tests/ref/fate/.gitattributes                 |    3 +
 tests/ref/fate/filter-overlay-dvdsub-2397     |  182 +--
 tests/ref/fate/mov-mp4-ttml-dfxp              |    8 +-
 tests/ref/fate/mov-mp4-ttml-stpp              |    8 +-
 tests/ref/fate/sub-dvb                        |  162 ++-
 tests/ref/fate/sub-textenc                    |   10 +-
 tests/ref/fate/sub-ttmlenc                    |    8 +-
 tests/ref/fate/sub-webvttenc                  |   10 +-
 tests/ref/fate/sub2video                      | 1091 ++++++++++++++-
 tests/ref/fate/sub2video_basic                | 1239 +++++++++++++++--
 tests/ref/fate/sub2video_time_limited         |   78 +-
 91 files changed, 11077 insertions(+), 1392 deletions(-)
 create mode 100644 libavfilter/sf_graphicsub2text.c
 create mode 100644 libavfilter/sf_splitcc.c
 create mode 100644 libavfilter/sf_stripstyles.c
 create mode 100644 libavfilter/sf_subfeed.c
 create mode 100644 libavfilter/sf_subscale.c
 create mode 100644 libavfilter/sf_textmod.c
 create mode 100644 libavfilter/subtitles.c
 create mode 100644 libavfilter/subtitles.h
 create mode 100644 libavfilter/vf_overlaygraphicsubs.c
 create mode 100644 libavfilter/vf_overlaytextsubs.c
 rename {libavcodec => libavutil}/ass.c (65%)
 create mode 100644 libavutil/ass_internal.h
 rename {libavcodec => libavutil}/ass_split.c (93%)
 rename libavcodec/ass_split.h => libavutil/ass_split_internal.h (86%)
 create mode 100644 libavutil/subfmt.c
 create mode 100644 libavutil/subfmt.h
 create mode 100644 tests/ref/fate/.gitattributes


base-commit: dd17c86aa11feae2b86de054dd0679cc5f88ebab
Published-As: https://github.com/ffstaging/FFmpeg/releases/tag/pr-ffstaging-18%2Fsoftworkz%2Fsubmit_subfiltering-v3
Fetch-It-Via: git fetch https://github.com/ffstaging/FFmpeg pr-ffstaging-18/softworkz/submit_subfiltering-v3
Pull-Request: https://github.com/ffstaging/FFmpeg/pull/18

Range-diff vs v2:

  1:  13b9a26b25 =  1:  7767933235 avcodec,avutil: Move enum AVSubtitleType to avutil, add new and deprecate old values
  2:  54ed1290d2 =  2:  e922f141ba avutil/frame: Prepare AVFrame for subtitle handling
  3:  840683a6e1 =  3:  ec262914b0 avcodec/subtitles: Introduce new frame-based subtitle decoding API
  4:  e326e96492 =  4:  77bd67ee37 avfilter/subtitles: Update vf_subtitles to use new decoding api
  5:  20dc27a6c6 =  5:  26bad0c088 avcodec,avutil: Move ass helper functions to avutil as avpriv_ and extend ass dialog parsing
  6:  3de0be7980 =  6:  c6fb78e038 avcodec/subtitles: Replace deprecated enum values
  7:  05e3f2a6fe =  7:  3d8673919f fftools/play,probe: Adjust for subtitle changes
  8:  a86457f868 =  8:  ba8b675326 avfilter/subtitles: Add subtitles.c for subtitle frame allocation
  9:  9fec875c51 =  9:  eb09db0e00 avfilter/avfilter: Handle subtitle frames
 10:  50bf6c36be = 10:  b31a991320 avfilter/avfilter: Fix hardcoded input index
 11:  d1ef5004aa = 11:  59abea7693 avfilter/sbuffer: Add sbuffersrc and sbuffersink filters
 12:  f4af184a7b ! 12:  867b60c60d avfilter/overlaygraphicsubs: Add overlaygraphicsubs and graphicsub2video filters
     @@ libavfilter/Makefile: OBJS-$(CONFIG_GBLUR_FILTER)                  += vf_gblur.o
       OBJS-$(CONFIG_GRAYWORLD_FILTER)              += vf_grayworld.o
       OBJS-$(CONFIG_GREYEDGE_FILTER)               += vf_colorconstancy.o
      @@ libavfilter/Makefile: OBJS-$(CONFIG_OVERLAY_OPENCL_FILTER)         += vf_overlay_opencl.o opencl.o \
     -                                                 opencl/overlay.o framesync.o
       OBJS-$(CONFIG_OVERLAY_QSV_FILTER)            += vf_overlay_qsv.o framesync.o
     + OBJS-$(CONFIG_OVERLAY_VAAPI_FILTER)          += vf_overlay_vaapi.o framesync.o vaapi_vpp.o
       OBJS-$(CONFIG_OVERLAY_VULKAN_FILTER)         += vf_overlay_vulkan.o vulkan.o vulkan_filter.o
      +OBJS-$(CONFIG_OVERLAYGRAPHICSUBS_FILTER)     += vf_overlaygraphicsubs.o framesync.o
       OBJS-$(CONFIG_OWDENOISE_FILTER)              += vf_owdenoise.o
     @@ libavfilter/allfilters.c: extern const AVFilter ff_vf_oscilloscope;
       extern const AVFilter ff_vf_overlay_opencl;
       extern const AVFilter ff_vf_overlay_qsv;
      +extern const AVFilter ff_vf_overlaygraphicsubs;
     + extern const AVFilter ff_vf_overlay_vaapi;
       extern const AVFilter ff_vf_overlay_vulkan;
       extern const AVFilter ff_vf_overlay_cuda;
     - extern const AVFilter ff_vf_owdenoise;
      @@ libavfilter/allfilters.c: extern const AVFilter ff_avf_showvolume;
       extern const AVFilter ff_avf_showwaves;
       extern const AVFilter ff_avf_showwavespic;
 13:  47b045ad74 ! 13:  f88a5667e1 avfilter/overlaytextsubs: Add overlaytextsubs and textsubs2video filters
     @@ Commit message
          Signed-off-by: softworkz <softworkz@hotmail.com>
      
       ## configure ##
     -@@ configure: overlay_opencl_filter_deps="opencl"
     - overlay_qsv_filter_deps="libmfx"
     +@@ configure: overlay_qsv_filter_deps="libmfx"
       overlay_qsv_filter_select="qsvvpp"
     + overlay_vaapi_filter_deps="vaapi VAProcPipelineCaps_blend_flags"
       overlay_vulkan_filter_deps="vulkan spirv_compiler"
      +overlaytextsubs_filter_deps="avcodec libass"
       owdenoise_filter_deps="gpl"
     @@ doc/filters.texi: Overlay PGS subtitles
       @chapter Multimedia Filters
      
       ## libavfilter/Makefile ##
     -@@ libavfilter/Makefile: OBJS-$(CONFIG_OVERLAY_OPENCL_FILTER)         += vf_overlay_opencl.o opencl.o \
     - OBJS-$(CONFIG_OVERLAY_QSV_FILTER)            += vf_overlay_qsv.o framesync.o
     +@@ libavfilter/Makefile: OBJS-$(CONFIG_OVERLAY_QSV_FILTER)            += vf_overlay_qsv.o framesync.o
     + OBJS-$(CONFIG_OVERLAY_VAAPI_FILTER)          += vf_overlay_vaapi.o framesync.o vaapi_vpp.o
       OBJS-$(CONFIG_OVERLAY_VULKAN_FILTER)         += vf_overlay_vulkan.o vulkan.o vulkan_filter.o
       OBJS-$(CONFIG_OVERLAYGRAPHICSUBS_FILTER)     += vf_overlaygraphicsubs.o framesync.o
      +OBJS-$(CONFIG_OVERLAYTEXTSUBS_FILTER)        += vf_overlaytextsubs.o
     @@ libavfilter/allfilters.c: extern const AVFilter ff_vf_oscilloscope;
       extern const AVFilter ff_vf_overlay_opencl;
       extern const AVFilter ff_vf_overlay_qsv;
      -extern const AVFilter ff_vf_overlaygraphicsubs;
     +-extern const AVFilter ff_vf_overlay_vaapi;
       extern const AVFilter ff_vf_overlay_vulkan;
       extern const AVFilter ff_vf_overlay_cuda;
      +extern const AVFilter ff_vf_overlaygraphicsubs;
      +extern const AVFilter ff_vf_overlaytextsubs;
     ++extern const AVFilter ff_vf_overlay_vaapi;
       extern const AVFilter ff_vf_owdenoise;
       extern const AVFilter ff_vf_pad;
       extern const AVFilter ff_vf_pad_opencl;
 14:  6f200be0c3 = 14:  25cda21970 avfilter/textmod: Add textmod, censor and show_speaker filters
 15:  ab8736ac43 = 15:  296f1f697b avfilter/stripstyles: Add stripstyles filter
 16:  5369aca080 = 16:  509d6c67ba avfilter/splitcc: Add splitcc filter for closed caption handling
 17:  43a20d1024 = 17:  1d7acba39f avfilter/graphicsub2text: Add new graphicsub2text filter (OCR)
 18:  0d3c46a68f = 18:  244dd6de33 avfilter/subscale: Add filter for scaling and/or re-arranging graphical subtitles
 19:  2c20886389 = 19:  a87812bd3c avfilter/subfeed: add subtitle feed filter
 20:  64ce976c19 = 20:  aea8acb057 avcodec/subtitles: Migrate subtitle encoders to frame-based API
 21:  56a162b3a4 = 21:  314d1da505 fftools/ffmpeg: Introduce subtitle filtering and new frame-based subtitle encoding
 22:  1a0c6e01f3 = 22:  d3cdd2a0e2 avutil/ass_split: Add parsing of hard-space tags (\h)
 23:  44b4e203d8 = 23:  5fca566749 avcodec/webvttenc: convert hard-space tags to &nbsp;
 24:  5773f2a1ff = 24:  93b469b8d0 doc/APIchanges: update for subtitle filtering changes
 25:  7ab6dedf80 = 25:  0c279550d6 avcodec/webvttenc: Don't encode drawing codes and empty lines
 26:  217d96c39d = 26:  03e1a98e08 avcodec/dvbsubdec: Fix conditions for fallback to default resolution