diff mbox series

[FFmpeg-devel,8/9] lavc/decode: move submitting input packets to bitstream filters

Message ID 20230620141608.31759-8-anton@khirnov.net
State New
Headers show
Series [FFmpeg-devel,1/9] lavc: add a header for internal generic-layer APIs | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 fail Make fate failed
andriy/make_x86 success Make finished
andriy/make_fate_x86 fail Make fate failed

Commit Message

Anton Khirnov June 20, 2023, 2:16 p.m. UTC
Do it from ff_decode_get_packet() rather than from
avcodec_send_packet(). This way all nontrivial stages of the decoding
pipeline (i.e. other than just placing a packet at its entrance) are
pull-based rather than a mix of push an pull.
---
 libavcodec/decode.c | 36 ++++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)

Comments

James Almer June 20, 2023, 5:48 p.m. UTC | #1
On 6/20/2023 11:16 AM, Anton Khirnov wrote:
> Do it from ff_decode_get_packet() rather than from
> avcodec_send_packet(). This way all nontrivial stages of the decoding
> pipeline (i.e. other than just placing a packet at its entrance) are
> pull-based rather than a mix of push an pull.
> ---
>   libavcodec/decode.c | 36 ++++++++++++++++++++++++++----------
>   1 file changed, 26 insertions(+), 10 deletions(-)
> 
> diff --git a/libavcodec/decode.c b/libavcodec/decode.c
> index c61ce74fb8..8d892432be 100644
> --- a/libavcodec/decode.c
> +++ b/libavcodec/decode.c
> @@ -193,14 +193,11 @@ fail:
>       return ret;
>   }
>   
> -int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
> +static int decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
>   {
>       AVCodecInternal *avci = avctx->internal;
>       int ret;
>   
> -    if (avci->draining)
> -        return AVERROR_EOF;
> -
>       ret = av_bsf_receive_packet(avci->bsf, pkt);
>       if (ret == AVERROR_EOF)
>           avci->draining = 1;
> @@ -223,6 +220,31 @@ finish:
>       return ret;
>   }
>   
> +int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
> +{
> +    AVCodecInternal *avci = avctx->internal;
> +
> +    if (avci->draining)
> +        return AVERROR_EOF;
> +
> +    while (1) {
> +        int ret = decode_get_packet(avctx, pkt);
> +        if (ret == AVERROR(EAGAIN) &&
> +            (avci->buffer_pkt->data || avci->buffer_pkt->side_data_elems ||

nit: Since i removed the IS_EMPTY() macro recently from this file, you 
could move the one in bsf.c into packet_internal.h, rename it to 
something like AVPACKET_IS_EMPTY(), and use it here.


> +             avci->d->draining_started)) {
> +            ret = av_bsf_send_packet(avci->bsf, avci->buffer_pkt);
> +            if (ret < 0) {
> +                av_packet_unref(avci->buffer_pkt);
> +                return ret;
> +            }
> +
> +            continue;
> +        }
> +
> +        return ret;
> +    }
> +}
> +
>   /**
>    * Attempt to guess proper monotonic timestamps for decoded video frames
>    * which might have incorrect times. Input timestamps may wrap around, in
> @@ -643,12 +665,6 @@ int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacke
>       } else
>           avci->d->draining_started = 1;
>   
> -    ret = av_bsf_send_packet(avci->bsf, avci->buffer_pkt);
> -    if (ret < 0) {
> -        av_packet_unref(avci->buffer_pkt);
> -        return ret;
> -    }
> -
>       if (!avci->buffer_frame->buf[0]) {
>           ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
>           if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
diff mbox series

Patch

diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index c61ce74fb8..8d892432be 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -193,14 +193,11 @@  fail:
     return ret;
 }
 
-int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
+static int decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
 {
     AVCodecInternal *avci = avctx->internal;
     int ret;
 
-    if (avci->draining)
-        return AVERROR_EOF;
-
     ret = av_bsf_receive_packet(avci->bsf, pkt);
     if (ret == AVERROR_EOF)
         avci->draining = 1;
@@ -223,6 +220,31 @@  finish:
     return ret;
 }
 
+int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
+{
+    AVCodecInternal *avci = avctx->internal;
+
+    if (avci->draining)
+        return AVERROR_EOF;
+
+    while (1) {
+        int ret = decode_get_packet(avctx, pkt);
+        if (ret == AVERROR(EAGAIN) &&
+            (avci->buffer_pkt->data || avci->buffer_pkt->side_data_elems ||
+             avci->d->draining_started)) {
+            ret = av_bsf_send_packet(avci->bsf, avci->buffer_pkt);
+            if (ret < 0) {
+                av_packet_unref(avci->buffer_pkt);
+                return ret;
+            }
+
+            continue;
+        }
+
+        return ret;
+    }
+}
+
 /**
  * Attempt to guess proper monotonic timestamps for decoded video frames
  * which might have incorrect times. Input timestamps may wrap around, in
@@ -643,12 +665,6 @@  int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacke
     } else
         avci->d->draining_started = 1;
 
-    ret = av_bsf_send_packet(avci->bsf, avci->buffer_pkt);
-    if (ret < 0) {
-        av_packet_unref(avci->buffer_pkt);
-        return ret;
-    }
-
     if (!avci->buffer_frame->buf[0]) {
         ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
         if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)