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