2022-07-23  Juan Manuel Guerrero <juan.guerrero@gmx.de>


	* common/osdep.h [_O_BINARY]: Define OS specific setmode stuff.
	[MSDOS_DJGPP]: Include required DJGPP specific headers.
	[MSDOS_DJGPP, __CYGWIN__, _WIN32]: Define for compiler/OS an
	appropriate version of macro SET_BINARY.

	* configure: Set host_cpu, EXE, SYS, HAVE_MALLOC_H, libm and
	ASFLAGS to appropriate values.
	Define new system variable MSDOS_DJGPP that identifies DJGPP.
	Copy the makefile instead of linking it.

	* Makefile: Add SHELL variable.

	* version.sh: Create DJGPP specific release information.

	* x264.c (main_internal): Use SET_BINARY to switch stdin/stdout/stderr
	to binary mode.  For DJGPP, reenable SIGINT and SIGQUIT signals if stdin,
	stdout and/or stderr have been connected to console and have been switched
	into binary mode.

	* execute.c (main): Use SET_BINARY to switch stdin/stdout/stderr to binary
	mode.  For DJGPP, reenable SIGINT and SIGQUIT signals if stdin, stdout
	and/or stderr have been connected to console and have been switched into
	binary mode.







diff -aprNU3 x264-master.orig/common/osdep.h x264-master/common/osdep.h
--- x264-master.orig/common/osdep.h	2022-06-01 22:31:50 +0000
+++ x264-master/common/osdep.h	2022-07-23 12:15:38 +0000
@@ -552,4 +552,50 @@ static ALWAYS_INLINE void x264_prefetch(
 #define x264_prefetch(x)
 #endif
 
+/* We need to set stdin/stdout to binary mode, not only for _WIN32. */
+#if defined(_O_BINARY)
+# define _O_BINARY       O_BINARY
+# define _fileno(f)      fileno(f)
+# define _setmode(f, m)  setmode(f, m)
+#endif /* _O_BINARY */
+
+#if defined(MSDOS_DJGPP)
+# if defined (__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))
+#   define __gnuc_extension__  __extension__
+# else
+#   define __gnuc_extension__
+# endif
+
+# include <inttypes.h>     /* required by x264cli.h*/
+
+# include <io.h>           /* declares setmode() */
+# include <fcntl.h>        /* defines O_BINARY */
+# include <unistd.h>       /* to declare isatty() */
+# include <sys/exceptn.h>  /* to declare __djgpp_set_ctrl_c() */
+  /* This is DJGPP-specific.  By default, switching console
+     to binary mode disables SIGINT and SIGQUIT.  But it
+     is preferable to have terminal reads and writes to
+     be interruptible.  */
+# define SET_BINARY(f)                                          \
+  (__gnuc_extension__                                           \
+    ({                                                          \
+       int file_descriptor = fileno(f);                         \
+       int previous_mode = setmode(file_descriptor, O_BINARY);  \
+       if (isatty(file_descriptor))                             \
+         __djgpp_set_ctrl_c(1);                                 \
+       previous_mode;                                           \
+    })                                                          \
+  )
+#endif /* __DJGPP__ */
+#if defined(_WIN32)
+# define SET_BINARY(f)   (setmode(fileno(f), O_BINARY))
+#endif
+#if defined(__CYGWIN__)
+# define SET_BINARY(f)   /* */
+#endif
+
+#if !defined(SET_BINARY)
+# define SET_BINARY(f)   /* */
+#endif
+
 #endif /* X264_OSDEP_H */
diff -aprNU3 x264-master.orig/configure x264-master/configure
--- x264-master.orig/configure	2022-06-01 22:31:50 +0000
+++ x264-master/configure	2022-07-23 12:15:38 +0000
@@ -625,6 +625,8 @@ if [[ $host_os = mingw* || $host_os = ms
         # MinGW uses broken pre-VS2015 Microsoft printf functions unless it's told to use the POSIX ones.
         CFLAGS="$CFLAGS -D_POSIX_C_SOURCE=200112L"
     fi
+elif [[ $host_os = msdosdjgpp ]]; then
+    host_cpu=i586
 else
     if [[ "$cc_base" = icc || "$cc_base" = icc[\ .]* ]]; then
         AR="xiar"
@@ -729,6 +731,14 @@ case $host_os in
     *haiku*)
         SYS="HAIKU"
         ;;
+    *msdosdjgpp*)
+        EXE=".exe"
+        SYS="MSDOS_DJGPP"
+        HAVE_GETOPT_LONG=0
+        define HAVE_MALLOC_H
+        libm="-lm"
+        CFLAGS="-march=i386 -mtune=i586 $CFLAGS"
+        ;;
     *)
         die "Unknown system $host, edit the configure"
         ;;
