Merge branch 'upstream/1.16' into tizen_gst_1.16.2
[platform/upstream/gst-plugins-base.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     GST_WARNING
190         ("color matrix RGB is only supported with RGB format, %s is not",
191         finfo->name);
192     return FALSE;
193   }
194
195   if (GST_VIDEO_FORMAT_INFO_IS_YUV (finfo) &&
196       info->colorimetry.matrix == GST_VIDEO_COLOR_MATRIX_UNKNOWN) {
197     GST_WARNING ("Need to specify a color matrix when using YUV format (%s)",
198         finfo->name);
199     return FALSE;
200   }
201
202   return TRUE;
203 }
204
205 static gboolean
206 gst_video_info_set_format_common (GstVideoInfo * info, GstVideoFormat format,
207     guint width, guint height)
208 {
209   g_return_val_if_fail (info != NULL, FALSE);
210   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, FALSE);
211
212   if (width > G_MAXINT || height > G_MAXINT)
213     return FALSE;
214
215   gst_video_info_init (info);
216
217   info->finfo = gst_video_format_get_info (format);
218   info->width = width;
219   info->height = height;
220   info->views = 1;
221
222   set_default_colorimetry (info);
223
224   return TRUE;
225 }
226
227 /**
228  * gst_video_info_set_format:
229  * @info: a #GstVideoInfo
230  * @format: the format
231  * @width: a width
232  * @height: a height
233  *
234  * Set the default info for a video frame of @format and @width and @height.
235  *
236  * Note: This initializes @info first, no values are preserved. This function
237  * does not set the offsets correctly for interlaced vertically
238  * subsampled formats.
239  *
240  * Returns: %FALSE if the returned video info is invalid, e.g. because the
241  *   size of a frame can't be represented as a 32 bit integer (Since: 1.12)
242  */
243 gboolean
244 gst_video_info_set_format (GstVideoInfo * info, GstVideoFormat format,
245     guint width, guint height)
246 {
247   if (!gst_video_info_set_format_common (info, format, width, height))
248     return FALSE;
249
250   return fill_planes (info);
251 }
252
253 /**
254  * gst_video_info_set_interlaced_format:
255  * @info: a #GstVideoInfo
256  * @format: the format
257  * @mode: a #GstVideoInterlaceMode
258  * @width: a width
259  * @height: a height
260  *
261  * Same as #gst_video_info_set_format but also allowing to set the interlaced
262  * mode.
263  *
264  * Returns: %FALSE if the returned video info is invalid, e.g. because the
265  *   size of a frame can't be represented as a 32 bit integer.
266  *
267  * Since: 1.16
268  */
269 gboolean
270 gst_video_info_set_interlaced_format (GstVideoInfo * info,
271     GstVideoFormat format, GstVideoInterlaceMode mode, guint width,
272     guint height)
273 {
274   if (!gst_video_info_set_format_common (info, format, width, height))
275     return FALSE;
276
277   GST_VIDEO_INFO_INTERLACE_MODE (info) = mode;
278   return fill_planes (info);
279 }
280
281 static const gchar *interlace_mode[] = {
282   "progressive",
283   "interleaved",
284   "mixed",
285   "fields",
286   "alternate"
287 };
288
289 /**
290  * gst_video_interlace_mode_to_string:
291  * @mode: a #GstVideoInterlaceMode
292  *
293  * Convert @mode to its string representation.
294  *
295  * Returns: @mode as a string or NULL if @mode in invalid.
296  *
297  * Since: 1.6
298  */
299 const gchar *
300 gst_video_interlace_mode_to_string (GstVideoInterlaceMode mode)
301 {
302   if (((guint) mode) >= G_N_ELEMENTS (interlace_mode))
303     return NULL;
304
305   return interlace_mode[mode];
306 }
307
308 /**
309  * gst_video_interlace_mode_from_string:
310  * @mode: a mode
311  *
312  * Convert @mode to a #GstVideoInterlaceMode
313  *
314  * Returns: the #GstVideoInterlaceMode of @mode or
315  *    #GST_VIDEO_INTERLACE_MODE_PROGRESSIVE when @mode is not a valid
316  *    string representation for a #GstVideoInterlaceMode.
317  *
318  * Since: 1.6
319  */
320 GstVideoInterlaceMode
321 gst_video_interlace_mode_from_string (const gchar * mode)
322 {
323   gint i;
324   for (i = 0; i < G_N_ELEMENTS (interlace_mode); i++) {
325     if (g_str_equal (interlace_mode[i], mode))
326       return i;
327   }
328   return GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
329 }
330
331 static const gchar *field_order[] = {
332   "unknown",
333   "top-field-first",
334   "bottom-field-first"
335 };
336
337 /**
338  * gst_video_field_order_to_string:
339  * @order: a #GstVideoFieldOrder
340  *
341  * Convert @order to its string representation.
342  *
343  * Returns: @order as a string or NULL if @order in invalid.
344  *
345  * Since: 1.12
346  */
347 const gchar *
348 gst_video_field_order_to_string (GstVideoFieldOrder order)
349 {
350   if (((guint) order) >= G_N_ELEMENTS (field_order))
351     return NULL;
352
353   return field_order[order];
354 }
355
356 /**
357  * gst_video_field_order_from_string:
358  * @order: a field order
359  *
360  * Convert @order to a #GstVideoFieldOrder
361  *
362  * Returns: the #GstVideoFieldOrder of @order or
363  *    #GST_VIDEO_FIELD_ORDER_UNKNOWN when @order is not a valid
364  *    string representation for a #GstVideoFieldOrder.
365  *
366  * Since: 1.12
367  */
368 GstVideoFieldOrder
369 gst_video_field_order_from_string (const gchar * order)
370 {
371   gint i;
372   for (i = 0; i < G_N_ELEMENTS (field_order); i++) {
373     if (g_str_equal (field_order[i], order))
374       return i;
375   }
376   return GST_VIDEO_FIELD_ORDER_UNKNOWN;
377 }
378
379 /**
380  * gst_video_info_from_caps:
381  * @info: a #GstVideoInfo
382  * @caps: a #GstCaps
383  *
384  * Parse @caps and update @info.
385  *
386  * Returns: TRUE if @caps could be parsed
387  */
388 gboolean
389 gst_video_info_from_caps (GstVideoInfo * info, const GstCaps * caps)
390 {
391   GstStructure *structure;
392   const gchar *s;
393   GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
394   gint width = 0, height = 0;
395   gint fps_n, fps_d;
396   gint par_n, par_d;
397   guint multiview_flags;
398
399   g_return_val_if_fail (info != NULL, FALSE);
400   g_return_val_if_fail (caps != NULL, FALSE);
401   g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
402
403   GST_DEBUG ("parsing caps %" GST_PTR_FORMAT, caps);
404
405   structure = gst_caps_get_structure (caps, 0);
406
407   if (gst_structure_has_name (structure, "video/x-raw")) {
408     if (!(s = gst_structure_get_string (structure, "format")))
409       goto no_format;
410
411     format = gst_video_format_from_string (s);
412     if (format == GST_VIDEO_FORMAT_UNKNOWN)
413       goto unknown_format;
414
415   } else if (g_str_has_prefix (gst_structure_get_name (structure), "video/") ||
416       g_str_has_prefix (gst_structure_get_name (structure), "image/")) {
417     format = GST_VIDEO_FORMAT_ENCODED;
418   } else {
419     goto wrong_name;
420   }
421
422   /* width and height are mandatory, except for non-raw-formats */
423   if (!gst_structure_get_int (structure, "width", &width) &&
424       format != GST_VIDEO_FORMAT_ENCODED)
425     goto no_width;
426   if (!gst_structure_get_int (structure, "height", &height) &&
427       format != GST_VIDEO_FORMAT_ENCODED)
428     goto no_height;
429
430   gst_video_info_init (info);
431
432   info->finfo = gst_video_format_get_info (format);
433   info->width = width;
434   info->height = height;
435
436   if (gst_structure_get_fraction (structure, "framerate", &fps_n, &fps_d)) {
437     if (fps_n == 0) {
438       /* variable framerate */
439       info->flags |= GST_VIDEO_FLAG_VARIABLE_FPS;
440       /* see if we have a max-framerate */
441       gst_structure_get_fraction (structure, "max-framerate", &fps_n, &fps_d);
442     }
443     info->fps_n = fps_n;
444     info->fps_d = fps_d;
445   } else {
446     /* unspecified is variable framerate */
447     info->fps_n = 0;
448     info->fps_d = 1;
449   }
450
451   if (gst_structure_get_fraction (structure, "pixel-aspect-ratio",
452           &par_n, &par_d)) {
453     info->par_n = par_n;
454     info->par_d = par_d;
455   } else {
456     info->par_n = 1;
457     info->par_d = 1;
458   }
459
460   if ((s = gst_structure_get_string (structure, "interlace-mode")))
461     info->interlace_mode = gst_video_interlace_mode_from_string (s);
462   else
463     info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
464
465   /* Interlaced feature is mandatory for raw alternate streams */
466   if (info->interlace_mode == GST_VIDEO_INTERLACE_MODE_ALTERNATE &&
467       format != GST_VIDEO_FORMAT_ENCODED) {
468     GstCapsFeatures *f;
469
470     f = gst_caps_get_features (caps, 0);
471     if (!f
472         || !gst_caps_features_contains (f, GST_CAPS_FEATURE_FORMAT_INTERLACED))
473       goto alternate_no_feature;
474   }
475
476   if (GST_VIDEO_INFO_IS_INTERLACED (info) &&
477       (s = gst_structure_get_string (structure, "field-order"))) {
478     GST_VIDEO_INFO_FIELD_ORDER (info) = gst_video_field_order_from_string (s);
479   } else {
480     GST_VIDEO_INFO_FIELD_ORDER (info) = GST_VIDEO_FIELD_ORDER_UNKNOWN;
481   }
482
483   {
484     if ((s = gst_structure_get_string (structure, "multiview-mode")))
485       GST_VIDEO_INFO_MULTIVIEW_MODE (info) =
486           gst_video_multiview_mode_from_caps_string (s);
487     else
488       GST_VIDEO_INFO_MULTIVIEW_MODE (info) = GST_VIDEO_MULTIVIEW_MODE_NONE;
489
490     if (gst_structure_get_flagset (structure, "multiview-flags",
491             &multiview_flags, NULL))
492       GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) = multiview_flags;
493
494     if (!gst_structure_get_int (structure, "views", &info->views))
495       info->views = 1;
496
497     /* At one point, I tried normalising the half-aspect flag here,
498      * but it behaves weird for GstVideoInfo operations other than
499      * directly converting to/from caps - sometimes causing the
500      * PAR to be doubled/halved too many times */
501   }
502
503   if ((s = gst_structure_get_string (structure, "chroma-site")))
504     info->chroma_site = gst_video_chroma_from_string (s);
505   else
506     info->chroma_site = GST_VIDEO_CHROMA_SITE_UNKNOWN;
507
508   if ((s = gst_structure_get_string (structure, "colorimetry"))) {
509     if (!gst_video_colorimetry_from_string (&info->colorimetry, s)) {
510       GST_WARNING ("unparsable colorimetry, using default");
511       set_default_colorimetry (info);
512     } else if (!validate_colorimetry (info)) {
513       GST_WARNING ("invalid colorimetry, using default");
514       set_default_colorimetry (info);
515     } else {
516       /* force RGB matrix for RGB formats */
517       if (GST_VIDEO_FORMAT_INFO_IS_RGB (info->finfo) &&
518           info->colorimetry.matrix != GST_VIDEO_COLOR_MATRIX_RGB) {
519         GST_WARNING ("invalid matrix %d for RGB format, using RGB",
520             info->colorimetry.matrix);
521         info->colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_RGB;
522       }
523     }
524   } else {
525     GST_DEBUG ("no colorimetry, using default");
526     set_default_colorimetry (info);
527   }
528
529   if (!fill_planes (info))
530     return FALSE;
531
532   return TRUE;
533
534   /* ERROR */
535 wrong_name:
536   {
537     GST_ERROR ("wrong name '%s', expected video/ or image/",
538         gst_structure_get_name (structure));
539     return FALSE;
540   }
541 no_format:
542   {
543     GST_ERROR ("no format given");
544     return FALSE;
545   }
546 unknown_format:
547   {
548     GST_ERROR ("unknown format '%s' given", s);
549     return FALSE;
550   }
551 no_width:
552   {
553     GST_ERROR ("no width property given");
554     return FALSE;
555   }
556 no_height:
557   {
558     GST_ERROR ("no height property given");
559     return FALSE;
560   }
561 alternate_no_feature:
562   {
563     GST_ERROR
564         ("caps has 'interlace-mode=alternate' but doesn't have the Interlaced feature");
565     return FALSE;
566   }
567 }
568
569 /**
570  * gst_video_info_is_equal:
571  * @info: a #GstVideoInfo
572  * @other: a #GstVideoInfo
573  *
574  * Compares two #GstVideoInfo and returns whether they are equal or not
575  *
576  * Returns: %TRUE if @info and @other are equal, else %FALSE.
577  */
578 gboolean
579 gst_video_info_is_equal (const GstVideoInfo * info, const GstVideoInfo * other)
580 {
581   gint i;
582
583   if (GST_VIDEO_INFO_FORMAT (info) != GST_VIDEO_INFO_FORMAT (other))
584     return FALSE;
585   if (GST_VIDEO_INFO_INTERLACE_MODE (info) !=
586       GST_VIDEO_INFO_INTERLACE_MODE (other))
587     return FALSE;
588   if (GST_VIDEO_INFO_FLAGS (info) != GST_VIDEO_INFO_FLAGS (other))
589     return FALSE;
590   if (GST_VIDEO_INFO_WIDTH (info) != GST_VIDEO_INFO_WIDTH (other))
591     return FALSE;
592   if (GST_VIDEO_INFO_HEIGHT (info) != GST_VIDEO_INFO_HEIGHT (other))
593     return FALSE;
594   if (GST_VIDEO_INFO_SIZE (info) != GST_VIDEO_INFO_SIZE (other))
595     return FALSE;
596   if (GST_VIDEO_INFO_PAR_N (info) != GST_VIDEO_INFO_PAR_N (other))
597     return FALSE;
598   if (GST_VIDEO_INFO_PAR_D (info) != GST_VIDEO_INFO_PAR_D (other))
599     return FALSE;
600   if (GST_VIDEO_INFO_FPS_N (info) != GST_VIDEO_INFO_FPS_N (other))
601     return FALSE;
602   if (GST_VIDEO_INFO_FPS_D (info) != GST_VIDEO_INFO_FPS_D (other))
603     return FALSE;
604   if (!gst_video_colorimetry_is_equal (&GST_VIDEO_INFO_COLORIMETRY (info),
605           &GST_VIDEO_INFO_COLORIMETRY (other)))
606     return FALSE;
607   if (GST_VIDEO_INFO_CHROMA_SITE (info) != GST_VIDEO_INFO_CHROMA_SITE (other))
608     return FALSE;
609   if (GST_VIDEO_INFO_MULTIVIEW_MODE (info) !=
610       GST_VIDEO_INFO_MULTIVIEW_MODE (other))
611     return FALSE;
612   if (GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) !=
613       GST_VIDEO_INFO_MULTIVIEW_FLAGS (other))
614     return FALSE;
615   if (GST_VIDEO_INFO_VIEWS (info) != GST_VIDEO_INFO_VIEWS (other))
616     return FALSE;
617
618   for (i = 0; i < info->finfo->n_planes; i++) {
619     if (info->stride[i] != other->stride[i])
620       return FALSE;
621     if (info->offset[i] != other->offset[i])
622       return FALSE;
623   }
624
625   return TRUE;
626 }
627
628 /**
629  * gst_video_info_to_caps:
630  * @info: a #GstVideoInfo
631  *
632  * Convert the values of @info into a #GstCaps.
633  *
634  * Returns: a new #GstCaps containing the info of @info.
635  */
636 GstCaps *
637 gst_video_info_to_caps (GstVideoInfo * info)
638 {
639   GstCaps *caps;
640   const gchar *format;
641   gchar *color;
642   gint par_n, par_d;
643   GstVideoColorimetry colorimetry;
644
645   g_return_val_if_fail (info != NULL, NULL);
646   g_return_val_if_fail (info->finfo != NULL, NULL);
647   g_return_val_if_fail (info->finfo->format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
648
649   format = gst_video_format_to_string (info->finfo->format);
650   g_return_val_if_fail (format != NULL, NULL);
651
652   caps = gst_caps_new_simple ("video/x-raw",
653       "format", G_TYPE_STRING, format,
654       "width", G_TYPE_INT, info->width,
655       "height", G_TYPE_INT, info->height, NULL);
656
657   par_n = info->par_n;
658   par_d = info->par_d;
659
660   gst_caps_set_simple (caps, "interlace-mode", G_TYPE_STRING,
661       gst_video_interlace_mode_to_string (info->interlace_mode), NULL);
662
663   if ((info->interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED ||
664           info->interlace_mode == GST_VIDEO_INTERLACE_MODE_ALTERNATE) &&
665       GST_VIDEO_INFO_FIELD_ORDER (info) != GST_VIDEO_FIELD_ORDER_UNKNOWN) {
666     gst_caps_set_simple (caps, "field-order", G_TYPE_STRING,
667         gst_video_field_order_to_string (GST_VIDEO_INFO_FIELD_ORDER (info)),
668         NULL);
669   }
670
671   if (info->interlace_mode == GST_VIDEO_INTERLACE_MODE_ALTERNATE) {
672     /* 'alternate' mode must always be accompanied by interlaced caps feature.
673      */
674     GstCapsFeatures *features;
675
676     features = gst_caps_features_new (GST_CAPS_FEATURE_FORMAT_INTERLACED, NULL);
677     gst_caps_set_features (caps, 0, features);
678   }
679
680   if (GST_VIDEO_INFO_MULTIVIEW_MODE (info) != GST_VIDEO_MULTIVIEW_MODE_NONE) {
681     const gchar *caps_str = NULL;
682
683     /* If the half-aspect flag is set, applying it into the PAR of the
684      * resulting caps now seems safe, and helps with automatic behaviour
685      * in elements that aren't explicitly multiview aware */
686     if (GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) &
687         GST_VIDEO_MULTIVIEW_FLAGS_HALF_ASPECT) {
688       GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) &=
689           ~GST_VIDEO_MULTIVIEW_FLAGS_HALF_ASPECT;
690       switch (GST_VIDEO_INFO_MULTIVIEW_MODE (info)) {
691         case GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE:
692         case GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE_QUINCUNX:
693         case GST_VIDEO_MULTIVIEW_MODE_COLUMN_INTERLEAVED:
694         case GST_VIDEO_MULTIVIEW_MODE_CHECKERBOARD:
695           par_n *= 2;           /* double the width / half the height */
696           break;
697         case GST_VIDEO_MULTIVIEW_MODE_ROW_INTERLEAVED:
698         case GST_VIDEO_MULTIVIEW_MODE_TOP_BOTTOM:
699           par_d *= 2;           /* half the width / double the height */
700           break;
701         default:
702           break;
703       }
704     }
705
706     caps_str =
707         gst_video_multiview_mode_to_caps_string (GST_VIDEO_INFO_MULTIVIEW_MODE
708         (info));
709     if (caps_str != NULL) {
710       gst_caps_set_simple (caps, "multiview-mode", G_TYPE_STRING,
711           caps_str, "multiview-flags", GST_TYPE_VIDEO_MULTIVIEW_FLAGSET,
712           GST_VIDEO_INFO_MULTIVIEW_FLAGS (info), GST_FLAG_SET_MASK_EXACT, NULL);
713     }
714   }
715
716   gst_caps_set_simple (caps, "pixel-aspect-ratio",
717       GST_TYPE_FRACTION, par_n, par_d, NULL);
718
719   if (info->chroma_site != GST_VIDEO_CHROMA_SITE_UNKNOWN)
720     gst_caps_set_simple (caps, "chroma-site", G_TYPE_STRING,
721         gst_video_chroma_to_string (info->chroma_site), NULL);
722
723   /* make sure we set the RGB matrix for RGB formats */
724   colorimetry = info->colorimetry;
725   if (GST_VIDEO_FORMAT_INFO_IS_RGB (info->finfo) &&
726       colorimetry.matrix != GST_VIDEO_COLOR_MATRIX_RGB) {
727     GST_WARNING ("invalid matrix %d for RGB format, using RGB",
728         colorimetry.matrix);
729     colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_RGB;
730   }
731   if ((color = gst_video_colorimetry_to_string (&colorimetry))) {
732     gst_caps_set_simple (caps, "colorimetry", G_TYPE_STRING, color, NULL);
733     g_free (color);
734   }
735
736   if (info->views > 1)
737     gst_caps_set_simple (caps, "views", G_TYPE_INT, info->views, NULL);
738
739   if (info->flags & GST_VIDEO_FLAG_VARIABLE_FPS && info->fps_n != 0) {
740     /* variable fps with a max-framerate */
741     gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, 0, 1,
742         "max-framerate", GST_TYPE_FRACTION, info->fps_n, info->fps_d, NULL);
743   } else {
744     /* no variable fps or no max-framerate */
745     gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION,
746         info->fps_n, info->fps_d, NULL);
747   }
748
749   return caps;
750 }
751
752 static gboolean
753 fill_planes (GstVideoInfo * info)
754 {
755   gsize width, height, cr_h;
756   gint bpp = 0, i;
757
758   width = (gsize) info->width;
759   height = (gsize) GST_VIDEO_INFO_FIELD_HEIGHT (info);
760
761   /* Sanity check the resulting frame size for overflows */
762   for (i = 0; i < GST_VIDEO_INFO_N_COMPONENTS (info); i++)
763     bpp += GST_VIDEO_INFO_COMP_DEPTH (info, i);
764   bpp = GST_ROUND_UP_8 (bpp) / 8;
765   if (bpp > 0 && GST_ROUND_UP_128 ((guint64) width) * ((guint64) height) >=
766       G_MAXUINT / bpp) {
767     GST_ERROR ("Frame size %ux%u would overflow", info->width, info->height);
768     return FALSE;
769   }
770
771   switch (info->finfo->format) {
772     case GST_VIDEO_FORMAT_YUY2:
773     case GST_VIDEO_FORMAT_YVYU:
774     case GST_VIDEO_FORMAT_UYVY:
775     case GST_VIDEO_FORMAT_VYUY:
776       info->stride[0] = GST_ROUND_UP_4 (width * 2);
777       info->offset[0] = 0;
778       info->size = info->stride[0] * height;
779       break;
780     case GST_VIDEO_FORMAT_AYUV:
781     case GST_VIDEO_FORMAT_RGBx:
782     case GST_VIDEO_FORMAT_RGBA:
783     case GST_VIDEO_FORMAT_BGRx:
784     case GST_VIDEO_FORMAT_BGRA:
785     case GST_VIDEO_FORMAT_SR32:
786     case GST_VIDEO_FORMAT_xRGB:
787     case GST_VIDEO_FORMAT_ARGB:
788     case GST_VIDEO_FORMAT_xBGR:
789     case GST_VIDEO_FORMAT_ABGR:
790     case GST_VIDEO_FORMAT_r210:
791     case GST_VIDEO_FORMAT_Y410:
792     case GST_VIDEO_FORMAT_VUYA:
793     case GST_VIDEO_FORMAT_BGR10A2_LE:
794       info->stride[0] = width * 4;
795       info->offset[0] = 0;
796       info->size = info->stride[0] * height;
797       break;
798     case GST_VIDEO_FORMAT_RGB16:
799     case GST_VIDEO_FORMAT_BGR16:
800     case GST_VIDEO_FORMAT_RGB15:
801     case GST_VIDEO_FORMAT_BGR15:
802       info->stride[0] = GST_ROUND_UP_4 (width * 2);
803       info->offset[0] = 0;
804       info->size = info->stride[0] * height;
805       break;
806     case GST_VIDEO_FORMAT_RGB:
807     case GST_VIDEO_FORMAT_BGR:
808     case GST_VIDEO_FORMAT_v308:
809     case GST_VIDEO_FORMAT_IYU2:
810       info->stride[0] = GST_ROUND_UP_4 (width * 3);
811       info->offset[0] = 0;
812       info->size = info->stride[0] * height;
813       break;
814     case GST_VIDEO_FORMAT_v210:
815       info->stride[0] = ((width + 47) / 48) * 128;
816       info->offset[0] = 0;
817       info->size = info->stride[0] * height;
818       break;
819     case GST_VIDEO_FORMAT_v216:
820     case GST_VIDEO_FORMAT_Y210:
821       info->stride[0] = GST_ROUND_UP_8 (width * 4);
822       info->offset[0] = 0;
823       info->size = info->stride[0] * height;
824       break;
825     case GST_VIDEO_FORMAT_GRAY8:
826       info->stride[0] = GST_ROUND_UP_4 (width);
827       info->offset[0] = 0;
828       info->size = info->stride[0] * height;
829       break;
830     case GST_VIDEO_FORMAT_GRAY16_BE:
831     case GST_VIDEO_FORMAT_GRAY16_LE:
832     case GST_VIDEO_FORMAT_INVZ:
833       info->stride[0] = GST_ROUND_UP_4 (width * 2);
834       info->offset[0] = 0;
835       info->size = info->stride[0] * height;
836       break;
837     case GST_VIDEO_FORMAT_UYVP:
838       info->stride[0] = GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4);
839       info->offset[0] = 0;
840       info->size = info->stride[0] * height;
841       break;
842     case GST_VIDEO_FORMAT_RGB8P:
843       info->stride[0] = GST_ROUND_UP_4 (width);
844       info->stride[1] = 4;
845       info->offset[0] = 0;
846       info->offset[1] = info->stride[0] * height;
847       info->size = info->offset[1] + (4 * 256);
848       break;
849     case GST_VIDEO_FORMAT_IYU1:
850       info->stride[0] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) +
851           GST_ROUND_UP_4 (width) / 2);
852       info->offset[0] = 0;
853       info->size = info->stride[0] * height;
854       break;
855     case GST_VIDEO_FORMAT_ARGB64:
856     case GST_VIDEO_FORMAT_AYUV64:
857       info->stride[0] = width * 8;
858       info->offset[0] = 0;
859       info->size = info->stride[0] * height;
860       break;
861     case GST_VIDEO_FORMAT_I420:
862     case GST_VIDEO_FORMAT_S420:
863     case GST_VIDEO_FORMAT_YV12:        /* same as I420, but plane 1+2 swapped */
864 #ifdef TIZEN_PROFILE_TV
865     case GST_VIDEO_FORMAT_STV0:
866     case GST_VIDEO_FORMAT_STV1:
867 #endif
868       info->stride[0] = GST_ROUND_UP_4 (width);
869       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
870       info->stride[2] = info->stride[1];
871       info->offset[0] = 0;
872       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
873       cr_h = GST_ROUND_UP_2 (height) / 2;
874       if (GST_VIDEO_INFO_IS_INTERLACED (info))
875         cr_h = GST_ROUND_UP_2 (cr_h);
876       info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
877       info->size = info->offset[2] + info->stride[2] * cr_h;
878       break;
879     case GST_VIDEO_FORMAT_Y41B:
880       info->stride[0] = GST_ROUND_UP_4 (width);
881       info->stride[1] = GST_ROUND_UP_16 (width) / 4;
882       info->stride[2] = info->stride[1];
883       info->offset[0] = 0;
884       info->offset[1] = info->stride[0] * height;
885       info->offset[2] = info->offset[1] + info->stride[1] * height;
886       /* simplification of ROUNDUP4(w)*h + 2*((ROUNDUP16(w)/4)*h */
887       info->size = (info->stride[0] + (GST_ROUND_UP_16 (width) / 2)) * height;
888       break;
889     case GST_VIDEO_FORMAT_Y42B:
890       info->stride[0] = GST_ROUND_UP_4 (width);
891       info->stride[1] = GST_ROUND_UP_8 (width) / 2;
892       info->stride[2] = info->stride[1];
893       info->offset[0] = 0;
894       info->offset[1] = info->stride[0] * height;
895       info->offset[2] = info->offset[1] + info->stride[1] * height;
896       /* simplification of ROUNDUP4(w)*h + 2*(ROUNDUP8(w)/2)*h */
897       info->size = (info->stride[0] + GST_ROUND_UP_8 (width)) * height;
898       break;
899     case GST_VIDEO_FORMAT_Y444:
900     case GST_VIDEO_FORMAT_GBR:
901       info->stride[0] = GST_ROUND_UP_4 (width);
902       info->stride[1] = info->stride[0];
903       info->stride[2] = info->stride[0];
904       info->offset[0] = 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;
908       break;
909     case GST_VIDEO_FORMAT_GBRA:
910       info->stride[0] = GST_ROUND_UP_4 (width);
911       info->stride[1] = info->stride[0];
912       info->stride[2] = info->stride[0];
913       info->stride[3] = info->stride[0];
914       info->offset[0] = 0;
915       info->offset[1] = info->stride[0] * height;
916       info->offset[2] = info->offset[1] * 2;
917       info->offset[3] = info->offset[1] * 3;
918       info->size = info->stride[0] * height * 4;
919       break;
920     case GST_VIDEO_FORMAT_NV12:
921 #ifdef TIZEN_FEATURE_VIDEO_MODIFICATION
922     case GST_VIDEO_FORMAT_SN12:
923     case GST_VIDEO_FORMAT_ST12:
924     case GST_VIDEO_FORMAT_SN21:
925 #endif
926     case GST_VIDEO_FORMAT_NV21:
927       info->stride[0] = GST_ROUND_UP_4 (width);
928       info->stride[1] = info->stride[0];
929       info->offset[0] = 0;
930       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
931 #ifdef TIZEN_FEATURE_VIDEO_MODIFICATION
932       info->size = info->stride[0] * GST_ROUND_UP_2 (height) * 3 / 2;
933 #else
934       cr_h = GST_ROUND_UP_2 (height) / 2;
935       if (GST_VIDEO_INFO_IS_INTERLACED (info))
936         cr_h = GST_ROUND_UP_2 (cr_h);
937       info->size = info->offset[1] + info->stride[0] * cr_h;
938 #endif
939       break;
940     case GST_VIDEO_FORMAT_NV16:
941     case GST_VIDEO_FORMAT_NV61:
942       info->stride[0] = GST_ROUND_UP_4 (width);
943       info->stride[1] = info->stride[0];
944       info->offset[0] = 0;
945       info->offset[1] = info->stride[0] * height;
946       info->size = info->stride[0] * height * 2;
947       break;
948     case GST_VIDEO_FORMAT_NV24:
949       info->stride[0] = GST_ROUND_UP_4 (width);
950       info->stride[1] = GST_ROUND_UP_4 (width * 2);
951       info->offset[0] = 0;
952       info->offset[1] = info->stride[0] * height;
953       info->size = info->stride[0] * height + info->stride[1] * height;
954       break;
955     case GST_VIDEO_FORMAT_A420:
956       info->stride[0] = GST_ROUND_UP_4 (width);
957       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
958       info->stride[2] = info->stride[1];
959       info->stride[3] = info->stride[0];
960       info->offset[0] = 0;
961       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
962       cr_h = GST_ROUND_UP_2 (height) / 2;
963       if (GST_VIDEO_INFO_IS_INTERLACED (info))
964         cr_h = GST_ROUND_UP_2 (cr_h);
965       info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
966       info->offset[3] = info->offset[2] + info->stride[2] * cr_h;
967       info->size = info->offset[3] + info->stride[0] * GST_ROUND_UP_2 (height);
968       break;
969     case GST_VIDEO_FORMAT_YUV9:
970     case GST_VIDEO_FORMAT_YVU9:
971       info->stride[0] = GST_ROUND_UP_4 (width);
972       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
973       info->stride[2] = info->stride[1];
974       info->offset[0] = 0;
975       info->offset[1] = info->stride[0] * height;
976       cr_h = GST_ROUND_UP_4 (height) / 4;
977       if (GST_VIDEO_INFO_IS_INTERLACED (info))
978         cr_h = GST_ROUND_UP_2 (cr_h);
979       info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
980       info->size = info->offset[2] + info->stride[2] * cr_h;
981       break;
982     case GST_VIDEO_FORMAT_I420_10LE:
983     case GST_VIDEO_FORMAT_I420_10BE:
984     case GST_VIDEO_FORMAT_I420_12LE:
985     case GST_VIDEO_FORMAT_I420_12BE:
986       info->stride[0] = GST_ROUND_UP_4 (width * 2);
987       info->stride[1] = GST_ROUND_UP_4 (width);
988       info->stride[2] = info->stride[1];
989       info->offset[0] = 0;
990       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
991       cr_h = GST_ROUND_UP_2 (height) / 2;
992       if (GST_VIDEO_INFO_IS_INTERLACED (info))
993         cr_h = GST_ROUND_UP_2 (cr_h);
994       info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
995       info->size = info->offset[2] + info->stride[2] * cr_h;
996       break;
997     case GST_VIDEO_FORMAT_I422_10LE:
998     case GST_VIDEO_FORMAT_I422_10BE:
999     case GST_VIDEO_FORMAT_I422_12LE:
1000     case GST_VIDEO_FORMAT_I422_12BE:
1001       info->stride[0] = GST_ROUND_UP_4 (width * 2);
1002       info->stride[1] = GST_ROUND_UP_4 (width);
1003       info->stride[2] = info->stride[1];
1004       info->offset[0] = 0;
1005       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1006       info->offset[2] = info->offset[1] +
1007           info->stride[1] * GST_ROUND_UP_2 (height);
1008       info->size = info->offset[2] + info->stride[2] * GST_ROUND_UP_2 (height);
1009       break;
1010     case GST_VIDEO_FORMAT_Y444_10LE:
1011     case GST_VIDEO_FORMAT_Y444_10BE:
1012     case GST_VIDEO_FORMAT_Y444_12LE:
1013     case GST_VIDEO_FORMAT_Y444_12BE:
1014     case GST_VIDEO_FORMAT_GBR_10LE:
1015     case GST_VIDEO_FORMAT_GBR_10BE:
1016     case GST_VIDEO_FORMAT_GBR_12LE:
1017     case GST_VIDEO_FORMAT_GBR_12BE:
1018       info->stride[0] = GST_ROUND_UP_4 (width * 2);
1019       info->stride[1] = info->stride[0];
1020       info->stride[2] = info->stride[0];
1021       info->offset[0] = 0;
1022       info->offset[1] = info->stride[0] * height;
1023       info->offset[2] = info->offset[1] * 2;
1024       info->size = info->stride[0] * height * 3;
1025       break;
1026     case GST_VIDEO_FORMAT_GBRA_10LE:
1027     case GST_VIDEO_FORMAT_GBRA_10BE:
1028     case GST_VIDEO_FORMAT_GBRA_12LE:
1029     case GST_VIDEO_FORMAT_GBRA_12BE:
1030       info->stride[0] = GST_ROUND_UP_4 (width * 2);
1031       info->stride[1] = info->stride[0];
1032       info->stride[2] = info->stride[0];
1033       info->stride[3] = info->stride[0];
1034       info->offset[0] = 0;
1035       info->offset[1] = info->stride[0] * height;
1036       info->offset[2] = info->offset[1] * 2;
1037       info->offset[3] = info->offset[1] * 3;
1038       info->size = info->stride[0] * height * 4;
1039       break;
1040     case GST_VIDEO_FORMAT_NV12_64Z32:
1041       info->stride[0] =
1042           GST_VIDEO_TILE_MAKE_STRIDE (GST_ROUND_UP_128 (width) / 64,
1043           GST_ROUND_UP_32 (height) / 32);
1044       info->stride[1] =
1045           GST_VIDEO_TILE_MAKE_STRIDE (GST_ROUND_UP_128 (width) / 64,
1046           GST_ROUND_UP_64 (height) / 64);
1047       info->offset[0] = 0;
1048       info->offset[1] = GST_ROUND_UP_128 (width) * GST_ROUND_UP_32 (height);
1049       info->size = info->offset[1] +
1050           GST_ROUND_UP_128 (width) * GST_ROUND_UP_64 (height) / 2;
1051       break;
1052     case GST_VIDEO_FORMAT_A420_10LE:
1053     case GST_VIDEO_FORMAT_A420_10BE:
1054       info->stride[0] = GST_ROUND_UP_4 (width * 2);
1055       info->stride[1] = GST_ROUND_UP_4 (width);
1056       info->stride[2] = info->stride[1];
1057       info->stride[3] = info->stride[0];
1058       info->offset[0] = 0;
1059       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1060       cr_h = GST_ROUND_UP_2 (height) / 2;
1061       if (GST_VIDEO_INFO_IS_INTERLACED (info))
1062         cr_h = GST_ROUND_UP_2 (cr_h);
1063       info->offset[2] = info->offset[1] + info->stride[1] * cr_h;
1064       info->offset[3] = info->offset[2] + info->stride[2] * cr_h;
1065       info->size = info->offset[3] + info->stride[0] * GST_ROUND_UP_2 (height);
1066       break;
1067     case GST_VIDEO_FORMAT_A422_10LE:
1068     case GST_VIDEO_FORMAT_A422_10BE:
1069       info->stride[0] = GST_ROUND_UP_4 (width * 2);
1070       info->stride[1] = GST_ROUND_UP_4 (width);
1071       info->stride[2] = info->stride[1];
1072       info->stride[3] = info->stride[0];
1073       info->offset[0] = 0;
1074       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1075       info->offset[2] = info->offset[1] +
1076           info->stride[1] * GST_ROUND_UP_2 (height);
1077       info->offset[3] =
1078           info->offset[2] + info->stride[2] * GST_ROUND_UP_2 (height);
1079       info->size = info->offset[3] + info->stride[0] * GST_ROUND_UP_2 (height);
1080       break;
1081     case GST_VIDEO_FORMAT_A444_10LE:
1082     case GST_VIDEO_FORMAT_A444_10BE:
1083       info->stride[0] = GST_ROUND_UP_4 (width * 2);
1084       info->stride[1] = info->stride[0];
1085       info->stride[2] = info->stride[0];
1086       info->stride[3] = info->stride[0];
1087       info->offset[0] = 0;
1088       info->offset[1] = info->stride[0] * height;
1089       info->offset[2] = info->offset[1] * 2;
1090       info->offset[3] = info->offset[1] * 3;
1091       info->size = info->stride[0] * height * 4;
1092       break;
1093     case GST_VIDEO_FORMAT_P010_10LE:
1094     case GST_VIDEO_FORMAT_P010_10BE:
1095       info->stride[0] = GST_ROUND_UP_4 (width * 2);
1096       info->stride[1] = info->stride[0];
1097       info->offset[0] = 0;
1098       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1099       cr_h = GST_ROUND_UP_2 (height) / 2;
1100       info->size = info->offset[1] + info->stride[0] * cr_h;
1101       break;
1102     case GST_VIDEO_FORMAT_GRAY10_LE32:
1103       info->stride[0] = (width + 2) / 3 * 4;
1104       info->offset[0] = 0;
1105       info->size = info->stride[0] * GST_ROUND_UP_2 (height);
1106       break;
1107     case GST_VIDEO_FORMAT_NV12_10LE32:
1108       info->stride[0] = (width + 2) / 3 * 4;
1109       info->stride[1] = info->stride[0];
1110       info->offset[0] = 0;
1111       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1112       cr_h = GST_ROUND_UP_2 (height) / 2;
1113       if (GST_VIDEO_INFO_IS_INTERLACED (info))
1114         cr_h = GST_ROUND_UP_2 (cr_h);
1115       info->size = info->offset[1] + info->stride[0] * cr_h;
1116       break;
1117     case GST_VIDEO_FORMAT_NV16_10LE32:
1118       info->stride[0] = (width + 2) / 3 * 4;
1119       info->stride[1] = info->stride[0];
1120       info->offset[0] = 0;
1121       info->offset[1] = info->stride[0] * height;
1122       info->size = info->stride[0] * height * 2;
1123       break;
1124     case GST_VIDEO_FORMAT_NV12_10LE40:
1125       info->stride[0] = ((width * 5 >> 2) + 4) / 5 * 5;
1126       info->stride[1] = info->stride[0];
1127       info->offset[0] = 0;
1128       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1129       cr_h = GST_ROUND_UP_2 (height) / 2;
1130       if (GST_VIDEO_INFO_IS_INTERLACED (info))
1131         cr_h = GST_ROUND_UP_2 (cr_h);
1132       info->size = info->offset[1] + info->stride[0] * cr_h;
1133       break;
1134
1135     case GST_VIDEO_FORMAT_ENCODED:
1136       break;
1137     case GST_VIDEO_FORMAT_UNKNOWN:
1138 #ifdef TIZEN_FEATURE_VIDEO_MODIFICATION
1139     default:
1140 #endif
1141       GST_ERROR ("invalid format");
1142       g_warning ("invalid format");
1143       return FALSE;
1144       break;
1145   }
1146   return TRUE;
1147 }
1148
1149 /**
1150  * gst_video_info_convert:
1151  * @info: a #GstVideoInfo
1152  * @src_format: #GstFormat of the @src_value
1153  * @src_value: value to convert
1154  * @dest_format: #GstFormat of the @dest_value
1155  * @dest_value: (out): pointer to destination value
1156  *
1157  * Converts among various #GstFormat types.  This function handles
1158  * GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT.  For
1159  * raw video, GST_FORMAT_DEFAULT corresponds to video frames.  This
1160  * function can be used to handle pad queries of the type GST_QUERY_CONVERT.
1161  *
1162  * Returns: TRUE if the conversion was successful.
1163  */
1164 gboolean
1165 gst_video_info_convert (GstVideoInfo * info,
1166     GstFormat src_format, gint64 src_value,
1167     GstFormat dest_format, gint64 * dest_value)
1168 {
1169   gboolean ret = FALSE;
1170   int fps_n, fps_d;
1171   gsize size;
1172
1173   g_return_val_if_fail (info != NULL, 0);
1174   g_return_val_if_fail (info->finfo != NULL, 0);
1175   g_return_val_if_fail (info->finfo->format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1176   g_return_val_if_fail (info->size > 0, 0);
1177
1178   size = info->size;
1179   fps_n = info->fps_n;
1180   fps_d = info->fps_d;
1181
1182   GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s to %s",
1183       src_value, gst_format_get_name (src_format),
1184       gst_format_get_name (dest_format));
1185
1186   if (src_format == dest_format) {
1187     *dest_value = src_value;
1188     ret = TRUE;
1189     goto done;
1190   }
1191
1192   if (src_value == -1) {
1193     *dest_value = -1;
1194     ret = TRUE;
1195     goto done;
1196   }
1197
1198   /* bytes to frames */
1199   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_DEFAULT) {
1200     if (size != 0) {
1201       *dest_value = gst_util_uint64_scale (src_value, 1, size);
1202     } else {
1203       GST_ERROR ("blocksize is 0");
1204       *dest_value = 0;
1205     }
1206     ret = TRUE;
1207     goto done;
1208   }
1209
1210   /* frames to bytes */
1211   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_BYTES) {
1212     *dest_value = gst_util_uint64_scale (src_value, size, 1);
1213     ret = TRUE;
1214     goto done;
1215   }
1216
1217   /* time to frames */
1218   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_DEFAULT) {
1219     if (fps_d != 0) {
1220       *dest_value = gst_util_uint64_scale (src_value,
1221           fps_n, GST_SECOND * fps_d);
1222     } else {
1223       GST_ERROR ("framerate denominator is 0");
1224       *dest_value = 0;
1225     }
1226     ret = TRUE;
1227     goto done;
1228   }
1229
1230   /* frames to time */
1231   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) {
1232     if (fps_n != 0) {
1233       *dest_value = gst_util_uint64_scale (src_value,
1234           GST_SECOND * fps_d, fps_n);
1235     } else {
1236       GST_ERROR ("framerate numerator is 0");
1237       *dest_value = 0;
1238     }
1239     ret = TRUE;
1240     goto done;
1241   }
1242
1243   /* time to bytes */
1244   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
1245     if (fps_d != 0) {
1246       *dest_value = gst_util_uint64_scale (src_value,
1247           fps_n * size, GST_SECOND * fps_d);
1248     } else {
1249       GST_ERROR ("framerate denominator is 0");
1250       *dest_value = 0;
1251     }
1252     ret = TRUE;
1253     goto done;
1254   }
1255
1256   /* bytes to time */
1257   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
1258     if (fps_n != 0 && size != 0) {
1259       *dest_value = gst_util_uint64_scale (src_value,
1260           GST_SECOND * fps_d, fps_n * size);
1261     } else {
1262       GST_ERROR ("framerate denominator and/or blocksize is 0");
1263       *dest_value = 0;
1264     }
1265     ret = TRUE;
1266   }
1267
1268 done:
1269
1270   GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, ret, *dest_value);
1271
1272   return ret;
1273 }
1274
1275 /**
1276  * gst_video_info_align:
1277  * @info: a #GstVideoInfo
1278  * @align: alignment parameters
1279  *
1280  * Adjust the offset and stride fields in @info so that the padding and
1281  * stride alignment in @align is respected.
1282  *
1283  * Extra padding will be added to the right side when stride alignment padding
1284  * is required and @align will be updated with the new padding values.
1285  *
1286  * Returns: %FALSE if alignment could not be applied, e.g. because the
1287  *   size of a frame can't be represented as a 32 bit integer (Since: 1.12)
1288  */
1289 gboolean
1290 gst_video_info_align (GstVideoInfo * info, GstVideoAlignment * align)
1291 {
1292   const GstVideoFormatInfo *vinfo = info->finfo;
1293   gint width, height;
1294   gint padded_width, padded_height;
1295   gint i, n_planes;
1296   gboolean aligned;
1297
1298   width = GST_VIDEO_INFO_WIDTH (info);
1299   height = GST_VIDEO_INFO_HEIGHT (info);
1300
1301   GST_LOG ("padding %u-%ux%u-%u", align->padding_top,
1302       align->padding_left, align->padding_right, align->padding_bottom);
1303
1304   n_planes = GST_VIDEO_INFO_N_PLANES (info);
1305
1306   if (GST_VIDEO_FORMAT_INFO_HAS_PALETTE (vinfo))
1307     n_planes--;
1308
1309   /* first make sure the left padding does not cause alignment problems later */
1310   do {
1311     GST_LOG ("left padding %u", align->padding_left);
1312     aligned = TRUE;
1313     for (i = 0; i < n_planes; i++) {
1314       gint hedge;
1315
1316       /* this is the amout of pixels to add as left padding */
1317       hedge = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (vinfo, i, align->padding_left);
1318       hedge *= GST_VIDEO_FORMAT_INFO_PSTRIDE (vinfo, i);
1319
1320       GST_LOG ("plane %d, padding %d, alignment %u", i, hedge,
1321           align->stride_align[i]);
1322       aligned &= (hedge & align->stride_align[i]) == 0;
1323     }
1324     if (aligned)
1325       break;
1326
1327     GST_LOG ("unaligned padding, increasing padding");
1328     /* increase padded_width */
1329     align->padding_left += align->padding_left & ~(align->padding_left - 1);
1330   } while (!aligned);
1331
1332   /* add the padding */
1333   padded_width = width + align->padding_left + align->padding_right;
1334   padded_height = height + align->padding_top + align->padding_bottom;
1335
1336   do {
1337     GST_LOG ("padded dimension %u-%u", padded_width, padded_height);
1338
1339     info->width = padded_width;
1340     info->height = padded_height;
1341
1342     if (!fill_planes (info))
1343       return FALSE;
1344
1345     /* check alignment */
1346     aligned = TRUE;
1347     for (i = 0; i < n_planes; i++) {
1348       GST_LOG ("plane %d, stride %d, alignment %u", i, info->stride[i],
1349           align->stride_align[i]);
1350       aligned &= (info->stride[i] & align->stride_align[i]) == 0;
1351     }
1352     if (aligned)
1353       break;
1354
1355     GST_LOG ("unaligned strides, increasing dimension");
1356     /* increase padded_width */
1357     padded_width += padded_width & ~(padded_width - 1);
1358   } while (!aligned);
1359
1360   align->padding_right = padded_width - width - align->padding_left;
1361
1362   info->width = width;
1363   info->height = height;
1364
1365   for (i = 0; i < n_planes; i++) {
1366     gint vedge, hedge, comp;
1367
1368     /* Find the component for this plane, FIXME, we assume the plane number and
1369      * component number is the same for now, for scaling the dimensions this is
1370      * currently true for all formats but it might not be when adding new
1371      * formats. We might need to add a plane subsamling in the format info to
1372      * make this more generic or maybe use a plane -> component mapping. */
1373     comp = i;
1374
1375     hedge =
1376         GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (vinfo, comp, align->padding_left);
1377     vedge =
1378         GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vinfo, comp, align->padding_top);
1379
1380     GST_DEBUG ("plane %d: comp: %d, hedge %d vedge %d align %d stride %d", i,
1381         comp, hedge, vedge, align->stride_align[i], info->stride[i]);
1382
1383     info->offset[i] += (vedge * info->stride[i]) +
1384         (hedge * GST_VIDEO_FORMAT_INFO_PSTRIDE (vinfo, comp));
1385   }
1386
1387   return TRUE;
1388 }