vivid-core.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * vivid-core.h - core datastructures
  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_CORE_H_
  20. #define _VIVID_CORE_H_
  21. #include <linux/fb.h>
  22. #include <media/videobuf2-core.h>
  23. #include <media/v4l2-device.h>
  24. #include <media/v4l2-dev.h>
  25. #include <media/v4l2-ctrls.h>
  26. #include "vivid-tpg.h"
  27. #include "vivid-rds-gen.h"
  28. #include "vivid-vbi-gen.h"
  29. #define dprintk(dev, level, fmt, arg...) \
  30. v4l2_dbg(level, vivid_debug, &dev->v4l2_dev, fmt, ## arg)
  31. /* Maximum allowed frame rate
  32. *
  33. * vivid will allow setting timeperframe in [1/FPS_MAX - FPS_MAX/1] range.
  34. *
  35. * Ideally FPS_MAX should be infinity, i.e. practically UINT_MAX, but that
  36. * might hit application errors when they manipulate these values.
  37. *
  38. * Besides, for tpf < 10ms image-generation logic should be changed, to avoid
  39. * producing frames with equal content.
  40. */
  41. #define FPS_MAX 100
  42. /* The maximum number of clip rectangles */
  43. #define MAX_CLIPS 16
  44. /* The maximum number of inputs */
  45. #define MAX_INPUTS 16
  46. /* The maximum number of outputs */
  47. #define MAX_OUTPUTS 16
  48. /* The maximum up or down scaling factor is 4 */
  49. #define MAX_ZOOM 4
  50. /* The maximum image width/height are set to 4K DMT */
  51. #define MAX_WIDTH 4096
  52. #define MAX_HEIGHT 2160
  53. /* The minimum image width/height */
  54. #define MIN_WIDTH 16
  55. #define MIN_HEIGHT 16
  56. /* The data_offset of plane 0 for the multiplanar formats */
  57. #define PLANE0_DATA_OFFSET 128
  58. /* The supported TV frequency range in MHz */
  59. #define MIN_TV_FREQ (44U * 16U)
  60. #define MAX_TV_FREQ (958U * 16U)
  61. /* The number of samples returned in every SDR buffer */
  62. #define SDR_CAP_SAMPLES_PER_BUF 0x4000
  63. /* used by the threads to know when to resync internal counters */
  64. #define JIFFIES_PER_DAY (3600U * 24U * HZ)
  65. #define JIFFIES_RESYNC (JIFFIES_PER_DAY * (0xf0000000U / JIFFIES_PER_DAY))
  66. extern const struct v4l2_rect vivid_min_rect;
  67. extern const struct v4l2_rect vivid_max_rect;
  68. extern unsigned vivid_debug;
  69. struct vivid_fmt {
  70. u32 fourcc; /* v4l2 format id */
  71. bool is_yuv;
  72. bool can_do_overlay;
  73. u8 vdownsampling[TPG_MAX_PLANES];
  74. u32 alpha_mask;
  75. u8 planes;
  76. u8 buffers;
  77. u32 data_offset[TPG_MAX_PLANES];
  78. u32 bit_depth[TPG_MAX_PLANES];
  79. };
  80. extern struct vivid_fmt vivid_formats[];
  81. /* buffer for one video frame */
  82. struct vivid_buffer {
  83. /* common v4l buffer stuff -- must be first */
  84. struct vb2_buffer vb;
  85. struct list_head list;
  86. };
  87. enum vivid_input {
  88. WEBCAM,
  89. TV,
  90. SVID,
  91. HDMI,
  92. };
  93. enum vivid_signal_mode {
  94. CURRENT_DV_TIMINGS,
  95. CURRENT_STD = CURRENT_DV_TIMINGS,
  96. NO_SIGNAL,
  97. NO_LOCK,
  98. OUT_OF_RANGE,
  99. SELECTED_DV_TIMINGS,
  100. SELECTED_STD = SELECTED_DV_TIMINGS,
  101. CYCLE_DV_TIMINGS,
  102. CYCLE_STD = CYCLE_DV_TIMINGS,
  103. CUSTOM_DV_TIMINGS,
  104. };
  105. enum vivid_colorspace {
  106. VIVID_CS_170M,
  107. VIVID_CS_709,
  108. VIVID_CS_SRGB,
  109. VIVID_CS_ADOBERGB,
  110. VIVID_CS_2020,
  111. VIVID_CS_240M,
  112. VIVID_CS_SYS_M,
  113. VIVID_CS_SYS_BG,
  114. };
  115. #define VIVID_INVALID_SIGNAL(mode) \
  116. ((mode) == NO_SIGNAL || (mode) == NO_LOCK || (mode) == OUT_OF_RANGE)
  117. struct vivid_dev {
  118. unsigned inst;
  119. struct v4l2_device v4l2_dev;
  120. struct v4l2_ctrl_handler ctrl_hdl_user_gen;
  121. struct v4l2_ctrl_handler ctrl_hdl_user_vid;
  122. struct v4l2_ctrl_handler ctrl_hdl_user_aud;
  123. struct v4l2_ctrl_handler ctrl_hdl_streaming;
  124. struct v4l2_ctrl_handler ctrl_hdl_sdtv_cap;
  125. struct v4l2_ctrl_handler ctrl_hdl_loop_cap;
  126. struct video_device vid_cap_dev;
  127. struct v4l2_ctrl_handler ctrl_hdl_vid_cap;
  128. struct video_device vid_out_dev;
  129. struct v4l2_ctrl_handler ctrl_hdl_vid_out;
  130. struct video_device vbi_cap_dev;
  131. struct v4l2_ctrl_handler ctrl_hdl_vbi_cap;
  132. struct video_device vbi_out_dev;
  133. struct v4l2_ctrl_handler ctrl_hdl_vbi_out;
  134. struct video_device radio_rx_dev;
  135. struct v4l2_ctrl_handler ctrl_hdl_radio_rx;
  136. struct video_device radio_tx_dev;
  137. struct v4l2_ctrl_handler ctrl_hdl_radio_tx;
  138. struct video_device sdr_cap_dev;
  139. struct v4l2_ctrl_handler ctrl_hdl_sdr_cap;
  140. spinlock_t slock;
  141. struct mutex mutex;
  142. /* capabilities */
  143. u32 vid_cap_caps;
  144. u32 vid_out_caps;
  145. u32 vbi_cap_caps;
  146. u32 vbi_out_caps;
  147. u32 sdr_cap_caps;
  148. u32 radio_rx_caps;
  149. u32 radio_tx_caps;
  150. /* supported features */
  151. bool multiplanar;
  152. unsigned num_inputs;
  153. u8 input_type[MAX_INPUTS];
  154. u8 input_name_counter[MAX_INPUTS];
  155. unsigned num_outputs;
  156. u8 output_type[MAX_OUTPUTS];
  157. u8 output_name_counter[MAX_OUTPUTS];
  158. bool has_audio_inputs;
  159. bool has_audio_outputs;
  160. bool has_vid_cap;
  161. bool has_vid_out;
  162. bool has_vbi_cap;
  163. bool has_raw_vbi_cap;
  164. bool has_sliced_vbi_cap;
  165. bool has_vbi_out;
  166. bool has_raw_vbi_out;
  167. bool has_sliced_vbi_out;
  168. bool has_radio_rx;
  169. bool has_radio_tx;
  170. bool has_sdr_cap;
  171. bool has_fb;
  172. bool can_loop_video;
  173. /* controls */
  174. struct v4l2_ctrl *brightness;
  175. struct v4l2_ctrl *contrast;
  176. struct v4l2_ctrl *saturation;
  177. struct v4l2_ctrl *hue;
  178. struct {
  179. /* autogain/gain cluster */
  180. struct v4l2_ctrl *autogain;
  181. struct v4l2_ctrl *gain;
  182. };
  183. struct v4l2_ctrl *volume;
  184. struct v4l2_ctrl *mute;
  185. struct v4l2_ctrl *alpha;
  186. struct v4l2_ctrl *button;
  187. struct v4l2_ctrl *boolean;
  188. struct v4l2_ctrl *int32;
  189. struct v4l2_ctrl *int64;
  190. struct v4l2_ctrl *menu;
  191. struct v4l2_ctrl *string;
  192. struct v4l2_ctrl *bitmask;
  193. struct v4l2_ctrl *int_menu;
  194. struct v4l2_ctrl *test_pattern;
  195. struct v4l2_ctrl *colorspace;
  196. struct v4l2_ctrl *rgb_range_cap;
  197. struct v4l2_ctrl *real_rgb_range_cap;
  198. struct {
  199. /* std_signal_mode/standard cluster */
  200. struct v4l2_ctrl *ctrl_std_signal_mode;
  201. struct v4l2_ctrl *ctrl_standard;
  202. };
  203. struct {
  204. /* dv_timings_signal_mode/timings cluster */
  205. struct v4l2_ctrl *ctrl_dv_timings_signal_mode;
  206. struct v4l2_ctrl *ctrl_dv_timings;
  207. };
  208. struct v4l2_ctrl *ctrl_has_crop_cap;
  209. struct v4l2_ctrl *ctrl_has_compose_cap;
  210. struct v4l2_ctrl *ctrl_has_scaler_cap;
  211. struct v4l2_ctrl *ctrl_has_crop_out;
  212. struct v4l2_ctrl *ctrl_has_compose_out;
  213. struct v4l2_ctrl *ctrl_has_scaler_out;
  214. struct v4l2_ctrl *ctrl_tx_mode;
  215. struct v4l2_ctrl *ctrl_tx_rgb_range;
  216. struct v4l2_ctrl *radio_tx_rds_pi;
  217. struct v4l2_ctrl *radio_tx_rds_pty;
  218. struct v4l2_ctrl *radio_tx_rds_mono_stereo;
  219. struct v4l2_ctrl *radio_tx_rds_art_head;
  220. struct v4l2_ctrl *radio_tx_rds_compressed;
  221. struct v4l2_ctrl *radio_tx_rds_dyn_pty;
  222. struct v4l2_ctrl *radio_tx_rds_ta;
  223. struct v4l2_ctrl *radio_tx_rds_tp;
  224. struct v4l2_ctrl *radio_tx_rds_ms;
  225. struct v4l2_ctrl *radio_tx_rds_psname;
  226. struct v4l2_ctrl *radio_tx_rds_radiotext;
  227. struct v4l2_ctrl *radio_rx_rds_pty;
  228. struct v4l2_ctrl *radio_rx_rds_ta;
  229. struct v4l2_ctrl *radio_rx_rds_tp;
  230. struct v4l2_ctrl *radio_rx_rds_ms;
  231. struct v4l2_ctrl *radio_rx_rds_psname;
  232. struct v4l2_ctrl *radio_rx_rds_radiotext;
  233. unsigned input_brightness[MAX_INPUTS];
  234. unsigned osd_mode;
  235. unsigned button_pressed;
  236. bool sensor_hflip;
  237. bool sensor_vflip;
  238. bool hflip;
  239. bool vflip;
  240. bool vbi_cap_interlaced;
  241. bool loop_video;
  242. /* Framebuffer */
  243. unsigned long video_pbase;
  244. void *video_vbase;
  245. u32 video_buffer_size;
  246. int display_width;
  247. int display_height;
  248. int display_byte_stride;
  249. int bits_per_pixel;
  250. int bytes_per_pixel;
  251. struct fb_info fb_info;
  252. struct fb_var_screeninfo fb_defined;
  253. struct fb_fix_screeninfo fb_fix;
  254. /* Error injection */
  255. bool queue_setup_error;
  256. bool buf_prepare_error;
  257. bool start_streaming_error;
  258. bool dqbuf_error;
  259. bool seq_wrap;
  260. bool time_wrap;
  261. __kernel_time_t time_wrap_offset;
  262. unsigned perc_dropped_buffers;
  263. enum vivid_signal_mode std_signal_mode;
  264. unsigned query_std_last;
  265. v4l2_std_id query_std;
  266. enum tpg_video_aspect std_aspect_ratio;
  267. enum vivid_signal_mode dv_timings_signal_mode;
  268. char **query_dv_timings_qmenu;
  269. unsigned query_dv_timings_size;
  270. unsigned query_dv_timings_last;
  271. unsigned query_dv_timings;
  272. enum tpg_video_aspect dv_timings_aspect_ratio;
  273. /* Input */
  274. unsigned input;
  275. v4l2_std_id std_cap;
  276. struct v4l2_dv_timings dv_timings_cap;
  277. u32 service_set_cap;
  278. struct vivid_vbi_gen_data vbi_gen;
  279. u8 *edid;
  280. unsigned edid_blocks;
  281. unsigned edid_max_blocks;
  282. unsigned webcam_size_idx;
  283. unsigned webcam_ival_idx;
  284. unsigned tv_freq;
  285. unsigned tv_audmode;
  286. unsigned tv_field_cap;
  287. unsigned tv_audio_input;
  288. /* Capture Overlay */
  289. struct v4l2_framebuffer fb_cap;
  290. struct v4l2_fh *overlay_cap_owner;
  291. void *fb_vbase_cap;
  292. int overlay_cap_top, overlay_cap_left;
  293. enum v4l2_field overlay_cap_field;
  294. void *bitmap_cap;
  295. struct v4l2_clip clips_cap[MAX_CLIPS];
  296. struct v4l2_clip try_clips_cap[MAX_CLIPS];
  297. unsigned clipcount_cap;
  298. /* Output */
  299. unsigned output;
  300. v4l2_std_id std_out;
  301. struct v4l2_dv_timings dv_timings_out;
  302. u32 colorspace_out;
  303. u32 ycbcr_enc_out;
  304. u32 quantization_out;
  305. u32 xfer_func_out;
  306. u32 service_set_out;
  307. unsigned bytesperline_out[TPG_MAX_PLANES];
  308. unsigned tv_field_out;
  309. unsigned tv_audio_output;
  310. bool vbi_out_have_wss;
  311. u8 vbi_out_wss[2];
  312. bool vbi_out_have_cc[2];
  313. u8 vbi_out_cc[2][2];
  314. bool dvi_d_out;
  315. u8 *scaled_line;
  316. u8 *blended_line;
  317. unsigned cur_scaled_line;
  318. /* Output Overlay */
  319. void *fb_vbase_out;
  320. bool overlay_out_enabled;
  321. int overlay_out_top, overlay_out_left;
  322. void *bitmap_out;
  323. struct v4l2_clip clips_out[MAX_CLIPS];
  324. struct v4l2_clip try_clips_out[MAX_CLIPS];
  325. unsigned clipcount_out;
  326. unsigned fbuf_out_flags;
  327. u32 chromakey_out;
  328. u8 global_alpha_out;
  329. /* video capture */
  330. struct tpg_data tpg;
  331. unsigned ms_vid_cap;
  332. bool must_blank[VIDEO_MAX_FRAME];
  333. const struct vivid_fmt *fmt_cap;
  334. struct v4l2_fract timeperframe_vid_cap;
  335. enum v4l2_field field_cap;
  336. struct v4l2_rect src_rect;
  337. struct v4l2_rect fmt_cap_rect;
  338. struct v4l2_rect crop_cap;
  339. struct v4l2_rect compose_cap;
  340. struct v4l2_rect crop_bounds_cap;
  341. struct vb2_queue vb_vid_cap_q;
  342. struct list_head vid_cap_active;
  343. struct vb2_queue vb_vbi_cap_q;
  344. struct list_head vbi_cap_active;
  345. /* thread for generating video capture stream */
  346. struct task_struct *kthread_vid_cap;
  347. unsigned long jiffies_vid_cap;
  348. u32 cap_seq_offset;
  349. u32 cap_seq_count;
  350. bool cap_seq_resync;
  351. u32 vid_cap_seq_start;
  352. u32 vid_cap_seq_count;
  353. bool vid_cap_streaming;
  354. u32 vbi_cap_seq_start;
  355. u32 vbi_cap_seq_count;
  356. bool vbi_cap_streaming;
  357. bool stream_sliced_vbi_cap;
  358. /* video output */
  359. const struct vivid_fmt *fmt_out;
  360. struct v4l2_fract timeperframe_vid_out;
  361. enum v4l2_field field_out;
  362. struct v4l2_rect sink_rect;
  363. struct v4l2_rect fmt_out_rect;
  364. struct v4l2_rect crop_out;
  365. struct v4l2_rect compose_out;
  366. struct v4l2_rect compose_bounds_out;
  367. struct vb2_queue vb_vid_out_q;
  368. struct list_head vid_out_active;
  369. struct vb2_queue vb_vbi_out_q;
  370. struct list_head vbi_out_active;
  371. /* video loop precalculated rectangles */
  372. /*
  373. * Intersection between what the output side composes and the capture side
  374. * crops. I.e., what actually needs to be copied from the output buffer to
  375. * the capture buffer.
  376. */
  377. struct v4l2_rect loop_vid_copy;
  378. /* The part of the output buffer that (after scaling) corresponds to loop_vid_copy. */
  379. struct v4l2_rect loop_vid_out;
  380. /* The part of the capture buffer that (after scaling) corresponds to loop_vid_copy. */
  381. struct v4l2_rect loop_vid_cap;
  382. /*
  383. * The intersection of the framebuffer, the overlay output window and
  384. * loop_vid_copy. I.e., the part of the framebuffer that actually should be
  385. * blended with the compose_out rectangle. This uses the framebuffer origin.
  386. */
  387. struct v4l2_rect loop_fb_copy;
  388. /* The same as loop_fb_copy but with compose_out origin. */
  389. struct v4l2_rect loop_vid_overlay;
  390. /*
  391. * The part of the capture buffer that (after scaling) corresponds
  392. * to loop_vid_overlay.
  393. */
  394. struct v4l2_rect loop_vid_overlay_cap;
  395. /* thread for generating video output stream */
  396. struct task_struct *kthread_vid_out;
  397. unsigned long jiffies_vid_out;
  398. u32 out_seq_offset;
  399. u32 out_seq_count;
  400. bool out_seq_resync;
  401. u32 vid_out_seq_start;
  402. u32 vid_out_seq_count;
  403. bool vid_out_streaming;
  404. u32 vbi_out_seq_start;
  405. u32 vbi_out_seq_count;
  406. bool vbi_out_streaming;
  407. bool stream_sliced_vbi_out;
  408. /* SDR capture */
  409. struct vb2_queue vb_sdr_cap_q;
  410. struct list_head sdr_cap_active;
  411. u32 sdr_pixelformat; /* v4l2 format id */
  412. unsigned sdr_buffersize;
  413. unsigned sdr_adc_freq;
  414. unsigned sdr_fm_freq;
  415. int sdr_fixp_src_phase;
  416. int sdr_fixp_mod_phase;
  417. bool tstamp_src_is_soe;
  418. bool has_crop_cap;
  419. bool has_compose_cap;
  420. bool has_scaler_cap;
  421. bool has_crop_out;
  422. bool has_compose_out;
  423. bool has_scaler_out;
  424. /* thread for generating SDR stream */
  425. struct task_struct *kthread_sdr_cap;
  426. unsigned long jiffies_sdr_cap;
  427. u32 sdr_cap_seq_offset;
  428. u32 sdr_cap_seq_count;
  429. bool sdr_cap_seq_resync;
  430. /* RDS generator */
  431. struct vivid_rds_gen rds_gen;
  432. /* Radio receiver */
  433. unsigned radio_rx_freq;
  434. unsigned radio_rx_audmode;
  435. int radio_rx_sig_qual;
  436. unsigned radio_rx_hw_seek_mode;
  437. bool radio_rx_hw_seek_prog_lim;
  438. bool radio_rx_rds_controls;
  439. bool radio_rx_rds_enabled;
  440. unsigned radio_rx_rds_use_alternates;
  441. unsigned radio_rx_rds_last_block;
  442. struct v4l2_fh *radio_rx_rds_owner;
  443. /* Radio transmitter */
  444. unsigned radio_tx_freq;
  445. unsigned radio_tx_subchans;
  446. bool radio_tx_rds_controls;
  447. unsigned radio_tx_rds_last_block;
  448. struct v4l2_fh *radio_tx_rds_owner;
  449. /* Shared between radio receiver and transmitter */
  450. bool radio_rds_loop;
  451. struct timespec radio_rds_init_ts;
  452. };
  453. static inline bool vivid_is_webcam(const struct vivid_dev *dev)
  454. {
  455. return dev->input_type[dev->input] == WEBCAM;
  456. }
  457. static inline bool vivid_is_tv_cap(const struct vivid_dev *dev)
  458. {
  459. return dev->input_type[dev->input] == TV;
  460. }
  461. static inline bool vivid_is_svid_cap(const struct vivid_dev *dev)
  462. {
  463. return dev->input_type[dev->input] == SVID;
  464. }
  465. static inline bool vivid_is_hdmi_cap(const struct vivid_dev *dev)
  466. {
  467. return dev->input_type[dev->input] == HDMI;
  468. }
  469. static inline bool vivid_is_sdtv_cap(const struct vivid_dev *dev)
  470. {
  471. return vivid_is_tv_cap(dev) || vivid_is_svid_cap(dev);
  472. }
  473. static inline bool vivid_is_svid_out(const struct vivid_dev *dev)
  474. {
  475. return dev->output_type[dev->output] == SVID;
  476. }
  477. static inline bool vivid_is_hdmi_out(const struct vivid_dev *dev)
  478. {
  479. return dev->output_type[dev->output] == HDMI;
  480. }
  481. #endif