vivid-tpg.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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/version.h>
  22. #include <linux/types.h>
  23. #include <linux/errno.h>
  24. #include <linux/random.h>
  25. #include <linux/slab.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/videodev2.h>
  28. #include "vivid-tpg-colors.h"
  29. enum tpg_pattern {
  30. TPG_PAT_75_COLORBAR,
  31. TPG_PAT_100_COLORBAR,
  32. TPG_PAT_CSC_COLORBAR,
  33. TPG_PAT_100_HCOLORBAR,
  34. TPG_PAT_100_COLORSQUARES,
  35. TPG_PAT_BLACK,
  36. TPG_PAT_WHITE,
  37. TPG_PAT_RED,
  38. TPG_PAT_GREEN,
  39. TPG_PAT_BLUE,
  40. TPG_PAT_CHECKERS_16X16,
  41. TPG_PAT_CHECKERS_1X1,
  42. TPG_PAT_ALTERNATING_HLINES,
  43. TPG_PAT_ALTERNATING_VLINES,
  44. TPG_PAT_CROSS_1_PIXEL,
  45. TPG_PAT_CROSS_2_PIXELS,
  46. TPG_PAT_CROSS_10_PIXELS,
  47. TPG_PAT_GRAY_RAMP,
  48. /* Must be the last pattern */
  49. TPG_PAT_NOISE,
  50. };
  51. extern const char * const tpg_pattern_strings[];
  52. enum tpg_quality {
  53. TPG_QUAL_COLOR,
  54. TPG_QUAL_GRAY,
  55. TPG_QUAL_NOISE
  56. };
  57. enum tpg_video_aspect {
  58. TPG_VIDEO_ASPECT_IMAGE,
  59. TPG_VIDEO_ASPECT_4X3,
  60. TPG_VIDEO_ASPECT_14X9_CENTRE,
  61. TPG_VIDEO_ASPECT_16X9_CENTRE,
  62. TPG_VIDEO_ASPECT_16X9_ANAMORPHIC,
  63. };
  64. enum tpg_pixel_aspect {
  65. TPG_PIXEL_ASPECT_SQUARE,
  66. TPG_PIXEL_ASPECT_NTSC,
  67. TPG_PIXEL_ASPECT_PAL,
  68. };
  69. enum tpg_move_mode {
  70. TPG_MOVE_NEG_FAST,
  71. TPG_MOVE_NEG,
  72. TPG_MOVE_NEG_SLOW,
  73. TPG_MOVE_NONE,
  74. TPG_MOVE_POS_SLOW,
  75. TPG_MOVE_POS,
  76. TPG_MOVE_POS_FAST,
  77. };
  78. extern const char * const tpg_aspect_strings[];
  79. #define TPG_MAX_PLANES 2
  80. #define TPG_MAX_PAT_LINES 8
  81. struct tpg_data {
  82. /* Source frame size */
  83. unsigned src_width, src_height;
  84. /* Buffer height */
  85. unsigned buf_height;
  86. /* Scaled output frame size */
  87. unsigned scaled_width;
  88. u32 field;
  89. /* crop coordinates are frame-based */
  90. struct v4l2_rect crop;
  91. /* compose coordinates are format-based */
  92. struct v4l2_rect compose;
  93. /* border and square coordinates are frame-based */
  94. struct v4l2_rect border;
  95. struct v4l2_rect square;
  96. /* Color-related fields */
  97. enum tpg_quality qual;
  98. unsigned qual_offset;
  99. u8 alpha_component;
  100. bool alpha_red_only;
  101. u8 brightness;
  102. u8 contrast;
  103. u8 saturation;
  104. s16 hue;
  105. u32 fourcc;
  106. bool is_yuv;
  107. u32 colorspace;
  108. enum tpg_video_aspect vid_aspect;
  109. enum tpg_pixel_aspect pix_aspect;
  110. unsigned rgb_range;
  111. unsigned real_rgb_range;
  112. unsigned planes;
  113. /* Used to store the colors in native format, either RGB or YUV */
  114. u8 colors[TPG_COLOR_MAX][3];
  115. u8 textfg[TPG_MAX_PLANES][8], textbg[TPG_MAX_PLANES][8];
  116. /* size in bytes for two pixels in each plane */
  117. unsigned twopixelsize[TPG_MAX_PLANES];
  118. unsigned bytesperline[TPG_MAX_PLANES];
  119. /* Configuration */
  120. enum tpg_pattern pattern;
  121. bool hflip;
  122. bool vflip;
  123. unsigned perc_fill;
  124. bool perc_fill_blank;
  125. bool show_border;
  126. bool show_square;
  127. bool insert_sav;
  128. bool insert_eav;
  129. /* Test pattern movement */
  130. enum tpg_move_mode mv_hor_mode;
  131. int mv_hor_count;
  132. int mv_hor_step;
  133. enum tpg_move_mode mv_vert_mode;
  134. int mv_vert_count;
  135. int mv_vert_step;
  136. bool recalc_colors;
  137. bool recalc_lines;
  138. bool recalc_square_border;
  139. /* Used to store TPG_MAX_PAT_LINES lines, each with up to two planes */
  140. unsigned max_line_width;
  141. u8 *lines[TPG_MAX_PAT_LINES][TPG_MAX_PLANES];
  142. u8 *random_line[TPG_MAX_PLANES];
  143. u8 *contrast_line[TPG_MAX_PLANES];
  144. u8 *black_line[TPG_MAX_PLANES];
  145. };
  146. void tpg_init(struct tpg_data *tpg, unsigned w, unsigned h);
  147. int tpg_alloc(struct tpg_data *tpg, unsigned max_w);
  148. void tpg_free(struct tpg_data *tpg);
  149. void tpg_reset_source(struct tpg_data *tpg, unsigned width, unsigned height,
  150. u32 field);
  151. void tpg_set_font(const u8 *f);
  152. void tpg_gen_text(struct tpg_data *tpg,
  153. u8 *basep[TPG_MAX_PLANES][2], int y, int x, char *text);
  154. void tpg_calc_text_basep(struct tpg_data *tpg,
  155. u8 *basep[TPG_MAX_PLANES][2], unsigned p, u8 *vbuf);
  156. void tpg_fillbuffer(struct tpg_data *tpg, v4l2_std_id std, unsigned p, u8 *vbuf);
  157. bool tpg_s_fourcc(struct tpg_data *tpg, u32 fourcc);
  158. void tpg_s_crop_compose(struct tpg_data *tpg, const struct v4l2_rect *crop,
  159. const struct v4l2_rect *compose);
  160. static inline void tpg_s_pattern(struct tpg_data *tpg, enum tpg_pattern pattern)
  161. {
  162. if (tpg->pattern == pattern)
  163. return;
  164. tpg->pattern = pattern;
  165. tpg->recalc_colors = true;
  166. }
  167. static inline void tpg_s_quality(struct tpg_data *tpg,
  168. enum tpg_quality qual, unsigned qual_offset)
  169. {
  170. if (tpg->qual == qual && tpg->qual_offset == qual_offset)
  171. return;
  172. tpg->qual = qual;
  173. tpg->qual_offset = qual_offset;
  174. tpg->recalc_colors = true;
  175. }
  176. static inline enum tpg_quality tpg_g_quality(const struct tpg_data *tpg)
  177. {
  178. return tpg->qual;
  179. }
  180. static inline void tpg_s_alpha_component(struct tpg_data *tpg,
  181. u8 alpha_component)
  182. {
  183. if (tpg->alpha_component == alpha_component)
  184. return;
  185. tpg->alpha_component = alpha_component;
  186. tpg->recalc_colors = true;
  187. }
  188. static inline void tpg_s_alpha_mode(struct tpg_data *tpg,
  189. bool red_only)
  190. {
  191. if (tpg->alpha_red_only == red_only)
  192. return;
  193. tpg->alpha_red_only = red_only;
  194. tpg->recalc_colors = true;
  195. }
  196. static inline void tpg_s_brightness(struct tpg_data *tpg,
  197. u8 brightness)
  198. {
  199. if (tpg->brightness == brightness)
  200. return;
  201. tpg->brightness = brightness;
  202. tpg->recalc_colors = true;
  203. }
  204. static inline void tpg_s_contrast(struct tpg_data *tpg,
  205. u8 contrast)
  206. {
  207. if (tpg->contrast == contrast)
  208. return;
  209. tpg->contrast = contrast;
  210. tpg->recalc_colors = true;
  211. }
  212. static inline void tpg_s_saturation(struct tpg_data *tpg,
  213. u8 saturation)
  214. {
  215. if (tpg->saturation == saturation)
  216. return;
  217. tpg->saturation = saturation;
  218. tpg->recalc_colors = true;
  219. }
  220. static inline void tpg_s_hue(struct tpg_data *tpg,
  221. s16 hue)
  222. {
  223. if (tpg->hue == hue)
  224. return;
  225. tpg->hue = hue;
  226. tpg->recalc_colors = true;
  227. }
  228. static inline void tpg_s_rgb_range(struct tpg_data *tpg,
  229. unsigned rgb_range)
  230. {
  231. if (tpg->rgb_range == rgb_range)
  232. return;
  233. tpg->rgb_range = rgb_range;
  234. tpg->recalc_colors = true;
  235. }
  236. static inline void tpg_s_real_rgb_range(struct tpg_data *tpg,
  237. unsigned rgb_range)
  238. {
  239. if (tpg->real_rgb_range == rgb_range)
  240. return;
  241. tpg->real_rgb_range = rgb_range;
  242. tpg->recalc_colors = true;
  243. }
  244. static inline void tpg_s_colorspace(struct tpg_data *tpg, u32 colorspace)
  245. {
  246. if (tpg->colorspace == colorspace)
  247. return;
  248. tpg->colorspace = colorspace;
  249. tpg->recalc_colors = true;
  250. }
  251. static inline u32 tpg_g_colorspace(const struct tpg_data *tpg)
  252. {
  253. return tpg->colorspace;
  254. }
  255. static inline unsigned tpg_g_planes(const struct tpg_data *tpg)
  256. {
  257. return tpg->planes;
  258. }
  259. static inline unsigned tpg_g_twopixelsize(const struct tpg_data *tpg, unsigned plane)
  260. {
  261. return tpg->twopixelsize[plane];
  262. }
  263. static inline unsigned tpg_g_bytesperline(const struct tpg_data *tpg, unsigned plane)
  264. {
  265. return tpg->bytesperline[plane];
  266. }
  267. static inline void tpg_s_bytesperline(struct tpg_data *tpg, unsigned plane, unsigned bpl)
  268. {
  269. tpg->bytesperline[plane] = bpl;
  270. }
  271. static inline void tpg_s_buf_height(struct tpg_data *tpg, unsigned h)
  272. {
  273. tpg->buf_height = h;
  274. }
  275. static inline void tpg_s_field(struct tpg_data *tpg, unsigned field)
  276. {
  277. tpg->field = field;
  278. }
  279. static inline void tpg_s_perc_fill(struct tpg_data *tpg,
  280. unsigned perc_fill)
  281. {
  282. tpg->perc_fill = perc_fill;
  283. }
  284. static inline unsigned tpg_g_perc_fill(const struct tpg_data *tpg)
  285. {
  286. return tpg->perc_fill;
  287. }
  288. static inline void tpg_s_perc_fill_blank(struct tpg_data *tpg,
  289. bool perc_fill_blank)
  290. {
  291. tpg->perc_fill_blank = perc_fill_blank;
  292. }
  293. static inline void tpg_s_video_aspect(struct tpg_data *tpg,
  294. enum tpg_video_aspect vid_aspect)
  295. {
  296. if (tpg->vid_aspect == vid_aspect)
  297. return;
  298. tpg->vid_aspect = vid_aspect;
  299. tpg->recalc_square_border = true;
  300. }
  301. static inline enum tpg_video_aspect tpg_g_video_aspect(const struct tpg_data *tpg)
  302. {
  303. return tpg->vid_aspect;
  304. }
  305. static inline void tpg_s_pixel_aspect(struct tpg_data *tpg,
  306. enum tpg_pixel_aspect pix_aspect)
  307. {
  308. if (tpg->pix_aspect == pix_aspect)
  309. return;
  310. tpg->pix_aspect = pix_aspect;
  311. tpg->recalc_square_border = true;
  312. }
  313. static inline void tpg_s_show_border(struct tpg_data *tpg,
  314. bool show_border)
  315. {
  316. tpg->show_border = show_border;
  317. }
  318. static inline void tpg_s_show_square(struct tpg_data *tpg,
  319. bool show_square)
  320. {
  321. tpg->show_square = show_square;
  322. }
  323. static inline void tpg_s_insert_sav(struct tpg_data *tpg, bool insert_sav)
  324. {
  325. tpg->insert_sav = insert_sav;
  326. }
  327. static inline void tpg_s_insert_eav(struct tpg_data *tpg, bool insert_eav)
  328. {
  329. tpg->insert_eav = insert_eav;
  330. }
  331. void tpg_update_mv_step(struct tpg_data *tpg);
  332. static inline void tpg_s_mv_hor_mode(struct tpg_data *tpg,
  333. enum tpg_move_mode mv_hor_mode)
  334. {
  335. tpg->mv_hor_mode = mv_hor_mode;
  336. tpg_update_mv_step(tpg);
  337. }
  338. static inline void tpg_s_mv_vert_mode(struct tpg_data *tpg,
  339. enum tpg_move_mode mv_vert_mode)
  340. {
  341. tpg->mv_vert_mode = mv_vert_mode;
  342. tpg_update_mv_step(tpg);
  343. }
  344. static inline void tpg_init_mv_count(struct tpg_data *tpg)
  345. {
  346. tpg->mv_hor_count = tpg->mv_vert_count = 0;
  347. }
  348. static inline void tpg_update_mv_count(struct tpg_data *tpg, bool frame_is_field)
  349. {
  350. tpg->mv_hor_count += tpg->mv_hor_step * (frame_is_field ? 1 : 2);
  351. tpg->mv_vert_count += tpg->mv_vert_step * (frame_is_field ? 1 : 2);
  352. }
  353. static inline void tpg_s_hflip(struct tpg_data *tpg, bool hflip)
  354. {
  355. if (tpg->hflip == hflip)
  356. return;
  357. tpg->hflip = hflip;
  358. tpg_update_mv_step(tpg);
  359. tpg->recalc_lines = true;
  360. }
  361. static inline bool tpg_g_hflip(const struct tpg_data *tpg)
  362. {
  363. return tpg->hflip;
  364. }
  365. static inline void tpg_s_vflip(struct tpg_data *tpg, bool vflip)
  366. {
  367. tpg->vflip = vflip;
  368. }
  369. static inline bool tpg_g_vflip(const struct tpg_data *tpg)
  370. {
  371. return tpg->vflip;
  372. }
  373. static inline bool tpg_pattern_is_static(const struct tpg_data *tpg)
  374. {
  375. return tpg->pattern != TPG_PAT_NOISE &&
  376. tpg->mv_hor_mode == TPG_MOVE_NONE &&
  377. tpg->mv_vert_mode == TPG_MOVE_NONE;
  378. }
  379. #endif