[FFmpeg-devel] [RFC PATCH] fftools/ffmpeg: make main function actually return

Zhao Zhili quinkblack at foxmail.com
Thu Dec 1 18:18:34 EET 2022


From: Zhao Zhili <zhilizhao at tencent.com>

---
When working on mediacodec encoder, I test it by making fftools/ffmpeg
into a lib, since JNI requires a JVM environment. There are three steps
mostly:
1. Rename main() function
2. Reset global variables
3. Make main() function actuall return

There is no point for this patch, except making fftools/ffmpeg a little
"lib friendly". And I understand fftools/ffmpeg isn't a lib by design.
But sometimes it's quick and easier to hack it.

 fftools/ffmpeg.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 771219f7df..4b12ad1ef8 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -100,6 +100,7 @@
 #include <conio.h>
 #endif
 
+#include <setjmp.h>
 #include <time.h>
 
 #include "ffmpeg.h"
@@ -491,10 +492,13 @@ static int decode_interrupt_cb(void *ctx)
 
 const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
 
+static jmp_buf exit_buf;
+
 static void ffmpeg_cleanup(int ret)
 {
     int i, j;
 
+    main_return_code = ret;
     if (do_benchmark) {
         int maxrss = getmaxrss() / 1024;
         av_log(NULL, AV_LOG_INFO, "bench: maxrss=%ikB\n", maxrss);
@@ -571,6 +575,7 @@ static void ffmpeg_cleanup(int ret)
     }
     term_exit();
     ffmpeg_exited = 1;
+    longjmp(exit_buf, 1);
 }
 
 /* iterate over all output streams in all output files;
@@ -3868,6 +3873,11 @@ int main(int argc, char **argv)
     int ret;
     BenchmarkTimeStamps ti;
 
+    if (setjmp(exit_buf)) {
+        // longjmp from ffmpeg_cleanup
+        return main_return_code;
+    }
+
     init_dynload();
 
     register_exit(ffmpeg_cleanup);
-- 
2.25.1



More information about the ffmpeg-devel mailing list