From 9d4cfde6c94e2c3400df487a080c2252f58aa166 Mon Sep 17 00:00:00 2001
From: jessejiang <jessejiang@pptv.com>
Date: Thu, 20 Nov 2014 15:32:04 +0800
Subject: [PATCH 1/2] *Add WindowsRT support

---
 compat/w32pthreads.h     |  4 +++-
 compat/winrtapiconvert.h | 42 ++++++++++++++++++++++++++++++++++++++++++
 configure                | 14 +++++++++++++-
 doc/platform.texi        | 10 ++++++++--
 libavutil/file.c         |  3 +++
 libavutil/random_seed.c  |  7 +++++++
 libavutil/time.c         |  3 +++
 7 files changed, 79 insertions(+), 4 deletions(-)
 create mode 100644 compat/winrtapiconvert.h

diff --git a/compat/w32pthreads.h b/compat/w32pthreads.h
index 87e816f..1523850 100644
--- a/compat/w32pthreads.h
+++ b/compat/w32pthreads.h
@@ -38,7 +38,9 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 #include <process.h>
-
+#if HAVE_WINRTAPI
+#include "winrtapiconvert.h"
+#endif
 #include "libavutil/attributes.h"
 #include "libavutil/common.h"
 #include "libavutil/internal.h"
diff --git a/compat/winrtapiconvert.h b/compat/winrtapiconvert.h
new file mode 100644
index 0000000..d7236cb
--- /dev/null
+++ b/compat/winrtapiconvert.h
@@ -0,0 +1,42 @@
+/*
+ * Windows Runtime API convert
+ * Copyright (c) 2014 Jesse Jiang <qyljcy@163.com>
+ * Copyright (c) 2014 PPLive Inc. <http://www.pptv.com>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * WinRT API convert
+ */
+
+#ifndef FFMPEG_COMPAT_WINRTAPICONVERT_H
+#define FFMPEG_COMPAT_WINRTAPICONVERT_H
+#include <Synchapi.h>
+#include <MemoryApi.h>
+
+#define Sleep(x) WaitForSingleObjectEx(GetCurrentProcess(),x,FALSE)
+
+#define _beginthreadex(a, b, c, d, e, f) CreateThread(a, b, c, d, e, f)
+
+// d is dwMaximumSizeHigh ,e is dwMaximumSizeLow as theay are all 0 in codes so CreateFileMappingFromApp should be 0
+#define CreateFileMapping(a, b, c, d, e, f) CreateFileMappingFromApp(a, b, c, e, f) 
+// same as CreateFileMapping
+#define MapViewOfFile(a, b, c, d, e) MapViewOfFileFromApp(a, b, d, e) 
+
+#endif /*FFMPEG_COMPAT_WINRTAPICONVERT_H*/
\ No newline at end of file
diff --git a/configure b/configure
index c0aa718..76c5656 100755
--- a/configure
+++ b/configure
@@ -136,6 +136,7 @@ Component options:
   --disable-pthreads       disable pthreads [auto]
   --disable-w32threads     disable Win32 threads [auto]
   --disable-os2threads     disable OS/2 threads [auto]
+  --disable-winrtapi       disable windows runtime API
   --disable-network        disable network support [no]
   --disable-dct            disable DCT code
   --disable-dwt            disable DWT code
@@ -1823,6 +1824,7 @@ HAVE_LIST="
     threads
     vdpau_x11
     xlib
+    winrtapi
 "
 
 # options emitted with CONFIG_ prefix but not available on the command line
@@ -1895,6 +1897,7 @@ CMDLINE_SELECT="
     optimizations
     rpath
     stripping
+    winrtapi
 "
 
 PATHS_LIST="
@@ -4737,6 +4740,12 @@ check_func_headers windows.h SetConsoleTextAttribute
 check_func_headers windows.h Sleep
 check_func_headers windows.h VirtualAlloc
 check_struct windows.h "CONDITION_VARIABLE" Ptr
+if enabled winrtapi; then
+    disable SetConsoleTextAttribute
+    disable GetProcessAffinityMask
+    disable VirtualAlloc
+fi
+    
 check_func_headers glob.h glob
 enabled xlib &&
     check_func_headers "X11/Xlib.h X11/extensions/Xvlib.h" XvGetPortAttribute -lXv -lX11 -lXext
