实例介绍
【实例简介】
海思解码库下载(包含库文件、头文件、文档以及官方测试程序和源码)
【实例截图】【核心代码】
P/Invoke 调用处理:
using System; using System.Runtime.InteropServices; namespace Demo { public class hi_h264dec { public const int HI_SUCCESS = 0; public const int HI_FAILURE = -1; public const int HI_LITTLE_ENDIAN = 1234; public const int HI_BIG_ENDIAN = 4321; public const int HI_DECODER_SLEEP_TIME = 60000; public const int HI_H264DEC_OK = 0; public const int HI_H264DEC_NEED_MORE_BITS = -1; public const int HI_H264DEC_NO_PICTURE = -2; public const int HI_H264DEC_ERR_HANDLE = -3; [DllImport("hi_h264dec_w.dll", EntryPoint = "Hi264DecImageEnhance", CallingConvention = CallingConvention.Cdecl)] public static extern int Hi264DecImageEnhance(IntPtr hDec, ref hiH264_DEC_FRAME_S pDecFrame, uint uEnhanceCoeff); [DllImport("hi_h264dec_w.dll", EntryPoint = "Hi264DecCreate", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr Hi264DecCreate(ref hiH264_DEC_ATTR_S pDecAttr); [DllImport("hi_h264dec_w.dll", EntryPoint = "Hi264DecDestroy", CallingConvention = CallingConvention.Cdecl)] public static extern void Hi264DecDestroy(IntPtr hDec); [DllImport("hi_h264dec_w.dll", EntryPoint = "Hi264DecGetInfo", CallingConvention = CallingConvention.Cdecl)] public static extern int Hi264DecGetInfo(ref hiH264_LIBINFO_S pLibInfo); [DllImport("hi_h264dec_w.dll", EntryPoint = "Hi264DecFrame", CallingConvention = CallingConvention.Cdecl)] public static extern int Hi264DecFrame(IntPtr hDec, IntPtr pStream, uint iStreamLen, ulong ullPTS, ref hiH264_DEC_FRAME_S pDecFrame, uint uFlags); [DllImport("hi_h264dec_w.dll", EntryPoint = "Hi264DecAU", CallingConvention = CallingConvention.Cdecl)] public static extern int Hi264DecAU(IntPtr hDec, IntPtr pStream, uint iStreamLen, ulong ullPTS, ref hiH264_DEC_FRAME_S pDecFrame, uint uFlags); [StructLayout(LayoutKind.Sequential)] public struct hiH264_DEC_ATTR_S { public uint uPictureFormat; public uint uStreamInType; public uint uPicWidthInMB; public uint uPicHeightInMB; public uint uBufNum; public uint uWorkMode; public IntPtr pUserData; public uint uReserved; } [StructLayout(LayoutKind.Sequential)] public struct hiH264_DEC_FRAME_S { public IntPtr pY; public IntPtr pU; public IntPtr pV; public uint uWidth; public uint uHeight; public uint uYStride; public uint uUVStride; public uint uCroppingLeftOffset; public uint uCroppingRightOffset; public uint uCroppingTopOffset; public uint uCroppingBottomOffset; public uint uDpbIdx; public uint uPicFlag; public uint bError; public uint bIntra; public ulong ullPTS; public uint uPictureID; public uint uReserved; public IntPtr pUserData; } [StructLayout(LayoutKind.Sequential)] public struct hiH264_LIBINFO_S { public uint uMajor; public uint uMinor; public uint uRelease; public uint uBuild; [MarshalAs(UnmanagedType.LPStr)] public string sVersion; [MarshalAs(UnmanagedType.LPStr)] public string sCopyRight; public uint uFunctionSet; public uint uPictureFormat; public uint uStreamInType; public uint uPicWidth; public uint uPicHeight; public uint uBufNum; public uint uReserved; } [StructLayout(LayoutKind.Sequential)] public struct hiH264_USERDATA_S { public uint uUserDataType; public uint uUserDataSize; public IntPtr pData; public IntPtr pNext; } } } View Code
解码的两种方式:
2.海思库解码分为两种方式:
第一种,输入分断的码流数据进行解码
第二种,输入一帧完整的码流数据进行解码
第一种方式的代码:
//初始化解码器,可以在 FormLoad 事件里完成 var decAttr = new hi_h264dec.hiH264_DEC_ATTR_S(); decAttr.uPictureFormat = 0; decAttr.uStreamInType = 0; decAttr.uPicWidthInMB = 480 >> 4; decAttr.uPicHeightInMB = 640 >> 4; decAttr.uBufNum = 8; decAttr.uWorkMode = 16; IntPtr _decHandle = hi_h264dec.Hi264DecCreate(ref decAttr); hi_h264dec.hiH264_DEC_FRAME_S _decodeFrame = new hi_h264dec.hiH264_DEC_FRAME_S(); //解码 //pData 为需要解码的 H264 nalu 数据,length 为该数据的长度 var resCode = hi_h264dec.Hi264DecFrame(_decHandle, pData, (uint)length, 0, ref _decodeFrame, 0); while (resCode != hi_h264dec.HI_H264DEC_NEED_MORE_BITS) { if (resCode == hi_h264dec.HI_H264DEC_OK) { if (_decodeFrame.bError == 0) { //计算 y u v 的长度 var yLength = _decodeFrame.uHeight * _decodeFrame.uYStride; var uLength = _decodeFrame.uHeight * _decodeFrame.uUVStride / 2; var vLength = uLength; var yBytes = new byte[yLength]; var uBytes = new byte[uLength]; var vBytes = new byte[vLength]; var decodedBytes = new byte[yLength uLength vLength]; //_decodeFrame 是解码后的数据对象,里面包含 YUV 数据、宽度、高度等信息 Marshal.Copy(_decodeFrame.pY, yBytes, 0, (int)yLength); Marshal.Copy(_decodeFrame.pU, uBytes, 0, (int)uLength); Marshal.Copy(_decodeFrame.pV, vBytes, 0, (int)vLength); //将从 _decodeFrame 中取出的 YUV 数据放入 decodedBytes 中 Array.Copy(yBytes, decodedBytes, yLength); Array.Copy(uBytes, 0, decodedBytes, yLength, uLength); Array.Copy(vBytes, 0, decodedBytes, yLength uLength, vLength); //decodedBytes 为yuv数据,可以将其转换为 RGB 数据后再转换为 BitMap 然后通过 PictureBox 控件即可显示 //这类代码网上比较常见,我就不贴了 } } resCode = hi_h264dec.Hi264DecFrame(_decHandle, IntPtr.Zero, 0, 0, ref _decodeFrame, 0); } //当所有解码操作完成后需要释放解码库,可以放在 FormClosing 事件里做 hi_h264dec.Hi264DecDestroy(_decHandle); View Code
第二种:
//初始化解码器,可以在 FormLoad 事件里完成 var decAttr = new hi_h264dec.hiH264_DEC_ATTR_S(); decAttr.uPictureFormat = 0; decAttr.uStreamInType = 0; decAttr.uPicWidthInMB = 480 >> 4; decAttr.uPicHeightInMB = 640 >> 4; decAttr.uBufNum = 8; decAttr.uWorkMode = 16; IntPtr _decHandle = hi_h264dec.Hi264DecCreate(ref decAttr); hi_h264dec.hiH264_DEC_FRAME_S _decodeFrame = new hi_h264dec.hiH264_DEC_FRAME_S(); //解码 //pData 为需要解码的 H264 nalu 数据,length 为该数据的长度 if (hi_h264dec.Hi264DecAU(_decHandle, pData, (uint) length, 0, ref _decodeFrame, 0) == 0) { if (_decodeFrame.bError == 0) { //计算 y u v 的长度 var yLength = _decodeFrame.uHeight * _decodeFrame.uYStride; var uLength = _decodeFrame.uHeight * _decodeFrame.uUVStride / 2; var vLength = uLength; var yBytes = new byte[yLength]; var uBytes = new byte[uLength]; var vBytes = new byte[vLength]; var decodedBytes = new byte[yLength uLength vLength]; //_decodeFrame 是解码后的数据对象,里面包含 YUV 数据、宽度、高度等信息 Marshal.Copy(_decodeFrame.pY, yBytes, 0, (int)yLength); Marshal.Copy(_decodeFrame.pU, uBytes, 0, (int)uLength); Marshal.Copy(_decodeFrame.pV, vBytes, 0, (int)vLength); //将从 _decodeFrame 中取出的 YUV 数据放入 decodedBytes 中 Array.Copy(yBytes, decodedBytes, yLength); Array.Copy(uBytes, 0, decodedBytes, yLength, uLength); Array.Copy(vBytes, 0, decodedBytes, yLength uLength, vLength); //decodedBytes 为yuv数据,可以将其转换为 RGB 数据后再转换为 BitMap 然后通过 PictureBox 控件即可显示 //这类代码网上比较常见,我就不贴了 } } //当所有解码操作完成后需要释放解码库,可以放在 FormClosing 事件里做 hi_h264dec.Hi264DecDestroy(_decHandle); View Code
对于在设备 SDK 回调里每次都能获取到一帧完整的 H264 裸数据时,建议使用第二种方式。而对于读取文件进行本地播放时,建议使用第一种方式。
另外,需要注意海思解码库为商业软件,在解码输出时,画面中会包含海思公司水印,自己想办法去除吧。
:标签: 文件
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)