Page 1 of 1

mpg module MPEG1 Video decoder, MP2 Audio decoder

Posted: Tue Aug 19, 2025 1:16 am
by idle
MPEG1 Video decoder, MP2 Audio decoder
Windows x64
Licence MIT
Mpeg1 and MP2 are patent free, all claims have ceased since 2017 when Mp3 expired.

To do:
Change audio to use miniaudio lib for cross platform support.
add in additional functions

The wrapper lets you play video on any surface via functions

MPG_GetSprite(Mpg)
MPG_GetTexture(mpg)
MPG_GetMaterial(mpg)
MPG_Image(mpg)

so you can render to Screen, Material, texture, image or window and canvas via image dynamically.
you should also be able to play multiple videos at once, though not tested

And it's very light weight 78kb
PL_MPEG - MPEG1 Video decoder, MP2 Audio decoder, MPEG-PS demuxer
https://github.com/phoboslab/pl_mpeg
if you want to compile the c file your self, it's as easy as
clang -O2 -c pl_mpeg.c

Download ;includes test video
https://atomicwebserver.com/MPG.zip

Draw video to cube example

Code: Select all

XIncludeFile "mpg.pbi" 
;compile thradsafe   
UseModule MPG  

InitEngine3D()
InitSprite()

Global st.d,et.d,lt.d 

Global mpg = MPG_LoadFile("bjork-all-is-full-of-love.mpg"); Load MPG file  

If MPG 
  Global w,h 
  
  w = MPG_GetWidth(mpg)  ;get video width
  h = MPG_GetHeight(mpg) 
  
  InitSprite()
  InitKeyboard()
  
  ExamineDesktops():dx=DesktopWidth(0)*0.8:dy=DesktopHeight(0)*0.8
  OpenWindow(0, 0,0, DesktopUnscaledX(dx),DesktopUnscaledY(dy), "mpeg",#PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  OpenWindowedScreen(WindowID(0), 0, 0, dx, dy, 0, 0, 0)
  
  CreateLight(#PB_Any, RGB(255, 255, 255),-5, 10, 5, #PB_Light_Point)
  CreateCamera(0, 0, 0, 100, 100)
  MoveCamera(0, 2, 1, 3, #PB_Absolute | #PB_Local)
  CameraLookAt(0, 0, 0, 0)
  CreateCube(0, 1.5)
  CreateEntity(0, MeshID(0), #PB_Material_None)
  
  textID = CreateTexture(#PB_Any, w, h)
  hdc = StartDrawing(TextureOutput(textid)) 
  If hdc 
    Box(0,0,w,h)
    StopDrawing()  
    MaterialID = CreateMaterial(#PB_Any,TextureID(textID))
    SetEntityMaterial(0,MaterialID(MaterialID))
  EndIf 
  
  MPG_Play(MPG) ;start the playback 
  
  Repeat
    While WindowEvent() : Wend  
    
    ExamineKeyboard() 
        
    mat = MPG_GetMaterial(mpg) ;Get material  
    If mat 
       SetEntityMaterial(0,MaterialID(Mat)) ;set material to cube
    EndIf 
    
    RotateEntity(0, 0, 0.3, 0, #PB_Relative)
    RenderWorld()
    FlipBuffers()
    
  Until KeyboardPushed(#PB_Key_Escape)
  MPG_Free(mpg) ;free mpg 
  
EndIf 

you can use ffmpeg to encode with
ffmpeg -i input.mp4 -c:v mpeg1video -q:v 0 -c:a libtwolame -b:a 224k -format mpeg output.mpg
-q:v sets a fixed video quality with a variable bitrate, where 0 is the highest. You may use -b:v to set a fixed bitrate instead; e.g. -b:v 2000k for 2000 kbit/s. Please refer to the ffmpeg documentation for more details.

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Posted: Tue Aug 19, 2025 1:25 am
by miso
Thank you Idle, this one is great. Performed well with and without avx. (Old and new CPU)

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Posted: Tue Aug 19, 2025 1:39 am
by idle
miso wrote: Tue Aug 19, 2025 1:25 am Thank you Idle, this one is great. Performed well with and without avx. (Old and new CPU)
Thanks, it was worth the effort and it might encourage Fred to add it or more :D

I will attempt to get it working with miniaudio, though I will have to do it in c and hope the PB lib has the required functions.
Also the transposing of frames might differ on linux and mac.

Hopefully our resident 3D guru will jump in and do the shaders.

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Posted: Tue Aug 19, 2025 9:26 am
by jamirokwai
Hi Idle,

great news!

My take on this is here: viewtopic.php?t=86166, but I never figured out how to play audio.

I tried your Code on MacOS, but "Line 118: Structure not found: WAVEHDR.". Hope you get it to work with MiniAudio :-)

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Posted: Tue Aug 19, 2025 10:03 am
by idle
jamirokwai wrote: Tue Aug 19, 2025 9:26 am Hi Idle,

great news!

My take on this is here: viewtopic.php?t=86166, but I never figured out how to play audio.

I tried your Code on MacOS, but "Line 118: Structure not found: WAVEHDR.". Hope you get it to work with MiniAudio :-)
I have a fix in mind. Infratec has a tip write a wav file and catchsound and it should work fine.

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Posted: Tue Aug 19, 2025 10:47 am
by jamirokwai
idle wrote: Tue Aug 19, 2025 10:03 am
jamirokwai wrote: Tue Aug 19, 2025 9:26 am Hi Idle,

great news!

My take on this is here: viewtopic.php?t=86166, but I never figured out how to play audio.

I tried your Code on MacOS, but "Line 118: Structure not found: WAVEHDR.". Hope you get it to work with MiniAudio :-)
I have a fix in mind. Infratec has a tip write a wav file and catchsound and it should work fine.
Catchsound may work, yes, I thought about that. But the sync may go out of hand somewhere in long videos.

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Posted: Tue Aug 19, 2025 10:55 am
by miso
You can get soundposition with pb, and can interpolate the video frame to it. I synced a sequence of images to soundposition, and works fine.
(though I don't know how the frames are created with mpeg1, so that might not be the solution.)

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Posted: Tue Aug 19, 2025 10:59 am
by idle
I tried it already and switched to the wav output and it actually sounded the same as I had a bug
So I will do it in the morning and I'm sure it'll work fine.

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Posted: Tue Aug 19, 2025 6:14 pm
by minimy
Hey idle, wow! we have video inside 3D engine!! Great job!
My mind is running again with a lot of ideas! :lol:
Thanks for share!

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Posted: Tue Aug 19, 2025 10:34 pm
by idle
minimy wrote: Tue Aug 19, 2025 6:14 pm Hey idle, wow! we have video inside 3D engine!! Great job!
My mind is running again with a lot of ideas! :lol:
Thanks for share!
I'm sure you'll find a good use for it
Just need to work out the sound now

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Posted: Wed Aug 20, 2025 8:41 am
by Fred
Works perfectly here as well, good job ! :)