Coverage Report

Created: 2025-12-28 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gstreamer/subprojects/gst-plugins-base/gst-libs/gst/video/video-blend.c
Line
Count
Source
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 *
40
ensure_debug_category (void)
41
0
{
42
0
  static gsize cat_gonce = 0;
43
44
0
  if (g_once_init_enter (&cat_gonce)) {
45
0
    gsize cat_done;
46
47
0
    cat_done = (gsize) _gst_debug_category_new ("video-blending", 0,
48
0
        "video blending");
49
50
0
    g_once_init_leave (&cat_gonce, cat_done);
51
0
  }
52
53
0
  return (GstDebugCategory *) cat_gonce;
54
0
}
55
56
#else
57
58
#define ensure_debug_category() /* NOOP */
59
60
#endif /* GST_DISABLE_GST_DEBUG */
61
62
static void
63
matrix_identity (guint8 * tmpline, guint width)
64
0
{
65
0
}
66
67
static void
68
matrix_prea_rgb_to_yuv (guint8 * tmpline, guint width)
69
0
{
70
0
  int i;
71
0
  int a, r, g, b;
72
0
  int y, u, v;
73
74
0
  for (i = 0; i < width; i++) {
75
0
    a = tmpline[i * 4 + 0];
76
0
    r = tmpline[i * 4 + 1];
77
0
    g = tmpline[i * 4 + 2];
78
0
    b = tmpline[i * 4 + 3];
79
0
    if (a) {
80
0
      r = (r * 255 + a / 2) / a;
81
0
      g = (g * 255 + a / 2) / a;
82
0
      b = (b * 255 + a / 2) / a;
83
0
    }
84
85
0
    y = (47 * r + 157 * g + 16 * b + 4096) >> 8;
86
0
    u = (-26 * r - 87 * g + 112 * b + 32768) >> 8;
87
0
    v = (112 * r - 102 * g - 10 * b + 32768) >> 8;
88
89
0
    tmpline[i * 4 + 1] = CLAMP (y, 0, 255);
90
0
    tmpline[i * 4 + 2] = CLAMP (u, 0, 255);
91
0
    tmpline[i * 4 + 3] = CLAMP (v, 0, 255);
92
0
  }
93
0
}
94
95
static void
96
matrix_rgb_to_yuv (guint8 * tmpline, guint width)
97
0
{
98
0
  int i;
99
0
  int r, g, b;
100
0
  int y, u, v;
101
102
0
  for (i = 0; i < width; i++) {
103
0
    r = tmpline[i * 4 + 1];
104
0
    g = tmpline[i * 4 + 2];
105
0
    b = tmpline[i * 4 + 3];
106
107
0
    y = (47 * r + 157 * g + 16 * b + 4096) >> 8;
108
0
    u = (-26 * r - 87 * g + 112 * b + 32768) >> 8;
109
0
    v = (112 * r - 102 * g - 10 * b + 32768) >> 8;
110
111
0
    tmpline[i * 4 + 1] = CLAMP (y, 0, 255);
112
0
    tmpline[i * 4 + 2] = CLAMP (u, 0, 255);
113
0
    tmpline[i * 4 + 3] = CLAMP (v, 0, 255);
114
0
  }
115
0
}
116
117
static void
118
matrix_yuv_to_rgb (guint8 * tmpline, guint width)
119
0
{
120
0
  int i;
121
0
  int r, g, b;
122
0
  int y, u, v;
123
124
0
  for (i = 0; i < width; i++) {
125
0
    y = tmpline[i * 4 + 1];
126
0
    u = tmpline[i * 4 + 2];
127
0
    v = tmpline[i * 4 + 3];
128
129
0
    r = (298 * y + 459 * v - 63514) >> 8;
130
0
    g = (298 * y - 55 * u - 136 * v + 19681) >> 8;
131
0
    b = (298 * y + 541 * u - 73988) >> 8;
132
133
0
    tmpline[i * 4 + 1] = CLAMP (r, 0, 255);
134
0
    tmpline[i * 4 + 2] = CLAMP (g, 0, 255);
135
0
    tmpline[i * 4 + 3] = CLAMP (b, 0, 255);
136
0
  }
137
0
}
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
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
0
{
161
0
  const guint8 *src_pixels;
162
0
  int acc;
163
0
  int y_increment;
164
0
  int x_increment;
165
0
  int y1;
166
0
  int i;
167
0
  int j;
168
0
  int x;
169
0
  int dest_size;
170
0
  guint dest_stride;
171
0
  guint src_stride;
172
0
  guint8 *dest_pixels;
173
0
  guint8 *tmpbuf;
174
0
  GstVideoFrame src_frame, dest_frame;
175
176
0
  g_return_if_fail (dest_buffer != NULL);
177
178
0
  gst_video_info_init (dest);
179
0
  if (!gst_video_info_set_format (dest, GST_VIDEO_INFO_FORMAT (src),
180
0
          dest_width, dest_height)) {
181
0
    g_warn_if_reached ();
182
0
    return;
183
0
  }
184
185
0
  tmpbuf = g_malloc (dest_width * 8 * 4);
186
187
0
  *dest_buffer = gst_buffer_new_and_alloc (GST_VIDEO_INFO_SIZE (dest));
188
189
0
  gst_video_frame_map (&src_frame, src, src_buffer, GST_MAP_READ);
190
0
  gst_video_frame_map (&dest_frame, dest, *dest_buffer, GST_MAP_WRITE);
191
192
0
  if (dest_height == 1 || src->height == 1)
193
0
    y_increment = 0;
194
0
  else
195
0
    y_increment = ((src->height - 1) << 16) / (dest_height - 1) - 1;
196
197
0
  if (dest_width == 1 || src->width == 1)
198
0
    x_increment = 0;
199
0
  else
200
0
    x_increment = ((src->width - 1) << 16) / (dest_width - 1) - 1;
201
202
0
  dest_size = dest_stride = dest_width * 4;
203
0
  src_stride = GST_VIDEO_FRAME_PLANE_STRIDE (&src_frame, 0);
204
205
0
#define LINE(x) ((tmpbuf) + (dest_size)*((x)&1))
206
207
0
  dest_pixels = GST_VIDEO_FRAME_PLANE_DATA (&dest_frame, 0);
208
0
  src_pixels = GST_VIDEO_FRAME_PLANE_DATA (&src_frame, 0);
209
210
0
  acc = 0;
211
0
  video_orc_resample_bilinear_u32 (LINE (0), src_pixels, 0, x_increment,
212
0
      dest_width);
213
0
  y1 = 0;
214
0
  for (i = 0; i < dest_height; i++) {
215
0
    j = acc >> 16;
216
0
    x = acc & 0xffff;
217
218
0
    if (x == 0) {
219
0
      memcpy (dest_pixels + i * dest_stride, LINE (j), dest_size);
220
0
    } else {
221
0
      if (j > y1) {
222
0
        video_orc_resample_bilinear_u32 (LINE (j),
223
0
            src_pixels + j * src_stride, 0, x_increment, dest_width);
224
0
        y1++;
225
0
      }
226
0
      if (j >= y1) {
227
0
        video_orc_resample_bilinear_u32 (LINE (j + 1),
228
0
            src_pixels + (j + 1) * src_stride, 0, x_increment, dest_width);
229
0
        y1++;
230
0
      }
231
0
      video_orc_merge_linear_u8 (dest_pixels + i * dest_stride,
232
0
          LINE (j), LINE (j + 1), (x >> 8), dest_width * 4);
233
0
    }
234
235
0
    acc += y_increment;
236
0
  }
237
238
0
  gst_video_frame_unmap (&src_frame);
239
0
  gst_video_frame_unmap (&dest_frame);
240
241
0
  g_free (tmpbuf);
242
0
}
243
244
/*
245
 * A OVER B alpha compositing operation, with:
246
 *  max: maximum value a color can have
247
 *  alphaG: global alpha to apply on the source color
248
 *     -> only needed for premultiplied source
249
 *  alphaA: source pixel alpha
250
 *  colorA: source pixel color
251
 *  alphaB: destination pixel alpha
252
 *  colorB: destination pixel color
253
 *  alphaD: blended pixel alpha
254
 *     -> only needed for premultiplied destination
255
 */
