Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / 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 = 0;
353       int red_mask = 0;
354       int green_mask = 0;
355       int blue_mask = 0;
356       int alpha_mask = 0;
357       gboolean have_alpha;
358
359       ok &= gst_structure_get_int (structure, "depth", &depth);
360       ok &= gst_structure_get_int (structure, "bpp", &bpp);
361
362       if (bpp != 8) {
363         ok &= gst_structure_get_int (structure, "endianness", &endianness);
364         ok &= gst_structure_get_int (structure, "red_mask", &red_mask);
365         ok &= gst_structure_get_int (structure, "green_mask", &green_mask);
366         ok &= gst_structure_get_int (structure, "blue_mask", &blue_mask);
367       }
368       have_alpha = gst_structure_get_int (structure, "alpha_mask", &alpha_mask);
369
370       if (depth == 30 && bpp == 32 && endianness == G_BIG_ENDIAN) {
371         *format = GST_VIDEO_FORMAT_r210;
372       } else if (depth == 24 && bpp == 32 && endianness == G_BIG_ENDIAN) {
373         *format = gst_video_format_from_rgb32_masks (red_mask, green_mask,
374             blue_mask);
375         if (*format == GST_VIDEO_FORMAT_UNKNOWN) {
376           ok = FALSE;
377         }
378       } else if (depth == 32 && bpp == 32 && endianness == G_BIG_ENDIAN &&
379           have_alpha) {
380         *format = gst_video_format_from_rgba32_masks (red_mask, green_mask,
381             blue_mask, alpha_mask);
382         if (*format == GST_VIDEO_FORMAT_UNKNOWN) {
383           ok = FALSE;
384         }
385       } else if (depth == 24 && bpp == 24 && endianness == G_BIG_ENDIAN) {
386         *format = gst_video_format_from_rgb24_masks (red_mask, green_mask,
387             blue_mask);
388         if (*format == GST_VIDEO_FORMAT_UNKNOWN) {
389           ok = FALSE;
390         }
391       } else if ((depth == 15 || depth == 16) && bpp == 16 &&
392           endianness == G_BYTE_ORDER) {
393         *format = gst_video_format_from_rgb16_masks (red_mask, green_mask,
394             blue_mask);
395         if (*format == GST_VIDEO_FORMAT_UNKNOWN) {
396           ok = FALSE;
397         }
398       } else if (depth == 8 && bpp == 8) {
399         *format = GST_VIDEO_FORMAT_RGB8_PALETTED;
400       } else if (depth == 64 && bpp == 64) {
401         *format = gst_video_format_from_rgba32_masks (red_mask, green_mask,
402             blue_mask, alpha_mask);
403         if (*format == GST_VIDEO_FORMAT_ARGB) {
404           *format = GST_VIDEO_FORMAT_ARGB64;
405         } else {
406           *format = GST_VIDEO_FORMAT_UNKNOWN;
407           ok = FALSE;
408         }
409       } else {
410         ok = FALSE;
411       }
412     } else if (gst_structure_has_name (structure, "video/x-raw-gray")) {
413       int depth;
414       int bpp;
415       int endianness;
416
417       ok &= gst_structure_get_int (structure, "depth", &depth);
418       ok &= gst_structure_get_int (structure, "bpp", &bpp);
419
420       if (bpp > 8)
421         ok &= gst_structure_get_int (structure, "endianness", &endianness);
422
423       if (depth == 8 && bpp == 8) {
424         *format = GST_VIDEO_FORMAT_GRAY8;
425       } else if (depth == 16 && bpp == 16 && endianness == G_BIG_ENDIAN) {
426         *format = GST_VIDEO_FORMAT_GRAY16_BE;
427       } else if (depth == 16 && bpp == 16 && endianness == G_LITTLE_ENDIAN) {
428         *format = GST_VIDEO_FORMAT_GRAY16_LE;
429       } else {
430         ok = FALSE;
431       }
432     } else {
433       ok = FALSE;
434     }
435   }
436
437   if (width) {
438     ok &= gst_structure_get_int (structure, "width", width);
439   }
440
441   if (height) {
442     ok &= gst_structure_get_int (structure, "height", height);
443   }
444
445   return ok;
446 }
447
448
449 /**
450  * gst_video_parse_caps_framerate:
451  * @caps: pointer to a #GstCaps instance
452  * @fps_n: pointer to integer to hold numerator of frame rate (output)
453  * @fps_d: pointer to integer to hold denominator of frame rate (output)
454  *
455  * Extracts the frame rate from @caps and places the values in the locations
456  * pointed to by @fps_n and @fps_d.  Returns TRUE if the values could be
457  * parsed correctly, FALSE if not.
458  *
459  * This function can be used with #GstCaps that have any media type; it
460  * is not limited to formats handled by #GstVideoFormat.
461  *
462  * Since: 0.10.16
463  *
464  * Returns: TRUE if @caps was parsed correctly.
465  */
466 gboolean
467 gst_video_parse_caps_framerate (GstCaps * caps, int *fps_n, int *fps_d)
468 {
469   GstStructure *structure;
470
471   if (!gst_caps_is_fixed (caps))
472     return FALSE;
473
474   structure = gst_caps_get_structure (caps, 0);
475
476   return gst_structure_get_fraction (structure, "framerate", fps_n, fps_d);
477 }
478
479 /**
480  * gst_video_parse_caps_pixel_aspect_ratio:
481  * @caps: pointer to a #GstCaps instance
482  * @par_n: pointer to numerator of pixel aspect ratio (output)
483  * @par_d: pointer to denominator of pixel aspect ratio (output)
484  *
485  * Extracts the pixel aspect ratio from @caps and places the values in
486  * the locations pointed to by @par_n and @par_d.  Returns TRUE if the
487  * values could be parsed correctly, FALSE if not.
488  *
489  * This function can be used with #GstCaps that have any media type; it
490  * is not limited to formats handled by #GstVideoFormat.
491  *
492  * Since: 0.10.16
493  *
494  * Returns: TRUE if @caps was parsed correctly.
495  */
496 gboolean
497 gst_video_parse_caps_pixel_aspect_ratio (GstCaps * caps, int *par_n, int *par_d)
498 {
499   GstStructure *structure;
500
501   if (!gst_caps_is_fixed (caps))
502     return FALSE;
503
504   structure = gst_caps_get_structure (caps, 0);
505
506   if (!gst_structure_get_fraction (structure, "pixel-aspect-ratio",
507           par_n, par_d)) {
508     *par_n = 1;
509     *par_d = 1;
510   }
511   return TRUE;
512 }
513
514 /**
515  * gst_video_format_new_caps_interlaced:
516  * @format: the #GstVideoFormat describing the raw video format
517  * @width: width of video
518  * @height: height of video
519  * @framerate_n: numerator of frame rate
520  * @framerate_d: denominator of frame rate
521  * @par_n: numerator of pixel aspect ratio
522  * @par_d: denominator of pixel aspect ratio
523  * @interlaced: #TRUE if the format is interlaced
524  *
525  * Creates a new #GstCaps object based on the parameters provided.
526  *
527  * Since: 0.10.23
528  *
529  * Returns: a new #GstCaps object, or NULL if there was an error
530  */
531 GstCaps *
532 gst_video_format_new_caps_interlaced (GstVideoFormat format,
533     int width, int height, int framerate_n, int framerate_d, int par_n,
534     int par_d, gboolean interlaced)
535 {
536   GstCaps *res;
537
538   res =
539       gst_video_format_new_caps (format, width, height, framerate_n,
540       framerate_d, par_n, par_d);
541   if (interlaced && (res != NULL))
542     gst_caps_set_simple (res, "interlaced", G_TYPE_BOOLEAN, TRUE, NULL);
543
544   return res;
545 }
546
547 static GstCaps *
548 gst_video_format_new_caps_raw (GstVideoFormat format)
549 {
550   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
551
552   if (gst_video_format_is_yuv (format)) {
553     return gst_caps_new_simple ("video/x-raw-yuv",
554         "format", GST_TYPE_FOURCC, gst_video_format_to_fourcc (format), NULL);
555   }
556   if (gst_video_format_is_rgb (format)) {
557     GstCaps *caps;
558     int red_mask = 0;
559     int blue_mask = 0;
560     int green_mask = 0;
561     int alpha_mask;
562     int depth;
563     int bpp;
564     gboolean have_alpha;
565     unsigned int mask = 0;
566
567     switch (format) {
568       case GST_VIDEO_FORMAT_RGBx:
569       case GST_VIDEO_FORMAT_BGRx:
570       case GST_VIDEO_FORMAT_xRGB:
571       case GST_VIDEO_FORMAT_xBGR:
572         bpp = 32;
573         depth = 24;
574         have_alpha = FALSE;
575         break;
576       case GST_VIDEO_FORMAT_RGBA:
577       case GST_VIDEO_FORMAT_BGRA:
578       case GST_VIDEO_FORMAT_ARGB:
579       case GST_VIDEO_FORMAT_ABGR:
580         bpp = 32;
581         depth = 32;
582         have_alpha = TRUE;
583         break;
584       case GST_VIDEO_FORMAT_RGB:
585       case GST_VIDEO_FORMAT_BGR:
586         bpp = 24;
587         depth = 24;
588         have_alpha = FALSE;
589         break;
590       case GST_VIDEO_FORMAT_RGB16:
591       case GST_VIDEO_FORMAT_BGR16:
592         bpp = 16;
593         depth = 16;
594         have_alpha = FALSE;
595         break;
596       case GST_VIDEO_FORMAT_RGB15:
597       case GST_VIDEO_FORMAT_BGR15:
598         bpp = 16;
599         depth = 15;
600         have_alpha = FALSE;
601         break;
602       case GST_VIDEO_FORMAT_RGB8_PALETTED:
603         bpp = 8;
604         depth = 8;
605         have_alpha = FALSE;
606         break;
607       case GST_VIDEO_FORMAT_ARGB64:
608         bpp = 64;
609         depth = 64;
610         have_alpha = TRUE;
611         break;
612       case GST_VIDEO_FORMAT_r210:
613         bpp = 32;
614         depth = 30;
615         have_alpha = FALSE;
616         break;
617       default:
618         return NULL;
619     }
620     if (bpp == 32 && depth == 30) {
621       red_mask = 0x3ff00000;
622       green_mask = 0x000ffc00;
623       blue_mask = 0x000003ff;
624       have_alpha = FALSE;
625     } else if (bpp == 32 || bpp == 24 || bpp == 64) {
626       if (bpp == 32) {
627         mask = 0xff000000;
628       } else {
629         mask = 0xff0000;
630       }
631       red_mask =
632           mask >> (8 * gst_video_format_get_component_offset (format, 0, 0, 0));
633       green_mask =
634           mask >> (8 * gst_video_format_get_component_offset (format, 1, 0, 0));
635       blue_mask =
636           mask >> (8 * gst_video_format_get_component_offset (format, 2, 0, 0));
637     } else if (bpp == 16) {
638       switch (format) {
639         case GST_VIDEO_FORMAT_RGB16:
640           red_mask = GST_VIDEO_COMP1_MASK_16_INT;
641           green_mask = GST_VIDEO_COMP2_MASK_16_INT;
642           blue_mask = GST_VIDEO_COMP3_MASK_16_INT;
643           break;
644         case GST_VIDEO_FORMAT_BGR16:
645           red_mask = GST_VIDEO_COMP3_MASK_16_INT;
646           green_mask = GST_VIDEO_COMP2_MASK_16_INT;
647           blue_mask = GST_VIDEO_COMP1_MASK_16_INT;
648           break;
649           break;
650         case GST_VIDEO_FORMAT_RGB15:
651           red_mask = GST_VIDEO_COMP1_MASK_15_INT;
652           green_mask = GST_VIDEO_COMP2_MASK_15_INT;
653           blue_mask = GST_VIDEO_COMP3_MASK_15_INT;
654           break;
655         case GST_VIDEO_FORMAT_BGR15:
656           red_mask = GST_VIDEO_COMP3_MASK_15_INT;
657           green_mask = GST_VIDEO_COMP2_MASK_15_INT;
658           blue_mask = GST_VIDEO_COMP1_MASK_15_INT;
659           break;
660         default:
661           return NULL;
662       }
663     } else if (bpp != 8) {
664       return NULL;
665     }
666
667     caps = gst_caps_new_simple ("video/x-raw-rgb",
668         "bpp", G_TYPE_INT, bpp, "depth", G_TYPE_INT, depth, NULL);
669
670     if (bpp != 8) {
671       gst_caps_set_simple (caps,
672           "endianness", G_TYPE_INT, G_BIG_ENDIAN,
673           "red_mask", G_TYPE_INT, red_mask,
674           "green_mask", G_TYPE_INT, green_mask,
675           "blue_mask", G_TYPE_INT, blue_mask, NULL);
676     }
677
678     if (have_alpha) {
679       alpha_mask =
680           mask >> (8 * gst_video_format_get_component_offset (format, 3, 0, 0));
681       gst_caps_set_simple (caps, "alpha_mask", G_TYPE_INT, alpha_mask, NULL);
682     }
683     return caps;
684   }
685
686   if (gst_video_format_is_gray (format)) {
687     GstCaps *caps;
688     int bpp;
689     int depth;
690     int endianness;
691
692     switch (format) {
693       case GST_VIDEO_FORMAT_GRAY8:
694         bpp = depth = 8;
695         endianness = G_BIG_ENDIAN;
696         break;
697       case GST_VIDEO_FORMAT_GRAY16_BE:
698         bpp = depth = 16;
699         endianness = G_BIG_ENDIAN;
700         break;
701       case GST_VIDEO_FORMAT_GRAY16_LE:
702         bpp = depth = 16;
703         endianness = G_LITTLE_ENDIAN;
704         break;
705       default:
706         return NULL;
707         break;
708     }
709
710     if (bpp <= 8) {
711       caps = gst_caps_new_simple ("video/x-raw-gray",
712           "bpp", G_TYPE_INT, bpp, "depth", G_TYPE_INT, depth, NULL);
713     } else {
714       caps = gst_caps_new_simple ("video/x-raw-gray",
715           "bpp", G_TYPE_INT, bpp,
716           "depth", G_TYPE_INT, depth,
717           "endianness", G_TYPE_INT, endianness, NULL);
718     }
719
720     return caps;
721   }
722
723   return NULL;
724 }
725
726 /**
727  * gst_video_format_new_template_caps:
728  * @format: the #GstVideoFormat describing the raw video format
729  *
730  * Creates a new #GstCaps object based on the parameters provided.
731  * Size, frame rate, and pixel aspect ratio are set to the full
732  * range.
733  *
734  * Since: 0.10.33
735  *
736  * Returns: a new #GstCaps object, or NULL if there was an error
737  */
738 GstCaps *
739 gst_video_format_new_template_caps (GstVideoFormat format)
740 {
741   GstCaps *caps;
742   GstStructure *structure;
743
744   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
745
746   caps = gst_video_format_new_caps_raw (format);
747   if (caps) {
748     GValue value = { 0 };
749     GValue v = { 0 };
750
751     structure = gst_caps_get_structure (caps, 0);
752
753     gst_structure_set (structure,
754         "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
755         "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
756         "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1,
757         "pixel-aspect-ratio", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
758
759     g_value_init (&value, GST_TYPE_LIST);
760     g_value_init (&v, G_TYPE_BOOLEAN);
761     g_value_set_boolean (&v, TRUE);
762     gst_value_list_append_value (&value, &v);
763     g_value_set_boolean (&v, FALSE);
764     gst_value_list_append_value (&value, &v);
765
766     gst_structure_set_value (structure, "interlaced", &value);
767
768     g_value_reset (&value);
769     g_value_reset (&v);
770   }
771
772   return caps;
773 }
774
775 /**
776  * gst_video_format_new_caps:
777  * @format: the #GstVideoFormat describing the raw video format
778  * @width: width of video
779  * @height: height of video
780  * @framerate_n: numerator of frame rate
781  * @framerate_d: denominator of frame rate
782  * @par_n: numerator of pixel aspect ratio
783  * @par_d: denominator of pixel aspect ratio
784  *
785  * Creates a new #GstCaps object based on the parameters provided.
786  *
787  * Since: 0.10.16
788  *
789  * Returns: a new #GstCaps object, or NULL if there was an error
790  */
791 GstCaps *
792 gst_video_format_new_caps (GstVideoFormat format, int width,
793     int height, int framerate_n, int framerate_d, int par_n, int par_d)
794 {
795   GstCaps *caps;
796   GstStructure *structure;
797
798   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
799   g_return_val_if_fail (width > 0 && height > 0, NULL);
800
801   caps = gst_video_format_new_caps_raw (format);
802   if (caps) {
803     structure = gst_caps_get_structure (caps, 0);
804
805     gst_structure_set (structure,
806         "width", G_TYPE_INT, width,
807         "height", G_TYPE_INT, height,
808         "framerate", GST_TYPE_FRACTION, framerate_n, framerate_d,
809         "pixel-aspect-ratio", GST_TYPE_FRACTION, par_n, par_d, NULL);
810   }
811
812   return caps;
813 }
814
815
816 /**
817  * gst_video_format_from_fourcc:
818  * @fourcc: a FOURCC value representing raw YUV video
819  *
820  * Converts a FOURCC value into the corresponding #GstVideoFormat.
821  * If the FOURCC cannot be represented by #GstVideoFormat,
822  * #GST_VIDEO_FORMAT_UNKNOWN is returned.
823  *
824  * Since: 0.10.16
825  *
826  * Returns: the #GstVideoFormat describing the FOURCC value
827  */
828 GstVideoFormat
829 gst_video_format_from_fourcc (guint32 fourcc)
830 {
831   switch (fourcc) {
832     case GST_MAKE_FOURCC ('I', '4', '2', '0'):
833       return GST_VIDEO_FORMAT_I420;
834     case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
835       return GST_VIDEO_FORMAT_YV12;
836     case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
837       return GST_VIDEO_FORMAT_YUY2;
838     case GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'):
839       return GST_VIDEO_FORMAT_YVYU;
840     case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
841       return GST_VIDEO_FORMAT_UYVY;
842     case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
843       return GST_VIDEO_FORMAT_AYUV;
844     case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
845       return GST_VIDEO_FORMAT_Y41B;
846     case GST_MAKE_FOURCC ('Y', '4', '2', 'B'):
847       return GST_VIDEO_FORMAT_Y42B;
848     case GST_MAKE_FOURCC ('Y', '4', '4', '4'):
849       return GST_VIDEO_FORMAT_Y444;
850     case GST_MAKE_FOURCC ('v', '2', '1', '0'):
851       return GST_VIDEO_FORMAT_v210;
852     case GST_MAKE_FOURCC ('v', '2', '1', '6'):
853       return GST_VIDEO_FORMAT_v216;
854     case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
855       return GST_VIDEO_FORMAT_NV12;
856     case GST_MAKE_FOURCC ('N', 'V', '2', '1'):
857       return GST_VIDEO_FORMAT_NV21;
858     case GST_MAKE_FOURCC ('v', '3', '0', '8'):
859       return GST_VIDEO_FORMAT_v308;
860     case GST_MAKE_FOURCC ('Y', '8', '0', '0'):
861     case GST_MAKE_FOURCC ('Y', '8', ' ', ' '):
862     case GST_MAKE_FOURCC ('G', 'R', 'E', 'Y'):
863       return GST_VIDEO_FORMAT_Y800;
864     case GST_MAKE_FOURCC ('Y', '1', '6', ' '):
865       return GST_VIDEO_FORMAT_Y16;
866     case GST_MAKE_FOURCC ('U', 'Y', 'V', 'P'):
867       return GST_VIDEO_FORMAT_UYVP;
868     case GST_MAKE_FOURCC ('A', '4', '2', '0'):
869       return GST_VIDEO_FORMAT_A420;
870     case GST_MAKE_FOURCC ('Y', 'U', 'V', '9'):
871       return GST_VIDEO_FORMAT_YUV9;
872     case GST_MAKE_FOURCC ('Y', 'V', 'U', '9'):
873       return GST_VIDEO_FORMAT_YVU9;
874     case GST_MAKE_FOURCC ('I', 'Y', 'U', '1'):
875       return GST_VIDEO_FORMAT_IYU1;
876     case GST_MAKE_FOURCC ('A', 'Y', '6', '4'):
877       return GST_VIDEO_FORMAT_AYUV64;
878     default:
879       return GST_VIDEO_FORMAT_UNKNOWN;
880   }
881 }
882
883 /**
884  * gst_video_format_to_fourcc:
885  * @format: a #GstVideoFormat video format
886  *
887  * Converts a #GstVideoFormat value into the corresponding FOURCC.  Only
888  * a few YUV formats have corresponding FOURCC values.  If @format has
889  * no corresponding FOURCC value, 0 is returned.
890  *
891  * Since: 0.10.16
892  *
893  * Returns: the FOURCC corresponding to @format
894  */
895 guint32
896 gst_video_format_to_fourcc (GstVideoFormat format)
897 {
898   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
899
900   switch (format) {
901     case GST_VIDEO_FORMAT_I420:
902       return GST_MAKE_FOURCC ('I', '4', '2', '0');
903     case GST_VIDEO_FORMAT_YV12:
904       return GST_MAKE_FOURCC ('Y', 'V', '1', '2');
905     case GST_VIDEO_FORMAT_YUY2:
906       return GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
907     case GST_VIDEO_FORMAT_YVYU:
908       return GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U');
909     case GST_VIDEO_FORMAT_UYVY:
910       return GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
911     case GST_VIDEO_FORMAT_AYUV:
912       return GST_MAKE_FOURCC ('A', 'Y', 'U', 'V');
913     case GST_VIDEO_FORMAT_Y41B:
914       return GST_MAKE_FOURCC ('Y', '4', '1', 'B');
915     case GST_VIDEO_FORMAT_Y42B:
916       return GST_MAKE_FOURCC ('Y', '4', '2', 'B');
917     case GST_VIDEO_FORMAT_Y444:
918       return GST_MAKE_FOURCC ('Y', '4', '4', '4');
919     case GST_VIDEO_FORMAT_v210:
920       return GST_MAKE_FOURCC ('v', '2', '1', '0');
921     case GST_VIDEO_FORMAT_v216:
922       return GST_MAKE_FOURCC ('v', '2', '1', '6');
923     case GST_VIDEO_FORMAT_NV12:
924       return GST_MAKE_FOURCC ('N', 'V', '1', '2');
925     case GST_VIDEO_FORMAT_NV21:
926       return GST_MAKE_FOURCC ('N', 'V', '2', '1');
927     case GST_VIDEO_FORMAT_v308:
928       return GST_MAKE_FOURCC ('v', '3', '0', '8');
929     case GST_VIDEO_FORMAT_Y800:
930       return GST_MAKE_FOURCC ('Y', '8', '0', '0');
931     case GST_VIDEO_FORMAT_Y16:
932       return GST_MAKE_FOURCC ('Y', '1', '6', ' ');
933     case GST_VIDEO_FORMAT_UYVP:
934       return GST_MAKE_FOURCC ('U', 'Y', 'V', 'P');
935     case GST_VIDEO_FORMAT_A420:
936       return GST_MAKE_FOURCC ('A', '4', '2', '0');
937     case GST_VIDEO_FORMAT_YUV9:
938       return GST_MAKE_FOURCC ('Y', 'U', 'V', '9');
939     case GST_VIDEO_FORMAT_YVU9:
940       return GST_MAKE_FOURCC ('Y', 'V', 'U', '9');
941     case GST_VIDEO_FORMAT_IYU1:
942       return GST_MAKE_FOURCC ('I', 'Y', 'U', '1');
943     case GST_VIDEO_FORMAT_AYUV64:
944       return GST_MAKE_FOURCC ('A', 'Y', '6', '4');
945     default:
946       return 0;
947   }
948 }
949
950 /*
951  * gst_video_format_from_rgb32_masks:
952  * @red_mask: red bit mask
953  * @green_mask: green bit mask
954  * @blue_mask: blue bit mask
955  *
956  * Converts red, green, blue bit masks into the corresponding
957  * #GstVideoFormat.  
958  *
959  * Since: 0.10.16
960  *
961  * Returns: the #GstVideoFormat corresponding to the bit masks
962  */
963 static GstVideoFormat
964 gst_video_format_from_rgb32_masks (int red_mask, int green_mask, int blue_mask)
965 {
966   if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
967       blue_mask == 0x0000ff00) {
968     return GST_VIDEO_FORMAT_RGBx;
969   }
970   if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
971       blue_mask == 0xff000000) {
972     return GST_VIDEO_FORMAT_BGRx;
973   }
974   if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
975       blue_mask == 0x000000ff) {
976     return GST_VIDEO_FORMAT_xRGB;
977   }
978   if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
979       blue_mask == 0x00ff0000) {
980     return GST_VIDEO_FORMAT_xBGR;
981   }
982
983   return GST_VIDEO_FORMAT_UNKNOWN;
984 }
985
986 static GstVideoFormat
987 gst_video_format_from_rgba32_masks (int red_mask, int green_mask,
988     int blue_mask, int alpha_mask)
989 {
990   if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
991       blue_mask == 0x0000ff00 && alpha_mask == 0x000000ff) {
992     return GST_VIDEO_FORMAT_RGBA;
993   }
994   if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
995       blue_mask == 0xff000000 && alpha_mask == 0x000000ff) {
996     return GST_VIDEO_FORMAT_BGRA;
997   }
998   if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
999       blue_mask == 0x000000ff && alpha_mask == 0xff000000) {
1000     return GST_VIDEO_FORMAT_ARGB;
1001   }
1002   if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
1003       blue_mask == 0x00ff0000 && alpha_mask == 0xff000000) {
1004     return GST_VIDEO_FORMAT_ABGR;
1005   }
1006
1007   return GST_VIDEO_FORMAT_UNKNOWN;
1008 }
1009
1010 static GstVideoFormat
1011 gst_video_format_from_rgb24_masks (int red_mask, int green_mask, int blue_mask)
1012 {
1013   if (red_mask == 0xff0000 && green_mask == 0x00ff00 && blue_mask == 0x0000ff) {
1014     return GST_VIDEO_FORMAT_RGB;
1015   }
1016   if (red_mask == 0x0000ff && green_mask == 0x00ff00 && blue_mask == 0xff0000) {
1017     return GST_VIDEO_FORMAT_BGR;
1018   }
1019
1020   return GST_VIDEO_FORMAT_UNKNOWN;
1021 }
1022
1023 static GstVideoFormat
1024 gst_video_format_from_rgb16_masks (int red_mask, int green_mask, int blue_mask)
1025 {
1026   if (red_mask == GST_VIDEO_COMP1_MASK_16_INT
1027       && green_mask == GST_VIDEO_COMP2_MASK_16_INT
1028       && blue_mask == GST_VIDEO_COMP3_MASK_16_INT) {
1029     return GST_VIDEO_FORMAT_RGB16;
1030   }
1031   if (red_mask == GST_VIDEO_COMP3_MASK_16_INT
1032       && green_mask == GST_VIDEO_COMP2_MASK_16_INT
1033       && blue_mask == GST_VIDEO_COMP1_MASK_16_INT) {
1034     return GST_VIDEO_FORMAT_BGR16;
1035   }
1036   if (red_mask == GST_VIDEO_COMP1_MASK_15_INT
1037       && green_mask == GST_VIDEO_COMP2_MASK_15_INT
1038       && blue_mask == GST_VIDEO_COMP3_MASK_15_INT) {
1039     return GST_VIDEO_FORMAT_RGB15;
1040   }
1041   if (red_mask == GST_VIDEO_COMP3_MASK_15_INT
1042       && green_mask == GST_VIDEO_COMP2_MASK_15_INT
1043       && blue_mask == GST_VIDEO_COMP1_MASK_15_INT) {
1044     return GST_VIDEO_FORMAT_BGR15;
1045   }
1046
1047   return GST_VIDEO_FORMAT_UNKNOWN;
1048 }
1049
1050 /**
1051  * gst_video_format_is_rgb:
1052  * @format: a #GstVideoFormat
1053  *
1054  * Determine whether the video format is an RGB format.
1055  *
1056  * Since: 0.10.16
1057  *
1058  * Returns: TRUE if @format represents RGB video
1059  */
1060 gboolean
1061 gst_video_format_is_rgb (GstVideoFormat format)
1062 {
1063   switch (format) {
1064     case GST_VIDEO_FORMAT_I420:
1065     case GST_VIDEO_FORMAT_YV12:
1066     case GST_VIDEO_FORMAT_YUY2:
1067     case GST_VIDEO_FORMAT_YVYU:
1068     case GST_VIDEO_FORMAT_UYVY:
1069     case GST_VIDEO_FORMAT_AYUV:
1070     case GST_VIDEO_FORMAT_Y41B:
1071     case GST_VIDEO_FORMAT_Y42B:
1072     case GST_VIDEO_FORMAT_Y444:
1073     case GST_VIDEO_FORMAT_v210:
1074     case GST_VIDEO_FORMAT_v216:
1075     case GST_VIDEO_FORMAT_NV12:
1076     case GST_VIDEO_FORMAT_NV21:
1077     case GST_VIDEO_FORMAT_v308:
1078     case GST_VIDEO_FORMAT_UYVP:
1079     case GST_VIDEO_FORMAT_A420:
1080     case GST_VIDEO_FORMAT_YUV9:
1081     case GST_VIDEO_FORMAT_YVU9:
1082     case GST_VIDEO_FORMAT_IYU1:
1083     case GST_VIDEO_FORMAT_AYUV64:
1084       return FALSE;
1085     case GST_VIDEO_FORMAT_RGBx:
1086     case GST_VIDEO_FORMAT_BGRx:
1087     case GST_VIDEO_FORMAT_xRGB:
1088     case GST_VIDEO_FORMAT_xBGR:
1089     case GST_VIDEO_FORMAT_RGBA:
1090     case GST_VIDEO_FORMAT_BGRA:
1091     case GST_VIDEO_FORMAT_ARGB:
1092     case GST_VIDEO_FORMAT_ABGR:
1093     case GST_VIDEO_FORMAT_RGB:
1094     case GST_VIDEO_FORMAT_BGR:
1095     case GST_VIDEO_FORMAT_RGB16:
1096     case GST_VIDEO_FORMAT_BGR16:
1097     case GST_VIDEO_FORMAT_RGB15:
1098     case GST_VIDEO_FORMAT_BGR15:
1099     case GST_VIDEO_FORMAT_RGB8_PALETTED:
1100     case GST_VIDEO_FORMAT_ARGB64:
1101     case GST_VIDEO_FORMAT_r210:
1102       return TRUE;
1103     default:
1104       return FALSE;
1105   }
1106 }
1107
1108 /**
1109  * gst_video_format_is_yuv:
1110  * @format: a #GstVideoFormat
1111  *
1112  * Determine whether the video format is a YUV format.
1113  *
1114  * Since: 0.10.16
1115  *
1116  * Returns: TRUE if @format represents YUV video
1117  */
1118 gboolean
1119 gst_video_format_is_yuv (GstVideoFormat format)
1120 {
1121   switch (format) {
1122     case GST_VIDEO_FORMAT_I420:
1123     case GST_VIDEO_FORMAT_YV12:
1124     case GST_VIDEO_FORMAT_YUY2:
1125     case GST_VIDEO_FORMAT_YVYU:
1126     case GST_VIDEO_FORMAT_UYVY:
1127     case GST_VIDEO_FORMAT_AYUV:
1128     case GST_VIDEO_FORMAT_Y41B:
1129     case GST_VIDEO_FORMAT_Y42B:
1130     case GST_VIDEO_FORMAT_Y444:
1131     case GST_VIDEO_FORMAT_v210:
1132     case GST_VIDEO_FORMAT_v216:
1133     case GST_VIDEO_FORMAT_NV12:
1134     case GST_VIDEO_FORMAT_NV21:
1135     case GST_VIDEO_FORMAT_v308:
1136     case GST_VIDEO_FORMAT_Y800:
1137     case GST_VIDEO_FORMAT_Y16:
1138     case GST_VIDEO_FORMAT_UYVP:
1139     case GST_VIDEO_FORMAT_A420:
1140     case GST_VIDEO_FORMAT_YUV9:
1141     case GST_VIDEO_FORMAT_YVU9:
1142     case GST_VIDEO_FORMAT_IYU1:
1143     case GST_VIDEO_FORMAT_AYUV64:
1144       return TRUE;
1145     case GST_VIDEO_FORMAT_RGBx:
1146     case GST_VIDEO_FORMAT_BGRx:
1147     case GST_VIDEO_FORMAT_xRGB:
1148     case GST_VIDEO_FORMAT_xBGR:
1149     case GST_VIDEO_FORMAT_RGBA:
1150     case GST_VIDEO_FORMAT_BGRA:
1151     case GST_VIDEO_FORMAT_ARGB:
1152     case GST_VIDEO_FORMAT_ABGR:
1153     case GST_VIDEO_FORMAT_RGB:
1154     case GST_VIDEO_FORMAT_BGR:
1155     case GST_VIDEO_FORMAT_RGB16:
1156     case GST_VIDEO_FORMAT_BGR16:
1157     case GST_VIDEO_FORMAT_RGB15:
1158     case GST_VIDEO_FORMAT_BGR15:
1159     case GST_VIDEO_FORMAT_RGB8_PALETTED:
1160     case GST_VIDEO_FORMAT_ARGB64:
1161     case GST_VIDEO_FORMAT_r210:
1162       return FALSE;
1163     default:
1164       return FALSE;
1165   }
1166 }
1167
1168 /**
1169  * gst_video_format_is_gray:
1170  * @format: a #GstVideoFormat
1171  *
1172  * Determine whether the video format is a grayscale format.
1173  *
1174  * Since: 0.10.29
1175  *
1176  * Returns: TRUE if @format represents grayscale video
1177  */
1178 gboolean
1179 gst_video_format_is_gray (GstVideoFormat format)
1180 {
1181   switch (format) {
1182     case GST_VIDEO_FORMAT_GRAY8:
1183     case GST_VIDEO_FORMAT_GRAY16_BE:
1184     case GST_VIDEO_FORMAT_GRAY16_LE:
1185     case GST_VIDEO_FORMAT_Y800:
1186     case GST_VIDEO_FORMAT_Y16:
1187       return TRUE;
1188     default:
1189       return FALSE;
1190   }
1191 }
1192
1193 /**
1194  * gst_video_format_has_alpha:
1195  * @format: a #GstVideoFormat
1196  * 
1197  * Returns TRUE or FALSE depending on if the video format provides an
1198  * alpha channel.
1199  *
1200  * Since: 0.10.16
1201  *
1202  * Returns: TRUE if @format has an alpha channel
1203  */
1204 gboolean
1205 gst_video_format_has_alpha (GstVideoFormat format)
1206 {
1207   switch (format) {
1208     case GST_VIDEO_FORMAT_I420:
1209     case GST_VIDEO_FORMAT_YV12:
1210     case GST_VIDEO_FORMAT_YUY2:
1211     case GST_VIDEO_FORMAT_YVYU:
1212     case GST_VIDEO_FORMAT_UYVY:
1213     case GST_VIDEO_FORMAT_Y41B:
1214     case GST_VIDEO_FORMAT_Y42B:
1215     case GST_VIDEO_FORMAT_Y444:
1216     case GST_VIDEO_FORMAT_v210:
1217     case GST_VIDEO_FORMAT_v216:
1218     case GST_VIDEO_FORMAT_NV12:
1219     case GST_VIDEO_FORMAT_NV21:
1220     case GST_VIDEO_FORMAT_v308:
1221     case GST_VIDEO_FORMAT_Y800:
1222     case GST_VIDEO_FORMAT_Y16:
1223     case GST_VIDEO_FORMAT_UYVP:
1224     case GST_VIDEO_FORMAT_YUV9:
1225     case GST_VIDEO_FORMAT_YVU9:
1226     case GST_VIDEO_FORMAT_IYU1:
1227       return FALSE;
1228     case GST_VIDEO_FORMAT_AYUV:
1229     case GST_VIDEO_FORMAT_RGBA:
1230     case GST_VIDEO_FORMAT_BGRA:
1231     case GST_VIDEO_FORMAT_ARGB:
1232     case GST_VIDEO_FORMAT_ABGR:
1233     case GST_VIDEO_FORMAT_A420:
1234     case GST_VIDEO_FORMAT_RGB8_PALETTED:
1235     case GST_VIDEO_FORMAT_ARGB64:
1236     case GST_VIDEO_FORMAT_AYUV64:
1237       return TRUE;
1238     case GST_VIDEO_FORMAT_RGBx:
1239     case GST_VIDEO_FORMAT_BGRx:
1240     case GST_VIDEO_FORMAT_xRGB:
1241     case GST_VIDEO_FORMAT_xBGR:
1242     case GST_VIDEO_FORMAT_RGB:
1243     case GST_VIDEO_FORMAT_BGR:
1244     case GST_VIDEO_FORMAT_RGB16:
1245     case GST_VIDEO_FORMAT_BGR16:
1246     case GST_VIDEO_FORMAT_RGB15:
1247     case GST_VIDEO_FORMAT_BGR15:
1248     case GST_VIDEO_FORMAT_r210:
1249       return FALSE;
1250     default:
1251       return FALSE;
1252   }
1253 }
1254
1255 /**
1256  * gst_video_format_get_component_depth:
1257  * @format: a #GstVideoFormat
1258  * 
1259  * Returns the number of bits used to encode an individual pixel of
1260  * a given component.  Typically this is 8, although higher and lower
1261  * values are possible for some formats.
1262  *
1263  * Since: 0.10.33
1264  *
1265  * Returns: depth of component
1266  */
1267 int
1268 gst_video_format_get_component_depth (GstVideoFormat format, int component)
1269 {
1270   if (component == 3 && !gst_video_format_has_alpha (format))
1271     return 0;
1272
1273   switch (format) {
1274     case GST_VIDEO_FORMAT_RGB16:
1275     case GST_VIDEO_FORMAT_BGR16:
1276       if (component == 1)
1277         return 6;
1278       return 5;
1279     case GST_VIDEO_FORMAT_RGB15:
1280     case GST_VIDEO_FORMAT_BGR15:
1281       return 5;
1282     case GST_VIDEO_FORMAT_I420:
1283     case GST_VIDEO_FORMAT_YV12:
1284     case GST_VIDEO_FORMAT_YUY2:
1285     case GST_VIDEO_FORMAT_YVYU:
1286     case GST_VIDEO_FORMAT_UYVY:
1287     case GST_VIDEO_FORMAT_Y41B:
1288     case GST_VIDEO_FORMAT_Y42B:
1289     case GST_VIDEO_FORMAT_Y444:
1290     case GST_VIDEO_FORMAT_NV12:
1291     case GST_VIDEO_FORMAT_NV21:
1292     case GST_VIDEO_FORMAT_v308:
1293     case GST_VIDEO_FORMAT_Y800:
1294     case GST_VIDEO_FORMAT_YUV9:
1295     case GST_VIDEO_FORMAT_YVU9:
1296     case GST_VIDEO_FORMAT_IYU1:
1297     case GST_VIDEO_FORMAT_AYUV:
1298     case GST_VIDEO_FORMAT_RGBA:
1299     case GST_VIDEO_FORMAT_BGRA:
1300     case GST_VIDEO_FORMAT_ARGB:
1301     case GST_VIDEO_FORMAT_ABGR:
1302     case GST_VIDEO_FORMAT_A420:
1303     case GST_VIDEO_FORMAT_RGB8_PALETTED:
1304     case GST_VIDEO_FORMAT_RGBx:
1305     case GST_VIDEO_FORMAT_BGRx:
1306     case GST_VIDEO_FORMAT_xRGB:
1307     case GST_VIDEO_FORMAT_xBGR:
1308     case GST_VIDEO_FORMAT_RGB:
1309     case GST_VIDEO_FORMAT_BGR:
1310     default:
1311       return 8;
1312     case GST_VIDEO_FORMAT_v210:
1313     case GST_VIDEO_FORMAT_UYVP:
1314     case GST_VIDEO_FORMAT_r210:
1315       return 10;
1316     case GST_VIDEO_FORMAT_Y16:
1317     case GST_VIDEO_FORMAT_v216:
1318     case GST_VIDEO_FORMAT_ARGB64:
1319     case GST_VIDEO_FORMAT_AYUV64:
1320       return 16;
1321   }
1322
1323 }
1324
1325 /**
1326  * gst_video_format_get_row_stride:
1327  * @format: a #GstVideoFormat
1328  * @component: the component index
1329  * @width: the width of video
1330  *
1331  * Calculates the row stride (number of bytes from one row of pixels to
1332  * the next) for the video component with an index of @component.  For
1333  * YUV video, Y, U, and V have component indices of 0, 1, and 2,
1334  * respectively.  For RGB video, R, G, and B have component indicies of
1335  * 0, 1, and 2, respectively.  Alpha channels, if present, have a component
1336  * index of 3.  The @width parameter always represents the width of the
1337  * video, not the component.
1338  *
1339  * Since: 0.10.16
1340  *
1341  * Returns: row stride of component @component
1342  */
1343 int
1344 gst_video_format_get_row_stride (GstVideoFormat format, int component,
1345     int width)
1346 {
1347   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1348   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1349   g_return_val_if_fail (width > 0, 0);
1350
1351   switch (format) {
1352     case GST_VIDEO_FORMAT_I420:
1353     case GST_VIDEO_FORMAT_YV12:
1354       if (component == 0) {
1355         return GST_ROUND_UP_4 (width);
1356       } else {
1357         return GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1358       }
1359     case GST_VIDEO_FORMAT_YUY2:
1360     case GST_VIDEO_FORMAT_YVYU:
1361     case GST_VIDEO_FORMAT_UYVY:
1362       return GST_ROUND_UP_4 (width * 2);
1363     case GST_VIDEO_FORMAT_AYUV:
1364     case GST_VIDEO_FORMAT_RGBx:
1365     case GST_VIDEO_FORMAT_BGRx:
1366     case GST_VIDEO_FORMAT_xRGB:
1367     case GST_VIDEO_FORMAT_xBGR:
1368     case GST_VIDEO_FORMAT_RGBA:
1369     case GST_VIDEO_FORMAT_BGRA:
1370     case GST_VIDEO_FORMAT_ARGB:
1371     case GST_VIDEO_FORMAT_ABGR:
1372     case GST_VIDEO_FORMAT_r210:
1373       return width * 4;
1374     case GST_VIDEO_FORMAT_RGB16:
1375     case GST_VIDEO_FORMAT_BGR16:
1376     case GST_VIDEO_FORMAT_RGB15:
1377     case GST_VIDEO_FORMAT_BGR15:
1378       return GST_ROUND_UP_4 (width * 2);
1379     case GST_VIDEO_FORMAT_RGB:
1380     case GST_VIDEO_FORMAT_BGR:
1381     case GST_VIDEO_FORMAT_v308:
1382       return GST_ROUND_UP_4 (width * 3);
1383     case GST_VIDEO_FORMAT_Y41B:
1384       if (component == 0) {
1385         return GST_ROUND_UP_4 (width);
1386       } else {
1387         return GST_ROUND_UP_16 (width) / 4;
1388       }
1389     case GST_VIDEO_FORMAT_Y42B:
1390       if (component == 0) {
1391         return GST_ROUND_UP_4 (width);
1392       } else {
1393         return GST_ROUND_UP_8 (width) / 2;
1394       }
1395     case GST_VIDEO_FORMAT_Y444:
1396       return GST_ROUND_UP_4 (width);
1397     case GST_VIDEO_FORMAT_v210:
1398       return ((width + 47) / 48) * 128;
1399     case GST_VIDEO_FORMAT_v216:
1400       return GST_ROUND_UP_8 (width * 4);
1401     case GST_VIDEO_FORMAT_NV12:
1402     case GST_VIDEO_FORMAT_NV21:
1403       return GST_ROUND_UP_4 (width);
1404     case GST_VIDEO_FORMAT_GRAY8:
1405     case GST_VIDEO_FORMAT_Y800:
1406       return GST_ROUND_UP_4 (width);
1407     case GST_VIDEO_FORMAT_GRAY16_BE:
1408     case GST_VIDEO_FORMAT_GRAY16_LE:
1409     case GST_VIDEO_FORMAT_Y16:
1410       return GST_ROUND_UP_4 (width * 2);
1411     case GST_VIDEO_FORMAT_UYVP:
1412       return GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4);
1413     case GST_VIDEO_FORMAT_A420:
1414       if (component == 0 || component == 3) {
1415         return GST_ROUND_UP_4 (width);
1416       } else {
1417         return GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1418       }
1419     case GST_VIDEO_FORMAT_RGB8_PALETTED:
1420       return GST_ROUND_UP_4 (width);
1421     case GST_VIDEO_FORMAT_YUV9:
1422     case GST_VIDEO_FORMAT_YVU9:
1423       if (component == 0) {
1424         return GST_ROUND_UP_4 (width);
1425       } else {
1426         return GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
1427       }
1428     case GST_VIDEO_FORMAT_IYU1:
1429       return GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) +
1430           GST_ROUND_UP_4 (width) / 2);
1431     case GST_VIDEO_FORMAT_ARGB64:
1432     case GST_VIDEO_FORMAT_AYUV64:
1433       return width * 8;
1434     default:
1435       return 0;
1436   }
1437 }
1438
1439 /**
1440  * gst_video_format_get_pixel_stride:
1441  * @format: a #GstVideoFormat
1442  * @component: the component index
1443  *
1444  * Calculates the pixel stride (number of bytes from one pixel to the
1445  * pixel to its immediate left) for the video component with an index
1446  * of @component.  See @gst_video_format_get_row_stride for a description
1447  * of the component index.
1448  *
1449  * Since: 0.10.16
1450  *
1451  * Returns: pixel stride of component @component
1452  */
1453 int
1454 gst_video_format_get_pixel_stride (GstVideoFormat format, int component)
1455 {
1456   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1457   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1458
1459   switch (format) {
1460     case GST_VIDEO_FORMAT_I420:
1461     case GST_VIDEO_FORMAT_YV12:
1462     case GST_VIDEO_FORMAT_Y41B:
1463     case GST_VIDEO_FORMAT_Y42B:
1464     case GST_VIDEO_FORMAT_Y444:
1465     case GST_VIDEO_FORMAT_A420:
1466     case GST_VIDEO_FORMAT_YUV9:
1467     case GST_VIDEO_FORMAT_YVU9:
1468       return 1;
1469     case GST_VIDEO_FORMAT_YUY2:
1470     case GST_VIDEO_FORMAT_YVYU:
1471     case GST_VIDEO_FORMAT_UYVY:
1472       if (component == 0) {
1473         return 2;
1474       } else {
1475         return 4;
1476       }
1477     case GST_VIDEO_FORMAT_IYU1:
1478       /* doesn't make much sense for IYU1 because it's 1 or 3
1479        * for luma depending on position */
1480       return 0;
1481     case GST_VIDEO_FORMAT_AYUV:
1482     case GST_VIDEO_FORMAT_RGBx:
1483     case GST_VIDEO_FORMAT_BGRx:
1484     case GST_VIDEO_FORMAT_xRGB:
1485     case GST_VIDEO_FORMAT_xBGR:
1486     case GST_VIDEO_FORMAT_RGBA:
1487     case GST_VIDEO_FORMAT_BGRA:
1488     case GST_VIDEO_FORMAT_ARGB:
1489     case GST_VIDEO_FORMAT_ABGR:
1490     case GST_VIDEO_FORMAT_r210:
1491       return 4;
1492     case GST_VIDEO_FORMAT_RGB16:
1493     case GST_VIDEO_FORMAT_BGR16:
1494     case GST_VIDEO_FORMAT_RGB15:
1495     case GST_VIDEO_FORMAT_BGR15:
1496       return 2;
1497     case GST_VIDEO_FORMAT_RGB:
1498     case GST_VIDEO_FORMAT_BGR:
1499     case GST_VIDEO_FORMAT_v308:
1500       return 3;
1501     case GST_VIDEO_FORMAT_v210:
1502       /* v210 is packed at the bit level, so pixel stride doesn't make sense */
1503       return 0;
1504     case GST_VIDEO_FORMAT_v216:
1505       if (component == 0) {
1506         return 4;
1507       } else {
1508         return 8;
1509       }
1510     case GST_VIDEO_FORMAT_NV12:
1511     case GST_VIDEO_FORMAT_NV21:
1512       if (component == 0) {
1513         return 1;
1514       } else {
1515         return 2;
1516       }
1517     case GST_VIDEO_FORMAT_GRAY8:
1518     case GST_VIDEO_FORMAT_Y800:
1519       return 1;
1520     case GST_VIDEO_FORMAT_GRAY16_BE:
1521     case GST_VIDEO_FORMAT_GRAY16_LE:
1522     case GST_VIDEO_FORMAT_Y16:
1523       return 2;
1524     case GST_VIDEO_FORMAT_UYVP:
1525       /* UYVP is packed at the bit level, so pixel stride doesn't make sense */
1526       return 0;
1527     case GST_VIDEO_FORMAT_RGB8_PALETTED:
1528       return 1;
1529     case GST_VIDEO_FORMAT_ARGB64:
1530     case GST_VIDEO_FORMAT_AYUV64:
1531       return 8;
1532     default:
1533       return 0;
1534   }
1535 }
1536
1537 /**
1538  * gst_video_format_get_component_width:
1539  * @format: a #GstVideoFormat
1540  * @component: the component index
1541  * @width: the width of video
1542  *
1543  * Calculates the width of the component.  See
1544  * @gst_video_format_get_row_stride for a description
1545  * of the component index.
1546  *
1547  * Since: 0.10.16
1548  *
1549  * Returns: width of component @component
1550  */
1551 int
1552 gst_video_format_get_component_width (GstVideoFormat format,
1553     int component, int width)
1554 {
1555   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1556   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1557   g_return_val_if_fail (width > 0, 0);
1558
1559   switch (format) {
1560     case GST_VIDEO_FORMAT_I420:
1561     case GST_VIDEO_FORMAT_YV12:
1562     case GST_VIDEO_FORMAT_YUY2:
1563     case GST_VIDEO_FORMAT_YVYU:
1564     case GST_VIDEO_FORMAT_UYVY:
1565     case GST_VIDEO_FORMAT_Y42B:
1566     case GST_VIDEO_FORMAT_v210:
1567     case GST_VIDEO_FORMAT_v216:
1568     case GST_VIDEO_FORMAT_NV12:
1569     case GST_VIDEO_FORMAT_NV21:
1570     case GST_VIDEO_FORMAT_UYVP:
1571       if (component == 0) {
1572         return width;
1573       } else {
1574         return GST_ROUND_UP_2 (width) / 2;
1575       }
1576     case GST_VIDEO_FORMAT_Y41B:
1577     case GST_VIDEO_FORMAT_YUV9:
1578     case GST_VIDEO_FORMAT_YVU9:
1579     case GST_VIDEO_FORMAT_IYU1:
1580       if (component == 0) {
1581         return width;
1582       } else {
1583         return GST_ROUND_UP_4 (width) / 4;
1584       }
1585     case GST_VIDEO_FORMAT_AYUV:
1586     case GST_VIDEO_FORMAT_RGBx:
1587     case GST_VIDEO_FORMAT_BGRx:
1588     case GST_VIDEO_FORMAT_xRGB:
1589     case GST_VIDEO_FORMAT_xBGR:
1590     case GST_VIDEO_FORMAT_RGBA:
1591     case GST_VIDEO_FORMAT_BGRA:
1592     case GST_VIDEO_FORMAT_ARGB:
1593     case GST_VIDEO_FORMAT_ABGR:
1594     case GST_VIDEO_FORMAT_RGB:
1595     case GST_VIDEO_FORMAT_BGR:
1596     case GST_VIDEO_FORMAT_RGB16:
1597     case GST_VIDEO_FORMAT_BGR16:
1598     case GST_VIDEO_FORMAT_RGB15:
1599     case GST_VIDEO_FORMAT_BGR15:
1600     case GST_VIDEO_FORMAT_Y444:
1601     case GST_VIDEO_FORMAT_v308:
1602     case GST_VIDEO_FORMAT_GRAY8:
1603     case GST_VIDEO_FORMAT_GRAY16_BE:
1604     case GST_VIDEO_FORMAT_GRAY16_LE:
1605     case GST_VIDEO_FORMAT_Y800:
1606     case GST_VIDEO_FORMAT_Y16:
1607     case GST_VIDEO_FORMAT_RGB8_PALETTED:
1608     case GST_VIDEO_FORMAT_ARGB64:
1609     case GST_VIDEO_FORMAT_AYUV64:
1610     case GST_VIDEO_FORMAT_r210:
1611       return width;
1612     case GST_VIDEO_FORMAT_A420:
1613       if (component == 0 || component == 3) {
1614         return width;
1615       } else {
1616         return GST_ROUND_UP_2 (width) / 2;
1617       }
1618     default:
1619       return 0;
1620   }
1621 }
1622
1623 /**
1624  * gst_video_format_get_component_height:
1625  * @format: a #GstVideoFormat
1626  * @component: the component index
1627  * @height: the height of video
1628  *
1629  * Calculates the height of the component.  See
1630  * @gst_video_format_get_row_stride for a description
1631  * of the component index.
1632  *
1633  * Since: 0.10.16
1634  *
1635  * Returns: height of component @component
1636  */
1637 int
1638 gst_video_format_get_component_height (GstVideoFormat format,
1639     int component, int height)
1640 {
1641   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1642   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1643   g_return_val_if_fail (height > 0, 0);
1644
1645   switch (format) {
1646     case GST_VIDEO_FORMAT_I420:
1647     case GST_VIDEO_FORMAT_YV12:
1648     case GST_VIDEO_FORMAT_NV12:
1649     case GST_VIDEO_FORMAT_NV21:
1650       if (component == 0) {
1651         return height;
1652       } else {
1653         return GST_ROUND_UP_2 (height) / 2;
1654       }
1655     case GST_VIDEO_FORMAT_Y41B:
1656     case GST_VIDEO_FORMAT_Y42B:
1657     case GST_VIDEO_FORMAT_YUY2:
1658     case GST_VIDEO_FORMAT_YVYU:
1659     case GST_VIDEO_FORMAT_UYVY:
1660     case GST_VIDEO_FORMAT_AYUV:
1661     case GST_VIDEO_FORMAT_RGBx:
1662     case GST_VIDEO_FORMAT_BGRx:
1663     case GST_VIDEO_FORMAT_xRGB:
1664     case GST_VIDEO_FORMAT_xBGR:
1665     case GST_VIDEO_FORMAT_RGBA:
1666     case GST_VIDEO_FORMAT_BGRA:
1667     case GST_VIDEO_FORMAT_ARGB:
1668     case GST_VIDEO_FORMAT_ABGR:
1669     case GST_VIDEO_FORMAT_RGB:
1670     case GST_VIDEO_FORMAT_BGR:
1671     case GST_VIDEO_FORMAT_RGB16:
1672     case GST_VIDEO_FORMAT_BGR16:
1673     case GST_VIDEO_FORMAT_RGB15:
1674     case GST_VIDEO_FORMAT_BGR15:
1675     case GST_VIDEO_FORMAT_Y444:
1676     case GST_VIDEO_FORMAT_v210:
1677     case GST_VIDEO_FORMAT_v216:
1678     case GST_VIDEO_FORMAT_v308:
1679     case GST_VIDEO_FORMAT_GRAY8:
1680     case GST_VIDEO_FORMAT_GRAY16_BE:
1681     case GST_VIDEO_FORMAT_GRAY16_LE:
1682     case GST_VIDEO_FORMAT_Y800:
1683     case GST_VIDEO_FORMAT_Y16:
1684     case GST_VIDEO_FORMAT_UYVP:
1685     case GST_VIDEO_FORMAT_RGB8_PALETTED:
1686     case GST_VIDEO_FORMAT_IYU1:
1687     case GST_VIDEO_FORMAT_ARGB64:
1688     case GST_VIDEO_FORMAT_AYUV64:
1689     case GST_VIDEO_FORMAT_r210:
1690       return height;
1691     case GST_VIDEO_FORMAT_A420:
1692       if (component == 0 || component == 3) {
1693         return height;
1694       } else {
1695         return GST_ROUND_UP_2 (height) / 2;
1696       }
1697     case GST_VIDEO_FORMAT_YUV9:
1698     case GST_VIDEO_FORMAT_YVU9:
1699       if (component == 0) {
1700         return height;
1701       } else {
1702         return GST_ROUND_UP_4 (height) / 4;
1703       }
1704     default:
1705       return 0;
1706   }
1707 }
1708
1709 /**
1710  * gst_video_format_get_component_offset:
1711  * @format: a #GstVideoFormat
1712  * @component: the component index
1713  * @width: the width of video
1714  * @height: the height of video
1715  *
1716  * Calculates the offset (in bytes) of the first pixel of the component
1717  * with index @component.  For packed formats, this will typically be a
1718  * small integer (0, 1, 2, 3).  For planar formats, this will be a
1719  * (relatively) large offset to the beginning of the second or third
1720  * component planes.  See @gst_video_format_get_row_stride for a description
1721  * of the component index.
1722  *
1723  * Since: 0.10.16
1724  *
1725  * Returns: offset of component @component
1726  */
1727 int
1728 gst_video_format_get_component_offset (GstVideoFormat format,
1729     int component, int width, int height)
1730 {
1731   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1732   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1733   g_return_val_if_fail ((!gst_video_format_is_yuv (format)) || (width > 0
1734           && height > 0), 0);
1735
1736   switch (format) {
1737     case GST_VIDEO_FORMAT_I420:
1738       if (component == 0)
1739         return 0;
1740       if (component == 1)
1741         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1742       if (component == 2) {
1743         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1744             GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1745             (GST_ROUND_UP_2 (height) / 2);
1746       }
1747       return 0;
1748     case GST_VIDEO_FORMAT_YV12:        /* same as I420, but components 1+2 swapped */
1749       if (component == 0)
1750         return 0;
1751       if (component == 2)
1752         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1753       if (component == 1) {
1754         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1755             GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1756             (GST_ROUND_UP_2 (height) / 2);
1757       }
1758       return 0;
1759     case GST_VIDEO_FORMAT_YUY2:
1760       if (component == 0)
1761         return 0;
1762       if (component == 1)
1763         return 1;
1764       if (component == 2)
1765         return 3;
1766       return 0;
1767     case GST_VIDEO_FORMAT_YVYU:
1768       if (component == 0)
1769         return 0;
1770       if (component == 1)
1771         return 3;
1772       if (component == 2)
1773         return 1;
1774       return 0;
1775     case GST_VIDEO_FORMAT_UYVY:
1776       if (component == 0)
1777         return 1;
1778       if (component == 1)
1779         return 0;
1780       if (component == 2)
1781         return 2;
1782       return 0;
1783     case GST_VIDEO_FORMAT_AYUV:
1784       if (component == 0)
1785         return 1;
1786       if (component == 1)
1787         return 2;
1788       if (component == 2)
1789         return 3;
1790       if (component == 3)
1791         return 0;
1792       return 0;
1793     case GST_VIDEO_FORMAT_RGBx:
1794     case GST_VIDEO_FORMAT_RGBA:
1795       if (component == 0)
1796         return 0;
1797       if (component == 1)
1798         return 1;
1799       if (component == 2)
1800         return 2;
1801       if (component == 3)
1802         return 3;
1803       return 0;
1804     case GST_VIDEO_FORMAT_BGRx:
1805     case GST_VIDEO_FORMAT_BGRA:
1806       if (component == 0)
1807         return 2;
1808       if (component == 1)
1809         return 1;
1810       if (component == 2)
1811         return 0;
1812       if (component == 3)
1813         return 3;
1814       return 0;
1815     case GST_VIDEO_FORMAT_xRGB:
1816     case GST_VIDEO_FORMAT_ARGB:
1817       if (component == 0)
1818         return 1;
1819       if (component == 1)
1820         return 2;
1821       if (component == 2)
1822         return 3;
1823       if (component == 3)
1824         return 0;
1825       return 0;
1826     case GST_VIDEO_FORMAT_xBGR:
1827     case GST_VIDEO_FORMAT_ABGR:
1828       if (component == 0)
1829         return 3;
1830       if (component == 1)
1831         return 2;
1832       if (component == 2)
1833         return 1;
1834       if (component == 3)
1835         return 0;
1836       return 0;
1837     case GST_VIDEO_FORMAT_RGB:
1838     case GST_VIDEO_FORMAT_v308:
1839       if (component == 0)
1840         return 0;
1841       if (component == 1)
1842         return 1;
1843       if (component == 2)
1844         return 2;
1845       return 0;
1846     case GST_VIDEO_FORMAT_BGR:
1847       if (component == 0)
1848         return 2;
1849       if (component == 1)
1850         return 1;
1851       if (component == 2)
1852         return 0;
1853       return 0;
1854     case GST_VIDEO_FORMAT_Y41B:
1855       if (component == 0)
1856         return 0;
1857       if (component == 1)
1858         return GST_ROUND_UP_4 (width) * height;
1859       if (component == 2)
1860         return (GST_ROUND_UP_4 (width) +
1861             (GST_ROUND_UP_16 (width) / 4)) * height;
1862       return 0;
1863     case GST_VIDEO_FORMAT_Y42B:
1864       if (component == 0)
1865         return 0;
1866       if (component == 1)
1867         return GST_ROUND_UP_4 (width) * height;
1868       if (component == 2)
1869         return (GST_ROUND_UP_4 (width) + (GST_ROUND_UP_8 (width) / 2)) * height;
1870       return 0;
1871     case GST_VIDEO_FORMAT_Y444:
1872       return GST_ROUND_UP_4 (width) * height * component;
1873     case GST_VIDEO_FORMAT_v210:
1874     case GST_VIDEO_FORMAT_r210:
1875       /* v210 is bit-packed, so this doesn't make sense */
1876       return 0;
1877     case GST_VIDEO_FORMAT_v216:
1878       if (component == 0)
1879         return 0;
1880       if (component == 1)
1881         return 2;
1882       if (component == 2)
1883         return 6;
1884       return 0;
1885     case GST_VIDEO_FORMAT_NV12:
1886       if (component == 0)
1887         return 0;
1888       if (component == 1)
1889         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1890       if (component == 2)
1891         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) + 1;
1892     case GST_VIDEO_FORMAT_NV21:
1893       if (component == 0)
1894         return 0;
1895       if (component == 1)
1896         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) + 1;
1897       if (component == 2)
1898         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1899     case GST_VIDEO_FORMAT_GRAY8:
1900     case GST_VIDEO_FORMAT_GRAY16_BE:
1901     case GST_VIDEO_FORMAT_GRAY16_LE:
1902     case GST_VIDEO_FORMAT_Y800:
1903     case GST_VIDEO_FORMAT_Y16:
1904       return 0;
1905     case GST_VIDEO_FORMAT_UYVP:
1906       /* UYVP is bit-packed, so this doesn't make sense */
1907       return 0;
1908     case GST_VIDEO_FORMAT_A420:
1909       if (component == 0)
1910         return 0;
1911       if (component == 1)
1912         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1913       if (component == 2) {
1914         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1915             GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1916             (GST_ROUND_UP_2 (height) / 2);
1917       }
1918       if (component == 3) {
1919         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1920             2 * GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1921             (GST_ROUND_UP_2 (height) / 2);
1922       }
1923     case GST_VIDEO_FORMAT_RGB8_PALETTED:
1924       return 0;
1925     case GST_VIDEO_FORMAT_YUV9:
1926       if (component == 0)
1927         return 0;
1928       if (component == 1)
1929         return GST_ROUND_UP_4 (width) * height;
1930       if (component == 2) {
1931         return GST_ROUND_UP_4 (width) * height +
1932             GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4) *
1933             (GST_ROUND_UP_4 (height) / 4);
1934       }
1935       return 0;
1936     case GST_VIDEO_FORMAT_YVU9:
1937       if (component == 0)
1938         return 0;
1939       if (component == 1) {
1940         return GST_ROUND_UP_4 (width) * height +
1941             GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4) *
1942             (GST_ROUND_UP_4 (height) / 4);
1943       }
1944       if (component == 2)
1945         return GST_ROUND_UP_4 (width) * height;
1946       return 0;
1947     case GST_VIDEO_FORMAT_IYU1:
1948       if (component == 0)
1949         return 1;
1950       if (component == 1)
1951         return 0;
1952       if (component == 2)
1953         return 4;
1954     case GST_VIDEO_FORMAT_ARGB64:
1955     case GST_VIDEO_FORMAT_AYUV64:
1956       if (component == 0)
1957         return 2;
1958       if (component == 1)
1959         return 4;
1960       if (component == 2)
1961         return 6;
1962       if (component == 3)
1963         return 0;
1964       return 0;
1965     default:
1966       return 0;
1967   }
1968 }
1969
1970 /**
1971  * gst_video_format_get_size:
1972  * @format: a #GstVideoFormat
1973  * @width: the width of video
1974  * @height: the height of video
1975  *
1976  * Calculates the total number of bytes in the raw video format.  This
1977  * number should be used when allocating a buffer for raw video.
1978  *
1979  * Since: 0.10.16
1980  *
1981  * Returns: size (in bytes) of raw video format
1982  */
1983 int
1984 gst_video_format_get_size (GstVideoFormat format, int width, int height)
1985 {
1986   int size;
1987
1988   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1989   g_return_val_if_fail (width > 0 && height > 0, 0);
1990
1991   switch (format) {
1992     case GST_VIDEO_FORMAT_I420:
1993     case GST_VIDEO_FORMAT_YV12:
1994       size = GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1995       size += GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1996           (GST_ROUND_UP_2 (height) / 2) * 2;
1997       return size;
1998     case GST_VIDEO_FORMAT_IYU1:
1999       return GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) +
2000           GST_ROUND_UP_4 (width) / 2) * height;
2001     case GST_VIDEO_FORMAT_YUY2:
2002     case GST_VIDEO_FORMAT_YVYU:
2003     case GST_VIDEO_FORMAT_UYVY:
2004       return GST_ROUND_UP_4 (width * 2) * height;
2005     case GST_VIDEO_FORMAT_AYUV:
2006     case GST_VIDEO_FORMAT_RGBx:
2007     case GST_VIDEO_FORMAT_BGRx:
2008     case GST_VIDEO_FORMAT_xRGB:
2009     case GST_VIDEO_FORMAT_xBGR:
2010     case GST_VIDEO_FORMAT_RGBA:
2011     case GST_VIDEO_FORMAT_BGRA:
2012     case GST_VIDEO_FORMAT_ARGB:
2013     case GST_VIDEO_FORMAT_ABGR:
2014     case GST_VIDEO_FORMAT_r210:
2015       return width * 4 * height;
2016     case GST_VIDEO_FORMAT_RGB16:
2017     case GST_VIDEO_FORMAT_BGR16:
2018     case GST_VIDEO_FORMAT_RGB15:
2019     case GST_VIDEO_FORMAT_BGR15:
2020       return GST_ROUND_UP_4 (width * 2) * height;
2021     case GST_VIDEO_FORMAT_RGB:
2022     case GST_VIDEO_FORMAT_BGR:
2023     case GST_VIDEO_FORMAT_v308:
2024       return GST_ROUND_UP_4 (width * 3) * height;
2025     case GST_VIDEO_FORMAT_Y41B:
2026       /* simplification of ROUNDUP4(w)*h + 2*((ROUNDUP16(w)/4)*h */
2027       return (GST_ROUND_UP_4 (width) + (GST_ROUND_UP_16 (width) / 2)) * height;
2028     case GST_VIDEO_FORMAT_Y42B:
2029       /* simplification of ROUNDUP4(w)*h + 2*(ROUNDUP8(w)/2)*h */
2030       return (GST_ROUND_UP_4 (width) + GST_ROUND_UP_8 (width)) * height;
2031     case GST_VIDEO_FORMAT_Y444:
2032       return GST_ROUND_UP_4 (width) * height * 3;
2033     case GST_VIDEO_FORMAT_v210:
2034       return ((width + 47) / 48) * 128 * height;
2035     case GST_VIDEO_FORMAT_v216:
2036       return GST_ROUND_UP_8 (width * 4) * height;
2037     case GST_VIDEO_FORMAT_NV12:
2038     case GST_VIDEO_FORMAT_NV21:
2039       return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) * 3 / 2;
2040     case GST_VIDEO_FORMAT_GRAY8:
2041     case GST_VIDEO_FORMAT_Y800:
2042     case GST_VIDEO_FORMAT_RGB8_PALETTED:
2043       return GST_ROUND_UP_4 (width) * height;
2044     case GST_VIDEO_FORMAT_GRAY16_BE:
2045     case GST_VIDEO_FORMAT_GRAY16_LE:
2046     case GST_VIDEO_FORMAT_Y16:
2047       return GST_ROUND_UP_4 (width * 2) * height;
2048     case GST_VIDEO_FORMAT_UYVP:
2049       return GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4) * height;
2050     case GST_VIDEO_FORMAT_A420:
2051       size = 2 * GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
2052       size += GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
2053           (GST_ROUND_UP_2 (height) / 2) * 2;
2054       return size;
2055     case GST_VIDEO_FORMAT_YUV9:
2056     case GST_VIDEO_FORMAT_YVU9:
2057       size = GST_ROUND_UP_4 (width) * height;
2058       size += GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4) *
2059           (GST_ROUND_UP_4 (height) / 4) * 2;
2060       return size;
2061     case GST_VIDEO_FORMAT_ARGB64:
2062     case GST_VIDEO_FORMAT_AYUV64:
2063       return width * 8 * height;
2064     default:
2065       return 0;
2066   }
2067 }
2068
2069 /**
2070  * gst_video_format_convert:
2071  * @format: a #GstVideoFormat
2072  * @width: the width of video
2073  * @height: the height of video
2074  * @fps_n: frame rate numerator
2075  * @fps_d: frame rate denominator
2076  * @src_format: #GstFormat of the @src_value
2077  * @src_value: value to convert
2078  * @dest_format: #GstFormat of the @dest_value
2079  * @dest_value: pointer to destination value
2080  *
2081  * Converts among various #GstFormat types.  This function handles
2082  * GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT.  For
2083  * raw video, GST_FORMAT_DEFAULT corresponds to video frames.  This
2084  * function can be to handle pad queries of the type GST_QUERY_CONVERT.
2085  *
2086  * Since: 0.10.16
2087  *
2088  * Returns: TRUE if the conversion was successful.
2089  */
2090 gboolean
2091 gst_video_format_convert (GstVideoFormat format, int width, int height,
2092     int fps_n, int fps_d,
2093     GstFormat src_format, gint64 src_value,
2094     GstFormat dest_format, gint64 * dest_value)
2095 {
2096   gboolean ret = FALSE;
2097   int size;
2098
2099   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
2100   g_return_val_if_fail (width > 0 && height > 0, 0);
2101
2102   size = gst_video_format_get_size (format, width, height);
2103
2104   GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s to %s",
2105       src_value, gst_format_get_name (src_format),
2106       gst_format_get_name (dest_format));
2107
2108   if (src_format == dest_format) {
2109     *dest_value = src_value;
2110     ret = TRUE;
2111     goto done;
2112   }
2113
2114   if (src_value == -1) {
2115     *dest_value = -1;
2116     ret = TRUE;
2117     goto done;
2118   }
2119
2120   /* bytes to frames */
2121   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_DEFAULT) {
2122     if (size != 0) {
2123       *dest_value = gst_util_uint64_scale_int (src_value, 1, size);
2124     } else {
2125       GST_ERROR ("blocksize is 0");
2126       *dest_value = 0;
2127     }
2128     ret = TRUE;
2129     goto done;
2130   }
2131
2132   /* frames to bytes */
2133   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_BYTES) {
2134     *dest_value = gst_util_uint64_scale_int (src_value, size, 1);
2135     ret = TRUE;
2136     goto done;
2137   }
2138
2139   /* time to frames */
2140   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_DEFAULT) {
2141     if (fps_d != 0) {
2142       *dest_value = gst_util_uint64_scale (src_value,
2143           fps_n, GST_SECOND * fps_d);
2144     } else {
2145       GST_ERROR ("framerate denominator is 0");
2146       *dest_value = 0;
2147     }
2148     ret = TRUE;
2149     goto done;
2150   }
2151
2152   /* frames to time */
2153   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) {
2154     if (fps_n != 0) {
2155       *dest_value = gst_util_uint64_scale (src_value,
2156           GST_SECOND * fps_d, fps_n);
2157     } else {
2158       GST_ERROR ("framerate numerator is 0");
2159       *dest_value = 0;
2160     }
2161     ret = TRUE;
2162     goto done;
2163   }
2164
2165   /* time to bytes */
2166   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
2167     if (fps_d != 0) {
2168       *dest_value = gst_util_uint64_scale (src_value,
2169           fps_n * size, GST_SECOND * fps_d);
2170     } else {
2171       GST_ERROR ("framerate denominator is 0");
2172       *dest_value = 0;
2173     }
2174     ret = TRUE;
2175     goto done;
2176   }
2177
2178   /* bytes to time */
2179   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
2180     if (fps_n != 0 && size != 0) {
2181       *dest_value = gst_util_uint64_scale (src_value,
2182           GST_SECOND * fps_d, fps_n * size);
2183     } else {
2184       GST_ERROR ("framerate denominator and/or blocksize is 0");
2185       *dest_value = 0;
2186     }
2187     ret = TRUE;
2188   }
2189
2190 done:
2191
2192   GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, ret, *dest_value);
2193
2194   return ret;
2195 }
2196
2197 #define GST_VIDEO_EVENT_STILL_STATE_NAME "GstEventStillFrame"
2198
2199 /**
2200  * gst_video_event_new_still_frame:
2201  * @in_still: boolean value for the still-frame state of the event.
2202  *
2203  * Creates a new Still Frame event. If @in_still is %TRUE, then the event
2204  * represents the start of a still frame sequence. If it is %FALSE, then
2205  * the event ends a still frame sequence.
2206  *
2207  * To parse an event created by gst_video_event_new_still_frame() use
2208  * gst_video_event_parse_still_frame().
2209  *
2210  * Returns: The new GstEvent
2211  * Since: 0.10.26
2212  */
2213 GstEvent *
2214 gst_video_event_new_still_frame (gboolean in_still)
2215 {
2216   GstEvent *still_event;
2217   GstStructure *s;
2218
2219   s = gst_structure_new (GST_VIDEO_EVENT_STILL_STATE_NAME,
2220       "still-state", G_TYPE_BOOLEAN, in_still, NULL);
2221   still_event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
2222
2223   return still_event;
2224 }
2225
2226 /**
2227  * gst_video_event_parse_still_frame:
2228  * @event: A #GstEvent to parse
2229  * @in_still: A boolean to receive the still-frame status from the event, or NULL
2230  *
2231  * Parse a #GstEvent, identify if it is a Still Frame event, and
2232  * return the still-frame state from the event if it is.
2233  * If the event represents the start of a still frame, the in_still
2234  * variable will be set to TRUE, otherwise FALSE. It is OK to pass NULL for the
2235  * in_still variable order to just check whether the event is a valid still-frame
2236  * event.
2237  *
2238  * Create a still frame event using gst_video_event_new_still_frame()
2239  *
2240  * Returns: %TRUE if the event is a valid still-frame event. %FALSE if not
2241  * Since: 0.10.26
2242  */
2243 gboolean
2244 gst_video_event_parse_still_frame (GstEvent * event, gboolean * in_still)
2245 {
2246   const GstStructure *s;
2247   gboolean ev_still_state;
2248
2249   g_return_val_if_fail (event != NULL, FALSE);
2250
2251   if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM)
2252     return FALSE;               /* Not a still frame event */
2253
2254   s = gst_event_get_structure (event);
2255   if (s == NULL
2256       || !gst_structure_has_name (s, GST_VIDEO_EVENT_STILL_STATE_NAME))
2257     return FALSE;               /* Not a still frame event */
2258   if (!gst_structure_get_boolean (s, "still-state", &ev_still_state))
2259     return FALSE;               /* Not a still frame event */
2260   if (in_still)
2261     *in_still = ev_still_state;
2262   return TRUE;
2263 }
2264
2265 /**
2266  * gst_video_parse_caps_palette:
2267  * @caps: #GstCaps to parse
2268  *
2269  * Returns the palette data from the caps as a #GstBuffer. For
2270  * #GST_VIDEO_FORMAT_RGB8_PALETTED this is containing 256 #guint32
2271  * values, each containing ARGB colors in native endianness.
2272  *
2273  * Returns: a #GstBuffer containing the palette data. Unref after usage.
2274  * Since: 0.10.32
2275  */
2276 GstBuffer *
2277 gst_video_parse_caps_palette (GstCaps * caps)
2278 {
2279   GstStructure *s;
2280   const GValue *p_v;
2281   GstBuffer *p;
2282
2283   if (!gst_caps_is_fixed (caps))
2284     return NULL;
2285
2286   s = gst_caps_get_structure (caps, 0);
2287
2288   p_v = gst_structure_get_value (s, "palette_data");
2289   if (!p_v || !GST_VALUE_HOLDS_BUFFER (p_v))
2290     return NULL;
2291
2292   p = gst_buffer_ref (gst_value_get_buffer (p_v));
2293
2294   return p;
2295 }