@@ -760,6 +770,8 @@ case $host_cpu in
             LDFLAGS="$LDFLAGS -Wl,--large-address-aware"
             [ $compiler = GNU ] && LDFLAGS="$LDFLAGS -Wl,--dynamicbase,--nxcompat,--tsaware"
             [ $compiler = GNU ] && RCFLAGS="--target=pe-i386 $RCFLAGS"
+        elif [ "$SYS" = MSDOS_DJGPP ]; then
+            ASFLAGS="$ASFLAGS -f coff"
         else
             ASFLAGS="$ASFLAGS -f elf32"
         fi
@@ -1037,7 +1049,11 @@ ASFLAGS="$ASFLAGS -DSTACK_ALIGNMENT=$sta
 CPU_ENDIAN="little-endian"
 if [ $compiler = GNU ]; then
     echo "int i[2] = {0x42494745,0}; double f[2] = {0x1.0656e6469616ep+102,0};" > conftest.c
-    $CC $CFLAGS -fno-lto conftest.c -c -o conftest.o 2>/dev/null || die "endian test failed"
+    if [ "$SYS" = MSDOS_DJGPP ]; then
+        $CC $CFLAGS conftest.c -c -o conftest.o 2>/dev/null || die "endian test failed"
+    else
+        $CC $CFLAGS -fno-lto conftest.c -c -o conftest.o 2>/dev/null || die "endian test failed"
+    fi
     if (${STRINGS} -a conftest.o | grep -q BIGE) && (${STRINGS} -a conftest.o | grep -q FPendian) ; then
         define WORDS_BIGENDIAN
         CPU_ENDIAN="big-endian"
@@ -1673,7 +1689,7 @@ echo >> config.log
 cat conftest.log >> config.log
 cat conftest.log
 
-[ "$SRCPATH" != "." ] && ln -sf ${SRCPATH}/Makefile ./Makefile
+[ "$SRCPATH" != "." ] && cp -vf ${SRCPATH}/Makefile ./Makefile
 mkdir -p common/{aarch64,arm,mips,ppc,x86} encoder extras filters/video input output tools
 
 echo
diff -aprNU3 x264-master.orig/example.c x264-master/example.c
--- x264-master.orig/example.c	2022-06-01 22:31:50 +0000
+++ x264-master/example.c	2022-07-23 12:15:38 +0000
@@ -54,11 +54,9 @@ int main( int argc, char **argv )
     x264_nal_t *nal;
     int i_nal;
 
-#ifdef _WIN32
-    _setmode( _fileno( stdin ),  _O_BINARY );
-    _setmode( _fileno( stdout ), _O_BINARY );
-    _setmode( _fileno( stderr ), _O_BINARY );
-#endif
+    SET_BINARY( stdin );
+    SET_BINARY( stdout );
+    SET_BINARY( stderr );
 
     FAIL_IF_ERROR( !(argc > 1), "Example usage: example 352x288 <input.yuv >output.h264\n" );
     FAIL_IF_ERROR( 2 != sscanf( argv[1], "%dx%d", &width, &height ), "resolution not specified or incorrect\n" );
diff -aprNU3 x264-master.orig/Makefile x264-master/Makefile
--- x264-master.orig/Makefile	2022-06-01 22:31:50 +0000
+++ x264-master/Makefile	2022-07-23 12:15:38 +0000
@@ -1,5 +1,7 @@
 # Makefile
 
+SHELL = /bin/sh
+
 include config.mak
 
 vpath %.c $(SRCPATH)
diff -aprNU3 x264-master.orig/version.sh x264-master/version.sh
--- x264-master.orig/version.sh	2022-06-01 22:31:50 +0000
+++ x264-master/version.sh	2022-07-23 12:15:38 +0000
@@ -24,5 +24,17 @@ if [ -d .git ] && command -v git >/dev/n
     fi
 fi
 
+# Create somehow a DJGPP specific identification.
+if [ "x${DJDIR}" != "x" ]; then
+    # version created from last git clone.
+    if [ "x$ver" = "xx" ]; then
+        ver="3095 baee400"
+    fi
+    if [ "x$version" = "x" ]; then
+        version=" r3095 baee400"
+    fi
+    ver="$ver  DJGPP 2.05 (r1)"
+    version="$version  DJGPP 2.05 (r1)"
+fi
 echo "#define X264_VERSION \"$version\""
 echo "#define X264_POINTVER \"0.$api.$ver\""
diff -aprNU3 x264-master.orig/x264.c x264-master/x264.c
--- x264-master.orig/x264.c	2022-06-01 22:31:50 +0000
+++ x264-master/x264.c	2022-07-23 12:15:38 +0000
@@ -38,7 +38,12 @@
 #endif
 
 #include <signal.h>
+#if __DJGPP__
+#include <inttypes.h>       /* required by x264cli.h */
+#include "extras/getopt.h"  /* or fixed by adjusting VPATH */
+#else
 #include <getopt.h>
+#endif
 #include "x264cli.h"
 #include "input/input.h"
 #include "output/output.h"
@@ -370,10 +375,10 @@ REALIGN_STACK int main( int argc, char *
     FAIL_IF_ERROR( !get_argv_utf8( &argc, &argv ), "unable to convert command line to UTF-8\n" );
 
     GetConsoleTitleW( org_console_title, CONSOLE_TITLE_SIZE );
-    _setmode( _fileno( stdin ),  _O_BINARY );
-    _setmode( _fileno( stdout ), _O_BINARY );
-    _setmode( _fileno( stderr ), _O_BINARY );
 #endif
+    SET_BINARY( stdin );
+    SET_BINARY( stdout );
+    SET_BINARY( stderr );
 
     x264_param_default( &param );
     /* Parse command line */
diff -aprNU3 x264-master.orig/x264.h x264-master/x264.h
--- x264-master.orig/x264.h	2022-06-01 22:31:50 +0000
+++ x264-master/x264.h	2022-07-23 12:15:38 +0000
@@ -33,7 +33,8 @@ extern "C" {
 #endif
 
 #if !defined(_STDINT_H) && !defined(_STDINT_H_) && !defined(_STDINT_H_INCLUDED) && !defined(_STDINT) &&\
-    !defined(_SYS_STDINT_H_) && !defined(_INTTYPES_H) && !defined(_INTTYPES_H_) && !defined(_INTTYPES)
+    !defined(_SYS_STDINT_H_) && !defined(_INTTYPES_H) && !defined(_INTTYPES_H_) && !defined(_INTTYPES) &&\
+    !defined(__dj_inttypes__h_)
 # ifdef _MSC_VER
 #  pragma message("You must include stdint.h or inttypes.h before x264.h")
 # else



2022-10-20  Juan Manuel Guerrero <juan.guerrero@gmx.de>


	* execute.c [__DJGPP__]: Include osdep.h to provide SET_BINARY macros.








diff -aprNU3 x264-master.orig/example.c x264-master/example.c
--- x264-master.orig/example.c	2022-10-20 22:57:28 +0000
+++ x264-master/example.c	2022-10-20 23:37:34 +0000
@@ -26,6 +26,8 @@
 #ifdef _WIN32
 #include <io.h>       /* _setmode() */
 #include <fcntl.h>    /* _O_BINARY */
+#elif defined(__DJGPP__)
+#include "common/osdep.h"  /* SET_BINARY */
 #endif
 
 #include <stdint.h>
