1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> 2<HTML 3><HEAD 4><TITLE 5>Image Cropping, Insertion and Scaling</TITLE 6><META 7NAME="GENERATOR" 8CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK 9REL="HOME" 10TITLE="Video for Linux Two API Specification" 11HREF="book1.htm"><LINK 12REL="UP" 13TITLE="Common API Elements" 14HREF="c174.htm"><LINK 15REL="PREVIOUS" 16TITLE="Data Formats" 17HREF="x1859.htm"><LINK 18REL="NEXT" 19TITLE="Streaming Parameters" 20HREF="x2009.htm"></HEAD 21><BODY 22CLASS="SECTION" 23BGCOLOR="#FFFFFF" 24TEXT="#000000" 25LINK="#0000FF" 26VLINK="#840084" 27ALINK="#0000FF" 28><DIV 29CLASS="NAVHEADER" 30><TABLE 31SUMMARY="Header navigation table" 32WIDTH="100%" 33BORDER="0" 34CELLPADDING="0" 35CELLSPACING="0" 36><TR 37><TH 38COLSPAN="3" 39ALIGN="center" 40>Video for Linux Two API Specification: Revision 0.24</TH 41></TR 42><TR 43><TD 44WIDTH="10%" 45ALIGN="left" 46VALIGN="bottom" 47><A 48HREF="x1859.htm" 49ACCESSKEY="P" 50>Prev</A 51></TD 52><TD 53WIDTH="80%" 54ALIGN="center" 55VALIGN="bottom" 56>Chapter 1. Common API Elements</TD 57><TD 58WIDTH="10%" 59ALIGN="right" 60VALIGN="bottom" 61><A 62HREF="x2009.htm" 63ACCESSKEY="N" 64>Next</A 65></TD 66></TR 67></TABLE 68><HR 69ALIGN="LEFT" 70WIDTH="100%"></DIV 71><DIV 72CLASS="SECTION" 73><H1 74CLASS="SECTION" 75><A 76NAME="CROP" 77>1.11. Image Cropping, Insertion and Scaling</A 78></H1 79><P 80>Some video capture devices can sample a subsection of the 81picture and shrink or enlarge it to an image of arbitrary size. We 82call these abilities cropping and scaling. Some video output devices 83can scale an image up or down and insert it at an arbitrary scan line 84and horizontal offset into a video signal.</P 85><P 86>Applications can use the following API to select an area in 87the video signal, query the default area and the hardware limits. 88<SPAN 89CLASS="emphasis" 90><I 91CLASS="EMPHASIS" 92>Despite their name, the <A 93HREF="r7771.htm" 94><CODE 95CLASS="CONSTANT" 96>VIDIOC_CROPCAP</CODE 97></A 98>, <A 99HREF="r9994.htm" 100><CODE 101CLASS="CONSTANT" 102>VIDIOC_G_CROP</CODE 103></A 104> 105and <A 106HREF="r9994.htm" 107><CODE 108CLASS="CONSTANT" 109>VIDIOC_S_CROP</CODE 110></A 111> ioctls apply to input as well as output 112devices.</I 113></SPAN 114></P 115><P 116>Scaling requires a source and a target. On a video capture 117or overlay device the source is the video signal, and the cropping 118ioctls determine the area actually sampled. The target are images 119read by the application or overlaid onto the graphics screen. Their 120size (and position for an overlay) is negotiated with the 121<A 122HREF="r10944.htm" 123><CODE 124CLASS="CONSTANT" 125>VIDIOC_G_FMT</CODE 126></A 127> and <A 128HREF="r10944.htm" 129><CODE 130CLASS="CONSTANT" 131>VIDIOC_S_FMT</CODE 132></A 133> ioctls.</P 134><P 135>On a video output device the source are the images passed in 136by the application, and their size is again negotiated with the 137<CODE 138CLASS="CONSTANT" 139>VIDIOC_G/S_FMT</CODE 140> ioctls, or may be encoded in a 141compressed video stream. The target is the video signal, and the 142cropping ioctls determine the area where the images are 143inserted.</P 144><P 145>Source and target rectangles are defined even if the device 146does not support scaling or the <CODE 147CLASS="CONSTANT" 148>VIDIOC_G/S_CROP</CODE 149> 150ioctls. Their size (and position where applicable) will be fixed in 151this case. <SPAN 152CLASS="emphasis" 153><I 154CLASS="EMPHASIS" 155>All capture and output device must support the 156<CODE 157CLASS="CONSTANT" 158>VIDIOC_CROPCAP</CODE 159> ioctl such that applications can 160determine if scaling takes place.</I 161></SPAN 162></P 163><DIV 164CLASS="SECTION" 165><H2 166CLASS="SECTION" 167><A 168NAME="AEN1926" 169>1.11.1. Cropping Structures</A 170></H2 171><DIV 172CLASS="FIGURE" 173><A 174NAME="CROP-SCALE" 175></A 176><P 177><B 178>Figure 1-1. Image Cropping, Insertion and Scaling</B 179></P 180><DIV 181CLASS="MEDIAOBJECT" 182><P 183><IMG 184SRC="crop.gif"></P 185></DIV 186></DIV 187><P 188>For capture devices the coordinates of the top left 189corner, width and height of the area which can be sampled is given by 190the <CODE 191CLASS="STRUCTFIELD" 192>bounds</CODE 193> substructure of the 194struct <A 195HREF="r7771.htm#V4L2-CROPCAP" 196>v4l2_cropcap</A 197> returned by the <CODE 198CLASS="CONSTANT" 199>VIDIOC_CROPCAP</CODE 200> 201ioctl. To support a wide range of hardware this specification does not 202define an origin or units. However by convention drivers should 203horizontally count unscaled samples relative to 0H (the leading edge 204of the horizontal sync pulse, see <A 205HREF="x7013.htm#VBI-HSYNC" 206>Figure 4-1</A 207>). 208Vertically ITU-R line 209numbers of the first field (<A 210HREF="x7013.htm#VBI-525" 211>Figure 4-2</A 212>, <A 213HREF="x7013.htm#VBI-625" 214>Figure 4-3</A 215>), multiplied by two if the driver can capture both 216fields.</P 217><P 218>The top left corner, width and height of the source 219rectangle, that is the area actually sampled, is given by struct <A 220HREF="r9994.htm#V4L2-CROP" 221>v4l2_crop</A 222> 223using the same coordinate system as struct <A 224HREF="r7771.htm#V4L2-CROPCAP" 225>v4l2_cropcap</A 226>. Applications can 227use the <CODE 228CLASS="CONSTANT" 229>VIDIOC_G_CROP</CODE 230> and 231<CODE 232CLASS="CONSTANT" 233>VIDIOC_S_CROP</CODE 234> ioctls to get and set this 235rectangle. It must lie completely within the capture boundaries and 236the driver may further adjust the requested size and/or position 237according to hardware limitations.</P 238><P 239>Each capture device has a default source rectangle, given 240by the <CODE 241CLASS="STRUCTFIELD" 242>defrect</CODE 243> substructure of 244struct <A 245HREF="r7771.htm#V4L2-CROPCAP" 246>v4l2_cropcap</A 247>. The center of this rectangle shall align with the 248center of the active picture area of the video signal, and cover what 249the driver writer considers the complete picture. Drivers shall reset 250the source rectangle to the default when the driver is first loaded, 251but not later.</P 252><P 253>For output devices these structures and ioctls are used 254accordingly, defining the <SPAN 255CLASS="emphasis" 256><I 257CLASS="EMPHASIS" 258>target</I 259></SPAN 260> rectangle where 261the images will be inserted into the video signal.</P 262></DIV 263><DIV 264CLASS="SECTION" 265><H2 266CLASS="SECTION" 267><A 268NAME="AEN1954" 269>1.11.2. Scaling Adjustments</A 270></H2 271><P 272>Video hardware can have various cropping, insertion and 273scaling limitations. It may only scale up or down, support only 274discrete scaling factors, or have different scaling abilities in 275horizontal and vertical direction. Also it may not support scaling at 276all. At the same time the struct <A 277HREF="r9994.htm#V4L2-CROP" 278>v4l2_crop</A 279> rectangle may have to be 280aligned, and both the source and target rectangles may have arbitrary 281upper and lower size limits. In particular the maximum 282<CODE 283CLASS="STRUCTFIELD" 284>width</CODE 285> and <CODE 286CLASS="STRUCTFIELD" 287>height</CODE 288> 289in struct <A 290HREF="r9994.htm#V4L2-CROP" 291>v4l2_crop</A 292> may be smaller than the 293struct <A 294HREF="r7771.htm#V4L2-CROPCAP" 295>v4l2_cropcap</A 296>.<CODE 297CLASS="STRUCTFIELD" 298>bounds</CODE 299> area. Therefore, as 300usual, drivers are expected to adjust the requested parameters and 301return the actual values selected.</P 302><P 303>Applications can change the source or the target rectangle 304first, as they may prefer a particular image size or a certain area in 305the video signal. If the driver has to adjust both to satisfy hardware 306limitations, the last requested rectangle shall take priority, and the 307driver should preferably adjust the opposite one. The <A 308HREF="r10944.htm" 309><CODE 310CLASS="CONSTANT" 311>VIDIOC_TRY_FMT</CODE 312></A 313> 314ioctl however shall not change the driver state and therefore only 315adjust the requested rectangle.</P 316><P 317>Suppose scaling on a video capture device is restricted to 318a factor 1:1 or 2:1 in either direction and the target image size must 319be a multiple of 16 × 16 pixels. The source cropping 320rectangle is set to defaults, which are also the upper limit in this 321example, of 640 × 400 pixels at offset 0, 0. An 322application requests an image size of 300 × 225 323pixels, assuming video will be scaled down from the "full picture" 324accordingly. The driver sets the image size to the closest possible 325values 304 × 224, then chooses the cropping rectangle 326closest to the requested size, that is 608 × 224 327(224 × 2:1 would exceed the limit 400). The offset 3280, 0 is still valid, thus unmodified. Given the default cropping 329rectangle reported by <CODE 330CLASS="CONSTANT" 331>VIDIOC_CROPCAP</CODE 332> the 333application can easily propose another offset to center the cropping 334rectangle.</P 335><P 336>Now the application may insist on covering an area using a 337picture aspect ratio closer to the original request, so it asks for a 338cropping rectangle of 608 × 456 pixels. The present 339scaling factors limit cropping to 640 × 384, so the 340driver returns the cropping size 608 × 384 and adjusts 341the image size to closest possible 304 × 192.</P 342></DIV 343><DIV 344CLASS="SECTION" 345><H2 346CLASS="SECTION" 347><A 348NAME="AEN1969" 349>1.11.3. Examples</A 350></H2 351><P 352>Source and target rectangles shall remain unchanged across 353closing and reopening a device, such that piping data into or out of a 354device will work without special preparations. More advanced 355applications should ensure the parameters are suitable before starting 356I/O.</P 357><DIV 358CLASS="EXAMPLE" 359><A 360NAME="AEN1972" 361></A 362><P 363><B 364>Example 1-10. Resetting the cropping parameters</B 365></P 366><P 367>(A video capture device is assumed; change 368<CODE 369CLASS="CONSTANT" 370>V4L2_BUF_TYPE_VIDEO_CAPTURE</CODE 371> for other 372devices.)</P 373><PRE 374CLASS="PROGRAMLISTING" 375>struct <A 376HREF="r7771.htm#V4L2-CROPCAP" 377>v4l2_cropcap</A 378> cropcap; 379struct <A 380HREF="r9994.htm#V4L2-CROP" 381>v4l2_crop</A 382> crop; 383 384memset (&cropcap, 0, sizeof (cropcap)); 385cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 386 387if (-1 == ioctl (fd, <A 388HREF="r7771.htm" 389><CODE 390CLASS="CONSTANT" 391>VIDIOC_CROPCAP</CODE 392></A 393>, &cropcap)) { 394 perror ("VIDIOC_CROPCAP"); 395 exit (EXIT_FAILURE); 396} 397 398memset (&crop, 0, sizeof (crop)); 399crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 400crop.c = cropcap.defrect; 401 402/* Ignore if cropping is not supported (EINVAL). */ 403 404if (-1 == ioctl (fd, <A 405HREF="r9994.htm" 406><CODE 407CLASS="CONSTANT" 408>VIDIOC_S_CROP</CODE 409></A 410>, &crop) 411 && errno != EINVAL) { 412 perror ("VIDIOC_S_CROP"); 413 exit (EXIT_FAILURE); 414} 415 </PRE 416></DIV 417><DIV 418CLASS="EXAMPLE" 419><A 420NAME="AEN1983" 421></A 422><P 423><B 424>Example 1-11. Simple downscaling</B 425></P 426><P 427>(A video capture device is assumed.)</P 428><PRE 429CLASS="PROGRAMLISTING" 430>struct <A 431HREF="r7771.htm#V4L2-CROPCAP" 432>v4l2_cropcap</A 433> cropcap; 434struct <A 435HREF="r10944.htm#V4L2-FORMAT" 436>v4l2_format</A 437> format; 438 439reset_cropping_parameters (); 440 441/* Scale down to 1/4 size of full picture. */ 442 443memset (&format, 0, sizeof (format)); /* defaults */ 444 445format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 446 447format.fmt.pix.width = cropcap.defrect.width >> 1; 448format.fmt.pix.height = cropcap.defrect.height >> 1; 449format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; 450 451if (-1 == ioctl (fd, <A 452HREF="r10944.htm" 453><CODE 454CLASS="CONSTANT" 455>VIDIOC_S_FMT</CODE 456></A 457>, &format)) { 458 perror ("VIDIOC_S_FORMAT"); 459 exit (EXIT_FAILURE); 460} 461 462/* We could check the actual image size now, the actual scaling factor 463 or if the driver can scale at all. */ 464 </PRE 465></DIV 466><DIV 467CLASS="EXAMPLE" 468><A 469NAME="AEN1991" 470></A 471><P 472><B 473>Example 1-12. Selecting an output area</B 474></P 475><PRE 476CLASS="PROGRAMLISTING" 477>struct <A 478HREF="r7771.htm#V4L2-CROPCAP" 479>v4l2_cropcap</A 480> cropcap; 481struct <A 482HREF="r9994.htm#V4L2-CROP" 483>v4l2_crop</A 484> crop; 485 486memset (&cropcap, 0, sizeof (cropcap)); 487cropcap.type = V4L2_BUF_TYPE_VIDEO_OUTPUT; 488 489if (-1 == ioctl (fd, VIDIOC_CROPCAP, &cropcap)) { 490 perror ("VIDIOC_CROPCAP"); 491 exit (EXIT_FAILURE); 492} 493 494memset (&crop, 0, sizeof (crop)); 495 496crop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT; 497crop.c = cropcap.defrect; 498 499/* Scale the width and height to 50 % of their original size 500 and center the output. */ 501 502crop.c.width /= 2; 503crop.c.height /= 2; 504crop.c.left += crop.c.width / 2; 505crop.c.top += crop.c.height / 2; 506 507/* Ignore if cropping is not supported (EINVAL). */ 508 509if (-1 == ioctl (fd, VIDIOC_S_CROP, &crop) 510 && errno != EINVAL) { 511 perror ("VIDIOC_S_CROP"); 512 exit (EXIT_FAILURE); 513}</PRE 514></DIV 515><DIV 516CLASS="EXAMPLE" 517><A 518NAME="AEN1996" 519></A 520><P 521><B 522>Example 1-13. Current scaling factor and pixel aspect</B 523></P 524><P 525>(A video capture device is assumed.)</P 526><PRE 527CLASS="PROGRAMLISTING" 528>struct <A 529HREF="r7771.htm#V4L2-CROPCAP" 530>v4l2_cropcap</A 531> cropcap; 532struct <A 533HREF="r9994.htm#V4L2-CROP" 534>v4l2_crop</A 535> crop; 536struct <A 537HREF="r10944.htm#V4L2-FORMAT" 538>v4l2_format</A 539> format; 540double hscale, vscale; 541double aspect; 542int dwidth, dheight; 543 544memset (&cropcap, 0, sizeof (cropcap)); 545cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 546 547if (-1 == ioctl (fd, <A 548HREF="r7771.htm" 549><CODE 550CLASS="CONSTANT" 551>VIDIOC_CROPCAP</CODE 552></A 553>, &cropcap)) { 554 perror ("VIDIOC_CROPCAP"); 555 exit (EXIT_FAILURE); 556} 557 558memset (&crop, 0, sizeof (crop)); 559crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 560 561if (-1 == ioctl (fd, <A 562HREF="r9994.htm" 563><CODE 564CLASS="CONSTANT" 565>VIDIOC_G_CROP</CODE 566></A 567>, &crop)) { 568 if (errno != EINVAL) { 569 perror ("VIDIOC_G_CROP"); 570 exit (EXIT_FAILURE); 571 } 572 573 /* Cropping not supported. */ 574 crop.c = cropcap.defrect; 575} 576 577memset (&format, 0, sizeof (format)); 578format.fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 579 580if (-1 == ioctl (fd, <A 581HREF="r10944.htm" 582><CODE 583CLASS="CONSTANT" 584>VIDIOC_G_FMT</CODE 585></A 586>, &format)) { 587 perror ("VIDIOC_G_FMT"); 588 exit (EXIT_FAILURE); 589} 590 591/* The scaling applied by the driver. */ 592 593hscale = format.fmt.pix.width / (double) crop.c.width; 594vscale = format.fmt.pix.height / (double) crop.c.height; 595 596aspect = cropcap.pixelaspect.numerator / 597 (double) cropcap.pixelaspect.denominator; 598aspect = aspect * hscale / vscale; 599 600/* Devices following ITU-R BT.601 do not capture 601 square pixels. For playback on a computer monitor 602 we should scale the images to this size. */ 603 604dwidth = format.fmt.pix.width / aspect; 605dheight = format.fmt.pix.height; 606 </PRE 607></DIV 608></DIV 609></DIV 610><DIV 611CLASS="NAVFOOTER" 612><HR 613ALIGN="LEFT" 614WIDTH="100%"><TABLE 615SUMMARY="Footer navigation table" 616WIDTH="100%" 617BORDER="0" 618CELLPADDING="0" 619CELLSPACING="0" 620><TR 621><TD 622WIDTH="33%" 623ALIGN="left" 624VALIGN="top" 625><A 626HREF="x1859.htm" 627ACCESSKEY="P" 628>Prev</A 629></TD 630><TD 631WIDTH="34%" 632ALIGN="center" 633VALIGN="top" 634><A 635HREF="book1.htm" 636ACCESSKEY="H" 637>Home</A 638></TD 639><TD 640WIDTH="33%" 641ALIGN="right" 642VALIGN="top" 643><A 644HREF="x2009.htm" 645ACCESSKEY="N" 646>Next</A 647></TD 648></TR 649><TR 650><TD 651WIDTH="33%" 652ALIGN="left" 653VALIGN="top" 654>Data Formats</TD 655><TD 656WIDTH="34%" 657ALIGN="center" 658VALIGN="top" 659><A 660HREF="c174.htm" 661ACCESSKEY="U" 662>Up</A 663></TD 664><TD 665WIDTH="33%" 666ALIGN="right" 667VALIGN="top" 668>Streaming Parameters</TD 669></TR 670></TABLE 671></DIV 672></BODY 673></HTML 674> 675