Skip to content

Commit a83b77d

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 99e95c5 commit a83b77d

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-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: no)
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: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ struct demux_mkv_opts {
236236
double subtitle_preroll_secs_index;
237237
int probe_duration;
238238
bool probe_start_time;
239+
bool crop_compat;
239240
};
240241

241242
const struct m_sub_options demux_mkv_conf = {
@@ -249,6 +250,7 @@ const struct m_sub_options demux_mkv_conf = {
249250
{"probe-video-duration", OPT_CHOICE(probe_duration,
250251
{"no", 0}, {"yes", 1}, {"full", 2})},
251252
{"probe-start-time", OPT_BOOL(probe_start_time)},
253+
{"crop-compat", OPT_BOOL(crop_compat)},
252254
{0}
253255
},
254256
.size = sizeof(struct demux_mkv_opts),
@@ -1683,9 +1685,24 @@ static int demux_mkv_open_video(demuxer_t *demuxer, mkv_track_t *track)
16831685
sh_v->crop = crop;
16841686
}
16851687

1686-
int dw = track->v_dwidth_set ? track->v_dwidth : mp_rect_w(crop);
1687-
int dh = track->v_dheight_set ? track->v_dheight : mp_rect_h(crop);
1688-
struct mp_image_params p = {.w = mp_rect_w(crop), .h = mp_rect_h(crop)};
1688+
mkv_demuxer_t *mkv_d = demuxer->priv;
1689+
1690+
// The Matroska specification mandates that the PAR is calculated after
1691+
// cropping. However, most files on the internet do not follow this, as it
1692+
// would make them incompatible with crop-unaware players. Additionally,
1693+
// MKVToolNix does not automatically adjust DisplayWidth/DisplayHeight when
1694+
// cropping metadata is added, so most files created with it also do not
1695+
// follow the specification.
1696+
// See the following links for more information:
1697+
// <https://github.com/ietf-wg-cellar/matroska-specification/pull/947>
1698+
// <https://gitlab.com/mbunkus/mkvtoolnix/-/issues/2389>
1699+
// <https://github.com/mpv-player/mpv/pull/13446>
1700+
int crop_compat_w = mkv_d->opts->crop_compat ? track->v_width : mp_rect_w(crop);
1701+
int crop_compat_h = mkv_d->opts->crop_compat ? track->v_height : mp_rect_h(crop);
1702+
1703+
int dw = track->v_dwidth_set ? track->v_dwidth : crop_compat_w;
1704+
int dh = track->v_dheight_set ? track->v_dheight : crop_compat_h;
1705+
struct mp_image_params p = {.w = crop_compat_w, .h = crop_compat_h};
16891706
mp_image_params_set_dsize(&p, dw, dh);
16901707
sh_v->par_w = p.p_w;
16911708
sh_v->par_h = p.p_h;

0 commit comments

Comments
 (0)