v4l2-tpg.h 14 KB

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