Statistics
| Branch: | Revision:

ffmpeg / cmdutils.c @ 61dbf4e5

History | View | Annotate | Download (7.65 KB)

1 01310af2 Fabrice Bellard
/*
2
 * Various utilities for command line tools
3
 * Copyright (c) 2000-2003 Fabrice Bellard
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 01310af2 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 01310af2 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 01310af2 Fabrice Bellard
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 01310af2 Fabrice Bellard
 */
21 364a9607 Diego Biurrun
22 0f4e8165 Ronald S. Bultje
#include <string.h>
23
#include <stdlib.h>
24
#include <errno.h>
25
26 01310af2 Fabrice Bellard
#include "avformat.h"
27 2208741a Michael Niedermayer
#include "avdevice.h"
28 01310af2 Fabrice Bellard
#include "cmdutils.h"
29 f7d78f36 Måns Rullgård
#include "avstring.h"
30 86074ed1 Stefano Sabatini
#include "version.h"
31 b6cd8593 Ronald S. Bultje
#include "config.h"
32 01310af2 Fabrice Bellard
33 c367d067 Michael Niedermayer
#undef exit
34
35 02d504a7 Fabrice Bellard
void show_help_options(const OptionDef *options, const char *msg, int mask, int value)
36 01310af2 Fabrice Bellard
{
37
    const OptionDef *po;
38 02d504a7 Fabrice Bellard
    int first;
39 01310af2 Fabrice Bellard
40 02d504a7 Fabrice Bellard
    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 f7d78f36 Måns Rullgård
            av_strlcpy(buf, po->name, sizeof(buf));
49 02d504a7 Fabrice Bellard
            if (po->flags & HAS_ARG) {
50 f7d78f36 Måns Rullgård
                av_strlcat(buf, " ", sizeof(buf));
51
                av_strlcat(buf, po->argname, sizeof(buf));
52 01310af2 Fabrice Bellard
            }
53 02d504a7 Fabrice Bellard
            printf("-%-17s  %s\n", buf, po->help);
54 01310af2 Fabrice Bellard
        }
55
    }
56
}
57
58 fccfc475 Måns Rullgård
static const OptionDef* find_option(const OptionDef *po, const char *name){
59 8bbf6db9 Michael Niedermayer
    while (po->name != NULL) {
60
        if (!strcmp(name, po->name))
61
            break;
62
        po++;
63
    }
64
    return po;
65
}
66
67 60a9966e Stefano Sabatini
void parse_options(int argc, char **argv, const OptionDef *options,
68
                   void (* parse_arg_function)(const char*))
69 01310af2 Fabrice Bellard
{
70
    const char *opt, *arg;
71 b0d7bc1e Loren Merritt
    int optindex, handleoptions=1;
72 01310af2 Fabrice Bellard
    const OptionDef *po;
73
74
    /* parse options */
75
    optindex = 1;
76
    while (optindex < argc) {
77
        opt = argv[optindex++];
78 115329f1 Diego Biurrun
79 84bf226b Timo Lindfors
        if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
80
          if (opt[1] == '-' && opt[2] == '\0') {
81
            handleoptions = 0;
82
            continue;
83
          }
84 8bbf6db9 Michael Niedermayer
            po= find_option(options, opt + 1);
85
            if (!po->name)
86
                po= find_option(options, "default");
87 01310af2 Fabrice Bellard
            if (!po->name) {
88 8bbf6db9 Michael Niedermayer
unknown_opt:
89 01310af2 Fabrice Bellard
                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 02d504a7 Fabrice Bellard
                str = av_strdup(arg);
103 01310af2 Fabrice Bellard
                *po->u.str_arg = str;
104
            } else if (po->flags & OPT_BOOL) {
105
                *po->u.int_arg = 1;
106 26d4f26b Michael Niedermayer
            } else if (po->flags & OPT_INT) {
107
                *po->u.int_arg = atoi(arg);
108 ffdf9a1f Baptiste Coudurier
            } else if (po->flags & OPT_INT64) {
109 946d3b12 Patrice Bensoussan
                *po->u.int64_arg = strtoll(arg, (char **)NULL, 10);
110 1f631450 Michael Niedermayer
            } else if (po->flags & OPT_FLOAT) {
111
                *po->u.float_arg = atof(arg);
112 8bbf6db9 Michael Niedermayer
            } else if (po->flags & OPT_FUNC2) {
113
                if(po->u.func2_arg(opt+1, arg)<0)
114
                    goto unknown_opt;
115 01310af2 Fabrice Bellard
            } else {
116 bb270c08 Diego Biurrun
                po->u.func_arg(arg);
117 01310af2 Fabrice Bellard
            }
118
        } else {
119 60a9966e Stefano Sabatini
            if (parse_arg_function)
120
                parse_arg_function(opt);
121 01310af2 Fabrice Bellard
        }
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 115329f1 Diego Biurrun
                "  for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
133 01310af2 Fabrice Bellard
                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 6f3e0b21 Panagiotis Issaris
    case AVERROR(EIO):
142 45ce5ddb Kostya Shishkov
        fprintf(stderr, "%s: I/O error occured\n"
143 bb270c08 Diego Biurrun
                "Usually that means that input file is truncated and/or corrupted.\n",
144
                filename);
145 45ce5ddb Kostya Shishkov
        break;
146 769e10f0 Panagiotis Issaris
    case AVERROR(ENOMEM):
147 45ce5ddb Kostya Shishkov
        fprintf(stderr, "%s: memory allocation error occured\n", filename);
148
        break;
149 24fddf48 Panagiotis Issaris
    case AVERROR(ENOENT):
150 0ba0c8de Benoit Fouet
        fprintf(stderr, "%s: no such file or directory\n", filename);
151
        break;
152 01310af2 Fabrice Bellard
    default:
153
        fprintf(stderr, "%s: Error while opening file\n", filename);
154
        break;
155
    }
156
}
157 f35917b2 Stefano Sabatini
158 86074ed1 Stefano Sabatini
void show_banner(const char *program_name, int program_birth_year)
159
{
160 69c12fbb Benoit Fouet
    fprintf(stderr, "%s version " FFMPEG_VERSION ", Copyright (c) %d-2008 Fabrice Bellard, et al.\n",
161 86074ed1 Stefano Sabatini
            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 2208741a Michael Niedermayer
    fprintf(stderr, "  libavdevice version: " AV_STRINGIFY(LIBAVDEVICE_VERSION) "\n");
167 86074ed1 Stefano Sabatini
    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 2208741a Michael Niedermayer
     /* TODO: add function interface to avutil and avformat avdevice*/
177 86074ed1 Stefano Sabatini
    printf("%s " FFMPEG_VERSION "\n", program_name);
178
    printf("libavutil   %d\n"
179
           "libavcodec  %d\n"
180 2208741a Michael Niedermayer
           "libavformat %d\n"
181
           "libavdevice %d\n",
182
           LIBAVUTIL_BUILD, avcodec_build(), LIBAVFORMAT_BUILD, LIBAVDEVICE_BUILD);
183 86074ed1 Stefano Sabatini
}
184
185 f35917b2 Stefano Sabatini
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
}