pl_mpeg.h and decoding question

Mac OSX specific forum
jamirokwai
Enthusiast
Enthusiast
Posts: 798
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

pl_mpeg.h and decoding question

Post by jamirokwai »

Edit: Idle works on sound with MiniAudio. That would be nice! See viewtopic.php?t=87321

---

Hi there,

I am trying to use pl_mpeg.h in a Purebasic-program.

You can find the source here: https://github.com/phoboslab/pl_mpeg/tree/master

I was able to compile a .a for macOS with

Code: Select all

 gcc -c -arch arm64 pl_mpeg.c -o pl_mpeg.o
and

Code: Select all

ar rcs libplmpeg.a pl_mpeg.o
using this intermediate c-snippet called pl_mpeg.c:

Code: Select all

#ifdef __cplusplus
extern "C" {
#endif

#define PL_MPEG_IMPLEMENTATION
#include "pl_mpeg.h"

#ifdef __cplusplus
}
#endif
Then, I tried to load the video from here: https://filesamples.com/formats/mpeg -> I got the second one, but anyone should work.

So, this source will tell me the width and height, but will crash, when trying to decode a frame...

Any help appreciated, and thanks for any hint :-)

Code: Select all

ImportC "libplmpeg.a"
  plm_create_with_filename(FileName.p-utf8)
  plm_decode(handle)
  plm_destroy(handle)
  plm_video_decode(handle)
  plm_get_width(handle)
  plm_get_height(handle)
  plm_frame_to_rgb(frame, buffer) ; convert YUV to RGB
  plm_get_num_video_streams(handle)
  plm_get_video_enabled(handle)
  plm_set_video_enabled(handle, enabled)
  plm_set_video_decode_callback(handle, callback, user)
  plm_set_audio_enabled(handle, enabled)  
EndImport

EnableExplicit

Structure plm_frame_t
  width.l
  height.l
  y_stride.l
  cr_stride.l
  cb_stride.l
  *y
  *cr
  *cb
EndStructure

Define mpeg, width, height
Define filename.s = "video.mpg"
Define *frame

mpeg = plm_create_with_filename(filename)

If mpeg
  width  = plm_get_width(mpeg)
  height = plm_get_height(mpeg)
  
  Debug "Video-Resolution: " + Str(width) + "x" + Str(height)
  
  While plm_decode(mpeg)
    *frame = plm_video_decode(mpeg)  ; CRASH
    If *frame
      Debug "Frame decoded!"
      Break
    EndIf
  Wend
  
  plm_destroy(mpeg)
Else
  Debug "Error loading mpeg"
EndIf
Last edited by jamirokwai on Tue Aug 19, 2025 9:27 am, edited 1 time in total.
Regards,
JamiroKwai
infratec
Always Here
Always Here
Posts: 7598
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: pl_mpeg.h and decoding question

Post by infratec »

Shouldn't the loop look like this:

Code: Select all

Debug "Video-Resolution: " + Str(width) + "x" + Str(height)

*frame = plm_decode_video(plm)
While *frame
  i + 1
  Debug "Frame " + Str(i) + " decoded"
  *frame = plm_decode_video(plm)
Wend
Without plm_decode(mpeg)
jamirokwai
Enthusiast
Enthusiast
Posts: 798
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: pl_mpeg.h and decoding question

Post by jamirokwai »

infratec wrote: Wed Jan 29, 2025 10:19 pm Shouldn't the loop look like this:

Code: Select all

Debug "Video-Resolution: " + Str(width) + "x" + Str(height)

*frame = plm_decode_video(plm)
While *frame
  i + 1
  Debug "Frame " + Str(i) + " decoded"
  *frame = plm_decode_video(plm)
Wend
Without plm_decode(mpeg)
Oh, no!!! You are (partly) right. My bad
It's plm_decode_video not plm_video_decode...

Thanks for that :-)
Regards,
JamiroKwai
jamirokwai
Enthusiast
Enthusiast
Posts: 798
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: pl_mpeg.h and decoding question

Post by jamirokwai »

Hi all!

I almost made it! Have a look at the repo under https://github.com/foodsnacker/pb_play_mpeg.
The source can decode an mpeg1 and play the video-part - unelegantly using an ImageGadget, but it proves the point.
You can export the single frames as images as well and export the audio as raw stereo, 32 bit float. Should be easy to save it as .wav.

Now I have to figure out how to play the audio in sync.

Thanks, Infratec, for the hint about the function-name!
Regards,
JamiroKwai
jamirokwai
Enthusiast
Enthusiast
Posts: 798
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: pl_mpeg.h and decoding question

Post by jamirokwai »

Maybe someone has information on how to access the raw audio-data from a wav-file "loadsound-ed" or "catchsound-ed" with Purebasics Miniaudio?

I figured out how to apply a ring buffer (or circular buffer). But, I would most certainly need access to the raw sound-data.
Regards,
JamiroKwai
Post Reply