[FFmpeg-devel] [PATCH] Windows support for av_file_map()

Daniel Verkamp daniel
Fri Dec 31 10:35:50 CET 2010


Hi,

Here's a simple implementation of av_file_map() using
CreateFileMapping() and MapViewOfFile(), which are the rough Win32
equivalent of mmap().

This currently maps a read-only view of the file; this could be
changed to R/W to match the mmap() path if desired, but any code
expecting to actually use it for writing will fail with the non-mmap()
path anyway since the file is never written back in that case.

Thanks,
-- Daniel Verkamp
-------------- next part --------------
Index: configure
===================================================================
--- configure	(revision 26178)
+++ configure	(working copy)
@@ -1040,6 +1040,7 @@
     machine_ioctl_bt848_h
     machine_ioctl_meteor_h
     malloc_h
+    MapViewOfFile
     memalign
     mkstemp
     mmap
@@ -2685,6 +2686,7 @@
 check_func_headers lzo/lzo1x.h lzo1x_999_compress
 check_lib2 "windows.h psapi.h" GetProcessMemoryInfo -lpsapi
 check_func_headers windows.h GetProcessTimes
+check_func_headers windows.h MapViewOfFile
 check_func_headers windows.h VirtualAlloc
 
 check_header conio.h
Index: libavutil/file.c
===================================================================
--- libavutil/file.c	(revision 26178)
+++ libavutil/file.c	(working copy)
@@ -22,6 +22,9 @@
 #include <unistd.h>
 #if HAVE_MMAP
 #include <sys/mman.h>
+#elif HAVE_MAPVIEWOFFILE
+#include <io.h>
+#include <windows.h>
 #endif
 
 typedef struct {
@@ -81,6 +84,28 @@
         return err;
     }
     *bufptr = ptr;
+#elif HAVE_MAPVIEWOFFILE
+    {
+        HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
+
+        mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
+        if (!mh) {
+            av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
+            close(fd);
+            return -1;
+        }
+
+        ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
+        if (!ptr) {
+            av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
+            CloseHandle(mh);
+            close(fd);
+            return -1;
+        }
+
+        CloseHandle(mh);
+        *bufptr = ptr;
+    }
 #else
     *bufptr = av_malloc(*size);
     if (!*bufptr) {
@@ -99,6 +124,8 @@
 {
 #if HAVE_MMAP
     munmap(bufptr, size);
+#elif HAVE_MAPVIEWOFFILE
+    UnmapViewOfFile(bufptr);
 #else
     av_free(bufptr);
 #endif



More information about the ffmpeg-devel mailing list