56da373cb3abf01ef72588b8b34893e8f04a7a16
[platform/upstream/gstreamer.git] / gst-libs / gst / video / video.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Library       <2002> Ronald Bultje <rbultje@ronald.bitfreak.net>
4  * Copyright (C) 2007 David A. Schleef <ds@schleef.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25
26 #include <string.h>
27 #include <stdio.h>
28
29 #include "video.h"
30 #include "gstvideometa.h"
31
32 /**
33  * SECTION:gstvideo
34  * @short_description: Support library for video operations
35  *
36  * <refsect2>
37  * <para>
38  * This library contains some helper functions and includes the
39  * videosink and videofilter base classes.
40  * </para>
41  * </refsect2>
42  */
43
44 /**
45  * gst_video_calculate_display_ratio:
46  * @dar_n: Numerator of the calculated display_ratio
47  * @dar_d: Denominator of the calculated display_ratio
48  * @video_width: Width of the video frame in pixels
49  * @video_height: Height of the video frame in pixels
50  * @video_par_n: Numerator of the pixel aspect ratio of the input video.
51  * @video_par_d: Denominator of the pixel aspect ratio of the input video.
52  * @display_par_n: Numerator of the pixel aspect ratio of the display device
53  * @display_par_d: Denominator of the pixel aspect ratio of the display device
54  *
55  * Given the Pixel Aspect Ratio and size of an input video frame, and the
56  * pixel aspect ratio of the intended display device, calculates the actual
57  * display ratio the video will be rendered with.
58  *
59  * Returns: A boolean indicating success and a calculated Display Ratio in the
60  * dar_n and dar_d parameters.
61  * The return value is FALSE in the case of integer overflow or other error.
62  */
63 gboolean
64 gst_video_calculate_display_ratio (guint * dar_n, guint * dar_d,
65     guint video_width, guint video_height,
66     guint video_par_n, guint video_par_d,
67     guint display_par_n, guint display_par_d)
68 {
69   gint num, den;
70   gint tmp_n, tmp_d;
71
72   g_return_val_if_fail (dar_n != NULL, FALSE);
73   g_return_val_if_fail (dar_d != NULL, FALSE);
74
75   /* Calculate (video_width * video_par_n * display_par_d) /
76    * (video_height * video_par_d * display_par_n) */
77   if (!gst_util_fraction_multiply (video_width, video_height, video_par_n,
78           video_par_d, &tmp_n, &tmp_d))
79     goto error_overflow;
80
81   if (!gst_util_fraction_multiply (tmp_n, tmp_d, display_par_d, display_par_n,
82           &num, &den))
83     goto error_overflow;
84
85   g_return_val_if_fail (num > 0, FALSE);
86   g_return_val_if_fail (den > 0, FALSE);
87
88   *dar_n = num;
89   *dar_d = den;
90
91   return TRUE;
92
93   /* ERRORS */
94 error_overflow:
95   {
96     GST_WARNING ("overflow in multiply");
97     return FALSE;
98   }
99 }
100
101 /**
102  * gst_video_alignment_reset:
103  * @align: a #GstVideoAlignment
104  *
105  * Set @align to its default values with no padding and no alignment.
106  */
107 void
108 gst_video_alignment_reset (GstVideoAlignment * align)
109 {
110   gint i;
111
112   g_return_if_fail (align != NULL);
113
114   align->padding_top = 0;
115   align->padding_bottom = 0;
116   align->padding_left = 0;
117   align->padding_right = 0;
118   for (i = 0; i < GST_VIDEO_MAX_PLANES; i++)
119     align->stride_align[i] = 0;
120 }