C#学习系列之H264解码

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


唠叨

最近忙着修改代码,但是遇到比较棘手的问题。修改了底层引用的文件,替换了地址,修改了图标。还是躲不过H264、H265的解码问题。作为一位图像处理专业的学生,我也是有点爱莫能助。写文章,记录自己的学习心得,也希望大家能分享一些经验!


一、H264/H265是什么?

  • H.264

MPEG-4第十部分,是由ITU-T视频编码专家组(VCEG)和ISO/IEC动态图像专家组(MPEG)联合组成的联合视频组(JVT,Joint Video Team)提出的高度压缩数字视频编解码器标准。这个标准通常被称之为H.264/AVC(或者AVC/H.264或者H.264/MPEG-4 AVC或MPEG-4/H.264 AVC)
优点
1、低码率(与MPEG相比);
2、高质量的图像(连续、流畅);
3、容错能力强(丢包问题);
4、网络适应性强(传输性好)
编码
1、帧内预测编码——压缩图像空间冗余。通过已有宏块预测其他宏块的预测值与实际值,差值进行编码;
2、帧间预测编码——连续帧中的时间冗余来进行运动估计和补偿。流间传送帧(SP帧)——类似内容、不同码率的码流之间快速切换。(运动补偿、多帧预测、自适应去除块的滤波器)

  • H265
    H265就是在H264的基础上,进行优化。
    优点
    1、降码率——编码单位;
    2、块的四叉树分化结构——预测与变化;
    3、传输速度、内容更多更快,存储空间少。

二、使用问题

1.H264解码基础理论

  • 内容

在VS平台上,使用.net搭配C#来解决H264解码,但是会出现图像马赛克的情况。
了解H264解码中,图像的连续传输可以理解成目标跟踪的原理,以第一帧图像作为参考,后面的图像就在第一帧的基础上进行差别对比,不断演变成后续的图像。后续的图像也会不会演变为第一张,然后依次循环。
H264编码数据从帧——片——宏块(基本单元)

  • 相关术语
    协议中定义三种帧:
    I帧:完整图像帧
    B帧:参考前后图像帧编码生成
    P帧:参考I帧生成
    GOP 画面组:变化不大的图像集,其中M指定I帧与P帧之间的距离;N指定两个I帧之间的距离
    IDR关键帧:为I帧,但是I不一定是关键帧。作为已解码、重新开始的机会,分水岭。
  • 压缩方式
    1、分组:GOP
    2、定义帧:划分为三类帧
    3、预测帧:I帧为基础,I帧预测P帧,I帧与P帧预测B帧
    4、数据传输:I帧与预测差值信息进行存储和传输
  • 分层结构
    1、视频编码(VCL)——视频编码层——视频内容
    2、网口抽象(NAL)——网络提取层——按照一定协议传输数据

2.H264实际应用

实际在项目中需要使用c#来调用海思H264的解码库。
1、读取压缩图像的H264裸码转化为yuv数据;
2、将yuv数据转化为图像数据;
3、利用图像数据用c#的控件将图像进行显示。

//初始化
// 这是解码器输出图像信息
hiH264_DEC_FRAME_S _decodeFrame = new hiH264_DEC_FRAME_S();
// 这是解码器属性信息
hiH264_DEC_ATTR_S decAttr = new hiH264_DEC_ATTR_S();
decAttr.uPictureFormat = 0;
decAttr.uStreamInType = 0;
/* 解码器最大图像宽高, D1图像(1280x720) */
decAttr.uPicWidthInMB = (uint)width / 16;
decAttr.uPicHeightInMB = (uint)height / 16;
/* 解码器最大参考帧数: 16 */
decAttr.uBufNum = 16;
/* bit0 = 1: 标准输出模式; bit0 = 0: 快速输出模式 */
/* bit4 = 1: 启动内部Deinterlace; bit4 = 0: 不启动内部Deinterlace */
decAttr.uWorkMode = 0x10;
//创建、初始化解码器句柄
IntPtr _decHandle = H264Dec.Hi264DecCreate(ref decAttr);

