NSMutableArray* _overlayDatas;
structSwsContext*nv12_420_ctx;
structSwsContext*rgb_420_ctx;
AVFrame*nv12_420_frame;
AVFrame* rgb_420_frame;
- (void)setupScaler:(SDL_VoutOverlay*) overlay
{
//初始化nv12 转 420p
nv12_420_frame = av_frame_alloc();
int res = av_image_alloc(nv12_420_frame->data, nv12_420_frame->linesize, overlay->w, overlay->h, AV_PIX_FMT_YUV420P, 1);
if(res <0) {
NSLog(@"失败");
}
// avpicture_alloc(&nv12_420_frame, AV_PIX_FMT_YUV420P, overlay->w, overlay->h);
staticintsws_flags = SWS_FAST_BILINEAR;
nv12_420_ctx = sws_getContext(overlay->w,
overlay->h,
AV_PIX_FMT_NV12,
overlay->w,
overlay->h,
AV_PIX_FMT_YUV420P,
sws_flags,NULL,NULL,NULL);
//初始化420p 转 rgb24
rgb_420_frame = av_frame_alloc();
int res1 = av_image_alloc(rgb_420_frame->data, rgb_420_frame->linesize, overlay->w, overlay->h, AV_PIX_FMT_RGB24, 1);
if(res1 <0) {
NSLog(@"失败");
}
// avpicture_alloc(&rgb_420_frame, AV_PIX_FMT_RGB24, overlay->w, overlay->h);
// return;
rgb_420_ctx = sws_getContext(overlay->w,
overlay->h,
AV_PIX_FMT_YUV420P,
overlay->w,
overlay->h,
AV_PIX_FMT_RGB24,
sws_flags,NULL,NULL,NULL);
}
- (void)convertFrameToRGB:(SDL_VoutOverlay*) overlay
{
intlinesize[3] = { overlay->pitches[0], overlay->pitches[1], overlay->pitches[2] };
sws_scale(nv12_420_ctx,
(constuint8_t**)overlay->pixels,
linesize,
0,
overlay->h,
nv12_420_frame->data,
nv12_420_frame->linesize);
// return;
sws_scale(rgb_420_ctx,
(constuint8_t**)nv12_420_frame->data,
nv12_420_frame->linesize,
0,
overlay->h,
rgb_420_frame->data,
rgb_420_frame->linesize);
}
- (GLuint) curTexture
{
if(_overlayDatas.count>0) {
NSValue* pixelValue = [_overlayDatasobjectAtIndex:0];
[_overlayDatas removeObjectAtIndex:0];
SDL_VoutOverlay* overlay = (SDL_VoutOverlay*)[pixelValuepointerValue];
if(!nv12_420_ctx) {
[selfsetupScaler:overlay];
}
[selfconvertFrameToRGB:overlay];
GLuinttexture =0;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB, overlay->w, overlay->h,0,GL_RGB,GL_UNSIGNED_BYTE, overlay->pixels[0]);
GLenumglError =glGetError();
if(GL_NO_ERROR!= glError) {
NSLog(@"glError %x\n", glError);
}
returntexture;
}
return 0;
}
- (void)releaseAVFrame {
NSLog(@"111111111111releaseAVFrame");
// av_frame_free(&nv12_420_frame);
sws_freeContext(nv12_420_ctx);
avpicture_free(&nv12_420_frame);
// av_frame_free(&rgb_420_frame);
avpicture_free(&rgb_420_frame);
sws_freeContext(rgb_420_ctx);
[_overlayDatas removeAllObjects];
_overlayDatas = nil;
}
- (void)display: (SDL_VoutOverlay*) overlay
{
dispatch_sync(dispatch_get_main_queue(), ^{
// 省略原来的代码
if(overlay !=NULL&& _overlayDatas.count<1) {
NSValue* overlayValue = [NSValuevalueWithPointer:overlay];
[_overlayDatasaddObject:overlayValue];
}
[self unlockGLActive];
});
}
网友评论