Page 1 of 1

pl_mpeg.h and decoding question

Posted: Wed Jan 29, 2025 5:58 pm
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

Re: pl_mpeg.h and decoding question

Posted: Wed Jan 29, 2025 10:19 pm
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)

Re: pl_mpeg.h and decoding question

Posted: Thu Jan 30, 2025 5:23 am
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 :-)

Re: pl_mpeg.h and decoding question

Posted: Thu Jan 30, 2025 7:33 am
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!

Re: pl_mpeg.h and decoding question

Posted: Thu Jan 30, 2025 2:38 pm
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.