//解码结束
bool isEnd = false;
int bufferLen = 0x1000;
IntPtr pData = Marshal.AllocHGlobal(0xFFFF);
//码流段
byte[] buf = new byte[0xFFFF];
int dataLenth = 0;
while (!isEnd && !isDispose)
{
    VideoFrameData frameDataTemp;
    byte tempByte;
    int j = 0;
    bool getData = dataFrameQueue2.TryDequeue(out frameDataTemp);
    if (getData)
    {
        Array.Copy(frameDataTemp.Data, 0, buf, dataLenth, frameDataTemp.Data.Length);
        dataLenth += frameDataTemp.Data.Length;
        //GC.Collect();
        //GC.SuppressFinalize(this);
    }
    //获取一段码流,积累一定缓存量再解
    if (dataLenth >= bufferLen || isStop == 1)
    {
        Marshal.Copy(buf, 0, pData, dataLenth);
        if (firstDecTimeBh)
        {
            firstDecTimeBh = false;
            Console.WriteLine("解码前时间:" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss:fff"));
        }
        int result = -1;
        result = H264Dec.Hi264DecFrame(_decHandle, pData, (UInt32)dataLenth, 0, ref _decodeFrame, (uint)isStop);
        dataLenth = 0;
        //IntPtr _decHandle2 = H264Dec.Hi264DecCreate(ref decAttr);
        //hiH264_DEC_FRAME_S _decodeFrame2 = new hiH264_DEC_FRAME_S();
        //IntPtr pData2 = Marshal.AllocHGlobal(frameDataTemp.DataLenth);
        //Marshal.Copy(frameDataTemp.Data, 0, pData2, frameDataTemp.DataLenth);
        //int result2 = 0;
        //result2 = H264Dec.Hi264DecFrame(_decHandle2, pData2, (UInt32)frameDataTemp.DataLenth, 0, ref _decodeFrame2, (uint)isStop);
        //if (result2 >= 0)
        //    Console.WriteLine("发现帧");
        while (HI_H264DEC_NEED_MORE_BITS != result)
        {
            if (firstDecTimeBf)
            {
                firstDecTimeBf = false;
                Console.WriteLine("解码后时间:" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss:fff"));
            }
            if (HI_H264DEC_NO_PICTURE == result)
            {
                isEnd = true;
                break;
            }
            if (HI_H264DEC_OK == result)/* 输出一帧图像 */
            {
                //获取yuv
                UInt32 tempWid = _decodeFrame.uWidth;
                UInt32 tempHeig = _decodeFrame.uHeight;
                UInt32 yStride = _decodeFrame.uYStride;
                UInt32 uvStride = _decodeFrame.uUVStride;
                byte[] y = new byte[tempHeig * yStride];
                byte[] u = new byte[tempHeig * uvStride / 2];
                byte[] v = new byte[tempHeig * uvStride / 2];
                Marshal.Copy(_decodeFrame.pY, y, 0, y.Length);
                Marshal.Copy(_decodeFrame.pU, u, 0, u.Length);
                Marshal.Copy(_decodeFrame.pV, v, 0, v.Length);
                _decodeFrame.uDpbIdx = (uint)frameDataTemp.FrameId;
                MyProcessEvent2(_decodeFrame);
                //转为yv12格式
                //byte[] yuvBytes = new byte[y.Length + u.Length + v.Length];
                //Array.Copy(y, 0, yuvBytes, 0, y.Length);
                //Array.Copy(v, 0, yuvBytes, y.Length , v.Length);
                //Array.Copy(u, 0, yuvBytes, y.Length + v.Length, u .Length);
                //更新显示
                //this.d3dSource.Render(_decodeFrame.pY, _decodeFrame.pU, _decodeFrame.pV);
            }
            /* 继续解码剩余H.264码流 */
            result = H264Dec.Hi264DecFrame(_decHandle, IntPtr.Zero, 0, 0, ref _decodeFrame, (uint)isStop);
        }
        System.Threading.Thread.Sleep(1);
    }

}
/* 销毁解码器 */
H264Dec.Hi264DecDestroy(_decHandle);

在使用时,将得到的H264压缩的源码提取到dataFrameQueue2队列中,然后从队列中取出来,进行存储,存储到一定码流后再进行解码。
除了以上解码,还会使用到

 [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解码库中的需要使用到的函数,将码流做图像输出。使用到extern也是可以理解的。

总结

H264解码首先了解解码原理,其次利用已有的解码库对H264结构进行了解,然后利用代码将其实现。

引用

1、https://baike.baidu.com/item/H.264/1022230?fromtitle=H264&fromid=7338504&fr=aladdin
2、https://zhuanlan.zhihu.com/p/71270595
3、https://blog.csdn.net/go_str/article/details/80340564
4、https://www.cnblogs.com/cangyue080180/p/5873351.html

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用FFmpeg解码H.264视频的示例代码,需要安装FFmpeg库并将其添加到项目引用中: ```csharp using System; using System.Diagnostics; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; using FFmpeg.AutoGen; namespace FFmpegDecodeH264 { class Program { static void Main(string[] args) { // 设置FFmpeg的路径 string ffmpegPath = @"C:\ffmpeg\bin\ffmpeg.exe"; ffmpeg.av_register_all(); // 打开输入文件 AVFormatContext* pFormatContext = null; int result = ffmpeg.avformat_open_input(&pFormatContext, "input.h264", null, null); if (result < 0) { Console.WriteLine("无法打开输入文件"); return; } // 获取流信息 result = ffmpeg.avformat_find_stream_info(pFormatContext, null); if (result < 0) { Console.WriteLine("无法获取流信息"); return; } // 查找视频流 int videoStreamIndex = -1; for (int i = 0; i < pFormatContext->nb_streams; i++) { if (pFormatContext->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; break; } } if (videoStreamIndex == -1) { Console.WriteLine("找不到视频流"); return; } // 获取视频流解码器 AVCodecContext* pCodecContext = pFormatContext->streams[videoStreamIndex]->codec; AVCodec* pCodec = ffmpeg.avcodec_find_decoder(pCodecContext->codec_id); if (pCodec == null) { Console.WriteLine("无法获取解码器"); return; } // 打开解码器 result = ffmpeg.avcodec_open2(pCodecContext, pCodec, null); if (result < 0) { Console.WriteLine("无法打开解码器"); return; } // 循环读取帧并解码 AVPacket packet = new AVPacket(); AVFrame* pFrame = ffmpeg.av_frame_alloc(); AVFrame* pFrameRGB = ffmpeg.av_frame_alloc(); SwsContext* pSwsContext = null; while (ffmpeg.av_read_frame(pFormatContext, &packet) >= 0) { if (packet.stream_index == videoStreamIndex) { // 解码一帧 int frameFinished = 0; result = ffmpeg.avcodec_decode_video2(pCodecContext, pFrame, &frameFinished, &packet); if (result < 0) { Console.WriteLine("解码错误"); return; } // 如果解码成功,将帧转换为RGB格式 if (frameFinished != 0) { // 初始化转换器 if (pSwsContext == null) { pSwsContext = ffmpeg.sws_getContext( pCodecContext->width, pCodecContext->height, pCodecContext->pix_fmt, pCodecContext->width, pCodecContext->height, AVPixelFormat.AV_PIX_FMT_RGB24, ffmpeg.SWS_BILINEAR, null, null, null); } // 分配RGB帧 int numBytes = ffmpeg.av_image_get_buffer_size(AVPixelFormat.AV_PIX_FMT_RGB24, pCodecContext->width, pCodecContext->height, 1); byte* buffer = (byte*)ffmpeg.av_malloc((ulong)numBytes); ffmpeg.av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, buffer, AVPixelFormat.AV_PIX_FMT_RGB24, pCodecContext->width, pCodecContext->height, 1); // 转换帧 ffmpeg.sws_scale(pSwsContext, pFrame->data, pFrame->linesize, 0, pCodecContext->height, pFrameRGB->data, pFrameRGB->linesize); // 显示帧 Bitmap bitmap = new Bitmap(pCodecContext->width, pCodecContext->height, pCodecContext->width * 3, PixelFormat.Format24bppRgb, new IntPtr(pFrameRGB->data[0])); bitmap.Save("output.bmp", ImageFormat.Bmp); // 释放RGB帧内存 ffmpeg.av_free(buffer); } } // 释放数据包 ffmpeg.av_packet_unref(&packet); } // 释放资源 ffmpeg.av_frame_free(&pFrame); ffmpeg.av_frame_free(&pFrameRGB); ffmpeg.sws_freeContext(pSwsContext); ffmpeg.avcodec_close(pCodecContext); ffmpeg.avformat_close_input(&pFormatContext); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值