diff mbox series

[FFmpeg-devel] avcodec/videotoolboxenc: add low-latency encoding

Message ID tencent_A5219EEC9C981C4615F0F96C883C89E57207@qq.com
State Accepted
Commit 5cd304298eddbdc88d8c3c7d1bbf71aa251b096c
Headers show
Series [FFmpeg-devel] avcodec/videotoolboxenc: add low-latency encoding | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished

Commit Message

徐福隆 May 19, 2023, 6:05 a.m. UTC
When using low-latency mode, it eliminates frame reordering
and follows a one-in-one-out encoding mode

Signed-off-by: xufuji456 <839789740@qq.com>
---
 libavcodec/videotoolboxenc.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
diff mbox series

Patch

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index c6f22723d6..e91d17c132 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -105,6 +105,7 @@  static struct{
 
     CFStringRef kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder;
     CFStringRef kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder;
+    CFStringRef kVTVideoEncoderSpecification_EnableLowLatencyRateControl;
 
     getParameterSetAtIndex CMVideoFormatDescriptionGetHEVCParameterSetAtIndex;
 } compat_keys;
@@ -171,6 +172,8 @@  static void loadVTEncSymbols(void){
             "EnableHardwareAcceleratedVideoEncoder");
     GET_SYM(kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder,
             "RequireHardwareAcceleratedVideoEncoder");
+    GET_SYM(kVTVideoEncoderSpecification_EnableLowLatencyRateControl,
+                "EnableLowLatencyRateControl");
 }
 
 typedef enum VT_H264Profile {
@@ -1441,6 +1444,17 @@  static int vtenc_create_encoder(AVCodecContext   *avctx,
         }
     }
 
+    // low-latency mode: eliminate frame reordering, follow a one-in-one-out encoding mode
+    if ((avctx->flags & AV_CODEC_FLAG_LOW_DELAY) && avctx->codec_id == AV_CODEC_ID_H264) {
+        status = VTSessionSetProperty(vtctx->session,
+                                      compat_keys.kVTVideoEncoderSpecification_EnableLowLatencyRateControl,
+                                      kCFBooleanTrue);
+
+        if (status) {
+            av_log(avctx, AV_LOG_ERROR, "Error setting low latency property: %d\n", status);
+        }
+    }
+
     status = VTCompressionSessionPrepareToEncodeFrames(vtctx->session);
     if (status) {
         av_log(avctx, AV_LOG_ERROR, "Error: cannot prepare encoder: %d\n", status);