* * *

Author Topic: Lazarus Mediaplayer with SDL2 and FFMPEG  (Read 516 times)


  • Newbie
  • Posts: 3
Lazarus Mediaplayer with SDL2 and FFMPEG
« on: March 12, 2018, 05:47:09 pm »
My Name is Reiner Sombrowsky (info@somby.de).
I am a german software developer and I developed many years with Delphi7
specially great database applications. Since 2 years ago I take
Lazarus for new development and I found it is the best what I could be found.
That's why I would like to say thank you to the developers.
You have done a very good job.

But I am interested to realize an video-/audio- or streamplayer
similar ffplay with the basic components of FFMPEG and SDL2 in Lazarus.
But I have tried my own considerations.
Perhaps we can talk in this forum about better solutions or to optimize it.

First I am looking for the basic components. Because my developing environment
is Windows 32bit I have dowloaded the FFMPEG-Windows-Builds

"ffmpeg-3.4.2-win32-shared.zip" from "https://ffmpeg.zeranoe.com/builds/win32/shared/"
"ffmpeg-3.4.2-win32-dev.zip" from "https://ffmpeg.zeranoe.com/builds/win32/dev/"

The developer-build I need it to compare the Header-Files.

I found in "http://www.delphiffmpeg.com/headers/" the pascal-header-files
"ffmpeg-delphi-pascal-headers-3.3.5.zip", and now I make my own pascal-files,
with updates to the current ffmpeg-header-files.
I compared the current header files of ffmpeg version 3.4.2 with the pascal-headers
of "ffmpeg-delphi-pascal-headers-3.3.5.zip". I uses only that functions, that I need
for the player. Because the units are not complete I named the .inc Files to the
units "min_....inc".

Now I was looking for SDL2. I downloaded the Runtime-Binaries
"SDL2-2.0.8-win32-x86.zip" from "https://www.libsdl.org/download-2.0.php"
"SDL2_ttf-2.0.14-win32-x86.zip" from "https://www.libsdl.org/projects/SDL_ttf/"
and I downloaded SDL2-pascal-headers from "https://github.com/ev1313/Pascal-SDL-2-Headers".
I found a little Error in the file sdlrenderer.inc marked with "// rso 03.02.2018".

No to the Considerations:
The basicly souces for documentation were:

I wanted to develop a control for Lazarus to play video, audiofiles and livestreams.
The base for the control was the TCustomControl-component.
Some comments to the current realized control.
After the "Play"-function was started correctly, a thread in the background
"ReadThread" reads the frames from the mediasource and stores these in
an audio-queue and a video-queue. If there is no place in the Queues the
thread is waiting (q.Put) so long if another thread or the mainthread set
Wakeup space.

If there is an AudioStream in the media  we start a SDL-callback AudioThread.

To synchronize Video and Audio I take the following selection:
- If we have an AudioStream we synchonize with the Audio-Timestamp
- If we have only a VideoStream or disable the Audio we syschronize with the
  current time.
We take a fixed sample rate to check for synchonizing (20ms you can change it).
A Timer invalidates every sample rate time a paint-procedure where we can show the
current Image from Videoframes. We dont calculate any delays etc., we take following

A): Synchronizing with Audio within the paint-procedure.
If the current VideoTimestamp is greater as the current AudioTimestamp we wait for the
next Videoframe. We dont need to present the renderer, because the last previos shown
Videoframe was copied to the renderer.
If the current Videotimestamp ist lower or equal as the current AudioTimestamp we
update a SDL-Texture with the decoded VideoFrame, copy the Texture to the Renderer
and do marking, that we have to present the renderer. We do it within a loop so long
if we don't have a VideoFrame whose Timestamp is lower or equal as the Audiotimestamp.
Because we have marked that we have to present the renderer, we present the renderer
if we leave the loop.
This is very efficient because we only present the Renderer after last frame.

B): Synchronizing with current Time.
We store at the beginning or after every "Resume" or "Seek" the current
VideoTimestamp in FVideoSyncPts and the current time in FVideSyncTime and then
we take the same algorithm as sychronizing with audio only we compare not
VideoTimestamp with AudioTimestamp, we compare the difference beetween the
current VideoTimeDifference (VideoTimestamp-FVideoSyncPts) and the
currenttimedifference (currenttime-FVideoSyncTime).

So you can tell me if you have good ideas.

Best regards
Reiner Sombrowsky


Today (13.03.2018) I take some updates.
The last update is sdl2_ffmpeg_player_001 from the same download
« Last Edit: March 13, 2018, 04:15:46 pm by somby »


  • Full Member
  • ***
  • Posts: 101
Re: Lazarus Mediaplayer with SDL2 and FFMPEG
« Reply #1 on: March 14, 2018, 01:52:44 pm »

nothing new for me, see:

FFPlay4Laz: http://forum.lazarus.freepascal.org/index.php/topic,26666.msg215455.html#msg215455

FFInfo4Laz: http://forum.lazarus.freepascal.org/index.php/topic,22038.msg208700.html#msg208700

Meanwhile I worked out a much more advanced Version of 'FFPlay4Laz', that
I use every Day for rendering VideoClips (-> very accurate AV-Sync and high Audioquality required !).
I'll publish it in this Forum within the next Weeks.

