1 /* Gstreamer video blending utility functions
2  *
3  * Copied/pasted from gst/videoconvert/videoconvert.c
4  *    Copyright (C) 2010 David Schleef <ds@schleef.org>
5  *    Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
6  *
7  * Copyright (C) <2011> Intel Corporation
8  * Copyright (C) <2011> Collabora Ltd.
9  * Copyright (C) <2011> Thibault Saunier <thibault.saunier@collabora.com>
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
24  * Boston, MA 02110-1301, USA.
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 
30 #include "video-blend.h"
31 #include "video-orc.h"
32 
33 #include <string.h>
34 
35 #ifndef GST_DISABLE_GST_DEBUG
36 
37 #define GST_CAT_DEFAULT ensure_debug_category()
38 
39 static GstDebugCategory *
ensure_debug_category(void)40 ensure_debug_category (void)
41 {
42   static gsize cat_gonce = 0;
43 
44   if (g_once_init_enter (&cat_gonce)) {
45     gsize cat_done;
46 
47     cat_done = (gsize) _gst_debug_category_new ("video-blending", 0,
48         "video blending");
49 
50     g_once_init_leave (&cat_gonce, cat_done);
51   }
52 
53   return (GstDebugCategory *) cat_gonce;
54 }
55 
56 #else
57 
58 #define ensure_debug_category() /* NOOP */
59 
60 #endif /* GST_DISABLE_GST_DEBUG */
61 
62 static void
matrix_identity(guint8 * tmpline,guint width)63 matrix_identity (guint8 * tmpline, guint width)
64 {
65 }
66 
67 static void
matrix_prea_rgb_to_yuv(guint8 * tmpline,guint width)68 matrix_prea_rgb_to_yuv (guint8 * tmpline, guint width)
69 {
70   int i;
71   int a, r, g, b;
72   int y, u, v;
73 
74   for (i = 0; i < width; i++) {
75     a = tmpline[i * 4 + 0];
76     r = tmpline[i * 4 + 1];
77     g = tmpline[i * 4 + 2];
78     b = tmpline[i * 4 + 3];
79     if (a) {
80       r = (r * 255 + a / 2) / a;
81       g = (g * 255 + a / 2) / a;
82       b = (b * 255 + a / 2) / a;
83     }
84 
85     y = (47 * r + 157 * g + 16 * b + 4096) >> 8;
86     u = (-26 * r - 87 * g + 112 * b + 32768) >> 8;
87     v = (112 * r - 102 * g - 10 * b + 32768) >> 8;
88 
89     tmpline[i * 4 + 1] = CLAMP (y, 0, 255);
90     tmpline[i * 4 + 2] = CLAMP (u, 0, 255);
91     tmpline[i * 4 + 3] = CLAMP (v, 0, 255);
92   }
93 }
94 
95 static void
matrix_rgb_to_yuv(guint8 * tmpline,guint width)96 matrix_rgb_to_yuv (guint8 * tmpline, guint width)
97 {
98   int i;
99   int r, g, b;
100   int y, u, v;
101 
102   for (i = 0; i < width; i++) {
103     r = tmpline[i * 4 + 1];
104     g = tmpline[i * 4 + 2];
105     b = tmpline[i * 4 + 3];
106 
107     y = (47 * r + 157 * g + 16 * b + 4096) >> 8;
108     u = (-26 * r - 87 * g + 112 * b + 32768) >> 8;
109     v = (112 * r - 102 * g - 10 * b + 32768) >> 8;
110 
111     tmpline[i * 4 + 1] = CLAMP (y, 0, 255);
112     tmpline[i * 4 + 2] = CLAMP (u, 0, 255);
113     tmpline[i * 4 + 3] = CLAMP (v, 0, 255);
114   }
115 }
116 
117 static void
matrix_yuv_to_rgb(guint8 * tmpline,guint width)118 matrix_yuv_to_rgb (guint8 * tmpline, guint width)
119 {
120   int i;
121   int r, g, b;
122   int y, u, v;
123 
124   for (i = 0; i < width; i++) {
125     y = tmpline[i * 4 + 1];
126     u = tmpline[i * 4 + 2];
127     v = tmpline[i * 4 + 3];
128 
129     r = (298 * y + 459 * v - 63514) >> 8;
130     g = (298 * y - 55 * u - 136 * v + 19681) >> 8;
131     b = (298 * y + 541 * u - 73988) >> 8;
132 
133     tmpline[i * 4 + 1] = CLAMP (r, 0, 255);
134     tmpline[i * 4 + 2] = CLAMP (g, 0, 255);
135     tmpline[i * 4 + 3] = CLAMP (b, 0, 255);
136   }
137 }
138 
139 /**
140  * gst_video_blend_scale_linear_RGBA:
141  * @src: the #GstVideoInfo describing the video data in @src_buffer
142  * @src_buffer: the source buffer containing video pixels to scale
143  * @dest_height: the height in pixels to scale the video data in @src_buffer to
144  * @dest_width: the width in pixels to scale the video data in @src_buffer to
145  * @dest: (out): pointer to a #GstVideoInfo structure that will be filled in
146  *     with the details for @dest_buffer
147  * @dest_buffer: (out): a pointer to a #GstBuffer variable, which will be
148  *     set to a newly-allocated buffer containing the scaled pixels.
149  *
150  * Scales a buffer containing RGBA (or AYUV) video. This is an internal
151  * helper function which is used to scale subtitle overlays, and may be
152  * deprecated in the near future. Use #GstVideoScaler to scale video buffers
153  * instead.
154  */
155 /* returns newly-allocated buffer, which caller must unref */
156 void
gst_video_blend_scale_linear_RGBA(GstVideoInfo * src,GstBuffer * src_buffer,gint dest_height,gint dest_width,GstVideoInfo * dest,GstBuffer ** dest_buffer)157 gst_video_blend_scale_linear_RGBA (GstVideoInfo * src, GstBuffer * src_buffer,
158     gint dest_height, gint dest_width, GstVideoInfo * dest,
159     GstBuffer ** dest_buffer)
160 {
161   const guint8 *src_pixels;
162   int acc;
163   int y_increment;
164   int x_increment;
165   int y1;
166   int i;
167   int j;
168   int x;
169   int dest_size;
170   guint dest_stride;
171   guint src_stride;
172   guint8 *dest_pixels;
173   guint8 *tmpbuf;
174   GstVideoFrame src_frame, dest_frame;
175 
176   g_return_if_fail (dest_buffer != NULL);
177 
178   gst_video_info_init (dest);
179   if (!gst_video_info_set_format (dest, GST_VIDEO_INFO_FORMAT (src),
180           dest_width, dest_height)) {
181     g_warn_if_reached ();
182     return;
183   }
184 
185   tmpbuf = g_malloc (dest_width * 8 * 4);
186 
187   *dest_buffer = gst_buffer_new_and_alloc (GST_VIDEO_INFO_SIZE (dest));
188 
189   gst_video_frame_map (&src_frame, src, src_buffer, GST_MAP_READ);
190   gst_video_frame_map (&dest_frame, dest, *dest_buffer, GST_MAP_WRITE);
191 
192   if (dest_height == 1 || src->height == 1)
193     y_increment = 0;
194   else
195     y_increment = ((src->height - 1) << 16) / (dest_height - 1) - 1;
196 
197   if (dest_width == 1 || src->width == 1)
198     x_increment = 0;
199   else
200     x_increment = ((src->width - 1) << 16) / (dest_width - 1) - 1;
201 
202   dest_size = dest_stride = dest_width * 4;
203   src_stride = GST_VIDEO_FRAME_PLANE_STRIDE (&src_frame, 0);
204 
205 #define LINE(x) ((tmpbuf) + (dest_size)*((x)&1))
206 
207   dest_pixels = GST_VIDEO_FRAME_PLANE_DATA (&dest_frame, 0);
208   src_pixels = GST_VIDEO_FRAME_PLANE_DATA (&src_frame, 0);
209 
210   acc = 0;
211   video_orc_resample_bilinear_u32 (LINE (0), src_pixels, 0, x_increment,
212       dest_width);
213   y1 = 0;
214   for (i = 0; i < dest_height; i++) {
215     j = acc >> 16;
216     x = acc & 0xffff;
217 
218     if (x == 0) {
219       memcpy (dest_pixels + i * dest_stride, LINE (j), dest_size);
220     } else {
221       if (j > y1) {
222         video_orc_resample_bilinear_u32 (LINE (j),
223             src_pixels + j * src_stride, 0, x_increment, dest_width);
224         y1++;
225       }
226       if (j >= y1) {
227         video_orc_resample_bilinear_u32 (LINE (j + 1),
228             src_pixels + (j + 1) * src_stride, 0, x_increment, dest_width);
229         y1++;
230       }
231       video_orc_merge_linear_u8 (dest_pixels + i * dest_stride,
232           LINE (j), LINE (j + 1), (x >> 8), dest_width * 4);
233     }
234 
235     acc += y_increment;
236   }
237 
238   gst_video_frame_unmap (&src_frame);
239   gst_video_frame_unmap (&dest_frame);
240 
241   g_free (tmpbuf);
242 }
243 
244 /*
245  * A OVER B alpha compositing operation, with:
246  *  alphaG: global alpha to apply on the source color
247  *     -> only needed for premultiplied source
248  *  alphaA: source pixel alpha
249  *  colorA: source pixel color
250  *  alphaB: destination pixel alpha
251  *  colorB: destination pixel color
252  *  alphaD: blended pixel alpha
253  *     -> only needed for premultiplied destination
254  */
255 
256 /* Source non-premultiplied, Destination non-premultiplied */
257 #define OVER00(alphaG, alphaA, colorA, alphaB, colorB, alphaD) \
258   ((colorA * alphaA + colorB * alphaB * (255 - alphaA) / 255) / alphaD)
259 
260 /* Source premultiplied, Destination non-premultiplied */
261 #define OVER10(alphaG, alphaA, colorA, alphaB, colorB, alphaD) \
262   ((colorA * alphaG + colorB * alphaB * (255 - alphaA) / 255) / alphaD)
263 
264 /* Source non-premultiplied, Destination premultiplied */
265 #define OVER01(alphaG, alphaA, colorA, alphaB, colorB, alphaD) \
266   ((colorA * alphaA + colorB * (255 - alphaA)) / 255)
267 
268 /* Source premultiplied, Destination premultiplied */
269 #define OVER11(alphaG, alphaA, colorA, alphaB, colorB, alphaD) \
270   ((colorA * alphaG + colorB * (255 - alphaA)) / 255)
271 
272 #define BLENDC(op, global_alpha, aa, ca, ab, cb, dest_alpha) \
273 G_STMT_START { \
274   int c = op(global_alpha, aa, ca, ab, cb, dest_alpha); \
275   cb = MIN(c, 255); \
276 } G_STMT_END
277 
278 
279 /**
280  * gst_video_blend:
281  * @dest: The #GstVideoFrame where to blend @src in
282  * @src: the #GstVideoFrame that we want to blend into
283  * @x: The x offset in pixel where the @src image should be blended
284  * @y: the y offset in pixel where the @src image should be blended
285  * @global_alpha: the global_alpha each per-pixel alpha value is multiplied
286  *                with
287  *
288  * Lets you blend the @src image into the @dest image
289  */
290 gboolean
gst_video_blend(GstVideoFrame * dest,GstVideoFrame * src,gint x,gint y,gfloat global_alpha)291 gst_video_blend (GstVideoFrame * dest,
292     GstVideoFrame * src, gint x, gint y, gfloat global_alpha)
293 {
294   gint i, j, global_alpha_val, src_width, src_height, dest_width, dest_height;
295   gint src_xoff = 0, src_yoff = 0;
296   guint8 *tmpdestline = NULL, *tmpsrcline = NULL;
297   gboolean src_premultiplied_alpha, dest_premultiplied_alpha;
298   void (*matrix) (guint8 * tmpline, guint width);
299   const GstVideoFormatInfo *sinfo, *dinfo, *dunpackinfo, *sunpackinfo;
300 
301   g_assert (dest != NULL);
302   g_assert (src != NULL);
303 
304   global_alpha_val = 255.0 * global_alpha;
305 
306   dest_premultiplied_alpha =
307       GST_VIDEO_INFO_FLAGS (&dest->info) & GST_VIDEO_FLAG_PREMULTIPLIED_ALPHA;
308   src_premultiplied_alpha =
309       GST_VIDEO_INFO_FLAGS (&src->info) & GST_VIDEO_FLAG_PREMULTIPLIED_ALPHA;
310 
311   src_width = GST_VIDEO_FRAME_WIDTH (src);
312   src_height = GST_VIDEO_FRAME_HEIGHT (src);
313 
314   dest_width = GST_VIDEO_FRAME_WIDTH (dest);
315   dest_height = GST_VIDEO_FRAME_HEIGHT (dest);
316 
317   ensure_debug_category ();
318 
319   GST_LOG ("blend src %dx%d onto dest %dx%d @ %d,%d", src_width, src_height,
320       dest_width, dest_height, x, y);
321 
322   /* In case overlay is completely outside the video, dont render */
323   if (x + src_width <= 0 || y + src_height <= 0
324       || x >= dest_width || y >= dest_height) {
325     goto nothing_to_do;
326   }
327 
328   dinfo = gst_video_format_get_info (GST_VIDEO_FRAME_FORMAT (dest));
329   sinfo = gst_video_format_get_info (GST_VIDEO_FRAME_FORMAT (src));
330 
331   if (!sinfo || !dinfo)
332     goto failed;
333 
334   dunpackinfo = gst_video_format_get_info (dinfo->unpack_format);
335   sunpackinfo = gst_video_format_get_info (sinfo->unpack_format);
336 
337   if (dunpackinfo == NULL || sunpackinfo == NULL)
338     goto failed;
339 
340   g_assert (GST_VIDEO_FORMAT_INFO_BITS (sunpackinfo) == 8);
341 
342   if (GST_VIDEO_FORMAT_INFO_BITS (dunpackinfo) != 8)
343     goto unpack_format_not_supported;
344 
345   tmpdestline = g_malloc (sizeof (guint8) * (dest_width + 8) * 4);
346   tmpsrcline = g_malloc (sizeof (guint8) * (src_width + 8) * 4);
347 
348   matrix = matrix_identity;
349   if (GST_VIDEO_INFO_IS_RGB (&src->info) != GST_VIDEO_INFO_IS_RGB (&dest->info)) {
350     if (GST_VIDEO_INFO_IS_RGB (&src->info)) {
351       if (src_premultiplied_alpha) {
352         matrix = matrix_prea_rgb_to_yuv;
353         src_premultiplied_alpha = FALSE;
354       } else {
355         matrix = matrix_rgb_to_yuv;
356       }
357     } else {
358       matrix = matrix_yuv_to_rgb;
359     }
360   }
361 
362   /* If we're here we know that the overlay image fully or
363    * partially overlaps with the video frame */
364 
365   /* adjust src image for negative offsets */
366   if (x < 0) {
367     src_xoff = -x;
368     src_width -= src_xoff;
369     x = 0;
370   }
371 
372   if (y < 0) {
373     src_yoff = -y;
374     src_height -= src_yoff;
375     y = 0;
376   }
377 
378   /* adjust width/height to render (i.e. clip source image) if the source
379    * image extends beyond the right or bottom border of the video surface */
380   if (x + src_width > dest_width)
381     src_width = dest_width - x;
382 
383   if (y + src_height > dest_height)
384     src_height = dest_height - y;
385 
386   /* Mainloop doing the needed conversions, and blending */
387   for (i = y; i < y + src_height; i++, src_yoff++) {
388 
389     dinfo->unpack_func (dinfo, 0, tmpdestline, dest->data, dest->info.stride,
390         0, i, dest_width);
391     sinfo->unpack_func (sinfo, 0, tmpsrcline, src->data, src->info.stride,
392         src_xoff, src_yoff, src_width);
393 
394     /* FIXME: use the x parameter of the unpack func once implemented */
395     tmpdestline += 4 * x;
396 
397     matrix (tmpsrcline, src_width);
398 
399 #define BLENDLOOP(op, alpha_val)                                                              \
400   G_STMT_START {                                                                              \
401     for (j = 0; j < src_width * 4; j += 4) {                                                  \
402       guint8 asrc, adst;                                                                      \
403       gint final_alpha;                                                                       \
404                                                                                               \
405       asrc = tmpsrcline[j] * alpha_val / 255;                                                 \
406       if (!asrc)                                                                              \
407         continue;                                                                             \
408                                                                                               \
409       adst = tmpdestline[j];                                                                  \
410       final_alpha = asrc + adst * (255 - asrc) / 255;                                         \
411       tmpdestline[j] = final_alpha;                                                           \
412       if (final_alpha == 0)                                                                   \
413         final_alpha = 1;                                                                      \
414                                                                                               \
415       BLENDC (op, alpha_val, asrc, tmpsrcline[j + 1], adst, tmpdestline[j + 1], final_alpha); \
416       BLENDC (op, alpha_val, asrc, tmpsrcline[j + 2], adst, tmpdestline[j + 2], final_alpha); \
417       BLENDC (op, alpha_val, asrc, tmpsrcline[j + 3], adst, tmpdestline[j + 3], final_alpha); \
418     }                                                                                         \
419   } G_STMT_END
420 
421     if (G_LIKELY (global_alpha == 1.0)) {
422       if (src_premultiplied_alpha && dest_premultiplied_alpha) {
423         BLENDLOOP (OVER11, 255);
424       } else if (!src_premultiplied_alpha && dest_premultiplied_alpha) {
425         BLENDLOOP (OVER01, 255);
426       } else if (src_premultiplied_alpha && !dest_premultiplied_alpha) {
427         BLENDLOOP (OVER10, 255);
428       } else {
429         BLENDLOOP (OVER00, 255);
430       }
431     } else {
432       if (src_premultiplied_alpha && dest_premultiplied_alpha) {
433         BLENDLOOP (OVER11, global_alpha_val);
434       } else if (!src_premultiplied_alpha && dest_premultiplied_alpha) {
435         BLENDLOOP (OVER01, global_alpha_val);
436       } else if (src_premultiplied_alpha && !dest_premultiplied_alpha) {
437         BLENDLOOP (OVER10, global_alpha_val);
438       } else {
439         BLENDLOOP (OVER00, global_alpha_val);
440       }
441     }
442 
443 #undef BLENDLOOP
444 
445     /* undo previous pointer adjustments to pass right pointer to g_free */
446     tmpdestline -= 4 * x;
447 
448     /* FIXME
449      * #if G_BYTE_ORDER == LITTLE_ENDIAN
450      * video_orc_blend_little (tmpdestline, tmpsrcline, dest->width);
451      * #else
452      * video_orc_blend_big (tmpdestline, tmpsrcline, src->width);
453      * #endif
454      */
455 
456     dinfo->pack_func (dinfo, 0, tmpdestline, dest_width,
457         dest->data, dest->info.stride, dest->info.chroma_site, i, dest_width);
458   }
459 
460   g_free (tmpdestline);
461   g_free (tmpsrcline);
462 
463   return TRUE;
464 
465 failed:
466   {
467     GST_WARNING ("Could not do the blending");
468     return FALSE;
469   }
470 unpack_format_not_supported:
471   {
472     GST_FIXME ("video format %s not supported yet for blending",
473         gst_video_format_to_string (dinfo->unpack_format));
474     return FALSE;
475   }
476 nothing_to_do:
477   {
478     GST_LOG
479         ("Overlay completely outside the video surface, hence not rendering");
480     return TRUE;
481   }
482 }
483