]> git.sesse.net Git - ffmpeg/blob - libavformat/concatdec.c
bba99d8e669dc5a7a050a6d7b40ee583d70ebe4e
[ffmpeg] / libavformat / concatdec.c
1 /*
2  * Copyright (c) 2012 Nicolas George
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "libavutil/avstring.h"
22 #include "libavutil/opt.h"
23 #include "avformat.h"
24 #include "internal.h"
25
26 typedef struct {
27     char *url;
28     int64_t start_time;
29     int64_t duration;
30 } ConcatFile;
31
32 typedef struct {
33     AVClass *class;
34     ConcatFile *files;
35     ConcatFile *cur_file;
36     unsigned nb_files;
37     AVFormatContext *avf;
38     int safe;
39 } ConcatContext;
40
41 static int concat_probe(AVProbeData *probe)
42 {
43     return 0;
44 }
45
46 static char *get_keyword(uint8_t **cursor)
47 {
48     char *ret = *cursor += strspn(*cursor, SPACE_CHARS);
49     *cursor += strcspn(*cursor, SPACE_CHARS);
50     if (**cursor) {
51         *((*cursor)++) = 0;
52         *cursor += strspn(*cursor, SPACE_CHARS);
53     }
54     return ret;
55 }
56
57 static int safe_filename(const char *f)
58 {
59     const char *start = f;
60
61     for (; *f; f++) {
62         /* A-Za-z0-9_- */
63         if (!((unsigned)((*f | 32) - 'a') < 26 ||
64               (unsigned)(*f - '0') < 10 || *f == '_' || *f == '-')) {
65             if (f == start)
66                 return 0;
67             else if (*f == '/')
68                 start = f + 1;
69             else if (*f != '.')
70                 return 0;
71         }
72     }
73     return 1;
74 }
75
76 #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0)
77
78 static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile,
79                     unsigned *nb_files_alloc)
80 {
81     ConcatContext *cat = avf->priv_data;
82     ConcatFile *file;
83     char *url;
84     size_t url_len;
85
86     if (cat->safe > 0 && !safe_filename(filename)) {
87         av_log(avf, AV_LOG_ERROR, "Unsafe file name '%s'\n", filename);
88         return AVERROR(EPERM);
89     }
90     url_len = strlen(avf->filename) + strlen(filename) + 16;
91     if (!(url = av_malloc(url_len)))
92         return AVERROR(ENOMEM);
93     ff_make_absolute_url(url, url_len, avf->filename, filename);
94     av_free(filename);
95
96     if (cat->nb_files >= *nb_files_alloc) {
97         size_t n = FFMAX(*nb_files_alloc * 2, 16);
98         ConcatFile *new_files;
99         if (n <= cat->nb_files || n > SIZE_MAX / sizeof(*cat->files) ||
100             !(new_files = av_realloc(cat->files, n * sizeof(*cat->files))))
101             return AVERROR(ENOMEM);
102         cat->files = new_files;
103         *nb_files_alloc = n;
104     }
105
106     file = &cat->files[cat->nb_files++];
107     memset(file, 0, sizeof(*file));
108     *rfile = file;
109
110     file->url        = url;
111     file->start_time = AV_NOPTS_VALUE;
112     file->duration   = AV_NOPTS_VALUE;
113
114     return 0;
115 }
116
117 static int open_file(AVFormatContext *avf, unsigned fileno)
118 {
119     ConcatContext *cat = avf->priv_data;
120     ConcatFile *file = &cat->files[fileno];
121     int ret;
122
123     if ((ret = avformat_open_input(&cat->avf, file->url, NULL, NULL)) < 0 ||
124         (ret = avformat_find_stream_info(cat->avf, NULL)) < 0) {
125         av_log(avf, AV_LOG_ERROR, "Impossible to open '%s'\n", file->url);
126         return ret;
127     }
128     cat->cur_file = file;
129     if (file->start_time == AV_NOPTS_VALUE)
130         file->start_time = !fileno ? 0 :
131                            cat->files[fileno - 1].start_time +
132                            cat->files[fileno - 1].duration;
133     return 0;
134 }
135
136 static int concat_read_close(AVFormatContext *avf)
137 {
138     ConcatContext *cat = avf->priv_data;
139     unsigned i;
140
141     if (cat->avf)
142         avformat_close_input(&cat->avf);
143     for (i = 0; i < cat->nb_files; i++)
144         av_freep(&cat->files[i].url);
145     av_freep(&cat->files);
146     return 0;
147 }
148
149 static int concat_read_header(AVFormatContext *avf)
150 {
151     ConcatContext *cat = avf->priv_data;
152     uint8_t buf[4096];
153     uint8_t *cursor, *keyword;
154     int ret, line = 0, i;
155     unsigned nb_files_alloc = 0;
156     ConcatFile *file = NULL;
157     AVStream *st, *source_st;
158
159     while (1) {
160         if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0)
161             break;
162         line++;
163         cursor = buf;
164         keyword = get_keyword(&cursor);
165         if (!*keyword || *keyword == '#')
166             continue;
167
168         if (!strcmp(keyword, "file")) {
169             char *filename = av_get_token((const char **)&cursor, SPACE_CHARS);
170             if (!filename) {
171                 av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line);
172                 FAIL(AVERROR_INVALIDDATA);
173             }
174             if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0)
175                 FAIL(ret);
176         } else {
177             av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
178                    line, keyword);
179             FAIL(AVERROR_INVALIDDATA);
180         }
181     }
182     if (ret < 0)
183         FAIL(ret);
184
185     if ((ret = open_file(avf, 0)) < 0)
186         FAIL(ret);
187     for (i = 0; i < cat->avf->nb_streams; i++) {
188         if (!(st = avformat_new_stream(avf, NULL)))
189             FAIL(AVERROR(ENOMEM));
190         source_st = cat->avf->streams[i];
191         if ((ret = avcodec_copy_context(st->codec, source_st->codec)) < 0)
192             FAIL(ret);
193         st->r_frame_rate        = source_st->r_frame_rate;
194         st->avg_frame_rate      = source_st->avg_frame_rate;
195         st->time_base           = source_st->time_base;
196         st->sample_aspect_ratio = source_st->sample_aspect_ratio;
197     }
198
199     return 0;
200
201 fail:
202     concat_read_close(avf);
203     return ret;
204 }
205
206 static int open_next_file(AVFormatContext *avf)
207 {
208     ConcatContext *cat = avf->priv_data;
209     unsigned fileno = cat->cur_file - cat->files;
210
211     if (cat->cur_file->duration == AV_NOPTS_VALUE)
212         cat->cur_file->duration = cat->avf->duration;
213
214     if (++fileno >= cat->nb_files)
215         return AVERROR_EOF;
216     avformat_close_input(&cat->avf);
217     return open_file(avf, fileno);
218 }
219
220 static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
221 {
222     ConcatContext *cat = avf->priv_data;
223     int ret;
224     int64_t delta;
225
226     while (1) {
227         if ((ret = av_read_frame(cat->avf, pkt)) != AVERROR_EOF ||
228             (ret = open_next_file(avf)) < 0)
229             break;
230     }
231     delta = av_rescale_q(cat->cur_file->start_time - cat->avf->start_time,
232                          AV_TIME_BASE_Q,
233                          cat->avf->streams[pkt->stream_index]->time_base);
234     if (pkt->pts != AV_NOPTS_VALUE)
235         pkt->pts += delta;
236     if (pkt->dts != AV_NOPTS_VALUE)
237         pkt->dts += delta;
238     return ret;
239 }
240
241 #define OFFSET(x) offsetof(ConcatContext, x)
242 #define DEC AV_OPT_FLAG_DECODING_PARAM
243
244 static const AVOption options[] = {
245     { "safe", "enable safe mode",
246       OFFSET(safe), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, DEC },
247     { NULL }
248 };
249
250 static const AVClass concat_class = {
251     .class_name = "concat demuxer",
252     .item_name  = av_default_item_name,
253     .option     = options,
254     .version    = LIBAVUTIL_VERSION_INT,
255 };
256
257
258 AVInputFormat ff_concat_demuxer = {
259     .name           = "concat",
260     .long_name      = NULL_IF_CONFIG_SMALL("Virtual concatenation script"),
261     .priv_data_size = sizeof(ConcatContext),
262     .read_probe     = concat_probe,
263     .read_header    = concat_read_header,
264     .read_packet    = concat_read_packet,
265     .read_close     = concat_read_close,
266     .priv_class     = &concat_class,
267 };