[FFmpeg-cvslog] examples/encode_video: allocate the packet dynamically

Anton Khirnov git at videolan.org
Tue Apr 4 12:47:35 EEST 2017


ffmpeg | branch: master | Anton Khirnov <anton at khirnov.net> | Thu Oct 20 11:03:20 2016 +0200| [59ab9e8ba1df7e3347a4cd2bd56c32e74aede802] | committer: Anton Khirnov

examples/encode_video: allocate the packet dynamically

AVPackets on stack are discouraged.

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

 doc/examples/encode_video.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/doc/examples/encode_video.c b/doc/examples/encode_video.c
index 2ff6354..cb12836 100644
--- a/doc/examples/encode_video.c
+++ b/doc/examples/encode_video.c
@@ -69,7 +69,7 @@ int main(int argc, char **argv)
     int i, ret, x, y;
     FILE *f;
     AVFrame *picture;
-    AVPacket pkt;
+    AVPacket *pkt;
     uint8_t endcode[] = { 0, 0, 1, 0xb7 };
 
     if (argc <= 1) {
@@ -90,6 +90,10 @@ int main(int argc, char **argv)
     c = avcodec_alloc_context3(codec);
     picture = av_frame_alloc();
 
+    pkt = av_packet_alloc();
+    if (!pkt)
+        exit(1);
+
     /* put sample parameters */
     c->bit_rate = 400000;
     /* resolution must be a multiple of two */
@@ -127,10 +131,6 @@ int main(int argc, char **argv)
 
     /* encode 1 second of video */
     for(i=0;i<25;i++) {
-        av_init_packet(&pkt);
-        pkt.data = NULL;    // packet data will be allocated by the encoder
-        pkt.size = 0;
-
         fflush(stdout);
 
         /* make sure the frame data is writable */
@@ -157,11 +157,11 @@ int main(int argc, char **argv)
         picture->pts = i;
 
         /* encode the image */
-        encode(c, picture, &pkt, f);
+        encode(c, picture, pkt, f);
     }
 
     /* flush the encoder */
-    encode(c, NULL, &pkt, f);
+    encode(c, NULL, pkt, f);
 
     /* add sequence end code to have a real MPEG file */
     fwrite(endcode, 1, sizeof(endcode), f);
@@ -169,6 +169,7 @@ int main(int argc, char **argv)
 
     avcodec_free_context(&c);
     av_frame_free(&picture);
+    av_packet_free(&pkt);
 
     return 0;
 }



More information about the ffmpeg-cvslog mailing list