v4l2-tpg.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. * v4l2-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 _V4L2_TPG_H_
  20. #define _V4L2_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 <media/v4l2-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. enum tgp_color_enc {
  81. TGP_COLOR_ENC_RGB,
  82. TGP_COLOR_ENC_YCBCR,
  83. TGP_COLOR_ENC_HSV,
  84. TGP_COLOR_ENC_LUMA,
  85. };
  86. extern const char * const tpg_aspect_strings[];
  87. #define TPG_MAX_PLANES 3
  88. #define TPG_MAX_PAT_LINES 8
  89. struct tpg_data {
  90. /* Source frame size */
  91. unsigned src_width, src_height;
  92. /* Buffer height */
  93. unsigned buf_height;
  94. /* Scaled output frame size */
  95. unsigned scaled_width;
  96. u32 field;
  97. bool field_alternate;
  98. /* crop coordinates are frame-based */
  99. struct v4l2_rect crop;
  100. /* compose coordinates are format-based */
  101. struct v4l2_rect compose;
  102. /* border and square coordinates are frame-based */
  103. struct v4l2_rect border;
  104. struct v4l2_rect square;
  105. /* Color-related fields */
  106. enum tpg_quality qual;
  107. unsigned qual_offset;
  108. u8 alpha_component;
  109. bool alpha_red_only;
  110. u8 brightness;
  111. u8 contrast;
  112. u8 saturation;
  113. s16 hue;
  114. u32 fourcc;
  115. enum tgp_color_enc color_enc;
  116. u32 colorspace;
  117. u32 xfer_func;
  118. u32 ycbcr_enc;
  119. u32 hsv_enc;
  120. /*
  121. * Stores the actual transfer function, i.e. will never be
  122. * V4L2_XFER_FUNC_DEFAULT.
  123. */
  124. u32 real_xfer_func;
  125. /*
  126. * Stores the actual Y'CbCr encoding, i.e. will never be
  127. * V4L2_YCBCR_ENC_DEFAULT.
  128. */
  129. u32 real_hsv_enc;
  130. u32 real_ycbcr_enc;
  131. u32 quantization;
  132. /*
  133. * Stores the actual quantization, i.e. will never be
  134. * V4L2_QUANTIZATION_DEFAULT.
  135. */
  136. u32 real_quantization;
  137. enum tpg_video_aspect vid_aspect;
  138. enum tpg_pixel_aspect pix_aspect;
  139. unsigned rgb_range;
  140. unsigned real_rgb_range;
  141. unsigned buffers;
  142. unsigned planes;
  143. bool interleaved;
  144. u8 vdownsampling[TPG_MAX_PLANES];
  145. u8 hdownsampling[TPG_MAX_PLANES];
  146. /*
  147. * horizontal positions must be ANDed with this value to enforce
  148. * correct boundaries for packed YUYV values.
  149. */
  150. unsigned hmask[TPG_MAX_PLANES];
  151. /* Used to store the colors in native format, either RGB or YUV */
  152. u8 colors[TPG_COLOR_MAX][3];
  153. u8 textfg[TPG_MAX_PLANES][8], textbg[TPG_MAX_PLANES][8];
  154. /* size in bytes for two pixels in each plane */
  155. unsigned twopixelsize[TPG_MAX_PLANES];
  156. unsigned bytesperline[TPG_MAX_PLANES];
  157. /* Configuration */
  158. enum tpg_pattern pattern;
  159. bool hflip;
  160. bool vflip;
  161. unsigned perc_fill;
  162. bool perc_fill_blank;
  163. bool show_border;
  164. bool show_square;
  165. bool insert_sav;
  166. bool insert_eav;
  167. /* Test pattern movement */
  168. enum tpg_move_mode mv_hor_mode;
  169. int mv_hor_count;
  170. int mv_hor_step;
  171. enum tpg_move_mode mv_vert_mode;
  172. int mv_vert_count;
  173. int mv_vert_step;
  174. bool recalc_colors;
  175. bool recalc_lines;
  176. bool recalc_square_border;
  177. /* Used to store TPG_MAX_PAT_LINES lines, each with up to two planes */
  178. unsigned max_line_width;
  179. u8 *lines[TPG_MAX_PAT_LINES][TPG_MAX_PLANES];
  180. u8 *downsampled_lines[TPG_MAX_PAT_LINES][TPG_MAX_PLANES];
  181. u8 *random_line[TPG_MAX_PLANES];
  182. u8 *contrast_line[TPG_MAX_PLANES];
  183. u8 *black_line[TPG_MAX_PLANES];
  184. };
  185. void tpg_init(struct tpg_data *tpg, unsigned w, unsigned h);
  186. int tpg_alloc(struct tpg_data *tpg, unsigned max_w);
  187. void tpg_free(struct tpg_data *tpg);
  188. void tpg_reset_source(struct tpg_data *tpg, unsigned width, unsigned height,
  189. u32 field);
  190. void tpg_log_status(struct tpg_data *tpg);
  191. void tpg_set_font(const u8 *f);
  192. void tpg_gen_text(const struct tpg_data *tpg,
  193. u8 *basep[TPG_MAX_PLANES][2], int y, int x, char *text);
  194. void tpg_calc_text_basep(struct tpg_data *tpg,
  195. u8 *basep[TPG_MAX_PLANES][2], unsigned p, u8 *vbuf);
  196. unsigned tpg_g_interleaved_plane(const struct tpg_data *tpg, unsigned buf_line);
  197. void tpg_fill_plane_buffer(struct tpg_data *tpg, v4l2_std_id std,
  198. unsigned p, u8 *vbuf);
  199. void tpg_fillbuffer(struct tpg_data *tpg, v4l2_std_id std,
  200. unsigned p, u8 *vbuf);
  201. bool tpg_s_fourcc(struct tpg_data *tpg, u32 fourcc);
  202. void tpg_s_crop_compose(struct tpg_data *tpg, const struct v4l2_rect *crop,
  203. const struct v4l2_rect *compose);
  204. static inline void tpg_s_pattern(struct tpg_data *tpg, enum tpg_pattern pattern)
  205. {
  206. if (tpg->pattern == pattern)
  207. return;
  208. tpg->pattern = pattern;
  209. tpg->recalc_colors = true;
  210. }
  211. static inline void tpg_s_quality(struct tpg_data *tpg,
  212. enum tpg_quality qual, unsigned qual_offset)
  213. {
  214. if (tpg->qual == qual && tpg->qual_offset == qual_offset)
  215. return;
  216. tpg->qual = qual;
  217. tpg->qual_offset = qual_offset;
  218. tpg->recalc_colors = true;
  219. }
  220. static inline enum tpg_quality tpg_g_quality(const struct tpg_data *tpg)
  221. {
  222. return tpg->qual;
  223. }
  224. static inline void tpg_s_alpha_component(struct tpg_data *tpg,
  225. u8 alpha_component)
  226. {
  227. if (tpg->alpha_component == alpha_component)
  228. return;
  229. tpg->alpha_component = alpha_component;
  230. tpg->recalc_colors = true;
  231. }
  232. static inline void tpg_s_alpha_mode(struct tpg_data *tpg,
  233. bool red_only)
  234. {
  235. if (tpg->alpha_red_only == red_only)
  236. return;
  237. tpg->alpha_red_only = red_only;
  238. tpg->recalc_colors = true;
  239. }
  240. static inline void tpg_s_brightness(struct tpg_data *tpg,
  241. u8 brightness)
  242. {
  243. if (tpg->brightness == brightness)
  244. return;
  245. tpg->brightness = brightness;
  246. tpg->recalc_colors = true;
  247. }
  248. static inline void tpg_s_contrast(struct tpg_data *tpg,
  249. u8 contrast)
  250. {
  251. if (tpg->contrast == contrast)
  252. return;
  253. tpg->contrast = contrast;
  254. tpg->recalc_colors = true;
  255. }
  256. static inline void tpg_s_saturation(struct tpg_data *tpg,
  257. u8 saturation)
  258. {
  259. if (tpg->saturation == saturation)
  260. return;
  261. tpg->saturation = saturation;
  262. tpg->recalc_colors = true;
  263. }
  264. static inline void tpg_s_hue(struct tpg_data *tpg,
  265. s16 hue)
  266. {
  267. if (tpg->hue == hue)
  268. return;
  269. tpg->hue = hue;
  270. tpg->recalc_colors = true;
  271. }
  272. static inline void tpg_s_rgb_range(struct tpg_data *tpg,
  273. unsigned rgb_range)
  274. {
  275. if (tpg->rgb_range == rgb_range)
  276. return;
  277. tpg->rgb_range = rgb_range;
  278. tpg->recalc_colors = true;
  279. }
  280. static inline void tpg_s_real_rgb_range(struct tpg_data *tpg,
  281. unsigned rgb_range)
  282. {
  283. if (tpg->real_rgb_range == rgb_range)
  284. return;
  285. tpg->real_rgb_range = rgb_range;
  286. tpg->recalc_colors = true;
  287. }
  288. static inline void tpg_s_colorspace(struct tpg_data *tpg, u32 colorspace)
  289. {
  290. if (tpg->colorspace == colorspace)
  291. return;
  292. tpg->colorspace = colorspace;
  293. tpg->recalc_colors = true;
  294. }
  295. static inline u32 tpg_g_colorspace(const struct tpg_data *tpg)
  296. {
  297. return tpg->colorspace;
  298. }
  299. static inline void tpg_s_ycbcr_enc(struct tpg_data *tpg, u32 ycbcr_enc)
  300. {
  301. if (tpg->ycbcr_enc == ycbcr_enc)
  302. return;
  303. tpg->ycbcr_enc = ycbcr_enc;
  304. tpg->recalc_colors = true;
  305. }
  306. static inline u32 tpg_g_ycbcr_enc(const struct tpg_data *tpg)
  307. {
  308. return tpg->ycbcr_enc;
  309. }
  310. static inline void tpg_s_hsv_enc(struct tpg_data *tpg, u32 hsv_enc)
  311. {
  312. if (tpg->hsv_enc == hsv_enc)
  313. return;
  314. tpg->hsv_enc = hsv_enc;
  315. tpg->recalc_colors = true;
  316. }
  317. static inline u32 tpg_g_hsv_enc(const struct tpg_data *tpg)
  318. {
  319. return tpg->hsv_enc;
  320. }
  321. static inline void tpg_s_xfer_func(struct tpg_data *tpg, u32 xfer_func)
  322. {
  323. if (tpg->xfer_func == xfer_func)
  324. return;
  325. tpg->xfer_func = xfer_func;
  326. tpg->recalc_colors = true;
  327. }
  328. static inline u32 tpg_g_xfer_func(const struct tpg_data *tpg)
  329. {
  330. return tpg->xfer_func;
  331. }
  332. static inline void tpg_s_quantization(struct tpg_data *tpg, u32 quantization)
  333. {
  334. if (tpg->quantization == quantization)
  335. return;
  336. tpg->quantization = quantization;
  337. tpg->recalc_colors = true;
  338. }
  339. static inline u32 tpg_g_quantization(const struct tpg_data *tpg)
  340. {
  341. return tpg->quantization;
  342. }
  343. static inline unsigned tpg_g_buffers(const struct tpg_data *tpg)
  344. {
  345. return tpg->buffers;
  346. }
  347. static inline unsigned tpg_g_planes(const struct tpg_data *tpg)
  348. {
  349. return tpg->interleaved ? 1 : tpg->planes;
  350. }
  351. static inline bool tpg_g_interleaved(const struct tpg_data *tpg)
  352. {
  353. return tpg->interleaved;
  354. }
  355. static inline unsigned tpg_g_twopixelsize(const struct tpg_data *tpg, unsigned plane)
  356. {
  357. return tpg->twopixelsize[plane];
  358. }
  359. static inline unsigned tpg_hdiv(const struct tpg_data *tpg,
  360. unsigned plane, unsigned x)
  361. {
  362. return ((x / tpg->hdownsampling[plane]) & tpg->hmask[plane]) *
  363. tpg->twopixelsize[plane] / 2;
  364. }
  365. static inline unsigned tpg_hscale(const struct tpg_data *tpg, unsigned x)
  366. {
  367. return (x * tpg->scaled_width) / tpg->src_width;
  368. }
  369. static inline unsigned tpg_hscale_div(const struct tpg_data *tpg,
  370. unsigned plane, unsigned x)
  371. {
  372. return tpg_hdiv(tpg, plane, tpg_hscale(tpg, x));
  373. }
  374. static inline unsigned tpg_g_bytesperline(const struct tpg_data *tpg, unsigned plane)
  375. {
  376. return tpg->bytesperline[plane];
  377. }
  378. static inline void tpg_s_bytesperline(struct tpg_data *tpg, unsigned plane, unsigned bpl)
  379. {
  380. unsigned p;
  381. if (tpg->buffers > 1) {
  382. tpg->bytesperline[plane] = bpl;
  383. return;
  384. }
  385. for (p = 0; p < tpg_g_planes(tpg); p++) {
  386. unsigned plane_w = bpl * tpg->twopixelsize[p] / tpg->twopixelsize[0];
  387. tpg->bytesperline[p] = plane_w / tpg->hdownsampling[p];
  388. }
  389. if (tpg_g_interleaved(tpg))
  390. tpg->bytesperline[1] = tpg->bytesperline[0];
  391. }
  392. static inline unsigned tpg_g_line_width(const struct tpg_data *tpg, unsigned plane)
  393. {
  394. unsigned w = 0;
  395. unsigned p;
  396. if (tpg->buffers > 1)
  397. return tpg_g_bytesperline(tpg, plane);
  398. for (p = 0; p < tpg_g_planes(tpg); p++) {
  399. unsigned plane_w = tpg_g_bytesperline(tpg, p);
  400. w += plane_w / tpg->vdownsampling[p];
  401. }
  402. return w;
  403. }
  404. static inline unsigned tpg_calc_line_width(const struct tpg_data *tpg,
  405. unsigned plane, unsigned bpl)
  406. {
  407. unsigned w = 0;
  408. unsigned p;
  409. if (tpg->buffers > 1)
  410. return bpl;
  411. for (p = 0; p < tpg_g_planes(tpg); p++) {
  412. unsigned plane_w = bpl * tpg->twopixelsize[p] / tpg->twopixelsize[0];
  413. plane_w /= tpg->hdownsampling[p];
  414. w += plane_w / tpg->vdownsampling[p];
  415. }
  416. return w;
  417. }
  418. static inline unsigned tpg_calc_plane_size(const struct tpg_data *tpg, unsigned plane)
  419. {
  420. if (plane >= tpg_g_planes(tpg))
  421. return 0;
  422. return tpg_g_bytesperline(tpg, plane) * tpg->buf_height /
  423. tpg->vdownsampling[plane];
  424. }
  425. static inline void tpg_s_buf_height(struct tpg_data *tpg, unsigned h)
  426. {
  427. tpg->buf_height = h;
  428. }
  429. static inline void tpg_s_field(struct tpg_data *tpg, unsigned field, bool alternate)
  430. {
  431. tpg->field = field;
  432. tpg->field_alternate = alternate;
  433. }
  434. static inline void tpg_s_perc_fill(struct tpg_data *tpg,
  435. unsigned perc_fill)
  436. {
  437. tpg->perc_fill = perc_fill;
  438. }
  439. static inline unsigned tpg_g_perc_fill(const struct tpg_data *tpg)
  440. {
  441. return tpg->perc_fill;
  442. }
  443. static inline void tpg_s_perc_fill_blank(struct tpg_data *tpg,
  444. bool perc_fill_blank)
  445. {
  446. tpg->perc_fill_blank = perc_fill_blank;
  447. }
  448. static inline void tpg_s_video_aspect(struct tpg_data *tpg,
  449. enum tpg_video_aspect vid_aspect)
  450. {
  451. if (tpg->vid_aspect == vid_aspect)
  452. return;
  453. tpg->vid_aspect = vid_aspect;
  454. tpg->recalc_square_border = true;
  455. }
  456. static inline enum tpg_video_aspect tpg_g_video_aspect(const struct tpg_data *tpg)
  457. {
  458. return tpg->vid_aspect;
  459. }
  460. static inline void tpg_s_pixel_aspect(struct tpg_data *tpg,
  461. enum tpg_pixel_aspect pix_aspect)
  462. {
  463. if (tpg->pix_aspect == pix_aspect)
  464. return;
  465. tpg->pix_aspect = pix_aspect;
  466. tpg->recalc_square_border = true;
  467. }
  468. static inline void tpg_s_show_border(struct tpg_data *tpg,
  469. bool show_border)
  470. {
  471. tpg->show_border = show_border;
  472. }
  473. static inline void tpg_s_show_square(struct tpg_data *tpg,
  474. bool show_square)
  475. {
  476. tpg->show_square = show_square;
  477. }
  478. static inline void tpg_s_insert_sav(struct tpg_data *tpg, bool insert_sav)
  479. {
  480. tpg->insert_sav = insert_sav;
  481. }
  482. static inline void tpg_s_insert_eav(struct tpg_data *tpg, bool insert_eav)
  483. {
  484. tpg->insert_eav = insert_eav;
  485. }
  486. void tpg_update_mv_step(struct tpg_data *tpg);
  487. static inline void tpg_s_mv_hor_mode(struct tpg_data *tpg,
  488. enum tpg_move_mode mv_hor_mode)
  489. {
  490. tpg->mv_hor_mode = mv_hor_mode;
  491. tpg_update_mv_step(tpg);
  492. }
  493. static inline void tpg_s_mv_vert_mode(struct tpg_data *tpg,
  494. enum tpg_move_mode mv_vert_mode)
  495. {
  496. tpg->mv_vert_mode = mv_vert_mode;
  497. tpg_update_mv_step(tpg);
  498. }
  499. static inline void tpg_init_mv_count(struct tpg_data *tpg)
  500. {
  501. tpg->mv_hor_count = tpg->mv_vert_count = 0;
  502. }
  503. static inline void tpg_update_mv_count(struct tpg_data *tpg, bool frame_is_field)
  504. {
  505. tpg->mv_hor_count += tpg->mv_hor_step * (frame_is_field ? 1 : 2);
  506. tpg->mv_vert_count += tpg->mv_vert_step * (frame_is_field ? 1 : 2);
  507. }
  508. static inline void tpg_s_hflip(struct tpg_data *tpg, bool hflip)
  509. {
  510. if (tpg->hflip == hflip)
  511. return;
  512. tpg->hflip = hflip;
  513. tpg_update_mv_step(tpg);
  514. tpg->recalc_lines = true;
  515. }
  516. static inline bool tpg_g_hflip(const struct tpg_data *tpg)
  517. {
  518. return tpg->hflip;
  519. }
  520. static inline void tpg_s_vflip(struct tpg_data *tpg, bool vflip)
  521. {
  522. tpg->vflip = vflip;
  523. }
  524. static inline bool tpg_g_vflip(const struct tpg_data *tpg)
  525. {
  526. return tpg->vflip;
  527. }
  528. static inline bool tpg_pattern_is_static(const struct tpg_data *tpg)
  529. {
  530. return tpg->pattern != TPG_PAT_NOISE &&
  531. tpg->mv_hor_mode == TPG_MOVE_NONE &&
  532. tpg->mv_vert_mode == TPG_MOVE_NONE;
  533. }
  534. #endif