Skip to content

Commit

Permalink
MP1-5154: Improved DVB subtitle rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
epbk committed Jul 12, 2023
1 parent b0abcbd commit a686fd1
Showing 1 changed file with 19 additions and 34 deletions.
53 changes: 19 additions & 34 deletions mediaportal/Core/Player/Subtitles/SubtitleRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,20 +199,8 @@ public bool Allocate()
{
DataRectangle dr = texture.LockRectangle(0, LockFlags.Discard);
BitmapData bmData = subBitmap.LockBits(new System.Drawing.Rectangle(0, 0, this.width, this.height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
int size = bmData.Stride * bmData.Height;

unsafe
{
byte* pTo = (byte*)dr.DataPointer;
byte* pFrom = (byte*)bmData.Scan0;
for (int iY = 0; iY < this.height; iY++)
{
for (int iX = 0; iX < this.width * 4; iX++)
{
pTo[dr.Pitch * iY + iX] = pFrom[bmData.Stride * iY + iX];
}
}
}
CopyBitmap(dr, bmData); //Fast copy

texture.UnlockRectangle(0);
subBitmap.UnlockBits(bmData);
Expand Down Expand Up @@ -260,6 +248,17 @@ public override string ToString()
{
return "Subtitle " + id + " meta data: Timeout=" + timeOut + " timestamp=" + presentTime;
}

public static void CopyBitmap(DataRectangle destination, BitmapData source)
{
//Fast copy
//The pitch of locked rectangle can be larger then bitmap stride; we need to copy the bitmap data line by line
UIntPtr pSize = new UIntPtr((uint)source.Width * 4);
for (int iY = 0; iY < source.Height; iY++)
{
memcpy(destination.DataPointer + (destination.Pitch * iY), source.Scan0 + (source.Stride * iY), pSize);
}
}
}

/// <summary>
Expand Down Expand Up @@ -597,28 +596,14 @@ public void OnTextSubtitle(ref TEXT_SUBTITLE sub)
BitmapData bd = subtitle.subBitmap.LockBits(new System.Drawing.Rectangle(0, 0, subtitle.subBitmap.Width,
subtitle.subBitmap.Height), ImageLockMode.ReadOnly,
PixelFormat.Format32bppArgb);

Subtitle.CopyBitmap(dr, bd); //Fast copy

// Quick copy of content
unsafe
{
byte* to = (byte*)dr.DataPointer;
byte* from = (byte*)bd.Scan0.ToPointer();
for (int y = 0; y < bd.Height; ++y)
{
for (int x = 0; x < bd.Width * 4; ++x)
{
to[dr.Pitch * y + x] = from[y * bd.Stride + x];

}
}

texture.UnlockRectangle(0);
subtitle.subBitmap.UnlockBits(bd);
subtitle.subBitmap.SafeDispose();
subtitle.subBitmap = null;
subtitle.texture = texture;

}
texture.UnlockRectangle(0);
subtitle.subBitmap.UnlockBits(bd);
subtitle.subBitmap.SafeDispose();
subtitle.subBitmap = null;
subtitle.texture = texture;
}
catch (Exception e)
{
Expand Down

0 comments on commit a686fd1

Please sign in to comment.