Coverage Report

Created: 2026-10-12 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gstreamer/subprojects/gst-plugins-base/gst-libs/gst/video/video-overlay-composition.c
Line
Count
Source
1
/* GStreamer Video Overlay Composition
2
 * Copyright (C) 2011 Intel Corporation
3
 * Copyright (C) 2011 Collabora Ltd.
4
 * Copyright (C) 2011 Tim-Philipp Müller <tim centricular net>
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
/**
23
 * SECTION:gstvideooverlaycomposition
24
 * @title: GstVideoOverlayRectangle
25
 * @short_description: Video Buffer Overlay Compositions (Subtitles, Logos)
26
 *
27
 * Functions to create and handle overlay compositions on video buffers.
28
 *
29
 * An overlay composition describes one or more overlay rectangles to be
30
 * blended on top of a video buffer.
31
 *
32
 * This API serves two main purposes:
33
 *
34
 * * it can be used to attach overlay information (subtitles or logos)
35
 *   to non-raw video buffers such as GL/VAAPI/VDPAU surfaces. The actual
36
 *   blending of the overlay can then be done by e.g. the video sink that
37
 *   processes these non-raw buffers.
38
 *
39
 * * it can also be used to blend overlay rectangles on top of raw video
40
 *   buffers, thus consolidating blending functionality for raw video in
41
 *   one place.
42
 *
43
 * Together, this allows existing overlay elements to easily handle raw
44
 * and non-raw video as input in without major changes (once the overlays
45
 * have been put into a #GstVideoOverlayComposition object anyway) - for raw
46
 * video the overlay can just use the blending function to blend the data
47
 * on top of the video, and for surface buffers it can just attach them to
48
 * the buffer and let the sink render the overlays.
49
 *
50
 */
51
52
/* TODO:
53
 *  - provide accessors for seq_num and other fields (as needed)
54
 *  - allow overlay to set/get original pango markup string on/from rectangle
55
 */
56
57
#ifdef HAVE_CONFIG_H
58
#include "config.h"
59
#endif
60
61
#include "video-overlay-composition.h"
62
#include "video-blend.h"
63
#include "gstvideometa.h"
64
#include <string.h>
65
66
struct _GstVideoOverlayComposition
67
{
68
  GstMiniObject parent;
69
70
  guint num_rectangles;
71
  GstVideoOverlayRectangle **rectangles;
72
73
  /* lowest rectangle sequence number still used by the upstream
74
   * overlay element. This way a renderer maintaining some kind of
75
   * rectangles <-> surface cache can know when to free cached
76
   * surfaces/rectangles. */
77
  guint min_seq_num_used;
78
79
  /* sequence number for the composition (same series as rectangles) */
80
  guint seq_num;
81
};
82
83
struct _GstVideoOverlayRectangle
84
{
85
  GstMiniObject parent;
86
87
  /* Position on video frame and dimension of output rectangle in
88
   * output frame terms (already adjusted for the PAR of the output
89
   * frame). x/y can be negative (overlay will be clipped then) */
90
  gint x, y;
91
  guint render_width, render_height;
92
93
  /* Info on overlay pixels (format, width, height) */
94
  GstVideoInfo info;
95
96
  /* The flags associated to this rectangle */
97
  GstVideoOverlayFormatFlags flags;
98
99
  /* Refcounted blob of memory, no caps or timestamps */
100
  GstBuffer *pixels;
101
102
  /* FIXME: how to express source like text or pango markup?
103
   *        (just add source type enum + source buffer with data)
104
   *
105
   * FOR 0.10: always send pixel blobs, but attach source data in
106
   * addition (reason: if downstream changes, we can't renegotiate
107
   * that properly, if we just do a query of supported formats from
108
   * the start). Sink will just ignore pixels and use pango markup
109
   * from source data if it supports that.
110
   *
111
   * FOR 0.11: overlay should query formats (pango markup, pixels)
112
   * supported by downstream and then only send that. We can
113
   * renegotiate via the reconfigure event.
114
   */
115
116
  /* sequence number: useful for backends/renderers/sinks that want
117
   * to maintain a cache of rectangles <-> surfaces. The value of
118
   * the min_seq_num_used in the composition tells the renderer which
119
   * rectangles have expired. */
120
  guint seq_num;
121
122
  /* global alpha: global alpha value of the rectangle. Each each per-pixel
123
   * alpha value of image-data will be multiplied with the global alpha value
124
   * during blending.
125
   * Can be used for efficient fading in/out of overlay rectangles.
126
   * GstElements that render OverlayCompositions and don't support global alpha
127
   * should simply ignore it.*/
128
  gfloat global_alpha;
129
130
  /* track alpha-values already applied: */
131
  gfloat applied_global_alpha;
132
  /* store initial per-pixel alpha values: */
133
  guint8 *initial_alpha;
134
135
  /* FIXME: we may also need a (private) way to cache converted/scaled
136
   * pixel blobs */
137
  GMutex lock;
138
139
  GList *scaled_rectangles;
140
};
141
142
0
#define GST_RECTANGLE_LOCK(rect)   g_mutex_lock(&rect->lock)
143
0
#define GST_RECTANGLE_UNLOCK(rect) g_mutex_unlock(&rect->lock)
144
145
/* --------------------------- utility functions --------------------------- */
146
147
#ifndef GST_DISABLE_GST_DEBUG
148
149
#define GST_CAT_DEFAULT ensure_debug_category()
150
151
static GstDebugCategory *
152
ensure_debug_category (void)
153
0
{
154
0
  static gsize cat_gonce = 0;
155
156
0
  if (g_once_init_enter (&cat_gonce)) {
157
0
    gsize cat_done;
158
159
0
    cat_done = (gsize) _gst_debug_category_new ("video-composition", 0,
160
0
        "video overlay composition");
161
162
0
    g_once_init_leave (&cat_gonce, cat_done);
163
0
  }
164
165
0
  return (GstDebugCategory *) cat_gonce;
166
0
}
167
168
#else
169
170
#define ensure_debug_category() /* NOOP */
171
172
#endif /* GST_DISABLE_GST_DEBUG */
173
174
static guint
175
gst_video_overlay_get_seqnum (void)
176
0
{
177
0
  static gint seqnum;           /* 0 */
178
179
0
  return (guint) g_atomic_int_add (&seqnum, 1);
180
0
}
181
182
static gboolean
183
gst_video_overlay_composition_meta_init (GstMeta * meta, gpointer params,
184
    GstBuffer * buf)
185
0
{
186
0
  GstVideoOverlayCompositionMeta *ometa;
187
188
0
  ometa = (GstVideoOverlayCompositionMeta *) meta;
189
190
0
  ometa->overlay = NULL;
191
192
0
  return TRUE;
193
0
}
194
195
static void
196
gst_video_overlay_composition_meta_free (GstMeta * meta, GstBuffer * buf)
197
0
{
198
0
  GstVideoOverlayCompositionMeta *ometa;
199
200
0
  ometa = (GstVideoOverlayCompositionMeta *) meta;
201
202
0
  if (ometa->overlay)
203
0
    gst_video_overlay_composition_unref (ometa->overlay);
204
0
}
205
206
static gboolean
207
gst_video_overlay_composition_meta_transform (GstBuffer * dest, GstMeta * meta,
208
    GstBuffer * buffer, GQuark type, gpointer data)
