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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, 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.
62 * - provide accessors for seq_num and other fields (as needed)
63 * - allow overlay to set/get original pango markup string on/from rectangle
70 #include "video-overlay-composition.h"
71 #include "video-blend.h"
74 struct _GstVideoOverlayComposition
79 GstVideoOverlayRectangle **rectangles;
81 /* lowest rectangle sequence number still used by the upstream
82 * overlay element. This way a renderer maintaining some kind of
83 * rectangles <-> surface cache can know when to free cached
84 * surfaces/rectangles. */
85 guint min_seq_num_used;
87 /* sequence number for the composition (same series as rectangles) */
91 struct _GstVideoOverlayCompositionClass
93 GstMiniObjectClass parent_class;
96 struct _GstVideoOverlayRectangle
100 /* Position on video frame and dimension of output rectangle in
101 * output frame terms (already adjusted for the PAR of the output
102 * frame). x/y can be negative (overlay will be clipped then) */
104 guint render_width, render_height;
106 /* Dimensions of overlay pixels */
107 guint width, height, stride;
109 /* The format of the data in pixels */
110 GstVideoFormat format;
112 /* The flags associated to this rectangle */
113 GstVideoOverlayFormatFlags flags;
115 /* Refcounted blob of memory, no caps or timestamps */
118 /* FIXME: how to express source like text or pango markup?
119 * (just add source type enum + source buffer with data)
121 * FOR 0.10: always send pixel blobs, but attach source data in
122 * addition (reason: if downstream changes, we can't renegotiate
123 * that properly, if we just do a query of supported formats from
124 * the start). Sink will just ignore pixels and use pango markup
125 * from source data if it supports that.
127 * FOR 0.11: overlay should query formats (pango markup, pixels)
128 * supported by downstream and then only send that. We can
129 * renegotiate via the reconfigure event.
132 /* sequence number: useful for backends/renderers/sinks that want
133 * to maintain a cache of rectangles <-> surfaces. The value of
134 * the min_seq_num_used in the composition tells the renderer which
135 * rectangles have expired. */
138 /* global alpha: global alpha value of the rectangle. Each each per-pixel
139 * alpha value of image-data will be multiplied with the global alpha value
141 * Can be used for efficient fading in/out of overlay rectangles.
142 * GstElements that render OverlayCompositions and don't support global alpha
143 * should simply ignore it.*/
146 /* track alpha-values already applied: */
147 gfloat applied_global_alpha;
148 /* store initial per-pixel alpha values: */
149 guint8 *initial_alpha;
151 /* FIXME: we may also need a (private) way to cache converted/scaled
155 GList *scaled_rectangles;
158 struct _GstVideoOverlayRectangleClass
160 GstMiniObjectClass parent_class;
163 #define GST_RECTANGLE_LOCK(rect) g_mutex_lock(&rect->lock)
164 #define GST_RECTANGLE_UNLOCK(rect) g_mutex_unlock(&rect->lock)
166 static void gst_video_overlay_composition_class_init (GstMiniObjectClass * k);
167 static void gst_video_overlay_composition_finalize (GstMiniObject * comp);
168 static void gst_video_overlay_rectangle_class_init (GstMiniObjectClass * klass);
169 static void gst_video_overlay_rectangle_finalize (GstMiniObject * rect);
171 /* --------------------------- utility functions --------------------------- */
173 #ifndef GST_DISABLE_GST_DEBUG
175 #define GST_CAT_DEFAULT ensure_debug_category()
177 static GstDebugCategory *
178 ensure_debug_category (void)
180 static gsize cat_gonce = 0;
182 if (g_once_init_enter (&cat_gonce)) {
185 cat_done = (gsize) _gst_debug_category_new ("video-composition", 0,
186 "video overlay composition");
188 g_once_init_leave (&cat_gonce, cat_done);
191 return (GstDebugCategory *) cat_gonce;
196 #define ensure_debug_category() /* NOOP */
198 #endif /* GST_DISABLE_GST_DEBUG */
201 gst_video_overlay_get_seqnum (void)
203 static gint seqnum; /* 0 */
205 return (guint) g_atomic_int_add (&seqnum, 1);
208 #define GST_OVERLAY_COMPOSITION_QUARK gst_overlay_composition_quark_get()
210 gst_overlay_composition_quark_get (void)
212 static gsize quark_gonce = 0;
214 if (g_once_init_enter (&quark_gonce)) {
217 quark = (gsize) g_quark_from_static_string ("GstVideoOverlayComposition");
219 g_once_init_leave (&quark_gonce, quark);
222 return (GQuark) quark_gonce;
225 #define COMPOSITION_QUARK composition_quark_get()
227 composition_quark_get (void)
229 static gsize quark_gonce = 0;
231 if (g_once_init_enter (&quark_gonce)) {
234 quark = (gsize) g_quark_from_static_string ("composition");
236 g_once_init_leave (&quark_gonce, quark);
239 return (GQuark) quark_gonce;
243 * gst_video_buffer_set_overlay_composition:
245 * @comp: (allow-none): a #GstVideoOverlayComposition, or NULL to clear a
246 * previously-set composition
248 * Sets an overlay composition on a buffer. The buffer will obtain its own
249 * reference to the composition, meaning this function does not take ownership
255 gst_video_buffer_set_overlay_composition (GstBuffer * buf,
256 GstVideoOverlayComposition * comp)
258 gst_buffer_set_qdata (buf, GST_OVERLAY_COMPOSITION_QUARK,
259 gst_structure_id_new (GST_OVERLAY_COMPOSITION_QUARK,
260 COMPOSITION_QUARK, GST_TYPE_VIDEO_OVERLAY_COMPOSITION, comp, NULL));
264 * gst_video_buffer_get_overlay_composition:
267 * Get the overlay composition that has previously been attached to a buffer
268 * with gst_video_buffer_get_overlay_composition(), usually by another element
271 * Returns: (transfer none): the #GstVideoOverlayComposition attached to
272 * this buffer, or NULL. Does not return a reference to the composition,
273 * caller must obtain her own ref via gst_video_overlay_composition_ref()
278 GstVideoOverlayComposition *
279 gst_video_buffer_get_overlay_composition (GstBuffer * buf)
281 const GstStructure *s;
284 s = gst_buffer_get_qdata (buf, GST_OVERLAY_COMPOSITION_QUARK);
288 val = gst_structure_id_get_value (s, COMPOSITION_QUARK);
292 return GST_VIDEO_OVERLAY_COMPOSITION (gst_value_get_mini_object (val));
295 /* ------------------------------ composition ------------------------------ */
297 #define RECTANGLE_ARRAY_STEP 4 /* premature optimization */
300 gst_video_overlay_composition_get_type (void)
302 static volatile gsize type_id = 0;
304 if (g_once_init_enter (&type_id)) {
305 GType new_type_id = g_type_register_static_simple (GST_TYPE_MINI_OBJECT,
306 g_intern_static_string ("GstVideoOverlayComposition"),
307 sizeof (GstVideoOverlayCompositionClass),
308 (GClassInitFunc) gst_video_overlay_composition_class_init,
309 sizeof (GstVideoOverlayComposition),
313 g_once_init_leave (&type_id, new_type_id);
320 gst_video_overlay_composition_finalize (GstMiniObject * mini_obj)
322 GstVideoOverlayComposition *comp = (GstVideoOverlayComposition *) mini_obj;
325 num = comp->num_rectangles;
328 gst_video_overlay_rectangle_unref (comp->rectangles[num - 1]);
332 g_free (comp->rectangles);
333 comp->rectangles = NULL;
334 comp->num_rectangles = 0;
336 /* not chaining up to GstMiniObject's finalize for now, we know it's empty */
340 gst_video_overlay_composition_class_init (GstMiniObjectClass * klass)
342 klass->finalize = gst_video_overlay_composition_finalize;
343 klass->copy = (GstMiniObjectCopyFunction) gst_video_overlay_composition_copy;
347 * gst_video_overlay_composition_new:
348 * @rectangle: (transfer none): a #GstVideoOverlayRectangle to add to the
351 * Creates a new video overlay composition object to hold one or more
352 * overlay rectangles.
354 * Returns: (transfer full): a new #GstVideoOverlayComposition. Unref with
355 * gst_video_overlay_composition_unref() when no longer needed.
359 GstVideoOverlayComposition *
360 gst_video_overlay_composition_new (GstVideoOverlayRectangle * rectangle)
362 GstVideoOverlayComposition *comp;
365 /* FIXME: should we allow empty compositions? Could also be expressed as
366 * buffer without a composition on it. Maybe there are cases where doing
367 * an empty new + _add() in a loop is easier? */
368 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
370 comp = (GstVideoOverlayComposition *)
371 gst_mini_object_new (GST_TYPE_VIDEO_OVERLAY_COMPOSITION);
373 comp->rectangles = g_new0 (GstVideoOverlayRectangle *, RECTANGLE_ARRAY_STEP);
374 comp->rectangles[0] = gst_video_overlay_rectangle_ref (rectangle);
375 comp->num_rectangles = 1;
377 comp->seq_num = gst_video_overlay_get_seqnum ();
379 /* since the rectangle was created earlier, its seqnum is smaller than ours */
380 comp->min_seq_num_used = rectangle->seq_num;
382 GST_LOG ("new composition %p: seq_num %u with rectangle %p", comp,
383 comp->seq_num, rectangle);
389 * gst_video_overlay_composition_add_rectangle:
390 * @comp: a #GstVideoOverlayComposition
391 * @rectangle: (transfer none): a #GstVideoOverlayRectangle to add to the
394 * Adds an overlay rectangle to an existing overlay composition object. This
395 * must be done right after creating the overlay composition.
400 gst_video_overlay_composition_add_rectangle (GstVideoOverlayComposition * comp,
401 GstVideoOverlayRectangle * rectangle)
403 g_return_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp));
404 g_return_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle));
405 g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (comp) == 1);
407 if (comp->num_rectangles % RECTANGLE_ARRAY_STEP == 0) {
409 g_renew (GstVideoOverlayRectangle *, comp->rectangles,
410 comp->num_rectangles + RECTANGLE_ARRAY_STEP);
413 comp->rectangles[comp->num_rectangles] =
414 gst_video_overlay_rectangle_ref (rectangle);
415 comp->num_rectangles += 1;
417 comp->min_seq_num_used = MIN (comp->min_seq_num_used, rectangle->seq_num);
419 GST_LOG ("composition %p: added rectangle %p", comp, rectangle);
423 * gst_video_overlay_composition_n_rectangles:
424 * @comp: a #GstVideoOverlayComposition
426 * Returns the number of #GstVideoOverlayRectangle<!-- -->s contained in @comp.
428 * Returns: the number of rectangles
433 gst_video_overlay_composition_n_rectangles (GstVideoOverlayComposition * comp)
435 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), 0);
437 return comp->num_rectangles;
441 * gst_video_overlay_composition_get_rectangle:
442 * @comp: a #GstVideoOverlayComposition
443 * @n: number of the rectangle to get
445 * Returns the @n-th #GstVideoOverlayRectangle contained in @comp.
447 * Returns: (transfer none): the @n-th rectangle, or NULL if @n is out of
448 * bounds. Will not return a new reference, the caller will need to
449 * obtain her own reference using gst_video_overlay_rectangle_ref()
454 GstVideoOverlayRectangle *
455 gst_video_overlay_composition_get_rectangle (GstVideoOverlayComposition * comp,
458 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), NULL);
460 if (n >= comp->num_rectangles)
463 return comp->rectangles[n];
467 gst_video_overlay_rectangle_needs_scaling (GstVideoOverlayRectangle * r)
469 return (r->width != r->render_width || r->height != r->render_height);
473 * gst_video_overlay_composition_blend:
474 * @comp: a #GstVideoOverlayComposition
475 * @video_buf: a #GstBuffer containing raw video data in a supported format
477 * Blends the overlay rectangles in @comp on top of the raw video data
478 * contained in @video_buf. The data in @video_buf must be writable. If
479 * needed, use gst_buffer_make_writable() before calling this function to
480 * ensure a buffer is writable. @video_buf must also have valid raw video
486 gst_video_overlay_composition_blend (GstVideoOverlayComposition * comp,
487 GstBuffer * video_buf)
489 GstBlendVideoFormatInfo video_info, rectangle_info;
495 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), FALSE);
496 g_return_val_if_fail (GST_IS_BUFFER (video_buf), FALSE);
497 g_return_val_if_fail (gst_buffer_is_writable (video_buf), FALSE);
498 g_return_val_if_fail (GST_BUFFER_CAPS (video_buf) != NULL, FALSE);
500 if (!gst_video_format_parse_caps (GST_BUFFER_CAPS (video_buf), &fmt, &w, &h)) {
501 gchar *str = gst_caps_to_string (GST_BUFFER_CAPS (video_buf));
502 g_warning ("%s: could not parse video buffer caps '%s'", GST_FUNCTION, str);
507 video_blend_format_info_init (&video_info, GST_BUFFER_DATA (video_buf),
510 num = comp->num_rectangles;
511 GST_LOG ("Blending composition %p with %u rectangles onto video buffer %p "
512 "(%ux%u, format %u)", comp, num, video_buf, w, h, fmt);
514 for (n = 0; n < num; ++n) {
515 GstVideoOverlayRectangle *rect;
516 gboolean needs_scaling;
518 rect = comp->rectangles[n];
520 GST_LOG (" rectangle %u %p: %ux%u, format %u", n, rect, rect->height,
521 rect->width, rect->format);
523 video_blend_format_info_init (&rectangle_info,
524 GST_BUFFER_DATA (rect->pixels), rect->height, rect->width,
526 ! !(rect->flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA));
528 needs_scaling = gst_video_overlay_rectangle_needs_scaling (rect);
530 video_blend_scale_linear_RGBA (&rectangle_info, rect->render_height,
534 ret = video_blend (&video_info, &rectangle_info, rect->x, rect->y,
537 GST_WARNING ("Could not blend overlay rectangle onto video buffer");
540 /* FIXME: should cache scaled pixels in the rectangle struct */
542 g_free (rectangle_info.pixels);
549 * gst_video_overlay_composition_copy:
550 * @comp: (transfer none): a #GstVideoOverlayComposition to copy
552 * Makes a copy of @comp and all contained rectangles, so that it is possible
553 * to modify the composition and contained rectangles (e.g. add additional
554 * rectangles or change the render co-ordinates or render dimension). The
555 * actual overlay pixel data buffers contained in the rectangles are not
558 * Returns: (transfer full): a new #GstVideoOverlayComposition equivalent
563 GstVideoOverlayComposition *
564 gst_video_overlay_composition_copy (GstVideoOverlayComposition * comp)
566 GstVideoOverlayComposition *copy;
567 GstVideoOverlayRectangle *rect;
570 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), NULL);
572 if (G_LIKELY (comp->num_rectangles == 0))
573 return gst_video_overlay_composition_new (NULL);
575 rect = gst_video_overlay_rectangle_copy (comp->rectangles[0]);
576 copy = gst_video_overlay_composition_new (rect);
577 gst_video_overlay_rectangle_unref (rect);
579 for (n = 1; n < comp->num_rectangles; ++n) {
580 rect = gst_video_overlay_rectangle_copy (comp->rectangles[n]);
581 gst_video_overlay_composition_add_rectangle (copy, rect);
582 gst_video_overlay_rectangle_unref (rect);
589 * gst_video_overlay_composition_make_writable:
590 * @comp: (transfer full): a #GstVideoOverlayComposition to copy
592 * Takes ownership of @comp and returns a version of @comp that is writable
593 * (i.e. can be modified). Will either return @comp right away, or create a
594 * new writable copy of @comp and unref @comp itself. All the contained
595 * rectangles will also be copied, but the actual overlay pixel data buffers
596 * contained in the rectangles are not copied.
598 * Returns: (transfer full): a writable #GstVideoOverlayComposition
599 * equivalent to @comp.
603 GstVideoOverlayComposition *
604 gst_video_overlay_composition_make_writable (GstVideoOverlayComposition * comp)
606 GstVideoOverlayComposition *writable_comp;
608 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), NULL);
610 if (GST_MINI_OBJECT_REFCOUNT_VALUE (comp) == 1) {
613 for (n = 0; n < comp->num_rectangles; ++n) {
614 if (GST_MINI_OBJECT_REFCOUNT_VALUE (comp->rectangles[n]) != 1)
622 writable_comp = gst_video_overlay_composition_copy (comp);
623 gst_video_overlay_composition_unref (comp);
625 return writable_comp;
629 * gst_video_overlay_composition_get_seqnum:
630 * @comp: a #GstVideoOverlayComposition
632 * Returns the sequence number of this composition. Sequence numbers are
633 * monotonically increasing and unique for overlay compositions and rectangles
634 * (meaning there will never be a rectangle with the same sequence number as
637 * Returns: the sequence number of @comp
642 gst_video_overlay_composition_get_seqnum (GstVideoOverlayComposition * comp)
644 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), 0);
646 return comp->seq_num;
649 /* ------------------------------ rectangles ------------------------------ -*/
651 static void gst_video_overlay_rectangle_instance_init (GstMiniObject * miniobj);
654 gst_video_overlay_rectangle_get_type (void)
656 static volatile gsize type_id = 0;
658 if (g_once_init_enter (&type_id)) {
659 GType new_type_id = g_type_register_static_simple (GST_TYPE_MINI_OBJECT,
660 g_intern_static_string ("GstVideoOverlayRectangle"),
661 sizeof (GstVideoOverlayRectangleClass),
662 (GClassInitFunc) gst_video_overlay_rectangle_class_init,
663 sizeof (GstVideoOverlayRectangle),
664 (GInstanceInitFunc) gst_video_overlay_rectangle_instance_init,
667 g_once_init_leave (&type_id, new_type_id);
674 gst_video_overlay_rectangle_finalize (GstMiniObject * mini_obj)
676 GstVideoOverlayRectangle *rect = (GstVideoOverlayRectangle *) mini_obj;
678 gst_buffer_replace (&rect->pixels, NULL);
680 while (rect->scaled_rectangles != NULL) {
681 GstVideoOverlayRectangle *scaled_rect = rect->scaled_rectangles->data;
683 gst_video_overlay_rectangle_unref (scaled_rect);
685 rect->scaled_rectangles =
686 g_list_delete_link (rect->scaled_rectangles, rect->scaled_rectangles);
689 g_free (rect->initial_alpha);
690 g_mutex_clear (&rect->lock);
692 /* not chaining up to GstMiniObject's finalize for now, we know it's empty */
696 gst_video_overlay_rectangle_class_init (GstMiniObjectClass * klass)
698 klass->finalize = gst_video_overlay_rectangle_finalize;
699 klass->copy = (GstMiniObjectCopyFunction) gst_video_overlay_rectangle_copy;
703 gst_video_overlay_rectangle_instance_init (GstMiniObject * mini_obj)
705 GstVideoOverlayRectangle *rect = (GstVideoOverlayRectangle *) mini_obj;
707 g_mutex_init (&rect->lock);
710 static inline gboolean
711 gst_video_overlay_rectangle_check_flags (GstVideoOverlayFormatFlags flags)
713 /* Check flags only contains flags we know about */
714 return (flags & ~(GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA |
715 GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA)) == 0;
719 gst_video_overlay_rectangle_is_same_alpha_type (GstVideoOverlayFormatFlags
720 flags1, GstVideoOverlayFormatFlags flags2)
722 return ((flags1 ^ flags2) & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA)
728 * gst_video_overlay_rectangle_new_argb:
729 * @pixels: (transfer none): a #GstBuffer pointing to the pixel memory
730 * @width: the width of the rectangle in @pixels
731 * @height: the height of the rectangle in @pixels
732 * @stride: the stride of the rectangle in @pixels in bytes (>= 4*width)
733 * @render_x: the X co-ordinate on the video where the top-left corner of this
734 * overlay rectangle should be rendered to
735 * @render_y: the Y co-ordinate on the video where the top-left corner of this
736 * overlay rectangle should be rendered to
737 * @render_width: the render width of this rectangle on the video
738 * @render_height: the render height of this rectangle on the video
741 * Creates a new video overlay rectangle with ARGB pixel data. The layout
742 * of the components in memory is B-G-R-A on little-endian platforms
743 * (corresponding to #GST_VIDEO_FORMAT_BGRA) and A-R-G-B on big-endian
744 * platforms (corresponding to #GST_VIDEO_FORMAT_ARGB). In other words,
745 * pixels are treated as 32-bit words and the lowest 8 bits then contain
746 * the blue component value and the highest 8 bits contain the alpha
747 * component value. Unless specified in the flags, the RGB values are
748 * non-premultiplied. This is the format that is used by most hardware,
749 * and also many rendering libraries such as Cairo, for example.
751 * Returns: (transfer full): a new #GstVideoOverlayRectangle. Unref with
752 * gst_video_overlay_rectangle_unref() when no longer needed.
756 GstVideoOverlayRectangle *
757 gst_video_overlay_rectangle_new_argb (GstBuffer * pixels,
758 guint width, guint height, guint stride, gint render_x, gint render_y,
759 guint render_width, guint render_height, GstVideoOverlayFormatFlags flags)
761 GstVideoOverlayRectangle *rect;
763 g_return_val_if_fail (GST_IS_BUFFER (pixels), NULL);
764 /* technically ((height-1)*stride)+width might be okay too */
765 g_return_val_if_fail (GST_BUFFER_SIZE (pixels) >= height * stride, NULL);
766 g_return_val_if_fail (stride >= (4 * width), NULL);
767 g_return_val_if_fail (height > 0 && width > 0, NULL);
768 g_return_val_if_fail (render_height > 0 && render_width > 0, NULL);
769 g_return_val_if_fail (gst_video_overlay_rectangle_check_flags (flags), NULL);
771 rect = (GstVideoOverlayRectangle *)
772 gst_mini_object_new (GST_TYPE_VIDEO_OVERLAY_RECTANGLE);
774 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
775 rect->format = GST_VIDEO_FORMAT_BGRA;
777 rect->format = GST_VIDEO_FORMAT_ARGB;
780 rect->pixels = gst_buffer_ref (pixels);
783 rect->height = height;
784 rect->stride = stride;
788 rect->render_width = render_width;
789 rect->render_height = render_height;
791 rect->global_alpha = 1.0;
792 rect->applied_global_alpha = 1.0;
793 rect->initial_alpha = NULL;
797 rect->seq_num = gst_video_overlay_get_seqnum ();
799 GST_LOG ("new rectangle %p: %ux%u => %ux%u @ %u,%u, seq_num %u, format %u, "
800 "flags %x, pixels %p, global_alpha=%f", rect, width, height, render_width,
801 render_height, render_x, render_y, rect->seq_num, rect->format,
802 rect->flags, pixels, rect->global_alpha);
808 * gst_video_overlay_rectangle_get_render_rectangle:
809 * @rectangle: a #GstVideoOverlayRectangle
810 * @render_x: (out) (allow-none): address where to store the X render offset
811 * @render_y: (out) (allow-none): address where to store the Y render offset
812 * @render_width: (out) (allow-none): address where to store the render width
813 * @render_height: (out) (allow-none): address where to store the render height
815 * Retrieves the render position and render dimension of the overlay
816 * rectangle on the video.
818 * Returns: TRUE if valid render dimensions were retrieved.
823 gst_video_overlay_rectangle_get_render_rectangle (GstVideoOverlayRectangle *
824 rectangle, gint * render_x, gint * render_y, guint * render_width,
825 guint * render_height)
827 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), FALSE);
830 *render_x = rectangle->x;
832 *render_y = rectangle->y;
834 *render_width = rectangle->render_width;
836 *render_height = rectangle->render_height;
842 * gst_video_overlay_rectangle_set_render_rectangle:
843 * @rectangle: a #GstVideoOverlayRectangle
844 * @render_x: render X position of rectangle on video
845 * @render_y: render Y position of rectangle on video
846 * @render_width: render width of rectangle
847 * @render_height: render height of rectangle
849 * Sets the render position and dimensions of the rectangle on the video.
850 * This function is mainly for elements that modify the size of the video
851 * in some way (e.g. through scaling or cropping) and need to adjust the
852 * details of any overlays to match the operation that changed the size.
854 * @rectangle must be writable, meaning its refcount must be 1. You can
855 * make the rectangles inside a #GstVideoOverlayComposition writable using
856 * gst_video_overlay_composition_make_writable() or
857 * gst_video_overlay_composition_copy().
862 gst_video_overlay_rectangle_set_render_rectangle (GstVideoOverlayRectangle *
863 rectangle, gint render_x, gint render_y, guint render_width,
866 g_return_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle));
867 g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (rectangle) == 1);
869 rectangle->x = render_x;
870 rectangle->y = render_y;
871 rectangle->render_width = render_width;
872 rectangle->render_height = render_height;
875 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
889 gst_video_overlay_rectangle_premultiply (GstBlendVideoFormatInfo * info)
892 for (j = 0; j < info->height; ++j) {
893 guint8 *line = info->pixels + info->stride[0] * j;
894 for (i = 0; i < info->width; ++i) {
895 int a = line[ARGB_A];
896 line[ARGB_R] = line[ARGB_R] * a / 255;
897 line[ARGB_G] = line[ARGB_G] * a / 255;
898 line[ARGB_B] = line[ARGB_B] * a / 255;
906 gst_video_overlay_rectangle_unpremultiply (GstBlendVideoFormatInfo * info)
909 for (j = 0; j < info->height; ++j) {
910 guint8 *line = info->pixels + info->stride[0] * j;
911 for (i = 0; i < info->width; ++i) {
912 int a = line[ARGB_A];
914 line[ARGB_R] = MIN ((line[ARGB_R] * 255 + a / 2) / a, 255);
915 line[ARGB_G] = MIN ((line[ARGB_G] * 255 + a / 2) / a, 255);
916 line[ARGB_B] = MIN ((line[ARGB_B] * 255 + a / 2) / a, 255);
925 gst_video_overlay_rectangle_extract_alpha (GstVideoOverlayRectangle * rect)
929 int alpha_size = rect->stride * rect->height / 4;
931 g_free (rect->initial_alpha);
932 rect->initial_alpha = NULL;
934 rect->initial_alpha = g_malloc (alpha_size);
935 src = GST_BUFFER_DATA (rect->pixels);
936 dst = rect->initial_alpha;
937 /* FIXME we're accessing possibly uninitialised bytes from the row padding */
938 while (offset < alpha_size) {
939 dst[offset] = src[offset * 4 + ARGB_A];
946 gst_video_overlay_rectangle_apply_global_alpha (GstVideoOverlayRectangle * rect,
952 g_assert (!(rect->applied_global_alpha != 1.0
953 && rect->initial_alpha == NULL));
955 if (global_alpha == rect->applied_global_alpha)
958 if (rect->initial_alpha == NULL)
959 gst_video_overlay_rectangle_extract_alpha (rect);
961 src = rect->initial_alpha;
962 rect->pixels = gst_buffer_make_writable (rect->pixels);
963 dst = GST_BUFFER_DATA (rect->pixels);
964 while (offset < (rect->height * rect->stride / 4)) {
965 guint doff = offset * 4;
966 guint8 na = (guint8) src[offset] * global_alpha;
967 if (! !(rect->flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA)) {
969 (guint8) ((double) (dst[doff + ARGB_R] * 255) / (double) dst[doff +
972 (guint8) ((double) (dst[doff + ARGB_G] * 255) / (double) dst[doff +
975 (guint8) ((double) (dst[doff + ARGB_B] * 255) / (double) dst[doff +
978 dst[doff + ARGB_A] = na;
982 rect->applied_global_alpha = global_alpha;
986 gst_video_overlay_rectangle_get_pixels_argb_internal (GstVideoOverlayRectangle *
987 rectangle, guint * stride, GstVideoOverlayFormatFlags flags,
990 GstVideoOverlayFormatFlags new_flags;
991 GstVideoOverlayRectangle *scaled_rect = NULL;
992 GstBlendVideoFormatInfo info;
995 guint wanted_width = unscaled ? rectangle->width : rectangle->render_width;
996 guint wanted_height = unscaled ? rectangle->height : rectangle->render_height;
997 gboolean apply_global_alpha;
998 gboolean revert_global_alpha;
1000 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1001 g_return_val_if_fail (stride != NULL, NULL);
1002 g_return_val_if_fail (gst_video_overlay_rectangle_check_flags (flags), NULL);
1004 apply_global_alpha =
1005 (! !(rectangle->flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA)
1006 && !(flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA));
1007 revert_global_alpha =
1008 (! !(rectangle->flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA)
1009 && ! !(flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA));
1011 /* This assumes we don't need to adjust the format */
1012 if (wanted_width == rectangle->width &&
1013 wanted_height == rectangle->height &&
1014 gst_video_overlay_rectangle_is_same_alpha_type (rectangle->flags,
1016 /* don't need to apply/revert global-alpha either: */
1017 if ((!apply_global_alpha
1018 || rectangle->applied_global_alpha == rectangle->global_alpha)
1019 && (!revert_global_alpha || rectangle->applied_global_alpha == 1.0)) {
1020 *stride = rectangle->stride;
1021 return rectangle->pixels;
1023 /* only apply/revert global-alpha */
1024 scaled_rect = rectangle;
1029 /* see if we've got one cached already */
1030 GST_RECTANGLE_LOCK (rectangle);
1031 for (l = rectangle->scaled_rectangles; l != NULL; l = l->next) {
1032 GstVideoOverlayRectangle *r = l->data;
1034 if (r->width == wanted_width &&
1035 r->height == wanted_height &&
1036 gst_video_overlay_rectangle_is_same_alpha_type (r->flags, flags)) {
1037 /* we'll keep these rectangles around until finalize, so it's ok not
1038 * to take our own ref here */
1043 GST_RECTANGLE_UNLOCK (rectangle);
1045 if (scaled_rect != NULL)
1048 /* not cached yet, do the preprocessing and put the result into our cache */
1049 video_blend_format_info_init (&info, GST_BUFFER_DATA (rectangle->pixels),
1050 rectangle->height, rectangle->width, rectangle->format,
1051 ! !(rectangle->flags &
1052 GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA));
1054 if (wanted_width != rectangle->width || wanted_height != rectangle->height) {
1055 /* we could check the cache for a scaled rect with global_alpha == 1 here */
1056 video_blend_scale_linear_RGBA (&info, wanted_height, wanted_width);
1058 /* if we don't have to scale, we have to modify the alpha values, so we
1059 * need to make a copy of the pixel memory (and we take ownership below) */
1060 info.pixels = g_memdup (info.pixels, info.size);
1063 new_flags = rectangle->flags;
1064 if (!gst_video_overlay_rectangle_is_same_alpha_type (rectangle->flags, flags)) {
1065 if (rectangle->flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA) {
1066 gst_video_overlay_rectangle_unpremultiply (&info);
1067 new_flags &= ~GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA;
1069 gst_video_overlay_rectangle_premultiply (&info);
1070 new_flags |= GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA;
1074 buf = gst_buffer_new ();
1075 GST_BUFFER_DATA (buf) = info.pixels;
1076 GST_BUFFER_MALLOCDATA (buf) = info.pixels;
1077 GST_BUFFER_SIZE (buf) = info.size;
1079 scaled_rect = gst_video_overlay_rectangle_new_argb (buf,
1080 wanted_width, wanted_height, info.stride[0],
1081 0, 0, wanted_width, wanted_height, new_flags);
1082 if (rectangle->global_alpha != 1.0)
1083 gst_video_overlay_rectangle_set_global_alpha (scaled_rect,
1084 rectangle->global_alpha);
1085 gst_buffer_unref (buf);
1087 GST_RECTANGLE_LOCK (rectangle);
1088 rectangle->scaled_rectangles =
1089 g_list_prepend (rectangle->scaled_rectangles, scaled_rect);
1090 GST_RECTANGLE_UNLOCK (rectangle);
1094 GST_RECTANGLE_LOCK (rectangle);
1095 if (apply_global_alpha
1096 && scaled_rect->applied_global_alpha != rectangle->global_alpha) {
1097 gst_video_overlay_rectangle_apply_global_alpha (scaled_rect,
1098 rectangle->global_alpha);
1099 gst_video_overlay_rectangle_set_global_alpha (scaled_rect,
1100 rectangle->global_alpha);
1101 } else if (revert_global_alpha && scaled_rect->applied_global_alpha != 1.0) {
1102 gst_video_overlay_rectangle_apply_global_alpha (scaled_rect, 1.0);
1104 GST_RECTANGLE_UNLOCK (rectangle);
1106 *stride = scaled_rect->stride;
1107 return scaled_rect->pixels;
1112 * gst_video_overlay_rectangle_get_pixels_argb:
1113 * @rectangle: a #GstVideoOverlayRectangle
1114 * @stride: (out) (allow-none): address of guint variable where to store the
1115 * row stride of the ARGB pixel data in the buffer
1117 * If a global_alpha value != 1 is set for the rectangle, the caller
1118 * should set the #GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA flag
1119 * if he wants to apply global-alpha himself. If the flag is not set
1120 * global_alpha is applied internally before returning the pixel-data.
1122 * Returns: (transfer none): a #GstBuffer holding the ARGB pixel data with
1123 * row stride @stride and width and height of the render dimensions as per
1124 * gst_video_overlay_rectangle_get_render_rectangle(). This function does
1125 * not return a reference, the caller should obtain a reference of her own
1126 * with gst_buffer_ref() if needed.
1131 gst_video_overlay_rectangle_get_pixels_argb (GstVideoOverlayRectangle *
1132 rectangle, guint * stride, GstVideoOverlayFormatFlags flags)
1134 return gst_video_overlay_rectangle_get_pixels_argb_internal (rectangle,
1135 stride, flags, FALSE);
1139 * gst_video_overlay_rectangle_get_pixels_unscaled_argb:
1140 * @rectangle: a #GstVideoOverlayRectangle
1141 * @width: (out): address where to store the width of the unscaled
1142 * rectangle in pixels
1143 * @height: (out): address where to store the height of the unscaled
1144 * rectangle in pixels
1145 * @stride: (out): address of guint variable where to store the row
1146 * stride of the ARGB pixel data in the buffer
1148 * If a global_alpha value != 1 is set for the rectangle, the caller
1149 * should set the #GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA flag
1150 * if he wants to apply global-alpha himself. If the flag is not set
1151 * global_alpha is applied internally before returning the pixel-data.
1153 * Retrieves the pixel data as it is. This is useful if the caller can
1154 * do the scaling itself when handling the overlaying. The rectangle will
1155 * need to be scaled to the render dimensions, which can be retrieved using
1156 * gst_video_overlay_rectangle_get_render_rectangle().
1158 * Returns: (transfer none): a #GstBuffer holding the ARGB pixel data with
1159 * row stride @stride. This function does not return a reference, the caller
1160 * should obtain a reference of her own with gst_buffer_ref() if needed.
1165 gst_video_overlay_rectangle_get_pixels_unscaled_argb (GstVideoOverlayRectangle *
1166 rectangle, guint * width, guint * height, guint * stride,
1167 GstVideoOverlayFormatFlags flags)
1169 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1170 g_return_val_if_fail (width != NULL, NULL);
1171 g_return_val_if_fail (height != NULL, NULL);
1172 g_return_val_if_fail (stride != NULL, NULL);
1174 *width = rectangle->width;
1175 *height = rectangle->height;
1176 return gst_video_overlay_rectangle_get_pixels_argb_internal (rectangle,
1177 stride, flags, TRUE);
1181 * gst_video_overlay_rectangle_get_flags:
1182 * @rectangle: a #GstVideoOverlayRectangle
1184 * Retrieves the flags associated with a #GstVideoOverlayRectangle.
1185 * This is useful if the caller can handle both premultiplied alpha and
1186 * non premultiplied alpha, for example. By knowing whether the rectangle
1187 * uses premultiplied or not, it can request the pixel data in the format
1188 * it is stored in, to avoid unnecessary conversion.
1190 * Returns: the #GstVideoOverlayFormatFlags associated with the rectangle.
1194 GstVideoOverlayFormatFlags
1195 gst_video_overlay_rectangle_get_flags (GstVideoOverlayRectangle * rectangle)
1197 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle),
1198 GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1200 return rectangle->flags;
1204 * gst_video_overlay_rectangle_get_global_alpha:
1205 * @rectangle: a #GstVideoOverlayRectangle
1207 * Retrieves the global-alpha value associated with a #GstVideoOverlayRectangle.
1209 * Returns: the global-alpha value associated with the rectangle.
1214 gst_video_overlay_rectangle_get_global_alpha (GstVideoOverlayRectangle *
1217 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), -1);
1219 return rectangle->global_alpha;
1223 * gst_video_overlay_rectangle_set_global_alpha:
1224 * @rectangle: a #GstVideoOverlayRectangle
1226 * Sets the global alpha value associated with a #GstVideoOverlayRectangle. Per-
1227 * pixel alpha values are multiplied with this value. Valid
1228 * values: 0 <= global_alpha <= 1; 1 to deactivate.
1230 # @rectangle must be writable, meaning its refcount must be 1. You can
1231 * make the rectangles inside a #GstVideoOverlayComposition writable using
1232 * gst_video_overlay_composition_make_writable() or
1233 * gst_video_overlay_composition_copy().
1238 gst_video_overlay_rectangle_set_global_alpha (GstVideoOverlayRectangle *
1239 rectangle, gfloat global_alpha)
1241 g_return_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle));
1242 g_return_if_fail (global_alpha >= 0 && global_alpha <= 1);
1244 if (rectangle->global_alpha != global_alpha) {
1245 rectangle->global_alpha = global_alpha;
1246 if (global_alpha != 1)
1247 rectangle->flags |= GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA;
1249 rectangle->flags &= ~GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA;
1250 /* update seq_num automatically to signal the consumer, that data has changed
1251 * note, that this might mislead renderers, that can handle global-alpha
1252 * themselves, because what they want to know is whether the actual pixel data
1254 rectangle->seq_num = gst_video_overlay_get_seqnum ();
1259 * gst_video_overlay_rectangle_copy:
1260 * @rectangle: (transfer none): a #GstVideoOverlayRectangle to copy
1262 * Makes a copy of @rectangle, so that it is possible to modify it
1263 * (e.g. to change the render co-ordinates or render dimension). The
1264 * actual overlay pixel data buffers contained in the rectangle are not
1267 * Returns: (transfer full): a new #GstVideoOverlayRectangle equivalent
1272 GstVideoOverlayRectangle *
1273 gst_video_overlay_rectangle_copy (GstVideoOverlayRectangle * rectangle)
1275 GstVideoOverlayRectangle *copy;
1277 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1279 copy = gst_video_overlay_rectangle_new_argb (rectangle->pixels,
1280 rectangle->width, rectangle->height, rectangle->stride,
1281 rectangle->x, rectangle->y,
1282 rectangle->render_width, rectangle->render_height, rectangle->flags);
1283 if (rectangle->global_alpha != 1)
1284 gst_video_overlay_rectangle_set_global_alpha (copy,
1285 rectangle->global_alpha);
1291 * gst_video_overlay_rectangle_get_seqnum:
1292 * @rectangle: a #GstVideoOverlayRectangle
1294 * Returns the sequence number of this rectangle. Sequence numbers are
1295 * monotonically increasing and unique for overlay compositions and rectangles
1296 * (meaning there will never be a rectangle with the same sequence number as
1299 * Using the sequence number of a rectangle as an indicator for changed
1300 * pixel-data of a rectangle is dangereous. Some API calls, like e.g.
1301 * gst_video_overlay_rectangle_set_global_alpha(), automatically update
1302 * the per rectangle sequence number, which is misleading for renderers/
1303 * consumers, that handle global-alpha themselves. For them the
1304 * pixel-data returned by gst_video_overlay_rectangle_get_pixels_*()
1305 * wont be different for different global-alpha values. In this case a
1306 * renderer could also use the GstBuffer pointers as a hint for changed
1309 * Returns: the sequence number of @rectangle
1314 gst_video_overlay_rectangle_get_seqnum (GstVideoOverlayRectangle * rectangle)
1316 g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), 0);
1318 return rectangle->seq_num;