video: rework part 1
[platform/upstream/gstreamer.git] / gst-libs / gst / video / video.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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25
26 #include <string.h>
27
28 #include "video.h"
29
30 typedef enum
31 {
32   VIDEO_FLAG_YUV = (1 << 0),
33   VIDEO_FLAG_RGB = (1 << 1),
34   VIDEO_FLAG_GRAY = (1 << 2),
35   VIDEO_FLAG_ALPHA = (1 << 3)
36 } VideoFlags;
37
38 typedef struct
39 {
40   const gchar *fmt;
41   GstVideoFormat format;
42   guint32 fourcc;
43   VideoFlags flags;
44   guint depth[GST_VIDEO_MAX_PLANES];
45 } VideoFormat;
46
47 #define COMP0            { 0, 0, 0, 0 }
48 #define COMP8            { 8, 0, 0, 0 }
49 #define COMP888          { 8, 8, 8, 0 }
50 #define COMP8888         { 8, 8, 8, 8 }
51 #define COMP10_10_10     { 10, 10, 10, 0 }
52 #define COMP16           { 16, 0, 0, 0 }
53 #define COMP16_16_16     { 16, 0, 0, 0 }
54 #define COMP16_16_16_16  { 16, 0, 0, 0 }
55 #define COMP555          { 5, 5, 5, 0 }
56 #define COMP565          { 5, 6, 5, 0 }
57
58 #define MAKE_YUV_FORMAT(name, fourcc, comp) \
59  { G_STRINGIFY(name), GST_VIDEO_FORMAT_ ##name, fourcc, VIDEO_FLAG_YUV, comp }
60 #define MAKE_YUVA_FORMAT(name, fourcc, comp) \
61  { G_STRINGIFY(name), GST_VIDEO_FORMAT_ ##name, fourcc, VIDEO_FLAG_YUV | VIDEO_FLAG_ALPHA, comp }
62
63 #define MAKE_RGB_FORMAT(name, comp) \
64  { G_STRINGIFY(name), GST_VIDEO_FORMAT_ ##name, 0x00000000, VIDEO_FLAG_RGB, comp }
65 #define MAKE_RGBA_FORMAT(name, comp) \
66  { G_STRINGIFY(name), GST_VIDEO_FORMAT_ ##name, 0x00000000, VIDEO_FLAG_RGB | VIDEO_FLAG_ALPHA, comp }
67
68 #define MAKE_GRAY_FORMAT(name, comp) \
69  { G_STRINGIFY(name), GST_VIDEO_FORMAT_ ##name, 0x00000000, VIDEO_FLAG_GRAY, comp }
70
71 static VideoFormat formats[] = {
72   {"UNKNOWN", GST_VIDEO_FORMAT_UNKNOWN, 0x00000000, 0, COMP0},
73
74   MAKE_YUV_FORMAT (I420, GST_MAKE_FOURCC ('I', '4', '2', '0'), COMP888),
75   MAKE_YUV_FORMAT (YV12, GST_MAKE_FOURCC ('Y', 'V', '1', '2'), COMP888),
76   MAKE_YUV_FORMAT (YUY2, GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'), COMP888),
77   MAKE_YUV_FORMAT (UYVY, GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'), COMP888),
78   MAKE_YUVA_FORMAT (AYUV, GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'), COMP8888),
79   MAKE_RGB_FORMAT (RGBx, COMP888),
80   MAKE_RGB_FORMAT (BGRx, COMP888),
81   MAKE_RGB_FORMAT (xRGB, COMP888),
82   MAKE_RGB_FORMAT (xBGR, COMP888),
83   MAKE_RGBA_FORMAT (RGBA, COMP8888),
84   MAKE_RGBA_FORMAT (BGRA, COMP8888),
85   MAKE_RGBA_FORMAT (ARGB, COMP8888),
86   MAKE_RGBA_FORMAT (ABGR, COMP8888),
87   MAKE_RGB_FORMAT (RGB, COMP888),
88   MAKE_RGB_FORMAT (BGR, COMP888),
89
90   MAKE_YUV_FORMAT (Y41B, GST_MAKE_FOURCC ('Y', '4', '1', 'B'), COMP888),
91   MAKE_YUV_FORMAT (Y42B, GST_MAKE_FOURCC ('Y', '4', '2', 'B'), COMP888),
92   MAKE_YUV_FORMAT (YVYU, GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'), COMP888),
93   MAKE_YUV_FORMAT (Y444, GST_MAKE_FOURCC ('Y', '4', '4', '4'), COMP888),
94   MAKE_YUV_FORMAT (v210, GST_MAKE_FOURCC ('v', '2', '1', '0'), COMP10_10_10),
95   MAKE_YUV_FORMAT (v216, GST_MAKE_FOURCC ('v', '2', '1', '6'), COMP16_16_16),
96   MAKE_YUV_FORMAT (NV12, GST_MAKE_FOURCC ('N', 'V', '1', '2'), COMP888),
97   MAKE_YUV_FORMAT (NV21, GST_MAKE_FOURCC ('N', 'V', '2', '1'), COMP888),
98
99   MAKE_GRAY_FORMAT (GRAY8, COMP8),
100   MAKE_GRAY_FORMAT (GRAY16_BE, COMP16),
101   MAKE_GRAY_FORMAT (GRAY16_LE, COMP16),
102
103   MAKE_YUV_FORMAT (v308, GST_MAKE_FOURCC ('v', '3', '0', '8'), COMP888),
104   MAKE_YUV_FORMAT (Y800, GST_MAKE_FOURCC ('Y', '8', '0', '0'), COMP8),
105   MAKE_YUV_FORMAT (Y16, GST_MAKE_FOURCC ('Y', '1', '6', ' '), COMP16),
106
107   MAKE_RGB_FORMAT (RGB16, COMP565),
108   MAKE_RGB_FORMAT (BGR16, COMP565),
109   MAKE_RGB_FORMAT (RGB15, COMP555),
110   MAKE_RGB_FORMAT (BGR15, COMP555),
111
112   MAKE_YUV_FORMAT (UYVP, GST_MAKE_FOURCC ('U', 'Y', 'V', 'P'), COMP10_10_10),
113   MAKE_YUVA_FORMAT (A420, GST_MAKE_FOURCC ('A', '4', '2', '0'), COMP888),
114   MAKE_RGBA_FORMAT (RGB8_PALETTED, COMP8888),
115   MAKE_YUV_FORMAT (YUV9, GST_MAKE_FOURCC ('Y', 'U', 'V', '9'), COMP888),
116   MAKE_YUV_FORMAT (YVU9, GST_MAKE_FOURCC ('Y', 'V', 'U', '9'), COMP888),
117   MAKE_YUV_FORMAT (IYU1, GST_MAKE_FOURCC ('I', 'Y', 'U', '1'), COMP888),
118   MAKE_RGBA_FORMAT (ARGB64, COMP16_16_16_16),
119   MAKE_YUVA_FORMAT (AYUV64, 0x00000000, COMP16_16_16_16),
120   MAKE_YUV_FORMAT (r210, GST_MAKE_FOURCC ('r', '2', '1', '0'), COMP10_10_10),
121 };
122
123 /**
124  * SECTION:gstvideo
125  * @short_description: Support library for video operations
126  *
127  * <refsect2>
128  * <para>
129  * This library contains some helper functions and includes the
130  * videosink and videofilter base classes.
131  * </para>
132  * </refsect2>
133  */
134
135 /**
136  * gst_video_frame_rate:
137  * @pad: pointer to a #GstPad
138  *
139  * A convenience function to retrieve a GValue holding the framerate
140  * from the caps on a pad.
141  *
142  * The pad needs to have negotiated caps containing a framerate property.
143  *
144  * Returns: NULL if the pad has no configured caps or the configured caps
145  * do not contain a framerate.
146  *
147  */
148 const GValue *
149 gst_video_frame_rate (GstPad * pad)
150 {
151   const GValue *fps;
152   gchar *fps_string;
153   GstCaps *caps = NULL;
154   GstStructure *structure;
155
156   /* get pad caps */
157   caps = gst_pad_get_current_caps (pad);
158   if (caps == NULL)
159     goto no_caps;
160
161   structure = gst_caps_get_structure (caps, 0);
162   if ((fps = gst_structure_get_value (structure, "framerate")) == NULL)
163     goto no_framerate;
164
165   if (!GST_VALUE_HOLDS_FRACTION (fps))
166     goto no_fraction;
167
168   fps_string = gst_value_serialize (fps);
169   GST_DEBUG ("Framerate request on pad %s:%s: %s",
170       GST_DEBUG_PAD_NAME (pad), fps_string);
171   g_free (fps_string);
172
173   gst_caps_unref (caps);
174
175   return fps;
176
177   /* ERRORS */
178 no_caps:
179   {
180     g_warning ("gstvideo: failed to get caps of pad %s:%s",
181         GST_DEBUG_PAD_NAME (pad));
182     return NULL;
183   }
184 no_framerate:
185   {
186     g_warning ("gstvideo: failed to get framerate property of pad %s:%s",
187         GST_DEBUG_PAD_NAME (pad));
188     gst_caps_unref (caps);
189     return NULL;
190   }
191 no_fraction:
192   {
193     g_warning
194         ("gstvideo: framerate property of pad %s:%s is not of type Fraction",
195         GST_DEBUG_PAD_NAME (pad));
196     gst_caps_unref (caps);
197     return NULL;
198   }
199 }
200
201 /**
202  * gst_video_calculate_display_ratio:
203  * @dar_n: Numerator of the calculated display_ratio
204  * @dar_d: Denominator of the calculated display_ratio
205  * @video_width: Width of the video frame in pixels
206  * @video_height: Height of the video frame in pixels
207  * @video_par_n: Numerator of the pixel aspect ratio of the input video.
208  * @video_par_d: Denominator of the pixel aspect ratio of the input video.
209  * @display_par_n: Numerator of the pixel aspect ratio of the display device
210  * @display_par_d: Denominator of the pixel aspect ratio of the display device
211  *
212  * Given the Pixel Aspect Ratio and size of an input video frame, and the
213  * pixel aspect ratio of the intended display device, calculates the actual
214  * display ratio the video will be rendered with.
215  *
216  * Returns: A boolean indicating success and a calculated Display Ratio in the
217  * dar_n and dar_d parameters.
218  * The return value is FALSE in the case of integer overflow or other error.
219  *
220  * Since: 0.10.7
221  */
222 gboolean
223 gst_video_calculate_display_ratio (guint * dar_n, guint * dar_d,
224     guint video_width, guint video_height,
225     guint video_par_n, guint video_par_d,
226     guint display_par_n, guint display_par_d)
227 {
228   gint num, den;
229   gint tmp_n, tmp_d;
230
231   g_return_val_if_fail (dar_n != NULL, FALSE);
232   g_return_val_if_fail (dar_d != NULL, FALSE);
233
234   /* Calculate (video_width * video_par_n * display_par_d) /
235    * (video_height * video_par_d * display_par_n) */
236   if (!gst_util_fraction_multiply (video_width, video_height, video_par_n,
237           video_par_d, &tmp_n, &tmp_d))
238     goto error_overflow;
239
240   if (!gst_util_fraction_multiply (tmp_n, tmp_d, display_par_d, display_par_n,
241           &num, &den))
242     goto error_overflow;
243
244   g_return_val_if_fail (num > 0, FALSE);
245   g_return_val_if_fail (den > 0, FALSE);
246
247   *dar_n = num;
248   *dar_d = den;
249
250   return TRUE;
251
252   /* ERRORS */
253 error_overflow:
254   {
255     GST_WARNING ("overflow in multiply");
256     return FALSE;
257   }
258 }
259
260 /**
261  * gst_video_format_parse_caps_interlaced:
262  * @caps: the fixed #GstCaps to parse
263  * @interlaced: whether @caps represents interlaced video or not, may be NULL (output)
264  *
265  * Extracts whether the caps represents interlaced content or not and places it
266  * in @interlaced.
267  *
268  * Since: 0.10.23
269  *
270  * Returns: TRUE if @caps was parsed correctly.
271  */
272 gboolean
273 gst_video_format_parse_caps_interlaced (GstCaps * caps, gboolean * interlaced)
274 {
275   GstStructure *structure;
276
277   if (!gst_caps_is_fixed (caps))
278     return FALSE;
279
280   structure = gst_caps_get_structure (caps, 0);
281
282   if (interlaced) {
283     if (!gst_structure_get_boolean (structure, "interlaced", interlaced))
284       *interlaced = FALSE;
285   }
286
287   return TRUE;
288 }
289
290 /**
291  * gst_video_parse_caps_color_matrix:
292  * @caps: the fixed #GstCaps to parse
293  *
294  * Extracts the color matrix used by the caps.  Possible values are
295  * "sdtv" for the standard definition color matrix (as specified in
296  * Rec. ITU-R BT.470-6) or "hdtv" for the high definition color
297  * matrix (as specified in Rec. ITU-R BT.709)
298  *
299  * Since: 0.10.29
300  *
301  * Returns: a color matrix string, or NULL if no color matrix could be
302  *     determined.
303  */
304 const char *
305 gst_video_parse_caps_color_matrix (GstCaps * caps)
306 {
307   GstStructure *structure;
308   const char *s;
309
310   if (!gst_caps_is_fixed (caps))
311     return NULL;
312
313   structure = gst_caps_get_structure (caps, 0);
314
315   s = gst_structure_get_string (structure, "color-matrix");
316   if (s)
317     return s;
318
319   if (gst_structure_has_name (structure, "video/x-raw-yuv")) {
320     return "sdtv";
321   }
322
323   return NULL;
324 }
325
326 /**
327  * gst_video_parse_caps_chroma_site:
328  * @caps: the fixed #GstCaps to parse
329  *
330  * Extracts the chroma site used by the caps.  Possible values are
331  * "mpeg2" for MPEG-2 style chroma siting (co-sited horizontally,
332  * halfway-sited vertically), "jpeg" for JPEG and Theora style
333  * chroma siting (halfway-sited both horizontally and vertically).
334  * Other chroma site values are possible, but uncommon.
335  *
336  * When no chroma site is specified in the caps, it should be assumed
337  * to be "mpeg2".
338  *
339  * Since: 0.10.29
340  *
341  * Returns: a chroma site string, or NULL if no chroma site could be
342  *     determined.
343  */
344 const char *
345 gst_video_parse_caps_chroma_site (GstCaps * caps)
346 {
347   GstStructure *structure;
348   const char *s;
349
350   if (!gst_caps_is_fixed (caps))
351     return NULL;
352
353   structure = gst_caps_get_structure (caps, 0);
354
355   s = gst_structure_get_string (structure, "chroma-site");
356   if (s)
357     return s;
358
359   if (gst_structure_has_name (structure, "video/x-raw-yuv")) {
360     return "mpeg2";
361   }
362
363   return NULL;
364 }
365
366 /**
367  * gst_video_parse_caps_framerate:
368  * @caps: pointer to a #GstCaps instance
369  * @fps_n: pointer to integer to hold numerator of frame rate (output)
370  * @fps_d: pointer to integer to hold denominator of frame rate (output)
371  *
372  * Extracts the frame rate from @caps and places the values in the locations
373  * pointed to by @fps_n and @fps_d.  Returns TRUE if the values could be
374  * parsed correctly, FALSE if not.
375  *
376  * This function can be used with #GstCaps that have any media type; it
377  * is not limited to formats handled by #GstVideoFormat.
378  *
379  * Since: 0.10.16
380  *
381  * Returns: TRUE if @caps was parsed correctly.
382  */
383 gboolean
384 gst_video_parse_caps_framerate (GstCaps * caps, int *fps_n, int *fps_d)
385 {
386   GstStructure *structure;
387
388   if (!gst_caps_is_fixed (caps))
389     return FALSE;
390
391   structure = gst_caps_get_structure (caps, 0);
392
393   return gst_structure_get_fraction (structure, "framerate", fps_n, fps_d);
394 }
395
396 /**
397  * gst_video_parse_caps_pixel_aspect_ratio:
398  * @caps: pointer to a #GstCaps instance
399  * @par_n: pointer to numerator of pixel aspect ratio (output)
400  * @par_d: pointer to denominator of pixel aspect ratio (output)
401  *
402  * Extracts the pixel aspect ratio from @caps and places the values in
403  * the locations pointed to by @par_n and @par_d.  Returns TRUE if the
404  * values could be parsed correctly, FALSE if not.
405  *
406  * This function can be used with #GstCaps that have any media type; it
407  * is not limited to formats handled by #GstVideoFormat.
408  *
409  * Since: 0.10.16
410  *
411  * Returns: TRUE if @caps was parsed correctly.
412  */
413 gboolean
414 gst_video_parse_caps_pixel_aspect_ratio (GstCaps * caps, int *par_n, int *par_d)
415 {
416   GstStructure *structure;
417
418   if (!gst_caps_is_fixed (caps))
419     return FALSE;
420
421   structure = gst_caps_get_structure (caps, 0);
422
423   if (!gst_structure_get_fraction (structure, "pixel-aspect-ratio",
424           par_n, par_d)) {
425     *par_n = 1;
426     *par_d = 1;
427   }
428   return TRUE;
429 }
430
431 /**
432  * gst_video_format_new_caps_interlaced:
433  * @format: the #GstVideoFormat describing the raw video format
434  * @width: width of video
435  * @height: height of video
436  * @framerate_n: numerator of frame rate
437  * @framerate_d: denominator of frame rate
438  * @par_n: numerator of pixel aspect ratio
439  * @par_d: denominator of pixel aspect ratio
440  * @interlaced: #TRUE if the format is interlaced
441  *
442  * Creates a new #GstCaps object based on the parameters provided.
443  *
444  * Since: 0.10.23
445  *
446  * Returns: a new #GstCaps object, or NULL if there was an error
447  */
448 GstCaps *
449 gst_video_format_new_caps_interlaced (GstVideoFormat format,
450     int width, int height, int framerate_n, int framerate_d, int par_n,
451     int par_d, gboolean interlaced)
452 {
453   GstCaps *res;
454
455   res =
456       gst_video_format_new_caps (format, width, height, framerate_n,
457       framerate_d, par_n, par_d);
458   if (interlaced && (res != NULL))
459     gst_caps_set_simple (res, "interlaced", G_TYPE_BOOLEAN, TRUE, NULL);
460
461   return res;
462 }
463
464 static GstCaps *
465 gst_video_format_new_caps_raw (GstVideoFormat format)
466 {
467   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
468
469   return gst_caps_new_simple ("video/x-raw",
470       "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
471 }
472
473 /**
474  * gst_video_format_new_template_caps:
475  * @format: the #GstVideoFormat describing the raw video format
476  *
477  * Creates a new #GstCaps object based on the parameters provided.
478  * Size, frame rate, and pixel aspect ratio are set to the full
479  * range.
480  *
481  * Since: 0.10.33
482  *
483  * Returns: a new #GstCaps object, or NULL if there was an error
484  */
485 GstCaps *
486 gst_video_format_new_template_caps (GstVideoFormat format)
487 {
488   GstCaps *caps;
489   GstStructure *structure;
490
491   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
492
493   caps = gst_video_format_new_caps_raw (format);
494   if (caps) {
495     GValue value = { 0 };
496     GValue v = { 0 };
497
498     structure = gst_caps_get_structure (caps, 0);
499
500     gst_structure_set (structure,
501         "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
502         "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
503         "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1,
504         "pixel-aspect-ratio", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
505
506     g_value_init (&value, GST_TYPE_LIST);
507     g_value_init (&v, G_TYPE_BOOLEAN);
508     g_value_set_boolean (&v, TRUE);
509     gst_value_list_append_value (&value, &v);
510     g_value_set_boolean (&v, FALSE);
511     gst_value_list_append_value (&value, &v);
512
513     gst_structure_set_value (structure, "interlaced", &value);
514
515     g_value_reset (&value);
516     g_value_reset (&v);
517   }
518
519   return caps;
520 }
521
522 /**
523  * gst_video_format_new_caps:
524  * @format: the #GstVideoFormat describing the raw video format
525  * @width: width of video
526  * @height: height of video
527  * @framerate_n: numerator of frame rate
528  * @framerate_d: denominator of frame rate
529  * @par_n: numerator of pixel aspect ratio
530  * @par_d: denominator of pixel aspect ratio
531  *
532  * Creates a new #GstCaps object based on the parameters provided.
533  *
534  * Since: 0.10.16
535  *
536  * Returns: a new #GstCaps object, or NULL if there was an error
537  */
538 GstCaps *
539 gst_video_format_new_caps (GstVideoFormat format, int width,
540     int height, int framerate_n, int framerate_d, int par_n, int par_d)
541 {
542   GstCaps *caps;
543   GstStructure *structure;
544
545   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
546   g_return_val_if_fail (width > 0 && height > 0, NULL);
547
548   caps = gst_video_format_new_caps_raw (format);
549   if (caps) {
550     structure = gst_caps_get_structure (caps, 0);
551
552     gst_structure_set (structure,
553         "width", G_TYPE_INT, width,
554         "height", G_TYPE_INT, height,
555         "framerate", GST_TYPE_FRACTION, framerate_n, framerate_d,
556         "pixel-aspect-ratio", GST_TYPE_FRACTION, par_n, par_d, NULL);
557   }
558
559   return caps;
560 }
561
562
563 static GstVideoFormat
564 gst_video_format_from_rgb32_masks (int red_mask, int green_mask, int blue_mask)
565 {
566   if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
567       blue_mask == 0x0000ff00) {
568     return GST_VIDEO_FORMAT_RGBx;
569   }
570   if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
571       blue_mask == 0xff000000) {
572     return GST_VIDEO_FORMAT_BGRx;
573   }
574   if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
575       blue_mask == 0x000000ff) {
576     return GST_VIDEO_FORMAT_xRGB;
577   }
578   if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
579       blue_mask == 0x00ff0000) {
580     return GST_VIDEO_FORMAT_xBGR;
581   }
582
583   return GST_VIDEO_FORMAT_UNKNOWN;
584 }
585
586 static GstVideoFormat
587 gst_video_format_from_rgba32_masks (int red_mask, int green_mask,
588     int blue_mask, int alpha_mask)
589 {
590   if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
591       blue_mask == 0x0000ff00 && alpha_mask == 0x000000ff) {
592     return GST_VIDEO_FORMAT_RGBA;
593   }
594   if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
595       blue_mask == 0xff000000 && alpha_mask == 0x000000ff) {
596     return GST_VIDEO_FORMAT_BGRA;
597   }
598   if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
599       blue_mask == 0x000000ff && alpha_mask == 0xff000000) {
600     return GST_VIDEO_FORMAT_ARGB;
601   }
602   if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
603       blue_mask == 0x00ff0000 && alpha_mask == 0xff000000) {
604     return GST_VIDEO_FORMAT_ABGR;
605   }
606   return GST_VIDEO_FORMAT_UNKNOWN;
607 }
608
609 static GstVideoFormat
610 gst_video_format_from_rgb24_masks (int red_mask, int green_mask, int blue_mask)
611 {
612   if (red_mask == 0xff0000 && green_mask == 0x00ff00 && blue_mask == 0x0000ff) {
613     return GST_VIDEO_FORMAT_RGB;
614   }
615   if (red_mask == 0x0000ff && green_mask == 0x00ff00 && blue_mask == 0xff0000) {
616     return GST_VIDEO_FORMAT_BGR;
617   }
618
619   return GST_VIDEO_FORMAT_UNKNOWN;
620 }
621
622 #define GST_VIDEO_COMP1_MASK_16_INT 0xf800
623 #define GST_VIDEO_COMP2_MASK_16_INT 0x07e0
624 #define GST_VIDEO_COMP3_MASK_16_INT 0x001f
625
626 #define GST_VIDEO_COMP1_MASK_15_INT 0x7c00
627 #define GST_VIDEO_COMP2_MASK_15_INT 0x03e0
628 #define GST_VIDEO_COMP3_MASK_15_INT 0x001f
629
630 static GstVideoFormat
631 gst_video_format_from_rgb16_masks (int red_mask, int green_mask, int blue_mask)
632 {
633   if (red_mask == GST_VIDEO_COMP1_MASK_16_INT
634       && green_mask == GST_VIDEO_COMP2_MASK_16_INT
635       && blue_mask == GST_VIDEO_COMP3_MASK_16_INT) {
636     return GST_VIDEO_FORMAT_RGB16;
637   }
638   if (red_mask == GST_VIDEO_COMP3_MASK_16_INT
639       && green_mask == GST_VIDEO_COMP2_MASK_16_INT
640       && blue_mask == GST_VIDEO_COMP1_MASK_16_INT) {
641     return GST_VIDEO_FORMAT_BGR16;
642   }
643   if (red_mask == GST_VIDEO_COMP1_MASK_15_INT
644       && green_mask == GST_VIDEO_COMP2_MASK_15_INT
645       && blue_mask == GST_VIDEO_COMP3_MASK_15_INT) {
646     return GST_VIDEO_FORMAT_RGB15;
647   }
648   if (red_mask == GST_VIDEO_COMP3_MASK_15_INT
649       && green_mask == GST_VIDEO_COMP2_MASK_15_INT
650       && blue_mask == GST_VIDEO_COMP1_MASK_15_INT) {
651     return GST_VIDEO_FORMAT_BGR15;
652   }
653   return GST_VIDEO_FORMAT_UNKNOWN;
654 }
655
656 GstVideoFormat
657 gst_video_format_from_masks (gint depth, gint bpp, gint endianness,
658     gint red_mask, gint green_mask, gint blue_mask, gint alpha_mask)
659 {
660   GstVideoFormat format;
661
662   /* our caps system handles 24/32bpp RGB as big-endian. */
663   if ((bpp == 24 || bpp == 32) && endianness == G_LITTLE_ENDIAN) {
664     red_mask = GUINT32_TO_BE (red_mask);
665     green_mask = GUINT32_TO_BE (green_mask);
666     blue_mask = GUINT32_TO_BE (blue_mask);
667     endianness = G_BIG_ENDIAN;
668     if (bpp == 24) {
669       red_mask >>= 8;
670       green_mask >>= 8;
671       blue_mask >>= 8;
672     }
673   }
674
675   if (depth == 30 && bpp == 32) {
676     format = GST_VIDEO_FORMAT_r210;
677   } else if (depth == 24 && bpp == 32) {
678     format = gst_video_format_from_rgb32_masks (red_mask, green_mask,
679         blue_mask);
680   } else if (depth == 32 && bpp == 32 && alpha_mask) {
681     format = gst_video_format_from_rgba32_masks (red_mask, green_mask,
682         blue_mask, alpha_mask);
683   } else if (depth == 24 && bpp == 24) {
684     format = gst_video_format_from_rgb24_masks (red_mask, green_mask,
685         blue_mask);
686   } else if ((depth == 15 || depth == 16) && bpp == 16 &&
687       endianness == G_BYTE_ORDER) {
688     format = gst_video_format_from_rgb16_masks (red_mask, green_mask,
689         blue_mask);
690   } else if (depth == 8 && bpp == 8) {
691     format = GST_VIDEO_FORMAT_RGB8_PALETTED;
692   } else if (depth == 64 && bpp == 64) {
693     format = gst_video_format_from_rgba32_masks (red_mask, green_mask,
694         blue_mask, alpha_mask);
695     if (format == GST_VIDEO_FORMAT_ARGB) {
696       format = GST_VIDEO_FORMAT_ARGB64;
697     } else {
698       format = GST_VIDEO_FORMAT_UNKNOWN;
699     }
700   } else {
701     format = GST_VIDEO_FORMAT_UNKNOWN;
702   }
703   return format;
704 }
705
706 /**
707  * gst_video_format_from_fourcc:
708  * @fourcc: a FOURCC value representing raw YUV video
709  *
710  * Converts a FOURCC value into the corresponding #GstVideoFormat.
711  * If the FOURCC cannot be represented by #GstVideoFormat,
712  * #GST_VIDEO_FORMAT_UNKNOWN is returned.
713  *
714  * Since: 0.10.16
715  *
716  * Returns: the #GstVideoFormat describing the FOURCC value
717  */
718 GstVideoFormat
719 gst_video_format_from_fourcc (guint32 fourcc)
720 {
721   switch (fourcc) {
722     case GST_MAKE_FOURCC ('I', '4', '2', '0'):
723       return GST_VIDEO_FORMAT_I420;
724     case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
725       return GST_VIDEO_FORMAT_YV12;
726     case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
727       return GST_VIDEO_FORMAT_YUY2;
728     case GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'):
729       return GST_VIDEO_FORMAT_YVYU;
730     case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
731       return GST_VIDEO_FORMAT_UYVY;
732     case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
733       return GST_VIDEO_FORMAT_AYUV;
734     case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
735       return GST_VIDEO_FORMAT_Y41B;
736     case GST_MAKE_FOURCC ('Y', '4', '2', 'B'):
737       return GST_VIDEO_FORMAT_Y42B;
738     case GST_MAKE_FOURCC ('Y', '4', '4', '4'):
739       return GST_VIDEO_FORMAT_Y444;
740     case GST_MAKE_FOURCC ('v', '2', '1', '0'):
741       return GST_VIDEO_FORMAT_v210;
742     case GST_MAKE_FOURCC ('v', '2', '1', '6'):
743       return GST_VIDEO_FORMAT_v216;
744     case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
745       return GST_VIDEO_FORMAT_NV12;
746     case GST_MAKE_FOURCC ('N', 'V', '2', '1'):
747       return GST_VIDEO_FORMAT_NV21;
748     case GST_MAKE_FOURCC ('v', '3', '0', '8'):
749       return GST_VIDEO_FORMAT_v308;
750     case GST_MAKE_FOURCC ('Y', '8', '0', '0'):
751     case GST_MAKE_FOURCC ('Y', '8', ' ', ' '):
752     case GST_MAKE_FOURCC ('G', 'R', 'E', 'Y'):
753       return GST_VIDEO_FORMAT_Y800;
754     case GST_MAKE_FOURCC ('Y', '1', '6', ' '):
755       return GST_VIDEO_FORMAT_Y16;
756     case GST_MAKE_FOURCC ('U', 'Y', 'V', 'P'):
757       return GST_VIDEO_FORMAT_UYVP;
758     case GST_MAKE_FOURCC ('A', '4', '2', '0'):
759       return GST_VIDEO_FORMAT_A420;
760     case GST_MAKE_FOURCC ('Y', 'U', 'V', '9'):
761       return GST_VIDEO_FORMAT_YUV9;
762     case GST_MAKE_FOURCC ('Y', 'V', 'U', '9'):
763       return GST_VIDEO_FORMAT_YVU9;
764     case GST_MAKE_FOURCC ('I', 'Y', 'U', '1'):
765       return GST_VIDEO_FORMAT_IYU1;
766     case GST_MAKE_FOURCC ('A', 'Y', '6', '4'):
767       return GST_VIDEO_FORMAT_AYUV64;
768     default:
769       return GST_VIDEO_FORMAT_UNKNOWN;
770   }
771 }
772
773 GstVideoFormat
774 gst_video_format_from_string (const gchar * format)
775 {
776   guint i;
777
778   for (i = 0; i < G_N_ELEMENTS (formats); i++) {
779     if (strcmp (formats[i].fmt, format) == 0)
780       return formats[i].format;
781   }
782   return GST_VIDEO_FORMAT_UNKNOWN;
783 }
784
785
786 /**
787  * gst_video_format_to_fourcc:
788  * @format: a #GstVideoFormat video format
789  *
790  * Converts a #GstVideoFormat value into the corresponding FOURCC.  Only
791  * a few YUV formats have corresponding FOURCC values.  If @format has
792  * no corresponding FOURCC value, 0 is returned.
793  *
794  * Since: 0.10.16
795  *
796  * Returns: the FOURCC corresponding to @format
797  */
798 guint32
799 gst_video_format_to_fourcc (GstVideoFormat format)
800 {
801   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
802
803   if (format >= G_N_ELEMENTS (formats))
804     return 0;
805
806   return formats[format].fourcc;
807 }
808
809 const gchar *
810 gst_video_format_to_string (GstVideoFormat format)
811 {
812   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
813
814   if (format >= G_N_ELEMENTS (formats))
815     return NULL;
816
817   return formats[format].fmt;
818 }
819
820 /**
821  * gst_video_format_is_rgb:
822  * @format: a #GstVideoFormat
823  *
824  * Determine whether the video format is an RGB format.
825  *
826  * Since: 0.10.16
827  *
828  * Returns: TRUE if @format represents RGB video
829  */
830 gboolean
831 gst_video_format_is_rgb (GstVideoFormat format)
832 {
833   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, FALSE);
834
835   if (format >= G_N_ELEMENTS (formats))
836     return FALSE;
837
838   return (formats[format].flags & VIDEO_FLAG_RGB) != 0;
839 }
840
841 /**
842  * gst_video_format_is_yuv:
843  * @format: a #GstVideoFormat
844  *
845  * Determine whether the video format is a YUV format.
846  *
847  * Since: 0.10.16
848  *
849  * Returns: TRUE if @format represents YUV video
850  */
851 gboolean
852 gst_video_format_is_yuv (GstVideoFormat format)
853 {
854   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, FALSE);
855
856   if (format >= G_N_ELEMENTS (formats))
857     return FALSE;
858
859   return (formats[format].flags & VIDEO_FLAG_YUV) != 0;
860 }
861
862 /**
863  * gst_video_format_is_gray:
864  * @format: a #GstVideoFormat
865  *
866  * Determine whether the video format is a grayscale format.
867  *
868  * Since: 0.10.29
869  *
870  * Returns: TRUE if @format represents grayscale video
871  */
872 gboolean
873 gst_video_format_is_gray (GstVideoFormat format)
874 {
875   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, FALSE);
876
877   if (format >= G_N_ELEMENTS (formats))
878     return FALSE;
879
880   return (formats[format].flags & VIDEO_FLAG_GRAY) != 0;
881 }
882
883 /**
884  * gst_video_format_has_alpha:
885  * @format: a #GstVideoFormat
886  *
887  * Returns TRUE or FALSE depending on if the video format provides an
888  * alpha channel.
889  *
890  * Since: 0.10.16
891  *
892  * Returns: TRUE if @format has an alpha channel
893  */
894 gboolean
895 gst_video_format_has_alpha (GstVideoFormat format)
896 {
897   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, FALSE);
898
899   if (format >= G_N_ELEMENTS (formats))
900     return FALSE;
901
902   return (formats[format].flags & VIDEO_FLAG_ALPHA) != 0;
903 }
904
905 /**
906  * gst_video_format_get_component_depth:
907  * @format: a #GstVideoFormat
908  * @component: the video component (e.g. 0 for 'R' in RGB)
909  *
910  * Returns the number of bits used to encode an individual pixel of
911  * a given @component.  Typically this is 8, although higher and lower
912  * values are possible for some formats.
913  *
914  * Since: 0.10.33
915  *
916  * Returns: depth of component
917  */
918 int
919 gst_video_format_get_component_depth (GstVideoFormat format, int component)
920 {
921   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
922   g_return_val_if_fail (component < GST_VIDEO_MAX_PLANES, 0);
923
924   if (format >= G_N_ELEMENTS (formats))
925     return FALSE;
926
927   return formats[format].depth[component];
928 }
929
930 /**
931  * gst_video_format_get_pixel_stride:
932  * @format: a #GstVideoFormat
933  * @component: the component index
934  *
935  * Calculates the pixel stride (number of bytes from one pixel to the
936  * pixel to its immediate left) for the video component with an index
937  * of @component.  See @gst_video_format_get_row_stride for a description
938  * of the component index.
939  *
940  * Since: 0.10.16
941  *
942  * Returns: pixel stride of component @component
943  */
944 int
945 gst_video_format_get_pixel_stride (GstVideoFormat format, int component)
946 {
947   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
948   g_return_val_if_fail (component >= 0 && component <= 3, 0);
949
950   switch (format) {
951     case GST_VIDEO_FORMAT_I420:
952     case GST_VIDEO_FORMAT_YV12:
953     case GST_VIDEO_FORMAT_Y41B:
954     case GST_VIDEO_FORMAT_Y42B:
955     case GST_VIDEO_FORMAT_Y444:
956     case GST_VIDEO_FORMAT_A420:
957     case GST_VIDEO_FORMAT_YUV9:
958     case GST_VIDEO_FORMAT_YVU9:
959       return 1;
960     case GST_VIDEO_FORMAT_YUY2:
961     case GST_VIDEO_FORMAT_YVYU:
962     case GST_VIDEO_FORMAT_UYVY:
963       if (component == 0) {
964         return 2;
965       } else {
966         return 4;
967       }
968     case GST_VIDEO_FORMAT_IYU1:
969       /* doesn't make much sense for IYU1 because it's 1 or 3
970        * for luma depending on position */
971       return 0;
972     case GST_VIDEO_FORMAT_AYUV:
973     case GST_VIDEO_FORMAT_RGBx:
974     case GST_VIDEO_FORMAT_BGRx:
975     case GST_VIDEO_FORMAT_xRGB:
976     case GST_VIDEO_FORMAT_xBGR:
977     case GST_VIDEO_FORMAT_RGBA:
978     case GST_VIDEO_FORMAT_BGRA:
979     case GST_VIDEO_FORMAT_ARGB:
980     case GST_VIDEO_FORMAT_ABGR:
981     case GST_VIDEO_FORMAT_r210:
982       return 4;
983     case GST_VIDEO_FORMAT_RGB16:
984     case GST_VIDEO_FORMAT_BGR16:
985     case GST_VIDEO_FORMAT_RGB15:
986     case GST_VIDEO_FORMAT_BGR15:
987       return 2;
988     case GST_VIDEO_FORMAT_RGB:
989     case GST_VIDEO_FORMAT_BGR:
990     case GST_VIDEO_FORMAT_v308:
991       return 3;
992     case GST_VIDEO_FORMAT_v210:
993       /* v210 is packed at the bit level, so pixel stride doesn't make sense */
994       return 0;
995     case GST_VIDEO_FORMAT_v216:
996       if (component == 0) {
997         return 4;
998       } else {
999         return 8;
1000       }
1001     case GST_VIDEO_FORMAT_NV12:
1002     case GST_VIDEO_FORMAT_NV21:
1003       if (component == 0) {
1004         return 1;
1005       } else {
1006         return 2;
1007       }
1008     case GST_VIDEO_FORMAT_GRAY8:
1009     case GST_VIDEO_FORMAT_Y800:
1010       return 1;
1011     case GST_VIDEO_FORMAT_GRAY16_BE:
1012     case GST_VIDEO_FORMAT_GRAY16_LE:
1013     case GST_VIDEO_FORMAT_Y16:
1014       return 2;
1015     case GST_VIDEO_FORMAT_UYVP:
1016       /* UYVP is packed at the bit level, so pixel stride doesn't make sense */
1017       return 0;
1018     case GST_VIDEO_FORMAT_RGB8_PALETTED:
1019       return 1;
1020     case GST_VIDEO_FORMAT_ARGB64:
1021     case GST_VIDEO_FORMAT_AYUV64:
1022       return 8;
1023     default:
1024       return 0;
1025   }
1026 }
1027
1028 /**
1029  * gst_video_info_init:
1030  * @info: a #GstVideoInfo
1031  *
1032  * Initialize @info with default values.
1033  */
1034 void
1035 gst_video_info_init (GstVideoInfo * info)
1036 {
1037   g_return_if_fail (info != NULL);
1038
1039   memset (info, 0, sizeof (GstVideoInfo));
1040 }
1041
1042 /**
1043  * gst_video_info_set_format:
1044  * @info: a #GstVideoInfo
1045  * @format: the format
1046  * @width: a width
1047  * @height: a height
1048  *
1049  * Set the default info for a video frame of @format and @width and @height.
1050  */
1051 void
1052 gst_video_info_set_format (GstVideoInfo * info, GstVideoFormat format,
1053     guint width, guint height)
1054 {
1055   g_return_if_fail (info != NULL);
1056   g_return_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN);
1057
1058   info->format = format;
1059   info->width = width;
1060   info->heigth = height;
1061 }
1062
1063 /**
1064  * gst_video_info_from_caps:
1065  * @info: a #GstVideoInfo
1066  * @caps: a #GstCaps
1067  *
1068  * Parse @caps and update @info.
1069  *
1070  * Returns: TRUE if @caps could be parsed
1071  */
1072 gboolean
1073 gst_video_info_from_caps (GstVideoInfo * info, const GstCaps * caps)
1074 {
1075   GstStructure *structure;
1076   const gchar *fmt;
1077   gboolean ok = TRUE;
1078   GstVideoFormat format;
1079   gint width, height g_return_val_if_fail (info != NULL, FALSE);
1080   g_return_val_if_fail (caps != NULL, FALSE);
1081
1082   if (!gst_caps_is_fixed (caps))
1083     return FALSE;
1084
1085   structure = gst_caps_get_structure (caps, 0);
1086
1087   if (!gst_structure_has_name (structure, "video/x-raw"))
1088     goto wrong_name;
1089
1090   if (!(fmt = gst_structure_get_string (structure, "format")))
1091     goto no_format;
1092
1093   format = gst_video_format_from_string (fmt);
1094   if (format == GST_VIDEO_FORMAT_UNKNOWN)
1095     goto unknown_format;
1096
1097   info->format = format;
1098
1099   if (gst_structure_get_int (structure, "width", &width))
1100     info->width = width;
1101   if (gst_structure_get_int (structure, "height", &height))
1102     info->height = height;
1103
1104   return TRUE;
1105
1106   /* ERROR */
1107 wrong_name:
1108   {
1109     return FALSE;
1110   }
1111 no_format:
1112   {
1113     return FALSE;
1114   }
1115 unknown_format:
1116   {
1117     return FALSE;
1118   }
1119 }
1120
1121 GstCaps *
1122 gst_video_info_to_caps (GstVideoInfo * info)
1123 {
1124   return NULL;
1125 }
1126
1127 gboolean
1128 gst_video_info_convert (GstVideoInfo * info,
1129     GstFormat src_format, gint64 src_value, GstFormat dest_format,
1130     gint64 * dest_value)
1131 {
1132   return TRUE;
1133 }
1134
1135 /**
1136  * gst_video_format_get_row_stride:
1137  * @format: a #GstVideoFormat
1138  * @component: the component index
1139  * @width: the width of video
1140  *
1141  * Calculates the row stride (number of bytes from one row of pixels to
1142  * the next) for the video component with an index of @component.  For
1143  * YUV video, Y, U, and V have component indices of 0, 1, and 2,
1144  * respectively.  For RGB video, R, G, and B have component indicies of
1145  * 0, 1, and 2, respectively.  Alpha channels, if present, have a component
1146  * index of 3.  The @width parameter always represents the width of the
1147  * video, not the component.
1148  *
1149  * Since: 0.10.16
1150  *
1151  * Returns: row stride of component @component
1152  */
1153 int
1154 gst_video_format_get_row_stride (GstVideoFormat format, int component,
1155     int width)
1156 {
1157   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1158   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1159   g_return_val_if_fail (width > 0, 0);
1160
1161   switch (format) {
1162     case GST_VIDEO_FORMAT_I420:
1163     case GST_VIDEO_FORMAT_YV12:
1164       if (component == 0) {
1165         return GST_ROUND_UP_4 (width);
1166       } else {
1167         return GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1168       }
1169     case GST_VIDEO_FORMAT_YUY2:
1170     case GST_VIDEO_FORMAT_YVYU:
1171     case GST_VIDEO_FORMAT_UYVY:
1172       return GST_ROUND_UP_4 (width * 2);
1173     case GST_VIDEO_FORMAT_AYUV:
1174     case GST_VIDEO_FORMAT_RGBx:
1175     case GST_VIDEO_FORMAT_BGRx:
1176     case GST_VIDEO_FORMAT_xRGB:
1177     case GST_VIDEO_FORMAT_xBGR:
1178     case GST_VIDEO_FORMAT_RGBA:
1179     case GST_VIDEO_FORMAT_BGRA:
1180     case GST_VIDEO_FORMAT_ARGB:
1181     case GST_VIDEO_FORMAT_ABGR:
1182     case GST_VIDEO_FORMAT_r210:
1183       return width * 4;
1184     case GST_VIDEO_FORMAT_RGB16:
1185     case GST_VIDEO_FORMAT_BGR16:
1186     case GST_VIDEO_FORMAT_RGB15:
1187     case GST_VIDEO_FORMAT_BGR15:
1188       return GST_ROUND_UP_4 (width * 2);
1189     case GST_VIDEO_FORMAT_RGB:
1190     case GST_VIDEO_FORMAT_BGR:
1191     case GST_VIDEO_FORMAT_v308:
1192       return GST_ROUND_UP_4 (width * 3);
1193     case GST_VIDEO_FORMAT_Y41B:
1194       if (component == 0) {
1195         return GST_ROUND_UP_4 (width);
1196       } else {
1197         return GST_ROUND_UP_16 (width) / 4;
1198       }
1199     case GST_VIDEO_FORMAT_Y42B:
1200       if (component == 0) {
1201         return GST_ROUND_UP_4 (width);
1202       } else {
1203         return GST_ROUND_UP_8 (width) / 2;
1204       }
1205     case GST_VIDEO_FORMAT_Y444:
1206       return GST_ROUND_UP_4 (width);
1207     case GST_VIDEO_FORMAT_v210:
1208       return ((width + 47) / 48) * 128;
1209     case GST_VIDEO_FORMAT_v216:
1210       return GST_ROUND_UP_8 (width * 4);
1211     case GST_VIDEO_FORMAT_NV12:
1212     case GST_VIDEO_FORMAT_NV21:
1213       return GST_ROUND_UP_4 (width);
1214     case GST_VIDEO_FORMAT_GRAY8:
1215     case GST_VIDEO_FORMAT_Y800:
1216       return GST_ROUND_UP_4 (width);
1217     case GST_VIDEO_FORMAT_GRAY16_BE:
1218     case GST_VIDEO_FORMAT_GRAY16_LE:
1219     case GST_VIDEO_FORMAT_Y16:
1220       return GST_ROUND_UP_4 (width * 2);
1221     case GST_VIDEO_FORMAT_UYVP:
1222       return GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4);
1223     case GST_VIDEO_FORMAT_A420:
1224       if (component == 0 || component == 3) {
1225         return GST_ROUND_UP_4 (width);
1226       } else {
1227         return GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1228       }
1229     case GST_VIDEO_FORMAT_RGB8_PALETTED:
1230       return GST_ROUND_UP_4 (width);
1231     case GST_VIDEO_FORMAT_YUV9:
1232     case GST_VIDEO_FORMAT_YVU9:
1233       if (component == 0) {
1234         return GST_ROUND_UP_4 (width);
1235       } else {
1236         return GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
1237       }
1238     case GST_VIDEO_FORMAT_IYU1:
1239       return GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) +
1240           GST_ROUND_UP_4 (width) / 2);
1241     case GST_VIDEO_FORMAT_ARGB64:
1242     case GST_VIDEO_FORMAT_AYUV64:
1243       return width * 8;
1244     default:
1245       return 0;
1246   }
1247 }
1248
1249 /**
1250  * gst_video_format_get_component_width:
1251  * @format: a #GstVideoFormat
1252  * @component: the component index
1253  * @width: the width of video
1254  *
1255  * Calculates the width of the component.  See
1256  * @gst_video_format_get_row_stride for a description
1257  * of the component index.
1258  *
1259  * Since: 0.10.16
1260  *
1261  * Returns: width of component @component
1262  */
1263 int
1264 gst_video_format_get_component_width (GstVideoFormat format,
1265     int component, int width)
1266 {
1267   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1268   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1269   g_return_val_if_fail (width > 0, 0);
1270
1271   switch (format) {
1272     case GST_VIDEO_FORMAT_I420:
1273     case GST_VIDEO_FORMAT_YV12:
1274     case GST_VIDEO_FORMAT_YUY2:
1275     case GST_VIDEO_FORMAT_YVYU:
1276     case GST_VIDEO_FORMAT_UYVY:
1277     case GST_VIDEO_FORMAT_Y42B:
1278     case GST_VIDEO_FORMAT_v210:
1279     case GST_VIDEO_FORMAT_v216:
1280     case GST_VIDEO_FORMAT_NV12:
1281     case GST_VIDEO_FORMAT_NV21:
1282     case GST_VIDEO_FORMAT_UYVP:
1283       if (component == 0) {
1284         return width;
1285       } else {
1286         return GST_ROUND_UP_2 (width) / 2;
1287       }
1288     case GST_VIDEO_FORMAT_Y41B:
1289     case GST_VIDEO_FORMAT_YUV9:
1290     case GST_VIDEO_FORMAT_YVU9:
1291     case GST_VIDEO_FORMAT_IYU1:
1292       if (component == 0) {
1293         return width;
1294       } else {
1295         return GST_ROUND_UP_4 (width) / 4;
1296       }
1297     case GST_VIDEO_FORMAT_AYUV:
1298     case GST_VIDEO_FORMAT_RGBx:
1299     case GST_VIDEO_FORMAT_BGRx:
1300     case GST_VIDEO_FORMAT_xRGB:
1301     case GST_VIDEO_FORMAT_xBGR:
1302     case GST_VIDEO_FORMAT_RGBA:
1303     case GST_VIDEO_FORMAT_BGRA:
1304     case GST_VIDEO_FORMAT_ARGB:
1305     case GST_VIDEO_FORMAT_ABGR:
1306     case GST_VIDEO_FORMAT_RGB:
1307     case GST_VIDEO_FORMAT_BGR:
1308     case GST_VIDEO_FORMAT_RGB16:
1309     case GST_VIDEO_FORMAT_BGR16:
1310     case GST_VIDEO_FORMAT_RGB15:
1311     case GST_VIDEO_FORMAT_BGR15:
1312     case GST_VIDEO_FORMAT_Y444:
1313     case GST_VIDEO_FORMAT_v308:
1314     case GST_VIDEO_FORMAT_GRAY8:
1315     case GST_VIDEO_FORMAT_GRAY16_BE:
1316     case GST_VIDEO_FORMAT_GRAY16_LE:
1317     case GST_VIDEO_FORMAT_Y800:
1318     case GST_VIDEO_FORMAT_Y16:
1319     case GST_VIDEO_FORMAT_RGB8_PALETTED:
1320     case GST_VIDEO_FORMAT_ARGB64:
1321     case GST_VIDEO_FORMAT_AYUV64:
1322     case GST_VIDEO_FORMAT_r210:
1323       return width;
1324     case GST_VIDEO_FORMAT_A420:
1325       if (component == 0 || component == 3) {
1326         return width;
1327       } else {
1328         return GST_ROUND_UP_2 (width) / 2;
1329       }
1330     default:
1331       return 0;
1332   }
1333 }
1334
1335 /**
1336  * gst_video_format_get_component_height:
1337  * @format: a #GstVideoFormat
1338  * @component: the component index
1339  * @height: the height of video
1340  *
1341  * Calculates the height of the component.  See
1342  * @gst_video_format_get_row_stride for a description
1343  * of the component index.
1344  *
1345  * Since: 0.10.16
1346  *
1347  * Returns: height of component @component
1348  */
1349 int
1350 gst_video_format_get_component_height (GstVideoFormat format,
1351     int component, int height)
1352 {
1353   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1354   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1355   g_return_val_if_fail (height > 0, 0);
1356
1357   switch (format) {
1358     case GST_VIDEO_FORMAT_I420:
1359     case GST_VIDEO_FORMAT_YV12:
1360     case GST_VIDEO_FORMAT_NV12:
1361     case GST_VIDEO_FORMAT_NV21:
1362       if (component == 0) {
1363         return height;
1364       } else {
1365         return GST_ROUND_UP_2 (height) / 2;
1366       }
1367     case GST_VIDEO_FORMAT_Y41B:
1368     case GST_VIDEO_FORMAT_Y42B:
1369     case GST_VIDEO_FORMAT_YUY2:
1370     case GST_VIDEO_FORMAT_YVYU:
1371     case GST_VIDEO_FORMAT_UYVY:
1372     case GST_VIDEO_FORMAT_AYUV:
1373     case GST_VIDEO_FORMAT_RGBx:
1374     case GST_VIDEO_FORMAT_BGRx:
1375     case GST_VIDEO_FORMAT_xRGB:
1376     case GST_VIDEO_FORMAT_xBGR:
1377     case GST_VIDEO_FORMAT_RGBA:
1378     case GST_VIDEO_FORMAT_BGRA:
1379     case GST_VIDEO_FORMAT_ARGB:
1380     case GST_VIDEO_FORMAT_ABGR:
1381     case GST_VIDEO_FORMAT_RGB:
1382     case GST_VIDEO_FORMAT_BGR:
1383     case GST_VIDEO_FORMAT_RGB16:
1384     case GST_VIDEO_FORMAT_BGR16:
1385     case GST_VIDEO_FORMAT_RGB15:
1386     case GST_VIDEO_FORMAT_BGR15:
1387     case GST_VIDEO_FORMAT_Y444:
1388     case GST_VIDEO_FORMAT_v210:
1389     case GST_VIDEO_FORMAT_v216:
1390     case GST_VIDEO_FORMAT_v308:
1391     case GST_VIDEO_FORMAT_GRAY8:
1392     case GST_VIDEO_FORMAT_GRAY16_BE:
1393     case GST_VIDEO_FORMAT_GRAY16_LE:
1394     case GST_VIDEO_FORMAT_Y800:
1395     case GST_VIDEO_FORMAT_Y16:
1396     case GST_VIDEO_FORMAT_UYVP:
1397     case GST_VIDEO_FORMAT_RGB8_PALETTED:
1398     case GST_VIDEO_FORMAT_IYU1:
1399     case GST_VIDEO_FORMAT_ARGB64:
1400     case GST_VIDEO_FORMAT_AYUV64:
1401     case GST_VIDEO_FORMAT_r210:
1402       return height;
1403     case GST_VIDEO_FORMAT_A420:
1404       if (component == 0 || component == 3) {
1405         return height;
1406       } else {
1407         return GST_ROUND_UP_2 (height) / 2;
1408       }
1409     case GST_VIDEO_FORMAT_YUV9:
1410     case GST_VIDEO_FORMAT_YVU9:
1411       if (component == 0) {
1412         return height;
1413       } else {
1414         return GST_ROUND_UP_4 (height) / 4;
1415       }
1416     default:
1417       return 0;
1418   }
1419 }
1420
1421 /**
1422  * gst_video_format_get_component_offset:
1423  * @format: a #GstVideoFormat
1424  * @component: the component index
1425  * @width: the width of video
1426  * @height: the height of video
1427  *
1428  * Calculates the offset (in bytes) of the first pixel of the component
1429  * with index @component.  For packed formats, this will typically be a
1430  * small integer (0, 1, 2, 3).  For planar formats, this will be a
1431  * (relatively) large offset to the beginning of the second or third
1432  * component planes.  See @gst_video_format_get_row_stride for a description
1433  * of the component index.
1434  *
1435  * Since: 0.10.16
1436  *
1437  * Returns: offset of component @component
1438  */
1439 int
1440 gst_video_format_get_component_offset (GstVideoFormat format,
1441     int component, int width, int height)
1442 {
1443   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1444   g_return_val_if_fail (component >= 0 && component <= 3, 0);
1445   g_return_val_if_fail ((!gst_video_format_is_yuv (format)) || (width > 0
1446           && height > 0), 0);
1447
1448   switch (format) {
1449     case GST_VIDEO_FORMAT_I420:
1450       if (component == 0)
1451         return 0;
1452       if (component == 1)
1453         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1454       if (component == 2) {
1455         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1456             GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1457             (GST_ROUND_UP_2 (height) / 2);
1458       }
1459       break;
1460     case GST_VIDEO_FORMAT_YV12:        /* same as I420, but components 1+2 swapped */
1461       if (component == 0)
1462         return 0;
1463       if (component == 2)
1464         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1465       if (component == 1) {
1466         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1467             GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1468             (GST_ROUND_UP_2 (height) / 2);
1469       }
1470       break;
1471     case GST_VIDEO_FORMAT_YUY2:
1472       if (component == 0)
1473         return 0;
1474       if (component == 1)
1475         return 1;
1476       if (component == 2)
1477         return 3;
1478       break;
1479     case GST_VIDEO_FORMAT_YVYU:
1480       if (component == 0)
1481         return 0;
1482       if (component == 1)
1483         return 3;
1484       if (component == 2)
1485         return 1;
1486       break;
1487     case GST_VIDEO_FORMAT_UYVY:
1488       if (component == 0)
1489         return 1;
1490       if (component == 1)
1491         return 0;
1492       if (component == 2)
1493         return 2;
1494       break;
1495     case GST_VIDEO_FORMAT_AYUV:
1496       if (component == 0)
1497         return 1;
1498       if (component == 1)
1499         return 2;
1500       if (component == 2)
1501         return 3;
1502       if (component == 3)
1503         return 0;
1504       break;
1505     case GST_VIDEO_FORMAT_RGBx:
1506     case GST_VIDEO_FORMAT_RGBA:
1507       if (component == 0)
1508         return 0;
1509       if (component == 1)
1510         return 1;
1511       if (component == 2)
1512         return 2;
1513       if (component == 3)
1514         return 3;
1515       break;
1516     case GST_VIDEO_FORMAT_BGRx:
1517     case GST_VIDEO_FORMAT_BGRA:
1518       if (component == 0)
1519         return 2;
1520       if (component == 1)
1521         return 1;
1522       if (component == 2)
1523         return 0;
1524       if (component == 3)
1525         return 3;
1526       break;
1527     case GST_VIDEO_FORMAT_xRGB:
1528     case GST_VIDEO_FORMAT_ARGB:
1529       if (component == 0)
1530         return 1;
1531       if (component == 1)
1532         return 2;
1533       if (component == 2)
1534         return 3;
1535       if (component == 3)
1536         return 0;
1537       break;
1538     case GST_VIDEO_FORMAT_xBGR:
1539     case GST_VIDEO_FORMAT_ABGR:
1540       if (component == 0)
1541         return 3;
1542       if (component == 1)
1543         return 2;
1544       if (component == 2)
1545         return 1;
1546       if (component == 3)
1547         return 0;
1548       break;
1549     case GST_VIDEO_FORMAT_RGB:
1550     case GST_VIDEO_FORMAT_v308:
1551       if (component == 0)
1552         return 0;
1553       if (component == 1)
1554         return 1;
1555       if (component == 2)
1556         return 2;
1557       break;
1558     case GST_VIDEO_FORMAT_BGR:
1559       if (component == 0)
1560         return 2;
1561       if (component == 1)
1562         return 1;
1563       if (component == 2)
1564         return 0;
1565       break;
1566     case GST_VIDEO_FORMAT_Y41B:
1567       if (component == 0)
1568         return 0;
1569       if (component == 1)
1570         return GST_ROUND_UP_4 (width) * height;
1571       if (component == 2)
1572         return (GST_ROUND_UP_4 (width) +
1573             (GST_ROUND_UP_16 (width) / 4)) * height;
1574       break;
1575     case GST_VIDEO_FORMAT_Y42B:
1576       if (component == 0)
1577         return 0;
1578       if (component == 1)
1579         return GST_ROUND_UP_4 (width) * height;
1580       if (component == 2)
1581         return (GST_ROUND_UP_4 (width) + (GST_ROUND_UP_8 (width) / 2)) * height;
1582       break;
1583     case GST_VIDEO_FORMAT_Y444:
1584       return GST_ROUND_UP_4 (width) * height * component;
1585     case GST_VIDEO_FORMAT_v210:
1586     case GST_VIDEO_FORMAT_r210:
1587       /* v210 is bit-packed, so this doesn't make sense */
1588       return 0;
1589     case GST_VIDEO_FORMAT_v216:
1590       if (component == 0)
1591         return 0;
1592       if (component == 1)
1593         return 2;
1594       if (component == 2)
1595         return 6;
1596       break;
1597     case GST_VIDEO_FORMAT_NV12:
1598       if (component == 0)
1599         return 0;
1600       if (component == 1)
1601         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1602       if (component == 2)
1603         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) + 1;
1604       break;
1605     case GST_VIDEO_FORMAT_NV21:
1606       if (component == 0)
1607         return 0;
1608       if (component == 1)
1609         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) + 1;
1610       if (component == 2)
1611         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1612       break;
1613     case GST_VIDEO_FORMAT_GRAY8:
1614     case GST_VIDEO_FORMAT_GRAY16_BE:
1615     case GST_VIDEO_FORMAT_GRAY16_LE:
1616     case GST_VIDEO_FORMAT_Y800:
1617     case GST_VIDEO_FORMAT_Y16:
1618       return 0;
1619     case GST_VIDEO_FORMAT_UYVP:
1620       /* UYVP is bit-packed, so this doesn't make sense */
1621       return 0;
1622     case GST_VIDEO_FORMAT_A420:
1623       if (component == 0)
1624         return 0;
1625       if (component == 1)
1626         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1627       if (component == 2) {
1628         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1629             GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1630             (GST_ROUND_UP_2 (height) / 2);
1631       }
1632       if (component == 3) {
1633         return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) +
1634             2 * GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1635             (GST_ROUND_UP_2 (height) / 2);
1636       }
1637       break;
1638     case GST_VIDEO_FORMAT_RGB8_PALETTED:
1639       return 0;
1640     case GST_VIDEO_FORMAT_YUV9:
1641       if (component == 0)
1642         return 0;
1643       if (component == 1)
1644         return GST_ROUND_UP_4 (width) * height;
1645       if (component == 2) {
1646         return GST_ROUND_UP_4 (width) * height +
1647             GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4) *
1648             (GST_ROUND_UP_4 (height) / 4);
1649       }
1650       break;
1651     case GST_VIDEO_FORMAT_YVU9:
1652       if (component == 0)
1653         return 0;
1654       if (component == 1) {
1655         return GST_ROUND_UP_4 (width) * height +
1656             GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4) *
1657             (GST_ROUND_UP_4 (height) / 4);
1658       }
1659       if (component == 2)
1660         return GST_ROUND_UP_4 (width) * height;
1661       break;
1662     case GST_VIDEO_FORMAT_IYU1:
1663       if (component == 0)
1664         return 1;
1665       if (component == 1)
1666         return 0;
1667       if (component == 2)
1668         return 4;
1669       break;
1670     case GST_VIDEO_FORMAT_ARGB64:
1671     case GST_VIDEO_FORMAT_AYUV64:
1672       if (component == 0)
1673         return 2;
1674       if (component == 1)
1675         return 4;
1676       if (component == 2)
1677         return 6;
1678       if (component == 3)
1679         return 0;
1680       break;
1681     default:
1682       break;
1683   }
1684   GST_WARNING ("unhandled format %d or component %d", format, component);
1685   return 0;
1686 }
1687
1688 /**
1689  * gst_video_format_get_size:
1690  * @format: a #GstVideoFormat
1691  * @width: the width of video
1692  * @height: the height of video
1693  *
1694  * Calculates the total number of bytes in the raw video format.  This
1695  * number should be used when allocating a buffer for raw video.
1696  *
1697  * Since: 0.10.16
1698  *
1699  * Returns: size (in bytes) of raw video format
1700  */
1701 int
1702 gst_video_format_get_size (GstVideoFormat format, int width, int height)
1703 {
1704   int size;
1705
1706   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1707   g_return_val_if_fail (width > 0 && height > 0, 0);
1708
1709   switch (format) {
1710     case GST_VIDEO_FORMAT_I420:
1711     case GST_VIDEO_FORMAT_YV12:
1712       size = GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1713       size += GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1714           (GST_ROUND_UP_2 (height) / 2) * 2;
1715       return size;
1716     case GST_VIDEO_FORMAT_IYU1:
1717       return GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) +
1718           GST_ROUND_UP_4 (width) / 2) * height;
1719     case GST_VIDEO_FORMAT_YUY2:
1720     case GST_VIDEO_FORMAT_YVYU:
1721     case GST_VIDEO_FORMAT_UYVY:
1722       return GST_ROUND_UP_4 (width * 2) * height;
1723     case GST_VIDEO_FORMAT_AYUV:
1724     case GST_VIDEO_FORMAT_RGBx:
1725     case GST_VIDEO_FORMAT_BGRx:
1726     case GST_VIDEO_FORMAT_xRGB:
1727     case GST_VIDEO_FORMAT_xBGR:
1728     case GST_VIDEO_FORMAT_RGBA:
1729     case GST_VIDEO_FORMAT_BGRA:
1730     case GST_VIDEO_FORMAT_ARGB:
1731     case GST_VIDEO_FORMAT_ABGR:
1732     case GST_VIDEO_FORMAT_r210:
1733       return width * 4 * height;
1734     case GST_VIDEO_FORMAT_RGB16:
1735     case GST_VIDEO_FORMAT_BGR16:
1736     case GST_VIDEO_FORMAT_RGB15:
1737     case GST_VIDEO_FORMAT_BGR15:
1738       return GST_ROUND_UP_4 (width * 2) * height;
1739     case GST_VIDEO_FORMAT_RGB:
1740     case GST_VIDEO_FORMAT_BGR:
1741     case GST_VIDEO_FORMAT_v308:
1742       return GST_ROUND_UP_4 (width * 3) * height;
1743     case GST_VIDEO_FORMAT_Y41B:
1744       /* simplification of ROUNDUP4(w)*h + 2*((ROUNDUP16(w)/4)*h */
1745       return (GST_ROUND_UP_4 (width) + (GST_ROUND_UP_16 (width) / 2)) * height;
1746     case GST_VIDEO_FORMAT_Y42B:
1747       /* simplification of ROUNDUP4(w)*h + 2*(ROUNDUP8(w)/2)*h */
1748       return (GST_ROUND_UP_4 (width) + GST_ROUND_UP_8 (width)) * height;
1749     case GST_VIDEO_FORMAT_Y444:
1750       return GST_ROUND_UP_4 (width) * height * 3;
1751     case GST_VIDEO_FORMAT_v210:
1752       return ((width + 47) / 48) * 128 * height;
1753     case GST_VIDEO_FORMAT_v216:
1754       return GST_ROUND_UP_8 (width * 4) * height;
1755     case GST_VIDEO_FORMAT_NV12:
1756     case GST_VIDEO_FORMAT_NV21:
1757       return GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height) * 3 / 2;
1758     case GST_VIDEO_FORMAT_GRAY8:
1759     case GST_VIDEO_FORMAT_Y800:
1760     case GST_VIDEO_FORMAT_RGB8_PALETTED:
1761       return GST_ROUND_UP_4 (width) * height;
1762     case GST_VIDEO_FORMAT_GRAY16_BE:
1763     case GST_VIDEO_FORMAT_GRAY16_LE:
1764     case GST_VIDEO_FORMAT_Y16:
1765       return GST_ROUND_UP_4 (width * 2) * height;
1766     case GST_VIDEO_FORMAT_UYVP:
1767       return GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4) * height;
1768     case GST_VIDEO_FORMAT_A420:
1769       size = 2 * GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height);
1770       size += GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2) *
1771           (GST_ROUND_UP_2 (height) / 2) * 2;
1772       return size;
1773     case GST_VIDEO_FORMAT_YUV9:
1774     case GST_VIDEO_FORMAT_YVU9:
1775       size = GST_ROUND_UP_4 (width) * height;
1776       size += GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4) *
1777           (GST_ROUND_UP_4 (height) / 4) * 2;
1778       return size;
1779     case GST_VIDEO_FORMAT_ARGB64:
1780     case GST_VIDEO_FORMAT_AYUV64:
1781       return width * 8 * height;
1782     default:
1783       return 0;
1784   }
1785 }
1786
1787 /**
1788  * gst_video_get_size_from_caps:
1789  * @caps: a pointer to #GstCaps
1790  * @size: a pointer to a gint that will be assigned the size (in bytes) of a video frame with the given caps
1791  *
1792  * Calculates the total number of bytes in the raw video format for the given
1793  * caps.  This number should be used when allocating a buffer for raw video.
1794  *
1795  * Since: 0.10.34
1796  *
1797  * Returns: %TRUE if the size could be calculated from the caps
1798  */
1799 gboolean
1800 gst_video_get_size_from_caps (const GstCaps * caps, gint * size)
1801 {
1802   GstVideoFormat format = 0;
1803   gint width = 0, height = 0;
1804
1805   g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
1806   g_return_val_if_fail (size != NULL, FALSE);
1807
1808   if (gst_video_format_parse_caps (caps, &format, &width, &height) == FALSE) {
1809     GST_WARNING ("Could not parse caps: %" GST_PTR_FORMAT, caps);
1810     return FALSE;
1811   }
1812
1813   *size = gst_video_format_get_size (format, width, height);
1814   return TRUE;
1815 }
1816
1817 /**
1818  * gst_video_format_convert:
1819  * @format: a #GstVideoFormat
1820  * @width: the width of video
1821  * @height: the height of video
1822  * @fps_n: frame rate numerator
1823  * @fps_d: frame rate denominator
1824  * @src_format: #GstFormat of the @src_value
1825  * @src_value: value to convert
1826  * @dest_format: #GstFormat of the @dest_value
1827  * @dest_value: pointer to destination value
1828  *
1829  * Converts among various #GstFormat types.  This function handles
1830  * GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT.  For
1831  * raw video, GST_FORMAT_DEFAULT corresponds to video frames.  This
1832  * function can be to handle pad queries of the type GST_QUERY_CONVERT.
1833  *
1834  * Since: 0.10.16
1835  *
1836  * Returns: TRUE if the conversion was successful.
1837  */
1838 gboolean
1839 gst_video_format_convert (GstVideoFormat format, int width, int height,
1840     int fps_n, int fps_d,
1841     GstFormat src_format, gint64 src_value,
1842     GstFormat dest_format, gint64 * dest_value)
1843 {
1844   gboolean ret = FALSE;
1845   int size;
1846
1847   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1848   g_return_val_if_fail (width > 0 && height > 0, 0);
1849
1850   size = gst_video_format_get_size (format, width, height);
1851
1852   GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s to %s",
1853       src_value, gst_format_get_name (src_format),
1854       gst_format_get_name (dest_format));
1855
1856   if (src_format == dest_format) {
1857     *dest_value = src_value;
1858     ret = TRUE;
1859     goto done;
1860   }
1861
1862   if (src_value == -1) {
1863     *dest_value = -1;
1864     ret = TRUE;
1865     goto done;
1866   }
1867
1868   /* bytes to frames */
1869   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_DEFAULT) {
1870     if (size != 0) {
1871       *dest_value = gst_util_uint64_scale_int (src_value, 1, size);
1872     } else {
1873       GST_ERROR ("blocksize is 0");
1874       *dest_value = 0;
1875     }
1876     ret = TRUE;
1877     goto done;
1878   }
1879
1880   /* frames to bytes */
1881   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_BYTES) {
1882     *dest_value = gst_util_uint64_scale_int (src_value, size, 1);
1883     ret = TRUE;
1884     goto done;
1885   }
1886
1887   /* time to frames */
1888   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_DEFAULT) {
1889     if (fps_d != 0) {
1890       *dest_value = gst_util_uint64_scale (src_value,
1891           fps_n, GST_SECOND * fps_d);
1892     } else {
1893       GST_ERROR ("framerate denominator is 0");
1894       *dest_value = 0;
1895     }
1896     ret = TRUE;
1897     goto done;
1898   }
1899
1900   /* frames to time */
1901   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) {
1902     if (fps_n != 0) {
1903       *dest_value = gst_util_uint64_scale (src_value,
1904           GST_SECOND * fps_d, fps_n);
1905     } else {
1906       GST_ERROR ("framerate numerator is 0");
1907       *dest_value = 0;
1908     }
1909     ret = TRUE;
1910     goto done;
1911   }
1912
1913   /* time to bytes */
1914   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
1915     if (fps_d != 0) {
1916       *dest_value = gst_util_uint64_scale (src_value,
1917           fps_n * size, GST_SECOND * fps_d);
1918     } else {
1919       GST_ERROR ("framerate denominator is 0");
1920       *dest_value = 0;
1921     }
1922     ret = TRUE;
1923     goto done;
1924   }
1925
1926   /* bytes to time */
1927   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
1928     if (fps_n != 0 && size != 0) {
1929       *dest_value = gst_util_uint64_scale (src_value,
1930           GST_SECOND * fps_d, fps_n * size);
1931     } else {
1932       GST_ERROR ("framerate denominator and/or blocksize is 0");
1933       *dest_value = 0;
1934     }
1935     ret = TRUE;
1936   }
1937
1938 done:
1939
1940   GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, ret, *dest_value);
1941
1942   return ret;
1943 }
1944
1945 #define GST_VIDEO_EVENT_STILL_STATE_NAME "GstEventStillFrame"
1946
1947 /**
1948  * gst_video_event_new_still_frame:
1949  * @in_still: boolean value for the still-frame state of the event.
1950  *
1951  * Creates a new Still Frame event. If @in_still is %TRUE, then the event
1952  * represents the start of a still frame sequence. If it is %FALSE, then
1953  * the event ends a still frame sequence.
1954  *
1955  * To parse an event created by gst_video_event_new_still_frame() use
1956  * gst_video_event_parse_still_frame().
1957  *
1958  * Returns: The new GstEvent
1959  * Since: 0.10.26
1960  */
1961 GstEvent *
1962 gst_video_event_new_still_frame (gboolean in_still)
1963 {
1964   GstEvent *still_event;
1965   GstStructure *s;
1966
1967   s = gst_structure_new (GST_VIDEO_EVENT_STILL_STATE_NAME,
1968       "still-state", G_TYPE_BOOLEAN, in_still, NULL);
1969   still_event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1970
1971   return still_event;
1972 }
1973
1974 /**
1975  * gst_video_event_parse_still_frame:
1976  * @event: A #GstEvent to parse
1977  * @in_still: A boolean to receive the still-frame status from the event, or NULL
1978  *
1979  * Parse a #GstEvent, identify if it is a Still Frame event, and
1980  * return the still-frame state from the event if it is.
1981  * If the event represents the start of a still frame, the in_still
1982  * variable will be set to TRUE, otherwise FALSE. It is OK to pass NULL for the
1983  * in_still variable order to just check whether the event is a valid still-frame
1984  * event.
1985  *
1986  * Create a still frame event using gst_video_event_new_still_frame()
1987  *
1988  * Returns: %TRUE if the event is a valid still-frame event. %FALSE if not
1989  * Since: 0.10.26
1990  */
1991 gboolean
1992 gst_video_event_parse_still_frame (GstEvent * event, gboolean * in_still)
1993 {
1994   const GstStructure *s;
1995   gboolean ev_still_state;
1996
1997   g_return_val_if_fail (event != NULL, FALSE);
1998
1999   if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM)
2000     return FALSE;               /* Not a still frame event */
2001
2002   s = gst_event_get_structure (event);
2003   if (s == NULL
2004       || !gst_structure_has_name (s, GST_VIDEO_EVENT_STILL_STATE_NAME))
2005     return FALSE;               /* Not a still frame event */
2006   if (!gst_structure_get_boolean (s, "still-state", &ev_still_state))
2007     return FALSE;               /* Not a still frame event */
2008   if (in_still)
2009     *in_still = ev_still_state;
2010   return TRUE;
2011 }
2012
2013 /**
2014  * gst_video_parse_caps_palette:
2015  * @caps: #GstCaps to parse
2016  *
2017  * Returns the palette data from the caps as a #GstBuffer. For
2018  * #GST_VIDEO_FORMAT_RGB8_PALETTED this is containing 256 #guint32
2019  * values, each containing ARGB colors in native endianness.
2020  *
2021  * Returns: a #GstBuffer containing the palette data. Unref after usage.
2022  * Since: 0.10.32
2023  */
2024 GstBuffer *
2025 gst_video_parse_caps_palette (GstCaps * caps)
2026 {
2027   GstStructure *s;
2028   const GValue *p_v;
2029   GstBuffer *p;
2030
2031   if (!gst_caps_is_fixed (caps))
2032     return NULL;
2033
2034   s = gst_caps_get_structure (caps, 0);
2035
2036   p_v = gst_structure_get_value (s, "palette_data");
2037   if (!p_v || !GST_VALUE_HOLDS_BUFFER (p_v))
2038     return NULL;
2039
2040   p = gst_buffer_ref (gst_value_get_buffer (p_v));
2041
2042   return p;
2043 }