summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsr55 <[email protected]>2014-12-28 20:56:40 +0000
committersr55 <[email protected]>2014-12-28 20:56:40 +0000
commit0559e985cb5cb1e4dec92fd9c868b31f64acbe27 (patch)
tree1b8b646193b097f4190dc03649b6e4aebe6de680
parent50a50dc5019f5940e83c0c7a6cb4e5c3b90d0f02 (diff)
WinGui: Updated to use hb_get_preview2.
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@6667 b64f7644-9d1e-0410-96f1-a4d463321fa5
-rw-r--r--win/CS/HandBrake.Interop/HandBrakeInterop/HandBrakeInstance.cs56
-rw-r--r--win/CS/HandBrake.Interop/HandBrakeInterop/HandBrakeInterop.csproj1
-rw-r--r--win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/hb_image_s.cs26
3 files changed, 64 insertions, 19 deletions
diff --git a/win/CS/HandBrake.Interop/HandBrakeInterop/HandBrakeInstance.cs b/win/CS/HandBrake.Interop/HandBrakeInterop/HandBrakeInstance.cs
index 9febb93c0..a8bf3db75 100644
--- a/win/CS/HandBrake.Interop/HandBrakeInterop/HandBrakeInstance.cs
+++ b/win/CS/HandBrake.Interop/HandBrakeInterop/HandBrakeInstance.cs
@@ -17,6 +17,7 @@ namespace HandBrake.Interop
using System.Globalization;
using System.IO;
using System.Linq;
+ using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Timers;
@@ -264,12 +265,13 @@ namespace HandBrake.Interop
/// <param name="job">The encode job to preview.</param>
/// <param name="previewNumber">The index of the preview to get (0-based).</param>
/// <returns>An image with the requested preview.</returns>
+ [HandleProcessCorruptedStateExceptions]
public BitmapImage GetPreview(EncodeJob job, int previewNumber)
{
-
Title title = this.Titles.FirstOrDefault(t => t.TitleNumber == job.Title);
Validate.NotNull(title, "GetPreview: Title should not have been null. This is probably a bug.");
+ // Creat the Expected Output Geometry details for libhb.
hb_geometry_settings_s uiGeometry = new hb_geometry_settings_s
{
crop = new[] { job.EncodingProfile.Cropping.Top, job.EncodingProfile.Cropping.Bottom, job.EncodingProfile.Cropping.Left, job.EncodingProfile.Cropping.Right },
@@ -279,40 +281,56 @@ namespace HandBrake.Interop
maxHeight = job.EncodingProfile.MaxHeight,
mode = (int)(hb_anamorphic_mode_t)job.EncodingProfile.Anamorphic,
modulus = job.EncodingProfile.Modulus,
- geometry = new hb_geometry_s { height = job.EncodingProfile.Height, width = job.EncodingProfile.Width,
- par = job.EncodingProfile.Anamorphic != Anamorphic.Custom
+ geometry = new hb_geometry_s
+ {
+ height = job.EncodingProfile.Height,
+ width = job.EncodingProfile.Width,
+ par = job.EncodingProfile.Anamorphic != Anamorphic.Custom
? new hb_rational_t { den = title.ParVal.Height, num = title.ParVal.Width }
- : new hb_rational_t { den = job.EncodingProfile.PixelAspectY, num = job.EncodingProfile.PixelAspectX } }
+ : new hb_rational_t { den = job.EncodingProfile.PixelAspectY, num = job.EncodingProfile.PixelAspectX }
+ }
};
// Sanatise the input.
- Json.Anamorphic.Geometry resultGeometry = AnamorphicFactory.CreateGeometry(job, title, AnamorphicFactory.KeepSetting.HB_KEEP_WIDTH); // TODO this keep isn't right.
-
- int outputWidth = resultGeometry.Width;
- int outputHeight = resultGeometry.Height;
- int imageBufferSize = outputWidth * outputHeight * 4;
- IntPtr nativeBuffer = Marshal.AllocHGlobal(imageBufferSize);
-
-
- HBFunctions.hb_get_preview2(this.hbHandle, job.Title, previewNumber, ref uiGeometry, 0);
+ Json.Anamorphic.Geometry resultGeometry = AnamorphicFactory.CreateGeometry(job, title, AnamorphicFactory.KeepSetting.HB_KEEP_WIDTH); // TODO this keep isn't right.
+ int width = resultGeometry.Width * resultGeometry.PAR.Num / resultGeometry.PAR.Den;
+ int height = resultGeometry.Height;
+ uiGeometry.geometry.height = resultGeometry.Height; // Prased the height now.
+ int outputWidth = width;
+ int outputHeight = height;
+
+ // Fetch the image data from LibHb
+ IntPtr resultingImageStuct = HBFunctions.hb_get_preview2(this.hbHandle, job.Title, previewNumber, ref uiGeometry, 0);
+ hb_image_s image = InteropUtilities.ToStructureFromPtr<hb_image_s>(resultingImageStuct);
// Copy the filled image buffer to a managed array.
+ int stride_width = image.plane[0].stride;
+ int stride_height = image.plane[0].height_stride;
+ int imageBufferSize = stride_width * stride_height; // int imageBufferSize = outputWidth * outputHeight * 4;
+
byte[] managedBuffer = new byte[imageBufferSize];
- Marshal.Copy(nativeBuffer, managedBuffer, 0, imageBufferSize);
+ Marshal.Copy(image.plane[0].data, managedBuffer, 0, imageBufferSize);
var bitmap = new Bitmap(outputWidth, outputHeight);
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, outputWidth, outputHeight), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
- IntPtr ptr = bitmapData.Scan0;
-
- for (int i = 0; i < job.EncodingProfile.Height; i++)
+ IntPtr ptr = bitmapData.Scan0; // Pointer to the first pixel.
+ for (int i = 0; i < image.height; i++)
{
- Marshal.Copy(managedBuffer, i * job.EncodingProfile.Width * 4, ptr, job.EncodingProfile.Width * 4);
- ptr = IntPtr.Add(ptr, bitmapData.Stride);
+ try
+ {
+ Marshal.Copy(managedBuffer, i * stride_width, ptr, stride_width);
+ ptr = IntPtr.Add(ptr, outputWidth * 4);
+ }
+ catch (Exception exc)
+ {
+ Debug.WriteLine(exc); // In theory, this will allow a partial image display if this happens. TODO add better logging of this.
+ }
}
bitmap.UnlockBits(bitmapData);
+ // Create a Bitmap Image for display.
using (var memoryStream = new MemoryStream())
{
try
diff --git a/win/CS/HandBrake.Interop/HandBrakeInterop/HandBrakeInterop.csproj b/win/CS/HandBrake.Interop/HandBrakeInterop/HandBrakeInterop.csproj
index 65f5458f0..936cff5bb 100644
--- a/win/CS/HandBrake.Interop/HandBrakeInterop/HandBrakeInterop.csproj
+++ b/win/CS/HandBrake.Interop/HandBrakeInterop/HandBrakeInterop.csproj
@@ -165,6 +165,7 @@
<Compile Include="HbLib\hb_filter_ids.cs" />
<Compile Include="HbLib\hb_geometry.cs" />
<Compile Include="HbLib\hb_handle_s.cs" />
+ <Compile Include="HbLib\hb_image_s.cs" />
<Compile Include="HbLib\hb_mixdown_s.cs" />
<Compile Include="HbLib\hb_rate_s.cs" />
<Compile Include="HbLib\hb_state_s.cs" />
diff --git a/win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/hb_image_s.cs b/win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/hb_image_s.cs
new file mode 100644
index 000000000..60c5251ca
--- /dev/null
+++ b/win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/hb_image_s.cs
@@ -0,0 +1,26 @@
+namespace HandBrake.Interop.HbLib
+{
+ using System;
+ using System.Runtime.InteropServices;
+
+ public struct hb_image_s
+ {
+ public int format;
+ public int width;
+ public int height;
+ public IntPtr data;
+
+ [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = UnmanagedType.I4)]
+ public image_plane[] plane;
+ }
+
+ public struct image_plane
+ {
+ public IntPtr data;
+ public int width;
+ public int height;
+ public int stride;
+ public int height_stride;
+ public int size;
+ }
+}