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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
29 #include "video-info.h"
30 #include "video-tile.h"
32 #ifndef GST_DISABLE_GST_DEBUG
33 #define GST_CAT_DEFAULT ensure_debug_category()
34 static GstDebugCategory *
35 ensure_debug_category (void)
37 static gsize cat_gonce = 0;
39 if (g_once_init_enter (&cat_gonce)) {
42 cat_done = (gsize) _gst_debug_category_new ("video-info", 0,
43 "video-info structure");
45 g_once_init_leave (&cat_gonce, cat_done);
48 return (GstDebugCategory *) cat_gonce;
51 #define ensure_debug_category() /* NOOP */
52 #endif /* GST_DISABLE_GST_DEBUG */
55 * gst_video_info_copy:
56 * @info: a #GstVideoInfo
58 * Copy a GstVideoInfo structure.
60 * Returns: a new #GstVideoInfo. free with gst_video_info_free.
65 gst_video_info_copy (const GstVideoInfo * info)
67 return g_slice_dup (GstVideoInfo, info);
71 * gst_video_info_free:
72 * @info: a #GstVideoInfo
74 * Free a GstVideoInfo structure previously allocated with gst_video_info_new()
75 * or gst_video_info_copy().
80 gst_video_info_free (GstVideoInfo * info)
82 g_slice_free (GstVideoInfo, info);
85 G_DEFINE_BOXED_TYPE (GstVideoInfo, gst_video_info,
86 (GBoxedCopyFunc) gst_video_info_copy, (GBoxedFreeFunc) gst_video_info_free);
91 * Allocate a new #GstVideoInfo that is also initialized with
92 * gst_video_info_init().
94 * Returns: a new #GstVideoInfo. free with gst_video_info_free().
99 gst_video_info_new (void)
103 info = g_slice_new (GstVideoInfo);
104 gst_video_info_init (info);
109 static gboolean fill_planes (GstVideoInfo * info);
112 * gst_video_info_init:
113 * @info: a #GstVideoInfo
115 * Initialize @info with default values.
118 gst_video_info_init (GstVideoInfo * info)
120 g_return_if_fail (info != NULL);
122 memset (info, 0, sizeof (GstVideoInfo));
124 info->finfo = gst_video_format_get_info (GST_VIDEO_FORMAT_UNKNOWN);
127 /* arrange for sensible defaults, e.g. if turned into caps */
132 GST_VIDEO_INFO_MULTIVIEW_MODE (info) = GST_VIDEO_MULTIVIEW_MODE_NONE;
133 GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) = GST_VIDEO_MULTIVIEW_FLAGS_NONE;
134 GST_VIDEO_INFO_FIELD_ORDER (info) = GST_VIDEO_FIELD_ORDER_UNKNOWN;
137 #define MAKE_COLORIMETRY(r,m,t,p) { \
138 GST_VIDEO_COLOR_RANGE ##r, GST_VIDEO_COLOR_MATRIX_ ##m, \
139 GST_VIDEO_TRANSFER_ ##t, GST_VIDEO_COLOR_PRIMARIES_ ##p }
141 #define DEFAULT_YUV_SD 0
142 #define DEFAULT_YUV_HD 1
143 #define DEFAULT_RGB 2
144 #define DEFAULT_GRAY 3
145 #define DEFAULT_UNKNOWN 4
146 #define DEFAULT_YUV_UHD 5
148 static const GstVideoColorimetry default_color[] = {
149 MAKE_COLORIMETRY (_16_235, BT601, BT709, SMPTE170M),
150 MAKE_COLORIMETRY (_16_235, BT709, BT709, BT709),
151 MAKE_COLORIMETRY (_0_255, RGB, SRGB, BT709),
152 MAKE_COLORIMETRY (_0_255, BT601, UNKNOWN, UNKNOWN),
153 MAKE_COLORIMETRY (_UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN),
154 MAKE_COLORIMETRY (_16_235, BT2020, BT2020_12, BT2020),
158 set_default_colorimetry (GstVideoInfo * info)
160 const GstVideoFormatInfo *finfo = info->finfo;
162 if (GST_VIDEO_FORMAT_INFO_IS_YUV (finfo)) {
163 if (info->height >= 2160) {
164 info->chroma_site = GST_VIDEO_CHROMA_SITE_H_COSITED;
165 info->colorimetry = default_color[DEFAULT_YUV_UHD];
166 } else if (info->height > 576) {
167 info->chroma_site = GST_VIDEO_CHROMA_SITE_H_COSITED;
168 info->colorimetry = default_color[DEFAULT_YUV_HD];
170 info->chroma_site = GST_VIDEO_CHROMA_SITE_NONE;
171 info->colorimetry = default_color[DEFAULT_YUV_SD];
173 } else if (GST_VIDEO_FORMAT_INFO_IS_GRAY (finfo)) {
174 info->colorimetry = default_color[DEFAULT_GRAY];
175 } else if (GST_VIDEO_FORMAT_INFO_IS_RGB (finfo)) {
176 info->colorimetry = default_color[DEFAULT_RGB];
178 info->colorimetry = default_color[DEFAULT_UNKNOWN];
183 validate_colorimetry (GstVideoInfo * info)
185 const GstVideoFormatInfo *finfo = info->finfo;
187 if (!GST_VIDEO_FORMAT_INFO_IS_RGB (finfo) &&
188 info->colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_RGB)
191 if (GST_VIDEO_FORMAT_INFO_IS_YUV (finfo) &&
192 info->colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_UNKNOWN)
199 * gst_video_info_set_format:
200 * @info: a #GstVideoInfo
201 * @format: the format
205 * Set the default info for a video frame of @format and @width and @height.
207 * Note: This initializes @info first, no values are preserved. This function
208 * does not set the offsets correctly for interlaced vertically
209 * subsampled formats.
211 * Returns: %FALSE if the returned video info is invalid, e.g. because the
212 * size of a frame can't be represented as a 32 bit integer (Since: 1.12)
215 gst_video_info_set_format (GstVideoInfo * info, GstVideoFormat format,
216 guint width, guint height)
218 g_return_val_if_fail (info != NULL, FALSE);
219 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, FALSE);
221 if (width > G_MAXINT || height > G_MAXINT)
224 gst_video_info_init (info);
226 info->finfo = gst_video_format_get_info (format);
228 info->height = height;
231 set_default_colorimetry (info);
233 return fill_planes (info);
236 static const gchar *interlace_mode[] = {
244 * gst_video_interlace_mode_to_string:
245 * @mode: a #GstVideoInterlaceMode
247 * Convert @mode to its string representation.
249 * Returns: @mode as a string or NULL if @mode in invalid.
254 gst_video_interlace_mode_to_string (GstVideoInterlaceMode mode)
256 if (((guint) mode) >= G_N_ELEMENTS (interlace_mode))
259 return interlace_mode[mode];
263 * gst_video_interlace_mode_from_string:
266 * Convert @mode to a #GstVideoInterlaceMode
268 * Returns: the #GstVideoInterlaceMode of @mode or
269 * #GST_VIDEO_INTERLACE_MODE_PROGRESSIVE when @mode is not a valid
270 * string representation for a #GstVideoInterlaceMode.
274 GstVideoInterlaceMode
275 gst_video_interlace_mode_from_string (const gchar * mode)
278 for (i = 0; i < G_N_ELEMENTS (interlace_mode); i++) {
279 if (g_str_equal (interlace_mode[i], mode))
282 return GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
285 static const gchar *field_order[] = {
292 * gst_video_field_order_to_string:
293 * @order: a #GstVideoFieldOrder
295 * Convert @order to its string representation.
297 * Returns: @order as a string or NULL if @order in invalid.
302 gst_video_field_order_to_string (GstVideoFieldOrder order)
304 if (((guint) order) >= G_N_ELEMENTS (field_order))
307 return field_order[order];
311 * gst_video_field_order_from_string:
312 * @order: a field order
314 * Convert @order to a #GstVideoFieldOrder
316 * Returns: the #GstVideoFieldOrder of @order or
317 * #GST_VIDEO_FIELD_ORDER_UNKNOWN when @order is not a valid
318 * string representation for a #GstVideoFieldOrder.
323 gst_video_field_order_from_string (const gchar * order)
326 for (i = 0; i < G_N_ELEMENTS (field_order); i++) {
327 if (g_str_equal (field_order[i], order))
330 return GST_VIDEO_FIELD_ORDER_UNKNOWN;
334 * gst_video_info_from_caps:
335 * @info: a #GstVideoInfo
338 * Parse @caps and update @info.
340 * Returns: TRUE if @caps could be parsed
343 gst_video_info_from_caps (GstVideoInfo * info, const GstCaps * caps)
345 GstStructure *structure;
347 GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
348 gint width = 0, height = 0;
351 guint multiview_flags;
353 g_return_val_if_fail (info != NULL, FALSE);
354 g_return_val_if_fail (caps != NULL, FALSE);
355 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
357 GST_DEBUG ("parsing caps %" GST_PTR_FORMAT, caps);
359 structure = gst_caps_get_structure (caps, 0);
361 if (gst_structure_has_name (structure, "video/x-raw")) {
362 if (!(s = gst_structure_get_string (structure, "format")))
365 format = gst_video_format_from_string (s);
366 if (format == GST_VIDEO_FORMAT_UNKNOWN)
369 } else if (g_str_has_prefix (gst_structure_get_name (structure), "video/") ||
370 g_str_has_prefix (gst_structure_get_name (structure), "image/")) {
371 format = GST_VIDEO_FORMAT_ENCODED;
376 /* width and height are mandatory, except for non-raw-formats */
377 if (!gst_structure_get_int (structure, "width", &width) &&
378 format != GST_VIDEO_FORMAT_ENCODED)
380 if (!gst_structure_get_int (structure, "height", &height) &&
381 format != GST_VIDEO_FORMAT_ENCODED)
384 gst_video_info_init (info);
386 info->finfo = gst_video_format_get_info (format);
388 info->height = height;
390 if (gst_structure_get_fraction (structure, "framerate", &fps_n, &fps_d)) {
392 /* variable framerate */
393 info->flags |= GST_VIDEO_FLAG_VARIABLE_FPS;
394 /* see if we have a max-framerate */
395 gst_structure_get_fraction (structure, "max-framerate", &fps_n, &fps_d);
400 /* unspecified is variable framerate */
405 if (gst_structure_get_fraction (structure, "pixel-aspect-ratio",
414 if ((s = gst_structure_get_string (structure, "interlace-mode")))
415 info->interlace_mode = gst_video_interlace_mode_from_string (s);
417 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
419 if (info->interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED &&
420 (s = gst_structure_get_string (structure, "field-order"))) {
421 GST_VIDEO_INFO_FIELD_ORDER (info) = gst_video_field_order_from_string (s);
423 GST_VIDEO_INFO_FIELD_ORDER (info) = GST_VIDEO_FIELD_ORDER_UNKNOWN;
427 if ((s = gst_structure_get_string (structure, "multiview-mode")))
428 GST_VIDEO_INFO_MULTIVIEW_MODE (info) =
429 gst_video_multiview_mode_from_caps_string (s);
431 GST_VIDEO_INFO_MULTIVIEW_MODE (info) = GST_VIDEO_MULTIVIEW_MODE_NONE;
433 if (gst_structure_get_flagset (structure, "multiview-flags",
434 &multiview_flags, NULL))
435 GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) = multiview_flags;
437 if (!gst_structure_get_int (structure, "views", &info->views))
440 /* At one point, I tried normalising the half-aspect flag here,
441 * but it behaves weird for GstVideoInfo operations other than
442 * directly converting to/from caps - sometimes causing the
443 * PAR to be doubled/halved too many times */
446 if ((s = gst_structure_get_string (structure, "chroma-site")))
447 info->chroma_site = gst_video_chroma_from_string (s);
449 info->chroma_site = GST_VIDEO_CHROMA_SITE_UNKNOWN;
451 if ((s = gst_structure_get_string (structure, "colorimetry"))) {
452 if (!gst_video_colorimetry_from_string (&info->colorimetry, s)) {
453 GST_WARNING ("unparsable colorimetry, using default");
454 set_default_colorimetry (info);
455 } else if (!validate_colorimetry (info)) {
456 GST_WARNING ("invalid colorimetry, using default");
457 set_default_colorimetry (info);
459 /* force RGB matrix for RGB formats */
460 if (GST_VIDEO_FORMAT_INFO_IS_RGB (info->finfo) &&
461 info->colorimetry.matrix != GST_VIDEO_COLOR_MATRIX_RGB) {
462 GST_WARNING ("invalid matrix %d for RGB format, using RGB",
463 info->colorimetry.matrix);
464 info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_RGB;
468 GST_DEBUG ("no colorimetry, using default");
469 set_default_colorimetry (info);
472 if (!fill_planes (info))
480 GST_ERROR ("wrong name '%s', expected video/ or image/",
481 gst_structure_get_name (structure));
486 GST_ERROR ("no format given");
491 GST_ERROR ("unknown format '%s' given", s);
496 GST_ERROR ("no width property given");
501 GST_ERROR ("no height property given");
507 * gst_video_info_is_equal:
508 * @info: a #GstVideoInfo
509 * @other: a #GstVideoInfo
511 * Compares two #GstVideoInfo and returns whether they are equal or not
513 * Returns: %TRUE if @info and @other are equal, else %FALSE.
516 gst_video_info_is_equal (const GstVideoInfo * info, const GstVideoInfo * other)
520 if (GST_VIDEO_INFO_FORMAT (info) != GST_VIDEO_INFO_FORMAT (other))
522 if (GST_VIDEO_INFO_INTERLACE_MODE (info) !=
523 GST_VIDEO_INFO_INTERLACE_MODE (other))
525 if (GST_VIDEO_INFO_FLAGS (info) != GST_VIDEO_INFO_FLAGS (other))
527 if (GST_VIDEO_INFO_WIDTH (info) != GST_VIDEO_INFO_WIDTH (other))
529 if (GST_VIDEO_INFO_HEIGHT (info) != GST_VIDEO_INFO_HEIGHT (other))
531 if (GST_VIDEO_INFO_SIZE (info) != GST_VIDEO_INFO_SIZE (other))
533 if (GST_VIDEO_INFO_PAR_N (info) != GST_VIDEO_INFO_PAR_N (other))
535 if (GST_VIDEO_INFO_PAR_D (info) != GST_VIDEO_INFO_PAR_D (other))
537 if (GST_VIDEO_INFO_FPS_N (info) != GST_VIDEO_INFO_FPS_N (other))
539 if (GST_VIDEO_INFO_FPS_D (info) != GST_VIDEO_INFO_FPS_D (other))
541 if (!gst_video_colorimetry_is_equal (&GST_VIDEO_INFO_COLORIMETRY (info),
542 &GST_VIDEO_INFO_COLORIMETRY (other)))
544 if (GST_VIDEO_INFO_CHROMA_SITE (info) != GST_VIDEO_INFO_CHROMA_SITE (other))
546 if (GST_VIDEO_INFO_MULTIVIEW_MODE (info) !=
547 GST_VIDEO_INFO_MULTIVIEW_MODE (other))
549 if (GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) !=
550 GST_VIDEO_INFO_MULTIVIEW_FLAGS (other))
552 if (GST_VIDEO_INFO_VIEWS (info) != GST_VIDEO_INFO_VIEWS (other))
555 for (i = 0; i < info->finfo->n_planes; i++) {
556 if (info->stride[i] != other->stride[i])
558 if (info->offset[i] != other->offset[i])
566 * gst_video_info_to_caps:
567 * @info: a #GstVideoInfo
569 * Convert the values of @info into a #GstCaps.
571 * Returns: a new #GstCaps containing the info of @info.
574 gst_video_info_to_caps (GstVideoInfo * info)
580 GstVideoColorimetry colorimetry;
582 g_return_val_if_fail (info != NULL, NULL);
583 g_return_val_if_fail (info->finfo != NULL, NULL);
584 g_return_val_if_fail (info->finfo->format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
586 format = gst_video_format_to_string (info->finfo->format);
587 g_return_val_if_fail (format != NULL, NULL);
589 caps = gst_caps_new_simple ("video/x-raw",
590 "format", G_TYPE_STRING, format,
591 "width", G_TYPE_INT, info->width,
592 "height", G_TYPE_INT, info->height, NULL);
597 gst_caps_set_simple (caps, "interlace-mode", G_TYPE_STRING,
598 gst_video_interlace_mode_to_string (info->interlace_mode), NULL);
600 if (info->interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED &&
601 GST_VIDEO_INFO_FIELD_ORDER (info) != GST_VIDEO_FIELD_ORDER_UNKNOWN) {
602 gst_caps_set_simple (caps, "field-order", G_TYPE_STRING,
603 gst_video_field_order_to_string (GST_VIDEO_INFO_FIELD_ORDER (info)),
607 if (GST_VIDEO_INFO_MULTIVIEW_MODE (info) != GST_VIDEO_MULTIVIEW_MODE_NONE) {
608 const gchar *caps_str = NULL;
610 /* If the half-aspect flag is set, applying it into the PAR of the
611 * resulting caps now seems safe, and helps with automatic behaviour
612 * in elements that aren't explicitly multiview aware */
613 if (GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) &
614 GST_VIDEO_MULTIVIEW_FLAGS_HALF_ASPECT) {
615 GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) &=
616 ~GST_VIDEO_MULTIVIEW_FLAGS_HALF_ASPECT;
617 switch (GST_VIDEO_INFO_MULTIVIEW_MODE (info)) {
618 case GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE:
619 case GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE_QUINCUNX:
620 case GST_VIDEO_MULTIVIEW_MODE_COLUMN_INTERLEAVED:
621 case GST_VIDEO_MULTIVIEW_MODE_CHECKERBOARD:
622 par_n *= 2; /* double the width / half the height */
624 case GST_VIDEO_MULTIVIEW_MODE_ROW_INTERLEAVED:
625 case GST_VIDEO_MULTIVIEW_MODE_TOP_BOTTOM:
626 par_d *= 2; /* half the width / double the height */
634 gst_video_multiview_mode_to_caps_string (GST_VIDEO_INFO_MULTIVIEW_MODE
636 if (caps_str != NULL) {
637 gst_caps_set_simple (caps, "multiview-mode", G_TYPE_STRING,
638 caps_str, "multiview-flags", GST_TYPE_VIDEO_MULTIVIEW_FLAGSET,
639 GST_VIDEO_INFO_MULTIVIEW_FLAGS (info), GST_FLAG_SET_MASK_EXACT, NULL);
643 gst_caps_set_simple (caps, "pixel-aspect-ratio",
644 GST_TYPE_FRACTION, par_n, par_d, NULL);
646 if (info->chroma_site != GST_VIDEO_CHROMA_SITE_UNKNOWN)
647 gst_caps_set_simple (caps, "chroma-site", G_TYPE_STRING,
648 gst_video_chroma_to_string (info->chroma_site), NULL);
650 /* make sure we set the RGB matrix for RGB formats */
651 colorimetry = info->colorimetry;
652 if (GST_VIDEO_FORMAT_INFO_IS_RGB (info->finfo) &&
653 colorimetry.matrix != GST_VIDEO_COLOR_MATRIX_RGB) {
654 GST_WARNING ("invalid matrix %d for RGB format, using RGB",
656 colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_RGB;
658 if ((color = gst_video_colorimetry_to_string (&colorimetry))) {
659 gst_caps_set_simple (caps, "colorimetry", G_TYPE_STRING, color, NULL);
664 gst_caps_set_simple (caps, "views", G_TYPE_INT, info->views, NULL);
666 if (info->flags & GST_VIDEO_FLAG_VARIABLE_FPS && info->fps_n != 0) {
667 /* variable fps with a max-framerate */
668 gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, 0, 1,
669 "max-framerate", GST_TYPE_FRACTION, info->fps_n, info->fps_d, NULL);
671 /* no variable fps or no max-framerate */
672 gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION,
673 info->fps_n, info->fps_d, NULL);
680 fill_planes (GstVideoInfo * info)
682 gsize width, height, cr_h;
685 width = (gsize) info->width;
686 height = (gsize) info->height;
688 /* Sanity check the resulting frame size for overflows */
689 for (i = 0; i < GST_VIDEO_INFO_N_COMPONENTS (info); i++)
690 bpp += GST_VIDEO_INFO_COMP_DEPTH (info, i);
691 bpp = GST_ROUND_UP_8 (bpp) / 8;
692 if (bpp > 0 && GST_ROUND_UP_128 ((guint64) width) * ((guint64) height) >=
694 GST_ERROR ("Frame size %ux%u would overflow", info->width, info->height);
698 switch (info->finfo->format) {
699 case GST_VIDEO_FORMAT_YUY2:
700 case GST_VIDEO_FORMAT_YVYU:
701 case GST_VIDEO_FORMAT_UYVY:
702 case GST_VIDEO_FORMAT_VYUY:
703 info->stride[0] = GST_ROUND_UP_4 (width * 2);
705 info->size = info->stride[0] * height;
707 case GST_VIDEO_FORMAT_AYUV:
708 case GST_VIDEO_FORMAT_RGBx:
709 case GST_VIDEO_FORMAT_RGBA:
710 case GST_VIDEO_FORMAT_BGRx:
711 case GST_VIDEO_FORMAT_BGRA:
712 case GST_VIDEO_FORMAT_xRGB:
713 case GST_VIDEO_FORMAT_ARGB:
714 case GST_VIDEO_FORMAT_xBGR:
715 case GST_VIDEO_FORMAT_ABGR:
716 case GST_VIDEO_FORMAT_r210:
717 info->stride[0] = width * 4;
719 info->size = info->stride[0] * height;
721 case GST_VIDEO_FORMAT_RGB16:
722 case GST_VIDEO_FORMAT_BGR16:
723 case GST_VIDEO_FORMAT_RGB15:
724 case GST_VIDEO_FORMAT_BGR15:
725 info->stride[0] = GST_ROUND_UP_4 (width * 2);
727 info->size = info->stride[0] * height;
729 case GST_VIDEO_FORMAT_RGB:
730 case GST_VIDEO_FORMAT_BGR:
731 case GST_VIDEO_FORMAT_v308:
732 case GST_VIDEO_FORMAT_IYU2:
733 info->stride[0] = GST_ROUND_UP_4 (width * 3);
735 info->size = info->stride[0] * height;
737 case GST_VIDEO_FORMAT_v210:
738 info->stride[0] = ((width + 47) / 48) * 128;
740 info->size = info->stride[0] * height;
742 case GST_VIDEO_FORMAT_v216:
743 info->stride[0] = GST_ROUND_UP_8 (width * 4);
745 info->size = info->stride[0] * height;
747 case GST_VIDEO_FORMAT_GRAY8:
748 info->stride[0] = GST_ROUND_UP_4 (width);
750 info->size = info->stride[0] * height;
752 case GST_VIDEO_FORMAT_GRAY16_BE:
753 case GST_VIDEO_FORMAT_GRAY16_LE:
754 info->stride[0] = GST_ROUND_UP_4 (width * 2);
756 info->size = info->stride[0] * height;
758 case GST_VIDEO_FORMAT_UYVP:
759 info->stride[0] = GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4);
761 info->size = info->stride[0] * height;
763 case GST_VIDEO_FORMAT_RGB8P:
764 info->stride[0] = GST_ROUND_UP_4 (width);
767 info->offset[1] = info->stride[0] * height;
768 info->size = info->offset[1] + (4 * 256);
770 case GST_VIDEO_FORMAT_IYU1:
771 info->stride[0] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) +
772 GST_ROUND_UP_4 (width) / 2);
774 info->size = info->stride[0] * height;
776 case GST_VIDEO_FORMAT_ARGB64:
777 case GST_VIDEO_FORMAT_AYUV64:
778 info->stride[0] = width * 8;
780 info->size = info->stride[0] * height;
782 case GST_VIDEO_FORMAT_I420:
783 case GST_VIDEO_FORMAT_YV12: /* same as I420, but plane 1+2 swapped */
784 info->stride[0] = GST_ROUND_UP_4 (width);
785 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
786 info->stride[2] = info->stride[1];
788 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
789 cr_h = GST_ROUND_UP_2 (height) / 2;
790 if (GST_VIDEO_INFO_IS_INTERLACED (info))
791 cr_h = GST_ROUND_UP_2 (cr_h);
792 info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
793 info->size = info->offset[2] + info->stride[2] * cr_h;
795 case GST_VIDEO_FORMAT_Y41B:
796 info->stride[0] = GST_ROUND_UP_4 (width);
797 info->stride[1] = GST_ROUND_UP_16 (width) / 4;
798 info->stride[2] = info->stride[1];
800 info->offset[1] = info->stride[0] * height;
801 info->offset[2] = info->offset[1] + info->stride[1] * height;
802 /* simplification of ROUNDUP4(w)*h + 2*((ROUNDUP16(w)/4)*h */
803 info->size = (info->stride[0] + (GST_ROUND_UP_16 (width) / 2)) * height;
805 case GST_VIDEO_FORMAT_Y42B:
806 info->stride[0] = GST_ROUND_UP_4 (width);
807 info->stride[1] = GST_ROUND_UP_8 (width) / 2;
808 info->stride[2] = info->stride[1];
810 info->offset[1] = info->stride[0] * height;
811 info->offset[2] = info->offset[1] + info->stride[1] * height;
812 /* simplification of ROUNDUP4(w)*h + 2*(ROUNDUP8(w)/2)*h */
813 info->size = (info->stride[0] + GST_ROUND_UP_8 (width)) * height;
815 case GST_VIDEO_FORMAT_Y444:
816 case GST_VIDEO_FORMAT_GBR:
817 info->stride[0] = GST_ROUND_UP_4 (width);
818 info->stride[1] = info->stride[0];
819 info->stride[2] = info->stride[0];
821 info->offset[1] = info->stride[0] * height;
822 info->offset[2] = info->offset[1] * 2;
823 info->size = info->stride[0] * height * 3;
825 case GST_VIDEO_FORMAT_GBRA:
826 info->stride[0] = GST_ROUND_UP_4 (width);
827 info->stride[1] = info->stride[0];
828 info->stride[2] = info->stride[0];
829 info->stride[3] = info->stride[0];
831 info->offset[1] = info->stride[0] * height;
832 info->offset[2] = info->offset[1] * 2;
833 info->offset[3] = info->offset[1] * 3;
834 info->size = info->stride[0] * height * 4;
836 case GST_VIDEO_FORMAT_NV12:
837 case GST_VIDEO_FORMAT_NV21:
838 info->stride[0] = GST_ROUND_UP_4 (width);
839 info->stride[1] = info->stride[0];
841 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
842 cr_h = GST_ROUND_UP_2 (height) / 2;
843 if (GST_VIDEO_INFO_IS_INTERLACED (info))
844 cr_h = GST_ROUND_UP_2 (cr_h);
845 info->size = info->offset[1] + info->stride[0] * cr_h;
847 case GST_VIDEO_FORMAT_NV16:
848 case GST_VIDEO_FORMAT_NV61:
849 info->stride[0] = GST_ROUND_UP_4 (width);
850 info->stride[1] = info->stride[0];
852 info->offset[1] = info->stride[0] * height;
853 info->size = info->stride[0] * height * 2;
855 case GST_VIDEO_FORMAT_NV24:
856 info->stride[0] = GST_ROUND_UP_4 (width);
857 info->stride[1] = GST_ROUND_UP_4 (width * 2);
859 info->offset[1] = info->stride[0] * height;
860 info->size = info->stride[0] * height + info->stride[1] * height;
862 case GST_VIDEO_FORMAT_A420:
863 info->stride[0] = GST_ROUND_UP_4 (width);
864 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
865 info->stride[2] = info->stride[1];
866 info->stride[3] = info->stride[0];
868 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
869 cr_h = GST_ROUND_UP_2 (height) / 2;
870 if (GST_VIDEO_INFO_IS_INTERLACED (info))
871 cr_h = GST_ROUND_UP_2 (cr_h);
872 info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
873 info->offset[3] = info->offset[2] + info->stride[2] * cr_h;
874 info->size = info->offset[3] + info->stride[0] * GST_ROUND_UP_2 (height);
876 case GST_VIDEO_FORMAT_YUV9:
877 case GST_VIDEO_FORMAT_YVU9:
878 info->stride[0] = GST_ROUND_UP_4 (width);
879 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
880 info->stride[2] = info->stride[1];
882 info->offset[1] = info->stride[0] * height;
883 cr_h = GST_ROUND_UP_4 (height) / 4;
884 if (GST_VIDEO_INFO_IS_INTERLACED (info))
885 cr_h = GST_ROUND_UP_2 (cr_h);
886 info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
887 info->size = info->offset[2] + info->stride[2] * cr_h;
889 case GST_VIDEO_FORMAT_I420_10LE:
890 case GST_VIDEO_FORMAT_I420_10BE:
891 case GST_VIDEO_FORMAT_I420_12LE:
892 case GST_VIDEO_FORMAT_I420_12BE:
893 info->stride[0] = GST_ROUND_UP_4 (width * 2);
894 info->stride[1] = GST_ROUND_UP_4 (width);
895 info->stride[2] = info->stride[1];
897 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
898 cr_h = GST_ROUND_UP_2 (height) / 2;
899 if (GST_VIDEO_INFO_IS_INTERLACED (info))
900 cr_h = GST_ROUND_UP_2 (cr_h);
901 info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
902 info->size = info->offset[2] + info->stride[2] * cr_h;
904 case GST_VIDEO_FORMAT_I422_10LE:
905 case GST_VIDEO_FORMAT_I422_10BE:
906 case GST_VIDEO_FORMAT_I422_12LE:
907 case GST_VIDEO_FORMAT_I422_12BE:
908 info->stride[0] = GST_ROUND_UP_4 (width * 2);
909 info->stride[1] = GST_ROUND_UP_4 (width);
910 info->stride[2] = info->stride[1];
912 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
913 info->offset[2] = info->offset[1] +
914 info->stride[1] * GST_ROUND_UP_2 (height);
915 info->size = info->offset[2] + info->stride[2] * GST_ROUND_UP_2 (height);
917 case GST_VIDEO_FORMAT_Y444_10LE:
918 case GST_VIDEO_FORMAT_Y444_10BE:
919 case GST_VIDEO_FORMAT_Y444_12LE:
920 case GST_VIDEO_FORMAT_Y444_12BE:
921 case GST_VIDEO_FORMAT_GBR_10LE:
922 case GST_VIDEO_FORMAT_GBR_10BE:
923 case GST_VIDEO_FORMAT_GBR_12LE:
924 case GST_VIDEO_FORMAT_GBR_12BE:
925 info->stride[0] = GST_ROUND_UP_4 (width * 2);
926 info->stride[1] = info->stride[0];
927 info->stride[2] = info->stride[0];
929 info->offset[1] = info->stride[0] * height;
930 info->offset[2] = info->offset[1] * 2;
931 info->size = info->stride[0] * height * 3;
933 case GST_VIDEO_FORMAT_GBRA_10LE:
934 case GST_VIDEO_FORMAT_GBRA_10BE:
935 case GST_VIDEO_FORMAT_GBRA_12LE:
936 case GST_VIDEO_FORMAT_GBRA_12BE:
937 info->stride[0] = GST_ROUND_UP_4 (width * 2);
938 info->stride[1] = info->stride[0];
939 info->stride[2] = info->stride[0];
940 info->stride[3] = info->stride[0];
942 info->offset[1] = info->stride[0] * height;
943 info->offset[2] = info->offset[1] * 2;
944 info->offset[3] = info->offset[1] * 3;
945 info->size = info->stride[0] * height * 4;
947 case GST_VIDEO_FORMAT_NV12_64Z32:
949 GST_VIDEO_TILE_MAKE_STRIDE (GST_ROUND_UP_128 (width) / 64,
950 GST_ROUND_UP_32 (height) / 32);
952 GST_VIDEO_TILE_MAKE_STRIDE (GST_ROUND_UP_128 (width) / 64,
953 GST_ROUND_UP_64 (height) / 64);
955 info->offset[1] = GST_ROUND_UP_128 (width) * GST_ROUND_UP_32 (height);
956 info->size = info->offset[1] +
957 GST_ROUND_UP_128 (width) * GST_ROUND_UP_64 (height) / 2;
959 case GST_VIDEO_FORMAT_A420_10LE:
960 case GST_VIDEO_FORMAT_A420_10BE:
961 info->stride[0] = GST_ROUND_UP_4 (width * 2);
962 info->stride[1] = GST_ROUND_UP_4 (width);
963 info->stride[2] = info->stride[1];
964 info->stride[3] = info->stride[0];
966 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
967 cr_h = GST_ROUND_UP_2 (height) / 2;
968 if (GST_VIDEO_INFO_IS_INTERLACED (info))
969 cr_h = GST_ROUND_UP_2 (cr_h);
970 info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
971 info->offset[3] = info->offset[2] + info->stride[2] * cr_h;
972 info->size = info->offset[3] + info->stride[0] * GST_ROUND_UP_2 (height);
974 case GST_VIDEO_FORMAT_A422_10LE:
975 case GST_VIDEO_FORMAT_A422_10BE:
976 info->stride[0] = GST_ROUND_UP_4 (width * 2);
977 info->stride[1] = GST_ROUND_UP_4 (width);
978 info->stride[2] = info->stride[1];
979 info->stride[3] = info->stride[0];
981 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
982 info->offset[2] = info->offset[1] +
983 info->stride[1] * GST_ROUND_UP_2 (height);
985 info->offset[2] + info->stride[2] * GST_ROUND_UP_2 (height);
986 info->size = info->offset[3] + info->stride[0] * GST_ROUND_UP_2 (height);
988 case GST_VIDEO_FORMAT_A444_10LE:
989 case GST_VIDEO_FORMAT_A444_10BE:
990 info->stride[0] = GST_ROUND_UP_4 (width * 2);
991 info->stride[1] = info->stride[0];
992 info->stride[2] = info->stride[0];
993 info->stride[3] = info->stride[0];
995 info->offset[1] = info->stride[0] * height;
996 info->offset[2] = info->offset[1] * 2;
997 info->offset[3] = info->offset[1] * 3;
998 info->size = info->stride[0] * height * 4;
1000 case GST_VIDEO_FORMAT_P010_10LE:
1001 case GST_VIDEO_FORMAT_P010_10BE:
1002 info->stride[0] = GST_ROUND_UP_4 (width * 2);
1003 info->stride[1] = info->stride[0];
1004 info->offset[0] = 0;
1005 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1006 cr_h = GST_ROUND_UP_2 (height) / 2;
1007 info->size = info->offset[1] + info->stride[0] * cr_h;
1009 case GST_VIDEO_FORMAT_GRAY10_LE32:
1010 info->stride[0] = (width + 2) / 3 * 4;
1011 info->offset[0] = 0;
1012 info->size = info->stride[0] * GST_ROUND_UP_2 (height);
1014 case GST_VIDEO_FORMAT_NV12_10LE32:
1015 info->stride[0] = (width + 2) / 3 * 4;
1016 info->stride[1] = info->stride[0];
1017 info->offset[0] = 0;
1018 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1019 cr_h = GST_ROUND_UP_2 (height) / 2;
1020 if (GST_VIDEO_INFO_IS_INTERLACED (info))
1021 cr_h = GST_ROUND_UP_2 (cr_h);
1022 info->size = info->offset[1] + info->stride[0] * cr_h;
1024 case GST_VIDEO_FORMAT_NV16_10LE32:
1025 info->stride[0] = (width + 2) / 3 * 4;
1026 info->stride[1] = info->stride[0];
1027 info->offset[0] = 0;
1028 info->offset[1] = info->stride[0] * height;
1029 info->size = info->stride[0] * height * 2;
1032 case GST_VIDEO_FORMAT_ENCODED:
1034 case GST_VIDEO_FORMAT_UNKNOWN:
1035 GST_ERROR ("invalid format");
1036 g_warning ("invalid format");
1044 * gst_video_info_convert:
1045 * @info: a #GstVideoInfo
1046 * @src_format: #GstFormat of the @src_value
1047 * @src_value: value to convert
1048 * @dest_format: #GstFormat of the @dest_value
1049 * @dest_value: (out): pointer to destination value
1051 * Converts among various #GstFormat types. This function handles
1052 * GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT. For
1053 * raw video, GST_FORMAT_DEFAULT corresponds to video frames. This
1054 * function can be used to handle pad queries of the type GST_QUERY_CONVERT.
1056 * Returns: TRUE if the conversion was successful.
1059 gst_video_info_convert (GstVideoInfo * info,
1060 GstFormat src_format, gint64 src_value,
1061 GstFormat dest_format, gint64 * dest_value)
1063 gboolean ret = FALSE;
1067 g_return_val_if_fail (info != NULL, 0);
1068 g_return_val_if_fail (info->finfo != NULL, 0);
1069 g_return_val_if_fail (info->finfo->format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1070 g_return_val_if_fail (info->size > 0, 0);
1073 fps_n = info->fps_n;
1074 fps_d = info->fps_d;
1076 GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s to %s",
1077 src_value, gst_format_get_name (src_format),
1078 gst_format_get_name (dest_format));
1080 if (src_format == dest_format) {
1081 *dest_value = src_value;
1086 if (src_value == -1) {
1092 /* bytes to frames */
1093 if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_DEFAULT) {
1095 *dest_value = gst_util_uint64_scale (src_value, 1, size);
1097 GST_ERROR ("blocksize is 0");
1104 /* frames to bytes */
1105 if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_BYTES) {
1106 *dest_value = gst_util_uint64_scale (src_value, size, 1);
1111 /* time to frames */
1112 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_DEFAULT) {
1114 *dest_value = gst_util_uint64_scale (src_value,
1115 fps_n, GST_SECOND * fps_d);
1117 GST_ERROR ("framerate denominator is 0");
1124 /* frames to time */
1125 if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) {
1127 *dest_value = gst_util_uint64_scale (src_value,
1128 GST_SECOND * fps_d, fps_n);
1130 GST_ERROR ("framerate numerator is 0");
1138 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
1140 *dest_value = gst_util_uint64_scale (src_value,
1141 fps_n * size, GST_SECOND * fps_d);
1143 GST_ERROR ("framerate denominator is 0");
1151 if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
1152 if (fps_n != 0 && size != 0) {
1153 *dest_value = gst_util_uint64_scale (src_value,
1154 GST_SECOND * fps_d, fps_n * size);
1156 GST_ERROR ("framerate denominator and/or blocksize is 0");
1164 GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, ret, *dest_value);
1170 * gst_video_info_align:
1171 * @info: a #GstVideoInfo
1172 * @align: alignment parameters
1174 * Adjust the offset and stride fields in @info so that the padding and
1175 * stride alignment in @align is respected.
1177 * Extra padding will be added to the right side when stride alignment padding
1178 * is required and @align will be updated with the new padding values.
1180 * Returns: %FALSE if alignment could not be applied, e.g. because the
1181 * size of a frame can't be represented as a 32 bit integer (Since: 1.12)
1184 gst_video_info_align (GstVideoInfo * info, GstVideoAlignment * align)
1186 const GstVideoFormatInfo *vinfo = info->finfo;
1188 gint padded_width, padded_height;
1192 width = GST_VIDEO_INFO_WIDTH (info);
1193 height = GST_VIDEO_INFO_HEIGHT (info);
1195 GST_LOG ("padding %u-%ux%u-%u", align->padding_top,
1196 align->padding_left, align->padding_right, align->padding_bottom);
1198 n_planes = GST_VIDEO_INFO_N_PLANES (info);
1200 if (GST_VIDEO_FORMAT_INFO_HAS_PALETTE (vinfo))
1203 /* first make sure the left padding does not cause alignment problems later */
1205 GST_LOG ("left padding %u", align->padding_left);
1207 for (i = 0; i < n_planes; i++) {
1210 /* this is the amout of pixels to add as left padding */
1211 hedge = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (vinfo, i, align->padding_left);
1212 hedge *= GST_VIDEO_FORMAT_INFO_PSTRIDE (vinfo, i);
1214 GST_LOG ("plane %d, padding %d, alignment %u", i, hedge,
1215 align->stride_align[i]);
1216 aligned &= (hedge & align->stride_align[i]) == 0;
1221 GST_LOG ("unaligned padding, increasing padding");
1222 /* increase padded_width */
1223 align->padding_left += align->padding_left & ~(align->padding_left - 1);
1226 /* add the padding */
1227 padded_width = width + align->padding_left + align->padding_right;
1228 padded_height = height + align->padding_top + align->padding_bottom;
1231 GST_LOG ("padded dimension %u-%u", padded_width, padded_height);
1233 info->width = padded_width;
1234 info->height = padded_height;
1236 if (!fill_planes (info))
1239 /* check alignment */
1241 for (i = 0; i < n_planes; i++) {
1242 GST_LOG ("plane %d, stride %d, alignment %u", i, info->stride[i],
1243 align->stride_align[i]);
1244 aligned &= (info->stride[i] & align->stride_align[i]) == 0;
1249 GST_LOG ("unaligned strides, increasing dimension");
1250 /* increase padded_width */
1251 padded_width += padded_width & ~(padded_width - 1);
1254 align->padding_right = padded_width - width - align->padding_left;
1256 info->width = width;
1257 info->height = height;
1259 for (i = 0; i < n_planes; i++) {
1260 gint vedge, hedge, comp;
1262 /* Find the component for this plane, FIXME, we assume the plane number and
1263 * component number is the same for now, for scaling the dimensions this is
1264 * currently true for all formats but it might not be when adding new
1265 * formats. We might need to add a plane subsamling in the format info to
1266 * make this more generic or maybe use a plane -> component mapping. */
1270 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (vinfo, comp, align->padding_left);
1272 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vinfo, comp, align->padding_top);
1274 GST_DEBUG ("plane %d: comp: %d, hedge %d vedge %d align %d stride %d", i,
1275 comp, hedge, vedge, align->stride_align[i], info->stride[i]);
1277 info->offset[i] += (vedge * info->stride[i]) +
1278 (hedge * GST_VIDEO_FORMAT_INFO_PSTRIDE (vinfo, comp));