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.
32 VIDEO_FLAG_YUV = (1 << 0),
33 VIDEO_FLAG_RGB = (1 << 1),
34 VIDEO_FLAG_GRAY = (1 << 2),
35 VIDEO_FLAG_ALPHA = (1 << 3)
41 GstVideoFormat format;
44 guint depth[GST_VIDEO_MAX_PLANES];
47 #define COMP0 { 0, 0, 0, 0 }
48 #define COMP8 { 8, 0, 0, 0 }
49 #define COMP888 { 8, 8, 8, 0 }
50 #define COMP8888 { 8, 8, 8, 8 }
51 #define COMP10_10_10 { 10, 10, 10, 0 }
52 #define COMP16 { 16, 0, 0, 0 }
53 #define COMP16_16_16 { 16, 0, 0, 0 }
54 #define COMP16_16_16_16 { 16, 0, 0, 0 }
55 #define COMP555 { 5, 5, 5, 0 }
56 #define COMP565 { 5, 6, 5, 0 }
58 #define MAKE_YUV_FORMAT(name, fourcc, comp) \
59 { G_STRINGIFY(name), GST_VIDEO_FORMAT_ ##name, fourcc, VIDEO_FLAG_YUV, comp }
60 #define MAKE_YUVA_FORMAT(name, fourcc, comp) \
61 { G_STRINGIFY(name), GST_VIDEO_FORMAT_ ##name, fourcc, VIDEO_FLAG_YUV | VIDEO_FLAG_ALPHA, comp }
63 #define MAKE_RGB_FORMAT(name, comp) \
64 { G_STRINGIFY(name), GST_VIDEO_FORMAT_ ##name, 0x00000000, VIDEO_FLAG_RGB, comp }
65 #define MAKE_RGBA_FORMAT(name, comp) \
66 { G_STRINGIFY(name), GST_VIDEO_FORMAT_ ##name, 0x00000000, VIDEO_FLAG_RGB | VIDEO_FLAG_ALPHA, comp }
68 #define MAKE_GRAY_FORMAT(name, comp) \
69 { G_STRINGIFY(name), GST_VIDEO_FORMAT_ ##name, 0x00000000, VIDEO_FLAG_GRAY, comp }
71 static VideoFormat formats[] = {
72 {"UNKNOWN", GST_VIDEO_FORMAT_UNKNOWN, 0x00000000, 0, COMP0},
74 MAKE_YUV_FORMAT (I420, GST_MAKE_FOURCC ('I', '4', '2', '0'), COMP888),
75 MAKE_YUV_FORMAT (YV12, GST_MAKE_FOURCC ('Y', 'V', '1', '2'), COMP888),
76 MAKE_YUV_FORMAT (YUY2, GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'), COMP888),
77 MAKE_YUV_FORMAT (UYVY, GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'), COMP888),
78 MAKE_YUVA_FORMAT (AYUV, GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'), COMP8888),
79 MAKE_RGB_FORMAT (RGBx, COMP888),
80 MAKE_RGB_FORMAT (BGRx, COMP888),
81 MAKE_RGB_FORMAT (xRGB, COMP888),
82 MAKE_RGB_FORMAT (xBGR, COMP888),
83 MAKE_RGBA_FORMAT (RGBA, COMP8888),
84 MAKE_RGBA_FORMAT (BGRA, COMP8888),
85 MAKE_RGBA_FORMAT (ARGB, COMP8888),
86 MAKE_RGBA_FORMAT (ABGR, COMP8888),
87 MAKE_RGB_FORMAT (RGB, COMP888),
88 MAKE_RGB_FORMAT (BGR, COMP888),
90 MAKE_YUV_FORMAT (Y41B, GST_MAKE_FOURCC ('Y', '4', '1', 'B'), COMP888),
91 MAKE_YUV_FORMAT (Y42B, GST_MAKE_FOURCC ('Y', '4', '2', 'B'), COMP888),
92 MAKE_YUV_FORMAT (YVYU, GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'), COMP888),
93 MAKE_YUV_FORMAT (Y444, GST_MAKE_FOURCC ('Y', '4', '4', '4'), COMP888),
94 MAKE_YUV_FORMAT (v210, GST_MAKE_FOURCC ('v', '2', '1', '0'), COMP10_10_10),
95 MAKE_YUV_FORMAT (v216, GST_MAKE_FOURCC ('v', '2', '1', '6'), COMP16_16_16),
96 MAKE_YUV_FORMAT (NV12, GST_MAKE_FOURCC ('N', 'V', '1', '2'), COMP888),
97 MAKE_YUV_FORMAT (NV21, GST_MAKE_FOURCC ('N', 'V', '2', '1'), COMP888),
99 MAKE_GRAY_FORMAT (GRAY8, COMP8),
100 MAKE_GRAY_FORMAT (GRAY16_BE, COMP16),
101 MAKE_GRAY_FORMAT (GRAY16_LE, COMP16),
103 MAKE_YUV_FORMAT (v308, GST_MAKE_FOURCC ('v', '3', '0', '8'), COMP888),
104 MAKE_YUV_FORMAT (Y800, GST_MAKE_FOURCC ('Y', '8', '0', '0'), COMP8),
105 MAKE_YUV_FORMAT (Y16, GST_MAKE_FOURCC ('Y', '1', '6', ' '), COMP16),
107 MAKE_RGB_FORMAT (RGB16, COMP565),
108 MAKE_RGB_FORMAT (BGR16, COMP565),
109 MAKE_RGB_FORMAT (RGB15, COMP555),
110 MAKE_RGB_FORMAT (BGR15, COMP555),
112 MAKE_YUV_FORMAT (UYVP, GST_MAKE_FOURCC ('U', 'Y', 'V', 'P'), COMP10_10_10),
113 MAKE_YUVA_FORMAT (A420, GST_MAKE_FOURCC ('A', '4', '2', '0'), COMP888),
114 MAKE_RGBA_FORMAT (RGB8_PALETTED, COMP8888),
115 MAKE_YUV_FORMAT (YUV9, GST_MAKE_FOURCC ('Y', 'U', 'V', '9'), COMP888),
116 MAKE_YUV_FORMAT (YVU9, GST_MAKE_FOURCC ('Y', 'V', 'U', '9'), COMP888),
117 MAKE_YUV_FORMAT (IYU1, GST_MAKE_FOURCC ('I', 'Y', 'U', '1'), COMP888),
118 MAKE_RGBA_FORMAT (ARGB64, COMP16_16_16_16),
119 MAKE_YUVA_FORMAT (AYUV64, 0x00000000, COMP16_16_16_16),
120 MAKE_YUV_FORMAT (r210, GST_MAKE_FOURCC ('r', '2', '1', '0'), COMP10_10_10),
125 * @short_description: Support library for video operations
129 * This library contains some helper functions and includes the
130 * videosink and videofilter base classes.
136 * gst_video_frame_rate:
137 * @pad: pointer to a #GstPad
139 * A convenience function to retrieve a GValue holding the framerate
140 * from the caps on a pad.
142 * The pad needs to have negotiated caps containing a framerate property.
144 * Returns: NULL if the pad has no configured caps or the configured caps
145 * do not contain a framerate.
149 gst_video_frame_rate (GstPad * pad)
153 GstCaps *caps = NULL;
154 GstStructure *structure;
157 caps = gst_pad_get_current_caps (pad);
161 structure = gst_caps_get_structure (caps, 0);
162 if ((fps = gst_structure_get_value (structure, "framerate")) == NULL)
165 if (!GST_VALUE_HOLDS_FRACTION (fps))
168 fps_string = gst_value_serialize (fps);
169 GST_DEBUG ("Framerate request on pad %s:%s: %s",
170 GST_DEBUG_PAD_NAME (pad), fps_string);
173 gst_caps_unref (caps);
180 g_warning ("gstvideo: failed to get caps of pad %s:%s",
181 GST_DEBUG_PAD_NAME (pad));
186 g_warning ("gstvideo: failed to get framerate property of pad %s:%s",
187 GST_DEBUG_PAD_NAME (pad));
188 gst_caps_unref (caps);
194 ("gstvideo: framerate property of pad %s:%s is not of type Fraction",
195 GST_DEBUG_PAD_NAME (pad));
196 gst_caps_unref (caps);
202 * gst_video_calculate_display_ratio:
203 * @dar_n: Numerator of the calculated display_ratio
204 * @dar_d: Denominator of the calculated display_ratio
205 * @video_width: Width of the video frame in pixels
206 * @video_height: Height of the video frame in pixels
207 * @video_par_n: Numerator of the pixel aspect ratio of the input video.
208 * @video_par_d: Denominator of the pixel aspect ratio of the input video.
209 * @display_par_n: Numerator of the pixel aspect ratio of the display device
210 * @display_par_d: Denominator of the pixel aspect ratio of the display device
212 * Given the Pixel Aspect Ratio and size of an input video frame, and the
213 * pixel aspect ratio of the intended display device, calculates the actual
214 * display ratio the video will be rendered with.
216 * Returns: A boolean indicating success and a calculated Display Ratio in the
217 * dar_n and dar_d parameters.
218 * The return value is FALSE in the case of integer overflow or other error.
223 gst_video_calculate_display_ratio (guint * dar_n, guint * dar_d,
224 guint video_width, guint video_height,
225 guint video_par_n, guint video_par_d,
226 guint display_par_n, guint display_par_d)
231 g_return_val_if_fail (dar_n != NULL, FALSE);
232 g_return_val_if_fail (dar_d != NULL, FALSE);
234 /* Calculate (video_width * video_par_n * display_par_d) /
235 * (video_height * video_par_d * display_par_n) */
236 if (!gst_util_fraction_multiply (video_width, video_height, video_par_n,
237 video_par_d, &tmp_n, &tmp_d))
240 if (!gst_util_fraction_multiply (tmp_n, tmp_d, display_par_d, display_par_n,
244 g_return_val_if_fail (num > 0, FALSE);
245 g_return_val_if_fail (den > 0, FALSE);
255 GST_WARNING ("overflow in multiply");
261 * gst_video_format_parse_caps_interlaced:
262 * @caps: the fixed #GstCaps to parse
263 * @interlaced: whether @caps represents interlaced video or not, may be NULL (output)
265 * Extracts whether the caps represents interlaced content or not and places it
270 * Returns: TRUE if @caps was parsed correctly.
273 gst_video_format_parse_caps_interlaced (GstCaps * caps, gboolean * interlaced)
275 GstStructure *structure;
277 if (!gst_caps_is_fixed (caps))
280 structure = gst_caps_get_structure (caps, 0);
283 if (!gst_structure_get_boolean (structure, "interlaced", interlaced))
291 * gst_video_parse_caps_color_matrix:
292 * @caps: the fixed #GstCaps to parse
294 * Extracts the color matrix used by the caps. Possible values are
295 * "sdtv" for the standard definition color matrix (as specified in
296 * Rec. ITU-R BT.470-6) or "hdtv" for the high definition color
297 * matrix (as specified in Rec. ITU-R BT.709)
301 * Returns: a color matrix string, or NULL if no color matrix could be
305 gst_video_parse_caps_color_matrix (GstCaps * caps)
307 GstStructure *structure;
310 if (!gst_caps_is_fixed (caps))
313 structure = gst_caps_get_structure (caps, 0);
315 s = gst_structure_get_string (structure, "color-matrix");
319 if (gst_structure_has_name (structure, "video/x-raw-yuv")) {
327 * gst_video_parse_caps_chroma_site:
328 * @caps: the fixed #GstCaps to parse
330 * Extracts the chroma site used by the caps. Possible values are
331 * "mpeg2" for MPEG-2 style chroma siting (co-sited horizontally,
332 * halfway-sited vertically), "jpeg" for JPEG and Theora style
333 * chroma siting (halfway-sited both horizontally and vertically).
334 * Other chroma site values are possible, but uncommon.
336 * When no chroma site is specified in the caps, it should be assumed
341 * Returns: a chroma site string, or NULL if no chroma site could be
345 gst_video_parse_caps_chroma_site (GstCaps * caps)
347 GstStructure *structure;
350 if (!gst_caps_is_fixed (caps))
353 structure = gst_caps_get_structure (caps, 0);
355 s = gst_structure_get_string (structure, "chroma-site");
359 if (gst_structure_has_name (structure, "video/x-raw-yuv")) {
367 * gst_video_parse_caps_framerate:
368 * @caps: pointer to a #GstCaps instance
369 * @fps_n: pointer to integer to hold numerator of frame rate (output)
370 * @fps_d: pointer to integer to hold denominator of frame rate (output)
372 * Extracts the frame rate from @caps and places the values in the locations
373 * pointed to by @fps_n and @fps_d. Returns TRUE if the values could be
374 * parsed correctly, FALSE if not.
376 * This function can be used with #GstCaps that have any media type; it
377 * is not limited to formats handled by #GstVideoFormat.
381 * Returns: TRUE if @caps was parsed correctly.
384 gst_video_parse_caps_framerate (GstCaps * caps, int *fps_n, int *fps_d)
386 GstStructure *structure;
388 if (!gst_caps_is_fixed (caps))
391 structure = gst_caps_get_structure (caps, 0);
393 return gst_structure_get_fraction (structure, "framerate", fps_n, fps_d);
397 * gst_video_parse_caps_pixel_aspect_ratio:
398 * @caps: pointer to a #GstCaps instance
399 * @par_n: pointer to numerator of pixel aspect ratio (output)
400 * @par_d: pointer to denominator of pixel aspect ratio (output)
402 * Extracts the pixel aspect ratio from @caps and places the values in
403 * the locations pointed to by @par_n and @par_d. Returns TRUE if the
404 * values could be parsed correctly, FALSE if not.
406 * This function can be used with #GstCaps that have any media type; it
407 * is not limited to formats handled by #GstVideoFormat.
411 * Returns: TRUE if @caps was parsed correctly.
414 gst_video_parse_caps_pixel_aspect_ratio (GstCaps * caps, int *par_n, int *par_d)
416 GstStructure *structure;
418 if (!gst_caps_is_fixed (caps))
421 structure = gst_caps_get_structure (caps, 0);
423 if (!gst_structure_get_fraction (structure, "pixel-aspect-ratio",
432 * gst_video_format_new_caps_interlaced:
433 * @format: the #GstVideoFormat describing the raw video format
434 * @width: width of video
435 * @height: height of video
436 * @framerate_n: numerator of frame rate
437 * @framerate_d: denominator of frame rate
438 * @par_n: numerator of pixel aspect ratio
439 * @par_d: denominator of pixel aspect ratio
440 * @interlaced: #TRUE if the format is interlaced
442 * Creates a new #GstCaps object based on the parameters provided.
446 * Returns: a new #GstCaps object, or NULL if there was an error
449 gst_video_format_new_caps_interlaced (GstVideoFormat format,
450 int width, int height, int framerate_n, int framerate_d, int par_n,
451 int par_d, gboolean interlaced)
456 gst_video_format_new_caps (format, width, height, framerate_n,
457 framerate_d, par_n, par_d);
458 if (interlaced && (res != NULL))
459 gst_caps_set_simple (res, "interlaced", G_TYPE_BOOLEAN, TRUE, NULL);
465 gst_video_format_new_caps_raw (GstVideoFormat format)
467 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
469 return gst_caps_new_simple ("video/x-raw",
470 "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
474 * gst_video_format_new_template_caps:
475 * @format: the #GstVideoFormat describing the raw video format
477 * Creates a new #GstCaps object based on the parameters provided.
478 * Size, frame rate, and pixel aspect ratio are set to the full
483 * Returns: a new #GstCaps object, or NULL if there was an error
486 gst_video_format_new_template_caps (GstVideoFormat format)
489 GstStructure *structure;
491 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
493 caps = gst_video_format_new_caps_raw (format);
495 GValue value = { 0 };
498 structure = gst_caps_get_structure (caps, 0);
500 gst_structure_set (structure,
501 "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
502 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
503 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1,
504 "pixel-aspect-ratio", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
506 g_value_init (&value, GST_TYPE_LIST);
507 g_value_init (&v, G_TYPE_BOOLEAN);
508 g_value_set_boolean (&v, TRUE);
509 gst_value_list_append_value (&value, &v);
510 g_value_set_boolean (&v, FALSE);
511 gst_value_list_append_value (&value, &v);
513 gst_structure_set_value (structure, "interlaced", &value);
515 g_value_reset (&value);
523 * gst_video_format_new_caps:
524 * @format: the #GstVideoFormat describing the raw video format
525 * @width: width of video
526 * @height: height of video
527 * @framerate_n: numerator of frame rate
528 * @framerate_d: denominator of frame rate
529 * @par_n: numerator of pixel aspect ratio
530 * @par_d: denominator of pixel aspect ratio
532 * Creates a new #GstCaps object based on the parameters provided.
536 * Returns: a new #GstCaps object, or NULL if there was an error
539 gst_video_format_new_caps (GstVideoFormat format, int width,
540 int height, int framerate_n, int framerate_d, int par_n, int par_d)
543 GstStructure *structure;
545 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
546 g_return_val_if_fail (width > 0 && height > 0, NULL);
548 caps = gst_video_format_new_caps_raw (format);
550 structure = gst_caps_get_structure (caps, 0);
552 gst_structure_set (structure,
553 "width", G_TYPE_INT, width,
554 "height", G_TYPE_INT, height,
555 "framerate", GST_TYPE_FRACTION, framerate_n, framerate_d,
556 "pixel-aspect-ratio", GST_TYPE_FRACTION, par_n, par_d, NULL);
563 static GstVideoFormat
564 gst_video_format_from_rgb32_masks (int red_mask, int green_mask, int blue_mask)
566 if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
567 blue_mask == 0x0000ff00) {
568 return GST_VIDEO_FORMAT_RGBx;
570 if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
571 blue_mask == 0xff000000) {
572 return GST_VIDEO_FORMAT_BGRx;
574 if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
575 blue_mask == 0x000000ff) {
576 return GST_VIDEO_FORMAT_xRGB;
578 if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
579 blue_mask == 0x00ff0000) {
580 return GST_VIDEO_FORMAT_xBGR;
583 return GST_VIDEO_FORMAT_UNKNOWN;
586 static GstVideoFormat
587 gst_video_format_from_rgba32_masks (int red_mask, int green_mask,
588 int blue_mask, int alpha_mask)
590 if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
591 blue_mask == 0x0000ff00 && alpha_mask == 0x000000ff) {
592 return GST_VIDEO_FORMAT_RGBA;
594 if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
595 blue_mask == 0xff000000 && alpha_mask == 0x000000ff) {
596 return GST_VIDEO_FORMAT_BGRA;
598 if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
599 blue_mask == 0x000000ff && alpha_mask == 0xff000000) {
600 return GST_VIDEO_FORMAT_ARGB;
602 if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
603 blue_mask == 0x00ff0000 && alpha_mask == 0xff000000) {
604 return GST_VIDEO_FORMAT_ABGR;
606 return GST_VIDEO_FORMAT_UNKNOWN;
609 static GstVideoFormat
610 gst_video_format_from_rgb24_masks (int red_mask, int green_mask, int blue_mask)
612 if (red_mask == 0xff0000 && green_mask == 0x00ff00 && blue_mask == 0x0000ff) {
613 return GST_VIDEO_FORMAT_RGB;
615 if (red_mask == 0x0000ff && green_mask == 0x00ff00 && blue_mask == 0xff0000) {
616 return GST_VIDEO_FORMAT_BGR;
619 return GST_VIDEO_FORMAT_UNKNOWN;
622 #define GST_VIDEO_COMP1_MASK_16_INT 0xf800
623 #define GST_VIDEO_COMP2_MASK_16_INT 0x07e0
624 #define GST_VIDEO_COMP3_MASK_16_INT 0x001f
626 #define GST_VIDEO_COMP1_MASK_15_INT 0x7c00
627 #define GST_VIDEO_COMP2_MASK_15_INT 0x03e0
628 #define GST_VIDEO_COMP3_MASK_15_INT 0x001f
630 static GstVideoFormat
631 gst_video_format_from_rgb16_masks (int red_mask, int green_mask, int blue_mask)
633 if (red_mask == GST_VIDEO_COMP1_MASK_16_INT
634 && green_mask == GST_VIDEO_COMP2_MASK_16_INT
635 && blue_mask == GST_VIDEO_COMP3_MASK_16_INT) {
636 return GST_VIDEO_FORMAT_RGB16;
638 if (red_mask == GST_VIDEO_COMP3_MASK_16_INT
639 && green_mask == GST_VIDEO_COMP2_MASK_16_INT
640 && blue_mask == GST_VIDEO_COMP1_MASK_16_INT) {
641 return GST_VIDEO_FORMAT_BGR16;
643 if (red_mask == GST_VIDEO_COMP1_MASK_15_INT
644 && green_mask == GST_VIDEO_COMP2_MASK_15_INT
645 && blue_mask == GST_VIDEO_COMP3_MASK_15_INT) {
646 return GST_VIDEO_FORMAT_RGB15;
648 if (red_mask == GST_VIDEO_COMP3_MASK_15_INT
649 && green_mask == GST_VIDEO_COMP2_MASK_15_INT
650 && blue_mask == GST_VIDEO_COMP1_MASK_15_INT) {
651 return GST_VIDEO_FORMAT_BGR15;
653 return GST_VIDEO_FORMAT_UNKNOWN;
657 gst_video_format_from_masks (gint depth, gint bpp, gint endianness,
658 gint red_mask, gint green_mask, gint blue_mask, gint alpha_mask)
660 GstVideoFormat format;
662 /* our caps system handles 24/32bpp RGB as big-endian. */
663 if ((bpp == 24 || bpp == 32) && endianness == G_LITTLE_ENDIAN) {
664 red_mask = GUINT32_TO_BE (red_mask);
665 green_mask = GUINT32_TO_BE (green_mask);
666 blue_mask = GUINT32_TO_BE (blue_mask);
667 endianness = G_BIG_ENDIAN;
675 if (depth == 30 && bpp == 32) {
676 format = GST_VIDEO_FORMAT_r210;
677 } else if (depth == 24 && bpp == 32) {
678 format = gst_video_format_from_rgb32_masks (red_mask, green_mask,
680 } else if (depth == 32 && bpp == 32 && alpha_mask) {
681 format = gst_video_format_from_rgba32_masks (red_mask, green_mask,
682 blue_mask, alpha_mask);
683 } else if (depth == 24 && bpp == 24) {
684 format = gst_video_format_from_rgb24_masks (red_mask, green_mask,
686 } else if ((depth == 15 || depth == 16) && bpp == 16 &&
687 endianness == G_BYTE_ORDER) {
688 format = gst_video_format_from_rgb16_masks (red_mask, green_mask,
690 } else if (depth == 8 && bpp == 8) {
691 format = GST_VIDEO_FORMAT_RGB8_PALETTED;
692 } else if (depth == 64 && bpp == 64) {
693 format = gst_video_format_from_rgba32_masks (red_mask, green_mask,
694 blue_mask, alpha_mask);
695 if (format == GST_VIDEO_FORMAT_ARGB) {
696 format = GST_VIDEO_FORMAT_ARGB64;
698 format = GST_VIDEO_FORMAT_UNKNOWN;
701 format = GST_VIDEO_FORMAT_UNKNOWN;
707 * gst_video_format_from_fourcc:
708 * @fourcc: a FOURCC value representing raw YUV video
710 * Converts a FOURCC value into the corresponding #GstVideoFormat.
711 * If the FOURCC cannot be represented by #GstVideoFormat,
712 * #GST_VIDEO_FORMAT_UNKNOWN is returned.
716 * Returns: the #GstVideoFormat describing the FOURCC value
719 gst_video_format_from_fourcc (guint32 fourcc)
722 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
723 return GST_VIDEO_FORMAT_I420;
724 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
725 return GST_VIDEO_FORMAT_YV12;
726 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
727 return GST_VIDEO_FORMAT_YUY2;
728 case GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'):
729 return GST_VIDEO_FORMAT_YVYU;
730 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
731 return GST_VIDEO_FORMAT_UYVY;
732 case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
733 return GST_VIDEO_FORMAT_AYUV;
734 case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
735 return GST_VIDEO_FORMAT_Y41B;
736 case GST_MAKE_FOURCC ('Y', '4', '2', 'B'):
737 return GST_VIDEO_FORMAT_Y42B;
738 case GST_MAKE_FOURCC ('Y', '4', '4', '4'):
739 return GST_VIDEO_FORMAT_Y444;
740 case GST_MAKE_FOURCC ('v', '2', '1', '0'):
741 return GST_VIDEO_FORMAT_v210;
742 case GST_MAKE_FOURCC ('v', '2', '1', '6'):
743 return GST_VIDEO_FORMAT_v216;
744 case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
745 return GST_VIDEO_FORMAT_NV12;
746 case GST_MAKE_FOURCC ('N', 'V', '2', '1'):
747 return GST_VIDEO_FORMAT_NV21;
748 case GST_MAKE_FOURCC ('v', '3', '0', '8'):
749 return GST_VIDEO_FORMAT_v308;
750 case GST_MAKE_FOURCC ('Y', '8', '0', '0'):
751 case GST_MAKE_FOURCC ('Y', '8', ' ', ' '):
752 case GST_MAKE_FOURCC ('G', 'R', 'E', 'Y'):
753 return GST_VIDEO_FORMAT_Y800;
754 case GST_MAKE_FOURCC ('Y', '1', '6', ' '):
755 return GST_VIDEO_FORMAT_Y16;
756 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'P'):
757 return GST_VIDEO_FORMAT_UYVP;
758 case GST_MAKE_FOURCC ('A', '4', '2', '0'):
759 return GST_VIDEO_FORMAT_A420;
760 case GST_MAKE_FOURCC ('Y', 'U', 'V', '9'):
761 return GST_VIDEO_FORMAT_YUV9;
762 case GST_MAKE_FOURCC ('Y', 'V', 'U', '9'):
763 return GST_VIDEO_FORMAT_YVU9;
764 case GST_MAKE_FOURCC ('I', 'Y', 'U', '1'):
765 return GST_VIDEO_FORMAT_IYU1;
766 case GST_MAKE_FOURCC ('A', 'Y', '6', '4'):
767 return GST_VIDEO_FORMAT_AYUV64;
769 return GST_VIDEO_FORMAT_UNKNOWN;
774 gst_video_format_from_string (const gchar * format)
778 for (i = 0; i < G_N_ELEMENTS (formats); i++) {
779 if (strcmp (formats[i].fmt, format) == 0)
780 return formats[i].format;
782 return GST_VIDEO_FORMAT_UNKNOWN;
787 * gst_video_format_to_fourcc:
788 * @format: a #GstVideoFormat video format
790 * Converts a #GstVideoFormat value into the corresponding FOURCC. Only
791 * a few YUV formats have corresponding FOURCC values. If @format has
792 * no corresponding FOURCC value, 0 is returned.
796 * Returns: the FOURCC corresponding to @format
799 gst_video_format_to_fourcc (GstVideoFormat format)
801 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
803 if (format >= G_N_ELEMENTS (formats))
806 return formats[format].fourcc;
810 gst_video_format_to_string (GstVideoFormat format)
812 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
814 if (format >= G_N_ELEMENTS (formats))
817 return formats[format].fmt;
821 * gst_video_format_is_rgb:
822 * @format: a #GstVideoFormat
824 * Determine whether the video format is an RGB format.
828 * Returns: TRUE if @format represents RGB video
831 gst_video_format_is_rgb (GstVideoFormat format)
833 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, FALSE);
835 if (format >= G_N_ELEMENTS (formats))
838 return (formats[format].flags & VIDEO_FLAG_RGB) != 0;
842 * gst_video_format_is_yuv:
843 * @format: a #GstVideoFormat
845 * Determine whether the video format is a YUV format.
849 * Returns: TRUE if @format represents YUV video
852 gst_video_format_is_yuv (GstVideoFormat format)
854 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, FALSE);
856 if (format >= G_N_ELEMENTS (formats))
859 return (formats[format].flags & VIDEO_FLAG_YUV) != 0;
863 * gst_video_format_is_gray:
864 * @format: a #GstVideoFormat
866 * Determine whether the video format is a grayscale format.
870 * Returns: TRUE if @format represents grayscale video
873 gst_video_format_is_gray (GstVideoFormat format)
875 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, FALSE);
877 if (format >= G_N_ELEMENTS (formats))
880 return (formats[format].flags & VIDEO_FLAG_GRAY) != 0;
884 * gst_video_format_has_alpha:
885 * @format: a #GstVideoFormat
887 * Returns TRUE or FALSE depending on if the video format provides an
892 * Returns: TRUE if @format has an alpha channel
895 gst_video_format_has_alpha (GstVideoFormat format)
897 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, FALSE);
899 if (format >= G_N_ELEMENTS (formats))
902 return (formats[format].flags & VIDEO_FLAG_ALPHA) != 0;
906 * gst_video_format_get_component_depth:
907 * @format: a #GstVideoFormat
908 * @component: the video component (e.g. 0 for 'R' in RGB)
910 * Returns the number of bits used to encode an individual pixel of
911 * a given @component. Typically this is 8, although higher and lower
912 * values are possible for some formats.
916 * Returns: depth of component
919 gst_video_format_get_component_depth (GstVideoFormat format, int component)
921 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
922 g_return_val_if_fail (component < GST_VIDEO_MAX_PLANES, 0);
924 if (format >= G_N_ELEMENTS (formats))
927 return formats[format].depth[component];
931 * gst_video_format_get_pixel_stride:
932 * @format: a #GstVideoFormat
933 * @component: the component index
935 * Calculates the pixel stride (number of bytes from one pixel to the
936 * pixel to its immediate left) for the video component with an index
937 * of @component. See @gst_video_format_get_row_stride for a description
938 * of the component index.
942 * Returns: pixel stride of component @component
945 gst_video_format_get_pixel_stride (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);
951 case GST_VIDEO_FORMAT_I420:
952 case GST_VIDEO_FORMAT_YV12:
953 case GST_VIDEO_FORMAT_Y41B:
954 case GST_VIDEO_FORMAT_Y42B:
955 case GST_VIDEO_FORMAT_Y444:
956 case GST_VIDEO_FORMAT_A420:
957 case GST_VIDEO_FORMAT_YUV9:
958 case GST_VIDEO_FORMAT_YVU9:
960 case GST_VIDEO_FORMAT_YUY2:
961 case GST_VIDEO_FORMAT_YVYU:
962 case GST_VIDEO_FORMAT_UYVY:
963 if (component == 0) {
968 case GST_VIDEO_FORMAT_IYU1:
969 /* doesn't make much sense for IYU1 because it's 1 or 3
970 * for luma depending on position */
972 case GST_VIDEO_FORMAT_AYUV:
973 case GST_VIDEO_FORMAT_RGBx:
974 case GST_VIDEO_FORMAT_BGRx:
975 case GST_VIDEO_FORMAT_xRGB:
976 case GST_VIDEO_FORMAT_xBGR:
977 case GST_VIDEO_FORMAT_RGBA:
978 case GST_VIDEO_FORMAT_BGRA:
979 case GST_VIDEO_FORMAT_ARGB:
980 case GST_VIDEO_FORMAT_ABGR:
981 case GST_VIDEO_FORMAT_r210:
983 case GST_VIDEO_FORMAT_RGB16:
984 case GST_VIDEO_FORMAT_BGR16:
985 case GST_VIDEO_FORMAT_RGB15:
986 case GST_VIDEO_FORMAT_BGR15:
988 case GST_VIDEO_FORMAT_RGB:
989 case GST_VIDEO_FORMAT_BGR:
990 case GST_VIDEO_FORMAT_v308:
992 case GST_VIDEO_FORMAT_v210:
993 /* v210 is packed at the bit level, so pixel stride doesn't make sense */
995 case GST_VIDEO_FORMAT_v216:
996 if (component == 0) {
1001 case GST_VIDEO_FORMAT_NV12:
1002 case GST_VIDEO_FORMAT_NV21:
1003 if (component == 0) {
1008 case GST_VIDEO_FORMAT_GRAY8:
1009 case GST_VIDEO_FORMAT_Y800:
1011 case GST_VIDEO_FORMAT_GRAY16_BE:
1012 case GST_VIDEO_FORMAT_GRAY16_LE:
1013 case GST_VIDEO_FORMAT_Y16:
1015 case GST_VIDEO_FORMAT_UYVP:
1016 /* UYVP is packed at the bit level, so pixel stride doesn't make sense */
1018 case GST_VIDEO_FORMAT_RGB8_PALETTED:
1020 case GST_VIDEO_FORMAT_ARGB64:
1021 case GST_VIDEO_FORMAT_AYUV64:
1029 * gst_video_info_init:
1030 * @info: a #GstVideoInfo
1032 * Initialize @info with default values.
1035 gst_video_info_init (GstVideoInfo * info)
1037 g_return_if_fail (info != NULL);
1039 memset (info, 0, sizeof (GstVideoInfo));
1043 * gst_video_info_set_format:
1044 * @info: a #GstVideoInfo
1045 * @format: the format
1049 * Set the default info for a video frame of @format and @width and @height.
1052 gst_video_info_set_format (GstVideoInfo * info, GstVideoFormat format,
1053 guint width, guint height)
1055 g_return_if_fail (info != NULL);
1056 g_return_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN);
1058 info->format = format;
1059 info->width = width;
1060 info->heigth = height;
1064 * gst_video_info_from_caps:
1065 * @info: a #GstVideoInfo
1068 * Parse @caps and update @info.
1070 * Returns: TRUE if @caps could be parsed
1073 gst_video_info_from_caps (GstVideoInfo * info, const GstCaps * caps)
1075 GstStructure *structure;
1078 GstVideoFormat format;
1079 gint width, height g_return_val_if_fail (info != NULL, FALSE);
1080 g_return_val_if_fail (caps != NULL, FALSE);
1082 if (!gst_caps_is_fixed (caps))
1085 structure = gst_caps_get_structure (caps, 0);
1087 if (!gst_structure_has_name (structure, "video/x-raw"))
1090 if (!(fmt = gst_structure_get_string (structure, "format")))
1093 format = gst_video_format_from_string (fmt);
1094 if (format == GST_VIDEO_FORMAT_UNKNOWN)
1095 goto unknown_format;
1097 info->format = format;
1099 if (gst_structure_get_int (structure, "width", &width))
1100 info->width = width;
1101 if (gst_structure_get_int (structure, "height", &height))
1102 info->height = height;
1122 gst_video_info_to_caps (GstVideoInfo * info)
1128 gst_video_info_convert (GstVideoInfo * info,
1129 GstFormat src_format, gint64 src_value, GstFormat dest_format,
1130 gint64 * dest_value)
1136 * gst_video_format_get_row_stride:
1137 * @format: a #GstVideoFormat
1138 * @component: the component index
1139 * @width: the width of video
1141 * Calculates the row stride (number of bytes from one row of pixels to
1142 * the next) for the video component with an index of @component. For
1143 * YUV video, Y, U, and V have component indices of 0, 1, and 2,
1144 * respectively. For RGB video, R, G, and B have component indicies of
1145 * 0, 1, and 2, respectively. Alpha channels, if present, have a component
1146 * index of 3. The @width parameter always represents the width of the
1147 * video, not the component.
1151 * Returns: row stride of component @component
1154 gst_video_format_get_row_stride (GstVideoFormat format, int component,
1157 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1158 g_return_val_if_fail (component >= 0 && component <= 3, 0);
1159 g_return_val_if_fail (width > 0, 0);
1162 case GST_VIDEO_FORMAT_I420:
1163 case GST_VIDEO_FORMAT_YV12:
1164 if (component == 0) {
1165 return GST_ROUND_UP_4 (width);
1167 return GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1169 case GST_VIDEO_FORMAT_YUY2:
1170 case GST_VIDEO_FORMAT_YVYU:
1171 case GST_VIDEO_FORMAT_UYVY:
1172 return GST_ROUND_UP_4 (width * 2);
1173 case GST_VIDEO_FORMAT_AYUV:
1174 case GST_VIDEO_FORMAT_RGBx:
1175 case GST_VIDEO_FORMAT_BGRx:
1176 case GST_VIDEO_FORMAT_xRGB:
1177 case GST_VIDEO_FORMAT_xBGR:
1178 case GST_VIDEO_FORMAT_RGBA:
1179 case GST_VIDEO_FORMAT_BGRA:
1180 case GST_VIDEO_FORMAT_ARGB:
1181 case GST_VIDEO_FORMAT_ABGR:
1182 case GST_VIDEO_FORMAT_r210:
1184 case GST_VIDEO_FORMAT_RGB16:
1185 case GST_VIDEO_FORMAT_BGR16:
1186 case GST_VIDEO_FORMAT_RGB15:
1187 case GST_VIDEO_FORMAT_BGR15:
1188 return GST_ROUND_UP_4 (width * 2);
1189 case GST_VIDEO_FORMAT_RGB:
1190 case GST_VIDEO_FORMAT_BGR:
1191 case GST_VIDEO_FORMAT_v308:
1192 return GST_ROUND_UP_4 (width * 3);
1193 case GST_VIDEO_FORMAT_Y41B:
1194 if (component == 0) {
1195 return GST_ROUND_UP_4 (width);
1197 return GST_ROUND_UP_16 (width) / 4;
1199 case GST_VIDEO_FORMAT_Y42B:
1200 if (component == 0) {
1201 return GST_ROUND_UP_4 (width);
1203 return GST_ROUND_UP_8 (width) / 2;
1205 case GST_VIDEO_FORMAT_Y444:
1206 return GST_ROUND_UP_4 (width);
1207 case GST_VIDEO_FORMAT_v210:
1208 return ((width + 47) / 48) * 128;
1209 case GST_VIDEO_FORMAT_v216:
1210 return GST_ROUND_UP_8 (width * 4);
1211 case GST_VIDEO_FORMAT_NV12:
1212 case GST_VIDEO_FORMAT_NV21:
1213 return GST_ROUND_UP_4 (width);
1214 case GST_VIDEO_FORMAT_GRAY8:
1215 case GST_VIDEO_FORMAT_Y800:
1216 return GST_ROUND_UP_4 (width);
1217 case GST_VIDEO_FORMAT_GRAY16_BE:
1218 case GST_VIDEO_FORMAT_GRAY16_LE:
1219 case GST_VIDEO_FORMAT_Y16:
1220 return GST_ROUND_UP_4 (width * 2);
1221 case GST_VIDEO_FORMAT_UYVP:
1222 return GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4);
1223 case GST_VIDEO_FORMAT_A420:
1224 if (component == 0 || component == 3) {
1225 return GST_ROUND_UP_4 (width);
1227 return GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1229 case GST_VIDEO_FORMAT_RGB8_PALETTED:
1230 return GST_ROUND_UP_4 (width);
1231 case GST_VIDEO_FORMAT_YUV9:
1232 case GST_VIDEO_FORMAT_YVU9:
1233 if (component == 0) {
1234 return GST_ROUND_UP_4 (width);
1236 return GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
1238 case GST_VIDEO_FORMAT_IYU1:
1239 return GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) +
1240 GST_ROUND_UP_4 (width) / 2);
1241 case GST_VIDEO_FORMAT_ARGB64:
1242 case GST_VIDEO_FORMAT_AYUV64:
1250 * gst_video_format_get_component_width:
1251 * @format: a #GstVideoFormat
1252 * @component: the component index
1253 * @width: the width of video
1255 * Calculates the width of the component. See
1256 * @gst_video_format_get_row_stride for a description
1257 * of the component index.
1261 * Returns: width of component @component
1264 gst_video_format_get_component_width (GstVideoFormat format,
1265 int component, int width)
1267 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1268 g_return_val_if_fail (component >= 0 && component <= 3, 0);
1269 g_return_val_if_fail (width > 0, 0);
1272 case GST_VIDEO_FORMAT_I420:
1273 case GST_VIDEO_FORMAT_YV12:
1274 case GST_VIDEO_FORMAT_YUY2:
1275 case GST_VIDEO_FORMAT_YVYU:
1276 case GST_VIDEO_FORMAT_UYVY:
1277 case GST_VIDEO_FORMAT_Y42B:
1278 case GST_VIDEO_FORMAT_v210:
1279 case GST_VIDEO_FORMAT_v216:
1280 case GST_VIDEO_FORMAT_NV12:
1281 case GST_VIDEO_FORMAT_NV21:
1282 case GST_VIDEO_FORMAT_UYVP:
1283 if (component == 0) {
1286 return GST_ROUND_UP_2 (width) / 2;
1288 case GST_VIDEO_FORMAT_Y41B:
1289 case GST_VIDEO_FORMAT_YUV9:
1290 case GST_VIDEO_FORMAT_YVU9:
1291 case GST_VIDEO_FORMAT_IYU1:
1292 if (component == 0) {
1295 return GST_ROUND_UP_4 (width) / 4;
1297 case GST_VIDEO_FORMAT_AYUV:
1298 case GST_VIDEO_FORMAT_RGBx:
1299 case GST_VIDEO_FORMAT_BGRx:
1300 case GST_VIDEO_FORMAT_xRGB:
1301 case GST_VIDEO_FORMAT_xBGR:
1302 case GST_VIDEO_FORMAT_RGBA:
1303 case GST_VIDEO_FORMAT_BGRA:
1304 case GST_VIDEO_FORMAT_ARGB:
1305 case GST_VIDEO_FORMAT_ABGR:
1306 case GST_VIDEO_FORMAT_RGB:
1307 case GST_VIDEO_FORMAT_BGR:
1308 case GST_VIDEO_FORMAT_RGB16:
1309 case GST_VIDEO_FORMAT_BGR16:
1310 case GST_VIDEO_FORMAT_RGB15:
1311 case GST_VIDEO_FORMAT_BGR15:
1312 case GST_VIDEO_FORMAT_Y444:
1313 case GST_VIDEO_FORMAT_v308:
1314 case GST_VIDEO_FORMAT_GRAY8:
1315 case GST_VIDEO_FORMAT_GRAY16_BE:
1316 case GST_VIDEO_FORMAT_GRAY16_LE:
1317 case GST_VIDEO_FORMAT_Y800:
1318 case GST_VIDEO_FORMAT_Y16:
1319 case GST_VIDEO_FORMAT_RGB8_PALETTED:
1320 case GST_VIDEO_FORMAT_ARGB64:
1321 case GST_VIDEO_FORMAT_AYUV64:
1322 case GST_VIDEO_FORMAT_r210:
1324 case GST_VIDEO_FORMAT_A420:
1325 if (component == 0 || component == 3) {
1328 return GST_ROUND_UP_2 (width) / 2;
1336 * gst_video_format_get_component_height:
1337 * @format: a #GstVideoFormat
1338 * @component: the component index
1339 * @height: the height of video
1341 * Calculates the height of the component. See
1342 * @gst_video_format_get_row_stride for a description
1343 * of the component index.
1347 * Returns: height of component @component
1350 gst_video_format_get_component_height (GstVideoFormat format,
1351 int component, int height)
1353 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1354 g_return_val_if_fail (component >= 0 && component <= 3, 0);
1355 g_return_val_if_fail (height > 0, 0);
1358 case GST_VIDEO_FORMAT_I420:
1359 case GST_VIDEO_FORMAT_YV12:
1360 case GST_VIDEO_FORMAT_NV12:
1361 case GST_VIDEO_FORMAT_NV21:
1362 if (component == 0) {
1365 return GST_ROUND_UP_2 (height) / 2;
1367 case GST_VIDEO_FORMAT_Y41B:
1368 case GST_VIDEO_FORMAT_Y42B:
1369 case GST_VIDEO_FORMAT_YUY2:
1370 case GST_VIDEO_FORMAT_YVYU:
1371 case GST_VIDEO_FORMAT_UYVY:
1372 case GST_VIDEO_FORMAT_AYUV:
1373 case GST_VIDEO_FORMAT_RGBx:
1374 case GST_VIDEO_FORMAT_BGRx:
1375 case GST_VIDEO_FORMAT_xRGB:
1376 case GST_VIDEO_FORMAT_xBGR:
1377 case GST_VIDEO_FORMAT_RGBA:
1378 case GST_VIDEO_FORMAT_BGRA:
1379 case GST_VIDEO_FORMAT_ARGB:
1380 case GST_VIDEO_FORMAT_ABGR:
1381 case GST_VIDEO_FORMAT_RGB:
1382 case GST_VIDEO_FORMAT_BGR:
1383 case GST_VIDEO_FORMAT_RGB16:
1384 case GST_VIDEO_FORMAT_BGR16:
1385 case GST_VIDEO_FORMAT_RGB15:
1386 case GST_VIDEO_FORMAT_BGR15:
1387 case GST_VIDEO_FORMAT_Y444:
1388 case GST_VIDEO_FORMAT_v210:
1389 case GST_VIDEO_FORMAT_v216:
1390 case GST_VIDEO_FORMAT_v308:
1391 case GST_VIDEO_FORMAT_GRAY8:
1392 case GST_VIDEO_FORMAT_GRAY16_BE:
1393 case GST_VIDEO_FORMAT_GRAY16_LE:
1394 case GST_VIDEO_FORMAT_Y800:
1395 case GST_VIDEO_FORMAT_Y16:
1396 case GST_VIDEO_FORMAT_UYVP:
1397 case GST_VIDEO_FORMAT_RGB8_PALETTED:
1398 case GST_VIDEO_FORMAT_IYU1:
1399 case GST_VIDEO_FORMAT_ARGB64:
1400 case GST_VIDEO_FORMAT_AYUV64:
1401 case GST_VIDEO_FORMAT_r210:
1403 case GST_VIDEO_FORMAT_A420:
1404 if (component == 0 || component == 3) {
1407 return GST_ROUND_UP_2 (height) / 2;
1409 case GST_VIDEO_FORMAT_YUV9:
1410 case GST_VIDEO_FORMAT_YVU9:
1411 if (component == 0) {
1414 return GST_ROUND_UP_4 (height) / 4;
1422 * gst_video_format_get_component_offset:
1423 * @format: a #GstVideoFormat
1424 * @component: the component index
1425 * @width: the width of video
1426 * @height: the height of video
1428 * Calculates the offset (in bytes) of the first pixel of the component
1429 * with index @component. For packed formats, this will typically be a
1430 * small integer (0, 1, 2, 3). For planar formats, this will be a
1431 * (relatively) large offset to the beginning of the second or third
1432 * component planes. See @gst_video_format_get_row_stride for a description
1433 * of the component index.
1437 * Returns: offset of component @component
1440 gst_video_format_get_component_offset (GstVideoFormat format,
1441 int component, int width, int height)
1443 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1444 g_return_val_if_fail (component >= 0 && component <= 3, 0);
1445 g_return_val_if_fail ((!gst_video_format_is_yuv (format)) || (width > 0
1449 case GST_VIDEO_FORMAT_I420:
1453 return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1454 if (component == 2) {
1455 return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1456 GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1457 (GST_ROUND_UP_2 (height) / 2);
1460 case GST_VIDEO_FORMAT_YV12: /* same as I420, but components 1+2 swapped */
1464 return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1465 if (component == 1) {
1466 return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1467 GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1468 (GST_ROUND_UP_2 (height) / 2);
1471 case GST_VIDEO_FORMAT_YUY2:
1479 case GST_VIDEO_FORMAT_YVYU:
1487 case GST_VIDEO_FORMAT_UYVY:
1495 case GST_VIDEO_FORMAT_AYUV:
1505 case GST_VIDEO_FORMAT_RGBx:
1506 case GST_VIDEO_FORMAT_RGBA:
1516 case GST_VIDEO_FORMAT_BGRx:
1517 case GST_VIDEO_FORMAT_BGRA:
1527 case GST_VIDEO_FORMAT_xRGB:
1528 case GST_VIDEO_FORMAT_ARGB:
1538 case GST_VIDEO_FORMAT_xBGR:
1539 case GST_VIDEO_FORMAT_ABGR:
1549 case GST_VIDEO_FORMAT_RGB:
1550 case GST_VIDEO_FORMAT_v308:
1558 case GST_VIDEO_FORMAT_BGR:
1566 case GST_VIDEO_FORMAT_Y41B:
1570 return GST_ROUND_UP_4 (width) * height;
1572 return (GST_ROUND_UP_4 (width) +
1573 (GST_ROUND_UP_16 (width) / 4)) * height;
1575 case GST_VIDEO_FORMAT_Y42B:
1579 return GST_ROUND_UP_4 (width) * height;
1581 return (GST_ROUND_UP_4 (width) + (GST_ROUND_UP_8 (width) / 2)) * height;
1583 case GST_VIDEO_FORMAT_Y444:
1584 return GST_ROUND_UP_4 (width) * height * component;
1585 case GST_VIDEO_FORMAT_v210:
1586 case GST_VIDEO_FORMAT_r210:
1587 /* v210 is bit-packed, so this doesn't make sense */
1589 case GST_VIDEO_FORMAT_v216:
1597 case GST_VIDEO_FORMAT_NV12:
1601 return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1603 return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) + 1;
1605 case GST_VIDEO_FORMAT_NV21:
1609 return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) + 1;
1611 return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1613 case GST_VIDEO_FORMAT_GRAY8:
1614 case GST_VIDEO_FORMAT_GRAY16_BE:
1615 case GST_VIDEO_FORMAT_GRAY16_LE:
1616 case GST_VIDEO_FORMAT_Y800:
1617 case GST_VIDEO_FORMAT_Y16:
1619 case GST_VIDEO_FORMAT_UYVP:
1620 /* UYVP is bit-packed, so this doesn't make sense */
1622 case GST_VIDEO_FORMAT_A420:
1626 return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1627 if (component == 2) {
1628 return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1629 GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1630 (GST_ROUND_UP_2 (height) / 2);
1632 if (component == 3) {
1633 return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1634 2 * GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1635 (GST_ROUND_UP_2 (height) / 2);
1638 case GST_VIDEO_FORMAT_RGB8_PALETTED:
1640 case GST_VIDEO_FORMAT_YUV9:
1644 return GST_ROUND_UP_4 (width) * height;
1645 if (component == 2) {
1646 return GST_ROUND_UP_4 (width) * height +
1647 GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4) *
1648 (GST_ROUND_UP_4 (height) / 4);
1651 case GST_VIDEO_FORMAT_YVU9:
1654 if (component == 1) {
1655 return GST_ROUND_UP_4 (width) * height +
1656 GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4) *
1657 (GST_ROUND_UP_4 (height) / 4);
1660 return GST_ROUND_UP_4 (width) * height;
1662 case GST_VIDEO_FORMAT_IYU1:
1670 case GST_VIDEO_FORMAT_ARGB64:
1671 case GST_VIDEO_FORMAT_AYUV64:
1684 GST_WARNING ("unhandled format %d or component %d", format, component);
1689 * gst_video_format_get_size:
1690 * @format: a #GstVideoFormat
1691 * @width: the width of video
1692 * @height: the height of video
1694 * Calculates the total number of bytes in the raw video format. This
1695 * number should be used when allocating a buffer for raw video.
1699 * Returns: size (in bytes) of raw video format
1702 gst_video_format_get_size (GstVideoFormat format, int width, int height)
1706 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1707 g_return_val_if_fail (width > 0 && height > 0, 0);
1710 case GST_VIDEO_FORMAT_I420:
1711 case GST_VIDEO_FORMAT_YV12:
1712 size = GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1713 size += GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1714 (GST_ROUND_UP_2 (height) / 2) * 2;
1716 case GST_VIDEO_FORMAT_IYU1:
1717 return GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) +
1718 GST_ROUND_UP_4 (width) / 2) * height;
1719 case GST_VIDEO_FORMAT_YUY2:
1720 case GST_VIDEO_FORMAT_YVYU:
1721 case GST_VIDEO_FORMAT_UYVY:
1722 return GST_ROUND_UP_4 (width * 2) * height;
1723 case GST_VIDEO_FORMAT_AYUV:
1724 case GST_VIDEO_FORMAT_RGBx:
1725 case GST_VIDEO_FORMAT_BGRx:
1726 case GST_VIDEO_FORMAT_xRGB:
1727 case GST_VIDEO_FORMAT_xBGR:
1728 case GST_VIDEO_FORMAT_RGBA:
1729 case GST_VIDEO_FORMAT_BGRA:
1730 case GST_VIDEO_FORMAT_ARGB:
1731 case GST_VIDEO_FORMAT_ABGR:
1732 case GST_VIDEO_FORMAT_r210:
1733 return width * 4 * height;
1734 case GST_VIDEO_FORMAT_RGB16:
1735 case GST_VIDEO_FORMAT_BGR16:
1736 case GST_VIDEO_FORMAT_RGB15:
1737 case GST_VIDEO_FORMAT_BGR15:
1738 return GST_ROUND_UP_4 (width * 2) * height;
1739 case GST_VIDEO_FORMAT_RGB:
1740 case GST_VIDEO_FORMAT_BGR:
1741 case GST_VIDEO_FORMAT_v308:
1742 return GST_ROUND_UP_4 (width * 3) * height;
1743 case GST_VIDEO_FORMAT_Y41B:
1744 /* simplification of ROUNDUP4(w)*h + 2*((ROUNDUP16(w)/4)*h */
1745 return (GST_ROUND_UP_4 (width) + (GST_ROUND_UP_16 (width) / 2)) * height;
1746 case GST_VIDEO_FORMAT_Y42B:
1747 /* simplification of ROUNDUP4(w)*h + 2*(ROUNDUP8(w)/2)*h */
1748 return (GST_ROUND_UP_4 (width) + GST_ROUND_UP_8 (width)) * height;
1749 case GST_VIDEO_FORMAT_Y444:
1750 return GST_ROUND_UP_4 (width) * height * 3;
1751 case GST_VIDEO_FORMAT_v210:
1752 return ((width + 47) / 48) * 128 * height;
1753 case GST_VIDEO_FORMAT_v216:
1754 return GST_ROUND_UP_8 (width * 4) * height;
1755 case GST_VIDEO_FORMAT_NV12:
1756 case GST_VIDEO_FORMAT_NV21:
1757 return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) * 3 / 2;
1758 case GST_VIDEO_FORMAT_GRAY8:
1759 case GST_VIDEO_FORMAT_Y800:
1760 case GST_VIDEO_FORMAT_RGB8_PALETTED:
1761 return GST_ROUND_UP_4 (width) * height;
1762 case GST_VIDEO_FORMAT_GRAY16_BE:
1763 case GST_VIDEO_FORMAT_GRAY16_LE:
1764 case GST_VIDEO_FORMAT_Y16:
1765 return GST_ROUND_UP_4 (width * 2) * height;
1766 case GST_VIDEO_FORMAT_UYVP:
1767 return GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4) * height;
1768 case GST_VIDEO_FORMAT_A420:
1769 size = 2 * GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1770 size += GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1771 (GST_ROUND_UP_2 (height) / 2) * 2;
1773 case GST_VIDEO_FORMAT_YUV9:
1774 case GST_VIDEO_FORMAT_YVU9:
1775 size = GST_ROUND_UP_4 (width) * height;
1776 size += GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4) *
1777 (GST_ROUND_UP_4 (height) / 4) * 2;
1779 case GST_VIDEO_FORMAT_ARGB64:
1780 case GST_VIDEO_FORMAT_AYUV64:
1781 return width * 8 * height;
1788 * gst_video_get_size_from_caps:
1789 * @caps: a pointer to #GstCaps
1790 * @size: a pointer to a gint that will be assigned the size (in bytes) of a video frame with the given caps
1792 * Calculates the total number of bytes in the raw video format for the given
1793 * caps. This number should be used when allocating a buffer for raw video.
1797 * Returns: %TRUE if the size could be calculated from the caps
1800 gst_video_get_size_from_caps (const GstCaps * caps, gint * size)
1802 GstVideoFormat format = 0;
1803 gint width = 0, height = 0;
1805 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
1806 g_return_val_if_fail (size != NULL, FALSE);
1808 if (gst_video_format_parse_caps (caps, &format, &width, &height) == FALSE) {
1809 GST_WARNING ("Could not parse caps: %" GST_PTR_FORMAT, caps);
1813 *size = gst_video_format_get_size (format, width, height);
1818 * gst_video_format_convert:
1819 * @format: a #GstVideoFormat
1820 * @width: the width of video
1821 * @height: the height of video
1822 * @fps_n: frame rate numerator
1823 * @fps_d: frame rate denominator
1824 * @src_format: #GstFormat of the @src_value
1825 * @src_value: value to convert
1826 * @dest_format: #GstFormat of the @dest_value
1827 * @dest_value: pointer to destination value
1829 * Converts among various #GstFormat types. This function handles
1830 * GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT. For
1831 * raw video, GST_FORMAT_DEFAULT corresponds to video frames. This
1832 * function can be to handle pad queries of the type GST_QUERY_CONVERT.
1836 * Returns: TRUE if the conversion was successful.
1839 gst_video_format_convert (GstVideoFormat format, int width, int height,
1840 int fps_n, int fps_d,
1841 GstFormat src_format, gint64 src_value,
1842 GstFormat dest_format, gint64 * dest_value)
1844 gboolean ret = FALSE;
1847 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1848 g_return_val_if_fail (width > 0 && height > 0, 0);
1850 size = gst_video_format_get_size (format, width, height);
1852 GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s to %s",
1853 src_value, gst_format_get_name (src_format),
1854 gst_format_get_name (dest_format));
1856 if (src_format == dest_format) {
1857 *dest_value = src_value;
1862 if (src_value == -1) {
1868 /* bytes to frames */
1869 if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_DEFAULT) {
1871 *dest_value = gst_util_uint64_scale_int (src_value, 1, size);
1873 GST_ERROR ("blocksize is 0");
1880 /* frames to bytes */
1881 if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_BYTES) {
1882 *dest_value = gst_util_uint64_scale_int (src_value, size, 1);
1887 /* time to frames */
1888 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_DEFAULT) {
1890 *dest_value = gst_util_uint64_scale (src_value,
1891 fps_n, GST_SECOND * fps_d);
1893 GST_ERROR ("framerate denominator is 0");
1900 /* frames to time */
1901 if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) {
1903 *dest_value = gst_util_uint64_scale (src_value,
1904 GST_SECOND * fps_d, fps_n);
1906 GST_ERROR ("framerate numerator is 0");
1914 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
1916 *dest_value = gst_util_uint64_scale (src_value,
1917 fps_n * size, GST_SECOND * fps_d);
1919 GST_ERROR ("framerate denominator is 0");
1927 if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
1928 if (fps_n != 0 && size != 0) {
1929 *dest_value = gst_util_uint64_scale (src_value,
1930 GST_SECOND * fps_d, fps_n * size);
1932 GST_ERROR ("framerate denominator and/or blocksize is 0");
1940 GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, ret, *dest_value);
1945 #define GST_VIDEO_EVENT_STILL_STATE_NAME "GstEventStillFrame"
1948 * gst_video_event_new_still_frame:
1949 * @in_still: boolean value for the still-frame state of the event.
1951 * Creates a new Still Frame event. If @in_still is %TRUE, then the event
1952 * represents the start of a still frame sequence. If it is %FALSE, then
1953 * the event ends a still frame sequence.
1955 * To parse an event created by gst_video_event_new_still_frame() use
1956 * gst_video_event_parse_still_frame().
1958 * Returns: The new GstEvent
1962 gst_video_event_new_still_frame (gboolean in_still)
1964 GstEvent *still_event;
1967 s = gst_structure_new (GST_VIDEO_EVENT_STILL_STATE_NAME,
1968 "still-state", G_TYPE_BOOLEAN, in_still, NULL);
1969 still_event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1975 * gst_video_event_parse_still_frame:
1976 * @event: A #GstEvent to parse
1977 * @in_still: A boolean to receive the still-frame status from the event, or NULL
1979 * Parse a #GstEvent, identify if it is a Still Frame event, and
1980 * return the still-frame state from the event if it is.
1981 * If the event represents the start of a still frame, the in_still
1982 * variable will be set to TRUE, otherwise FALSE. It is OK to pass NULL for the
1983 * in_still variable order to just check whether the event is a valid still-frame
1986 * Create a still frame event using gst_video_event_new_still_frame()
1988 * Returns: %TRUE if the event is a valid still-frame event. %FALSE if not
1992 gst_video_event_parse_still_frame (GstEvent * event, gboolean * in_still)
1994 const GstStructure *s;
1995 gboolean ev_still_state;
1997 g_return_val_if_fail (event != NULL, FALSE);
1999 if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM)
2000 return FALSE; /* Not a still frame event */
2002 s = gst_event_get_structure (event);
2004 || !gst_structure_has_name (s, GST_VIDEO_EVENT_STILL_STATE_NAME))
2005 return FALSE; /* Not a still frame event */
2006 if (!gst_structure_get_boolean (s, "still-state", &ev_still_state))
2007 return FALSE; /* Not a still frame event */
2009 *in_still = ev_still_state;
2014 * gst_video_parse_caps_palette:
2015 * @caps: #GstCaps to parse
2017 * Returns the palette data from the caps as a #GstBuffer. For
2018 * #GST_VIDEO_FORMAT_RGB8_PALETTED this is containing 256 #guint32
2019 * values, each containing ARGB colors in native endianness.
2021 * Returns: a #GstBuffer containing the palette data. Unref after usage.
2025 gst_video_parse_caps_palette (GstCaps * caps)
2031 if (!gst_caps_is_fixed (caps))
2034 s = gst_caps_get_structure (caps, 0);
2036 p_v = gst_structure_get_value (s, "palette_data");
2037 if (!p_v || !GST_VALUE_HOLDS_BUFFER (p_v))
2040 p = gst_buffer_ref (gst_value_get_buffer (p_v));