2 * Copyright (c) 2012 Nicolas George
4 * This file is part of FFmpeg.
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.
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.
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
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"
32 typedef enum ConcatMatchMode {
37 typedef struct ConcatStream {
45 int64_t file_start_time;
49 ConcatStream *streams;
52 AVDictionary *metadata;
65 ConcatMatchMode stream_match_mode;
66 unsigned auto_convert;
67 int segment_time_metadata;
70 static int concat_probe(AVProbeData *probe)
72 return memcmp(probe->buf, "ffconcat version 1.0", 20) ?
73 0 : AVPROBE_SCORE_MAX;
76 static char *get_keyword(uint8_t **cursor)
78 char *ret = *cursor += strspn(*cursor, SPACE_CHARS);
79 *cursor += strcspn(*cursor, SPACE_CHARS);
82 *cursor += strspn(*cursor, SPACE_CHARS);
87 static int safe_filename(const char *f)
89 const char *start = f;
93 if (!((unsigned)((*f | 32) - 'a') < 26 ||
94 (unsigned)(*f - '0') < 10 || *f == '_' || *f == '-')) {
106 #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0)
108 static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile,
109 unsigned *nb_files_alloc)
111 ConcatContext *cat = avf->priv_data;
115 size_t url_len, proto_len;
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));
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] == ',')) {
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);
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;
147 file = &cat->files[cat->nb_files++];
148 memset(file, 0, sizeof(*file));
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;
166 static int copy_stream_props(AVStream *st, AVStream *source_st)
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;
176 ret = ff_alloc_extradata(st->codecpar,
177 source_st->codecpar->extradata_size);
181 memcpy(st->codecpar->extradata, source_st->codecpar->extradata,
182 source_st->codecpar->extradata_size);
185 if ((ret = avcodec_parameters_copy(st->codecpar, source_st->codecpar)) < 0)
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);
192 av_dict_copy(&st->metadata, source_st->metadata, 0);
196 static int detect_stream_specific(AVFormatContext *avf, int idx)
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;
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))
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");
214 av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb bitstream filter "
215 "required for H.264 streams\n");
216 return AVERROR_BSF_NOT_FOUND;
218 ret = av_bsf_alloc(filter, &bsf);
223 ret = avcodec_parameters_copy(bsf->par_in, st->codecpar);
227 ret = av_bsf_init(bsf);
231 ret = avcodec_parameters_copy(st->codecpar, bsf->par_out);
238 static int match_streams_one_to_one(AVFormatContext *avf)
240 ConcatContext *cat = avf->priv_data;
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];
248 if (!(st = avformat_new_stream(avf, NULL)))
249 return AVERROR(ENOMEM);
251 if ((ret = copy_stream_props(st, cat->avf->streams[i])) < 0)
253 cat->cur_file->streams[i].out_stream_index = i;
258 static int match_streams_exact_id(AVFormatContext *avf)
260 ConcatContext *cat = avf->priv_data;
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",
271 if ((ret = copy_stream_props(avf->streams[j], st)) < 0)
273 cat->cur_file->streams[i].out_stream_index = j;
280 static int match_streams(AVFormatContext *avf)
282 ConcatContext *cat = avf->priv_data;
286 if (cat->cur_file->nb_streams >= cat->avf->nb_streams)
288 map = av_realloc(cat->cur_file->streams,
289 cat->avf->nb_streams * sizeof(*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));
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)
301 switch (cat->stream_match_mode) {
302 case MATCH_ONE_TO_ONE:
303 ret = match_streams_one_to_one(avf);
306 ret = match_streams_exact_id(avf);
313 cat->cur_file->nb_streams = cat->avf->nb_streams;
317 static int open_file(AVFormatContext *avf, unsigned fileno)
319 ConcatContext *cat = avf->priv_data;
320 ConcatFile *file = &cat->files[fileno];
324 avformat_close_input(&cat->avf);
326 cat->avf = avformat_alloc_context();
328 return AVERROR(ENOMEM);
330 cat->avf->flags |= avf->flags & ~AVFMT_FLAG_CUSTOM_IO;
331 cat->avf->interrupt_callback = avf->interrupt_callback;
333 if ((ret = ff_copy_whiteblacklists(cat->avf, avf)) < 0)
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);
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;
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);
358 if ((ret = match_streams(avf)) < 0)
360 if (file->inpoint != AV_NOPTS_VALUE) {
361 if ((ret = avformat_seek_file(cat->avf, -1, INT64_MIN, file->inpoint, file->inpoint, 0)) < 0)
367 static int concat_read_close(AVFormatContext *avf)
369 ConcatContext *cat = avf->priv_data;
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);
378 av_freep(&cat->files[i].streams);
379 av_dict_free(&cat->files[i].metadata);
382 avformat_close_input(&cat->avf);
383 av_freep(&cat->files);
387 static int concat_read_header(AVFormatContext *avf)
389 ConcatContext *cat = avf->priv_data;
391 uint8_t *cursor, *keyword;
393 unsigned nb_files_alloc = 0;
394 ConcatFile *file = NULL;
395 int64_t ret, time = 0;
397 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
399 while ((ret = ff_read_line_to_bprint_overwrite(avf->pb, &bp)) >= 0) {
402 keyword = get_keyword(&cursor);
403 if (!*keyword || *keyword == '#')
406 if (!strcmp(keyword, "file")) {
407 char *filename = av_get_token((const char **)&cursor, SPACE_CHARS);
409 av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line);
410 FAIL(AVERROR_INVALIDDATA);
412 if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0)
414 } else if (!strcmp(keyword, "duration") || !strcmp(keyword, "inpoint") || !strcmp(keyword, "outpoint")) {
415 char *dur_str = get_keyword(&cursor);
418 av_log(avf, AV_LOG_ERROR, "Line %d: %s without file\n",
420 FAIL(AVERROR_INVALIDDATA);
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);
427 if (!strcmp(keyword, "duration"))
428 file->duration = dur;
429 else if (!strcmp(keyword, "inpoint"))
431 else if (!strcmp(keyword, "outpoint"))
432 file->outpoint = dur;
433 } else if (!strcmp(keyword, "file_packet_metadata")) {
436 av_log(avf, AV_LOG_ERROR, "Line %d: %s without file\n",
438 FAIL(AVERROR_INVALIDDATA);
440 metadata = av_get_token((const char **)&cursor, SPACE_CHARS);
442 av_log(avf, AV_LOG_ERROR, "Line %d: packet metadata required\n", line);
443 FAIL(AVERROR_INVALIDDATA);
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);
448 FAIL(AVERROR_INVALIDDATA);
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",
458 FAIL(AVERROR_INVALIDDATA);
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);
467 FAIL(AVERROR_INVALIDDATA);
472 av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
474 FAIL(AVERROR_INVALIDDATA);
477 if (ret != AVERROR_EOF && ret < 0)
480 FAIL(AVERROR_INVALIDDATA);
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;
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)
490 cat->files[i].duration = cat->files[i].outpoint - cat->files[i].inpoint;
492 time += cat->files[i].duration;
494 if (i == cat->nb_files) {
495 avf->duration = time;
499 cat->stream_match_mode = avf->nb_streams ? MATCH_EXACT_ID :
501 if ((ret = open_file(avf, 0)) < 0)
503 av_bprint_finalize(&bp, NULL);
507 av_bprint_finalize(&bp, NULL);
508 concat_read_close(avf);
512 static int open_next_file(AVFormatContext *avf)
514 ConcatContext *cat = avf->priv_data;
515 unsigned fileno = cat->cur_file - cat->files;
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;
521 cat->cur_file->duration = cat->cur_file->next_dts;
523 cat->cur_file->duration -= (cat->cur_file->file_inpoint - cat->cur_file->file_start_time);
526 if (++fileno >= cat->nb_files) {
530 return open_file(avf, fileno);
533 static int filter_packet(AVFormatContext *avf, ConcatStream *cs, AVPacket *pkt)
538 ret = av_bsf_send_packet(cs->bsf, pkt);
540 av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb filter "
541 "failed to send input packet\n");
542 av_packet_unref(pkt);
547 ret = av_bsf_receive_packet(cs->bsf, pkt);
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");
558 /* Returns true if the packet dts is greater or equal to the specified outpoint. */
559 static int packet_after_outpoint(ConcatContext *cat, AVPacket *pkt)
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;
568 static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
570 ConcatContext *cat = avf->priv_data;
583 ret = av_read_frame(cat->avf, pkt);
584 if (ret == AVERROR_EOF) {
585 if ((ret = open_next_file(avf)) < 0)
591 if ((ret = match_streams(avf)) < 0) {
592 av_packet_unref(pkt);
595 if (packet_after_outpoint(cat, pkt)) {
596 av_packet_unref(pkt);
597 if ((ret = open_next_file(avf)) < 0)
601 cs = &cat->cur_file->streams[pkt->stream_index];
602 if (cs->out_stream_index < 0) {
603 av_packet_unref(pkt);
608 if ((ret = filter_packet(avf, cs, pkt)))
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));
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)
622 if (pkt->dts != AV_NOPTS_VALUE)
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) {
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);
637 memcpy(metadata, packed_metadata, metadata_len);
638 av_freep(&packed_metadata);
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;
648 pkt->stream_index = cs->out_stream_index;
652 static void rescale_interval(AVRational tb_in, AVRational tb_out,
653 int64_t *min_ts, int64_t *ts, int64_t *max_ts)
655 *ts = av_rescale_q (* ts, tb_in, tb_out);
656 *min_ts = av_rescale_q_rnd(*min_ts, tb_in, tb_out,
657 AV_ROUND_UP | AV_ROUND_PASS_MINMAX);
658 *max_ts = av_rescale_q_rnd(*max_ts, tb_in, tb_out,
659 AV_ROUND_DOWN | AV_ROUND_PASS_MINMAX);
662 static int try_seek(AVFormatContext *avf, int stream,
663 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
665 ConcatContext *cat = avf->priv_data;
666 int64_t t0 = cat->cur_file->start_time - cat->cur_file->file_inpoint;
669 min_ts = min_ts == INT64_MIN ? INT64_MIN : min_ts - t0;
670 max_ts = max_ts == INT64_MAX ? INT64_MAX : max_ts - t0;
672 if (stream >= cat->avf->nb_streams)
674 rescale_interval(AV_TIME_BASE_Q, cat->avf->streams[stream]->time_base,
675 &min_ts, &ts, &max_ts);
677 return avformat_seek_file(cat->avf, stream, min_ts, ts, max_ts, flags);
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)
683 ConcatContext *cat = avf->priv_data;
684 int ret, left, right;
687 if (stream >= avf->nb_streams)
688 return AVERROR(EINVAL);
689 rescale_interval(avf->streams[stream]->time_base, AV_TIME_BASE_Q,
690 &min_ts, &ts, &max_ts);
694 right = cat->nb_files;
695 while (right - left > 1) {
696 int mid = (left + right) / 2;
697 if (ts < cat->files[mid].start_time)
703 if (cat->cur_file != &cat->files[left]) {
704 if ((ret = open_file(avf, left)) < 0)
710 ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
712 left < cat->nb_files - 1 &&
713 cat->files[left + 1].start_time < max_ts) {
714 if (cat->cur_file == &cat->files[left])
716 if ((ret = open_file(avf, left + 1)) < 0)
718 ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
723 static int concat_seek(AVFormatContext *avf, int stream,
724 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
726 ConcatContext *cat = avf->priv_data;
727 ConcatFile *cur_file_saved = cat->cur_file;
728 AVFormatContext *cur_avf_saved = cat->avf;
732 return AVERROR(ESPIPE); /* XXX: can we use it? */
733 if (flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME))
734 return AVERROR(ENOSYS);
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) {
739 avformat_close_input(&cat->avf);
741 cat->avf = cur_avf_saved;
742 cat->cur_file = cur_file_saved;
744 if (cat->cur_file != cur_file_saved) {
745 avformat_close_input(&cur_avf_saved);
752 #define OFFSET(x) offsetof(ConcatContext, x)
753 #define DEC AV_OPT_FLAG_DECODING_PARAM
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 },
765 static const AVClass concat_class = {
766 .class_name = "concat demuxer",
767 .item_name = av_default_item_name,
769 .version = LIBAVUTIL_VERSION_INT,
773 AVInputFormat ff_concat_demuxer = {
775 .long_name = NULL_IF_CONFIG_SMALL("Virtual concatenation script"),
776 .priv_data_size = sizeof(ConcatContext),
777 .read_probe = concat_probe,
778 .read_header = concat_read_header,
779 .read_packet = concat_read_packet,
780 .read_close = concat_read_close,
781 .read_seek2 = concat_seek,
782 .priv_class = &concat_class,