xref: /openbmc/linux/Documentation/userspace-api/media/v4l/crop.rst (revision 54f38fcae536ea202ce7d6a359521492fba30c1f)
1*54f38fcaSMauro Carvalho Chehab.. Permission is granted to copy, distribute and/or modify this
2*54f38fcaSMauro Carvalho Chehab.. document under the terms of the GNU Free Documentation License,
3*54f38fcaSMauro Carvalho Chehab.. Version 1.1 or any later version published by the Free Software
4*54f38fcaSMauro Carvalho Chehab.. Foundation, with no Invariant Sections, no Front-Cover Texts
5*54f38fcaSMauro Carvalho Chehab.. and no Back-Cover Texts. A copy of the license is included at
6*54f38fcaSMauro Carvalho Chehab.. Documentation/userspace-api/media/fdl-appendix.rst.
7*54f38fcaSMauro Carvalho Chehab..
8*54f38fcaSMauro Carvalho Chehab.. TODO: replace it to GFDL-1.1-or-later WITH no-invariant-sections
9*54f38fcaSMauro Carvalho Chehab
10*54f38fcaSMauro Carvalho Chehab.. _crop:
11*54f38fcaSMauro Carvalho Chehab
12*54f38fcaSMauro Carvalho Chehab*****************************************************
13*54f38fcaSMauro Carvalho ChehabImage Cropping, Insertion and Scaling -- the CROP API
14*54f38fcaSMauro Carvalho Chehab*****************************************************
15*54f38fcaSMauro Carvalho Chehab
16*54f38fcaSMauro Carvalho Chehab.. note::
17*54f38fcaSMauro Carvalho Chehab
18*54f38fcaSMauro Carvalho Chehab   The CROP API is mostly superseded by the newer :ref:`SELECTION API
19*54f38fcaSMauro Carvalho Chehab   <selection-api>`. The new API should be preferred in most cases,
20*54f38fcaSMauro Carvalho Chehab   with the exception of pixel aspect ratio detection, which is
21*54f38fcaSMauro Carvalho Chehab   implemented by :ref:`VIDIOC_CROPCAP <VIDIOC_CROPCAP>` and has no
22*54f38fcaSMauro Carvalho Chehab   equivalent in the SELECTION API. See :ref:`selection-vs-crop` for a
23*54f38fcaSMauro Carvalho Chehab   comparison of the two APIs.
24*54f38fcaSMauro Carvalho Chehab
25*54f38fcaSMauro Carvalho ChehabSome video capture devices can sample a subsection of the picture and
26*54f38fcaSMauro Carvalho Chehabshrink or enlarge it to an image of arbitrary size. We call these
27*54f38fcaSMauro Carvalho Chehababilities cropping and scaling. Some video output devices can scale an
28*54f38fcaSMauro Carvalho Chehabimage up or down and insert it at an arbitrary scan line and horizontal
29*54f38fcaSMauro Carvalho Chehaboffset into a video signal.
30*54f38fcaSMauro Carvalho Chehab
31*54f38fcaSMauro Carvalho ChehabApplications can use the following API to select an area in the video
32*54f38fcaSMauro Carvalho Chehabsignal, query the default area and the hardware limits.
33*54f38fcaSMauro Carvalho Chehab
34*54f38fcaSMauro Carvalho Chehab.. note::
35*54f38fcaSMauro Carvalho Chehab
36*54f38fcaSMauro Carvalho Chehab   Despite their name, the :ref:`VIDIOC_CROPCAP <VIDIOC_CROPCAP>`,
37*54f38fcaSMauro Carvalho Chehab   :ref:`VIDIOC_G_CROP <VIDIOC_G_CROP>` and :ref:`VIDIOC_S_CROP
38*54f38fcaSMauro Carvalho Chehab   <VIDIOC_G_CROP>` ioctls apply to input as well as output devices.
39*54f38fcaSMauro Carvalho Chehab
40*54f38fcaSMauro Carvalho ChehabScaling requires a source and a target. On a video capture or overlay
41*54f38fcaSMauro Carvalho Chehabdevice the source is the video signal, and the cropping ioctls determine
42*54f38fcaSMauro Carvalho Chehabthe area actually sampled. The target are images read by the application
43*54f38fcaSMauro Carvalho Chehabor overlaid onto the graphics screen. Their size (and position for an
44*54f38fcaSMauro Carvalho Chehaboverlay) is negotiated with the :ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>`
45*54f38fcaSMauro Carvalho Chehaband :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctls.
46*54f38fcaSMauro Carvalho Chehab
47*54f38fcaSMauro Carvalho ChehabOn a video output device the source are the images passed in by the
48*54f38fcaSMauro Carvalho Chehabapplication, and their size is again negotiated with the
49*54f38fcaSMauro Carvalho Chehab:ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>` and :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>`
50*54f38fcaSMauro Carvalho Chehabioctls, or may be encoded in a compressed video stream. The target is
51*54f38fcaSMauro Carvalho Chehabthe video signal, and the cropping ioctls determine the area where the
52*54f38fcaSMauro Carvalho Chehabimages are inserted.
53*54f38fcaSMauro Carvalho Chehab
54*54f38fcaSMauro Carvalho ChehabSource and target rectangles are defined even if the device does not
55*54f38fcaSMauro Carvalho Chehabsupport scaling or the :ref:`VIDIOC_G_CROP <VIDIOC_G_CROP>` and
56*54f38fcaSMauro Carvalho Chehab:ref:`VIDIOC_S_CROP <VIDIOC_G_CROP>` ioctls. Their size (and position
57*54f38fcaSMauro Carvalho Chehabwhere applicable) will be fixed in this case.
58*54f38fcaSMauro Carvalho Chehab
59*54f38fcaSMauro Carvalho Chehab.. note::
60*54f38fcaSMauro Carvalho Chehab
61*54f38fcaSMauro Carvalho Chehab   All capture and output devices that support the CROP or SELECTION
62*54f38fcaSMauro Carvalho Chehab   API will also support the :ref:`VIDIOC_CROPCAP <VIDIOC_CROPCAP>`
63*54f38fcaSMauro Carvalho Chehab   ioctl.
64*54f38fcaSMauro Carvalho Chehab
65*54f38fcaSMauro Carvalho ChehabCropping Structures
66*54f38fcaSMauro Carvalho Chehab===================
67*54f38fcaSMauro Carvalho Chehab
68*54f38fcaSMauro Carvalho Chehab
69*54f38fcaSMauro Carvalho Chehab.. _crop-scale:
70*54f38fcaSMauro Carvalho Chehab
71*54f38fcaSMauro Carvalho Chehab.. kernel-figure:: crop.svg
72*54f38fcaSMauro Carvalho Chehab    :alt:    crop.svg
73*54f38fcaSMauro Carvalho Chehab    :align:  center
74*54f38fcaSMauro Carvalho Chehab
75*54f38fcaSMauro Carvalho Chehab    Image Cropping, Insertion and Scaling
76*54f38fcaSMauro Carvalho Chehab
77*54f38fcaSMauro Carvalho Chehab    The cropping, insertion and scaling process
78*54f38fcaSMauro Carvalho Chehab
79*54f38fcaSMauro Carvalho Chehab
80*54f38fcaSMauro Carvalho Chehab
81*54f38fcaSMauro Carvalho ChehabFor capture devices the coordinates of the top left corner, width and
82*54f38fcaSMauro Carvalho Chehabheight of the area which can be sampled is given by the ``bounds``
83*54f38fcaSMauro Carvalho Chehabsubstructure of the struct :c:type:`v4l2_cropcap` returned
84*54f38fcaSMauro Carvalho Chehabby the :ref:`VIDIOC_CROPCAP <VIDIOC_CROPCAP>` ioctl. To support a wide
85*54f38fcaSMauro Carvalho Chehabrange of hardware this specification does not define an origin or units.
86*54f38fcaSMauro Carvalho ChehabHowever by convention drivers should horizontally count unscaled samples
87*54f38fcaSMauro Carvalho Chehabrelative to 0H (the leading edge of the horizontal sync pulse, see
88*54f38fcaSMauro Carvalho Chehab:ref:`vbi-hsync`). Vertically ITU-R line numbers of the first field
89*54f38fcaSMauro Carvalho Chehab(see ITU R-525 line numbering for :ref:`525 lines <vbi-525>` and for
90*54f38fcaSMauro Carvalho Chehab:ref:`625 lines <vbi-625>`), multiplied by two if the driver
91*54f38fcaSMauro Carvalho Chehabcan capture both fields.
92*54f38fcaSMauro Carvalho Chehab
93*54f38fcaSMauro Carvalho ChehabThe top left corner, width and height of the source rectangle, that is
94*54f38fcaSMauro Carvalho Chehabthe area actually sampled, is given by struct
95*54f38fcaSMauro Carvalho Chehab:c:type:`v4l2_crop` using the same coordinate system as
96*54f38fcaSMauro Carvalho Chehabstruct :c:type:`v4l2_cropcap`. Applications can use the
97*54f38fcaSMauro Carvalho Chehab:ref:`VIDIOC_G_CROP <VIDIOC_G_CROP>` and :ref:`VIDIOC_S_CROP <VIDIOC_G_CROP>`
98*54f38fcaSMauro Carvalho Chehabioctls to get and set this rectangle. It must lie completely within the
99*54f38fcaSMauro Carvalho Chehabcapture boundaries and the driver may further adjust the requested size
100*54f38fcaSMauro Carvalho Chehaband/or position according to hardware limitations.
101*54f38fcaSMauro Carvalho Chehab
102*54f38fcaSMauro Carvalho ChehabEach capture device has a default source rectangle, given by the
103*54f38fcaSMauro Carvalho Chehab``defrect`` substructure of struct
104*54f38fcaSMauro Carvalho Chehab:c:type:`v4l2_cropcap`. The center of this rectangle
105*54f38fcaSMauro Carvalho Chehabshall align with the center of the active picture area of the video
106*54f38fcaSMauro Carvalho Chehabsignal, and cover what the driver writer considers the complete picture.
107*54f38fcaSMauro Carvalho ChehabDrivers shall reset the source rectangle to the default when the driver
108*54f38fcaSMauro Carvalho Chehabis first loaded, but not later.
109*54f38fcaSMauro Carvalho Chehab
110*54f38fcaSMauro Carvalho ChehabFor output devices these structures and ioctls are used accordingly,
111*54f38fcaSMauro Carvalho Chehabdefining the *target* rectangle where the images will be inserted into
112*54f38fcaSMauro Carvalho Chehabthe video signal.
113*54f38fcaSMauro Carvalho Chehab
114*54f38fcaSMauro Carvalho Chehab
115*54f38fcaSMauro Carvalho ChehabScaling Adjustments
116*54f38fcaSMauro Carvalho Chehab===================
117*54f38fcaSMauro Carvalho Chehab
118*54f38fcaSMauro Carvalho ChehabVideo hardware can have various cropping, insertion and scaling
119*54f38fcaSMauro Carvalho Chehablimitations. It may only scale up or down, support only discrete scaling
120*54f38fcaSMauro Carvalho Chehabfactors, or have different scaling abilities in horizontal and vertical
121*54f38fcaSMauro Carvalho Chehabdirection. Also it may not support scaling at all. At the same time the
122*54f38fcaSMauro Carvalho Chehabstruct :c:type:`v4l2_crop` rectangle may have to be aligned,
123*54f38fcaSMauro Carvalho Chehaband both the source and target rectangles may have arbitrary upper and
124*54f38fcaSMauro Carvalho Chehablower size limits. In particular the maximum ``width`` and ``height`` in
125*54f38fcaSMauro Carvalho Chehabstruct :c:type:`v4l2_crop` may be smaller than the struct
126*54f38fcaSMauro Carvalho Chehab:c:type:`v4l2_cropcap`. ``bounds`` area. Therefore, as
127*54f38fcaSMauro Carvalho Chehabusual, drivers are expected to adjust the requested parameters and
128*54f38fcaSMauro Carvalho Chehabreturn the actual values selected.
129*54f38fcaSMauro Carvalho Chehab
130*54f38fcaSMauro Carvalho ChehabApplications can change the source or the target rectangle first, as
131*54f38fcaSMauro Carvalho Chehabthey may prefer a particular image size or a certain area in the video
132*54f38fcaSMauro Carvalho Chehabsignal. If the driver has to adjust both to satisfy hardware
133*54f38fcaSMauro Carvalho Chehablimitations, the last requested rectangle shall take priority, and the
134*54f38fcaSMauro Carvalho Chehabdriver should preferably adjust the opposite one. The
135*54f38fcaSMauro Carvalho Chehab:ref:`VIDIOC_TRY_FMT <VIDIOC_G_FMT>` ioctl however shall not change
136*54f38fcaSMauro Carvalho Chehabthe driver state and therefore only adjust the requested rectangle.
137*54f38fcaSMauro Carvalho Chehab
138*54f38fcaSMauro Carvalho ChehabSuppose scaling on a video capture device is restricted to a factor 1:1
139*54f38fcaSMauro Carvalho Chehabor 2:1 in either direction and the target image size must be a multiple
140*54f38fcaSMauro Carvalho Chehabof 16 × 16 pixels. The source cropping rectangle is set to defaults,
141*54f38fcaSMauro Carvalho Chehabwhich are also the upper limit in this example, of 640 × 400 pixels at
142*54f38fcaSMauro Carvalho Chehaboffset 0, 0. An application requests an image size of 300 × 225 pixels,
143*54f38fcaSMauro Carvalho Chehabassuming video will be scaled down from the "full picture" accordingly.
144*54f38fcaSMauro Carvalho ChehabThe driver sets the image size to the closest possible values 304 × 224,
145*54f38fcaSMauro Carvalho Chehabthen chooses the cropping rectangle closest to the requested size, that
146*54f38fcaSMauro Carvalho Chehabis 608 × 224 (224 × 2:1 would exceed the limit 400). The offset 0, 0 is
147*54f38fcaSMauro Carvalho Chehabstill valid, thus unmodified. Given the default cropping rectangle
148*54f38fcaSMauro Carvalho Chehabreported by :ref:`VIDIOC_CROPCAP <VIDIOC_CROPCAP>` the application can
149*54f38fcaSMauro Carvalho Chehabeasily propose another offset to center the cropping rectangle.
150*54f38fcaSMauro Carvalho Chehab
151*54f38fcaSMauro Carvalho ChehabNow the application may insist on covering an area using a picture
152*54f38fcaSMauro Carvalho Chehabaspect ratio closer to the original request, so it asks for a cropping
153*54f38fcaSMauro Carvalho Chehabrectangle of 608 × 456 pixels. The present scaling factors limit
154*54f38fcaSMauro Carvalho Chehabcropping to 640 × 384, so the driver returns the cropping size 608 × 384
155*54f38fcaSMauro Carvalho Chehaband adjusts the image size to closest possible 304 × 192.
156*54f38fcaSMauro Carvalho Chehab
157*54f38fcaSMauro Carvalho Chehab
158*54f38fcaSMauro Carvalho ChehabExamples
159*54f38fcaSMauro Carvalho Chehab========
160*54f38fcaSMauro Carvalho Chehab
161*54f38fcaSMauro Carvalho ChehabSource and target rectangles shall remain unchanged across closing and
162*54f38fcaSMauro Carvalho Chehabreopening a device, such that piping data into or out of a device will
163*54f38fcaSMauro Carvalho Chehabwork without special preparations. More advanced applications should
164*54f38fcaSMauro Carvalho Chehabensure the parameters are suitable before starting I/O.
165*54f38fcaSMauro Carvalho Chehab
166*54f38fcaSMauro Carvalho Chehab.. note::
167*54f38fcaSMauro Carvalho Chehab
168*54f38fcaSMauro Carvalho Chehab   On the next two examples, a video capture device is assumed;
169*54f38fcaSMauro Carvalho Chehab   change ``V4L2_BUF_TYPE_VIDEO_CAPTURE`` for other types of device.
170*54f38fcaSMauro Carvalho Chehab
171*54f38fcaSMauro Carvalho ChehabExample: Resetting the cropping parameters
172*54f38fcaSMauro Carvalho Chehab==========================================
173*54f38fcaSMauro Carvalho Chehab
174*54f38fcaSMauro Carvalho Chehab.. code-block:: c
175*54f38fcaSMauro Carvalho Chehab
176*54f38fcaSMauro Carvalho Chehab    struct v4l2_cropcap cropcap;
177*54f38fcaSMauro Carvalho Chehab    struct v4l2_crop crop;
178*54f38fcaSMauro Carvalho Chehab
179*54f38fcaSMauro Carvalho Chehab    memset (&cropcap, 0, sizeof (cropcap));
180*54f38fcaSMauro Carvalho Chehab    cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
181*54f38fcaSMauro Carvalho Chehab
182*54f38fcaSMauro Carvalho Chehab    if (-1 == ioctl (fd, VIDIOC_CROPCAP, &cropcap)) {
183*54f38fcaSMauro Carvalho Chehab	perror ("VIDIOC_CROPCAP");
184*54f38fcaSMauro Carvalho Chehab	exit (EXIT_FAILURE);
185*54f38fcaSMauro Carvalho Chehab    }
186*54f38fcaSMauro Carvalho Chehab
187*54f38fcaSMauro Carvalho Chehab    memset (&crop, 0, sizeof (crop));
188*54f38fcaSMauro Carvalho Chehab    crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
189*54f38fcaSMauro Carvalho Chehab    crop.c = cropcap.defrect;
190*54f38fcaSMauro Carvalho Chehab
191*54f38fcaSMauro Carvalho Chehab    /* Ignore if cropping is not supported (EINVAL). */
192*54f38fcaSMauro Carvalho Chehab
193*54f38fcaSMauro Carvalho Chehab    if (-1 == ioctl (fd, VIDIOC_S_CROP, &crop)
194*54f38fcaSMauro Carvalho Chehab	&& errno != EINVAL) {
195*54f38fcaSMauro Carvalho Chehab	perror ("VIDIOC_S_CROP");
196*54f38fcaSMauro Carvalho Chehab	exit (EXIT_FAILURE);
197*54f38fcaSMauro Carvalho Chehab    }
198*54f38fcaSMauro Carvalho Chehab
199*54f38fcaSMauro Carvalho Chehab
200*54f38fcaSMauro Carvalho ChehabExample: Simple downscaling
201*54f38fcaSMauro Carvalho Chehab===========================
202*54f38fcaSMauro Carvalho Chehab
203*54f38fcaSMauro Carvalho Chehab.. code-block:: c
204*54f38fcaSMauro Carvalho Chehab
205*54f38fcaSMauro Carvalho Chehab    struct v4l2_cropcap cropcap;
206*54f38fcaSMauro Carvalho Chehab    struct v4l2_format format;
207*54f38fcaSMauro Carvalho Chehab
208*54f38fcaSMauro Carvalho Chehab    reset_cropping_parameters ();
209*54f38fcaSMauro Carvalho Chehab
210*54f38fcaSMauro Carvalho Chehab    /* Scale down to 1/4 size of full picture. */
211*54f38fcaSMauro Carvalho Chehab
212*54f38fcaSMauro Carvalho Chehab    memset (&format, 0, sizeof (format)); /* defaults */
213*54f38fcaSMauro Carvalho Chehab
214*54f38fcaSMauro Carvalho Chehab    format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
215*54f38fcaSMauro Carvalho Chehab
216*54f38fcaSMauro Carvalho Chehab    format.fmt.pix.width = cropcap.defrect.width >> 1;
217*54f38fcaSMauro Carvalho Chehab    format.fmt.pix.height = cropcap.defrect.height >> 1;
218*54f38fcaSMauro Carvalho Chehab    format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
219*54f38fcaSMauro Carvalho Chehab
220*54f38fcaSMauro Carvalho Chehab    if (-1 == ioctl (fd, VIDIOC_S_FMT, &format)) {
221*54f38fcaSMauro Carvalho Chehab	perror ("VIDIOC_S_FORMAT");
222*54f38fcaSMauro Carvalho Chehab	exit (EXIT_FAILURE);
223*54f38fcaSMauro Carvalho Chehab    }
224*54f38fcaSMauro Carvalho Chehab
225*54f38fcaSMauro Carvalho Chehab    /* We could check the actual image size now, the actual scaling factor
226*54f38fcaSMauro Carvalho Chehab       or if the driver can scale at all. */
227*54f38fcaSMauro Carvalho Chehab
228*54f38fcaSMauro Carvalho ChehabExample: Selecting an output area
229*54f38fcaSMauro Carvalho Chehab=================================
230*54f38fcaSMauro Carvalho Chehab
231*54f38fcaSMauro Carvalho Chehab.. note:: This example assumes an output device.
232*54f38fcaSMauro Carvalho Chehab
233*54f38fcaSMauro Carvalho Chehab.. code-block:: c
234*54f38fcaSMauro Carvalho Chehab
235*54f38fcaSMauro Carvalho Chehab    struct v4l2_cropcap cropcap;
236*54f38fcaSMauro Carvalho Chehab    struct v4l2_crop crop;
237*54f38fcaSMauro Carvalho Chehab
238*54f38fcaSMauro Carvalho Chehab    memset (&cropcap, 0, sizeof (cropcap));
239*54f38fcaSMauro Carvalho Chehab    cropcap.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
240*54f38fcaSMauro Carvalho Chehab
241*54f38fcaSMauro Carvalho Chehab    if (-1 == ioctl (fd, VIDIOC_CROPCAP;, &cropcap)) {
242*54f38fcaSMauro Carvalho Chehab	perror ("VIDIOC_CROPCAP");
243*54f38fcaSMauro Carvalho Chehab	exit (EXIT_FAILURE);
244*54f38fcaSMauro Carvalho Chehab    }
245*54f38fcaSMauro Carvalho Chehab
246*54f38fcaSMauro Carvalho Chehab    memset (&crop, 0, sizeof (crop));
247*54f38fcaSMauro Carvalho Chehab
248*54f38fcaSMauro Carvalho Chehab    crop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
249*54f38fcaSMauro Carvalho Chehab    crop.c = cropcap.defrect;
250*54f38fcaSMauro Carvalho Chehab
251*54f38fcaSMauro Carvalho Chehab    /* Scale the width and height to 50 % of their original size
252*54f38fcaSMauro Carvalho Chehab       and center the output. */
253*54f38fcaSMauro Carvalho Chehab
254*54f38fcaSMauro Carvalho Chehab    crop.c.width /= 2;
255*54f38fcaSMauro Carvalho Chehab    crop.c.height /= 2;
256*54f38fcaSMauro Carvalho Chehab    crop.c.left += crop.c.width / 2;
257*54f38fcaSMauro Carvalho Chehab    crop.c.top += crop.c.height / 2;
258*54f38fcaSMauro Carvalho Chehab
259*54f38fcaSMauro Carvalho Chehab    /* Ignore if cropping is not supported (EINVAL). */
260*54f38fcaSMauro Carvalho Chehab
261*54f38fcaSMauro Carvalho Chehab    if (-1 == ioctl (fd, VIDIOC_S_CROP, &crop)
262*54f38fcaSMauro Carvalho Chehab	&& errno != EINVAL) {
263*54f38fcaSMauro Carvalho Chehab	perror ("VIDIOC_S_CROP");
264*54f38fcaSMauro Carvalho Chehab	exit (EXIT_FAILURE);
265*54f38fcaSMauro Carvalho Chehab    }
266*54f38fcaSMauro Carvalho Chehab
267*54f38fcaSMauro Carvalho ChehabExample: Current scaling factor and pixel aspect
268*54f38fcaSMauro Carvalho Chehab================================================
269*54f38fcaSMauro Carvalho Chehab
270*54f38fcaSMauro Carvalho Chehab.. note:: This example assumes a video capture device.
271*54f38fcaSMauro Carvalho Chehab
272*54f38fcaSMauro Carvalho Chehab.. code-block:: c
273*54f38fcaSMauro Carvalho Chehab
274*54f38fcaSMauro Carvalho Chehab    struct v4l2_cropcap cropcap;
275*54f38fcaSMauro Carvalho Chehab    struct v4l2_crop crop;
276*54f38fcaSMauro Carvalho Chehab    struct v4l2_format format;
277*54f38fcaSMauro Carvalho Chehab    double hscale, vscale;
278*54f38fcaSMauro Carvalho Chehab    double aspect;
279*54f38fcaSMauro Carvalho Chehab    int dwidth, dheight;
280*54f38fcaSMauro Carvalho Chehab
281*54f38fcaSMauro Carvalho Chehab    memset (&cropcap, 0, sizeof (cropcap));
282*54f38fcaSMauro Carvalho Chehab    cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
283*54f38fcaSMauro Carvalho Chehab
284*54f38fcaSMauro Carvalho Chehab    if (-1 == ioctl (fd, VIDIOC_CROPCAP, &cropcap)) {
285*54f38fcaSMauro Carvalho Chehab	perror ("VIDIOC_CROPCAP");
286*54f38fcaSMauro Carvalho Chehab	exit (EXIT_FAILURE);
287*54f38fcaSMauro Carvalho Chehab    }
288*54f38fcaSMauro Carvalho Chehab
289*54f38fcaSMauro Carvalho Chehab    memset (&crop, 0, sizeof (crop));
290*54f38fcaSMauro Carvalho Chehab    crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
291*54f38fcaSMauro Carvalho Chehab
292*54f38fcaSMauro Carvalho Chehab    if (-1 == ioctl (fd, VIDIOC_G_CROP, &crop)) {
293*54f38fcaSMauro Carvalho Chehab	if (errno != EINVAL) {
294*54f38fcaSMauro Carvalho Chehab	    perror ("VIDIOC_G_CROP");
295*54f38fcaSMauro Carvalho Chehab	    exit (EXIT_FAILURE);
296*54f38fcaSMauro Carvalho Chehab	}
297*54f38fcaSMauro Carvalho Chehab
298*54f38fcaSMauro Carvalho Chehab	/* Cropping not supported. */
299*54f38fcaSMauro Carvalho Chehab	crop.c = cropcap.defrect;
300*54f38fcaSMauro Carvalho Chehab    }
301*54f38fcaSMauro Carvalho Chehab
302*54f38fcaSMauro Carvalho Chehab    memset (&format, 0, sizeof (format));
303*54f38fcaSMauro Carvalho Chehab    format.fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
304*54f38fcaSMauro Carvalho Chehab
305*54f38fcaSMauro Carvalho Chehab    if (-1 == ioctl (fd, VIDIOC_G_FMT, &format)) {
306*54f38fcaSMauro Carvalho Chehab	perror ("VIDIOC_G_FMT");
307*54f38fcaSMauro Carvalho Chehab	exit (EXIT_FAILURE);
308*54f38fcaSMauro Carvalho Chehab    }
309*54f38fcaSMauro Carvalho Chehab
310*54f38fcaSMauro Carvalho Chehab    /* The scaling applied by the driver. */
311*54f38fcaSMauro Carvalho Chehab
312*54f38fcaSMauro Carvalho Chehab    hscale = format.fmt.pix.width / (double) crop.c.width;
313*54f38fcaSMauro Carvalho Chehab    vscale = format.fmt.pix.height / (double) crop.c.height;
314*54f38fcaSMauro Carvalho Chehab
315*54f38fcaSMauro Carvalho Chehab    aspect = cropcap.pixelaspect.numerator /
316*54f38fcaSMauro Carvalho Chehab	 (double) cropcap.pixelaspect.denominator;
317*54f38fcaSMauro Carvalho Chehab    aspect = aspect * hscale / vscale;
318*54f38fcaSMauro Carvalho Chehab
319*54f38fcaSMauro Carvalho Chehab    /* Devices following ITU-R BT.601 do not capture
320*54f38fcaSMauro Carvalho Chehab       square pixels. For playback on a computer monitor
321*54f38fcaSMauro Carvalho Chehab       we should scale the images to this size. */
322*54f38fcaSMauro Carvalho Chehab
323*54f38fcaSMauro Carvalho Chehab    dwidth = format.fmt.pix.width / aspect;
324*54f38fcaSMauro Carvalho Chehab    dheight = format.fmt.pix.height;
325