Statistics
| Branch: | Revision:

ffmpeg / cmdutils.c @ 584c2f1d

History | View | Annotate | Download (28.2 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
#include <math.h>
26

    
27
/* Include only the enabled headers since some compilers (namely, Sun
28
   Studio) will not omit unused inline functions and create undefined
29
   references to libraries that are not being built. */
30

    
31
#include "config.h"
32
#include "libavformat/avformat.h"
33
#include "libavfilter/avfilter.h"
34
#include "libavdevice/avdevice.h"
35
#include "libswscale/swscale.h"
36
#include "libpostproc/postprocess.h"
37
#include "libavutil/avstring.h"
38
#include "libavutil/parseutils.h"
39
#include "libavutil/pixdesc.h"
40
#include "libavutil/eval.h"
41
#include "libavcodec/opt.h"
42
#include "cmdutils.h"
43
#include "version.h"
44
#if CONFIG_NETWORK
45
#include "libavformat/network.h"
46
#endif
47
#if HAVE_SYS_RESOURCE_H
48
#include <sys/resource.h>
49
#endif
50

    
51
const char **opt_names;
52
const char **opt_values;
53
static int opt_name_count;
54
AVCodecContext *avcodec_opts[AVMEDIA_TYPE_NB];
55
AVFormatContext *avformat_opts;
56
struct SwsContext *sws_opts;
57

    
58
static const int this_year = 2011;
59

    
60
void init_opts(void)
61
{
62
    int i;
63
    for (i = 0; i < AVMEDIA_TYPE_NB; i++)
64
        avcodec_opts[i] = avcodec_alloc_context2(i);
65
    avformat_opts = avformat_alloc_context();
66
#if CONFIG_SWSCALE
67
    sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC, NULL, NULL, NULL);
68
#endif
69
}
70

    
71
void uninit_opts(void)
72
{
73
    int i;
74
    for (i = 0; i < AVMEDIA_TYPE_NB; i++)
75
        av_freep(&avcodec_opts[i]);
76
    av_freep(&avformat_opts->key);
77
    av_freep(&avformat_opts);
78
#if CONFIG_SWSCALE
79
    av_freep(&sws_opts);
80
#endif
81
    for (i = 0; i < opt_name_count; i++) {
82
        //opt_values are only stored for codec-specific options in which case
83
        //both the name and value are dup'd
84
        if (opt_values[i]) {
85
            av_freep(&opt_names[i]);
86
            av_freep(&opt_values[i]);
87
        }
88
    }
89
    av_freep(&opt_names);
90
    av_freep(&opt_values);
91
}
92

    
93
void log_callback_help(void* ptr, int level, const char* fmt, va_list vl)
94
{
95
    vfprintf(stdout, fmt, vl);
96
}
97

    
98
double parse_number_or_die(const char *context, const char *numstr, int type, double min, double max)
99
{
100
    char *tail;
101
    const char *error;
102
    double d = av_strtod(numstr, &tail);
103
    if (*tail)
104
        error= "Expected number for %s but found: %s\n";
105
    else if (d < min || d > max)
106
        error= "The value for %s was %s which is not within %f - %f\n";
107
    else if(type == OPT_INT64 && (int64_t)d != d)
108
        error= "Expected int64 for %s but found %s\n";
109
    else if (type == OPT_INT && (int)d != d)
110
        error= "Expected int for %s but found %s\n";
111
    else
112
        return d;
113
    fprintf(stderr, error, context, numstr, min, max);
114
    exit(1);
115
}
116

    
117
int64_t parse_time_or_die(const char *context, const char *timestr, int is_duration)
118
{
119
    int64_t us;
120
    if (av_parse_time(&us, timestr, is_duration) < 0) {
121
        fprintf(stderr, "Invalid %s specification for %s: %s\n",
122
                is_duration ? "duration" : "date", context, timestr);
123
        exit(1);
124
    }
125
    return us;
126
}
127

    
128
void show_help_options(const OptionDef *options, const char *msg, int mask, int value)
129
{
130
    const OptionDef *po;
131
    int first;
132

    
133
    first = 1;
134
    for(po = options; po->name != NULL; po++) {
135
        char buf[64];
136
        if ((po->flags & mask) == value) {
137
            if (first) {
138
                printf("%s", msg);
139
                first = 0;
140
            }
141
            av_strlcpy(buf, po->name, sizeof(buf));
142
            if (po->flags & HAS_ARG) {
143
                av_strlcat(buf, " ", sizeof(buf));
144
                av_strlcat(buf, po->argname, sizeof(buf));
145
            }
146
            printf("-%-17s  %s\n", buf, po->help);
147
        }
148
    }
149
}
150

    
151
static const OptionDef* find_option(const OptionDef *po, const char *name){
152
    while (po->name != NULL) {
153
        if (!strcmp(name, po->name))
154
            break;
155
        po++;
156
    }
157
    return po;
158
}
159

    
160
void parse_options(int argc, char **argv, const OptionDef *options,
161
                   void (* parse_arg_function)(const char*))
