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 size_t n = FFMAX(*nb_files_alloc * 2, 16);
72 ConcatFile *new_files;
73 if (n <= cat->nb_files || n > SIZE_MAX / sizeof(*cat->files) ||
74 !(new_files = av_realloc(cat->files, n * sizeof(*cat->files))))
75 return AVERROR(ENOMEM);
76 cat->files = new_files;
80 file = &cat->files[cat->nb_files++];
81 memset(file, 0, sizeof(*file));
85 file->start_time = AV_NOPTS_VALUE;
86 file->duration = AV_NOPTS_VALUE;
91 static int open_file(AVFormatContext *avf, unsigned fileno)
93 ConcatContext *cat = avf->priv_data;
94 ConcatFile *file = &cat->files[fileno];
97 if ((ret = avformat_open_input(&cat->avf, file->url, NULL, NULL)) < 0 ||
98 (ret = avformat_find_stream_info(cat->avf, NULL)) < 0) {
99 av_log(avf, AV_LOG_ERROR, "Impossible to open '%s'\n", file->url);
102 cat->cur_file = file;
103 if (file->start_time == AV_NOPTS_VALUE)
104 file->start_time = !fileno ? 0 :
105 cat->files[fileno - 1].start_time +
106 cat->files[fileno - 1].duration;
110 static int concat_read_close(AVFormatContext *avf)
112 ConcatContext *cat = avf->priv_data;
116 avformat_close_input(&cat->avf);
117 for (i = 0; i < cat->nb_files; i++)
118 av_freep(&cat->files[i].url);
119 av_freep(&cat->files);
123 static int concat_read_header(AVFormatContext *avf)
125 ConcatContext *cat = avf->priv_data;
127 uint8_t *cursor, *keyword;
128 int ret, line = 0, i;
129 unsigned nb_files_alloc = 0;
130 ConcatFile *file = NULL;
131 AVStream *st, *source_st;
134 if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0)
138 keyword = get_keyword(&cursor);
139 if (!*keyword || *keyword == '#')
142 if (!strcmp(keyword, "file")) {
143 char *filename = av_get_token((const char **)&cursor, SPACE_CHARS);
145 av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line);
146 FAIL(AVERROR_INVALIDDATA);
148 if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0)
151 av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
153 FAIL(AVERROR_INVALIDDATA);
159 if ((ret = open_file(avf, 0)) < 0)
161 for (i = 0; i < cat->avf->nb_streams; i++) {
162 if (!(st = avformat_new_stream(avf, NULL)))
163 FAIL(AVERROR(ENOMEM));
164 source_st = cat->avf->streams[i];
165 if ((ret = avcodec_copy_context(st->codec, source_st->codec)) < 0)
167 st->r_frame_rate = source_st->r_frame_rate;
168 st->avg_frame_rate = source_st->avg_frame_rate;
169 st->time_base = source_st->time_base;
170 st->sample_aspect_ratio = source_st->sample_aspect_ratio;
176 concat_read_close(avf);
180 static int open_next_file(AVFormatContext *avf)
182 ConcatContext *cat = avf->priv_data;
183 unsigned fileno = cat->cur_file - cat->files;
185 if (cat->cur_file->duration == AV_NOPTS_VALUE)
186 cat->cur_file->duration = cat->avf->duration;
188 if (++fileno >= cat->nb_files)
190 avformat_close_input(&cat->avf);
191 return open_file(avf, fileno);
194 static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
196 ConcatContext *cat = avf->priv_data;
201 if ((ret = av_read_frame(cat->avf, pkt)) != AVERROR_EOF ||
202 (ret = open_next_file(avf)) < 0)
205 delta = av_rescale_q(cat->cur_file->start_time - cat->avf->start_time,
207 cat->avf->streams[pkt->stream_index]->time_base);
208 if (pkt->pts != AV_NOPTS_VALUE)
210 if (pkt->dts != AV_NOPTS_VALUE)
215 AVInputFormat ff_concat_demuxer = {
217 .long_name = NULL_IF_CONFIG_SMALL("Virtual concatenation script"),
218 .priv_data_size = sizeof(ConcatContext),
219 .read_probe = concat_probe,
220 .read_header = concat_read_header,
221 .read_packet = concat_read_packet,
222 .read_close = concat_read_close,