[FFmpeg-devel] [PATCH] libavdevice: allow cropping avfoundation screen capture

Thilo Borgmann thilo.borgmann at mail.de
Tue Oct 9 15:10:24 EEST 2018


Hi,

Am 17.09.18 um 05:53 schrieb Alan.Birtles at sony.com:
> 2018-09-13 17:52 GMT+02:00,
> Alan.Birtles at sony.com<mailto:Alan.Birtles at sony.com>
> <Alan.Birtles at sony.com<mailto:Alan.Birtles at sony.com>>:
> 
> 
> 
>> I've developed  a patch to add an option to allow cropping of the
> 
>> avfoundation screen capture.
> 
>> Note that if the captured width is not a multiple of 16 then
> 
>> https://urldefense.proofpoint.com/v2/url?u=https-3A__trac.ffmpeg.org_ticket_5654&d=DwIGaQ&c=fP4tf--1dS0biCFlB0saz0I0kjO5v7-GLPtvShAo4cc&r=DZ-pOCz_nOIJfdxhe1zNBFwbfB1WJzA5E9fM05n7yCs&m=KjmPPiSi2W239ebqa6B1xWjvn3F0kSW7fSlmVx4n70s&s=1a0Z8KLo3pYG0TjLl0zZzlF3E68mO58ssmKjXnW9yYg&e=
>> will be triggered.

This had been fixed in a259501e5a6ed07d76ce5d615123148b9b68d9f8.


> From 96b9cd33022bb6f3147174085c9ff2417cd006bc Mon Sep 17 00:00:00 2001
> From: Alan Birtles <alan.birtles at eu.sony.com>
> Date: Thu, 13 Sep 2018 15:56:55 +0100
> Subject: [PATCH] allow specifying "cropRect" for avfoundation screen capture
> 
> ---
>  libavdevice/avfoundation.m | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)
> 
> diff --git a/libavdevice/avfoundation.m b/libavdevice/avfoundation.m
> index fc6428a..c285984 100644
> --- a/libavdevice/avfoundation.m
> +++ b/libavdevice/avfoundation.m
> @@ -97,6 +97,10 @@
>  
>      int             capture_cursor;
>      int             capture_mouse_clicks;

> +    int             capture_crop_x;
> +    int             capture_crop_y;
> +    int             capture_crop_width;
> +    int             capture_crop_height;

Please remove the "capture_" prefix - if you see it fit, please use something
like "screen_".


>      int             list_devices;
>      int             video_device_index;
> @@ -750,6 +754,13 @@ static int avf_read_header(AVFormatContext *s)
>                  capture_screen_input.capturesMouseClicks = NO;
>              }
>  
> +            if (ctx->capture_crop_x != 0 ||
> +                ctx->capture_crop_y != 0 ||
> +                ctx->capture_crop_width != 0 ||
> +                ctx->capture_crop_height != 0) {
> +                capture_screen_input.cropRect = CGRectMake(ctx->capture_crop_x, ctx->capture_crop_y, ctx->capture_crop_width, ctx->capture_crop_height);
> +            }
> +

Unfortunately, AVFoundation does not allow for easily retrieving the screen
resolution before the capture session starts... So please add a warning if
crop_width or crop_height are 0 / unspecified. Also the if should check for >=1
in width and height, as well as >= 0 for x and y to avoid invalid cropping regions.

>              video_device = (AVCaptureDevice*) capture_screen_input;
>              capture_screen = 1;
>  #endif
> @@ -799,6 +810,13 @@ static int avf_read_header(AVFormatContext *s)
>                  } else {
>                      capture_screen_input.capturesMouseClicks = NO;
>                  }
> +
> +                if (ctx->capture_crop_x != 0 ||
> +                    ctx->capture_crop_y != 0 ||
> +                    ctx->capture_crop_width != 0 ||
> +                    ctx->capture_crop_height != 0) {
> +                    capture_screen_input.cropRect = CGRectMake(ctx->capture_crop_x, ctx->capture_crop_y, ctx->capture_crop_width, ctx->capture_crop_height);
> +                }

Same here.

>              }
>          }
>  #endif
> @@ -1069,6 +1087,9 @@ static int avf_close(AVFormatContext *s)
>      { "video_size", "set video size", offsetof(AVFContext, width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
>      { "capture_cursor", "capture the screen cursor", offsetof(AVFContext, capture_cursor), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
>      { "capture_mouse_clicks", "capture the screen mouse clicks", offsetof(AVFContext, capture_mouse_clicks), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },

> +    { "capture_crop_x", "crop the screen capture to the specified x offset", offsetof(AVFContext, capture_crop_x), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
> +    { "capture_crop_y", "crop the screen capture to the specified y offset", offsetof(AVFContext, capture_crop_y), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
> +    { "capture_crop_size", "crop the screen capture to the specified size", offsetof(AVFContext, capture_crop_width), AV_OPT_TYPE_IMAGE_SIZE,  {.str = NULL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },

See naming suggestions from above. Also, please change to:

"crop the screen capture starting at the specified x offset"

for x and y. Also, please add a description of the new parameters to
doc/indevs.texi. Please add a warning in the code that these options are without
effect whenever a non-screen device is selected but these options are set
(results in unexpected output). You might suggest in the message to apply an
additional non-avfoundation crop filter in the filter chain.

Thanks,
Thilo


More information about the ffmpeg-devel mailing list