48be215d028d3e58dddfd7f1390fe8c980948ac4
[platform/upstream/gstreamer.git] / gst-libs / gst / video / video.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Library       <2002> Ronald Bultje <rbultje@ronald.bitfreak.net>
4  * Copyright (C) 2007 David A. Schleef <ds@schleef.org>
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 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25
26 #include <string.h>
27
28 #include "video.h"
29 #include "gstvideometa.h"
30
31 static int fill_planes (GstVideoInfo * info);
32
33 typedef struct
34 {
35   guint32 fourcc;
36   GstVideoFormatInfo info;
37 } VideoFormat;
38
39 /* depths: bits, n_components, shift, depth */
40 #define DPTH0            0, 0, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
41 #define DPTH8            8, 1, { 0, 0, 0, 0 }, { 8, 0, 0, 0 }
42 #define DPTH888          8, 3, { 0, 0, 0, 0 }, { 8, 8, 8, 0 }
43 #define DPTH8888         8, 4, { 0, 0, 0, 0 }, { 8, 8, 8, 8 }
44 #define DPTH10_10_10     10, 3, { 0, 0, 0, 0 }, { 10, 10, 10, 0 }
45 #define DPTH16           16, 1, { 0, 0, 0, 0 }, { 16, 0, 0, 0 }
46 #define DPTH16_16_16     16, 3, { 0, 0, 0, 0 }, { 16, 16, 16, 0 }
47 #define DPTH16_16_16_16  16, 4, { 0, 0, 0, 0 }, { 16, 16, 16, 16 }
48 #define DPTH555          16, 3, { 10, 5, 0, 0 }, { 5, 5, 5, 0 }
49 #define DPTH565          16, 3, { 11, 5, 0, 0 }, { 5, 6, 5, 0 }
50
51 /* pixel strides */
52 #define PSTR0             { 0, 0, 0, 0 }
53 #define PSTR1             { 1, 0, 0, 0 }
54 #define PSTR111           { 1, 1, 1, 0 }
55 #define PSTR1111          { 1, 1, 1, 1 }
56 #define PSTR122           { 1, 2, 2, 0 }
57 #define PSTR2             { 2, 0, 0, 0 }
58 #define PSTR222           { 2, 2, 2, 0 }
59 #define PSTR244           { 2, 4, 4, 0 }
60 #define PSTR444           { 4, 4, 4, 0 }
61 #define PSTR4444          { 4, 4, 4, 4 }
62 #define PSTR333           { 3, 3, 3, 0 }
63 #define PSTR488           { 4, 8, 8, 0 }
64 #define PSTR8888          { 8, 8, 8, 8 }
65
66 /* planes */
67 #define PLANE_NA          0, { 0, 0, 0, 0 }
68 #define PLANE0            1, { 0, 0, 0, 0 }
69 #define PLANE011          2, { 0, 1, 1, 0 }
70 #define PLANE012          3, { 0, 1, 2, 0 }
71 #define PLANE0123         4, { 0, 1, 2, 3 }
72 #define PLANE021          3, { 0, 2, 1, 0 }
73
74 /* offsets */
75 #define OFFS0             { 0, 0, 0, 0 }
76 #define OFFS013           { 0, 1, 3, 0 }
77 #define OFFS102           { 1, 0, 2, 0 }
78 #define OFFS1230          { 1, 2, 3, 0 }
79 #define OFFS012           { 0, 1, 2, 0 }
80 #define OFFS210           { 2, 1, 0, 0 }
81 #define OFFS123           { 1, 2, 3, 0 }
82 #define OFFS321           { 3, 2, 1, 0 }
83 #define OFFS0123          { 0, 1, 2, 3 }
84 #define OFFS2103          { 2, 1, 0, 3 }
85 #define OFFS3210          { 3, 2, 1, 0 }
86 #define OFFS031           { 0, 3, 1, 0 }
87 #define OFFS026           { 0, 2, 6, 0 }
88 #define OFFS001           { 0, 0, 1, 0 }
89 #define OFFS010           { 0, 1, 0, 0 }
90 #define OFFS104           { 1, 0, 4, 0 }
91 #define OFFS2460          { 2, 4, 6, 0 }
92
93 /* subsampling */
94 #define SUB410            { 0, 2, 2, 0 }, { 0, 2, 2, 0 }
95 #define SUB411            { 0, 2, 2, 0 }, { 0, 0, 0, 0 }
96 #define SUB420            { 0, 1, 1, 0 }, { 0, 1, 1, 0 }
97 #define SUB422            { 0, 1, 1, 0 }, { 0, 0, 0, 0 }
98 #define SUB4              { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
99 #define SUB444            { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
100 #define SUB4444           { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
101 #define SUB4204           { 0, 1, 1, 0 }, { 0, 1, 1, 0 }
102
103 #define MAKE_YUV_FORMAT(name, desc, fourcc, depth, pstride, plane, offs, sub ) \
104  { fourcc, {GST_VIDEO_FORMAT_ ##name, G_STRINGIFY(name), desc, GST_VIDEO_FORMAT_FLAG_YUV, depth, pstride, plane, offs, sub } }
105 #define MAKE_YUVA_FORMAT(name, desc, fourcc, depth, pstride, plane, offs, sub) \
106  { fourcc, {GST_VIDEO_FORMAT_ ##name, G_STRINGIFY(name), desc, GST_VIDEO_FORMAT_FLAG_YUV | GST_VIDEO_FORMAT_FLAG_ALPHA, depth, pstride, plane, offs, sub } }
107
108 #define MAKE_RGB_FORMAT(name, desc, depth, pstride, plane, offs, sub) \
109  { 0x00000000, {GST_VIDEO_FORMAT_ ##name, G_STRINGIFY(name), desc, GST_VIDEO_FORMAT_FLAG_RGB, depth, pstride, plane, offs, sub } }
110 #define MAKE_RGBA_FORMAT(name, desc, depth, pstride, plane, offs, sub) \
111  { 0x00000000, {GST_VIDEO_FORMAT_ ##name, G_STRINGIFY(name), desc, GST_VIDEO_FORMAT_FLAG_RGB | GST_VIDEO_FORMAT_FLAG_ALPHA, depth, pstride, plane, offs, sub } }
112
113 #define MAKE_GRAY_FORMAT(name, desc, depth, pstride, plane, offs, sub) \
114  { 0x00000000, {GST_VIDEO_FORMAT_ ##name, G_STRINGIFY(name), desc, GST_VIDEO_FORMAT_FLAG_GRAY, depth, pstride, plane, offs, sub } }
115
116 static VideoFormat formats[] = {
117   {0x00000000, {GST_VIDEO_FORMAT_UNKNOWN, "UNKNOWN", "unknown video", 0, DPTH0,
118               PSTR0, PLANE_NA,
119           OFFS0}},
120
121   MAKE_YUV_FORMAT (I420, "raw video", GST_MAKE_FOURCC ('I', '4', '2', '0'),
122       DPTH888, PSTR111,
123       PLANE012, OFFS0, SUB420),
124   MAKE_YUV_FORMAT (YV12, "raw video", GST_MAKE_FOURCC ('Y', 'V', '1', '2'),
125       DPTH888, PSTR111,
126       PLANE021, OFFS0, SUB420),
127   MAKE_YUV_FORMAT (YUY2, "raw video", GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'),
128       DPTH888, PSTR244,
129       PLANE0, OFFS013, SUB422),
130   MAKE_YUV_FORMAT (UYVY, "raw video", GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'),
131       DPTH888, PSTR244,
132       PLANE0, OFFS102, SUB422),
133   MAKE_YUVA_FORMAT (AYUV, "raw video", GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'),
134       DPTH8888,
135       PSTR4444, PLANE0, OFFS1230, SUB4444),
136   MAKE_RGB_FORMAT (RGBx, "raw video", DPTH888, PSTR444, PLANE0, OFFS012,
137       SUB444),
138   MAKE_RGB_FORMAT (BGRx, "raw video", DPTH888, PSTR444, PLANE0, OFFS210,
139       SUB444),
140   MAKE_RGB_FORMAT (xRGB, "raw video", DPTH888, PSTR444, PLANE0, OFFS123,
141       SUB444),
142   MAKE_RGB_FORMAT (xBGR, "raw video", DPTH888, PSTR444, PLANE0, OFFS321,
143       SUB444),
144   MAKE_RGBA_FORMAT (RGBA, "raw video", DPTH8888, PSTR4444, PLANE0, OFFS0123,
145       SUB4444),
146   MAKE_RGBA_FORMAT (BGRA, "raw video", DPTH8888, PSTR4444, PLANE0, OFFS2103,
147       SUB4444),
148   MAKE_RGBA_FORMAT (ARGB, "raw video", DPTH8888, PSTR4444, PLANE0, OFFS1230,
149       SUB4444),
150   MAKE_RGBA_FORMAT (ABGR, "raw video", DPTH8888, PSTR4444, PLANE0, OFFS3210,
151       SUB4444),
152   MAKE_RGB_FORMAT (RGB, "raw video", DPTH888, PSTR333, PLANE0, OFFS012, SUB444),
153   MAKE_RGB_FORMAT (BGR, "raw video", DPTH888, PSTR333, PLANE0, OFFS210, SUB444),
154
155   MAKE_YUV_FORMAT (Y41B, "raw video", GST_MAKE_FOURCC ('Y', '4', '1', 'B'),
156       DPTH888, PSTR111,
157       PLANE012, OFFS0, SUB411),
158   MAKE_YUV_FORMAT (Y42B, "raw video", GST_MAKE_FOURCC ('Y', '4', '2', 'B'),
159       DPTH888, PSTR111,
160       PLANE012, OFFS0, SUB422),
161   MAKE_YUV_FORMAT (YVYU, "raw video", GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'),
162       DPTH888, PSTR244,
163       PLANE0, OFFS031, SUB422),
164   MAKE_YUV_FORMAT (Y444, "raw video", GST_MAKE_FOURCC ('Y', '4', '4', '4'),
165       DPTH888, PSTR111,
166       PLANE012, OFFS0, SUB444),
167   MAKE_YUV_FORMAT (v210, "raw video", GST_MAKE_FOURCC ('v', '2', '1', '0'),
168       DPTH10_10_10,
169       PSTR0, PLANE0, OFFS0, SUB422),
170   MAKE_YUV_FORMAT (v216, "raw video", GST_MAKE_FOURCC ('v', '2', '1', '6'),
171       DPTH16_16_16,
172       PSTR488, PLANE0, OFFS026, SUB422),
173   MAKE_YUV_FORMAT (NV12, "raw video", GST_MAKE_FOURCC ('N', 'V', '1', '2'),
174       DPTH888, PSTR122,
175       PLANE011, OFFS001, SUB420),
176   MAKE_YUV_FORMAT (NV21, "raw video", GST_MAKE_FOURCC ('N', 'V', '2', '1'),
177       DPTH888, PSTR122,
178       PLANE011, OFFS010, SUB420),
179
180   MAKE_GRAY_FORMAT (GRAY8, "raw video", DPTH8, PSTR1, PLANE0, OFFS0, SUB4),
181   MAKE_GRAY_FORMAT (GRAY16_BE, "raw video", DPTH16, PSTR2, PLANE0, OFFS0, SUB4),
182   MAKE_GRAY_FORMAT (GRAY16_LE, "raw video", DPTH16, PSTR2, PLANE0, OFFS0, SUB4),
183
184   MAKE_YUV_FORMAT (v308, "raw video", GST_MAKE_FOURCC ('v', '3', '0', '8'),
185       DPTH888, PSTR333,
186       PLANE0, OFFS012, SUB444),
187   MAKE_YUV_FORMAT (Y800, "raw video", GST_MAKE_FOURCC ('Y', '8', '0', '0'),
188       DPTH8, PSTR1,
189       PLANE0, OFFS0, SUB4),
190   MAKE_YUV_FORMAT (Y16, "raw video", GST_MAKE_FOURCC ('Y', '1', '6', ' '),
191       DPTH16, PSTR2,
192       PLANE0, OFFS0, SUB4),
193
194   MAKE_RGB_FORMAT (RGB16, "raw video", DPTH565, PSTR222, PLANE0, OFFS0, SUB444),
195   MAKE_RGB_FORMAT (BGR16, "raw video", DPTH565, PSTR222, PLANE0, OFFS0, SUB444),
196   MAKE_RGB_FORMAT (RGB15, "raw video", DPTH555, PSTR222, PLANE0, OFFS0, SUB444),
197   MAKE_RGB_FORMAT (BGR15, "raw video", DPTH555, PSTR222, PLANE0, OFFS0, SUB444),
198
199   MAKE_YUV_FORMAT (UYVP, "raw video", GST_MAKE_FOURCC ('U', 'Y', 'V', 'P'),
200       DPTH10_10_10,
201       PSTR0, PLANE0, OFFS0, SUB422),
202   MAKE_YUVA_FORMAT (A420, "raw video", GST_MAKE_FOURCC ('A', '4', '2', '0'),
203       DPTH8888,
204       PSTR1111, PLANE0123, OFFS0, SUB4204),
205   MAKE_RGBA_FORMAT (RGB8_PALETTED, "raw video", DPTH8888, PSTR1111, PLANE0,
206       OFFS0, SUB4444),
207   MAKE_YUV_FORMAT (YUV9, "raw video", GST_MAKE_FOURCC ('Y', 'U', 'V', '9'),
208       DPTH888, PSTR111,
209       PLANE012, OFFS0, SUB410),
210   MAKE_YUV_FORMAT (YVU9, "raw video", GST_MAKE_FOURCC ('Y', 'V', 'U', '9'),
211       DPTH888, PSTR111,
212       PLANE021, OFFS0, SUB410),
213   MAKE_YUV_FORMAT (IYU1, "raw video", GST_MAKE_FOURCC ('I', 'Y', 'U', '1'),
214       DPTH888, PSTR0,
215       PLANE0, OFFS104, SUB411),
216   MAKE_RGBA_FORMAT (ARGB64, "raw video", DPTH16_16_16_16, PSTR8888, PLANE0,
217       OFFS2460,
218       SUB444),
219   MAKE_YUVA_FORMAT (AYUV64, "raw video", 0x00000000, DPTH16_16_16_16, PSTR8888,
220       PLANE0,
221       OFFS2460, SUB444),
222   MAKE_YUV_FORMAT (r210, "raw video", GST_MAKE_FOURCC ('r', '2', '1', '0'),
223       DPTH10_10_10,
224       PSTR444, PLANE0, OFFS0, SUB444),
225 };
226
227 /**
228  * SECTION:gstvideo
229  * @short_description: Support library for video operations
230  *
231  * <refsect2>
232  * <para>
233  * This library contains some helper functions and includes the
234  * videosink and videofilter base classes.
235  * </para>
236  * </refsect2>
237  */
238
239 /**
240  * gst_video_calculate_display_ratio:
241  * @dar_n: Numerator of the calculated display_ratio
242  * @dar_d: Denominator of the calculated display_ratio
243  * @video_width: Width of the video frame in pixels
244  * @video_height: Height of the video frame in pixels
245  * @video_par_n: Numerator of the pixel aspect ratio of the input video.
246  * @video_par_d: Denominator of the pixel aspect ratio of the input video.
247  * @display_par_n: Numerator of the pixel aspect ratio of the display device
248  * @display_par_d: Denominator of the pixel aspect ratio of the display device
249  *
250  * Given the Pixel Aspect Ratio and size of an input video frame, and the
251  * pixel aspect ratio of the intended display device, calculates the actual
252  * display ratio the video will be rendered with.
253  *
254  * Returns: A boolean indicating success and a calculated Display Ratio in the
255  * dar_n and dar_d parameters.
256  * The return value is FALSE in the case of integer overflow or other error.
257  *
258  * Since: 0.10.7
259  */
260 gboolean
261 gst_video_calculate_display_ratio (guint * dar_n, guint * dar_d,
262     guint video_width, guint video_height,
263     guint video_par_n, guint video_par_d,
264     guint display_par_n, guint display_par_d)
265 {
266   gint num, den;
267   gint tmp_n, tmp_d;
268
269   g_return_val_if_fail (dar_n != NULL, FALSE);
270   g_return_val_if_fail (dar_d != NULL, FALSE);
271
272   /* Calculate (video_width * video_par_n * display_par_d) /
273    * (video_height * video_par_d * display_par_n) */
274   if (!gst_util_fraction_multiply (video_width, video_height, video_par_n,
275           video_par_d, &tmp_n, &tmp_d))
276     goto error_overflow;
277
278   if (!gst_util_fraction_multiply (tmp_n, tmp_d, display_par_d, display_par_n,
279           &num, &den))
280     goto error_overflow;
281
282   g_return_val_if_fail (num > 0, FALSE);
283   g_return_val_if_fail (den > 0, FALSE);
284
285   *dar_n = num;
286   *dar_d = den;
287
288   return TRUE;
289
290   /* ERRORS */
291 error_overflow:
292   {
293     GST_WARNING ("overflow in multiply");
294     return FALSE;
295   }
296 }
297
298 static GstVideoFormat
299 gst_video_format_from_rgb32_masks (int red_mask, int green_mask, int blue_mask)
300 {
301   if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
302       blue_mask == 0x0000ff00) {
303     return GST_VIDEO_FORMAT_RGBx;
304   }
305   if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
306       blue_mask == 0xff000000) {
307     return GST_VIDEO_FORMAT_BGRx;
308   }
309   if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
310       blue_mask == 0x000000ff) {
311     return GST_VIDEO_FORMAT_xRGB;
312   }
313   if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
314       blue_mask == 0x00ff0000) {
315     return GST_VIDEO_FORMAT_xBGR;
316   }
317
318   return GST_VIDEO_FORMAT_UNKNOWN;
319 }
320
321 static GstVideoFormat
322 gst_video_format_from_rgba32_masks (int red_mask, int green_mask,
323     int blue_mask, int alpha_mask)
324 {
325   if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
326       blue_mask == 0x0000ff00 && alpha_mask == 0x000000ff) {
327     return GST_VIDEO_FORMAT_RGBA;
328   }
329   if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
330       blue_mask == 0xff000000 && alpha_mask == 0x000000ff) {
331     return GST_VIDEO_FORMAT_BGRA;
332   }
333   if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
334       blue_mask == 0x000000ff && alpha_mask == 0xff000000) {
335     return GST_VIDEO_FORMAT_ARGB;
336   }
337   if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
338       blue_mask == 0x00ff0000 && alpha_mask == 0xff000000) {
339     return GST_VIDEO_FORMAT_ABGR;
340   }
341   return GST_VIDEO_FORMAT_UNKNOWN;
342 }
343
344 static GstVideoFormat
345 gst_video_format_from_rgb24_masks (int red_mask, int green_mask, int blue_mask)
346 {
347   if (red_mask == 0xff0000 && green_mask == 0x00ff00 && blue_mask == 0x0000ff) {
348     return GST_VIDEO_FORMAT_RGB;
349   }
350   if (red_mask == 0x0000ff && green_mask == 0x00ff00 && blue_mask == 0xff0000) {
351     return GST_VIDEO_FORMAT_BGR;
352   }
353
354   return GST_VIDEO_FORMAT_UNKNOWN;
355 }
356
357 #define GST_VIDEO_COMP1_MASK_16_INT 0xf800
358 #define GST_VIDEO_COMP2_MASK_16_INT 0x07e0
359 #define GST_VIDEO_COMP3_MASK_16_INT 0x001f
360
361 #define GST_VIDEO_COMP1_MASK_15_INT 0x7c00
362 #define GST_VIDEO_COMP2_MASK_15_INT 0x03e0
363 #define GST_VIDEO_COMP3_MASK_15_INT 0x001f
364
365 static GstVideoFormat
366 gst_video_format_from_rgb16_masks (int red_mask, int green_mask, int blue_mask)
367 {
368   if (red_mask == GST_VIDEO_COMP1_MASK_16_INT
369       && green_mask == GST_VIDEO_COMP2_MASK_16_INT
370       && blue_mask == GST_VIDEO_COMP3_MASK_16_INT) {
371     return GST_VIDEO_FORMAT_RGB16;
372   }
373   if (red_mask == GST_VIDEO_COMP3_MASK_16_INT
374       && green_mask == GST_VIDEO_COMP2_MASK_16_INT
375       && blue_mask == GST_VIDEO_COMP1_MASK_16_INT) {
376     return GST_VIDEO_FORMAT_BGR16;
377   }
378   if (red_mask == GST_VIDEO_COMP1_MASK_15_INT
379       && green_mask == GST_VIDEO_COMP2_MASK_15_INT
380       && blue_mask == GST_VIDEO_COMP3_MASK_15_INT) {
381     return GST_VIDEO_FORMAT_RGB15;
382   }
383   if (red_mask == GST_VIDEO_COMP3_MASK_15_INT
384       && green_mask == GST_VIDEO_COMP2_MASK_15_INT
385       && blue_mask == GST_VIDEO_COMP1_MASK_15_INT) {
386     return GST_VIDEO_FORMAT_BGR15;
387   }
388   return GST_VIDEO_FORMAT_UNKNOWN;
389 }
390
391 /**
392  * gst_video_format_from_masks:
393  * @depth: the amount of bits used for a pixel
394  * @bpp: the amount of bits used to store a pixel. This value is bigger than
395  *   @depth
396  * @endianness: the endianness of the masks
397  * @red_mask: the red mask
398  * @green_mask: the green mask
399  * @blue_mask: the blue mask
400  * @alpha_mask: the optional alpha mask
401  *
402  * Find the #GstVideoFormat for the given parameters.
403  *
404  * Returns: a #GstVideoFormat or GST_VIDEO_FORMAT_UNKNOWN when the parameters to
405  * not specify a known format.
406  */
407 GstVideoFormat
408 gst_video_format_from_masks (gint depth, gint bpp, gint endianness,
409     gint red_mask, gint green_mask, gint blue_mask, gint alpha_mask)
410 {
411   GstVideoFormat format;
412
413   /* our caps system handles 24/32bpp RGB as big-endian. */
414   if ((bpp == 24 || bpp == 32) && endianness == G_LITTLE_ENDIAN) {
415     red_mask = GUINT32_TO_BE (red_mask);
416     green_mask = GUINT32_TO_BE (green_mask);
417     blue_mask = GUINT32_TO_BE (blue_mask);
418     endianness = G_BIG_ENDIAN;
419     if (bpp == 24) {
420       red_mask >>= 8;
421       green_mask >>= 8;
422       blue_mask >>= 8;
423     }
424   }
425
426   if (depth == 30 && bpp == 32) {
427     format = GST_VIDEO_FORMAT_r210;
428   } else if (depth == 24 && bpp == 32) {
429     format = gst_video_format_from_rgb32_masks (red_mask, green_mask,
430         blue_mask);
431   } else if (depth == 32 && bpp == 32 && alpha_mask) {
432     format = gst_video_format_from_rgba32_masks (red_mask, green_mask,
433         blue_mask, alpha_mask);
434   } else if (depth == 24 && bpp == 24) {
435     format = gst_video_format_from_rgb24_masks (red_mask, green_mask,
436         blue_mask);
437   } else if ((depth == 15 || depth == 16) && bpp == 16 &&
438       endianness == G_BYTE_ORDER) {
439     format = gst_video_format_from_rgb16_masks (red_mask, green_mask,
440         blue_mask);
441   } else if (depth == 8 && bpp == 8) {
442     format = GST_VIDEO_FORMAT_RGB8_PALETTED;
443   } else if (depth == 64 && bpp == 64) {
444     format = gst_video_format_from_rgba32_masks (red_mask, green_mask,
445         blue_mask, alpha_mask);
446     if (format == GST_VIDEO_FORMAT_ARGB) {
447       format = GST_VIDEO_FORMAT_ARGB64;
448     } else {
449       format = GST_VIDEO_FORMAT_UNKNOWN;
450     }
451   } else {
452     format = GST_VIDEO_FORMAT_UNKNOWN;
453   }
454   return format;
455 }
456
457 /**
458  * gst_video_format_from_fourcc:
459  * @fourcc: a FOURCC value representing raw YUV video
460  *
461  * Converts a FOURCC value into the corresponding #GstVideoFormat.
462  * If the FOURCC cannot be represented by #GstVideoFormat,
463  * #GST_VIDEO_FORMAT_UNKNOWN is returned.
464  *
465  * Since: 0.10.16
466  *
467  * Returns: the #GstVideoFormat describing the FOURCC value
468  */
469 GstVideoFormat
470 gst_video_format_from_fourcc (guint32 fourcc)
471 {
472   switch (fourcc) {
473     case GST_MAKE_FOURCC ('I', '4', '2', '0'):
474       return GST_VIDEO_FORMAT_I420;
475     case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
476       return GST_VIDEO_FORMAT_YV12;
477     case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
478       return GST_VIDEO_FORMAT_YUY2;
479     case GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'):
480       return GST_VIDEO_FORMAT_YVYU;
481     case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
482       return GST_VIDEO_FORMAT_UYVY;
483     case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
484       return GST_VIDEO_FORMAT_AYUV;
485     case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
486       return GST_VIDEO_FORMAT_Y41B;
487     case GST_MAKE_FOURCC ('Y', '4', '2', 'B'):
488       return GST_VIDEO_FORMAT_Y42B;
489     case GST_MAKE_FOURCC ('Y', '4', '4', '4'):
490       return GST_VIDEO_FORMAT_Y444;
491     case GST_MAKE_FOURCC ('v', '2', '1', '0'):
492       return GST_VIDEO_FORMAT_v210;
493     case GST_MAKE_FOURCC ('v', '2', '1', '6'):
494       return GST_VIDEO_FORMAT_v216;
495     case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
496       return GST_VIDEO_FORMAT_NV12;
497     case GST_MAKE_FOURCC ('N', 'V', '2', '1'):
498       return GST_VIDEO_FORMAT_NV21;
499     case GST_MAKE_FOURCC ('v', '3', '0', '8'):
500       return GST_VIDEO_FORMAT_v308;
501     case GST_MAKE_FOURCC ('Y', '8', '0', '0'):
502     case GST_MAKE_FOURCC ('Y', '8', ' ', ' '):
503     case GST_MAKE_FOURCC ('G', 'R', 'E', 'Y'):
504       return GST_VIDEO_FORMAT_Y800;
505     case GST_MAKE_FOURCC ('Y', '1', '6', ' '):
506       return GST_VIDEO_FORMAT_Y16;
507     case GST_MAKE_FOURCC ('U', 'Y', 'V', 'P'):
508       return GST_VIDEO_FORMAT_UYVP;
509     case GST_MAKE_FOURCC ('A', '4', '2', '0'):
510       return GST_VIDEO_FORMAT_A420;
511     case GST_MAKE_FOURCC ('Y', 'U', 'V', '9'):
512       return GST_VIDEO_FORMAT_YUV9;
513     case GST_MAKE_FOURCC ('Y', 'V', 'U', '9'):
514       return GST_VIDEO_FORMAT_YVU9;
515     case GST_MAKE_FOURCC ('I', 'Y', 'U', '1'):
516       return GST_VIDEO_FORMAT_IYU1;
517     case GST_MAKE_FOURCC ('A', 'Y', '6', '4'):
518       return GST_VIDEO_FORMAT_AYUV64;
519     default:
520       return GST_VIDEO_FORMAT_UNKNOWN;
521   }
522 }
523
524 /**
525  * gst_video_format_from_string:
526  * @format: a format string
527  *
528  * Convert the @format string to its #GstVideoFormat.
529  *
530  * Returns: the #GstVideoFormat for @format or GST_VIDEO_FORMAT_UNKNOWN when the
531  * string is not a known format.
532  */
533 GstVideoFormat
534 gst_video_format_from_string (const gchar * format)
535 {
536   guint i;
537
538   g_return_val_if_fail (format != NULL, GST_VIDEO_FORMAT_UNKNOWN);
539
540   for (i = 0; i < G_N_ELEMENTS (formats); i++) {
541     if (strcmp (GST_VIDEO_FORMAT_INFO_NAME (&formats[i].info), format) == 0)
542       return GST_VIDEO_FORMAT_INFO_FORMAT (&formats[i].info);
543   }
544   return GST_VIDEO_FORMAT_UNKNOWN;
545 }
546
547
548 /**
549  * gst_video_format_to_fourcc:
550  * @format: a #GstVideoFormat video format
551  *
552  * Converts a #GstVideoFormat value into the corresponding FOURCC.  Only
553  * a few YUV formats have corresponding FOURCC values.  If @format has
554  * no corresponding FOURCC value, 0 is returned.
555  *
556  * Since: 0.10.16
557  *
558  * Returns: the FOURCC corresponding to @format
559  */
560 guint32
561 gst_video_format_to_fourcc (GstVideoFormat format)
562 {
563   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
564
565   if (format >= G_N_ELEMENTS (formats))
566     return 0;
567
568   return formats[format].fourcc;
569 }
570
571 const gchar *
572 gst_video_format_to_string (GstVideoFormat format)
573 {
574   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
575
576   if (format >= G_N_ELEMENTS (formats))
577     return NULL;
578
579   return GST_VIDEO_FORMAT_INFO_NAME (&formats[format].info);
580 }
581
582 /**
583  * gst_video_format_get_info:
584  * @format: a #GstVideoFormat
585  *
586  * Get the #GstVideoFormatInfo for @format
587  *
588  * Returns: The #GstVideoFormatInfo for @format.
589  */
590 const GstVideoFormatInfo *
591 gst_video_format_get_info (GstVideoFormat format)
592 {
593   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
594   g_return_val_if_fail (format < G_N_ELEMENTS (formats), NULL);
595
596   return &formats[format].info;
597 }
598
599 /**
600  * gst_video_info_init:
601  * @info: a #GstVideoInfo
602  *
603  * Initialize @info with default values.
604  */
605 void
606 gst_video_info_init (GstVideoInfo * info)
607 {
608   g_return_if_fail (info != NULL);
609
610   memset (info, 0, sizeof (GstVideoInfo));
611
612   info->finfo = &formats[GST_VIDEO_FORMAT_UNKNOWN].info;
613
614   info->views = 1;
615   /* arrange for sensible defaults, e.g. if turned into caps */
616   info->fps_n = 0;
617   info->fps_d = 1;
618   info->par_n = 1;
619   info->par_d = 1;
620 }
621
622 /**
623  * gst_video_info_set_format:
624  * @info: a #GstVideoInfo
625  * @format: the format
626  * @width: a width
627  * @height: a height
628  *
629  * Set the default info for a video frame of @format and @width and @height.
630  */
631 void
632 gst_video_info_set_format (GstVideoInfo * info, GstVideoFormat format,
633     guint width, guint height)
634 {
635   const GstVideoFormatInfo *finfo;
636
637   g_return_if_fail (info != NULL);
638   g_return_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN);
639
640   finfo = &formats[format].info;
641
642   info->flags = 0;
643   info->finfo = finfo;
644   info->width = width;
645   info->height = height;
646
647   fill_planes (info);
648 }
649
650 typedef struct
651 {
652   const gchar *name;
653   GstVideoChromaSite site;
654 } ChromaSiteInfo;
655
656 static const ChromaSiteInfo chromasite[] = {
657   {"jpeg", GST_VIDEO_CHROMA_SITE_JPEG},
658   {"mpeg2", GST_VIDEO_CHROMA_SITE_MPEG2},
659   {"dv", GST_VIDEO_CHROMA_SITE_DV}
660 };
661
662 static GstVideoChromaSite
663 gst_video_chroma_from_string (const gchar * s)
664 {
665   gint i;
666   for (i = 0; i < G_N_ELEMENTS (chromasite); i++) {
667     if (g_str_equal (chromasite[i].name, s))
668       return chromasite[i].site;
669   }
670   return GST_VIDEO_CHROMA_SITE_UNKNOWN;
671 }
672
673 static const gchar *
674 gst_video_chroma_to_string (GstVideoChromaSite site)
675 {
676   gint i;
677   for (i = 0; i < G_N_ELEMENTS (chromasite); i++) {
678     if (chromasite[i].site == site)
679       return chromasite[i].name;
680   }
681   return NULL;
682 }
683
684 typedef struct
685 {
686   const gchar *name;
687   GstVideoColorimetry color;
688 } ColorimetryInfo;
689
690 #define MAKE_COLORIMETRY(n,r,m,t,p) { GST_VIDEO_COLORIMETRY_ ##n, \
691   { GST_VIDEO_COLOR_RANGE ##r, GST_VIDEO_COLOR_MATRIX_ ##m, \
692   GST_VIDEO_TRANSFER_ ##t, GST_VIDEO_COLOR_PRIMARIES_ ##p } }
693
694 static const ColorimetryInfo colorimetry[] = {
695   MAKE_COLORIMETRY (BT601, _16_235, BT601, BT709, BT470M),
696   MAKE_COLORIMETRY (BT709, _16_235, BT709, BT709, BT709),
697   MAKE_COLORIMETRY (SMPTE240M, _16_235, SMPTE240M, SMPTE240M, SMPTE240M),
698 };
699
700 static const ColorimetryInfo *
701 gst_video_get_colorimetry (const gchar * s)
702 {
703   gint i;
704
705   for (i = 0; i < G_N_ELEMENTS (colorimetry); i++) {
706     if (g_str_equal (colorimetry[i].name, s))
707       return &colorimetry[i];
708   }
709   return NULL;
710 }
711
712 #define IS_EQUAL(ci,i) (((ci)->color.range == (i)->range) && \
713                         ((ci)->color.matrix == (i)->matrix) && \
714                         ((ci)->color.transfer == (i)->transfer) && \
715                         ((ci)->color.primaries == (i)->primaries))
716
717
718 /**
719  * gst_video_colorimetry_from_string
720  * @cinfo: a #GstVideoColorimetry
721  * @color: a colorimetry string
722  *
723  * Parse the colorimetry string and update @cinfo with the parsed
724  * values.
725  *
726  * Returns: #TRUE if @color points to valid colorimetry info.
727  */
728 gboolean
729 gst_video_colorimetry_from_string (GstVideoColorimetry * cinfo,
730     const gchar * color)
731 {
732   const ColorimetryInfo *ci;
733
734   if ((ci = gst_video_get_colorimetry (color))) {
735     *cinfo = ci->color;
736   } else {
737     /* FIXME, split and parse */
738     cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
739     cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
740     cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
741     cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
742   }
743   return TRUE;
744 }
745
746 static void
747 gst_video_caps_set_colorimetry (GstCaps * caps, GstVideoColorimetry * cinfo)
748 {
749   gint i;
750
751   for (i = 0; i < G_N_ELEMENTS (colorimetry); i++) {
752     if (IS_EQUAL (&colorimetry[i], cinfo)) {
753       gst_caps_set_simple (caps, "colorimetry", G_TYPE_STRING,
754           colorimetry[i].name, NULL);
755       return;
756     }
757   }
758   /* FIXME, construct colorimetry */
759 }
760
761 /**
762  * gst_video_colorimetry_matches:
763  * @info: a #GstVideoInfo
764  * @color: a colorimetry string
765  *
766  * Check if the colorimetry information in @info matches that of the
767  * string @color.
768  *
769  * Returns: #TRUE if @color conveys the same colorimetry info as the color
770  * information in @info.
771  */
772 gboolean
773 gst_video_colorimetry_matches (GstVideoColorimetry * cinfo, const gchar * color)
774 {
775   const ColorimetryInfo *ci;
776
777   if ((ci = gst_video_get_colorimetry (color)))
778     return IS_EQUAL (ci, cinfo);
779
780   return FALSE;
781 }
782
783 /**
784  * gst_video_info_from_caps:
785  * @info: a #GstVideoInfo
786  * @caps: a #GstCaps
787  *
788  * Parse @caps and update @info.
789  *
790  * Returns: TRUE if @caps could be parsed
791  */
792 gboolean
793 gst_video_info_from_caps (GstVideoInfo * info, const GstCaps * caps)
794 {
795   GstStructure *structure;
796   const gchar *s;
797   GstVideoFormat format;
798   gint width, height, views;
799   gint fps_n, fps_d;
800   gboolean interlaced;
801   gint par_n, par_d;
802
803   g_return_val_if_fail (info != NULL, FALSE);
804   g_return_val_if_fail (caps != NULL, FALSE);
805   g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
806
807   GST_DEBUG ("parsing caps %" GST_PTR_FORMAT, caps);
808
809   structure = gst_caps_get_structure (caps, 0);
810
811   if (!gst_structure_has_name (structure, "video/x-raw"))
812     goto wrong_name;
813
814   if (!(s = gst_structure_get_string (structure, "format")))
815     goto no_format;
816
817   format = gst_video_format_from_string (s);
818   if (format == GST_VIDEO_FORMAT_UNKNOWN)
819     goto unknown_format;
820
821   if (!gst_structure_get_int (structure, "width", &width))
822     goto no_width;
823   if (!gst_structure_get_int (structure, "height", &height))
824     goto no_height;
825
826   gst_video_info_set_format (info, format, width, height);
827
828   if (gst_structure_get_fraction (structure, "framerate", &fps_n, &fps_d)) {
829     if (fps_n == 0) {
830       /* variable framerate */
831       info->flags |= GST_VIDEO_FLAG_VARIABLE_FPS;
832       /* see if we have a max-framerate */
833       gst_structure_get_fraction (structure, "max-framerate", &fps_n, &fps_d);
834     }
835     info->fps_n = fps_n;
836     info->fps_d = fps_d;
837   } else {
838     /* unspecified is variable framerate */
839     info->fps_n = 0;
840     info->fps_d = 1;
841   }
842
843   if (!gst_structure_get_boolean (structure, "interlaced", &interlaced))
844     interlaced = FALSE;
845   if (interlaced)
846     info->flags |= GST_VIDEO_FLAG_INTERLACED;
847   else
848     info->flags &= ~GST_VIDEO_FLAG_INTERLACED;
849
850   if (gst_structure_get_int (structure, "views", &views))
851     info->views = views;
852   else
853     info->views = 1;
854
855   if ((s = gst_structure_get_string (structure, "chroma-site")))
856     info->chroma_site = gst_video_chroma_from_string (s);
857   else
858     info->chroma_site = GST_VIDEO_CHROMA_SITE_UNKNOWN;
859
860   if ((s = gst_structure_get_string (structure, "colorimetry")))
861     gst_video_colorimetry_from_string (&info->colorimetry, s);
862   else
863     memset (&info->colorimetry, 0, sizeof (GstVideoColorimetry));
864
865   if (gst_structure_get_fraction (structure, "pixel-aspect-ratio",
866           &par_n, &par_d)) {
867     info->par_n = par_n;
868     info->par_d = par_d;
869   } else {
870     info->par_n = 1;
871     info->par_d = 1;
872   }
873   return TRUE;
874
875   /* ERROR */
876 wrong_name:
877   {
878     GST_ERROR ("wrong name '%s', expected video/x-raw",
879         gst_structure_get_name (structure));
880     return FALSE;
881   }
882 no_format:
883   {
884     GST_ERROR ("no format given");
885     return FALSE;
886   }
887 unknown_format:
888   {
889     GST_ERROR ("unknown format '%s' given", s);
890     return FALSE;
891   }
892 no_width:
893   {
894     GST_ERROR ("no width property given");
895     return FALSE;
896   }
897 no_height:
898   {
899     GST_ERROR ("no height property given");
900     return FALSE;
901   }
902 }
903
904 /**
905  * gst_video_info_to_caps:
906  * @info: a #GstVideoInfo
907  *
908  * Convert the values of @info into a #GstCaps.
909  *
910  * Returns: a new #GstCaps containing the info of @info.
911  */
912 GstCaps *
913 gst_video_info_to_caps (GstVideoInfo * info)
914 {
915   GstCaps *caps;
916   const gchar *format;
917
918   g_return_val_if_fail (info != NULL, NULL);
919   g_return_val_if_fail (info->finfo != NULL, NULL);
920   g_return_val_if_fail (info->finfo->format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
921
922   format = gst_video_format_to_string (info->finfo->format);
923   g_return_val_if_fail (format != NULL, NULL);
924
925   caps = gst_caps_new_simple ("video/x-raw",
926       "format", G_TYPE_STRING, format,
927       "width", G_TYPE_INT, info->width,
928       "height", G_TYPE_INT, info->height,
929       "pixel-aspect-ratio", GST_TYPE_FRACTION, info->par_n, info->par_d, NULL);
930
931   if (info->flags & GST_VIDEO_FLAG_INTERLACED)
932     gst_caps_set_simple (caps, "interlaced", G_TYPE_BOOLEAN, TRUE, NULL);
933
934   if (info->chroma_site != GST_VIDEO_CHROMA_SITE_UNKNOWN)
935     gst_caps_set_simple (caps, "chroma-site", G_TYPE_STRING,
936         gst_video_chroma_to_string (info->chroma_site), NULL);
937
938   gst_video_caps_set_colorimetry (caps, &info->colorimetry);
939
940   if (info->views > 1)
941     gst_caps_set_simple (caps, "views", G_TYPE_INT, info->views, NULL);
942
943   if (info->flags & GST_VIDEO_FLAG_VARIABLE_FPS && info->fps_n != 0) {
944     /* variable fps with a max-framerate */
945     gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, 0, 1,
946         "max-framerate", GST_TYPE_FRACTION, info->fps_n, info->fps_d, NULL);
947   } else {
948     /* no variable fps or no max-framerate */
949     gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION,
950         info->fps_n, info->fps_d, NULL);
951   }
952
953   return caps;
954 }
955
956 /**
957  * gst_video_frame_map_id:
958  * @frame: pointer to #GstVideoFrame
959  * @info: a #GstVideoInfo
960  * @buffer: the buffer to map
961  * @id: the frame id to map
962  * @flags: #GstMapFlags
963  *
964  * Use @info and @buffer to fill in the values of @frame with the video frame
965  * information of frame @id.
966  *
967  * When @id is -1, the default frame is mapped. When @id != -1, this function
968  * will return %FALSE when there is no GstVideoMeta with that id.
969  *
970  * All video planes of @buffer will be mapped and the pointers will be set in
971  * @frame->data.
972  *
973  * Returns: %TRUE on success.
974  */
975 gboolean
976 gst_video_frame_map_id (GstVideoFrame * frame, GstVideoInfo * info,
977     GstBuffer * buffer, gint id, GstMapFlags flags)
978 {
979   GstVideoMeta *meta;
980   guint8 *data;
981   gsize size;
982   gint i;
983
984   g_return_val_if_fail (frame != NULL, FALSE);
985   g_return_val_if_fail (info != NULL, FALSE);
986   g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
987
988   if (id == -1)
989     meta = gst_buffer_get_video_meta (buffer);
990   else
991     meta = gst_buffer_get_video_meta_id (buffer, id);
992
993   frame->buffer = buffer;
994   frame->meta = meta;
995
996   if (meta) {
997     frame->info.flags = meta->flags;
998     frame->info.finfo = &formats[meta->format].info;
999     frame->info.width = meta->width;
1000     frame->info.height = meta->height;
1001     frame->id = meta->id;
1002
1003     for (i = 0; i < info->finfo->n_planes; i++) {
1004       frame->data[i] =
1005           gst_video_meta_map (meta, i, &frame->info.stride[i], flags);
1006     }
1007   } else {
1008     /* no metadata, we really need to have the metadata when the id is
1009      * specified. */
1010     if (id != -1)
1011       goto no_metadata;
1012
1013     /* copy the info */
1014     frame->info = *info;
1015     frame->id = id;
1016
1017     data = gst_buffer_map (buffer, &size, NULL, flags);
1018
1019     /* do some sanity checks */
1020     if (size < info->size)
1021       goto invalid_size;
1022
1023     /* set up pointers */
1024     for (i = 0; i < info->finfo->n_planes; i++) {
1025       frame->data[i] = data + info->offset[i];
1026     }
1027   }
1028   return TRUE;
1029
1030   /* ERRORS */
1031 no_metadata:
1032   {
1033     GST_ERROR ("no GstVideoMeta for id %d", id);
1034     return FALSE;
1035   }
1036 invalid_size:
1037   {
1038     GST_ERROR ("invalid buffer size %" G_GSIZE_FORMAT " < %" G_GSIZE_FORMAT,
1039         size, info->size);
1040     gst_buffer_unmap (buffer, data, size);
1041     return FALSE;
1042   }
1043 }
1044
1045 /**
1046  * gst_video_frame_map:
1047  * @frame: pointer to #GstVideoFrame
1048  * @info: a #GstVideoInfo
1049  * @buffer: the buffer to map
1050  * @flags: #GstMapFlags
1051  *
1052  * Use @info and @buffer to fill in the values of @frame.
1053  *
1054  * All video planes of @buffer will be mapped and the pointers will be set in
1055  * @frame->data.
1056  *
1057  * Returns: %TRUE on success.
1058  */
1059 gboolean
1060 gst_video_frame_map (GstVideoFrame * frame, GstVideoInfo * info,
1061     GstBuffer * buffer, GstMapFlags flags)
1062 {
1063   return gst_video_frame_map_id (frame, info, buffer, -1, flags);
1064 }
1065
1066 /**
1067  * gst_video_frame_unmap:
1068  * @frame: a #GstVideoFrame
1069  *
1070  * Unmap the memory previously mapped with gst_video_frame_map.
1071  */
1072 void
1073 gst_video_frame_unmap (GstVideoFrame * frame)
1074 {
1075   GstBuffer *buffer;
1076   GstVideoMeta *meta;
1077   gint i;
1078
1079   g_return_if_fail (frame != NULL);
1080
1081   buffer = frame->buffer;
1082   meta = frame->meta;
1083
1084   if (meta) {
1085     for (i = 0; i < frame->info.finfo->n_planes; i++) {
1086       gst_video_meta_unmap (meta, i, frame->data[i]);
1087     }
1088   } else {
1089     guint8 *data;
1090
1091     data = frame->data[0];
1092     data -= frame->info.offset[0];
1093     gst_buffer_unmap (buffer, data, -1);
1094   }
1095 }
1096
1097 /**
1098  * gst_video_frame_copy:
1099  * @dest: a #GstVideoFrame
1100  * @src: a #GstVideoFrame
1101  *
1102  * Copy the contents from @src to @dest.
1103  *
1104  * Returns: TRUE if the contents could be copied.
1105  */
1106 gboolean
1107 gst_video_frame_copy (GstVideoFrame * dest, const GstVideoFrame * src)
1108 {
1109   guint i, n_planes;
1110   const GstVideoInfo *sinfo;
1111   GstVideoInfo *dinfo;
1112
1113   sinfo = &src->info;
1114   dinfo = &dest->info;
1115
1116   g_return_val_if_fail (dinfo->finfo->format == sinfo->finfo->format, FALSE);
1117   g_return_val_if_fail (dinfo->width == sinfo->width
1118       && dinfo->height == sinfo->height, FALSE);
1119
1120   n_planes = dinfo->finfo->n_planes;
1121
1122   for (i = 0; i < n_planes; i++) {
1123     guint w, h, j;
1124     guint8 *sp, *dp;
1125     gint ss, ds;
1126
1127     sp = src->data[i];
1128     dp = dest->data[i];
1129
1130     ss = sinfo->stride[i];
1131     ds = dinfo->stride[i];
1132
1133     w = MIN (ABS (ss), ABS (ds));
1134     h = GST_VIDEO_FRAME_COMP_HEIGHT (dest, i);
1135
1136     GST_DEBUG ("w %d h %d", w, h);
1137
1138     for (j = 0; j < h; j++) {
1139       memcpy (dp, sp, w);
1140       dp += ds;
1141       sp += ss;
1142     }
1143   }
1144   return TRUE;
1145 }
1146
1147 static int
1148 fill_planes (GstVideoInfo * info)
1149 {
1150   gint width, height;
1151
1152   width = info->width;
1153   height = info->height;
1154
1155   switch (info->finfo->format) {
1156     case GST_VIDEO_FORMAT_YUY2:
1157     case GST_VIDEO_FORMAT_YVYU:
1158     case GST_VIDEO_FORMAT_UYVY:
1159       info->stride[0] = GST_ROUND_UP_4 (width * 2);
1160       info->offset[0] = 0;
1161       info->size = info->stride[0] * height;
1162       break;
1163     case GST_VIDEO_FORMAT_AYUV:
1164     case GST_VIDEO_FORMAT_RGBx:
1165     case GST_VIDEO_FORMAT_RGBA:
1166     case GST_VIDEO_FORMAT_BGRx:
1167     case GST_VIDEO_FORMAT_BGRA:
1168     case GST_VIDEO_FORMAT_xRGB:
1169     case GST_VIDEO_FORMAT_ARGB:
1170     case GST_VIDEO_FORMAT_xBGR:
1171     case GST_VIDEO_FORMAT_ABGR:
1172     case GST_VIDEO_FORMAT_r210:
1173       info->stride[0] = width * 4;
1174       info->offset[0] = 0;
1175       info->size = info->stride[0] * height;
1176       break;
1177     case GST_VIDEO_FORMAT_RGB16:
1178     case GST_VIDEO_FORMAT_BGR16:
1179     case GST_VIDEO_FORMAT_RGB15:
1180     case GST_VIDEO_FORMAT_BGR15:
1181       info->stride[0] = GST_ROUND_UP_4 (width * 2);
1182       info->offset[0] = 0;
1183       info->size = info->stride[0] * height;
1184       break;
1185     case GST_VIDEO_FORMAT_RGB:
1186     case GST_VIDEO_FORMAT_BGR:
1187     case GST_VIDEO_FORMAT_v308:
1188       info->stride[0] = GST_ROUND_UP_4 (width * 3);
1189       info->offset[0] = 0;
1190       info->size = info->stride[0] * height;
1191       break;
1192     case GST_VIDEO_FORMAT_v210:
1193       info->stride[0] = ((width + 47) / 48) * 128;
1194       info->offset[0] = 0;
1195       info->size = info->stride[0] * height;
1196       break;
1197     case GST_VIDEO_FORMAT_v216:
1198       info->stride[0] = GST_ROUND_UP_8 (width * 4);
1199       info->offset[0] = 0;
1200       info->size = info->stride[0] * height;
1201       break;
1202     case GST_VIDEO_FORMAT_GRAY8:
1203     case GST_VIDEO_FORMAT_Y800:
1204       info->stride[0] = GST_ROUND_UP_4 (width);
1205       info->offset[0] = 0;
1206       info->size = info->stride[0] * height;
1207       break;
1208     case GST_VIDEO_FORMAT_GRAY16_BE:
1209     case GST_VIDEO_FORMAT_GRAY16_LE:
1210     case GST_VIDEO_FORMAT_Y16:
1211       info->stride[0] = GST_ROUND_UP_4 (width * 2);
1212       info->offset[0] = 0;
1213       info->size = info->stride[0] * height;
1214       break;
1215     case GST_VIDEO_FORMAT_UYVP:
1216       info->stride[0] = GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4);
1217       info->offset[0] = 0;
1218       info->size = info->stride[0] * height;
1219       break;
1220     case GST_VIDEO_FORMAT_RGB8_PALETTED:
1221       info->stride[0] = GST_ROUND_UP_4 (width);
1222       info->offset[0] = 0;
1223       info->size = info->stride[0] * height;
1224       break;
1225     case GST_VIDEO_FORMAT_IYU1:
1226       info->stride[0] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) +
1227           GST_ROUND_UP_4 (width) / 2);
1228       info->offset[0] = 0;
1229       info->size = info->stride[0] * height;
1230       break;
1231     case GST_VIDEO_FORMAT_ARGB64:
1232     case GST_VIDEO_FORMAT_AYUV64:
1233       info->stride[0] = width * 8;
1234       info->offset[0] = 0;
1235       info->size = info->stride[0] * height;
1236       break;
1237     case GST_VIDEO_FORMAT_I420:
1238       info->stride[0] = GST_ROUND_UP_4 (width);
1239       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1240       info->stride[2] = info->stride[1];
1241       info->offset[0] = 0;
1242       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1243       info->offset[2] = info->offset[1] +
1244           info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
1245       info->size = info->offset[2] +
1246           info->stride[2] * (GST_ROUND_UP_2 (height) / 2);
1247       break;
1248     case GST_VIDEO_FORMAT_YV12:        /* same as I420, but plane 1+2 swapped */
1249       info->stride[0] = GST_ROUND_UP_4 (width);
1250       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1251       info->stride[2] = info->stride[1];
1252       info->offset[0] = 0;
1253       info->offset[2] = info->stride[0] * GST_ROUND_UP_2 (height);
1254       info->offset[1] = info->offset[2] +
1255           info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
1256       info->size = info->offset[1] +
1257           info->stride[2] * (GST_ROUND_UP_2 (height) / 2);
1258       break;
1259     case GST_VIDEO_FORMAT_Y41B:
1260       info->stride[0] = GST_ROUND_UP_4 (width);
1261       info->stride[1] = GST_ROUND_UP_16 (width) / 4;
1262       info->stride[2] = info->stride[1];
1263       info->offset[0] = 0;
1264       info->offset[1] = info->stride[0] * height;
1265       info->offset[2] = info->offset[1] + info->stride[1] * height;
1266       /* simplification of ROUNDUP4(w)*h + 2*((ROUNDUP16(w)/4)*h */
1267       info->size = (info->stride[0] + (GST_ROUND_UP_16 (width) / 2)) * height;
1268       break;
1269     case GST_VIDEO_FORMAT_Y42B:
1270       info->stride[0] = GST_ROUND_UP_4 (width);
1271       info->stride[1] = GST_ROUND_UP_8 (width) / 2;
1272       info->stride[2] = info->stride[1];
1273       info->offset[0] = 0;
1274       info->offset[1] = info->stride[0] * height;
1275       info->offset[2] = info->offset[1] + info->stride[1] * height;
1276       /* simplification of ROUNDUP4(w)*h + 2*(ROUNDUP8(w)/2)*h */
1277       info->size = (info->stride[0] + GST_ROUND_UP_8 (width)) * height;
1278       break;
1279     case GST_VIDEO_FORMAT_Y444:
1280       info->stride[0] = GST_ROUND_UP_4 (width);
1281       info->stride[1] = info->stride[0];
1282       info->stride[2] = info->stride[0];
1283       info->offset[0] = 0;
1284       info->offset[1] = info->stride[0] * height;
1285       info->offset[2] = info->offset[1] * 2;
1286       info->size = info->stride[0] * height * 3;
1287       break;
1288     case GST_VIDEO_FORMAT_NV12:
1289     case GST_VIDEO_FORMAT_NV21:
1290       info->stride[0] = GST_ROUND_UP_4 (width);
1291       info->stride[1] = info->stride[0];
1292       info->offset[0] = 0;
1293       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1294       info->size = info->stride[0] * GST_ROUND_UP_2 (height) * 3 / 2;
1295       break;
1296     case GST_VIDEO_FORMAT_A420:
1297       info->stride[0] = GST_ROUND_UP_4 (width);
1298       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1299       info->stride[2] = info->stride[1];
1300       info->stride[3] = info->stride[0];
1301       info->offset[0] = 0;
1302       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1303       info->offset[2] = info->offset[1] +
1304           info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
1305       info->offset[3] = info->offset[2] +
1306           info->stride[2] * (GST_ROUND_UP_2 (height) / 2);
1307       info->size = info->offset[3] + info->stride[0];
1308       break;
1309     case GST_VIDEO_FORMAT_YUV9:
1310       info->stride[0] = GST_ROUND_UP_4 (width);
1311       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
1312       info->stride[2] = info->stride[1];
1313       info->offset[0] = 0;
1314       info->offset[1] = info->stride[0] * height;
1315       info->offset[2] = info->offset[1] +
1316           info->stride[1] * (GST_ROUND_UP_4 (height) / 4);
1317       info->size = info->offset[2] +
1318           info->stride[2] * (GST_ROUND_UP_4 (height) / 4);
1319       break;
1320     case GST_VIDEO_FORMAT_YVU9:
1321       info->stride[0] = GST_ROUND_UP_4 (width);
1322       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
1323       info->stride[2] = info->stride[1];
1324       info->offset[0] = 0;
1325       info->offset[2] = info->stride[0] * height;
1326       info->offset[1] = info->offset[2] +
1327           info->stride[1] * (GST_ROUND_UP_4 (height) / 4);
1328       info->size = info->offset[2] +
1329           info->stride[2] * (GST_ROUND_UP_4 (height) / 4);
1330       break;
1331     case GST_VIDEO_FORMAT_UNKNOWN:
1332       GST_ERROR ("invalid format");
1333       g_warning ("invalid format");
1334       break;
1335   }
1336   return 0;
1337 }
1338
1339 /**
1340  * gst_video_format_convert:
1341  * @info: a #GstVideoInfo
1342  * @src_format: #GstFormat of the @src_value
1343  * @src_value: value to convert
1344  * @dest_format: #GstFormat of the @dest_value
1345  * @dest_value: pointer to destination value
1346  *
1347  * Converts among various #GstFormat types.  This function handles
1348  * GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT.  For
1349  * raw video, GST_FORMAT_DEFAULT corresponds to video frames.  This
1350  * function can be used to handle pad queries of the type GST_QUERY_CONVERT.
1351  *
1352  * Since: 0.10.16
1353  *
1354  * Returns: TRUE if the conversion was successful.
1355  */
1356 gboolean
1357 gst_video_info_convert (GstVideoInfo * info,
1358     GstFormat src_format, gint64 src_value,
1359     GstFormat dest_format, gint64 * dest_value)
1360 {
1361   gboolean ret = FALSE;
1362   int size, fps_n, fps_d;
1363
1364   g_return_val_if_fail (info != NULL, 0);
1365   g_return_val_if_fail (info->finfo != NULL, 0);
1366   g_return_val_if_fail (info->finfo->format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1367   g_return_val_if_fail (info->size > 0, 0);
1368
1369   size = info->size;
1370   fps_n = info->fps_n;
1371   fps_d = info->fps_d;
1372
1373   GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s to %s",
1374       src_value, gst_format_get_name (src_format),
1375       gst_format_get_name (dest_format));
1376
1377   if (src_format == dest_format) {
1378     *dest_value = src_value;
1379     ret = TRUE;
1380     goto done;
1381   }
1382
1383   if (src_value == -1) {
1384     *dest_value = -1;
1385     ret = TRUE;
1386     goto done;
1387   }
1388
1389   /* bytes to frames */
1390   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_DEFAULT) {
1391     if (size != 0) {
1392       *dest_value = gst_util_uint64_scale_int (src_value, 1, size);
1393     } else {
1394       GST_ERROR ("blocksize is 0");
1395       *dest_value = 0;
1396     }
1397     ret = TRUE;
1398     goto done;
1399   }
1400
1401   /* frames to bytes */
1402   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_BYTES) {
1403     *dest_value = gst_util_uint64_scale_int (src_value, size, 1);
1404     ret = TRUE;
1405     goto done;
1406   }
1407
1408   /* time to frames */
1409   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_DEFAULT) {
1410     if (fps_d != 0) {
1411       *dest_value = gst_util_uint64_scale (src_value,
1412           fps_n, GST_SECOND * fps_d);
1413     } else {
1414       GST_ERROR ("framerate denominator is 0");
1415       *dest_value = 0;
1416     }
1417     ret = TRUE;
1418     goto done;
1419   }
1420
1421   /* frames to time */
1422   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) {
1423     if (fps_n != 0) {
1424       *dest_value = gst_util_uint64_scale (src_value,
1425           GST_SECOND * fps_d, fps_n);
1426     } else {
1427       GST_ERROR ("framerate numerator is 0");
1428       *dest_value = 0;
1429     }
1430     ret = TRUE;
1431     goto done;
1432   }
1433
1434   /* time to bytes */
1435   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
1436     if (fps_d != 0) {
1437       *dest_value = gst_util_uint64_scale (src_value,
1438           fps_n * size, GST_SECOND * fps_d);
1439     } else {
1440       GST_ERROR ("framerate denominator is 0");
1441       *dest_value = 0;
1442     }
1443     ret = TRUE;
1444     goto done;
1445   }
1446
1447   /* bytes to time */
1448   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
1449     if (fps_n != 0 && size != 0) {
1450       *dest_value = gst_util_uint64_scale (src_value,
1451           GST_SECOND * fps_d, fps_n * size);
1452     } else {
1453       GST_ERROR ("framerate denominator and/or blocksize is 0");
1454       *dest_value = 0;
1455     }
1456     ret = TRUE;
1457   }
1458
1459 done:
1460
1461   GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, ret, *dest_value);
1462
1463   return ret;
1464 }
1465
1466 #define GST_VIDEO_EVENT_STILL_STATE_NAME "GstEventStillFrame"
1467
1468 /**
1469  * gst_video_event_new_still_frame:
1470  * @in_still: boolean value for the still-frame state of the event.
1471  *
1472  * Creates a new Still Frame event. If @in_still is %TRUE, then the event
1473  * represents the start of a still frame sequence. If it is %FALSE, then
1474  * the event ends a still frame sequence.
1475  *
1476  * To parse an event created by gst_video_event_new_still_frame() use
1477  * gst_video_event_parse_still_frame().
1478  *
1479  * Returns: The new GstEvent
1480  * Since: 0.10.26
1481  */
1482 GstEvent *
1483 gst_video_event_new_still_frame (gboolean in_still)
1484 {
1485   GstEvent *still_event;
1486   GstStructure *s;
1487
1488   s = gst_structure_new (GST_VIDEO_EVENT_STILL_STATE_NAME,
1489       "still-state", G_TYPE_BOOLEAN, in_still, NULL);
1490   still_event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1491
1492   return still_event;
1493 }
1494
1495 /**
1496  * gst_video_event_parse_still_frame:
1497  * @event: A #GstEvent to parse
1498  * @in_still: A boolean to receive the still-frame status from the event, or NULL
1499  *
1500  * Parse a #GstEvent, identify if it is a Still Frame event, and
1501  * return the still-frame state from the event if it is.
1502  * If the event represents the start of a still frame, the in_still
1503  * variable will be set to TRUE, otherwise FALSE. It is OK to pass NULL for the
1504  * in_still variable order to just check whether the event is a valid still-frame
1505  * event.
1506  *
1507  * Create a still frame event using gst_video_event_new_still_frame()
1508  *
1509  * Returns: %TRUE if the event is a valid still-frame event. %FALSE if not
1510  * Since: 0.10.26
1511  */
1512 gboolean
1513 gst_video_event_parse_still_frame (GstEvent * event, gboolean * in_still)
1514 {
1515   const GstStructure *s;
1516   gboolean ev_still_state;
1517
1518   g_return_val_if_fail (event != NULL, FALSE);
1519
1520   if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM)
1521     return FALSE;               /* Not a still frame event */
1522
1523   s = gst_event_get_structure (event);
1524   if (s == NULL
1525       || !gst_structure_has_name (s, GST_VIDEO_EVENT_STILL_STATE_NAME))
1526     return FALSE;               /* Not a still frame event */
1527   if (!gst_structure_get_boolean (s, "still-state", &ev_still_state))
1528     return FALSE;               /* Not a still frame event */
1529   if (in_still)
1530     *in_still = ev_still_state;
1531   return TRUE;
1532 }
1533
1534 /**
1535  * gst_video_parse_caps_palette:
1536  * @caps: #GstCaps to parse
1537  *
1538  * Returns the palette data from the caps as a #GstBuffer. For
1539  * #GST_VIDEO_FORMAT_RGB8_PALETTED this is containing 256 #guint32
1540  * values, each containing ARGB colors in native endianness.
1541  *
1542  * Returns: a #GstBuffer containing the palette data. Unref after usage.
1543  * Since: 0.10.32
1544  */
1545 GstBuffer *
1546 gst_video_parse_caps_palette (GstCaps * caps)
1547 {
1548   GstStructure *s;
1549   const GValue *p_v;
1550   GstBuffer *p;
1551
1552   if (!gst_caps_is_fixed (caps))
1553     return NULL;
1554
1555   s = gst_caps_get_structure (caps, 0);
1556
1557   p_v = gst_structure_get_value (s, "palette_data");
1558   if (!p_v || !GST_VALUE_HOLDS_BUFFER (p_v))
1559     return NULL;
1560
1561   p = g_value_dup_boxed (p_v);
1562
1563   return p;
1564 }
1565
1566 #define GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME "GstForceKeyUnit"
1567
1568 /**
1569  * gst_video_event_new_downstream_force_key_unit:
1570  * @timestamp: the timestamp of the buffer that starts a new key unit
1571  * @stream_time: the stream_time of the buffer that starts a new key unit
1572  * @running_time: the running_time of the buffer that starts a new key unit
1573  * @all_headers: %TRUE to produce headers when starting a new key unit
1574  * @count: integer that can be used to number key units
1575  *
1576  * Creates a new downstream force key unit event. A downstream force key unit
1577  * event can be sent down the pipeline to request downstream elements to produce
1578  * a key unit. A downstream force key unit event must also be sent when handling
1579  * an upstream force key unit event to notify downstream that the latter has been
1580  * handled.
1581  *
1582  * To parse an event created by gst_video_event_new_downstream_force_key_unit() use
1583  * gst_video_event_parse_downstream_force_key_unit().
1584  *
1585  * Returns: The new GstEvent
1586  * Since: 0.10.36
1587  */
1588 GstEvent *
1589 gst_video_event_new_downstream_force_key_unit (GstClockTime timestamp,
1590     GstClockTime stream_time, GstClockTime running_time, gboolean all_headers,
1591     guint count)
1592 {
1593   GstEvent *force_key_unit_event;
1594   GstStructure *s;
1595
1596   s = gst_structure_new (GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME,
1597       "timestamp", G_TYPE_UINT64, timestamp,
1598       "stream-time", G_TYPE_UINT64, stream_time,
1599       "running-time", G_TYPE_UINT64, running_time,
1600       "all-headers", G_TYPE_BOOLEAN, all_headers,
1601       "count", G_TYPE_UINT, count, NULL);
1602   force_key_unit_event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1603
1604   return force_key_unit_event;
1605 }
1606
1607 /**
1608  * gst_video_event_new_upstream_force_key_unit:
1609  * @running_time: the running_time at which a new key unit should be produced
1610  * @all_headers: %TRUE to produce headers when starting a new key unit
1611  * @count: integer that can be used to number key units
1612  *
1613  * Creates a new upstream force key unit event. An upstream force key unit event
1614  * can be sent to request upstream elements to produce a key unit. 
1615  *
1616  * @running_time can be set to request a new key unit at a specific
1617  * running_time. If set to GST_CLOCK_TIME_NONE, upstream elements will produce a
1618  * new key unit as soon as possible.
1619  *
1620  * To parse an event created by gst_video_event_new_downstream_force_key_unit() use
1621  * gst_video_event_parse_downstream_force_key_unit().
1622  *
1623  * Returns: The new GstEvent
1624  * Since: 0.10.36
1625  */
1626 GstEvent *
1627 gst_video_event_new_upstream_force_key_unit (GstClockTime running_time,
1628     gboolean all_headers, guint count)
1629 {
1630   GstEvent *force_key_unit_event;
1631   GstStructure *s;
1632
1633   s = gst_structure_new (GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME,
1634       "running-time", GST_TYPE_CLOCK_TIME, running_time,
1635       "all-headers", G_TYPE_BOOLEAN, all_headers,
1636       "count", G_TYPE_UINT, count, NULL);
1637   force_key_unit_event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, s);
1638
1639   return force_key_unit_event;
1640 }
1641
1642 /**
1643  * gst_video_event_is_force_key_unit:
1644  * @event: A #GstEvent to check
1645  *
1646  * Checks if an event is a force key unit event. Returns true for both upstream
1647  * and downstream force key unit events.
1648  *
1649  * Returns: %TRUE if the event is a valid force key unit event
1650  * Since: 0.10.36
1651  */
1652 gboolean
1653 gst_video_event_is_force_key_unit (GstEvent * event)
1654 {
1655   const GstStructure *s;
1656
1657   g_return_val_if_fail (event != NULL, FALSE);
1658
1659   if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM &&
1660       GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_UPSTREAM)
1661     return FALSE;               /* Not a force key unit event */
1662
1663   s = gst_event_get_structure (event);
1664   if (s == NULL
1665       || !gst_structure_has_name (s, GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME))
1666     return FALSE;
1667
1668   return TRUE;
1669 }
1670
1671 /**
1672  * gst_video_event_parse_downstream_force_key_unit:
1673  * @event: A #GstEvent to parse
1674  * @timestamp: (out): A pointer to the timestamp in the event
1675  * @stream_time: (out): A pointer to the stream-time in the event
1676  * @running_time: (out): A pointer to the running-time in the event
1677  * @all_headers: (out): A pointer to the all_headers flag in the event
1678  * @count: (out): A pointer to the count field of the event
1679  *
1680  * Get timestamp, stream-time, running-time, all-headers and count in the force
1681  * key unit event. See gst_video_event_new_downstream_force_key_unit() for a
1682  * full description of the downstream force key unit event.
1683  *
1684  * Returns: %TRUE if the event is a valid downstream force key unit event.
1685  * Since: 0.10.36
1686  */
1687 gboolean
1688 gst_video_event_parse_downstream_force_key_unit (GstEvent * event,
1689     GstClockTime * timestamp, GstClockTime * stream_time,
1690     GstClockTime * running_time, gboolean * all_headers, guint * count)
1691 {
1692   const GstStructure *s;
1693   GstClockTime ev_timestamp, ev_stream_time, ev_running_time;
1694   gboolean ev_all_headers;
1695   guint ev_count;
1696
1697   g_return_val_if_fail (event != NULL, FALSE);
1698
1699   if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM)
1700     return FALSE;               /* Not a force key unit event */
1701
1702   s = gst_event_get_structure (event);
1703   if (s == NULL
1704       || !gst_structure_has_name (s, GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME))
1705     return FALSE;
1706
1707   if (!gst_structure_get_clock_time (s, "timestamp", &ev_timestamp))
1708     return FALSE;               /* Not a force key unit event */
1709   if (!gst_structure_get_clock_time (s, "stream-time", &ev_stream_time))
1710     return FALSE;               /* Not a force key unit event */
1711   if (!gst_structure_get_clock_time (s, "running-time", &ev_running_time))
1712     return FALSE;               /* Not a force key unit event */
1713   if (!gst_structure_get_boolean (s, "all-headers", &ev_all_headers))
1714     return FALSE;               /* Not a force key unit event */
1715   if (!gst_structure_get_uint (s, "count", &ev_count))
1716     return FALSE;               /* Not a force key unit event */
1717
1718   if (timestamp)
1719     *timestamp = ev_timestamp;
1720
1721   if (stream_time)
1722     *stream_time = ev_stream_time;
1723
1724   if (running_time)
1725     *running_time = ev_running_time;
1726
1727   if (all_headers)
1728     *all_headers = ev_all_headers;
1729
1730   if (count)
1731     *count = ev_count;
1732
1733   return TRUE;
1734 }
1735
1736 /**
1737  * gst_video_event_parse_upstream_force_key_unit:
1738  * @event: A #GstEvent to parse
1739  * @running_time: (out): A pointer to the running_time in the event
1740  * @all_headers: (out): A pointer to the all_headers flag in the event
1741  * @count: (out): A pointer to the count field in the event
1742  *
1743  * Get running-time, all-headers and count in the force key unit event. See
1744  * gst_video_event_new_upstream_force_key_unit() for a full description of the
1745  * upstream force key unit event.
1746  *
1747  * Create an upstream force key unit event using  gst_video_event_new_upstream_force_key_unit()
1748  *
1749  * Returns: %TRUE if the event is a valid upstream force-key-unit event. %FALSE if not
1750  * Since: 0.10.36
1751  */
1752 gboolean
1753 gst_video_event_parse_upstream_force_key_unit (GstEvent * event,
1754     GstClockTime * running_time, gboolean * all_headers, guint * count)
1755 {
1756   const GstStructure *s;
1757   GstClockTime ev_running_time;
1758   gboolean ev_all_headers;
1759   guint ev_count;
1760
1761   g_return_val_if_fail (event != NULL, FALSE);
1762
1763   if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_UPSTREAM)
1764     return FALSE;               /* Not a force key unit event */
1765
1766   s = gst_event_get_structure (event);
1767   if (s == NULL
1768       || !gst_structure_has_name (s, GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME))
1769     return FALSE;
1770
1771   if (!gst_structure_get_clock_time (s, "running-time", &ev_running_time))
1772     return FALSE;               /* Not a force key unit event */
1773   if (!gst_structure_get_boolean (s, "all-headers", &ev_all_headers))
1774     return FALSE;               /* Not a force key unit event */
1775   if (!gst_structure_get_uint (s, "count", &ev_count))
1776     return FALSE;               /* Not a force key unit event */
1777
1778   if (running_time)
1779     *running_time = ev_running_time;
1780
1781   if (all_headers)
1782     *all_headers = ev_all_headers;
1783
1784   if (count)
1785     *count = ev_count;
1786
1787   return TRUE;
1788 }