2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Library <2002> Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * Copyright (C) 2007 David A. Schleef <ds@schleef.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
29 #include "gstvideometa.h"
31 static int fill_planes (GstVideoInfo * info);
36 GstVideoFormatInfo info;
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 }
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 }
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 }
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 }
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 }
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 } }
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 } }
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 } }
116 static VideoFormat formats[] = {
117 {0x00000000, {GST_VIDEO_FORMAT_UNKNOWN, "UNKNOWN", "unknown video", 0, DPTH0,
121 MAKE_YUV_FORMAT (I420, "raw video", GST_MAKE_FOURCC ('I', '4', '2', '0'),
123 PLANE012, OFFS0, SUB420),
124 MAKE_YUV_FORMAT (YV12, "raw video", GST_MAKE_FOURCC ('Y', 'V', '1', '2'),
126 PLANE021, OFFS0, SUB420),
127 MAKE_YUV_FORMAT (YUY2, "raw video", GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'),
129 PLANE0, OFFS013, SUB422),
130 MAKE_YUV_FORMAT (UYVY, "raw video", GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'),
132 PLANE0, OFFS102, SUB422),
133 MAKE_YUVA_FORMAT (AYUV, "raw video", GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'),
135 PSTR4444, PLANE0, OFFS1230, SUB4444),
136 MAKE_RGB_FORMAT (RGBx, "raw video", DPTH888, PSTR444, PLANE0, OFFS012,
138 MAKE_RGB_FORMAT (BGRx, "raw video", DPTH888, PSTR444, PLANE0, OFFS210,
140 MAKE_RGB_FORMAT (xRGB, "raw video", DPTH888, PSTR444, PLANE0, OFFS123,
142 MAKE_RGB_FORMAT (xBGR, "raw video", DPTH888, PSTR444, PLANE0, OFFS321,
144 MAKE_RGBA_FORMAT (RGBA, "raw video", DPTH8888, PSTR4444, PLANE0, OFFS0123,
146 MAKE_RGBA_FORMAT (BGRA, "raw video", DPTH8888, PSTR4444, PLANE0, OFFS2103,
148 MAKE_RGBA_FORMAT (ARGB, "raw video", DPTH8888, PSTR4444, PLANE0, OFFS1230,
150 MAKE_RGBA_FORMAT (ABGR, "raw video", DPTH8888, PSTR4444, PLANE0, OFFS3210,
152 MAKE_RGB_FORMAT (RGB, "raw video", DPTH888, PSTR333, PLANE0, OFFS012, SUB444),
153 MAKE_RGB_FORMAT (BGR, "raw video", DPTH888, PSTR333, PLANE0, OFFS210, SUB444),
155 MAKE_YUV_FORMAT (Y41B, "raw video", GST_MAKE_FOURCC ('Y', '4', '1', 'B'),
157 PLANE012, OFFS0, SUB411),
158 MAKE_YUV_FORMAT (Y42B, "raw video", GST_MAKE_FOURCC ('Y', '4', '2', 'B'),
160 PLANE012, OFFS0, SUB422),
161 MAKE_YUV_FORMAT (YVYU, "raw video", GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'),
163 PLANE0, OFFS031, SUB422),
164 MAKE_YUV_FORMAT (Y444, "raw video", GST_MAKE_FOURCC ('Y', '4', '4', '4'),
166 PLANE012, OFFS0, SUB444),
167 MAKE_YUV_FORMAT (v210, "raw video", GST_MAKE_FOURCC ('v', '2', '1', '0'),
169 PSTR0, PLANE0, OFFS0, SUB422),
170 MAKE_YUV_FORMAT (v216, "raw video", GST_MAKE_FOURCC ('v', '2', '1', '6'),
172 PSTR488, PLANE0, OFFS026, SUB422),
173 MAKE_YUV_FORMAT (NV12, "raw video", GST_MAKE_FOURCC ('N', 'V', '1', '2'),
175 PLANE011, OFFS001, SUB420),
176 MAKE_YUV_FORMAT (NV21, "raw video", GST_MAKE_FOURCC ('N', 'V', '2', '1'),
178 PLANE011, OFFS010, SUB420),
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),
184 MAKE_YUV_FORMAT (v308, "raw video", GST_MAKE_FOURCC ('v', '3', '0', '8'),
186 PLANE0, OFFS012, SUB444),
187 MAKE_YUV_FORMAT (Y800, "raw video", GST_MAKE_FOURCC ('Y', '8', '0', '0'),
189 PLANE0, OFFS0, SUB4),
190 MAKE_YUV_FORMAT (Y16, "raw video", GST_MAKE_FOURCC ('Y', '1', '6', ' '),
192 PLANE0, OFFS0, SUB4),
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),
199 MAKE_YUV_FORMAT (UYVP, "raw video", GST_MAKE_FOURCC ('U', 'Y', 'V', 'P'),
201 PSTR0, PLANE0, OFFS0, SUB422),
202 MAKE_YUVA_FORMAT (A420, "raw video", GST_MAKE_FOURCC ('A', '4', '2', '0'),
204 PSTR1111, PLANE0123, OFFS0, SUB4204),
205 MAKE_RGBA_FORMAT (RGB8_PALETTED, "raw video", DPTH8888, PSTR1111, PLANE0,
207 MAKE_YUV_FORMAT (YUV9, "raw video", GST_MAKE_FOURCC ('Y', 'U', 'V', '9'),
209 PLANE012, OFFS0, SUB410),
210 MAKE_YUV_FORMAT (YVU9, "raw video", GST_MAKE_FOURCC ('Y', 'V', 'U', '9'),
212 PLANE021, OFFS0, SUB410),
213 MAKE_YUV_FORMAT (IYU1, "raw video", GST_MAKE_FOURCC ('I', 'Y', 'U', '1'),
215 PLANE0, OFFS104, SUB411),
216 MAKE_RGBA_FORMAT (ARGB64, "raw video", DPTH16_16_16_16, PSTR8888, PLANE0,
219 MAKE_YUVA_FORMAT (AYUV64, "raw video", 0x00000000, DPTH16_16_16_16, PSTR8888,
222 MAKE_YUV_FORMAT (r210, "raw video", GST_MAKE_FOURCC ('r', '2', '1', '0'),
224 PSTR444, PLANE0, OFFS0, SUB444),
229 * @short_description: Support library for video operations
233 * This library contains some helper functions and includes the
234 * videosink and videofilter base classes.
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
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.
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.
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)
269 g_return_val_if_fail (dar_n != NULL, FALSE);
270 g_return_val_if_fail (dar_d != NULL, FALSE);
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))
278 if (!gst_util_fraction_multiply (tmp_n, tmp_d, display_par_d, display_par_n,
282 g_return_val_if_fail (num > 0, FALSE);
283 g_return_val_if_fail (den > 0, FALSE);
293 GST_WARNING ("overflow in multiply");
298 static GstVideoFormat
299 gst_video_format_from_rgb32_masks (int red_mask, int green_mask, int blue_mask)
301 if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
302 blue_mask == 0x0000ff00) {
303 return GST_VIDEO_FORMAT_RGBx;
305 if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
306 blue_mask == 0xff000000) {
307 return GST_VIDEO_FORMAT_BGRx;
309 if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
310 blue_mask == 0x000000ff) {
311 return GST_VIDEO_FORMAT_xRGB;
313 if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
314 blue_mask == 0x00ff0000) {
315 return GST_VIDEO_FORMAT_xBGR;
318 return GST_VIDEO_FORMAT_UNKNOWN;
321 static GstVideoFormat
322 gst_video_format_from_rgba32_masks (int red_mask, int green_mask,
323 int blue_mask, int alpha_mask)
325 if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
326 blue_mask == 0x0000ff00 && alpha_mask == 0x000000ff) {
327 return GST_VIDEO_FORMAT_RGBA;
329 if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
330 blue_mask == 0xff000000 && alpha_mask == 0x000000ff) {
331 return GST_VIDEO_FORMAT_BGRA;
333 if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
334 blue_mask == 0x000000ff && alpha_mask == 0xff000000) {
335 return GST_VIDEO_FORMAT_ARGB;
337 if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
338 blue_mask == 0x00ff0000 && alpha_mask == 0xff000000) {
339 return GST_VIDEO_FORMAT_ABGR;
341 return GST_VIDEO_FORMAT_UNKNOWN;
344 static GstVideoFormat
345 gst_video_format_from_rgb24_masks (int red_mask, int green_mask, int blue_mask)
347 if (red_mask == 0xff0000 && green_mask == 0x00ff00 && blue_mask == 0x0000ff) {
348 return GST_VIDEO_FORMAT_RGB;
350 if (red_mask == 0x0000ff && green_mask == 0x00ff00 && blue_mask == 0xff0000) {
351 return GST_VIDEO_FORMAT_BGR;
354 return GST_VIDEO_FORMAT_UNKNOWN;
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
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
365 static GstVideoFormat
366 gst_video_format_from_rgb16_masks (int red_mask, int green_mask, int blue_mask)
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;
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;
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;
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;
388 return GST_VIDEO_FORMAT_UNKNOWN;
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
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
402 * Find the #GstVideoFormat for the given parameters.
404 * Returns: a #GstVideoFormat or GST_VIDEO_FORMAT_UNKNOWN when the parameters to
405 * not specify a known format.
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)
411 GstVideoFormat format;
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;
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,
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,
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,
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;
449 format = GST_VIDEO_FORMAT_UNKNOWN;
452 format = GST_VIDEO_FORMAT_UNKNOWN;
458 * gst_video_format_from_fourcc:
459 * @fourcc: a FOURCC value representing raw YUV video
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.
467 * Returns: the #GstVideoFormat describing the FOURCC value
470 gst_video_format_from_fourcc (guint32 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;
520 return GST_VIDEO_FORMAT_UNKNOWN;
525 * gst_video_format_from_string:
526 * @format: a format string
528 * Convert the @format string to its #GstVideoFormat.
530 * Returns: the #GstVideoFormat for @format or GST_VIDEO_FORMAT_UNKNOWN when the
531 * string is not a known format.
534 gst_video_format_from_string (const gchar * format)
538 g_return_val_if_fail (format != NULL, GST_VIDEO_FORMAT_UNKNOWN);
540 for (i = 0; i < G_N_ELEMENTS (formats); i++) {
541 if (strcmp (GST_VIDEO_FORMAT_INFO_NAME (&formats[i].info), format) == 0)
542 return GST_VIDEO_FORMAT_INFO_FORMAT (&formats[i].info);
544 return GST_VIDEO_FORMAT_UNKNOWN;
549 * gst_video_format_to_fourcc:
550 * @format: a #GstVideoFormat video format
552 * Converts a #GstVideoFormat value into the corresponding FOURCC. Only
553 * a few YUV formats have corresponding FOURCC values. If @format has
554 * no corresponding FOURCC value, 0 is returned.
558 * Returns: the FOURCC corresponding to @format
561 gst_video_format_to_fourcc (GstVideoFormat format)
563 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
565 if (format >= G_N_ELEMENTS (formats))
568 return formats[format].fourcc;
572 gst_video_format_to_string (GstVideoFormat format)
574 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
576 if (format >= G_N_ELEMENTS (formats))
579 return GST_VIDEO_FORMAT_INFO_NAME (&formats[format].info);
583 * gst_video_format_get_info:
584 * @format: a #GstVideoFormat
586 * Get the #GstVideoFormatInfo for @format
588 * Returns: The #GstVideoFormatInfo for @format.
590 const GstVideoFormatInfo *
591 gst_video_format_get_info (GstVideoFormat format)
593 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
594 g_return_val_if_fail (format < G_N_ELEMENTS (formats), NULL);
596 return &formats[format].info;
600 * gst_video_info_init:
601 * @info: a #GstVideoInfo
603 * Initialize @info with default values.
606 gst_video_info_init (GstVideoInfo * info)
608 g_return_if_fail (info != NULL);
610 memset (info, 0, sizeof (GstVideoInfo));
612 info->finfo = &formats[GST_VIDEO_FORMAT_UNKNOWN].info;
615 /* arrange for sensible defaults, e.g. if turned into caps */
623 * gst_video_info_set_format:
624 * @info: a #GstVideoInfo
625 * @format: the format
629 * Set the default info for a video frame of @format and @width and @height.
632 gst_video_info_set_format (GstVideoInfo * info, GstVideoFormat format,
633 guint width, guint height)
635 const GstVideoFormatInfo *finfo;
637 g_return_if_fail (info != NULL);
638 g_return_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN);
640 finfo = &formats[format].info;
645 info->height = height;
653 GstVideoChromaSite site;
656 static const ChromaSiteInfo chromasite[] = {
657 {"jpeg", GST_VIDEO_CHROMA_SITE_JPEG},
658 {"mpeg2", GST_VIDEO_CHROMA_SITE_MPEG2},
659 {"dv", GST_VIDEO_CHROMA_SITE_DV}
662 static GstVideoChromaSite
663 gst_video_chroma_from_string (const gchar * s)
666 for (i = 0; i < G_N_ELEMENTS (chromasite); i++) {
667 if (g_str_equal (chromasite[i].name, s))
668 return chromasite[i].site;
670 return GST_VIDEO_CHROMA_SITE_UNKNOWN;
674 gst_video_chroma_to_string (GstVideoChromaSite site)
677 for (i = 0; i < G_N_ELEMENTS (chromasite); i++) {
678 if (chromasite[i].site == site)
679 return chromasite[i].name;
687 GstVideoColorimetry color;
690 #define MAKE_COLORIMETRY(n,r,m,t,p) { GST_VIDEO_COLORIMETRY_ ##n, \
691 { GST_VIDEO_COLOR_RANGE ##r, GST_VIDEO_COLOR_MATRIX_ ##m, \
692 GST_VIDEO_TRANSFER_ ##t, GST_VIDEO_COLOR_PRIMARIES_ ##p } }
694 static const ColorimetryInfo colorimetry[] = {
695 MAKE_COLORIMETRY (BT601, _16_235, BT601, BT709, BT470M),
696 MAKE_COLORIMETRY (BT709, _16_235, BT709, BT709, BT709),
697 MAKE_COLORIMETRY (SMPTE240M, _16_235, SMPTE240M, SMPTE240M, SMPTE240M),
700 static const ColorimetryInfo *
701 gst_video_get_colorimetry (const gchar * s)
705 for (i = 0; i < G_N_ELEMENTS (colorimetry); i++) {
706 if (g_str_equal (colorimetry[i].name, s))
707 return &colorimetry[i];
712 #define IS_EQUAL(ci,i) (((ci)->color.range == (i)->range) && \
713 ((ci)->color.matrix == (i)->matrix) && \
714 ((ci)->color.transfer == (i)->transfer) && \
715 ((ci)->color.primaries == (i)->primaries))
719 * gst_video_colorimetry_from_string
720 * @cinfo: a #GstVideoColorimetry
721 * @color: a colorimetry string
723 * Parse the colorimetry string and update @cinfo with the parsed
726 * Returns: #TRUE if @color points to valid colorimetry info.
729 gst_video_colorimetry_from_string (GstVideoColorimetry * cinfo,
732 const ColorimetryInfo *ci;
734 if ((ci = gst_video_get_colorimetry (color))) {
737 /* FIXME, split and parse */
738 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
739 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
740 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
741 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
747 gst_video_caps_set_colorimetry (GstCaps * caps, GstVideoColorimetry * cinfo)
751 for (i = 0; i < G_N_ELEMENTS (colorimetry); i++) {
752 if (IS_EQUAL (&colorimetry[i], cinfo)) {
753 gst_caps_set_simple (caps, "colorimetry", G_TYPE_STRING,
754 colorimetry[i].name, NULL);
758 /* FIXME, construct colorimetry */
762 * gst_video_colorimetry_matches:
763 * @info: a #GstVideoInfo
764 * @color: a colorimetry string
766 * Check if the colorimetry information in @info matches that of the
769 * Returns: #TRUE if @color conveys the same colorimetry info as the color
770 * information in @info.
773 gst_video_colorimetry_matches (GstVideoColorimetry * cinfo, const gchar * color)
775 const ColorimetryInfo *ci;
777 if ((ci = gst_video_get_colorimetry (color)))
778 return IS_EQUAL (ci, cinfo);
784 * gst_video_info_from_caps:
785 * @info: a #GstVideoInfo
788 * Parse @caps and update @info.
790 * Returns: TRUE if @caps could be parsed
793 gst_video_info_from_caps (GstVideoInfo * info, const GstCaps * caps)
795 GstStructure *structure;
797 GstVideoFormat format;
798 gint width, height, views;
803 g_return_val_if_fail (info != NULL, FALSE);
804 g_return_val_if_fail (caps != NULL, FALSE);
805 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
807 GST_DEBUG ("parsing caps %" GST_PTR_FORMAT, caps);
809 structure = gst_caps_get_structure (caps, 0);
811 if (!gst_structure_has_name (structure, "video/x-raw"))
814 if (!(s = gst_structure_get_string (structure, "format")))
817 format = gst_video_format_from_string (s);
818 if (format == GST_VIDEO_FORMAT_UNKNOWN)
821 if (!gst_structure_get_int (structure, "width", &width))
823 if (!gst_structure_get_int (structure, "height", &height))
826 gst_video_info_set_format (info, format, width, height);
828 if (gst_structure_get_fraction (structure, "framerate", &fps_n, &fps_d)) {
830 /* variable framerate */
831 info->flags |= GST_VIDEO_FLAG_VARIABLE_FPS;
832 /* see if we have a max-framerate */
833 gst_structure_get_fraction (structure, "max-framerate", &fps_n, &fps_d);
838 /* unspecified is variable framerate */
843 if (!gst_structure_get_boolean (structure, "interlaced", &interlaced))
846 info->flags |= GST_VIDEO_FLAG_INTERLACED;
848 info->flags &= ~GST_VIDEO_FLAG_INTERLACED;
850 if (gst_structure_get_int (structure, "views", &views))
855 if ((s = gst_structure_get_string (structure, "chroma-site")))
856 info->chroma_site = gst_video_chroma_from_string (s);
858 info->chroma_site = GST_VIDEO_CHROMA_SITE_UNKNOWN;
860 if ((s = gst_structure_get_string (structure, "colorimetry")))
861 gst_video_colorimetry_from_string (&info->colorimetry, s);
863 memset (&info->colorimetry, 0, sizeof (GstVideoColorimetry));
865 if (gst_structure_get_fraction (structure, "pixel-aspect-ratio",
878 GST_ERROR ("wrong name '%s', expected video/x-raw",
879 gst_structure_get_name (structure));
884 GST_ERROR ("no format given");
889 GST_ERROR ("unknown format '%s' given", s);
894 GST_ERROR ("no width property given");
899 GST_ERROR ("no height property given");
905 * gst_video_info_to_caps:
906 * @info: a #GstVideoInfo
908 * Convert the values of @info into a #GstCaps.
910 * Returns: a new #GstCaps containing the info of @info.
913 gst_video_info_to_caps (GstVideoInfo * info)
918 g_return_val_if_fail (info != NULL, NULL);
919 g_return_val_if_fail (info->finfo != NULL, NULL);
920 g_return_val_if_fail (info->finfo->format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
922 format = gst_video_format_to_string (info->finfo->format);
923 g_return_val_if_fail (format != NULL, NULL);
925 caps = gst_caps_new_simple ("video/x-raw",
926 "format", G_TYPE_STRING, format,
927 "width", G_TYPE_INT, info->width,
928 "height", G_TYPE_INT, info->height,
929 "pixel-aspect-ratio", GST_TYPE_FRACTION, info->par_n, info->par_d, NULL);
931 if (info->flags & GST_VIDEO_FLAG_INTERLACED)
932 gst_caps_set_simple (caps, "interlaced", G_TYPE_BOOLEAN, TRUE, NULL);
934 if (info->chroma_site != GST_VIDEO_CHROMA_SITE_UNKNOWN)
935 gst_caps_set_simple (caps, "chroma-site", G_TYPE_STRING,
936 gst_video_chroma_to_string (info->chroma_site), NULL);
938 gst_video_caps_set_colorimetry (caps, &info->colorimetry);
941 gst_caps_set_simple (caps, "views", G_TYPE_INT, info->views, NULL);
943 if (info->flags & GST_VIDEO_FLAG_VARIABLE_FPS && info->fps_n != 0) {
944 /* variable fps with a max-framerate */
945 gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, 0, 1,
946 "max-framerate", GST_TYPE_FRACTION, info->fps_n, info->fps_d, NULL);
948 /* no variable fps or no max-framerate */
949 gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION,
950 info->fps_n, info->fps_d, NULL);
957 * gst_video_frame_map_id:
958 * @frame: pointer to #GstVideoFrame
959 * @info: a #GstVideoInfo
960 * @buffer: the buffer to map
961 * @id: the frame id to map
962 * @flags: #GstMapFlags
964 * Use @info and @buffer to fill in the values of @frame with the video frame
965 * information of frame @id.
967 * When @id is -1, the default frame is mapped. When @id != -1, this function
968 * will return %FALSE when there is no GstVideoMeta with that id.
970 * All video planes of @buffer will be mapped and the pointers will be set in
973 * Returns: %TRUE on success.
976 gst_video_frame_map_id (GstVideoFrame * frame, GstVideoInfo * info,
977 GstBuffer * buffer, gint id, GstMapFlags flags)
984 g_return_val_if_fail (frame != NULL, FALSE);
985 g_return_val_if_fail (info != NULL, FALSE);
986 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
989 meta = gst_buffer_get_video_meta (buffer);
991 meta = gst_buffer_get_video_meta_id (buffer, id);
993 frame->buffer = buffer;
997 frame->info.flags = meta->flags;
998 frame->info.finfo = &formats[meta->format].info;
999 frame->info.width = meta->width;
1000 frame->info.height = meta->height;
1001 frame->id = meta->id;
1003 for (i = 0; i < info->finfo->n_planes; i++) {
1005 gst_video_meta_map (meta, i, &frame->info.stride[i], flags);
1008 /* no metadata, we really need to have the metadata when the id is
1014 frame->info = *info;
1017 data = gst_buffer_map (buffer, &size, NULL, flags);
1019 /* do some sanity checks */
1020 if (size < info->size)
1023 /* set up pointers */
1024 for (i = 0; i < info->finfo->n_planes; i++) {
1025 frame->data[i] = data + info->offset[i];
1033 GST_ERROR ("no GstVideoMeta for id %d", id);
1038 GST_ERROR ("invalid buffer size %" G_GSIZE_FORMAT " < %" G_GSIZE_FORMAT,
1040 gst_buffer_unmap (buffer, data, size);
1046 * gst_video_frame_map:
1047 * @frame: pointer to #GstVideoFrame
1048 * @info: a #GstVideoInfo
1049 * @buffer: the buffer to map
1050 * @flags: #GstMapFlags
1052 * Use @info and @buffer to fill in the values of @frame.
1054 * All video planes of @buffer will be mapped and the pointers will be set in
1057 * Returns: %TRUE on success.
1060 gst_video_frame_map (GstVideoFrame * frame, GstVideoInfo * info,
1061 GstBuffer * buffer, GstMapFlags flags)
1063 return gst_video_frame_map_id (frame, info, buffer, -1, flags);
1067 * gst_video_frame_unmap:
1068 * @frame: a #GstVideoFrame
1070 * Unmap the memory previously mapped with gst_video_frame_map.
1073 gst_video_frame_unmap (GstVideoFrame * frame)
1079 g_return_if_fail (frame != NULL);
1081 buffer = frame->buffer;
1085 for (i = 0; i < frame->info.finfo->n_planes; i++) {
1086 gst_video_meta_unmap (meta, i, frame->data[i]);
1091 data = frame->data[0];
1092 data -= frame->info.offset[0];
1093 gst_buffer_unmap (buffer, data, -1);
1098 * gst_video_frame_copy:
1099 * @dest: a #GstVideoFrame
1100 * @src: a #GstVideoFrame
1102 * Copy the contents from @src to @dest.
1104 * Returns: TRUE if the contents could be copied.
1107 gst_video_frame_copy (GstVideoFrame * dest, const GstVideoFrame * src)
1110 const GstVideoInfo *sinfo;
1111 GstVideoInfo *dinfo;
1114 dinfo = &dest->info;
1116 g_return_val_if_fail (dinfo->finfo->format == sinfo->finfo->format, FALSE);
1117 g_return_val_if_fail (dinfo->width == sinfo->width
1118 && dinfo->height == sinfo->height, FALSE);
1120 n_planes = dinfo->finfo->n_planes;
1122 for (i = 0; i < n_planes; i++) {
1130 ss = sinfo->stride[i];
1131 ds = dinfo->stride[i];
1133 w = MIN (ABS (ss), ABS (ds));
1134 h = GST_VIDEO_FRAME_COMP_HEIGHT (dest, i);
1136 GST_DEBUG ("w %d h %d", w, h);
1138 for (j = 0; j < h; j++) {
1148 fill_planes (GstVideoInfo * info)
1152 width = info->width;
1153 height = info->height;
1155 switch (info->finfo->format) {
1156 case GST_VIDEO_FORMAT_YUY2:
1157 case GST_VIDEO_FORMAT_YVYU:
1158 case GST_VIDEO_FORMAT_UYVY:
1159 info->stride[0] = GST_ROUND_UP_4 (width * 2);
1160 info->offset[0] = 0;
1161 info->size = info->stride[0] * height;
1163 case GST_VIDEO_FORMAT_AYUV:
1164 case GST_VIDEO_FORMAT_RGBx:
1165 case GST_VIDEO_FORMAT_RGBA:
1166 case GST_VIDEO_FORMAT_BGRx:
1167 case GST_VIDEO_FORMAT_BGRA:
1168 case GST_VIDEO_FORMAT_xRGB:
1169 case GST_VIDEO_FORMAT_ARGB:
1170 case GST_VIDEO_FORMAT_xBGR:
1171 case GST_VIDEO_FORMAT_ABGR:
1172 case GST_VIDEO_FORMAT_r210:
1173 info->stride[0] = width * 4;
1174 info->offset[0] = 0;
1175 info->size = info->stride[0] * height;
1177 case GST_VIDEO_FORMAT_RGB16:
1178 case GST_VIDEO_FORMAT_BGR16:
1179 case GST_VIDEO_FORMAT_RGB15:
1180 case GST_VIDEO_FORMAT_BGR15:
1181 info->stride[0] = GST_ROUND_UP_4 (width * 2);
1182 info->offset[0] = 0;
1183 info->size = info->stride[0] * height;
1185 case GST_VIDEO_FORMAT_RGB:
1186 case GST_VIDEO_FORMAT_BGR:
1187 case GST_VIDEO_FORMAT_v308:
1188 info->stride[0] = GST_ROUND_UP_4 (width * 3);
1189 info->offset[0] = 0;
1190 info->size = info->stride[0] * height;
1192 case GST_VIDEO_FORMAT_v210:
1193 info->stride[0] = ((width + 47) / 48) * 128;
1194 info->offset[0] = 0;
1195 info->size = info->stride[0] * height;
1197 case GST_VIDEO_FORMAT_v216:
1198 info->stride[0] = GST_ROUND_UP_8 (width * 4);
1199 info->offset[0] = 0;
1200 info->size = info->stride[0] * height;
1202 case GST_VIDEO_FORMAT_GRAY8:
1203 case GST_VIDEO_FORMAT_Y800:
1204 info->stride[0] = GST_ROUND_UP_4 (width);
1205 info->offset[0] = 0;
1206 info->size = info->stride[0] * height;
1208 case GST_VIDEO_FORMAT_GRAY16_BE:
1209 case GST_VIDEO_FORMAT_GRAY16_LE:
1210 case GST_VIDEO_FORMAT_Y16:
1211 info->stride[0] = GST_ROUND_UP_4 (width * 2);
1212 info->offset[0] = 0;
1213 info->size = info->stride[0] * height;
1215 case GST_VIDEO_FORMAT_UYVP:
1216 info->stride[0] = GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4);
1217 info->offset[0] = 0;
1218 info->size = info->stride[0] * height;
1220 case GST_VIDEO_FORMAT_RGB8_PALETTED:
1221 info->stride[0] = GST_ROUND_UP_4 (width);
1222 info->offset[0] = 0;
1223 info->size = info->stride[0] * height;
1225 case GST_VIDEO_FORMAT_IYU1:
1226 info->stride[0] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) +
1227 GST_ROUND_UP_4 (width) / 2);
1228 info->offset[0] = 0;
1229 info->size = info->stride[0] * height;
1231 case GST_VIDEO_FORMAT_ARGB64:
1232 case GST_VIDEO_FORMAT_AYUV64:
1233 info->stride[0] = width * 8;
1234 info->offset[0] = 0;
1235 info->size = info->stride[0] * height;
1237 case GST_VIDEO_FORMAT_I420:
1238 info->stride[0] = GST_ROUND_UP_4 (width);
1239 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1240 info->stride[2] = info->stride[1];
1241 info->offset[0] = 0;
1242 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1243 info->offset[2] = info->offset[1] +
1244 info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
1245 info->size = info->offset[2] +
1246 info->stride[2] * (GST_ROUND_UP_2 (height) / 2);
1248 case GST_VIDEO_FORMAT_YV12: /* same as I420, but plane 1+2 swapped */
1249 info->stride[0] = GST_ROUND_UP_4 (width);
1250 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1251 info->stride[2] = info->stride[1];
1252 info->offset[0] = 0;
1253 info->offset[2] = info->stride[0] * GST_ROUND_UP_2 (height);
1254 info->offset[1] = info->offset[2] +
1255 info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
1256 info->size = info->offset[1] +
1257 info->stride[2] * (GST_ROUND_UP_2 (height) / 2);
1259 case GST_VIDEO_FORMAT_Y41B:
1260 info->stride[0] = GST_ROUND_UP_4 (width);
1261 info->stride[1] = GST_ROUND_UP_16 (width) / 4;
1262 info->stride[2] = info->stride[1];
1263 info->offset[0] = 0;
1264 info->offset[1] = info->stride[0] * height;
1265 info->offset[2] = info->offset[1] + info->stride[1] * height;
1266 /* simplification of ROUNDUP4(w)*h + 2*((ROUNDUP16(w)/4)*h */
1267 info->size = (info->stride[0] + (GST_ROUND_UP_16 (width) / 2)) * height;
1269 case GST_VIDEO_FORMAT_Y42B:
1270 info->stride[0] = GST_ROUND_UP_4 (width);
1271 info->stride[1] = GST_ROUND_UP_8 (width) / 2;
1272 info->stride[2] = info->stride[1];
1273 info->offset[0] = 0;
1274 info->offset[1] = info->stride[0] * height;
1275 info->offset[2] = info->offset[1] + info->stride[1] * height;
1276 /* simplification of ROUNDUP4(w)*h + 2*(ROUNDUP8(w)/2)*h */
1277 info->size = (info->stride[0] + GST_ROUND_UP_8 (width)) * height;
1279 case GST_VIDEO_FORMAT_Y444:
1280 info->stride[0] = GST_ROUND_UP_4 (width);
1281 info->stride[1] = info->stride[0];
1282 info->stride[2] = info->stride[0];
1283 info->offset[0] = 0;
1284 info->offset[1] = info->stride[0] * height;
1285 info->offset[2] = info->offset[1] * 2;
1286 info->size = info->stride[0] * height * 3;
1288 case GST_VIDEO_FORMAT_NV12:
1289 case GST_VIDEO_FORMAT_NV21:
1290 info->stride[0] = GST_ROUND_UP_4 (width);
1291 info->stride[1] = info->stride[0];
1292 info->offset[0] = 0;
1293 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1294 info->size = info->stride[0] * GST_ROUND_UP_2 (height) * 3 / 2;
1296 case GST_VIDEO_FORMAT_A420:
1297 info->stride[0] = GST_ROUND_UP_4 (width);
1298 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1299 info->stride[2] = info->stride[1];
1300 info->stride[3] = info->stride[0];
1301 info->offset[0] = 0;
1302 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1303 info->offset[2] = info->offset[1] +
1304 info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
1305 info->offset[3] = info->offset[2] +
1306 info->stride[2] * (GST_ROUND_UP_2 (height) / 2);
1307 info->size = info->offset[3] + info->stride[0];
1309 case GST_VIDEO_FORMAT_YUV9:
1310 info->stride[0] = GST_ROUND_UP_4 (width);
1311 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
1312 info->stride[2] = info->stride[1];
1313 info->offset[0] = 0;
1314 info->offset[1] = info->stride[0] * height;
1315 info->offset[2] = info->offset[1] +
1316 info->stride[1] * (GST_ROUND_UP_4 (height) / 4);
1317 info->size = info->offset[2] +
1318 info->stride[2] * (GST_ROUND_UP_4 (height) / 4);
1320 case GST_VIDEO_FORMAT_YVU9:
1321 info->stride[0] = GST_ROUND_UP_4 (width);
1322 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
1323 info->stride[2] = info->stride[1];
1324 info->offset[0] = 0;
1325 info->offset[2] = info->stride[0] * height;
1326 info->offset[1] = info->offset[2] +
1327 info->stride[1] * (GST_ROUND_UP_4 (height) / 4);
1328 info->size = info->offset[2] +
1329 info->stride[2] * (GST_ROUND_UP_4 (height) / 4);
1331 case GST_VIDEO_FORMAT_UNKNOWN:
1332 GST_ERROR ("invalid format");
1333 g_warning ("invalid format");
1340 * gst_video_format_convert:
1341 * @info: a #GstVideoInfo
1342 * @src_format: #GstFormat of the @src_value
1343 * @src_value: value to convert
1344 * @dest_format: #GstFormat of the @dest_value
1345 * @dest_value: pointer to destination value
1347 * Converts among various #GstFormat types. This function handles
1348 * GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT. For
1349 * raw video, GST_FORMAT_DEFAULT corresponds to video frames. This
1350 * function can be used to handle pad queries of the type GST_QUERY_CONVERT.
1354 * Returns: TRUE if the conversion was successful.
1357 gst_video_info_convert (GstVideoInfo * info,
1358 GstFormat src_format, gint64 src_value,
1359 GstFormat dest_format, gint64 * dest_value)
1361 gboolean ret = FALSE;
1362 int size, fps_n, fps_d;
1364 g_return_val_if_fail (info != NULL, 0);
1365 g_return_val_if_fail (info->finfo != NULL, 0);
1366 g_return_val_if_fail (info->finfo->format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1367 g_return_val_if_fail (info->size > 0, 0);
1370 fps_n = info->fps_n;
1371 fps_d = info->fps_d;
1373 GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s to %s",
1374 src_value, gst_format_get_name (src_format),
1375 gst_format_get_name (dest_format));
1377 if (src_format == dest_format) {
1378 *dest_value = src_value;
1383 if (src_value == -1) {
1389 /* bytes to frames */
1390 if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_DEFAULT) {
1392 *dest_value = gst_util_uint64_scale_int (src_value, 1, size);
1394 GST_ERROR ("blocksize is 0");
1401 /* frames to bytes */
1402 if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_BYTES) {
1403 *dest_value = gst_util_uint64_scale_int (src_value, size, 1);
1408 /* time to frames */
1409 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_DEFAULT) {
1411 *dest_value = gst_util_uint64_scale (src_value,
1412 fps_n, GST_SECOND * fps_d);
1414 GST_ERROR ("framerate denominator is 0");
1421 /* frames to time */
1422 if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) {
1424 *dest_value = gst_util_uint64_scale (src_value,
1425 GST_SECOND * fps_d, fps_n);
1427 GST_ERROR ("framerate numerator is 0");
1435 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
1437 *dest_value = gst_util_uint64_scale (src_value,
1438 fps_n * size, GST_SECOND * fps_d);
1440 GST_ERROR ("framerate denominator is 0");
1448 if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
1449 if (fps_n != 0 && size != 0) {
1450 *dest_value = gst_util_uint64_scale (src_value,
1451 GST_SECOND * fps_d, fps_n * size);
1453 GST_ERROR ("framerate denominator and/or blocksize is 0");
1461 GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, ret, *dest_value);
1466 #define GST_VIDEO_EVENT_STILL_STATE_NAME "GstEventStillFrame"
1469 * gst_video_event_new_still_frame:
1470 * @in_still: boolean value for the still-frame state of the event.
1472 * Creates a new Still Frame event. If @in_still is %TRUE, then the event
1473 * represents the start of a still frame sequence. If it is %FALSE, then
1474 * the event ends a still frame sequence.
1476 * To parse an event created by gst_video_event_new_still_frame() use
1477 * gst_video_event_parse_still_frame().
1479 * Returns: The new GstEvent
1483 gst_video_event_new_still_frame (gboolean in_still)
1485 GstEvent *still_event;
1488 s = gst_structure_new (GST_VIDEO_EVENT_STILL_STATE_NAME,
1489 "still-state", G_TYPE_BOOLEAN, in_still, NULL);
1490 still_event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1496 * gst_video_event_parse_still_frame:
1497 * @event: A #GstEvent to parse
1498 * @in_still: A boolean to receive the still-frame status from the event, or NULL
1500 * Parse a #GstEvent, identify if it is a Still Frame event, and
1501 * return the still-frame state from the event if it is.
1502 * If the event represents the start of a still frame, the in_still
1503 * variable will be set to TRUE, otherwise FALSE. It is OK to pass NULL for the
1504 * in_still variable order to just check whether the event is a valid still-frame
1507 * Create a still frame event using gst_video_event_new_still_frame()
1509 * Returns: %TRUE if the event is a valid still-frame event. %FALSE if not
1513 gst_video_event_parse_still_frame (GstEvent * event, gboolean * in_still)
1515 const GstStructure *s;
1516 gboolean ev_still_state;
1518 g_return_val_if_fail (event != NULL, FALSE);
1520 if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM)
1521 return FALSE; /* Not a still frame event */
1523 s = gst_event_get_structure (event);
1525 || !gst_structure_has_name (s, GST_VIDEO_EVENT_STILL_STATE_NAME))
1526 return FALSE; /* Not a still frame event */
1527 if (!gst_structure_get_boolean (s, "still-state", &ev_still_state))
1528 return FALSE; /* Not a still frame event */
1530 *in_still = ev_still_state;
1535 * gst_video_parse_caps_palette:
1536 * @caps: #GstCaps to parse
1538 * Returns the palette data from the caps as a #GstBuffer. For
1539 * #GST_VIDEO_FORMAT_RGB8_PALETTED this is containing 256 #guint32
1540 * values, each containing ARGB colors in native endianness.
1542 * Returns: a #GstBuffer containing the palette data. Unref after usage.
1546 gst_video_parse_caps_palette (GstCaps * caps)
1552 if (!gst_caps_is_fixed (caps))
1555 s = gst_caps_get_structure (caps, 0);
1557 p_v = gst_structure_get_value (s, "palette_data");
1558 if (!p_v || !GST_VALUE_HOLDS_BUFFER (p_v))
1561 p = g_value_dup_boxed (p_v);
1566 #define GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME "GstForceKeyUnit"
1569 * gst_video_event_new_downstream_force_key_unit:
1570 * @timestamp: the timestamp of the buffer that starts a new key unit
1571 * @stream_time: the stream_time of the buffer that starts a new key unit
1572 * @running_time: the running_time of the buffer that starts a new key unit
1573 * @all_headers: %TRUE to produce headers when starting a new key unit
1574 * @count: integer that can be used to number key units
1576 * Creates a new downstream force key unit event. A downstream force key unit
1577 * event can be sent down the pipeline to request downstream elements to produce
1578 * a key unit. A downstream force key unit event must also be sent when handling
1579 * an upstream force key unit event to notify downstream that the latter has been
1582 * To parse an event created by gst_video_event_new_downstream_force_key_unit() use
1583 * gst_video_event_parse_downstream_force_key_unit().
1585 * Returns: The new GstEvent
1589 gst_video_event_new_downstream_force_key_unit (GstClockTime timestamp,
1590 GstClockTime stream_time, GstClockTime running_time, gboolean all_headers,
1593 GstEvent *force_key_unit_event;
1596 s = gst_structure_new (GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME,
1597 "timestamp", G_TYPE_UINT64, timestamp,
1598 "stream-time", G_TYPE_UINT64, stream_time,
1599 "running-time", G_TYPE_UINT64, running_time,
1600 "all-headers", G_TYPE_BOOLEAN, all_headers,
1601 "count", G_TYPE_UINT, count, NULL);
1602 force_key_unit_event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1604 return force_key_unit_event;
1608 * gst_video_event_new_upstream_force_key_unit:
1609 * @running_time: the running_time at which a new key unit should be produced
1610 * @all_headers: %TRUE to produce headers when starting a new key unit
1611 * @count: integer that can be used to number key units
1613 * Creates a new upstream force key unit event. An upstream force key unit event
1614 * can be sent to request upstream elements to produce a key unit.
1616 * @running_time can be set to request a new key unit at a specific
1617 * running_time. If set to GST_CLOCK_TIME_NONE, upstream elements will produce a
1618 * new key unit as soon as possible.
1620 * To parse an event created by gst_video_event_new_downstream_force_key_unit() use
1621 * gst_video_event_parse_downstream_force_key_unit().
1623 * Returns: The new GstEvent
1627 gst_video_event_new_upstream_force_key_unit (GstClockTime running_time,
1628 gboolean all_headers, guint count)
1630 GstEvent *force_key_unit_event;
1633 s = gst_structure_new (GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME,
1634 "running-time", GST_TYPE_CLOCK_TIME, running_time,
1635 "all-headers", G_TYPE_BOOLEAN, all_headers,
1636 "count", G_TYPE_UINT, count, NULL);
1637 force_key_unit_event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, s);
1639 return force_key_unit_event;
1643 * gst_video_event_is_force_key_unit:
1644 * @event: A #GstEvent to check
1646 * Checks if an event is a force key unit event. Returns true for both upstream
1647 * and downstream force key unit events.
1649 * Returns: %TRUE if the event is a valid force key unit event
1653 gst_video_event_is_force_key_unit (GstEvent * event)
1655 const GstStructure *s;
1657 g_return_val_if_fail (event != NULL, FALSE);
1659 if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM &&
1660 GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_UPSTREAM)
1661 return FALSE; /* Not a force key unit event */
1663 s = gst_event_get_structure (event);
1665 || !gst_structure_has_name (s, GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME))
1672 * gst_video_event_parse_downstream_force_key_unit:
1673 * @event: A #GstEvent to parse
1674 * @timestamp: (out): A pointer to the timestamp in the event
1675 * @stream_time: (out): A pointer to the stream-time in the event
1676 * @running_time: (out): A pointer to the running-time in the event
1677 * @all_headers: (out): A pointer to the all_headers flag in the event
1678 * @count: (out): A pointer to the count field of the event
1680 * Get timestamp, stream-time, running-time, all-headers and count in the force
1681 * key unit event. See gst_video_event_new_downstream_force_key_unit() for a
1682 * full description of the downstream force key unit event.
1684 * Returns: %TRUE if the event is a valid downstream force key unit event.
1688 gst_video_event_parse_downstream_force_key_unit (GstEvent * event,
1689 GstClockTime * timestamp, GstClockTime * stream_time,
1690 GstClockTime * running_time, gboolean * all_headers, guint * count)
1692 const GstStructure *s;
1693 GstClockTime ev_timestamp, ev_stream_time, ev_running_time;
1694 gboolean ev_all_headers;
1697 g_return_val_if_fail (event != NULL, FALSE);
1699 if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM)
1700 return FALSE; /* Not a force key unit event */
1702 s = gst_event_get_structure (event);
1704 || !gst_structure_has_name (s, GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME))
1707 if (!gst_structure_get_clock_time (s, "timestamp", &ev_timestamp))
1708 return FALSE; /* Not a force key unit event */
1709 if (!gst_structure_get_clock_time (s, "stream-time", &ev_stream_time))
1710 return FALSE; /* Not a force key unit event */
1711 if (!gst_structure_get_clock_time (s, "running-time", &ev_running_time))
1712 return FALSE; /* Not a force key unit event */
1713 if (!gst_structure_get_boolean (s, "all-headers", &ev_all_headers))
1714 return FALSE; /* Not a force key unit event */
1715 if (!gst_structure_get_uint (s, "count", &ev_count))
1716 return FALSE; /* Not a force key unit event */
1719 *timestamp = ev_timestamp;
1722 *stream_time = ev_stream_time;
1725 *running_time = ev_running_time;
1728 *all_headers = ev_all_headers;
1737 * gst_video_event_parse_upstream_force_key_unit:
1738 * @event: A #GstEvent to parse
1739 * @running_time: (out): A pointer to the running_time in the event
1740 * @all_headers: (out): A pointer to the all_headers flag in the event
1741 * @count: (out): A pointer to the count field in the event
1743 * Get running-time, all-headers and count in the force key unit event. See
1744 * gst_video_event_new_upstream_force_key_unit() for a full description of the
1745 * upstream force key unit event.
1747 * Create an upstream force key unit event using gst_video_event_new_upstream_force_key_unit()
1749 * Returns: %TRUE if the event is a valid upstream force-key-unit event. %FALSE if not
1753 gst_video_event_parse_upstream_force_key_unit (GstEvent * event,
1754 GstClockTime * running_time, gboolean * all_headers, guint * count)
1756 const GstStructure *s;
1757 GstClockTime ev_running_time;
1758 gboolean ev_all_headers;
1761 g_return_val_if_fail (event != NULL, FALSE);
1763 if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_UPSTREAM)
1764 return FALSE; /* Not a force key unit event */
1766 s = gst_event_get_structure (event);
1768 || !gst_structure_has_name (s, GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME))
1771 if (!gst_structure_get_clock_time (s, "running-time", &ev_running_time))
1772 return FALSE; /* Not a force key unit event */
1773 if (!gst_structure_get_boolean (s, "all-headers", &ev_all_headers))
1774 return FALSE; /* Not a force key unit event */
1775 if (!gst_structure_get_uint (s, "count", &ev_count))
1776 return FALSE; /* Not a force key unit event */
1779 *running_time = ev_running_time;
1782 *all_headers = ev_all_headers;