Skip to content

Commit

Permalink
MP2-860: Updated DvbSub3 filter to fix crash due to wrong struct layo…
Browse files Browse the repository at this point in the history
…ut for x64. Minor cleanups
  • Loading branch information
morpheusxx committed Jan 4, 2021
1 parent f9073f2 commit 2f1316c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 29 deletions.
16 changes: 7 additions & 9 deletions MediaPortal/Source/UI/Players/VideoPlayer/Subtitles/Subtitle.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MediaPortal.Common;
using MediaPortal.Common;
using MediaPortal.Common.Logging;
using MediaPortal.UI.Players.Video.Tools;
using SharpDX.Direct3D9;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace MediaPortal.UI.Players.Video.Subtitles
{
Expand Down Expand Up @@ -81,6 +77,8 @@ public void Dispose()
{
SubTexture?.Dispose();
SubTexture = null;
SubBitmap?.Dispose();
SubBitmap = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public struct NativeSubtitle
public Int32 ScreenWidth;
public Int32 ScreenHeight;

// Subtitle timestmap
// Subtitle timestamp
public UInt64 TimeStamp;

// How long to display subtitle
Expand Down Expand Up @@ -587,7 +587,6 @@ protected virtual Subtitle ToSubtitle(IntPtr nativeSubPtr)
NativeSubtitle nativeSub = (NativeSubtitle)Marshal.PtrToStructure(nativeSubPtr, typeof(NativeSubtitle));
Subtitle subtitle = new Subtitle(_device)
{
SubBitmap = new Bitmap(nativeSub.Width, nativeSub.Height, PixelFormat.Format32bppArgb),
TimeOut = nativeSub.TimeOut,
PresentTime = ((double)nativeSub.TimeStamp / 1000.0f) + _startPos,
Height = (uint)nativeSub.Height,
Expand All @@ -598,31 +597,28 @@ protected virtual Subtitle ToSubtitle(IntPtr nativeSubPtr)
HorizontalPosition = nativeSub.HorizontalPosition,
Id = _subCounter++
};
CopyBits(nativeSub.Bits, ref subtitle.SubBitmap, nativeSub.Width, nativeSub.Height, nativeSub.WidthBytes);
CopyBits(nativeSub, subtitle);
return subtitle;
}

protected static void CopyBits(IntPtr srcBits, ref Bitmap destBitmap, int width, int height, int widthBytes)
protected static void CopyBits(NativeSubtitle nativeSubtitle, Subtitle subtitle)
{
// get bits of allocated image
BitmapData bmData = destBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
int newSize = bmData.Stride * height;
int size = widthBytes * height;
var bitmap = subtitle.SubBitmap = new Bitmap((int)subtitle.Width, (int)subtitle.Height, PixelFormat.Format32bppArgb);
BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, (int)subtitle.Width, (int)subtitle.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
int newSize = bmData.Stride * (int)subtitle.Height;
int size = nativeSubtitle.WidthBytes * (int)subtitle.Height;

if (newSize != size)
{
ServiceRegistration.Get<ILogger>().Error("SubtitleRenderer: newSize != size : {0} != {1}", newSize, size);
}
// Copy to new bitmap
//Marshal.Copy(sub.Bits,bmData.Scan0, 0, newSize);
byte[] srcData = new byte[size];


// could be done in one copy, but no IntPtr -> IntPtr Marshal.Copy method exists?
Marshal.Copy(srcBits, srcData, 0, size);
Marshal.Copy(srcData, 0, bmData.Scan0, newSize);

destBitmap.UnlockBits(bmData);
// Copy to new bitmap
unsafe
{
Buffer.MemoryCopy(nativeSubtitle.Bits.ToPointer(), bmData.Scan0.ToPointer(), newSize, size);
}
bitmap.UnlockBits(bmData);
}

#endregion
Expand Down Expand Up @@ -655,7 +651,7 @@ public void DrawOverlay(Texture targetTexture)
if (_textBuffer == null)
_textBuffer = new TextBuffer(FontManager.DefaultFontFamily, fontSize);
_textBuffer.Text = currentSubtitle.Text;

RectangleF rectangleF = new RectangleF(0, 0, SkinContext.WindowSize.Width, SkinContext.WindowSize.Height);

HorizontalTextAlignEnum horzAlign = HorizontalTextAlignEnum.Center;
Expand Down Expand Up @@ -701,7 +697,7 @@ public void DrawOverlay(Texture targetTexture)
_onTextureInvalidated?.Invoke();
}
}

#endregion

#region Subtitle queue handling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
<Version>4.0.0</Version>
</PackageReference>
<PackageReference Include="MediaPortal.DVBSub">
<Version>1.19.1</Version>
<Version>1.27.0</Version>
</PackageReference>
<PackageReference Include="MediaPortal.MpcSubs">
<Version>2.0.0.2</Version>
Expand Down

0 comments on commit 2f1316c

Please sign in to comment.