]> git.sesse.net Git - ffmpeg/blob - libavformat/concatdec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / concatdec.c
1 /*
2  * Copyright (c) 2012 Nicolas George
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "libavutil/avstring.h"
22 #include "avformat.h"
23 #include "internal.h"
24
25 typedef struct {
26     char *url;
27     int64_t start_time;
28     int64_t duration;
29 } ConcatFile;
30
31 typedef struct {
32     ConcatFile *files;
33     ConcatFile *cur_file;
34     unsigned nb_files;
35     AVFormatContext *avf;
36 } ConcatContext;
37
38 static int concat_probe(AVProbeData *probe)
39 {
40     return 0;
41 }
42
43 static char *get_keyword(uint8_t **cursor)
44 {
45     char *ret = *cursor += strspn(*cursor, SPACE_CHARS);
46     *cursor += strcspn(*cursor, SPACE_CHARS);
47     if (**cursor) {
48         *((*cursor)++) = 0;
49         *cursor += strspn(*cursor, SPACE_CHARS);
50     }
51     return ret;
52 }
53
54 #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0)
55
56 static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile,
57                     unsigned *nb_files_alloc)
58 {
59     ConcatContext *cat = avf->priv_data;
60     ConcatFile *file;
61     char *url;
62     size_t url_len;
63
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);
68     av_free(filename);
69
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);
75         *nb_files_alloc = n;
76     }
77
78     file = &cat->files[cat->nb_files++];
79     memset(file, 0, sizeof(*file));
80     *rfile = file;
81
82     file->url        = url;
83     file->start_time = AV_NOPTS_VALUE;
84     file->duration   = AV_NOPTS_VALUE;
85
86     return 0;
87 }
88
89 static int open_file(AVFormatContext *avf, unsigned fileno)
90 {
91     ConcatContext *cat = avf->priv_data;
92     ConcatFile *file = &cat->files[fileno];
93     int ret;
94
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);
98         return ret;
99     }
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;
105     return 0;
106 }
107
108 static int concat_read_close(AVFormatContext *avf)
109 {
110     ConcatContext *cat = avf->priv_data;
111     unsigned i;
112
113     if (cat->avf)
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);
118     return 0;
119 }
120
121 static int concat_read_header(AVFormatContext *avf)
122 {
123     ConcatContext *cat = avf->priv_data;
124     uint8_t buf[4096];
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;
130
131     while (1) {
132         if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0)
133             break;
134         line++;
135         cursor = buf;
136         keyword = get_keyword(&cursor);
137         if (!*keyword || *keyword == '#')
138             continue;
139
140         if (!strcmp(keyword, "file")) {
141             char *filename = av_get_token((const char **)&cursor, SPACE_CHARS);
142             if (!filename) {
143                 av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line);
144                 FAIL(AVERROR_INVALIDDATA);
145             }
146             if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0)
147                 FAIL(ret);
148         } else {
149             av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
150                    line, keyword);
151             FAIL(AVERROR_INVALIDDATA);
152         }
153     }
154     if (ret < 0)
155         FAIL(ret);
156
157     if ((ret = open_file(avf, 0)) < 0)
158         FAIL(ret);
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)
164             FAIL(ret);
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;
169     }
170
171     return 0;
172
173 fail:
174     concat_read_close(avf);
175     return ret;
176 }
177
178 static int open_next_file(AVFormatContext *avf)
179 {
180     ConcatContext *cat = avf->priv_data;
181     unsigned fileno = cat->cur_file - cat->files;
182
183     if (cat->cur_file->duration == AV_NOPTS_VALUE)
184         cat->cur_file->duration = cat->avf->duration;
185
186     if (++fileno >= cat->nb_files)
187         return AVERROR_EOF;
188     avformat_close_input(&cat->avf);
189     return open_file(avf, fileno);
190 }
191
192 static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
193 {
194     ConcatContext *cat = avf->priv_data;
195     int ret;
196     int64_t delta;
197
198     while (1) {
199         if ((ret = av_read_frame(cat->avf, pkt)) != AVERROR_EOF ||
200             (ret = open_next_file(avf)) < 0)
201             break;
202     }
203     delta = av_rescale_q(cat->cur_file->start_time - cat->avf->start_time,
204                          AV_TIME_BASE_Q,
205                          cat->avf->streams[pkt->stream_index]->time_base);
206     if (pkt->pts != AV_NOPTS_VALUE)
207         pkt->pts += delta;
208     if (pkt->dts != AV_NOPTS_VALUE)
209         pkt->dts += delta;
210     return ret;
211 }
212
213 AVInputFormat ff_concat_demuxer = {
214     .name           = "concat",
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,
221 };