soc_scale_crop.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * soc-camera generic scaling-cropping manipulation functions
  3. *
  4. * Copyright (C) 2013 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  5. *
  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.
  10. */
  11. #include <linux/device.h>
  12. #include <linux/module.h>
  13. #include <media/soc_camera.h>
  14. #include <media/v4l2-common.h>
  15. #include "soc_scale_crop.h"
  16. #ifdef DEBUG_GEOMETRY
  17. #define dev_geo dev_info
  18. #else
  19. #define dev_geo dev_dbg
  20. #endif
  21. /* Check if any dimension of r1 is smaller than respective one of r2 */
  22. static bool is_smaller(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
  23. {
  24. return r1->width < r2->width || r1->height < r2->height;
  25. }
  26. /* Check if r1 fails to cover r2 */
  27. static bool is_inside(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
  28. {
  29. return r1->left > r2->left || r1->top > r2->top ||
  30. r1->left + r1->width < r2->left + r2->width ||
  31. r1->top + r1->height < r2->top + r2->height;
  32. }
  33. /* Get and store current client crop */
  34. int soc_camera_client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
  35. {
  36. struct v4l2_crop crop;
  37. struct v4l2_cropcap cap;
  38. int ret;
  39. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  40. ret = v4l2_subdev_call(sd, video, g_crop, &crop);
  41. if (!ret) {
  42. *rect = crop.c;
  43. return ret;
  44. }
  45. /* Camera driver doesn't support .g_crop(), assume default rectangle */
  46. cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  47. ret = v4l2_subdev_call(sd, video, cropcap, &cap);
  48. if (!ret)
  49. *rect = cap.defrect;
  50. return ret;
  51. }
  52. EXPORT_SYMBOL(soc_camera_client_g_rect);
  53. /* Client crop has changed, update our sub-rectangle to remain within the area */
  54. static void update_subrect(struct v4l2_rect *rect, struct v4l2_rect *subrect)
  55. {
  56. if (rect->width < subrect->width)
  57. subrect->width = rect->width;
  58. if (rect->height < subrect->height)
  59. subrect->height = rect->height;
  60. if (rect->left > subrect->left)
  61. subrect->left = rect->left;
  62. else if (rect->left + rect->width >
  63. subrect->left + subrect->width)
  64. subrect->left = rect->left + rect->width -
  65. subrect->width;
  66. if (rect->top > subrect->top)
  67. subrect->top = rect->top;
  68. else if (rect->top + rect->height >
  69. subrect->top + subrect->height)
  70. subrect->top = rect->top + rect->height -
  71. subrect->height;
  72. }
  73. /*
  74. * The common for both scaling and cropping iterative approach is:
  75. * 1. try if the client can produce exactly what requested by the user
  76. * 2. if (1) failed, try to double the client image until we get one big enough
  77. * 3. if (2) failed, try to request the maximum image
  78. */
  79. int soc_camera_client_s_crop(struct v4l2_subdev *sd,
  80. struct v4l2_crop *crop, struct v4l2_crop *cam_crop,
  81. struct v4l2_rect *target_rect, struct v4l2_rect *subrect)
  82. {
  83. struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
  84. struct device *dev = sd->v4l2_dev->dev;
  85. struct v4l2_cropcap cap;
  86. int ret;
  87. unsigned int width, height;
  88. v4l2_subdev_call(sd, video, s_crop, crop);
  89. ret = soc_camera_client_g_rect(sd, cam_rect);
  90. if (ret < 0)
  91. return ret;
  92. /*
  93. * Now cam_crop contains the current camera input rectangle, and it must
  94. * be within camera cropcap bounds
  95. */
  96. if (!memcmp(rect, cam_rect, sizeof(*rect))) {
  97. /* Even if camera S_CROP failed, but camera rectangle matches */
  98. dev_dbg(dev, "Camera S_CROP successful for %dx%d@%d:%d\n",
  99. rect->width, rect->height, rect->left, rect->top);
  100. *target_rect = *cam_rect;
  101. return 0;
  102. }
  103. /* Try to fix cropping, that camera hasn't managed to set */
  104. dev_geo(dev, "Fix camera S_CROP for %dx%d@%d:%d to %dx%d@%d:%d\n",
  105. cam_rect->width, cam_rect->height,
  106. cam_rect->left, cam_rect->top,
  107. rect->width, rect->height, rect->left, rect->top);
  108. /* We need sensor maximum rectangle */
  109. ret = v4l2_subdev_call(sd, video, cropcap, &cap);
  110. if (ret < 0)
  111. return ret;
  112. /* Put user requested rectangle within sensor bounds */
  113. soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
  114. cap.bounds.width);
  115. soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
  116. cap.bounds.height);
  117. /*
  118. * Popular special case - some cameras can only handle fixed sizes like
  119. * QVGA, VGA,... Take care to avoid infinite loop.
  120. */
  121. width = max_t(unsigned int, cam_rect->width, 2);
  122. height = max_t(unsigned int, cam_rect->height, 2);
  123. /*
  124. * Loop as long as sensor is not covering the requested rectangle and
  125. * is still within its bounds
  126. */
  127. while (!ret && (is_smaller(cam_rect, rect) ||
  128. is_inside(cam_rect, rect)) &&
  129. (cap.bounds.width > width || cap.bounds.height > height)) {
  130. width *= 2;
  131. height *= 2;
  132. cam_rect->width = width;
  133. cam_rect->height = height;
  134. /*
  135. * We do not know what capabilities the camera has to set up
  136. * left and top borders. We could try to be smarter in iterating
  137. * them, e.g., if camera current left is to the right of the
  138. * target left, set it to the middle point between the current
  139. * left and minimum left. But that would add too much
  140. * complexity: we would have to iterate each border separately.
  141. * Instead we just drop to the left and top bounds.
  142. */
  143. if (cam_rect->left > rect->left)
  144. cam_rect->left = cap.bounds.left;
  145. if (cam_rect->left + cam_rect->width < rect->left + rect->width)
  146. cam_rect->width = rect->left + rect->width -
  147. cam_rect->left;
  148. if (cam_rect->top > rect->top)
  149. cam_rect->top = cap.bounds.top;
  150. if (cam_rect->top + cam_rect->height < rect->top + rect->height)
  151. cam_rect->height = rect->top + rect->height -
  152. cam_rect->top;
  153. v4l2_subdev_call(sd, video, s_crop, cam_crop);
  154. ret = soc_camera_client_g_rect(sd, cam_rect);
  155. dev_geo(dev, "Camera S_CROP %d for %dx%d@%d:%d\n", ret,
  156. cam_rect->width, cam_rect->height,
  157. cam_rect->left, cam_rect->top);
  158. }
  159. /* S_CROP must not modify the rectangle */
  160. if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
  161. /*
  162. * The camera failed to configure a suitable cropping,
  163. * we cannot use the current rectangle, set to max
  164. */
  165. *cam_rect = cap.bounds;
  166. v4l2_subdev_call(sd, video, s_crop, cam_crop);
  167. ret = soc_camera_client_g_rect(sd, cam_rect);
  168. dev_geo(dev, "Camera S_CROP %d for max %dx%d@%d:%d\n", ret,
  169. cam_rect->width, cam_rect->height,
  170. cam_rect->left, cam_rect->top);
  171. }
  172. if (!ret) {
  173. *target_rect = *cam_rect;
  174. update_subrect(target_rect, subrect);
  175. }
  176. return ret;
  177. }
  178. EXPORT_SYMBOL(soc_camera_client_s_crop);
  179. /* Iterative set_fmt, also updates cached client crop on success */
  180. static int client_set_fmt(struct soc_camera_device *icd,
  181. struct v4l2_rect *rect, struct v4l2_rect *subrect,
  182. unsigned int max_width, unsigned int max_height,
  183. struct v4l2_subdev_format *format, bool host_can_scale)
  184. {
  185. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  186. struct device *dev = icd->parent;
  187. struct v4l2_mbus_framefmt *mf = &format->format;
  188. unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h;
  189. struct v4l2_cropcap cap;
  190. bool host_1to1;
  191. int ret;
  192. ret = v4l2_device_call_until_err(sd->v4l2_dev,
  193. soc_camera_grp_id(icd), pad,
  194. set_fmt, NULL, format);
  195. if (ret < 0)
  196. return ret;
  197. dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height);
  198. if (width == mf->width && height == mf->height) {
  199. /* Perfect! The client has done it all. */
  200. host_1to1 = true;
  201. goto update_cache;
  202. }
  203. host_1to1 = false;
  204. if (!host_can_scale)
  205. goto update_cache;
  206. cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  207. ret = v4l2_subdev_call(sd, video, cropcap, &cap);
  208. if (ret < 0)
  209. return ret;
  210. if (max_width > cap.bounds.width)
  211. max_width = cap.bounds.width;
  212. if (max_height > cap.bounds.height)
  213. max_height = cap.bounds.height;
  214. /* Camera set a format, but geometry is not precise, try to improve */
  215. tmp_w = mf->width;
  216. tmp_h = mf->height;
  217. /* width <= max_width && height <= max_height - guaranteed by try_fmt */
  218. while ((width > tmp_w || height > tmp_h) &&
  219. tmp_w < max_width && tmp_h < max_height) {
  220. tmp_w = min(2 * tmp_w, max_width);
  221. tmp_h = min(2 * tmp_h, max_height);
  222. mf->width = tmp_w;
  223. mf->height = tmp_h;
  224. ret = v4l2_device_call_until_err(sd->v4l2_dev,
  225. soc_camera_grp_id(icd), pad,
  226. set_fmt, NULL, format);
  227. dev_geo(dev, "Camera scaled to %ux%u\n",
  228. mf->width, mf->height);
  229. if (ret < 0) {
  230. /* This shouldn't happen */
  231. dev_err(dev, "Client failed to set format: %d\n", ret);
  232. return ret;
  233. }
  234. }
  235. update_cache:
  236. /* Update cache */
  237. ret = soc_camera_client_g_rect(sd, rect);
  238. if (ret < 0)
  239. return ret;
  240. if (host_1to1)
  241. *subrect = *rect;
  242. else
  243. update_subrect(rect, subrect);
  244. return 0;
  245. }
  246. /**
  247. * @icd - soc-camera device
  248. * @rect - camera cropping window
  249. * @subrect - part of rect, sent to the user
  250. * @mf - in- / output camera output window
  251. * @width - on input: max host input width
  252. * on output: user width, mapped back to input
  253. * @height - on input: max host input height
  254. * on output: user height, mapped back to input
  255. * @host_can_scale - host can scale this pixel format
  256. * @shift - shift, used for scaling
  257. */
  258. int soc_camera_client_scale(struct soc_camera_device *icd,
  259. struct v4l2_rect *rect, struct v4l2_rect *subrect,
  260. struct v4l2_mbus_framefmt *mf,
  261. unsigned int *width, unsigned int *height,
  262. bool host_can_scale, unsigned int shift)
  263. {
  264. struct device *dev = icd->parent;
  265. struct v4l2_subdev_format fmt_tmp = {
  266. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  267. .format = *mf,
  268. };
  269. struct v4l2_mbus_framefmt *mf_tmp = &fmt_tmp.format;
  270. unsigned int scale_h, scale_v;
  271. int ret;
  272. /*
  273. * 5. Apply iterative camera S_FMT for camera user window (also updates
  274. * client crop cache and the imaginary sub-rectangle).
  275. */
  276. ret = client_set_fmt(icd, rect, subrect, *width, *height,
  277. &fmt_tmp, host_can_scale);
  278. if (ret < 0)
  279. return ret;
  280. dev_geo(dev, "5: camera scaled to %ux%u\n",
  281. mf_tmp->width, mf_tmp->height);
  282. /* 6. Retrieve camera output window (g_fmt) */
  283. /* unneeded - it is already in "mf_tmp" */
  284. /* 7. Calculate new client scales. */
  285. scale_h = soc_camera_calc_scale(rect->width, shift, mf_tmp->width);
  286. scale_v = soc_camera_calc_scale(rect->height, shift, mf_tmp->height);
  287. mf->width = mf_tmp->width;
  288. mf->height = mf_tmp->height;
  289. mf->colorspace = mf_tmp->colorspace;
  290. /*
  291. * 8. Calculate new host crop - apply camera scales to previously
  292. * updated "effective" crop.
  293. */
  294. *width = soc_camera_shift_scale(subrect->width, shift, scale_h);
  295. *height = soc_camera_shift_scale(subrect->height, shift, scale_v);
  296. dev_geo(dev, "8: new client sub-window %ux%u\n", *width, *height);
  297. return 0;
  298. }
  299. EXPORT_SYMBOL(soc_camera_client_scale);
  300. /*
  301. * Calculate real client output window by applying new scales to the current
  302. * client crop. New scales are calculated from the requested output format and
  303. * host crop, mapped backed onto the client input (subrect).
  304. */
  305. void soc_camera_calc_client_output(struct soc_camera_device *icd,
  306. struct v4l2_rect *rect, struct v4l2_rect *subrect,
  307. const struct v4l2_pix_format *pix, struct v4l2_mbus_framefmt *mf,
  308. unsigned int shift)
  309. {
  310. struct device *dev = icd->parent;
  311. unsigned int scale_v, scale_h;
  312. if (subrect->width == rect->width &&
  313. subrect->height == rect->height) {
  314. /* No sub-cropping */
  315. mf->width = pix->width;
  316. mf->height = pix->height;
  317. return;
  318. }
  319. /* 1.-2. Current camera scales and subwin - cached. */
  320. dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
  321. subrect->width, subrect->height,
  322. subrect->left, subrect->top);
  323. /*
  324. * 3. Calculate new combined scales from input sub-window to requested
  325. * user window.
  326. */
  327. /*
  328. * TODO: CEU cannot scale images larger than VGA to smaller than SubQCIF
  329. * (128x96) or larger than VGA. This and similar limitations have to be
  330. * taken into account here.
  331. */
  332. scale_h = soc_camera_calc_scale(subrect->width, shift, pix->width);
  333. scale_v = soc_camera_calc_scale(subrect->height, shift, pix->height);
  334. dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
  335. /*
  336. * 4. Calculate desired client output window by applying combined scales
  337. * to client (real) input window.
  338. */
  339. mf->width = soc_camera_shift_scale(rect->width, shift, scale_h);
  340. mf->height = soc_camera_shift_scale(rect->height, shift, scale_v);
  341. }
  342. EXPORT_SYMBOL(soc_camera_calc_client_output);
  343. MODULE_DESCRIPTION("soc-camera scaling-cropping functions");
  344. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  345. MODULE_LICENSE("GPL");