[13/13] examples/decode_video: allocate the packet dynamically

Message ID 1478793085-10046-13-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.
---
 doc/examples/decode_video.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

Comments

Vittorio Giovara Nov. 10, 2016, 6:18 p.m. | #1
On Thu, Nov 10, 2016 at 10:51 AM, Anton Khirnov <anton@khirnov.net> wrote:
> AVPackets on stack are discouraged.
> ---
>  doc/examples/decode_video.c | 13 ++++++++-----
>  1 file changed, 8 insertions(+), 5 deletions(-)
>
> diff --git a/doc/examples/decode_video.c b/doc/examples/decode_video.c
> index 7414643..d6803ef 100644
> --- a/doc/examples/decode_video.c
> +++ b/doc/examples/decode_video.c
> @@ -94,7 +94,7 @@ int main(int argc, char **argv)
>      uint8_t *data;
>      size_t   data_size;
>      int ret;
> -    AVPacket avpkt;
> +    AVPacket *pkt;
>
>      if (argc <= 2) {
>          fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
> @@ -105,7 +105,9 @@ int main(int argc, char **argv)
>
>      avcodec_register_all();
>
> -    av_init_packet(&avpkt);
> +    pkt = av_packet_alloc();
> +    if (!pkt)
> +        exit(1);
>
>      /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
>      memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
> @@ -151,7 +153,7 @@ int main(int argc, char **argv)
>          /* use the parser to split the data into frames */
>          data = inbuf;
>          while (data_size > 0) {
> -            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) {
>                  fprintf(stderr, "Error while parsing\n");
> @@ -160,8 +162,8 @@ int main(int argc, char **argv)
>              data      += ret;
>              data_size -= ret;
>
> -            if (avpkt.size)
> -                decode(c, picture, &avpkt, outfilename);
> +            if (pkt->size)
> +                decode(c, picture, pkt, outfilename);
>          }
>      }
>
> @@ -173,6 +175,7 @@ int main(int argc, char **argv)
>      av_parser_close(parser);
>      avcodec_free_context(&c);
>      av_frame_free(&picture);
> +    av_packet_free(&pkt);
>
>      return 0;
>  }
> --

ok

Patch

diff --git a/doc/examples/decode_video.c b/doc/examples/decode_video.c
index 7414643..d6803ef 100644
--- a/doc/examples/decode_video.c
+++ b/doc/examples/decode_video.c
@@ -94,7 +94,7 @@  int main(int argc, char **argv)
     uint8_t *data;
     size_t   data_size;
     int ret;
-    AVPacket avpkt;
+    AVPacket *pkt;
 
     if (argc <= 2) {
         fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
@@ -105,7 +105,9 @@  int main(int argc, char **argv)
 
     avcodec_register_all();
 
-    av_init_packet(&avpkt);
+    pkt = av_packet_alloc();
+    if (!pkt)
+        exit(1);
 
     /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
     memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
@@ -151,7 +153,7 @@  int main(int argc, char **argv)
         /* use the parser to split the data into frames */
         data = inbuf;
         while (data_size > 0) {
-            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) {
                 fprintf(stderr, "Error while parsing\n");
@@ -160,8 +162,8 @@  int main(int argc, char **argv)
             data      += ret;
             data_size -= ret;
 
-            if (avpkt.size)
-                decode(c, picture, &avpkt, outfilename);
+            if (pkt->size)
+                decode(c, picture, pkt, outfilename);
         }
     }
 
@@ -173,6 +175,7 @@  int main(int argc, char **argv)
     av_parser_close(parser);
     avcodec_free_context(&c);
     av_frame_free(&picture);
+    av_packet_free(&pkt);
 
     return 0;
 }