]> git.sesse.net Git - ffmpeg/blob - libavformat/file.c
omx: Add support for broadcom OMX on raspberry pi
[ffmpeg] / libavformat / file.c
1 /*
2  * buffered file I/O
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/avstring.h"
23 #include "libavutil/internal.h"
24 #include "libavutil/opt.h"
25 #include "avformat.h"
26 #include <fcntl.h>
27 #if HAVE_IO_H
28 #include <io.h>
29 #endif
30 #if HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #include <sys/stat.h>
34 #include <stdlib.h>
35 #include "os_support.h"
36 #include "url.h"
37
38
39 /* standard file protocol */
40
41 typedef struct FileContext {
42     const AVClass *class;
43     int fd;
44     int trunc;
45     int follow;
46 } FileContext;
47
48 static const AVOption file_options[] = {
49     { "truncate", "Truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
50     { "follow", "Follow a file as it is being written", offsetof(FileContext, follow), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
51     { NULL }
52 };
53
54 static const AVClass file_class = {
55     .class_name = "file",
56     .item_name  = av_default_item_name,
57     .option     = file_options,
58     .version    = LIBAVUTIL_VERSION_INT,
59 };
60
61 static int file_read(URLContext *h, unsigned char *buf, int size)
62 {
63     FileContext *c = h->priv_data;
64     int ret = read(c->fd, buf, size);
65     if (ret == 0 && c->follow)
66         return AVERROR(EAGAIN);
67     return (ret == -1) ? AVERROR(errno) : ret;
68 }
69
70 static int file_write(URLContext *h, const unsigned char *buf, int size)
71 {
72     FileContext *c = h->priv_data;
73     int ret = write(c->fd, buf, size);
74     return (ret == -1) ? AVERROR(errno) : ret;
75 }
76
77 static int file_get_handle(URLContext *h)
78 {
79     FileContext *c = h->priv_data;
80     return c->fd;
81 }
82
83 static int file_check(URLContext *h, int mask)
84 {
85     struct stat st;
86     int ret = stat(h->filename, &st);
87     if (ret < 0)
88         return AVERROR(errno);
89
90     ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ  : 0;
91     ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
92
93     return ret;
94 }
95
96 #if CONFIG_FILE_PROTOCOL
97
98 static int file_open(URLContext *h, const char *filename, int flags)
99 {
100     FileContext *c = h->priv_data;
101     int access;
102     int fd;
103
104     av_strstart(filename, "file:", &filename);
105
106     if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
107         access = O_CREAT | O_RDWR;
108         if (c->trunc)
109             access |= O_TRUNC;
110     } else if (flags & AVIO_FLAG_WRITE) {
111         access = O_CREAT | O_WRONLY;
112         if (c->trunc)
113             access |= O_TRUNC;
114     } else {
115         access = O_RDONLY;
116     }
117 #ifdef O_BINARY
118     access |= O_BINARY;
119 #endif
120     fd = avpriv_open(filename, access, 0666);
121     if (fd == -1)
122         return AVERROR(errno);
123     c->fd = fd;
124     return 0;
125 }
126
127 /* XXX: use llseek */
128 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
129 {
130     FileContext *c = h->priv_data;
131     int64_t ret;
132
133     if (whence == AVSEEK_SIZE) {
134         struct stat st;
135
136         ret = fstat(c->fd, &st);
137         return ret < 0 ? AVERROR(errno) : st.st_size;
138     }
139
140     ret = lseek(c->fd, pos, whence);
141
142     return ret < 0 ? AVERROR(errno) : ret;
143 }
144
145 static int file_close(URLContext *h)
146 {
147     FileContext *c = h->priv_data;
148     return close(c->fd);
149 }
150
151 const URLProtocol ff_file_protocol = {
152     .name                = "file",
153     .url_open            = file_open,
154     .url_read            = file_read,
155     .url_write           = file_write,
156     .url_seek            = file_seek,
157     .url_close           = file_close,
158     .url_get_file_handle = file_get_handle,
159     .url_check           = file_check,
160     .priv_data_size      = sizeof(FileContext),
161     .priv_data_class     = &file_class,
162 };
163
164 #endif /* CONFIG_FILE_PROTOCOL */
165
166 #if CONFIG_PIPE_PROTOCOL
167
168 static int pipe_open(URLContext *h, const char *filename, int flags)
169 {
170     FileContext *c = h->priv_data;
171     int fd;
172     char *final;
173     av_strstart(filename, "pipe:", &filename);
174
175     fd = strtol(filename, &final, 10);
176     if((filename == final) || *final ) {/* No digits found, or something like 10ab */
177         if (flags & AVIO_FLAG_WRITE) {
178             fd = 1;
179         } else {
180             fd = 0;
181         }
182     }
183 #if HAVE_SETMODE
184     setmode(fd, O_BINARY);
185 #endif
186     c->fd = fd;
187     h->is_streamed = 1;
188     return 0;
189 }
190
191 const URLProtocol ff_pipe_protocol = {
192     .name                = "pipe",
193     .url_open            = pipe_open,
194     .url_read            = file_read,
195     .url_write           = file_write,
196     .url_get_file_handle = file_get_handle,
197     .url_check           = file_check,
198     .priv_data_size      = sizeof(FileContext),
199 };
200
201 #endif /* CONFIG_PIPE_PROTOCOL */