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;
135 #define MAKE_COLORIMETRY(r,m,t,p) { \
136 GST_VIDEO_COLOR_RANGE ##r, GST_VIDEO_COLOR_MATRIX_ ##m, \
137 GST_VIDEO_TRANSFER_ ##t, GST_VIDEO_COLOR_PRIMARIES_ ##p }
139 #define DEFAULT_YUV_SD 0
140 #define DEFAULT_YUV_HD 1
141 #define DEFAULT_RGB 2
142 #define DEFAULT_GRAY 3
143 #define DEFAULT_UNKNOWN 4
144 #define DEFAULT_YUV_UHD 5
146 static const GstVideoColorimetry default_color[] = {
147 MAKE_COLORIMETRY (_16_235, BT601, BT709, SMPTE170M),
148 MAKE_COLORIMETRY (_16_235, BT709, BT709, BT709),
149 MAKE_COLORIMETRY (_0_255, RGB, SRGB, BT709),
150 MAKE_COLORIMETRY (_0_255, BT601, UNKNOWN, UNKNOWN),
151 MAKE_COLORIMETRY (_UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN),
152 MAKE_COLORIMETRY (_16_235, BT2020, BT2020_12, BT2020),
156 set_default_colorimetry (GstVideoInfo * info)
158 const GstVideoFormatInfo *finfo = info->finfo;
160 if (GST_VIDEO_FORMAT_INFO_IS_YUV (finfo)) {
161 if (info->height >= 2160) {
162 info->chroma_site = GST_VIDEO_CHROMA_SITE_H_COSITED;
163 info->colorimetry = default_color[DEFAULT_YUV_UHD];
164 } else if (info->height > 576) {
165 info->chroma_site = GST_VIDEO_CHROMA_SITE_H_COSITED;
166 info->colorimetry = default_color[DEFAULT_YUV_HD];
168 info->chroma_site = GST_VIDEO_CHROMA_SITE_NONE;
169 info->colorimetry = default_color[DEFAULT_YUV_SD];
171 } else if (GST_VIDEO_FORMAT_INFO_IS_GRAY (finfo)) {
172 info->colorimetry = default_color[DEFAULT_GRAY];
173 } else if (GST_VIDEO_FORMAT_INFO_IS_RGB (finfo)) {
174 info->colorimetry = default_color[DEFAULT_RGB];
176 info->colorimetry = default_color[DEFAULT_UNKNOWN];
181 validate_colorimetry (GstVideoInfo * info)
183 const GstVideoFormatInfo *finfo = info->finfo;
185 if (!GST_VIDEO_FORMAT_INFO_IS_RGB (finfo) &&
186 info->colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_RGB)
189 if (GST_VIDEO_FORMAT_INFO_IS_YUV (finfo) &&
190 info->colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_UNKNOWN)
197 * gst_video_info_set_format:
198 * @info: a #GstVideoInfo
199 * @format: the format
203 * Set the default info for a video frame of @format and @width and @height.
205 * Note: This initializes @info first, no values are preserved. This function
206 * does not set the offsets correctly for interlaced vertically
207 * subsampled formats.
209 * Returns: %FALSE if the returned video info is invalid, e.g. because the
210 * size of a frame can't be represented as a 32 bit integer (Since: 1.12)
213 gst_video_info_set_format (GstVideoInfo * info, GstVideoFormat format,
214 guint width, guint height)
216 g_return_val_if_fail (info != NULL, FALSE);
217 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, FALSE);
219 gst_video_info_init (info);
221 info->finfo = gst_video_format_get_info (format);
223 info->height = height;
226 set_default_colorimetry (info);
228 return fill_planes (info);
231 static const gchar *interlace_mode[] = {
239 * gst_video_interlace_mode_to_string:
240 * @mode: a #GstVideoInterlaceMode
242 * Convert @mode to its string representation.
244 * Returns: @mode as a string or NULL if @mode in invalid.
249 gst_video_interlace_mode_to_string (GstVideoInterlaceMode mode)
251 if (((guint) mode) >= G_N_ELEMENTS (interlace_mode))
254 return interlace_mode[mode];
258 * gst_video_interlace_mode_from_string:
261 * Convert @mode to a #GstVideoInterlaceMode
263 * Returns: the #GstVideoInterlaceMode of @mode or
264 * #GST_VIDEO_INTERLACE_MODE_PROGRESSIVE when @mode is not a valid
265 * string representation for a #GstVideoInterlaceMode.
269 GstVideoInterlaceMode
270 gst_video_interlace_mode_from_string (const gchar * mode)
273 for (i = 0; i < G_N_ELEMENTS (interlace_mode); i++) {
274 if (g_str_equal (interlace_mode[i], mode))
277 return GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
280 static const gchar *field_order[] = {
287 * gst_video_field_order_to_string:
288 * @order: a #GstVideoFieldOrder
290 * Convert @order to its string representation.
292 * Returns: @order as a string or NULL if @order in invalid.
297 gst_video_field_order_to_string (GstVideoFieldOrder order)
299 if (((guint) order) >= G_N_ELEMENTS (field_order))
302 return field_order[order];
306 * gst_video_field_order_from_string:
307 * @order: a field order
309 * Convert @order to a #GstVideoFieldOrder
311 * Returns: the #GstVideoFieldOrder of @order or
312 * #GST_VIDEO_FIELD_ORDER_UNKNOWN when @order is not a valid
313 * string representation for a #GstVideoFieldOrder.
318 gst_video_field_order_from_string (const gchar * order)
321 for (i = 0; i < G_N_ELEMENTS (field_order); i++) {
322 if (g_str_equal (field_order[i], order))
325 return GST_VIDEO_FIELD_ORDER_UNKNOWN;
329 * gst_video_info_from_caps:
330 * @info: a #GstVideoInfo
333 * Parse @caps and update @info.
335 * Returns: TRUE if @caps could be parsed
338 gst_video_info_from_caps (GstVideoInfo * info, const GstCaps * caps)
340 GstStructure *structure;
342 GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
343 gint width = 0, height = 0;
346 guint multiview_flags;
348 g_return_val_if_fail (info != NULL, FALSE);
349 g_return_val_if_fail (caps != NULL, FALSE);
350 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
352 GST_DEBUG ("parsing caps %" GST_PTR_FORMAT, caps);
354 structure = gst_caps_get_structure (caps, 0);
356 if (gst_structure_has_name (structure, "video/x-raw")) {
357 if (!(s = gst_structure_get_string (structure, "format")))
360 format = gst_video_format_from_string (s);
361 if (format == GST_VIDEO_FORMAT_UNKNOWN)
364 } else if (g_str_has_prefix (gst_structure_get_name (structure), "video/") ||
365 g_str_has_prefix (gst_structure_get_name (structure), "image/")) {
366 format = GST_VIDEO_FORMAT_ENCODED;
371 /* width and height are mandatory, except for non-raw-formats */
372 if (!gst_structure_get_int (structure, "width", &width) &&
373 format != GST_VIDEO_FORMAT_ENCODED)
375 if (!gst_structure_get_int (structure, "height", &height) &&
376 format != GST_VIDEO_FORMAT_ENCODED)
379 gst_video_info_init (info);
381 info->finfo = gst_video_format_get_info (format);
383 info->height = height;
385 if (gst_structure_get_fraction (structure, "framerate", &fps_n, &fps_d)) {
387 /* variable framerate */
388 info->flags |= GST_VIDEO_FLAG_VARIABLE_FPS;
389 /* see if we have a max-framerate */
390 gst_structure_get_fraction (structure, "max-framerate", &fps_n, &fps_d);
395 /* unspecified is variable framerate */
400 if (gst_structure_get_fraction (structure, "pixel-aspect-ratio",
409 if ((s = gst_structure_get_string (structure, "interlace-mode")))
410 info->interlace_mode = gst_video_interlace_mode_from_string (s);
412 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
414 if (info->interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED &&
415 (s = gst_structure_get_string (structure, "field-order"))) {
416 GST_VIDEO_INFO_FIELD_ORDER (info) = gst_video_field_order_from_string (s);
418 GST_VIDEO_INFO_FIELD_ORDER (info) = GST_VIDEO_FIELD_ORDER_UNKNOWN;
422 if ((s = gst_structure_get_string (structure, "multiview-mode")))
423 GST_VIDEO_INFO_MULTIVIEW_MODE (info) =
424 gst_video_multiview_mode_from_caps_string (s);
426 GST_VIDEO_INFO_MULTIVIEW_MODE (info) = GST_VIDEO_MULTIVIEW_MODE_NONE;
428 gst_structure_get_flagset (structure, "multiview-flags", &multiview_flags,
430 GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) = multiview_flags;
432 if (!gst_structure_get_int (structure, "views", &info->views))
435 /* At one point, I tried normalising the half-aspect flag here,
436 * but it behaves weird for GstVideoInfo operations other than
437 * directly converting to/from caps - sometimes causing the
438 * PAR to be doubled/halved too many times */
441 if ((s = gst_structure_get_string (structure, "chroma-site")))
442 info->chroma_site = gst_video_chroma_from_string (s);
444 info->chroma_site = GST_VIDEO_CHROMA_SITE_UNKNOWN;
446 if ((s = gst_structure_get_string (structure, "colorimetry"))) {
447 if (!gst_video_colorimetry_from_string (&info->colorimetry, s)) {
448 GST_WARNING ("unparsable colorimetry, using default");
449 set_default_colorimetry (info);
450 } else if (!validate_colorimetry (info)) {
451 GST_WARNING ("invalid colorimetry, using default");
452 set_default_colorimetry (info);
454 /* force RGB matrix for RGB formats */
455 if (GST_VIDEO_FORMAT_INFO_IS_RGB (info->finfo) &&
456 info->colorimetry.matrix != GST_VIDEO_COLOR_MATRIX_RGB) {
457 GST_WARNING ("invalid matrix %d for RGB format, using RGB",
458 info->colorimetry.matrix);
459 info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_RGB;
463 GST_DEBUG ("no colorimetry, using default");
464 set_default_colorimetry (info);
467 if (!fill_planes (info))
475 GST_ERROR ("wrong name '%s', expected video/ or image/",
476 gst_structure_get_name (structure));
481 GST_ERROR ("no format given");
486 GST_ERROR ("unknown format '%s' given", s);
491 GST_ERROR ("no width property given");
496 GST_ERROR ("no height property given");
502 * gst_video_info_is_equal:
503 * @info: a #GstVideoInfo
504 * @other: a #GstVideoInfo
506 * Compares two #GstVideoInfo and returns whether they are equal or not
508 * Returns: %TRUE if @info and @other are equal, else %FALSE.
511 gst_video_info_is_equal (const GstVideoInfo * info, const GstVideoInfo * other)
515 if (GST_VIDEO_INFO_FORMAT (info) != GST_VIDEO_INFO_FORMAT (other))
517 if (GST_VIDEO_INFO_INTERLACE_MODE (info) !=
518 GST_VIDEO_INFO_INTERLACE_MODE (other))
520 if (GST_VIDEO_INFO_FLAGS (info) != GST_VIDEO_INFO_FLAGS (other))
522 if (GST_VIDEO_INFO_WIDTH (info) != GST_VIDEO_INFO_WIDTH (other))
524 if (GST_VIDEO_INFO_HEIGHT (info) != GST_VIDEO_INFO_HEIGHT (other))
526 if (GST_VIDEO_INFO_SIZE (info) != GST_VIDEO_INFO_SIZE (other))
528 if (GST_VIDEO_INFO_PAR_N (info) != GST_VIDEO_INFO_PAR_N (other))
530 if (GST_VIDEO_INFO_PAR_D (info) != GST_VIDEO_INFO_PAR_D (other))
532 if (GST_VIDEO_INFO_FPS_N (info) != GST_VIDEO_INFO_FPS_N (other))
534 if (GST_VIDEO_INFO_FPS_D (info) != GST_VIDEO_INFO_FPS_D (other))
536 if (!gst_video_colorimetry_is_equal (&GST_VIDEO_INFO_COLORIMETRY (info),
537 &GST_VIDEO_INFO_COLORIMETRY (other)))
539 if (GST_VIDEO_INFO_CHROMA_SITE (info) != GST_VIDEO_INFO_CHROMA_SITE (other))
541 if (GST_VIDEO_INFO_MULTIVIEW_MODE (info) !=
542 GST_VIDEO_INFO_MULTIVIEW_MODE (other))
544 if (GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) !=
545 GST_VIDEO_INFO_MULTIVIEW_FLAGS (other))
547 if (GST_VIDEO_INFO_VIEWS (info) != GST_VIDEO_INFO_VIEWS (other))
550 for (i = 0; i < info->finfo->n_planes; i++) {
551 if (info->stride[i] != other->stride[i])
553 if (info->offset[i] != other->offset[i])
561 * gst_video_info_to_caps:
562 * @info: a #GstVideoInfo
564 * Convert the values of @info into a #GstCaps.
566 * Returns: a new #GstCaps containing the info of @info.
569 gst_video_info_to_caps (GstVideoInfo * info)
575 GstVideoColorimetry colorimetry;
577 g_return_val_if_fail (info != NULL, NULL);
578 g_return_val_if_fail (info->finfo != NULL, NULL);
579 g_return_val_if_fail (info->finfo->format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
581 format = gst_video_format_to_string (info->finfo->format);
582 g_return_val_if_fail (format != NULL, NULL);
584 caps = gst_caps_new_simple ("video/x-raw",
585 "format", G_TYPE_STRING, format,
586 "width", G_TYPE_INT, info->width,
587 "height", G_TYPE_INT, info->height, NULL);
592 gst_caps_set_simple (caps, "interlace-mode", G_TYPE_STRING,
593 gst_video_interlace_mode_to_string (info->interlace_mode), NULL);
595 if (info->interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED &&
596 GST_VIDEO_INFO_FIELD_ORDER (info) != GST_VIDEO_FIELD_ORDER_UNKNOWN) {
597 gst_caps_set_simple (caps, "field-order", G_TYPE_STRING,
598 gst_video_field_order_to_string (GST_VIDEO_INFO_FIELD_ORDER (info)),
602 if (GST_VIDEO_INFO_MULTIVIEW_MODE (info) != GST_VIDEO_MULTIVIEW_MODE_NONE) {
603 const gchar *caps_str = NULL;
605 /* If the half-aspect flag is set, applying it into the PAR of the
606 * resulting caps now seems safe, and helps with automatic behaviour
607 * in elements that aren't explicitly multiview aware */
608 if (GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) &
609 GST_VIDEO_MULTIVIEW_FLAGS_HALF_ASPECT) {
610 GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) &=
611 ~GST_VIDEO_MULTIVIEW_FLAGS_HALF_ASPECT;
612 switch (GST_VIDEO_INFO_MULTIVIEW_MODE (info)) {
613 case GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE:
614 case GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE_QUINCUNX:
615 case GST_VIDEO_MULTIVIEW_MODE_COLUMN_INTERLEAVED:
616 case GST_VIDEO_MULTIVIEW_MODE_CHECKERBOARD:
617 par_n *= 2; /* double the width / half the height */
619 case GST_VIDEO_MULTIVIEW_MODE_ROW_INTERLEAVED:
620 case GST_VIDEO_MULTIVIEW_MODE_TOP_BOTTOM:
621 par_d *= 2; /* half the width / double the height */
629 gst_video_multiview_mode_to_caps_string (GST_VIDEO_INFO_MULTIVIEW_MODE
631 if (caps_str != NULL) {
632 gst_caps_set_simple (caps, "multiview-mode", G_TYPE_STRING,
633 caps_str, "multiview-flags", GST_TYPE_VIDEO_MULTIVIEW_FLAGSET,
634 GST_VIDEO_INFO_MULTIVIEW_FLAGS (info), GST_FLAG_SET_MASK_EXACT, NULL);
638 gst_caps_set_simple (caps, "pixel-aspect-ratio",
639 GST_TYPE_FRACTION, par_n, par_d, NULL);
641 if (info->chroma_site != GST_VIDEO_CHROMA_SITE_UNKNOWN)
642 gst_caps_set_simple (caps, "chroma-site", G_TYPE_STRING,
643 gst_video_chroma_to_string (info->chroma_site), NULL);
645 /* make sure we set the RGB matrix for RGB formats */
646 colorimetry = info->colorimetry;
647 if (GST_VIDEO_FORMAT_INFO_IS_RGB (info->finfo) &&
648 colorimetry.matrix != GST_VIDEO_COLOR_MATRIX_RGB) {
649 GST_WARNING ("invalid matrix %d for RGB format, using RGB",
651 colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_RGB;
653 if ((color = gst_video_colorimetry_to_string (&colorimetry))) {
654 gst_caps_set_simple (caps, "colorimetry", G_TYPE_STRING, color, NULL);
659 gst_caps_set_simple (caps, "views", G_TYPE_INT, info->views, NULL);
661 if (info->flags & GST_VIDEO_FLAG_VARIABLE_FPS && info->fps_n != 0) {
662 /* variable fps with a max-framerate */
663 gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, 0, 1,
664 "max-framerate", GST_TYPE_FRACTION, info->fps_n, info->fps_d, NULL);
666 /* no variable fps or no max-framerate */
667 gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION,
668 info->fps_n, info->fps_d, NULL);
675 fill_planes (GstVideoInfo * info)
677 gsize width, height, cr_h;
680 width = (gsize) info->width;
681 height = (gsize) info->height;
683 /* Sanity check the resulting frame size for overflows */
684 for (i = 0; i < GST_VIDEO_INFO_N_COMPONENTS (info); i++)
685 bpp += GST_VIDEO_INFO_COMP_DEPTH (info, i);
686 bpp = GST_ROUND_UP_8 (bpp) / 8;
687 if (GST_ROUND_UP_128 ((guint64) width) * ((guint64) height) * bpp >=
689 GST_ERROR ("Frame size %ux%u would overflow", info->width, info->height);
693 switch (info->finfo->format) {
694 case GST_VIDEO_FORMAT_YUY2:
695 case GST_VIDEO_FORMAT_YVYU:
696 case GST_VIDEO_FORMAT_UYVY:
697 case GST_VIDEO_FORMAT_VYUY:
698 info->stride[0] = GST_ROUND_UP_4 (width * 2);
700 info->size = info->stride[0] * height;
702 case GST_VIDEO_FORMAT_AYUV:
703 case GST_VIDEO_FORMAT_RGBx:
704 case GST_VIDEO_FORMAT_RGBA:
705 case GST_VIDEO_FORMAT_BGRx:
706 case GST_VIDEO_FORMAT_BGRA:
707 case GST_VIDEO_FORMAT_xRGB:
708 case GST_VIDEO_FORMAT_ARGB:
709 case GST_VIDEO_FORMAT_xBGR:
710 case GST_VIDEO_FORMAT_ABGR:
711 case GST_VIDEO_FORMAT_r210:
712 info->stride[0] = width * 4;
714 info->size = info->stride[0] * height;
716 case GST_VIDEO_FORMAT_RGB16:
717 case GST_VIDEO_FORMAT_BGR16:
718 case GST_VIDEO_FORMAT_RGB15:
719 case GST_VIDEO_FORMAT_BGR15:
720 info->stride[0] = GST_ROUND_UP_4 (width * 2);
722 info->size = info->stride[0] * height;
724 case GST_VIDEO_FORMAT_RGB:
725 case GST_VIDEO_FORMAT_BGR:
726 case GST_VIDEO_FORMAT_v308:
727 case GST_VIDEO_FORMAT_IYU2:
728 info->stride[0] = GST_ROUND_UP_4 (width * 3);
730 info->size = info->stride[0] * height;
732 case GST_VIDEO_FORMAT_v210:
733 info->stride[0] = ((width + 47) / 48) * 128;
735 info->size = info->stride[0] * height;
737 case GST_VIDEO_FORMAT_v216:
738 info->stride[0] = GST_ROUND_UP_8 (width * 4);
740 info->size = info->stride[0] * height;
742 case GST_VIDEO_FORMAT_GRAY8:
743 info->stride[0] = GST_ROUND_UP_4 (width);
745 info->size = info->stride[0] * height;
747 case GST_VIDEO_FORMAT_GRAY16_BE:
748 case GST_VIDEO_FORMAT_GRAY16_LE:
749 info->stride[0] = GST_ROUND_UP_4 (width * 2);
751 info->size = info->stride[0] * height;
753 case GST_VIDEO_FORMAT_UYVP:
754 info->stride[0] = GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4);
756 info->size = info->stride[0] * height;
758 case GST_VIDEO_FORMAT_RGB8P:
759 info->stride[0] = GST_ROUND_UP_4 (width);
762 info->offset[1] = info->stride[0] * height;
763 info->size = info->offset[1] + (4 * 256);
765 case GST_VIDEO_FORMAT_IYU1:
766 info->stride[0] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) +
767 GST_ROUND_UP_4 (width) / 2);
769 info->size = info->stride[0] * height;
771 case GST_VIDEO_FORMAT_ARGB64:
772 case GST_VIDEO_FORMAT_AYUV64:
773 info->stride[0] = width * 8;
775 info->size = info->stride[0] * height;
777 case GST_VIDEO_FORMAT_I420:
778 case GST_VIDEO_FORMAT_YV12: /* same as I420, but plane 1+2 swapped */
779 info->stride[0] = GST_ROUND_UP_4 (width);
780 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
781 info->stride[2] = info->stride[1];
783 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
784 cr_h = GST_ROUND_UP_2 (height) / 2;
785 if (GST_VIDEO_INFO_IS_INTERLACED (info))
786 cr_h = GST_ROUND_UP_2 (cr_h);
787 info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
788 info->size = info->offset[2] + info->stride[2] * cr_h;
790 case GST_VIDEO_FORMAT_Y41B:
791 info->stride[0] = GST_ROUND_UP_4 (width);
792 info->stride[1] = GST_ROUND_UP_16 (width) / 4;
793 info->stride[2] = info->stride[1];
795 info->offset[1] = info->stride[0] * height;
796 info->offset[2] = info->offset[1] + info->stride[1] * height;
797 /* simplification of ROUNDUP4(w)*h + 2*((ROUNDUP16(w)/4)*h */
798 info->size = (info->stride[0] + (GST_ROUND_UP_16 (width) / 2)) * height;
800 case GST_VIDEO_FORMAT_Y42B:
801 info->stride[0] = GST_ROUND_UP_4 (width);
802 info->stride[1] = GST_ROUND_UP_8 (width) / 2;
803 info->stride[2] = info->stride[1];
805 info->offset[1] = info->stride[0] * height;
806 info->offset[2] = info->offset[1] + info->stride[1] * height;
807 /* simplification of ROUNDUP4(w)*h + 2*(ROUNDUP8(w)/2)*h */
808 info->size = (info->stride[0] + GST_ROUND_UP_8 (width)) * height;
810 case GST_VIDEO_FORMAT_Y444:
811 case GST_VIDEO_FORMAT_GBR:
812 info->stride[0] = GST_ROUND_UP_4 (width);
813 info->stride[1] = info->stride[0];
814 info->stride[2] = info->stride[0];
816 info->offset[1] = info->stride[0] * height;
817 info->offset[2] = info->offset[1] * 2;
818 info->size = info->stride[0] * height * 3;
820 case GST_VIDEO_FORMAT_NV12:
821 case GST_VIDEO_FORMAT_NV21:
822 info->stride[0] = GST_ROUND_UP_4 (width);
823 info->stride[1] = info->stride[0];
825 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
826 cr_h = GST_ROUND_UP_2 (height) / 2;
827 if (GST_VIDEO_INFO_IS_INTERLACED (info))
828 cr_h = GST_ROUND_UP_2 (cr_h);
829 info->size = info->offset[1] + info->stride[0] * cr_h;
831 case GST_VIDEO_FORMAT_NV16:
832 case GST_VIDEO_FORMAT_NV61:
833 info->stride[0] = GST_ROUND_UP_4 (width);
834 info->stride[1] = info->stride[0];
836 info->offset[1] = info->stride[0] * height;
837 info->size = info->stride[0] * height * 2;
839 case GST_VIDEO_FORMAT_NV24:
840 info->stride[0] = GST_ROUND_UP_4 (width);
841 info->stride[1] = GST_ROUND_UP_4 (width * 2);
843 info->offset[1] = info->stride[0] * height;
844 info->size = info->stride[0] * height + info->stride[1] * height;
846 case GST_VIDEO_FORMAT_A420:
847 info->stride[0] = GST_ROUND_UP_4 (width);
848 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
849 info->stride[2] = info->stride[1];
850 info->stride[3] = info->stride[0];
852 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
853 cr_h = GST_ROUND_UP_2 (height) / 2;
854 if (GST_VIDEO_INFO_IS_INTERLACED (info))
855 cr_h = GST_ROUND_UP_2 (cr_h);
856 info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
857 info->offset[3] = info->offset[2] + info->stride[2] * cr_h;
858 info->size = info->offset[3] + info->stride[0] * GST_ROUND_UP_2 (height);
860 case GST_VIDEO_FORMAT_YUV9:
861 case GST_VIDEO_FORMAT_YVU9:
862 info->stride[0] = GST_ROUND_UP_4 (width);
863 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
864 info->stride[2] = info->stride[1];
866 info->offset[1] = info->stride[0] * height;
867 cr_h = GST_ROUND_UP_4 (height) / 4;
868 if (GST_VIDEO_INFO_IS_INTERLACED (info))
869 cr_h = GST_ROUND_UP_2 (cr_h);
870 info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
871 info->size = info->offset[2] + info->stride[2] * cr_h;
873 case GST_VIDEO_FORMAT_I420_10LE:
874 case GST_VIDEO_FORMAT_I420_10BE:
875 info->stride[0] = GST_ROUND_UP_4 (width * 2);
876 info->stride[1] = GST_ROUND_UP_4 (width);
877 info->stride[2] = info->stride[1];
879 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
880 cr_h = GST_ROUND_UP_2 (height) / 2;
881 if (GST_VIDEO_INFO_IS_INTERLACED (info))
882 cr_h = GST_ROUND_UP_2 (cr_h);
883 info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
884 info->size = info->offset[2] + info->stride[2] * cr_h;
886 case GST_VIDEO_FORMAT_I422_10LE:
887 case GST_VIDEO_FORMAT_I422_10BE:
888 info->stride[0] = GST_ROUND_UP_4 (width * 2);
889 info->stride[1] = GST_ROUND_UP_4 (width);
890 info->stride[2] = info->stride[1];
892 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
893 info->offset[2] = info->offset[1] +
894 info->stride[1] * GST_ROUND_UP_2 (height);
895 info->size = info->offset[2] + info->stride[2] * GST_ROUND_UP_2 (height);
897 case GST_VIDEO_FORMAT_Y444_10LE:
898 case GST_VIDEO_FORMAT_Y444_10BE:
899 case GST_VIDEO_FORMAT_GBR_10LE:
900 case GST_VIDEO_FORMAT_GBR_10BE:
901 info->stride[0] = GST_ROUND_UP_4 (width * 2);
902 info->stride[1] = info->stride[0];
903 info->stride[2] = info->stride[0];
905 info->offset[1] = info->stride[0] * height;
906 info->offset[2] = info->offset[1] * 2;
907 info->size = info->stride[0] * height * 3;
909 case GST_VIDEO_FORMAT_NV12_64Z32:
911 GST_VIDEO_TILE_MAKE_STRIDE (GST_ROUND_UP_128 (width) / 64,
912 GST_ROUND_UP_32 (height) / 32);
914 GST_VIDEO_TILE_MAKE_STRIDE (GST_ROUND_UP_128 (width) / 64,
915 GST_ROUND_UP_64 (height) / 64);
917 info->offset[1] = GST_ROUND_UP_128 (width) * GST_ROUND_UP_32 (height);
918 info->size = info->offset[1] +
919 GST_ROUND_UP_128 (width) * GST_ROUND_UP_64 (height) / 2;
921 case GST_VIDEO_FORMAT_A420_10LE:
922 case GST_VIDEO_FORMAT_A420_10BE:
923 info->stride[0] = GST_ROUND_UP_4 (width * 2);
924 info->stride[1] = GST_ROUND_UP_4 (width);
925 info->stride[2] = info->stride[1];
926 info->stride[3] = info->stride[0];
928 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
929 cr_h = GST_ROUND_UP_2 (height) / 2;
930 if (GST_VIDEO_INFO_IS_INTERLACED (info))
931 cr_h = GST_ROUND_UP_2 (cr_h);
932 info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
933 info->offset[3] = info->offset[2] + info->stride[2] * cr_h;
934 info->size = info->offset[3] + info->stride[0] * GST_ROUND_UP_2 (height);
936 case GST_VIDEO_FORMAT_A422_10LE:
937 case GST_VIDEO_FORMAT_A422_10BE:
938 info->stride[0] = GST_ROUND_UP_4 (width * 2);
939 info->stride[1] = GST_ROUND_UP_4 (width);
940 info->stride[2] = info->stride[1];
941 info->stride[3] = info->stride[0];
943 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
944 info->offset[2] = info->offset[1] +
945 info->stride[1] * GST_ROUND_UP_2 (height);
947 info->offset[2] + info->stride[2] * GST_ROUND_UP_2 (height);
948 info->size = info->offset[3] + info->stride[0] * GST_ROUND_UP_2 (height);
950 case GST_VIDEO_FORMAT_A444_10LE:
951 case GST_VIDEO_FORMAT_A444_10BE:
952 info->stride[0] = GST_ROUND_UP_4 (width * 2);
953 info->stride[1] = info->stride[0];
954 info->stride[2] = info->stride[0];
955 info->stride[3] = info->stride[0];
957 info->offset[1] = info->stride[0] * height;
958 info->offset[2] = info->offset[1] * 2;
959 info->offset[3] = info->offset[1] * 3;
960 info->size = info->stride[0] * height * 4;
962 case GST_VIDEO_FORMAT_P010_10LE:
963 case GST_VIDEO_FORMAT_P010_10BE:
964 info->stride[0] = GST_ROUND_UP_4 (width * 2);
965 info->stride[1] = info->stride[0];
967 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
968 cr_h = GST_ROUND_UP_2 (height) / 2;
969 info->size = info->offset[1] + info->stride[0] * cr_h;
972 case GST_VIDEO_FORMAT_ENCODED:
974 case GST_VIDEO_FORMAT_UNKNOWN:
975 GST_ERROR ("invalid format");
976 g_warning ("invalid format");
984 * gst_video_info_convert:
985 * @info: a #GstVideoInfo
986 * @src_format: #GstFormat of the @src_value
987 * @src_value: value to convert
988 * @dest_format: #GstFormat of the @dest_value
989 * @dest_value: pointer to destination value
991 * Converts among various #GstFormat types. This function handles
992 * GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT. For
993 * raw video, GST_FORMAT_DEFAULT corresponds to video frames. This
994 * function can be used to handle pad queries of the type GST_QUERY_CONVERT.
996 * Returns: TRUE if the conversion was successful.
999 gst_video_info_convert (GstVideoInfo * info,
1000 GstFormat src_format, gint64 src_value,
1001 GstFormat dest_format, gint64 * dest_value)
1003 gboolean ret = FALSE;
1007 g_return_val_if_fail (info != NULL, 0);
1008 g_return_val_if_fail (info->finfo != NULL, 0);
1009 g_return_val_if_fail (info->finfo->format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1010 g_return_val_if_fail (info->size > 0, 0);
1013 fps_n = info->fps_n;
1014 fps_d = info->fps_d;
1016 GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s to %s",
1017 src_value, gst_format_get_name (src_format),
1018 gst_format_get_name (dest_format));
1020 if (src_format == dest_format) {
1021 *dest_value = src_value;
1026 if (src_value == -1) {
1032 /* bytes to frames */
1033 if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_DEFAULT) {
1035 *dest_value = gst_util_uint64_scale (src_value, 1, size);
1037 GST_ERROR ("blocksize is 0");
1044 /* frames to bytes */
1045 if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_BYTES) {
1046 *dest_value = gst_util_uint64_scale (src_value, size, 1);
1051 /* time to frames */
1052 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_DEFAULT) {
1054 *dest_value = gst_util_uint64_scale (src_value,
1055 fps_n, GST_SECOND * fps_d);
1057 GST_ERROR ("framerate denominator is 0");
1064 /* frames to time */
1065 if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) {
1067 *dest_value = gst_util_uint64_scale (src_value,
1068 GST_SECOND * fps_d, fps_n);
1070 GST_ERROR ("framerate numerator is 0");
1078 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
1080 *dest_value = gst_util_uint64_scale (src_value,
1081 fps_n * size, GST_SECOND * fps_d);
1083 GST_ERROR ("framerate denominator is 0");
1091 if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
1092 if (fps_n != 0 && size != 0) {
1093 *dest_value = gst_util_uint64_scale (src_value,
1094 GST_SECOND * fps_d, fps_n * size);
1096 GST_ERROR ("framerate denominator and/or blocksize is 0");
1104 GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, ret, *dest_value);
1110 * gst_video_info_align:
1111 * @info: a #GstVideoInfo
1112 * @align: alignment parameters
1114 * Adjust the offset and stride fields in @info so that the padding and
1115 * stride alignment in @align is respected.
1117 * Extra padding will be added to the right side when stride alignment padding
1118 * is required and @align will be updated with the new padding values.
1120 * Returns: %FALSE if alignment could not be applied, e.g. because the
1121 * size of a frame can't be represented as a 32 bit integer (Since: 1.12)
1124 gst_video_info_align (GstVideoInfo * info, GstVideoAlignment * align)
1126 const GstVideoFormatInfo *vinfo = info->finfo;
1128 gint padded_width, padded_height;
1132 width = GST_VIDEO_INFO_WIDTH (info);
1133 height = GST_VIDEO_INFO_HEIGHT (info);
1135 GST_LOG ("padding %u-%ux%u-%u", align->padding_top,
1136 align->padding_left, align->padding_right, align->padding_bottom);
1138 n_planes = GST_VIDEO_INFO_N_PLANES (info);
1140 if (GST_VIDEO_FORMAT_INFO_HAS_PALETTE (vinfo))
1143 /* first make sure the left padding does not cause alignment problems later */
1145 GST_LOG ("left padding %u", align->padding_left);
1147 for (i = 0; i < n_planes; i++) {
1150 /* this is the amout of pixels to add as left padding */
1151 hedge = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (vinfo, i, align->padding_left);
1152 hedge *= GST_VIDEO_FORMAT_INFO_PSTRIDE (vinfo, i);
1154 GST_LOG ("plane %d, padding %d, alignment %u", i, hedge,
1155 align->stride_align[i]);
1156 aligned &= (hedge & align->stride_align[i]) == 0;
1161 GST_LOG ("unaligned padding, increasing padding");
1162 /* increase padded_width */
1163 align->padding_left += align->padding_left & ~(align->padding_left - 1);
1166 /* add the padding */
1167 padded_width = width + align->padding_left + align->padding_right;
1168 padded_height = height + align->padding_top + align->padding_bottom;
1171 GST_LOG ("padded dimension %u-%u", padded_width, padded_height);
1173 info->width = padded_width;
1174 info->height = padded_height;
1176 if (!fill_planes (info))
1179 /* check alignment */
1181 for (i = 0; i < n_planes; i++) {
1182 GST_LOG ("plane %d, stride %d, alignment %u", i, info->stride[i],
1183 align->stride_align[i]);
1184 aligned &= (info->stride[i] & align->stride_align[i]) == 0;
1189 GST_LOG ("unaligned strides, increasing dimension");
1190 /* increase padded_width */
1191 padded_width += padded_width & ~(padded_width - 1);
1194 align->padding_right = padded_width - width - align->padding_left;
1196 info->width = width;
1197 info->height = height;
1199 for (i = 0; i < n_planes; i++) {
1200 gint vedge, hedge, comp;
1202 /* Find the component for this plane, FIXME, we assume the plane number and
1203 * component number is the same for now, for scaling the dimensions this is
1204 * currently true for all formats but it might not be when adding new
1205 * formats. We might need to add a plane subsamling in the format info to
1206 * make this more generic or maybe use a plane -> component mapping. */
1210 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (vinfo, comp, align->padding_left);
1212 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vinfo, comp, align->padding_top);
1214 GST_DEBUG ("plane %d: comp: %d, hedge %d vedge %d align %d stride %d", i,
1215 comp, hedge, vedge, align->stride_align[i], info->stride[i]);
1217 info->offset[i] += (vedge * info->stride[i]) +
1218 (hedge * GST_VIDEO_FORMAT_INFO_PSTRIDE (vinfo, comp));