audio/video add descriptions
[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 #include "gstmetavideo.h"
30
31 static int fill_planes (GstVideoInfo * info);
32
33 typedef struct
34 {
35   guint32 fourcc;
36   GstVideoFormatInfo info;
37 } VideoFormat;
38
39 /* depths: bits, n_components, shift, depth */
40 #define DPTH0            0, 0, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
41 #define DPTH8            8, 1, { 0, 0, 0, 0 }, { 8, 0, 0, 0 }
42 #define DPTH888          8, 3, { 0, 0, 0, 0 }, { 8, 8, 8, 0 }
43 #define DPTH8888         8, 4, { 0, 0, 0, 0 }, { 8, 8, 8, 8 }
44 #define DPTH10_10_10     10, 3, { 0, 0, 0, 0 }, { 10, 10, 10, 0 }
45 #define DPTH16           16, 1, { 0, 0, 0, 0 }, { 16, 0, 0, 0 }
46 #define DPTH16_16_16     16, 3, { 0, 0, 0, 0 }, { 16, 16, 16, 0 }
47 #define DPTH16_16_16_16  16, 4, { 0, 0, 0, 0 }, { 16, 16, 16, 16 }
48 #define DPTH555          16, 3, { 10, 5, 0, 0 }, { 5, 5, 5, 0 }
49 #define DPTH565          16, 3, { 11, 5, 0, 0 }, { 5, 6, 5, 0 }
50
51 /* pixel strides */
52 #define PSTR0             { 0, 0, 0, 0 }
53 #define PSTR1             { 1, 0, 0, 0 }
54 #define PSTR111           { 1, 1, 1, 0 }
55 #define PSTR1111          { 1, 1, 1, 1 }
56 #define PSTR122           { 1, 2, 2, 0 }
57 #define PSTR2             { 2, 0, 0, 0 }
58 #define PSTR222           { 2, 2, 2, 0 }
59 #define PSTR244           { 2, 4, 4, 0 }
60 #define PSTR444           { 4, 4, 4, 0 }
61 #define PSTR4444          { 4, 4, 4, 4 }
62 #define PSTR333           { 3, 3, 3, 0 }
63 #define PSTR488           { 4, 8, 8, 0 }
64 #define PSTR8888          { 8, 8, 8, 8 }
65
66 /* planes */
67 #define PLANE_NA          0, { 0, 0, 0, 0 }
68 #define PLANE0            1, { 0, 0, 0, 0 }
69 #define PLANE011          2, { 0, 1, 1, 0 }
70 #define PLANE012          3, { 0, 1, 2, 0 }
71 #define PLANE0123         4, { 0, 1, 2, 3 }
72 #define PLANE021          3, { 0, 2, 1, 0 }
73
74 /* offsets */
75 #define OFFS0             { 0, 0, 0, 0 }
76 #define OFFS013           { 0, 1, 3, 0 }
77 #define OFFS102           { 1, 0, 2, 0 }
78 #define OFFS1230          { 1, 2, 3, 0 }
79 #define OFFS012           { 0, 1, 2, 0 }
80 #define OFFS210           { 2, 1, 0, 0 }
81 #define OFFS123           { 1, 2, 3, 0 }
82 #define OFFS321           { 3, 2, 1, 0 }
83 #define OFFS0123          { 0, 1, 2, 3 }
84 #define OFFS2103          { 2, 1, 0, 3 }
85 #define OFFS3210          { 3, 2, 1, 0 }
86 #define OFFS031           { 0, 3, 1, 0 }
87 #define OFFS026           { 0, 2, 6, 0 }
88 #define OFFS001           { 0, 0, 1, 0 }
89 #define OFFS010           { 0, 1, 0, 0 }
90 #define OFFS104           { 1, 0, 4, 0 }
91 #define OFFS2460          { 2, 4, 6, 0 }
92
93 /* subsampling */
94 #define SUB410            { 0, 2, 2, 0 }, { 0, 2, 2, 0 }
95 #define SUB411            { 0, 2, 2, 0 }, { 0, 0, 0, 0 }
96 #define SUB420            { 0, 1, 1, 0 }, { 0, 1, 1, 0 }
97 #define SUB422            { 0, 1, 1, 0 }, { 0, 0, 0, 0 }
98 #define SUB4              { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
99 #define SUB444            { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
100 #define SUB4444           { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
101 #define SUB4204           { 0, 1, 1, 0 }, { 0, 1, 1, 0 }
102
103 #define MAKE_YUV_FORMAT(name, desc, fourcc, depth, pstride, plane, offs, sub ) \
104  { fourcc, {GST_VIDEO_FORMAT_ ##name, G_STRINGIFY(name), desc, GST_VIDEO_FORMAT_FLAG_YUV, depth, pstride, plane, offs, sub } }
105 #define MAKE_YUVA_FORMAT(name, desc, fourcc, depth, pstride, plane, offs, sub) \
106  { fourcc, {GST_VIDEO_FORMAT_ ##name, G_STRINGIFY(name), desc, GST_VIDEO_FORMAT_FLAG_YUV | GST_VIDEO_FORMAT_FLAG_ALPHA, depth, pstride, plane, offs, sub } }
107
108 #define MAKE_RGB_FORMAT(name, desc, depth, pstride, plane, offs, sub) \
109  { 0x00000000, {GST_VIDEO_FORMAT_ ##name, G_STRINGIFY(name), desc, GST_VIDEO_FORMAT_FLAG_RGB, depth, pstride, plane, offs, sub } }
110 #define MAKE_RGBA_FORMAT(name, desc, depth, pstride, plane, offs, sub) \
111  { 0x00000000, {GST_VIDEO_FORMAT_ ##name, G_STRINGIFY(name), desc, GST_VIDEO_FORMAT_FLAG_RGB | GST_VIDEO_FORMAT_FLAG_ALPHA, depth, pstride, plane, offs, sub } }
112
113 #define MAKE_GRAY_FORMAT(name, desc, depth, pstride, plane, offs, sub) \
114  { 0x00000000, {GST_VIDEO_FORMAT_ ##name, G_STRINGIFY(name), desc, GST_VIDEO_FORMAT_FLAG_GRAY, depth, pstride, plane, offs, sub } }
115
116 static VideoFormat formats[] = {
117   {0x00000000, {GST_VIDEO_FORMAT_UNKNOWN, "UNKNOWN", "unknown video", 0, DPTH0,
118               PSTR0, PLANE_NA,
119           OFFS0}},
120
121   MAKE_YUV_FORMAT (I420, "raw video", GST_MAKE_FOURCC ('I', '4', '2', '0'),
122       DPTH888, PSTR111,
123       PLANE012, OFFS0, SUB420),
124   MAKE_YUV_FORMAT (YV12, "raw video", GST_MAKE_FOURCC ('Y', 'V', '1', '2'),
125       DPTH888, PSTR111,
126       PLANE021, OFFS0, SUB420),
127   MAKE_YUV_FORMAT (YUY2, "raw video", GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'),
128       DPTH888, PSTR244,
129       PLANE0, OFFS013, SUB422),
130   MAKE_YUV_FORMAT (UYVY, "raw video", GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'),
131       DPTH888, PSTR244,
132       PLANE0, OFFS102, SUB422),
133   MAKE_YUVA_FORMAT (AYUV, "raw video", GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'),
134       DPTH8888,
135       PSTR4444, PLANE0, OFFS1230, SUB4444),
136   MAKE_RGB_FORMAT (RGBx, "raw video", DPTH888, PSTR444, PLANE0, OFFS012,
137       SUB444),
138   MAKE_RGB_FORMAT (BGRx, "raw video", DPTH888, PSTR444, PLANE0, OFFS210,
139       SUB444),
140   MAKE_RGB_FORMAT (xRGB, "raw video", DPTH888, PSTR444, PLANE0, OFFS123,
141       SUB444),
142   MAKE_RGB_FORMAT (xBGR, "raw video", DPTH888, PSTR444, PLANE0, OFFS321,
143       SUB444),
144   MAKE_RGBA_FORMAT (RGBA, "raw video", DPTH8888, PSTR4444, PLANE0, OFFS0123,
145       SUB4444),
146   MAKE_RGBA_FORMAT (BGRA, "raw video", DPTH8888, PSTR4444, PLANE0, OFFS2103,
147       SUB4444),
148   MAKE_RGBA_FORMAT (ARGB, "raw video", DPTH8888, PSTR4444, PLANE0, OFFS1230,
149       SUB4444),
150   MAKE_RGBA_FORMAT (ABGR, "raw video", DPTH8888, PSTR4444, PLANE0, OFFS3210,
151       SUB4444),
152   MAKE_RGB_FORMAT (RGB, "raw video", DPTH888, PSTR333, PLANE0, OFFS012, SUB444),
153   MAKE_RGB_FORMAT (BGR, "raw video", DPTH888, PSTR333, PLANE0, OFFS210, SUB444),
154
155   MAKE_YUV_FORMAT (Y41B, "raw video", GST_MAKE_FOURCC ('Y', '4', '1', 'B'),
156       DPTH888, PSTR111,
157       PLANE012, OFFS0, SUB411),
158   MAKE_YUV_FORMAT (Y42B, "raw video", GST_MAKE_FOURCC ('Y', '4', '2', 'B'),
159       DPTH888, PSTR111,
160       PLANE012, OFFS0, SUB422),
161   MAKE_YUV_FORMAT (YVYU, "raw video", GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'),
162       DPTH888, PSTR244,
163       PLANE0, OFFS031, SUB422),
164   MAKE_YUV_FORMAT (Y444, "raw video", GST_MAKE_FOURCC ('Y', '4', '4', '4'),
165       DPTH888, PSTR111,
166       PLANE012, OFFS0, SUB444),
167   MAKE_YUV_FORMAT (v210, "raw video", GST_MAKE_FOURCC ('v', '2', '1', '0'),
168       DPTH10_10_10,
169       PSTR0, PLANE0, OFFS0, SUB422),
170   MAKE_YUV_FORMAT (v216, "raw video", GST_MAKE_FOURCC ('v', '2', '1', '6'),
171       DPTH16_16_16,
172       PSTR488, PLANE0, OFFS026, SUB422),
173   MAKE_YUV_FORMAT (NV12, "raw video", GST_MAKE_FOURCC ('N', 'V', '1', '2'),
174       DPTH888, PSTR122,
175       PLANE011, OFFS001, SUB420),
176   MAKE_YUV_FORMAT (NV21, "raw video", GST_MAKE_FOURCC ('N', 'V', '2', '1'),
177       DPTH888, PSTR122,
178       PLANE011, OFFS010, SUB420),
179
180   MAKE_GRAY_FORMAT (GRAY8, "raw video", DPTH8, PSTR1, PLANE0, OFFS0, SUB4),
181   MAKE_GRAY_FORMAT (GRAY16_BE, "raw video", DPTH16, PSTR2, PLANE0, OFFS0, SUB4),
182   MAKE_GRAY_FORMAT (GRAY16_LE, "raw video", DPTH16, PSTR2, PLANE0, OFFS0, SUB4),
183
184   MAKE_YUV_FORMAT (v308, "raw video", GST_MAKE_FOURCC ('v', '3', '0', '8'),
185       DPTH888, PSTR333,
186       PLANE0, OFFS012, SUB444),
187   MAKE_YUV_FORMAT (Y800, "raw video", GST_MAKE_FOURCC ('Y', '8', '0', '0'),
188       DPTH8, PSTR1,
189       PLANE0, OFFS0, SUB4),
190   MAKE_YUV_FORMAT (Y16, "raw video", GST_MAKE_FOURCC ('Y', '1', '6', ' '),
191       DPTH16, PSTR2,
192       PLANE0, OFFS0, SUB4),
193
194   MAKE_RGB_FORMAT (RGB16, "raw video", DPTH565, PSTR222, PLANE0, OFFS0, SUB444),
195   MAKE_RGB_FORMAT (BGR16, "raw video", DPTH565, PSTR222, PLANE0, OFFS0, SUB444),
196   MAKE_RGB_FORMAT (RGB15, "raw video", DPTH555, PSTR222, PLANE0, OFFS0, SUB444),
197   MAKE_RGB_FORMAT (BGR15, "raw video", DPTH555, PSTR222, PLANE0, OFFS0, SUB444),
198
199   MAKE_YUV_FORMAT (UYVP, "raw video", GST_MAKE_FOURCC ('U', 'Y', 'V', 'P'),
200       DPTH10_10_10,
201       PSTR0, PLANE0, OFFS0, SUB422),
202   MAKE_YUVA_FORMAT (A420, "raw video", GST_MAKE_FOURCC ('A', '4', '2', '0'),
203       DPTH8888,
204       PSTR1111, PLANE0123, OFFS0, SUB4204),
205   MAKE_RGBA_FORMAT (RGB8_PALETTED, "raw video", DPTH8888, PSTR1111, PLANE0,
206       OFFS0, SUB4444),
207   MAKE_YUV_FORMAT (YUV9, "raw video", GST_MAKE_FOURCC ('Y', 'U', 'V', '9'),
208       DPTH888, PSTR111,
209       PLANE012, OFFS0, SUB410),
210   MAKE_YUV_FORMAT (YVU9, "raw video", GST_MAKE_FOURCC ('Y', 'V', 'U', '9'),
211       DPTH888, PSTR111,
212       PLANE021, OFFS0, SUB410),
213   MAKE_YUV_FORMAT (IYU1, "raw video", GST_MAKE_FOURCC ('I', 'Y', 'U', '1'),
214       DPTH888, PSTR0,
215       PLANE0, OFFS104, SUB411),
216   MAKE_RGBA_FORMAT (ARGB64, "raw video", DPTH16_16_16_16, PSTR8888, PLANE0,
217       OFFS2460,
218       SUB444),
219   MAKE_YUVA_FORMAT (AYUV64, "raw video", 0x00000000, DPTH16_16_16_16, PSTR8888,
220       PLANE0,
221       OFFS2460, SUB444),
222   MAKE_YUV_FORMAT (r210, "raw video", GST_MAKE_FOURCC ('r', '2', '1', '0'),
223       DPTH10_10_10,
224       PSTR444, PLANE0, OFFS0, SUB444),
225 };
226
227 /**
228  * SECTION:gstvideo
229  * @short_description: Support library for video operations
230  *
231  * <refsect2>
232  * <para>
233  * This library contains some helper functions and includes the
234  * videosink and videofilter base classes.
235  * </para>
236  * </refsect2>
237  */
238
239 /**
240  * gst_video_calculate_display_ratio:
241  * @dar_n: Numerator of the calculated display_ratio
242  * @dar_d: Denominator of the calculated display_ratio
243  * @video_width: Width of the video frame in pixels
244  * @video_height: Height of the video frame in pixels
245  * @video_par_n: Numerator of the pixel aspect ratio of the input video.
246  * @video_par_d: Denominator of the pixel aspect ratio of the input video.
247  * @display_par_n: Numerator of the pixel aspect ratio of the display device
248  * @display_par_d: Denominator of the pixel aspect ratio of the display device
249  *
250  * Given the Pixel Aspect Ratio and size of an input video frame, and the
251  * pixel aspect ratio of the intended display device, calculates the actual
252  * display ratio the video will be rendered with.
253  *
254  * Returns: A boolean indicating success and a calculated Display Ratio in the
255  * dar_n and dar_d parameters.
256  * The return value is FALSE in the case of integer overflow or other error.
257  *
258  * Since: 0.10.7
259  */
260 gboolean
261 gst_video_calculate_display_ratio (guint * dar_n, guint * dar_d,
262     guint video_width, guint video_height,
263     guint video_par_n, guint video_par_d,
264     guint display_par_n, guint display_par_d)
265 {
266   gint num, den;
267   gint tmp_n, tmp_d;
268
269   g_return_val_if_fail (dar_n != NULL, FALSE);
270   g_return_val_if_fail (dar_d != NULL, FALSE);
271
272   /* Calculate (video_width * video_par_n * display_par_d) /
273    * (video_height * video_par_d * display_par_n) */
274   if (!gst_util_fraction_multiply (video_width, video_height, video_par_n,
275           video_par_d, &tmp_n, &tmp_d))
276     goto error_overflow;
277
278   if (!gst_util_fraction_multiply (tmp_n, tmp_d, display_par_d, display_par_n,
279           &num, &den))
280     goto error_overflow;
281
282   g_return_val_if_fail (num > 0, FALSE);
283   g_return_val_if_fail (den > 0, FALSE);
284
285   *dar_n = num;
286   *dar_d = den;
287
288   return TRUE;
289
290   /* ERRORS */
291 error_overflow:
292   {
293     GST_WARNING ("overflow in multiply");
294     return FALSE;
295   }
296 }
297
298 static GstVideoFormat
299 gst_video_format_from_rgb32_masks (int red_mask, int green_mask, int blue_mask)
300 {
301   if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
302       blue_mask == 0x0000ff00) {
303     return GST_VIDEO_FORMAT_RGBx;
304   }
305   if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
306       blue_mask == 0xff000000) {
307     return GST_VIDEO_FORMAT_BGRx;
308   }
309   if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
310       blue_mask == 0x000000ff) {
311     return GST_VIDEO_FORMAT_xRGB;
312   }
313   if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
314       blue_mask == 0x00ff0000) {
315     return GST_VIDEO_FORMAT_xBGR;
316   }
317
318   return GST_VIDEO_FORMAT_UNKNOWN;
319 }
320
321 static GstVideoFormat
322 gst_video_format_from_rgba32_masks (int red_mask, int green_mask,
323     int blue_mask, int alpha_mask)
324 {
325   if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
326       blue_mask == 0x0000ff00 && alpha_mask == 0x000000ff) {
327     return GST_VIDEO_FORMAT_RGBA;
328   }
329   if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
330       blue_mask == 0xff000000 && alpha_mask == 0x000000ff) {
331     return GST_VIDEO_FORMAT_BGRA;
332   }
333   if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
334       blue_mask == 0x000000ff && alpha_mask == 0xff000000) {
335     return GST_VIDEO_FORMAT_ARGB;
336   }
337   if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
338       blue_mask == 0x00ff0000 && alpha_mask == 0xff000000) {
339     return GST_VIDEO_FORMAT_ABGR;
340   }
341   return GST_VIDEO_FORMAT_UNKNOWN;
342 }
343
344 static GstVideoFormat
345 gst_video_format_from_rgb24_masks (int red_mask, int green_mask, int blue_mask)
346 {
347   if (red_mask == 0xff0000 && green_mask == 0x00ff00 && blue_mask == 0x0000ff) {
348     return GST_VIDEO_FORMAT_RGB;
349   }
350   if (red_mask == 0x0000ff && green_mask == 0x00ff00 && blue_mask == 0xff0000) {
351     return GST_VIDEO_FORMAT_BGR;
352   }
353
354   return GST_VIDEO_FORMAT_UNKNOWN;
355 }
356
357 #define GST_VIDEO_COMP1_MASK_16_INT 0xf800
358 #define GST_VIDEO_COMP2_MASK_16_INT 0x07e0
359 #define GST_VIDEO_COMP3_MASK_16_INT 0x001f
360
361 #define GST_VIDEO_COMP1_MASK_15_INT 0x7c00
362 #define GST_VIDEO_COMP2_MASK_15_INT 0x03e0
363 #define GST_VIDEO_COMP3_MASK_15_INT 0x001f
364
365 static GstVideoFormat
366 gst_video_format_from_rgb16_masks (int red_mask, int green_mask, int blue_mask)
367 {
368   if (red_mask == GST_VIDEO_COMP1_MASK_16_INT
369       && green_mask == GST_VIDEO_COMP2_MASK_16_INT
370       && blue_mask == GST_VIDEO_COMP3_MASK_16_INT) {
371     return GST_VIDEO_FORMAT_RGB16;
372   }
373   if (red_mask == GST_VIDEO_COMP3_MASK_16_INT
374       && green_mask == GST_VIDEO_COMP2_MASK_16_INT
375       && blue_mask == GST_VIDEO_COMP1_MASK_16_INT) {
376     return GST_VIDEO_FORMAT_BGR16;
377   }
378   if (red_mask == GST_VIDEO_COMP1_MASK_15_INT
379       && green_mask == GST_VIDEO_COMP2_MASK_15_INT
380       && blue_mask == GST_VIDEO_COMP3_MASK_15_INT) {
381     return GST_VIDEO_FORMAT_RGB15;
382   }
383   if (red_mask == GST_VIDEO_COMP3_MASK_15_INT
384       && green_mask == GST_VIDEO_COMP2_MASK_15_INT
385       && blue_mask == GST_VIDEO_COMP1_MASK_15_INT) {
386     return GST_VIDEO_FORMAT_BGR15;
387   }
388   return GST_VIDEO_FORMAT_UNKNOWN;
389 }
390
391 /**
392  * gst_video_format_from_masks:
393  * @depth: the amount of bits used for a pixel
394  * @bpp: the amount of bits used to store a pixel. This value is bigger than
395  *   @depth
396  * @endianness: the endianness of the masks
397  * @red_mask: the red mask
398  * @green_mask: the green mask
399  * @blue_mask: the blue mask
400  * @alpha_mask: the optional alpha mask
401  *
402  * Find the #GstVideoFormat for the given parameters.
403  *
404  * Returns: a #GstVideoFormat or GST_VIDEO_FORMAT_UNKNOWN when the parameters to
405  * not specify a known format.
406  */
407 GstVideoFormat
408 gst_video_format_from_masks (gint depth, gint bpp, gint endianness,
409     gint red_mask, gint green_mask, gint blue_mask, gint alpha_mask)
410 {
411   GstVideoFormat format;
412
413   /* our caps system handles 24/32bpp RGB as big-endian. */
414   if ((bpp == 24 || bpp == 32) && endianness == G_LITTLE_ENDIAN) {
415     red_mask = GUINT32_TO_BE (red_mask);
416     green_mask = GUINT32_TO_BE (green_mask);
417     blue_mask = GUINT32_TO_BE (blue_mask);
418     endianness = G_BIG_ENDIAN;
419     if (bpp == 24) {
420       red_mask >>= 8;
421       green_mask >>= 8;
422       blue_mask >>= 8;
423     }
424   }
425
426   if (depth == 30 && bpp == 32) {
427     format = GST_VIDEO_FORMAT_r210;
428   } else if (depth == 24 && bpp == 32) {
429     format = gst_video_format_from_rgb32_masks (red_mask, green_mask,
430         blue_mask);
431   } else if (depth == 32 && bpp == 32 && alpha_mask) {
432     format = gst_video_format_from_rgba32_masks (red_mask, green_mask,
433         blue_mask, alpha_mask);
434   } else if (depth == 24 && bpp == 24) {
435     format = gst_video_format_from_rgb24_masks (red_mask, green_mask,
436         blue_mask);
437   } else if ((depth == 15 || depth == 16) && bpp == 16 &&
438       endianness == G_BYTE_ORDER) {
439     format = gst_video_format_from_rgb16_masks (red_mask, green_mask,
440         blue_mask);
441   } else if (depth == 8 && bpp == 8) {
442     format = GST_VIDEO_FORMAT_RGB8_PALETTED;
443   } else if (depth == 64 && bpp == 64) {
444     format = gst_video_format_from_rgba32_masks (red_mask, green_mask,
445         blue_mask, alpha_mask);
446     if (format == GST_VIDEO_FORMAT_ARGB) {
447       format = GST_VIDEO_FORMAT_ARGB64;
448     } else {
449       format = GST_VIDEO_FORMAT_UNKNOWN;
450     }
451   } else {
452     format = GST_VIDEO_FORMAT_UNKNOWN;
453   }
454   return format;
455 }
456
457 /**
458  * gst_video_format_from_fourcc:
459  * @fourcc: a FOURCC value representing raw YUV video
460  *
461  * Converts a FOURCC value into the corresponding #GstVideoFormat.
462  * If the FOURCC cannot be represented by #GstVideoFormat,
463  * #GST_VIDEO_FORMAT_UNKNOWN is returned.
464  *
465  * Since: 0.10.16
466  *
467  * Returns: the #GstVideoFormat describing the FOURCC value
468  */
469 GstVideoFormat
470 gst_video_format_from_fourcc (guint32 fourcc)
471 {
472   switch (fourcc) {
473     case GST_MAKE_FOURCC ('I', '4', '2', '0'):
474       return GST_VIDEO_FORMAT_I420;
475     case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
476       return GST_VIDEO_FORMAT_YV12;
477     case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
478       return GST_VIDEO_FORMAT_YUY2;
479     case GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'):
480       return GST_VIDEO_FORMAT_YVYU;
481     case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
482       return GST_VIDEO_FORMAT_UYVY;
483     case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
484       return GST_VIDEO_FORMAT_AYUV;
485     case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
486       return GST_VIDEO_FORMAT_Y41B;
487     case GST_MAKE_FOURCC ('Y', '4', '2', 'B'):
488       return GST_VIDEO_FORMAT_Y42B;
489     case GST_MAKE_FOURCC ('Y', '4', '4', '4'):
490       return GST_VIDEO_FORMAT_Y444;
491     case GST_MAKE_FOURCC ('v', '2', '1', '0'):
492       return GST_VIDEO_FORMAT_v210;
493     case GST_MAKE_FOURCC ('v', '2', '1', '6'):
494       return GST_VIDEO_FORMAT_v216;
495     case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
496       return GST_VIDEO_FORMAT_NV12;
497     case GST_MAKE_FOURCC ('N', 'V', '2', '1'):
498       return GST_VIDEO_FORMAT_NV21;
499     case GST_MAKE_FOURCC ('v', '3', '0', '8'):
500       return GST_VIDEO_FORMAT_v308;
501     case GST_MAKE_FOURCC ('Y', '8', '0', '0'):
502     case GST_MAKE_FOURCC ('Y', '8', ' ', ' '):
503     case GST_MAKE_FOURCC ('G', 'R', 'E', 'Y'):
504       return GST_VIDEO_FORMAT_Y800;
505     case GST_MAKE_FOURCC ('Y', '1', '6', ' '):
506       return GST_VIDEO_FORMAT_Y16;
507     case GST_MAKE_FOURCC ('U', 'Y', 'V', 'P'):
508       return GST_VIDEO_FORMAT_UYVP;
509     case GST_MAKE_FOURCC ('A', '4', '2', '0'):
510       return GST_VIDEO_FORMAT_A420;
511     case GST_MAKE_FOURCC ('Y', 'U', 'V', '9'):
512       return GST_VIDEO_FORMAT_YUV9;
513     case GST_MAKE_FOURCC ('Y', 'V', 'U', '9'):
514       return GST_VIDEO_FORMAT_YVU9;
515     case GST_MAKE_FOURCC ('I', 'Y', 'U', '1'):
516       return GST_VIDEO_FORMAT_IYU1;
517     case GST_MAKE_FOURCC ('A', 'Y', '6', '4'):
518       return GST_VIDEO_FORMAT_AYUV64;
519     default:
520       return GST_VIDEO_FORMAT_UNKNOWN;
521   }
522 }
523
524 /**
525  * gst_video_format_from_string:
526  * @format: a format string
527  *
528  * Convert the @format string to its #GstVideoFormat.
529  *
530  * Returns: the #GstVideoFormat for @format or GST_VIDEO_FORMAT_UNKNOWN when the
531  * string is not a known format.
532  */
533 GstVideoFormat
534 gst_video_format_from_string (const gchar * format)
535 {
536   guint i;
537
538   for (i = 0; i < G_N_ELEMENTS (formats); i++) {
539     if (strcmp (GST_VIDEO_FORMAT_INFO_NAME (&formats[i].info), format) == 0)
540       return GST_VIDEO_FORMAT_INFO_FORMAT (&formats[i].info);
541   }
542   return GST_VIDEO_FORMAT_UNKNOWN;
543 }
544
545
546 /**
547  * gst_video_format_to_fourcc:
548  * @format: a #GstVideoFormat video format
549  *
550  * Converts a #GstVideoFormat value into the corresponding FOURCC.  Only
551  * a few YUV formats have corresponding FOURCC values.  If @format has
552  * no corresponding FOURCC value, 0 is returned.
553  *
554  * Since: 0.10.16
555  *
556  * Returns: the FOURCC corresponding to @format
557  */
558 guint32
559 gst_video_format_to_fourcc (GstVideoFormat format)
560 {
561   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
562
563   if (format >= G_N_ELEMENTS (formats))
564     return 0;
565
566   return formats[format].fourcc;
567 }
568
569 const gchar *
570 gst_video_format_to_string (GstVideoFormat format)
571 {
572   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
573
574   if (format >= G_N_ELEMENTS (formats))
575     return NULL;
576
577   return GST_VIDEO_FORMAT_INFO_NAME (&formats[format].info);
578 }
579
580 /**
581  * gst_video_format_get_info:
582  * @format: a #GstVideoFormat
583  *
584  * Get the #GstVideoFormatInfo for @format
585  *
586  * Returns: The #GstVideoFormatInfo for @format.
587  */
588 const GstVideoFormatInfo *
589 gst_video_format_get_info (GstVideoFormat format)
590 {
591   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
592   g_return_val_if_fail (format < G_N_ELEMENTS (formats), NULL);
593
594   return &formats[format].info;
595 }
596
597 /**
598  * gst_video_info_init:
599  * @info: a #GstVideoInfo
600  *
601  * Initialize @info with default values.
602  */
603 void
604 gst_video_info_init (GstVideoInfo * info)
605 {
606   g_return_if_fail (info != NULL);
607
608   memset (info, 0, sizeof (GstVideoInfo));
609   info->views = 1;
610   /* arrange for sensible defaults, e.g. if turned into caps */
611   info->fps_n = 0;
612   info->fps_d = 1;
613   info->par_n = 1;
614   info->par_d = 1;
615 }
616
617 /**
618  * gst_video_info_set_format:
619  * @info: a #GstVideoInfo
620  * @format: the format
621  * @width: a width
622  * @height: a height
623  *
624  * Set the default info for a video frame of @format and @width and @height.
625  */
626 void
627 gst_video_info_set_format (GstVideoInfo * info, GstVideoFormat format,
628     guint width, guint height)
629 {
630   const GstVideoFormatInfo *finfo;
631
632   g_return_if_fail (info != NULL);
633   g_return_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN);
634
635   finfo = &formats[format].info;
636
637   info->flags = 0;
638   info->finfo = finfo;
639   info->width = width;
640   info->height = height;
641
642   fill_planes (info);
643 }
644
645 typedef struct
646 {
647   const gchar *name;
648   GstVideoChromaSite site;
649 } ChromaSiteInfo;
650
651 static const ChromaSiteInfo chromasite[] = {
652   {"jpeg", GST_VIDEO_CHROMA_SITE_JPEG},
653   {"mpeg2", GST_VIDEO_CHROMA_SITE_MPEG2},
654   {"dv", GST_VIDEO_CHROMA_SITE_DV}
655 };
656
657 static GstVideoChromaSite
658 gst_video_chroma_from_string (const gchar * s)
659 {
660   gint i;
661   for (i = 0; i < G_N_ELEMENTS (chromasite); i++) {
662     if (g_str_equal (chromasite[i].name, s))
663       return chromasite[i].site;
664   }
665   return GST_VIDEO_CHROMA_SITE_UNKNOWN;
666 }
667
668 static const gchar *
669 gst_video_chroma_to_string (GstVideoChromaSite site)
670 {
671   gint i;
672   for (i = 0; i < G_N_ELEMENTS (chromasite); i++) {
673     if (chromasite[i].site == site)
674       return chromasite[i].name;
675   }
676   return NULL;
677 }
678
679 typedef struct
680 {
681   const gchar *name;
682   GstVideoColorimetry color;
683 } ColorimetryInfo;
684
685 #define MAKE_COLORIMETRY(n,r,m,t,p) { GST_VIDEO_COLORIMETRY_ ##n, \
686   { GST_VIDEO_COLOR_RANGE ##r, GST_VIDEO_COLOR_MATRIX_ ##m, \
687   GST_VIDEO_TRANSFER_ ##t, GST_VIDEO_COLOR_PRIMARIES_ ##p } }
688
689 static const ColorimetryInfo colorimetry[] = {
690   MAKE_COLORIMETRY (BT601, _16_235, BT601, BT709, BT470M),
691   MAKE_COLORIMETRY (BT709, _16_235, BT709, BT709, BT709),
692   MAKE_COLORIMETRY (SMPTE240M, _16_235, SMPTE240M, SMPTE240M, SMPTE240M),
693 };
694
695 static const ColorimetryInfo *
696 gst_video_get_colorimetry (const gchar * s)
697 {
698   gint i;
699
700   for (i = 0; i < G_N_ELEMENTS (colorimetry); i++) {
701     if (g_str_equal (colorimetry[i].name, s))
702       return &colorimetry[i];
703   }
704   return NULL;
705 }
706
707 #define IS_EQUAL(ci,i) (((ci)->color.range == (i)->range) && \
708                         ((ci)->color.matrix == (i)->matrix) && \
709                         ((ci)->color.transfer == (i)->transfer) && \
710                         ((ci)->color.primaries == (i)->primaries))
711
712
713 /**
714  * gst_video_colorimetry_from_string
715  * @cinfo: a #GstVideoColorimetry
716  * @color: a colorimetry string
717  *
718  * Parse the colorimetry string and update @cinfo with the parsed
719  * values.
720  *
721  * Returns: #TRUE if @color points to valid colorimetry info.
722  */
723 gboolean
724 gst_video_colorimetry_from_string (GstVideoColorimetry * cinfo,
725     const gchar * color)
726 {
727   const ColorimetryInfo *ci;
728
729   if ((ci = gst_video_get_colorimetry (color))) {
730     *cinfo = ci->color;
731   } else {
732     /* FIXME, split and parse */
733     cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
734     cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
735     cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
736     cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
737   }
738   return TRUE;
739 }
740
741 static void
742 gst_video_caps_set_colorimetry (GstCaps * caps, GstVideoColorimetry * cinfo)
743 {
744   gint i;
745
746   for (i = 0; i < G_N_ELEMENTS (colorimetry); i++) {
747     if (IS_EQUAL (&colorimetry[i], cinfo)) {
748       gst_caps_set_simple (caps, "colorimetry", G_TYPE_STRING,
749           colorimetry[i].name, NULL);
750       return;
751     }
752   }
753   /* FIXME, construct colorimetry */
754 }
755
756 /**
757  * gst_video_colorimetry_matches:
758  * @info: a #GstVideoInfo
759  * @color: a colorimetry string
760  *
761  * Check if the colorimetry information in @info matches that of the
762  * string @color.
763  *
764  * Returns: #TRUE if @color conveys the same colorimetry info as the color
765  * information in @info.
766  */
767 gboolean
768 gst_video_colorimetry_matches (GstVideoColorimetry * cinfo, const gchar * color)
769 {
770   const ColorimetryInfo *ci;
771
772   if ((ci = gst_video_get_colorimetry (color)))
773     return IS_EQUAL (ci, cinfo);
774
775   return FALSE;
776 }
777
778 /**
779  * gst_video_info_from_caps:
780  * @info: a #GstVideoInfo
781  * @caps: a #GstCaps
782  *
783  * Parse @caps and update @info.
784  *
785  * Returns: TRUE if @caps could be parsed
786  */
787 gboolean
788 gst_video_info_from_caps (GstVideoInfo * info, const GstCaps * caps)
789 {
790   GstStructure *structure;
791   const gchar *s;
792   GstVideoFormat format;
793   gint width, height, views;
794   gint fps_n, fps_d;
795   gboolean interlaced;
796   gint par_n, par_d;
797
798   g_return_val_if_fail (info != NULL, FALSE);
799   g_return_val_if_fail (caps != NULL, FALSE);
800   g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
801
802   GST_DEBUG ("parsing caps %" GST_PTR_FORMAT, caps);
803
804   structure = gst_caps_get_structure (caps, 0);
805
806   if (!gst_structure_has_name (structure, "video/x-raw"))
807     goto wrong_name;
808
809   if (!(s = gst_structure_get_string (structure, "format")))
810     goto no_format;
811
812   format = gst_video_format_from_string (s);
813   if (format == GST_VIDEO_FORMAT_UNKNOWN)
814     goto unknown_format;
815
816   if (!gst_structure_get_int (structure, "width", &width))
817     goto no_width;
818   if (!gst_structure_get_int (structure, "height", &height))
819     goto no_height;
820
821   gst_video_info_set_format (info, format, width, height);
822
823   if (gst_structure_get_fraction (structure, "framerate", &fps_n, &fps_d)) {
824     info->fps_n = fps_n;
825     info->fps_d = fps_d;
826   } else {
827     info->fps_n = 0;
828     info->fps_d = 1;
829   }
830
831   if (!gst_structure_get_boolean (structure, "interlaced", &interlaced))
832     interlaced = FALSE;
833   if (interlaced)
834     info->flags |= GST_VIDEO_FLAG_INTERLACED;
835   else
836     info->flags &= ~GST_VIDEO_FLAG_INTERLACED;
837
838   if (gst_structure_get_int (structure, "views", &views))
839     info->views = views;
840   else
841     info->views = 1;
842
843   if ((s = gst_structure_get_string (structure, "chroma-site")))
844     info->chroma_site = gst_video_chroma_from_string (s);
845
846   if ((s = gst_structure_get_string (structure, "colorimetry")))
847     gst_video_colorimetry_from_string (&info->colorimetry, s);
848
849   if (gst_structure_get_fraction (structure, "pixel-aspect-ratio",
850           &par_n, &par_d)) {
851     info->par_n = par_n;
852     info->par_d = par_d;
853   } else {
854     info->par_n = 1;
855     info->par_d = 1;
856   }
857   return TRUE;
858
859   /* ERROR */
860 wrong_name:
861   {
862     GST_ERROR ("wrong name, expected video/x-raw");
863     return FALSE;
864   }
865 no_format:
866   {
867     GST_ERROR ("no format given");
868     return FALSE;
869   }
870 unknown_format:
871   {
872     GST_ERROR ("unknown format given");
873     return FALSE;
874   }
875 no_width:
876   {
877     GST_ERROR ("no width property given");
878     return FALSE;
879   }
880 no_height:
881   {
882     GST_ERROR ("no height property given");
883     return FALSE;
884   }
885 }
886
887 /**
888  * gst_video_info_to_caps:
889  * @info: a #GstVideoInfo
890  *
891  * Convert the values of @info into a #GstCaps.
892  *
893  * Returns: a new #GstCaps containing the info of @info.
894  */
895 GstCaps *
896 gst_video_info_to_caps (GstVideoInfo * info)
897 {
898   GstCaps *caps;
899   const gchar *format;
900
901   g_return_val_if_fail (info != NULL, NULL);
902   g_return_val_if_fail (info->finfo != NULL, NULL);
903   g_return_val_if_fail (info->finfo->format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
904
905   format = gst_video_format_to_string (info->finfo->format);
906   g_return_val_if_fail (format != NULL, NULL);
907
908   caps = gst_caps_new_simple ("video/x-raw",
909       "format", G_TYPE_STRING, format,
910       "width", G_TYPE_INT, info->width,
911       "height", G_TYPE_INT, info->height,
912       "framerate", GST_TYPE_FRACTION, info->fps_n, info->fps_d,
913       "pixel-aspect-ratio", GST_TYPE_FRACTION, info->par_n, info->par_d, NULL);
914
915   if (info->flags & GST_VIDEO_FLAG_INTERLACED)
916     gst_caps_set_simple (caps, "interlaced", G_TYPE_BOOLEAN, TRUE, NULL);
917
918   if (info->chroma_site != GST_VIDEO_CHROMA_SITE_UNKNOWN)
919     gst_caps_set_simple (caps, "chroma-site", G_TYPE_STRING,
920         gst_video_chroma_to_string (info->chroma_site), NULL);
921
922   gst_video_caps_set_colorimetry (caps, &info->colorimetry);
923
924   if (info->views > 1)
925     gst_caps_set_simple (caps, "views", G_TYPE_INT, info->views, NULL);
926
927   return caps;
928 }
929
930 /**
931  * gst_video_frame_map_id:
932  * @frame: pointer to #GstVideoFrame
933  * @info: a #GstVideoInfo
934  * @buffer: the buffer to map
935  * @id: the frame id to map
936  * @flags: #GstMapFlags
937  *
938  * Use @info and @buffer to fill in the values of @frame with the video frame
939  * information of frame @id.
940  *
941  * When @id is -1, the default frame is mapped. When @id != -1, this function
942  * will return %FALSE when there is no GstMetaVideo with that id.
943  *
944  * All video planes of @buffer will be mapped and the pointers will be set in
945  * @frame->data.
946  *
947  * Returns: %TRUE on success.
948  */
949 gboolean
950 gst_video_frame_map_id (GstVideoFrame * frame, GstVideoInfo * info,
951     GstBuffer * buffer, gint id, GstMapFlags flags)
952 {
953   GstMetaVideo *meta;
954   guint8 *data;
955   gsize size;
956   gint i;
957
958   g_return_val_if_fail (frame != NULL, FALSE);
959   g_return_val_if_fail (info != NULL, FALSE);
960   g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
961
962   if (id == -1)
963     meta = gst_buffer_get_meta_video (buffer);
964   else
965     meta = gst_buffer_get_meta_video_id (buffer, id);
966
967   frame->buffer = buffer;
968   frame->meta = meta;
969
970   if (meta) {
971     frame->info.flags = meta->flags;
972     frame->info.finfo = &formats[meta->format].info;
973     frame->info.width = meta->width;
974     frame->info.height = meta->height;
975     frame->id = meta->id;
976
977     for (i = 0; i < info->finfo->n_planes; i++) {
978       frame->data[i] =
979           gst_meta_video_map (meta, i, &frame->info.stride[i], flags);
980     }
981   } else {
982     /* no metadata, we really need to have the metadata when the id is
983      * specified. */
984     if (id != -1)
985       goto no_metadata;
986
987     /* copy the info */
988     frame->info = *info;
989     frame->id = id;
990
991     data = gst_buffer_map (buffer, &size, NULL, flags);
992
993     /* do some sanity checks */
994     if (size < info->size)
995       goto invalid_size;
996
997     /* set up pointers */
998     for (i = 0; i < info->finfo->n_planes; i++) {
999       frame->data[i] = data + info->offset[i];
1000     }
1001   }
1002   return TRUE;
1003
1004   /* ERRORS */
1005 no_metadata:
1006   {
1007     GST_ERROR ("no GstMetaVideo for id", id);
1008     return FALSE;
1009   }
1010 invalid_size:
1011   {
1012     GST_ERROR ("invalid buffer size %" G_GSIZE_FORMAT " < %" G_GSIZE_FORMAT,
1013         size, info->size);
1014     gst_buffer_unmap (buffer, data, size);
1015     return FALSE;
1016   }
1017 }
1018
1019 /**
1020  * gst_video_frame_map:
1021  * @frame: pointer to #GstVideoFrame
1022  * @info: a #GstVideoInfo
1023  * @buffer: the buffer to map
1024  * @flags: #GstMapFlags
1025  *
1026  * Use @info and @buffer to fill in the values of @frame.
1027  *
1028  * All video planes of @buffer will be mapped and the pointers will be set in
1029  * @frame->data.
1030  *
1031  * Returns: %TRUE on success.
1032  */
1033 gboolean
1034 gst_video_frame_map (GstVideoFrame * frame, GstVideoInfo * info,
1035     GstBuffer * buffer, GstMapFlags flags)
1036 {
1037   return gst_video_frame_map_id (frame, info, buffer, -1, flags);
1038 }
1039
1040 /**
1041  * gst_video_frame_unmap:
1042  * @frame: a #GstVideoFrame
1043  *
1044  * Unmap the memory previously mapped with gst_video_frame_map.
1045  */
1046 void
1047 gst_video_frame_unmap (GstVideoFrame * frame)
1048 {
1049   GstBuffer *buffer;
1050   GstMetaVideo *meta;
1051   gint i;
1052
1053   g_return_if_fail (frame != NULL);
1054
1055   buffer = frame->buffer;
1056   meta = frame->meta;
1057
1058   if (meta) {
1059     for (i = 0; i < frame->info.finfo->n_planes; i++) {
1060       gst_meta_video_unmap (meta, i, frame->data[i]);
1061     }
1062   } else {
1063     guint8 *data;
1064
1065     data = frame->data[0];
1066     data -= frame->info.offset[0];
1067     gst_buffer_unmap (buffer, data, -1);
1068   }
1069 }
1070
1071 /**
1072  * gst_video_frame_copy:
1073  * @dest: a #GstVideoFrame
1074  * @src: a #GstVideoFrame
1075  *
1076  * Copy the contents from @src to @dest.
1077  *
1078  * Returns: TRUE if the contents could be copied.
1079  */
1080 gboolean
1081 gst_video_frame_copy (GstVideoFrame * dest, const GstVideoFrame * src)
1082 {
1083   guint i, n_planes;
1084   const GstVideoInfo *sinfo;
1085   GstVideoInfo *dinfo;
1086
1087   sinfo = &src->info;
1088   dinfo = &dest->info;
1089
1090   g_return_val_if_fail (dinfo->finfo->format == sinfo->finfo->format, FALSE);
1091   g_return_val_if_fail (dinfo->width == sinfo->width
1092       && dinfo->height == sinfo->height, FALSE);
1093
1094   n_planes = dinfo->finfo->n_planes;
1095
1096   for (i = 0; i < n_planes; i++) {
1097     guint w, h, j;
1098     guint8 *sp, *dp;
1099     gint ss, ds;
1100
1101     sp = src->data[i];
1102     dp = dest->data[i];
1103
1104     ss = sinfo->stride[i];
1105     ds = dinfo->stride[i];
1106
1107     w = MIN (ABS (ss), ABS (ds));
1108     h = GST_VIDEO_FRAME_COMP_HEIGHT (dest, i);
1109
1110     GST_DEBUG ("w %d h %d", w, h);
1111
1112     for (j = 0; j < h; j++) {
1113       memcpy (dp, sp, w);
1114       dp += ds;
1115       sp += ss;
1116     }
1117   }
1118   return TRUE;
1119 }
1120
1121 static int
1122 fill_planes (GstVideoInfo * info)
1123 {
1124   gint width, height;
1125
1126   width = info->width;
1127   height = info->height;
1128
1129   switch (info->finfo->format) {
1130     case GST_VIDEO_FORMAT_YUY2:
1131     case GST_VIDEO_FORMAT_YVYU:
1132     case GST_VIDEO_FORMAT_UYVY:
1133       info->stride[0] = GST_ROUND_UP_4 (width * 2);
1134       info->offset[0] = 0;
1135       info->size = info->stride[0] * height;
1136       break;
1137     case GST_VIDEO_FORMAT_AYUV:
1138     case GST_VIDEO_FORMAT_RGBx:
1139     case GST_VIDEO_FORMAT_RGBA:
1140     case GST_VIDEO_FORMAT_BGRx:
1141     case GST_VIDEO_FORMAT_BGRA:
1142     case GST_VIDEO_FORMAT_xRGB:
1143     case GST_VIDEO_FORMAT_ARGB:
1144     case GST_VIDEO_FORMAT_xBGR:
1145     case GST_VIDEO_FORMAT_ABGR:
1146     case GST_VIDEO_FORMAT_r210:
1147       info->stride[0] = width * 4;
1148       info->offset[0] = 0;
1149       info->size = info->stride[0] * height;
1150       break;
1151     case GST_VIDEO_FORMAT_RGB16:
1152     case GST_VIDEO_FORMAT_BGR16:
1153     case GST_VIDEO_FORMAT_RGB15:
1154     case GST_VIDEO_FORMAT_BGR15:
1155       info->stride[0] = GST_ROUND_UP_4 (width * 2);
1156       info->offset[0] = 0;
1157       info->size = info->stride[0] * height;
1158       break;
1159     case GST_VIDEO_FORMAT_RGB:
1160     case GST_VIDEO_FORMAT_BGR:
1161     case GST_VIDEO_FORMAT_v308:
1162       info->stride[0] = GST_ROUND_UP_4 (width * 3);
1163       info->offset[0] = 0;
1164       info->size = info->stride[0] * height;
1165       break;
1166     case GST_VIDEO_FORMAT_v210:
1167       info->stride[0] = ((width + 47) / 48) * 128;
1168       info->offset[0] = 0;
1169       info->size = info->stride[0] * height;
1170       break;
1171     case GST_VIDEO_FORMAT_v216:
1172       info->stride[0] = GST_ROUND_UP_8 (width * 4);
1173       info->offset[0] = 0;
1174       info->size = info->stride[0] * height;
1175       break;
1176     case GST_VIDEO_FORMAT_GRAY8:
1177     case GST_VIDEO_FORMAT_Y800:
1178       info->stride[0] = GST_ROUND_UP_4 (width);
1179       info->offset[0] = 0;
1180       info->size = info->stride[0] * height;
1181       break;
1182     case GST_VIDEO_FORMAT_GRAY16_BE:
1183     case GST_VIDEO_FORMAT_GRAY16_LE:
1184     case GST_VIDEO_FORMAT_Y16:
1185       info->stride[0] = GST_ROUND_UP_4 (width * 2);
1186       info->offset[0] = 0;
1187       info->size = info->stride[0] * height;
1188       break;
1189     case GST_VIDEO_FORMAT_UYVP:
1190       info->stride[0] = GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4);
1191       info->offset[0] = 0;
1192       info->size = info->stride[0] * height;
1193       break;
1194     case GST_VIDEO_FORMAT_RGB8_PALETTED:
1195       info->stride[0] = GST_ROUND_UP_4 (width);
1196       info->offset[0] = 0;
1197       info->size = info->stride[0] * height;
1198       break;
1199     case GST_VIDEO_FORMAT_IYU1:
1200       info->stride[0] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) +
1201           GST_ROUND_UP_4 (width) / 2);
1202       info->offset[0] = 0;
1203       info->size = info->stride[0] * height;
1204       break;
1205     case GST_VIDEO_FORMAT_ARGB64:
1206     case GST_VIDEO_FORMAT_AYUV64:
1207       info->stride[0] = width * 8;
1208       info->offset[0] = 0;
1209       info->size = info->stride[0] * height;
1210       break;
1211     case GST_VIDEO_FORMAT_I420:
1212       info->stride[0] = GST_ROUND_UP_4 (width);
1213       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1214       info->stride[2] = info->stride[1];
1215       info->offset[0] = 0;
1216       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1217       info->offset[2] = info->offset[1] +
1218           info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
1219       info->size = info->offset[2] +
1220           info->stride[2] * (GST_ROUND_UP_2 (height) / 2);
1221       break;
1222     case GST_VIDEO_FORMAT_YV12:        /* same as I420, but plane 1+2 swapped */
1223       info->stride[0] = GST_ROUND_UP_4 (width);
1224       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1225       info->stride[2] = info->stride[1];
1226       info->offset[0] = 0;
1227       info->offset[2] = info->stride[0] * GST_ROUND_UP_2 (height);
1228       info->offset[1] = info->offset[2] +
1229           info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
1230       info->size = info->offset[1] +
1231           info->stride[2] * (GST_ROUND_UP_2 (height) / 2);
1232       break;
1233     case GST_VIDEO_FORMAT_Y41B:
1234       info->stride[0] = GST_ROUND_UP_4 (width);
1235       info->stride[1] = GST_ROUND_UP_16 (width) / 4;
1236       info->stride[2] = info->stride[1];
1237       info->offset[0] = 0;
1238       info->offset[1] = info->stride[0] * height;
1239       info->offset[2] = info->offset[1] + info->stride[1] * height;
1240       /* simplification of ROUNDUP4(w)*h + 2*((ROUNDUP16(w)/4)*h */
1241       info->size = (info->stride[0] + (GST_ROUND_UP_16 (width) / 2)) * height;
1242       break;
1243     case GST_VIDEO_FORMAT_Y42B:
1244       info->stride[0] = GST_ROUND_UP_4 (width);
1245       info->stride[1] = GST_ROUND_UP_8 (width) / 2;
1246       info->stride[2] = info->stride[1];
1247       info->offset[0] = 0;
1248       info->offset[1] = info->stride[0] * height;
1249       info->offset[2] = info->offset[1] + info->stride[1] * height;
1250       /* simplification of ROUNDUP4(w)*h + 2*(ROUNDUP8(w)/2)*h */
1251       info->size = (info->stride[0] + GST_ROUND_UP_8 (width)) * height;
1252       break;
1253     case GST_VIDEO_FORMAT_Y444:
1254       info->stride[0] = GST_ROUND_UP_4 (width);
1255       info->stride[1] = info->stride[0];
1256       info->stride[2] = info->stride[0];
1257       info->offset[0] = 0;
1258       info->offset[1] = info->stride[0] * height;
1259       info->offset[2] = info->offset[1] * 2;
1260       info->size = info->stride[0] * height * 3;
1261       break;
1262     case GST_VIDEO_FORMAT_NV12:
1263     case GST_VIDEO_FORMAT_NV21:
1264       info->stride[0] = GST_ROUND_UP_4 (width);
1265       info->stride[1] = info->stride[0];
1266       info->offset[0] = 0;
1267       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1268       info->size = info->stride[0] * GST_ROUND_UP_2 (height) * 3 / 2;
1269       break;
1270     case GST_VIDEO_FORMAT_A420:
1271       info->stride[0] = GST_ROUND_UP_4 (width);
1272       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1273       info->stride[2] = info->stride[1];
1274       info->stride[3] = info->stride[0];
1275       info->offset[0] = 0;
1276       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1277       info->offset[2] = info->offset[1] +
1278           info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
1279       info->offset[3] = info->offset[2] +
1280           info->stride[2] * (GST_ROUND_UP_2 (height) / 2);
1281       info->size = info->offset[3] + info->stride[0];
1282       break;
1283     case GST_VIDEO_FORMAT_YUV9:
1284       info->stride[0] = GST_ROUND_UP_4 (width);
1285       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
1286       info->stride[2] = info->stride[1];
1287       info->offset[0] = 0;
1288       info->offset[1] = info->stride[0] * height;
1289       info->offset[2] = info->offset[1] +
1290           info->stride[1] * (GST_ROUND_UP_4 (height) / 4);
1291       info->size = info->offset[2] +
1292           info->stride[2] * (GST_ROUND_UP_4 (height) / 4);
1293       break;
1294     case GST_VIDEO_FORMAT_YVU9:
1295       info->stride[0] = GST_ROUND_UP_4 (width);
1296       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
1297       info->stride[2] = info->stride[1];
1298       info->offset[0] = 0;
1299       info->offset[2] = info->stride[0] * height;
1300       info->offset[1] = info->offset[2] +
1301           info->stride[1] * (GST_ROUND_UP_4 (height) / 4);
1302       info->size = info->offset[2] +
1303           info->stride[2] * (GST_ROUND_UP_4 (height) / 4);
1304       break;
1305     case GST_VIDEO_FORMAT_UNKNOWN:
1306       GST_ERROR ("invalid format");
1307       g_warning ("invalid format");
1308       break;
1309   }
1310   return 0;
1311 }
1312
1313 /**
1314  * gst_video_format_convert:
1315  * @info: a #GstVideoInfo
1316  * @src_format: #GstFormat of the @src_value
1317  * @src_value: value to convert
1318  * @dest_format: #GstFormat of the @dest_value
1319  * @dest_value: pointer to destination value
1320  *
1321  * Converts among various #GstFormat types.  This function handles
1322  * GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT.  For
1323  * raw video, GST_FORMAT_DEFAULT corresponds to video frames.  This
1324  * function can be used to handle pad queries of the type GST_QUERY_CONVERT.
1325  *
1326  * Since: 0.10.16
1327  *
1328  * Returns: TRUE if the conversion was successful.
1329  */
1330 gboolean
1331 gst_video_info_convert (GstVideoInfo * info,
1332     GstFormat src_format, gint64 src_value,
1333     GstFormat dest_format, gint64 * dest_value)
1334 {
1335   gboolean ret = FALSE;
1336   int size, fps_n, fps_d;
1337
1338   g_return_val_if_fail (info != NULL, 0);
1339   g_return_val_if_fail (info->finfo != NULL, 0);
1340   g_return_val_if_fail (info->finfo->format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1341   g_return_val_if_fail (info->size > 0, 0);
1342
1343   size = info->size;
1344   fps_n = info->fps_n;
1345   fps_d = info->fps_d;
1346
1347   GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s to %s",
1348       src_value, gst_format_get_name (src_format),
1349       gst_format_get_name (dest_format));
1350
1351   if (src_format == dest_format) {
1352     *dest_value = src_value;
1353     ret = TRUE;
1354     goto done;
1355   }
1356
1357   if (src_value == -1) {
1358     *dest_value = -1;
1359     ret = TRUE;
1360     goto done;
1361   }
1362
1363   /* bytes to frames */
1364   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_DEFAULT) {
1365     if (size != 0) {
1366       *dest_value = gst_util_uint64_scale_int (src_value, 1, size);
1367     } else {
1368       GST_ERROR ("blocksize is 0");
1369       *dest_value = 0;
1370     }
1371     ret = TRUE;
1372     goto done;
1373   }
1374
1375   /* frames to bytes */
1376   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_BYTES) {
1377     *dest_value = gst_util_uint64_scale_int (src_value, size, 1);
1378     ret = TRUE;
1379     goto done;
1380   }
1381
1382   /* time to frames */
1383   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_DEFAULT) {
1384     if (fps_d != 0) {
1385       *dest_value = gst_util_uint64_scale (src_value,
1386           fps_n, GST_SECOND * fps_d);
1387     } else {
1388       GST_ERROR ("framerate denominator is 0");
1389       *dest_value = 0;
1390     }
1391     ret = TRUE;
1392     goto done;
1393   }
1394
1395   /* frames to time */
1396   if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) {
1397     if (fps_n != 0) {
1398       *dest_value = gst_util_uint64_scale (src_value,
1399           GST_SECOND * fps_d, fps_n);
1400     } else {
1401       GST_ERROR ("framerate numerator is 0");
1402       *dest_value = 0;
1403     }
1404     ret = TRUE;
1405     goto done;
1406   }
1407
1408   /* time to bytes */
1409   if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
1410     if (fps_d != 0) {
1411       *dest_value = gst_util_uint64_scale (src_value,
1412           fps_n * size, GST_SECOND * fps_d);
1413     } else {
1414       GST_ERROR ("framerate denominator is 0");
1415       *dest_value = 0;
1416     }
1417     ret = TRUE;
1418     goto done;
1419   }
1420
1421   /* bytes to time */
1422   if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
1423     if (fps_n != 0 && size != 0) {
1424       *dest_value = gst_util_uint64_scale (src_value,
1425           GST_SECOND * fps_d, fps_n * size);
1426     } else {
1427       GST_ERROR ("framerate denominator and/or blocksize is 0");
1428       *dest_value = 0;
1429     }
1430     ret = TRUE;
1431   }
1432
1433 done:
1434
1435   GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, ret, *dest_value);
1436
1437   return ret;
1438 }
1439
1440 #define GST_VIDEO_EVENT_STILL_STATE_NAME "GstEventStillFrame"
1441
1442 /**
1443  * gst_video_event_new_still_frame:
1444  * @in_still: boolean value for the still-frame state of the event.
1445  *
1446  * Creates a new Still Frame event. If @in_still is %TRUE, then the event
1447  * represents the start of a still frame sequence. If it is %FALSE, then
1448  * the event ends a still frame sequence.
1449  *
1450  * To parse an event created by gst_video_event_new_still_frame() use
1451  * gst_video_event_parse_still_frame().
1452  *
1453  * Returns: The new GstEvent
1454  * Since: 0.10.26
1455  */
1456 GstEvent *
1457 gst_video_event_new_still_frame (gboolean in_still)
1458 {
1459   GstEvent *still_event;
1460   GstStructure *s;
1461
1462   s = gst_structure_new (GST_VIDEO_EVENT_STILL_STATE_NAME,
1463       "still-state", G_TYPE_BOOLEAN, in_still, NULL);
1464   still_event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1465
1466   return still_event;
1467 }
1468
1469 /**
1470  * gst_video_event_parse_still_frame:
1471  * @event: A #GstEvent to parse
1472  * @in_still: A boolean to receive the still-frame status from the event, or NULL
1473  *
1474  * Parse a #GstEvent, identify if it is a Still Frame event, and
1475  * return the still-frame state from the event if it is.
1476  * If the event represents the start of a still frame, the in_still
1477  * variable will be set to TRUE, otherwise FALSE. It is OK to pass NULL for the
1478  * in_still variable order to just check whether the event is a valid still-frame
1479  * event.
1480  *
1481  * Create a still frame event using gst_video_event_new_still_frame()
1482  *
1483  * Returns: %TRUE if the event is a valid still-frame event. %FALSE if not
1484  * Since: 0.10.26
1485  */
1486 gboolean
1487 gst_video_event_parse_still_frame (GstEvent * event, gboolean * in_still)
1488 {
1489   const GstStructure *s;
1490   gboolean ev_still_state;
1491
1492   g_return_val_if_fail (event != NULL, FALSE);
1493
1494   if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM)
1495     return FALSE;               /* Not a still frame event */
1496
1497   s = gst_event_get_structure (event);
1498   if (s == NULL
1499       || !gst_structure_has_name (s, GST_VIDEO_EVENT_STILL_STATE_NAME))
1500     return FALSE;               /* Not a still frame event */
1501   if (!gst_structure_get_boolean (s, "still-state", &ev_still_state))
1502     return FALSE;               /* Not a still frame event */
1503   if (in_still)
1504     *in_still = ev_still_state;
1505   return TRUE;
1506 }
1507
1508 /**
1509  * gst_video_parse_caps_palette:
1510  * @caps: #GstCaps to parse
1511  *
1512  * Returns the palette data from the caps as a #GstBuffer. For
1513  * #GST_VIDEO_FORMAT_RGB8_PALETTED this is containing 256 #guint32
1514  * values, each containing ARGB colors in native endianness.
1515  *
1516  * Returns: a #GstBuffer containing the palette data. Unref after usage.
1517  * Since: 0.10.32
1518  */
1519 GstBuffer *
1520 gst_video_parse_caps_palette (GstCaps * caps)
1521 {
1522   GstStructure *s;
1523   const GValue *p_v;
1524   GstBuffer *p;
1525
1526   if (!gst_caps_is_fixed (caps))
1527     return NULL;
1528
1529   s = gst_caps_get_structure (caps, 0);
1530
1531   p_v = gst_structure_get_value (s, "palette_data");
1532   if (!p_v || !GST_VALUE_HOLDS_BUFFER (p_v))
1533     return NULL;
1534
1535   p = g_value_dup_boxed (p_v);
1536
1537   return p;
1538 }