Statistics
| Branch: | Revision:

ffmpeg / cmdutils.c @ 61dbf4e5

History | View | Annotate | Download (7.65 KB)

1
/*
2
 * Various utilities for command line tools
3
 * Copyright (c) 2000-2003 Fabrice Bellard
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
#include <string.h>
23
#include <stdlib.h>
24
#include <errno.h>
25

    
26
#include "avformat.h"
27
#include "avdevice.h"
28
#include "cmdutils.h"
29
#include "avstring.h"
30
#include "version.h"
31
#include "config.h"
32

    
33
#undef exit
34

    
35
void show_help_options(const OptionDef *options, const char *msg, int mask, int value)
36
{
37
    const OptionDef *po;
38
    int first;
39

    
40
    first = 1;
41
    for(po = options; po->name != NULL; po++) {
42
        char buf[64];
43
        if ((po->flags & mask) == value) {
44
            if (first) {
45
                printf("%s", msg);
46
                first = 0;
47
            }
48
            av_strlcpy(buf, po->name, sizeof(buf));
49
            if (po->flags & HAS_ARG) {
50
                av_strlcat(buf, " ", sizeof(buf));
51
                av_strlcat(buf, po->argname, sizeof(buf));
52
            }
53
            printf("-%-17s  %s\n", buf, po->help);
54
        }
55
    }
56
}
57

    
58
static const OptionDef* find_option(const OptionDef *po, const char *name){
59
    while (po->name != NULL) {
60
        if (!strcmp(name, po->name))
61
            break;
62
        po++;
63
    }
64
    return po;
65
}
66

    
67
void parse_options(int argc, char **argv, const OptionDef *options,
68
                   void (* parse_arg_function)(const char*))
69
{
70
    const char *opt, *arg;
71
    int optindex, handleoptions=1;
72
    const OptionDef *po;
73

    
74
    /* parse options */
75
    optindex = 1;
76
    while (optindex < argc) {
77
        opt = argv[optindex++];
78

    
79
        if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
80
          if (opt[1] == '-' && opt[2] == '\0') {
81
            handleoptions = 0;
82
            continue;
83
          }
84
            po= find_option(options, opt + 1);
85
            if (!po->name)
86
                po= find_option(options, "default");
87
            if (!po->name) {
88
unknown_opt:
89
                fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
90
                exit(1);
91
            }
92
            arg = NULL;
93
            if (po->flags & HAS_ARG) {
94
                arg = argv[optindex++];
95
                if (!arg) {
96
                    fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
97
                    exit(1);
98
                }
99
            }
100
            if (po->flags & OPT_STRING) {
101
                char *str;
102
                str = av_strdup(arg);
103
                *po->u.str_arg = str;
104
            } else if (po->flags & OPT_BOOL) {
105
                *po->u.int_arg = 1;
106
            } else if (po->flags & OPT_INT) {
107
                *po->u.int_arg = atoi(arg);
108
            } else if (po->flags & OPT_INT64) {
109
                *po->u.int64_arg = strtoll(arg, (char **)NULL, 10);
110
            } else if (po->flags & OPT_FLOAT) {
111
                *po->u.float_arg = atof(arg);
112
            } else if (po->flags & OPT_FUNC2) {
113
                if(po->u.func2_arg(opt+1, arg)<0)
114
                    goto unknown_opt;
115
            } else {
116
                po->u.func_arg(arg);
117
            }
118
        } else {
119
            if (parse_arg_function)
120
                parse_arg_function(opt);
121
        }
122
    }
