+static void
+real_parse_asm_rule(AVStream *st, const char *p, const char *end)
+{
+ do {
+ /* can be either averagebandwidth= or AverageBandwidth= */
+ if (sscanf(p, " %*1[Aa]verage%*1[Bb]andwidth=%d", &st->codec->bit_rate) == 1)
+ break;
+ if (!(p = strchr(p, ',')) || p > end)
+ p = end;
+ p++;
+ } while (p < end);
+}
+
+static AVStream *
+add_dstream(AVFormatContext *s, AVStream *orig_st)
+{
+ AVStream *st;
+
+ if (!(st = avformat_new_stream(s, NULL)))
+ return NULL;
+ st->id = orig_st->id;
+ st->codec->codec_type = orig_st->codec->codec_type;
+ st->first_dts = orig_st->first_dts;
+
+ return st;
+}
+
+static void
+real_parse_asm_rulebook(AVFormatContext *s, AVStream *orig_st,
+ const char *p)
+{
+ const char *end;
+ int n_rules = 0, odd = 0;
+ AVStream *st;
+
+ /**
+ * The ASMRuleBook contains a list of comma-separated strings per rule,
+ * and each rule is separated by a ;. The last one also has a ; at the
+ * end so we can use it as delimiter.
+ * Every rule occurs twice, once for when the RTSP packet header marker
+ * is set and once for if it isn't. We only read the first because we
+ * don't care much (that's what the "odd" variable is for).
+ * Each rule contains a set of one or more statements, optionally
+ * preceded by a single condition. If there's a condition, the rule
+ * starts with a '#'. Multiple conditions are merged between brackets,
+ * so there are never multiple conditions spread out over separate
+ * statements. Generally, these conditions are bitrate limits (min/max)
+ * for multi-bitrate streams.
+ */
+ if (*p == '\"') p++;
+ while (1) {
+ if (!(end = strchr(p, ';')))
+ break;
+ if (!odd && end != p) {
+ if (n_rules > 0)
+ st = add_dstream(s, orig_st);
+ else
+ st = orig_st;
+ if (!st)
+ break;
+ real_parse_asm_rule(st, p, end);
+ n_rules++;
+ }
+ p = end + 1;
+ odd ^= 1;
+ }
+}
+
+void
+ff_real_parse_sdp_a_line (AVFormatContext *s, int stream_index,
+ const char *line)
+{
+ const char *p = line;
+
+ if (av_strstart(p, "ASMRuleBook:string;", &p))
+ real_parse_asm_rulebook(s, s->streams[stream_index], p);
+}
+