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>
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.
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.
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.
23 * SECTION:gstvideooverlaycomposition
24 * @short_description: Video Buffer Overlay Compositions (Subtitles, Logos)
28 * Functions to create and handle overlay compositions on video buffers.
31 * An overlay composition describes one or more overlay rectangles to be
32 * blended on top of a video buffer.
35 * This API serves two main purposes:
38 * it can be used to attach overlay information (subtitles or logos)
39 * to non-raw video buffers such as GL/VAAPI/VDPAU surfaces. The actual
40 * blending of the overlay can then be done by e.g. the video sink that
41 * processes these non-raw buffers.
44 * it can also be used to blend overlay rectangles on top of raw video
45 * buffers, thus consolidating blending functionality for raw video in
48 * Together, this allows existing overlay elements to easily handle raw
49 * and non-raw video as input in without major changes (once the overlays
50 * have been put into a #GstOverlayComposition object anyway) - for raw
51 * video the overlay can just use the blending function to blend the data
52 * on top of the video, and for surface buffers it can just attach them to
53 * the buffer and let the sink render the overlays.
60 * - provide accessors for seq_num and other fields (as needed)
61 * - allow overlay to set/get original pango markup string on/from rectangle
68 #include "video-overlay-composition.h"
69 #include "video-blend.h"
70 #include "gstvideometa.h"
73 struct _GstVideoOverlayComposition
78 GstVideoOverlayRectangle **rectangles;
80 /* lowest rectangle sequence number still used by the upstream
81 * overlay element. This way a renderer maintaining some kind of
82 * rectangles <-> surface cache can know when to free cached
83 * surfaces/rectangles. */
84 guint min_seq_num_used;
86 /* sequence number for the composition (same series as rectangles) */
90 struct _GstVideoOverlayRectangle
94 /* Position on video frame and dimension of output rectangle in
95 * output frame terms (already adjusted for the PAR of the output
96 * frame). x/y can be negative (overlay will be clipped then) */
98 guint render_width, render_height;
100 /* Info on overlay pixels (format, width, height) */
103 /* The flags associated to this rectangle */
104 GstVideoOverlayFormatFlags flags;
106 /* Refcounted blob of memory, no caps or timestamps */
109 /* FIXME: how to express source like text or pango markup?
110 * (just add source type enum + source buffer with data)
112 * FOR 0.10: always send pixel blobs, but attach source data in
113 * addition (reason: if downstream changes, we can't renegotiate
114 * that properly, if we just do a query of supported formats from
115 * the start). Sink will just ignore pixels and use pango markup
116 * from source data if it supports that.
118 * FOR 0.11: overlay should query formats (pango markup, pixels)
119 * supported by downstream and then only send that. We can
120 * renegotiate via the reconfigure event.
123 /* sequence number: useful for backends/renderers/sinks that want
124 * to maintain a cache of rectangles <-> surfaces. The value of
125 * the min_seq_num_used in the composition tells the renderer which
126 * rectangles have expired. */
129 /* global alpha: global alpha value of the rectangle. Each each per-pixel
130 * alpha value of image-data will be multiplied with the global alpha value
132 * Can be used for efficient fading in/out of overlay rectangles.
133 * GstElements that render OverlayCompositions and don't support global alpha
134 * should simply ignore it.*/
137 /* track alpha-values already applied: */
138 gfloat applied_global_alpha;
139 /* store initial per-pixel alpha values: */
140 guint8 *initial_alpha;
142 /* FIXME: we may also need a (private) way to cache converted/scaled
146 GList *scaled_rectangles;
149 #define GST_RECTANGLE_LOCK(rect) g_mutex_lock(&rect->lock)
150 #define GST_RECTANGLE_UNLOCK(rect) g_mutex_unlock(&rect->lock)
152 /* --------------------------- utility functions --------------------------- */
154 #ifndef GST_DISABLE_GST_DEBUG
156 #define GST_CAT_DEFAULT ensure_debug_category()
158 static GstDebugCategory *
159 ensure_debug_category (void)
161 static gsize cat_gonce = 0;
163 if (g_once_init_enter (&cat_gonce)) {
166 cat_done = (gsize) _gst_debug_category_new ("video-composition", 0,
167 "video overlay composition");
169 g_once_init_leave (&cat_gonce, cat_done);
172 return (GstDebugCategory *) cat_gonce;
177 #define ensure_debug_category() /* NOOP */
179 #endif /* GST_DISABLE_GST_DEBUG */
182 gst_video_overlay_get_seqnum (void)
184 static gint seqnum; /* 0 */
186 return (guint) g_atomic_int_add (&seqnum, 1);
190 gst_video_overlay_composition_meta_free (GstMeta * meta, GstBuffer * buf)
192 GstVideoOverlayCompositionMeta *ometa;
194 ometa = (GstVideoOverlayCompositionMeta *) meta;
197 gst_video_overlay_composition_unref (ometa->overlay);
201 gst_video_overlay_composition_meta_transform (GstBuffer * dest, GstMeta * meta,
202 GstBuffer * buffer, GQuark type, gpointer data)
204 GstVideoOverlayCompositionMeta *dmeta, *smeta;
206 smeta = (GstVideoOverlayCompositionMeta *) meta;
208 if (GST_META_TRANSFORM_IS_COPY (type)) {
209 GstMetaTransformCopy *copy = data;
212 GST_DEBUG ("copy video overlay composition metadata");
214 /* only copy if the complete data is copied as well */
216 (GstVideoOverlayCompositionMeta *) gst_buffer_add_meta (dest,
217 GST_VIDEO_OVERLAY_COMPOSITION_META_INFO, NULL);
218 dmeta->overlay = gst_video_overlay_composition_ref (smeta->overlay);
225 gst_video_overlay_composition_meta_api_get_type (void)
227 static volatile GType type = 0;
228 static const gchar *tags[] = { NULL };
230 if (g_once_init_enter (&type)) {
232 gst_meta_api_type_register ("GstVideoOverlayCompositionMetaAPI", tags);
233 g_once_init_leave (&type, _type);
238 /* video overlay composition metadata */
240 gst_video_overlay_composition_meta_get_info (void)
242 static const GstMetaInfo *video_overlay_composition_meta_info = NULL;
244 if (g_once_init_enter (&video_overlay_composition_meta_info)) {
245 const GstMetaInfo *meta =
246 gst_meta_register (GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE,
247 "GstVideoOverlayCompositionMeta",
248 sizeof (GstVideoOverlayCompositionMeta), (GstMetaInitFunction) NULL,
249 (GstMetaFreeFunction) gst_video_overlay_composition_meta_free,
250 (GstMetaTransformFunction)
251 gst_video_overlay_composition_meta_transform);
252 g_once_init_leave (&video_overlay_composition_meta_info, meta);
254 return video_overlay_composition_meta_info;
258 * gst_buffer_add_video_overlay_composition_meta:
260 * @comp: (allow-none): a #GstVideoOverlayComposition
262 * Sets an overlay composition on a buffer. The buffer will obtain its own
263 * reference to the composition, meaning this function does not take ownership
266 GstVideoOverlayCompositionMeta *
267 gst_buffer_add_video_overlay_composition_meta (GstBuffer * buf,
268 GstVideoOverlayComposition * comp)
270 GstVideoOverlayCompositionMeta *ometa;
272 g_return_val_if_fail (gst_buffer_is_writable (buf), NULL);
274 ometa = (GstVideoOverlayCompositionMeta *)
275 gst_buffer_add_meta (buf, GST_VIDEO_OVERLAY_COMPOSITION_META_INFO, NULL);
277 ometa->overlay = gst_video_overlay_composition_ref (comp);
282 /* ------------------------------ composition ------------------------------ */
284 #define RECTANGLE_ARRAY_STEP 4 /* premature optimization */
286 GST_DEFINE_MINI_OBJECT_TYPE (GstVideoOverlayComposition,
287 gst_video_overlay_composition);
290 gst_video_overlay_composition_free (GstMiniObject * mini_obj)
292 GstVideoOverlayComposition *comp = (GstVideoOverlayComposition *) mini_obj;
295 num = comp->num_rectangles;
298 gst_video_overlay_rectangle_unref (comp->rectangles[num - 1]);
302 g_free (comp->rectangles);
303 comp->rectangles = NULL;
304 comp->num_rectangles = 0;
306 g_slice_free (GstVideoOverlayComposition, comp);
310 * gst_video_overlay_composition_new:
311 * @rectangle: (transfer none): a #GstVideoOverlayRectangle to add to the
314 * Creates a new video overlay composition object to hold one or more
315 * overlay rectangles.
317 * Returns: (transfer full): a new #GstVideoOverlayComposition. Unref with
318 * gst_video_overlay_composition_unref() when no longer needed.
320 GstVideoOverlayComposition *
321 gst_video_overlay_composition_new (GstVideoOverlayRectangle * rectangle)
323 GstVideoOverlayComposition *comp;
326 /* FIXME: should we allow empty compositions? Could also be expressed as
327 * buffer without a composition on it. Maybe there are cases where doing
328 * an empty new + _add() in a loop is easier? */
329 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
331 comp = g_slice_new0 (GstVideoOverlayComposition);
333 gst_mini_object_init (GST_MINI_OBJECT_CAST (comp), 0,
334 GST_TYPE_VIDEO_OVERLAY_COMPOSITION,
335 (GstMiniObjectCopyFunction) gst_video_overlay_composition_copy,
336 NULL, (GstMiniObjectFreeFunction) gst_video_overlay_composition_free);
338 comp->rectangles = g_new0 (GstVideoOverlayRectangle *, RECTANGLE_ARRAY_STEP);
339 comp->rectangles[0] = gst_video_overlay_rectangle_ref (rectangle);
340 comp->num_rectangles = 1;
342 comp->seq_num = gst_video_overlay_get_seqnum ();
344 /* since the rectangle was created earlier, its seqnum is smaller than ours */
345 comp->min_seq_num_used = rectangle->seq_num;
347 GST_LOG ("new composition %p: seq_num %u with rectangle %p", comp,
348 comp->seq_num, rectangle);
354 * gst_video_overlay_composition_add_rectangle:
355 * @comp: a #GstVideoOverlayComposition
356 * @rectangle: (transfer none): a #GstVideoOverlayRectangle to add to the
359 * Adds an overlay rectangle to an existing overlay composition object. This
360 * must be done right after creating the overlay composition.
363 gst_video_overlay_composition_add_rectangle (GstVideoOverlayComposition * comp,
364 GstVideoOverlayRectangle * rectangle)
366 g_return_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp));
367 g_return_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle));
368 g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (comp) == 1);
370 if (comp->num_rectangles % RECTANGLE_ARRAY_STEP == 0) {
372 g_renew (GstVideoOverlayRectangle *, comp->rectangles,
373 comp->num_rectangles + RECTANGLE_ARRAY_STEP);
376 comp->rectangles[comp->num_rectangles] =
377 gst_video_overlay_rectangle_ref (rectangle);
378 comp->num_rectangles += 1;
380 comp->min_seq_num_used = MIN (comp->min_seq_num_used, rectangle->seq_num);
382 GST_LOG ("composition %p: added rectangle %p", comp, rectangle);
386 * gst_video_overlay_composition_n_rectangles:
387 * @comp: a #GstVideoOverlayComposition
389 * Returns the number of #GstVideoOverlayRectangle<!-- -->s contained in @comp.
391 * Returns: the number of rectangles
394 gst_video_overlay_composition_n_rectangles (GstVideoOverlayComposition * comp)
396 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), 0);
398 return comp->num_rectangles;
402 * gst_video_overlay_composition_get_rectangle:
403 * @comp: a #GstVideoOverlayComposition
404 * @n: number of the rectangle to get
406 * Returns the @n-th #GstVideoOverlayRectangle contained in @comp.
408 * Returns: (transfer none): the @n-th rectangle, or NULL if @n is out of
409 * bounds. Will not return a new reference, the caller will need to
410 * obtain her own reference using gst_video_overlay_rectangle_ref()
413 GstVideoOverlayRectangle *
414 gst_video_overlay_composition_get_rectangle (GstVideoOverlayComposition * comp,
417 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), NULL);
419 if (n >= comp->num_rectangles)
422 return comp->rectangles[n];
426 gst_video_overlay_rectangle_needs_scaling (GstVideoOverlayRectangle * r)
428 return (GST_VIDEO_INFO_WIDTH (&r->info) != r->render_width ||
429 GST_VIDEO_INFO_HEIGHT (&r->info) != r->render_height);
433 * gst_video_overlay_composition_blend:
434 * @comp: a #GstVideoOverlayComposition
435 * @video_buf: a #GstVideoFrame containing raw video data in a supported format
437 * Blends the overlay rectangles in @comp on top of the raw video data
438 * contained in @video_buf. The data in @video_buf must be writable and
439 * mapped appropriately.
442 gst_video_overlay_composition_blend (GstVideoOverlayComposition * comp,
443 GstVideoFrame * video_buf)
445 GstVideoInfo scaled_info;
447 GstVideoFrame rectangle_frame;
449 GstBuffer *pixels = NULL;
454 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), FALSE);
455 g_return_val_if_fail (video_buf != NULL, FALSE);
457 w = GST_VIDEO_FRAME_WIDTH (video_buf);
458 h = GST_VIDEO_FRAME_HEIGHT (video_buf);
459 fmt = GST_VIDEO_FRAME_FORMAT (video_buf);
461 num = comp->num_rectangles;
462 GST_LOG ("Blending composition %p with %u rectangles onto video buffer %p "
463 "(%ux%u, format %u)", comp, num, video_buf, w, h, fmt);
465 for (n = 0; n < num; ++n) {
466 GstVideoOverlayRectangle *rect;
467 gboolean needs_scaling;
469 rect = comp->rectangles[n];
471 GST_LOG (" rectangle %u %p: %ux%u, format %u", n, rect,
472 GST_VIDEO_INFO_WIDTH (&rect->info), GST_VIDEO_INFO_HEIGHT (&rect->info),
473 GST_VIDEO_INFO_FORMAT (&rect->info));
475 needs_scaling = gst_video_overlay_rectangle_needs_scaling (rect);
477 gst_video_blend_scale_linear_RGBA (&rect->info, rect->pixels,
478 rect->render_height, rect->render_width, &scaled_info, &pixels);
479 vinfo = &scaled_info;
481 pixels = gst_buffer_ref (rect->pixels);
485 gst_video_frame_map (&rectangle_frame, vinfo, pixels, GST_MAP_READ);
487 ret = gst_video_blend (video_buf, &rectangle_frame, rect->x, rect->y,
489 gst_video_frame_unmap (&rectangle_frame);
491 GST_WARNING ("Could not blend overlay rectangle onto video buffer");
494 /* FIXME: should cache scaled pixels in the rectangle struct */
495 gst_buffer_unref (pixels);
502 * gst_video_overlay_composition_copy:
503 * @comp: (transfer none): a #GstVideoOverlayComposition to copy
505 * Makes a copy of @comp and all contained rectangles, so that it is possible
506 * to modify the composition and contained rectangles (e.g. add additional
507 * rectangles or change the render co-ordinates or render dimension). The
508 * actual overlay pixel data buffers contained in the rectangles are not
511 * Returns: (transfer full): a new #GstVideoOverlayComposition equivalent
514 GstVideoOverlayComposition *
515 gst_video_overlay_composition_copy (GstVideoOverlayComposition * comp)
517 GstVideoOverlayComposition *copy;
518 GstVideoOverlayRectangle *rect;
521 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), NULL);
523 if (G_LIKELY (comp->num_rectangles == 0))
524 return gst_video_overlay_composition_new (NULL);
526 rect = gst_video_overlay_rectangle_copy (comp->rectangles[0]);
527 copy = gst_video_overlay_composition_new (rect);
528 gst_video_overlay_rectangle_unref (rect);
530 for (n = 1; n < comp->num_rectangles; ++n) {
531 rect = gst_video_overlay_rectangle_copy (comp->rectangles[n]);
532 gst_video_overlay_composition_add_rectangle (copy, rect);
533 gst_video_overlay_rectangle_unref (rect);
540 * gst_video_overlay_composition_make_writable:
541 * @comp: (transfer full): a #GstVideoOverlayComposition to copy
543 * Takes ownership of @comp and returns a version of @comp that is writable
544 * (i.e. can be modified). Will either return @comp right away, or create a
545 * new writable copy of @comp and unref @comp itself. All the contained
546 * rectangles will also be copied, but the actual overlay pixel data buffers
547 * contained in the rectangles are not copied.
549 * Returns: (transfer full): a writable #GstVideoOverlayComposition
550 * equivalent to @comp.
552 GstVideoOverlayComposition *
553 gst_video_overlay_composition_make_writable (GstVideoOverlayComposition * comp)
555 GstVideoOverlayComposition *writable_comp;
557 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), NULL);
559 if (GST_MINI_OBJECT_REFCOUNT_VALUE (comp) == 1) {
562 for (n = 0; n < comp->num_rectangles; ++n) {
563 if (GST_MINI_OBJECT_REFCOUNT_VALUE (comp->rectangles[n]) != 1)
571 writable_comp = gst_video_overlay_composition_copy (comp);
572 gst_video_overlay_composition_unref (comp);
574 return writable_comp;
578 * gst_video_overlay_composition_get_seqnum:
579 * @comp: a #GstVideoOverlayComposition
581 * Returns the sequence number of this composition. Sequence numbers are
582 * monotonically increasing and unique for overlay compositions and rectangles
583 * (meaning there will never be a rectangle with the same sequence number as
586 * Returns: the sequence number of @comp
589 gst_video_overlay_composition_get_seqnum (GstVideoOverlayComposition * comp)
591 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), 0);
593 return comp->seq_num;
596 /* ------------------------------ rectangles ------------------------------ -*/
598 GST_DEFINE_MINI_OBJECT_TYPE (GstVideoOverlayRectangle,
599 gst_video_overlay_rectangle);
602 gst_video_overlay_rectangle_free (GstMiniObject * mini_obj)
604 GstVideoOverlayRectangle *rect = (GstVideoOverlayRectangle *) mini_obj;
606 gst_buffer_replace (&rect->pixels, NULL);
608 while (rect->scaled_rectangles != NULL) {
609 GstVideoOverlayRectangle *scaled_rect = rect->scaled_rectangles->data;
611 gst_video_overlay_rectangle_unref (scaled_rect);
613 rect->scaled_rectangles =
614 g_list_delete_link (rect->scaled_rectangles, rect->scaled_rectangles);
617 g_free (rect->initial_alpha);
618 g_mutex_clear (&rect->lock);
620 g_slice_free (GstVideoOverlayRectangle, rect);
623 static inline gboolean
624 gst_video_overlay_rectangle_check_flags (GstVideoOverlayFormatFlags flags)
626 /* Check flags only contains flags we know about */
627 return (flags & ~(GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA |
628 GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA)) == 0;
632 gst_video_overlay_rectangle_is_same_alpha_type (GstVideoOverlayFormatFlags
633 flags1, GstVideoOverlayFormatFlags flags2)
635 return ((flags1 ^ flags2) & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA)
641 * gst_video_overlay_rectangle_new_raw:
642 * @pixels: (transfer none): a #GstBuffer pointing to the pixel memory
643 * @render_x: the X co-ordinate on the video where the top-left corner of this
644 * overlay rectangle should be rendered to
645 * @render_y: the Y co-ordinate on the video where the top-left corner of this
646 * overlay rectangle should be rendered to
647 * @render_width: the render width of this rectangle on the video
648 * @render_height: the render height of this rectangle on the video
651 * Creates a new video overlay rectangle with ARGB or AYUV pixel data.
652 * The layout in case of ARGB of the components in memory is B-G-R-A
653 * on little-endian platforms
654 * (corresponding to #GST_VIDEO_FORMAT_BGRA) and A-R-G-B on big-endian
655 * platforms (corresponding to #GST_VIDEO_FORMAT_ARGB). In other words,
656 * pixels are treated as 32-bit words and the lowest 8 bits then contain
657 * the blue component value and the highest 8 bits contain the alpha
658 * component value. Unless specified in the flags, the RGB values are
659 * non-premultiplied. This is the format that is used by most hardware,
660 * and also many rendering libraries such as Cairo, for example.
661 * The pixel data buffer must have #GstVideoMeta set.
663 * Returns: (transfer full): a new #GstVideoOverlayRectangle. Unref with
664 * gst_video_overlay_rectangle_unref() when no longer needed.
666 GstVideoOverlayRectangle *
667 gst_video_overlay_rectangle_new_raw (GstBuffer * pixels,
668 gint render_x, gint render_y, guint render_width, guint render_height,
669 GstVideoOverlayFormatFlags flags)
671 GstVideoOverlayRectangle *rect;
673 GstVideoFormat format;
676 g_return_val_if_fail (GST_IS_BUFFER (pixels), NULL);
677 g_return_val_if_fail (render_height > 0 && render_width > 0, NULL);
678 g_return_val_if_fail (gst_video_overlay_rectangle_check_flags (flags), NULL);
680 /* buffer must have video meta with some expected settings */
681 vmeta = gst_buffer_get_video_meta (pixels);
682 g_return_val_if_fail (vmeta, NULL);
683 g_return_val_if_fail (vmeta->format ==
684 GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB ||
685 vmeta->format == GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV, NULL);
686 g_return_val_if_fail (vmeta->flags == GST_VIDEO_FRAME_FLAG_NONE, NULL);
688 format = vmeta->format;
689 width = vmeta->width;
690 height = vmeta->height;
692 /* technically ((height-1)*stride)+width might be okay too */
693 g_return_val_if_fail (gst_buffer_get_size (pixels) >= height * width * 4,
695 g_return_val_if_fail (height > 0 && width > 0, NULL);
697 rect = g_slice_new0 (GstVideoOverlayRectangle);
699 gst_mini_object_init (GST_MINI_OBJECT_CAST (rect), 0,
700 GST_TYPE_VIDEO_OVERLAY_RECTANGLE,
701 (GstMiniObjectCopyFunction) gst_video_overlay_rectangle_copy,
702 NULL, (GstMiniObjectFreeFunction) gst_video_overlay_rectangle_free);
704 g_mutex_init (&rect->lock);
706 rect->pixels = gst_buffer_ref (pixels);
707 rect->scaled_rectangles = NULL;
709 gst_video_info_init (&rect->info);
710 gst_video_info_set_format (&rect->info, format, width, height);
711 if (flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA)
712 rect->info.flags |= GST_VIDEO_FLAG_PREMULTIPLIED_ALPHA;
716 rect->render_width = render_width;
717 rect->render_height = render_height;
719 rect->global_alpha = 1.0;
720 rect->applied_global_alpha = 1.0;
721 rect->initial_alpha = NULL;
725 rect->seq_num = gst_video_overlay_get_seqnum ();
727 GST_LOG ("new rectangle %p: %ux%u => %ux%u @ %u,%u, seq_num %u, format %u, "
728 "flags %x, pixels %p, global_alpha=%f", rect, width, height, render_width,
729 render_height, render_x, render_y, rect->seq_num, format,
730 rect->flags, pixels, rect->global_alpha);
736 * gst_video_overlay_rectangle_get_render_rectangle:
737 * @rectangle: a #GstVideoOverlayRectangle
738 * @render_x: (out) (allow-none): address where to store the X render offset
739 * @render_y: (out) (allow-none): address where to store the Y render offset
740 * @render_width: (out) (allow-none): address where to store the render width
741 * @render_height: (out) (allow-none): address where to store the render height
743 * Retrieves the render position and render dimension of the overlay
744 * rectangle on the video.
746 * Returns: TRUE if valid render dimensions were retrieved.
749 gst_video_overlay_rectangle_get_render_rectangle (GstVideoOverlayRectangle *
750 rectangle, gint * render_x, gint * render_y, guint * render_width,
751 guint * render_height)
753 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), FALSE);
756 *render_x = rectangle->x;
758 *render_y = rectangle->y;
760 *render_width = rectangle->render_width;
762 *render_height = rectangle->render_height;
768 * gst_video_overlay_rectangle_set_render_rectangle:
769 * @rectangle: a #GstVideoOverlayRectangle
770 * @render_x: render X position of rectangle on video
771 * @render_y: render Y position of rectangle on video
772 * @render_width: render width of rectangle
773 * @render_height: render height of rectangle
775 * Sets the render position and dimensions of the rectangle on the video.
776 * This function is mainly for elements that modify the size of the video
777 * in some way (e.g. through scaling or cropping) and need to adjust the
778 * details of any overlays to match the operation that changed the size.
780 * @rectangle must be writable, meaning its refcount must be 1. You can
781 * make the rectangles inside a #GstVideoOverlayComposition writable using
782 * gst_video_overlay_composition_make_writable() or
783 * gst_video_overlay_composition_copy().
786 gst_video_overlay_rectangle_set_render_rectangle (GstVideoOverlayRectangle *
787 rectangle, gint render_x, gint render_y, guint render_width,
790 g_return_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle));
791 g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (rectangle) == 1);
793 rectangle->x = render_x;
794 rectangle->y = render_y;
795 rectangle->render_width = render_width;
796 rectangle->render_height = render_height;
801 gst_video_overlay_rectangle_premultiply_0 (GstVideoFrame * frame)
804 for (j = 0; j < GST_VIDEO_FRAME_HEIGHT (frame); ++j) {
807 line = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
808 line += GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0) * j;
809 for (i = 0; i < GST_VIDEO_FRAME_WIDTH (frame); ++i) {
811 line[1] = line[1] * a / 255;
812 line[2] = line[2] * a / 255;
813 line[3] = line[3] * a / 255;
820 gst_video_overlay_rectangle_premultiply_3 (GstVideoFrame * frame)
823 for (j = 0; j < GST_VIDEO_FRAME_HEIGHT (frame); ++j) {
826 line = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
827 line += GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0) * j;
828 for (i = 0; i < GST_VIDEO_FRAME_WIDTH (frame); ++i) {
830 line[0] = line[0] * a / 255;
831 line[1] = line[1] * a / 255;
832 line[2] = line[2] * a / 255;
839 gst_video_overlay_rectangle_premultiply (GstVideoFrame * frame)
843 alpha_offset = GST_VIDEO_FRAME_COMP_POFFSET (frame, 3);
844 switch (alpha_offset) {
846 gst_video_overlay_rectangle_premultiply_0 (frame);
849 gst_video_overlay_rectangle_premultiply_3 (frame);
852 g_assert_not_reached ();
859 gst_video_overlay_rectangle_unpremultiply_0 (GstVideoFrame * frame)
862 for (j = 0; j < GST_VIDEO_FRAME_HEIGHT (frame); ++j) {
865 line = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
866 line += GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0) * j;
867 for (i = 0; i < GST_VIDEO_FRAME_WIDTH (frame); ++i) {
870 line[1] = MIN ((line[1] * 255 + a / 2) / a, 255);
871 line[2] = MIN ((line[2] * 255 + a / 2) / a, 255);
872 line[3] = MIN ((line[3] * 255 + a / 2) / a, 255);
880 gst_video_overlay_rectangle_unpremultiply_3 (GstVideoFrame * frame)
883 for (j = 0; j < GST_VIDEO_FRAME_HEIGHT (frame); ++j) {
886 line = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
887 line += GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0) * j;
888 for (i = 0; i < GST_VIDEO_FRAME_WIDTH (frame); ++i) {
891 line[0] = MIN ((line[0] * 255 + a / 2) / a, 255);
892 line[1] = MIN ((line[1] * 255 + a / 2) / a, 255);
893 line[2] = MIN ((line[2] * 255 + a / 2) / a, 255);
901 gst_video_overlay_rectangle_unpremultiply (GstVideoFrame * frame)
905 alpha_offset = GST_VIDEO_FRAME_COMP_POFFSET (frame, 3);
906 switch (alpha_offset) {
908 gst_video_overlay_rectangle_unpremultiply_0 (frame);
911 gst_video_overlay_rectangle_unpremultiply_3 (frame);
914 g_assert_not_reached ();
921 gst_video_overlay_rectangle_extract_alpha (GstVideoOverlayRectangle * rect)
925 gint i, j, w, h, stride, alpha_offset;
927 alpha_offset = GST_VIDEO_INFO_COMP_POFFSET (&rect->info, 3);
928 g_return_if_fail (alpha_offset == 0 || alpha_offset == 3);
930 gst_video_frame_map (&frame, &rect->info, rect->pixels, GST_MAP_READ);
931 src = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
932 w = GST_VIDEO_INFO_WIDTH (&rect->info);
933 h = GST_VIDEO_INFO_HEIGHT (&rect->info);
934 stride = GST_VIDEO_INFO_PLANE_STRIDE (&rect->info, 0);
936 g_free (rect->initial_alpha);
937 rect->initial_alpha = g_malloc (w * h);
938 dst = rect->initial_alpha;
940 for (i = 0; i < h; i++) {
941 for (j = 0; j < w; j++) {
942 *dst = src[alpha_offset];
946 src += stride - 4 * w;
948 gst_video_frame_unmap (&frame);
953 gst_video_overlay_rectangle_apply_global_alpha (GstVideoOverlayRectangle * rect,
958 gint i, j, w, h, stride;
959 gint argb_a, argb_r, argb_g, argb_b;
961 g_assert (!(rect->applied_global_alpha != 1.0
962 && rect->initial_alpha == NULL));
964 if (global_alpha == rect->applied_global_alpha)
967 if (rect->initial_alpha == NULL)
968 gst_video_overlay_rectangle_extract_alpha (rect);
970 src = rect->initial_alpha;
971 rect->pixels = gst_buffer_make_writable (rect->pixels);
973 gst_video_frame_map (&frame, &rect->info, rect->pixels, GST_MAP_READ);
974 dst = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
975 w = GST_VIDEO_INFO_WIDTH (&rect->info);
976 h = GST_VIDEO_INFO_HEIGHT (&rect->info);
977 stride = GST_VIDEO_INFO_PLANE_STRIDE (&rect->info, 0);
979 argb_a = GST_VIDEO_INFO_COMP_POFFSET (&rect->info, 3);
980 argb_r = (argb_a + 1) % 4;
981 argb_g = (argb_a + 2) % 4;
982 argb_b = (argb_a + 3) % 4;
984 for (i = 0; i < h; i++) {
985 for (j = 0; j < w; j++) {
986 guint8 na = (guint8) (*src * global_alpha);
988 if (! !(rect->flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA)) {
990 (guint8) ((double) (dst[argb_r] * 255) / (double) dst[argb_a]) *
993 (guint8) ((double) (dst[argb_g] * 255) / (double) dst[argb_a]) *
996 (guint8) ((double) (dst[argb_b] * 255) / (double) dst[argb_a]) *
1003 dst += stride - 4 * w;
1005 gst_video_frame_unmap (&frame);
1007 rect->applied_global_alpha = global_alpha;
1011 gst_video_overlay_rectangle_convert (GstVideoInfo * src, GstBuffer * src_buffer,
1012 GstVideoFormat dest_format, GstVideoInfo * dest, GstBuffer ** dest_buffer)
1014 gint width, height, stride;
1015 GstVideoFrame src_frame, dest_frame;
1016 GstVideoFormat format;
1018 guint8 *sdata, *ddata;
1020 format = GST_VIDEO_INFO_FORMAT (src);
1022 width = GST_VIDEO_INFO_WIDTH (src);
1023 height = GST_VIDEO_INFO_HEIGHT (src);
1025 gst_video_info_init (dest);
1026 gst_video_info_set_format (dest, dest_format, width, height);
1028 *dest_buffer = gst_buffer_new_and_alloc (GST_VIDEO_INFO_SIZE (dest));
1030 gst_video_frame_map (&src_frame, src, src_buffer, GST_MAP_READ);
1031 gst_video_frame_map (&dest_frame, dest, *dest_buffer, GST_MAP_WRITE);
1033 sdata = GST_VIDEO_FRAME_PLANE_DATA (&src_frame, 0);
1034 ddata = GST_VIDEO_FRAME_PLANE_DATA (&dest_frame, 0);
1035 stride = GST_VIDEO_FRAME_PLANE_STRIDE (&src_frame, 0);
1037 if (format == GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV &&
1038 dest_format == GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB) {
1040 gint a, y, u, v, r, g, b;
1042 for (k = 0; k < height; k++) {
1043 for (l = 0; l < width; l++) {
1044 ayuv = GST_READ_UINT32_BE (sdata);
1046 y = (ayuv >> 16) & 0xff;
1047 u = (ayuv >> 8) & 0xff;
1050 r = (298 * y + 459 * v - 63514) >> 8;
1051 g = (298 * y - 55 * u - 136 * v + 19681) >> 8;
1052 b = (298 * y + 541 * u - 73988) >> 8;
1054 r = CLAMP (r, 0, 255);
1055 g = CLAMP (g, 0, 255);
1056 b = CLAMP (b, 0, 255);
1058 /* native endian ARGB */
1059 *ddata = ((a << 24) | (r << 16) | (g << 8) | b);
1064 sdata += stride - 4 * width;
1066 } else if (format == GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB &&
1067 dest_format == GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV) {
1069 gint a, y, u, v, r, g, b;
1071 for (k = 0; k < height; k++) {
1072 for (l = 0; l < width; l++) {
1073 /* native endian ARGB */
1076 r = (argb >> 16) & 0xff;
1077 g = (argb >> 8) & 0xff;
1080 y = (47 * r + 157 * g + 16 * b + 4096) >> 8;
1081 u = (-26 * r - 87 * g + 112 * b + 32768) >> 8;
1082 v = (112 * r - 102 * g - 10 * b + 32768) >> 8;
1084 y = CLAMP (y, 0, 255);
1085 u = CLAMP (u, 0, 255);
1086 v = CLAMP (v, 0, 255);
1088 GST_WRITE_UINT32_BE (ddata, ((a << 24) | (y << 16) | (u << 8) | v));
1093 sdata += stride - 4 * width;
1096 GST_ERROR ("unsupported conversion");
1097 g_assert_not_reached ();
1100 gst_video_frame_unmap (&src_frame);
1101 gst_video_frame_unmap (&dest_frame);
1105 gst_video_overlay_rectangle_get_pixels_raw_internal (GstVideoOverlayRectangle *
1106 rectangle, GstVideoOverlayFormatFlags flags, gboolean unscaled,
1107 GstVideoFormat wanted_format)
1109 GstVideoOverlayFormatFlags new_flags;
1110 GstVideoOverlayRectangle *scaled_rect = NULL, *conv_rect = NULL;
1112 GstVideoFrame frame;
1115 guint width, height;
1117 guint wanted_height;
1118 gboolean apply_global_alpha;
1119 gboolean revert_global_alpha;
1120 GstVideoFormat format;
1122 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1123 g_return_val_if_fail (gst_video_overlay_rectangle_check_flags (flags), NULL);
1125 width = GST_VIDEO_INFO_WIDTH (&rectangle->info);
1126 height = GST_VIDEO_INFO_HEIGHT (&rectangle->info);
1127 wanted_width = unscaled ? width : rectangle->render_width;
1128 wanted_height = unscaled ? height : rectangle->render_height;
1129 format = GST_VIDEO_INFO_FORMAT (&rectangle->info);
1131 apply_global_alpha =
1132 (! !(rectangle->flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA)
1133 && !(flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA));
1134 revert_global_alpha =
1135 (! !(rectangle->flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA)
1136 && ! !(flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA));
1138 /* This assumes we don't need to adjust the format */
1139 if (wanted_width == width &&
1140 wanted_height == height &&
1141 wanted_format == format &&
1142 gst_video_overlay_rectangle_is_same_alpha_type (rectangle->flags,
1144 /* don't need to apply/revert global-alpha either: */
1145 if ((!apply_global_alpha
1146 || rectangle->applied_global_alpha == rectangle->global_alpha)
1147 && (!revert_global_alpha || rectangle->applied_global_alpha == 1.0)) {
1148 return rectangle->pixels;
1150 /* only apply/revert global-alpha */
1151 scaled_rect = rectangle;
1156 /* see if we've got one cached already */
1157 GST_RECTANGLE_LOCK (rectangle);
1158 for (l = rectangle->scaled_rectangles; l != NULL; l = l->next) {
1159 GstVideoOverlayRectangle *r = l->data;
1161 if (GST_VIDEO_INFO_WIDTH (&r->info) == wanted_width &&
1162 GST_VIDEO_INFO_HEIGHT (&r->info) == wanted_height &&
1163 GST_VIDEO_INFO_FORMAT (&r->info) == wanted_format &&
1164 gst_video_overlay_rectangle_is_same_alpha_type (r->flags, flags)) {
1165 /* we'll keep these rectangles around until finalize, so it's ok not
1166 * to take our own ref here */
1171 GST_RECTANGLE_UNLOCK (rectangle);
1173 if (scaled_rect != NULL)
1176 /* maybe have one in the right format though */
1177 if (format != wanted_format) {
1178 GST_RECTANGLE_LOCK (rectangle);
1179 for (l = rectangle->scaled_rectangles; l != NULL; l = l->next) {
1180 GstVideoOverlayRectangle *r = l->data;
1182 if (GST_VIDEO_INFO_FORMAT (&r->info) == wanted_format &&
1183 gst_video_overlay_rectangle_is_same_alpha_type (r->flags, flags)) {
1184 /* we'll keep these rectangles around until finalize, so it's ok not
1185 * to take our own ref here */
1190 GST_RECTANGLE_UNLOCK (rectangle);
1192 conv_rect = rectangle;
1195 if (conv_rect == NULL) {
1196 GstVideoInfo conv_info;
1198 gst_video_overlay_rectangle_convert (&rectangle->info, rectangle->pixels,
1199 wanted_format, &conv_info, &buf);
1200 gst_buffer_add_video_meta (buf, GST_VIDEO_FRAME_FLAG_NONE,
1201 GST_VIDEO_INFO_FORMAT (&conv_info), width, height);
1202 conv_rect = gst_video_overlay_rectangle_new_raw (buf,
1203 0, 0, width, height, rectangle->flags);
1204 if (rectangle->global_alpha != 1.0)
1205 gst_video_overlay_rectangle_set_global_alpha (scaled_rect,
1206 rectangle->global_alpha);
1207 gst_buffer_unref (buf);
1208 /* keep this converted one around as well in any case */
1209 GST_RECTANGLE_LOCK (rectangle);
1210 rectangle->scaled_rectangles =
1211 g_list_prepend (rectangle->scaled_rectangles, conv_rect);
1212 GST_RECTANGLE_UNLOCK (rectangle);
1215 /* now we continue from conv_rect */
1216 width = GST_VIDEO_INFO_WIDTH (&conv_rect->info);
1217 height = GST_VIDEO_INFO_HEIGHT (&conv_rect->info);
1218 format = GST_VIDEO_INFO_FORMAT (&conv_rect->info);
1220 /* not cached yet, do the preprocessing and put the result into our cache */
1221 if (wanted_width != width || wanted_height != height) {
1222 GstVideoInfo scaled_info;
1224 /* we could check the cache for a scaled rect with global_alpha == 1 here */
1225 gst_video_blend_scale_linear_RGBA (&conv_rect->info, conv_rect->pixels,
1226 wanted_height, wanted_width, &scaled_info, &buf);
1228 gst_buffer_add_video_meta (buf, GST_VIDEO_FRAME_FLAG_NONE,
1229 GST_VIDEO_INFO_FORMAT (&conv_rect->info), wanted_width, wanted_height);
1230 } else if (!gst_video_overlay_rectangle_is_same_alpha_type (conv_rect->flags,
1232 /* if we don't have to scale, we have to modify the alpha values, so we
1233 * need to make a copy of the pixel memory (and we take ownership below) */
1234 buf = gst_buffer_copy (conv_rect->pixels);
1235 info = conv_rect->info;
1237 /* do not need to scale or modify alpha values, almost done then */
1238 scaled_rect = conv_rect;
1242 new_flags = conv_rect->flags;
1243 gst_video_frame_map (&frame, &info, buf, GST_MAP_READWRITE);
1244 if (!gst_video_overlay_rectangle_is_same_alpha_type (conv_rect->flags, flags)) {
1245 if (rectangle->flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA) {
1246 gst_video_overlay_rectangle_unpremultiply (&frame);
1247 new_flags &= ~GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA;
1249 gst_video_overlay_rectangle_premultiply (&frame);
1250 new_flags |= GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA;
1253 gst_video_frame_unmap (&frame);
1255 scaled_rect = gst_video_overlay_rectangle_new_raw (buf,
1256 0, 0, wanted_width, wanted_height, new_flags);
1257 if (conv_rect->global_alpha != 1.0)
1258 gst_video_overlay_rectangle_set_global_alpha (scaled_rect,
1259 conv_rect->global_alpha);
1260 gst_buffer_unref (buf);
1262 GST_RECTANGLE_LOCK (rectangle);
1263 rectangle->scaled_rectangles =
1264 g_list_prepend (rectangle->scaled_rectangles, scaled_rect);
1265 GST_RECTANGLE_UNLOCK (rectangle);
1269 GST_RECTANGLE_LOCK (rectangle);
1270 if (apply_global_alpha
1271 && scaled_rect->applied_global_alpha != rectangle->global_alpha) {
1272 gst_video_overlay_rectangle_apply_global_alpha (scaled_rect,
1273 rectangle->global_alpha);
1274 gst_video_overlay_rectangle_set_global_alpha (scaled_rect,
1275 rectangle->global_alpha);
1276 } else if (revert_global_alpha && scaled_rect->applied_global_alpha != 1.0) {
1277 gst_video_overlay_rectangle_apply_global_alpha (scaled_rect, 1.0);
1279 GST_RECTANGLE_UNLOCK (rectangle);
1281 return scaled_rect->pixels;
1286 * gst_video_overlay_rectangle_get_pixels_raw:
1287 * @rectangle: a #GstVideoOverlayRectangle
1289 * If a global_alpha value != 1 is set for the rectangle, the caller
1290 * should set the #GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA flag
1291 * if he wants to apply global-alpha himself. If the flag is not set
1292 * global_alpha is applied internally before returning the pixel-data.
1294 * Returns: (transfer none): a #GstBuffer holding the pixel data with
1295 * format as originally provided and specified in video meta with
1296 * width and height of the render dimensions as per
1297 * gst_video_overlay_rectangle_get_render_rectangle(). This function does
1298 * not return a reference, the caller should obtain a reference of her own
1299 * with gst_buffer_ref() if needed.
1302 gst_video_overlay_rectangle_get_pixels_raw (GstVideoOverlayRectangle *
1303 rectangle, GstVideoOverlayFormatFlags flags)
1305 return gst_video_overlay_rectangle_get_pixels_raw_internal (rectangle,
1306 flags, FALSE, GST_VIDEO_INFO_FORMAT (&rectangle->info));
1310 * gst_video_overlay_rectangle_get_pixels_argb:
1311 * @rectangle: a #GstVideoOverlayRectangle
1313 * If a global_alpha value != 1 is set for the rectangle, the caller
1314 * should set the #GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA flag
1315 * if he wants to apply global-alpha himself. If the flag is not set
1316 * global_alpha is applied internally before returning the pixel-data.
1318 * Returns: (transfer none): a #GstBuffer holding the ARGB pixel data with
1319 * width and height of the render dimensions as per
1320 * gst_video_overlay_rectangle_get_render_rectangle(). This function does
1321 * not return a reference, the caller should obtain a reference of her own
1322 * with gst_buffer_ref() if needed.
1325 gst_video_overlay_rectangle_get_pixels_argb (GstVideoOverlayRectangle *
1326 rectangle, GstVideoOverlayFormatFlags flags)
1328 return gst_video_overlay_rectangle_get_pixels_raw_internal (rectangle,
1329 flags, FALSE, GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB);
1333 * gst_video_overlay_rectangle_get_pixels_ayuv:
1334 * @rectangle: a #GstVideoOverlayRectangle
1336 * If a global_alpha value != 1 is set for the rectangle, the caller
1337 * should set the #GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA flag
1338 * if he wants to apply global-alpha himself. If the flag is not set
1339 * global_alpha is applied internally before returning the pixel-data.
1341 * Returns: (transfer none): a #GstBuffer holding the AYUV pixel data with
1342 * width and height of the render dimensions as per
1343 * gst_video_overlay_rectangle_get_render_rectangle(). This function does
1344 * not return a reference, the caller should obtain a reference of her own
1345 * with gst_buffer_ref() if needed.
1348 gst_video_overlay_rectangle_get_pixels_ayuv (GstVideoOverlayRectangle *
1349 rectangle, GstVideoOverlayFormatFlags flags)
1351 return gst_video_overlay_rectangle_get_pixels_raw_internal (rectangle,
1352 flags, FALSE, GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV);
1356 * gst_video_overlay_rectangle_get_pixels_unscaled_raw:
1357 * @rectangle: a #GstVideoOverlayRectangle
1359 * If a global_alpha value != 1 is set for the rectangle, the caller
1360 * should set the #GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA flag
1361 * if he wants to apply global-alpha himself. If the flag is not set
1362 * global_alpha is applied internally before returning the pixel-data.
1364 * Retrieves the pixel data as it is. This is useful if the caller can
1365 * do the scaling itself when handling the overlaying. The rectangle will
1366 * need to be scaled to the render dimensions, which can be retrieved using
1367 * gst_video_overlay_rectangle_get_render_rectangle().
1369 * Returns: (transfer none): a #GstBuffer holding the pixel data with
1370 * #GstVideoMeta set. This function does not return a reference, the caller
1371 * should obtain a reference of her own with gst_buffer_ref() if needed.
1374 gst_video_overlay_rectangle_get_pixels_unscaled_raw (GstVideoOverlayRectangle *
1375 rectangle, GstVideoOverlayFormatFlags flags)
1377 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1379 return gst_video_overlay_rectangle_get_pixels_raw_internal (rectangle,
1380 flags, TRUE, GST_VIDEO_INFO_FORMAT (&rectangle->info));
1384 * gst_video_overlay_rectangle_get_pixels_unscaled_argb:
1385 * @rectangle: a #GstVideoOverlayRectangle
1387 * If a global_alpha value != 1 is set for the rectangle, the caller
1388 * should set the #GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA flag
1389 * if he wants to apply global-alpha himself. If the flag is not set
1390 * global_alpha is applied internally before returning the pixel-data.
1392 * Retrieves the pixel data as it is. This is useful if the caller can
1393 * do the scaling itself when handling the overlaying. The rectangle will
1394 * need to be scaled to the render dimensions, which can be retrieved using
1395 * gst_video_overlay_rectangle_get_render_rectangle().
1397 * Returns: (transfer none): a #GstBuffer holding the ARGB pixel data with
1398 * #GstVideoMeta set. This function does not return a reference, the caller
1399 * should obtain a reference of her own with gst_buffer_ref() if needed.
1402 gst_video_overlay_rectangle_get_pixels_unscaled_argb (GstVideoOverlayRectangle *
1403 rectangle, GstVideoOverlayFormatFlags flags)
1405 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1407 return gst_video_overlay_rectangle_get_pixels_raw_internal (rectangle,
1408 flags, TRUE, GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB);
1412 * gst_video_overlay_rectangle_get_pixels_unscaled_ayuv:
1413 * @rectangle: a #GstVideoOverlayRectangle
1415 * If a global_alpha value != 1 is set for the rectangle, the caller
1416 * should set the #GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA flag
1417 * if he wants to apply global-alpha himself. If the flag is not set
1418 * global_alpha is applied internally before returning the pixel-data.
1420 * Retrieves the pixel data as it is. This is useful if the caller can
1421 * do the scaling itself when handling the overlaying. The rectangle will
1422 * need to be scaled to the render dimensions, which can be retrieved using
1423 * gst_video_overlay_rectangle_get_render_rectangle().
1425 * Returns: (transfer none): a #GstBuffer holding the AYUV pixel data with
1426 * #GstVideoMeta set. This function does not return a reference, the caller
1427 * should obtain a reference of her own with gst_buffer_ref() if needed.
1430 gst_video_overlay_rectangle_get_pixels_unscaled_ayuv (GstVideoOverlayRectangle *
1431 rectangle, GstVideoOverlayFormatFlags flags)
1433 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1435 return gst_video_overlay_rectangle_get_pixels_raw_internal (rectangle,
1436 flags, TRUE, GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV);
1440 * gst_video_overlay_rectangle_get_flags:
1441 * @rectangle: a #GstVideoOverlayRectangle
1443 * Retrieves the flags associated with a #GstVideoOverlayRectangle.
1444 * This is useful if the caller can handle both premultiplied alpha and
1445 * non premultiplied alpha, for example. By knowing whether the rectangle
1446 * uses premultiplied or not, it can request the pixel data in the format
1447 * it is stored in, to avoid unnecessary conversion.
1449 * Returns: the #GstVideoOverlayFormatFlags associated with the rectangle.
1451 GstVideoOverlayFormatFlags
1452 gst_video_overlay_rectangle_get_flags (GstVideoOverlayRectangle * rectangle)
1454 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle),
1455 GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1457 return rectangle->flags;
1461 * gst_video_overlay_rectangle_get_global_alpha:
1462 * @rectangle: a #GstVideoOverlayRectangle
1464 * Retrieves the global-alpha value associated with a #GstVideoOverlayRectangle.
1466 * Returns: the global-alpha value associated with the rectangle.
1469 gst_video_overlay_rectangle_get_global_alpha (GstVideoOverlayRectangle *
1472 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), -1);
1474 return rectangle->global_alpha;
1478 * gst_video_overlay_rectangle_set_global_alpha:
1479 * @rectangle: a #GstVideoOverlayRectangle
1481 * Sets the global alpha value associated with a #GstVideoOverlayRectangle. Per-
1482 * pixel alpha values are multiplied with this value. Valid
1483 * values: 0 <= global_alpha <= 1; 1 to deactivate.
1485 # @rectangle must be writable, meaning its refcount must be 1. You can
1486 * make the rectangles inside a #GstVideoOverlayComposition writable using
1487 * gst_video_overlay_composition_make_writable() or
1488 * gst_video_overlay_composition_copy().
1491 gst_video_overlay_rectangle_set_global_alpha (GstVideoOverlayRectangle *
1492 rectangle, gfloat global_alpha)
1494 g_return_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle));
1495 g_return_if_fail (global_alpha >= 0 && global_alpha <= 1);
1497 if (rectangle->global_alpha != global_alpha) {
1498 rectangle->global_alpha = global_alpha;
1499 if (global_alpha != 1)
1500 rectangle->flags |= GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA;
1502 rectangle->flags &= ~GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA;
1503 /* update seq_num automatically to signal the consumer, that data has changed
1504 * note, that this might mislead renderers, that can handle global-alpha
1505 * themselves, because what they want to know is whether the actual pixel data
1507 rectangle->seq_num = gst_video_overlay_get_seqnum ();
1512 * gst_video_overlay_rectangle_copy:
1513 * @rectangle: (transfer none): a #GstVideoOverlayRectangle to copy
1515 * Makes a copy of @rectangle, so that it is possible to modify it
1516 * (e.g. to change the render co-ordinates or render dimension). The
1517 * actual overlay pixel data buffers contained in the rectangle are not
1520 * Returns: (transfer full): a new #GstVideoOverlayRectangle equivalent
1523 GstVideoOverlayRectangle *
1524 gst_video_overlay_rectangle_copy (GstVideoOverlayRectangle * rectangle)
1526 GstVideoOverlayRectangle *copy;
1528 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1530 copy = gst_video_overlay_rectangle_new_raw (rectangle->pixels,
1531 rectangle->x, rectangle->y,
1532 rectangle->render_width, rectangle->render_height, rectangle->flags);
1533 if (rectangle->global_alpha != 1)
1534 gst_video_overlay_rectangle_set_global_alpha (copy,
1535 rectangle->global_alpha);
1541 * gst_video_overlay_rectangle_get_seqnum:
1542 * @rectangle: a #GstVideoOverlayRectangle
1544 * Returns the sequence number of this rectangle. Sequence numbers are
1545 * monotonically increasing and unique for overlay compositions and rectangles
1546 * (meaning there will never be a rectangle with the same sequence number as
1549 * Using the sequence number of a rectangle as an indicator for changed
1550 * pixel-data of a rectangle is dangereous. Some API calls, like e.g.
1551 * gst_video_overlay_rectangle_set_global_alpha(), automatically update
1552 * the per rectangle sequence number, which is misleading for renderers/
1553 * consumers, that handle global-alpha themselves. For them the
1554 * pixel-data returned by gst_video_overlay_rectangle_get_pixels_*()
1555 * wont be different for different global-alpha values. In this case a
1556 * renderer could also use the GstBuffer pointers as a hint for changed
1559 * Returns: the sequence number of @rectangle
1562 gst_video_overlay_rectangle_get_seqnum (GstVideoOverlayRectangle * rectangle)
1564 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), 0);
1566 return rectangle->seq_num;