xref: /aosp_15_r20/external/libyuv/docs/rotation.md (revision 4e366538070a3a6c5c163c31b791eab742e1657a)
1*4e366538SXin Li# Introduction
2*4e366538SXin Li
3*4e366538SXin LiRotation by multiplies of 90 degrees allows mobile devices to rotate webcams from landscape to portrait.  The higher level functions ConvertToI420 and ConvertToARGB allow rotation of any format.  Optimized functionality is supported for I420, ARGB, NV12 and NV21.
4*4e366538SXin Li
5*4e366538SXin Li# ConvertToI420
6*4e366538SXin Li
7*4e366538SXin Li    int ConvertToI420(const uint8* src_frame, size_t src_size,
8*4e366538SXin Li                      uint8* dst_y, int dst_stride_y,
9*4e366538SXin Li                      uint8* dst_u, int dst_stride_u,
10*4e366538SXin Li                      uint8* dst_v, int dst_stride_v,
11*4e366538SXin Li                      int crop_x, int crop_y,
12*4e366538SXin Li                      int src_width, int src_height,
13*4e366538SXin Li                      int crop_width, int crop_height,
14*4e366538SXin Li                      enum RotationMode rotation,
15*4e366538SXin Li                      uint32 format);
16*4e366538SXin Li
17*4e366538SXin LiThis function crops, converts, and rotates.  You should think of it in that order.
18*4e366538SXin Li  * Crops the original image, which is src_width x src_height, to crop_width x crop_height.  At this point the image is still not rotated.
19*4e366538SXin Li  * Converts the cropped region to I420.  Supports inverted source for src_height negative.
20*4e366538SXin Li  * Rotates by 90, 180 or 270 degrees.
21*4e366538SXin LiThe buffer the caller provides should account for rotation.  Be especially important to get stride of the destination correct.
22*4e366538SXin Li
23*4e366538SXin Lie.g.
24*4e366538SXin Li640 x 480 NV12 captured<br>
25*4e366538SXin LiCrop to 640 x 360<br>
26*4e366538SXin LiRotate by 90 degrees to 360 x 640.<br>
27*4e366538SXin LiCaller passes stride of 360 for Y and 360 / 2 for U and V.<br>
28*4e366538SXin LiCaller passes crop_width of 640, crop_height of 360.<br>
29*4e366538SXin Li
30*4e366538SXin Li# ConvertToARGB
31*4e366538SXin Li
32*4e366538SXin Li    int ConvertToARGB(const uint8* src_frame, size_t src_size,
33*4e366538SXin Li                      uint8* dst_argb, int dst_stride_argb,
34*4e366538SXin Li                      int crop_x, int crop_y,
35*4e366538SXin Li                      int src_width, int src_height,
36*4e366538SXin Li                      int crop_width, int crop_height,
37*4e366538SXin Li                      enum RotationMode rotation,
38*4e366538SXin Li                      uint32 format);
39*4e366538SXin Li
40*4e366538SXin LiSame as I420, but implementation is less optimized - reads columns and writes rows, 16 bytes at a time.
41*4e366538SXin Li
42*4e366538SXin Li# I420Rotate
43*4e366538SXin Li
44*4e366538SXin Li    int I420Rotate(const uint8* src_y, int src_stride_y,
45*4e366538SXin Li                   const uint8* src_u, int src_stride_u,
46*4e366538SXin Li                   const uint8* src_v, int src_stride_v,
47*4e366538SXin Li                   uint8* dst_y, int dst_stride_y,
48*4e366538SXin Li                   uint8* dst_u, int dst_stride_u,
49*4e366538SXin Li                   uint8* dst_v, int dst_stride_v,
50*4e366538SXin Li                   int src_width, int src_height, enum RotationMode mode);
51*4e366538SXin Li
52*4e366538SXin LiDestination is rotated, so pass dst_stride_y etc that consider rotation.<br>
53*4e366538SXin LiRotate by 180 can be done in place, but 90 and 270 can not.
54*4e366538SXin Li
55*4e366538SXin LiImplementation (Neon/SSE2) uses 8 x 8 block transpose, so best efficiency is with sizes and pointers that are aligned to 8.
56*4e366538SXin Li
57*4e366538SXin LiCropping can be achieved by adjusting the src_y/u/v pointers and src_width, src_height.
58*4e366538SXin Li
59*4e366538SXin LiLower level plane functions are provided, allowing other planar formats to be rotated.  (e.g. I444)
60*4e366538SXin Li
61*4e366538SXin LiFor other planar YUV formats (I444, I422, I411, I400, NV16, NV24), the planar functions are exposed and can be called directly
62*4e366538SXin Li
63*4e366538SXin Li
64*4e366538SXin Li    // Rotate a plane by 0, 90, 180, or 270.
65*4e366538SXin Li    int RotatePlane(const uint8* src, int src_stride,
66*4e366538SXin Li                    uint8* dst, int dst_stride,
67*4e366538SXin Li                    int src_width, int src_height, enum RotationMode mode);
68*4e366538SXin Li
69*4e366538SXin Li# ARGBRotate
70*4e366538SXin Li
71*4e366538SXin Li    LIBYUV_API
72*4e366538SXin Li    int ARGBRotate(const uint8* src_argb, int src_stride_argb,
73*4e366538SXin Li                   uint8* dst_argb, int dst_stride_argb,
74*4e366538SXin Li                   int src_width, int src_height, enum RotationMode mode);
75*4e366538SXin Li
76*4e366538SXin LiSame as I420, but implementation is less optimized - reads columns and writes rows.
77*4e366538SXin Li
78*4e366538SXin LiRotate by 90, or any angle, can be achieved using ARGBAffine.
79*4e366538SXin Li
80*4e366538SXin Li# Mirror - Horizontal Flip
81*4e366538SXin Li
82*4e366538SXin LiMirror functions for horizontally flipping an image, which can be useful for 'self view' of a webcam.
83*4e366538SXin Li
84*4e366538SXin Li    int I420Mirror(const uint8* src_y, int src_stride_y,
85*4e366538SXin Li                   const uint8* src_u, int src_stride_u,
86*4e366538SXin Li                   const uint8* src_v, int src_stride_v,
87*4e366538SXin Li                   uint8* dst_y, int dst_stride_y,
88*4e366538SXin Li                   uint8* dst_u, int dst_stride_u,
89*4e366538SXin Li                   uint8* dst_v, int dst_stride_v,
90*4e366538SXin Li                   int width, int height);
91*4e366538SXin Li    int ARGBMirror(const uint8* src_argb, int src_stride_argb,
92*4e366538SXin Li                   uint8* dst_argb, int dst_stride_argb,
93*4e366538SXin Li                   int width, int height);
94*4e366538SXin Li
95*4e366538SXin LiMirror functionality can also be achieved with the I420Scale and ARGBScale functions by passing negative width and/or height.
96*4e366538SXin Li
97*4e366538SXin Li# Invert - Vertical Flip
98*4e366538SXin Li
99*4e366538SXin LiInverting can be achieved with almost any libyuv function by passing a negative source height.
100*4e366538SXin Li
101*4e366538SXin LiI420Mirror and ARGBMirror can also be used to rotate by 180 degrees by passing a negative height.
102*4e366538SXin Li
103*4e366538SXin Li# Cropping - Vertical Flip
104*4e366538SXin Li
105*4e366538SXin LiWhen cropping from a subsampled format like NV21, the method of setting the start pointers wont work for odd crop start y on the UV plane.
106*4e366538SXin LiIf the height after cropping will be odd, invert the source - point to the last row, negate the strides, and pass negative height, which
107*4e366538SXin Liwill re-invert the image as the conversion outputs.
108