2 * Copyright (c) 2012 Nicolas George
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/avstring.h"
22 #include "libavutil/opt.h"
41 static int concat_probe(AVProbeData *probe)
43 return memcmp(probe->buf, "ffconcat version 1.0", 20) ?
44 0 : AVPROBE_SCORE_MAX;
47 static char *get_keyword(uint8_t **cursor)
49 char *ret = *cursor += strspn(*cursor, SPACE_CHARS);
50 *cursor += strcspn(*cursor, SPACE_CHARS);
53 *cursor += strspn(*cursor, SPACE_CHARS);
58 static int safe_filename(const char *f)
60 const char *start = f;
64 if (!((unsigned)((*f | 32) - 'a') < 26 ||
65 (unsigned)(*f - '0') < 10 || *f == '_' || *f == '-')) {
77 #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0)
79 static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile,
80 unsigned *nb_files_alloc)
82 ConcatContext *cat = avf->priv_data;
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);
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);
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;
107 file = &cat->files[cat->nb_files++];
108 memset(file, 0, sizeof(*file));
112 file->start_time = AV_NOPTS_VALUE;
113 file->duration = AV_NOPTS_VALUE;
118 static int open_file(AVFormatContext *avf, unsigned fileno)
120 ConcatContext *cat = avf->priv_data;
121 ConcatFile *file = &cat->files[fileno];
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);
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;
137 static int concat_read_close(AVFormatContext *avf)
139 ConcatContext *cat = avf->priv_data;
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);
150 static int concat_read_header(AVFormatContext *avf)
152 ConcatContext *cat = avf->priv_data;
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;
161 if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0)
165 keyword = get_keyword(&cursor);
166 if (!*keyword || *keyword == '#')
169 if (!strcmp(keyword, "file")) {
170 char *filename = av_get_token((const char **)&cursor, SPACE_CHARS);
172 av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line);
173 FAIL(AVERROR_INVALIDDATA);
175 if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0)
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);
187 av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
189 FAIL(AVERROR_INVALIDDATA);
195 if ((ret = open_file(avf, 0)) < 0)
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)
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;
212 concat_read_close(avf);
216 static int open_next_file(AVFormatContext *avf)
218 ConcatContext *cat = avf->priv_data;
219 unsigned fileno = cat->cur_file - cat->files;
221 if (cat->cur_file->duration == AV_NOPTS_VALUE)
222 cat->cur_file->duration = cat->avf->duration;
224 if (++fileno >= cat->nb_files)
226 avformat_close_input(&cat->avf);
227 return open_file(avf, fileno);
230 static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
232 ConcatContext *cat = avf->priv_data;
237 if ((ret = av_read_frame(cat->avf, pkt)) != AVERROR_EOF ||
238 (ret = open_next_file(avf)) < 0)
241 delta = av_rescale_q(cat->cur_file->start_time - cat->avf->start_time,
243 cat->avf->streams[pkt->stream_index]->time_base);
244 if (pkt->pts != AV_NOPTS_VALUE)
246 if (pkt->dts != AV_NOPTS_VALUE)
251 #define OFFSET(x) offsetof(ConcatContext, x)
252 #define DEC AV_OPT_FLAG_DECODING_PARAM
254 static const AVOption options[] = {
255 { "safe", "enable safe mode",
256 OFFSET(safe), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, DEC },
260 static const AVClass concat_class = {
261 .class_name = "concat demuxer",
262 .item_name = av_default_item_name,
264 .version = LIBAVUTIL_VERSION_INT,
268 AVInputFormat ff_concat_demuxer = {
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,