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: { // TODO(fbarchard): Find better odd crop fix.
93 uint8_t* u = (crop_x & 1) ? dst_v : dst_u;
94 uint8_t* v = (crop_x & 1) ? dst_u : dst_v;
95 int stride_u = (crop_x & 1) ? dst_stride_v : dst_stride_u;
96 int stride_v = (crop_x & 1) ? dst_stride_u : dst_stride_v;
97 src = sample + (aligned_src_width * crop_y + crop_x) * 2;
98 r = YUY2ToI420(src, aligned_src_width * 2, dst_y, dst_stride_y, u,
99 stride_u, v, stride_v, crop_width, inv_crop_height);
100 break;
101 }
102 case FOURCC_UYVY: {
103 uint8_t* u = (crop_x & 1) ? dst_v : dst_u;
104 uint8_t* v = (crop_x & 1) ? dst_u : dst_v;
105 int stride_u = (crop_x & 1) ? dst_stride_v : dst_stride_u;
106 int stride_v = (crop_x & 1) ? dst_stride_u : dst_stride_v;
107 src = sample + (aligned_src_width * crop_y + crop_x) * 2;
108 r = UYVYToI420(src, aligned_src_width * 2, dst_y, dst_stride_y, u,
109 stride_u, v, stride_v, crop_width, inv_crop_height);
110 break;
111 }
112 case FOURCC_RGBP:
113 src = sample + (src_width * crop_y + crop_x) * 2;
114 r = RGB565ToI420(src, src_width * 2, dst_y, dst_stride_y, dst_u,
115 dst_stride_u, dst_v, dst_stride_v, crop_width,
116 inv_crop_height);
117 break;
118 case FOURCC_RGBO:
119 src = sample + (src_width * crop_y + crop_x) * 2;
120 r = ARGB1555ToI420(src, src_width * 2, dst_y, dst_stride_y, dst_u,
121 dst_stride_u, dst_v, dst_stride_v, crop_width,
122 inv_crop_height);
123 break;
124 case FOURCC_R444:
125 src = sample + (src_width * crop_y + crop_x) * 2;
126 r = ARGB4444ToI420(src, src_width * 2, dst_y, dst_stride_y, dst_u,
127 dst_stride_u, dst_v, dst_stride_v, crop_width,
128 inv_crop_height);
129 break;
130 case FOURCC_24BG:
131 src = sample + (src_width * crop_y + crop_x) * 3;
132 r = RGB24ToI420(src, src_width * 3, dst_y, dst_stride_y, dst_u,
133 dst_stride_u, dst_v, dst_stride_v, crop_width,
134 inv_crop_height);
135 break;
136 case FOURCC_RAW:
137 src = sample + (src_width * crop_y + crop_x) * 3;
138 r = RAWToI420(src, src_width * 3, dst_y, dst_stride_y, dst_u,
139 dst_stride_u, dst_v, dst_stride_v, crop_width,
140 inv_crop_height);
141 break;
142 case FOURCC_ARGB:
143 src = sample + (src_width * crop_y + crop_x) * 4;
144 r = ARGBToI420(src, src_width * 4, dst_y, dst_stride_y, dst_u,
145 dst_stride_u, dst_v, dst_stride_v, crop_width,
146 inv_crop_height);
147 break;
148 case FOURCC_BGRA:
149 src = sample + (src_width * crop_y + crop_x) * 4;
150 r = BGRAToI420(src, src_width * 4, dst_y, dst_stride_y, dst_u,
151 dst_stride_u, dst_v, dst_stride_v, crop_width,
152 inv_crop_height);
153 break;
154 case FOURCC_ABGR:
155 src = sample + (src_width * crop_y + crop_x) * 4;
156 r = ABGRToI420(src, src_width * 4, dst_y, dst_stride_y, dst_u,
157 dst_stride_u, dst_v, dst_stride_v, crop_width,
158 inv_crop_height);
159 break;
160 case FOURCC_RGBA:
161 src = sample + (src_width * crop_y + crop_x) * 4;
162 r = RGBAToI420(src, src_width * 4, dst_y, dst_stride_y, dst_u,
163 dst_stride_u, dst_v, dst_stride_v, crop_width,
164 inv_crop_height);
165 break;
166 // TODO(fbarchard): Add AR30 and AB30
167 case FOURCC_I400:
168 src = sample + src_width * crop_y + crop_x;
169 r = I400ToI420(src, src_width, dst_y, dst_stride_y, dst_u, dst_stride_u,
170 dst_v, dst_stride_v, crop_width, inv_crop_height);
171 break;
172 // Biplanar formats
173 case FOURCC_NV12:
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 r = NV12ToI420Rotate(src, src_width, src_uv, aligned_src_width, dst_y,
178 dst_stride_y, dst_u, dst_stride_u, dst_v,
179 dst_stride_v, crop_width, inv_crop_height, rotation);
180 break;
181 case FOURCC_NV21:
182 src = sample + (src_width * crop_y + crop_x);
183 src_uv = sample + (src_width * abs_src_height) +
184 ((crop_y / 2) * aligned_src_width) + ((crop_x / 2) * 2);
185 // Call NV12 but with dst_u and dst_v parameters swapped.
186 r = NV12ToI420Rotate(src, src_width, src_uv, aligned_src_width, dst_y,
187 dst_stride_y, dst_v, dst_stride_v, dst_u,
188 dst_stride_u, crop_width, inv_crop_height, rotation);
189 break;
190 // Triplanar formats
191 case FOURCC_I420:
192 case FOURCC_YV12: {
193 const uint8_t* src_y = sample + (src_width * crop_y + crop_x);
194 const uint8_t* src_u;
195 const uint8_t* src_v;
196 int halfwidth = (src_width + 1) / 2;
197 int halfheight = (abs_src_height + 1) / 2;
198 if (format == FOURCC_YV12) {
199 src_v = sample + src_width * abs_src_height + halfwidth * (crop_y / 2) +
200 (crop_x / 2);
201 src_u = sample + src_width * abs_src_height +
202 halfwidth * (halfheight + (crop_y / 2)) + (crop_x / 2);
203 } else {
204 src_u = sample + src_width * abs_src_height + halfwidth * (crop_y / 2) +
205 (crop_x / 2);
206 src_v = sample + src_width * abs_src_height +
207 halfwidth * (halfheight + (crop_y / 2)) + (crop_x / 2);
208 }
209 r = I420Rotate(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
210 dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
211 dst_stride_v, crop_width, inv_crop_height, rotation);
212 break;
213 }
214 case FOURCC_I422:
215 case FOURCC_YV16: {
216 const uint8_t* src_y = sample + src_width * crop_y + crop_x;
217 const uint8_t* src_u;
218 const uint8_t* src_v;
219 int halfwidth = (src_width + 1) / 2;
220 if (format == FOURCC_YV16) {
221 src_v = sample + src_width * abs_src_height + halfwidth * crop_y +
222 (crop_x / 2);
223 src_u = sample + src_width * abs_src_height +
224 halfwidth * (abs_src_height + crop_y) + (crop_x / 2);
225 } else {
226 src_u = sample + src_width * abs_src_height + halfwidth * crop_y +
227 (crop_x / 2);
228 src_v = sample + src_width * abs_src_height +
229 halfwidth * (abs_src_height + crop_y) + (crop_x / 2);
230 }
231 r = I422ToI420(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
232 dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
233 dst_stride_v, crop_width, inv_crop_height);
234 break;
235 }
236 case FOURCC_I444:
237 case FOURCC_YV24: {
238 const uint8_t* src_y = sample + src_width * crop_y + crop_x;
239 const uint8_t* src_u;
240 const uint8_t* src_v;
241 if (format == FOURCC_YV24) {
242 src_v = sample + src_width * (abs_src_height + crop_y) + crop_x;
243 src_u = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x;
244 } else {
245 src_u = sample + src_width * (abs_src_height + crop_y) + crop_x;
246 src_v = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x;
247 }
248 r = I444ToI420(src_y, src_width, src_u, src_width, src_v, src_width,
249 dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
250 dst_stride_v, crop_width, inv_crop_height);
251 break;
252 }
253 #ifdef HAVE_JPEG
254 case FOURCC_MJPG:
255 r = MJPGToI420(sample, sample_size, dst_y, dst_stride_y, dst_u,
256 dst_stride_u, dst_v, dst_stride_v, src_width,
257 abs_src_height, crop_width, inv_crop_height);
258 break;
259 #endif
260 default:
261 r = -1; // unknown fourcc - return failure code.
262 }
263
264 if (need_buf) {
265 if (!r) {
266 r = I420Rotate(dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
267 dst_stride_v, tmp_y, tmp_y_stride, tmp_u, tmp_u_stride,
268 tmp_v, tmp_v_stride, crop_width, abs_crop_height,
269 rotation);
270 }
271 free(rotate_buffer);
272 }
273
274 return r;
275 }
276
277 #ifdef __cplusplus
278 } // extern "C"
279 } // namespace libyuv
280 #endif
281