2 * soc-camera generic scaling-cropping manipulation functions
4 * Copyright (C) 2013 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/device.h>
13 #include <linux/module.h>
15 #include <media/soc_camera.h>
16 #include <media/v4l2-common.h>
18 #include "soc_scale_crop.h"
21 #define dev_geo dev_info
23 #define dev_geo dev_dbg
26 /* Check if any dimension of r1 is smaller than respective one of r2 */
27 static bool is_smaller(const struct v4l2_rect
*r1
, const struct v4l2_rect
*r2
)
29 return r1
->width
< r2
->width
|| r1
->height
< r2
->height
;
32 /* Check if r1 fails to cover r2 */
33 static bool is_inside(const struct v4l2_rect
*r1
, const struct v4l2_rect
*r2
)
35 return r1
->left
> r2
->left
|| r1
->top
> r2
->top
||
36 r1
->left
+ r1
->width
< r2
->left
+ r2
->width
||
37 r1
->top
+ r1
->height
< r2
->top
+ r2
->height
;
40 /* Get and store current client crop */
41 int soc_camera_client_g_rect(struct v4l2_subdev
*sd
, struct v4l2_rect
*rect
)
43 struct v4l2_subdev_selection sdsel
= {
44 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
45 .target
= V4L2_SEL_TGT_CROP
,
49 ret
= v4l2_subdev_call(sd
, pad
, get_selection
, NULL
, &sdsel
);
55 sdsel
.target
= V4L2_SEL_TGT_CROP_DEFAULT
;
56 ret
= v4l2_subdev_call(sd
, pad
, get_selection
, NULL
, &sdsel
);
62 EXPORT_SYMBOL(soc_camera_client_g_rect
);
64 /* Client crop has changed, update our sub-rectangle to remain within the area */
65 static void update_subrect(struct v4l2_rect
*rect
, struct v4l2_rect
*subrect
)
67 if (rect
->width
< subrect
->width
)
68 subrect
->width
= rect
->width
;
70 if (rect
->height
< subrect
->height
)
71 subrect
->height
= rect
->height
;
73 if (rect
->left
> subrect
->left
)
74 subrect
->left
= rect
->left
;
75 else if (rect
->left
+ rect
->width
>
76 subrect
->left
+ subrect
->width
)
77 subrect
->left
= rect
->left
+ rect
->width
-
80 if (rect
->top
> subrect
->top
)
81 subrect
->top
= rect
->top
;
82 else if (rect
->top
+ rect
->height
>
83 subrect
->top
+ subrect
->height
)
84 subrect
->top
= rect
->top
+ rect
->height
-
89 * The common for both scaling and cropping iterative approach is:
90 * 1. try if the client can produce exactly what requested by the user
91 * 2. if (1) failed, try to double the client image until we get one big enough
92 * 3. if (2) failed, try to request the maximum image
94 int soc_camera_client_s_selection(struct v4l2_subdev
*sd
,
95 struct v4l2_selection
*sel
, struct v4l2_selection
*cam_sel
,
96 struct v4l2_rect
*target_rect
, struct v4l2_rect
*subrect
)
98 struct v4l2_subdev_selection sdsel
= {
99 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
100 .target
= sel
->target
,
104 struct v4l2_subdev_selection bounds
= {
105 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
106 .target
= V4L2_SEL_TGT_CROP_BOUNDS
,
108 struct v4l2_rect
*rect
= &sel
->r
, *cam_rect
= &cam_sel
->r
;
109 struct device
*dev
= sd
->v4l2_dev
->dev
;
111 unsigned int width
, height
;
113 v4l2_subdev_call(sd
, pad
, set_selection
, NULL
, &sdsel
);
115 ret
= soc_camera_client_g_rect(sd
, cam_rect
);
120 * Now cam_crop contains the current camera input rectangle, and it must
121 * be within camera cropcap bounds
123 if (!memcmp(rect
, cam_rect
, sizeof(*rect
))) {
124 /* Even if camera S_SELECTION failed, but camera rectangle matches */
125 dev_dbg(dev
, "Camera S_SELECTION successful for %dx%d@%d:%d\n",
126 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
127 *target_rect
= *cam_rect
;
131 /* Try to fix cropping, that camera hasn't managed to set */
132 dev_geo(dev
, "Fix camera S_SELECTION for %dx%d@%d:%d to %dx%d@%d:%d\n",
133 cam_rect
->width
, cam_rect
->height
,
134 cam_rect
->left
, cam_rect
->top
,
135 rect
->width
, rect
->height
, rect
->left
, rect
->top
);
137 /* We need sensor maximum rectangle */
138 ret
= v4l2_subdev_call(sd
, pad
, get_selection
, NULL
, &bounds
);
142 /* Put user requested rectangle within sensor bounds */
143 soc_camera_limit_side(&rect
->left
, &rect
->width
, sdsel
.r
.left
, 2,
145 soc_camera_limit_side(&rect
->top
, &rect
->height
, sdsel
.r
.top
, 4,
149 * Popular special case - some cameras can only handle fixed sizes like
150 * QVGA, VGA,... Take care to avoid infinite loop.
152 width
= max_t(unsigned int, cam_rect
->width
, 2);
153 height
= max_t(unsigned int, cam_rect
->height
, 2);
156 * Loop as long as sensor is not covering the requested rectangle and
157 * is still within its bounds
159 while (!ret
&& (is_smaller(cam_rect
, rect
) ||
160 is_inside(cam_rect
, rect
)) &&
161 (bounds
.r
.width
> width
|| bounds
.r
.height
> height
)) {
166 cam_rect
->width
= width
;
167 cam_rect
->height
= height
;
170 * We do not know what capabilities the camera has to set up
171 * left and top borders. We could try to be smarter in iterating
172 * them, e.g., if camera current left is to the right of the
173 * target left, set it to the middle point between the current
174 * left and minimum left. But that would add too much
175 * complexity: we would have to iterate each border separately.
176 * Instead we just drop to the left and top bounds.
178 if (cam_rect
->left
> rect
->left
)
179 cam_rect
->left
= bounds
.r
.left
;
181 if (cam_rect
->left
+ cam_rect
->width
< rect
->left
+ rect
->width
)
182 cam_rect
->width
= rect
->left
+ rect
->width
-
185 if (cam_rect
->top
> rect
->top
)
186 cam_rect
->top
= bounds
.r
.top
;
188 if (cam_rect
->top
+ cam_rect
->height
< rect
->top
+ rect
->height
)
189 cam_rect
->height
= rect
->top
+ rect
->height
-
193 v4l2_subdev_call(sd
, pad
, set_selection
, NULL
, &sdsel
);
195 ret
= soc_camera_client_g_rect(sd
, cam_rect
);
196 dev_geo(dev
, "Camera S_SELECTION %d for %dx%d@%d:%d\n", ret
,
197 cam_rect
->width
, cam_rect
->height
,
198 cam_rect
->left
, cam_rect
->top
);
201 /* S_SELECTION must not modify the rectangle */
202 if (is_smaller(cam_rect
, rect
) || is_inside(cam_rect
, rect
)) {
204 * The camera failed to configure a suitable cropping,
205 * we cannot use the current rectangle, set to max
208 v4l2_subdev_call(sd
, pad
, set_selection
, NULL
, &sdsel
);
211 ret
= soc_camera_client_g_rect(sd
, cam_rect
);
212 dev_geo(dev
, "Camera S_SELECTION %d for max %dx%d@%d:%d\n", ret
,
213 cam_rect
->width
, cam_rect
->height
,
214 cam_rect
->left
, cam_rect
->top
);
218 *target_rect
= *cam_rect
;
219 update_subrect(target_rect
, subrect
);
224 EXPORT_SYMBOL(soc_camera_client_s_selection
);
226 /* Iterative set_fmt, also updates cached client crop on success */
227 static int client_set_fmt(struct soc_camera_device
*icd
,
228 struct v4l2_rect
*rect
, struct v4l2_rect
*subrect
,
229 unsigned int max_width
, unsigned int max_height
,
230 struct v4l2_subdev_format
*format
, bool host_can_scale
)
232 struct v4l2_subdev
*sd
= soc_camera_to_subdev(icd
);
233 struct device
*dev
= icd
->parent
;
234 struct v4l2_mbus_framefmt
*mf
= &format
->format
;
235 unsigned int width
= mf
->width
, height
= mf
->height
, tmp_w
, tmp_h
;
236 struct v4l2_subdev_selection sdsel
= {
237 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
238 .target
= V4L2_SEL_TGT_CROP_BOUNDS
,
243 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
,
244 soc_camera_grp_id(icd
), pad
,
245 set_fmt
, NULL
, format
);
249 dev_geo(dev
, "camera scaled to %ux%u\n", mf
->width
, mf
->height
);
251 if (width
== mf
->width
&& height
== mf
->height
) {
252 /* Perfect! The client has done it all. */
261 ret
= v4l2_subdev_call(sd
, pad
, get_selection
, NULL
, &sdsel
);
265 if (max_width
> sdsel
.r
.width
)
266 max_width
= sdsel
.r
.width
;
267 if (max_height
> sdsel
.r
.height
)
268 max_height
= sdsel
.r
.height
;
270 /* Camera set a format, but geometry is not precise, try to improve */
274 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
275 while ((width
> tmp_w
|| height
> tmp_h
) &&
276 tmp_w
< max_width
&& tmp_h
< max_height
) {
277 tmp_w
= min(2 * tmp_w
, max_width
);
278 tmp_h
= min(2 * tmp_h
, max_height
);
281 ret
= v4l2_device_call_until_err(sd
->v4l2_dev
,
282 soc_camera_grp_id(icd
), pad
,
283 set_fmt
, NULL
, format
);
284 dev_geo(dev
, "Camera scaled to %ux%u\n",
285 mf
->width
, mf
->height
);
287 /* This shouldn't happen */
288 dev_err(dev
, "Client failed to set format: %d\n", ret
);
295 ret
= soc_camera_client_g_rect(sd
, rect
);
302 update_subrect(rect
, subrect
);
308 * @icd - soc-camera device
309 * @rect - camera cropping window
310 * @subrect - part of rect, sent to the user
311 * @mf - in- / output camera output window
312 * @width - on input: max host input width
313 * on output: user width, mapped back to input
314 * @height - on input: max host input height
315 * on output: user height, mapped back to input
316 * @host_can_scale - host can scale this pixel format
317 * @shift - shift, used for scaling
319 int soc_camera_client_scale(struct soc_camera_device
*icd
,
320 struct v4l2_rect
*rect
, struct v4l2_rect
*subrect
,
321 struct v4l2_mbus_framefmt
*mf
,
322 unsigned int *width
, unsigned int *height
,
323 bool host_can_scale
, unsigned int shift
)
325 struct device
*dev
= icd
->parent
;
326 struct v4l2_subdev_format fmt_tmp
= {
327 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
330 struct v4l2_mbus_framefmt
*mf_tmp
= &fmt_tmp
.format
;
331 unsigned int scale_h
, scale_v
;
335 * 5. Apply iterative camera S_FMT for camera user window (also updates
336 * client crop cache and the imaginary sub-rectangle).
338 ret
= client_set_fmt(icd
, rect
, subrect
, *width
, *height
,
339 &fmt_tmp
, host_can_scale
);
343 dev_geo(dev
, "5: camera scaled to %ux%u\n",
344 mf_tmp
->width
, mf_tmp
->height
);
346 /* 6. Retrieve camera output window (g_fmt) */
348 /* unneeded - it is already in "mf_tmp" */
350 /* 7. Calculate new client scales. */
351 scale_h
= soc_camera_calc_scale(rect
->width
, shift
, mf_tmp
->width
);
352 scale_v
= soc_camera_calc_scale(rect
->height
, shift
, mf_tmp
->height
);
354 mf
->width
= mf_tmp
->width
;
355 mf
->height
= mf_tmp
->height
;
356 mf
->colorspace
= mf_tmp
->colorspace
;
359 * 8. Calculate new host crop - apply camera scales to previously
360 * updated "effective" crop.
362 *width
= soc_camera_shift_scale(subrect
->width
, shift
, scale_h
);
363 *height
= soc_camera_shift_scale(subrect
->height
, shift
, scale_v
);
365 dev_geo(dev
, "8: new client sub-window %ux%u\n", *width
, *height
);
369 EXPORT_SYMBOL(soc_camera_client_scale
);
372 * Calculate real client output window by applying new scales to the current
373 * client crop. New scales are calculated from the requested output format and
374 * host crop, mapped backed onto the client input (subrect).
376 void soc_camera_calc_client_output(struct soc_camera_device
*icd
,
377 struct v4l2_rect
*rect
, struct v4l2_rect
*subrect
,
378 const struct v4l2_pix_format
*pix
, struct v4l2_mbus_framefmt
*mf
,
381 struct device
*dev
= icd
->parent
;
382 unsigned int scale_v
, scale_h
;
384 if (subrect
->width
== rect
->width
&&
385 subrect
->height
== rect
->height
) {
386 /* No sub-cropping */
387 mf
->width
= pix
->width
;
388 mf
->height
= pix
->height
;
392 /* 1.-2. Current camera scales and subwin - cached. */
394 dev_geo(dev
, "2: subwin %ux%u@%u:%u\n",
395 subrect
->width
, subrect
->height
,
396 subrect
->left
, subrect
->top
);
399 * 3. Calculate new combined scales from input sub-window to requested
404 * TODO: CEU cannot scale images larger than VGA to smaller than SubQCIF
405 * (128x96) or larger than VGA. This and similar limitations have to be
406 * taken into account here.
408 scale_h
= soc_camera_calc_scale(subrect
->width
, shift
, pix
->width
);
409 scale_v
= soc_camera_calc_scale(subrect
->height
, shift
, pix
->height
);
411 dev_geo(dev
, "3: scales %u:%u\n", scale_h
, scale_v
);
414 * 4. Calculate desired client output window by applying combined scales
415 * to client (real) input window.
417 mf
->width
= soc_camera_shift_scale(rect
->width
, shift
, scale_h
);
418 mf
->height
= soc_camera_shift_scale(rect
->height
, shift
, scale_v
);
420 EXPORT_SYMBOL(soc_camera_calc_client_output
);