209
0
{
210
0
  GstVideoOverlayCompositionMeta *dmeta = NULL, *smeta;
211
212
0
  smeta = (GstVideoOverlayCompositionMeta *) meta;
213
214
0
  if (GST_META_TRANSFORM_IS_COPY (type)) {
215
0
    GST_DEBUG ("copy video overlay composition metadata");
216
217
0
    dmeta =
218
0
        (GstVideoOverlayCompositionMeta *) gst_buffer_add_meta (dest,
219
0
        GST_VIDEO_OVERLAY_COMPOSITION_META_INFO, NULL);
220
0
    if (!dmeta)
221
0
      return FALSE;
222
223
0
    dmeta->overlay = gst_video_overlay_composition_ref (smeta->overlay);
224
0
  } else if (GST_VIDEO_META_TRANSFORM_IS_MATRIX (type)) {
225
0
    GstVideoMetaTransformMatrix *trans = data;
226
0
    GstVideoOverlayComposition *new_comp;
227
0
    guint n_rectangles;
228
0
    gboolean have_rect = FALSE;
229
230
0
    new_comp = gst_video_overlay_composition_new (NULL);
231
0
    n_rectangles = gst_video_overlay_composition_n_rectangles (smeta->overlay);
232
0
    for (guint i = 0; i < n_rectangles; i++) {
233
0
      GstVideoOverlayRectangle *rect =
234
0
          gst_video_overlay_composition_get_rectangle (smeta->overlay, i);
235
0
      GstVideoOverlayRectangle *new_rect =
236
0
          gst_video_overlay_rectangle_copy (rect);
237
0
      GstVideoRectangle render_rect;
238
0
      guint width = G_MAXINT;
239
0
      guint height = G_MAXINT;
240
241
0
      gst_video_overlay_rectangle_get_render_rectangle (rect, &render_rect.x,
242
0
          &render_rect.y, &width, &height);
243
0
      render_rect.w = MIN (width, G_MAXINT);
244
0
      render_rect.h = MIN (height, G_MAXINT);
245
246
0
      GstVideoRectangle orig_render_rect = render_rect;
247
248
0
      if (gst_video_meta_transform_matrix_rectangle (trans, &render_rect)) {
249
250
0
        GST_DEBUG ("overlay rectangle %u (seqnum: %d) (%dx%d)x(%dx%d)"
251
0
            " -> (%dx%d)->(%dx%d)", i,
252
0
            gst_video_overlay_rectangle_get_seqnum (rect), orig_render_rect.x,
253
0
            orig_render_rect.y, orig_render_rect.w, orig_render_rect.h,
254
0
            render_rect.x, render_rect.y, render_rect.w, render_rect.h);
255
256
0
        gst_video_overlay_rectangle_set_render_rectangle (new_rect,
257
0
            render_rect.x, render_rect.y, render_rect.w, render_rect.h);
258
0
        gst_video_overlay_composition_add_rectangle (new_comp, new_rect);
259
0
        have_rect = TRUE;
260
0
      }
261
0
      gst_video_overlay_rectangle_unref (new_rect);
262
0
    }
263
264
0
    if (have_rect)
265
0
      dmeta = gst_buffer_add_video_overlay_composition_meta (dest, new_comp);
266
0
    gst_video_overlay_composition_unref (new_comp);
267
0
    if (!have_rect || !dmeta)
268
0
      return FALSE;
269
0
  } else if (GST_VIDEO_META_TRANSFORM_IS_SCALE (type)) {
270
0
    GstVideoMetaTransform *trans = data;
271
0
    GstVideoOverlayComposition *new_comp;
272
0
    gint ow, oh, nw, nh;
273
0
    guint n_rectangles;
274
275
0
    ow = GST_VIDEO_INFO_WIDTH (trans->in_info);
276
0
    nw = GST_VIDEO_INFO_WIDTH (trans->out_info);
277
0
    oh = GST_VIDEO_INFO_HEIGHT (trans->in_info);
278
0
    nh = GST_VIDEO_INFO_HEIGHT (trans->out_info);
279
280
0
    GST_DEBUG ("scaling video overlay composition metadata %dx%d -> %dx%d", ow,
281
0
        oh, nw, nh);
282
283
0
    new_comp = gst_video_overlay_composition_new (NULL);
284
0
    n_rectangles = gst_video_overlay_composition_n_rectangles (smeta->overlay);
285
0
    for (guint i = 0; i < n_rectangles; i++) {
286
0
      GstVideoOverlayRectangle *rect =
287
0
          gst_video_overlay_composition_get_rectangle (smeta->overlay, i);
288
0
      GstVideoOverlayRectangle *new_rect =
289
0
          gst_video_overlay_rectangle_copy (rect);
290
0
      gint render_x = 0, render_y = 0;
291
0
      gint new_render_x, new_render_y;
292
0
      guint render_width = 0, render_height = 0;
293
0
      guint new_render_width, new_render_height;
294
295
0
      gst_video_overlay_rectangle_get_render_rectangle (rect, &render_x,
296
0
          &render_y, &render_width, &render_height);
297
298
0
      new_render_x = (render_x * nw) / ow;
299
0
      new_render_y = (render_y * nh) / oh;
300
0
      new_render_width = (render_width * nw) / ow;
301
0
      new_render_height = (render_height * nh) / oh;
302
303
0
      GST_DEBUG
304
0
          ("overlay rectangle %u (seqnum: %d) (%dx%d)x(%ux%u) -> (%dx%d)->(%ux%u)",
305
0
          i, gst_video_overlay_rectangle_get_seqnum (rect), render_x, render_y,
306
0
          render_width, render_height, new_render_x, new_render_y,
307
0
          new_render_width, new_render_height);
308
309
0
      gst_video_overlay_rectangle_set_render_rectangle (new_rect,
310
0
          new_render_x, new_render_y, new_render_width, new_render_height);
311
0
      gst_video_overlay_composition_add_rectangle (new_comp, new_rect);
312
0
      gst_video_overlay_rectangle_unref (new_rect);
313
0
    }
314
315
0
    dmeta = gst_buffer_add_video_overlay_composition_meta (dest, new_comp);
316
0
    gst_video_overlay_composition_unref (new_comp);
317
0
    if (!dmeta)
318
0
      return FALSE;
319
0
  } else {
320
    /* return FALSE, if transform type is not supported */
321
0
    return FALSE;
322
0
  }
323
0
  return TRUE;
324
0
}
325
326
GType
327
gst_video_overlay_composition_meta_api_get_type (void)
328
0
{
329
0
  static GType type = 0;
330
0
  static const gchar *tags[] =
331
0
      { GST_META_TAG_VIDEO_STR, GST_META_TAG_VIDEO_ORIENTATION_STR,
332
0
    GST_META_TAG_VIDEO_SIZE_STR, NULL
333
0
  };
334
335
0
  if (g_once_init_enter (&type)) {
336
0
    GType _type =
337
0
        gst_meta_api_type_register ("GstVideoOverlayCompositionMetaAPI", tags);
338
0
    g_once_init_leave (&type, _type);
339
0
  }
340
0
  return type;
341
0
}
342
343
/* video overlay composition metadata */
344
const GstMetaInfo *
345
gst_video_overlay_composition_meta_get_info (void)
346
0
{
347
0
  static const GstMetaInfo *video_overlay_composition_meta_info = NULL;
348
349
0
  if (g_once_init_enter ((GstMetaInfo **) &
350
0
          video_overlay_composition_meta_info)) {
351
0
    const GstMetaInfo *meta =
352
0
        gst_meta_register (GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE,
353
0
        "GstVideoOverlayCompositionMeta",
354
0
        sizeof (GstVideoOverlayCompositionMeta),
355
0
        (GstMetaInitFunction) gst_video_overlay_composition_meta_init,
356
0
        (GstMetaFreeFunction) gst_video_overlay_composition_meta_free,
357
0
        (GstMetaTransformFunction)
358
0
        gst_video_overlay_composition_meta_transform);
359
0
    g_once_init_leave ((GstMetaInfo **) & video_overlay_composition_meta_info,
360
0
        (GstMetaInfo *) meta);
361
0
  }
362
0
  return video_overlay_composition_meta_info;
363
0
}
364
365
/**
366
 * gst_buffer_add_video_overlay_composition_meta:
367
 * @buf: a #GstBuffer
368
 * @comp: (allow-none): a #GstVideoOverlayComposition
369
 *
370
 * Sets an overlay composition on a buffer. The buffer will obtain its own
371
 * reference to the composition, meaning this function does not take ownership
372
 * of @comp.
373
 *
374
 * Returns: (transfer none): a #GstVideoOverlayCompositionMeta
375
 */
376
GstVideoOverlayCompositionMeta *
377
gst_buffer_add_video_overlay_composition_meta (GstBuffer * buf,
378
    GstVideoOverlayComposition * comp)
379
0
{
380
0
  GstVideoOverlayCompositionMeta *ometa;
381
382
0
  g_return_val_if_fail (gst_buffer_is_writable (buf), NULL);
383
384
0
  ometa = (GstVideoOverlayCompositionMeta *)
385
0
      gst_buffer_add_meta (buf, GST_VIDEO_OVERLAY_COMPOSITION_META_INFO, NULL);
386
387
0
  ometa->overlay = gst_video_overlay_composition_ref (comp);
388
389
0
  return ometa;
390
0
}
391
392
/* ------------------------------ composition ------------------------------ */
393
394
0
#define RECTANGLE_ARRAY_STEP 4  /* premature optimization */
395
396
11
GST_DEFINE_MINI_OBJECT_TYPE (GstVideoOverlayComposition,
397
11
    gst_video_overlay_composition);
398
11
399
11
static void
400
11
gst_video_overlay_composition_free (GstMiniObject * mini_obj)
401
11
{
402
0
  GstVideoOverlayComposition *comp = (GstVideoOverlayComposition *) mini_obj;
403
0
  guint num;
404
405
0
  num = comp->num_rectangles;
406
407
0
  while (num > 0) {
408
0
    gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (comp->rectangles[num -
409
0
                1]), GST_MINI_OBJECT_CAST (comp));
410
0
    gst_video_overlay_rectangle_unref (comp->rectangles[num - 1]);
411
0
    --num;
412
0
  }
413
414
0
  g_free (comp->rectangles);
415
0
  comp->rectangles = NULL;
416
0
  comp->num_rectangles = 0;
417
418
0
  g_free (comp);
419
0
}
420
421
/**
422
 * gst_video_overlay_composition_new:
423
 * @rectangle: (transfer none) (nullable): a #GstVideoOverlayRectangle to add to the
424
 *     composition
425
 *
426
 * Creates a new video overlay composition object to hold one or more
427
 * overlay rectangles.
428
 *
429
 * Note that since 1.20 this allows to pass %NULL for @rectangle.
430
 *
431
 * Returns: (transfer full): a new #GstVideoOverlayComposition. Unref with
432
 *     gst_video_overlay_composition_unref() when no longer needed.
433
 */
434
GstVideoOverlayComposition *
435
gst_video_overlay_composition_new (GstVideoOverlayRectangle * rectangle)
436
0
{
437
0
  GstVideoOverlayComposition *comp;
438
439
0
  g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle)
440
0
      || rectangle == NULL, NULL);
441
442
0
  comp = g_new0 (GstVideoOverlayComposition, 1);
443
444
0
  gst_mini_object_init (GST_MINI_OBJECT_CAST (comp), 0,
445
0
      GST_TYPE_VIDEO_OVERLAY_COMPOSITION,
446
0
      (GstMiniObjectCopyFunction) gst_video_overlay_composition_copy,
447
0
      NULL, (GstMiniObjectFreeFunction) gst_video_overlay_composition_free);
448
449
0
  comp->rectangles = g_new0 (GstVideoOverlayRectangle *, RECTANGLE_ARRAY_STEP);
450
451
0
  comp->seq_num = gst_video_overlay_get_seqnum ();
452
0
  comp->min_seq_num_used = comp->seq_num;
453
454
0
  GST_LOG ("new composition %p: seq_num %u", comp, comp->seq_num);
455
456
0
  if (rectangle) {
457
    /* since the rectangle was created earlier, its seqnum is smaller than ours */
458
0
    comp->min_seq_num_used = rectangle->seq_num;
459
0
    gst_video_overlay_composition_add_rectangle (comp, rectangle);
460
0
  }
461
462
0
  return comp;
