common.xml 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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 recomended 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. Other features can be queried
  213. by calling the respective ioctl, for example &VIDIOC-ENUMINPUT;
  214. to learn about the number, types and names of video connectors on the
  215. device. Although abstraction is a major objective of this API, the
  216. ioctl also allows driver specific applications to reliable identify
  217. the driver.</para>
  218. <para>All V4L2 drivers must support
  219. <constant>VIDIOC_QUERYCAP</constant>. Applications should always call
  220. this ioctl after opening the device.</para>
  221. </section>
  222. <section id="app-pri">
  223. <title>Application Priority</title>
  224. <para>When multiple applications share a device it may be
  225. desirable to assign them different priorities. Contrary to the
  226. traditional "rm -rf /" school of thought a video recording application
  227. could for example block other applications from changing video
  228. controls or switching the current TV channel. Another objective is to
  229. permit low priority applications working in background, which can be
  230. preempted by user controlled applications and automatically regain
  231. control of the device at a later time.</para>
  232. <para>Since these features cannot be implemented entirely in user
  233. space V4L2 defines the &VIDIOC-G-PRIORITY; and &VIDIOC-S-PRIORITY;
  234. ioctls to request and query the access priority associate with a file
  235. descriptor. Opening a device assigns a medium priority, compatible
  236. with earlier versions of V4L2 and drivers not supporting these ioctls.
  237. Applications requiring a different priority will usually call
  238. <constant>VIDIOC_S_PRIORITY</constant> after verifying the device with
  239. the &VIDIOC-QUERYCAP; ioctl.</para>
  240. <para>Ioctls changing driver properties, such as &VIDIOC-S-INPUT;,
  241. return an &EBUSY; after another application obtained higher priority.
  242. An event mechanism to notify applications about asynchronous property
  243. changes has been proposed but not added yet.</para>
  244. </section>
  245. <section id="video">
  246. <title>Video Inputs and Outputs</title>
  247. <para>Video inputs and outputs are physical connectors of a
  248. device. These can be for example RF connectors (antenna/cable), CVBS
  249. a.k.a. Composite Video, S-Video or RGB connectors. Only video and VBI
  250. capture devices have inputs, output devices have outputs, at least one
  251. each. Radio devices have no video inputs or outputs.</para>
  252. <para>To learn about the number and attributes of the
  253. available inputs and outputs applications can enumerate them with the
  254. &VIDIOC-ENUMINPUT; and &VIDIOC-ENUMOUTPUT; ioctl, respectively. The
  255. &v4l2-input; returned by the <constant>VIDIOC_ENUMINPUT</constant>
  256. ioctl also contains signal status information applicable when the
  257. current video input is queried.</para>
  258. <para>The &VIDIOC-G-INPUT; and &VIDIOC-G-OUTPUT; ioctl return the
  259. index of the current video input or output. To select a different
  260. input or output applications call the &VIDIOC-S-INPUT; and
  261. &VIDIOC-S-OUTPUT; ioctl. Drivers must implement all the input ioctls
  262. when the device has one or more inputs, all the output ioctls when the
  263. device has one or more outputs.</para>
  264. <!--
  265. <figure id=io-tree>
  266. <title>Input and output enumeration is the root of most device properties.</title>
  267. <mediaobject>
  268. <imageobject>
  269. <imagedata fileref="links.pdf" format="ps" />
  270. </imageobject>
  271. <imageobject>
  272. <imagedata fileref="links.gif" format="gif" />
  273. </imageobject>
  274. <textobject>
  275. <phrase>Links between various device property structures.</phrase>
  276. </textobject>
  277. </mediaobject>
  278. </figure>
  279. -->
  280. <example>
  281. <title>Information about the current video input</title>
  282. <programlisting>
  283. &v4l2-input; input;
  284. int index;
  285. if (-1 == ioctl (fd, &VIDIOC-G-INPUT;, &amp;index)) {
  286. perror ("VIDIOC_G_INPUT");
  287. exit (EXIT_FAILURE);
  288. }
  289. memset (&amp;input, 0, sizeof (input));
  290. input.index = index;
  291. if (-1 == ioctl (fd, &VIDIOC-ENUMINPUT;, &amp;input)) {
  292. perror ("VIDIOC_ENUMINPUT");
  293. exit (EXIT_FAILURE);
  294. }
  295. printf ("Current input: %s\n", input.name);
  296. </programlisting>
  297. </example>
  298. <example>
  299. <title>Switching to the first video input</title>
  300. <programlisting>
  301. int index;
  302. index = 0;
  303. if (-1 == ioctl (fd, &VIDIOC-S-INPUT;, &amp;index)) {
  304. perror ("VIDIOC_S_INPUT");
  305. exit (EXIT_FAILURE);
  306. }
  307. </programlisting>
  308. </example>
  309. </section>
  310. <section id="audio">
  311. <title>Audio Inputs and Outputs</title>
  312. <para>Audio inputs and outputs are physical connectors of a
  313. device. Video capture devices have inputs, output devices have
  314. outputs, zero or more each. Radio devices have no audio inputs or
  315. outputs. They have exactly one tuner which in fact
  316. <emphasis>is</emphasis> an audio source, but this API associates
  317. tuners with video inputs or outputs only, and radio devices have
  318. none of these.<footnote>
  319. <para>Actually &v4l2-audio; ought to have a
  320. <structfield>tuner</structfield> field like &v4l2-input;, not only
  321. making the API more consistent but also permitting radio devices with
  322. multiple tuners.</para>
  323. </footnote> A connector on a TV card to loop back the received
  324. audio signal to a sound card is not considered an audio output.</para>
  325. <para>Audio and video inputs and outputs are associated. Selecting
  326. a video source also selects an audio source. This is most evident when
  327. the video and audio source is a tuner. Further audio connectors can
  328. combine with more than one video input or output. Assumed two
  329. composite video inputs and two audio inputs exist, there may be up to
  330. four valid combinations. The relation of video and audio connectors
  331. is defined in the <structfield>audioset</structfield> field of the
  332. respective &v4l2-input; or &v4l2-output;, where each bit represents
  333. the index number, starting at zero, of one audio input or output.</para>
  334. <para>To learn about the number and attributes of the
  335. available inputs and outputs applications can enumerate them with the
  336. &VIDIOC-ENUMAUDIO; and &VIDIOC-ENUMAUDOUT; ioctl, respectively. The
  337. &v4l2-audio; returned by the <constant>VIDIOC_ENUMAUDIO</constant> ioctl
  338. also contains signal status information applicable when the current
  339. audio input is queried.</para>
  340. <para>The &VIDIOC-G-AUDIO; and &VIDIOC-G-AUDOUT; ioctl report
  341. the current audio input and output, respectively. Note that, unlike
  342. &VIDIOC-G-INPUT; and &VIDIOC-G-OUTPUT; these ioctls return a structure
  343. as <constant>VIDIOC_ENUMAUDIO</constant> and
  344. <constant>VIDIOC_ENUMAUDOUT</constant> do, not just an index.</para>
  345. <para>To select an audio input and change its properties
  346. applications call the &VIDIOC-S-AUDIO; ioctl. To select an audio
  347. output (which presently has no changeable properties) applications
  348. call the &VIDIOC-S-AUDOUT; ioctl.</para>
  349. <para>Drivers must implement all input ioctls when the device
  350. has one or more inputs, all output ioctls when the device has one
  351. or more outputs. When the device has any audio inputs or outputs the
  352. driver must set the <constant>V4L2_CAP_AUDIO</constant> flag in the
  353. &v4l2-capability; returned by the &VIDIOC-QUERYCAP; ioctl.</para>
  354. <example>
  355. <title>Information about the current audio input</title>
  356. <programlisting>
  357. &v4l2-audio; audio;
  358. memset (&amp;audio, 0, sizeof (audio));
  359. if (-1 == ioctl (fd, &VIDIOC-G-AUDIO;, &amp;audio)) {
  360. perror ("VIDIOC_G_AUDIO");
  361. exit (EXIT_FAILURE);
  362. }
  363. printf ("Current input: %s\n", audio.name);
  364. </programlisting>
  365. </example>
  366. <example>
  367. <title>Switching to the first audio input</title>
  368. <programlisting>
  369. &v4l2-audio; audio;
  370. memset (&amp;audio, 0, sizeof (audio)); /* clear audio.mode, audio.reserved */
  371. audio.index = 0;
  372. if (-1 == ioctl (fd, &VIDIOC-S-AUDIO;, &amp;audio)) {
  373. perror ("VIDIOC_S_AUDIO");
  374. exit (EXIT_FAILURE);
  375. }
  376. </programlisting>
  377. </example>
  378. </section>
  379. <section id="tuner">
  380. <title>Tuners and Modulators</title>
  381. <section>
  382. <title>Tuners</title>
  383. <para>Video input devices can have one or more tuners
  384. demodulating a RF signal. Each tuner is associated with one or more
  385. video inputs, depending on the number of RF connectors on the tuner.
  386. The <structfield>type</structfield> field of the respective
  387. &v4l2-input; returned by the &VIDIOC-ENUMINPUT; ioctl is set to
  388. <constant>V4L2_INPUT_TYPE_TUNER</constant> and its
  389. <structfield>tuner</structfield> field contains the index number of
  390. the tuner.</para>
  391. <para>Radio devices have exactly one tuner with index zero, no
  392. video inputs.</para>
  393. <para>To query and change tuner properties applications use the
  394. &VIDIOC-G-TUNER; and &VIDIOC-S-TUNER; ioctl, respectively. The
  395. &v4l2-tuner; returned by <constant>VIDIOC_G_TUNER</constant> also
  396. contains signal status information applicable when the tuner of the
  397. current video input, or a radio tuner is queried. Note that
  398. <constant>VIDIOC_S_TUNER</constant> does not switch the current tuner,
  399. when there is more than one at all. The tuner is solely determined by
  400. the current video input. Drivers must support both ioctls and set the
  401. <constant>V4L2_CAP_TUNER</constant> flag in the &v4l2-capability;
  402. returned by the &VIDIOC-QUERYCAP; ioctl when the device has one or
  403. more tuners.</para>
  404. </section>
  405. <section>
  406. <title>Modulators</title>
  407. <para>Video output devices can have one or more modulators, uh,
  408. modulating a video signal for radiation or connection to the antenna
  409. input of a TV set or video recorder. Each modulator is associated with
  410. one or more video outputs, depending on the number of RF connectors on
  411. the modulator. The <structfield>type</structfield> field of the
  412. respective &v4l2-output; returned by the &VIDIOC-ENUMOUTPUT; ioctl is
  413. set to <constant>V4L2_OUTPUT_TYPE_MODULATOR</constant> and its
  414. <structfield>modulator</structfield> field contains the index number
  415. of the modulator. This specification does not define radio output
  416. devices.</para>
  417. <para>To query and change modulator properties applications use
  418. the &VIDIOC-G-MODULATOR; and &VIDIOC-S-MODULATOR; ioctl. Note that
  419. <constant>VIDIOC_S_MODULATOR</constant> does not switch the current
  420. modulator, when there is more than one at all. The modulator is solely
  421. determined by the current video output. Drivers must support both
  422. ioctls and set the <constant>V4L2_CAP_MODULATOR</constant> flag in
  423. the &v4l2-capability; returned by the &VIDIOC-QUERYCAP; ioctl when the
  424. device has one or more modulators.</para>
  425. </section>
  426. <section>
  427. <title>Radio Frequency</title>
  428. <para>To get and set the tuner or modulator radio frequency
  429. applications use the &VIDIOC-G-FREQUENCY; and &VIDIOC-S-FREQUENCY;
  430. ioctl which both take a pointer to a &v4l2-frequency;. These ioctls
  431. are used for TV and radio devices alike. Drivers must support both
  432. ioctls when the tuner or modulator ioctls are supported, or
  433. when the device is a radio device.</para>
  434. </section>
  435. </section>
  436. <section id="standard">
  437. <title>Video Standards</title>
  438. <para>Video devices typically support one or more different video
  439. standards or variations of standards. Each video input and output may
  440. support another set of standards. This set is reported by the
  441. <structfield>std</structfield> field of &v4l2-input; and
  442. &v4l2-output; returned by the &VIDIOC-ENUMINPUT; and
  443. &VIDIOC-ENUMOUTPUT; ioctl, respectively.</para>
  444. <para>V4L2 defines one bit for each analog video standard
  445. currently in use worldwide, and sets aside bits for driver defined
  446. standards, &eg; hybrid standards to watch NTSC video tapes on PAL TVs
  447. and vice versa. Applications can use the predefined bits to select a
  448. particular standard, although presenting the user a menu of supported
  449. standards is preferred. To enumerate and query the attributes of the
  450. supported standards applications use the &VIDIOC-ENUMSTD; ioctl.</para>
  451. <para>Many of the defined standards are actually just variations
  452. of a few major standards. The hardware may in fact not distinguish
  453. between them, or do so internal and switch automatically. Therefore
  454. enumerated standards also contain sets of one or more standard
  455. bits.</para>
  456. <para>Assume a hypothetic tuner capable of demodulating B/PAL,
  457. G/PAL and I/PAL signals. The first enumerated standard is a set of B
  458. and G/PAL, switched automatically depending on the selected radio
  459. frequency in UHF or VHF band. Enumeration gives a "PAL-B/G" or "PAL-I"
  460. choice. Similar a Composite input may collapse standards, enumerating
  461. "PAL-B/G/H/I", "NTSC-M" and "SECAM-D/K".<footnote>
  462. <para>Some users are already confused by technical terms PAL,
  463. NTSC and SECAM. There is no point asking them to distinguish between
  464. B, G, D, or K when the software or hardware can do that
  465. automatically.</para>
  466. </footnote></para>
  467. <para>To query and select the standard used by the current video
  468. input or output applications call the &VIDIOC-G-STD; and
  469. &VIDIOC-S-STD; ioctl, respectively. The <emphasis>received</emphasis>
  470. 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>
  471. <para>An alternative to the current scheme is to use pointers
  472. to indices as arguments of <constant>VIDIOC_G_STD</constant> and
  473. <constant>VIDIOC_S_STD</constant>, the &v4l2-input; and
  474. &v4l2-output; <structfield>std</structfield> field would be a set of
  475. indices like <structfield>audioset</structfield>.</para>
  476. <para>Indices are consistent with the rest of the API
  477. and identify the standard unambiguously. In the present scheme of
  478. things an enumerated standard is looked up by &v4l2-std-id;. Now the
  479. standards supported by the inputs of a device can overlap. Just
  480. assume the tuner and composite input in the example above both
  481. exist on a device. An enumeration of "PAL-B/G", "PAL-H/I" suggests
  482. a choice which does not exist. We cannot merge or omit sets, because
  483. applications would be unable to find the standards reported by
  484. <constant>VIDIOC_G_STD</constant>. That leaves separate enumerations
  485. for each input. Also selecting a standard by &v4l2-std-id; can be
  486. ambiguous. Advantage of this method is that applications need not
  487. identify the standard indirectly, after enumerating.</para><para>So in
  488. summary, the lookup itself is unavoidable. The difference is only
  489. whether the lookup is necessary to find an enumerated standard or to
  490. switch to a standard by &v4l2-std-id;.</para>
  491. </footnote> Drivers must implement all video standard ioctls
  492. when the device has one or more video inputs or outputs.</para>
  493. <para>Special rules apply to USB cameras where the notion of video
  494. standards makes little sense. More generally any capture device,
  495. output devices accordingly, which is <itemizedlist>
  496. <listitem>
  497. <para>incapable of capturing fields or frames at the nominal
  498. rate of the video standard, or</para>
  499. </listitem>
  500. <listitem>
  501. <para>where <link linkend="buffer">timestamps</link> refer
  502. to the instant the field or frame was received by the driver, not the
  503. capture time, or</para>
  504. </listitem>
  505. <listitem>
  506. <para>where <link linkend="buffer">sequence numbers</link>
  507. refer to the frames received by the driver, not the captured
  508. frames.</para>
  509. </listitem>
  510. </itemizedlist> Here the driver shall set the
  511. <structfield>std</structfield> field of &v4l2-input; and &v4l2-output;
  512. to zero, the <constant>VIDIOC_G_STD</constant>,
  513. <constant>VIDIOC_S_STD</constant>,
  514. <constant>VIDIOC_QUERYSTD</constant> and
  515. <constant>VIDIOC_ENUMSTD</constant> ioctls shall return the
  516. &EINVAL;.<footnote>
  517. <para>See <xref linkend="buffer" /> for a rationale. Probably
  518. even USB cameras follow some well known video standard. It might have
  519. been better to explicitly indicate elsewhere if a device cannot live
  520. up to normal expectations, instead of this exception.</para>
  521. </footnote></para>
  522. <example>
  523. <title>Information about the current video standard</title>
  524. <programlisting>
  525. &v4l2-std-id; std_id;
  526. &v4l2-standard; standard;
  527. if (-1 == ioctl (fd, &VIDIOC-G-STD;, &amp;std_id)) {
  528. /* Note when VIDIOC_ENUMSTD always returns EINVAL this
  529. is no video device or it falls under the USB exception,
  530. and VIDIOC_G_STD returning EINVAL is no error. */
  531. perror ("VIDIOC_G_STD");
  532. exit (EXIT_FAILURE);
  533. }
  534. memset (&amp;standard, 0, sizeof (standard));
  535. standard.index = 0;
  536. while (0 == ioctl (fd, &VIDIOC-ENUMSTD;, &amp;standard)) {
  537. if (standard.id &amp; std_id) {
  538. printf ("Current video standard: %s\n", standard.name);
  539. exit (EXIT_SUCCESS);
  540. }
  541. standard.index++;
  542. }
  543. /* EINVAL indicates the end of the enumeration, which cannot be
  544. empty unless this device falls under the USB exception. */
  545. if (errno == EINVAL || standard.index == 0) {
  546. perror ("VIDIOC_ENUMSTD");
  547. exit (EXIT_FAILURE);
  548. }
  549. </programlisting>
  550. </example>
  551. <example>
  552. <title>Listing the video standards supported by the current
  553. input</title>
  554. <programlisting>
  555. &v4l2-input; input;
  556. &v4l2-standard; standard;
  557. memset (&amp;input, 0, sizeof (input));
  558. if (-1 == ioctl (fd, &VIDIOC-G-INPUT;, &amp;input.index)) {
  559. perror ("VIDIOC_G_INPUT");
  560. exit (EXIT_FAILURE);
  561. }
  562. if (-1 == ioctl (fd, &VIDIOC-ENUMINPUT;, &amp;input)) {
  563. perror ("VIDIOC_ENUM_INPUT");
  564. exit (EXIT_FAILURE);
  565. }
  566. printf ("Current input %s supports:\n", input.name);
  567. memset (&amp;standard, 0, sizeof (standard));
  568. standard.index = 0;
  569. while (0 == ioctl (fd, &VIDIOC-ENUMSTD;, &amp;standard)) {
  570. if (standard.id &amp; input.std)
  571. printf ("%s\n", standard.name);
  572. standard.index++;
  573. }
  574. /* EINVAL indicates the end of the enumeration, which cannot be
  575. empty unless this device falls under the USB exception. */
  576. if (errno != EINVAL || standard.index == 0) {
  577. perror ("VIDIOC_ENUMSTD");
  578. exit (EXIT_FAILURE);
  579. }
  580. </programlisting>
  581. </example>
  582. <example>
  583. <title>Selecting a new video standard</title>
  584. <programlisting>
  585. &v4l2-input; input;
  586. &v4l2-std-id; std_id;
  587. memset (&amp;input, 0, sizeof (input));
  588. if (-1 == ioctl (fd, &VIDIOC-G-INPUT;, &amp;input.index)) {
  589. perror ("VIDIOC_G_INPUT");
  590. exit (EXIT_FAILURE);
  591. }
  592. if (-1 == ioctl (fd, &VIDIOC-ENUMINPUT;, &amp;input)) {
  593. perror ("VIDIOC_ENUM_INPUT");
  594. exit (EXIT_FAILURE);
  595. }
  596. if (0 == (input.std &amp; V4L2_STD_PAL_BG)) {
  597. fprintf (stderr, "Oops. B/G PAL is not supported.\n");
  598. exit (EXIT_FAILURE);
  599. }
  600. /* Note this is also supposed to work when only B
  601. <emphasis>or</emphasis> G/PAL is supported. */
  602. std_id = V4L2_STD_PAL_BG;
  603. if (-1 == ioctl (fd, &VIDIOC-S-STD;, &amp;std_id)) {
  604. perror ("VIDIOC_S_STD");
  605. exit (EXIT_FAILURE);
  606. }
  607. </programlisting>
  608. </example>
  609. </section>
  610. &sub-controls;
  611. <section id="format">
  612. <title>Data Formats</title>
  613. <section>
  614. <title>Data Format Negotiation</title>
  615. <para>Different devices exchange different kinds of data with
  616. applications, for example video images, raw or sliced VBI data, RDS
  617. datagrams. Even within one kind many different formats are possible,
  618. in particular an abundance of image formats. Although drivers must
  619. provide a default and the selection persists across closing and
  620. reopening a device, applications should always negotiate a data format
  621. before engaging in data exchange. Negotiation means the application
  622. asks for a particular format and the driver selects and reports the
  623. best the hardware can do to satisfy the request. Of course
  624. applications can also just query the current selection.</para>
  625. <para>A single mechanism exists to negotiate all data formats
  626. using the aggregate &v4l2-format; and the &VIDIOC-G-FMT; and
  627. &VIDIOC-S-FMT; ioctls. Additionally the &VIDIOC-TRY-FMT; ioctl can be
  628. used to examine what the hardware <emphasis>could</emphasis> do,
  629. without actually selecting a new data format. The data formats
  630. supported by the V4L2 API are covered in the respective device section
  631. in <xref linkend="devices" />. For a closer look at image formats see
  632. <xref linkend="pixfmt" />.</para>
  633. <para>The <constant>VIDIOC_S_FMT</constant> ioctl is a major
  634. turning-point in the initialization sequence. Prior to this point
  635. multiple panel applications can access the same device concurrently to
  636. select the current input, change controls or modify other properties.
  637. The first <constant>VIDIOC_S_FMT</constant> assigns a logical stream
  638. (video data, VBI data etc.) exclusively to one file descriptor.</para>
  639. <para>Exclusive means no other application, more precisely no
  640. other file descriptor, can grab this stream or change device
  641. properties inconsistent with the negotiated parameters. A video
  642. standard change for example, when the new standard uses a different
  643. number of scan lines, can invalidate the selected image format.
  644. Therefore only the file descriptor owning the stream can make
  645. invalidating changes. Accordingly multiple file descriptors which
  646. grabbed different logical streams prevent each other from interfering
  647. with their settings. When for example video overlay is about to start
  648. or already in progress, simultaneous video capturing may be restricted
  649. to the same cropping and image size.</para>
  650. <para>When applications omit the
  651. <constant>VIDIOC_S_FMT</constant> ioctl its locking side effects are
  652. implied by the next step, the selection of an I/O method with the
  653. &VIDIOC-REQBUFS; ioctl or implicit with the first &func-read; or
  654. &func-write; call.</para>
  655. <para>Generally only one logical stream can be assigned to a
  656. file descriptor, the exception being drivers permitting simultaneous
  657. video capturing and overlay using the same file descriptor for
  658. compatibility with V4L and earlier versions of V4L2. Switching the
  659. logical stream or returning into "panel mode" is possible by closing
  660. and reopening the device. Drivers <emphasis>may</emphasis> support a
  661. switch using <constant>VIDIOC_S_FMT</constant>.</para>
  662. <para>All drivers exchanging data with
  663. applications must support the <constant>VIDIOC_G_FMT</constant> and
  664. <constant>VIDIOC_S_FMT</constant> ioctl. Implementation of the
  665. <constant>VIDIOC_TRY_FMT</constant> is highly recommended but
  666. optional.</para>
  667. </section>
  668. <section>
  669. <title>Image Format Enumeration</title>
  670. <para>Apart of the generic format negotiation functions
  671. a special ioctl to enumerate all image formats supported by video
  672. capture, overlay or output devices is available.<footnote>
  673. <para>Enumerating formats an application has no a-priori
  674. knowledge of (otherwise it could explicitly ask for them and need not
  675. enumerate) seems useless, but there are applications serving as proxy
  676. between drivers and the actual video applications for which this is
  677. useful.</para>
  678. </footnote></para>
  679. <para>The &VIDIOC-ENUM-FMT; ioctl must be supported
  680. by all drivers exchanging image data with applications.</para>
  681. <important>
  682. <para>Drivers are not supposed to convert image formats in
  683. kernel space. They must enumerate only formats directly supported by
  684. the hardware. If necessary driver writers should publish an example
  685. conversion routine or library for integration into applications.</para>
  686. </important>
  687. </section>
  688. </section>
  689. <section id="crop">
  690. <title>Image Cropping, Insertion and Scaling</title>
  691. <para>Some video capture devices can sample a subsection of the
  692. picture and shrink or enlarge it to an image of arbitrary size. We
  693. call these abilities cropping and scaling. Some video output devices
  694. can scale an image up or down and insert it at an arbitrary scan line
  695. and horizontal offset into a video signal.</para>
  696. <para>Applications can use the following API to select an area in
  697. the video signal, query the default area and the hardware limits.
  698. <emphasis>Despite their name, the &VIDIOC-CROPCAP;, &VIDIOC-G-CROP;
  699. and &VIDIOC-S-CROP; ioctls apply to input as well as output
  700. devices.</emphasis></para>
  701. <para>Scaling requires a source and a target. On a video capture
  702. or overlay device the source is the video signal, and the cropping
  703. ioctls determine the area actually sampled. The target are images
  704. read by the application or overlaid onto the graphics screen. Their
  705. size (and position for an overlay) is negotiated with the
  706. &VIDIOC-G-FMT; and &VIDIOC-S-FMT; ioctls.</para>
  707. <para>On a video output device the source are the images passed in
  708. by the application, and their size is again negotiated with the
  709. <constant>VIDIOC_G/S_FMT</constant> ioctls, or may be encoded in a
  710. compressed video stream. The target is the video signal, and the
  711. cropping ioctls determine the area where the images are
  712. inserted.</para>
  713. <para>Source and target rectangles are defined even if the device
  714. does not support scaling or the <constant>VIDIOC_G/S_CROP</constant>
  715. ioctls. Their size (and position where applicable) will be fixed in
  716. this case. <emphasis>All capture and output device must support the
  717. <constant>VIDIOC_CROPCAP</constant> ioctl such that applications can
  718. determine if scaling takes place.</emphasis></para>
  719. <section>
  720. <title>Cropping Structures</title>
  721. <figure id="crop-scale">
  722. <title>Image Cropping, Insertion and Scaling</title>
  723. <mediaobject>
  724. <imageobject>
  725. <imagedata fileref="crop.pdf" format="PS" />
  726. </imageobject>
  727. <imageobject>
  728. <imagedata fileref="crop.gif" format="GIF" />
  729. </imageobject>
  730. <textobject>
  731. <phrase>The cropping, insertion and scaling process</phrase>
  732. </textobject>
  733. </mediaobject>
  734. </figure>
  735. <para>For capture devices the coordinates of the top left
  736. corner, width and height of the area which can be sampled is given by
  737. the <structfield>bounds</structfield> substructure of the
  738. &v4l2-cropcap; returned by the <constant>VIDIOC_CROPCAP</constant>
  739. ioctl. To support a wide range of hardware this specification does not
  740. define an origin or units. However by convention drivers should
  741. horizontally count unscaled samples relative to 0H (the leading edge
  742. of the horizontal sync pulse, see <xref linkend="vbi-hsync" />).
  743. Vertically ITU-R line
  744. numbers of the first field (<xref linkend="vbi-525" />, <xref
  745. linkend="vbi-625" />), multiplied by two if the driver can capture both
  746. fields.</para>
  747. <para>The top left corner, width and height of the source
  748. rectangle, that is the area actually sampled, is given by &v4l2-crop;
  749. using the same coordinate system as &v4l2-cropcap;. Applications can
  750. use the <constant>VIDIOC_G_CROP</constant> and
  751. <constant>VIDIOC_S_CROP</constant> ioctls to get and set this
  752. rectangle. It must lie completely within the capture boundaries and
  753. the driver may further adjust the requested size and/or position
  754. according to hardware limitations.</para>
  755. <para>Each capture device has a default source rectangle, given
  756. by the <structfield>defrect</structfield> substructure of
  757. &v4l2-cropcap;. The center of this rectangle shall align with the
  758. center of the active picture area of the video signal, and cover what
  759. the driver writer considers the complete picture. Drivers shall reset
  760. the source rectangle to the default when the driver is first loaded,
  761. but not later.</para>
  762. <para>For output devices these structures and ioctls are used
  763. accordingly, defining the <emphasis>target</emphasis> rectangle where
  764. the images will be inserted into the video signal.</para>
  765. </section>
  766. <section>
  767. <title>Scaling Adjustments</title>
  768. <para>Video hardware can have various cropping, insertion and
  769. scaling limitations. It may only scale up or down, support only
  770. discrete scaling factors, or have different scaling abilities in
  771. horizontal and vertical direction. Also it may not support scaling at
  772. all. At the same time the &v4l2-crop; rectangle may have to be
  773. aligned, and both the source and target rectangles may have arbitrary
  774. upper and lower size limits. In particular the maximum
  775. <structfield>width</structfield> and <structfield>height</structfield>
  776. in &v4l2-crop; may be smaller than the
  777. &v4l2-cropcap;.<structfield>bounds</structfield> area. Therefore, as
  778. usual, drivers are expected to adjust the requested parameters and
  779. return the actual values selected.</para>
  780. <para>Applications can change the source or the target rectangle
  781. first, as they may prefer a particular image size or a certain area in
  782. the video signal. If the driver has to adjust both to satisfy hardware
  783. limitations, the last requested rectangle shall take priority, and the
  784. driver should preferably adjust the opposite one. The &VIDIOC-TRY-FMT;
  785. ioctl however shall not change the driver state and therefore only
  786. adjust the requested rectangle.</para>
  787. <para>Suppose scaling on a video capture device is restricted to
  788. a factor 1:1 or 2:1 in either direction and the target image size must
  789. be a multiple of 16&nbsp;&times;&nbsp;16 pixels. The source cropping
  790. rectangle is set to defaults, which are also the upper limit in this
  791. example, of 640&nbsp;&times;&nbsp;400 pixels at offset 0,&nbsp;0. An
  792. application requests an image size of 300&nbsp;&times;&nbsp;225
  793. pixels, assuming video will be scaled down from the "full picture"
  794. accordingly. The driver sets the image size to the closest possible
  795. values 304&nbsp;&times;&nbsp;224, then chooses the cropping rectangle
  796. closest to the requested size, that is 608&nbsp;&times;&nbsp;224
  797. (224&nbsp;&times;&nbsp;2:1 would exceed the limit 400). The offset
  798. 0,&nbsp;0 is still valid, thus unmodified. Given the default cropping
  799. rectangle reported by <constant>VIDIOC_CROPCAP</constant> the
  800. application can easily propose another offset to center the cropping
  801. rectangle.</para>
  802. <para>Now the application may insist on covering an area using a
  803. picture aspect ratio closer to the original request, so it asks for a
  804. cropping rectangle of 608&nbsp;&times;&nbsp;456 pixels. The present
  805. scaling factors limit cropping to 640&nbsp;&times;&nbsp;384, so the
  806. driver returns the cropping size 608&nbsp;&times;&nbsp;384 and adjusts
  807. the image size to closest possible 304&nbsp;&times;&nbsp;192.</para>
  808. </section>
  809. <section>
  810. <title>Examples</title>
  811. <para>Source and target rectangles shall remain unchanged across
  812. closing and reopening a device, such that piping data into or out of a
  813. device will work without special preparations. More advanced
  814. applications should ensure the parameters are suitable before starting
  815. I/O.</para>
  816. <example>
  817. <title>Resetting the cropping parameters</title>
  818. <para>(A video capture device is assumed; change
  819. <constant>V4L2_BUF_TYPE_VIDEO_CAPTURE</constant> for other
  820. devices.)</para>
  821. <programlisting>
  822. &v4l2-cropcap; cropcap;
  823. &v4l2-crop; crop;
  824. memset (&amp;cropcap, 0, sizeof (cropcap));
  825. cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  826. if (-1 == ioctl (fd, &VIDIOC-CROPCAP;, &amp;cropcap)) {
  827. perror ("VIDIOC_CROPCAP");
  828. exit (EXIT_FAILURE);
  829. }
  830. memset (&amp;crop, 0, sizeof (crop));
  831. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  832. crop.c = cropcap.defrect;
  833. /* Ignore if cropping is not supported (EINVAL). */
  834. if (-1 == ioctl (fd, &VIDIOC-S-CROP;, &amp;crop)
  835. &amp;&amp; errno != EINVAL) {
  836. perror ("VIDIOC_S_CROP");
  837. exit (EXIT_FAILURE);
  838. }
  839. </programlisting>
  840. </example>
  841. <example>
  842. <title>Simple downscaling</title>
  843. <para>(A video capture device is assumed.)</para>
  844. <programlisting>
  845. &v4l2-cropcap; cropcap;
  846. &v4l2-format; format;
  847. reset_cropping_parameters ();
  848. /* Scale down to 1/4 size of full picture. */
  849. memset (&amp;format, 0, sizeof (format)); /* defaults */
  850. format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  851. format.fmt.pix.width = cropcap.defrect.width &gt;&gt; 1;
  852. format.fmt.pix.height = cropcap.defrect.height &gt;&gt; 1;
  853. format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  854. if (-1 == ioctl (fd, &VIDIOC-S-FMT;, &amp;format)) {
  855. perror ("VIDIOC_S_FORMAT");
  856. exit (EXIT_FAILURE);
  857. }
  858. /* We could check the actual image size now, the actual scaling factor
  859. or if the driver can scale at all. */
  860. </programlisting>
  861. </example>
  862. <example>
  863. <title>Selecting an output area</title>
  864. <programlisting>
  865. &v4l2-cropcap; cropcap;
  866. &v4l2-crop; crop;
  867. memset (&amp;cropcap, 0, sizeof (cropcap));
  868. cropcap.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  869. if (-1 == ioctl (fd, VIDIOC_CROPCAP;, &amp;cropcap)) {
  870. perror ("VIDIOC_CROPCAP");
  871. exit (EXIT_FAILURE);
  872. }
  873. memset (&amp;crop, 0, sizeof (crop));
  874. crop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  875. crop.c = cropcap.defrect;
  876. /* Scale the width and height to 50 % of their original size
  877. and center the output. */
  878. crop.c.width /= 2;
  879. crop.c.height /= 2;
  880. crop.c.left += crop.c.width / 2;
  881. crop.c.top += crop.c.height / 2;
  882. /* Ignore if cropping is not supported (EINVAL). */
  883. if (-1 == ioctl (fd, VIDIOC_S_CROP, &amp;crop)
  884. &amp;&amp; errno != EINVAL) {
  885. perror ("VIDIOC_S_CROP");
  886. exit (EXIT_FAILURE);
  887. }
  888. </programlisting>
  889. </example>
  890. <example>
  891. <title>Current scaling factor and pixel aspect</title>
  892. <para>(A video capture device is assumed.)</para>
  893. <programlisting>
  894. &v4l2-cropcap; cropcap;
  895. &v4l2-crop; crop;
  896. &v4l2-format; format;
  897. double hscale, vscale;
  898. double aspect;
  899. int dwidth, dheight;
  900. memset (&amp;cropcap, 0, sizeof (cropcap));
  901. cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  902. if (-1 == ioctl (fd, &VIDIOC-CROPCAP;, &amp;cropcap)) {
  903. perror ("VIDIOC_CROPCAP");
  904. exit (EXIT_FAILURE);
  905. }
  906. memset (&amp;crop, 0, sizeof (crop));
  907. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  908. if (-1 == ioctl (fd, &VIDIOC-G-CROP;, &amp;crop)) {
  909. if (errno != EINVAL) {
  910. perror ("VIDIOC_G_CROP");
  911. exit (EXIT_FAILURE);
  912. }
  913. /* Cropping not supported. */
  914. crop.c = cropcap.defrect;
  915. }
  916. memset (&amp;format, 0, sizeof (format));
  917. format.fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  918. if (-1 == ioctl (fd, &VIDIOC-G-FMT;, &amp;format)) {
  919. perror ("VIDIOC_G_FMT");
  920. exit (EXIT_FAILURE);
  921. }
  922. /* The scaling applied by the driver. */
  923. hscale = format.fmt.pix.width / (double) crop.c.width;
  924. vscale = format.fmt.pix.height / (double) crop.c.height;
  925. aspect = cropcap.pixelaspect.numerator /
  926. (double) cropcap.pixelaspect.denominator;
  927. aspect = aspect * hscale / vscale;
  928. /* Devices following ITU-R BT.601 do not capture
  929. square pixels. For playback on a computer monitor
  930. we should scale the images to this size. */
  931. dwidth = format.fmt.pix.width / aspect;
  932. dheight = format.fmt.pix.height;
  933. </programlisting>
  934. </example>
  935. </section>
  936. </section>
  937. <section id="streaming-par">
  938. <title>Streaming Parameters</title>
  939. <para>Streaming parameters are intended to optimize the video
  940. capture process as well as I/O. Presently applications can request a
  941. high quality capture mode with the &VIDIOC-S-PARM; ioctl.</para>
  942. <para>The current video standard determines a nominal number of
  943. frames per second. If less than this number of frames is to be
  944. captured or output, applications can request frame skipping or
  945. duplicating on the driver side. This is especially useful when using
  946. the &func-read; or &func-write;, which are not augmented by timestamps
  947. or sequence counters, and to avoid unneccessary data copying.</para>
  948. <para>Finally these ioctls can be used to determine the number of
  949. buffers used internally by a driver in read/write mode. For
  950. implications see the section discussing the &func-read;
  951. function.</para>
  952. <para>To get and set the streaming parameters applications call
  953. the &VIDIOC-G-PARM; and &VIDIOC-S-PARM; ioctl, respectively. They take
  954. a pointer to a &v4l2-streamparm;, which contains a union holding
  955. separate parameters for input and output devices.</para>
  956. <para>These ioctls are optional, drivers need not implement
  957. them. If so, they return the &EINVAL;.</para>
  958. </section>
  959. <!--
  960. Local Variables:
  961. mode: sgml
  962. sgml-parent-document: "v4l2.sgml"
  963. indent-tabs-mode: nil
  964. End:
  965. -->