123
}
124

    
125
void print_error(const char *filename, int err)
126
{
127
    switch(err) {
128
    case AVERROR_NUMEXPECTED:
129
        fprintf(stderr, "%s: Incorrect image filename syntax.\n"
130
                "Use '%%d' to specify the image number:\n"
131
                "  for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
132
                "  for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
133
                filename);
134
        break;
135
    case AVERROR_INVALIDDATA:
136
        fprintf(stderr, "%s: Error while parsing header\n", filename);
137
        break;
138
    case AVERROR_NOFMT:
139
        fprintf(stderr, "%s: Unknown format\n", filename);
140
        break;
141
    case AVERROR(EIO):
142
        fprintf(stderr, "%s: I/O error occured\n"
143
                "Usually that means that input file is truncated and/or corrupted.\n",
144
                filename);
145
        break;
146
    case AVERROR(ENOMEM):
147
        fprintf(stderr, "%s: memory allocation error occured\n", filename);
148
        break;
149
    case AVERROR(ENOENT):
150
        fprintf(stderr, "%s: no such file or directory\n", filename);
151
        break;
152
    default:
153
        fprintf(stderr, "%s: Error while opening file\n", filename);
154
        break;
155
    }
156
}
157

    
158
void show_banner(const char *program_name, int program_birth_year)
159
{
160
    fprintf(stderr, "%s version " FFMPEG_VERSION ", Copyright (c) %d-2008 Fabrice Bellard, et al.\n",
161
            program_name, program_birth_year);
162
    fprintf(stderr, "  configuration: " FFMPEG_CONFIGURATION "\n");
163
    fprintf(stderr, "  libavutil version: " AV_STRINGIFY(LIBAVUTIL_VERSION) "\n");
164
    fprintf(stderr, "  libavcodec version: " AV_STRINGIFY(LIBAVCODEC_VERSION) "\n");
165
    fprintf(stderr, "  libavformat version: " AV_STRINGIFY(LIBAVFORMAT_VERSION) "\n");
166
    fprintf(stderr, "  libavdevice version: " AV_STRINGIFY(LIBAVDEVICE_VERSION) "\n");
167
    fprintf(stderr, "  built on " __DATE__ " " __TIME__);
168
#ifdef __GNUC__
169
    fprintf(stderr, ", gcc: " __VERSION__ "\n");
170
#else
171
    fprintf(stderr, ", using a non-gcc compiler\n");
172
#endif
173
}
174

    
175
void show_version(const char *program_name) {
176
     /* TODO: add function interface to avutil and avformat avdevice*/
177
    printf("%s " FFMPEG_VERSION "\n", program_name);
178
    printf("libavutil   %d\n"
179
           "libavcodec  %d\n"
180
           "libavformat %d\n"
181
           "libavdevice %d\n",
182
           LIBAVUTIL_BUILD, avcodec_build(), LIBAVFORMAT_BUILD, LIBAVDEVICE_BUILD);
183
}
184

    
185
void show_license(void)
186
{
187
#ifdef CONFIG_GPL
188
    printf(
189
    "FFmpeg is free software; you can redistribute it and/or modify\n"
190
    "it under the terms of the GNU General Public License as published by\n"
191
    "the Free Software Foundation; either version 2 of the License, or\n"
192
    "(at your option) any later version.\n"
193
    "\n"
194
    "FFmpeg is distributed in the hope that it will be useful,\n"
195
    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
196
    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
197
    "GNU General Public License for more details.\n"
198
    "\n"
199
    "You should have received a copy of the GNU General Public License\n"
200
    "along with FFmpeg; if not, write to the Free Software\n"
201
    "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n"
202
    );
203
#else
204
    printf(
205
    "FFmpeg is free software; you can redistribute it and/or\n"
206
    "modify it under the terms of the GNU Lesser General Public\n"
207
    "License as published by the Free Software Foundation; either\n"
208
    "version 2.1 of the License, or (at your option) any later version.\n"
209
    "\n"
210
    "FFmpeg is distributed in the hope that it will be useful,\n"
211
    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
212
    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
213
    "Lesser General Public License for more details.\n"
214
    "\n"
215
    "You should have received a copy of the GNU Lesser General Public\n"
216
    "License along with FFmpeg; if not, write to the Free Software\n"
217
    "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n"
218
    );
219
#endif
220
}