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 GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
33 static int fill_planes (GstVideoInfo * info);
38 GstVideoFormatInfo info;
41 /* depths: bits, n_components, shift, depth */
42 #define DPTH0 0, 0, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
43 #define DPTH8 8, 1, { 0, 0, 0, 0 }, { 8, 0, 0, 0 }
44 #define DPTH888 8, 3, { 0, 0, 0, 0 }, { 8, 8, 8, 0 }
45 #define DPTH8888 8, 4, { 0, 0, 0, 0 }, { 8, 8, 8, 8 }
46 #define DPTH10_10_10 10, 3, { 0, 0, 0, 0 }, { 10, 10, 10, 0 }
47 #define DPTH16 16, 1, { 0, 0, 0, 0 }, { 16, 0, 0, 0 }
48 #define DPTH16_16_16 16, 3, { 0, 0, 0, 0 }, { 16, 16, 16, 0 }
49 #define DPTH16_16_16_16 16, 4, { 0, 0, 0, 0 }, { 16, 16, 16, 16 }
50 #define DPTH555 16, 3, { 10, 5, 0, 0 }, { 5, 5, 5, 0 }
51 #define DPTH565 16, 3, { 11, 5, 0, 0 }, { 5, 6, 5, 0 }
54 #define PSTR0 { 0, 0, 0, 0 }
55 #define PSTR1 { 1, 0, 0, 0 }
56 #define PSTR111 { 1, 1, 1, 0 }
57 #define PSTR1111 { 1, 1, 1, 1 }
58 #define PSTR122 { 1, 2, 2, 0 }
59 #define PSTR2 { 2, 0, 0, 0 }
60 #define PSTR222 { 2, 2, 2, 0 }
61 #define PSTR244 { 2, 4, 4, 0 }
62 #define PSTR444 { 4, 4, 4, 0 }
63 #define PSTR4444 { 4, 4, 4, 4 }
64 #define PSTR333 { 3, 3, 3, 0 }
65 #define PSTR488 { 4, 8, 8, 0 }
66 #define PSTR8888 { 8, 8, 8, 8 }
69 #define PLANE_NA 0, { 0, 0, 0, 0 }
70 #define PLANE0 1, { 0, 0, 0, 0 }
71 #define PLANE011 2, { 0, 1, 1, 0 }
72 #define PLANE012 3, { 0, 1, 2, 0 }
73 #define PLANE0123 4, { 0, 1, 2, 3 }
74 #define PLANE021 3, { 0, 2, 1, 0 }
77 #define OFFS0 { 0, 0, 0, 0 }
78 #define OFFS013 { 0, 1, 3, 0 }
79 #define OFFS102 { 1, 0, 2, 0 }
80 #define OFFS1230 { 1, 2, 3, 0 }
81 #define OFFS012 { 0, 1, 2, 0 }
82 #define OFFS210 { 2, 1, 0, 0 }
83 #define OFFS123 { 1, 2, 3, 0 }
84 #define OFFS321 { 3, 2, 1, 0 }
85 #define OFFS0123 { 0, 1, 2, 3 }
86 #define OFFS2103 { 2, 1, 0, 3 }
87 #define OFFS3210 { 3, 2, 1, 0 }
88 #define OFFS031 { 0, 3, 1, 0 }
89 #define OFFS026 { 0, 2, 6, 0 }
90 #define OFFS001 { 0, 0, 1, 0 }
91 #define OFFS010 { 0, 1, 0, 0 }
92 #define OFFS104 { 1, 0, 4, 0 }
93 #define OFFS2460 { 2, 4, 6, 0 }
96 #define SUB410 { 0, 2, 2, 0 }, { 0, 2, 2, 0 }
97 #define SUB411 { 0, 2, 2, 0 }, { 0, 0, 0, 0 }
98 #define SUB420 { 0, 1, 1, 0 }, { 0, 1, 1, 0 }
99 #define SUB422 { 0, 1, 1, 0 }, { 0, 0, 0, 0 }
100 #define SUB4 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
101 #define SUB444 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
102 #define SUB4444 { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
103 #define SUB4204 { 0, 1, 1, 0 }, { 0, 1, 1, 0 }
105 #define MAKE_YUV_FORMAT(name, desc, fourcc, depth, pstride, plane, offs, sub ) \
106 { fourcc, {GST_VIDEO_FORMAT_ ##name, G_STRINGIFY(name), desc, GST_VIDEO_FORMAT_FLAG_YUV, depth, pstride, plane, offs, sub } }
107 #define MAKE_YUVA_FORMAT(name, desc, fourcc, depth, pstride, plane, offs, sub) \
108 { fourcc, {GST_VIDEO_FORMAT_ ##name, G_STRINGIFY(name), desc, GST_VIDEO_FORMAT_FLAG_YUV | GST_VIDEO_FORMAT_FLAG_ALPHA, depth, pstride, plane, offs, sub } }
110 #define MAKE_RGB_FORMAT(name, desc, depth, pstride, plane, offs, sub) \
111 { 0x00000000, {GST_VIDEO_FORMAT_ ##name, G_STRINGIFY(name), desc, GST_VIDEO_FORMAT_FLAG_RGB, depth, pstride, plane, offs, sub } }
112 #define MAKE_RGB_LE_FORMAT(name, desc, depth, pstride, plane, offs, sub) \
113 { 0x00000000, {GST_VIDEO_FORMAT_ ##name, G_STRINGIFY(name), desc, GST_VIDEO_FORMAT_FLAG_RGB | GST_VIDEO_FORMAT_FLAG_LE, depth, pstride, plane, offs, sub } }
114 #define MAKE_RGBA_FORMAT(name, desc, depth, pstride, plane, offs, sub) \
115 { 0x00000000, {GST_VIDEO_FORMAT_ ##name, G_STRINGIFY(name), desc, GST_VIDEO_FORMAT_FLAG_RGB | GST_VIDEO_FORMAT_FLAG_ALPHA, depth, pstride, plane, offs, sub } }
117 #define MAKE_GRAY_FORMAT(name, desc, depth, pstride, plane, offs, sub) \
118 { 0x00000000, {GST_VIDEO_FORMAT_ ##name, G_STRINGIFY(name), desc, GST_VIDEO_FORMAT_FLAG_GRAY, depth, pstride, plane, offs, sub } }
119 #define MAKE_GRAY_LE_FORMAT(name, desc, depth, pstride, plane, offs, sub) \
120 { 0x00000000, {GST_VIDEO_FORMAT_ ##name, G_STRINGIFY(name), desc, GST_VIDEO_FORMAT_FLAG_GRAY | GST_VIDEO_FORMAT_FLAG_LE, depth, pstride, plane, offs, sub } }
122 static VideoFormat formats[] = {
123 {0x00000000, {GST_VIDEO_FORMAT_UNKNOWN, "UNKNOWN", "unknown video", 0, DPTH0,
127 MAKE_YUV_FORMAT (I420, "raw video", GST_MAKE_FOURCC ('I', '4', '2', '0'),
129 PLANE012, OFFS0, SUB420),
130 MAKE_YUV_FORMAT (YV12, "raw video", GST_MAKE_FOURCC ('Y', 'V', '1', '2'),
132 PLANE021, OFFS0, SUB420),
133 MAKE_YUV_FORMAT (YUY2, "raw video", GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'),
135 PLANE0, OFFS013, SUB422),
136 MAKE_YUV_FORMAT (UYVY, "raw video", GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'),
138 PLANE0, OFFS102, SUB422),
139 MAKE_YUVA_FORMAT (AYUV, "raw video", GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'),
141 PSTR4444, PLANE0, OFFS1230, SUB4444),
142 MAKE_RGB_FORMAT (RGBx, "raw video", DPTH888, PSTR444, PLANE0, OFFS012,
144 MAKE_RGB_FORMAT (BGRx, "raw video", DPTH888, PSTR444, PLANE0, OFFS210,
146 MAKE_RGB_FORMAT (xRGB, "raw video", DPTH888, PSTR444, PLANE0, OFFS123,
148 MAKE_RGB_FORMAT (xBGR, "raw video", DPTH888, PSTR444, PLANE0, OFFS321,
150 MAKE_RGBA_FORMAT (RGBA, "raw video", DPTH8888, PSTR4444, PLANE0, OFFS0123,
152 MAKE_RGBA_FORMAT (BGRA, "raw video", DPTH8888, PSTR4444, PLANE0, OFFS2103,
154 MAKE_RGBA_FORMAT (ARGB, "raw video", DPTH8888, PSTR4444, PLANE0, OFFS1230,
156 MAKE_RGBA_FORMAT (ABGR, "raw video", DPTH8888, PSTR4444, PLANE0, OFFS3210,
158 MAKE_RGB_FORMAT (RGB, "raw video", DPTH888, PSTR333, PLANE0, OFFS012, SUB444),
159 MAKE_RGB_FORMAT (BGR, "raw video", DPTH888, PSTR333, PLANE0, OFFS210, SUB444),
161 MAKE_YUV_FORMAT (Y41B, "raw video", GST_MAKE_FOURCC ('Y', '4', '1', 'B'),
163 PLANE012, OFFS0, SUB411),
164 MAKE_YUV_FORMAT (Y42B, "raw video", GST_MAKE_FOURCC ('Y', '4', '2', 'B'),
166 PLANE012, OFFS0, SUB422),
167 MAKE_YUV_FORMAT (YVYU, "raw video", GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'),
169 PLANE0, OFFS031, SUB422),
170 MAKE_YUV_FORMAT (Y444, "raw video", GST_MAKE_FOURCC ('Y', '4', '4', '4'),
172 PLANE012, OFFS0, SUB444),
173 MAKE_YUV_FORMAT (v210, "raw video", GST_MAKE_FOURCC ('v', '2', '1', '0'),
175 PSTR0, PLANE0, OFFS0, SUB422),
176 MAKE_YUV_FORMAT (v216, "raw video", GST_MAKE_FOURCC ('v', '2', '1', '6'),
178 PSTR488, PLANE0, OFFS026, SUB422),
179 MAKE_YUV_FORMAT (NV12, "raw video", GST_MAKE_FOURCC ('N', 'V', '1', '2'),
181 PLANE011, OFFS001, SUB420),
182 MAKE_YUV_FORMAT (NV21, "raw video", GST_MAKE_FOURCC ('N', 'V', '2', '1'),
184 PLANE011, OFFS010, SUB420),
186 MAKE_GRAY_FORMAT (GRAY8, "raw video", DPTH8, PSTR1, PLANE0, OFFS0, SUB4),
187 MAKE_GRAY_FORMAT (GRAY16_BE, "raw video", DPTH16, PSTR2, PLANE0, OFFS0, SUB4),
188 MAKE_GRAY_LE_FORMAT (GRAY16_LE, "raw video", DPTH16, PSTR2, PLANE0, OFFS0,
191 MAKE_YUV_FORMAT (v308, "raw video", GST_MAKE_FOURCC ('v', '3', '0', '8'),
193 PLANE0, OFFS012, SUB444),
194 MAKE_YUV_FORMAT (Y800, "raw video", GST_MAKE_FOURCC ('Y', '8', '0', '0'),
196 PLANE0, OFFS0, SUB4),
197 MAKE_YUV_FORMAT (Y16, "raw video", GST_MAKE_FOURCC ('Y', '1', '6', ' '),
199 PLANE0, OFFS0, SUB4),
201 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
202 MAKE_RGB_LE_FORMAT (RGB16, "raw video", DPTH565, PSTR222, PLANE0, OFFS0,
204 MAKE_RGB_LE_FORMAT (BGR16, "raw video", DPTH565, PSTR222, PLANE0, OFFS0,
206 MAKE_RGB_LE_FORMAT (RGB15, "raw video", DPTH555, PSTR222, PLANE0, OFFS0,
208 MAKE_RGB_LE_FORMAT (BGR15, "raw video", DPTH555, PSTR222, PLANE0, OFFS0,
211 MAKE_RGB_FORMAT (RGB16, "raw video", DPTH565, PSTR222, PLANE0, OFFS0, SUB444),
212 MAKE_RGB_FORMAT (BGR16, "raw video", DPTH565, PSTR222, PLANE0, OFFS0, SUB444),
213 MAKE_RGB_FORMAT (RGB15, "raw video", DPTH555, PSTR222, PLANE0, OFFS0, SUB444),
214 MAKE_RGB_FORMAT (BGR15, "raw video", DPTH555, PSTR222, PLANE0, OFFS0, SUB444),
217 MAKE_YUV_FORMAT (UYVP, "raw video", GST_MAKE_FOURCC ('U', 'Y', 'V', 'P'),
219 PSTR0, PLANE0, OFFS0, SUB422),
220 MAKE_YUVA_FORMAT (A420, "raw video", GST_MAKE_FOURCC ('A', '4', '2', '0'),
222 PSTR1111, PLANE0123, OFFS0, SUB4204),
223 MAKE_RGBA_FORMAT (RGB8_PALETTED, "raw video", DPTH8888, PSTR1111, PLANE0,
225 MAKE_YUV_FORMAT (YUV9, "raw video", GST_MAKE_FOURCC ('Y', 'U', 'V', '9'),
227 PLANE012, OFFS0, SUB410),
228 MAKE_YUV_FORMAT (YVU9, "raw video", GST_MAKE_FOURCC ('Y', 'V', 'U', '9'),
230 PLANE021, OFFS0, SUB410),
231 MAKE_YUV_FORMAT (IYU1, "raw video", GST_MAKE_FOURCC ('I', 'Y', 'U', '1'),
233 PLANE0, OFFS104, SUB411),
234 MAKE_RGBA_FORMAT (ARGB64, "raw video", DPTH16_16_16_16, PSTR8888, PLANE0,
237 MAKE_YUVA_FORMAT (AYUV64, "raw video", 0x00000000, DPTH16_16_16_16, PSTR8888,
240 MAKE_YUV_FORMAT (r210, "raw video", GST_MAKE_FOURCC ('r', '2', '1', '0'),
242 PSTR444, PLANE0, OFFS0, SUB444),
247 * @short_description: Support library for video operations
251 * This library contains some helper functions and includes the
252 * videosink and videofilter base classes.
258 * gst_video_calculate_display_ratio:
259 * @dar_n: Numerator of the calculated display_ratio
260 * @dar_d: Denominator of the calculated display_ratio
261 * @video_width: Width of the video frame in pixels
262 * @video_height: Height of the video frame in pixels
263 * @video_par_n: Numerator of the pixel aspect ratio of the input video.
264 * @video_par_d: Denominator of the pixel aspect ratio of the input video.
265 * @display_par_n: Numerator of the pixel aspect ratio of the display device
266 * @display_par_d: Denominator of the pixel aspect ratio of the display device
268 * Given the Pixel Aspect Ratio and size of an input video frame, and the
269 * pixel aspect ratio of the intended display device, calculates the actual
270 * display ratio the video will be rendered with.
272 * Returns: A boolean indicating success and a calculated Display Ratio in the
273 * dar_n and dar_d parameters.
274 * The return value is FALSE in the case of integer overflow or other error.
279 gst_video_calculate_display_ratio (guint * dar_n, guint * dar_d,
280 guint video_width, guint video_height,
281 guint video_par_n, guint video_par_d,
282 guint display_par_n, guint display_par_d)
287 g_return_val_if_fail (dar_n != NULL, FALSE);
288 g_return_val_if_fail (dar_d != NULL, FALSE);
290 /* Calculate (video_width * video_par_n * display_par_d) /
291 * (video_height * video_par_d * display_par_n) */
292 if (!gst_util_fraction_multiply (video_width, video_height, video_par_n,
293 video_par_d, &tmp_n, &tmp_d))
296 if (!gst_util_fraction_multiply (tmp_n, tmp_d, display_par_d, display_par_n,
300 g_return_val_if_fail (num > 0, FALSE);
301 g_return_val_if_fail (den > 0, FALSE);
311 GST_WARNING ("overflow in multiply");
316 static GstVideoFormat
317 gst_video_format_from_rgb32_masks (int red_mask, int green_mask, int blue_mask)
319 if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
320 blue_mask == 0x0000ff00) {
321 return GST_VIDEO_FORMAT_RGBx;
323 if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
324 blue_mask == 0xff000000) {
325 return GST_VIDEO_FORMAT_BGRx;
327 if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
328 blue_mask == 0x000000ff) {
329 return GST_VIDEO_FORMAT_xRGB;
331 if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
332 blue_mask == 0x00ff0000) {
333 return GST_VIDEO_FORMAT_xBGR;
336 return GST_VIDEO_FORMAT_UNKNOWN;
339 static GstVideoFormat
340 gst_video_format_from_rgba32_masks (int red_mask, int green_mask,
341 int blue_mask, int alpha_mask)
343 if (red_mask == 0xff000000 && green_mask == 0x00ff0000 &&
344 blue_mask == 0x0000ff00 && alpha_mask == 0x000000ff) {
345 return GST_VIDEO_FORMAT_RGBA;
347 if (red_mask == 0x0000ff00 && green_mask == 0x00ff0000 &&
348 blue_mask == 0xff000000 && alpha_mask == 0x000000ff) {
349 return GST_VIDEO_FORMAT_BGRA;
351 if (red_mask == 0x00ff0000 && green_mask == 0x0000ff00 &&
352 blue_mask == 0x000000ff && alpha_mask == 0xff000000) {
353 return GST_VIDEO_FORMAT_ARGB;
355 if (red_mask == 0x000000ff && green_mask == 0x0000ff00 &&
356 blue_mask == 0x00ff0000 && alpha_mask == 0xff000000) {
357 return GST_VIDEO_FORMAT_ABGR;
359 return GST_VIDEO_FORMAT_UNKNOWN;
362 static GstVideoFormat
363 gst_video_format_from_rgb24_masks (int red_mask, int green_mask, int blue_mask)
365 if (red_mask == 0xff0000 && green_mask == 0x00ff00 && blue_mask == 0x0000ff) {
366 return GST_VIDEO_FORMAT_RGB;
368 if (red_mask == 0x0000ff && green_mask == 0x00ff00 && blue_mask == 0xff0000) {
369 return GST_VIDEO_FORMAT_BGR;
372 return GST_VIDEO_FORMAT_UNKNOWN;
375 #define GST_VIDEO_COMP1_MASK_16_INT 0xf800
376 #define GST_VIDEO_COMP2_MASK_16_INT 0x07e0
377 #define GST_VIDEO_COMP3_MASK_16_INT 0x001f
379 #define GST_VIDEO_COMP1_MASK_15_INT 0x7c00
380 #define GST_VIDEO_COMP2_MASK_15_INT 0x03e0
381 #define GST_VIDEO_COMP3_MASK_15_INT 0x001f
383 static GstVideoFormat
384 gst_video_format_from_rgb16_masks (int red_mask, int green_mask, int blue_mask)
386 if (red_mask == GST_VIDEO_COMP1_MASK_16_INT
387 && green_mask == GST_VIDEO_COMP2_MASK_16_INT
388 && blue_mask == GST_VIDEO_COMP3_MASK_16_INT) {
389 return GST_VIDEO_FORMAT_RGB16;
391 if (red_mask == GST_VIDEO_COMP3_MASK_16_INT
392 && green_mask == GST_VIDEO_COMP2_MASK_16_INT
393 && blue_mask == GST_VIDEO_COMP1_MASK_16_INT) {
394 return GST_VIDEO_FORMAT_BGR16;
396 if (red_mask == GST_VIDEO_COMP1_MASK_15_INT
397 && green_mask == GST_VIDEO_COMP2_MASK_15_INT
398 && blue_mask == GST_VIDEO_COMP3_MASK_15_INT) {
399 return GST_VIDEO_FORMAT_RGB15;
401 if (red_mask == GST_VIDEO_COMP3_MASK_15_INT
402 && green_mask == GST_VIDEO_COMP2_MASK_15_INT
403 && blue_mask == GST_VIDEO_COMP1_MASK_15_INT) {
404 return GST_VIDEO_FORMAT_BGR15;
406 return GST_VIDEO_FORMAT_UNKNOWN;
410 * gst_video_format_from_masks:
411 * @depth: the amount of bits used for a pixel
412 * @bpp: the amount of bits used to store a pixel. This value is bigger than
414 * @endianness: the endianness of the masks
415 * @red_mask: the red mask
416 * @green_mask: the green mask
417 * @blue_mask: the blue mask
418 * @alpha_mask: the optional alpha mask
420 * Find the #GstVideoFormat for the given parameters.
422 * Returns: a #GstVideoFormat or GST_VIDEO_FORMAT_UNKNOWN when the parameters to
423 * not specify a known format.
426 gst_video_format_from_masks (gint depth, gint bpp, gint endianness,
427 gint red_mask, gint green_mask, gint blue_mask, gint alpha_mask)
429 GstVideoFormat format;
431 /* our caps system handles 24/32bpp RGB as big-endian. */
432 if ((bpp == 24 || bpp == 32) && endianness == G_LITTLE_ENDIAN) {
433 red_mask = GUINT32_TO_BE (red_mask);
434 green_mask = GUINT32_TO_BE (green_mask);
435 blue_mask = GUINT32_TO_BE (blue_mask);
436 endianness = G_BIG_ENDIAN;
444 if (depth == 30 && bpp == 32) {
445 format = GST_VIDEO_FORMAT_r210;
446 } else if (depth == 24 && bpp == 32) {
447 format = gst_video_format_from_rgb32_masks (red_mask, green_mask,
449 } else if (depth == 32 && bpp == 32 && alpha_mask) {
450 format = gst_video_format_from_rgba32_masks (red_mask, green_mask,
451 blue_mask, alpha_mask);
452 } else if (depth == 24 && bpp == 24) {
453 format = gst_video_format_from_rgb24_masks (red_mask, green_mask,
455 } else if ((depth == 15 || depth == 16) && bpp == 16 &&
456 endianness == G_BYTE_ORDER) {
457 format = gst_video_format_from_rgb16_masks (red_mask, green_mask,
459 } else if (depth == 8 && bpp == 8) {
460 format = GST_VIDEO_FORMAT_RGB8_PALETTED;
461 } else if (depth == 64 && bpp == 64) {
462 format = gst_video_format_from_rgba32_masks (red_mask, green_mask,
463 blue_mask, alpha_mask);
464 if (format == GST_VIDEO_FORMAT_ARGB) {
465 format = GST_VIDEO_FORMAT_ARGB64;
467 format = GST_VIDEO_FORMAT_UNKNOWN;
470 format = GST_VIDEO_FORMAT_UNKNOWN;
476 * gst_video_format_from_fourcc:
477 * @fourcc: a FOURCC value representing raw YUV video
479 * Converts a FOURCC value into the corresponding #GstVideoFormat.
480 * If the FOURCC cannot be represented by #GstVideoFormat,
481 * #GST_VIDEO_FORMAT_UNKNOWN is returned.
485 * Returns: the #GstVideoFormat describing the FOURCC value
488 gst_video_format_from_fourcc (guint32 fourcc)
491 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
492 return GST_VIDEO_FORMAT_I420;
493 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
494 return GST_VIDEO_FORMAT_YV12;
495 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
496 return GST_VIDEO_FORMAT_YUY2;
497 case GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'):
498 return GST_VIDEO_FORMAT_YVYU;
499 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
500 return GST_VIDEO_FORMAT_UYVY;
501 case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
502 return GST_VIDEO_FORMAT_AYUV;
503 case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
504 return GST_VIDEO_FORMAT_Y41B;
505 case GST_MAKE_FOURCC ('Y', '4', '2', 'B'):
506 return GST_VIDEO_FORMAT_Y42B;
507 case GST_MAKE_FOURCC ('Y', '4', '4', '4'):
508 return GST_VIDEO_FORMAT_Y444;
509 case GST_MAKE_FOURCC ('v', '2', '1', '0'):
510 return GST_VIDEO_FORMAT_v210;
511 case GST_MAKE_FOURCC ('v', '2', '1', '6'):
512 return GST_VIDEO_FORMAT_v216;
513 case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
514 return GST_VIDEO_FORMAT_NV12;
515 case GST_MAKE_FOURCC ('N', 'V', '2', '1'):
516 return GST_VIDEO_FORMAT_NV21;
517 case GST_MAKE_FOURCC ('v', '3', '0', '8'):
518 return GST_VIDEO_FORMAT_v308;
519 case GST_MAKE_FOURCC ('Y', '8', '0', '0'):
520 case GST_MAKE_FOURCC ('Y', '8', ' ', ' '):
521 case GST_MAKE_FOURCC ('G', 'R', 'E', 'Y'):
522 return GST_VIDEO_FORMAT_Y800;
523 case GST_MAKE_FOURCC ('Y', '1', '6', ' '):
524 return GST_VIDEO_FORMAT_Y16;
525 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'P'):
526 return GST_VIDEO_FORMAT_UYVP;
527 case GST_MAKE_FOURCC ('A', '4', '2', '0'):
528 return GST_VIDEO_FORMAT_A420;
529 case GST_MAKE_FOURCC ('Y', 'U', 'V', '9'):
530 return GST_VIDEO_FORMAT_YUV9;
531 case GST_MAKE_FOURCC ('Y', 'V', 'U', '9'):
532 return GST_VIDEO_FORMAT_YVU9;
533 case GST_MAKE_FOURCC ('I', 'Y', 'U', '1'):
534 return GST_VIDEO_FORMAT_IYU1;
535 case GST_MAKE_FOURCC ('A', 'Y', '6', '4'):
536 return GST_VIDEO_FORMAT_AYUV64;
538 return GST_VIDEO_FORMAT_UNKNOWN;
543 * gst_video_format_from_string:
544 * @format: a format string
546 * Convert the @format string to its #GstVideoFormat.
548 * Returns: the #GstVideoFormat for @format or GST_VIDEO_FORMAT_UNKNOWN when the
549 * string is not a known format.
552 gst_video_format_from_string (const gchar * format)
556 g_return_val_if_fail (format != NULL, GST_VIDEO_FORMAT_UNKNOWN);
558 for (i = 0; i < G_N_ELEMENTS (formats); i++) {
559 if (strcmp (GST_VIDEO_FORMAT_INFO_NAME (&formats[i].info), format) == 0)
560 return GST_VIDEO_FORMAT_INFO_FORMAT (&formats[i].info);
562 return GST_VIDEO_FORMAT_UNKNOWN;
567 * gst_video_format_to_fourcc:
568 * @format: a #GstVideoFormat video format
570 * Converts a #GstVideoFormat value into the corresponding FOURCC. Only
571 * a few YUV formats have corresponding FOURCC values. If @format has
572 * no corresponding FOURCC value, 0 is returned.
576 * Returns: the FOURCC corresponding to @format
579 gst_video_format_to_fourcc (GstVideoFormat format)
581 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, 0);
583 if (format >= G_N_ELEMENTS (formats))
586 return formats[format].fourcc;
590 gst_video_format_to_string (GstVideoFormat format)
592 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
594 if (format >= G_N_ELEMENTS (formats))
597 return GST_VIDEO_FORMAT_INFO_NAME (&formats[format].info);
601 * gst_video_format_get_info:
602 * @format: a #GstVideoFormat
604 * Get the #GstVideoFormatInfo for @format
606 * Returns: The #GstVideoFormatInfo for @format.
608 const GstVideoFormatInfo *
609 gst_video_format_get_info (GstVideoFormat format)
611 g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
612 g_return_val_if_fail (format < G_N_ELEMENTS (formats), NULL);
614 return &formats[format].info;
618 * gst_video_info_init:
619 * @info: a #GstVideoInfo
621 * Initialize @info with default values.
624 gst_video_info_init (GstVideoInfo * info)
626 g_return_if_fail (info != NULL);
628 memset (info, 0, sizeof (GstVideoInfo));
630 info->finfo = &formats[GST_VIDEO_FORMAT_UNKNOWN].info;
633 /* arrange for sensible defaults, e.g. if turned into caps */
641 * gst_video_info_set_format:
642 * @info: a #GstVideoInfo
643 * @format: the format
647 * Set the default info for a video frame of @format and @width and @height.
650 gst_video_info_set_format (GstVideoInfo * info, GstVideoFormat format,
651 guint width, guint height)
653 const GstVideoFormatInfo *finfo;
655 g_return_if_fail (info != NULL);
656 g_return_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN);
658 finfo = &formats[format].info;
663 info->height = height;
668 static const gchar *interlace_mode[] = {
676 gst_interlace_mode_to_string (GstVideoInterlaceMode mode)
678 if (((guint) mode) >= G_N_ELEMENTS (interlace_mode))
681 return interlace_mode[mode];
684 static GstVideoInterlaceMode
685 gst_interlace_mode_from_string (const gchar * mode)
688 for (i = 0; i < G_N_ELEMENTS (interlace_mode); i++) {
689 if (g_str_equal (interlace_mode[i], mode))
692 return GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
698 GstVideoChromaSite site;
701 static const ChromaSiteInfo chromasite[] = {
702 {"jpeg", GST_VIDEO_CHROMA_SITE_JPEG},
703 {"mpeg2", GST_VIDEO_CHROMA_SITE_MPEG2},
704 {"dv", GST_VIDEO_CHROMA_SITE_DV}
707 static GstVideoChromaSite
708 gst_video_chroma_from_string (const gchar * s)
711 for (i = 0; i < G_N_ELEMENTS (chromasite); i++) {
712 if (g_str_equal (chromasite[i].name, s))
713 return chromasite[i].site;
715 return GST_VIDEO_CHROMA_SITE_UNKNOWN;
719 gst_video_chroma_to_string (GstVideoChromaSite site)
722 for (i = 0; i < G_N_ELEMENTS (chromasite); i++) {
723 if (chromasite[i].site == site)
724 return chromasite[i].name;
732 GstVideoColorimetry color;
735 #define MAKE_COLORIMETRY(n,r,m,t,p) { GST_VIDEO_COLORIMETRY_ ##n, \
736 { GST_VIDEO_COLOR_RANGE ##r, GST_VIDEO_COLOR_MATRIX_ ##m, \
737 GST_VIDEO_TRANSFER_ ##t, GST_VIDEO_COLOR_PRIMARIES_ ##p } }
739 static const ColorimetryInfo colorimetry[] = {
740 MAKE_COLORIMETRY (BT601, _16_235, BT601, BT709, BT470M),
741 MAKE_COLORIMETRY (BT709, _16_235, BT709, BT709, BT709),
742 MAKE_COLORIMETRY (SMPTE240M, _16_235, SMPTE240M, SMPTE240M, SMPTE240M),
745 static const ColorimetryInfo *
746 gst_video_get_colorimetry (const gchar * s)
750 for (i = 0; i < G_N_ELEMENTS (colorimetry); i++) {
751 if (g_str_equal (colorimetry[i].name, s))
752 return &colorimetry[i];
757 #define IS_EQUAL(ci,i) (((ci)->color.range == (i)->range) && \
758 ((ci)->color.matrix == (i)->matrix) && \
759 ((ci)->color.transfer == (i)->transfer) && \
760 ((ci)->color.primaries == (i)->primaries))
764 * gst_video_colorimetry_from_string
765 * @cinfo: a #GstVideoColorimetry
766 * @color: a colorimetry string
768 * Parse the colorimetry string and update @cinfo with the parsed
771 * Returns: #TRUE if @color points to valid colorimetry info.
774 gst_video_colorimetry_from_string (GstVideoColorimetry * cinfo,
777 const ColorimetryInfo *ci;
779 if ((ci = gst_video_get_colorimetry (color))) {
782 /* FIXME, split and parse */
783 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
784 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
785 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
786 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
792 gst_video_caps_set_colorimetry (GstCaps * caps, GstVideoColorimetry * cinfo)
796 for (i = 0; i < G_N_ELEMENTS (colorimetry); i++) {
797 if (IS_EQUAL (&colorimetry[i], cinfo)) {
798 gst_caps_set_simple (caps, "colorimetry", G_TYPE_STRING,
799 colorimetry[i].name, NULL);
803 /* FIXME, construct colorimetry */
807 * gst_video_colorimetry_matches:
808 * @info: a #GstVideoInfo
809 * @color: a colorimetry string
811 * Check if the colorimetry information in @info matches that of the
814 * Returns: #TRUE if @color conveys the same colorimetry info as the color
815 * information in @info.
818 gst_video_colorimetry_matches (GstVideoColorimetry * cinfo, const gchar * color)
820 const ColorimetryInfo *ci;
822 if ((ci = gst_video_get_colorimetry (color)))
823 return IS_EQUAL (ci, cinfo);
829 * gst_video_info_from_caps:
830 * @info: a #GstVideoInfo
833 * Parse @caps and update @info.
835 * Returns: TRUE if @caps could be parsed
838 gst_video_info_from_caps (GstVideoInfo * info, const GstCaps * caps)
840 GstStructure *structure;
842 GstVideoFormat format;
843 gint width, height, views;
847 g_return_val_if_fail (info != NULL, FALSE);
848 g_return_val_if_fail (caps != NULL, FALSE);
849 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
851 GST_DEBUG ("parsing caps %" GST_PTR_FORMAT, caps);
853 structure = gst_caps_get_structure (caps, 0);
855 if (!gst_structure_has_name (structure, "video/x-raw"))
858 if (!(s = gst_structure_get_string (structure, "format")))
861 format = gst_video_format_from_string (s);
862 if (format == GST_VIDEO_FORMAT_UNKNOWN)
865 if (!gst_structure_get_int (structure, "width", &width))
867 if (!gst_structure_get_int (structure, "height", &height))
870 gst_video_info_set_format (info, format, width, height);
872 if (gst_structure_get_fraction (structure, "framerate", &fps_n, &fps_d)) {
874 /* variable framerate */
875 info->flags |= GST_VIDEO_FLAG_VARIABLE_FPS;
876 /* see if we have a max-framerate */
877 gst_structure_get_fraction (structure, "max-framerate", &fps_n, &fps_d);
882 /* unspecified is variable framerate */
887 if ((s = gst_structure_get_string (structure, "interlace-mode")))
888 info->interlace_mode = gst_interlace_mode_from_string (s);
890 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
892 if (gst_structure_get_int (structure, "views", &views))
897 if ((s = gst_structure_get_string (structure, "chroma-site")))
898 info->chroma_site = gst_video_chroma_from_string (s);
900 info->chroma_site = GST_VIDEO_CHROMA_SITE_UNKNOWN;
902 if ((s = gst_structure_get_string (structure, "colorimetry")))
903 gst_video_colorimetry_from_string (&info->colorimetry, s);
905 memset (&info->colorimetry, 0, sizeof (GstVideoColorimetry));
907 if (gst_structure_get_fraction (structure, "pixel-aspect-ratio",
920 GST_ERROR ("wrong name '%s', expected video/x-raw",
921 gst_structure_get_name (structure));
926 GST_ERROR ("no format given");
931 GST_ERROR ("unknown format '%s' given", s);
936 GST_ERROR ("no width property given");
941 GST_ERROR ("no height property given");
947 * gst_video_info_to_caps:
948 * @info: a #GstVideoInfo
950 * Convert the values of @info into a #GstCaps.
952 * Returns: a new #GstCaps containing the info of @info.
955 gst_video_info_to_caps (GstVideoInfo * info)
960 g_return_val_if_fail (info != NULL, NULL);
961 g_return_val_if_fail (info->finfo != NULL, NULL);
962 g_return_val_if_fail (info->finfo->format != GST_VIDEO_FORMAT_UNKNOWN, NULL);
964 format = gst_video_format_to_string (info->finfo->format);
965 g_return_val_if_fail (format != NULL, NULL);
967 caps = gst_caps_new_simple ("video/x-raw",
968 "format", G_TYPE_STRING, format,
969 "width", G_TYPE_INT, info->width,
970 "height", G_TYPE_INT, info->height,
971 "pixel-aspect-ratio", GST_TYPE_FRACTION, info->par_n, info->par_d, NULL);
973 gst_caps_set_simple (caps, "interlace-mode", G_TYPE_STRING,
974 gst_interlace_mode_to_string (info->interlace_mode), NULL);
976 if (info->chroma_site != GST_VIDEO_CHROMA_SITE_UNKNOWN)
977 gst_caps_set_simple (caps, "chroma-site", G_TYPE_STRING,
978 gst_video_chroma_to_string (info->chroma_site), NULL);
980 gst_video_caps_set_colorimetry (caps, &info->colorimetry);
983 gst_caps_set_simple (caps, "views", G_TYPE_INT, info->views, NULL);
985 if (info->flags & GST_VIDEO_FLAG_VARIABLE_FPS && info->fps_n != 0) {
986 /* variable fps with a max-framerate */
987 gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, 0, 1,
988 "max-framerate", GST_TYPE_FRACTION, info->fps_n, info->fps_d, NULL);
990 /* no variable fps or no max-framerate */
991 gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION,
992 info->fps_n, info->fps_d, NULL);
999 * gst_video_frame_map_id:
1000 * @frame: pointer to #GstVideoFrame
1001 * @info: a #GstVideoInfo
1002 * @buffer: the buffer to map
1003 * @id: the frame id to map
1004 * @flags: #GstMapFlags
1006 * Use @info and @buffer to fill in the values of @frame with the video frame
1007 * information of frame @id.
1009 * When @id is -1, the default frame is mapped. When @id != -1, this function
1010 * will return %FALSE when there is no GstVideoMeta with that id.
1012 * All video planes of @buffer will be mapped and the pointers will be set in
1015 * Returns: %TRUE on success.
1018 gst_video_frame_map_id (GstVideoFrame * frame, GstVideoInfo * info,
1019 GstBuffer * buffer, gint id, GstMapFlags flags)
1024 g_return_val_if_fail (frame != NULL, FALSE);
1025 g_return_val_if_fail (info != NULL, FALSE);
1026 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1029 meta = gst_buffer_get_video_meta (buffer);
1031 meta = gst_buffer_get_video_meta_id (buffer, id);
1034 frame->info = *info;
1037 frame->info.finfo = &formats[meta->format].info;
1038 frame->info.width = meta->width;
1039 frame->info.height = meta->height;
1040 frame->id = meta->id;
1041 frame->flags = meta->flags;
1043 for (i = 0; i < info->finfo->n_planes; i++)
1044 gst_video_meta_map (meta, i, &frame->map[i], &frame->data[i],
1045 &frame->info.stride[i], flags);
1047 /* no metadata, we really need to have the metadata when the id is
1055 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_VIDEO_BUFFER_FLAG_TFF))
1056 frame->flags |= GST_VIDEO_FRAME_FLAG_TFF;
1057 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_VIDEO_BUFFER_FLAG_RFF))
1058 frame->flags |= GST_VIDEO_FRAME_FLAG_RFF;
1059 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_VIDEO_BUFFER_FLAG_ONEFIELD))
1060 frame->flags |= GST_VIDEO_FRAME_FLAG_ONEFIELD;
1062 gst_buffer_map (buffer, &frame->map[0], flags);
1064 /* do some sanity checks */
1065 if (frame->map[0].size < info->size)
1068 /* set up pointers */
1069 for (i = 0; i < info->finfo->n_planes; i++) {
1070 frame->data[i] = frame->map[0].data + info->offset[i];
1073 frame->buffer = gst_buffer_ref (buffer);
1081 GST_ERROR ("no GstVideoMeta for id %d", id);
1086 GST_ERROR ("invalid buffer size %" G_GSIZE_FORMAT " < %" G_GSIZE_FORMAT,
1087 frame->map[0].size, info->size);
1088 gst_buffer_unmap (buffer, &frame->map[0]);
1094 * gst_video_frame_map:
1095 * @frame: pointer to #GstVideoFrame
1096 * @info: a #GstVideoInfo
1097 * @buffer: the buffer to map
1098 * @flags: #GstMapFlags
1100 * Use @info and @buffer to fill in the values of @frame.
1102 * All video planes of @buffer will be mapped and the pointers will be set in
1105 * Returns: %TRUE on success.
1108 gst_video_frame_map (GstVideoFrame * frame, GstVideoInfo * info,
1109 GstBuffer * buffer, GstMapFlags flags)
1111 return gst_video_frame_map_id (frame, info, buffer, -1, flags);
1115 * gst_video_frame_unmap:
1116 * @frame: a #GstVideoFrame
1118 * Unmap the memory previously mapped with gst_video_frame_map.
1121 gst_video_frame_unmap (GstVideoFrame * frame)
1127 g_return_if_fail (frame != NULL);
1129 buffer = frame->buffer;
1133 for (i = 0; i < frame->info.finfo->n_planes; i++) {
1134 gst_video_meta_unmap (meta, i, &frame->map[i]);
1137 gst_buffer_unmap (buffer, &frame->map[0]);
1139 gst_buffer_unref (buffer);
1143 * gst_video_frame_copy:
1144 * @dest: a #GstVideoFrame
1145 * @src: a #GstVideoFrame
1148 * Copy the plane with index @plane from @src to @dest.
1150 * Returns: TRUE if the contents could be copied.
1153 gst_video_frame_copy_plane (GstVideoFrame * dest, const GstVideoFrame * src,
1156 const GstVideoInfo *sinfo;
1157 GstVideoInfo *dinfo;
1162 g_return_val_if_fail (dest != NULL, FALSE);
1163 g_return_val_if_fail (src != NULL, FALSE);
1166 dinfo = &dest->info;
1168 g_return_val_if_fail (dinfo->finfo->format == sinfo->finfo->format, FALSE);
1169 g_return_val_if_fail (dinfo->width == sinfo->width
1170 && dinfo->height == sinfo->height, FALSE);
1171 g_return_val_if_fail (dinfo->finfo->n_planes > plane, FALSE);
1173 sp = src->data[plane];
1174 dp = dest->data[plane];
1176 ss = sinfo->stride[plane];
1177 ds = dinfo->stride[plane];
1179 w = MIN (ABS (ss), ABS (ds));
1180 h = GST_VIDEO_FRAME_COMP_HEIGHT (dest, plane);
1182 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy plane %d, w:%d h:%d ", plane, w, h);
1184 for (j = 0; j < h; j++) {
1193 * gst_video_frame_copy:
1194 * @dest: a #GstVideoFrame
1195 * @src: a #GstVideoFrame
1197 * Copy the contents from @src to @dest.
1199 * Returns: TRUE if the contents could be copied.
1202 gst_video_frame_copy (GstVideoFrame * dest, const GstVideoFrame * src)
1205 const GstVideoInfo *sinfo;
1206 GstVideoInfo *dinfo;
1208 g_return_val_if_fail (dest != NULL, FALSE);
1209 g_return_val_if_fail (src != NULL, FALSE);
1212 dinfo = &dest->info;
1214 g_return_val_if_fail (dinfo->finfo->format == sinfo->finfo->format, FALSE);
1215 g_return_val_if_fail (dinfo->width == sinfo->width
1216 && dinfo->height == sinfo->height, FALSE);
1218 n_planes = dinfo->finfo->n_planes;
1220 for (i = 0; i < n_planes; i++)
1221 gst_video_frame_copy_plane (dest, src, i);
1227 fill_planes (GstVideoInfo * info)
1231 width = info->width;
1232 height = info->height;
1234 switch (info->finfo->format) {
1235 case GST_VIDEO_FORMAT_YUY2:
1236 case GST_VIDEO_FORMAT_YVYU:
1237 case GST_VIDEO_FORMAT_UYVY:
1238 info->stride[0] = GST_ROUND_UP_4 (width * 2);
1239 info->offset[0] = 0;
1240 info->size = info->stride[0] * height;
1242 case GST_VIDEO_FORMAT_AYUV:
1243 case GST_VIDEO_FORMAT_RGBx:
1244 case GST_VIDEO_FORMAT_RGBA:
1245 case GST_VIDEO_FORMAT_BGRx:
1246 case GST_VIDEO_FORMAT_BGRA:
1247 case GST_VIDEO_FORMAT_xRGB:
1248 case GST_VIDEO_FORMAT_ARGB:
1249 case GST_VIDEO_FORMAT_xBGR:
1250 case GST_VIDEO_FORMAT_ABGR:
1251 case GST_VIDEO_FORMAT_r210:
1252 info->stride[0] = width * 4;
1253 info->offset[0] = 0;
1254 info->size = info->stride[0] * height;
1256 case GST_VIDEO_FORMAT_RGB16:
1257 case GST_VIDEO_FORMAT_BGR16:
1258 case GST_VIDEO_FORMAT_RGB15:
1259 case GST_VIDEO_FORMAT_BGR15:
1260 info->stride[0] = GST_ROUND_UP_4 (width * 2);
1261 info->offset[0] = 0;
1262 info->size = info->stride[0] * height;
1264 case GST_VIDEO_FORMAT_RGB:
1265 case GST_VIDEO_FORMAT_BGR:
1266 case GST_VIDEO_FORMAT_v308:
1267 info->stride[0] = GST_ROUND_UP_4 (width * 3);
1268 info->offset[0] = 0;
1269 info->size = info->stride[0] * height;
1271 case GST_VIDEO_FORMAT_v210:
1272 info->stride[0] = ((width + 47) / 48) * 128;
1273 info->offset[0] = 0;
1274 info->size = info->stride[0] * height;
1276 case GST_VIDEO_FORMAT_v216:
1277 info->stride[0] = GST_ROUND_UP_8 (width * 4);
1278 info->offset[0] = 0;
1279 info->size = info->stride[0] * height;
1281 case GST_VIDEO_FORMAT_GRAY8:
1282 case GST_VIDEO_FORMAT_Y800:
1283 info->stride[0] = GST_ROUND_UP_4 (width);
1284 info->offset[0] = 0;
1285 info->size = info->stride[0] * height;
1287 case GST_VIDEO_FORMAT_GRAY16_BE:
1288 case GST_VIDEO_FORMAT_GRAY16_LE:
1289 case GST_VIDEO_FORMAT_Y16:
1290 info->stride[0] = GST_ROUND_UP_4 (width * 2);
1291 info->offset[0] = 0;
1292 info->size = info->stride[0] * height;
1294 case GST_VIDEO_FORMAT_UYVP:
1295 info->stride[0] = GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4);
1296 info->offset[0] = 0;
1297 info->size = info->stride[0] * height;
1299 case GST_VIDEO_FORMAT_RGB8_PALETTED:
1300 info->stride[0] = GST_ROUND_UP_4 (width);
1301 info->offset[0] = 0;
1302 info->size = info->stride[0] * height;
1304 case GST_VIDEO_FORMAT_IYU1:
1305 info->stride[0] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) +
1306 GST_ROUND_UP_4 (width) / 2);
1307 info->offset[0] = 0;
1308 info->size = info->stride[0] * height;
1310 case GST_VIDEO_FORMAT_ARGB64:
1311 case GST_VIDEO_FORMAT_AYUV64:
1312 info->stride[0] = width * 8;
1313 info->offset[0] = 0;
1314 info->size = info->stride[0] * height;
1316 case GST_VIDEO_FORMAT_I420:
1317 case GST_VIDEO_FORMAT_YV12: /* same as I420, but plane 1+2 swapped */
1318 info->stride[0] = GST_ROUND_UP_4 (width);
1319 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1320 info->stride[2] = info->stride[1];
1321 info->offset[0] = 0;
1322 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1323 info->offset[2] = info->offset[1] +
1324 info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
1325 info->size = info->offset[2] +
1326 info->stride[2] * (GST_ROUND_UP_2 (height) / 2);
1328 case GST_VIDEO_FORMAT_Y41B:
1329 info->stride[0] = GST_ROUND_UP_4 (width);
1330 info->stride[1] = GST_ROUND_UP_16 (width) / 4;
1331 info->stride[2] = info->stride[1];
1332 info->offset[0] = 0;
1333 info->offset[1] = info->stride[0] * height;
1334 info->offset[2] = info->offset[1] + info->stride[1] * height;
1335 /* simplification of ROUNDUP4(w)*h + 2*((ROUNDUP16(w)/4)*h */
1336 info->size = (info->stride[0] + (GST_ROUND_UP_16 (width) / 2)) * height;
1338 case GST_VIDEO_FORMAT_Y42B:
1339 info->stride[0] = GST_ROUND_UP_4 (width);
1340 info->stride[1] = GST_ROUND_UP_8 (width) / 2;
1341 info->stride[2] = info->stride[1];
1342 info->offset[0] = 0;
1343 info->offset[1] = info->stride[0] * height;
1344 info->offset[2] = info->offset[1] + info->stride[1] * height;
1345 /* simplification of ROUNDUP4(w)*h + 2*(ROUNDUP8(w)/2)*h */
1346 info->size = (info->stride[0] + GST_ROUND_UP_8 (width)) * height;
1348 case GST_VIDEO_FORMAT_Y444:
1349 info->stride[0] = GST_ROUND_UP_4 (width);
1350 info->stride[1] = info->stride[0];
1351 info->stride[2] = info->stride[0];
1352 info->offset[0] = 0;
1353 info->offset[1] = info->stride[0] * height;
1354 info->offset[2] = info->offset[1] * 2;
1355 info->size = info->stride[0] * height * 3;
1357 case GST_VIDEO_FORMAT_NV12:
1358 case GST_VIDEO_FORMAT_NV21:
1359 info->stride[0] = GST_ROUND_UP_4 (width);
1360 info->stride[1] = info->stride[0];
1361 info->offset[0] = 0;
1362 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1363 info->size = info->stride[0] * GST_ROUND_UP_2 (height) * 3 / 2;
1365 case GST_VIDEO_FORMAT_A420:
1366 info->stride[0] = GST_ROUND_UP_4 (width);
1367 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1368 info->stride[2] = info->stride[1];
1369 info->stride[3] = info->stride[0];
1370 info->offset[0] = 0;
1371 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1372 info->offset[2] = info->offset[1] +
1373 info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
1374 info->offset[3] = info->offset[2] +
1375 info->stride[2] * (GST_ROUND_UP_2 (height) / 2);
1376 info->size = info->offset[3] + info->stride[0];
1378 case GST_VIDEO_FORMAT_YUV9:
1379 case GST_VIDEO_FORMAT_YVU9:
1380 info->stride[0] = GST_ROUND_UP_4 (width);
1381 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
1382 info->stride[2] = info->stride[1];
1383 info->offset[0] = 0;
1384 info->offset[1] = info->stride[0] * height;
1385 info->offset[2] = info->offset[1] +
1386 info->stride[1] * (GST_ROUND_UP_4 (height) / 4);
1387 info->size = info->offset[2] +
1388 info->stride[2] * (GST_ROUND_UP_4 (height) / 4);
1390 case GST_VIDEO_FORMAT_UNKNOWN:
1391 GST_ERROR ("invalid format");
1392 g_warning ("invalid format");
1399 * gst_video_format_convert:
1400 * @info: a #GstVideoInfo
1401 * @src_format: #GstFormat of the @src_value
1402 * @src_value: value to convert
1403 * @dest_format: #GstFormat of the @dest_value
1404 * @dest_value: pointer to destination value
1406 * Converts among various #GstFormat types. This function handles
1407 * GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT. For
1408 * raw video, GST_FORMAT_DEFAULT corresponds to video frames. This
1409 * function can be used to handle pad queries of the type GST_QUERY_CONVERT.
1413 * Returns: TRUE if the conversion was successful.
1416 gst_video_info_convert (GstVideoInfo * info,
1417 GstFormat src_format, gint64 src_value,
1418 GstFormat dest_format, gint64 * dest_value)
1420 gboolean ret = FALSE;
1421 int size, fps_n, fps_d;
1423 g_return_val_if_fail (info != NULL, 0);
1424 g_return_val_if_fail (info->finfo != NULL, 0);
1425 g_return_val_if_fail (info->finfo->format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1426 g_return_val_if_fail (info->size > 0, 0);
1429 fps_n = info->fps_n;
1430 fps_d = info->fps_d;
1432 GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s to %s",
1433 src_value, gst_format_get_name (src_format),
1434 gst_format_get_name (dest_format));
1436 if (src_format == dest_format) {
1437 *dest_value = src_value;
1442 if (src_value == -1) {
1448 /* bytes to frames */
1449 if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_DEFAULT) {
1451 *dest_value = gst_util_uint64_scale_int (src_value, 1, size);
1453 GST_ERROR ("blocksize is 0");
1460 /* frames to bytes */
1461 if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_BYTES) {
1462 *dest_value = gst_util_uint64_scale_int (src_value, size, 1);
1467 /* time to frames */
1468 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_DEFAULT) {
1470 *dest_value = gst_util_uint64_scale (src_value,
1471 fps_n, GST_SECOND * fps_d);
1473 GST_ERROR ("framerate denominator is 0");
1480 /* frames to time */
1481 if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) {
1483 *dest_value = gst_util_uint64_scale (src_value,
1484 GST_SECOND * fps_d, fps_n);
1486 GST_ERROR ("framerate numerator is 0");
1494 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
1496 *dest_value = gst_util_uint64_scale (src_value,
1497 fps_n * size, GST_SECOND * fps_d);
1499 GST_ERROR ("framerate denominator is 0");
1507 if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
1508 if (fps_n != 0 && size != 0) {
1509 *dest_value = gst_util_uint64_scale (src_value,
1510 GST_SECOND * fps_d, fps_n * size);
1512 GST_ERROR ("framerate denominator and/or blocksize is 0");
1520 GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, ret, *dest_value);
1525 #define GST_VIDEO_EVENT_STILL_STATE_NAME "GstEventStillFrame"
1528 * gst_video_event_new_still_frame:
1529 * @in_still: boolean value for the still-frame state of the event.
1531 * Creates a new Still Frame event. If @in_still is %TRUE, then the event
1532 * represents the start of a still frame sequence. If it is %FALSE, then
1533 * the event ends a still frame sequence.
1535 * To parse an event created by gst_video_event_new_still_frame() use
1536 * gst_video_event_parse_still_frame().
1538 * Returns: The new GstEvent
1542 gst_video_event_new_still_frame (gboolean in_still)
1544 GstEvent *still_event;
1547 s = gst_structure_new (GST_VIDEO_EVENT_STILL_STATE_NAME,
1548 "still-state", G_TYPE_BOOLEAN, in_still, NULL);
1549 still_event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1555 * gst_video_event_parse_still_frame:
1556 * @event: A #GstEvent to parse
1557 * @in_still: A boolean to receive the still-frame status from the event, or NULL
1559 * Parse a #GstEvent, identify if it is a Still Frame event, and
1560 * return the still-frame state from the event if it is.
1561 * If the event represents the start of a still frame, the in_still
1562 * variable will be set to TRUE, otherwise FALSE. It is OK to pass NULL for the
1563 * in_still variable order to just check whether the event is a valid still-frame
1566 * Create a still frame event using gst_video_event_new_still_frame()
1568 * Returns: %TRUE if the event is a valid still-frame event. %FALSE if not
1572 gst_video_event_parse_still_frame (GstEvent * event, gboolean * in_still)
1574 const GstStructure *s;
1575 gboolean ev_still_state;
1577 g_return_val_if_fail (event != NULL, FALSE);
1579 if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM)
1580 return FALSE; /* Not a still frame event */
1582 s = gst_event_get_structure (event);
1584 || !gst_structure_has_name (s, GST_VIDEO_EVENT_STILL_STATE_NAME))
1585 return FALSE; /* Not a still frame event */
1586 if (!gst_structure_get_boolean (s, "still-state", &ev_still_state))
1587 return FALSE; /* Not a still frame event */
1589 *in_still = ev_still_state;
1594 * gst_video_parse_caps_palette:
1595 * @caps: #GstCaps to parse
1597 * Returns the palette data from the caps as a #GstBuffer. For
1598 * #GST_VIDEO_FORMAT_RGB8_PALETTED this is containing 256 #guint32
1599 * values, each containing ARGB colors in native endianness.
1601 * Returns: a #GstBuffer containing the palette data. Unref after usage.
1605 gst_video_parse_caps_palette (GstCaps * caps)
1611 if (!gst_caps_is_fixed (caps))
1614 s = gst_caps_get_structure (caps, 0);
1616 p_v = gst_structure_get_value (s, "palette_data");
1617 if (!p_v || !GST_VALUE_HOLDS_BUFFER (p_v))
1620 p = g_value_dup_boxed (p_v);
1625 #define GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME "GstForceKeyUnit"
1628 * gst_video_event_new_downstream_force_key_unit:
1629 * @timestamp: the timestamp of the buffer that starts a new key unit
1630 * @stream_time: the stream_time of the buffer that starts a new key unit
1631 * @running_time: the running_time of the buffer that starts a new key unit
1632 * @all_headers: %TRUE to produce headers when starting a new key unit
1633 * @count: integer that can be used to number key units
1635 * Creates a new downstream force key unit event. A downstream force key unit
1636 * event can be sent down the pipeline to request downstream elements to produce
1637 * a key unit. A downstream force key unit event must also be sent when handling
1638 * an upstream force key unit event to notify downstream that the latter has been
1641 * To parse an event created by gst_video_event_new_downstream_force_key_unit() use
1642 * gst_video_event_parse_downstream_force_key_unit().
1644 * Returns: The new GstEvent
1648 gst_video_event_new_downstream_force_key_unit (GstClockTime timestamp,
1649 GstClockTime stream_time, GstClockTime running_time, gboolean all_headers,
1652 GstEvent *force_key_unit_event;
1655 s = gst_structure_new (GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME,
1656 "timestamp", G_TYPE_UINT64, timestamp,
1657 "stream-time", G_TYPE_UINT64, stream_time,
1658 "running-time", G_TYPE_UINT64, running_time,
1659 "all-headers", G_TYPE_BOOLEAN, all_headers,
1660 "count", G_TYPE_UINT, count, NULL);
1661 force_key_unit_event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1663 return force_key_unit_event;
1667 * gst_video_event_new_upstream_force_key_unit:
1668 * @running_time: the running_time at which a new key unit should be produced
1669 * @all_headers: %TRUE to produce headers when starting a new key unit
1670 * @count: integer that can be used to number key units
1672 * Creates a new upstream force key unit event. An upstream force key unit event
1673 * can be sent to request upstream elements to produce a key unit.
1675 * @running_time can be set to request a new key unit at a specific
1676 * running_time. If set to GST_CLOCK_TIME_NONE, upstream elements will produce a
1677 * new key unit as soon as possible.
1679 * To parse an event created by gst_video_event_new_downstream_force_key_unit() use
1680 * gst_video_event_parse_downstream_force_key_unit().
1682 * Returns: The new GstEvent
1686 gst_video_event_new_upstream_force_key_unit (GstClockTime running_time,
1687 gboolean all_headers, guint count)
1689 GstEvent *force_key_unit_event;
1692 s = gst_structure_new (GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME,
1693 "running-time", GST_TYPE_CLOCK_TIME, running_time,
1694 "all-headers", G_TYPE_BOOLEAN, all_headers,
1695 "count", G_TYPE_UINT, count, NULL);
1696 force_key_unit_event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, s);
1698 return force_key_unit_event;
1702 * gst_video_event_is_force_key_unit:
1703 * @event: A #GstEvent to check
1705 * Checks if an event is a force key unit event. Returns true for both upstream
1706 * and downstream force key unit events.
1708 * Returns: %TRUE if the event is a valid force key unit event
1712 gst_video_event_is_force_key_unit (GstEvent * event)
1714 const GstStructure *s;
1716 g_return_val_if_fail (event != NULL, FALSE);
1718 if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM &&
1719 GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_UPSTREAM)
1720 return FALSE; /* Not a force key unit event */
1722 s = gst_event_get_structure (event);
1724 || !gst_structure_has_name (s, GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME))
1731 * gst_video_event_parse_downstream_force_key_unit:
1732 * @event: A #GstEvent to parse
1733 * @timestamp: (out): A pointer to the timestamp in the event
1734 * @stream_time: (out): A pointer to the stream-time in the event
1735 * @running_time: (out): A pointer to the running-time in the event
1736 * @all_headers: (out): A pointer to the all_headers flag in the event
1737 * @count: (out): A pointer to the count field of the event
1739 * Get timestamp, stream-time, running-time, all-headers and count in the force
1740 * key unit event. See gst_video_event_new_downstream_force_key_unit() for a
1741 * full description of the downstream force key unit event.
1743 * Returns: %TRUE if the event is a valid downstream force key unit event.
1747 gst_video_event_parse_downstream_force_key_unit (GstEvent * event,
1748 GstClockTime * timestamp, GstClockTime * stream_time,
1749 GstClockTime * running_time, gboolean * all_headers, guint * count)
1751 const GstStructure *s;
1752 GstClockTime ev_timestamp, ev_stream_time, ev_running_time;
1753 gboolean ev_all_headers;
1756 g_return_val_if_fail (event != NULL, FALSE);
1758 if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM)
1759 return FALSE; /* Not a force key unit event */
1761 s = gst_event_get_structure (event);
1763 || !gst_structure_has_name (s, GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME))
1766 if (!gst_structure_get_clock_time (s, "timestamp", &ev_timestamp))
1767 return FALSE; /* Not a force key unit event */
1768 if (!gst_structure_get_clock_time (s, "stream-time", &ev_stream_time))
1769 return FALSE; /* Not a force key unit event */
1770 if (!gst_structure_get_clock_time (s, "running-time", &ev_running_time))
1771 return FALSE; /* Not a force key unit event */
1772 if (!gst_structure_get_boolean (s, "all-headers", &ev_all_headers))
1773 return FALSE; /* Not a force key unit event */
1774 if (!gst_structure_get_uint (s, "count", &ev_count))
1775 return FALSE; /* Not a force key unit event */
1778 *timestamp = ev_timestamp;
1781 *stream_time = ev_stream_time;
1784 *running_time = ev_running_time;
1787 *all_headers = ev_all_headers;
1796 * gst_video_event_parse_upstream_force_key_unit:
1797 * @event: A #GstEvent to parse
1798 * @running_time: (out): A pointer to the running_time in the event
1799 * @all_headers: (out): A pointer to the all_headers flag in the event
1800 * @count: (out): A pointer to the count field in the event
1802 * Get running-time, all-headers and count in the force key unit event. See
1803 * gst_video_event_new_upstream_force_key_unit() for a full description of the
1804 * upstream force key unit event.
1806 * Create an upstream force key unit event using gst_video_event_new_upstream_force_key_unit()
1808 * Returns: %TRUE if the event is a valid upstream force-key-unit event. %FALSE if not
1812 gst_video_event_parse_upstream_force_key_unit (GstEvent * event,
1813 GstClockTime * running_time, gboolean * all_headers, guint * count)
1815 const GstStructure *s;
1816 GstClockTime ev_running_time;
1817 gboolean ev_all_headers;
1820 g_return_val_if_fail (event != NULL, FALSE);
1822 if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_UPSTREAM)
1823 return FALSE; /* Not a force key unit event */
1825 s = gst_event_get_structure (event);
1827 || !gst_structure_has_name (s, GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME))
1830 if (!gst_structure_get_clock_time (s, "running-time", &ev_running_time))
1831 return FALSE; /* Not a force key unit event */
1832 if (!gst_structure_get_boolean (s, "all-headers", &ev_all_headers))
1833 return FALSE; /* Not a force key unit event */
1834 if (!gst_structure_get_uint (s, "count", &ev_count))
1835 return FALSE; /* Not a force key unit event */
1838 *running_time = ev_running_time;
1841 *all_headers = ev_all_headers;