]> git.sesse.net Git - ffmpeg/blob - libavutil/file.c
cpu.h: define AV_CPU_FLAG_MMX2 for libavutil major 52
[ffmpeg] / libavutil / file.c
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include "config.h"
20 #include "file.h"
21 #include "log.h"
22 #include "mem.h"
23 #include <fcntl.h>
24 #include <sys/stat.h>
25 #if HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #if HAVE_MMAP
29 #include <sys/mman.h>
30 #elif HAVE_MAPVIEWOFFILE
31 #include <io.h>
32 #include <windows.h>
33 #endif
34
35 typedef struct {
36     const AVClass *class;
37     int   log_offset;
38     void *log_ctx;
39 } FileLogContext;
40
41 static const AVClass file_log_ctx_class = {
42     "FILE", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT,
43     offsetof(FileLogContext, log_offset), offsetof(FileLogContext, log_ctx)
44 };
45
46 int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
47                 int log_offset, void *log_ctx)
48 {
49     FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
50     int err, fd = open(filename, O_RDONLY);
51     struct stat st;
52     av_unused void *ptr;
53     off_t off_size;
54     char errbuf[128];
55     *bufptr = NULL;
56
57     if (fd < 0) {
58         err = AVERROR(errno);
59         av_strerror(err, errbuf, sizeof(errbuf));
60         av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
61         return err;
62     }
63
64     if (fstat(fd, &st) < 0) {
65         err = AVERROR(errno);
66         av_strerror(err, errbuf, sizeof(errbuf));
67         av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
68         close(fd);
69         return err;
70     }
71
72     off_size = st.st_size;
73     if (off_size > SIZE_MAX) {
74         av_log(&file_log_ctx, AV_LOG_ERROR,
75                "File size for file '%s' is too big\n", filename);
76         close(fd);
77         return AVERROR(EINVAL);
78     }
79     *size = off_size;
80
81 #if HAVE_MMAP
82     ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
83     if (ptr == MAP_FAILED) {
84         err = AVERROR(errno);
85         av_strerror(err, errbuf, sizeof(errbuf));
86         av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
87         close(fd);
88         return err;
89     }
90     *bufptr = ptr;
91 #elif HAVE_MAPVIEWOFFILE
92     {
93         HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
94
95         mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
96         if (!mh) {
97             av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
98             close(fd);
99             return -1;
100         }
101
102         ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
103         CloseHandle(mh);
104         if (!ptr) {
105             av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
106             close(fd);
107             return -1;
108         }
109
110         *bufptr = ptr;
111     }
112 #else
113     *bufptr = av_malloc(*size);
114     if (!*bufptr) {
115         av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
116         close(fd);
117         return AVERROR(ENOMEM);
118     }
119     read(fd, *bufptr, *size);
120 #endif
121
122     close(fd);
123     return 0;
124 }
125
126 void av_file_unmap(uint8_t *bufptr, size_t size)
127 {
128 #if HAVE_MMAP
129     munmap(bufptr, size);
130 #elif HAVE_MAPVIEWOFFILE
131     UnmapViewOfFile(bufptr);
132 #else
133     av_free(bufptr);
134 #endif
135 }