Add new GstVideFormat enum and write a bunch of helper functions based around it.
[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 /* This is simply a convenience function, nothing more or less */
41 const GValue *
42 gst_video_frame_rate (GstPad * pad)
43 {
44   const GValue *fps;
45   gchar *fps_string;
46
47   const GstCaps *caps = NULL;
48   GstStructure *structure;
49
50   /* get pad caps */
51   caps = GST_PAD_CAPS (pad);
52   if (caps == NULL) {
53     g_warning ("gstvideo: failed to get caps of pad %s:%s",
54         GST_DEBUG_PAD_NAME (pad));
55     return NULL;
56   }
57
58   structure = gst_caps_get_structure (caps, 0);
59   if ((fps = gst_structure_get_value (structure, "framerate")) == NULL) {
60     g_warning ("gstvideo: failed to get framerate property of pad %s:%s",
61         GST_DEBUG_PAD_NAME (pad));
62     return NULL;
63   }
64   if (!GST_VALUE_HOLDS_FRACTION (fps)) {
65     g_warning
66         ("gstvideo: framerate property of pad %s:%s is not of type Fraction",
67         GST_DEBUG_PAD_NAME (pad));
68     return NULL;
69   }
70
71   fps_string = gst_value_serialize (fps);
72   GST_DEBUG ("Framerate request on pad %s:%s: %s",
73       GST_DEBUG_PAD_NAME (pad), fps_string);
74   g_free (fps_string);
75
76   return fps;
77 }
78
79 gboolean
80 gst_video_get_size (GstPad * pad, gint * width, gint * height)
81 {
82   const GstCaps *caps = NULL;
83   GstStructure *structure;
84   gboolean ret;
85
86   g_return_val_if_fail (pad != NULL, FALSE);
87   g_return_val_if_fail (width != NULL, FALSE);
88   g_return_val_if_fail (height != NULL, FALSE);
89
90   caps = GST_PAD_CAPS (pad);
91
92   if (caps == NULL) {
93     g_warning ("gstvideo: failed to get caps of pad %s:%s",
94         GST_DEBUG_PAD_NAME (pad));
95     return FALSE;
96   }
97
98   structure = gst_caps_get_structure (caps, 0);
99   ret = gst_structure_get_int (structure, "width", width);
100   ret &= gst_structure_get_int (structure, "height", height);
101
102   if (!ret) {
103     g_warning ("gstvideo: failed to get size properties on pad %s:%s",
104         GST_DEBUG_PAD_NAME (pad));
105     return FALSE;
106   }
107
108   GST_DEBUG ("size request on pad %s:%s: %dx%d",
109       GST_DEBUG_PAD_NAME (pad), width ? *width : -1, height ? *height : -1);
110
111   return TRUE;
112 }
113
114 /**
115  * gst_video_calculate_display_ratio:
116  * @dar_n: Numerator of the calculated display_ratio
117  * @dar_d: Denominator of the calculated display_ratio
118  * @video_width: Width of the video frame in pixels
119  * @video_height: Height of the video frame in pixels
120  * @video_par_n: Numerator of the pixel aspect ratio of the input video.
121  * @video_par_d: Denominator of the pixel aspect ratio of the input video.
122  * @display_par_n: Numerator of the pixel aspect ratio of the display device
123  * @display_par_d: Denominator of the pixel aspect ratio of the display device
124  *
125  * Given the Pixel Aspect Ratio and size of an input video frame, and the 
126  * pixel aspect ratio of the intended display device, calculates the actual 
127  * display ratio the video will be rendered with.
128  *
129  * Returns: A boolean indicating success and a calculated Display Ratio in the 
130  * dar_n and dar_d parameters. 
131  * The return value is FALSE in the case of integer overflow or other error. 
132  *
133  * Since: 0.10.7
134  */
135 gboolean
136 gst_video_calculate_display_ratio (guint * dar_n, guint * dar_d,
137     guint video_width, guint video_height,
138     guint video_par_n, guint video_par_d,
139     guint display_par_n, guint display_par_d)
140 {
141   gint num, den;
142
143   GValue display_ratio = { 0, };
144   GValue tmp = { 0, };
145   GValue tmp2 = { 0, };
146
147   g_return_val_if_fail (dar_n != NULL, FALSE);
148   g_return_val_if_fail (dar_d != NULL, FALSE);
149
150   g_value_init (&display_ratio, GST_TYPE_FRACTION);
151   g_value_init (&tmp, GST_TYPE_FRACTION);
152   g_value_init (&tmp2, GST_TYPE_FRACTION);
153
154   /* Calculate (video_width * video_par_n * display_par_d) /
155    * (video_height * video_par_d * display_par_n) */
156   gst_value_set_fraction (&display_ratio, video_width, video_height);
157   gst_value_set_fraction (&tmp, video_par_n, video_par_d);
158
159   if (!gst_value_fraction_multiply (&tmp2, &display_ratio, &tmp))
160     goto error_overflow;
161
162   gst_value_set_fraction (&tmp, display_par_d, display_par_n);
163
164   if (!gst_value_fraction_multiply (&display_ratio, &tmp2, &tmp))
165     goto error_overflow;
166
167   num = gst_value_get_fraction_numerator (&display_ratio);
168   den = gst_value_get_fraction_denominator (&display_ratio);
169
170   g_value_unset (&display_ratio);
171   g_value_unset (&tmp);
172   g_value_unset (&tmp2);
173
174   g_return_val_if_fail (num > 0, FALSE);
175   g_return_val_if_fail (den > 0, FALSE);
176
177   *dar_n = num;
178   *dar_d = den;
179
180   return TRUE;
181 error_overflow:
182   g_value_unset (&display_ratio);
183   g_value_unset (&tmp);
184   g_value_unset (&tmp2);
185   return FALSE;
186 }
187
188 /**
189  * gst_video_format_parse_caps:
190  * @caps: the #GstCaps to parse
191  * @format: the #GstVideoFormat of the video represented by @caps (output)
192  * @width: the width of the video represented by @caps (output)
193  * @height: the height of the video represented by @caps (output)
194  *
195  * Determines the #GstVideoFormat of @caps and places it in the location
196  * pointed to by @format.  Extracts the size of the video and places it
197  * in the location pointed to by @width and @height.  If @caps does not
198  * reprsent one of the raw video formats listed in #GstVideoFormat, the
199  * function will fail and return FALSE.
200  *
201  * Since: 0.10.16
202  *
203  * Returns: TRUE if @caps was parsed correctly.
204  */
205 gboolean
206 gst_video_format_parse_caps (GstCaps * caps, GstVideoFormat * format,
207     int *width, int *height)
208 {
209   GstStructure *structure;
210   gboolean ok = TRUE;
211
212   if (!gst_caps_is_fixed (caps))
213     return FALSE;
214
215   structure = gst_caps_get_structure (caps, 0);
216
217   if (format) {
218     if (gst_structure_has_name (structure, "video/x-raw-yuv")) {
219       guint32 fourcc;
220
221       ok &= gst_structure_get_fourcc (structure, "format", &fourcc);
222
223       *format = gst_video_format_from_fourcc (fourcc);
224       if (*format == GST_VIDEO_FORMAT_UNKNOWN) {
225         ok = FALSE;
226       }
227     } else if (gst_structure_has_name (structure, "video/x-raw-rgb")) {
228       int depth;
229       int bpp;
230       int endianness;
231       int red_mask;
232       int green_mask;
233       int blue_mask;
234
235       ok &= gst_structure_get_int (structure, "depth", &depth);
236       ok &= gst_structure_get_int (structure, "bpp", &bpp);
237       ok &= gst_structure_get_int (structure, "endianness", &endianness);
238       ok &= gst_structure_get_int (structure, "red_mask", &red_mask);
239       ok &= gst_structure_get_int (structure, "green_mask", &green_mask);
240       ok &= gst_structure_get_int (structure, "blue_mask", &blue_mask);
241
242       if (depth != 24 || bpp != 32 || endianness != G_BIG_ENDIAN) {
243         ok = FALSE;
244       } else {
245         *format = gst_video_format_from_rgb32_masks (red_mask, green_mask,
246             blue_mask);
247         if (*format == GST_VIDEO_FORMAT_UNKNOWN) {
248           ok = FALSE;
249         }
250       }
251     } else {
252       ok = FALSE;
253     }
254   }
255
256   if (width) {
257     ok &= gst_structure_get_int (structure, "width", width);
258   }
259
260   if (height) {
261     ok &= gst_structure_get_int (structure, "height", height);
262   }
263
264   return ok;
265 }
266
267 /**
268  * gst_video_parse_caps_framerate:
269  * @caps:
270  * @fps_n: pointer to numerator of frame rate (output)
271  * @fps_d: pointer to denominator of frame rate (output)
272  *
273  * Extracts the frame rate from @caps and places the values in the locations
274  * pointed to by @fps_n and @fps_d.  Returns TRUE if the values could be
275  * parsed correctly, FALSE if not.
276  *
277  * This function can be used with #GstCaps that have any media type; it
278  * is not limited to formats handled by #GstVideoFormat.
279  *
280  * Since: 0.10.16
281  *
282  * Returns: TRUE if @caps was parsed correctly.
283  */
284 gboolean
285 gst_video_parse_caps_framerate (GstCaps * caps, int *fps_n, int *fps_d)
286 {
287   GstStructure *structure;
288
289   if (!gst_caps_is_fixed (caps))
290     return FALSE;
291
292   structure = gst_caps_get_structure (caps, 0);
293
294   return gst_structure_get_fraction (structure, "framerate", fps_n, fps_d);
295 }
296
297 /**
298  * gst_video_parse_caps_pixel_aspect_ratio:
299  * @caps:
300  * @par_n: pointer to numerator of pixel aspect ratio (output)
301  * @par_d: pointer to denominator of pixel aspect ratio (output)
302  *
303  * Extracts the pixel aspect ratio from @caps and places the values in
304  * the locations pointed to by @par_n and @par_d.  Returns TRUE if the
305  * values could be parsed correctly, FALSE if not.
306  *
307  * This function can be used with #GstCaps that have any media type; it
308  * is not limited to formats handled by #GstVideoFormat.
309  *
310  * Since: 0.10.16
311  *
312  * Returns: TRUE if @caps was parsed correctly.
313  */
314 gboolean
315 gst_video_parse_caps_pixel_aspect_ratio (GstCaps * caps, int *par_n, int *par_d)
316 {
317   GstStructure *structure;
318
319   if (!gst_caps_is_fixed (caps))
320     return FALSE;
321
322   structure = gst_caps_get_structure (caps, 0);
323
324   if (!gst_structure_get_fraction (structure, "pixel-aspect-ratio",
325           par_n, par_d)) {
326     *par_n = 1;
327     *par_d = 1;
328   }
329   return TRUE;
330 }
331
332 /**
333  * gst_video_format_new_caps:
334  * @format: the #GstVideoFormat describing the raw video format
335  * @width: width of video
336  * @height: height of video
337  * @framerate_n: numerator of frame rate
338  * @framerate_d: denominator of frame rate
339  * @par_n: numerator of pixel aspect ratio
340  * @par_d: denominator of pixel aspect ratio
341  *
342  * Creates a new #GstCaps object based on the parameters provided.
343  *
344  * Since: 0.10.16
345  *
346  * Returns: a new #GstCaps object, or NULL if there was an error
347  */
348 GstCaps *
349 gst_video_format_new_caps (GstVideoFormat format, int width, int height,
350     int framerate_n, int framerate_d, int par_n, int par_d)
351 {
352   if (gst_video_format_is_yuv (format)) {
353     return gst_caps_new_simple ("video/x-raw-yuv",
354         "format", GST_TYPE_FOURCC, gst_video_format_to_fourcc (format),
355         "width", G_TYPE_INT, width,
356         "height", G_TYPE_INT, height,
357         "framerate", GST_TYPE_FRACTION, framerate_n, framerate_d,
358         "pixel-aspect-ratio", GST_TYPE_FRACTION, par_n, par_d, NULL);
359   }
360   if (gst_video_format_is_rgb (format)) {
361     int red_mask;
362     int blue_mask;
363     int green_mask;
364
365     red_mask =
366         0xff000000U >> gst_video_format_get_component_offset (format, 0, width,
367         height);
368     green_mask =
369         0xff000000U >> gst_video_format_get_component_offset (format, 1, width,
370         height);
371     blue_mask =
372         0xff000000U >> gst_video_format_get_component_offset (format, 2, width,
373         height);
374
375     return gst_caps_new_simple ("video/x-raw-rgb",
376         "bpp", G_TYPE_INT, 32,
377         "depth", G_TYPE_INT, 24,
378         "endianness", G_TYPE_INT, G_BIG_ENDIAN,
379         "red_mask", G_TYPE_INT, red_mask,
380         "green_mask", G_TYPE_INT, green_mask,
381         "blue_mask", G_TYPE_INT, blue_mask,
382         "width", G_TYPE_INT, width,
383         "height", G_TYPE_INT, height,
384         "framerate", GST_TYPE_FRACTION, framerate_n, framerate_d,
385         "pixel-aspect-ratio", GST_TYPE_FRACTION, par_n, par_d, NULL);
386   }
387   return NULL;
388 }
389
390 /**
391  * gst_video_format_from_fourcc:
392  * @fourcc: a FOURCC value representing raw YUV video
393  *
394  * Converts a FOURCC value into the corresponding #GstVideoFormat.
395  * If the FOURCC cannot be represented by #GstVideoFormat,
396  * #GST_VIDEO_FORMAT_UNKNOWN is returned.
397  *
398  * Since: 0.10.16
399  *
400  * Returns: the #GstVideoFormat describing the FOURCC value
401  */
402 GstVideoFormat
403 gst_video_format_from_fourcc (guint32 fourcc)
404 {
405   switch (fourcc) {
406     case GST_MAKE_FOURCC ('I', '4', '2', '0'):
407       return GST_VIDEO_FORMAT_I420;
408     case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
409       return GST_VIDEO_FORMAT_YV12;
410     case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
411       return GST_VIDEO_FORMAT_YUY2;
412     case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
413       return GST_VIDEO_FORMAT_UYVY;
414     case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
415       return GST_VIDEO_FORMAT_AYUV;
416     default:
417       return GST_VIDEO_FORMAT_UNKNOWN;
418   }
419 }
420
421 /**
422  * gst_video_format_to_fourcc:
423  * @format: a #GstVideoFormat video format
424  *
425  * Converts a #GstVideoFormat value into the corresponding FOURCC.  Only
426  * a few YUV formats have corresponding FOURCC values.  If @format has
427  * no corresponding FOURCC value, 0 is returned.
428  *
429  * Since: 0.10.16
430  *
431  * Returns: the FOURCC corresponding to @format
432  */
433 guint32
434 gst_video_format_to_fourcc (GstVideoFormat format)
435 {
436   switch (format) {
437     case GST_VIDEO_FORMAT_I420:
438       return GST_MAKE_FOURCC ('I', '4', '2', '0');
439     case GST_VIDEO_FORMAT_YV12:
440       return GST_MAKE_FOURCC ('Y', 'V', '1', '2');
441     case GST_VIDEO_FORMAT_YUY2:
442       return GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
443     case GST_VIDEO_FORMAT_UYVY:
444       return GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
445     case GST_VIDEO_FORMAT_AYUV:
446       return GST_MAKE_FOURCC ('A', 'Y', 'U', 'V');
447     default:
448       return 0;
449   }
450 }
451
452 /**
453  * gst_video_format_from_rgb32_masks:
454  * @red_mask: red bit mask
455  * @green_mask: green bit mask
456  * @blue_mask: blue bit mask
457  *
458  * Converts red, green, blue bit masks into the corresponding
459  * #GstVideoFormat.  
460  *
461  * Since: 0.10.16
462  *
463  * Returns: the #GstVideoFormat corresponding to the bit masks
464  */
465 GstVideoFormat
466 gst_video_format_from_rgb32_masks (int red_mask, int green_mask, int blue_mask)
467 {
468   if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
469       blue_mask == 0x0000ff00) {
470     return GST_VIDEO_FORMAT_RGBx;
471   }
472   if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
473       blue_mask == 0xff000000) {
474     return GST_VIDEO_FORMAT_BGRx;
475   }
476   if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
477       blue_mask == 0x000000ff) {
478     return GST_VIDEO_FORMAT_xRGB;
479   }
480   if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
481       blue_mask == 0x00ff0000) {
482     return GST_VIDEO_FORMAT_xBGR;
483   }
484
485   return GST_VIDEO_FORMAT_UNKNOWN;
486 }
487
488 /**
489  * gst_video_format_is_rgb:
490  * @format: a #GstVideoFormat
491  *
492  * Since: 0.10.16
493  *
494  * Returns: TRUE if @format represents RGB video
495  */
496 gboolean
497 gst_video_format_is_rgb (GstVideoFormat format)
498 {
499   switch (format) {
500     case GST_VIDEO_FORMAT_I420:
501     case GST_VIDEO_FORMAT_YV12:
502     case GST_VIDEO_FORMAT_YUY2:
503     case GST_VIDEO_FORMAT_UYVY:
504     case GST_VIDEO_FORMAT_AYUV:
505       return FALSE;
506     case GST_VIDEO_FORMAT_RGBx:
507     case GST_VIDEO_FORMAT_BGRx:
508     case GST_VIDEO_FORMAT_xRGB:
509     case GST_VIDEO_FORMAT_xBGR:
510       return TRUE;
511     default:
512       return FALSE;
513   }
514 }
515
516 /**
517  * gst_video_format_is_yuv:
518  * @format: a #GstVideoFormat
519  *
520  * Since: 0.10.16
521  *
522  * Returns: TRUE if @format represents YUV video
523  */
524 gboolean
525 gst_video_format_is_yuv (GstVideoFormat format)
526 {
527   switch (format) {
528     case GST_VIDEO_FORMAT_I420:
529     case GST_VIDEO_FORMAT_YV12:
530     case GST_VIDEO_FORMAT_YUY2:
531     case GST_VIDEO_FORMAT_UYVY:
532     case GST_VIDEO_FORMAT_AYUV:
533       return TRUE;
534     case GST_VIDEO_FORMAT_RGBx:
535     case GST_VIDEO_FORMAT_BGRx:
536     case GST_VIDEO_FORMAT_xRGB:
537     case GST_VIDEO_FORMAT_xBGR:
538       return FALSE;
539     default:
540       return FALSE;
541   }
542 }
543
544 /**
545  * gst_video_format_has_alpha:
546  * @format: a #GstVideoFormat
547  *
548  * Since: 0.10.16
549  *
550  * Returns: TRUE if @format has an alpha channel
551  */
552 gboolean
553 gst_video_format_has_alpha (GstVideoFormat format)
554 {
555   switch (format) {
556     case GST_VIDEO_FORMAT_I420:
557     case GST_VIDEO_FORMAT_YV12:
558     case GST_VIDEO_FORMAT_YUY2:
559     case GST_VIDEO_FORMAT_UYVY:
560       return FALSE;
561     case GST_VIDEO_FORMAT_AYUV:
562       return TRUE;
563     case GST_VIDEO_FORMAT_RGBx:
564     case GST_VIDEO_FORMAT_BGRx:
565     case GST_VIDEO_FORMAT_xRGB:
566     case GST_VIDEO_FORMAT_xBGR:
567       return FALSE;
568     default:
569       return FALSE;
570   }
571 }
572
573 /**
574  * gst_video_format_get_row_stride:
575  * @format: a #GstVideoFormat
576  * @component: the component index
577  * @width: the width of video
578  *
579  * Calculates the row stride (number of bytes from one row of pixels to
580  * the next) for the video component with an index of @component.  For
581  * YUV video, Y, U, and V have component indices of 0, 1, and 2,
582  * respectively.  For RGB video, R, G, and B have component indicies of
583  * 0, 1, and 2, respectively.  Alpha channels, if present, have a component
584  * index of 3.  The @width parameter always represents the width of the
585  * video, not the component.
586  *
587  * Since: 0.10.16
588  *
589  * Returns: row stride of component @component
590  */
591 int
592 gst_video_format_get_row_stride (GstVideoFormat format, int component,
593     int width)
594 {
595   switch (format) {
596     case GST_VIDEO_FORMAT_I420:
597     case GST_VIDEO_FORMAT_YV12:
598       if (component == 0) {
599         return GST_ROUND_UP_4 (width);
600       } else {
601         return GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
602       }
603     case GST_VIDEO_FORMAT_YUY2:
604     case GST_VIDEO_FORMAT_UYVY:
605       return GST_ROUND_UP_4 (width * 2);
606     case GST_VIDEO_FORMAT_AYUV:
607     case GST_VIDEO_FORMAT_RGBx:
608     case GST_VIDEO_FORMAT_BGRx:
609     case GST_VIDEO_FORMAT_xRGB:
610     case GST_VIDEO_FORMAT_xBGR:
611       return width * 4;
612     default:
613       return 0;
614   }
615 }
616
617 /**
618  * gst_video_format_get_pixel_stride:
619  * @format: a #GstVideoFormat
620  * @component: the component index
621  *
622  * Calculates the pixel stride (number of bytes from one pixel to the
623  * pixel to its immediate left) for the video component with an index
624  * of @component.  See @gst_video_format_get_row_stride for a description
625  * of the component index.
626  *
627  * Since: 0.10.16
628  *
629  * Returns: pixel stride of component @component
630  */
631 int
632 gst_video_format_get_pixel_stride (GstVideoFormat format, int component)
633 {
634   switch (format) {
635     case GST_VIDEO_FORMAT_I420:
636     case GST_VIDEO_FORMAT_YV12:
637       return 1;
638     case GST_VIDEO_FORMAT_YUY2:
639     case GST_VIDEO_FORMAT_UYVY:
640       if (component == 0) {
641         return 2;
642       } else {
643         return 4;
644       }
645     case GST_VIDEO_FORMAT_AYUV:
646     case GST_VIDEO_FORMAT_RGBx:
647     case GST_VIDEO_FORMAT_BGRx:
648     case GST_VIDEO_FORMAT_xRGB:
649     case GST_VIDEO_FORMAT_xBGR:
650       return 4;
651     default:
652       return 0;
653   }
654 }
655
656 /**
657  * gst_video_format_get_component_width:
658  * @format: a #GstVideoFormat
659  * @component: the component index
660  * @width: the width of video
661  *
662  * Calculates the width of the component.  See
663  * @gst_video_format_get_row_stride for a description
664  * of the component index.
665  *
666  * Since: 0.10.16
667  *
668  * Returns: width of component @component
669  */
670 int
671 gst_video_format_get_component_width (GstVideoFormat format, int component,
672     int width)
673 {
674   switch (format) {
675     case GST_VIDEO_FORMAT_I420:
676     case GST_VIDEO_FORMAT_YV12:
677     case GST_VIDEO_FORMAT_YUY2:
678     case GST_VIDEO_FORMAT_UYVY:
679       if (component == 0) {
680         return width;
681       } else {
682         return GST_ROUND_UP_2 (width) / 2;
683       }
684     case GST_VIDEO_FORMAT_AYUV:
685     case GST_VIDEO_FORMAT_RGBx:
686     case GST_VIDEO_FORMAT_BGRx:
687     case GST_VIDEO_FORMAT_xRGB:
688     case GST_VIDEO_FORMAT_xBGR:
689       return width;
690     default:
691       return 0;
692   }
693 }
694
695 /**
696  * gst_video_format_get_component_height:
697  * @format: a #GstVideoFormat
698  * @component: the component index
699  * @height: the height of video
700  *
701  * Calculates the height of the component.  See
702  * @gst_video_format_get_row_stride for a description
703  * of the component index.
704  *
705  * Since: 0.10.16
706  *
707  * Returns: height of component @component
708  */
709 int
710 gst_video_format_get_component_height (GstVideoFormat format, int component,
711     int height)
712 {
713   switch (format) {
714     case GST_VIDEO_FORMAT_I420:
715     case GST_VIDEO_FORMAT_YV12:
716       if (component == 0) {
717         return height;
718       } else {
719         return GST_ROUND_UP_2 (height) / 2;
720       }
721     case GST_VIDEO_FORMAT_YUY2:
722     case GST_VIDEO_FORMAT_UYVY:
723     case GST_VIDEO_FORMAT_AYUV:
724     case GST_VIDEO_FORMAT_RGBx:
725     case GST_VIDEO_FORMAT_BGRx:
726     case GST_VIDEO_FORMAT_xRGB:
727     case GST_VIDEO_FORMAT_xBGR:
728       return height;
729     default:
730       return 0;
731   }
732 }
733
734 /**
735  * gst_video_format_get_component_offset:
736  * @format: a #GstVideoFormat
737  * @component: the component index
738  * @width: the width of video
739  * @height: the height of video
740  *
741  * Calculates the offset (in bytes) of the first pixel of the component
742  * with index @component.  For packed formats, this will typically be a
743  * small integer (0, 1, 2, 3).  For planar formats, this will be a
744  * (relatively) large offset to the beginning of the second or third
745  * component planes.  See @gst_video_format_get_row_stride for a description
746  * of the component index.
747  *
748  * Since: 0.10.16
749  *
750  * Returns: offset of component @component
751  */
752 int
753 gst_video_format_get_component_offset (GstVideoFormat format, int component,
754     int width, int height)
755 {
756   switch (format) {
757     case GST_VIDEO_FORMAT_I420:
758     case GST_VIDEO_FORMAT_YV12:
759       if (component == 0) {
760         return 0;
761       } else {
762         int offset;
763
764         offset = GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
765         if (component == 2) {
766           offset += GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
767               (GST_ROUND_UP_2 (height) / 2);
768         }
769         return offset;
770       }
771     case GST_VIDEO_FORMAT_YUY2:
772       if (component == 0)
773         return 0;
774       if (component == 1)
775         return 1;
776       if (component == 2)
777         return 3;
778       return 0;
779     case GST_VIDEO_FORMAT_UYVY:
780       if (component == 0)
781         return 1;
782       if (component == 1)
783         return 0;
784       if (component == 2)
785         return 2;
786       return 0;
787     case GST_VIDEO_FORMAT_AYUV:
788       if (component == 0)
789         return 1;
790       if (component == 1)
791         return 2;
792       if (component == 2)
793         return 3;
794       if (component == 3)
795         return 0;
796       return 0;
797     case GST_VIDEO_FORMAT_RGBx:
798       if (component == 0)
799         return 0;
800       if (component == 1)
801         return 1;
802       if (component == 2)
803         return 2;
804       if (component == 3)
805         return 3;
806       return 0;
807     case GST_VIDEO_FORMAT_BGRx:
808       if (component == 0)
809         return 2;
810       if (component == 1)
811         return 1;
812       if (component == 2)
813         return 0;
814       if (component == 3)
815         return 3;
816       return 0;
817     case GST_VIDEO_FORMAT_xRGB:
818       if (component == 0)
819         return 1;
820       if (component == 1)
821         return 2;
822       if (component == 2)
823         return 3;
824       if (component == 3)
825         return 0;
826       return 0;
827     case GST_VIDEO_FORMAT_xBGR:
828       if (component == 0)
829         return 3;
830       if (component == 1)
831         return 2;
832       if (component == 2)
833         return 1;
834       if (component == 3)
835         return 0;
836       return 0;
837     default:
838       return 0;
839   }
840 }
841
842 /**
843  * gst_video_format_get_size:
844  * @format: a #GstVideoFormat
845  * @width: the width of video
846  * @height: the height of video
847  *
848  * Calculates the total number of bytes in the raw video format.  This
849  * number should be used when allocating a buffer for raw video.
850  *
851  * Since: 0.10.16
852  *
853  * Returns: size (in bytes) of raw video format
854  */
855 int
856 gst_video_format_get_size (GstVideoFormat format, int width, int height)
857 {
858   int size;
859
860   switch (format) {
861     case GST_VIDEO_FORMAT_I420:
862     case GST_VIDEO_FORMAT_YV12:
863       size = GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
864       size += GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
865           (GST_ROUND_UP_2 (height) / 2) * 2;
866       return size;
867     case GST_VIDEO_FORMAT_YUY2:
868     case GST_VIDEO_FORMAT_UYVY:
869       return GST_ROUND_UP_4 (width * 2) * height;
870     case GST_VIDEO_FORMAT_AYUV:
871     case GST_VIDEO_FORMAT_RGBx:
872     case GST_VIDEO_FORMAT_BGRx:
873     case GST_VIDEO_FORMAT_xRGB:
874     case GST_VIDEO_FORMAT_xBGR:
875       return width * 4 * height;
876     default:
877       return 0;
878   }
879 }
880
881 /**
882  * gst_video_format_convert:
883  * @format: a #GstVideoFormat
884  * @width: the width of video
885  * @height: the height of video
886  * @fps_n: frame rate numerator
887  * @fps_d: frame rate denominator
888  * @src_format: #GstFormat of the @src_value
889  * @src_value: value to convert
890  * @dest_format: #GstFormat of the @dest_value
891  * @dest_value: pointer to destination value
892  *
893  * Converts among various #GstFormat types.  This function handles
894  * GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT.  For
895  * raw video, GST_FORMAT_DEFAULT corresponds to video frames.  This
896  * function can be to handle pad queries of the type GST_QUERY_CONVERT.
897  *
898  * Since: 0.10.16
899  *
900  * Returns: TRUE if the conversion was successful.
901  */
902 gboolean
903 gst_video_format_convert (GstVideoFormat format, int width, int height,
904     int fps_n, int fps_d,
905     GstFormat src_format, gint64 src_value,
906     GstFormat dest_format, gint64 * dest_value)
907 {
908   gboolean ret = FALSE;
909   int size;
910
911   size = gst_video_format_get_size (format, width, height);
912
913   GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s to %s",
914       src_value, gst_format_get_name (src_format),
915       gst_format_get_name (dest_format));
916
917   if (src_format == dest_format) {
918     *dest_value = src_value;
919     ret = TRUE;
920     goto done;
921   }
922
923   if (src_value == -1) {
924     *dest_value = -1;
925     ret = TRUE;
926     goto done;
927   }
928
929   /* bytes to frames */
930   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_DEFAULT) {
931     if (size != 0) {
932       *dest_value = gst_util_uint64_scale_int (src_value, 1, size);
933     } else {
934       GST_ERROR ("blocksize is 0");
935       *dest_value = 0;
936     }
937     ret = TRUE;
938     goto done;
939   }
940
941   /* frames to bytes */
942   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_BYTES) {
943     *dest_value = gst_util_uint64_scale_int (src_value, size, 1);
944     ret = TRUE;
945     goto done;
946   }
947
948   /* time to frames */
949   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_DEFAULT) {
950     if (fps_d != 0) {
951       *dest_value = gst_util_uint64_scale (src_value,
952           fps_n, GST_SECOND * fps_d);
953     } else {
954       GST_ERROR ("framerate denominator is 0");
955       *dest_value = 0;
956     }
957     ret = TRUE;
958     goto done;
959   }
960
961   /* frames to time */
962   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) {
963     if (fps_n != 0) {
964       *dest_value = gst_util_uint64_scale (src_value,
965           GST_SECOND * fps_d, fps_n);
966     } else {
967       GST_ERROR ("framerate numerator is 0");
968       *dest_value = 0;
969     }
970     ret = TRUE;
971     goto done;
972   }
973
974   /* time to bytes */
975   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
976     if (fps_d != 0) {
977       *dest_value = gst_util_uint64_scale (src_value,
978           fps_n * size, GST_SECOND * fps_d);
979     } else {
980       GST_ERROR ("framerate denominator is 0");
981       *dest_value = 0;
982     }
983     ret = TRUE;
984     goto done;
985   }
986
987   /* bytes to time */
988   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
989     if (fps_n != 0 && size != 0) {
990       *dest_value = gst_util_uint64_scale (src_value,
991           GST_SECOND * fps_d, fps_n * size);
992     } else {
993       GST_ERROR ("framerate denominator and/or blocksize is 0");
994       *dest_value = 0;
995     }
996     ret = TRUE;
997   }
998
999 done:
1000
1001   GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, ret, *dest_value);
1002
1003   return ret;
1004 }