Skip to content

Commit bcb1b8f

Browse files
committed
demux_mkv: add demuxer-mkv-crop-compat option
I don't expect the Matroska specification to be adjusted for this to work soon, so in the meantime, follow the spec as written and add an option to support most files in the wild. I would prefer this to never be an option, but it looks like it's unavoidable. Recently, FFmpeg added cropping support for Matroska, so we don’t want to be the odd one out by doing it differently... See for more details: ietf-wg-cellar/matroska-specification#947 https://gitlab.com/mbunkus/mkvtoolnix/-/issues/2389 mpv-player#13446 Fixes: mpv-player#15800
1 parent f2d5075 commit bcb1b8f

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add `demuxer-mkv-crop-compat` option

DOCS/man/options.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4032,6 +4032,29 @@ Demuxer
40324032
file and can make a reliable estimate even without an index present (such
40334033
as partial files).
40344034

4035+
``--demuxer-mkv-crop-compat=<yes|no>``
4036+
Enable compatibility mode for files that do not fully comply with the
4037+
Matroska specification. (default: yes)
4038+
4039+
Most files containing cropping metadata require this mode to display correctly.
4040+
4041+
If this option is enabled, crop metadata will be applied before calculating
4042+
the video's aspect ratio, ensuring it is cropped accordingly. If this option
4043+
is disabled, the image will be cropped first and then stretched to match
4044+
DisplayWidth and DisplayHeight.
4045+
4046+
According to the Matroska specification, the Pixel Aspect Ratio (PAR) should
4047+
be calculated after cropping. However, the majority of files do not adhere
4048+
to this rule, as it would cause incompatibility with crop-unaware players.
4049+
Additionally, MKVToolNix does not automatically adjust DisplayWidth and
4050+
DisplayHeight when cropping metadata is applied, leading to most of files
4051+
created with it also failing to conform to the specification.
4052+
4053+
See for more details:
4054+
https://github.com/ietf-wg-cellar/matroska-specification/pull/947
4055+
https://gitlab.com/mbunkus/mkvtoolnix/-/issues/2389
4056+
https://github.com/mpv-player/mpv/pull/13446
4057+
40354058
``--demuxer-rawaudio-channels=<value>``
40364059
Number of channels (or channel layout) if ``--demuxer=rawaudio`` is used
40374060
(default: stereo).

demux/demux_mkv.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ struct demux_mkv_opts {
237237
double subtitle_preroll_secs_index;
238238
int probe_duration;
239239
bool probe_start_time;
240+
bool crop_compat;
240241
};
241242

242243
const struct m_sub_options demux_mkv_conf = {
@@ -250,6 +251,7 @@ const struct m_sub_options demux_mkv_conf = {
250251
{"probe-video-duration", OPT_CHOICE(probe_duration,
251252
{"no", 0}, {"yes", 1}, {"full", 2})},
252253
{"probe-start-time", OPT_BOOL(probe_start_time)},
254+
{"crop-compat", OPT_BOOL(crop_compat)},
253255
{0}
254256
},
255257
.size = sizeof(struct demux_mkv_opts),
@@ -258,6 +260,7 @@ const struct m_sub_options demux_mkv_conf = {
258260
.subtitle_preroll_secs = 1.0,
259261
.subtitle_preroll_secs_index = 10.0,
260262
.probe_start_time = true,
263+
.crop_compat = true,
261264
},
262265
.change_flags = UPDATE_DEMUXER,
263266
};
@@ -1706,9 +1709,24 @@ static int demux_mkv_open_video(demuxer_t *demuxer, mkv_track_t *track)
17061709
sh_v->crop = crop;
17071710
}
17081711

1709-
int dw = track->v_dwidth_set ? track->v_dwidth : mp_rect_w(crop);
1710-
int dh = track->v_dheight_set ? track->v_dheight : mp_rect_h(crop);
1711-
struct mp_image_params p = {.w = mp_rect_w(crop), .h = mp_rect_h(crop)};
1712+
mkv_demuxer_t *mkv_d = demuxer->priv;
1713+
1714+
// The Matroska specification mandates that the PAR is calculated after
1715+
// cropping. However, most files on the internet do not follow this, as it
1716+
// would make them incompatible with crop-unaware players. Additionally,
1717+
// MKVToolNix does not automatically adjust DisplayWidth/DisplayHeight when
1718+
// cropping metadata is added, so most files created with it also do not
1719+
// follow the specification.
1720+
// See the following links for more information:
1721+
// <https://github.com/ietf-wg-cellar/matroska-specification/pull/947>
1722+
// <https://gitlab.com/mbunkus/mkvtoolnix/-/issues/2389>
1723+
// <https://github.com/mpv-player/mpv/pull/13446>
1724+
int crop_compat_w = mkv_d->opts->crop_compat ? track->v_width : mp_rect_w(crop);
1725+
int crop_compat_h = mkv_d->opts->crop_compat ? track->v_height : mp_rect_h(crop);
1726+
1727+
int dw = track->v_dwidth_set ? track->v_dwidth : crop_compat_w;
1728+
int dh = track->v_dheight_set ? track->v_dheight : crop_compat_h;
1729+
struct mp_image_params p = {.w = crop_compat_w, .h = crop_compat_h};
17121730
mp_image_params_set_dsize(&p, dw, dh);
17131731
sh_v->par_w = p.p_w;
17141732
sh_v->par_h = p.p_h;

0 commit comments

Comments
 (0)