463
0
}
464
465
/**
466
 * gst_video_overlay_composition_add_rectangle:
467
 * @comp: a #GstVideoOverlayComposition
468
 * @rectangle: (transfer none): a #GstVideoOverlayRectangle to add to the
469
 *     composition
470
 *
471
 * Adds an overlay rectangle to an existing overlay composition object. This
472
 * must be done right after creating the overlay composition.
473
 */
474
void
475
gst_video_overlay_composition_add_rectangle (GstVideoOverlayComposition * comp,
476
    GstVideoOverlayRectangle * rectangle)
477
0
{
478
0
  g_return_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp));
479
0
  g_return_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle));
480
0
  g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (comp)));
481
482
0
  if (comp->num_rectangles % RECTANGLE_ARRAY_STEP == 0) {
483
0
    comp->rectangles =
484
0
        g_renew (GstVideoOverlayRectangle *, comp->rectangles,
485
0
        comp->num_rectangles + RECTANGLE_ARRAY_STEP);
486
0
  }
487
488
0
  comp->rectangles[comp->num_rectangles] =
489
0
      gst_video_overlay_rectangle_ref (rectangle);
490
0
  gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (rectangle),
491
0
      GST_MINI_OBJECT_CAST (comp));
492
0
  comp->num_rectangles += 1;
493
494
0
  comp->min_seq_num_used = MIN (comp->min_seq_num_used, rectangle->seq_num);
495
496
0
  GST_LOG ("composition %p: added rectangle %p", comp, rectangle);
497
0
}
498
499
/**
500
 * gst_video_overlay_composition_n_rectangles:
501
 * @comp: a #GstVideoOverlayComposition
502
 *
503
 * Returns the number of #GstVideoOverlayRectangle<!-- -->s contained in @comp.
504
 *
505
 * Returns: the number of rectangles
506
 */
507
guint
508
gst_video_overlay_composition_n_rectangles (GstVideoOverlayComposition * comp)
509
0
{
510
0
  g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), 0);
511
512
0
  return comp->num_rectangles;
513
0
}
514
515
/**
516
 * gst_video_overlay_composition_get_rectangle:
517
 * @comp: a #GstVideoOverlayComposition
518
 * @n: number of the rectangle to get
519
 *
520
 * Returns the @n-th #GstVideoOverlayRectangle contained in @comp.
521
 *
522
 * Returns: (transfer none) (nullable): the @n-th rectangle, or NULL if @n is out of
523
 *     bounds. Will not return a new reference, the caller will need to
524
 *     obtain her own reference using gst_video_overlay_rectangle_ref()
525
 *     if needed.
526
 */
527
GstVideoOverlayRectangle *
528
gst_video_overlay_composition_get_rectangle (GstVideoOverlayComposition * comp,
529
    guint n)
530
0
{
531
0
  g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), NULL);
532
533
0
  if (n >= comp->num_rectangles)
534
0
    return NULL;
535
536
0
  return comp->rectangles[n];
537
0
}
538
539
static gboolean
540
gst_video_overlay_rectangle_needs_scaling (GstVideoOverlayRectangle * r)
541
0
{
542
0
  return (GST_VIDEO_INFO_WIDTH (&r->info) != r->render_width ||
543
0
      GST_VIDEO_INFO_HEIGHT (&r->info) != r->render_height);
544
0
}
545
546
/**
547
 * gst_video_overlay_composition_blend:
548
 * @comp: a #GstVideoOverlayComposition
549
 * @video_buf: a #GstVideoFrame containing raw video data in a
550
 *             supported format. It should be mapped using GST_MAP_READWRITE
551
 *
552
 * Blends the overlay rectangles in @comp on top of the raw video data
553
 * contained in @video_buf. The data in @video_buf must be writable and
554
 * mapped appropriately.
555
 *
556
 * Since @video_buf data is read and will be modified, it ought be
557
 * mapped with flag GST_MAP_READWRITE.
558
 */
559
/* FIXME: formats with more than 8 bit per component which get unpacked into
560
 * ARGB64 or AYUV64 (such as v210, v216, UYVP, GRAY16_LE and GRAY16_BE)
561
 * are not supported yet by the code in video-blend.c.
562
 */
563
gboolean
564
gst_video_overlay_composition_blend (GstVideoOverlayComposition * comp,
565
    GstVideoFrame * video_buf)
566
0
{
567
0
  GstVideoInfo scaled_info;
568
0
  GstVideoInfo *vinfo;
569
0
  GstVideoFrame rectangle_frame;
570
0
  GstVideoFormat fmt;
571
0
  GstBuffer *pixels = NULL;
572
0
  gboolean ret = TRUE;
573
0
  guint n, num;
574
0
  int w, h;
575
576
0
  g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), FALSE);
577
0
  g_return_val_if_fail (video_buf != NULL, FALSE);
578
579
0
  w = GST_VIDEO_FRAME_WIDTH (video_buf);
580
0
  h = GST_VIDEO_FRAME_HEIGHT (video_buf);
581
0
  fmt = GST_VIDEO_FRAME_FORMAT (video_buf);
582
583
0
  num = comp->num_rectangles;
584
0
  GST_LOG ("Blending composition %p with %u rectangles onto video buffer %p "
585
0
      "(%ux%u, format %u)", comp, num, video_buf, w, h, fmt);
586
587
0
  for (n = 0; n < num; ++n) {
588
0
    GstVideoOverlayRectangle *rect;
589
0
    gboolean needs_scaling;
590
591
0
    rect = comp->rectangles[n];
592
593
0
    GST_LOG (" rectangle %u %p: %ux%u, format %u", n, rect,
594
0
        GST_VIDEO_INFO_WIDTH (&rect->info), GST_VIDEO_INFO_HEIGHT (&rect->info),
595
0
        GST_VIDEO_INFO_FORMAT (&rect->info));
596
597
0
    needs_scaling = gst_video_overlay_rectangle_needs_scaling (rect);
598
0
    if (needs_scaling) {
599
0
      gst_video_blend_scale_linear_RGBA (&rect->info, rect->pixels,
600
0
          rect->render_height, rect->render_width, &scaled_info, &pixels);
601
602
0
      if (!pixels) {
603
0
        GST_WARNING ("Couldn't get scaled rectangle %u", n);
604
0
        ret = FALSE;
605
0
        continue;
606
0
      }
607
608
0
      vinfo = &scaled_info;
609
0
    } else {
610
0
      pixels = gst_buffer_ref (rect->pixels);
611
0
      vinfo = &rect->info;
612
0
    }
613
614
0
    gst_video_frame_map (&rectangle_frame, vinfo, pixels, GST_MAP_READ);
615
616
0
    ret = gst_video_blend (video_buf, &rectangle_frame, rect->x, rect->y,
617
0
        rect->global_alpha);
618
0
    gst_video_frame_unmap (&rectangle_frame);
619
0
    if (!ret) {
620
0
      GST_WARNING ("Could not blend overlay rectangle onto video buffer");
621
0
    }
622
623
    /* FIXME: should cache scaled pixels in the rectangle struct */
624
0
    gst_buffer_unref (pixels);
625
0
  }
626
627
0
  return ret;
628
0
}
629
630
/**
631
 * gst_video_overlay_composition_copy:
632
 * @comp: (transfer none): a #GstVideoOverlayComposition to copy
633
 *
634
 * Makes a copy of @comp and all contained rectangles, so that it is possible
635
 * to modify the composition and contained rectangles (e.g. add additional
636
 * rectangles or change the render co-ordinates or render dimension). The
637
 * actual overlay pixel data buffers contained in the rectangles are not
638
 * copied.
639
 *
640
 * This should be avoided unless rectangles need to be modified because it
641
 * invalidates caching in sinks and compositor elements. To add extra rectangles
642
 * it is rather recommended to add an extra composition meta using
643
 * gst_buffer_add_video_overlay_composition_meta().
644
 *
645
 * Returns: (transfer full): a new #GstVideoOverlayComposition equivalent
646
 *     to @comp.
647
 */
648
GstVideoOverlayComposition *
649
gst_video_overlay_composition_copy (GstVideoOverlayComposition * comp)
650
0
{
651
0
  GstVideoOverlayComposition *copy;
652
0
  GstVideoOverlayRectangle *rect;
653
0
  guint n;
654
655
0
  g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), NULL);
656
657
0
  if (G_LIKELY (comp->num_rectangles == 0))
658
0
    return gst_video_overlay_composition_new (NULL);
659
660
0
  rect = gst_video_overlay_rectangle_copy (comp->rectangles[0]);
661
0
  copy = gst_video_overlay_composition_new (rect);
662
0
  gst_video_overlay_rectangle_unref (rect);
663
664
0
  for (n = 1; n < comp->num_rectangles; ++n) {
665
0
    rect = gst_video_overlay_rectangle_copy (comp->rectangles[n]);
666
0
    gst_video_overlay_composition_add_rectangle (copy, rect);
667
0
    gst_video_overlay_rectangle_unref (rect);
668
0
  }
669
670
0
  return copy;
671
0
}
672
673
/**
674
 * gst_video_overlay_composition_make_writable:
675
 * @comp: (transfer full): a #GstVideoOverlayComposition to copy
676
 *
677
 * Takes ownership of @comp and returns a version of @comp that is writable
678
 * (i.e. can be modified). Will either return @comp right away, or create a
679
 * new writable copy of @comp and unref @comp itself. All the contained
680
 * rectangles will also be copied, but the actual overlay pixel data buffers
681
 * contained in the rectangles are not copied.
682
 *
683
 * This should be avoided unless rectangles need to be modified because it
684
 * invalidates caching in sinks and compositor elements. To add extra rectangles
685
 * it is rather recommended to add an extra composition meta using
686
 * gst_buffer_add_video_overlay_composition_meta().
687
 *
688
 * Returns: (transfer full): a writable #GstVideoOverlayComposition
689
 *     equivalent to @comp.
690
 */
691
GstVideoOverlayComposition *
692
gst_video_overlay_composition_make_writable (GstVideoOverlayComposition * comp)
693
0
{
694
0
  GstVideoOverlayComposition *writable_comp;
695
696
0
  g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), NULL);
