[FFmpeg-devel] [PATCH] doc/examples: unify approach for get_format on hwaccel related examples

Leandro Santiago leandrosansilva at gmail.com
Thu Oct 31 22:44:25 EET 2024


This adds lots of copy&paste, but introducing ways to reuse the code would
find the examples harder to read.

Signed-off-by: Leandro Santiago <leandrosansilva at gmail.com>
---
 doc/examples/hw_decode.c       | 10 ++++------
 doc/examples/qsv_decode.c      | 11 ++++-------
 doc/examples/qsv_transcode.c   | 11 ++++-------
 doc/examples/vaapi_transcode.c | 10 ++++------
 4 files changed, 16 insertions(+), 26 deletions(-)

diff --git a/doc/examples/hw_decode.c b/doc/examples/hw_decode.c
index ac4e445505..dc40c864a2 100644
--- a/doc/examples/hw_decode.c
+++ b/doc/examples/hw_decode.c
@@ -60,17 +60,15 @@ static int hw_decoder_init(AVCodecContext *ctx, const enum AVHWDeviceType type)
     return err;
 }
 
-static enum AVPixelFormat get_hw_format(AVCodecContext *ctx,
-                                        const enum AVPixelFormat *pix_fmts)
+static enum AVPixelFormat get_format(AVCodecContext *ctx, const enum AVPixelFormat *p)
 {
-    const enum AVPixelFormat *p;
-
-    for (p = pix_fmts; *p != -1; p++) {
+    for (; *p != AV_PIX_FMT_NONE; p++) {
         if (*p == hw_pix_fmt)
             return *p;
     }
 
     fprintf(stderr, "Failed to get HW surface format.\n");
+
     return AV_PIX_FMT_NONE;
 }
 
@@ -218,7 +216,7 @@ int main(int argc, char *argv[])
     if (avcodec_parameters_to_context(decoder_ctx, video->codecpar) < 0)
         return -1;
 
-    decoder_ctx->get_format  = get_hw_format;
+    decoder_ctx->get_format  = get_format;
 
     if (hw_decoder_init(decoder_ctx, type) < 0)
         return -1;
diff --git a/doc/examples/qsv_decode.c b/doc/examples/qsv_decode.c
index 5a6f3625aa..9f5ddf31a7 100644
--- a/doc/examples/qsv_decode.c
+++ b/doc/examples/qsv_decode.c
@@ -41,14 +41,11 @@
 #include <libavutil/hwcontext_qsv.h>
 #include <libavutil/mem.h>
 
-static int get_format(AVCodecContext *avctx, const enum AVPixelFormat *pix_fmts)
+static enum AVPixelFormat get_format(AVCodecContext *avctx, const enum AVPixelFormat *p)
 {
-    while (*pix_fmts != AV_PIX_FMT_NONE) {
-        if (*pix_fmts == AV_PIX_FMT_QSV) {
-            return AV_PIX_FMT_QSV;
-        }
-
-        pix_fmts++;
+    for (; *p != AV_PIX_FMT_NONE; p++) {
+        if (*p == AV_PIX_FMT_QSV)
+            return *p;
     }
 
     fprintf(stderr, "The QSV pixel format not offered in get_format()\n");
diff --git a/doc/examples/qsv_transcode.c b/doc/examples/qsv_transcode.c
index 665a76af2e..d8b959967a 100644
--- a/doc/examples/qsv_transcode.c
+++ b/doc/examples/qsv_transcode.c
@@ -115,14 +115,11 @@ fail:
     return ret;
 }
 
-static int get_format(AVCodecContext *avctx, const enum AVPixelFormat *pix_fmts)
+static enum AVPixelFormat get_format(AVCodecContext *avctx, const enum AVPixelFormat *p)
 {
-    while (*pix_fmts != AV_PIX_FMT_NONE) {
-        if (*pix_fmts == AV_PIX_FMT_QSV) {
-            return AV_PIX_FMT_QSV;
-        }
-
-        pix_fmts++;
+    for (; *p != AV_PIX_FMT_NONE; p++) {
+        if (*p == AV_PIX_FMT_QSV)
+            return *p;
     }
 
     fprintf(stderr, "The QSV pixel format not offered in get_format()\n");
diff --git a/doc/examples/vaapi_transcode.c b/doc/examples/vaapi_transcode.c
index e1b7a43883..06358624a3 100644
--- a/doc/examples/vaapi_transcode.c
+++ b/doc/examples/vaapi_transcode.c
@@ -42,17 +42,15 @@ static int video_stream = -1;
 static AVStream *ost;
 static int initialized = 0;
 
-static enum AVPixelFormat get_vaapi_format(AVCodecContext *ctx,
-                                           const enum AVPixelFormat *pix_fmts)
+static enum AVPixelFormat get_format(AVCodecContext *ctx, const enum AVPixelFormat *p)
 {
-    const enum AVPixelFormat *p;
-
-    for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
+    for (; *p != AV_PIX_FMT_NONE; p++) {
         if (*p == AV_PIX_FMT_VAAPI)
             return *p;
     }
 
     fprintf(stderr, "Unable to decode this file using VA-API.\n");
+
     return AV_PIX_FMT_NONE;
 }
 
@@ -97,7 +95,7 @@ static int open_input_file(const char *filename)
         fprintf(stderr, "A hardware device reference create failed.\n");
         return AVERROR(ENOMEM);
     }
-    decoder_ctx->get_format    = get_vaapi_format;
+    decoder_ctx->get_format    = get_format;
 
     if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0)
         fprintf(stderr, "Failed to open codec for decoding. Error code: %s\n",
-- 
2.46.1



More information about the ffmpeg-devel mailing list