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