]> granicus.if.org Git - libx264/commitdiff
cli: Prefetch yuv/y4m input frames on Windows 8 and newer
authorHenrik Gramner <henrik@gramner.com>
Thu, 28 Jul 2016 19:58:40 +0000 (21:58 +0200)
committerAnton Mitrofanov <BugMaster@narod.ru>
Tue, 20 Sep 2016 18:01:55 +0000 (21:01 +0300)
Use PrefetchVirtualMemory() (if available) on memory-mapped input frames.

Significantly improves performance when the source file is not already
present in the OS page cache by asking the OS to bring in those pages from
disk using large, concurrent I/O requests.

Most beneficial on fast encoding settings. Up to 40% faster overall with
--preset ultrafast, and up to 20% faster overall with --preset veryfast.

This API was introduced in Windows 8, so call it conditionally. On older
Windows systems the previous behavior remains unchanged.

input/avs.c
input/ffms.c
input/input.c
input/input.h

index 6eab41f91d29a9253c728bc00ea58aa3124f4cff..25732e416c0c320a0521ddaffde5976fed403abb 100644 (file)
@@ -34,7 +34,6 @@
 #define avs_close dlclose
 #define avs_address dlsym
 #else
-#include <windows.h>
 #define avs_open() LoadLibraryW( L"avisynth" )
 #define avs_close FreeLibrary
 #define avs_address GetProcAddress
index 35dc7cac428aad4153b3e122b85d361126c0e87b..a2a87cefd45ef81ce3e59186f3d343a8434ce038 100644 (file)
 #include <libavcodec/avcodec.h>
 #include <libswscale/swscale.h>
 
-#ifdef _WIN32
-#include <windows.h>
-#endif
-
 #define PROGRESS_LENGTH 36
 
 typedef struct
index 82d32c39f52c99677ee58b0e27525a511d11cc06..90083989c18cab2c8959c9461e4a484858f83841 100644 (file)
@@ -28,7 +28,6 @@
 
 #ifdef _WIN32
 #include <io.h>
-#include <windows.h>
 #elif HAVE_MMAP
 #include <sys/mman.h>
 #include <unistd.h>
@@ -154,6 +153,8 @@ int x264_cli_mmap_init( cli_mmap_t *h, FILE *fh )
         SYSTEM_INFO si;
         GetSystemInfo( &si );
         h->align_mask = si.dwAllocationGranularity - 1;
+        h->prefetch_virtual_memory = (void*)GetProcAddress( GetModuleHandleW( L"kernel32.dll" ), "PrefetchVirtualMemory" );
+        h->process_handle = GetCurrentProcess();
         h->map_handle = CreateFileMappingW( osfhandle, NULL, PAGE_READONLY, 0, 0, NULL );
         return !h->map_handle;
     }
@@ -173,9 +174,16 @@ void *x264_cli_mmap( cli_mmap_t *h, int64_t offset, size_t size )
     size   += align;
 #ifdef _WIN32
     uint8_t *base = MapViewOfFile( h->map_handle, FILE_MAP_READ, offset >> 32, offset, size );
-    /* TODO: Would PrefetchVirtualMemory() (only available on Win8+) be beneficial? */
     if( base )
+    {
+        /* PrefetchVirtualMemory() is only available on Windows 8 and newer. */
+        if( h->prefetch_virtual_memory )
+        {
+            struct { void *addr; size_t size; } mem_range = { base, size };
+            h->prefetch_virtual_memory( h->process_handle, 1, &mem_range, 0 );
+        }
         return base + align;
+    }
 #else
     uint8_t *base = mmap( NULL, size, PROT_READ, MAP_PRIVATE, h->fd, offset );
     if( base != MAP_FAILED )
index 9e72bab8afa7108e126722474a7716feca252676..d9a716fd013be1c2c5f2fdb69857622173900812 100644 (file)
 
 #include "x264cli.h"
 
+#ifdef _WIN32
+#include <windows.h>
+#endif
+
 /* options that are used by only some demuxers */
 typedef struct
 {
@@ -135,7 +139,9 @@ typedef struct
 {
     int align_mask;
 #ifdef _WIN32
-    void *map_handle;
+    BOOL (WINAPI *prefetch_virtual_memory)( HANDLE, ULONG_PTR, PVOID, ULONG );
+    HANDLE process_handle;
+    HANDLE map_handle;
 #elif HAVE_MMAP
     int fd;
 #endif