Skip to content

Commit

Permalink
Pre-allocate memory for processed image bytes too
Browse files Browse the repository at this point in the history
  • Loading branch information
GoaLitiuM committed Sep 27, 2016
1 parent 6aaccf3 commit 7da2056
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 53 deletions.
15 changes: 0 additions & 15 deletions Pulsus/FFmpeg/FFmpegContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public int audioSamplesTotal

Stream stream;
string path;
byte[] managedBuffer;

int decodedFrames;

Expand Down Expand Up @@ -484,20 +483,6 @@ private bool ConvertFrame()
return true;
}

public byte[] GetFrameData()
{
int bufferSize = 0;
sbyte* buffer = GetFrameBuffer(out bufferSize);

// allocate and copy the data to managed memory
if (managedBuffer == null || managedBuffer.Length != bufferSize)
managedBuffer = new byte[bufferSize];

Marshal.Copy((IntPtr)buffer, managedBuffer, 0, bufferSize);

return managedBuffer;
}

public int GetFrameData(ref byte[] bytes, int startIndex)
{
int bufferSize = 0;
Expand Down
34 changes: 14 additions & 20 deletions Pulsus/FFmpeg/FFmpegHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,22 @@ public static void Init()

public static byte[] ImageFromFile(string path, out int width, out int height, out int bytesPerPixel)
{
FFmpegVideo video = new FFmpegVideo();
width = 0;
height = 0;
bytesPerPixel = 0;

video.Load(path);

List<byte> bytes = new List<byte>();
video.OnNextFrame += (data) => bytes.AddRange(data);

if (video.isVideo)
video.ReadNextFrame();
else
video.ReadFrames();

width = video.width;
height = video.height;
bytesPerPixel = 4;
using (FFmpegVideo video = new FFmpegVideo())
{
video.Load(path);

video.Dispose();
width = video.width;
height = video.height;
bytesPerPixel = 4;

return bytes.ToArray();
if (video.isVideo)
{
video.ReadNextFrame();
return video.imageBytes;
}
else
return video.ReadFrames();
}
}

public static byte[] SoundFromFile(string path, out int sampleRate, out int channels, out ushort sampleFormatSDL)
Expand Down
33 changes: 21 additions & 12 deletions Pulsus/FFmpeg/FFmpegVideo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ public class FFmpegVideo : IDisposable

public OnNextFrameDelegate OnNextFrame;

private byte[] bytes = null;
public byte[] imageBytes { get { return bytes; } }

public bool isVideo { get { return length > 0.0; } }

string path;
double nextFramePts;
FFmpegContext ffContext;
Thread loadThread;
AutoResetEvent nextFrameEvent;
private string path;
private double nextFramePts;
private FFmpegContext ffContext;
private Thread loadThread;
private AutoResetEvent nextFrameEvent;

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void OnNextFrameDelegate(byte[] data);
Expand Down Expand Up @@ -80,6 +83,8 @@ private void Load(Stream stream)

// setup resamplers and other format converters if needed
ffContext.ConvertToFormat(AVPixelFormat.AV_PIX_FMT_BGRA);

bytes = new byte[width * height * 4];
}

public void Update(double deltaTime)
Expand All @@ -91,19 +96,23 @@ public void Update(double deltaTime)

if (currentTime >= nextFramePts && presentedFrames < decodedFrames)
{
OnNextFrame(ffContext.GetFrameData());
ffContext.GetFrameData(ref bytes, 0);
if (OnNextFrame != null)
OnNextFrame(bytes);

presentedFrames++;
nextFrameEvent.Set();
}
}

public void ReadFrames()
public byte[] ReadFrames()
{
while (ffContext.ReadFrame())
{
OnNextFrame(ffContext.GetFrameData());
presentedFrames++;
}
if (!ffContext.ReadFrame())
return null;

ffContext.GetFrameData(ref bytes, 0);
presentedFrames++;
return bytes;
}

public void Start()
Expand Down
12 changes: 6 additions & 6 deletions Pulsus/Gameplay/BGAObject.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Pulsus.FFmpeg;
using Pulsus.Graphics;
using System;
using System.IO;
using System;
using Pulsus.FFmpeg;
using Pulsus.Graphics;

namespace Pulsus.Gameplay
{
Expand Down Expand Up @@ -78,7 +78,7 @@ public bool Load(string basePath = "")
{
video.Load(fullPath);
texture = new Texture2D(video.width, video.height);
video.OnNextFrame += texture.SetData;
video.OnNextFrame += (data) => texture.SetData(data);
}
catch when (Path.GetExtension(filename).ToLower() == ".lua")
{
Expand All @@ -93,7 +93,7 @@ public bool Load(string basePath = "")
if (video != null)
video.Dispose();
video = null;

return false;
}

Expand All @@ -105,7 +105,7 @@ public bool Load(string basePath = "")
else
{
// fully load image files
video.ReadFrames();
video.OnNextFrame(video.ReadFrames());
video.Dispose();
video = null;
}
Expand Down

0 comments on commit 7da2056

Please sign in to comment.