soc_scale_crop.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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(cam_rect->width, 2);
  122. height = max(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 s_mbus_fmt, also updates cached client crop on success */
  180. static int client_s_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_mbus_framefmt *mf, bool host_can_scale)
  184. {
  185. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  186. struct device *dev = icd->parent;
  187. unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h;
  188. struct v4l2_cropcap cap;
  189. bool host_1to1;
  190. int ret;
  191. ret = v4l2_device_call_until_err(sd->v4l2_dev,
  192. soc_camera_grp_id(icd), video,
  193. s_mbus_fmt, mf);
  194. if (ret < 0)
  195. return ret;
  196. dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height);
  197. if (width == mf->width && height == mf->height) {
  198. /* Perfect! The client has done it all. */
  199. host_1to1 = true;
  200. goto update_cache;
  201. }
  202. host_1to1 = false;
  203. if (!host_can_scale)
  204. goto update_cache;
  205. cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  206. ret = v4l2_subdev_call(sd, video, cropcap, &cap);
  207. if (ret < 0)
  208. return ret;
  209. if (max_width > cap.bounds.width)
  210. max_width = cap.bounds.width;
  211. if (max_height > cap.bounds.height)
  212. max_height = cap.bounds.height;
  213. /* Camera set a format, but geometry is not precise, try to improve */
  214. tmp_w = mf->width;
  215. tmp_h = mf->height;
  216. /* width <= max_width && height <= max_height - guaranteed by try_fmt */
  217. while ((width > tmp_w || height > tmp_h) &&
  218. tmp_w < max_width && tmp_h < max_height) {
  219. tmp_w = min(2 * tmp_w, max_width);
  220. tmp_h = min(2 * tmp_h, max_height);
  221. mf->width = tmp_w;
  222. mf->height = tmp_h;
  223. ret = v4l2_device_call_until_err(sd->v4l2_dev,
  224. soc_camera_grp_id(icd), video,
  225. s_mbus_fmt, mf);
  226. dev_geo(dev, "Camera scaled to %ux%u\n",
  227. mf->width, mf->height);
  228. if (ret < 0) {
  229. /* This shouldn't happen */
  230. dev_err(dev, "Client failed to set format: %d\n", ret);
  231. return ret;
  232. }
  233. }
  234. update_cache:
  235. /* Update cache */
  236. ret = soc_camera_client_g_rect(sd, rect);
  237. if (ret < 0)
  238. return ret;
  239. if (host_1to1)
  240. *subrect = *rect;
  241. else
  242. update_subrect(rect, subrect);
  243. return 0;
  244. }
  245. /**
  246. * @icd - soc-camera device
  247. * @rect - camera cropping window
  248. * @subrect - part of rect, sent to the user
  249. * @mf - in- / output camera output window
  250. * @width - on input: max host input width
  251. * on output: user width, mapped back to input
  252. * @height - on input: max host input height
  253. * on output: user height, mapped back to input
  254. * @host_can_scale - host can scale this pixel format
  255. * @shift - shift, used for scaling
  256. */
  257. int soc_camera_client_scale(struct soc_camera_device *icd,
  258. struct v4l2_rect *rect, struct v4l2_rect *subrect,
  259. struct v4l2_mbus_framefmt *mf,
  260. unsigned int *width, unsigned int *height,
  261. bool host_can_scale, unsigned int shift)
  262. {
  263. struct device *dev = icd->parent;
  264. struct v4l2_mbus_framefmt mf_tmp = *mf;
  265. unsigned int scale_h, scale_v;
  266. int ret;
  267. /*
  268. * 5. Apply iterative camera S_FMT for camera user window (also updates
  269. * client crop cache and the imaginary sub-rectangle).
  270. */
  271. ret = client_s_fmt(icd, rect, subrect, *width, *height,
  272. &mf_tmp, host_can_scale);
  273. if (ret < 0)
  274. return ret;
  275. dev_geo(dev, "5: camera scaled to %ux%u\n",
  276. mf_tmp.width, mf_tmp.height);
  277. /* 6. Retrieve camera output window (g_fmt) */
  278. /* unneeded - it is already in "mf_tmp" */
  279. /* 7. Calculate new client scales. */
  280. scale_h = soc_camera_calc_scale(rect->width, shift, mf_tmp.width);
  281. scale_v = soc_camera_calc_scale(rect->height, shift, mf_tmp.height);
  282. mf->width = mf_tmp.width;
  283. mf->height = mf_tmp.height;
  284. mf->colorspace = mf_tmp.colorspace;
  285. /*
  286. * 8. Calculate new host crop - apply camera scales to previously
  287. * updated "effective" crop.
  288. */
  289. *width = soc_camera_shift_scale(subrect->width, shift, scale_h);
  290. *height = soc_camera_shift_scale(subrect->height, shift, scale_v);
  291. dev_geo(dev, "8: new client sub-window %ux%u\n", *width, *height);
  292. return 0;
  293. }
  294. EXPORT_SYMBOL(soc_camera_client_scale);
  295. /*
  296. * Calculate real client output window by applying new scales to the current
  297. * client crop. New scales are calculated from the requested output format and
  298. * host crop, mapped backed onto the client input (subrect).
  299. */
  300. void soc_camera_calc_client_output(struct soc_camera_device *icd,
  301. struct v4l2_rect *rect, struct v4l2_rect *subrect,
  302. const struct v4l2_pix_format *pix, struct v4l2_mbus_framefmt *mf,
  303. unsigned int shift)
  304. {
  305. struct device *dev = icd->parent;
  306. unsigned int scale_v, scale_h;
  307. if (subrect->width == rect->width &&
  308. subrect->height == rect->height) {
  309. /* No sub-cropping */
  310. mf->width = pix->width;
  311. mf->height = pix->height;
  312. return;
  313. }
  314. /* 1.-2. Current camera scales and subwin - cached. */
  315. dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
  316. subrect->width, subrect->height,
  317. subrect->left, subrect->top);
  318. /*
  319. * 3. Calculate new combined scales from input sub-window to requested
  320. * user window.
  321. */
  322. /*
  323. * TODO: CEU cannot scale images larger than VGA to smaller than SubQCIF
  324. * (128x96) or larger than VGA. This and similar limitations have to be
  325. * taken into account here.
  326. */
  327. scale_h = soc_camera_calc_scale(subrect->width, shift, pix->width);
  328. scale_v = soc_camera_calc_scale(subrect->height, shift, pix->height);
  329. dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
  330. /*
  331. * 4. Calculate desired client output window by applying combined scales
  332. * to client (real) input window.
  333. */
  334. mf->width = soc_camera_shift_scale(rect->width, shift, scale_h);
  335. mf->height = soc_camera_shift_scale(rect->height, shift, scale_v);
  336. }
  337. EXPORT_SYMBOL(soc_camera_calc_client_output);