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/bprint.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/parseutils.h"
27 #include "libavutil/timestamp.h"
28 #include "avformat.h"
29 #include "internal.h"
30 #include "url.h"
31 
32 typedef enum ConcatMatchMode {
36 
37 typedef struct ConcatStream {
40 } ConcatStream;
41 
42 typedef struct {
43  char *url;
44  int64_t start_time;
45  int64_t file_start_time;
46  int64_t file_inpoint;
47  int64_t duration;
48  int64_t next_dts;
50  int64_t inpoint;
51  int64_t outpoint;
54 } ConcatFile;
55 
56 typedef struct {
57  AVClass *class;
60  unsigned nb_files;
62  int safe;
63  int seekable;
64  int eof;
66  unsigned auto_convert;
69 
71 {
72  return memcmp(probe->buf, "ffconcat version 1.0", 20) ?
74 }
75 
76 static char *get_keyword(uint8_t **cursor)
77 {
78  char *ret = *cursor += strspn(*cursor, SPACE_CHARS);
79  *cursor += strcspn(*cursor, SPACE_CHARS);
80  if (**cursor) {
81  *((*cursor)++) = 0;
82  *cursor += strspn(*cursor, SPACE_CHARS);
83  }
84  return ret;
85 }
86 
87 static int safe_filename(const char *f)
88 {
89  const char *start = f;
90 
91  for (; *f; f++) {
92  /* A-Za-z0-9_- */
93  if (!((unsigned)((*f | 32) - 'a') < 26 ||
94  (unsigned)(*f - '0') < 10 || *f == '_' || *f == '-')) {
95  if (f == start)
96  return 0;
97  else if (*f == '/')
98  start = f + 1;
99  else if (*f != '.')
100  return 0;
101  }
102  }
103  return 1;
104 }
105 
106 #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0)
107 
108 static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile,
109  unsigned *nb_files_alloc)
110 {
111  ConcatContext *cat = avf->priv_data;
112  ConcatFile *file;
113  char *url = NULL;
114  const char *proto;
115  size_t url_len, proto_len;
116  int ret;
117 
118  if (cat->safe > 0 && !safe_filename(filename)) {
119  av_log(avf, AV_LOG_ERROR, "Unsafe file name '%s'\n", filename);
120  FAIL(AVERROR(EPERM));
121  }
122 
123  proto = avio_find_protocol_name(filename);
124  proto_len = proto ? strlen(proto) : 0;
125  if (proto && !memcmp(filename, proto, proto_len) &&
126  (filename[proto_len] == ':' || filename[proto_len] == ',')) {
127  url = filename;
128  filename = NULL;
129  } else {
130  url_len = strlen(avf->url) + strlen(filename) + 16;
131  if (!(url = av_malloc(url_len)))
132  FAIL(AVERROR(ENOMEM));
133  ff_make_absolute_url(url, url_len, avf->url, filename);
134  av_freep(&filename);
135  }
136 
137  if (cat->nb_files >= *nb_files_alloc) {
138  size_t n = FFMAX(*nb_files_alloc * 2, 16);
139  ConcatFile *new_files;
140  if (n <= cat->nb_files || n > SIZE_MAX / sizeof(*cat->files) ||
141  !(new_files = av_realloc(cat->files, n * sizeof(*cat->files))))
142  FAIL(AVERROR(ENOMEM));
143  cat->files = new_files;
144  *nb_files_alloc = n;
145  }
146 
147  file = &cat->files[cat->nb_files++];
148  memset(file, 0, sizeof(*file));
149  *rfile = file;
150 
151  file->url = url;
152  file->start_time = AV_NOPTS_VALUE;
153  file->duration = AV_NOPTS_VALUE;
154  file->next_dts = AV_NOPTS_VALUE;
155  file->inpoint = AV_NOPTS_VALUE;
156  file->outpoint = AV_NOPTS_VALUE;
157 
158  return 0;
159 
160 fail:
161  av_free(url);
162  av_free(filename);
163  return ret;
164 }
165 
166 static int copy_stream_props(AVStream *st, AVStream *source_st)
167 {
168  int ret;
169 
170  if (st->codecpar->codec_id || !source_st->codecpar->codec_id) {
171  if (st->codecpar->extradata_size < source_st->codecpar->extradata_size) {
172  if (st->codecpar->extradata) {
173  av_freep(&st->codecpar->extradata);
174  st->codecpar->extradata_size = 0;
175  }
176  ret = ff_alloc_extradata(st->codecpar,
177  source_st->codecpar->extradata_size);
178  if (ret < 0)
179  return ret;
180  }
181  memcpy(st->codecpar->extradata, source_st->codecpar->extradata,
182  source_st->codecpar->extradata_size);
183  return 0;
184  }
185  if ((ret = avcodec_parameters_copy(st->codecpar, source_st->codecpar)) < 0)
186  return ret;
187  st->r_frame_rate = source_st->r_frame_rate;
188  st->avg_frame_rate = source_st->avg_frame_rate;
189  st->sample_aspect_ratio = source_st->sample_aspect_ratio;
190  avpriv_set_pts_info(st, 64, source_st->time_base.num, source_st->time_base.den);
191 
192  av_dict_copy(&st->metadata, source_st->metadata, 0);
193  return 0;
194 }
195 
196 static int detect_stream_specific(AVFormatContext *avf, int idx)
197 {
198  ConcatContext *cat = avf->priv_data;
199  AVStream *st = cat->avf->streams[idx];
200  ConcatStream *cs = &cat->cur_file->streams[idx];
201  const AVBitStreamFilter *filter;
202  AVBSFContext *bsf;
203  int ret;
204 
205  if (cat->auto_convert && st->codecpar->codec_id == AV_CODEC_ID_H264) {
206  if (!st->codecpar->extradata_size ||
207  (st->codecpar->extradata_size >= 3 && AV_RB24(st->codecpar->extradata) == 1) ||
208  (st->codecpar->extradata_size >= 4 && AV_RB32(st->codecpar->extradata) == 1))
209  return 0;
210  av_log(cat->avf, AV_LOG_INFO,
211  "Auto-inserting h264_mp4toannexb bitstream filter\n");
212  filter = av_bsf_get_by_name("h264_mp4toannexb");
213  if (!filter) {
214  av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb bitstream filter "
215  "required for H.264 streams\n");
216  return AVERROR_BSF_NOT_FOUND;
217  }
218  ret = av_bsf_alloc(filter, &bsf);
219  if (ret < 0)
220  return ret;
221  cs->bsf = bsf;
222 
223  ret = avcodec_parameters_copy(bsf->par_in, st->codecpar);
224  if (ret < 0)
225  return ret;
226 
227  ret = av_bsf_init(bsf);
228  if (ret < 0)
229  return ret;
230 
231  ret = avcodec_parameters_copy(st->codecpar, bsf->par_out);
232  if (ret < 0)
233  return ret;
234  }
235  return 0;
236 }
237 
239 {
240  ConcatContext *cat = avf->priv_data;
241  AVStream *st;
242  int i, ret;
243 
244  for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) {
245  if (i < avf->nb_streams) {
246  st = avf->streams[i];
247  } else {
248  if (!(st = avformat_new_stream(avf, NULL)))
249  return AVERROR(ENOMEM);
250  }
251  if ((ret = copy_stream_props(st, cat->avf->streams[i])) < 0)
252  return ret;
253  cat->cur_file->streams[i].out_stream_index = i;
254  }
255  return 0;
256 }
257 
259 {
260  ConcatContext *cat = avf->priv_data;
261  AVStream *st;
262  int i, j, ret;
263 
264  for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) {
265  st = cat->avf->streams[i];
266  for (j = 0; j < avf->nb_streams; j++) {
267  if (avf->streams[j]->id == st->id) {
268  av_log(avf, AV_LOG_VERBOSE,
269  "Match slave stream #%d with stream #%d id 0x%x\n",
270  i, j, st->id);
271  if ((ret = copy_stream_props(avf->streams[j], st)) < 0)
272  return ret;
273  cat->cur_file->streams[i].out_stream_index = j;
274  }
275  }
276  }
277  return 0;
278 }
279 
281 {
282  ConcatContext *cat = avf->priv_data;
283  ConcatStream *map;
284  int i, ret;
285 
286  if (cat->cur_file->nb_streams >= cat->avf->nb_streams)
287  return 0;
288  map = av_realloc(cat->cur_file->streams,
289  cat->avf->nb_streams * sizeof(*map));
290  if (!map)
291  return AVERROR(ENOMEM);
292  cat->cur_file->streams = map;
293  memset(map + cat->cur_file->nb_streams, 0,
294  (cat->avf->nb_streams - cat->cur_file->nb_streams) * sizeof(*map));
295 
296  for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) {
297  map[i].out_stream_index = -1;
298  if ((ret = detect_stream_specific(avf, i)) < 0)
299  return ret;
300  }
301  switch (cat->stream_match_mode) {
302  case MATCH_ONE_TO_ONE:
303  ret = match_streams_one_to_one(avf);
304  break;
305  case MATCH_EXACT_ID:
306  ret = match_streams_exact_id(avf);
307  break;
308  default:
309  ret = AVERROR_BUG;
310  }
311  if (ret < 0)
312  return ret;
313  cat->cur_file->nb_streams = cat->avf->nb_streams;
314  return 0;
315 }
316 
317 static int open_file(AVFormatContext *avf, unsigned fileno)
318 {
319  ConcatContext *cat = avf->priv_data;
320  ConcatFile *file = &cat->files[fileno];
321  int ret;
322 
323  if (cat->avf)
324  avformat_close_input(&cat->avf);
325 
326  cat->avf = avformat_alloc_context();
327  if (!cat->avf)
328  return AVERROR(ENOMEM);
329 
330  cat->avf->flags |= avf->flags & ~AVFMT_FLAG_CUSTOM_IO;
332 
333  if ((ret = ff_copy_whiteblacklists(cat->avf, avf)) < 0)
334  return ret;
335 
336  if ((ret = avformat_open_input(&cat->avf, file->url, NULL, NULL)) < 0 ||
337  (ret = avformat_find_stream_info(cat->avf, NULL)) < 0) {
338  av_log(avf, AV_LOG_ERROR, "Impossible to open '%s'\n", file->url);
339  avformat_close_input(&cat->avf);
340  return ret;
341  }
342  cat->cur_file = file;
343  if (file->start_time == AV_NOPTS_VALUE)
344  file->start_time = !fileno ? 0 :
345  cat->files[fileno - 1].start_time +
346  cat->files[fileno - 1].duration;
347  file->file_start_time = (cat->avf->start_time == AV_NOPTS_VALUE) ? 0 : cat->avf->start_time;
348  file->file_inpoint = (file->inpoint == AV_NOPTS_VALUE) ? file->file_start_time : file->inpoint;
349  if (file->duration == AV_NOPTS_VALUE && file->outpoint != AV_NOPTS_VALUE)
350  file->duration = file->outpoint - file->file_inpoint;
351 
352  if (cat->segment_time_metadata) {
353  av_dict_set_int(&file->metadata, "lavf.concatdec.start_time", file->start_time, 0);
354  if (file->duration != AV_NOPTS_VALUE)
355  av_dict_set_int(&file->metadata, "lavf.concatdec.duration", file->duration, 0);
356  }
357 
358  if ((ret = match_streams(avf)) < 0)
359  return ret;
360  if (file->inpoint != AV_NOPTS_VALUE) {
361  if ((ret = avformat_seek_file(cat->avf, -1, INT64_MIN, file->inpoint, file->inpoint, 0)) < 0)
362  return ret;
363  }
364  return 0;
365 }
366 
368 {
369  ConcatContext *cat = avf->priv_data;
370  unsigned i, j;
371 
372  for (i = 0; i < cat->nb_files; i++) {
373  av_freep(&cat->files[i].url);
374  for (j = 0; j < cat->files[i].nb_streams; j++) {
375  if (cat->files[i].streams[j].bsf)
376  av_bsf_free(&cat->files[i].streams[j].bsf);
377  }
378  av_freep(&cat->files[i].streams);
379  av_dict_free(&cat->files[i].metadata);
380  }
381  if (cat->avf)
382  avformat_close_input(&cat->avf);
383  av_freep(&cat->files);
384  return 0;
385 }
386 
388 {
389  ConcatContext *cat = avf->priv_data;
390  AVBPrint bp;
391  uint8_t *cursor, *keyword;
392  int line = 0, i;
393  unsigned nb_files_alloc = 0;
394  ConcatFile *file = NULL;
395  int64_t ret, time = 0;
396 
398 
399  while ((ret = ff_read_line_to_bprint_overwrite(avf->pb, &bp)) >= 0) {
400  line++;
401  cursor = bp.str;
402  keyword = get_keyword(&cursor);
403  if (!*keyword || *keyword == '#')
404  continue;
405 
406  if (!strcmp(keyword, "file")) {
407  char *filename = av_get_token((const char **)&cursor, SPACE_CHARS);
408  if (!filename) {
409  av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line);
411  }
412  if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0)
413  goto fail;
414  } else if (!strcmp(keyword, "duration") || !strcmp(keyword, "inpoint") || !strcmp(keyword, "outpoint")) {
415  char *dur_str = get_keyword(&cursor);
416  int64_t dur;
417  if (!file) {
418  av_log(avf, AV_LOG_ERROR, "Line %d: %s without file\n",
419  line, keyword);
421  }
422  if ((ret = av_parse_time(&dur, dur_str, 1)) < 0) {
423  av_log(avf, AV_LOG_ERROR, "Line %d: invalid %s '%s'\n",
424  line, keyword, dur_str);
425  goto fail;
426  }
427  if (!strcmp(keyword, "duration"))
428  file->duration = dur;
429  else if (!strcmp(keyword, "inpoint"))
430  file->inpoint = dur;
431  else if (!strcmp(keyword, "outpoint"))
432  file->outpoint = dur;
433  } else if (!strcmp(keyword, "file_packet_metadata")) {
434  char *metadata;
435  if (!file) {
436  av_log(avf, AV_LOG_ERROR, "Line %d: %s without file\n",
437  line, keyword);
439  }
440  metadata = av_get_token((const char **)&cursor, SPACE_CHARS);
441  if (!metadata) {
442  av_log(avf, AV_LOG_ERROR, "Line %d: packet metadata required\n", line);
444  }
445  if ((ret = av_dict_parse_string(&file->metadata, metadata, "=", "", 0)) < 0) {
446  av_log(avf, AV_LOG_ERROR, "Line %d: failed to parse metadata string\n", line);
447  av_freep(&metadata);
449  }
450  av_freep(&metadata);
451  } else if (!strcmp(keyword, "stream")) {
452  if (!avformat_new_stream(avf, NULL))
453  FAIL(AVERROR(ENOMEM));
454  } else if (!strcmp(keyword, "exact_stream_id")) {
455  if (!avf->nb_streams) {
456  av_log(avf, AV_LOG_ERROR, "Line %d: exact_stream_id without stream\n",
457  line);
459  }
460  avf->streams[avf->nb_streams - 1]->id =
461  strtol(get_keyword(&cursor), NULL, 0);
462  } else if (!strcmp(keyword, "ffconcat")) {
463  char *ver_kw = get_keyword(&cursor);
464  char *ver_val = get_keyword(&cursor);
465  if (strcmp(ver_kw, "version") || strcmp(ver_val, "1.0")) {
466  av_log(avf, AV_LOG_ERROR, "Line %d: invalid version\n", line);
468  }
469  if (cat->safe < 0)
470  cat->safe = 1;
471  } else {
472  av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
473  line, keyword);
475  }
476  }
477  if (ret != AVERROR_EOF && ret < 0)
478  goto fail;
479  if (!cat->nb_files)
481 
482  for (i = 0; i < cat->nb_files; i++) {
483  if (cat->files[i].start_time == AV_NOPTS_VALUE)
484  cat->files[i].start_time = time;
485  else
486  time = cat->files[i].start_time;
487  if (cat->files[i].duration == AV_NOPTS_VALUE) {
488  if (cat->files[i].inpoint == AV_NOPTS_VALUE || cat->files[i].outpoint == AV_NOPTS_VALUE)
489  break;
490  cat->files[i].duration = cat->files[i].outpoint - cat->files[i].inpoint;
491  }
492  time += cat->files[i].duration;
493  }
494  if (i == cat->nb_files) {
495  avf->duration = time;
496  cat->seekable = 1;
497  }
498 
501  if ((ret = open_file(avf, 0)) < 0)
502  goto fail;
503  av_bprint_finalize(&bp, NULL);
504  return 0;
505 
506 fail:
507  av_bprint_finalize(&bp, NULL);
508  concat_read_close(avf);
509  return ret;
510 }
511 
513 {
514  ConcatContext *cat = avf->priv_data;
515  unsigned fileno = cat->cur_file - cat->files;
516 
517  if (cat->cur_file->duration == AV_NOPTS_VALUE) {
518  if (cat->avf->duration > 0 || cat->cur_file->next_dts == AV_NOPTS_VALUE) {
519  cat->cur_file->duration = cat->avf->duration;
520  } else {
521  cat->cur_file->duration = cat->cur_file->next_dts;
522  }
524  }
525 
526  if (++fileno >= cat->nb_files) {
527  cat->eof = 1;
528  return AVERROR_EOF;
529  }
530  return open_file(avf, fileno);
531 }
532 
534 {
535  int ret;
536 
537  if (cs->bsf) {
538  ret = av_bsf_send_packet(cs->bsf, pkt);
539  if (ret < 0) {
540  av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb filter "
541  "failed to send input packet\n");
542  av_packet_unref(pkt);
543  return ret;
544  }
545 
546  while (!ret)
547  ret = av_bsf_receive_packet(cs->bsf, pkt);
548 
549  if (ret < 0 && (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)) {
550  av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb filter "
551  "failed to receive output packet\n");
552  return ret;
553  }
554  }
555  return 0;
556 }
557 
558 /* Returns true if the packet dts is greater or equal to the specified outpoint. */
560 {
561  if (cat->cur_file->outpoint != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE) {
562  return av_compare_ts(pkt->dts, cat->avf->streams[pkt->stream_index]->time_base,
563  cat->cur_file->outpoint, AV_TIME_BASE_Q) >= 0;
564  }
565  return 0;
566 }
567 
569 {
570  ConcatContext *cat = avf->priv_data;
571  int ret;
572  int64_t delta;
573  ConcatStream *cs;
574  AVStream *st;
575 
576  if (cat->eof)
577  return AVERROR_EOF;
578 
579  if (!cat->avf)
580  return AVERROR(EIO);
581 
582  while (1) {
583  ret = av_read_frame(cat->avf, pkt);
584  if (ret == AVERROR_EOF) {
585  if ((ret = open_next_file(avf)) < 0)
586  return ret;
587  continue;
588  }
589  if (ret < 0)
590  return ret;
591  if ((ret = match_streams(avf)) < 0) {
592  av_packet_unref(pkt);
593  return ret;
594  }
595  if (packet_after_outpoint(cat, pkt)) {
596  av_packet_unref(pkt);
597  if ((ret = open_next_file(avf)) < 0)
598  return ret;
599  continue;
600  }
601  cs = &cat->cur_file->streams[pkt->stream_index];
602  if (cs->out_stream_index < 0) {
603  av_packet_unref(pkt);
604  continue;
605  }
606  break;
607  }
608  if ((ret = filter_packet(avf, cs, pkt)))
609  return ret;
610 
611  st = cat->avf->streams[pkt->stream_index];
612  av_log(avf, AV_LOG_DEBUG, "file:%d stream:%d pts:%s pts_time:%s dts:%s dts_time:%s",
613  (unsigned)(cat->cur_file - cat->files), pkt->stream_index,
614  av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
615  av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
616 
617  delta = av_rescale_q(cat->cur_file->start_time - cat->cur_file->file_inpoint,
619  cat->avf->streams[pkt->stream_index]->time_base);
620  if (pkt->pts != AV_NOPTS_VALUE)
621  pkt->pts += delta;
622  if (pkt->dts != AV_NOPTS_VALUE)
623  pkt->dts += delta;
624  av_log(avf, AV_LOG_DEBUG, " -> pts:%s pts_time:%s dts:%s dts_time:%s\n",
625  av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
626  av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
627  if (cat->cur_file->metadata) {
628  uint8_t* metadata;
629  int metadata_len;
630  char* packed_metadata = av_packet_pack_dictionary(cat->cur_file->metadata, &metadata_len);
631  if (!packed_metadata)
632  return AVERROR(ENOMEM);
633  if (!(metadata = av_packet_new_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA, metadata_len))) {
634  av_freep(&packed_metadata);
635  return AVERROR(ENOMEM);
636  }
637  memcpy(metadata, packed_metadata, metadata_len);
638  av_freep(&packed_metadata);
639  }
640 
641  if (cat->cur_file->duration == AV_NOPTS_VALUE && st->cur_dts != AV_NOPTS_VALUE) {
642  int64_t next_dts = av_rescale_q(st->cur_dts, st->time_base, AV_TIME_BASE_Q);
643  if (cat->cur_file->next_dts == AV_NOPTS_VALUE || next_dts > cat->cur_file->next_dts) {
644  cat->cur_file->next_dts = next_dts;
645  }
646  }
647 
648  pkt->stream_index = cs->out_stream_index;
649  return ret;
650 }
651 
652 static void rescale_interval(AVRational tb_in, AVRational tb_out,
653  int64_t *min_ts, int64_t *ts, int64_t *max_ts)
654 {
655  *ts = av_rescale_q (* ts, tb_in, tb_out);
656  *min_ts = av_rescale_q_rnd(*min_ts, tb_in, tb_out,
658  *max_ts = av_rescale_q_rnd(*max_ts, tb_in, tb_out,
660 }
661 
662 static int try_seek(AVFormatContext *avf, int stream,
663  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
664 {
665  ConcatContext *cat = avf->priv_data;
666  int64_t t0 = cat->cur_file->start_time - cat->cur_file->file_inpoint;
667 
668  ts -= t0;
669  min_ts = min_ts == INT64_MIN ? INT64_MIN : min_ts - t0;
670  max_ts = max_ts == INT64_MAX ? INT64_MAX : max_ts - t0;
671  if (stream >= 0) {
672  if (stream >= cat->avf->nb_streams)
673  return AVERROR(EIO);
675  &min_ts, &ts, &max_ts);
676  }
677  return avformat_seek_file(cat->avf, stream, min_ts, ts, max_ts, flags);
678 }
679 
680 static int real_seek(AVFormatContext *avf, int stream,
681  int64_t min_ts, int64_t ts, int64_t max_ts, int flags, AVFormatContext *cur_avf)
682 {
683  ConcatContext *cat = avf->priv_data;
684  int ret, left, right;
685 
686  if (stream >= 0) {
687  if (stream >= avf->nb_streams)
688  return AVERROR(EINVAL);
690  &min_ts, &ts, &max_ts);
691  }
692 
693  left = 0;
694  right = cat->nb_files;
695  while (right - left > 1) {
696  int mid = (left + right) / 2;
697  if (ts < cat->files[mid].start_time)
698  right = mid;
699  else
700  left = mid;
701  }
702 
703  if (cat->cur_file != &cat->files[left]) {
704  if ((ret = open_file(avf, left)) < 0)
705  return ret;
706  } else {
707  cat->avf = cur_avf;
708  }
709 
710  ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
711  if (ret < 0 &&
712  left < cat->nb_files - 1 &&
713  cat->files[left + 1].start_time < max_ts) {
714  if (cat->cur_file == &cat->files[left])
715  cat->avf = NULL;
716  if ((ret = open_file(avf, left + 1)) < 0)
717  return ret;
718  ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
719  }
720  return ret;
721 }
722 
723 static int concat_seek(AVFormatContext *avf, int stream,
724  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
725 {
726  ConcatContext *cat = avf->priv_data;
727  ConcatFile *cur_file_saved = cat->cur_file;
728  AVFormatContext *cur_avf_saved = cat->avf;
729  int ret;
730 
731  if (!cat->seekable)
732  return AVERROR(ESPIPE); /* XXX: can we use it? */
733  if (flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME))
734  return AVERROR(ENOSYS);
735  cat->avf = NULL;
736  if ((ret = real_seek(avf, stream, min_ts, ts, max_ts, flags, cur_avf_saved)) < 0) {
737  if (cat->cur_file != cur_file_saved) {
738  if (cat->avf)
739  avformat_close_input(&cat->avf);
740  }
741  cat->avf = cur_avf_saved;
742  cat->cur_file = cur_file_saved;
743  } else {
744  if (cat->cur_file != cur_file_saved) {
745  avformat_close_input(&cur_avf_saved);
746  }
747  cat->eof = 0;
748  }
749  return ret;
750 }
751 
752 #define OFFSET(x) offsetof(ConcatContext, x)
753 #define DEC AV_OPT_FLAG_DECODING_PARAM
754 
755 static const AVOption options[] = {
756  { "safe", "enable safe mode",
757  OFFSET(safe), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, DEC },
758  { "auto_convert", "automatically convert bitstream format",
759  OFFSET(auto_convert), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, DEC },
760  { "segment_time_metadata", "output file segment start time and duration as packet metadata",
761  OFFSET(segment_time_metadata), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DEC },
762  { NULL }
763 };
764 
765 static const AVClass concat_class = {
766  .class_name = "concat demuxer",
767  .item_name = av_default_item_name,
768  .option = options,
769  .version = LIBAVUTIL_VERSION_INT,
770 };
771 
772 
774  .name = "concat",
775  .long_name = NULL_IF_CONFIG_SMALL("Virtual concatenation script"),
776  .priv_data_size = sizeof(ConcatContext),
781  .read_seek2 = concat_seek,
782  .priv_class = &concat_class,
783 };
void av_bsf_free(AVBSFContext **ctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
Definition: bsf.c:35
#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
int64_t next_dts
Definition: concatdec.c:48
AVCodecParameters * par_out
Parameters of the output stream.
Definition: avcodec.h:5737
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1629
AVOption.
Definition: opt.h:246
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:135
static int concat_read_close(AVFormatContext *avf)
Definition: concatdec.c:367
ConcatMatchMode
Definition: concatdec.c:32
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
#define DEC
Definition: concatdec.c:753
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4882
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:587
int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
Copies the whilelists from one context to the other.
Definition: utils.c:164
#define FAIL(retcode)
Definition: concatdec.c:106
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3900
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:936
static void rescale_interval(AVRational tb_in, AVRational tb_out, int64_t *min_ts, int64_t *ts, int64_t *max_ts)
Definition: concatdec.c:652
int num
Numerator.
Definition: rational.h:59
The bitstream filter state.
Definition: avcodec.h:5703
AVDictionary * metadata
Definition: concatdec.c:52
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
AVBSFContext * bsf
Definition: concatdec.c:38
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:217
static AVPacket pkt
static const AVOption options[]
Definition: concatdec.c:755
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition: bsf.c:134
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
int64_t outpoint
Definition: concatdec.c:51
Format I/O context.
Definition: avformat.h:1351
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **ctx)
Allocate a context for a given bitstream filter.
Definition: bsf.c:81
int64_t cur_dts
Definition: avformat.h:1077
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
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition: bsf.c:211
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, int clip)
Definition: cfhd.c:153
static int concat_seek(AVFormatContext *avf, int stream, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Definition: concatdec.c:723
ConcatStream * streams
Definition: concatdec.c:49
static int64_t start_time
Definition: ffplay.c:330
uint8_t
Round toward +infinity.
Definition: mathematics.h:83
static int nb_streams
Definition: ffprobe.c:276
#define av_malloc(s)
float delta
static int match_streams(AVFormatContext *avf)
Definition: concatdec.c:280
AVOptions.
timestamp utils, mostly useful for debugging/logging purposes
#define f(width, name)
Definition: cbs_vp9.c:255
int id
Format-specific stream ID.
Definition: avformat.h:881
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4455
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1419
#define t0
Definition: regdef.h:28
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:144
AVInputFormat ff_concat_demuxer
Definition: concatdec.c:773
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1482
#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
#define av_log(a,...)
int64_t inpoint
Definition: concatdec.c:50
static int concat_read_header(AVFormatContext *avf)
Definition: concatdec.c:387
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
int out_stream_index
Definition: concatdec.c:39
int segment_time_metadata
Definition: concatdec.c:67
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2013
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AV_BPRINT_SIZE_UNLIMITED
unsigned auto_convert
Definition: concatdec.c:66
#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
int64_t ff_read_line_to_bprint_overwrite(AVIOContext *s, AVBPrint *bp)
Read a whole line of text from AVIOContext to an AVBPrint buffer overwriting its contents.
Definition: aviobuf.c:865
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
char * url
input or output URL.
Definition: avformat.h:1447
static int concat_probe(AVProbeData *probe)
Definition: concatdec.c:70
#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:203
Definition: graph2dot.c:48
simple assert() macros that are a bit more flexible than ISO C assert().
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:947
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:117
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:45
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare two timestamps each in its own time base.
Definition: mathematics.c:147
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3918
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1407
static int match_streams_one_to_one(AVFormatContext *avf)
Definition: concatdec.c:238
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0...
Definition: utils.c:3287
static int try_seek(AVFormatContext *avf, int stream, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Definition: concatdec.c:662
static int packet_after_outpoint(ConcatContext *cat, AVPacket *pkt)
Definition: concatdec.c:559
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
static int filter_packet(AVFormatContext *avf, ConcatStream *cs, AVPacket *pkt)
Definition: concatdec.c:533
static const AVClass concat_class
Definition: concatdec.c:765
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition: bsf.c:185
static int open_file(AVFormatContext *avf, unsigned fileno)
Definition: concatdec.c:317
#define AVERROR_BSF_NOT_FOUND
Bitstream filter not found.
Definition: error.h:49
int n
Definition: avisynth_c.h:684
AVDictionary * metadata
Definition: avformat.h:938
#define AVFMT_FLAG_CUSTOM_IO
The caller has supplied a custom AVIOContext, don't avio_close() it.
Definition: avformat.h:1490
ConcatMatchMode stream_match_mode
Definition: concatdec.c:65
#define cat(a, bpp, b)
Definition: vp9dsp_init.h:29
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
Stream structure.
Definition: avformat.h:874
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:180
ConcatFile * cur_file
Definition: concatdec.c:59
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
A list of zero terminated key/value strings.
Definition: avcodec.h:1284
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:87
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
ConcatFile * files
Definition: concatdec.c:58
uint8_t * av_packet_pack_dictionary(AVDictionary *dict, int *size)
Pack a dictionary for use in side_data.
Definition: avpacket.c:488
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:598
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
int nb_streams
Definition: concatdec.c:53
Describe the class of an AVClass context structure.
Definition: log.h:67
#define SPACE_CHARS
Definition: internal.h:354
Rational number (pair of numerator and denominator).
Definition: rational.h:58
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition: avformat.h:2505
const VDPAUPixFmtMap * map
static int match_streams_exact_id(AVFormatContext *avf)
Definition: concatdec.c:258
unsigned nb_files
Definition: concatdec.c:60
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
misc parsing utilities
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1768
Round toward -infinity.
Definition: mathematics.h:82
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:2529
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
Definition: avio.c:473
int64_t start_time
Definition: concatdec.c:44
#define flags(name, subs,...)
Definition: cbs_av1.c:596
static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
Definition: concatdec.c:568
int64_t start_time
Position of the first frame of the component, in AV_TIME_BASE fractional seconds. ...
Definition: avformat.h:1456
char * url
Definition: concatdec.c:43
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
Main libavformat public API header.
static char * get_keyword(uint8_t **cursor)
Definition: concatdec.c:76
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:3564
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set that converts the value to a string and stores it...
Definition: dict.c:147
int den
Denominator.
Definition: rational.h:60
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:4427
Flag telling rescaling functions to pass INT64_MIN/MAX through unchanged, avoiding special cases for ...
Definition: mathematics.h:108
static int open_next_file(AVFormatContext *avf)
Definition: concatdec.c:512
#define av_free(p)
static int copy_stream_props(AVStream *st, AVStream *source_st)
Definition: concatdec.c:166
int64_t duration
Definition: concatdec.c:47
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:54
void * priv_data
Format private data.
Definition: avformat.h:1379
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:537
#define AVSEEK_FLAG_FRAME
seeking based on frame number
Definition: avformat.h:2507
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3914
static int real_seek(AVFormatContext *avf, int stream, int64_t min_ts, int64_t ts, int64_t max_ts, int flags, AVFormatContext *cur_avf)
Definition: concatdec.c:680
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1444
#define OFFSET(x)
Definition: concatdec.c:752
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1466
static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile, unsigned *nb_files_alloc)
Definition: concatdec.c:108
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:647
unbuffered private I/O API
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1021
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:134
static int detect_stream_specific(AVFormatContext *avf, int idx)
Definition: concatdec.c:196
static int safe_filename(const char *f)
Definition: concatdec.c:87
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:329
int stream_index
Definition: avcodec.h:1447
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:903
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:998
int64_t file_inpoint
Definition: concatdec.c:46
This structure stores compressed data.
Definition: avcodec.h:1422
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5731
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1438
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AVFormatContext * avf
Definition: concatdec.c:61