From c40b064e38b762e14f0b50229ccbf882ddb9bffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= Date: Fri, 1 Sep 2023 22:26:05 +0200 Subject: [PATCH] demux: add crop to mp_codec_params --- DOCS/man/input.rst | 10 ++++++++++ demux/stheader.h | 1 + filters/f_decoder_wrapper.c | 19 +++++++++++++++++++ player/command.c | 5 +++++ 4 files changed, 35 insertions(+) diff --git a/DOCS/man/input.rst b/DOCS/man/input.rst index 9c9bca5a5d..5de39d78e8 100644 --- a/DOCS/man/input.rst +++ b/DOCS/man/input.rst @@ -2965,6 +2965,12 @@ Property list ``track-list/N/demux-w``, ``track-list/N/demux-h`` Video size hint as indicated by the container. (Not always accurate.) + ``track-list/N/demux-crop-x``, ``track-list/N/demux-crop-y`` + Crop offset of the source video frame. + + ``track-list/N/demux-crop-w``, ``track-list/N/demux-crop-h`` + Video size after cropping. + ``track-list/N/demux-channel-count`` Number of audio channels as indicated by the container. (Not always accurate - in particular, the track could be decoded as a different @@ -3027,6 +3033,10 @@ Property list "decoder-desc" MPV_FORMAT_STRING "demux-w" MPV_FORMAT_INT64 "demux-h" MPV_FORMAT_INT64 + "demux-crop-x" MPV_FORMAT_INT64 + "demux-crop-y" MPV_FORMAT_INT64 + "demux-crop-w" MPV_FORMAT_INT64 + "demux-crop-h" MPV_FORMAT_INT64 "demux-channel-count" MPV_FORMAT_INT64 "demux-channels" MPV_FORMAT_STRING "demux-samplerate" MPV_FORMAT_INT64 diff --git a/demux/stheader.h b/demux/stheader.h index 447bc0aa1f..1bc036d648 100644 --- a/demux/stheader.h +++ b/demux/stheader.h @@ -106,6 +106,7 @@ struct mp_codec_params { int rotate; // intended display rotation, in degrees, [0, 359] int stereo_mode; // mp_stereo3d_mode (0 if none/unknown) struct mp_colorspace color; // colorspace info where available + struct mp_rect crop; // crop to be applied // STREAM_VIDEO + STREAM_AUDIO int bits_per_coded_sample; diff --git a/filters/f_decoder_wrapper.c b/filters/f_decoder_wrapper.c index 5553002821..7e51c57a92 100644 --- a/filters/f_decoder_wrapper.c +++ b/filters/f_decoder_wrapper.c @@ -593,6 +593,25 @@ static void fix_image_params(struct priv *p, m.stereo3d = p->codec->stereo_mode; + if (!mp_rect_equals(&p->codec->crop, &(struct mp_rect){0})) { + struct mp_rect crop = p->codec->crop; + // Offset to respect existing decoder crop. + crop.x0 += m.crop.x0; + crop.x1 += m.crop.x0; + crop.y0 += m.crop.y0; + crop.y1 += m.crop.y0; + // Crop has to be inside existing image bounds. + if (mp_image_crop_valid(&(struct mp_image_params) { + .w = mp_rect_w(m.crop), .h = mp_rect_h(m.crop), .crop = crop })) + { + m.crop = crop; + } else { + MP_WARN(p, "Invalid container crop %dx%d+%d+%d for %dx%d image\n", + mp_rect_w(crop), mp_rect_h(crop), crop.x0, crop.y0, + mp_rect_w(m.crop), mp_rect_h(m.crop)); + } + } + if (opts->video_rotate < 0) { m.rotate = 0; } else { diff --git a/player/command.c b/player/command.c index dd0b0259de..6416ba6db3 100644 --- a/player/command.c +++ b/player/command.c @@ -1959,6 +1959,7 @@ static int get_track_entry(int item, int action, void *arg, void *ctx) } } + bool has_crop = mp_rect_w(p.crop) > 0 && mp_rect_h(p.crop) > 0; struct m_sub_property props[] = { {"id", SUB_PROP_INT(track->user_tid)}, {"type", SUB_PROP_STR(stream_type_name(track->type)), @@ -1994,6 +1995,10 @@ static int get_track_entry(int item, int action, void *arg, void *ctx) .unavailable = !p.codec}, {"demux-w", SUB_PROP_INT(p.disp_w), .unavailable = !p.disp_w}, {"demux-h", SUB_PROP_INT(p.disp_h), .unavailable = !p.disp_h}, + {"demux-crop-x",SUB_PROP_INT(p.crop.x0), .unavailable = !has_crop}, + {"demux-crop-y",SUB_PROP_INT(p.crop.y0), .unavailable = !has_crop}, + {"demux-crop-w",SUB_PROP_INT(mp_rect_w(p.crop)), .unavailable = !has_crop}, + {"demux-crop-h",SUB_PROP_INT(mp_rect_h(p.crop)), .unavailable = !has_crop}, {"demux-channel-count", SUB_PROP_INT(p.channels.num), .unavailable = !p.channels.num}, {"demux-channels", SUB_PROP_STR(mp_chmap_to_str(&p.channels)),