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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
30 #include "gstvideometa.h"
35 * @short_description: Support library for video operations
37 * This library contains some helper functions and includes the
38 * videosink and videofilter base classes.
42 * gst_video_calculate_display_ratio:
43 * @dar_n: (out): Numerator of the calculated display_ratio
44 * @dar_d: (out): Denominator of the calculated display_ratio
45 * @video_width: Width of the video frame in pixels
46 * @video_height: Height of the video frame in pixels
47 * @video_par_n: Numerator of the pixel aspect ratio of the input video.
48 * @video_par_d: Denominator of the pixel aspect ratio of the input video.
49 * @display_par_n: Numerator of the pixel aspect ratio of the display device
50 * @display_par_d: Denominator of the pixel aspect ratio of the display device
52 * Given the Pixel Aspect Ratio and size of an input video frame, and the
53 * pixel aspect ratio of the intended display device, calculates the actual
54 * display ratio the video will be rendered with.
56 * Returns: A boolean indicating success and a calculated Display Ratio in the
57 * dar_n and dar_d parameters.
58 * The return value is FALSE in the case of integer overflow or other error.
61 gst_video_calculate_display_ratio (guint * dar_n, guint * dar_d,
62 guint video_width, guint video_height,
63 guint video_par_n, guint video_par_d,
64 guint display_par_n, guint display_par_d)
69 g_return_val_if_fail (dar_n != NULL, FALSE);
70 g_return_val_if_fail (dar_d != NULL, FALSE);
72 /* Calculate (video_width * video_par_n * display_par_d) /
73 * (video_height * video_par_d * display_par_n) */
74 if (!gst_util_fraction_multiply (video_width, video_height, video_par_n,
75 video_par_d, &tmp_n, &tmp_d))
78 if (!gst_util_fraction_multiply (tmp_n, tmp_d, display_par_d, display_par_n,
82 g_return_val_if_fail (num > 0, FALSE);
83 g_return_val_if_fail (den > 0, FALSE);
93 GST_WARNING ("overflow in multiply");
99 * gst_video_guess_framerate:
100 * @duration: Nominal duration of one frame
101 * @dest_n: (out) (allow-none): Numerator of the calculated framerate
102 * @dest_d: (out) (allow-none): Denominator of the calculated framerate
104 * Given the nominal duration of one video frame,
105 * this function will check some standard framerates for
106 * a close match (within 0.1%) and return one if possible,
108 * It will calculate an arbitrary framerate if no close
109 * match was found, and return %FALSE.
111 * It returns %FALSE if a duration of 0 is passed.
113 * Returns: %TRUE if a close "standard" framerate was
114 * recognised, and %FALSE otherwise.
119 gst_video_guess_framerate (GstClockTime duration, gint * dest_n, gint * dest_d)
121 const int common_den[] = { 1, 2, 3, 4, 1001 };
122 int best_n, best_d, gcd;
123 guint64 best_error = G_MAXUINT64;
127 if (G_UNLIKELY (duration == 0))
130 /* Use a limited precision conversion by default for more sensible results,
131 * unless the frame duration is absurdly small (high speed cameras?) */
132 if (duration > 100000) {
133 best_n = GST_SECOND / 10000;
134 best_d = duration / 10000;
140 for (i = 0; i < G_N_ELEMENTS (common_den); i++) {
141 gint d = common_den[i];
142 gint n = gst_util_uint64_scale_round (d, GST_SECOND, duration);
144 /* For NTSC framerates, round to the nearest 1000 fps */
151 /* See what duration the given framerate should be */
152 a = gst_util_uint64_scale_int (GST_SECOND, d, n);
153 /* Compute absolute error */
154 a = (a < duration) ? (duration - a) : (a - duration);
156 /* Really precise - take this option */
163 /* If within 0.1%, remember this denominator */
164 if (a * 1000 < duration && a < best_error) {
173 gcd = gst_util_greatest_common_divisor (best_n, best_d);
183 return (best_error != G_MAXUINT64);
188 * gst_video_alignment_reset:
189 * @align: a #GstVideoAlignment
191 * Set @align to its default values with no padding and no alignment.
194 gst_video_alignment_reset (GstVideoAlignment * align)
198 g_return_if_fail (align != NULL);
200 align->padding_top = 0;
201 align->padding_bottom = 0;
202 align->padding_left = 0;
203 align->padding_right = 0;
204 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++)
205 align->stride_align[i] = 0;
208 static const GEnumValue video_orientation_methods[] = {
209 {GST_VIDEO_ORIENTATION_IDENTITY, "Identity (no rotation)", "identity"},
210 {GST_VIDEO_ORIENTATION_90R, "Rotate clockwise 90 degrees", "90r"},
211 {GST_VIDEO_ORIENTATION_180, "Rotate 180 degrees", "180"},
212 {GST_VIDEO_ORIENTATION_90L, "Rotate counter-clockwise 90 degrees",
214 {GST_VIDEO_ORIENTATION_HORIZ, "Flip horizontally", "horiz"},
215 {GST_VIDEO_ORIENTATION_VERT, "Flip vertically", "vert"},
216 {GST_VIDEO_ORIENTATION_UL_LR,
217 "Flip across upper left/lower right diagonal", "ul-lr"},
218 {GST_VIDEO_ORIENTATION_UR_LL,
219 "Flip across upper right/lower left diagonal", "ur-ll"},
220 {GST_VIDEO_ORIENTATION_AUTO,
221 "Select rotate method based on image-orientation tag", "auto"},
222 {GST_VIDEO_ORIENTATION_CUSTOM,
223 "Current status depends on plugin internal setup", "custom"},
228 gst_video_orientation_method_get_type (void)
230 static gsize video_orientation_method_type = 0;
232 if (g_once_init_enter (&video_orientation_method_type)) {
233 gsize res = g_enum_register_static ("GstVideoOrientationMethod",
234 video_orientation_methods);
235 g_once_init_leave (&video_orientation_method_type, res);
238 return (GType) video_orientation_method_type;