697
698
0
  if (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (comp))) {
699
0
    guint n;
700
701
0
    for (n = 0; n < comp->num_rectangles; ++n) {
702
0
      if (!gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (comp->rectangles
703
0
                  [n])))
704
0
        goto copy;
705
0
    }
706
0
    return comp;
707
0
  }
708
709
0
copy:
710
711
0
  writable_comp = gst_video_overlay_composition_copy (comp);
712
0
  gst_video_overlay_composition_unref (comp);
713
714
0
  return writable_comp;
715
0
}
716
717
/**
718
 * gst_video_overlay_composition_get_seqnum:
719
 * @comp: a #GstVideoOverlayComposition
720
 *
721
 * Returns the sequence number of this composition. Sequence numbers are
722
 * monotonically increasing and unique for overlay compositions and rectangles
723
 * (meaning there will never be a rectangle with the same sequence number as
724
 * a composition).
725
 *
726
 * Returns: the sequence number of @comp
727
 */
728
guint
729
gst_video_overlay_composition_get_seqnum (GstVideoOverlayComposition * comp)
730
0
{
731
0
  g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), 0);
732
733
0
  return comp->seq_num;
734
0
}
735
736
/* ------------------------------ rectangles ------------------------------ -*/
737
738
0
GST_DEFINE_MINI_OBJECT_TYPE (GstVideoOverlayRectangle,
739
0
    gst_video_overlay_rectangle);
740
0
741
0
static void
742
0
gst_video_overlay_rectangle_free (GstMiniObject * mini_obj)
743
0
{
744
0
  GstVideoOverlayRectangle *rect = (GstVideoOverlayRectangle *) mini_obj;
745
746
0
  gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (rect->pixels),
747
0
      GST_MINI_OBJECT_CAST (rect));
748
0
  gst_buffer_replace (&rect->pixels, NULL);
749
750
0
  while (rect->scaled_rectangles != NULL) {
751
0
    GstVideoOverlayRectangle *scaled_rect = rect->scaled_rectangles->data;
752
753
0
    gst_video_overlay_rectangle_unref (scaled_rect);
754
755
0
    rect->scaled_rectangles =
756
0
        g_list_delete_link (rect->scaled_rectangles, rect->scaled_rectangles);
757
0
  }
758
759
0
  g_free (rect->initial_alpha);
760
0
  g_mutex_clear (&rect->lock);
761
762
0
  g_free (rect);
763
0
}
764
765
#ifndef G_DISABLE_CHECKS
766
static inline gboolean
767
gst_video_overlay_rectangle_check_flags (GstVideoOverlayFormatFlags flags)
768
0
{
769
  /* Check flags only contains flags we know about */
770
0
  return (flags & ~(GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA |
771
0
          GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA)) == 0;
772
0
}
773
#endif
774
775
static gboolean
776
gst_video_overlay_rectangle_is_same_alpha_type (GstVideoOverlayFormatFlags
777
    flags1, GstVideoOverlayFormatFlags flags2)
778
0
{
779
0
  return ((flags1 ^ flags2) & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA)
780
0
      == 0;
781
0
}
782
783
784
/**
785
 * gst_video_overlay_rectangle_new_raw:
786
 * @pixels: (transfer none): a #GstBuffer pointing to the pixel memory
787
 * @render_x: the X co-ordinate on the video where the top-left corner of this
788
 *     overlay rectangle should be rendered to
789
 * @render_y: the Y co-ordinate on the video where the top-left corner of this
790
 *     overlay rectangle should be rendered to
791
 * @render_width: the render width of this rectangle on the video
792
 * @render_height: the render height of this rectangle on the video
793
 * @flags: flags
794
 *
795
 * Creates a new video overlay rectangle with ARGB or AYUV pixel data.
796
 * The layout in case of ARGB of the components in memory is B-G-R-A
797
 * on little-endian platforms
798
 * (corresponding to #GST_VIDEO_FORMAT_BGRA) and A-R-G-B on big-endian
799
 * platforms (corresponding to #GST_VIDEO_FORMAT_ARGB). In other words,
800
 * pixels are treated as 32-bit words and the lowest 8 bits then contain
801
 * the blue component value and the highest 8 bits contain the alpha
802
 * component value. Unless specified in the flags, the RGB values are
803
 * non-premultiplied. This is the format that is used by most hardware,
804
 * and also many rendering libraries such as Cairo, for example.
805
 * The pixel data buffer must have #GstVideoMeta set.
806
 *
807
 * Returns: (transfer full): a new #GstVideoOverlayRectangle. Unref with
808
 *     gst_video_overlay_rectangle_unref() when no longer needed.
809
 */
810
GstVideoOverlayRectangle *
811
gst_video_overlay_rectangle_new_raw (GstBuffer * pixels,
812
    gint render_x, gint render_y, guint render_width, guint render_height,
813
    GstVideoOverlayFormatFlags flags)
814
0
{
815
0
  GstVideoOverlayRectangle *rect;
816
0
  GstVideoMeta *vmeta;
817
0
  GstVideoFormat format;
818
0
  guint width, height;
819
820
0
  g_return_val_if_fail (GST_IS_BUFFER (pixels), NULL);
821
0
  g_return_val_if_fail (render_height > 0 && render_width > 0, NULL);
822
0
  g_return_val_if_fail (gst_video_overlay_rectangle_check_flags (flags), NULL);
823
824
  /* buffer must have video meta with some expected settings */
825
0
  vmeta = gst_buffer_get_video_meta (pixels);
826
0
  g_return_val_if_fail (vmeta, NULL);
827
0
  g_return_val_if_fail (vmeta->format ==
828
0
      GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB ||
829
0
      vmeta->format == GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV, NULL);
830
0
  g_return_val_if_fail (vmeta->flags == GST_VIDEO_FRAME_FLAG_NONE, NULL);
831
832
0
  format = vmeta->format;
833
0
  width = vmeta->width;
834
0
  height = vmeta->height;
835
836
  /* technically ((height-1)*stride)+width might be okay too */
837
0
  g_return_val_if_fail (gst_buffer_get_size (pixels) >= height * width * 4,
838
0
      NULL);
839
0
  g_return_val_if_fail (height > 0 && width > 0, NULL);
840
841
0
  rect = g_new0 (GstVideoOverlayRectangle, 1);
842
843
0
  gst_mini_object_init (GST_MINI_OBJECT_CAST (rect), 0,
844
0
      GST_TYPE_VIDEO_OVERLAY_RECTANGLE,
845
0
      (GstMiniObjectCopyFunction) gst_video_overlay_rectangle_copy,
846
0
      NULL, (GstMiniObjectFreeFunction) gst_video_overlay_rectangle_free);
847
848
0
  g_mutex_init (&rect->lock);
849
850
0
  rect->pixels = gst_buffer_ref (pixels);
851
0
  gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (pixels),
852
0
      GST_MINI_OBJECT_CAST (rect));
853
0
  rect->scaled_rectangles = NULL;
854
855
0
  gst_video_info_init (&rect->info);
856
0
  if (!gst_video_info_set_format (&rect->info, format, width, height)) {
857
0
    gst_mini_object_unref (GST_MINI_OBJECT_CAST (rect));
858
0
    return NULL;
859
0
  }
860
0
  if (flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA)
861
0
    rect->info.flags |= GST_VIDEO_FLAG_PREMULTIPLIED_ALPHA;
862
863
0
  rect->x = render_x;
864
0
  rect->y = render_y;
865
0
  rect->render_width = render_width;
866
0
  rect->render_height = render_height;
867
868
0
  rect->global_alpha = 1.0;
869
0
  rect->applied_global_alpha = 1.0;
870
0
  rect->initial_alpha = NULL;
871
872
0
  rect->flags = flags;
873
874
0
  rect->seq_num = gst_video_overlay_get_seqnum ();
875
876
0
  GST_LOG ("new rectangle %p: %ux%u => %ux%u @ %u,%u, seq_num %u, format %u, "
877
0
      "flags %x, pixels %p, global_alpha=%f", rect, width, height, render_width,
878
0
      render_height, render_x, render_y, rect->seq_num, format,
879
0
      rect->flags, pixels, rect->global_alpha);
880
881
0
  return rect;
882
0
}
883
884
/**
885
 * gst_video_overlay_rectangle_get_render_rectangle:
886
 * @rectangle: a #GstVideoOverlayRectangle
887
 * @render_x: (out) (allow-none): address where to store the X render offset
888
 * @render_y: (out) (allow-none): address where to store the Y render offset
889
 * @render_width: (out) (allow-none): address where to store the render width
890
 * @render_height: (out) (allow-none): address where to store the render height
891
 *
892
 * Retrieves the render position and render dimension of the overlay
893
 * rectangle on the video.
894
 *
895
 * Returns: TRUE if valid render dimensions were retrieved.
896
 */
897
gboolean
898
gst_video_overlay_rectangle_get_render_rectangle (GstVideoOverlayRectangle *
899
    rectangle, gint * render_x, gint * render_y, guint * render_width,
900
    guint * render_height)
901
0
{
902
0
  g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), FALSE);
903
904
0
  if (render_x)
905
0
    *render_x = rectangle->x;
906
0
  if (render_y)
907
0
    *render_y = rectangle->y;
908
0
  if (render_width)
909
0
    *render_width = rectangle->render_width;
910
0
  if (render_height)
911
0
    *render_height = rectangle->render_height;
912
913
0
  return TRUE;
