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"
23 #include "libavutil/parseutils.h"
43 static int concat_probe(AVProbeData *probe)
45 return memcmp(probe->buf, "ffconcat version 1.0", 20) ?
46 0 : AVPROBE_SCORE_MAX;
49 static char *get_keyword(uint8_t **cursor)
51 char *ret = *cursor += strspn(*cursor, SPACE_CHARS);
52 *cursor += strcspn(*cursor, SPACE_CHARS);
55 *cursor += strspn(*cursor, SPACE_CHARS);
60 static int safe_filename(const char *f)
62 const char *start = f;
66 if (!((unsigned)((*f | 32) - 'a') < 26 ||
67 (unsigned)(*f - '0') < 10 || *f == '_' || *f == '-')) {
79 #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0)
81 static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile,
82 unsigned *nb_files_alloc)
84 ConcatContext *cat = avf->priv_data;
90 if (cat->safe > 0 && !safe_filename(filename)) {
91 av_log(avf, AV_LOG_ERROR, "Unsafe file name '%s'\n", filename);
94 url_len = strlen(avf->filename) + strlen(filename) + 16;
95 if (!(url = av_malloc(url_len)))
96 FAIL(AVERROR(ENOMEM));
97 ff_make_absolute_url(url, url_len, avf->filename, filename);
100 if (cat->nb_files >= *nb_files_alloc) {
101 size_t n = FFMAX(*nb_files_alloc * 2, 16);
102 ConcatFile *new_files;
103 if (n <= cat->nb_files || n > SIZE_MAX / sizeof(*cat->files) ||
104 !(new_files = av_realloc(cat->files, n * sizeof(*cat->files))))
105 FAIL(AVERROR(ENOMEM));
106 cat->files = new_files;
110 file = &cat->files[cat->nb_files++];
111 memset(file, 0, sizeof(*file));
115 file->start_time = AV_NOPTS_VALUE;
116 file->duration = AV_NOPTS_VALUE;
126 static int open_file(AVFormatContext *avf, unsigned fileno)
128 ConcatContext *cat = avf->priv_data;
129 ConcatFile *file = &cat->files[fileno];
133 avformat_close_input(&cat->avf);
134 if ((ret = avformat_open_input(&cat->avf, file->url, NULL, NULL)) < 0 ||
135 (ret = avformat_find_stream_info(cat->avf, NULL)) < 0) {
136 av_log(avf, AV_LOG_ERROR, "Impossible to open '%s'\n", file->url);
139 cat->cur_file = file;
140 if (file->start_time == AV_NOPTS_VALUE)
141 file->start_time = !fileno ? 0 :
142 cat->files[fileno - 1].start_time +
143 cat->files[fileno - 1].duration;
147 static int concat_read_close(AVFormatContext *avf)
149 ConcatContext *cat = avf->priv_data;
153 avformat_close_input(&cat->avf);
154 for (i = 0; i < cat->nb_files; i++)
155 av_freep(&cat->files[i].url);
156 av_freep(&cat->files);
160 static int concat_read_header(AVFormatContext *avf)
162 ConcatContext *cat = avf->priv_data;
164 uint8_t *cursor, *keyword;
165 int ret, line = 0, i;
166 unsigned nb_files_alloc = 0;
167 ConcatFile *file = NULL;
168 AVStream *st, *source_st;
172 if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0)
176 keyword = get_keyword(&cursor);
177 if (!*keyword || *keyword == '#')
180 if (!strcmp(keyword, "file")) {
181 char *filename = av_get_token((const char **)&cursor, SPACE_CHARS);
183 av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line);
184 FAIL(AVERROR_INVALIDDATA);
186 if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0)
188 } else if (!strcmp(keyword, "duration")) {
189 char *dur_str = get_keyword(&cursor);
192 av_log(avf, AV_LOG_ERROR, "Line %d: duration without file\n",
194 FAIL(AVERROR_INVALIDDATA);
196 if ((ret = av_parse_time(&dur, dur_str, 1)) < 0) {
197 av_log(avf, AV_LOG_ERROR, "Line %d: invalid duration '%s'\n",
201 file->duration = dur;
202 } else if (!strcmp(keyword, "ffconcat")) {
203 char *ver_kw = get_keyword(&cursor);
204 char *ver_val = get_keyword(&cursor);
205 if (strcmp(ver_kw, "version") || strcmp(ver_val, "1.0")) {
206 av_log(avf, AV_LOG_ERROR, "Line %d: invalid version\n", line);
207 FAIL(AVERROR_INVALIDDATA);
212 av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
214 FAIL(AVERROR_INVALIDDATA);
220 FAIL(AVERROR_INVALIDDATA);
222 for (i = 0; i < cat->nb_files; i++) {
223 if (cat->files[i].start_time == AV_NOPTS_VALUE)
224 cat->files[i].start_time = time;
226 time = cat->files[i].start_time;
227 if (cat->files[i].duration == AV_NOPTS_VALUE)
229 time += cat->files[i].duration;
231 if (i == cat->nb_files) {
232 avf->duration = time;
236 if ((ret = open_file(avf, 0)) < 0)
238 for (i = 0; i < cat->avf->nb_streams; i++) {
239 if (!(st = avformat_new_stream(avf, NULL)))
240 FAIL(AVERROR(ENOMEM));
241 source_st = cat->avf->streams[i];
242 if ((ret = avcodec_copy_context(st->codec, source_st->codec)) < 0)
244 st->r_frame_rate = source_st->r_frame_rate;
245 st->avg_frame_rate = source_st->avg_frame_rate;
246 st->time_base = source_st->time_base;
247 st->sample_aspect_ratio = source_st->sample_aspect_ratio;
253 concat_read_close(avf);
257 static int open_next_file(AVFormatContext *avf)
259 ConcatContext *cat = avf->priv_data;
260 unsigned fileno = cat->cur_file - cat->files;
262 if (cat->cur_file->duration == AV_NOPTS_VALUE)
263 cat->cur_file->duration = cat->avf->duration;
265 if (++fileno >= cat->nb_files)
267 return open_file(avf, fileno);
270 static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
272 ConcatContext *cat = avf->priv_data;
277 if ((ret = av_read_frame(cat->avf, pkt)) != AVERROR_EOF ||
278 (ret = open_next_file(avf)) < 0)
281 delta = av_rescale_q(cat->cur_file->start_time - cat->avf->start_time,
283 cat->avf->streams[pkt->stream_index]->time_base);
284 if (pkt->pts != AV_NOPTS_VALUE)
286 if (pkt->dts != AV_NOPTS_VALUE)
291 static void rescale_interval(AVRational tb_in, AVRational tb_out,
292 int64_t *min_ts, int64_t *ts, int64_t *max_ts)
294 *ts = av_rescale_q (* ts, tb_in, tb_out);
295 *min_ts = av_rescale_q_rnd(*min_ts, tb_in, tb_out,
296 AV_ROUND_UP | AV_ROUND_PASS_MINMAX);
297 *max_ts = av_rescale_q_rnd(*max_ts, tb_in, tb_out,
298 AV_ROUND_DOWN | AV_ROUND_PASS_MINMAX);
301 static int try_seek(AVFormatContext *avf, int stream,
302 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
304 ConcatContext *cat = avf->priv_data;
305 int64_t t0 = cat->cur_file->start_time - cat->avf->start_time;
308 min_ts = min_ts == INT64_MIN ? INT64_MIN : min_ts - t0;
309 max_ts = max_ts == INT64_MAX ? INT64_MAX : max_ts - t0;
311 if (stream >= cat->avf->nb_streams)
313 rescale_interval(AV_TIME_BASE_Q, cat->avf->streams[stream]->time_base,
314 &min_ts, &ts, &max_ts);
316 return avformat_seek_file(cat->avf, stream, min_ts, ts, max_ts, flags);
319 static int real_seek(AVFormatContext *avf, int stream,
320 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
322 ConcatContext *cat = avf->priv_data;
323 int ret, left, right;
326 if (stream >= avf->nb_streams)
327 return AVERROR(EINVAL);
328 rescale_interval(avf->streams[stream]->time_base, AV_TIME_BASE_Q,
329 &min_ts, &ts, &max_ts);
333 right = cat->nb_files;
334 while (right - left > 1) {
335 int mid = (left + right) / 2;
336 if (ts < cat->files[mid].start_time)
342 if ((ret = open_file(avf, left)) < 0)
345 ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
347 left < cat->nb_files - 1 &&
348 cat->files[left + 1].start_time < max_ts) {
349 if ((ret = open_file(avf, left + 1)) < 0)
351 ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
356 static int concat_seek(AVFormatContext *avf, int stream,
357 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
359 ConcatContext *cat = avf->priv_data;
360 ConcatFile *cur_file_saved = cat->cur_file;
361 AVFormatContext *cur_avf_saved = cat->avf;
365 return AVERROR(ESPIPE); /* XXX: can we use it? */
366 if (flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME))
367 return AVERROR(ENOSYS);
369 if ((ret = real_seek(avf, stream, min_ts, ts, max_ts, flags)) < 0) {
371 avformat_close_input(&cat->avf);
372 cat->avf = cur_avf_saved;
373 cat->cur_file = cur_file_saved;
375 avformat_close_input(&cur_avf_saved);
380 #define OFFSET(x) offsetof(ConcatContext, x)
381 #define DEC AV_OPT_FLAG_DECODING_PARAM
383 static const AVOption options[] = {
384 { "safe", "enable safe mode",
385 OFFSET(safe), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, DEC },
389 static const AVClass concat_class = {
390 .class_name = "concat demuxer",
391 .item_name = av_default_item_name,
393 .version = LIBAVUTIL_VERSION_INT,
397 AVInputFormat ff_concat_demuxer = {
399 .long_name = NULL_IF_CONFIG_SMALL("Virtual concatenation script"),
400 .priv_data_size = sizeof(ConcatContext),
401 .read_probe = concat_probe,
402 .read_header = concat_read_header,
403 .read_packet = concat_read_packet,
404 .read_close = concat_read_close,
405 .read_seek2 = concat_seek,
406 .priv_class = &concat_class,