在上篇h264解码为yuv12后http://jhlong12345.blog.163.com/blog/static/1230631292015725115058709/
,需要显示出来,使用sdl比较方便,网上没C#的例子,全是C++的,
SDL_LockYUVOverlay(overlay); memcpy(overlay->pixels[], y_video_data, w * h); memcpy(overlay->pixels[], u_video_data, w * h / ); memcpy(overlay->pixels[], v_video_data, w * h / ); SDL_UnlockYUVOverlay(overlay);
这里贴出来研究很久才做出来的C#代码
if (H264Dec.Hi264DecAU(_decHandle, pH264Data, frameLen, , ref _decodeFrame, ) == ) { IntPtr[] ips = new IntPtr[] { _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, , , rect.w, rect.h); if (hObject.IsAllocated) hObject.Free(); }
其中 IntPtr yuv = Sdl.SDL_CreateYUVOverlay(width, height, Sdl.SDL_YV12_OVERLAY, surfacePtr);