summaryrefslogtreecommitdiff
path: root/gst-libs/gst/video/gstvideometa.c
blob: 6ce4d0d1e34d51996b011e4c747eb61bbaf46b34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* GStreamer
 * Copyright (C) <2011> Wim Taymans <wim.taymans@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "gstvideometa.h"

static void
gst_video_meta_copy (GstBuffer * dest, GstMeta * meta,
    GstBuffer * buffer, gsize offset, gsize size)
{
  GstVideoMeta *dmeta, *smeta;
  guint i;

  smeta = (GstVideoMeta *) meta;

  dmeta =
      (GstVideoMeta *) gst_buffer_add_meta (dest, GST_VIDEO_META_INFO, NULL);
  dmeta->buffer = dest;

  dmeta->flags = smeta->flags;
  dmeta->id = smeta->id;
  dmeta->format = smeta->format;
  dmeta->width = smeta->width;
  dmeta->height = smeta->height;

  dmeta->n_planes = smeta->n_planes;
  for (i = 0; i < dmeta->n_planes; i++) {
    dmeta->offset[i] = smeta->offset[i];
    dmeta->stride[i] = smeta->stride[i];
  }
}

/* video metadata */
const GstMetaInfo *
gst_video_meta_get_info (void)
{
  static const GstMetaInfo *video_meta_info = NULL;

  if (video_meta_info == NULL) {
    video_meta_info = gst_meta_register (GST_VIDEO_META_API, "GstVideoMeta",
        sizeof (GstVideoMeta),
        (GstMetaInitFunction) NULL,
        (GstMetaFreeFunction) NULL,
        gst_video_meta_copy, (GstMetaTransformFunction) NULL);
  }
  return video_meta_info;
}

/**
 * gst_buffer_get_video_meta_id:
 * @buffer: a #GstBuffer
 * @id: a metadata id
 *
 * Find the #GstVideoMeta on @buffer with the given @id.
 *
 * Buffers can contain multiple #GstVideoMeta metadata items when dealing with
 * multiview buffers.
 *
 * Returns: the #GstVideoMeta with @id or %NULL when there is no such metadata
 * on @buffer.
 */
GstVideoMeta *
gst_buffer_get_video_meta_id (GstBuffer * buffer, gint id)
{
  gpointer state = NULL;
  GstMeta *meta;
  const GstMetaInfo *info = GST_VIDEO_META_INFO;

  while ((meta = gst_buffer_iterate_meta (buffer, &state))) {
    if (meta->info->api == info->api) {
      GstVideoMeta *vmeta = (GstVideoMeta *) meta;
      if (vmeta->id == id)
        return vmeta;
    }
  }
  return NULL;
}

/**
 * gst_buffer_add_video_meta:
 * @buffer: a #GstBuffer
 * @flags: #GstVideoFlags
 * @format: a #GstVideoFormat
 * @width: the width
 * @height: the height
 *
 * Attaches GstVideoMeta metadata to @buffer with the given parameters and the
 * default offsets and strides for @format and @width x @height.
 *
 * This function calculates the default offsets and strides and then calls
 * gst_buffer_add_video_meta_full() with them.
 *
 * Returns: the #GstVideoMeta on @buffer.
 */
GstVideoMeta *
gst_buffer_add_video_meta (GstBuffer * buffer, GstVideoFlags flags,
    GstVideoFormat format, guint width, guint height)
{
  GstVideoMeta *meta;
  GstVideoInfo info;

  gst_video_info_set_format (&info, format, width, height);

  meta = gst_buffer_add_video_meta_full (buffer, flags, format, width, height,
      info.finfo->n_planes, info.offset, info.stride);

  return meta;
}

/**
 * gst_buffer_add_video_meta_full:
 * @buffer: a #GstBuffer
 * @flags: #GstVideoFlags
 * @format: a #GstVideoFormat
 * @width: the width
 * @height: the height
 * @n_planes: number of planes
 * @offset: offset of each plane
 * @stride: stride of each plane
 *
 * Attaches GstVideoMeta metadata to @buffer with the given parameters.
 *
 * Returns: the #GstVideoMeta on @buffer.
 */
