Commit b24d6c53 authored by Nicolas George's avatar Nicolas George

lavf/concatdec: always do stream matching.

parent c27939d8
...@@ -25,6 +25,11 @@ ...@@ -25,6 +25,11 @@
#include "internal.h" #include "internal.h"
#include "url.h" #include "url.h"
typedef enum ConcatMatchMode {
MATCH_ONE_TO_ONE,
MATCH_EXACT_ID,
} ConcatMatchMode;
typedef struct ConcatStream { typedef struct ConcatStream {
int out_stream_index; int out_stream_index;
} ConcatStream; } ConcatStream;
...@@ -45,7 +50,7 @@ typedef struct { ...@@ -45,7 +50,7 @@ typedef struct {
AVFormatContext *avf; AVFormatContext *avf;
int safe; int safe;
int seekable; int seekable;
int match_streams; ConcatMatchMode stream_match_mode;
} ConcatContext; } ConcatContext;
static int concat_probe(AVProbeData *probe) static int concat_probe(AVProbeData *probe)
...@@ -145,6 +150,8 @@ static int copy_stream_props(AVStream *st, AVStream *source_st) ...@@ -145,6 +150,8 @@ static int copy_stream_props(AVStream *st, AVStream *source_st)
{ {
int ret; int ret;
if (st->codec->codec_id || !source_st->codec->codec_id)
return 0;
if ((ret = avcodec_copy_context(st->codec, source_st->codec)) < 0) if ((ret = avcodec_copy_context(st->codec, source_st->codec)) < 0)
return ret; return ret;
st->r_frame_rate = source_st->r_frame_rate; st->r_frame_rate = source_st->r_frame_rate;
...@@ -154,38 +161,76 @@ static int copy_stream_props(AVStream *st, AVStream *source_st) ...@@ -154,38 +161,76 @@ static int copy_stream_props(AVStream *st, AVStream *source_st)
return 0; return 0;
} }
static int match_streams(AVFormatContext *avf) static int match_streams_one_to_one(AVFormatContext *avf)
{ {
ConcatContext *cat = avf->priv_data; ConcatContext *cat = avf->priv_data;
AVStream *st; AVStream *st;
ConcatStream *map; int i, ret;
int i, j, ret;
if (!cat->match_streams || for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) {
cat->cur_file->nb_streams >= cat->avf->nb_streams) if (i < avf->nb_streams) {
return 0; st = avf->streams[i];
map = av_realloc(cat->cur_file->streams, } else {
cat->avf->nb_streams * sizeof(*map)); if (!(st = avformat_new_stream(avf, NULL)))
if (!map) return AVERROR(ENOMEM);
return AVERROR(ENOMEM); }
cat->cur_file->streams = map; 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++) { for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++) {
st = cat->avf->streams[i]; st = cat->avf->streams[i];
map[i].out_stream_index = -1;
for (j = 0; j < avf->nb_streams; j++) { for (j = 0; j < avf->nb_streams; j++) {
if (avf->streams[j]->id == st->id) { if (avf->streams[j]->id == st->id) {
av_log(avf, AV_LOG_VERBOSE, av_log(avf, AV_LOG_VERBOSE,
"Match slave stream #%d with stream #%d id 0x%x\n", "Match slave stream #%d with stream #%d id 0x%x\n",
i, j, st->id); i, j, st->id);
map[i].out_stream_index = j; if ((ret = copy_stream_props(avf->streams[j], st)) < 0)
if (!avf->streams[j]->codec->codec_id && st->codec->codec_id) return ret;
if ((ret = copy_stream_props(avf->streams[j], st)) < 0) cat->cur_file->streams[i].out_stream_index = j;
return ret;
} }
} }
} }
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;
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;
cat->cur_file->nb_streams = cat->avf->nb_streams; cat->cur_file->nb_streams = cat->avf->nb_streams;
return 0; return 0;
} }
...@@ -227,8 +272,10 @@ static int concat_read_close(AVFormatContext *avf) ...@@ -227,8 +272,10 @@ static int concat_read_close(AVFormatContext *avf)
if (cat->avf) if (cat->avf)
avformat_close_input(&cat->avf); avformat_close_input(&cat->avf);
for (i = 0; i < cat->nb_files; i++) for (i = 0; i < cat->nb_files; i++) {
av_freep(&cat->files[i].url); av_freep(&cat->files[i].url);
av_freep(&cat->files[i].streams);
}
av_freep(&cat->files); av_freep(&cat->files);
return 0; return 0;
} }
...@@ -241,7 +288,6 @@ static int concat_read_header(AVFormatContext *avf) ...@@ -241,7 +288,6 @@ static int concat_read_header(AVFormatContext *avf)
int ret, line = 0, i; int ret, line = 0, i;
unsigned nb_files_alloc = 0; unsigned nb_files_alloc = 0;
ConcatFile *file = NULL; ConcatFile *file = NULL;
AVStream *st;
int64_t time = 0; int64_t time = 0;
while (1) { while (1) {
...@@ -320,18 +366,10 @@ static int concat_read_header(AVFormatContext *avf) ...@@ -320,18 +366,10 @@ static int concat_read_header(AVFormatContext *avf)
cat->seekable = 1; cat->seekable = 1;
} }
cat->match_streams = !!avf->nb_streams; cat->stream_match_mode = avf->nb_streams ? MATCH_EXACT_ID :
MATCH_ONE_TO_ONE;
if ((ret = open_file(avf, 0)) < 0) if ((ret = open_file(avf, 0)) < 0)
FAIL(ret); FAIL(ret);
if (!cat->match_streams) {
for (i = 0; i < cat->avf->nb_streams; i++) {
if (!(st = avformat_new_stream(avf, NULL)))
FAIL(AVERROR(ENOMEM));
if ((ret = copy_stream_props(st, cat->avf->streams[i])) < 0)
FAIL(ret);
}
}
return 0; return 0;
fail: fail:
...@@ -368,7 +406,7 @@ static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt) ...@@ -368,7 +406,7 @@ static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
} }
if (ret < 0) if (ret < 0)
return ret; return ret;
if (cat->match_streams) { /* TODO reindent */
if ((ret = match_streams(avf)) < 0) { if ((ret = match_streams(avf)) < 0) {
av_packet_unref(pkt); av_packet_unref(pkt);
return ret; return ret;
...@@ -379,7 +417,6 @@ static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt) ...@@ -379,7 +417,6 @@ static int concat_read_packet(AVFormatContext *avf, AVPacket *pkt)
continue; continue;
} }
pkt->stream_index = cs->out_stream_index; pkt->stream_index = cs->out_stream_index;
}
break; break;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment