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)
46 static char *get_keyword(uint8_t **cursor)
48 char *ret = *cursor += strspn(*cursor, SPACE_CHARS);
49 *cursor += strcspn(*cursor, SPACE_CHARS);
52 *cursor += strspn(*cursor, SPACE_CHARS);
57 static int safe_filename(const char *f)
59 const char *start = f;
63 if (!((unsigned)((*f | 32) - 'a') < 26 ||
64 (unsigned)(*f - '0') < 10 || *f == '_' || *f == '-')) {
76 #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0)
78 static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile,
79 unsigned *nb_files_alloc)
81 ConcatContext *cat = avf->priv_data;
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);
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);
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;
106 file = &cat->files[cat->nb_files++];
107 memset(file, 0, sizeof(*file));
111 file->start_time = AV_NOPTS_VALUE;
112 file->duration = AV_NOPTS_VALUE;
117 static int open_file(AVFormatContext *avf, unsigned fileno)
119 ConcatContext *cat = avf->priv_data;
120 ConcatFile *file = &cat->files[fileno];
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);
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;
136 static int concat_read_close(AVFormatContext *avf)
138 ConcatContext *cat = avf->priv_data;
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);
149 static int concat_read_header(AVFormatContext *avf)
151 ConcatContext *cat = avf->priv_data;
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;
160 if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0)
164 keyword = get_keyword(&cursor);
165 if (!*keyword || *keyword == '#')
168 if (!strcmp(keyword, "file")) {
169 char *filename = av_get_token((const char **)&cursor, SPACE_CHARS);
171 av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line);
172 FAIL(AVERROR_INVALIDDATA);
174 if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0)
177 av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
179 FAIL(AVERROR_INVALIDDATA);
185 if ((ret = open_file(avf, 0)) < 0)
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)
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;
202 concat_read_close(avf);
206 static int open_next_file(AVFormatContext *avf)
208 ConcatContext *cat = avf->priv_data;
209 unsigned fileno = cat->cur_file - cat->files;
211 if (cat->cur_file->duration == AV_NOPTS_VALUE)
212 cat->cur_file->duration = cat->avf->duration;
214 if (++fileno >= cat->nb_files)
216 avformat_close_input(&cat->avf);
217 return open_file(avf, fileno);
220 static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
222 ConcatContext *cat = avf->priv_data;
227 if ((ret = av_read_frame(cat->avf, pkt)) != AVERROR_EOF ||
228 (ret = open_next_file(avf)) < 0)
231 delta = av_rescale_q(cat->cur_file->start_time - cat->avf->start_time,
233 cat->avf->streams[pkt->stream_index]->time_base);
234 if (pkt->pts != AV_NOPTS_VALUE)
236 if (pkt->dts != AV_NOPTS_VALUE)
241 #define OFFSET(x) offsetof(ConcatContext, x)
242 #define DEC AV_OPT_FLAG_DECODING_PARAM
244 static const AVOption options[] = {
245 { "safe", "enable safe mode",
246 OFFSET(safe), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, DEC },
250 static const AVClass concat_class = {
251 .class_name = "concat demuxer",
252 .item_name = av_default_item_name,
254 .version = LIBAVUTIL_VERSION_INT,
258 AVInputFormat ff_concat_demuxer = {
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,