diff mbox

[FFmpeg-devel] avcodec/videotoolbox: add rc_max_bitrate control into videotoolbox

Message ID 20170313035504.15060-1-lq@chinaffmpeg.org
State Superseded
Headers show

Commit Message

Steven Liu March 13, 2017, 3:55 a.m. UTC
add kVTCompressionPropertyKey_DataRateLimits support by rc_max_bitrate

Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
---
 libavcodec/videotoolboxenc.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
diff mbox

Patch

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 005f5d6..9738152 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -898,6 +898,7 @@  static int vtenc_create_encoder(AVCodecContext   *avctx,
 {
     VTEncContext *vtctx = avctx->priv_data;
     SInt32       bit_rate = avctx->bit_rate;
+    SInt32       max_rate = avctx->rc_max_rate;
     CFNumberRef  bit_rate_num;
 
     int status = VTCompressionSessionCreate(kCFAllocatorDefault,
@@ -938,6 +939,32 @@  static int vtenc_create_encoder(AVCodecContext   *avctx,
         return AVERROR_EXTERNAL;
     }
 
+    int64_t bytes_per_second_value = max_rate >> 3;
+    CFNumberRef bytes_per_second = CFNumberCreate(kCFAllocatorDefault,
+                                                  kCFNumberSInt64Type,
+                                                  &bytes_per_second_value);
+    int64_t two_second_value = 2;
+    CFNumberRef two_second = CFNumberCreate(kCFAllocatorDefault,
+                                            kCFNumberSInt64Type,
+                                            &two_second_value);
+    const void* nums[2] = { bytes_per_second, two_second };
+    CFArrayRef data_rate_limits = CFArrayCreate(kCFAllocatorDefault,
+                                                nums,
+                                                2,
+                                                &kCFTypeArrayCallBacks);
+
+    status = VTSessionSetProperty((VTCompressionSessionRef)(vtctx->session),
+                                  kVTCompressionPropertyKey_DataRateLimits,
+                                  data_rate_limits);
+    CFRelease(bytes_per_second);
+    CFRelease(two_second);
+    CFRelease(data_rate_limits);
+
+    if (status) {
+        av_log(avctx, AV_LOG_ERROR, "Error setting max bitrate property: %d\n", status);
+        return AVERROR_EXTERNAL;
+    }
+
     if (profile_level) {
         status = VTSessionSetProperty(vtctx->session,
                                       kVTCompressionPropertyKey_ProfileLevel,