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.flags = meta->flags;
1035 frame->info.finfo = &formats[meta->format].info;
1036 frame->info.width = meta->width;
1037 frame->info.height = meta->height;
1038 frame->id = meta->id;
1040 for (i = 0; i < info->finfo->n_planes; i++)
1041 gst_video_meta_map (meta, i, &frame->map[i], &frame->data[i],
1042 &frame->info.stride[i], flags);
1044 /* no metadata, we really need to have the metadata when the id is
1050 frame->info = *info;
1053 gst_buffer_map (buffer, &frame->map[0], flags);
1055 /* do some sanity checks */
1056 if (frame->map[0].size < info->size)
1059 /* set up pointers */
1060 for (i = 0; i < info->finfo->n_planes; i++) {
1061 frame->data[i] = frame->map[0].data + info->offset[i];
1064 frame->buffer = gst_buffer_ref (buffer);
1072 GST_ERROR ("no GstVideoMeta for id %d", id);
1077 GST_ERROR ("invalid buffer size %" G_GSIZE_FORMAT " < %" G_GSIZE_FORMAT,
1078 frame->map[0].size, info->size);
1079 gst_buffer_unmap (buffer, &frame->map[0]);
1085 * gst_video_frame_map:
1086 * @frame: pointer to #GstVideoFrame
1087 * @info: a #GstVideoInfo
1088 * @buffer: the buffer to map
1089 * @flags: #GstMapFlags
1091 * Use @info and @buffer to fill in the values of @frame.
1093 * All video planes of @buffer will be mapped and the pointers will be set in
1096 * Returns: %TRUE on success.
1099 gst_video_frame_map (GstVideoFrame * frame, GstVideoInfo * info,
1100 GstBuffer * buffer, GstMapFlags flags)
1102 return gst_video_frame_map_id (frame, info, buffer, -1, flags);
1106 * gst_video_frame_unmap:
1107 * @frame: a #GstVideoFrame
1109 * Unmap the memory previously mapped with gst_video_frame_map.
1112 gst_video_frame_unmap (GstVideoFrame * frame)
1118 g_return_if_fail (frame != NULL);
1120 buffer = frame->buffer;
1124 for (i = 0; i < frame->info.finfo->n_planes; i++) {
1125 gst_video_meta_unmap (meta, i, &frame->map[i]);
1128 gst_buffer_unmap (buffer, &frame->map[0]);
1130 gst_buffer_unref (buffer);
1134 * gst_video_frame_copy:
1135 * @dest: a #GstVideoFrame
1136 * @src: a #GstVideoFrame
1139 * Copy the plane with index @plane from @src to @dest.
1141 * Returns: TRUE if the contents could be copied.
1144 gst_video_frame_copy_plane (GstVideoFrame * dest, const GstVideoFrame * src,
1147 const GstVideoInfo *sinfo;
1148 GstVideoInfo *dinfo;
1153 g_return_val_if_fail (dest != NULL, FALSE);
1154 g_return_val_if_fail (src != NULL, FALSE);
1157 dinfo = &dest->info;
1159 g_return_val_if_fail (dinfo->finfo->format == sinfo->finfo->format, FALSE);
1160 g_return_val_if_fail (dinfo->width == sinfo->width
1161 && dinfo->height == sinfo->height, FALSE);
1162 g_return_val_if_fail (dinfo->finfo->n_planes > plane, FALSE);
1164 sp = src->data[plane];
1165 dp = dest->data[plane];
1167 ss = sinfo->stride[plane];
1168 ds = dinfo->stride[plane];
1170 w = MIN (ABS (ss), ABS (ds));
1171 h = GST_VIDEO_FRAME_COMP_HEIGHT (dest, plane);
1173 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy plane %d, w:%d h:%d ", plane, w, h);
1175 for (j = 0; j < h; j++) {
1184 * gst_video_frame_copy:
1185 * @dest: a #GstVideoFrame
1186 * @src: a #GstVideoFrame
1188 * Copy the contents from @src to @dest.
1190 * Returns: TRUE if the contents could be copied.
1193 gst_video_frame_copy (GstVideoFrame * dest, const GstVideoFrame * src)
1196 const GstVideoInfo *sinfo;
1197 GstVideoInfo *dinfo;
1199 g_return_val_if_fail (dest != NULL, FALSE);
1200 g_return_val_if_fail (src != NULL, FALSE);
1203 dinfo = &dest->info;
1205 g_return_val_if_fail (dinfo->finfo->format == sinfo->finfo->format, FALSE);
1206 g_return_val_if_fail (dinfo->width == sinfo->width
1207 && dinfo->height == sinfo->height, FALSE);
1209 n_planes = dinfo->finfo->n_planes;
1211 for (i = 0; i < n_planes; i++)
1212 gst_video_frame_copy_plane (dest, src, i);
1218 fill_planes (GstVideoInfo * info)
1222 width = info->width;
1223 height = info->height;
1225 switch (info->finfo->format) {
1226 case GST_VIDEO_FORMAT_YUY2:
1227 case GST_VIDEO_FORMAT_YVYU:
1228 case GST_VIDEO_FORMAT_UYVY:
1229 info->stride[0] = GST_ROUND_UP_4 (width * 2);
1230 info->offset[0] = 0;
1231 info->size = info->stride[0] * height;
1233 case GST_VIDEO_FORMAT_AYUV:
1234 case GST_VIDEO_FORMAT_RGBx:
1235 case GST_VIDEO_FORMAT_RGBA:
1236 case GST_VIDEO_FORMAT_BGRx:
1237 case GST_VIDEO_FORMAT_BGRA:
1238 case GST_VIDEO_FORMAT_xRGB:
1239 case GST_VIDEO_FORMAT_ARGB:
1240 case GST_VIDEO_FORMAT_xBGR:
1241 case GST_VIDEO_FORMAT_ABGR:
1242 case GST_VIDEO_FORMAT_r210:
1243 info->stride[0] = width * 4;
1244 info->offset[0] = 0;
1245 info->size = info->stride[0] * height;
1247 case GST_VIDEO_FORMAT_RGB16:
1248 case GST_VIDEO_FORMAT_BGR16:
1249 case GST_VIDEO_FORMAT_RGB15:
1250 case GST_VIDEO_FORMAT_BGR15:
1251 info->stride[0] = GST_ROUND_UP_4 (width * 2);
1252 info->offset[0] = 0;
1253 info->size = info->stride[0] * height;
1255 case GST_VIDEO_FORMAT_RGB:
1256 case GST_VIDEO_FORMAT_BGR:
1257 case GST_VIDEO_FORMAT_v308:
1258 info->stride[0] = GST_ROUND_UP_4 (width * 3);
1259 info->offset[0] = 0;
1260 info->size = info->stride[0] * height;
1262 case GST_VIDEO_FORMAT_v210:
1263 info->stride[0] = ((width + 47) / 48) * 128;
1264 info->offset[0] = 0;
1265 info->size = info->stride[0] * height;
1267 case GST_VIDEO_FORMAT_v216:
1268 info->stride[0] = GST_ROUND_UP_8 (width * 4);
1269 info->offset[0] = 0;
1270 info->size = info->stride[0] * height;
1272 case GST_VIDEO_FORMAT_GRAY8:
1273 case GST_VIDEO_FORMAT_Y800:
1274 info->stride[0] = GST_ROUND_UP_4 (width);
1275 info->offset[0] = 0;
1276 info->size = info->stride[0] * height;
1278 case GST_VIDEO_FORMAT_GRAY16_BE:
1279 case GST_VIDEO_FORMAT_GRAY16_LE:
1280 case GST_VIDEO_FORMAT_Y16:
1281 info->stride[0] = GST_ROUND_UP_4 (width * 2);
1282 info->offset[0] = 0;
1283 info->size = info->stride[0] * height;
1285 case GST_VIDEO_FORMAT_UYVP:
1286 info->stride[0] = GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4);
1287 info->offset[0] = 0;
1288 info->size = info->stride[0] * height;
1290 case GST_VIDEO_FORMAT_RGB8_PALETTED:
1291 info->stride[0] = GST_ROUND_UP_4 (width);
1292 info->offset[0] = 0;
1293 info->size = info->stride[0] * height;
1295 case GST_VIDEO_FORMAT_IYU1:
1296 info->stride[0] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) +
1297 GST_ROUND_UP_4 (width) / 2);
1298 info->offset[0] = 0;
1299 info->size = info->stride[0] * height;
1301 case GST_VIDEO_FORMAT_ARGB64:
1302 case GST_VIDEO_FORMAT_AYUV64:
1303 info->stride[0] = width * 8;
1304 info->offset[0] = 0;
1305 info->size = info->stride[0] * height;
1307 case GST_VIDEO_FORMAT_I420:
1308 case GST_VIDEO_FORMAT_YV12: /* same as I420, but plane 1+2 swapped */
1309 info->stride[0] = GST_ROUND_UP_4 (width);
1310 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1311 info->stride[2] = info->stride[1];
1312 info->offset[0] = 0;
1313 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1314 info->offset[2] = info->offset[1] +
1315 info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
1316 info->size = info->offset[2] +
1317 info->stride[2] * (GST_ROUND_UP_2 (height) / 2);
1319 case GST_VIDEO_FORMAT_Y41B:
1320 info->stride[0] = GST_ROUND_UP_4 (width);
1321 info->stride[1] = GST_ROUND_UP_16 (width) / 4;
1322 info->stride[2] = info->stride[1];
1323 info->offset[0] = 0;
1324 info->offset[1] = info->stride[0] * height;
1325 info->offset[2] = info->offset[1] + info->stride[1] * height;
1326 /* simplification of ROUNDUP4(w)*h + 2*((ROUNDUP16(w)/4)*h */
1327 info->size = (info->stride[0] + (GST_ROUND_UP_16 (width) / 2)) * height;
1329 case GST_VIDEO_FORMAT_Y42B:
1330 info->stride[0] = GST_ROUND_UP_4 (width);
1331 info->stride[1] = GST_ROUND_UP_8 (width) / 2;
1332 info->stride[2] = info->stride[1];
1333 info->offset[0] = 0;
1334 info->offset[1] = info->stride[0] * height;
1335 info->offset[2] = info->offset[1] + info->stride[1] * height;
1336 /* simplification of ROUNDUP4(w)*h + 2*(ROUNDUP8(w)/2)*h */
1337 info->size = (info->stride[0] + GST_ROUND_UP_8 (width)) * height;
1339 case GST_VIDEO_FORMAT_Y444:
1340 info->stride[0] = GST_ROUND_UP_4 (width);
1341 info->stride[1] = info->stride[0];
1342 info->stride[2] = info->stride[0];
1343 info->offset[0] = 0;
1344 info->offset[1] = info->stride[0] * height;
1345 info->offset[2] = info->offset[1] * 2;
1346 info->size = info->stride[0] * height * 3;
1348 case GST_VIDEO_FORMAT_NV12:
1349 case GST_VIDEO_FORMAT_NV21:
1350 info->stride[0] = GST_ROUND_UP_4 (width);
1351 info->stride[1] = info->stride[0];
1352 info->offset[0] = 0;
1353 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1354 info->size = info->stride[0] * GST_ROUND_UP_2 (height) * 3 / 2;
1356 case GST_VIDEO_FORMAT_A420:
1357 info->stride[0] = GST_ROUND_UP_4 (width);
1358 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
1359 info->stride[2] = info->stride[1];
1360 info->stride[3] = info->stride[0];
1361 info->offset[0] = 0;
1362 info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
1363 info->offset[2] = info->offset[1] +
1364 info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
1365 info->offset[3] = info->offset[2] +
1366 info->stride[2] * (GST_ROUND_UP_2 (height) / 2);
1367 info->size = info->offset[3] + info->stride[0];
1369 case GST_VIDEO_FORMAT_YUV9:
1370 case GST_VIDEO_FORMAT_YVU9:
1371 info->stride[0] = GST_ROUND_UP_4 (width);
1372 info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
1373 info->stride[2] = info->stride[1];
1374 info->offset[0] = 0;
1375 info->offset[1] = info->stride[0] * height;
1376 info->offset[2] = info->offset[1] +
1377 info->stride[1] * (GST_ROUND_UP_4 (height) / 4);
1378 info->size = info->offset[2] +
1379 info->stride[2] * (GST_ROUND_UP_4 (height) / 4);
1381 case GST_VIDEO_FORMAT_UNKNOWN:
1382 GST_ERROR ("invalid format");
1383 g_warning ("invalid format");
1390 * gst_video_format_convert:
1391 * @info: a #GstVideoInfo
1392 * @src_format: #GstFormat of the @src_value
1393 * @src_value: value to convert
1394 * @dest_format: #GstFormat of the @dest_value
1395 * @dest_value: pointer to destination value
1397 * Converts among various #GstFormat types. This function handles
1398 * GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT. For
1399 * raw video, GST_FORMAT_DEFAULT corresponds to video frames. This
1400 * function can be used to handle pad queries of the type GST_QUERY_CONVERT.
1404 * Returns: TRUE if the conversion was successful.
1407 gst_video_info_convert (GstVideoInfo * info,
1408 GstFormat src_format, gint64 src_value,
1409 GstFormat dest_format, gint64 * dest_value)
1411 gboolean ret = FALSE;
1412 int size, fps_n, fps_d;
1414 g_return_val_if_fail (info != NULL, 0);
1415 g_return_val_if_fail (info->finfo != NULL, 0);
1416 g_return_val_if_fail (info->finfo->format != GST_VIDEO_FORMAT_UNKNOWN, 0);
1417 g_return_val_if_fail (info->size > 0, 0);
1420 fps_n = info->fps_n;
1421 fps_d = info->fps_d;
1423 GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s to %s",
1424 src_value, gst_format_get_name (src_format),
1425 gst_format_get_name (dest_format));
1427 if (src_format == dest_format) {
1428 *dest_value = src_value;
1433 if (src_value == -1) {
1439 /* bytes to frames */
1440 if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_DEFAULT) {
1442 *dest_value = gst_util_uint64_scale_int (src_value, 1, size);
1444 GST_ERROR ("blocksize is 0");
1451 /* frames to bytes */
1452 if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_BYTES) {
1453 *dest_value = gst_util_uint64_scale_int (src_value, size, 1);
1458 /* time to frames */
1459 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_DEFAULT) {
1461 *dest_value = gst_util_uint64_scale (src_value,
1462 fps_n, GST_SECOND * fps_d);
1464 GST_ERROR ("framerate denominator is 0");
1471 /* frames to time */
1472 if (src_format == GST_FORMAT_DEFAULT && dest_format == GST_FORMAT_TIME) {
1474 *dest_value = gst_util_uint64_scale (src_value,
1475 GST_SECOND * fps_d, fps_n);
1477 GST_ERROR ("framerate numerator is 0");
1485 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
1487 *dest_value = gst_util_uint64_scale (src_value,
1488 fps_n * size, GST_SECOND * fps_d);
1490 GST_ERROR ("framerate denominator is 0");
1498 if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME) {
1499 if (fps_n != 0 && size != 0) {
1500 *dest_value = gst_util_uint64_scale (src_value,
1501 GST_SECOND * fps_d, fps_n * size);
1503 GST_ERROR ("framerate denominator and/or blocksize is 0");
1511 GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, ret, *dest_value);
1516 #define GST_VIDEO_EVENT_STILL_STATE_NAME "GstEventStillFrame"
1519 * gst_video_event_new_still_frame:
1520 * @in_still: boolean value for the still-frame state of the event.
1522 * Creates a new Still Frame event. If @in_still is %TRUE, then the event
1523 * represents the start of a still frame sequence. If it is %FALSE, then
1524 * the event ends a still frame sequence.
1526 * To parse an event created by gst_video_event_new_still_frame() use
1527 * gst_video_event_parse_still_frame().
1529 * Returns: The new GstEvent
1533 gst_video_event_new_still_frame (gboolean in_still)
1535 GstEvent *still_event;
1538 s = gst_structure_new (GST_VIDEO_EVENT_STILL_STATE_NAME,
1539 "still-state", G_TYPE_BOOLEAN, in_still, NULL);
1540 still_event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1546 * gst_video_event_parse_still_frame:
1547 * @event: A #GstEvent to parse
1548 * @in_still: A boolean to receive the still-frame status from the event, or NULL
1550 * Parse a #GstEvent, identify if it is a Still Frame event, and
1551 * return the still-frame state from the event if it is.
1552 * If the event represents the start of a still frame, the in_still
1553 * variable will be set to TRUE, otherwise FALSE. It is OK to pass NULL for the
1554 * in_still variable order to just check whether the event is a valid still-frame
1557 * Create a still frame event using gst_video_event_new_still_frame()
1559 * Returns: %TRUE if the event is a valid still-frame event. %FALSE if not
1563 gst_video_event_parse_still_frame (GstEvent * event, gboolean * in_still)
1565 const GstStructure *s;
1566 gboolean ev_still_state;
1568 g_return_val_if_fail (event != NULL, FALSE);
1570 if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM)
1571 return FALSE; /* Not a still frame event */
1573 s = gst_event_get_structure (event);
1575 || !gst_structure_has_name (s, GST_VIDEO_EVENT_STILL_STATE_NAME))
1576 return FALSE; /* Not a still frame event */
1577 if (!gst_structure_get_boolean (s, "still-state", &ev_still_state))
1578 return FALSE; /* Not a still frame event */
1580 *in_still = ev_still_state;
1585 * gst_video_parse_caps_palette:
1586 * @caps: #GstCaps to parse
1588 * Returns the palette data from the caps as a #GstBuffer. For
1589 * #GST_VIDEO_FORMAT_RGB8_PALETTED this is containing 256 #guint32
1590 * values, each containing ARGB colors in native endianness.
1592 * Returns: a #GstBuffer containing the palette data. Unref after usage.
1596 gst_video_parse_caps_palette (GstCaps * caps)
1602 if (!gst_caps_is_fixed (caps))
1605 s = gst_caps_get_structure (caps, 0);
1607 p_v = gst_structure_get_value (s, "palette_data");
1608 if (!p_v || !GST_VALUE_HOLDS_BUFFER (p_v))
1611 p = g_value_dup_boxed (p_v);
1616 #define GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME "GstForceKeyUnit"
1619 * gst_video_event_new_downstream_force_key_unit:
1620 * @timestamp: the timestamp of the buffer that starts a new key unit
1621 * @stream_time: the stream_time of the buffer that starts a new key unit
1622 * @running_time: the running_time of the buffer that starts a new key unit
1623 * @all_headers: %TRUE to produce headers when starting a new key unit
1624 * @count: integer that can be used to number key units
1626 * Creates a new downstream force key unit event. A downstream force key unit
1627 * event can be sent down the pipeline to request downstream elements to produce
1628 * a key unit. A downstream force key unit event must also be sent when handling
1629 * an upstream force key unit event to notify downstream that the latter has been
1632 * To parse an event created by gst_video_event_new_downstream_force_key_unit() use
1633 * gst_video_event_parse_downstream_force_key_unit().
1635 * Returns: The new GstEvent
1639 gst_video_event_new_downstream_force_key_unit (GstClockTime timestamp,
1640 GstClockTime stream_time, GstClockTime running_time, gboolean all_headers,
1643 GstEvent *force_key_unit_event;
1646 s = gst_structure_new (GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME,
1647 "timestamp", G_TYPE_UINT64, timestamp,
1648 "stream-time", G_TYPE_UINT64, stream_time,
1649 "running-time", G_TYPE_UINT64, running_time,
1650 "all-headers", G_TYPE_BOOLEAN, all_headers,
1651 "count", G_TYPE_UINT, count, NULL);
1652 force_key_unit_event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1654 return force_key_unit_event;
1658 * gst_video_event_new_upstream_force_key_unit:
1659 * @running_time: the running_time at which a new key unit should be produced
1660 * @all_headers: %TRUE to produce headers when starting a new key unit
1661 * @count: integer that can be used to number key units
1663 * Creates a new upstream force key unit event. An upstream force key unit event
1664 * can be sent to request upstream elements to produce a key unit.
1666 * @running_time can be set to request a new key unit at a specific
1667 * running_time. If set to GST_CLOCK_TIME_NONE, upstream elements will produce a
1668 * new key unit as soon as possible.
1670 * To parse an event created by gst_video_event_new_downstream_force_key_unit() use
1671 * gst_video_event_parse_downstream_force_key_unit().
1673 * Returns: The new GstEvent
1677 gst_video_event_new_upstream_force_key_unit (GstClockTime running_time,
1678 gboolean all_headers, guint count)
1680 GstEvent *force_key_unit_event;
1683 s = gst_structure_new (GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME,
1684 "running-time", GST_TYPE_CLOCK_TIME, running_time,
1685 "all-headers", G_TYPE_BOOLEAN, all_headers,
1686 "count", G_TYPE_UINT, count, NULL);
1687 force_key_unit_event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, s);
1689 return force_key_unit_event;
1693 * gst_video_event_is_force_key_unit:
1694 * @event: A #GstEvent to check
1696 * Checks if an event is a force key unit event. Returns true for both upstream
1697 * and downstream force key unit events.
1699 * Returns: %TRUE if the event is a valid force key unit event
1703 gst_video_event_is_force_key_unit (GstEvent * event)
1705 const GstStructure *s;
1707 g_return_val_if_fail (event != NULL, FALSE);
1709 if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM &&
1710 GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_UPSTREAM)
1711 return FALSE; /* Not a force key unit event */
1713 s = gst_event_get_structure (event);
1715 || !gst_structure_has_name (s, GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME))
1722 * gst_video_event_parse_downstream_force_key_unit:
1723 * @event: A #GstEvent to parse
1724 * @timestamp: (out): A pointer to the timestamp in the event
1725 * @stream_time: (out): A pointer to the stream-time in the event
1726 * @running_time: (out): A pointer to the running-time in the event
1727 * @all_headers: (out): A pointer to the all_headers flag in the event
1728 * @count: (out): A pointer to the count field of the event
1730 * Get timestamp, stream-time, running-time, all-headers and count in the force
1731 * key unit event. See gst_video_event_new_downstream_force_key_unit() for a
1732 * full description of the downstream force key unit event.
1734 * Returns: %TRUE if the event is a valid downstream force key unit event.
1738 gst_video_event_parse_downstream_force_key_unit (GstEvent * event,
1739 GstClockTime * timestamp, GstClockTime * stream_time,
1740 GstClockTime * running_time, gboolean * all_headers, guint * count)
1742 const GstStructure *s;
1743 GstClockTime ev_timestamp, ev_stream_time, ev_running_time;
1744 gboolean ev_all_headers;
1747 g_return_val_if_fail (event != NULL, FALSE);
1749 if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM)
1750 return FALSE; /* Not a force key unit event */
1752 s = gst_event_get_structure (event);
1754 || !gst_structure_has_name (s, GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME))
1757 if (!gst_structure_get_clock_time (s, "timestamp", &ev_timestamp))
1758 return FALSE; /* Not a force key unit event */
1759 if (!gst_structure_get_clock_time (s, "stream-time", &ev_stream_time))
1760 return FALSE; /* Not a force key unit event */
1761 if (!gst_structure_get_clock_time (s, "running-time", &ev_running_time))
1762 return FALSE; /* Not a force key unit event */
1763 if (!gst_structure_get_boolean (s, "all-headers", &ev_all_headers))
1764 return FALSE; /* Not a force key unit event */
1765 if (!gst_structure_get_uint (s, "count", &ev_count))
1766 return FALSE; /* Not a force key unit event */
1769 *timestamp = ev_timestamp;
1772 *stream_time = ev_stream_time;
1775 *running_time = ev_running_time;
1778 *all_headers = ev_all_headers;
1787 * gst_video_event_parse_upstream_force_key_unit:
1788 * @event: A #GstEvent to parse
1789 * @running_time: (out): A pointer to the running_time in the event
1790 * @all_headers: (out): A pointer to the all_headers flag in the event
1791 * @count: (out): A pointer to the count field in the event
1793 * Get running-time, all-headers and count in the force key unit event. See
1794 * gst_video_event_new_upstream_force_key_unit() for a full description of the
1795 * upstream force key unit event.
1797 * Create an upstream force key unit event using gst_video_event_new_upstream_force_key_unit()
1799 * Returns: %TRUE if the event is a valid upstream force-key-unit event. %FALSE if not
1803 gst_video_event_parse_upstream_force_key_unit (GstEvent * event,
1804 GstClockTime * running_time, gboolean * all_headers, guint * count)
1806 const GstStructure *s;
1807 GstClockTime ev_running_time;
1808 gboolean ev_all_headers;
1811 g_return_val_if_fail (event != NULL, FALSE);
1813 if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_UPSTREAM)
1814 return FALSE; /* Not a force key unit event */
1816 s = gst_event_get_structure (event);
1818 || !gst_structure_has_name (s, GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME))
1821 if (!gst_structure_get_clock_time (s, "running-time", &ev_running_time))
1822 return FALSE; /* Not a force key unit event */
1823 if (!gst_structure_get_boolean (s, "all-headers", &ev_all_headers))
1824 return FALSE; /* Not a force key unit event */
1825 if (!gst_structure_get_uint (s, "count", &ev_count))
1826 return FALSE; /* Not a force key unit event */
1829 *running_time = ev_running_time;
1832 *all_headers = ev_all_headers;