FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
concatdec.c
Go to the documentation of this file.
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 "libavutil/opt.h"
23 #include "libavutil/parseutils.h"
24 #include "avformat.h"
25 #include "internal.h"
26 
27 typedef struct {
28  char *url;
29  int64_t start_time;
30  int64_t duration;
31 } ConcatFile;
32 
33 typedef struct {
34  AVClass *class;
37  unsigned nb_files;
39  int safe;
41 
43 {
44  return memcmp(probe->buf, "ffconcat version 1.0", 20) ?
46 }
47 
48 static char *get_keyword(uint8_t **cursor)
49 {
50  char *ret = *cursor += strspn(*cursor, SPACE_CHARS);
51  *cursor += strcspn(*cursor, SPACE_CHARS);
52  if (**cursor) {
53  *((*cursor)++) = 0;
54  *cursor += strspn(*cursor, SPACE_CHARS);
55  }
56  return ret;
57 }
58 
59 static int safe_filename(const char *f)
60 {
61  const char *start = f;
62 
63  for (; *f; f++) {
64  /* A-Za-z0-9_- */
65  if (!((unsigned)((*f | 32) - 'a') < 26 ||
66  (unsigned)(*f - '0') < 10 || *f == '_' || *f == '-')) {
67  if (f == start)
68  return 0;
69  else if (*f == '/')
70  start = f + 1;
71  else if (*f != '.')
72  return 0;
73  }
74  }
75  return 1;
76 }
77 
78 #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0)
79 
80 static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile,
81  unsigned *nb_files_alloc)
82 {
83  ConcatContext *cat = avf->priv_data;
84  ConcatFile *file;
85  char *url;
86  size_t url_len;
87 
88  if (cat->safe > 0 && !safe_filename(filename)) {
89  av_log(avf, AV_LOG_ERROR, "Unsafe file name '%s'\n", filename);
90  return AVERROR(EPERM);
91  }
92  url_len = strlen(avf->filename) + strlen(filename) + 16;
93  if (!(url = av_malloc(url_len)))
94  return AVERROR(ENOMEM);
95  ff_make_absolute_url(url, url_len, avf->filename, filename);
96  av_free(filename);
97 
98  if (cat->nb_files >= *nb_files_alloc) {
99  size_t n = FFMAX(*nb_files_alloc * 2, 16);
100  ConcatFile *new_files;
101  if (n <= cat->nb_files || n > SIZE_MAX / sizeof(*cat->files) ||
102  !(new_files = av_realloc(cat->files, n * sizeof(*cat->files))))
103  return AVERROR(ENOMEM);
104  cat->files = new_files;
105  *nb_files_alloc = n;
106  }
107 
108  file = &cat->files[cat->nb_files++];
109  memset(file, 0, sizeof(*file));
110  *rfile = file;
111 
112  file->url = url;
113  file->start_time = AV_NOPTS_VALUE;
114  file->duration = AV_NOPTS_VALUE;
115 
116  return 0;
117 }
118 
119 static int open_file(AVFormatContext *avf, unsigned fileno)
120 {
121  ConcatContext *cat = avf->priv_data;
122  ConcatFile *file = &cat->files[fileno];
123  int ret;
124 
125  if ((ret = avformat_open_input(&cat->avf, file->url, NULL, NULL)) < 0 ||
126  (ret = avformat_find_stream_info(cat->avf, NULL)) < 0) {
127  av_log(avf, AV_LOG_ERROR, "Impossible to open '%s'\n", file->url);
128  return ret;
129  }
130  cat->cur_file = file;
131  if (file->start_time == AV_NOPTS_VALUE)
132  file->start_time = !fileno ? 0 :
133  cat->files[fileno - 1].start_time +
134  cat->files[fileno - 1].duration;
135  return 0;
136 }
137 
139 {
140  ConcatContext *cat = avf->priv_data;
141  unsigned i;
142 
143  if (cat->avf)
144  avformat_close_input(&cat->avf);
145  for (i = 0; i < cat->nb_files; i++)
146  av_freep(&cat->files[i].url);
147  av_freep(&cat->files);
148  return 0;
149 }
150 
152 {
153  ConcatContext *cat = avf->priv_data;
154  uint8_t buf[4096];
155  uint8_t *cursor, *keyword;
156  int ret, line = 0, i;
157  unsigned nb_files_alloc = 0;
158  ConcatFile *file = NULL;
159  AVStream *st, *source_st;
160  int64_t time = 0;
161 
162  while (1) {
163  if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0)
164  break;
165  line++;
166  cursor = buf;
167  keyword = get_keyword(&cursor);
168  if (!*keyword || *keyword == '#')
169  continue;
170 
171  if (!strcmp(keyword, "file")) {
172  char *filename = av_get_token((const char **)&cursor, SPACE_CHARS);
173  if (!filename) {
174  av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line);
176  }
177  if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0)
178  FAIL(ret);
179  } else if (!strcmp(keyword, "duration")) {
180  char *dur_str = get_keyword(&cursor);
181  int64_t dur;
182  if (!file) {
183  av_log(avf, AV_LOG_ERROR, "Line %d: duration without file\n",
184  line);
186  }
187  if ((ret = av_parse_time(&dur, dur_str, 1)) < 0) {
188  av_log(avf, AV_LOG_ERROR, "Line %d: invalid duration '%s'\n",
189  line, dur_str);
190  FAIL(ret);
191  }
192  file->duration = dur;
193  } else if (!strcmp(keyword, "ffconcat")) {
194  char *ver_kw = get_keyword(&cursor);
195  char *ver_val = get_keyword(&cursor);
196  if (strcmp(ver_kw, "version") || strcmp(ver_val, "1.0")) {
197  av_log(avf, AV_LOG_ERROR, "Line %d: invalid version\n", line);
199  }
200  if (cat->safe < 0)
201  cat->safe = 1;
202  } else {
203  av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
204  line, keyword);
206  }
207  }
208  if (ret < 0)
209  FAIL(ret);
210 
211  for (i = 0; i < cat->nb_files; i++) {
212  if (cat->files[i].start_time == AV_NOPTS_VALUE)
213  cat->files[i].start_time = time;
214  else
215  time = cat->files[i].start_time;
216  if (cat->files[i].duration == AV_NOPTS_VALUE)
217  break;
218  time += cat->files[i].duration;
219  }
220  if (i == cat->nb_files)
221  avf->duration = time;
222 
223  if ((ret = open_file(avf, 0)) < 0)
224  FAIL(ret);
225  for (i = 0; i < cat->avf->nb_streams; i++) {
226  if (!(st = avformat_new_stream(avf, NULL)))
227  FAIL(AVERROR(ENOMEM));
228  source_st = cat->avf->streams[i];
229  if ((ret = avcodec_copy_context(st->codec, source_st->codec)) < 0)
230  FAIL(ret);
231  st->r_frame_rate = source_st->r_frame_rate;
232  st->avg_frame_rate = source_st->avg_frame_rate;
233  st->time_base = source_st->time_base;
234  st->sample_aspect_ratio = source_st->sample_aspect_ratio;
235  }
236 
237  return 0;
238 
239 fail:
240  concat_read_close(avf);
241  return ret;
242 }
243 
245 {
246  ConcatContext *cat = avf->priv_data;
247  unsigned fileno = cat->cur_file - cat->files;
248 
249  if (cat->cur_file->duration == AV_NOPTS_VALUE)
250  cat->cur_file->duration = cat->avf->duration;
251 
252  if (++fileno >= cat->nb_files)
253  return AVERROR_EOF;
254  avformat_close_input(&cat->avf);
255  return open_file(avf, fileno);
256 }
257 
259 {
260  ConcatContext *cat = avf->priv_data;
261  int ret;
262  int64_t delta;
263 
264  while (1) {
265  if ((ret = av_read_frame(cat->avf, pkt)) != AVERROR_EOF ||
266  (ret = open_next_file(avf)) < 0)
267  break;
268  }
269  delta = av_rescale_q(cat->cur_file->start_time - cat->avf->start_time,
271  cat->avf->streams[pkt->stream_index]->time_base);
272  if (pkt->pts != AV_NOPTS_VALUE)
273  pkt->pts += delta;
274  if (pkt->dts != AV_NOPTS_VALUE)
275  pkt->dts += delta;
276  return ret;
277 }
278 
279 #define OFFSET(x) offsetof(ConcatContext, x)
280 #define DEC AV_OPT_FLAG_DECODING_PARAM
281 
282 static const AVOption options[] = {
283  { "safe", "enable safe mode",
284  OFFSET(safe), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, DEC },
285  { NULL }
286 };
287 
288 static const AVClass concat_class = {
289  .class_name = "concat demuxer",
290  .item_name = av_default_item_name,
291  .option = options,
292  .version = LIBAVUTIL_VERSION_INT,
293 };
294 
295 
297  .name = "concat",
298  .long_name = NULL_IF_CONFIG_SMALL("Virtual concatenation script"),
299  .priv_data_size = sizeof(ConcatContext),
304  .priv_class = &concat_class,
305 };