Makefile: Merge dhahelperwinclean and dhahelperclean targets
[mplayer.git] / libmpdemux / yuv4mpeg_ratio.c
blobbe5475c1b10231cf3928c931a064ddebe8c46589
1 /*
2 * yuv4mpeg_ratio.c: Functions for dealing with y4m_ratio_t datatype.
4 * Copyright (C) 2001 Matthew J. Marjanovic <maddog@mir.com>
6 * This file is part of the MJPEG Tools package (mjpeg.sourceforge.net).
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "config.h"
26 #include <string.h>
27 #include "yuv4mpeg.h"
28 #include "yuv4mpeg_intern.h"
31 /* useful list of standard framerates */
32 const y4m_ratio_t y4m_fps_UNKNOWN = Y4M_FPS_UNKNOWN;
33 const y4m_ratio_t y4m_fps_NTSC_FILM = Y4M_FPS_NTSC_FILM;
34 const y4m_ratio_t y4m_fps_FILM = Y4M_FPS_FILM;
35 const y4m_ratio_t y4m_fps_PAL = Y4M_FPS_PAL;
36 const y4m_ratio_t y4m_fps_NTSC = Y4M_FPS_NTSC;
37 const y4m_ratio_t y4m_fps_30 = Y4M_FPS_30;
38 const y4m_ratio_t y4m_fps_PAL_FIELD = Y4M_FPS_PAL_FIELD;
39 const y4m_ratio_t y4m_fps_NTSC_FIELD = Y4M_FPS_NTSC_FIELD;
40 const y4m_ratio_t y4m_fps_60 = Y4M_FPS_60;
42 /* useful list of standard pixel aspect ratios */
43 const y4m_ratio_t y4m_sar_UNKNOWN = Y4M_SAR_UNKNOWN;
44 const y4m_ratio_t y4m_sar_SQUARE = Y4M_SAR_SQUARE;
45 const y4m_ratio_t y4m_sar_NTSC_CCIR601 = Y4M_SAR_NTSC_CCIR601;
46 const y4m_ratio_t y4m_sar_NTSC_16_9 = Y4M_SAR_NTSC_16_9;
47 const y4m_ratio_t y4m_sar_NTSC_SVCD_4_3 = Y4M_SAR_NTSC_SVCD_4_3;
48 const y4m_ratio_t y4m_sar_NTSC_SVCD_16_9 = Y4M_SAR_NTSC_SVCD_16_9;
49 const y4m_ratio_t y4m_sar_PAL_CCIR601 = Y4M_SAR_PAL_CCIR601;
50 const y4m_ratio_t y4m_sar_PAL_16_9 = Y4M_SAR_PAL_16_9;
51 const y4m_ratio_t y4m_sar_PAL_SVCD_4_3 = Y4M_SAR_PAL_SVCD_4_3;
52 const y4m_ratio_t y4m_sar_PAL_SVCD_16_9 = Y4M_SAR_PAL_SVCD_16_9;
56 * Euler's algorithm for greatest common divisor
59 static int gcd(int a, int b)
61 a = (a >= 0) ? a : -a;
62 b = (b >= 0) ? b : -b;
64 while (b > 0) {
65 int x = b;
66 b = a % b;
67 a = x;
69 return a;
73 /*************************************************************************
75 * Remove common factors from a ratio
77 *************************************************************************/
80 void y4m_ratio_reduce(y4m_ratio_t *r)
82 int d;
83 if ((r->n == 0) && (r->d == 0)) return; /* "unknown" */
84 d = gcd(r->n, r->d);
85 r->n /= d;
86 r->d /= d;
91 /*************************************************************************
93 * Parse "nnn:ddd" into a ratio
95 * returns: Y4M_OK - success
96 * Y4M_ERR_RANGE - range error
98 *************************************************************************/
100 int y4m_parse_ratio(y4m_ratio_t *r, const char *s)
102 char *t = strchr(s, ':');
104 if (t == NULL) return Y4M_ERR_RANGE;
105 r->n = atoi(s);
106 r->d = atoi(t+1);
107 if (r->d < 0) return Y4M_ERR_RANGE;
108 /* 0:0 == unknown, so that is ok, otherwise zero denominator is bad */
109 if ((r->d == 0) && (r->n != 0)) return Y4M_ERR_RANGE;
110 y4m_ratio_reduce(r);
111 return Y4M_OK;