mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2025-04-19 09:02:26 +02:00
avformat/concatdec: port to the new bitstream filter API
Reviewed-by: wm4 <nfxjfg@googlemail.com> Reviewed-by: Aaron Levinson <alevinsn@aracnet.com> Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
parent
dec2fa8cc7
commit
14e092448f
@ -34,8 +34,7 @@ typedef enum ConcatMatchMode {
|
|||||||
} ConcatMatchMode;
|
} ConcatMatchMode;
|
||||||
|
|
||||||
typedef struct ConcatStream {
|
typedef struct ConcatStream {
|
||||||
AVBitStreamFilterContext *bsf;
|
AVBSFContext *bsf;
|
||||||
AVCodecContext *avctx;
|
|
||||||
int out_stream_index;
|
int out_stream_index;
|
||||||
} ConcatStream;
|
} ConcatStream;
|
||||||
|
|
||||||
@ -196,7 +195,8 @@ static int detect_stream_specific(AVFormatContext *avf, int idx)
|
|||||||
ConcatContext *cat = avf->priv_data;
|
ConcatContext *cat = avf->priv_data;
|
||||||
AVStream *st = cat->avf->streams[idx];
|
AVStream *st = cat->avf->streams[idx];
|
||||||
ConcatStream *cs = &cat->cur_file->streams[idx];
|
ConcatStream *cs = &cat->cur_file->streams[idx];
|
||||||
AVBitStreamFilterContext *bsf;
|
const AVBitStreamFilter *filter;
|
||||||
|
AVBSFContext *bsf;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if (cat->auto_convert && st->codecpar->codec_id == AV_CODEC_ID_H264) {
|
if (cat->auto_convert && st->codecpar->codec_id == AV_CODEC_ID_H264) {
|
||||||
@ -206,29 +206,28 @@ static int detect_stream_specific(AVFormatContext *avf, int idx)
|
|||||||
return 0;
|
return 0;
|
||||||
av_log(cat->avf, AV_LOG_INFO,
|
av_log(cat->avf, AV_LOG_INFO,
|
||||||
"Auto-inserting h264_mp4toannexb bitstream filter\n");
|
"Auto-inserting h264_mp4toannexb bitstream filter\n");
|
||||||
if (!(bsf = av_bitstream_filter_init("h264_mp4toannexb"))) {
|
filter = av_bsf_get_by_name("h264_mp4toannexb");
|
||||||
|
if (!filter) {
|
||||||
av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb bitstream filter "
|
av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb bitstream filter "
|
||||||
"required for H.264 streams\n");
|
"required for H.264 streams\n");
|
||||||
return AVERROR_BSF_NOT_FOUND;
|
return AVERROR_BSF_NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
ret = av_bsf_alloc(filter, &bsf);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
cs->bsf = bsf;
|
cs->bsf = bsf;
|
||||||
|
|
||||||
cs->avctx = avcodec_alloc_context3(NULL);
|
ret = avcodec_parameters_copy(bsf->par_in, st->codecpar);
|
||||||
if (!cs->avctx)
|
if (ret < 0)
|
||||||
return AVERROR(ENOMEM);
|
|
||||||
|
|
||||||
/* This really should be part of the bsf work.
|
|
||||||
Note: input bitstream filtering will not work with bsf that
|
|
||||||
create extradata from the first packet. */
|
|
||||||
av_freep(&st->codecpar->extradata);
|
|
||||||
st->codecpar->extradata_size = 0;
|
|
||||||
|
|
||||||
ret = avcodec_parameters_to_context(cs->avctx, st->codecpar);
|
|
||||||
if (ret < 0) {
|
|
||||||
avcodec_free_context(&cs->avctx);
|
|
||||||
return ret;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -291,8 +290,11 @@ static int match_streams(AVFormatContext *avf)
|
|||||||
memset(map + cat->cur_file->nb_streams, 0,
|
memset(map + cat->cur_file->nb_streams, 0,
|
||||||
(cat->avf->nb_streams - cat->cur_file->nb_streams) * sizeof(*map));
|
(cat->avf->nb_streams - cat->cur_file->nb_streams) * sizeof(*map));
|
||||||
|
|
||||||
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++) {
|
||||||
map[i].out_stream_index = -1;
|
map[i].out_stream_index = -1;
|
||||||
|
if ((ret = detect_stream_specific(avf, i)) < 0)
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
switch (cat->stream_match_mode) {
|
switch (cat->stream_match_mode) {
|
||||||
case MATCH_ONE_TO_ONE:
|
case MATCH_ONE_TO_ONE:
|
||||||
ret = match_streams_one_to_one(avf);
|
ret = match_streams_one_to_one(avf);
|
||||||
@ -305,9 +307,6 @@ static int match_streams(AVFormatContext *avf)
|
|||||||
}
|
}
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
for (i = cat->cur_file->nb_streams; i < cat->avf->nb_streams; i++)
|
|
||||||
if ((ret = detect_stream_specific(avf, i)) < 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;
|
||||||
}
|
}
|
||||||
@ -370,10 +369,8 @@ static int concat_read_close(AVFormatContext *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);
|
||||||
for (j = 0; j < cat->files[i].nb_streams; j++) {
|
for (j = 0; j < cat->files[i].nb_streams; j++) {
|
||||||
if (cat->files[i].streams[j].avctx)
|
|
||||||
avcodec_free_context(&cat->files[i].streams[j].avctx);
|
|
||||||
if (cat->files[i].streams[j].bsf)
|
if (cat->files[i].streams[j].bsf)
|
||||||
av_bitstream_filter_close(cat->files[i].streams[j].bsf);
|
av_bsf_free(&cat->files[i].streams[j].bsf);
|
||||||
}
|
}
|
||||||
av_freep(&cat->files[i].streams);
|
av_freep(&cat->files[i].streams);
|
||||||
av_dict_free(&cat->files[i].metadata);
|
av_dict_free(&cat->files[i].metadata);
|
||||||
@ -524,56 +521,25 @@ static int open_next_file(AVFormatContext *avf)
|
|||||||
|
|
||||||
static int filter_packet(AVFormatContext *avf, ConcatStream *cs, AVPacket *pkt)
|
static int filter_packet(AVFormatContext *avf, ConcatStream *cs, AVPacket *pkt)
|
||||||
{
|
{
|
||||||
AVStream *st = avf->streams[cs->out_stream_index];
|
|
||||||
AVBitStreamFilterContext *bsf;
|
|
||||||
AVPacket pkt2;
|
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
av_assert0(cs->out_stream_index >= 0);
|
if (cs->bsf) {
|
||||||
for (bsf = cs->bsf; bsf; bsf = bsf->next) {
|
ret = av_bsf_send_packet(cs->bsf, pkt);
|
||||||
pkt2 = *pkt;
|
|
||||||
|
|
||||||
ret = av_bitstream_filter_filter(bsf, cs->avctx, NULL,
|
|
||||||
&pkt2.data, &pkt2.size,
|
|
||||||
pkt->data, pkt->size,
|
|
||||||
!!(pkt->flags & AV_PKT_FLAG_KEY));
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
|
av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb filter "
|
||||||
|
"failed to send input packet\n");
|
||||||
av_packet_unref(pkt);
|
av_packet_unref(pkt);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cs->avctx->extradata_size > st->codecpar->extradata_size) {
|
while (!ret)
|
||||||
int eret;
|
ret = av_bsf_receive_packet(cs->bsf, pkt);
|
||||||
if (st->codecpar->extradata)
|
|
||||||
av_freep(&st->codecpar->extradata);
|
|
||||||
|
|
||||||
eret = ff_alloc_extradata(st->codecpar, cs->avctx->extradata_size);
|
if (ret < 0 && (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)) {
|
||||||
if (eret < 0) {
|
av_log(avf, AV_LOG_ERROR, "h264_mp4toannexb filter "
|
||||||
av_packet_unref(pkt);
|
"failed to receive output packet\n");
|
||||||
return AVERROR(ENOMEM);
|
|
||||||
}
|
|
||||||
st->codecpar->extradata_size = cs->avctx->extradata_size;
|
|
||||||
memcpy(st->codecpar->extradata, cs->avctx->extradata, cs->avctx->extradata_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
return ret;
|
||||||
}
|
}
|
||||||
ret = 1;
|
|
||||||
}
|
|
||||||
if (ret > 0) {
|
|
||||||
av_packet_unref(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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user