vivid-core.h 14 KB

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