@@ -4770,7 +4779,10 @@ check_header asm/types.h
 check_lib2 "windows.h shellapi.h" CommandLineToArgvW -lshell32
 check_lib2 "windows.h wincrypt.h" CryptGenRandom -ladvapi32
 check_lib2 "windows.h psapi.h" GetProcessMemoryInfo -lpsapi
-
+if enabled winrtapi; then
+    disable CryptGenRandom
+fi
+    
 check_struct "sys/time.h sys/resource.h" "struct rusage" ru_maxrss
 
 if ! disabled w32threads && ! enabled pthreads; then
diff --git a/doc/platform.texi b/doc/platform.texi
index ca76492..14e39fb 100644
--- a/doc/platform.texi
+++ b/doc/platform.texi
@@ -121,7 +121,7 @@ libavformat) as DLLs.
 
 @end itemize
 
-@section Microsoft Visual C++ or Intel C++ Compiler for Windows
+@section Microsoft Visual C++ or Intel C++ Compiler for Windows or Windows RT
 
 FFmpeg can be built with MSVC 2012 or earlier using a C99-to-C89 conversion utility
 and wrapper, or with MSVC 2013 and ICL natively.
@@ -137,6 +137,8 @@ You will need the following prerequisites:
 @item @uref{http://yasm.tortall.net/, YASM}
 @item @uref{http://gnuwin32.sourceforge.net/packages/bc.htm, bc for Windows} if
 you want to run @uref{fate.html, FATE}.
+@item @uref{https://github.com/FFmpeg/gas-preprocessor/, gas-preprocessor.pl} if
+you want to build ARM version, for Windows Store App ARM or Windows Phone.
 @end itemize
 
 To set up a proper environment in MSYS, you need to run @code{msys.bat} from
@@ -144,7 +146,7 @@ the Visual Studio or Intel Compiler command prompt.
 
 Place @code{yasm.exe} somewhere in your @code{PATH}. If using MSVC 2012 or
 earlier, place @code{c99wrap.exe} and @code{c99conv.exe} somewhere in your
-@code{PATH} as well.
+@code{PATH} as well. Place gas-preprocessor.pl somewhere in your @code{PATH}.
 
 Next, make sure any other headers and libs you want to use, such as zlib, are
 located in a spot that the compiler can see. Do so by modifying the @code{LIB}
@@ -197,6 +199,10 @@ erroneously included when building FFmpeg.
 can see.
 @end enumerate
 
+@item If you wish to compile for Windows RT, for either Windows Store or 
+Windows Phone, add @code{--enable-winrtapi} to your configure options. You must
+use static library for Windows RT.
+
 @item FFmpeg has been tested with the following on i686 and x86_64:
 @itemize
 @item Visual Studio 2010 Pro and Express
diff --git a/libavutil/file.c b/libavutil/file.c
index 359d290..bf8795a 100644
--- a/libavutil/file.c
+++ b/libavutil/file.c
@@ -33,6 +33,9 @@
 #include <sys/mman.h>
 #elif HAVE_MAPVIEWOFFILE
 #include <windows.h>
+#if HAVE_WINRTAPI
+#include "compat/winrtapiconvert.h"
+#endif
 #endif
 
 typedef struct {
diff --git a/libavutil/random_seed.c b/libavutil/random_seed.c
index 8aa8c38..54da5e4 100644
--- a/libavutil/random_seed.c
+++ b/libavutil/random_seed.c
@@ -30,6 +30,9 @@
 #include <windows.h>
 #include <wincrypt.h>
 #endif
+#if HAVE_WINRTAPI
+#include <stdlib.h>
+#endif
 #include <fcntl.h>
 #include <math.h>
 #include <time.h>
@@ -121,6 +124,10 @@ uint32_t av_get_random_seed(void)
     }
 #endif
 
+#if HAVE_WINRTAPI
+	return rand();
+#endif
+
     if (read_random(&seed, "/dev/urandom") == sizeof(seed))
         return seed;
     if (read_random(&seed, "/dev/random")  == sizeof(seed))
diff --git a/libavutil/time.c b/libavutil/time.c
index dbaee02..82c77cd 100644
--- a/libavutil/time.c
+++ b/libavutil/time.c
@@ -31,6 +31,9 @@
 #endif
 #if HAVE_WINDOWS_H
 #include <windows.h>
+#if HAVE_WINRTAPI
+#include "compat/winrtapiconvert.h"
+#endif
 #endif
 
 #include "time.h"
-- 
1.9.0.msysgit.0

