Remove compatibility code cruft for old GLib versions
[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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, 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  * Since: 0.10.36
59  */
60
61 /* TODO:
62  *  - provide accessors for seq_num and other fields (as needed)
63  *  - allow overlay to set/get original pango markup string on/from rectangle
64  */
65
66 #ifdef HAVE_CONFIG_H
67 #include "config.h"
68 #endif
69
70 #include "video-overlay-composition.h"
71 #include "video-blend.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 _GstVideoOverlayCompositionClass
91 {
92   GstMiniObjectClass parent_class;
93 };
94
95 struct _GstVideoOverlayRectangle
96 {
97   GstMiniObject parent;
98
99   /* Position on video frame and dimension of output rectangle in
100    * output frame terms (already adjusted for the PAR of the output
101    * frame). x/y can be negative (overlay will be clipped then) */
102   gint x, y;
103   guint render_width, render_height;
104
105   /* Dimensions of overlay pixels */
106   guint width, height, stride;
107
108   /* The format of the data in pixels */
109   GstVideoFormat format;
110
111   /* The flags associated to this rectangle */
112   GstVideoOverlayFormatFlags flags;
113
114   /* Refcounted blob of memory, no caps or timestamps */
115   GstBuffer *pixels;
116
117   /* FIXME: how to express source like text or pango markup?
118    *        (just add source type enum + source buffer with data)
119    *
120    * FOR 0.10: always send pixel blobs, but attach source data in
121    * addition (reason: if downstream changes, we can't renegotiate
122    * that properly, if we just do a query of supported formats from
123    * the start). Sink will just ignore pixels and use pango markup
124    * from source data if it supports that.
125    *
126    * FOR 0.11: overlay should query formats (pango markup, pixels)
127    * supported by downstream and then only send that. We can
128    * renegotiate via the reconfigure event.
129    */
130
131   /* sequence number: useful for backends/renderers/sinks that want
132    * to maintain a cache of rectangles <-> surfaces. The value of
133    * the min_seq_num_used in the composition tells the renderer which
134    * rectangles have expired. */
135   guint seq_num;
136
137   /* FIXME: we may also need a (private) way to cache converted/scaled
138    * pixel blobs */
139   GMutex lock;
140
141   GList *scaled_rectangles;
142 };
143
144 struct _GstVideoOverlayRectangleClass
145 {
146   GstMiniObjectClass parent_class;
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 static void gst_video_overlay_composition_class_init (GstMiniObjectClass * k);
153 static void gst_video_overlay_composition_finalize (GstMiniObject * comp);
154 static void gst_video_overlay_rectangle_class_init (GstMiniObjectClass * klass);
155 static void gst_video_overlay_rectangle_finalize (GstMiniObject * rect);
156
157 /* --------------------------- utility functions --------------------------- */
158
159 #ifndef GST_DISABLE_GST_DEBUG
160
161 #define GST_CAT_DEFAULT ensure_debug_category()
162
163 static GstDebugCategory *
164 ensure_debug_category (void)
165 {
166   static gsize cat_gonce = 0;
167
168   if (g_once_init_enter (&cat_gonce)) {
169     gsize cat_done;
170
171     cat_done = (gsize) _gst_debug_category_new ("video-composition", 0,
172         "video overlay composition");
173
174     g_once_init_leave (&cat_gonce, cat_done);
175   }
176
177   return (GstDebugCategory *) cat_gonce;
178 }
179
180 #else
181
182 #define ensure_debug_category() /* NOOP */
183
184 #endif /* GST_DISABLE_GST_DEBUG */
185
186 static guint
187 gst_video_overlay_get_seqnum (void)
188 {
189   static gint seqnum;           /* 0 */
190
191   return (guint) g_atomic_int_add (&seqnum, 1);
192 }
193
194 #define GST_OVERLAY_COMPOSITION_QUARK gst_overlay_composition_quark_get()
195 static GQuark
196 gst_overlay_composition_quark_get (void)
197 {
198   static gsize quark_gonce = 0;
199
200   if (g_once_init_enter (&quark_gonce)) {
201     gsize quark;
202
203     quark = (gsize) g_quark_from_static_string ("GstVideoOverlayComposition");
204
205     g_once_init_leave (&quark_gonce, quark);
206   }
207
208   return (GQuark) quark_gonce;
209 }
210
211 #define COMPOSITION_QUARK composition_quark_get()
212 static GQuark
213 composition_quark_get (void)
214 {
215   static gsize quark_gonce = 0;
216
217   if (g_once_init_enter (&quark_gonce)) {
218     gsize quark;
219
220     quark = (gsize) g_quark_from_static_string ("composition");
221
222     g_once_init_leave (&quark_gonce, quark);
223   }
224
225   return (GQuark) quark_gonce;
226 }
227
228 /**
229  * gst_video_buffer_set_overlay_composition:
230  * @buf: a #GstBuffer
231  * @comp: (allow-none): a #GstVideoOverlayComposition, or NULL to clear a
232  *     previously-set composition
233  *
234  * Sets an overlay composition on a buffer. The buffer will obtain its own
235  * reference to the composition, meaning this function does not take ownership
236  * of @comp.
237  *
238  * Since: 0.10.36
239  */
240 void
241 gst_video_buffer_set_overlay_composition (GstBuffer * buf,
242     GstVideoOverlayComposition * comp)
243 {
244   gst_buffer_set_qdata (buf, GST_OVERLAY_COMPOSITION_QUARK,
245       gst_structure_id_new (GST_OVERLAY_COMPOSITION_QUARK,
246           COMPOSITION_QUARK, GST_TYPE_VIDEO_OVERLAY_COMPOSITION, comp, NULL));
247 }
248
249 /**
250  * gst_video_buffer_get_overlay_composition:
251  * @buf: a #GstBuffer
252  *
253  * Get the overlay composition that has previously been attached to a buffer
254  * with gst_video_buffer_get_overlay_composition(), usually by another element
255  * upstream.
256  *
257  * Returns: (transfer none): the #GstVideoOverlayComposition attached to
258  *    this buffer, or NULL. Does not return a reference to the composition,
259  *    caller must obtain her own ref via gst_video_overlay_composition_ref()
260  *    if needed.
261  *
262  * Since: 0.10.36
263  */
264 GstVideoOverlayComposition *
265 gst_video_buffer_get_overlay_composition (GstBuffer * buf)
266 {
267   const GstStructure *s;
268   const GValue *val;
269
270   s = gst_buffer_get_qdata (buf, GST_OVERLAY_COMPOSITION_QUARK);
271   if (s == NULL)
272     return NULL;
273
274   val = gst_structure_id_get_value (s, COMPOSITION_QUARK);
275   if (val == NULL)
276     return NULL;
277
278   return GST_VIDEO_OVERLAY_COMPOSITION (gst_value_get_mini_object (val));
279 }
280
281 /* ------------------------------ composition ------------------------------ */
282
283 #define RECTANGLE_ARRAY_STEP 4  /* premature optimization */
284
285 GType
286 gst_video_overlay_composition_get_type (void)
287 {
288   static volatile gsize type_id = 0;
289
290   if (g_once_init_enter (&type_id)) {
291     GType new_type_id = g_type_register_static_simple (GST_TYPE_MINI_OBJECT,
292         g_intern_static_string ("GstVideoOverlayComposition"),
293         sizeof (GstVideoOverlayCompositionClass),
294         (GClassInitFunc) gst_video_overlay_composition_class_init,
295         sizeof (GstVideoOverlayComposition),
296         NULL,
297         (GTypeFlags) 0);
298
299     g_once_init_leave (&type_id, new_type_id);
300   }
301
302   return type_id;
303 }
304
305 static void
306 gst_video_overlay_composition_finalize (GstMiniObject * mini_obj)
307 {
308   GstVideoOverlayComposition *comp = (GstVideoOverlayComposition *) mini_obj;
309   guint num;
310
311   num = comp->num_rectangles;
312
313   while (num > 0) {
314     gst_video_overlay_rectangle_unref (comp->rectangles[num - 1]);
315     --num;
316   }
317
318   g_free (comp->rectangles);
319   comp->rectangles = NULL;
320   comp->num_rectangles = 0;
321
322   /* not chaining up to GstMiniObject's finalize for now, we know it's empty */
323 }
324
325 static void
326 gst_video_overlay_composition_class_init (GstMiniObjectClass * klass)
327 {
328   klass->finalize = gst_video_overlay_composition_finalize;
329   klass->copy = (GstMiniObjectCopyFunction) gst_video_overlay_composition_copy;
330 }
331
332 /**
333  * gst_video_overlay_composition_new:
334  * @rectangle: (transfer none): a #GstVideoOverlayRectangle to add to the
335  *     composition
336  *
337  * Creates a new video overlay composition object to hold one or more
338  * overlay rectangles.
339  *
340  * Returns: (transfer full): a new #GstVideoOverlayComposition. Unref with
341  *     gst_video_overlay_composition_unref() when no longer needed.
342  *
343  * Since: 0.10.36
344  */
345 GstVideoOverlayComposition *
346 gst_video_overlay_composition_new (GstVideoOverlayRectangle * rectangle)
347 {
348   GstVideoOverlayComposition *comp;
349
350
351   /* FIXME: should we allow empty compositions? Could also be expressed as
352    * buffer without a composition on it. Maybe there are cases where doing
353    * an empty new + _add() in a loop is easier? */
354   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
355
356   comp = (GstVideoOverlayComposition *)
357       gst_mini_object_new (GST_TYPE_VIDEO_OVERLAY_COMPOSITION);
358
359   comp->rectangles = g_new0 (GstVideoOverlayRectangle *, RECTANGLE_ARRAY_STEP);
360   comp->rectangles[0] = gst_video_overlay_rectangle_ref (rectangle);
361   comp->num_rectangles = 1;
362
363   comp->seq_num = gst_video_overlay_get_seqnum ();
364
365   /* since the rectangle was created earlier, its seqnum is smaller than ours */
366   comp->min_seq_num_used = rectangle->seq_num;
367
368   GST_LOG ("new composition %p: seq_num %u with rectangle %p", comp,
369       comp->seq_num, rectangle);
370
371   return comp;
372 }
373
374 /**
375  * gst_video_overlay_composition_add_rectangle:
376  * @comp: a #GstVideoOverlayComposition
377  * @rectangle: (transfer none): a #GstVideoOverlayRectangle to add to the
378  *     composition
379  *
380  * Adds an overlay rectangle to an existing overlay composition object. This
381  * must be done right after creating the overlay composition.
382  *
383  * Since: 0.10.36
384  */
385 void
386 gst_video_overlay_composition_add_rectangle (GstVideoOverlayComposition * comp,
387     GstVideoOverlayRectangle * rectangle)
388 {
389   g_return_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp));
390   g_return_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle));
391   g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (comp) == 1);
392
393   if (comp->num_rectangles % RECTANGLE_ARRAY_STEP == 0) {
394     comp->rectangles =
395         g_renew (GstVideoOverlayRectangle *, comp->rectangles,
396         comp->num_rectangles + RECTANGLE_ARRAY_STEP);
397   }
398
399   comp->rectangles[comp->num_rectangles] =
400       gst_video_overlay_rectangle_ref (rectangle);
401   comp->num_rectangles += 1;
402
403   comp->min_seq_num_used = MIN (comp->min_seq_num_used, rectangle->seq_num);
404
405   GST_LOG ("composition %p: added rectangle %p", comp, rectangle);
406 }
407
408 /**
409  * gst_video_overlay_composition_n_rectangles:
410  * @comp: a #GstVideoOverlayComposition
411  *
412  * Returns the number of #GstVideoOverlayRectangle<!-- -->s contained in @comp.
413  *
414  * Returns: the number of rectangles
415  *
416  * Since: 0.10.36
417  */
418 guint
419 gst_video_overlay_composition_n_rectangles (GstVideoOverlayComposition * comp)
420 {
421   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), 0);
422
423   return comp->num_rectangles;
424 }
425
426 /**
427  * gst_video_overlay_composition_get_rectangle:
428  * @comp: a #GstVideoOverlayComposition
429  * @n: number of the rectangle to get
430  *
431  * Returns the @n-th #GstVideoOverlayRectangle contained in @comp.
432  *
433  * Returns: (transfer none): the @n-th rectangle, or NULL if @n is out of
434  *     bounds. Will not return a new reference, the caller will need to
435  *     obtain her own reference using gst_video_overlay_rectangle_ref()
436  *     if needed.
437  *
438  * Since: 0.10.36
439  */
440 GstVideoOverlayRectangle *
441 gst_video_overlay_composition_get_rectangle (GstVideoOverlayComposition * comp,
442     guint n)
443 {
444   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), NULL);
445
446   if (n >= comp->num_rectangles)
447     return NULL;
448
449   return comp->rectangles[n];
450 }
451
452 static gboolean
453 gst_video_overlay_rectangle_needs_scaling (GstVideoOverlayRectangle * r)
454 {
455   return (r->width != r->render_width || r->height != r->render_height);
456 }
457
458 /**
459  * gst_video_overlay_composition_blend:
460  * @comp: a #GstVideoOverlayComposition
461  * @video_buf: a #GstBuffer containing raw video data in a supported format
462  *
463  * Blends the overlay rectangles in @comp on top of the raw video data
464  * contained in @video_buf. The data in @video_buf must be writable. If
465  * needed, use gst_buffer_make_writable() before calling this function to
466  * ensure a buffer is writable. @video_buf must also have valid raw video
467  * caps set on it.
468  *
469  * Since: 0.10.36
470  */
471 gboolean
472 gst_video_overlay_composition_blend (GstVideoOverlayComposition * comp,
473     GstBuffer * video_buf)
474 {
475   GstBlendVideoFormatInfo video_info, rectangle_info;
476   GstVideoFormat fmt;
477   gboolean ret = TRUE;
478   guint n, num;
479   int w, h;
480
481   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), FALSE);
482   g_return_val_if_fail (GST_IS_BUFFER (video_buf), FALSE);
483   g_return_val_if_fail (gst_buffer_is_writable (video_buf), FALSE);
484   g_return_val_if_fail (GST_BUFFER_CAPS (video_buf) != NULL, FALSE);
485
486   if (!gst_video_format_parse_caps (GST_BUFFER_CAPS (video_buf), &fmt, &w, &h)) {
487     gchar *str = gst_caps_to_string (GST_BUFFER_CAPS (video_buf));
488     g_warning ("%s: could not parse video buffer caps '%s'", GST_FUNCTION, str);
489     g_free (str);
490     return FALSE;
491   }
492
493   video_blend_format_info_init (&video_info, GST_BUFFER_DATA (video_buf),
494       h, w, fmt, FALSE);
495
496   num = comp->num_rectangles;
497   GST_LOG ("Blending composition %p with %u rectangles onto video buffer %p "
498       "(%ux%u, format %u)", comp, num, video_buf, w, h, fmt);
499
500   for (n = 0; n < num; ++n) {
501     GstVideoOverlayRectangle *rect;
502     gboolean needs_scaling;
503
504     rect = comp->rectangles[n];
505
506     GST_LOG (" rectangle %u %p: %ux%u, format %u", n, rect, rect->height,
507         rect->width, rect->format);
508
509     video_blend_format_info_init (&rectangle_info,
510         GST_BUFFER_DATA (rect->pixels), rect->height, rect->width,
511         rect->format,
512         ! !(rect->flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA));
513
514     needs_scaling = gst_video_overlay_rectangle_needs_scaling (rect);
515     if (needs_scaling) {
516       video_blend_scale_linear_RGBA (&rectangle_info, rect->render_height,
517           rect->render_width);
518     }
519
520     ret = video_blend (&video_info, &rectangle_info, rect->x, rect->y);
521     if (!ret) {
522       GST_WARNING ("Could not blend overlay rectangle onto video buffer");
523     }
524
525     /* FIXME: should cache scaled pixels in the rectangle struct */
526     if (needs_scaling)
527       g_free (rectangle_info.pixels);
528   }
529
530   return ret;
531 }
532
533 /**
534  * gst_video_overlay_composition_copy:
535  * @comp: (transfer none): a #GstVideoOverlayComposition to copy
536  *
537  * Makes a copy of @comp and all contained rectangles, so that it is possible
538  * to modify the composition and contained rectangles (e.g. add additional
539  * rectangles or change the render co-ordinates or render dimension). The
540  * actual overlay pixel data buffers contained in the rectangles are not
541  * copied.
542  *
543  * Returns: (transfer full): a new #GstVideoOverlayComposition equivalent
544  *     to @comp.
545  *
546  * Since: 0.10.36
547  */
548 GstVideoOverlayComposition *
549 gst_video_overlay_composition_copy (GstVideoOverlayComposition * comp)
550 {
551   GstVideoOverlayComposition *copy;
552   GstVideoOverlayRectangle *rect;
553   guint n;
554
555   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), NULL);
556
557   if (G_LIKELY (comp->num_rectangles == 0))
558     return gst_video_overlay_composition_new (NULL);
559
560   rect = gst_video_overlay_rectangle_copy (comp->rectangles[0]);
561   copy = gst_video_overlay_composition_new (rect);
562   gst_video_overlay_rectangle_unref (rect);
563
564   for (n = 1; n < comp->num_rectangles; ++n) {
565     rect = gst_video_overlay_rectangle_copy (comp->rectangles[n]);
566     gst_video_overlay_composition_add_rectangle (copy, rect);
567     gst_video_overlay_rectangle_unref (rect);
568   }
569
570   return copy;
571 }
572
573 /**
574  * gst_video_overlay_composition_make_writable:
575  * @comp: (transfer full): a #GstVideoOverlayComposition to copy
576  *
577  * Takes ownership of @comp and returns a version of @comp that is writable
578  * (i.e. can be modified). Will either return @comp right away, or create a
579  * new writable copy of @comp and unref @comp itself. All the contained
580  * rectangles will also be copied, but the actual overlay pixel data buffers
581  * contained in the rectangles are not copied.
582  *
583  * Returns: (transfer full): a writable #GstVideoOverlayComposition
584  *     equivalent to @comp.
585  *
586  * Since: 0.10.36
587  */
588 GstVideoOverlayComposition *
589 gst_video_overlay_composition_make_writable (GstVideoOverlayComposition * comp)
590 {
591   GstVideoOverlayComposition *writable_comp;
592
593   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), NULL);
594
595   if (GST_MINI_OBJECT_REFCOUNT_VALUE (comp) == 1) {
596     guint n;
597
598     for (n = 0; n < comp->num_rectangles; ++n) {
599       if (GST_MINI_OBJECT_REFCOUNT_VALUE (comp->rectangles[n]) != 1)
600         goto copy;
601     }
602     return comp;
603   }
604
605 copy:
606
607   writable_comp = gst_video_overlay_composition_copy (comp);
608   gst_video_overlay_composition_unref (comp);
609
610   return writable_comp;
611 }
612
613 /**
614  * gst_video_overlay_composition_get_seqnum:
615  * @comp: a #GstVideoOverlayComposition
616  *
617  * Returns the sequence number of this composition. Sequence numbers are
618  * monotonically increasing and unique for overlay compositions and rectangles
619  * (meaning there will never be a rectangle with the same sequence number as
620  * a composition).
621  *
622  * Returns: the sequence number of @comp
623  *
624  * Since: 0.10.36
625  */
626 guint
627 gst_video_overlay_composition_get_seqnum (GstVideoOverlayComposition * comp)
628 {
629   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_COMPOSITION (comp), 0);
630
631   return comp->seq_num;
632 }
633
634 /* ------------------------------ rectangles ------------------------------ -*/
635
636 static void gst_video_overlay_rectangle_instance_init (GstMiniObject * miniobj);
637
638 GType
639 gst_video_overlay_rectangle_get_type (void)
640 {
641   static volatile gsize type_id = 0;
642
643   if (g_once_init_enter (&type_id)) {
644     GType new_type_id = g_type_register_static_simple (GST_TYPE_MINI_OBJECT,
645         g_intern_static_string ("GstVideoOverlayRectangle"),
646         sizeof (GstVideoOverlayRectangleClass),
647         (GClassInitFunc) gst_video_overlay_rectangle_class_init,
648         sizeof (GstVideoOverlayRectangle),
649         (GInstanceInitFunc) gst_video_overlay_rectangle_instance_init,
650         (GTypeFlags) 0);
651
652     g_once_init_leave (&type_id, new_type_id);
653   }
654
655   return type_id;
656 }
657
658 static void
659 gst_video_overlay_rectangle_finalize (GstMiniObject * mini_obj)
660 {
661   GstVideoOverlayRectangle *rect = (GstVideoOverlayRectangle *) mini_obj;
662
663   gst_buffer_replace (&rect->pixels, NULL);
664
665   while (rect->scaled_rectangles != NULL) {
666     GstVideoOverlayRectangle *scaled_rect = rect->scaled_rectangles->data;
667
668     gst_video_overlay_rectangle_unref (scaled_rect);
669
670     rect->scaled_rectangles =
671         g_list_delete_link (rect->scaled_rectangles, rect->scaled_rectangles);
672   }
673   g_mutex_clear (&rect->lock);
674
675   /* not chaining up to GstMiniObject's finalize for now, we know it's empty */
676 }
677
678 static void
679 gst_video_overlay_rectangle_class_init (GstMiniObjectClass * klass)
680 {
681   klass->finalize = gst_video_overlay_rectangle_finalize;
682   klass->copy = (GstMiniObjectCopyFunction) gst_video_overlay_rectangle_copy;
683 }
684
685 static void
686 gst_video_overlay_rectangle_instance_init (GstMiniObject * mini_obj)
687 {
688   GstVideoOverlayRectangle *rect = (GstVideoOverlayRectangle *) mini_obj;
689
690   g_mutex_init (&rect->lock);
691 }
692
693 static inline gboolean
694 gst_video_overlay_rectangle_check_flags (GstVideoOverlayFormatFlags flags)
695 {
696   /* Check flags only contains flags we know about */
697   return (flags & ~(GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA)) == 0;
698 }
699
700 static gboolean
701 gst_video_overlay_rectangle_is_same_alpha_type (GstVideoOverlayFormatFlags
702     flags1, GstVideoOverlayFormatFlags flags2)
703 {
704   return ((flags1 ^ flags2) & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA)
705       == 0;
706 }
707
708
709 /**
710  * gst_video_overlay_rectangle_new_argb:
711  * @pixels: (transfer none): a #GstBuffer pointing to the pixel memory
712  * @width: the width of the rectangle in @pixels
713  * @height: the height of the rectangle in @pixels
714  * @stride: the stride of the rectangle in @pixels in bytes (&gt;= 4*width)
715  * @render_x: the X co-ordinate on the video where the top-left corner of this
716  *     overlay rectangle should be rendered to
717  * @render_y: the Y co-ordinate on the video where the top-left corner of this
718  *     overlay rectangle should be rendered to
719  * @render_width: the render width of this rectangle on the video
720  * @render_height: the render height of this rectangle on the video
721  * @flags: flags
722  *
723  * Creates a new video overlay rectangle with ARGB pixel data. The layout
724  * of the components in memory is B-G-R-A on little-endian platforms
725  * (corresponding to #GST_VIDEO_FORMAT_BGRA) and A-R-G-B on big-endian
726  * platforms (corresponding to #GST_VIDEO_FORMAT_ARGB). In other words,
727  * pixels are treated as 32-bit words and the lowest 8 bits then contain
728  * the blue component value and the highest 8 bits contain the alpha
729  * component value. Unless specified in the flags, the RGB values are
730  * non-premultiplied. This is the format that is used by most hardware,
731  * and also many rendering libraries such as Cairo, for example.
732  *
733  * Returns: (transfer full): a new #GstVideoOverlayRectangle. Unref with
734  *     gst_video_overlay_rectangle_unref() when no longer needed.
735  *
736  * Since: 0.10.36
737  */
738 GstVideoOverlayRectangle *
739 gst_video_overlay_rectangle_new_argb (GstBuffer * pixels,
740     guint width, guint height, guint stride, gint render_x, gint render_y,
741     guint render_width, guint render_height, GstVideoOverlayFormatFlags flags)
742 {
743   GstVideoOverlayRectangle *rect;
744
745   g_return_val_if_fail (GST_IS_BUFFER (pixels), NULL);
746   /* technically ((height-1)*stride)+width might be okay too */
747   g_return_val_if_fail (GST_BUFFER_SIZE (pixels) >= height * stride, NULL);
748   g_return_val_if_fail (stride >= (4 * width), NULL);
749   g_return_val_if_fail (height > 0 && width > 0, NULL);
750   g_return_val_if_fail (render_height > 0 && render_width > 0, NULL);
751   g_return_val_if_fail (gst_video_overlay_rectangle_check_flags (flags), NULL);
752
753   rect = (GstVideoOverlayRectangle *)
754       gst_mini_object_new (GST_TYPE_VIDEO_OVERLAY_RECTANGLE);
755
756 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
757   rect->format = GST_VIDEO_FORMAT_BGRA;
758 #else
759   rect->format = GST_VIDEO_FORMAT_ARGB;
760 #endif
761
762   rect->pixels = gst_buffer_ref (pixels);
763
764   rect->width = width;
765   rect->height = height;
766   rect->stride = stride;
767
768   rect->x = render_x;
769   rect->y = render_y;
770   rect->render_width = render_width;
771   rect->render_height = render_height;
772
773   rect->flags = flags;
774
775   rect->seq_num = gst_video_overlay_get_seqnum ();
776
777   GST_LOG ("new rectangle %p: %ux%u => %ux%u @ %u,%u, seq_num %u, format %u, "
778       "flags %x, pixels %p", rect, width, height, render_width, render_height,
779       render_x, render_y, rect->seq_num, rect->format, rect->flags, pixels);
780
781   return rect;
782 }
783
784 /**
785  * gst_video_overlay_rectangle_get_render_rectangle:
786  * @rectangle: a #GstVideoOverlayRectangle
787  * @render_x: (out) (allow-none): address where to store the X render offset
788  * @render_y: (out) (allow-none): address where to store the Y render offset
789  * @render_width: (out) (allow-none): address where to store the render width
790  * @render_height: (out) (allow-none): address where to store the render height
791  *
792  * Retrieves the render position and render dimension of the overlay
793  * rectangle on the video.
794  *
795  * Returns: TRUE if valid render dimensions were retrieved.
796  *
797  * Since: 0.10.36
798  */
799 gboolean
800 gst_video_overlay_rectangle_get_render_rectangle (GstVideoOverlayRectangle *
801     rectangle, gint * render_x, gint * render_y, guint * render_width,
802     guint * render_height)
803 {
804   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), FALSE);
805
806   if (render_x)
807     *render_x = rectangle->x;
808   if (render_y)
809     *render_y = rectangle->y;
810   if (render_width)
811     *render_width = rectangle->render_width;
812   if (render_height)
813     *render_height = rectangle->render_height;
814
815   return TRUE;
816 }
817
818 /**
819  * gst_video_overlay_rectangle_set_render_rectangle:
820  * @rectangle: a #GstVideoOverlayRectangle
821  * @render_x: render X position of rectangle on video
822  * @render_y: render Y position of rectangle on video
823  * @render_width: render width of rectangle
824  * @render_height: render height of rectangle
825  *
826  * Sets the render position and dimensions of the rectangle on the video.
827  * This function is mainly for elements that modify the size of the video
828  * in some way (e.g. through scaling or cropping) and need to adjust the
829  * details of any overlays to match the operation that changed the size.
830  *
831  * @rectangle must be writable, meaning its refcount must be 1. You can
832  * make the rectangles inside a #GstVideoOverlayComposition writable using
833  * gst_video_overlay_composition_make_writable() or
834  * gst_video_overlay_composition_copy().
835  *
836  * Since: 0.10.36
837  */
838 void
839 gst_video_overlay_rectangle_set_render_rectangle (GstVideoOverlayRectangle *
840     rectangle, gint render_x, gint render_y, guint render_width,
841     guint render_height)
842 {
843   g_return_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle));
844   g_return_if_fail (GST_MINI_OBJECT_REFCOUNT_VALUE (rectangle) == 1);
845
846   rectangle->x = render_x;
847   rectangle->y = render_y;
848   rectangle->render_width = render_width;
849   rectangle->render_height = render_height;
850 }
851
852 static void
853 gst_video_overlay_rectangle_premultiply (GstBlendVideoFormatInfo * info)
854 {
855   int i, j;
856   for (j = 0; j < info->height; ++j) {
857     guint8 *line = info->pixels + info->stride[0] * j;
858     for (i = 0; i < info->width; ++i) {
859       int a = line[0];
860       line[1] = line[1] * a / 255;
861       line[2] = line[2] * a / 255;
862       line[3] = line[3] * a / 255;
863       line += 4;
864     }
865   }
866 }
867
868 static void
869 gst_video_overlay_rectangle_unpremultiply (GstBlendVideoFormatInfo * info)
870 {
871   int i, j;
872   for (j = 0; j < info->height; ++j) {
873     guint8 *line = info->pixels + info->stride[0] * j;
874     for (i = 0; i < info->width; ++i) {
875       int a = line[0];
876       if (a) {
877         line[1] = MIN ((line[1] * 255 + a / 2) / a, 255);
878         line[2] = MIN ((line[2] * 255 + a / 2) / a, 255);
879         line[3] = MIN ((line[3] * 255 + a / 2) / a, 255);
880       }
881       line += 4;
882     }
883   }
884 }
885
886 static GstBuffer *
887 gst_video_overlay_rectangle_get_pixels_argb_internal (GstVideoOverlayRectangle *
888     rectangle, guint * stride, GstVideoOverlayFormatFlags flags,
889     gboolean unscaled)
890 {
891   GstVideoOverlayRectangle *scaled_rect = NULL;
892   GstBlendVideoFormatInfo info;
893   GstBuffer *buf;
894   GList *l;
895   guint wanted_width = unscaled ? rectangle->width : rectangle->render_width;
896   guint wanted_height = unscaled ? rectangle->height : rectangle->render_height;
897
898   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
899   g_return_val_if_fail (stride != NULL, NULL);
900   g_return_val_if_fail (gst_video_overlay_rectangle_check_flags (flags), NULL);
901
902   /* This assumes we don't need to adjust the format */
903   if (wanted_width == rectangle->width &&
904       wanted_height == rectangle->height &&
905       gst_video_overlay_rectangle_is_same_alpha_type (rectangle->flags,
906           flags)) {
907     *stride = rectangle->stride;
908     return rectangle->pixels;
909   }
910
911   /* see if we've got one cached already */
912   GST_RECTANGLE_LOCK (rectangle);
913   for (l = rectangle->scaled_rectangles; l != NULL; l = l->next) {
914     GstVideoOverlayRectangle *r = l->data;
915
916     if (r->width == wanted_width &&
917         r->height == wanted_height &&
918         gst_video_overlay_rectangle_is_same_alpha_type (rectangle->flags,
919             flags)) {
920       /* we'll keep these rectangles around until finalize, so it's ok not
921        * to take our own ref here */
922       scaled_rect = r;
923       break;
924     }
925   }
926   GST_RECTANGLE_UNLOCK (rectangle);
927
928   if (scaled_rect != NULL)
929     goto done;
930
931   /* not cached yet, do the scaling and put the result into our cache */
932   video_blend_format_info_init (&info, GST_BUFFER_DATA (rectangle->pixels),
933       rectangle->height, rectangle->width, rectangle->format,
934       ! !(rectangle->flags &
935           GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA));
936
937   if (wanted_width != rectangle->width || wanted_height != rectangle->height) {
938     video_blend_scale_linear_RGBA (&info, wanted_height, wanted_width);
939   }
940
941   if (!gst_video_overlay_rectangle_is_same_alpha_type (rectangle->flags, flags)) {
942     if (rectangle->flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA) {
943       gst_video_overlay_rectangle_unpremultiply (&info);
944     } else {
945       gst_video_overlay_rectangle_premultiply (&info);
946     }
947   }
948
949   buf = gst_buffer_new ();
950   GST_BUFFER_DATA (buf) = info.pixels;
951   GST_BUFFER_MALLOCDATA (buf) = info.pixels;
952   GST_BUFFER_SIZE (buf) = info.size;
953
954   scaled_rect = gst_video_overlay_rectangle_new_argb (buf,
955       wanted_width, wanted_height, info.stride[0],
956       0, 0, wanted_width, wanted_height, rectangle->flags);
957
958   gst_buffer_unref (buf);
959
960   GST_RECTANGLE_LOCK (rectangle);
961   rectangle->scaled_rectangles =
962       g_list_prepend (rectangle->scaled_rectangles, scaled_rect);
963   GST_RECTANGLE_UNLOCK (rectangle);
964
965 done:
966
967   *stride = scaled_rect->stride;
968   return scaled_rect->pixels;
969 }
970
971 /**
972  * gst_video_overlay_rectangle_get_pixels_argb:
973  * @rectangle: a #GstVideoOverlayRectangle
974  * @stride: (out) (allow-none): address of guint variable where to store the
975  *    row stride of the ARGB pixel data in the buffer
976  * @flags: flags
977  *
978  * Returns: (transfer none): a #GstBuffer holding the ARGB pixel data with
979  *    row stride @stride and width and height of the render dimensions as per
980  *    gst_video_overlay_rectangle_get_render_rectangle(). This function does
981  *    not return a reference, the caller should obtain a reference of her own
982  *    with gst_buffer_ref() if needed.
983  *
984  * Since: 0.10.36
985  */
986 GstBuffer *
987 gst_video_overlay_rectangle_get_pixels_argb (GstVideoOverlayRectangle *
988     rectangle, guint * stride, GstVideoOverlayFormatFlags flags)
989 {
990   return gst_video_overlay_rectangle_get_pixels_argb_internal (rectangle,
991       stride, flags, FALSE);
992 }
993
994 /**
995  * gst_video_overlay_rectangle_get_pixels_unscaled_argb:
996  * @rectangle: a #GstVideoOverlayRectangle
997  * @width: (out): address where to store the width of the unscaled
998  *    rectangle in pixels
999  * @height: (out): address where to store the height of the unscaled
1000  *    rectangle in pixels
1001  * @stride: (out): address of guint variable where to store the row
1002  *    stride of the ARGB pixel data in the buffer
1003  * @flags: flags
1004  *
1005  * Retrieves the pixel data as it is. This is useful if the caller can
1006  * do the scaling itself when handling the overlaying. The rectangle will
1007  * need to be scaled to the render dimensions, which can be retrieved using
1008  * gst_video_overlay_rectangle_get_render_rectangle().
1009  *
1010  * Returns: (transfer none): a #GstBuffer holding the ARGB pixel data with
1011  *    row stride @stride. This function does not return a reference, the caller
1012  *    should obtain a reference of her own with gst_buffer_ref() if needed.
1013  *
1014  * Since: 0.10.36
1015  */
1016 GstBuffer *
1017 gst_video_overlay_rectangle_get_pixels_unscaled_argb (GstVideoOverlayRectangle *
1018     rectangle, guint * width, guint * height, guint * stride,
1019     GstVideoOverlayFormatFlags flags)
1020 {
1021   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1022   g_return_val_if_fail (width != NULL, NULL);
1023   g_return_val_if_fail (height != NULL, NULL);
1024   g_return_val_if_fail (stride != NULL, NULL);
1025
1026   *width = rectangle->width;
1027   *height = rectangle->height;
1028   return gst_video_overlay_rectangle_get_pixels_argb_internal (rectangle,
1029       stride, flags, TRUE);
1030 }
1031
1032 /**
1033  * gst_video_overlay_rectangle_get_flags:
1034  * @rectangle: a #GstVideoOverlayRectangle
1035  *
1036  * Retrieves the flags associated with a #GstVideoOverlayRectangle.
1037  * This is useful if the caller can handle both premultiplied alpha and
1038  * non premultiplied alpha, for example. By knowing whether the rectangle
1039  * uses premultiplied or not, it can request the pixel data in the format
1040  * it is stored in, to avoid unnecessary conversion.
1041  *
1042  * Returns: the #GstVideoOverlayFormatFlags associated with the rectangle.
1043  *
1044  * Since: 0.10.37
1045  */
1046 GstVideoOverlayFormatFlags
1047 gst_video_overlay_rectangle_get_flags (GstVideoOverlayRectangle * rectangle)
1048 {
1049   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle),
1050       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1051
1052   return rectangle->flags;
1053 }
1054
1055 /**
1056  * gst_video_overlay_rectangle_copy:
1057  * @rectangle: (transfer none): a #GstVideoOverlayRectangle to copy
1058  *
1059  * Makes a copy of @rectangle, so that it is possible to modify it
1060  * (e.g. to change the render co-ordinates or render dimension). The
1061  * actual overlay pixel data buffers contained in the rectangle are not
1062  * copied.
1063  *
1064  * Returns: (transfer full): a new #GstVideoOverlayRectangle equivalent
1065  *     to @rectangle.
1066  *
1067  * Since: 0.10.36
1068  */
1069 GstVideoOverlayRectangle *
1070 gst_video_overlay_rectangle_copy (GstVideoOverlayRectangle * rectangle)
1071 {
1072   GstVideoOverlayRectangle *copy;
1073
1074   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), NULL);
1075
1076   copy = gst_video_overlay_rectangle_new_argb (rectangle->pixels,
1077       rectangle->width, rectangle->height, rectangle->stride,
1078       rectangle->x, rectangle->y,
1079       rectangle->render_width, rectangle->render_height, rectangle->flags);
1080
1081   return copy;
1082 }
1083
1084 /**
1085  * gst_video_overlay_rectangle_get_seqnum:
1086  * @rectangle: a #GstVideoOverlayRectangle
1087  *
1088  * Returns the sequence number of this rectangle. Sequence numbers are
1089  * monotonically increasing and unique for overlay compositions and rectangles
1090  * (meaning there will never be a rectangle with the same sequence number as
1091  * a composition).
1092  *
1093  * Returns: the sequence number of @rectangle
1094  *
1095  * Since: 0.10.36
1096  */
1097 guint
1098 gst_video_overlay_rectangle_get_seqnum (GstVideoOverlayRectangle * rectangle)
1099 {
1100   g_return_val_if_fail (GST_IS_VIDEO_OVERLAY_RECTANGLE (rectangle), 0);
1101
1102   return rectangle->seq_num;
1103 }