History

fftools/cmdutils: put stream specifier handling back into cmdutils

Stream specifiers were originally designed exclusively for CLI use and were not intended to be public API. Handling them in avformat places major restrictions on how they are used. E.g. if ffmpeg CLI wishes to override some stream parameters, it has to change the demuxer fields (since avformat_match_stream_specifier() does not have access to anything else). However, such fields are supposed to be read-only for the caller. Furthermore having this code in avformat restricts extending the specifier syntax. An example of such an extension will be added in following commits.

Anton Khirnov committed 2 years ago

Commit e1b38681 parent 93227a61 file · +210 −1Browse files

Unified · Split

fftools/cmdutils.c+210−1
@@ -980,10 +980,219 @@ FILE *get_preset_file(char *filename, size_t filename_size, · 979 unmodified lines
980 return f;980 return f;
981}981}
982 982
983/**
984 * Matches a stream specifier (but ignores requested index).
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{
996 int match = 1; /* Stores if the specifier matches so far. */
997 while (*spec) {
998 if (*spec <= '9' && *spec >= '0') { /* opt:index */
999 if (indexptr)
1000 *indexptr = spec;
1001 return match;
1002 } else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
1003 *spec == 't' || *spec == 'V') { /* opt:[vasdtV] */
1004 enum AVMediaType type;
1005 int nopic = 0;
1006
1007 switch (*spec++) {
1008 case 'v': type = AVMEDIA_TYPE_VIDEO; break;
1009 case 'a': type = AVMEDIA_TYPE_AUDIO; break;
1010 case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
1011 case 'd': type = AVMEDIA_TYPE_DATA; break;
1012 case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
1013 case 'V': type = AVMEDIA_TYPE_VIDEO; nopic = 1; break;
1014 default: av_assert0(0);
1015 }
1016 if (*spec && *spec++ != ':') /* If we are not at the end, then another specifier must follow. */
1017 return AVERROR(EINVAL);
1018
1019 if (type != st->codecpar->codec_type)
1020 match = 0;
1021 if (nopic && (st->disposition & AV_DISPOSITION_ATTACHED_PIC))
1022 match = 0;
1023 } else if (*spec == 'g' && *(spec + 1) == ':') {
1024 int64_t group_idx = -1, group_id = -1;
1025 int found = 0;
1026 char *endptr;
1027 spec += 2;
1028 if (*spec == '#' || (*spec == 'i' && *(spec + 1) == ':')) {
1029 spec += 1 + (*spec == 'i');
1030 group_id = strtol(spec, &endptr, 0);
1031 if (spec == endptr || (*endptr && *endptr++ != ':'))
1032 return AVERROR(EINVAL);
1033 spec = endptr;
1034 } else {
1035 group_idx = strtol(spec, &endptr, 0);
1036 /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */
1037 if (spec == endptr || (*endptr && *endptr++ != ':'))
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 }
1061 if (!found)
1062 match = 0;
1063 } else if (*spec == 'p' && *(spec + 1) == ':') {
1064 int prog_id;
1065 int found = 0;
1066 char *endptr;
1067 spec += 2;
1068 prog_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 || (*endptr && *endptr++ != ':'))
1071 return AVERROR(EINVAL);
1072 spec = endptr;
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 }
1089 if (!found)
1090 match = 0;
1091 } else if (*spec == '#' ||
1092 (*spec == 'i' && *(spec + 1) == ':')) {
1093 int stream_id;
1094 char *endptr;
1095 spec += 1 + (*spec == 'i');
1096 stream_id = strtol(spec, &endptr, 0);
1097 if (spec == endptr || *endptr) /* Disallow empty id and make sure we are at the end. */
1098 return AVERROR(EINVAL);
1099 return match && (stream_id == st->id);
1100 } else if (*spec == 'm' && *(spec + 1) == ':') {
1101 const AVDictionaryEntry *tag;
1102 char *key, *val;
1103 int ret;
1104
1105 if (match) {
1106 spec += 2;
1107 val = strchr(spec, ':');
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 }
1124 return match && ret;
1125 } else if (*spec == 'u' && *(spec + 1) == '\0') {
1126 const AVCodecParameters *par = st->codecpar;
1127 int val;
1128 switch (par->codec_type) {
1129 case AVMEDIA_TYPE_AUDIO:
1130 val = par->sample_rate && par->ch_layout.nb_channels;
1131 if (par->format == AV_SAMPLE_FMT_NONE)
1132 return 0;
1133 break;
1134 case AVMEDIA_TYPE_VIDEO:
1135 val = par->width && par->height;
1136 if (par->format == AV_PIX_FMT_NONE)
1137 return 0;
1138 break;
1139 case AVMEDIA_TYPE_UNKNOWN:
1140 val = 0;
1141 break;
1142 default:
1143 val = 1;
1144 break;
1145 }
1146 return match && (par->codec_id != AV_CODEC_ID_NONE && val != 0);
1147 } else {
1148 return AVERROR(EINVAL);
1149 }
1150 }
1151
1152 return match;
1153}
1154
983int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)1155int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
984{1156{
985 int ret = avformat_match_stream_specifier(s, st, spec);1157 int ret, index;
1158 char *endptr;
1159 const char *indexptr = NULL;
1160 const AVStreamGroup *g = NULL;
1161 const AVProgram *p = NULL;
1162 int nb_streams;
1163
1164 ret = match_stream_specifier(s, st, spec, &indexptr, &g, &p);
986 if (ret < 0)1165 if (ret < 0)
1166 goto error;
1167
1168 if (!indexptr)
1169 return ret;
1170
1171 index = strtol(indexptr, &endptr, 0);
1172 if (*endptr) { /* We can't have anything after the requested index. */
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))
987 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);1196 av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
988 return ret;1197 return ret;
989}1198}

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