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"
38 static int concat_probe(AVProbeData *probe)
43 static char *get_keyword(uint8_t **cursor)
45 char *ret = *cursor += strspn(*cursor, SPACE_CHARS);
46 *cursor += strcspn(*cursor, SPACE_CHARS);
49 *cursor += strspn(*cursor, SPACE_CHARS);
54 #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0)
56 static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile,
57 unsigned *nb_files_alloc)
59 ConcatContext *cat = avf->priv_data;
64 url_len = strlen(avf->filename) + strlen(filename) + 16;
65 if (!(url = av_malloc(url_len)))
66 return AVERROR(ENOMEM);
67 ff_make_absolute_url(url, url_len, avf->filename, filename);
70 if (cat->nb_files >= *nb_files_alloc) {
71 unsigned n = FFMAX(*nb_files_alloc * 2, 16);
72 if (n <= cat->nb_files ||
73 !(cat->files = av_realloc_f(cat->files, n, sizeof(*cat->files))))
74 return AVERROR(ENOMEM);
78 file = &cat->files[cat->nb_files++];
79 memset(file, 0, sizeof(*file));
83 file->start_time = AV_NOPTS_VALUE;
84 file->duration = AV_NOPTS_VALUE;
89 static int open_file(AVFormatContext *avf, unsigned fileno)
91 ConcatContext *cat = avf->priv_data;
92 ConcatFile *file = &cat->files[fileno];
95 if ((ret = avformat_open_input(&cat->avf, file->url, NULL, NULL)) < 0 ||
96 (ret = avformat_find_stream_info(cat->avf, NULL)) < 0) {
97 av_log(avf, AV_LOG_ERROR, "Impossible to open '%s'\n", file->url);
100 cat->cur_file = file;
101 if (file->start_time == AV_NOPTS_VALUE)
102 file->start_time = !fileno ? 0 :
103 cat->files[fileno - 1].start_time +
104 cat->files[fileno - 1].duration;
108 static int concat_read_close(AVFormatContext *avf)
110 ConcatContext *cat = avf->priv_data;
114 avformat_close_input(&cat->avf);
115 for (i = 0; i < cat->nb_files; i++)
116 av_freep(&cat->files[i].url);
117 av_freep(&cat->files);
121 static int concat_read_header(AVFormatContext *avf)
123 ConcatContext *cat = avf->priv_data;
125 uint8_t *cursor, *keyword;
126 int ret, line = 0, i;
127 unsigned nb_files_alloc = 0;
128 ConcatFile *file = NULL;
129 AVStream *st, *source_st;
132 if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0)
136 keyword = get_keyword(&cursor);
137 if (!*keyword || *keyword == '#')
140 if (!strcmp(keyword, "file")) {
141 char *filename = av_get_token((const char **)&cursor, SPACE_CHARS);
143 av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line);
144 FAIL(AVERROR_INVALIDDATA);
146 if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0)
149 av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
151 FAIL(AVERROR_INVALIDDATA);
157 if ((ret = open_file(avf, 0)) < 0)
159 for (i = 0; i < cat->avf->nb_streams; i++) {
160 if (!(st = avformat_new_stream(avf, NULL)))
161 FAIL(AVERROR(ENOMEM));
162 source_st = cat->avf->streams[i];
163 if ((ret = avcodec_copy_context(st->codec, source_st->codec)) < 0)
165 st->r_frame_rate = source_st->r_frame_rate;
166 st->avg_frame_rate = source_st->avg_frame_rate;
167 st->time_base = source_st->time_base;
168 st->sample_aspect_ratio = source_st->sample_aspect_ratio;
174 concat_read_close(avf);
178 static int open_next_file(AVFormatContext *avf)
180 ConcatContext *cat = avf->priv_data;
181 unsigned fileno = cat->cur_file - cat->files;
183 if (cat->cur_file->duration == AV_NOPTS_VALUE)
184 cat->cur_file->duration = cat->avf->duration;
186 if (++fileno >= cat->nb_files)
188 avformat_close_input(&cat->avf);
189 return open_file(avf, fileno);
192 static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
194 ConcatContext *cat = avf->priv_data;
199 if ((ret = av_read_frame(cat->avf, pkt)) != AVERROR_EOF ||
200 (ret = open_next_file(avf)) < 0)
203 delta = av_rescale_q(cat->cur_file->start_time - cat->avf->start_time,
205 cat->avf->streams[pkt->stream_index]->time_base);
206 if (pkt->pts != AV_NOPTS_VALUE)
208 if (pkt->dts != AV_NOPTS_VALUE)
213 AVInputFormat ff_concat_demuxer = {
215 .long_name = NULL_IF_CONFIG_SMALL("Virtual concatenation script"),
216 .priv_data_size = sizeof(ConcatContext),
217 .read_probe = concat_probe,
218 .read_header = concat_read_header,
219 .read_packet = concat_read_packet,
220 .read_close = concat_read_close,