FFmpeg
 All Data Structures Namespaces 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/avassert.h"
22 #include "libavutil/avstring.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/parseutils.h"
26 #include "libavutil/timestamp.h"
27 #include "avformat.h"
28 #include "internal.h"
29 #include "url.h"
30 
31 typedef enum ConcatMatchMode {
35 
36 typedef struct ConcatStream {
39 } ConcatStream;
40 
41 typedef struct {
42  char *url;
43  int64_t start_time;
44  int64_t duration;
47 } ConcatFile;
48 
49 typedef struct {
50  AVClass *class;
53  unsigned nb_files;
55  int safe;
56  int seekable;
58  unsigned auto_convert;
60 
62 {
63  return memcmp(probe->buf, "ffconcat version 1.0", 20) ?
65 }
66 
67 static char *get_keyword(uint8_t **cursor)
68 {
69  char *ret = *cursor += strspn(*cursor, SPACE_CHARS);
70  *cursor += strcspn(*cursor, SPACE_CHARS);
71  if (**cursor) {
72  *((*cursor)++) = 0;
73  *cursor += strspn(*cursor, SPACE_CHARS);
74  }
75  return ret;
76 }
77 
78 static int safe_filename(const char *f)
79 {
80  const char *start = f;
81 
82  for (; *f; f++) {
83  /* A-Za-z0-9_- */
84  if (!((unsigned)((*f | 32) - 'a') < 26 ||
85  (unsigned)(*f - '0') < 10 || *f == '_' || *f == '-')) {
86  if (f == start)
87  return 0;
88  else if (*f == '/')
89  start = f + 1;
90  else if (*f != '.')
91  return 0;
92  }
93  }
94  return 1;
95 }
96 
97 #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0)
98 
99 static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile,
100  unsigned *nb_files_alloc)
101 {
102  ConcatContext *cat = avf->priv_data;
103  ConcatFile *file;
104  char *url = NULL;
105  const char *proto;
106  size_t url_len, proto_len;
107  int ret;
108 
109  if (cat->safe > 0 && !safe_filename(filename)) {
110  av_log(avf, AV_LOG_ERROR, "Unsafe file name '%s'\n", filename);
111  FAIL(AVERROR(EPERM));
112  }
113 
114  proto = avio_find_protocol_name(filename);
115  proto_len = proto ? strlen(proto) : 0;
116  if (!memcmp(filename, proto, proto_len) &&
117  (filename[proto_len] == ':' || filename[proto_len] == ',')) {
118  url = filename;
119  filename = NULL;
120  } else {
121  url_len = strlen(avf->filename) + strlen(filename) + 16;
122  if (!(url = av_malloc(url_len)))
123  FAIL(AVERROR(ENOMEM));
124  ff_make_absolute_url(url, url_len, avf->filename, filename);
125  av_freep(&filename);
126  }
127 
128  if (cat->nb_files >= *nb_files_alloc) {
129  size_t n = FFMAX(*nb_files_alloc * 2, 16);
130  ConcatFile *new_files;
131  if (n <= cat->nb_files || n > SIZE_MAX / sizeof(*cat->files) ||
132  !(new_files = av_realloc(cat->files, n * sizeof(*cat->files))))
133  FAIL(AVERROR(ENOMEM));
134  cat->files = new_files;
135  *nb_files_alloc = n;
136  }
137 
138  file = &cat->files[cat->nb_files++];
139  memset(file, 0, sizeof(*file));
140  *rfile = file;
141 
142  file->url = url;
143  file->start_time = AV_NOPTS_VALUE;
144  file->duration = AV_NOPTS_VALUE;
145 
146  return 0;
147 
148 fail:
149  av_free(url);
150  av_free(filename);
151  return ret;
152 }
153 
154 static int copy_stream_props(AVStream *st, AVStream *source_st)
155 {
156  int ret;
157 
158  if (st->codec->codec_id || !source_st->codec->codec_id) {
159  if (st->codec->extradata_size < source_st->codec->extradata_size) {
160  ret = ff_alloc_extradata(st->codec,
161  source_st->codec->extradata_size);
162  if (ret < 0)
163  return ret;
164  }
165  memcpy(st->codec->extradata, source_st->codec->extradata,
166  source_st->codec->extradata_size);
167  return 0;
168  }
169  if ((ret = avcodec_copy_context(st->codec, source_st->codec)) < 0)
170  return ret;
171  st->r_frame_rate = source_st->r_frame_rate;
172  st->avg_frame_rate = source_st->avg_frame_rate;
173  st->time_base = source_st->time_base;
174  st->sample_aspect_ratio = source_st->sample_aspect_ratio;
175  return 0;
176 }
177 
178 static int detect_stream_specific(AVFormatContext *avf, int idx)
179 {
180  ConcatContext *cat = avf->priv_data;
181  AVStream *st = cat->avf->streams[idx];
182  ConcatStream *cs = &cat->cur_file->streams[idx];
184 
185  if (cat->auto_convert && st->codec->codec_id == AV_CODEC_ID_H264 &&
186  (st->codec->extradata_size < 4 || AV_RB32(st->codec->extradata) != 1)) {
187  av_log(cat->avf, AV_LOG_INFO,
188  "Auto-inserting h264_mp4toannexb bitstream filter\n");
189  if (!(bsf = av_bitstream_filter_init("h264_mp4toannexb"))) {
190  av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb bitstream filter "
191  "required for H.264 streams\n");
192  return AVERROR_BSF_NOT_FOUND;
193  }
194  cs->bsf = bsf;
195  }
196  return 0;
197 }
198 
200 {
201  ConcatContext *cat = avf->priv_data;
202  AVStream *st;
203  int i, ret;
204 
205  for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) {
206  if (i < avf->nb_streams) {
207  st = avf->streams[i];
208  } else {
209  if (!(st = avformat_new_stream(avf, NULL)))
210  return AVERROR(ENOMEM);
211  }
212  if ((ret = copy_stream_props(st, cat->avf->streams[i])) < 0)
213  return ret;
214  cat->cur_file->streams[i].out_stream_index = i;
215  }
216  return 0;
217 }
218 
220 {
221  ConcatContext *cat = avf->priv_data;
222  AVStream *st;
223  int i, j, ret;
224 
225  for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) {
226  st = cat->avf->streams[i];
227  for (j = 0; j < avf->nb_streams; j++) {
228  if (avf->streams[j]->id == st->id) {
229  av_log(avf, AV_LOG_VERBOSE,
230  "Match slave stream #%d with stream #%d id 0x%x\n",
231  i, j, st->id);
232  if ((ret = copy_stream_props(avf->streams[j], st)) < 0)
233  return ret;
234  cat->cur_file->streams[i].out_stream_index = j;
235  }
236  }
237  }
238  return 0;
239 }
240 
242 {
243  ConcatContext *cat = avf->priv_data;
244  ConcatStream *map;
245  int i, ret;
246 
247  if (cat->cur_file->nb_streams >= cat->avf->nb_streams)
248  return 0;
249  map = av_realloc(cat->cur_file->streams,
250  cat->avf->nb_streams * sizeof(*map));
251  if (!map)
252  return AVERROR(ENOMEM);
253  cat->cur_file->streams = map;
254  memset(map + cat->cur_file->nb_streams, 0,
255  (cat->avf->nb_streams - cat->cur_file->nb_streams) * sizeof(*map));
256 
257  for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++)
258  map[i].out_stream_index = -1;
259  switch (cat->stream_match_mode) {
260  case MATCH_ONE_TO_ONE:
261  ret = match_streams_one_to_one(avf);
262  break;
263  case MATCH_EXACT_ID:
264  ret = match_streams_exact_id(avf);
265  break;
266  default:
267  ret = AVERROR_BUG;
268  }
269  if (ret < 0)
270  return ret;
271  for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++)
272  if ((ret = detect_stream_specific(avf, i)) < 0)
273  return ret;
274  cat->cur_file->nb_streams = cat->avf->nb_streams;
275  return 0;
276 }
277 
278 static int open_file(AVFormatContext *avf, unsigned fileno)
279 {
280  ConcatContext *cat = avf->priv_data;
281  ConcatFile *file = &cat->files[fileno];
282  int ret;
283 
284  if (cat->avf)
285  avformat_close_input(&cat->avf);
286 
287  cat->avf = avformat_alloc_context();
288  if (!cat->avf)
289  return AVERROR(ENOMEM);
290 
292 
293  if ((ret = ff_copy_whitelists(cat->avf, avf)) < 0)
294  return ret;
295 
296  if ((ret = avformat_open_input(&cat->avf, file->url, NULL, NULL)) < 0 ||
297  (ret = avformat_find_stream_info(cat->avf, NULL)) < 0) {
298  av_log(avf, AV_LOG_ERROR, "Impossible to open '%s'\n", file->url);
299  avformat_close_input(&cat->avf);
300  return ret;
301  }
302  cat->cur_file = file;
303  if (file->start_time == AV_NOPTS_VALUE)
304  file->start_time = !fileno ? 0 :
305  cat->files[fileno - 1].start_time +
306  cat->files[fileno - 1].duration;
307  if ((ret = match_streams(avf)) < 0)
308  return ret;
309  return 0;
310 }
311 
313 {
314  ConcatContext *cat = avf->priv_data;
315  unsigned i;
316 
317  if (cat->avf)
318  avformat_close_input(&cat->avf);
319  for (i = 0; i < cat->nb_files; i++) {
320  av_freep(&cat->files[i].url);
321  av_freep(&cat->files[i].streams);
322  }
323  av_freep(&cat->files);
324  return 0;
325 }
326 
328 {
329  ConcatContext *cat = avf->priv_data;
330  uint8_t buf[4096];
331  uint8_t *cursor, *keyword;
332  int ret, line = 0, i;
333  unsigned nb_files_alloc = 0;
334  ConcatFile *file = NULL;
335  int64_t time = 0;
336 
337  while (1) {
338  if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0)
339  break;
340  line++;
341  cursor = buf;
342  keyword = get_keyword(&cursor);
343  if (!*keyword || *keyword == '#')
344  continue;
345 
346  if (!strcmp(keyword, "file")) {
347  char *filename = av_get_token((const char **)&cursor, SPACE_CHARS);
348  if (!filename) {
349  av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line);
351  }
352  if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0)
353  goto fail;
354  } else if (!strcmp(keyword, "duration")) {
355  char *dur_str = get_keyword(&cursor);
356  int64_t dur;
357  if (!file) {
358  av_log(avf, AV_LOG_ERROR, "Line %d: duration without file\n",
359  line);
361  }
362  if ((ret = av_parse_time(&dur, dur_str, 1)) < 0) {
363  av_log(avf, AV_LOG_ERROR, "Line %d: invalid duration '%s'\n",
364  line, dur_str);
365  goto fail;
366  }
367  file->duration = dur;
368  } else if (!strcmp(keyword, "stream")) {
369  if (!avformat_new_stream(avf, NULL))
370  FAIL(AVERROR(ENOMEM));
371  } else if (!strcmp(keyword, "exact_stream_id")) {
372  if (!avf->nb_streams) {
373  av_log(avf, AV_LOG_ERROR, "Line %d: exact_stream_id without stream\n",
374  line);
376  }
377  avf->streams[avf->nb_streams - 1]->id =
378  strtol(get_keyword(&cursor), NULL, 0);
379  } else if (!strcmp(keyword, "ffconcat")) {
380  char *ver_kw = get_keyword(&cursor);
381  char *ver_val = get_keyword(&cursor);
382  if (strcmp(ver_kw, "version") || strcmp(ver_val, "1.0")) {
383  av_log(avf, AV_LOG_ERROR, "Line %d: invalid version\n", line);
385  }
386  if (cat->safe < 0)
387  cat->safe = 1;
388  } else {
389  av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
390  line, keyword);
392  }
393  }
394  if (ret < 0)
395  goto fail;
396  if (!cat->nb_files)
398 
399  for (i = 0; i < cat->nb_files; i++) {
400  if (cat->files[i].start_time == AV_NOPTS_VALUE)
401  cat->files[i].start_time = time;
402  else
403  time = cat->files[i].start_time;
404  if (cat->files[i].duration == AV_NOPTS_VALUE)
405  break;
406  time += cat->files[i].duration;
407  }
408  if (i == cat->nb_files) {
409  avf->duration = time;
410  cat->seekable = 1;
411  }
412 
415  if ((ret = open_file(avf, 0)) < 0)
416  goto fail;
417  return 0;
418 
419 fail:
420  concat_read_close(avf);
421  return ret;
422 }
423 
425 {
426  ConcatContext *cat = avf->priv_data;
427  unsigned fileno = cat->cur_file - cat->files;
428 
429  if (cat->cur_file->duration == AV_NOPTS_VALUE)
430  cat->cur_file->duration = cat->avf->duration;
431 
432  if (++fileno >= cat->nb_files)
433  return AVERROR_EOF;
434  return open_file(avf, fileno);
435 }
436 
438 {
439  AVStream *st = avf->streams[cs->out_stream_index];
441  AVPacket pkt2;
442  int ret;
443 
444  av_assert0(cs->out_stream_index >= 0);
445  for (bsf = cs->bsf; bsf; bsf = bsf->next) {
446  pkt2 = *pkt;
447  ret = av_bitstream_filter_filter(bsf, st->codec, NULL,
448  &pkt2.data, &pkt2.size,
449  pkt->data, pkt->size,
450  !!(pkt->flags & AV_PKT_FLAG_KEY));
451  if (ret < 0) {
452  av_packet_unref(pkt);
453  return ret;
454  }
455  av_assert0(pkt2.buf);
456  if (ret == 0 && pkt2.data != pkt->data) {
457  if ((ret = av_copy_packet(&pkt2, pkt)) < 0) {
458  av_free(pkt2.data);
459  return ret;
460  }
461  ret = 1;
462  }
463  if (ret > 0) {
464  av_free_packet(pkt);
465  pkt2.buf = av_buffer_create(pkt2.data, pkt2.size,
467  if (!pkt2.buf) {
468  av_free(pkt2.data);
469  return AVERROR(ENOMEM);
470  }
471  }
472  *pkt = pkt2;
473  }
474  return 0;
475 }
476 
478 {
479  ConcatContext *cat = avf->priv_data;
480  int ret;
481  int64_t file_start_time, delta;
482  ConcatStream *cs;
483  AVStream *st;
484 
485  if (!cat->avf)
486  return AVERROR(EIO);
487 
488  while (1) {
489  ret = av_read_frame(cat->avf, pkt);
490  if (ret == AVERROR_EOF) {
491  if ((ret = open_next_file(avf)) < 0)
492  return ret;
493  continue;
494  }
495  if (ret < 0)
496  return ret;
497  if ((ret = match_streams(avf)) < 0) {
498  av_packet_unref(pkt);
499  return ret;
500  }
501  cs = &cat->cur_file->streams[pkt->stream_index];
502  if (cs->out_stream_index < 0) {
503  av_packet_unref(pkt);
504  continue;
505  }
506  pkt->stream_index = cs->out_stream_index;
507  break;
508  }
509  if ((ret = filter_packet(avf, cs, pkt)))
510  return ret;
511 
512  st = cat->avf->streams[pkt->stream_index];
513  av_log(avf, AV_LOG_DEBUG, "file:%d stream:%d pts:%s pts_time:%s dts:%s dts_time:%s",
514  (unsigned)(cat->cur_file - cat->files), pkt->stream_index,
515  av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
516  av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
517 
518  file_start_time = cat->avf->start_time;
519  if (file_start_time == AV_NOPTS_VALUE)
520  file_start_time = 0;
521  delta = av_rescale_q(cat->cur_file->start_time - file_start_time,
523  cat->avf->streams[pkt->stream_index]->time_base);
524  if (pkt->pts != AV_NOPTS_VALUE)
525  pkt->pts += delta;
526  if (pkt->dts != AV_NOPTS_VALUE)
527  pkt->dts += delta;
528  av_log(avf, AV_LOG_DEBUG, " -> pts:%s pts_time:%s dts:%s dts_time:%s\n",
529  av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
530  av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
531  return ret;
532 }
533 
534 static void rescale_interval(AVRational tb_in, AVRational tb_out,
535  int64_t *min_ts, int64_t *ts, int64_t *max_ts)
536 {
537  *ts = av_rescale_q (* ts, tb_in, tb_out);
538  *min_ts = av_rescale_q_rnd(*min_ts, tb_in, tb_out,
540  *max_ts = av_rescale_q_rnd(*max_ts, tb_in, tb_out,
542 }
543 
544 static int try_seek(AVFormatContext *avf, int stream,
545  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
546 {
547  ConcatContext *cat = avf->priv_data;
548  int64_t t0 = cat->cur_file->start_time - cat->avf->start_time;
549 
550  ts -= t0;
551  min_ts = min_ts == INT64_MIN ? INT64_MIN : min_ts - t0;
552  max_ts = max_ts == INT64_MAX ? INT64_MAX : max_ts - t0;
553  if (stream >= 0) {
554  if (stream >= cat->avf->nb_streams)
555  return AVERROR(EIO);
557  &min_ts, &ts, &max_ts);
558  }
559  return avformat_seek_file(cat->avf, stream, min_ts, ts, max_ts, flags);
560 }
561 
562 static int real_seek(AVFormatContext *avf, int stream,
563  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
564 {
565  ConcatContext *cat = avf->priv_data;
566  int ret, left, right;
567 
568  if (stream >= 0) {
569  if (stream >= avf->nb_streams)
570  return AVERROR(EINVAL);
572  &min_ts, &ts, &max_ts);
573  }
574 
575  left = 0;
576  right = cat->nb_files;
577  while (right - left > 1) {
578  int mid = (left + right) / 2;
579  if (ts < cat->files[mid].start_time)
580  right = mid;
581  else
582  left = mid;
583  }
584 
585  if ((ret = open_file(avf, left)) < 0)
586  return ret;
587 
588  ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
589  if (ret < 0 &&
590  left < cat->nb_files - 1 &&
591  cat->files[left + 1].start_time < max_ts) {
592  if ((ret = open_file(avf, left + 1)) < 0)
593  return ret;
594  ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
595  }
596  return ret;
597 }
598 
599 static int concat_seek(AVFormatContext *avf, int stream,
600  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
601 {
602  ConcatContext *cat = avf->priv_data;
603  ConcatFile *cur_file_saved = cat->cur_file;
604  AVFormatContext *cur_avf_saved = cat->avf;
605  int ret;
606 
607  if (!cat->seekable)
608  return AVERROR(ESPIPE); /* XXX: can we use it? */
609  if (flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME))
610  return AVERROR(ENOSYS);
611  cat->avf = NULL;
612  if ((ret = real_seek(avf, stream, min_ts, ts, max_ts, flags)) < 0) {
613  if (cat->avf)
614  avformat_close_input(&cat->avf);
615  cat->avf = cur_avf_saved;
616  cat->cur_file = cur_file_saved;
617  } else {
618  avformat_close_input(&cur_avf_saved);
619  }
620  return ret;
621 }
622 
623 #define OFFSET(x) offsetof(ConcatContext, x)
624 #define DEC AV_OPT_FLAG_DECODING_PARAM
625 
626 static const AVOption options[] = {
627  { "safe", "enable safe mode",
628  OFFSET(safe), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, DEC },
629  { "auto_convert", "automatically convert bitstream format",
630  OFFSET(auto_convert), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC },
631  { NULL }
632 };
633 
634 static const AVClass concat_class = {
635  .class_name = "concat demuxer",
636  .item_name = av_default_item_name,
637  .option = options,
638  .version = LIBAVUTIL_VERSION_INT,
639 };
640 
641 
643  .name = "concat",
644  .long_name = NULL_IF_CONFIG_SMALL("Virtual concatenation script"),
645  .priv_data_size = sizeof(ConcatContext),
650  .read_seek2 = concat_seek,
651  .priv_class = &concat_class,
652 };