Refering to 'SDL2':
I know, that there are already FPC-Wrapper for it, but for the Moment I'm happy with SDL1, and
I wanted to wait, until SDL2 is implemented in Lazarus - Finally my Project is called FFPlay4Laz.

We take a fixed sample rate to check for synchonizing + We dont calculate any delays etc.
ffplay.c (and FFPlay4Laz) does it much more precise, see:
-> GoTo:
"To demonstrate the PictureQueue-Sync at Work, You'll find in the Attachment a ScreenShot of
  a typical Series of real Values for 'Current VideoPictures per Sec.', generated by my 'FFPlay4Laz'."

Pleased to have found someone, who got the same interests - after more than 3 Years.  :)
« Last Edit: March 14, 2018, 03:06:10 pm by metis »
Life could be so easy, if there weren't those f*** Details.


  • Newbie
  • Posts: 3
Re: Lazarus Mediaplayer with SDL2 and FFMPEG
« Reply #2 on: March 15, 2018, 06:47:31 pm »
Hi metis,
thank you for your first replay.
I only would like to show my idea to realize such a Video-/Audio-/Livestreamplayer.
I don't want to invent the world new.
It was very important to use the latest versions of FFMPEG and SDL2.
The comment:
"We take a fixed sample rate to check for synchonizing + We dont calculate any delays etc."
does not say that this method don't give very accurate AV-Sync and high Audioquality.
You see it if you consider my method  "Paint".
I can't find any sources of your "FFPlay4Laz" to compare.
But you wrote that you will publish in the next weeks.
My download has all sources of my component.

Best regards



  • Full Member
  • ***
  • Posts: 101
Re: Lazarus Mediaplayer with SDL2 and FFMPEG
« Reply #3 on: March 16, 2018, 02:07:36 pm »

I don't want to invent the world new.
I know - Anything else would be stupid. There are already plenty of Players written in C.

does not say...and high Audioquality.
For "high Audioquality", I use Portaudio -> faster, more responsive, better Sound.
This was not meant to be in the Context with Your AV-Sync-Method.

You see it if you consider my method  "Paint".
I did: Updating the VideoImage in fix Intervals is not the same like Updating it, when needed.
These Differences are marginal, but sometimes, it can be seen (with reactive Eyes).
That's the Reason, why I use specially VideoClips for Testing the Code.
I want to achieve, that the Movement of the Lips is totally synchronous to the Sound.
You won't see this - indeed - little Difference, e.g. with translated Movies.

I can't find any sources of your "FFPlay4Laz" to compare.
You didn't find it, because there isn't, and it's not intended, that there will be.
The Meaning of my Thread was (and is), to inspire FPC-People to deal with FFmpeg-DLLs, and
not only with the EXEs (-> "NON(!)-EXE FFPlay4Laz").
The Meaning of this is, to encourage People to find own Solutions - like You did, now.  ;)
But I will always help anybody in his efforts, and I've already published lots of Code-Parts.

But you wrote that you will publish in the next weeks.
This refers only to the current running Version - why, see above.
The Meaning of presenting only a working Example is to show, that yes it's possible to do it in FPC, too.
There are still too much People out there, who think, that Pascal is not a Language for Programmers !  :(

My download has all sources of my component.
I checked them...

I had a Look at the FFExample-Code, that is apparently the Base of Your Code:
This is, how 'ffplay.c' did it some Years ago - only Differences:
As an ExampleCode it's reduced to the Minimum (e.g. no Seek-Code, no SDL-Events for UserEntries),
the SDL-Threads are replaced with C-Threads, and the Code is updated to FFmpeg v3.2.2 and SDL2.
( This short Description is not for You; it's for those, who are not familiar with FFmpeg (yet?). )

I had a Look at Your Code:
- Your Units are in Delphi-Mode - Why, if we're talking about a "Lazarus Mediaplayer" ?

- Sorry, still true: Updating the VideoImage with a fix Timer is less accurate than 'ffplay.c'.

- Why do You use 'TList', instead of FFmpeg's 'AVPacketList' ?
  The C-Code for the PacketQueues is simple, works, and is maybe faster than 'TList'.
  The FPC-Analogue to this Part of 'ffplay.c' is here (it did not change till this day):
  -> Download the File in the Attachment and GoTo: "AV-PacketQueues".

I tried Your Player:
- Considering Your very simplified AV-Sync-Code, AV-Syncing is surprisingly o.k., but 'ffplay.c' is more accurate.
-> Solution: Have I Look at 'ffplay.c': AV-Syncing is much more complex there.

- With quite a lot VideoFiles...
  ...Seeking is deactivated (e.g. with all my MKV-Files).
  ...Seeking results in a black Screen for quite a long Time.
  ...Seeking relative ( [Seek(10s)] ) does not work: The Stream only seeks back to the same Position or to BOF.

- In those Cases, where the Seek works, ...
  ...jumping to the new AudioStreamPosition is audible -> not nice.
  ...with some VideoFiles, I get ugly ("verpixelt" - I'm German, too) VideoImages, when Seeking.
-> Solution: Consider using 'avcodec_flush_buffers()'.

=> Still lots of Work.  ;)
« Last Edit: March 21, 2018, 05:14:04 pm by metis »
Life could be so easy, if there weren't those f*** Details.



Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus