From f2ca02b9220a88a018759932dd9608b2de8a91cf Mon Sep 17 00:00:00 2001 From: sr55 Date: Fri, 21 Feb 2014 22:06:24 +0000 Subject: WinGui: Some tidy up and refactoring in the interop library. Few small internal namespace changes. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@6053 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- .../HandBrakeInterop/Helpers/InteropUtilities.cs | 249 +++++++++++++++++++++ .../HandBrakeInterop/Helpers/Languages.cs | 51 +++++ .../Helpers/MarshalingConstants.cs | 32 +++ .../HandBrakeInterop/Helpers/NativeList.cs | 125 +++++++++++ .../HandBrakeInterop/Helpers/Utilities.cs | 66 ++++++ 5 files changed, 523 insertions(+) create mode 100644 win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/InteropUtilities.cs create mode 100644 win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/Languages.cs create mode 100644 win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/MarshalingConstants.cs create mode 100644 win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/NativeList.cs create mode 100644 win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/Utilities.cs (limited to 'win/CS/HandBrake.Interop/HandBrakeInterop/Helpers') diff --git a/win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/InteropUtilities.cs b/win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/InteropUtilities.cs new file mode 100644 index 000000000..0019aa2d2 --- /dev/null +++ b/win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/InteropUtilities.cs @@ -0,0 +1,249 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// Helper utilities for native interop. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrake.Interop.Helpers +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Runtime.InteropServices; + using System.Text; + + using HandBrake.Interop.HbLib; + + /// + /// Helper utilities for native interop. + /// + public static class InteropUtilities + { + /// + /// Reads the given native structure pointer. + /// + /// The type to convert the structure to. + /// The pointer to the native structure. + /// The converted structure. + public static T ReadStructure(IntPtr structPtr) + { + return (T)Marshal.PtrToStructure(structPtr, typeof(T)); + } + + /// + /// Reads the given native UTF-8 string. + /// + /// The pointer to the string. + /// The resulting string. + public static string ReadUtf8Ptr(IntPtr stringPtr) + { + var data = new List(); + var ptr = stringPtr; + var offset = 0; + while (true) + { + byte ch = Marshal.ReadByte(ptr, offset++); + if (ch == 0) + { + break; + } + + data.Add(ch); + } + + return Encoding.UTF8.GetString(data.ToArray()); + } + + /// + /// Creates a pointer to a UTF-8 null-terminated string. + /// + /// + /// The string to encode. + /// + /// + /// The . + /// + public static IntPtr CreateUtf8Ptr(string str) + { + byte[] bytes = Encoding.UTF8.GetBytes(str); + IntPtr stringPtr = Marshal.AllocHGlobal(bytes.Length + 1); + var offset = 0; + foreach (byte b in bytes) + { + Marshal.WriteByte(stringPtr, offset, b); + offset++; + } + + Marshal.WriteByte(stringPtr, offset, 0); + return stringPtr; + } + + /// + /// Converts the given native HandBrake list to a managed list. + /// + /// The type of structure in the list. + /// The pointer to the native list. + /// The converted managed list. + public static List ToList(this IntPtr listPtr) + { + List returnList = new List(); + NativeList nativeList = new NativeList(listPtr); + + for (int i = 0; i < nativeList.Count; i++) + { + IntPtr itemPtr = nativeList[i]; + returnList.Add(ReadStructure(itemPtr)); + } + + return returnList; + } + + /// + /// Converts the HB list to a managed list of pointers. + /// + /// The list to convert. + /// The managed list of pointers. + public static List ToIntPtrList(this IntPtr listPtr) + { + var returnList = new List(); + NativeList nativeList = new NativeList(listPtr); + + for (int i = 0; i < nativeList.Count; i++) + { + IntPtr itemPtr = nativeList[i]; + returnList.Add(itemPtr); + } + + return returnList; + } + + /// + /// Converts the given native array to a managed collection. + /// + /// The type of item in the list. + /// The pointer to the array. + /// The number of items in the array. + /// The converted collection. + public static IEnumerable ConvertArray(IntPtr arrayPtr, int count) + { + IntPtr currentItem = arrayPtr; + + var result = new List(); + for (int i = 0; i < count; i++) + { + T nativeEncoder = ReadStructure(currentItem); + result.Add(nativeEncoder); + + currentItem = IntPtr.Add(currentItem, Marshal.SizeOf(typeof(T))); + } + + return result; + } + + /// + /// Creates a native HandBrake list from the given managed list of pointers. + /// + /// The managed list to convert. + /// The converted native list. + public static NativeList CreateIntPtrList(List list) + { + NativeList returnList = NativeList.CreateList(); + + foreach (IntPtr ptr in list) + { + returnList.Add(ptr); + } + + return returnList; + } + + /// + /// Creates a native HandBrake list from the given managed list of structures. + /// + /// The type of structures in the list. + /// The managed list to convert. + /// The converted native list. + public static NativeList ConvertListBack(List list) + { + NativeList returnList = NativeList.CreateList(); + foreach (T item in list) + { + IntPtr itemPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(T))); + returnList.AllocatedMemory.Add(itemPtr); + Marshal.StructureToPtr(item, itemPtr, false); + + returnList.Add(itemPtr); + } + + return returnList; + } + + /// + /// Reads in a list of objects given an interator and a conversion function. + /// + /// The type of the struct given by the iterator. + /// The object type to convert to. + /// The iterator to use to build the list. + /// The converter to convert from the struct to the object. + /// The list of objects. + public static List GetListFromIterator(Func iterator, Func converter) + { + return ReadStructureListFromIterator(iterator).Select(converter).ToList(); + } + + /// + /// Reads in a list of structs given an iterator. + /// + /// The type of the struct. + /// The iterator to use to build the list. + /// The list of structs. + public static List ReadStructureListFromIterator(Func iterator) + { + var structureList = new List(); + IntPtr current = IntPtr.Zero; + + current = iterator(current); + while (current != IntPtr.Zero) + { + T encoder = ReadStructure(current); + structureList.Add(encoder); + + current = iterator(current); + } + + return structureList; + } + + /// + /// Closes the given job. + /// + /// The pointer to the job. + public static void CloseJob(IntPtr nativeJobPtr) + { + // Create a point to the job pointer first. + IntPtr nativeJobPtrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr))); + + // Assign the new pointer to the job pointer and tell HB to clean the job up. + Marshal.WriteIntPtr(nativeJobPtrPtr, nativeJobPtr); + HBFunctions.hb_job_close(nativeJobPtrPtr); + + // Free the pointer we used. + Marshal.FreeHGlobal(nativeJobPtrPtr); + } + + /// + /// Frees all the memory locations in the given list. + /// + /// The list of memory locations to free. + public static void FreeMemory(List memoryList) + { + foreach (IntPtr memoryLocation in memoryList) + { + Marshal.FreeHGlobal(memoryLocation); + } + } + } +} diff --git a/win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/Languages.cs b/win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/Languages.cs new file mode 100644 index 000000000..b89e190eb --- /dev/null +++ b/win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/Languages.cs @@ -0,0 +1,51 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// Contains utilities for converting language codes. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrake.Interop.Helpers +{ + using System.Collections.Generic; + + using HandBrake.Interop.HbLib; + using HandBrake.Interop.Model; + + /// + /// Contains utilities for converting language codes. + /// + public static class Languages + { + /// + /// The list of all languages. + /// + private static IList allLanguages; + + /// + /// Gets a list of all languages. + /// + public static IList AllLanguages + { + get + { + return allLanguages + ?? (allLanguages = + InteropUtilities.GetListFromIterator(HBFunctions.lang_get_next, Converters.Converters.NativeToLanguage)); + } + } + + /// + /// Gets the language object for the given code. + /// + /// The ISO-639-2 code for the language. + /// Object that describes the language. + public static Language Get(string code) + { + iso639_lang_t language = InteropUtilities.ReadStructure(HBFunctions.lang_for_code2(code)); + return Converters.Converters.NativeToLanguage(language); + } + } +} diff --git a/win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/MarshalingConstants.cs b/win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/MarshalingConstants.cs new file mode 100644 index 000000000..98ed5048f --- /dev/null +++ b/win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/MarshalingConstants.cs @@ -0,0 +1,32 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// Defines the MarshalingConstants type. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrake.Interop.Helpers +{ + /// + /// The marshaling constants. + /// + public static class MarshalingConstants + { +#if X64 + public const int JobPaddingBytes = 49264; + public const int AudioPaddingBytes = 49208; +#else + /// + /// Job Padding Bytes + /// + public const int JobPaddingBytes = 49216; + + /// + /// Audio Padding Bytes + /// + public const int AudioPaddingBytes = 49180; +#endif + } +} diff --git a/win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/NativeList.cs b/win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/NativeList.cs new file mode 100644 index 000000000..c77c58cb9 --- /dev/null +++ b/win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/NativeList.cs @@ -0,0 +1,125 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// Represents a HandBrake style native list. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrake.Interop.Helpers +{ + using System; + using System.Collections.Generic; + using System.Runtime.InteropServices; + + using HandBrake.Interop.HbLib; + + /// + /// Represents a HandBrake style native list. + /// + public class NativeList : IDisposable + { + /// + /// Initializes a new instance of the NativeList class. + /// + /// The pointer to use for the list. + public NativeList(IntPtr listPtr) + { + this.Ptr = listPtr; + } + + /// + /// The list of native memory locations allocated for this list. + /// + private readonly List allocatedMemory = new List(); + + /// + /// Gets the pointer to the native list. + /// + public IntPtr Ptr { get; private set; } + + /// + /// Gets the number of items in the list. + /// + public int Count + { + get + { + return HBFunctions.hb_list_count(this.Ptr); + } + } + + /// + /// Gets the list of native memory locations allocated for this list. + /// + public List AllocatedMemory + { + get + { + return this.allocatedMemory; + } + } + + /// + /// Adds an item to the end of the list. + /// + /// The item to add. + public void Add(IntPtr item) + { + HBFunctions.hb_list_add(this.Ptr, item); + } + + /// + /// Inserts an item into the list. + /// + /// The index to insert the item at. + /// The item to insert. + public void Insert(int position, IntPtr item) + { + HBFunctions.hb_list_insert(this.Ptr, position, item); + } + + /// + /// Removes an item from the list. + /// + /// The item to remove. + public void Remove(IntPtr item) + { + HBFunctions.hb_list_rem(this.Ptr, item); + } + + /// + /// Gets an item out of the list. + /// + /// Index in the list. + /// The item at that index in the list. + public IntPtr this[int i] + { + get + { + return HBFunctions.hb_list_item(this.Ptr, i); + } + } + + /// + /// Disposes resources associated with this object. + /// + public void Dispose() + { + IntPtr listPtrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr))); + Marshal.WriteIntPtr(listPtrPtr, this.Ptr); + HBFunctions.hb_list_close(listPtrPtr); + Marshal.FreeHGlobal(listPtrPtr); + } + + /// + /// Creates a new list in unmanaged memory. + /// + /// The created list. + public static NativeList CreateList() + { + return new NativeList(HBFunctions.hb_list_init()); + } + } +} diff --git a/win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/Utilities.cs b/win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/Utilities.cs new file mode 100644 index 000000000..77106f72d --- /dev/null +++ b/win/CS/HandBrake.Interop/HandBrakeInterop/Helpers/Utilities.cs @@ -0,0 +1,66 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// Defines the Utilities type. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrake.Interop.Helpers +{ + using HandBrake.Interop.Model.Encoding; + + /// + /// The utilities. + /// + public static class Utilities + { + /// + /// Get the Greatest Common Factor + /// + /// + /// The a. + /// + /// + /// The b. + /// + /// + /// The greatest common factor + /// + public static int GreatestCommonFactor(int a, int b) + { + if (a == 0) + { + return b; + } + + if (b == 0) + { + return a; + } + + if (a > b) + { + return GreatestCommonFactor(a % b, b); + } + + return GreatestCommonFactor(a, b % a); + } + + /// + /// Determines if the given audio encoder is a passthrough encoder choice. + /// + /// The audio encoder to examine. + /// True if the encoder is passthrough. + public static bool IsPassthrough(AudioEncoder encoder) + { + return encoder == AudioEncoder.Ac3Passthrough || + encoder == AudioEncoder.DtsHDPassthrough || + encoder == AudioEncoder.DtsPassthrough || + encoder == AudioEncoder.Mp3Passthru || + encoder == AudioEncoder.AacPassthru || + encoder == AudioEncoder.Passthrough; + } + } +} -- cgit v1.2.3