radek-k / FFMediaToolkit

FFMediaToolkit is a cross-platform video decoder/encoder library for .NET that uses FFmpeg native libraries. It supports video frames extraction, reading stream metadata and creating videos from bitmaps in any format supported by FFmpeg.
MIT License
353 stars 56 forks source link

Performance Recomendations #9

Closed redbaty closed 3 years ago

redbaty commented 4 years ago

Are there any improvements to be made on the performance side? Currently file.Video.ReadNextFrame(); is taking on average 22,33ms to run, which on a 24fps video isn't much faster than real-time ☹️

Are there any options for making it faster?

radek-k commented 4 years ago

I know that the library performance isn't optimal, but I currently don't have many time to improve it. If you have any ideas, please open a pull request.

redbaty commented 4 years ago

Yeah, this is more of a "where to look" kind of question, the question I'm gonna ask is kinda dumb, but I don't know FFmpeg too much, can you walk me through the frame grabbing process?

Example

        //VideoStream.cs - Line 95
        private unsafe ImageData Read()
        {
            stream.Read(frame); //This does blabla bla and send packet so that this does that.
            FramePosition++; // Moves frame position.

            var targetLayout = GetTargetSize(); //Calculate targetsize.
            var bitmap = ImageData.CreatePooled(targetLayout, mediaOptions.VideoPixelFormat); // Rent memory space.
            scaler.AVFrameToBitmap(frame, bitmap); // Do a thing.
            return bitmap;
        }