video4linux--2

Video standards
當然世界上現在有多個視頻標準,如NTSC和PAL,他們又細分爲好多種,那麼我們的設備輸入/輸出究竟支持什麼樣的標準呢?我們的當前在使用的輸入和輸出正在使用的是哪個標準呢?我們怎麼設置我們的某個輸入輸出使用的標準呢?這都是有方法的。
 
(1)查詢,我們的輸入支持什麼標準,首先就得找到當前的這個輸入的index,然後查出它的屬性,在其屬性裏面可以得到該輸入所支持的標準, 將它所支持的各個標準與所有的標準的信息進行比較,就可以獲知所支持的各個標準的屬性。一個輸入所支持的標準應該是一個集合,而這個集合是用bit與的方 式使用一個64位數字表示的。因此我們所查到的是一個數字。
  1. Example 1-5. Information about the current video standard
  2. v4l2_std_id std_id; //這個就是個64bit得數
  3. struct v4l2_standard standard;
  4. //VIDIOC_G_STD就是獲得當前輸入使用的standard,不過這裏只是得到了該標準的id即flag,還沒有得到其具體的屬性信息,具體的屬性信息要通過列舉操作來得到。
  5. if (-1 == ioctl (fd, VIDIOC_G_STD, &std_id)) { //獲得了當前輸入使用的standard
  6.         /* Note when VIDIOC_ENUMSTD always returns EINVAL this
  7.            is no video device or it falls under the USB exception,
  8.            and VIDIOC_G_STD returning EINVAL is no error. */
  9.         perror ("VIDIOC_G_STD");
  10.         exit (EXIT_FAILURE);
  11. }
  1. memset (&standard, 0, sizeof (standard));
  2. standard.index = 0; //從第一個開始列舉
  3. //VIDIOC_ENUMSTD用來列舉所支持的所有的video標準的信息,不過要先給standard結構的index域制定一個數值,所列舉的標準的信息屬性包含在standard裏面,如果我們所列舉的標準和std_id有共同的bit,那麼就意味着這個標準就是當前輸入所使用的標準,這樣我們就得到了當前輸入使用的標準的屬性信息
  4. while (0 == ioctl (fd, VIDIOC_ENUMSTD, &standard)) {
  5.         if (standard.id & std_id) {
  6.                printf ("Current video standard: %s/n", standard.name);
  7.                exit (EXIT_SUCCESS);
  8.         }
  9.         standard.index++;
  10. }
  11. /* EINVAL indicates the end of the enumeration, which cannot be
  12.    empty unless this device falls under the USB exception. */
  13. if (errno == EINVAL || standard.index == 0) {
  14.         perror ("VIDIOC_ENUMSTD");
  15.         exit (EXIT_FAILURE);
  16. }
  17.  
  18. Example 1-6. Listing the video standards supported by the current input
  19. struct v4l2_input input;
  20. struct v4l2_standard standard;
  21. memset (&input, 0, sizeof (input));
  22. //首先獲得當前輸入的index,注意只是index,要獲得具體的信息,就的調用列舉操作
  23. if (-1 == ioctl (fd, VIDIOC_G_INPUT, &input.index)) {
  24.         perror ("VIDIOC_G_INPUT");
  25.         exit (EXIT_FAILURE);
  26. }
  27. //調用列舉操作,獲得input.index對應的輸入的具體信息
  28. if (-1 == ioctl (fd, VIDIOC_ENUMINPUT, &input)) {
  29.         perror ("VIDIOC_ENUM_INPUT");
  30.         exit (EXIT_FAILURE);
  31. }
  32. printf ("Current input %s supports:/n", input.name);
  33. memset (&standard, 0, sizeof (standard));
  34. standard.index = 0;
  35. //列舉所有的所支持的standard,如果standard.id與當前input的input.std有共同的bit flag,意味着當前的輸入支持這個standard,這樣將所有驅動所支持的standard列舉一個遍,就可以找到該輸入所支持的所有standard了。
  36. while (0 == ioctl (fd, VIDIOC_ENUMSTD, &standard)) {
  37.         if (standard.id & input.std)
  38.                 printf ("%s/n", standard.name);
  39.         standard.index++;
  40. }
  41. /* EINVAL indicates the end of the enumeration, which cannot be
  42.    empty unless this device falls under the USB exception. */
  43. if (errno != EINVAL || standard.index == 0) {
  44.         perror ("VIDIOC_ENUMSTD");
  45.         exit (EXIT_FAILURE);
  46. }
  47. Example 1-7. Selecting a new video standard
  48. struct v4l2_input input;
  49. v4l2_std_id std_id;
  50. memset (&input, 0, sizeof (input));
  51. //獲得當前input的index
  52. if (-1 == ioctl (fd, VIDIOC_G_INPUT, &input.index)) {
  53.         perror ("VIDIOC_G_INPUT");
  54.         exit (EXIT_FAILURE);
  55. }
  56. //列舉出下標爲input.index的input的屬性到input裏
  57. if (-1 == ioctl (fd, VIDIOC_ENUMINPUT, &input)) {
  58.         perror ("VIDIOC_ENUM_INPUT");
  59.         exit (EXIT_FAILURE);
  60. }
  61. //如果該input所支持的標準裏不包含V4L2_STD_PAL_BG,就退出
  62. if (0 == (input.std & V4L2_STD_PAL_BG)) {
  63.         fprintf (stderr, "Oops. B/G PAL is not supported./n");
  64.         exit (EXIT_FAILURE);
  65. }
  66. /* Note this is also supposed to work when only B
  67.    or G/PAL is supported. */
  68. std_id = V4L2_STD_PAL_BG;
  69. //如果當前input支持V4L2_STD_PAL_BG,就將其設置爲V4L2_STD_PAL_BG
  70. if (-1 == ioctl (fd, VIDIOC_S_STD, &std_id)) {
  71.         perror ("VIDIOC_S_STD");
  72.         exit (EXIT_FAILURE);

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章