[FFmpeg-cvslog] doc/examples/transcode_aac: use av_packet_alloc() to allocate packets

James Almer git at videolan.org
Wed Mar 17 21:17:34 EET 2021


ffmpeg | branch: master | James Almer <jamrial at gmail.com> | Sun Jan 31 13:33:56 2021 -0300| [16dba17329cebd8a9a5fa80b9e91ef0958fe90c0] | committer: James Almer

doc/examples/transcode_aac: use av_packet_alloc() to allocate packets

Signed-off-by: James Almer <jamrial at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=16dba17329cebd8a9a5fa80b9e91ef0958fe90c0
---

 doc/examples/transcode_aac.c | 46 ++++++++++++++++++++++++++------------------
 1 file changed, 27 insertions(+), 19 deletions(-)

diff --git a/doc/examples/transcode_aac.c b/doc/examples/transcode_aac.c
index e0c76f5b35..73786ab59b 100644
--- a/doc/examples/transcode_aac.c
+++ b/doc/examples/transcode_aac.c
@@ -245,14 +245,16 @@ cleanup:
 
 /**
  * Initialize one data packet for reading or writing.
- * @param packet Packet to be initialized
+ * @param[out] packet Packet to be initialized
+ * @return Error code (0 if successful)
  */
-static void init_packet(AVPacket *packet)
+static int init_packet(AVPacket **packet)
 {
-    av_init_packet(packet);
-    /* Set the packet data and size so that it is recognized as being empty. */
-    packet->data = NULL;
-    packet->size = 0;
+    if (!(*packet = av_packet_alloc())) {
+        fprintf(stderr, "Could not allocate packet\n");
+        return AVERROR(ENOMEM);
+    }
+    return 0;
 }
 
 /**
@@ -371,28 +373,31 @@ static int decode_audio_frame(AVFrame *frame,
                               int *data_present, int *finished)
 {
     /* Packet used for temporary storage. */
-    AVPacket input_packet;
+    AVPacket *input_packet;
     int error;
-    init_packet(&input_packet);
+
+    error = init_packet(&input_packet);
+    if (error < 0)
+        return error;
 
     /* Read one audio frame from the input file into a temporary packet. */
-    if ((error = av_read_frame(input_format_context, &input_packet)) < 0) {
+    if ((error = av_read_frame(input_format_context, input_packet)) < 0) {
         /* If we are at the end of the file, flush the decoder below. */
         if (error == AVERROR_EOF)
             *finished = 1;
         else {
             fprintf(stderr, "Could not read frame (error '%s')\n",
                     av_err2str(error));
-            return error;
+            goto cleanup;
         }
     }
 
     /* Send the audio frame stored in the temporary packet to the decoder.
      * The input audio stream decoder is used to do this. */
-    if ((error = avcodec_send_packet(input_codec_context, &input_packet)) < 0) {
+    if ((error = avcodec_send_packet(input_codec_context, input_packet)) < 0) {
         fprintf(stderr, "Could not send packet for decoding (error '%s')\n",
                 av_err2str(error));
-        return error;
+        goto cleanup;
     }
 
     /* Receive one frame from the decoder. */
@@ -418,7 +423,7 @@ static int decode_audio_frame(AVFrame *frame,
     }
 
 cleanup:
-    av_packet_unref(&input_packet);
+    av_packet_free(&input_packet);
     return error;
 }
 
@@ -661,9 +666,12 @@ static int encode_audio_frame(AVFrame *frame,
                               int *data_present)
 {
     /* Packet used for temporary storage. */
-    AVPacket output_packet;
+    AVPacket *output_packet;
     int error;
-    init_packet(&output_packet);
+
+    error = init_packet(&output_packet);
+    if (error < 0)
+        return error;
 
     /* Set a timestamp based on the sample rate for the container. */
     if (frame) {
@@ -681,11 +689,11 @@ static int encode_audio_frame(AVFrame *frame,
     } else if (error < 0) {
         fprintf(stderr, "Could not send packet for encoding (error '%s')\n",
                 av_err2str(error));
-        return error;
+        goto cleanup;
     }
 
     /* Receive one encoded frame from the encoder. */
-    error = avcodec_receive_packet(output_codec_context, &output_packet);
+    error = avcodec_receive_packet(output_codec_context, output_packet);
     /* If the encoder asks for more data to be able to provide an
      * encoded frame, return indicating that no data is present. */
     if (error == AVERROR(EAGAIN)) {
@@ -706,14 +714,14 @@ static int encode_audio_frame(AVFrame *frame,
 
     /* Write one audio frame from the temporary packet to the output file. */
     if (*data_present &&
-        (error = av_write_frame(output_format_context, &output_packet)) < 0) {
+        (error = av_write_frame(output_format_context, output_packet)) < 0) {
         fprintf(stderr, "Could not write frame (error '%s')\n",
                 av_err2str(error));
         goto cleanup;
     }
 
 cleanup:
-    av_packet_unref(&output_packet);
+    av_packet_free(&output_packet);
     return error;
 }
 



More information about the ffmpeg-cvslog mailing list