vivid-tpg.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * vivid-tpg.h - Test Pattern Generator
  3. *
  4. * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  5. *
  6. * This program is free software; you may redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  13. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  14. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  15. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. * SOFTWARE.
  18. */
  19. #ifndef _VIVID_TPG_H_
  20. #define _VIVID_TPG_H_
  21. #include <linux/types.h>
  22. #include <linux/errno.h>
  23. #include <linux/random.h>
  24. #include <linux/slab.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/videodev2.h>
  27. #include "vivid-tpg-colors.h"
  28. enum tpg_pattern {
  29. TPG_PAT_75_COLORBAR,
  30. TPG_PAT_100_COLORBAR,
  31. TPG_PAT_CSC_COLORBAR,
  32. TPG_PAT_100_HCOLORBAR,
  33. TPG_PAT_100_COLORSQUARES,
  34. TPG_PAT_BLACK,
  35. TPG_PAT_WHITE,
  36. TPG_PAT_RED,
  37. TPG_PAT_GREEN,
  38. TPG_PAT_BLUE,
  39. TPG_PAT_CHECKERS_16X16,
  40. TPG_PAT_CHECKERS_2X2,
  41. TPG_PAT_CHECKERS_1X1,
  42. TPG_PAT_COLOR_CHECKERS_2X2,
  43. TPG_PAT_COLOR_CHECKERS_1X1,
  44. TPG_PAT_ALTERNATING_HLINES,
  45. TPG_PAT_ALTERNATING_VLINES,
  46. TPG_PAT_CROSS_1_PIXEL,
  47. TPG_PAT_CROSS_2_PIXELS,
  48. TPG_PAT_CROSS_10_PIXELS,
  49. TPG_PAT_GRAY_RAMP,
  50. /* Must be the last pattern */
  51. TPG_PAT_NOISE,
  52. };
  53. extern const char * const tpg_pattern_strings[];
  54. enum tpg_quality {
  55. TPG_QUAL_COLOR,
  56. TPG_QUAL_GRAY,
  57. TPG_QUAL_NOISE
  58. };
  59. enum tpg_video_aspect {
  60. TPG_VIDEO_ASPECT_IMAGE,
  61. TPG_VIDEO_ASPECT_4X3,
  62. TPG_VIDEO_ASPECT_14X9_CENTRE,
  63. TPG_VIDEO_ASPECT_16X9_CENTRE,
  64. TPG_VIDEO_ASPECT_16X9_ANAMORPHIC,
  65. };
  66. enum tpg_pixel_aspect {
  67. TPG_PIXEL_ASPECT_SQUARE,
  68. TPG_PIXEL_ASPECT_NTSC,
  69. TPG_PIXEL_ASPECT_PAL,
  70. };
  71. enum tpg_move_mode {
  72. TPG_MOVE_NEG_FAST,
  73. TPG_MOVE_NEG,
  74. TPG_MOVE_NEG_SLOW,
  75. TPG_MOVE_NONE,
  76. TPG_MOVE_POS_SLOW,
  77. TPG_MOVE_POS,
  78. TPG_MOVE_POS_FAST,
  79. };
  80. extern const char * const tpg_aspect_strings[];
  81. #define TPG_MAX_PLANES 3
  82. #define TPG_MAX_PAT_LINES 8
  83. struct tpg_data {
  84. /* Source frame size */
  85. unsigned src_width, src_height;
  86. /* Buffer height */
  87. unsigned buf_height;
  88. /* Scaled output frame size */
  89. unsigned scaled_width;
  90. u32 field;
  91. bool field_alternate;
  92. /* crop coordinates are frame-based */
  93. struct v4l2_rect crop;
  94. /* compose coordinates are format-based */
  95. struct v4l2_rect compose;
  96. /* border and square coordinates are frame-based */
  97. struct v4l2_rect border;
  98. struct v4l2_rect square;
  99. /* Color-related fields */
  100. enum tpg_quality qual;
  101. unsigned qual_offset;
  102. u8 alpha_component;
  103. bool alpha_red_only;
  104. u8 brightness;
  105. u8 contrast;
  106. u8 saturation;
  107. s16 hue;
  108. u32 fourcc;
  109. bool is_yuv;
  110. u32 colorspace;
  111. u32 ycbcr_enc;
  112. /*
  113. * Stores the actual Y'CbCr encoding, i.e. will never be
  114. * V4L2_YCBCR_ENC_DEFAULT.
  115. */
  116. u32 real_ycbcr_enc;
  117. u32 quantization;
  118. /*
  119. * Stores the actual quantization, i.e. will never be
  120. * V4L2_QUANTIZATION_DEFAULT.
  121. */
  122. u32 real_quantization;
  123. enum tpg_video_aspect vid_aspect;
  124. enum tpg_pixel_aspect pix_aspect;
  125. unsigned rgb_range;
  126. unsigned real_rgb_range;
  127. unsigned buffers;
  128. unsigned planes;
  129. bool interleaved;
  130. u8 vdownsampling[TPG_MAX_PLANES];
  131. u8 hdownsampling[TPG_MAX_PLANES];
  132. /*
  133. * horizontal positions must be ANDed with this value to enforce
  134. * correct boundaries for packed YUYV values.
  135. */
  136. unsigned hmask[TPG_MAX_PLANES];
  137. /* Used to store the colors in native format, either RGB or YUV */
  138. u8 colors[TPG_COLOR_MAX][3];
  139. u8 textfg[TPG_MAX_PLANES][8], textbg[TPG_MAX_PLANES][8];
  140. /* size in bytes for two pixels in each plane */
  141. unsigned twopixelsize[TPG_MAX_PLANES];
  142. unsigned bytesperline[TPG_MAX_PLANES];
  143. /* Configuration */
  144. enum tpg_pattern pattern;
  145. bool hflip;
  146. bool vflip;
  147. unsigned perc_fill;
  148. bool perc_fill_blank;
  149. bool show_border;
  150. bool show_square;
  151. bool insert_sav;
  152. bool insert_eav;
  153. /* Test pattern movement */
  154. enum tpg_move_mode mv_hor_mode;
  155. int mv_hor_count;
  156. int mv_hor_step;
  157. enum tpg_move_mode mv_vert_mode;
  158. int mv_vert_count;
  159. int mv_vert_step;
  160. bool recalc_colors;
  161. bool recalc_lines;
  162. bool recalc_square_border;
  163. /* Used to store TPG_MAX_PAT_LINES lines, each with up to two planes */
  164. unsigned max_line_width;
  165. u8 *lines[TPG_MAX_PAT_LINES][TPG_MAX_PLANES];
  166. u8 *downsampled_lines[TPG_MAX_PAT_LINES][TPG_MAX_PLANES];
  167. u8 *random_line[TPG_MAX_PLANES];
  168. u8 *contrast_line[TPG_MAX_PLANES];
  169. u8 *black_line[TPG_MAX_PLANES];
  170. };
  171. void tpg_init(struct tpg_data *tpg, unsigned w, unsigned h);
  172. int tpg_alloc(struct tpg_data *tpg, unsigned max_w);
  173. void tpg_free(struct tpg_data *tpg);
  174. void tpg_reset_source(struct tpg_data *tpg, unsigned width, unsigned height,
  175. u32 field);
  176. void tpg_set_font(const u8 *f);
  177. void tpg_gen_text(const struct tpg_data *tpg,
  178. u8 *basep[TPG_MAX_PLANES][2], int y, int x, char *text);
  179. void tpg_calc_text_basep(struct tpg_data *tpg,
  180. u8 *basep[TPG_MAX_PLANES][2], unsigned p, u8 *vbuf);
  181. unsigned tpg_g_interleaved_plane(const struct tpg_data *tpg, unsigned buf_line);
  182. void tpg_fill_plane_buffer(struct tpg_data *tpg, v4l2_std_id std,
  183. unsigned p, u8 *vbuf);
  184. void tpg_fillbuffer(struct tpg_data *tpg, v4l2_std_id std,
  185. unsigned p, u8 *vbuf);
  186. bool tpg_s_fourcc(struct tpg_data *tpg, u32 fourcc);
  187. void tpg_s_crop_compose(struct tpg_data *tpg, const struct v4l2_rect *crop,
  188. const struct v4l2_rect *compose);
  189. static inline void tpg_s_pattern(struct tpg_data *tpg, enum tpg_pattern pattern)
  190. {
  191. if (tpg->pattern == pattern)
  192. return;
  193. tpg->pattern = pattern;
  194. tpg->recalc_colors = true;
  195. }
  196. static inline void tpg_s_quality(struct tpg_data *tpg,
  197. enum tpg_quality qual, unsigned qual_offset)
  198. {
  199. if (tpg->qual == qual && tpg->qual_offset == qual_offset)
  200. return;
  201. tpg->qual = qual;
  202. tpg->qual_offset = qual_offset;
  203. tpg->recalc_colors = true;
  204. }
  205. static inline enum tpg_quality tpg_g_quality(const struct tpg_data *tpg)
  206. {
  207. return tpg->qual;
  208. }
  209. static inline void tpg_s_alpha_component(struct tpg_data *tpg,
  210. u8 alpha_component)
  211. {
  212. if (tpg->alpha_component == alpha_component)
  213. return;
  214. tpg->alpha_component = alpha_component;
  215. tpg->recalc_colors = true;
  216. }
  217. static inline void tpg_s_alpha_mode(struct tpg_data *tpg,
  218. bool red_only)
  219. {
  220. if (tpg->alpha_red_only == red_only)
  221. return;
  222. tpg->alpha_red_only = red_only;
  223. tpg->recalc_colors = true;
  224. }
  225. static inline void tpg_s_brightness(struct tpg_data *tpg,
  226. u8 brightness)
  227. {
  228. if (tpg->brightness == brightness)
  229. return;
  230. tpg->brightness = brightness;
  231. tpg->recalc_colors = true;
  232. }
  233. static inline void tpg_s_contrast(struct tpg_data *tpg,
  234. u8 contrast)
  235. {
  236. if (tpg->contrast == contrast)
  237. return;
  238. tpg->contrast = contrast;
  239. tpg->recalc_colors = true;
  240. }
  241. static inline void tpg_s_saturation(struct tpg_data *tpg,
  242. u8 saturation)
  243. {
  244. if (tpg->saturation == saturation)
  245. return;
  246. tpg->saturation = saturation;
  247. tpg->recalc_colors = true;
  248. }
  249. static inline void tpg_s_hue(struct tpg_data *tpg,
  250. s16 hue)
  251. {
  252. if (tpg->hue == hue)
  253. return;
  254. tpg->hue = hue;
  255. tpg->recalc_colors = true;
  256. }
  257. static inline void tpg_s_rgb_range(struct tpg_data *tpg,
  258. unsigned rgb_range)
  259. {
  260. if (tpg->rgb_range == rgb_range)
  261. return;
  262. tpg->rgb_range = rgb_range;
  263. tpg->recalc_colors = true;
  264. }
  265. static inline void tpg_s_real_rgb_range(struct tpg_data *tpg,
  266. unsigned rgb_range)
  267. {
  268. if (tpg->real_rgb_range == rgb_range)
  269. return;
  270. tpg->real_rgb_range = rgb_range;
  271. tpg->recalc_colors = true;
  272. }
  273. static inline void tpg_s_colorspace(struct tpg_data *tpg, u32 colorspace)
  274. {
  275. if (tpg->colorspace == colorspace)
  276. return;
  277. tpg->colorspace = colorspace;
  278. tpg->recalc_colors = true;
  279. }
  280. static inline u32 tpg_g_colorspace(const struct tpg_data *tpg)
  281. {
  282. return tpg->colorspace;
  283. }
  284. static inline void tpg_s_ycbcr_enc(struct tpg_data *tpg, u32 ycbcr_enc)
  285. {
  286. if (tpg->ycbcr_enc == ycbcr_enc)
  287. return;
  288. tpg->ycbcr_enc = ycbcr_enc;
  289. tpg->recalc_colors = true;
  290. }
  291. static inline u32 tpg_g_ycbcr_enc(const struct tpg_data *tpg)
  292. {
  293. return tpg->ycbcr_enc;
  294. }
  295. static inline void tpg_s_quantization(struct tpg_data *tpg, u32 quantization)
  296. {
  297. if (tpg->quantization == quantization)
  298. return;
  299. tpg->quantization = quantization;
  300. tpg->recalc_colors = true;
  301. }
  302. static inline u32 tpg_g_quantization(const struct tpg_data *tpg)
  303. {
  304. return tpg->quantization;
  305. }
  306. static inline unsigned tpg_g_buffers(const struct tpg_data *tpg)
  307. {
  308. return tpg->buffers;
  309. }
  310. static inline unsigned tpg_g_planes(const struct tpg_data *tpg)
  311. {
  312. return tpg->interleaved ? 1 : tpg->planes;
  313. }
  314. static inline bool tpg_g_interleaved(const struct tpg_data *tpg)
  315. {
  316. return tpg->interleaved;
  317. }
  318. static inline unsigned tpg_g_twopixelsize(const struct tpg_data *tpg, unsigned plane)
  319. {
  320. return tpg->twopixelsize[plane];
  321. }
  322. static inline unsigned tpg_hdiv(const struct tpg_data *tpg,
  323. unsigned plane, unsigned x)
  324. {
  325. return ((x / tpg->hdownsampling[plane]) & tpg->hmask[plane]) *
  326. tpg->twopixelsize[plane] / 2;
  327. }
  328. static inline unsigned tpg_hscale(const struct tpg_data *tpg, unsigned x)
  329. {
  330. return (x * tpg->scaled_width) / tpg->src_width;
  331. }
  332. static inline unsigned tpg_hscale_div(const struct tpg_data *tpg,
  333. unsigned plane, unsigned x)
  334. {
  335. return tpg_hdiv(tpg, plane, tpg_hscale(tpg, x));
  336. }
  337. static inline unsigned tpg_g_bytesperline(const struct tpg_data *tpg, unsigned plane)
  338. {
  339. return tpg->bytesperline[plane];
  340. }
  341. static inline void tpg_s_bytesperline(struct tpg_data *tpg, unsigned plane, unsigned bpl)
  342. {
  343. unsigned p;
  344. if (tpg->buffers > 1) {
  345. tpg->bytesperline[plane] = bpl;
  346. return;
  347. }
  348. for (p = 0; p < tpg_g_planes(tpg); p++) {
  349. unsigned plane_w = bpl * tpg->twopixelsize[p] / tpg->twopixelsize[0];
  350. tpg->bytesperline[p] = plane_w / tpg->hdownsampling[p];
  351. }
  352. }
  353. static inline unsigned tpg_g_line_width(const struct tpg_data *tpg, unsigned plane)
  354. {
  355. unsigned w = 0;
  356. unsigned p;
  357. if (tpg->buffers > 1)
  358. return tpg_g_bytesperline(tpg, plane);
  359. for (p = 0; p < tpg_g_planes(tpg); p++) {
  360. unsigned plane_w = tpg_g_bytesperline(tpg, p);
  361. w += plane_w / tpg->vdownsampling[p];
  362. }
  363. return w;
  364. }
  365. static inline unsigned tpg_calc_line_width(const struct tpg_data *tpg,
  366. unsigned plane, unsigned bpl)
  367. {
  368. unsigned w = 0;
  369. unsigned p;
  370. if (tpg->buffers > 1)
  371. return bpl;
  372. for (p = 0; p < tpg_g_planes(tpg); p++) {
  373. unsigned plane_w = bpl * tpg->twopixelsize[p] / tpg->twopixelsize[0];
  374. plane_w /= tpg->hdownsampling[p];
  375. w += plane_w / tpg->vdownsampling[p];
  376. }
  377. return w;
  378. }
  379. static inline unsigned tpg_calc_plane_size(const struct tpg_data *tpg, unsigned plane)
  380. {
  381. if (plane >= tpg_g_planes(tpg))
  382. return 0;
  383. return tpg_g_bytesperline(tpg, plane) * tpg->buf_height /
  384. tpg->vdownsampling[plane];
  385. }
  386. static inline void tpg_s_buf_height(struct tpg_data *tpg, unsigned h)
  387. {
  388. tpg->buf_height = h;
  389. }
  390. static inline void tpg_s_field(struct tpg_data *tpg, unsigned field, bool alternate)
  391. {
  392. tpg->field = field;
  393. tpg->field_alternate = alternate;
  394. }
  395. static inline void tpg_s_perc_fill(struct tpg_data *tpg,
  396. unsigned perc_fill)
  397. {
  398. tpg->perc_fill = perc_fill;
  399. }
  400. static inline unsigned tpg_g_perc_fill(const struct tpg_data *tpg)
  401. {
  402. return tpg->perc_fill;
  403. }
  404. static inline void tpg_s_perc_fill_blank(struct tpg_data *tpg,
  405. bool perc_fill_blank)
  406. {
  407. tpg->perc_fill_blank = perc_fill_blank;
  408. }
  409. static inline void tpg_s_video_aspect(struct tpg_data *tpg,
  410. enum tpg_video_aspect vid_aspect)
  411. {
  412. if (tpg->vid_aspect == vid_aspect)
  413. return;
  414. tpg->vid_aspect = vid_aspect;
  415. tpg->recalc_square_border = true;
  416. }
  417. static inline enum tpg_video_aspect tpg_g_video_aspect(const struct tpg_data *tpg)
  418. {
  419. return tpg->vid_aspect;
  420. }
  421. static inline void tpg_s_pixel_aspect(struct tpg_data *tpg,
  422. enum tpg_pixel_aspect pix_aspect)
  423. {
  424. if (tpg->pix_aspect == pix_aspect)
  425. return;
  426. tpg->pix_aspect = pix_aspect;
  427. tpg->recalc_square_border = true;
  428. }
  429. static inline void tpg_s_show_border(struct tpg_data *tpg,
  430. bool show_border)
  431. {
  432. tpg->show_border = show_border;
  433. }
  434. static inline void tpg_s_show_square(struct tpg_data *tpg,
  435. bool show_square)
  436. {
  437. tpg->show_square = show_square;
  438. }
  439. static inline void tpg_s_insert_sav(struct tpg_data *tpg, bool insert_sav)
  440. {
  441. tpg->insert_sav = insert_sav;
  442. }
  443. static inline void tpg_s_insert_eav(struct tpg_data *tpg, bool insert_eav)
  444. {
  445. tpg->insert_eav = insert_eav;
  446. }
  447. void tpg_update_mv_step(struct tpg_data *tpg);
  448. static inline void tpg_s_mv_hor_mode(struct tpg_data *tpg,
  449. enum tpg_move_mode mv_hor_mode)
  450. {
  451. tpg->mv_hor_mode = mv_hor_mode;
  452. tpg_update_mv_step(tpg);
  453. }
  454. static inline void tpg_s_mv_vert_mode(struct tpg_data *tpg,
  455. enum tpg_move_mode mv_vert_mode)
  456. {
  457. tpg->mv_vert_mode = mv_vert_mode;
  458. tpg_update_mv_step(tpg);
  459. }
  460. static inline void tpg_init_mv_count(struct tpg_data *tpg)
  461. {
  462. tpg->mv_hor_count = tpg->mv_vert_count = 0;
  463. }
  464. static inline void tpg_update_mv_count(struct tpg_data *tpg, bool frame_is_field)
  465. {
  466. tpg->mv_hor_count += tpg->mv_hor_step * (frame_is_field ? 1 : 2);
  467. tpg->mv_vert_count += tpg->mv_vert_step * (frame_is_field ? 1 : 2);
  468. }
  469. static inline void tpg_s_hflip(struct tpg_data *tpg, bool hflip)
  470. {
  471. if (tpg->hflip == hflip)
  472. return;
  473. tpg->hflip = hflip;
  474. tpg_update_mv_step(tpg);
  475. tpg->recalc_lines = true;
  476. }
  477. static inline bool tpg_g_hflip(const struct tpg_data *tpg)
  478. {
  479. return tpg->hflip;
  480. }
  481. static inline void tpg_s_vflip(struct tpg_data *tpg, bool vflip)
  482. {
  483. tpg->vflip = vflip;
  484. }
  485. static inline bool tpg_g_vflip(const struct tpg_data *tpg)
  486. {
  487. return tpg->vflip;
  488. }
  489. static inline bool tpg_pattern_is_static(const struct tpg_data *tpg)
  490. {
  491. return tpg->pattern != TPG_PAT_NOISE &&
  492. tpg->mv_hor_mode == TPG_MOVE_NONE &&
  493. tpg->mv_vert_mode == TPG_MOVE_NONE;
  494. }
  495. #endif