FFmpeg
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 "file.h"
21 #include "file_open.h"
22 #include "internal.h"
23 #include "log.h"
24 #include "mem.h"
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #if HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30 #if HAVE_IO_H
31 #include <io.h>
32 #endif
33 #if HAVE_MMAP
34 #include <sys/mman.h>
35 #elif HAVE_MAPVIEWOFFILE
36 #include <windows.h>
37 #endif
38 
39 typedef struct FileLogContext {
40  const AVClass *class;
42  void *log_ctx;
44 
45 static const AVClass file_log_ctx_class = {
46  .class_name = "FILE",
47  .item_name = av_default_item_name,
48  .option = NULL,
49  .version = LIBAVUTIL_VERSION_INT,
50  .log_level_offset_offset = offsetof(FileLogContext, log_offset),
51  .parent_log_context_offset = offsetof(FileLogContext, log_ctx),
52 };
53 
54 int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
55  int log_offset, void *log_ctx)
56 {
57  FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
58  int err, fd = avpriv_open(filename, O_RDONLY);
59  struct stat st;
60  av_unused void *ptr;
61  off_t off_size;
62  char errbuf[128];
63  *bufptr = NULL;
64  *size = 0;
65 
66  if (fd < 0) {
67  err = AVERROR(errno);
68  av_strerror(err, errbuf, sizeof(errbuf));
69  av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
70  return err;
71  }
72 
73  if (fstat(fd, &st) < 0) {
74  err = AVERROR(errno);
75  av_strerror(err, errbuf, sizeof(errbuf));
76  av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
77  close(fd);
78  return err;
79  }
80 
81  off_size = st.st_size;
82  if (off_size > SIZE_MAX) {
83  av_log(&file_log_ctx, AV_LOG_ERROR,
84  "File size for file '%s' is too big\n", filename);
85  close(fd);
86  return AVERROR(EINVAL);
87  }
88  *size = off_size;
89 
90  if (!*size) {
91  *bufptr = NULL;
92  goto out;
93  }
94 
95 #if HAVE_MMAP
96  ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
97  if (ptr == MAP_FAILED) {
98  err = AVERROR(errno);
99  av_strerror(err, errbuf, sizeof(errbuf));
100  av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
101  close(fd);
102  *size = 0;
103  return err;
104  }
105  *bufptr = ptr;
106 #elif HAVE_MAPVIEWOFFILE
107  {
108  HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
109 
110  mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
111  if (!mh) {
112  av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
113  close(fd);
114  *size = 0;
115  return -1;
116  }
117 
118  ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
119  CloseHandle(mh);
120  if (!ptr) {
121  av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
122  close(fd);
123  *size = 0;
124  return -1;
125  }
126 
127  *bufptr = ptr;
128  }
129 #else
130  *bufptr = av_malloc(*size);
131  if (!*bufptr) {
132  av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
133  close(fd);
134  *size = 0;
135  return AVERROR(ENOMEM);
136  }
137  read(fd, *bufptr, *size);
138 #endif
139 
140 out:
141  close(fd);
142  return 0;
143 }
144 
145 void av_file_unmap(uint8_t *bufptr, size_t size)
146 {
147  if (!size || !bufptr)
148  return;
149 #if HAVE_MMAP
150  munmap(bufptr, size);
151 #elif HAVE_MAPVIEWOFFILE
152  UnmapViewOfFile(bufptr);
153 #else
154  av_free(bufptr);
155 #endif
156 }
157 
158 #if FF_API_AV_FOPEN_UTF8
159 int av_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx) {
160  return avpriv_tempfile(prefix, filename, log_offset, log_ctx);
161 }
162 #endif
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
out
FILE * out
Definition: movenc.c:54
av_unused
#define av_unused
Definition: attributes.h:131
FileLogContext::log_offset
int log_offset
Definition: file.c:41
mh
#define mh
Definition: vf_colormatrix.c:107
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
av_file_map
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:54
av_strerror
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
Definition: error.c:108
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
avpriv_open
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition: file_open.c:67
file_open.h
av_file_unmap
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
Definition: file.c:145
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
av_tempfile
int av_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx)
Wrapper to work around the lack of mkstemp() on mingw.
Definition: file.c:159
avpriv_tempfile
int avpriv_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx)
Wrapper to work around the lack of mkstemp() on mingw.
Definition: file_open.c:111
size
int size
Definition: twinvq_data.h:10344
FileLogContext
Definition: file.c:39
log.h
internal.h
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
FileLogContext::log_ctx
void * log_ctx
Definition: file.c:42
file_log_ctx_class
static const AVClass file_log_ctx_class
Definition: file.c:45
file.h
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27