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