History

fftools/cmdutils: split stream specifier parsing and matching

This approach has the major advantage that only parsing can fail (due to a malformed specifier or memory allocation failure). Since parsing is done generically, while matching is per-option, this will allow to remove substantial amounts of error checking code in following commits. The new code also explicitly allows stream specifiers to be followed by additional characters, which should allow cleaner handling of optional maps, i.e. -map <stream_specifier>?, which is currently implemented in a hacky way that breaks when the stream specifier itself contains the '?' character (this can happen when matching metadata). It will also allow further extending the syntax, which will be useful in following commits. This introduces some minor behaviour changes: * Matching metadata tags now requires the ':' character in keys or values to be escaped. Previously it could not be present in keys, and would be used verbatim in values. The change is required in order to know where the value terminates. * Multiple stream types in a single specifier are now rejected - such a specifier makes no sense. * Non-existent stream group ID or index is now ignored with a warning rather than causing a failure. This is consistent with program handling and is required to make matching fail-free.

Anton Khirnov committed 2 years ago

Commit 46cbe4a1 parent e1b38682 files · +321 −173Browse files

Unified · Split

2 files
  1. fftools/cmdutils.c +265 −173
  2. fftools/cmdutils.h +56 −0
fftools/cmdutils.c+265−173
@@ -980,220 +980,312 @@ FILE *get_preset_file(char *filename, size_t filename_size, · 979 unmodified lines
980 return f;980 return f;
981}981}
982 982
983/**983
984 * Matches a stream specifier (but ignores requested index).984void stream_specifier_uninit(StreamSpecifier *ss)
985 *
986 * @param indexptr set to point to the requested stream index if there is one
987 *
988 * @return <0 on error
989 * 0 if st is NOT a matching stream
990 * >0 if st is a matching stream
991 */
992static int match_stream_specifier(const AVFormatContext *s, const AVStream *st,
993 const char *spec, const char **indexptr,
994 const AVStreamGroup **g, const AVProgram **p)
995{985{
996 int match = 1; /* Stores if the specifier matches so far. */986 av_freep(&ss->meta_key);
987 av_freep(&ss->meta_val);
988 av_freep(&ss->remainder);
989
990 memset(ss, 0, sizeof(*ss));
991}
992
993int stream_specifier_parse(StreamSpecifier *ss, const char *spec,
994 int allow_remainder, void *logctx)
995{
996 char *endptr;
997 int ret;
998
999 memset(ss, 0, sizeof(*ss));
1000
1001 ss->idx = -1;
1002 ss->media_type = AVMEDIA_TYPE_UNKNOWN;
1003 ss->stream_list = STREAM_LIST_ALL;
1004
1005 av_log(logctx, AV_LOG_TRACE, "Parsing stream specifier: %s\n", spec);
1006
997 while (*spec) {1007 while (*spec) {
998 if (*spec <= '9' && *spec >= '0') { /* opt:index */1008 if (*spec <= '9' && *spec >= '0') { /* opt:index */
999 if (indexptr)1009 ss->idx = strtol(spec, &endptr, 0);
1000 *indexptr = spec;1010
1001 return match;1011 av_assert0(endptr > spec);
1012 spec = endptr;
1013
1014 av_log(logctx, AV_LOG_TRACE,
1015 "Parsed index: %d; remainder: %s\n", ss->idx, spec);
1016
1017 // this terminates the specifier
1018 break;
1002 } else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||1019 } else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
1003 *spec == 't' || *spec == 'V') { /* opt:[vasdtV] */1020 *spec == 't' || *spec == 'V') { /* opt:[vasdtV] */
1004 enum AVMediaType type;1021 if (ss->media_type != AVMEDIA_TYPE_UNKNOWN) {
1005 int nopic = 0;1022 av_log(logctx, AV_LOG_ERROR, "Stream type specified multiple times\n");
1023 ret = AVERROR(EINVAL);
1024 goto fail;
1025 }
1006 1026
1007 switch (*spec++) {1027 switch (*spec++) {
1008 case 'v': type = AVMEDIA_TYPE_VIDEO; break;1028 case 'v': ss->media_type = AVMEDIA_TYPE_VIDEO; break;
1009 case 'a': type = AVMEDIA_TYPE_AUDIO; break;1029 case 'a': ss->media_type = AVMEDIA_TYPE_AUDIO; break;
1010 case 's': type = AVMEDIA_TYPE_SUBTITLE; break;1030 case 's': ss->media_type = AVMEDIA_TYPE_SUBTITLE; break;
1011 case 'd': type = AVMEDIA_TYPE_DATA; break;1031 case 'd': ss->media_type = AVMEDIA_TYPE_DATA; break;
1012 case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;1032 case 't': ss->media_type = AVMEDIA_TYPE_ATTACHMENT; break;
1013 case 'V': type = AVMEDIA_TYPE_VIDEO; nopic = 1; break;1033 case 'V': ss->media_type = AVMEDIA_TYPE_VIDEO;
1034 ss->no_apic = 1; break;
1014 default: av_assert0(0);1035 default: av_assert0(0);
1015 }1036 }
1016 if (*spec && *spec++ != ':') /* If we are not at the end, then another specifier must follow. */
1017 return AVERROR(EINVAL);
1018 1037
1019 if (type != st->codecpar->codec_type)1038 av_log(logctx, AV_LOG_TRACE, "Parsed media type: %s; remainder: %s\n",
1020 match = 0;1039 av_get_media_type_string(ss->media_type), spec);
1021 if (nopic && (st->disposition & AV_DISPOSITION_ATTACHED_PIC))
1022 match = 0;
1023 } else if (*spec == 'g' && *(spec + 1) == ':') {1040 } else if (*spec == 'g' && *(spec + 1) == ':') {
1024 int64_t group_idx = -1, group_id = -1;1041 if (ss->stream_list != STREAM_LIST_ALL)
1025 int found = 0;1042 goto multiple_stream_lists;
1026 char *endptr;1043
1027 spec += 2;1044 spec += 2;
1028 if (*spec == '#' || (*spec == 'i' && *(spec + 1) == ':')) {1045 if (*spec == '#' || (*spec == 'i' && *(spec + 1) == ':')) {
1046 ss->stream_list = STREAM_LIST_GROUP_ID;
1047
1029 spec += 1 + (*spec == 'i');1048 spec += 1 + (*spec == 'i');
1030 group_id = strtol(spec, &endptr, 0);1049 } else
1031 if (spec == endptr || (*endptr && *endptr++ != ':'))1050 ss->stream_list = STREAM_LIST_GROUP_IDX;
1032 return AVERROR(EINVAL);1051
1033 spec = endptr;1052 ss->list_id = strtol(spec, &endptr, 0);
1034 } else {1053 if (spec == endptr) {
1035 group_idx = strtol(spec, &endptr, 0);1054 av_log(logctx, AV_LOG_ERROR, "Expected stream group idx/ID, got: %s\n", spec);
1036 /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */1055 ret = AVERROR(EINVAL);
1037 if (spec == endptr || (*endptr && *endptr++ != ':'))1056 goto fail;
1038 return AVERROR(EINVAL);
1039 spec = endptr;
1040 }
1041 if (match) {
1042 if (group_id > 0) {
1043 for (unsigned i = 0; i < s->nb_stream_groups; i++) {
1044 if (group_id == s->stream_groups[i]->id) {
1045 group_idx = i;
1046 break;
1047 }
1048 }
1049 }
1050 if (group_idx < 0 || group_idx >= s->nb_stream_groups)
1051 return AVERROR(EINVAL);
1052 for (unsigned j = 0; j < s->stream_groups[group_idx]->nb_streams; j++) {
1053 if (st->index == s->stream_groups[group_idx]->streams[j]->index) {
1054 found = 1;
1055 if (g)
1056 *g = s->stream_groups[group_idx];
1057 break;
1058 }
1059 }
1060 }1057 }
1061 if (!found)1058 spec = endptr;
1062 match = 0;1059
1060 av_log(logctx, AV_LOG_TRACE, "Parsed stream group %s: %"PRId64"; remainder: %s\n",
1061 ss->stream_list == STREAM_LIST_GROUP_ID ? "ID" : "index", ss->list_id, spec);
1063 } else if (*spec == 'p' && *(spec + 1) == ':') {1062 } else if (*spec == 'p' && *(spec + 1) == ':') {
1064 int prog_id;1063 if (ss->stream_list != STREAM_LIST_ALL)
1065 int found = 0;1064 goto multiple_stream_lists;
1066 char *endptr;1065
1066 ss->stream_list = STREAM_LIST_PROGRAM;
1067
1067 spec += 2;1068 spec += 2;
1068 prog_id = strtol(spec, &endptr, 0);1069 ss->list_id = strtol(spec, &endptr, 0);
1069 /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */1070 if (spec == endptr) {
1070 if (spec == endptr || (*endptr && *endptr++ != ':'))1071 av_log(logctx, AV_LOG_ERROR, "Expected program ID, got: %s\n", spec);
1071 return AVERROR(EINVAL);1072 ret = AVERROR(EINVAL);
1072 spec = endptr;1073 goto fail;
1073 if (match) {
1074 for (unsigned i = 0; i < s->nb_programs; i++) {
1075 if (s->programs[i]->id != prog_id)
1076 continue;
1077
1078 for (unsigned j = 0; j < s->programs[i]->nb_stream_indexes; j++) {
1079 if (st->index == s->programs[i]->stream_index[j]) {
1080 found = 1;
1081 if (p)
1082 *p = s->programs[i];
1083 i = s->nb_programs;
1084 break;
1085 }
1086 }
1087 }
1088 }1074 }
1089 if (!found)1075 spec = endptr;
1090 match = 0;1076
1077 av_log(logctx, AV_LOG_TRACE,
1078 "Parsed program ID: %"PRId64"; remainder: %s\n", ss->list_id, spec);
1091 } else if (*spec == '#' ||1079 } else if (*spec == '#' ||
1092 (*spec == 'i' && *(spec + 1) == ':')) {1080 (*spec == 'i' && *(spec + 1) == ':')) {
1093 int stream_id;1081 if (ss->stream_list != STREAM_LIST_ALL)
1094 char *endptr;1082 goto multiple_stream_lists;
1083
1084 ss->stream_list = STREAM_LIST_STREAM_ID;
1085
1095 spec += 1 + (*spec == 'i');1086 spec += 1 + (*spec == 'i');
1096 stream_id = strtol(spec, &endptr, 0);1087 ss->list_id = strtol(spec, &endptr, 0);
1097 if (spec == endptr || *endptr) /* Disallow empty id and make sure we are at the end. */1088 if (spec == endptr) {
1098 return AVERROR(EINVAL);1089 av_log(logctx, AV_LOG_ERROR, "Expected stream ID, got: %s\n", spec);
1099 return match && (stream_id == st->id);1090 ret = AVERROR(EINVAL);
1091 goto fail;
1092 }
1093 spec = endptr;
1094
1095 av_log(logctx, AV_LOG_TRACE,
1096 "Parsed stream ID: %"PRId64"; remainder: %s\n", ss->list_id, spec);
1097
1098 // this terminates the specifier
1099 break;
1100 } else if (*spec == 'm' && *(spec + 1) == ':') {1100 } else if (*spec == 'm' && *(spec + 1) == ':') {
1101 const AVDictionaryEntry *tag;1101 av_assert0(!ss->meta_key && !ss->meta_val);
1102 char *key, *val;1102
1103 int ret;1103 spec += 2;
1104 1104 ss->meta_key = av_get_token(&spec, ":");
1105 if (match) {1105 if (!ss->meta_key) {
1106 spec += 2;1106 ret = AVERROR(ENOMEM);
1107 val = strchr(spec, ':');1107 goto fail;
1108
1109 key = val ? av_strndup(spec, val - spec) : av_strdup(spec);
1110 if (!key)
1111 return AVERROR(ENOMEM);
1112
1113 tag = av_dict_get(st->metadata, key, NULL, 0);
1114 if (tag) {
1115 if (!val || !strcmp(tag->value, val + 1))
1116 ret = 1;
1117 else
1118 ret = 0;
1119 } else
1120 ret = 0;
1121
1122 av_freep(&key);
1123 }1108 }
1124 return match && ret;1109 if (*spec == ':') {
1125 } else if (*spec == 'u' && *(spec + 1) == '\0') {1110 spec++;
1126 const AVCodecParameters *par = st->codecpar;1111 ss->meta_val = av_get_token(&spec, ":");
1127 int val;1112 if (!ss->meta_val) {
1113 ret = AVERROR(ENOMEM);
1114 goto fail;
1115 }
1116 }
1117
1118 av_log(logctx, AV_LOG_TRACE,
1119 "Parsed metadata: %s:%s; remainder: %s", ss->meta_key,
1120 ss->meta_val ? ss->meta_val : "<any value>", spec);
1121
1122 // this terminates the specifier
1123 break;
1124 } else if (*spec == 'u' && (*(spec + 1) == '\0' || *(spec + 1) == ':')) {
1125 ss->usable_only = 1;
1126 spec++;
1127 av_log(logctx, AV_LOG_ERROR, "Parsed 'usable only'\n");
1128
1129 // this terminates the specifier
1130 break;
1131 } else
1132 break;
1133
1134 if (*spec == ':')
1135 spec++;
1136 }
1137
1138 if (*spec) {
1139 if (!allow_remainder) {
1140 av_log(logctx, AV_LOG_ERROR,
1141 "Trailing garbage at the end of a stream specifier: %s\n",
1142 spec);
1143 ret = AVERROR(EINVAL);
1144 goto fail;
1145 }
1146
1147 if (*spec == ':')
1148 spec++;
1149
1150 ss->remainder = av_strdup(spec);
1151 if (!ss->remainder) {
1152 ret = AVERROR(EINVAL);
1153 goto fail;
1154 }
1155 }
1156
1157 return 0;
1158
1159multiple_stream_lists:
1160 av_log(logctx, AV_LOG_ERROR,
1161 "Cannot combine multiple program/group designators in a "
1162 "single stream specifier");
1163 ret = AVERROR(EINVAL);
1164
1165fail:
1166 stream_specifier_uninit(ss);
1167 return ret;
1168}
1169
1170unsigned stream_specifier_match(const StreamSpecifier *ss,
1171 const AVFormatContext *s, const AVStream *st,
1172 void *logctx)
1173{
1174 const AVStreamGroup *g = NULL;
1175 const AVProgram *p = NULL;
1176 int start_stream = 0, nb_streams;
1177 int nb_matched = 0;
1178
1179 switch (ss->stream_list) {
1180 case STREAM_LIST_STREAM_ID:
1181 // <n-th> stream with given ID makes no sense and should be impossible to request
1182 av_assert0(ss->idx < 0);
1183 // return early if we know for sure the stream does not match
1184 if (st->id != ss->list_id)
1185 return 0;
1186 start_stream = st->index;
1187 nb_streams = st->index + 1;
1188 break;
1189 case STREAM_LIST_ALL:
1190 start_stream = ss->idx >= 0 ? 0 : st->index;
1191 nb_streams = st->index + 1;
1192 break;
1193 case STREAM_LIST_PROGRAM:
1194 for (unsigned i = 0; i < s->nb_programs; i++) {
1195 if (s->programs[i]->id == ss->list_id) {
1196 p = s->programs[i];
1197 break;
1198 }
1199 }
1200 if (!p) {
1201 av_log(logctx, AV_LOG_WARNING, "No program with ID %"PRId64" exists,"
1202 " stream specifier can never match\n", ss->list_id);
1203 return 0;
1204 }
1205 nb_streams = p->nb_stream_indexes;
1206 break;
1207 case STREAM_LIST_GROUP_ID:
1208 for (unsigned i = 0; i < s->nb_stream_groups; i++) {
1209 if (ss->list_id == s->stream_groups[i]->id) {
1210 g = s->stream_groups[i];
1211 break;
1212 }
1213 }
1214 // fall-through
1215 case STREAM_LIST_GROUP_IDX:
1216 if (ss->stream_list == STREAM_LIST_GROUP_IDX &&
1217 ss->list_id >= 0 && ss->list_id < s->nb_stream_groups)
1218 g = s->stream_groups[ss->list_id];
1219
1220 if (!g) {
1221 av_log(logctx, AV_LOG_WARNING, "No stream group with group %s %"
1222 PRId64" exists, stream specifier can never match\n",
1223 ss->stream_list == STREAM_LIST_GROUP_ID ? "ID" : "index",
1224 ss->list_id);
1225 return 0;
1226 }
1227 nb_streams = g->nb_streams;
1228 break;
1229 default: av_assert0(0);
1230 }
1231
1232 for (int i = start_stream; i < nb_streams; i++) {
1233 const AVStream *candidate = s->streams[g ? g->streams[i]->index :
1234 p ? p->stream_index[i] : i];
1235
1236 if (ss->media_type != AVMEDIA_TYPE_UNKNOWN &&
1237 (ss->media_type != candidate->codecpar->codec_type ||
1238 (ss->no_apic && (candidate->disposition & AV_DISPOSITION_ATTACHED_PIC))))
1239 continue;
1240
1241 if (ss->meta_key) {
1242 const AVDictionaryEntry *tag = av_dict_get(candidate->metadata,
1243 ss->meta_key, NULL, 0);
1244
1245 if (!tag)
1246 continue;
1247 if (ss->meta_val && strcmp(tag->value, ss->meta_val))
1248 continue;
1249 }
1250
1251 if (ss->usable_only) {
1252 const AVCodecParameters *par = candidate->codecpar;
1253
1128 switch (par->codec_type) {1254 switch (par->codec_type) {
1129 case AVMEDIA_TYPE_AUDIO:1255 case AVMEDIA_TYPE_AUDIO:
1130 val = par->sample_rate && par->ch_layout.nb_channels;1256 if (!par->sample_rate || !par->ch_layout.nb_channels ||
1131 if (par->format == AV_SAMPLE_FMT_NONE)1257 par->format == AV_SAMPLE_FMT_NONE)
1132 return 0;1258 continue;
1133 break;1259 break;
1134 case AVMEDIA_TYPE_VIDEO:1260 case AVMEDIA_TYPE_VIDEO:
1135 val = par->width && par->height;1261 if (!par->width || !par->height || par->format == AV_PIX_FMT_NONE)
1136 if (par->format == AV_PIX_FMT_NONE)1262 continue;
1137 return 0;
1138 break;1263 break;
1139 case AVMEDIA_TYPE_UNKNOWN:1264 case AVMEDIA_TYPE_UNKNOWN:
1140 val = 0;1265 continue;
1141 break;
1142 default:
1143 val = 1;
1144 break;
1145 }1266 }
1146 return match && (par->codec_id != AV_CODEC_ID_NONE && val != 0);
1147 } else {
1148 return AVERROR(EINVAL);
1149 }1267 }
1268
1269 if (st == candidate)
1270 return ss->idx < 0 || ss->idx == nb_matched;
1271
1272 nb_matched++;
1150 }1273 }
1151 1274
1152 return match;1275 return 0;
1153}1276}
1154 1277
1155int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)1278int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
1156{1279{
1157 int ret, index;1280 StreamSpecifier ss;
1158 char *endptr;1281 int ret;
1159 const char *indexptr = NULL;
1160 const AVStreamGroup *g = NULL;
1161 const AVProgram *p = NULL;
1162 int nb_streams;
1163 1282
1164 ret = match_stream_specifier(s, st, spec, &indexptr, &g, &p);1283 ret = stream_specifier_parse(&ss, spec, 0, NULL);
1165 if (ret < 0)1284 if (ret < 0)
1166 goto error;
1167
1168 if (!indexptr)
1169 return ret;1285 return ret;
1170 1286
1171 index = strtol(indexptr, &endptr, 0);1287 ret = stream_specifier_match(&ss, s, st, NULL);
1172 if (*endptr) { /* We can't have anything after the requested index. */1288 stream_specifier_uninit(&ss);
1173 ret = AVERROR(EINVAL);
1174 goto error;
1175 }
1176
1177 /* This is not really needed but saves us a loop for simple stream index specifiers. */
1178 if (spec == indexptr)
1179 return (index == st->index);
1180
1181 /* If we requested a matching stream index, we have to ensure st is that. */
1182 nb_streams = g ? g->nb_streams : (p ? p->nb_stream_indexes : s->nb_streams);
1183 for (int i = 0; i < nb_streams && index >= 0; i++) {
1184 unsigned idx = g ? g->streams[i]->index : (p ? p->stream_index[i] : i);
1185 const AVStream *candidate = s->streams[idx];
1186 ret = match_stream_specifier(s, candidate, spec, NULL, NULL, NULL);
1187 if (ret < 0)
1188 goto error;
1189 if (ret > 0 && index-- == 0 && st == candidate)
1190 return 1;
1191 }
1192 return 0;
1193
1194error:
1195 if (ret == AVERROR(EINVAL))
1196 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
1197 return ret;1289 return ret;
1198}1290}
1199 1291
fftools/cmdutils.h+56−0
@@ -102,6 +102,62 @@ enum OptionType { · 101 unmodified lines
102int parse_number(const char *context, const char *numstr, enum OptionType type,102int parse_number(const char *context, const char *numstr, enum OptionType type,
103 double min, double max, double *dst);103 double min, double max, double *dst);
104 104
105enum StreamList {
106 STREAM_LIST_ALL,
107 STREAM_LIST_STREAM_ID,
108 STREAM_LIST_PROGRAM,
109 STREAM_LIST_GROUP_ID,
110 STREAM_LIST_GROUP_IDX,
111};
112
113typedef struct StreamSpecifier {
114 // trailing stream index - pick idx-th stream that matches
115 // all the other constraints; -1 when not present
116 int idx;
117
118 // which stream list to consider
119 enum StreamList stream_list;
120
121 // STREAM_LIST_STREAM_ID: stream ID
122 // STREAM_LIST_GROUP_IDX: group index
123 // STREAM_LIST_GROUP_ID: group ID
124 // STREAM_LIST_PROGRAM: program ID
125 int64_t list_id;
126
127 // when not AVMEDIA_TYPE_UNKNOWN, consider only streams of this type
128 enum AVMediaType media_type;
129 uint8_t no_apic;
130
131 uint8_t usable_only;
132
133 char *meta_key;
134 char *meta_val;
135
136 char *remainder;
137} StreamSpecifier;
138
139/**
140 * Parse a stream specifier string into a form suitable for matching.
141 *
142 * @param ss Parsed specifier will be stored here; must be uninitialized
143 * with stream_specifier_uninit() when no longer needed.
144 * @param spec String containing the stream specifier to be parsed.
145 * @param allow_remainder When 1, the part of spec that is left after parsing
146 * the stream specifier is stored into ss->remainder.
147 * When 0, any remainder will cause parsing to fail.
148 */
149int stream_specifier_parse(StreamSpecifier *ss, const char *spec,
150 int allow_remainder, void *logctx);
151
152/**
153 * @return 1 if st matches the parsed specifier, 0 if it does not
154 */
155unsigned stream_specifier_match(const StreamSpecifier *ss,
156 const AVFormatContext *s, const AVStream *st,
157 void *logctx);
158
159void stream_specifier_uninit(StreamSpecifier *ss);
160
105typedef struct SpecifierOpt {161typedef struct SpecifierOpt {
106 char *specifier; /**< stream/chapter/program/... specifier */162 char *specifier; /**< stream/chapter/program/... specifier */
107 union {163 union {

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