]> git.sesse.net Git - ffmpeg/blobdiff - tests/api/api-flac-test.c
avcodec/codec, allcodecs: Constify the AVCodec API
[ffmpeg] / tests / api / api-flac-test.c
index 7c96a4d99e8d05f798cea6282f580bedc43a12b7..88b15e87229de7fc22ff8513b014b76613a06fd9 100644 (file)
@@ -48,7 +48,7 @@ static int generate_raw_frame(uint16_t *frame_data, int i, int sample_rate,
     return 0;
 }
 
-static int init_encoder(AVCodec *enc, AVCodecContext **enc_ctx,
+static int init_encoder(const AVCodec *enc, AVCodecContext **enc_ctx,
                         int64_t ch_layout, int sample_rate)
 {
     AVCodecContext *ctx;
@@ -78,7 +78,7 @@ static int init_encoder(AVCodec *enc, AVCodecContext **enc_ctx,
     return 0;
 }
 
-static int init_decoder(AVCodec *dec, AVCodecContext **dec_ctx,
+static int init_decoder(const AVCodec *dec, AVCodecContext **dec_ctx,
                         int64_t ch_layout)
 {
     AVCodecContext *ctx;
@@ -105,18 +105,23 @@ static int init_decoder(AVCodec *dec, AVCodecContext **dec_ctx,
     return 0;
 }
 
-static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx,
-                    AVCodecContext *dec_ctx)
+static int run_test(const AVCodec *enc, const 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;
     int result = 0;
-    int got_output = 0;
     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 +155,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;
-
         result = av_frame_make_writable(in_frame);
         if (result < 0)
             return result;
@@ -167,50 +168,63 @@ 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 submitting a packet for decoding\n");
+                return result;
+            }
+
+            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 (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;
-                }
-
-                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");
-                    return AVERROR_UNKNOWN;
-                }
-
-                if (in_frame->channel_layout != out_frame->channel_layout) {
-                    av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different channel layout\n");
-                    return AVERROR_UNKNOWN;
-                }
-
-                if (in_frame->format != out_frame->format) {
-                    av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different sample format\n");
-                    return AVERROR_UNKNOWN;
-                }
-                out_frame_bytes = out_frame->nb_samples * out_frame->channels * sizeof(uint16_t);
-                if (out_frame_bytes > out_frame->linesize[0]) {
-                    av_log(NULL, AV_LOG_ERROR, "Incorrect value of output frame linesize\n");
-                    return 1;
-                }
-                memcpy(raw_out + out_offset, out_frame->data[0], out_frame_bytes);
-                out_offset += out_frame_bytes;
+            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");
+                return AVERROR_UNKNOWN;
+            }
+
+            if (in_frame->channel_layout != out_frame->channel_layout) {
+                av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different channel layout\n");
+                return AVERROR_UNKNOWN;
+            }
+
+            if (in_frame->format != out_frame->format) {
+                av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different sample format\n");
+                return AVERROR_UNKNOWN;
+            }
+            out_frame_bytes = out_frame->nb_samples * out_frame->channels * sizeof(uint16_t);
+            if (out_frame_bytes > out_frame->linesize[0]) {
+                av_log(NULL, AV_LOG_ERROR, "Incorrect value of output frame linesize\n");
+                return 1;
             }
+            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) {
@@ -222,6 +236,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;
@@ -229,7 +244,7 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx,
 
 int main(void)
 {
-    AVCodec *enc = NULL, *dec = NULL;
+    const AVCodec *enc = NULL, *dec = NULL;
     AVCodecContext *enc_ctx = NULL, *dec_ctx = NULL;
     uint64_t channel_layouts[] = {AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_5POINT1_BACK, AV_CH_LAYOUT_SURROUND, AV_CH_LAYOUT_STEREO_DOWNMIX};
     int sample_rates[] = {8000, 44100, 48000, 192000};