Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于cvpixelbuffer 一些数据的异常 #7

Open
yaoming04111 opened this issue Aug 23, 2021 · 0 comments
Open

关于cvpixelbuffer 一些数据的异常 #7

yaoming04111 opened this issue Aug 23, 2021 · 0 comments

Comments

@yaoming04111
Copy link

yaoming04111 commented Aug 23, 2021

目前我通过 didOutputVideoSampleBuffer 接口获取的 sampleBuffer 数据
将他转换成 nsdata 数据 然后在 把nsdata 数据 转换回 CVPixelBufferRef 的时候。设置的格式 为 960540 底部数据会有部分丢失
设置成 1920
1080 的时候也会有部分的丢失 但是会比 960*540的时候有所好转
转换代码如下:
`-(NSMutableData *) convertVideoSmapleBufferToYuvData:(CVImageBufferRef) pixelBuffer{

OSType pixeltype = CVPixelBufferGetPixelFormatType(pixelBuffer);
bool isPlanar = CVPixelBufferIsPlanar(pixelBuffer);
size_t planeCount = CVPixelBufferGetPlaneCount(pixelBuffer);
//表示开始操作数据
CVPixelBufferLockBaseAddress(pixelBuffer, 0);

//图像宽度(像素)
size_t pixelWidth = CVPixelBufferGetWidth(pixelBuffer);

// a_width = pixelWidth;
//图像高度(像素)
size_t pixelHeight = CVPixelBufferGetHeight(pixelBuffer);
// a_heigt = pixelHeight;
//yuv中的y所占字节数
size_t y_size = pixelWidth * pixelHeight;
//yuv中的uv所占的字节数
size_t uv_size = y_size / 2;

uint8_t *yuv_frame = (uint8_t *)malloc(uv_size + y_size);

//获取CVImageBufferRef中的y数据
uint8_t *y_frame = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);
memmove(yuv_frame, y_frame, y_size);

//获取CMVImageBufferRef中的uv数据
uint8_t *uv_frame = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1);
memmove(yuv_frame + y_size, uv_frame, uv_size);

CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);

//返回数据
return [NSMutableData dataWithBytesNoCopy:yuv_frame length:y_size + uv_size];

// [NSMutableData dataWithBytesNoCopy:yuv_frame length:y_size + uv_size];
}

-(CVPixelBufferRef)createCVPixelBufferRefFromNV12buffer:(unsigned char )buffer width:(int)w height:(int)h {
NSDictionary pixelAttributes = @{
(NSString
)kCVPixelBufferIOSurfacePropertiesKey:@{},
(NSString
)kCVPixelBufferOpenGLESCompatibilityKey:@yES
};
CVReturn result = 0;
if(_aaapixelBuffer == NULL){
result = CVPixelBufferCreate(kCFAllocatorDefault,
w,
h,// - 58
kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange,
(__bridge CFDictionaryRef)(pixelAttributes),
&_aaapixelBuffer);//kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange,
}
CVPixelBufferLockBaseAddress(_aaapixelBuffer,0);
unsigned char *yDestPlane = CVPixelBufferGetBaseAddressOfPlane(_aaapixelBuffer, 0);

// Here y_ch0 is Y-Plane of YUV(NV12) data.
unsigned char *y_ch0 = buffer;

memmove(yDestPlane, y_ch0, w * h);
unsigned char *uvDestPlane = CVPixelBufferGetBaseAddressOfPlane(_aaapixelBuffer, 1);

// Here y_ch1 is UV-Plane of YUV(NV12) data.
unsigned char *y_ch1 = buffer + w * h;
memmove(uvDestPlane, y_ch1, w * h/2);
CVPixelBufferUnlockBaseAddress(_aaapixelBuffer, 0);

if (result != kCVReturnSuccess) {
    NSLog(@"Unable to create cvpixelbuffer %d", result);
}
return _aaapixelBuffer;

}`

麻烦帮忙协助查看一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant