common.xml 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. <title>Common API Elements</title>
  2. <para>Programming a V4L2 device consists of these
  3. steps:</para>
  4. <itemizedlist>
  5. <listitem>
  6. <para>Opening the device</para>
  7. </listitem>
  8. <listitem>
  9. <para>Changing device properties, selecting a video and audio
  10. input, video standard, picture brightness a.&nbsp;o.</para>
  11. </listitem>
  12. <listitem>
  13. <para>Negotiating a data format</para>
  14. </listitem>
  15. <listitem>
  16. <para>Negotiating an input/output method</para>
  17. </listitem>
  18. <listitem>
  19. <para>The actual input/output loop</para>
  20. </listitem>
  21. <listitem>
  22. <para>Closing the device</para>
  23. </listitem>
  24. </itemizedlist>
  25. <para>In practice most steps are optional and can be executed out of
  26. order. It depends on the V4L2 device type, you can read about the
  27. details in <xref linkend="devices" />. In this chapter we will discuss
  28. the basic concepts applicable to all devices.</para>
  29. <section id="open">
  30. <title>Opening and Closing Devices</title>
  31. <section>
  32. <title>Device Naming</title>
  33. <para>V4L2 drivers are implemented as kernel modules, loaded
  34. manually by the system administrator or automatically when a device is
  35. first opened. The driver modules plug into the "videodev" kernel
  36. module. It provides helper functions and a common application
  37. interface specified in this document.</para>
  38. <para>Each driver thus loaded registers one or more device nodes
  39. with major number 81 and a minor number between 0 and 255. Assigning
  40. minor numbers to V4L2 devices is entirely up to the system administrator,
  41. this is primarily intended to solve conflicts between devices.<footnote>
  42. <para>Access permissions are associated with character
  43. device special files, hence we must ensure device numbers cannot
  44. change with the module load order. To this end minor numbers are no
  45. longer automatically assigned by the "videodev" module as in V4L but
  46. requested by the driver. The defaults will suffice for most people
  47. unless two drivers compete for the same minor numbers.</para>
  48. </footnote> The module options to select minor numbers are named
  49. after the device special file with a "_nr" suffix. For example "video_nr"
  50. for <filename>/dev/video</filename> video capture devices. The number is
  51. an offset to the base minor number associated with the device type.
  52. <footnote>
  53. <para>In earlier versions of the V4L2 API the module options
  54. where named after the device special file with a "unit_" prefix, expressing
  55. the minor number itself, not an offset. Rationale for this change is unknown.
  56. Lastly the naming and semantics are just a convention among driver writers,
  57. the point to note is that minor numbers are not supposed to be hardcoded
  58. into drivers.</para>
  59. </footnote> When the driver supports multiple devices of the same
  60. type more than one minor number can be assigned, separated by commas:
  61. <informalexample>
  62. <screen>
  63. &gt; insmod mydriver.o video_nr=0,1 radio_nr=0,1</screen>
  64. </informalexample></para>
  65. <para>In <filename>/etc/modules.conf</filename> this may be
  66. written as: <informalexample>
  67. <screen>
  68. alias char-major-81-0 mydriver
  69. alias char-major-81-1 mydriver
  70. alias char-major-81-64 mydriver <co id="alias" />
  71. options mydriver video_nr=0,1 radio_nr=0,1 <co id="options" />
  72. </screen>
  73. <calloutlist>
  74. <callout arearefs="alias">
  75. <para>When an application attempts to open a device
  76. special file with major number 81 and minor number 0, 1, or 64, load
  77. "mydriver" (and the "videodev" module it depends upon).</para>
  78. </callout>
  79. <callout arearefs="options">
  80. <para>Register the first two video capture devices with
  81. minor number 0 and 1 (base number is 0), the first two radio device
  82. with minor number 64 and 65 (base 64).</para>
  83. </callout>
  84. </calloutlist>
  85. </informalexample> When no minor number is given as module
  86. option the driver supplies a default. <xref linkend="devices" />
  87. recommends the base minor numbers to be used for the various device
  88. types. Obviously minor numbers must be unique. When the number is
  89. already in use the <emphasis>offending device</emphasis> will not be
  90. registered. <!-- Blessed by Linus Torvalds on
  91. linux-kernel@vger.kernel.org, 2002-11-20. --></para>
  92. <para>By convention system administrators create various
  93. character device special files with these major and minor numbers in
  94. the <filename>/dev</filename> directory. The names recommended for the
  95. different V4L2 device types are listed in <xref linkend="devices" />.
  96. </para>
  97. <para>The creation of character special files (with
  98. <application>mknod</application>) is a privileged operation and
  99. devices cannot be opened by major and minor number. That means
  100. applications cannot <emphasis>reliable</emphasis> scan for loaded or
  101. installed drivers. The user must enter a device name, or the
  102. application can try the conventional device names.</para>
  103. <para>Under the device filesystem (devfs) the minor number
  104. options are ignored. V4L2 drivers (or by proxy the "videodev" module)
  105. automatically create the required device files in the
  106. <filename>/dev/v4l</filename> directory using the conventional device
  107. names above.</para>
  108. </section>
  109. <section id="related">
  110. <title>Related Devices</title>
  111. <para>Devices can support several related functions. For example
  112. video capturing, video overlay and VBI capturing are related because
  113. these functions share, amongst other, the same video input and tuner
  114. frequency. V4L and earlier versions of V4L2 used the same device name
  115. and minor number for video capturing and overlay, but different ones
  116. for VBI. Experience showed this approach has several problems<footnote>
  117. <para>Given a device file name one cannot reliable find
  118. related devices. For once names are arbitrary and in a system with
  119. multiple devices, where only some support VBI capturing, a
  120. <filename>/dev/video2</filename> is not necessarily related to
  121. <filename>/dev/vbi2</filename>. The V4L
  122. <constant>VIDIOCGUNIT</constant> ioctl would require a search for a
  123. device file with a particular major and minor number.</para>
  124. </footnote>, and to make things worse the V4L videodev module
  125. used to prohibit multiple opens of a device.</para>
  126. <para>As a remedy the present version of the V4L2 API relaxed the
  127. concept of device types with specific names and minor numbers. For
  128. compatibility with old applications drivers must still register different
  129. minor numbers to assign a default function to the device. But if related
  130. functions are supported by the driver they must be available under all
  131. registered minor numbers. The desired function can be selected after
  132. opening the device as described in <xref linkend="devices" />.</para>
  133. <para>Imagine a driver supporting video capturing, video
  134. overlay, raw VBI capturing, and FM radio reception. It registers three
  135. devices with minor number 0, 64 and 224 (this numbering scheme is
  136. inherited from the V4L API). Regardless if
  137. <filename>/dev/video</filename> (81, 0) or
  138. <filename>/dev/vbi</filename> (81, 224) is opened the application can
  139. select any one of the video capturing, overlay or VBI capturing
  140. functions. Without programming (e.&nbsp;g. reading from the device
  141. with <application>dd</application> or <application>cat</application>)
  142. <filename>/dev/video</filename> captures video images, while
  143. <filename>/dev/vbi</filename> captures raw VBI data.
  144. <filename>/dev/radio</filename> (81, 64) is invariable a radio device,
  145. unrelated to the video functions. Being unrelated does not imply the
  146. devices can be used at the same time, however. The &func-open;
  147. function may very well return an &EBUSY;.</para>
  148. <para>Besides video input or output the hardware may also
  149. support audio sampling or playback. If so, these functions are
  150. implemented as OSS or ALSA PCM devices and eventually OSS or ALSA
  151. audio mixer. The V4L2 API makes no provisions yet to find these
  152. related devices. If you have an idea please write to the linux-media
  153. mailing list: &v4l-ml;.</para>
  154. </section>
  155. <section>
  156. <title>Multiple Opens</title>
  157. <para>In general, V4L2 devices can be opened more than once.
  158. When this is supported by the driver, users can for example start a
  159. "panel" application to change controls like brightness or audio
  160. volume, while another application captures video and audio. In other words, panel
  161. applications are comparable to an OSS or ALSA audio mixer application.
  162. When a device supports multiple functions like capturing and overlay
  163. <emphasis>simultaneously</emphasis>, multiple opens allow concurrent
  164. use of the device by forked processes or specialized applications.</para>
  165. <para>Multiple opens are optional, although drivers should
  166. permit at least concurrent accesses without data exchange, &ie; panel
  167. applications. This implies &func-open; can return an &EBUSY; when the
  168. device is already in use, as well as &func-ioctl; functions initiating
  169. data exchange (namely the &VIDIOC-S-FMT; ioctl), and the &func-read;
  170. and &func-write; functions.</para>
  171. <para>Mere opening a V4L2 device does not grant exclusive
  172. access.<footnote>
  173. <para>Drivers could recognize the
  174. <constant>O_EXCL</constant> open flag. Presently this is not required,
  175. so applications cannot know if it really works.</para>
  176. </footnote> Initiating data exchange however assigns the right
  177. to read or write the requested type of data, and to change related
  178. properties, to this file descriptor. Applications can request
  179. additional access privileges using the priority mechanism described in
  180. <xref linkend="app-pri" />.</para>
  181. </section>
  182. <section>
  183. <title>Shared Data Streams</title>
  184. <para>V4L2 drivers should not support multiple applications
  185. reading or writing the same data stream on a device by copying
  186. buffers, time multiplexing or similar means. This is better handled by
  187. a proxy application in user space. When the driver supports stream
  188. sharing anyway it must be implemented transparently. The V4L2 API does
  189. not specify how conflicts are solved. <!-- For example O_EXCL when the
  190. application does not want to be preempted, PROT_READ mmapped buffers
  191. which can be mapped twice, what happens when image formats do not
  192. match etc.--></para>
  193. </section>
  194. <section>
  195. <title>Functions</title>
  196. <para>To open and close V4L2 devices applications use the
  197. &func-open; and &func-close; function, respectively. Devices are
  198. programmed using the &func-ioctl; function as explained in the
  199. following sections.</para>
  200. </section>
  201. </section>
  202. <section id="querycap">
  203. <title>Querying Capabilities</title>
  204. <para>Because V4L2 covers a wide variety of devices not all
  205. aspects of the API are equally applicable to all types of devices.
  206. Furthermore devices of the same type have different capabilities and
  207. this specification permits the omission of a few complicated and less
  208. important parts of the API.</para>
  209. <para>The &VIDIOC-QUERYCAP; ioctl is available to check if the kernel
  210. device is compatible with this specification, and to query the <link
  211. linkend="devices">functions</link> and <link linkend="io">I/O
  212. methods</link> supported by the device.</para>
  213. <para>Starting with kernel version 3.1, VIDIOC-QUERYCAP will return the
  214. V4L2 API version used by the driver, with generally matches the Kernel version.
  215. There's no need of using &VIDIOC-QUERYCAP; to check if an specific ioctl is
  216. supported, the V4L2 core now returns ENOIOCTLCMD if a driver doesn't provide
  217. support for an ioctl.</para>
  218. <para>Other features can be queried
  219. by calling the respective ioctl, for example &VIDIOC-ENUMINPUT;
  220. to learn about the number, types and names of video connectors on the
  221. device. Although abstraction is a major objective of this API, the
  222. ioctl also allows driver specific applications to reliable identify
  223. the driver.</para>
  224. <para>All V4L2 drivers must support
  225. <constant>VIDIOC_QUERYCAP</constant>. Applications should always call
  226. this ioctl after opening the device.</para>
  227. </section>
  228. <section id="app-pri">
  229. <title>Application Priority</title>
  230. <para>When multiple applications share a device it may be
  231. desirable to assign them different priorities. Contrary to the
  232. traditional "rm -rf /" school of thought a video recording application
  233. could for example block other applications from changing video
  234. controls or switching the current TV channel. Another objective is to
  235. permit low priority applications working in background, which can be
  236. preempted by user controlled applications and automatically regain
  237. control of the device at a later time.</para>
  238. <para>Since these features cannot be implemented entirely in user
  239. space V4L2 defines the &VIDIOC-G-PRIORITY; and &VIDIOC-S-PRIORITY;
  240. ioctls to request and query the access priority associate with a file
  241. descriptor. Opening a device assigns a medium priority, compatible
  242. with earlier versions of V4L2 and drivers not supporting these ioctls.
  243. Applications requiring a different priority will usually call
  244. <constant>VIDIOC_S_PRIORITY</constant> after verifying the device with
  245. the &VIDIOC-QUERYCAP; ioctl.</para>
  246. <para>Ioctls changing driver properties, such as &VIDIOC-S-INPUT;,
  247. return an &EBUSY; after another application obtained higher priority.
  248. An event mechanism to notify applications about asynchronous property
  249. changes has been proposed but not added yet.</para>
  250. </section>
  251. <section id="video">
  252. <title>Video Inputs and Outputs</title>
  253. <para>Video inputs and outputs are physical connectors of a
  254. device. These can be for example RF connectors (antenna/cable), CVBS
  255. a.k.a. Composite Video, S-Video or RGB connectors. Only video and VBI
  256. capture devices have inputs, output devices have outputs, at least one
  257. each. Radio devices have no video inputs or outputs.</para>
  258. <para>To learn about the number and attributes of the
  259. available inputs and outputs applications can enumerate them with the
  260. &VIDIOC-ENUMINPUT; and &VIDIOC-ENUMOUTPUT; ioctl, respectively. The
  261. &v4l2-input; returned by the <constant>VIDIOC_ENUMINPUT</constant>
  262. ioctl also contains signal status information applicable when the
  263. current video input is queried.</para>
  264. <para>The &VIDIOC-G-INPUT; and &VIDIOC-G-OUTPUT; ioctl return the
  265. index of the current video input or output. To select a different
  266. input or output applications call the &VIDIOC-S-INPUT; and
  267. &VIDIOC-S-OUTPUT; ioctl. Drivers must implement all the input ioctls
  268. when the device has one or more inputs, all the output ioctls when the
  269. device has one or more outputs.</para>
  270. <!--
  271. <figure id=io-tree>
  272. <title>Input and output enumeration is the root of most device properties.</title>
  273. <mediaobject>
  274. <imageobject>
  275. <imagedata fileref="links.pdf" format="ps" />
  276. </imageobject>
  277. <imageobject>
  278. <imagedata fileref="links.gif" format="gif" />
  279. </imageobject>
  280. <textobject>
  281. <phrase>Links between various device property structures.</phrase>
  282. </textobject>
  283. </mediaobject>
  284. </figure>
  285. -->
  286. <example>
  287. <title>Information about the current video input</title>
  288. <programlisting>
  289. &v4l2-input; input;
  290. int index;
  291. if (-1 == ioctl (fd, &VIDIOC-G-INPUT;, &amp;index)) {
  292. perror ("VIDIOC_G_INPUT");
  293. exit (EXIT_FAILURE);
  294. }
  295. memset (&amp;input, 0, sizeof (input));
  296. input.index = index;
  297. if (-1 == ioctl (fd, &VIDIOC-ENUMINPUT;, &amp;input)) {
  298. perror ("VIDIOC_ENUMINPUT");
  299. exit (EXIT_FAILURE);
  300. }
  301. printf ("Current input: %s\n", input.name);
  302. </programlisting>
  303. </example>
  304. <example>
  305. <title>Switching to the first video input</title>
  306. <programlisting>
  307. int index;
  308. index = 0;
  309. if (-1 == ioctl (fd, &VIDIOC-S-INPUT;, &amp;index)) {
  310. perror ("VIDIOC_S_INPUT");
  311. exit (EXIT_FAILURE);
  312. }
  313. </programlisting>
  314. </example>
  315. </section>
  316. <section id="audio">
  317. <title>Audio Inputs and Outputs</title>
  318. <para>Audio inputs and outputs are physical connectors of a
  319. device. Video capture devices have inputs, output devices have
  320. outputs, zero or more each. Radio devices have no audio inputs or
  321. outputs. They have exactly one tuner which in fact
  322. <emphasis>is</emphasis> an audio source, but this API associates
  323. tuners with video inputs or outputs only, and radio devices have
  324. none of these.<footnote>
  325. <para>Actually &v4l2-audio; ought to have a
  326. <structfield>tuner</structfield> field like &v4l2-input;, not only
  327. making the API more consistent but also permitting radio devices with
  328. multiple tuners.</para>
  329. </footnote> A connector on a TV card to loop back the received
  330. audio signal to a sound card is not considered an audio output.</para>
  331. <para>Audio and video inputs and outputs are associated. Selecting
  332. a video source also selects an audio source. This is most evident when
  333. the video and audio source is a tuner. Further audio connectors can
  334. combine with more than one video input or output. Assumed two
  335. composite video inputs and two audio inputs exist, there may be up to
  336. four valid combinations. The relation of video and audio connectors
  337. is defined in the <structfield>audioset</structfield> field of the
  338. respective &v4l2-input; or &v4l2-output;, where each bit represents
  339. the index number, starting at zero, of one audio input or output.</para>
  340. <para>To learn about the number and attributes of the
  341. available inputs and outputs applications can enumerate them with the
  342. &VIDIOC-ENUMAUDIO; and &VIDIOC-ENUMAUDOUT; ioctl, respectively. The
  343. &v4l2-audio; returned by the <constant>VIDIOC_ENUMAUDIO</constant> ioctl
  344. also contains signal status information applicable when the current
  345. audio input is queried.</para>
  346. <para>The &VIDIOC-G-AUDIO; and &VIDIOC-G-AUDOUT; ioctl report
  347. the current audio input and output, respectively. Note that, unlike
  348. &VIDIOC-G-INPUT; and &VIDIOC-G-OUTPUT; these ioctls return a structure
  349. as <constant>VIDIOC_ENUMAUDIO</constant> and
  350. <constant>VIDIOC_ENUMAUDOUT</constant> do, not just an index.</para>
  351. <para>To select an audio input and change its properties
  352. applications call the &VIDIOC-S-AUDIO; ioctl. To select an audio
  353. output (which presently has no changeable properties) applications
  354. call the &VIDIOC-S-AUDOUT; ioctl.</para>
  355. <para>Drivers must implement all input ioctls when the device
  356. has one or more inputs, all output ioctls when the device has one
  357. or more outputs. When the device has any audio inputs or outputs the
  358. driver must set the <constant>V4L2_CAP_AUDIO</constant> flag in the
  359. &v4l2-capability; returned by the &VIDIOC-QUERYCAP; ioctl.</para>
  360. <example>
  361. <title>Information about the current audio input</title>
  362. <programlisting>
  363. &v4l2-audio; audio;
  364. memset (&amp;audio, 0, sizeof (audio));
  365. if (-1 == ioctl (fd, &VIDIOC-G-AUDIO;, &amp;audio)) {
  366. perror ("VIDIOC_G_AUDIO");
  367. exit (EXIT_FAILURE);
  368. }
  369. printf ("Current input: %s\n", audio.name);
  370. </programlisting>
  371. </example>
  372. <example>
  373. <title>Switching to the first audio input</title>
  374. <programlisting>
  375. &v4l2-audio; audio;
  376. memset (&amp;audio, 0, sizeof (audio)); /* clear audio.mode, audio.reserved */
  377. audio.index = 0;
  378. if (-1 == ioctl (fd, &VIDIOC-S-AUDIO;, &amp;audio)) {
  379. perror ("VIDIOC_S_AUDIO");
  380. exit (EXIT_FAILURE);
  381. }
  382. </programlisting>
  383. </example>
  384. </section>
  385. <section id="tuner">
  386. <title>Tuners and Modulators</title>
  387. <section>
  388. <title>Tuners</title>
  389. <para>Video input devices can have one or more tuners
  390. demodulating a RF signal. Each tuner is associated with one or more
  391. video inputs, depending on the number of RF connectors on the tuner.
  392. The <structfield>type</structfield> field of the respective
  393. &v4l2-input; returned by the &VIDIOC-ENUMINPUT; ioctl is set to
  394. <constant>V4L2_INPUT_TYPE_TUNER</constant> and its
  395. <structfield>tuner</structfield> field contains the index number of
  396. the tuner.</para>
  397. <para>Radio input devices have exactly one tuner with index zero, no
  398. video inputs.</para>
  399. <para>To query and change tuner properties applications use the
  400. &VIDIOC-G-TUNER; and &VIDIOC-S-TUNER; ioctl, respectively. The
  401. &v4l2-tuner; returned by <constant>VIDIOC_G_TUNER</constant> also
  402. contains signal status information applicable when the tuner of the
  403. current video or radio input is queried. Note that
  404. <constant>VIDIOC_S_TUNER</constant> does not switch the current tuner,
  405. when there is more than one at all. The tuner is solely determined by
  406. the current video input. Drivers must support both ioctls and set the
  407. <constant>V4L2_CAP_TUNER</constant> flag in the &v4l2-capability;
  408. returned by the &VIDIOC-QUERYCAP; ioctl when the device has one or
  409. more tuners.</para>
  410. </section>
  411. <section>
  412. <title>Modulators</title>
  413. <para>Video output devices can have one or more modulators, uh,
  414. modulating a video signal for radiation or connection to the antenna
  415. input of a TV set or video recorder. Each modulator is associated with
  416. one or more video outputs, depending on the number of RF connectors on
  417. the modulator. The <structfield>type</structfield> field of the
  418. respective &v4l2-output; returned by the &VIDIOC-ENUMOUTPUT; ioctl is
  419. set to <constant>V4L2_OUTPUT_TYPE_MODULATOR</constant> and its
  420. <structfield>modulator</structfield> field contains the index number
  421. of the modulator.</para>
  422. <para>Radio output devices have exactly one modulator with index
  423. zero, no video outputs.</para>
  424. <para>A video or radio device cannot support both a tuner and a
  425. modulator. Two separate device nodes will have to be used for such
  426. hardware, one that supports the tuner functionality and one that supports
  427. the modulator functionality. The reason is a limitation with the
  428. &VIDIOC-S-FREQUENCY; ioctl where you cannot specify whether the frequency
  429. is for a tuner or a modulator.</para>
  430. <para>To query and change modulator properties applications use
  431. the &VIDIOC-G-MODULATOR; and &VIDIOC-S-MODULATOR; ioctl. Note that
  432. <constant>VIDIOC_S_MODULATOR</constant> does not switch the current
  433. modulator, when there is more than one at all. The modulator is solely
  434. determined by the current video output. Drivers must support both
  435. ioctls and set the <constant>V4L2_CAP_MODULATOR</constant> flag in
  436. the &v4l2-capability; returned by the &VIDIOC-QUERYCAP; ioctl when the
  437. device has one or more modulators.</para>
  438. </section>
  439. <section>
  440. <title>Radio Frequency</title>
  441. <para>To get and set the tuner or modulator radio frequency
  442. applications use the &VIDIOC-G-FREQUENCY; and &VIDIOC-S-FREQUENCY;
  443. ioctl which both take a pointer to a &v4l2-frequency;. These ioctls
  444. are used for TV and radio devices alike. Drivers must support both
  445. ioctls when the tuner or modulator ioctls are supported, or
  446. when the device is a radio device.</para>
  447. </section>
  448. </section>
  449. <section id="standard">
  450. <title>Video Standards</title>
  451. <para>Video devices typically support one or more different video
  452. standards or variations of standards. Each video input and output may
  453. support another set of standards. This set is reported by the
  454. <structfield>std</structfield> field of &v4l2-input; and
  455. &v4l2-output; returned by the &VIDIOC-ENUMINPUT; and
  456. &VIDIOC-ENUMOUTPUT; ioctl, respectively.</para>
  457. <para>V4L2 defines one bit for each analog video standard
  458. currently in use worldwide, and sets aside bits for driver defined
  459. standards, &eg; hybrid standards to watch NTSC video tapes on PAL TVs
  460. and vice versa. Applications can use the predefined bits to select a
  461. particular standard, although presenting the user a menu of supported
  462. standards is preferred. To enumerate and query the attributes of the
  463. supported standards applications use the &VIDIOC-ENUMSTD; ioctl.</para>
  464. <para>Many of the defined standards are actually just variations
  465. of a few major standards. The hardware may in fact not distinguish
  466. between them, or do so internal and switch automatically. Therefore
  467. enumerated standards also contain sets of one or more standard
  468. bits.</para>
  469. <para>Assume a hypothetic tuner capable of demodulating B/PAL,
  470. G/PAL and I/PAL signals. The first enumerated standard is a set of B
  471. and G/PAL, switched automatically depending on the selected radio
  472. frequency in UHF or VHF band. Enumeration gives a "PAL-B/G" or "PAL-I"
  473. choice. Similar a Composite input may collapse standards, enumerating
  474. "PAL-B/G/H/I", "NTSC-M" and "SECAM-D/K".<footnote>
  475. <para>Some users are already confused by technical terms PAL,
  476. NTSC and SECAM. There is no point asking them to distinguish between
  477. B, G, D, or K when the software or hardware can do that
  478. automatically.</para>
  479. </footnote></para>
  480. <para>To query and select the standard used by the current video
  481. input or output applications call the &VIDIOC-G-STD; and
  482. &VIDIOC-S-STD; ioctl, respectively. The <emphasis>received</emphasis>
  483. standard can be sensed with the &VIDIOC-QUERYSTD; ioctl. Note parameter of all these ioctls is a pointer to a &v4l2-std-id; type (a standard set), <emphasis>not</emphasis> an index into the standard enumeration.<footnote>
  484. <para>An alternative to the current scheme is to use pointers
  485. to indices as arguments of <constant>VIDIOC_G_STD</constant> and
  486. <constant>VIDIOC_S_STD</constant>, the &v4l2-input; and
  487. &v4l2-output; <structfield>std</structfield> field would be a set of
  488. indices like <structfield>audioset</structfield>.</para>
  489. <para>Indices are consistent with the rest of the API
  490. and identify the standard unambiguously. In the present scheme of
  491. things an enumerated standard is looked up by &v4l2-std-id;. Now the
  492. standards supported by the inputs of a device can overlap. Just
  493. assume the tuner and composite input in the example above both
  494. exist on a device. An enumeration of "PAL-B/G", "PAL-H/I" suggests
  495. a choice which does not exist. We cannot merge or omit sets, because
  496. applications would be unable to find the standards reported by
  497. <constant>VIDIOC_G_STD</constant>. That leaves separate enumerations
  498. for each input. Also selecting a standard by &v4l2-std-id; can be
  499. ambiguous. Advantage of this method is that applications need not
  500. identify the standard indirectly, after enumerating.</para><para>So in
  501. summary, the lookup itself is unavoidable. The difference is only
  502. whether the lookup is necessary to find an enumerated standard or to
  503. switch to a standard by &v4l2-std-id;.</para>
  504. </footnote> Drivers must implement all video standard ioctls
  505. when the device has one or more video inputs or outputs.</para>
  506. <para>Special rules apply to USB cameras where the notion of video
  507. standards makes little sense. More generally any capture device,
  508. output devices accordingly, which is <itemizedlist>
  509. <listitem>
  510. <para>incapable of capturing fields or frames at the nominal
  511. rate of the video standard, or</para>
  512. </listitem>
  513. <listitem>
  514. <para>where <link linkend="buffer">timestamps</link> refer
  515. to the instant the field or frame was received by the driver, not the
  516. capture time, or</para>
  517. </listitem>
  518. <listitem>
  519. <para>where <link linkend="buffer">sequence numbers</link>
  520. refer to the frames received by the driver, not the captured
  521. frames.</para>
  522. </listitem>
  523. </itemizedlist> Here the driver shall set the
  524. <structfield>std</structfield> field of &v4l2-input; and &v4l2-output;
  525. to zero, the <constant>VIDIOC_G_STD</constant>,
  526. <constant>VIDIOC_S_STD</constant>,
  527. <constant>VIDIOC_QUERYSTD</constant> and
  528. <constant>VIDIOC_ENUMSTD</constant> ioctls shall return the
  529. &EINVAL;.<footnote>
  530. <para>See <xref linkend="buffer" /> for a rationale. Probably
  531. even USB cameras follow some well known video standard. It might have
  532. been better to explicitly indicate elsewhere if a device cannot live
  533. up to normal expectations, instead of this exception.</para>
  534. </footnote></para>
  535. <example>
  536. <title>Information about the current video standard</title>
  537. <programlisting>
  538. &v4l2-std-id; std_id;
  539. &v4l2-standard; standard;
  540. if (-1 == ioctl (fd, &VIDIOC-G-STD;, &amp;std_id)) {
  541. /* Note when VIDIOC_ENUMSTD always returns EINVAL this
  542. is no video device or it falls under the USB exception,
  543. and VIDIOC_G_STD returning EINVAL is no error. */
  544. perror ("VIDIOC_G_STD");
  545. exit (EXIT_FAILURE);
  546. }
  547. memset (&amp;standard, 0, sizeof (standard));
  548. standard.index = 0;
  549. while (0 == ioctl (fd, &VIDIOC-ENUMSTD;, &amp;standard)) {
  550. if (standard.id &amp; std_id) {
  551. printf ("Current video standard: %s\n", standard.name);
  552. exit (EXIT_SUCCESS);
  553. }
  554. standard.index++;
  555. }
  556. /* EINVAL indicates the end of the enumeration, which cannot be
  557. empty unless this device falls under the USB exception. */
  558. if (errno == EINVAL || standard.index == 0) {
  559. perror ("VIDIOC_ENUMSTD");
  560. exit (EXIT_FAILURE);
  561. }
  562. </programlisting>
  563. </example>
  564. <example>
  565. <title>Listing the video standards supported by the current
  566. input</title>
  567. <programlisting>
  568. &v4l2-input; input;
  569. &v4l2-standard; standard;
  570. memset (&amp;input, 0, sizeof (input));
  571. if (-1 == ioctl (fd, &VIDIOC-G-INPUT;, &amp;input.index)) {
  572. perror ("VIDIOC_G_INPUT");
  573. exit (EXIT_FAILURE);
  574. }
  575. if (-1 == ioctl (fd, &VIDIOC-ENUMINPUT;, &amp;input)) {
  576. perror ("VIDIOC_ENUM_INPUT");
  577. exit (EXIT_FAILURE);
  578. }
  579. printf ("Current input %s supports:\n", input.name);
  580. memset (&amp;standard, 0, sizeof (standard));
  581. standard.index = 0;
  582. while (0 == ioctl (fd, &VIDIOC-ENUMSTD;, &amp;standard)) {
  583. if (standard.id &amp; input.std)
  584. printf ("%s\n", standard.name);
  585. standard.index++;
  586. }
  587. /* EINVAL indicates the end of the enumeration, which cannot be
  588. empty unless this device falls under the USB exception. */
  589. if (errno != EINVAL || standard.index == 0) {
  590. perror ("VIDIOC_ENUMSTD");
  591. exit (EXIT_FAILURE);
  592. }
  593. </programlisting>
  594. </example>
  595. <example>
  596. <title>Selecting a new video standard</title>
  597. <programlisting>
  598. &v4l2-input; input;
  599. &v4l2-std-id; std_id;
  600. memset (&amp;input, 0, sizeof (input));
  601. if (-1 == ioctl (fd, &VIDIOC-G-INPUT;, &amp;input.index)) {
  602. perror ("VIDIOC_G_INPUT");
  603. exit (EXIT_FAILURE);
  604. }
  605. if (-1 == ioctl (fd, &VIDIOC-ENUMINPUT;, &amp;input)) {
  606. perror ("VIDIOC_ENUM_INPUT");
  607. exit (EXIT_FAILURE);
  608. }
  609. if (0 == (input.std &amp; V4L2_STD_PAL_BG)) {
  610. fprintf (stderr, "Oops. B/G PAL is not supported.\n");
  611. exit (EXIT_FAILURE);
  612. }
  613. /* Note this is also supposed to work when only B
  614. <emphasis>or</emphasis> G/PAL is supported. */
  615. std_id = V4L2_STD_PAL_BG;
  616. if (-1 == ioctl (fd, &VIDIOC-S-STD;, &amp;std_id)) {
  617. perror ("VIDIOC_S_STD");
  618. exit (EXIT_FAILURE);
  619. }
  620. </programlisting>
  621. </example>
  622. </section>
  623. <section id="dv-timings">
  624. <title>Digital Video (DV) Timings</title>
  625. <para>
  626. The video standards discussed so far have been dealing with Analog TV and the
  627. corresponding video timings. Today there are many more different hardware interfaces
  628. such as High Definition TV interfaces (HDMI), VGA, DVI connectors etc., that carry
  629. video signals and there is a need to extend the API to select the video timings
  630. for these interfaces. Since it is not possible to extend the &v4l2-std-id; due to
  631. the limited bits available, a new set of IOCTLs was added to set/get video timings at
  632. the input and output: </para><itemizedlist>
  633. <listitem>
  634. <para>DV Timings: This will allow applications to define detailed
  635. video timings for the interface. This includes parameters such as width, height,
  636. polarities, frontporch, backporch etc. The <filename>linux/v4l2-dv-timings.h</filename>
  637. header can be used to get the timings of the formats in the <xref linkend="cea861" /> and
  638. <xref linkend="vesadmt" /> standards.
  639. </para>
  640. </listitem>
  641. <listitem>
  642. <para>DV Presets: Digital Video (DV) presets (<emphasis role="bold">deprecated</emphasis>).
  643. These are IDs representing a
  644. video timing at the input/output. Presets are pre-defined timings implemented
  645. by the hardware according to video standards. A __u32 data type is used to represent
  646. a preset unlike the bit mask that is used in &v4l2-std-id; allowing future extensions
  647. to support as many different presets as needed. This API is deprecated in favor of the DV Timings
  648. API.</para>
  649. </listitem>
  650. </itemizedlist>
  651. <para>To enumerate and query the attributes of the DV timings supported by a device,
  652. applications use the &VIDIOC-ENUM-DV-TIMINGS; and &VIDIOC-DV-TIMINGS-CAP; ioctls.
  653. To set DV timings for the device, applications use the
  654. &VIDIOC-S-DV-TIMINGS; ioctl and to get current DV timings they use the
  655. &VIDIOC-G-DV-TIMINGS; ioctl. To detect the DV timings as seen by the video receiver applications
  656. use the &VIDIOC-QUERY-DV-TIMINGS; ioctl.</para>
  657. <para>To enumerate and query the attributes of DV presets supported by a device,
  658. applications use the &VIDIOC-ENUM-DV-PRESETS; ioctl. To get the current DV preset,
  659. applications use the &VIDIOC-G-DV-PRESET; ioctl and to set a preset they use the
  660. &VIDIOC-S-DV-PRESET; ioctl. To detect the preset as seen by the video receiver applications
  661. use the &VIDIOC-QUERY-DV-PRESET; ioctl.</para>
  662. <para>Applications can make use of the <xref linkend="input-capabilities" /> and
  663. <xref linkend="output-capabilities"/> flags to decide what ioctls are available to set the
  664. video timings for the device.</para>
  665. </section>
  666. &sub-controls;
  667. <section id="format">
  668. <title>Data Formats</title>
  669. <section>
  670. <title>Data Format Negotiation</title>
  671. <para>Different devices exchange different kinds of data with
  672. applications, for example video images, raw or sliced VBI data, RDS
  673. datagrams. Even within one kind many different formats are possible,
  674. in particular an abundance of image formats. Although drivers must
  675. provide a default and the selection persists across closing and
  676. reopening a device, applications should always negotiate a data format
  677. before engaging in data exchange. Negotiation means the application
  678. asks for a particular format and the driver selects and reports the
  679. best the hardware can do to satisfy the request. Of course
  680. applications can also just query the current selection.</para>
  681. <para>A single mechanism exists to negotiate all data formats
  682. using the aggregate &v4l2-format; and the &VIDIOC-G-FMT; and
  683. &VIDIOC-S-FMT; ioctls. Additionally the &VIDIOC-TRY-FMT; ioctl can be
  684. used to examine what the hardware <emphasis>could</emphasis> do,
  685. without actually selecting a new data format. The data formats
  686. supported by the V4L2 API are covered in the respective device section
  687. in <xref linkend="devices" />. For a closer look at image formats see
  688. <xref linkend="pixfmt" />.</para>
  689. <para>The <constant>VIDIOC_S_FMT</constant> ioctl is a major
  690. turning-point in the initialization sequence. Prior to this point
  691. multiple panel applications can access the same device concurrently to
  692. select the current input, change controls or modify other properties.
  693. The first <constant>VIDIOC_S_FMT</constant> assigns a logical stream
  694. (video data, VBI data etc.) exclusively to one file descriptor.</para>
  695. <para>Exclusive means no other application, more precisely no
  696. other file descriptor, can grab this stream or change device
  697. properties inconsistent with the negotiated parameters. A video
  698. standard change for example, when the new standard uses a different
  699. number of scan lines, can invalidate the selected image format.
  700. Therefore only the file descriptor owning the stream can make
  701. invalidating changes. Accordingly multiple file descriptors which
  702. grabbed different logical streams prevent each other from interfering
  703. with their settings. When for example video overlay is about to start
  704. or already in progress, simultaneous video capturing may be restricted
  705. to the same cropping and image size.</para>
  706. <para>When applications omit the
  707. <constant>VIDIOC_S_FMT</constant> ioctl its locking side effects are
  708. implied by the next step, the selection of an I/O method with the
  709. &VIDIOC-REQBUFS; ioctl or implicit with the first &func-read; or
  710. &func-write; call.</para>
  711. <para>Generally only one logical stream can be assigned to a
  712. file descriptor, the exception being drivers permitting simultaneous
  713. video capturing and overlay using the same file descriptor for
  714. compatibility with V4L and earlier versions of V4L2. Switching the
  715. logical stream or returning into "panel mode" is possible by closing
  716. and reopening the device. Drivers <emphasis>may</emphasis> support a
  717. switch using <constant>VIDIOC_S_FMT</constant>.</para>
  718. <para>All drivers exchanging data with
  719. applications must support the <constant>VIDIOC_G_FMT</constant> and
  720. <constant>VIDIOC_S_FMT</constant> ioctl. Implementation of the
  721. <constant>VIDIOC_TRY_FMT</constant> is highly recommended but
  722. optional.</para>
  723. </section>
  724. <section>
  725. <title>Image Format Enumeration</title>
  726. <para>Apart of the generic format negotiation functions
  727. a special ioctl to enumerate all image formats supported by video
  728. capture, overlay or output devices is available.<footnote>
  729. <para>Enumerating formats an application has no a-priori
  730. knowledge of (otherwise it could explicitly ask for them and need not
  731. enumerate) seems useless, but there are applications serving as proxy
  732. between drivers and the actual video applications for which this is
  733. useful.</para>
  734. </footnote></para>
  735. <para>The &VIDIOC-ENUM-FMT; ioctl must be supported
  736. by all drivers exchanging image data with applications.</para>
  737. <important>
  738. <para>Drivers are not supposed to convert image formats in
  739. kernel space. They must enumerate only formats directly supported by
  740. the hardware. If necessary driver writers should publish an example
  741. conversion routine or library for integration into applications.</para>
  742. </important>
  743. </section>
  744. </section>
  745. &sub-planar-apis;
  746. <section id="crop">
  747. <title>Image Cropping, Insertion and Scaling</title>
  748. <para>Some video capture devices can sample a subsection of the
  749. picture and shrink or enlarge it to an image of arbitrary size. We
  750. call these abilities cropping and scaling. Some video output devices
  751. can scale an image up or down and insert it at an arbitrary scan line
  752. and horizontal offset into a video signal.</para>
  753. <para>Applications can use the following API to select an area in
  754. the video signal, query the default area and the hardware limits.
  755. <emphasis>Despite their name, the &VIDIOC-CROPCAP;, &VIDIOC-G-CROP;
  756. and &VIDIOC-S-CROP; ioctls apply to input as well as output
  757. devices.</emphasis></para>
  758. <para>Scaling requires a source and a target. On a video capture
  759. or overlay device the source is the video signal, and the cropping
  760. ioctls determine the area actually sampled. The target are images
  761. read by the application or overlaid onto the graphics screen. Their
  762. size (and position for an overlay) is negotiated with the
  763. &VIDIOC-G-FMT; and &VIDIOC-S-FMT; ioctls.</para>
  764. <para>On a video output device the source are the images passed in
  765. by the application, and their size is again negotiated with the
  766. <constant>VIDIOC_G/S_FMT</constant> ioctls, or may be encoded in a
  767. compressed video stream. The target is the video signal, and the
  768. cropping ioctls determine the area where the images are
  769. inserted.</para>
  770. <para>Source and target rectangles are defined even if the device
  771. does not support scaling or the <constant>VIDIOC_G/S_CROP</constant>
  772. ioctls. Their size (and position where applicable) will be fixed in
  773. this case. <emphasis>All capture and output device must support the
  774. <constant>VIDIOC_CROPCAP</constant> ioctl such that applications can
  775. determine if scaling takes place.</emphasis></para>
  776. <section>
  777. <title>Cropping Structures</title>
  778. <figure id="crop-scale">
  779. <title>Image Cropping, Insertion and Scaling</title>
  780. <mediaobject>
  781. <imageobject>
  782. <imagedata fileref="crop.pdf" format="PS" />
  783. </imageobject>
  784. <imageobject>
  785. <imagedata fileref="crop.gif" format="GIF" />
  786. </imageobject>
  787. <textobject>
  788. <phrase>The cropping, insertion and scaling process</phrase>
  789. </textobject>
  790. </mediaobject>
  791. </figure>
  792. <para>For capture devices the coordinates of the top left
  793. corner, width and height of the area which can be sampled is given by
  794. the <structfield>bounds</structfield> substructure of the
  795. &v4l2-cropcap; returned by the <constant>VIDIOC_CROPCAP</constant>
  796. ioctl. To support a wide range of hardware this specification does not
  797. define an origin or units. However by convention drivers should
  798. horizontally count unscaled samples relative to 0H (the leading edge
  799. of the horizontal sync pulse, see <xref linkend="vbi-hsync" />).
  800. Vertically ITU-R line
  801. numbers of the first field (<xref linkend="vbi-525" />, <xref
  802. linkend="vbi-625" />), multiplied by two if the driver can capture both
  803. fields.</para>
  804. <para>The top left corner, width and height of the source
  805. rectangle, that is the area actually sampled, is given by &v4l2-crop;
  806. using the same coordinate system as &v4l2-cropcap;. Applications can
  807. use the <constant>VIDIOC_G_CROP</constant> and
  808. <constant>VIDIOC_S_CROP</constant> ioctls to get and set this
  809. rectangle. It must lie completely within the capture boundaries and
  810. the driver may further adjust the requested size and/or position
  811. according to hardware limitations.</para>
  812. <para>Each capture device has a default source rectangle, given
  813. by the <structfield>defrect</structfield> substructure of
  814. &v4l2-cropcap;. The center of this rectangle shall align with the
  815. center of the active picture area of the video signal, and cover what
  816. the driver writer considers the complete picture. Drivers shall reset
  817. the source rectangle to the default when the driver is first loaded,
  818. but not later.</para>
  819. <para>For output devices these structures and ioctls are used
  820. accordingly, defining the <emphasis>target</emphasis> rectangle where
  821. the images will be inserted into the video signal.</para>
  822. </section>
  823. <section>
  824. <title>Scaling Adjustments</title>
  825. <para>Video hardware can have various cropping, insertion and
  826. scaling limitations. It may only scale up or down, support only
  827. discrete scaling factors, or have different scaling abilities in
  828. horizontal and vertical direction. Also it may not support scaling at
  829. all. At the same time the &v4l2-crop; rectangle may have to be
  830. aligned, and both the source and target rectangles may have arbitrary
  831. upper and lower size limits. In particular the maximum
  832. <structfield>width</structfield> and <structfield>height</structfield>
  833. in &v4l2-crop; may be smaller than the
  834. &v4l2-cropcap;.<structfield>bounds</structfield> area. Therefore, as
  835. usual, drivers are expected to adjust the requested parameters and
  836. return the actual values selected.</para>
  837. <para>Applications can change the source or the target rectangle
  838. first, as they may prefer a particular image size or a certain area in
  839. the video signal. If the driver has to adjust both to satisfy hardware
  840. limitations, the last requested rectangle shall take priority, and the
  841. driver should preferably adjust the opposite one. The &VIDIOC-TRY-FMT;
  842. ioctl however shall not change the driver state and therefore only
  843. adjust the requested rectangle.</para>
  844. <para>Suppose scaling on a video capture device is restricted to
  845. a factor 1:1 or 2:1 in either direction and the target image size must
  846. be a multiple of 16&nbsp;&times;&nbsp;16 pixels. The source cropping
  847. rectangle is set to defaults, which are also the upper limit in this
  848. example, of 640&nbsp;&times;&nbsp;400 pixels at offset 0,&nbsp;0. An
  849. application requests an image size of 300&nbsp;&times;&nbsp;225
  850. pixels, assuming video will be scaled down from the "full picture"
  851. accordingly. The driver sets the image size to the closest possible
  852. values 304&nbsp;&times;&nbsp;224, then chooses the cropping rectangle
  853. closest to the requested size, that is 608&nbsp;&times;&nbsp;224
  854. (224&nbsp;&times;&nbsp;2:1 would exceed the limit 400). The offset
  855. 0,&nbsp;0 is still valid, thus unmodified. Given the default cropping
  856. rectangle reported by <constant>VIDIOC_CROPCAP</constant> the
  857. application can easily propose another offset to center the cropping
  858. rectangle.</para>
  859. <para>Now the application may insist on covering an area using a
  860. picture aspect ratio closer to the original request, so it asks for a
  861. cropping rectangle of 608&nbsp;&times;&nbsp;456 pixels. The present
  862. scaling factors limit cropping to 640&nbsp;&times;&nbsp;384, so the
  863. driver returns the cropping size 608&nbsp;&times;&nbsp;384 and adjusts
  864. the image size to closest possible 304&nbsp;&times;&nbsp;192.</para>
  865. </section>
  866. <section>
  867. <title>Examples</title>
  868. <para>Source and target rectangles shall remain unchanged across
  869. closing and reopening a device, such that piping data into or out of a
  870. device will work without special preparations. More advanced
  871. applications should ensure the parameters are suitable before starting
  872. I/O.</para>
  873. <example>
  874. <title>Resetting the cropping parameters</title>
  875. <para>(A video capture device is assumed; change
  876. <constant>V4L2_BUF_TYPE_VIDEO_CAPTURE</constant> for other
  877. devices.)</para>
  878. <programlisting>
  879. &v4l2-cropcap; cropcap;
  880. &v4l2-crop; crop;
  881. memset (&amp;cropcap, 0, sizeof (cropcap));
  882. cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  883. if (-1 == ioctl (fd, &VIDIOC-CROPCAP;, &amp;cropcap)) {
  884. perror ("VIDIOC_CROPCAP");
  885. exit (EXIT_FAILURE);
  886. }
  887. memset (&amp;crop, 0, sizeof (crop));
  888. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  889. crop.c = cropcap.defrect;
  890. /* Ignore if cropping is not supported (EINVAL). */
  891. if (-1 == ioctl (fd, &VIDIOC-S-CROP;, &amp;crop)
  892. &amp;&amp; errno != EINVAL) {
  893. perror ("VIDIOC_S_CROP");
  894. exit (EXIT_FAILURE);
  895. }
  896. </programlisting>
  897. </example>
  898. <example>
  899. <title>Simple downscaling</title>
  900. <para>(A video capture device is assumed.)</para>
  901. <programlisting>
  902. &v4l2-cropcap; cropcap;
  903. &v4l2-format; format;
  904. reset_cropping_parameters ();
  905. /* Scale down to 1/4 size of full picture. */
  906. memset (&amp;format, 0, sizeof (format)); /* defaults */
  907. format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  908. format.fmt.pix.width = cropcap.defrect.width &gt;&gt; 1;
  909. format.fmt.pix.height = cropcap.defrect.height &gt;&gt; 1;
  910. format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  911. if (-1 == ioctl (fd, &VIDIOC-S-FMT;, &amp;format)) {
  912. perror ("VIDIOC_S_FORMAT");
  913. exit (EXIT_FAILURE);
  914. }
  915. /* We could check the actual image size now, the actual scaling factor
  916. or if the driver can scale at all. */
  917. </programlisting>
  918. </example>
  919. <example>
  920. <title>Selecting an output area</title>
  921. <programlisting>
  922. &v4l2-cropcap; cropcap;
  923. &v4l2-crop; crop;
  924. memset (&amp;cropcap, 0, sizeof (cropcap));
  925. cropcap.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  926. if (-1 == ioctl (fd, VIDIOC_CROPCAP;, &amp;cropcap)) {
  927. perror ("VIDIOC_CROPCAP");
  928. exit (EXIT_FAILURE);
  929. }
  930. memset (&amp;crop, 0, sizeof (crop));
  931. crop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  932. crop.c = cropcap.defrect;
  933. /* Scale the width and height to 50 % of their original size
  934. and center the output. */
  935. crop.c.width /= 2;
  936. crop.c.height /= 2;
  937. crop.c.left += crop.c.width / 2;
  938. crop.c.top += crop.c.height / 2;
  939. /* Ignore if cropping is not supported (EINVAL). */
  940. if (-1 == ioctl (fd, VIDIOC_S_CROP, &amp;crop)
  941. &amp;&amp; errno != EINVAL) {
  942. perror ("VIDIOC_S_CROP");
  943. exit (EXIT_FAILURE);
  944. }
  945. </programlisting>
  946. </example>
  947. <example>
  948. <title>Current scaling factor and pixel aspect</title>
  949. <para>(A video capture device is assumed.)</para>
  950. <programlisting>
  951. &v4l2-cropcap; cropcap;
  952. &v4l2-crop; crop;
  953. &v4l2-format; format;
  954. double hscale, vscale;
  955. double aspect;
  956. int dwidth, dheight;
  957. memset (&amp;cropcap, 0, sizeof (cropcap));
  958. cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  959. if (-1 == ioctl (fd, &VIDIOC-CROPCAP;, &amp;cropcap)) {
  960. perror ("VIDIOC_CROPCAP");
  961. exit (EXIT_FAILURE);
  962. }
  963. memset (&amp;crop, 0, sizeof (crop));
  964. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  965. if (-1 == ioctl (fd, &VIDIOC-G-CROP;, &amp;crop)) {
  966. if (errno != EINVAL) {
  967. perror ("VIDIOC_G_CROP");
  968. exit (EXIT_FAILURE);
  969. }
  970. /* Cropping not supported. */
  971. crop.c = cropcap.defrect;
  972. }
  973. memset (&amp;format, 0, sizeof (format));
  974. format.fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  975. if (-1 == ioctl (fd, &VIDIOC-G-FMT;, &amp;format)) {
  976. perror ("VIDIOC_G_FMT");
  977. exit (EXIT_FAILURE);
  978. }
  979. /* The scaling applied by the driver. */
  980. hscale = format.fmt.pix.width / (double) crop.c.width;
  981. vscale = format.fmt.pix.height / (double) crop.c.height;
  982. aspect = cropcap.pixelaspect.numerator /
  983. (double) cropcap.pixelaspect.denominator;
  984. aspect = aspect * hscale / vscale;
  985. /* Devices following ITU-R BT.601 do not capture
  986. square pixels. For playback on a computer monitor
  987. we should scale the images to this size. */
  988. dwidth = format.fmt.pix.width / aspect;
  989. dheight = format.fmt.pix.height;
  990. </programlisting>
  991. </example>
  992. </section>
  993. </section>
  994. &sub-selection-api;
  995. <section id="streaming-par">
  996. <title>Streaming Parameters</title>
  997. <para>Streaming parameters are intended to optimize the video
  998. capture process as well as I/O. Presently applications can request a
  999. high quality capture mode with the &VIDIOC-S-PARM; ioctl.</para>
  1000. <para>The current video standard determines a nominal number of
  1001. frames per second. If less than this number of frames is to be
  1002. captured or output, applications can request frame skipping or
  1003. duplicating on the driver side. This is especially useful when using
  1004. the &func-read; or &func-write;, which are not augmented by timestamps
  1005. or sequence counters, and to avoid unnecessary data copying.</para>
  1006. <para>Finally these ioctls can be used to determine the number of
  1007. buffers used internally by a driver in read/write mode. For
  1008. implications see the section discussing the &func-read;
  1009. function.</para>
  1010. <para>To get and set the streaming parameters applications call
  1011. the &VIDIOC-G-PARM; and &VIDIOC-S-PARM; ioctl, respectively. They take
  1012. a pointer to a &v4l2-streamparm;, which contains a union holding
  1013. separate parameters for input and output devices.</para>
  1014. <para>These ioctls are optional, drivers need not implement
  1015. them. If so, they return the &EINVAL;.</para>
  1016. </section>