]> git.sesse.net Git - nageru/blobdiff - nageru/audio_encoder.cpp
Stop using av_init_packet().
[nageru] / nageru / audio_encoder.cpp
index 126e0e2dce19ef40f0406bea7a3d4abed120c731..e683265c67f17485937fd4d7393538825f0649cc 100644 (file)
@@ -135,18 +135,16 @@ void AudioEncoder::encode_audio_one_frame(const float *audio, size_t num_samples
        }
 
        for ( ;; ) {  // Termination condition within loop.
-               AVPacket pkt;
-               av_init_packet(&pkt);
-               pkt.data = nullptr;
-               pkt.size = 0;
-               int err = avcodec_receive_packet(ctx, &pkt);
+               AVPacketWithDeleter pkt = av_packet_alloc_unique();
+               pkt->data = nullptr;
+               pkt->size = 0;
+               int err = avcodec_receive_packet(ctx, pkt.get());
                if (err == 0) {
-                       pkt.stream_index = 1;
-                       pkt.flags = 0;
+                       pkt->stream_index = 1;
+                       pkt->flags = 0;
                        for (Mux *mux : muxes) {
-                               mux->add_packet(pkt, pkt.pts, pkt.dts);
+                               mux->add_packet(*pkt, pkt->pts, pkt->dts);
                        }
-                       av_packet_unref(&pkt);
                } else if (err == AVERROR(EAGAIN)) {
                        break;
                } else {
@@ -171,18 +169,16 @@ void AudioEncoder::encode_last_audio()
        if (ctx->codec->capabilities & AV_CODEC_CAP_DELAY) {
                // Collect any delayed frames.
                for ( ;; ) {
-                       AVPacket pkt;
-                       av_init_packet(&pkt);
-                       pkt.data = nullptr;
-                       pkt.size = 0;
-                       int err = avcodec_receive_packet(ctx, &pkt);
+                       AVPacketWithDeleter pkt = av_packet_alloc_unique();
+                       pkt->data = nullptr;
+                       pkt->size = 0;
+                       int err = avcodec_receive_packet(ctx, pkt.get());
                        if (err == 0) {
-                               pkt.stream_index = 1;
-                               pkt.flags = 0;
+                               pkt->stream_index = 1;
+                               pkt->flags = 0;
                                for (Mux *mux : muxes) {
-                                       mux->add_packet(pkt, pkt.pts, pkt.dts);
+                                       mux->add_packet(*pkt, pkt->pts, pkt->dts);
                                }
-                               av_packet_unref(&pkt);
                        } else if (err == AVERROR_EOF) {
                                break;
                        } else {