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.
Unified · Split
| @@ -427,22 +427,20 @@ static int opt_map(void *optctx, const char *opt, const char *arg) · 426 unmodified lines | ||
| 427 | 427 | { |
| 428 | 428 | OptionsContext *o = optctx; |
| 429 | 429 | StreamMap *m = NULL; |
| 430 | + StreamSpecifier ss; | |
| 430 | 431 | 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)); | |
| 434 | 435 | |
| 435 | 436 | if (*arg == '-') { |
| 436 | 437 | negative = 1; |
| 437 | 438 | arg++; |
| 438 | 439 | } |
| 439 | − map = av_strdup(arg); | |
| 440 | − if (!map) | |
| 441 | − return AVERROR(ENOMEM); | |
| 442 | 440 | |
| 443 | − if (map[0] == '[') { | |
| 441 | + if (arg[0] == '[') { | |
| 444 | 442 | /* this mapping refers to lavfi output */ |
| 445 | − const char *c = map + 1; | |
| 443 | + const char *c = arg + 1; | |
| 446 | 444 | |
| 447 | 445 | ret = GROW_ARRAY(o->stream_maps, o->nb_stream_maps); |
| 448 | 446 | if (ret < 0) |
| @@ -451,33 +449,55 @@ static int opt_map(void *optctx, const char *opt, const char *arg) · 2 unmodified lines | ||
| 451 | 449 | m = &o->stream_maps[o->nb_stream_maps - 1]; |
| 452 | 450 | m->linklabel = av_get_token(&c, "]"); |
| 453 | 451 | 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); | |
| 455 | 453 | ret = AVERROR(EINVAL); |
| 456 | 454 | goto fail; |
| 457 | 455 | } |
| 458 | 456 | } 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); | |
| 462 | 460 | if (file_idx >= nb_input_files || file_idx < 0) { |
| 463 | 461 | av_log(NULL, AV_LOG_FATAL, "Invalid input file index: %d.\n", file_idx); |
| 464 | 462 | ret = AVERROR(EINVAL); |
| 465 | 463 | goto fail; |
| 466 | 464 | } |
| 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 | + | |
| 467 | 484 | if (negative) |
| 468 | 485 | /* disable some already defined maps */ |
| 469 | 486 | for (i = 0; i < o->nb_stream_maps; i++) { |
| 470 | 487 | m = &o->stream_maps[i]; |
| 471 | 488 | 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, | |
| 473 | 491 | input_files[m->file_index]->ctx->streams[m->stream_index], |
| 474 | − *p == ':' ? p + 1 : p) > 0) | |
| 492 | + NULL)) | |
| 475 | 493 | m->disabled = 1; |
| 476 | 494 | } |
| 477 | 495 | else |
| 478 | 496 | 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)) | |
| 481 | 501 | continue; |
| 482 | 502 | if (input_files[file_idx]->streams[i]->user_set_discard == AVDISCARD_ALL) { |
| 483 | 503 | disabled = 1; |
| @@ -511,7 +531,7 @@ static int opt_map(void *optctx, const char *opt, const char *arg) · 27 unmodified lines | ||
| 511 | 531 | } |
| 512 | 532 | ret = 0; |
| 513 | 533 | fail: |
| 514 | − av_freep(&map); | |
| 534 | + stream_specifier_uninit(&ss); | |
| 515 | 535 | return ret; |
| 516 | 536 | } |
| 517 | 537 | |