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)

Billboard image

The fastest way to detect downtimes

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitoring.

Get started now

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay