]> git.sesse.net Git - ffmpeg/blob - libavformat/concatdec.c
lavf/microdvd: fix muxing.
[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 memcmp(probe->buf, "ffconcat version 1.0", 20) ?
44            0 : AVPROBE_SCORE_MAX;
45 }
46
47 static char *get_keyword(uint8_t **cursor)
48 {
49     char *ret = *cursor += strspn(*cursor, SPACE_CHARS);
50     *cursor += strcspn(*cursor, SPACE_CHARS);
51     if (**cursor) {
52         *((*cursor)++) = 0;
53         *cursor += strspn(*cursor, SPACE_CHARS);
54     }
55     return ret;
56 }
57
58 static int safe_filename(const char *f)
59 {
60     const char *start = f;
61
62     for (; *f; f++) {
63         /* A-Za-z0-9_- */
64         if (!((unsigned)((*f | 32) - 'a') < 26 ||
65               (unsigned)(*f - '0') < 10 || *f == '_' || *f == '-')) {
66             if (f == start)
67                 return 0;
68             else if (*f == '/')
69                 start = f + 1;
70             else if (*f != '.')
71                 return 0;
72         }
73     }
74     return 1;
75 }
76
77 #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0)
78
79 static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile,
80                     unsigned *nb_files_alloc)
81 {
82     ConcatContext *cat = avf->priv_data;
83     ConcatFile *file;
84     char *url;
85     size_t url_len;
86
87     if (cat->safe > 0 && !safe_filename(filename)) {
88         av_log(avf, AV_LOG_ERROR, "Unsafe file name '%s'\n", filename);
89         return AVERROR(EPERM);
90     }
91     url_len = strlen(avf->filename) + strlen(filename) + 16;
92     if (!(url = av_malloc(url_len)))
93         return AVERROR(ENOMEM);
94     ff_make_absolute_url(url, url_len, avf->filename, filename);
95     av_free(filename);
96
97     if (cat->nb_files >= *nb_files_alloc) {
98         size_t n = FFMAX(*nb_files_alloc * 2, 16);
99         ConcatFile *new_files;
100         if (n <= cat->nb_files || n > SIZE_MAX / sizeof(*cat->files) ||
101             !(new_files = av_realloc(cat->files, n * sizeof(*cat->files))))
102             return AVERROR(ENOMEM);
103         cat->files = new_files;
104         *nb_files_alloc = n;
105     }
106
107     file = &cat->files[cat->nb_files++];
108     memset(file, 0, sizeof(*file));
109     *rfile = file;
110
111     file->url        = url;
112     file->start_time = AV_NOPTS_VALUE;
113     file->duration   = AV_NOPTS_VALUE;
114
115     return 0;
116 }
117
118 static int open_file(AVFormatContext *avf, unsigned fileno)
119 {
120     ConcatContext *cat = avf->priv_data;
121     ConcatFile *file = &cat->files[fileno];
122     int ret;
123
124     if ((ret = avformat_open_input(&cat->avf, file->url, NULL, NULL)) < 0 ||
125         (ret = avformat_find_stream_info(cat->avf, NULL)) < 0) {
126         av_log(avf, AV_LOG_ERROR, "Impossible to open '%s'\n", file->url);
127         return ret;
128     }
129     cat->cur_file = file;
130     if (file->start_time == AV_NOPTS_VALUE)
131         file->start_time = !fileno ? 0 :
132                            cat->files[fileno - 1].start_time +
133                            cat->files[fileno - 1].duration;
134     return 0;
135 }
136
137 static int concat_read_close(AVFormatContext *avf)
138 {
139     ConcatContext *cat = avf->priv_data;
140     unsigned i;
141
142     if (cat->avf)
143         avformat_close_input(&cat->avf);
144     for (i = 0; i < cat->nb_files; i++)
145         av_freep(&cat->files[i].url);
146     av_freep(&cat->files);
147     return 0;
148 }
149
150 static int concat_read_header(AVFormatContext *avf)
151 {
152     ConcatContext *cat = avf->priv_data;
153     uint8_t buf[4096];
154     uint8_t *cursor, *keyword;
155     int ret, line = 0, i;
156     unsigned nb_files_alloc = 0;
157     ConcatFile *file = NULL;
158     AVStream *st, *source_st;
159
160     while (1) {
161         if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0)
162             break;
163         line++;
164         cursor = buf;
165         keyword = get_keyword(&cursor);
166         if (!*keyword || *keyword == '#')
167             continue;
168
169         if (!strcmp(keyword, "file")) {
170             char *filename = av_get_token((const char **)&cursor, SPACE_CHARS);
171             if (!filename) {
172                 av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line);
173                 FAIL(AVERROR_INVALIDDATA);
174             }
175             if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0)
176                 FAIL(ret);
177         } else if (!strcmp(keyword, "ffconcat")) {
178             char *ver_kw  = get_keyword(&cursor);
179             char *ver_val = get_keyword(&cursor);
180             if (strcmp(ver_kw, "version") || strcmp(ver_val, "1.0")) {
181                 av_log(avf, AV_LOG_ERROR, "Line %d: invalid version\n", line);
182                 FAIL(AVERROR_INVALIDDATA);
183             }
184             if (cat->safe < 0)
185                 cat->safe = 1;
186         } else {
187             av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
188                    line, keyword);
189             FAIL(AVERROR_INVALIDDATA);
190         }
191     }
192     if (ret < 0)
193         FAIL(ret);
194
195     if ((ret = open_file(avf, 0)) < 0)
196         FAIL(ret);
197     for (i = 0; i < cat->avf->nb_streams; i++) {
198         if (!(st = avformat_new_stream(avf, NULL)))
199             FAIL(AVERROR(ENOMEM));
200         source_st = cat->avf->streams[i];
201         if ((ret = avcodec_copy_context(st->codec, source_st->codec)) < 0)
202             FAIL(ret);
203         st->r_frame_rate        = source_st->r_frame_rate;
204         st->avg_frame_rate      = source_st->avg_frame_rate;
205         st->time_base           = source_st->time_base;
206         st->sample_aspect_ratio = source_st->sample_aspect_ratio;
207     }
208
209     return 0;
210
211 fail:
212     concat_read_close(avf);
213     return ret;
214 }
215
216 static int open_next_file(AVFormatContext *avf)
217 {
218     ConcatContext *cat = avf->priv_data;
219     unsigned fileno = cat->cur_file - cat->files;
220
221     if (cat->cur_file->duration == AV_NOPTS_VALUE)
222         cat->cur_file->duration = cat->avf->duration;
223
224     if (++fileno >= cat->nb_files)
225         return AVERROR_EOF;
226     avformat_close_input(&cat->avf);
227     return open_file(avf, fileno);
228 }
229
230 static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
231 {
232     ConcatContext *cat = avf->priv_data;
233     int ret;
234     int64_t delta;
235
236     while (1) {
237         if ((ret = av_read_frame(cat->avf, pkt)) != AVERROR_EOF ||
238             (ret = open_next_file(avf)) < 0)
239             break;
240     }
241     delta = av_rescale_q(cat->cur_file->start_time - cat->avf->start_time,
242                          AV_TIME_BASE_Q,
243                          cat->avf->streams[pkt->stream_index]->time_base);
244     if (pkt->pts != AV_NOPTS_VALUE)
245         pkt->pts += delta;
246     if (pkt->dts != AV_NOPTS_VALUE)
247         pkt->dts += delta;
248     return ret;
249 }
250
251 #define OFFSET(x) offsetof(ConcatContext, x)
252 #define DEC AV_OPT_FLAG_DECODING_PARAM
253
254 static const AVOption options[] = {
255     { "safe", "enable safe mode",
256       OFFSET(safe), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, DEC },
257     { NULL }
258 };
259
260 static const AVClass concat_class = {
261     .class_name = "concat demuxer",
262     .item_name  = av_default_item_name,
263     .option     = options,
264     .version    = LIBAVUTIL_VERSION_INT,
265 };
266
267
268 AVInputFormat ff_concat_demuxer = {
269     .name           = "concat",
270     .long_name      = NULL_IF_CONFIG_SMALL("Virtual concatenation script"),
271     .priv_data_size = sizeof(ConcatContext),
272     .read_probe     = concat_probe,
273     .read_header    = concat_read_header,
274     .read_packet    = concat_read_packet,
275     .read_close     = concat_read_close,
276     .priv_class     = &concat_class,
277 };