History

fftools/ffmpeg: switch -map parsing to new stream specifier API

Makes optional map handling less hacky, fixes combining optional maps with metadata matching on tags/values containing the '?' character/ Forward errors from stream specifier parsing, previously the code would ignore them.

Anton Khirnov committed 2 years ago

Commit 9e3b5b81 parent d1bdd891 file · +37 −17Browse files

Unified · Split

fftools/ffmpeg_opt.c+37−17
@@ -427,22 +427,20 @@ static int opt_map(void *optctx, const char *opt, const char *arg) · 426 unmodified lines
427427 {
428428 OptionsContext *o = optctx;
429429 StreamMap *m = NULL;
430+ StreamSpecifier ss;
430431 int i, negative = 0, file_idx, disabled = 0;
431− int ret;
432− char *map, *p;
433− char *allow_unused;
432+ int ret, allow_unused = 0;
433+
434+ memset(&ss, 0, sizeof(ss));
434435
435436 if (*arg == '-') {
436437 negative = 1;
437438 arg++;
438439 }
439− map = av_strdup(arg);
440− if (!map)
441− return AVERROR(ENOMEM);
442440
443− if (map[0] == '[') {
441+ if (arg[0] == '[') {
444442 /* this mapping refers to lavfi output */
445− const char *c = map + 1;
443+ const char *c = arg + 1;
446444
447445 ret = GROW_ARRAY(o->stream_maps, o->nb_stream_maps);
448446 if (ret < 0)
@@ -451,33 +449,55 @@ static int opt_map(void *optctx, const char *opt, const char *arg) · 2 unmodified lines
451449 m = &o->stream_maps[o->nb_stream_maps - 1];
452450 m->linklabel = av_get_token(&c, "]");
453451 if (!m->linklabel) {
454− av_log(NULL, AV_LOG_ERROR, "Invalid output link label: %s.\n", map);
452+ av_log(NULL, AV_LOG_ERROR, "Invalid output link label: %s.\n", arg);
455453 ret = AVERROR(EINVAL);
456454 goto fail;
457455 }
458456 } else {
459− if (allow_unused = strchr(map, '?'))
460− *allow_unused = 0;
461− file_idx = strtol(map, &p, 0);
457+ char *endptr;
458+
459+ file_idx = strtol(arg, &endptr, 0);
462460 if (file_idx >= nb_input_files || file_idx < 0) {
463461 av_log(NULL, AV_LOG_FATAL, "Invalid input file index: %d.\n", file_idx);
464462 ret = AVERROR(EINVAL);
465463 goto fail;
466464 }
465+ arg = endptr;
466+
467+ ret = stream_specifier_parse(&ss, *arg == ':' ? arg + 1 : arg, 1, NULL);
468+ if (ret < 0) {
469+ av_log(NULL, AV_LOG_ERROR, "Invalid stream specifier: %s\n", arg);
470+ goto fail;
471+ }
472+
473+ if (ss.remainder) {
474+ if (!strcmp(ss.remainder, "?"))
475+ allow_unused = 1;
476+ else {
477+ av_log(NULL, AV_LOG_ERROR, "Trailing garbage after stream specifier: %s\n",
478+ ss.remainder);
479+ ret = AVERROR(EINVAL);
480+ goto fail;
481+ }
482+ }
483+
467484 if (negative)
468485 /* disable some already defined maps */
469486 for (i = 0; i < o->nb_stream_maps; i++) {
470487 m = &o->stream_maps[i];
471488 if (file_idx == m->file_index &&
472− check_stream_specifier(input_files[m->file_index]->ctx,
489+ stream_specifier_match(&ss,
490+ input_files[m->file_index]->ctx,
473491 input_files[m->file_index]->ctx->streams[m->stream_index],
474− *p == ':' ? p + 1 : p) > 0)
492+ NULL))
475493 m->disabled = 1;
476494 }
477495 else
478496 for (i = 0; i < input_files[file_idx]->nb_streams; i++) {
479− if (check_stream_specifier(input_files[file_idx]->ctx, input_files[file_idx]->ctx->streams[i],
480− *p == ':' ? p + 1 : p) <= 0)
497+ if (!stream_specifier_match(&ss,
498+ input_files[file_idx]->ctx,
499+ input_files[file_idx]->ctx->streams[i],
500+ NULL))
481501 continue;
482502 if (input_files[file_idx]->streams[i]->user_set_discard == AVDISCARD_ALL) {
483503 disabled = 1;
@@ -511,7 +531,7 @@ static int opt_map(void *optctx, const char *opt, const char *arg) · 27 unmodified lines
511531 }
512532 ret = 0;
513533 fail:
514− av_freep(&map);
534+ stream_specifier_uninit(&ss);
515535 return ret;
516536 }
517537

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t