[FFmpeg-devel] [PATCH 01/10] avformat/mux: Only prepare input packet if there is a packet

Marton Balint cus at passwd.hu
Tue Mar 31 22:33:26 EEST 2020



On Tue, 31 Mar 2020, Andreas Rheinhardt wrote:

> It is unnecessary to call prepare_input_packet if there is no packet as
> it doesn't do anything, except when the currently inactive code guarded
> by !FF_API_COMPUTE_PKT_FIELDS2 || !FF_API_LAVF_AVCTX becomes active:
> Then attempting to access pkt->stream_index will crash.
>
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
> ---
> This works very well together with Marton's patch #4 [1]:
> write_packets_common(): The prepare_input_packet() call can be moved
> into write_packets_common(). Unfortunately, it's not compatible with #5
> [2] as-is, but I think I have a solution for this.
>
> [1]: https://ffmpeg.org/pipermail/ffmpeg-devel/2020-March/259203.html
> [2]: https://ffmpeg.org/pipermail/ffmpeg-devel/2020-March/259204.html
>
> libavformat/mux.c | 22 ++++++++++------------
> 1 file changed, 10 insertions(+), 12 deletions(-)
>
> diff --git a/libavformat/mux.c b/libavformat/mux.c
> index bfd1bf491b..814d773b9d 100644
> --- a/libavformat/mux.c
> +++ b/libavformat/mux.c
> @@ -771,9 +771,6 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
> 
> static int check_packet(AVFormatContext *s, AVPacket *pkt)
> {
> -    if (!pkt)
> -        return 0;
> -
>     if (pkt->stream_index < 0 || pkt->stream_index >= s->nb_streams) {
>         av_log(s, AV_LOG_ERROR, "Invalid packet stream index: %d\n",
>                pkt->stream_index);
> @@ -887,10 +884,6 @@ int av_write_frame(AVFormatContext *s, AVPacket *pkt)
> {
>     int ret;
> 
> -    ret = prepare_input_packet(s, pkt);
> -    if (ret < 0)
> -        return ret;
> -
>     if (!pkt) {
>         if (s->oformat->flags & AVFMT_ALLOW_FLUSH) {
>             ret = s->oformat->write_packet(s, NULL);
> @@ -902,6 +895,10 @@ int av_write_frame(AVFormatContext *s, AVPacket *pkt)
>         return 1;
>     }
> 
> +    ret = prepare_input_packet(s, pkt);
> +    if (ret < 0)
> +        return ret;
> +
>     ret = do_packet_auto_bsf(s, pkt);
>     if (ret <= 0)
>         return ret;
> @@ -1191,12 +1188,12 @@ int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
> {
>     int ret, flush = 0;
> 
> -    ret = prepare_input_packet(s, pkt);
> -    if (ret < 0)
> -        goto fail;
> -
>     if (pkt) {
> -        AVStream *st = s->streams[pkt->stream_index];
> +        AVStream *st;

Delaying the initialization is not needed as far as I see, so you can keep 
this as is.

> +
> +        ret = prepare_input_packet(s, pkt);
> +        if (ret < 0)
> +            goto fail;
>
>         ret = do_packet_auto_bsf(s, pkt);
>         if (ret == 0)
> @@ -1209,6 +1206,7 @@ int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
>                 pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
> 
> #if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX
> +        st = s->streams[pkt->stream_index];
>         if ((ret = compute_muxer_pkt_fields(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
>             goto fail;
> #endif
> -- 
> 2.20.1

Otherwise LGTM.

Thanks,
Marton


More information about the ffmpeg-devel mailing list