GstVideoMeta *
gst_buffer_add_video_meta_full (GstBuffer * buffer, GstVideoFlags flags,
    GstVideoFormat format, guint width, guint height,
    guint n_planes, gsize offset[GST_VIDEO_MAX_PLANES],
    gint stride[GST_VIDEO_MAX_PLANES])
{
  GstVideoMeta *meta;
  guint i;

  meta =
      (GstVideoMeta *) gst_buffer_add_meta (buffer, GST_VIDEO_META_INFO, NULL);

  meta->flags = flags;
  meta->format = format;
  meta->id = 0;
  meta->width = width;
  meta->height = height;
  meta->buffer = buffer;

  meta->n_planes = n_planes;
  for (i = 0; i < n_planes; i++) {
    meta->offset[i] = offset[i];
    meta->stride[i] = stride[i];
  }
  return meta;
}

static GstMemory *
find_mem_for_offset (GstBuffer * buffer, guint * offset, GstMapFlags flags)
{
  guint n, i;
  GstMemory *res = NULL;

  n = gst_buffer_n_memory (buffer);
  for (i = 0; i < n; i++) {
    GstMemory *mem = NULL;
    gsize size;

    mem = gst_buffer_peek_memory (buffer, i, flags);
    size = gst_memory_get_sizes (mem, NULL, NULL);

    if (*offset < size) {
      res = mem;
      break;
    }
    *offset -= size;
  }
  return res;
}

/**
 * gst_video_meta_map:
 * @meta: a #GstVideoMeta
 * @plane: a plane
 * @stride: result stride
 * @flags: @GstMapFlags
 *
 * Map the video plane with index @plane in @meta and return a pointer to the
 * first byte of the plane and the stride of the plane.
 *
 * Returns: a pointer to the first byte of the plane data
 */
gpointer
gst_video_meta_map (GstVideoMeta * meta, guint plane, gint * stride,
    GstMapFlags flags)
{
  guint offset;
  gboolean write;
  GstBuffer *buffer;
  GstMemory *mem;
  guint8 *base;

  g_return_val_if_fail (meta != NULL, NULL);
  g_return_val_if_fail (plane < meta->n_planes, NULL);
  g_return_val_if_fail (stride != NULL, NULL);

  buffer = meta->buffer;
  g_return_val_if_fail (buffer != NULL, NULL);

  write = (flags & GST_MAP_WRITE) != 0;
  g_return_val_if_fail (!write || gst_buffer_is_writable (buffer), NULL);

  offset = meta->offset[plane];
  *stride = meta->stride[plane];
  /* find the memory block for this plane, this is the memory block containing
   * the plane offset */
  mem = find_mem_for_offset (buffer, &offset, flags);

  base = gst_memory_map (mem, NULL, NULL, flags);

  /* move to the right offset inside the block */
  return base + offset;
}

/**
 * gst_video_meta_unmap:
 * @meta: a #GstVideoMeta
 * @plane: a plane
 * @data: the data to unmap
 *
 * Unmap previously mapped data with gst_video_meta_map().
 *
 * Returns: TRUE if the memory was successfully unmapped.
 */
gboolean
gst_video_meta_unmap (GstVideoMeta * meta, guint plane, gpointer data)
{
  guint offset;
  GstBuffer *buffer;
  GstMemory *mem;
  guint8 *base;

  g_return_val_if_fail (meta != NULL, FALSE);
  g_return_val_if_fail (plane < meta->n_planes, FALSE);

  buffer = meta->buffer;
  g_return_val_if_fail (buffer != NULL, FALSE);

  offset = meta->offset[plane];
  mem = find_mem_for_offset (buffer, &offset, GST_MAP_READ);
  base = data;

  gst_memory_unmap (mem, base - offset, -1);

  return TRUE;
}

static void
gst_video_crop_meta_copy (GstBuffer * dest, GstMeta * meta,
    GstBuffer * buffer, gsize offset, gsize size)
{
  GstVideoCropMeta *dmeta, *smeta;

  smeta = (GstVideoCropMeta *) meta;
  dmeta = gst_buffer_add_video_crop_meta (dest);

  dmeta->x = smeta->x;
  dmeta->y = smeta->y;
  dmeta->width = smeta->width;
  dmeta->height = smeta->height;
}

const GstMetaInfo *
gst_video_crop_meta_get_info (void)
{
  static const GstMetaInfo *video_crop_meta_info = NULL;

  if (video_crop_meta_info == NULL) {
    video_crop_meta_info =
        gst_meta_register (GST_VIDEO_CROP_META_API, "GstVideoCropMeta",
        sizeof (GstVideoCropMeta), (GstMetaInitFunction) NULL,
        (GstMetaFreeFunction) NULL, gst_video_crop_meta_copy,
        (GstMetaTransformFunction) NULL);
  }
  return video_crop_meta_info;
}