[FFmpeg-devel] [PATCH 4/8] avformat/mux: factorize writing a packet
Marton Balint
cus at passwd.hu
Sat Mar 28 20:15:11 EET 2020
In preparation for N:M bsf support.
Signed-off-by: Marton Balint <cus at passwd.hu>
---
libavformat/mux.c | 82 +++++++++++++++++++++++++++++++------------------------
1 file changed, 47 insertions(+), 35 deletions(-)
diff --git a/libavformat/mux.c b/libavformat/mux.c
index f68f962a92..8c2d6a8060 100644
--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -883,6 +883,49 @@ static int do_packet_auto_bsf(AVFormatContext *s, AVPacket *pkt) {
return 1;
}
+static int interleaved_write_packet(AVFormatContext *s, AVPacket *pkt, int flush);
+
+static int write_packet_common(AVFormatContext *s, AVStream *st, AVPacket *pkt, int interleaved)
+{
+ int ret;
+
+ if (s->debug & FF_FDEBUG_TS)
+ av_log(s, AV_LOG_DEBUG, "%s size:%d dts:%s pts:%s\n", __FUNCTION__,
+ pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
+
+#if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX
+ if ((ret = compute_muxer_pkt_fields(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
+ return ret;
+#endif
+
+ if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
+ return AVERROR(EINVAL);
+
+ if (interleaved)
+ return interleaved_write_packet(s, pkt, 0);
+ else
+ return write_packet(s, pkt);
+}
+
+static int write_packets_common(AVFormatContext *s, AVStream *st, AVPacket *pkt, int interleaved)
+{
+ int ret = do_packet_auto_bsf(s, pkt);
+ if (ret == 0)
+ return 0;
+ else if (ret < 0)
+ goto fail;
+
+ ret = write_packet_common(s, st, pkt, interleaved);
+ if (ret < 0)
+ goto fail;
+ return ret;
+
+fail:
+ if (interleaved)
+ av_packet_unref(pkt);
+ return ret;
+}
+
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
{
int ret;
@@ -902,18 +945,7 @@ int av_write_frame(AVFormatContext *s, AVPacket *pkt)
return 1;
}
- ret = do_packet_auto_bsf(s, pkt);
- if (ret <= 0)
- return ret;
-
-#if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX
- ret = compute_muxer_pkt_fields(s, s->streams[pkt->stream_index], pkt);
-
- if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
- return ret;
-#endif
-
- return write_packet(s, pkt);
+ return write_packets_common(s, s->streams[pkt->stream_index], pkt, 0/*non-interleaved*/);
}
#define CHUNK_START 0x1000
@@ -1211,7 +1243,7 @@ static int interleaved_write_packet(AVFormatContext *s, AVPacket *pkt, int flush
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
{
- int ret, flush = 0;
+ int ret;
ret = prepare_input_packet(s, pkt);
if (ret < 0)
@@ -1219,31 +1251,11 @@ int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
if (pkt) {
AVStream *st = s->streams[pkt->stream_index];
-
- ret = do_packet_auto_bsf(s, pkt);
- if (ret == 0)
- return 0;
- else if (ret < 0)
- goto fail;
-
- if (s->debug & FF_FDEBUG_TS)
- av_log(s, AV_LOG_DEBUG, "av_interleaved_write_frame size:%d dts:%s pts:%s\n",
- pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
-
-#if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX
- if ((ret = compute_muxer_pkt_fields(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
- goto fail;
-#endif
-
- if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
- ret = AVERROR(EINVAL);
- goto fail;
- }
+ return write_packets_common(s, st, pkt, 1/*interleaved*/);
} else {
av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n");
- flush = 1;
+ return interleaved_write_packet(s, NULL, 1/*flush*/);
}
- return interleaved_write_packet(s, pkt, flush);
fail:
av_packet_unref(pkt);
--
2.16.4
More information about the ffmpeg-devel
mailing list