2012-08-22 19:28:37 +03:00
|
|
|
/*
|
|
|
|
* 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/avstring.h"
|
2021-08-31 13:30:01 +02:00
|
|
|
#include "libavutil/avassert.h"
|
2018-02-03 23:05:21 +02:00
|
|
|
#include "libavutil/bprint.h"
|
2014-04-14 14:49:12 +03:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2013-02-03 12:16:33 +03:00
|
|
|
#include "libavutil/opt.h"
|
2013-02-13 19:25:56 +03:00
|
|
|
#include "libavutil/parseutils.h"
|
2014-11-18 11:36:16 +02:00
|
|
|
#include "libavutil/timestamp.h"
|
2021-06-11 23:35:33 +02:00
|
|
|
#include "libavcodec/bsf.h"
|
2012-08-22 19:28:37 +03:00
|
|
|
#include "avformat.h"
|
2021-07-23 04:01:44 +02:00
|
|
|
#include "avio_internal.h"
|
2012-08-22 19:28:37 +03:00
|
|
|
#include "internal.h"
|
2013-06-17 01:05:02 +03:00
|
|
|
#include "url.h"
|
2012-08-22 19:28:37 +03:00
|
|
|
|
2014-04-14 12:57:28 +03:00
|
|
|
typedef enum ConcatMatchMode {
|
|
|
|
MATCH_ONE_TO_ONE,
|
|
|
|
MATCH_EXACT_ID,
|
|
|
|
} ConcatMatchMode;
|
|
|
|
|
2014-04-14 11:34:53 +03:00
|
|
|
typedef struct ConcatStream {
|
2017-04-26 05:36:17 +02:00
|
|
|
AVBSFContext *bsf;
|
2014-04-14 11:34:53 +03:00
|
|
|
int out_stream_index;
|
|
|
|
} ConcatStream;
|
|
|
|
|
2012-08-22 19:28:37 +03:00
|
|
|
typedef struct {
|
|
|
|
char *url;
|
|
|
|
int64_t start_time;
|
2015-07-02 19:56:25 +02:00
|
|
|
int64_t file_start_time;
|
|
|
|
int64_t file_inpoint;
|
2012-08-22 19:28:37 +03:00
|
|
|
int64_t duration;
|
2018-11-22 01:57:01 +02:00
|
|
|
int64_t user_duration;
|
2017-08-29 04:45:20 +02:00
|
|
|
int64_t next_dts;
|
2014-04-14 11:34:53 +03:00
|
|
|
ConcatStream *streams;
|
2015-07-02 19:56:25 +02:00
|
|
|
int64_t inpoint;
|
2015-07-02 20:35:04 +02:00
|
|
|
int64_t outpoint;
|
2015-07-02 23:20:09 +02:00
|
|
|
AVDictionary *metadata;
|
2021-07-28 12:34:19 +02:00
|
|
|
AVDictionary *options;
|
2014-04-14 11:34:53 +03:00
|
|
|
int nb_streams;
|
2012-08-22 19:28:37 +03:00
|
|
|
} ConcatFile;
|
|
|
|
|
|
|
|
typedef struct {
|
2013-02-03 12:16:33 +03:00
|
|
|
AVClass *class;
|
2012-08-22 19:28:37 +03:00
|
|
|
ConcatFile *files;
|
|
|
|
ConcatFile *cur_file;
|
|
|
|
unsigned nb_files;
|
|
|
|
AVFormatContext *avf;
|
2013-02-03 12:16:33 +03:00
|
|
|
int safe;
|
2013-02-14 18:00:06 +03:00
|
|
|
int seekable;
|
2015-07-04 23:34:17 +02:00
|
|
|
int eof;
|
2014-04-14 12:57:28 +03:00
|
|
|
ConcatMatchMode stream_match_mode;
|
2014-04-14 14:49:12 +03:00
|
|
|
unsigned auto_convert;
|
2015-10-24 17:16:11 +02:00
|
|
|
int segment_time_metadata;
|
2012-08-22 19:28:37 +03:00
|
|
|
} ConcatContext;
|
|
|
|
|
2019-03-21 02:18:37 +02:00
|
|
|
static int concat_probe(const AVProbeData *probe)
|
2012-08-22 19:28:37 +03:00
|
|
|
{
|
2013-02-03 12:27:11 +03:00
|
|
|
return memcmp(probe->buf, "ffconcat version 1.0", 20) ?
|
|
|
|
0 : AVPROBE_SCORE_MAX;
|
2012-08-22 19:28:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-02-03 12:16:33 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:28:37 +03:00
|
|
|
#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;
|
2013-03-20 12:18:12 +03:00
|
|
|
char *url = NULL;
|
2014-03-01 14:13:11 +03:00
|
|
|
const char *proto;
|
2020-11-01 00:46:33 +02:00
|
|
|
const char *ptr;
|
|
|
|
size_t url_len;
|
2013-03-20 12:18:12 +03:00
|
|
|
int ret;
|
2012-08-22 19:28:37 +03:00
|
|
|
|
2021-07-28 12:38:08 +02:00
|
|
|
if (cat->safe && !safe_filename(filename)) {
|
2013-02-03 12:16:33 +03:00
|
|
|
av_log(avf, AV_LOG_ERROR, "Unsafe file name '%s'\n", filename);
|
2013-03-20 12:18:12 +03:00
|
|
|
FAIL(AVERROR(EPERM));
|
2013-02-03 12:16:33 +03:00
|
|
|
}
|
2014-03-01 14:13:11 +03:00
|
|
|
|
|
|
|
proto = avio_find_protocol_name(filename);
|
2020-11-01 00:46:33 +02:00
|
|
|
if (proto && av_strstart(filename, proto, &ptr) &&
|
|
|
|
(*ptr == ':' || *ptr == ',')) {
|
2014-03-01 14:13:11 +03:00
|
|
|
url = filename;
|
|
|
|
filename = NULL;
|
|
|
|
} else {
|
2017-12-30 00:30:14 +02:00
|
|
|
url_len = strlen(avf->url) + strlen(filename) + 16;
|
2014-03-08 14:44:45 +03:00
|
|
|
if (!(url = av_malloc(url_len)))
|
|
|
|
FAIL(AVERROR(ENOMEM));
|
2017-12-30 00:30:14 +02:00
|
|
|
ff_make_absolute_url(url, url_len, avf->url, filename);
|
2014-03-08 14:44:45 +03:00
|
|
|
av_freep(&filename);
|
2014-03-01 14:13:11 +03:00
|
|
|
}
|
2012-08-22 19:28:37 +03:00
|
|
|
|
|
|
|
if (cat->nb_files >= *nb_files_alloc) {
|
2012-12-17 16:20:29 +03:00
|
|
|
size_t n = FFMAX(*nb_files_alloc * 2, 16);
|
2012-12-12 14:15:57 +03:00
|
|
|
ConcatFile *new_files;
|
|
|
|
if (n <= cat->nb_files || n > SIZE_MAX / sizeof(*cat->files) ||
|
|
|
|
!(new_files = av_realloc(cat->files, n * sizeof(*cat->files))))
|
2013-03-20 12:18:12 +03:00
|
|
|
FAIL(AVERROR(ENOMEM));
|
2012-12-12 14:15:57 +03:00
|
|
|
cat->files = new_files;
|
2012-08-22 19:28:37 +03:00
|
|
|
*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;
|
2017-08-29 04:45:20 +02:00
|
|
|
file->next_dts = AV_NOPTS_VALUE;
|
2015-07-02 19:56:25 +02:00
|
|
|
file->inpoint = AV_NOPTS_VALUE;
|
2015-07-02 20:35:04 +02:00
|
|
|
file->outpoint = AV_NOPTS_VALUE;
|
2018-11-22 01:57:01 +02:00
|
|
|
file->user_duration = AV_NOPTS_VALUE;
|
2012-08-22 19:28:37 +03:00
|
|
|
|
|
|
|
return 0;
|
2013-03-20 12:18:12 +03:00
|
|
|
|
|
|
|
fail:
|
|
|
|
av_free(url);
|
|
|
|
av_free(filename);
|
|
|
|
return ret;
|
2012-08-22 19:28:37 +03:00
|
|
|
}
|
|
|
|
|
2014-02-26 22:22:46 +03:00
|
|
|
static int copy_stream_props(AVStream *st, AVStream *source_st)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->codec_id || !source_st->codecpar->codec_id) {
|
|
|
|
if (st->codecpar->extradata_size < source_st->codecpar->extradata_size) {
|
|
|
|
ret = ff_alloc_extradata(st->codecpar,
|
|
|
|
source_st->codecpar->extradata_size);
|
2014-04-14 14:49:12 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
2016-04-10 21:58:15 +02:00
|
|
|
memcpy(st->codecpar->extradata, source_st->codecpar->extradata,
|
|
|
|
source_st->codecpar->extradata_size);
|
2014-04-14 12:57:28 +03:00
|
|
|
return 0;
|
2014-04-14 14:49:12 +03:00
|
|
|
}
|
2016-04-10 21:58:15 +02:00
|
|
|
if ((ret = avcodec_parameters_copy(st->codecpar, source_st->codecpar)) < 0)
|
2014-02-26 22:22:46 +03:00
|
|
|
return ret;
|
|
|
|
st->r_frame_rate = source_st->r_frame_rate;
|
|
|
|
st->avg_frame_rate = source_st->avg_frame_rate;
|
|
|
|
st->sample_aspect_ratio = source_st->sample_aspect_ratio;
|
2017-12-30 13:17:08 +02:00
|
|
|
avpriv_set_pts_info(st, 64, source_st->time_base.num, source_st->time_base.den);
|
2015-07-01 12:49:06 +02:00
|
|
|
|
|
|
|
av_dict_copy(&st->metadata, source_st->metadata, 0);
|
2014-02-26 22:22:46 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-14 14:49:12 +03:00
|
|
|
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];
|
2017-04-26 05:36:17 +02:00
|
|
|
const AVBitStreamFilter *filter;
|
|
|
|
AVBSFContext *bsf;
|
2016-04-10 21:58:15 +02:00
|
|
|
int ret;
|
2014-04-14 14:49:12 +03:00
|
|
|
|
2017-04-26 01:23:12 +02:00
|
|
|
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;
|
2014-04-14 14:49:12 +03:00
|
|
|
av_log(cat->avf, AV_LOG_INFO,
|
|
|
|
"Auto-inserting h264_mp4toannexb bitstream filter\n");
|
2017-04-26 05:36:17 +02:00
|
|
|
filter = av_bsf_get_by_name("h264_mp4toannexb");
|
|
|
|
if (!filter) {
|
2014-04-14 14:49:12 +03:00
|
|
|
av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb bitstream filter "
|
|
|
|
"required for H.264 streams\n");
|
|
|
|
return AVERROR_BSF_NOT_FOUND;
|
|
|
|
}
|
2017-04-26 05:36:17 +02:00
|
|
|
ret = av_bsf_alloc(filter, &bsf);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2014-04-14 14:49:12 +03:00
|
|
|
cs->bsf = bsf;
|
2016-04-10 21:58:15 +02:00
|
|
|
|
2017-04-26 05:36:17 +02:00
|
|
|
ret = avcodec_parameters_copy(bsf->par_in, st->codecpar);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2016-04-25 13:46:10 +02:00
|
|
|
|
2017-04-26 05:36:17 +02:00
|
|
|
ret = av_bsf_init(bsf);
|
|
|
|
if (ret < 0)
|
2016-04-10 21:58:15 +02:00
|
|
|
return ret;
|
|
|
|
|
2017-04-26 05:36:17 +02:00
|
|
|
ret = avcodec_parameters_copy(st->codecpar, bsf->par_out);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2014-04-14 14:49:12 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-14 12:57:28 +03:00
|
|
|
static int match_streams_one_to_one(AVFormatContext *avf)
|
2014-02-26 22:22:46 +03:00
|
|
|
{
|
|
|
|
ConcatContext *cat = avf->priv_data;
|
|
|
|
AVStream *st;
|
2014-04-14 12:57:28 +03:00
|
|
|
int i, ret;
|
2014-02-26 22:22:46 +03:00
|
|
|
|
2014-04-14 12:57:28 +03:00
|
|
|
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;
|
2014-02-26 22:22:46 +03:00
|
|
|
|
2014-04-14 11:34:53 +03:00
|
|
|
for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) {
|
2014-02-26 22:22:46 +03:00
|
|
|
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);
|
2014-04-14 12:57:28 +03:00
|
|
|
if ((ret = copy_stream_props(avf->streams[j], st)) < 0)
|
|
|
|
return ret;
|
|
|
|
cat->cur_file->streams[i].out_stream_index = j;
|
2014-02-26 22:22:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-14 12:57:28 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2014-02-26 22:22:46 +03:00
|
|
|
|
2014-04-14 12:57:28 +03:00
|
|
|
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;
|
2014-04-14 14:49:12 +03:00
|
|
|
memset(map + cat->cur_file->nb_streams, 0,
|
|
|
|
(cat->avf->nb_streams - cat->cur_file->nb_streams) * sizeof(*map));
|
2014-04-14 12:57:28 +03:00
|
|
|
|
2017-04-26 05:36:17 +02:00
|
|
|
for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) {
|
2014-04-14 12:57:28 +03:00
|
|
|
map[i].out_stream_index = -1;
|
2017-04-26 05:36:17 +02:00
|
|
|
if ((ret = detect_stream_specific(avf, i)) < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
2014-04-14 12:57:28 +03:00
|
|
|
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;
|
2014-04-14 11:34:53 +03:00
|
|
|
cat->cur_file->nb_streams = cat->avf->nb_streams;
|
2014-02-26 22:22:46 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-22 01:57:01 +02:00
|
|
|
static int64_t get_best_effort_duration(ConcatFile *file, AVFormatContext *avf)
|
|
|
|
{
|
|
|
|
if (file->user_duration != AV_NOPTS_VALUE)
|
|
|
|
return file->user_duration;
|
|
|
|
if (file->outpoint != AV_NOPTS_VALUE)
|
|
|
|
return file->outpoint - file->file_inpoint;
|
|
|
|
if (avf->duration > 0)
|
|
|
|
return avf->duration - (file->file_inpoint - file->file_start_time);
|
|
|
|
if (file->next_dts != AV_NOPTS_VALUE)
|
2018-11-22 01:59:04 +02:00
|
|
|
return file->next_dts - file->file_inpoint;
|
2018-11-22 01:57:01 +02:00
|
|
|
return AV_NOPTS_VALUE;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:28:37 +03:00
|
|
|
static int open_file(AVFormatContext *avf, unsigned fileno)
|
|
|
|
{
|
|
|
|
ConcatContext *cat = avf->priv_data;
|
|
|
|
ConcatFile *file = &cat->files[fileno];
|
2021-07-28 12:34:19 +02:00
|
|
|
AVDictionary *options = NULL;
|
2012-08-22 19:28:37 +03:00
|
|
|
int ret;
|
|
|
|
|
2013-02-14 18:00:06 +03:00
|
|
|
if (cat->avf)
|
|
|
|
avformat_close_input(&cat->avf);
|
2014-02-08 07:17:42 +03:00
|
|
|
|
|
|
|
cat->avf = avformat_alloc_context();
|
|
|
|
if (!cat->avf)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2017-05-22 16:55:28 +02:00
|
|
|
cat->avf->flags |= avf->flags & ~AVFMT_FLAG_CUSTOM_IO;
|
2014-02-08 07:17:42 +03:00
|
|
|
cat->avf->interrupt_callback = avf->interrupt_callback;
|
2014-10-23 17:13:05 +03:00
|
|
|
|
2016-03-03 19:14:26 +02:00
|
|
|
if ((ret = ff_copy_whiteblacklists(cat->avf, avf)) < 0)
|
2014-10-24 20:23:23 +03:00
|
|
|
return ret;
|
2014-10-23 17:13:05 +03:00
|
|
|
|
2021-07-28 12:34:19 +02:00
|
|
|
ret = av_dict_copy(&options, file->options, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if ((ret = avformat_open_input(&cat->avf, file->url, NULL, &options)) < 0 ||
|
2012-08-22 19:28:37 +03:00
|
|
|
(ret = avformat_find_stream_info(cat->avf, NULL)) < 0) {
|
|
|
|
av_log(avf, AV_LOG_ERROR, "Impossible to open '%s'\n", file->url);
|
2021-07-28 12:34:19 +02:00
|
|
|
av_dict_free(&options);
|
2014-02-08 07:17:42 +03:00
|
|
|
avformat_close_input(&cat->avf);
|
2012-08-22 19:28:37 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2021-07-28 12:34:19 +02:00
|
|
|
if (options) {
|
|
|
|
av_log(avf, AV_LOG_WARNING, "Unused options for '%s'.\n", file->url);
|
|
|
|
/* TODO log unused options once we have a proper string API */
|
|
|
|
av_dict_free(&options);
|
|
|
|
}
|
2012-08-22 19:28:37 +03:00
|
|
|
cat->cur_file = file;
|
2018-11-22 01:59:45 +02:00
|
|
|
file->start_time = !fileno ? 0 :
|
|
|
|
cat->files[fileno - 1].start_time +
|
|
|
|
cat->files[fileno - 1].duration;
|
2015-10-13 22:56:00 +02:00
|
|
|
file->file_start_time = (cat->avf->start_time == AV_NOPTS_VALUE) ? 0 : cat->avf->start_time;
|
2015-08-08 13:56:42 +02:00
|
|
|
file->file_inpoint = (file->inpoint == AV_NOPTS_VALUE) ? file->file_start_time : file->inpoint;
|
2018-11-22 01:59:45 +02:00
|
|
|
file->duration = get_best_effort_duration(file, cat->avf);
|
2015-10-24 17:16:11 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-02-26 22:22:46 +03:00
|
|
|
if ((ret = match_streams(avf)) < 0)
|
|
|
|
return ret;
|
2015-07-02 19:56:25 +02:00
|
|
|
if (file->inpoint != AV_NOPTS_VALUE) {
|
|
|
|
if ((ret = avformat_seek_file(cat->avf, -1, INT64_MIN, file->inpoint, file->inpoint, 0)) < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
2012-08-22 19:28:37 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int concat_read_close(AVFormatContext *avf)
|
|
|
|
{
|
|
|
|
ConcatContext *cat = avf->priv_data;
|
2016-04-10 21:58:15 +02:00
|
|
|
unsigned i, j;
|
2012-08-22 19:28:37 +03:00
|
|
|
|
2014-04-14 12:57:28 +03:00
|
|
|
for (i = 0; i < cat->nb_files; i++) {
|
2012-08-22 19:28:37 +03:00
|
|
|
av_freep(&cat->files[i].url);
|
2016-04-11 19:27:18 +02:00
|
|
|
for (j = 0; j < cat->files[i].nb_streams; j++) {
|
2016-04-10 21:58:15 +02:00
|
|
|
if (cat->files[i].streams[j].bsf)
|
2017-04-26 05:36:17 +02:00
|
|
|
av_bsf_free(&cat->files[i].streams[j].bsf);
|
2016-04-10 21:58:15 +02:00
|
|
|
}
|
2014-04-14 12:57:28 +03:00
|
|
|
av_freep(&cat->files[i].streams);
|
2015-07-02 23:20:09 +02:00
|
|
|
av_dict_free(&cat->files[i].metadata);
|
2021-07-28 12:34:19 +02:00
|
|
|
av_dict_free(&cat->files[i].options);
|
2014-04-14 12:57:28 +03:00
|
|
|
}
|
2016-04-10 21:58:15 +02:00
|
|
|
if (cat->avf)
|
|
|
|
avformat_close_input(&cat->avf);
|
2012-08-22 19:28:37 +03:00
|
|
|
av_freep(&cat->files);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-31 15:57:48 +02:00
|
|
|
#define MAX_ARGS 3
|
2021-08-31 13:30:01 +02:00
|
|
|
#define NEEDS_UNSAFE (1 << 1)
|
|
|
|
#define NEEDS_FILE (1 << 1)
|
|
|
|
#define NEEDS_STREAM (1 << 2)
|
|
|
|
|
|
|
|
typedef struct ParseSyntax {
|
|
|
|
const char *keyword;
|
|
|
|
char args[MAX_ARGS];
|
|
|
|
uint8_t flags;
|
|
|
|
} ParseSyntax;
|
|
|
|
|
|
|
|
typedef enum ParseDirective {
|
|
|
|
DIR_FFCONCAT,
|
|
|
|
DIR_FILE,
|
|
|
|
DIR_DURATION,
|
|
|
|
DIR_INPOINT,
|
|
|
|
DIR_OUTPOINT,
|
2021-08-31 19:56:24 +02:00
|
|
|
DIR_FPMETA,
|
2021-08-31 13:30:01 +02:00
|
|
|
DIR_FPMETAS,
|
|
|
|
DIR_OPTION,
|
|
|
|
DIR_STREAM,
|
|
|
|
DIR_EXSID,
|
2021-08-31 14:09:57 +02:00
|
|
|
DIR_STMETA,
|
2021-08-31 14:30:28 +02:00
|
|
|
DIR_STCODEC,
|
2021-08-31 19:36:35 +02:00
|
|
|
DIR_STEDATA,
|
2021-08-31 15:57:48 +02:00
|
|
|
DIR_CHAPTER,
|
2021-08-31 13:30:01 +02:00
|
|
|
} ParseDirective;
|
|
|
|
|
|
|
|
static const ParseSyntax syntax[] = {
|
|
|
|
[DIR_FFCONCAT ] = { "ffconcat", "kk", 0 },
|
|
|
|
[DIR_FILE ] = { "file", "s", 0 },
|
|
|
|
[DIR_DURATION ] = { "duration", "d", NEEDS_FILE },
|
|
|
|
[DIR_INPOINT ] = { "inpoint", "d", NEEDS_FILE },
|
|
|
|
[DIR_OUTPOINT ] = { "outpoint", "d", NEEDS_FILE },
|
2021-08-31 19:56:24 +02:00
|
|
|
[DIR_FPMETA ] = { "file_packet_meta", "ks", NEEDS_FILE },
|
2021-08-31 13:30:01 +02:00
|
|
|
[DIR_FPMETAS ] = { "file_packet_metadata", "s", NEEDS_FILE },
|
|
|
|
[DIR_OPTION ] = { "option", "ks", NEEDS_FILE | NEEDS_UNSAFE },
|
|
|
|
[DIR_STREAM ] = { "stream", "", 0 },
|
|
|
|
[DIR_EXSID ] = { "exact_stream_id", "i", NEEDS_STREAM },
|
2021-08-31 14:09:57 +02:00
|
|
|
[DIR_STMETA ] = { "stream_meta", "ks", NEEDS_STREAM },
|
2021-08-31 14:30:28 +02:00
|
|
|
[DIR_STCODEC ] = { "stream_codec", "k", NEEDS_STREAM },
|
2021-08-31 19:36:35 +02:00
|
|
|
[DIR_STEDATA ] = { "stream_extradata", "k", NEEDS_STREAM },
|
2021-08-31 15:57:48 +02:00
|
|
|
[DIR_CHAPTER ] = { "chapter", "idd", 0 },
|
2021-08-31 13:30:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static int concat_parse_script(AVFormatContext *avf)
|
2012-08-22 19:28:37 +03:00
|
|
|
{
|
|
|
|
ConcatContext *cat = avf->priv_data;
|
2021-08-31 13:30:01 +02:00
|
|
|
unsigned nb_files_alloc = 0;
|
2018-02-03 23:05:21 +02:00
|
|
|
AVBPrint bp;
|
2012-08-22 19:28:37 +03:00
|
|
|
uint8_t *cursor, *keyword;
|
|
|
|
ConcatFile *file = NULL;
|
2021-08-31 14:09:57 +02:00
|
|
|
AVStream *stream = NULL;
|
2021-08-31 15:57:48 +02:00
|
|
|
AVChapter *chapter = NULL;
|
2021-08-31 13:30:01 +02:00
|
|
|
unsigned line = 0, arg;
|
|
|
|
const ParseSyntax *dir;
|
|
|
|
char *arg_kw[MAX_ARGS];
|
|
|
|
char *arg_str[MAX_ARGS] = { 0 };
|
|
|
|
int64_t arg_int[MAX_ARGS];
|
|
|
|
int ret;
|
2012-08-22 19:28:37 +03:00
|
|
|
|
2018-02-03 23:05:21 +02:00
|
|
|
av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
|
|
|
|
|
|
|
|
while ((ret = ff_read_line_to_bprint_overwrite(avf->pb, &bp)) >= 0) {
|
2012-08-22 19:28:37 +03:00
|
|
|
line++;
|
2018-02-03 23:05:21 +02:00
|
|
|
cursor = bp.str;
|
2012-08-22 19:28:37 +03:00
|
|
|
keyword = get_keyword(&cursor);
|
|
|
|
if (!*keyword || *keyword == '#')
|
|
|
|
continue;
|
2021-08-31 13:30:01 +02:00
|
|
|
for (dir = syntax; dir < syntax + FF_ARRAY_ELEMS(syntax); dir++)
|
|
|
|
if (!strcmp(dir->keyword, keyword))
|
|
|
|
break;
|
|
|
|
if (dir >= syntax + FF_ARRAY_ELEMS(syntax)) {
|
|
|
|
av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
|
|
|
|
line, keyword);
|
|
|
|
FAIL(AVERROR_INVALIDDATA);
|
|
|
|
}
|
2012-08-22 19:28:37 +03:00
|
|
|
|
2021-08-31 13:30:01 +02:00
|
|
|
/* Flags check */
|
|
|
|
if ((dir->flags & NEEDS_UNSAFE) && cat->safe) {
|
|
|
|
av_log(avf, AV_LOG_ERROR, "Line %d: %s not allowed if safe\n", line, keyword);
|
|
|
|
FAIL(AVERROR_INVALIDDATA);
|
|
|
|
}
|
|
|
|
if ((dir->flags & NEEDS_FILE) && !cat->nb_files) {
|
|
|
|
av_log(avf, AV_LOG_ERROR, "Line %d: %s without file\n", line, keyword);
|
|
|
|
FAIL(AVERROR_INVALIDDATA);
|
|
|
|
}
|
|
|
|
if ((dir->flags & NEEDS_STREAM) && !avf->nb_streams) {
|
|
|
|
av_log(avf, AV_LOG_ERROR, "Line %d: %s without stream\n", line, keyword);
|
|
|
|
FAIL(AVERROR_INVALIDDATA);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Arguments parsing */
|
|
|
|
for (arg = 0; arg < FF_ARRAY_ELEMS(dir->args) && dir->args[arg]; arg++) {
|
|
|
|
switch (dir->args[arg]) {
|
|
|
|
case 'd': /* duration */
|
|
|
|
arg_kw[arg] = get_keyword(&cursor);
|
|
|
|
ret = av_parse_time(&arg_int[arg], arg_kw[arg], 1);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avf, AV_LOG_ERROR, "Line %d: invalid duration '%s'\n",
|
|
|
|
line, arg_kw[arg]);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'i': /* integer */
|
|
|
|
arg_int[arg] = strtol(get_keyword(&cursor), NULL, 0);
|
|
|
|
break;
|
|
|
|
case 'k': /* keyword */
|
|
|
|
arg_kw[arg] = get_keyword(&cursor);
|
|
|
|
break;
|
|
|
|
case 's': /* string */
|
|
|
|
av_assert0(!arg_str[arg]);
|
|
|
|
arg_str[arg] = av_get_token((const char **)&cursor, SPACE_CHARS);
|
|
|
|
if (!arg_str[arg])
|
|
|
|
FAIL(AVERROR(ENOMEM));
|
|
|
|
if (!*arg_str[arg]) {
|
|
|
|
av_log(avf, AV_LOG_ERROR, "Line %d: string required\n", line);
|
|
|
|
FAIL(AVERROR_INVALIDDATA);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FAIL(AVERROR_BUG);
|
2012-08-22 19:28:37 +03:00
|
|
|
}
|
2021-08-31 13:30:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Directive action */
|
|
|
|
switch ((ParseDirective)(dir - syntax)) {
|
|
|
|
|
|
|
|
case DIR_FFCONCAT:
|
|
|
|
if (strcmp(arg_kw[0], "version") || strcmp(arg_kw[1], "1.0")) {
|
|
|
|
av_log(avf, AV_LOG_ERROR, "Line %d: invalid version\n", line);
|
2013-02-13 19:25:56 +03:00
|
|
|
FAIL(AVERROR_INVALIDDATA);
|
|
|
|
}
|
2021-08-31 13:30:01 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DIR_FILE:
|
|
|
|
ret = add_file(avf, arg_str[0], &file, &nb_files_alloc);
|
|
|
|
arg_str[0] = NULL;
|
|
|
|
if (ret < 0)
|
2014-09-10 02:22:57 +03:00
|
|
|
goto fail;
|
2021-08-31 13:30:01 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DIR_DURATION:
|
|
|
|
file->user_duration = arg_int[0];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DIR_INPOINT:
|
|
|
|
file->inpoint = arg_int[0];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DIR_OUTPOINT:
|
|
|
|
file->outpoint = arg_int[0];
|
|
|
|
break;
|
|
|
|
|
2021-08-31 19:56:24 +02:00
|
|
|
case DIR_FPMETA:
|
|
|
|
ret = av_dict_set(&file->metadata, arg_kw[0], arg_str[1], AV_DICT_DONT_STRDUP_VAL);
|
|
|
|
arg_str[1] = NULL;
|
|
|
|
if (ret < 0)
|
|
|
|
FAIL(ret);
|
|
|
|
break;
|
|
|
|
|
2021-08-31 13:30:01 +02:00
|
|
|
case DIR_FPMETAS:
|
|
|
|
if ((ret = av_dict_parse_string(&file->metadata, arg_str[0], "=", "", 0)) < 0) {
|
2015-07-02 23:20:09 +02:00
|
|
|
av_log(avf, AV_LOG_ERROR, "Line %d: failed to parse metadata string\n", line);
|
|
|
|
FAIL(AVERROR_INVALIDDATA);
|
|
|
|
}
|
2021-08-31 13:48:31 +02:00
|
|
|
av_log(avf, AV_LOG_WARNING,
|
|
|
|
"'file_packet_metadata key=value:key=value' is deprecated, "
|
|
|
|
"use multiple 'file_packet_meta key value' instead\n");
|
2021-08-31 13:30:01 +02:00
|
|
|
av_freep(&arg_str[0]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DIR_OPTION:
|
|
|
|
ret = av_dict_set(&file->options, arg_kw[0], arg_str[1], AV_DICT_DONT_STRDUP_VAL);
|
|
|
|
arg_str[1] = NULL;
|
2021-07-28 12:34:19 +02:00
|
|
|
if (ret < 0)
|
|
|
|
FAIL(ret);
|
2021-08-31 13:30:01 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DIR_STREAM:
|
2021-08-31 14:09:57 +02:00
|
|
|
stream = avformat_new_stream(avf, NULL);
|
|
|
|
if (!stream)
|
2014-02-26 22:22:46 +03:00
|
|
|
FAIL(AVERROR(ENOMEM));
|
2021-08-31 13:30:01 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DIR_EXSID:
|
2021-08-31 14:09:57 +02:00
|
|
|
stream->id = arg_int[0];
|
|
|
|
break;
|
|
|
|
case DIR_STMETA:
|
|
|
|
ret = av_dict_set(&stream->metadata, arg_kw[0], arg_str[1], AV_DICT_DONT_STRDUP_VAL);
|
|
|
|
arg_str[1] = NULL;
|
|
|
|
if (ret < 0)
|
|
|
|
FAIL(ret);
|
2021-08-31 13:30:01 +02:00
|
|
|
break;
|
|
|
|
|
2021-08-31 14:30:28 +02:00
|
|
|
case DIR_STCODEC: {
|
|
|
|
const AVCodecDescriptor *codec = avcodec_descriptor_get_by_name(arg_kw[0]);
|
|
|
|
if (!codec) {
|
|
|
|
av_log(avf, AV_LOG_ERROR, "Line %d: codec '%s' not found\n", line, arg_kw[0]);
|
|
|
|
FAIL(AVERROR_DECODER_NOT_FOUND);
|
|
|
|
}
|
|
|
|
stream->codecpar->codec_type = codec->type;
|
|
|
|
stream->codecpar->codec_id = codec->id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-08-31 19:36:35 +02:00
|
|
|
case DIR_STEDATA: {
|
|
|
|
int size = ff_hex_to_data(NULL, arg_kw[0]);
|
|
|
|
ret = ff_alloc_extradata(stream->codecpar, size);
|
|
|
|
if (ret < 0)
|
|
|
|
FAIL(ret);
|
|
|
|
ff_hex_to_data(stream->codecpar->extradata, arg_kw[0]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-08-31 15:57:48 +02:00
|
|
|
case DIR_CHAPTER:
|
|
|
|
chapter = avpriv_new_chapter(avf, arg_int[0], AV_TIME_BASE_Q,
|
|
|
|
arg_int[1], arg_int[2], NULL);
|
|
|
|
if (!chapter)
|
2021-09-22 00:01:00 +02:00
|
|
|
FAIL(ENOMEM);
|
2021-08-31 15:57:48 +02:00
|
|
|
break;
|
|
|
|
|
2021-08-31 13:30:01 +02:00
|
|
|
default:
|
|
|
|
FAIL(AVERROR_BUG);
|
2012-08-22 19:28:37 +03:00
|
|
|
}
|
|
|
|
}
|
2021-08-31 13:30:01 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
for (arg = 0; arg < MAX_ARGS; arg++)
|
|
|
|
av_freep(&arg_str[arg]);
|
|
|
|
av_bprint_finalize(&bp, NULL);
|
|
|
|
return ret == AVERROR_EOF ? 0 : ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int concat_read_header(AVFormatContext *avf)
|
|
|
|
{
|
|
|
|
ConcatContext *cat = avf->priv_data;
|
|
|
|
int64_t time = 0;
|
|
|
|
unsigned i;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = concat_parse_script(avf);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if (!cat->nb_files) {
|
|
|
|
av_log(avf, AV_LOG_ERROR, "No files to concat\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2012-08-22 19:28:37 +03:00
|
|
|
|
2013-02-13 21:25:26 +03:00
|
|
|
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;
|
2018-11-22 01:57:01 +02:00
|
|
|
if (cat->files[i].user_duration == AV_NOPTS_VALUE) {
|
2015-07-02 20:35:04 +02:00
|
|
|
if (cat->files[i].inpoint == AV_NOPTS_VALUE || cat->files[i].outpoint == AV_NOPTS_VALUE)
|
|
|
|
break;
|
2018-11-22 01:57:01 +02:00
|
|
|
cat->files[i].user_duration = cat->files[i].outpoint - cat->files[i].inpoint;
|
2015-07-02 20:35:04 +02:00
|
|
|
}
|
2018-11-22 01:59:45 +02:00
|
|
|
cat->files[i].duration = cat->files[i].user_duration;
|
2018-11-22 01:57:01 +02:00
|
|
|
time += cat->files[i].user_duration;
|
2013-02-13 21:25:26 +03:00
|
|
|
}
|
2013-02-14 18:00:06 +03:00
|
|
|
if (i == cat->nb_files) {
|
2013-02-13 21:25:26 +03:00
|
|
|
avf->duration = time;
|
2013-02-14 18:00:06 +03:00
|
|
|
cat->seekable = 1;
|
|
|
|
}
|
2013-02-13 21:25:26 +03:00
|
|
|
|
2014-04-14 12:57:28 +03:00
|
|
|
cat->stream_match_mode = avf->nb_streams ? MATCH_EXACT_ID :
|
|
|
|
MATCH_ONE_TO_ONE;
|
2012-08-22 19:28:37 +03:00
|
|
|
if ((ret = open_file(avf, 0)) < 0)
|
2021-08-31 13:30:01 +02:00
|
|
|
return ret;
|
2012-08-22 19:28:37 +03:00
|
|
|
|
2021-08-31 13:30:01 +02:00
|
|
|
return 0;
|
2012-08-22 19:28:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int open_next_file(AVFormatContext *avf)
|
|
|
|
{
|
|
|
|
ConcatContext *cat = avf->priv_data;
|
|
|
|
unsigned fileno = cat->cur_file - cat->files;
|
|
|
|
|
2018-11-22 01:59:45 +02:00
|
|
|
cat->cur_file->duration = get_best_effort_duration(cat->cur_file, cat->avf);
|
2012-08-22 19:28:37 +03:00
|
|
|
|
2015-07-04 23:34:17 +02:00
|
|
|
if (++fileno >= cat->nb_files) {
|
|
|
|
cat->eof = 1;
|
2012-08-22 19:28:37 +03:00
|
|
|
return AVERROR_EOF;
|
2015-07-04 23:34:17 +02:00
|
|
|
}
|
2012-08-22 19:28:37 +03:00
|
|
|
return open_file(avf, fileno);
|
|
|
|
}
|
|
|
|
|
2014-04-14 14:49:12 +03:00
|
|
|
static int filter_packet(AVFormatContext *avf, ConcatStream *cs, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2017-04-26 05:36:17 +02:00
|
|
|
if (cs->bsf) {
|
|
|
|
ret = av_bsf_send_packet(cs->bsf, pkt);
|
2014-04-14 14:49:12 +03:00
|
|
|
if (ret < 0) {
|
2017-04-26 05:36:17 +02:00
|
|
|
av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb filter "
|
|
|
|
"failed to send input packet\n");
|
2014-04-14 14:49:12 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2016-04-10 21:58:15 +02:00
|
|
|
|
2017-04-26 05:36:17 +02:00
|
|
|
while (!ret)
|
|
|
|
ret = av_bsf_receive_packet(cs->bsf, pkt);
|
2016-04-10 21:58:15 +02:00
|
|
|
|
2017-04-26 05:36:17 +02:00
|
|
|
if (ret < 0 && (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)) {
|
|
|
|
av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb filter "
|
|
|
|
"failed to receive output packet\n");
|
|
|
|
return ret;
|
2014-04-14 14:49:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-02 20:35:04 +02:00
|
|
|
/* Returns true if the packet dts is greater or equal to the specified outpoint. */
|
|
|
|
static int packet_after_outpoint(ConcatContext *cat, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
if (cat->cur_file->outpoint != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE) {
|
|
|
|
return av_compare_ts(pkt->dts, cat->avf->streams[pkt->stream_index]->time_base,
|
|
|
|
cat->cur_file->outpoint, AV_TIME_BASE_Q) >= 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:28:37 +03:00
|
|
|
static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
ConcatContext *cat = avf->priv_data;
|
|
|
|
int ret;
|
2015-07-02 19:56:25 +02:00
|
|
|
int64_t delta;
|
2014-04-14 11:34:53 +03:00
|
|
|
ConcatStream *cs;
|
2014-11-18 11:36:16 +02:00
|
|
|
AVStream *st;
|
2021-08-24 19:41:16 +02:00
|
|
|
FFStream *sti;
|
2012-08-22 19:28:37 +03:00
|
|
|
|
2015-07-04 23:34:17 +02:00
|
|
|
if (cat->eof)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
2015-01-29 11:55:32 +02:00
|
|
|
if (!cat->avf)
|
|
|
|
return AVERROR(EIO);
|
|
|
|
|
2012-08-22 19:28:37 +03:00
|
|
|
while (1) {
|
2014-02-26 22:22:46 +03:00
|
|
|
ret = av_read_frame(cat->avf, pkt);
|
2015-11-28 14:05:30 +02:00
|
|
|
if (ret == AVERROR_EOF) {
|
2014-02-26 22:22:46 +03:00
|
|
|
if ((ret = open_next_file(avf)) < 0)
|
|
|
|
return ret;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2014-04-14 12:58:17 +03:00
|
|
|
if ((ret = match_streams(avf)) < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2015-11-28 14:05:30 +02:00
|
|
|
if (packet_after_outpoint(cat, pkt)) {
|
|
|
|
av_packet_unref(pkt);
|
|
|
|
if ((ret = open_next_file(avf)) < 0)
|
|
|
|
return ret;
|
|
|
|
continue;
|
|
|
|
}
|
2014-04-14 12:58:17 +03:00
|
|
|
cs = &cat->cur_file->streams[pkt->stream_index];
|
|
|
|
if (cs->out_stream_index < 0) {
|
|
|
|
av_packet_unref(pkt);
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-26 22:22:46 +03:00
|
|
|
break;
|
2012-08-22 19:28:37 +03:00
|
|
|
}
|
2020-01-07 15:55:40 +02:00
|
|
|
if ((ret = filter_packet(avf, cs, pkt)) < 0)
|
2014-04-14 14:49:12 +03:00
|
|
|
return ret;
|
2013-12-18 16:56:36 +03:00
|
|
|
|
2014-11-18 11:36:16 +02:00
|
|
|
st = cat->avf->streams[pkt->stream_index];
|
2021-08-24 19:41:16 +02:00
|
|
|
sti = ffstream(st);
|
2014-11-18 11:36:16 +02:00
|
|
|
av_log(avf, AV_LOG_DEBUG, "file:%d stream:%d pts:%s pts_time:%s dts:%s dts_time:%s",
|
|
|
|
(unsigned)(cat->cur_file - cat->files), pkt->stream_index,
|
|
|
|
av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
|
|
|
|
av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
|
|
|
|
|
2015-07-02 19:56:25 +02:00
|
|
|
delta = av_rescale_q(cat->cur_file->start_time - cat->cur_file->file_inpoint,
|
2012-08-22 19:28:37 +03:00
|
|
|
AV_TIME_BASE_Q,
|
|
|
|
cat->avf->streams[pkt->stream_index]->time_base);
|
|
|
|
if (pkt->pts != AV_NOPTS_VALUE)
|
|
|
|
pkt->pts += delta;
|
|
|
|
if (pkt->dts != AV_NOPTS_VALUE)
|
|
|
|
pkt->dts += delta;
|
2014-11-18 11:36:16 +02:00
|
|
|
av_log(avf, AV_LOG_DEBUG, " -> pts:%s pts_time:%s dts:%s dts_time:%s\n",
|
|
|
|
av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
|
|
|
|
av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
|
2015-07-02 23:20:09 +02:00
|
|
|
if (cat->cur_file->metadata) {
|
2021-04-14 14:59:32 +02:00
|
|
|
size_t metadata_len;
|
2015-07-02 23:20:09 +02:00
|
|
|
char* packed_metadata = av_packet_pack_dictionary(cat->cur_file->metadata, &metadata_len);
|
|
|
|
if (!packed_metadata)
|
|
|
|
return AVERROR(ENOMEM);
|
2020-05-23 12:57:29 +02:00
|
|
|
ret = av_packet_add_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA,
|
|
|
|
packed_metadata, metadata_len);
|
|
|
|
if (ret < 0) {
|
2015-07-02 23:20:09 +02:00
|
|
|
av_freep(&packed_metadata);
|
2020-05-23 12:57:29 +02:00
|
|
|
return ret;
|
2015-07-02 23:20:09 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-29 04:45:20 +02:00
|
|
|
|
2021-08-24 19:41:16 +02:00
|
|
|
if (cat->cur_file->duration == AV_NOPTS_VALUE && sti->cur_dts != AV_NOPTS_VALUE) {
|
|
|
|
int64_t next_dts = av_rescale_q(sti->cur_dts, st->time_base, AV_TIME_BASE_Q);
|
2017-08-29 04:45:20 +02:00
|
|
|
if (cat->cur_file->next_dts == AV_NOPTS_VALUE || next_dts > cat->cur_file->next_dts) {
|
|
|
|
cat->cur_file->next_dts = next_dts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-25 22:19:28 +02:00
|
|
|
pkt->stream_index = cs->out_stream_index;
|
2020-05-23 12:57:29 +02:00
|
|
|
return 0;
|
2012-08-22 19:28:37 +03:00
|
|
|
}
|
|
|
|
|
2013-02-14 18:00:06 +03:00
|
|
|
static void rescale_interval(AVRational tb_in, AVRational tb_out,
|
|
|
|
int64_t *min_ts, int64_t *ts, int64_t *max_ts)
|
|
|
|
{
|
|
|
|
*ts = av_rescale_q (* ts, tb_in, tb_out);
|
|
|
|
*min_ts = av_rescale_q_rnd(*min_ts, tb_in, tb_out,
|
|
|
|
AV_ROUND_UP | AV_ROUND_PASS_MINMAX);
|
|
|
|
*max_ts = av_rescale_q_rnd(*max_ts, tb_in, tb_out,
|
|
|
|
AV_ROUND_DOWN | AV_ROUND_PASS_MINMAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int try_seek(AVFormatContext *avf, int stream,
|
|
|
|
int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
|
|
|
|
{
|
|
|
|
ConcatContext *cat = avf->priv_data;
|
2015-07-02 19:56:25 +02:00
|
|
|
int64_t t0 = cat->cur_file->start_time - cat->cur_file->file_inpoint;
|
2013-02-14 18:00:06 +03:00
|
|
|
|
|
|
|
ts -= t0;
|
|
|
|
min_ts = min_ts == INT64_MIN ? INT64_MIN : min_ts - t0;
|
|
|
|
max_ts = max_ts == INT64_MAX ? INT64_MAX : max_ts - t0;
|
|
|
|
if (stream >= 0) {
|
|
|
|
if (stream >= cat->avf->nb_streams)
|
|
|
|
return AVERROR(EIO);
|
|
|
|
rescale_interval(AV_TIME_BASE_Q, cat->avf->streams[stream]->time_base,
|
|
|
|
&min_ts, &ts, &max_ts);
|
|
|
|
}
|
|
|
|
return avformat_seek_file(cat->avf, stream, min_ts, ts, max_ts, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int real_seek(AVFormatContext *avf, int stream,
|
2016-09-23 05:48:40 +02:00
|
|
|
int64_t min_ts, int64_t ts, int64_t max_ts, int flags, AVFormatContext *cur_avf)
|
2013-02-14 18:00:06 +03:00
|
|
|
{
|
|
|
|
ConcatContext *cat = avf->priv_data;
|
|
|
|
int ret, left, right;
|
|
|
|
|
|
|
|
if (stream >= 0) {
|
|
|
|
if (stream >= avf->nb_streams)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
rescale_interval(avf->streams[stream]->time_base, AV_TIME_BASE_Q,
|
|
|
|
&min_ts, &ts, &max_ts);
|
|
|
|
}
|
|
|
|
|
|
|
|
left = 0;
|
|
|
|
right = cat->nb_files;
|
2018-12-13 23:13:09 +02:00
|
|
|
|
|
|
|
/* Always support seek to start */
|
|
|
|
if (ts <= 0)
|
|
|
|
right = 1;
|
|
|
|
else if (!cat->seekable)
|
|
|
|
return AVERROR(ESPIPE); /* XXX: can we use it? */
|
|
|
|
|
2013-02-14 18:00:06 +03:00
|
|
|
while (right - left > 1) {
|
|
|
|
int mid = (left + right) / 2;
|
|
|
|
if (ts < cat->files[mid].start_time)
|
|
|
|
right = mid;
|
|
|
|
else
|
|
|
|
left = mid;
|
|
|
|
}
|
|
|
|
|
2016-09-23 05:48:40 +02:00
|
|
|
if (cat->cur_file != &cat->files[left]) {
|
|
|
|
if ((ret = open_file(avf, left)) < 0)
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
cat->avf = cur_avf;
|
|
|
|
}
|
2013-02-14 18:00:06 +03:00
|
|
|
|
|
|
|
ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
|
2013-05-03 11:32:45 +03:00
|
|
|
if (ret < 0 &&
|
2013-02-14 18:00:06 +03:00
|
|
|
left < cat->nb_files - 1 &&
|
|
|
|
cat->files[left + 1].start_time < max_ts) {
|
2016-09-23 05:48:40 +02:00
|
|
|
if (cat->cur_file == &cat->files[left])
|
|
|
|
cat->avf = NULL;
|
2013-02-14 18:00:06 +03:00
|
|
|
if ((ret = open_file(avf, left + 1)) < 0)
|
|
|
|
return ret;
|
|
|
|
ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int concat_seek(AVFormatContext *avf, int stream,
|
|
|
|
int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
|
|
|
|
{
|
|
|
|
ConcatContext *cat = avf->priv_data;
|
|
|
|
ConcatFile *cur_file_saved = cat->cur_file;
|
|
|
|
AVFormatContext *cur_avf_saved = cat->avf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME))
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
cat->avf = NULL;
|
2016-09-23 05:48:40 +02:00
|
|
|
if ((ret = real_seek(avf, stream, min_ts, ts, max_ts, flags, cur_avf_saved)) < 0) {
|
|
|
|
if (cat->cur_file != cur_file_saved) {
|
|
|
|
if (cat->avf)
|
|
|
|
avformat_close_input(&cat->avf);
|
|
|
|
}
|
2013-02-14 18:00:06 +03:00
|
|
|
cat->avf = cur_avf_saved;
|
|
|
|
cat->cur_file = cur_file_saved;
|
|
|
|
} else {
|
2016-09-23 05:48:40 +02:00
|
|
|
if (cat->cur_file != cur_file_saved) {
|
|
|
|
avformat_close_input(&cur_avf_saved);
|
|
|
|
}
|
2015-07-04 23:34:17 +02:00
|
|
|
cat->eof = 0;
|
2013-02-14 18:00:06 +03:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-02-03 12:16:33 +03:00
|
|
|
#define OFFSET(x) offsetof(ConcatContext, x)
|
|
|
|
#define DEC AV_OPT_FLAG_DECODING_PARAM
|
|
|
|
|
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "safe", "enable safe mode",
|
2021-07-28 12:38:08 +02:00
|
|
|
OFFSET(safe), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, DEC },
|
2014-04-14 14:49:12 +03:00
|
|
|
{ "auto_convert", "automatically convert bitstream format",
|
2015-11-21 23:05:07 +02:00
|
|
|
OFFSET(auto_convert), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, DEC },
|
2015-10-24 17:16:11 +02:00
|
|
|
{ "segment_time_metadata", "output file segment start time and duration as packet metadata",
|
|
|
|
OFFSET(segment_time_metadata), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DEC },
|
2013-02-03 12:16:33 +03:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass concat_class = {
|
|
|
|
.class_name = "concat demuxer",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-04-19 19:45:24 +02:00
|
|
|
const AVInputFormat ff_concat_demuxer = {
|
2012-08-22 19:28:37 +03:00
|
|
|
.name = "concat",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Virtual concatenation script"),
|
|
|
|
.priv_data_size = sizeof(ConcatContext),
|
2020-07-21 01:09:48 +02:00
|
|
|
.flags_internal = FF_FMT_INIT_CLEANUP,
|
2012-08-22 19:28:37 +03:00
|
|
|
.read_probe = concat_probe,
|
|
|
|
.read_header = concat_read_header,
|
|
|
|
.read_packet = concat_read_packet,
|
|
|
|
.read_close = concat_read_close,
|
2013-02-14 18:00:06 +03:00
|
|
|
.read_seek2 = concat_seek,
|
2013-02-03 12:16:33 +03:00
|
|
|
.priv_class = &concat_class,
|
2012-08-22 19:28:37 +03:00
|
|
|
};
|