FFmpeg
Loading...
Searching...
No Matches
file.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 "error.h"
21#include "file.h"
22#include "file_open.h"
23#include "internal.h"
24#include "log.h"
25#include "mem.h"
26#include <fcntl.h>
27#include <sys/stat.h>
28#if HAVE_UNISTD_H
29#include <unistd.h>
30#endif
31#if HAVE_IO_H
32#include <io.h>
33#endif
34#if HAVE_MMAP
35#include <sys/mman.h>
36#elif HAVE_MAPVIEWOFFILE
37#include <windows.h>
38#endif
39
40#ifdef _WIN32
41/* struct stat has a 32 bit st_size with the Windows SDK, use the 64 bit variants */
42#undef stat
43#undef fstat
44#define stat _stat64
45#define fstat _fstat64
46#endif
47
48typedef struct FileLogContext {
49 const AVClass *class;
51 void *log_ctx;
53
55 .class_name = "FILE",
56 .item_name = av_default_item_name,
57 .option = NULL,
58 .version = LIBAVUTIL_VERSION_INT,
59 .log_level_offset_offset = offsetof(FileLogContext, log_offset),
60 .parent_log_context_offset = offsetof(FileLogContext, log_ctx),
61};
62
63#if HAVE_MMAP || HAVE_MAPVIEWOFFILE
64/* Map the first size bytes of the file, as a private copy or shared and
65 * writable. The shared mapping extends the file to size, on Windows the
66 * mapping object does it. */
67static int map_file(int fd, size_t size, int shared, void **ptr)
68{
69#if HAVE_MMAP
70 if (shared) {
71 struct stat st;
72 if (fstat(fd, &st) < 0)
73 return AVERROR(errno);
74 if (st.st_size < size && ftruncate(fd, size) < 0)
75 return AVERROR(errno);
76 }
77
78 void *map = mmap(NULL, size, PROT_READ | PROT_WRITE,
79 shared ? MAP_SHARED : MAP_PRIVATE, fd, 0);
80 if (map == MAP_FAILED)
81 return AVERROR(errno);
82#else
83 HANDLE fh = (HANDLE)_get_osfhandle(fd);
84 if (fh == INVALID_HANDLE_VALUE)
85 return AVERROR(EBADF);
86
87 HANDLE mh = CreateFileMapping(fh, NULL,
88 shared ? PAGE_READWRITE : PAGE_READONLY,
89 (uint64_t)size >> 32, size, NULL);
90 if (!mh)
91 return AVERROR(EIO);
92
93 void *map = MapViewOfFile(mh, shared ? FILE_MAP_ALL_ACCESS : FILE_MAP_COPY,
94 0, 0, size);
95 CloseHandle(mh);
96 if (!map)
97 return AVERROR(EIO);
98#endif
99
100 *ptr = map;
101 return 0;
102}
103
104static void unmap_file(void *ptr, size_t size)
105{
106#if HAVE_MMAP
107 munmap(ptr, size);
108#else
109 UnmapViewOfFile(ptr);
110#endif
111}
112#endif
113
114int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
115 int log_offset, void *log_ctx)
116{
117 FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
118 int err, fd = avpriv_open(filename, O_RDONLY);
119 struct stat st;
120 av_unused void *ptr;
121 *bufptr = NULL;
122 *size = 0;
123
124 if (fd < 0) {
125 err = AVERROR(errno);
126 av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, av_err2str(err));
127 return err;
128 }
129
130 if (fstat(fd, &st) < 0) {
131 err = AVERROR(errno);
132 av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", av_err2str(err));
133 close(fd);
134 return err;
135 }
136
137 if (st.st_size > SIZE_MAX) {
138 av_log(&file_log_ctx, AV_LOG_ERROR,
139 "File size for file '%s' is too big\n", filename);
140 close(fd);
141 return AVERROR(EINVAL);
142 }
143 *size = st.st_size;
144
145 if (!*size) {
146 *bufptr = NULL;
147 goto out;
148 }
149
150#if HAVE_MMAP || HAVE_MAPVIEWOFFILE
151 err = map_file(fd, *size, 0, &ptr);
152 if (err < 0) {
153 av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot map file '%s': %s\n", filename, av_err2str(err));
154 close(fd);
155 *size = 0;
156 return err;
157 }
158 *bufptr = ptr;
159#else
160 *bufptr = av_malloc(*size);
161 if (!*bufptr) {
162 av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
163 close(fd);
164 *size = 0;
165 return AVERROR(ENOMEM);
166 }
167 read(fd, *bufptr, *size);
168#endif
169
170out:
171 close(fd);
172 return 0;
173}
174
175void av_file_unmap(uint8_t *bufptr, size_t size)
176{
177 if (!size || !bufptr)
178 return;
179#if HAVE_MMAP || HAVE_MAPVIEWOFFILE
180 unmap_file(bufptr, size);
181#else
182 av_free(bufptr);
183#endif
184}
185
186int av_file_map_shared(int fd, size_t size, void **bufptr)
187{
188 *bufptr = NULL;
189 if (!size)
190 return AVERROR(EINVAL);
191#if HAVE_MMAP || HAVE_MAPVIEWOFFILE
192 return map_file(fd, size, 1, bufptr);
193#else
194 return AVERROR(ENOSYS);
195#endif
196}
197
198void av_file_unmap_shared(void *bufptr, size_t size)
199{
200#if HAVE_MMAP || HAVE_MAPVIEWOFFILE
201 if (size && bufptr)
202 unmap_file(bufptr, size);
203#endif
204}
static FILE * out
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
static uint32_t BS_FUNC read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
#define NULL
Definition coverity.c:32
error code definitions
Misc file utilities.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
const VDPAUPixFmtMap * map
#define av_unused
Definition attributes.h:164
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
Definition file.c:175
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size, int log_offset, void *log_ctx)
Read the file with name filename, and put its content in a newly allocated buffer or map it with mmap...
Definition file.c:114
static const AVClass file_log_ctx_class
Definition file.c:54
int av_file_map_shared(int fd, size_t size, void **bufptr)
Map the beginning of an open file into memory for shared read and write access.
Definition file.c:186
void av_file_unmap_shared(void *bufptr, size_t size)
Unmap the memory mapped by av_file_map_shared().
Definition file.c:198
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition file_open.c:67
common internal API header
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
Describe the class of an AVClass context structure.
Definition log.h:76
void * log_ctx
Definition file.c:51
int log_offset
Definition file.c:50
#define av_free(p)
#define av_log(a,...)
int size
#define mh