]> git.sesse.net Git - ffmpeg/blob - libavcore/parseutils.c
Deprecate av_parse_video_frame_size() and av_parse_video_frame_rate()
[ffmpeg] / 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 } VideoFrameSizeAbbr;
31
32 typedef struct {
33     const char *abbr;
34     int rate_num, rate_den;
35 } VideoFrameRateAbbr;
36
37 static const VideoFrameSizeAbbr video_frame_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 VideoFrameRateAbbr video_frame_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_frame_size_abbrs);
92     char *p;
93     int frame_width = 0, frame_height = 0;
94
95     for (i = 0; i < n; i++) {
96         if (!strcmp(video_frame_size_abbrs[i].abbr, str)) {
97             frame_width = video_frame_size_abbrs[i].width;
98             frame_height = video_frame_size_abbrs[i].height;
99             break;
100         }
101     }
102     if (i == n) {
103         p = str;
104         frame_width = strtol(p, &p, 10);
105         if (*p)
106             p++;
107         frame_height = strtol(p, &p, 10);
108     }
109     if (frame_width <= 0 || frame_height <= 0)
110         return -1;
111     *width_ptr  = frame_width;
112     *height_ptr = frame_height;
113     return 0;
114 }
115
116 int av_parse_video_rate(AVRational *frame_rate, const char *arg)
117 {
118     int i;
119     int n = FF_ARRAY_ELEMS(video_frame_rate_abbrs);
120     char *cp;
121
122     /* First, we check our abbreviation table */
123     for (i = 0; i < n; ++i)
124          if (!strcmp(video_frame_rate_abbrs[i].abbr, arg)) {
125              frame_rate->num = video_frame_rate_abbrs[i].rate_num;
126              frame_rate->den = video_frame_rate_abbrs[i].rate_den;
127              return 0;
128          }
129
130     /* Then, we try to parse it as fraction */
131     cp = strchr(arg, '/');
132     if (!cp)
133         cp = strchr(arg, ':');
134     if (cp) {
135         char *cpp;
136         frame_rate->num = strtol(arg, &cpp, 10);
137         if (cpp != arg || cpp == cp)
138             frame_rate->den = strtol(cp+1, &cpp, 10);
139         else
140             frame_rate->num = 0;
141     } else {
142         /* Finally we give up and parse it as double */
143         AVRational time_base = av_d2q(strtod(arg, 0), 1001000);
144         frame_rate->den = time_base.den;
145         frame_rate->num = time_base.num;
146     }
147     if (frame_rate->num <= 0 || frame_rate->den <= 0)
148         return -1;
149     return 0;
150 }