Debian Codegraph @

gst/videobox/gstvideobox.c

raw symbols SCIP
1 /* GStreamer
2 * Copyright (C) 1999 Erik Walthinsen <[email protected]>
3 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
4 * Copyright (C) 2010 Sebastian Dröge <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21 /**
22 * SECTION:element-videobox
23 * @title: videobox
24 * @see_also: #GstVideoCrop
25 *
26 * This plugin crops or enlarges the image. It takes 4 values as input, a
27 * top, bottom, left and right offset. Positive values will crop that much
28 * pixels from the respective border of the image, negative values will add
29 * that much pixels. When pixels are added, you can specify their color.
30 * Some predefined colors are usable with an enum property.
31 *
32 * The plugin is alpha channel aware and will try to negotiate with a format
33 * that supports alpha channels first. When alpha channel is active two
34 * other properties, alpha and border_alpha can be used to set the alpha
35 * values of the inner picture and the border respectively. an alpha value of
36 * 0.0 means total transparency, 1.0 is opaque.
37 *
38 * The videobox plugin has many uses such as doing a mosaic of pictures,
39 * letterboxing video, cutting out pieces of video, picture in picture, etc..
40 *
41 * Setting autocrop to true changes the behavior of the plugin so that
42 * caps determine crop properties rather than the other way around: given
43 * input and output dimensions, the crop values are selected so that the
44 * smaller frame is effectively centered in the larger frame. This
45 * involves either cropping or padding.
46 *
47 * If you use autocrop there is little point in setting the other
48 * properties manually because they will be overridden if the caps change,
49 * but nothing stops you from doing so.
50 *
51 * Sample pipeline:
52 * |[
53 * gst-launch-1.0 videotestsrc ! videobox autocrop=true ! \
54 * "video/x-raw, width=600, height=400" ! videoconvert ! ximagesink
55 * ]|
56 */
57
58 #ifdef HAVE_CONFIG_H
59 #include "config.h"
60 #endif
61
62 #include "gstvideobox.h"
63 #include "gstvideoboxorc.h"
64
65 #include <math.h>
66 #include <string.h>
67
68 GST_DEBUG_CATEGORY_STATIC (videobox_debug);
69 #define GST_CAT_DEFAULT videobox_debug
70
71 static GQuark _size_quark;
72 static GQuark _colorspace_quark;
73
74 /* From videotestsrc.c */
75 static const guint8 yuv_sdtv_colors_Y[VIDEO_BOX_FILL_LAST] =
76 { 16, 145, 41, 81, 210, 235 };
77 static const guint8 yuv_sdtv_colors_U[VIDEO_BOX_FILL_LAST] =
78 { 128, 54, 240, 90, 16, 128 };
79 static const guint8 yuv_sdtv_colors_V[VIDEO_BOX_FILL_LAST] =
80 { 128, 34, 110, 240, 146, 128 };
81
82 static const guint8 yuv_hdtv_colors_Y[VIDEO_BOX_FILL_LAST] =
83 { 16, 173, 32, 63, 219, 235 };
84 static const guint8 yuv_hdtv_colors_U[VIDEO_BOX_FILL_LAST] =
85 { 128, 42, 240, 102, 16, 128 };
86 static const guint8 yuv_hdtv_colors_V[VIDEO_BOX_FILL_LAST] =
87 { 128, 26, 118, 240, 138, 128 };
88
89 static const guint8 rgb_colors_R[VIDEO_BOX_FILL_LAST] =
90 { 0, 0, 0, 255, 255, 255 };
91 static const guint8 rgb_colors_G[VIDEO_BOX_FILL_LAST] =
92 { 0, 255, 0, 0, 255, 255 };
93 static const guint8 rgb_colors_B[VIDEO_BOX_FILL_LAST] =
94 { 0, 0, 255, 0, 0, 255 };
95
96 /* Generated by -bad/ext/cog/generate_tables */
97 static const int cog_ycbcr_to_rgb_matrix_8bit_hdtv[] = {
98 298, 0, 459, -63514,
99 298, -55, -136, 19681,
100 298, 541, 0, -73988,
101 };
102
103 static const int cog_ycbcr_to_rgb_matrix_8bit_sdtv[] = {
104 298, 0, 409, -57068,
105 298, -100, -208, 34707,
106 298, 516, 0, -70870,
107 };
108
109 static const gint cog_rgb_to_ycbcr_matrix_8bit_hdtv[] = {
110 47, 157, 16, 4096,
111 -26, -87, 112, 32768,
112 112, -102, -10, 32768,
113 };
114
115 static const gint cog_rgb_to_ycbcr_matrix_8bit_sdtv[] = {
116 66, 129, 25, 4096,
117 -38, -74, 112, 32768,
118 112, -94, -18, 32768,
119 };
120
121 static const gint cog_ycbcr_sdtv_to_ycbcr_hdtv_matrix_8bit[] = {
122 256, -30, -53, 10600,
123 0, 261, 29, -4367,
124 0, 19, 262, -3289,
125 };
126
127 static const gint cog_ycbcr_hdtv_to_ycbcr_sdtv_matrix_8bit[] = {
128 256, 25, 49, -9536,
129 0, 253, -28, 3958,
130 0, -19, 252, 2918,
131 };
132
133 static const gint cog_identity_matrix_8bit[] = {
134 256, 0, 0, 0,
135 0, 256, 0, 0,
136 0, 0, 256, 0,
137 };
138
139 #define APPLY_MATRIX(m,o,v1,v2,v3) ((m[o*4] * v1 + m[o*4+1] * v2 + m[o*4+2] * v3 + m[o*4+3]) >> 8)
140
141 static void
142 fill_ayuv (GstVideoBoxFill fill_type, guint b_alpha,
143 GstVideoFrame * frame, gboolean sdtv)
144 {
145 guint32 empty_pixel;
146 guint8 *dest;
147 gint width, height;
148 gint stride;
149
150 width = GST_VIDEO_FRAME_WIDTH (frame);
151 height = GST_VIDEO_FRAME_HEIGHT (frame);
152
153 b_alpha = MIN (b_alpha, 255);
154
155 if (sdtv)
156 empty_pixel = GUINT32_FROM_BE ((b_alpha << 24) |
157 (yuv_sdtv_colors_Y[fill_type] << 16) |
158 (yuv_sdtv_colors_U[fill_type] << 8) | yuv_sdtv_colors_V[fill_type]);
159 else
160 empty_pixel = GUINT32_FROM_BE ((b_alpha << 24) |
161 (yuv_hdtv_colors_Y[fill_type] << 16) |
162 (yuv_hdtv_colors_U[fill_type] << 8) | yuv_hdtv_colors_V[fill_type]);
163
164 dest = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
165 stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
166
167 if (G_LIKELY (stride == 4 * width))
168 video_box_orc_splat_u32 ((guint32 *) dest, empty_pixel, width * height);
169 else if (height) {
170 for (; height; --height) {
171 video_box_orc_splat_u32 ((guint32 *) dest, empty_pixel, width);
172 dest += stride;
173 }
174 }
175 }
176
177 static void
178 copy_ayuv_ayuv (guint i_alpha, GstVideoFrame * dest_frame,
179 gboolean dest_sdtv, gint dest_x, gint dest_y, GstVideoFrame * src_frame,
180 gboolean src_sdtv, gint src_x, gint src_y, gint w, gint h)
181 {
182 gint i, j;
183 gint src_stride;
184 gint dest_stride;
185 guint8 *dest, *src;
186
187 src_stride = GST_VIDEO_FRAME_PLANE_STRIDE (src_frame, 0);
188 dest_stride = GST_VIDEO_FRAME_PLANE_STRIDE (dest_frame, 0);
189
190 src = GST_VIDEO_FRAME_PLANE_DATA (src_frame, 0);
191 dest = GST_VIDEO_FRAME_PLANE_DATA (dest_frame, 0);
192
193 dest = dest + dest_y * dest_stride + dest_x * 4;
194 src = src + src_y * src_stride + src_x * 4;
195
196 w *= 4;
197
198 if (dest_sdtv != src_sdtv) {
199 gint matrix[12];
200 gint y, u, v;
201
202 memcpy (matrix,
203 dest_sdtv ? cog_ycbcr_hdtv_to_ycbcr_sdtv_matrix_8bit :
204 cog_ycbcr_sdtv_to_ycbcr_hdtv_matrix_8bit, 12 * sizeof (gint));
205
206 for (i = 0; i < h; i++) {
207 for (j = 0; j < w; j += 4) {
208 /* ORC FIXME */
209 dest[j] = (src[j] * i_alpha) >> 8;
210 y = src[j + 1];
211 u = src[j + 2];
212 v = src[j + 3];
213 dest[j + 1] = APPLY_MATRIX (matrix, 0, y, u, v);
214 dest[j + 2] = APPLY_MATRIX (matrix, 1, y, u, v);
215 dest[j + 3] = APPLY_MATRIX (matrix, 2, y, u, v);
216 }
217 dest += dest_stride;
218 src += src_stride;
219 }
220 } else {
221 for (i = 0; i < h; i++) {
222 for (j = 0; j < w; j += 4) {
223 /* ORC FIXME */
224 dest[j] = (src[j] * i_alpha) >> 8;
225 dest[j + 1] = src[j + 1];
226 dest[j + 2] = src[j + 2];
227 dest[j + 3] = src[j + 3];
228 }
229 dest += dest_stride;
230 src += src_stride;
231 }
232 }
233 }
234
235 static void
236 copy_ayuv_i420 (guint i_alpha, GstVideoFrame * dest_frame,
237 gboolean dest_sdtv, gint dest_x, gint dest_y, GstVideoFrame * src_frame,
238 gboolean src_sdtv, gint src_x, gint src_y, gint w, gint h)
239 {
240 gint i, j;
241 guint8 *destY, *destY2, *destU, *destV;
242 gint dest_strideY, dest_strideU, dest_strideV;
243 const guint8 *src2;
244 gint src_stride;
245 gint y_idx, uv_idx;
246 gint y1, y2, y3, y4;
247 gint u1, u2, u3, u4;
248 gint v1, v2, v3, v4;
249 gint matrix[12];
250 guint8 *src;
251 gint dest_height, src_height, dest_width;
252
253 dest_height = GST_VIDEO_FRAME_HEIGHT (dest_frame);
254 dest_width = GST_VIDEO_FRAME_WIDTH (dest_frame);
255 src_height = GST_VIDEO_FRAME_HEIGHT (src_frame);
256
257 dest_strideY = GST_VIDEO_FRAME_COMP_STRIDE (dest_frame, 0);
258 dest_strideU = GST_VIDEO_FRAME_COMP_STRIDE (dest_frame, 1);
259 dest_strideV = GST_VIDEO_FRAME_COMP_STRIDE (dest_frame, 2);
260
261 src_stride = GST_VIDEO_FRAME_PLANE_STRIDE (src_frame, 0);
262
263 destY = GST_VIDEO_FRAME_COMP_DATA (dest_frame, 0);
264 destU = GST_VIDEO_FRAME_COMP_DATA (dest_frame, 1);
265 destV = GST_VIDEO_FRAME_COMP_DATA (dest_frame, 2);
266
267 destY = destY + dest_y * dest_strideY + dest_x;
268 destY2 = (dest_y < dest_height) ? destY + dest_strideY : destY;
269 destU = destU + (dest_y / 2) * dest_strideU + dest_x / 2;
270 destV = destV + (dest_y / 2) * dest_strideV + dest_x / 2;
271
272 src = GST_VIDEO_FRAME_PLANE_DATA (src_frame, 0);
273 src = src + src_y * src_stride + src_x * 4;
274 src2 = (src_y < src_height) ? src + src_stride : src;
275
276 h = dest_y + h;
277 w = dest_x + w;
278
279 if (src_sdtv != dest_sdtv)
280 memcpy (matrix,
281 dest_sdtv ? cog_ycbcr_hdtv_to_ycbcr_sdtv_matrix_8bit :
282 cog_ycbcr_sdtv_to_ycbcr_hdtv_matrix_8bit, 12 * sizeof (gint));
283 else
284 memcpy (matrix, cog_identity_matrix_8bit, 12 * sizeof (gint));
285
286 /* 1. Handle the first destination scanline specially if it
287 * doesn't start at the macro pixel boundary, i.e. blend
288 * with the background! */
289 if (dest_y % 2 == 1) {
290 /* 1.1. Handle the first destination pixel if it doesn't
291 * start at the macro pixel boundary, i.e. blend with
292 * the background! */
293 if (dest_x % 2 == 1) {
294 y1 = src[4 * 0 + 1];
295 u1 = src[4 * 0 + 2];
296 v1 = src[4 * 0 + 3];
297
298 destY[0] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
299 destU[0] =
300 CLAMP ((3 * destU[0] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 4, 0,
301 255);
302 destV[0] =
303 CLAMP ((3 * destV[0] + APPLY_MATRIX (matrix, 2, y1, u1, v1)) / 4, 0,
304 255);
305
306 j = dest_x + 1;
307 y_idx = uv_idx = 1;
308 } else {
309 j = dest_x;
310 y_idx = uv_idx = 0;
311 }
312
313 /* 1.2. Copy all macro pixels from the source to the destination
314 * but blend with the background because we're only filling
315 * the lower part of the macro pixels. */
316 for (; j < w - 1; j += 2) {
317 y1 = src[4 * y_idx + 1];
318 y2 = src[4 * y_idx + 4 + 1];
319
320 u1 = src[4 * y_idx + 2];
321 u2 = src[4 * y_idx + 4 + 2];
322
323 v1 = src[4 * y_idx + 3];
324 v2 = src[4 * y_idx + 4 + 3];
325
326 destY[y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
327 destY[y_idx + 1] = CLAMP (APPLY_MATRIX (matrix, 0, y2, u2, v2), 0, 255);
328 destU[uv_idx] = CLAMP (
329 (2 * destU[uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
330 v1) + APPLY_MATRIX (matrix, 1, y2, u2, v2)) / 4, 0, 255);
331 destV[uv_idx] = CLAMP (
332 (2 * destV[uv_idx] + APPLY_MATRIX (matrix, 2, y1, u1,
333 v1) + APPLY_MATRIX (matrix, 2, y2, u2, v2)) / 4, 0, 255);
334
335 y_idx += 2;
336 uv_idx++;
337 }
338
339 /* 1.3. Now copy the last pixel if one exists and blend it
340 * with the background because we only fill part of
341 * the macro pixel. In case this is the last pixel of
342 * the destination we will a larger part. */
343 if (j == w - 1 && j == dest_width - 1) {
344 y1 = src[4 * y_idx + 1];
345 u1 = src[4 * y_idx + 2];
346 v1 = src[4 * y_idx + 3];
347
348 destY[y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
349 destU[uv_idx] = CLAMP (
350 (destU[uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 2, 0, 255);
351 destV[uv_idx] = CLAMP (
352 (destV[uv_idx] + APPLY_MATRIX (matrix, 2, y1, u1, v1)) / 2, 0, 255);
353 } else if (j == w - 1) {
354 y1 = src[4 * y_idx + 1];
355 u1 = src[4 * y_idx + 2];
356 v1 = src[4 * y_idx + 3];
357
358 destY[y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
359 destU[uv_idx] = CLAMP (
360 (3 * destU[uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 4, 0,
361 255);
362 destV[uv_idx] =
363 CLAMP ((3 * destV[uv_idx] + APPLY_MATRIX (matrix, 2, y1, u1, v1)) / 4,
364 0, 255);
365 }
366
367 destY += dest_strideY;
368 destY2 += dest_strideY;
369 destU += dest_strideU;
370 destV += dest_strideV;
371 src += src_stride;
372 src2 += src_stride;
373 i = dest_y + 1;
374 } else {
375 i = dest_y;
376 }
377
378 /* 2. Copy all macro pixel scanlines, the destination scanline
379 * now starts at macro pixel boundary. */
380 for (; i < h - 1; i += 2) {
381 /* 2.1. Handle the first destination pixel if it doesn't
382 * start at the macro pixel boundary, i.e. blend with
383 * the background! */
384 if (dest_x % 2 == 1) {
385 y1 = src[4 * 0 + 1];
386 y2 = src2[4 * 0 + 1];
387 u1 = src[4 * 0 + 2];
388 u2 = src2[4 * 0 + 2];
389 v1 = src[4 * 0 + 3];
390 v2 = src2[4 * 0 + 3];
391
392 destY[0] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
393 destY2[0] = CLAMP (APPLY_MATRIX (matrix, 0, y2, u2, v2), 0, 255);
394 destU[0] = CLAMP (
395 (2 * destU[0] + APPLY_MATRIX (matrix, 1, y1, u1,
396 v1) + APPLY_MATRIX (matrix, 1, y2, u2, v2)) / 4, 0, 255);
397 destV[0] = CLAMP (
398 (2 * destV[0] + APPLY_MATRIX (matrix, 2, y1, u1,
399 v1) + APPLY_MATRIX (matrix, 2, y2, u2, v2)) / 4, 0, 255);
400 j = dest_x + 1;
401 y_idx = uv_idx = 1;
402 } else {
403 j = dest_x;
404 y_idx = uv_idx = 0;
405 }
406
407 /* 2.2. Copy all macro pixels from the source to the destination.
408 * All pixels now start at macro pixel boundary, i.e. no
409 * blending with the background is necessary. */
410 for (; j < w - 1; j += 2) {
411 y1 = src[4 * y_idx + 1];
412 y2 = src[4 * y_idx + 4 + 1];
413 y3 = src2[4 * y_idx + 1];
414 y4 = src2[4 * y_idx + 4 + 1];
415
416 u1 = src[4 * y_idx + 2];
417 u2 = src[4 * y_idx + 4 + 2];
418 u3 = src2[4 * y_idx + 2];
419 u4 = src2[4 * y_idx + 4 + 2];
420
421 v1 = src[4 * y_idx + 3];
422 v2 = src[4 * y_idx + 4 + 3];
423 v3 = src2[4 * y_idx + 3];
424 v4 = src2[4 * y_idx + 4 + 3];
425
426 destY[y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
427 destY[y_idx + 1] = CLAMP (APPLY_MATRIX (matrix, 0, y2, u2, v2), 0, 255);
428 destY2[y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y3, u3, v3), 0, 255);
429 destY2[y_idx + 1] = CLAMP (APPLY_MATRIX (matrix, 0, y4, u4, v4), 0, 255);
430
431 destU[uv_idx] = CLAMP (
432 (APPLY_MATRIX (matrix, 1, y1, u1, v1) + APPLY_MATRIX (matrix, 1, y2,
433 u2, v2) + APPLY_MATRIX (matrix, 1, y3, u3,
434 v3) + APPLY_MATRIX (matrix, 1, y4, u4, v4)) / 4, 0, 255);
435 destV[uv_idx] = CLAMP (
436 (APPLY_MATRIX (matrix, 2, y1, u1, v1) + APPLY_MATRIX (matrix, 2, y2,
437 u2, v2) + APPLY_MATRIX (matrix, 2, y3, u3,
438 v3) + APPLY_MATRIX (matrix, 2, y4, u4, v4)) / 4, 0, 255);
439
440 y_idx += 2;
441 uv_idx++;
442 }
443
444 /* 2.3. Now copy the last pixel if one exists and blend it
445 * with the background because we only fill part of
446 * the macro pixel. In case this is the last pixel of
447 * the destination we will a larger part. */
448 if (j == w - 1 && j == dest_width - 1) {
449 y1 = src[4 * y_idx + 1];
450 y2 = src2[4 * y_idx + 1];
451
452 u1 = src[4 * y_idx + 2];
453 u2 = src2[4 * y_idx + 2];
454
455 v1 = src[4 * y_idx + 3];
456 v2 = src2[4 * y_idx + 3];
457
458 destY[y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
459 destY2[y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y2, u2, v2), 0, 255);
460 destU[uv_idx] = CLAMP (
461 (APPLY_MATRIX (matrix, 1, y1, u1, v1) + APPLY_MATRIX (matrix, 2, y2,
462 u2, v2)) / 2, 0, 255);
463 destV[uv_idx] = CLAMP (
464 (APPLY_MATRIX (matrix, 1, y1, u1, v1) + APPLY_MATRIX (matrix, 2, y2,
465 u2, v2)) / 2, 0, 255);
466 } else if (j == w - 1) {
467 y1 = src[4 * y_idx + 1];
468 y2 = src2[4 * y_idx + 1];
469
470 u1 = src[4 * y_idx + 2];
471 u2 = src2[4 * y_idx + 2];
472
473 v1 = src[4 * y_idx + 3];
474 v2 = src2[4 * y_idx + 3];
475
476 destY[y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
477 destY2[y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y2, u2, v2), 0, 255);
478 destU[uv_idx] = CLAMP (
479 (2 * destU[uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
480 v1) + APPLY_MATRIX (matrix, 2, y2, u2, v2)) / 4, 0, 255);
481 destV[uv_idx] = CLAMP (
482 (2 * destV[uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
483 v1) + APPLY_MATRIX (matrix, 2, y2, u2, v2)) / 4, 0, 255);
484 }
485
486 destY += 2 * dest_strideY;
487 destY2 += 2 * dest_strideY;
488 destU += dest_strideU;
489 destV += dest_strideV;
490 src += 2 * src_stride;
491 src2 += 2 * src_stride;
492 }
493
494 /* 3. Handle the last scanline if one exists. This again
495 * doesn't start at macro pixel boundary but should
496 * only fill the upper part of the macro pixels. */
497 if (i == h - 1 && i == dest_height - 1) {
498 /* 3.1. Handle the first destination pixel if it doesn't
499 * start at the macro pixel boundary, i.e. blend with
500 * the background! */
501 if (dest_x % 2 == 1) {
502 y1 = src[4 * 0 + 1];
503 u1 = src[4 * 0 + 2];
504 v1 = src[4 * 0 + 3];
505
506 destY[0] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
507 destU[0] =
508 CLAMP ((destU[0] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 2, 0, 255);
509 destV[0] =
510 CLAMP ((destV[0] + APPLY_MATRIX (matrix, 2, y1, u1, v1)) / 2, 0, 255);
511
512 j = dest_x + 1;
513 y_idx = uv_idx = 1;
514 } else {
515 j = dest_x;
516 y_idx = uv_idx = 0;
517 }
518
519 /* 3.2. Copy all macro pixels from the source to the destination
520 * but blend with the background because we're only filling
521 * the upper part of the macro pixels. */
522 for (; j < w - 1; j += 2) {
523 y1 = src[4 * y_idx + 1];
524 y2 = src[4 * y_idx + 4 + 1];
525
526 u1 = src[4 * y_idx + 2];
527 u2 = src[4 * y_idx + 4 + 2];
528
529 v1 = src[4 * y_idx + 3];
530 v2 = src[4 * y_idx + 4 + 3];
531
532 destY[y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
533 destY[y_idx + 1] = CLAMP (APPLY_MATRIX (matrix, 0, y2, u2, v2), 0, 255);
534
535 destU[uv_idx] = CLAMP (
536 (2 * destU[uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
537 v1) + APPLY_MATRIX (matrix, 1, y2, u2, v2)) / 4, 0, 255);
538 destV[uv_idx] = CLAMP (
539 (2 * destV[uv_idx] + APPLY_MATRIX (matrix, 2, y1, u1,
540 v1) + APPLY_MATRIX (matrix, 2, y2, u2, v2)) / 4, 0, 255);
541
542 y_idx += 2;
543 uv_idx++;
544 }
545
546 /* 3.3. Now copy the last pixel if one exists and blend it
547 * with the background because we only fill part of
548 * the macro pixel. In case this is the last pixel of
549 * the destination we will a larger part. */
550 if (j == w - 1 && j == dest_width - 1) {
551 y1 = src[4 * y_idx + 1];
552 u1 = src[4 * y_idx + 2];
553 v1 = src[4 * y_idx + 3];
554
555 destY[y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
556 destU[uv_idx] = CLAMP (
557 (destU[uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 2, 0, 255);
558 destV[uv_idx] = CLAMP (
559 (destV[uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 2, 0, 255);
560 } else if (j == w - 1) {
561 y1 = src[4 * y_idx + 1];
562 u1 = src[4 * y_idx + 2];
563 v1 = src[4 * y_idx + 3];
564
565 destY[y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
566 destU[uv_idx] = CLAMP (
567 (3 * destU[uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 4, 0,
568 255);
569 destV[uv_idx] =
570 CLAMP ((3 * destV[uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 4,
571 0, 255);
572 }
573 } else if (i == h - 1) {
574 /* 3.1. Handle the first destination pixel if it doesn't
575 * start at the macro pixel boundary, i.e. blend with
576 * the background! */
577 if (dest_x % 2 == 1) {
578 y1 = src[4 * 0 + 1];
579 u1 = src[4 * 0 + 2];
580 v1 = src[4 * 0 + 3];
581
582 destY[0] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
583 destU[0] =
584 CLAMP ((3 * destU[0] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 4, 0,
585 255);
586 destV[0] =
587 CLAMP ((3 * destV[0] + APPLY_MATRIX (matrix, 2, y1, u1, v1)) / 4, 0,
588 255);
589
590 j = dest_x + 1;
591 y_idx = uv_idx = 1;
592 } else {
593 j = dest_x;
594 y_idx = uv_idx = 0;
595 }
596
597 /* 3.2. Copy all macro pixels from the source to the destination
598 * but blend with the background because we're only filling
599 * the upper part of the macro pixels. */
600 for (; j < w - 1; j += 2) {
601 y1 = src[4 * y_idx + 1];
602 y2 = src[4 * y_idx + 4 + 1];
603
604 u1 = src[4 * y_idx + 2];
605 u2 = src[4 * y_idx + 4 + 2];
606
607 v1 = src[4 * y_idx + 3];
608 v2 = src[4 * y_idx + 4 + 3];
609
610 destY[y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
611 destY[y_idx + 1] = CLAMP (APPLY_MATRIX (matrix, 0, y2, u2, v2), 0, 255);
612
613 destU[uv_idx] = CLAMP (
614 (2 * destU[uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
615 v1) + APPLY_MATRIX (matrix, 1, y2, u2, v2)) / 4, 0, 255);
616 destV[uv_idx] = CLAMP (
617 (2 * destV[uv_idx] + APPLY_MATRIX (matrix, 2, y1, u1,
618 v1) + APPLY_MATRIX (matrix, 2, y2, u2, v2)) / 4, 0, 255);
619
620 y_idx += 2;
621 uv_idx++;
622 }
623
624 /* 3.3. Now copy the last pixel if one exists and blend it
625 * with the background because we only fill part of
626 * the macro pixel. In case this is the last pixel of
627 * the destination we will a larger part. */
628 if (j == w - 1 && j == dest_width - 1) {
629 y1 = src[4 * y_idx + 1];
630 u1 = src[4 * y_idx + 2];
631 v1 = src[4 * y_idx + 3];
632
633 destY[y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
634 destU[uv_idx] = CLAMP (
635 (destU[uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 2, 0, 255);
636 destV[uv_idx] = CLAMP (
637 (destV[uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 2, 0, 255);
638 } else if (j == w - 1) {
639 y1 = src[4 * y_idx + 1];
640 u1 = src[4 * y_idx + 2];
641 v1 = src[4 * y_idx + 3];
642
643 destY[y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
644 destU[uv_idx] = CLAMP (
645 (3 * destU[uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 4, 0,
646 255);
647 destV[uv_idx] =
648 CLAMP ((3 * destV[uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 4,
649 0, 255);
650 }
651 }
652 }
653
654 static void
655 fill_planar_yuv (GstVideoBoxFill fill_type, guint b_alpha,
656 GstVideoFrame * frame, gboolean sdtv)
657 {
658 guint8 empty_pixel[3];
659 guint8 *destY, *destU, *destV;
660 gint strideY, strideU, strideV;
661 gint heightY, heightU, heightV;
662 gint widthY, widthU, widthV;
663
664 if (sdtv) {
665 empty_pixel[0] = yuv_sdtv_colors_Y[fill_type];
666 empty_pixel[1] = yuv_sdtv_colors_U[fill_type];
667 empty_pixel[2] = yuv_sdtv_colors_V[fill_type];
668 } else {
669 empty_pixel[0] = yuv_hdtv_colors_Y[fill_type];
670 empty_pixel[1] = yuv_hdtv_colors_U[fill_type];
671 empty_pixel[2] = yuv_hdtv_colors_V[fill_type];
672 }
673
674 strideY = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
675 strideU = GST_VIDEO_FRAME_COMP_STRIDE (frame, 1);
676 strideV = GST_VIDEO_FRAME_COMP_STRIDE (frame, 2);
677
678 destY = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
679 destU = GST_VIDEO_FRAME_COMP_DATA (frame, 1);
680 destV = GST_VIDEO_FRAME_COMP_DATA (frame, 2);
681
682 widthY = GST_VIDEO_FRAME_COMP_WIDTH (frame, 0);
683 widthU = GST_VIDEO_FRAME_COMP_WIDTH (frame, 1);
684 widthV = GST_VIDEO_FRAME_COMP_WIDTH (frame, 2);
685
686 heightY = GST_VIDEO_FRAME_COMP_HEIGHT (frame, 0);
687 heightU = GST_VIDEO_FRAME_COMP_HEIGHT (frame, 1);
688 heightV = GST_VIDEO_FRAME_COMP_HEIGHT (frame, 2);
689
690 if (strideY == widthY) {
691 memset (destY, empty_pixel[0], strideY * heightY);
692 } else if (heightY) {
693 for (; heightY; --heightY) {
694 memset (destY, empty_pixel[0], widthY);
695 destY += strideY;
696 }
697 }
698 if (strideU == widthU) {
699 memset (destU, empty_pixel[1], strideU * heightU);
700 } else if (heightU) {
701 for (; heightU; --heightU) {
702 memset (destU, empty_pixel[1], widthU);
703 destU += strideU;
704 }
705 }
706 if (strideV == widthV) {
707 memset (destV, empty_pixel[2], strideV * heightV);
708 } else if (heightV) {
709 for (; heightV; --heightV) {
710 memset (destV, empty_pixel[2], widthV);
711 destV += strideV;
712 }
713 }
714 }
715
716 static void
717 copy_y444_y444 (guint i_alpha, GstVideoFrame * dest,
718 gboolean dest_sdtv, gint dest_x, gint dest_y, GstVideoFrame * src,
719 gboolean src_sdtv, gint src_x, gint src_y, gint w, gint h)
720 {
721 gint i, j;
722 guint8 *destY, *destU, *destV;
723 const guint8 *srcY, *srcU, *srcV;
724 gint dest_strideY, dest_strideU, dest_strideV;
725 gint src_strideY, src_strideU, src_strideV;
726
727 dest_strideY = GST_VIDEO_FRAME_COMP_STRIDE (dest, 0);
728 dest_strideU = GST_VIDEO_FRAME_COMP_STRIDE (dest, 1);
729 dest_strideV = GST_VIDEO_FRAME_COMP_STRIDE (dest, 2);
730
731 src_strideY = GST_VIDEO_FRAME_COMP_STRIDE (src, 0);
732 src_strideU = GST_VIDEO_FRAME_COMP_STRIDE (src, 1);
733 src_strideV = GST_VIDEO_FRAME_COMP_STRIDE (src, 2);
734
735 destY = GST_VIDEO_FRAME_COMP_DATA (dest, 0);
736 destU = GST_VIDEO_FRAME_COMP_DATA (dest, 1);
737 destV = GST_VIDEO_FRAME_COMP_DATA (dest, 2);
738
739 srcY = GST_VIDEO_FRAME_COMP_DATA (src, 0);
740 srcU = GST_VIDEO_FRAME_COMP_DATA (src, 1);
741 srcV = GST_VIDEO_FRAME_COMP_DATA (src, 2);
742
743 destY = destY + dest_y * dest_strideY + dest_x;
744 destU = destU + dest_y * dest_strideU + dest_x;
745 destV = destV + dest_y * dest_strideV + dest_x;
746
747 srcY = srcY + src_y * src_strideY + src_x;
748 srcU = srcU + src_y * src_strideU + src_x;
749 srcV = srcV + src_y * src_strideV + src_x;
750
751 if (src_sdtv != dest_sdtv) {
752 gint matrix[12];
753 gint y, u, v;
754
755 memcpy (matrix,
756 dest_sdtv ? cog_ycbcr_hdtv_to_ycbcr_sdtv_matrix_8bit :
757 cog_ycbcr_sdtv_to_ycbcr_hdtv_matrix_8bit, 12 * sizeof (gint));
758
759 for (i = 0; i < h; i++) {
760 for (j = 0; j < w; j++) {
761 y = APPLY_MATRIX (matrix, 0, srcY[j], srcU[j], srcV[j]);
762 u = APPLY_MATRIX (matrix, 1, srcY[j], srcU[j], srcV[j]);
763 v = APPLY_MATRIX (matrix, 2, srcY[j], srcU[j], srcV[j]);
764
765 destY[j] = y;
766 destU[j] = u;
767 destV[j] = v;
768 }
769 destY += dest_strideY;
770 destU += dest_strideU;
771 destV += dest_strideV;
772
773 srcY += src_strideY;
774 srcU += src_strideU;
775 srcV += src_strideV;
776 }
777 } else {
778 for (i = 0; i < h; i++) {
779 memcpy (destY, srcY, w);
780 memcpy (destU, srcU, w);
781 memcpy (destV, srcV, w);
782
783 destY += dest_strideY;
784 destU += dest_strideU;
785 destV += dest_strideV;
786
787 srcY += src_strideY;
788 srcU += src_strideU;
789 srcV += src_strideV;
790 }
791 }
792 }
793
794 static void
795 copy_y42b_y42b (guint i_alpha, GstVideoFrame * dest,
796 gboolean dest_sdtv, gint dest_x, gint dest_y, GstVideoFrame * src,
797 gboolean src_sdtv, gint src_x, gint src_y, gint w, gint h)
798 {
799 gint i, j;
800 guint8 *destY, *destU, *destV;
801 const guint8 *srcY, *srcU, *srcV;
802 gint dest_strideY, dest_strideU, dest_strideV;
803 gint src_strideY, src_strideU, src_strideV;
804 gint src_y_idx, src_uv_idx;
805 gint dest_y_idx, dest_uv_idx;
806 gint matrix[12];
807 gint y1, y2;
808 gint u1, u2;
809 gint v1, v2;
810 gint dest_width;
811
812 dest_width = GST_VIDEO_FRAME_WIDTH (dest);
813
814 dest_strideY = GST_VIDEO_FRAME_COMP_STRIDE (dest, 0);
815 dest_strideU = GST_VIDEO_FRAME_COMP_STRIDE (dest, 1);
816 dest_strideV = GST_VIDEO_FRAME_COMP_STRIDE (dest, 2);
817
818 src_strideY = GST_VIDEO_FRAME_COMP_STRIDE (src, 0);
819 src_strideU = GST_VIDEO_FRAME_COMP_STRIDE (src, 1);
820 src_strideV = GST_VIDEO_FRAME_COMP_STRIDE (src, 2);
821
822 destY = GST_VIDEO_FRAME_COMP_DATA (dest, 0);
823 destU = GST_VIDEO_FRAME_COMP_DATA (dest, 1);
824 destV = GST_VIDEO_FRAME_COMP_DATA (dest, 2);
825
826 srcY = GST_VIDEO_FRAME_COMP_DATA (src, 0);
827 srcU = GST_VIDEO_FRAME_COMP_DATA (src, 1);
828 srcV = GST_VIDEO_FRAME_COMP_DATA (src, 2);
829
830 destY = destY + dest_y * dest_strideY + dest_x;
831 destU = destU + dest_y * dest_strideU + dest_x / 2;
832 destV = destV + dest_y * dest_strideV + dest_x / 2;
833
834 srcY = srcY + src_y * src_strideY + src_x;
835 srcU = srcU + src_y * src_strideU + src_x / 2;
836 srcV = srcV + src_y * src_strideV + src_x / 2;
837
838 h = dest_y + h;
839 w = dest_x + w;
840
841 if (src_sdtv != dest_sdtv)
842 memcpy (matrix,
843 dest_sdtv ? cog_ycbcr_hdtv_to_ycbcr_sdtv_matrix_8bit :
844 cog_ycbcr_sdtv_to_ycbcr_hdtv_matrix_8bit, 12 * sizeof (gint));
845 else
846 memcpy (matrix, cog_identity_matrix_8bit, 12 * sizeof (gint));
847
848 /* 1. Copy all macro pixel scanlines, the destination scanline
849 * now starts at macro pixel boundary. */
850 for (i = dest_y; i < h; i++) {
851 /* 1.1. Handle the first destination pixel if it doesn't
852 * start at the macro pixel boundary, i.e. blend with
853 * the background! */
854 if (dest_x % 2 == 1) {
855 y1 = srcY[0];
856 u1 = srcU[0];
857 v1 = srcV[0];
858
859 destY[0] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
860 destU[0] = CLAMP (
861 (destU[0] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 2, 0, 255);
862 destV[0] = CLAMP (
863 (destV[0] + APPLY_MATRIX (matrix, 2, y1, u1, v1)) / 2, 0, 255);
864 j = dest_x + 1;
865 src_y_idx = dest_y_idx = dest_uv_idx = 1;
866 src_uv_idx = (src_x % 2) + 1;
867 } else {
868 j = dest_x;
869 src_y_idx = dest_y_idx = dest_uv_idx = 0;
870 src_uv_idx = (src_x % 2);
871 }
872
873 /* 1.2. Copy all macro pixels from the source to the destination.
874 * All pixels now start at macro pixel boundary, i.e. no
875 * blending with the background is necessary. */
876 for (; j < w - 1; j += 2) {
877 y1 = srcY[src_y_idx];
878 y2 = srcY[src_y_idx + 1];
879
880 u1 = srcU[src_uv_idx / 2];
881 v1 = srcV[src_uv_idx / 2];
882 src_uv_idx++;
883 u2 = srcU[src_uv_idx / 2];
884 v2 = srcV[src_uv_idx / 2];
885 src_uv_idx++;
886
887 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
888 destY[dest_y_idx + 1] =
889 CLAMP (APPLY_MATRIX (matrix, 0, y2, u2, v2), 0, 255);
890
891 destU[dest_uv_idx] = CLAMP (
892 (APPLY_MATRIX (matrix, 1, y1, u1, v1) + APPLY_MATRIX (matrix, 1, y2,
893 u2, v2)) / 2, 0, 255);
894 destV[dest_uv_idx] = CLAMP (
895 (APPLY_MATRIX (matrix, 2, y1, u1, v1) + APPLY_MATRIX (matrix, 2, y2,
896 u2, v2)) / 2, 0, 255);
897
898 dest_y_idx += 2;
899 src_y_idx += 2;
900 dest_uv_idx++;
901 }
902
903 /* 1.3. Now copy the last pixel if one exists and blend it
904 * with the background because we only fill part of
905 * the macro pixel. In case this is the last pixel of
906 * the destination we will a larger part. */
907 if (j == w - 1 && j == dest_width - 1) {
908 y1 = srcY[src_y_idx];
909 u1 = srcU[src_uv_idx / 2];
910 v1 = srcV[src_uv_idx / 2];
911
912 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
913 destU[dest_uv_idx] = CLAMP (APPLY_MATRIX (matrix, 1, y1, u1, v1), 0, 255);
914 destV[dest_uv_idx] = CLAMP (APPLY_MATRIX (matrix, 1, y1, u1, v1), 0, 255);
915 } else if (j == w - 1) {
916 y1 = srcY[src_y_idx];
917 u1 = srcU[src_uv_idx / 2];
918 v1 = srcV[src_uv_idx / 2];
919
920 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
921 destU[dest_uv_idx] = CLAMP (
922 (destU[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
923 v1)) / 2, 0, 255);
924 destV[dest_uv_idx] = CLAMP (
925 (destV[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
926 v1)) / 2, 0, 255);
927 }
928
929 destY += dest_strideY;
930 destU += dest_strideU;
931 destV += dest_strideV;
932 srcY += src_strideY;
933
934 srcU += src_strideU;
935 srcV += src_strideV;
936 }
937 }
938
939 static void
940 copy_y41b_y41b (guint i_alpha, GstVideoFrame * dest,
941 gboolean dest_sdtv, gint dest_x, gint dest_y, GstVideoFrame * src,
942 gboolean src_sdtv, gint src_x, gint src_y, gint w, gint h)
943 {
944 gint i, j;
945 guint8 *destY, *destU, *destV;
946 const guint8 *srcY, *srcU, *srcV;
947 gint dest_strideY, dest_strideU, dest_strideV;
948 gint src_strideY, src_strideU, src_strideV;
949 gint src_y_idx, src_uv_idx;
950 gint dest_y_idx, dest_uv_idx;
951 gint matrix[12];
952 gint y1, y2, y3, y4;
953 gint u1, u2, u3, u4;
954 gint v1, v2, v3, v4;
955 gint dest_width;
956
957 dest_width = GST_VIDEO_FRAME_WIDTH (dest);
958
959 dest_strideY = GST_VIDEO_FRAME_COMP_STRIDE (dest, 0);
960 dest_strideU = GST_VIDEO_FRAME_COMP_STRIDE (dest, 1);
961 dest_strideV = GST_VIDEO_FRAME_COMP_STRIDE (dest, 2);
962
963 src_strideY = GST_VIDEO_FRAME_COMP_STRIDE (src, 0);
964 src_strideU = GST_VIDEO_FRAME_COMP_STRIDE (src, 1);
965 src_strideV = GST_VIDEO_FRAME_COMP_STRIDE (src, 2);
966
967 destY = GST_VIDEO_FRAME_COMP_DATA (dest, 0);
968 destU = GST_VIDEO_FRAME_COMP_DATA (dest, 1);
969 destV = GST_VIDEO_FRAME_COMP_DATA (dest, 2);
970
971 srcY = GST_VIDEO_FRAME_COMP_DATA (src, 0);
972 srcU = GST_VIDEO_FRAME_COMP_DATA (src, 1);
973 srcV = GST_VIDEO_FRAME_COMP_DATA (src, 2);
974
975 destY = destY + dest_y * dest_strideY + dest_x;
976 destU = destU + dest_y * dest_strideU + dest_x / 4;
977 destV = destV + dest_y * dest_strideV + dest_x / 4;
978
979 srcY = srcY + src_y * src_strideY + src_x;
980 srcU = srcU + src_y * src_strideU + src_x / 4;
981 srcV = srcV + src_y * src_strideV + src_x / 4;
982
983 h = dest_y + h;
984 w = dest_x + w;
985
986 if (src_sdtv != dest_sdtv)
987 memcpy (matrix,
988 dest_sdtv ? cog_ycbcr_hdtv_to_ycbcr_sdtv_matrix_8bit :
989 cog_ycbcr_sdtv_to_ycbcr_hdtv_matrix_8bit, 12 * sizeof (gint));
990 else
991 memcpy (matrix, cog_identity_matrix_8bit, 12 * sizeof (gint));
992
993 /* 1. Copy all macro pixel scanlines, the destination scanline
994 * now starts at macro pixel boundary. */
995 for (i = dest_y; i < h; i++) {
996 /* 1.1. Handle the first destination pixel if it doesn't
997 * start at the macro pixel boundary, i.e. blend with
998 * the background! */
999 if (dest_x % 4 == 1) {
1000 y1 = srcY[0];
1001 y2 = srcY[1];
1002 y3 = srcY[2];
1003 u1 = srcU[0];
1004 v1 = srcV[0];
1005
1006 destY[0] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1007 destY[1] = CLAMP (APPLY_MATRIX (matrix, 0, y2, u1, v1), 0, 255);
1008 destY[2] = CLAMP (APPLY_MATRIX (matrix, 0, y3, u1, v1), 0, 255);
1009
1010 destU[0] = CLAMP (
1011 (destU[0] + APPLY_MATRIX (matrix, 1, y1, u1,
1012 v1) + APPLY_MATRIX (matrix, 1, y2, u1,
1013 v1) + APPLY_MATRIX (matrix, 1, y3, u1, v1)) / 4, 0, 255);
1014 destV[0] =
1015 CLAMP ((destV[0] + APPLY_MATRIX (matrix, 2, y1, u1,
1016 v1) + APPLY_MATRIX (matrix, 2, y2, u1,
1017 v1) + APPLY_MATRIX (matrix, 2, y3, u1, v1)) / 4, 0, 255);
1018
1019 j = dest_x + 3;
1020 src_y_idx = dest_y_idx = 3;
1021 dest_uv_idx = 1;
1022 src_uv_idx = (src_x % 4) + 3;
1023 } else if (dest_x % 4 == 2) {
1024 y1 = srcY[0];
1025 y2 = srcY[1];
1026 u1 = srcU[0];
1027 v1 = srcV[0];
1028
1029 destY[0] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1030 destY[1] = CLAMP (APPLY_MATRIX (matrix, 0, y2, u1, v1), 0, 255);
1031
1032 destU[0] = CLAMP (
1033 (2 * destU[0] + APPLY_MATRIX (matrix, 1, y1, u1,
1034 v1) + APPLY_MATRIX (matrix, 1, y2, u1, v1)) / 4, 0, 255);
1035 destV[0] =
1036 CLAMP ((2 * destV[0] + APPLY_MATRIX (matrix, 2, y1, u1,
1037 v1) + APPLY_MATRIX (matrix, 2, y2, u1, v1)) / 4, 0, 255);
1038
1039 j = dest_x + 2;
1040 src_y_idx = dest_y_idx = 2;
1041 dest_uv_idx = 1;
1042 src_uv_idx = (src_x % 4) + 2;
1043 } else if (dest_x % 4 == 3) {
1044 y1 = srcY[0];
1045 u1 = srcU[0];
1046 v1 = srcV[0];
1047
1048 destY[0] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1049
1050 destU[0] = CLAMP (
1051 (3 * destU[0] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 4, 0, 255);
1052 destV[0] = CLAMP (
1053 (3 * destV[0] + APPLY_MATRIX (matrix, 2, y1, u1, v1)) / 4, 0, 255);
1054
1055 j = dest_x + 1;
1056 src_y_idx = dest_y_idx = 1;
1057 dest_uv_idx = 1;
1058 src_uv_idx = (src_x % 4) + 1;
1059 } else {
1060 j = dest_x;
1061 src_y_idx = dest_y_idx = dest_uv_idx = 0;
1062 src_uv_idx = (src_x % 4);
1063 }
1064
1065 /* 1.2. Copy all macro pixels from the source to the destination.
1066 * All pixels now start at macro pixel boundary, i.e. no
1067 * blending with the background is necessary. */
1068 for (; j < w - 3; j += 4) {
1069 y1 = srcY[src_y_idx];
1070 y2 = srcY[src_y_idx + 1];
1071 y3 = srcY[src_y_idx + 2];
1072 y4 = srcY[src_y_idx + 3];
1073
1074 u1 = srcU[src_uv_idx / 4];
1075 v1 = srcV[src_uv_idx / 4];
1076 src_uv_idx++;
1077 u2 = srcU[src_uv_idx / 4];
1078 v2 = srcV[src_uv_idx / 4];
1079 src_uv_idx++;
1080 u3 = srcU[src_uv_idx / 4];
1081 v3 = srcV[src_uv_idx / 4];
1082 src_uv_idx++;
1083 u4 = srcU[src_uv_idx / 4];
1084 v4 = srcV[src_uv_idx / 4];
1085 src_uv_idx++;
1086
1087 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1088 destY[dest_y_idx + 1] =
1089 CLAMP (APPLY_MATRIX (matrix, 0, y2, u2, v2), 0, 255);
1090 destY[dest_y_idx + 2] =
1091 CLAMP (APPLY_MATRIX (matrix, 0, y3, u3, v3), 0, 255);
1092 destY[dest_y_idx + 3] =
1093 CLAMP (APPLY_MATRIX (matrix, 0, y4, u4, v4), 0, 255);
1094
1095 destU[dest_uv_idx] = CLAMP (
1096 (APPLY_MATRIX (matrix, 1, y1, u1, v1) + APPLY_MATRIX (matrix, 1, y2,
1097 u2, v2) + APPLY_MATRIX (matrix, 1, y3, u3,
1098 v3) + APPLY_MATRIX (matrix, 1, y4, u4, v4)) / 4, 0, 255);
1099 destV[dest_uv_idx] =
1100 CLAMP ((APPLY_MATRIX (matrix, 2, y1, u1, v1) + APPLY_MATRIX (matrix,
1101 2, y2, u2, v2) + APPLY_MATRIX (matrix, 2, y3, u3,
1102 v3) + APPLY_MATRIX (matrix, 2, y4, u4, v4)) / 4, 0, 255);
1103
1104 dest_y_idx += 4;
1105 src_y_idx += 4;
1106 dest_uv_idx++;
1107 }
1108
1109 /* 1.3. Now copy the last pixel if one exists and blend it
1110 * with the background because we only fill part of
1111 * the macro pixel. In case this is the last pixel of
1112 * the destination we will a larger part. */
1113 if (j == w - 1 && j == dest_width - 1) {
1114 y1 = srcY[src_y_idx];
1115 u1 = srcU[src_uv_idx / 4];
1116 v1 = srcV[src_uv_idx / 4];
1117
1118 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1119 destU[dest_uv_idx] = CLAMP (APPLY_MATRIX (matrix, 1, y1, u1, v1), 0, 255);
1120 destV[dest_uv_idx] = CLAMP (APPLY_MATRIX (matrix, 1, y1, u1, v1), 0, 255);
1121 } else if (j == w - 1) {
1122 y1 = srcY[src_y_idx];
1123 u1 = srcU[src_uv_idx / 4];
1124 v1 = srcV[src_uv_idx / 4];
1125
1126 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1127 destU[dest_uv_idx] = CLAMP (
1128 (destU[dest_uv_idx] + 3 * APPLY_MATRIX (matrix, 1, y1, u1,
1129 v1)) / 4, 0, 255);
1130 destV[dest_uv_idx] = CLAMP (
1131 (destV[dest_uv_idx] + 3 * APPLY_MATRIX (matrix, 1, y1, u1,
1132 v1)) / 4, 0, 255);
1133 } else if (j == w - 2 && j == dest_width - 2) {
1134 y1 = srcY[src_y_idx];
1135 y2 = srcY[src_y_idx + 1];
1136 u1 = srcU[src_uv_idx / 4];
1137 v1 = srcV[src_uv_idx / 4];
1138
1139 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1140 destY[dest_y_idx + 1] =
1141 CLAMP (APPLY_MATRIX (matrix, 0, y2, u1, v1), 0, 255);
1142 destU[dest_uv_idx] = CLAMP (APPLY_MATRIX (matrix, 1, y1, u1, v1), 0, 255);
1143 destV[dest_uv_idx] = CLAMP (APPLY_MATRIX (matrix, 1, y1, u1, v1), 0, 255);
1144 } else if (j == w - 2) {
1145 y1 = srcY[src_y_idx];
1146 y2 = srcY[src_y_idx + 1];
1147 u1 = srcU[src_uv_idx / 4];
1148 v1 = srcV[src_uv_idx / 4];
1149
1150 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1151 destY[dest_y_idx + 1] =
1152 CLAMP (APPLY_MATRIX (matrix, 0, y2, u1, v1), 0, 255);
1153 destU[dest_uv_idx] =
1154 CLAMP ((destU[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
1155 v1)) / 2, 0, 255);
1156 destV[dest_uv_idx] =
1157 CLAMP ((destV[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
1158 v1)) / 2, 0, 255);
1159 } else if (j == w - 3 && j == dest_width - 3) {
1160 y1 = srcY[src_y_idx];
1161 y2 = srcY[src_y_idx + 1];
1162 y3 = srcY[src_y_idx + 2];
1163 u1 = srcU[src_uv_idx / 4];
1164 v1 = srcV[src_uv_idx / 4];
1165
1166 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1167 destY[dest_y_idx + 1] =
1168 CLAMP (APPLY_MATRIX (matrix, 0, y2, u1, v1), 0, 255);
1169 destY[dest_y_idx + 2] =
1170 CLAMP (APPLY_MATRIX (matrix, 0, y3, u1, v1), 0, 255);
1171 destU[dest_uv_idx] = CLAMP (APPLY_MATRIX (matrix, 1, y1, u1, v1), 0, 255);
1172 destV[dest_uv_idx] = CLAMP (APPLY_MATRIX (matrix, 1, y1, u1, v1), 0, 255);
1173 } else if (j == w - 3) {
1174 y1 = srcY[src_y_idx];
1175 y2 = srcY[src_y_idx + 1];
1176 y3 = srcY[src_y_idx + 2];
1177 u1 = srcU[src_uv_idx / 4];
1178 v1 = srcV[src_uv_idx / 4];
1179
1180 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1181 destY[dest_y_idx + 1] =
1182 CLAMP (APPLY_MATRIX (matrix, 0, y2, u1, v1), 0, 255);
1183 destY[dest_y_idx + 2] =
1184 CLAMP (APPLY_MATRIX (matrix, 0, y3, u1, v1), 0, 255);
1185 destU[dest_uv_idx] =
1186 CLAMP ((3 * destU[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
1187 v1)) / 4, 0, 255);
1188 destV[dest_uv_idx] =
1189 CLAMP ((3 * destV[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
1190 v1)) / 4, 0, 255);
1191 }
1192
1193 destY += dest_strideY;
1194 destU += dest_strideU;
1195 destV += dest_strideV;
1196 srcY += src_strideY;
1197 srcU += src_strideU;
1198 srcV += src_strideV;
1199 }
1200 }
1201
1202 static void
1203 copy_i420_i420 (guint i_alpha, GstVideoFrame * dest,
1204 gboolean dest_sdtv, gint dest_x, gint dest_y, GstVideoFrame * src,
1205 gboolean src_sdtv, gint src_x, gint src_y, gint w, gint h)
1206 {
1207 gint i, j;
1208 guint8 *destY, *destU, *destV;
1209 const guint8 *srcY, *srcU, *srcV;
1210 guint8 *destY2;
1211 const guint8 *srcY2, *srcU2, *srcV2;
1212 gint dest_strideY, dest_strideU, dest_strideV;
1213 gint src_strideY, src_strideU, src_strideV;
1214 gint src_y_idx, src_uv_idx;
1215 gint dest_y_idx, dest_uv_idx;
1216 gint matrix[12];
1217 gint y1, y2, y3, y4;
1218 gint u1, u2, u3, u4;
1219 gint v1, v2, v3, v4;
1220 gint dest_width, dest_height;
1221
1222 dest_width = GST_VIDEO_FRAME_WIDTH (dest);
1223 dest_height = GST_VIDEO_FRAME_HEIGHT (dest);
1224
1225 dest_strideY = GST_VIDEO_FRAME_COMP_STRIDE (dest, 0);
1226 dest_strideU = GST_VIDEO_FRAME_COMP_STRIDE (dest, 1);
1227 dest_strideV = GST_VIDEO_FRAME_COMP_STRIDE (dest, 2);
1228
1229 src_strideY = GST_VIDEO_FRAME_COMP_STRIDE (src, 0);
1230 src_strideU = GST_VIDEO_FRAME_COMP_STRIDE (src, 1);
1231 src_strideV = GST_VIDEO_FRAME_COMP_STRIDE (src, 2);
1232
1233 destY = GST_VIDEO_FRAME_COMP_DATA (dest, 0);
1234 destU = GST_VIDEO_FRAME_COMP_DATA (dest, 1);
1235 destV = GST_VIDEO_FRAME_COMP_DATA (dest, 2);
1236
1237 srcY = GST_VIDEO_FRAME_COMP_DATA (src, 0);
1238 srcU = GST_VIDEO_FRAME_COMP_DATA (src, 1);
1239 srcV = GST_VIDEO_FRAME_COMP_DATA (src, 2);
1240
1241 destY = destY + dest_y * dest_strideY + dest_x;
1242 destU = destU + (dest_y / 2) * dest_strideU + dest_x / 2;
1243 destV = destV + (dest_y / 2) * dest_strideV + dest_x / 2;
1244
1245 srcY = srcY + src_y * src_strideY + src_x;
1246 srcU = srcU + (src_y / 2) * src_strideU + src_x / 2;
1247 srcV = srcV + (src_y / 2) * src_strideV + src_x / 2;
1248
1249 destY2 = destY + dest_strideY;
1250 srcY2 = srcY + src_strideY;
1251
1252 h = dest_y + h;
1253 w = dest_x + w;
1254
1255 if (src_sdtv != dest_sdtv)
1256 memcpy (matrix,
1257 dest_sdtv ? cog_ycbcr_hdtv_to_ycbcr_sdtv_matrix_8bit :
1258 cog_ycbcr_sdtv_to_ycbcr_hdtv_matrix_8bit, 12 * sizeof (gint));
1259 else
1260 memcpy (matrix, cog_identity_matrix_8bit, 12 * sizeof (gint));
1261
1262 /* 1. Handle the first destination scanline specially if it
1263 * doesn't start at the macro pixel boundary, i.e. blend
1264 * with the background! */
1265 if (dest_y % 2 == 1) {
1266 /* 1.1. Handle the first destination pixel if it doesn't
1267 * start at the macro pixel boundary, i.e. blend with
1268 * the background! */
1269 if (dest_x % 2 == 1) {
1270 y1 = srcY[0];
1271 u1 = srcU[0];
1272 v1 = srcV[0];
1273
1274 destY[0] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1275 destU[0] =
1276 CLAMP ((3 * destU[0] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 4, 0,
1277 255);
1278 destV[0] =
1279 CLAMP ((3 * destV[0] + APPLY_MATRIX (matrix, 2, y1, u1, v1)) / 4, 0,
1280 255);
1281
1282 j = dest_x + 1;
1283 src_y_idx = dest_y_idx = dest_uv_idx = 1;
1284 src_uv_idx = (src_x % 2) + 1;
1285 } else {
1286 j = dest_x;
1287 src_y_idx = dest_y_idx = dest_uv_idx = 0;
1288 src_uv_idx = (src_x % 2);
1289 }
1290
1291 /* 1.2. Copy all macro pixels from the source to the destination
1292 * but blend with the background because we're only filling
1293 * the lower part of the macro pixels. */
1294 for (; j < w - 1; j += 2) {
1295 y1 = srcY[src_y_idx];
1296 y2 = srcY[src_y_idx + 1];
1297
1298 u1 = srcU[src_uv_idx / 2];
1299 v1 = srcV[src_uv_idx / 2];
1300 src_uv_idx++;
1301 u2 = srcU[src_uv_idx / 2];
1302 v2 = srcV[src_uv_idx / 2];
1303 src_uv_idx++;
1304
1305 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1306 destY[dest_y_idx + 1] =
1307 CLAMP (APPLY_MATRIX (matrix, 0, y2, u2, v2), 0, 255);
1308 destU[dest_uv_idx] =
1309 CLAMP ((2 * destU[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
1310 v1) + APPLY_MATRIX (matrix, 1, y2, u2, v2)) / 4, 0, 255);
1311 destV[dest_uv_idx] =
1312 CLAMP ((2 * destV[dest_uv_idx] + APPLY_MATRIX (matrix, 2, y1, u1,
1313 v1) + APPLY_MATRIX (matrix, 2, y2, u2, v2)) / 4, 0, 255);
1314
1315 dest_y_idx += 2;
1316 src_y_idx += 2;
1317 dest_uv_idx++;
1318 }
1319
1320 /* 1.3. Now copy the last pixel if one exists and blend it
1321 * with the background because we only fill part of
1322 * the macro pixel. In case this is the last pixel of
1323 * the destination we will a larger part. */
1324 if (j == w - 1 && j == dest_width - 1) {
1325 y1 = srcY[src_y_idx];
1326 u1 = srcU[src_uv_idx / 2];
1327 v1 = srcV[src_uv_idx / 2];
1328
1329 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1330 destU[dest_uv_idx] = CLAMP (
1331 (destU[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 2, 0,
1332 255);
1333 destV[dest_uv_idx] =
1334 CLAMP ((destV[dest_uv_idx] + APPLY_MATRIX (matrix, 2, y1, u1,
1335 v1)) / 2, 0, 255);
1336 } else if (j == w - 1) {
1337 y1 = srcY[src_y_idx];
1338 u1 = srcU[src_uv_idx / 2];
1339 v1 = srcV[src_uv_idx / 2];
1340
1341 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1342 destU[dest_uv_idx] = CLAMP (
1343 (3 * destU[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 4,
1344 0, 255);
1345 destV[dest_uv_idx] =
1346 CLAMP ((3 * destV[dest_uv_idx] + APPLY_MATRIX (matrix, 2, y1, u1,
1347 v1)) / 4, 0, 255);
1348 }
1349
1350 destY += dest_strideY;
1351 destY2 += dest_strideY;
1352 destU += dest_strideU;
1353 destV += dest_strideV;
1354 srcY += src_strideY;
1355 srcY2 += src_strideY;
1356 src_y++;
1357 if (src_y % 2 == 0) {
1358 srcU += src_strideU;
1359 srcV += src_strideV;
1360 }
1361 i = dest_y + 1;
1362 } else {
1363 i = dest_y;
1364 }
1365
1366 /* 2. Copy all macro pixel scanlines, the destination scanline
1367 * now starts at macro pixel boundary. */
1368 for (; i < h - 1; i += 2) {
1369 /* 2.1. Handle the first destination pixel if it doesn't
1370 * start at the macro pixel boundary, i.e. blend with
1371 * the background! */
1372
1373 srcU2 = srcU;
1374 srcV2 = srcV;
1375 if (src_y % 2 == 1) {
1376 srcU2 += src_strideU;
1377 srcV2 += src_strideV;
1378 }
1379
1380 if (dest_x % 2 == 1) {
1381 y1 = srcY[0];
1382 y2 = srcY2[0];
1383 u1 = srcU[0];
1384 v1 = srcV[0];
1385 u2 = srcU2[0];
1386 v2 = srcV2[0];
1387
1388 destY[0] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1389 destY2[0] = CLAMP (APPLY_MATRIX (matrix, 0, y2, u2, v2), 0, 255);
1390 destU[0] = CLAMP (
1391 (2 * destU[0] + APPLY_MATRIX (matrix, 1, y1, u1,
1392 v1) + APPLY_MATRIX (matrix, 1, y2, u2, v2)) / 4, 0, 255);
1393 destV[0] = CLAMP (
1394 (2 * destV[0] + APPLY_MATRIX (matrix, 2, y1, u1,
1395 v1) + APPLY_MATRIX (matrix, 2, y2, u2, v2)) / 4, 0, 255);
1396 j = dest_x + 1;
1397 src_y_idx = dest_y_idx = dest_uv_idx = 1;
1398 src_uv_idx = (src_x % 2) + 1;
1399 } else {
1400 j = dest_x;
1401 src_y_idx = dest_y_idx = dest_uv_idx = 0;
1402 src_uv_idx = (src_x % 2);
1403 }
1404
1405 /* 2.2. Copy all macro pixels from the source to the destination.
1406 * All pixels now start at macro pixel boundary, i.e. no
1407 * blending with the background is necessary. */
1408 for (; j < w - 1; j += 2) {
1409 y1 = srcY[src_y_idx];
1410 y2 = srcY[src_y_idx + 1];
1411 y3 = srcY2[src_y_idx];
1412 y4 = srcY2[src_y_idx + 1];
1413
1414 u1 = srcU[src_uv_idx / 2];
1415 u3 = srcU2[src_uv_idx / 2];
1416 v1 = srcV[src_uv_idx / 2];
1417 v3 = srcV2[src_uv_idx / 2];
1418 src_uv_idx++;
1419 u2 = srcU[src_uv_idx / 2];
1420 u4 = srcU2[src_uv_idx / 2];
1421 v2 = srcV[src_uv_idx / 2];
1422 v4 = srcV2[src_uv_idx / 2];
1423 src_uv_idx++;
1424
1425 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1426 destY[dest_y_idx + 1] =
1427 CLAMP (APPLY_MATRIX (matrix, 0, y2, u2, v2), 0, 255);
1428 destY2[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y3, u3, v3), 0, 255);
1429 destY2[dest_y_idx + 1] =
1430 CLAMP (APPLY_MATRIX (matrix, 0, y4, u4, v4), 0, 255);
1431
1432 destU[dest_uv_idx] = CLAMP (
1433 (APPLY_MATRIX (matrix, 1, y1, u1, v1) + APPLY_MATRIX (matrix, 1, y2,
1434 u2, v2) + APPLY_MATRIX (matrix, 1, y3, u3,
1435 v3) + APPLY_MATRIX (matrix, 1, y4, u4, v4)) / 4, 0, 255);
1436 destV[dest_uv_idx] = CLAMP (
1437 (APPLY_MATRIX (matrix, 2, y1, u1, v1) + APPLY_MATRIX (matrix, 2, y2,
1438 u2, v2) + APPLY_MATRIX (matrix, 2, y3, u3,
1439 v3) + APPLY_MATRIX (matrix, 2, y4, u4, v4)) / 4, 0, 255);
1440
1441 dest_y_idx += 2;
1442 src_y_idx += 2;
1443 dest_uv_idx++;
1444 }
1445
1446 /* 2.3. Now copy the last pixel if one exists and blend it
1447 * with the background because we only fill part of
1448 * the macro pixel. In case this is the last pixel of
1449 * the destination we will a larger part. */
1450 if (j == w - 1 && j == dest_width - 1) {
1451 y1 = srcY[src_y_idx];
1452 y2 = srcY2[src_y_idx];
1453
1454 u1 = srcU[src_uv_idx / 2];
1455 u2 = srcU2[src_uv_idx / 2];
1456
1457 v1 = srcV[src_uv_idx / 2];
1458 v2 = srcV2[src_uv_idx / 2];
1459
1460 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1461 destY2[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y2, u2, v2), 0, 255);
1462 destU[dest_uv_idx] = CLAMP (
1463 (APPLY_MATRIX (matrix, 1, y1, u1, v1) + APPLY_MATRIX (matrix, 2, y2,
1464 u2, v2)) / 2, 0, 255);
1465 destV[dest_uv_idx] = CLAMP (
1466 (APPLY_MATRIX (matrix, 1, y1, u1, v1) + APPLY_MATRIX (matrix, 2, y2,
1467 u2, v2)) / 2, 0, 255);
1468 } else if (j == w - 1) {
1469 y1 = srcY[src_y_idx];
1470 y2 = srcY2[src_y_idx];
1471
1472 u1 = srcU[src_uv_idx / 2];
1473 u2 = srcU2[src_uv_idx / 2];
1474
1475 v1 = srcV[src_uv_idx / 2];
1476 v2 = srcV2[src_uv_idx / 2];
1477
1478 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1479 destY2[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y2, u2, v2), 0, 255);
1480 destU[dest_uv_idx] = CLAMP (
1481 (2 * destU[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
1482 v1) + APPLY_MATRIX (matrix, 2, y2, u2, v2)) / 4, 0, 255);
1483 destV[dest_uv_idx] = CLAMP (
1484 (2 * destV[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
1485 v1) + APPLY_MATRIX (matrix, 2, y2, u2, v2)) / 4, 0, 255);
1486 }
1487
1488 destY += 2 * dest_strideY;
1489 destY2 += 2 * dest_strideY;
1490 destU += dest_strideU;
1491 destV += dest_strideV;
1492 srcY += 2 * src_strideY;
1493 srcY2 += 2 * src_strideY;
1494
1495 src_y += 2;
1496 srcU += src_strideU;
1497 srcV += src_strideV;
1498 }
1499
1500 /* 3. Handle the last scanline if one exists. This again
1501 * doesn't start at macro pixel boundary but should
1502 * only fill the upper part of the macro pixels. */
1503 if (i == h - 1 && i == dest_height - 1) {
1504 /* 3.1. Handle the first destination pixel if it doesn't
1505 * start at the macro pixel boundary, i.e. blend with
1506 * the background! */
1507 if (dest_x % 2 == 1) {
1508 y1 = srcY[0];
1509 u1 = srcU[0];
1510 v1 = srcV[0];
1511
1512 destY[0] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1513 destU[0] =
1514 CLAMP ((destU[0] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 2, 0, 255);
1515 destV[0] =
1516 CLAMP ((destV[0] + APPLY_MATRIX (matrix, 2, y1, u1, v1)) / 2, 0, 255);
1517
1518 j = dest_x + 1;
1519 src_y_idx = dest_y_idx = dest_uv_idx = 1;
1520 src_uv_idx = (src_x % 2) + 1;
1521 } else {
1522 j = dest_x;
1523 src_y_idx = dest_y_idx = dest_uv_idx = 0;
1524 src_uv_idx = (src_x % 2);
1525 }
1526
1527 /* 3.2. Copy all macro pixels from the source to the destination
1528 * but blend with the background because we're only filling
1529 * the upper part of the macro pixels. */
1530 for (; j < w - 1; j += 2) {
1531 y1 = srcY[src_y_idx];
1532 y2 = srcY[src_y_idx + 1];
1533
1534 u1 = srcU[src_uv_idx / 2];
1535 v1 = srcV[src_uv_idx / 2];
1536 src_uv_idx++;
1537 u2 = srcU[src_uv_idx / 2];
1538 v2 = srcV[src_uv_idx / 2];
1539 src_uv_idx++;
1540
1541 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1542 destY[dest_y_idx + 1] =
1543 CLAMP (APPLY_MATRIX (matrix, 0, y2, u2, v2), 0, 255);
1544
1545 destU[dest_uv_idx] = CLAMP (
1546 (2 * destU[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
1547 v1) + APPLY_MATRIX (matrix, 1, y2, u2, v2)) / 4, 0, 255);
1548 destV[dest_uv_idx] = CLAMP (
1549 (2 * destV[dest_uv_idx] + APPLY_MATRIX (matrix, 2, y1, u1,
1550 v1) + APPLY_MATRIX (matrix, 2, y2, u2, v2)) / 4, 0, 255);
1551
1552 dest_y_idx += 2;
1553 src_y_idx += 2;
1554 dest_uv_idx++;
1555 }
1556
1557 /* 3.3. Now copy the last pixel if one exists and blend it
1558 * with the background because we only fill part of
1559 * the macro pixel. In case this is the last pixel of
1560 * the destination we will a larger part. */
1561 if (j == w - 1 && j == dest_width - 1) {
1562 y1 = srcY[src_y_idx];
1563 u1 = srcU[src_uv_idx / 2];
1564 v1 = srcV[src_uv_idx / 2];
1565
1566 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1567 destU[dest_uv_idx] = CLAMP (
1568 (destU[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 2, 0,
1569 255);
1570 destV[dest_uv_idx] =
1571 CLAMP ((destV[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
1572 v1)) / 2, 0, 255);
1573 } else if (j == w - 1) {
1574 y1 = srcY[src_y_idx];
1575 u1 = srcU[src_uv_idx / 2];
1576 v1 = srcV[src_uv_idx / 2];
1577
1578 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1579 destU[dest_uv_idx] = CLAMP (
1580 (3 * destU[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 4,
1581 0, 255);
1582 destV[dest_uv_idx] =
1583 CLAMP ((3 * destV[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
1584 v1)) / 4, 0, 255);
1585 }
1586 } else if (i == h - 1) {
1587 /* 3.1. Handle the first destination pixel if it doesn't
1588 * start at the macro pixel boundary, i.e. blend with
1589 * the background! */
1590 if (dest_x % 2 == 1) {
1591 y1 = srcY[0];
1592 u1 = srcU[0];
1593 v1 = srcV[0];
1594
1595 destY[0] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1596 destU[0] =
1597 CLAMP ((3 * destU[0] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 4, 0,
1598 255);
1599 destV[0] =
1600 CLAMP ((3 * destV[0] + APPLY_MATRIX (matrix, 2, y1, u1, v1)) / 4, 0,
1601 255);
1602
1603 j = dest_x + 1;
1604 src_y_idx = dest_y_idx = dest_uv_idx = 1;
1605 src_uv_idx = (src_x % 2) + 1;
1606 } else {
1607 j = dest_x;
1608 src_y_idx = dest_y_idx = dest_uv_idx = 0;
1609 src_uv_idx = (src_x % 2);
1610 }
1611
1612 /* 3.2. Copy all macro pixels from the source to the destination
1613 * but blend with the background because we're only filling
1614 * the upper part of the macro pixels. */
1615 for (; j < w - 1; j += 2) {
1616 y1 = srcY[src_y_idx];
1617 y2 = srcY[src_y_idx + 1];
1618
1619 u1 = srcU[src_uv_idx / 2];
1620 v1 = srcV[src_uv_idx / 2];
1621 src_uv_idx++;
1622 u2 = srcU[src_uv_idx / 2];
1623 v2 = srcV[src_uv_idx / 2];
1624 src_uv_idx++;
1625
1626 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1627 destY[dest_y_idx + 1] =
1628 CLAMP (APPLY_MATRIX (matrix, 0, y2, u2, v2), 0, 255);
1629
1630 destU[dest_uv_idx] = CLAMP (
1631 (2 * destU[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
1632 v1) + APPLY_MATRIX (matrix, 1, y2, u2, v2)) / 4, 0, 255);
1633 destV[dest_uv_idx] = CLAMP (
1634 (2 * destV[dest_uv_idx] + APPLY_MATRIX (matrix, 2, y1, u1,
1635 v1) + APPLY_MATRIX (matrix, 2, y2, u2, v2)) / 4, 0, 255);
1636
1637 dest_y_idx += 2;
1638 src_y_idx += 2;
1639 dest_uv_idx++;
1640 }
1641
1642 /* 3.3. Now copy the last pixel if one exists and blend it
1643 * with the background because we only fill part of
1644 * the macro pixel. In case this is the last pixel of
1645 * the destination we will a larger part. */
1646 if (j == w - 1 && j == dest_width - 1) {
1647 y1 = srcY[src_y_idx];
1648 u1 = srcU[src_uv_idx / 2];
1649 v1 = srcV[src_uv_idx / 2];
1650
1651 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1652 destU[dest_uv_idx] = CLAMP (
1653 (destU[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 2, 0,
1654 255);
1655 destV[dest_uv_idx] =
1656 CLAMP ((destV[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
1657 v1)) / 2, 0, 255);
1658 } else if (j == w - 1) {
1659 y1 = srcY[src_y_idx];
1660 u1 = srcU[src_uv_idx / 2];
1661 v1 = srcV[src_uv_idx / 2];
1662
1663 destY[dest_y_idx] = CLAMP (APPLY_MATRIX (matrix, 0, y1, u1, v1), 0, 255);
1664 destU[dest_uv_idx] = CLAMP (
1665 (3 * destU[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1, v1)) / 4,
1666 0, 255);
1667 destV[dest_uv_idx] =
1668 CLAMP ((3 * destV[dest_uv_idx] + APPLY_MATRIX (matrix, 1, y1, u1,
1669 v1)) / 4, 0, 255);
1670 }
1671 }
1672 }
1673
1674 static void
1675 copy_i420_ayuv (guint i_alpha, GstVideoFrame * dest_frame,
1676 gboolean dest_sdtv, gint dest_x, gint dest_y, GstVideoFrame * src_frame,
1677 gboolean src_sdtv, gint src_x, gint src_y, gint w, gint h)
1678 {
1679 const guint8 *srcY, *srcU, *srcV;
1680 gint src_strideY, src_strideU, src_strideV;
1681 gint dest_stride;
1682 guint8 *dest;
1683
1684 src_strideY = GST_VIDEO_FRAME_COMP_STRIDE (src_frame, 0);
1685 src_strideU = GST_VIDEO_FRAME_COMP_STRIDE (src_frame, 1);
1686 src_strideV = GST_VIDEO_FRAME_COMP_STRIDE (src_frame, 2);
1687
1688 srcY = GST_VIDEO_FRAME_COMP_DATA (src_frame, 0);
1689 srcU = GST_VIDEO_FRAME_COMP_DATA (src_frame, 1);
1690 srcV = GST_VIDEO_FRAME_COMP_DATA (src_frame, 2);
1691
1692 dest_stride = GST_VIDEO_FRAME_PLANE_STRIDE (dest_frame, 0);
1693
1694 dest = GST_VIDEO_FRAME_PLANE_DATA (dest_frame, 0);
1695 dest = dest + dest_y * dest_stride + dest_x * 4;
1696
1697 srcY = srcY + src_y * src_strideY + src_x;
1698 srcU = srcU + (src_y / 2) * src_strideU + src_x / 2;
1699 srcV = srcV + (src_y / 2) * src_strideV + src_x / 2;
1700
1701 i_alpha = MIN (i_alpha, 255);
1702
1703 if (src_sdtv != dest_sdtv) {
1704 gint i, j, uv_idx;
1705 gint y, u, v;
1706 gint y1, u1, v1;
1707 gint matrix[12];
1708
1709 memcpy (matrix,
1710 dest_sdtv ? cog_ycbcr_hdtv_to_ycbcr_sdtv_matrix_8bit :
1711 cog_ycbcr_sdtv_to_ycbcr_hdtv_matrix_8bit, 12 * sizeof (gint));
1712
1713 for (i = 0; i < h; i++) {
1714 for (j = 0, uv_idx = src_x % 2; j < w; j++, uv_idx++) {
1715 y = srcY[j];
1716 u = srcU[uv_idx / 2];
1717 v = srcV[uv_idx / 2];
1718
1719 y1 = APPLY_MATRIX (matrix, 0, y, u, v);
1720 u1 = APPLY_MATRIX (matrix, 1, y, u, v);
1721 v1 = APPLY_MATRIX (matrix, 2, y, u, v);
1722
1723 dest[4 * j + 0] = i_alpha;
1724 dest[4 * j + 1] = y1;
1725 dest[4 * j + 2] = u1;
1726 dest[4 * j + 3] = v1;
1727 }
1728 dest += dest_stride;
1729
1730 src_y++;
1731 srcY += src_strideY;
1732 if (src_y % 2 == 0) {
1733 srcU += src_strideU;
1734 srcV += src_strideV;
1735 }
1736 }
1737 } else {
1738 gint i, j, uv_idx;
1739 gint y, u, v;
1740
1741 for (i = 0; i < h; i++) {
1742 for (j = 0, uv_idx = src_x % 2; j < w; j++, uv_idx++) {
1743 y = srcY[j];
1744 u = srcU[uv_idx / 2];
1745 v = srcV[uv_idx / 2];
1746
1747 dest[4 * j + 0] = i_alpha;
1748 dest[4 * j + 1] = y;
1749 dest[4 * j + 2] = u;
1750 dest[4 * j + 3] = v;
1751 }
1752 dest += dest_stride;
1753
1754 src_y++;
1755 srcY += src_strideY;
1756 if (src_y % 2 == 0) {
1757 srcU += src_strideU;
1758 srcV += src_strideV;
1759 }
1760 }
1761 }
1762 }
1763
1764 static void
1765 fill_rgb32 (GstVideoBoxFill fill_type, guint b_alpha,
1766 GstVideoFrame * frame, gboolean sdtv)
1767 {
1768 guint32 empty_pixel;
1769 gint p[4];
1770 guint8 *dest;
1771 guint stride;
1772 gint width, height;
1773
1774 width = GST_VIDEO_FRAME_WIDTH (frame);
1775 height = GST_VIDEO_FRAME_HEIGHT (frame);
1776
1777 dest = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
1778 stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
1779
1780 p[0] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 3);
1781 p[1] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 0);
1782 p[2] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 1);
1783 p[3] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 2);
1784
1785 b_alpha = MIN (b_alpha, 255);
1786
1787 if (GST_VIDEO_FRAME_N_COMPONENTS (frame) == 4) {
1788 empty_pixel = GUINT32_FROM_LE ((b_alpha << (p[0] * 8)) |
1789 (rgb_colors_R[fill_type] << (p[1] * 8)) |
1790 (rgb_colors_G[fill_type] << (p[2] * 8)) |
1791 (rgb_colors_B[fill_type] << (p[3] * 8)));
1792 } else {
1793 empty_pixel = GUINT32_FROM_LE (
1794 (rgb_colors_R[fill_type] << (p[1] * 8)) |
1795 (rgb_colors_G[fill_type] << (p[2] * 8)) |
1796 (rgb_colors_B[fill_type] << (p[3] * 8)));
1797 }
1798
1799 if (stride == width * 4) {
1800 video_box_orc_splat_u32 ((guint32 *) dest, empty_pixel, width * height);
1801 } else if (height) {
1802 for (; height; --height) {
1803 video_box_orc_splat_u32 ((guint32 *) dest, empty_pixel, width);
1804 dest += stride;
1805 }
1806 }
1807 }
1808
1809 static void
1810 fill_rgb24 (GstVideoBoxFill fill_type, guint b_alpha,
1811 GstVideoFrame * frame, gboolean sdtv)
1812 {
1813 gint dest_stride;
1814 gint p[4];
1815 gint i, j;
1816 guint8 *dest;
1817 gint width, height;
1818
1819 width = GST_VIDEO_FRAME_WIDTH (frame);
1820 height = GST_VIDEO_FRAME_HEIGHT (frame);
1821
1822 dest = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
1823 dest_stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
1824
1825 p[1] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 0);
1826 p[2] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 1);
1827 p[3] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 2);
1828
1829 for (i = 0; i < height; i++) {
1830 for (j = 0; j < width; j++) {
1831 dest[3 * j + p[1]] = rgb_colors_R[fill_type];
1832 dest[3 * j + p[2]] = rgb_colors_G[fill_type];
1833 dest[3 * j + p[3]] = rgb_colors_B[fill_type];
1834 }
1835 dest += dest_stride;
1836 }
1837 }
1838
1839 static void
1840 copy_rgb32 (guint i_alpha, GstVideoFrame * dest_frame,
1841 gboolean dest_sdtv, gint dest_x, gint dest_y, GstVideoFrame * src_frame,
1842 gboolean src_sdtv, gint src_x, gint src_y, gint w, gint h)
1843 {
1844 gint i, j;
1845 gint src_stride, dest_stride;
1846 gboolean in_alpha, out_alpha;
1847 gint in_bpp, out_bpp;
1848 gint p_out[4];
1849 gint p_in[4];
1850 gboolean packed_out;
1851 gboolean packed_in;
1852 guint8 *src, *dest;
1853
1854 src_stride = GST_VIDEO_FRAME_PLANE_STRIDE (src_frame, 0);
1855 dest_stride = GST_VIDEO_FRAME_PLANE_STRIDE (dest_frame, 0);
1856 in_bpp = GST_VIDEO_FRAME_COMP_PSTRIDE (src_frame, 0);
1857 out_bpp = GST_VIDEO_FRAME_COMP_PSTRIDE (dest_frame, 0);
1858 packed_in = (in_bpp < 4);
1859 packed_out = (out_bpp < 4);
1860
1861 out_alpha = GST_VIDEO_INFO_HAS_ALPHA (&dest_frame->info);
1862 p_out[0] = GST_VIDEO_FRAME_COMP_OFFSET (dest_frame, 3);
1863 p_out[1] = GST_VIDEO_FRAME_COMP_OFFSET (dest_frame, 0);
1864 p_out[2] = GST_VIDEO_FRAME_COMP_OFFSET (dest_frame, 1);
1865 p_out[3] = GST_VIDEO_FRAME_COMP_OFFSET (dest_frame, 2);
1866
1867 in_alpha = GST_VIDEO_INFO_HAS_ALPHA (&src_frame->info);
1868 p_in[0] = GST_VIDEO_FRAME_COMP_OFFSET (src_frame, 3);
1869 p_in[1] = GST_VIDEO_FRAME_COMP_OFFSET (src_frame, 0);
1870 p_in[2] = GST_VIDEO_FRAME_COMP_OFFSET (src_frame, 1);
1871 p_in[3] = GST_VIDEO_FRAME_COMP_OFFSET (src_frame, 2);
1872
1873 dest = GST_VIDEO_FRAME_PLANE_DATA (dest_frame, 0);
1874 dest = dest + dest_y * dest_stride + dest_x * out_bpp;
1875 src = GST_VIDEO_FRAME_PLANE_DATA (src_frame, 0);
1876 src = src + src_y * src_stride + src_x * in_bpp;
1877
1878 if (in_alpha && out_alpha) {
1879 w *= 4;
1880 for (i = 0; i < h; i++) {
1881 for (j = 0; j < w; j += 4) {
1882 dest[j + p_out[0]] = (src[j + p_in[0]] * i_alpha) >> 8;
1883 dest[j + p_out[1]] = src[j + p_in[1]];
1884 dest[j + p_out[2]] = src[j + p_in[2]];
1885 dest[j + p_out[3]] = src[j + p_in[3]];
1886 }
1887 dest += dest_stride;
1888 src += src_stride;
1889 }
1890 } else if (out_alpha && !packed_in) {
1891 w *= 4;
1892 i_alpha = MIN (i_alpha, 255);
1893
1894 for (i = 0; i < h; i++) {
1895 for (j = 0; j < w; j += 4) {
1896 dest[j + p_out[0]] = i_alpha;
1897 dest[j + p_out[1]] = src[j + p_in[1]];
1898 dest[j + p_out[2]] = src[j + p_in[2]];
1899 dest[j + p_out[3]] = src[j + p_in[3]];
1900 }
1901 dest += dest_stride;
1902 src += src_stride;
1903 }
1904 } else if (out_alpha && packed_in) {
1905 i_alpha = MIN (i_alpha, 255);
1906
1907 for (i = 0; i < h; i++) {
1908 for (j = 0; j < w; j++) {
1909 dest[4 * j + p_out[0]] = i_alpha;
1910 dest[4 * j + p_out[1]] = src[in_bpp * j + p_in[1]];
1911 dest[4 * j + p_out[2]] = src[in_bpp * j + p_in[2]];
1912 dest[4 * j + p_out[3]] = src[in_bpp * j + p_in[3]];
1913 }
1914 dest += dest_stride;
1915 src += src_stride;
1916 }
1917 } else if (!packed_out && !packed_in) {
1918 w *= 4;
1919 for (i = 0; i < h; i++) {
1920 for (j = 0; j < w; j += 4) {
1921 dest[j + p_out[1]] = src[j + p_in[1]];
1922 dest[j + p_out[2]] = src[j + p_in[2]];
1923 dest[j + p_out[3]] = src[j + p_in[3]];
1924 }
1925 dest += dest_stride;
1926 src += src_stride;
1927 }
1928 } else {
1929 for (i = 0; i < h; i++) {
1930 for (j = 0; j < w; j++) {
1931 dest[out_bpp * j + p_out[1]] = src[in_bpp * j + p_in[1]];
1932 dest[out_bpp * j + p_out[2]] = src[in_bpp * j + p_in[2]];
1933 dest[out_bpp * j + p_out[3]] = src[in_bpp * j + p_in[3]];
1934 }
1935 dest += dest_stride;
1936 src += src_stride;
1937 }
1938 }
1939 }
1940
1941 static void
1942 copy_rgb32_ayuv (guint i_alpha, GstVideoFrame * dest_frame,
1943 gboolean dest_sdtv, gint dest_x, gint dest_y, GstVideoFrame * src_frame,
1944 gboolean src_sdtv, gint src_x, gint src_y, gint w, gint h)
1945 {
1946 gint i, j;
1947 gint src_stride, dest_stride;
1948 gboolean in_alpha;
1949 gint in_bpp;
1950 gint p_in[4];
1951 gboolean packed_in;
1952 gint matrix[12];
1953 gint a;
1954 gint y, u, v;
1955 gint r, g, b;
1956 guint8 *dest, *src;
1957
1958 dest_stride = GST_VIDEO_FRAME_PLANE_STRIDE (dest_frame, 0);
1959 src_stride = GST_VIDEO_FRAME_PLANE_STRIDE (src_frame, 0);
1960 in_bpp = GST_VIDEO_FRAME_COMP_PSTRIDE (src_frame, 0);
1961 packed_in = (in_bpp < 4);
1962
1963 in_alpha = GST_VIDEO_INFO_HAS_ALPHA (&src_frame->info);
1964 p_in[0] = GST_VIDEO_FRAME_COMP_OFFSET (src_frame, 3);
1965 p_in[1] = GST_VIDEO_FRAME_COMP_OFFSET (src_frame, 0);
1966 p_in[2] = GST_VIDEO_FRAME_COMP_OFFSET (src_frame, 1);
1967 p_in[3] = GST_VIDEO_FRAME_COMP_OFFSET (src_frame, 2);
1968
1969 memcpy (matrix,
1970 (dest_sdtv) ? cog_rgb_to_ycbcr_matrix_8bit_sdtv :
1971 cog_rgb_to_ycbcr_matrix_8bit_hdtv, 12 * sizeof (gint));
1972
1973 dest = GST_VIDEO_FRAME_PLANE_DATA (dest_frame, 0);
1974 dest = dest + dest_y * dest_stride + dest_x * 4;
1975 src = GST_VIDEO_FRAME_PLANE_DATA (src_frame, 0);
1976 src = src + src_y * src_stride + src_x * in_bpp;
1977
1978 if (in_alpha) {
1979 w *= 4;
1980 for (i = 0; i < h; i++) {
1981 for (j = 0; j < w; j += 4) {
1982 a = (src[j + p_in[0]] * i_alpha) >> 8;
1983 r = src[j + p_in[1]];
1984 g = src[j + p_in[2]];
1985 b = src[j + p_in[3]];
1986
1987 y = APPLY_MATRIX (matrix, 0, r, g, b);
1988 u = APPLY_MATRIX (matrix, 1, r, g, b);
1989 v = APPLY_MATRIX (matrix, 2, r, g, b);
1990
1991 dest[j + 0] = a;
1992 dest[j + 1] = CLAMP (y, 0, 255);
1993 dest[j + 2] = CLAMP (u, 0, 255);
1994 dest[j + 3] = CLAMP (v, 0, 255);
1995 }
1996 dest += dest_stride;
1997 src += src_stride;
1998 }
1999 } else if (!packed_in) {
2000 w *= 4;
2001 i_alpha = MIN (i_alpha, 255);
2002
2003 for (i = 0; i < h; i++) {
2004 for (j = 0; j < w; j += 4) {
2005 a = i_alpha;
2006 r = src[j + p_in[1]];
2007 g = src[j + p_in[2]];
2008 b = src[j + p_in[3]];
2009
2010 y = APPLY_MATRIX (matrix, 0, r, g, b);
2011 u = APPLY_MATRIX (matrix, 1, r, g, b);
2012 v = APPLY_MATRIX (matrix, 2, r, g, b);
2013
2014 dest[j + 0] = a;
2015 dest[j + 1] = CLAMP (y, 0, 255);
2016 dest[j + 2] = CLAMP (u, 0, 255);
2017 dest[j + 3] = CLAMP (v, 0, 255);
2018 }
2019 dest += dest_stride;
2020 src += src_stride;
2021 }
2022 } else {
2023 i_alpha = MIN (i_alpha, 255);
2024
2025 for (i = 0; i < h; i++) {
2026 for (j = 0; j < w; j++) {
2027 a = i_alpha;
2028 r = src[in_bpp * j + p_in[1]];
2029 g = src[in_bpp * j + p_in[2]];
2030 b = src[in_bpp * j + p_in[3]];
2031
2032 y = APPLY_MATRIX (matrix, 0, r, g, b);
2033 u = APPLY_MATRIX (matrix, 1, r, g, b);
2034 v = APPLY_MATRIX (matrix, 2, r, g, b);
2035
2036 dest[4 * j + 0] = a;
2037 dest[4 * j + 1] = CLAMP (y, 0, 255);
2038 dest[4 * j + 2] = CLAMP (u, 0, 255);
2039 dest[4 * j + 3] = CLAMP (v, 0, 255);
2040 }
2041 dest += dest_stride;
2042 src += src_stride;
2043 }
2044 }
2045 }
2046
2047 static void
2048 copy_ayuv_rgb32 (guint i_alpha, GstVideoFrame * dest_frame,
2049 gboolean dest_sdtv, gint dest_x, gint dest_y, GstVideoFrame * src_frame,
2050 gboolean src_sdtv, gint src_x, gint src_y, gint w, gint h)
2051 {
2052 gint i, j;
2053 gint src_stride, dest_stride;
2054 gboolean out_alpha;
2055 gint out_bpp;
2056 gint p_out[4];
2057 gboolean packed_out;
2058 gint matrix[12];
2059 gint a;
2060 gint y, u, v;
2061 gint r, g, b;
2062 guint8 *src, *dest;
2063
2064 src_stride = GST_VIDEO_FRAME_PLANE_STRIDE (src_frame, 0);
2065 dest_stride = GST_VIDEO_FRAME_PLANE_STRIDE (dest_frame, 0);
2066 out_bpp = GST_VIDEO_FRAME_COMP_PSTRIDE (dest_frame, 0);
2067 packed_out = (out_bpp < 4);
2068
2069 out_alpha = GST_VIDEO_INFO_HAS_ALPHA (&dest_frame->info);
2070 p_out[0] = GST_VIDEO_FRAME_COMP_OFFSET (dest_frame, 3);
2071 p_out[1] = GST_VIDEO_FRAME_COMP_OFFSET (dest_frame, 0);
2072 p_out[2] = GST_VIDEO_FRAME_COMP_OFFSET (dest_frame, 1);
2073 p_out[3] = GST_VIDEO_FRAME_COMP_OFFSET (dest_frame, 2);
2074
2075 memcpy (matrix,
2076 (src_sdtv) ? cog_ycbcr_to_rgb_matrix_8bit_sdtv :
2077 cog_ycbcr_to_rgb_matrix_8bit_hdtv, 12 * sizeof (gint));
2078
2079 dest = GST_VIDEO_FRAME_PLANE_DATA (dest_frame, 0);
2080 dest = dest + dest_y * dest_stride + dest_x * out_bpp;
2081 src = GST_VIDEO_FRAME_PLANE_DATA (src_frame, 0);
2082 src = src + src_y * src_stride + src_x * 4;
2083
2084 if (out_alpha) {
2085 w *= 4;
2086 for (i = 0; i < h; i++) {
2087 for (j = 0; j < w; j += 4) {
2088 a = (src[j + 0] * i_alpha) >> 8;
2089 y = src[j + 1];
2090 u = src[j + 2];
2091 v = src[j + 3];
2092
2093 r = APPLY_MATRIX (matrix, 0, y, u, v);
2094 g = APPLY_MATRIX (matrix, 1, y, u, v);
2095 b = APPLY_MATRIX (matrix, 2, y, u, v);
2096
2097 dest[j + p_out[0]] = a;
2098 dest[j + p_out[1]] = CLAMP (r, 0, 255);
2099 dest[j + p_out[2]] = CLAMP (g, 0, 255);
2100 dest[j + p_out[3]] = CLAMP (b, 0, 255);
2101 }
2102 dest += dest_stride;
2103 src += src_stride;
2104 }
2105 } else if (!packed_out) {
2106 w *= 4;
2107 for (i = 0; i < h; i++) {
2108 for (j = 0; j < w; j += 4) {
2109 y = src[j + 1];
2110 u = src[j + 2];
2111 v = src[j + 3];
2112
2113 r = APPLY_MATRIX (matrix, 0, y, u, v);
2114 g = APPLY_MATRIX (matrix, 1, y, u, v);
2115 b = APPLY_MATRIX (matrix, 2, y, u, v);
2116
2117 dest[j + p_out[1]] = CLAMP (r, 0, 255);
2118 dest[j + p_out[2]] = CLAMP (g, 0, 255);
2119 dest[j + p_out[3]] = CLAMP (b, 0, 255);
2120 }
2121 dest += dest_stride;
2122 src += src_stride;
2123 }
2124 } else {
2125 for (i = 0; i < h; i++) {
2126 for (j = 0; j < w; j++) {
2127 y = src[4 * j + 1];
2128 u = src[4 * j + 2];
2129 v = src[4 * j + 3];
2130
2131 r = APPLY_MATRIX (matrix, 0, y, u, v);
2132 g = APPLY_MATRIX (matrix, 1, y, u, v);
2133 b = APPLY_MATRIX (matrix, 2, y, u, v);
2134
2135 dest[out_bpp * j + p_out[1]] = CLAMP (r, 0, 255);
2136 dest[out_bpp * j + p_out[2]] = CLAMP (g, 0, 255);
2137 dest[out_bpp * j + p_out[3]] = CLAMP (b, 0, 255);
2138 }
2139 dest += dest_stride;
2140 src += src_stride;
2141 }
2142 }
2143 }
2144
2145 static void
2146 fill_gray (GstVideoBoxFill fill_type, guint b_alpha,
2147 GstVideoFrame * frame, gboolean sdtv)
2148 {
2149 gint i, j;
2150 gint dest_stride;
2151 guint8 *dest;
2152 gint width, height;
2153 GstVideoFormat format;
2154
2155 format = GST_VIDEO_FRAME_FORMAT (frame);
2156
2157 width = GST_VIDEO_FRAME_WIDTH (frame);
2158 height = GST_VIDEO_FRAME_HEIGHT (frame);
2159
2160 dest = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
2161 dest_stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
2162
2163 if (format == GST_VIDEO_FORMAT_GRAY8) {
2164 guint8 val = yuv_sdtv_colors_Y[fill_type];
2165
2166 for (i = 0; i < height; i++) {
2167 memset (dest, val, width);
2168 dest += dest_stride;
2169 }
2170 } else {
2171 guint16 val = yuv_sdtv_colors_Y[fill_type] << 8;
2172
2173 if (format == GST_VIDEO_FORMAT_GRAY16_BE) {
2174 for (i = 0; i < height; i++) {
2175 for (j = 0; j < width; j++) {
2176 GST_WRITE_UINT16_BE (dest + 2 * j, val);
2177 }
2178 dest += dest_stride;
2179 }
2180 } else {
2181 for (i = 0; i < height; i++) {
2182 for (j = 0; j < width; j++) {
2183 GST_WRITE_UINT16_LE (dest + 2 * j, val);
2184 }
2185 dest += dest_stride;
2186 }
2187 }
2188 }
2189 }
2190
2191 static void
2192 copy_packed_simple (guint i_alpha, GstVideoFrame * dest_frame,
2193 gboolean dest_sdtv, gint dest_x, gint dest_y, GstVideoFrame * src_frame,
2194 gboolean src_sdtv, gint src_x, gint src_y, gint w, gint h)
2195 {
2196 gint i;
2197 gint src_stride, dest_stride;
2198 gint pixel_stride, row_size;
2199 guint8 *src, *dest;
2200
2201 src_stride = GST_VIDEO_FRAME_PLANE_STRIDE (src_frame, 0);
2202 dest_stride = GST_VIDEO_FRAME_PLANE_STRIDE (dest_frame, 0);
2203 pixel_stride = GST_VIDEO_FRAME_COMP_PSTRIDE (dest_frame, 0);
2204 row_size = w * pixel_stride;
2205
2206 dest = GST_VIDEO_FRAME_PLANE_DATA (dest_frame, 0);
2207 dest = dest + dest_y * dest_stride + dest_x * pixel_stride;
2208 src = GST_VIDEO_FRAME_PLANE_DATA (src_frame, 0);
2209 src = src + src_y * src_stride + src_x * pixel_stride;
2210
2211 for (i = 0; i < h; i++) {
2212 memcpy (dest, src, row_size);
2213 dest += dest_stride;
2214 src += src_stride;
2215 }
2216 }
2217
2218 static void
2219 fill_yuy2 (GstVideoBoxFill fill_type, guint b_alpha,
2220 GstVideoFrame * frame, gboolean sdtv)
2221 {
2222 guint8 y, u, v;
2223 gint i, j;
2224 gint stride;
2225 gint width, height;
2226 guint8 *dest;
2227 GstVideoFormat format;
2228
2229 format = GST_VIDEO_FRAME_FORMAT (frame);
2230
2231 width = GST_VIDEO_FRAME_WIDTH (frame);
2232 height = GST_VIDEO_FRAME_HEIGHT (frame);
2233
2234 dest = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
2235 stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
2236
2237 y = (sdtv) ? yuv_sdtv_colors_Y[fill_type] : yuv_hdtv_colors_Y[fill_type];
2238 u = (sdtv) ? yuv_sdtv_colors_U[fill_type] : yuv_hdtv_colors_U[fill_type];
2239 v = (sdtv) ? yuv_sdtv_colors_V[fill_type] : yuv_hdtv_colors_V[fill_type];
2240
2241 width = width + (width % 2);
2242
2243 if (format == GST_VIDEO_FORMAT_YUY2) {
2244 for (i = 0; i < height; i++) {
2245 for (j = 0; j < width; j += 2) {
2246 dest[j * 2 + 0] = y;
2247 dest[j * 2 + 1] = u;
2248 dest[j * 2 + 2] = y;
2249 dest[j * 2 + 3] = v;
2250 }
2251
2252 dest += stride;
2253 }
2254 } else if (format == GST_VIDEO_FORMAT_YVYU) {
2255 for (i = 0; i < height; i++) {
2256 for (j = 0; j < width; j += 2) {
2257 dest[j * 2 + 0] = y;
2258 dest[j * 2 + 1] = v;
2259 dest[j * 2 + 2] = y;
2260 dest[j * 2 + 3] = u;
2261 }
2262
2263 dest += stride;
2264 }
2265 } else {
2266 for (i = 0; i < height; i++) {
2267 for (j = 0; j < width; j += 2) {
2268 dest[j * 2 + 0] = u;
2269 dest[j * 2 + 1] = y;
2270 dest[j * 2 + 2] = v;
2271 dest[j * 2 + 3] = y;
2272 }
2273
2274 dest += stride;
2275 }
2276 }
2277 }
2278
2279 static void
2280 copy_yuy2_yuy2 (guint i_alpha, GstVideoFrame * dest_frame,
2281 gboolean dest_sdtv, gint dest_x, gint dest_y, GstVideoFrame * src_frame,
2282 gboolean src_sdtv, gint src_x, gint src_y, gint w, gint h)
2283 {
2284 gint i, j;
2285 gint src_stride, dest_stride;
2286 guint8 *src, *dest;
2287 GstVideoFormat src_format;
2288
2289 src_format = GST_VIDEO_FRAME_FORMAT (src_frame);
2290
2291 src_stride = GST_VIDEO_FRAME_PLANE_STRIDE (src_frame, 0);
2292 dest_stride = GST_VIDEO_FRAME_PLANE_STRIDE (dest_frame, 0);
2293
2294 dest_x = (dest_x & ~1);
2295 src_x = (src_x & ~1);
2296
2297 w = w + (w % 2);
2298
2299 dest = GST_VIDEO_FRAME_PLANE_DATA (dest_frame, 0);
2300 dest = dest + dest_y * dest_stride + dest_x * 2;
2301 src = GST_VIDEO_FRAME_PLANE_DATA (src_frame, 0);
2302 src = src + src_y * src_stride + src_x * 2;
2303
2304 if (src_sdtv != dest_sdtv) {
2305 gint y1, u1, v1;
2306 gint y2, u2, v2;
2307 gint matrix[12];
2308
2309 memcpy (matrix,
2310 dest_sdtv ? cog_ycbcr_hdtv_to_ycbcr_sdtv_matrix_8bit :
2311 cog_ycbcr_sdtv_to_ycbcr_hdtv_matrix_8bit, 12 * sizeof (gint));
2312
2313 if (src_format == GST_VIDEO_FORMAT_YUY2) {
2314 for (i = 0; i < h; i++) {
2315 for (j = 0; j < w; j += 2) {
2316 y1 = src[j * 2 + 0];
2317 y2 = src[j * 2 + 2];
2318 u1 = u2 = src[j * 2 + 1];
2319 v1 = v2 = src[j * 2 + 3];
2320
2321 dest[j * 2 + 0] = APPLY_MATRIX (matrix, 0, y1, u1, v1);
2322 dest[j * 2 + 1] = APPLY_MATRIX (matrix, 1, y1, u1, v1);
2323 dest[j * 2 + 2] = APPLY_MATRIX (matrix, 0, y1, u2, v2);
2324 dest[j * 2 + 3] = APPLY_MATRIX (matrix, 2, y2, u2, v2);
2325 }
2326 dest += dest_stride;
2327 src += src_stride;
2328 }
2329 } else if (src_format == GST_VIDEO_FORMAT_YVYU) {
2330 for (i = 0; i < h; i++) {
2331 for (j = 0; j < w; j += 2) {
2332 y1 = src[j * 2 + 0];
2333 y2 = src[j * 2 + 2];
2334 v1 = v2 = src[j * 2 + 1];
2335 u1 = u2 = src[j * 2 + 3];
2336
2337 dest[j * 2 + 0] = APPLY_MATRIX (matrix, 0, y1, u1, v1);
2338 dest[j * 2 + 1] = APPLY_MATRIX (matrix, 2, y1, u1, v1);
2339 dest[j * 2 + 2] = APPLY_MATRIX (matrix, 0, y1, u2, v2);
2340 dest[j * 2 + 3] = APPLY_MATRIX (matrix, 1, y2, u2, v2);
2341 }
2342 dest += dest_stride;
2343 src += src_stride;
2344 }
2345 } else {
2346 for (i = 0; i < h; i++) {
2347 for (j = 0; j < w; j += 2) {
2348 u1 = u2 = src[j * 2 + 0];
2349 v1 = v2 = src[j * 2 + 2];
2350 y1 = src[j * 2 + 1];
2351 y2 = src[j * 2 + 3];
2352
2353 dest[j * 2 + 1] = APPLY_MATRIX (matrix, 0, y1, u1, v1);
2354 dest[j * 2 + 0] = APPLY_MATRIX (matrix, 1, y1, u1, v1);
2355 dest[j * 2 + 3] = APPLY_MATRIX (matrix, 0, y1, u2, v2);
2356 dest[j * 2 + 2] = APPLY_MATRIX (matrix, 2, y2, u2, v2);
2357 }
2358 dest += dest_stride;
2359 src += src_stride;
2360 }
2361 }
2362 } else {
2363 for (i = 0; i < h; i++) {
2364 memcpy (dest, src, w * 2);
2365 dest += dest_stride;
2366 src += src_stride;
2367 }
2368 }
2369 }
2370
2371 #define DEFAULT_LEFT 0
2372 #define DEFAULT_RIGHT 0
2373 #define DEFAULT_TOP 0
2374 #define DEFAULT_BOTTOM 0
2375 #define DEFAULT_FILL_TYPE VIDEO_BOX_FILL_BLACK
2376 #define DEFAULT_ALPHA 1.0
2377 #define DEFAULT_BORDER_ALPHA 1.0
2378
2379 enum
2380 {
2381 PROP_0,
2382 PROP_LEFT,
2383 PROP_RIGHT,
2384 PROP_TOP,
2385 PROP_BOTTOM,
2386 PROP_FILL_TYPE,
2387 PROP_ALPHA,
2388 PROP_BORDER_ALPHA,
2389 PROP_AUTOCROP
2390 /* FILL ME */
2391 };
2392
2393 static GstStaticPadTemplate gst_video_box_src_template =
2394 GST_STATIC_PAD_TEMPLATE ("src",
2395 GST_PAD_SRC,
2396 GST_PAD_ALWAYS,
2397 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ AYUV, "
2398 "ARGB, BGRA, ABGR, RGBA, xRGB, BGRx, xBGR, RGBx, RGB, BGR, "
2399 "Y444, Y42B, YUY2, YVYU, UYVY, I420, YV12, Y41B, "
2400 "GRAY8, GRAY16_BE, GRAY16_LE } "))
2401 );
2402
2403 static GstStaticPadTemplate gst_video_box_sink_template =
2404 GST_STATIC_PAD_TEMPLATE ("sink",
2405 GST_PAD_SINK,
2406 GST_PAD_ALWAYS,
2407 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ AYUV, "
2408 "ARGB, BGRA, ABGR, RGBA, xRGB, BGRx, xBGR, RGBx, RGB, BGR, "
2409 "Y444, Y42B, YUY2, YVYU, UYVY, I420, YV12, Y41B, "
2410 "GRAY8, GRAY16_BE, GRAY16_LE } "))
2411 );
2412
2413 #define gst_video_box_parent_class parent_class
2414 G_DEFINE_TYPE (GstVideoBox, gst_video_box, GST_TYPE_VIDEO_FILTER);
2415 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (videobox, "videobox", GST_RANK_NONE,
2416 GST_TYPE_VIDEO_BOX, GST_DEBUG_CATEGORY_INIT (videobox_debug, "videobox", 0,
2417 "Resizes a video by adding borders or cropping"));
2418
2419 static void gst_video_box_set_property (GObject * object, guint prop_id,
2420 const GValue * value, GParamSpec * pspec);
2421 static void gst_video_box_get_property (GObject * object, guint prop_id,
2422 GValue * value, GParamSpec * pspec);
2423
2424 static gboolean gst_video_box_recalc_transform (GstVideoBox * video_box);
2425 static GstCaps *gst_video_box_transform_caps (GstBaseTransform * trans,
2426 GstPadDirection direction, GstCaps * from, GstCaps * filter);
2427 static void gst_video_box_before_transform (GstBaseTransform * trans,
2428 GstBuffer * in);
2429 static gboolean gst_video_box_src_event (GstBaseTransform * trans,
2430 GstEvent * event);
2431
2432 static gboolean gst_video_box_set_info (GstVideoFilter * vfilter, GstCaps * in,
2433 GstVideoInfo * in_info, GstCaps * out, GstVideoInfo * out_info);
2434 static GstFlowReturn gst_video_box_transform_frame (GstVideoFilter * vfilter,
2435 GstVideoFrame * in_frame, GstVideoFrame * out_frame);
2436 static gboolean gst_video_box_transform_meta (GstBaseTransform * trans,
2437 GstBuffer * outbuf, GstMeta * meta, GstBuffer * inbuf);
2438
2439
2440 #define GST_TYPE_VIDEO_BOX_FILL (gst_video_box_fill_get_type())
2441 static GType
2442 gst_video_box_fill_get_type (void)
2443 {
2444 static GType video_box_fill_type = 0;
2445 static const GEnumValue video_box_fill[] = {
2446 {VIDEO_BOX_FILL_BLACK, "Black", "black"},
2447 {VIDEO_BOX_FILL_GREEN, "Green", "green"},
2448 {VIDEO_BOX_FILL_BLUE, "Blue", "blue"},
2449 {VIDEO_BOX_FILL_RED, "Red", "red"},
2450 {VIDEO_BOX_FILL_YELLOW, "Yellow", "yellow"},
2451 {VIDEO_BOX_FILL_WHITE, "White", "white"},
2452 {0, NULL, NULL},
2453 };
2454
2455 if (!video_box_fill_type) {
2456 video_box_fill_type =
2457 g_enum_register_static ("GstVideoBoxFill", video_box_fill);
2458 }
2459 return video_box_fill_type;
2460 }
2461
2462 static void
2463 gst_video_box_finalize (GObject * object)
2464 {
2465 GstVideoBox *video_box = GST_VIDEO_BOX (object);
2466
2467 g_mutex_clear (&video_box->mutex);
2468
2469 G_OBJECT_CLASS (parent_class)->finalize (object);
2470 }
2471
2472 static void
2473 gst_video_box_class_init (GstVideoBoxClass * klass)
2474 {
2475 GObjectClass *gobject_class = (GObjectClass *) klass;
2476 GstElementClass *element_class = (GstElementClass *) (klass);
2477 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
2478 GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
2479
2480 gobject_class->set_property = gst_video_box_set_property;
2481 gobject_class->get_property = gst_video_box_get_property;
2482 gobject_class->finalize = gst_video_box_finalize;
2483
2484 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FILL_TYPE,
2485 g_param_spec_enum ("fill", "Fill", "How to fill the borders",
2486 GST_TYPE_VIDEO_BOX_FILL, DEFAULT_FILL_TYPE,
2487 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
2488 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LEFT,
2489 g_param_spec_int ("left", "Left",
2490 "Pixels to box at left (<0 = add a border)", G_MININT, G_MAXINT,
2491 DEFAULT_LEFT,
2492 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
2493 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_RIGHT,
2494 g_param_spec_int ("right", "Right",
2495 "Pixels to box at right (<0 = add a border)", G_MININT, G_MAXINT,
2496 DEFAULT_RIGHT,
2497 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
2498 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TOP,
2499 g_param_spec_int ("top", "Top",
2500 "Pixels to box at top (<0 = add a border)", G_MININT, G_MAXINT,
2501 DEFAULT_TOP,
2502 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
2503 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BOTTOM,
2504 g_param_spec_int ("bottom", "Bottom",
2505 "Pixels to box at bottom (<0 = add a border)", G_MININT, G_MAXINT,
2506 DEFAULT_BOTTOM,
2507 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
2508 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ALPHA,
2509 g_param_spec_double ("alpha", "Alpha", "Alpha value picture", 0.0, 1.0,
2510 DEFAULT_ALPHA,
2511 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
2512 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BORDER_ALPHA,
2513 g_param_spec_double ("border-alpha", "Border Alpha",
2514 "Alpha value of the border", 0.0, 1.0, DEFAULT_BORDER_ALPHA,
2515 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
2516 /**
2517 * GstVideoBox:autocrop:
2518 *
2519 * If set to %TRUE videobox will automatically crop/pad the input
2520 * video to be centered in the output.
2521 */
2522 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_AUTOCROP,
2523 g_param_spec_boolean ("autocrop", "Auto crop",
2524 "Auto crop", FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2525
2526
2527 _size_quark = g_quark_from_static_string (GST_META_TAG_VIDEO_SIZE_STR);
2528 _colorspace_quark =
2529 g_quark_from_static_string (GST_META_TAG_VIDEO_COLORSPACE_STR);
2530
2531 trans_class->before_transform =
2532 GST_DEBUG_FUNCPTR (gst_video_box_before_transform);
2533 trans_class->transform_caps =
2534 GST_DEBUG_FUNCPTR (gst_video_box_transform_caps);
2535 trans_class->src_event = GST_DEBUG_FUNCPTR (gst_video_box_src_event);
2536 trans_class->transform_meta =
2537 GST_DEBUG_FUNCPTR (gst_video_box_transform_meta);
2538
2539 vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_video_box_set_info);
2540 vfilter_class->transform_frame =
2541 GST_DEBUG_FUNCPTR (gst_video_box_transform_frame);
2542
2543 gst_element_class_set_static_metadata (element_class, "Video box filter",
2544 "Filter/Effect/Video",
2545 "Resizes a video by adding borders or cropping",
2546 "Wim Taymans <[email protected]>");
2547
2548 gst_element_class_add_static_pad_template (element_class,
2549 &gst_video_box_sink_template);
2550 gst_element_class_add_static_pad_template (element_class,
2551 &gst_video_box_src_template);
2552
2553 gst_type_mark_as_plugin_api (GST_TYPE_VIDEO_BOX_FILL, 0);
2554 }
2555
2556 static void
2557 gst_video_box_init (GstVideoBox * video_box)
2558 {
2559 video_box->box_right = DEFAULT_RIGHT;
2560 video_box->box_left = DEFAULT_LEFT;
2561 video_box->box_top = DEFAULT_TOP;
2562 video_box->box_bottom = DEFAULT_BOTTOM;
2563 video_box->fill_type = DEFAULT_FILL_TYPE;
2564 video_box->alpha = DEFAULT_ALPHA;
2565 video_box->border_alpha = DEFAULT_BORDER_ALPHA;
2566 video_box->autocrop = FALSE;
2567
2568 g_mutex_init (&video_box->mutex);
2569 }
2570
2571 static void
2572 gst_video_box_set_property (GObject * object, guint prop_id,
2573 const GValue * value, GParamSpec * pspec)
2574 {
2575 GstVideoBox *video_box = GST_VIDEO_BOX (object);
2576
2577 g_mutex_lock (&video_box->mutex);
2578 switch (prop_id) {
2579 case PROP_LEFT:
2580 video_box->box_left = g_value_get_int (value);
2581 break;
2582 case PROP_RIGHT:
2583 video_box->box_right = g_value_get_int (value);
2584 break;
2585 case PROP_TOP:
2586 video_box->box_top = g_value_get_int (value);
2587 break;
2588 case PROP_BOTTOM:
2589 video_box->box_bottom = g_value_get_int (value);
2590 break;
2591 case PROP_FILL_TYPE:
2592 video_box->fill_type = g_value_get_enum (value);
2593 break;
2594 case PROP_ALPHA:
2595 video_box->alpha = g_value_get_double (value);
2596 break;
2597 case PROP_BORDER_ALPHA:
2598 video_box->border_alpha = g_value_get_double (value);
2599 break;
2600 case PROP_AUTOCROP:
2601 video_box->autocrop = g_value_get_boolean (value);
2602 break;
2603 default:
2604 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2605 break;
2606 }
2607 gst_video_box_recalc_transform (video_box);
2608
2609 GST_DEBUG_OBJECT (video_box, "Calling reconfigure");
2610 gst_base_transform_reconfigure_src (GST_BASE_TRANSFORM_CAST (video_box));
2611
2612 g_mutex_unlock (&video_box->mutex);
2613 }
2614
2615 static void
2616 gst_video_box_autocrop (GstVideoBox * video_box)
2617 {
2618 gint crop_w = video_box->in_width - video_box->out_width;
2619 gint crop_h = video_box->in_height - video_box->out_height;
2620
2621 video_box->box_left = crop_w / 2;
2622
2623 /* Round down/up for odd width differences */
2624 if (crop_w < 0)
2625 crop_w -= 1;
2626 else
2627 crop_w += 1;
2628
2629 video_box->box_right = crop_w / 2;
2630 video_box->box_top = crop_h / 2;
2631
2632 /* Round down/up for odd height differences */
2633 if (crop_h < 0)
2634 crop_h -= 1;
2635 else
2636 crop_h += 1;
2637 video_box->box_bottom = crop_h / 2;
2638 }
2639
2640 static void
2641 gst_video_box_get_property (GObject * object, guint prop_id, GValue * value,
2642 GParamSpec * pspec)
2643 {
2644 GstVideoBox *video_box = GST_VIDEO_BOX (object);
2645
2646 switch (prop_id) {
2647 case PROP_LEFT:
2648 g_value_set_int (value, video_box->box_left);
2649 break;
2650 case PROP_RIGHT:
2651 g_value_set_int (value, video_box->box_right);
2652 break;
2653 case PROP_TOP:
2654 g_value_set_int (value, video_box->box_top);
2655 break;
2656 case PROP_BOTTOM:
2657 g_value_set_int (value, video_box->box_bottom);
2658 break;
2659 case PROP_FILL_TYPE:
2660 g_value_set_enum (value, video_box->fill_type);
2661 break;
2662 case PROP_ALPHA:
2663 g_value_set_double (value, video_box->alpha);
2664 break;
2665 case PROP_BORDER_ALPHA:
2666 g_value_set_double (value, video_box->border_alpha);
2667 break;
2668 case PROP_AUTOCROP:
2669 g_value_set_boolean (value, video_box->autocrop);
2670 break;
2671 default:
2672 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2673 break;
2674 }
2675 }
2676
2677 static inline gint
2678 gst_video_box_transform_dimension (gint val, gint delta)
2679 {
2680 gint64 new_val = (gint64) val + (gint64) delta;
2681
2682 new_val = CLAMP (new_val, 1, G_MAXINT);
2683
2684 return (gint) new_val;
2685 }
2686
2687 static gboolean
2688 gst_video_box_transform_dimension_value (const GValue * src_val,
2689 gint delta, GValue * dest_val)
2690 {
2691 gboolean ret = TRUE;
2692
2693 g_value_init (dest_val, G_VALUE_TYPE (src_val));
2694
2695 if (G_VALUE_HOLDS_INT (src_val)) {
2696 gint ival = g_value_get_int (src_val);
2697
2698 ival = gst_video_box_transform_dimension (ival, delta);
2699 g_value_set_int (dest_val, ival);
2700 } else if (GST_VALUE_HOLDS_INT_RANGE (src_val)) {
2701 gint min = gst_value_get_int_range_min (src_val);
2702 gint max = gst_value_get_int_range_max (src_val);
2703
2704 min = gst_video_box_transform_dimension (min, delta);
2705 max = gst_video_box_transform_dimension (max, delta);
2706 if (min >= max) {
2707 ret = FALSE;
2708 g_value_unset (dest_val);
2709 } else {
2710 gst_value_set_int_range (dest_val, min, max);
2711 }
2712 } else if (GST_VALUE_HOLDS_LIST (src_val)) {
2713 gint i;
2714
2715 for (i = 0; i < gst_value_list_get_size (src_val); ++i) {
2716 const GValue *list_val;
2717 GValue newval = { 0, };
2718
2719 list_val = gst_value_list_get_value (src_val, i);
2720 if (gst_video_box_transform_dimension_value (list_val, delta, &newval))
2721 gst_value_list_append_value (dest_val, &newval);
2722 g_value_unset (&newval);
2723 }
2724
2725 if (gst_value_list_get_size (dest_val) == 0) {
2726 g_value_unset (dest_val);
2727 ret = FALSE;
2728 }
2729 } else {
2730 g_value_unset (dest_val);
2731 ret = FALSE;
2732 }
2733
2734 return ret;
2735 }
2736
2737 static GstCaps *
2738 gst_video_box_transform_caps (GstBaseTransform * trans,
2739 GstPadDirection direction, GstCaps * from, GstCaps * filter)
2740 {
2741 GstVideoBox *video_box = GST_VIDEO_BOX (trans);
2742 GstCaps *to, *ret;
2743 GstCaps *templ;
2744 GstStructure *structure;
2745 GstPad *other;
2746 gint i, j;
2747
2748 to = gst_caps_new_empty ();
2749 for (i = 0; i < gst_caps_get_size (from); i++) {
2750 const GValue *fval, *lval;
2751 GValue list = { 0, };
2752 GValue val = { 0, };
2753 gboolean seen_yuv = FALSE, seen_rgb = FALSE;
2754 const gchar *str;
2755
2756 structure = gst_structure_copy (gst_caps_get_structure (from, i));
2757
2758 /* Transform width/height */
2759 if (video_box->autocrop) {
2760 gst_structure_remove_field (structure, "width");
2761 gst_structure_remove_field (structure, "height");
2762 } else {
2763 gint dw = 0, dh = 0;
2764 const GValue *v;
2765 GValue w_val = { 0, };
2766 GValue h_val = { 0, };
2767
2768 /* calculate width and height */
2769 if (direction == GST_PAD_SINK) {
2770 dw -= video_box->box_left;
2771 dw -= video_box->box_right;
2772 } else {
2773 dw += video_box->box_left;
2774 dw += video_box->box_right;
2775 }
2776
2777 if (direction == GST_PAD_SINK) {
2778 dh -= video_box->box_top;
2779 dh -= video_box->box_bottom;
2780 } else {
2781 dh += video_box->box_top;
2782 dh += video_box->box_bottom;
2783 }
2784
2785 v = gst_structure_get_value (structure, "width");
2786 if (!gst_video_box_transform_dimension_value (v, dw, &w_val)) {
2787 GST_WARNING_OBJECT (video_box,
2788 "could not transform width value with dw=%d" ", caps structure=%"
2789 GST_PTR_FORMAT, dw, structure);
2790 goto bail;
2791 }
2792 gst_structure_set_value (structure, "width", &w_val);
2793
2794 v = gst_structure_get_value (structure, "height");
2795 if (!gst_video_box_transform_dimension_value (v, dh, &h_val)) {
2796 g_value_unset (&w_val);
2797 GST_WARNING_OBJECT (video_box,
2798 "could not transform height value with dh=%d" ", caps structure=%"
2799 GST_PTR_FORMAT, dh, structure);
2800 goto bail;
2801 }
2802 gst_structure_set_value (structure, "height", &h_val);
2803 g_value_unset (&w_val);
2804 g_value_unset (&h_val);
2805 }
2806
2807 /* Supported conversions:
2808 * I420->AYUV
2809 * I420->YV12
2810 * YV12->AYUV
2811 * YV12->I420
2812 * AYUV->I420
2813 * AYUV->YV12
2814 * AYUV->xRGB (24bpp, 32bpp, incl. alpha)
2815 * xRGB->xRGB (24bpp, 32bpp, from/to all variants, incl. alpha)
2816 * xRGB->AYUV (24bpp, 32bpp, incl. alpha)
2817 *
2818 * Passthrough only for everything else.
2819 */
2820 fval = gst_structure_get_value (structure, "format");
2821 if (fval && GST_VALUE_HOLDS_LIST (fval)) {
2822 for (j = 0; j < gst_value_list_get_size (fval); j++) {
2823 lval = gst_value_list_get_value (fval, j);
2824 if ((str = g_value_get_string (lval))) {
2825 if (strcmp (str, "AYUV") == 0) {
2826 seen_yuv = TRUE;
2827 seen_rgb = TRUE;
2828 break;
2829 } else if (strstr (str, "RGB") || strstr (str, "BGR")) {
2830 seen_rgb = TRUE;
2831 } else if (strcmp (str, "I420") == 0 || strcmp (str, "YV12") == 0) {
2832 seen_yuv = TRUE;
2833 }
2834 }
2835 }
2836 } else if (fval && G_VALUE_HOLDS_STRING (fval)) {
2837 if ((str = g_value_get_string (fval))) {
2838 if (strcmp (str, "AYUV") == 0) {
2839 seen_yuv = TRUE;
2840 seen_rgb = TRUE;
2841 } else if (strstr (str, "RGB") || strstr (str, "BGR")) {
2842 seen_rgb = TRUE;
2843 } else if (strcmp (str, "I420") == 0 || strcmp (str, "YV12") == 0) {
2844 seen_yuv = TRUE;
2845 }
2846 }
2847 }
2848
2849 if (seen_yuv || seen_rgb) {
2850 g_value_init (&list, GST_TYPE_LIST);
2851
2852 g_value_init (&val, G_TYPE_STRING);
2853 g_value_set_string (&val, "AYUV");
2854 gst_value_list_append_value (&list, &val);
2855 g_value_unset (&val);
2856
2857 if (seen_yuv) {
2858 g_value_init (&val, G_TYPE_STRING);
2859 g_value_set_string (&val, "I420");
2860 gst_value_list_append_value (&list, &val);
2861 g_value_reset (&val);
2862 g_value_set_string (&val, "YV12");
2863 gst_value_list_append_value (&list, &val);
2864 g_value_unset (&val);
2865 }
2866 if (seen_rgb) {
2867 g_value_init (&val, G_TYPE_STRING);
2868 g_value_set_string (&val, "RGBx");
2869 gst_value_list_append_value (&list, &val);
2870 g_value_reset (&val);
2871 g_value_set_string (&val, "BGRx");
2872 gst_value_list_append_value (&list, &val);
2873 g_value_reset (&val);
2874 g_value_set_string (&val, "xRGB");
2875 gst_value_list_append_value (&list, &val);
2876 g_value_reset (&val);
2877 g_value_set_string (&val, "xBGR");
2878 gst_value_list_append_value (&list, &val);
2879 g_value_reset (&val);
2880 g_value_set_string (&val, "RGBA");
2881 gst_value_list_append_value (&list, &val);
2882 g_value_reset (&val);
2883 g_value_set_string (&val, "BGRA");
2884 gst_value_list_append_value (&list, &val);
2885 g_value_reset (&val);
2886 g_value_set_string (&val, "ARGB");
2887 gst_value_list_append_value (&list, &val);
2888 g_value_reset (&val);
2889 g_value_set_string (&val, "ABGR");
2890 gst_value_list_append_value (&list, &val);
2891 g_value_reset (&val);
2892 g_value_set_string (&val, "RGB");
2893 gst_value_list_append_value (&list, &val);
2894 g_value_reset (&val);
2895 g_value_set_string (&val, "BGR");
2896 gst_value_list_append_value (&list, &val);
2897 g_value_unset (&val);
2898 }
2899 gst_value_list_merge (&val, fval, &list);
2900 gst_structure_set_value (structure, "format", &val);
2901 g_value_unset (&val);
2902 g_value_unset (&list);
2903
2904 /* format list above makes for non-fixed caps;
2905 * so basetransform and peers will be enlisted to decide these parts,
2906 * otherwise leave as-is for passthrough case */
2907 gst_structure_remove_field (structure, "colorimetry");
2908 gst_structure_remove_field (structure, "chroma-site");
2909 }
2910 gst_caps_append_structure (to, structure);
2911 }
2912
2913 /* filter against set allowed caps on the pad */
2914 other = (direction == GST_PAD_SINK) ? trans->srcpad : trans->sinkpad;
2915 templ = gst_pad_get_pad_template_caps (other);
2916 ret = gst_caps_intersect (to, templ);
2917 gst_caps_unref (to);
2918 gst_caps_unref (templ);
2919
2920 GST_DEBUG_OBJECT (video_box, "direction %d, transformed %" GST_PTR_FORMAT
2921 " to %" GST_PTR_FORMAT, direction, from, ret);
2922
2923 if (ret && filter) {
2924 GstCaps *intersection;
2925
2926 GST_DEBUG_OBJECT (video_box, "Using filter caps %" GST_PTR_FORMAT, filter);
2927 intersection =
2928 gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
2929 gst_caps_unref (ret);
2930 ret = intersection;
2931 GST_DEBUG_OBJECT (video_box, "Intersection %" GST_PTR_FORMAT, ret);
2932 }
2933
2934 return ret;
2935
2936 /* ERRORS */
2937 bail:
2938 {
2939 gst_structure_free (structure);
2940 gst_caps_unref (to);
2941 to = gst_caps_new_empty ();
2942 return to;
2943 }
2944 }
2945
2946 static gboolean
2947 gst_video_box_recalc_transform (GstVideoBox * video_box)
2948 {
2949 gint br, bl, bt, bb;
2950 gboolean res = TRUE;
2951
2952 /* if we have the same format in and out and we don't need to perform any
2953 * cropping at all, we can just operate in passthrough mode */
2954 if (video_box->in_format == video_box->out_format &&
2955 video_box->box_left == 0 && video_box->box_right == 0 &&
2956 video_box->box_top == 0 && video_box->box_bottom == 0 &&
2957 video_box->in_sdtv == video_box->out_sdtv) {
2958
2959 GST_LOG_OBJECT (video_box, "we are using passthrough");
2960 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM_CAST (video_box),
2961 TRUE);
2962 } else {
2963 GST_LOG_OBJECT (video_box, "we are not using passthrough");
2964 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM_CAST (video_box),
2965 FALSE);
2966 }
2967
2968
2969 video_box->crop_h = 0;
2970 video_box->crop_w = 0;
2971
2972 br = video_box->box_right;
2973 bl = video_box->box_left;
2974 bt = video_box->box_top;
2975 bb = video_box->box_bottom;
2976
2977 if (br >= 0 && bl >= 0) {
2978 video_box->crop_w = video_box->in_width - (br + bl);
2979 } else if (br >= 0 && bl < 0) {
2980 video_box->crop_w = video_box->in_width - (br);
2981 } else if (br < 0 && bl >= 0) {
2982 video_box->crop_w = video_box->in_width - (bl);
2983 } else if (br < 0 && bl < 0) {
2984 video_box->crop_w = video_box->in_width;
2985 }
2986
2987 if (bb >= 0 && bt >= 0) {
2988 video_box->crop_h = video_box->in_height - (bb + bt);
2989 } else if (bb >= 0 && bt < 0) {
2990 video_box->crop_h = video_box->in_height - (bb);
2991 } else if (bb < 0 && bt >= 0) {
2992 video_box->crop_h = video_box->in_height - (bt);
2993 } else if (bb < 0 && bt < 0) {
2994 video_box->crop_h = video_box->in_height;
2995 }
2996
2997 GST_DEBUG_OBJECT (video_box, "Borders are: L:%d, R:%d, T:%d, B:%d", bl, br,
2998 bt, bb);
2999
3000 video_box->dest_x = video_box->src_x = 0;
3001 video_box->dest_y = video_box->src_y = 0;
3002
3003 /* Top border */
3004 if (bt < 0) {
3005 video_box->dest_y = -bt;
3006 } else {
3007 video_box->src_y = bt;
3008 }
3009
3010 /* Left border */
3011 if (bl < 0) {
3012 video_box->dest_x = -bl;
3013 } else {
3014 video_box->src_x = bl;
3015 }
3016
3017 return res;
3018 }
3019
3020 static gboolean
3021 gst_video_box_select_processing_functions (GstVideoBox * video_box)
3022 {
3023 switch (video_box->out_format) {
3024 case GST_VIDEO_FORMAT_AYUV:
3025 video_box->fill = fill_ayuv;
3026 switch (video_box->in_format) {
3027 case GST_VIDEO_FORMAT_AYUV:
3028 video_box->copy = copy_ayuv_ayuv;
3029 break;
3030 case GST_VIDEO_FORMAT_I420:
3031 case GST_VIDEO_FORMAT_YV12:
3032 video_box->copy = copy_i420_ayuv;
3033 break;
3034 case GST_VIDEO_FORMAT_ARGB:
3035 case GST_VIDEO_FORMAT_ABGR:
3036 case GST_VIDEO_FORMAT_RGBA:
3037 case GST_VIDEO_FORMAT_BGRA:
3038 case GST_VIDEO_FORMAT_xRGB:
3039 case GST_VIDEO_FORMAT_xBGR:
3040 case GST_VIDEO_FORMAT_RGBx:
3041 case GST_VIDEO_FORMAT_BGRx:
3042 case GST_VIDEO_FORMAT_RGB:
3043 case GST_VIDEO_FORMAT_BGR:
3044 video_box->copy = copy_rgb32_ayuv;
3045 break;
3046 default:
3047 break;
3048 }
3049 break;
3050 case GST_VIDEO_FORMAT_I420:
3051 case GST_VIDEO_FORMAT_YV12:
3052 video_box->fill = fill_planar_yuv;
3053 switch (video_box->in_format) {
3054 case GST_VIDEO_FORMAT_AYUV:
3055 video_box->copy = copy_ayuv_i420;
3056 break;
3057 case GST_VIDEO_FORMAT_I420:
3058 case GST_VIDEO_FORMAT_YV12:
3059 video_box->copy = copy_i420_i420;
3060 break;
3061 default:
3062 break;
3063 }
3064 break;
3065 case GST_VIDEO_FORMAT_ARGB:
3066 case GST_VIDEO_FORMAT_ABGR:
3067 case GST_VIDEO_FORMAT_RGBA:
3068 case GST_VIDEO_FORMAT_BGRA:
3069 case GST_VIDEO_FORMAT_xRGB:
3070 case GST_VIDEO_FORMAT_xBGR:
3071 case GST_VIDEO_FORMAT_RGBx:
3072 case GST_VIDEO_FORMAT_BGRx:
3073 case GST_VIDEO_FORMAT_RGB:
3074 case GST_VIDEO_FORMAT_BGR:
3075 video_box->fill = (video_box->out_format == GST_VIDEO_FORMAT_BGR
3076 || video_box->out_format ==
3077 GST_VIDEO_FORMAT_RGB) ? fill_rgb24 : fill_rgb32;
3078 switch (video_box->in_format) {
3079 case GST_VIDEO_FORMAT_ARGB:
3080 case GST_VIDEO_FORMAT_ABGR:
3081 case GST_VIDEO_FORMAT_RGBA:
3082 case GST_VIDEO_FORMAT_BGRA:
3083 case GST_VIDEO_FORMAT_xRGB:
3084 case GST_VIDEO_FORMAT_xBGR:
3085 case GST_VIDEO_FORMAT_RGBx:
3086 case GST_VIDEO_FORMAT_BGRx:
3087 case GST_VIDEO_FORMAT_RGB:
3088 case GST_VIDEO_FORMAT_BGR:
3089 video_box->copy = copy_rgb32;
3090 break;
3091 case GST_VIDEO_FORMAT_AYUV:
3092 video_box->copy = copy_ayuv_rgb32;
3093 default:
3094 break;
3095 }
3096 break;
3097 case GST_VIDEO_FORMAT_GRAY8:
3098 case GST_VIDEO_FORMAT_GRAY16_BE:
3099 case GST_VIDEO_FORMAT_GRAY16_LE:
3100 video_box->fill = fill_gray;
3101 switch (video_box->in_format) {
3102 case GST_VIDEO_FORMAT_GRAY8:
3103 case GST_VIDEO_FORMAT_GRAY16_BE:
3104 case GST_VIDEO_FORMAT_GRAY16_LE:
3105 video_box->copy = copy_packed_simple;
3106 break;
3107 default:
3108 break;
3109 }
3110 break;
3111 case GST_VIDEO_FORMAT_YUY2:
3112 case GST_VIDEO_FORMAT_YVYU:
3113 case GST_VIDEO_FORMAT_UYVY:
3114 video_box->fill = fill_yuy2;
3115 switch (video_box->in_format) {
3116 case GST_VIDEO_FORMAT_YUY2:
3117 case GST_VIDEO_FORMAT_YVYU:
3118 case GST_VIDEO_FORMAT_UYVY:
3119 video_box->copy = copy_yuy2_yuy2;
3120 break;
3121 default:
3122 break;
3123 }
3124 break;
3125 case GST_VIDEO_FORMAT_Y444:
3126 case GST_VIDEO_FORMAT_Y42B:
3127 case GST_VIDEO_FORMAT_Y41B:
3128 video_box->fill = fill_planar_yuv;
3129 switch (video_box->in_format) {
3130 case GST_VIDEO_FORMAT_Y444:
3131 video_box->copy = copy_y444_y444;
3132 break;
3133 case GST_VIDEO_FORMAT_Y42B:
3134 video_box->copy = copy_y42b_y42b;
3135 break;
3136 case GST_VIDEO_FORMAT_Y41B:
3137 video_box->copy = copy_y41b_y41b;
3138 break;
3139 default:
3140 break;
3141 }
3142 break;
3143 default:
3144 break;
3145 }
3146
3147 return video_box->fill != NULL && video_box->copy != NULL;
3148 }
3149
3150 static gboolean
3151 gst_video_box_set_info (GstVideoFilter * vfilter, GstCaps * in,
3152 GstVideoInfo * in_info, GstCaps * out, GstVideoInfo * out_info)
3153 {
3154 GstVideoBox *video_box = GST_VIDEO_BOX (vfilter);
3155 gboolean ret;
3156
3157 g_mutex_lock (&video_box->mutex);
3158
3159 video_box->in_format = GST_VIDEO_INFO_FORMAT (in_info);
3160 video_box->in_width = GST_VIDEO_INFO_WIDTH (in_info);
3161 video_box->in_height = GST_VIDEO_INFO_HEIGHT (in_info);
3162
3163 video_box->out_format = GST_VIDEO_INFO_FORMAT (out_info);
3164 video_box->out_width = GST_VIDEO_INFO_WIDTH (out_info);
3165 video_box->out_height = GST_VIDEO_INFO_HEIGHT (out_info);
3166
3167 video_box->in_sdtv =
3168 in_info->colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_BT601;
3169 video_box->out_sdtv =
3170 out_info->colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_BT601;
3171
3172 GST_DEBUG_OBJECT (video_box, "Input w: %d h: %d", video_box->in_width,
3173 video_box->in_height);
3174 GST_DEBUG_OBJECT (video_box, "Output w: %d h: %d", video_box->out_width,
3175 video_box->out_height);
3176
3177 if (video_box->autocrop)
3178 gst_video_box_autocrop (video_box);
3179
3180 /* recalc the transformation strategy */
3181 ret = gst_video_box_recalc_transform (video_box);
3182
3183 if (ret)
3184 ret = gst_video_box_select_processing_functions (video_box);
3185 g_mutex_unlock (&video_box->mutex);
3186
3187 return ret;
3188 }
3189
3190 static gboolean
3191 gst_video_box_src_event (GstBaseTransform * trans, GstEvent * event)
3192 {
3193 GstVideoBox *video_box = GST_VIDEO_BOX (trans);
3194 gdouble x, y, new_x, new_y;
3195
3196 GST_OBJECT_LOCK (video_box);
3197
3198 switch (GST_EVENT_TYPE (event)) {
3199 case GST_EVENT_NAVIGATION:
3200 if ((video_box->box_left != 0 || video_box->box_top != 0)
3201 && gst_navigation_event_get_coordinates (event, &x, &y)) {
3202
3203 event = gst_event_make_writable (event);
3204 new_x = x + video_box->box_left;
3205 new_y = y + video_box->box_top;
3206
3207 GST_TRACE_OBJECT (video_box, "from %fx%f to %fx%f", x, y, new_x, new_y);
3208 gst_navigation_event_set_coordinates (event, new_x, new_y);
3209 }
3210 break;
3211 default:
3212 break;
3213 }
3214 GST_OBJECT_UNLOCK (video_box);
3215
3216 return GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans, event);
3217 }
3218
3219 static void
3220 gst_video_box_process (GstVideoBox * video_box, GstVideoFrame * in,
3221 GstVideoFrame * out)
3222 {
3223 guint b_alpha = CLAMP (video_box->border_alpha * 256, 0, 255);
3224 guint i_alpha = CLAMP (video_box->alpha * 256, 0, 255);
3225 GstVideoBoxFill fill_type = video_box->fill_type;
3226 gint br, bl, bt, bb;
3227
3228 GST_LOG_OBJECT (video_box, "Alpha value is: %u (frame) %u (border)",
3229 i_alpha, b_alpha);
3230
3231 br = video_box->box_right;
3232 bl = video_box->box_left;
3233 bt = video_box->box_top;
3234 bb = video_box->box_bottom;
3235
3236 if (video_box->crop_h < 0 || video_box->crop_w < 0) {
3237 video_box->fill (fill_type, b_alpha, out, video_box->out_sdtv);
3238 } else if (bb == 0 && bt == 0 && br == 0 && bl == 0) {
3239 video_box->copy (i_alpha, out, video_box->out_sdtv, 0, 0, in,
3240 video_box->in_sdtv, 0, 0, video_box->crop_w, video_box->crop_h);
3241 } else {
3242 /* Fill everything if a border should be added somewhere */
3243 if (bt < 0 || bb < 0 || br < 0 || bl < 0)
3244 video_box->fill (fill_type, b_alpha, out, video_box->out_sdtv);
3245
3246 /* Frame */
3247 video_box->copy (i_alpha, out, video_box->out_sdtv, video_box->dest_x,
3248 video_box->dest_y, in, video_box->in_sdtv, video_box->src_x,
3249 video_box->src_y, video_box->crop_w, video_box->crop_h);
3250 }
3251
3252 GST_LOG_OBJECT (video_box, "image created");
3253 }
3254
3255 static void
3256 gst_video_box_before_transform (GstBaseTransform * trans, GstBuffer * in)
3257 {
3258 GstVideoBox *video_box = GST_VIDEO_BOX (trans);
3259 GstClockTime timestamp, stream_time;
3260
3261 timestamp = GST_BUFFER_TIMESTAMP (in);
3262 stream_time =
3263 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
3264
3265 GST_DEBUG_OBJECT (video_box, "sync to %" GST_TIME_FORMAT,
3266 GST_TIME_ARGS (timestamp));
3267
3268 if (GST_CLOCK_TIME_IS_VALID (stream_time))
3269 gst_object_sync_values (GST_OBJECT (video_box), stream_time);
3270 }
3271
3272 static GstFlowReturn
3273 gst_video_box_transform_frame (GstVideoFilter * vfilter,
3274 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
3275 {
3276 GstVideoBox *video_box = GST_VIDEO_BOX (vfilter);
3277
3278 g_mutex_lock (&video_box->mutex);
3279 gst_video_box_process (video_box, in_frame, out_frame);
3280 g_mutex_unlock (&video_box->mutex);
3281 return GST_FLOW_OK;
3282 }
3283
3284 static gboolean
3285 gst_video_box_transform_meta (GstBaseTransform * trans,
3286 GstBuffer * outbuf, GstMeta * meta, GstBuffer * inbuf)
3287 {
3288 GstVideoBox *video_box = GST_VIDEO_BOX (trans);
3289 GstVideoFilter *video_filter = GST_VIDEO_FILTER (trans);
3290 const GstMetaInfo *info = meta->info;
3291 const gchar *const *tags;
3292 const gchar *const *curr = NULL;
3293 gboolean should_copy = TRUE;
3294 const gchar *const valid_tags[] = {
3295 GST_META_TAG_VIDEO_STR,
3296 GST_META_TAG_VIDEO_ORIENTATION_STR,
3297 GST_META_TAG_VIDEO_SIZE_STR,
3298 NULL
3299 };
3300
3301 tags = gst_meta_api_type_get_tags (info->api);
3302
3303 /* No specific tags, we are good to copy */
3304 if (!tags) {
3305 return TRUE;
3306 }
3307
3308 if (video_box->in_format != video_box->out_format &&
3309 gst_meta_api_type_has_tag (info->api, _colorspace_quark)) {
3310 /* don't copy colorspace specific metadata, FIXME, we need a MetaTransform
3311 * for the colorspace metadata. */
3312 return FALSE;
3313 }
3314
3315 /* We are only changing size, we can preserve other metas tagged as
3316 orientation and colorspace */
3317 for (curr = tags; *curr; ++curr) {
3318
3319 /* We dont handle any other tag */
3320 if (!g_strv_contains (valid_tags, *curr)) {
3321 should_copy = FALSE;
3322 break;
3323 }
3324 }
3325
3326 /* Cant handle the tags in this meta, let the parent class handle it */
3327 if (!should_copy) {
3328 return GST_BASE_TRANSFORM_CLASS (parent_class)->transform_meta (trans,
3329 outbuf, meta, inbuf);
3330 }
3331
3332 /* This meta is size sensitive, try to transform it accordingly */
3333 if (gst_meta_api_type_has_tag (info->api, _size_quark)) {
3334 if (info->transform_func) {
3335 GstVideoMetaTransformMatrix trans_matrix;
3336 const GstVideoRectangle in_rectangle = { video_box->src_x,
3337 video_box->src_y, video_box->crop_w, video_box->crop_h
3338 };
3339 const GstVideoRectangle out_rectangle =
3340 { video_box->dest_x, video_box->dest_y,
3341 video_box->crop_w, video_box->crop_h
3342 };
3343
3344 gst_video_meta_transform_matrix_init (&trans_matrix,
3345 &video_filter->in_info, &in_rectangle, &video_filter->out_info,
3346 &out_rectangle);
3347
3348 info->transform_func (outbuf, meta, inbuf,
3349 gst_video_meta_transform_matrix_get_quark (), &trans_matrix);
3350 }
3351 return FALSE;
3352 }
3353
3354 /* No need to transform, we can safely copy this meta */
3355 return TRUE;
3356 }
3357
3358
3359 /* FIXME: 0.11 merge with videocrop plugin */
3360 static gboolean
3361 plugin_init (GstPlugin * plugin)
3362 {
3363 return GST_ELEMENT_REGISTER (videobox, plugin);
3364 }
3365
3366 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
3367 GST_VERSION_MINOR,
3368 videobox,
3369 "resizes a video by adding borders or cropping",
3370 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)