video: Return correct component width/height for A420
[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_BYTE_ORDER) {
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,
517     int width, int height, int framerate_n, int framerate_d, int par_n,
518     int par_d, 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,
549     int height, 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 = 0;
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,
619               width, height));
620       green_mask =
621           mask >> (8 * gst_video_format_get_component_offset (format, 1,
622               width, height));
623       blue_mask =
624           mask >> (8 * gst_video_format_get_component_offset (format, 2,
625               width, 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,
670               width, 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     case GST_MAKE_FOURCC ('U', 'Y', 'V', 'P'):
776       return GST_VIDEO_FORMAT_UYVP;
777     case GST_MAKE_FOURCC ('A', '4', '2', '0'):
778       return GST_VIDEO_FORMAT_A420;
779     default:
780       return GST_VIDEO_FORMAT_UNKNOWN;
781   }
782 }
783
784 /**
785  * gst_video_format_to_fourcc:
786  * @format: a #GstVideoFormat video format
787  *
788  * Converts a #GstVideoFormat value into the corresponding FOURCC.  Only
789  * a few YUV formats have corresponding FOURCC values.  If @format has
790  * no corresponding FOURCC value, 0 is returned.
791  *
792  * Since: 0.10.16
793  *
794  * Returns: the FOURCC corresponding to @format
795  */
796 guint32
797 gst_video_format_to_fourcc (GstVideoFormat format)
798 {
799   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
800
801   switch (format) {
802     case GST_VIDEO_FORMAT_I420:
803       return GST_MAKE_FOURCC ('I', '4', '2', '0');
804     case GST_VIDEO_FORMAT_YV12:
805       return GST_MAKE_FOURCC ('Y', 'V', '1', '2');
806     case GST_VIDEO_FORMAT_YUY2:
807       return GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
808     case GST_VIDEO_FORMAT_YVYU:
809       return GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U');
810     case GST_VIDEO_FORMAT_UYVY:
811       return GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
812     case GST_VIDEO_FORMAT_AYUV:
813       return GST_MAKE_FOURCC ('A', 'Y', 'U', 'V');
814     case GST_VIDEO_FORMAT_Y41B:
815       return GST_MAKE_FOURCC ('Y', '4', '1', 'B');
816     case GST_VIDEO_FORMAT_Y42B:
817       return GST_MAKE_FOURCC ('Y', '4', '2', 'B');
818     case GST_VIDEO_FORMAT_Y444:
819       return GST_MAKE_FOURCC ('Y', '4', '4', '4');
820     case GST_VIDEO_FORMAT_v210:
821       return GST_MAKE_FOURCC ('v', '2', '1', '0');
822     case GST_VIDEO_FORMAT_v216:
823       return GST_MAKE_FOURCC ('v', '2', '1', '6');
824     case GST_VIDEO_FORMAT_NV12:
825       return GST_MAKE_FOURCC ('N', 'V', '1', '2');
826     case GST_VIDEO_FORMAT_NV21:
827       return GST_MAKE_FOURCC ('N', 'V', '2', '1');
828     case GST_VIDEO_FORMAT_v308:
829       return GST_MAKE_FOURCC ('v', '3', '0', '8');
830     case GST_VIDEO_FORMAT_Y800:
831       return GST_MAKE_FOURCC ('Y', '8', '0', '0');
832     case GST_VIDEO_FORMAT_Y16:
833       return GST_MAKE_FOURCC ('Y', '1', '6', ' ');
834     case GST_VIDEO_FORMAT_UYVP:
835       return GST_MAKE_FOURCC ('U', 'Y', 'V', 'P');
836     case GST_VIDEO_FORMAT_A420:
837       return GST_MAKE_FOURCC ('A', '4', '2', '0');
838     default:
839       return 0;
840   }
841 }
842
843 /*
844  * gst_video_format_from_rgb32_masks:
845  * @red_mask: red bit mask
846  * @green_mask: green bit mask
847  * @blue_mask: blue bit mask
848  *
849  * Converts red, green, blue bit masks into the corresponding
850  * #GstVideoFormat.  
851  *
852  * Since: 0.10.16
853  *
854  * Returns: the #GstVideoFormat corresponding to the bit masks
855  */
856 static GstVideoFormat
857 gst_video_format_from_rgb32_masks (int red_mask, int green_mask, int blue_mask)
858 {
859   if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
860       blue_mask == 0x0000ff00) {
861     return GST_VIDEO_FORMAT_RGBx;
862   }
863   if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
864       blue_mask == 0xff000000) {
865     return GST_VIDEO_FORMAT_BGRx;
866   }
867   if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
868       blue_mask == 0x000000ff) {
869     return GST_VIDEO_FORMAT_xRGB;
870   }
871   if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
872       blue_mask == 0x00ff0000) {
873     return GST_VIDEO_FORMAT_xBGR;
874   }
875
876   return GST_VIDEO_FORMAT_UNKNOWN;
877 }
878
879 static GstVideoFormat
880 gst_video_format_from_rgba32_masks (int red_mask, int green_mask,
881     int blue_mask, int alpha_mask)
882 {
883   if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
884       blue_mask == 0x0000ff00 && alpha_mask == 0x000000ff) {
885     return GST_VIDEO_FORMAT_RGBA;
886   }
887   if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
888       blue_mask == 0xff000000 && alpha_mask == 0x000000ff) {
889     return GST_VIDEO_FORMAT_BGRA;
890   }
891   if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
892       blue_mask == 0x000000ff && alpha_mask == 0xff000000) {
893     return GST_VIDEO_FORMAT_ARGB;
894   }
895   if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
896       blue_mask == 0x00ff0000 && alpha_mask == 0xff000000) {
897     return GST_VIDEO_FORMAT_ABGR;
898   }
899
900   return GST_VIDEO_FORMAT_UNKNOWN;
901 }
902
903 static GstVideoFormat
904 gst_video_format_from_rgb24_masks (int red_mask, int green_mask, int blue_mask)
905 {
906   if (red_mask == 0xff0000 && green_mask == 0x00ff00 && blue_mask == 0x0000ff) {
907     return GST_VIDEO_FORMAT_RGB;
908   }
909   if (red_mask == 0x0000ff && green_mask == 0x00ff00 && blue_mask == 0xff0000) {
910     return GST_VIDEO_FORMAT_BGR;
911   }
912
913   return GST_VIDEO_FORMAT_UNKNOWN;
914 }
915
916 static GstVideoFormat
917 gst_video_format_from_rgb16_masks (int red_mask, int green_mask, int blue_mask)
918 {
919   if (red_mask == GST_VIDEO_COMP1_MASK_16_INT
920       && green_mask == GST_VIDEO_COMP2_MASK_16_INT
921       && blue_mask == GST_VIDEO_COMP3_MASK_16_INT) {
922     return GST_VIDEO_FORMAT_RGB16;
923   }
924   if (red_mask == GST_VIDEO_COMP3_MASK_16_INT
925       && green_mask == GST_VIDEO_COMP2_MASK_16_INT
926       && blue_mask == GST_VIDEO_COMP1_MASK_16_INT) {
927     return GST_VIDEO_FORMAT_BGR16;
928   }
929   if (red_mask == GST_VIDEO_COMP1_MASK_15_INT
930       && green_mask == GST_VIDEO_COMP2_MASK_15_INT
931       && blue_mask == GST_VIDEO_COMP3_MASK_15_INT) {
932     return GST_VIDEO_FORMAT_RGB15;
933   }
934   if (red_mask == GST_VIDEO_COMP3_MASK_15_INT
935       && green_mask == GST_VIDEO_COMP2_MASK_15_INT
936       && blue_mask == GST_VIDEO_COMP1_MASK_15_INT) {
937     return GST_VIDEO_FORMAT_BGR15;
938   }
939
940   return GST_VIDEO_FORMAT_UNKNOWN;
941 }
942
943 /**
944  * gst_video_format_is_rgb:
945  * @format: a #GstVideoFormat
946  *
947  * Determine whether the video format is an RGB format.
948  *
949  * Since: 0.10.16
950  *
951  * Returns: TRUE if @format represents RGB video
952  */
953 gboolean
954 gst_video_format_is_rgb (GstVideoFormat format)
955 {
956   switch (format) {
957     case GST_VIDEO_FORMAT_I420:
958     case GST_VIDEO_FORMAT_YV12:
959     case GST_VIDEO_FORMAT_YUY2:
960     case GST_VIDEO_FORMAT_YVYU:
961     case GST_VIDEO_FORMAT_UYVY:
962     case GST_VIDEO_FORMAT_AYUV:
963     case GST_VIDEO_FORMAT_Y41B:
964     case GST_VIDEO_FORMAT_Y42B:
965     case GST_VIDEO_FORMAT_Y444:
966     case GST_VIDEO_FORMAT_v210:
967     case GST_VIDEO_FORMAT_v216:
968     case GST_VIDEO_FORMAT_NV12:
969     case GST_VIDEO_FORMAT_NV21:
970     case GST_VIDEO_FORMAT_v308:
971     case GST_VIDEO_FORMAT_UYVP:
972     case GST_VIDEO_FORMAT_A420:
973       return FALSE;
974     case GST_VIDEO_FORMAT_RGBx:
975     case GST_VIDEO_FORMAT_BGRx:
976     case GST_VIDEO_FORMAT_xRGB:
977     case GST_VIDEO_FORMAT_xBGR:
978     case GST_VIDEO_FORMAT_RGBA:
979     case GST_VIDEO_FORMAT_BGRA:
980     case GST_VIDEO_FORMAT_ARGB:
981     case GST_VIDEO_FORMAT_ABGR:
982     case GST_VIDEO_FORMAT_RGB:
983     case GST_VIDEO_FORMAT_BGR:
984     case GST_VIDEO_FORMAT_RGB16:
985     case GST_VIDEO_FORMAT_BGR16:
986     case GST_VIDEO_FORMAT_RGB15:
987     case GST_VIDEO_FORMAT_BGR15:
988       return TRUE;
989     default:
990       return FALSE;
991   }
992 }
993
994 /**
995  * gst_video_format_is_yuv:
996  * @format: a #GstVideoFormat
997  *
998  * Determine whether the video format is a YUV format.
999  *
1000  * Since: 0.10.16
1001  *
1002  * Returns: TRUE if @format represents YUV video
1003  */
1004 gboolean
1005 gst_video_format_is_yuv (GstVideoFormat format)
1006 {
1007   switch (format) {
1008     case GST_VIDEO_FORMAT_I420:
1009     case GST_VIDEO_FORMAT_YV12:
1010     case GST_VIDEO_FORMAT_YUY2:
1011     case GST_VIDEO_FORMAT_YVYU:
1012     case GST_VIDEO_FORMAT_UYVY:
1013     case GST_VIDEO_FORMAT_AYUV:
1014     case GST_VIDEO_FORMAT_Y41B:
1015     case GST_VIDEO_FORMAT_Y42B:
1016     case GST_VIDEO_FORMAT_Y444:
1017     case GST_VIDEO_FORMAT_v210:
1018     case GST_VIDEO_FORMAT_v216:
1019     case GST_VIDEO_FORMAT_NV12:
1020     case GST_VIDEO_FORMAT_NV21:
1021     case GST_VIDEO_FORMAT_v308:
1022     case GST_VIDEO_FORMAT_Y800:
1023     case GST_VIDEO_FORMAT_Y16:
1024     case GST_VIDEO_FORMAT_UYVP:
1025     case GST_VIDEO_FORMAT_A420:
1026       return TRUE;
1027     case GST_VIDEO_FORMAT_RGBx:
1028     case GST_VIDEO_FORMAT_BGRx:
1029     case GST_VIDEO_FORMAT_xRGB:
1030     case GST_VIDEO_FORMAT_xBGR:
1031     case GST_VIDEO_FORMAT_RGBA:
1032     case GST_VIDEO_FORMAT_BGRA:
1033     case GST_VIDEO_FORMAT_ARGB:
1034     case GST_VIDEO_FORMAT_ABGR:
1035     case GST_VIDEO_FORMAT_RGB:
1036     case GST_VIDEO_FORMAT_BGR:
1037     case GST_VIDEO_FORMAT_RGB16:
1038     case GST_VIDEO_FORMAT_BGR16:
1039     case GST_VIDEO_FORMAT_RGB15:
1040     case GST_VIDEO_FORMAT_BGR15:
1041       return FALSE;
1042     default:
1043       return FALSE;
1044   }
1045 }
1046
1047 /**
1048  * gst_video_format_is_gray:
1049  * @format: a #GstVideoFormat
1050  *
1051  * Determine whether the video format is a grayscale format.
1052  *
1053  * Since: 0.10.29
1054  *
1055  * Returns: TRUE if @format represents grayscale video
1056  */
1057 gboolean
1058 gst_video_format_is_gray (GstVideoFormat format)
1059 {
1060   switch (format) {
1061     case GST_VIDEO_FORMAT_GRAY8:
1062     case GST_VIDEO_FORMAT_GRAY16_BE:
1063     case GST_VIDEO_FORMAT_GRAY16_LE:
1064     case GST_VIDEO_FORMAT_Y800:
1065     case GST_VIDEO_FORMAT_Y16:
1066       return TRUE;
1067     default:
1068       return FALSE;
1069   }
1070 }
1071
1072 /**
1073  * gst_video_format_has_alpha:
1074  * @format: a #GstVideoFormat
1075  * 
1076  * Returns TRUE or FALSE depending on if the video format provides an
1077  * alpha channel.
1078  *
1079  * Since: 0.10.16
1080  *
1081  * Returns: TRUE if @format has an alpha channel
1082  */
1083 gboolean
1084 gst_video_format_has_alpha (GstVideoFormat format)
1085 {
1086   switch (format) {
1087     case GST_VIDEO_FORMAT_I420:
1088     case GST_VIDEO_FORMAT_YV12:
1089     case GST_VIDEO_FORMAT_YUY2:
1090     case GST_VIDEO_FORMAT_YVYU:
1091     case GST_VIDEO_FORMAT_UYVY:
1092     case GST_VIDEO_FORMAT_Y41B:
1093     case GST_VIDEO_FORMAT_Y42B:
1094     case GST_VIDEO_FORMAT_Y444:
1095     case GST_VIDEO_FORMAT_v210:
1096     case GST_VIDEO_FORMAT_v216:
1097     case GST_VIDEO_FORMAT_NV12:
1098     case GST_VIDEO_FORMAT_NV21:
1099     case GST_VIDEO_FORMAT_v308:
1100     case GST_VIDEO_FORMAT_Y800:
1101     case GST_VIDEO_FORMAT_Y16:
1102     case GST_VIDEO_FORMAT_UYVP:
1103       return FALSE;
1104     case GST_VIDEO_FORMAT_AYUV:
1105     case GST_VIDEO_FORMAT_RGBA:
1106     case GST_VIDEO_FORMAT_BGRA:
1107     case GST_VIDEO_FORMAT_ARGB:
1108     case GST_VIDEO_FORMAT_ABGR:
1109     case GST_VIDEO_FORMAT_A420:
1110       return TRUE;
1111     case GST_VIDEO_FORMAT_RGBx:
1112     case GST_VIDEO_FORMAT_BGRx:
1113     case GST_VIDEO_FORMAT_xRGB:
1114     case GST_VIDEO_FORMAT_xBGR:
1115     case GST_VIDEO_FORMAT_RGB:
1116     case GST_VIDEO_FORMAT_BGR:
1117     case GST_VIDEO_FORMAT_RGB16:
1118     case GST_VIDEO_FORMAT_BGR16:
1119     case GST_VIDEO_FORMAT_RGB15:
1120     case GST_VIDEO_FORMAT_BGR15:
1121       return FALSE;
1122     default:
1123       return FALSE;
1124   }
1125 }
1126
1127 /**
1128  * gst_video_format_get_row_stride:
1129  * @format: a #GstVideoFormat
1130  * @component: the component index
1131  * @width: the width of video
1132  *
1133  * Calculates the row stride (number of bytes from one row of pixels to
1134  * the next) for the video component with an index of @component.  For
1135  * YUV video, Y, U, and V have component indices of 0, 1, and 2,
1136  * respectively.  For RGB video, R, G, and B have component indicies of
1137  * 0, 1, and 2, respectively.  Alpha channels, if present, have a component
1138  * index of 3.  The @width parameter always represents the width of the
1139  * video, not the component.
1140  *
1141  * Since: 0.10.16
1142  *
1143  * Returns: row stride of component @component
1144  */
1145 int
1146 gst_video_format_get_row_stride (GstVideoFormat format, int component,
1147     int width)
1148 {
1149   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1150   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1151   g_return_val_if_fail (width > 0, 0);
1152
1153   switch (format) {
1154     case GST_VIDEO_FORMAT_I420:
1155     case GST_VIDEO_FORMAT_YV12:
1156       if (component == 0) {
1157         return GST_ROUND_UP_4 (width);
1158       } else {
1159         return GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1160       }
1161     case GST_VIDEO_FORMAT_YUY2:
1162     case GST_VIDEO_FORMAT_YVYU:
1163     case GST_VIDEO_FORMAT_UYVY:
1164       return GST_ROUND_UP_4 (width * 2);
1165     case GST_VIDEO_FORMAT_AYUV:
1166     case GST_VIDEO_FORMAT_RGBx:
1167     case GST_VIDEO_FORMAT_BGRx:
1168     case GST_VIDEO_FORMAT_xRGB:
1169     case GST_VIDEO_FORMAT_xBGR:
1170     case GST_VIDEO_FORMAT_RGBA:
1171     case GST_VIDEO_FORMAT_BGRA:
1172     case GST_VIDEO_FORMAT_ARGB:
1173     case GST_VIDEO_FORMAT_ABGR:
1174       return width * 4;
1175     case GST_VIDEO_FORMAT_RGB16:
1176     case GST_VIDEO_FORMAT_BGR16:
1177     case GST_VIDEO_FORMAT_RGB15:
1178     case GST_VIDEO_FORMAT_BGR15:
1179       return GST_ROUND_UP_4 (width * 2);
1180     case GST_VIDEO_FORMAT_RGB:
1181     case GST_VIDEO_FORMAT_BGR:
1182     case GST_VIDEO_FORMAT_v308:
1183       return GST_ROUND_UP_4 (width * 3);
1184     case GST_VIDEO_FORMAT_Y41B:
1185       if (component == 0) {
1186         return GST_ROUND_UP_4 (width);
1187       } else {
1188         return GST_ROUND_UP_16 (width) / 4;
1189       }
1190     case GST_VIDEO_FORMAT_Y42B:
1191       if (component == 0) {
1192         return GST_ROUND_UP_4 (width);
1193       } else {
1194         return GST_ROUND_UP_8 (width) / 2;
1195       }
1196     case GST_VIDEO_FORMAT_Y444:
1197       return GST_ROUND_UP_4 (width);
1198     case GST_VIDEO_FORMAT_v210:
1199       return ((width + 47) / 48) * 128;
1200     case GST_VIDEO_FORMAT_v216:
1201       return GST_ROUND_UP_8 (width * 4);
1202     case GST_VIDEO_FORMAT_NV12:
1203     case GST_VIDEO_FORMAT_NV21:
1204       return GST_ROUND_UP_4 (width);
1205     case GST_VIDEO_FORMAT_GRAY8:
1206     case GST_VIDEO_FORMAT_Y800:
1207       return GST_ROUND_UP_4 (width);
1208     case GST_VIDEO_FORMAT_GRAY16_BE:
1209     case GST_VIDEO_FORMAT_GRAY16_LE:
1210     case GST_VIDEO_FORMAT_Y16:
1211       return GST_ROUND_UP_4 (width * 2);
1212     case GST_VIDEO_FORMAT_UYVP:
1213       return GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4);
1214     case GST_VIDEO_FORMAT_A420:
1215       if (component == 0 || component == 3) {
1216         return GST_ROUND_UP_4 (width);
1217       } else {
1218         return GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1219       }
1220     default:
1221       return 0;
1222   }
1223 }
1224
1225 /**
1226  * gst_video_format_get_pixel_stride:
1227  * @format: a #GstVideoFormat
1228  * @component: the component index
1229  *
1230  * Calculates the pixel stride (number of bytes from one pixel to the
1231  * pixel to its immediate left) for the video component with an index
1232  * of @component.  See @gst_video_format_get_row_stride for a description
1233  * of the component index.
1234  *
1235  * Since: 0.10.16
1236  *
1237  * Returns: pixel stride of component @component
1238  */
1239 int
1240 gst_video_format_get_pixel_stride (GstVideoFormat format, int component)
1241 {
1242   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1243   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1244
1245   switch (format) {
1246     case GST_VIDEO_FORMAT_I420:
1247     case GST_VIDEO_FORMAT_YV12:
1248     case GST_VIDEO_FORMAT_Y41B:
1249     case GST_VIDEO_FORMAT_Y42B:
1250     case GST_VIDEO_FORMAT_Y444:
1251     case GST_VIDEO_FORMAT_A420:
1252       return 1;
1253     case GST_VIDEO_FORMAT_YUY2:
1254     case GST_VIDEO_FORMAT_YVYU:
1255     case GST_VIDEO_FORMAT_UYVY:
1256       if (component == 0) {
1257         return 2;
1258       } else {
1259         return 4;
1260       }
1261     case GST_VIDEO_FORMAT_AYUV:
1262     case GST_VIDEO_FORMAT_RGBx:
1263     case GST_VIDEO_FORMAT_BGRx:
1264     case GST_VIDEO_FORMAT_xRGB:
1265     case GST_VIDEO_FORMAT_xBGR:
1266     case GST_VIDEO_FORMAT_RGBA:
1267     case GST_VIDEO_FORMAT_BGRA:
1268     case GST_VIDEO_FORMAT_ARGB:
1269     case GST_VIDEO_FORMAT_ABGR:
1270       return 4;
1271     case GST_VIDEO_FORMAT_RGB16:
1272     case GST_VIDEO_FORMAT_BGR16:
1273     case GST_VIDEO_FORMAT_RGB15:
1274     case GST_VIDEO_FORMAT_BGR15:
1275       return 2;
1276     case GST_VIDEO_FORMAT_RGB:
1277     case GST_VIDEO_FORMAT_BGR:
1278     case GST_VIDEO_FORMAT_v308:
1279       return 3;
1280     case GST_VIDEO_FORMAT_v210:
1281       /* v210 is packed at the bit level, so pixel stride doesn't make sense */
1282       return 0;
1283     case GST_VIDEO_FORMAT_v216:
1284       if (component == 0) {
1285         return 4;
1286       } else {
1287         return 8;
1288       }
1289     case GST_VIDEO_FORMAT_NV12:
1290     case GST_VIDEO_FORMAT_NV21:
1291       if (component == 0) {
1292         return 1;
1293       } else {
1294         return 2;
1295       }
1296     case GST_VIDEO_FORMAT_GRAY8:
1297     case GST_VIDEO_FORMAT_Y800:
1298       return 1;
1299     case GST_VIDEO_FORMAT_GRAY16_BE:
1300     case GST_VIDEO_FORMAT_GRAY16_LE:
1301     case GST_VIDEO_FORMAT_Y16:
1302       return 2;
1303     case GST_VIDEO_FORMAT_UYVP:
1304       /* UYVP is packed at the bit level, so pixel stride doesn't make sense */
1305       return 0;
1306     default:
1307       return 0;
1308   }
1309 }
1310
1311 /**
1312  * gst_video_format_get_component_width:
1313  * @format: a #GstVideoFormat
1314  * @component: the component index
1315  * @width: the width of video
1316  *
1317  * Calculates the width of the component.  See
1318  * @gst_video_format_get_row_stride for a description
1319  * of the component index.
1320  *
1321  * Since: 0.10.16
1322  *
1323  * Returns: width of component @component
1324  */
1325 int
1326 gst_video_format_get_component_width (GstVideoFormat format,
1327     int component, int width)
1328 {
1329   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1330   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1331   g_return_val_if_fail (width > 0, 0);
1332
1333   switch (format) {
1334     case GST_VIDEO_FORMAT_I420:
1335     case GST_VIDEO_FORMAT_YV12:
1336     case GST_VIDEO_FORMAT_YUY2:
1337     case GST_VIDEO_FORMAT_YVYU:
1338     case GST_VIDEO_FORMAT_UYVY:
1339     case GST_VIDEO_FORMAT_Y42B:
1340     case GST_VIDEO_FORMAT_v210:
1341     case GST_VIDEO_FORMAT_v216:
1342     case GST_VIDEO_FORMAT_NV12:
1343     case GST_VIDEO_FORMAT_NV21:
1344     case GST_VIDEO_FORMAT_UYVP:
1345       if (component == 0) {
1346         return width;
1347       } else {
1348         return GST_ROUND_UP_2 (width) / 2;
1349       }
1350     case GST_VIDEO_FORMAT_Y41B:
1351       if (component == 0) {
1352         return width;
1353       } else {
1354         return GST_ROUND_UP_4 (width) / 4;
1355       }
1356     case GST_VIDEO_FORMAT_AYUV:
1357     case GST_VIDEO_FORMAT_RGBx:
1358     case GST_VIDEO_FORMAT_BGRx:
1359     case GST_VIDEO_FORMAT_xRGB:
1360     case GST_VIDEO_FORMAT_xBGR:
1361     case GST_VIDEO_FORMAT_RGBA:
1362     case GST_VIDEO_FORMAT_BGRA:
1363     case GST_VIDEO_FORMAT_ARGB:
1364     case GST_VIDEO_FORMAT_ABGR:
1365     case GST_VIDEO_FORMAT_RGB:
1366     case GST_VIDEO_FORMAT_BGR:
1367     case GST_VIDEO_FORMAT_RGB16:
1368     case GST_VIDEO_FORMAT_BGR16:
1369     case GST_VIDEO_FORMAT_RGB15:
1370     case GST_VIDEO_FORMAT_BGR15:
1371     case GST_VIDEO_FORMAT_Y444:
1372     case GST_VIDEO_FORMAT_v308:
1373     case GST_VIDEO_FORMAT_GRAY8:
1374     case GST_VIDEO_FORMAT_GRAY16_BE:
1375     case GST_VIDEO_FORMAT_GRAY16_LE:
1376     case GST_VIDEO_FORMAT_Y800:
1377     case GST_VIDEO_FORMAT_Y16:
1378       return width;
1379     case GST_VIDEO_FORMAT_A420:
1380       if (component == 0 || component == 3) {
1381         return width;
1382       } else {
1383         return GST_ROUND_UP_2 (width) / 2;
1384       }
1385     default:
1386       return 0;
1387   }
1388 }
1389
1390 /**
1391  * gst_video_format_get_component_height:
1392  * @format: a #GstVideoFormat
1393  * @component: the component index
1394  * @height: the height of video
1395  *
1396  * Calculates the height of the component.  See
1397  * @gst_video_format_get_row_stride for a description
1398  * of the component index.
1399  *
1400  * Since: 0.10.16
1401  *
1402  * Returns: height of component @component
1403  */
1404 int
1405 gst_video_format_get_component_height (GstVideoFormat format,
1406     int component, int height)
1407 {
1408   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1409   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1410   g_return_val_if_fail (height > 0, 0);
1411
1412   switch (format) {
1413     case GST_VIDEO_FORMAT_I420:
1414     case GST_VIDEO_FORMAT_YV12:
1415     case GST_VIDEO_FORMAT_NV12:
1416     case GST_VIDEO_FORMAT_NV21:
1417       if (component == 0) {
1418         return height;
1419       } else {
1420         return GST_ROUND_UP_2 (height) / 2;
1421       }
1422     case GST_VIDEO_FORMAT_Y41B:
1423     case GST_VIDEO_FORMAT_Y42B:
1424     case GST_VIDEO_FORMAT_YUY2:
1425     case GST_VIDEO_FORMAT_YVYU:
1426     case GST_VIDEO_FORMAT_UYVY:
1427     case GST_VIDEO_FORMAT_AYUV:
1428     case GST_VIDEO_FORMAT_RGBx:
1429     case GST_VIDEO_FORMAT_BGRx:
1430     case GST_VIDEO_FORMAT_xRGB:
1431     case GST_VIDEO_FORMAT_xBGR:
1432     case GST_VIDEO_FORMAT_RGBA:
1433     case GST_VIDEO_FORMAT_BGRA:
1434     case GST_VIDEO_FORMAT_ARGB:
1435     case GST_VIDEO_FORMAT_ABGR:
1436     case GST_VIDEO_FORMAT_RGB:
1437     case GST_VIDEO_FORMAT_BGR:
1438     case GST_VIDEO_FORMAT_RGB16:
1439     case GST_VIDEO_FORMAT_BGR16:
1440     case GST_VIDEO_FORMAT_RGB15:
1441     case GST_VIDEO_FORMAT_BGR15:
1442     case GST_VIDEO_FORMAT_Y444:
1443     case GST_VIDEO_FORMAT_v210:
1444     case GST_VIDEO_FORMAT_v216:
1445     case GST_VIDEO_FORMAT_v308:
1446     case GST_VIDEO_FORMAT_GRAY8:
1447     case GST_VIDEO_FORMAT_GRAY16_BE:
1448     case GST_VIDEO_FORMAT_GRAY16_LE:
1449     case GST_VIDEO_FORMAT_Y800:
1450     case GST_VIDEO_FORMAT_Y16:
1451     case GST_VIDEO_FORMAT_UYVP:
1452       return height;
1453     case GST_VIDEO_FORMAT_A420:
1454       if (component == 0 || component == 3) {
1455         return height;
1456       } else {
1457         return GST_ROUND_UP_2 (height) / 2;
1458       }
1459     default:
1460       return 0;
1461   }
1462 }
1463
1464 /**
1465  * gst_video_format_get_component_offset:
1466  * @format: a #GstVideoFormat
1467  * @component: the component index
1468  * @width: the width of video
1469  * @height: the height of video
1470  *
1471  * Calculates the offset (in bytes) of the first pixel of the component
1472  * with index @component.  For packed formats, this will typically be a
1473  * small integer (0, 1, 2, 3).  For planar formats, this will be a
1474  * (relatively) large offset to the beginning of the second or third
1475  * component planes.  See @gst_video_format_get_row_stride for a description
1476  * of the component index.
1477  *
1478  * Since: 0.10.16
1479  *
1480  * Returns: offset of component @component
1481  */
1482 int
1483 gst_video_format_get_component_offset (GstVideoFormat format,
1484     int component, int width, int height)
1485 {
1486   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1487   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1488   g_return_val_if_fail (width > 0 && height > 0, 0);
1489
1490   switch (format) {
1491     case GST_VIDEO_FORMAT_I420:
1492       if (component == 0)
1493         return 0;
1494       if (component == 1)
1495         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1496       if (component == 2) {
1497         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1498             GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1499             (GST_ROUND_UP_2 (height) / 2);
1500       }
1501       return 0;
1502     case GST_VIDEO_FORMAT_YV12:        /* same as I420, but components 1+2 swapped */
1503       if (component == 0)
1504         return 0;
1505       if (component == 2)
1506         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1507       if (component == 1) {
1508         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1509             GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1510             (GST_ROUND_UP_2 (height) / 2);
1511       }
1512       return 0;
1513     case GST_VIDEO_FORMAT_YUY2:
1514       if (component == 0)
1515         return 0;
1516       if (component == 1)
1517         return 1;
1518       if (component == 2)
1519         return 3;
1520       return 0;
1521     case GST_VIDEO_FORMAT_YVYU:
1522       if (component == 0)
1523         return 0;
1524       if (component == 1)
1525         return 3;
1526       if (component == 2)
1527         return 1;
1528       return 0;
1529     case GST_VIDEO_FORMAT_UYVY:
1530       if (component == 0)
1531         return 1;
1532       if (component == 1)
1533         return 0;
1534       if (component == 2)
1535         return 2;
1536       return 0;
1537     case GST_VIDEO_FORMAT_AYUV:
1538       if (component == 0)
1539         return 1;
1540       if (component == 1)
1541         return 2;
1542       if (component == 2)
1543         return 3;
1544       if (component == 3)
1545         return 0;
1546       return 0;
1547     case GST_VIDEO_FORMAT_RGBx:
1548     case GST_VIDEO_FORMAT_RGBA:
1549       if (component == 0)
1550         return 0;
1551       if (component == 1)
1552         return 1;
1553       if (component == 2)
1554         return 2;
1555       if (component == 3)
1556         return 3;
1557       return 0;
1558     case GST_VIDEO_FORMAT_BGRx:
1559     case GST_VIDEO_FORMAT_BGRA:
1560       if (component == 0)
1561         return 2;
1562       if (component == 1)
1563         return 1;
1564       if (component == 2)
1565         return 0;
1566       if (component == 3)
1567         return 3;
1568       return 0;
1569     case GST_VIDEO_FORMAT_xRGB:
1570     case GST_VIDEO_FORMAT_ARGB:
1571       if (component == 0)
1572         return 1;
1573       if (component == 1)
1574         return 2;
1575       if (component == 2)
1576         return 3;
1577       if (component == 3)
1578         return 0;
1579       return 0;
1580     case GST_VIDEO_FORMAT_xBGR:
1581     case GST_VIDEO_FORMAT_ABGR:
1582       if (component == 0)
1583         return 3;
1584       if (component == 1)
1585         return 2;
1586       if (component == 2)
1587         return 1;
1588       if (component == 3)
1589         return 0;
1590       return 0;
1591     case GST_VIDEO_FORMAT_RGB:
1592     case GST_VIDEO_FORMAT_v308:
1593       if (component == 0)
1594         return 0;
1595       if (component == 1)
1596         return 1;
1597       if (component == 2)
1598         return 2;
1599       return 0;
1600     case GST_VIDEO_FORMAT_BGR:
1601       if (component == 0)
1602         return 2;
1603       if (component == 1)
1604         return 1;
1605       if (component == 2)
1606         return 0;
1607       return 0;
1608     case GST_VIDEO_FORMAT_Y41B:
1609       if (component == 0)
1610         return 0;
1611       if (component == 1)
1612         return GST_ROUND_UP_4 (width) * height;
1613       if (component == 2)
1614         return (GST_ROUND_UP_4 (width) +
1615             (GST_ROUND_UP_16 (width) / 4)) * height;
1616       return 0;
1617     case GST_VIDEO_FORMAT_Y42B:
1618       if (component == 0)
1619         return 0;
1620       if (component == 1)
1621         return GST_ROUND_UP_4 (width) * height;
1622       if (component == 2)
1623         return (GST_ROUND_UP_4 (width) + (GST_ROUND_UP_8 (width) / 2)) * height;
1624       return 0;
1625     case GST_VIDEO_FORMAT_Y444:
1626       return GST_ROUND_UP_4 (width) * height * component;
1627     case GST_VIDEO_FORMAT_v210:
1628       /* v210 is bit-packed, so this doesn't make sense */
1629       return 0;
1630     case GST_VIDEO_FORMAT_v216:
1631       if (component == 0)
1632         return 0;
1633       if (component == 1)
1634         return 2;
1635       if (component == 2)
1636         return 6;
1637       return 0;
1638     case GST_VIDEO_FORMAT_NV12:
1639       if (component == 0)
1640         return 0;
1641       if (component == 1)
1642         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1643       if (component == 2)
1644         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) + 1;
1645     case GST_VIDEO_FORMAT_NV21:
1646       if (component == 0)
1647         return 0;
1648       if (component == 1)
1649         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) + 1;
1650       if (component == 2)
1651         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1652     case GST_VIDEO_FORMAT_GRAY8:
1653     case GST_VIDEO_FORMAT_GRAY16_BE:
1654     case GST_VIDEO_FORMAT_GRAY16_LE:
1655     case GST_VIDEO_FORMAT_Y800:
1656     case GST_VIDEO_FORMAT_Y16:
1657       return 0;
1658     case GST_VIDEO_FORMAT_UYVP:
1659       /* UYVP is bit-packed, so this doesn't make sense */
1660       return 0;
1661     case GST_VIDEO_FORMAT_A420:
1662       if (component == 0)
1663         return 0;
1664       if (component == 1)
1665         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1666       if (component == 2) {
1667         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1668             GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1669             (GST_ROUND_UP_2 (height) / 2);
1670       }
1671       if (component == 3) {
1672         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1673             2 * GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1674             (GST_ROUND_UP_2 (height) / 2);
1675       }
1676     default:
1677       return 0;
1678   }
1679 }
1680
1681 /**
1682  * gst_video_format_get_size:
1683  * @format: a #GstVideoFormat
1684  * @width: the width of video
1685  * @height: the height of video
1686  *
1687  * Calculates the total number of bytes in the raw video format.  This
1688  * number should be used when allocating a buffer for raw video.
1689  *
1690  * Since: 0.10.16
1691  *
1692  * Returns: size (in bytes) of raw video format
1693  */
1694 int
1695 gst_video_format_get_size (GstVideoFormat format, int width, int height)
1696 {
1697   int size;
1698
1699   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1700   g_return_val_if_fail (width > 0 && height > 0, 0);
1701
1702   switch (format) {
1703     case GST_VIDEO_FORMAT_I420:
1704     case GST_VIDEO_FORMAT_YV12:
1705       size = GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1706       size += GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1707           (GST_ROUND_UP_2 (height) / 2) * 2;
1708       return size;
1709     case GST_VIDEO_FORMAT_YUY2:
1710     case GST_VIDEO_FORMAT_YVYU:
1711     case GST_VIDEO_FORMAT_UYVY:
1712       return GST_ROUND_UP_4 (width * 2) * height;
1713     case GST_VIDEO_FORMAT_AYUV:
1714     case GST_VIDEO_FORMAT_RGBx:
1715     case GST_VIDEO_FORMAT_BGRx:
1716     case GST_VIDEO_FORMAT_xRGB:
1717     case GST_VIDEO_FORMAT_xBGR:
1718     case GST_VIDEO_FORMAT_RGBA:
1719     case GST_VIDEO_FORMAT_BGRA:
1720     case GST_VIDEO_FORMAT_ARGB:
1721     case GST_VIDEO_FORMAT_ABGR:
1722       return width * 4 * height;
1723     case GST_VIDEO_FORMAT_RGB16:
1724     case GST_VIDEO_FORMAT_BGR16:
1725     case GST_VIDEO_FORMAT_RGB15:
1726     case GST_VIDEO_FORMAT_BGR15:
1727       return GST_ROUND_UP_4 (width * 2) * height;
1728     case GST_VIDEO_FORMAT_RGB:
1729     case GST_VIDEO_FORMAT_BGR:
1730     case GST_VIDEO_FORMAT_v308:
1731       return GST_ROUND_UP_4 (width * 3) * height;
1732     case GST_VIDEO_FORMAT_Y41B:
1733       /* simplification of ROUNDUP4(w)*h + 2*((ROUNDUP16(w)/4)*h */
1734       return (GST_ROUND_UP_4 (width) + (GST_ROUND_UP_16 (width) / 2)) * height;
1735     case GST_VIDEO_FORMAT_Y42B:
1736       /* simplification of ROUNDUP4(w)*h + 2*(ROUNDUP8(w)/2)*h */
1737       return (GST_ROUND_UP_4 (width) + GST_ROUND_UP_8 (width)) * height;
1738     case GST_VIDEO_FORMAT_Y444:
1739       return GST_ROUND_UP_4 (width) * height * 3;
1740     case GST_VIDEO_FORMAT_v210:
1741       return ((width + 47) / 48) * 128 * height;
1742     case GST_VIDEO_FORMAT_v216:
1743       return GST_ROUND_UP_8 (width * 4) * height;
1744     case GST_VIDEO_FORMAT_NV12:
1745     case GST_VIDEO_FORMAT_NV21:
1746       return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) * 3 / 2;
1747     case GST_VIDEO_FORMAT_GRAY8:
1748     case GST_VIDEO_FORMAT_Y800:
1749       return GST_ROUND_UP_4 (width) * height;
1750     case GST_VIDEO_FORMAT_GRAY16_BE:
1751     case GST_VIDEO_FORMAT_GRAY16_LE:
1752     case GST_VIDEO_FORMAT_Y16:
1753       return GST_ROUND_UP_4 (width * 2) * height;
1754     case GST_VIDEO_FORMAT_UYVP:
1755       return GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4) * height;
1756     case GST_VIDEO_FORMAT_A420:
1757       size = 2 * GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1758       size += GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1759           (GST_ROUND_UP_2 (height) / 2) * 2;
1760       return size;
1761     default:
1762       return 0;
1763   }
1764 }
1765
1766 /**
1767  * gst_video_format_convert:
1768  * @format: a #GstVideoFormat
1769  * @width: the width of video
1770  * @height: the height of video
1771  * @fps_n: frame rate numerator
1772  * @fps_d: frame rate denominator
1773  * @src_format: #GstFormat of the @src_value
1774  * @src_value: value to convert
1775  * @dest_format: #GstFormat of the @dest_value
1776  * @dest_value: pointer to destination value
1777  *
1778  * Converts among various #GstFormat types.  This function handles
1779  * GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT.  For
1780  * raw video, GST_FORMAT_DEFAULT corresponds to video frames.  This
1781  * function can be to handle pad queries of the type GST_QUERY_CONVERT.
1782  *
1783  * Since: 0.10.16
1784  *
1785  * Returns: TRUE if the conversion was successful.
1786  */
1787 gboolean
1788 gst_video_format_convert (GstVideoFormat format, int width, int height,
1789     int fps_n, int fps_d,
1790     GstFormat src_format, gint64 src_value,
1791     GstFormat dest_format, gint64 * dest_value)
1792 {
1793   gboolean ret = FALSE;
1794   int size;
1795
1796   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1797   g_return_val_if_fail (width > 0 && height > 0, 0);
1798
1799   size = gst_video_format_get_size (format, width, height);
1800
1801   GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s to %s",
1802       src_value, gst_format_get_name (src_format),
1803       gst_format_get_name (dest_format));
1804
1805   if (src_format == dest_format) {
1806     *dest_value = src_value;
1807     ret = TRUE;
1808     goto done;
1809   }
1810
1811   if (src_value == -1) {
1812     *dest_value = -1;
1813     ret = TRUE;
1814     goto done;
1815   }
1816
1817   /* bytes to frames */
1818   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_DEFAULT) {
1819     if (size != 0) {
1820       *dest_value = gst_util_uint64_scale_int (src_value, 1, size);
1821     } else {
1822       GST_ERROR ("blocksize is 0");
1823       *dest_value = 0;
1824     }
1825     ret = TRUE;
1826     goto done;
1827   }
1828
1829   /* frames to bytes */
1830   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_BYTES) {
1831     *dest_value = gst_util_uint64_scale_int (src_value, size, 1);
1832     ret = TRUE;
1833     goto done;
1834   }
1835
1836   /* time to frames */
1837   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_DEFAULT) {
1838     if (fps_d != 0) {
1839       *dest_value = gst_util_uint64_scale (src_value,
1840           fps_n, GST_SECOND * fps_d);
1841     } else {
1842       GST_ERROR ("framerate denominator is 0");
1843       *dest_value = 0;
1844     }
1845     ret = TRUE;
1846     goto done;
1847   }
1848
1849   /* frames to time */
1850   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) {
1851     if (fps_n != 0) {
1852       *dest_value = gst_util_uint64_scale (src_value,
1853           GST_SECOND * fps_d, fps_n);
1854     } else {
1855       GST_ERROR ("framerate numerator is 0");
1856       *dest_value = 0;
1857     }
1858     ret = TRUE;
1859     goto done;
1860   }
1861
1862   /* time to bytes */
1863   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
1864     if (fps_d != 0) {
1865       *dest_value = gst_util_uint64_scale (src_value,
1866           fps_n * size, GST_SECOND * fps_d);
1867     } else {
1868       GST_ERROR ("framerate denominator is 0");
1869       *dest_value = 0;
1870     }
1871     ret = TRUE;
1872     goto done;
1873   }
1874
1875   /* bytes to time */
1876   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
1877     if (fps_n != 0 && size != 0) {
1878       *dest_value = gst_util_uint64_scale (src_value,
1879           GST_SECOND * fps_d, fps_n * size);
1880     } else {
1881       GST_ERROR ("framerate denominator and/or blocksize is 0");
1882       *dest_value = 0;
1883     }
1884     ret = TRUE;
1885   }
1886
1887 done:
1888
1889   GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, ret, *dest_value);
1890
1891   return ret;
1892 }
1893
1894 #define GST_VIDEO_EVENT_STILL_STATE_NAME "GstEventStillFrame"
1895
1896 /**
1897  * gst_video_event_new_still_frame:
1898  * @in_still: boolean value for the still-frame state of the event.
1899  *
1900  * Creates a new Still Frame event. If @in_still is %TRUE, then the event
1901  * represents the start of a still frame sequence. If it is %FALSE, then
1902  * the event ends a still frame sequence.
1903  *
1904  * To parse an event created by gst_video_event_new_still_frame() use
1905  * gst_video_event_parse_still_frame().
1906  *
1907  * Returns: The new GstEvent
1908  * Since: 0.10.26
1909  */
1910 GstEvent *
1911 gst_video_event_new_still_frame (gboolean in_still)
1912 {
1913   GstEvent *still_event;
1914   GstStructure *s;
1915
1916   s = gst_structure_new (GST_VIDEO_EVENT_STILL_STATE_NAME,
1917       "still-state", G_TYPE_BOOLEAN, in_still, NULL);
1918   still_event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1919
1920   return still_event;
1921 }
1922
1923 /**
1924  * gst_video_event_parse_still_frame:
1925  * @event: A #GstEvent to parse
1926  * @in_still: A boolean to receive the still-frame status from the event, or NULL
1927  *
1928  * Parse a #GstEvent, identify if it is a Still Frame event, and
1929  * return the still-frame state from the event if it is.
1930  * If the event represents the start of a still frame, the in_still
1931  * variable will be set to TRUE, otherwise FALSE. It is OK to pass NULL for the
1932  * in_still variable order to just check whether the event is a valid still-frame
1933  * event.
1934  *
1935  * Create a still frame event using gst_video_event_new_still_frame()
1936  *
1937  * Returns: %TRUE if the event is a valid still-frame event. %FALSE if not
1938  * Since: 0.10.26
1939  */
1940 gboolean
1941 gst_video_event_parse_still_frame (GstEvent * event, gboolean * in_still)
1942 {
1943   const GstStructure *s;
1944   gboolean ev_still_state;
1945
1946   g_return_val_if_fail (event != NULL, FALSE);
1947
1948   if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM)
1949     return FALSE;               /* Not a still frame event */
1950
1951   s = gst_event_get_structure (event);
1952   if (s == NULL
1953       || !gst_structure_has_name (s, GST_VIDEO_EVENT_STILL_STATE_NAME))
1954     return FALSE;               /* Not a still frame event */
1955   if (!gst_structure_get_boolean (s, "still-state", &ev_still_state))
1956     return FALSE;               /* Not a still frame event */
1957   if (in_still)
1958     *in_still = ev_still_state;
1959   return TRUE;
1960 }