diff mbox series

[FFmpeg-devel,3/5] tests/api-flac-test: convert to new encoding/decoding API

Message ID 20210224100402.22300-3-anton@khirnov.net
State Accepted
Commit f942e1488400e94bfc2aeb38a413a9056c2f4b0d
Headers show
Series [FFmpeg-devel,1/5] tests/api-band-test: simplify code | expand

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Anton Khirnov Feb. 24, 2021, 10:04 a.m. UTC
---
 tests/api/api-flac-test.c | 40 +++++++++++++++++++++++++--------------
 1 file changed, 26 insertions(+), 14 deletions(-)

Comments

James Almer Feb. 26, 2021, 6:59 p.m. UTC | #1
On 2/24/2021 7:04 AM, Anton Khirnov wrote:
> ---
>   tests/api/api-flac-test.c | 40 +++++++++++++++++++++++++--------------
>   1 file changed, 26 insertions(+), 14 deletions(-)
> 
> diff --git a/tests/api/api-flac-test.c b/tests/api/api-flac-test.c
> index 7c96a4d99e..3d83c56987 100644
> --- a/tests/api/api-flac-test.c
> +++ b/tests/api/api-flac-test.c
> @@ -113,7 +113,6 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx,
>       uint8_t *raw_in = NULL, *raw_out = NULL;
>       int in_offset = 0, out_offset = 0;
>       int result = 0;
> -    int got_output = 0;
>       int i = 0;
>       int in_frame_bytes, out_frame_bytes;
>   
> @@ -167,25 +166,40 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx,
>           }
>           memcpy(raw_in + in_offset, in_frame->data[0], in_frame_bytes);
>           in_offset += in_frame_bytes;
> -        result = avcodec_encode_audio2(enc_ctx, &enc_pkt, in_frame, &got_output);
> +        result = avcodec_send_frame(enc_ctx, in_frame);
>           if (result < 0) {
> -            av_log(NULL, AV_LOG_ERROR, "Error encoding audio frame\n");
> +            av_log(NULL, AV_LOG_ERROR, "Error submitting a frame for encoding\n");
>               return result;
>           }
>   
> -        /* if we get an encoded packet, feed it straight to the decoder */
> -        if (got_output) {
> -            result = avcodec_decode_audio4(dec_ctx, out_frame, &got_output, &enc_pkt);
> +        while (result >= 0) {
> +            result = avcodec_receive_packet(enc_ctx, &enc_pkt);
> +            if (result == AVERROR(EAGAIN))
> +                break;
> +            else if (result < 0 && result != AVERROR_EOF) {
> +                av_log(NULL, AV_LOG_ERROR, "Error encoding audio frame\n");
> +                return result;
> +            }
> +
> +            /* if we get an encoded packet, feed it straight to the decoder */
> +            result = avcodec_send_packet(dec_ctx, &enc_pkt);
> +            av_packet_unref(&enc_pkt);
>               if (result < 0) {
> -                av_log(NULL, AV_LOG_ERROR, "Error decoding audio packet\n");
> +                av_log(NULL, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
>                   return result;
>               }
>   
> -            if (got_output) {
> -                if (result != enc_pkt.size) {
> -                    av_log(NULL, AV_LOG_INFO, "Decoder consumed only part of a packet, it is allowed to do so -- need to update this test\n");
> -                    return AVERROR_UNKNOWN;
> -                }
> +            result = avcodec_receive_frame(dec_ctx, out_frame);
> +            if (result == AVERROR(EAGAIN)) {
> +                result = 0;
> +                continue;
> +            } else if (result == AVERROR(EOF)) {
> +                result = 0;
> +                break;
> +            } else if (result < 0) {
> +                av_log(NULL, AV_LOG_ERROR, "Error decoding audio packet\n");
> +                return result;
> +            }
>   
>                   if (in_frame->nb_samples != out_frame->nb_samples) {
>                       av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different number of samples\n");
> @@ -208,9 +222,7 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx,
>                   }
>                   memcpy(raw_out + out_offset, out_frame->data[0], out_frame_bytes);
>                   out_offset += out_frame_bytes;
> -            }
>           }
> -        av_packet_unref(&enc_pkt);
>       }
>   
>       if (memcmp(raw_in, raw_out, out_frame_bytes * NUMBER_OF_AUDIO_FRAMES) != 0) {

LGTM.

Also, FWIW, this is not flushing the encoder (It wasn't before this 
patch either). For being an API test it's not exactly using it right, 
but it doesn't really matter here since for FLAC, flushing the encoder 
will return a side data only packet with an updated stream header info 
and not any delayed/buffered packet, and this test only cares about 
comparing the raw pcm audio pre encoding and post decoding.
Anton Khirnov March 1, 2021, 1:42 p.m. UTC | #2
Quoting James Almer (2021-02-26 19:59:57)
> 
> LGTM.
> 
> Also, FWIW, this is not flushing the encoder (It wasn't before this 
> patch either). For being an API test it's not exactly using it right, 
> but it doesn't really matter here since for FLAC, flushing the encoder 
> will return a side data only packet with an updated stream header info 
> and not any delayed/buffered packet, and this test only cares about 
> comparing the raw pcm audio pre encoding and post decoding.

I agree that this tests makes a bit too many assumptions, but I didn't
want to rewrite everything, since I'd like to actually get to the bump
reasonably soon.
diff mbox series

Patch

diff --git a/tests/api/api-flac-test.c b/tests/api/api-flac-test.c
index 7c96a4d99e..3d83c56987 100644
--- a/tests/api/api-flac-test.c
+++ b/tests/api/api-flac-test.c
@@ -113,7 +113,6 @@  static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx,
     uint8_t *raw_in = NULL, *raw_out = NULL;
     int in_offset = 0, out_offset = 0;
     int result = 0;
-    int got_output = 0;
     int i = 0;
     int in_frame_bytes, out_frame_bytes;
 
@@ -167,25 +166,40 @@  static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx,
         }
         memcpy(raw_in + in_offset, in_frame->data[0], in_frame_bytes);
         in_offset += in_frame_bytes;
-        result = avcodec_encode_audio2(enc_ctx, &enc_pkt, in_frame, &got_output);
+        result = avcodec_send_frame(enc_ctx, in_frame);
         if (result < 0) {
-            av_log(NULL, AV_LOG_ERROR, "Error encoding audio frame\n");
+            av_log(NULL, AV_LOG_ERROR, "Error submitting a frame for encoding\n");
             return result;
         }
 
-        /* if we get an encoded packet, feed it straight to the decoder */
-        if (got_output) {
-            result = avcodec_decode_audio4(dec_ctx, out_frame, &got_output, &enc_pkt);
+        while (result >= 0) {
+            result = avcodec_receive_packet(enc_ctx, &enc_pkt);
+            if (result == AVERROR(EAGAIN))
+                break;
+            else if (result < 0 && result != AVERROR_EOF) {
+                av_log(NULL, AV_LOG_ERROR, "Error encoding audio frame\n");
+                return result;
+            }
+
+            /* if we get an encoded packet, feed it straight to the decoder */
+            result = avcodec_send_packet(dec_ctx, &enc_pkt);
+            av_packet_unref(&enc_pkt);
             if (result < 0) {
-                av_log(NULL, AV_LOG_ERROR, "Error decoding audio packet\n");
+                av_log(NULL, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
                 return result;
             }
 
-            if (got_output) {
-                if (result != enc_pkt.size) {
-                    av_log(NULL, AV_LOG_INFO, "Decoder consumed only part of a packet, it is allowed to do so -- need to update this test\n");
-                    return AVERROR_UNKNOWN;
-                }
+            result = avcodec_receive_frame(dec_ctx, out_frame);
+            if (result == AVERROR(EAGAIN)) {
+                result = 0;
+                continue;
+            } else if (result == AVERROR(EOF)) {
+                result = 0;
+                break;
+            } else if (result < 0) {
+                av_log(NULL, AV_LOG_ERROR, "Error decoding audio packet\n");
+                return result;
+            }
 
                 if (in_frame->nb_samples != out_frame->nb_samples) {
                     av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different number of samples\n");
@@ -208,9 +222,7 @@  static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx,
                 }
                 memcpy(raw_out + out_offset, out_frame->data[0], out_frame_bytes);
                 out_offset += out_frame_bytes;
-            }
         }
-        av_packet_unref(&enc_pkt);
     }
 
     if (memcmp(raw_in, raw_out, out_frame_bytes * NUMBER_OF_AUDIO_FRAMES) != 0) {