9a87d07e98bbe7ddb2ac6b3aa1f69aab9527143d
[libav.git] / libavcore / parseutils.c
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /**
20 * @file
21 * misc parsing utilities for libavcore
22 */
23
24 #include "parseutils.h"
25 #include "libavutil/avutil.h"
26
27 typedef struct {
28 const char *abbr;
29 int width, height;
30 } VideoSizeAbbr;
31
32 typedef struct {
33 const char *abbr;
34 AVRational rate;
35 } VideoRateAbbr;
36
37 static const VideoSizeAbbr video_size_abbrs[] = {
38 { "ntsc", 720, 480 },
39 { "pal", 720, 576 },
40 { "qntsc", 352, 240 }, /* VCD compliant NTSC */
41 { "qpal", 352, 288 }, /* VCD compliant PAL */
42 { "sntsc", 640, 480 }, /* square pixel NTSC */
43 { "spal", 768, 576 }, /* square pixel PAL */
44 { "film", 352, 240 },
45 { "ntsc-film", 352, 240 },
46 { "sqcif", 128, 96 },
47 { "qcif", 176, 144 },
48 { "cif", 352, 288 },
49 { "4cif", 704, 576 },
50 { "16cif", 1408,1152 },
51 { "qqvga", 160, 120 },
52 { "qvga", 320, 240 },
53 { "vga", 640, 480 },
54 { "svga", 800, 600 },
55 { "xga", 1024, 768 },
56 { "uxga", 1600,1200 },
57 { "qxga", 2048,1536 },
58 { "sxga", 1280,1024 },
59 { "qsxga", 2560,2048 },
60 { "hsxga", 5120,4096 },
61 { "wvga", 852, 480 },
62 { "wxga", 1366, 768 },
63 { "wsxga", 1600,1024 },
64 { "wuxga", 1920,1200 },
65 { "woxga", 2560,1600 },
66 { "wqsxga", 3200,2048 },
67 { "wquxga", 3840,2400 },
68 { "whsxga", 6400,4096 },
69 { "whuxga", 7680,4800 },
70 { "cga", 320, 200 },
71 { "ega", 640, 350 },
72 { "hd480", 852, 480 },
73 { "hd720", 1280, 720 },
74 { "hd1080", 1920,1080 },
75 };
76
77 static const VideoRateAbbr video_rate_abbrs[]= {
78 { "ntsc", { 30000, 1001 } },
79 { "pal", { 25, 1 } },
80 { "qntsc", { 30000, 1001 } }, /* VCD compliant NTSC */
81 { "qpal", { 25, 1 } }, /* VCD compliant PAL */
82 { "sntsc", { 30000, 1001 } }, /* square pixel NTSC */
83 { "spal", { 25, 1 } }, /* square pixel PAL */
84 { "film", { 24, 1 } },
85 { "ntsc-film", { 24000, 1001 } },
86 };
87
88 int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str)
89 {
90 int i;
91 int n = FF_ARRAY_ELEMS(video_size_abbrs);
92 char *p;
93 int width = 0, height = 0;
94
95 for (i = 0; i < n; i++) {
96 if (!strcmp(video_size_abbrs[i].abbr, str)) {
97 width = video_size_abbrs[i].width;
98 height = video_size_abbrs[i].height;
99 break;
100 }
101 }
102 if (i == n) {
103 p = str;
104 width = strtol(p, &p, 10);
105 if (*p)
106 p++;
107 height = strtol(p, &p, 10);
108 }
109 if (width <= 0 || height <= 0)
110 return AVERROR(EINVAL);
111 *width_ptr = width;
112 *height_ptr = height;
113 return 0;
114 }
115
116 int av_parse_video_rate(AVRational *rate, const char *arg)
117 {
118 int i;
119 int n = FF_ARRAY_ELEMS(video_rate_abbrs);
120 char *cp;
121
122 /* First, we check our abbreviation table */
123 for (i = 0; i < n; ++i)
124 if (!strcmp(video_rate_abbrs[i].abbr, arg)) {
125 *rate = video_rate_abbrs[i].rate;
126 return 0;
127 }
128
129 /* Then, we try to parse it as fraction */
130 cp = strchr(arg, '/');
131 if (!cp)
132 cp = strchr(arg, ':');
133 if (cp) {
134 char *cpp;
135 rate->num = strtol(arg, &cpp, 10);
136 if (cpp != arg || cpp == cp)
137 rate->den = strtol(cp+1, &cpp, 10);
138 else
139 rate->num = 0;
140 } else {
141 /* Finally we give up and parse it as double */
142 AVRational time_base = av_d2q(strtod(arg, 0), 1001000);
143 rate->den = time_base.den;
144 rate->num = time_base.num;
145 }
146 if (rate->num <= 0 || rate->den <= 0)
147 return AVERROR(EINVAL);
148 return 0;
149 }