• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // I010ToARGB
35 // J400ToARGB
36 // J422ToARGB
37 // J444ToARGB
38 
39 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)40 int ConvertToARGB(const uint8_t* sample,
41                   size_t sample_size,
42                   uint8_t* dst_argb,
43                   int dst_stride_argb,
44                   int crop_x,
45                   int crop_y,
46                   int src_width,
47                   int src_height,
48                   int crop_width,
49                   int crop_height,
50                   enum RotationMode rotation,
51                   uint32_t fourcc) {
52   uint32_t format = CanonicalFourCC(fourcc);
53   int aligned_src_width = (src_width + 1) & ~1;
54   const uint8_t* src;
55   const uint8_t* src_uv;
56   int abs_src_height = (src_height < 0) ? -src_height : src_height;
57   int inv_crop_height = (crop_height < 0) ? -crop_height : crop_height;
58   int r = 0;
59 
60   // One pass rotation is available for some formats. For the rest, convert
61   // to ARGB (with optional vertical flipping) into a temporary ARGB buffer,
62   // and then rotate the ARGB to the final destination buffer.
63   // For in-place conversion, if destination dst_argb is same as source sample,
64   // also enable temporary buffer.
65   LIBYUV_BOOL need_buf =
66       (rotation && format != FOURCC_ARGB) || dst_argb == sample;
67   uint8_t* dest_argb = dst_argb;
68   int dest_dst_stride_argb = dst_stride_argb;
69   uint8_t* rotate_buffer = NULL;
70   int abs_crop_height = (crop_height < 0) ? -crop_height : crop_height;
71 
72   if (dst_argb == NULL || sample == NULL || src_width <= 0 || crop_width <= 0 ||
73       src_height == 0 || crop_height == 0) {
74     return -1;
75   }
76   if (src_height < 0) {
77     inv_crop_height = -inv_crop_height;
78   }
79 
80   if (need_buf) {
81     int argb_size = crop_width * 4 * abs_crop_height;
82     rotate_buffer = (uint8_t*)malloc(argb_size); /* NOLINT */
83     if (!rotate_buffer) {
84       return 1;  // Out of memory runtime error.
85     }
86     dst_argb = rotate_buffer;
87     dst_stride_argb = crop_width * 4;
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 = YUY2ToARGB(src, aligned_src_width * 2, dst_argb, dst_stride_argb,
95                      crop_width, inv_crop_height);
96       break;
97     case FOURCC_UYVY:
98       src = sample + (aligned_src_width * crop_y + crop_x) * 2;
99       r = UYVYToARGB(src, aligned_src_width * 2, dst_argb, dst_stride_argb,
100                      crop_width, inv_crop_height);
101       break;
102     case FOURCC_24BG:
103       src = sample + (src_width * crop_y + crop_x) * 3;
104       r = RGB24ToARGB(src, src_width * 3, dst_argb, dst_stride_argb, crop_width,
105                       inv_crop_height);
106       break;
107     case FOURCC_RAW:
108       src = sample + (src_width * crop_y + crop_x) * 3;
109       r = RAWToARGB(src, src_width * 3, dst_argb, dst_stride_argb, crop_width,
110                     inv_crop_height);
111       break;
112     case FOURCC_ARGB:
113       if (!need_buf && !rotation) {
114         src = sample + (src_width * crop_y + crop_x) * 4;
115         r = ARGBToARGB(src, src_width * 4, dst_argb, dst_stride_argb,
116                        crop_width, inv_crop_height);
117       }
118       break;
119     case FOURCC_BGRA:
120       src = sample + (src_width * crop_y + crop_x) * 4;
121       r = BGRAToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width,
122                      inv_crop_height);
123       break;
124     case FOURCC_ABGR:
125       src = sample + (src_width * crop_y + crop_x) * 4;
126       r = ABGRToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width,
127                      inv_crop_height);
128       break;
129     case FOURCC_RGBA:
130       src = sample + (src_width * crop_y + crop_x) * 4;
131       r = RGBAToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width,
132                      inv_crop_height);
133       break;
134     case FOURCC_AR30:
135       src = sample + (src_width * crop_y + crop_x) * 4;
136       r = AR30ToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width,
137                      inv_crop_height);
138       break;
139     case FOURCC_AB30:
140       src = sample + (src_width * crop_y + crop_x) * 4;
141       r = AB30ToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width,
142                      inv_crop_height);
143       break;
144     case FOURCC_RGBP:
145       src = sample + (src_width * crop_y + crop_x) * 2;
146       r = RGB565ToARGB(src, src_width * 2, dst_argb, dst_stride_argb,
147                        crop_width, inv_crop_height);
148       break;
149     case FOURCC_RGBO:
150       src = sample + (src_width * crop_y + crop_x) * 2;
151       r = ARGB1555ToARGB(src, src_width * 2, dst_argb, dst_stride_argb,
152                          crop_width, inv_crop_height);
153       break;
154     case FOURCC_R444:
155       src = sample + (src_width * crop_y + crop_x) * 2;
156       r = ARGB4444ToARGB(src, src_width * 2, dst_argb, dst_stride_argb,
157                          crop_width, inv_crop_height);
158       break;
159     case FOURCC_I400:
160       src = sample + src_width * crop_y + crop_x;
161       r = I400ToARGB(src, src_width, dst_argb, dst_stride_argb, crop_width,
162                      inv_crop_height);
163       break;
164 
165     // Biplanar formats
166     case FOURCC_NV12:
167       src = sample + (src_width * crop_y + crop_x);
168       src_uv =
169           sample + aligned_src_width * (abs_src_height + crop_y / 2) + crop_x;
170       r = NV12ToARGB(src, src_width, src_uv, aligned_src_width, dst_argb,
171                      dst_stride_argb, crop_width, inv_crop_height);
172       break;
173     case FOURCC_NV21:
174       src = sample + (src_width * crop_y + crop_x);
175       src_uv =
176           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_H420: {
212       int halfwidth = (src_width + 1) / 2;
213       int halfheight = (abs_src_height + 1) / 2;
214       const uint8_t* src_y = sample + (src_width * crop_y + crop_x);
215       const uint8_t* src_u = sample + src_width * abs_src_height +
216                              (halfwidth * crop_y + crop_x) / 2;
217       const uint8_t* src_v = sample + src_width * abs_src_height +
218                              halfwidth * (halfheight + crop_y / 2) + crop_x / 2;
219       r = H420ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
220                      dst_argb, dst_stride_argb, crop_width, inv_crop_height);
221       break;
222     }
223 
224     case FOURCC_J420: {
225       int halfwidth = (src_width + 1) / 2;
226       int halfheight = (abs_src_height + 1) / 2;
227       const uint8_t* src_y = sample + (src_width * crop_y + crop_x);
228       const uint8_t* src_u = sample + src_width * abs_src_height +
229                              (halfwidth * crop_y + crop_x) / 2;
230       const uint8_t* src_v = sample + src_width * abs_src_height +
231                              halfwidth * (halfheight + crop_y / 2) + crop_x / 2;
232       r = J420ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
233                      dst_argb, dst_stride_argb, crop_width, inv_crop_height);
234       break;
235     }
236 
237     case FOURCC_I422:
238     case FOURCC_YV16: {
239       int halfwidth = (src_width + 1) / 2;
240       const uint8_t* src_y = sample + src_width * crop_y + crop_x;
241       const uint8_t* src_u;
242       const uint8_t* src_v;
243       if (format == FOURCC_YV16) {
244         src_v = sample + src_width * abs_src_height + halfwidth * crop_y +
245                 crop_x / 2;
246         src_u = sample + src_width * abs_src_height +
247                 halfwidth * (abs_src_height + crop_y) + crop_x / 2;
248       } else {
249         src_u = sample + src_width * abs_src_height + halfwidth * crop_y +
250                 crop_x / 2;
251         src_v = sample + src_width * abs_src_height +
252                 halfwidth * (abs_src_height + crop_y) + crop_x / 2;
253       }
254       r = I422ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
255                      dst_argb, dst_stride_argb, crop_width, inv_crop_height);
256       break;
257     }
258 
259     case FOURCC_H422: {
260       int halfwidth = (src_width + 1) / 2;
261       const uint8_t* src_y = sample + src_width * crop_y + crop_x;
262       const uint8_t* src_u =
263           sample + src_width * abs_src_height + halfwidth * crop_y + crop_x / 2;
264       const uint8_t* src_v = sample + src_width * abs_src_height +
265                              halfwidth * (abs_src_height + crop_y) + crop_x / 2;
266       r = H422ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
267                      dst_argb, dst_stride_argb, crop_width, inv_crop_height);
268       break;
269     }
270 
271     case FOURCC_I444:
272     case FOURCC_YV24: {
273       const uint8_t* src_y = sample + src_width * crop_y + crop_x;
274       const uint8_t* src_u;
275       const uint8_t* src_v;
276       if (format == FOURCC_YV24) {
277         src_v = sample + src_width * (abs_src_height + crop_y) + crop_x;
278         src_u = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x;
279       } else {
280         src_u = sample + src_width * (abs_src_height + crop_y) + crop_x;
281         src_v = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x;
282       }
283       r = I444ToARGB(src_y, src_width, src_u, src_width, src_v, src_width,
284                      dst_argb, dst_stride_argb, crop_width, inv_crop_height);
285       break;
286     }
287 #ifdef HAVE_JPEG
288     case FOURCC_MJPG:
289       r = MJPGToARGB(sample, sample_size, dst_argb, dst_stride_argb, src_width,
290                      abs_src_height, crop_width, inv_crop_height);
291       break;
292 #endif
293     default:
294       r = -1;  // unknown fourcc - return failure code.
295   }
296 
297   if (need_buf) {
298     if (!r) {
299       r = ARGBRotate(dst_argb, dst_stride_argb, dest_argb, dest_dst_stride_argb,
300                      crop_width, abs_crop_height, rotation);
301     }
302     free(rotate_buffer);
303   } else if (rotation) {
304     src = sample + (src_width * crop_y + crop_x) * 4;
305     r = ARGBRotate(src, src_width * 4, dst_argb, dst_stride_argb, crop_width,
306                    inv_crop_height, rotation);
307   }
308 
309   return r;
310 }
311 
312 #ifdef __cplusplus
313 }  // extern "C"
314 }  // namespace libyuv
315 #endif
316