]> git.sesse.net Git - ffmpeg/blob - libavformat/file.c
lavc decoders: properly initialize AVFrame.
[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/opt.h"
24 #include "avformat.h"
25 #include <fcntl.h>
26 #if HAVE_IO_H
27 #include <io.h>
28 #endif
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #include <sys/stat.h>
33 #include <stdlib.h>
34 #include "os_support.h"
35 #include "url.h"
36
37
38 /* standard file protocol */
39
40 typedef struct FileContext {
41     const AVClass *class;
42     int fd;
43     int trunc;
44 } FileContext;
45
46 static const AVOption file_options[] = {
47     { "truncate", "Truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
48     { NULL }
49 };
50
51 static const AVClass file_class = {
52     .class_name = "file",
53     .item_name  = av_default_item_name,
54     .option     = file_options,
55     .version    = LIBAVUTIL_VERSION_INT,
56 };
57
58 static int file_read(URLContext *h, unsigned char *buf, int size)
59 {
60     FileContext *c = h->priv_data;
61     return read(c->fd, buf, size);
62 }
63
64 static int file_write(URLContext *h, const unsigned char *buf, int size)
65 {
66     FileContext *c = h->priv_data;
67     return write(c->fd, buf, size);
68 }
69
70 static int file_get_handle(URLContext *h)
71 {
72     FileContext *c = h->priv_data;
73     return c->fd;
74 }
75
76 static int file_check(URLContext *h, int mask)
77 {
78     struct stat st;
79     int ret = stat(h->filename, &st);
80     if (ret < 0)
81         return AVERROR(errno);
82
83     ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ  : 0;
84     ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
85
86     return ret;
87 }
88
89 #if CONFIG_FILE_PROTOCOL
90
91 static int file_open(URLContext *h, const char *filename, int flags)
92 {
93     FileContext *c = h->priv_data;
94     int access;
95     int fd;
96
97     av_strstart(filename, "file:", &filename);
98
99     if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
100         access = O_CREAT | O_RDWR;
101         if (c->trunc)
102             access |= O_TRUNC;
103     } else if (flags & AVIO_FLAG_WRITE) {
104         access = O_CREAT | O_WRONLY;
105         if (c->trunc)
106             access |= O_TRUNC;
107     } else {
108         access = O_RDONLY;
109     }
110 #ifdef O_BINARY
111     access |= O_BINARY;
112 #endif
113     fd = open(filename, access, 0666);
114     if (fd == -1)
115         return AVERROR(errno);
116     c->fd = fd;
117     return 0;
118 }
119
120 /* XXX: use llseek */
121 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
122 {
123     FileContext *c = h->priv_data;
124     int64_t ret;
125
126     if (whence == AVSEEK_SIZE) {
127         struct stat st;
128
129         ret = fstat(c->fd, &st);
130         return ret < 0 ? AVERROR(errno) : st.st_size;
131     }
132
133     ret = lseek(c->fd, pos, whence);
134
135     return ret < 0 ? AVERROR(errno) : ret;
136 }
137
138 static int file_close(URLContext *h)
139 {
140     FileContext *c = h->priv_data;
141     return close(c->fd);
142 }
143
144 URLProtocol ff_file_protocol = {
145     .name                = "file",
146     .url_open            = file_open,
147     .url_read            = file_read,
148     .url_write           = file_write,
149     .url_seek            = file_seek,
150     .url_close           = file_close,
151     .url_get_file_handle = file_get_handle,
152     .url_check           = file_check,
153     .priv_data_size      = sizeof(FileContext),
154     .priv_data_class     = &file_class,
155 };
156
157 #endif /* CONFIG_FILE_PROTOCOL */
158
159 #if CONFIG_PIPE_PROTOCOL
160
161 static int pipe_open(URLContext *h, const char *filename, int flags)
162 {
163     FileContext *c = h->priv_data;
164     int fd;
165     char *final;
166     av_strstart(filename, "pipe:", &filename);
167
168     fd = strtol(filename, &final, 10);
169     if((filename == final) || *final ) {/* No digits found, or something like 10ab */
170         if (flags & AVIO_FLAG_WRITE) {
171             fd = 1;
172         } else {
173             fd = 0;
174         }
175     }
176 #if HAVE_SETMODE
177     setmode(fd, O_BINARY);
178 #endif
179     c->fd = fd;
180     h->is_streamed = 1;
181     return 0;
182 }
183
184 URLProtocol ff_pipe_protocol = {
185     .name                = "pipe",
186     .url_open            = pipe_open,
187     .url_read            = file_read,
188     .url_write           = file_write,
189     .url_get_file_handle = file_get_handle,
190     .url_check           = file_check,
191     .priv_data_size      = sizeof(FileContext),
192 };
193
194 #endif /* CONFIG_PIPE_PROTOCOL */