diff options
author | Marton Balint <cus@passwd.hu> | 2019-12-26 19:57:04 +0100 |
---|---|---|
committer | Marton Balint <cus@passwd.hu> | 2020-01-03 11:23:55 +0100 |
commit | b693b06b22079bf3490525088c9ceba55f66d22a (patch) | |
tree | 199e44c5c7594737f3d09a8560f4a83c99d3648c /libavformat/img2enc.c | |
parent | bc17b831ddced87110bdb68c67f2ca6d3d638f35 (diff) | |
download | ffmpeg-b693b06b22079bf3490525088c9ceba55f66d22a.tar.gz |
avformat/img2enc: factorize writing fully muxed file
Signed-off-by: Marton Balint <cus@passwd.hu>
Diffstat (limited to 'libavformat/img2enc.c')
-rw-r--r-- | libavformat/img2enc.c | 65 |
1 files changed, 37 insertions, 28 deletions
diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c index bec4bf81dd..0ce8ef5bff 100644 --- a/libavformat/img2enc.c +++ b/libavformat/img2enc.c @@ -78,6 +78,41 @@ static int write_header(AVFormatContext *s) return 0; } +static int write_muxed_file(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt) +{ + VideoMuxData *img = s->priv_data; + AVCodecParameters *par = s->streams[0]->codecpar; + AVStream *st; + AVPacket pkt2 = {0}; + AVFormatContext *fmt = NULL; + int ret; + + /* URL is not used directly as we are overriding the IO context later. */ + ret = avformat_alloc_output_context2(&fmt, NULL, img->muxer, s->url); + if (ret < 0) + return ret; + st = avformat_new_stream(fmt, NULL); + if (!st) { + avformat_free_context(fmt); + return AVERROR(ENOMEM); + } + st->id = pkt->stream_index; + + fmt->pb = pb; + if ((ret = av_packet_ref(&pkt2, pkt)) < 0 || + (ret = avcodec_parameters_copy(st->codecpar, par)) < 0 || + (ret = avformat_write_header(fmt, NULL)) < 0 || + (ret = av_interleaved_write_frame(fmt, &pkt2)) < 0 || + (ret = av_write_trailer(fmt)) < 0) { + av_packet_unref(&pkt2); + avformat_free_context(fmt); + return ret; + } + av_packet_unref(&pkt2); + avformat_free_context(fmt); + return 0; +} + static int write_packet(AVFormatContext *s, AVPacket *pkt) { VideoMuxData *img = s->priv_data; @@ -85,7 +120,7 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) char filename[1024]; AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar; const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(par->format); - int i; + int ret, i; int nb_renames = 0; if (!img->is_pipe) { @@ -150,35 +185,9 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) ff_format_io_close(s, &pb[3]); } } else if (img->muxer) { - int ret; - AVStream *st; - AVPacket pkt2 = {0}; - AVFormatContext *fmt = NULL; - - av_assert0(!img->split_planes); - - ret = avformat_alloc_output_context2(&fmt, NULL, img->muxer, s->url); + ret = write_muxed_file(s, pb[0], pkt); if (ret < 0) return ret; - st = avformat_new_stream(fmt, NULL); - if (!st) { - avformat_free_context(fmt); - return AVERROR(ENOMEM); - } - st->id = pkt->stream_index; - - fmt->pb = pb[0]; - if ((ret = av_packet_ref(&pkt2, pkt)) < 0 || - (ret = avcodec_parameters_copy(st->codecpar, s->streams[0]->codecpar)) < 0 || - (ret = avformat_write_header(fmt, NULL)) < 0 || - (ret = av_interleaved_write_frame(fmt, &pkt2)) < 0 || - (ret = av_write_trailer(fmt)) < 0) { - av_packet_unref(&pkt2); - avformat_free_context(fmt); - return ret; - } - av_packet_unref(&pkt2); - avformat_free_context(fmt); } else { avio_write(pb[0], pkt->data, pkt->size); } |