diff mbox series

[FFmpeg-devel,v2] lavc/libopenh264: Check for noopenh264

Message ID 20240329-noopenh264-v2-1-ec34a7223def@gmail.com
State New
Headers show
Series [FFmpeg-devel,v2] lavc/libopenh264: Check for noopenh264 | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Akihiko Odaki March 29, 2024, 6:49 a.m. UTC
noopenh264 is a "fake implementation of the OpenH264 library we can link
from regardless of the actual library being available":
https://gitlab.com/freedesktop-sdk/noopenh264

A distributor may wish to link against openh264/noopenh264 and let
the decoder and encoder work only if the actual library is available.

On the other hand, an application may want to know if the decoder or
encoder is available beforehand so that it can determine what format to
download for decoding, or what format to advertise for the peer
receiving the encoded video.

Check the availability of the actual library at runtime initialization
and do not expose the encoder and decoder if they are not available.

The availability check is done by calling WelsCreateDecoder() or
WelsCreateSVCEncoder(). These functions do not perform initialization
tasks other than allocating objects and assigning function pointers;
these tasks are performed by calling the Initialize() function pointer
member of the object these functions return. By *not* calling
Initialize(), we can ensure that potentially expensive and failable
initialization tasks are not unconditionally performed and avoid
preparing decoding/encoding parameters it requires.

Signed-off-by: Akihiko Odaki <akihiko.odaki@gmail.com>
---
Changes in v2:
- Changed to filter codecs with av_codec_iterate().
- Clarified potentially expensive and failable initialization tasks are
  not performed by WelsCreateDecoder() or WelsCreateSVCEncoder() but by
  the Initialize() function pointer member of what they return.
- Link to v1: https://lore.kernel.org/r/20240211-noopenh264-v1-1-e18090bdcfed@gmail.com
---
 libavcodec/allcodecs.c      |  5 +++++
 libavcodec/codec_internal.h |  4 ++++
 libavcodec/libopenh264dec.c | 14 +++++++++++++-
 libavcodec/libopenh264enc.c | 14 +++++++++++++-
 4 files changed, 35 insertions(+), 2 deletions(-)


---
base-commit: 8d1093a78413be6718a23dcdbed0b21cee7fcfe6
change-id: 20240210-noopenh264-650461efbc33

Best regards,
diff mbox series

Patch

diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 2386b450a6..73dd4ebd52 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -924,6 +924,11 @@  const AVCodec *av_codec_iterate(void **opaque)
     uintptr_t i = (uintptr_t)*opaque;
     const FFCodec *c = codec_list[i];
 
+    while (c && (c->caps_internal & FF_CODEC_CAP_DISABLED)) {
+        i++;
+        c = codec_list[i];
+    }
+
     ff_thread_once(&av_codec_static_init, av_codec_init_static);
 
     if (c) {
diff --git a/libavcodec/codec_internal.h b/libavcodec/codec_internal.h
index d6757e2def..a7f833af72 100644
--- a/libavcodec/codec_internal.h
+++ b/libavcodec/codec_internal.h
@@ -88,6 +88,10 @@ 
  * encoders do.
  */
 #define FF_CODEC_CAP_EOF_FLUSH              (1 << 10)
+/**
+ * Codec was disabled at runtime.
+ */
+#define FF_CODEC_CAP_DISABLED               (1 << 11)
 
 /**
  * FFCodec.codec_tags termination value
diff --git a/libavcodec/libopenh264dec.c b/libavcodec/libopenh264dec.c
index b6a9bba2dc..8778f490bf 100644
--- a/libavcodec/libopenh264dec.c
+++ b/libavcodec/libopenh264dec.c
@@ -48,6 +48,17 @@  static av_cold int svc_decode_close(AVCodecContext *avctx)
     return 0;
 }
 
+static av_cold void svc_decode_init_static_data(FFCodec *codec)
+{
+    ISVCDecoder *decoder;
+
+    if (WelsCreateDecoder(&decoder)) {
+        codec->caps_internal |= FF_CODEC_CAP_DISABLED;
+    } else {
+        WelsDestroyDecoder(decoder);
+    }
+}
+
 static av_cold int svc_decode_init(AVCodecContext *avctx)
 {
     SVCContext *s = avctx->priv_data;
@@ -153,12 +164,13 @@  static int svc_decode_frame(AVCodecContext *avctx, AVFrame *avframe,
     return avpkt->size;
 }
 
-const FFCodec ff_libopenh264_decoder = {
+FFCodec ff_libopenh264_decoder = {
     .p.name         = "libopenh264",
     CODEC_LONG_NAME("OpenH264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
     .p.type         = AVMEDIA_TYPE_VIDEO,
     .p.id           = AV_CODEC_ID_H264,
     .priv_data_size = sizeof(SVCContext),
+    .init_static_data = svc_decode_init_static_data,
     .init           = svc_decode_init,
     FF_CODEC_DECODE_CB(svc_decode_frame),
     .close          = svc_decode_close,
diff --git a/libavcodec/libopenh264enc.c b/libavcodec/libopenh264enc.c
index eef769eed0..6203d1176e 100644
--- a/libavcodec/libopenh264enc.c
+++ b/libavcodec/libopenh264enc.c
@@ -106,6 +106,17 @@  static av_cold int svc_encode_close(AVCodecContext *avctx)
     return 0;
 }
 
+static av_cold void svc_encode_init_static_data(FFCodec *codec)
+{
+    ISVCEncoder *encoder;
+
+    if (WelsCreateSVCEncoder(&encoder)) {
+        codec->caps_internal |= FF_CODEC_CAP_DISABLED;
+    } else {
+        WelsDestroySVCEncoder(encoder);
+    }
+}
+
 static av_cold int svc_encode_init(AVCodecContext *avctx)
 {
     SVCContext *s = avctx->priv_data;
@@ -429,7 +440,7 @@  static const FFCodecDefault svc_enc_defaults[] = {
     { NULL },
 };
 
-const FFCodec ff_libopenh264_encoder = {
+FFCodec ff_libopenh264_encoder = {
     .p.name         = "libopenh264",
     CODEC_LONG_NAME("OpenH264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
     .p.type         = AVMEDIA_TYPE_VIDEO,
@@ -437,6 +448,7 @@  const FFCodec ff_libopenh264_encoder = {
     .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_OTHER_THREADS |
                       AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
     .priv_data_size = sizeof(SVCContext),
+    .init_static_data = svc_encode_init_static_data,
     .init           = svc_encode_init,
     FF_CODEC_ENCODE_CB(svc_encode_frame),
     .close          = svc_encode_close,