914
0
}
915
916
/**
917
 * gst_video_overlay_rectangle_set_render_rectangle:
918
 * @rectangle: a #GstVideoOverlayRectangle
919
 * @render_x: render X position of rectangle on video
920
 * @render_y: render Y position of rectangle on video
921
 * @render_width: render width of rectangle
922
 * @render_height: render height of rectangle
923
 *
924
 * Sets the render position and dimensions of the rectangle on the video.
925
 * This function is mainly for elements that modify the size of the video
926
 * in some way (e.g. through scaling or cropping) and need to adjust the
927
 * details of any overlays to match the operation that changed the size.
928
 *
929
 * @rectangle must be writable, meaning its refcount must be 1. You can
930
 * make the rectangles inside a #GstVideoOverlayComposition writable using
931
 * gst_video_overlay_composition_make_writable() or
932
 * gst_video_overlay_composition_copy().
933
 */
934
void
935
gst_video_overlay_rectangle_set_render_rectangle (GstVideoOverlayRectangle *
936
    rectangle, gint render_x, gint render_y, guint render_width,
937
    guint render_height)
938
0
{
939
0
  g_return_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle));
940
0
  g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST
941
0
          (rectangle)));
942
943
0
  rectangle->x = render_x;
944
0
  rectangle->y = render_y;
945
0
  rectangle->render_width = render_width;
946
0
  rectangle->render_height = render_height;
947
0
}
948
949
/* FIXME: orc-ify */
950
static void
951
gst_video_overlay_rectangle_premultiply_0 (GstVideoFrame * frame)
952
0
{
953
0
  int i, j;
954
0
  int width = GST_VIDEO_FRAME_WIDTH (frame);
955
0
  int height = GST_VIDEO_FRAME_HEIGHT (frame);
956
0
  int stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
957
0
  guint8 *data = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
958
959
0
  for (j = 0; j < height; ++j) {
960
0
    guint8 *line;
961
962
0
    line = data;
963
0
    line += stride * j;
964
0
    for (i = 0; i < width; ++i) {
965
0
      int a = line[0];
966
0
      line[1] = line[1] * a / 255;
967
0
      line[2] = line[2] * a / 255;
968
0
      line[3] = line[3] * a / 255;
969
0
      line += 4;
970
0
    }
971
0
  }
972
0
}
973
974
static void
975
gst_video_overlay_rectangle_premultiply_3 (GstVideoFrame * frame)
976
0
{
977
0
  int i, j;
978
0
  int width = GST_VIDEO_FRAME_WIDTH (frame);
979
0
  int height = GST_VIDEO_FRAME_HEIGHT (frame);
980
0
  int stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
981
0
  guint8 *data = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
982
983
0
  for (j = 0; j < height; ++j) {
984
0
    guint8 *line;
985
986
0
    line = data;
987
0
    line += stride * j;
988
0
    for (i = 0; i < width; ++i) {
989
0
      int a = line[3];
990
0
      line[0] = line[0] * a / 255;
991
0
      line[1] = line[1] * a / 255;
992
0
      line[2] = line[2] * a / 255;
993
0
      line += 4;
994
0
    }
995
0
  }
996
0
}
997
998
static void
999
gst_video_overlay_rectangle_premultiply (GstVideoFrame * frame)
1000
0
{
1001
0
  gint alpha_offset;
1002
1003
0
  alpha_offset = GST_VIDEO_FRAME_COMP_POFFSET (frame, 3);
1004
0
  switch (alpha_offset) {
1005
0
    case 0:
1006
0
      gst_video_overlay_rectangle_premultiply_0 (frame);
1007
0
      break;
1008
0
    case 3:
1009
0
      gst_video_overlay_rectangle_premultiply_3 (frame);
1010
0
      break;
1011
0
    default:
1012
0
      g_assert_not_reached ();
1013
0
      break;
1014
0
  }
1015
0
}
1016
1017
/* FIXME: orc-ify */
1018
static void
1019
gst_video_overlay_rectangle_unpremultiply_0 (GstVideoFrame * frame)
1020
0
{
1021
0
  int i, j;
1022
0
  int width = GST_VIDEO_FRAME_WIDTH (frame);
1023
0
  int height = GST_VIDEO_FRAME_HEIGHT (frame);
1024
0
  int stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
1025
0
  guint8 *data = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
1026
1027
0
  for (j = 0; j < height; ++j) {
1028
0
    guint8 *line;
1029
1030
0
    line = data;
1031
0
    line += stride * j;
1032
0
    for (i = 0; i < width; ++i) {
1033
0
      int a = line[0];
1034
0
      if (a) {
1035
0
        line[1] = MIN ((line[1] * 255 + a / 2) / a, 255);
1036
0
        line[2] = MIN ((line[2] * 255 + a / 2) / a, 255);
1037
0
        line[3] = MIN ((line[3] * 255 + a / 2) / a, 255);
1038
0
      }
1039
0
      line += 4;
1040
0
    }
1041
0
  }
1042
0
}
1043
1044
static void
1045
gst_video_overlay_rectangle_unpremultiply_3 (GstVideoFrame * frame)
1046
0
{
1047
0
  int i, j;
1048
0
  int width = GST_VIDEO_FRAME_WIDTH (frame);
1049
0
  int height = GST_VIDEO_FRAME_HEIGHT (frame);
1050
0
  int stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
1051
0
  guint8 *data = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
1052
1053
0
  for (j = 0; j < height; ++j) {
1054
0
    guint8 *line;
1055
1056
0
    line = data;
1057
0
    line += stride * j;
1058
0
    for (i = 0; i < width; ++i) {
1059
0
      int a = line[3];
1060
0
      if (a) {
1061
0
        line[0] = MIN ((line[0] * 255 + a / 2) / a, 255);
1062
0
        line[1] = MIN ((line[1] * 255 + a / 2) / a, 255);
1063
0
        line[2] = MIN ((line[2] * 255 + a / 2) / a, 255);
1064
0
      }
1065
0
      line += 4;
1066
0
    }
1067
0
  }
1068
0
}
1069
1070
static void
1071
gst_video_overlay_rectangle_unpremultiply (GstVideoFrame * frame)
1072
0
{
1073
0
  gint alpha_offset;
1074
1075
0
  alpha_offset = GST_VIDEO_FRAME_COMP_POFFSET (frame, 3);
1076
0
  switch (alpha_offset) {
1077
0
    case 0:
1078
0
      gst_video_overlay_rectangle_unpremultiply_0 (frame);
1079
0
      break;
1080
0
    case 3:
1081
0
      gst_video_overlay_rectangle_unpremultiply_3 (frame);
1082
0
      break;
1083
0
    default:
1084
0
      g_assert_not_reached ();
1085
0
      break;
1086
0
  }
1087
0
}
1088
1089
1090
static void
1091
gst_video_overlay_rectangle_extract_alpha (GstVideoOverlayRectangle * rect)
1092
0
{
1093
0
  guint8 *src, *dst;
1094
0
  GstVideoFrame frame;
1095
0
  gint i, j, w, h, stride, alpha_offset;
1096
1097
0
  alpha_offset = GST_VIDEO_INFO_COMP_POFFSET (&rect->info, 3);
1098
0
  g_return_if_fail (alpha_offset == 0 || alpha_offset == 3);
1099
1100
0
  gst_video_frame_map (&frame, &rect->info, rect->pixels, GST_MAP_READ);
1101
0
  src = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
1102
0
  w = GST_VIDEO_INFO_WIDTH (&rect->info);
1103
0
  h = GST_VIDEO_INFO_HEIGHT (&rect->info);
1104
0
  stride = GST_VIDEO_INFO_PLANE_STRIDE (&rect->info, 0);
1105
1106
0
  g_free (rect->initial_alpha);
1107
0
  rect->initial_alpha = g_malloc (w * h);
1108
0
  dst = rect->initial_alpha;
1109
1110
0
  for (i = 0; i < h; i++) {
1111
0
    for (j = 0; j < w; j++) {
1112
0
      *dst = src[alpha_offset];
1113
0
      dst++;
1114
0
      src += 4;
1115
0
    }
1116
0
    src += stride - 4 * w;
1117
0
  }
1118
0
  gst_video_frame_unmap (&frame);
1119
0
}
1120
1121
1122
static void
1123
gst_video_overlay_rectangle_apply_global_alpha (GstVideoOverlayRectangle * rect,
1124
    float global_alpha)
1125
0
{
1126
0
  guint8 *src, *dst;
1127
0
  GstVideoFrame frame;
1128
0
  gint i, j, w, h, stride;
1129
0
  gint argb_a, argb_r, argb_g, argb_b;
1130
0
  gint alpha_offset GST_UNUSED_CHECKS;
1131
1132
0
  g_assert (!(rect->applied_global_alpha != 1.0
1133
0
          && rect->initial_alpha == NULL));
1134
1135
0
#ifndef G_DISABLE_CHECKS
1136
0
  alpha_offset = GST_VIDEO_INFO_COMP_POFFSET (&rect->info, 3);
1137
0
  g_return_if_fail (alpha_offset == 0 || alpha_offset == 3);
1138
0
#endif
1139
1140
0
  if (global_alpha == rect->applied_global_alpha)
1141
0
    return;
1142
1143
0
  if (rect->initial_alpha == NULL)
1144
0
    gst_video_overlay_rectangle_extract_alpha (rect);
1145
1146
0
  src = rect->initial_alpha;
1147
0
  if (!gst_buffer_is_writable (rect->pixels)) {
1148
0
    gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (rect->pixels),
1149
0
        GST_MINI_OBJECT_CAST (rect));
1150
0
    rect->pixels = gst_buffer_copy (rect->pixels);
1151
0
    gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (rect->pixels),
1152
0
        GST_MINI_OBJECT_CAST (rect));
1153
0
  }
1154
1155
0
  gst_video_frame_map (&frame, &rect->info, rect->pixels, GST_MAP_READ);
1156
0
  dst = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
1157
0
  w = GST_VIDEO_INFO_WIDTH (&rect->info);
1158
0
  h = GST_VIDEO_INFO_HEIGHT (&rect->info);
1159
0
  stride = GST_VIDEO_INFO_PLANE_STRIDE (&rect->info, 0);
1160
1161
0
  argb_a = GST_VIDEO_INFO_COMP_POFFSET (&rect->info, 3);
