soc_scale_crop.c 12 KB

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