video: fix some GIR annotations
[platform/upstream/gstreamer.git] / gst-libs / gst / video / gstvideometa.c
1 /* GStreamer
2  * Copyright (C) <2011> Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "gstvideometa.h"
21
22 #include <string.h>
23
24 /**
25  * SECTION:gstvideometa
26  * @title: GstMeta for video
27  * @short_description: Video related GstMeta
28  *
29  */
30
31 #ifndef GST_DISABLE_GST_DEBUG
32 #define GST_CAT_DEFAULT ensure_debug_category()
33 static GstDebugCategory *
34 ensure_debug_category (void)
35 {
36   static gsize cat_gonce = 0;
37
38   if (g_once_init_enter (&cat_gonce)) {
39     gsize cat_done;
40
41     cat_done = (gsize) _gst_debug_category_new ("videometa", 0, "videometa");
42
43     g_once_init_leave (&cat_gonce, cat_done);
44   }
45
46   return (GstDebugCategory *) cat_gonce;
47 }
48 #else
49 #define ensure_debug_category() /* NOOP */
50 #endif /* GST_DISABLE_GST_DEBUG */
51
52 static gboolean
53 gst_video_meta_init (GstMeta * meta, gpointer params, GstBuffer * buffer)
54 {
55   GstVideoMeta *emeta = (GstVideoMeta *) meta;
56
57   emeta->buffer = NULL;
58   emeta->flags = GST_VIDEO_FRAME_FLAG_NONE;
59   emeta->format = GST_VIDEO_FORMAT_UNKNOWN;
60   emeta->id = 0;
61   emeta->width = emeta->height = emeta->n_planes = 0;
62   memset (emeta->offset, 0, sizeof (emeta->offset));
63   memset (emeta->stride, 0, sizeof (emeta->stride));
64   emeta->map = NULL;
65   emeta->unmap = NULL;
66
67   return TRUE;
68 }
69
70 static gboolean
71 gst_video_meta_transform (GstBuffer * dest, GstMeta * meta,
72     GstBuffer * buffer, GQuark type, gpointer data)
73 {
74   GstVideoMeta *dmeta, *smeta;
75   guint i;
76
77   smeta = (GstVideoMeta *) meta;
78
79   if (GST_META_TRANSFORM_IS_COPY (type)) {
80     GstMetaTransformCopy *copy = data;
81
82     if (!copy->region) {
83       /* only copy if the complete data is copied as well */
84       dmeta =
85           (GstVideoMeta *) gst_buffer_add_meta (dest, GST_VIDEO_META_INFO,
86           NULL);
87
88       if (!dmeta)
89         return FALSE;
90
91       dmeta->buffer = dest;
92
93       GST_DEBUG ("copy video metadata");
94       dmeta->flags = smeta->flags;
95       dmeta->format = smeta->format;
96       dmeta->id = smeta->id;
97       dmeta->width = smeta->width;
98       dmeta->height = smeta->height;
99
100       dmeta->n_planes = smeta->n_planes;
101       for (i = 0; i < dmeta->n_planes; i++) {
102         dmeta->offset[i] = smeta->offset[i];
103         dmeta->stride[i] = smeta->stride[i];
104       }
105       dmeta->map = smeta->map;
106       dmeta->unmap = smeta->unmap;
107     }
108   } else {
109     /* return FALSE, if transform type is not supported */
110     return FALSE;
111   }
112   return TRUE;
113 }
114
115 GType
116 gst_video_meta_api_get_type (void)
117 {
118   static volatile GType type = 0;
119   static const gchar *tags[] =
120       { GST_META_TAG_VIDEO_STR, GST_META_TAG_MEMORY_STR,
121     GST_META_TAG_VIDEO_COLORSPACE_STR,
122     GST_META_TAG_VIDEO_SIZE_STR, NULL
123   };
124
125   if (g_once_init_enter (&type)) {
126     GType _type = gst_meta_api_type_register ("GstVideoMetaAPI", tags);
127     g_once_init_leave (&type, _type);
128   }
129   return type;
130 }
131
132 /* video metadata */
133 const GstMetaInfo *
134 gst_video_meta_get_info (void)
135 {
136   static const GstMetaInfo *video_meta_info = NULL;
137
138   if (g_once_init_enter ((GstMetaInfo **) & video_meta_info)) {
139     const GstMetaInfo *meta =
140         gst_meta_register (GST_VIDEO_META_API_TYPE, "GstVideoMeta",
141         sizeof (GstVideoMeta), (GstMetaInitFunction) gst_video_meta_init,
142         (GstMetaFreeFunction) NULL, gst_video_meta_transform);
143     g_once_init_leave ((GstMetaInfo **) & video_meta_info,
144         (GstMetaInfo *) meta);
145   }
146   return video_meta_info;
147 }
148
149 /**
150  * gst_buffer_get_video_meta:
151  * @buffer: a #GstBuffer
152  *
153  * Find the #GstVideoMeta on @buffer with the lowest @id.
154  *
155  * Buffers can contain multiple #GstVideoMeta metadata items when dealing with
156  * multiview buffers.
157  *
158  * Returns: (transfer none): the #GstVideoMeta with lowest id (usually 0) or %NULL when there
159  * is no such metadata on @buffer.
160  */
161 GstVideoMeta *
162 gst_buffer_get_video_meta (GstBuffer * buffer)
163 {
164   gpointer state = NULL;
165   GstVideoMeta *out = NULL;
166   GstMeta *meta;
167   const GstMetaInfo *info = GST_VIDEO_META_INFO;
168
169   while ((meta = gst_buffer_iterate_meta (buffer, &state))) {
170     if (meta->info->api == info->api) {
171       GstVideoMeta *vmeta = (GstVideoMeta *) meta;
172       if (vmeta->id == 0)
173         return vmeta;           /* Early out for id 0 */
174       if (out == NULL || vmeta->id < out->id)
175         out = vmeta;
176     }
177   }
178   return out;
179 }
180
181 /**
182  * gst_buffer_get_video_meta_id:
183  * @buffer: a #GstBuffer
184  * @id: a metadata id
185  *
186  * Find the #GstVideoMeta on @buffer with the given @id.
187  *
188  * Buffers can contain multiple #GstVideoMeta metadata items when dealing with
189  * multiview buffers.
190  *
191  * Returns: (transfer none): the #GstVideoMeta with @id or %NULL when there is no such metadata
192  * on @buffer.
193  */
194 GstVideoMeta *
195 gst_buffer_get_video_meta_id (GstBuffer * buffer, gint id)
196 {
197   gpointer state = NULL;
198   GstMeta *meta;
199   const GstMetaInfo *info = GST_VIDEO_META_INFO;
200
201   while ((meta = gst_buffer_iterate_meta (buffer, &state))) {
202     if (meta->info->api == info->api) {
203       GstVideoMeta *vmeta = (GstVideoMeta *) meta;
204       if (vmeta->id == id)
205         return vmeta;
206     }
207   }
208   return NULL;
209 }
210
211 static gboolean
212 default_map (GstVideoMeta * meta, guint plane, GstMapInfo * info,
213     gpointer * data, gint * stride, GstMapFlags flags)
214 {
215   guint idx, length;
216   gsize offset, skip;
217   GstBuffer *buffer = meta->buffer;
218
219   offset = meta->offset[plane];
220
221   /* find the memory block for this plane, this is the memory block containing
222    * the plane offset. FIXME use plane size */
223   if (!gst_buffer_find_memory (buffer, offset, 1, &idx, &length, &skip))
224     goto no_memory;
225
226   if (!gst_buffer_map_range (buffer, idx, length, info, flags))
227     goto cannot_map;
228
229   *stride = meta->stride[plane];
230   *data = (guint8 *) info->data + skip;
231
232   return TRUE;
233
234   /* ERRORS */
235 no_memory:
236   {
237     GST_DEBUG ("plane %u, no memory at offset %" G_GSIZE_FORMAT, plane, offset);
238     return FALSE;
239   }
240 cannot_map:
241   {
242     GST_DEBUG ("cannot map memory range %u-%u", idx, length);
243     return FALSE;
244   }
245 }
246
247 static gboolean
248 default_unmap (GstVideoMeta * meta, guint plane, GstMapInfo * info)
249 {
250   GstBuffer *buffer = meta->buffer;
251
252   gst_buffer_unmap (buffer, info);
253
254   return TRUE;
255 }
256
257 /**
258  * gst_buffer_add_video_meta:
259  * @buffer: a #GstBuffer
260  * @flags: #GstVideoFrameFlags
261  * @format: a #GstVideoFormat
262  * @width: the width
263  * @height: the height
264  *
265  * Attaches GstVideoMeta metadata to @buffer with the given parameters and the
266  * default offsets and strides for @format and @width x @height.
267  *
268  * This function calculates the default offsets and strides and then calls
269  * gst_buffer_add_video_meta_full() with them.
270  *
271  * Returns: (transfer none): the #GstVideoMeta on @buffer.
272  */
273 GstVideoMeta *
274 gst_buffer_add_video_meta (GstBuffer * buffer,
275     GstVideoFrameFlags flags, GstVideoFormat format, guint width, guint height)
276 {
277   GstVideoMeta *meta;
278   GstVideoInfo info;
279
280   if (!gst_video_info_set_format (&info, format, width, height))
281     return NULL;
282
283   meta =
284       gst_buffer_add_video_meta_full (buffer, flags, format, width,
285       height, info.finfo->n_planes, info.offset, info.stride);
286
287   return meta;
288 }
289
290 /**
291  * gst_buffer_add_video_meta_full:
292  * @buffer: a #GstBuffer
293  * @flags: #GstVideoFrameFlags
294  * @format: a #GstVideoFormat
295  * @width: the width
296  * @height: the height
297  * @n_planes: number of planes
298  * @offset: offset of each plane
299  * @stride: stride of each plane
300  *
301  * Attaches GstVideoMeta metadata to @buffer with the given parameters.
302  *
303  * Returns: (transfer none): the #GstVideoMeta on @buffer.
304  */
305 GstVideoMeta *
306 gst_buffer_add_video_meta_full (GstBuffer * buffer,
307     GstVideoFrameFlags flags, GstVideoFormat format, guint width,
308     guint height, guint n_planes, gsize offset[GST_VIDEO_MAX_PLANES],
309     gint stride[GST_VIDEO_MAX_PLANES])
310 {
311   GstVideoMeta *meta;
312   guint i;
313
314   meta =
315       (GstVideoMeta *) gst_buffer_add_meta (buffer, GST_VIDEO_META_INFO, NULL);
316
317   if (!meta)
318     return NULL;
319
320   meta->flags = flags;
321   meta->format = format;
322   meta->id = 0;
323   meta->width = width;
324   meta->height = height;
325   meta->buffer = buffer;
326
327   meta->n_planes = n_planes;
328   for (i = 0; i < n_planes; i++) {
329     meta->offset[i] = offset[i];
330     meta->stride[i] = stride[i];
331     GST_LOG ("plane %d, offset %" G_GSIZE_FORMAT ", stride %d", i, offset[i],
332         stride[i]);
333   }
334   meta->map = default_map;
335   meta->unmap = default_unmap;
336
337   return meta;
338 }
339
340 /**
341  * gst_video_meta_map:
342  * @meta: a #GstVideoMeta
343  * @plane: a plane
344  * @info: a #GstMapInfo
345  * @data: (out): the data of @plane
346  * @stride: (out): the stride of @plane
347  * @flags: @GstMapFlags
348  *
349  * Map the video plane with index @plane in @meta and return a pointer to the
350  * first byte of the plane and the stride of the plane.
351  *
352  * Returns: TRUE if the map operation was successful.
353  */
354 gboolean
355 gst_video_meta_map (GstVideoMeta * meta, guint plane, GstMapInfo * info,
356     gpointer * data, gint * stride, GstMapFlags flags)
357 {
358   g_return_val_if_fail (meta != NULL, FALSE);
359   g_return_val_if_fail (meta->map != NULL, FALSE);
360   g_return_val_if_fail (plane < meta->n_planes, FALSE);
361   g_return_val_if_fail (info != NULL, FALSE);
362   g_return_val_if_fail (data != NULL, FALSE);
363   g_return_val_if_fail (stride != NULL, FALSE);
364   g_return_val_if_fail (meta->buffer != NULL, FALSE);
365   g_return_val_if_fail (!(flags & GST_MAP_WRITE)
366       || gst_buffer_is_writable (meta->buffer), FALSE);
367
368   return meta->map (meta, plane, info, data, stride, flags);
369 }
370
371 /**
372  * gst_video_meta_unmap:
373  * @meta: a #GstVideoMeta
374  * @plane: a plane
375  * @info: a #GstMapInfo
376  *
377  * Unmap a previously mapped plane with gst_video_meta_map().
378  *
379  * Returns: TRUE if the memory was successfully unmapped.
380  */
381 gboolean
382 gst_video_meta_unmap (GstVideoMeta * meta, guint plane, GstMapInfo * info)
383 {
384   g_return_val_if_fail (meta != NULL, FALSE);
385   g_return_val_if_fail (meta->unmap != NULL, FALSE);
386   g_return_val_if_fail (plane < meta->n_planes, FALSE);
387   g_return_val_if_fail (info != NULL, FALSE);
388
389   return meta->unmap (meta, plane, info);
390 }
391
392 static gboolean
393 gst_video_crop_meta_transform (GstBuffer * dest, GstMeta * meta,
394     GstBuffer * buffer, GQuark type, gpointer data)
395 {
396   GstVideoCropMeta *dmeta, *smeta;
397
398   if (GST_META_TRANSFORM_IS_COPY (type)) {
399     smeta = (GstVideoCropMeta *) meta;
400     dmeta = gst_buffer_add_video_crop_meta (dest);
401     if (!dmeta)
402       return FALSE;
403
404     GST_DEBUG ("copy crop metadata");
405     dmeta->x = smeta->x;
406     dmeta->y = smeta->y;
407     dmeta->width = smeta->width;
408     dmeta->height = smeta->height;
409   } else if (GST_VIDEO_META_TRANSFORM_IS_SCALE (type)) {
410     GstVideoMetaTransform *trans = data;
411     gint ow, oh, nw, nh;
412
413     smeta = (GstVideoCropMeta *) meta;
414     dmeta = gst_buffer_add_video_crop_meta (dest);
415     if (!dmeta)
416       return FALSE;
417
418     ow = GST_VIDEO_INFO_WIDTH (trans->in_info);
419     nw = GST_VIDEO_INFO_WIDTH (trans->out_info);
420     oh = GST_VIDEO_INFO_HEIGHT (trans->in_info);
421     nh = GST_VIDEO_INFO_HEIGHT (trans->out_info);
422
423     GST_DEBUG ("scaling crop metadata %dx%d -> %dx%d", ow, oh, nw, nh);
424     dmeta->x = (smeta->x * nw) / ow;
425     dmeta->y = (smeta->y * nh) / oh;
426     dmeta->width = (smeta->width * nw) / ow;
427     dmeta->height = (smeta->height * nh) / oh;
428     GST_DEBUG ("crop offset %dx%d -> %dx%d", smeta->x, smeta->y, dmeta->x,
429         dmeta->y);
430     GST_DEBUG ("crop size   %dx%d -> %dx%d", smeta->width, smeta->height,
431         dmeta->width, dmeta->height);
432   } else {
433     /* return FALSE, if transform type is not supported */
434     return FALSE;
435   }
436   return TRUE;
437 }
438
439 GType
440 gst_video_crop_meta_api_get_type (void)
441 {
442   static volatile GType type = 0;
443   static const gchar *tags[] =
444       { GST_META_TAG_VIDEO_STR, GST_META_TAG_VIDEO_SIZE_STR,
445     GST_META_TAG_VIDEO_ORIENTATION_STR, NULL
446   };
447
448   if (g_once_init_enter (&type)) {
449     GType _type = gst_meta_api_type_register ("GstVideoCropMetaAPI", tags);
450     g_once_init_leave (&type, _type);
451   }
452   return type;
453 }
454
455 static gboolean
456 gst_video_crop_meta_init (GstMeta * meta, gpointer params, GstBuffer * buffer)
457 {
458   GstVideoCropMeta *emeta = (GstVideoCropMeta *) meta;
459   emeta->x = emeta->y = emeta->width = emeta->height = 0;
460
461   return TRUE;
462 }
463
464 const GstMetaInfo *
465 gst_video_crop_meta_get_info (void)
466 {
467   static const GstMetaInfo *video_crop_meta_info = NULL;
468
469   if (g_once_init_enter ((GstMetaInfo **) & video_crop_meta_info)) {
470     const GstMetaInfo *meta =
471         gst_meta_register (GST_VIDEO_CROP_META_API_TYPE, "GstVideoCropMeta",
472         sizeof (GstVideoCropMeta),
473         (GstMetaInitFunction) gst_video_crop_meta_init,
474         (GstMetaFreeFunction) NULL, gst_video_crop_meta_transform);
475     g_once_init_leave ((GstMetaInfo **) & video_crop_meta_info,
476         (GstMetaInfo *) meta);
477   }
478   return video_crop_meta_info;
479 }
480
481 /**
482  * gst_video_meta_transform_scale_get_quark:
483  *
484  * Get the #GQuark for the "gst-video-scale" metadata transform operation.
485  *
486  * Returns: a #GQuark
487  */
488 GQuark
489 gst_video_meta_transform_scale_get_quark (void)
490 {
491   static GQuark _value = 0;
492
493   if (_value == 0) {
494     _value = g_quark_from_static_string ("gst-video-scale");
495   }
496   return _value;
497 }
498
499
500 GType
501 gst_video_gl_texture_upload_meta_api_get_type (void)
502 {
503   static volatile GType type = 0;
504   static const gchar *tags[] =
505       { GST_META_TAG_VIDEO_STR, GST_META_TAG_MEMORY_STR, NULL };
506
507   if (g_once_init_enter (&type)) {
508     GType _type =
509         gst_meta_api_type_register ("GstVideoGLTextureUploadMetaAPI", tags);
510     g_once_init_leave (&type, _type);
511   }
512   return type;
513 }
514
515 static gboolean
516 gst_video_gl_texture_upload_meta_init (GstMeta * meta, gpointer params,
517     GstBuffer * buffer)
518 {
519   GstVideoGLTextureUploadMeta *vmeta = (GstVideoGLTextureUploadMeta *) meta;
520
521   vmeta->texture_orientation =
522       GST_VIDEO_GL_TEXTURE_ORIENTATION_X_NORMAL_Y_NORMAL;
523   vmeta->n_textures = 0;
524   memset (vmeta->texture_type, 0, sizeof (vmeta->texture_type));
525   vmeta->buffer = NULL;
526   vmeta->upload = NULL;
527   vmeta->user_data = NULL;
528   vmeta->user_data_copy = NULL;
529   vmeta->user_data_free = NULL;
530
531   return TRUE;
532 }
533
534 static void
535 gst_video_gl_texture_upload_meta_free (GstMeta * meta, GstBuffer * buffer)
536 {
537   GstVideoGLTextureUploadMeta *vmeta = (GstVideoGLTextureUploadMeta *) meta;
538
539   if (vmeta->user_data_free)
540     vmeta->user_data_free (vmeta->user_data);
541 }
542
543 static gboolean
544 gst_video_gl_texture_upload_meta_transform (GstBuffer * dest, GstMeta * meta,
545     GstBuffer * buffer, GQuark type, gpointer data)
546 {
547   GstVideoGLTextureUploadMeta *dmeta, *smeta;
548
549   smeta = (GstVideoGLTextureUploadMeta *) meta;
550
551   if (GST_META_TRANSFORM_IS_COPY (type)) {
552     GstMetaTransformCopy *copy = data;
553
554     if (!copy->region) {
555       /* only copy if the complete data is copied as well */
556       dmeta =
557           (GstVideoGLTextureUploadMeta *) gst_buffer_add_meta (dest,
558           GST_VIDEO_GL_TEXTURE_UPLOAD_META_INFO, NULL);
559
560       if (!dmeta)
561         return FALSE;
562
563       dmeta->texture_orientation = smeta->texture_orientation;
564       dmeta->n_textures = smeta->n_textures;
565       memcpy (dmeta->texture_type, smeta->texture_type,
566           sizeof (smeta->texture_type[0]) * 4);
567       dmeta->buffer = dest;
568       dmeta->upload = smeta->upload;
569       dmeta->user_data = smeta->user_data;
570       dmeta->user_data_copy = smeta->user_data_copy;
571       dmeta->user_data_free = smeta->user_data_free;
572       if (dmeta->user_data_copy)
573         dmeta->user_data = dmeta->user_data_copy (dmeta->user_data);
574     }
575   } else {
576     /* return FALSE, if transform type is not supported */
577     return FALSE;
578   }
579   return TRUE;
580 }
581
582 const GstMetaInfo *
583 gst_video_gl_texture_upload_meta_get_info (void)
584 {
585   static const GstMetaInfo *info = NULL;
586
587   if (g_once_init_enter ((GstMetaInfo **) & info)) {
588     const GstMetaInfo *meta =
589         gst_meta_register (GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE,
590         "GstVideoGLTextureUploadMeta",
591         sizeof (GstVideoGLTextureUploadMeta),
592         gst_video_gl_texture_upload_meta_init,
593         gst_video_gl_texture_upload_meta_free,
594         gst_video_gl_texture_upload_meta_transform);
595     g_once_init_leave ((GstMetaInfo **) & info, (GstMetaInfo *) meta);
596   }
597   return info;
598 }
599
600 /**
601  * gst_buffer_add_video_gl_texture_upload_meta:
602  * @buffer: a #GstBuffer
603  * @texture_orientation: the #GstVideoGLTextureOrientation
604  * @n_textures: the number of textures
605  * @texture_type: array of #GstVideoGLTextureType
606  * @upload: (scope call): the function to upload the buffer to a specific texture ID
607  * @user_data: user data for the implementor of @upload
608  * @user_data_copy: (scope call): function to copy @user_data
609  * @user_data_free: (scope call): function to free @user_data
610  *
611  * Attaches GstVideoGLTextureUploadMeta metadata to @buffer with the given
612  * parameters.
613  *
614  * Returns: (transfer none): the #GstVideoGLTextureUploadMeta on @buffer.
615  */
616 GstVideoGLTextureUploadMeta *
617 gst_buffer_add_video_gl_texture_upload_meta (GstBuffer * buffer,
618     GstVideoGLTextureOrientation texture_orientation, guint n_textures,
619     GstVideoGLTextureType texture_type[4], GstVideoGLTextureUpload upload,
620     gpointer user_data, GBoxedCopyFunc user_data_copy,
621     GBoxedFreeFunc user_data_free)
622 {
623   GstVideoGLTextureUploadMeta *meta;
624
625   g_return_val_if_fail (buffer != NULL, NULL);
626   g_return_val_if_fail (upload != NULL, NULL);
627   g_return_val_if_fail (n_textures > 0 && n_textures < 5, NULL);
628
629   meta =
630       (GstVideoGLTextureUploadMeta *) gst_buffer_add_meta (buffer,
631       GST_VIDEO_GL_TEXTURE_UPLOAD_META_INFO, NULL);
632
633   if (!meta)
634     return NULL;
635
636   meta->texture_orientation = texture_orientation;
637   meta->n_textures = n_textures;
638   memcpy (meta->texture_type, texture_type, sizeof (texture_type[0]) * 4);
639   meta->buffer = buffer;
640   meta->upload = upload;
641   meta->user_data = user_data;
642   meta->user_data_copy = user_data_copy;
643   meta->user_data_free = user_data_free;
644
645   return meta;
646 }
647
648 /**
649  * gst_video_gl_texture_upload_meta_upload:
650  * @meta: a #GstVideoGLTextureUploadMeta
651  * @texture_id: the texture IDs to upload to
652  *
653  * Uploads the buffer which owns the meta to a specific texture ID.
654  *
655  * Returns: %TRUE if uploading succeeded, %FALSE otherwise.
656  */
657 gboolean
658 gst_video_gl_texture_upload_meta_upload (GstVideoGLTextureUploadMeta * meta,
659     guint texture_id[4])
660 {
661   g_return_val_if_fail (meta != NULL, FALSE);
662
663   return meta->upload (meta, texture_id);
664 }
665
666 /* Region of Interest Meta implementation *******************************************/
667
668 GType
669 gst_video_region_of_interest_meta_api_get_type (void)
670 {
671   static volatile GType type;
672   static const gchar *tags[] =
673       { GST_META_TAG_VIDEO_STR, GST_META_TAG_VIDEO_ORIENTATION_STR,
674     GST_META_TAG_VIDEO_SIZE_STR, NULL
675   };
676
677   if (g_once_init_enter (&type)) {
678     GType _type =
679         gst_meta_api_type_register ("GstVideoRegionOfInterestMetaAPI", tags);
680     GST_INFO ("registering");
681     g_once_init_leave (&type, _type);
682   }
683   return type;
684 }
685
686
687 static gboolean
688 gst_video_region_of_interest_meta_transform (GstBuffer * dest, GstMeta * meta,
689     GstBuffer * buffer, GQuark type, gpointer data)
690 {
691   GstVideoRegionOfInterestMeta *dmeta, *smeta;
692
693   if (GST_META_TRANSFORM_IS_COPY (type)) {
694     smeta = (GstVideoRegionOfInterestMeta *) meta;
695
696     GST_DEBUG ("copy region of interest metadata");
697     dmeta =
698         gst_buffer_add_video_region_of_interest_meta_id (dest,
699         smeta->roi_type, smeta->x, smeta->y, smeta->w, smeta->h);
700     if (!dmeta)
701       return FALSE;
702
703     dmeta->id = smeta->id;
704     dmeta->parent_id = smeta->parent_id;
705     dmeta->params = g_list_copy_deep (smeta->params,
706         (GCopyFunc) gst_structure_copy, NULL);
707   } else if (GST_VIDEO_META_TRANSFORM_IS_SCALE (type)) {
708     GstVideoMetaTransform *trans = data;
709     gint ow, oh, nw, nh;
710     ow = GST_VIDEO_INFO_WIDTH (trans->in_info);
711     nw = GST_VIDEO_INFO_WIDTH (trans->out_info);
712     oh = GST_VIDEO_INFO_HEIGHT (trans->in_info);
713     nh = GST_VIDEO_INFO_HEIGHT (trans->out_info);
714     GST_DEBUG ("scaling region of interest metadata %dx%d -> %dx%d", ow, oh, nw,
715         nh);
716
717     smeta = (GstVideoRegionOfInterestMeta *) meta;
718     dmeta =
719         gst_buffer_add_video_region_of_interest_meta_id (dest,
720         smeta->roi_type, (smeta->x * nw) / ow, (smeta->y * nh) / oh,
721         (smeta->w * nw) / ow, (smeta->h * nh) / oh);
722     if (!dmeta)
723       return FALSE;
724
725     dmeta->id = smeta->id;
726     dmeta->parent_id = smeta->parent_id;
727
728     GST_DEBUG ("region of interest (id:%d, parent id:%d) offset %dx%d -> %dx%d",
729         smeta->id, smeta->parent_id, smeta->x, smeta->y, dmeta->x, dmeta->y);
730     GST_DEBUG ("region of interest size   %dx%d -> %dx%d", smeta->w, smeta->h,
731         dmeta->w, dmeta->h);
732   } else {
733     /* return FALSE, if transform type is not supported */
734     return FALSE;
735   }
736   return TRUE;
737 }
738
739 static gboolean
740 gst_video_region_of_interest_meta_init (GstMeta * meta, gpointer params,
741     GstBuffer * buffer)
742 {
743   GstVideoRegionOfInterestMeta *emeta = (GstVideoRegionOfInterestMeta *) meta;
744   emeta->roi_type = 0;
745   emeta->id = 0;
746   emeta->parent_id = 0;
747   emeta->x = emeta->y = emeta->w = emeta->h = 0;
748   emeta->params = NULL;
749
750   return TRUE;
751 }
752
753 static void
754 gst_video_region_of_interest_meta_free (GstMeta * meta, GstBuffer * buffer)
755 {
756   GstVideoRegionOfInterestMeta *emeta = (GstVideoRegionOfInterestMeta *) meta;
757
758   g_list_free_full (emeta->params, (GDestroyNotify) gst_structure_free);
759 }
760
761 const GstMetaInfo *
762 gst_video_region_of_interest_meta_get_info (void)
763 {
764   static const GstMetaInfo *meta_info = NULL;
765
766   if (g_once_init_enter ((GstMetaInfo **) & meta_info)) {
767     const GstMetaInfo *mi =
768         gst_meta_register (GST_VIDEO_REGION_OF_INTEREST_META_API_TYPE,
769         "GstVideoRegionOfInterestMeta",
770         sizeof (GstVideoRegionOfInterestMeta),
771         gst_video_region_of_interest_meta_init,
772         gst_video_region_of_interest_meta_free,
773         gst_video_region_of_interest_meta_transform);
774     g_once_init_leave ((GstMetaInfo **) & meta_info, (GstMetaInfo *) mi);
775   }
776   return meta_info;
777 }
778
779 /**
780  * gst_buffer_get_video_region_of_interest_meta_id:
781  * @buffer: a #GstBuffer
782  * @id: a metadata id
783  *
784  * Find the #GstVideoRegionOfInterestMeta on @buffer with the given @id.
785  *
786  * Buffers can contain multiple #GstVideoRegionOfInterestMeta metadata items if
787  * multiple regions of interests are marked on a frame.
788  *
789  * Returns: (transfer none): the #GstVideoRegionOfInterestMeta with @id or %NULL when there is
790  * no such metadata on @buffer.
791  */
792 GstVideoRegionOfInterestMeta *
793 gst_buffer_get_video_region_of_interest_meta_id (GstBuffer * buffer, gint id)
794 {
795   gpointer state = NULL;
796   GstMeta *meta;
797   const GstMetaInfo *info = GST_VIDEO_REGION_OF_INTEREST_META_INFO;
798
799   while ((meta = gst_buffer_iterate_meta (buffer, &state))) {
800     if (meta->info->api == info->api) {
801       GstVideoRegionOfInterestMeta *vmeta =
802           (GstVideoRegionOfInterestMeta *) meta;
803       if (vmeta->id == id)
804         return vmeta;
805     }
806   }
807   return NULL;
808 }
809
810 /**
811  * gst_buffer_add_video_region_of_interest_meta:
812  * @buffer: a #GstBuffer
813  * @roi_type: Type of the region of interest (e.g. "face")
814  * @x: X position
815  * @y: Y position
816  * @w: width
817  * @h: height
818  *
819  * Attaches #GstVideoRegionOfInterestMeta metadata to @buffer with the given
820  * parameters.
821  *
822  * Returns: (transfer none): the #GstVideoRegionOfInterestMeta on @buffer.
823  */
824 GstVideoRegionOfInterestMeta *
825 gst_buffer_add_video_region_of_interest_meta (GstBuffer * buffer,
826     const gchar * roi_type, guint x, guint y, guint w, guint h)
827 {
828   return gst_buffer_add_video_region_of_interest_meta_id (buffer,
829       g_quark_from_string (roi_type), x, y, w, h);
830 }
831
832 /**
833  * gst_buffer_add_video_region_of_interest_meta_id:
834  * @buffer: a #GstBuffer
835  * @roi_type: Type of the region of interest (e.g. "face")
836  * @x: X position
837  * @y: Y position
838  * @w: width
839  * @h: height
840  *
841  * Attaches #GstVideoRegionOfInterestMeta metadata to @buffer with the given
842  * parameters.
843  *
844  * Returns: (transfer none): the #GstVideoRegionOfInterestMeta on @buffer.
845  */
846 GstVideoRegionOfInterestMeta *
847 gst_buffer_add_video_region_of_interest_meta_id (GstBuffer * buffer,
848     GQuark roi_type, guint x, guint y, guint w, guint h)
849 {
850   GstVideoRegionOfInterestMeta *meta;
851
852   g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
853
854   meta = (GstVideoRegionOfInterestMeta *) gst_buffer_add_meta (buffer,
855       GST_VIDEO_REGION_OF_INTEREST_META_INFO, NULL);
856   meta->roi_type = roi_type;
857   meta->x = x;
858   meta->y = y;
859   meta->w = w;
860   meta->h = h;
861
862   return meta;
863 }
864
865 /**
866  * gst_video_region_of_interest_meta_add_param:
867  * @meta: a #GstVideoRegionOfInterestMeta
868  * @s: (transfer full): a #GstStructure
869  *
870  * Attach element-specific parameters to @meta meant to be used by downstream
871  * elements which may handle this ROI.
872  * The name of @s is used to identify the element these parameters are meant for.
873  *
874  * This is typically used to tell encoders how they should encode this specific region.
875  * For example, a structure named "roi/x264enc" could be used to give the
876  * QP offsets this encoder should use when encoding the region described in @meta.
877  * Multiple parameters can be defined for the same meta so different encoders
878  * can be supported by cross platform applications).
879  *
880  * Since: 1.14
881  */
882 void
883 gst_video_region_of_interest_meta_add_param (GstVideoRegionOfInterestMeta *
884     meta, GstStructure * s)
885 {
886   g_return_if_fail (meta);
887   g_return_if_fail (s);
888
889   meta->params = g_list_append (meta->params, s);
890 }
891
892 /**
893  * gst_video_region_of_interest_meta_get_param:
894  * @meta: a #GstVideoRegionOfInterestMeta
895  * @name: a name.
896  *
897  * Retrieve the parameter for @meta having @name as structure name,
898  * or %NULL if there is none.
899  *
900  * Returns: (transfer none) (nullable): a #GstStructure
901  *
902  * Since: 1.14
903  * See also: gst_video_region_of_interest_meta_add_param()
904  */
905 GstStructure *
906 gst_video_region_of_interest_meta_get_param (GstVideoRegionOfInterestMeta *
907     meta, const gchar * name)
908 {
909   GList *l;
910
911   g_return_val_if_fail (meta, NULL);
912   g_return_val_if_fail (name, NULL);
913
914   for (l = meta->params; l; l = g_list_next (l)) {
915     GstStructure *s = l->data;
916
917     if (gst_structure_has_name (s, name))
918       return s;
919   }
920
921   return NULL;
922 }
923
924 /* Time Code Meta implementation *******************************************/
925
926 GType
927 gst_video_time_code_meta_api_get_type (void)
928 {
929   static volatile GType type;
930
931   if (g_once_init_enter (&type)) {
932     static const gchar *tags[] = { NULL };
933     GType _type = gst_meta_api_type_register ("GstVideoTimeCodeMetaAPI", tags);
934     GST_INFO ("registering");
935     g_once_init_leave (&type, _type);
936   }
937   return type;
938 }
939
940
941 static gboolean
942 gst_video_time_code_meta_transform (GstBuffer * dest, GstMeta * meta,
943     GstBuffer * buffer, GQuark type, gpointer data)
944 {
945   GstVideoTimeCodeMeta *dmeta, *smeta;
946
947   if (GST_META_TRANSFORM_IS_COPY (type)) {
948     smeta = (GstVideoTimeCodeMeta *) meta;
949
950     GST_DEBUG ("copy time code metadata");
951     dmeta =
952         gst_buffer_add_video_time_code_meta_full (dest, smeta->tc.config.fps_n,
953         smeta->tc.config.fps_d, smeta->tc.config.latest_daily_jam,
954         smeta->tc.config.flags, smeta->tc.hours, smeta->tc.minutes,
955         smeta->tc.seconds, smeta->tc.frames, smeta->tc.field_count);
956     if (!dmeta)
957       return FALSE;
958   } else {
959     /* return FALSE, if transform type is not supported */
960     return FALSE;
961   }
962   return TRUE;
963 }
964
965 static gboolean
966 gst_video_time_code_meta_init (GstMeta * meta, gpointer params,
967     GstBuffer * buffer)
968 {
969   GstVideoTimeCodeMeta *emeta = (GstVideoTimeCodeMeta *) meta;
970   memset (&emeta->tc, 0, sizeof (emeta->tc));
971   gst_video_time_code_clear (&emeta->tc);
972
973   return TRUE;
974 }
975
976 static void
977 gst_video_time_code_meta_free (GstMeta * meta, GstBuffer * buffer)
978 {
979   GstVideoTimeCodeMeta *emeta = (GstVideoTimeCodeMeta *) meta;
980
981   gst_video_time_code_clear (&emeta->tc);
982 }
983
984 const GstMetaInfo *
985 gst_video_time_code_meta_get_info (void)
986 {
987   static const GstMetaInfo *meta_info = NULL;
988
989   if (g_once_init_enter ((GstMetaInfo **) & meta_info)) {
990     const GstMetaInfo *mi =
991         gst_meta_register (GST_VIDEO_TIME_CODE_META_API_TYPE,
992         "GstVideoTimeCodeMeta",
993         sizeof (GstVideoTimeCodeMeta),
994         gst_video_time_code_meta_init,
995         gst_video_time_code_meta_free,
996         gst_video_time_code_meta_transform);
997     g_once_init_leave ((GstMetaInfo **) & meta_info, (GstMetaInfo *) mi);
998   }
999   return meta_info;
1000 }
1001
1002 /**
1003  * gst_buffer_add_video_time_code_meta:
1004  * @buffer: a #GstBuffer
1005  * @tc: a #GstVideoTimeCode
1006  *
1007  * Attaches #GstVideoTimeCodeMeta metadata to @buffer with the given
1008  * parameters.
1009  *
1010  * Returns: (transfer none): the #GstVideoTimeCodeMeta on @buffer.
1011  *
1012  * Since: 1.10
1013  */
1014 GstVideoTimeCodeMeta *
1015 gst_buffer_add_video_time_code_meta (GstBuffer * buffer, GstVideoTimeCode * tc)
1016 {
1017   g_return_val_if_fail (gst_video_time_code_is_valid (tc), NULL);
1018   return gst_buffer_add_video_time_code_meta_full (buffer, tc->config.fps_n,
1019       tc->config.fps_d, tc->config.latest_daily_jam, tc->config.flags,
1020       tc->hours, tc->minutes, tc->seconds, tc->frames, tc->field_count);
1021 }
1022
1023 /**
1024  * gst_buffer_add_video_time_code_meta_full:
1025  * @buffer: a #GstBuffer
1026  * @fps_n: framerate numerator
1027  * @fps_d: framerate denominator
1028  * @latest_daily_jam: a #GDateTime for the latest daily jam
1029  * @flags: a #GstVideoTimeCodeFlags
1030  * @hours: hours since the daily jam
1031  * @minutes: minutes since the daily jam
1032  * @seconds: seconds since the daily jam
1033  * @frames: frames since the daily jam
1034  * @field_count: fields since the daily jam
1035  *
1036  * Attaches #GstVideoTimeCodeMeta metadata to @buffer with the given
1037  * parameters.
1038  *
1039  * Returns: (transfer none): the #GstVideoTimeCodeMeta on @buffer.
1040  *
1041  * Since: 1.10
1042  */
1043 GstVideoTimeCodeMeta *
1044 gst_buffer_add_video_time_code_meta_full (GstBuffer * buffer, guint fps_n,
1045     guint fps_d, GDateTime * latest_daily_jam, GstVideoTimeCodeFlags flags,
1046     guint hours, guint minutes, guint seconds, guint frames, guint field_count)
1047 {
1048   GstVideoTimeCodeMeta *meta;
1049
1050   g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
1051
1052   meta = (GstVideoTimeCodeMeta *) gst_buffer_add_meta (buffer,
1053       GST_VIDEO_TIME_CODE_META_INFO, NULL);
1054   g_return_val_if_fail (meta != NULL, NULL);
1055
1056   gst_video_time_code_init (&meta->tc, fps_n, fps_d, latest_daily_jam, flags,
1057       hours, minutes, seconds, frames, field_count);
1058
1059   g_return_val_if_fail (gst_video_time_code_is_valid (&meta->tc), NULL);
1060
1061   return meta;
1062 }