vivid-core.h 14 KB

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