omap_voutlib.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * omap_voutlib.c
  3. *
  4. * Copyright (C) 2005-2010 Texas Instruments.
  5. *
  6. * This file is licensed under the terms of the GNU General Public License
  7. * version 2. This program is licensed "as is" without any warranty of any
  8. * kind, whether express or implied.
  9. *
  10. * Based on the OMAP2 camera driver
  11. * Video-for-Linux (Version 2) camera capture driver for
  12. * the OMAP24xx camera controller.
  13. *
  14. * Author: Andy Lowe (source@mvista.com)
  15. *
  16. * Copyright (C) 2004 MontaVista Software, Inc.
  17. * Copyright (C) 2010 Texas Instruments.
  18. *
  19. */
  20. #include <linux/module.h>
  21. #include <linux/errno.h>
  22. #include <linux/kernel.h>
  23. #include <linux/types.h>
  24. #include <linux/videodev2.h>
  25. #include <plat/cpu.h>
  26. MODULE_AUTHOR("Texas Instruments");
  27. MODULE_DESCRIPTION("OMAP Video library");
  28. MODULE_LICENSE("GPL");
  29. /* Return the default overlay cropping rectangle in crop given the image
  30. * size in pix and the video display size in fbuf. The default
  31. * cropping rectangle is the largest rectangle no larger than the capture size
  32. * that will fit on the display. The default cropping rectangle is centered in
  33. * the image. All dimensions and offsets are rounded down to even numbers.
  34. */
  35. void omap_vout_default_crop(struct v4l2_pix_format *pix,
  36. struct v4l2_framebuffer *fbuf, struct v4l2_rect *crop)
  37. {
  38. crop->width = (pix->width < fbuf->fmt.width) ?
  39. pix->width : fbuf->fmt.width;
  40. crop->height = (pix->height < fbuf->fmt.height) ?
  41. pix->height : fbuf->fmt.height;
  42. crop->width &= ~1;
  43. crop->height &= ~1;
  44. crop->left = ((pix->width - crop->width) >> 1) & ~1;
  45. crop->top = ((pix->height - crop->height) >> 1) & ~1;
  46. }
  47. EXPORT_SYMBOL_GPL(omap_vout_default_crop);
  48. /* Given a new render window in new_win, adjust the window to the
  49. * nearest supported configuration. The adjusted window parameters are
  50. * returned in new_win.
  51. * Returns zero if succesful, or -EINVAL if the requested window is
  52. * impossible and cannot reasonably be adjusted.
  53. */
  54. int omap_vout_try_window(struct v4l2_framebuffer *fbuf,
  55. struct v4l2_window *new_win)
  56. {
  57. struct v4l2_rect try_win;
  58. /* make a working copy of the new_win rectangle */
  59. try_win = new_win->w;
  60. /* adjust the preview window so it fits on the display by clipping any
  61. * offscreen areas
  62. */
  63. if (try_win.left < 0) {
  64. try_win.width += try_win.left;
  65. try_win.left = 0;
  66. }
  67. if (try_win.top < 0) {
  68. try_win.height += try_win.top;
  69. try_win.top = 0;
  70. }
  71. try_win.width = (try_win.width < fbuf->fmt.width) ?
  72. try_win.width : fbuf->fmt.width;
  73. try_win.height = (try_win.height < fbuf->fmt.height) ?
  74. try_win.height : fbuf->fmt.height;
  75. if (try_win.left + try_win.width > fbuf->fmt.width)
  76. try_win.width = fbuf->fmt.width - try_win.left;
  77. if (try_win.top + try_win.height > fbuf->fmt.height)
  78. try_win.height = fbuf->fmt.height - try_win.top;
  79. try_win.width &= ~1;
  80. try_win.height &= ~1;
  81. if (try_win.width <= 0 || try_win.height <= 0)
  82. return -EINVAL;
  83. /* We now have a valid preview window, so go with it */
  84. new_win->w = try_win;
  85. new_win->field = V4L2_FIELD_ANY;
  86. return 0;
  87. }
  88. EXPORT_SYMBOL_GPL(omap_vout_try_window);
  89. /* Given a new render window in new_win, adjust the window to the
  90. * nearest supported configuration. The image cropping window in crop
  91. * will also be adjusted if necessary. Preference is given to keeping the
  92. * the window as close to the requested configuration as possible. If
  93. * successful, new_win, vout->win, and crop are updated.
  94. * Returns zero if succesful, or -EINVAL if the requested preview window is
  95. * impossible and cannot reasonably be adjusted.
  96. */
  97. int omap_vout_new_window(struct v4l2_rect *crop,
  98. struct v4l2_window *win, struct v4l2_framebuffer *fbuf,
  99. struct v4l2_window *new_win)
  100. {
  101. int err;
  102. err = omap_vout_try_window(fbuf, new_win);
  103. if (err)
  104. return err;
  105. /* update our preview window */
  106. win->w = new_win->w;
  107. win->field = new_win->field;
  108. win->chromakey = new_win->chromakey;
  109. /* Adjust the cropping window to allow for resizing limitation */
  110. if (cpu_is_omap24xx()) {
  111. /* For 24xx limit is 8x to 1/2x scaling. */
  112. if ((crop->height/win->w.height) >= 2)
  113. crop->height = win->w.height * 2;
  114. if ((crop->width/win->w.width) >= 2)
  115. crop->width = win->w.width * 2;
  116. if (crop->width > 768) {
  117. /* The OMAP2420 vertical resizing line buffer is 768
  118. * pixels wide. If the cropped image is wider than
  119. * 768 pixels then it cannot be vertically resized.
  120. */
  121. if (crop->height != win->w.height)
  122. crop->width = 768;
  123. }
  124. } else if (cpu_is_omap34xx()) {
  125. /* For 34xx limit is 8x to 1/4x scaling. */
  126. if ((crop->height/win->w.height) >= 4)
  127. crop->height = win->w.height * 4;
  128. if ((crop->width/win->w.width) >= 4)
  129. crop->width = win->w.width * 4;
  130. }
  131. return 0;
  132. }
  133. EXPORT_SYMBOL_GPL(omap_vout_new_window);
  134. /* Given a new cropping rectangle in new_crop, adjust the cropping rectangle to
  135. * the nearest supported configuration. The image render window in win will
  136. * also be adjusted if necessary. The preview window is adjusted such that the
  137. * horizontal and vertical rescaling ratios stay constant. If the render
  138. * window would fall outside the display boundaries, the cropping rectangle
  139. * will also be adjusted to maintain the rescaling ratios. If successful, crop
  140. * and win are updated.
  141. * Returns zero if succesful, or -EINVAL if the requested cropping rectangle is
  142. * impossible and cannot reasonably be adjusted.
  143. */
  144. int omap_vout_new_crop(struct v4l2_pix_format *pix,
  145. struct v4l2_rect *crop, struct v4l2_window *win,
  146. struct v4l2_framebuffer *fbuf, const struct v4l2_rect *new_crop)
  147. {
  148. struct v4l2_rect try_crop;
  149. unsigned long vresize, hresize;
  150. /* make a working copy of the new_crop rectangle */
  151. try_crop = *new_crop;
  152. /* adjust the cropping rectangle so it fits in the image */
  153. if (try_crop.left < 0) {
  154. try_crop.width += try_crop.left;
  155. try_crop.left = 0;
  156. }
  157. if (try_crop.top < 0) {
  158. try_crop.height += try_crop.top;
  159. try_crop.top = 0;
  160. }
  161. try_crop.width = (try_crop.width < pix->width) ?
  162. try_crop.width : pix->width;
  163. try_crop.height = (try_crop.height < pix->height) ?
  164. try_crop.height : pix->height;
  165. if (try_crop.left + try_crop.width > pix->width)
  166. try_crop.width = pix->width - try_crop.left;
  167. if (try_crop.top + try_crop.height > pix->height)
  168. try_crop.height = pix->height - try_crop.top;
  169. try_crop.width &= ~1;
  170. try_crop.height &= ~1;
  171. if (try_crop.width <= 0 || try_crop.height <= 0)
  172. return -EINVAL;
  173. if (cpu_is_omap24xx()) {
  174. if (crop->height != win->w.height) {
  175. /* If we're resizing vertically, we can't support a
  176. * crop width wider than 768 pixels.
  177. */
  178. if (try_crop.width > 768)
  179. try_crop.width = 768;
  180. }
  181. }
  182. /* vertical resizing */
  183. vresize = (1024 * crop->height) / win->w.height;
  184. if (cpu_is_omap24xx() && (vresize > 2048))
  185. vresize = 2048;
  186. else if (cpu_is_omap34xx() && (vresize > 4096))
  187. vresize = 4096;
  188. win->w.height = ((1024 * try_crop.height) / vresize) & ~1;
  189. if (win->w.height == 0)
  190. win->w.height = 2;
  191. if (win->w.height + win->w.top > fbuf->fmt.height) {
  192. /* We made the preview window extend below the bottom of the
  193. * display, so clip it to the display boundary and resize the
  194. * cropping height to maintain the vertical resizing ratio.
  195. */
  196. win->w.height = (fbuf->fmt.height - win->w.top) & ~1;
  197. if (try_crop.height == 0)
  198. try_crop.height = 2;
  199. }
  200. /* horizontal resizing */
  201. hresize = (1024 * crop->width) / win->w.width;
  202. if (cpu_is_omap24xx() && (hresize > 2048))
  203. hresize = 2048;
  204. else if (cpu_is_omap34xx() && (hresize > 4096))
  205. hresize = 4096;
  206. win->w.width = ((1024 * try_crop.width) / hresize) & ~1;
  207. if (win->w.width == 0)
  208. win->w.width = 2;
  209. if (win->w.width + win->w.left > fbuf->fmt.width) {
  210. /* We made the preview window extend past the right side of the
  211. * display, so clip it to the display boundary and resize the
  212. * cropping width to maintain the horizontal resizing ratio.
  213. */
  214. win->w.width = (fbuf->fmt.width - win->w.left) & ~1;
  215. if (try_crop.width == 0)
  216. try_crop.width = 2;
  217. }
  218. if (cpu_is_omap24xx()) {
  219. if ((try_crop.height/win->w.height) >= 2)
  220. try_crop.height = win->w.height * 2;
  221. if ((try_crop.width/win->w.width) >= 2)
  222. try_crop.width = win->w.width * 2;
  223. if (try_crop.width > 768) {
  224. /* The OMAP2420 vertical resizing line buffer is
  225. * 768 pixels wide. If the cropped image is wider
  226. * than 768 pixels then it cannot be vertically resized.
  227. */
  228. if (try_crop.height != win->w.height)
  229. try_crop.width = 768;
  230. }
  231. } else if (cpu_is_omap34xx()) {
  232. if ((try_crop.height/win->w.height) >= 4)
  233. try_crop.height = win->w.height * 4;
  234. if ((try_crop.width/win->w.width) >= 4)
  235. try_crop.width = win->w.width * 4;
  236. }
  237. /* update our cropping rectangle and we're done */
  238. *crop = try_crop;
  239. return 0;
  240. }
  241. EXPORT_SYMBOL_GPL(omap_vout_new_crop);
  242. /* Given a new format in pix and fbuf, crop and win
  243. * structures are initialized to default values. crop
  244. * is initialized to the largest window size that will fit on the display. The
  245. * crop window is centered in the image. win is initialized to
  246. * the same size as crop and is centered on the display.
  247. * All sizes and offsets are constrained to be even numbers.
  248. */
  249. void omap_vout_new_format(struct v4l2_pix_format *pix,
  250. struct v4l2_framebuffer *fbuf, struct v4l2_rect *crop,
  251. struct v4l2_window *win)
  252. {
  253. /* crop defines the preview source window in the image capture
  254. * buffer
  255. */
  256. omap_vout_default_crop(pix, fbuf, crop);
  257. /* win defines the preview target window on the display */
  258. win->w.width = crop->width;
  259. win->w.height = crop->height;
  260. win->w.left = ((fbuf->fmt.width - win->w.width) >> 1) & ~1;
  261. win->w.top = ((fbuf->fmt.height - win->w.height) >> 1) & ~1;
  262. }
  263. EXPORT_SYMBOL_GPL(omap_vout_new_format);