[FFmpeg-devel] [PATCH v3] libavcodec: Make disposition forced work with mov_text subtitles.

facefunk facefunk at fcfnk.com
Mon Jul 18 23:00:52 EEST 2022


We are not currently able to force mov_text subtitles by setting -disposition:s:0 +forced or equivalent. By setting the forced flags in movtextenc as specifid in https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html subtitles can be forced as expected in VLC and similar players.

Copy AVStream disposition to AVCodecContext and use to set DISPLAY_FLAG_ALL_SAMPLES_FORCED in movtextenc.c.

Signed-off-by: facefunk <facefunk at fcfnk.com>
---
 fftools/ffmpeg_opt.c    |  5 +++++
 libavcodec/avcodec.h    | 11 +++++++++++
 libavcodec/movtextenc.c | 41 ++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index e08455478f..a7fa809859 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -2244,6 +2244,11 @@ static int set_dispositions(OutputFile *of, AVFormatContext *ctx)
 
             if (ret < 0)
                 return ret;
+
+            // For output streams, Set subtitle flags to forced when stream
+            // disposition is forced.
+            if(ost->enc_ctx && ost->st->disposition & AV_DISPOSITION_FORCED)
+                ost->enc_ctx->subtitle_flags |= AV_SUBTITLE_FLAG_FORCED;
         }
     } else {
         // For each media type with more than one stream, find a suitable stream to
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index cb5c25bf63..182f452557 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -2054,6 +2054,17 @@ typedef struct AVCodecContext {
      *             The decoder can then override during decoding as needed.
      */
     AVChannelLayout ch_layout;
+
+    /**
+     * Subtitle codecs only. Bit set of AV_SUBTITLE_FLAG_*.
+     *
+     * Some subtitle codecs may use this field to determine the subtitle
+     * display flags to encode.
+     *
+     * - encoding: set by user
+     * - decoding: unused
+     */
+    int subtitle_flags;
 } AVCodecContext;
 
 /**
diff --git a/libavcodec/movtextenc.c b/libavcodec/movtextenc.c
index 728338f2cc..bb6021f6d1 100644
--- a/libavcodec/movtextenc.c
+++ b/libavcodec/movtextenc.c
@@ -50,6 +50,40 @@
 #define FONTSIZE_SCALE(s,fs) ((fs) * (s)->font_scale_factor + 0.5)
 #define av_bprint_append_any(buf, data, size)   av_bprint_append_data(buf, ((const char*)data), size)
 
+/**
+ * https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html
+ *
+ * Display flags
+ * A 32-bit integer containing flags that describe how the subtitle text should
+ * be drawn.
+ * The following flags are defined:
+ *
+ * Vertical placement
+ * Controls vertical placement of the subtitle text.
+ * If this flag is set, the subtitle media handler uses the top coordinate of
+ * the display bounds of the override 'tbox' text box to determine the
+ * subtitle’s vertical placement as described in Subtitle Track Header Size
+ * and Placement. Otherwise, the subtitle displays at the bottom of the video.
+ */
+#define DISPLAY_FLAG_VERTICAL_PLACEMENT_TOP 0x20000000
+
+/**
+ * Some samples are forced
+ * Indicates whether any subtitle samples contain forced atoms. If this flag is
+ * set, at least one sample contains a forced ('frcd') atom as described in
+ * Subtitle Sample Data.
+ */
+#define DISPLAY_FLAG_SOME_SAMPLES_FORCED    0x40000000
+
+/**
+ * All samples are forced
+ * If this flag is set, the subtitle media handler treats all samples as forced
+ * subtitles, regardless of the presence or absence of a 'frcd' atom.
+ * If this flag is set, the Some Samples Are Forced flag must also be set
+ * (making 0xC0000000).
+ */
+#define DISPLAY_FLAG_ALL_SAMPLES_FORCED     0x80000000
+
 typedef struct {
     uint16_t style_start;
     uint16_t style_end;
@@ -183,6 +217,7 @@ static int encode_sample_description(AVCodecContext *avctx)
     int font_names_total_len = 0;
     MovTextContext *s = avctx->priv_data;
     uint8_t buf[30], *p = buf;
+    uint32_t display_flags = 0;
 
     //  0x00, 0x00, 0x00, 0x00, // uint32_t displayFlags
     //  0x01,                   // int8_t horizontal-justification
@@ -241,7 +276,11 @@ static int encode_sample_description(AVCodecContext *avctx)
                      (255 - ((uint32_t)style->back_color >> 24));
     }
 
-    bytestream_put_be32(&p, 0); // displayFlags
+    if (avctx->subtitle_flags & AV_SUBTITLE_FLAG_FORCED)
+        display_flags = DISPLAY_FLAG_SOME_SAMPLES_FORCED |
+            DISPLAY_FLAG_ALL_SAMPLES_FORCED;
+
+    bytestream_put_be32(&p, display_flags); // displayFlags
     bytestream_put_be16(&p, 0x01FF); // horizontal/vertical justification (2x int8_t)
     bytestream_put_be32(&p, back_color);
     bytestream_put_be64(&p, 0); // BoxRecord - 4xint16_t: top, left, bottom, right
-- 
2.25.1



More information about the ffmpeg-devel mailing list