1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

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 4d686fb721
(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@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2023-08-11 04:26:13 +02:00
parent 230312b3c8
commit 884653ee5b
2 changed files with 70 additions and 67 deletions

View File

@ -1580,95 +1580,96 @@ static void mkv_write_field_order(EbmlWriter *writer, int is_webm,
#define MAX_STEREO_MODE_ELEMS 1 #define MAX_STEREO_MODE_ELEMS 1
static int mkv_write_stereo_mode(AVFormatContext *s, EbmlWriter *writer, 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) int *h_width, int *h_height)
{ {
const AVDictionaryEntry *tag; const AVDictionaryEntry *tag;
MatroskaVideoStereoModeType format = MATROSKA_VIDEO_STEREOMODE_TYPE_NB; 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_width = 1;
*h_height = 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)) || if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) ||
(tag = av_dict_get( s->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++) for (int i = 0; i < MATROSKA_VIDEO_STEREOMODE_TYPE_NB; i++)
if (!strcmp(tag->value, ff_matroska_video_stereo_mode[i])){ if (!strcmp(tag->value, ff_matroska_video_stereo_mode[i])){
stereo_mode = i; format = i;
break; break;
} }
if (format == MATROSKA_VIDEO_STEREOMODE_TYPE_NB) {
if ((unsigned long)stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB && long stereo_mode = strtol(tag->value, NULL, 0);
stereo_mode != 10 && stereo_mode != 12) { if ((unsigned long)stereo_mode >= MATROSKA_VIDEO_STEREOMODE_TYPE_NB)
int ret = ff_mkv_stereo3d_conv(st, stereo_mode); goto fail;
if (ret < 0) format = stereo_mode;
return ret;
} }
} } 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, stereo = (const AVStereo3D*)av_stream_get_side_data(st, AV_PKT_DATA_STEREO3D,
NULL); NULL);
if (stereo) { if (!stereo)
switch (stereo->type) { return 0;
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;
}
}
if (format == MATROSKA_VIDEO_STEREOMODE_TYPE_NB) /* A garbage AVStereo3D or something with no Matroska analogon. */
return 0; if ((unsigned)stereo->type >= FF_ARRAY_ELEMS(conversion_table))
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 webm, do not write unsupported modes
if ((is_webm && if (is_webm && !(webm_bitfield >> format)) {
format > MATROSKA_VIDEO_STEREOMODE_TYPE_TOP_BOTTOM && goto fail;
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);
} }
*h_width = 1 << ((width_bitfield >> format) & 1);
*h_height = 1 << ((height_bitfield >> format) & 1);
// write StereoMode if format is valid // write StereoMode if format is valid
ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOSTEREOMODE, format); ebml_writer_add_uint(writer, MATROSKA_ID_VIDEOSTEREOMODE, format);
return 0; 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, 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, static int mkv_write_track_video(AVFormatContext *s, MatroskaMuxContext *mkv,
AVStream *st, const AVCodecParameters *par, const AVStream *st, const AVCodecParameters *par,
AVIOContext *pb) AVIOContext *pb)
{ {
const AVDictionaryEntry *tag; const AVDictionaryEntry *tag;

View File

@ -1,5 +1,5 @@
41cc6b0d334ac0465988579b99b78002 *tests/data/fate/matroska-stereo_mode.matroska a7a220a77001e81685ec807ce5ac3bc6 *tests/data/fate/matroska-stereo_mode.matroska
1470756 tests/data/fate/matroska-stereo_mode.matroska 1470764 tests/data/fate/matroska-stereo_mode.matroska
#extradata 0: 3510, 0x560c3919 #extradata 0: 3510, 0x560c3919
#extradata 1: 3510, 0x560c3919 #extradata 1: 3510, 0x560c3919
#extradata 2: 3510, 0x560c3919 #extradata 2: 3510, 0x560c3919
@ -181,6 +181,7 @@ DISPOSITION:default=1
DISPOSITION:dub=0 DISPOSITION:dub=0
DISPOSITION:original=0 DISPOSITION:original=0
TAG:language=fra TAG:language=fra
TAG:stereo_mode=anaglyph_cyan_red
TAG:DURATION=00:00:10.000000000 TAG:DURATION=00:00:10.000000000
[/STREAM] [/STREAM]
[STREAM] [STREAM]
@ -188,6 +189,7 @@ DISPOSITION:default=0
DISPOSITION:dub=0 DISPOSITION:dub=0
DISPOSITION:original=0 DISPOSITION:original=0
TAG:language=de TAG:language=de
TAG:stereo_mode=anaglyph_green_magenta
TAG:DESCRIPTION-ger=Deutsch TAG:DESCRIPTION-ger=Deutsch
TAG:DURATION=00:00:10.000000000 TAG:DURATION=00:00:10.000000000
[/STREAM] [/STREAM]