162
{
163
    const char *opt, *arg;
164
    int optindex, handleoptions=1;
165
    const OptionDef *po;
166

    
167
    /* parse options */
168
    optindex = 1;
169
    while (optindex < argc) {
170
        opt = argv[optindex++];
171

    
172
        if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
173
            int bool_val = 1;
174
            if (opt[1] == '-' && opt[2] == '\0') {
175
                handleoptions = 0;
176
                continue;
177
            }
178
            opt++;
179
            po= find_option(options, opt);
180
            if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
181
                /* handle 'no' bool option */
182
                po = find_option(options, opt + 2);
183
                if (!(po->name && (po->flags & OPT_BOOL)))
184
                    goto unknown_opt;
185
                bool_val = 0;
186
            }
187
            if (!po->name)
188
                po= find_option(options, "default");
189
            if (!po->name) {
190
unknown_opt:
191
                fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
192
                exit(1);
193
            }
194
            arg = NULL;
195
            if (po->flags & HAS_ARG) {
196
                arg = argv[optindex++];
197
                if (!arg) {
198
                    fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
199
                    exit(1);
200
                }
201
            }
202
            if (po->flags & OPT_STRING) {
203
                char *str;
204
                str = av_strdup(arg);
205
                *po->u.str_arg = str;
206
            } else if (po->flags & OPT_BOOL) {
207
                *po->u.int_arg = bool_val;
208
            } else if (po->flags & OPT_INT) {
209
                *po->u.int_arg = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
210
            } else if (po->flags & OPT_INT64) {
211
                *po->u.int64_arg = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
212
            } else if (po->flags & OPT_FLOAT) {
213
                *po->u.float_arg = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
214
            } else if (po->flags & OPT_FUNC2) {
215
                if (po->u.func2_arg(opt, arg) < 0) {
216
                    fprintf(stderr, "%s: failed to set value '%s' for option '%s'\n", argv[0], arg, opt);
217
                    exit(1);
218
                }
219
            } else if (po->flags & OPT_DUMMY) {
220
                /* Do nothing for this option */
221
            } else {
222
                po->u.func_arg(arg);
223
            }
224
            if(po->flags & OPT_EXIT)
225
                exit(0);
226
        } else {
227
            if (parse_arg_function)
228
                parse_arg_function(opt);
229
        }
230
    }