1162
0
  argb_r = (argb_a + 1) % 4;
1163
0
  argb_g = (argb_a + 2) % 4;
1164
0
  argb_b = (argb_a + 3) % 4;
1165
1166
0
  for (i = 0; i < h; i++) {
1167
0
    for (j = 0; j < w; j++) {
1168
0
      guint8 na = (guint8) (*src * global_alpha);
1169
1170
0
      if (!!(rect->flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA)) {
1171
0
        dst[argb_r] =
1172
0
            (guint8) ((double) (dst[argb_r] * 255) / (double) dst[argb_a]) *
1173
0
            na / 255;
1174
0
        dst[argb_g] =
1175
0
            (guint8) ((double) (dst[argb_g] * 255) / (double) dst[argb_a]) *
1176
0
            na / 255;
1177
0
        dst[argb_b] =
1178
0
            (guint8) ((double) (dst[argb_b] * 255) / (double) dst[argb_a]) *
1179
0
            na / 255;
1180
0
      }
1181
0
      dst[argb_a] = na;
1182
0
      src++;
1183
0
      dst += 4;
1184
0
    }
1185
0
    dst += stride - 4 * w;
1186
0
  }
1187
0
  gst_video_frame_unmap (&frame);
1188
1189
0
  rect->applied_global_alpha = global_alpha;
1190
0
}
1191
1192
static void
1193
gst_video_overlay_rectangle_convert (const GstVideoInfo * src,
1194
    GstBuffer * src_buffer, GstVideoFormat dest_format, GstVideoInfo * dest,
1195
    GstBuffer ** dest_buffer)
1196
0
{
1197
0
  gint width, height, stride;
1198
0
  GstVideoFrame src_frame, dest_frame;
1199
0
  GstVideoFormat format;
1200
0
  gint k, l;
1201
0
  guint8 *sdata, *ddata;
1202
1203
0
  format = GST_VIDEO_INFO_FORMAT (src);
1204
1205
0
  width = GST_VIDEO_INFO_WIDTH (src);
1206
0
  height = GST_VIDEO_INFO_HEIGHT (src);
1207
1208
0
  gst_video_info_init (dest);
1209
0
  if (!gst_video_info_set_format (dest, dest_format, width, height)) {
1210
0
    g_warn_if_reached ();
1211
0
    return;
1212
0
  }
1213
1214
0
  *dest_buffer = gst_buffer_new_and_alloc (GST_VIDEO_INFO_SIZE (dest));
1215
1216
0
  gst_video_frame_map (&src_frame, src, src_buffer, GST_MAP_READ);
1217
0
  gst_video_frame_map (&dest_frame, dest, *dest_buffer, GST_MAP_WRITE);
1218
1219
0
  sdata = GST_VIDEO_FRAME_PLANE_DATA (&src_frame, 0);
1220
0
  ddata = GST_VIDEO_FRAME_PLANE_DATA (&dest_frame, 0);
1221
0
  stride = GST_VIDEO_FRAME_PLANE_STRIDE (&src_frame, 0);
1222
1223
0
  if (format == GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV &&
1224
0
      dest_format == GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB) {
1225
0
    gint ayuv;
1226
0
    gint a, y, u, v, r, g, b;
1227
1228
0
    for (k = 0; k < height; k++) {
1229
0
      for (l = 0; l < width; l++) {
1230
0
        ayuv = GST_READ_UINT32_BE (sdata);
1231
0
        a = ayuv >> 24;
1232
0
        y = (ayuv >> 16) & 0xff;
1233
0
        u = (ayuv >> 8) & 0xff;
1234
0
        v = (ayuv & 0xff);
1235
1236
0
        r = (298 * y + 459 * v - 63514) >> 8;
1237
0
        g = (298 * y - 55 * u - 136 * v + 19681) >> 8;
1238
0
        b = (298 * y + 541 * u - 73988) >> 8;
1239
1240
0
        r = CLAMP (r, 0, 255);
1241
0
        g = CLAMP (g, 0, 255);
1242
0
        b = CLAMP (b, 0, 255);
1243
1244
        /* native endian ARGB */
1245
0
        *(guint32 *) ddata = ((a << 24) | (r << 16) | (g << 8) | b);
1246
1247
0
        sdata += 4;
1248
0
        ddata += 4;
1249
0
      }
1250
0
      sdata += stride - 4 * width;
1251
0
    }
1252
0
  } else if (format == GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB &&
1253
0
      dest_format == GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV) {
1254
0
    gint argb;
1255
0
    gint a, y, u, v, r, g, b;
1256
1257
0
    for (k = 0; k < height; k++) {
1258
0
      for (l = 0; l < width; l++) {
1259
        /* native endian ARGB */
1260
0
        argb = *(guint32 *) sdata;
1261
0
        a = argb >> 24;
1262
0
        r = (argb >> 16) & 0xff;
1263
0
        g = (argb >> 8) & 0xff;
1264
0
        b = (argb & 0xff);
1265
1266
0
        y = (47 * r + 157 * g + 16 * b + 4096) >> 8;
1267
0
        u = (-26 * r - 87 * g + 112 * b + 32768) >> 8;
1268
0
        v = (112 * r - 102 * g - 10 * b + 32768) >> 8;
1269
1270
0
        y = CLAMP (y, 0, 255);
1271
0
        u = CLAMP (u, 0, 255);
1272
0
        v = CLAMP (v, 0, 255);
1273
1274
0
        GST_WRITE_UINT32_BE (ddata, ((a << 24) | (y << 16) | (u << 8) | v));
1275
1276
0
        sdata += 4;
1277
0
        ddata += 4;
1278
0
      }
1279
0
      sdata += stride - 4 * width;
1280
0
    }
1281
0
  } else {
1282
0
    GST_ERROR ("unsupported conversion");
1283
0
    g_assert_not_reached ();
1284
0
  }
1285
1286
0
  gst_video_frame_unmap (&src_frame);
1287
0
  gst_video_frame_unmap (&dest_frame);
1288
0
}
1289
1290
static GstBuffer *
1291
gst_video_overlay_rectangle_get_pixels_raw_internal (GstVideoOverlayRectangle *
1292
    rectangle, GstVideoOverlayFormatFlags flags, gboolean unscaled,
1293
    GstVideoFormat wanted_format)
