2012-08-22 18:28:37 +02: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
|
|
|
|
*/
|
|
|
|
|
2014-04-14 13:49:12 +02:00
|
|
|
#include "libavutil/avassert.h"
|
2012-08-22 18:28:37 +02:00
|
|
|
#include "libavutil/avstring.h"
|
2014-04-14 13:49:12 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2013-02-03 10:16:33 +01:00
|
|
|
#include "libavutil/opt.h"
|
2013-02-13 17:25:56 +01:00
|
|
|
#include "libavutil/parseutils.h"
|
2014-11-18 10:36:16 +01:00
|
|
|
#include "libavutil/timestamp.h"
|
2012-08-22 18:28:37 +02:00
|
|
|
#include "avformat.h"
|
|
|
|
#include "internal.h"
|
2013-06-17 00:05:02 +02:00
|
|
|
#include "url.h"
|
2012-08-22 18:28:37 +02:00
|
|
|
|
2014-04-14 11:57:28 +02:00
|
|
|
typedef enum ConcatMatchMode {
|
|
|
|
MATCH_ONE_TO_ONE,
|
|
|
|
MATCH_EXACT_ID,
|
|
|
|
} ConcatMatchMode;
|
|
|
|
|
2014-04-14 10:34:53 +02:00
|
|
|
typedef struct ConcatStream {
|
2014-04-14 13:49:12 +02:00
|
|
|
AVBitStreamFilterContext *bsf;
|
2014-04-14 10:34:53 +02:00
|
|
|
int out_stream_index;
|
|
|
|
} ConcatStream;
|
|
|
|
|
2012-08-22 18:28:37 +02:00
|
|
|
typedef struct {
|
|
|
|
char *url;
|
|
|
|
int64_t start_time;
|
|
|
|
int64_t duration;
|
2014-04-14 10:34:53 +02:00
|
|
|
ConcatStream *streams;
|
|
|
|
int nb_streams;
|
2012-08-22 18:28:37 +02:00
|
|
|
} ConcatFile;
|
|
|
|
|
|
|
|
typedef struct {
|
2013-02-03 10:16:33 +01:00
|
|
|
AVClass *class;
|
2012-08-22 18:28:37 +02:00
|
|
|
ConcatFile *files;
|
|
|
|
ConcatFile *cur_file;
|
|
|
|
unsigned nb_files;
|
|
|
|
AVFormatContext *avf;
|
2013-02-03 10:16:33 +01:00
|
|
|
int safe;
|
2013-02-14 16:00:06 +01:00
|
|
|
int seekable;
|
2014-04-14 11:57:28 +02:00
|
|
|
ConcatMatchMode stream_match_mode;
|
2014-04-14 13:49:12 +02:00
|
|
|
unsigned auto_convert;
|
2012-08-22 18:28:37 +02:00
|
|
|
} ConcatContext;
|
|
|
|
|
|
|
|
static int concat_probe(AVProbeData *probe)
|
|
|
|
{
|
2013-02-03 10:27:11 +01:00
|
|
|
return memcmp(probe->buf, "ffconcat version 1.0", 20) ?
|
|
|
|
0 : AVPROBE_SCORE_MAX;
|
2012-08-22 18:28:37 +02: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 10:16:33 +01: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 18:28:37 +02: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 10:18:12 +01:00
|
|
|
char *url = NULL;
|
2014-03-01 12:13:11 +01:00
|
|
|
const char *proto;
|
|
|
|
size_t url_len, proto_len;
|
2013-03-20 10:18:12 +01:00
|
|
|
int ret;
|
2012-08-22 18:28:37 +02:00
|
|
|
|
2013-02-03 10:16:33 +01:00
|
|
|
if (cat->safe > 0 && !safe_filename(filename)) {
|
|
|
|
av_log(avf, AV_LOG_ERROR, "Unsafe file name '%s'\n", filename);
|
2013-03-20 10:18:12 +01:00
|
|
|
FAIL(AVERROR(EPERM));
|
2013-02-03 10:16:33 +01:00
|
|
|
}
|
2014-03-01 12:13:11 +01:00
|
|
|
|
|
|
|
proto = avio_find_protocol_name(filename);
|
|
|
|
proto_len = proto ? strlen(proto) : 0;
|
|
|
|
if (!memcmp(filename, proto, proto_len) &&
|
|
|
|
(filename[proto_len] == ':' || filename[proto_len] == ',')) {
|
|
|
|
url = filename;
|
|
|
|
filename = NULL;
|
|
|
|
} else {
|
2014-03-08 12:44:45 +01:00
|
|
|
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);
|
2014-03-01 12:13:11 +01:00
|
|
|
}
|
2012-08-22 18:28:37 +02:00
|
|
|
|
|
|
|
if (cat->nb_files >= *nb_files_alloc) {
|
2012-12-17 14:20:29 +01:00
|
|
|
size_t n = FFMAX(*nb_files_alloc * 2, 16);
|
2012-12-12 12:15:57 +01: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 10:18:12 +01:00
|
|
|
FAIL(AVERROR(ENOMEM));
|
2012-12-12 12:15:57 +01:00
|
|
|
cat->files = new_files;
|
2012-08-22 18:28:37 +02: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;
|
|
|
|
|
|
|
|
return 0;
|
2013-03-20 10:18:12 +01:00
|
|
|
|
|
|
|
fail:
|
|
|
|
av_free(url);
|
|
|
|
av_free(filename);
|
|
|
|
return ret;
|
2012-08-22 18:28:37 +02:00
|
|
|
}
|
|
|
|
|
2014-02-26 20:22:46 +01:00
|
|
|
static int copy_stream_props(AVStream *st, AVStream *source_st)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2014-04-14 13:49:12 +02:00
|
|
|
if (st->codec->codec_id || !source_st->codec->codec_id) {
|
|
|
|
if (st->codec->extradata_size < source_st->codec->extradata_size) {
|
|
|
|
ret = ff_alloc_extradata(st->codec,
|
|
|
|
source_st->codec->extradata_size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
memcpy(st->codec->extradata, source_st->codec->extradata,
|
|
|
|
source_st->codec->extradata_size);
|
2014-04-14 11:57:28 +02:00
|
|
|
return 0;
|
2014-04-14 13:49:12 +02:00
|
|
|
}
|
2014-02-26 20:22:46 +01:00
|
|
|
if ((ret = avcodec_copy_context(st->codec, source_st->codec)) < 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;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-14 13:49:12 +02: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];
|
|
|
|
AVBitStreamFilterContext *bsf;
|
|
|
|
|
|
|
|
if (cat->auto_convert && st->codec->codec_id == AV_CODEC_ID_H264 &&
|
|
|
|
(st->codec->extradata_size < 4 || AV_RB32(st->codec->extradata) != 1)) {
|
|
|
|
av_log(cat->avf, AV_LOG_INFO,
|
|
|
|
"Auto-inserting h264_mp4toannexb bitstream filter\n");
|
|
|
|
if (!(bsf = av_bitstream_filter_init("h264_mp4toannexb"))) {
|
|
|
|
av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb bitstream filter "
|
|
|
|
"required for H.264 streams\n");
|
|
|
|
return AVERROR_BSF_NOT_FOUND;
|
|
|
|
}
|
|
|
|
cs->bsf = bsf;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-14 11:57:28 +02:00
|
|
|
static int match_streams_one_to_one(AVFormatContext *avf)
|
2014-02-26 20:22:46 +01:00
|
|
|
{
|
|
|
|
ConcatContext *cat = avf->priv_data;
|
|
|
|
AVStream *st;
|
2014-04-14 11:57:28 +02:00
|
|
|
int i, ret;
|
2014-02-26 20:22:46 +01:00
|
|
|
|
2014-04-14 11:57:28 +02: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 20:22:46 +01:00
|
|
|
|
2014-04-14 10:34:53 +02:00
|
|
|
for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) {
|
2014-02-26 20:22:46 +01: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 11:57:28 +02: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 20:22:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-14 11:57:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2014-02-26 20:22:46 +01:00
|
|
|
|
2014-04-14 11:57:28 +02: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 13:49:12 +02:00
|
|
|
memset(map + cat->cur_file->nb_streams, 0,
|
|
|
|
(cat->avf->nb_streams - cat->cur_file->nb_streams) * sizeof(*map));
|
2014-04-14 11:57:28 +02:00
|
|
|
|
|
|
|
for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++)
|
|
|
|
map[i].out_stream_index = -1;
|
|
|
|
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 13:49:12 +02:00
|
|
|
for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++)
|
|
|
|
if ((ret = detect_stream_specific(avf, i)) < 0)
|
|
|
|
return ret;
|
2014-04-14 10:34:53 +02:00
|
|
|
cat->cur_file->nb_streams = cat->avf->nb_streams;
|
2014-02-26 20:22:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 18:28:37 +02:00
|
|
|
static int open_file(AVFormatContext *avf, unsigned fileno)
|
|
|
|
{
|
|
|
|
ConcatContext *cat = avf->priv_data;
|
|
|
|
ConcatFile *file = &cat->files[fileno];
|
|
|
|
int ret;
|
|
|
|
|
2013-02-14 16:00:06 +01:00
|
|
|
if (cat->avf)
|
|
|
|
avformat_close_input(&cat->avf);
|
2014-02-08 05:17:42 +01:00
|
|
|
|
|
|
|
cat->avf = avformat_alloc_context();
|
|
|
|
if (!cat->avf)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
cat->avf->interrupt_callback = avf->interrupt_callback;
|
2014-10-23 16:13:05 +02:00
|
|
|
|
2014-10-24 19:23:23 +02:00
|
|
|
if ((ret = ff_copy_whitelists(cat->avf, avf)) < 0)
|
|
|
|
return ret;
|
2014-10-23 16:13:05 +02:00
|
|
|
|
2012-08-22 18:28:37 +02:00
|
|
|
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);
|
2014-02-08 05:17:42 +01:00
|
|
|
avformat_close_input(&cat->avf);
|
2012-08-22 18:28:37 +02:00
|
|
|
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;
|
2014-02-26 20:22:46 +01:00
|
|
|
if ((ret = match_streams(avf)) < 0)
|
|
|
|
return ret;
|
2012-08-22 18:28:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int concat_read_close(AVFormatContext *avf)
|
|
|
|
{
|
|
|
|
ConcatContext *cat = avf->priv_data;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
if (cat->avf)
|
|
|
|
avformat_close_input(&cat->avf);
|
2014-04-14 11:57:28 +02:00
|
|
|
for (i = 0; i < cat->nb_files; i++) {
|
2012-08-22 18:28:37 +02:00
|
|
|
av_freep(&cat->files[i].url);
|
2014-04-14 11:57:28 +02:00
|
|
|
av_freep(&cat->files[i].streams);
|
|
|
|
}
|
2012-08-22 18:28:37 +02:00
|
|
|
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;
|
2013-02-13 19:25:26 +01:00
|
|
|
int64_t time = 0;
|
2012-08-22 18:28:37 +02:00
|
|
|
|
|
|
|
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)
|
2014-09-10 01:22:57 +02:00
|
|
|
goto fail;
|
2013-02-13 17:25:56 +01:00
|
|
|
} else if (!strcmp(keyword, "duration")) {
|
|
|
|
char *dur_str = get_keyword(&cursor);
|
|
|
|
int64_t dur;
|
|
|
|
if (!file) {
|
|
|
|
av_log(avf, AV_LOG_ERROR, "Line %d: duration without file\n",
|
|
|
|
line);
|
|
|
|
FAIL(AVERROR_INVALIDDATA);
|
|
|
|
}
|
|
|
|
if ((ret = av_parse_time(&dur, dur_str, 1)) < 0) {
|
|
|
|
av_log(avf, AV_LOG_ERROR, "Line %d: invalid duration '%s'\n",
|
|
|
|
line, dur_str);
|
2014-09-10 01:22:57 +02:00
|
|
|
goto fail;
|
2013-02-13 17:25:56 +01:00
|
|
|
}
|
|
|
|
file->duration = dur;
|
2014-02-26 20:22:46 +01:00
|
|
|
} 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);
|
2013-02-03 10:22:57 +01:00
|
|
|
} 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;
|
2012-08-22 18:28:37 +02:00
|
|
|
} else {
|
|
|
|
av_log(avf, AV_LOG_ERROR, "Line %d: unknown keyword '%s'\n",
|
|
|
|
line, keyword);
|
|
|
|
FAIL(AVERROR_INVALIDDATA);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret < 0)
|
2014-09-10 01:22:57 +02:00
|
|
|
goto fail;
|
2013-05-15 16:53:35 +02:00
|
|
|
if (!cat->nb_files)
|
|
|
|
FAIL(AVERROR_INVALIDDATA);
|
2012-08-22 18:28:37 +02:00
|
|
|
|
2013-02-13 19:25:26 +01: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;
|
|
|
|
if (cat->files[i].duration == AV_NOPTS_VALUE)
|
|
|
|
break;
|
|
|
|
time += cat->files[i].duration;
|
|
|
|
}
|
2013-02-14 16:00:06 +01:00
|
|
|
if (i == cat->nb_files) {
|
2013-02-13 19:25:26 +01:00
|
|
|
avf->duration = time;
|
2013-02-14 16:00:06 +01:00
|
|
|
cat->seekable = 1;
|
|
|
|
}
|
2013-02-13 19:25:26 +01:00
|
|
|
|
2014-04-14 11:57:28 +02:00
|
|
|
cat->stream_match_mode = avf->nb_streams ? MATCH_EXACT_ID :
|
|
|
|
MATCH_ONE_TO_ONE;
|
2012-08-22 18:28:37 +02:00
|
|
|
if ((ret = open_file(avf, 0)) < 0)
|
2014-09-10 01:22:57 +02:00
|
|
|
goto fail;
|
2012-08-22 18:28:37 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
concat_read_close(avf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int open_next_file(AVFormatContext *avf)
|
|
|
|
{
|
|
|
|
ConcatContext *cat = avf->priv_data;
|
|
|
|
unsigned fileno = cat->cur_file - cat->files;
|
|
|
|
|
|
|
|
if (cat->cur_file->duration == AV_NOPTS_VALUE)
|
|
|
|
cat->cur_file->duration = cat->avf->duration;
|
|
|
|
|
|
|
|
if (++fileno >= cat->nb_files)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
return open_file(avf, fileno);
|
|
|
|
}
|
|
|
|
|
2014-04-14 13:49:12 +02:00
|
|
|
static int filter_packet(AVFormatContext *avf, ConcatStream *cs, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
AVStream *st = avf->streams[cs->out_stream_index];
|
|
|
|
AVBitStreamFilterContext *bsf;
|
|
|
|
AVPacket pkt2;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
av_assert0(cs->out_stream_index >= 0);
|
|
|
|
for (bsf = cs->bsf; bsf; bsf = bsf->next) {
|
|
|
|
pkt2 = *pkt;
|
|
|
|
ret = av_bitstream_filter_filter(bsf, st->codec, NULL,
|
|
|
|
&pkt2.data, &pkt2.size,
|
|
|
|
pkt->data, pkt->size,
|
|
|
|
!!(pkt->flags & AV_PKT_FLAG_KEY));
|
|
|
|
if (ret < 0) {
|
|
|
|
av_packet_unref(pkt);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
av_assert0(pkt2.buf);
|
|
|
|
if (ret == 0 && pkt2.data != pkt->data) {
|
|
|
|
if ((ret = av_copy_packet(&pkt2, pkt)) < 0) {
|
|
|
|
av_free(pkt2.data);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
if (ret > 0) {
|
|
|
|
av_free_packet(pkt);
|
|
|
|
pkt2.buf = av_buffer_create(pkt2.data, pkt2.size,
|
|
|
|
av_buffer_default_free, NULL, 0);
|
|
|
|
if (!pkt2.buf) {
|
|
|
|
av_free(pkt2.data);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*pkt = pkt2;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 18:28:37 +02:00
|
|
|
static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
ConcatContext *cat = avf->priv_data;
|
|
|
|
int ret;
|
2014-12-15 18:42:41 +01:00
|
|
|
int64_t file_start_time, delta;
|
2014-04-14 10:34:53 +02:00
|
|
|
ConcatStream *cs;
|
2014-11-18 10:36:16 +01:00
|
|
|
AVStream *st;
|
2012-08-22 18:28:37 +02:00
|
|
|
|
2015-01-29 10:55:32 +01:00
|
|
|
if (!cat->avf)
|
|
|
|
return AVERROR(EIO);
|
|
|
|
|
2012-08-22 18:28:37 +02:00
|
|
|
while (1) {
|
2014-02-26 20:22:46 +01:00
|
|
|
ret = av_read_frame(cat->avf, pkt);
|
|
|
|
if (ret == AVERROR_EOF) {
|
|
|
|
if ((ret = open_next_file(avf)) < 0)
|
|
|
|
return ret;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2014-04-14 11:58:17 +02:00
|
|
|
if ((ret = match_streams(avf)) < 0) {
|
|
|
|
av_packet_unref(pkt);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
cs = &cat->cur_file->streams[pkt->stream_index];
|
|
|
|
if (cs->out_stream_index < 0) {
|
|
|
|
av_packet_unref(pkt);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pkt->stream_index = cs->out_stream_index;
|
2014-02-26 20:22:46 +01:00
|
|
|
break;
|
2012-08-22 18:28:37 +02:00
|
|
|
}
|
2014-04-14 13:49:12 +02:00
|
|
|
if ((ret = filter_packet(avf, cs, pkt)))
|
|
|
|
return ret;
|
2013-12-18 14:56:36 +01:00
|
|
|
|
2014-11-18 10:36:16 +01:00
|
|
|
st = cat->avf->streams[pkt->stream_index];
|
|
|
|
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));
|
|
|
|
|
2014-12-15 18:42:41 +01:00
|
|
|
file_start_time = cat->avf->start_time;
|
|
|
|
if (file_start_time == AV_NOPTS_VALUE)
|
|
|
|
file_start_time = 0;
|
|
|
|
delta = av_rescale_q(cat->cur_file->start_time - file_start_time,
|
2012-08-22 18:28:37 +02: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 10:36:16 +01: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));
|
2012-08-22 18:28:37 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-02-14 16:00:06 +01: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;
|
|
|
|
int64_t t0 = cat->cur_file->start_time - cat->avf->start_time;
|
|
|
|
|
|
|
|
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,
|
|
|
|
int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
while (right - left > 1) {
|
|
|
|
int mid = (left + right) / 2;
|
|
|
|
if (ts < cat->files[mid].start_time)
|
|
|
|
right = mid;
|
|
|
|
else
|
|
|
|
left = mid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = open_file(avf, left)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
|
2013-05-03 10:32:45 +02:00
|
|
|
if (ret < 0 &&
|
2013-02-14 16:00:06 +01:00
|
|
|
left < cat->nb_files - 1 &&
|
|
|
|
cat->files[left + 1].start_time < max_ts) {
|
|
|
|
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 (!cat->seekable)
|
|
|
|
return AVERROR(ESPIPE); /* XXX: can we use it? */
|
|
|
|
if (flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME))
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
cat->avf = NULL;
|
|
|
|
if ((ret = real_seek(avf, stream, min_ts, ts, max_ts, flags)) < 0) {
|
|
|
|
if (cat->avf)
|
|
|
|
avformat_close_input(&cat->avf);
|
|
|
|
cat->avf = cur_avf_saved;
|
|
|
|
cat->cur_file = cur_file_saved;
|
|
|
|
} else {
|
|
|
|
avformat_close_input(&cur_avf_saved);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-02-03 10:16:33 +01:00
|
|
|
#define OFFSET(x) offsetof(ConcatContext, x)
|
|
|
|
#define DEC AV_OPT_FLAG_DECODING_PARAM
|
|
|
|
|
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "safe", "enable safe mode",
|
|
|
|
OFFSET(safe), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, DEC },
|
2014-04-14 13:49:12 +02:00
|
|
|
{ "auto_convert", "automatically convert bitstream format",
|
2015-05-26 22:16:43 +02:00
|
|
|
OFFSET(auto_convert), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, DEC },
|
2013-02-03 10:16:33 +01:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass concat_class = {
|
|
|
|
.class_name = "concat demuxer",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-08-22 18:28:37 +02:00
|
|
|
AVInputFormat ff_concat_demuxer = {
|
|
|
|
.name = "concat",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Virtual concatenation script"),
|
|
|
|
.priv_data_size = sizeof(ConcatContext),
|
|
|
|
.read_probe = concat_probe,
|
|
|
|
.read_header = concat_read_header,
|
|
|
|
.read_packet = concat_read_packet,
|
|
|
|
.read_close = concat_read_close,
|
2013-02-14 16:00:06 +01:00
|
|
|
.read_seek2 = concat_seek,
|
2013-02-03 10:16:33 +01:00
|
|
|
.priv_class = &concat_class,
|
2012-08-22 18:28:37 +02:00
|
|
|
};
|