231
}
232

    
233
int opt_default(const char *opt, const char *arg){
234
    int type;
235
    int ret= 0;
236
    const AVOption *o= NULL;
237
    int opt_types[]={AV_OPT_FLAG_VIDEO_PARAM, AV_OPT_FLAG_AUDIO_PARAM, 0, AV_OPT_FLAG_SUBTITLE_PARAM, 0};
238

    
239
    for(type=0; *avcodec_opts && type<AVMEDIA_TYPE_NB && ret>= 0; type++){
240
        const AVOption *o2 = av_find_opt(avcodec_opts[0], opt, NULL, opt_types[type], opt_types[type]);
241
        if(o2)
242
            ret = av_set_string3(avcodec_opts[type], opt, arg, 1, &o);
243
    }
244
    if(!o && avformat_opts)
245
        ret = av_set_string3(avformat_opts, opt, arg, 1, &o);
246
    if(!o && sws_opts)
247
        ret = av_set_string3(sws_opts, opt, arg, 1, &o);
248
    if(!o){
249
        if (opt[0] == 'a' && avcodec_opts[AVMEDIA_TYPE_AUDIO])
250
            ret = av_set_string3(avcodec_opts[AVMEDIA_TYPE_AUDIO], opt+1, arg, 1, &o);
251
        else if(opt[0] == 'v' && avcodec_opts[AVMEDIA_TYPE_VIDEO])
252
            ret = av_set_string3(avcodec_opts[AVMEDIA_TYPE_VIDEO], opt+1, arg, 1, &o);
253
        else if(opt[0] == 's' && avcodec_opts[AVMEDIA_TYPE_SUBTITLE])
254
            ret = av_set_string3(avcodec_opts[AVMEDIA_TYPE_SUBTITLE], opt+1, arg, 1, &o);
255
    }
256
    if (o && ret < 0) {
257
        fprintf(stderr, "Invalid value '%s' for option '%s'\n", arg, opt);
258
        exit(1);
259
    }
260
    if (!o) {
261
        AVCodec *p = NULL;
262
        AVOutputFormat *oformat = NULL;
263
        while ((p=av_codec_next(p))){
264
            AVClass *c= p->priv_class;
265
            if(c && av_find_opt(&c, opt, NULL, 0, 0))
266
                break;
267
        }
268
        if (!p) {
269
            while ((oformat = av_oformat_next(oformat))) {
270
                const AVClass *c = oformat->priv_class;
271
                if (c && av_find_opt(&c, opt, NULL, 0, 0))
272
                    break;
273
            }
274
        }
275
        if(!p && !oformat){
276
            fprintf(stderr, "Unrecognized option '%s'\n", opt);
277
            exit(1);
278
        }
279
    }
280

    
281
//    av_log(NULL, AV_LOG_ERROR, "%s:%s: %f 0x%0X\n", opt, arg, av_get_double(avcodec_opts, opt, NULL), (int)av_get_int(avcodec_opts, opt, NULL));
282

    
283
    //FIXME we should always use avcodec_opts, ... for storing options so there will not be any need to keep track of what i set over this
284
    opt_values= av_realloc(opt_values, sizeof(void*)*(opt_name_count+1));
285
    opt_values[opt_name_count]= o ? NULL : av_strdup(arg);
286
    opt_names= av_realloc(opt_names, sizeof(void*)*(opt_name_count+1));
287
    opt_names[opt_name_count++]= o ? o->name : av_strdup(opt);
288

    
289
    if ((*avcodec_opts && avcodec_opts[0]->debug) || (avformat_opts && avformat_opts->debug))
290
        av_log_set_level(AV_LOG_DEBUG);
291
    return 0;
292
}
293

    
294
int opt_loglevel(const char *opt, const char *arg)
295
{
296
    const struct { const char *name; int level; } log_levels[] = {
297
        { "quiet"  , AV_LOG_QUIET   },
298
        { "panic"  , AV_LOG_PANIC   },
299
        { "fatal"  , AV_LOG_FATAL   },
300
        { "error"  , AV_LOG_ERROR   },
301
        { "warning", AV_LOG_WARNING },
302
        { "info"   , AV_LOG_INFO    },
303
        { "verbose", AV_LOG_VERBOSE },
304
        { "debug"  , AV_LOG_DEBUG   },
305
    };
306
    char *tail;
307
    int level;
308
    int i;
309

    
310
    for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
311
        if (!strcmp(log_levels[i].name, arg)) {
312
            av_log_set_level(log_levels[i].level);
313
            return 0;
314
        }
315
    }
316

    
317
    level = strtol(arg, &tail, 10);
318
    if (*tail) {
319
        fprintf(stderr, "Invalid loglevel \"%s\". "
320
                        "Possible levels are numbers or:\n", arg);
321
        for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
322
            fprintf(stderr, "\"%s\"\n", log_levels[i].name);
323
        exit(1);
324
    }
