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:
https://github.com/ietf-wg-cellar/matroska-specification/pull/947
https://gitlab.com/mbunkus/mkvtoolnix/-/issues/2389
https://github.com/mpv-player/mpv/pull/13446

Fixes: #15800
This commit is contained in:
Kacper Michajłow committed 2025-03-04 19:33:27 +01:00
1 parent 403d57dbf3
commit 1582705f27
3 files changed
+45 -3

No files matched your search

+21 -3
View File
@@ -237,6 +237,7 @@ struct demux_mkv_opts {
double subtitle_preroll_secs_index;
int probe_duration;
bool probe_start_time;
bool crop_compat;
};
const struct m_sub_options demux_mkv_conf = {
@@ -250,6 +251,7 @@ const struct m_sub_options demux_mkv_conf = {
{"probe-video-duration", OPT_CHOICE(probe_duration,
{"no", 0}, {"yes", 1}, {"full", 2})},
{"probe-start-time", OPT_BOOL(probe_start_time)},
{"crop-compat", OPT_BOOL(crop_compat)},
{0}
},
.size = sizeof(struct demux_mkv_opts),
@@ -258,6 +260,7 @@ const struct m_sub_options demux_mkv_conf = {
.subtitle_preroll_secs = 1.0,
.subtitle_preroll_secs_index = 10.0,
.probe_start_time = true,
.crop_compat = true,
},
.change_flags = UPDATE_DEMUXER,
};
@@ -1706,9 +1709,24 @@ static int demux_mkv_open_video(demuxer_t *demuxer, mkv_track_t *track)
sh_v->crop = crop;
}
int dw = track->v_dwidth_set ? track->v_dwidth : mp_rect_w(crop);
int dh = track->v_dheight_set ? track->v_dheight : mp_rect_h(crop);
struct mp_image_params p = {.w = mp_rect_w(crop), .h = mp_rect_h(crop)};
mkv_demuxer_t *mkv_d = demuxer->priv;
// The Matroska specification mandates that the PAR is calculated after
// cropping. However, most files on the internet do not follow this, as it
// would make them incompatible with crop-unaware players. Additionally,
// MKVToolNix does not automatically adjust DisplayWidth/DisplayHeight when
// cropping metadata is added, so most files created with it also do not
// follow the specification.
// See the following links for more information:
// <https://github.com/ietf-wg-cellar/matroska-specification/pull/947>
// <https://gitlab.com/mbunkus/mkvtoolnix/-/issues/2389>
// <https://github.com/mpv-player/mpv/pull/13446>
int crop_compat_w = mkv_d->opts->crop_compat ? track->v_width : mp_rect_w(crop);
int crop_compat_h = mkv_d->opts->crop_compat ? track->v_height : mp_rect_h(crop);
int dw = track->v_dwidth_set ? track->v_dwidth : crop_compat_w;
int dh = track->v_dheight_set ? track->v_dheight : crop_compat_h;
struct mp_image_params p = {.w = crop_compat_w, .h = crop_compat_h};
mp_image_params_set_dsize(&p, dw, dh);
sh_v->par_w = p.p_w;
sh_v->par_h = p.p_h;