dav1d_cli_parse.c 13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * Copyright © 2018, VideoLAN and dav1d authors
 * Copyright © 2018, Two Orioles, LLC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#include <getopt.h>
31
#include <limits.h>
32
33
34
35
36
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Steve Lhomme's avatar
Steve Lhomme committed
37
38
39
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
40
41

#include "dav1d_cli_parse.h"
42
#include "src/cpu.h"
43

44
static const char short_opts[] = "i:o:vql:s:";
45
46

enum {
47
    ARG_DEMUXER = 256,
48
    ARG_MUXER,
49
50
51
    ARG_FRAME_TIMES,
    ARG_REALTIME,
    ARG_REALTIME_CACHE,
52
53
    ARG_FRAME_THREADS,
    ARG_TILE_THREADS,
54
    ARG_VERIFY,
55
    ARG_FILM_GRAIN,
56
    ARG_OPPOINT,
57
    ARG_ALL_LAYERS,
58
    ARG_SIZE_LIMIT,
59
    ARG_CPU_MASK,
60
    ARG_OUTPUT_INVISIBLE,
61
62
63
};

static const struct option long_opts[] = {
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    { "input",           1, NULL, 'i' },
    { "output",          1, NULL, 'o' },
    { "quiet",           0, NULL, 'q' },
    { "demuxer",         1, NULL, ARG_DEMUXER },
    { "muxer",           1, NULL, ARG_MUXER },
    { "version",         0, NULL, 'v' },
    { "frametimes",      1, NULL, ARG_FRAME_TIMES },
    { "limit",           1, NULL, 'l' },
    { "skip",            1, NULL, 's' },
    { "realtime",        2, NULL, ARG_REALTIME },
    { "realtimecache",   1, NULL, ARG_REALTIME_CACHE },
    { "framethreads",    1, NULL, ARG_FRAME_THREADS },
    { "tilethreads",     1, NULL, ARG_TILE_THREADS },
    { "verify",          1, NULL, ARG_VERIFY },
    { "filmgrain",       1, NULL, ARG_FILM_GRAIN },
    { "oppoint",         1, NULL, ARG_OPPOINT },
    { "alllayers",       1, NULL, ARG_ALL_LAYERS },
    { "sizelimit",       1, NULL, ARG_SIZE_LIMIT },
    { "cpumask",         1, NULL, ARG_CPU_MASK },
    { "outputinvisible", 1, NULL, ARG_OUTPUT_INVISIBLE },
    { NULL,              0, NULL, 0 },
85
86
};

87
88
89
90
#if ARCH_AARCH64 || ARCH_ARM
#define ALLOWED_CPU_MASKS " or 'neon'"
#elif ARCH_X86
#define ALLOWED_CPU_MASKS \
91
    ", 'sse2', 'ssse3', 'sse41', 'avx2', 'avx512' or 'avx512icl'"
92
93
94
95
#else
#define ALLOWED_CPU_MASKS "not yet implemented for this architecture"
#endif

96
97
98
99
100
101
102
103
104
105
106
static void usage(const char *const app, const char *const reason, ...) {
    if (reason) {
        va_list args;

        va_start(args, reason);
        vfprintf(stderr, reason, args);
        va_end(args);
        fprintf(stderr, "\n\n");
    }
    fprintf(stderr, "Usage: %s [options]\n\n", app);
    fprintf(stderr, "Supported options:\n"
107
108
            " --input/-i  $file:    input file\n"
            " --output/-o $file:    output file\n"
109
            " --demuxer $name:      force demuxer type ('ivf', 'section5' or 'annexb'; default: detect from extension)\n"
110
111
112
113
114
115
116
117
118
119
120
121
122
            " --muxer $name:        force muxer type ('md5', 'yuv', 'yuv4mpeg2' or 'null'; default: detect from extension)\n"
            " --quiet/-q:           disable status messages\n"
            " --frametimes $file:   dump frame times to file\n"
            " --limit/-l $num:      stop decoding after $num frames\n"
            " --skip/-s $num:       skip decoding of the first $num frames\n"
            " --realtime [$fract]:  limit framerate, optional argument to override input framerate\n"
            " --realtimecache $num: set the size of the cache in realtime mode (default: 0)\n"
            " --version/-v:         print version and exit\n"
            " --framethreads $num:  number of frame threads (default: 1)\n"
            " --tilethreads $num:   number of tile threads (default: 1)\n"
            " --filmgrain $num:     enable film grain application (default: 1, except if muxer is md5)\n"
            " --oppoint $num:       select an operating point of a scalable AV1 bitstream (0 - 32)\n"
            " --alllayers $num:     output all spatial layers of a scalable AV1 bitstream (default: 1)\n"
123
            " --sizelimit $num:     stop decoding if the frame size exceeds the specified limit\n"
124
            " --verify $md5:        verify decoded md5. implies --muxer md5, no output\n"
125
126
            " --cpumask $mask:      restrict permitted CPU instruction sets (0" ALLOWED_CPU_MASKS "; default: -1)\n"
            " --outputinvisible $num: whether to output invisible (alt-ref) frames (default: 0)\n");
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    exit(1);
}

