diff mbox series

[FFmpeg-devel,05/11] examples/remuxing: Don't use stack packet

Message ID AM7PR03MB666054F968C595079AC314BD8FCF9@AM7PR03MB6660.eurprd03.prod.outlook.com
State Accepted
Commit 53f374c08d5cc97158c17ea34b1c8ee0116c0578
Headers show
Series [FFmpeg-devel,01/11] avformat/mux: Sanitize packets without data and side-data
Related show

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Andreas Rheinhardt Sept. 3, 2021, 11:18 p.m. UTC
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 doc/examples/remuxing.c | 33 ++++++++++++++++++++-------------
 1 file changed, 20 insertions(+), 13 deletions(-)
diff mbox series

Patch

diff --git a/doc/examples/remuxing.c b/doc/examples/remuxing.c
index b23431567c..4e9354c535 100644
--- a/doc/examples/remuxing.c
+++ b/doc/examples/remuxing.c
@@ -47,7 +47,7 @@  int main(int argc, char **argv)
 {
     const AVOutputFormat *ofmt = NULL;
     AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
-    AVPacket pkt;
+    AVPacket *pkt = NULL;
     const char *in_filename, *out_filename;
     int ret, i;
     int stream_index = 0;
@@ -65,6 +65,12 @@  int main(int argc, char **argv)
     in_filename  = argv[1];
     out_filename = argv[2];
 
+    pkt = av_packet_alloc();
+    if (!pkt) {
+        fprintf(stderr, "Could not allocate AVPacket\n");
+        goto end;
+    }
+
     if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
         fprintf(stderr, "Could not open input file '%s'", in_filename);
         goto end;
@@ -140,27 +146,27 @@  int main(int argc, char **argv)
     while (1) {
         AVStream *in_stream, *out_stream;
 
-        ret = av_read_frame(ifmt_ctx, &pkt);
+        ret = av_read_frame(ifmt_ctx, pkt);
         if (ret < 0)
             break;
 
-        in_stream  = ifmt_ctx->streams[pkt.stream_index];
-        if (pkt.stream_index >= stream_mapping_size ||
-            stream_mapping[pkt.stream_index] < 0) {
-            av_packet_unref(&pkt);
+        in_stream  = ifmt_ctx->streams[pkt->stream_index];
+        if (pkt->stream_index >= stream_mapping_size ||
+            stream_mapping[pkt->stream_index] < 0) {
+            av_packet_unref(pkt);
             continue;
         }
 
-        pkt.stream_index = stream_mapping[pkt.stream_index];
-        out_stream = ofmt_ctx->streams[pkt.stream_index];
-        log_packet(ifmt_ctx, &pkt, "in");
+        pkt->stream_index = stream_mapping[pkt->stream_index];
+        out_stream = ofmt_ctx->streams[pkt->stream_index];
+        log_packet(ifmt_ctx, pkt, "in");
 
         /* copy packet */
-        av_packet_rescale_ts(&pkt, in_stream->time_base, out_stream->time_base);
-        pkt.pos = -1;
-        log_packet(ofmt_ctx, &pkt, "out");
+        av_packet_rescale_ts(pkt, in_stream->time_base, out_stream->time_base);
+        pkt->pos = -1;
+        log_packet(ofmt_ctx, pkt, "out");
 
-        ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
+        ret = av_interleaved_write_frame(ofmt_ctx, pkt);
         /* pkt is now blank (av_interleaved_write_frame() takes ownership of
          * its contents and resets pkt), so that no unreferencing is necessary.
          * This would be different if one used av_write_frame(). */
@@ -172,6 +178,7 @@  int main(int argc, char **argv)
 
     av_write_trailer(ofmt_ctx);
 end:
+    av_packet_free(&pkt);
 
     avformat_close_input(&ifmt_ctx);