256
257
/* Source non-premultiplied, Destination non-premultiplied */
258
#define OVER00_8BIT(max, alphaG, alphaA, colorA, alphaB, colorB, alphaD) \
259
0
  ((colorA * alphaA + colorB * alphaB * (max - alphaA) / max) / alphaD)
260
261
#define OVER00_16BIT(max, alphaG, alphaA, colorA, alphaB, colorB, alphaD) \
262
0
  ((colorA * alphaA + (guint64) colorB * alphaB * (max - alphaA) / max) / alphaD)
263
264
/* Source premultiplied, Destination non-premultiplied */
265
#define OVER10_8BIT(max, alphaG, alphaA, colorA, alphaB, colorB, alphaD) \
266
0
  ((colorA * alphaG + colorB * alphaB * (max - alphaA) / max) / alphaD)
267
268
#define OVER10_16BIT(max, alphaG, alphaA, colorA, alphaB, colorB, alphaD) \
269
0
  ((colorA * alphaG + (guint64) colorB * alphaB * (max - alphaA) / max) / alphaD)
270
271
/* Source non-premultiplied, Destination premultiplied */
272
#define OVER01(max, alphaG, alphaA, colorA, alphaB, colorB, alphaD) \
273
0
  ((colorA * alphaA + colorB * (max - alphaA)) / max)
