diff --git a/compat/w32dlfcn.h b/compat/w32dlfcn.h
index fb1aa1b72e..ac20e83a7a 100644
--- a/compat/w32dlfcn.h
+++ b/compat/w32dlfcn.h
@@ -26,6 +26,7 @@
 
 #include "config.h"
 #include "libavutil/macros.h"
+#include "libavutil/mem.h"
 #include "libavutil/wchar_filename.h"
 
 static inline wchar_t *get_module_filename(HMODULE module)
diff --git a/doc/examples/avio_read_callback.c b/doc/examples/avio_read_callback.c
index 4cf81ad72e..dffc061d9f 100644
--- a/doc/examples/avio_read_callback.c
+++ b/doc/examples/avio_read_callback.c
@@ -32,6 +32,7 @@
 #include <libavformat/avformat.h>
 #include <libavformat/avio.h>
 #include <libavutil/file.h>
+#include <libavutil/mem.h>
 
 struct buffer_data {
     uint8_t *ptr;
diff --git a/doc/examples/decode_filter_audio.c b/doc/examples/decode_filter_audio.c
index 2046419819..13bceb3fd0 100644
--- a/doc/examples/decode_filter_audio.c
+++ b/doc/examples/decode_filter_audio.c
@@ -37,6 +37,7 @@
 #include <libavfilter/buffersink.h>
 #include <libavfilter/buffersrc.h>
 #include <libavutil/channel_layout.h>
+#include <libavutil/mem.h>
 #include <libavutil/opt.h>
 
 static const char *filter_descr = "aresample=8000,aformat=sample_fmts=s16:channel_layouts=mono";
diff --git a/doc/examples/decode_filter_video.c b/doc/examples/decode_filter_video.c
index 454c19222f..4c7528702f 100644
--- a/doc/examples/decode_filter_video.c
+++ b/doc/examples/decode_filter_video.c
@@ -36,6 +36,7 @@
 #include <libavformat/avformat.h>
 #include <libavfilter/buffersink.h>
 #include <libavfilter/buffersrc.h>
+#include <libavutil/mem.h>
 #include <libavutil/opt.h>
 
 const char *filter_descr = "scale=78:24,transpose=cclock";
diff --git a/doc/examples/hw_decode.c b/doc/examples/hw_decode.c
index 6a4a4fb83d..ac4e445505 100644
--- a/doc/examples/hw_decode.c
+++ b/doc/examples/hw_decode.c
@@ -35,6 +35,7 @@
 
 #include <libavcodec/avcodec.h>
 #include <libavformat/avformat.h>
+#include <libavutil/mem.h>
 #include <libavutil/pixdesc.h>
 #include <libavutil/hwcontext.h>
 #include <libavutil/opt.h>
diff --git a/doc/examples/qsv_transcode.c b/doc/examples/qsv_transcode.c
index 972126800b..8e7d2899f1 100644
--- a/doc/examples/qsv_transcode.c
+++ b/doc/examples/qsv_transcode.c
@@ -38,6 +38,7 @@
 #include <errno.h>
 
 #include <libavutil/hwcontext.h>
+#include <libavutil/mem.h>
 #include <libavcodec/avcodec.h>
 #include <libavformat/avformat.h>
 #include <libavutil/opt.h>
diff --git a/doc/examples/remux.c b/doc/examples/remux.c
index ecf30489f1..1f002987c5 100644
--- a/doc/examples/remux.c
+++ b/doc/examples/remux.c
@@ -28,6 +28,7 @@
  * input to the output without transcoding.
  */
 
+#include <libavutil/mem.h>
 #include <libavutil/timestamp.h>
 #include <libavformat/avformat.h>
 
diff --git a/doc/examples/transcode.c b/doc/examples/transcode.c
index a544ec0340..cbe5088ef6 100644
--- a/doc/examples/transcode.c
+++ b/doc/examples/transcode.c
@@ -35,6 +35,7 @@
 #include <libavfilter/buffersink.h>
 #include <libavfilter/buffersrc.h>
 #include <libavutil/channel_layout.h>
+#include <libavutil/mem.h>
 #include <libavutil/opt.h>
 #include <libavutil/pixdesc.h>
 
diff --git a/doc/examples/transcode_aac.c b/doc/examples/transcode_aac.c
index 84caf612ac..20de08d93b 100644
--- a/doc/examples/transcode_aac.c
+++ b/doc/examples/transcode_aac.c
@@ -29,6 +29,7 @@
 
 #include <stdio.h>
 
+#include <libavutil/mem.h>
 #include <libavformat/avformat.h>
 #include <libavformat/avio.h>
 
diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c
index f3c258bb99..653d39f717 100644
--- a/fftools/cmdutils.c
+++ b/fftools/cmdutils.c
@@ -33,17 +33,14 @@
 #include "compat/va_copy.h"
 #include "libavformat/avformat.h"
 #include "libswscale/swscale.h"
-#include "libswscale/version.h"
 #include "libswresample/swresample.h"
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 #include "libavutil/bprint.h"
-#include "libavutil/channel_layout.h"
 #include "libavutil/display.h"
 #include "libavutil/getenv_utf8.h"
-#include "libavutil/mathematics.h"
-#include "libavutil/imgutils.h"
 #include "libavutil/libm.h"
+#include "libavutil/mem.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/eval.h"
 #include "libavutil/dict.h"
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index d4e5f978f1..c3ec1b9e8a 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -68,40 +68,19 @@
 #include <conio.h>
 #endif
 
-#include "libavutil/avassert.h"
-#include "libavutil/avstring.h"
 #include "libavutil/bprint.h"
-#include "libavutil/channel_layout.h"
 #include "libavutil/dict.h"
-#include "libavutil/display.h"
-#include "libavutil/fifo.h"
-#include "libavutil/hwcontext.h"
-#include "libavutil/imgutils.h"
-#include "libavutil/intreadwrite.h"
-#include "libavutil/libm.h"
-#include "libavutil/mathematics.h"
-#include "libavutil/opt.h"
-#include "libavutil/parseutils.h"
-#include "libavutil/pixdesc.h"
-#include "libavutil/samplefmt.h"
-#include "libavutil/thread.h"
-#include "libavutil/threadmessage.h"
+#include "libavutil/mem.h"
 #include "libavutil/time.h"
-#include "libavutil/timestamp.h"
-
-#include "libavcodec/version.h"
 
 #include "libavformat/avformat.h"
 
 #include "libavdevice/avdevice.h"
 
-#include "libswresample/swresample.h"
-
 #include "cmdutils.h"
 #include "ffmpeg.h"
 #include "ffmpeg_sched.h"
 #include "ffmpeg_utils.h"
-#include "sync_queue.h"
 
 const char program_name[] = "ffmpeg";
 const int program_birth_year = 2000;
diff --git a/fftools/ffmpeg_dec.c b/fftools/ffmpeg_dec.c
index ed411b6bf8..3f5d37d1d3 100644
--- a/fftools/ffmpeg_dec.c
+++ b/fftools/ffmpeg_dec.c
@@ -21,6 +21,7 @@
 #include "libavutil/dict.h"
 #include "libavutil/error.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/pixfmt.h"
 #include "libavutil/time.h"
@@ -29,11 +30,7 @@
 #include "libavcodec/avcodec.h"
 #include "libavcodec/codec.h"
 
-#include "libavfilter/buffersrc.h"
-
 #include "ffmpeg.h"
-#include "ffmpeg_utils.h"
-#include "thread_queue.h"
 
 typedef struct DecoderPriv {
     Decoder             dec;
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index 47312c9fe1..0dac86e1c9 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -28,6 +28,7 @@
 #include "libavutil/display.h"
 #include "libavutil/error.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index 5f7fcf8a5f..589e26613f 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -20,8 +20,6 @@
 #include <stdint.h>
 
 #include "ffmpeg.h"
-#include "ffmpeg_utils.h"
-#include "thread_queue.h"
 
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
@@ -32,6 +30,7 @@
 #include "libavutil/frame.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/rational.h"
 #include "libavutil/time.h"
@@ -39,8 +38,6 @@
 
 #include "libavcodec/avcodec.h"
 
-#include "libavformat/avformat.h"
-
 struct Encoder {
     // combined size of all the packets received from the encoder
     uint64_t data_size;
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 3d88482d07..dcd849bdbf 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -30,11 +30,10 @@
 #include "libavutil/avstring.h"
 #include "libavutil/bprint.h"
 #include "libavutil/channel_layout.h"
-#include "libavutil/display.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/pixfmt.h"
-#include "libavutil/imgutils.h"
 #include "libavutil/samplefmt.h"
 #include "libavutil/time.h"
 #include "libavutil/timestamp.h"
diff --git a/fftools/ffmpeg_hw.c b/fftools/ffmpeg_hw.c
index 8608d24517..5d4c06c28e 100644
--- a/fftools/ffmpeg_hw.c
+++ b/fftools/ffmpeg_hw.c
@@ -18,9 +18,7 @@
 
 #include <string.h>
 
-#include "libavutil/avstring.h"
-#include "libavutil/pixdesc.h"
-#include "libavfilter/buffersink.h"
+#include "libavutil/mem.h"
 
 #include "ffmpeg.h"
 
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 4b3f9789ba..f764da1ed4 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -31,7 +31,6 @@
 #include "ffmpeg_sched.h"
 #include "cmdutils.h"
 #include "opt_common.h"
-#include "sync_queue.h"
 
 #include "libavformat/avformat.h"
 
@@ -43,16 +42,10 @@
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 #include "libavutil/avutil.h"
-#include "libavutil/bprint.h"
-#include "libavutil/channel_layout.h"
-#include "libavutil/display.h"
-#include "libavutil/intreadwrite.h"
-#include "libavutil/fifo.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
-#include "libavutil/pixdesc.h"
-#include "libavutil/pixfmt.h"
 
 HWDevice *filter_hw_device;
 
diff --git a/fftools/ffplay.c b/fftools/ffplay.c
index fcd1319ce7..d4300d5d46 100644
--- a/fftools/ffplay.c
+++ b/fftools/ffplay.c
@@ -25,7 +25,6 @@
 
 #include "config.h"
 #include "config_components.h"
-#include <inttypes.h>
 #include <math.h>
 #include <limits.h>
 #include <signal.h>
@@ -33,13 +32,11 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
-#include "libavutil/eval.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
-#include "libavutil/imgutils.h"
 #include "libavutil/dict.h"
 #include "libavutil/fifo.h"
-#include "libavutil/parseutils.h"
 #include "libavutil/samplefmt.h"
 #include "libavutil/time.h"
 #include "libavutil/bprint.h"
diff --git a/fftools/ffplay_renderer.c b/fftools/ffplay_renderer.c
index 73072a2851..80b700b3c5 100644
--- a/fftools/ffplay_renderer.c
+++ b/fftools/ffplay_renderer.c
@@ -42,6 +42,7 @@
 #include <SDL_vulkan.h>
 
 #include "libavutil/bprint.h"
+#include "libavutil/mem.h"
 
 #endif
 
diff --git a/fftools/ffprobe.c b/fftools/ffprobe.c
index 7d9998b428..0d4cd0b048 100644
--- a/fftools/ffprobe.c
+++ b/fftools/ffprobe.c
@@ -46,6 +46,7 @@
 #include "libavutil/mastering_display_metadata.h"
 #include "libavutil/hdr_dynamic_vivid_metadata.h"
 #include "libavutil/dovi_meta.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/spherical.h"
diff --git a/fftools/fopen_utf8.h b/fftools/fopen_utf8.h
index e5be84179a..1cfd158185 100644
--- a/fftools/fopen_utf8.h
+++ b/fftools/fopen_utf8.h
@@ -27,6 +27,7 @@
  * CRT, and FILE* handles can't be shared across them.) */
 
 #ifdef _WIN32
+#include "libavutil/mem.h"
 #include "libavutil/wchar_filename.h"
 
 static inline FILE *fopen_utf8(const char *path_utf8, const char *mode)
diff --git a/libavcodec/4xm.c b/libavcodec/4xm.c
index cfe65c178a..fd3a45f093 100644
--- a/libavcodec/4xm.c
+++ b/libavcodec/4xm.c
@@ -30,6 +30,7 @@
 #include "libavutil/frame.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/thread.h"
 #include "avcodec.h"
diff --git a/libavcodec/8svx.c b/libavcodec/8svx.c
index 0a6d311cf1..43567df3d7 100644
--- a/libavcodec/8svx.c
+++ b/libavcodec/8svx.c
@@ -40,6 +40,7 @@
 #include "config_components.h"
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
diff --git a/libavcodec/a64multienc.c b/libavcodec/a64multienc.c
index 26a9debc22..38edbe3a77 100644
--- a/libavcodec/a64multienc.c
+++ b/libavcodec/a64multienc.c
@@ -32,8 +32,8 @@
 #include "elbg.h"
 #include "encode.h"
 #include "libavutil/avassert.h"
-#include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 
 #define DITHERSTEPS   8
 #define CHARSET_CHARS 256
diff --git a/libavcodec/aacdec.c b/libavcodec/aacdec.c
index 97e51dd72a..b4870a6b1f 100644
--- a/libavcodec/aacdec.c
+++ b/libavcodec/aacdec.c
@@ -36,6 +36,7 @@
 #define TX_TYPE AV_TX_FLOAT_MDCT
 
 #include "libavutil/float_dsp.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "codec_internal.h"
diff --git a/libavcodec/aacdec_template.c b/libavcodec/aacdec_template.c
index aa856d7fe1..b2f069f83a 100644
--- a/libavcodec/aacdec_template.c
+++ b/libavcodec/aacdec_template.c
@@ -90,6 +90,7 @@
  */
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 #include "decode.h"
 #include "internal.h"
diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c
index 55fa307809..7feb723289 100644
--- a/libavcodec/aacenc.c
+++ b/libavcodec/aacenc.c
@@ -33,6 +33,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/libm.h"
 #include "libavutil/float_dsp.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "codec_internal.h"
diff --git a/libavcodec/aacpsy.c b/libavcodec/aacpsy.c
index 1fbd259e51..84c8375d3f 100644
--- a/libavcodec/aacpsy.c
+++ b/libavcodec/aacpsy.c
@@ -26,6 +26,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/ffmath.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
 #include "aac.h"
diff --git a/libavcodec/ac3_parser.c b/libavcodec/ac3_parser.c
index 13b8d3b7d8..4152fd4e01 100644
--- a/libavcodec/ac3_parser.c
+++ b/libavcodec/ac3_parser.c
@@ -20,10 +20,10 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "config.h"
 #include "config_components.h"
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "parser.h"
 #include "ac3defs.h"
 #include "ac3tab.h"
diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c
index 504c75cdd4..2d7e11c5b8 100644
--- a/libavcodec/ac3dec.c
+++ b/libavcodec/ac3dec.c
@@ -35,6 +35,7 @@
 #include "libavutil/crc.h"
 #include "libavutil/downmix_info.h"
 #include "libavutil/intmath.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/thread.h"
 #include "bswapdsp.h"
diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c
index ff43088df5..7a6bcf7900 100644
--- a/libavcodec/ac3enc.c
+++ b/libavcodec/ac3enc.c
@@ -35,6 +35,7 @@
 #include "libavutil/crc.h"
 #include "libavutil/emms.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 #include "libavutil/thread.h"
diff --git a/libavcodec/ac3enc_fixed.c b/libavcodec/ac3enc_fixed.c
index a9bbb0a04e..c399d6cd09 100644
--- a/libavcodec/ac3enc_fixed.c
+++ b/libavcodec/ac3enc_fixed.c
@@ -27,6 +27,7 @@
  */
 
 #define AC3ENC_FLOAT 0
+#include "libavutil/mem.h"
 #include "audiodsp.h"
 #include "ac3enc.h"
 #include "codec_internal.h"
diff --git a/libavcodec/ac3enc_float.c b/libavcodec/ac3enc_float.c
index 3462c5f474..24960f318b 100644
--- a/libavcodec/ac3enc_float.c
+++ b/libavcodec/ac3enc_float.c
@@ -27,6 +27,7 @@
  */
 
 #define AC3ENC_FLOAT 1
+#include "libavutil/mem.h"
 #include "audiodsp.h"
 #include "ac3enc.h"
 #include "codec_internal.h"
diff --git a/libavcodec/ac3enc_template.c b/libavcodec/ac3enc_template.c
index 34d07cc9e5..45dbc98804 100644
--- a/libavcodec/ac3enc_template.c
+++ b/libavcodec/ac3enc_template.c
@@ -32,6 +32,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #include "audiodsp.h"
diff --git a/libavcodec/adpcmenc.c b/libavcodec/adpcmenc.c
index 63afffc58f..4d7534e47f 100644
--- a/libavcodec/adpcmenc.c
+++ b/libavcodec/adpcmenc.c
@@ -24,6 +24,7 @@
 
 #include "config_components.h"
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/adts_parser.c b/libavcodec/adts_parser.c
index f2e155fc99..6c22c86ef2 100644
--- a/libavcodec/adts_parser.c
+++ b/libavcodec/adts_parser.c
@@ -21,6 +21,7 @@
 #include <stddef.h>
 #include <stdint.h>
 
+#include "libavutil/mem.h"
 #include "adts_header.h"
 #include "adts_parser.h"
 
diff --git a/libavcodec/agm.c b/libavcodec/agm.c
index 88272cc3aa..fdf896d61a 100644
--- a/libavcodec/agm.c
+++ b/libavcodec/agm.c
@@ -24,6 +24,7 @@
 
 #define BITSTREAM_READER_LE
 
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/aic.c b/libavcodec/aic.c
index f8b0f60354..440c399049 100644
--- a/libavcodec/aic.c
+++ b/libavcodec/aic.c
@@ -22,6 +22,7 @@
 
 #include <inttypes.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/alac.c b/libavcodec/alac.c
index 538d1e5984..f91288e97c 100644
--- a/libavcodec/alac.c
+++ b/libavcodec/alac.c
@@ -48,6 +48,7 @@
 #include <inttypes.h>
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "get_bits.h"
diff --git a/libavcodec/alacenc.c b/libavcodec/alacenc.c
index f43adb57f9..caac624217 100644
--- a/libavcodec/alacenc.c
+++ b/libavcodec/alacenc.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/alsdec.c b/libavcodec/alsdec.c
index c64d1032a4..be72994432 100644
--- a/libavcodec/alsdec.c
+++ b/libavcodec/alsdec.c
@@ -37,10 +37,10 @@
 #include "decode.h"
 #include "internal.h"
 #include "mlz.h"
+#include "libavutil/mem.h"
 #include "libavutil/samplefmt.h"
 #include "libavutil/crc.h"
 #include "libavutil/softfloat_ieee754.h"
-#include "libavutil/intfloat.h"
 #include "libavutil/intreadwrite.h"
 
 #include <stdint.h>
diff --git a/libavcodec/amfenc_av1.c b/libavcodec/amfenc_av1.c
index 3878f0d461..9f18aac648 100644
--- a/libavcodec/amfenc_av1.c
+++ b/libavcodec/amfenc_av1.c
@@ -17,10 +17,10 @@
  */
 
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "amfenc.h"
 #include "codec_internal.h"
-#include "internal.h"
 
 #define OFFSET(x) offsetof(AmfContext, x)
 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
diff --git a/libavcodec/amfenc_h264.c b/libavcodec/amfenc_h264.c
index c1d5f4054e..abfac2a90f 100644
--- a/libavcodec/amfenc_h264.c
+++ b/libavcodec/amfenc_h264.c
@@ -18,10 +18,10 @@
 
 
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "amfenc.h"
 #include "codec_internal.h"
-#include "internal.h"
 #include <AMF/components/PreAnalysis.h>
 
 #define OFFSET(x) offsetof(AmfContext, x)
diff --git a/libavcodec/amfenc_hevc.c b/libavcodec/amfenc_hevc.c
index 33a167aa52..0a74ccd6c4 100644
--- a/libavcodec/amfenc_hevc.c
+++ b/libavcodec/amfenc_hevc.c
@@ -17,10 +17,10 @@
  */
 
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "amfenc.h"
 #include "codec_internal.h"
-#include "internal.h"
 #include <AMF/components/PreAnalysis.h>
 
 #define OFFSET(x) offsetof(AmfContext, x)
diff --git a/libavcodec/apac.c b/libavcodec/apac.c
index b6cb6c669e..f740fb5553 100644
--- a/libavcodec/apac.c
+++ b/libavcodec/apac.c
@@ -19,8 +19,7 @@
  */
 
 #include "libavutil/audio_fifo.h"
-#include "libavutil/internal.h"
-#include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
diff --git a/libavcodec/apedec.c b/libavcodec/apedec.c
index 0f48bdff77..86b79182a7 100644
--- a/libavcodec/apedec.c
+++ b/libavcodec/apedec.c
@@ -25,6 +25,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/crc.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "lossless_audiodsp.h"
 #include "avcodec.h"
diff --git a/libavcodec/ass.c b/libavcodec/ass.c
index 5058dc8337..5ff4ac8e07 100644
--- a/libavcodec/ass.c
+++ b/libavcodec/ass.c
@@ -23,7 +23,7 @@
 #include "ass.h"
 #include "libavutil/avstring.h"
 #include "libavutil/bprint.h"
-#include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "version.h"
 
 int ff_ass_subtitle_header_full(AVCodecContext *avctx,
diff --git a/libavcodec/atrac1.c b/libavcodec/atrac1.c
index 1309bb95a2..cdcc7a669e 100644
--- a/libavcodec/atrac1.c
+++ b/libavcodec/atrac1.c
@@ -31,6 +31,7 @@
 #include <math.h>
 
 #include "libavutil/float_dsp.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/tx.h"
 
diff --git a/libavcodec/atrac3.c b/libavcodec/atrac3.c
index fad2299aaf..39ddfd9e45 100644
--- a/libavcodec/atrac3.c
+++ b/libavcodec/atrac3.c
@@ -38,6 +38,7 @@
 #include "libavutil/attributes.h"
 #include "libavutil/float_dsp.h"
 #include "libavutil/libm.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/thread.h"
 #include "libavutil/tx.h"
diff --git a/libavcodec/atrac3plusdec.c b/libavcodec/atrac3plusdec.c
index aa4d42f44a..9696a523be 100644
--- a/libavcodec/atrac3plusdec.c
+++ b/libavcodec/atrac3plusdec.c
@@ -39,6 +39,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/float_dsp.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/thread.h"
 #include "avcodec.h"
diff --git a/libavcodec/atrac9dec.c b/libavcodec/atrac9dec.c
index 5b84f0c6d6..df68407af9 100644
--- a/libavcodec/atrac9dec.c
+++ b/libavcodec/atrac9dec.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 
 #include "codec_internal.h"
diff --git a/libavcodec/atsc_a53.c b/libavcodec/atsc_a53.c
index 29ec71bc5f..1e9ea15ae0 100644
--- a/libavcodec/atsc_a53.c
+++ b/libavcodec/atsc_a53.c
@@ -19,6 +19,7 @@
 #include <stddef.h>
 #include <stdint.h>
 
+#include "libavutil/mem.h"
 #include "atsc_a53.h"
 #include "get_bits.h"
 
diff --git a/libavcodec/audio_frame_queue.c b/libavcodec/audio_frame_queue.c
index 08b4b368c7..10b5d21392 100644
--- a/libavcodec/audio_frame_queue.c
+++ b/libavcodec/audio_frame_queue.c
@@ -20,7 +20,7 @@
  */
 
 #include "libavutil/attributes.h"
-#include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "audio_frame_queue.h"
 #include "encode.h"
 #include "libavutil/avassert.h"
diff --git a/libavcodec/audiotoolboxdec.c b/libavcodec/audiotoolboxdec.c
index 5456f916cf..0f7ce8e4eb 100644
--- a/libavcodec/audiotoolboxdec.c
+++ b/libavcodec/audiotoolboxdec.c
@@ -32,6 +32,7 @@
 #include "mpegaudiodecheader.h"
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/log.h"
 
diff --git a/libavcodec/audiotoolboxenc.c b/libavcodec/audiotoolboxenc.c
index 2c65101fdc..ad2b863bb9 100644
--- a/libavcodec/audiotoolboxenc.c
+++ b/libavcodec/audiotoolboxenc.c
@@ -35,6 +35,7 @@
 #include "libavformat/isom.h"
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/log.h"
 
diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c
index 32a795e758..e5088fc7b5 100644
--- a/libavcodec/av1dec.c
+++ b/libavcodec/av1dec.c
@@ -23,6 +23,7 @@
 #include "libavutil/hdr_dynamic_metadata.h"
 #include "libavutil/film_grain_params.h"
 #include "libavutil/mastering_display_metadata.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
diff --git a/libavcodec/avdct.c b/libavcodec/avdct.c
index 37266f4b4e..f995e73eab 100644
--- a/libavcodec/avdct.c
+++ b/libavcodec/avdct.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "idctdsp.h"
 #include "fdctdsp.h"
diff --git a/libavcodec/avuienc.c b/libavcodec/avuienc.c
index 8a093d3d19..495f6d36fb 100644
--- a/libavcodec/avuienc.c
+++ b/libavcodec/avuienc.c
@@ -24,6 +24,7 @@
 #include "codec_internal.h"
 #include "encode.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 
 static av_cold int avui_encode_init(AVCodecContext *avctx)
 {
diff --git a/libavcodec/bfi.c b/libavcodec/bfi.c
index 901669a3a9..a7dd29a4e8 100644
--- a/libavcodec/bfi.c
+++ b/libavcodec/bfi.c
@@ -26,7 +26,7 @@
  * @see http://wiki.multimedia.cx/index.php?title=BFI
  */
 
-#include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/bgmc.c b/libavcodec/bgmc.c
index 0e41a39e00..97c53a49ce 100644
--- a/libavcodec/bgmc.c
+++ b/libavcodec/bgmc.c
@@ -26,6 +26,7 @@
  */
 
 #include "libavutil/attributes.h"
+#include "libavutil/mem.h"
 #include "bgmc.h"
 
 #define FREQ_BITS  14                      // bits used by frequency counters
diff --git a/libavcodec/bink.c b/libavcodec/bink.c
index 804c141981..04fd90aa11 100644
--- a/libavcodec/bink.c
+++ b/libavcodec/bink.c
@@ -23,7 +23,7 @@
 #include "libavutil/attributes.h"
 #include "libavutil/emms.h"
 #include "libavutil/imgutils.h"
-#include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/thread.h"
 
diff --git a/libavcodec/bonk.c b/libavcodec/bonk.c
index 65679e5fb6..14b7457a1a 100644
--- a/libavcodec/bonk.c
+++ b/libavcodec/bonk.c
@@ -18,14 +18,13 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
 #define BITSTREAM_READER_LE
 #include "get_bits.h"
-#include "bytestream.h"
 
 typedef struct BitCount {
     uint8_t bit;
diff --git a/libavcodec/bsf/dts2pts.c b/libavcodec/bsf/dts2pts.c
index 263514faad..53a54fb1cc 100644
--- a/libavcodec/bsf/dts2pts.c
+++ b/libavcodec/bsf/dts2pts.c
@@ -25,6 +25,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/fifo.h"
+#include "libavutil/mem.h"
 #include "libavutil/tree.h"
 
 #include "bsf.h"
diff --git a/libavcodec/bsf/evc_frame_merge.c b/libavcodec/bsf/evc_frame_merge.c
index 7b8e6b1c9e..1b52ba2900 100644
--- a/libavcodec/bsf/evc_frame_merge.c
+++ b/libavcodec/bsf/evc_frame_merge.c
@@ -17,6 +17,7 @@
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
+#include "libavutil/mem.h"
 #include "get_bits.h"
 #include "bsf.h"
 #include "bsf_internal.h"
diff --git a/libavcodec/bsf/extract_extradata.c b/libavcodec/bsf/extract_extradata.c
index 5d5d80c90f..c3c98c7d77 100644
--- a/libavcodec/bsf/extract_extradata.c
+++ b/libavcodec/bsf/extract_extradata.c
@@ -19,6 +19,7 @@
 #include <stdint.h>
 
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "av1.h"
diff --git a/libavcodec/bsf/filter_units.c b/libavcodec/bsf/filter_units.c
index 9aead542cc..029c86048b 100644
--- a/libavcodec/bsf/filter_units.c
+++ b/libavcodec/bsf/filter_units.c
@@ -18,7 +18,7 @@
 
 #include <stdlib.h>
 
-#include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "bsf.h"
diff --git a/libavcodec/bsf/h264_metadata.c b/libavcodec/bsf/h264_metadata.c
index 239e82aa3a..477f1b18b6 100644
--- a/libavcodec/bsf/h264_metadata.c
+++ b/libavcodec/bsf/h264_metadata.c
@@ -19,6 +19,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/display.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "bsf.h"
diff --git a/libavcodec/bsf/noise.c b/libavcodec/bsf/noise.c
index 3a0f388dd6..a622855717 100644
--- a/libavcodec/bsf/noise.c
+++ b/libavcodec/bsf/noise.c
@@ -24,6 +24,7 @@
 #include "bsf_internal.h"
 
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/eval.h"
 
diff --git a/libavcodec/cavs.c b/libavcodec/cavs.c
index fdd577f7fb..172cc5cc7a 100644
--- a/libavcodec/cavs.c
+++ b/libavcodec/cavs.c
@@ -25,6 +25,7 @@
  * @author Stefan Gehrer <stefan.gehrer@gmx.de>
  */
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "golomb.h"
 #include "h264chroma.h"
diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c
index b356da0b04..3cf8c9fb3a 100644
--- a/libavcodec/cavsdec.c
+++ b/libavcodec/cavsdec.c
@@ -27,6 +27,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/emms.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "get_bits.h"
 #include "golomb.h"
diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c
index de7b1361aa..b26e39eab4 100644
--- a/libavcodec/cbs.c
+++ b/libavcodec/cbs.c
@@ -23,6 +23,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/buffer.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c
index 8e4af7b2cc..fe2e383ff3 100644
--- a/libavcodec/cbs_h2645.c
+++ b/libavcodec/cbs_h2645.c
@@ -18,6 +18,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 
 #include "bytestream.h"
 #include "cbs.h"
diff --git a/libavcodec/cbs_jpeg.c b/libavcodec/cbs_jpeg.c
index 5921d624a1..b1b58dcd65 100644
--- a/libavcodec/cbs_jpeg.c
+++ b/libavcodec/cbs_jpeg.c
@@ -16,6 +16,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "cbs.h"
 #include "cbs_internal.h"
 #include "cbs_jpeg.h"
diff --git a/libavcodec/cbs_sei.c b/libavcodec/cbs_sei.c
index e28c2f9093..eefa07cf00 100644
--- a/libavcodec/cbs_sei.c
+++ b/libavcodec/cbs_sei.c
@@ -16,6 +16,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "cbs.h"
 #include "cbs_internal.h"
 #include "cbs_h264.h"
diff --git a/libavcodec/cdtoons.c b/libavcodec/cdtoons.c
index 94c49f0c81..89b6d76d84 100644
--- a/libavcodec/cdtoons.c
+++ b/libavcodec/cdtoons.c
@@ -29,6 +29,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/cdxl.c b/libavcodec/cdxl.c
index 6843fceb2f..26f05e6280 100644
--- a/libavcodec/cdxl.c
+++ b/libavcodec/cdxl.c
@@ -28,6 +28,7 @@
 #define UNCHECKED_BITSTREAM_READER 1
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/cfhd.c b/libavcodec/cfhd.c
index 42d7dcc3f6..6f1d960058 100644
--- a/libavcodec/cfhd.c
+++ b/libavcodec/cfhd.c
@@ -24,9 +24,9 @@
  */
 
 #include "libavutil/attributes.h"
-#include "libavutil/buffer.h"
 #include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/cfhdenc.c b/libavcodec/cfhdenc.c
index 7169d20a8c..5657d0f96c 100644
--- a/libavcodec/cfhdenc.c
+++ b/libavcodec/cfhdenc.c
@@ -23,11 +23,11 @@
  * Cineform HD video encoder
  */
 
-#include <stdlib.h>
-#include <string.h>
+#include <stddef.h>
 
-#include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
 
 #include "avcodec.h"
 #include "bytestream.h"
@@ -36,7 +36,6 @@
 #include "codec_internal.h"
 #include "encode.h"
 #include "put_bits.h"
-#include "thread.h"
 
 /* Derived from existing tables from decoder */
 static const unsigned codebook[256][2] = {
diff --git a/libavcodec/cinepakenc.c b/libavcodec/cinepakenc.c
index f15325ad0d..f6145131a2 100644
--- a/libavcodec/cinepakenc.c
+++ b/libavcodec/cinepakenc.c
@@ -37,10 +37,9 @@
 #include <string.h>
 
 #include "libavutil/avassert.h"
-#include "libavutil/common.h"
-#include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/lfg.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/clearvideo.c b/libavcodec/clearvideo.c
index 0b59d44cd6..bb1e818c94 100644
--- a/libavcodec/clearvideo.c
+++ b/libavcodec/clearvideo.c
@@ -24,6 +24,7 @@
  * ClearVideo decoder
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/thread.h"
 
diff --git a/libavcodec/cllc.c b/libavcodec/cllc.c
index 0c6ae13d08..9b07f7c78e 100644
--- a/libavcodec/cllc.c
+++ b/libavcodec/cllc.c
@@ -23,6 +23,7 @@
 #include <inttypes.h>
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "bswapdsp.h"
 #include "canopus.h"
 #include "get_bits.h"
diff --git a/libavcodec/cngdec.c b/libavcodec/cngdec.c
index 5cf5dc9bcf..bacbd54fab 100644
--- a/libavcodec/cngdec.c
+++ b/libavcodec/cngdec.c
@@ -23,7 +23,7 @@
 
 #include "libavutil/common.h"
 #include "libavutil/ffmath.h"
-#include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "celp_filters.h"
 #include "codec_internal.h"
diff --git a/libavcodec/cngenc.c b/libavcodec/cngenc.c
index 15a410ea50..c430136809 100644
--- a/libavcodec/cngenc.c
+++ b/libavcodec/cngenc.c
@@ -23,6 +23,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "encode.h"
diff --git a/libavcodec/cook.c b/libavcodec/cook.c
index 705213de49..dbe6b5b96c 100644
--- a/libavcodec/cook.c
+++ b/libavcodec/cook.c
@@ -44,6 +44,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/lfg.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/thread.h"
 #include "libavutil/tx.h"
diff --git a/libavcodec/cscd.c b/libavcodec/cscd.c
index 685cf18cec..e552b0a14f 100644
--- a/libavcodec/cscd.c
+++ b/libavcodec/cscd.c
@@ -22,7 +22,7 @@
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
-#include "libavutil/common.h"
+#include "libavutil/mem.h"
 
 #if CONFIG_ZLIB
 #include <zlib.h>
diff --git a/libavcodec/cuviddec.c b/libavcodec/cuviddec.c
index acc405f78a..f88ad75e88 100644
--- a/libavcodec/cuviddec.c
+++ b/libavcodec/cuviddec.c
@@ -30,6 +30,7 @@
 #include "libavutil/cuda_check.h"
 #include "libavutil/fifo.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 
diff --git a/libavcodec/d3d12va_av1.c b/libavcodec/d3d12va_av1.c
index 76d9729900..4a4d207b4f 100644
--- a/libavcodec/d3d12va_av1.c
+++ b/libavcodec/d3d12va_av1.c
@@ -23,6 +23,7 @@
 #include "config_components.h"
 #include "libavutil/avassert.h"
 #include "libavutil/hwcontext_d3d12va_internal.h"
+#include "libavutil/mem.h"
 #include "av1dec.h"
 #include "dxva2_internal.h"
 #include "d3d12va_decode.h"
diff --git a/libavcodec/d3d12va_decode.c b/libavcodec/d3d12va_decode.c
index af7cf11640..94a3794807 100644
--- a/libavcodec/d3d12va_decode.c
+++ b/libavcodec/d3d12va_decode.c
@@ -25,6 +25,7 @@
 
 #include "libavutil/common.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/time.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/hwcontext_d3d12va_internal.h"
diff --git a/libavcodec/dca_core.c b/libavcodec/dca_core.c
index 697fc74295..cb1f7b7bbf 100644
--- a/libavcodec/dca_core.c
+++ b/libavcodec/dca_core.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "dcaadpcm.h"
 #include "dcadec.h"
 #include "dcadata.h"
diff --git a/libavcodec/dca_lbr.c b/libavcodec/dca_lbr.c
index c9c5c0f856..4996c204e6 100644
--- a/libavcodec/dca_lbr.c
+++ b/libavcodec/dca_lbr.c
@@ -21,6 +21,7 @@
 #define BITSTREAM_READER_LE
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #include "dcadec.h"
diff --git a/libavcodec/dca_xll.c b/libavcodec/dca_xll.c
index 15f9bd9196..005a51ed69 100644
--- a/libavcodec/dca_xll.c
+++ b/libavcodec/dca_xll.c
@@ -20,6 +20,7 @@
 
 #include "avcodec.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "dcadec.h"
 #include "dcadata.h"
 #include "dcamath.h"
diff --git a/libavcodec/dcaadpcm.c b/libavcodec/dcaadpcm.c
index 9f615e3793..1d39da9ffc 100644
--- a/libavcodec/dcaadpcm.c
+++ b/libavcodec/dcaadpcm.c
@@ -20,6 +20,7 @@
  */
 
 
+#include "libavutil/mem.h"
 #include "dcaadpcm.h"
 #include "dcaenc.h"
 #include "dca_core.h"
diff --git a/libavcodec/dcadec.c b/libavcodec/dcadec.c
index 8a62d3b870..86d86ea458 100644
--- a/libavcodec/dcadec.c
+++ b/libavcodec/dcadec.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/thread.h"
diff --git a/libavcodec/dcaenc.c b/libavcodec/dcaenc.c
index 192121524b..83f13472d3 100644
--- a/libavcodec/dcaenc.c
+++ b/libavcodec/dcaenc.c
@@ -25,13 +25,13 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
 #include "libavutil/ffmath.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 #include "libavutil/thread.h"
 #include "libavutil/tx.h"
 #include "avcodec.h"
 #include "codec_internal.h"
-#include "dca.h"
 #include "dcaadpcm.h"
 #include "dcamath.h"
 #include "dca_core.h"
diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 34bcb7cc64..db3d1a198c 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -36,6 +36,7 @@
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
 #include "libavutil/mastering_display_metadata.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
 #include "avcodec_internal.h"
diff --git a/libavcodec/dirac.c b/libavcodec/dirac.c
index 4736304977..7fac7409a5 100644
--- a/libavcodec/dirac.c
+++ b/libavcodec/dirac.c
@@ -28,6 +28,7 @@
 
 #include "config.h"
 
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 
 #include "dirac.h"
diff --git a/libavcodec/diracdec.c b/libavcodec/diracdec.c
index 24c7df0001..f1fde0b339 100644
--- a/libavcodec/diracdec.c
+++ b/libavcodec/diracdec.c
@@ -26,6 +26,7 @@
  * @author Marco Gerards <marco@gnu.org>, David Conrad, Jordi Ortiz <nenjordi@gmail.com>
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/thread.h"
diff --git a/libavcodec/dnxhddec.c b/libavcodec/dnxhddec.c
index 703d0e341c..e549b38720 100644
--- a/libavcodec/dnxhddec.c
+++ b/libavcodec/dnxhddec.c
@@ -24,6 +24,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/pixdesc.h"
 
diff --git a/libavcodec/dnxhdenc.c b/libavcodec/dnxhdenc.c
index 2316083b54..0cb25d7714 100644
--- a/libavcodec/dnxhdenc.c
+++ b/libavcodec/dnxhdenc.c
@@ -25,6 +25,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 
diff --git a/libavcodec/dovi_rpu.c b/libavcodec/dovi_rpu.c
index 31c64fb060..ccd5216500 100644
--- a/libavcodec/dovi_rpu.c
+++ b/libavcodec/dovi_rpu.c
@@ -22,6 +22,7 @@
  */
 
 #include "libavutil/buffer.h"
+#include "libavutil/mem.h"
 
 #include "dovi_rpu.h"
 #include "golomb.h"
diff --git a/libavcodec/dsddec.c b/libavcodec/dsddec.c
index 3962075773..22009c70ef 100644
--- a/libavcodec/dsddec.c
+++ b/libavcodec/dsddec.c
@@ -26,7 +26,8 @@
  * Direct Stream Digital (DSD) decoder
  */
 
-#include "libavcodec/internal.h"
+#include "libavutil/mem.h"
+
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
diff --git a/libavcodec/dsicinvideo.c b/libavcodec/dsicinvideo.c
index 000d79e169..10b536d405 100644
--- a/libavcodec/dsicinvideo.c
+++ b/libavcodec/dsicinvideo.c
@@ -24,6 +24,7 @@
  * Delphine Software International CIN video decoder
  */
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/dvbsubdec.c b/libavcodec/dvbsubdec.c
index bcc607d1d7..4553c45b3d 100644
--- a/libavcodec/dvbsubdec.c
+++ b/libavcodec/dvbsubdec.c
@@ -26,6 +26,7 @@
 #include "decode.h"
 #include "libavutil/colorspace.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/thread.h"
 
diff --git a/libavcodec/dvdsubdec.c b/libavcodec/dvdsubdec.c
index a5da0d7b08..f8769353a0 100644
--- a/libavcodec/dvdsubdec.c
+++ b/libavcodec/dvdsubdec.c
@@ -28,6 +28,7 @@
 #include "libavutil/attributes.h"
 #include "libavutil/colorspace.h"
 #include "libavutil/file_open.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/bswap.h"
 
diff --git a/libavcodec/dvdsubenc.c b/libavcodec/dvdsubenc.c
index 06c2cf5e5a..c6110c29ff 100644
--- a/libavcodec/dvdsubenc.c
+++ b/libavcodec/dvdsubenc.c
@@ -25,6 +25,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/bprint.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 typedef struct {
diff --git a/libavcodec/dxa.c b/libavcodec/dxa.c
index d33ac3c8b0..59e2411d8e 100644
--- a/libavcodec/dxa.c
+++ b/libavcodec/dxa.c
@@ -24,8 +24,8 @@
  * DXA Video decoder
  */
 
-#include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "bytestream.h"
 #include "avcodec.h"
 #include "codec_internal.h"
diff --git a/libavcodec/dxv.c b/libavcodec/dxv.c
index b5553a0c86..f130db3a4e 100644
--- a/libavcodec/dxv.c
+++ b/libavcodec/dxv.c
@@ -23,8 +23,8 @@
 #include <stdint.h>
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 
-#include "mathops.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/dxva2.c b/libavcodec/dxva2.c
index 59025633f7..6eb66c02e4 100644
--- a/libavcodec/dxva2.c
+++ b/libavcodec/dxva2.c
@@ -26,6 +26,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/common.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/time.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/dxva2_av1.c b/libavcodec/dxva2_av1.c
index 85d747bca8..184a922fd8 100644
--- a/libavcodec/dxva2_av1.c
+++ b/libavcodec/dxva2_av1.c
@@ -23,6 +23,7 @@
 #include "config_components.h"
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 
 #include "dxva2_internal.h"
diff --git a/libavcodec/dxvenc.c b/libavcodec/dxvenc.c
index 91f4ba7619..8229438373 100644
--- a/libavcodec/dxvenc.c
+++ b/libavcodec/dxvenc.c
@@ -23,6 +23,7 @@
 
 #include "libavutil/crc.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "bytestream.h"
diff --git a/libavcodec/eamad.c b/libavcodec/eamad.c
index 287575e073..1c3f97653c 100644
--- a/libavcodec/eamad.c
+++ b/libavcodec/eamad.c
@@ -28,6 +28,7 @@
  * http://wiki.multimedia.cx/index.php?title=Electronic_Arts_MAD
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/eatqi.c b/libavcodec/eatqi.c
index e4f12b3db2..421ed51009 100644
--- a/libavcodec/eatqi.c
+++ b/libavcodec/eatqi.c
@@ -26,6 +26,7 @@
  * @see http://wiki.multimedia.cx/index.php?title=Electronic_Arts_TQI
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/elbg.c b/libavcodec/elbg.c
index a52ee1e3d8..84043af4fd 100644
--- a/libavcodec/elbg.c
+++ b/libavcodec/elbg.c
@@ -28,6 +28,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/common.h"
 #include "libavutil/lfg.h"
+#include "libavutil/mem.h"
 #include "elbg.h"
 
 #define DELTA_ERR_MAX 0.1  ///< Precision of the ELBG algorithm (as percentage error)
diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index 7fc9737e93..16ba5c072a 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -25,6 +25,7 @@
 #include "libavutil/frame.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/samplefmt.h"
 
diff --git a/libavcodec/error_resilience.c b/libavcodec/error_resilience.c
index 9d03e53237..efbacb8760 100644
--- a/libavcodec/error_resilience.c
+++ b/libavcodec/error_resilience.c
@@ -27,13 +27,12 @@
 
 #include <limits.h>
 
-#include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "error_resilience.h"
 #include "me_cmp.h"
 #include "mpegutils.h"
 #include "mpegvideo.h"
-#include "rectangle.h"
 #include "threadframe.h"
 
 /**
diff --git a/libavcodec/escape124.c b/libavcodec/escape124.c
index 357320ef94..b7e26e1326 100644
--- a/libavcodec/escape124.c
+++ b/libavcodec/escape124.c
@@ -20,6 +20,7 @@
  */
 
 #define BITSTREAM_READER_LE
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
diff --git a/libavcodec/evc_ps.c b/libavcodec/evc_ps.c
index 19895d404d..637572d324 100644
--- a/libavcodec/evc_ps.c
+++ b/libavcodec/evc_ps.c
@@ -16,6 +16,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "get_bits.h"
 #include "golomb.h"
 #include "evc.h"
diff --git a/libavcodec/exr.c b/libavcodec/exr.c
index 5711fbbdcd..0f1f683e7e 100644
--- a/libavcodec/exr.c
+++ b/libavcodec/exr.c
@@ -40,6 +40,7 @@
 #include "libavutil/imgutils.h"
 #include "libavutil/intfloat.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/half2float.h"
 
diff --git a/libavcodec/exrenc.c b/libavcodec/exrenc.c
index 1b710fc088..71ec07054e 100644
--- a/libavcodec/exrenc.c
+++ b/libavcodec/exrenc.c
@@ -27,10 +27,9 @@
 #include <zlib.h>
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
-#include "libavutil/intreadwrite.h"
 #include "libavutil/imgutils.h"
-#include "libavutil/pixdesc.h"
 #include "libavutil/float2half.h"
 #include "avcodec.h"
 #include "bytestream.h"
diff --git a/libavcodec/fastaudio.c b/libavcodec/fastaudio.c
index f5569f5206..33ad26f8a1 100644
--- a/libavcodec/fastaudio.c
+++ b/libavcodec/fastaudio.c
@@ -21,6 +21,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/faxcompr.c b/libavcodec/faxcompr.c
index 41a1eec081..cf744df31c 100644
--- a/libavcodec/faxcompr.c
+++ b/libavcodec/faxcompr.c
@@ -24,6 +24,7 @@
  * CCITT Fax Group 3 and 4 decompression
  * @author Konstantin Shishkov
  */
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 #include "avcodec.h"
 #include "get_bits.h"
diff --git a/libavcodec/ffv1.c b/libavcodec/ffv1.c
index b6204740ed..6ec24fed4a 100644
--- a/libavcodec/ffv1.c
+++ b/libavcodec/ffv1.c
@@ -27,11 +27,10 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
-#include "rangecoder.h"
 #include "ffv1.h"
-#include "threadframe.h"
 
 av_cold int ff_ffv1_common_init(AVCodecContext *avctx)
 {
diff --git a/libavcodec/ffv1dec.c b/libavcodec/ffv1dec.c
index 97877b9106..ba535e800d 100644
--- a/libavcodec/ffv1dec.c
+++ b/libavcodec/ffv1dec.c
@@ -27,7 +27,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/crc.h"
-#include "libavutil/opt.h"
+#include "libavutil/mem.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/pixdesc.h"
 #include "avcodec.h"
diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index e510abf6ff..94d9215acd 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -28,8 +28,8 @@
 #include "libavutil/attributes.h"
 #include "libavutil/avassert.h"
 #include "libavutil/crc.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
-#include "libavutil/imgutils.h"
 #include "libavutil/pixdesc.h"
 
 #include "avcodec.h"
@@ -38,7 +38,6 @@
 #include "put_bits.h"
 #include "put_golomb.h"
 #include "rangecoder.h"
-#include "mathops.h"
 #include "ffv1.h"
 
 static const int8_t quant5_10bit[256] = {
diff --git a/libavcodec/ffwavesynth.c b/libavcodec/ffwavesynth.c
index b932326fd0..022cb50cd8 100644
--- a/libavcodec/ffwavesynth.c
+++ b/libavcodec/ffwavesynth.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
diff --git a/libavcodec/fic.c b/libavcodec/fic.c
index fb99802137..ec26e3154d 100644
--- a/libavcodec/fic.c
+++ b/libavcodec/fic.c
@@ -22,6 +22,7 @@
  */
 
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
diff --git a/libavcodec/flac_parser.c b/libavcodec/flac_parser.c
index bd91cc1a05..47904d515a 100644
--- a/libavcodec/flac_parser.c
+++ b/libavcodec/flac_parser.c
@@ -34,8 +34,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/crc.h"
-#include "bytestream.h"
-#include "parser.h"
+#include "libavutil/mem.h"
 #include "flac_parse.h"
 
 /** maximum number of adjacent headers that compare CRCs against each other   */
diff --git a/libavcodec/flacdec.c b/libavcodec/flacdec.c
index ed2de14d0a..91bbdc657d 100644
--- a/libavcodec/flacdec.c
+++ b/libavcodec/flacdec.c
@@ -35,14 +35,13 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/crc.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "get_bits.h"
-#include "bytestream.h"
 #include "golomb.h"
 #include "flac.h"
-#include "flacdata.h"
 #include "flacdsp.h"
 #include "flac_parse.h"
 #include "thread.h"
diff --git a/libavcodec/flacenc.c b/libavcodec/flacenc.c
index 3dc4dc2e4f..e29be5822b 100644
--- a/libavcodec/flacenc.c
+++ b/libavcodec/flacenc.c
@@ -24,6 +24,7 @@
 #include "libavutil/crc.h"
 #include "libavutil/intmath.h"
 #include "libavutil/md5.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/flashsv.c b/libavcodec/flashsv.c
index fe00e529a5..d27918c923 100644
--- a/libavcodec/flashsv.c
+++ b/libavcodec/flashsv.c
@@ -39,6 +39,7 @@
 #include <zlib.h>
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/flashsv2enc.c b/libavcodec/flashsv2enc.c
index 6dcb9102a8..6156d00d0d 100644
--- a/libavcodec/flashsv2enc.c
+++ b/libavcodec/flashsv2enc.c
@@ -47,11 +47,11 @@
 #include <zlib.h>
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "encode.h"
 #include "put_bits.h"
-#include "bytestream.h"
 #include "zlib_wrapper.h"
 
 #define HAS_IFRAME_IMAGE 0x02
diff --git a/libavcodec/fmvc.c b/libavcodec/fmvc.c
index 5e26a541ca..30f7aaf6bc 100644
--- a/libavcodec/fmvc.c
+++ b/libavcodec/fmvc.c
@@ -23,6 +23,7 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/frame_thread_encoder.c b/libavcodec/frame_thread_encoder.c
index cda5158117..d729105c43 100644
--- a/libavcodec/frame_thread_encoder.c
+++ b/libavcodec/frame_thread_encoder.c
@@ -24,6 +24,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/cpu.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/thread.h"
 #include "avcodec.h"
diff --git a/libavcodec/fraps.c b/libavcodec/fraps.c
index bed244e4e9..4e45efeba8 100644
--- a/libavcodec/fraps.c
+++ b/libavcodec/fraps.c
@@ -35,6 +35,7 @@
 
 #define CACHED_BITSTREAM_READER HAVE_FAST_64BIT
 #define UNCHECKED_BITSTREAM_READER 1
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "get_bits.h"
 #include "huffman.h"
diff --git a/libavcodec/g2meet.c b/libavcodec/g2meet.c
index 34ff60a3cf..7ae987ec40 100644
--- a/libavcodec/g2meet.c
+++ b/libavcodec/g2meet.c
@@ -29,6 +29,7 @@
 #include <zlib.h>
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/g722enc.c b/libavcodec/g722enc.c
index 2f19a1b29a..19ab0f8b55 100644
--- a/libavcodec/g722enc.c
+++ b/libavcodec/g722enc.c
@@ -29,6 +29,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "encode.h"
diff --git a/libavcodec/g729dec.c b/libavcodec/g729dec.c
index 33e1fb9c29..3e1be326d0 100644
--- a/libavcodec/g729dec.c
+++ b/libavcodec/g729dec.c
@@ -24,6 +24,7 @@
 
 #include "avcodec.h"
 #include "libavutil/avutil.h"
+#include "libavutil/mem.h"
 #include "get_bits.h"
 #include "audiodsp.h"
 #include "codec_internal.h"
diff --git a/libavcodec/gdv.c b/libavcodec/gdv.c
index e114f3e80f..670597448b 100644
--- a/libavcodec/gdv.c
+++ b/libavcodec/gdv.c
@@ -21,6 +21,7 @@
  */
 
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/gemdec.c b/libavcodec/gemdec.c
index 9e2a50c766..5ece2e2ee3 100644
--- a/libavcodec/gemdec.c
+++ b/libavcodec/gemdec.c
@@ -24,6 +24,7 @@
  * GEM Raster image decoder
  */
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/gif.c b/libavcodec/gif.c
index 49356236e7..56042d36a7 100644
--- a/libavcodec/gif.c
+++ b/libavcodec/gif.c
@@ -30,6 +30,7 @@
  * @see http://www.w3.org/Graphics/GIF/spec-gif89a.txt
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "bytestream.h"
diff --git a/libavcodec/gifdec.c b/libavcodec/gifdec.c
index a97a6d15fc..ae45eb0f6d 100644
--- a/libavcodec/gifdec.c
+++ b/libavcodec/gifdec.c
@@ -21,6 +21,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "bytestream.h"
diff --git a/libavcodec/h2645_sei.c b/libavcodec/h2645_sei.c
index afc103b69c..f0f107f73b 100644
--- a/libavcodec/h2645_sei.c
+++ b/libavcodec/h2645_sei.c
@@ -30,7 +30,7 @@
 #include "libavutil/hdr_dynamic_metadata.h"
 #include "libavutil/film_grain_params.h"
 #include "libavutil/mastering_display_metadata.h"
-#include "libavutil/pixdesc.h"
+#include "libavutil/mem.h"
 #include "libavutil/stereo3d.h"
 
 #include "atsc_a53.h"
diff --git a/libavcodec/h264_parse.c b/libavcodec/h264_parse.c
index 3378650fd5..c58d0c0fa9 100644
--- a/libavcodec/h264_parse.c
+++ b/libavcodec/h264_parse.c
@@ -16,6 +16,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "bytestream.h"
 #include "get_bits.h"
 #include "golomb.h"
diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c
index e9a404e41b..4b01c54147 100644
--- a/libavcodec/h264_slice.c
+++ b/libavcodec/h264_slice.c
@@ -28,6 +28,7 @@
 #include "config_components.h"
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/timecode.h"
 #include "decode.h"
diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c
index b4973fce29..727dc1a662 100644
--- a/libavcodec/h264dec.c
+++ b/libavcodec/h264dec.c
@@ -32,6 +32,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/emms.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/thread.h"
 #include "libavutil/video_enc_params.h"
diff --git a/libavcodec/hap.c b/libavcodec/hap.c
index 1a330c9c9b..ac4032ce11 100644
--- a/libavcodec/hap.c
+++ b/libavcodec/hap.c
@@ -23,6 +23,7 @@
  * @file
  * Hap utilities
  */
+#include "libavutil/mem.h"
 #include "hap.h"
 
 int ff_hap_set_chunk_count(HapContext *ctx, int count, int first_in_frame)
diff --git a/libavcodec/hapdec.c b/libavcodec/hapdec.c
index 6066cb814c..22b7c281d1 100644
--- a/libavcodec/hapdec.c
+++ b/libavcodec/hapdec.c
@@ -34,6 +34,7 @@
 #include <stdint.h>
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
 #include "bytestream.h"
diff --git a/libavcodec/hapenc.c b/libavcodec/hapenc.c
index 3f74098d13..8dc12a851c 100644
--- a/libavcodec/hapenc.c
+++ b/libavcodec/hapenc.c
@@ -34,7 +34,7 @@
 
 #include "libavutil/frame.h"
 #include "libavutil/imgutils.h"
-#include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/hcadec.c b/libavcodec/hcadec.c
index 026b998341..3d1d8f6fca 100644
--- a/libavcodec/hcadec.c
+++ b/libavcodec/hcadec.c
@@ -18,6 +18,7 @@
 
 #include "libavutil/crc.h"
 #include "libavutil/float_dsp.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/tx.h"
 
diff --git a/libavcodec/hcom.c b/libavcodec/hcom.c
index 9284cd11c9..d080330992 100644
--- a/libavcodec/hcom.c
+++ b/libavcodec/hcom.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
 #include "codec_internal.h"
diff --git a/libavcodec/hdrenc.c b/libavcodec/hdrenc.c
index 40d283ee61..20e42134bc 100644
--- a/libavcodec/hdrenc.c
+++ b/libavcodec/hdrenc.c
@@ -18,7 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/hevc_parser.c b/libavcodec/hevc_parser.c
index 99a4272fad..f174d419e4 100644
--- a/libavcodec/hevc_parser.c
+++ b/libavcodec/hevc_parser.c
@@ -21,6 +21,7 @@
  */
 
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 
 #include "golomb.h"
 #include "hevc.h"
diff --git a/libavcodec/hevc_ps.c b/libavcodec/hevc_ps.c
index cbef3ef4cd..86c8ceb128 100644
--- a/libavcodec/hevc_ps.c
+++ b/libavcodec/hevc_ps.c
@@ -24,6 +24,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "golomb.h"
 #include "h2645_vui.h"
 #include "hevc_data.h"
diff --git a/libavcodec/hevc_refs.c b/libavcodec/hevc_refs.c
index 54e3d40e1c..aed649933d 100644
--- a/libavcodec/hevc_refs.c
+++ b/libavcodec/hevc_refs.c
@@ -21,10 +21,9 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 
 #include "decode.h"
-#include "thread.h"
 #include "hevc.h"
 #include "hevcdec.h"
 #include "refstruct.h"
diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
index 575836e340..752459af2d 100644
--- a/libavcodec/hevcdec.c
+++ b/libavcodec/hevcdec.c
@@ -31,6 +31,7 @@
 #include "libavutil/film_grain_params.h"
 #include "libavutil/internal.h"
 #include "libavutil/md5.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/timecode.h"
diff --git a/libavcodec/huffyuvdec.c b/libavcodec/huffyuvdec.c
index ce6d4d4c59..3bed27be21 100644
--- a/libavcodec/huffyuvdec.c
+++ b/libavcodec/huffyuvdec.c
@@ -44,6 +44,7 @@
 #include "thread.h"
 #include "libavutil/emms.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 
 #define VLC_BITS 12
diff --git a/libavcodec/huffyuvenc.c b/libavcodec/huffyuvenc.c
index 494ca7e603..0222565245 100644
--- a/libavcodec/huffyuvenc.c
+++ b/libavcodec/huffyuvenc.c
@@ -40,6 +40,7 @@
 #include "lossless_videoencdsp.h"
 #include "put_bits.h"
 #include "libavutil/emms.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 
diff --git a/libavcodec/iff.c b/libavcodec/iff.c
index faf4e21c42..133f69350f 100644
--- a/libavcodec/iff.c
+++ b/libavcodec/iff.c
@@ -29,6 +29,7 @@
 #include <stdint.h>
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 
 #include "bytestream.h"
 #include "avcodec.h"
diff --git a/libavcodec/imc.c b/libavcodec/imc.c
index 99eb089236..5891e3621a 100644
--- a/libavcodec/imc.c
+++ b/libavcodec/imc.c
@@ -38,6 +38,7 @@
 #include "libavutil/ffmath.h"
 #include "libavutil/float_dsp.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/thread.h"
 #include "libavutil/tx.h"
diff --git a/libavcodec/imm4.c b/libavcodec/imm4.c
index 656fc9c05f..3a4ad8616f 100644
--- a/libavcodec/imm4.c
+++ b/libavcodec/imm4.c
@@ -23,6 +23,7 @@
 #include <stddef.h>
 #include <string.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/thread.h"
 
diff --git a/libavcodec/indeo3.c b/libavcodec/indeo3.c
index 7bb0235bdb..fbabd4b6ad 100644
--- a/libavcodec/indeo3.c
+++ b/libavcodec/indeo3.c
@@ -31,6 +31,7 @@
 
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 #include "avcodec.h"
 #include "codec_internal.h"
diff --git a/libavcodec/interplayacm.c b/libavcodec/interplayacm.c
index 057ab16e85..2ae992a71d 100644
--- a/libavcodec/interplayacm.c
+++ b/libavcodec/interplayacm.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 
 #define BITSTREAM_READER_LE
diff --git a/libavcodec/intrax8.c b/libavcodec/intrax8.c
index 948391599f..40085c69ce 100644
--- a/libavcodec/intrax8.c
+++ b/libavcodec/intrax8.c
@@ -22,6 +22,7 @@
  */
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 #include "avcodec.h"
 #include "get_bits.h"
diff --git a/libavcodec/ivi.c b/libavcodec/ivi.c
index 60769664a1..e7d8d10c3e 100644
--- a/libavcodec/ivi.c
+++ b/libavcodec/ivi.c
@@ -30,6 +30,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 
 #define BITSTREAM_READER_LE
diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index a80e74d4ec..8cf82f7216 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -72,6 +72,7 @@
 #include "jpeg2000.h"
 #include "version.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/opt.h"
 #include "libavutil/intreadwrite.h"
diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 1afc6b1e2d..857379da2d 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -32,6 +32,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/common.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avcodec.h"
diff --git a/libavcodec/jpeg2000htdec.c b/libavcodec/jpeg2000htdec.c
index 6b9898d3ff..b2f29a4372 100644
--- a/libavcodec/jpeg2000htdec.c
+++ b/libavcodec/jpeg2000htdec.c
@@ -51,6 +51,7 @@
 #include "libavutil/attributes.h"
 #include "libavutil/common.h"
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "jpeg2000htdec.h"
 #include "jpeg2000.h"
 #include "jpeg2000dec.h"
diff --git a/libavcodec/jpeglsdec.c b/libavcodec/jpeglsdec.c
index c245cf0279..5f463c9660 100644
--- a/libavcodec/jpeglsdec.c
+++ b/libavcodec/jpeglsdec.c
@@ -25,6 +25,7 @@
  * JPEG-LS decoder.
  */
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "get_bits.h"
diff --git a/libavcodec/jpeglsenc.c b/libavcodec/jpeglsenc.c
index 3481c61ee3..d2f4a52f24 100644
--- a/libavcodec/jpeglsenc.c
+++ b/libavcodec/jpeglsenc.c
@@ -26,6 +26,7 @@
  */
 
 #define UNCHECKED_BITSTREAM_READER 1
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "bytestream.h"
diff --git a/libavcodec/kgv1dec.c b/libavcodec/kgv1dec.c
index 139ec9b05e..bdc5a16259 100644
--- a/libavcodec/kgv1dec.c
+++ b/libavcodec/kgv1dec.c
@@ -27,6 +27,7 @@
 #include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
diff --git a/libavcodec/leaddec.c b/libavcodec/leaddec.c
index f7d31681b8..1406cb0014 100644
--- a/libavcodec/leaddec.c
+++ b/libavcodec/leaddec.c
@@ -30,6 +30,7 @@
 #include "jpegquanttables.h"
 #include "jpegtables.h"
 #include "leaddata.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/thread.h"
 
diff --git a/libavcodec/libaomenc.c b/libavcodec/libaomenc.c
index 8c1f84cc9f..d660afab4e 100644
--- a/libavcodec/libaomenc.c
+++ b/libavcodec/libaomenc.c
@@ -35,6 +35,7 @@
 #include "libavutil/cpu.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 
diff --git a/libavcodec/libaribb24.c b/libavcodec/libaribb24.c
index 507a11511f..009c995ded 100644
--- a/libavcodec/libaribb24.c
+++ b/libavcodec/libaribb24.c
@@ -23,6 +23,7 @@
 #include "libavcodec/ass.h"
 #include "codec_internal.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include <aribb24/aribb24.h>
diff --git a/libavcodec/libaribcaption.c b/libavcodec/libaribcaption.c
index fba6827375..0b67d41772 100644
--- a/libavcodec/libaribcaption.c
+++ b/libavcodec/libaribcaption.c
@@ -25,6 +25,7 @@
 #include "libavcodec/ass.h"
 #include "libavutil/avstring.h"
 #include "libavutil/avutil.h"
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 #include "libavutil/log.h"
 #include "libavutil/opt.h"
diff --git a/libavcodec/libcodec2.c b/libavcodec/libcodec2.c
index b8fa396fac..ebcd20c447 100644
--- a/libavcodec/libcodec2.c
+++ b/libavcodec/libcodec2.c
@@ -21,6 +21,7 @@
 
 #include <codec2/codec2.h>
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "libavutil/opt.h"
 #include "codec_internal.h"
diff --git a/libavcodec/libfdk-aacdec.c b/libavcodec/libfdk-aacdec.c
index fa9d771c4e..1324377fb4 100644
--- a/libavcodec/libfdk-aacdec.c
+++ b/libavcodec/libfdk-aacdec.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "codec_internal.h"
diff --git a/libavcodec/libfdk-aacenc.c b/libavcodec/libfdk-aacenc.c
index fe3ba14ee8..0f33cdb8c9 100644
--- a/libavcodec/libfdk-aacenc.c
+++ b/libavcodec/libfdk-aacenc.c
@@ -22,6 +22,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "audio_frame_queue.h"
diff --git a/libavcodec/libjxlenc.c b/libavcodec/libjxlenc.c
index 3f2c74097d..8b0e60df6f 100644
--- a/libavcodec/libjxlenc.c
+++ b/libavcodec/libjxlenc.c
@@ -31,6 +31,7 @@
 #include "libavutil/error.h"
 #include "libavutil/frame.h"
 #include "libavutil/libm.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/pixfmt.h"
diff --git a/libavcodec/libmp3lame.c b/libavcodec/libmp3lame.c
index d3810eb2ab..42558178e8 100644
--- a/libavcodec/libmp3lame.c
+++ b/libavcodec/libmp3lame.c
@@ -31,6 +31,7 @@
 #include "libavutil/float_dsp.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "audio_frame_queue.h"
diff --git a/libavcodec/libopencore-amr.c b/libavcodec/libopencore-amr.c
index 641a156129..b127b33101 100644
--- a/libavcodec/libopencore-amr.c
+++ b/libavcodec/libopencore-amr.c
@@ -26,6 +26,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "audio_frame_queue.h"
diff --git a/libavcodec/libopenh264enc.c b/libavcodec/libopenh264enc.c
index eef769eed0..0aa2a54e68 100644
--- a/libavcodec/libopenh264enc.c
+++ b/libavcodec/libopenh264enc.c
@@ -24,6 +24,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
@@ -32,7 +33,6 @@
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "encode.h"
-#include "internal.h"
 #include "libopenh264.h"
 
 #if !OPENH264_VER_AT_LEAST(1, 6)
diff --git a/libavcodec/libopusenc.c b/libavcodec/libopusenc.c
index af25f27f74..6b8b2cda0e 100644
--- a/libavcodec/libopusenc.c
+++ b/libavcodec/libopusenc.c
@@ -23,6 +23,7 @@
 #include <opus_multistream.h>
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "bytestream.h"
diff --git a/libavcodec/librav1e.c b/libavcodec/librav1e.c
index 2a6d8bfbed..b83e2118ed 100644
--- a/libavcodec/librav1e.c
+++ b/libavcodec/librav1e.c
@@ -28,6 +28,7 @@
 #include "libavutil/base64.h"
 #include "libavutil/common.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avcodec.h"
diff --git a/libavcodec/libspeexenc.c b/libavcodec/libspeexenc.c
index 4c12cef255..7b19db5a2a 100644
--- a/libavcodec/libspeexenc.c
+++ b/libavcodec/libspeexenc.c
@@ -87,6 +87,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "codec_internal.h"
diff --git a/libavcodec/libsvtav1.c b/libavcodec/libsvtav1.c
index 8fa42d590b..105c3369c0 100644
--- a/libavcodec/libsvtav1.c
+++ b/libavcodec/libsvtav1.c
@@ -29,12 +29,12 @@
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mastering_display_metadata.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/avassert.h"
 
 #include "codec_internal.h"
-#include "internal.h"
 #include "encode.h"
 #include "packet_internal.h"
 #include "avcodec.h"
diff --git a/libavcodec/libtheoraenc.c b/libavcodec/libtheoraenc.c
index 06eeaae006..58da008a82 100644
--- a/libavcodec/libtheoraenc.c
+++ b/libavcodec/libtheoraenc.c
@@ -33,6 +33,7 @@
 /* FFmpeg includes */
 #include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/log.h"
 #include "libavutil/base64.h"
diff --git a/libavcodec/libvorbisenc.c b/libavcodec/libvorbisenc.c
index 6331cf0d79..e4f8cb67ef 100644
--- a/libavcodec/libvorbisenc.c
+++ b/libavcodec/libvorbisenc.c
@@ -23,6 +23,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/fifo.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "audio_frame_queue.h"
diff --git a/libavcodec/libvpxenc.c b/libavcodec/libvpxenc.c
index 4b89e47e83..554a3b55ef 100644
--- a/libavcodec/libvpxenc.c
+++ b/libavcodec/libvpxenc.c
@@ -33,8 +33,8 @@
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "encode.h"
-#include "internal.h"
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libvpx.h"
 #include "packet_internal.h"
 #include "profiles.h"
diff --git a/libavcodec/libwebpenc.c b/libavcodec/libwebpenc.c
index d6edd86603..f40dd01345 100644
--- a/libavcodec/libwebpenc.c
+++ b/libavcodec/libwebpenc.c
@@ -24,6 +24,7 @@
  * WebP encoder using libwebp (WebPEncode API)
  */
 
+#include "libavutil/mem.h"
 #include "codec_internal.h"
 #include "encode.h"
 #include "libwebpenc_common.h"
diff --git a/libavcodec/libwebpenc_animencoder.c b/libavcodec/libwebpenc_animencoder.c
index 02f0b6f64d..67884bf432 100644
--- a/libavcodec/libwebpenc_animencoder.c
+++ b/libavcodec/libwebpenc_animencoder.c
@@ -25,6 +25,7 @@
  */
 
 #include "libavutil/buffer.h"
+#include "libavutil/mem.h"
 
 #include "config.h"
 #include "codec_internal.h"
diff --git a/libavcodec/libwebpenc_common.c b/libavcodec/libwebpenc_common.c
index 5645d11727..80040ea9e3 100644
--- a/libavcodec/libwebpenc_common.c
+++ b/libavcodec/libwebpenc_common.c
@@ -24,6 +24,7 @@
  * WebP encoder using libwebp: common structs and methods.
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libwebpenc_common.h"
 
diff --git a/libavcodec/libx265.c b/libavcodec/libx265.c
index d3e74eaacf..40ddce6030 100644
--- a/libavcodec/libx265.c
+++ b/libavcodec/libx265.c
@@ -31,6 +31,7 @@
 #include "libavutil/buffer.h"
 #include "libavutil/internal.h"
 #include "libavutil/mastering_display_metadata.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avcodec.h"
diff --git a/libavcodec/libxeve.c b/libavcodec/libxeve.c
index c923ac1cac..1d9972619d 100644
--- a/libavcodec/libxeve.c
+++ b/libavcodec/libxeve.c
@@ -28,6 +28,7 @@
 
 #include "libavutil/internal.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/pixfmt.h"
diff --git a/libavcodec/libzvbi-teletextdec.c b/libavcodec/libzvbi-teletextdec.c
index c2cbb0a424..68ffe1f76c 100644
--- a/libavcodec/libzvbi-teletextdec.c
+++ b/libavcodec/libzvbi-teletextdec.c
@@ -22,10 +22,10 @@
 #include "libavcodec/ass.h"
 #include "codec_internal.h"
 #include "libavcodec/dvbtxt.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/bprint.h"
 #include "libavutil/internal.h"
-#include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
 #include "libavutil/common.h"
 
diff --git a/libavcodec/lpc.c b/libavcodec/lpc.c
index 53f5c3d379..8305cc0596 100644
--- a/libavcodec/lpc.c
+++ b/libavcodec/lpc.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/common.h"
 #include "libavutil/lls.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #define LPC_USE_DOUBLE
diff --git a/libavcodec/lscrdec.c b/libavcodec/lscrdec.c
index 415914bf0a..199d2d6827 100644
--- a/libavcodec/lscrdec.c
+++ b/libavcodec/lscrdec.c
@@ -24,6 +24,7 @@
 #include "libavutil/frame.h"
 #include "libavutil/error.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
 #include "bytestream.h"
diff --git a/libavcodec/magicyuv.c b/libavcodec/magicyuv.c
index 3f6348b531..d2897019bd 100644
--- a/libavcodec/magicyuv.c
+++ b/libavcodec/magicyuv.c
@@ -24,6 +24,7 @@
 
 #define CACHED_BITSTREAM_READER !ARCH_X86_32
 
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/magicyuvenc.c b/libavcodec/magicyuvenc.c
index 1c3ab56460..3ae1ee2c71 100644
--- a/libavcodec/magicyuvenc.c
+++ b/libavcodec/magicyuvenc.c
@@ -23,6 +23,7 @@
 #include <string.h>
 
 #include "libavutil/cpu.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/qsort.h"
@@ -32,7 +33,6 @@
 #include "codec_internal.h"
 #include "encode.h"
 #include "put_bits.h"
-#include "thread.h"
 #include "lossless_videoencdsp.h"
 
 #define MAGICYUV_EXTRADATA_SIZE 32
diff --git a/libavcodec/mdec.c b/libavcodec/mdec.c
index c4904216b8..31c09de5bc 100644
--- a/libavcodec/mdec.c
+++ b/libavcodec/mdec.c
@@ -27,6 +27,7 @@
  * This is very similar to intra-only MPEG-1.
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/mediacodecdec.c b/libavcodec/mediacodecdec.c
index b8587289a2..df8d40dd10 100644
--- a/libavcodec/mediacodecdec.c
+++ b/libavcodec/mediacodecdec.c
@@ -27,6 +27,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/pixfmt.h"
diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c
index 984014f1b1..b59de75b9b 100644
--- a/libavcodec/mediacodecenc.c
+++ b/libavcodec/mediacodecenc.c
@@ -25,6 +25,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/hwcontext_mediacodec.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/mfenc.c b/libavcodec/mfenc.c
index 9225692c51..bdea4b42c4 100644
--- a/libavcodec/mfenc.c
+++ b/libavcodec/mfenc.c
@@ -25,6 +25,7 @@
 #include "encode.h"
 #include "mf_utils.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/time.h"
 #include "codec_internal.h"
diff --git a/libavcodec/mimic.c b/libavcodec/mimic.c
index cd5d0d7e3e..8928f24022 100644
--- a/libavcodec/mimic.c
+++ b/libavcodec/mimic.c
@@ -21,6 +21,7 @@
 
 #include <stdint.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/thread.h"
 
diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index c9409eac6c..1481a7f285 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -36,6 +36,7 @@
 #include "libavutil/emms.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "blockdsp.h"
diff --git a/libavcodec/mjpegenc.c b/libavcodec/mjpegenc.c
index 521c9e8aff..51bce103da 100644
--- a/libavcodec/mjpegenc.c
+++ b/libavcodec/mjpegenc.c
@@ -32,6 +32,7 @@
 
 #include "config_components.h"
 
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/mlz.c b/libavcodec/mlz.c
index 9087ffd8f0..5176366439 100644
--- a/libavcodec/mlz.c
+++ b/libavcodec/mlz.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "mlz.h"
 
 av_cold int ff_mlz_init_dict(void *context, MLZ *mlz)
diff --git a/libavcodec/mmaldec.c b/libavcodec/mmaldec.c
index a8cda27570..e42591110f 100644
--- a/libavcodec/mmaldec.c
+++ b/libavcodec/mmaldec.c
@@ -42,6 +42,7 @@
 #include "libavutil/buffer.h"
 #include "libavutil/common.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/log.h"
 
diff --git a/libavcodec/mobiclip.c b/libavcodec/mobiclip.c
index 433de0abb2..133a835ad1 100644
--- a/libavcodec/mobiclip.c
+++ b/libavcodec/mobiclip.c
@@ -24,6 +24,7 @@
 #include <inttypes.h>
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/motionpixels.c b/libavcodec/motionpixels.c
index 9df0b20676..287f35f305 100644
--- a/libavcodec/motionpixels.c
+++ b/libavcodec/motionpixels.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 
 #include "config.h"
diff --git a/libavcodec/mpc7.c b/libavcodec/mpc7.c
index b23b4094db..f1ce4075cd 100644
--- a/libavcodec/mpc7.c
+++ b/libavcodec/mpc7.c
@@ -28,6 +28,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/internal.h"
 #include "libavutil/lfg.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/thread.h"
 
diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
index 4ad1eb6572..da9a85f3ed 100644
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -46,7 +46,6 @@
 #include "hwaccel_internal.h"
 #include "hwconfig.h"
 #include "idctdsp.h"
-#include "internal.h"
 #include "mpeg_er.h"
 #include "mpeg12.h"
 #include "mpeg12codecs.h"
diff --git a/libavcodec/mpeg4videoenc.c b/libavcodec/mpeg4videoenc.c
index d1e50612dd..f806ad8a74 100644
--- a/libavcodec/mpeg4videoenc.c
+++ b/libavcodec/mpeg4videoenc.c
@@ -22,6 +22,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/thread.h"
 #include "codec_internal.h"
diff --git a/libavcodec/mpeg_er.c b/libavcodec/mpeg_er.c
index 8034963253..d429b0a839 100644
--- a/libavcodec/mpeg_er.c
+++ b/libavcodec/mpeg_er.c
@@ -16,6 +16,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "error_resilience.h"
 #include "mpegvideo.h"
 #include "mpegvideodec.h"
diff --git a/libavcodec/mpegaudiodec_template.c b/libavcodec/mpegaudiodec_template.c
index 2b84e65705..c73b1e0054 100644
--- a/libavcodec/mpegaudiodec_template.c
+++ b/libavcodec/mpegaudiodec_template.c
@@ -32,6 +32,7 @@
 #include "libavutil/crc.h"
 #include "libavutil/float_dsp.h"
 #include "libavutil/libm.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/thread.h"
 
diff --git a/libavcodec/mpegpicture.c b/libavcodec/mpegpicture.c
index 270630bea6..5bf85bb7fe 100644
--- a/libavcodec/mpegpicture.c
+++ b/libavcodec/mpegpicture.c
@@ -22,6 +22,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/imgutils.h"
 
diff --git a/libavcodec/mpegutils.c b/libavcodec/mpegutils.c
index fc3e270631..7a792a9be0 100644
--- a/libavcodec/mpegutils.c
+++ b/libavcodec/mpegutils.c
@@ -24,6 +24,7 @@
 #include "libavutil/common.h"
 #include "libavutil/emms.h"
 #include "libavutil/frame.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/motion_vector.h"
 #include "libavutil/avassert.h"
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 81796e42bb..8a733afdb8 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -31,6 +31,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
 #include "blockdsp.h"
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index c20e364cac..d1b1917824 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -39,6 +39,7 @@
 #include "libavutil/internal.h"
 #include "libavutil/intmath.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 #include "libavutil/thread.h"
diff --git a/libavcodec/mscc.c b/libavcodec/mscc.c
index d1d23e6751..39bfad0b98 100644
--- a/libavcodec/mscc.c
+++ b/libavcodec/mscc.c
@@ -23,6 +23,7 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/mss12.c b/libavcodec/mss12.c
index 5afdaacfe6..8dd3b4fc8a 100644
--- a/libavcodec/mss12.c
+++ b/libavcodec/mss12.c
@@ -27,6 +27,7 @@
 
 #include "libavutil/intfloat.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "mss12.h"
 
diff --git a/libavcodec/mss2.c b/libavcodec/mss2.c
index d54a1220b5..dd0d403338 100644
--- a/libavcodec/mss2.c
+++ b/libavcodec/mss2.c
@@ -24,12 +24,12 @@
  */
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "codec_internal.h"
 #include "decode.h"
 #include "error_resilience.h"
 #include "mpeg_er.h"
 #include "mpegvideodec.h"
-#include "qpeldsp.h"
 #include "vc1.h"
 #include "wmv2data.h"
 #include "mss12.h"
diff --git a/libavcodec/mss3.c b/libavcodec/mss3.c
index 2701e9b912..257943f648 100644
--- a/libavcodec/mss3.c
+++ b/libavcodec/mss3.c
@@ -24,6 +24,7 @@
  * Microsoft Screen 3 (aka Microsoft ATC Screen) decoder
  */
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/mss4.c b/libavcodec/mss4.c
index 8ae4f152c6..c931aa4e4a 100644
--- a/libavcodec/mss4.c
+++ b/libavcodec/mss4.c
@@ -25,6 +25,7 @@
  * aka Microsoft Expression Encoder Screen) decoder
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 #include "libavutil/imgutils.h"
 
diff --git a/libavcodec/msvideo1enc.c b/libavcodec/msvideo1enc.c
index 3bea3ed297..b8704367c7 100644
--- a/libavcodec/msvideo1enc.c
+++ b/libavcodec/msvideo1enc.c
@@ -29,6 +29,7 @@
 #include "encode.h"
 #include "bytestream.h"
 #include "libavutil/lfg.h"
+#include "libavutil/mem.h"
 #include "elbg.h"
 #include "libavutil/imgutils.h"
 /**
diff --git a/libavcodec/mv30.c b/libavcodec/mv30.c
index 8c45c8304b..124a37e23f 100644
--- a/libavcodec/mv30.c
+++ b/libavcodec/mv30.c
@@ -23,6 +23,7 @@
 #include <stddef.h>
 #include <string.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/mwsc.c b/libavcodec/mwsc.c
index f8c53c33ff..06a151a72a 100644
--- a/libavcodec/mwsc.c
+++ b/libavcodec/mwsc.c
@@ -22,6 +22,7 @@
 
 #include <stdio.h>
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/mxpegdec.c b/libavcodec/mxpegdec.c
index 73df2ff9ff..c5c14cbe79 100644
--- a/libavcodec/mxpegdec.c
+++ b/libavcodec/mxpegdec.c
@@ -25,6 +25,7 @@
  * MxPEG decoder
  */
 
+#include "libavutil/mem.h"
 #include "codec_internal.h"
 #include "decode.h"
 #include "mjpeg.h"
diff --git a/libavcodec/nellymoserdec.c b/libavcodec/nellymoserdec.c
index 727cd4833d..e9b124399a 100644
--- a/libavcodec/nellymoserdec.c
+++ b/libavcodec/nellymoserdec.c
@@ -34,6 +34,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/float_dsp.h"
 #include "libavutil/lfg.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/tx.h"
 
diff --git a/libavcodec/nellymoserenc.c b/libavcodec/nellymoserenc.c
index a550ae46c5..32ae5f7828 100644
--- a/libavcodec/nellymoserenc.c
+++ b/libavcodec/nellymoserenc.c
@@ -38,6 +38,7 @@
 #include "libavutil/common.h"
 #include "libavutil/float_dsp.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 #include "libavutil/tx.h"
 
diff --git a/libavcodec/notchlc.c b/libavcodec/notchlc.c
index 0feb0918f0..6dd3f88440 100644
--- a/libavcodec/notchlc.c
+++ b/libavcodec/notchlc.c
@@ -23,6 +23,7 @@
 #include <string.h>
 
 #define BITSTREAM_READER_LE
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/nuv.c b/libavcodec/nuv.c
index 1dda770c97..c2301be77b 100644
--- a/libavcodec/nuv.c
+++ b/libavcodec/nuv.c
@@ -26,6 +26,7 @@
 #include "libavutil/intreadwrite.h"
 #include "libavutil/lzo.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
diff --git a/libavcodec/nvdec.c b/libavcodec/nvdec.c
index 553c9bdf18..ffd5cc61e2 100644
--- a/libavcodec/nvdec.c
+++ b/libavcodec/nvdec.c
@@ -28,6 +28,7 @@
 #include "libavutil/hwcontext.h"
 #include "libavutil/hwcontext_cuda_internal.h"
 #include "libavutil/cuda_check.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/pixfmt.h"
 
diff --git a/libavcodec/nvdec_av1.c b/libavcodec/nvdec_av1.c
index 35f22ebf80..b0b013846e 100644
--- a/libavcodec/nvdec_av1.c
+++ b/libavcodec/nvdec_av1.c
@@ -20,6 +20,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "nvdec.h"
 #include "decode.h"
diff --git a/libavcodec/nvdec_h264.c b/libavcodec/nvdec_h264.c
index ea6c1081eb..9adbc521ec 100644
--- a/libavcodec/nvdec_h264.c
+++ b/libavcodec/nvdec_h264.c
@@ -23,6 +23,7 @@
 #include <stdint.h>
 #include <string.h>
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "nvdec.h"
 #include "decode.h"
diff --git a/libavcodec/nvdec_hevc.c b/libavcodec/nvdec_hevc.c
index ff118af04b..f26ced71ef 100644
--- a/libavcodec/nvdec_hevc.c
+++ b/libavcodec/nvdec_hevc.c
@@ -23,6 +23,7 @@
 #include <stdint.h>
 #include <string.h>
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "nvdec.h"
 #include "decode.h"
diff --git a/libavcodec/omx.c b/libavcodec/omx.c
index 3fdde753b5..86913237b2 100644
--- a/libavcodec/omx.c
+++ b/libavcodec/omx.c
@@ -38,6 +38,7 @@
 #include "libavutil/common.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/on2avc.c b/libavcodec/on2avc.c
index 8d0c881248..3badfae891 100644
--- a/libavcodec/on2avc.c
+++ b/libavcodec/on2avc.c
@@ -23,6 +23,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/ffmath.h"
 #include "libavutil/float_dsp.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/tx.h"
 
diff --git a/libavcodec/opus_parser.c b/libavcodec/opus_parser.c
index e6df34abab..3bdacf7cd5 100644
--- a/libavcodec/opus_parser.c
+++ b/libavcodec/opus_parser.c
@@ -25,6 +25,7 @@
  * Determines the duration for each packet.
  */
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "opus.h"
diff --git a/libavcodec/opus_pvq.c b/libavcodec/opus_pvq.c
index 0a6dc4cdf1..554a72609f 100644
--- a/libavcodec/opus_pvq.c
+++ b/libavcodec/opus_pvq.c
@@ -27,6 +27,7 @@
 
 #include "config_components.h"
 
+#include "libavutil/mem.h"
 #include "mathops.h"
 #include "opustab.h"
 #include "opus_pvq.h"
diff --git a/libavcodec/opus_silk.c b/libavcodec/opus_silk.c
index 27671516a0..98ffe00b26 100644
--- a/libavcodec/opus_silk.c
+++ b/libavcodec/opus_silk.c
@@ -26,6 +26,7 @@
 
 #include <stdint.h>
 
+#include "libavutil/mem.h"
 #include "mathops.h"
 #include "opus.h"
 #include "opus_rc.h"
diff --git a/libavcodec/opusdec.c b/libavcodec/opusdec.c
index c5f06e0600..81653b5bd0 100644
--- a/libavcodec/opusdec.c
+++ b/libavcodec/opusdec.c
@@ -41,6 +41,7 @@
 #include "libavutil/ffmath.h"
 #include "libavutil/float_dsp.h"
 #include "libavutil/frame.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 
diff --git a/libavcodec/opusdec_celt.c b/libavcodec/opusdec_celt.c
index c2904cc9e0..fd8e9929e9 100644
--- a/libavcodec/opusdec_celt.c
+++ b/libavcodec/opusdec_celt.c
@@ -27,6 +27,7 @@
 
 #include <float.h>
 
+#include "libavutil/mem.h"
 #include "opus_celt.h"
 #include "opustab.h"
 #include "opus_pvq.h"
diff --git a/libavcodec/opusenc.c b/libavcodec/opusenc.c
index 379d0e0c5d..e0c1732227 100644
--- a/libavcodec/opusenc.c
+++ b/libavcodec/opusenc.c
@@ -29,6 +29,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/float_dsp.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 #include "bytestream.h"
diff --git a/libavcodec/opusenc_psy.c b/libavcodec/opusenc_psy.c
index 48ccd2ebd0..44a61e6939 100644
--- a/libavcodec/opusenc_psy.c
+++ b/libavcodec/opusenc_psy.c
@@ -21,6 +21,7 @@
 
 #include <float.h>
 
+#include "libavutil/mem.h"
 #include "opusenc_psy.h"
 #include "opus_celt.h"
 #include "opus_pvq.h"
diff --git a/libavcodec/osq.c b/libavcodec/osq.c
index f47213bf0c..315b6cafb7 100644
--- a/libavcodec/osq.c
+++ b/libavcodec/osq.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
diff --git a/libavcodec/pafvideo.c b/libavcodec/pafvideo.c
index 14eb42435a..c17ae9ae4d 100644
--- a/libavcodec/pafvideo.c
+++ b/libavcodec/pafvideo.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
 #include "bytestream.h"
diff --git a/libavcodec/pcm.c b/libavcodec/pcm.c
index 4abca7cc07..a51086a92d 100644
--- a/libavcodec/pcm.c
+++ b/libavcodec/pcm.c
@@ -28,6 +28,7 @@
 #include "config_components.h"
 #include "libavutil/attributes.h"
 #include "libavutil/float_dsp.h"
+#include "libavutil/mem.h"
 #include "libavutil/reverse.h"
 #include "libavutil/thread.h"
 #include "avcodec.h"
diff --git a/libavcodec/pcx.c b/libavcodec/pcx.c
index 8e2aae7b5e..15f2e3c2cf 100644
--- a/libavcodec/pcx.c
+++ b/libavcodec/pcx.c
@@ -22,6 +22,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/pgssubdec.c b/libavcodec/pgssubdec.c
index 5f76f12615..d93bcf1b6a 100644
--- a/libavcodec/pgssubdec.c
+++ b/libavcodec/pgssubdec.c
@@ -31,6 +31,7 @@
 #include "mathops.h"
 
 #include "libavutil/colorspace.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
diff --git a/libavcodec/pictordec.c b/libavcodec/pictordec.c
index 5aaa725bd3..845a882454 100644
--- a/libavcodec/pictordec.c
+++ b/libavcodec/pictordec.c
@@ -25,6 +25,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "cga_data.h"
diff --git a/libavcodec/pixlet.c b/libavcodec/pixlet.c
index 6e925308b8..f432b15d71 100644
--- a/libavcodec/pixlet.c
+++ b/libavcodec/pixlet.c
@@ -22,6 +22,7 @@
 #include <stdint.h>
 
 #include "libavutil/intmath.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
 #include "bytestream.h"
diff --git a/libavcodec/pngdec.c b/libavcodec/pngdec.c
index de50e6a5b6..16e35a8cc6 100644
--- a/libavcodec/pngdec.c
+++ b/libavcodec/pngdec.c
@@ -30,6 +30,7 @@
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mastering_display_metadata.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixfmt.h"
 #include "libavutil/rational.h"
 #include "libavutil/stereo3d.h"
diff --git a/libavcodec/pngenc.c b/libavcodec/pngenc.c
index 28d645ea48..89c3d5b83e 100644
--- a/libavcodec/pngenc.c
+++ b/libavcodec/pngenc.c
@@ -33,6 +33,7 @@
 #include "libavutil/csp.h"
 #include "libavutil/libm.h"
 #include "libavutil/mastering_display_metadata.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/rational.h"
 #include "libavutil/stereo3d.h"
diff --git a/libavcodec/proresdec2.c b/libavcodec/proresdec2.c
index 3a5b753430..73fbd3458e 100644
--- a/libavcodec/proresdec2.c
+++ b/libavcodec/proresdec2.c
@@ -31,6 +31,7 @@
 #include "config_components.h"
 
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/proresenc_anatoliy.c b/libavcodec/proresenc_anatoliy.c
index 02ab07d28e..2fb96e9cf5 100644
--- a/libavcodec/proresenc_anatoliy.c
+++ b/libavcodec/proresenc_anatoliy.c
@@ -27,6 +27,7 @@
  * Known FOURCCs: 'ap4h' (444), 'apch' (HQ), 'apcn' (422), 'apcs' (LT), 'acpo' (Proxy)
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
diff --git a/libavcodec/proresenc_kostya.c b/libavcodec/proresenc_kostya.c
index 90cc87e388..b2b5f880ae 100644
--- a/libavcodec/proresenc_kostya.c
+++ b/libavcodec/proresenc_kostya.c
@@ -21,6 +21,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavcodec/psd.c b/libavcodec/psd.c
index d3456e6b3d..3e5bfcd360 100644
--- a/libavcodec/psd.c
+++ b/libavcodec/psd.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "bytestream.h"
 #include "codec_internal.h"
 #include "decode.h"
diff --git a/libavcodec/qsv.c b/libavcodec/qsv.c
index 7563625627..d2a18d3b40 100644
--- a/libavcodec/qsv.c
+++ b/libavcodec/qsv.c
@@ -31,6 +31,7 @@
 #include "libavutil/hwcontext_qsv.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
 #include "qsv_internal.h"
diff --git a/libavcodec/qsvenc_hevc.c b/libavcodec/qsvenc_hevc.c
index 4920b0bca4..2ccbe0464b 100644
--- a/libavcodec/qsvenc_hevc.c
+++ b/libavcodec/qsvenc_hevc.c
@@ -25,6 +25,7 @@
 #include <mfxvideo.h>
 
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/mastering_display_metadata.h"
 
diff --git a/libavcodec/qtrleenc.c b/libavcodec/qtrleenc.c
index 92e6e84380..71d57d7eb8 100644
--- a/libavcodec/qtrleenc.c
+++ b/libavcodec/qtrleenc.c
@@ -23,6 +23,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/ra288.c b/libavcodec/ra288.c
index e4b14ef2a2..5b186a7a3d 100644
--- a/libavcodec/ra288.c
+++ b/libavcodec/ra288.c
@@ -22,6 +22,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/float_dsp.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #define BITSTREAM_READER_LE
diff --git a/libavcodec/rasc.c b/libavcodec/rasc.c
index 21c1829fc7..7b0251ca3f 100644
--- a/libavcodec/rasc.c
+++ b/libavcodec/rasc.c
@@ -23,6 +23,7 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/ratecontrol.c b/libavcodec/ratecontrol.c
index 55ce054735..1e861e20c1 100644
--- a/libavcodec/ratecontrol.c
+++ b/libavcodec/ratecontrol.c
@@ -28,6 +28,7 @@
 #include "libavutil/attributes.h"
 #include "libavutil/emms.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
 #include "ratecontrol.h"
diff --git a/libavcodec/rawdec.c b/libavcodec/rawdec.c
index 8e9358f95d..8de90e0477 100644
--- a/libavcodec/rawdec.c
+++ b/libavcodec/rawdec.c
@@ -29,13 +29,12 @@
 #include "codec_internal.h"
 #include "decode.h"
 #include "get_bits.h"
-#include "internal.h"
 #include "raw.h"
 #include "libavutil/avassert.h"
 #include "libavutil/buffer.h"
-#include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 typedef struct RawVideoContext {
diff --git a/libavcodec/rka.c b/libavcodec/rka.c
index 724107f3e2..600c160028 100644
--- a/libavcodec/rka.c
+++ b/libavcodec/rka.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
 #include "codec_internal.h"
diff --git a/libavcodec/rkmppdec.c b/libavcodec/rkmppdec.c
index 53b6f6d5e8..7665098c6a 100644
--- a/libavcodec/rkmppdec.c
+++ b/libavcodec/rkmppdec.c
@@ -37,6 +37,7 @@
 #include "libavutil/hwcontext_drm.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 
 #define RECEIVE_FRAME_TIMEOUT   100
 #define FRAMEGROUP_MAX_FRAMES   16
diff --git a/libavcodec/roqaudioenc.c b/libavcodec/roqaudioenc.c
index 81dccd09b5..8d08a13654 100644
--- a/libavcodec/roqaudioenc.c
+++ b/libavcodec/roqaudioenc.c
@@ -21,6 +21,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/roqvideoenc.c b/libavcodec/roqvideoenc.c
index 0933abf4f9..a6c167b1dc 100644
--- a/libavcodec/roqvideoenc.c
+++ b/libavcodec/roqvideoenc.c
@@ -58,6 +58,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/lfg.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "roqvideo.h"
 #include "bytestream.h"
diff --git a/libavcodec/rscc.c b/libavcodec/rscc.c
index ace9aeeb40..e601ff39ed 100644
--- a/libavcodec/rscc.c
+++ b/libavcodec/rscc.c
@@ -40,6 +40,7 @@
 
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
 #include "bytestream.h"
diff --git a/libavcodec/rv34.c b/libavcodec/rv34.c
index 914bde4a2a..23a570bb80 100644
--- a/libavcodec/rv34.c
+++ b/libavcodec/rv34.c
@@ -27,6 +27,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/thread.h"
 
diff --git a/libavcodec/samidec.c b/libavcodec/samidec.c
index e84c4bc6b8..6576ce93fe 100644
--- a/libavcodec/samidec.c
+++ b/libavcodec/samidec.c
@@ -27,6 +27,7 @@
 #include "ass.h"
 #include "libavutil/avstring.h"
 #include "libavutil/bprint.h"
+#include "libavutil/mem.h"
 #include "codec_internal.h"
 #include "htmlsubtitles.h"
 
diff --git a/libavcodec/sanm.c b/libavcodec/sanm.c
index b70daab7ac..8b7c0d9556 100644
--- a/libavcodec/sanm.c
+++ b/libavcodec/sanm.c
@@ -21,6 +21,7 @@
  */
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
 #include "bytestream.h"
diff --git a/libavcodec/scpr.c b/libavcodec/scpr.c
index b096965de5..9a6e8bdcb4 100644
--- a/libavcodec/scpr.c
+++ b/libavcodec/scpr.c
@@ -22,6 +22,7 @@
 
 #include <string.h>
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/sga.c b/libavcodec/sga.c
index 4ced6e9890..0f42cf912b 100644
--- a/libavcodec/sga.c
+++ b/libavcodec/sga.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "get_bits.h"
 #include "bytestream.h"
diff --git a/libavcodec/sgienc.c b/libavcodec/sgienc.c
index 5bbb72c03a..0901cf58b3 100644
--- a/libavcodec/sgienc.c
+++ b/libavcodec/sgienc.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/shorten.c b/libavcodec/shorten.c
index 0ad95bf97e..102992e2b2 100644
--- a/libavcodec/shorten.c
+++ b/libavcodec/shorten.c
@@ -26,6 +26,7 @@
  */
 
 #include <limits.h>
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bswapdsp.h"
 #include "bytestream.h"
diff --git a/libavcodec/siren.c b/libavcodec/siren.c
index fa8636946d..e76f21bc7e 100644
--- a/libavcodec/siren.c
+++ b/libavcodec/siren.c
@@ -22,6 +22,7 @@
  */
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/tx.h"
 #include "libavutil/float_dsp.h"
 #include "libavutil/mem_internal.h"
diff --git a/libavcodec/smacker.c b/libavcodec/smacker.c
index ffa0820f52..8f198d6957 100644
--- a/libavcodec/smacker.c
+++ b/libavcodec/smacker.c
@@ -31,6 +31,7 @@
 #include <stddef.h>
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
 
diff --git a/libavcodec/snow.c b/libavcodec/snow.c
index 59815d00d9..0285362d43 100644
--- a/libavcodec/snow.c
+++ b/libavcodec/snow.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 #include "avcodec.h"
 #include "snow_dwt.h"
diff --git a/libavcodec/snow_dwt.c b/libavcodec/snow_dwt.c
index 965f409002..1250597ee0 100644
--- a/libavcodec/snow_dwt.c
+++ b/libavcodec/snow_dwt.c
@@ -22,6 +22,7 @@
 #include "libavutil/attributes.h"
 #include "libavutil/avassert.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "me_cmp.h"
 #include "snow_dwt.h"
 
diff --git a/libavcodec/snowdec.c b/libavcodec/snowdec.c
index 70fbab9a49..50dcaf8b93 100644
--- a/libavcodec/snowdec.c
+++ b/libavcodec/snowdec.c
@@ -21,7 +21,7 @@
 #include "libavutil/emms.h"
 #include "libavutil/intmath.h"
 #include "libavutil/log.h"
-#include "libavutil/opt.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
diff --git a/libavcodec/snowenc.c b/libavcodec/snowenc.c
index d81ff6f2aa..43ca602762 100644
--- a/libavcodec/snowenc.c
+++ b/libavcodec/snowenc.c
@@ -22,6 +22,7 @@
 #include "libavutil/intmath.h"
 #include "libavutil/libm.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avcodec.h"
diff --git a/libavcodec/sonic.c b/libavcodec/sonic.c
index 0544fecf46..46c15b5fb1 100644
--- a/libavcodec/sonic.c
+++ b/libavcodec/sonic.c
@@ -21,6 +21,7 @@
 
 #include "config_components.h"
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
diff --git a/libavcodec/sp5xdec.c b/libavcodec/sp5xdec.c
index dfed725500..8dda5b6152 100644
--- a/libavcodec/sp5xdec.c
+++ b/libavcodec/sp5xdec.c
@@ -26,9 +26,9 @@
 
 #include "config_components.h"
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
-#include "mjpeg.h"
 #include "mjpegdec.h"
 #include "sp5x.h"
 
diff --git a/libavcodec/speexdec.c b/libavcodec/speexdec.c
index ba0df687de..d25823ef6e 100644
--- a/libavcodec/speexdec.c
+++ b/libavcodec/speexdec.c
@@ -54,6 +54,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 #include "libavutil/float_dsp.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/sunrast.c b/libavcodec/sunrast.c
index 3668d2be7f..9e49c4f275 100644
--- a/libavcodec/sunrast.c
+++ b/libavcodec/sunrast.c
@@ -22,6 +22,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
diff --git a/libavcodec/svq1dec.c b/libavcodec/svq1dec.c
index d2cfe4cf40..57cac8ee7b 100644
--- a/libavcodec/svq1dec.c
+++ b/libavcodec/svq1dec.c
@@ -33,6 +33,7 @@
  */
 
 #include "libavutil/crc.h"
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/svq1enc.c b/libavcodec/svq1enc.c
index 77dbf07275..d71ad07b86 100644
--- a/libavcodec/svq1enc.c
+++ b/libavcodec/svq1enc.c
@@ -27,6 +27,7 @@
  */
 
 #include "libavutil/emms.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "encode.h"
diff --git a/libavcodec/svq3.c b/libavcodec/svq3.c
index d2f53742e1..ce4c58e14f 100644
--- a/libavcodec/svq3.c
+++ b/libavcodec/svq3.c
@@ -44,6 +44,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/crc.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #include "codec_internal.h"
diff --git a/libavcodec/takdec.c b/libavcodec/takdec.c
index 5cbc2de6bd..cfa69f4217 100644
--- a/libavcodec/takdec.c
+++ b/libavcodec/takdec.c
@@ -26,6 +26,7 @@
  */
 
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/samplefmt.h"
 
diff --git a/libavcodec/tdsc.c b/libavcodec/tdsc.c
index 624e219cb4..ab0a70859b 100644
--- a/libavcodec/tdsc.c
+++ b/libavcodec/tdsc.c
@@ -37,6 +37,7 @@
 #include <zlib.h>
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
 #include "bytestream.h"
diff --git a/libavcodec/tests/avpacket.c b/libavcodec/tests/avpacket.c
index 7a70ade4c3..fed700b4be 100644
--- a/libavcodec/tests/avpacket.c
+++ b/libavcodec/tests/avpacket.c
@@ -22,6 +22,7 @@
 #include <string.h>
 #include "libavcodec/avcodec.h"
 #include "libavutil/error.h"
+#include "libavutil/mem.h"
 
 
 
diff --git a/libavcodec/tests/snowenc.c b/libavcodec/tests/snowenc.c
index 3dad07d3a5..eb4e64f377 100644
--- a/libavcodec/tests/snowenc.c
+++ b/libavcodec/tests/snowenc.c
@@ -26,6 +26,7 @@
 
 #include "libavutil/lfg.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 
 int main(void){
 #define width  256
diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 7ce1ab32f6..1b934457b5 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -39,6 +39,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/error.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/reverse.h"
 #include "avcodec.h"
diff --git a/libavcodec/tiff_common.c b/libavcodec/tiff_common.c
index 2b872ea7e2..22ebca814c 100644
--- a/libavcodec/tiff_common.c
+++ b/libavcodec/tiff_common.c
@@ -26,6 +26,7 @@
  */
 
 #include "libavutil/bprint.h"
+#include "libavutil/mem.h"
 #include "tiff_common.h"
 
 
diff --git a/libavcodec/tiffenc.c b/libavcodec/tiffenc.c
index 7c3c03f1f3..25539d03dc 100644
--- a/libavcodec/tiffenc.c
+++ b/libavcodec/tiffenc.c
@@ -31,6 +31,7 @@
 #endif
 
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avcodec.h"
diff --git a/libavcodec/truemotion2.c b/libavcodec/truemotion2.c
index 366d8aefc1..7ae9a67ec4 100644
--- a/libavcodec/truemotion2.c
+++ b/libavcodec/truemotion2.c
@@ -26,6 +26,7 @@
 
 #include <inttypes.h>
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bswapdsp.h"
 #include "bytestream.h"
diff --git a/libavcodec/tscc.c b/libavcodec/tscc.c
index 346d93e1f2..575173698c 100644
--- a/libavcodec/tscc.c
+++ b/libavcodec/tscc.c
@@ -34,6 +34,7 @@
  * Supports: BGR8,BGR555,BGR24 - only BGR8 and BGR555 tested
  */
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
diff --git a/libavcodec/tscc2.c b/libavcodec/tscc2.c
index 168efe93f6..cfe5a6e752 100644
--- a/libavcodec/tscc2.c
+++ b/libavcodec/tscc2.c
@@ -26,6 +26,7 @@
 
 #include <inttypes.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 
 #define BITSTREAM_READER_LE
diff --git a/libavcodec/tta.c b/libavcodec/tta.c
index 7763ed7ffc..467c78514f 100644
--- a/libavcodec/tta.c
+++ b/libavcodec/tta.c
@@ -32,6 +32,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/crc.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #define BITSTREAM_READER_LE
diff --git a/libavcodec/ttaenc.c b/libavcodec/ttaenc.c
index db79c38b43..1b9aeec8ff 100644
--- a/libavcodec/ttaenc.c
+++ b/libavcodec/ttaenc.c
@@ -26,6 +26,7 @@
 #include "encode.h"
 #include "put_bits.h"
 #include "libavutil/crc.h"
+#include "libavutil/mem.h"
 
 typedef struct TTAEncContext {
     const AVCRC *crc_table;
diff --git a/libavcodec/ttmlenc.c b/libavcodec/ttmlenc.c
index 779cb75199..897e16ebb1 100644
--- a/libavcodec/ttmlenc.c
+++ b/libavcodec/ttmlenc.c
@@ -31,6 +31,7 @@
 #include "codec_internal.h"
 #include "libavutil/bprint.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "ass_split.h"
 #include "ttmlenc.h"
 
diff --git a/libavcodec/twinvq.c b/libavcodec/twinvq.c
index 4cc0583f8a..69d8f5536a 100644
--- a/libavcodec/twinvq.c
+++ b/libavcodec/twinvq.c
@@ -24,6 +24,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/float_dsp.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "decode.h"
 #include "lsp.h"
diff --git a/libavcodec/utvideodec.c b/libavcodec/utvideodec.c
index ce5d00f7af..49e34cc90a 100644
--- a/libavcodec/utvideodec.c
+++ b/libavcodec/utvideodec.c
@@ -31,6 +31,7 @@
 #define UNCHECKED_BITSTREAM_READER 1
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "avcodec.h"
 #include "bswapdsp.h"
diff --git a/libavcodec/utvideoenc.c b/libavcodec/utvideoenc.c
index 36dcb8283a..59e198458b 100644
--- a/libavcodec/utvideoenc.c
+++ b/libavcodec/utvideoenc.c
@@ -26,6 +26,7 @@
 
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "avcodec.h"
@@ -35,7 +36,6 @@
 #include "bytestream.h"
 #include "lossless_videoencdsp.h"
 #include "put_bits.h"
-#include "mathops.h"
 #include "utvideo.h"
 #include "huffman.h"
 
diff --git a/libavcodec/v210dec.c b/libavcodec/v210dec.c
index d80fb4e7c6..5cead150d3 100644
--- a/libavcodec/v210dec.c
+++ b/libavcodec/v210dec.c
@@ -29,6 +29,7 @@
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "thread.h"
 
 typedef struct ThreadData {
diff --git a/libavcodec/v4l2_context.c b/libavcodec/v4l2_context.c
index f20f713e1d..be1df3785b 100644
--- a/libavcodec/v4l2_context.c
+++ b/libavcodec/v4l2_context.c
@@ -27,6 +27,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <poll.h>
+#include "libavutil/mem.h"
 #include "libavcodec/avcodec.h"
 #include "decode.h"
 #include "v4l2_buffers.h"
diff --git a/libavcodec/v4l2_m2m.c b/libavcodec/v4l2_m2m.c
index bac3eb0588..ac086a7913 100644
--- a/libavcodec/v4l2_m2m.c
+++ b/libavcodec/v4l2_m2m.c
@@ -28,6 +28,7 @@
 #include <dirent.h>
 #include <fcntl.h>
 #include "libavcodec/avcodec.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/pixfmt.h"
diff --git a/libavcodec/vaapi_decode.c b/libavcodec/vaapi_decode.c
index cca94b5336..5665639dd7 100644
--- a/libavcodec/vaapi_decode.c
+++ b/libavcodec/vaapi_decode.c
@@ -20,6 +20,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
index 940f0678a5..ca6e6911ff 100644
--- a/libavcodec/vaapi_encode.c
+++ b/libavcodec/vaapi_encode.c
@@ -25,6 +25,7 @@
 #include "libavutil/common.h"
 #include "libavutil/internal.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 
 #include "vaapi_encode.h"
diff --git a/libavcodec/vaapi_encode_h264.c b/libavcodec/vaapi_encode_h264.c
index 37df9103ae..bf51df0f51 100644
--- a/libavcodec/vaapi_encode_h264.c
+++ b/libavcodec/vaapi_encode_h264.c
@@ -23,8 +23,8 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
-#include "libavutil/internal.h"
 #include "libavutil/opt.h"
 
 #include "atsc_a53.h"
@@ -34,7 +34,6 @@
 #include "codec_internal.h"
 #include "h264.h"
 #include "h264_levels.h"
-#include "h264_sei.h"
 #include "h2645data.h"
 #include "vaapi_encode.h"
 #include "version.h"
diff --git a/libavcodec/vaapi_encode_h265.c b/libavcodec/vaapi_encode_h265.c
index c4aabbf5ed..2777632fb6 100644
--- a/libavcodec/vaapi_encode_h265.c
+++ b/libavcodec/vaapi_encode_h265.c
@@ -23,6 +23,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/opt.h"
 #include "libavutil/mastering_display_metadata.h"
@@ -35,8 +36,6 @@
 #include "h2645data.h"
 #include "h265_profile_level.h"
 #include "hevc.h"
-#include "hevc_sei.h"
-#include "put_bits.h"
 #include "vaapi_encode.h"
 
 enum {
diff --git a/libavcodec/vaapi_vc1.c b/libavcodec/vaapi_vc1.c
index 09a5c852fc..5594118a69 100644
--- a/libavcodec/vaapi_vc1.c
+++ b/libavcodec/vaapi_vc1.c
@@ -22,6 +22,7 @@
 
 #include "config_components.h"
 
+#include "libavutil/mem.h"
 #include "hwaccel_internal.h"
 #include "mpegvideodec.h"
 #include "vaapi_decode.h"
diff --git a/libavcodec/vb.c b/libavcodec/vb.c
index 5744faa983..0a9528a8d2 100644
--- a/libavcodec/vb.c
+++ b/libavcodec/vb.c
@@ -24,6 +24,7 @@
  * VB Video decoder
  */
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/vble.c b/libavcodec/vble.c
index 7711bf1bb1..32157913c7 100644
--- a/libavcodec/vble.c
+++ b/libavcodec/vble.c
@@ -25,13 +25,13 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 
 #define BITSTREAM_READER_LE
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "get_bits.h"
 #include "lossless_videodsp.h"
-#include "mathops.h"
 #include "thread.h"
 
 typedef struct VBLEContext {
diff --git a/libavcodec/vbndec.c b/libavcodec/vbndec.c
index 68b2236124..4a38b02168 100644
--- a/libavcodec/vbndec.c
+++ b/libavcodec/vbndec.c
@@ -30,6 +30,7 @@
 #include "texturedsp.h"
 #include "vbn.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 
 typedef struct VBNContext {
     TextureDSPContext texdsp;
diff --git a/libavcodec/vc1dec.c b/libavcodec/vc1dec.c
index 3ca478e82a..2c413e0bf1 100644
--- a/libavcodec/vc1dec.c
+++ b/libavcodec/vc1dec.c
@@ -33,7 +33,6 @@
 #include "codec_internal.h"
 #include "decode.h"
 #include "get_bits.h"
-#include "h263dec.h"
 #include "hwaccel_internal.h"
 #include "hwconfig.h"
 #include "mpeg_er.h"
@@ -48,6 +47,7 @@
 #include "libavutil/attributes.h"
 #include "libavutil/avassert.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 
 
diff --git a/libavcodec/vc2enc.c b/libavcodec/vc2enc.c
index 6fbac29032..365d43146d 100644
--- a/libavcodec/vc2enc.c
+++ b/libavcodec/vc2enc.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/opt.h"
 #include "libavutil/version.h"
diff --git a/libavcodec/vdpau.c b/libavcodec/vdpau.c
index 538c57f9e8..6df3e88dac 100644
--- a/libavcodec/vdpau.c
+++ b/libavcodec/vdpau.c
@@ -23,14 +23,12 @@
 
 #include "config_components.h"
 
-#include <limits.h>
-
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "decode.h"
 #include "hwaccel_internal.h"
 #include "internal.h"
 #include "mpegvideodec.h"
-#include "vc1.h"
 #include "vdpau.h"
 #include "vdpau_internal.h"
 
diff --git a/libavcodec/videotoolbox.c b/libavcodec/videotoolbox.c
index f4da80640d..d6990a39c0 100644
--- a/libavcodec/videotoolbox.c
+++ b/libavcodec/videotoolbox.c
@@ -24,6 +24,7 @@
 #include "config_components.h"
 #include "videotoolbox.h"
 #include "libavutil/hwcontext_videotoolbox.h"
+#include "libavutil/mem.h"
 #include "vt_internal.h"
 #include "libavutil/avutil.h"
 #include "libavutil/hwcontext.h"
diff --git a/libavcodec/videotoolbox_vp9.c b/libavcodec/videotoolbox_vp9.c
index f5489854e3..da94ff4e54 100644
--- a/libavcodec/videotoolbox_vp9.c
+++ b/libavcodec/videotoolbox_vp9.c
@@ -23,6 +23,7 @@
 #include "config.h"
 #include "videotoolbox.h"
 #include "libavutil/hwcontext_videotoolbox.h"
+#include "libavutil/mem.h"
 #include "vt_internal.h"
 #include "libavutil/avassert.h"
 #include "libavutil/avutil.h"
diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 15c34d59c3..42065cbcb3 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -24,6 +24,7 @@
 #include <TargetConditionals.h>
 #include <Availability.h>
 #include "avcodec.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
diff --git a/libavcodec/vmdvideo.c b/libavcodec/vmdvideo.c
index 2da8af31f5..b168a931ce 100644
--- a/libavcodec/vmdvideo.c
+++ b/libavcodec/vmdvideo.c
@@ -37,6 +37,7 @@
 
 #include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
 #include "codec_internal.h"
diff --git a/libavcodec/vmixdec.c b/libavcodec/vmixdec.c
index d6b6e3557f..06584ebef5 100644
--- a/libavcodec/vmixdec.c
+++ b/libavcodec/vmixdec.c
@@ -24,11 +24,11 @@
 #include <string.h>
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #include "avcodec.h"
 #include "codec_internal.h"
-#include "decode.h"
 #define CACHED_BITSTREAM_READER !ARCH_X86_32
 #include "golomb.h"
 #include "get_bits.h"
diff --git a/libavcodec/vmnc.c b/libavcodec/vmnc.c
index 17e3a2f3c1..6188aa7158 100644
--- a/libavcodec/vmnc.c
+++ b/libavcodec/vmnc.c
@@ -26,6 +26,7 @@
  */
 
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
diff --git a/libavcodec/vorbis_parser.c b/libavcodec/vorbis_parser.c
index d2c9e647ce..c6969f139f 100644
--- a/libavcodec/vorbis_parser.c
+++ b/libavcodec/vorbis_parser.c
@@ -28,9 +28,9 @@
 #include "config_components.h"
 
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 
 #include "get_bits.h"
-#include "parser.h"
 #include "xiph.h"
 #include "vorbis_parser_internal.h"
 
diff --git a/libavcodec/vorbisdec.c b/libavcodec/vorbisdec.c
index 8462739c14..700c6c8918 100644
--- a/libavcodec/vorbisdec.c
+++ b/libavcodec/vorbisdec.c
@@ -31,6 +31,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/float_dsp.h"
+#include "libavutil/mem.h"
 #include "libavutil/tx.h"
 
 #define BITSTREAM_READER_LE
diff --git a/libavcodec/vorbisenc.c b/libavcodec/vorbisenc.c
index 378a3b38da..62684a7d8c 100644
--- a/libavcodec/vorbisenc.c
+++ b/libavcodec/vorbisenc.c
@@ -26,6 +26,7 @@
 
 #include <float.h>
 #include "libavutil/float_dsp.h"
+#include "libavutil/mem.h"
 #include "libavutil/tx.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c
index 89946135dc..201bab0e32 100644
--- a/libavcodec/vp3.c
+++ b/libavcodec/vp3.c
@@ -37,6 +37,7 @@
 
 #include "libavutil/emms.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/thread.h"
 
diff --git a/libavcodec/vp56.c b/libavcodec/vp56.c
index f2ee3fafd4..1da47ca43f 100644
--- a/libavcodec/vp56.c
+++ b/libavcodec/vp56.c
@@ -23,6 +23,7 @@
  * VP5 and VP6 compatible video decoder (common features)
  */
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "decode.h"
diff --git a/libavcodec/vp8.c b/libavcodec/vp8.c
index dd6c1b361b..539b5c5395 100644
--- a/libavcodec/vp8.c
+++ b/libavcodec/vp8.c
@@ -26,6 +26,7 @@
 
 #include "config_components.h"
 
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/vp9.c b/libavcodec/vp9.c
index 855936cdc1..6bcda8bfff 100644
--- a/libavcodec/vp9.c
+++ b/libavcodec/vp9.c
@@ -42,6 +42,7 @@
 #include "vp9dec.h"
 #include "vpx_rac.h"
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/video_enc_params.h"
 
diff --git a/libavcodec/vqavideo.c b/libavcodec/vqavideo.c
index 2977cf9a52..4fd1861d25 100644
--- a/libavcodec/vqavideo.c
+++ b/libavcodec/vqavideo.c
@@ -73,6 +73,7 @@
 #include <string.h>
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "codec_internal.h"
diff --git a/libavcodec/vqcdec.c b/libavcodec/vqcdec.c
index dc9248d99f..5c6cab3c1a 100644
--- a/libavcodec/vqcdec.c
+++ b/libavcodec/vqcdec.c
@@ -23,6 +23,7 @@
 #include "get_bits.h"
 #include "codec_internal.h"
 #include "decode.h"
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 
 #define VECTOR_VLC_BITS 6
diff --git a/libavcodec/vulkan_decode.c b/libavcodec/vulkan_decode.c
index 91929d165f..5168f4d512 100644
--- a/libavcodec/vulkan_decode.c
+++ b/libavcodec/vulkan_decode.c
@@ -21,6 +21,7 @@
 #include "vulkan_decode.h"
 #include "config_components.h"
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/vulkan_loader.h"
 
 #if CONFIG_H264_VULKAN_HWACCEL
diff --git a/libavcodec/vulkan_hevc.c b/libavcodec/vulkan_hevc.c
index 239bff75e5..2705a965b9 100644
--- a/libavcodec/vulkan_hevc.c
+++ b/libavcodec/vulkan_hevc.c
@@ -16,6 +16,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "hevcdec.h"
 #include "hevc_data.h"
 #include "hevc_ps.h"
diff --git a/libavcodec/vulkan_video.c b/libavcodec/vulkan_video.c
index 4be13ecc55..412ae9b61e 100644
--- a/libavcodec/vulkan_video.c
+++ b/libavcodec/vulkan_video.c
@@ -16,6 +16,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "vulkan_video.h"
 
 #define ASPECT_2PLANE (VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT)
diff --git a/libavcodec/vvc/vvc_ps.c b/libavcodec/vvc/vvc_ps.c
index 7972803da6..270e1c1535 100644
--- a/libavcodec/vvc/vvc_ps.c
+++ b/libavcodec/vvc/vvc_ps.c
@@ -20,8 +20,10 @@
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
+
 #include "libavcodec/cbs_h266.h"
-#include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
+#include "libavutil/pixdesc.h"
 #include "libavcodec/refstruct.h"
 #include "vvc_data.h"
 #include "vvc_ps.h"
diff --git a/libavcodec/vvc/vvc_refs.c b/libavcodec/vvc/vvc_refs.c
index afcfc09da7..3128122fff 100644
--- a/libavcodec/vvc/vvc_refs.c
+++ b/libavcodec/vvc/vvc_refs.c
@@ -22,6 +22,7 @@
 
 #include <stdatomic.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 #include "libavcodec/refstruct.h"
 #include "libavcodec/thread.h"
diff --git a/libavcodec/vvc/vvc_thread.c b/libavcodec/vvc/vvc_thread.c
index 31c931f050..59a8b203ce 100644
--- a/libavcodec/vvc/vvc_thread.c
+++ b/libavcodec/vvc/vvc_thread.c
@@ -23,6 +23,7 @@
 #include <stdatomic.h>
 
 #include "libavutil/executor.h"
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 
 #include "vvc_thread.h"
diff --git a/libavcodec/vvc/vvcdec.c b/libavcodec/vvc/vvcdec.c
index d5704aca25..8222f9d141 100644
--- a/libavcodec/vvc/vvcdec.c
+++ b/libavcodec/vvc/vvcdec.c
@@ -25,6 +25,7 @@
 #include "libavcodec/profiles.h"
 #include "libavcodec/refstruct.h"
 #include "libavutil/cpu.h"
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 
 #include "vvcdec.h"
diff --git a/libavcodec/vvc_parser.c b/libavcodec/vvc_parser.c
index 245cb214c9..a6a5be27ae 100644
--- a/libavcodec/vvc_parser.c
+++ b/libavcodec/vvc_parser.c
@@ -20,6 +20,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "cbs.h"
 #include "cbs_h266.h"
 #include "parser.h"
diff --git a/libavcodec/wavarc.c b/libavcodec/wavarc.c
index 09ed4d473a..d1581f6025 100644
--- a/libavcodec/wavarc.c
+++ b/libavcodec/wavarc.c
@@ -19,13 +19,12 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
 #include "get_bits.h"
-#include "bytestream.h"
 #include "mathops.h"
 #include "unary.h"
 
diff --git a/libavcodec/wavpack.c b/libavcodec/wavpack.c
index 09b8731465..7e60a1456a 100644
--- a/libavcodec/wavpack.c
+++ b/libavcodec/wavpack.c
@@ -20,8 +20,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "libavutil/buffer.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 
 #define BITSTREAM_READER_LE
 #include "avcodec.h"
diff --git a/libavcodec/wavpackenc.c b/libavcodec/wavpackenc.c
index 33a5dfcc89..ba0371592d 100644
--- a/libavcodec/wavpackenc.c
+++ b/libavcodec/wavpackenc.c
@@ -22,6 +22,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "codec_internal.h"
diff --git a/libavcodec/webp.c b/libavcodec/webp.c
index 9308ea2b69..dbcc5e73eb 100644
--- a/libavcodec/webp.c
+++ b/libavcodec/webp.c
@@ -41,6 +41,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 
 #define BITSTREAM_READER_LE
 #include "avcodec.h"
diff --git a/libavcodec/wma.c b/libavcodec/wma.c
index 5eacf230fa..da9c914b57 100644
--- a/libavcodec/wma.c
+++ b/libavcodec/wma.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/attributes.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
 #include "sinewin.h"
diff --git a/libavcodec/wmaenc.c b/libavcodec/wmaenc.c
index 80ff696be6..6949f08fb6 100644
--- a/libavcodec/wmaenc.c
+++ b/libavcodec/wmaenc.c
@@ -23,6 +23,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/ffmath.h"
+#include "libavutil/mem.h"
 
 #include "avcodec.h"
 #include "codec_internal.h"
diff --git a/libavcodec/wmalosslessdec.c b/libavcodec/wmalosslessdec.c
index 5d1c7ac66b..9559fe1faf 100644
--- a/libavcodec/wmalosslessdec.c
+++ b/libavcodec/wmalosslessdec.c
@@ -26,6 +26,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #include "avcodec.h"
diff --git a/libavcodec/wmaprodec.c b/libavcodec/wmaprodec.c
index 65b269adda..2afdcb2b52 100644
--- a/libavcodec/wmaprodec.c
+++ b/libavcodec/wmaprodec.c
@@ -89,6 +89,7 @@
 #include <inttypes.h>
 
 #include "libavutil/audio_fifo.h"
+#include "libavutil/mem.h"
 #include "libavutil/tx.h"
 #include "libavutil/ffmath.h"
 #include "libavutil/float_dsp.h"
diff --git a/libavcodec/wmavoice.c b/libavcodec/wmavoice.c
index 4e93aadab2..258c71330c 100644
--- a/libavcodec/wmavoice.c
+++ b/libavcodec/wmavoice.c
@@ -29,6 +29,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/float_dsp.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/thread.h"
 #include "libavutil/tx.h"
diff --git a/libavcodec/wmv2enc.c b/libavcodec/wmv2enc.c
index 8eb56444a3..13ec2bf6ff 100644
--- a/libavcodec/wmv2enc.c
+++ b/libavcodec/wmv2enc.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "h263.h"
diff --git a/libavcodec/wrapped_avframe.c b/libavcodec/wrapped_avframe.c
index 0278ea42ba..a513bb3d39 100644
--- a/libavcodec/wrapped_avframe.c
+++ b/libavcodec/wrapped_avframe.c
@@ -28,10 +28,9 @@
 #include "codec_internal.h"
 #include "decode.h"
 
-#include "libavutil/internal.h"
 #include "libavutil/frame.h"
 #include "libavutil/buffer.h"
-#include "libavutil/pixdesc.h"
+#include "libavutil/mem.h"
 
 static void wrapped_avframe_release_buffer(void *unused, uint8_t *data)
 {
diff --git a/libavcodec/xpmdec.c b/libavcodec/xpmdec.c
index b915abe0bf..6db9f5248e 100644
--- a/libavcodec/xpmdec.c
+++ b/libavcodec/xpmdec.c
@@ -22,6 +22,7 @@
  */
 
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
diff --git a/libavcodec/xsubdec.c b/libavcodec/xsubdec.c
index f86b7c58e7..db7873593c 100644
--- a/libavcodec/xsubdec.c
+++ b/libavcodec/xsubdec.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/mathematics.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "get_bits.h"
 #include "bytestream.h"
diff --git a/libavcodec/zmbv.c b/libavcodec/zmbv.c
index d309a8612b..2c09ccbd73 100644
--- a/libavcodec/zmbv.c
+++ b/libavcodec/zmbv.c
@@ -29,6 +29,7 @@
 #include "libavutil/common.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
diff --git a/libavcodec/zmbvenc.c b/libavcodec/zmbvenc.c
index d20330568d..04889b97d2 100644
--- a/libavcodec/zmbvenc.c
+++ b/libavcodec/zmbvenc.c
@@ -24,11 +24,10 @@
  * Zip Motion Blocks Video encoder
  */
 
-#include <stdio.h>
-#include <stdlib.h>
+#include <stddef.h>
 
-#include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "encode.h"
diff --git a/libavdevice/alsa.c b/libavdevice/alsa.c
index b055f6c1a6..d62ccc09c6 100644
--- a/libavdevice/alsa.c
+++ b/libavdevice/alsa.c
@@ -34,6 +34,7 @@
 #include "avdevice.h"
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 
 #include "alsa.h"
 
diff --git a/libavdevice/android_camera.c b/libavdevice/android_camera.c
index 3aa8597c63..5de44e6d86 100644
--- a/libavdevice/android_camera.c
+++ b/libavdevice/android_camera.c
@@ -39,6 +39,7 @@
 #include "libavutil/display.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/pixfmt.h"
diff --git a/libavdevice/audiotoolbox.m b/libavdevice/audiotoolbox.m
index 3bd707f236..7d95c34593 100644
--- a/libavdevice/audiotoolbox.m
+++ b/libavdevice/audiotoolbox.m
@@ -28,6 +28,7 @@
 #import <AudioToolbox/AudioToolbox.h>
 #include <pthread.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavformat/internal.h"
 #include "libavformat/mux.h"
diff --git a/libavdevice/avdevice.c b/libavdevice/avdevice.c
index cd7b03ef48..5712fa47cf 100644
--- a/libavdevice/avdevice.c
+++ b/libavdevice/avdevice.c
@@ -17,6 +17,7 @@
  */
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "avdevice.h"
 #include "internal.h"
 #include "libavformat/demux.h"
diff --git a/libavdevice/avfoundation.m b/libavdevice/avfoundation.m
index d9b17ccdae..e558ad7d90 100644
--- a/libavdevice/avfoundation.m
+++ b/libavdevice/avfoundation.m
@@ -29,6 +29,7 @@
 #include <pthread.h>
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/opt.h"
 #include "libavutil/avstring.h"
diff --git a/libavdevice/bktr.c b/libavdevice/bktr.c
index ac5b534868..eed75a9a92 100644
--- a/libavdevice/bktr.c
+++ b/libavdevice/bktr.c
@@ -29,6 +29,7 @@
 #include "libavutil/file_open.h"
 #include "libavutil/internal.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/time.h"
diff --git a/libavdevice/caca.c b/libavdevice/caca.c
index c3b2329758..42cceccf38 100644
--- a/libavdevice/caca.c
+++ b/libavdevice/caca.c
@@ -19,6 +19,7 @@
  */
 
 #include <caca.h>
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavformat/mux.h"
diff --git a/libavdevice/decklink_common.h b/libavdevice/decklink_common.h
index 34ab1b9670..c54a635876 100644
--- a/libavdevice/decklink_common.h
+++ b/libavdevice/decklink_common.h
@@ -30,6 +30,7 @@
 #endif
 
 extern "C" {
+#include "libavutil/mem.h"
 #include "libavcodec/packet_internal.h"
 #include "libavfilter/ccfifo.h"
 }
diff --git a/libavdevice/dshow_enummediatypes.c b/libavdevice/dshow_enummediatypes.c
index 4cb7057e17..64a236cc12 100644
--- a/libavdevice/dshow_enummediatypes.c
+++ b/libavdevice/dshow_enummediatypes.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "dshow_capture.h"
 
 DECLARE_QUERYINTERFACE(enummediatypes, DShowEnumMediaTypes,
diff --git a/libavdevice/dshow_pin.c b/libavdevice/dshow_pin.c
index f846b68918..baa001b5dd 100644
--- a/libavdevice/dshow_pin.c
+++ b/libavdevice/dshow_pin.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "dshow_capture.h"
 
 #include <stddef.h>
diff --git a/libavdevice/fbdev_common.c b/libavdevice/fbdev_common.c
index 2c70abb1f0..d8b99614d3 100644
--- a/libavdevice/fbdev_common.c
+++ b/libavdevice/fbdev_common.c
@@ -27,6 +27,7 @@
 #include "fbdev_common.h"
 #include "libavutil/common.h"
 #include "libavutil/file_open.h"
+#include "libavutil/mem.h"
 #include "avdevice.h"
 
 struct rgb_pixfmt_map_entry {
diff --git a/libavdevice/gdigrab.c b/libavdevice/gdigrab.c
index 58e876ea9e..c91661c556 100644
--- a/libavdevice/gdigrab.c
+++ b/libavdevice/gdigrab.c
@@ -31,6 +31,7 @@
 #include "config.h"
 #include "libavformat/demux.h"
 #include "libavformat/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/time.h"
 #include "libavutil/wchar_filename.h"
diff --git a/libavdevice/iec61883.c b/libavdevice/iec61883.c
index 689bcc81d5..670e29aa30 100644
--- a/libavdevice/iec61883.c
+++ b/libavdevice/iec61883.c
@@ -33,6 +33,7 @@
 #include "libavformat/demux.h"
 #include "libavformat/dv.h"
 #include "libavformat/mpegts.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avdevice.h"
 
diff --git a/libavdevice/jack.c b/libavdevice/jack.c
index 40f2af5ccc..d52bb06822 100644
--- a/libavdevice/jack.c
+++ b/libavdevice/jack.c
@@ -27,6 +27,7 @@
 #include "libavutil/internal.h"
 #include "libavutil/log.h"
 #include "libavutil/fifo.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/time.h"
 #include "libavformat/avformat.h"
diff --git a/libavdevice/kmsgrab.c b/libavdevice/kmsgrab.c
index bf6341e9fd..3d15754f3b 100644
--- a/libavdevice/kmsgrab.c
+++ b/libavdevice/kmsgrab.c
@@ -36,6 +36,7 @@
 #include "libavutil/hwcontext_drm.h"
 #include "libavutil/internal.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixfmt.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavdevice/opengl_enc.c b/libavdevice/opengl_enc.c
index 69de6fad03..6f7a30ff9e 100644
--- a/libavdevice/opengl_enc.c
+++ b/libavdevice/opengl_enc.c
@@ -50,6 +50,7 @@
 
 #include "libavutil/common.h"
 #include "libavutil/frame.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/log.h"
 #include "libavutil/opt.h"
diff --git a/libavdevice/sdl2.c b/libavdevice/sdl2.c
index ec3c3d19b5..3824b4437b 100644
--- a/libavdevice/sdl2.c
+++ b/libavdevice/sdl2.c
@@ -28,6 +28,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavdevice/sndio.c b/libavdevice/sndio.c
index 46f287588d..2297ff08ba 100644
--- a/libavdevice/sndio.c
+++ b/libavdevice/sndio.c
@@ -22,6 +22,7 @@
 #include <stdint.h>
 #include <sndio.h>
 
+#include "libavutil/mem.h"
 #include "avdevice.h"
 
 #include "libavdevice/sndio.h"
diff --git a/libavdevice/utils.c b/libavdevice/utils.c
index c72a839dfa..74574715ef 100644
--- a/libavdevice/utils.c
+++ b/libavdevice/utils.c
@@ -17,6 +17,7 @@
  */
 
 #include "internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavformat/avformat.h"
 #include "libavformat/demux.h"
diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c
index 51291246b3..3706582bc6 100644
--- a/libavdevice/v4l2.c
+++ b/libavdevice/v4l2.c
@@ -35,6 +35,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/time.h"
diff --git a/libavdevice/vfwcap.c b/libavdevice/vfwcap.c
index dd077dd08c..1fda90efa1 100644
--- a/libavdevice/vfwcap.c
+++ b/libavdevice/vfwcap.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/internal.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 
diff --git a/libavdevice/xcbgrab.c b/libavdevice/xcbgrab.c
index b2ff1f9828..2e6b118d8c 100644
--- a/libavdevice/xcbgrab.c
+++ b/libavdevice/xcbgrab.c
@@ -40,6 +40,7 @@
 
 #include "libavutil/internal.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/time.h"
diff --git a/libavdevice/xv.c b/libavdevice/xv.c
index cba933ef17..d1bc4e8d69 100644
--- a/libavdevice/xv.c
+++ b/libavdevice/xv.c
@@ -33,6 +33,7 @@
 #include <sys/shm.h>
 
 #include "libavutil/frame.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/imgutils.h"
diff --git a/libavfilter/aeval.c b/libavfilter/aeval.c
index b26d75f129..dfd5519bd6 100644
--- a/libavfilter/aeval.c
+++ b/libavfilter/aeval.c
@@ -28,6 +28,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/eval.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "avfilter.h"
diff --git a/libavfilter/af_aap.c b/libavfilter/af_aap.c
index 85d905c1ab..e4cd6f8281 100644
--- a/libavfilter/af_aap.c
+++ b/libavfilter/af_aap.c
@@ -21,6 +21,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
 #include "libavutil/float_dsp.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "audio.h"
diff --git a/libavfilter/af_acrossover.c b/libavfilter/af_acrossover.c
index 0e7781c77a..f5c8e889fb 100644
--- a/libavfilter/af_acrossover.c
+++ b/libavfilter/af_acrossover.c
@@ -29,6 +29,7 @@
 #include "libavutil/eval.h"
 #include "libavutil/float_dsp.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "audio.h"
diff --git a/libavfilter/af_acrusher.c b/libavfilter/af_acrusher.c
index 5fc942224f..48d7029b05 100644
--- a/libavfilter/af_acrusher.c
+++ b/libavfilter/af_acrusher.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "internal.h"
diff --git a/libavfilter/af_adeclick.c b/libavfilter/af_adeclick.c
index 3789225739..a5ae1d872b 100644
--- a/libavfilter/af_adeclick.c
+++ b/libavfilter/af_adeclick.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/audio_fifo.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/tx.h"
 #include "avfilter.h"
diff --git a/libavfilter/af_adecorrelate.c b/libavfilter/af_adecorrelate.c
index 87355e72e0..7216c3fc83 100644
--- a/libavfilter/af_adecorrelate.c
+++ b/libavfilter/af_adecorrelate.c
@@ -22,6 +22,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/ffmath.h"
 #include "libavutil/lfg.h"
+#include "libavutil/mem.h"
 #include "libavutil/random_seed.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
diff --git a/libavfilter/af_adelay.c b/libavfilter/af_adelay.c
index 87a86170a8..116fcc3a38 100644
--- a/libavfilter/af_adelay.c
+++ b/libavfilter/af_adelay.c
@@ -20,6 +20,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/eval.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/samplefmt.h"
 #include "avfilter.h"
diff --git a/libavfilter/af_adrc.c b/libavfilter/af_adrc.c
index 34e5433a6b..7a7d5e0370 100644
--- a/libavfilter/af_adrc.c
+++ b/libavfilter/af_adrc.c
@@ -22,6 +22,7 @@
 
 #include "libavutil/eval.h"
 #include "libavutil/ffmath.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/tx.h"
 #include "audio.h"
diff --git a/libavfilter/af_adynamicequalizer.c b/libavfilter/af_adynamicequalizer.c
index aa1dc5f45c..59fdaf99b7 100644
--- a/libavfilter/af_adynamicequalizer.c
+++ b/libavfilter/af_adynamicequalizer.c
@@ -19,6 +19,7 @@
 #include <float.h>
 
 #include "libavutil/ffmath.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "audio.h"
diff --git a/libavfilter/af_aecho.c b/libavfilter/af_aecho.c
index 0b57c0e587..68fa5aaf7b 100644
--- a/libavfilter/af_aecho.c
+++ b/libavfilter/af_aecho.c
@@ -20,6 +20,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/samplefmt.h"
 #include "avfilter.h"
diff --git a/libavfilter/af_aexciter.c b/libavfilter/af_aexciter.c
index 400d6707b5..1f1639d24c 100644
--- a/libavfilter/af_aexciter.c
+++ b/libavfilter/af_aexciter.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "internal.h"
diff --git a/libavfilter/af_afftdn.c b/libavfilter/af_afftdn.c
index b3ee6a4222..96c2246074 100644
--- a/libavfilter/af_afftdn.c
+++ b/libavfilter/af_afftdn.c
@@ -22,6 +22,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/tx.h"
 #include "avfilter.h"
diff --git a/libavfilter/af_afftfilt.c b/libavfilter/af_afftfilt.c
index 2b2a70b0b6..aa3e868caf 100644
--- a/libavfilter/af_afftfilt.c
+++ b/libavfilter/af_afftfilt.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavfilter/internal.h"
 #include "libavutil/common.h"
 #include "libavutil/cpu.h"
diff --git a/libavfilter/af_afir.c b/libavfilter/af_afir.c
index 6e54a7239d..8997fbd7c9 100644
--- a/libavfilter/af_afir.c
+++ b/libavfilter/af_afir.c
@@ -26,16 +26,14 @@
 #include <float.h>
 
 #include "libavutil/cpu.h"
+#include "libavutil/mem.h"
 #include "libavutil/tx.h"
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
-#include "libavutil/common.h"
 #include "libavutil/float_dsp.h"
 #include "libavutil/frame.h"
-#include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
 #include "libavutil/opt.h"
-#include "libavutil/rational.h"
 
 #include "audio.h"
 #include "avfilter.h"
@@ -44,7 +42,6 @@
 #include "internal.h"
 #include "af_afir.h"
 #include "af_afirdsp.h"
-#include "video.h"
 
 #define DEPTH 32
 #include "afir_template.c"
diff --git a/libavfilter/af_afwtdn.c b/libavfilter/af_afwtdn.c
index 8d69259e75..fb172f26cc 100644
--- a/libavfilter/af_afwtdn.c
+++ b/libavfilter/af_afwtdn.c
@@ -21,6 +21,7 @@
 #include <float.h>
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "audio.h"
diff --git a/libavfilter/af_aiir.c b/libavfilter/af_aiir.c
index ff35f6b926..b5e5b41db6 100644
--- a/libavfilter/af_aiir.c
+++ b/libavfilter/af_aiir.c
@@ -22,6 +22,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/xga_font_data.h"
 #include "audio.h"
diff --git a/libavfilter/af_alimiter.c b/libavfilter/af_alimiter.c
index 9a86704764..6ccce2d7de 100644
--- a/libavfilter/af_alimiter.c
+++ b/libavfilter/af_alimiter.c
@@ -27,6 +27,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
 #include "libavutil/fifo.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "audio.h"
diff --git a/libavfilter/af_amerge.c b/libavfilter/af_amerge.c
index 5daf639e74..493c852138 100644
--- a/libavfilter/af_amerge.c
+++ b/libavfilter/af_amerge.c
@@ -26,6 +26,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/bprint.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "filters.h"
diff --git a/libavfilter/af_amix.c b/libavfilter/af_amix.c
index f55c5724d6..ade4ef76a8 100644
--- a/libavfilter/af_amix.c
+++ b/libavfilter/af_amix.c
@@ -37,6 +37,7 @@
 #include "libavutil/eval.h"
 #include "libavutil/float_dsp.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/samplefmt.h"
 
diff --git a/libavfilter/af_amultiply.c b/libavfilter/af_amultiply.c
index 4ae355c794..b24254e9dc 100644
--- a/libavfilter/af_amultiply.c
+++ b/libavfilter/af_amultiply.c
@@ -21,6 +21,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
 #include "libavutil/float_dsp.h"
+#include "libavutil/mem.h"
 
 #include "audio.h"
 #include "avfilter.h"
diff --git a/libavfilter/af_anequalizer.c b/libavfilter/af_anequalizer.c
index 37ebcb5db1..b85190db34 100644
--- a/libavfilter/af_anequalizer.c
+++ b/libavfilter/af_anequalizer.c
@@ -22,6 +22,7 @@
 #include "libavutil/intreadwrite.h"
 #include "libavutil/avstring.h"
 #include "libavutil/ffmath.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "avfilter.h"
diff --git a/libavfilter/af_anlms.c b/libavfilter/af_anlms.c
index 9b0d11cd6d..e1c85da053 100644
--- a/libavfilter/af_anlms.c
+++ b/libavfilter/af_anlms.c
@@ -21,6 +21,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
 #include "libavutil/float_dsp.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "audio.h"
diff --git a/libavfilter/af_aphaser.c b/libavfilter/af_aphaser.c
index 4690276a65..f3334aa2c4 100644
--- a/libavfilter/af_aphaser.c
+++ b/libavfilter/af_aphaser.c
@@ -24,6 +24,7 @@
  */
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "audio.h"
 #include "avfilter.h"
diff --git a/libavfilter/af_apsyclip.c b/libavfilter/af_apsyclip.c
index 6a0c23d6eb..b542166b56 100644
--- a/libavfilter/af_apsyclip.c
+++ b/libavfilter/af_apsyclip.c
@@ -19,6 +19,7 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/tx.h"
 #include "audio.h"
diff --git a/libavfilter/af_arls.c b/libavfilter/af_arls.c
index bbe8e3cb21..aab0990409 100644
--- a/libavfilter/af_arls.c
+++ b/libavfilter/af_arls.c
@@ -20,6 +20,7 @@
 
 #include "libavutil/common.h"
 #include "libavutil/float_dsp.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "audio.h"
diff --git a/libavfilter/af_arnndn.c b/libavfilter/af_arnndn.c
index ee005eb34c..b29af87df9 100644
--- a/libavfilter/af_arnndn.c
+++ b/libavfilter/af_arnndn.c
@@ -34,6 +34,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/file_open.h"
 #include "libavutil/float_dsp.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 #include "libavutil/tx.h"
diff --git a/libavfilter/af_asdr.c b/libavfilter/af_asdr.c
index bb5c30faba..9f5ca80d78 100644
--- a/libavfilter/af_asdr.c
+++ b/libavfilter/af_asdr.c
@@ -22,6 +22,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 
 #include "avfilter.h"
 #include "filters.h"
diff --git a/libavfilter/af_ashowinfo.c b/libavfilter/af_ashowinfo.c
index b1b462d68f..de3c81f90b 100644
--- a/libavfilter/af_ashowinfo.c
+++ b/libavfilter/af_ashowinfo.c
@@ -24,14 +24,11 @@
  */
 
 #include <inttypes.h>
-#include <stddef.h>
 
 #include "libavutil/adler32.h"
 #include "libavutil/attributes.h"
 #include "libavutil/channel_layout.h"
-#include "libavutil/common.h"
 #include "libavutil/downmix_info.h"
-#include "libavutil/intreadwrite.h"
 #include "libavutil/mem.h"
 #include "libavutil/replaygain.h"
 #include "libavutil/timestamp.h"
diff --git a/libavfilter/af_aspectralstats.c b/libavfilter/af_aspectralstats.c
index 49c9d97e8d..592d7e5530 100644
--- a/libavfilter/af_aspectralstats.c
+++ b/libavfilter/af_aspectralstats.c
@@ -21,6 +21,7 @@
 #include <float.h>
 #include <math.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/tx.h"
 #include "audio.h"
diff --git a/libavfilter/af_asr.c b/libavfilter/af_asr.c
index 884e17afb7..9d3c88f2a0 100644
--- a/libavfilter/af_asr.c
+++ b/libavfilter/af_asr.c
@@ -22,6 +22,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "audio.h"
 #include "avfilter.h"
diff --git a/libavfilter/af_astats.c b/libavfilter/af_astats.c
index 9e8c963582..c7e7c11b7d 100644
--- a/libavfilter/af_astats.c
+++ b/libavfilter/af_astats.c
@@ -22,6 +22,7 @@
 #include <float.h>
 #include <math.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "audio.h"
 #include "avfilter.h"
diff --git a/libavfilter/af_asubboost.c b/libavfilter/af_asubboost.c
index 31db4b81a0..f559895418 100644
--- a/libavfilter/af_asubboost.c
+++ b/libavfilter/af_asubboost.c
@@ -18,6 +18,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/ffmath.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "audio.h"
diff --git a/libavfilter/af_atempo.c b/libavfilter/af_atempo.c
index 654b080e89..0c36eb4dd7 100644
--- a/libavfilter/af_atempo.c
+++ b/libavfilter/af_atempo.c
@@ -40,9 +40,8 @@
 
 #include <float.h>
 #include "libavutil/avassert.h"
-#include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
-#include "libavutil/eval.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/samplefmt.h"
 #include "libavutil/tx.h"
diff --git a/libavfilter/af_biquads.c b/libavfilter/af_biquads.c
index ddca9d8748..21d3acf850 100644
--- a/libavfilter/af_biquads.c
+++ b/libavfilter/af_biquads.c
@@ -67,6 +67,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/ffmath.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "audio.h"
 #include "avfilter.h"
diff --git a/libavfilter/af_channelmap.c b/libavfilter/af_channelmap.c
index 64efacb576..0429490d9f 100644
--- a/libavfilter/af_channelmap.c
+++ b/libavfilter/af_channelmap.c
@@ -29,6 +29,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/samplefmt.h"
 
diff --git a/libavfilter/af_channelsplit.c b/libavfilter/af_channelsplit.c
index f026c74494..d18d91dcb6 100644
--- a/libavfilter/af_channelsplit.c
+++ b/libavfilter/af_channelsplit.c
@@ -26,6 +26,7 @@
 #include "libavutil/attributes.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "audio.h"
diff --git a/libavfilter/af_chorus.c b/libavfilter/af_chorus.c
index 19826cfb15..72dc8915d7 100644
--- a/libavfilter/af_chorus.c
+++ b/libavfilter/af_chorus.c
@@ -30,6 +30,7 @@
  */
 
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "audio.h"
 #include "avfilter.h"
diff --git a/libavfilter/af_compand.c b/libavfilter/af_compand.c
index 3e799c3b15..880ee6241a 100644
--- a/libavfilter/af_compand.c
+++ b/libavfilter/af_compand.c
@@ -30,6 +30,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 #include "libavutil/ffmath.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/samplefmt.h"
 #include "audio.h"
diff --git a/libavfilter/af_crossfeed.c b/libavfilter/af_crossfeed.c
index ee6540a9fc..36f05cfd21 100644
--- a/libavfilter/af_crossfeed.c
+++ b/libavfilter/af_crossfeed.c
@@ -18,6 +18,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/ffmath.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "audio.h"
diff --git a/libavfilter/af_deesser.c b/libavfilter/af_deesser.c
index 16c10d21bc..917ef2f43f 100644
--- a/libavfilter/af_deesser.c
+++ b/libavfilter/af_deesser.c
@@ -21,6 +21,7 @@
  */
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "audio.h"
diff --git a/libavfilter/af_dialoguenhance.c b/libavfilter/af_dialoguenhance.c
index 03bfccb076..76c9877238 100644
--- a/libavfilter/af_dialoguenhance.c
+++ b/libavfilter/af_dialoguenhance.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/tx.h"
 #include "audio.h"
diff --git a/libavfilter/af_drmeter.c b/libavfilter/af_drmeter.c
index 5eea865575..0decc0fde6 100644
--- a/libavfilter/af_drmeter.c
+++ b/libavfilter/af_drmeter.c
@@ -21,6 +21,7 @@
 #include <float.h>
 
 #include "libavutil/ffmath.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "audio.h"
 #include "avfilter.h"
diff --git a/libavfilter/af_dynaudnorm.c b/libavfilter/af_dynaudnorm.c
index fb0581b6e4..846d62584b 100644
--- a/libavfilter/af_dynaudnorm.c
+++ b/libavfilter/af_dynaudnorm.c
@@ -29,6 +29,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/eval.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #define MIN_FILTER_SIZE 3
diff --git a/libavfilter/af_firequalizer.c b/libavfilter/af_firequalizer.c
index 657dfbc0ca..5108edca48 100644
--- a/libavfilter/af_firequalizer.c
+++ b/libavfilter/af_firequalizer.c
@@ -20,6 +20,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/file_open.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/eval.h"
 #include "libavutil/avassert.h"
diff --git a/libavfilter/af_flanger.c b/libavfilter/af_flanger.c
index 453704ea63..381cf96649 100644
--- a/libavfilter/af_flanger.c
+++ b/libavfilter/af_flanger.c
@@ -18,7 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/samplefmt.h"
 #include "avfilter.h"
diff --git a/libavfilter/af_haas.c b/libavfilter/af_haas.c
index dd2ea26ec3..753a3505e5 100644
--- a/libavfilter/af_haas.c
+++ b/libavfilter/af_haas.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "audio.h"
diff --git a/libavfilter/af_headphone.c b/libavfilter/af_headphone.c
index 9124f945a8..37ed9b84a1 100644
--- a/libavfilter/af_headphone.c
+++ b/libavfilter/af_headphone.c
@@ -24,6 +24,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/float_dsp.h"
 #include "libavutil/intmath.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/tx.h"
 
diff --git a/libavfilter/af_join.c b/libavfilter/af_join.c
index 8dab3f0931..4a4fc04d0d 100644
--- a/libavfilter/af_join.c
+++ b/libavfilter/af_join.c
@@ -28,6 +28,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "audio.h"
diff --git a/libavfilter/af_ladspa.c b/libavfilter/af_ladspa.c
index 7567c0577d..547e91979d 100644
--- a/libavfilter/af_ladspa.c
+++ b/libavfilter/af_ladspa.c
@@ -30,6 +30,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/fifo.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "audio.h"
 #include "avfilter.h"
diff --git a/libavfilter/af_loudnorm.c b/libavfilter/af_loudnorm.c
index 6cb9c06064..dd5edec1c8 100644
--- a/libavfilter/af_loudnorm.c
+++ b/libavfilter/af_loudnorm.c
@@ -20,6 +20,7 @@
 
 /* http://k.ylo.ph/2016/04/04/loudnorm.html */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "filters.h"
diff --git a/libavfilter/af_lv2.c b/libavfilter/af_lv2.c
index 7b3ddf9448..a5980d5e9c 100644
--- a/libavfilter/af_lv2.c
+++ b/libavfilter/af_lv2.c
@@ -30,6 +30,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "audio.h"
 #include "avfilter.h"
diff --git a/libavfilter/af_mcompand.c b/libavfilter/af_mcompand.c
index 1267cd9f34..89fe806a02 100644
--- a/libavfilter/af_mcompand.c
+++ b/libavfilter/af_mcompand.c
@@ -30,6 +30,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/ffmath.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/samplefmt.h"
 #include "audio.h"
diff --git a/libavfilter/af_pan.c b/libavfilter/af_pan.c
index 34073203d2..31c6be45c3 100644
--- a/libavfilter/af_pan.c
+++ b/libavfilter/af_pan.c
@@ -30,6 +30,7 @@
 #include <stdio.h>
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libswresample/swresample.h"
 #include "audio.h"
diff --git a/libavfilter/af_silencedetect.c b/libavfilter/af_silencedetect.c
index 845c65bfed..1d2bf03f72 100644
--- a/libavfilter/af_silencedetect.c
+++ b/libavfilter/af_silencedetect.c
@@ -25,6 +25,7 @@
 
 #include <float.h> /* DBL_MAX */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/timestamp.h"
 #include "audio.h"
diff --git a/libavfilter/af_silenceremove.c b/libavfilter/af_silenceremove.c
index d60c86ad93..eb23e78957 100644
--- a/libavfilter/af_silenceremove.c
+++ b/libavfilter/af_silenceremove.c
@@ -24,6 +24,7 @@
 #include <float.h> /* DBL_MAX */
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "audio.h"
 #include "filters.h"
diff --git a/libavfilter/af_sofalizer.c b/libavfilter/af_sofalizer.c
index 7349fa06f5..4bf54014a3 100644
--- a/libavfilter/af_sofalizer.c
+++ b/libavfilter/af_sofalizer.c
@@ -28,6 +28,7 @@
 #include <math.h>
 #include <mysofa.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/tx.h"
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
diff --git a/libavfilter/af_speechnorm.c b/libavfilter/af_speechnorm.c
index a8bed2cb8f..e6a8a95829 100644
--- a/libavfilter/af_speechnorm.c
+++ b/libavfilter/af_speechnorm.c
@@ -29,6 +29,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #define FF_BUFQUEUE_SIZE (1024)
diff --git a/libavfilter/af_stereotools.c b/libavfilter/af_stereotools.c
index 330d91cd2b..70d14ebe17 100644
--- a/libavfilter/af_stereotools.c
+++ b/libavfilter/af_stereotools.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "audio.h"
diff --git a/libavfilter/af_stereowiden.c b/libavfilter/af_stereowiden.c
index 1273c77116..96d77cd2ac 100644
--- a/libavfilter/af_stereowiden.c
+++ b/libavfilter/af_stereowiden.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "audio.h"
diff --git a/libavfilter/af_superequalizer.c b/libavfilter/af_superequalizer.c
index fbe707cb2f..c532048406 100644
--- a/libavfilter/af_superequalizer.c
+++ b/libavfilter/af_superequalizer.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/tx.h"
 
diff --git a/libavfilter/af_surround.c b/libavfilter/af_surround.c
index 5957860283..e37dddc361 100644
--- a/libavfilter/af_surround.c
+++ b/libavfilter/af_surround.c
@@ -20,6 +20,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/tx.h"
 #include "avfilter.h"
diff --git a/libavfilter/af_tremolo.c b/libavfilter/af_tremolo.c
index 024c402b79..30e35935b4 100644
--- a/libavfilter/af_tremolo.c
+++ b/libavfilter/af_tremolo.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "internal.h"
diff --git a/libavfilter/af_vibrato.c b/libavfilter/af_vibrato.c
index e54ae2ad4b..3d709d561e 100644
--- a/libavfilter/af_vibrato.c
+++ b/libavfilter/af_vibrato.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "internal.h"
diff --git a/libavfilter/af_volume.c b/libavfilter/af_volume.c
index 686e8c651c..5a3c9220f4 100644
--- a/libavfilter/af_volume.c
+++ b/libavfilter/af_volume.c
@@ -29,7 +29,7 @@
 #include "libavutil/eval.h"
 #include "libavutil/ffmath.h"
 #include "libavutil/float_dsp.h"
-#include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/replaygain.h"
 
diff --git a/libavfilter/asrc_afirsrc.c b/libavfilter/asrc_afirsrc.c
index 20cb84bc01..f8040b4404 100644
--- a/libavfilter/asrc_afirsrc.c
+++ b/libavfilter/asrc_afirsrc.c
@@ -22,6 +22,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/ffmath.h"
 #include "libavutil/eval.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/tx.h"
 #include "audio.h"
diff --git a/libavfilter/asrc_flite.c b/libavfilter/asrc_flite.c
index 6055db52b9..4327341098 100644
--- a/libavfilter/asrc_flite.c
+++ b/libavfilter/asrc_flite.c
@@ -28,6 +28,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/file.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/thread.h"
 #include "avfilter.h"
diff --git a/libavfilter/asrc_hilbert.c b/libavfilter/asrc_hilbert.c
index 98248e7e4e..0ca8e0c9d5 100644
--- a/libavfilter/asrc_hilbert.c
+++ b/libavfilter/asrc_hilbert.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "audio.h"
 #include "avfilter.h"
diff --git a/libavfilter/asrc_sinc.c b/libavfilter/asrc_sinc.c
index 6366fb802b..a16d8285af 100644
--- a/libavfilter/asrc_sinc.c
+++ b/libavfilter/asrc_sinc.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/tx.h"
 
diff --git a/libavfilter/asrc_sine.c b/libavfilter/asrc_sine.c
index c576b57fec..72a24cce65 100644
--- a/libavfilter/asrc_sine.c
+++ b/libavfilter/asrc_sine.c
@@ -23,6 +23,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/eval.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "audio.h"
 #include "avfilter.h"
diff --git a/libavfilter/avf_abitscope.c b/libavfilter/avf_abitscope.c
index 60dba21be1..13c704ff27 100644
--- a/libavfilter/avf_abitscope.c
+++ b/libavfilter/avf_abitscope.c
@@ -20,6 +20,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "avfilter.h"
diff --git a/libavfilter/avf_ahistogram.c b/libavfilter/avf_ahistogram.c
index f80f341e33..303b12e14c 100644
--- a/libavfilter/avf_ahistogram.c
+++ b/libavfilter/avf_ahistogram.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "avfilter.h"
diff --git a/libavfilter/avf_concat.c b/libavfilter/avf_concat.c
index 33edd7a394..5d11631798 100644
--- a/libavfilter/avf_concat.c
+++ b/libavfilter/avf_concat.c
@@ -25,6 +25,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "filters.h"
diff --git a/libavfilter/avf_showcqt.c b/libavfilter/avf_showcqt.c
index 676f77ecb2..249b6245ca 100644
--- a/libavfilter/avf_showcqt.c
+++ b/libavfilter/avf_showcqt.c
@@ -19,6 +19,7 @@
  */
 
 #include "config.h"
+#include "libavutil/mem.h"
 #include "libavutil/tx.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/opt.h"
diff --git a/libavfilter/avf_showcwt.c b/libavfilter/avf_showcwt.c
index 9436881f2a..bc47dadb49 100644
--- a/libavfilter/avf_showcwt.c
+++ b/libavfilter/avf_showcwt.c
@@ -21,6 +21,7 @@
 #include <float.h>
 #include <math.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/tx.h"
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
diff --git a/libavfilter/avf_showfreqs.c b/libavfilter/avf_showfreqs.c
index e3c7ce96f8..23083ad50a 100644
--- a/libavfilter/avf_showfreqs.c
+++ b/libavfilter/avf_showfreqs.c
@@ -21,6 +21,7 @@
 #include <float.h>
 #include <math.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/tx.h"
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
diff --git a/libavfilter/avf_showspatial.c b/libavfilter/avf_showspatial.c
index 29b41bee85..3f8df98167 100644
--- a/libavfilter/avf_showspatial.c
+++ b/libavfilter/avf_showspatial.c
@@ -21,6 +21,7 @@
 #include <float.h>
 #include <math.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/tx.h"
 #include "libavutil/audio_fifo.h"
 #include "libavutil/avassert.h"
diff --git a/libavfilter/avf_showspectrum.c b/libavfilter/avf_showspectrum.c
index 5d76da3bfc..3a4b489af3 100644
--- a/libavfilter/avf_showspectrum.c
+++ b/libavfilter/avf_showspectrum.c
@@ -31,6 +31,7 @@
 #include <float.h>
 #include <math.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/tx.h"
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
diff --git a/libavfilter/avf_showvolume.c b/libavfilter/avf_showvolume.c
index f359e4d97b..7b558df4ae 100644
--- a/libavfilter/avf_showvolume.c
+++ b/libavfilter/avf_showvolume.c
@@ -22,6 +22,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/eval.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/xga_font_data.h"
diff --git a/libavfilter/avf_showwaves.c b/libavfilter/avf_showwaves.c
index f090d93714..63e0b36e09 100644
--- a/libavfilter/avf_showwaves.c
+++ b/libavfilter/avf_showwaves.c
@@ -29,6 +29,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "avfilter.h"
diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c
index 831871de90..856862a393 100644
--- a/libavfilter/avfilter.c
+++ b/libavfilter/avfilter.c
@@ -29,6 +29,7 @@
 #include "libavutil/frame.h"
 #include "libavutil/hwcontext.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/rational.h"
diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c
index bb5399c55e..51ea6ec013 100644
--- a/libavfilter/avfiltergraph.c
+++ b/libavfilter/avfiltergraph.c
@@ -29,6 +29,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/hwcontext.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 
diff --git a/libavfilter/boxblur.c b/libavfilter/boxblur.c
index 2287396f2e..43c724daea 100644
--- a/libavfilter/boxblur.c
+++ b/libavfilter/boxblur.c
@@ -20,6 +20,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "boxblur.h"
 
 static const char *const var_names[] = {
diff --git a/libavfilter/buffersrc.c b/libavfilter/buffersrc.c
index ddcd403785..f78743ff47 100644
--- a/libavfilter/buffersrc.c
+++ b/libavfilter/buffersrc.c
@@ -31,6 +31,7 @@
 #include "libavutil/hwcontext.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/samplefmt.h"
 #include "libavutil/timestamp.h"
diff --git a/libavfilter/cuda/load_helper.c b/libavfilter/cuda/load_helper.c
index 280e596ecb..b049ec7130 100644
--- a/libavfilter/cuda/load_helper.c
+++ b/libavfilter/cuda/load_helper.c
@@ -21,6 +21,7 @@
 #include "libavutil/hwcontext.h"
 #include "libavutil/hwcontext_cuda_internal.h"
 #include "libavutil/cuda_check.h"
+#include "libavutil/mem.h"
 
 #if CONFIG_PTX_COMPRESSION
 #include <zlib.h>
diff --git a/libavfilter/dialoguenhance_template.c b/libavfilter/dialoguenhance_template.c
index 26650f7d8c..07f42f0958 100644
--- a/libavfilter/dialoguenhance_template.c
+++ b/libavfilter/dialoguenhance_template.c
@@ -16,6 +16,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/tx.h"
 #include "avfilter.h"
 #include "internal.h"
diff --git a/libavfilter/dnn/dnn_backend_common.c b/libavfilter/dnn/dnn_backend_common.c
index 632832ec36..e45eefd14d 100644
--- a/libavfilter/dnn/dnn_backend_common.c
+++ b/libavfilter/dnn/dnn_backend_common.c
@@ -21,6 +21,7 @@
  * DNN common functions different backends.
  */
 
+#include "libavutil/mem.h"
 #include "dnn_backend_common.h"
 
 #define DNN_ASYNC_SUCCESS (void *)0
diff --git a/libavfilter/dnn/dnn_backend_openvino.c b/libavfilter/dnn/dnn_backend_openvino.c
index 375643377f..bedf983f8f 100644
--- a/libavfilter/dnn/dnn_backend_openvino.c
+++ b/libavfilter/dnn/dnn_backend_openvino.c
@@ -27,6 +27,7 @@
 #include "libavformat/avio.h"
 #include "libavutil/avassert.h"
 #include "libavutil/cpu.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/avstring.h"
 #include "libavutil/detection_bbox.h"
diff --git a/libavfilter/dnn/dnn_backend_tf.c b/libavfilter/dnn/dnn_backend_tf.c
index 27c5178bb5..2ed17c3c87 100644
--- a/libavfilter/dnn/dnn_backend_tf.c
+++ b/libavfilter/dnn/dnn_backend_tf.c
@@ -27,6 +27,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 #include "libavutil/cpu.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavcodec/defs.h"
 #include "../internal.h"
diff --git a/libavfilter/dnn/dnn_io_proc.c b/libavfilter/dnn/dnn_io_proc.c
index e5d6edb301..f3a605f73c 100644
--- a/libavfilter/dnn/dnn_io_proc.c
+++ b/libavfilter/dnn/dnn_io_proc.c
@@ -20,6 +20,7 @@
 
 #include "dnn_io_proc.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libswscale/swscale.h"
 #include "libavutil/avassert.h"
 #include "libavutil/detection_bbox.h"
diff --git a/libavfilter/dnn_filter_common.c b/libavfilter/dnn_filter_common.c
index 7d194c9ade..5e76b9ba45 100644
--- a/libavfilter/dnn_filter_common.c
+++ b/libavfilter/dnn_filter_common.c
@@ -18,6 +18,7 @@
 
 #include "dnn_filter_common.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 
 #define MAX_SUPPORTED_OUTPUTS_NB 4
 
diff --git a/libavfilter/f_drawgraph.c b/libavfilter/f_drawgraph.c
index fd8641ff75..e1707254ac 100644
--- a/libavfilter/f_drawgraph.c
+++ b/libavfilter/f_drawgraph.c
@@ -25,6 +25,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/eval.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "formats.h"
diff --git a/libavfilter/f_ebur128.c b/libavfilter/f_ebur128.c
index 3a8dba5816..3d9093df1a 100644
--- a/libavfilter/f_ebur128.c
+++ b/libavfilter/f_ebur128.c
@@ -34,6 +34,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/dict.h"
 #include "libavutil/ffmath.h"
+#include "libavutil/mem.h"
 #include "libavutil/xga_font_data.h"
 #include "libavutil/opt.h"
 #include "libavutil/timestamp.h"
diff --git a/libavfilter/f_graphmonitor.c b/libavfilter/f_graphmonitor.c
index e0b20114e0..01c0eff81b 100644
--- a/libavfilter/f_graphmonitor.c
+++ b/libavfilter/f_graphmonitor.c
@@ -22,6 +22,7 @@
 
 #include "float.h"
 
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/eval.h"
 #include "libavutil/intreadwrite.h"
diff --git a/libavfilter/f_loop.c b/libavfilter/f_loop.c
index 0b08a2ead3..f58436e679 100644
--- a/libavfilter/f_loop.c
+++ b/libavfilter/f_loop.c
@@ -22,6 +22,7 @@
 
 #include "libavutil/audio_fifo.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "audio.h"
diff --git a/libavfilter/f_reverse.c b/libavfilter/f_reverse.c
index 744f4a3cc8..71ef30f9b5 100644
--- a/libavfilter/f_reverse.c
+++ b/libavfilter/f_reverse.c
@@ -20,6 +20,7 @@
 
 #include "config_components.h"
 
+#include "libavutil/mem.h"
 #include "avfilter.h"
 #include "internal.h"
 
diff --git a/libavfilter/f_segment.c b/libavfilter/f_segment.c
index 03a071a05b..6c9cbe4dda 100644
--- a/libavfilter/f_segment.c
+++ b/libavfilter/f_segment.c
@@ -25,6 +25,7 @@
 #include "libavutil/common.h"
 #include "libavutil/log.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/samplefmt.h"
diff --git a/libavfilter/f_sendcmd.c b/libavfilter/f_sendcmd.c
index 922ce78287..47bfaba9c9 100644
--- a/libavfilter/f_sendcmd.c
+++ b/libavfilter/f_sendcmd.c
@@ -29,6 +29,7 @@
 #include "libavutil/bprint.h"
 #include "libavutil/eval.h"
 #include "libavutil/file.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "avfilter.h"
diff --git a/libavfilter/f_streamselect.c b/libavfilter/f_streamselect.c
index 5fbb13a323..c17b019969 100644
--- a/libavfilter/f_streamselect.c
+++ b/libavfilter/f_streamselect.c
@@ -18,6 +18,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "audio.h"
diff --git a/libavfilter/f_zmq.c b/libavfilter/f_zmq.c
index 3829b55224..03c6c65c93 100644
--- a/libavfilter/f_zmq.c
+++ b/libavfilter/f_zmq.c
@@ -27,7 +27,7 @@
 
 #include <zmq.h>
 #include "libavutil/avstring.h"
-#include "libavutil/bprint.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "internal.h"
diff --git a/libavfilter/formats.c b/libavfilter/formats.c
index d3f4c7f09c..857f93113d 100644
--- a/libavfilter/formats.c
+++ b/libavfilter/formats.c
@@ -23,6 +23,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
 #include "libavutil/eval.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
 #include "internal.h"
diff --git a/libavfilter/framequeue.c b/libavfilter/framequeue.c
index ace0dad689..79255fe532 100644
--- a/libavfilter/framequeue.c
+++ b/libavfilter/framequeue.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "framequeue.h"
 
 static inline FFFrameBucket *bucket(FFFrameQueue *fq, size_t idx)
diff --git a/libavfilter/framesync.c b/libavfilter/framesync.c
index cf2f355c89..e4eb861726 100644
--- a/libavfilter/framesync.c
+++ b/libavfilter/framesync.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "filters.h"
diff --git a/libavfilter/graphdump.c b/libavfilter/graphdump.c
index df79903c52..20370f360f 100644
--- a/libavfilter/graphdump.c
+++ b/libavfilter/graphdump.c
@@ -23,6 +23,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/bprint.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
 #include "internal.h"
diff --git a/libavfilter/qrencode.c b/libavfilter/qrencode.c
index b084bf1d1f..0b1f7bb50a 100644
--- a/libavfilter/qrencode.c
+++ b/libavfilter/qrencode.c
@@ -33,6 +33,7 @@
 
 #include "libavutil/internal.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/lfg.h"
 #include "libavutil/random_seed.h"
diff --git a/libavfilter/qsvvpp.c b/libavfilter/qsvvpp.c
index 5cdba7d54a..8c92fec0c1 100644
--- a/libavfilter/qsvvpp.c
+++ b/libavfilter/qsvvpp.c
@@ -23,6 +23,7 @@
 
 #include "libavutil/common.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/time.h"
 #include "libavutil/pixdesc.h"
 
diff --git a/libavfilter/signature_lookup.c b/libavfilter/signature_lookup.c
index 9c69c02fbf..ab7320d47f 100644
--- a/libavfilter/signature_lookup.c
+++ b/libavfilter/signature_lookup.c
@@ -23,6 +23,7 @@
  * MPEG-7 video signature calculation and lookup filter
  */
 
+#include "libavutil/mem.h"
 #include "signature.h"
 
 #define HOUGH_MAX_OFFSET 90
diff --git a/libavfilter/src_movie.c b/libavfilter/src_movie.c
index e50ebc99dc..6b0e9c75d2 100644
--- a/libavfilter/src_movie.c
+++ b/libavfilter/src_movie.c
@@ -34,6 +34,7 @@
 #include "libavutil/attributes.h"
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
diff --git a/libavfilter/tests/integral.c b/libavfilter/tests/integral.c
index 6bdf0fd7fa..3d9487f6b8 100644
--- a/libavfilter/tests/integral.c
+++ b/libavfilter/tests/integral.c
@@ -16,6 +16,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavfilter/vf_nlmeans.c"
 
 static void display_integral(const uint32_t *ii, int w, int h, int lz_32)
diff --git a/libavfilter/textutils.c b/libavfilter/textutils.c
index ef658d04a2..45967794bc 100644
--- a/libavfilter/textutils.c
+++ b/libavfilter/textutils.c
@@ -29,6 +29,7 @@
 #include "libavutil/avutil.h"
 #include "libavutil/error.h"
 #include "libavutil/file.h"
+#include "libavutil/mem.h"
 #include "libavutil/time.h"
 
 static int ff_expand_text_function_internal(FFExpandTextContext *expand_text, AVBPrint *bp,
diff --git a/libavfilter/vaapi_vpp.c b/libavfilter/vaapi_vpp.c
index 59961bfa4a..ace1153a23 100644
--- a/libavfilter/vaapi_vpp.c
+++ b/libavfilter/vaapi_vpp.c
@@ -19,6 +19,7 @@
 #include <string.h>
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "formats.h"
 #include "internal.h"
diff --git a/libavfilter/vaf_spectrumsynth.c b/libavfilter/vaf_spectrumsynth.c
index 3a9aacd2b5..14d5016e54 100644
--- a/libavfilter/vaf_spectrumsynth.c
+++ b/libavfilter/vaf_spectrumsynth.c
@@ -24,6 +24,7 @@
  * @todo support float pixel format
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/tx.h"
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
diff --git a/libavfilter/vf_amplify.c b/libavfilter/vf_amplify.c
index 0ebca60ea2..7f2cf81150 100644
--- a/libavfilter/vf_amplify.c
+++ b/libavfilter/vf_amplify.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 
diff --git a/libavfilter/vf_avgblur.c b/libavfilter/vf_avgblur.c
index 8ff6111bcc..ced0a2ac28 100644
--- a/libavfilter/vf_avgblur.c
+++ b/libavfilter/vf_avgblur.c
@@ -22,6 +22,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_backgroundkey.c b/libavfilter/vf_backgroundkey.c
index 26fb08bf86..54f7621a10 100644
--- a/libavfilter/vf_backgroundkey.c
+++ b/libavfilter/vf_backgroundkey.c
@@ -16,6 +16,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_bilateral.c b/libavfilter/vf_bilateral.c
index fe16419026..3fe7e69b9f 100644
--- a/libavfilter/vf_bilateral.c
+++ b/libavfilter/vf_bilateral.c
@@ -22,6 +22,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_blackdetect.c b/libavfilter/vf_blackdetect.c
index 55033ba5ea..23ff8600ca 100644
--- a/libavfilter/vf_blackdetect.c
+++ b/libavfilter/vf_blackdetect.c
@@ -25,6 +25,7 @@
  */
 
 #include <float.h>
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/timestamp.h"
diff --git a/libavfilter/vf_blend.c b/libavfilter/vf_blend.c
index 74bc5989d9..6b52647966 100644
--- a/libavfilter/vf_blend.c
+++ b/libavfilter/vf_blend.c
@@ -21,6 +21,7 @@
 #include "config_components.h"
 
 #include "libavutil/eval.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixfmt.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_blockdetect.c b/libavfilter/vf_blockdetect.c
index d787aff5e4..dae02678bb 100644
--- a/libavfilter/vf_blockdetect.c
+++ b/libavfilter/vf_blockdetect.c
@@ -30,6 +30,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "internal.h"
 #include "video.h"
diff --git a/libavfilter/vf_blurdetect.c b/libavfilter/vf_blurdetect.c
index 354a6b6100..f7b02b7d1c 100644
--- a/libavfilter/vf_blurdetect.c
+++ b/libavfilter/vf_blurdetect.c
@@ -31,6 +31,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/qsort.h"
 #include "internal.h"
diff --git a/libavfilter/vf_bm3d.c b/libavfilter/vf_bm3d.c
index 17d39ee7cf..f53a206b51 100644
--- a/libavfilter/vf_bm3d.c
+++ b/libavfilter/vf_bm3d.c
@@ -33,6 +33,7 @@
 
 #include "libavutil/cpu.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/tx.h"
diff --git a/libavfilter/vf_boxblur.c b/libavfilter/vf_boxblur.c
index 60375463a6..205bc99a6d 100644
--- a/libavfilter/vf_boxblur.c
+++ b/libavfilter/vf_boxblur.c
@@ -27,6 +27,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "formats.h"
diff --git a/libavfilter/vf_codecview.c b/libavfilter/vf_codecview.c
index 77e541e37d..b204f39fbf 100644
--- a/libavfilter/vf_codecview.c
+++ b/libavfilter/vf_codecview.c
@@ -30,6 +30,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/motion_vector.h"
 #include "libavutil/opt.h"
 #include "libavutil/video_enc_params.h"
diff --git a/libavfilter/vf_colorchannelmixer.c b/libavfilter/vf_colorchannelmixer.c
index 8402b57858..006a8ee289 100644
--- a/libavfilter/vf_colorchannelmixer.c
+++ b/libavfilter/vf_colorchannelmixer.c
@@ -20,6 +20,7 @@
 
 #include <float.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_colorconstancy.c b/libavfilter/vf_colorconstancy.c
index 3d4d433cc1..5ea9062bfa 100644
--- a/libavfilter/vf_colorconstancy.c
+++ b/libavfilter/vf_colorconstancy.c
@@ -29,6 +29,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "avfilter.h"
diff --git a/libavfilter/vf_colorcorrect.c b/libavfilter/vf_colorcorrect.c
index 3433b5c4c3..d86d9f1927 100644
--- a/libavfilter/vf_colorcorrect.c
+++ b/libavfilter/vf_colorcorrect.c
@@ -20,6 +20,7 @@
 
 #include <float.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_colorspace.c b/libavfilter/vf_colorspace.c
index f367ce17c6..d181e81ace 100644
--- a/libavfilter/vf_colorspace.c
+++ b/libavfilter/vf_colorspace.c
@@ -25,6 +25,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/csp.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_convolution.c b/libavfilter/vf_convolution.c
index 88b89289a9..d516db3717 100644
--- a/libavfilter/vf_convolution.c
+++ b/libavfilter/vf_convolution.c
@@ -24,6 +24,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_convolve.c b/libavfilter/vf_convolve.c
index 95d1743597..971494f3d5 100644
--- a/libavfilter/vf_convolve.c
+++ b/libavfilter/vf_convolve.c
@@ -22,6 +22,7 @@
 
 #include <float.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/tx.h"
diff --git a/libavfilter/vf_coreimage.m b/libavfilter/vf_coreimage.m
index 979eab3b01..4d4cdfb7c7 100644
--- a/libavfilter/vf_coreimage.m
+++ b/libavfilter/vf_coreimage.m
@@ -31,6 +31,7 @@
 #include "internal.h"
 #include "video.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 
diff --git a/libavfilter/vf_corr.c b/libavfilter/vf_corr.c
index 7e0f81921f..27e01b8775 100644
--- a/libavfilter/vf_corr.c
+++ b/libavfilter/vf_corr.c
@@ -22,6 +22,7 @@
  */
 
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_cover_rect.c b/libavfilter/vf_cover_rect.c
index a4321fdb13..5e32ba9928 100644
--- a/libavfilter/vf_cover_rect.c
+++ b/libavfilter/vf_cover_rect.c
@@ -23,6 +23,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "filters.h"
 #include "internal.h"
diff --git a/libavfilter/vf_cropdetect.c b/libavfilter/vf_cropdetect.c
index a1cfc465d8..486f723a4b 100644
--- a/libavfilter/vf_cropdetect.c
+++ b/libavfilter/vf_cropdetect.c
@@ -25,6 +25,7 @@
 
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/motion_vector.h"
 #include "libavutil/qsort.h"
diff --git a/libavfilter/vf_curves.c b/libavfilter/vf_curves.c
index 55c0a1236e..3e4a42bab3 100644
--- a/libavfilter/vf_curves.c
+++ b/libavfilter/vf_curves.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/bprint.h"
 #include "libavutil/eval.h"
diff --git a/libavfilter/vf_datascope.c b/libavfilter/vf_datascope.c
index fedd57b838..99bfd70330 100644
--- a/libavfilter/vf_datascope.c
+++ b/libavfilter/vf_datascope.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_dblur.c b/libavfilter/vf_dblur.c
index 3110d19271..5202c57489 100644
--- a/libavfilter/vf_dblur.c
+++ b/libavfilter/vf_dblur.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_dctdnoiz.c b/libavfilter/vf_dctdnoiz.c
index ab96dbe15e..ef68337816 100644
--- a/libavfilter/vf_dctdnoiz.c
+++ b/libavfilter/vf_dctdnoiz.c
@@ -30,6 +30,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/eval.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 #include "internal.h"
diff --git a/libavfilter/vf_deband.c b/libavfilter/vf_deband.c
index 177d888bed..9888285586 100644
--- a/libavfilter/vf_deband.c
+++ b/libavfilter/vf_deband.c
@@ -21,6 +21,7 @@
  * SOFTWARE.
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_decimate.c b/libavfilter/vf_decimate.c
index dbeca427f1..56b7ef0877 100644
--- a/libavfilter/vf_decimate.c
+++ b/libavfilter/vf_decimate.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/timestamp.h"
diff --git a/libavfilter/vf_deflicker.c b/libavfilter/vf_deflicker.c
index f84fe5eafb..c17a8c8a3b 100644
--- a/libavfilter/vf_deflicker.c
+++ b/libavfilter/vf_deflicker.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/qsort.h"
diff --git a/libavfilter/vf_dejudder.c b/libavfilter/vf_dejudder.c
index d74cd7b159..5edadf56cf 100644
--- a/libavfilter/vf_dejudder.c
+++ b/libavfilter/vf_dejudder.c
@@ -49,6 +49,7 @@
  *      even output then setting frame_rate=1/0 in practice.
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/mathematics.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_dnn_classify.c b/libavfilter/vf_dnn_classify.c
index 11389c029d..1f8f227e3a 100644
--- a/libavfilter/vf_dnn_classify.c
+++ b/libavfilter/vf_dnn_classify.c
@@ -22,6 +22,7 @@
  */
 
 #include "libavutil/file_open.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "filters.h"
 #include "dnn_filter_common.h"
diff --git a/libavfilter/vf_dnn_detect.c b/libavfilter/vf_dnn_detect.c
index 8f50573527..bacea3ef29 100644
--- a/libavfilter/vf_dnn_detect.c
+++ b/libavfilter/vf_dnn_detect.c
@@ -22,6 +22,7 @@
  */
 
 #include "libavutil/file_open.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "filters.h"
 #include "dnn_filter_common.h"
diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c
index 72629df2dc..2b6a0d0839 100644
--- a/libavfilter/vf_drawtext.c
+++ b/libavfilter/vf_drawtext.c
@@ -48,6 +48,7 @@
 #include "libavutil/bprint.h"
 #include "libavutil/common.h"
 #include "libavutil/eval.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/random_seed.h"
 #include "libavutil/parseutils.h"
diff --git a/libavfilter/vf_edgedetect.c b/libavfilter/vf_edgedetect.c
index 38c3503ce6..dcf20ab11c 100644
--- a/libavfilter/vf_edgedetect.c
+++ b/libavfilter/vf_edgedetect.c
@@ -27,6 +27,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "formats.h"
diff --git a/libavfilter/vf_elbg.c b/libavfilter/vf_elbg.c
index 17947e226a..732a2c8abf 100644
--- a/libavfilter/vf_elbg.c
+++ b/libavfilter/vf_elbg.c
@@ -24,6 +24,7 @@
  */
 
 #include "libavcodec/elbg.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/random_seed.h"
diff --git a/libavfilter/vf_entropy.c b/libavfilter/vf_entropy.c
index 81772e6c97..a26cbb19d8 100644
--- a/libavfilter/vf_entropy.c
+++ b/libavfilter/vf_entropy.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_fftdnoiz.c b/libavfilter/vf_fftdnoiz.c
index 93fc23bc40..93d068d046 100644
--- a/libavfilter/vf_fftdnoiz.c
+++ b/libavfilter/vf_fftdnoiz.c
@@ -20,6 +20,7 @@
 
 #include "libavutil/common.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/tx.h"
diff --git a/libavfilter/vf_fftfilt.c b/libavfilter/vf_fftfilt.c
index 634d2a4911..31e79a1163 100644
--- a/libavfilter/vf_fftfilt.c
+++ b/libavfilter/vf_fftfilt.c
@@ -29,6 +29,7 @@
 #include "libavutil/common.h"
 #include "libavutil/cpu.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/tx.h"
diff --git a/libavfilter/vf_fieldmatch.c b/libavfilter/vf_fieldmatch.c
index bcf0ba49e2..ffa36c9449 100644
--- a/libavfilter/vf_fieldmatch.c
+++ b/libavfilter/vf_fieldmatch.c
@@ -34,6 +34,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/timestamp.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_find_rect.c b/libavfilter/vf_find_rect.c
index 9f4ee1e32f..81564e4be1 100644
--- a/libavfilter/vf_find_rect.c
+++ b/libavfilter/vf_find_rect.c
@@ -23,6 +23,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "internal.h"
 #include "video.h"
diff --git a/libavfilter/vf_floodfill.c b/libavfilter/vf_floodfill.c
index d5fa42651a..3f94af0fc4 100644
--- a/libavfilter/vf_floodfill.c
+++ b/libavfilter/vf_floodfill.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_fspp.c b/libavfilter/vf_fspp.c
index 8f44390117..da32326b94 100644
--- a/libavfilter/vf_fspp.c
+++ b/libavfilter/vf_fspp.c
@@ -37,6 +37,7 @@
 
 #include "libavutil/emms.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_fsync.c b/libavfilter/vf_fsync.c
index a18f1a009b..63a5446fa3 100644
--- a/libavfilter/vf_fsync.c
+++ b/libavfilter/vf_fsync.c
@@ -27,6 +27,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/error.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavformat/avio.h"
 #include "video.h"
diff --git a/libavfilter/vf_gblur.c b/libavfilter/vf_gblur.c
index 6ce2c84736..812fad72a3 100644
--- a/libavfilter/vf_gblur.c
+++ b/libavfilter/vf_gblur.c
@@ -28,6 +28,7 @@
 #include <float.h>
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_gblur_vulkan.c b/libavfilter/vf_gblur_vulkan.c
index bac05b87b8..09be6015c3 100644
--- a/libavfilter/vf_gblur_vulkan.c
+++ b/libavfilter/vf_gblur_vulkan.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/random_seed.h"
 #include "libavutil/opt.h"
 #include "vulkan_filter.h"
diff --git a/libavfilter/vf_geq.c b/libavfilter/vf_geq.c
index ff243ad843..dbe50e5250 100644
--- a/libavfilter/vf_geq.c
+++ b/libavfilter/vf_geq.c
@@ -29,6 +29,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 #include "libavutil/eval.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "formats.h"
diff --git a/libavfilter/vf_gradfun.c b/libavfilter/vf_gradfun.c
index e8d9cae828..e854e320c6 100644
--- a/libavfilter/vf_gradfun.c
+++ b/libavfilter/vf_gradfun.c
@@ -35,6 +35,7 @@
 #include "libavutil/emms.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_grayworld.c b/libavfilter/vf_grayworld.c
index e9c959416e..61ed832253 100644
--- a/libavfilter/vf_grayworld.c
+++ b/libavfilter/vf_grayworld.c
@@ -26,6 +26,7 @@
   */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "avfilter.h"
diff --git a/libavfilter/vf_guided.c b/libavfilter/vf_guided.c
index e5c534a6ae..68a1b97d1c 100644
--- a/libavfilter/vf_guided.c
+++ b/libavfilter/vf_guided.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_hqdn3d.c b/libavfilter/vf_hqdn3d.c
index d95ae8f898..9f32b943c9 100644
--- a/libavfilter/vf_hqdn3d.c
+++ b/libavfilter/vf_hqdn3d.c
@@ -32,6 +32,7 @@
 #include "libavutil/attributes.h"
 #include "libavutil/common.h"
 #include "libavutil/emms.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/opt.h"
diff --git a/libavfilter/vf_hue.c b/libavfilter/vf_hue.c
index 8fea2128fa..bf390a03fc 100644
--- a/libavfilter/vf_hue.c
+++ b/libavfilter/vf_hue.c
@@ -28,6 +28,7 @@
 #include <float.h>
 #include "libavutil/eval.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 
diff --git a/libavfilter/vf_hysteresis.c b/libavfilter/vf_hysteresis.c
index d2fd3011b8..42678a034c 100644
--- a/libavfilter/vf_hysteresis.c
+++ b/libavfilter/vf_hysteresis.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_identity.c b/libavfilter/vf_identity.c
index d3a5ec14f6..ed94069647 100644
--- a/libavfilter/vf_identity.c
+++ b/libavfilter/vf_identity.c
@@ -26,6 +26,7 @@
 #include "config_components.h"
 
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_kerndeint.c b/libavfilter/vf_kerndeint.c
index bbad3f9bfc..5baa8d1897 100644
--- a/libavfilter/vf_kerndeint.c
+++ b/libavfilter/vf_kerndeint.c
@@ -28,6 +28,7 @@
 
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 
diff --git a/libavfilter/vf_lagfun.c b/libavfilter/vf_lagfun.c
index 20cdd9fa65..4d7496c3eb 100644
--- a/libavfilter/vf_lagfun.c
+++ b/libavfilter/vf_lagfun.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 
diff --git a/libavfilter/vf_lenscorrection.c b/libavfilter/vf_lenscorrection.c
index aa545f71c6..732c7264d0 100644
--- a/libavfilter/vf_lenscorrection.c
+++ b/libavfilter/vf_lenscorrection.c
@@ -27,6 +27,7 @@
 #include <math.h>
 
 #include "libavutil/colorspace.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_lensfun.c b/libavfilter/vf_lensfun.c
index 3d7e6cd6b3..22016a7e8a 100644
--- a/libavfilter/vf_lensfun.c
+++ b/libavfilter/vf_lensfun.c
@@ -28,6 +28,7 @@
 #include <float.h>
 #include <math.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "filters.h"
diff --git a/libavfilter/vf_libopencv.c b/libavfilter/vf_libopencv.c
index 86407d18eb..8a35985b20 100644
--- a/libavfilter/vf_libopencv.c
+++ b/libavfilter/vf_libopencv.c
@@ -34,6 +34,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/common.h"
 #include "libavutil/file.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "formats.h"
diff --git a/libavfilter/vf_libplacebo.c b/libavfilter/vf_libplacebo.c
index f0c34bd3bd..be9000aa8e 100644
--- a/libavfilter/vf_libplacebo.c
+++ b/libavfilter/vf_libplacebo.c
@@ -20,6 +20,7 @@
 #include "libavutil/eval.h"
 #include "libavutil/fifo.h"
 #include "libavutil/file.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "formats.h"
diff --git a/libavfilter/vf_libvmaf.c b/libavfilter/vf_libvmaf.c
index 180ada040f..2c99d792af 100644
--- a/libavfilter/vf_libvmaf.c
+++ b/libavfilter/vf_libvmaf.c
@@ -29,6 +29,7 @@
 #include <libvmaf.h>
 
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_lut.c b/libavfilter/vf_lut.c
index 1a6ca06546..01df8f287d 100644
--- a/libavfilter/vf_lut.c
+++ b/libavfilter/vf_lut.c
@@ -30,6 +30,7 @@
 #include "libavutil/bswap.h"
 #include "libavutil/common.h"
 #include "libavutil/eval.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_lut2.c b/libavfilter/vf_lut2.c
index 49755544b0..5eed623cfe 100644
--- a/libavfilter/vf_lut2.c
+++ b/libavfilter/vf_lut2.c
@@ -23,6 +23,7 @@
 #include "libavutil/attributes.h"
 #include "libavutil/common.h"
 #include "libavutil/eval.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_lut3d.c b/libavfilter/vf_lut3d.c
index a312ca76c9..02b1f6c717 100644
--- a/libavfilter/vf_lut3d.c
+++ b/libavfilter/vf_lut3d.c
@@ -28,6 +28,7 @@
 
 #include "float.h"
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/file_open.h"
 #include "libavutil/intfloat.h"
diff --git a/libavfilter/vf_median.c b/libavfilter/vf_median.c
index 57514f9289..5ed787af5e 100644
--- a/libavfilter/vf_median.c
+++ b/libavfilter/vf_median.c
@@ -21,6 +21,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_mestimate.c b/libavfilter/vf_mestimate.c
index d551dab852..3397109831 100644
--- a/libavfilter/vf_mestimate.c
+++ b/libavfilter/vf_mestimate.c
@@ -21,6 +21,7 @@
 #include "motion_estimation.h"
 #include "libavcodec/mathops.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/motion_vector.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_midequalizer.c b/libavfilter/vf_midequalizer.c
index fae2b7ef19..37a4df7105 100644
--- a/libavfilter/vf_midequalizer.c
+++ b/libavfilter/vf_midequalizer.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_minterpolate.c b/libavfilter/vf_minterpolate.c
index 171f865b0b..27743d2f46 100644
--- a/libavfilter/vf_minterpolate.c
+++ b/libavfilter/vf_minterpolate.c
@@ -22,6 +22,7 @@
 #include "motion_estimation.h"
 #include "libavcodec/mathops.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_mix.c b/libavfilter/vf_mix.c
index 10da1cadd3..c4ef1fc002 100644
--- a/libavfilter/vf_mix.c
+++ b/libavfilter/vf_mix.c
@@ -23,6 +23,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 
diff --git a/libavfilter/vf_morpho.c b/libavfilter/vf_morpho.c
index 35d688a849..ce0f01c9c0 100644
--- a/libavfilter/vf_morpho.c
+++ b/libavfilter/vf_morpho.c
@@ -26,6 +26,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_nlmeans.c b/libavfilter/vf_nlmeans.c
index dc935538f0..64877317ed 100644
--- a/libavfilter/vf_nlmeans.c
+++ b/libavfilter/vf_nlmeans.c
@@ -30,6 +30,7 @@
  */
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_nlmeans_vulkan.c b/libavfilter/vf_nlmeans_vulkan.c
index fac38d16f4..5840aea4a7 100644
--- a/libavfilter/vf_nlmeans_vulkan.c
+++ b/libavfilter/vf_nlmeans_vulkan.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/random_seed.h"
 #include "libavutil/opt.h"
 #include "vulkan_filter.h"
diff --git a/libavfilter/vf_nnedi.c b/libavfilter/vf_nnedi.c
index 436d7a339e..2168c5dbc5 100644
--- a/libavfilter/vf_nnedi.c
+++ b/libavfilter/vf_nnedi.c
@@ -25,6 +25,7 @@
 #include "libavutil/file_open.h"
 #include "libavutil/float_dsp.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_noise.c b/libavfilter/vf_noise.c
index af33a73efa..d1943bc807 100644
--- a/libavfilter/vf_noise.c
+++ b/libavfilter/vf_noise.c
@@ -25,6 +25,7 @@
  */
 
 #include "libavutil/emms.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/lfg.h"
diff --git a/libavfilter/vf_normalize.c b/libavfilter/vf_normalize.c
index d81b2d6457..337c37f0ab 100644
--- a/libavfilter/vf_normalize.c
+++ b/libavfilter/vf_normalize.c
@@ -73,6 +73,7 @@
  */
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_overlay_qsv.c b/libavfilter/vf_overlay_qsv.c
index 19aaf03c17..6522ef4723 100644
--- a/libavfilter/vf_overlay_qsv.c
+++ b/libavfilter/vf_overlay_qsv.c
@@ -21,6 +21,7 @@
  * A hardware accelerated overlay filter based on Intel Quick Sync Video VPP
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/common.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_owdenoise.c b/libavfilter/vf_owdenoise.c
index fad85bfa8e..9bcab5a75e 100644
--- a/libavfilter/vf_owdenoise.c
+++ b/libavfilter/vf_owdenoise.c
@@ -34,6 +34,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_palettegen.c b/libavfilter/vf_palettegen.c
index dcd4e99aff..81897a69ec 100644
--- a/libavfilter/vf_palettegen.c
+++ b/libavfilter/vf_palettegen.c
@@ -26,6 +26,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/intreadwrite.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_paletteuse.c b/libavfilter/vf_paletteuse.c
index 8394f62f0f..cb251fa5f9 100644
--- a/libavfilter/vf_paletteuse.c
+++ b/libavfilter/vf_paletteuse.c
@@ -27,6 +27,7 @@
 #include "libavutil/bprint.h"
 #include "libavutil/file_open.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/qsort.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_perspective.c b/libavfilter/vf_perspective.c
index 0206c75b6d..a97b97bcb8 100644
--- a/libavfilter/vf_perspective.c
+++ b/libavfilter/vf_perspective.c
@@ -22,6 +22,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/eval.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_pixdesctest.c b/libavfilter/vf_pixdesctest.c
index c2638d058f..02145a327f 100644
--- a/libavfilter/vf_pixdesctest.c
+++ b/libavfilter/vf_pixdesctest.c
@@ -24,6 +24,7 @@
  */
 
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
 #include "internal.h"
diff --git a/libavfilter/vf_pp.c b/libavfilter/vf_pp.c
index aa37325a24..44705789e6 100644
--- a/libavfilter/vf_pp.c
+++ b/libavfilter/vf_pp.c
@@ -25,6 +25,7 @@
  */
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "internal.h"
diff --git a/libavfilter/vf_pp7.c b/libavfilter/vf_pp7.c
index c4f1b20086..24b39eebd2 100644
--- a/libavfilter/vf_pp7.c
+++ b/libavfilter/vf_pp7.c
@@ -29,6 +29,7 @@
 
 #include "libavutil/emms.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_psnr.c b/libavfilter/vf_psnr.c
index 058a8932f4..413d1fa1ee 100644
--- a/libavfilter/vf_psnr.c
+++ b/libavfilter/vf_psnr.c
@@ -27,6 +27,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/file_open.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_pullup.c b/libavfilter/vf_pullup.c
index 231f09620b..39a6cb14c2 100644
--- a/libavfilter/vf_pullup.c
+++ b/libavfilter/vf_pullup.c
@@ -21,6 +21,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/emms.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_readeia608.c b/libavfilter/vf_readeia608.c
index 50bf915765..7fb143cde0 100644
--- a/libavfilter/vf_readeia608.c
+++ b/libavfilter/vf_readeia608.c
@@ -27,6 +27,7 @@
 #include <string.h>
 
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 
diff --git a/libavfilter/vf_removelogo.c b/libavfilter/vf_removelogo.c
index b293054309..77f0f64ba3 100644
--- a/libavfilter/vf_removelogo.c
+++ b/libavfilter/vf_removelogo.c
@@ -70,6 +70,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "internal.h"
diff --git a/libavfilter/vf_sab.c b/libavfilter/vf_sab.c
index 5e0687c9a2..9e83e08355 100644
--- a/libavfilter/vf_sab.c
+++ b/libavfilter/vf_sab.c
@@ -23,6 +23,7 @@
  * Shape Adaptive Blur filter, ported from MPlayer libmpcodecs/vf_sab.c
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libswscale/swscale.h"
diff --git a/libavfilter/vf_scale.c b/libavfilter/vf_scale.c
index fc3b5a91e6..00fcb31b1c 100644
--- a/libavfilter/vf_scale.c
+++ b/libavfilter/vf_scale.c
@@ -32,14 +32,12 @@
 #include "internal.h"
 #include "scale_eval.h"
 #include "video.h"
-#include "libavutil/avstring.h"
 #include "libavutil/eval.h"
 #include "libavutil/internal.h"
-#include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
-#include "libavutil/imgutils.h"
 #include "libswscale/swscale.h"
 
 static const char *const var_names[] = {
diff --git a/libavfilter/vf_scale_npp.c b/libavfilter/vf_scale_npp.c
index 9e55fe6995..27e5e584ae 100644
--- a/libavfilter/vf_scale_npp.c
+++ b/libavfilter/vf_scale_npp.c
@@ -29,6 +29,7 @@
 #include "libavutil/hwcontext_cuda_internal.h"
 #include "libavutil/cuda_check.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/eval.h"
diff --git a/libavfilter/vf_shuffleframes.c b/libavfilter/vf_shuffleframes.c
index cf8f197e27..000be9c090 100644
--- a/libavfilter/vf_shuffleframes.c
+++ b/libavfilter/vf_shuffleframes.c
@@ -21,6 +21,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/common.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "avfilter.h"
diff --git a/libavfilter/vf_shufflepixels.c b/libavfilter/vf_shufflepixels.c
index d3e1af087d..27b7ecec84 100644
--- a/libavfilter/vf_shufflepixels.c
+++ b/libavfilter/vf_shufflepixels.c
@@ -24,6 +24,7 @@
 #include "libavutil/internal.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/lfg.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/random_seed.h"
diff --git a/libavfilter/vf_signalstats.c b/libavfilter/vf_signalstats.c
index 49bd9aa509..c2358c66cb 100644
--- a/libavfilter/vf_signalstats.c
+++ b/libavfilter/vf_signalstats.c
@@ -21,6 +21,7 @@
  */
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "filters.h"
diff --git a/libavfilter/vf_signature.c b/libavfilter/vf_signature.c
index 758b6e5101..35ed86cd9e 100644
--- a/libavfilter/vf_signature.c
+++ b/libavfilter/vf_signature.c
@@ -26,6 +26,7 @@
 
 #include "libavcodec/put_bits.h"
 #include "libavformat/avformat.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/avstring.h"
 #include "libavutil/file_open.h"
diff --git a/libavfilter/vf_siti.c b/libavfilter/vf_siti.c
index 1b6ff32d6e..722e7cecc7 100644
--- a/libavfilter/vf_siti.c
+++ b/libavfilter/vf_siti.c
@@ -28,6 +28,7 @@
 
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "avfilter.h"
diff --git a/libavfilter/vf_spp.c b/libavfilter/vf_spp.c
index 5c6495612b..48911b296a 100644
--- a/libavfilter/vf_spp.c
+++ b/libavfilter/vf_spp.c
@@ -33,6 +33,7 @@
 
 #include "libavutil/emms.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_ssim.c b/libavfilter/vf_ssim.c
index 53eb72fe6a..2210ea40ec 100644
--- a/libavfilter/vf_ssim.c
+++ b/libavfilter/vf_ssim.c
@@ -36,6 +36,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/file_open.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_ssim360.c b/libavfilter/vf_ssim360.c
index 0f5b11c723..33606ea599 100644
--- a/libavfilter/vf_ssim360.c
+++ b/libavfilter/vf_ssim360.c
@@ -45,6 +45,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/file_open.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 
diff --git a/libavfilter/vf_stack.c b/libavfilter/vf_stack.c
index 2bb3d9b1d2..5bb50148a7 100644
--- a/libavfilter/vf_stack.c
+++ b/libavfilter/vf_stack.c
@@ -22,6 +22,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_stack_qsv.c b/libavfilter/vf_stack_qsv.c
index 3e6aefe44b..abaf156915 100644
--- a/libavfilter/vf_stack_qsv.c
+++ b/libavfilter/vf_stack_qsv.c
@@ -23,6 +23,7 @@
 
 #include "config_components.h"
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/common.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_subtitles.c b/libavfilter/vf_subtitles.c
index a6338f3daf..902ee960c5 100644
--- a/libavfilter/vf_subtitles.c
+++ b/libavfilter/vf_subtitles.c
@@ -38,6 +38,7 @@
 #endif
 #include "libavutil/avstring.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "drawutils.h"
diff --git a/libavfilter/vf_swaprect.c b/libavfilter/vf_swaprect.c
index 119fd692fa..54400f0304 100644
--- a/libavfilter/vf_swaprect.c
+++ b/libavfilter/vf_swaprect.c
@@ -19,9 +19,9 @@
  */
 
 #include "libavutil/avassert.h"
-#include "libavutil/avstring.h"
 #include "libavutil/eval.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "avfilter.h"
diff --git a/libavfilter/vf_thumbnail.c b/libavfilter/vf_thumbnail.c
index 0b335c2a71..f7001c4ada 100644
--- a/libavfilter/vf_thumbnail.c
+++ b/libavfilter/vf_thumbnail.c
@@ -27,6 +27,7 @@
  * @see http://notbrainsurgery.livejournal.com/29773.html
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_thumbnail_cuda.c b/libavfilter/vf_thumbnail_cuda.c
index c8dd905123..0459070800 100644
--- a/libavfilter/vf_thumbnail_cuda.c
+++ b/libavfilter/vf_thumbnail_cuda.c
@@ -23,6 +23,7 @@
 #include "libavutil/hwcontext.h"
 #include "libavutil/hwcontext_cuda_internal.h"
 #include "libavutil/cuda_check.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 
diff --git a/libavfilter/vf_tiltandshift.c b/libavfilter/vf_tiltandshift.c
index dc30f4ff1b..85cce84fc3 100644
--- a/libavfilter/vf_tiltandshift.c
+++ b/libavfilter/vf_tiltandshift.c
@@ -25,14 +25,12 @@
 
 #include <string.h>
 
-#include "libavutil/common.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
-#include "libavutil/rational.h"
 
 #include "avfilter.h"
-#include "formats.h"
 #include "internal.h"
 #include "video.h"
 
diff --git a/libavfilter/vf_tinterlace.c b/libavfilter/vf_tinterlace.c
index c916e58375..89af1b89c5 100644
--- a/libavfilter/vf_tinterlace.c
+++ b/libavfilter/vf_tinterlace.c
@@ -26,6 +26,7 @@
  * temporal field interlace filter, ported from MPlayer/libmpcodecs
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/avassert.h"
diff --git a/libavfilter/vf_tmidequalizer.c b/libavfilter/vf_tmidequalizer.c
index 650aa36636..9e7b14cb67 100644
--- a/libavfilter/vf_tmidequalizer.c
+++ b/libavfilter/vf_tmidequalizer.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_uspp.c b/libavfilter/vf_uspp.c
index 10c8aaeb6c..ac3e8ea0da 100644
--- a/libavfilter/vf_uspp.c
+++ b/libavfilter/vf_uspp.c
@@ -29,6 +29,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_v360.c b/libavfilter/vf_v360.c
index 942b47d7a4..ba3de6dc6a 100644
--- a/libavfilter/vf_v360.c
+++ b/libavfilter/vf_v360.c
@@ -37,6 +37,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_vaguedenoiser.c b/libavfilter/vf_vaguedenoiser.c
index 19deaa25e2..7c92736df2 100644
--- a/libavfilter/vf_vaguedenoiser.c
+++ b/libavfilter/vf_vaguedenoiser.c
@@ -23,6 +23,7 @@
 #include "libavutil/imgutils.h"
 #include "libavutil/attributes.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/opt.h"
 
diff --git a/libavfilter/vf_varblur.c b/libavfilter/vf_varblur.c
index 6ebb9c0663..7a022099e9 100644
--- a/libavfilter/vf_varblur.c
+++ b/libavfilter/vf_varblur.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_vectorscope.c b/libavfilter/vf_vectorscope.c
index 735cf35a85..829ab1bc98 100644
--- a/libavfilter/vf_vectorscope.c
+++ b/libavfilter/vf_vectorscope.c
@@ -20,6 +20,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_vif.c b/libavfilter/vf_vif.c
index a927abaf6f..38b6a32365 100644
--- a/libavfilter/vf_vif.c
+++ b/libavfilter/vf_vif.c
@@ -27,6 +27,7 @@
 
 #include <float.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_vignette.c b/libavfilter/vf_vignette.c
index b329c510ab..13c511fdc7 100644
--- a/libavfilter/vf_vignette.c
+++ b/libavfilter/vf_vignette.c
@@ -20,6 +20,7 @@
 
 #include <float.h>  /* DBL_MAX */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/eval.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_vmafmotion.c b/libavfilter/vf_vmafmotion.c
index 022816e896..d1b67c7fb2 100644
--- a/libavfilter/vf_vmafmotion.c
+++ b/libavfilter/vf_vmafmotion.c
@@ -25,6 +25,7 @@
  */
 
 #include "libavutil/file_open.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_w3fdif.c b/libavfilter/vf_w3fdif.c
index 6814905a4f..a5a9cdd5cb 100644
--- a/libavfilter/vf_w3fdif.c
+++ b/libavfilter/vf_w3fdif.c
@@ -23,6 +23,7 @@
 
 #include "libavutil/common.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_waveform.c b/libavfilter/vf_waveform.c
index e004ea84f1..15202b3dd3 100644
--- a/libavfilter/vf_waveform.c
+++ b/libavfilter/vf_waveform.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vf_xmedian.c b/libavfilter/vf_xmedian.c
index ebcbea97ed..334d4018a2 100644
--- a/libavfilter/vf_xmedian.c
+++ b/libavfilter/vf_xmedian.c
@@ -22,6 +22,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/qsort.h"
diff --git a/libavfilter/vf_yaepblur.c b/libavfilter/vf_yaepblur.c
index b39738b577..9b5ec0348f 100644
--- a/libavfilter/vf_yaepblur.c
+++ b/libavfilter/vf_yaepblur.c
@@ -27,6 +27,7 @@
  * Anal. Mach. Intell. PAMI-2, 1980."
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/imgutils.h"
 #include "avfilter.h"
diff --git a/libavfilter/vf_zscale.c b/libavfilter/vf_zscale.c
index ad8fdb6eb7..7d8c8e95ee 100644
--- a/libavfilter/vf_zscale.c
+++ b/libavfilter/vf_zscale.c
@@ -39,6 +39,7 @@
 #include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavfilter/vidstabutils.c b/libavfilter/vidstabutils.c
index 56fbdbdb87..a0c1fc990e 100644
--- a/libavfilter/vidstabutils.c
+++ b/libavfilter/vidstabutils.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "vidstabutils.h"
 
 const enum AVPixelFormat ff_vidstab_pix_fmts[] =  {
diff --git a/libavfilter/vsrc_cellauto.c b/libavfilter/vsrc_cellauto.c
index b469f1bf20..f9223fd848 100644
--- a/libavfilter/vsrc_cellauto.c
+++ b/libavfilter/vsrc_cellauto.c
@@ -28,6 +28,7 @@
 #include "libavutil/file.h"
 #include "libavutil/internal.h"
 #include "libavutil/lfg.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/random_seed.h"
 #include "libavutil/avstring.h"
diff --git a/libavfilter/vsrc_ddagrab.c b/libavfilter/vsrc_ddagrab.c
index 018f46ed94..2fb109dede 100644
--- a/libavfilter/vsrc_ddagrab.c
+++ b/libavfilter/vsrc_ddagrab.c
@@ -34,6 +34,7 @@
 #include <dxgi1_5.h>
 #endif
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/time.h"
 #include "libavutil/avstring.h"
diff --git a/libavfilter/vsrc_life.c b/libavfilter/vsrc_life.c
index 9f83ca5e18..b0129d0beb 100644
--- a/libavfilter/vsrc_life.c
+++ b/libavfilter/vsrc_life.c
@@ -29,6 +29,7 @@
 #include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/lfg.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/random_seed.h"
diff --git a/libavfilter/vsrc_mandelbrot.c b/libavfilter/vsrc_mandelbrot.c
index 8eede77377..982ef71814 100644
--- a/libavfilter/vsrc_mandelbrot.c
+++ b/libavfilter/vsrc_mandelbrot.c
@@ -30,6 +30,7 @@
 #include "video.h"
 #include "internal.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include <float.h>
 #include <math.h>
diff --git a/libavfilter/vsrc_testsrc.c b/libavfilter/vsrc_testsrc.c
index 55c7e30ed6..22a7f00da7 100644
--- a/libavfilter/vsrc_testsrc.c
+++ b/libavfilter/vsrc_testsrc.c
@@ -40,6 +40,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/common.h"
 #include "libavutil/ffmath.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
diff --git a/libavfilter/x86/vf_gblur_init.c b/libavfilter/x86/vf_gblur_init.c
index b47f6fbffb..6aabfb4106 100644
--- a/libavfilter/x86/vf_gblur_init.c
+++ b/libavfilter/x86/vf_gblur_init.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/cpu.h"
+#include "libavutil/mem.h"
 #include "libavutil/x86/cpu.h"
 #include "libavfilter/gblur.h"
 
diff --git a/libavformat/4xm.c b/libavformat/4xm.c
index 3424791b81..80e028fb61 100644
--- a/libavformat/4xm.c
+++ b/libavformat/4xm.c
@@ -29,6 +29,7 @@
 
 #include "libavutil/intreadwrite.h"
 #include "libavutil/intfloat.h"
+#include "libavutil/mem.h"
 #include "libavcodec/internal.h"
 #include "avformat.h"
 #include "demux.h"
diff --git a/libavformat/aadec.c b/libavformat/aadec.c
index dd698d0d5d..894bdc3f55 100644
--- a/libavformat/aadec.c
+++ b/libavformat/aadec.c
@@ -29,6 +29,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/dict.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/tea.h"
 #include "libavutil/opt.h"
 
diff --git a/libavformat/aaxdec.c b/libavformat/aaxdec.c
index 0ccd4944db..b9f98bd657 100644
--- a/libavformat/aaxdec.c
+++ b/libavformat/aaxdec.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "avio_internal.h"
 #include "demux.h"
diff --git a/libavformat/aiffdec.c b/libavformat/aiffdec.c
index 9318943f96..eb565054e7 100644
--- a/libavformat/aiffdec.c
+++ b/libavformat/aiffdec.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/intreadwrite.h"
 #include "libavutil/dict.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/ape.c b/libavformat/ape.c
index b3994d12fd..40937df9c4 100644
--- a/libavformat/ape.c
+++ b/libavformat/ape.c
@@ -23,6 +23,7 @@
 #include <stdio.h>
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/apetag.c b/libavformat/apetag.c
index f2794c46f2..bd0a19e4d6 100644
--- a/libavformat/apetag.c
+++ b/libavformat/apetag.c
@@ -23,6 +23,7 @@
 #include <inttypes.h>
 
 #include "libavutil/dict.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "avio_internal.h"
 #include "apetag.h"
diff --git a/libavformat/apngdec.c b/libavformat/apngdec.c
index 4690283337..d0005046c1 100644
--- a/libavformat/apngdec.c
+++ b/libavformat/apngdec.c
@@ -32,6 +32,7 @@
 #include "internal.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavcodec/apng.h"
 #include "libavcodec/png.h"
diff --git a/libavformat/apngenc.c b/libavformat/apngenc.c
index 4d0438f824..0d56d784f2 100644
--- a/libavformat/apngenc.c
+++ b/libavformat/apngenc.c
@@ -27,6 +27,7 @@
 #include "libavutil/crc.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavcodec/apng.h"
 #include "libavcodec/png.h"
diff --git a/libavformat/asf.c b/libavformat/asf.c
index 1285062220..a71337aa3c 100644
--- a/libavformat/asf.c
+++ b/libavformat/asf.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "asf.h"
 #include "demux.h"
 #include "id3v2.h"
diff --git a/libavformat/asfdec_f.c b/libavformat/asfdec_f.c
index 9713c02b0a..39e59c68e9 100644
--- a/libavformat/asfdec_f.c
+++ b/libavformat/asfdec_f.c
@@ -24,11 +24,11 @@
 #include "libavutil/attributes.h"
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
-#include "libavutil/bswap.h"
 #include "libavutil/common.h"
 #include "libavutil/dict.h"
 #include "libavutil/internal.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avformat.h"
 #include "avio_internal.h"
diff --git a/libavformat/asfdec_o.c b/libavformat/asfdec_o.c
index 484fb64b36..dfe448e9f7 100644
--- a/libavformat/asfdec_o.c
+++ b/libavformat/asfdec_o.c
@@ -26,6 +26,7 @@
 #include "libavutil/dict.h"
 #include "libavutil/internal.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/time_internal.h"
 
 #include "avformat.h"
diff --git a/libavformat/asfenc.c b/libavformat/asfenc.c
index 2f2d9b1bab..75285b065c 100644
--- a/libavformat/asfenc.c
+++ b/libavformat/asfenc.c
@@ -24,6 +24,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/dict.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavcodec/codec_desc.h"
 #include "avformat.h"
diff --git a/libavformat/assenc.c b/libavformat/assenc.c
index 7b474a6005..75c19afd05 100644
--- a/libavformat/assenc.c
+++ b/libavformat/assenc.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "avio_internal.h"
 #include "internal.h"
diff --git a/libavformat/au.c b/libavformat/au.c
index 98f1a5b507..2244995825 100644
--- a/libavformat/au.c
+++ b/libavformat/au.c
@@ -31,6 +31,7 @@
 
 #include "libavutil/bprint.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/av1dec.c b/libavformat/av1dec.c
index 3382613d54..f39aecce66 100644
--- a/libavformat/av1dec.c
+++ b/libavformat/av1dec.c
@@ -21,7 +21,6 @@
 
 #include "config_components.h"
 
-#include "libavutil/common.h"
 #include "libavutil/opt.h"
 #include "libavcodec/av1_parse.h"
 #include "libavcodec/bsf.h"
diff --git a/libavformat/avc.c b/libavformat/avc.c
index b0ceb1d2d8..047ea88077 100644
--- a/libavformat/avc.c
+++ b/libavformat/avc.c
@@ -20,9 +20,9 @@
  */
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavcodec/h264.h"
 #include "libavcodec/get_bits.h"
-#include "avformat.h"
 #include "avio.h"
 #include "avc.h"
 #include "avio_internal.h"
diff --git a/libavformat/avidec.c b/libavformat/avidec.c
index 985a9bf022..b50d606ea8 100644
--- a/libavformat/avidec.c
+++ b/libavformat/avidec.c
@@ -25,6 +25,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/dict.h"
 #include "libavutil/internal.h"
diff --git a/libavformat/avienc.c b/libavformat/avienc.c
index a61e5c3109..2a3cff1210 100644
--- a/libavformat/avienc.c
+++ b/libavformat/avienc.c
@@ -25,7 +25,6 @@
 #include "internal.h"
 #include "avi.h"
 #include "avio_internal.h"
-#include "config_components.h"
 #include "riff.h"
 #include "mpegts.h"
 #include "mux.h"
@@ -36,6 +35,7 @@
 #include "libavutil/internal.h"
 #include "libavutil/dict.h"
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/timestamp.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavformat/avio.c b/libavformat/avio.c
index 0503b22786..d109f3adff 100644
--- a/libavformat/avio.c
+++ b/libavformat/avio.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/dict.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/time.h"
 #include "libavutil/avassert.h"
diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index 3609960907..6a74c1ce68 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -24,6 +24,7 @@
 #include "libavutil/dict.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/avassert.h"
 #include "libavcodec/defs.h"
diff --git a/libavformat/avisynth.c b/libavformat/avisynth.c
index e85b9ae488..1709bf4051 100644
--- a/libavformat/avisynth.c
+++ b/libavformat/avisynth.c
@@ -21,10 +21,9 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
-#include "libavcodec/internal.h"
-
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/bethsoftvid.c b/libavformat/bethsoftvid.c
index bdf1bdc6c0..e3c4758f30 100644
--- a/libavformat/bethsoftvid.c
+++ b/libavformat/bethsoftvid.c
@@ -30,6 +30,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/bmv.c b/libavformat/bmv.c
index b2980cf582..84ab2aac5a 100644
--- a/libavformat/bmv.c
+++ b/libavformat/bmv.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/brstm.c b/libavformat/brstm.c
index 5b2a59b6eb..4d2ff752d9 100644
--- a/libavformat/brstm.c
+++ b/libavformat/brstm.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavcodec/bytestream.h"
 #include "avformat.h"
 #include "demux.h"
diff --git a/libavformat/cache.c b/libavformat/cache.c
index 115c2c2490..5f78adba9d 100644
--- a/libavformat/cache.c
+++ b/libavformat/cache.c
@@ -30,6 +30,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 #include "libavutil/file_open.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/tree.h"
 #include "avio.h"
diff --git a/libavformat/cafdec.c b/libavformat/cafdec.c
index 426c56b9bd..2b638ed2b9 100644
--- a/libavformat/cafdec.c
+++ b/libavformat/cafdec.c
@@ -36,6 +36,7 @@
 #include "libavutil/intreadwrite.h"
 #include "libavutil/intfloat.h"
 #include "libavutil/dict.h"
+#include "libavutil/mem.h"
 #include "caf.h"
 
 typedef struct CafContext {
diff --git a/libavformat/cafenc.c b/libavformat/cafenc.c
index 426bc05209..89ecb51e53 100644
--- a/libavformat/cafenc.c
+++ b/libavformat/cafenc.c
@@ -26,6 +26,7 @@
 #include "mux.h"
 #include "libavutil/intfloat.h"
 #include "libavutil/dict.h"
+#include "libavutil/mem.h"
 
 #define FRAME_SIZE_OFFSET 40
 
diff --git a/libavformat/cinedec.c b/libavformat/cinedec.c
index 9ddfc90b47..e0bcca4ce5 100644
--- a/libavformat/cinedec.c
+++ b/libavformat/cinedec.c
@@ -26,6 +26,7 @@
  */
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavcodec/bmp.h"
 #include "libavutil/intfloat.h"
 #include "avformat.h"
diff --git a/libavformat/concatdec.c b/libavformat/concatdec.c
index ac541a592f..51e64dfdd7 100644
--- a/libavformat/concatdec.c
+++ b/libavformat/concatdec.c
@@ -22,6 +22,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/bprint.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/timestamp.h"
diff --git a/libavformat/crypto.c b/libavformat/crypto.c
index 4393fb6399..cd462e7b9d 100644
--- a/libavformat/crypto.c
+++ b/libavformat/crypto.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/aes.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "url.h"
 
diff --git a/libavformat/dashdec.c b/libavformat/dashdec.c
index 2998bcfb48..7395da85a5 100644
--- a/libavformat/dashdec.c
+++ b/libavformat/dashdec.c
@@ -22,6 +22,7 @@
 #include <libxml/parser.h>
 #include <time.h>
 #include "libavutil/bprint.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/time.h"
 #include "libavutil/parseutils.h"
diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c
index 5cb38c4c31..8c14aa746e 100644
--- a/libavformat/dashenc.c
+++ b/libavformat/dashenc.c
@@ -33,6 +33,7 @@
 #include "libavutil/bprint.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/rational.h"
diff --git a/libavformat/data_uri.c b/libavformat/data_uri.c
index 28eb2b9e08..3868a19630 100644
--- a/libavformat/data_uri.c
+++ b/libavformat/data_uri.c
@@ -22,6 +22,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/avutil.h"
 #include "libavutil/base64.h"
+#include "libavutil/mem.h"
 #include "url.h"
 
 typedef struct {
diff --git a/libavformat/demux.c b/libavformat/demux.c
index e50d1a9cfe..8736ab337b 100644
--- a/libavformat/demux.c
+++ b/libavformat/demux.c
@@ -29,6 +29,7 @@
 #include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixfmt.h"
 #include "libavutil/time.h"
diff --git a/libavformat/demux_utils.c b/libavformat/demux_utils.c
index 171a07107b..86f551245b 100644
--- a/libavformat/demux_utils.c
+++ b/libavformat/demux_utils.c
@@ -19,7 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "libavutil/version.h"
+#include "libavutil/mem.h"
 
 #include "libavutil/avassert.h"
 #include "libavcodec/bytestream.h"
diff --git a/libavformat/dhav.c b/libavformat/dhav.c
index 997875eff6..303fb42bed 100644
--- a/libavformat/dhav.c
+++ b/libavformat/dhav.c
@@ -22,6 +22,7 @@
 
 #include <time.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/parseutils.h"
 #include "avio_internal.h"
 #include "avformat.h"
diff --git a/libavformat/dovi_isom.c b/libavformat/dovi_isom.c
index ef7ab1b6d9..d49aa5a75f 100644
--- a/libavformat/dovi_isom.c
+++ b/libavformat/dovi_isom.c
@@ -22,6 +22,7 @@
  */
 
 #include "libavutil/dovi_meta.h"
+#include "libavutil/mem.h"
 
 #include "libavcodec/put_bits.h"
 
diff --git a/libavformat/dss.c b/libavformat/dss.c
index 510b1bd60c..47c8f49d67 100644
--- a/libavformat/dss.c
+++ b/libavformat/dss.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 
 #include "avformat.h"
 #include "demux.h"
diff --git a/libavformat/dtshddec.c b/libavformat/dtshddec.c
index 9939724ac7..28d3aeb47a 100644
--- a/libavformat/dtshddec.c
+++ b/libavformat/dtshddec.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/intreadwrite.h"
 #include "libavutil/dict.h"
+#include "libavutil/mem.h"
 #include "libavcodec/dca.h"
 #include "avformat.h"
 #include "demux.h"
diff --git a/libavformat/dump.c b/libavformat/dump.c
index ca47d733f7..059fb84522 100644
--- a/libavformat/dump.c
+++ b/libavformat/dump.c
@@ -32,6 +32,7 @@
 #include "libavutil/ambient_viewing_environment.h"
 #include "libavutil/dovi_meta.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/replaygain.h"
 #include "libavutil/spherical.h"
diff --git a/libavformat/dv.c b/libavformat/dv.c
index c0d3343e37..8af0d5a652 100644
--- a/libavformat/dv.c
+++ b/libavformat/dv.c
@@ -40,6 +40,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/timecode.h"
 #include "dv.h"
 #include "libavutil/avassert.h"
diff --git a/libavformat/evc.c b/libavformat/evc.c
index 95f07266b0..fabccb054c 100644
--- a/libavformat/evc.c
+++ b/libavformat/evc.c
@@ -19,14 +19,12 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavcodec/get_bits.h"
 #include "libavcodec/golomb.h"
 #include "libavcodec/evc.h"
-#include "avformat.h"
 #include "avio.h"
 #include "evc.h"
-#include "avio_internal.h"
 
 // @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.1
 enum {
diff --git a/libavformat/ffmetadec.c b/libavformat/ffmetadec.c
index ab62b7006e..bfda7d0bd8 100644
--- a/libavformat/ffmetadec.c
+++ b/libavformat/ffmetadec.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/bprint.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "ffmeta.h"
diff --git a/libavformat/file.c b/libavformat/file.c
index dd5819c06f..b95af0ccba 100644
--- a/libavformat/file.c
+++ b/libavformat/file.c
@@ -24,6 +24,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/file_open.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avio.h"
 #if HAVE_DIRENT_H
diff --git a/libavformat/fitsdec.c b/libavformat/fitsdec.c
index fe2dd5ad5d..2302807b24 100644
--- a/libavformat/fitsdec.c
+++ b/libavformat/fitsdec.c
@@ -25,7 +25,7 @@
  */
 
 #include "libavutil/avassert.h"
-#include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "demux.h"
 #include "internal.h"
 #include "libavutil/opt.h"
diff --git a/libavformat/fitsenc.c b/libavformat/fitsenc.c
index a8efe93d3c..69da4319da 100644
--- a/libavformat/fitsenc.c
+++ b/libavformat/fitsenc.c
@@ -24,8 +24,8 @@
  * FITS muxer.
  */
 
+#include "avformat.h"
 #include "avio_internal.h"
-#include "internal.h"
 #include "mux.h"
 
 typedef struct FITSContext {
diff --git a/libavformat/flacdec.c b/libavformat/flacdec.c
index 4ce6251137..3d35da5fea 100644
--- a/libavformat/flacdec.c
+++ b/libavformat/flacdec.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavcodec/avcodec.h"
 #include "libavcodec/bytestream.h"
 #include "libavcodec/flac.h"
diff --git a/libavformat/flvdec.c b/libavformat/flvdec.c
index 892371fcd0..22a9b9e4a7 100644
--- a/libavformat/flvdec.c
+++ b/libavformat/flvdec.c
@@ -29,12 +29,12 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/dict.h"
 #include "libavutil/dict_internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/internal.h"
 #include "libavutil/intfloat.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mastering_display_metadata.h"
-#include "libavutil/mathematics.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index a7d4fa46a2..aba1d7d80b 100644
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -25,6 +25,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/mastering_display_metadata.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavcodec/codec_desc.h"
 #include "libavcodec/mpeg4audio.h"
 #include "avio.h"
diff --git a/libavformat/format.c b/libavformat/format.c
index 0cdfd85c22..e65a6fc05e 100644
--- a/libavformat/format.c
+++ b/libavformat/format.c
@@ -22,9 +22,8 @@
 #include "config_components.h"
 
 #include "libavutil/avstring.h"
-#include "libavutil/bprint.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
-#include "libavutil/thread.h"
 
 #include "avio_internal.h"
 #include "avformat.h"
diff --git a/libavformat/ftp.c b/libavformat/ftp.c
index f9b069a59e..fba32715ec 100644
--- a/libavformat/ftp.c
+++ b/libavformat/ftp.c
@@ -23,6 +23,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/parseutils.h"
 #include "avformat.h"
 #include "internal.h"
diff --git a/libavformat/gxfenc.c b/libavformat/gxfenc.c
index 0aea7bd7c2..43ffa9d455 100644
--- a/libavformat/gxfenc.c
+++ b/libavformat/gxfenc.c
@@ -22,6 +22,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/intfloat.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "avio_internal.h"
 #include "internal.h"
diff --git a/libavformat/hashenc.c b/libavformat/hashenc.c
index 17ee2f7e9f..e4e3244246 100644
--- a/libavformat/hashenc.c
+++ b/libavformat/hashenc.c
@@ -24,6 +24,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/hash.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avformat.h"
 #include "internal.h"
diff --git a/libavformat/hdsenc.c b/libavformat/hdsenc.c
index 17fa0c807f..01efa1f8da 100644
--- a/libavformat/hdsenc.c
+++ b/libavformat/hdsenc.c
@@ -33,6 +33,7 @@
 #include "libavutil/base64.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 typedef struct Fragment {
diff --git a/libavformat/hevc.c b/libavformat/hevc.c
index ca5187a92e..395907807f 100644
--- a/libavformat/hevc.c
+++ b/libavformat/hevc.c
@@ -22,6 +22,7 @@
 #include "libavcodec/golomb.h"
 #include "libavcodec/hevc.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avc.h"
 #include "avio.h"
 #include "avio_internal.h"
diff --git a/libavformat/hls.c b/libavformat/hls.c
index f6b44c2e35..97ae2db6ce 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -35,6 +35,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/dict.h"
 #include "libavutil/time.h"
diff --git a/libavformat/hls_sample_encryption.c b/libavformat/hls_sample_encryption.c
index d5b4c11b66..8ab066f9c8 100644
--- a/libavformat/hls_sample_encryption.c
+++ b/libavformat/hls_sample_encryption.c
@@ -28,6 +28,7 @@
 
 #include "libavutil/aes.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 
 #include "hls_sample_encryption.h"
 
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 2202ce64e4..ac8bb189f0 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -33,6 +33,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/bprint.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/log.h"
 #include "libavutil/random_seed.h"
diff --git a/libavformat/hlsproto.c b/libavformat/hlsproto.c
index 4db61dee30..02f695c1db 100644
--- a/libavformat/hlsproto.c
+++ b/libavformat/hlsproto.c
@@ -26,6 +26,7 @@
  */
 
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/time.h"
 #include "avio_internal.h"
 #include "internal.h"
diff --git a/libavformat/http.c b/libavformat/http.c
index 9e933b18b3..ed20359552 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -31,6 +31,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/bprint.h"
 #include "libavutil/getenv_utf8.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/time.h"
 #include "libavutil/parseutils.h"
diff --git a/libavformat/httpauth.c b/libavformat/httpauth.c
index 9780928357..9048362509 100644
--- a/libavformat/httpauth.c
+++ b/libavformat/httpauth.c
@@ -22,6 +22,7 @@
 #include "httpauth.h"
 #include "libavutil/base64.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "internal.h"
 #include "libavutil/random_seed.h"
 #include "libavutil/md5.h"
diff --git a/libavformat/iamf_parse.c b/libavformat/iamf_parse.c
index cb49cf0a57..bda1580bbd 100644
--- a/libavformat/iamf_parse.c
+++ b/libavformat/iamf_parse.c
@@ -20,10 +20,10 @@
  */
 
 #include "libavutil/avassert.h"
-#include "libavutil/common.h"
 #include "libavutil/iamf.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavcodec/get_bits.h"
 #include "libavcodec/flac.h"
 #include "libavcodec/leb.h"
diff --git a/libavformat/iamf_reader.c b/libavformat/iamf_reader.c
index 270cfac389..42f9770c13 100644
--- a/libavformat/iamf_reader.c
+++ b/libavformat/iamf_reader.c
@@ -22,6 +22,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavcodec/mathops.h"
 #include "libavcodec/packet.h"
 #include "avformat.h"
diff --git a/libavformat/icecast.c b/libavformat/icecast.c
index b06c53cabd..3146d7f384 100644
--- a/libavformat/icecast.c
+++ b/libavformat/icecast.c
@@ -20,8 +20,8 @@
  */
 
 
-#include "libavutil/avstring.h"
 #include "libavutil/bprint.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "avformat.h"
diff --git a/libavformat/icodec.c b/libavformat/icodec.c
index 0ec6e0b258..04421e1d68 100644
--- a/libavformat/icodec.c
+++ b/libavformat/icodec.c
@@ -25,6 +25,7 @@
  */
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavcodec/bytestream.h"
 #include "libavcodec/png.h"
 #include "avformat.h"
diff --git a/libavformat/icoenc.c b/libavformat/icoenc.c
index caa3ba4965..7a7d839d84 100644
--- a/libavformat/icoenc.c
+++ b/libavformat/icoenc.c
@@ -25,7 +25,7 @@
  */
 
 #include "libavutil/intreadwrite.h"
-#include "libavutil/pixdesc.h"
+#include "libavutil/mem.h"
 
 #include "libavcodec/codec_id.h"
 
diff --git a/libavformat/id3v2.c b/libavformat/id3v2.c
index d83716dcf4..a720b7261a 100644
--- a/libavformat/id3v2.c
+++ b/libavformat/id3v2.c
@@ -36,6 +36,7 @@
 #include "libavutil/bprint.h"
 #include "libavutil/dict.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavcodec/png.h"
 #include "avio_internal.h"
 #include "demux.h"
diff --git a/libavformat/iff.c b/libavformat/iff.c
index 633e0c11fd..3e51bf8cc7 100644
--- a/libavformat/iff.c
+++ b/libavformat/iff.c
@@ -34,6 +34,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/dict.h"
+#include "libavutil/mem.h"
 #include "libavcodec/bytestream.h"
 #include "avformat.h"
 #include "demux.h"
diff --git a/libavformat/imf_cpl.c b/libavformat/imf_cpl.c
index 5f1a67443f..137cff2a63 100644
--- a/libavformat/imf_cpl.c
+++ b/libavformat/imf_cpl.c
@@ -51,9 +51,9 @@
  */
 
 #include "imf.h"
-#include "libavformat/mxf.h"
 #include "libavutil/bprint.h"
 #include "libavutil/error.h"
+#include "libavutil/mem.h"
 #include <libxml/parser.h>
 
 xmlNodePtr ff_imf_xml_get_child_element_by_name(xmlNodePtr parent, const char *name_utf8)
diff --git a/libavformat/imfdec.c b/libavformat/imfdec.c
index b57c54bd14..8ebc946b1c 100644
--- a/libavformat/imfdec.c
+++ b/libavformat/imfdec.c
@@ -70,8 +70,8 @@
 #include "libavutil/avstring.h"
 #include "libavutil/bprint.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
-#include "mxf.h"
 #include <inttypes.h>
 #include <libxml/parser.h>
 
diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index ff0065aff7..a40675d434 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -27,9 +27,9 @@
 #include <sys/stat.h>
 #include "libavutil/avstring.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
-#include "libavutil/parseutils.h"
 #include "libavutil/intreadwrite.h"
 #include "libavcodec/gif.h"
 #include "avformat.h"
diff --git a/libavformat/ipfsgateway.c b/libavformat/ipfsgateway.c
index 9c0d461099..9a15bcf93b 100644
--- a/libavformat/ipfsgateway.c
+++ b/libavformat/ipfsgateway.c
@@ -22,6 +22,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/file_open.h"
 #include "libavutil/getenv_utf8.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include <sys/stat.h>
 #include "os_support.h"
diff --git a/libavformat/jvdec.c b/libavformat/jvdec.c
index 89c82483aa..2125044060 100644
--- a/libavformat/jvdec.c
+++ b/libavformat/jvdec.c
@@ -27,6 +27,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 
 #include "avformat.h"
 #include "demux.h"
diff --git a/libavformat/lafdec.c b/libavformat/lafdec.c
index 05f30691ba..f766b102c9 100644
--- a/libavformat/lafdec.c
+++ b/libavformat/lafdec.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "avio_internal.h"
 #include "demux.h"
diff --git a/libavformat/libamqp.c b/libavformat/libamqp.c
index 27851e1b28..3cc6bc0e94 100644
--- a/libavformat/libamqp.c
+++ b/libavformat/libamqp.c
@@ -23,9 +23,8 @@
 #include <amqp_tcp_socket.h>
 #include <sys/time.h>
 #include "avformat.h"
-#include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
-#include "libavutil/time.h"
 #include "network.h"
 #include "url.h"
 #include "urldecode.h"
diff --git a/libavformat/libgme.c b/libavformat/libgme.c
index c2baa9c3ff..d2bd588f4c 100644
--- a/libavformat/libgme.c
+++ b/libavformat/libgme.c
@@ -22,8 +22,7 @@
 */
 
 #include <gme/gme.h>
-#include "libavutil/avstring.h"
-#include "libavutil/eval.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avformat.h"
 #include "demux.h"
diff --git a/libavformat/libmodplug.c b/libavformat/libmodplug.c
index 8c6569f778..680a5fe9bc 100644
--- a/libavformat/libmodplug.c
+++ b/libavformat/libmodplug.c
@@ -26,6 +26,7 @@
 #include <libmodplug/modplug.h>
 #include "libavutil/avstring.h"
 #include "libavutil/eval.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avformat.h"
 #include "demux.h"
diff --git a/libavformat/libopenmpt.c b/libavformat/libopenmpt.c
index 1fab0f09d8..e67fb0b6b8 100644
--- a/libavformat/libopenmpt.c
+++ b/libavformat/libopenmpt.c
@@ -30,8 +30,8 @@
 #define OPENMPT_API_VERSION_AT_LEAST(major, minor, patch) (OPENMPT_API_VERSION >= OPENMPT_API_VERSION_MAKE((major), (minor), (patch)))
 #endif
 
-#include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avformat.h"
 #include "demux.h"
diff --git a/libavformat/libsmbclient.c b/libavformat/libsmbclient.c
index f81fff2dfc..28de2daa9b 100644
--- a/libavformat/libsmbclient.c
+++ b/libavformat/libsmbclient.c
@@ -20,6 +20,7 @@
 
 #include <libsmbclient.h>
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "url.h"
 
diff --git a/libavformat/libsrt.c b/libavformat/libsrt.c
index a7aafea536..9e860abccd 100644
--- a/libavformat/libsrt.c
+++ b/libavformat/libsrt.c
@@ -23,6 +23,7 @@
 
 #include <srt/srt.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/time.h"
diff --git a/libavformat/libssh.c b/libavformat/libssh.c
index 127faaabd3..5c062a7245 100644
--- a/libavformat/libssh.c
+++ b/libavformat/libssh.c
@@ -22,6 +22,7 @@
 #define LIBSSH_STATIC
 #include <libssh/sftp.h>
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/attributes.h"
 #include "libavformat/avio.h"
diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index 8897fd622c..0e9fa04ad1 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -46,6 +46,7 @@
 #include "libavutil/lzo.h"
 #include "libavutil/mastering_display_metadata.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/time_internal.h"
diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 0de4ec1dc0..566e9f4981 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -50,6 +50,7 @@
 #include "libavutil/lfg.h"
 #include "libavutil/mastering_display_metadata.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavformat/mlvdec.c b/libavformat/mlvdec.c
index b969a45550..bd3af2ecea 100644
--- a/libavformat/mlvdec.c
+++ b/libavformat/mlvdec.c
@@ -28,6 +28,7 @@
 
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/rational.h"
 #include "avformat.h"
 #include "demux.h"
diff --git a/libavformat/mms.c b/libavformat/mms.c
index 16babc0954..95414a2fb7 100644
--- a/libavformat/mms.c
+++ b/libavformat/mms.c
@@ -23,6 +23,7 @@
 #include "mms.h"
 #include "asf.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 
 #define MMS_MAX_STREAMS 256    /**< arbitrary sanity check value */
 
diff --git a/libavformat/mmsh.c b/libavformat/mmsh.c
index 60113d61d2..aeadb12dab 100644
--- a/libavformat/mmsh.c
+++ b/libavformat/mmsh.c
@@ -28,8 +28,9 @@
 #include <string.h>
 #include "libavutil/intreadwrite.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
-#include "internal.h"
+#include "avformat.h"
 #include "mms.h"
 #include "http.h"
 #include "url.h"
diff --git a/libavformat/mmst.c b/libavformat/mmst.c
index d7f71304e5..e68606086e 100644
--- a/libavformat/mmst.c
+++ b/libavformat/mmst.c
@@ -30,9 +30,9 @@
 
 #include "avformat.h"
 #include "mms.h"
-#include "internal.h"
 #include "avio_internal.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavcodec/bytestream.h"
 #include "network.h"
 #include "url.h"
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 8d1135270c..08a8cc5899 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -40,6 +40,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 #include "libavutil/dict.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/aes.h"
 #include "libavutil/aes_ctr.h"
diff --git a/libavformat/mov_chan.c b/libavformat/mov_chan.c
index d5225acddf..906d962c8e 100644
--- a/libavformat/mov_chan.c
+++ b/libavformat/mov_chan.c
@@ -27,6 +27,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavcodec/codec_id.h"
 #include "mov_chan.h"
 
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index ee6734995d..f34468b0c3 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -42,19 +42,18 @@
 #include "libavcodec/flac.h"
 #include "libavcodec/get_bits.h"
 
-#include "libavcodec/bsf.h"
 #include "libavcodec/internal.h"
 #include "libavcodec/put_bits.h"
 #include "libavcodec/vc1_common.h"
 #include "libavcodec/raw.h"
 #include "internal.h"
 #include "libavutil/avstring.h"
-#include "libavutil/bprint.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/csp.h"
 #include "libavutil/intfloat.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/libm.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/dict.h"
 #include "libavutil/pixdesc.h"
diff --git a/libavformat/movenc_ttml.c b/libavformat/movenc_ttml.c
index 6deae49657..413eccfc0f 100644
--- a/libavformat/movenc_ttml.c
+++ b/libavformat/movenc_ttml.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "avio_internal.h"
 #include "isom.h"
diff --git a/libavformat/movenccenc.c b/libavformat/movenccenc.c
index 74f3c4ccfb..ce79c0dc3b 100644
--- a/libavformat/movenccenc.c
+++ b/libavformat/movenccenc.c
@@ -20,6 +20,7 @@
  */
 #include "movenccenc.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avio_internal.h"
 #include "movenc.h"
 #include "avc.h"
diff --git a/libavformat/movenchint.c b/libavformat/movenchint.c
index 0169341189..1fa22fbffb 100644
--- a/libavformat/movenchint.c
+++ b/libavformat/movenchint.c
@@ -21,6 +21,7 @@
 
 #include "movenc.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "mux.h"
 #include "rtpenc_chain.h"
 #include "avio_internal.h"
diff --git a/libavformat/mp3enc.c b/libavformat/mp3enc.c
index 4a02a45069..5695eeec9e 100644
--- a/libavformat/mp3enc.c
+++ b/libavformat/mp3enc.c
@@ -26,6 +26,7 @@
 #include "mux.h"
 #include "rawenc.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavcodec/mpegaudio.h"
 #include "libavcodec/mpegaudiodata.h"
 #include "libavcodec/mpegaudiodecheader.h"
diff --git a/libavformat/mpc.c b/libavformat/mpc.c
index 89130d5a7e..60cb768ab6 100644
--- a/libavformat/mpc.c
+++ b/libavformat/mpc.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 
 #include "avformat.h"
 #include "demux.h"
diff --git a/libavformat/mpc8.c b/libavformat/mpc8.c
index 42a34a3255..6b6db1b283 100644
--- a/libavformat/mpc8.c
+++ b/libavformat/mpc8.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavcodec/get_bits.h"
 #include "libavcodec/unary.h"
 #include "apetag.h"
diff --git a/libavformat/mpeg.c b/libavformat/mpeg.c
index 8399ad7062..111b4a3d89 100644
--- a/libavformat/mpeg.c
+++ b/libavformat/mpeg.c
@@ -22,6 +22,7 @@
 #include "config_components.h"
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "avio_internal.h"
 #include "demux.h"
diff --git a/libavformat/mpegenc.c b/libavformat/mpegenc.c
index 14caf9e162..6b6763c30f 100644
--- a/libavformat/mpegenc.c
+++ b/libavformat/mpegenc.c
@@ -27,6 +27,7 @@
 #include "libavutil/fifo.h"
 #include "libavutil/log.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "libavcodec/put_bits.h"
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index de7a3c8b45..d540126172 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -22,17 +22,15 @@
 #include "config_components.h"
 
 #include "libavutil/buffer.h"
-#include "libavutil/common.h"
 #include "libavutil/crc.h"
 #include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
 #include "libavutil/dict.h"
-#include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/avassert.h"
 #include "libavutil/dovi_meta.h"
-#include "libavcodec/avcodec.h"
 #include "libavcodec/bytestream.h"
 #include "libavcodec/defs.h"
 #include "libavcodec/get_bits.h"
diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index d8c148bd3c..99ce3416f1 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -25,6 +25,7 @@
 #include "libavutil/dict.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "libavcodec/ac3_parser_internal.h"
diff --git a/libavformat/mpjpegdec.c b/libavformat/mpjpegdec.c
index e20c61be9b..125b17585e 100644
--- a/libavformat/mpjpegdec.c
+++ b/libavformat/mpjpegdec.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "avformat.h"
diff --git a/libavformat/mtv.c b/libavformat/mtv.c
index 9f52cb6c67..01379a18e7 100644
--- a/libavformat/mtv.c
+++ b/libavformat/mtv.c
@@ -24,8 +24,8 @@
  * MTV demuxer.
  */
 
-#include "libavutil/bswap.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/mux.c b/libavformat/mux.c
index f23eb0188d..84aace0a7a 100644
--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -27,6 +27,7 @@
 #include "libavcodec/codec_desc.h"
 #include "libavcodec/internal.h"
 #include "libavcodec/packet_internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/dict.h"
 #include "libavutil/timestamp.h"
diff --git a/libavformat/mvdec.c b/libavformat/mvdec.c
index 31640f7590..0718f0483a 100644
--- a/libavformat/mvdec.c
+++ b/libavformat/mvdec.c
@@ -28,6 +28,7 @@
 #include "libavutil/eval.h"
 #include "libavutil/intfloat.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/rational.h"
 
 #include "avformat.h"
diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c
index 558a6c3b0a..3dfcd1e34b 100644
--- a/libavformat/mxfdec.c
+++ b/libavformat/mxfdec.c
@@ -51,6 +51,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/mastering_display_metadata.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavcodec/bytestream.h"
 #include "libavcodec/defs.h"
 #include "libavcodec/internal.h"
diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c
index adc31c1cf4..f424858fc4 100644
--- a/libavformat/mxfenc.c
+++ b/libavformat/mxfenc.c
@@ -40,6 +40,7 @@
 #include <inttypes.h>
 #include <time.h>
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/random_seed.h"
 #include "libavutil/timecode.h"
diff --git a/libavformat/mxg.c b/libavformat/mxg.c
index 23d72ad23b..6a39873f21 100644
--- a/libavformat/mxg.c
+++ b/libavformat/mxg.c
@@ -22,6 +22,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavcodec/mjpeg.h"
 #include "avformat.h"
 #include "demux.h"
diff --git a/libavformat/nsvdec.c b/libavformat/nsvdec.c
index d6a39730cc..f7cc1ba668 100644
--- a/libavformat/nsvdec.c
+++ b/libavformat/nsvdec.c
@@ -23,6 +23,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/nut.c b/libavformat/nut.c
index a0bf257c7d..9692336ece 100644
--- a/libavformat/nut.c
+++ b/libavformat/nut.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/tree.h"
 #include "nut.h"
 #include "riff.h"
diff --git a/libavformat/nutdec.c b/libavformat/nutdec.c
index 670964fab3..da8c3b919f 100644
--- a/libavformat/nutdec.c
+++ b/libavformat/nutdec.c
@@ -26,6 +26,7 @@
 #include "libavutil/dict.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/tree.h"
 #include "libavcodec/bytestream.h"
 #include "avio_internal.h"
diff --git a/libavformat/nutenc.c b/libavformat/nutenc.c
index 2a33f1581b..55efb114c3 100644
--- a/libavformat/nutenc.c
+++ b/libavformat/nutenc.c
@@ -23,6 +23,7 @@
 
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/tree.h"
 #include "libavutil/dict.h"
 #include "libavutil/avassert.h"
diff --git a/libavformat/oggdec.c b/libavformat/oggdec.c
index e473683aeb..ed89ea87f0 100644
--- a/libavformat/oggdec.c
+++ b/libavformat/oggdec.c
@@ -31,6 +31,7 @@
 #include <stdio.h>
 #include "libavutil/avassert.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avio_internal.h"
 #include "demux.h"
 #include "oggdec.h"
diff --git a/libavformat/oggenc.c b/libavformat/oggenc.c
index bdd19530ce..f5782cb583 100644
--- a/libavformat/oggenc.c
+++ b/libavformat/oggenc.c
@@ -25,6 +25,7 @@
 
 #include "libavutil/crc.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/random_seed.h"
 #include "libavcodec/xiph.h"
diff --git a/libavformat/oggparsecelt.c b/libavformat/oggparsecelt.c
index e528fd1f41..626e1ab27d 100644
--- a/libavformat/oggparsecelt.c
+++ b/libavformat/oggparsecelt.c
@@ -22,6 +22,7 @@
 #include <string.h>
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "internal.h"
 #include "oggdec.h"
diff --git a/libavformat/oggparsedirac.c b/libavformat/oggparsedirac.c
index 74b9ba4f8b..c5bd43a757 100644
--- a/libavformat/oggparsedirac.c
+++ b/libavformat/oggparsedirac.c
@@ -20,6 +20,7 @@
 
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavcodec/dirac.h"
 #include "avformat.h"
 #include "internal.h"
diff --git a/libavformat/oggparseopus.c b/libavformat/oggparseopus.c
index 54aa725be6..218e9df581 100644
--- a/libavformat/oggparseopus.c
+++ b/libavformat/oggparseopus.c
@@ -22,6 +22,7 @@
 #include <string.h>
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "internal.h"
 #include "oggdec.h"
diff --git a/libavformat/oggparsespeex.c b/libavformat/oggparsespeex.c
index d20d14fa41..0a4b32de15 100644
--- a/libavformat/oggparsespeex.c
+++ b/libavformat/oggparsespeex.c
@@ -24,6 +24,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 
 #include "avformat.h"
 #include "internal.h"
diff --git a/libavformat/oggparsetheora.c b/libavformat/oggparsetheora.c
index 85119352c3..6cdd962759 100644
--- a/libavformat/oggparsetheora.c
+++ b/libavformat/oggparsetheora.c
@@ -23,7 +23,7 @@
  **/
 
 #include <stdlib.h>
-#include "libavutil/bswap.h"
+#include "libavutil/mem.h"
 #include "libavcodec/get_bits.h"
 #include "avformat.h"
 #include "internal.h"
diff --git a/libavformat/oggparsevorbis.c b/libavformat/oggparsevorbis.c
index 6fd12560bc..9f50ab9ffc 100644
--- a/libavformat/oggparsevorbis.c
+++ b/libavformat/oggparsevorbis.c
@@ -27,6 +27,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/base64.h"
 #include "libavutil/dict.h"
+#include "libavutil/mem.h"
 
 #include "libavcodec/bytestream.h"
 #include "libavcodec/vorbis_parser.h"
diff --git a/libavformat/omadec.c b/libavformat/omadec.c
index f4bd6fb964..483386e7c4 100644
--- a/libavformat/omadec.c
+++ b/libavformat/omadec.c
@@ -43,6 +43,7 @@
 #include <inttypes.h>
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/options.c b/libavformat/options.c
index 34d2c6e23b..60a73d79b9 100644
--- a/libavformat/options.c
+++ b/libavformat/options.c
@@ -29,6 +29,7 @@
 #include "libavutil/iamf.h"
 #include "libavutil/internal.h"
 #include "libavutil/intmath.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 /**
diff --git a/libavformat/os_support.c b/libavformat/os_support.c
index 2de6a7c3d9..4d6eb8a74c 100644
--- a/libavformat/os_support.c
+++ b/libavformat/os_support.c
@@ -25,6 +25,7 @@
 #define _SVID_SOURCE
 
 #include "config.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "os_support.h"
 
diff --git a/libavformat/os_support.h b/libavformat/os_support.h
index f2ff38e23b..8479163f70 100644
--- a/libavformat/os_support.h
+++ b/libavformat/os_support.h
@@ -173,6 +173,7 @@ int ff_poll(struct pollfd *fds, nfds_t numfds, int timeout);
 #ifdef _WIN32
 #include <stdio.h>
 #include <windows.h>
+#include "libavutil/mem.h"
 #include "libavutil/wchar_filename.h"
 
 #define DEF_FS_FUNCTION(name, wfunc, afunc)               \
diff --git a/libavformat/paf.c b/libavformat/paf.c
index c99acbd3e7..f14317a0ce 100644
--- a/libavformat/paf.c
+++ b/libavformat/paf.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavcodec/paf.h"
 #include "avformat.h"
 #include "demux.h"
diff --git a/libavformat/pcmdec.c b/libavformat/pcmdec.c
index e9c97f7959..2f6508b75a 100644
--- a/libavformat/pcmdec.c
+++ b/libavformat/pcmdec.c
@@ -23,6 +23,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/pdvdec.c b/libavformat/pdvdec.c
index 79e09bd9d4..87f57ce140 100644
--- a/libavformat/pdvdec.c
+++ b/libavformat/pdvdec.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/pmpdec.c b/libavformat/pmpdec.c
index 06ef53d277..28aa746d36 100644
--- a/libavformat/pmpdec.c
+++ b/libavformat/pmpdec.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/pp_bnk.c b/libavformat/pp_bnk.c
index d545694bef..20f365baf4 100644
--- a/libavformat/pp_bnk.c
+++ b/libavformat/pp_bnk.c
@@ -26,6 +26,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 
 #define PP_BNK_MAX_READ_SIZE    4096
 #define PP_BNK_FILE_HEADER_SIZE 20
diff --git a/libavformat/prompeg.c b/libavformat/prompeg.c
index 59faa824bb..322eb6560a 100644
--- a/libavformat/prompeg.c
+++ b/libavformat/prompeg.c
@@ -80,10 +80,9 @@
 
  */
 
-#include "libavutil/avstring.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
-#include "libavutil/parseutils.h"
 #include "libavutil/random_seed.h"
 #include "avformat.h"
 #include "config.h"
diff --git a/libavformat/rdt.c b/libavformat/rdt.c
index 4721f01ace..60449d256a 100644
--- a/libavformat/rdt.c
+++ b/libavformat/rdt.c
@@ -27,6 +27,7 @@
 
 #include "avformat.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "demux.h"
 #include "rtpdec.h"
 #include "rdt.h"
diff --git a/libavformat/realtextdec.c b/libavformat/realtextdec.c
index 713e174b92..336b6dd937 100644
--- a/libavformat/realtextdec.c
+++ b/libavformat/realtextdec.c
@@ -30,7 +30,7 @@
 #include "subtitles.h"
 #include "libavutil/avstring.h"
 #include "libavutil/bprint.h"
-#include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 
 typedef struct {
     FFDemuxSubtitlesQueue q;
diff --git a/libavformat/riffdec.c b/libavformat/riffdec.c
index 0fe4e02b7b..b7a85a6ab2 100644
--- a/libavformat/riffdec.c
+++ b/libavformat/riffdec.c
@@ -24,6 +24,7 @@
 #include "libavutil/error.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "avio_internal.h"
 #include "demux.h"
diff --git a/libavformat/rl2.c b/libavformat/rl2.c
index d64c4b81d4..aa59332783 100644
--- a/libavformat/rl2.c
+++ b/libavformat/rl2.c
@@ -36,6 +36,7 @@
 
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/rmdec.c b/libavformat/rmdec.c
index ded194bddd..ba416e6390 100644
--- a/libavformat/rmdec.c
+++ b/libavformat/rmdec.c
@@ -26,6 +26,7 @@
 #include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/dict.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "avio_internal.h"
 #include "demux.h"
diff --git a/libavformat/rtmphttp.c b/libavformat/rtmphttp.c
index c920c19ab5..69dde826ae 100644
--- a/libavformat/rtmphttp.c
+++ b/libavformat/rtmphttp.c
@@ -25,10 +25,9 @@
  */
 
 #include "libavutil/avstring.h"
-#include "libavutil/intfloat.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/time.h"
-#include "internal.h"
 #include "http.h"
 #include "rtmp.h"
 
diff --git a/libavformat/rtmppkt.c b/libavformat/rtmppkt.c
index a602bf6a96..bb7e6d20bc 100644
--- a/libavformat/rtmppkt.c
+++ b/libavformat/rtmppkt.c
@@ -20,9 +20,8 @@
  */
 
 #include "libavcodec/bytestream.h"
-#include "libavutil/avstring.h"
 #include "libavutil/intfloat.h"
-#include "avformat.h"
+#include "libavutil/mem.h"
 
 #include "rtmppkt.h"
 #include "flv.h"
diff --git a/libavformat/rtmpproto.c b/libavformat/rtmpproto.c
index 4b01b67d28..bc3d9df7b9 100644
--- a/libavformat/rtmpproto.c
+++ b/libavformat/rtmpproto.c
@@ -32,6 +32,7 @@
 #include "libavutil/intfloat.h"
 #include "libavutil/lfg.h"
 #include "libavutil/md5.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/random_seed.h"
 #include "avformat.h"
diff --git a/libavformat/rtpdec.c b/libavformat/rtpdec.c
index fa7544cc07..729bf83685 100644
--- a/libavformat/rtpdec.c
+++ b/libavformat/rtpdec.c
@@ -22,6 +22,7 @@
 #include "libavutil/mathematics.h"
 #include "libavutil/avstring.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/time.h"
 
 #include "libavcodec/bytestream.h"
diff --git a/libavformat/rtpdec_asf.c b/libavformat/rtpdec_asf.c
index 20ca8295f9..9664623e57 100644
--- a/libavformat/rtpdec_asf.c
+++ b/libavformat/rtpdec_asf.c
@@ -28,6 +28,7 @@
 #include "libavutil/base64.h"
 #include "libavutil/avstring.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "rtpdec_formats.h"
 #include "rtsp.h"
 #include "asf.h"
diff --git a/libavformat/rtpdec_h264.c b/libavformat/rtpdec_h264.c
index a785120c23..72724c2909 100644
--- a/libavformat/rtpdec_h264.c
+++ b/libavformat/rtpdec_h264.c
@@ -37,6 +37,7 @@
 #include "libavutil/base64.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 
 #include "rtpdec.h"
diff --git a/libavformat/rtpdec_hevc.c b/libavformat/rtpdec_hevc.c
index a739ed5bd2..0e29efc3d6 100644
--- a/libavformat/rtpdec_hevc.c
+++ b/libavformat/rtpdec_hevc.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 
 #include "avformat.h"
 #include "internal.h"
diff --git a/libavformat/rtpdec_latm.c b/libavformat/rtpdec_latm.c
index 329b8dbc3e..2b4478289e 100644
--- a/libavformat/rtpdec_latm.c
+++ b/libavformat/rtpdec_latm.c
@@ -23,6 +23,7 @@
 #include "rtpdec_formats.h"
 #include "internal.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavcodec/get_bits.h"
 
 struct PayloadContext {
diff --git a/libavformat/rtpdec_mpa_robust.c b/libavformat/rtpdec_mpa_robust.c
index c0355edec2..8709b0d4ed 100644
--- a/libavformat/rtpdec_mpa_robust.c
+++ b/libavformat/rtpdec_mpa_robust.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 
 #include "avio_internal.h"
 #include "rtpdec_formats.h"
diff --git a/libavformat/rtpdec_mpeg4.c b/libavformat/rtpdec_mpeg4.c
index 723b6fc6ed..6531632b2d 100644
--- a/libavformat/rtpdec_mpeg4.c
+++ b/libavformat/rtpdec_mpeg4.c
@@ -31,6 +31,7 @@
 #include "internal.h"
 #include "libavutil/attributes.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavcodec/get_bits.h"
 
 #define MAX_AAC_HBR_FRAME_SIZE 8191
diff --git a/libavformat/rtpdec_qt.c b/libavformat/rtpdec_qt.c
index b71055f8b8..af00a7e767 100644
--- a/libavformat/rtpdec_qt.c
+++ b/libavformat/rtpdec_qt.c
@@ -25,6 +25,7 @@
  * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
  */
 
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "internal.h"
 #include "avio_internal.h"
diff --git a/libavformat/rtpdec_rfc4175.c b/libavformat/rtpdec_rfc4175.c
index 83abe499f8..d6260ab69e 100644
--- a/libavformat/rtpdec_rfc4175.c
+++ b/libavformat/rtpdec_rfc4175.c
@@ -24,6 +24,7 @@
 #include "avio_internal.h"
 #include "rtpdec_formats.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/parseutils.h"
 
diff --git a/libavformat/rtpdec_xiph.c b/libavformat/rtpdec_xiph.c
index c2db10dab8..95f4bdf3a1 100644
--- a/libavformat/rtpdec_xiph.c
+++ b/libavformat/rtpdec_xiph.c
@@ -31,6 +31,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 #include "libavutil/base64.h"
+#include "libavutil/mem.h"
 #include "libavcodec/bytestream.h"
 
 #include "avio_internal.h"
diff --git a/libavformat/rtpenc.c b/libavformat/rtpenc.c
index a8d296a154..7b4ae37d13 100644
--- a/libavformat/rtpenc.c
+++ b/libavformat/rtpenc.c
@@ -24,6 +24,7 @@
 #include "internal.h"
 #include "mux.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/random_seed.h"
 #include "libavutil/opt.h"
 
diff --git a/libavformat/rtpenc_mpegts.c b/libavformat/rtpenc_mpegts.c
index 0d6403a048..f9ff7e99cd 100644
--- a/libavformat/rtpenc_mpegts.c
+++ b/libavformat/rtpenc_mpegts.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avformat.h"
 #include "avio_internal.h"
diff --git a/libavformat/rtpproto.c b/libavformat/rtpproto.c
index cac361047a..b1cdf061f0 100644
--- a/libavformat/rtpproto.c
+++ b/libavformat/rtpproto.c
@@ -24,6 +24,7 @@
  * RTP protocol
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/avstring.h"
 #include "libavutil/opt.h"
@@ -34,7 +35,6 @@
 #include "ip.h"
 
 #include <stdarg.h>
-#include "internal.h"
 #include "network.h"
 #include "os_support.h"
 #include <fcntl.h>
diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
index 9a20481f21..b0c61ee00a 100644
--- a/libavformat/rtsp.c
+++ b/libavformat/rtsp.c
@@ -27,6 +27,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/random_seed.h"
 #include "libavutil/dict.h"
diff --git a/libavformat/rtspdec.c b/libavformat/rtspdec.c
index 8ca6f723e4..10078ce2fa 100644
--- a/libavformat/rtspdec.c
+++ b/libavformat/rtspdec.c
@@ -24,6 +24,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/random_seed.h"
 #include "libavutil/time.h"
 #include "avformat.h"
diff --git a/libavformat/rtspenc.c b/libavformat/rtspenc.c
index 3d444ccbfd..f2f4b6d525 100644
--- a/libavformat/rtspenc.c
+++ b/libavformat/rtspenc.c
@@ -32,6 +32,7 @@
 #include "avio_internal.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/time.h"
 #include "url.h"
 
diff --git a/libavformat/sapdec.c b/libavformat/sapdec.c
index 7480697f83..357c0dd514 100644
--- a/libavformat/sapdec.c
+++ b/libavformat/sapdec.c
@@ -23,6 +23,7 @@
 #include "demux.h"
 #include "libavutil/avstring.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "network.h"
 #include "os_support.h"
 #include "internal.h"
diff --git a/libavformat/sapenc.c b/libavformat/sapenc.c
index 28839b837f..87a834a8d8 100644
--- a/libavformat/sapenc.c
+++ b/libavformat/sapenc.c
@@ -20,6 +20,7 @@
  */
 
 #include "avformat.h"
+#include "libavutil/mem.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/random_seed.h"
 #include "libavutil/avstring.h"
diff --git a/libavformat/sauce.c b/libavformat/sauce.c
index 5ac9ca9d14..0c0266d782 100644
--- a/libavformat/sauce.c
+++ b/libavformat/sauce.c
@@ -26,6 +26,7 @@
 
 #include "libavutil/intreadwrite.h"
 #include "libavutil/dict.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "sauce.h"
 
diff --git a/libavformat/sbgdec.c b/libavformat/sbgdec.c
index bc2469afd1..b05a1e35c6 100644
--- a/libavformat/sbgdec.c
+++ b/libavformat/sbgdec.c
@@ -26,6 +26,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/time_internal.h"
 #include "avformat.h"
diff --git a/libavformat/scd.c b/libavformat/scd.c
index e34d60dcfb..cccf1ca307 100644
--- a/libavformat/scd.c
+++ b/libavformat/scd.c
@@ -26,6 +26,7 @@
 #include "libavutil/internal.h"
 #include "libavutil/macros.h"
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavformat/internal.h"
 #include "avformat.h"
 #include "demux.h"
diff --git a/libavformat/sdp.c b/libavformat/sdp.c
index 6888936290..7e11a75918 100644
--- a/libavformat/sdp.c
+++ b/libavformat/sdp.c
@@ -24,6 +24,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/base64.h"
 #include "libavutil/dict.h"
+#include "libavutil/mem.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/opt.h"
 #include "libavcodec/xiph.h"
diff --git a/libavformat/seek.c b/libavformat/seek.c
index 9ac133bf23..800ef40576 100644
--- a/libavformat/seek.c
+++ b/libavformat/seek.c
@@ -23,6 +23,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/timestamp.h"
 
 #include "libavcodec/avcodec.h"
diff --git a/libavformat/segafilm.c b/libavformat/segafilm.c
index 5cbeed6d91..73d751a08a 100644
--- a/libavformat/segafilm.c
+++ b/libavformat/segafilm.c
@@ -28,6 +28,7 @@
  */
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/segment.c b/libavformat/segment.c
index a38baa4f23..65323ec678 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -35,6 +35,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/internal.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/avstring.h"
 #include "libavutil/parseutils.h"
diff --git a/libavformat/sierravmd.c b/libavformat/sierravmd.c
index e7cc4075de..4ecfb49bed 100644
--- a/libavformat/sierravmd.c
+++ b/libavformat/sierravmd.c
@@ -29,6 +29,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/smacker.c b/libavformat/smacker.c
index c87174476b..774aad6790 100644
--- a/libavformat/smacker.c
+++ b/libavformat/smacker.c
@@ -27,6 +27,7 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "avio_internal.h"
 #include "demux.h"
diff --git a/libavformat/smjpegdec.c b/libavformat/smjpegdec.c
index 0003dbf0f5..8fd7659ae4 100644
--- a/libavformat/smjpegdec.c
+++ b/libavformat/smjpegdec.c
@@ -26,6 +26,7 @@
 
 #include <inttypes.h>
 
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/smoothstreamingenc.c b/libavformat/smoothstreamingenc.c
index e9c9f5d59f..9547419d31 100644
--- a/libavformat/smoothstreamingenc.c
+++ b/libavformat/smoothstreamingenc.c
@@ -31,6 +31,7 @@
 #include "avc.h"
 #include "url.h"
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/avstring.h"
 #include "libavutil/mathematics.h"
diff --git a/libavformat/soxdec.c b/libavformat/soxdec.c
index 661d6b30e5..ba349c870e 100644
--- a/libavformat/soxdec.c
+++ b/libavformat/soxdec.c
@@ -32,6 +32,7 @@
 #include "libavutil/intreadwrite.h"
 #include "libavutil/intfloat.h"
 #include "libavutil/dict.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/spdifenc.c b/libavformat/spdifenc.c
index f97f4d0175..ab3f73da0d 100644
--- a/libavformat/spdifenc.c
+++ b/libavformat/spdifenc.c
@@ -54,6 +54,7 @@
 #include "libavcodec/adts_parser.h"
 #include "libavcodec/dca.h"
 #include "libavcodec/dca_syncwords.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 typedef struct IEC61937Context {
diff --git a/libavformat/srtp.c b/libavformat/srtp.c
index f8b686c5aa..7e5a42e327 100644
--- a/libavformat/srtp.c
+++ b/libavformat/srtp.c
@@ -24,6 +24,7 @@
 #include "libavutil/hmac.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "rtp.h"
 #include "rtpdec.h"
 #include "srtp.h"
diff --git a/libavformat/subtitles.c b/libavformat/subtitles.c
index 3413763c7b..d7723c497f 100644
--- a/libavformat/subtitles.c
+++ b/libavformat/subtitles.c
@@ -22,6 +22,7 @@
 #include "subtitles.h"
 #include "avio_internal.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 
 void ff_text_init_avio(void *s, FFTextReader *r, AVIOContext *pb)
 {
diff --git a/libavformat/swfdec.c b/libavformat/swfdec.c
index 6bba5fb7eb..29eefc68a2 100644
--- a/libavformat/swfdec.c
+++ b/libavformat/swfdec.c
@@ -31,6 +31,7 @@
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavcodec/get_bits.h"
 #include "demux.h"
 #include "swf.h"
diff --git a/libavformat/takdec.c b/libavformat/takdec.c
index 5c405ebd24..21fff3fcbf 100644
--- a/libavformat/takdec.c
+++ b/libavformat/takdec.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/crc.h"
+#include "libavutil/mem.h"
 
 #define BITSTREAM_READER_LE
 #include "libavcodec/tak.h"
diff --git a/libavformat/tcp.c b/libavformat/tcp.c
index db41394a84..7dff9e9071 100644
--- a/libavformat/tcp.c
+++ b/libavformat/tcp.c
@@ -20,6 +20,7 @@
  */
 #include "avformat.h"
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/opt.h"
 #include "libavutil/time.h"
diff --git a/libavformat/tee.c b/libavformat/tee.c
index 9597137ec2..1cbbb80dbb 100644
--- a/libavformat/tee.c
+++ b/libavformat/tee.c
@@ -22,6 +22,7 @@
 
 #include "libavutil/avutil.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavcodec/bsf.h"
 #include "internal.h"
diff --git a/libavformat/tests/movenc.c b/libavformat/tests/movenc.c
index 1ec9b6729b..77f73abdfa 100644
--- a/libavformat/tests/movenc.c
+++ b/libavformat/tests/movenc.c
@@ -23,6 +23,7 @@
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/md5.h"
+#include "libavutil/mem.h"
 
 #include "libavformat/avformat.h"
 
diff --git a/libavformat/tiertexseq.c b/libavformat/tiertexseq.c
index 9de3689720..9187d79da8 100644
--- a/libavformat/tiertexseq.c
+++ b/libavformat/tiertexseq.c
@@ -25,6 +25,7 @@
  */
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/tls.c b/libavformat/tls.c
index 33e8ec384f..4577c42a3f 100644
--- a/libavformat/tls.c
+++ b/libavformat/tls.c
@@ -27,6 +27,7 @@
 #include "tls.h"
 #include "libavutil/avstring.h"
 #include "libavutil/getenv_utf8.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 
diff --git a/libavformat/tls_mbedtls.c b/libavformat/tls_mbedtls.c
index 8503523b6d..59ed4b447a 100644
--- a/libavformat/tls_mbedtls.c
+++ b/libavformat/tls_mbedtls.c
@@ -31,6 +31,7 @@
 #include "internal.h"
 #include "url.h"
 #include "tls.h"
+#include "libavutil/mem.h"
 #include "libavutil/parseutils.h"
 
 typedef struct TLSContext {
diff --git a/libavformat/tls_openssl.c b/libavformat/tls_openssl.c
index 8bf766c01f..b875be32f0 100644
--- a/libavformat/tls_openssl.c
+++ b/libavformat/tls_openssl.c
@@ -27,6 +27,7 @@
 #include "tls.h"
 #include "libavutil/avstring.h"
 #include "libavutil/avutil.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/thread.h"
diff --git a/libavformat/tls_schannel.c b/libavformat/tls_schannel.c
index d4959f75fa..214a47a218 100644
--- a/libavformat/tls_schannel.c
+++ b/libavformat/tls_schannel.c
@@ -20,6 +20,7 @@
 
 /** Based on the CURL SChannel module */
 
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "internal.h"
 #include "network.h"
diff --git a/libavformat/tls_securetransport.c b/libavformat/tls_securetransport.c
index f6a1a5e7b6..a5a3bd87a1 100644
--- a/libavformat/tls_securetransport.c
+++ b/libavformat/tls_securetransport.c
@@ -30,6 +30,7 @@
 #include "tls.h"
 #include "libavcodec/internal.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 
diff --git a/libavformat/ty.c b/libavformat/ty.c
index 71707f750f..ac3b9071fc 100644
--- a/libavformat/ty.c
+++ b/libavformat/ty.c
@@ -24,6 +24,7 @@
  */
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/udp.c b/libavformat/udp.c
index d9514f5026..c1b4e9b629 100644
--- a/libavformat/udp.c
+++ b/libavformat/udp.c
@@ -29,6 +29,7 @@
 
 #include "avformat.h"
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/fifo.h"
 #include "libavutil/intreadwrite.h"
diff --git a/libavformat/usmdec.c b/libavformat/usmdec.c
index 0c4e8d41be..9a21cc3041 100644
--- a/libavformat/usmdec.c
+++ b/libavformat/usmdec.c
@@ -20,6 +20,7 @@
  */
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavcodec/bytestream.h"
 
 #include "avformat.h"
diff --git a/libavformat/utils.c b/libavformat/utils.c
index c722743744..4dded7aea4 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -26,6 +26,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/bprint.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/thread.h"
 #include "libavutil/time.h"
 
diff --git a/libavformat/vapoursynth.c b/libavformat/vapoursynth.c
index e300392a8a..7697f02d55 100644
--- a/libavformat/vapoursynth.c
+++ b/libavformat/vapoursynth.c
@@ -33,6 +33,7 @@
 #include "libavutil/eval.h"
 #include "libavutil/frame.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avformat.h"
diff --git a/libavformat/vividas.c b/libavformat/vividas.c
index 98bf134f25..352a3bad72 100644
--- a/libavformat/vividas.c
+++ b/libavformat/vividas.c
@@ -30,6 +30,7 @@
 
 #include "libavutil/avassert.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avio_internal.h"
 #include "avformat.h"
 #include "demux.h"
diff --git a/libavformat/vqf.c b/libavformat/vqf.c
index 4c7f5aa22e..8f769e3222 100644
--- a/libavformat/vqf.c
+++ b/libavformat/vqf.c
@@ -25,6 +25,7 @@
 #include "libavutil/intreadwrite.h"
 #include "libavutil/dict.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "riff.h"
 
 typedef struct VqfContext {
diff --git a/libavformat/vvc.c b/libavformat/vvc.c
index 14a4c0a2f3..3123cd8d83 100644
--- a/libavformat/vvc.c
+++ b/libavformat/vvc.c
@@ -24,6 +24,7 @@
 #include "libavcodec/golomb.h"
 #include "libavcodec/vvc.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "avc.h"
 #include "avio.h"
 #include "avio_internal.h"
diff --git a/libavformat/wavdec.c b/libavformat/wavdec.c
index 668c8adc36..8df960d01b 100644
--- a/libavformat/wavdec.c
+++ b/libavformat/wavdec.c
@@ -33,6 +33,7 @@
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "avformat.h"
 #include "avio.h"
diff --git a/libavformat/wavenc.c b/libavformat/wavenc.c
index 7e853ce2fb..679eb2ce54 100644
--- a/libavformat/wavenc.c
+++ b/libavformat/wavenc.c
@@ -39,6 +39,7 @@
 #include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/time.h"
 #include "libavutil/time_internal.h"
diff --git a/libavformat/wc3movie.c b/libavformat/wc3movie.c
index 022b276fbd..0a4456b899 100644
--- a/libavformat/wc3movie.c
+++ b/libavformat/wc3movie.c
@@ -31,6 +31,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/dict.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavformat/webm_chunk.c b/libavformat/webm_chunk.c
index 494a83f507..255b8697c5 100644
--- a/libavformat/webm_chunk.c
+++ b/libavformat/webm_chunk.c
@@ -31,6 +31,7 @@
 #include "mux.h"
 
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/mathematics.h"
 
diff --git a/libavformat/webmdashenc.c b/libavformat/webmdashenc.c
index 31ea3c6e8c..3d9badc0d3 100644
--- a/libavformat/webmdashenc.c
+++ b/libavformat/webmdashenc.c
@@ -37,6 +37,7 @@
 
 #include "libavutil/avstring.h"
 #include "libavutil/dict.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/time_internal.h"
 
diff --git a/libavformat/wsddec.c b/libavformat/wsddec.c
index f9408be5ec..b0bf49cb04 100644
--- a/libavformat/wsddec.c
+++ b/libavformat/wsddec.c
@@ -21,10 +21,10 @@
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/timecode.h"
 #include "avformat.h"
 #include "demux.h"
-#include "internal.h"
 #include "rawdec.h"
 
 static int wsd_probe(const AVProbeData *p)
diff --git a/libavformat/wtvdec.c b/libavformat/wtvdec.c
index f41b0f2c68..e153034aa1 100644
--- a/libavformat/wtvdec.c
+++ b/libavformat/wtvdec.c
@@ -31,6 +31,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/intfloat.h"
+#include "libavutil/mem.h"
 #include "libavutil/time_internal.h"
 #include "avformat.h"
 #include "demux.h"
diff --git a/libavformat/wtvenc.c b/libavformat/wtvenc.c
index c0e5621646..02fe84573f 100644
--- a/libavformat/wtvenc.c
+++ b/libavformat/wtvenc.c
@@ -26,6 +26,7 @@
  */
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "avio_internal.h"
 #include "internal.h"
diff --git a/libavformat/xmv.c b/libavformat/xmv.c
index e2c4c7f93e..ced9f3be36 100644
--- a/libavformat/xmv.c
+++ b/libavformat/xmv.c
@@ -28,6 +28,7 @@
 #include <inttypes.h>
 
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 
 #include "avformat.h"
 #include "demux.h"
diff --git a/libavformat/xwma.c b/libavformat/xwma.c
index 525baee6f5..b77c6ed605 100644
--- a/libavformat/xwma.c
+++ b/libavformat/xwma.c
@@ -22,6 +22,7 @@
 #include <inttypes.h>
 #include <stdint.h>
 
+#include "libavutil/mem.h"
 #include "avformat.h"
 #include "demux.h"
 #include "internal.h"
diff --git a/libavutil/channel_layout.c b/libavutil/channel_layout.c
index a83618bcc7..65a86bdd45 100644
--- a/libavutil/channel_layout.c
+++ b/libavutil/channel_layout.c
@@ -33,6 +33,7 @@
 #include "common.h"
 #include "error.h"
 #include "macros.h"
+#include "mem.h"
 #include "opt.h"
 
 #define CHAN_IS_AMBI(x) ((x) >= AV_CHAN_AMBISONIC_BASE &&\
diff --git a/libavutil/common.h b/libavutil/common.h
index 57c87f1919..3e4c339893 100644
--- a/libavutil/common.h
+++ b/libavutil/common.h
@@ -42,12 +42,13 @@
 #include "attributes.h"
 #include "error.h"
 #include "macros.h"
-#include "mem.h"
 
 #ifdef HAVE_AV_CONFIG_H
 #   include "config.h"
 #   include "intmath.h"
 #   include "internal.h"
+#else
+#   include "mem.h"
 #endif /* HAVE_AV_CONFIG_H */
 
 //rounded division & shift
diff --git a/libavutil/detection_bbox.c b/libavutil/detection_bbox.c
index bae8f0642e..cb157b355b 100644
--- a/libavutil/detection_bbox.c
+++ b/libavutil/detection_bbox.c
@@ -17,6 +17,7 @@
  */
 
 #include "detection_bbox.h"
+#include "mem.h"
 
 AVDetectionBBoxHeader *av_detection_bbox_alloc(uint32_t nb_bboxes, size_t *out_size)
 {
diff --git a/libavutil/eval.c b/libavutil/eval.c
index bc4a00d7d2..298925a6e5 100644
--- a/libavutil/eval.c
+++ b/libavutil/eval.c
@@ -32,13 +32,12 @@
 #include "common.h"
 #include "eval.h"
 #include "ffmath.h"
-#include "internal.h"
 #include "log.h"
 #include "mathematics.h"
+#include "mem.h"
 #include "sfc64.h"
 #include "time.h"
 #include "avstring.h"
-#include "timer.h"
 #include "reverse.h"
 
 typedef struct Parser {
diff --git a/libavutil/executor.c b/libavutil/executor.c
index 0da919f4e8..26691fe157 100644
--- a/libavutil/executor.c
+++ b/libavutil/executor.c
@@ -17,7 +17,9 @@
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
-#include "internal.h"
+
+#include "config.h"
+
 #include "mem.h"
 #include "thread.h"
 
diff --git a/libavutil/film_grain_params.c b/libavutil/film_grain_params.c
index 8f8dcee569..0a6004b6b3 100644
--- a/libavutil/film_grain_params.c
+++ b/libavutil/film_grain_params.c
@@ -17,6 +17,7 @@
  */
 
 #include "film_grain_params.h"
+#include "mem.h"
 #include "pixdesc.h"
 
 AVFilmGrainParams *av_film_grain_params_alloc(size_t *size)
diff --git a/libavutil/fixed_dsp.c b/libavutil/fixed_dsp.c
index b144aa359e..95f0eb2595 100644
--- a/libavutil/fixed_dsp.c
+++ b/libavutil/fixed_dsp.c
@@ -47,6 +47,7 @@
 
 #include "common.h"
 #include "fixed_dsp.h"
+#include "mem.h"
 
 static void vector_fmul_add_c(int *dst, const int *src0, const int *src1, const int *src2, int len){
     int i;
diff --git a/libavutil/hwcontext_d3d11va.c b/libavutil/hwcontext_d3d11va.c
index e30c8fc238..8963c9fc85 100644
--- a/libavutil/hwcontext_d3d11va.c
+++ b/libavutil/hwcontext_d3d11va.c
@@ -36,6 +36,7 @@
 #include "hwcontext_d3d11va.h"
 #include "hwcontext_internal.h"
 #include "imgutils.h"
+#include "mem.h"
 #include "pixdesc.h"
 #include "pixfmt.h"
 #include "thread.h"
diff --git a/libavutil/hwcontext_d3d12va.c b/libavutil/hwcontext_d3d12va.c
index 353807359b..cfc016315d 100644
--- a/libavutil/hwcontext_d3d12va.c
+++ b/libavutil/hwcontext_d3d12va.c
@@ -27,6 +27,7 @@
 #include "hwcontext_d3d12va_internal.h"
 #include "hwcontext_d3d12va.h"
 #include "imgutils.h"
+#include "mem.h"
 #include "pixdesc.h"
 #include "pixfmt.h"
 #include "thread.h"
diff --git a/libavutil/hwcontext_drm.c b/libavutil/hwcontext_drm.c
index 7a9fdbd263..0847db09a0 100644
--- a/libavutil/hwcontext_drm.c
+++ b/libavutil/hwcontext_drm.c
@@ -38,6 +38,7 @@
 #include "hwcontext_drm.h"
 #include "hwcontext_internal.h"
 #include "imgutils.h"
+#include "mem.h"
 
 
 static void drm_device_free(AVHWDeviceContext *hwdev)
diff --git a/libavutil/hwcontext_dxva2.c b/libavutil/hwcontext_dxva2.c
index 77f34919a8..03cb739a7f 100644
--- a/libavutil/hwcontext_dxva2.c
+++ b/libavutil/hwcontext_dxva2.c
@@ -31,6 +31,7 @@
 #include "hwcontext_dxva2.h"
 #include "hwcontext_internal.h"
 #include "imgutils.h"
+#include "mem.h"
 #include "pixdesc.h"
 #include "pixfmt.h"
 #include "compat/w32dlfcn.h"
diff --git a/libavutil/hwcontext_vulkan.c b/libavutil/hwcontext_vulkan.c
index 91b9f96ccf..83ca51e3d8 100644
--- a/libavutil/hwcontext_vulkan.c
+++ b/libavutil/hwcontext_vulkan.c
@@ -37,9 +37,9 @@
 #include "avstring.h"
 #include "imgutils.h"
 #include "hwcontext.h"
-#include "avassert.h"
 #include "hwcontext_internal.h"
 #include "hwcontext_vulkan.h"
+#include "mem.h"
 
 #include "vulkan.h"
 #include "vulkan_loader.h"
diff --git a/libavutil/imgutils.c b/libavutil/imgutils.c
index 1e15f7c920..16e3133c33 100644
--- a/libavutil/imgutils.c
+++ b/libavutil/imgutils.c
@@ -29,6 +29,7 @@
 #include "intreadwrite.h"
 #include "log.h"
 #include "mathematics.h"
+#include "mem.h"
 #include "pixdesc.h"
 #include "rational.h"
 
diff --git a/libavutil/opt.c b/libavutil/opt.c
index 685206f416..76080f87e8 100644
--- a/libavutil/opt.c
+++ b/libavutil/opt.c
@@ -29,10 +29,10 @@
 #include "avassert.h"
 #include "avstring.h"
 #include "channel_layout.h"
-#include "common.h"
 #include "dict.h"
 #include "eval.h"
 #include "log.h"
+#include "mem.h"
 #include "parseutils.h"
 #include "pixdesc.h"
 #include "mathematics.h"
diff --git a/libavutil/pca.c b/libavutil/pca.c
index 4e52c7b362..7c3eb42b14 100644
--- a/libavutil/pca.c
+++ b/libavutil/pca.c
@@ -25,6 +25,7 @@
  */
 
 #include "common.h"
+#include "mem.h"
 #include "pca.h"
 
 typedef struct PCA{
diff --git a/libavutil/tests/dict.c b/libavutil/tests/dict.c
index bececefb31..e45bc220cb 100644
--- a/libavutil/tests/dict.c
+++ b/libavutil/tests/dict.c
@@ -18,6 +18,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
+
 #include "libavutil/dict.c"
 
 static const AVDictionaryEntry *dict_iterate(const AVDictionary *m,
diff --git a/libavutil/tests/imgutils.c b/libavutil/tests/imgutils.c
index 582a358157..6a5097bc35 100644
--- a/libavutil/tests/imgutils.c
+++ b/libavutil/tests/imgutils.c
@@ -18,6 +18,7 @@
 
 #include "libavutil/imgutils.c"
 #include "libavutil/crc.h"
+#include "libavutil/mem.h"
 
 #undef printf
 static int check_image_fill(enum AVPixelFormat pix_fmt, int w, int h) {
diff --git a/libavutil/tests/lfg.c b/libavutil/tests/lfg.c
index bf127e3031..859ada73a9 100644
--- a/libavutil/tests/lfg.c
+++ b/libavutil/tests/lfg.c
@@ -17,6 +17,7 @@
  */
 
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "libavutil/timer.h"
 #include "libavutil/lfg.h"
 
diff --git a/libavutil/tests/tea.c b/libavutil/tests/tea.c
index 605bb524af..738920e994 100644
--- a/libavutil/tests/tea.c
+++ b/libavutil/tests/tea.c
@@ -25,6 +25,7 @@
 #include <stdio.h>
 
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/tea.h"
 
 #define TEA_NUM_TESTS 4
diff --git a/libavutil/tests/tree.c b/libavutil/tests/tree.c
index 7503228970..5cd3e70de6 100644
--- a/libavutil/tests/tree.c
+++ b/libavutil/tests/tree.c
@@ -23,6 +23,7 @@
 #include "libavutil/common.h"
 #include "libavutil/lfg.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 
 static int check(AVTreeNode *t)
 {
diff --git a/libavutil/tx.c b/libavutil/tx.c
index 7a0e9effb3..0aae4c7cf7 100644
--- a/libavutil/tx.c
+++ b/libavutil/tx.c
@@ -19,6 +19,7 @@
 #include "avassert.h"
 #include "intmath.h"
 #include "cpu.h"
+#include "mem.h"
 #include "qsort.h"
 #include "bprint.h"
 
diff --git a/libavutil/tx_template.c b/libavutil/tx_template.c
index a2c27465cb..701ef0d6de 100644
--- a/libavutil/tx_template.c
+++ b/libavutil/tx_template.c
@@ -24,6 +24,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "mem.h"
+
 #define TABLE_DEF(name, size) \
     DECLARE_ALIGNED(32, TXSample, TX_TAB(ff_tx_tab_ ##name))[size]
 
diff --git a/libavutil/vulkan.c b/libavutil/vulkan.c
index 67b9526255..e0208c5a7c 100644
--- a/libavutil/vulkan.c
+++ b/libavutil/vulkan.c
@@ -19,6 +19,7 @@
  */
 
 #include "avassert.h"
+#include "mem.h"
 
 #include "vulkan.h"
 
diff --git a/libavutil/x86/tx_float_init.c b/libavutil/x86/tx_float_init.c
index 84ec1122f6..36da9325e5 100644
--- a/libavutil/x86/tx_float_init.c
+++ b/libavutil/x86/tx_float_init.c
@@ -19,6 +19,7 @@
 #define TX_FLOAT
 #include "libavutil/tx_priv.h"
 #include "libavutil/attributes.h"
+#include "libavutil/mem.h"
 #include "libavutil/x86/cpu.h"
 
 #include "config.h"
diff --git a/libpostproc/postprocess.c b/libpostproc/postprocess.c
index 0586e458b4..661de33ea8 100644
--- a/libpostproc/postprocess.c
+++ b/libpostproc/postprocess.c
@@ -77,6 +77,7 @@ try to unroll inner for(x=0 ... loop to avoid these damn if(x ... checks
 #include "libavutil/avassert.h"
 #include "libavutil/cpu.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/libswresample/audioconvert.c b/libswresample/audioconvert.c
index 1d75ba1495..04108fb966 100644
--- a/libswresample/audioconvert.c
+++ b/libswresample/audioconvert.c
@@ -25,9 +25,9 @@
  * @author Michael Niedermayer <michaelni@gmx.at>
  */
 
-#include "libavutil/avstring.h"
 #include "libavutil/avassert.h"
 #include "libavutil/libm.h"
+#include "libavutil/mem.h"
 #include "libavutil/samplefmt.h"
 #include "audioconvert.h"
 
diff --git a/libswresample/dither.c b/libswresample/dither.c
index 64068d35b6..61151a26ea 100644
--- a/libswresample/dither.c
+++ b/libswresample/dither.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "swresample_internal.h"
 
 #include "noise_shaping_data.c"
diff --git a/libswresample/options.c b/libswresample/options.c
index a3284a4896..11b2884e3c 100644
--- a/libswresample/options.c
+++ b/libswresample/options.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "swresample_internal.h"
 
diff --git a/libswresample/rematrix.c b/libswresample/rematrix.c
index fb4d9aecf0..b9bf4dcac0 100644
--- a/libswresample/rematrix.c
+++ b/libswresample/rematrix.c
@@ -21,6 +21,7 @@
 #include "swresample_internal.h"
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
 
 #define TEMPLATE_REMATRIX_FLT
 #include "rematrix_template.c"
diff --git a/libswresample/resample.c b/libswresample/resample.c
index 17cebad01b..2b8571bbd6 100644
--- a/libswresample/resample.c
+++ b/libswresample/resample.c
@@ -27,7 +27,7 @@
  */
 
 #include "libavutil/avassert.h"
-#include "libavutil/cpu.h"
+#include "libavutil/mem.h"
 #include "resample.h"
 
 /**
diff --git a/libswresample/swresample.c b/libswresample/swresample.c
index 71b07d69bc..0a4e216f9b 100644
--- a/libswresample/swresample.c
+++ b/libswresample/swresample.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "swresample_internal.h"
 #include "audioconvert.h"
diff --git a/libswresample/x86/rematrix_init.c b/libswresample/x86/rematrix_init.c
index ce794eef90..623e154f5d 100644
--- a/libswresample/x86/rematrix_init.c
+++ b/libswresample/x86/rematrix_init.c
@@ -19,6 +19,7 @@
  */
 
 #include "libavutil/attributes.h"
+#include "libavutil/mem.h"
 #include "libavutil/x86/cpu.h"
 #include "libswresample/swresample_internal.h"
 
diff --git a/libswscale/gamma.c b/libswscale/gamma.c
index d7470cb1c9..7c5534e1b7 100644
--- a/libswscale/gamma.c
+++ b/libswscale/gamma.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "swscale_internal.h"
 
 typedef struct GammaContext
diff --git a/libswscale/hscale.c b/libswscale/hscale.c
index 6789ce7540..5a949732bf 100644
--- a/libswscale/hscale.c
+++ b/libswscale/hscale.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "swscale_internal.h"
 
 /// Scaler instance data
diff --git a/libswscale/slice.c b/libswscale/slice.c
index db1c696727..1cc3f6c405 100644
--- a/libswscale/slice.c
+++ b/libswscale/slice.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/mem.h"
 #include "swscale_internal.h"
 
 static void free_lines(SwsSlice *s)
diff --git a/libswscale/swscale.c b/libswscale/swscale.c
index fe0e74f871..2795429b6c 100644
--- a/libswscale/swscale.c
+++ b/libswscale/swscale.c
@@ -28,6 +28,7 @@
 #include "libavutil/cpu.h"
 #include "libavutil/emms.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/pixdesc.h"
 #include "config.h"
diff --git a/libswscale/utils.c b/libswscale/utils.c
index ab8a68e241..d88fd731f4 100644
--- a/libswscale/utils.c
+++ b/libswscale/utils.c
@@ -45,6 +45,7 @@
 #include "libavutil/intreadwrite.h"
 #include "libavutil/libm.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/slicethread.h"
diff --git a/libswscale/vscale.c b/libswscale/vscale.c
index 9216112b87..5b3234198d 100644
--- a/libswscale/vscale.c
+++ b/libswscale/vscale.c
@@ -17,6 +17,7 @@
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
+#include "libavutil/mem.h"
 #include "swscale_internal.h"
 
 typedef struct VScalerContext
diff --git a/libswscale/yuv2rgb.c b/libswscale/yuv2rgb.c
index 0a84b662f9..1e82649798 100644
--- a/libswscale/yuv2rgb.c
+++ b/libswscale/yuv2rgb.c
@@ -28,6 +28,7 @@
 #include <inttypes.h>
 
 #include "libavutil/bswap.h"
+#include "libavutil/mem.h"
 #include "config.h"
 #include "rgb2rgb.h"
 #include "swscale.h"
diff --git a/tests/api/api-band-test.c b/tests/api/api-band-test.c
index b00e2927a9..bbaa6edb58 100644
--- a/tests/api/api-band-test.c
+++ b/tests/api/api-band-test.c
@@ -25,6 +25,7 @@
  */
 
 #include "libavutil/adler32.h"
+#include "libavutil/mem.h"
 #include "libavcodec/avcodec.h"
 #include "libavformat/avformat.h"
 #include "libavutil/imgutils.h"
diff --git a/tests/api/api-flac-test.c b/tests/api/api-flac-test.c
index d1e45acda7..e65e261b98 100644
--- a/tests/api/api-flac-test.c
+++ b/tests/api/api-flac-test.c
@@ -30,6 +30,7 @@
 #include "libavcodec/avcodec.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
+#include "libavutil/mem.h"
 #include "libavutil/samplefmt.h"
 
 #define NUMBER_OF_AUDIO_FRAMES 200
diff --git a/tests/api/api-h264-slice-test.c b/tests/api/api-h264-slice-test.c
index b7aa405b0d..f4a9c865c4 100644
--- a/tests/api/api-h264-slice-test.c
+++ b/tests/api/api-h264-slice-test.c
@@ -39,6 +39,7 @@
 #include <fcntl.h>
 
 #include "libavcodec/avcodec.h"
+#include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/hash.h"
 #include "libavutil/bswap.h"
diff --git a/tests/api/api-h264-test.c b/tests/api/api-h264-test.c
index c89f1ba41a..e1e8803c1e 100644
--- a/tests/api/api-h264-test.c
+++ b/tests/api/api-h264-test.c
@@ -25,6 +25,7 @@
  */
 
 #include "libavutil/adler32.h"
+#include "libavutil/mem.h"
 #include "libavcodec/avcodec.h"
 #include "libavformat/avformat.h"
 #include "libavutil/imgutils.h"
diff --git a/tests/api/api-seek-test.c b/tests/api/api-seek-test.c
index e86908e285..1bf9a1bde7 100644
--- a/tests/api/api-seek-test.c
+++ b/tests/api/api-seek-test.c
@@ -25,6 +25,7 @@
  */
 
 #include "libavutil/adler32.h"
+#include "libavutil/mem.h"
 #include "libavcodec/avcodec.h"
 #include "libavformat/avformat.h"
 #include "libavutil/imgutils.h"
diff --git a/tests/api/api-threadmessage-test.c b/tests/api/api-threadmessage-test.c
index c96b473c43..b9a4bdca16 100644
--- a/tests/api/api-threadmessage-test.c
+++ b/tests/api/api-threadmessage-test.c
@@ -25,6 +25,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 #include "libavutil/frame.h"
+#include "libavutil/mem.h"
 #include "libavutil/threadmessage.h"
 #include "libavutil/thread.h" // not public
 
diff --git a/tests/checkasm/av_tx.c b/tests/checkasm/av_tx.c
index aa8fc6b4e9..2d7b4e662d 100644
--- a/tests/checkasm/av_tx.c
+++ b/tests/checkasm/av_tx.c
@@ -16,6 +16,7 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 #include "libavutil/tx.h"
 #include "libavutil/error.h"
diff --git a/tests/checkasm/float_dsp.c b/tests/checkasm/float_dsp.c
index a025117b6b..cadfa65e2a 100644
--- a/tests/checkasm/float_dsp.c
+++ b/tests/checkasm/float_dsp.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/float_dsp.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #include "checkasm.h"
diff --git a/tests/checkasm/sw_scale.c b/tests/checkasm/sw_scale.c
index 1305b38744..32a5d1c1ac 100644
--- a/tests/checkasm/sw_scale.c
+++ b/tests/checkasm/sw_scale.c
@@ -21,6 +21,7 @@
 
 #include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #include "libswscale/swscale.h"
diff --git a/tests/checkasm/vc1dsp.c b/tests/checkasm/vc1dsp.c
index 570785776f..8ad2b51acf 100644
--- a/tests/checkasm/vc1dsp.c
+++ b/tests/checkasm/vc1dsp.c
@@ -27,6 +27,7 @@
 #include "libavutil/common.h"
 #include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/mem_internal.h"
 
 #define VC1DSP_TEST(func) { #func, offsetof(VC1DSPContext, func) },
diff --git a/tests/checkasm/vf_blend.c b/tests/checkasm/vf_blend.c
index 484ed0b1d8..b5a96ee4bc 100644
--- a/tests/checkasm/vf_blend.c
+++ b/tests/checkasm/vf_blend.c
@@ -24,6 +24,7 @@
 #include "libavutil/common.h"
 #include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 
 #define WIDTH 256
 #define HEIGHT 256
diff --git a/tests/checkasm/vf_gblur.c b/tests/checkasm/vf_gblur.c
index 3686a6c9f6..1f8b6c83b9 100644
--- a/tests/checkasm/vf_gblur.c
+++ b/tests/checkasm/vf_gblur.c
@@ -19,6 +19,7 @@
 #include <float.h>
 #include <string.h>
 #include "checkasm.h"
+#include "libavutil/mem.h"
 #include "libavfilter/vf_gblur_init.h"
 
 #define WIDTH 256
diff --git a/tests/checkasm/vf_nlmeans.c b/tests/checkasm/vf_nlmeans.c
index 0f1f9fd403..e61a2efae6 100644
--- a/tests/checkasm/vf_nlmeans.c
+++ b/tests/checkasm/vf_nlmeans.c
@@ -21,6 +21,7 @@
 #include "checkasm.h"
 #include "libavfilter/vf_nlmeans_init.h"
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 
 #define randomize_buffer(buf, size) do {    \
     int i;                                  \
diff --git a/tools/crypto_bench.c b/tools/crypto_bench.c
index 0aff4ea784..45046a0a90 100644
--- a/tools/crypto_bench.c
+++ b/tools/crypto_bench.c
@@ -32,6 +32,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/crc.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/timer.h"
 
 #ifndef AV_READ_TIME
diff --git a/tools/enc_recon_frame_test.c b/tools/enc_recon_frame_test.c
index a8e152bf44..c099beb3f4 100644
--- a/tools/enc_recon_frame_test.c
+++ b/tools/enc_recon_frame_test.c
@@ -32,6 +32,7 @@
 #include "libavutil/error.h"
 #include "libavutil/frame.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "libavformat/avformat.h"
diff --git a/tools/ismindex.c b/tools/ismindex.c
index 39b5378190..6fe68d8a79 100644
--- a/tools/ismindex.c
+++ b/tools/ismindex.c
@@ -52,6 +52,7 @@
 #include "libavformat/os_support.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 
 static int usage(const char *argv0, int ret)
 {
diff --git a/tools/probetest.c b/tools/probetest.c
index e742d598b1..a8ce404792 100644
--- a/tools/probetest.c
+++ b/tools/probetest.c
@@ -24,6 +24,7 @@
 #include "libavformat/demux.h"
 #include "libavcodec/put_bits.h"
 #include "libavutil/lfg.h"
+#include "libavutil/mem.h"
 #include "libavutil/timer.h"
 
 #define MAX_FORMATS 1000 //this must be larger than the number of formats
diff --git a/tools/sidxindex.c b/tools/sidxindex.c
index 88aea6bcb9..3e43d95e02 100644
--- a/tools/sidxindex.c
+++ b/tools/sidxindex.c
@@ -25,6 +25,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
 
 static int usage(const char *argv0, int ret)
 {
diff --git a/tools/target_bsf_fuzzer.c b/tools/target_bsf_fuzzer.c
index 76e3b89cf1..44a4d1467d 100644
--- a/tools/target_bsf_fuzzer.c
+++ b/tools/target_bsf_fuzzer.c
@@ -18,6 +18,7 @@
 
 #include "config.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "libavcodec/avcodec.h"
diff --git a/tools/target_dec_fuzzer.c b/tools/target_dec_fuzzer.c
index 57ba24a308..d403e5060d 100644
--- a/tools/target_dec_fuzzer.c
+++ b/tools/target_dec_fuzzer.c
@@ -51,6 +51,7 @@
 #include "libavutil/cpu.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 
 #include "libavcodec/avcodec.h"
 #include "libavcodec/bytestream.h"
diff --git a/tools/target_dem_fuzzer.c b/tools/target_dem_fuzzer.c
index fe69eb9be0..8e96fad7f8 100644
--- a/tools/target_dem_fuzzer.c
+++ b/tools/target_dem_fuzzer.c
@@ -19,6 +19,7 @@
 #include "config.h"
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
+#include "libavutil/mem.h"
 
 #include "libavcodec/avcodec.h"
 #include "libavcodec/bytestream.h"
diff --git a/tools/target_sws_fuzzer.c b/tools/target_sws_fuzzer.c
index 6d9d15da25..fd8314cae1 100644
--- a/tools/target_sws_fuzzer.c
+++ b/tools/target_sws_fuzzer.c
@@ -24,6 +24,7 @@
 #include "libavutil/cpu.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
 #include "libavcodec/bytestream.h"
diff --git a/tools/uncoded_frame.c b/tools/uncoded_frame.c
index edea2367c4..447bfc8b0d 100644
--- a/tools/uncoded_frame.c
+++ b/tools/uncoded_frame.c
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include "libavutil/avassert.h"
+#include "libavutil/mem.h"
 #include "libavdevice/avdevice.h"
 #include "libavfilter/avfilter.h"
 #include "libavfilter/buffersink.h"
