video: Add GBRA_10LE/BE, GBR_12LE/BE, GBRA_12LE/BE pixel formats
[platform/upstream/gstreamer.git] / gst-libs / gst / video / video-info.c
1 /* GStreamer
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>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25
26 #include <string.h>
27 #include <stdio.h>
28
29 #include "video-info.h"
30 #include "video-tile.h"
31
32 #ifndef GST_DISABLE_GST_DEBUG
33 #define GST_CAT_DEFAULT ensure_debug_category()
34 static GstDebugCategory *
35 ensure_debug_category (void)
36 {
37   static gsize cat_gonce = 0;
38
39   if (g_once_init_enter (&cat_gonce)) {
40     gsize cat_done;
41
42     cat_done = (gsize) _gst_debug_category_new ("video-info", 0,
43         "video-info structure");
44
45     g_once_init_leave (&cat_gonce, cat_done);
46   }
47
48   return (GstDebugCategory *) cat_gonce;
49 }
50 #else
51 #define ensure_debug_category() /* NOOP */
52 #endif /* GST_DISABLE_GST_DEBUG */
53
54 /**
55  * gst_video_info_copy:
56  * @info: a #GstVideoInfo
57  *
58  * Copy a GstVideoInfo structure.
59  *
60  * Returns: a new #GstVideoInfo. free with gst_video_info_free.
61  *
62  * Since: 1.6
63  */
64 GstVideoInfo *
65 gst_video_info_copy (const GstVideoInfo * info)
66 {
67   return g_slice_dup (GstVideoInfo, info);
68 }
69
70 /**
71  * gst_video_info_free:
72  * @info: a #GstVideoInfo
73  *
74  * Free a GstVideoInfo structure previously allocated with gst_video_info_new()
75  * or gst_video_info_copy().
76  *
77  * Since: 1.6
78  */
79 void
80 gst_video_info_free (GstVideoInfo * info)
81 {
82   g_slice_free (GstVideoInfo, info);
83 }
84
85 G_DEFINE_BOXED_TYPE (GstVideoInfo, gst_video_info,
86     (GBoxedCopyFunc) gst_video_info_copy, (GBoxedFreeFunc) gst_video_info_free);
87
88 /**
89  * gst_video_info_new:
90  *
91  * Allocate a new #GstVideoInfo that is also initialized with
92  * gst_video_info_init().
93  *
94  * Returns: a new #GstVideoInfo. free with gst_video_info_free().
95  *
96  * Since: 1.6
97  */
98 GstVideoInfo *
99 gst_video_info_new (void)
100 {
101   GstVideoInfo *info;
102
103   info = g_slice_new (GstVideoInfo);
104   gst_video_info_init (info);
105
106   return info;
107 }
108
109 static gboolean fill_planes (GstVideoInfo * info);
110
111 /**
112  * gst_video_info_init:
113  * @info: a #GstVideoInfo
114  *
115  * Initialize @info with default values.
116  */
117 void
118 gst_video_info_init (GstVideoInfo * info)
119 {
120   g_return_if_fail (info != NULL);
121
122   memset (info, 0, sizeof (GstVideoInfo));
123
124   info->finfo = gst_video_format_get_info (GST_VIDEO_FORMAT_UNKNOWN);
125
126   info->views = 1;
127   /* arrange for sensible defaults, e.g. if turned into caps */
128   info->fps_n = 0;
129   info->fps_d = 1;
130   info->par_n = 1;
131   info->par_d = 1;
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;
135 }
136
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 }
140
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
147
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),
155 };
156
157 static void
158 set_default_colorimetry (GstVideoInfo * info)
159 {
160   const GstVideoFormatInfo *finfo = info->finfo;
161
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];
169     } else {
170       info->chroma_site = GST_VIDEO_CHROMA_SITE_NONE;
171       info->colorimetry = default_color[DEFAULT_YUV_SD];
172     }
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];
177   } else {
178     info->colorimetry = default_color[DEFAULT_UNKNOWN];
179   }
180 }
181
182 static gboolean
183 validate_colorimetry (GstVideoInfo * info)
184 {
185   const GstVideoFormatInfo *finfo = info->finfo;
186
187   if (!GST_VIDEO_FORMAT_INFO_IS_RGB (finfo) &&
188       info->colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_RGB)
189     return FALSE;
190
191   if (GST_VIDEO_FORMAT_INFO_IS_YUV (finfo) &&
192       info->colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_UNKNOWN)
193     return FALSE;
194
195   return TRUE;
196 }
197
198 /**
199  * gst_video_info_set_format:
200  * @info: a #GstVideoInfo
201  * @format: the format
202  * @width: a width
203  * @height: a height
204  *
205  * Set the default info for a video frame of @format and @width and @height.
206  *
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.
210  *
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)
213  */
214 gboolean
215 gst_video_info_set_format (GstVideoInfo * info, GstVideoFormat format,
216     guint width, guint height)
217 {
218   g_return_val_if_fail (info != NULL, FALSE);
219   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, FALSE);
220
221   if (width > G_MAXINT || height > G_MAXINT)
222     return FALSE;
223
224   gst_video_info_init (info);
225
226   info->finfo = gst_video_format_get_info (format);
227   info->width = width;
228   info->height = height;
229   info->views = 1;
230
231   set_default_colorimetry (info);
232
233   return fill_planes (info);
234 }
235
236 static const gchar *interlace_mode[] = {
237   "progressive",
238   "interleaved",
239   "mixed",
240   "fields"
241 };
242
243 /**
244  * gst_video_interlace_mode_to_string:
245  * @mode: a #GstVideoInterlaceMode
246  *
247  * Convert @mode to its string representation.
248  *
249  * Returns: @mode as a string or NULL if @mode in invalid.
250  *
251  * Since: 1.6
252  */
253 const gchar *
254 gst_video_interlace_mode_to_string (GstVideoInterlaceMode mode)
255 {
256   if (((guint) mode) >= G_N_ELEMENTS (interlace_mode))
257     return NULL;
258
259   return interlace_mode[mode];
260 }
261
262 /**
263  * gst_video_interlace_mode_from_string:
264  * @mode: a mode
265  *
266  * Convert @mode to a #GstVideoInterlaceMode
267  *
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.
271  *
272  * Since: 1.6
273  */
274 GstVideoInterlaceMode
275 gst_video_interlace_mode_from_string (const gchar * mode)
276 {
277   gint i;
278   for (i = 0; i < G_N_ELEMENTS (interlace_mode); i++) {
279     if (g_str_equal (interlace_mode[i], mode))
280       return i;
281   }
282   return GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
283 }
284
285 static const gchar *field_order[] = {
286   "unknown",
287   "top-field-first",
288   "bottom-field-first"
289 };
290
291 /**
292  * gst_video_field_order_to_string:
293  * @order: a #GstVideoFieldOrder
294  *
295  * Convert @order to its string representation.
296  *
297  * Returns: @order as a string or NULL if @order in invalid.
298  *
299  * Since: 1.12
300  */
301 const gchar *
302 gst_video_field_order_to_string (GstVideoFieldOrder order)
303 {
304   if (((guint) order) >= G_N_ELEMENTS (field_order))
305     return NULL;
306
307   return field_order[order];
308 }
309
310 /**
311  * gst_video_field_order_from_string:
312  * @order: a field order
313  *
314  * Convert @order to a #GstVideoFieldOrder
315  *
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.
319  *
320  * Since: 1.12
321  */
322 GstVideoFieldOrder
323 gst_video_field_order_from_string (const gchar * order)
324 {
325   gint i;
326   for (i = 0; i < G_N_ELEMENTS (field_order); i++) {
327     if (g_str_equal (field_order[i], order))
328       return i;
329   }
330   return GST_VIDEO_FIELD_ORDER_UNKNOWN;
331 }
332
333 /**
334  * gst_video_info_from_caps:
335  * @info: a #GstVideoInfo
336  * @caps: a #GstCaps
337  *
338  * Parse @caps and update @info.
339  *
340  * Returns: TRUE if @caps could be parsed
341  */
342 gboolean
343 gst_video_info_from_caps (GstVideoInfo * info, const GstCaps * caps)
344 {
345   GstStructure *structure;
346   const gchar *s;
347   GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
348   gint width = 0, height = 0;
349   gint fps_n, fps_d;
350   gint par_n, par_d;
351   guint multiview_flags;
352
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);
356
357   GST_DEBUG ("parsing caps %" GST_PTR_FORMAT, caps);
358
359   structure = gst_caps_get_structure (caps, 0);
360
361   if (gst_structure_has_name (structure, "video/x-raw")) {
362     if (!(s = gst_structure_get_string (structure, "format")))
363       goto no_format;
364
365     format = gst_video_format_from_string (s);
366     if (format == GST_VIDEO_FORMAT_UNKNOWN)
367       goto unknown_format;
368
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;
372   } else {
373     goto wrong_name;
374   }
375
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)
379     goto no_width;
380   if (!gst_structure_get_int (structure, "height", &height) &&
381       format != GST_VIDEO_FORMAT_ENCODED)
382     goto no_height;
383
384   gst_video_info_init (info);
385
386   info->finfo = gst_video_format_get_info (format);
387   info->width = width;
388   info->height = height;
389
390   if (gst_structure_get_fraction (structure, "framerate", &fps_n, &fps_d)) {
391     if (fps_n == 0) {
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);
396     }
397     info->fps_n = fps_n;
398     info->fps_d = fps_d;
399   } else {
400     /* unspecified is variable framerate */
401     info->fps_n = 0;
402     info->fps_d = 1;
403   }
404
405   if (gst_structure_get_fraction (structure, "pixel-aspect-ratio",
406           &par_n, &par_d)) {
407     info->par_n = par_n;
408     info->par_d = par_d;
409   } else {
410     info->par_n = 1;
411     info->par_d = 1;
412   }
413
414   if ((s = gst_structure_get_string (structure, "interlace-mode")))
415     info->interlace_mode = gst_video_interlace_mode_from_string (s);
416   else
417     info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
418
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);
422   } else {
423     GST_VIDEO_INFO_FIELD_ORDER (info) = GST_VIDEO_FIELD_ORDER_UNKNOWN;
424   }
425
426   {
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);
430     else
431       GST_VIDEO_INFO_MULTIVIEW_MODE (info) = GST_VIDEO_MULTIVIEW_MODE_NONE;
432
433     if (gst_structure_get_flagset (structure, "multiview-flags",
434             &multiview_flags, NULL))
435       GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) = multiview_flags;
436
437     if (!gst_structure_get_int (structure, "views", &info->views))
438       info->views = 1;
439
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 */
444   }
445
446   if ((s = gst_structure_get_string (structure, "chroma-site")))
447     info->chroma_site = gst_video_chroma_from_string (s);
448   else
449     info->chroma_site = GST_VIDEO_CHROMA_SITE_UNKNOWN;
450
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);
458     } else {
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;
465       }
466     }
467   } else {
468     GST_DEBUG ("no colorimetry, using default");
469     set_default_colorimetry (info);
470   }
471
472   if (!fill_planes (info))
473     return FALSE;
474
475   return TRUE;
476
477   /* ERROR */
478 wrong_name:
479   {
480     GST_ERROR ("wrong name '%s', expected video/ or image/",
481         gst_structure_get_name (structure));
482     return FALSE;
483   }
484 no_format:
485   {
486     GST_ERROR ("no format given");
487     return FALSE;
488   }
489 unknown_format:
490   {
491     GST_ERROR ("unknown format '%s' given", s);
492     return FALSE;
493   }
494 no_width:
495   {
496     GST_ERROR ("no width property given");
497     return FALSE;
498   }
499 no_height:
500   {
501     GST_ERROR ("no height property given");
502     return FALSE;
503   }
504 }
505
506 /**
507  * gst_video_info_is_equal:
508  * @info: a #GstVideoInfo
509  * @other: a #GstVideoInfo
510  *
511  * Compares two #GstVideoInfo and returns whether they are equal or not
512  *
513  * Returns: %TRUE if @info and @other are equal, else %FALSE.
514  */
515 gboolean
516 gst_video_info_is_equal (const GstVideoInfo * info, const GstVideoInfo * other)
517 {
518   gint i;
519
520   if (GST_VIDEO_INFO_FORMAT (info) != GST_VIDEO_INFO_FORMAT (other))
521     return FALSE;
522   if (GST_VIDEO_INFO_INTERLACE_MODE (info) !=
523       GST_VIDEO_INFO_INTERLACE_MODE (other))
524     return FALSE;
525   if (GST_VIDEO_INFO_FLAGS (info) != GST_VIDEO_INFO_FLAGS (other))
526     return FALSE;
527   if (GST_VIDEO_INFO_WIDTH (info) != GST_VIDEO_INFO_WIDTH (other))
528     return FALSE;
529   if (GST_VIDEO_INFO_HEIGHT (info) != GST_VIDEO_INFO_HEIGHT (other))
530     return FALSE;
531   if (GST_VIDEO_INFO_SIZE (info) != GST_VIDEO_INFO_SIZE (other))
532     return FALSE;
533   if (GST_VIDEO_INFO_PAR_N (info) != GST_VIDEO_INFO_PAR_N (other))
534     return FALSE;
535   if (GST_VIDEO_INFO_PAR_D (info) != GST_VIDEO_INFO_PAR_D (other))
536     return FALSE;
537   if (GST_VIDEO_INFO_FPS_N (info) != GST_VIDEO_INFO_FPS_N (other))
538     return FALSE;
539   if (GST_VIDEO_INFO_FPS_D (info) != GST_VIDEO_INFO_FPS_D (other))
540     return FALSE;
541   if (!gst_video_colorimetry_is_equal (&GST_VIDEO_INFO_COLORIMETRY (info),
542           &GST_VIDEO_INFO_COLORIMETRY (other)))
543     return FALSE;
544   if (GST_VIDEO_INFO_CHROMA_SITE (info) != GST_VIDEO_INFO_CHROMA_SITE (other))
545     return FALSE;
546   if (GST_VIDEO_INFO_MULTIVIEW_MODE (info) !=
547       GST_VIDEO_INFO_MULTIVIEW_MODE (other))
548     return FALSE;
549   if (GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) !=
550       GST_VIDEO_INFO_MULTIVIEW_FLAGS (other))
551     return FALSE;
552   if (GST_VIDEO_INFO_VIEWS (info) != GST_VIDEO_INFO_VIEWS (other))
553     return FALSE;
554
555   for (i = 0; i < info->finfo->n_planes; i++) {
556     if (info->stride[i] != other->stride[i])
557       return FALSE;
558     if (info->offset[i] != other->offset[i])
559       return FALSE;
560   }
561
562   return TRUE;
563 }
564
565 /**
566  * gst_video_info_to_caps:
567  * @info: a #GstVideoInfo
568  *
569  * Convert the values of @info into a #GstCaps.
570  *
571  * Returns: a new #GstCaps containing the info of @info.
572  */
573 GstCaps *
574 gst_video_info_to_caps (GstVideoInfo * info)
575 {
576   GstCaps *caps;
577   const gchar *format;
578   gchar *color;
579   gint par_n, par_d;
580   GstVideoColorimetry colorimetry;
581
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);
585
586   format = gst_video_format_to_string (info->finfo->format);
587   g_return_val_if_fail (format != NULL, NULL);
588
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);
593
594   par_n = info->par_n;
595   par_d = info->par_d;
596
597   gst_caps_set_simple (caps, "interlace-mode", G_TYPE_STRING,
598       gst_video_interlace_mode_to_string (info->interlace_mode), NULL);
599
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)),
604         NULL);
605   }
606
607   if (GST_VIDEO_INFO_MULTIVIEW_MODE (info) != GST_VIDEO_MULTIVIEW_MODE_NONE) {
608     const gchar *caps_str = NULL;
609
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 */
623           break;
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 */
627           break;
628         default:
629           break;
630       }
631     }
632
633     caps_str =
634         gst_video_multiview_mode_to_caps_string (GST_VIDEO_INFO_MULTIVIEW_MODE
635         (info));
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);
640     }
641   }
642
643   gst_caps_set_simple (caps, "pixel-aspect-ratio",
644       GST_TYPE_FRACTION, par_n, par_d, NULL);
645
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);
649
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",
655         colorimetry.matrix);
656     colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_RGB;
657   }
658   if ((color = gst_video_colorimetry_to_string (&colorimetry))) {
659     gst_caps_set_simple (caps, "colorimetry", G_TYPE_STRING, color, NULL);
660     g_free (color);
661   }
662
663   if (info->views > 1)
664     gst_caps_set_simple (caps, "views", G_TYPE_INT, info->views, NULL);
665
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);
670   } else {
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);
674   }
675
676   return caps;
677 }
678
679 static gboolean
680 fill_planes (GstVideoInfo * info)
681 {
682   gsize width, height, cr_h;
683   gint bpp = 0, i;
684
685   width = (gsize) info->width;
686   height = (gsize) info->height;
687
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) >=
693       G_MAXUINT / bpp) {
694     GST_ERROR ("Frame size %ux%u would overflow", info->width, info->height);
695     return FALSE;
696   }
697
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);
704       info->offset[0] = 0;
705       info->size = info->stride[0] * height;
706       break;
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;
718       info->offset[0] = 0;
719       info->size = info->stride[0] * height;
720       break;
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);
726       info->offset[0] = 0;
727       info->size = info->stride[0] * height;
728       break;
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);
734       info->offset[0] = 0;
735       info->size = info->stride[0] * height;
736       break;
737     case GST_VIDEO_FORMAT_v210:
738       info->stride[0] = ((width + 47) / 48) * 128;
739       info->offset[0] = 0;
740       info->size = info->stride[0] * height;
741       break;
742     case GST_VIDEO_FORMAT_v216:
743       info->stride[0] = GST_ROUND_UP_8 (width * 4);
744       info->offset[0] = 0;
745       info->size = info->stride[0] * height;
746       break;
747     case GST_VIDEO_FORMAT_GRAY8:
748       info->stride[0] = GST_ROUND_UP_4 (width);
749       info->offset[0] = 0;
750       info->size = info->stride[0] * height;
751       break;
752     case GST_VIDEO_FORMAT_GRAY16_BE:
753     case GST_VIDEO_FORMAT_GRAY16_LE:
754       info->stride[0] = GST_ROUND_UP_4 (width * 2);
755       info->offset[0] = 0;
756       info->size = info->stride[0] * height;
757       break;
758     case GST_VIDEO_FORMAT_UYVP:
759       info->stride[0] = GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4);
760       info->offset[0] = 0;
761       info->size = info->stride[0] * height;
762       break;
763     case GST_VIDEO_FORMAT_RGB8P:
764       info->stride[0] = GST_ROUND_UP_4 (width);
765       info->stride[1] = 4;
766       info->offset[0] = 0;
767       info->offset[1] = info->stride[0] * height;
768       info->size = info->offset[1] + (4 * 256);
769       break;
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);
773       info->offset[0] = 0;
774       info->size = info->stride[0] * height;
775       break;
776     case GST_VIDEO_FORMAT_ARGB64:
777     case GST_VIDEO_FORMAT_AYUV64:
778       info->stride[0] = width * 8;
779       info->offset[0] = 0;
780       info->size = info->stride[0] * height;
781       break;
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];
787       info->offset[0] = 0;
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;
794       break;
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];
799       info->offset[0] = 0;
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;
804       break;
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];
809       info->offset[0] = 0;
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;
814       break;
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];
820       info->offset[0] = 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;
824       break;
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];
829       info->offset[0] = 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;
835       break;
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];
840       info->offset[0] = 0;
841       info->offset[1] = info->stride[0] * height;
842       info->size = info->stride[0] * height * 2;
843       break;
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);
847       info->offset[0] = 0;
848       info->offset[1] = info->stride[0] * height;
849       info->size = info->stride[0] * height + info->stride[1] * height;
850       break;
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];
856       info->offset[0] = 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);
864       break;
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];
870       info->offset[0] = 0;
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;
877       break;
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];
883       info->offset[0] = 0;
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;
890       break;
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];
896       info->offset[0] = 0;
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);
901       break;
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];
911       info->offset[0] = 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;
915       break;
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];
924       info->offset[0] = 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;
929       break;
930     case GST_VIDEO_FORMAT_NV12_64Z32:
931       info->stride[0] =
932           GST_VIDEO_TILE_MAKE_STRIDE (GST_ROUND_UP_128 (width) / 64,
933           GST_ROUND_UP_32 (height) / 32);
934       info->stride[1] =
935           GST_VIDEO_TILE_MAKE_STRIDE (GST_ROUND_UP_128 (width) / 64,
936           GST_ROUND_UP_64 (height) / 64);
937       info->offset[0] = 0;
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;
941       break;
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];
948       info->offset[0] = 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);
956       break;
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];
963       info->offset[0] = 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);
967       info->offset[3] =
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);
970       break;
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];
977       info->offset[0] = 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;
982       break;
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];
987       info->offset[0] = 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;
991       break;
992
993     case GST_VIDEO_FORMAT_ENCODED:
994       break;
995     case GST_VIDEO_FORMAT_UNKNOWN:
996       GST_ERROR ("invalid format");
997       g_warning ("invalid format");
998       return FALSE;
999       break;
1000   }
1001   return TRUE;
1002 }
1003
1004 /**
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
1011  *
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.
1016  *
1017  * Returns: TRUE if the conversion was successful.
1018  */
1019 gboolean
1020 gst_video_info_convert (GstVideoInfo * info,
1021     GstFormat src_format, gint64 src_value,
1022     GstFormat dest_format, gint64 * dest_value)
1023 {
1024   gboolean ret = FALSE;
1025   int fps_n, fps_d;
1026   gsize size;
1027
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);
1032
1033   size = info->size;
1034   fps_n = info->fps_n;
1035   fps_d = info->fps_d;
1036
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));
1040
1041   if (src_format == dest_format) {
1042     *dest_value = src_value;
1043     ret = TRUE;
1044     goto done;
1045   }
1046
1047   if (src_value == -1) {
1048     *dest_value = -1;
1049     ret = TRUE;
1050     goto done;
1051   }
1052
1053   /* bytes to frames */
1054   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_DEFAULT) {
1055     if (size != 0) {
1056       *dest_value = gst_util_uint64_scale (src_value, 1, size);
1057     } else {
1058       GST_ERROR ("blocksize is 0");
1059       *dest_value = 0;
1060     }
1061     ret = TRUE;
1062     goto done;
1063   }
1064
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);
1068     ret = TRUE;
1069     goto done;
1070   }
1071
1072   /* time to frames */
1073   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_DEFAULT) {
1074     if (fps_d != 0) {
1075       *dest_value = gst_util_uint64_scale (src_value,
1076           fps_n, GST_SECOND * fps_d);
1077     } else {
1078       GST_ERROR ("framerate denominator is 0");
1079       *dest_value = 0;
1080     }
1081     ret = TRUE;
1082     goto done;
1083   }
1084
1085   /* frames to time */
1086   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) {
1087     if (fps_n != 0) {
1088       *dest_value = gst_util_uint64_scale (src_value,
1089           GST_SECOND * fps_d, fps_n);
1090     } else {
1091       GST_ERROR ("framerate numerator is 0");
1092       *dest_value = 0;
1093     }
1094     ret = TRUE;
1095     goto done;
1096   }
1097
1098   /* time to bytes */
1099   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
1100     if (fps_d != 0) {
1101       *dest_value = gst_util_uint64_scale (src_value,
1102           fps_n * size, GST_SECOND * fps_d);
1103     } else {
1104       GST_ERROR ("framerate denominator is 0");
1105       *dest_value = 0;
1106     }
1107     ret = TRUE;
1108     goto done;
1109   }
1110
1111   /* bytes to time */
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);
1116     } else {
1117       GST_ERROR ("framerate denominator and/or blocksize is 0");
1118       *dest_value = 0;
1119     }
1120     ret = TRUE;
1121   }
1122
1123 done:
1124
1125   GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, ret, *dest_value);
1126
1127   return ret;
1128 }
1129
1130 /**
1131  * gst_video_info_align:
1132  * @info: a #GstVideoInfo
1133  * @align: alignment parameters
1134  *
1135  * Adjust the offset and stride fields in @info so that the padding and
1136  * stride alignment in @align is respected.
1137  *
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.
1140  *
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)
1143  */
1144 gboolean
1145 gst_video_info_align (GstVideoInfo * info, GstVideoAlignment * align)
1146 {
1147   const GstVideoFormatInfo *vinfo = info->finfo;
1148   gint width, height;
1149   gint padded_width, padded_height;
1150   gint i, n_planes;
1151   gboolean aligned;
1152
1153   width = GST_VIDEO_INFO_WIDTH (info);
1154   height = GST_VIDEO_INFO_HEIGHT (info);
1155
1156   GST_LOG ("padding %u-%ux%u-%u", align->padding_top,
1157       align->padding_left, align->padding_right, align->padding_bottom);
1158
1159   n_planes = GST_VIDEO_INFO_N_PLANES (info);
1160
1161   if (GST_VIDEO_FORMAT_INFO_HAS_PALETTE (vinfo))
1162     n_planes--;
1163
1164   /* first make sure the left padding does not cause alignment problems later */
1165   do {
1166     GST_LOG ("left padding %u", align->padding_left);
1167     aligned = TRUE;
1168     for (i = 0; i < n_planes; i++) {
1169       gint hedge;
1170
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);
1174
1175       GST_LOG ("plane %d, padding %d, alignment %u", i, hedge,
1176           align->stride_align[i]);
1177       aligned &= (hedge & align->stride_align[i]) == 0;
1178     }
1179     if (aligned)
1180       break;
1181
1182     GST_LOG ("unaligned padding, increasing padding");
1183     /* increase padded_width */
1184     align->padding_left += align->padding_left & ~(align->padding_left - 1);
1185   } while (!aligned);
1186
1187   /* add the padding */
1188   padded_width = width + align->padding_left + align->padding_right;
1189   padded_height = height + align->padding_top + align->padding_bottom;
1190
1191   do {
1192     GST_LOG ("padded dimension %u-%u", padded_width, padded_height);
1193
1194     info->width = padded_width;
1195     info->height = padded_height;
1196
1197     if (!fill_planes (info))
1198       return FALSE;
1199
1200     /* check alignment */
1201     aligned = TRUE;
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;
1206     }
1207     if (aligned)
1208       break;
1209
1210     GST_LOG ("unaligned strides, increasing dimension");
1211     /* increase padded_width */
1212     padded_width += padded_width & ~(padded_width - 1);
1213   } while (!aligned);
1214
1215   align->padding_right = padded_width - width - align->padding_left;
1216
1217   info->width = width;
1218   info->height = height;
1219
1220   for (i = 0; i < n_planes; i++) {
1221     gint vedge, hedge, comp;
1222
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. */
1228     comp = i;
1229
1230     hedge =
1231         GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (vinfo, comp, align->padding_left);
1232     vedge =
1233         GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vinfo, comp, align->padding_top);
1234
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]);
1237
1238     info->offset[i] += (vedge * info->stride[i]) +
1239         (hedge * GST_VIDEO_FORMAT_INFO_PSTRIDE (vinfo, comp));
1240   }
1241
1242   return TRUE;
1243 }