Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-videodev/util.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2014-2015 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #include "util.h"
20
21 #include <pj/assert.h>
22 #include <pj/errno.h>
23 #include <pj/log.h>
24
25 #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
26
27 #if defined(PJMEDIA_HAS_LIBYUV) && PJMEDIA_HAS_LIBYUV != 0
28 /* When using newer external libyuv, allow use of deprecated type uint8 */
29 #define LIBYUV_LEGACY_TYPES
30 #include <libyuv.h>
31 #define HAS_ROTATION 1
32 #else
33 #define HAS_ROTATION 0
34 #endif
35
36 #define THIS_FILE "vid_util.c"
37
38 pj_status_t
39 pjmedia_vid_dev_conv_create_converter(pjmedia_vid_dev_conv *conv,
40 pj_pool_t *pool,
41 pjmedia_format *fmt,
42 pjmedia_rect_size src_size,
43 pjmedia_rect_size dst_size,
44 pj_bool_t handle_rotation,
45 pj_bool_t maintain_aspect_ratio)
46 {
47 pj_status_t status;
48 pjmedia_conversion_param conv_param;
49 const pjmedia_video_format_info *vfi;
50
51 pj_assert((src_size.w == dst_size.w || src_size.h == dst_size.h) ||
52 (src_size.w == dst_size.h || src_size.h == dst_size.w));
53
54 if (conv->conv)
55 return PJ_SUCCESS;
56
57 if (fmt->id != PJMEDIA_FORMAT_I420 && fmt->id != PJMEDIA_FORMAT_BGRA)
58 return PJ_EINVAL;
59
60 /* Currently, for BGRA format, device must handle the rotation. */
61 if (fmt->id == PJMEDIA_FORMAT_BGRA && handle_rotation)
62 return PJ_ENOTSUP;
63
64 if (handle_rotation) {
65 #if !HAS_ROTATION
66 return PJ_ENOTSUP;
67 #endif
68 }
69
70 conv->src_size = src_size;
71 conv->dst_size = dst_size;
72 conv->handle_rotation = handle_rotation;
73 pjmedia_format_copy(&conv->fmt, fmt);
74 pjmedia_format_copy(&conv_param.src, fmt);
75 pjmedia_format_copy(&conv_param.dst, fmt);
76
77 /* If we do the rotation, the conversion's source size must be the same
78 * as the device's original size. Otherwise, frames that require conversion
79 * are the ones of which orientation differ by 90 or 270 degrees from the
80 * destination size.
81 */
82 if (handle_rotation) {
83 conv_param.src.det.vid.size = src_size;
84 } else {
85 conv_param.src.det.vid.size.w = dst_size.h;
86 conv_param.src.det.vid.size.h = dst_size.w;
87 }
88
89 /* Maintaining aspect ratio requires filling the left&right /
90 * top&bottom area with black color.
91 * Currently it is only supported for I420.
92 * TODO: support BGRA as well
93 */
94 if (fmt->id != PJMEDIA_FORMAT_I420)
95 maintain_aspect_ratio = PJ_FALSE;
96
97 /* Calculate the size after rotation.
98 * If aspect ratio doesn't need to be maintained, rot_size is simply equal
99 * to the destination size. Otherwise, we need to fit the rotated frame
100 * to height or to width.
101 */
102 conv->maintain_aspect_ratio = maintain_aspect_ratio;
103 if (maintain_aspect_ratio) {
104 conv->fit_to_h = (dst_size.w >= dst_size.h? PJ_TRUE: PJ_FALSE);
105 if (conv->fit_to_h) { /* Fit to height */
106 conv->rot_size.h = dst_size.h;
107 conv->rot_size.w = dst_size.h * dst_size.h / dst_size.w;
108 /* Make sure the width difference is divisible by four
109 * so we can have equal padding left and right.
110 */
111 conv->rot_size.w += (dst_size.w - conv->rot_size.w) % 4;
112 conv->pad = (conv->dst_size.w - conv->rot_size.w) / 2;
113 } else { /* Fit to width */
114 conv->rot_size.w = dst_size.w;
115 conv->rot_size.h = dst_size.w * dst_size.w / dst_size.h;
116 conv->rot_size.h += (dst_size.h - conv->rot_size.h) % 4;
117 conv->pad = (conv->dst_size.h - conv->rot_size.h) / 2;
118 }
119 } else {
120 conv->rot_size = dst_size;
121 }
122
123 /* Calculate the size after resizing. */
124 if (handle_rotation) {
125 /* If we do the rotation, conversion is done before rotation. */
126 if (maintain_aspect_ratio) {
127 /* Since aspect ratio is maintained, the long side after
128 * conversion must be the same as before conversion.
129 * For example: 352x288 will be converted to 288x236
130 */
131 pj_size_t long_s = (conv->rot_size.h > conv->rot_size.w?
132 conv->rot_size.h: conv->rot_size.w);
133 pj_size_t short_s = (conv->rot_size.h > conv->rot_size.w?
134 conv->rot_size.w: conv->rot_size.h);
135 if (src_size.w > src_size.h) {
136 conv->res_size.w = long_s;
137 conv->res_size.h = short_s;
138 } else {
139 conv->res_size.w = short_s;
140 conv->res_size.h = long_s;
141 }
142 } else {
143 /* We don't need to maintain aspect ratio,
144 * so just swap the width and height.
145 * For example: 352x288 will be resized to 288x352
146 */
147 conv->res_size.w = src_size.h;
148 conv->res_size.h = src_size.w;
149 }
150 conv_param.dst.det.vid.size = conv->res_size;
151 } else {
152 conv->res_size = conv->rot_size;
153 conv_param.dst.det.vid.size = conv->rot_size;
154 }
155
156 status = pjmedia_converter_create(NULL, pool, &conv_param,
157 &conv->conv);
158 if (status != PJ_SUCCESS) {
159 PJ_LOG(3, (THIS_FILE, "Error creating converter"));
160 return status;
161 }
162
163 vfi = pjmedia_get_video_format_info(NULL, fmt->id);
164 pj_assert(vfi);
165
166 conv->wxh = conv->dst_size.w * conv->dst_size.h;
167 conv->src_frame_size = dst_size.w * dst_size.h * vfi->bpp / 8;
168 conv->conv_frame_size = conv->rot_size.w * conv->rot_size.h *
169 vfi->bpp / 8;
170 conv->conv_buf = pj_pool_alloc(pool, conv->src_frame_size);
171
172 pjmedia_vid_dev_conv_set_rotation(conv, PJMEDIA_ORIENT_NATURAL);
173
174 PJ_LOG(4, (THIS_FILE, "Orientation converter created: %dx%d to %dx%d, "
175 "maintain aspect ratio=%s",
176 conv_param.src.det.vid.size.w,
177 conv_param.src.det.vid.size.h,
178 conv_param.dst.det.vid.size.w,
179 conv_param.dst.det.vid.size.h,
180 maintain_aspect_ratio? "yes": "no"));
181
182 return PJ_SUCCESS;
183 }
184
185 void pjmedia_vid_dev_conv_set_rotation(pjmedia_vid_dev_conv *conv,
186 pjmedia_orient rotation)
187 {
188 pjmedia_rect_size new_size = conv->src_size;
189
190 conv->rotation = rotation;
191
192 if (rotation == PJMEDIA_ORIENT_ROTATE_90DEG ||
193 rotation == PJMEDIA_ORIENT_ROTATE_270DEG)
194 {
195 new_size.w = conv->src_size.h;
196 new_size.h = conv->src_size.w;
197 }
198
199 /* Check whether new size (size after rotation) and destination
200 * are both portrait or both landscape. If yes, resize will not
201 * be required in pjmedia_vid_dev_conv_resize_and_rotate() below.
202 * For example, 352x288 frame rotated 270 degrees will fit into
203 * a destination frame of 288x352 (no resize needed).
204 */
205 if ((new_size.w > new_size.h && conv->dst_size.w > conv->dst_size.h) ||
206 (new_size.h > new_size.w && conv->dst_size.h > conv->dst_size.w))
207 {
208 conv->match_src_dst = PJ_TRUE;
209 } else {
210 conv->match_src_dst = PJ_FALSE;
211 }
212 }
213
214 pj_status_t pjmedia_vid_dev_conv_resize_and_rotate(pjmedia_vid_dev_conv *conv,
215 void *src_buf,
216 void **result)
217 {
218 #define swap(a, b) {pj_uint8_t *c = a; a = b; b = c;}
219
220 pj_status_t status;
221 pjmedia_frame src_frame, dst_frame;
222 pjmedia_rect_size src_size = conv->src_size;
223 pj_uint8_t *src = src_buf;
224 pj_uint8_t *dst = conv->conv_buf;
225
226 pj_assert(src_buf);
227
228 if (!conv->conv) return PJ_EINVALIDOP;
229
230 if (!conv->match_src_dst) {
231 /* We need to resize. */
232 src_frame.buf = src;
233 dst_frame.buf = dst;
234 src_frame.size = conv->src_frame_size;
235 dst_frame.size = conv->conv_frame_size;
236
237 status = pjmedia_converter_convert(conv->conv, &src_frame, &dst_frame);
238 if (status != PJ_SUCCESS) {
239 PJ_LOG(3, (THIS_FILE, "Failed to convert frame"));
240 return status;
241 }
242
243 src_size = conv->res_size;
244
245 swap(src, dst);
246 }
247
248 if (conv->handle_rotation && conv->rotation != PJMEDIA_ORIENT_NATURAL) {
249 /* We need to do rotation. */
250 if (conv->fmt.id == PJMEDIA_FORMAT_I420) {
251 pjmedia_rect_size dst_size = src_size;
252 pj_size_t p_len = src_size.w * src_size.h;
253
254 if (conv->rotation == PJMEDIA_ORIENT_ROTATE_90DEG ||
255 conv->rotation == PJMEDIA_ORIENT_ROTATE_270DEG)
256 {
257 dst_size.w = src_size.h;
258 dst_size.h = src_size.w;
259 }
260
261 #if defined(PJMEDIA_HAS_LIBYUV) && PJMEDIA_HAS_LIBYUV != 0
262 enum RotationMode mode;
263
264 switch (conv->rotation) {
265 case PJMEDIA_ORIENT_ROTATE_90DEG:
266 mode = kRotate90;
267 break;
268 case PJMEDIA_ORIENT_ROTATE_180DEG:
269 mode = kRotate180;
270 break;
271 case PJMEDIA_ORIENT_ROTATE_270DEG:
272 mode = kRotate270;
273 break;
274 default:
275 mode = kRotate0;
276 }
277
278 I420Rotate(src, src_size.w,
279 src+p_len, src_size.w/2,
280 src+p_len+p_len/4, src_size.w/2,
281 dst, dst_size.w,
282 dst+p_len, dst_size.w/2,
283 dst+p_len+p_len/4, dst_size.w/2,
284 src_size.w, src_size.h, mode);
285
286 swap(src, dst);
287 #else
288 PJ_UNUSED_ARG(p_len);
289 PJ_UNUSED_ARG(dst_size);
290 #endif
291 }
292 }
293
294 if (!conv->match_src_dst && conv->maintain_aspect_ratio) {
295 /* Center the frame and fill the area with black color */
296 if (conv->fmt.id == PJMEDIA_FORMAT_I420) {
297 unsigned i = 0;
298 pj_uint8_t *pdst = dst;
299 pj_uint8_t *psrc = src;
300 pj_size_t p_len_src = 0, p_len_dst = conv->wxh;
301 int pad = conv->pad;
302
303 pj_bzero(pdst, p_len_dst);
304
305 if (conv->fit_to_h) {
306 /* Fill the left and right with black */
307 for (; i < conv->dst_size.h; ++i) {
308 pdst += pad;
309 pj_memcpy(pdst, psrc, conv->rot_size.w);
310 pdst += conv->rot_size.w;
311 psrc += conv->rot_size.w;
312 pdst += pad;
313 }
314 } else {
315 /* Fill the top and bottom with black */
316 p_len_src = conv->rot_size.w * conv->rot_size.h;
317 pj_memcpy(pdst + conv->rot_size.w * pad, psrc, p_len_src);
318 psrc += p_len_src;
319 pdst += p_len_dst;
320 }
321
322 /* Fill the U&V components with 0x80 to make it black.
323 * Bzero-ing will make the area look green instead.
324 */
325 pj_memset(pdst, 0x80, p_len_dst/2);
326 pad /= 2;
327 if (conv->fit_to_h) {
328 p_len_src = conv->rot_size.w / 2;
329 for (i = conv->dst_size.h; i > 0; --i) {
330 pdst += pad;
331 pj_memcpy(pdst, psrc, p_len_src);
332 pdst += p_len_src;
333 psrc += p_len_src;
334 pdst += pad;
335 }
336 } else {
337 pj_uint8_t *U, *V;
338 pj_size_t gap = conv->rot_size.w * pad / 2;
339
340 p_len_src /= 4;
341 U = pdst;
342 V = U + p_len_dst/4;
343
344 pj_memcpy(U + gap, psrc, p_len_src);
345 psrc += p_len_src;
346 pj_memcpy(V + gap, psrc, p_len_src);
347 }
348
349 swap(src, dst);
350 }
351 }
352
353 *result = src;
354
355 return PJ_SUCCESS;
356 }
357
358 void pjmedia_vid_dev_conv_destroy_converter(pjmedia_vid_dev_conv *conv)
359 {
360 if (conv->conv) {
361 pjmedia_converter_destroy(conv->conv);
362 conv->conv = NULL;
363 }
364 }
365
366 #endif /* PJMEDIA_HAS_VIDEO */