325
    av_log_set_level(level);
326
    return 0;
327
}
328

    
329
int opt_timelimit(const char *opt, const char *arg)
330
{
331
#if HAVE_SETRLIMIT
332
    int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
333
    struct rlimit rl = { lim, lim + 1 };
334
    if (setrlimit(RLIMIT_CPU, &rl))
335
        perror("setrlimit");
336
#else
337
    fprintf(stderr, "Warning: -%s not implemented on this OS\n", opt);
338
#endif
339
    return 0;
340
}
341

    
342
void set_context_opts(void *ctx, void *opts_ctx, int flags, AVCodec *codec)
343
{
344
    int i;
345
    void *priv_ctx=NULL;
346
    if(!strcmp("AVCodecContext", (*(AVClass**)ctx)->class_name)){
347
        AVCodecContext *avctx= ctx;
348
        if(codec && codec->priv_class && avctx->priv_data){
349
            priv_ctx= avctx->priv_data;
350
        }
351
    } else if (!strcmp("AVFormatContext", (*(AVClass**)ctx)->class_name)) {
352
        AVFormatContext *avctx = ctx;
353
        if (avctx->oformat && avctx->oformat->priv_class) {
354
            priv_ctx = avctx->priv_data;
355
        }
356
    }
357

    
358
    for(i=0; i<opt_name_count; i++){
359
        char buf[256];
360
        const AVOption *opt;
361
        const char *str= av_get_string(opts_ctx, opt_names[i], &opt, buf, sizeof(buf));
362
        /* if an option with name opt_names[i] is present in opts_ctx then str is non-NULL */
363
        if(str && ((opt->flags & flags) == flags))
364
            av_set_string3(ctx, opt_names[i], str, 1, NULL);
365
        /* We need to use a differnt system to pass options to the private context because
366
           it is not known which codec and thus context kind that will be when parsing options
367
           we thus use opt_values directly instead of opts_ctx */
368
        if(!str && priv_ctx && av_get_string(priv_ctx, opt_names[i], &opt, buf, sizeof(buf))){
369
            av_set_string3(priv_ctx, opt_names[i], opt_values[i], 1, NULL);
370
        }
371
    }
372
}
373

    
374
void print_error(const char *filename, int err)
375
{
376
    char errbuf[128];
377
    const char *errbuf_ptr = errbuf;
378

    
379
    if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
380
        errbuf_ptr = strerror(AVUNERROR(err));
381
    fprintf(stderr, "%s: %s\n", filename, errbuf_ptr);
382
}
383

    
384
static int warned_cfg = 0;
385

    
386
#define INDENT        1
387
#define SHOW_VERSION  2
388
#define SHOW_CONFIG   4
389

    
390
#define PRINT_LIB_INFO(outstream,libname,LIBNAME,flags)                 \
391
    if (CONFIG_##LIBNAME) {                                             \
392
        const char *indent = flags & INDENT? "  " : "";                 \
393
        if (flags & SHOW_VERSION) {                                     \
394
            unsigned int version = libname##_version();                 \
395
            fprintf(outstream, "%slib%-9s %2d.%3d.%2d / %2d.%3d.%2d\n", \
396
                    indent, #libname,                                   \
397
                    LIB##LIBNAME##_VERSION_MAJOR,                       \
398
                    LIB##LIBNAME##_VERSION_MINOR,                       \
399
                    LIB##LIBNAME##_VERSION_MICRO,                       \
400
                    version >> 16, version >> 8 & 0xff, version & 0xff); \
401
        }                                                               \
402
        if (flags & SHOW_CONFIG) {                                      \
403
            const char *cfg = libname##_configuration();                \
404
            if (strcmp(FFMPEG_CONFIGURATION, cfg)) {                    \
405
                if (!warned_cfg) {                                      \
406
                    fprintf(outstream,                                  \
407
                            "%sWARNING: library configuration mismatch\n", \
408
                            indent);                                    \
409
                    warned_cfg = 1;                                     \
410
                }                                                       \
411
                fprintf(stderr, "%s%-11s configuration: %s\n",          \
412
                        indent, #libname, cfg);                         \
413
            }                                                           \
414
        }                                                               \
415
    }                                                                   \
416

    
417
static void print_all_libs_info(FILE* outstream, int flags)
418
{
419
    PRINT_LIB_INFO(outstream, avutil,   AVUTIL,   flags);
420
    PRINT_LIB_INFO(outstream, avcodec,  AVCODEC,  flags);
421
    PRINT_LIB_INFO(outstream, avformat, AVFORMAT, flags);
422
    PRINT_LIB_INFO(outstream, avdevice, AVDEVICE, flags);
423
    PRINT_LIB_INFO(outstream, avfilter, AVFILTER, flags);
424
    PRINT_LIB_INFO(outstream, swscale,  SWSCALE,  flags);
425
    PRINT_LIB_INFO(outstream, postproc, POSTPROC, flags);
426
}
427

    
428
void show_banner(void)
429
{
430
    fprintf(stderr, "%s version " FFMPEG_VERSION ", Copyright (c) %d-%d the FFmpeg developers\n",
431
            program_name, program_birth_year, this_year);
432
    fprintf(stderr, "  built on %s %s with %s %s\n",
433
            __DATE__, __TIME__, CC_TYPE, CC_VERSION);
434
    fprintf(stderr, "  configuration: " FFMPEG_CONFIGURATION "\n");
435
    print_all_libs_info(stderr, INDENT|SHOW_CONFIG);
436
    print_all_libs_info(stderr, INDENT|SHOW_VERSION);
437
}
438

    
439
void show_version(void) {
440
    printf("%s " FFMPEG_VERSION "\n", program_name);
441
    print_all_libs_info(stdout, SHOW_VERSION);
442
}
443

    
444
void show_license(void)
445
{
446
    printf(
447
#if CONFIG_NONFREE
448
    "This version of %s has nonfree parts compiled in.\n"
449
    "Therefore it is not legally redistributable.\n",
450
    program_name
451
#elif CONFIG_GPLV3
452
    "%s is free software; you can redistribute it and/or modify\n"
453
    "it under the terms of the GNU General Public License as published by\n"
454
    "the Free Software Foundation; either version 3 of the License, or\n"
455
    "(at your option) any later version.\n"
456
    "\n"
457
    "%s is distributed in the hope that it will be useful,\n"
458
    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
459
    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
460
    "GNU General Public License for more details.\n"
461
    "\n"
462
    "You should have received a copy of the GNU General Public License\n"
463
    "along with %s.  If not, see <http://www.gnu.org/licenses/>.\n",
464
    program_name, program_name, program_name
465
#elif CONFIG_GPL
466
    "%s is free software; you can redistribute it and/or modify\n"
467
    "it under the terms of the GNU General Public License as published by\n"
468
    "the Free Software Foundation; either version 2 of the License, or\n"
469
    "(at your option) any later version.\n"
470
    "\n"
471
    "%s is distributed in the hope that it will be useful,\n"
472
    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
473
    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
474
    "GNU General Public License for more details.\n"
475
    "\n"
476
    "You should have received a copy of the GNU General Public License\n"
477
    "along with %s; if not, write to the Free Software\n"
478
    "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
479
    program_name, program_name, program_name
480
#elif CONFIG_LGPLV3
481
    "%s is free software; you can redistribute it and/or modify\n"
482
    "it under the terms of the GNU Lesser General Public License as published by\n"
483
    "the Free Software Foundation; either version 3 of the License, or\n"
484
    "(at your option) any later version.\n"
485
    "\n"
486
    "%s is distributed in the hope that it will be useful,\n"
487
    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
488
    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
489
    "GNU Lesser General Public License for more details.\n"
490
    "\n"
491
    "You should have received a copy of the GNU Lesser General Public License\n"
492
    "along with %s.  If not, see <http://www.gnu.org/licenses/>.\n",
493
    program_name, program_name, program_name
494
#else
495
    "%s is free software; you can redistribute it and/or\n"
496
    "modify it under the terms of the GNU Lesser General Public\n"
497
    "License as published by the Free Software Foundation; either\n"
498
    "version 2.1 of the License, or (at your option) any later version.\n"
499
    "\n"
500
    "%s is distributed in the hope that it will be useful,\n"
501
    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
502
    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
503
    "Lesser General Public License for more details.\n"
504
    "\n"
505
    "You should have received a copy of the GNU Lesser General Public\n"
506
    "License along with %s; if not, write to the Free Software\n"
507
    "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
508
    program_name, program_name, program_name
509
#endif
510
    );
511
}
512

    
513
void show_formats(void)
514
{
515
    AVInputFormat *ifmt=NULL;
516
    AVOutputFormat *ofmt=NULL;
517
    const char *last_name;
518

    
519
    printf(
520
        "File formats:\n"
521
        " D. = Demuxing supported\n"
522
        " .E = Muxing supported\n"
523
        " --\n");
524
    last_name= "000";
525
    for(;;){
526
        int decode=0;
527
        int encode=0;
528
        const char *name=NULL;
529
        const char *long_name=NULL;
530

    
531
        while((ofmt= av_oformat_next(ofmt))) {
532
            if((name == NULL || strcmp(ofmt->name, name)<0) &&
533
                strcmp(ofmt->name, last_name)>0){
534
                name= ofmt->name;
535
                long_name= ofmt->long_name;
536
                encode=1;
537
            }
538
        }
539
        while((ifmt= av_iformat_next(ifmt))) {
540
            if((name == NULL || strcmp(ifmt->name, name)<0) &&
541
                strcmp(ifmt->name, last_name)>0){
542
                name= ifmt->name;
543
                long_name= ifmt->long_name;
544
                encode=0;
545
            }
546
            if(name && strcmp(ifmt->name, name)==0)
547
                decode=1;
548
        }
549
        if(name==NULL)
550
            break;
551
        last_name= name;
552

    
553
        printf(
554
            " %s%s %-15s %s\n",
555
            decode ? "D":" ",
556
            encode ? "E":" ",
557
            name,
558
            long_name ? long_name:" ");
559
    }
560
}
561

    
562
void show_codecs(void)
563
{
564
    AVCodec *p=NULL, *p2;
565
    const char *last_name;
566
    printf(
567
        "Codecs:\n"
568
        " D..... = Decoding supported\n"
569
        " .E.... = Encoding supported\n"
570
        " ..V... = Video codec\n"
571
        " ..A... = Audio codec\n"
572
        " ..S... = Subtitle codec\n"
573
        " ...S.. = Supports draw_horiz_band\n"
574
        " ....D. = Supports direct rendering method 1\n"
575
        " .....T = Supports weird frame truncation\n"
576
        " ------\n");
577
    last_name= "000";
578
    for(;;){
579
        int decode=0;
580
        int encode=0;
581
        int cap=0;
582
        const char *type_str;
583

    
584
        p2=NULL;
585
        while((p= av_codec_next(p))) {
586
            if((p2==NULL || strcmp(p->name, p2->name)<0) &&
587
                strcmp(p->name, last_name)>0){
588
                p2= p;
589
                decode= encode= cap=0;
590
            }
591
            if(p2 && strcmp(p->name, p2->name)==0){
592
                if(p->decode) decode=1;
593
                if(p->encode) encode=1;
594
                cap |= p->capabilities;
595
            }
596
        }
597
        if(p2==NULL)
598
            break;
599
        last_name= p2->name;
600

    
601
        switch(p2->type) {
602
        case AVMEDIA_TYPE_VIDEO:
603
            type_str = "V";
604
            break;
605
        case AVMEDIA_TYPE_AUDIO:
606
            type_str = "A";
607
            break;
608
        case AVMEDIA_TYPE_SUBTITLE:
609
            type_str = "S";
610
            break;
611
        default:
612
            type_str = "?";
613
            break;
614
        }
615
        printf(
616
            " %s%s%s%s%s%s %-15s %s",
617
            decode ? "D": (/*p2->decoder ? "d":*/" "),
618
            encode ? "E":" ",
619
            type_str,
620
            cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S":" ",
621
            cap & CODEC_CAP_DR1 ? "D":" ",
622
            cap & CODEC_CAP_TRUNCATED ? "T":" ",
623
            p2->name,
624
            p2->long_name ? p2->long_name : "");
625
       /* if(p2->decoder && decode==0)
626
            printf(" use %s for decoding", p2->decoder->name);*/
627
        printf("\n");
628
    }
629
    printf("\n");
630
    printf(
631
"Note, the names of encoders and decoders do not always match, so there are\n"
632
"several cases where the above table shows encoder only or decoder only entries\n"
633
"even though both encoding and decoding are supported. For example, the h263\n"
634
"decoder corresponds to the h263 and h263p encoders, for file formats it is even\n"
635
"worse.\n");
636
}
637

    
638
void show_bsfs(void)
639
{
640
    AVBitStreamFilter *bsf=NULL;
641

    
642
    printf("Bitstream filters:\n");
643
    while((bsf = av_bitstream_filter_next(bsf)))
644
        printf("%s\n", bsf->name);
645
    printf("\n");
646
}
647

    
648
void show_protocols(void)
649
{
650
    URLProtocol *up=NULL;
651

    
652
    printf("Supported file protocols:\n"
653
           "I.. = Input  supported\n"
654
           ".O. = Output supported\n"
655
           "..S = Seek   supported\n"
656
           "FLAGS NAME\n"
657
           "----- \n");
658
    while((up = av_protocol_next(up)))
659
        printf("%c%c%c   %s\n",
660
               up->url_read  ? 'I' : '.',
661
               up->url_write ? 'O' : '.',
662
               up->url_seek  ? 'S' : '.',
663
               up->name);
664
}
665

    
666
void show_filters(void)
667
{
668
    AVFilter av_unused(**filter) = NULL;
669

    
670
    printf("Filters:\n");
671
#if CONFIG_AVFILTER
672
    while ((filter = av_filter_next(filter)) && *filter)
673
        printf("%-16s %s\n", (*filter)->name, (*filter)->description);
674
#endif
675
}
676

    
677
void show_pix_fmts(void)
678
{
679
    enum PixelFormat pix_fmt;
680

    
681
    printf(
682
        "Pixel formats:\n"
683
        "I.... = Supported Input  format for conversion\n"
684
        ".O... = Supported Output format for conversion\n"
685
        "..H.. = Hardware accelerated format\n"
686
        "...P. = Paletted format\n"
687
        "....B = Bitstream format\n"
688
        "FLAGS NAME            NB_COMPONENTS BITS_PER_PIXEL\n"
689
        "-----\n");
690

    
691
#if !CONFIG_SWSCALE
692
#   define sws_isSupportedInput(x)  0
693
#   define sws_isSupportedOutput(x) 0
694
#endif
695

    
696
    for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
697
        const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
698
        printf("%c%c%c%c%c %-16s       %d            %2d\n",
699
               sws_isSupportedInput (pix_fmt)      ? 'I' : '.',
700
               sws_isSupportedOutput(pix_fmt)      ? 'O' : '.',
701
               pix_desc->flags & PIX_FMT_HWACCEL   ? 'H' : '.',
702
               pix_desc->flags & PIX_FMT_PAL       ? 'P' : '.',
703
               pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
704
               pix_desc->name,
705
               pix_desc->nb_components,
706
               av_get_bits_per_pixel(pix_desc));
707
    }
708
}
709

    
710
int read_yesno(void)
711
{
712
    int c = getchar();
713
    int yesno = (toupper(c) == 'Y');
714

    
715
    while (c != '\n' && c != EOF)
716
        c = getchar();
717

    
718
    return yesno;
719
}
720

    
721
int read_file(const char *filename, char **bufptr, size_t *size)
722
{
723
    FILE *f = fopen(filename, "rb");
724

    
725
    if (!f) {
726
        fprintf(stderr, "Cannot read file '%s': %s\n", filename, strerror(errno));
727
        return AVERROR(errno);
728
    }
729
    fseek(f, 0, SEEK_END);
730
    *size = ftell(f);
731
    fseek(f, 0, SEEK_SET);
732
    *bufptr = av_malloc(*size + 1);
733
    if (!*bufptr) {
734
        fprintf(stderr, "Could not allocate file buffer\n");
735
        fclose(f);
736
        return AVERROR(ENOMEM);
737
    }
738
    fread(*bufptr, 1, *size, f);
739
    (*bufptr)[*size++] = '\0';
740

    
741
    fclose(f);
742
    return 0;
743
}
744

    
745
FILE *get_preset_file(char *filename, size_t filename_size,
746
                      const char *preset_name, int is_path, const char *codec_name)
747
{
748
    FILE *f = NULL;
749
    int i;
750
    const char *base[3]= { getenv("FFMPEG_DATADIR"),
751
                           getenv("HOME"),
752
                           FFMPEG_DATADIR,
753
                         };
754

    
755
    if (is_path) {
756
        av_strlcpy(filename, preset_name, filename_size);
757
        f = fopen(filename, "r");
758
    } else {
759
        for (i = 0; i < 3 && !f; i++) {
760
            if (!base[i])
761
                continue;
762
            snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", preset_name);
763
            f = fopen(filename, "r");
764
            if (!f && codec_name) {
765
                snprintf(filename, filename_size,
766
                         "%s%s/%s-%s.ffpreset", base[i],  i != 1 ? "" : "/.ffmpeg", codec_name, preset_name);
767
                f = fopen(filename, "r");
768
            }
769
        }
770
    }
771

    
772
    return f;
773
}
774

    
775
#if CONFIG_AVFILTER
776

    
777
static int ffsink_init(AVFilterContext *ctx, const char *args, void *opaque)
778
{
779
    FFSinkContext *priv = ctx->priv;
780

    
781
    if (!opaque)
782
        return AVERROR(EINVAL);
783
    *priv = *(FFSinkContext *)opaque;
784

    
785
    return 0;
786
}
787

    
788
static void null_end_frame(AVFilterLink *inlink) { }
789

    
790
static int ffsink_query_formats(AVFilterContext *ctx)
791
{
792
    FFSinkContext *priv = ctx->priv;
793
    enum PixelFormat pix_fmts[] = { priv->pix_fmt, PIX_FMT_NONE };
794

    
795
    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
796
    return 0;
797
}
798

    
799
AVFilter ffsink = {
800
    .name      = "ffsink",
801
    .priv_size = sizeof(FFSinkContext),
802
    .init      = ffsink_init,
803

    
804
    .query_formats = ffsink_query_formats,
805

    
806
    .inputs    = (AVFilterPad[]) {{ .name          = "default",
807
                                    .type          = AVMEDIA_TYPE_VIDEO,
808
                                    .end_frame     = null_end_frame,
809
                                    .min_perms     = AV_PERM_READ, },
810
                                  { .name = NULL }},
811
    .outputs   = (AVFilterPad[]) {{ .name = NULL }},
812
};
813

    
814
int get_filtered_video_frame(AVFilterContext *ctx, AVFrame *frame,
815
                             AVFilterBufferRef **picref_ptr, AVRational *tb)
816
{
817
    int ret;
818
    AVFilterBufferRef *picref;
819

    
820
    if ((ret = avfilter_request_frame(ctx->inputs[0])) < 0)
821
        return ret;
822
    if (!(picref = ctx->inputs[0]->cur_buf))
823
        return AVERROR(ENOENT);
824
    *picref_ptr = picref;
825
    ctx->inputs[0]->cur_buf = NULL;
826
    *tb = ctx->inputs[0]->time_base;
827

    
828
    memcpy(frame->data,     picref->data,     sizeof(frame->data));
829
    memcpy(frame->linesize, picref->linesize, sizeof(frame->linesize));
830
    frame->interlaced_frame = picref->video->interlaced;
831
    frame->top_field_first  = picref->video->top_field_first;
832
    frame->key_frame        = picref->video->key_frame;
833
    frame->pict_type        = picref->video->pict_type;
834

    
835
    return 1;
836
}
837

    
838
#endif /* CONFIG_AVFILTER */