[07/13] examples/decode_audio: allocate the packet dynamically

Message ID 1478793085-10046-7-git-send-email-anton@khirnov.net
State New
Headers show

Commit Message

Anton Khirnov Nov. 10, 2016, 3:51 p.m.
AVPackets on stack are discouraged now.
---
 doc/examples/decode_audio.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

Comments

Vittorio Giovara Nov. 10, 2016, 6:15 p.m. | #1
On Thu, Nov 10, 2016 at 10:51 AM, Anton Khirnov <anton@khirnov.net> wrote:
> AVPackets on stack are discouraged now.
> ---
>  doc/examples/decode_audio.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
>
> diff --git a/doc/examples/decode_audio.c b/doc/examples/decode_audio.c
> index c00d488..e7b27d3 100644
> --- a/doc/examples/decode_audio.c
> +++ b/doc/examples/decode_audio.c
> @@ -97,7 +97,7 @@ int main(int argc, char **argv)
>      uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
>      uint8_t *data;
>      size_t   data_size;
> -    AVPacket avpkt;
> +    AVPacket *pkt;
>      AVFrame *decoded_frame = NULL;
>
>      if (argc <= 2) {
> @@ -110,7 +110,7 @@ int main(int argc, char **argv)
>      /* register all the codecs */
>      avcodec_register_all();
>
> -    av_init_packet(&avpkt);
> +    pkt = av_packet_alloc();
>
>      /* find the MPEG audio decoder */
>      codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
> @@ -156,7 +156,7 @@ int main(int argc, char **argv)
>              }
>          }
>
> -        ret = av_parser_parse2(parser, c, &avpkt.data, &avpkt.size,
> +        ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
>                                 data, data_size,
>                                 AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
>          if (ret < 0) {
> @@ -166,8 +166,8 @@ int main(int argc, char **argv)
>          data      += ret;
>          data_size -= ret;
>
> -        if (avpkt.size)
> -            decode(c, &avpkt, decoded_frame, outfile);
> +        if (pkt->size)
> +            decode(c, pkt, decoded_frame, outfile);
>
>          if (data_size < AUDIO_REFILL_THRESH) {
>              memmove(inbuf, data, data_size);
> @@ -185,6 +185,7 @@ int main(int argc, char **argv)
>      avcodec_free_context(&c);
>      av_parser_close(parser);
>      av_frame_free(&decoded_frame);
> +    av_packet_free(&pkt);
>
>      return 0;
>  }
> --

ok

Patch

diff --git a/doc/examples/decode_audio.c b/doc/examples/decode_audio.c
index c00d488..e7b27d3 100644
--- a/doc/examples/decode_audio.c
+++ b/doc/examples/decode_audio.c
@@ -97,7 +97,7 @@  int main(int argc, char **argv)
     uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
     uint8_t *data;
     size_t   data_size;
-    AVPacket avpkt;
+    AVPacket *pkt;
     AVFrame *decoded_frame = NULL;
 
     if (argc <= 2) {
@@ -110,7 +110,7 @@  int main(int argc, char **argv)
     /* register all the codecs */
     avcodec_register_all();
 
-    av_init_packet(&avpkt);
+    pkt = av_packet_alloc();
 
     /* find the MPEG audio decoder */
     codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
@@ -156,7 +156,7 @@  int main(int argc, char **argv)
             }
         }
 
-        ret = av_parser_parse2(parser, c, &avpkt.data, &avpkt.size,
+        ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
                                data, data_size,
                                AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
         if (ret < 0) {
@@ -166,8 +166,8 @@  int main(int argc, char **argv)
         data      += ret;
         data_size -= ret;
 
-        if (avpkt.size)
-            decode(c, &avpkt, decoded_frame, outfile);
+        if (pkt->size)
+            decode(c, pkt, decoded_frame, outfile);
 
         if (data_size < AUDIO_REFILL_THRESH) {
             memmove(inbuf, data, data_size);
@@ -185,6 +185,7 @@  int main(int argc, char **argv)
     avcodec_free_context(&c);
     av_parser_close(parser);
     av_frame_free(&decoded_frame);
+    av_packet_free(&pkt);
 
     return 0;
 }