1294
0
{
1295
0
  GstVideoOverlayFormatFlags new_flags;
1296
0
  GstVideoOverlayRectangle *scaled_rect = NULL, *conv_rect = NULL;
1297
0
  GstVideoInfo info;
1298
0
  GstVideoFrame frame;
1299
0
  GstBuffer *buf;
1300
0
  GList *l;
1301
0
  guint width, height;
1302
0
  guint wanted_width;
1303
0
  guint wanted_height;
1304
0
  gboolean apply_global_alpha;
1305
0
  gboolean revert_global_alpha;
1306
0
  GstVideoFormat format;
1307
1308
0
  g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1309
0
  g_return_val_if_fail (gst_video_overlay_rectangle_check_flags (flags), NULL);
1310
1311
0
  width = GST_VIDEO_INFO_WIDTH (&rectangle->info);
1312
0
  height = GST_VIDEO_INFO_HEIGHT (&rectangle->info);
1313
0
  wanted_width = unscaled ? width : rectangle->render_width;
1314
0
  wanted_height = unscaled ? height : rectangle->render_height;
1315
0
  format = GST_VIDEO_INFO_FORMAT (&rectangle->info);
1316
1317
0
  apply_global_alpha =
1318
0
      (!!(rectangle->flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA)
1319
0
      && !(flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA));
1320
0
  revert_global_alpha =
1321
0
      (!!(rectangle->flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA)
1322
0
      && !!(flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA));
1323
1324
  /* This assumes we don't need to adjust the format */
1325
0
  if (wanted_width == width &&
1326
0
      wanted_height == height &&
1327
0
      wanted_format == format &&
1328
0
      gst_video_overlay_rectangle_is_same_alpha_type (rectangle->flags,
1329
0
          flags)) {
1330
    /* don't need to apply/revert global-alpha either: */
1331
0
    if ((!apply_global_alpha
1332
0
            || rectangle->applied_global_alpha == rectangle->global_alpha)
1333
0
        && (!revert_global_alpha || rectangle->applied_global_alpha == 1.0)) {
1334
0
      return rectangle->pixels;
1335
0
    } else {
1336
      /* only apply/revert global-alpha */
1337
0
      scaled_rect = rectangle;
1338
0
      goto done;
1339
0
    }
1340
0
  }
1341
1342
  /* see if we've got one cached already */
1343
0
  GST_RECTANGLE_LOCK (rectangle);
1344
0
  for (l = rectangle->scaled_rectangles; l != NULL; l = l->next) {
1345
0
    GstVideoOverlayRectangle *r = l->data;
1346
1347
0
    if (GST_VIDEO_INFO_WIDTH (&r->info) == wanted_width &&
1348
0
        GST_VIDEO_INFO_HEIGHT (&r->info) == wanted_height &&
1349
0
        GST_VIDEO_INFO_FORMAT (&r->info) == wanted_format &&
1350
0
        gst_video_overlay_rectangle_is_same_alpha_type (r->flags, flags)) {
1351
      /* we'll keep these rectangles around until finalize, so it's ok not
1352
       * to take our own ref here */
1353
0
      scaled_rect = r;
1354
0
      break;
1355
0
    }
1356
0
  }
1357
0
  GST_RECTANGLE_UNLOCK (rectangle);
1358
1359
0
  if (scaled_rect != NULL)
1360
0
    goto done;
1361
1362
  /* maybe have one in the right format though */
1363
0
  if (format != wanted_format) {
1364
0
    GST_RECTANGLE_LOCK (rectangle);
1365
0
    for (l = rectangle->scaled_rectangles; l != NULL; l = l->next) {
1366
0
      GstVideoOverlayRectangle *r = l->data;
1367
1368
0
      if (GST_VIDEO_INFO_FORMAT (&r->info) == wanted_format &&
1369
0
          gst_video_overlay_rectangle_is_same_alpha_type (r->flags, flags)) {
1370
        /* we'll keep these rectangles around until finalize, so it's ok not
1371
         * to take our own ref here */
1372
0
        conv_rect = r;
1373
0
        break;
1374
0
      }
1375
0
    }
1376
0
    GST_RECTANGLE_UNLOCK (rectangle);
1377
0
  } else {
1378
0
    conv_rect = rectangle;
1379
0
  }
1380
1381
0
  if (conv_rect == NULL) {
1382
0
    GstVideoInfo conv_info;
1383
1384
0
    gst_video_overlay_rectangle_convert (&rectangle->info, rectangle->pixels,
1385
0
        wanted_format, &conv_info, &buf);
1386
0
    gst_buffer_add_video_meta (buf, GST_VIDEO_FRAME_FLAG_NONE,
1387
0
        GST_VIDEO_INFO_FORMAT (&conv_info), width, height);
1388
0
    conv_rect = gst_video_overlay_rectangle_new_raw (buf,
1389
0
        0, 0, width, height, rectangle->flags);
1390
0
    if (rectangle->global_alpha != 1.0)
1391
0
      gst_video_overlay_rectangle_set_global_alpha (scaled_rect,
1392
0
          rectangle->global_alpha);
1393
0
    gst_buffer_unref (buf);
1394
    /* keep this converted one around as well in any case */
1395
0
    GST_RECTANGLE_LOCK (rectangle);
1396
0
    rectangle->scaled_rectangles =
1397
0
        g_list_prepend (rectangle->scaled_rectangles, conv_rect);
1398
0
    GST_RECTANGLE_UNLOCK (rectangle);
1399
0
  }
1400
1401
  /* now we continue from conv_rect */
1402
0
  width = GST_VIDEO_INFO_WIDTH (&conv_rect->info);
1403
0
  height = GST_VIDEO_INFO_HEIGHT (&conv_rect->info);
1404
0
  format = GST_VIDEO_INFO_FORMAT (&conv_rect->info);
1405
1406
  /* not cached yet, do the preprocessing and put the result into our cache */
1407
0
  if (wanted_width != width || wanted_height != height) {
1408
0
    GstVideoInfo scaled_info;
1409
1410
    /* we could check the cache for a scaled rect with global_alpha == 1 here */
1411
0
    gst_video_blend_scale_linear_RGBA (&conv_rect->info, conv_rect->pixels,
1412
0
        wanted_height, wanted_width, &scaled_info, &buf);
1413
0
    info = scaled_info;
1414
0
    gst_buffer_add_video_meta (buf, GST_VIDEO_FRAME_FLAG_NONE,
1415
0
        GST_VIDEO_INFO_FORMAT (&conv_rect->info), wanted_width, wanted_height);
1416
0
  } else if (!gst_video_overlay_rectangle_is_same_alpha_type (conv_rect->flags,
1417
0
          flags)) {
1418
    /* if we don't have to scale, we have to modify the alpha values, so we
1419
     * need to make a copy of the pixel memory (and we take ownership below) */
1420
0
    buf = gst_buffer_copy (conv_rect->pixels);
1421
0
    info = conv_rect->info;
1422
0
  } else {
1423
    /* do not need to scale or modify alpha values, almost done then */
1424
0
    scaled_rect = conv_rect;
1425
0
    goto done;
1426
0
  }
1427
1428
0
  new_flags = conv_rect->flags;
1429
0
  gst_video_frame_map (&frame, &info, buf, GST_MAP_READWRITE);
1430
0
  if (!gst_video_overlay_rectangle_is_same_alpha_type (conv_rect->flags, flags)) {
1431
0
    if (rectangle->flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA) {
1432
0
      gst_video_overlay_rectangle_unpremultiply (&frame);
1433
0
      new_flags &= ~GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA;
1434
0
    } else {
1435
0
      gst_video_overlay_rectangle_premultiply (&frame);
1436
0
      new_flags |= GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA;
1437
0
    }
1438
0
  }
1439
0
  gst_video_frame_unmap (&frame);
1440
1441
0
  scaled_rect = gst_video_overlay_rectangle_new_raw (buf,
1442
0
      0, 0, wanted_width, wanted_height, new_flags);
1443
0
  if (conv_rect->global_alpha != 1.0)
1444
0
    gst_video_overlay_rectangle_set_global_alpha (scaled_rect,
1445
0
        conv_rect->global_alpha);
1446
0
  gst_buffer_unref (buf);
1447
1448
0
  GST_RECTANGLE_LOCK (rectangle);
1449
0
  rectangle->scaled_rectangles =
1450
0
      g_list_prepend (rectangle->scaled_rectangles, scaled_rect);
1451
0
  GST_RECTANGLE_UNLOCK (rectangle);
1452
1453
0
done:
1454
1455
0
  GST_RECTANGLE_LOCK (rectangle);
1456
0
  if (apply_global_alpha
1457
0
      && scaled_rect->applied_global_alpha != rectangle->global_alpha) {
1458
0
    gst_video_overlay_rectangle_apply_global_alpha (scaled_rect,
1459
0
        rectangle->global_alpha);
1460
0
    gst_video_overlay_rectangle_set_global_alpha (scaled_rect,
1461
0
        rectangle->global_alpha);
1462
0
  } else if (revert_global_alpha && scaled_rect->applied_global_alpha != 1.0) {
1463
0
    gst_video_overlay_rectangle_apply_global_alpha (scaled_rect, 1.0);
1464
0
  }
1465
0
  GST_RECTANGLE_UNLOCK (rectangle);
1466
1467
0
  return scaled_rect->pixels;
1468
0
}
1469
1470
1471
/**
1472
 * gst_video_overlay_rectangle_get_pixels_raw:
1473
 * @rectangle: a #GstVideoOverlayRectangle
1474
 * @flags: flags
1475
 *    If a global_alpha value != 1 is set for the rectangle, the caller
1476
 *    should set the #GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA flag
1477
 *    if he wants to apply global-alpha himself. If the flag is not set
1478
 *    global_alpha is applied internally before returning the pixel-data.
1479
 *
1480
 * Returns: (transfer none): a #GstBuffer holding the pixel data with
1481
 *    format as originally provided and specified in video meta with
1482
 *    width and height of the render dimensions as per
1483
 *    gst_video_overlay_rectangle_get_render_rectangle(). This function does
1484
 *    not return a reference, the caller should obtain a reference of her own
1485
 *    with gst_buffer_ref() if needed.
1486
 */
1487
GstBuffer *
1488
gst_video_overlay_rectangle_get_pixels_raw (GstVideoOverlayRectangle *
1489
    rectangle, GstVideoOverlayFormatFlags flags)
1490
0
{
1491
0
  return gst_video_overlay_rectangle_get_pixels_raw_internal (rectangle,
1492
0
      flags, FALSE, GST_VIDEO_INFO_FORMAT (&rectangle->info));
1493
0
}
1494
1495
/**
1496
 * gst_video_overlay_rectangle_get_pixels_argb:
1497
 * @rectangle: a #GstVideoOverlayRectangle
1498
 * @flags: flags
1499
 *    If a global_alpha value != 1 is set for the rectangle, the caller
1500
 *    should set the #GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA flag
1501
 *    if he wants to apply global-alpha himself. If the flag is not set
1502
 *    global_alpha is applied internally before returning the pixel-data.
1503
 *
1504
 * Returns: (transfer none): a #GstBuffer holding the ARGB pixel data with
1505
 *    width and height of the render dimensions as per
1506
 *    gst_video_overlay_rectangle_get_render_rectangle(). This function does
1507
 *    not return a reference, the caller should obtain a reference of her own
1508
 *    with gst_buffer_ref() if needed.
1509
 */
1510
GstBuffer *
1511
gst_video_overlay_rectangle_get_pixels_argb (GstVideoOverlayRectangle *
1512
    rectangle, GstVideoOverlayFormatFlags flags)
1513
0
{
1514
0
  return gst_video_overlay_rectangle_get_pixels_raw_internal (rectangle,
1515
0
      flags, FALSE, GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB);
1516
0
}
1517
1518
/**
1519
 * gst_video_overlay_rectangle_get_pixels_ayuv:
1520
 * @rectangle: a #GstVideoOverlayRectangle
1521
 * @flags: flags
1522
 *    If a global_alpha value != 1 is set for the rectangle, the caller
1523
 *    should set the #GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA flag
1524
 *    if he wants to apply global-alpha himself. If the flag is not set
1525
 *    global_alpha is applied internally before returning the pixel-data.
1526
 *
1527
 * Returns: (transfer none): a #GstBuffer holding the AYUV pixel data with
1528
 *    width and height of the render dimensions as per
1529
 *    gst_video_overlay_rectangle_get_render_rectangle(). This function does
1530
 *    not return a reference, the caller should obtain a reference of her own
1531
 *    with gst_buffer_ref() if needed.
1532
 */
1533
GstBuffer *
1534
gst_video_overlay_rectangle_get_pixels_ayuv (GstVideoOverlayRectangle *
1535
    rectangle, GstVideoOverlayFormatFlags flags)
1536
0
{
1537
0
  return gst_video_overlay_rectangle_get_pixels_raw_internal (rectangle,
1538
0
      flags, FALSE, GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV);
1539
0
}
1540
1541
/**
1542
 * gst_video_overlay_rectangle_get_pixels_unscaled_raw:
1543
 * @rectangle: a #GstVideoOverlayRectangle
1544
 * @flags: flags.
1545
 *    If a global_alpha value != 1 is set for the rectangle, the caller
1546
 *    should set the #GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA flag
1547
 *    if he wants to apply global-alpha himself. If the flag is not set
1548
 *    global_alpha is applied internally before returning the pixel-data.
1549
 *
1550
 * Retrieves the pixel data as it is. This is useful if the caller can
1551
 * do the scaling itself when handling the overlaying. The rectangle will
1552
 * need to be scaled to the render dimensions, which can be retrieved using
1553
 * gst_video_overlay_rectangle_get_render_rectangle().
1554
 *
1555
 * Returns: (transfer none): a #GstBuffer holding the pixel data with
1556
 *    #GstVideoMeta set. This function does not return a reference, the caller
1557
 *    should obtain a reference of her own with gst_buffer_ref() if needed.
1558
 */
