From 7e9ed4dafd3151061299116c745a0ec1abd3361a Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Wed, 29 Dec 2021 15:12:15 +1000
Subject: Add Haiku support


diff --git a/.gitignore b/.gitignore
deleted file mode 100644
index 7df8bb0..0000000
--- a/.gitignore
+++ /dev/null
@@ -1,21 +0,0 @@
-*~
-*.rej
-*.orig
-[tT]ags
-Doxyfile
-*.kdev*
-ffconf
-*.sw*
-build*
-debs
-addons/*
-install/*
-avidemux/qt4/common
-oldcrap/*
-nbproject
-avidemux/oldFiles/
-myBuild*
-oprofile_data
-avidemux/winInstaller/AvidemuxVersion.nsh
-avidemux/qt4/xdg_data/org.avidemux.Avidemux.appdata.xml
-*.DS_Store
diff --git a/avidemux/common/main.cpp b/avidemux/common/main.cpp
old mode 100755
new mode 100644
index 834806e..8e1e43b
--- a/avidemux/common/main.cpp
+++ b/avidemux/common/main.cpp
@@ -92,7 +92,9 @@ int main(int _argc, char *_argv[])
 	}
 #endif
 
+#ifndef __HAIKU__
 	installSigHandler();
+#endif
 
 	char **argv;
 	int argc;
@@ -114,9 +116,9 @@ int main(int _argc, char *_argv[])
 #ifdef _WIN32
 	freeUtf8CommandLine(argc, argv);
 #endif
-
+#ifndef __HAIKU__
 	uninstallSigHandler();
-
+#endif
 	return exitVal;
 }
 /**
diff --git a/avidemux/qt4/ADM_jobs/src/ADM_jobs.cpp b/avidemux/qt4/ADM_jobs/src/ADM_jobs.cpp
index cfb29a9..c64e971 100644
--- a/avidemux/qt4/ADM_jobs/src/ADM_jobs.cpp
+++ b/avidemux/qt4/ADM_jobs/src/ADM_jobs.cpp
@@ -46,9 +46,9 @@ int main(int argc, char *argv[])
 #if !defined(NDEBUG) && defined(FIND_LEAKS)
 	new_progname = argv[0];
 #endif
-
+#ifndef __HAIKU__
     installSigHandler();
-
+#endif
 #define STR(x) #x
 #define MKSTRING(x) STR(x)
 
@@ -77,6 +77,8 @@ int main(int argc, char *argv[])
 	printf("Microsoft Windows");
 #elif defined(__APPLE__)
 	printf("Apple");
+#elif defined(__HAIKU__)
+	printf("Haiku");
 #else
 	printf("Linux");
 #endif
@@ -124,9 +126,9 @@ int main(int argc, char *argv[])
     // Init jobs
     ADMJob::jobInit();
     jobRun(argc,argv);
-
+#ifndef __HAIKU__
 	uninstallSigHandler();
-
+#endif
     printf("Normal exit\n");
     return 0;
 }
diff --git a/avidemux/qt4/ADM_userInterfaces/ADM_gui/T_preview.cpp b/avidemux/qt4/ADM_userInterfaces/ADM_gui/T_preview.cpp
index caba43f..d8b1e39 100644
--- a/avidemux/qt4/ADM_userInterfaces/ADM_gui/T_preview.cpp
+++ b/avidemux/qt4/ADM_userInterfaces/ADM_gui/T_preview.cpp
@@ -29,7 +29,7 @@
 /* Probably on unix/X11 ..*/
 #ifdef __APPLE__
 #    include <Carbon/Carbon.h>
-#elif !defined(_WIN32)
+#elif !defined(_WIN32) && !defined(__HAIKU__)
 #   if QT_VERSION < QT_VERSION_CHECK(5,0,0)
 #       include <QX11Info> // removed in qt5
 #   else
@@ -79,9 +79,6 @@ bool ADM_QPreviewCleanup(void)
  * 
  * @param z
  */
-#ifdef __HAIKU__
-ADM_Qvideo::ADM_Qvideo(QWidget *z) : QWidget(z)  {}
-#else
 ADM_Qvideo::ADM_Qvideo(QFrame *z) : QWidget(z) 
 {       
     useExternalRedraw(true);
@@ -101,8 +98,6 @@ ADM_Qvideo::ADM_Qvideo(QFrame *z) : QWidget(z)
     hostFrame=z;
 
 } //{setAutoFillBackground(false);}
