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 case FOURCC_M420:
183 src = sample + (src_width * crop_y) * 12 / 8 + crop_x;
184 r = M420ToI420(src, src_width, dst_y, dst_stride_y, dst_u, dst_stride_u,
185 dst_v, dst_stride_v, crop_width, inv_crop_height);
186 break;
187 // Triplanar formats
188 case FOURCC_I420:
189 case FOURCC_YV12: {
190 const uint8_t* src_y = sample + (src_width * crop_y + crop_x);
191 const uint8_t* src_u;
192 const uint8_t* src_v;
193 int halfwidth = (src_width + 1) / 2;
194 int halfheight = (abs_src_height + 1) / 2;
195 if (format == FOURCC_YV12) {
196 src_v = sample + src_width * abs_src_height +
197 (halfwidth * crop_y + crop_x) / 2;
198 src_u = sample + src_width * abs_src_height +
199 halfwidth * (halfheight + crop_y / 2) + crop_x / 2;
200 } else {
201 src_u = sample + src_width * abs_src_height +
202 (halfwidth * crop_y + crop_x) / 2;
203 src_v = sample + src_width * abs_src_height +
204 halfwidth * (halfheight + crop_y / 2) + crop_x / 2;
205 }
206 r = I420Rotate(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
207 dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
208 dst_stride_v, crop_width, inv_crop_height, rotation);
209 break;
210 }
211 case FOURCC_I422:
212 case FOURCC_YV16: {
213 const uint8_t* src_y = sample + src_width * crop_y + crop_x;
214 const uint8_t* src_u;
215 const uint8_t* src_v;
216 int halfwidth = (src_width + 1) / 2;
217 if (format == FOURCC_YV16) {
218 src_v = sample + src_width * abs_src_height + halfwidth * crop_y +
219 crop_x / 2;
220 src_u = sample + src_width * abs_src_height +
221 halfwidth * (abs_src_height + crop_y) + crop_x / 2;
222 } else {
223 src_u = sample + src_width * abs_src_height + halfwidth * crop_y +
224 crop_x / 2;
225 src_v = sample + src_width * abs_src_height +
226 halfwidth * (abs_src_height + crop_y) + crop_x / 2;
227 }
228 r = I422ToI420(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
229 dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
230 dst_stride_v, crop_width, inv_crop_height);
231 break;
232 }
233 case FOURCC_I444:
234 case FOURCC_YV24: {
235 const uint8_t* src_y = sample + src_width * crop_y + crop_x;
236 const uint8_t* src_u;
237 const uint8_t* src_v;
238 if (format == FOURCC_YV24) {
239 src_v = sample + src_width * (abs_src_height + crop_y) + crop_x;
240 src_u = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x;
241 } else {
242 src_u = sample + src_width * (abs_src_height + crop_y) + crop_x;
243 src_v = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x;
244 }
245 r = I444ToI420(src_y, src_width, src_u, src_width, src_v, src_width,
246 dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
247 dst_stride_v, crop_width, inv_crop_height);
248 break;
249 }
250 #ifdef HAVE_JPEG
251 case FOURCC_MJPG:
252 r = MJPGToI420(sample, sample_size, dst_y, dst_stride_y, dst_u,
253 dst_stride_u, dst_v, dst_stride_v, src_width,
254 abs_src_height, crop_width, inv_crop_height);
255 break;
256 #endif
257 default:
258 r = -1; // unknown fourcc - return failure code.
259 }
260
261 if (need_buf) {
262 if (!r) {
263 r = I420Rotate(dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v,
264 dst_stride_v, tmp_y, tmp_y_stride, tmp_u, tmp_u_stride,
265 tmp_v, tmp_v_stride, crop_width, abs_crop_height,
266 rotation);
267 }
268 free(rotate_buffer);
269 }
270
271 return r;
272 }
273
274 #ifdef __cplusplus
275 } // extern "C"
276 } // namespace libyuv
277 #endif
278