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_NV12:
826 case GST_VIDEO_FORMAT_NV21:
827 info->stride[0] = GST_ROUND_UP_4 (width);
828 info->stride[1] = info->stride[0];
830 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
831 cr_h = GST_ROUND_UP_2 (height) / 2;
832 if (GST_VIDEO_INFO_IS_INTERLACED (info))
833 cr_h = GST_ROUND_UP_2 (cr_h);
834 info->size = info->offset[1] + info->stride[0] * cr_h;
836 case GST_VIDEO_FORMAT_NV16:
837 case GST_VIDEO_FORMAT_NV61:
838 info->stride[0] = GST_ROUND_UP_4 (width);
839 info->stride[1] = info->stride[0];
841 info->offset[1] = info->stride[0] * height;
842 info->size = info->stride[0] * height * 2;
844 case GST_VIDEO_FORMAT_NV24:
845 info->stride[0] = GST_ROUND_UP_4 (width);
846 info->stride[1] = GST_ROUND_UP_4 (width * 2);
848 info->offset[1] = info->stride[0] * height;
849 info->size = info->stride[0] * height + info->stride[1] * height;
851 case GST_VIDEO_FORMAT_A420:
852 info->stride[0] = GST_ROUND_UP_4 (width);
853 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
854 info->stride[2] = info->stride[1];
855 info->stride[3] = info->stride[0];
857 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
858 cr_h = GST_ROUND_UP_2 (height) / 2;
859 if (GST_VIDEO_INFO_IS_INTERLACED (info))
860 cr_h = GST_ROUND_UP_2 (cr_h);
861 info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
862 info->offset[3] = info->offset[2] + info->stride[2] * cr_h;
863 info->size = info->offset[3] + info->stride[0] * GST_ROUND_UP_2 (height);
865 case GST_VIDEO_FORMAT_YUV9:
866 case GST_VIDEO_FORMAT_YVU9:
867 info->stride[0] = GST_ROUND_UP_4 (width);
868 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
869 info->stride[2] = info->stride[1];
871 info->offset[1] = info->stride[0] * height;
872 cr_h = GST_ROUND_UP_4 (height) / 4;
873 if (GST_VIDEO_INFO_IS_INTERLACED (info))
874 cr_h = GST_ROUND_UP_2 (cr_h);
875 info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
876 info->size = info->offset[2] + info->stride[2] * cr_h;
878 case GST_VIDEO_FORMAT_I420_10LE:
879 case GST_VIDEO_FORMAT_I420_10BE:
880 info->stride[0] = GST_ROUND_UP_4 (width * 2);
881 info->stride[1] = GST_ROUND_UP_4 (width);
882 info->stride[2] = info->stride[1];
884 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
885 cr_h = GST_ROUND_UP_2 (height) / 2;
886 if (GST_VIDEO_INFO_IS_INTERLACED (info))
887 cr_h = GST_ROUND_UP_2 (cr_h);
888 info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
889 info->size = info->offset[2] + info->stride[2] * cr_h;
891 case GST_VIDEO_FORMAT_I422_10LE:
892 case GST_VIDEO_FORMAT_I422_10BE:
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 info->offset[2] = info->offset[1] +
899 info->stride[1] * GST_ROUND_UP_2 (height);
900 info->size = info->offset[2] + info->stride[2] * GST_ROUND_UP_2 (height);
902 case GST_VIDEO_FORMAT_Y444_10LE:
903 case GST_VIDEO_FORMAT_Y444_10BE:
904 case GST_VIDEO_FORMAT_GBR_10LE:
905 case GST_VIDEO_FORMAT_GBR_10BE:
906 case GST_VIDEO_FORMAT_GBR_12LE:
907 case GST_VIDEO_FORMAT_GBR_12BE:
908 info->stride[0] = GST_ROUND_UP_4 (width * 2);
909 info->stride[1] = info->stride[0];
910 info->stride[2] = info->stride[0];
912 info->offset[1] = info->stride[0] * height;
913 info->offset[2] = info->offset[1] * 2;
914 info->size = info->stride[0] * height * 3;
916 case GST_VIDEO_FORMAT_GBRA_10LE:
917 case GST_VIDEO_FORMAT_GBRA_10BE:
918 case GST_VIDEO_FORMAT_GBRA_12LE:
919 case GST_VIDEO_FORMAT_GBRA_12BE:
920 info->stride[0] = GST_ROUND_UP_4 (width * 2);
921 info->stride[1] = info->stride[0];
922 info->stride[2] = info->stride[0];
923 info->stride[3] = info->stride[0];
925 info->offset[1] = info->stride[0] * height;
926 info->offset[2] = info->offset[1] * 2;
927 info->offset[3] = info->offset[1] * 3;
928 info->size = info->stride[0] * height * 4;
930 case GST_VIDEO_FORMAT_NV12_64Z32:
932 GST_VIDEO_TILE_MAKE_STRIDE (GST_ROUND_UP_128 (width) / 64,
933 GST_ROUND_UP_32 (height) / 32);
935 GST_VIDEO_TILE_MAKE_STRIDE (GST_ROUND_UP_128 (width) / 64,
936 GST_ROUND_UP_64 (height) / 64);
938 info->offset[1] = GST_ROUND_UP_128 (width) * GST_ROUND_UP_32 (height);
939 info->size = info->offset[1] +
940 GST_ROUND_UP_128 (width) * GST_ROUND_UP_64 (height) / 2;
942 case GST_VIDEO_FORMAT_A420_10LE:
943 case GST_VIDEO_FORMAT_A420_10BE:
944 info->stride[0] = GST_ROUND_UP_4 (width * 2);
945 info->stride[1] = GST_ROUND_UP_4 (width);
946 info->stride[2] = info->stride[1];
947 info->stride[3] = info->stride[0];
949 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
950 cr_h = GST_ROUND_UP_2 (height) / 2;
951 if (GST_VIDEO_INFO_IS_INTERLACED (info))
952 cr_h = GST_ROUND_UP_2 (cr_h);
953 info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
954 info->offset[3] = info->offset[2] + info->stride[2] * cr_h;
955 info->size = info->offset[3] + info->stride[0] * GST_ROUND_UP_2 (height);
957 case GST_VIDEO_FORMAT_A422_10LE:
958 case GST_VIDEO_FORMAT_A422_10BE:
959 info->stride[0] = GST_ROUND_UP_4 (width * 2);
960 info->stride[1] = GST_ROUND_UP_4 (width);
961 info->stride[2] = info->stride[1];
962 info->stride[3] = info->stride[0];
964 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
965 info->offset[2] = info->offset[1] +
966 info->stride[1] * GST_ROUND_UP_2 (height);
968 info->offset[2] + info->stride[2] * GST_ROUND_UP_2 (height);
969 info->size = info->offset[3] + info->stride[0] * GST_ROUND_UP_2 (height);
971 case GST_VIDEO_FORMAT_A444_10LE:
972 case GST_VIDEO_FORMAT_A444_10BE:
973 info->stride[0] = GST_ROUND_UP_4 (width * 2);
974 info->stride[1] = info->stride[0];
975 info->stride[2] = info->stride[0];
976 info->stride[3] = info->stride[0];
978 info->offset[1] = info->stride[0] * height;
979 info->offset[2] = info->offset[1] * 2;
980 info->offset[3] = info->offset[1] * 3;
981 info->size = info->stride[0] * height * 4;
983 case GST_VIDEO_FORMAT_P010_10LE:
984 case GST_VIDEO_FORMAT_P010_10BE:
985 info->stride[0] = GST_ROUND_UP_4 (width * 2);
986 info->stride[1] = info->stride[0];
988 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
989 cr_h = GST_ROUND_UP_2 (height) / 2;
990 info->size = info->offset[1] + info->stride[0] * cr_h;
993 case GST_VIDEO_FORMAT_ENCODED:
995 case GST_VIDEO_FORMAT_UNKNOWN:
996 GST_ERROR ("invalid format");
997 g_warning ("invalid format");
1005 * gst_video_info_convert:
1006 * @info: a #GstVideoInfo
1007 * @src_format: #GstFormat of the @src_value
1008 * @src_value: value to convert
1009 * @dest_format: #GstFormat of the @dest_value
1010 * @dest_value: pointer to destination value
1012 * Converts among various #GstFormat types. This function handles
1013 * GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT. For
1014 * raw video, GST_FORMAT_DEFAULT corresponds to video frames. This
1015 * function can be used to handle pad queries of the type GST_QUERY_CONVERT.
1017 * Returns: TRUE if the conversion was successful.
1020 gst_video_info_convert (GstVideoInfo * info,
1021 GstFormat src_format, gint64 src_value,
1022 GstFormat dest_format, gint64 * dest_value)
1024 gboolean ret = FALSE;
1028 g_return_val_if_fail (info != NULL, 0);
1029 g_return_val_if_fail (info->finfo != NULL, 0);
1030 g_return_val_if_fail (info->finfo->format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1031 g_return_val_if_fail (info->size > 0, 0);
1034 fps_n = info->fps_n;
1035 fps_d = info->fps_d;
1037 GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s to %s",
1038 src_value, gst_format_get_name (src_format),
1039 gst_format_get_name (dest_format));
1041 if (src_format == dest_format) {
1042 *dest_value = src_value;
1047 if (src_value == -1) {
1053 /* bytes to frames */
1054 if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_DEFAULT) {
1056 *dest_value = gst_util_uint64_scale (src_value, 1, size);
1058 GST_ERROR ("blocksize is 0");
1065 /* frames to bytes */
1066 if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_BYTES) {
1067 *dest_value = gst_util_uint64_scale (src_value, size, 1);
1072 /* time to frames */
1073 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_DEFAULT) {
1075 *dest_value = gst_util_uint64_scale (src_value,
1076 fps_n, GST_SECOND * fps_d);
1078 GST_ERROR ("framerate denominator is 0");
1085 /* frames to time */
1086 if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) {
1088 *dest_value = gst_util_uint64_scale (src_value,
1089 GST_SECOND * fps_d, fps_n);
1091 GST_ERROR ("framerate numerator is 0");
1099 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
1101 *dest_value = gst_util_uint64_scale (src_value,
1102 fps_n * size, GST_SECOND * fps_d);
1104 GST_ERROR ("framerate denominator is 0");
1112 if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
1113 if (fps_n != 0 && size != 0) {
1114 *dest_value = gst_util_uint64_scale (src_value,
1115 GST_SECOND * fps_d, fps_n * size);
1117 GST_ERROR ("framerate denominator and/or blocksize is 0");
1125 GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, ret, *dest_value);
1131 * gst_video_info_align:
1132 * @info: a #GstVideoInfo
1133 * @align: alignment parameters
1135 * Adjust the offset and stride fields in @info so that the padding and
1136 * stride alignment in @align is respected.
1138 * Extra padding will be added to the right side when stride alignment padding
1139 * is required and @align will be updated with the new padding values.
1141 * Returns: %FALSE if alignment could not be applied, e.g. because the
1142 * size of a frame can't be represented as a 32 bit integer (Since: 1.12)
1145 gst_video_info_align (GstVideoInfo * info, GstVideoAlignment * align)
1147 const GstVideoFormatInfo *vinfo = info->finfo;
1149 gint padded_width, padded_height;
1153 width = GST_VIDEO_INFO_WIDTH (info);
1154 height = GST_VIDEO_INFO_HEIGHT (info);
1156 GST_LOG ("padding %u-%ux%u-%u", align->padding_top,
1157 align->padding_left, align->padding_right, align->padding_bottom);
1159 n_planes = GST_VIDEO_INFO_N_PLANES (info);
1161 if (GST_VIDEO_FORMAT_INFO_HAS_PALETTE (vinfo))
1164 /* first make sure the left padding does not cause alignment problems later */
1166 GST_LOG ("left padding %u", align->padding_left);
1168 for (i = 0; i < n_planes; i++) {
1171 /* this is the amout of pixels to add as left padding */
1172 hedge = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (vinfo, i, align->padding_left);
1173 hedge *= GST_VIDEO_FORMAT_INFO_PSTRIDE (vinfo, i);
1175 GST_LOG ("plane %d, padding %d, alignment %u", i, hedge,
1176 align->stride_align[i]);
1177 aligned &= (hedge & align->stride_align[i]) == 0;
1182 GST_LOG ("unaligned padding, increasing padding");
1183 /* increase padded_width */
1184 align->padding_left += align->padding_left & ~(align->padding_left - 1);
1187 /* add the padding */
1188 padded_width = width + align->padding_left + align->padding_right;
1189 padded_height = height + align->padding_top + align->padding_bottom;
1192 GST_LOG ("padded dimension %u-%u", padded_width, padded_height);
1194 info->width = padded_width;
1195 info->height = padded_height;
1197 if (!fill_planes (info))
1200 /* check alignment */
1202 for (i = 0; i < n_planes; i++) {
1203 GST_LOG ("plane %d, stride %d, alignment %u", i, info->stride[i],
1204 align->stride_align[i]);
1205 aligned &= (info->stride[i] & align->stride_align[i]) == 0;
1210 GST_LOG ("unaligned strides, increasing dimension");
1211 /* increase padded_width */
1212 padded_width += padded_width & ~(padded_width - 1);
1215 align->padding_right = padded_width - width - align->padding_left;
1217 info->width = width;
1218 info->height = height;
1220 for (i = 0; i < n_planes; i++) {
1221 gint vedge, hedge, comp;
1223 /* Find the component for this plane, FIXME, we assume the plane number and
1224 * component number is the same for now, for scaling the dimensions this is
1225 * currently true for all formats but it might not be when adding new
1226 * formats. We might need to add a plane subsamling in the format info to
1227 * make this more generic or maybe use a plane -> component mapping. */
1231 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (vinfo, comp, align->padding_left);
1233 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vinfo, comp, align->padding_top);
1235 GST_DEBUG ("plane %d: comp: %d, hedge %d vedge %d align %d stride %d", i,
1236 comp, hedge, vedge, align->stride_align[i], info->stride[i]);
1238 info->offset[i] += (vedge * info->stride[i]) +
1239 (hedge * GST_VIDEO_FORMAT_INFO_PSTRIDE (vinfo, comp));