Fix FSF address
[platform/upstream/gstreamer.git] / gst-libs / gst / video / video-overlay-composition.c
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  * @short_description: Video Buffer Overlay Compositions (Subtitles, Logos)
25  *
26  * <refsect2>
27  * <para>
28  * Functions to create and handle overlay compositions on video buffers.
29  * </para>
30  * <para>
31  * An overlay composition describes one or more overlay rectangles to be
32  * blended on top of a video buffer.
33  * </para>
34  * <para>
35  * This API serves two main purposes:
36  * <itemizedlist>
37  * <listitem>
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.
42  * </listitem>
43  * <listitem>
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
46  * one place.
47  * </listitem>
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.
54  * </itemizedlist>
55  * </para>
56  * </refsect2>
57  */
58
59 /* TODO:
60  *  - provide accessors for seq_num and other fields (as needed)
61  *  - allow overlay to set/get original pango markup string on/from rectangle
62  */
63
64 #ifdef HAVE_CONFIG_H
65 #include "config.h"
66 #endif
67
68 #include "video-overlay-composition.h"
69 #include "video-blend.h"
70 #include "gstvideometa.h"
71 #include <string.h>
72
73 struct _GstVideoOverlayComposition
74 {
75   GstMiniObject parent;
76
77   guint num_rectangles;
78   GstVideoOverlayRectangle **rectangles;
79
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;
85
86   /* sequence number for the composition (same series as rectangles) */
87   guint seq_num;
88 };
89
90 struct _GstVideoOverlayRectangle
91 {
92   GstMiniObject parent;
93
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) */
97   gint x, y;
98   guint render_width, render_height;
99
100   /* Info on overlay pixels (format, width, height) */
101   GstVideoInfo info;
102
103   /* The flags associated to this rectangle */
104   GstVideoOverlayFormatFlags flags;
105
106   /* Refcounted blob of memory, no caps or timestamps */
107   GstBuffer *pixels;
108
109   /* FIXME: how to express source like text or pango markup?
110    *        (just add source type enum + source buffer with data)
111    *
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.
117    *
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.
121    */
122
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. */
127   guint seq_num;
128
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
131    * during blending.
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.*/
135   gfloat global_alpha;
136
137   /* track alpha-values already applied: */
138   gfloat applied_global_alpha;
139   /* store initial per-pixel alpha values: */
140   guint8 *initial_alpha;
141
142   /* FIXME: we may also need a (private) way to cache converted/scaled
143    * pixel blobs */
144   GMutex lock;
145
146   GList *scaled_rectangles;
147 };
148
149 #define GST_RECTANGLE_LOCK(rect)   g_mutex_lock(&rect->lock)
150 #define GST_RECTANGLE_UNLOCK(rect) g_mutex_unlock(&rect->lock)
151
152 /* --------------------------- utility functions --------------------------- */
153
154 #ifndef GST_DISABLE_GST_DEBUG
155
156 #define GST_CAT_DEFAULT ensure_debug_category()
157
158 static GstDebugCategory *
159 ensure_debug_category (void)
160 {
161   static gsize cat_gonce = 0;
162
163   if (g_once_init_enter (&cat_gonce)) {
164     gsize cat_done;
165
166     cat_done = (gsize) _gst_debug_category_new ("video-composition", 0,
167         "video overlay composition");
168
169     g_once_init_leave (&cat_gonce, cat_done);
170   }
171
172   return (GstDebugCategory *) cat_gonce;
173 }
174
175 #else
176
177 #define ensure_debug_category() /* NOOP */
178
179 #endif /* GST_DISABLE_GST_DEBUG */
180
181 static guint
182 gst_video_overlay_get_seqnum (void)
183 {
184   static gint seqnum;           /* 0 */
185
186   return (guint) g_atomic_int_add (&seqnum, 1);
187 }
188
189 static void
190 gst_video_overlay_composition_meta_free (GstMeta * meta, GstBuffer * buf)
191 {
192   GstVideoOverlayCompositionMeta *ometa;
193
194   ometa = (GstVideoOverlayCompositionMeta *) meta;
195
196   if (ometa->overlay)
197     gst_video_overlay_composition_unref (ometa->overlay);
198 }
199
200 static gboolean
201 gst_video_overlay_composition_meta_transform (GstBuffer * dest, GstMeta * meta,
202     GstBuffer * buffer, GQuark type, gpointer data)
203 {
204   GstVideoOverlayCompositionMeta *dmeta, *smeta;
205
206   smeta = (GstVideoOverlayCompositionMeta *) meta;
207
208   if (GST_META_TRANSFORM_IS_COPY (type)) {
209     GstMetaTransformCopy *copy = data;
210
211     if (!copy->region) {
212       GST_DEBUG ("copy video overlay composition metadata");
213
214       /* only copy if the complete data is copied as well */
215       dmeta =
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);
219     }
220   }
221   return TRUE;
222 }
223
224 GType
225 gst_video_overlay_composition_meta_api_get_type (void)
226 {
227   static volatile GType type = 0;
228   static const gchar *tags[] = { NULL };
229
230   if (g_once_init_enter (&type)) {
231     GType _type =
232         gst_meta_api_type_register ("GstVideoOverlayCompositionMetaAPI", tags);
233     g_once_init_leave (&type, _type);
234   }
235   return type;
236 }
237
238 /* video overlay composition metadata */
239 const GstMetaInfo *
240 gst_video_overlay_composition_meta_get_info (void)
241 {
242   static const GstMetaInfo *video_overlay_composition_meta_info = NULL;
243
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);
253   }
254   return video_overlay_composition_meta_info;
255 }
256
257 /**
258  * gst_buffer_add_video_overlay_composition_meta:
259  * @buf: a #GstBuffer
260  * @comp: (allow-none): a #GstVideoOverlayComposition
261  *
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
264  * of @comp.
265  */
266 GstVideoOverlayCompositionMeta *
267 gst_buffer_add_video_overlay_composition_meta (GstBuffer * buf,
268     GstVideoOverlayComposition * comp)
269 {
270   GstVideoOverlayCompositionMeta *ometa;
271
272   g_return_val_if_fail (gst_buffer_is_writable (buf), NULL);
273
274   ometa = (GstVideoOverlayCompositionMeta *)
275       gst_buffer_add_meta (buf, GST_VIDEO_OVERLAY_COMPOSITION_META_INFO, NULL);
276
277   ometa->overlay = gst_video_overlay_composition_ref (comp);
278
279   return ometa;
280 }
281
282 /* ------------------------------ composition ------------------------------ */
283
284 #define RECTANGLE_ARRAY_STEP 4  /* premature optimization */
285
286 GST_DEFINE_MINI_OBJECT_TYPE (GstVideoOverlayComposition,
287     gst_video_overlay_composition);
288
289 static void
290 gst_video_overlay_composition_free (GstMiniObject * mini_obj)
291 {
292   GstVideoOverlayComposition *comp = (GstVideoOverlayComposition *) mini_obj;
293   guint num;
294
295   num = comp->num_rectangles;
296
297   while (num > 0) {
298     gst_video_overlay_rectangle_unref (comp->rectangles[num - 1]);
299     --num;
300   }
301
302   g_free (comp->rectangles);
303   comp->rectangles = NULL;
304   comp->num_rectangles = 0;
305
306   g_slice_free (GstVideoOverlayComposition, comp);
307 }
308
309 /**
310  * gst_video_overlay_composition_new:
311  * @rectangle: (transfer none): a #GstVideoOverlayRectangle to add to the
312  *     composition
313  *
314  * Creates a new video overlay composition object to hold one or more
315  * overlay rectangles.
316  *
317  * Returns: (transfer full): a new #GstVideoOverlayComposition. Unref with
318  *     gst_video_overlay_composition_unref() when no longer needed.
319  */
320 GstVideoOverlayComposition *
321 gst_video_overlay_composition_new (GstVideoOverlayRectangle * rectangle)
322 {
323   GstVideoOverlayComposition *comp;
324
325
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);
330
331   comp = g_slice_new0 (GstVideoOverlayComposition);
332
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);
337
338   comp->rectangles = g_new0 (GstVideoOverlayRectangle *, RECTANGLE_ARRAY_STEP);
339   comp->rectangles[0] = gst_video_overlay_rectangle_ref (rectangle);
340   comp->num_rectangles = 1;
341
342   comp->seq_num = gst_video_overlay_get_seqnum ();
343
344   /* since the rectangle was created earlier, its seqnum is smaller than ours */
345   comp->min_seq_num_used = rectangle->seq_num;
346
347   GST_LOG ("new composition %p: seq_num %u with rectangle %p", comp,
348       comp->seq_num, rectangle);
349
350   return comp;
351 }
352
353 /**
354  * gst_video_overlay_composition_add_rectangle:
355  * @comp: a #GstVideoOverlayComposition
356  * @rectangle: (transfer none): a #GstVideoOverlayRectangle to add to the
357  *     composition
358  *
359  * Adds an overlay rectangle to an existing overlay composition object. This
360  * must be done right after creating the overlay composition.
361  */
362 void
363 gst_video_overlay_composition_add_rectangle (GstVideoOverlayComposition * comp,
364     GstVideoOverlayRectangle * rectangle)
365 {
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);
369
370   if (comp->num_rectangles % RECTANGLE_ARRAY_STEP == 0) {
371     comp->rectangles =
372         g_renew (GstVideoOverlayRectangle *, comp->rectangles,
373         comp->num_rectangles + RECTANGLE_ARRAY_STEP);
374   }
375
376   comp->rectangles[comp->num_rectangles] =
377       gst_video_overlay_rectangle_ref (rectangle);
378   comp->num_rectangles += 1;
379
380   comp->min_seq_num_used = MIN (comp->min_seq_num_used, rectangle->seq_num);
381
382   GST_LOG ("composition %p: added rectangle %p", comp, rectangle);
383 }
384
385 /**
386  * gst_video_overlay_composition_n_rectangles:
387  * @comp: a #GstVideoOverlayComposition
388  *
389  * Returns the number of #GstVideoOverlayRectangle<!-- -->s contained in @comp.
390  *
391  * Returns: the number of rectangles
392  */
393 guint
394 gst_video_overlay_composition_n_rectangles (GstVideoOverlayComposition * comp)
395 {
396   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), 0);
397
398   return comp->num_rectangles;
399 }
400
401 /**
402  * gst_video_overlay_composition_get_rectangle:
403  * @comp: a #GstVideoOverlayComposition
404  * @n: number of the rectangle to get
405  *
406  * Returns the @n-th #GstVideoOverlayRectangle contained in @comp.
407  *
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()
411  *     if needed.
412  */
413 GstVideoOverlayRectangle *
414 gst_video_overlay_composition_get_rectangle (GstVideoOverlayComposition * comp,
415     guint n)
416 {
417   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), NULL);
418
419   if (n >= comp->num_rectangles)
420     return NULL;
421
422   return comp->rectangles[n];
423 }
424
425 static gboolean
426 gst_video_overlay_rectangle_needs_scaling (GstVideoOverlayRectangle * r)
427 {
428   return (GST_VIDEO_INFO_WIDTH (&r->info) != r->render_width ||
429       GST_VIDEO_INFO_HEIGHT (&r->info) != r->render_height);
430 }
431
432 /**
433  * gst_video_overlay_composition_blend:
434  * @comp: a #GstVideoOverlayComposition
435  * @video_buf: a #GstVideoFrame containing raw video data in a supported format
436  *
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.
440  */
441 gboolean
442 gst_video_overlay_composition_blend (GstVideoOverlayComposition * comp,
443     GstVideoFrame * video_buf)
444 {
445   GstVideoInfo scaled_info;
446   GstVideoInfo *vinfo;
447   GstVideoFrame rectangle_frame;
448   GstVideoFormat fmt;
449   GstBuffer *pixels = NULL;
450   gboolean ret = TRUE;
451   guint n, num;
452   int w, h;
453
454   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), FALSE);
455   g_return_val_if_fail (video_buf != NULL, FALSE);
456
457   w = GST_VIDEO_FRAME_WIDTH (video_buf);
458   h = GST_VIDEO_FRAME_HEIGHT (video_buf);
459   fmt = GST_VIDEO_FRAME_FORMAT (video_buf);
460
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);
464
465   for (n = 0; n < num; ++n) {
466     GstVideoOverlayRectangle *rect;
467     gboolean needs_scaling;
468
469     rect = comp->rectangles[n];
470
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));
474
475     needs_scaling = gst_video_overlay_rectangle_needs_scaling (rect);
476     if (needs_scaling) {
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;
480     } else {
481       pixels = gst_buffer_ref (rect->pixels);
482       vinfo = &rect->info;
483     }
484
485     gst_video_frame_map (&rectangle_frame, vinfo, pixels, GST_MAP_READ);
486
487     ret = gst_video_blend (video_buf, &rectangle_frame, rect->x, rect->y,
488         rect->global_alpha);
489     gst_video_frame_unmap (&rectangle_frame);
490     if (!ret) {
491       GST_WARNING ("Could not blend overlay rectangle onto video buffer");
492     }
493
494     /* FIXME: should cache scaled pixels in the rectangle struct */
495     gst_buffer_unref (pixels);
496   }
497
498   return ret;
499 }
500
501 /**
502  * gst_video_overlay_composition_copy:
503  * @comp: (transfer none): a #GstVideoOverlayComposition to copy
504  *
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
509  * copied.
510  *
511  * Returns: (transfer full): a new #GstVideoOverlayComposition equivalent
512  *     to @comp.
513  */
514 GstVideoOverlayComposition *
515 gst_video_overlay_composition_copy (GstVideoOverlayComposition * comp)
516 {
517   GstVideoOverlayComposition *copy;
518   GstVideoOverlayRectangle *rect;
519   guint n;
520
521   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), NULL);
522
523   if (G_LIKELY (comp->num_rectangles == 0))
524     return gst_video_overlay_composition_new (NULL);
525
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);
529
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);
534   }
535
536   return copy;
537 }
538
539 /**
540  * gst_video_overlay_composition_make_writable:
541  * @comp: (transfer full): a #GstVideoOverlayComposition to copy
542  *
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.
548  *
549  * Returns: (transfer full): a writable #GstVideoOverlayComposition
550  *     equivalent to @comp.
551  */
552 GstVideoOverlayComposition *
553 gst_video_overlay_composition_make_writable (GstVideoOverlayComposition * comp)
554 {
555   GstVideoOverlayComposition *writable_comp;
556
557   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), NULL);
558
559   if (GST_MINI_OBJECT_REFCOUNT_VALUE (comp) == 1) {
560     guint n;
561
562     for (n = 0; n < comp->num_rectangles; ++n) {
563       if (GST_MINI_OBJECT_REFCOUNT_VALUE (comp->rectangles[n]) != 1)
564         goto copy;
565     }
566     return comp;
567   }
568
569 copy:
570
571   writable_comp = gst_video_overlay_composition_copy (comp);
572   gst_video_overlay_composition_unref (comp);
573
574   return writable_comp;
575 }
576
577 /**
578  * gst_video_overlay_composition_get_seqnum:
579  * @comp: a #GstVideoOverlayComposition
580  *
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
584  * a composition).
585  *
586  * Returns: the sequence number of @comp
587  */
588 guint
589 gst_video_overlay_composition_get_seqnum (GstVideoOverlayComposition * comp)
590 {
591   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), 0);
592
593   return comp->seq_num;
594 }
595
596 /* ------------------------------ rectangles ------------------------------ -*/
597
598 GST_DEFINE_MINI_OBJECT_TYPE (GstVideoOverlayRectangle,
599     gst_video_overlay_rectangle);
600
601 static void
602 gst_video_overlay_rectangle_free (GstMiniObject * mini_obj)
603 {
604   GstVideoOverlayRectangle *rect = (GstVideoOverlayRectangle *) mini_obj;
605
606   gst_buffer_replace (&rect->pixels, NULL);
607
608   while (rect->scaled_rectangles != NULL) {
609     GstVideoOverlayRectangle *scaled_rect = rect->scaled_rectangles->data;
610
611     gst_video_overlay_rectangle_unref (scaled_rect);
612
613     rect->scaled_rectangles =
614         g_list_delete_link (rect->scaled_rectangles, rect->scaled_rectangles);
615   }
616
617   g_free (rect->initial_alpha);
618   g_mutex_clear (&rect->lock);
619
620   g_slice_free (GstVideoOverlayRectangle, rect);
621 }
622
623 static inline gboolean
624 gst_video_overlay_rectangle_check_flags (GstVideoOverlayFormatFlags flags)
625 {
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;
629 }
630
631 static gboolean
632 gst_video_overlay_rectangle_is_same_alpha_type (GstVideoOverlayFormatFlags
633     flags1, GstVideoOverlayFormatFlags flags2)
634 {
635   return ((flags1 ^ flags2) & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA)
636       == 0;
637 }
638
639
640 /**
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
649  * @flags: flags
650  *
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.
662  *
663  * Returns: (transfer full): a new #GstVideoOverlayRectangle. Unref with
664  *     gst_video_overlay_rectangle_unref() when no longer needed.
665  */
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)
670 {
671   GstVideoOverlayRectangle *rect;
672   GstVideoMeta *vmeta;
673   GstVideoFormat format;
674   guint width, height;
675
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);
679
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);
687
688   format = vmeta->format;
689   width = vmeta->width;
690   height = vmeta->height;
691
692   /* technically ((height-1)*stride)+width might be okay too */
693   g_return_val_if_fail (gst_buffer_get_size (pixels) >= height * width * 4,
694       NULL);
695   g_return_val_if_fail (height > 0 && width > 0, NULL);
696
697   rect = g_slice_new0 (GstVideoOverlayRectangle);
698
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);
703
704   g_mutex_init (&rect->lock);
705
706   rect->pixels = gst_buffer_ref (pixels);
707   rect->scaled_rectangles = NULL;
708
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;
713
714   rect->x = render_x;
715   rect->y = render_y;
716   rect->render_width = render_width;
717   rect->render_height = render_height;
718
719   rect->global_alpha = 1.0;
720   rect->applied_global_alpha = 1.0;
721   rect->initial_alpha = NULL;
722
723   rect->flags = flags;
724
725   rect->seq_num = gst_video_overlay_get_seqnum ();
726
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);
731
732   return rect;
733 }
734
735 /**
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
742  *
743  * Retrieves the render position and render dimension of the overlay
744  * rectangle on the video.
745  *
746  * Returns: TRUE if valid render dimensions were retrieved.
747  */
748 gboolean
749 gst_video_overlay_rectangle_get_render_rectangle (GstVideoOverlayRectangle *
750     rectangle, gint * render_x, gint * render_y, guint * render_width,
751     guint * render_height)
752 {
753   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), FALSE);
754
755   if (render_x)
756     *render_x = rectangle->x;
757   if (render_y)
758     *render_y = rectangle->y;
759   if (render_width)
760     *render_width = rectangle->render_width;
761   if (render_height)
762     *render_height = rectangle->render_height;
763
764   return TRUE;
765 }
766
767 /**
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
774  *
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.
779  *
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().
784  */
785 void
786 gst_video_overlay_rectangle_set_render_rectangle (GstVideoOverlayRectangle *
787     rectangle, gint render_x, gint render_y, guint render_width,
788     guint render_height)
789 {
790   g_return_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle));
791   g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (rectangle) == 1);
792
793   rectangle->x = render_x;
794   rectangle->y = render_y;
795   rectangle->render_width = render_width;
796   rectangle->render_height = render_height;
797 }
798
799 /* FIXME: orc-ify */
800 static void
801 gst_video_overlay_rectangle_premultiply_0 (GstVideoFrame * frame)
802 {
803   int i, j;
804   for (j = 0; j < GST_VIDEO_FRAME_HEIGHT (frame); ++j) {
805     guint8 *line;
806
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) {
810       int a = line[0];
811       line[1] = line[1] * a / 255;
812       line[2] = line[2] * a / 255;
813       line[3] = line[3] * a / 255;
814       line += 4;
815     }
816   }
817 }
818
819 static void
820 gst_video_overlay_rectangle_premultiply_3 (GstVideoFrame * frame)
821 {
822   int i, j;
823   for (j = 0; j < GST_VIDEO_FRAME_HEIGHT (frame); ++j) {
824     guint8 *line;
825
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) {
829       int a = line[3];
830       line[0] = line[0] * a / 255;
831       line[1] = line[1] * a / 255;
832       line[2] = line[2] * a / 255;
833       line += 4;
834     }
835   }
836 }
837
838 static void
839 gst_video_overlay_rectangle_premultiply (GstVideoFrame * frame)
840 {
841   gint alpha_offset;
842
843   alpha_offset = GST_VIDEO_FRAME_COMP_POFFSET (frame, 3);
844   switch (alpha_offset) {
845     case 0:
846       gst_video_overlay_rectangle_premultiply_0 (frame);
847       break;
848     case 3:
849       gst_video_overlay_rectangle_premultiply_3 (frame);
850       break;
851     default:
852       g_assert_not_reached ();
853       break;
854   }
855 }
856
857 /* FIXME: orc-ify */
858 static void
859 gst_video_overlay_rectangle_unpremultiply_0 (GstVideoFrame * frame)
860 {
861   int i, j;
862   for (j = 0; j < GST_VIDEO_FRAME_HEIGHT (frame); ++j) {
863     guint8 *line;
864
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) {
868       int a = line[0];
869       if (a) {
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);
873       }
874       line += 4;
875     }
876   }
877 }
878
879 static void
880 gst_video_overlay_rectangle_unpremultiply_3 (GstVideoFrame * frame)
881 {
882   int i, j;
883   for (j = 0; j < GST_VIDEO_FRAME_HEIGHT (frame); ++j) {
884     guint8 *line;
885
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) {
889       int a = line[3];
890       if (a) {
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);
894       }
895       line += 4;
896     }
897   }
898 }
899
900 static void
901 gst_video_overlay_rectangle_unpremultiply (GstVideoFrame * frame)
902 {
903   gint alpha_offset;
904
905   alpha_offset = GST_VIDEO_FRAME_COMP_POFFSET (frame, 3);
906   switch (alpha_offset) {
907     case 0:
908       gst_video_overlay_rectangle_unpremultiply_0 (frame);
909       break;
910     case 3:
911       gst_video_overlay_rectangle_unpremultiply_3 (frame);
912       break;
913     default:
914       g_assert_not_reached ();
915       break;
916   }
917 }
918
919
920 static void
921 gst_video_overlay_rectangle_extract_alpha (GstVideoOverlayRectangle * rect)
922 {
923   guint8 *src, *dst;
924   GstVideoFrame frame;
925   gint i, j, w, h, stride, alpha_offset;
926
927   alpha_offset = GST_VIDEO_INFO_COMP_POFFSET (&rect->info, 3);
928   g_return_if_fail (alpha_offset == 0 || alpha_offset == 3);
929
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);
935
936   g_free (rect->initial_alpha);
937   rect->initial_alpha = g_malloc (w * h);
938   dst = rect->initial_alpha;
939
940   for (i = 0; i < h; i++) {
941     for (j = 0; j < w; j++) {
942       *dst = src[alpha_offset];
943       dst++;
944       src += 4;
945     }
946     src += stride - 4 * w;
947   }
948   gst_video_frame_unmap (&frame);
949 }
950
951
952 static void
953 gst_video_overlay_rectangle_apply_global_alpha (GstVideoOverlayRectangle * rect,
954     float global_alpha)
955 {
956   guint8 *src, *dst;
957   GstVideoFrame frame;
958   gint i, j, w, h, stride;
959   gint argb_a, argb_r, argb_g, argb_b;
960
961   g_assert (!(rect->applied_global_alpha != 1.0
962           && rect->initial_alpha == NULL));
963
964   if (global_alpha == rect->applied_global_alpha)
965     return;
966
967   if (rect->initial_alpha == NULL)
968     gst_video_overlay_rectangle_extract_alpha (rect);
969
970   src = rect->initial_alpha;
971   rect->pixels = gst_buffer_make_writable (rect->pixels);
972
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);
978
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;
983
984   for (i = 0; i < h; i++) {
985     for (j = 0; j < w; j++) {
986       guint8 na = (guint8) (*src * global_alpha);
987
988       if (! !(rect->flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA)) {
989         dst[argb_r] =
990             (guint8) ((double) (dst[argb_r] * 255) / (double) dst[argb_a]) *
991             na / 255;
992         dst[argb_g] =
993             (guint8) ((double) (dst[argb_g] * 255) / (double) dst[argb_a]) *
994             na / 255;
995         dst[argb_b] =
996             (guint8) ((double) (dst[argb_b] * 255) / (double) dst[argb_a]) *
997             na / 255;
998       }
999       dst[argb_a] = na;
1000       src++;
1001       dst += 4;
1002     }
1003     dst += stride - 4 * w;
1004   }
1005   gst_video_frame_unmap (&frame);
1006
1007   rect->applied_global_alpha = global_alpha;
1008 }
1009
1010 static void
1011 gst_video_overlay_rectangle_convert (GstVideoInfo * src, GstBuffer * src_buffer,
1012     GstVideoFormat dest_format, GstVideoInfo * dest, GstBuffer ** dest_buffer)
1013 {
1014   gint width, height, stride;
1015   GstVideoFrame src_frame, dest_frame;
1016   GstVideoFormat format;
1017   gint k, l;
1018   guint8 *sdata, *ddata;
1019
1020   format = GST_VIDEO_INFO_FORMAT (src);
1021
1022   width = GST_VIDEO_INFO_WIDTH (src);
1023   height = GST_VIDEO_INFO_HEIGHT (src);
1024
1025   gst_video_info_init (dest);
1026   gst_video_info_set_format (dest, dest_format, width, height);
1027
1028   *dest_buffer = gst_buffer_new_and_alloc (GST_VIDEO_INFO_SIZE (dest));
1029
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);
1032
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);
1036
1037   if (format == GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV &&
1038       dest_format == GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB) {
1039     gint ayuv;
1040     gint a, y, u, v, r, g, b;
1041
1042     for (k = 0; k < height; k++) {
1043       for (l = 0; l < width; l++) {
1044         ayuv = GST_READ_UINT32_BE (sdata);
1045         a = ayuv >> 24;
1046         y = (ayuv >> 16) & 0xff;
1047         u = (ayuv >> 8) & 0xff;
1048         v = (ayuv & 0xff);
1049
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;
1053
1054         r = CLAMP (r, 0, 255);
1055         g = CLAMP (g, 0, 255);
1056         b = CLAMP (b, 0, 255);
1057
1058         /* native endian ARGB */
1059         *ddata = ((a << 24) | (r << 16) | (g << 8) | b);
1060
1061         sdata += 4;
1062         ddata += 4;
1063       }
1064       sdata += stride - 4 * width;
1065     }
1066   } else if (format == GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB &&
1067       dest_format == GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV) {
1068     gint argb;
1069     gint a, y, u, v, r, g, b;
1070
1071     for (k = 0; k < height; k++) {
1072       for (l = 0; l < width; l++) {
1073         /* native endian ARGB */
1074         argb = *sdata;
1075         a = argb >> 24;
1076         r = (argb >> 16) & 0xff;
1077         g = (argb >> 8) & 0xff;
1078         b = (argb & 0xff);
1079
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;
1083
1084         y = CLAMP (y, 0, 255);
1085         u = CLAMP (u, 0, 255);
1086         v = CLAMP (v, 0, 255);
1087
1088         GST_WRITE_UINT32_BE (ddata, ((a << 24) | (y << 16) | (u << 8) | v));
1089
1090         sdata += 4;
1091         ddata += 4;
1092       }
1093       sdata += stride - 4 * width;
1094     }
1095   } else {
1096     GST_ERROR ("unsupported conversion");
1097     g_assert_not_reached ();
1098   }
1099
1100   gst_video_frame_unmap (&src_frame);
1101   gst_video_frame_unmap (&dest_frame);
1102 }
1103
1104 static GstBuffer *
1105 gst_video_overlay_rectangle_get_pixels_raw_internal (GstVideoOverlayRectangle *
1106     rectangle, GstVideoOverlayFormatFlags flags, gboolean unscaled,
1107     GstVideoFormat wanted_format)
1108 {
1109   GstVideoOverlayFormatFlags new_flags;
1110   GstVideoOverlayRectangle *scaled_rect = NULL, *conv_rect = NULL;
1111   GstVideoInfo info;
1112   GstVideoFrame frame;
1113   GstBuffer *buf;
1114   GList *l;
1115   guint width, height;
1116   guint wanted_width;
1117   guint wanted_height;
1118   gboolean apply_global_alpha;
1119   gboolean revert_global_alpha;
1120   GstVideoFormat format;
1121
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);
1124
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);
1130
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));
1137
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,
1143           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;
1149     } else {
1150       /* only apply/revert global-alpha */
1151       scaled_rect = rectangle;
1152       goto done;
1153     }
1154   }
1155
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;
1160
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 */
1167       scaled_rect = r;
1168       break;
1169     }
1170   }
1171   GST_RECTANGLE_UNLOCK (rectangle);
1172
1173   if (scaled_rect != NULL)
1174     goto done;
1175
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;
1181
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 */
1186         conv_rect = r;
1187         break;
1188       }
1189     }
1190     GST_RECTANGLE_UNLOCK (rectangle);
1191   } else {
1192     conv_rect = rectangle;
1193   }
1194
1195   if (conv_rect == NULL) {
1196     GstVideoInfo conv_info;
1197
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);
1213   }
1214
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);
1219
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;
1223
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);
1227     info = scaled_info;
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,
1231           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;
1236   } else {
1237     /* do not need to scale or modify alpha values, almost done then */
1238     scaled_rect = conv_rect;
1239     goto done;
1240   }
1241
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;
1248     } else {
1249       gst_video_overlay_rectangle_premultiply (&frame);
1250       new_flags |= GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA;
1251     }
1252   }
1253   gst_video_frame_unmap (&frame);
1254
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);
1261
1262   GST_RECTANGLE_LOCK (rectangle);
1263   rectangle->scaled_rectangles =
1264       g_list_prepend (rectangle->scaled_rectangles, scaled_rect);
1265   GST_RECTANGLE_UNLOCK (rectangle);
1266
1267 done:
1268
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);
1278   }
1279   GST_RECTANGLE_UNLOCK (rectangle);
1280
1281   return scaled_rect->pixels;
1282 }
1283
1284
1285 /**
1286  * gst_video_overlay_rectangle_get_pixels_raw:
1287  * @rectangle: a #GstVideoOverlayRectangle
1288  * @flags: flags
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.
1293  *
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.
1300  */
1301 GstBuffer *
1302 gst_video_overlay_rectangle_get_pixels_raw (GstVideoOverlayRectangle *
1303     rectangle, GstVideoOverlayFormatFlags flags)
1304 {
1305   return gst_video_overlay_rectangle_get_pixels_raw_internal (rectangle,
1306       flags, FALSE, GST_VIDEO_INFO_FORMAT (&rectangle->info));
1307 }
1308
1309 /**
1310  * gst_video_overlay_rectangle_get_pixels_argb:
1311  * @rectangle: a #GstVideoOverlayRectangle
1312  * @flags: flags
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.
1317  *
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.
1323  */
1324 GstBuffer *
1325 gst_video_overlay_rectangle_get_pixels_argb (GstVideoOverlayRectangle *
1326     rectangle, GstVideoOverlayFormatFlags flags)
1327 {
1328   return gst_video_overlay_rectangle_get_pixels_raw_internal (rectangle,
1329       flags, FALSE, GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB);
1330 }
1331
1332 /**
1333  * gst_video_overlay_rectangle_get_pixels_ayuv:
1334  * @rectangle: a #GstVideoOverlayRectangle
1335  * @flags: flags
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.
1340  *
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.
1346  */
1347 GstBuffer *
1348 gst_video_overlay_rectangle_get_pixels_ayuv (GstVideoOverlayRectangle *
1349     rectangle, GstVideoOverlayFormatFlags flags)
1350 {
1351   return gst_video_overlay_rectangle_get_pixels_raw_internal (rectangle,
1352       flags, FALSE, GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV);
1353 }
1354
1355 /**
1356  * gst_video_overlay_rectangle_get_pixels_unscaled_raw:
1357  * @rectangle: a #GstVideoOverlayRectangle
1358  * @flags: flags.
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.
1363  *
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().
1368  *
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.
1372  */
1373 GstBuffer *
1374 gst_video_overlay_rectangle_get_pixels_unscaled_raw (GstVideoOverlayRectangle *
1375     rectangle, GstVideoOverlayFormatFlags flags)
1376 {
1377   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1378
1379   return gst_video_overlay_rectangle_get_pixels_raw_internal (rectangle,
1380       flags, TRUE, GST_VIDEO_INFO_FORMAT (&rectangle->info));
1381 }
1382
1383 /**
1384  * gst_video_overlay_rectangle_get_pixels_unscaled_argb:
1385  * @rectangle: a #GstVideoOverlayRectangle
1386  * @flags: flags.
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.
1391  *
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().
1396  *
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.
1400  */
1401 GstBuffer *
1402 gst_video_overlay_rectangle_get_pixels_unscaled_argb (GstVideoOverlayRectangle *
1403     rectangle, GstVideoOverlayFormatFlags flags)
1404 {
1405   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1406
1407   return gst_video_overlay_rectangle_get_pixels_raw_internal (rectangle,
1408       flags, TRUE, GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB);
1409 }
1410
1411 /**
1412  * gst_video_overlay_rectangle_get_pixels_unscaled_ayuv:
1413  * @rectangle: a #GstVideoOverlayRectangle
1414  * @flags: flags.
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.
1419  *
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().
1424  *
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.
1428  */
1429 GstBuffer *
1430 gst_video_overlay_rectangle_get_pixels_unscaled_ayuv (GstVideoOverlayRectangle *
1431     rectangle, GstVideoOverlayFormatFlags flags)
1432 {
1433   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1434
1435   return gst_video_overlay_rectangle_get_pixels_raw_internal (rectangle,
1436       flags, TRUE, GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV);
1437 }
1438
1439 /**
1440  * gst_video_overlay_rectangle_get_flags:
1441  * @rectangle: a #GstVideoOverlayRectangle
1442  *
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.
1448  *
1449  * Returns: the #GstVideoOverlayFormatFlags associated with the rectangle.
1450  */
1451 GstVideoOverlayFormatFlags
1452 gst_video_overlay_rectangle_get_flags (GstVideoOverlayRectangle * rectangle)
1453 {
1454   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle),
1455       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1456
1457   return rectangle->flags;
1458 }
1459
1460 /**
1461  * gst_video_overlay_rectangle_get_global_alpha:
1462  * @rectangle: a #GstVideoOverlayRectangle
1463  *
1464  * Retrieves the global-alpha value associated with a #GstVideoOverlayRectangle.
1465  *
1466  * Returns: the global-alpha value associated with the rectangle.
1467  */
1468 gfloat
1469 gst_video_overlay_rectangle_get_global_alpha (GstVideoOverlayRectangle *
1470     rectangle)
1471 {
1472   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), -1);
1473
1474   return rectangle->global_alpha;
1475 }
1476
1477 /**
1478  * gst_video_overlay_rectangle_set_global_alpha:
1479  * @rectangle: a #GstVideoOverlayRectangle
1480  *
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.
1484  *
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().
1489  */
1490 void
1491 gst_video_overlay_rectangle_set_global_alpha (GstVideoOverlayRectangle *
1492     rectangle, gfloat global_alpha)
1493 {
1494   g_return_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle));
1495   g_return_if_fail (global_alpha >= 0 && global_alpha <= 1);
1496
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;
1501     else
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
1506      * has changed. */
1507     rectangle->seq_num = gst_video_overlay_get_seqnum ();
1508   }
1509 }
1510
1511 /**
1512  * gst_video_overlay_rectangle_copy:
1513  * @rectangle: (transfer none): a #GstVideoOverlayRectangle to copy
1514  *
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
1518  * copied.
1519  *
1520  * Returns: (transfer full): a new #GstVideoOverlayRectangle equivalent
1521  *     to @rectangle.
1522  */
1523 GstVideoOverlayRectangle *
1524 gst_video_overlay_rectangle_copy (GstVideoOverlayRectangle * rectangle)
1525 {
1526   GstVideoOverlayRectangle *copy;
1527
1528   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1529
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);
1536
1537   return copy;
1538 }
1539
1540 /**
1541  * gst_video_overlay_rectangle_get_seqnum:
1542  * @rectangle: a #GstVideoOverlayRectangle
1543  *
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
1547  * a composition).
1548  *
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
1557  * pixel-data.
1558  *
1559  * Returns: the sequence number of @rectangle
1560  */
1561 guint
1562 gst_video_overlay_rectangle_get_seqnum (GstVideoOverlayRectangle * rectangle)
1563 {
1564   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), 0);
1565
1566   return rectangle->seq_num;
1567 }