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 "libyuv/convert_argb.h"
12
13 #include "libyuv/cpu_id.h"
14 #ifdef HAVE_JPEG
15 #include "libyuv/mjpeg_decoder.h"
16 #endif
17 #include "libyuv/rotate_argb.h"
18 #include "libyuv/row.h"
19 #include "libyuv/video_common.h"
20
21 #ifdef __cplusplus
22 namespace libyuv {
23 extern "C" {
24 #endif
25
26 // Convert camera sample to ARGB with cropping, rotation and vertical flip.
27 // src_width is used for source stride computation
28 // src_height is used to compute location of planes, and indicate inversion
29 // sample_size is measured in bytes and is the size of the frame.
30 // With MJPEG it is the compressed size of the frame.
31
32 // TODO(fbarchard): Add the following:
33 // H010ToARGB
34 // H420ToARGB
35 // H422ToARGB
36 // I010ToARGB
37 // J400ToARGB
38 // J422ToARGB
39 // J444ToARGB
40
41 LIBYUV_API
ConvertToARGB(const uint8_t * sample,size_t sample_size,uint8_t * dst_argb,int dst_stride_argb,int crop_x,int crop_y,int src_width,int src_height,int crop_width,int crop_height,enum RotationMode rotation,uint32_t fourcc)42 int ConvertToARGB(const uint8_t* sample,
43 size_t sample_size,
44 uint8_t* dst_argb,
45 int dst_stride_argb,
46 int crop_x,
47 int crop_y,
48 int src_width,
49 int src_height,
50 int crop_width,
51 int crop_height,
52 enum RotationMode rotation,
53 uint32_t fourcc) {
54 uint32_t format = CanonicalFourCC(fourcc);
55 int aligned_src_width = (src_width + 1) & ~1;
56 const uint8_t* src;
57 const uint8_t* src_uv;
58 int abs_src_height = (src_height < 0) ? -src_height : src_height;
59 int inv_crop_height = (crop_height < 0) ? -crop_height : crop_height;
60 int r = 0;
61
62 // One pass rotation is available for some formats. For the rest, convert
63 // to ARGB (with optional vertical flipping) into a temporary ARGB buffer,
64 // and then rotate the ARGB to the final destination buffer.
65 // For in-place conversion, if destination dst_argb is same as source sample,
66 // also enable temporary buffer.
67 LIBYUV_BOOL need_buf =
68 (rotation && format != FOURCC_ARGB) || dst_argb == sample;
69 uint8_t* dest_argb = dst_argb;
70 int dest_dst_stride_argb = dst_stride_argb;
71 uint8_t* rotate_buffer = NULL;
72 int abs_crop_height = (crop_height < 0) ? -crop_height : crop_height;
73
74 if (dst_argb == NULL || sample == NULL || src_width <= 0 || crop_width <= 0 ||
75 src_height == 0 || crop_height == 0) {
76 return -1;
77 }
78 if (src_height < 0) {
79 inv_crop_height = -inv_crop_height;
80 }
81
82 if (need_buf) {
83 int argb_size = crop_width * 4 * abs_crop_height;
84 rotate_buffer = (uint8_t*)malloc(argb_size); /* NOLINT */
85 if (!rotate_buffer) {
86 return 1; // Out of memory runtime error.
87 }
88 dst_argb = rotate_buffer;
89 dst_stride_argb = crop_width * 4;
90 }
91
92 switch (format) {
93 // Single plane formats
94 case FOURCC_YUY2:
95 src = sample + (aligned_src_width * crop_y + crop_x) * 2;
96 r = YUY2ToARGB(src, aligned_src_width * 2, dst_argb, dst_stride_argb,
97 crop_width, inv_crop_height);
98 break;
99 case FOURCC_UYVY:
100 src = sample + (aligned_src_width * crop_y + crop_x) * 2;
101 r = UYVYToARGB(src, aligned_src_width * 2, dst_argb, dst_stride_argb,
102 crop_width, inv_crop_height);
103 break;
104 case FOURCC_24BG:
105 src = sample + (src_width * crop_y + crop_x) * 3;
106 r = RGB24ToARGB(src, src_width * 3, dst_argb, dst_stride_argb, crop_width,
107 inv_crop_height);
108 break;
109 case FOURCC_RAW:
110 src = sample + (src_width * crop_y + crop_x) * 3;
111 r = RAWToARGB(src, src_width * 3, dst_argb, dst_stride_argb, crop_width,
112 inv_crop_height);
113 break;
114 case FOURCC_ARGB:
115 if (!need_buf && !rotation) {
116 src = sample + (src_width * crop_y + crop_x) * 4;
117 r = ARGBToARGB(src, src_width * 4, dst_argb, dst_stride_argb,
118 crop_width, inv_crop_height);
119 }
120 break;
121 case FOURCC_BGRA:
122 src = sample + (src_width * crop_y + crop_x) * 4;
123 r = BGRAToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width,
124 inv_crop_height);
125 break;
126 case FOURCC_ABGR:
127 src = sample + (src_width * crop_y + crop_x) * 4;
128 r = ABGRToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width,
129 inv_crop_height);
130 break;
131 case FOURCC_RGBA:
132 src = sample + (src_width * crop_y + crop_x) * 4;
133 r = RGBAToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width,
134 inv_crop_height);
135 break;
136 case FOURCC_AR30:
137 src = sample + (src_width * crop_y + crop_x) * 4;
138 r = AR30ToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width,
139 inv_crop_height);
140 break;
141 case FOURCC_AB30:
142 src = sample + (src_width * crop_y + crop_x) * 4;
143 r = AB30ToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width,
144 inv_crop_height);
145 break;
146 case FOURCC_RGBP:
147 src = sample + (src_width * crop_y + crop_x) * 2;
148 r = RGB565ToARGB(src, src_width * 2, dst_argb, dst_stride_argb,
149 crop_width, inv_crop_height);
150 break;
151 case FOURCC_RGBO:
152 src = sample + (src_width * crop_y + crop_x) * 2;
153 r = ARGB1555ToARGB(src, src_width * 2, dst_argb, dst_stride_argb,
154 crop_width, inv_crop_height);
155 break;
156 case FOURCC_R444:
157 src = sample + (src_width * crop_y + crop_x) * 2;
158 r = ARGB4444ToARGB(src, src_width * 2, dst_argb, dst_stride_argb,
159 crop_width, inv_crop_height);
160 break;
161 case FOURCC_I400:
162 src = sample + src_width * crop_y + crop_x;
163 r = I400ToARGB(src, src_width, dst_argb, dst_stride_argb, crop_width,
164 inv_crop_height);
165 break;
166
167 // Biplanar formats
168 case FOURCC_NV12:
169 src = sample + (src_width * crop_y + crop_x);
170 src_uv = sample + aligned_src_width * (abs_src_height + crop_y / 2) + crop_x;
171 r = NV12ToARGB(src, src_width, src_uv, aligned_src_width, dst_argb,
172 dst_stride_argb, crop_width, inv_crop_height);
173 break;
174 case FOURCC_NV21:
175 src = sample + (src_width * crop_y + crop_x);
176 src_uv = sample + aligned_src_width * (abs_src_height + crop_y / 2) + crop_x;
177 // Call NV12 but with u and v parameters swapped.
178 r = NV21ToARGB(src, src_width, src_uv, aligned_src_width, dst_argb,
179 dst_stride_argb, crop_width, inv_crop_height);
180 break;
181 case FOURCC_M420:
182 src = sample + (src_width * crop_y) * 12 / 8 + crop_x;
183 r = M420ToARGB(src, src_width, dst_argb, dst_stride_argb, crop_width,
184 inv_crop_height);
185 break;
186
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 = I420ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
207 dst_argb, dst_stride_argb, crop_width, inv_crop_height);
208 break;
209 }
210
211 case FOURCC_J420: {
212 const uint8_t* src_y = sample + (src_width * crop_y + crop_x);
213 const uint8_t* src_u;
214 const uint8_t* src_v;
215 int halfwidth = (src_width + 1) / 2;
216 int halfheight = (abs_src_height + 1) / 2;
217 src_u = sample + src_width * abs_src_height +
218 (halfwidth * crop_y + crop_x) / 2;
219 src_v = sample + src_width * abs_src_height +
220 halfwidth * (halfheight + crop_y / 2) + crop_x / 2;
221 r = J420ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
222 dst_argb, dst_stride_argb, crop_width, inv_crop_height);
223 break;
224 }
225
226 case FOURCC_I422:
227 case FOURCC_YV16: {
228 const uint8_t* src_y = sample + src_width * crop_y + crop_x;
229 const uint8_t* src_u;
230 const uint8_t* src_v;
231 int halfwidth = (src_width + 1) / 2;
232 if (format == FOURCC_YV16) {
233 src_v = sample + src_width * abs_src_height + halfwidth * crop_y +
234 crop_x / 2;
235 src_u = sample + src_width * abs_src_height +
236 halfwidth * (abs_src_height + crop_y) + crop_x / 2;
237 } else {
238 src_u = sample + src_width * abs_src_height + halfwidth * crop_y +
239 crop_x / 2;
240 src_v = sample + src_width * abs_src_height +
241 halfwidth * (abs_src_height + crop_y) + crop_x / 2;
242 }
243 r = I422ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
244 dst_argb, dst_stride_argb, crop_width, inv_crop_height);
245 break;
246 }
247 case FOURCC_I444:
248 case FOURCC_YV24: {
249 const uint8_t* src_y = sample + src_width * crop_y + crop_x;
250 const uint8_t* src_u;
251 const uint8_t* src_v;
252 if (format == FOURCC_YV24) {
253 src_v = sample + src_width * (abs_src_height + crop_y) + crop_x;
254 src_u = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x;
255 } else {
256 src_u = sample + src_width * (abs_src_height + crop_y) + crop_x;
257 src_v = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x;
258 }
259 r = I444ToARGB(src_y, src_width, src_u, src_width, src_v, src_width,
260 dst_argb, dst_stride_argb, crop_width, inv_crop_height);
261 break;
262 }
263 #ifdef HAVE_JPEG
264 case FOURCC_MJPG:
265 r = MJPGToARGB(sample, sample_size, dst_argb, dst_stride_argb, src_width,
266 abs_src_height, crop_width, inv_crop_height);
267 break;
268 #endif
269 default:
270 r = -1; // unknown fourcc - return failure code.
271 }
272
273 if (need_buf) {
274 if (!r) {
275 r = ARGBRotate(dst_argb, dst_stride_argb, dest_argb, dest_dst_stride_argb,
276 crop_width, abs_crop_height, rotation);
277 }
278 free(rotate_buffer);
279 } else if (rotation) {
280 src = sample + (src_width * crop_y + crop_x) * 4;
281 r = ARGBRotate(src, src_width * 4, dst_argb, dst_stride_argb, crop_width,
282 inv_crop_height, rotation);
283 }
284
285 return r;
286 }
287
288 #ifdef __cplusplus
289 } // extern "C"
290 } // namespace libyuv
291 #endif
292