274
275
/* Source premultiplied, Destination premultiplied */
276
#define OVER11(max, alphaG, alphaA, colorA, alphaB, colorB, alphaD) \
277
0
  ((colorA * alphaG + colorB * (max - alphaA)) / max)
278
279
0
#define BLENDC(op, max, global_alpha, aa, ca, ab, cb, dest_alpha) \
280
0
G_STMT_START { \
281
0
  int c = op((max), (global_alpha), (aa), (ca), (ab), (cb), (dest_alpha)); \
282
0
  cb = MIN(c, (max)); \
283
0
} G_STMT_END
284
285
286
/**
287
 * gst_video_blend:
288
 * @dest: The #GstVideoFrame where to blend @src in
289
 * @src: the #GstVideoFrame that we want to blend into
290
 * @x: The x offset in pixel where the @src image should be blended
291
 * @y: the y offset in pixel where the @src image should be blended
292
 * @global_alpha: the global_alpha each per-pixel alpha value is multiplied
293
 *                with
294
 *
295
 * Lets you blend the @src image into the @dest image
296
 */
297
gboolean
298
gst_video_blend (GstVideoFrame * dest,
299
    GstVideoFrame * src, gint x, gint y, gfloat global_alpha)
300
0
{
301
0
  gint i, j, global_alpha_val, src_width, src_height, dest_width, dest_height;
302
0
  gint src_xoff = 0, src_yoff = 0;
303
0
  guint8 *tmpdestline = NULL, *tmpsrcline = NULL;
304
0
  gboolean src_premultiplied_alpha, dest_premultiplied_alpha;
305
0
  gint bpp;
306
0
  void (*matrix) (guint8 * tmpline, guint width);
307
0
  const GstVideoFormatInfo *sinfo, *dinfo, *dunpackinfo, *sunpackinfo;
308
309
0
  g_assert (dest != NULL);
310
0
  g_assert (src != NULL);
311
312
0
  dest_premultiplied_alpha =
313
0
      GST_VIDEO_INFO_FLAGS (&dest->info) & GST_VIDEO_FLAG_PREMULTIPLIED_ALPHA;
314
0
  src_premultiplied_alpha =
315
0
      GST_VIDEO_INFO_FLAGS (&src->info) & GST_VIDEO_FLAG_PREMULTIPLIED_ALPHA;
316
317
0
  src_width = GST_VIDEO_FRAME_WIDTH (src);
318
0
  src_height = GST_VIDEO_FRAME_HEIGHT (src);
319
320
0
  dest_width = GST_VIDEO_FRAME_WIDTH (dest);
321
0
  dest_height = GST_VIDEO_FRAME_HEIGHT (dest);
322
323
0
  ensure_debug_category ();
324
325
0
  GST_LOG ("blend src %dx%d onto dest %dx%d @ %d,%d", src_width, src_height,
326
0
      dest_width, dest_height, x, y);
327
328
  /* In case overlay is completely outside the video, don't render */
329
0
  if (x + src_width <= 0 || y + src_height <= 0
330
0
      || x >= dest_width || y >= dest_height) {
331
0
    goto nothing_to_do;
332
0
  }
333
334
0
  dinfo = gst_video_format_get_info (GST_VIDEO_FRAME_FORMAT (dest));
335
0
  sinfo = gst_video_format_get_info (GST_VIDEO_FRAME_FORMAT (src));
336
337
0
  if (!sinfo || !dinfo)
338
0
    goto failed;
339
340
0
  dunpackinfo = gst_video_format_get_info (dinfo->unpack_format);
341
0
  sunpackinfo = gst_video_format_get_info (sinfo->unpack_format);
342
343
0
  if (dunpackinfo == NULL || sunpackinfo == NULL)
344
0
    goto failed;
345
346
0
  g_assert (GST_VIDEO_FORMAT_INFO_BITS (sunpackinfo) == 8);
347
348
0
  if (GST_VIDEO_FORMAT_INFO_BITS (dunpackinfo) != 8
349
0
      && GST_VIDEO_FORMAT_INFO_BITS (dunpackinfo) != 16)
350
0
    goto unpack_format_not_supported;
351
352
  /* Source is always 8 bit but destination might be 8 or 16 bit */
353
0
  bpp = 4 * (GST_VIDEO_FORMAT_INFO_BITS (dunpackinfo) / 8);
354
355
0
  global_alpha_val = (bpp == 4) ? 255.0 * global_alpha : 65535.0 * global_alpha;
356
357
0
  matrix = matrix_identity;
358
0
  if (GST_VIDEO_INFO_IS_RGB (&src->info) != GST_VIDEO_INFO_IS_RGB (&dest->info)) {
359
0
    if (GST_VIDEO_INFO_IS_RGB (&src->info)) {
360
0
      if (src_premultiplied_alpha) {
361
0
        matrix = matrix_prea_rgb_to_yuv;
362
0
        src_premultiplied_alpha = FALSE;
363
0
      } else {
364
0
        matrix = matrix_rgb_to_yuv;
365
0
      }
366
0
    } else {
367
0
      matrix = matrix_yuv_to_rgb;
368
0
    }
369
0
  }
370
371
  /* If we're here we know that the overlay image fully or
372
   * partially overlaps with the video frame */
373
374
  /* adjust src image for negative offsets */
375
0
  if (x < 0) {
376
0
    src_xoff = -x;
377
0
    src_width -= src_xoff;
378
0
    x = 0;
379
0
  }
380
381
0
  if (y < 0) {
382
0
    src_yoff = -y;
383
0
    src_height -= src_yoff;
384
0
    y = 0;
385
0
  }
386
387
  /* adjust width/height to render (i.e. clip source image) if the source
388
   * image extends beyond the right or bottom border of the video surface */
389
0
  if (x + src_width > dest_width)
390
0
    src_width = dest_width - x;
391
392
0
  if (y + src_height > dest_height)
393
0
    src_height = dest_height - y;
394
395
0
  tmpsrcline = g_malloc (sizeof (guint8) * (src_width + 8) * 4);
396
0
  tmpdestline = g_malloc (sizeof (guint8) * (dest_width + 8) * bpp);
397
398
  /* Mainloop doing the needed conversions, and blending */
399
0
  for (i = y; i < y + src_height; i++, src_yoff++) {
400
401
0
    dinfo->unpack_func (dinfo, 0, tmpdestline, dest->data, dest->info.stride,
402
0
        0, i, dest_width);
403
0
    sinfo->unpack_func (sinfo, 0, tmpsrcline, src->data, src->info.stride,
404
0
        src_xoff, src_yoff, src_width);
405
406
    /* FIXME: use the x parameter of the unpack func once implemented */
407
0
    tmpdestline += bpp * x;
408
409
0
    matrix (tmpsrcline, src_width);
410
411
0
#define BLENDLOOP(op, dest_type, max, shift, alpha_val)                                       \
412
0
  G_STMT_START {                                                                              \
413
0
    for (j = 0; j < src_width * 4; j += 4) {                                                  \
414
0
      guint asrc, adst;                                                                       \
415
0
      guint final_alpha;                                                                      \
416
0
      dest_type * dest = (dest_type *) tmpdestline;                                           \
417
0
                                                                                              \
418
0
      asrc = ((guint) tmpsrcline[j]) * alpha_val / max;                                       \
419
0
      asrc = asrc << shift;                                                                   \
420
0
      if (asrc == 0)                                                                          \
421
0
        continue;                                                                             \
422
0
                                                                                              \
423
0
      adst = dest[j];                                                                         \
424
0
      final_alpha = asrc + adst * (max - asrc) / max;                                         \
425
0
      dest[j] = final_alpha;                                                                  \
426
0
      if (final_alpha == 0)                                                                   \
427
0
        final_alpha = 1;                                                                      \
428
0
                                                                                              \
429
0
      BLENDC (op, max, alpha_val, asrc, tmpsrcline[j + 1] << shift, adst, dest[j + 1], final_alpha); \
430
0
      BLENDC (op, max, alpha_val, asrc, tmpsrcline[j + 2] << shift, adst, dest[j + 2], final_alpha); \
431
0
      BLENDC (op, max, alpha_val, asrc, tmpsrcline[j + 3] << shift, adst, dest[j + 3], final_alpha); \
432
0
    }                                                                                         \
433
0
  } G_STMT_END
434
435
0
    if (bpp == 4) {
436
0
      if (G_LIKELY (global_alpha_val == 255)) {
437
0
        if (src_premultiplied_alpha && dest_premultiplied_alpha) {
438
0
          BLENDLOOP (OVER11, guint8, 255, 0, 255);
439
0
        } else if (!src_premultiplied_alpha && dest_premultiplied_alpha) {
440
0
          BLENDLOOP (OVER01, guint8, 255, 0, 255);
441
0
        } else if (src_premultiplied_alpha && !dest_premultiplied_alpha) {
442
0
          BLENDLOOP (OVER10_8BIT, guint8, 255, 0, 255);
443
0
        } else {
444
0
          BLENDLOOP (OVER00_8BIT, guint8, 255, 0, 255);
445
0
        }
446
0
      } else {
447
0
        if (src_premultiplied_alpha && dest_premultiplied_alpha) {
448
0
          BLENDLOOP (OVER11, guint8, 255, 0, global_alpha_val);
449
0
        } else if (!src_premultiplied_alpha && dest_premultiplied_alpha) {
450
0
          BLENDLOOP (OVER01, guint8, 255, 0, global_alpha_val);
451
0
        } else if (src_premultiplied_alpha && !dest_premultiplied_alpha) {
452
0
          BLENDLOOP (OVER10_8BIT, guint8, 255, 0, global_alpha_val);
453
0
        } else {
454
0
          BLENDLOOP (OVER00_8BIT, guint8, 255, 0, global_alpha_val);
455
0
        }
456
0
      }
457
0
    } else {
458
0
      g_assert (bpp == 8);
459
460
0
      if (G_LIKELY (global_alpha_val == 65535)) {
461
0
        if (src_premultiplied_alpha && dest_premultiplied_alpha) {
462
0
          BLENDLOOP (OVER11, guint16, 65535, 8, 65535);
463
0
        } else if (!src_premultiplied_alpha && dest_premultiplied_alpha) {
464
0
          BLENDLOOP (OVER01, guint16, 65535, 8, 65535);
465
0
        } else if (src_premultiplied_alpha && !dest_premultiplied_alpha) {
466
0
          BLENDLOOP (OVER10_16BIT, guint16, 65535, 8, 65535);
467
0
        } else {
468
0
          BLENDLOOP (OVER00_16BIT, guint16, 65535, 8, 65535);
469
0
        }
470
0
      } else {
471
0
        if (src_premultiplied_alpha && dest_premultiplied_alpha) {
472
0
          BLENDLOOP (OVER11, guint16, 65535, 8, global_alpha_val);
473
0
        } else if (!src_premultiplied_alpha && dest_premultiplied_alpha) {
474
0
          BLENDLOOP (OVER01, guint16, 65535, 8, global_alpha_val);
475
0
        } else if (src_premultiplied_alpha && !dest_premultiplied_alpha) {
476
0
          BLENDLOOP (OVER10_16BIT, guint16, 65535, 8, global_alpha_val);
477
0
        } else {
478
0
          BLENDLOOP (OVER00_16BIT, guint16, 65535, 8, global_alpha_val);
479
0
        }
480
0
      }
481
0
    }
482
483
0
#undef BLENDLOOP
484
485
    /* undo previous pointer adjustments to pass right pointer to g_free */
486
0
    tmpdestline -= bpp * x;
487
488
    /* FIXME
489
     * #if G_BYTE_ORDER == LITTLE_ENDIAN
490
     * video_orc_blend_little (tmpdestline, tmpsrcline, dest->width);
491
     * #else
492
     * video_orc_blend_big (tmpdestline, tmpsrcline, src->width);
493
     * #endif
494
     */
495
496
0
    dinfo->pack_func (dinfo, 0, tmpdestline, dest_width,
497
0
        dest->data, dest->info.stride, dest->info.chroma_site, i, dest_width);
498
0
  }
499
500
0
  g_free (tmpdestline);
501
0
  g_free (tmpsrcline);
502
503
0
  return TRUE;
504
505
0
failed:
506
0
  {
507
0
    GST_WARNING ("Could not do the blending");
508
0
    return FALSE;
509
0
  }
510
0
unpack_format_not_supported:
511
0
  {
512
0
    GST_FIXME ("video format %s not supported yet for blending",
513
0
        gst_video_format_to_string (dinfo->unpack_format));
514
0
    return FALSE;
515
0
  }
516
0
nothing_to_do:
517
0
  {
518
0
    GST_LOG
519
0
        ("Overlay completely outside the video surface, hence not rendering");
520
0
    return TRUE;
521
0
  }
522
0
}