在开发过程中,由于项目需求,需要把解码后的视频转成YUV进行绘制,后发iPhonese2的手机出现绘制YUV花屏,其他手机能够正常绘制。经过详细的排查,发现主要是因为解码的一个参数和解析YUV的一个参数没对应上导致,代码如下:
视频解码初始化代码如下:
NSDictionary*defaultAttributes = @{(NSString *)kCVPixelBufferPixelFormatTypeKey:@(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange),(NSString *)kCVPixelBufferIOSurfacePropertiesKey:@{},(NSString *)kCVPixelBufferOpenGLCompatibilityKey:@(YES),(NSString *)kCVPixelBufferMetalCompatibilityKey:@(YES)};
VTDecompressionOutputCallbackRecord callBackRecord; callBackRecord.decompressionOutputCallback = lin_didDecompress;
callBackRecord.decompressionOutputRefCon= (__bridgevoid*)self;
status =VTDecompressionSessionCreate(kCFAllocatorDefault, _decoderFormatDescription,NULL,(__bridgeCFDictionaryRef)defaultAttributes,&callBackRecord,&_decoderSession);
YUV转换如下
-(CVPixelBufferRef)createCVPixelBufferRefFromNV12Data:(NSData *)data width:(int)w height:(int)h {
unsignedchar*buffer = data.bytes;
NSDictionary *pixelAttributes = @{(NSString*)kCVPixelBufferIOSurfacePropertiesKey:@{}}; CVPixelBufferRefpixelBuffer =NULL;
CVReturn result = CVPixelBufferCreate(kCFAllocatorDefault, w, h,kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange,(__bridgeCFDictionaryRef)(pixelAttributes), &pixelBuffer);//kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange,
以上代码经过详细排查,主要是因为解码器和YUV转化器中的kCVPixelFormatType_420YpCbCr8BiPlanarFullRange和kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange两个参数没对应上导致,目前猜测是其他手机解码能力比较强,导致这两个参数不一致还能解析出来。把YUV转换器中的kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange改成kCVPixelFormatType_420YpCbCr8BiPlanarFullRange即可正常使用
网友评论