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>
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.
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.
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.
30 * @short_description: Support library for video operations
34 * This library contains some helper functions and includes the
35 * videosink and videofilter base classes.
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);
49 * gst_video_frame_rate:
50 * @pad: pointer to a #GstPad
52 * A convenience function to retrieve a GValue holding the framerate
53 * from the caps on a pad.
55 * The pad needs to have negotiated caps containing a framerate property.
57 * Returns: NULL if the pad has no configured caps or the configured caps
58 * do not contain a framerate.
62 gst_video_frame_rate (GstPad * pad)
67 const GstCaps *caps = NULL;
68 GstStructure *structure;
71 caps = GST_PAD_CAPS (pad);
73 g_warning ("gstvideo: failed to get caps of pad %s:%s",
74 GST_DEBUG_PAD_NAME (pad));
78 structure = gst_caps_get_structure (caps, 0);
79 if ((fps = gst_structure_get_value (structure, "framerate")) == NULL) {
80 g_warning ("gstvideo: failed to get framerate property of pad %s:%s",
81 GST_DEBUG_PAD_NAME (pad));
84 if (!GST_VALUE_HOLDS_FRACTION (fps)) {
86 ("gstvideo: framerate property of pad %s:%s is not of type Fraction",
87 GST_DEBUG_PAD_NAME (pad));
91 fps_string = gst_value_serialize (fps);
92 GST_DEBUG ("Framerate request on pad %s:%s: %s",
93 GST_DEBUG_PAD_NAME (pad), fps_string);
100 * gst_video_get_size:
101 * @pad: pointer to a #GstPad
102 * @width: pointer to integer to hold pixel width of the video frames (output)
103 * @height: pointer to integer to hold pixel height of the video frames (output)
105 * Inspect the caps of the provided pad and retrieve the width and height of
106 * the video frames it is configured for.
108 * The pad needs to have negotiated caps containing width and height properties.
110 * Returns: TRUE if the width and height could be retrieved.
114 gst_video_get_size (GstPad * pad, gint * width, gint * height)
116 const GstCaps *caps = NULL;
117 GstStructure *structure;
120 g_return_val_if_fail (pad != NULL, FALSE);
121 g_return_val_if_fail (width != NULL, FALSE);
122 g_return_val_if_fail (height != NULL, FALSE);
124 caps = GST_PAD_CAPS (pad);
127 g_warning ("gstvideo: failed to get caps of pad %s:%s",
128 GST_DEBUG_PAD_NAME (pad));
132 structure = gst_caps_get_structure (caps, 0);
133 ret = gst_structure_get_int (structure, "width", width);
134 ret &= gst_structure_get_int (structure, "height", height);
137 g_warning ("gstvideo: failed to get size properties on pad %s:%s",
138 GST_DEBUG_PAD_NAME (pad));
142 GST_DEBUG ("size request on pad %s:%s: %dx%d",
143 GST_DEBUG_PAD_NAME (pad), width ? *width : -1, height ? *height : -1);
149 * gst_video_calculate_display_ratio:
150 * @dar_n: Numerator of the calculated display_ratio
151 * @dar_d: Denominator of the calculated display_ratio
152 * @video_width: Width of the video frame in pixels
153 * @video_height: Height of the video frame in pixels
154 * @video_par_n: Numerator of the pixel aspect ratio of the input video.
155 * @video_par_d: Denominator of the pixel aspect ratio of the input video.
156 * @display_par_n: Numerator of the pixel aspect ratio of the display device
157 * @display_par_d: Denominator of the pixel aspect ratio of the display device
159 * Given the Pixel Aspect Ratio and size of an input video frame, and the
160 * pixel aspect ratio of the intended display device, calculates the actual
161 * display ratio the video will be rendered with.
163 * Returns: A boolean indicating success and a calculated Display Ratio in the
164 * dar_n and dar_d parameters.
165 * The return value is FALSE in the case of integer overflow or other error.
170 gst_video_calculate_display_ratio (guint * dar_n, guint * dar_d,
171 guint video_width, guint video_height,
172 guint video_par_n, guint video_par_d,
173 guint display_par_n, guint display_par_d)
177 GValue display_ratio = { 0, };
179 GValue tmp2 = { 0, };
181 g_return_val_if_fail (dar_n != NULL, FALSE);
182 g_return_val_if_fail (dar_d != NULL, FALSE);
184 g_value_init (&display_ratio, GST_TYPE_FRACTION);
185 g_value_init (&tmp, GST_TYPE_FRACTION);
186 g_value_init (&tmp2, GST_TYPE_FRACTION);
188 /* Calculate (video_width * video_par_n * display_par_d) /
189 * (video_height * video_par_d * display_par_n) */
190 gst_value_set_fraction (&display_ratio, video_width, video_height);
191 gst_value_set_fraction (&tmp, video_par_n, video_par_d);
193 if (!gst_value_fraction_multiply (&tmp2, &display_ratio, &tmp))
196 gst_value_set_fraction (&tmp, display_par_d, display_par_n);
198 if (!gst_value_fraction_multiply (&display_ratio, &tmp2, &tmp))
201 num = gst_value_get_fraction_numerator (&display_ratio);
202 den = gst_value_get_fraction_denominator (&display_ratio);
204 g_value_unset (&display_ratio);
205 g_value_unset (&tmp);
206 g_value_unset (&tmp2);
208 g_return_val_if_fail (num > 0, FALSE);
209 g_return_val_if_fail (den > 0, FALSE);
216 g_value_unset (&display_ratio);
217 g_value_unset (&tmp);
218 g_value_unset (&tmp2);
223 * gst_video_format_parse_caps:
224 * @caps: the #GstCaps to parse
225 * @format: the #GstVideoFormat of the video represented by @caps (output)
226 * @width: the width of the video represented by @caps, may be NULL (output)
227 * @height: the height of the video represented by @caps, may be NULL (output)
229 * Determines the #GstVideoFormat of @caps and places it in the location
230 * pointed to by @format. Extracts the size of the video and places it
231 * in the location pointed to by @width and @height. If @caps does not
232 * represent one of the raw video formats listed in #GstVideoFormat, the
233 * function will fail and return FALSE.
237 * Returns: TRUE if @caps was parsed correctly.
240 gst_video_format_parse_caps (GstCaps * caps, GstVideoFormat * format,
241 int *width, int *height)
243 GstStructure *structure;
246 /* caps must hold exactly 1 structure to be unambiguous,
247 * but need not be fixed to determine format */
248 if (gst_caps_get_size (caps) != 1)
251 structure = gst_caps_get_structure (caps, 0);
254 if (gst_structure_has_name (structure, "video/x-raw-yuv")) {
257 ok &= gst_structure_get_fourcc (structure, "format", &fourcc);
259 *format = gst_video_format_from_fourcc (fourcc);
260 if (*format == GST_VIDEO_FORMAT_UNKNOWN) {
263 } else if (gst_structure_has_name (structure, "video/x-raw-rgb")) {
273 ok &= gst_structure_get_int (structure, "depth", &depth);
274 ok &= gst_structure_get_int (structure, "bpp", &bpp);
275 ok &= gst_structure_get_int (structure, "endianness", &endianness);
276 ok &= gst_structure_get_int (structure, "red_mask", &red_mask);
277 ok &= gst_structure_get_int (structure, "green_mask", &green_mask);
278 ok &= gst_structure_get_int (structure, "blue_mask", &blue_mask);
279 have_alpha = gst_structure_get_int (structure, "alpha_mask", &alpha_mask);
281 if (depth == 24 && bpp == 32 && endianness == G_BIG_ENDIAN) {
282 *format = gst_video_format_from_rgb32_masks (red_mask, green_mask,
284 if (*format == GST_VIDEO_FORMAT_UNKNOWN) {
287 } else if (depth == 32 && bpp == 32 && endianness == G_BIG_ENDIAN &&
289 *format = gst_video_format_from_rgba32_masks (red_mask, green_mask,
290 blue_mask, alpha_mask);
291 if (*format == GST_VIDEO_FORMAT_UNKNOWN) {
294 } else if (depth == 24 && bpp == 24 && endianness == G_BIG_ENDIAN) {
295 *format = gst_video_format_from_rgb24_masks (red_mask, green_mask,
297 if (*format == GST_VIDEO_FORMAT_UNKNOWN) {
309 ok &= gst_structure_get_int (structure, "width", width);
313 ok &= gst_structure_get_int (structure, "height", height);
320 * gst_video_parse_caps_framerate:
321 * @caps: pointer to a #GstCaps instance
322 * @fps_n: pointer to integer to hold numerator of frame rate (output)
323 * @fps_d: pointer to integer to hold denominator of frame rate (output)
325 * Extracts the frame rate from @caps and places the values in the locations
326 * pointed to by @fps_n and @fps_d. Returns TRUE if the values could be
327 * parsed correctly, FALSE if not.
329 * This function can be used with #GstCaps that have any media type; it
330 * is not limited to formats handled by #GstVideoFormat.
334 * Returns: TRUE if @caps was parsed correctly.
337 gst_video_parse_caps_framerate (GstCaps * caps, int *fps_n, int *fps_d)
339 GstStructure *structure;
341 if (!gst_caps_is_fixed (caps))
344 structure = gst_caps_get_structure (caps, 0);
346 return gst_structure_get_fraction (structure, "framerate", fps_n, fps_d);
350 * gst_video_parse_caps_pixel_aspect_ratio:
351 * @caps: pointer to a #GstCaps instance
352 * @par_n: pointer to numerator of pixel aspect ratio (output)
353 * @par_d: pointer to denominator of pixel aspect ratio (output)
355 * Extracts the pixel aspect ratio from @caps and places the values in
356 * the locations pointed to by @par_n and @par_d. Returns TRUE if the
357 * values could be parsed correctly, FALSE if not.
359 * This function can be used with #GstCaps that have any media type; it
360 * is not limited to formats handled by #GstVideoFormat.
364 * Returns: TRUE if @caps was parsed correctly.
367 gst_video_parse_caps_pixel_aspect_ratio (GstCaps * caps, int *par_n, int *par_d)
369 GstStructure *structure;
371 if (!gst_caps_is_fixed (caps))
374 structure = gst_caps_get_structure (caps, 0);
376 if (!gst_structure_get_fraction (structure, "pixel-aspect-ratio",
385 * gst_video_format_new_caps:
386 * @format: the #GstVideoFormat describing the raw video format
387 * @width: width of video
388 * @height: height of video
389 * @framerate_n: numerator of frame rate
390 * @framerate_d: denominator of frame rate
391 * @par_n: numerator of pixel aspect ratio
392 * @par_d: denominator of pixel aspect ratio
394 * Creates a new #GstCaps object based on the parameters provided.
398 * Returns: a new #GstCaps object, or NULL if there was an error
401 gst_video_format_new_caps (GstVideoFormat format, int width, int height,
402 int framerate_n, int framerate_d, int par_n, int par_d)
404 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
405 g_return_val_if_fail (width > 0 && height > 0, NULL);
407 if (gst_video_format_is_yuv (format)) {
408 return gst_caps_new_simple ("video/x-raw-yuv",
409 "format", GST_TYPE_FOURCC, gst_video_format_to_fourcc (format),
410 "width", G_TYPE_INT, width,
411 "height", G_TYPE_INT, height,
412 "framerate", GST_TYPE_FRACTION, framerate_n, framerate_d,
413 "pixel-aspect-ratio", GST_TYPE_FRACTION, par_n, par_d, NULL);
415 if (gst_video_format_is_rgb (format)) {
427 case GST_VIDEO_FORMAT_RGBx:
428 case GST_VIDEO_FORMAT_BGRx:
429 case GST_VIDEO_FORMAT_xRGB:
430 case GST_VIDEO_FORMAT_xBGR:
435 case GST_VIDEO_FORMAT_RGBA:
436 case GST_VIDEO_FORMAT_BGRA:
437 case GST_VIDEO_FORMAT_ARGB:
438 case GST_VIDEO_FORMAT_ABGR:
443 case GST_VIDEO_FORMAT_RGB:
444 case GST_VIDEO_FORMAT_BGR:
458 mask >> (8 * gst_video_format_get_component_offset (format, 0, width,
461 mask >> (8 * gst_video_format_get_component_offset (format, 1, width,
464 mask >> (8 * gst_video_format_get_component_offset (format, 2, width,
467 caps = gst_caps_new_simple ("video/x-raw-rgb",
468 "bpp", G_TYPE_INT, bpp,
469 "depth", G_TYPE_INT, depth,
470 "endianness", G_TYPE_INT, G_BIG_ENDIAN,
471 "red_mask", G_TYPE_INT, red_mask,
472 "green_mask", G_TYPE_INT, green_mask,
473 "blue_mask", G_TYPE_INT, blue_mask,
474 "width", G_TYPE_INT, width,
475 "height", G_TYPE_INT, height,
476 "framerate", GST_TYPE_FRACTION, framerate_n, framerate_d,
477 "pixel-aspect-ratio", GST_TYPE_FRACTION, par_n, par_d, NULL);
480 mask >> (8 * gst_video_format_get_component_offset (format, 3, width,
482 gst_caps_set_simple (caps, "alpha_mask", G_TYPE_INT, alpha_mask, NULL);
490 * gst_video_format_from_fourcc:
491 * @fourcc: a FOURCC value representing raw YUV video
493 * Converts a FOURCC value into the corresponding #GstVideoFormat.
494 * If the FOURCC cannot be represented by #GstVideoFormat,
495 * #GST_VIDEO_FORMAT_UNKNOWN is returned.
499 * Returns: the #GstVideoFormat describing the FOURCC value
502 gst_video_format_from_fourcc (guint32 fourcc)
505 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
506 return GST_VIDEO_FORMAT_I420;
507 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
508 return GST_VIDEO_FORMAT_YV12;
509 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
510 return GST_VIDEO_FORMAT_YUY2;
511 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
512 return GST_VIDEO_FORMAT_UYVY;
513 case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
514 return GST_VIDEO_FORMAT_AYUV;
515 case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
516 return GST_VIDEO_FORMAT_Y41B;
517 case GST_MAKE_FOURCC ('Y', '4', '2', 'B'):
518 return GST_VIDEO_FORMAT_Y42B;
520 return GST_VIDEO_FORMAT_UNKNOWN;
525 * gst_video_format_to_fourcc:
526 * @format: a #GstVideoFormat video format
528 * Converts a #GstVideoFormat value into the corresponding FOURCC. Only
529 * a few YUV formats have corresponding FOURCC values. If @format has
530 * no corresponding FOURCC value, 0 is returned.
534 * Returns: the FOURCC corresponding to @format
537 gst_video_format_to_fourcc (GstVideoFormat format)
539 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
542 case GST_VIDEO_FORMAT_I420:
543 return GST_MAKE_FOURCC ('I', '4', '2', '0');
544 case GST_VIDEO_FORMAT_YV12:
545 return GST_MAKE_FOURCC ('Y', 'V', '1', '2');
546 case GST_VIDEO_FORMAT_YUY2:
547 return GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
548 case GST_VIDEO_FORMAT_UYVY:
549 return GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
550 case GST_VIDEO_FORMAT_AYUV:
551 return GST_MAKE_FOURCC ('A', 'Y', 'U', 'V');
552 case GST_VIDEO_FORMAT_Y41B:
553 return GST_MAKE_FOURCC ('Y', '4', '1', 'B');
554 case GST_VIDEO_FORMAT_Y42B:
555 return GST_MAKE_FOURCC ('Y', '4', '2', 'B');
562 * gst_video_format_from_rgb32_masks:
563 * @red_mask: red bit mask
564 * @green_mask: green bit mask
565 * @blue_mask: blue bit mask
567 * Converts red, green, blue bit masks into the corresponding
572 * Returns: the #GstVideoFormat corresponding to the bit masks
574 static GstVideoFormat
575 gst_video_format_from_rgb32_masks (int red_mask, int green_mask, int blue_mask)
577 if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
578 blue_mask == 0x0000ff00) {
579 return GST_VIDEO_FORMAT_RGBx;
581 if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
582 blue_mask == 0xff000000) {
583 return GST_VIDEO_FORMAT_BGRx;
585 if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
586 blue_mask == 0x000000ff) {
587 return GST_VIDEO_FORMAT_xRGB;
589 if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
590 blue_mask == 0x00ff0000) {
591 return GST_VIDEO_FORMAT_xBGR;
594 return GST_VIDEO_FORMAT_UNKNOWN;
597 static GstVideoFormat
598 gst_video_format_from_rgba32_masks (int red_mask, int green_mask, int blue_mask,
601 if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
602 blue_mask == 0x0000ff00 && alpha_mask == 0x000000ff) {
603 return GST_VIDEO_FORMAT_RGBA;
605 if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
606 blue_mask == 0xff000000 && alpha_mask == 0x000000ff) {
607 return GST_VIDEO_FORMAT_BGRA;
609 if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
610 blue_mask == 0x000000ff && alpha_mask == 0xff000000) {
611 return GST_VIDEO_FORMAT_ARGB;
613 if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
614 blue_mask == 0x00ff0000 && alpha_mask == 0xff000000) {
615 return GST_VIDEO_FORMAT_ABGR;
618 return GST_VIDEO_FORMAT_UNKNOWN;
621 static GstVideoFormat
622 gst_video_format_from_rgb24_masks (int red_mask, int green_mask, int blue_mask)
624 if (red_mask == 0xff0000 && green_mask == 0x00ff00 && blue_mask == 0x0000ff) {
625 return GST_VIDEO_FORMAT_RGB;
627 if (red_mask == 0x0000ff && green_mask == 0x00ff00 && blue_mask == 0xff0000) {
628 return GST_VIDEO_FORMAT_BGR;
631 return GST_VIDEO_FORMAT_UNKNOWN;
635 * gst_video_format_is_rgb:
636 * @format: a #GstVideoFormat
638 * Determine whether the video format is an RGB format.
642 * Returns: TRUE if @format represents RGB video
645 gst_video_format_is_rgb (GstVideoFormat format)
648 case GST_VIDEO_FORMAT_I420:
649 case GST_VIDEO_FORMAT_YV12:
650 case GST_VIDEO_FORMAT_YUY2:
651 case GST_VIDEO_FORMAT_UYVY:
652 case GST_VIDEO_FORMAT_AYUV:
653 case GST_VIDEO_FORMAT_Y41B:
654 case GST_VIDEO_FORMAT_Y42B:
656 case GST_VIDEO_FORMAT_RGBx:
657 case GST_VIDEO_FORMAT_BGRx:
658 case GST_VIDEO_FORMAT_xRGB:
659 case GST_VIDEO_FORMAT_xBGR:
660 case GST_VIDEO_FORMAT_RGBA:
661 case GST_VIDEO_FORMAT_BGRA:
662 case GST_VIDEO_FORMAT_ARGB:
663 case GST_VIDEO_FORMAT_ABGR:
664 case GST_VIDEO_FORMAT_RGB:
665 case GST_VIDEO_FORMAT_BGR:
673 * gst_video_format_is_yuv:
674 * @format: a #GstVideoFormat
676 * Determine whether the video format is a YUV format.
680 * Returns: TRUE if @format represents YUV video
683 gst_video_format_is_yuv (GstVideoFormat format)
686 case GST_VIDEO_FORMAT_I420:
687 case GST_VIDEO_FORMAT_YV12:
688 case GST_VIDEO_FORMAT_YUY2:
689 case GST_VIDEO_FORMAT_UYVY:
690 case GST_VIDEO_FORMAT_AYUV:
691 case GST_VIDEO_FORMAT_Y41B:
692 case GST_VIDEO_FORMAT_Y42B:
694 case GST_VIDEO_FORMAT_RGBx:
695 case GST_VIDEO_FORMAT_BGRx:
696 case GST_VIDEO_FORMAT_xRGB:
697 case GST_VIDEO_FORMAT_xBGR:
698 case GST_VIDEO_FORMAT_RGBA:
699 case GST_VIDEO_FORMAT_BGRA:
700 case GST_VIDEO_FORMAT_ARGB:
701 case GST_VIDEO_FORMAT_ABGR:
702 case GST_VIDEO_FORMAT_RGB:
703 case GST_VIDEO_FORMAT_BGR:
711 * gst_video_format_has_alpha:
712 * @format: a #GstVideoFormat
714 * Returns TRUE or FALSE depending on if the video format provides an
719 * Returns: TRUE if @format has an alpha channel
722 gst_video_format_has_alpha (GstVideoFormat format)
725 case GST_VIDEO_FORMAT_I420:
726 case GST_VIDEO_FORMAT_YV12:
727 case GST_VIDEO_FORMAT_YUY2:
728 case GST_VIDEO_FORMAT_UYVY:
729 case GST_VIDEO_FORMAT_Y41B:
730 case GST_VIDEO_FORMAT_Y42B:
732 case GST_VIDEO_FORMAT_AYUV:
733 case GST_VIDEO_FORMAT_RGBA:
734 case GST_VIDEO_FORMAT_BGRA:
735 case GST_VIDEO_FORMAT_ARGB:
736 case GST_VIDEO_FORMAT_ABGR:
738 case GST_VIDEO_FORMAT_RGBx:
739 case GST_VIDEO_FORMAT_BGRx:
740 case GST_VIDEO_FORMAT_xRGB:
741 case GST_VIDEO_FORMAT_xBGR:
742 case GST_VIDEO_FORMAT_RGB:
743 case GST_VIDEO_FORMAT_BGR:
751 * gst_video_format_get_row_stride:
752 * @format: a #GstVideoFormat
753 * @component: the component index
754 * @width: the width of video
756 * Calculates the row stride (number of bytes from one row of pixels to
757 * the next) for the video component with an index of @component. For
758 * YUV video, Y, U, and V have component indices of 0, 1, and 2,
759 * respectively. For RGB video, R, G, and B have component indicies of
760 * 0, 1, and 2, respectively. Alpha channels, if present, have a component
761 * index of 3. The @width parameter always represents the width of the
762 * video, not the component.
766 * Returns: row stride of component @component
769 gst_video_format_get_row_stride (GstVideoFormat format, int component,
772 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
773 g_return_val_if_fail (component >= 0 && component <= 3, 0);
774 g_return_val_if_fail (width > 0, 0);
777 case GST_VIDEO_FORMAT_I420:
778 case GST_VIDEO_FORMAT_YV12:
779 if (component == 0) {
780 return GST_ROUND_UP_4 (width);
782 return GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
784 case GST_VIDEO_FORMAT_YUY2:
785 case GST_VIDEO_FORMAT_UYVY:
786 return GST_ROUND_UP_4 (width * 2);
787 case GST_VIDEO_FORMAT_AYUV:
788 case GST_VIDEO_FORMAT_RGBx:
789 case GST_VIDEO_FORMAT_BGRx:
790 case GST_VIDEO_FORMAT_xRGB:
791 case GST_VIDEO_FORMAT_xBGR:
792 case GST_VIDEO_FORMAT_RGBA:
793 case GST_VIDEO_FORMAT_BGRA:
794 case GST_VIDEO_FORMAT_ARGB:
795 case GST_VIDEO_FORMAT_ABGR:
797 case GST_VIDEO_FORMAT_RGB:
798 case GST_VIDEO_FORMAT_BGR:
799 return GST_ROUND_UP_4 (width * 3);
800 case GST_VIDEO_FORMAT_Y41B:
801 if (component == 0) {
802 return GST_ROUND_UP_4 (width);
804 return GST_ROUND_UP_8 (width) / 4;
806 case GST_VIDEO_FORMAT_Y42B:
807 if (component == 0) {
808 return GST_ROUND_UP_4 (width);
810 return GST_ROUND_UP_8 (width) / 2;
818 * gst_video_format_get_pixel_stride:
819 * @format: a #GstVideoFormat
820 * @component: the component index
822 * Calculates the pixel stride (number of bytes from one pixel to the
823 * pixel to its immediate left) for the video component with an index
824 * of @component. See @gst_video_format_get_row_stride for a description
825 * of the component index.
829 * Returns: pixel stride of component @component
832 gst_video_format_get_pixel_stride (GstVideoFormat format, int component)
834 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
835 g_return_val_if_fail (component >= 0 && component <= 3, 0);
838 case GST_VIDEO_FORMAT_I420:
839 case GST_VIDEO_FORMAT_YV12:
840 case GST_VIDEO_FORMAT_Y41B:
841 case GST_VIDEO_FORMAT_Y42B:
843 case GST_VIDEO_FORMAT_YUY2:
844 case GST_VIDEO_FORMAT_UYVY:
845 if (component == 0) {
850 case GST_VIDEO_FORMAT_AYUV:
851 case GST_VIDEO_FORMAT_RGBx:
852 case GST_VIDEO_FORMAT_BGRx:
853 case GST_VIDEO_FORMAT_xRGB:
854 case GST_VIDEO_FORMAT_xBGR:
855 case GST_VIDEO_FORMAT_RGBA:
856 case GST_VIDEO_FORMAT_BGRA:
857 case GST_VIDEO_FORMAT_ARGB:
858 case GST_VIDEO_FORMAT_ABGR:
860 case GST_VIDEO_FORMAT_RGB:
861 case GST_VIDEO_FORMAT_BGR:
869 * gst_video_format_get_component_width:
870 * @format: a #GstVideoFormat
871 * @component: the component index
872 * @width: the width of video
874 * Calculates the width of the component. See
875 * @gst_video_format_get_row_stride for a description
876 * of the component index.
880 * Returns: width of component @component
883 gst_video_format_get_component_width (GstVideoFormat format, int component,
886 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
887 g_return_val_if_fail (component >= 0 && component <= 3, 0);
888 g_return_val_if_fail (width > 0, 0);
891 case GST_VIDEO_FORMAT_I420:
892 case GST_VIDEO_FORMAT_YV12:
893 case GST_VIDEO_FORMAT_YUY2:
894 case GST_VIDEO_FORMAT_UYVY:
895 if (component == 0) {
898 return GST_ROUND_UP_2 (width) / 2;
900 case GST_VIDEO_FORMAT_Y41B: /* CHECKME: component_width for Y41B */
901 if (component == 0) {
904 return GST_ROUND_UP_8 (width) / 4;
906 case GST_VIDEO_FORMAT_Y42B: /* CHECKME: component_width for Y42B */
907 if (component == 0) {
910 return GST_ROUND_UP_8 (width) / 2;
912 case GST_VIDEO_FORMAT_AYUV:
913 case GST_VIDEO_FORMAT_RGBx:
914 case GST_VIDEO_FORMAT_BGRx:
915 case GST_VIDEO_FORMAT_xRGB:
916 case GST_VIDEO_FORMAT_xBGR:
917 case GST_VIDEO_FORMAT_RGBA:
918 case GST_VIDEO_FORMAT_BGRA:
919 case GST_VIDEO_FORMAT_ARGB:
920 case GST_VIDEO_FORMAT_ABGR:
921 case GST_VIDEO_FORMAT_RGB:
922 case GST_VIDEO_FORMAT_BGR:
930 * gst_video_format_get_component_height:
931 * @format: a #GstVideoFormat
932 * @component: the component index
933 * @height: the height of video
935 * Calculates the height of the component. See
936 * @gst_video_format_get_row_stride for a description
937 * of the component index.
941 * Returns: height of component @component
944 gst_video_format_get_component_height (GstVideoFormat format, int component,
947 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
948 g_return_val_if_fail (component >= 0 && component <= 3, 0);
949 g_return_val_if_fail (height > 0, 0);
952 case GST_VIDEO_FORMAT_I420:
953 case GST_VIDEO_FORMAT_YV12:
954 if (component == 0) {
957 return GST_ROUND_UP_2 (height) / 2;
959 case GST_VIDEO_FORMAT_Y41B:
960 case GST_VIDEO_FORMAT_Y42B:
961 case GST_VIDEO_FORMAT_YUY2:
962 case GST_VIDEO_FORMAT_UYVY:
963 case GST_VIDEO_FORMAT_AYUV:
964 case GST_VIDEO_FORMAT_RGBx:
965 case GST_VIDEO_FORMAT_BGRx:
966 case GST_VIDEO_FORMAT_xRGB:
967 case GST_VIDEO_FORMAT_xBGR:
968 case GST_VIDEO_FORMAT_RGBA:
969 case GST_VIDEO_FORMAT_BGRA:
970 case GST_VIDEO_FORMAT_ARGB:
971 case GST_VIDEO_FORMAT_ABGR:
972 case GST_VIDEO_FORMAT_RGB:
973 case GST_VIDEO_FORMAT_BGR:
981 * gst_video_format_get_component_offset:
982 * @format: a #GstVideoFormat
983 * @component: the component index
984 * @width: the width of video
985 * @height: the height of video
987 * Calculates the offset (in bytes) of the first pixel of the component
988 * with index @component. For packed formats, this will typically be a
989 * small integer (0, 1, 2, 3). For planar formats, this will be a
990 * (relatively) large offset to the beginning of the second or third
991 * component planes. See @gst_video_format_get_row_stride for a description
992 * of the component index.
996 * Returns: offset of component @component
999 gst_video_format_get_component_offset (GstVideoFormat format, int component,
1000 int width, int height)
1002 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1003 g_return_val_if_fail (component >= 0 && component <= 3, 0);
1004 g_return_val_if_fail (width > 0 && height > 0, 0);
1007 case GST_VIDEO_FORMAT_I420:
1011 return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1012 if (component == 2) {
1013 return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1014 GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1015 (GST_ROUND_UP_2 (height) / 2);
1018 case GST_VIDEO_FORMAT_YV12: /* same as I420, but components 1+2 swapped */
1022 return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1023 if (component == 1) {
1024 return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1025 GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1026 (GST_ROUND_UP_2 (height) / 2);
1029 case GST_VIDEO_FORMAT_YUY2:
1037 case GST_VIDEO_FORMAT_UYVY:
1045 case GST_VIDEO_FORMAT_AYUV:
1055 case GST_VIDEO_FORMAT_RGBx:
1056 case GST_VIDEO_FORMAT_RGBA:
1066 case GST_VIDEO_FORMAT_BGRx:
1067 case GST_VIDEO_FORMAT_BGRA:
1077 case GST_VIDEO_FORMAT_xRGB:
1078 case GST_VIDEO_FORMAT_ARGB:
1088 case GST_VIDEO_FORMAT_xBGR:
1089 case GST_VIDEO_FORMAT_ABGR:
1099 case GST_VIDEO_FORMAT_RGB:
1107 case GST_VIDEO_FORMAT_BGR:
1115 case GST_VIDEO_FORMAT_Y41B:
1119 return GST_ROUND_UP_4 (width) * height;
1121 return (GST_ROUND_UP_4 (width) + (GST_ROUND_UP_8 (width) / 4)) * height;
1123 case GST_VIDEO_FORMAT_Y42B:
1127 return GST_ROUND_UP_4 (width) * height;
1129 return (GST_ROUND_UP_4 (width) + (GST_ROUND_UP_8 (width) / 2)) * height;
1137 * gst_video_format_get_size:
1138 * @format: a #GstVideoFormat
1139 * @width: the width of video
1140 * @height: the height of video
1142 * Calculates the total number of bytes in the raw video format. This
1143 * number should be used when allocating a buffer for raw video.
1147 * Returns: size (in bytes) of raw video format
1150 gst_video_format_get_size (GstVideoFormat format, int width, int height)
1154 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1155 g_return_val_if_fail (width > 0 && height > 0, 0);
1158 case GST_VIDEO_FORMAT_I420:
1159 case GST_VIDEO_FORMAT_YV12:
1160 size = GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1161 size += GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1162 (GST_ROUND_UP_2 (height) / 2) * 2;
1164 case GST_VIDEO_FORMAT_YUY2:
1165 case GST_VIDEO_FORMAT_UYVY:
1166 return GST_ROUND_UP_4 (width * 2) * height;
1167 case GST_VIDEO_FORMAT_AYUV:
1168 case GST_VIDEO_FORMAT_RGBx:
1169 case GST_VIDEO_FORMAT_BGRx:
1170 case GST_VIDEO_FORMAT_xRGB:
1171 case GST_VIDEO_FORMAT_xBGR:
1172 case GST_VIDEO_FORMAT_RGBA:
1173 case GST_VIDEO_FORMAT_BGRA:
1174 case GST_VIDEO_FORMAT_ARGB:
1175 case GST_VIDEO_FORMAT_ABGR:
1176 return width * 4 * height;
1177 case GST_VIDEO_FORMAT_RGB:
1178 case GST_VIDEO_FORMAT_BGR:
1179 return GST_ROUND_UP_4 (width * 3) * height;
1180 case GST_VIDEO_FORMAT_Y41B:
1181 /* simplification of ROUNDUP4(w)*h + 2*((ROUNDUP8(w)/4)*h */
1182 return (GST_ROUND_UP_4 (width) + (GST_ROUND_UP_8 (width) / 2)) * height;
1183 case GST_VIDEO_FORMAT_Y42B:
1184 /* simplification of ROUNDUP4(w)*h + 2*(ROUNDUP8(w)/2)*h: */
1185 return (GST_ROUND_UP_4 (width) + GST_ROUND_UP_8 (width)) * height;
1192 * gst_video_format_convert:
1193 * @format: a #GstVideoFormat
1194 * @width: the width of video
1195 * @height: the height of video
1196 * @fps_n: frame rate numerator
1197 * @fps_d: frame rate denominator
1198 * @src_format: #GstFormat of the @src_value
1199 * @src_value: value to convert
1200 * @dest_format: #GstFormat of the @dest_value
1201 * @dest_value: pointer to destination value
1203 * Converts among various #GstFormat types. This function handles
1204 * GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT. For
1205 * raw video, GST_FORMAT_DEFAULT corresponds to video frames. This
1206 * function can be to handle pad queries of the type GST_QUERY_CONVERT.
1210 * Returns: TRUE if the conversion was successful.
1213 gst_video_format_convert (GstVideoFormat format, int width, int height,
1214 int fps_n, int fps_d,
1215 GstFormat src_format, gint64 src_value,
1216 GstFormat dest_format, gint64 * dest_value)
1218 gboolean ret = FALSE;
1221 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1222 g_return_val_if_fail (width > 0 && height > 0, 0);
1224 size = gst_video_format_get_size (format, width, height);
1226 GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s to %s",
1227 src_value, gst_format_get_name (src_format),
1228 gst_format_get_name (dest_format));
1230 if (src_format == dest_format) {
1231 *dest_value = src_value;
1236 if (src_value == -1) {
1242 /* bytes to frames */
1243 if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_DEFAULT) {
1245 *dest_value = gst_util_uint64_scale_int (src_value, 1, size);
1247 GST_ERROR ("blocksize is 0");
1254 /* frames to bytes */
1255 if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_BYTES) {
1256 *dest_value = gst_util_uint64_scale_int (src_value, size, 1);
1261 /* time to frames */
1262 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_DEFAULT) {
1264 *dest_value = gst_util_uint64_scale (src_value,
1265 fps_n, GST_SECOND * fps_d);
1267 GST_ERROR ("framerate denominator is 0");
1274 /* frames to time */
1275 if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) {
1277 *dest_value = gst_util_uint64_scale (src_value,
1278 GST_SECOND * fps_d, fps_n);
1280 GST_ERROR ("framerate numerator is 0");
1288 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
1290 *dest_value = gst_util_uint64_scale (src_value,
1291 fps_n * size, GST_SECOND * fps_d);
1293 GST_ERROR ("framerate denominator is 0");
1301 if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
1302 if (fps_n != 0 && size != 0) {
1303 *dest_value = gst_util_uint64_scale (src_value,
1304 GST_SECOND * fps_d, fps_n * size);
1306 GST_ERROR ("framerate denominator and/or blocksize is 0");
1314 GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, ret, *dest_value);