1 /*
2 * Copyright 2011 The LibYuv Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <stdlib.h>
12
13 #include "libyuv/convert.h"
14
15 #include "libyuv/video_common.h"
16
17 #ifdef __cplusplus
18 namespace libyuv {
19 extern "C" {
20 #endif
21
22 // Convert camera sample to I420 with cropping, rotation and vertical flip.
23 // src_width is used for source stride computation
24 // src_height is used to compute location of planes, and indicate inversion
25 // sample_size is measured in bytes and is the size of the frame.
26 // With MJPEG it is the compressed size of the frame.
27 LIBYUV_API
ConvertToI420(const uint8_t * sample,size_t sample_size,uint8_t * dst_y,int dst_stride_y,uint8_t * dst_u,int dst_stride_u,uint8_t * dst_v,int dst_stride_v,int crop_x,int crop_y,int src_width,int src_height,int crop_width,int crop_height,enum RotationMode rotation,uint32_t fourcc)28 int ConvertToI420(const uint8_t* sample,
29 size_t sample_size,
30 uint8_t* dst_y,
31 int dst_stride_y,
32 uint8_t* dst_u,
33 int dst_stride_u,
34 uint8_t* dst_v,
35 int dst_stride_v,
36 int crop_x,
37 int crop_y,
38 int src_width,
39 int src_height,
40 int crop_width,
41 int crop_height,
42 enum RotationMode rotation,
43 uint32_t fourcc) {
44 uint32_t format = CanonicalFourCC(fourcc);
45 int aligned_src_width = (src_width + 1) & ~1;
46 const uint8_t* src;
47 const uint8_t* src_uv;
48 const int abs_src_height = (src_height < 0) ? -src_height : src_height;
49 // TODO(nisse): Why allow crop_height < 0?
50 const int abs_crop_height = (crop_height < 0) ? -crop_height : crop_height;
51 int r = 0;
52 LIBYUV_BOOL need_buf =
53 (rotation && format != FOURCC_I420 && format != FOURCC_NV12 &&
54 format != FOURCC_NV21 && format != FOURCC_YV12) ||
55 dst_y == sample;
56 uint8_t* tmp_y = dst_y;
57 uint8_t* tmp_u = dst_u;
58 uint8_t* tmp_v = dst_v;
59 int tmp_y_stride = dst_stride_y;
60 int tmp_u_stride = dst_stride_u;
61 int tmp_v_stride = dst_stride_v;
62 uint8_t* rotate_buffer = NULL;
63 const int inv_crop_height =
64 (src_height < 0) ? -abs_crop_height : abs_crop_height;
65
66 if (!dst_y || !dst_u || !dst_v || !sample || src_width <= 0 ||
67 crop_width <= 0 || src_height == 0 || crop_height == 0) {
68 return -1;
69 }
70
71 // One pass rotation is available for some formats. For the rest, convert
72 // to I420 (with optional vertical flipping) into a temporary I420 buffer,
73 // and then rotate the I420 to the final destination buffer.
74 // For in-place conversion, if destination dst_y is same as source sample,
75 // also enable temporary buffer.
76 if (need_buf) {
77 int y_size = crop_width * abs_crop_height;
78 int uv_size = ((crop_width + 1) / 2) * ((abs_crop_height + 1) / 2);
79 rotate_buffer = (uint8_t*)malloc(y_size + uv_size * 2); /* NOLINT */
80 if (!rotate_buffer) {
81 return 1; // Out of memory runtime error.
82 }
83 dst_y = rotate_buffer;
84 dst_u = dst_y + y_size;
85 dst_v = dst_u + uv_size;
86 dst_stride_y = crop_width;
87 dst_stride_u = dst_stride_v = ((crop_width + 1) / 2);
88 }
89
90 switch (format) {
91 // Single plane formats
92 case FOURCC_YUY2:
93 src = sample + (aligned_src_width * crop_y + crop_x) * 2;
94 r = YUY2ToI420(src, aligned_src_width * 2, dst_y, dst_stride_y, dst_u,
95 dst_stride_u, dst_v, dst_stride_v, crop_width,
96 inv_crop_height);
97 break;
98 case FOURCC_UYVY:
99 src = sample + (aligned_src_width * crop_y + crop_x) * 2;
100 r = UYVYToI420(src, aligned_src_width * 2, dst_y, dst_stride_y, dst_u,
101 dst_stride_u, dst_v, dst_stride_v, crop_width,
102 inv_crop_height);
103 break;
104 case FOURCC_RGBP:
105 src = sample + (src_width * crop_y + crop_x) * 2;
106 r = RGB565ToI420(src, src_width * 2, dst_y, dst_stride_y, dst_u,
107 dst_stride_u, dst_v, dst_stride_v, crop_width,
108 inv_crop_height);
109 break;
110 case FOURCC_RGBO:
111 src = sample + (src_width * crop_y + crop_x) * 2;
112 r = ARGB1555ToI420(src, src_width * 2, dst_y, dst_stride_y, dst_u,
113 dst_stride_u, dst_v, dst_stride_v, crop_width,
114 inv_crop_height);
115 break;
116 case FOURCC_R444:
117 src = sample + (src_width * crop_y + crop_x) * 2;
118 r = ARGB4444ToI420(src, src_width * 2, dst_y, dst_stride_y, dst_u,
119 dst_stride_u, dst_v, dst_stride_v, crop_width,
120 inv_crop_height);
121 break;
122 case FOURCC_24BG:
123 src = sample + (src_width * crop_y + crop_x) * 3;
124 r = RGB24ToI420(src, src_width * 3, dst_y, dst_stride_y, dst_u,
125 dst_stride_u, dst_v, dst_stride_v, crop_width,
126 inv_crop_height);
127 break;
128 case FOURCC_RAW:
129 src = sample + (src_width * crop_y + crop_x) * 3;
130 r = RAWToI420(src, src_width * 3, dst_y, dst_stride_y, dst_u,
131 dst_stride_u, dst_v, dst_stride_v, crop_width,
132 inv_crop_height);
133 break;
134 case FOURCC_ARGB:
135 src = sample + (src_width * crop_y + crop_x) * 4;
136 r = ARGBToI420(src, src_width * 4, dst_y, dst_stride_y, dst_u,
137 dst_stride_u, dst_v, dst_stride_v, crop_width,
138 inv_crop_height);
139 break;
140 case FOURCC_BGRA:
141 src = sample + (src_width * crop_y + crop_x) * 4;
142 r = BGRAToI420(src, src_width * 4, dst_y, dst_stride_y, dst_u,
143 dst_stride_u, dst_v, dst_stride_v, crop_width,
144 inv_crop_height);
145 break;
146 case FOURCC_ABGR:
147 src = sample + (src_width * crop_y + crop_x) * 4;
148 r = ABGRToI420(src, src_width * 4, dst_y, dst_stride_y, dst_u,
149 dst_stride_u, dst_v, dst_stride_v, crop_width,
150 inv_crop_height);
151 break;
152 case FOURCC_RGBA:
153 src = sample + (src_width * crop_y + crop_x) * 4;
154 r = RGBAToI420(src, src_width * 4, dst_y, dst_stride_y, dst_u,
155 dst_stride_u, dst_v, dst_stride_v, crop_width,
156 inv_crop_height);
157 break;
158 // TODO(fbarchard): Add AR30 and AB30
159 case FOURCC_I400:
160 src = sample + src_width * crop_y + crop_x;
161 r = I400ToI420(src, src_width, dst_y, dst_stride_y, dst_u, dst_stride_u,
162 dst_v, dst_stride_v, crop_width, inv_crop_height);
163 break;
164 // Biplanar formats
165 case FOURCC_NV12:
166 src = sample + (src_width * crop_y + crop_x);
167 src_uv = sample + (src_width * abs_src_height) +
168 ((crop_y / 2) * aligned_src_width) + ((crop_x / 2) * 2);
169 r = NV12ToI420Rotate(src, src_width, src_uv, aligned_src_width, dst_y,
170 dst_stride_y, dst_u, dst_stride_u, dst_v,
171 dst_stride_v, crop_width, inv_crop_height, rotation);
172 break;
173 case FOURCC_NV21:
174 src = sample + (src_width * crop_y + crop_x);
175 src_uv = sample + (src_width * abs_src_height) +
176 ((crop_y / 2) * aligned_src_width) + ((crop_x / 2) * 2);
177 // Call NV12 but with dst_u and dst_v parameters swapped.
178 r = NV12ToI420Rotate(src, src_width, src_uv, aligned_src_width, dst_y,
179 dst_stride_y, dst_v, dst_stride_v, dst_u,
180 dst_stride_u, crop_width, inv_crop_height, rotation);
181 break;
182 // Triplanar formats
183 case FOURCC_I420:
184 case FOURCC_YV12: {
185 const uint8_t* src_y = sample + (src_width * crop_y + crop_x);
186 const uint8_t* src_u;
187 const uint8_t* src_v;
188 int halfwidth = (src_width + 1) / 2;
189 int halfheight = (abs_src_height + 1) / 2;
190 if (format == FOURCC_YV12) {
191 src_v = sample + src_width * abs_src_height + halfwidth * (crop_y / 2) +
192 (crop_x / 2);
193 src_u = sample + src_width * abs_src_height +
194 halfwidth * (halfheight + (crop_y / 2)) + (crop_x / 2);
195 } else {
196 src_u = sample + src_width * abs_src_height + halfwidth * (crop_y / 2) +
197 (crop_x / 2);
198 src_v = sample + src_width * abs_src_height +
199 halfwidth * (halfheight + (crop_y / 2)) + (crop_x / 2);
200 }
201 r = I420Rotate(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
202 dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
203 dst_stride_v, crop_width, inv_crop_height, rotation);
204 break;
205 }
206 case FOURCC_I422:
207 case FOURCC_YV16: {
208 const uint8_t* src_y = sample + src_width * crop_y + crop_x;
209 const uint8_t* src_u;
210 const uint8_t* src_v;
211 int halfwidth = (src_width + 1) / 2;
212 if (format == FOURCC_YV16) {
213 src_v = sample + src_width * abs_src_height + halfwidth * crop_y +
214 (crop_x / 2);
215 src_u = sample + src_width * abs_src_height +
216 halfwidth * (abs_src_height + crop_y) + (crop_x / 2);
217 } else {
218 src_u = sample + src_width * abs_src_height + halfwidth * crop_y +
219 (crop_x / 2);
220 src_v = sample + src_width * abs_src_height +
221 halfwidth * (abs_src_height + crop_y) + (crop_x / 2);
222 }
223 r = I422ToI420(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
224 dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
225 dst_stride_v, crop_width, inv_crop_height);
226 break;
227 }
228 case FOURCC_I444:
229 case FOURCC_YV24: {
230 const uint8_t* src_y = sample + src_width * crop_y + crop_x;
231 const uint8_t* src_u;
232 const uint8_t* src_v;
233 if (format == FOURCC_YV24) {
234 src_v = sample + src_width * (abs_src_height + crop_y) + crop_x;
235 src_u = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x;
236 } else {
237 src_u = sample + src_width * (abs_src_height + crop_y) + crop_x;
238 src_v = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x;
239 }
240 r = I444ToI420(src_y, src_width, src_u, src_width, src_v, src_width,
241 dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
242 dst_stride_v, crop_width, inv_crop_height);
243 break;
244 }
245 #ifdef HAVE_JPEG
246 case FOURCC_MJPG:
247 r = MJPGToI420(sample, sample_size, dst_y, dst_stride_y, dst_u,
248 dst_stride_u, dst_v, dst_stride_v, src_width,
249 abs_src_height, crop_width, inv_crop_height);
250 break;
251 #endif
252 default:
253 r = -1; // unknown fourcc - return failure code.
254 }
255
256 if (need_buf) {
257 if (!r) {
258 r = I420Rotate(dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
259 dst_stride_v, tmp_y, tmp_y_stride, tmp_u, tmp_u_stride,
260 tmp_v, tmp_v_stride, crop_width, abs_crop_height,
261 rotation);
262 }
263 free(rotate_buffer);
264 }
265
266 return r;
267 }
268
269 #ifdef __cplusplus
270 } // extern "C"
271 } // namespace libyuv
272 #endif
273