FFmpeg  2.8.17
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules 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 file_start_time;
45  int64_t file_inpoint;
46  int64_t duration;
48  int64_t inpoint;
49  int64_t outpoint;
52 } ConcatFile;
53 
54 typedef struct {
55  AVClass *class;
58  unsigned nb_files;
60  int safe;
61  int seekable;
62  int eof;
64  unsigned auto_convert;
66 
68 {
69  return memcmp(probe->buf, "ffconcat version 1.0", 20) ?
71 }
72 
73 static char *get_keyword(uint8_t **cursor)
74 {
75  char *ret = *cursor += strspn(*cursor, SPACE_CHARS);
76  *cursor += strcspn(*cursor, SPACE_CHARS);
77  if (**cursor) {
78  *((*cursor)++) = 0;
79  *cursor += strspn(*cursor, SPACE_CHARS);
80  }
81  return ret;
82 }
83 
84 static int safe_filename(const char *f)
85 {
86  const char *start = f;
87 
88  for (; *f; f++) {
89  /* A-Za-z0-9_- */
90  if (!((unsigned)((*f | 32) - 'a') < 26 ||
91  (unsigned)(*f - '0') < 10 || *f == '_' || *f == '-')) {
92  if (f == start)
93  return 0;
94  else if (*f == '/')
95  start = f + 1;
96  else if (*f != '.')
97  return 0;
98  }
99  }
100  return 1;
101 }
102 
103 #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0)
104 
105 static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile,
106  unsigned *nb_files_alloc)
107 {
108  ConcatContext *cat = avf->priv_data;
109  ConcatFile *file;
110  char *url = NULL;
111  const char *proto;
112  size_t url_len, proto_len;
113  int ret;
114 
115  if (cat->safe > 0 && !safe_filename(filename)) {
116  av_log(avf, AV_LOG_ERROR, "Unsafe file name '%s'\n", filename);
117  FAIL(AVERROR(EPERM));
118  }
119 
120  proto = avio_find_protocol_name(filename);
121  proto_len = proto ? strlen(proto) : 0;
122  if (!memcmp(filename, proto, proto_len) &&
123  (filename[proto_len] == ':' || filename[proto_len] == ',')) {
124  url = filename;
125  filename = NULL;
126  } else {
127  url_len = strlen(avf->filename) + strlen(filename) + 16;
128  if (!(url = av_malloc(url_len)))
129  FAIL(AVERROR(ENOMEM));
130  ff_make_absolute_url(url, url_len, avf->filename, filename);
131  av_freep(&filename);
132  }
133 
134  if (cat->nb_files >= *nb_files_alloc) {
135  size_t n = FFMAX(*nb_files_alloc * 2, 16);
136  ConcatFile *new_files;
137  if (n <= cat->nb_files || n > SIZE_MAX / sizeof(*cat->files) ||
138  !(new_files = av_realloc(cat->files, n * sizeof(*cat->files))))
139  FAIL(AVERROR(ENOMEM));
140  cat->files = new_files;
141  *nb_files_alloc = n;
142  }
143 
144  file = &cat->files[cat->nb_files++];
145  memset(file, 0, sizeof(*file));
146  *rfile = file;
147 
148  file->url = url;
149  file->start_time = AV_NOPTS_VALUE;
150  file->duration = AV_NOPTS_VALUE;
151  file->inpoint = AV_NOPTS_VALUE;
152  file->outpoint = AV_NOPTS_VALUE;
153 
154  return 0;
155 
156 fail:
157  av_free(url);
158  av_free(filename);
159  return ret;
160 }
161 
162 static int copy_stream_props(AVStream *st, AVStream *source_st)
163 {
164  int ret;
165 
166  if (st->codec->codec_id || !source_st->codec->codec_id) {
167  if (st->codec->extradata_size < source_st->codec->extradata_size) {
168  ret = ff_alloc_extradata(st->codec,
169  source_st->codec->extradata_size);
170  if (ret < 0)
171  return ret;
172  }
173  memcpy(st->codec->extradata, source_st->codec->extradata,
174  source_st->codec->extradata_size);
175  return 0;
176  }
177  if ((ret = avcodec_copy_context(st->codec, source_st->codec)) < 0)
178  return ret;
179  st->r_frame_rate = source_st->r_frame_rate;
180  st->avg_frame_rate = source_st->avg_frame_rate;
181  st->time_base = source_st->time_base;
182  st->sample_aspect_ratio = source_st->sample_aspect_ratio;
183 
184  av_dict_copy(&st->metadata, source_st->metadata, 0);
185  return 0;
186 }
187 
188 static int detect_stream_specific(AVFormatContext *avf, int idx)
189 {
190  ConcatContext *cat = avf->priv_data;
191  AVStream *st = cat->avf->streams[idx];
192  ConcatStream *cs = &cat->cur_file->streams[idx];
194 
195  if (cat->auto_convert && st->codec->codec_id == AV_CODEC_ID_H264) {
196  if (!st->codec->extradata_size ||
197  (st->codec->extradata_size >= 3 && AV_RB24(st->codec->extradata) == 1) ||
198  (st->codec->extradata_size >= 4 && AV_RB32(st->codec->extradata) == 1))
199  return 0;
200  av_log(cat->avf, AV_LOG_INFO,
201  "Auto-inserting h264_mp4toannexb bitstream filter\n");
202  if (!(bsf = av_bitstream_filter_init("h264_mp4toannexb"))) {
203  av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb bitstream filter "
204  "required for H.264 streams\n");
205  return AVERROR_BSF_NOT_FOUND;
206  }
207  cs->bsf = bsf;
208  }
209  return 0;
210 }
211 
213 {
214  ConcatContext *cat = avf->priv_data;
215  AVStream *st;
216  int i, ret;
217 
218  for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) {
219  if (i < avf->nb_streams) {
220  st = avf->streams[i];
221  } else {
222  if (!(st = avformat_new_stream(avf, NULL)))
223  return AVERROR(ENOMEM);
224  }
225  if ((ret = copy_stream_props(st, cat->avf->streams[i])) < 0)
226  return ret;
227  cat->cur_file->streams[i].out_stream_index = i;
228  }
229  return 0;
230 }
231 
233 {
234  ConcatContext *cat = avf->priv_data;
235  AVStream *st;
236  int i, j, ret;
237 
238  for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) {
239  st = cat->avf->streams[i];
240  for (j = 0; j < avf->nb_streams; j++) {
241  if (avf->streams[j]->id == st->id) {
242  av_log(avf, AV_LOG_VERBOSE,
243  "Match slave stream #%d with stream #%d id 0x%x\n",
244  i, j, st->id);
245  if ((ret = copy_stream_props(avf->streams[j], st)) < 0)
246  return ret;
247  cat->cur_file->streams[i].out_stream_index = j;
248  }
249  }
250  }
251  return 0;
252 }
253 
255 {
256  ConcatContext *cat = avf->priv_data;
257  ConcatStream *map;
258  int i, ret;
259 
260  if (cat->cur_file->nb_streams >= cat->avf->nb_streams)
261  return 0;
262  map = av_realloc(cat->cur_file->streams,
263  cat->avf->nb_streams * sizeof(*map));
264  if (!map)
265  return AVERROR(ENOMEM);
266  cat->cur_file->streams = map;
267  memset(map + cat->cur_file->nb_streams, 0,
268  (cat->avf->nb_streams - cat->cur_file->nb_streams) * sizeof(*map));
269 
270  for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++)
271  map[i].out_stream_index = -1;
272  switch (cat->stream_match_mode) {
273  case MATCH_ONE_TO_ONE:
274  ret = match_streams_one_to_one(avf);
275  break;
276  case MATCH_EXACT_ID:
277  ret = match_streams_exact_id(avf);
278  break;
279  default:
280  ret = AVERROR_BUG;
281  }
282  if (ret < 0)
283  return ret;
284  for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++)
285  if ((ret = detect_stream_specific(avf, i)) < 0)
286  return ret;
287  cat->cur_file->nb_streams = cat->avf->nb_streams;
288  return 0;
289 }
290 
291 static int open_file(AVFormatContext *avf, unsigned fileno)
292 {
293  ConcatContext *cat = avf->priv_data;
294  ConcatFile *file = &cat->files[fileno];
295  int ret;
296 
297  if (cat->avf)
298  avformat_close_input(&cat->avf);
299 
300  cat->avf = avformat_alloc_context();
301  if (!cat->avf)
302  return AVERROR(ENOMEM);
303 
305 
306  if ((ret = ff_copy_whitelists(cat->avf, avf)) < 0)
307  return ret;
308 
309  if ((ret = avformat_open_input(&cat->avf, file->url, NULL, NULL)) < 0 ||
310  (ret = avformat_find_stream_info(cat->avf, NULL)) < 0) {
311  av_log(avf, AV_LOG_ERROR, "Impossible to open '%s'\n", file->url);
312  avformat_close_input(&cat->avf);
313  return ret;
314  }
315  cat->cur_file = file;
316  if (file->start_time == AV_NOPTS_VALUE)
317  file->start_time = !fileno ? 0 :
318  cat->files[fileno - 1].start_time +
319  cat->files[fileno - 1].duration;
320  file->file_start_time = (cat->avf->start_time == AV_NOPTS_VALUE) ? 0 : cat->avf->start_time;
321  file->file_inpoint = (file->inpoint == AV_NOPTS_VALUE) ? file->file_start_time : file->inpoint;
322  if ((ret = match_streams(avf)) < 0)
323  return ret;
324  if (file->inpoint != AV_NOPTS_VALUE) {
325  if ((ret = avformat_seek_file(cat->avf, -1, INT64_MIN, file->inpoint, file->inpoint, 0)) < 0)
326  return ret;
327  }
328  return 0;
329 }
330 
332 {
333  ConcatContext *cat = avf->priv_data;
334  unsigned i;
335 
336  if (cat->avf)
337  avformat_close_input(&cat->avf);
338  for (i = 0; i < cat->nb_files; i++) {
339  av_freep(&cat->files[i].url);
340  av_freep(&cat->files[i].streams);
341  av_dict_free(&cat->files[i].metadata);
342  }
343  av_freep(&cat->files);
344  return 0;
345 }
346 
348 {
349  ConcatContext *cat = avf->priv_data;
350  uint8_t buf[4096];
351  uint8_t *cursor, *keyword;
352  int ret, line = 0, i;
353  unsigned nb_files_alloc = 0;
354  ConcatFile *file = NULL;
355  int64_t time = 0;
356 
357  while (1) {
358  if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0)
359  break;
360  line++;
361  cursor = buf;
362  keyword = get_keyword(&cursor);
363  if (!*keyword || *keyword == '#')
364  continue;
365 
366  if (!strcmp(keyword, "file")) {
367  char *filename = av_get_token((const char **)&cursor, SPACE_CHARS);
368  if (!filename) {
369  av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line);
371  }
372  if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0)
373  goto fail;
374  } else if (!strcmp(keyword, "duration") || !strcmp(keyword, "inpoint") || !strcmp(keyword, "outpoint")) {
375  char *dur_str = get_keyword(&cursor);
376  int64_t dur;
377  if (!file) {
378  av_log(avf, AV_LOG_ERROR, "Line %d: %s without file\n",
379  line, keyword);
381  }
382  if ((ret = av_parse_time(&dur, dur_str, 1)) < 0) {
383  av_log(avf, AV_LOG_ERROR, "Line %d: invalid %s '%s'\n",
384  line, keyword, dur_str);
385  goto fail;
386  }
387  if (!strcmp(keyword, "duration"))
388  file->duration = dur;
389  else if (!strcmp(keyword, "inpoint"))
390  file->inpoint = dur;
391  else if (!strcmp(keyword, "outpoint"))
392  file->outpoint = dur;
393  } else if (!strcmp(keyword, "file_packet_metadata")) {
394  char *metadata;
395  metadata = av_get_token((const char **)&cursor, SPACE_CHARS);
396  if (!metadata) {
397  av_log(avf, AV_LOG_ERROR, "Line %d: packet metadata required\n", line);
399  }
400  if (!file) {
401  av_log(avf, AV_LOG_ERROR, "Line %d: %s without file\n",
402  line, keyword);
404  }
405  if ((ret = av_dict_parse_string(&file->metadata, metadata, "=", "", 0)) < 0) {
406  av_log(avf, AV_LOG_ERROR, "Line %d: failed to parse metadata string\n", line);
407  av_freep(&metadata);
409  }
410  av_freep(&metadata);
411  } else if (!strcmp(keyword, "stream")) {
412  if (!avformat_new_stream(avf, NULL))
413  FAIL(AVERROR(ENOMEM));
414  } else if (!strcmp(keyword, "exact_stream_id")) {
415  if (!avf->nb_streams) {
416  av_log(avf, AV_LOG_ERROR, "Line %d: exact_stream_id without stream\n",
417  line);
419  }
420  avf->streams[avf->nb_streams - 1]->id =
421  strtol(get_keyword(&cursor), NULL, 0);
422  } else if (!strcmp(keyword, "ffconcat")) {
423  char *ver_kw = get_keyword(&cursor);
424  char *ver_val = get_keyword(&cursor);
425  if (strcmp(ver_kw, "version") || strcmp(ver_val, "1.0")) {
426  av_log(avf, AV_LOG_ERROR, "Line %d: invalid version\n", line);
428  }
429  if (cat->safe < 0)
430  cat->safe = 1;
431  } else {
432  av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
433  line, keyword);
435  }
436  }
437  if (ret < 0)
438  goto fail;
439  if (!cat->nb_files)
441 
442  for (i = 0; i < cat->nb_files; i++) {
443  if (cat->files[i].start_time == AV_NOPTS_VALUE)
444  cat->files[i].start_time = time;
445  else
446  time = cat->files[i].start_time;
447  if (cat->files[i].duration == AV_NOPTS_VALUE) {
448  if (cat->files[i].inpoint == AV_NOPTS_VALUE || cat->files[i].outpoint == AV_NOPTS_VALUE)
449  break;
450  cat->files[i].duration = cat->files[i].outpoint - cat->files[i].inpoint;
451  }
452  time += cat->files[i].duration;
453  }
454  if (i == cat->nb_files) {
455  avf->duration = time;
456  cat->seekable = 1;
457  }
458 
461  if ((ret = open_file(avf, 0)) < 0)
462  goto fail;
463  return 0;
464 
465 fail:
466  concat_read_close(avf);
467  return ret;
468 }
469 
471 {
472  ConcatContext *cat = avf->priv_data;
473  unsigned fileno = cat->cur_file - cat->files;
474 
475  if (cat->cur_file->duration == AV_NOPTS_VALUE) {
476  cat->cur_file->duration = cat->avf->duration;
477  if (cat->cur_file->inpoint != AV_NOPTS_VALUE)
478  cat->cur_file->duration -= (cat->cur_file->inpoint - cat->cur_file->file_start_time);
479  if (cat->cur_file->outpoint != AV_NOPTS_VALUE)
480  cat->cur_file->duration -= cat->avf->duration - (cat->cur_file->outpoint - cat->cur_file->file_start_time);
481  }
482 
483  if (++fileno >= cat->nb_files) {
484  cat->eof = 1;
485  return AVERROR_EOF;
486  }
487  return open_file(avf, fileno);
488 }
489 
491 {
492  AVStream *st = avf->streams[cs->out_stream_index];
494  AVPacket pkt2;
495  int ret;
496 
497  av_assert0(cs->out_stream_index >= 0);
498  for (bsf = cs->bsf; bsf; bsf = bsf->next) {
499  pkt2 = *pkt;
500  ret = av_bitstream_filter_filter(bsf, st->codec, NULL,
501  &pkt2.data, &pkt2.size,
502  pkt->data, pkt->size,
503  !!(pkt->flags & AV_PKT_FLAG_KEY));
504  if (ret < 0) {
505  av_packet_unref(pkt);
506  return ret;
507  }
508  av_assert0(pkt2.buf);
509  if (ret == 0 && pkt2.data != pkt->data) {
510  if ((ret = av_copy_packet(&pkt2, pkt)) < 0) {
511  av_free(pkt2.data);
512  return ret;
513  }
514  ret = 1;
515  }
516  if (ret > 0) {
517  av_free_packet(pkt);
518  pkt2.buf = av_buffer_create(pkt2.data, pkt2.size,
520  if (!pkt2.buf) {
521  av_free(pkt2.data);
522  return AVERROR(ENOMEM);
523  }
524  }
525  *pkt = pkt2;
526  }
527  return 0;
528 }
529 
530 /* Returns true if the packet dts is greater or equal to the specified outpoint. */
532 {
533  if (cat->cur_file->outpoint != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE) {
534  return av_compare_ts(pkt->dts, cat->avf->streams[pkt->stream_index]->time_base,
535  cat->cur_file->outpoint, AV_TIME_BASE_Q) >= 0;
536  }
537  return 0;
538 }
539 
541 {
542  ConcatContext *cat = avf->priv_data;
543  int ret;
544  int64_t delta;
545  ConcatStream *cs;
546  AVStream *st;
547 
548  if (cat->eof)
549  return AVERROR_EOF;
550 
551  if (!cat->avf)
552  return AVERROR(EIO);
553 
554  while (1) {
555  ret = av_read_frame(cat->avf, pkt);
556  if (ret == AVERROR_EOF || packet_after_outpoint(cat, pkt)) {
557  if (ret == 0)
558  av_packet_unref(pkt);
559  if ((ret = open_next_file(avf)) < 0)
560  return ret;
561  continue;
562  }
563  if (ret < 0)
564  return ret;
565  if ((ret = match_streams(avf)) < 0) {
566  av_packet_unref(pkt);
567  return ret;
568  }
569  cs = &cat->cur_file->streams[pkt->stream_index];
570  if (cs->out_stream_index < 0) {
571  av_packet_unref(pkt);
572  continue;
573  }
574  pkt->stream_index = cs->out_stream_index;
575  break;
576  }
577  if ((ret = filter_packet(avf, cs, pkt)))
578  return ret;
579 
580  st = cat->avf->streams[pkt->stream_index];
581  av_log(avf, AV_LOG_DEBUG, "file:%d stream:%d pts:%s pts_time:%s dts:%s dts_time:%s",
582  (unsigned)(cat->cur_file - cat->files), pkt->stream_index,
583  av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
584  av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
585 
586  delta = av_rescale_q(cat->cur_file->start_time - cat->cur_file->file_inpoint,
588  cat->avf->streams[pkt->stream_index]->time_base);
589  if (pkt->pts != AV_NOPTS_VALUE)
590  pkt->pts += delta;
591  if (pkt->dts != AV_NOPTS_VALUE)
592  pkt->dts += delta;
593  av_log(avf, AV_LOG_DEBUG, " -> pts:%s pts_time:%s dts:%s dts_time:%s\n",
594  av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
595  av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
596  if (cat->cur_file->metadata) {
597  uint8_t* metadata;
598  int metadata_len;
599  char* packed_metadata = av_packet_pack_dictionary(cat->cur_file->metadata, &metadata_len);
600  if (!packed_metadata)
601  return AVERROR(ENOMEM);
602  if (!(metadata = av_packet_new_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA, metadata_len))) {
603  av_freep(&packed_metadata);
604  return AVERROR(ENOMEM);
605  }
606  memcpy(metadata, packed_metadata, metadata_len);
607  av_freep(&packed_metadata);
608  }
609  return ret;
610 }
611 
612 static void rescale_interval(AVRational tb_in, AVRational tb_out,
613  int64_t *min_ts, int64_t *ts, int64_t *max_ts)
614 {
615  *ts = av_rescale_q (* ts, tb_in, tb_out);
616  *min_ts = av_rescale_q_rnd(*min_ts, tb_in, tb_out,
618  *max_ts = av_rescale_q_rnd(*max_ts, tb_in, tb_out,
620 }
621 
622 static int try_seek(AVFormatContext *avf, int stream,
623  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
624 {
625  ConcatContext *cat = avf->priv_data;
626  int64_t t0 = cat->cur_file->start_time - cat->cur_file->file_inpoint;
627 
628  ts -= t0;
629  min_ts = min_ts == INT64_MIN ? INT64_MIN : min_ts - t0;
630  max_ts = max_ts == INT64_MAX ? INT64_MAX : max_ts - t0;
631  if (stream >= 0) {
632  if (stream >= cat->avf->nb_streams)
633  return AVERROR(EIO);
635  &min_ts, &ts, &max_ts);
636  }
637  return avformat_seek_file(cat->avf, stream, min_ts, ts, max_ts, flags);
638 }
639 
640 static int real_seek(AVFormatContext *avf, int stream,
641  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
642 {
643  ConcatContext *cat = avf->priv_data;
644  int ret, left, right;
645 
646  if (stream >= 0) {
647  if (stream >= avf->nb_streams)
648  return AVERROR(EINVAL);
650  &min_ts, &ts, &max_ts);
651  }
652 
653  left = 0;
654  right = cat->nb_files;
655  while (right - left > 1) {
656  int mid = (left + right) / 2;
657  if (ts < cat->files[mid].start_time)
658  right = mid;
659  else
660  left = mid;
661  }
662 
663  if ((ret = open_file(avf, left)) < 0)
664  return ret;
665 
666  ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
667  if (ret < 0 &&
668  left < cat->nb_files - 1 &&
669  cat->files[left + 1].start_time < max_ts) {
670  if ((ret = open_file(avf, left + 1)) < 0)
671  return ret;
672  ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
673  }
674  return ret;
675 }
676 
677 static int concat_seek(AVFormatContext *avf, int stream,
678  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
679 {
680  ConcatContext *cat = avf->priv_data;
681  ConcatFile *cur_file_saved = cat->cur_file;
682  AVFormatContext *cur_avf_saved = cat->avf;
683  int ret;
684 
685  if (!cat->seekable)
686  return AVERROR(ESPIPE); /* XXX: can we use it? */
687  if (flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME))
688  return AVERROR(ENOSYS);
689  cat->avf = NULL;
690  if ((ret = real_seek(avf, stream, min_ts, ts, max_ts, flags)) < 0) {
691  if (cat->avf)
692  avformat_close_input(&cat->avf);
693  cat->avf = cur_avf_saved;
694  cat->cur_file = cur_file_saved;
695  } else {
696  avformat_close_input(&cur_avf_saved);
697  cat->eof = 0;
698  }
699  return ret;
700 }
701 
702 #define OFFSET(x) offsetof(ConcatContext, x)
703 #define DEC AV_OPT_FLAG_DECODING_PARAM
704 
705 static const AVOption options[] = {
706  { "safe", "enable safe mode",
707  OFFSET(safe), AV_OPT_TYPE_INT, {.i64 = 1}, -1, 1, DEC },
708  { "auto_convert", "automatically convert bitstream format",
709  OFFSET(auto_convert), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, DEC },
710  { NULL }
711 };
712 
713 static const AVClass concat_class = {
714  .class_name = "concat demuxer",
715  .item_name = av_default_item_name,
716  .option = options,
717  .version = LIBAVUTIL_VERSION_INT,
718 };
719 
720 
722  .name = "concat",
723  .long_name = NULL_IF_CONFIG_SMALL("Virtual concatenation script"),
724  .priv_data_size = sizeof(ConcatContext),
729  .read_seek2 = concat_seek,
730  .priv_class = &concat_class,
731 };
#define NULL
Definition: coverity.c:32
void ff_make_absolute_url(char *buf, int size, const char *base, const char *rel)
Convert a relative url into an absolute url, given a base url.
Definition: url.c:80
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:284
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1535
AVOption.
Definition: opt.h:255
static int concat_read_close(AVFormatContext *avf)
Definition: concatdec.c:331
ConcatMatchMode
Definition: concatdec.c:31
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
#define DEC
Definition: concatdec.c:703
int av_parse_time(int64_t *timeval, const char *timestr, int duration)
Parse timestr and return in *time a corresponding number of microseconds.
Definition: parseutils.c:558
#define FAIL(retcode)
Definition: concatdec.c:103
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:926
static void rescale_interval(AVRational tb_in, AVRational tb_out, int64_t *min_ts, int64_t *ts, int64_t *max_ts)
Definition: concatdec.c:612
int size
Definition: avcodec.h:1434
AVDictionary * metadata
Definition: concatdec.c:50
#define AV_RB24
Definition: intreadwrite.h:64
static AVPacket pkt
static const AVOption options[]
Definition: concatdec.c:705
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
Definition: options.c:199
int64_t outpoint
Definition: concatdec.c:49
Format I/O context.
Definition: avformat.h:1285
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static int concat_seek(AVFormatContext *avf, int stream, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Definition: concatdec.c:677
ConcatStream * streams
Definition: concatdec.c:47
static int64_t start_time
Definition: ffplay.c:325
uint8_t
Round toward +infinity.
Definition: mathematics.h:74
static int nb_streams
Definition: ffprobe.c:226
#define av_malloc(s)
float delta
static int match_streams(AVFormatContext *avf)
Definition: concatdec.c:254
AVOptions.
timestamp utils, mostly useful for debugging/logging purposes
#define AV_RB32
Definition: intreadwrite.h:130
int id
Format-specific stream ID.
Definition: avformat.h:861
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1627
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3761
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1353
#define t0
Definition: regdef.h:28
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:111
AVInputFormat ff_concat_demuxer
Definition: concatdec.c:721
uint8_t * data
Definition: avcodec.h:1433
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
static int probe(AVProbeData *p)
Definition: act.c:36
void av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:213
struct AVBitStreamFilterContext * next
Definition: avcodec.h:5379
#define av_log(a,...)
int64_t inpoint
Definition: concatdec.c:48
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1479
static int concat_read_header(AVFormatContext *avf)
Definition: concatdec.c:347
void av_buffer_default_free(void *opaque, uint8_t *data)
Default free callback, which calls av_free() on the buffer data.
Definition: buffer.c:61
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:147
int out_stream_index
Definition: concatdec.c:38
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
unsigned auto_convert
Definition: concatdec.c:64
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:76
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:178
static int concat_probe(AVProbeData *probe)
Definition: concatdec.c:67
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:199
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1416
Definition: graph2dot.c:48
simple assert() macros that are a bit more flexible than ISO C assert().
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:937
#define FFMAX(a, b)
Definition: common.h:90
#define fail()
Definition: checkasm.h:57
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
Definition: avstring.c:149
int64_t file_start_time
Definition: concatdec.c:44
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1439
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare 2 timestamps each in its own timebases.
Definition: mathematics.c:152
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:873
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:462
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1341
static int match_streams_one_to_one(AVFormatContext *avf)
Definition: concatdec.c:212
int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, enum AVRounding rnd)
Rescale a 64-bit integer by 2 rational numbers with specified rounding.
Definition: mathematics.c:139
static int try_seek(AVFormatContext *avf, int stream, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Definition: concatdec.c:622
char filename[1024]
input or output filename
Definition: avformat.h:1361
static int packet_after_outpoint(ConcatContext *cat, AVPacket *pkt)
Definition: concatdec.c:531
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
AVBitStreamFilterContext * av_bitstream_filter_init(const char *name)
Create and initialize a bitstream filter context given a bitstream filter name.
static int filter_packet(AVFormatContext *avf, ConcatStream *cs, AVPacket *pkt)
Definition: concatdec.c:490
static const AVClass concat_class
Definition: concatdec.c:713
static int open_file(AVFormatContext *avf, unsigned fileno)
Definition: concatdec.c:291
#define AVERROR_BSF_NOT_FOUND
Bitstream filter not found.
Definition: error.h:49
int n
Definition: avisynth_c.h:547
AVDictionary * metadata
Definition: avformat.h:928
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
Read a whole line of text from AVIOContext.
Definition: aviobuf.c:702
ConcatMatchMode stream_match_mode
Definition: concatdec.c:63
AVBitStreamFilterContext * bsf
Definition: concatdec.c:37
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:634
Stream structure.
Definition: avformat.h:854
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
int av_dict_parse_string(AVDictionary **pm, const char *str, const char *key_val_sep, const char *pairs_sep, int flags)
Parse the key/value pairs list and add the parsed entries to a dictionary.
Definition: dict.c:176
ConcatFile * cur_file
Definition: concatdec.c:57
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int keyframe)
Filter bitstream.
A list of zero terminated key/value strings.
Definition: avcodec.h:1330
enum AVCodecID codec_id
Definition: avcodec.h:1529
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:252
AVIOContext * pb
I/O context.
Definition: avformat.h:1327
ConcatFile * files
Definition: concatdec.c:56
uint8_t * av_packet_pack_dictionary(AVDictionary *dict, int *size)
Pack a dictionary for use in side_data.
Definition: avpacket.c:458
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:562
int av_copy_packet(AVPacket *dst, const AVPacket *src)
Copy packet, including contents.
Definition: avpacket.c:269
int ff_alloc_extradata(AVCodecContext *avctx, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0...
Definition: utils.c:2941
void * buf
Definition: avisynth_c.h:553
int extradata_size
Definition: avcodec.h:1628
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
int nb_streams
Definition: concatdec.c:51
Describe the class of an AVClass context structure.
Definition: log.h:67
#define SPACE_CHARS
Definition: internal.h:225
rational number numerator/denominator
Definition: rational.h:43
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition: avformat.h:2297
static int match_streams_exact_id(AVFormatContext *avf)
Definition: concatdec.c:232
unsigned nb_files
Definition: concatdec.c:58
This structure contains the data a format has to probe a file.
Definition: avformat.h:460
misc parsing utilities
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1481
Round toward -infinity.
Definition: mathematics.h:73
int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Seek to timestamp ts.
Definition: utils.c:2228
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
Definition: avio.c:425
int64_t start_time
Definition: concatdec.c:43
int ff_copy_whitelists(AVFormatContext *dst, AVFormatContext *src)
Copies the whilelists from one context to the other.
Definition: utils.c:140
static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
Definition: concatdec.c:540
static int flags
Definition: cpu.c:47
int64_t start_time
Position of the first frame of the component, in AV_TIME_BASE fractional seconds. ...
Definition: avformat.h:1370
char * url
Definition: concatdec.c:42
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:472
Main libavformat public API header.
static char * get_keyword(uint8_t **cursor)
Definition: concatdec.c:73
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:145
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:3103
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:3733
Flag to pass INT64_MIN/MAX through instead of rescaling, this avoids special cases for AV_NOPTS_VALUE...
Definition: mathematics.h:76
static int open_next_file(AVFormatContext *avf)
Definition: concatdec.c:470
#define av_free(p)
static int copy_stream_props(AVStream *st, AVStream *source_st)
Definition: concatdec.c:162
int64_t duration
Definition: concatdec.c:46
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:54
static int real_seek(AVFormatContext *avf, int stream, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Definition: concatdec.c:640
void * priv_data
Format private data.
Definition: avformat.h:1313
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:405
#define AVSEEK_FLAG_FRAME
seeking based on frame number
Definition: avformat.h:2299
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1432
#define OFFSET(x)
Definition: concatdec.c:702
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1380
static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile, unsigned *nb_files_alloc)
Definition: concatdec.c:105
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:553
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:640
unbuffered private I/O API
static int detect_stream_specific(AVFormatContext *avf, int idx)
Definition: concatdec.c:188
static int safe_filename(const char *f)
Definition: concatdec.c:84
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:303
int stream_index
Definition: avcodec.h:1435
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:896
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:1073
int64_t file_inpoint
Definition: concatdec.c:45
This structure stores compressed data.
Definition: avcodec.h:1410
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1426
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
AVFormatContext * avf
Definition: concatdec.c:59