msm_media_info.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  2. /*
  3. * Copyright (c) 2020-2021,, The Linux Foundation. All rights reserved.
  4. */
  5. #ifndef __MSM_MEDIA_INFO_H__
  6. #define __MSM_MEDIA_INFO_H__
  7. #include "msm_vidc_internal.h"
  8. /* Width and Height should be multiple of 16 */
  9. #define INTERLACE_WIDTH_MAX 1920
  10. #define INTERLACE_HEIGHT_MAX 1920
  11. #define INTERLACE_MB_PER_FRAME_MAX ((1920*1088)/256)
  12. #ifndef MSM_MEDIA_ALIGN
  13. #define MSM_MEDIA_ALIGN(__sz, __align) (((__align) & ((__align) - 1)) ?\
  14. ((((__sz) + (__align) - 1) / (__align)) * (__align)) :\
  15. (((__sz) + (__align) - 1) & (~((__align) - 1))))
  16. #endif
  17. #ifndef MSM_MEDIA_ROUNDUP
  18. #define MSM_MEDIA_ROUNDUP(__sz, __r) (((__sz) + ((__r) - 1)) / (__r))
  19. #endif
  20. /*
  21. * Function arguments:
  22. * @v4l2_fmt
  23. * @width
  24. * Progressive: width
  25. * Interlaced: width
  26. */
  27. static inline unsigned int video_y_stride_bytes(unsigned int colorformat,
  28. unsigned int width)
  29. {
  30. unsigned int alignment, stride = 0;
  31. if (!width)
  32. goto invalid_input;
  33. switch (colorformat) {
  34. case MSM_VIDC_FMT_NV12:
  35. case MSM_VIDC_FMT_NV21:
  36. case MSM_VIDC_FMT_NV12C:
  37. alignment = 128;
  38. stride = MSM_MEDIA_ALIGN(width, alignment);
  39. break;
  40. case MSM_VIDC_FMT_TP10C:
  41. alignment = 256;
  42. stride = MSM_MEDIA_ALIGN(width, 192);
  43. stride = MSM_MEDIA_ALIGN(stride * 4/3, alignment);
  44. break;
  45. case MSM_VIDC_FMT_P010:
  46. alignment = 256;
  47. stride = MSM_MEDIA_ALIGN(width * 2, alignment);
  48. break;
  49. default:
  50. break;
  51. }
  52. invalid_input:
  53. return stride;
  54. }
  55. /*
  56. * Function arguments:
  57. * @v4l2_fmt
  58. * @width
  59. * Progressive: width
  60. * Interlaced: width
  61. */
  62. static inline unsigned int video_y_stride_pix(unsigned int colorformat,
  63. unsigned int width)
  64. {
  65. unsigned int alignment, stride = 0;
  66. if (!width)
  67. goto invalid_input;
  68. switch (colorformat) {
  69. case MSM_VIDC_FMT_NV12:
  70. case MSM_VIDC_FMT_NV21:
  71. case MSM_VIDC_FMT_NV12C:
  72. case MSM_VIDC_FMT_P010:
  73. alignment = 128;
  74. stride = MSM_MEDIA_ALIGN(width, alignment);
  75. break;
  76. case MSM_VIDC_FMT_TP10C:
  77. alignment = 192;
  78. stride = MSM_MEDIA_ALIGN(width, alignment);
  79. break;
  80. default:
  81. break;
  82. }
  83. invalid_input:
  84. return stride;
  85. }
  86. /*
  87. * Function arguments:
  88. * @v4l2_fmt
  89. * @width
  90. * Progressive: width
  91. * Interlaced: width
  92. */
  93. static inline unsigned int video_uv_stride_bytes(unsigned int colorformat,
  94. unsigned int width)
  95. {
  96. unsigned int alignment, stride = 0;
  97. if (!width)
  98. goto invalid_input;
  99. switch (colorformat) {
  100. case MSM_VIDC_FMT_NV21:
  101. case MSM_VIDC_FMT_NV12:
  102. case MSM_VIDC_FMT_NV12C:
  103. alignment = 128;
  104. stride = MSM_MEDIA_ALIGN(width, alignment);
  105. break;
  106. case MSM_VIDC_FMT_TP10C:
  107. alignment = 256;
  108. stride = MSM_MEDIA_ALIGN(width, 192);
  109. stride = MSM_MEDIA_ALIGN(stride * 4/3, alignment);
  110. break;
  111. case MSM_VIDC_FMT_P010:
  112. alignment = 256;
  113. stride = MSM_MEDIA_ALIGN(width * 2, alignment);
  114. break;
  115. default:
  116. break;
  117. }
  118. invalid_input:
  119. return stride;
  120. }
  121. /*
  122. * Function arguments:
  123. * @v4l2_fmt
  124. * @width
  125. * Progressive: width
  126. * Interlaced: width
  127. */
  128. static inline unsigned int video_uv_stride_pix(unsigned int colorformat,
  129. unsigned int width)
  130. {
  131. unsigned int alignment, stride = 0;
  132. if (!width)
  133. goto invalid_input;
  134. switch (colorformat) {
  135. case MSM_VIDC_FMT_NV21:
  136. case MSM_VIDC_FMT_NV12:
  137. case MSM_VIDC_FMT_NV12C:
  138. case MSM_VIDC_FMT_P010:
  139. alignment = 128;
  140. stride = MSM_MEDIA_ALIGN(width, alignment);
  141. break;
  142. case MSM_VIDC_FMT_TP10C:
  143. alignment = 192;
  144. stride = MSM_MEDIA_ALIGN(width, alignment);
  145. break;
  146. default:
  147. break;
  148. }
  149. invalid_input:
  150. return stride;
  151. }
  152. /*
  153. * Function arguments:
  154. * @v4l2_fmt
  155. * @height
  156. * Progressive: height
  157. * Interlaced: (height+1)>>1
  158. */
  159. static inline unsigned int video_y_scanlines(unsigned int colorformat,
  160. unsigned int height)
  161. {
  162. unsigned int alignment, sclines = 0;
  163. if (!height)
  164. goto invalid_input;
  165. switch (colorformat) {
  166. case MSM_VIDC_FMT_NV12:
  167. case MSM_VIDC_FMT_NV21:
  168. case MSM_VIDC_FMT_NV12C:
  169. case MSM_VIDC_FMT_P010:
  170. alignment = 32;
  171. break;
  172. case MSM_VIDC_FMT_TP10C:
  173. alignment = 16;
  174. break;
  175. default:
  176. return 0;
  177. }
  178. sclines = MSM_MEDIA_ALIGN(height, alignment);
  179. invalid_input:
  180. return sclines;
  181. }
  182. /*
  183. * Function arguments:
  184. * @v4l2_fmt
  185. * @height
  186. * Progressive: height
  187. * Interlaced: (height+1)>>1
  188. */
  189. static inline unsigned int video_uv_scanlines(unsigned int colorformat,
  190. unsigned int height)
  191. {
  192. unsigned int alignment, sclines = 0;
  193. if (!height)
  194. goto invalid_input;
  195. switch (colorformat) {
  196. case MSM_VIDC_FMT_NV21:
  197. case MSM_VIDC_FMT_NV12:
  198. case MSM_VIDC_FMT_TP10C:
  199. case MSM_VIDC_FMT_P010:
  200. alignment = 16;
  201. break;
  202. case MSM_VIDC_FMT_NV12C:
  203. alignment = 32;
  204. break;
  205. default:
  206. goto invalid_input;
  207. }
  208. sclines = MSM_MEDIA_ALIGN((height+1)>>1, alignment);
  209. invalid_input:
  210. return sclines;
  211. }
  212. /*
  213. * Function arguments:
  214. * @v4l2_fmt
  215. * @width
  216. * Progressive: width
  217. * Interlaced: width
  218. */
  219. static inline unsigned int video_y_meta_stride(unsigned int colorformat,
  220. unsigned int width)
  221. {
  222. int y_tile_width = 0, y_meta_stride = 0;
  223. if (!width)
  224. goto invalid_input;
  225. switch (colorformat) {
  226. case MSM_VIDC_FMT_NV12C:
  227. y_tile_width = 32;
  228. break;
  229. case MSM_VIDC_FMT_TP10C:
  230. y_tile_width = 48;
  231. break;
  232. default:
  233. goto invalid_input;
  234. }
  235. y_meta_stride = MSM_MEDIA_ROUNDUP(width, y_tile_width);
  236. y_meta_stride = MSM_MEDIA_ALIGN(y_meta_stride, 64);
  237. invalid_input:
  238. return y_meta_stride;
  239. }
  240. /*
  241. * Function arguments:
  242. * @v4l2_fmt
  243. * @height
  244. * Progressive: height
  245. * Interlaced: (height+1)>>1
  246. */
  247. static inline unsigned int video_y_meta_scanlines(unsigned int colorformat,
  248. unsigned int height)
  249. {
  250. int y_tile_height = 0, y_meta_scanlines = 0;
  251. if (!height)
  252. goto invalid_input;
  253. switch (colorformat) {
  254. case MSM_VIDC_FMT_NV12C:
  255. y_tile_height = 8;
  256. break;
  257. case MSM_VIDC_FMT_TP10C:
  258. y_tile_height = 4;
  259. break;
  260. default:
  261. goto invalid_input;
  262. }
  263. y_meta_scanlines = MSM_MEDIA_ROUNDUP(height, y_tile_height);
  264. y_meta_scanlines = MSM_MEDIA_ALIGN(y_meta_scanlines, 16);
  265. invalid_input:
  266. return y_meta_scanlines;
  267. }
  268. /*
  269. * Function arguments:
  270. * @v4l2_fmt
  271. * @width
  272. * Progressive: width
  273. * Interlaced: width
  274. */
  275. static inline unsigned int video_uv_meta_stride(unsigned int colorformat,
  276. unsigned int width)
  277. {
  278. int uv_tile_width = 0, uv_meta_stride = 0;
  279. if (!width)
  280. goto invalid_input;
  281. switch (colorformat) {
  282. case MSM_VIDC_FMT_NV12C:
  283. uv_tile_width = 16;
  284. break;
  285. case MSM_VIDC_FMT_TP10C:
  286. uv_tile_width = 24;
  287. break;
  288. default:
  289. goto invalid_input;
  290. }
  291. uv_meta_stride = MSM_MEDIA_ROUNDUP((width+1)>>1, uv_tile_width);
  292. uv_meta_stride = MSM_MEDIA_ALIGN(uv_meta_stride, 64);
  293. invalid_input:
  294. return uv_meta_stride;
  295. }
  296. /*
  297. * Function arguments:
  298. * @v4l2_fmt
  299. * @height
  300. * Progressive: height
  301. * Interlaced: (height+1)>>1
  302. */
  303. static inline unsigned int video_uv_meta_scanlines(
  304. unsigned int colorformat, unsigned int height)
  305. {
  306. int uv_tile_height = 0, uv_meta_scanlines = 0;
  307. if (!height)
  308. goto invalid_input;
  309. switch (colorformat) {
  310. case MSM_VIDC_FMT_NV12C:
  311. uv_tile_height = 8;
  312. break;
  313. case MSM_VIDC_FMT_TP10C:
  314. uv_tile_height = 4;
  315. break;
  316. default:
  317. goto invalid_input;
  318. }
  319. uv_meta_scanlines = MSM_MEDIA_ROUNDUP((height+1)>>1, uv_tile_height);
  320. uv_meta_scanlines = MSM_MEDIA_ALIGN(uv_meta_scanlines, 16);
  321. invalid_input:
  322. return uv_meta_scanlines;
  323. }
  324. static inline unsigned int video_rgb_stride_bytes(
  325. unsigned int colorformat, unsigned int width)
  326. {
  327. unsigned int alignment = 0, stride = 0, bpp = 4;
  328. if (!width)
  329. goto invalid_input;
  330. switch (colorformat) {
  331. case MSM_VIDC_FMT_RGBA8888C:
  332. case MSM_VIDC_FMT_RGBA8888:
  333. alignment = 256;
  334. break;
  335. default:
  336. goto invalid_input;
  337. }
  338. stride = MSM_MEDIA_ALIGN(width * bpp, alignment);
  339. invalid_input:
  340. return stride;
  341. }
  342. static inline unsigned int video_rgb_stride_pix(unsigned int colorformat,
  343. unsigned int width)
  344. {
  345. unsigned int bpp = 4;
  346. return video_rgb_stride_bytes(colorformat, width) / bpp;
  347. }
  348. static inline unsigned int video_rgb_scanlines(unsigned int colorformat,
  349. unsigned int height)
  350. {
  351. unsigned int alignment = 0, scanlines = 0;
  352. if (!height)
  353. goto invalid_input;
  354. switch (colorformat) {
  355. case MSM_VIDC_FMT_RGBA8888C:
  356. alignment = 16;
  357. break;
  358. case MSM_VIDC_FMT_RGBA8888:
  359. alignment = 32;
  360. break;
  361. default:
  362. goto invalid_input;
  363. }
  364. scanlines = MSM_MEDIA_ALIGN(height, alignment);
  365. invalid_input:
  366. return scanlines;
  367. }
  368. static inline unsigned int video_rgb_meta_stride(
  369. unsigned int colorformat, unsigned int width)
  370. {
  371. int rgb_tile_width = 0, rgb_meta_stride = 0;
  372. if (!width)
  373. goto invalid_input;
  374. switch (colorformat) {
  375. case MSM_VIDC_FMT_RGBA8888C:
  376. case MSM_VIDC_FMT_RGBA8888:
  377. rgb_tile_width = 16;
  378. break;
  379. default:
  380. goto invalid_input;
  381. }
  382. rgb_meta_stride = MSM_MEDIA_ROUNDUP(width, rgb_tile_width);
  383. rgb_meta_stride = MSM_MEDIA_ALIGN(rgb_meta_stride, 64);
  384. invalid_input:
  385. return rgb_meta_stride;
  386. }
  387. static inline unsigned int video_rgb_meta_scanlines(
  388. unsigned int colorformat, unsigned int height)
  389. {
  390. int rgb_tile_height = 0, rgb_meta_scanlines = 0;
  391. if (!height)
  392. goto invalid_input;
  393. switch (colorformat) {
  394. case MSM_VIDC_FMT_RGBA8888C:
  395. case MSM_VIDC_FMT_RGBA8888:
  396. rgb_tile_height = 4;
  397. break;
  398. default:
  399. goto invalid_input;
  400. }
  401. rgb_meta_scanlines = MSM_MEDIA_ROUNDUP(height, rgb_tile_height);
  402. rgb_meta_scanlines = MSM_MEDIA_ALIGN(rgb_meta_scanlines, 16);
  403. invalid_input:
  404. return rgb_meta_scanlines;
  405. }
  406. static inline unsigned int video_buffer_size(unsigned int colorformat,
  407. unsigned int pix_width, unsigned int pix_height, unsigned int interlace)
  408. {
  409. unsigned int size = 0;
  410. unsigned int y_plane, uv_plane, y_stride,
  411. uv_stride, y_sclines, uv_sclines;
  412. unsigned int y_ubwc_plane = 0, uv_ubwc_plane = 0;
  413. unsigned int y_meta_stride = 0, y_meta_scanlines = 0;
  414. unsigned int uv_meta_stride = 0, uv_meta_scanlines = 0;
  415. unsigned int y_meta_plane = 0, uv_meta_plane = 0;
  416. unsigned int rgb_stride = 0, rgb_scanlines = 0;
  417. unsigned int rgb_plane = 0, rgb_ubwc_plane = 0, rgb_meta_plane = 0;
  418. unsigned int rgb_meta_stride = 0, rgb_meta_scanlines = 0;
  419. if (!pix_width || !pix_height)
  420. goto invalid_input;
  421. y_stride = video_y_stride_bytes(colorformat, pix_width);
  422. uv_stride = video_uv_stride_bytes(colorformat, pix_width);
  423. y_sclines = video_y_scanlines(colorformat, pix_height);
  424. uv_sclines = video_uv_scanlines(colorformat, pix_height);
  425. rgb_stride = video_rgb_stride_bytes(colorformat, pix_width);
  426. rgb_scanlines = video_rgb_scanlines(colorformat, pix_height);
  427. switch (colorformat) {
  428. case MSM_VIDC_FMT_NV21:
  429. case MSM_VIDC_FMT_NV12:
  430. case MSM_VIDC_FMT_P010:
  431. y_plane = y_stride * y_sclines;
  432. uv_plane = uv_stride * uv_sclines;
  433. size = y_plane + uv_plane;
  434. break;
  435. case MSM_VIDC_FMT_NV12C:
  436. y_meta_stride = video_y_meta_stride(colorformat, pix_width);
  437. uv_meta_stride = video_uv_meta_stride(colorformat, pix_width);
  438. if (!interlace && colorformat == MSM_VIDC_FMT_NV12C) {
  439. y_ubwc_plane = MSM_MEDIA_ALIGN(y_stride * y_sclines, 4096);
  440. uv_ubwc_plane = MSM_MEDIA_ALIGN(uv_stride * uv_sclines, 4096);
  441. y_meta_scanlines =
  442. video_y_meta_scanlines(colorformat, pix_height);
  443. y_meta_plane = MSM_MEDIA_ALIGN(
  444. y_meta_stride * y_meta_scanlines, 4096);
  445. uv_meta_scanlines =
  446. video_uv_meta_scanlines(colorformat, pix_height);
  447. uv_meta_plane = MSM_MEDIA_ALIGN(uv_meta_stride *
  448. uv_meta_scanlines, 4096);
  449. size = (y_ubwc_plane + uv_ubwc_plane + y_meta_plane +
  450. uv_meta_plane);
  451. } else {
  452. if (pix_width <= INTERLACE_WIDTH_MAX &&
  453. pix_height <= INTERLACE_HEIGHT_MAX &&
  454. (pix_height * pix_width) / 256 <= INTERLACE_MB_PER_FRAME_MAX) {
  455. y_sclines =
  456. video_y_scanlines(colorformat, (pix_height+1)>>1);
  457. y_ubwc_plane =
  458. MSM_MEDIA_ALIGN(y_stride * y_sclines, 4096);
  459. uv_sclines =
  460. video_uv_scanlines(colorformat, (pix_height+1)>>1);
  461. uv_ubwc_plane =
  462. MSM_MEDIA_ALIGN(uv_stride * uv_sclines, 4096);
  463. y_meta_scanlines =
  464. video_y_meta_scanlines(colorformat, (pix_height+1)>>1);
  465. y_meta_plane = MSM_MEDIA_ALIGN(
  466. y_meta_stride * y_meta_scanlines, 4096);
  467. uv_meta_scanlines =
  468. video_uv_meta_scanlines(colorformat, (pix_height+1)>>1);
  469. uv_meta_plane = MSM_MEDIA_ALIGN(uv_meta_stride *
  470. uv_meta_scanlines, 4096);
  471. size = (y_ubwc_plane + uv_ubwc_plane + y_meta_plane +
  472. uv_meta_plane)*2;
  473. } else {
  474. y_sclines = video_y_scanlines(colorformat, pix_height);
  475. y_ubwc_plane =
  476. MSM_MEDIA_ALIGN(y_stride * y_sclines, 4096);
  477. uv_sclines = video_uv_scanlines(colorformat, pix_height);
  478. uv_ubwc_plane =
  479. MSM_MEDIA_ALIGN(uv_stride * uv_sclines, 4096);
  480. y_meta_scanlines =
  481. video_y_meta_scanlines(colorformat, pix_height);
  482. y_meta_plane = MSM_MEDIA_ALIGN(
  483. y_meta_stride * y_meta_scanlines, 4096);
  484. uv_meta_scanlines =
  485. video_uv_meta_scanlines(colorformat, pix_height);
  486. uv_meta_plane = MSM_MEDIA_ALIGN(uv_meta_stride *
  487. uv_meta_scanlines, 4096);
  488. size = (y_ubwc_plane + uv_ubwc_plane + y_meta_plane +
  489. uv_meta_plane);
  490. }
  491. }
  492. break;
  493. case MSM_VIDC_FMT_TP10C:
  494. y_ubwc_plane = MSM_MEDIA_ALIGN(y_stride * y_sclines, 4096);
  495. uv_ubwc_plane = MSM_MEDIA_ALIGN(uv_stride * uv_sclines, 4096);
  496. y_meta_stride = video_y_meta_stride(colorformat, pix_width);
  497. y_meta_scanlines = video_y_meta_scanlines(colorformat, pix_height);
  498. y_meta_plane = MSM_MEDIA_ALIGN(
  499. y_meta_stride * y_meta_scanlines, 4096);
  500. uv_meta_stride = video_uv_meta_stride(colorformat, pix_width);
  501. uv_meta_scanlines = video_uv_meta_scanlines(colorformat, pix_height);
  502. uv_meta_plane = MSM_MEDIA_ALIGN(uv_meta_stride *
  503. uv_meta_scanlines, 4096);
  504. size = y_ubwc_plane + uv_ubwc_plane + y_meta_plane +
  505. uv_meta_plane;
  506. break;
  507. case MSM_VIDC_FMT_RGBA8888C:
  508. rgb_ubwc_plane = MSM_MEDIA_ALIGN(rgb_stride * rgb_scanlines,
  509. 4096);
  510. rgb_meta_stride = video_rgb_meta_stride(colorformat, pix_width);
  511. rgb_meta_scanlines = video_rgb_meta_scanlines(colorformat,
  512. pix_height);
  513. rgb_meta_plane = MSM_MEDIA_ALIGN(rgb_meta_stride *
  514. rgb_meta_scanlines, 4096);
  515. size = rgb_ubwc_plane + rgb_meta_plane;
  516. break;
  517. case MSM_VIDC_FMT_RGBA8888:
  518. rgb_plane = MSM_MEDIA_ALIGN(rgb_stride * rgb_scanlines, 4096);
  519. size = rgb_plane;
  520. break;
  521. default:
  522. break;
  523. }
  524. invalid_input:
  525. size = MSM_MEDIA_ALIGN(size, 4096);
  526. return size;
  527. }
  528. #endif