[FFmpeg-devel] [PATCH 42/50] tests/api/api-flac-test: use av_packet_alloc() to allocate packets

James Almer jamrial at gmail.com
Thu Feb 4 21:09:57 EET 2021


Signed-off-by: James Almer <jamrial at gmail.com>
---
 tests/api/api-flac-test.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/tests/api/api-flac-test.c b/tests/api/api-flac-test.c
index 3fea3258f3..4ce62ed8ba 100644
--- a/tests/api/api-flac-test.c
+++ b/tests/api/api-flac-test.c
@@ -108,7 +108,7 @@ static int init_decoder(AVCodec *dec, AVCodecContext **dec_ctx,
 static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx,
                     AVCodecContext *dec_ctx)
 {
-    AVPacket enc_pkt;
+    AVPacket *enc_pkt;
     AVFrame *in_frame, *out_frame;
     uint8_t *raw_in = NULL, *raw_out = NULL;
     int in_offset = 0, out_offset = 0;
@@ -117,6 +117,12 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx,
     int i = 0;
     int in_frame_bytes, out_frame_bytes;
 
+    enc_pkt = av_packet_alloc();
+    if (!enc_pkt) {
+        av_log(NULL, AV_LOG_ERROR, "Can't allocate output packet\n");
+        return AVERROR(ENOMEM);
+    }
+
     in_frame = av_frame_alloc();
     if (!in_frame) {
         av_log(NULL, AV_LOG_ERROR, "Can't allocate input frame\n");
@@ -150,10 +156,6 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx,
     }
 
     for (i = 0; i < NUMBER_OF_AUDIO_FRAMES; i++) {
-        av_init_packet(&enc_pkt);
-        enc_pkt.data = NULL;
-        enc_pkt.size = 0;
-
         generate_raw_frame((uint16_t*)(in_frame->data[0]), i, enc_ctx->sample_rate,
                            enc_ctx->channels, enc_ctx->frame_size);
         in_frame_bytes = in_frame->nb_samples * in_frame->channels * sizeof(uint16_t);
@@ -163,7 +165,7 @@ 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_encode_audio2(enc_ctx, enc_pkt, in_frame, &got_output);
         if (result < 0) {
             av_log(NULL, AV_LOG_ERROR, "Error encoding audio frame\n");
             return result;
@@ -171,14 +173,14 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx,
 
         /* 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);
+            result = avcodec_decode_audio4(dec_ctx, out_frame, &got_output, enc_pkt);
             if (result < 0) {
                 av_log(NULL, AV_LOG_ERROR, "Error decoding audio packet\n");
                 return result;
             }
 
             if (got_output) {
-                if (result != enc_pkt.size) {
+                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;
                 }
@@ -206,7 +208,7 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx,
                 out_offset += out_frame_bytes;
             }
         }
-        av_packet_unref(&enc_pkt);
+        av_packet_unref(enc_pkt);
     }
 
     if (memcmp(raw_in, raw_out, out_frame_bytes * NUMBER_OF_AUDIO_FRAMES) != 0) {
@@ -218,6 +220,7 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx,
 
     av_freep(&raw_in);
     av_freep(&raw_out);
+    av_packet_free(&enc_pkt);
     av_frame_free(&in_frame);
     av_frame_free(&out_frame);
     return 0;
-- 
2.30.0



More information about the ffmpeg-devel mailing list