基本信息
源码名称:C# WinForm 调用海思 H264 解码库进行解码 思解码库下载(包含库文件、头文件、文档以及官方测试程序和源码)
源码大小:0.82M
文件格式:.rar
开发语言:C#
更新时间:2013-07-08
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
海思解码库下载(包含库文件、头文件、文档以及官方测试程序和源码)
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 裸数据时,建议使用第二种方式。而对于读取文件进行本地播放时,建议使用第一种方式。
另外,需要注意海思解码库为商业软件,在解码输出时,画面中会包含海思公司水印,自己想办法去除吧。
: