Skip to content

Commit b84ba2b

Browse files
author
Dogancan Ozturk
committed
memory allocation check
1 parent cc3e8b6 commit b84ba2b

File tree

11 files changed

+86
-45
lines changed

11 files changed

+86
-45
lines changed
12.5 KB
Binary file not shown.

H264Sharp/Converter.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,29 @@ public static void SetOption(ConverterOption option, int value)
7373
/// </summary>
7474
/// <param name="size">The size, in bytes, of the memory to allocate.</param>
7575
/// <returns>A pointer to the allocated memory, or <see cref="IntPtr.Zero"/> if allocation fails.</returns>
76-
public static IntPtr AllocAllignedNative(int size) => //Marshal.AllocHGlobal(size);
77-
Defines.Native.AllocAllignedNative(size);
76+
public static IntPtr AllocAllignedNative(int size)
77+
{
78+
//Marshal.AllocHGlobal(size);
79+
IntPtr ptr = Defines.Native.AllocAllignedNative(size);
80+
if (ptr == IntPtr.Zero)
81+
{
82+
throw new OutOfMemoryException("Memory allocation failed.");
83+
}
84+
return ptr;
85+
}
7886

7987
/// <summary>
8088
/// Frees native memory allocated by <see cref="AllocAllignedNative"/>.
8189
/// </summary>
8290
/// <param name="p"></param>
83-
public static void FreeAllignedNative(IntPtr p) => //Marshal.FreeHGlobal(p);
84-
Defines.Native.FreeAllignedNative(p);
91+
public static void FreeAllignedNative(IntPtr p)
92+
{
93+
// Marshal.FreeHGlobal(p);
94+
if (p != IntPtr.Zero)
95+
{
96+
Defines.Native.FreeAllignedNative(p);
97+
}
98+
}
8599

86100
/// <summary>
87101
/// Converts RGB,BGR,RGBA,BGRA to YUVI420P
@@ -133,6 +147,11 @@ public static void Yuv2Rgb(YuvImage yuv,RgbImage image)
133147
Yuv2Rgb(yuv.ToYUVImagePointer(), image);
134148
}
135149

150+
/// <summary>
151+
/// Converts YUV420p image to RGB format
152+
/// </summary>
153+
/// <param name="yuv"></param>
154+
/// <param name="image"></param>
136155
public static void Yuv2Rgb(YUVImagePointer yuv, RgbImage image)
137156
{
138157
unsafe

H264Sharp/Data.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ public YuvImage(int width, int height)
385385
Height = height;
386386
strideY = width;
387387
strideUV = width / 2;
388-
this.ImageBytes = Converter.AllocAllignedNative((width * height) + (width * height) / 2);//Marshal.AllocHGlobal((width * height)+(width*height)/2);
388+
this.ImageBytes = Converter.AllocAllignedNative((width * height) + ((width * height) / 2));
389389
ownsNativeMemory = true;
390390
}
391391

@@ -404,21 +404,38 @@ public YuvImage(IntPtr data, int width, int height)
404404
this.ImageBytes = data;
405405
}
406406

407+
/// <summary>
408+
/// Creates Reference instance to existing native yuv data
409+
/// </summary>
410+
/// <param name="data"></param>
411+
/// <param name="width"></param>
412+
/// <param name="height"></param>
413+
/// <param name="Ystride"></param>
414+
/// <param name="UVstride"></param>
415+
public YuvImage(IntPtr data, int width, int height, int Ystride, int UVstride)
416+
{
417+
Width = width;
418+
Height = height;
419+
strideY = Ystride;
420+
strideUV = UVstride;
421+
this.ImageBytes = data;
422+
}
423+
407424

408425
internal YUVImagePointer ToYUVImagePointer()
409426
{
410427

411428
return new YUVImagePointer(
412429
ImageBytes,
413-
IntPtr.Add(ImageBytes, Width * Height),
414-
IntPtr.Add(ImageBytes, Width * Height + (Width * Height) / 4),
430+
IntPtr.Add(ImageBytes, strideY * Height),
431+
IntPtr.Add(ImageBytes, strideY * Height + (strideUV * (Height / 2))),
415432
Width, Height, strideY, strideUV);
416433

417434
}
418435

