adf_format.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (C) 2013 Google, Inc.
  3. * modified from drivers/gpu/drm/drm_crtc.c
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/export.h>
  16. #include <linux/kernel.h>
  17. #include <drm/drm_fourcc.h>
  18. #include <video/adf_format.h>
  19. bool adf_format_is_standard(u32 format)
  20. {
  21. switch (format) {
  22. case DRM_FORMAT_C8:
  23. case DRM_FORMAT_RGB332:
  24. case DRM_FORMAT_BGR233:
  25. case DRM_FORMAT_XRGB4444:
  26. case DRM_FORMAT_XBGR4444:
  27. case DRM_FORMAT_RGBX4444:
  28. case DRM_FORMAT_BGRX4444:
  29. case DRM_FORMAT_ARGB4444:
  30. case DRM_FORMAT_ABGR4444:
  31. case DRM_FORMAT_RGBA4444:
  32. case DRM_FORMAT_BGRA4444:
  33. case DRM_FORMAT_XRGB1555:
  34. case DRM_FORMAT_XBGR1555:
  35. case DRM_FORMAT_RGBX5551:
  36. case DRM_FORMAT_BGRX5551:
  37. case DRM_FORMAT_ARGB1555:
  38. case DRM_FORMAT_ABGR1555:
  39. case DRM_FORMAT_RGBA5551:
  40. case DRM_FORMAT_BGRA5551:
  41. case DRM_FORMAT_RGB565:
  42. case DRM_FORMAT_BGR565:
  43. case DRM_FORMAT_RGB888:
  44. case DRM_FORMAT_BGR888:
  45. case DRM_FORMAT_XRGB8888:
  46. case DRM_FORMAT_XBGR8888:
  47. case DRM_FORMAT_RGBX8888:
  48. case DRM_FORMAT_BGRX8888:
  49. case DRM_FORMAT_ARGB8888:
  50. case DRM_FORMAT_ABGR8888:
  51. case DRM_FORMAT_RGBA8888:
  52. case DRM_FORMAT_BGRA8888:
  53. case DRM_FORMAT_XRGB2101010:
  54. case DRM_FORMAT_XBGR2101010:
  55. case DRM_FORMAT_RGBX1010102:
  56. case DRM_FORMAT_BGRX1010102:
  57. case DRM_FORMAT_ARGB2101010:
  58. case DRM_FORMAT_ABGR2101010:
  59. case DRM_FORMAT_RGBA1010102:
  60. case DRM_FORMAT_BGRA1010102:
  61. case DRM_FORMAT_YUYV:
  62. case DRM_FORMAT_YVYU:
  63. case DRM_FORMAT_UYVY:
  64. case DRM_FORMAT_VYUY:
  65. case DRM_FORMAT_AYUV:
  66. case DRM_FORMAT_NV12:
  67. case DRM_FORMAT_NV21:
  68. case DRM_FORMAT_NV16:
  69. case DRM_FORMAT_NV61:
  70. case DRM_FORMAT_YUV410:
  71. case DRM_FORMAT_YVU410:
  72. case DRM_FORMAT_YUV411:
  73. case DRM_FORMAT_YVU411:
  74. case DRM_FORMAT_YUV420:
  75. case DRM_FORMAT_YVU420:
  76. case DRM_FORMAT_YUV422:
  77. case DRM_FORMAT_YVU422:
  78. case DRM_FORMAT_YUV444:
  79. case DRM_FORMAT_YVU444:
  80. return true;
  81. default:
  82. return false;
  83. }
  84. }
  85. EXPORT_SYMBOL(adf_format_is_standard);
  86. bool adf_format_is_rgb(u32 format)
  87. {
  88. switch (format) {
  89. case DRM_FORMAT_C8:
  90. case DRM_FORMAT_RGB332:
  91. case DRM_FORMAT_BGR233:
  92. case DRM_FORMAT_XRGB1555:
  93. case DRM_FORMAT_XBGR1555:
  94. case DRM_FORMAT_RGBX5551:
  95. case DRM_FORMAT_BGRX5551:
  96. case DRM_FORMAT_ARGB1555:
  97. case DRM_FORMAT_ABGR1555:
  98. case DRM_FORMAT_RGBA5551:
  99. case DRM_FORMAT_BGRA5551:
  100. case DRM_FORMAT_RGB565:
  101. case DRM_FORMAT_BGR565:
  102. case DRM_FORMAT_RGB888:
  103. case DRM_FORMAT_BGR888:
  104. case DRM_FORMAT_XRGB8888:
  105. case DRM_FORMAT_XBGR8888:
  106. case DRM_FORMAT_RGBX8888:
  107. case DRM_FORMAT_BGRX8888:
  108. case DRM_FORMAT_XRGB2101010:
  109. case DRM_FORMAT_XBGR2101010:
  110. case DRM_FORMAT_RGBX1010102:
  111. case DRM_FORMAT_BGRX1010102:
  112. case DRM_FORMAT_ARGB2101010:
  113. case DRM_FORMAT_ABGR2101010:
  114. case DRM_FORMAT_RGBA1010102:
  115. case DRM_FORMAT_BGRA1010102:
  116. case DRM_FORMAT_ARGB8888:
  117. case DRM_FORMAT_ABGR8888:
  118. case DRM_FORMAT_RGBA8888:
  119. case DRM_FORMAT_BGRA8888:
  120. return true;
  121. default:
  122. return false;
  123. }
  124. }
  125. EXPORT_SYMBOL(adf_format_is_rgb);
  126. u8 adf_format_num_planes(u32 format)
  127. {
  128. switch (format) {
  129. case DRM_FORMAT_YUV410:
  130. case DRM_FORMAT_YVU410:
  131. case DRM_FORMAT_YUV411:
  132. case DRM_FORMAT_YVU411:
  133. case DRM_FORMAT_YUV420:
  134. case DRM_FORMAT_YVU420:
  135. case DRM_FORMAT_YUV422:
  136. case DRM_FORMAT_YVU422:
  137. case DRM_FORMAT_YUV444:
  138. case DRM_FORMAT_YVU444:
  139. return 3;
  140. case DRM_FORMAT_NV12:
  141. case DRM_FORMAT_NV21:
  142. case DRM_FORMAT_NV16:
  143. case DRM_FORMAT_NV61:
  144. return 2;
  145. default:
  146. return 1;
  147. }
  148. }
  149. EXPORT_SYMBOL(adf_format_num_planes);
  150. u8 adf_format_bpp(u32 format)
  151. {
  152. switch (format) {
  153. case DRM_FORMAT_C8:
  154. case DRM_FORMAT_RGB332:
  155. case DRM_FORMAT_BGR233:
  156. return 8;
  157. case DRM_FORMAT_XRGB1555:
  158. case DRM_FORMAT_XBGR1555:
  159. case DRM_FORMAT_RGBX5551:
  160. case DRM_FORMAT_BGRX5551:
  161. case DRM_FORMAT_ARGB1555:
  162. case DRM_FORMAT_ABGR1555:
  163. case DRM_FORMAT_RGBA5551:
  164. case DRM_FORMAT_BGRA5551:
  165. case DRM_FORMAT_RGB565:
  166. case DRM_FORMAT_BGR565:
  167. return 16;
  168. case DRM_FORMAT_RGB888:
  169. case DRM_FORMAT_BGR888:
  170. return 24;
  171. case DRM_FORMAT_XRGB8888:
  172. case DRM_FORMAT_XBGR8888:
  173. case DRM_FORMAT_RGBX8888:
  174. case DRM_FORMAT_BGRX8888:
  175. case DRM_FORMAT_XRGB2101010:
  176. case DRM_FORMAT_XBGR2101010:
  177. case DRM_FORMAT_RGBX1010102:
  178. case DRM_FORMAT_BGRX1010102:
  179. case DRM_FORMAT_ARGB2101010:
  180. case DRM_FORMAT_ABGR2101010:
  181. case DRM_FORMAT_RGBA1010102:
  182. case DRM_FORMAT_BGRA1010102:
  183. case DRM_FORMAT_ARGB8888:
  184. case DRM_FORMAT_ABGR8888:
  185. case DRM_FORMAT_RGBA8888:
  186. case DRM_FORMAT_BGRA8888:
  187. return 32;
  188. default:
  189. pr_debug("%s: unsupported pixel format %u\n", __func__, format);
  190. return 0;
  191. }
  192. }
  193. EXPORT_SYMBOL(adf_format_bpp);
  194. u8 adf_format_plane_cpp(u32 format, int plane)
  195. {
  196. if (plane >= adf_format_num_planes(format))
  197. return 0;
  198. switch (format) {
  199. case DRM_FORMAT_YUYV:
  200. case DRM_FORMAT_YVYU:
  201. case DRM_FORMAT_UYVY:
  202. case DRM_FORMAT_VYUY:
  203. return 2;
  204. case DRM_FORMAT_NV12:
  205. case DRM_FORMAT_NV21:
  206. case DRM_FORMAT_NV16:
  207. case DRM_FORMAT_NV61:
  208. return plane ? 2 : 1;
  209. case DRM_FORMAT_YUV410:
  210. case DRM_FORMAT_YVU410:
  211. case DRM_FORMAT_YUV411:
  212. case DRM_FORMAT_YVU411:
  213. case DRM_FORMAT_YUV420:
  214. case DRM_FORMAT_YVU420:
  215. case DRM_FORMAT_YUV422:
  216. case DRM_FORMAT_YVU422:
  217. case DRM_FORMAT_YUV444:
  218. case DRM_FORMAT_YVU444:
  219. return 1;
  220. default:
  221. return adf_format_bpp(format) / 8;
  222. }
  223. }
  224. EXPORT_SYMBOL(adf_format_plane_cpp);
  225. u8 adf_format_horz_chroma_subsampling(u32 format)
  226. {
  227. switch (format) {
  228. case DRM_FORMAT_YUV411:
  229. case DRM_FORMAT_YVU411:
  230. case DRM_FORMAT_YUV410:
  231. case DRM_FORMAT_YVU410:
  232. return 4;
  233. case DRM_FORMAT_YUYV:
  234. case DRM_FORMAT_YVYU:
  235. case DRM_FORMAT_UYVY:
  236. case DRM_FORMAT_VYUY:
  237. case DRM_FORMAT_NV12:
  238. case DRM_FORMAT_NV21:
  239. case DRM_FORMAT_NV16:
  240. case DRM_FORMAT_NV61:
  241. case DRM_FORMAT_YUV422:
  242. case DRM_FORMAT_YVU422:
  243. case DRM_FORMAT_YUV420:
  244. case DRM_FORMAT_YVU420:
  245. return 2;
  246. default:
  247. return 1;
  248. }
  249. }
  250. EXPORT_SYMBOL(adf_format_horz_chroma_subsampling);
  251. u8 adf_format_vert_chroma_subsampling(u32 format)
  252. {
  253. switch (format) {
  254. case DRM_FORMAT_YUV410:
  255. case DRM_FORMAT_YVU410:
  256. return 4;
  257. case DRM_FORMAT_YUV420:
  258. case DRM_FORMAT_YVU420:
  259. case DRM_FORMAT_NV12:
  260. case DRM_FORMAT_NV21:
  261. return 2;
  262. default:
  263. return 1;
  264. }
  265. }
  266. EXPORT_SYMBOL(adf_format_vert_chroma_subsampling);