video: update interlace caps and docs
[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 static const gchar *interlace_mode[] = {
651   "progressive",
652   "interleaved",
653   "mixed",
654   "fields"
655 };
656
657 static const gchar *
658 gst_interlace_mode_to_string (GstVideoInterlaceMode mode)
659 {
660   if (mode < 0 || mode >= G_N_ELEMENTS (interlace_mode))
661     return NULL;
662
663   return interlace_mode[mode];
664 }
665
666 static GstVideoInterlaceMode
667 gst_interlace_mode_from_string (const gchar * mode)
668 {
669   gint i;
670   for (i = 0; i < G_N_ELEMENTS (interlace_mode); i++) {
671     if (g_str_equal (interlace_mode[i], mode))
672       return i;
673   }
674   return GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
675 }
676
677 typedef struct
678 {
679   const gchar *name;
680   GstVideoChromaSite site;
681 } ChromaSiteInfo;
682
683 static const ChromaSiteInfo chromasite[] = {
684   {"jpeg", GST_VIDEO_CHROMA_SITE_JPEG},
685   {"mpeg2", GST_VIDEO_CHROMA_SITE_MPEG2},
686   {"dv", GST_VIDEO_CHROMA_SITE_DV}
687 };
688
689 static GstVideoChromaSite
690 gst_video_chroma_from_string (const gchar * s)
691 {
692   gint i;
693   for (i = 0; i < G_N_ELEMENTS (chromasite); i++) {
694     if (g_str_equal (chromasite[i].name, s))
695       return chromasite[i].site;
696   }
697   return GST_VIDEO_CHROMA_SITE_UNKNOWN;
698 }
699
700 static const gchar *
701 gst_video_chroma_to_string (GstVideoChromaSite site)
702 {
703   gint i;
704   for (i = 0; i < G_N_ELEMENTS (chromasite); i++) {
705     if (chromasite[i].site == site)
706       return chromasite[i].name;
707   }
708   return NULL;
709 }
710
711 typedef struct
712 {
713   const gchar *name;
714   GstVideoColorimetry color;
715 } ColorimetryInfo;
716
717 #define MAKE_COLORIMETRY(n,r,m,t,p) { GST_VIDEO_COLORIMETRY_ ##n, \
718   { GST_VIDEO_COLOR_RANGE ##r, GST_VIDEO_COLOR_MATRIX_ ##m, \
719   GST_VIDEO_TRANSFER_ ##t, GST_VIDEO_COLOR_PRIMARIES_ ##p } }
720
721 static const ColorimetryInfo colorimetry[] = {
722   MAKE_COLORIMETRY (BT601, _16_235, BT601, BT709, BT470M),
723   MAKE_COLORIMETRY (BT709, _16_235, BT709, BT709, BT709),
724   MAKE_COLORIMETRY (SMPTE240M, _16_235, SMPTE240M, SMPTE240M, SMPTE240M),
725 };
726
727 static const ColorimetryInfo *
728 gst_video_get_colorimetry (const gchar * s)
729 {
730   gint i;
731
732   for (i = 0; i < G_N_ELEMENTS (colorimetry); i++) {
733     if (g_str_equal (colorimetry[i].name, s))
734       return &colorimetry[i];
735   }
736   return NULL;
737 }
738
739 #define IS_EQUAL(ci,i) (((ci)->color.range == (i)->range) && \
740                         ((ci)->color.matrix == (i)->matrix) && \
741                         ((ci)->color.transfer == (i)->transfer) && \
742                         ((ci)->color.primaries == (i)->primaries))
743
744
745 /**
746  * gst_video_colorimetry_from_string
747  * @cinfo: a #GstVideoColorimetry
748  * @color: a colorimetry string
749  *
750  * Parse the colorimetry string and update @cinfo with the parsed
751  * values.
752  *
753  * Returns: #TRUE if @color points to valid colorimetry info.
754  */
755 gboolean
756 gst_video_colorimetry_from_string (GstVideoColorimetry * cinfo,
757     const gchar * color)
758 {
759   const ColorimetryInfo *ci;
760
761   if ((ci = gst_video_get_colorimetry (color))) {
762     *cinfo = ci->color;
763   } else {
764     /* FIXME, split and parse */
765     cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
766     cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
767     cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
768     cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
769   }
770   return TRUE;
771 }
772
773 static void
774 gst_video_caps_set_colorimetry (GstCaps * caps, GstVideoColorimetry * cinfo)
775 {
776   gint i;
777
778   for (i = 0; i < G_N_ELEMENTS (colorimetry); i++) {
779     if (IS_EQUAL (&colorimetry[i], cinfo)) {
780       gst_caps_set_simple (caps, "colorimetry", G_TYPE_STRING,
781           colorimetry[i].name, NULL);
782       return;
783     }
784   }
785   /* FIXME, construct colorimetry */
786 }
787
788 /**
789  * gst_video_colorimetry_matches:
790  * @info: a #GstVideoInfo
791  * @color: a colorimetry string
792  *
793  * Check if the colorimetry information in @info matches that of the
794  * string @color.
795  *
796  * Returns: #TRUE if @color conveys the same colorimetry info as the color
797  * information in @info.
798  */
799 gboolean
800 gst_video_colorimetry_matches (GstVideoColorimetry * cinfo, const gchar * color)
801 {
802   const ColorimetryInfo *ci;
803
804   if ((ci = gst_video_get_colorimetry (color)))
805     return IS_EQUAL (ci, cinfo);
806
807   return FALSE;
808 }
809
810 /**
811  * gst_video_info_from_caps:
812  * @info: a #GstVideoInfo
813  * @caps: a #GstCaps
814  *
815  * Parse @caps and update @info.
816  *
817  * Returns: TRUE if @caps could be parsed
818  */
819 gboolean
820 gst_video_info_from_caps (GstVideoInfo * info, const GstCaps * caps)
821 {
822   GstStructure *structure;
823   const gchar *s;
824   GstVideoFormat format;
825   gint width, height, views;
826   gint fps_n, fps_d;
827   gint par_n, par_d;
828
829   g_return_val_if_fail (info != NULL, FALSE);
830   g_return_val_if_fail (caps != NULL, FALSE);
831   g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
832
833   GST_DEBUG ("parsing caps %" GST_PTR_FORMAT, caps);
834
835   structure = gst_caps_get_structure (caps, 0);
836
837   if (!gst_structure_has_name (structure, "video/x-raw"))
838     goto wrong_name;
839
840   if (!(s = gst_structure_get_string (structure, "format")))
841     goto no_format;
842
843   format = gst_video_format_from_string (s);
844   if (format == GST_VIDEO_FORMAT_UNKNOWN)
845     goto unknown_format;
846
847   if (!gst_structure_get_int (structure, "width", &width))
848     goto no_width;
849   if (!gst_structure_get_int (structure, "height", &height))
850     goto no_height;
851
852   gst_video_info_set_format (info, format, width, height);
853
854   if (gst_structure_get_fraction (structure, "framerate", &fps_n, &fps_d)) {
855     if (fps_n == 0) {
856       /* variable framerate */
857       info->flags |= GST_VIDEO_FLAG_VARIABLE_FPS;
858       /* see if we have a max-framerate */
859       gst_structure_get_fraction (structure, "max-framerate", &fps_n, &fps_d);
860     }
861     info->fps_n = fps_n;
862     info->fps_d = fps_d;
863   } else {
864     /* unspecified is variable framerate */
865     info->fps_n = 0;
866     info->fps_d = 1;
867   }
868
869   if ((s = gst_structure_get_string (structure, "interlace-mode")))
870     info->interlace_mode = gst_interlace_mode_from_string (s);
871   else
872     info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
873
874   if (gst_structure_get_int (structure, "views", &views))
875     info->views = views;
876   else
877     info->views = 1;
878
879   if ((s = gst_structure_get_string (structure, "chroma-site")))
880     info->chroma_site = gst_video_chroma_from_string (s);
881   else
882     info->chroma_site = GST_VIDEO_CHROMA_SITE_UNKNOWN;
883
884   if ((s = gst_structure_get_string (structure, "colorimetry")))
885     gst_video_colorimetry_from_string (&info->colorimetry, s);
886   else
887     memset (&info->colorimetry, 0, sizeof (GstVideoColorimetry));
888
889   if (gst_structure_get_fraction (structure, "pixel-aspect-ratio",
890           &par_n, &par_d)) {
891     info->par_n = par_n;
892     info->par_d = par_d;
893   } else {
894     info->par_n = 1;
895     info->par_d = 1;
896   }
897   return TRUE;
898
899   /* ERROR */
900 wrong_name:
901   {
902     GST_ERROR ("wrong name '%s', expected video/x-raw",
903         gst_structure_get_name (structure));
904     return FALSE;
905   }
906 no_format:
907   {
908     GST_ERROR ("no format given");
909     return FALSE;
910   }
911 unknown_format:
912   {
913     GST_ERROR ("unknown format '%s' given", s);
914     return FALSE;
915   }
916 no_width:
917   {
918     GST_ERROR ("no width property given");
919     return FALSE;
920   }
921 no_height:
922   {
923     GST_ERROR ("no height property given");
924     return FALSE;
925   }
926 }
927
928 /**
929  * gst_video_info_to_caps:
930  * @info: a #GstVideoInfo
931  *
932  * Convert the values of @info into a #GstCaps.
933  *
934  * Returns: a new #GstCaps containing the info of @info.
935  */
936 GstCaps *
937 gst_video_info_to_caps (GstVideoInfo * info)
938 {
939   GstCaps *caps;
940   const gchar *format;
941
942   g_return_val_if_fail (info != NULL, NULL);
943   g_return_val_if_fail (info->finfo != NULL, NULL);
944   g_return_val_if_fail (info->finfo->format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
945
946   format = gst_video_format_to_string (info->finfo->format);
947   g_return_val_if_fail (format != NULL, NULL);
948
949   caps = gst_caps_new_simple ("video/x-raw",
950       "format", G_TYPE_STRING, format,
951       "width", G_TYPE_INT, info->width,
952       "height", G_TYPE_INT, info->height,
953       "pixel-aspect-ratio", GST_TYPE_FRACTION, info->par_n, info->par_d, NULL);
954
955   gst_caps_set_simple (caps, "interlace-mode", G_TYPE_STRING,
956       gst_interlace_mode_to_string (info->interlace_mode), NULL);
957
958   if (info->chroma_site != GST_VIDEO_CHROMA_SITE_UNKNOWN)
959     gst_caps_set_simple (caps, "chroma-site", G_TYPE_STRING,
960         gst_video_chroma_to_string (info->chroma_site), NULL);
961
962   gst_video_caps_set_colorimetry (caps, &info->colorimetry);
963
964   if (info->views > 1)
965     gst_caps_set_simple (caps, "views", G_TYPE_INT, info->views, NULL);
966
967   if (info->flags & GST_VIDEO_FLAG_VARIABLE_FPS && info->fps_n != 0) {
968     /* variable fps with a max-framerate */
969     gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, 0, 1,
970         "max-framerate", GST_TYPE_FRACTION, info->fps_n, info->fps_d, NULL);
971   } else {
972     /* no variable fps or no max-framerate */
973     gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION,
974         info->fps_n, info->fps_d, NULL);
975   }
976
977   return caps;
978 }
979
980 /**
981  * gst_video_frame_map_id:
982  * @frame: pointer to #GstVideoFrame
983  * @info: a #GstVideoInfo
984  * @buffer: the buffer to map
985  * @id: the frame id to map
986  * @flags: #GstMapFlags
987  *
988  * Use @info and @buffer to fill in the values of @frame with the video frame
989  * information of frame @id.
990  *
991  * When @id is -1, the default frame is mapped. When @id != -1, this function
992  * will return %FALSE when there is no GstVideoMeta with that id.
993  *
994  * All video planes of @buffer will be mapped and the pointers will be set in
995  * @frame->data.
996  *
997  * Returns: %TRUE on success.
998  */
999 gboolean
1000 gst_video_frame_map_id (GstVideoFrame * frame, GstVideoInfo * info,
1001     GstBuffer * buffer, gint id, GstMapFlags flags)
1002 {
1003   GstVideoMeta *meta;
1004   guint8 *data;
1005   gsize size;
1006   gint i;
1007
1008   g_return_val_if_fail (frame != NULL, FALSE);
1009   g_return_val_if_fail (info != NULL, FALSE);
1010   g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1011
1012   if (id == -1)
1013     meta = gst_buffer_get_video_meta (buffer);
1014   else
1015     meta = gst_buffer_get_video_meta_id (buffer, id);
1016
1017   frame->buffer = buffer;
1018   frame->meta = meta;
1019
1020   if (meta) {
1021     frame->info.flags = meta->flags;
1022     frame->info.finfo = &formats[meta->format].info;
1023     frame->info.width = meta->width;
1024     frame->info.height = meta->height;
1025     frame->id = meta->id;
1026
1027     for (i = 0; i < info->finfo->n_planes; i++) {
1028       frame->data[i] =
1029           gst_video_meta_map (meta, i, &frame->info.stride[i], flags);
1030     }
1031   } else {
1032     /* no metadata, we really need to have the metadata when the id is
1033      * specified. */
1034     if (id != -1)
1035       goto no_metadata;
1036
1037     /* copy the info */
1038     frame->info = *info;
1039     frame->id = id;
1040
1041     data = gst_buffer_map (buffer, &size, NULL, flags);
1042
1043     /* do some sanity checks */
1044     if (size < info->size)
1045       goto invalid_size;
1046
1047     /* set up pointers */
1048     for (i = 0; i < info->finfo->n_planes; i++) {
1049       frame->data[i] = data + info->offset[i];
1050     }
1051   }
1052   return TRUE;
1053
1054   /* ERRORS */
1055 no_metadata:
1056   {
1057     GST_ERROR ("no GstVideoMeta for id %d", id);
1058     return FALSE;
1059   }
1060 invalid_size:
1061   {
1062     GST_ERROR ("invalid buffer size %" G_GSIZE_FORMAT " < %" G_GSIZE_FORMAT,
1063         size, info->size);
1064     gst_buffer_unmap (buffer, data, size);
1065     return FALSE;
1066   }
1067 }
1068
1069 /**
1070  * gst_video_frame_map:
1071  * @frame: pointer to #GstVideoFrame
1072  * @info: a #GstVideoInfo
1073  * @buffer: the buffer to map
1074  * @flags: #GstMapFlags
1075  *
1076  * Use @info and @buffer to fill in the values of @frame.
1077  *
1078  * All video planes of @buffer will be mapped and the pointers will be set in
1079  * @frame->data.
1080  *
1081  * Returns: %TRUE on success.
1082  */
1083 gboolean
1084 gst_video_frame_map (GstVideoFrame * frame, GstVideoInfo * info,
1085     GstBuffer * buffer, GstMapFlags flags)
1086 {
1087   return gst_video_frame_map_id (frame, info, buffer, -1, flags);
1088 }
1089
1090 /**
1091  * gst_video_frame_unmap:
1092  * @frame: a #GstVideoFrame
1093  *
1094  * Unmap the memory previously mapped with gst_video_frame_map.
1095  */
1096 void
1097 gst_video_frame_unmap (GstVideoFrame * frame)
1098 {
1099   GstBuffer *buffer;
1100   GstVideoMeta *meta;
1101   gint i;
1102
1103   g_return_if_fail (frame != NULL);
1104
1105   buffer = frame->buffer;
1106   meta = frame->meta;
1107
1108   if (meta) {
1109     for (i = 0; i < frame->info.finfo->n_planes; i++) {
1110       gst_video_meta_unmap (meta, i, frame->data[i]);
1111     }
1112   } else {
1113     guint8 *data;
1114
1115     data = frame->data[0];
1116     data -= frame->info.offset[0];
1117     gst_buffer_unmap (buffer, data, -1);
1118   }
1119 }
1120
1121 /**
1122  * gst_video_frame_copy:
1123  * @dest: a #GstVideoFrame
1124  * @src: a #GstVideoFrame
1125  *
1126  * Copy the contents from @src to @dest.
1127  *
1128  * Returns: TRUE if the contents could be copied.
1129  */
1130 gboolean
1131 gst_video_frame_copy (GstVideoFrame * dest, const GstVideoFrame * src)
1132 {
1133   guint i, n_planes;
1134   const GstVideoInfo *sinfo;
1135   GstVideoInfo *dinfo;
1136
1137   sinfo = &src->info;
1138   dinfo = &dest->info;
1139
1140   g_return_val_if_fail (dinfo->finfo->format == sinfo->finfo->format, FALSE);
1141   g_return_val_if_fail (dinfo->width == sinfo->width
1142       && dinfo->height == sinfo->height, FALSE);
1143
1144   n_planes = dinfo->finfo->n_planes;
1145
1146   for (i = 0; i < n_planes; i++) {
1147     guint w, h, j;
1148     guint8 *sp, *dp;
1149     gint ss, ds;
1150
1151     sp = src->data[i];
1152     dp = dest->data[i];
1153
1154     ss = sinfo->stride[i];
1155     ds = dinfo->stride[i];
1156
1157     w = MIN (ABS (ss), ABS (ds));
1158     h = GST_VIDEO_FRAME_COMP_HEIGHT (dest, i);
1159
1160     GST_DEBUG ("w %d h %d", w, h);
1161
1162     for (j = 0; j < h; j++) {
1163       memcpy (dp, sp, w);
1164       dp += ds;
1165       sp += ss;
1166     }
1167   }
1168   return TRUE;
1169 }
1170
1171 static int
1172 fill_planes (GstVideoInfo * info)
1173 {
1174   gint width, height;
1175
1176   width = info->width;
1177   height = info->height;
1178
1179   switch (info->finfo->format) {
1180     case GST_VIDEO_FORMAT_YUY2:
1181     case GST_VIDEO_FORMAT_YVYU:
1182     case GST_VIDEO_FORMAT_UYVY:
1183       info->stride[0] = GST_ROUND_UP_4 (width * 2);
1184       info->offset[0] = 0;
1185       info->size = info->stride[0] * height;
1186       break;
1187     case GST_VIDEO_FORMAT_AYUV:
1188     case GST_VIDEO_FORMAT_RGBx:
1189     case GST_VIDEO_FORMAT_RGBA:
1190     case GST_VIDEO_FORMAT_BGRx:
1191     case GST_VIDEO_FORMAT_BGRA:
1192     case GST_VIDEO_FORMAT_xRGB:
1193     case GST_VIDEO_FORMAT_ARGB:
1194     case GST_VIDEO_FORMAT_xBGR:
1195     case GST_VIDEO_FORMAT_ABGR:
1196     case GST_VIDEO_FORMAT_r210:
1197       info->stride[0] = width * 4;
1198       info->offset[0] = 0;
1199       info->size = info->stride[0] * height;
1200       break;
1201     case GST_VIDEO_FORMAT_RGB16:
1202     case GST_VIDEO_FORMAT_BGR16:
1203     case GST_VIDEO_FORMAT_RGB15:
1204     case GST_VIDEO_FORMAT_BGR15:
1205       info->stride[0] = GST_ROUND_UP_4 (width * 2);
1206       info->offset[0] = 0;
1207       info->size = info->stride[0] * height;
1208       break;
1209     case GST_VIDEO_FORMAT_RGB:
1210     case GST_VIDEO_FORMAT_BGR:
1211     case GST_VIDEO_FORMAT_v308:
1212       info->stride[0] = GST_ROUND_UP_4 (width * 3);
1213       info->offset[0] = 0;
1214       info->size = info->stride[0] * height;
1215       break;
1216     case GST_VIDEO_FORMAT_v210:
1217       info->stride[0] = ((width + 47) / 48) * 128;
1218       info->offset[0] = 0;
1219       info->size = info->stride[0] * height;
1220       break;
1221     case GST_VIDEO_FORMAT_v216:
1222       info->stride[0] = GST_ROUND_UP_8 (width * 4);
1223       info->offset[0] = 0;
1224       info->size = info->stride[0] * height;
1225       break;
1226     case GST_VIDEO_FORMAT_GRAY8:
1227     case GST_VIDEO_FORMAT_Y800:
1228       info->stride[0] = GST_ROUND_UP_4 (width);
1229       info->offset[0] = 0;
1230       info->size = info->stride[0] * height;
1231       break;
1232     case GST_VIDEO_FORMAT_GRAY16_BE:
1233     case GST_VIDEO_FORMAT_GRAY16_LE:
1234     case GST_VIDEO_FORMAT_Y16:
1235       info->stride[0] = GST_ROUND_UP_4 (width * 2);
1236       info->offset[0] = 0;
1237       info->size = info->stride[0] * height;
1238       break;
1239     case GST_VIDEO_FORMAT_UYVP:
1240       info->stride[0] = GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4);
1241       info->offset[0] = 0;
1242       info->size = info->stride[0] * height;
1243       break;
1244     case GST_VIDEO_FORMAT_RGB8_PALETTED:
1245       info->stride[0] = GST_ROUND_UP_4 (width);
1246       info->offset[0] = 0;
1247       info->size = info->stride[0] * height;
1248       break;
1249     case GST_VIDEO_FORMAT_IYU1:
1250       info->stride[0] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) +
1251           GST_ROUND_UP_4 (width) / 2);
1252       info->offset[0] = 0;
1253       info->size = info->stride[0] * height;
1254       break;
1255     case GST_VIDEO_FORMAT_ARGB64:
1256     case GST_VIDEO_FORMAT_AYUV64:
1257       info->stride[0] = width * 8;
1258       info->offset[0] = 0;
1259       info->size = info->stride[0] * height;
1260       break;
1261     case GST_VIDEO_FORMAT_I420:
1262       info->stride[0] = GST_ROUND_UP_4 (width);
1263       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1264       info->stride[2] = info->stride[1];
1265       info->offset[0] = 0;
1266       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1267       info->offset[2] = info->offset[1] +
1268           info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
1269       info->size = info->offset[2] +
1270           info->stride[2] * (GST_ROUND_UP_2 (height) / 2);
1271       break;
1272     case GST_VIDEO_FORMAT_YV12:        /* same as I420, but plane 1+2 swapped */
1273       info->stride[0] = GST_ROUND_UP_4 (width);
1274       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1275       info->stride[2] = info->stride[1];
1276       info->offset[0] = 0;
1277       info->offset[2] = info->stride[0] * GST_ROUND_UP_2 (height);
1278       info->offset[1] = info->offset[2] +
1279           info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
1280       info->size = info->offset[1] +
1281           info->stride[2] * (GST_ROUND_UP_2 (height) / 2);
1282       break;
1283     case GST_VIDEO_FORMAT_Y41B:
1284       info->stride[0] = GST_ROUND_UP_4 (width);
1285       info->stride[1] = GST_ROUND_UP_16 (width) / 4;
1286       info->stride[2] = info->stride[1];
1287       info->offset[0] = 0;
1288       info->offset[1] = info->stride[0] * height;
1289       info->offset[2] = info->offset[1] + info->stride[1] * height;
1290       /* simplification of ROUNDUP4(w)*h + 2*((ROUNDUP16(w)/4)*h */
1291       info->size = (info->stride[0] + (GST_ROUND_UP_16 (width) / 2)) * height;
1292       break;
1293     case GST_VIDEO_FORMAT_Y42B:
1294       info->stride[0] = GST_ROUND_UP_4 (width);
1295       info->stride[1] = GST_ROUND_UP_8 (width) / 2;
1296       info->stride[2] = info->stride[1];
1297       info->offset[0] = 0;
1298       info->offset[1] = info->stride[0] * height;
1299       info->offset[2] = info->offset[1] + info->stride[1] * height;
1300       /* simplification of ROUNDUP4(w)*h + 2*(ROUNDUP8(w)/2)*h */
1301       info->size = (info->stride[0] + GST_ROUND_UP_8 (width)) * height;
1302       break;
1303     case GST_VIDEO_FORMAT_Y444:
1304       info->stride[0] = GST_ROUND_UP_4 (width);
1305       info->stride[1] = info->stride[0];
1306       info->stride[2] = info->stride[0];
1307       info->offset[0] = 0;
1308       info->offset[1] = info->stride[0] * height;
1309       info->offset[2] = info->offset[1] * 2;
1310       info->size = info->stride[0] * height * 3;
1311       break;
1312     case GST_VIDEO_FORMAT_NV12:
1313     case GST_VIDEO_FORMAT_NV21:
1314       info->stride[0] = GST_ROUND_UP_4 (width);
1315       info->stride[1] = info->stride[0];
1316       info->offset[0] = 0;
1317       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1318       info->size = info->stride[0] * GST_ROUND_UP_2 (height) * 3 / 2;
1319       break;
1320     case GST_VIDEO_FORMAT_A420:
1321       info->stride[0] = GST_ROUND_UP_4 (width);
1322       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1323       info->stride[2] = info->stride[1];
1324       info->stride[3] = info->stride[0];
1325       info->offset[0] = 0;
1326       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1327       info->offset[2] = info->offset[1] +
1328           info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
1329       info->offset[3] = info->offset[2] +
1330           info->stride[2] * (GST_ROUND_UP_2 (height) / 2);
1331       info->size = info->offset[3] + info->stride[0];
1332       break;
1333     case GST_VIDEO_FORMAT_YUV9:
1334       info->stride[0] = GST_ROUND_UP_4 (width);
1335       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
1336       info->stride[2] = info->stride[1];
1337       info->offset[0] = 0;
1338       info->offset[1] = info->stride[0] * height;
1339       info->offset[2] = info->offset[1] +
1340           info->stride[1] * (GST_ROUND_UP_4 (height) / 4);
1341       info->size = info->offset[2] +
1342           info->stride[2] * (GST_ROUND_UP_4 (height) / 4);
1343       break;
1344     case GST_VIDEO_FORMAT_YVU9:
1345       info->stride[0] = GST_ROUND_UP_4 (width);
1346       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
1347       info->stride[2] = info->stride[1];
1348       info->offset[0] = 0;
1349       info->offset[2] = info->stride[0] * height;
1350       info->offset[1] = info->offset[2] +
1351           info->stride[1] * (GST_ROUND_UP_4 (height) / 4);
1352       info->size = info->offset[2] +
1353           info->stride[2] * (GST_ROUND_UP_4 (height) / 4);
1354       break;
1355     case GST_VIDEO_FORMAT_UNKNOWN:
1356       GST_ERROR ("invalid format");
1357       g_warning ("invalid format");
1358       break;
1359   }
1360   return 0;
1361 }
1362
1363 /**
1364  * gst_video_format_convert:
1365  * @info: a #GstVideoInfo
1366  * @src_format: #GstFormat of the @src_value
1367  * @src_value: value to convert
1368  * @dest_format: #GstFormat of the @dest_value
1369  * @dest_value: pointer to destination value
1370  *
1371  * Converts among various #GstFormat types.  This function handles
1372  * GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT.  For
1373  * raw video, GST_FORMAT_DEFAULT corresponds to video frames.  This
1374  * function can be used to handle pad queries of the type GST_QUERY_CONVERT.
1375  *
1376  * Since: 0.10.16
1377  *
1378  * Returns: TRUE if the conversion was successful.
1379  */
1380 gboolean
1381 gst_video_info_convert (GstVideoInfo * info,
1382     GstFormat src_format, gint64 src_value,
1383     GstFormat dest_format, gint64 * dest_value)
1384 {
1385   gboolean ret = FALSE;
1386   int size, fps_n, fps_d;
1387
1388   g_return_val_if_fail (info != NULL, 0);
1389   g_return_val_if_fail (info->finfo != NULL, 0);
1390   g_return_val_if_fail (info->finfo->format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1391   g_return_val_if_fail (info->size > 0, 0);
1392
1393   size = info->size;
1394   fps_n = info->fps_n;
1395   fps_d = info->fps_d;
1396
1397   GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s to %s",
1398       src_value, gst_format_get_name (src_format),
1399       gst_format_get_name (dest_format));
1400
1401   if (src_format == dest_format) {
1402     *dest_value = src_value;
1403     ret = TRUE;
1404     goto done;
1405   }
1406
1407   if (src_value == -1) {
1408     *dest_value = -1;
1409     ret = TRUE;
1410     goto done;
1411   }
1412
1413   /* bytes to frames */
1414   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_DEFAULT) {
1415     if (size != 0) {
1416       *dest_value = gst_util_uint64_scale_int (src_value, 1, size);
1417     } else {
1418       GST_ERROR ("blocksize is 0");
1419       *dest_value = 0;
1420     }
1421     ret = TRUE;
1422     goto done;
1423   }
1424
1425   /* frames to bytes */
1426   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_BYTES) {
1427     *dest_value = gst_util_uint64_scale_int (src_value, size, 1);
1428     ret = TRUE;
1429     goto done;
1430   }
1431
1432   /* time to frames */
1433   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_DEFAULT) {
1434     if (fps_d != 0) {
1435       *dest_value = gst_util_uint64_scale (src_value,
1436           fps_n, GST_SECOND * fps_d);
1437     } else {
1438       GST_ERROR ("framerate denominator is 0");
1439       *dest_value = 0;
1440     }
1441     ret = TRUE;
1442     goto done;
1443   }
1444
1445   /* frames to time */
1446   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) {
1447     if (fps_n != 0) {
1448       *dest_value = gst_util_uint64_scale (src_value,
1449           GST_SECOND * fps_d, fps_n);
1450     } else {
1451       GST_ERROR ("framerate numerator is 0");
1452       *dest_value = 0;
1453     }
1454     ret = TRUE;
1455     goto done;
1456   }
1457
1458   /* time to bytes */
1459   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
1460     if (fps_d != 0) {
1461       *dest_value = gst_util_uint64_scale (src_value,
1462           fps_n * size, GST_SECOND * fps_d);
1463     } else {
1464       GST_ERROR ("framerate denominator is 0");
1465       *dest_value = 0;
1466     }
1467     ret = TRUE;
1468     goto done;
1469   }
1470
1471   /* bytes to time */
1472   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
1473     if (fps_n != 0 && size != 0) {
1474       *dest_value = gst_util_uint64_scale (src_value,
1475           GST_SECOND * fps_d, fps_n * size);
1476     } else {
1477       GST_ERROR ("framerate denominator and/or blocksize is 0");
1478       *dest_value = 0;
1479     }
1480     ret = TRUE;
1481   }
1482
1483 done:
1484
1485   GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, ret, *dest_value);
1486
1487   return ret;
1488 }
1489
1490 #define GST_VIDEO_EVENT_STILL_STATE_NAME "GstEventStillFrame"
1491
1492 /**
1493  * gst_video_event_new_still_frame:
1494  * @in_still: boolean value for the still-frame state of the event.
1495  *
1496  * Creates a new Still Frame event. If @in_still is %TRUE, then the event
1497  * represents the start of a still frame sequence. If it is %FALSE, then
1498  * the event ends a still frame sequence.
1499  *
1500  * To parse an event created by gst_video_event_new_still_frame() use
1501  * gst_video_event_parse_still_frame().
1502  *
1503  * Returns: The new GstEvent
1504  * Since: 0.10.26
1505  */
1506 GstEvent *
1507 gst_video_event_new_still_frame (gboolean in_still)
1508 {
1509   GstEvent *still_event;
1510   GstStructure *s;
1511
1512   s = gst_structure_new (GST_VIDEO_EVENT_STILL_STATE_NAME,
1513       "still-state", G_TYPE_BOOLEAN, in_still, NULL);
1514   still_event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1515
1516   return still_event;
1517 }
1518
1519 /**
1520  * gst_video_event_parse_still_frame:
1521  * @event: A #GstEvent to parse
1522  * @in_still: A boolean to receive the still-frame status from the event, or NULL
1523  *
1524  * Parse a #GstEvent, identify if it is a Still Frame event, and
1525  * return the still-frame state from the event if it is.
1526  * If the event represents the start of a still frame, the in_still
1527  * variable will be set to TRUE, otherwise FALSE. It is OK to pass NULL for the
1528  * in_still variable order to just check whether the event is a valid still-frame
1529  * event.
1530  *
1531  * Create a still frame event using gst_video_event_new_still_frame()
1532  *
1533  * Returns: %TRUE if the event is a valid still-frame event. %FALSE if not
1534  * Since: 0.10.26
1535  */
1536 gboolean
1537 gst_video_event_parse_still_frame (GstEvent * event, gboolean * in_still)
1538 {
1539   const GstStructure *s;
1540   gboolean ev_still_state;
1541
1542   g_return_val_if_fail (event != NULL, FALSE);
1543
1544   if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM)
1545     return FALSE;               /* Not a still frame event */
1546
1547   s = gst_event_get_structure (event);
1548   if (s == NULL
1549       || !gst_structure_has_name (s, GST_VIDEO_EVENT_STILL_STATE_NAME))
1550     return FALSE;               /* Not a still frame event */
1551   if (!gst_structure_get_boolean (s, "still-state", &ev_still_state))
1552     return FALSE;               /* Not a still frame event */
1553   if (in_still)
1554     *in_still = ev_still_state;
1555   return TRUE;
1556 }
1557
1558 /**
1559  * gst_video_parse_caps_palette:
1560  * @caps: #GstCaps to parse
1561  *
1562  * Returns the palette data from the caps as a #GstBuffer. For
1563  * #GST_VIDEO_FORMAT_RGB8_PALETTED this is containing 256 #guint32
1564  * values, each containing ARGB colors in native endianness.
1565  *
1566  * Returns: a #GstBuffer containing the palette data. Unref after usage.
1567  * Since: 0.10.32
1568  */
1569 GstBuffer *
1570 gst_video_parse_caps_palette (GstCaps * caps)
1571 {
1572   GstStructure *s;
1573   const GValue *p_v;
1574   GstBuffer *p;
1575
1576   if (!gst_caps_is_fixed (caps))
1577     return NULL;
1578
1579   s = gst_caps_get_structure (caps, 0);
1580
1581   p_v = gst_structure_get_value (s, "palette_data");
1582   if (!p_v || !GST_VALUE_HOLDS_BUFFER (p_v))
1583     return NULL;
1584
1585   p = g_value_dup_boxed (p_v);
1586
1587   return p;
1588 }
1589
1590 #define GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME "GstForceKeyUnit"
1591
1592 /**
1593  * gst_video_event_new_downstream_force_key_unit:
1594  * @timestamp: the timestamp of the buffer that starts a new key unit
1595  * @stream_time: the stream_time of the buffer that starts a new key unit
1596  * @running_time: the running_time of the buffer that starts a new key unit
1597  * @all_headers: %TRUE to produce headers when starting a new key unit
1598  * @count: integer that can be used to number key units
1599  *
1600  * Creates a new downstream force key unit event. A downstream force key unit
1601  * event can be sent down the pipeline to request downstream elements to produce
1602  * a key unit. A downstream force key unit event must also be sent when handling
1603  * an upstream force key unit event to notify downstream that the latter has been
1604  * handled.
1605  *
1606  * To parse an event created by gst_video_event_new_downstream_force_key_unit() use
1607  * gst_video_event_parse_downstream_force_key_unit().
1608  *
1609  * Returns: The new GstEvent
1610  * Since: 0.10.36
1611  */
1612 GstEvent *
1613 gst_video_event_new_downstream_force_key_unit (GstClockTime timestamp,
1614     GstClockTime stream_time, GstClockTime running_time, gboolean all_headers,
1615     guint count)
1616 {
1617   GstEvent *force_key_unit_event;
1618   GstStructure *s;
1619
1620   s = gst_structure_new (GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME,
1621       "timestamp", G_TYPE_UINT64, timestamp,
1622       "stream-time", G_TYPE_UINT64, stream_time,
1623       "running-time", G_TYPE_UINT64, running_time,
1624       "all-headers", G_TYPE_BOOLEAN, all_headers,
1625       "count", G_TYPE_UINT, count, NULL);
1626   force_key_unit_event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1627
1628   return force_key_unit_event;
1629 }
1630
1631 /**
1632  * gst_video_event_new_upstream_force_key_unit:
1633  * @running_time: the running_time at which a new key unit should be produced
1634  * @all_headers: %TRUE to produce headers when starting a new key unit
1635  * @count: integer that can be used to number key units
1636  *
1637  * Creates a new upstream force key unit event. An upstream force key unit event
1638  * can be sent to request upstream elements to produce a key unit. 
1639  *
1640  * @running_time can be set to request a new key unit at a specific
1641  * running_time. If set to GST_CLOCK_TIME_NONE, upstream elements will produce a
1642  * new key unit as soon as possible.
1643  *
1644  * To parse an event created by gst_video_event_new_downstream_force_key_unit() use
1645  * gst_video_event_parse_downstream_force_key_unit().
1646  *
1647  * Returns: The new GstEvent
1648  * Since: 0.10.36
1649  */
1650 GstEvent *
1651 gst_video_event_new_upstream_force_key_unit (GstClockTime running_time,
1652     gboolean all_headers, guint count)
1653 {
1654   GstEvent *force_key_unit_event;
1655   GstStructure *s;
1656
1657   s = gst_structure_new (GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME,
1658       "running-time", GST_TYPE_CLOCK_TIME, running_time,
1659       "all-headers", G_TYPE_BOOLEAN, all_headers,
1660       "count", G_TYPE_UINT, count, NULL);
1661   force_key_unit_event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, s);
1662
1663   return force_key_unit_event;
1664 }
1665
1666 /**
1667  * gst_video_event_is_force_key_unit:
1668  * @event: A #GstEvent to check
1669  *
1670  * Checks if an event is a force key unit event. Returns true for both upstream
1671  * and downstream force key unit events.
1672  *
1673  * Returns: %TRUE if the event is a valid force key unit event
1674  * Since: 0.10.36
1675  */
1676 gboolean
1677 gst_video_event_is_force_key_unit (GstEvent * event)
1678 {
1679   const GstStructure *s;
1680
1681   g_return_val_if_fail (event != NULL, FALSE);
1682
1683   if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM &&
1684       GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_UPSTREAM)
1685     return FALSE;               /* Not a force key unit event */
1686
1687   s = gst_event_get_structure (event);
1688   if (s == NULL
1689       || !gst_structure_has_name (s, GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME))
1690     return FALSE;
1691
1692   return TRUE;
1693 }
1694
1695 /**
1696  * gst_video_event_parse_downstream_force_key_unit:
1697  * @event: A #GstEvent to parse
1698  * @timestamp: (out): A pointer to the timestamp in the event
1699  * @stream_time: (out): A pointer to the stream-time in the event
1700  * @running_time: (out): A pointer to the running-time in the event
1701  * @all_headers: (out): A pointer to the all_headers flag in the event
1702  * @count: (out): A pointer to the count field of the event
1703  *
1704  * Get timestamp, stream-time, running-time, all-headers and count in the force
1705  * key unit event. See gst_video_event_new_downstream_force_key_unit() for a
1706  * full description of the downstream force key unit event.
1707  *
1708  * Returns: %TRUE if the event is a valid downstream force key unit event.
1709  * Since: 0.10.36
1710  */
1711 gboolean
1712 gst_video_event_parse_downstream_force_key_unit (GstEvent * event,
1713     GstClockTime * timestamp, GstClockTime * stream_time,
1714     GstClockTime * running_time, gboolean * all_headers, guint * count)
1715 {
1716   const GstStructure *s;
1717   GstClockTime ev_timestamp, ev_stream_time, ev_running_time;
1718   gboolean ev_all_headers;
1719   guint ev_count;
1720
1721   g_return_val_if_fail (event != NULL, FALSE);
1722
1723   if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM)
1724     return FALSE;               /* Not a force key unit event */
1725
1726   s = gst_event_get_structure (event);
1727   if (s == NULL
1728       || !gst_structure_has_name (s, GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME))
1729     return FALSE;
1730
1731   if (!gst_structure_get_clock_time (s, "timestamp", &ev_timestamp))
1732     return FALSE;               /* Not a force key unit event */
1733   if (!gst_structure_get_clock_time (s, "stream-time", &ev_stream_time))
1734     return FALSE;               /* Not a force key unit event */
1735   if (!gst_structure_get_clock_time (s, "running-time", &ev_running_time))
1736     return FALSE;               /* Not a force key unit event */
1737   if (!gst_structure_get_boolean (s, "all-headers", &ev_all_headers))
1738     return FALSE;               /* Not a force key unit event */
1739   if (!gst_structure_get_uint (s, "count", &ev_count))
1740     return FALSE;               /* Not a force key unit event */
1741
1742   if (timestamp)
1743     *timestamp = ev_timestamp;
1744
1745   if (stream_time)
1746     *stream_time = ev_stream_time;
1747
1748   if (running_time)
1749     *running_time = ev_running_time;
1750
1751   if (all_headers)
1752     *all_headers = ev_all_headers;
1753
1754   if (count)
1755     *count = ev_count;
1756
1757   return TRUE;
1758 }
1759
1760 /**
1761  * gst_video_event_parse_upstream_force_key_unit:
1762  * @event: A #GstEvent to parse
1763  * @running_time: (out): A pointer to the running_time in the event
1764  * @all_headers: (out): A pointer to the all_headers flag in the event
1765  * @count: (out): A pointer to the count field in the event
1766  *
1767  * Get running-time, all-headers and count in the force key unit event. See
1768  * gst_video_event_new_upstream_force_key_unit() for a full description of the
1769  * upstream force key unit event.
1770  *
1771  * Create an upstream force key unit event using  gst_video_event_new_upstream_force_key_unit()
1772  *
1773  * Returns: %TRUE if the event is a valid upstream force-key-unit event. %FALSE if not
1774  * Since: 0.10.36
1775  */
1776 gboolean
1777 gst_video_event_parse_upstream_force_key_unit (GstEvent * event,
1778     GstClockTime * running_time, gboolean * all_headers, guint * count)
1779 {
1780   const GstStructure *s;
1781   GstClockTime ev_running_time;
1782   gboolean ev_all_headers;
1783   guint ev_count;
1784
1785   g_return_val_if_fail (event != NULL, FALSE);
1786
1787   if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_UPSTREAM)
1788     return FALSE;               /* Not a force key unit event */
1789
1790   s = gst_event_get_structure (event);
1791   if (s == NULL
1792       || !gst_structure_has_name (s, GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME))
1793     return FALSE;
1794
1795   if (!gst_structure_get_clock_time (s, "running-time", &ev_running_time))
1796     return FALSE;               /* Not a force key unit event */
1797   if (!gst_structure_get_boolean (s, "all-headers", &ev_all_headers))
1798     return FALSE;               /* Not a force key unit event */
1799   if (!gst_structure_get_uint (s, "count", &ev_count))
1800     return FALSE;               /* Not a force key unit event */
1801
1802   if (running_time)
1803     *running_time = ev_running_time;
1804
1805   if (all_headers)
1806     *all_headers = ev_all_headers;
1807
1808   if (count)
1809     *count = ev_count;
1810
1811   return TRUE;
1812 }