[FFmpeg-cvslog] avformat/matroskaenc: Don't add side-data to input stream

Andreas Rheinhardt git at videolan.org
Wed Aug 30 21:09:13 EEST 2023


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Fri Aug 11 04:26:13 2023 +0200| [884653ee5be03ed38db957c14fad51b300611c8c] | committer: Andreas Rheinhardt

avformat/matroskaenc: Don't add side-data to input stream

When muxing, the AVStreams' side-data is typically set
by the caller before avformat_write_header();
it is not documented to be else. Yet the Matroska muxer
added an AVStereo3D side data if certain metadata
was present:

Since commit 4d686fb721b485ebbc4c7779d927d876c1e630f7
(adding support for AVStereo3D stream side-data),
the Matroska muxer checked certain stream tags that
contain Matroska's StereoMode and (if they are present)
converted this value into an AVStereo3D struct that
gets attached to the AVStream (reusing a function from
the demuxer). Afterwards the AVStereo3D side data struct
(whether it has just been added by the muxer or not) gets
parsed and converted back into a Matroska StereoMode.

Besides being an API violation this change broke
StereoMode values without a corresponding AVStereo3D
(namely the anaglyph ones).

This commit fixes this: A StereoMode given via tags
is now used-as-is; if no such tag exists and an AVStereo3D
side data exists, it is converted into the corresponding
StereoMode (if possible). This approach also fixes
handling of the anaglyph ones; the changes to the
matroska-stereo_mode are due to this.

The new STEREOMODE_STEREO3D_MAPPING has been put to
good use for this.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=884653ee5be03ed38db957c14fad51b300611c8c
---

 libavformat/matroskaenc.c           | 131 ++++++++++++++++++------------------
 tests/ref/fate/matroska-stereo_mode |   6 +-
 2 files changed, 70 insertions(+), 67 deletions(-)

diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 34aa2a768d..64046f38aa 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -1580,95 +1580,96 @@ static void mkv_write_field_order(EbmlWriter *writer, int is_webm,
 
 #define MAX_STEREO_MODE_ELEMS 1
 static int mkv_write_stereo_mode(AVFormatContext *s, EbmlWriter *writer,
-                                 AVStream *st, int is_webm,
+                                 const AVStream *st, int is_webm,
                                  int *h_width, int *h_height)
 {
     const AVDictionaryEntry *tag;
     MatroskaVideoStereoModeType format = MATROSKA_VIDEO_STEREOMODE_TYPE_NB;
-    const AVStereo3D *stereo;
+
+    /* The following macros create bitfields where the ith bit
+     * indicates whether the MatroskaVideoStereoModeType with that value
+     * uses double width/height or is WebM compatible. */
+#define FLAG(STEREOMODETYPE, BOOL) | (BOOL) << (STEREOMODETYPE)
+#define WDIV1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
+    FLAG(STEREOMODETYPE, WDIV)
+#define WDIV2(STEREOMODETYPE, WDIV, HDIV, WEBM) \
+    FLAG(STEREOMODETYPE, WDIV)
+    // The zero in the following line consumes the first '|'.
+    const unsigned width_bitfield = 0 STEREOMODE_STEREO3D_MAPPING(WDIV1, WDIV2);
+
+#define HDIV1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
+    FLAG(STEREOMODETYPE, HDIV)
+#define HDIV2(STEREOMODETYPE, WDIV, HDIV, WEBM) \
+    FLAG(STEREOMODETYPE, HDIV)
+    const unsigned height_bitfield = 0 STEREOMODE_STEREO3D_MAPPING(HDIV1, HDIV2);
+
+#define WEBM1(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
+    FLAG(STEREOMODETYPE, WEBM)
+#define WEBM2(STEREOMODETYPE, WDIV, HDIV, WEBM) \
+    FLAG(STEREOMODETYPE, WEBM)
+    const unsigned webm_bitfield = 0 STEREOMODE_STEREO3D_MAPPING(WEBM1, WEBM2);
 
     *h_width = 1;
     *h_height = 1;
-    // convert metadata into proper side data and add it to the stream
+
     if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) ||
         (tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) {
-        long stereo_mode = strtol(tag->value, NULL, 0);
 
         for (int i = 0; i < MATROSKA_VIDEO_STEREOMODE_TYPE_NB; i++)
             if (!strcmp(tag->value, ff_matroska_video_stereo_mode[i])){
-                stereo_mode = i;
+                format = i;
                 break;
             }
-
-        if ((unsigned long)stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB &&
-            stereo_mode != 10 && stereo_mode != 12) {
-            int ret = ff_mkv_stereo3d_conv(st, stereo_mode);
-            if (ret < 0)
-                return ret;
+        if (format == MATROSKA_VIDEO_STEREOMODE_TYPE_NB) {
+            long stereo_mode = strtol(tag->value, NULL, 0);
+            if ((unsigned long)stereo_mode >= MATROSKA_VIDEO_STEREOMODE_TYPE_NB)
+                goto fail;
+            format = stereo_mode;
         }
-    }
+    } else {
+        const AVStereo3D *stereo;
+        /* The following macro presumes all MATROSKA_VIDEO_STEREOMODE_TYPE_*
+         * values to be in the range 0..254. */
+#define STEREOMODE(STEREOMODETYPE, STEREO3DTYPE, FLAGS, WDIV, HDIV, WEBM) \
+    [(STEREO3DTYPE)][!!((FLAGS) & AV_STEREO3D_FLAG_INVERT)] = (STEREOMODETYPE) + 1,
+#define IGNORE(STEREOMODETYPE, WDIV, HDIV, WEBM)
+        static const unsigned char conversion_table[][2] = {
+            STEREOMODE_STEREO3D_MAPPING(STEREOMODE, IGNORE)
+        };
+        int fmt;
+
+        stereo = (const AVStereo3D*)av_stream_get_side_data(st, AV_PKT_DATA_STEREO3D,
+                                                            NULL);
+        if (!stereo)
+            return 0;
 
-    stereo = (const AVStereo3D*)av_stream_get_side_data(st, AV_PKT_DATA_STEREO3D,
-                                                        NULL);
-    if (stereo) {
-        switch (stereo->type) {
-        case AV_STEREO3D_2D:
-            format = MATROSKA_VIDEO_STEREOMODE_TYPE_MONO;
-            break;
-        case AV_STEREO3D_SIDEBYSIDE:
-            format = (stereo->flags & AV_STEREO3D_FLAG_INVERT)
-                     ? MATROSKA_VIDEO_STEREOMODE_TYPE_RIGHT_LEFT
-                     : MATROSKA_VIDEO_STEREOMODE_TYPE_LEFT_RIGHT;
-            *h_width = 2;
-            break;
-        case AV_STEREO3D_TOPBOTTOM:
-            format = MATROSKA_VIDEO_STEREOMODE_TYPE_TOP_BOTTOM;
-            if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
-                format--;
-            *h_height = 2;
-            break;
-        case AV_STEREO3D_CHECKERBOARD:
-            format = MATROSKA_VIDEO_STEREOMODE_TYPE_CHECKERBOARD_LR;
-            if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
-                format--;
-            break;
-        case AV_STEREO3D_LINES:
-            format = MATROSKA_VIDEO_STEREOMODE_TYPE_ROW_INTERLEAVED_LR;
-            if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
-                format--;
-            *h_height = 2;
-            break;
-        case AV_STEREO3D_COLUMNS:
-            format = MATROSKA_VIDEO_STEREOMODE_TYPE_COL_INTERLEAVED_LR;
-            if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
-                format--;
-            *h_width = 2;
-            break;
-        case AV_STEREO3D_FRAMESEQUENCE:
-            format = MATROSKA_VIDEO_STEREOMODE_TYPE_BOTH_EYES_BLOCK_LR;
-            if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
-                format++;
-            break;
-        }
-    }
+        /* A garbage AVStereo3D or something with no Matroska analogon. */
+        if ((unsigned)stereo->type >= FF_ARRAY_ELEMS(conversion_table))
+            return 0;
 
-    if (format == MATROSKA_VIDEO_STEREOMODE_TYPE_NB)
-        return 0;
+        fmt = conversion_table[stereo->type][!!(stereo->flags & AV_STEREO3D_FLAG_INVERT)];
+        /* Format with no Matroska analogon; ignore it */
+        if (!fmt)
+            return 0;
+        format = fmt - 1;
+    }
 
     // if webm, do not write unsupported modes
-    if ((is_webm &&
-        format > MATROSKA_VIDEO_STEREOMODE_TYPE_TOP_BOTTOM &&
-        format != MATROSKA_VIDEO_STEREOMODE_TYPE_RIGHT_LEFT)
-        || format >= MATROSKA_VIDEO_STEREOMODE_TYPE_NB) {
-        av_log(s, AV_LOG_ERROR,
-               "The specified stereo mode is not valid.\n");
-        return AVERROR(EINVAL);
+    if (is_webm && !(webm_bitfield >> format)) {
+        goto fail;
     }
 
+    *h_width  = 1 << ((width_bitfield  >> format) & 1);
+    *h_height = 1 << ((height_bitfield >> format) & 1);
+
     // write StereoMode if format is valid
     ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOSTEREOMODE, format);
 
     return 0;
+fail:
+    av_log(s, AV_LOG_ERROR,
+            "The specified stereo mode is not valid.\n");
+    return AVERROR(EINVAL);
 }
 
 static void mkv_write_blockadditionmapping(AVFormatContext *s, const MatroskaMuxContext *mkv,
@@ -1723,7 +1724,7 @@ static void mkv_write_blockadditionmapping(AVFormatContext *s, const MatroskaMux
 }
 
 static int mkv_write_track_video(AVFormatContext *s, MatroskaMuxContext *mkv,
-                                 AVStream *st, const AVCodecParameters *par,
+                                 const AVStream *st, const AVCodecParameters *par,
                                  AVIOContext *pb)
 {
     const AVDictionaryEntry *tag;
diff --git a/tests/ref/fate/matroska-stereo_mode b/tests/ref/fate/matroska-stereo_mode
index d406bb9b52..5c36a6d666 100644
--- a/tests/ref/fate/matroska-stereo_mode
+++ b/tests/ref/fate/matroska-stereo_mode
@@ -1,5 +1,5 @@
-41cc6b0d334ac0465988579b99b78002 *tests/data/fate/matroska-stereo_mode.matroska
-1470756 tests/data/fate/matroska-stereo_mode.matroska
+a7a220a77001e81685ec807ce5ac3bc6 *tests/data/fate/matroska-stereo_mode.matroska
+1470764 tests/data/fate/matroska-stereo_mode.matroska
 #extradata 0:     3510, 0x560c3919
 #extradata 1:     3510, 0x560c3919
 #extradata 2:     3510, 0x560c3919
@@ -181,6 +181,7 @@ DISPOSITION:default=1
 DISPOSITION:dub=0
 DISPOSITION:original=0
 TAG:language=fra
+TAG:stereo_mode=anaglyph_cyan_red
 TAG:DURATION=00:00:10.000000000
 [/STREAM]
 [STREAM]
@@ -188,6 +189,7 @@ DISPOSITION:default=0
 DISPOSITION:dub=0
 DISPOSITION:original=0
 TAG:language=de
+TAG:stereo_mode=anaglyph_green_magenta
 TAG:DESCRIPTION-ger=Deutsch
 TAG:DURATION=00:00:10.000000000
 [/STREAM]



More information about the ffmpeg-cvslog mailing list