1

I'm now using Videotoolbox to deal with h.264 encoding.

And I found a sample code and it works fine :

    #define VTB_HEIGHT 480
    #define VTB_WIDTH 640

    int bitRate = VTB_WIDTH * VTB_HEIGHT * 3 * 4 * 8;

    CFNumberRef bitRateRef = CFNumberCreate(kCFAllocatorDefault,
                                            kCFNumberSInt32Type,
                                            &bitRate);

    VTSessionSetProperty(encodingSession,
                         kVTCompressionPropertyKey_AverageBitRate,
                         bitRateRef);

    CFRelease(bitRateRef);


    int bitRateLimit = bitRate / 8;

    CFNumberRef bitRateLimitRef = CFNumberCreate(kCFAllocatorDefault,
                                                 kCFNumberSInt32Type,
                                                 &bitRateLimit);

    VTSessionSetProperty(encodingSession,
                         kVTCompressionPropertyKey_DataRateLimits,
                         bitRateLimitRef);

    CFRelease(bitRateLimitRef);

But these two lines, I don't understand:

int bitRate = VTB_WIDTH * VTB_HEIGHT * 3 * 4 * 8;

int bitRateLimit = bitRate / 8;

What's the right way to use them?

hope someone can tell me.

Thanks for your time!

scchn
  • 315
  • 3
  • 16

1 Answers1

0

From the document of kvtcompressionpropertykey_dataratelimits say:

Each hard limit is described by a data size in bytes and a duration in seconds...

So you need set this property with 2 parameters(data size in bytes, duration in seconds)

int bitRate = VTB_WIDTH * VTB_HEIGHT * 3 * 4 * 8; 
int bitRateLimit = bitRate / 8; 

// that's say we set data in byte/second
CFNumberRef byteNum = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &bitRateLimit);
int second = 1;
CFNumberRef secNum = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &second);

// add parameters into a array
const void* numbers[2] = {byteNum, secNum};
CFArrayRef dataRateLimits = CFArrayCreate(NULL, numbers, 2, &kCFTypeArrayCallBacks);

// then set property with array
status = VTSessionSetProperty(compressionSession, kVTCompressionPropertyKey_DataRateLimits, arrayValues);
Will Tu
  • 60
  • 2
  • 9
  • Apple released a detailed explanation about [kVTCompressionPropertyKey_DataRateLimits](https://developer.apple.com/library/content/qa/qa1958/_index.html#//apple_ref/doc/uid/DTS40017665) property recently. – Will Tu Aug 22 '17 at 02:20