1559
GstBuffer *
1560
gst_video_overlay_rectangle_get_pixels_unscaled_raw (GstVideoOverlayRectangle *
1561
    rectangle, GstVideoOverlayFormatFlags flags)
1562
0
{
1563
0
  g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1564
1565
0
  return gst_video_overlay_rectangle_get_pixels_raw_internal (rectangle,
1566
0
      flags, TRUE, GST_VIDEO_INFO_FORMAT (&rectangle->info));
1567
0
}
1568
1569
/**
1570
 * gst_video_overlay_rectangle_get_pixels_unscaled_argb:
1571
 * @rectangle: a #GstVideoOverlayRectangle
1572
 * @flags: flags.
1573
 *    If a global_alpha value != 1 is set for the rectangle, the caller
1574
 *    should set the #GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA flag
1575
 *    if he wants to apply global-alpha himself. If the flag is not set
1576
 *    global_alpha is applied internally before returning the pixel-data.
1577
 *
1578
 * Retrieves the pixel data as it is. This is useful if the caller can
1579
 * do the scaling itself when handling the overlaying. The rectangle will
1580
 * need to be scaled to the render dimensions, which can be retrieved using
1581
 * gst_video_overlay_rectangle_get_render_rectangle().
1582
 *
1583
 * Returns: (transfer none): a #GstBuffer holding the ARGB pixel data with
1584
 *    #GstVideoMeta set. This function does not return a reference, the caller
1585
 *    should obtain a reference of her own with gst_buffer_ref() if needed.
1586
 */
1587
GstBuffer *
1588
gst_video_overlay_rectangle_get_pixels_unscaled_argb (GstVideoOverlayRectangle *
1589
    rectangle, GstVideoOverlayFormatFlags flags)
1590
0
{
1591
0
  g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1592
1593
0
  return gst_video_overlay_rectangle_get_pixels_raw_internal (rectangle,
1594
0
      flags, TRUE, GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB);
1595
0
}
1596
1597
/**
1598
 * gst_video_overlay_rectangle_get_pixels_unscaled_ayuv:
1599
 * @rectangle: a #GstVideoOverlayRectangle
1600
 * @flags: flags.
1601
 *    If a global_alpha value != 1 is set for the rectangle, the caller
1602
 *    should set the #GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA flag
1603
 *    if he wants to apply global-alpha himself. If the flag is not set
1604
 *    global_alpha is applied internally before returning the pixel-data.
1605
 *
1606
 * Retrieves the pixel data as it is. This is useful if the caller can
1607
 * do the scaling itself when handling the overlaying. The rectangle will
1608
 * need to be scaled to the render dimensions, which can be retrieved using
1609
 * gst_video_overlay_rectangle_get_render_rectangle().
1610
 *
1611
 * Returns: (transfer none): a #GstBuffer holding the AYUV pixel data with
1612
 *    #GstVideoMeta set. This function does not return a reference, the caller
1613
 *    should obtain a reference of her own with gst_buffer_ref() if needed.
1614
 */
1615
GstBuffer *
1616
gst_video_overlay_rectangle_get_pixels_unscaled_ayuv (GstVideoOverlayRectangle *
1617
    rectangle, GstVideoOverlayFormatFlags flags)
1618
0
{
1619
0
  g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1620
1621
0
  return gst_video_overlay_rectangle_get_pixels_raw_internal (rectangle,
1622
0
      flags, TRUE, GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV);
1623
0
}
1624
1625
/**
1626
 * gst_video_overlay_rectangle_get_flags:
1627
 * @rectangle: a #GstVideoOverlayRectangle
1628
 *
1629
 * Retrieves the flags associated with a #GstVideoOverlayRectangle.
1630
 * This is useful if the caller can handle both premultiplied alpha and
1631
 * non premultiplied alpha, for example. By knowing whether the rectangle
1632
 * uses premultiplied or not, it can request the pixel data in the format
1633
 * it is stored in, to avoid unnecessary conversion.
1634
 *
1635
 * Returns: the #GstVideoOverlayFormatFlags associated with the rectangle.
1636
 */
1637
GstVideoOverlayFormatFlags
1638
gst_video_overlay_rectangle_get_flags (GstVideoOverlayRectangle * rectangle)
1639
0
{
1640
0
  g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle),
1641
0
      GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1642
1643
0
  return rectangle->flags;
1644
0
}
1645
1646
/**
1647
 * gst_video_overlay_rectangle_get_global_alpha:
1648
 * @rectangle: a #GstVideoOverlayRectangle
1649
 *
1650
 * Retrieves the global-alpha value associated with a #GstVideoOverlayRectangle.
1651
 *
1652
 * Returns: the global-alpha value associated with the rectangle.
1653
 */
1654
gfloat
1655
gst_video_overlay_rectangle_get_global_alpha (GstVideoOverlayRectangle *
1656
    rectangle)
1657
0
{
1658
0
  g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), -1);
1659
1660
0
  return rectangle->global_alpha;
1661
0
}
1662
1663
/**
1664
 * gst_video_overlay_rectangle_set_global_alpha:
1665
 * @rectangle: a #GstVideoOverlayRectangle
1666
 * @global_alpha: Global alpha value (0 to 1.0)
1667
 *
1668
 * Sets the global alpha value associated with a #GstVideoOverlayRectangle. Per-
1669
 * pixel alpha values are multiplied with this value. Valid
1670
 * values: 0 <= global_alpha <= 1; 1 to deactivate.
1671
 *
1672
 * @rectangle must be writable, meaning its refcount must be 1. You can
1673
 * make the rectangles inside a #GstVideoOverlayComposition writable using
1674
 * gst_video_overlay_composition_make_writable() or
1675
 * gst_video_overlay_composition_copy().
1676
 */
1677
void
1678
gst_video_overlay_rectangle_set_global_alpha (GstVideoOverlayRectangle *
1679
    rectangle, gfloat global_alpha)
1680
0
{
1681
0
  g_return_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle));
1682
0
  g_return_if_fail (gst_mini_object_is_writable (GST_MINI_OBJECT_CAST
1683
0
          (rectangle)));
1684
0
  g_return_if_fail (global_alpha >= 0 && global_alpha <= 1);
1685
1686
0
  if (rectangle->global_alpha != global_alpha) {
1687
0
    rectangle->global_alpha = global_alpha;
1688
0
    if (global_alpha != 1)
1689
0
      rectangle->flags |= GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA;
1690
0
    else
1691
0
      rectangle->flags &= ~GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA;
1692
    /* update seq_num automatically to signal the consumer, that data has changed
1693
     * note, that this might mislead renderers, that can handle global-alpha
1694
     * themselves, because what they want to know is whether the actual pixel data
1695
     * has changed. */
1696
0
    rectangle->seq_num = gst_video_overlay_get_seqnum ();
1697
0
  }
1698
0
}
1699
1700
/**
1701
 * gst_video_overlay_rectangle_copy:
1702
 * @rectangle: (transfer none): a #GstVideoOverlayRectangle to copy
1703
 *
1704
 * Makes a copy of @rectangle, so that it is possible to modify it
1705
 * (e.g. to change the render co-ordinates or render dimension). The
1706
 * actual overlay pixel data buffers contained in the rectangle are not
1707
 * copied.
1708
 *
1709
 * Returns: (transfer full): a new #GstVideoOverlayRectangle equivalent
1710
 *     to @rectangle.
1711
 */
1712
GstVideoOverlayRectangle *
1713
gst_video_overlay_rectangle_copy (GstVideoOverlayRectangle * rectangle)
1714
0
{
1715
0
  GstVideoOverlayRectangle *copy;
1716
1717
0
  g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1718
1719
0
  copy = gst_video_overlay_rectangle_new_raw (rectangle->pixels,
1720
0
      rectangle->x, rectangle->y,
1721
0
      rectangle->render_width, rectangle->render_height, rectangle->flags);
1722
0
  if (rectangle->global_alpha != 1)
1723
0
    gst_video_overlay_rectangle_set_global_alpha (copy,
1724
0
        rectangle->global_alpha);
1725
1726
0
  return copy;
1727
0
}
1728
1729
/**
1730
 * gst_video_overlay_rectangle_get_seqnum:
1731
 * @rectangle: a #GstVideoOverlayRectangle
1732
 *
1733
 * Returns the sequence number of this rectangle. Sequence numbers are
1734
 * monotonically increasing and unique for overlay compositions and rectangles
1735
 * (meaning there will never be a rectangle with the same sequence number as
1736
 * a composition).
1737
 *
1738
 * Using the sequence number of a rectangle as an indicator for changed
1739
 * pixel-data of a rectangle is dangereous. Some API calls, like e.g.
1740
 * gst_video_overlay_rectangle_set_global_alpha(), automatically update
1741
 * the per rectangle sequence number, which is misleading for renderers/
1742
 * consumers, that handle global-alpha themselves. For them  the
1743
 * pixel-data returned by gst_video_overlay_rectangle_get_pixels_*()
1744
 * won't be different for different global-alpha values. In this case a
1745
 * renderer could also use the GstBuffer pointers as a hint for changed
1746
 * pixel-data.
1747
 *
1748
 * Returns: the sequence number of @rectangle
1749
 */
1750
guint
1751
gst_video_overlay_rectangle_get_seqnum (GstVideoOverlayRectangle * rectangle)
1752
0
{
1753
0
  g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), 0);
1754
1755
0
  return rectangle->seq_num;
1756
0
}