mpg module MPEG1 Video decoder, MP2 Audio decoder

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
idle
Always Here
Always Here
Posts: 5870
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

mpg module MPEG1 Video decoder, MP2 Audio decoder

Post by idle »

MPEG1 Video decoder, MP2 Audio decoder to play video on any PB 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

currently windows x64, should work on linux and mac if you compile the lib

Licence MIT
note Mpeg1 and MP2 are patent free, all claims have ceased since 2017 when Mp3 expired.

v 0.4
red and blue where reversed on windows
changed how to flip image vertically on windows
v 0.3
changed to mono for 3D sound
added draw to canvas gadget test
it will likely need fix ups for linux mac as windows draws upside down canvas should be ok
sprites might not
v 0.2
changed to use native sound functions so should be cross platform now
but you will need to compile pl_mpeg.c with gcc or clang like
clang -O2 -c pl_mpeg.c

To do:
add in additional functions as required

it's very light weight at 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  
  
#CameraSpeed = 4

Define.f KeyX, KeyY, MouseX, MouseY
Define.i mpg,sound,w,h

InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()

ExamineDesktops():dx=DesktopWidth(0)*0.8:dy=DesktopHeight(0)*0.8
OpenWindow(0, 0,0, DesktopUnscaledX(dx),DesktopUnscaledY(dy), " Sound3D - [Esc] quit",#PB_Window_ScreenCentered | #PB_Window_Invisible)
OpenWindowedScreen(WindowID(0), 0, 0, dx, dy, 0, 0, 0)

Add3DArchive(#PB_Compiler_Home + "examples/3d/Data", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/desert.zip", #PB_3DArchive_Zip)

;-load movie with 3D sound 
mpg = MPG_LoadFile("bjork-all-is-full-of-love.mpg",#MPG_SOUND3D); Load MPG file set it to 3D sound mode   
HideWindow(0,0) 

Parse3DScripts()

;- Mesh
CreateCube(0, 100)
CreateSphere(1, 50)

CreateMaterial(0, #PB_Material_None)
GetScriptMaterial(1, "Color/Red")

;- Entity
CreateEntity(0, MeshID(0), MaterialID(0))
CreateEntity(1, MeshID(1), MaterialID(1))

;- Get the movie Sound
sound = MPG_GetSound(mpg)
If mpg
  SoundVolume3D(sound, 100)
  SoundRange3D(sound, 1, 500)
EndIf

If LoadSound3D(1, "Siren.ogg")
  SoundVolume3D(1, 50)
  SoundRange3D(1, 1, 500)
  PlaySound3D(1, #PB_Sound3D_Loop)
EndIf

;- Node
; Create a node, so we can link the entity and the sound
CreateNode(0, -400, 0, 0)
AttachNodeObject(0, SoundID3D(sound))
AttachNodeObject(0, EntityID(0))

; Create a node, so we can link the entity and the sound
CreateNode(1, 400, 0, 0)
AttachNodeObject(1, SoundID3D(1))
AttachNodeObject(1, EntityID(1))

;- Camera
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, -550, 70, 10, #PB_Absolute)
CameraLookAt(0, NodeX(1), NodeY(1), NodeZ(1))

;- Light
AmbientColor(0)
CreateLight(0, RGB(255, 255, 255), 0, 700, 0)

;- Sky
SkyBox("desert07.jpg")

;-Play movie 
MPG_Play(MPG) ;Start movie playback


Repeat
  While WindowEvent():Wend
  
  If ExamineMouse()
    MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
    MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
  EndIf
  
  If ExamineKeyboard()
    
    If KeyboardPushed(#PB_Key_Left)
      KeyX = -#CameraSpeed
    ElseIf KeyboardPushed(#PB_Key_Right)
      KeyX = #CameraSpeed
    Else
      KeyX = 0
    EndIf
    
    If KeyboardPushed(#PB_Key_Up)
      KeyY = -#CameraSpeed
    ElseIf KeyboardPushed(#PB_Key_Down)
      KeyY = #CameraSpeed
    Else
      KeyY = 0
    EndIf
    
  EndIf
  ;-Get frame as material 
   mat = MPG_GetMaterial(mpg)   
   If mat 
     SetMaterialColor(mat,#PB_Material_SelfIlluminationColor,RGB(255,255,255))
     SetEntityMaterial(0,MaterialID(Mat)) ;Set material to cube
   EndIf 
     
   RotateEntity(0, 0, 0.3, 0, #PB_Relative)
  
  
  RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
  MoveCamera  (0, KeyX, 0, KeyY)
  
  SoundListenerLocate(CameraX(0), CameraY(0), CameraZ(0)) ; The 'ear' follows the camera
  
  RenderWorld()
  
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1

 MPG_Free(mpg) ;free mpg 

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.
miso
Enthusiast
Enthusiast
Posts: 459
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Post by miso »

Thank you Idle, this one is great. Performed well with and without avx. (Old and new CPU)
User avatar
idle
Always Here
Always Here
Posts: 5870
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Post 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.
jamirokwai
Enthusiast
Enthusiast
Posts: 798
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Post 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 :-)
Regards,
JamiroKwai
User avatar
idle
Always Here
Always Here
Posts: 5870
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Post 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.
jamirokwai
Enthusiast
Enthusiast
Posts: 798
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Post 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.
Regards,
JamiroKwai
miso
Enthusiast
Enthusiast
Posts: 459
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Post 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.)
User avatar
idle
Always Here
Always Here
Posts: 5870
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Post 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.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 599
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Post 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!
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
idle
Always Here
Always Here
Posts: 5870
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Post 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
Fred
Administrator
Administrator
Posts: 18169
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Post by Fred »

Works perfectly here as well, good job ! :)
User avatar
idle
Always Here
Always Here
Posts: 5870
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Post by idle »

@Fred
Thanks, it's a cool little lib

Changed to decode sound to memory with catchsound and loadsound3D
if you can compile the pl_mpeg file with clang and test on mac and linux it should work now

compile as
clang -O2 -c pl_mpeg.c

you can get the sound number back via MPG_GetSound(Mpg)

https://atomicwebserver.com/MPG.zip
User avatar
idle
Always Here
Always Here
Posts: 5870
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Post by idle »

small fix, missed that 3Dsounds need to be mono! :oops:
see 3dtest.pb

added draw to canvas test with MPG_GetImage(mpg)

https://atomicwebserver.com/MPG.zip
User avatar
idle
Always Here
Always Here
Posts: 5870
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: mpg module MPEG1 Video decoder, MP2 Audio decoder

Post by idle »

This seems to be correct now for windows.
Sprite and textures needed flipping vertically so am doing it via a dib section and image
Also colors where reversed though I actually preferred the warmth of the red vs blue

https://atomicwebserver.com/MPG.zip

oddly the canvas demo uses less memory and cpu than the windowscreen version. I didn't expect that!
also it seeks better than windows media player! :lol:
Post Reply