419436
public byte[] GetBytes()
420437
{
421-
byte[] dat = new byte[Width * Height + (Width * Height) / 2];
438+
byte[] dat = new byte[Width * Height + (strideUV * (Height / 2))];
422439

423440
unsafe
424441
{
Binary file not shown.
-4.68 MB
Binary file not shown.
-4.45 MB
Binary file not shown.

H264SharpNative/AVX2Common.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,6 @@ inline void Store(uint8_t* dst,__m256i r1, __m256i g1, __m256i b1) {
285285
}
286286

287287

288-
alignas(32) static const uint8_t shuffle_pattern[16] = {
289-
0, 3, 6, 9, 12, 15, 2, 5,
290-
8, 11, 14, 1, 4, 7, 10, 13
291-
};
292-
293-
alignas(32) static const uint8_t blend_mask[16] = {
294-
255, 255, 255, 255, 255, 255, 255, 255,
295-
255, 255, 255, 0, 0, 0, 0, 0
296-
};
297-
298288
__attribute__((target("avx2")))
299289
inline void GetChannels3_16x16(uint8_t* RESTRICT input, __m256i& rl, __m256i& gl, __m256i& bl, __m256i& rh, __m256i& gh, __m256i& bh)
300290
{
@@ -365,13 +355,13 @@ inline void pack_16x16(__m256i a, __m256i b, __m256i c, __m256i d, __m256i& low,
365355
__attribute__((target("avx2")))
366356
inline void GetChannels4_16x16(uint8_t* RESTRICT src, __m256i& rl, __m256i& gl, __m256i& bl, __m256i& rh, __m256i& gh, __m256i& bh)
367357
{
368-
const auto rmask = _mm256_setr_epi8(
358+
const __m256i rmask = _mm256_setr_epi8(
369359
0, -1, -1, -1, 4, -1, -1, -1, 8, -1, -1, -1, 12, -1, -1, -1, 16,
370360
-1, -1, -1, 20, -1, -1, -1, 24, -1, -1, -1, 28, -1, -1, -1);
371-
const auto gmask = _mm256_setr_epi8(
361+
const __m256i gmask = _mm256_setr_epi8(
372362
1, -1, -1, -1, 5, -1, -1, -1, 9, -1, -1, -1, 13, -1, -1, -1, 17,
373363
-1, -1, -1, 21, -1, -1, -1, 25, -1, -1, -1, 29, -1, -1, -1);
374-
const auto bmask = _mm256_setr_epi8(
364+
const __m256i bmask = _mm256_setr_epi8(
375365
2, -1, -1, -1, 6, -1, -1, -1, 10, -1, -1, -1, 14, -1, -1, -1, 18,
376366
-1, -1, -1, 22, -1, -1, -1, 26, -1, -1, -1, 30, -1, -1, -1);
377367
__m256i rgb1 = _mm256_loadu_si256((__m256i*)src);

H264SharpNative/CMakeLists.txt

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
21
cmake_minimum_required (VERSION 3.8)
3-
42
project ("OpenH264Wrapper2")
53

6-
74
add_definitions(-DUNICODE -D_UNICODE)
85
add_compile_options(-Rpass=loop-vectorize)
96

107
add_compile_options(-O3)
118
#add_compile_options(-mavx2)
12-
#add_compile_options(-msse4.2)
139
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.2")
1410

1511
if(ANDROID)
@@ -72,19 +68,18 @@ add_library (CMakeProject1 SHARED
7268
"YuvNV12toYV12.cpp"
7369
"Rgb2Yuv.cpp")
7470

75-
if (CMAKE_VERSION VERSION_GREATER 3.12)
76-
set_property(TARGET CMakeProject1 PROPERTY CXX_STANDARD 17)
77-
endif()
78-
#output name
79-
#H264SharpNative-linux64.so
80-
#H264SharpNative-linux64.so
81-
71+
set_property(TARGET CMakeProject1 PROPERTY CXX_STANDARD 17)
72+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8273

8374
if(ANDROID)
75+
76+
target_link_libraries(CMakeProject1 PUBLIC m)
77+
set(CMAKE_CXX_STANDARD_LIBRARIES "-stdlib=libc++")
78+
target_link_libraries(CMakeProject1 PUBLIC c++_shared)
79+
8480
set_target_properties(CMakeProject1 PROPERTIES PREFIX "")
85-
set_target_properties(CMakeProject1 PROPERTIES OUTPUT_NAME "H264SharpNative-android-arm64")
81+
set_target_properties(CMakeProject1 PROPERTIES OUTPUT_NAME "H264SharpNative-android-x64")
8682
set_target_properties(CMakeProject1 PROPERTIES SUFFIX ".so")
87-
8883
elseif(UNIX)
8984
set_target_properties(CMakeProject1 PROPERTIES PREFIX "")
9085
set_target_properties(CMakeProject1 PROPERTIES OUTPUT_NAME "H264SharpNative-linux-x64")

H264SharpNative/ThreadPool.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
#include <queue>
77
#include <functional>
88
#include <iostream>
9-
#include <memory>
10-
#include <new>
9+
1110
#include <atomic>
1211
#include"pch.h"
13-
#include <variant>
1412

1513
#if defined(__linux__) || defined(__ANDROID__)
1614
#include <sys/syscall.h>

H264SharpNative/pch.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
1717
-DANDROID_ABI=arm64-v8a \
1818
-G Ninja .
1919
20+
export ANDROID_NDK="C:/Program Files (x86)/Android/AndroidNDK/android-ndk-r27c"
21+
export PATH="$ANDROID_NDK/toolchains/llvm/prebuilt/windows-x86_64/bin:$PATH"
22+
23+
cmake -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake" \
24+
-DANDROID_ABI=arm64-v8a \
25+
-G Ninja .
2026
2127
28+
/---------------------------------------------------
29+
2230
2331
export ANDROID_NDK="C:/Program Files (x86)/Android/AndroidNDK/android-ndk-r23c"
2432
export PATH="$ANDROID_NDK/toolchains/llvm/prebuilt/windows-x86_64/bin:$PATH"
@@ -36,14 +44,27 @@ cmake -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake"
3644
-G Ninja .
3745
3846
47+
//NDK 27
3948
49+
export ANDROID_NDK="C:/Program Files (x86)/Android/AndroidNDK/android-ndk-r27c"
50+
export PATH="$ANDROID_NDK/toolchains/llvm/prebuilt/windows-x86_64/bin:$PATH"
4051
52+
cmake -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake" \
53+
-DANDROID_ABI=arm64-v8a \
54+
-G Ninja .
4155
4256
43-
export ANDROID_NDK="C:/Program Files (x86)/Android/AndroidNDK/android-ndk-r27c"
57+
export ANDROID_NDK="C:/Program Files (x86)/Android/AndroidNDK/android-ndk-r27c"
4458
export PATH="$ANDROID_NDK/toolchains/llvm/prebuilt/windows-x86_64/bin:$PATH"
4559
4660
cmake -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake" \
47-
-DANDROID_ABI=arm64-v8a \
61+
-DANDROID_ABI=x86_64 \
4862
-G Ninja .
63+
64+
65+
66+
67+
68+
69+
4970
*/

0 commit comments

Comments
 (0)