From 37d0deadd40d57b43bd8020f0143be46f3bf998a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= Date: Fri, 1 Sep 2023 12:24:13 +0200 Subject: [PATCH] video: allow overriding container crop if it is present Setting `--video-crop=0x0+0+0` applies full frame crop, ignoring the container one. Setting --video-crop=0 disables manual crop and restores container one if it is available. --- DOCS/man/options.rst | 7 +++++-- player/command.c | 8 ++++++-- player/video.c | 16 ++++++++++------ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/DOCS/man/options.rst b/DOCS/man/options.rst index 139e32a289..3090be72f0 100644 --- a/DOCS/man/options.rst +++ b/DOCS/man/options.rst @@ -1570,8 +1570,11 @@ Video Crop the video by starting at the x, y offset for w, h pixels. The crop is applied to the source video rectangle (before anamorphic stretch) by the VO. A crop rectangle that is not within the video rectangle will be ignored. - This works with hwdec, unlike the equivalent 'lavfi-crop'. Setting the crop - to '0' disables it. When offset is omitted, the central area will be cropped. + This works with hwdec, unlike the equivalent 'lavfi-crop'. When offset is + omitted, the central area will be cropped. Setting the crop to empty one + ``--video-crop=0x0+0+0`` overrides container crop and disables cropping. + Setting the crop to ``--video-crop=0`` disables manual cropping and restores + the container crop if it's specified. ``--video-zoom=`` Adjust the video display scale factor by the given value. The parameter is diff --git a/player/command.c b/player/command.c index ea41ec1814..dd0b0259de 100644 --- a/player/command.c +++ b/player/command.c @@ -2332,8 +2332,12 @@ static struct mp_image_params get_video_out_params(struct MPContext *mpctx) struct mp_image_params o_params = mpctx->vo_chain->filter->output_params; if (mpctx->video_out) { - m_rect_apply(&o_params.crop, o_params.w, o_params.h, - &mpctx->video_out->opts->video_crop); + struct m_geometry *gm = &mpctx->video_out->opts->video_crop; + if (gm->xy_valid || (gm->wh_valid && (gm->w > 0 || gm->w_per > 0 || + gm->h > 0 || gm->h_per > 0))) + { + m_rect_apply(&o_params.crop, o_params.w, o_params.h, gm); + } } return o_params; diff --git a/player/video.c b/player/video.c index d9e371d9fd..e67fa66785 100644 --- a/player/video.c +++ b/player/video.c @@ -1130,19 +1130,23 @@ void write_video(struct MPContext *mpctx) // Filter output is different from VO input? struct mp_image_params *p = &mpctx->next_frames[0]->params; // Inject vo crop to notify and reconfig if needed - struct mp_rect rc; - m_rect_apply(&rc, p->w, p->h, &vo->opts->video_crop); - if (rc.x1 > 0 && rc.y1 > 0) + struct m_geometry *gm = &vo->opts->video_crop; + struct mp_rect rc = {0}; + if (gm->xy_valid || (gm->wh_valid && (gm->w > 0 || gm->w_per > 0 || + gm->h > 0 || gm->h_per > 0))) { + m_rect_apply(&rc, p->w, p->h, gm); + } + if (rc.x1 > 0 && rc.y1 > 0) { p->crop = rc; if (!mp_image_crop_valid(p)) { - char *str = m_option_type_rect.print(NULL, &vo->opts->video_crop); + char *str = m_option_type_rect.print(NULL, gm); MP_WARN(vo, "Ignoring invalid --video-crop=%s for %dx%d image\n", str, p->w, p->h); talloc_free(str); if (vo->params) { p->crop = vo->params->crop; - vo->opts->video_crop = (struct m_geometry){ + *gm = (struct m_geometry){ .x = p->crop.x0, .y = p->crop.y0, .w = mp_rect_w(p->crop), @@ -1153,7 +1157,7 @@ void write_video(struct MPContext *mpctx) } if (!mp_image_crop_valid(p)) { p->crop = (struct mp_rect){0}; - vo->opts->video_crop = (struct m_geometry){0}; + *gm = (struct m_geometry){0}; } } }