日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

H.264编码器在需要时强制插入关键帧

發布時間:2023/12/29 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 H.264编码器在需要时强制插入关键帧 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

廢話不多說,直接上關鍵部分的代碼

-(void)encode:(CMSampleBufferRef)sampleBuffer isCrop:(BOOL)isCrop {log4cplus_debug("h264", "encoder video data\n");if (self.error) {log4cplus_error("h264", "error = %d",self.error);}if(self.error == Error_Restart5TimesStillFailured){log4cplus_debug("h264", "%s,restart encoder more than 5 times, %d",__func__,__LINE__);[self.delegate didRestartFailured];}[m_lock lock];if(self.isReadyForEncode){if(compressionSession == NULL){log4cplus_error("h264", "%s,compressionSession =null, %d",__func__,__LINE__);goto RELEASE_BUFFER;}//the first frame must be iframe then create the reference timeStamp;static BOOL isFirstFrame = YES;if(isFirstFrame){CMTime pts = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);log4cplus_info("h264","g_vstarttime = %f",CMTimeGetSeconds(pts));g_vstarttime = CMTimeGetSeconds(pts);// system absolutly time(s)g_tvustartcaptureTime = CTimeThread::GetCurrentTime();if(ntp_time_offset != 0){g_tvustartcaptureTime = g_tvustartcaptureTime - (ntp_time_offset/1000);}log4cplus_error("h264","CAPTURETIME = %u, %ld",g_tvustartcaptureTime, ntp_time_offset);isFirstFrame = NO;}if(initialized == false){log4cplus_error("h264", "h264 encoder is not ready\n");goto RELEASE_BUFFER;}[self doSetBitrate];CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);// 關鍵步驟 begin OSStatus status = noErr; if (g_needForceInsertKeyFrame) {NSDictionary *properties = @{(__bridge NSString *)kVTEncodeFrameOptionKey_ForceKeyFrame:@YES};status = VTCompressionSessionEncodeFrame(compressionSession, imageBuffer, presentationTimeStamp, kCMTimeInvalid, (__bridge CFDictionaryRef)properties, NULL, NULL);// 強制插入關鍵幀 (__bridge CFDictionaryRef)properties 該參數必須設置為YES,并在編碼時傳入,就能立即插入一幀關鍵幀g_needForceInsertKeyFrame = false;}else {status = VTCompressionSessionEncodeFrame(compressionSession, imageBuffer, presentationTimeStamp, kCMTimeInvalid, NULL, NULL, NULL);}// 關鍵步驟 end //NSAssert(status == noErr, @"assert : Encoder frame failured!");if(status != noErr){NSLog(@"H264: VTCompressionSessionEncodeFrame failed");VTCompressionSessionCompleteFrames(compressionSession, kCMTimeInvalid);VTCompressionSessionInvalidate(compressionSession);CFRelease(compressionSession);compressionSession = NULL;_error = Error_EncodeFrameFailured;[m_lock unlock];if (isCrop) {if (sampleBuffer != NULL) {CFRelease(sampleBuffer);log4cplus_debug("h264", "Release crop sample buffer");}}[self.delegate didEncoderErrorOccured:Error_EncodeFrameFailured];return;}}// if sample buffer are from system needn't to release, if sample buffer are from we create need to release. RELEASE_BUFFER:[m_lock unlock];if (isCrop) {if (sampleBuffer != NULL) {CFRelease(sampleBuffer);log4cplus_debug("h264", "Release crop sample buffer");}} }

總結

以上是生活随笔為你收集整理的H.264编码器在需要时强制插入关键帧的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。