amf_parse_object() stores every scalar onMetaData entry via av_dict_set(), which scans the whole dictionary for duplicates and is therefore O(n) per call. An FLV script tag carries an unbounded number of ECMA-array entries (bounded only by the 24-bit tag size), so parsing is quadratic in the entry count: a 1.7 MB file with 100k keys takes ~111 s of CPU, and a 16 MB file with ~1M keys extrapolates to ~3 h. Cap the stored entries at 1024 (real files carry only a handful of keys) while still parsing the values, so stream detection is unaffected and parsing becomes linear in the entry count.
Signed-off-by: Mamat9q <[email protected]> --- libavformat/flvdec.c | 45 +++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/libavformat/flvdec.c b/libavformat/flvdec.c index 5e742c8..6a5810a 100644 --- a/libavformat/flvdec.c +++ b/libavformat/flvdec.c @@ -46,6 +46,7 @@ #define RESYNC_BUFFER_SIZE (1<<20) #define MAX_DEPTH 16 ///< arbitrary limit to prevent unbounded recursion +#define FLV_MAX_METADATA_ENTRIES 1024 ///< cap on onMetaData entries stored; av_dict_set() is O(n) typedef struct FLVMasteringMeta { float r_x; @@ -831,23 +832,33 @@ static int amf_parse_object(AVFormatContext *s, AVStream *astream, return 0; s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED; - if (amf_type == AMF_DATA_TYPE_BOOL) { - av_strlcpy(str_val, num_val > 0 ? "true" : "false", - sizeof(str_val)); - av_dict_set(&s->metadata, key, str_val, 0); - } else if (amf_type == AMF_DATA_TYPE_NUMBER) { - snprintf(str_val, sizeof(str_val), "%.f", num_val); - av_dict_set(&s->metadata, key, str_val, 0); - } else if (amf_type == AMF_DATA_TYPE_STRING) { - av_dict_set(&s->metadata, key, str_val, 0); - } else if ( amf_type == AMF_DATA_TYPE_DATE - && isfinite(date.milliseconds) - && date.milliseconds > INT64_MIN/1000 - && date.milliseconds < INT64_MAX/1000 - ) { - // timezone is ignored, since there is no easy way to offset the UTC - // timestamp into the specified timezone - ff_dict_set_timestamp(&s->metadata, key, 1000 * (int64_t)date.milliseconds); + /* Cap the number of metadata entries stored from AMF data. + * av_dict_set() scans the whole dictionary for duplicates, so + * it is O(n) per call; an onMetaData object with an unbounded + * number of entries makes parsing quadratic in the entry count + * (a ~2 MB FLV with 100k keys stalls the demuxer for ~2 min). + * Real files carry only a handful of onMetaData keys. Stop + * storing beyond the cap but keep parsing the values, so stream + * detection (handled above) is unaffected. */ + if (av_dict_count(s->metadata) < FLV_MAX_METADATA_ENTRIES) { + if (amf_type == AMF_DATA_TYPE_BOOL) { + av_strlcpy(str_val, num_val > 0 ? "true" : "false", + sizeof(str_val)); + av_dict_set(&s->metadata, key, str_val, 0); + } else if (amf_type == AMF_DATA_TYPE_NUMBER) { + snprintf(str_val, sizeof(str_val), "%.f", num_val); + av_dict_set(&s->metadata, key, str_val, 0); + } else if (amf_type == AMF_DATA_TYPE_STRING) { + av_dict_set(&s->metadata, key, str_val, 0); + } else if ( amf_type == AMF_DATA_TYPE_DATE + && isfinite(date.milliseconds) + && date.milliseconds > INT64_MIN/1000 + && date.milliseconds < INT64_MAX/1000 + ) { + // timezone is ignored, since there is no easy way to offset the UTC + // timestamp into the specified timezone + ff_dict_set_timestamp(&s->metadata, key, 1000 * (int64_t)date.milliseconds); + } } } -- 2.43.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
