java-topology/docs/tickets/ffmpeg-0001-codec-tag-linear-scan-per-stream.md
russell@unturf.com 9934133dcf whitepaper: 312 sites / 151 ecosystems — wave2+3 defect tables and PDF rebuild
Add 88 new defect entries to HIGH and MEDIUM tables:
  HIGH: mysql-0001/0002, mariadb-0001, redis-0001/0002, valkey-0001/0002, openvpn-0001,
        vlc-0001, prometheus-0001, otel-collector-0001, cockroachdb-0001..0004,
        tidb-0001..0008, kubernetes-0001/0002, go-0001, kotlin-0002, scala-0001,
        allegro5-0001, sdl2-0001, grafana-0001, clickhouse-0001, duckdb-0001,
        mongodb-0001, envoy-0001, istio-0001, cilium-0001, linkerd2-0001,
        linux-0001/0002/0003, tor-0002/0003, curl-0001, julia-0001, lua-0001,
        perl5-0001, nats-0001, spring-0003/0004, tomcat-0001, onos-0002, odl-0002

  MEDIUM: helm-0001, mariadb-0002, openssl-0001/0002, memcached-0001,
          cassandra-0001..0004, flink-0001, storm-0001/0002, zookeeper-0001..0003,
          pip-0001, gradle-0001, nginx-0001, haproxy-0001, caddy-0001, varnish-0001,
          ffmpeg-0001, gstreamer-0001, raylib-0001, love2d-0001, php-0001/0002,
          r-source-0001, cpython-0002, ruby-0001, rabbitmq-0003/0004, activemq-0001,
          ovs-0001, onos-0003, odl-0002, jetty-0001

PDF: 976K
2026-03-27 15:23:43 -04:00

2.1 KiB
Raw Permalink Blame History

ffmpeg-0001 — Codec Tag Linear Scan Per Stream (CWE-407)

Status: PATCHED Severity: MEDIUM Target: FFmpeg libavformat/utils.c Functions: ff_codec_get_tag(), ff_codec_get_id(), av_codec_get_tag2()

Defect

ff_codec_get_tag() performs an O(N) linear scan over a static AVCodecTag[] array to map a codec ID to a 4-byte FourCC tag. The arrays are large:

Table Entries
ff_codec_movvideo_tags 239
ff_codec_bmp_tags 460
ff_codec_movaudio_tags 60
ff_codec_wav_tags 85

This function is called for every stream in every muxer hot path:

  • movenc.c:2135mov_get_codec_tag() calls it 23 times per video/audio track
  • matroskaenc.c:1251,1262,1267,1281 — Matroska mux does the same
  • flvenc.c:260,1012,1296 — FLV encoder
  • cafenc.c:120,153 — CAF encoder
  • au.c:295 — AU encoder

When multiplexing a file with many streams (e.g., a playlist transcode or broadcast ingest with 50+ streams), this becomes O(S × N) where S = stream count and N = table size (up to 460).

ff_codec_get_id() is worse — it scans the array twice (once exact, once case-insensitive via ff_toupper4) for a total of O(2N) per call.

Root Cause

AVCodecTag arrays are statically allocated flat arrays with sentinel AV_CODEC_ID_NONE terminators. No hash index is built at startup. Both directions (id→tag and tag→id) are O(N) linear scans.

Fix

Build a uint32_t → AVCodecID and AVCodecID → uint32_t hash map at program startup (or lazily on first call) keyed on codec ID / FourCC tag. The arrays are read-only after init; one-time O(N) build cost, then O(1) lookups.

Patch: defects/ffmpeg/patch/ffmpeg-0001.patch

Complexity

Before After
ff_codec_get_tag O(N), N≤460 O(1) amortized
ff_codec_get_id O(2N) O(1)
av_codec_get_tag2 O(T×N) T=table count O(1)

Benchmark

See defects/ffmpeg/unit/FFmpegCodecTagTest.java — at N=500 (synthetic), the linear scan executes ≥100× more comparisons than the hash map.