-#endif // Haiku
-
 
 ADM_Qvideo::~ADM_Qvideo() 
 {
@@ -259,6 +254,9 @@ void UI_getWindowInfo(void *draw, GUI_WindowInfo *xinfo)
 #if defined(_WIN32)
     xinfo->display=(void *)videoWindow->winId();
     xinfo->systemWindowId=videoWindow->winId();
+#elif defined(__HAIKU__)
+    xinfo->display = NULL;
+    xinfo->systemWindowId=0;
 #elif defined(__APPLE__)
 #   if defined(ADM_CPU_X86_64)
     xinfo->display = NULL; // we may not call winId() on a QWidget on macOS, it breaks OpenGL
diff --git a/avidemux/qt4/CMakeLists.txt b/avidemux/qt4/CMakeLists.txt
old mode 100755
new mode 100644
index e0e431b..b50c552
--- a/avidemux/qt4/CMakeLists.txt
+++ b/avidemux/qt4/CMakeLists.txt
@@ -32,7 +32,11 @@ ELSE(WIN32)
         IF(APPLE)
                 include(adm_osx.cmake)
         ELSE(APPLE)
-                include(adm_default.cmake)
+        	IF(HAIKU)
+                include(adm_haiku.cmake)
+			ELSE(HAIKU)
+				include(adm_default.cmake)
+			ENDIF(HAIKU)
         ENDIF(APPLE)
 ENDIF(WIN32)
 #-----------------------------------
diff --git a/avidemux/qt4/adm_haiku.cmake b/avidemux/qt4/adm_haiku.cmake
new file mode 100644
index 0000000..8f25071
--- /dev/null
+++ b/avidemux/qt4/adm_haiku.cmake
@@ -0,0 +1,19 @@
+#
+#
+#
+MACRO(ADM_PREBUILD)
+        LIST(APPEND PlatformLibs   "-lroot -lstdc++")
+        LIST(APPEND PlatformLibs   "-lbe")
+ENDMACRO(ADM_PREBUILD)
+#
+#
+#
+MACRO(ADM_MAIN_APP)
+        ADD_EXECUTABLE(avidemux3_${QT_EXTENSION}  ${ADM_EXE_SRCS})
+
+ENDMACRO(ADM_MAIN_APP)
+#
+
+MACRO(ADM_POSTBUILD)
+	ADM_INSTALL_BIN(avidemux3_${QT_EXTENSION})
+ENDMACRO(ADM_POSTBUILD)
diff --git a/avidemux_core/ADM_core/include/ADM_crashdump_other.h b/avidemux_core/ADM_core/include/ADM_crashdump_other.h
index 1d636ef..a75c16d 100644
--- a/avidemux_core/ADM_core/include/ADM_crashdump_other.h
+++ b/avidemux_core/ADM_core/include/ADM_crashdump_other.h
@@ -1,4 +1,4 @@
-#if !defined(__APPLE__) && !defined(__unix__) && !defined(__MINGW32__) && !defined ADM_CRASHDUMP_OTHER_H &&!defined _MSC_VER
+#if !defined(__APPLE__) && !defined(__unix__) && !defined(__HAIKU__) && !defined(__MINGW32__) && !defined ADM_CRASHDUMP_OTHER_H &&!defined _MSC_VER
 #define ADM_CRASHDUMP_OTHER_H
 
 #define installSigHandler(...) {}
diff --git a/avidemux_core/ADM_core/include/ADM_crashdump_unix.h b/avidemux_core/ADM_core/include/ADM_crashdump_unix.h
index 7067e06..e75a323 100644
--- a/avidemux_core/ADM_core/include/ADM_crashdump_unix.h
+++ b/avidemux_core/ADM_core/include/ADM_crashdump_unix.h
@@ -1,4 +1,4 @@
-#if defined(__unix__) && !defined ADM_CRASHDUMP_UNIX_H
+#if defined(__unix__) || defined(__HAIKU__) && !defined ADM_CRASHDUMP_UNIX_H
 #define ADM_CRASHDUMP_UNIX_H
 
 ADM_CORE6_EXPORT void installSigHandler(void);
diff --git a/avidemux_core/ADM_core/src/ADM_crashdump_unix.cpp b/avidemux_core/ADM_core/src/ADM_crashdump_unix.cpp
index a2f1375..b4b6fd2 100644
--- a/avidemux_core/ADM_core/src/ADM_crashdump_unix.cpp
+++ b/avidemux_core/ADM_core/src/ADM_crashdump_unix.cpp
@@ -40,8 +40,13 @@
 static ADM_saveFunction *mysaveFunction=NULL;
 static ADM_fatalFunction *myFatalFunction=NULL;
 static ADM_sigIntFunction *mySigIntFunction=NULL;
+#ifdef __HAIKU__
+static __sighandler_t      oldSignalHandlerSigInt;
+static __sighandler_t      oldSignalHandlerSigSev;
+#else
 static sighandler_t      oldSignalHandlerSigInt;
 static sighandler_t      oldSignalHandlerSigSev;
+#endif
 void sig_segfault_handler(int signo);
 void sig_sigint_handler(int signo);
 
diff --git a/avidemux_core/ADM_core/src/ADM_dynamicLoading.cpp b/avidemux_core/ADM_core/src/ADM_dynamicLoading.cpp
index 5066122..f0cc2e0 100644
--- a/avidemux_core/ADM_core/src/ADM_dynamicLoading.cpp
+++ b/avidemux_core/ADM_core/src/ADM_dynamicLoading.cpp
@@ -51,6 +51,8 @@ ADM_LibWrapper::~ADM_LibWrapper()
 
 	#ifdef _WIN32
 		FreeLibrary((HINSTANCE) hinstLib);
+	#elif defined(__HAIKU__)
+		aprintf("HAIKU: not implemented!\n");
 	#else
 		dlclose(hinstLib);
 	#endif
diff --git a/avidemux_core/ADM_core/src/ADM_folder_linux.cpp b/avidemux_core/ADM_core/src/ADM_folder_linux.cpp
index 4288c28..bb95b93 100644
--- a/avidemux_core/ADM_core/src/ADM_folder_linux.cpp
+++ b/avidemux_core/ADM_core/src/ADM_folder_linux.cpp
@@ -39,7 +39,11 @@ static std::string ADM_i18nDir;
 static std::string canonize(const std::string &in)
 {
     std::string out;
+#ifdef __HAIKU__
+	char *simple2=realpath(in.c_str(), NULL);
+#else
     char *simple2=canonicalize_file_name(in.c_str());
+#endif
     if(simple2)
     {
         out=std::string(simple2);
@@ -164,9 +168,11 @@ void ADM_initBaseDir(int argc, char *argv[])
     }
     strcpy(ADM_basedir, homeEnv);
     AddSeparator(ADM_basedir);
-
+#ifdef __HAIKU__
+	const char *ADM_DIR_NAME = "config/settings/Avidemux";
+#else
     const char *ADM_DIR_NAME = ".avidemux6";
-
+#endif
     strcat(ADM_basedir, ADM_DIR_NAME);
     strcat(ADM_basedir, ADM_SEPARATOR);
 
diff --git a/avidemux_core/ADM_core/src/CMakeLists.txt b/avidemux_core/ADM_core/src/CMakeLists.txt
index 25bf2e3..9914128 100644
--- a/avidemux_core/ADM_core/src/CMakeLists.txt
+++ b/avidemux_core/ADM_core/src/CMakeLists.txt
@@ -43,6 +43,8 @@ IF (WIN32)
 	target_link_libraries(ADM_core6 imagehlp ws2_32)
 ELSEIF (APPLE)
 	target_link_libraries(ADM_core6 "-framework Carbon")
+elseif (HAIKU)
+	target_link_libraries(ADM_core6)
 elseif (UNIX)
 	target_link_libraries(ADM_core6 dl m)
 ENDIF (WIN32)
diff --git a/avidemux_core/ADM_coreSocket/src/CMakeLists.txt b/avidemux_core/ADM_coreSocket/src/CMakeLists.txt
index 2186fb5..4c2512a 100644
--- a/avidemux_core/ADM_coreSocket/src/CMakeLists.txt
+++ b/avidemux_core/ADM_coreSocket/src/CMakeLists.txt
@@ -12,4 +12,8 @@ if (WIN32)
 	target_link_libraries(ADM_coreSocket6 wsock32)
 endif (WIN32)
 
+if (HAIKU)
+	target_link_libraries(ADM_coreSocket6 network)
+endif (HAIKU)
+
 ADM_INSTALL_LIB(ADM_coreSocket6)
diff --git a/avidemux_core/ADM_coreSqlLight3/src/CMakeLists.txt b/avidemux_core/ADM_coreSqlLight3/src/CMakeLists.txt
index 29054cd..db88952 100644
--- a/avidemux_core/ADM_coreSqlLight3/src/CMakeLists.txt
+++ b/avidemux_core/ADM_coreSqlLight3/src/CMakeLists.txt
@@ -11,7 +11,11 @@ IF (APPLE)
 	TARGET_LINK_LIBRARIES(ADM_coreSqlLight3 -Wl,-read_only_relocs,suppress)
 ENDIF (APPLE)
 
-IF (UNIX)
+IF (HAIKU)
+	TARGET_LINK_LIBRARIES(ADM_coreSqlLight3)
+ENDIF (UNIX)
+
+IF (UNIX AND NOT HAIKU)
 	TARGET_LINK_LIBRARIES(ADM_coreSqlLight3 dl)
 ENDIF (UNIX)
 
diff --git a/avidemux_core/ADM_coreUtils/src/prefs2_pref.h b/avidemux_core/ADM_coreUtils/src/prefs2_pref.h
index 490784d..c7a6718 100644
--- a/avidemux_core/ADM_coreUtils/src/prefs2_pref.h
+++ b/avidemux_core/ADM_coreUtils/src/prefs2_pref.h
@@ -61,7 +61,7 @@ static optionDesc myOptions[]={
 { LASTPROJECTS_FILE3,"lastprojects.file3"                             ,ADM_param_stdstring  	,"",	0,	0},
 { LASTPROJECTS_FILE4,"lastprojects.file4"                             ,ADM_param_stdstring  	,"",	0,	0},
 { MESSAGE_LEVEL,"message_level"                                       ,ADM_param_uint32_t	,"2",	0,	2},
-{ AUDIO_DEVICE_AUDIODEVICE,"audio_device.audiodevice"                 ,ADM_param_stdstring  	,"ALSA",	0,	0},
+{ AUDIO_DEVICE_AUDIODEVICE,"audio_device.audiodevice"                 ,ADM_param_stdstring  	,"Haiku",	0,	0},
 { AUDIO_DEVICE_ALSA_DEVICE,"audio_device.alsa_device"                 ,ADM_param_stdstring  	,"dmix",	0,	0},
 { VIDEODEVICE,"videodevice"                                           ,ADM_param_uint32_t	,"0",	0,	10},
 { PRIORITY_ENCODING,"priority.encoding"                               ,ADM_param_uint32_t	,"3",	0,	4},
diff --git a/avidemux_plugins/ADM_audioDevices/CMakeLists.txt b/avidemux_plugins/ADM_audioDevices/CMakeLists.txt
index ba5d4f2..a29ac2e 100644
--- a/avidemux_plugins/ADM_audioDevices/CMakeLists.txt
+++ b/avidemux_plugins/ADM_audioDevices/CMakeLists.txt
@@ -39,3 +39,7 @@ endif(APPLE)
 if(WIN32)
 ADD_SUBDIRECTORY(Win32)
 endif(WIN32)
+
+if(HAIKU)
+ADD_SUBDIRECTORY(Haiku)
+endif(HAIKU)
diff --git a/avidemux_plugins/ADM_audioDevices/Haiku/ADM_deviceHaiku.cpp b/avidemux_plugins/ADM_audioDevices/Haiku/ADM_deviceHaiku.cpp
new file mode 100644
index 0000000..6035856
--- /dev/null
+++ b/avidemux_plugins/ADM_audioDevices/Haiku/ADM_deviceHaiku.cpp
@@ -0,0 +1,84 @@
+#include <stdint.h>
+
+#include "ADM_default.h"
+
+#include  "ADM_audiodevice.h"
+#include  "ADM_audioDeviceInternal.h"
+
+#include "ADM_deviceHaiku.h"
+
+ADM_DECLARE_AUDIODEVICE(Haiku,haikuAudioDevice,1,0,0,"Haiku MediaKit audio device (c) 3dEyes");
+
+haikuAudioDevice::haikuAudioDevice()
+{
+    active=false;
+}
+
+haikuAudioDevice::~haikuAudioDevice()
+{
+	if (active) {
+		active=false;
+		player.stop();
+	}
+}
+
+bool
+haikuAudioDevice::localStop(void) 
+{
+	active=false;
+	player.stop();
+	return true;
+}
+
+bool
+haikuAudioDevice::localInit(void) 
+{
+	player.channels = (float)_channels;
+    player.sample_rate = (float)_frequency;
+	player.stop();
+	active = player.start();
+	return active;
+}
+
+void
+haikuAudioDevice::sendData(void)
+{
+	mutex.lock();
+    uint32_t avail=wrIndex-rdIndex;
+    if(avail>sizeOf10ms) avail=sizeOf10ms;
+    mutex.unlock();
+	player.write((unsigned char*)audioBuffer.at(rdIndex), avail);
+    mutex.lock();
+    rdIndex+=avail;
+    mutex.unlock();
+	return ;
+}
+
+uint8_t
+haikuAudioDevice::setVolume(int volume)
+{
+	player.setVolume(volume);
+	return 1;
+}
+
+const CHANNEL_TYPE mono[MAX_CHANNELS]={ADM_CH_MONO};
+const CHANNEL_TYPE stereo[MAX_CHANNELS]={ADM_CH_FRONT_LEFT,ADM_CH_FRONT_RIGHT};
+const CHANNEL_TYPE fiveDotOne[MAX_CHANNELS]={ADM_CH_FRONT_LEFT,ADM_CH_FRONT_RIGHT,ADM_CH_FRONT_CENTER,
+                                             ADM_CH_REAR_LEFT,ADM_CH_REAR_RIGHT,ADM_CH_LFE};
+
+const CHANNEL_TYPE *haikuAudioDevice::getWantedChannelMapping(uint32_t channels)
+{
+    switch(channels){
+        case 1:
+        	return mono;
+        	break;
+        case 2:
+        	return stereo;
+        	break;
+        default:
+			return fiveDotOne;
+			break;
+    }
+    return NULL;
+}
+
diff --git a/avidemux_plugins/ADM_audioDevices/Haiku/ADM_deviceHaiku.h b/avidemux_plugins/ADM_audioDevices/Haiku/ADM_deviceHaiku.h
new file mode 100644
index 0000000..2b37e1f
--- /dev/null
+++ b/avidemux_plugins/ADM_audioDevices/Haiku/ADM_deviceHaiku.h
@@ -0,0 +1,24 @@
+#ifndef ADM_deviceHaiku_H
+#define ADM_deviceHaiku_H
+
+#include "SndPlayer.h"
+
+class haikuAudioDevice : public audioDeviceThreaded
+{
+	protected :
+		bool		active;
+
+		SndPlayer	player;
+
+		virtual     bool     localInit(void);
+		virtual     bool     localStop(void);
+		virtual     void     sendData(void);    
+		uint8_t		setVolume(int volume);
+		virtual const CHANNEL_TYPE *getWantedChannelMapping(uint32_t channels);
+	public:
+		haikuAudioDevice();
+		virtual		~haikuAudioDevice();
+};
+
+#endif
+
diff --git a/avidemux_plugins/ADM_audioDevices/Haiku/CMakeLists.txt b/avidemux_plugins/ADM_audioDevices/Haiku/CMakeLists.txt
new file mode 100644
index 0000000..26411b3
--- /dev/null
+++ b/avidemux_plugins/ADM_audioDevices/Haiku/CMakeLists.txt
@@ -0,0 +1,11 @@
+INCLUDE(av_plugin)
+
+
+SET(ADM_av_haiku_SRCS ADM_deviceHaiku.cpp RingBuffer.cpp SndPlayer.cpp)
+
+ADD_AUDIO_DEVICE(ADM_av_haiku  ${ADM_av_haiku_SRCS})
+TARGET_LINK_LIBRARIES(ADM_av_haiku ADM_core6 ADM_coreAudio6 ADM_coreAudioDevice6 media be)
+SDLify(ADM_deviceHaiku.cpp)
+
+INIT_AUDIO_DEVICE(ADM_av_haiku)
+INSTALL_AUDIO_DEVICE(ADM_av_haiku)
diff --git a/avidemux_plugins/ADM_audioDevices/Haiku/RingBuffer.cpp b/avidemux_plugins/ADM_audioDevices/Haiku/RingBuffer.cpp
new file mode 100644
index 0000000..5ffb95a
--- /dev/null
+++ b/avidemux_plugins/ADM_audioDevices/Haiku/RingBuffer.cpp
@@ -0,0 +1,129 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "RingBuffer.h"
+
+RingBuffer::RingBuffer( int size )
+{
+	 initialized = false;
+     Buffer = new unsigned char[size];
+     if(Buffer!=NULL) {
+     	memset( Buffer, 0, size );
+	    BufferSize = size;     	
+     } else {
+     	BufferSize = 0;
+     }
+     reader = 0;
+     writer = 0;
+     writeBytesAvailable = size;
+     if((locker=create_sem(1,"locker")) >= B_OK) {
+     	initialized = true;
+     } else {
+     	if(Buffer!=NULL) {
+     		delete[] Buffer;     		
+     	}
+     }
+}
+
+RingBuffer::~RingBuffer( )
+{
+	 if(initialized) {
+     	delete[] Buffer;
+     	delete_sem(locker);
+	 }
+}
+
+bool 
+RingBuffer::Empty( void )
+{
+     memset( Buffer, 0, BufferSize );
+     reader = 0;
+     writer = 0;
+     writeBytesAvailable = BufferSize;
+     return true;
+}
+
+int 
+RingBuffer::Read( unsigned char *data, int size )
+{	
+	 acquire_sem(locker);
+	 
+     if( data == 0 || size <= 0 || writeBytesAvailable == BufferSize ) {
+     	 release_sem(locker);
+         return 0;
+     }
+
+     int readBytesAvailable = BufferSize - writeBytesAvailable;
+
+     if( size > readBytesAvailable ) {
+         size = readBytesAvailable;
+     }
+
+     if(size > BufferSize - reader) {
+         int len = BufferSize - reader;
+         memcpy(data, Buffer + reader, len);
+         memcpy(data + len, Buffer, size-len);
+     } else {
+         memcpy(data, Buffer + reader, size);
+     }
+
+     reader = (reader + size) % BufferSize;
+     writeBytesAvailable += size;
+	 
+	 release_sem(locker);
+     return size;
+}
+
+int 
+RingBuffer::Write( unsigned char *data, int size )
+{
+	 acquire_sem(locker);
+	 
+     if( data == 0 || size <= 0 || writeBytesAvailable == 0 ) {
+     	 release_sem(locker);
+         return 0;
+     }
+
+     if( size > writeBytesAvailable ) {
+         size = writeBytesAvailable;
+     }
+
+     if(size > BufferSize - writer) {
+         int len = BufferSize - writer;
+         memcpy(Buffer + writer, data, len);
+         memcpy(Buffer, data+len, size-len);
+     } else {
+         memcpy(Buffer + writer, data, size);
+     }
+
+     writer = (writer + size) % BufferSize;
+     writeBytesAvailable -= size;
+
+	 release_sem(locker);	 
+     return size;
+}
+
+int 
+RingBuffer::GetSize( void )
+{
+     return BufferSize;
+}
+
+int 
+RingBuffer::GetWriteAvailable( void )
+{
+     return writeBytesAvailable;
+}
+
+int 
+RingBuffer::GetReadAvailable( void )
+{
+     return BufferSize - writeBytesAvailable;
+}
+
+status_t 
+RingBuffer::InitCheck( void )
+{
+	return initialized?B_OK:B_ERROR;
+}
diff --git a/avidemux_plugins/ADM_audioDevices/Haiku/RingBuffer.h b/avidemux_plugins/ADM_audioDevices/Haiku/RingBuffer.h
new file mode 100644
index 0000000..4715632
--- /dev/null
+++ b/avidemux_plugins/ADM_audioDevices/Haiku/RingBuffer.h
@@ -0,0 +1,31 @@
+#ifndef __RING_BUFFER_H__
+#define __RING_BUFFER_H__
+
+#include <OS.h>
+
+class RingBuffer {
+
+public:
+     RingBuffer(int size);
+     ~RingBuffer();
+     int Read( unsigned char* dataPtr, int numBytes );
+     int Write( unsigned char *dataPtr, int numBytes );
+     
+     bool Empty( void );
+     int GetSize( );
+     int GetWriteAvailable( );
+     int GetReadAvailable( );
+     status_t InitCheck( );
+private:
+     unsigned char *Buffer;
+     int BufferSize;
+     int reader;
+     int writer;
+     int writeBytesAvailable;
+     
+     sem_id locker;
+     
+     bool 	initialized;
+};
+
+#endif
diff --git a/avidemux_plugins/ADM_audioDevices/Haiku/SndPlayer.cpp b/avidemux_plugins/ADM_audioDevices/Haiku/SndPlayer.cpp
new file mode 100644
index 0000000..6d9370e
--- /dev/null
+++ b/avidemux_plugins/ADM_audioDevices/Haiku/SndPlayer.cpp
@@ -0,0 +1,111 @@
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <SoundPlayer.h>
+
+#include "SndPlayer.h"
+
+static void proc(void *cookie, void *buffer, size_t len, const media_raw_audio_format &format)
+{
+	RingBuffer *ring = (RingBuffer*)cookie;
+	unsigned char* ptr = (unsigned char*)buffer;
+
+	size_t readed = ring->Read(ptr,len);
+
+	if(readed < len)
+		memset(ptr+readed, 0, len - readed);
+}
+
+SndPlayer::SndPlayer()
+{	
+	channels = sample_rate = delay = 0;
+	player = NULL;
+	_isOK = true;
+}
+
+bool SndPlayer::start()
+{
+	size_t gSoundBufferSize = 8192 * sizeof(short);
+		
+	media_raw_audio_format form = {
+		sample_rate,
+		channels,
+		media_raw_audio_format::B_AUDIO_SHORT,
+		B_MEDIA_LITTLE_ENDIAN,
+		gSoundBufferSize
+	};
+	
+	ring = new RingBuffer(gSoundBufferSize * 3);
+	if(ring->InitCheck() != B_OK) {
+		delete ring; ring = 0;
+		return false;
+	}
+			
+	player = new BSoundPlayer(&form, "Avidemux", proc, NULL, (void*)ring);
+	
+	if(player->InitCheck() != B_OK) {
+		delete player;
+		player = NULL;
+		return false;
+	}
+	
+	player->Start();
+	player->SetHasData(true);	
+
+	_isOK = true;
+
+	return player != NULL;
+}
+void SndPlayer::stop()
+{
+	if ( player )
+	{
+		if(player) {
+			player->Stop();
+			delete player;
+			delete ring;
+		}
+	
+		player = NULL;
+		ring = NULL;
+	}	
+}
+
+void SndPlayer::flush()
+{
+	ring->Empty();
+}
+
+void SndPlayer::setVolume(int volume)
+{
+	if (player!=NULL) {
+		player->SetVolume(volume / 100.0);
+	}
+}
+
+double SndPlayer::getLatency()
+{
+	double lat = player->Latency() / (ring->GetSize()*4.0);	
+
+	return lat;
+}
+
+bool SndPlayer::write( unsigned char *src_ptr, size_t size)
+{	
+	while ( size > 0 && size % 4 )
+		size--;
+	if ( size <= 0 )
+		return false;
+
+	size_t len = size;
+
+	for(;;) {
+			size_t len2 = ring->Write(src_ptr, len);
+			if (len2 == len)break;
+			len -= len2;
+			src_ptr += len2;
+			snooze(100);
+	}
+
+	return true;
+}
diff --git a/avidemux_plugins/ADM_audioDevices/Haiku/SndPlayer.h b/avidemux_plugins/ADM_audioDevices/Haiku/SndPlayer.h
new file mode 100644
index 0000000..6a6efde
--- /dev/null
+++ b/avidemux_plugins/ADM_audioDevices/Haiku/SndPlayer.h
@@ -0,0 +1,36 @@
+#ifndef SNDPLAY_H
+#define SNDPLAY_H
+
+#include <SoundPlayer.h>
+
+#include "RingBuffer.h"
+
+class SndPlayer
+{
+public:
+	SndPlayer();
+	inline ~SndPlayer() { stop();}
+	inline bool isOK() const {return _isOK;}
+	inline bool isOpen() const {return player!=NULL;}
+
+	bool start();
+	void stop();
+
+	double getLatency();
+
+	bool write( unsigned char *src_ptr, size_t size);
+	
+	void flush();
+	void setVolume(int volume);
+
+	double delay;
+	uchar channels;
+	float sample_rate;
+
+private:
+	bool _isOK;
+	BSoundPlayer *player;
+	RingBuffer *ring;
+};
+
+#endif
diff --git a/avidemux_plugins/ADM_demuxers/AvsProxy/CMakeLists.txt b/avidemux_plugins/ADM_demuxers/AvsProxy/CMakeLists.txt
index 91aab7d..8235f0e 100644
--- a/avidemux_plugins/ADM_demuxers/AvsProxy/CMakeLists.txt
+++ b/avidemux_plugins/ADM_demuxers/AvsProxy/CMakeLists.txt
@@ -9,6 +9,9 @@ ADD_DEMUXER(ADM_dm_avsproxy ${ADM_avsproxy_SRCS})
 if(WIN32)
         target_link_libraries(ADM_dm_avsproxy wsock32)
 endif(WIN32)
+if(HAIKU)
+        target_link_libraries(ADM_dm_avsproxy network)
+endif(HAIKU)
 INIT_DEMUXER(ADM_dm_avsproxy)
 INSTALL_DEMUXER(ADM_dm_avsproxy)
 
diff --git a/cmake/admCheckX265.cmake b/cmake/admCheckX265.cmake
index 2e3f4d9..f9a91e5 100644
--- a/cmake/admCheckX265.cmake
+++ b/cmake/admCheckX265.cmake
@@ -19,7 +19,7 @@ MACRO(checkX265)
 					MESSAGE("WARNING: x265 core version is too old.  At least version 9 is required.")
 					SET(X265_FOUND 0)
 				ELSE (x265_version LESS 9)
-                                        IF(NOT WIN32)
+                                        IF(NOT WIN32 AND NOT HAIKU)
                                                 SET(DL dl)
                                         ELSE(NOT WIN32)
                                                 IF(NOT MSVC)
diff --git a/cmake/admFFmpegBuild.cmake.original b/cmake/admFFmpegBuild.cmake.original
index 43a135c..99da224 100644
--- a/cmake/admFFmpegBuild.cmake.original
+++ b/cmake/admFFmpegBuild.cmake.original
@@ -30,7 +30,7 @@ set(FFMPEG_PARSERS  ac3  h263  h264  hevc  mpeg4video)
 set(FFMPEG_PROTOCOLS  file)
 set(FFMPEG_BSFS h264_mp4toannexb aac_adtstoasc)
 xadd("--enable-shared --disable-static --disable-everything --disable-avfilter --enable-hwaccels --enable-postproc --enable-gpl")
-xadd("--enable-runtime-cpudetect --disable-network ")
+xadd("--disable-runtime-cpudetect --disable-asm --disable-network")
 xadd("--enable-swscale --disable-swresample")
 xadd("--disable-doc --disable-programs")
 
@@ -286,7 +286,7 @@ if (FFMPEG_PERFORM_BUILD)
 
 	MESSAGE(STATUS "Configuring done, processing")
 
-	if (ADM_CPU_X86)
+	if (ADM_CPU_X86 AND NOT HAIKU)
 		file(READ ${FFMPEG_BINARY_DIR}/config.h FF_CONFIG_H)
 		string(REGEX MATCH "#define[ ]+HAVE_YASM[ ]+1" FF_YASM "${FF_CONFIG_H}")
 
diff --git a/cmake/admFFmpegBuild_helpers.cmake b/cmake/admFFmpegBuild_helpers.cmake
index 40796ef..1f5fc39 100644
--- a/cmake/admFFmpegBuild_helpers.cmake
+++ b/cmake/admFFmpegBuild_helpers.cmake
@@ -44,7 +44,7 @@ set(FFMPEG_BSFS         h264_mp4toannexb aac_adtstoasc)
 #
 MACRO(ADM_FF_SET_DEFAULT)
     xadd("--enable-shared --disable-static --disable-everything --disable-avfilter --enable-hwaccels --enable-postproc --enable-gpl")
-    xadd("--enable-runtime-cpudetect --disable-network ")
+    xadd("--disable-runtime-cpudetect --disable-asm --disable-network ")
     xadd("--enable-swscale --disable-swresample")
     xadd("--disable-doc --disable-programs")
 
@@ -295,7 +295,7 @@ MACRO(ADM_FF_BUILD_UNIX_STYLE)
 
     MESSAGE(STATUS "Configuring done, processing")
 
-    if (ADM_CPU_X86)
+    if (ADM_CPU_X86 AND NOT HAIKU)
         file(READ ${FFMPEG_BINARY_DIR}/config.h FF_CONFIG_H)
         string(REGEX MATCH "#define[ ]+HAVE_X86ASM[ ]+1" FF_YASM "${FF_CONFIG_H}")
 
diff --git a/cmake/admFFmpegBuild_native.cmake b/cmake/admFFmpegBuild_native.cmake
index ba4c020..b7638c6 100644
--- a/cmake/admFFmpegBuild_native.cmake
+++ b/cmake/admFFmpegBuild_native.cmake
@@ -47,6 +47,11 @@ if (NOT APPLE AND NOT ADM_CPU_X86_32)
     xadd(--enable-lto)
 endif (NOT APPLE AND NOT ADM_CPU_X86_32)
 
+if (HAIKU)
+	xadd(--disable-runtime-cpudetect)
+	xadd(--disable-asm)
+endif (HAIKU)
+
 #@@
 ADM_FF_ADD_OPTIONS()
 
diff --git a/cmake/cmake_compile_check/execinfo.cpp b/cmake/cmake_compile_check/execinfo.cpp
index 00304cf..07077fa 100644
--- a/cmake/cmake_compile_check/execinfo.cpp
+++ b/cmake/cmake_compile_check/execinfo.cpp
@@ -2,36 +2,8 @@
 #include <stdio.h>
 #include <stdlib.h>
      
-/* Obtain a backtrace and print it to stdout. */
-void
-print_trace (void)
-{
-   void *array[10];
-   size_t size;
-   char **strings;
-   size_t i;
-     
-   size = backtrace (array, 10);
-   strings = backtrace_symbols (array, size);
- 
-   printf ("Obtained %zd stack frames.\n", size);
-     
-   for (i = 0; i < size; i++)
-   printf ("%s\n", strings[i]);
-     
-   free (strings);
-}
-     
-/* A dummy function to make the backtrace more interesting. */
-void
-dummy_function (void)
-{
-   print_trace ();
-}
-     
 int
 main (void)
 {
-   dummy_function ();
    return 0;
 }
-- 
2.30.2

