SDL 显示解码后的yuv12数据
在上篇h264解码为yuv12后http://jhlong12345.blog.163.com/blog/static/1230631292015725115058709/
,需要显示出来,使用sdl比较方便,网上没C#的例子,全是C++的,
SDL_LockYUVOverlay(overlay); memcpy(overlay->pixels[0], y_video_data, w * h); memcpy(overlay->pixels[1], u_video_data, w * h / 4); memcpy(overlay->pixels[2], v_video_data, w * h / 4); SDL_UnlockYUVOverlay(overlay);
这里贴出来研究很久才做出来的C#代码
if (H264Dec.Hi264DecAU(_decHandle, pH264Data, frameLen, 0, ref _decodeFrame, 0) == 0) { IntPtr[] ips = new IntPtr[3] { _decodeFrame.pY, _decodeFrame.pV,_decodeFrame.pU }; //U(Cb)分量 V(Cr)分量 object obj = Marshal.PtrToStructure(yuv, typeof(Sdl.SDL_Overlay)); //在I420格式中,U平面紧跟在Y平面之后,然后才是V平面(即:YUV);但YV12则是相反(即:YVU) Sdl.SDL_Overlay overlayYuv = (Sdl.SDL_Overlay)obj; Sdl.SDL_LockYUVOverlay(yuv); GCHandle hObject = GCHandle.Alloc(ips, GCHandleType.Pinned); IntPtr pObject = hObject.AddrOfPinnedObject(); overlayYuv.pixels = pObject; Marshal.StructureToPtr(overlayYuv, yuv, true); Sdl.SDL_UnlockYUVOverlay(yuv); Sdl.SDL_DisplayYUVOverlay(yuv, ref rect); //画点文字图形啥的 Sdl.SDL_Flip(surfacePtr); Sdl.SDL_UpdateRect(surfacePtr, 0, 0, rect.w, rect.h); if (hObject.IsAllocated) hObject.Free(); }
其中 IntPtr yuv = Sdl.SDL_CreateYUVOverlay(width, height, Sdl.SDL_YV12_OVERLAY, surfacePtr);