static void error(const char *const app, const char *const optarg,
                  const int option, const char *const shouldbe)
{
    char optname[256];
    int n;

    for (n = 0; long_opts[n].name; n++)
        if (long_opts[n].val == option)
            break;
    assert(long_opts[n].name);
    if (long_opts[n].val < 256) {
        sprintf(optname, "-%c/--%s", long_opts[n].val, long_opts[n].name);
    } else {
        sprintf(optname, "--%s", long_opts[n].name);
    }

    usage(app, "Invalid argument \"%s\" for option %s; should be %s",
          optarg, optname, shouldbe);
}

150
151
152
static unsigned parse_unsigned(const char *const optarg, const int option,
                               const char *const app)
{
153
    char *end;
154
    const unsigned res = (unsigned) strtoul(optarg, &end, 0);
155
156
157
158
    if (*end || end == optarg) error(app, optarg, option, "an integer");
    return res;
}

159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
static int parse_optional_fraction(const char *const optarg, const int option,
                                   const char *const app, double *value)
{
    if (optarg == NULL) return 0;
    char *end;
    *value = strtod(optarg, &end);
    if (*end == '/' && end != optarg) {
        const char *optarg2 = end + 1;
        *value /= strtod(optarg2, &end);
        if (*end || end == optarg2) error(app, optarg, option, "a fraction");
    } else if (*end || end == optarg) {
        error(app, optarg, option, "a fraction");
    }
    return 1;
}

175
176
177
178
179
180
181
typedef struct EnumParseTable {
    const char *str;
    const int val;
} EnumParseTable;

#if ARCH_X86
enum CpuMask {
Henrik Gramner's avatar
Henrik Gramner committed
182
183
    X86_CPU_MASK_SSE2      = DAV1D_X86_CPU_FLAG_SSE2,
    X86_CPU_MASK_SSSE3     = DAV1D_X86_CPU_FLAG_SSSE3     | X86_CPU_MASK_SSE2,
184
    X86_CPU_MASK_SSE41     = DAV1D_X86_CPU_FLAG_SSE41     | X86_CPU_MASK_SSSE3,
Henrik Gramner's avatar
Henrik Gramner committed
185
186
    X86_CPU_MASK_AVX2      = DAV1D_X86_CPU_FLAG_AVX2      | X86_CPU_MASK_SSE41,
    X86_CPU_MASK_AVX512ICL = DAV1D_X86_CPU_FLAG_AVX512ICL | X86_CPU_MASK_AVX2,
187
188
189
190
191
192
193
};
#endif

static const EnumParseTable cpu_mask_tbl[] = {
#if ARCH_AARCH64 || ARCH_ARM
    { "neon", DAV1D_ARM_CPU_FLAG_NEON },
#elif ARCH_X86
194
195
196
197
198
    { "sse2",      X86_CPU_MASK_SSE2 },
    { "ssse3",     X86_CPU_MASK_SSSE3 },
    { "sse41",     X86_CPU_MASK_SSE41 },
    { "avx2",      X86_CPU_MASK_AVX2 },
    { "avx512icl", X86_CPU_MASK_AVX512ICL },
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#endif
    { 0 },
};

static unsigned parse_enum(char *optarg, const EnumParseTable *const tbl,
                           const int option, const char *app)
{
    char str[1024];

    strcpy(str, "any of ");
    for (int n = 0; tbl[n].str; n++) {
        if (!strcmp(tbl[n].str, optarg))
            return tbl[n].val;

        if (n) {
            if (!tbl[n + 1].str)
                strcat(str, " or ");
            else
                strcat(str, ", ");
        }
        strcat(str, tbl[n].str);
    }

    char *end;
    unsigned res;
    if (!strncmp(optarg, "0x", 2)) {
225
        res = (unsigned) strtoul(&optarg[2], &end, 16);
226
    } else {
227
        res = (unsigned) strtoul(optarg, &end, 0);
228
229
230
231
232
233
234
235
236
237
    }

    if (*end || end == optarg) {
        strcat(str, ", a hexadecimal (starting with 0x), or an integer");
        error(app, optarg, option, str);
    }

    return res;
}

