common.xml 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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 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 input, or a radio tuner 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. This specification does not define radio output
  422. devices.</para>
  423. <para>To query and change modulator properties applications use
  424. the &VIDIOC-G-MODULATOR; and &VIDIOC-S-MODULATOR; ioctl. Note that
  425. <constant>VIDIOC_S_MODULATOR</constant> does not switch the current
  426. modulator, when there is more than one at all. The modulator is solely
  427. determined by the current video output. Drivers must support both
  428. ioctls and set the <constant>V4L2_CAP_MODULATOR</constant> flag in
  429. the &v4l2-capability; returned by the &VIDIOC-QUERYCAP; ioctl when the
  430. device has one or more modulators.</para>
  431. </section>
  432. <section>
  433. <title>Radio Frequency</title>
  434. <para>To get and set the tuner or modulator radio frequency
  435. applications use the &VIDIOC-G-FREQUENCY; and &VIDIOC-S-FREQUENCY;
  436. ioctl which both take a pointer to a &v4l2-frequency;. These ioctls
  437. are used for TV and radio devices alike. Drivers must support both
  438. ioctls when the tuner or modulator ioctls are supported, or
  439. when the device is a radio device.</para>
  440. </section>
  441. </section>
  442. <section id="standard">
  443. <title>Video Standards</title>
  444. <para>Video devices typically support one or more different video
  445. standards or variations of standards. Each video input and output may
  446. support another set of standards. This set is reported by the
  447. <structfield>std</structfield> field of &v4l2-input; and
  448. &v4l2-output; returned by the &VIDIOC-ENUMINPUT; and
  449. &VIDIOC-ENUMOUTPUT; ioctl, respectively.</para>
  450. <para>V4L2 defines one bit for each analog video standard
  451. currently in use worldwide, and sets aside bits for driver defined
  452. standards, &eg; hybrid standards to watch NTSC video tapes on PAL TVs
  453. and vice versa. Applications can use the predefined bits to select a
  454. particular standard, although presenting the user a menu of supported
  455. standards is preferred. To enumerate and query the attributes of the
  456. supported standards applications use the &VIDIOC-ENUMSTD; ioctl.</para>
  457. <para>Many of the defined standards are actually just variations
  458. of a few major standards. The hardware may in fact not distinguish
  459. between them, or do so internal and switch automatically. Therefore
  460. enumerated standards also contain sets of one or more standard
  461. bits.</para>
  462. <para>Assume a hypothetic tuner capable of demodulating B/PAL,
  463. G/PAL and I/PAL signals. The first enumerated standard is a set of B
  464. and G/PAL, switched automatically depending on the selected radio
  465. frequency in UHF or VHF band. Enumeration gives a "PAL-B/G" or "PAL-I"
  466. choice. Similar a Composite input may collapse standards, enumerating
  467. "PAL-B/G/H/I", "NTSC-M" and "SECAM-D/K".<footnote>
  468. <para>Some users are already confused by technical terms PAL,
  469. NTSC and SECAM. There is no point asking them to distinguish between
  470. B, G, D, or K when the software or hardware can do that
  471. automatically.</para>
  472. </footnote></para>
  473. <para>To query and select the standard used by the current video
  474. input or output applications call the &VIDIOC-G-STD; and
  475. &VIDIOC-S-STD; ioctl, respectively. The <emphasis>received</emphasis>
  476. 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>
  477. <para>An alternative to the current scheme is to use pointers
  478. to indices as arguments of <constant>VIDIOC_G_STD</constant> and
  479. <constant>VIDIOC_S_STD</constant>, the &v4l2-input; and
  480. &v4l2-output; <structfield>std</structfield> field would be a set of
  481. indices like <structfield>audioset</structfield>.</para>
  482. <para>Indices are consistent with the rest of the API
  483. and identify the standard unambiguously. In the present scheme of
  484. things an enumerated standard is looked up by &v4l2-std-id;. Now the
  485. standards supported by the inputs of a device can overlap. Just
  486. assume the tuner and composite input in the example above both
  487. exist on a device. An enumeration of "PAL-B/G", "PAL-H/I" suggests
  488. a choice which does not exist. We cannot merge or omit sets, because
  489. applications would be unable to find the standards reported by
  490. <constant>VIDIOC_G_STD</constant>. That leaves separate enumerations
  491. for each input. Also selecting a standard by &v4l2-std-id; can be
  492. ambiguous. Advantage of this method is that applications need not
  493. identify the standard indirectly, after enumerating.</para><para>So in
  494. summary, the lookup itself is unavoidable. The difference is only
  495. whether the lookup is necessary to find an enumerated standard or to
  496. switch to a standard by &v4l2-std-id;.</para>
  497. </footnote> Drivers must implement all video standard ioctls
  498. when the device has one or more video inputs or outputs.</para>
  499. <para>Special rules apply to USB cameras where the notion of video
  500. standards makes little sense. More generally any capture device,
  501. output devices accordingly, which is <itemizedlist>
  502. <listitem>
  503. <para>incapable of capturing fields or frames at the nominal
  504. rate of the video standard, or</para>
  505. </listitem>
  506. <listitem>
  507. <para>where <link linkend="buffer">timestamps</link> refer
  508. to the instant the field or frame was received by the driver, not the
  509. capture time, or</para>
  510. </listitem>
  511. <listitem>
  512. <para>where <link linkend="buffer">sequence numbers</link>
  513. refer to the frames received by the driver, not the captured
  514. frames.</para>
  515. </listitem>
  516. </itemizedlist> Here the driver shall set the
  517. <structfield>std</structfield> field of &v4l2-input; and &v4l2-output;
  518. to zero, the <constant>VIDIOC_G_STD</constant>,
  519. <constant>VIDIOC_S_STD</constant>,
  520. <constant>VIDIOC_QUERYSTD</constant> and
  521. <constant>VIDIOC_ENUMSTD</constant> ioctls shall return the
  522. &EINVAL;.<footnote>
  523. <para>See <xref linkend="buffer" /> for a rationale. Probably
  524. even USB cameras follow some well known video standard. It might have
  525. been better to explicitly indicate elsewhere if a device cannot live
  526. up to normal expectations, instead of this exception.</para>
  527. </footnote></para>
  528. <example>
  529. <title>Information about the current video standard</title>
  530. <programlisting>
  531. &v4l2-std-id; std_id;
  532. &v4l2-standard; standard;
  533. if (-1 == ioctl (fd, &VIDIOC-G-STD;, &amp;std_id)) {
  534. /* Note when VIDIOC_ENUMSTD always returns EINVAL this
  535. is no video device or it falls under the USB exception,
  536. and VIDIOC_G_STD returning EINVAL is no error. */
  537. perror ("VIDIOC_G_STD");
  538. exit (EXIT_FAILURE);
  539. }
  540. memset (&amp;standard, 0, sizeof (standard));
  541. standard.index = 0;
  542. while (0 == ioctl (fd, &VIDIOC-ENUMSTD;, &amp;standard)) {
  543. if (standard.id &amp; std_id) {
  544. printf ("Current video standard: %s\n", standard.name);
  545. exit (EXIT_SUCCESS);
  546. }
  547. standard.index++;
  548. }
  549. /* EINVAL indicates the end of the enumeration, which cannot be
  550. empty unless this device falls under the USB exception. */
  551. if (errno == EINVAL || standard.index == 0) {
  552. perror ("VIDIOC_ENUMSTD");
  553. exit (EXIT_FAILURE);
  554. }
  555. </programlisting>
  556. </example>
  557. <example>
  558. <title>Listing the video standards supported by the current
  559. input</title>
  560. <programlisting>
  561. &v4l2-input; input;
  562. &v4l2-standard; standard;
  563. memset (&amp;input, 0, sizeof (input));
  564. if (-1 == ioctl (fd, &VIDIOC-G-INPUT;, &amp;input.index)) {
  565. perror ("VIDIOC_G_INPUT");
  566. exit (EXIT_FAILURE);
  567. }
  568. if (-1 == ioctl (fd, &VIDIOC-ENUMINPUT;, &amp;input)) {
  569. perror ("VIDIOC_ENUM_INPUT");
  570. exit (EXIT_FAILURE);
  571. }
  572. printf ("Current input %s supports:\n", input.name);
  573. memset (&amp;standard, 0, sizeof (standard));
  574. standard.index = 0;
  575. while (0 == ioctl (fd, &VIDIOC-ENUMSTD;, &amp;standard)) {
  576. if (standard.id &amp; input.std)
  577. printf ("%s\n", standard.name);
  578. standard.index++;
  579. }
  580. /* EINVAL indicates the end of the enumeration, which cannot be
  581. empty unless this device falls under the USB exception. */
  582. if (errno != EINVAL || standard.index == 0) {
  583. perror ("VIDIOC_ENUMSTD");
  584. exit (EXIT_FAILURE);
  585. }
  586. </programlisting>
  587. </example>
  588. <example>
  589. <title>Selecting a new video standard</title>
  590. <programlisting>
  591. &v4l2-input; input;
  592. &v4l2-std-id; std_id;
  593. memset (&amp;input, 0, sizeof (input));
  594. if (-1 == ioctl (fd, &VIDIOC-G-INPUT;, &amp;input.index)) {
  595. perror ("VIDIOC_G_INPUT");
  596. exit (EXIT_FAILURE);
  597. }
  598. if (-1 == ioctl (fd, &VIDIOC-ENUMINPUT;, &amp;input)) {
  599. perror ("VIDIOC_ENUM_INPUT");
  600. exit (EXIT_FAILURE);
  601. }
  602. if (0 == (input.std &amp; V4L2_STD_PAL_BG)) {
  603. fprintf (stderr, "Oops. B/G PAL is not supported.\n");
  604. exit (EXIT_FAILURE);
  605. }
  606. /* Note this is also supposed to work when only B
  607. <emphasis>or</emphasis> G/PAL is supported. */
  608. std_id = V4L2_STD_PAL_BG;
  609. if (-1 == ioctl (fd, &VIDIOC-S-STD;, &amp;std_id)) {
  610. perror ("VIDIOC_S_STD");
  611. exit (EXIT_FAILURE);
  612. }
  613. </programlisting>
  614. </example>
  615. <section id="dv-timings">
  616. <title>Digital Video (DV) Timings</title>
  617. <para>
  618. The video standards discussed so far has been dealing with Analog TV and the
  619. corresponding video timings. Today there are many more different hardware interfaces
  620. such as High Definition TV interfaces (HDMI), VGA, DVI connectors etc., that carry
  621. video signals and there is a need to extend the API to select the video timings
  622. for these interfaces. Since it is not possible to extend the &v4l2-std-id; due to
  623. the limited bits available, a new set of IOCTLs is added to set/get video timings at
  624. the input and output: </para><itemizedlist>
  625. <listitem>
  626. <para>DV Presets: Digital Video (DV) presets. These are IDs representing a
  627. video timing at the input/output. Presets are pre-defined timings implemented
  628. by the hardware according to video standards. A __u32 data type is used to represent
  629. a preset unlike the bit mask that is used in &v4l2-std-id; allowing future extensions
  630. to support as many different presets as needed.</para>
  631. </listitem>
  632. <listitem>
  633. <para>Custom DV Timings: This will allow applications to define more detailed
  634. custom video timings for the interface. This includes parameters such as width, height,
  635. polarities, frontporch, backporch etc.
  636. </para>
  637. </listitem>
  638. </itemizedlist>
  639. <para>To enumerate and query the attributes of DV presets supported by a device,
  640. applications use the &VIDIOC-ENUM-DV-PRESETS; ioctl. To get the current DV preset,
  641. applications use the &VIDIOC-G-DV-PRESET; ioctl and to set a preset they use the
  642. &VIDIOC-S-DV-PRESET; ioctl.</para>
  643. <para>To set custom DV timings for the device, applications use the
  644. &VIDIOC-S-DV-TIMINGS; ioctl and to get current custom DV timings they use the
  645. &VIDIOC-G-DV-TIMINGS; ioctl.</para>
  646. <para>Applications can make use of the <xref linkend="input-capabilities" /> and
  647. <xref linkend="output-capabilities"/> flags to decide what ioctls are available to set the
  648. video timings for the device.</para>
  649. </section>
  650. </section>
  651. &sub-controls;
  652. <section id="format">
  653. <title>Data Formats</title>
  654. <section>
  655. <title>Data Format Negotiation</title>
  656. <para>Different devices exchange different kinds of data with
  657. applications, for example video images, raw or sliced VBI data, RDS
  658. datagrams. Even within one kind many different formats are possible,
  659. in particular an abundance of image formats. Although drivers must
  660. provide a default and the selection persists across closing and
  661. reopening a device, applications should always negotiate a data format
  662. before engaging in data exchange. Negotiation means the application
  663. asks for a particular format and the driver selects and reports the
  664. best the hardware can do to satisfy the request. Of course
  665. applications can also just query the current selection.</para>
  666. <para>A single mechanism exists to negotiate all data formats
  667. using the aggregate &v4l2-format; and the &VIDIOC-G-FMT; and
  668. &VIDIOC-S-FMT; ioctls. Additionally the &VIDIOC-TRY-FMT; ioctl can be
  669. used to examine what the hardware <emphasis>could</emphasis> do,
  670. without actually selecting a new data format. The data formats
  671. supported by the V4L2 API are covered in the respective device section
  672. in <xref linkend="devices" />. For a closer look at image formats see
  673. <xref linkend="pixfmt" />.</para>
  674. <para>The <constant>VIDIOC_S_FMT</constant> ioctl is a major
  675. turning-point in the initialization sequence. Prior to this point
  676. multiple panel applications can access the same device concurrently to
  677. select the current input, change controls or modify other properties.
  678. The first <constant>VIDIOC_S_FMT</constant> assigns a logical stream
  679. (video data, VBI data etc.) exclusively to one file descriptor.</para>
  680. <para>Exclusive means no other application, more precisely no
  681. other file descriptor, can grab this stream or change device
  682. properties inconsistent with the negotiated parameters. A video
  683. standard change for example, when the new standard uses a different
  684. number of scan lines, can invalidate the selected image format.
  685. Therefore only the file descriptor owning the stream can make
  686. invalidating changes. Accordingly multiple file descriptors which
  687. grabbed different logical streams prevent each other from interfering
  688. with their settings. When for example video overlay is about to start
  689. or already in progress, simultaneous video capturing may be restricted
  690. to the same cropping and image size.</para>
  691. <para>When applications omit the
  692. <constant>VIDIOC_S_FMT</constant> ioctl its locking side effects are
  693. implied by the next step, the selection of an I/O method with the
  694. &VIDIOC-REQBUFS; ioctl or implicit with the first &func-read; or
  695. &func-write; call.</para>
  696. <para>Generally only one logical stream can be assigned to a
  697. file descriptor, the exception being drivers permitting simultaneous
  698. video capturing and overlay using the same file descriptor for
  699. compatibility with V4L and earlier versions of V4L2. Switching the
  700. logical stream or returning into "panel mode" is possible by closing
  701. and reopening the device. Drivers <emphasis>may</emphasis> support a
  702. switch using <constant>VIDIOC_S_FMT</constant>.</para>
  703. <para>All drivers exchanging data with
  704. applications must support the <constant>VIDIOC_G_FMT</constant> and
  705. <constant>VIDIOC_S_FMT</constant> ioctl. Implementation of the
  706. <constant>VIDIOC_TRY_FMT</constant> is highly recommended but
  707. optional.</para>
  708. </section>
  709. <section>
  710. <title>Image Format Enumeration</title>
  711. <para>Apart of the generic format negotiation functions
  712. a special ioctl to enumerate all image formats supported by video
  713. capture, overlay or output devices is available.<footnote>
  714. <para>Enumerating formats an application has no a-priori
  715. knowledge of (otherwise it could explicitly ask for them and need not
  716. enumerate) seems useless, but there are applications serving as proxy
  717. between drivers and the actual video applications for which this is
  718. useful.</para>
  719. </footnote></para>
  720. <para>The &VIDIOC-ENUM-FMT; ioctl must be supported
  721. by all drivers exchanging image data with applications.</para>
  722. <important>
  723. <para>Drivers are not supposed to convert image formats in
  724. kernel space. They must enumerate only formats directly supported by
  725. the hardware. If necessary driver writers should publish an example
  726. conversion routine or library for integration into applications.</para>
  727. </important>
  728. </section>
  729. </section>
  730. &sub-planar-apis;
  731. <section id="crop">
  732. <title>Image Cropping, Insertion and Scaling</title>
  733. <para>Some video capture devices can sample a subsection of the
  734. picture and shrink or enlarge it to an image of arbitrary size. We
  735. call these abilities cropping and scaling. Some video output devices
  736. can scale an image up or down and insert it at an arbitrary scan line
  737. and horizontal offset into a video signal.</para>
  738. <para>Applications can use the following API to select an area in
  739. the video signal, query the default area and the hardware limits.
  740. <emphasis>Despite their name, the &VIDIOC-CROPCAP;, &VIDIOC-G-CROP;
  741. and &VIDIOC-S-CROP; ioctls apply to input as well as output
  742. devices.</emphasis></para>
  743. <para>Scaling requires a source and a target. On a video capture
  744. or overlay device the source is the video signal, and the cropping
  745. ioctls determine the area actually sampled. The target are images
  746. read by the application or overlaid onto the graphics screen. Their
  747. size (and position for an overlay) is negotiated with the
  748. &VIDIOC-G-FMT; and &VIDIOC-S-FMT; ioctls.</para>
  749. <para>On a video output device the source are the images passed in
  750. by the application, and their size is again negotiated with the
  751. <constant>VIDIOC_G/S_FMT</constant> ioctls, or may be encoded in a
  752. compressed video stream. The target is the video signal, and the
  753. cropping ioctls determine the area where the images are
  754. inserted.</para>
  755. <para>Source and target rectangles are defined even if the device
  756. does not support scaling or the <constant>VIDIOC_G/S_CROP</constant>
  757. ioctls. Their size (and position where applicable) will be fixed in
  758. this case. <emphasis>All capture and output device must support the
  759. <constant>VIDIOC_CROPCAP</constant> ioctl such that applications can
  760. determine if scaling takes place.</emphasis></para>
  761. <section>
  762. <title>Cropping Structures</title>
  763. <figure id="crop-scale">
  764. <title>Image Cropping, Insertion and Scaling</title>
  765. <mediaobject>
  766. <imageobject>
  767. <imagedata fileref="crop.pdf" format="PS" />
  768. </imageobject>
  769. <imageobject>
  770. <imagedata fileref="crop.gif" format="GIF" />
  771. </imageobject>
  772. <textobject>
  773. <phrase>The cropping, insertion and scaling process</phrase>
  774. </textobject>
  775. </mediaobject>
  776. </figure>
  777. <para>For capture devices the coordinates of the top left
  778. corner, width and height of the area which can be sampled is given by
  779. the <structfield>bounds</structfield> substructure of the
  780. &v4l2-cropcap; returned by the <constant>VIDIOC_CROPCAP</constant>
  781. ioctl. To support a wide range of hardware this specification does not
  782. define an origin or units. However by convention drivers should
  783. horizontally count unscaled samples relative to 0H (the leading edge
  784. of the horizontal sync pulse, see <xref linkend="vbi-hsync" />).
  785. Vertically ITU-R line
  786. numbers of the first field (<xref linkend="vbi-525" />, <xref
  787. linkend="vbi-625" />), multiplied by two if the driver can capture both
  788. fields.</para>
  789. <para>The top left corner, width and height of the source
  790. rectangle, that is the area actually sampled, is given by &v4l2-crop;
  791. using the same coordinate system as &v4l2-cropcap;. Applications can
  792. use the <constant>VIDIOC_G_CROP</constant> and
  793. <constant>VIDIOC_S_CROP</constant> ioctls to get and set this
  794. rectangle. It must lie completely within the capture boundaries and
  795. the driver may further adjust the requested size and/or position
  796. according to hardware limitations.</para>
  797. <para>Each capture device has a default source rectangle, given
  798. by the <structfield>defrect</structfield> substructure of
  799. &v4l2-cropcap;. The center of this rectangle shall align with the
  800. center of the active picture area of the video signal, and cover what
  801. the driver writer considers the complete picture. Drivers shall reset
  802. the source rectangle to the default when the driver is first loaded,
  803. but not later.</para>
  804. <para>For output devices these structures and ioctls are used
  805. accordingly, defining the <emphasis>target</emphasis> rectangle where
  806. the images will be inserted into the video signal.</para>
  807. </section>
  808. <section>
  809. <title>Scaling Adjustments</title>
  810. <para>Video hardware can have various cropping, insertion and
  811. scaling limitations. It may only scale up or down, support only
  812. discrete scaling factors, or have different scaling abilities in
  813. horizontal and vertical direction. Also it may not support scaling at
  814. all. At the same time the &v4l2-crop; rectangle may have to be
  815. aligned, and both the source and target rectangles may have arbitrary
  816. upper and lower size limits. In particular the maximum
  817. <structfield>width</structfield> and <structfield>height</structfield>
  818. in &v4l2-crop; may be smaller than the
  819. &v4l2-cropcap;.<structfield>bounds</structfield> area. Therefore, as
  820. usual, drivers are expected to adjust the requested parameters and
  821. return the actual values selected.</para>
  822. <para>Applications can change the source or the target rectangle
  823. first, as they may prefer a particular image size or a certain area in
  824. the video signal. If the driver has to adjust both to satisfy hardware
  825. limitations, the last requested rectangle shall take priority, and the
  826. driver should preferably adjust the opposite one. The &VIDIOC-TRY-FMT;
  827. ioctl however shall not change the driver state and therefore only
  828. adjust the requested rectangle.</para>
  829. <para>Suppose scaling on a video capture device is restricted to
  830. a factor 1:1 or 2:1 in either direction and the target image size must
  831. be a multiple of 16&nbsp;&times;&nbsp;16 pixels. The source cropping
  832. rectangle is set to defaults, which are also the upper limit in this
  833. example, of 640&nbsp;&times;&nbsp;400 pixels at offset 0,&nbsp;0. An
  834. application requests an image size of 300&nbsp;&times;&nbsp;225
  835. pixels, assuming video will be scaled down from the "full picture"
  836. accordingly. The driver sets the image size to the closest possible
  837. values 304&nbsp;&times;&nbsp;224, then chooses the cropping rectangle
  838. closest to the requested size, that is 608&nbsp;&times;&nbsp;224
  839. (224&nbsp;&times;&nbsp;2:1 would exceed the limit 400). The offset
  840. 0,&nbsp;0 is still valid, thus unmodified. Given the default cropping
  841. rectangle reported by <constant>VIDIOC_CROPCAP</constant> the
  842. application can easily propose another offset to center the cropping
  843. rectangle.</para>
  844. <para>Now the application may insist on covering an area using a
  845. picture aspect ratio closer to the original request, so it asks for a
  846. cropping rectangle of 608&nbsp;&times;&nbsp;456 pixels. The present
  847. scaling factors limit cropping to 640&nbsp;&times;&nbsp;384, so the
  848. driver returns the cropping size 608&nbsp;&times;&nbsp;384 and adjusts
  849. the image size to closest possible 304&nbsp;&times;&nbsp;192.</para>
  850. </section>
  851. <section>
  852. <title>Examples</title>
  853. <para>Source and target rectangles shall remain unchanged across
  854. closing and reopening a device, such that piping data into or out of a
  855. device will work without special preparations. More advanced
  856. applications should ensure the parameters are suitable before starting
  857. I/O.</para>
  858. <example>
  859. <title>Resetting the cropping parameters</title>
  860. <para>(A video capture device is assumed; change
  861. <constant>V4L2_BUF_TYPE_VIDEO_CAPTURE</constant> for other
  862. devices.)</para>
  863. <programlisting>
  864. &v4l2-cropcap; cropcap;
  865. &v4l2-crop; crop;
  866. memset (&amp;cropcap, 0, sizeof (cropcap));
  867. cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  868. if (-1 == ioctl (fd, &VIDIOC-CROPCAP;, &amp;cropcap)) {
  869. perror ("VIDIOC_CROPCAP");
  870. exit (EXIT_FAILURE);
  871. }
  872. memset (&amp;crop, 0, sizeof (crop));
  873. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  874. crop.c = cropcap.defrect;
  875. /* Ignore if cropping is not supported (EINVAL). */
  876. if (-1 == ioctl (fd, &VIDIOC-S-CROP;, &amp;crop)
  877. &amp;&amp; errno != EINVAL) {
  878. perror ("VIDIOC_S_CROP");
  879. exit (EXIT_FAILURE);
  880. }
  881. </programlisting>
  882. </example>
  883. <example>
  884. <title>Simple downscaling</title>
  885. <para>(A video capture device is assumed.)</para>
  886. <programlisting>
  887. &v4l2-cropcap; cropcap;
  888. &v4l2-format; format;
  889. reset_cropping_parameters ();
  890. /* Scale down to 1/4 size of full picture. */
  891. memset (&amp;format, 0, sizeof (format)); /* defaults */
  892. format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  893. format.fmt.pix.width = cropcap.defrect.width &gt;&gt; 1;
  894. format.fmt.pix.height = cropcap.defrect.height &gt;&gt; 1;
  895. format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  896. if (-1 == ioctl (fd, &VIDIOC-S-FMT;, &amp;format)) {
  897. perror ("VIDIOC_S_FORMAT");
  898. exit (EXIT_FAILURE);
  899. }
  900. /* We could check the actual image size now, the actual scaling factor
  901. or if the driver can scale at all. */
  902. </programlisting>
  903. </example>
  904. <example>
  905. <title>Selecting an output area</title>
  906. <programlisting>
  907. &v4l2-cropcap; cropcap;
  908. &v4l2-crop; crop;
  909. memset (&amp;cropcap, 0, sizeof (cropcap));
  910. cropcap.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  911. if (-1 == ioctl (fd, VIDIOC_CROPCAP;, &amp;cropcap)) {
  912. perror ("VIDIOC_CROPCAP");
  913. exit (EXIT_FAILURE);
  914. }
  915. memset (&amp;crop, 0, sizeof (crop));
  916. crop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  917. crop.c = cropcap.defrect;
  918. /* Scale the width and height to 50 % of their original size
  919. and center the output. */
  920. crop.c.width /= 2;
  921. crop.c.height /= 2;
  922. crop.c.left += crop.c.width / 2;
  923. crop.c.top += crop.c.height / 2;
  924. /* Ignore if cropping is not supported (EINVAL). */
  925. if (-1 == ioctl (fd, VIDIOC_S_CROP, &amp;crop)
  926. &amp;&amp; errno != EINVAL) {
  927. perror ("VIDIOC_S_CROP");
  928. exit (EXIT_FAILURE);
  929. }
  930. </programlisting>
  931. </example>
  932. <example>
  933. <title>Current scaling factor and pixel aspect</title>
  934. <para>(A video capture device is assumed.)</para>
  935. <programlisting>
  936. &v4l2-cropcap; cropcap;
  937. &v4l2-crop; crop;
  938. &v4l2-format; format;
  939. double hscale, vscale;
  940. double aspect;
  941. int dwidth, dheight;
  942. memset (&amp;cropcap, 0, sizeof (cropcap));
  943. cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  944. if (-1 == ioctl (fd, &VIDIOC-CROPCAP;, &amp;cropcap)) {
  945. perror ("VIDIOC_CROPCAP");
  946. exit (EXIT_FAILURE);
  947. }
  948. memset (&amp;crop, 0, sizeof (crop));
  949. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  950. if (-1 == ioctl (fd, &VIDIOC-G-CROP;, &amp;crop)) {
  951. if (errno != EINVAL) {
  952. perror ("VIDIOC_G_CROP");
  953. exit (EXIT_FAILURE);
  954. }
  955. /* Cropping not supported. */
  956. crop.c = cropcap.defrect;
  957. }
  958. memset (&amp;format, 0, sizeof (format));
  959. format.fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  960. if (-1 == ioctl (fd, &VIDIOC-G-FMT;, &amp;format)) {
  961. perror ("VIDIOC_G_FMT");
  962. exit (EXIT_FAILURE);
  963. }
  964. /* The scaling applied by the driver. */
  965. hscale = format.fmt.pix.width / (double) crop.c.width;
  966. vscale = format.fmt.pix.height / (double) crop.c.height;
  967. aspect = cropcap.pixelaspect.numerator /
  968. (double) cropcap.pixelaspect.denominator;
  969. aspect = aspect * hscale / vscale;
  970. /* Devices following ITU-R BT.601 do not capture
  971. square pixels. For playback on a computer monitor
  972. we should scale the images to this size. */
  973. dwidth = format.fmt.pix.width / aspect;
  974. dheight = format.fmt.pix.height;
  975. </programlisting>
  976. </example>
  977. </section>
  978. </section>
  979. &sub-selection-api;
  980. <section id="streaming-par">
  981. <title>Streaming Parameters</title>
  982. <para>Streaming parameters are intended to optimize the video
  983. capture process as well as I/O. Presently applications can request a
  984. high quality capture mode with the &VIDIOC-S-PARM; ioctl.</para>
  985. <para>The current video standard determines a nominal number of
  986. frames per second. If less than this number of frames is to be
  987. captured or output, applications can request frame skipping or
  988. duplicating on the driver side. This is especially useful when using
  989. the &func-read; or &func-write;, which are not augmented by timestamps
  990. or sequence counters, and to avoid unnecessary data copying.</para>
  991. <para>Finally these ioctls can be used to determine the number of
  992. buffers used internally by a driver in read/write mode. For
  993. implications see the section discussing the &func-read;
  994. function.</para>
  995. <para>To get and set the streaming parameters applications call
  996. the &VIDIOC-G-PARM; and &VIDIOC-S-PARM; ioctl, respectively. They take
  997. a pointer to a &v4l2-streamparm;, which contains a union holding
  998. separate parameters for input and output devices.</para>
  999. <para>These ioctls are optional, drivers need not implement
  1000. them. If so, they return the &EINVAL;.</para>
  1001. </section>