video.{c,h}: Add support for RGB and BGR with 15 and 16 bits.
[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 "video.h"
27
28 /**
29  * SECTION:gstvideo
30  * @short_description: Support library for video operations
31  *
32  * <refsect2>
33  * <para>
34  * This library contains some helper functions and includes the 
35  * videosink and videofilter base classes.
36  * </para>
37  * </refsect2>
38  */
39
40 static GstVideoFormat gst_video_format_from_rgb32_masks (int red_mask,
41     int green_mask, int blue_mask);
42 static GstVideoFormat gst_video_format_from_rgba32_masks (int red_mask,
43     int green_mask, int blue_mask, int alpha_mask);
44 static GstVideoFormat gst_video_format_from_rgb24_masks (int red_mask,
45     int green_mask, int blue_mask);
46 static GstVideoFormat gst_video_format_from_rgb16_masks (int red_mask,
47     int green_mask, int blue_mask);
48
49
50 /**
51  * gst_video_frame_rate:
52  * @pad: pointer to a #GstPad
53  *
54  * A convenience function to retrieve a GValue holding the framerate
55  * from the caps on a pad.
56  * 
57  * The pad needs to have negotiated caps containing a framerate property.
58  *
59  * Returns: NULL if the pad has no configured caps or the configured caps
60  * do not contain a framerate.
61  *
62  */
63 const GValue *
64 gst_video_frame_rate (GstPad * pad)
65 {
66   const GValue *fps;
67   gchar *fps_string;
68
69   const GstCaps *caps = NULL;
70   GstStructure *structure;
71
72   /* get pad caps */
73   caps = GST_PAD_CAPS (pad);
74   if (caps == NULL) {
75     g_warning ("gstvideo: failed to get caps of pad %s:%s",
76         GST_DEBUG_PAD_NAME (pad));
77     return NULL;
78   }
79
80   structure = gst_caps_get_structure (caps, 0);
81   if ((fps = gst_structure_get_value (structure, "framerate")) == NULL) {
82     g_warning ("gstvideo: failed to get framerate property of pad %s:%s",
83         GST_DEBUG_PAD_NAME (pad));
84     return NULL;
85   }
86   if (!GST_VALUE_HOLDS_FRACTION (fps)) {
87     g_warning
88         ("gstvideo: framerate property of pad %s:%s is not of type Fraction",
89         GST_DEBUG_PAD_NAME (pad));
90     return NULL;
91   }
92
93   fps_string = gst_value_serialize (fps);
94   GST_DEBUG ("Framerate request on pad %s:%s: %s",
95       GST_DEBUG_PAD_NAME (pad), fps_string);
96   g_free (fps_string);
97
98   return fps;
99 }
100
101 /**
102  * gst_video_get_size:
103  * @pad: pointer to a #GstPad
104  * @width: pointer to integer to hold pixel width of the video frames (output)
105  * @height: pointer to integer to hold pixel height of the video frames (output)
106  *
107  * Inspect the caps of the provided pad and retrieve the width and height of
108  * the video frames it is configured for.
109  * 
110  * The pad needs to have negotiated caps containing width and height properties.
111  *
112  * Returns: TRUE if the width and height could be retrieved.
113  *
114  */
115 gboolean
116 gst_video_get_size (GstPad * pad, gint * width, gint * height)
117 {
118   const GstCaps *caps = NULL;
119   GstStructure *structure;
120   gboolean ret;
121
122   g_return_val_if_fail (pad != NULL, FALSE);
123   g_return_val_if_fail (width != NULL, FALSE);
124   g_return_val_if_fail (height != NULL, FALSE);
125
126   caps = GST_PAD_CAPS (pad);
127
128   if (caps == NULL) {
129     g_warning ("gstvideo: failed to get caps of pad %s:%s",
130         GST_DEBUG_PAD_NAME (pad));
131     return FALSE;
132   }
133
134   structure = gst_caps_get_structure (caps, 0);
135   ret = gst_structure_get_int (structure, "width", width);
136   ret &= gst_structure_get_int (structure, "height", height);
137
138   if (!ret) {
139     g_warning ("gstvideo: failed to get size properties on pad %s:%s",
140         GST_DEBUG_PAD_NAME (pad));
141     return FALSE;
142   }
143
144   GST_DEBUG ("size request on pad %s:%s: %dx%d",
145       GST_DEBUG_PAD_NAME (pad), width ? *width : -1, height ? *height : -1);
146
147   return TRUE;
148 }
149
150 /**
151  * gst_video_calculate_display_ratio:
152  * @dar_n: Numerator of the calculated display_ratio
153  * @dar_d: Denominator of the calculated display_ratio
154  * @video_width: Width of the video frame in pixels
155  * @video_height: Height of the video frame in pixels
156  * @video_par_n: Numerator of the pixel aspect ratio of the input video.
157  * @video_par_d: Denominator of the pixel aspect ratio of the input video.
158  * @display_par_n: Numerator of the pixel aspect ratio of the display device
159  * @display_par_d: Denominator of the pixel aspect ratio of the display device
160  *
161  * Given the Pixel Aspect Ratio and size of an input video frame, and the 
162  * pixel aspect ratio of the intended display device, calculates the actual 
163  * display ratio the video will be rendered with.
164  *
165  * Returns: A boolean indicating success and a calculated Display Ratio in the 
166  * dar_n and dar_d parameters. 
167  * The return value is FALSE in the case of integer overflow or other error. 
168  *
169  * Since: 0.10.7
170  */
171 gboolean
172 gst_video_calculate_display_ratio (guint * dar_n, guint * dar_d,
173     guint video_width, guint video_height,
174     guint video_par_n, guint video_par_d,
175     guint display_par_n, guint display_par_d)
176 {
177   gint num, den;
178   gint tmp_n, tmp_d;
179
180   g_return_val_if_fail (dar_n != NULL, FALSE);
181   g_return_val_if_fail (dar_d != NULL, FALSE);
182
183   /* Calculate (video_width * video_par_n * display_par_d) /
184    * (video_height * video_par_d * display_par_n) */
185   if (!gst_util_fraction_multiply (video_width, video_height, video_par_n,
186           video_par_d, &tmp_n, &tmp_d))
187     goto error_overflow;
188
189   if (!gst_util_fraction_multiply (tmp_n, tmp_d, display_par_d, display_par_n,
190           &num, &den))
191     goto error_overflow;
192
193   g_return_val_if_fail (num > 0, FALSE);
194   g_return_val_if_fail (den > 0, FALSE);
195
196   *dar_n = num;
197   *dar_d = den;
198
199   return TRUE;
200 error_overflow:
201   return FALSE;
202 }
203
204 /**
205  * gst_video_format_parse_caps_interlaced:
206  * @caps: the fixed #GstCaps to parse
207  * @interlaced: whether @caps represents interlaced video or not, may be NULL (output)
208  *
209  * Extracts whether the caps represents interlaced content or not and places it
210  * in @interlaced.
211  *
212  * Since: 0.10.23
213  *
214  * Returns: TRUE if @caps was parsed correctly.
215  */
216 gboolean
217 gst_video_format_parse_caps_interlaced (GstCaps * caps, gboolean * interlaced)
218 {
219   GstStructure *structure;
220
221   if (!gst_caps_is_fixed (caps))
222     return FALSE;
223
224   structure = gst_caps_get_structure (caps, 0);
225
226   if (interlaced) {
227     if (!gst_structure_get_boolean (structure, "interlaced", interlaced))
228       *interlaced = FALSE;
229   }
230
231   return TRUE;
232 }
233
234 /**
235  * gst_video_parse_caps_color_matrix:
236  * @caps: the fixed #GstCaps to parse
237  *
238  * Extracts the color matrix used by the caps.  Possible values are
239  * "sdtv" for the standard definition color matrix (as specified in
240  * Rec. ITU-R BT.470-6) or "hdtv" for the high definition color
241  * matrix (as specified in Rec. ITU-R BT.709)
242  *
243  * Since: 0.10.29
244  *
245  * Returns: a color matrix string, or NULL if no color matrix could be
246  *     determined.
247  */
248 const char *
249 gst_video_parse_caps_color_matrix (GstCaps * caps)
250 {
251   GstStructure *structure;
252   const char *s;
253
254   if (!gst_caps_is_fixed (caps))
255     return NULL;
256
257   structure = gst_caps_get_structure (caps, 0);
258
259   s = gst_structure_get_string (structure, "color-matrix");
260   if (s)
261     return s;
262
263   if (gst_structure_has_name (structure, "video/x-raw-yuv")) {
264     return "sdtv";
265   }
266
267   return NULL;
268 }
269
270 /**
271  * gst_video_parse_caps_chroma_site:
272  * @caps: the fixed #GstCaps to parse
273  *
274  * Extracts the chroma site used by the caps.  Possible values are
275  * "mpeg2" for MPEG-2 style chroma siting (co-sited horizontally,
276  * halfway-sited vertically), "jpeg" for JPEG and Theora style
277  * chroma siting (halfway-sited both horizontally and vertically).
278  * Other chroma site values are possible, but uncommon.
279  * 
280  * When no chroma site is specified in the caps, it should be assumed
281  * to be "mpeg2".
282  *
283  * Since: 0.10.29
284  *
285  * Returns: a chroma site string, or NULL if no chroma site could be
286  *     determined.
287  */
288 const char *
289 gst_video_parse_caps_chroma_site (GstCaps * caps)
290 {
291   GstStructure *structure;
292   const char *s;
293
294   if (!gst_caps_is_fixed (caps))
295     return NULL;
296
297   structure = gst_caps_get_structure (caps, 0);
298
299   s = gst_structure_get_string (structure, "chroma-site");
300   if (s)
301     return s;
302
303   if (gst_structure_has_name (structure, "video/x-raw-yuv")) {
304     return "mpeg2";
305   }
306
307   return NULL;
308 }
309
310 /**
311  * gst_video_format_parse_caps:
312  * @caps: the #GstCaps to parse
313  * @format: the #GstVideoFormat of the video represented by @caps (output)
314  * @width: the width of the video represented by @caps, may be NULL (output)
315  * @height: the height of the video represented by @caps, may be NULL (output)
316  *
317  * Determines the #GstVideoFormat of @caps and places it in the location
318  * pointed to by @format.  Extracts the size of the video and places it
319  * in the location pointed to by @width and @height.  If @caps does not
320  * represent one of the raw video formats listed in #GstVideoFormat, the
321  * function will fail and return FALSE.
322  *
323  * Since: 0.10.16
324  *
325  * Returns: TRUE if @caps was parsed correctly.
326  */
327 gboolean
328 gst_video_format_parse_caps (GstCaps * caps, GstVideoFormat * format,
329     int *width, int *height)
330 {
331   GstStructure *structure;
332   gboolean ok = TRUE;
333
334   if (!gst_caps_is_fixed (caps))
335     return FALSE;
336
337   structure = gst_caps_get_structure (caps, 0);
338
339   if (format) {
340     if (gst_structure_has_name (structure, "video/x-raw-yuv")) {
341       guint32 fourcc;
342
343       ok &= gst_structure_get_fourcc (structure, "format", &fourcc);
344
345       *format = gst_video_format_from_fourcc (fourcc);
346       if (*format == GST_VIDEO_FORMAT_UNKNOWN) {
347         ok = FALSE;
348       }
349     } else if (gst_structure_has_name (structure, "video/x-raw-rgb")) {
350       int depth;
351       int bpp;
352       int endianness;
353       int red_mask;
354       int green_mask;
355       int blue_mask;
356       int alpha_mask;
357       gboolean have_alpha;
358
359       ok &= gst_structure_get_int (structure, "depth", &depth);
360       ok &= gst_structure_get_int (structure, "bpp", &bpp);
361       ok &= gst_structure_get_int (structure, "endianness", &endianness);
362       ok &= gst_structure_get_int (structure, "red_mask", &red_mask);
363       ok &= gst_structure_get_int (structure, "green_mask", &green_mask);
364       ok &= gst_structure_get_int (structure, "blue_mask", &blue_mask);
365       have_alpha = gst_structure_get_int (structure, "alpha_mask", &alpha_mask);
366
367       if (depth == 24 && bpp == 32 && endianness == G_BIG_ENDIAN) {
368         *format = gst_video_format_from_rgb32_masks (red_mask, green_mask,
369             blue_mask);
370         if (*format == GST_VIDEO_FORMAT_UNKNOWN) {
371           ok = FALSE;
372         }
373       } else if (depth == 32 && bpp == 32 && endianness == G_BIG_ENDIAN &&
374           have_alpha) {
375         *format = gst_video_format_from_rgba32_masks (red_mask, green_mask,
376             blue_mask, alpha_mask);
377         if (*format == GST_VIDEO_FORMAT_UNKNOWN) {
378           ok = FALSE;
379         }
380       } else if (depth == 24 && bpp == 24 && endianness == G_BIG_ENDIAN) {
381         *format = gst_video_format_from_rgb24_masks (red_mask, green_mask,
382             blue_mask);
383         if (*format == GST_VIDEO_FORMAT_UNKNOWN) {
384           ok = FALSE;
385         }
386       } else if ((depth == 15 || depth == 16) && bpp == 16 &&
387           endianness == G_BIG_ENDIAN) {
388         *format = gst_video_format_from_rgb16_masks (red_mask, green_mask,
389             blue_mask);
390         if (*format == GST_VIDEO_FORMAT_UNKNOWN) {
391           ok = FALSE;
392         }
393       } else {
394         ok = FALSE;
395       }
396     } else if (gst_structure_has_name (structure, "video/x-raw-gray")) {
397       int depth;
398       int bpp;
399       int endianness;
400
401       ok &= gst_structure_get_int (structure, "depth", &depth);
402       ok &= gst_structure_get_int (structure, "bpp", &bpp);
403
404       if (bpp > 8)
405         ok &= gst_structure_get_int (structure, "endianness", &endianness);
406
407       if (depth == 8 && bpp == 8) {
408         *format = GST_VIDEO_FORMAT_GRAY8;
409       } else if (depth == 16 && bpp == 16 && endianness == G_BIG_ENDIAN) {
410         *format = GST_VIDEO_FORMAT_GRAY16_BE;
411       } else if (depth == 16 && bpp == 16 && endianness == G_LITTLE_ENDIAN) {
412         *format = GST_VIDEO_FORMAT_GRAY16_LE;
413       } else {
414         ok = FALSE;
415       }
416     } else {
417       ok = FALSE;
418     }
419   }
420
421   if (width) {
422     ok &= gst_structure_get_int (structure, "width", width);
423   }
424
425   if (height) {
426     ok &= gst_structure_get_int (structure, "height", height);
427   }
428
429   return ok;
430 }
431
432
433 /**
434  * gst_video_parse_caps_framerate:
435  * @caps: pointer to a #GstCaps instance
436  * @fps_n: pointer to integer to hold numerator of frame rate (output)
437  * @fps_d: pointer to integer to hold denominator of frame rate (output)
438  *
439  * Extracts the frame rate from @caps and places the values in the locations
440  * pointed to by @fps_n and @fps_d.  Returns TRUE if the values could be
441  * parsed correctly, FALSE if not.
442  *
443  * This function can be used with #GstCaps that have any media type; it
444  * is not limited to formats handled by #GstVideoFormat.
445  *
446  * Since: 0.10.16
447  *
448  * Returns: TRUE if @caps was parsed correctly.
449  */
450 gboolean
451 gst_video_parse_caps_framerate (GstCaps * caps, int *fps_n, int *fps_d)
452 {
453   GstStructure *structure;
454
455   if (!gst_caps_is_fixed (caps))
456     return FALSE;
457
458   structure = gst_caps_get_structure (caps, 0);
459
460   return gst_structure_get_fraction (structure, "framerate", fps_n, fps_d);
461 }
462
463 /**
464  * gst_video_parse_caps_pixel_aspect_ratio:
465  * @caps: pointer to a #GstCaps instance
466  * @par_n: pointer to numerator of pixel aspect ratio (output)
467  * @par_d: pointer to denominator of pixel aspect ratio (output)
468  *
469  * Extracts the pixel aspect ratio from @caps and places the values in
470  * the locations pointed to by @par_n and @par_d.  Returns TRUE if the
471  * values could be parsed correctly, FALSE if not.
472  *
473  * This function can be used with #GstCaps that have any media type; it
474  * is not limited to formats handled by #GstVideoFormat.
475  *
476  * Since: 0.10.16
477  *
478  * Returns: TRUE if @caps was parsed correctly.
479  */
480 gboolean
481 gst_video_parse_caps_pixel_aspect_ratio (GstCaps * caps, int *par_n, int *par_d)
482 {
483   GstStructure *structure;
484
485   if (!gst_caps_is_fixed (caps))
486     return FALSE;
487
488   structure = gst_caps_get_structure (caps, 0);
489
490   if (!gst_structure_get_fraction (structure, "pixel-aspect-ratio",
491           par_n, par_d)) {
492     *par_n = 1;
493     *par_d = 1;
494   }
495   return TRUE;
496 }
497
498 /**
499  * gst_video_format_new_caps_interlaced:
500  * @format: the #GstVideoFormat describing the raw video format
501  * @width: width of video
502  * @height: height of video
503  * @framerate_n: numerator of frame rate
504  * @framerate_d: denominator of frame rate
505  * @par_n: numerator of pixel aspect ratio
506  * @par_d: denominator of pixel aspect ratio
507  * @interlaced: #TRUE if the format is interlaced
508  *
509  * Creates a new #GstCaps object based on the parameters provided.
510  *
511  * Since: 0.10.23
512  *
513  * Returns: a new #GstCaps object, or NULL if there was an error
514  */
515 GstCaps *
516 gst_video_format_new_caps_interlaced (GstVideoFormat format, int width,
517     int height, int framerate_n, int framerate_d, int par_n, int par_d,
518     gboolean interlaced)
519 {
520   GstCaps *res;
521
522   res =
523       gst_video_format_new_caps (format, width, height, framerate_n,
524       framerate_d, par_n, par_d);
525   if (interlaced && (res != NULL))
526     gst_caps_set_simple (res, "interlaced", G_TYPE_BOOLEAN, TRUE, NULL);
527
528   return res;
529 }
530
531 /**
532  * gst_video_format_new_caps:
533  * @format: the #GstVideoFormat describing the raw video format
534  * @width: width of video
535  * @height: height of video
536  * @framerate_n: numerator of frame rate
537  * @framerate_d: denominator of frame rate
538  * @par_n: numerator of pixel aspect ratio
539  * @par_d: denominator of pixel aspect ratio
540  *
541  * Creates a new #GstCaps object based on the parameters provided.
542  *
543  * Since: 0.10.16
544  *
545  * Returns: a new #GstCaps object, or NULL if there was an error
546  */
547 GstCaps *
548 gst_video_format_new_caps (GstVideoFormat format, int width, int height,
549     int framerate_n, int framerate_d, int par_n, int par_d)
550 {
551   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
552   g_return_val_if_fail (width > 0 && height > 0, NULL);
553
554   if (gst_video_format_is_yuv (format)) {
555     return gst_caps_new_simple ("video/x-raw-yuv",
556         "format", GST_TYPE_FOURCC, gst_video_format_to_fourcc (format),
557         "width", G_TYPE_INT, width,
558         "height", G_TYPE_INT, height,
559         "framerate", GST_TYPE_FRACTION, framerate_n, framerate_d,
560         "pixel-aspect-ratio", GST_TYPE_FRACTION, par_n, par_d, NULL);
561   }
562   if (gst_video_format_is_rgb (format)) {
563     GstCaps *caps;
564     int red_mask;
565     int blue_mask;
566     int green_mask;
567     int alpha_mask;
568     int depth;
569     int bpp;
570     gboolean have_alpha;
571     unsigned int mask;
572
573     switch (format) {
574       case GST_VIDEO_FORMAT_RGBx:
575       case GST_VIDEO_FORMAT_BGRx:
576       case GST_VIDEO_FORMAT_xRGB:
577       case GST_VIDEO_FORMAT_xBGR:
578         bpp = 32;
579         depth = 24;
580         have_alpha = FALSE;
581         break;
582       case GST_VIDEO_FORMAT_RGBA:
583       case GST_VIDEO_FORMAT_BGRA:
584       case GST_VIDEO_FORMAT_ARGB:
585       case GST_VIDEO_FORMAT_ABGR:
586         bpp = 32;
587         depth = 32;
588         have_alpha = TRUE;
589         break;
590       case GST_VIDEO_FORMAT_RGB:
591       case GST_VIDEO_FORMAT_BGR:
592         bpp = 24;
593         depth = 24;
594         have_alpha = FALSE;
595         break;
596       case GST_VIDEO_FORMAT_RGB16:
597       case GST_VIDEO_FORMAT_BGR16:
598         bpp = 16;
599         depth = 16;
600         have_alpha = FALSE;
601         break;
602       case GST_VIDEO_FORMAT_RGB15:
603       case GST_VIDEO_FORMAT_BGR15:
604         bpp = 16;
605         depth = 15;
606         have_alpha = FALSE;
607         break;
608       default:
609         return NULL;
610     }
611     if (bpp == 32 || bpp == 24) {
612       if (bpp == 32) {
613         mask = 0xff000000;
614       } else {
615         mask = 0xff0000;
616       }
617       red_mask =
618           mask >> (8 * gst_video_format_get_component_offset (format, 0, width,
619               height));
620       green_mask =
621           mask >> (8 * gst_video_format_get_component_offset (format, 1, width,
622               height));
623       blue_mask =
624           mask >> (8 * gst_video_format_get_component_offset (format, 2, width,
625               height));
626     } else if (bpp == 16) {
627       switch (format) {
628         case GST_VIDEO_FORMAT_RGB16:
629           red_mask = GST_VIDEO_COMP1_MASK_16_INT;
630           green_mask = GST_VIDEO_COMP2_MASK_16_INT;
631           blue_mask = GST_VIDEO_COMP3_MASK_16_INT;
632           break;
633         case GST_VIDEO_FORMAT_BGR16:
634           red_mask = GST_VIDEO_COMP3_MASK_16_INT;
635           green_mask = GST_VIDEO_COMP2_MASK_16_INT;
636           blue_mask = GST_VIDEO_COMP1_MASK_16_INT;
637           break;
638           break;
639         case GST_VIDEO_FORMAT_RGB15:
640           red_mask = GST_VIDEO_COMP1_MASK_15_INT;
641           green_mask = GST_VIDEO_COMP2_MASK_15_INT;
642           blue_mask = GST_VIDEO_COMP3_MASK_15_INT;
643           break;
644         case GST_VIDEO_FORMAT_BGR15:
645           red_mask = GST_VIDEO_COMP3_MASK_15_INT;
646           green_mask = GST_VIDEO_COMP2_MASK_15_INT;
647           blue_mask = GST_VIDEO_COMP1_MASK_15_INT;
648           break;
649         default:
650           return NULL;
651       }
652     } else {
653       return NULL;
654     }
655
656     caps = gst_caps_new_simple ("video/x-raw-rgb",
657         "bpp", G_TYPE_INT, bpp,
658         "depth", G_TYPE_INT, depth,
659         "endianness", G_TYPE_INT, G_BIG_ENDIAN,
660         "red_mask", G_TYPE_INT, red_mask,
661         "green_mask", G_TYPE_INT, green_mask,
662         "blue_mask", G_TYPE_INT, blue_mask,
663         "width", G_TYPE_INT, width,
664         "height", G_TYPE_INT, height,
665         "framerate", GST_TYPE_FRACTION, framerate_n, framerate_d,
666         "pixel-aspect-ratio", GST_TYPE_FRACTION, par_n, par_d, NULL);
667     if (have_alpha) {
668       alpha_mask =
669           mask >> (8 * gst_video_format_get_component_offset (format, 3, width,
670               height));
671       gst_caps_set_simple (caps, "alpha_mask", G_TYPE_INT, alpha_mask, NULL);
672     }
673     return caps;
674   }
675
676   if (gst_video_format_is_gray (format)) {
677     GstCaps *caps;
678     int bpp;
679     int depth;
680     int endianness;
681
682     switch (format) {
683       case GST_VIDEO_FORMAT_GRAY8:
684         bpp = depth = 8;
685         endianness = G_BIG_ENDIAN;
686         break;
687       case GST_VIDEO_FORMAT_GRAY16_BE:
688         bpp = depth = 16;
689         endianness = G_BIG_ENDIAN;
690         break;
691       case GST_VIDEO_FORMAT_GRAY16_LE:
692         bpp = depth = 16;
693         endianness = G_LITTLE_ENDIAN;
694         break;
695       default:
696         return NULL;
697         break;
698     }
699
700     if (bpp > 8) {
701       caps = gst_caps_new_simple ("video/x-raw-gray",
702           "bpp", G_TYPE_INT, bpp,
703           "depth", G_TYPE_INT, depth,
704           "width", G_TYPE_INT, width,
705           "height", G_TYPE_INT, height,
706           "framerate", GST_TYPE_FRACTION, framerate_n, framerate_d,
707           "pixel-aspect-ratio", GST_TYPE_FRACTION, par_n, par_d, NULL);
708     } else {
709       caps = gst_caps_new_simple ("video/x-raw-gray",
710           "bpp", G_TYPE_INT, bpp,
711           "depth", G_TYPE_INT, depth,
712           "endianness", G_TYPE_INT, G_BIG_ENDIAN,
713           "width", G_TYPE_INT, width,
714           "height", G_TYPE_INT, height,
715           "framerate", GST_TYPE_FRACTION, framerate_n, framerate_d,
716           "pixel-aspect-ratio", GST_TYPE_FRACTION, par_n, par_d, NULL);
717     }
718
719     return caps;
720   }
721
722   return NULL;
723 }
724
725 /**
726  * gst_video_format_from_fourcc:
727  * @fourcc: a FOURCC value representing raw YUV video
728  *
729  * Converts a FOURCC value into the corresponding #GstVideoFormat.
730  * If the FOURCC cannot be represented by #GstVideoFormat,
731  * #GST_VIDEO_FORMAT_UNKNOWN is returned.
732  *
733  * Since: 0.10.16
734  *
735  * Returns: the #GstVideoFormat describing the FOURCC value
736  */
737 GstVideoFormat
738 gst_video_format_from_fourcc (guint32 fourcc)
739 {
740   switch (fourcc) {
741     case GST_MAKE_FOURCC ('I', '4', '2', '0'):
742       return GST_VIDEO_FORMAT_I420;
743     case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
744       return GST_VIDEO_FORMAT_YV12;
745     case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
746       return GST_VIDEO_FORMAT_YUY2;
747     case GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'):
748       return GST_VIDEO_FORMAT_YVYU;
749     case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
750       return GST_VIDEO_FORMAT_UYVY;
751     case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
752       return GST_VIDEO_FORMAT_AYUV;
753     case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
754       return GST_VIDEO_FORMAT_Y41B;
755     case GST_MAKE_FOURCC ('Y', '4', '2', 'B'):
756       return GST_VIDEO_FORMAT_Y42B;
757     case GST_MAKE_FOURCC ('Y', '4', '4', '4'):
758       return GST_VIDEO_FORMAT_Y444;
759     case GST_MAKE_FOURCC ('v', '2', '1', '0'):
760       return GST_VIDEO_FORMAT_v210;
761     case GST_MAKE_FOURCC ('v', '2', '1', '6'):
762       return GST_VIDEO_FORMAT_v216;
763     case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
764       return GST_VIDEO_FORMAT_NV12;
765     case GST_MAKE_FOURCC ('N', 'V', '2', '1'):
766       return GST_VIDEO_FORMAT_NV21;
767     case GST_MAKE_FOURCC ('v', '3', '0', '8'):
768       return GST_VIDEO_FORMAT_v308;
769     case GST_MAKE_FOURCC ('Y', '8', '0', '0'):
770     case GST_MAKE_FOURCC ('Y', '8', ' ', ' '):
771     case GST_MAKE_FOURCC ('G', 'R', 'E', 'Y'):
772       return GST_VIDEO_FORMAT_Y800;
773     case GST_MAKE_FOURCC ('Y', '1', '6', ' '):
774       return GST_VIDEO_FORMAT_Y16;
775     default:
776       return GST_VIDEO_FORMAT_UNKNOWN;
777   }
778 }
779
780 /**
781  * gst_video_format_to_fourcc:
782  * @format: a #GstVideoFormat video format
783  *
784  * Converts a #GstVideoFormat value into the corresponding FOURCC.  Only
785  * a few YUV formats have corresponding FOURCC values.  If @format has
786  * no corresponding FOURCC value, 0 is returned.
787  *
788  * Since: 0.10.16
789  *
790  * Returns: the FOURCC corresponding to @format
791  */
792 guint32
793 gst_video_format_to_fourcc (GstVideoFormat format)
794 {
795   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
796
797   switch (format) {
798     case GST_VIDEO_FORMAT_I420:
799       return GST_MAKE_FOURCC ('I', '4', '2', '0');
800     case GST_VIDEO_FORMAT_YV12:
801       return GST_MAKE_FOURCC ('Y', 'V', '1', '2');
802     case GST_VIDEO_FORMAT_YUY2:
803       return GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
804     case GST_VIDEO_FORMAT_YVYU:
805       return GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U');
806     case GST_VIDEO_FORMAT_UYVY:
807       return GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
808     case GST_VIDEO_FORMAT_AYUV:
809       return GST_MAKE_FOURCC ('A', 'Y', 'U', 'V');
810     case GST_VIDEO_FORMAT_Y41B:
811       return GST_MAKE_FOURCC ('Y', '4', '1', 'B');
812     case GST_VIDEO_FORMAT_Y42B:
813       return GST_MAKE_FOURCC ('Y', '4', '2', 'B');
814     case GST_VIDEO_FORMAT_Y444:
815       return GST_MAKE_FOURCC ('Y', '4', '4', '4');
816     case GST_VIDEO_FORMAT_v210:
817       return GST_MAKE_FOURCC ('v', '2', '1', '0');
818     case GST_VIDEO_FORMAT_v216:
819       return GST_MAKE_FOURCC ('v', '2', '1', '6');
820     case GST_VIDEO_FORMAT_NV12:
821       return GST_MAKE_FOURCC ('N', 'V', '1', '2');
822     case GST_VIDEO_FORMAT_NV21:
823       return GST_MAKE_FOURCC ('N', 'V', '2', '1');
824     case GST_VIDEO_FORMAT_v308:
825       return GST_MAKE_FOURCC ('v', '3', '0', '8');
826     case GST_VIDEO_FORMAT_Y800:
827       return GST_MAKE_FOURCC ('Y', '8', '0', '0');
828     case GST_VIDEO_FORMAT_Y16:
829       return GST_MAKE_FOURCC ('Y', '1', '6', ' ');
830     default:
831       return 0;
832   }
833 }
834
835 /*
836  * gst_video_format_from_rgb32_masks:
837  * @red_mask: red bit mask
838  * @green_mask: green bit mask
839  * @blue_mask: blue bit mask
840  *
841  * Converts red, green, blue bit masks into the corresponding
842  * #GstVideoFormat.  
843  *
844  * Since: 0.10.16
845  *
846  * Returns: the #GstVideoFormat corresponding to the bit masks
847  */
848 static GstVideoFormat
849 gst_video_format_from_rgb32_masks (int red_mask, int green_mask, int blue_mask)
850 {
851   if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
852       blue_mask == 0x0000ff00) {
853     return GST_VIDEO_FORMAT_RGBx;
854   }
855   if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
856       blue_mask == 0xff000000) {
857     return GST_VIDEO_FORMAT_BGRx;
858   }
859   if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
860       blue_mask == 0x000000ff) {
861     return GST_VIDEO_FORMAT_xRGB;
862   }
863   if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
864       blue_mask == 0x00ff0000) {
865     return GST_VIDEO_FORMAT_xBGR;
866   }
867
868   return GST_VIDEO_FORMAT_UNKNOWN;
869 }
870
871 static GstVideoFormat
872 gst_video_format_from_rgba32_masks (int red_mask, int green_mask, int blue_mask,
873     int alpha_mask)
874 {
875   if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
876       blue_mask == 0x0000ff00 && alpha_mask == 0x000000ff) {
877     return GST_VIDEO_FORMAT_RGBA;
878   }
879   if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
880       blue_mask == 0xff000000 && alpha_mask == 0x000000ff) {
881     return GST_VIDEO_FORMAT_BGRA;
882   }
883   if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
884       blue_mask == 0x000000ff && alpha_mask == 0xff000000) {
885     return GST_VIDEO_FORMAT_ARGB;
886   }
887   if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
888       blue_mask == 0x00ff0000 && alpha_mask == 0xff000000) {
889     return GST_VIDEO_FORMAT_ABGR;
890   }
891
892   return GST_VIDEO_FORMAT_UNKNOWN;
893 }
894
895 static GstVideoFormat
896 gst_video_format_from_rgb24_masks (int red_mask, int green_mask, int blue_mask)
897 {
898   if (red_mask == 0xff0000 && green_mask == 0x00ff00 && blue_mask == 0x0000ff) {
899     return GST_VIDEO_FORMAT_RGB;
900   }
901   if (red_mask == 0x0000ff && green_mask == 0x00ff00 && blue_mask == 0xff0000) {
902     return GST_VIDEO_FORMAT_BGR;
903   }
904
905   return GST_VIDEO_FORMAT_UNKNOWN;
906 }
907
908 static GstVideoFormat
909 gst_video_format_from_rgb16_masks (int red_mask, int green_mask, int blue_mask)
910 {
911   if (red_mask == GST_VIDEO_COMP1_MASK_16_INT
912       && green_mask == GST_VIDEO_COMP2_MASK_16_INT
913       && blue_mask == GST_VIDEO_COMP3_MASK_16_INT) {
914     return GST_VIDEO_FORMAT_RGB16;
915   }
916   if (red_mask == GST_VIDEO_COMP3_MASK_16_INT
917       && green_mask == GST_VIDEO_COMP2_MASK_16_INT
918       && blue_mask == GST_VIDEO_COMP1_MASK_16_INT) {
919     return GST_VIDEO_FORMAT_BGR16;
920   }
921   if (red_mask == GST_VIDEO_COMP1_MASK_15_INT
922       && green_mask == GST_VIDEO_COMP2_MASK_15_INT
923       && blue_mask == GST_VIDEO_COMP3_MASK_15_INT) {
924     return GST_VIDEO_FORMAT_RGB15;
925   }
926   if (red_mask == GST_VIDEO_COMP3_MASK_15_INT
927       && green_mask == GST_VIDEO_COMP2_MASK_15_INT
928       && blue_mask == GST_VIDEO_COMP1_MASK_15_INT) {
929     return GST_VIDEO_FORMAT_BGR15;
930   }
931
932   return GST_VIDEO_FORMAT_UNKNOWN;
933 }
934
935 /**
936  * gst_video_format_is_rgb:
937  * @format: a #GstVideoFormat
938  *
939  * Determine whether the video format is an RGB format.
940  *
941  * Since: 0.10.16
942  *
943  * Returns: TRUE if @format represents RGB video
944  */
945 gboolean
946 gst_video_format_is_rgb (GstVideoFormat format)
947 {
948   switch (format) {
949     case GST_VIDEO_FORMAT_I420:
950     case GST_VIDEO_FORMAT_YV12:
951     case GST_VIDEO_FORMAT_YUY2:
952     case GST_VIDEO_FORMAT_YVYU:
953     case GST_VIDEO_FORMAT_UYVY:
954     case GST_VIDEO_FORMAT_AYUV:
955     case GST_VIDEO_FORMAT_Y41B:
956     case GST_VIDEO_FORMAT_Y42B:
957     case GST_VIDEO_FORMAT_Y444:
958     case GST_VIDEO_FORMAT_v210:
959     case GST_VIDEO_FORMAT_v216:
960     case GST_VIDEO_FORMAT_NV12:
961     case GST_VIDEO_FORMAT_NV21:
962     case GST_VIDEO_FORMAT_v308:
963       return FALSE;
964     case GST_VIDEO_FORMAT_RGBx:
965     case GST_VIDEO_FORMAT_BGRx:
966     case GST_VIDEO_FORMAT_xRGB:
967     case GST_VIDEO_FORMAT_xBGR:
968     case GST_VIDEO_FORMAT_RGBA:
969     case GST_VIDEO_FORMAT_BGRA:
970     case GST_VIDEO_FORMAT_ARGB:
971     case GST_VIDEO_FORMAT_ABGR:
972     case GST_VIDEO_FORMAT_RGB:
973     case GST_VIDEO_FORMAT_BGR:
974     case GST_VIDEO_FORMAT_RGB16:
975     case GST_VIDEO_FORMAT_BGR16:
976     case GST_VIDEO_FORMAT_RGB15:
977     case GST_VIDEO_FORMAT_BGR15:
978       return TRUE;
979     default:
980       return FALSE;
981   }
982 }
983
984 /**
985  * gst_video_format_is_yuv:
986  * @format: a #GstVideoFormat
987  *
988  * Determine whether the video format is a YUV format.
989  *
990  * Since: 0.10.16
991  *
992  * Returns: TRUE if @format represents YUV video
993  */
994 gboolean
995 gst_video_format_is_yuv (GstVideoFormat format)
996 {
997   switch (format) {
998     case GST_VIDEO_FORMAT_I420:
999     case GST_VIDEO_FORMAT_YV12:
1000     case GST_VIDEO_FORMAT_YUY2:
1001     case GST_VIDEO_FORMAT_YVYU:
1002     case GST_VIDEO_FORMAT_UYVY:
1003     case GST_VIDEO_FORMAT_AYUV:
1004     case GST_VIDEO_FORMAT_Y41B:
1005     case GST_VIDEO_FORMAT_Y42B:
1006     case GST_VIDEO_FORMAT_Y444:
1007     case GST_VIDEO_FORMAT_v210:
1008     case GST_VIDEO_FORMAT_v216:
1009     case GST_VIDEO_FORMAT_NV12:
1010     case GST_VIDEO_FORMAT_NV21:
1011     case GST_VIDEO_FORMAT_v308:
1012     case GST_VIDEO_FORMAT_Y800:
1013     case GST_VIDEO_FORMAT_Y16:
1014       return TRUE;
1015     case GST_VIDEO_FORMAT_RGBx:
1016     case GST_VIDEO_FORMAT_BGRx:
1017     case GST_VIDEO_FORMAT_xRGB:
1018     case GST_VIDEO_FORMAT_xBGR:
1019     case GST_VIDEO_FORMAT_RGBA:
1020     case GST_VIDEO_FORMAT_BGRA:
1021     case GST_VIDEO_FORMAT_ARGB:
1022     case GST_VIDEO_FORMAT_ABGR:
1023     case GST_VIDEO_FORMAT_RGB:
1024     case GST_VIDEO_FORMAT_BGR:
1025     case GST_VIDEO_FORMAT_RGB16:
1026     case GST_VIDEO_FORMAT_BGR16:
1027     case GST_VIDEO_FORMAT_RGB15:
1028     case GST_VIDEO_FORMAT_BGR15:
1029       return FALSE;
1030     default:
1031       return FALSE;
1032   }
1033 }
1034
1035 /**
1036  * gst_video_format_is_gray:
1037  * @format: a #GstVideoFormat
1038  *
1039  * Determine whether the video format is a grayscale format.
1040  *
1041  * Since: 0.10.29
1042  *
1043  * Returns: TRUE if @format represents grayscale video
1044  */
1045 gboolean
1046 gst_video_format_is_gray (GstVideoFormat format)
1047 {
1048   switch (format) {
1049     case GST_VIDEO_FORMAT_GRAY8:
1050     case GST_VIDEO_FORMAT_GRAY16_BE:
1051     case GST_VIDEO_FORMAT_GRAY16_LE:
1052     case GST_VIDEO_FORMAT_Y800:
1053     case GST_VIDEO_FORMAT_Y16:
1054       return TRUE;
1055     default:
1056       return FALSE;
1057   }
1058 }
1059
1060 /**
1061  * gst_video_format_has_alpha:
1062  * @format: a #GstVideoFormat
1063  * 
1064  * Returns TRUE or FALSE depending on if the video format provides an
1065  * alpha channel.
1066  *
1067  * Since: 0.10.16
1068  *
1069  * Returns: TRUE if @format has an alpha channel
1070  */
1071 gboolean
1072 gst_video_format_has_alpha (GstVideoFormat format)
1073 {
1074   switch (format) {
1075     case GST_VIDEO_FORMAT_I420:
1076     case GST_VIDEO_FORMAT_YV12:
1077     case GST_VIDEO_FORMAT_YUY2:
1078     case GST_VIDEO_FORMAT_YVYU:
1079     case GST_VIDEO_FORMAT_UYVY:
1080     case GST_VIDEO_FORMAT_Y41B:
1081     case GST_VIDEO_FORMAT_Y42B:
1082     case GST_VIDEO_FORMAT_Y444:
1083     case GST_VIDEO_FORMAT_v210:
1084     case GST_VIDEO_FORMAT_v216:
1085     case GST_VIDEO_FORMAT_NV12:
1086     case GST_VIDEO_FORMAT_NV21:
1087     case GST_VIDEO_FORMAT_v308:
1088     case GST_VIDEO_FORMAT_Y800:
1089     case GST_VIDEO_FORMAT_Y16:
1090       return FALSE;
1091     case GST_VIDEO_FORMAT_AYUV:
1092     case GST_VIDEO_FORMAT_RGBA:
1093     case GST_VIDEO_FORMAT_BGRA:
1094     case GST_VIDEO_FORMAT_ARGB:
1095     case GST_VIDEO_FORMAT_ABGR:
1096       return TRUE;
1097     case GST_VIDEO_FORMAT_RGBx:
1098     case GST_VIDEO_FORMAT_BGRx:
1099     case GST_VIDEO_FORMAT_xRGB:
1100     case GST_VIDEO_FORMAT_xBGR:
1101     case GST_VIDEO_FORMAT_RGB:
1102     case GST_VIDEO_FORMAT_BGR:
1103     case GST_VIDEO_FORMAT_RGB16:
1104     case GST_VIDEO_FORMAT_BGR16:
1105     case GST_VIDEO_FORMAT_RGB15:
1106     case GST_VIDEO_FORMAT_BGR15:
1107       return FALSE;
1108     default:
1109       return FALSE;
1110   }
1111 }
1112
1113 /**
1114  * gst_video_format_get_row_stride:
1115  * @format: a #GstVideoFormat
1116  * @component: the component index
1117  * @width: the width of video
1118  *
1119  * Calculates the row stride (number of bytes from one row of pixels to
1120  * the next) for the video component with an index of @component.  For
1121  * YUV video, Y, U, and V have component indices of 0, 1, and 2,
1122  * respectively.  For RGB video, R, G, and B have component indicies of
1123  * 0, 1, and 2, respectively.  Alpha channels, if present, have a component
1124  * index of 3.  The @width parameter always represents the width of the
1125  * video, not the component.
1126  *
1127  * Since: 0.10.16
1128  *
1129  * Returns: row stride of component @component
1130  */
1131 int
1132 gst_video_format_get_row_stride (GstVideoFormat format, int component,
1133     int width)
1134 {
1135   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1136   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1137   g_return_val_if_fail (width > 0, 0);
1138
1139   switch (format) {
1140     case GST_VIDEO_FORMAT_I420:
1141     case GST_VIDEO_FORMAT_YV12:
1142       if (component == 0) {
1143         return GST_ROUND_UP_4 (width);
1144       } else {
1145         return GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1146       }
1147     case GST_VIDEO_FORMAT_YUY2:
1148     case GST_VIDEO_FORMAT_YVYU:
1149     case GST_VIDEO_FORMAT_UYVY:
1150       return GST_ROUND_UP_4 (width * 2);
1151     case GST_VIDEO_FORMAT_AYUV:
1152     case GST_VIDEO_FORMAT_RGBx:
1153     case GST_VIDEO_FORMAT_BGRx:
1154     case GST_VIDEO_FORMAT_xRGB:
1155     case GST_VIDEO_FORMAT_xBGR:
1156     case GST_VIDEO_FORMAT_RGBA:
1157     case GST_VIDEO_FORMAT_BGRA:
1158     case GST_VIDEO_FORMAT_ARGB:
1159     case GST_VIDEO_FORMAT_ABGR:
1160       return width * 4;
1161     case GST_VIDEO_FORMAT_RGB16:
1162     case GST_VIDEO_FORMAT_BGR16:
1163     case GST_VIDEO_FORMAT_RGB15:
1164     case GST_VIDEO_FORMAT_BGR15:
1165       return GST_ROUND_UP_4 (width * 2);
1166     case GST_VIDEO_FORMAT_RGB:
1167     case GST_VIDEO_FORMAT_BGR:
1168     case GST_VIDEO_FORMAT_v308:
1169       return GST_ROUND_UP_4 (width * 3);
1170     case GST_VIDEO_FORMAT_Y41B:
1171       if (component == 0) {
1172         return GST_ROUND_UP_4 (width);
1173       } else {
1174         return GST_ROUND_UP_16 (width) / 4;
1175       }
1176     case GST_VIDEO_FORMAT_Y42B:
1177       if (component == 0) {
1178         return GST_ROUND_UP_4 (width);
1179       } else {
1180         return GST_ROUND_UP_8 (width) / 2;
1181       }
1182     case GST_VIDEO_FORMAT_Y444:
1183       return GST_ROUND_UP_4 (width);
1184     case GST_VIDEO_FORMAT_v210:
1185       return ((width + 47) / 48) * 128;
1186     case GST_VIDEO_FORMAT_v216:
1187       return GST_ROUND_UP_8 (width * 4);
1188     case GST_VIDEO_FORMAT_NV12:
1189     case GST_VIDEO_FORMAT_NV21:
1190       return GST_ROUND_UP_4 (width);
1191     case GST_VIDEO_FORMAT_GRAY8:
1192     case GST_VIDEO_FORMAT_Y800:
1193       return GST_ROUND_UP_4 (width);
1194     case GST_VIDEO_FORMAT_GRAY16_BE:
1195     case GST_VIDEO_FORMAT_GRAY16_LE:
1196     case GST_VIDEO_FORMAT_Y16:
1197       return GST_ROUND_UP_4 (width * 2);
1198     default:
1199       return 0;
1200   }
1201 }
1202
1203 /**
1204  * gst_video_format_get_pixel_stride:
1205  * @format: a #GstVideoFormat
1206  * @component: the component index
1207  *
1208  * Calculates the pixel stride (number of bytes from one pixel to the
1209  * pixel to its immediate left) for the video component with an index
1210  * of @component.  See @gst_video_format_get_row_stride for a description
1211  * of the component index.
1212  *
1213  * Since: 0.10.16
1214  *
1215  * Returns: pixel stride of component @component
1216  */
1217 int
1218 gst_video_format_get_pixel_stride (GstVideoFormat format, int component)
1219 {
1220   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1221   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1222
1223   switch (format) {
1224     case GST_VIDEO_FORMAT_I420:
1225     case GST_VIDEO_FORMAT_YV12:
1226     case GST_VIDEO_FORMAT_Y41B:
1227     case GST_VIDEO_FORMAT_Y42B:
1228     case GST_VIDEO_FORMAT_Y444:
1229       return 1;
1230     case GST_VIDEO_FORMAT_YUY2:
1231     case GST_VIDEO_FORMAT_YVYU:
1232     case GST_VIDEO_FORMAT_UYVY:
1233       if (component == 0) {
1234         return 2;
1235       } else {
1236         return 4;
1237       }
1238     case GST_VIDEO_FORMAT_AYUV:
1239     case GST_VIDEO_FORMAT_RGBx:
1240     case GST_VIDEO_FORMAT_BGRx:
1241     case GST_VIDEO_FORMAT_xRGB:
1242     case GST_VIDEO_FORMAT_xBGR:
1243     case GST_VIDEO_FORMAT_RGBA:
1244     case GST_VIDEO_FORMAT_BGRA:
1245     case GST_VIDEO_FORMAT_ARGB:
1246     case GST_VIDEO_FORMAT_ABGR:
1247       return 4;
1248     case GST_VIDEO_FORMAT_RGB16:
1249     case GST_VIDEO_FORMAT_BGR16:
1250     case GST_VIDEO_FORMAT_RGB15:
1251     case GST_VIDEO_FORMAT_BGR15:
1252       return 2;
1253     case GST_VIDEO_FORMAT_RGB:
1254     case GST_VIDEO_FORMAT_BGR:
1255     case GST_VIDEO_FORMAT_v308:
1256       return 3;
1257     case GST_VIDEO_FORMAT_v210:
1258       /* v210 is packed at the bit level, so pixel stride doesn't make sense */
1259       return 0;
1260     case GST_VIDEO_FORMAT_v216:
1261       if (component == 0) {
1262         return 4;
1263       } else {
1264         return 8;
1265       }
1266     case GST_VIDEO_FORMAT_NV12:
1267     case GST_VIDEO_FORMAT_NV21:
1268       if (component == 0) {
1269         return 1;
1270       } else {
1271         return 2;
1272       }
1273     case GST_VIDEO_FORMAT_GRAY8:
1274     case GST_VIDEO_FORMAT_Y800:
1275       return 1;
1276     case GST_VIDEO_FORMAT_GRAY16_BE:
1277     case GST_VIDEO_FORMAT_GRAY16_LE:
1278     case GST_VIDEO_FORMAT_Y16:
1279       return 2;
1280     default:
1281       return 0;
1282   }
1283 }
1284
1285 /**
1286  * gst_video_format_get_component_width:
1287  * @format: a #GstVideoFormat
1288  * @component: the component index
1289  * @width: the width of video
1290  *
1291  * Calculates the width of the component.  See
1292  * @gst_video_format_get_row_stride for a description
1293  * of the component index.
1294  *
1295  * Since: 0.10.16
1296  *
1297  * Returns: width of component @component
1298  */
1299 int
1300 gst_video_format_get_component_width (GstVideoFormat format, int component,
1301     int width)
1302 {
1303   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1304   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1305   g_return_val_if_fail (width > 0, 0);
1306
1307   switch (format) {
1308     case GST_VIDEO_FORMAT_I420:
1309     case GST_VIDEO_FORMAT_YV12:
1310     case GST_VIDEO_FORMAT_YUY2:
1311     case GST_VIDEO_FORMAT_YVYU:
1312     case GST_VIDEO_FORMAT_UYVY:
1313     case GST_VIDEO_FORMAT_Y42B:
1314     case GST_VIDEO_FORMAT_v210:
1315     case GST_VIDEO_FORMAT_v216:
1316       if (component == 0) {
1317         return width;
1318       } else {
1319         return GST_ROUND_UP_2 (width) / 2;
1320       }
1321     case GST_VIDEO_FORMAT_Y41B:
1322       if (component == 0) {
1323         return width;
1324       } else {
1325         return GST_ROUND_UP_4 (width) / 4;
1326       }
1327     case GST_VIDEO_FORMAT_AYUV:
1328     case GST_VIDEO_FORMAT_RGBx:
1329     case GST_VIDEO_FORMAT_BGRx:
1330     case GST_VIDEO_FORMAT_xRGB:
1331     case GST_VIDEO_FORMAT_xBGR:
1332     case GST_VIDEO_FORMAT_RGBA:
1333     case GST_VIDEO_FORMAT_BGRA:
1334     case GST_VIDEO_FORMAT_ARGB:
1335     case GST_VIDEO_FORMAT_ABGR:
1336     case GST_VIDEO_FORMAT_RGB:
1337     case GST_VIDEO_FORMAT_BGR:
1338     case GST_VIDEO_FORMAT_RGB16:
1339     case GST_VIDEO_FORMAT_BGR16:
1340     case GST_VIDEO_FORMAT_RGB15:
1341     case GST_VIDEO_FORMAT_BGR15:
1342     case GST_VIDEO_FORMAT_Y444:
1343     case GST_VIDEO_FORMAT_v308:
1344     case GST_VIDEO_FORMAT_NV12:
1345     case GST_VIDEO_FORMAT_NV21:
1346     case GST_VIDEO_FORMAT_GRAY8:
1347     case GST_VIDEO_FORMAT_GRAY16_BE:
1348     case GST_VIDEO_FORMAT_GRAY16_LE:
1349     case GST_VIDEO_FORMAT_Y800:
1350     case GST_VIDEO_FORMAT_Y16:
1351       return width;
1352     default:
1353       return 0;
1354   }
1355 }
1356
1357 /**
1358  * gst_video_format_get_component_height:
1359  * @format: a #GstVideoFormat
1360  * @component: the component index
1361  * @height: the height of video
1362  *
1363  * Calculates the height of the component.  See
1364  * @gst_video_format_get_row_stride for a description
1365  * of the component index.
1366  *
1367  * Since: 0.10.16
1368  *
1369  * Returns: height of component @component
1370  */
1371 int
1372 gst_video_format_get_component_height (GstVideoFormat format, int component,
1373     int height)
1374 {
1375   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1376   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1377   g_return_val_if_fail (height > 0, 0);
1378
1379   switch (format) {
1380     case GST_VIDEO_FORMAT_I420:
1381     case GST_VIDEO_FORMAT_YV12:
1382     case GST_VIDEO_FORMAT_NV12:
1383     case GST_VIDEO_FORMAT_NV21:
1384       if (component == 0) {
1385         return height;
1386       } else {
1387         return GST_ROUND_UP_2 (height) / 2;
1388       }
1389     case GST_VIDEO_FORMAT_Y41B:
1390     case GST_VIDEO_FORMAT_Y42B:
1391     case GST_VIDEO_FORMAT_YUY2:
1392     case GST_VIDEO_FORMAT_YVYU:
1393     case GST_VIDEO_FORMAT_UYVY:
1394     case GST_VIDEO_FORMAT_AYUV:
1395     case GST_VIDEO_FORMAT_RGBx:
1396     case GST_VIDEO_FORMAT_BGRx:
1397     case GST_VIDEO_FORMAT_xRGB:
1398     case GST_VIDEO_FORMAT_xBGR:
1399     case GST_VIDEO_FORMAT_RGBA:
1400     case GST_VIDEO_FORMAT_BGRA:
1401     case GST_VIDEO_FORMAT_ARGB:
1402     case GST_VIDEO_FORMAT_ABGR:
1403     case GST_VIDEO_FORMAT_RGB:
1404     case GST_VIDEO_FORMAT_BGR:
1405     case GST_VIDEO_FORMAT_RGB16:
1406     case GST_VIDEO_FORMAT_BGR16:
1407     case GST_VIDEO_FORMAT_RGB15:
1408     case GST_VIDEO_FORMAT_BGR15:
1409     case GST_VIDEO_FORMAT_Y444:
1410     case GST_VIDEO_FORMAT_v210:
1411     case GST_VIDEO_FORMAT_v216:
1412     case GST_VIDEO_FORMAT_v308:
1413     case GST_VIDEO_FORMAT_GRAY8:
1414     case GST_VIDEO_FORMAT_GRAY16_BE:
1415     case GST_VIDEO_FORMAT_GRAY16_LE:
1416     case GST_VIDEO_FORMAT_Y800:
1417     case GST_VIDEO_FORMAT_Y16:
1418       return height;
1419     default:
1420       return 0;
1421   }
1422 }
1423
1424 /**
1425  * gst_video_format_get_component_offset:
1426  * @format: a #GstVideoFormat
1427  * @component: the component index
1428  * @width: the width of video
1429  * @height: the height of video
1430  *
1431  * Calculates the offset (in bytes) of the first pixel of the component
1432  * with index @component.  For packed formats, this will typically be a
1433  * small integer (0, 1, 2, 3).  For planar formats, this will be a
1434  * (relatively) large offset to the beginning of the second or third
1435  * component planes.  See @gst_video_format_get_row_stride for a description
1436  * of the component index.
1437  *
1438  * Since: 0.10.16
1439  *
1440  * Returns: offset of component @component
1441  */
1442 int
1443 gst_video_format_get_component_offset (GstVideoFormat format, int component,
1444     int width, int height)
1445 {
1446   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1447   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1448   g_return_val_if_fail (width > 0 && height > 0, 0);
1449
1450   switch (format) {
1451     case GST_VIDEO_FORMAT_I420:
1452       if (component == 0)
1453         return 0;
1454       if (component == 1)
1455         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1456       if (component == 2) {
1457         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1458             GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1459             (GST_ROUND_UP_2 (height) / 2);
1460       }
1461       return 0;
1462     case GST_VIDEO_FORMAT_YV12:        /* same as I420, but components 1+2 swapped */
1463       if (component == 0)
1464         return 0;
1465       if (component == 2)
1466         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1467       if (component == 1) {
1468         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1469             GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1470             (GST_ROUND_UP_2 (height) / 2);
1471       }
1472       return 0;
1473     case GST_VIDEO_FORMAT_YUY2:
1474       if (component == 0)
1475         return 0;
1476       if (component == 1)
1477         return 1;
1478       if (component == 2)
1479         return 3;
1480       return 0;
1481     case GST_VIDEO_FORMAT_YVYU:
1482       if (component == 0)
1483         return 0;
1484       if (component == 1)
1485         return 3;
1486       if (component == 2)
1487         return 1;
1488       return 0;
1489     case GST_VIDEO_FORMAT_UYVY:
1490       if (component == 0)
1491         return 1;
1492       if (component == 1)
1493         return 0;
1494       if (component == 2)
1495         return 2;
1496       return 0;
1497     case GST_VIDEO_FORMAT_AYUV:
1498       if (component == 0)
1499         return 1;
1500       if (component == 1)
1501         return 2;
1502       if (component == 2)
1503         return 3;
1504       if (component == 3)
1505         return 0;
1506       return 0;
1507     case GST_VIDEO_FORMAT_RGBx:
1508     case GST_VIDEO_FORMAT_RGBA:
1509       if (component == 0)
1510         return 0;
1511       if (component == 1)
1512         return 1;
1513       if (component == 2)
1514         return 2;
1515       if (component == 3)
1516         return 3;
1517       return 0;
1518     case GST_VIDEO_FORMAT_BGRx:
1519     case GST_VIDEO_FORMAT_BGRA:
1520       if (component == 0)
1521         return 2;
1522       if (component == 1)
1523         return 1;
1524       if (component == 2)
1525         return 0;
1526       if (component == 3)
1527         return 3;
1528       return 0;
1529     case GST_VIDEO_FORMAT_xRGB:
1530     case GST_VIDEO_FORMAT_ARGB:
1531       if (component == 0)
1532         return 1;
1533       if (component == 1)
1534         return 2;
1535       if (component == 2)
1536         return 3;
1537       if (component == 3)
1538         return 0;
1539       return 0;
1540     case GST_VIDEO_FORMAT_xBGR:
1541     case GST_VIDEO_FORMAT_ABGR:
1542       if (component == 0)
1543         return 3;
1544       if (component == 1)
1545         return 2;
1546       if (component == 2)
1547         return 1;
1548       if (component == 3)
1549         return 0;
1550       return 0;
1551     case GST_VIDEO_FORMAT_RGB:
1552     case GST_VIDEO_FORMAT_v308:
1553       if (component == 0)
1554         return 0;
1555       if (component == 1)
1556         return 1;
1557       if (component == 2)
1558         return 2;
1559       return 0;
1560     case GST_VIDEO_FORMAT_BGR:
1561       if (component == 0)
1562         return 2;
1563       if (component == 1)
1564         return 1;
1565       if (component == 2)
1566         return 0;
1567       return 0;
1568     case GST_VIDEO_FORMAT_Y41B:
1569       if (component == 0)
1570         return 0;
1571       if (component == 1)
1572         return GST_ROUND_UP_4 (width) * height;
1573       if (component == 2)
1574         return (GST_ROUND_UP_4 (width) +
1575             (GST_ROUND_UP_16 (width) / 4)) * height;
1576       return 0;
1577     case GST_VIDEO_FORMAT_Y42B:
1578       if (component == 0)
1579         return 0;
1580       if (component == 1)
1581         return GST_ROUND_UP_4 (width) * height;
1582       if (component == 2)
1583         return (GST_ROUND_UP_4 (width) + (GST_ROUND_UP_8 (width) / 2)) * height;
1584       return 0;
1585     case GST_VIDEO_FORMAT_Y444:
1586       return GST_ROUND_UP_4 (width) * height * component;
1587     case GST_VIDEO_FORMAT_v210:
1588       /* v210 is bit-packed, so this doesn't make sense */
1589       return 0;
1590     case GST_VIDEO_FORMAT_v216:
1591       if (component == 0)
1592         return 0;
1593       if (component == 1)
1594         return 2;
1595       if (component == 2)
1596         return 6;
1597       return 0;
1598     case GST_VIDEO_FORMAT_NV12:
1599       if (component == 0)
1600         return 0;
1601       if (component == 1)
1602         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1603       if (component == 2)
1604         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) + 1;
1605     case GST_VIDEO_FORMAT_NV21:
1606       if (component == 0)
1607         return 0;
1608       if (component == 1)
1609         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) + 1;
1610       if (component == 2)
1611         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1612     case GST_VIDEO_FORMAT_GRAY8:
1613     case GST_VIDEO_FORMAT_GRAY16_BE:
1614     case GST_VIDEO_FORMAT_GRAY16_LE:
1615     case GST_VIDEO_FORMAT_Y800:
1616     case GST_VIDEO_FORMAT_Y16:
1617       return 0;
1618     default:
1619       return 0;
1620   }
1621 }
1622
1623 /**
1624  * gst_video_format_get_size:
1625  * @format: a #GstVideoFormat
1626  * @width: the width of video
1627  * @height: the height of video
1628  *
1629  * Calculates the total number of bytes in the raw video format.  This
1630  * number should be used when allocating a buffer for raw video.
1631  *
1632  * Since: 0.10.16
1633  *
1634  * Returns: size (in bytes) of raw video format
1635  */
1636 int
1637 gst_video_format_get_size (GstVideoFormat format, int width, int height)
1638 {
1639   int size;
1640
1641   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1642   g_return_val_if_fail (width > 0 && height > 0, 0);
1643
1644   switch (format) {
1645     case GST_VIDEO_FORMAT_I420:
1646     case GST_VIDEO_FORMAT_YV12:
1647       size = GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1648       size += GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1649           (GST_ROUND_UP_2 (height) / 2) * 2;
1650       return size;
1651     case GST_VIDEO_FORMAT_YUY2:
1652     case GST_VIDEO_FORMAT_YVYU:
1653     case GST_VIDEO_FORMAT_UYVY:
1654       return GST_ROUND_UP_4 (width * 2) * height;
1655     case GST_VIDEO_FORMAT_AYUV:
1656     case GST_VIDEO_FORMAT_RGBx:
1657     case GST_VIDEO_FORMAT_BGRx:
1658     case GST_VIDEO_FORMAT_xRGB:
1659     case GST_VIDEO_FORMAT_xBGR:
1660     case GST_VIDEO_FORMAT_RGBA:
1661     case GST_VIDEO_FORMAT_BGRA:
1662     case GST_VIDEO_FORMAT_ARGB:
1663     case GST_VIDEO_FORMAT_ABGR:
1664       return width * 4 * height;
1665     case GST_VIDEO_FORMAT_RGB16:
1666     case GST_VIDEO_FORMAT_BGR16:
1667     case GST_VIDEO_FORMAT_RGB15:
1668     case GST_VIDEO_FORMAT_BGR15:
1669       return GST_ROUND_UP_4 (width * 2) * height;
1670     case GST_VIDEO_FORMAT_RGB:
1671     case GST_VIDEO_FORMAT_BGR:
1672     case GST_VIDEO_FORMAT_v308:
1673       return GST_ROUND_UP_4 (width * 3) * height;
1674     case GST_VIDEO_FORMAT_Y41B:
1675       /* simplification of ROUNDUP4(w)*h + 2*((ROUNDUP16(w)/4)*h */
1676       return (GST_ROUND_UP_4 (width) + (GST_ROUND_UP_16 (width) / 2)) * height;
1677     case GST_VIDEO_FORMAT_Y42B:
1678       /* simplification of ROUNDUP4(w)*h + 2*(ROUNDUP8(w)/2)*h */
1679       return (GST_ROUND_UP_4 (width) + GST_ROUND_UP_8 (width)) * height;
1680     case GST_VIDEO_FORMAT_Y444:
1681       return GST_ROUND_UP_4 (width) * height * 3;
1682     case GST_VIDEO_FORMAT_v210:
1683       return ((width + 47) / 48) * 128 * height;
1684     case GST_VIDEO_FORMAT_v216:
1685       return GST_ROUND_UP_8 (width * 4) * height;
1686     case GST_VIDEO_FORMAT_NV12:
1687     case GST_VIDEO_FORMAT_NV21:
1688       return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) * 3 / 2;
1689     case GST_VIDEO_FORMAT_GRAY8:
1690     case GST_VIDEO_FORMAT_Y800:
1691       return GST_ROUND_UP_4 (width) * height;
1692     case GST_VIDEO_FORMAT_GRAY16_BE:
1693     case GST_VIDEO_FORMAT_GRAY16_LE:
1694     case GST_VIDEO_FORMAT_Y16:
1695       return GST_ROUND_UP_4 (width * 2) * height;
1696     default:
1697       return 0;
1698   }
1699 }
1700
1701 /**
1702  * gst_video_format_convert:
1703  * @format: a #GstVideoFormat
1704  * @width: the width of video
1705  * @height: the height of video
1706  * @fps_n: frame rate numerator
1707  * @fps_d: frame rate denominator
1708  * @src_format: #GstFormat of the @src_value
1709  * @src_value: value to convert
1710  * @dest_format: #GstFormat of the @dest_value
1711  * @dest_value: pointer to destination value
1712  *
1713  * Converts among various #GstFormat types.  This function handles
1714  * GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT.  For
1715  * raw video, GST_FORMAT_DEFAULT corresponds to video frames.  This
1716  * function can be to handle pad queries of the type GST_QUERY_CONVERT.
1717  *
1718  * Since: 0.10.16
1719  *
1720  * Returns: TRUE if the conversion was successful.
1721  */
1722 gboolean
1723 gst_video_format_convert (GstVideoFormat format, int width, int height,
1724     int fps_n, int fps_d,
1725     GstFormat src_format, gint64 src_value,
1726     GstFormat dest_format, gint64 * dest_value)
1727 {
1728   gboolean ret = FALSE;
1729   int size;
1730
1731   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1732   g_return_val_if_fail (width > 0 && height > 0, 0);
1733
1734   size = gst_video_format_get_size (format, width, height);
1735
1736   GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s to %s",
1737       src_value, gst_format_get_name (src_format),
1738       gst_format_get_name (dest_format));
1739
1740   if (src_format == dest_format) {
1741     *dest_value = src_value;
1742     ret = TRUE;
1743     goto done;
1744   }
1745
1746   if (src_value == -1) {
1747     *dest_value = -1;
1748     ret = TRUE;
1749     goto done;
1750   }
1751
1752   /* bytes to frames */
1753   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_DEFAULT) {
1754     if (size != 0) {
1755       *dest_value = gst_util_uint64_scale_int (src_value, 1, size);
1756     } else {
1757       GST_ERROR ("blocksize is 0");
1758       *dest_value = 0;
1759     }
1760     ret = TRUE;
1761     goto done;
1762   }
1763
1764   /* frames to bytes */
1765   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_BYTES) {
1766     *dest_value = gst_util_uint64_scale_int (src_value, size, 1);
1767     ret = TRUE;
1768     goto done;
1769   }
1770
1771   /* time to frames */
1772   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_DEFAULT) {
1773     if (fps_d != 0) {
1774       *dest_value = gst_util_uint64_scale (src_value,
1775           fps_n, GST_SECOND * fps_d);
1776     } else {
1777       GST_ERROR ("framerate denominator is 0");
1778       *dest_value = 0;
1779     }
1780     ret = TRUE;
1781     goto done;
1782   }
1783
1784   /* frames to time */
1785   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) {
1786     if (fps_n != 0) {
1787       *dest_value = gst_util_uint64_scale (src_value,
1788           GST_SECOND * fps_d, fps_n);
1789     } else {
1790       GST_ERROR ("framerate numerator is 0");
1791       *dest_value = 0;
1792     }
1793     ret = TRUE;
1794     goto done;
1795   }
1796
1797   /* time to bytes */
1798   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
1799     if (fps_d != 0) {
1800       *dest_value = gst_util_uint64_scale (src_value,
1801           fps_n * size, GST_SECOND * fps_d);
1802     } else {
1803       GST_ERROR ("framerate denominator is 0");
1804       *dest_value = 0;
1805     }
1806     ret = TRUE;
1807     goto done;
1808   }
1809
1810   /* bytes to time */
1811   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
1812     if (fps_n != 0 && size != 0) {
1813       *dest_value = gst_util_uint64_scale (src_value,
1814           GST_SECOND * fps_d, fps_n * size);
1815     } else {
1816       GST_ERROR ("framerate denominator and/or blocksize is 0");
1817       *dest_value = 0;
1818     }
1819     ret = TRUE;
1820   }
1821
1822 done:
1823
1824   GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, ret, *dest_value);
1825
1826   return ret;
1827 }
1828
1829 #define GST_VIDEO_EVENT_STILL_STATE_NAME "GstEventStillFrame"
1830
1831 /**
1832  * gst_video_event_new_still_frame:
1833  * @in_still: boolean value for the still-frame state of the event.
1834  *
1835  * Creates a new Still Frame event. If @in_still is %TRUE, then the event
1836  * represents the start of a still frame sequence. If it is %FALSE, then
1837  * the event ends a still frame sequence.
1838  *
1839  * To parse an event created by gst_video_event_new_still_frame() use
1840  * gst_video_event_parse_still_frame().
1841  *
1842  * Returns: The new GstEvent
1843  * Since: 0.10.26
1844  */
1845 GstEvent *
1846 gst_video_event_new_still_frame (gboolean in_still)
1847 {
1848   GstEvent *still_event;
1849   GstStructure *s;
1850
1851   s = gst_structure_new (GST_VIDEO_EVENT_STILL_STATE_NAME,
1852       "still-state", G_TYPE_BOOLEAN, in_still, NULL);
1853   still_event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1854
1855   return still_event;
1856 }
1857
1858 /**
1859  * gst_video_event_parse_still_frame:
1860  * @event: A #GstEvent to parse
1861  * @in_still: A boolean to receive the still-frame status from the event, or NULL
1862  *
1863  * Parse a #GstEvent, identify if it is a Still Frame event, and
1864  * return the still-frame state from the event if it is.
1865  * If the event represents the start of a still frame, the in_still
1866  * variable will be set to TRUE, otherwise FALSE. It is OK to pass NULL for the
1867  * in_still variable order to just check whether the event is a valid still-frame
1868  * event.
1869  *
1870  * Create a still frame event using gst_video_event_new_still_frame()
1871  *
1872  * Returns: %TRUE if the event is a valid still-frame event. %FALSE if not
1873  * Since: 0.10.26
1874  */
1875 gboolean
1876 gst_video_event_parse_still_frame (GstEvent * event, gboolean * in_still)
1877 {
1878   const GstStructure *s;
1879   gboolean ev_still_state;
1880
1881   g_return_val_if_fail (event != NULL, FALSE);
1882
1883   if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM)
1884     return FALSE;               /* Not a still frame event */
1885
1886   s = gst_event_get_structure (event);
1887   if (s == NULL
1888       || !gst_structure_has_name (s, GST_VIDEO_EVENT_STILL_STATE_NAME))
1889     return FALSE;               /* Not a still frame event */
1890   if (!gst_structure_get_boolean (s, "still-state", &ev_still_state))
1891     return FALSE;               /* Not a still frame event */
1892   if (in_still)
1893     *in_still = ev_still_state;
1894   return TRUE;
1895 }