/* * Copyright (c) 2012 Nicolas George * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with FFmpeg; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "libavutil/avassert.h"#include "libavutil/avstring.h"#include "libavutil/intreadwrite.h"#include "libavutil/opt.h"#include "libavutil/parseutils.h"#include "libavutil/timestamp.h"#include "avformat.h"#include "internal.h"#include "url.h" typedef enum ConcatMatchMode { MATCH_ONE_TO_ONE, MATCH_EXACT_ID,} ConcatMatchMode; typedef struct ConcatStream { AVBSFContext *bsf; int out_stream_index;} ConcatStream; typedef struct { char *url; int64_t start_time; int64_t file_start_time; int64_t file_inpoint; int64_t duration; ConcatStream *streams; int64_t inpoint; int64_t outpoint; AVDictionary *metadata; int nb_streams;} ConcatFile; typedef struct { AVClass *class; ConcatFile *files; ConcatFile *cur_file; unsigned nb_files; AVFormatContext *avf; int safe; int seekable; int eof; ConcatMatchMode stream_match_mode; unsigned auto_convert; int segment_time_metadata;} ConcatContext; static int concat_probe(AVProbeData *probe){ return memcmp(probe->buf, "ffconcat version 1.0", 20) ? 0 : AVPROBE_SCORE_MAX;} static char *get_keyword(uint8_t **cursor){ char *ret = *cursor += strspn(*cursor, SPACE_CHARS); *cursor += strcspn(*cursor, SPACE_CHARS); if (**cursor) { *((*cursor)++) = 0; *cursor += strspn(*cursor, SPACE_CHARS); } return ret;} static int safe_filename(const char *f){ const char *start = f; for (; *f; f++) { /* A-Za-z0-9_- */ if (!((unsigned)((*f | 32) - 'a') < 26 || (unsigned)(*f - '0') < 10 || *f == '_' || *f == '-')) { if (f == start) return 0; else if (*f == '/') start = f + 1; else if (*f != '.') return 0; } } return 1;} #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0) static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile, unsigned *nb_files_alloc){ ConcatContext *cat = avf->priv_data; ConcatFile *file; char *url = NULL; const char *proto; size_t url_len, proto_len; int ret; if (cat->safe > 0 && !safe_filename(filename)) { av_log(avf, AV_LOG_ERROR, "Unsafe file name '%s'\n", filename); FAIL(AVERROR(EPERM)); } proto = avio_find_protocol_name(filename); proto_len = proto ? strlen(proto) : 0; if (proto && !memcmp(filename, proto, proto_len) && (filename[proto_len] == ':' || filename[proto_len] == ',')) { url = filename; filename = NULL; } else { url_len = strlen(avf->filename) + strlen(filename) + 16; if (!(url = av_malloc(url_len))) FAIL(AVERROR(ENOMEM)); ff_make_absolute_url(url, url_len, avf->filename, filename); av_freep(&filename); } if (cat->nb_files >= *nb_files_alloc) { size_t n = FFMAX(*nb_files_alloc * 2, 16); ConcatFile *new_files; if (n <= cat->nb_files || n > SIZE_MAX / sizeof(*cat->files) || !(new_files = av_realloc(cat->files, n * sizeof(*cat->files)))) FAIL(AVERROR(ENOMEM)); cat->files = new_files; *nb_files_alloc = n; } file = &cat->files[cat->nb_files++]; memset(file, 0, sizeof(*file)); *rfile = file; file->url = url; file->start_time = AV_NOPTS_VALUE; file->duration = AV_NOPTS_VALUE; file->inpoint = AV_NOPTS_VALUE; file->outpoint = AV_NOPTS_VALUE; return 0; fail: av_free(url); av_free(filename); return ret;} static int copy_stream_props(AVStream *st, AVStream *source_st){ int ret; if (st->codecpar->codec_id || !source_st->codecpar->codec_id) { if (st->codecpar->extradata_size < source_st->codecpar->extradata_size) { if (st->codecpar->extradata) { av_freep(&st->codecpar->extradata); st->codecpar->extradata_size = 0; } ret = ff_alloc_extradata(st->codecpar, source_st->codecpar->extradata_size); if (ret < 0) return ret; } memcpy(st->codecpar->extradata, source_st->codecpar->extradata, source_st->codecpar->extradata_size); return 0; } if ((ret = avcodec_parameters_copy(st->codecpar, source_st->codecpar)) < 0) return ret; st->r_frame_rate = source_st->r_frame_rate; st->avg_frame_rate = source_st->avg_frame_rate; st->time_base = source_st->time_base; st->sample_aspect_ratio = source_st->sample_aspect_ratio; av_dict_copy(&st->metadata, source_st->metadata, 0); return 0;} static int detect_stream_specific(AVFormatContext *avf, int idx){ ConcatContext *cat = avf->priv_data; AVStream *st = cat->avf->streams[idx]; ConcatStream *cs = &cat->cur_file->streams[idx]; const AVBitStreamFilter *filter; AVBSFContext *bsf; int ret; if (cat->auto_convert && st->codecpar->codec_id == AV_CODEC_ID_H264) { if (!st->codecpar->extradata_size || (st->codecpar->extradata_size >= 3 && AV_RB24(st->codecpar->extradata) == 1) || (st->codecpar->extradata_size >= 4 && AV_RB32(st->codecpar->extradata) == 1)) return 0; av_log(cat->avf, AV_LOG_INFO, "Auto-inserting h264_mp4toannexb bitstream filter\n"); filter = av_bsf_get_by_name("h264_mp4toannexb"); if (!filter) { av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb bitstream filter " "required for H.264 streams\n"); return AVERROR_BSF_NOT_FOUND; } ret = av_bsf_alloc(filter, &bsf); if (ret < 0) return ret; cs->bsf = bsf; ret = avcodec_parameters_copy(bsf->par_in, st->codecpar); if (ret < 0) return ret; ret = av_bsf_init(bsf); if (ret < 0) return ret; ret = avcodec_parameters_copy(st->codecpar, bsf->par_out); if (ret < 0) return ret; } return 0;} static int match_streams_one_to_one(AVFormatContext *avf){ ConcatContext *cat = avf->priv_data; AVStream *st; int i, ret; for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) { if (i < avf->nb_streams) { st = avf->streams[i]; } else { if (!(st = avformat_new_stream(avf, NULL))) return AVERROR(ENOMEM); } if ((ret = copy_stream_props(st, cat->avf->streams[i])) < 0) return ret; cat->cur_file->streams[i].out_stream_index = i; } return 0;} static int match_streams_exact_id(AVFormatContext *avf){ ConcatContext *cat = avf->priv_data; AVStream *st; int i, j, ret; for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) { st = cat->avf->streams[i]; for (j = 0; j < avf->nb_streams; j++) { if (avf->streams[j]->id == st->id) { av_log(avf, AV_LOG_VERBOSE, "Match slave stream #%d with stream #%d id 0x%x\n", i, j, st->id); if ((ret = copy_stream_props(avf->streams[j], st)) < 0) return ret; cat->cur_file->streams[i].out_stream_index = j; } } } return 0;} static int match_streams(AVFormatContext *avf){ ConcatContext *cat = avf->priv_data; ConcatStream *map; int i, ret; if (cat->cur_file->nb_streams >= cat->avf->nb_streams) return 0; map = av_realloc(cat->cur_file->streams, cat->avf->nb_streams * sizeof(*map)); if (!map) return AVERROR(ENOMEM); cat->cur_file->streams = map; memset(map + cat->cur_file->nb_streams, 0, (cat->avf->nb_streams - cat->cur_file->nb_streams) * sizeof(*map)); for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) { map[i].out_stream_index = -1; if ((ret = detect_stream_specific(avf, i)) < 0) return ret; } switch (cat->stream_match_mode) { case MATCH_ONE_TO_ONE: ret = match_streams_one_to_one(avf); break; case MATCH_EXACT_ID: ret = match_streams_exact_id(avf); break; default: ret = AVERROR_BUG; } if (ret < 0) return ret; cat->cur_file->nb_streams = cat->avf->nb_streams; return 0;} static int open_file(AVFormatContext *avf, unsigned fileno){ ConcatContext *cat = avf->priv_data; ConcatFile *file = &cat->files[fileno]; int ret; if (cat->avf) avformat_close_input(&cat->avf); cat->avf = avformat_alloc_context(); if (!cat->avf) return AVERROR(ENOMEM); cat->avf->flags |= avf->flags & ~AVFMT_FLAG_CUSTOM_IO; cat->avf->interrupt_callback = avf->interrupt_callback; if ((ret = ff_copy_whiteblacklists(cat->avf, avf)) < 0) return ret; if ((ret = avformat_open_input(&cat->avf, file->url, NULL, NULL)) < 0 || (ret = avformat_find_stream_info(cat->avf, NULL)) < 0) { av_log(avf, AV_LOG_ERROR, "Impossible to open '%s'\n", file->url); avformat_close_input(&cat->avf); return ret; } cat->cur_file = file; if (file->start_time == AV_NOPTS_VALUE) file->start_time = !fileno ? 0 : cat->files[fileno - 1].start_time + cat->files[fileno - 1].duration; file->file_start_time = (cat->avf->start_time == AV_NOPTS_VALUE) ? 0 : cat->avf->start_time; file->file_inpoint = (file->inpoint == AV_NOPTS_VALUE) ? file->file_start_time : file->inpoint; if (file->duration == AV_NOPTS_VALUE && file->outpoint != AV_NOPTS_VALUE) file->duration = file->outpoint - file->file_inpoint; if (cat->segment_time_metadata) { av_dict_set_int(&file->metadata, "lavf.concatdec.start_time", file->start_time, 0); if (file->duration != AV_NOPTS_VALUE) av_dict_set_int(&file->metadata, "lavf.concatdec.duration", file->duration, 0); } if ((ret = match_streams(avf)) < 0) return ret; if (file->inpoint != AV_NOPTS_VALUE) { if ((ret = avformat_seek_file(cat->avf, -1, INT64_MIN, file->inpoint, file->inpoint, 0)) < 0) return ret; } return 0;} static int concat_read_close(AVFormatContext *avf){ ConcatContext *cat = avf->priv_data; unsigned i, j; for (i = 0; i < cat->nb_files; i++) { av_freep(&cat->files[i].url); for (j = 0; j < cat->files[i].nb_streams; j++) { if (cat->files[i].streams[j].bsf) av_bsf_free(&cat->files[i].streams[j].bsf); } av_freep(&cat->files[i].streams); av_dict_free(&cat->files[i].metadata); } if (cat->avf) avformat_close_input(&cat->avf); av_freep(&cat->files); return 0;} static int concat_read_header(AVFormatContext *avf){ ConcatContext *cat = avf->priv_data; uint8_t buf[4096]; uint8_t *cursor, *keyword; int ret, line = 0, i; unsigned nb_files_alloc = 0; ConcatFile *file = NULL; int64_t time = 0; while (1) { if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0) break; line++; cursor = buf; keyword = get_keyword(&cursor); if (!*keyword || *keyword == '#') continue; if (!strcmp(keyword, "file")) { char *filename = av_get_token((const char **)&cursor, SPACE_CHARS); if (!filename) { av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line); FAIL(AVERROR_INVALIDDATA); } if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0) goto fail; } else if (!strcmp(keyword, "duration") || !strcmp(keyword, "inpoint") || !strcmp(keyword, "outpoint")) { char *dur_str = get_keyword(&cursor); int64_t dur; if (!file) { av_log(avf, AV_LOG_ERROR, "Line %d: %s without file\n", line, keyword); FAIL(AVERROR_INVALIDDATA); } if ((ret = av_parse_time(&dur, dur_str, 1)) < 0) { av_log(avf, AV_LOG_ERROR, "Line %d: invalid %s '%s'\n", line, keyword, dur_str); goto fail; } if (!strcmp(keyword, "duration")) file->duration = dur; else if (!strcmp(keyword, "inpoint")) file->inpoint = dur; else if (!strcmp(keyword, "outpoint")) file->outpoint = dur; } else if (!strcmp(keyword, "file_packet_metadata")) { char *metadata; if (!file) { av_log(avf, AV_LOG_ERROR, "Line %d: %s without file\n", line, keyword); FAIL(AVERROR_INVALIDDATA); } metadata = av_get_token((const char **)&cursor, SPACE_CHARS); if (!metadata) { av_log(avf, AV_LOG_ERROR, "Line %d: packet metadata required\n", line); FAIL(AVERROR_INVALIDDATA); } if ((ret = av_dict_parse_string(&file->metadata, metadata, "=", "", 0)) < 0) { av_log(avf, AV_LOG_ERROR, "Line %d: failed to parse metadata string\n", line); av_freep(&metadata); FAIL(AVERROR_INVALIDDATA); } av_freep(&metadata); } else if (!strcmp(keyword, "stream")) { if (!avformat_new_stream(avf, NULL)) FAIL(AVERROR(ENOMEM)); } else if (!strcmp(keyword, "exact_stream_id")) { if (!avf->nb_streams) { av_log(avf, AV_LOG_ERROR, "Line %d: exact_stream_id without stream\n", line); FAIL(AVERROR_INVALIDDATA); } avf->streams[avf->nb_streams - 1]->id = strtol(get_keyword(&cursor), NULL, 0); } else if (!strcmp(keyword, "ffconcat")) { char *ver_kw = get_keyword(&cursor); char *ver_val = get_keyword(&cursor); if (strcmp(ver_kw, "version") || strcmp(ver_val, "1.0")) { av_log(avf, AV_LOG_ERROR, "Line %d: invalid version\n", line); FAIL(AVERROR_INVALIDDATA); } if (cat->safe < 0) cat->safe = 1; } else { av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n", line, keyword); FAIL(AVERROR_INVALIDDATA); } } if (ret < 0) goto fail; if (!cat->nb_files) FAIL(AVERROR_INVALIDDATA); for (i = 0; i < cat->nb_files; i++) { if (cat->files[i].start_time == AV_NOPTS_VALUE) cat->files[i].start_time = time; else time = cat->files[i].start_time; if (cat->files[i].duration == AV_NOPTS_VALUE) { if (cat->files[i].inpoint == AV_NOPTS_VALUE || cat->files[i].outpoint == AV_NOPTS_VALUE) break; cat->files[i].duration = cat->files[i].outpoint - cat->files[i].inpoint; } time += cat->files[i].duration; } if (i == cat->nb_files) { avf->duration = time; cat->seekable = 1; } cat->stream_match_mode = avf->nb_streams ? MATCH_EXACT_ID : MATCH_ONE_TO_ONE; if ((ret = open_file(avf, 0)) < 0) goto fail; return 0;Lines 1–500 of 764. Load more
/* * Copyright (c) 2012 Nicolas George * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with FFmpeg; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "libavutil/avassert.h" #include "libavutil/avstring.h" #include "libavutil/intreadwrite.h" #include "libavutil/opt.h" #include "libavutil/parseutils.h" #include "libavutil/timestamp.h" #include "avformat.h" #include "internal.h" #include "url.h" typedef enum ConcatMatchMode { MATCH_ONE_TO_ONE, MATCH_EXACT_ID, } ConcatMatchMode; typedef struct ConcatStream { AVBSFContext *bsf; int out_stream_index; } ConcatStream; typedef struct { char *url; int64_t start_time; int64_t file_start_time; int64_t file_inpoint; int64_t duration; ConcatStream *streams; int64_t inpoint; int64_t outpoint; AVDictionary *metadata; int nb_streams; } ConcatFile; typedef struct { AVClass *class; ConcatFile *files; ConcatFile *cur_file; unsigned nb_files; AVFormatContext *avf; int safe; int seekable; int eof; ConcatMatchMode stream_match_mode; unsigned auto_convert; int segment_time_metadata; } ConcatContext; static int concat_probe(AVProbeData *probe) { return memcmp(probe->buf, "ffconcat version 1.0", 20) ? 0 : AVPROBE_SCORE_MAX; } static char *get_keyword(uint8_t **cursor) { char *ret = *cursor += strspn(*cursor, SPACE_CHARS); *cursor += strcspn(*cursor, SPACE_CHARS); if (**cursor) { *((*cursor)++) = 0; *cursor += strspn(*cursor, SPACE_CHARS); } return ret; } static int safe_filename(const char *f) { const char *start = f; for (; *f; f++) { /* A-Za-z0-9_- */ if (!((unsigned)((*f | 32) - 'a') < 26 || (unsigned)(*f - '0') < 10 || *f == '_' || *f == '-')) { if (f == start) return 0; else if (*f == '/') start = f + 1; else if (*f != '.') return 0; } } return 1; } #define FAIL(retcode) do { ret = (retcode); goto fail; } while(0) static int add_file(AVFormatContext *avf, char *filename, ConcatFile **rfile, unsigned *nb_files_alloc) { ConcatContext *cat = avf->priv_data; ConcatFile *file; char *url = NULL; const char *proto; size_t url_len, proto_len; int ret; if (cat->safe > 0 && !safe_filename(filename)) { av_log(avf, AV_LOG_ERROR, "Unsafe file name '%s'\n", filename); FAIL(AVERROR(EPERM)); } proto = avio_find_protocol_name(filename); proto_len = proto ? strlen(proto) : 0; if (proto && !memcmp(filename, proto, proto_len) && (filename[proto_len] == ':' || filename[proto_len] == ',')) { url = filename; filename = NULL; } else { url_len = strlen(avf->filename) + strlen(filename) + 16; if (!(url = av_malloc(url_len))) FAIL(AVERROR(ENOMEM)); ff_make_absolute_url(url, url_len, avf->filename, filename); av_freep(&filename); } if (cat->nb_files >= *nb_files_alloc) { size_t n = FFMAX(*nb_files_alloc * 2, 16); ConcatFile *new_files; if (n <= cat->nb_files || n > SIZE_MAX / sizeof(*cat->files) || !(new_files = av_realloc(cat->files, n * sizeof(*cat->files)))) FAIL(AVERROR(ENOMEM)); cat->files = new_files; *nb_files_alloc = n; } file = &cat->files[cat->nb_files++]; memset(file, 0, sizeof(*file)); *rfile = file; file->url = url; file->start_time = AV_NOPTS_VALUE; file->duration = AV_NOPTS_VALUE; file->inpoint = AV_NOPTS_VALUE; file->outpoint = AV_NOPTS_VALUE; return 0; fail: av_free(url); av_free(filename); return ret; } static int copy_stream_props(AVStream *st, AVStream *source_st) { int ret; if (st->codecpar->codec_id || !source_st->codecpar->codec_id) { if (st->codecpar->extradata_size < source_st->codecpar->extradata_size) { if (st->codecpar->extradata) { av_freep(&st->codecpar->extradata); st->codecpar->extradata_size = 0; } ret = ff_alloc_extradata(st->codecpar, source_st->codecpar->extradata_size); if (ret < 0) return ret; } memcpy(st->codecpar->extradata, source_st->codecpar->extradata, source_st->codecpar->extradata_size); return 0; } if ((ret = avcodec_parameters_copy(st->codecpar, source_st->codecpar)) < 0) return ret; st->r_frame_rate = source_st->r_frame_rate; st->avg_frame_rate = source_st->avg_frame_rate; st->time_base = source_st->time_base; st->sample_aspect_ratio = source_st->sample_aspect_ratio; av_dict_copy(&st->metadata, source_st->metadata, 0); return 0; } static int detect_stream_specific(AVFormatContext *avf, int idx) { ConcatContext *cat = avf->priv_data; AVStream *st = cat->avf->streams[idx]; ConcatStream *cs = &cat->cur_file->streams[idx]; const AVBitStreamFilter *filter; AVBSFContext *bsf; int ret; if (cat->auto_convert && st->codecpar->codec_id == AV_CODEC_ID_H264) { if (!st->codecpar->extradata_size || (st->codecpar->extradata_size >= 3 && AV_RB24(st->codecpar->extradata) == 1) || (st->codecpar->extradata_size >= 4 && AV_RB32(st->codecpar->extradata) == 1)) return 0; av_log(cat->avf, AV_LOG_INFO, "Auto-inserting h264_mp4toannexb bitstream filter\n"); filter = av_bsf_get_by_name("h264_mp4toannexb"); if (!filter) { av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb bitstream filter " "required for H.264 streams\n"); return AVERROR_BSF_NOT_FOUND; } ret = av_bsf_alloc(filter, &bsf); if (ret < 0) return ret; cs->bsf = bsf; ret = avcodec_parameters_copy(bsf->par_in, st->codecpar); if (ret < 0) return ret; ret = av_bsf_init(bsf); if (ret < 0) return ret; ret = avcodec_parameters_copy(st->codecpar, bsf->par_out); if (ret < 0) return ret; } return 0; } static int match_streams_one_to_one(AVFormatContext *avf) { ConcatContext *cat = avf->priv_data; AVStream *st; int i, ret; for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) { if (i < avf->nb_streams) { st = avf->streams[i]; } else { if (!(st = avformat_new_stream(avf, NULL))) return AVERROR(ENOMEM); } if ((ret = copy_stream_props(st, cat->avf->streams[i])) < 0) return ret; cat->cur_file->streams[i].out_stream_index = i; } return 0; } static int match_streams_exact_id(AVFormatContext *avf) { ConcatContext *cat = avf->priv_data; AVStream *st; int i, j, ret; for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) { st = cat->avf->streams[i]; for (j = 0; j < avf->nb_streams; j++) { if (avf->streams[j]->id == st->id) { av_log(avf, AV_LOG_VERBOSE, "Match slave stream #%d with stream #%d id 0x%x\n", i, j, st->id); if ((ret = copy_stream_props(avf->streams[j], st)) < 0) return ret; cat->cur_file->streams[i].out_stream_index = j; } } } return 0; } static int match_streams(AVFormatContext *avf) { ConcatContext *cat = avf->priv_data; ConcatStream *map; int i, ret; if (cat->cur_file->nb_streams >= cat->avf->nb_streams) return 0; map = av_realloc(cat->cur_file->streams, cat->avf->nb_streams * sizeof(*map)); if (!map) return AVERROR(ENOMEM); cat->cur_file->streams = map; memset(map + cat->cur_file->nb_streams, 0, (cat->avf->nb_streams - cat->cur_file->nb_streams) * sizeof(*map)); for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) { map[i].out_stream_index = -1; if ((ret = detect_stream_specific(avf, i)) < 0) return ret; } switch (cat->stream_match_mode) { case MATCH_ONE_TO_ONE: ret = match_streams_one_to_one(avf); break; case MATCH_EXACT_ID: ret = match_streams_exact_id(avf); break; default: ret = AVERROR_BUG; } if (ret < 0) return ret; cat->cur_file->nb_streams = cat->avf->nb_streams; return 0; } static int open_file(AVFormatContext *avf, unsigned fileno) { ConcatContext *cat = avf->priv_data; ConcatFile *file = &cat->files[fileno]; int ret; if (cat->avf) avformat_close_input(&cat->avf); cat->avf = avformat_alloc_context(); if (!cat->avf) return AVERROR(ENOMEM); cat->avf->flags |= avf->flags & ~AVFMT_FLAG_CUSTOM_IO; cat->avf->interrupt_callback = avf->interrupt_callback; if ((ret = ff_copy_whiteblacklists(cat->avf, avf)) < 0) return ret; if ((ret = avformat_open_input(&cat->avf, file->url, NULL, NULL)) < 0 || (ret = avformat_find_stream_info(cat->avf, NULL)) < 0) { av_log(avf, AV_LOG_ERROR, "Impossible to open '%s'\n", file->url); avformat_close_input(&cat->avf); return ret; } cat->cur_file = file; if (file->start_time == AV_NOPTS_VALUE) file->start_time = !fileno ? 0 : cat->files[fileno - 1].start_time + cat->files[fileno - 1].duration; file->file_start_time = (cat->avf->start_time == AV_NOPTS_VALUE) ? 0 : cat->avf->start_time; file->file_inpoint = (file->inpoint == AV_NOPTS_VALUE) ? file->file_start_time : file->inpoint; if (file->duration == AV_NOPTS_VALUE && file->outpoint != AV_NOPTS_VALUE) file->duration = file->outpoint - file->file_inpoint; if (cat->segment_time_metadata) { av_dict_set_int(&file->metadata, "lavf.concatdec.start_time", file->start_time, 0); if (file->duration != AV_NOPTS_VALUE) av_dict_set_int(&file->metadata, "lavf.concatdec.duration", file->duration, 0); } if ((ret = match_streams(avf)) < 0) return ret; if (file->inpoint != AV_NOPTS_VALUE) { if ((ret = avformat_seek_file(cat->avf, -1, INT64_MIN, file->inpoint, file->inpoint, 0)) < 0) return ret; } return 0; } static int concat_read_close(AVFormatContext *avf) { ConcatContext *cat = avf->priv_data; unsigned i, j; for (i = 0; i < cat->nb_files; i++) { av_freep(&cat->files[i].url); for (j = 0; j < cat->files[i].nb_streams; j++) { if (cat->files[i].streams[j].bsf) av_bsf_free(&cat->files[i].streams[j].bsf); } av_freep(&cat->files[i].streams); av_dict_free(&cat->files[i].metadata); } if (cat->avf) avformat_close_input(&cat->avf); av_freep(&cat->files); return 0; } static int concat_read_header(AVFormatContext *avf) { ConcatContext *cat = avf->priv_data; uint8_t buf[4096]; uint8_t *cursor, *keyword; int ret, line = 0, i; unsigned nb_files_alloc = 0; ConcatFile *file = NULL; int64_t time = 0; while (1) { if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0) break; line++; cursor = buf; keyword = get_keyword(&cursor); if (!*keyword || *keyword == '#') continue; if (!strcmp(keyword, "file")) { char *filename = av_get_token((const char **)&cursor, SPACE_CHARS); if (!filename) { av_log(avf, AV_LOG_ERROR, "Line %d: filename required\n", line); FAIL(AVERROR_INVALIDDATA); } if ((ret = add_file(avf, filename, &file, &nb_files_alloc)) < 0) goto fail; } else if (!strcmp(keyword, "duration") || !strcmp(keyword, "inpoint") || !strcmp(keyword, "outpoint")) { char *dur_str = get_keyword(&cursor); int64_t dur; if (!file) { av_log(avf, AV_LOG_ERROR, "Line %d: %s without file\n", line, keyword); FAIL(AVERROR_INVALIDDATA); } if ((ret = av_parse_time(&dur, dur_str, 1)) < 0) { av_log(avf, AV_LOG_ERROR, "Line %d: invalid %s '%s'\n", line, keyword, dur_str); goto fail; } if (!strcmp(keyword, "duration")) file->duration = dur; else if (!strcmp(keyword, "inpoint")) file->inpoint = dur; else if (!strcmp(keyword, "outpoint")) file->outpoint = dur; } else if (!strcmp(keyword, "file_packet_metadata")) { char *metadata; if (!file) { av_log(avf, AV_LOG_ERROR, "Line %d: %s without file\n", line, keyword); FAIL(AVERROR_INVALIDDATA); } metadata = av_get_token((const char **)&cursor, SPACE_CHARS); if (!metadata) { av_log(avf, AV_LOG_ERROR, "Line %d: packet metadata required\n", line); FAIL(AVERROR_INVALIDDATA); } if ((ret = av_dict_parse_string(&file->metadata, metadata, "=", "", 0)) < 0) { av_log(avf, AV_LOG_ERROR, "Line %d: failed to parse metadata string\n", line); av_freep(&metadata); FAIL(AVERROR_INVALIDDATA); } av_freep(&metadata); } else if (!strcmp(keyword, "stream")) { if (!avformat_new_stream(avf, NULL)) FAIL(AVERROR(ENOMEM)); } else if (!strcmp(keyword, "exact_stream_id")) { if (!avf->nb_streams) { av_log(avf, AV_LOG_ERROR, "Line %d: exact_stream_id without stream\n", line); FAIL(AVERROR_INVALIDDATA); } avf->streams[avf->nb_streams - 1]->id = strtol(get_keyword(&cursor), NULL, 0); } else if (!strcmp(keyword, "ffconcat")) { char *ver_kw = get_keyword(&cursor); char *ver_val = get_keyword(&cursor); if (strcmp(ver_kw, "version") || strcmp(ver_val, "1.0")) { av_log(avf, AV_LOG_ERROR, "Line %d: invalid version\n", line); FAIL(AVERROR_INVALIDDATA); } if (cat->safe < 0) cat->safe = 1; } else { av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n", line, keyword); FAIL(AVERROR_INVALIDDATA); } } if (ret < 0) goto fail; if (!cat->nb_files) FAIL(AVERROR_INVALIDDATA); for (i = 0; i < cat->nb_files; i++) { if (cat->files[i].start_time == AV_NOPTS_VALUE) cat->files[i].start_time = time; else time = cat->files[i].start_time; if (cat->files[i].duration == AV_NOPTS_VALUE) { if (cat->files[i].inpoint == AV_NOPTS_VALUE || cat->files[i].outpoint == AV_NOPTS_VALUE) break; cat->files[i].duration = cat->files[i].outpoint - cat->files[i].inpoint; } time += cat->files[i].duration; } if (i == cat->nb_files) { avf->duration = time; cat->seekable = 1; } cat->stream_match_mode = avf->nb_streams ? MATCH_EXACT_ID : MATCH_ONE_TO_ONE; if ((ret = open_file(avf, 0)) < 0) goto fail; return 0;