iOS17 VideoToolBox video encode h264 file set average bitrate not work?

I use VTCompressionSession and set the average bit rate by kVTCompressionPropertyKey_AverageBitRate and kVTCompressionPropertyKey_DataRateLimits. The code like this:

    
    VTCompressionSessionRef vtSession = session;
    if (vtSession == NULL) {
        vtSession = _encoderSession;
    }
    
    if (vtSession == nil) {
        return;
    }
    
    int tmp         = bitrate;
    int bytesTmp    = tmp * 0.15; 
    int durationTmp = 1;
    
    CFNumberRef bitrateRef   = CFNumberCreate(NULL, kCFNumberSInt32Type, &tmp);
    CFNumberRef bytes        = CFNumberCreate(NULL, kCFNumberSInt32Type, &bytesTmp);
    CFNumberRef duration     = CFNumberCreate(NULL, kCFNumberSInt32Type, &durationTmp);

    if ([self isSupportPropertyWithSession:vtSession key:kVTCompressionPropertyKey_AverageBitRate]) {
        [self setSessionPropertyWithSession:vtSession key:kVTCompressionPropertyKey_AverageBitRate value:bitrateRef];
    }else {
        NSLog(@"Video Encoder: set average bitRate error");
    }
    
    NSLog(@"Video Encoder: set bitrate bytes = %d, _bitrate = %d",bytesTmp, bitrate);
    
    CFMutableArrayRef limit = CFArrayCreateMutable(NULL, 2, &kCFTypeArrayCallBacks);
    CFArrayAppendValue(limit, bytes);
    CFArrayAppendValue(limit, duration);
    if([self isSupportPropertyWithSession:vtSession key:kVTCompressionPropertyKey_DataRateLimits]) {
        OSStatus ret = VTSessionSetProperty(vtSession, kVTCompressionPropertyKey_DataRateLimits, limit);
        if(ret != noErr){
            NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:ret userInfo:nil];
            NSLog(@"Video Encoder: set DataRateLimits failed with %s",error.description.UTF8String);
        }
    }else {
        NSLog(@"Video Encoder: set data rate limits error");
    }
    CFRelease(bitrateRef);
    CFRelease(limit);
    CFRelease(bytes);
    CFRelease(duration);
}

This work fine on iOS16 and below. But on iOS17 the bitrate of the generate video file is much lower than the value I set. For exmaple, I set biterate 600k but on iOS17 the encoded video bitrate is lower than 150k. What went wrong?

Answered by Elvis-su in 771319022

already solved. no related to the api. event thing works fine!

Accepted Answer

already solved. no related to the api. event thing works fine!

I'm experiencing a similar situation, may I ask if it's not API related, what kind of problem is it related to?

I used CMSampleBufferCreateForImageBuffer to create CMSampleBufferRef from CVPixelBufferRef. Then use VTCompressionSessionEncodeFrame to encode CMSampleBufferRef. This may cause the LOW bitrate on iOS17.
So I changed the way, directly encode the CVPixelBufferRef instead of CMSampleBufferRef.

    OSStatus status = VTCompressionSessionEncodeFrame(compressionSession, **pixelBuffer,** kCMTimeInvalid, kCMTimeInvalid, (__bridge CFDictionaryRef)properties, (__bridge_retained void *)timeNumber, NULL);

then the encoded file has the desired bitrate.

iOS17 VideoToolBox video encode h264 file set average bitrate not work?
 
 
Q