Single file C library for decoding MPEG1 Video and MP2 Audio

Related tags

Audio pl_mpeg

PL_MPEG - MPEG1 Video decoder, MP2 Audio decoder, MPEG-PS demuxer

Single-file MIT licensed library for C/C++

See pl_mpeg.h for the documentation.


This is meant as a simple way to get video playback into your app or game. Other solutions, such as ffmpeg require huge libraries and a lot of glue code.

MPEG1 is an old and inefficient codec, but it's still good enough for many use cases. All patents related to MPEG1 and MP2 have expired, so it's completely free now.

This library does not make use of any SIMD instructions, but because of the relative simplicity of the codec it still manages to decode 4k60fps video on a single CPU core (on my i7-6700k at least).

Example Usage

Encoding for PL_MPEG

Most MPEG-PS (.mpg) files containing MPEG1 Video ("mpeg1") and MPEG1 Audio Layer II ("mp2") streams should work with PL_MPEG. Note that .mpg files can also contain MPEG2 Video, which is not supported by this library.

You can encode video in a suitable format using ffmpeg:

ffmpeg -i input.mp4 -c:v mpeg1video -c:a mp2 -format mpeg output.mpg

If you just want to quickly test the library, try this file:


  • no error reporting. PL_MPEG will silently ignore any invalid data.
  • the pts (presentation time stamp) for packets in the MPEG-PS container is ignored. This may cause sync issues with some files.
  • bugs, probably.
  • Corrupt decoding :(

    Corrupt decoding :(

    Hi, I was looking forward to using this in a m.a.m.e. project but for some reason it doesn't correctly decode the mpg files I've got. The files are OK, they play with vlc and all other players I've tried including a hardware decoder :( The decoded data is wrong from frame one :( I've absolutely no idea how to fault finding on this :( I've many files all with similar problems. This is the first frame from a file here. 000001

    Anyone any suggestions or ideas ? Any help is really appreciated. Thanks Paul

    opened by Paul-Arnold 14
  • Any chance of a seek API?

    Any chance of a seek API?

    I'm using this library with raylib to play MPEG files, it works flawlessly! In under 150 lines of C I got a nice video player!

    Just wondering if there is any plan to add a frame/sample seek API. It would be really great.

    (I can keep decoding frames/samples to implement a seek mechanism but it seems a bit inefficient)

    EDIT: Just created a function to get video length but it's extremely inefficient, it takes up to 10 seconds on my system. I can probably execute it in a second thread while starting playing the video in main thread... but... any better idea to get video length?

    int plm_get_total_frames(const char *filename)
        int total_frames = 0;
        plm_t *plm = plm_create_with_filename(filename);
        if (plm != NULL)
            plm_frame_t *frame = plm_decode_video(plm);
            while (frame != NULL)
                frame = plm_decode_video(plm);
        return total_frames;
    opened by raysan5 9
  • Decode slice macroblock loop condition may be incorrect

    Decode slice macroblock loop condition may be incorrect

    When looping to decode macroblocks you call plm_buffer_no_start_code, which is an aligned check for no start code. I think this can lose undecoded bits in the stream. In the spec it seems they do an unaligned bitcheck for 23 0's. I believe if there were no more macroblocks and the stream wasn't aligned it would be padded with 0's. What do you think?



    opened by matsondawson 3
  • Green rectangles when decoding

    Green rectangles when decoding

    I'm porting pl_mpeg_player.c to GLFW and it decodes smoothly and accurately, except for these rectangles which appear in the areas of the video which are updating. (The colors are accurate where there is no movement.)

    I'm hoping that someone knows at a glance what I'm doing wrong. Is there a step that, when skipped, results in these artifacts? The same artifacting appears regardless of whether I'm using the three-texture YCRCB approach or the one-texture RGB approach; they work equally well and give the same result.

    I'd like to thank the creator for his hard work; this is a fantastic library with a ton of utility for creators such as myself. 2021-09-24 15_51_41-WIP 2021-09-24 17_19_45-WIP

    opened by ric-a-tic 2
  • Update


    make large (3X 4X) video files which retain quality. I think this is what most people would want. When I tried this command without this option (-q:v 0) the results were very low quality.

    opened by nicolas42 2
  • Fix memory issue of mode `PLM_BUFFER_MODE_RING`

    Fix memory issue of mode `PLM_BUFFER_MODE_RING`

    video_buffer keep calling realloc() and keep expanding its memory usage:

    In plm_buffer_has_start_code(), the discard_read_bytes is temporarily set to FALSE.

    But when it goes all the way to plm_buffer_has(), it will trigger the the load_callback(), which is set to plm_read_video_packet() in plm_init_decoders().

    Then plm_buffer_write() will eventually be called when discard_read_bytes is set to FALSE. Finally it causes the video_buffer to keep expanding.

    opened by CW-B-W 1
  • added proper slice guarding

    added proper slice guarding

    What is happening is that sometimes a slice boundary is blown through because the ISO spec allows for a bit of extra '0' padding at the end, but the current version of PL_MPEG is not quite handling this right. I made a patch which guards the slice boundary based on the known last macroblock address for that specific slice. Tested with several DOSBox ReelMagic MPEG assets and the MPG file posted in this ticket:

    opened by jrdennisoss 1
  • Corrupted video

    Corrupted video

    When playing video sometimes I get corrupt blocks on the right hand side. When played in VLC media player it is not corrupted.

    See example here: image

    Source file here:

    Frame number 980 I wonder if it's something to do with certain motion blocks on the right hand side?

    opened by matsondawson 1
  • Duplicate lines in plm_buffer_has method

    Duplicate lines in plm_buffer_has method

    There seem to be some duplicate lines in plm_buffer_has method:

    int plm_buffer_has(plm_buffer_t *self, size_t count) {
    	if (((self->length << 3) - self->bit_index) >= count) {
    		return TRUE;
    	if (self->load_callback) {
    		self->load_callback(self, self->load_callback_user_data);
    	if (((self->length << 3) - self->bit_index) >= count) {
    		return TRUE;
    	if (self->total_size != 0 && self->length == self->total_size) {
    		self->has_ended = TRUE;
    	return FALSE;
    opened by yasoob 1
  • Segfault found by AFL

    Segfault found by AFL

    For fun I ran american fuzzy lop against this. Here's an MPEG that segfaults when pl_mpeg_extract_frames is run on it. Looks like a wild pointer dereference.


    opened by pcwalton 1
  • Fix pl_mpeg_player.c not compiling on macOS

    Fix pl_mpeg_player.c not compiling on macOS

    This pull request removes the SDL_opengl.h include for macOS, since it prevents pl_mpeg_player.c from compiling and isn't needed anyway (SDL's OpenGL context creation/loading functions aren't declared in that header).

    Currently, it is included before the system OpenGL headers, which disables them via header guards. Since SDL_opengl.h only declares the legacy OpenGL 1.3 API, pl_mpeg_player.c can't be compiled due to missing API functions.

    Moving the include to be after the system GL headers works, but makes it a no-op: the system headers define __gl_h, which causes the majority of SDL_opengl.h to not be included. (this is probably the case with other platforms as well)

    opened by sridenour 0
  • Made it possible to compile it in Windows when using MinGW via MSYS2

    Made it possible to compile it in Windows when using MinGW via MSYS2

    The compiled exe works but it is not statically linked. I used the following command to compile it:

    gcc pl_mpeg_player.c -o pl_mpeg_player.exe -mconsole -lglew32 -lopengl32 -lsdl2

    opened by Zero3K 0
  • Corrupted video on GBA

    Corrupted video on GBA

    Hello, great library. I am attempting to play a video on the GBA the library compiles fine with devkitpro. However everything decoded is corrupted. For example, my first frame should be completely black. But when grabbing a frame with the library on the gameboy advance it has randomish characters like 7e 6e 0f 0f 00 44 44 FF FF FF instead of being 0's. I can send the project to an email or upload here if interested. It would be awesome if you were. I did verify the same code on windows works. So I know it's GBA specific just not sure what.


    opened by interdpth 3
  • (Minor suggestion) Compiler warnings

    (Minor suggestion) Compiler warnings

    Lines 1187 and 1872 involve an assignment within a conditional. Some compilers complain about this. Easy fix: explicitly adding "!= 0" in each conditional appears to work equivalently and silences the warnings.

    opened by ric-a-tic 0
  • pl_mpeg + physfs = possible?

    pl_mpeg + physfs = possible?

    Hi, I'm looking around for a video playback solution and considering pl_mpeg. Can it be made to work with physfs?? I'm currently using physfs to load resources from a zip archive, so I need to be able to also play the videos from this archive as well. At present I don't see a way to easily do it. Maybe I've overlooked something, please advise.



    opened by jarroddavis68 3
  • Custom malloc/realloc/free and disable stdlib

    Custom malloc/realloc/free and disable stdlib

    Sometimes we don't have access to the standard library, and/or we want to use custom allocation functions. Thus, it would make sense to add in some defines that can be set by the user of the library before the header is included. These would define memory allocation functions. There will also be a define that controls whether stdio is used, and also remove the *_create_with_file functions to prevent compilation errors. stb_image is an example implementation of this. Of course, it would be trivial for the user to manually modify the source code, and the license is permissive enough. However, it is better to have this be done in a well-defined manner, signed off by someone who has a more intimate knowledge of the code, and therefore is less likely to make mistakes than some impatient newbie running %s/malloc/my_malloc.

    opened by KeinR 0
Dominic Szablewski
Dominic Szablewski
Single file audio playback and capture library written in C.

A single file library for audio playback and capture. Example - Documentation - Supported Platforms - Backends - Major Features - Building - Unofficia

David Reid 2.4k Oct 2, 2022
PortAudio is a portable audio I/O library designed for cross-platform support of audio

PortAudio is a cross-platform, open-source C language library for real-time audio input and output.

PortAudio 707 Sep 27, 2022
Block all ads in vídeo, áudio, banner, anti-skip

NoAdSpotify Block spotify ad This is an updated and simplified version of the project: BlockTheSpot Last updated: 6th June 2021 Last tested version: 1

null 13 Apr 7, 2022
Example for transmit video + audio to tv via hackRF

Этот код может передавать звук и изображение на телевизор через hackRF В этом проекте использовался код из следующих репозиториев:

null 9 Sep 27, 2022
Audio File Library

Audio File Library

michael pruett 134 Aug 27, 2022
An audio mixer that supports various file formats for Simple Directmedia Layer.

An audio mixer that supports various file formats for Simple Directmedia Layer.

Simple Directmedia Layer 163 Sep 26, 2022
A simple CLI to extract & save artwork of a 🎵 music/audio file.

artwork-extractor A simple CLI to extract & save artwork of a ?? music/audio file. Usage Dependencies MediaInfoLib On Debian based distros, one may in

Hitesh Kumar Saini 5 Aug 4, 2021
Single file synth + demo song

P.S. is a music track written from scratch using the C programming language. All sounds and notes were entered manually without using any music progra

NightRadio 43 Aug 14, 2022
C++ library for audio and music analysis, description and synthesis, including Python bindings

Essentia Essentia is an open-source C++ library for audio analysis and audio-based music information retrieval released under the Affero GPL license.

Music Technology Group - Universitat Pompeu Fabra 2.2k Sep 29, 2022
A simple C++ library for reading and writing audio files.

AudioFile A simple header-only C++ library for reading and writing audio files. Current supported formats: WAV AIFF Author AudioFile is written and ma

Adam Stark 651 Sep 30, 2022
A C library for reading and writing sound files containing sampled audio data.

libsndfile libsndfile is a C library for reading and writing files containing sampled audio data. Authors The libsndfile project was originally develo

null 1k Sep 28, 2022
C library for cross-platform real-time audio input and output

libsoundio C library providing cross-platform audio input and output. The API is suitable for real-time software such as digital audio workstations as

Andrew Kelley 1.6k Oct 3, 2022
C++ Audio and Music DSP Library

_____ _____ ___ __ _ _____ __ __ __ ____ ____ / \\_ \\ \/ / |/ \| | | | \_ \/ \ | Y Y \/ /_ \> <| | Y Y \ | |_|

Mick Grierson 1.3k Oct 6, 2022
a library for audio and music analysis

aubio is a library to label music and sounds. It listens to audio signals and attempts to detect events. For instance, when a drum is hit, at which frequency is a note, or at what tempo is a rhythmic melody.

aubio 2.8k Oct 1, 2022
A simple and easy-to-use audio library based on miniaudio

raudio A simple and easy-to-use audio library based on miniaudio raudio forks from module to become an standalone library. Actually, it w

Ray 60 Sep 29, 2022
C library for audio noise reduction and other spectral effects

libspecbleach C library for audio noise reduction and other spectral effects Background This library is based on the algorithms that were used in nois

Luciano Dato 36 Sep 28, 2022
C++17 library for creating macOS Audio Server plugins.

libASPL Synopsis Instructions Versioning API reference Example driver Quick start Object model Types of setters Customization Thread and realtime safe

Victor Gaydov 28 Sep 28, 2022
A discord audio playback library in c++ with node bindings

Tokio This project is still WIP. C++ Discord library with node bindings focused on audio playback. The Core parts as networking with discord/audio dec

Liz3 2 Nov 17, 2021