238
239
240
241
242
243
244
void parse(const int argc, char *const *const argv,
           CLISettings *const cli_settings, Dav1dSettings *const lib_settings)
{
    int o;

    memset(cli_settings, 0, sizeof(*cli_settings));
    dav1d_default_settings(lib_settings);
245
    int grain_specified = 0;
246

247
    while ((o = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
        switch (o) {
        case 'o':
            cli_settings->outputfile = optarg;
            break;
        case 'i':
            cli_settings->inputfile = optarg;
            break;
        case 'q':
            cli_settings->quiet = 1;
            break;
        case 'l':
            cli_settings->limit = parse_unsigned(optarg, 'l', argv[0]);
            break;
        case 's':
            cli_settings->skip = parse_unsigned(optarg, 's', argv[0]);
            break;
Nathan Egge's avatar
Nathan Egge committed
264
265
266
        case ARG_DEMUXER:
            cli_settings->demuxer = optarg;
            break;
267
268
269
        case ARG_MUXER:
            cli_settings->muxer = optarg;
            break;
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
        case ARG_FRAME_TIMES:
            cli_settings->frametimes = optarg;
            break;
        case ARG_REALTIME:
            // workaround to parse an optional argument of the form `--a b`
            // (getopt only allows `--a=b`)
            if (optarg == NULL && optind < argc && argv[optind] != NULL &&
                argv[optind][0] != '-')
            {
                optarg = argv[optind];
                optind++;
            }
            cli_settings->realtime = 1 + parse_optional_fraction(optarg,
                ARG_REALTIME, argv[0], &cli_settings->realtime_fps);
            break;
        case ARG_REALTIME_CACHE:
            cli_settings->realtime_cache =
                parse_unsigned(optarg, ARG_REALTIME_CACHE, argv[0]);
            break;
289
290
291
292
293
294
295
296
        case ARG_FRAME_THREADS:
            lib_settings->n_frame_threads =
                parse_unsigned(optarg, ARG_FRAME_THREADS, argv[0]);
            break;
        case ARG_TILE_THREADS:
            lib_settings->n_tile_threads =
                parse_unsigned(optarg, ARG_TILE_THREADS, argv[0]);
            break;
297
298
299
        case ARG_VERIFY:
            cli_settings->verify = optarg;
            break;
300
301
302
303
304
        case ARG_FILM_GRAIN:
            lib_settings->apply_grain =
                !!parse_unsigned(optarg, ARG_FILM_GRAIN, argv[0]);
            grain_specified = 1;
            break;
305
306
307
308
        case ARG_OPPOINT:
            lib_settings->operating_point =
                parse_unsigned(optarg, ARG_OPPOINT, argv[0]);
            break;
309
310
311
312
        case ARG_ALL_LAYERS:
            lib_settings->all_layers =
                !!parse_unsigned(optarg, ARG_ALL_LAYERS, argv[0]);
            break;
313
        case ARG_SIZE_LIMIT: {
314
315
316
317
318
319
320
321
322
            char *arg = optarg, *end;
            uint64_t res = strtoul(arg, &end, 0);
            if (*end == 'x') // NxM
                res *= strtoul((arg = end + 1), &end, 0);
            if (*end || end == arg || res >= UINT_MAX)
                error(argv[0], optarg, ARG_SIZE_LIMIT, "an integer or dimension");
            lib_settings->frame_size_limit = (unsigned) res;
            break;
        }
323
        case 'v':
324
            fprintf(stderr, "%s\n", dav1d_version());
325
            exit(0);
326
327
328
329
        case ARG_CPU_MASK:
            dav1d_set_cpu_flags_mask(parse_enum(optarg, cpu_mask_tbl,
                                                ARG_CPU_MASK, argv[0]));
            break;
330
331
332
333
        case ARG_OUTPUT_INVISIBLE:
            lib_settings->output_invisible_frames =
                !!parse_unsigned(optarg, ARG_OUTPUT_INVISIBLE, argv[0]);
            break;
334
        default:
335
            usage(argv[0], NULL);
336
337
338
        }
    }

339
340
    if (optind < argc)
        usage(argv[0], "Extra/unused arguments found, e.g. '%s'\n", argv[optind]);
341
342
343
344
345
346
347
348
349
350
351
    if (cli_settings->verify) {
        if (cli_settings->outputfile)
            usage(argv[0], "Verification (--verify) requires output file (-o/--output) to not set");
        if (cli_settings->muxer && !strcmp(cli_settings->muxer, "md5"))
            usage(argv[0], "Verification (--verify) requires the md5 muxer (--muxer md5)");

        cli_settings->outputfile = "-";
        if (!cli_settings->muxer)
            cli_settings->muxer = "md5";
    }

352
353
354
355
356
357
    if (!grain_specified && cli_settings->muxer &&
        !strcmp(cli_settings->muxer, "md5"))
    {
        lib_settings->apply_grain = 0;
    }

358
359
    if (!cli_settings->inputfile)
        usage(argv[0], "Input file (-i/--input) is required");
360
361
362
    if ((!cli_settings->muxer || strcmp(cli_settings->muxer, "null")) &&
        !cli_settings->outputfile)
    {
363
        usage(argv[0], "Output file (-o/--output) is required");
364
    }
365
}