DEV Community

Murage Kibicho
Murage Kibicho

Posted on

FFMPEG C Data Structures Memory Leak Cheatsheet

FFMPEG C API Memory Leak Cheatsheet

Every malloc has an equal and opposite free ~ Newton's fourth law of Physics.

Hello guys,
My name is Murage Kibicho and this is a quick guide to freeing memory when using the FFmpeg C api.
I list the data structure, how to allocate memory and how to free memory. I also write about making a video player in less than 1000 lines of C here

AVFormatContext

  • Allocate avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)
  • Free avformat_close_input(AVFormatContext **s)

AVFrame

  • Allocate av_frame_alloc()
  • Dereference buffers av_frame_unref (AVFrame *frame)
  • Free and leave pointer null av_freep(void *ptr)
  • Regular Free av_free(void *ptr)

AVPacket

  • Allocate av_packet_alloc()
  • Dereference buffers av_packet_unref(AVPacket *pkt)
  • Free av_packet_free(AVPacket **pkt)
  • NOTE av_free_packet is deprecated. NEVER use.

AVCodecContext

  • Allocate avcodec_alloc_context3(const AVCodec *codec)
  • Free avcodec_free_context (AVCodecContext **avctx)

struct SwsContext

  • Allocate sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, enum AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
  • Free sws_freeContext(struct SwsContext *swsContext)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay