使用ffmpeg实现对h264视频解码 -- (实现了一个易于使用的c++封装库)
H264是当今流行的视频压缩格式;ffmpeg是一个开源库,实现了对h264视频文件的解压缩。
为了降低使用ffmpeg的复杂性,尽量隐藏实现细节,我写了一个封装库。c#也可以很方便的使用此库。解压后的数据可以为yuv格式,也可以为rgb格式。同时可以对rgb格式视频做缩放。
类H264Decode实现了所有解码功能。最后,再把此类封装成c函数,以便于与c#交互。
代码下载地址 https://download.csdn.net/download/qq_29939347/10674597
下面讲述一下此类的实现细节:
1 BOOL H264Decode::Init()
初始化解码库,打开h264解码器。
BOOL H264Decode::Init()
{
if (_init)
return TRUE; _init = TRUE;
if (!_av_register)
{
_av_register = TRUE;
av_register_all();
} _pCodecContext = avcodec_alloc_context3(NULL);
_pH264VideoDecoder = avcodec_find_decoder(AV_CODEC_ID_H264);
if (_pH264VideoDecoder == NULL)
{
return FALSE;
} //初始化参数,下面的参数应该由具体的业务决定 AV_PIX_FMT_YUV420P;
_pCodecContext->time_base.num = ;
_pCodecContext->frame_number = ; //每包一个视频帧
_pCodecContext->codec_type = AVMEDIA_TYPE_VIDEO;
_pCodecContext->bit_rate = ;
_pCodecContext->time_base.den = ;//帧率
_pCodecContext->width = ;//视频宽
_pCodecContext->height = ;//视频高
_pCodecContext->pix_fmt = AV_PIX_FMT_YUVJ420P;
_pCodecContext->color_range = AVCOL_RANGE_MPEG; if (avcodec_open2(_pCodecContext, _pH264VideoDecoder, NULL) >= )
_pFrameYuv = av_frame_alloc();
else
return FALSE; return TRUE;
}
2 向解码器输入数据
INT32 H264Decode::H264_PutVideoStream(char* buffer, INT32 bufferLen)
{
AVPacket packet = { };
packet.data = (uint8_t*)buffer; //这里填入一个指向完整H264数据帧的指针
packet.size = bufferLen; //这个填入H264数据帧的大小 int ret = avcodec_send_packet(_pCodecContext, &packet);
return ret;
}
3 读取解压后的数据 yuv格式
INT32 H264Decode::H264_GetNextVideoFrame(char* buffer, INT32 bufferLen, INT32 yuFormate)
{
if (avcodec_receive_frame(_pCodecContext, _pFrameYuv) == )
{
int height = _pCodecContext->height;
int width = _pCodecContext->width; if (yuFormate == )
{
////写入数据
int yLen = height * width;
memcpy(buffer, _pFrameYuv->data[], yLen); int uLen = yLen / ;
memcpy(buffer + yLen, _pFrameYuv->data[], uLen); int vLen = uLen;
memcpy(buffer + yLen + uLen, _pFrameYuv->data[], vLen);
return ;
}
else
{
////写入数据
int yLen = height * width;
memcpy(buffer, _pFrameYuv->data[], yLen); int uLen = yLen / ;
memcpy(buffer + yLen, _pFrameYuv->data[], uLen); int vLen = uLen;
memcpy(buffer + yLen + uLen, _pFrameYuv->data[], vLen);
return ;
}
}
return -;
}
4 获取RGB格式视频;可同时,根据需要的大小,对视频做缩放。
INT32 H264Decode::H264_GetNextVideoFrame_Rgb(char* buffer, INT32 bufferLen, INT32 width, INT32 height)
{
if (avcodec_receive_frame(_pCodecContext, _pFrameYuv) == )
{
ResetRgbScale(width, height); int n = (_out_rgb_buffer_len == bufferLen); uint8_t * data[];
data[] = _pFrameYuv->data[];
data[] = _pFrameYuv->data[]; //u v 向量互换
data[] = _pFrameYuv->data[]; _dst_dataTmp[] = (uint8_t *)buffer; //少一次复制
int ret = sws_scale(_img_convert_ctx, (const unsigned char* const*)data, _pFrameYuv->linesize, , _pCodecContext->height,
_dst_dataTmp, _dst_linesize);
return ;
}
return -;
}
C语言封装接口
extern "C"
{
LibFfmpegWrapper_API INT64 H264_CreateHandle();
LibFfmpegWrapper_API INT32 H264_CloseHandle(INT64 handle); LibFfmpegWrapper_API INT32 H264_SetDefaultAlgorithm(INT32 flag);
LibFfmpegWrapper_API INT32 H264_SetAlgorithm(INT64 handle, INT32 flag); LibFfmpegWrapper_API INT32 H264_PutVideoStream(INT64 handle, char* buffer, INT32 bufferLen);
LibFfmpegWrapper_API INT32 H264_GetVideoParam(INT64 handle, INT32& width, INT32& height); LibFfmpegWrapper_API INT32 H264_GetVideoFrameSize(INT64 handle);
LibFfmpegWrapper_API INT32 H264_GetVideoFrameFormate(INT64 handle); LibFfmpegWrapper_API INT32 H264_GetNextVideoFrame(INT64 handle, char* buffer, INT32 bufferLen, INT32 yuFormate); LibFfmpegWrapper_API INT32 H264_GetVideoFrameSize_Rgb(INT64 handle);
LibFfmpegWrapper_API INT32 H264_GetNextVideoFrame_Rgb(INT64 handle, char* buffer, INT32 bufferLen); LibFfmpegWrapper_API INT32 H264_GetVideoFrameSize_Rgb2(INT64 handle,INT32 width, INT32 height);
LibFfmpegWrapper_API INT32 H264_GetNextVideoFrame_Rgb2(INT64 handle, char* buffer, INT32 bufferLen, INT32 width, INT32 height);
}
c# 调用封装
class H264Decode
{
public const int Decode_Result_OK = ; long _handleDecode = ; public static void SetDefaultAlgorithm(EN_H264Algorithm flag)
{
H264DecodeWrapper.SetDefaultAlgorithm((int)flag);
} ~H264Decode()
{
Close();
} public void Init()
{
_handleDecode = H264DecodeWrapper.CreateHandle();
} public Int32 SetAlgorithm(EN_H264Algorithm flag)
{
return H264DecodeWrapper.SetAlgorithm(_handleDecode, (int)flag);
} public void Close()
{
if (_handleDecode == )
return;
H264DecodeWrapper.CloseHandle(_handleDecode);
_handleDecode = ;
} public int PutVideoStream(byte[] buffer)
{
return PutVideoStream(buffer, buffer.Length);
} public int PutVideoStream(byte[] buffer, Int32 bufferLen)
{
return H264DecodeWrapper.PutVideoStream(_handleDecode, buffer, bufferLen);
} public int GetVideoOrgSize(out int width, out int height)
{
int result = H264DecodeWrapper.GetVideoParam(_handleDecode, out width, out height);
return result;
} public int GetVideoFrameSize()
{
int result = H264DecodeWrapper.GetVideoFrameSize(_handleDecode);
return result;
}
public AVPixelFormat GetVideoFrameFormate()
{
int result = H264DecodeWrapper.GetVideoFrameFormate(_handleDecode);
return (AVPixelFormat)result;
} public int GetNextVideoFrame(byte[] buffer, Int32 bufferLen, EN_H264_YU_Formate formate)
{
int result = H264DecodeWrapper.GetNextVideoFrame(_handleDecode, buffer, bufferLen, formate);
return result;
}
public int GetNextVideoFrame(byte[] buffer, EN_H264_YU_Formate formate)
{
int result = H264DecodeWrapper.GetNextVideoFrame(_handleDecode, buffer, buffer.Length, formate);
return result;
} public int GetVideoFrameSize_Rgb()
{
int result = H264DecodeWrapper.GetVideoFrameSize_Rgb(_handleDecode);
return result;
} public int GetNextVideoFrame_Rgb(byte[] buffer, Int32 bufferLen)
{
int result = H264DecodeWrapper.GetNextVideoFrame_Rgb(_handleDecode, buffer, bufferLen);
return result;
} public int GetVideoFrameSize_Rgb2(Int32 width, Int32 height)
{
int result = H264DecodeWrapper.GetVideoFrameSize_Rgb2(_handleDecode, width, height);
return result;
} public int GetNextVideoFrame_Rgb2(byte[] buffer, Int32 bufferLen, Int32 width, Int32 height)
{
int result = H264DecodeWrapper.GetNextVideoFrame_Rgb2(_handleDecode, buffer, bufferLen, width, height);
return result;
} } public enum EN_H264Algorithm
{
SWS_FAST_BILINEAR = ,
SWS_BILINEAR = ,
SWS_BICUBIC = ,
SWS_X = ,
SWS_POINT = 0x10,
SWS_AREA = 0x20,
SWS_BICUBLIN = 0x40,
SWS_GAUSS = 0x80,
SWS_SINC = 0x100,
SWS_LANCZOS = 0x200,
SWS_SPLINE = 0x400,
} public class H264DecodeWrapper
{
private const string DLLName = "LibFfmpegWrapper.dll"; [DllImport(DLLName, EntryPoint = "H264_CreateHandle", CallingConvention = CallingConvention.Cdecl)]
private static extern long H264_CreateHandle(); [DllImport(DLLName, EntryPoint = "H264_CloseHandle", CallingConvention = CallingConvention.Cdecl)]
private static extern Int32 H264_CloseHandle(long handle); [DllImport(DLLName, EntryPoint = "H264_PutVideoStream", CallingConvention = CallingConvention.Cdecl)]
private static extern Int32 H264_PutVideoStream(long handle, IntPtr buffer, Int32 bufferLen); [DllImport(DLLName, EntryPoint = "H264_GetVideoParam", CallingConvention = CallingConvention.Cdecl)]
private static extern Int32 H264_GetVideoParam(long handle, IntPtr width, IntPtr height); [DllImport(DLLName, EntryPoint = "H264_GetVideoFrameSize", CallingConvention = CallingConvention.Cdecl)]
private static extern Int32 H264_GetVideoFrameSize(long handle); [DllImport(DLLName, EntryPoint = "H264_GetVideoFrameFormate", CallingConvention = CallingConvention.Cdecl)]
private static extern Int32 H264_GetVideoFrameFormate(long handle); [DllImport(DLLName, EntryPoint = "H264_GetNextVideoFrame", CallingConvention = CallingConvention.Cdecl)]
private static extern Int32 H264_GetNextVideoFrame(long handle, IntPtr buffer, Int32 bufferLen, Int32 yuFormate); [DllImport(DLLName, EntryPoint = "H264_GetVideoFrameSize_Rgb", CallingConvention = CallingConvention.Cdecl)]
private static extern Int32 H264_GetVideoFrameSize_Rgb(long handle); [DllImport(DLLName, EntryPoint = "H264_GetNextVideoFrame_Rgb", CallingConvention = CallingConvention.Cdecl)]
private static extern Int32 H264_GetNextVideoFrame_Rgb(long handle, IntPtr buffer, Int32 bufferLen); [DllImport(DLLName, EntryPoint = "H264_GetVideoFrameSize_Rgb2", CallingConvention = CallingConvention.Cdecl)]
private static extern Int32 H264_GetVideoFrameSize_Rgb2(long handle, Int32 width, Int32 height); [DllImport(DLLName, EntryPoint = "H264_GetNextVideoFrame_Rgb2", CallingConvention = CallingConvention.Cdecl)]
private static extern Int32 H264_GetNextVideoFrame_Rgb2(long handle, IntPtr buffer, Int32 bufferLen, Int32 width, Int32 height); [DllImport(DLLName, EntryPoint = "H264_SetDefaultAlgorithm", CallingConvention = CallingConvention.Cdecl)]
private static extern Int32 H264_SetDefaultAlgorithm(Int32 flag); [DllImport(DLLName, EntryPoint = "H264_SetAlgorithm", CallingConvention = CallingConvention.Cdecl)]
private static extern Int32 H264_SetAlgorithm(long handle,Int32 flag); public static long CreateHandle()
{
return H264_CreateHandle();
} public static long CloseHandle(long handle)
{
return H264_CloseHandle(handle);
} public static int PutVideoStream(long handle, byte[] buffer, Int32 bufferLen)
{
GCHandle hin = GCHandle.Alloc(buffer, GCHandleType.Pinned);
int result = H264_PutVideoStream(handle, hin.AddrOfPinnedObject(), bufferLen);
hin.Free();
return result;
} public static int GetVideoParam(long handle, out int width, out int height)
{
width = ;
height = ;
byte[] width2 = new byte[];
byte[] height2 = new byte[]; GCHandle hin_width = GCHandle.Alloc(width2, GCHandleType.Pinned);
GCHandle hin_height = GCHandle.Alloc(height2, GCHandleType.Pinned);
int result = H264_GetVideoParam(handle, hin_width.AddrOfPinnedObject(), hin_height.AddrOfPinnedObject());
hin_width.Free();
hin_height.Free(); if (result != )
{
return result;
} width = BitConverter.ToInt32(width2, );
height = BitConverter.ToInt32(height2, );
return result;
} public static int GetVideoFrameSize(long handle)
{
int result = H264_GetVideoFrameSize(handle);
return result;
}
public static int GetVideoFrameFormate(long handle)
{
int result = H264_GetVideoFrameFormate(handle);
return result;
} public static int GetNextVideoFrame(long handle, byte[] buffer, Int32 bufferLen, EN_H264_YU_Formate formate)
{
GCHandle hin = GCHandle.Alloc(buffer, GCHandleType.Pinned);
int result = H264_GetNextVideoFrame(handle, hin.AddrOfPinnedObject(), bufferLen, (int)formate);
hin.Free();
return result;
} public static int GetVideoFrameSize_Rgb(long handle)
{
int result = H264_GetVideoFrameSize_Rgb(handle);
return result;
} public static int GetNextVideoFrame_Rgb(long handle, byte[] buffer, Int32 bufferLen)
{
GCHandle hin = GCHandle.Alloc(buffer, GCHandleType.Pinned);
int result = H264_GetNextVideoFrame_Rgb(handle, hin.AddrOfPinnedObject(), bufferLen);
hin.Free();
return result;
} public static int GetVideoFrameSize_Rgb2(long handle, Int32 width, Int32 height)
{
int result = H264_GetVideoFrameSize_Rgb2(handle, width, height);
return result;
} public static Int32 SetDefaultAlgorithm(Int32 flag)
{
return H264_SetDefaultAlgorithm(flag);
} public static Int32 SetAlgorithm(long handle, Int32 flag)
{
return H264_SetAlgorithm(handle,flag);
} public static int GetNextVideoFrame_Rgb2(long handle, byte[] buffer, Int32 bufferLen, Int32 width, Int32 height)
{
GCHandle hin = GCHandle.Alloc(buffer, GCHandleType.Pinned);
int result = H264_GetNextVideoFrame_Rgb2(handle, hin.AddrOfPinnedObject(), bufferLen, width, height);
hin.Free();
return result;
} public enum EN_H264_YU_Formate
{
Y_U_V = ,
Y_V_U = ,
}
}
使用ffmpeg实现对h264视频解码 -- (实现了一个易于使用的c++封装库)的更多相关文章
- 基于DevExpress实现对PDF、Word、Excel文档的预览及操作处理
http://www.cnblogs.com/wuhuacong/p/4175266.html 在一般的管理系统模块里面,越来越多的设计到一些常用文档的上传保存操作,其中如PDF.Word.Excel ...
- C#代码实现对HTTP POST参数进行排序
private static string GetSortedParas(Dictionary<string, string> dic) { dic = dic.OrderBy(key = ...
- 在VS2015中用C++创建DLL并用C#调用且同时实现对DLL的调试
from:http://m.blog.csdn.net/article/details?id=51075023 在VS2015中先创建C#项目,然后再创建要编写的动态库DLL项目,这样做的好处是整个解 ...
- 【JAVA使用XPath、DOM4J解析XML文件,实现对XML文件的CRUD操作】
一.简介 1.使用XPath可以快速精确定位指定的节点,以实现对XML文件的CRUD操作. 2.去网上下载一个“XPath帮助文档”,以便于查看语法等详细信息,最好是那种有很多实例的那种. 3.学习X ...
- Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问
本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...
- struts2视频学习笔记 22-23(基于XML配置方式实现对action的所有方法及部分方法进行校验)
课时22 基于XML配置方式实现对action的所有方法进行校验 使用基于XML配置方式实现输入校验时,Action也需要继承ActionSupport,并且提供校验文件,校验文件和action类 ...
- 用 Python 脚本实现对 Linux 服务器的监控
目前 Linux 下有一些使用 Python 语言编写的 Linux 系统监控工具 比如 inotify-sync(文件系统安全监控软件).glances(资源监控工具)在实际工作中,Linux 系统 ...
- 在应用程序中实现对NandFlash的操作
以TC58NVG2S3ETA00 为例: 下面是它的一些物理参数: 图一 图二 图三 图四 图五 图6-0 图6-1 说明一下,在图6-1中中间的那个布局表可以看做是实际的NandFlash一页数据的 ...
- JDOM方法实现对XML文件的解析
首先要下载JDOM.jar包,下载地址:http://download.csdn.net/detail/ww6055/8880371 下载到JDOM.jar包之后导入到工程中去. 实例程序: book ...
随机推荐
- ejb servlet demo
官方文档: http://docs.oracle.com/javaee/6/tutorial/doc/gijre.html package converter.ejb; import java.mat ...
- 20155326刘美岑 2016-2017-2 《Java程序设计》第二周学习总结
20155326刘美岑 2016-2017-2 <Java程序设计>第二周学习总结 教材学习内容总结 教材第三章主要讲解的是基础语法,主要包括认识类型与变量.学习运算符的基本使用.了解类型 ...
- POJ1468 Sorting Slides
Sorting Slides Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4442 Accepted: 1757 De ...
- Scala_对象
对象 单例对象 Scala并没有提供Java那样的静态方法或静态字段,但是,可以采用 object关键字实现单例对象,具备和Java静态方法同样的功能. 可以看出,单例对象的定义和类的定义很相似,明显 ...
- wordpress和数据库的连接
1.首先在数据库里创建wordpress数据库 2.在网页上配置WordPress,安装WordPress 如上配置不对,提交时提示了错误,于是我选择了root用户 123456, 3.提交后,连上了 ...
- Tencent interview
1.常见的聚类算法 1):划分法:k-means 2):基于密度的方法: 2.EM 算法 EM算法是在概率模型中寻找参数的最大似然估计或者最大后验概率的算法,其中概率模型依赖于无法观测的隐藏变量.EM ...
- Delphi FastReport报表常用方法
Delphi FastReport报表常用方法 作者及来源: EasyPass - 博客园 收藏到→_→: 摘要: Delphi FastReport报表常用方法 点击这里! ...
- JS学习笔记1_基础与常识
1.六种数据类型 5种基础的:Undefined,Null,Boolean,Number,String(其中Undefined派生自Null) 1种复杂的:Object(本质是一组无序键值对) 2.字 ...
- 【NumberValidators】工商营业执照号码和统一社会信用代码验证
从本质上讲,工商营业执照号码和统一社会信用代码是两套完全不一样的编码规则,识别结果也仅有行政区划部分为两者共有,但因为这两种编码同时存在的原因,所以如果需要在系统中唯一标志一家企业时,还是可以通过工商 ...
- Winform DataGridView控件在业务逻辑上的简单使用
需要对文字列表进行处理,然后用到DataGridView控件来处理,记录一下.效果如下: 主要是想通过禁用和取消单元格选择来使图标单元格呈现出鼠标点击的效果.因为有个单元格选择的问题困扰着我. 是这样 ...