在好例子网,分享、交流、成长!
您当前所在位置:首页C/C++ 开发实例多媒体编程 → C++ 解码h264

C++ 解码h264

多媒体编程

下载此实例
  • 开发语言:C/C++
  • 实例大小:0.82M
  • 下载次数:45
  • 浏览次数:1274
  • 发布时间:2016-05-26
  • 实例类别:多媒体编程
  • 发 布 人:fjd5286
  • 文件格式:.rar
  • 所需积分:2
 相关标签: h264

实例介绍

【实例简介】

【实例截图】

【核心代码】


/******************************************************************************

  Copyright (C), 2007-2017, Hisilicon Tech. Co., Ltd.
  ******************************************************************************
  File Name     : hi_h264sample.c
  Version       : Initial Draft
  Author        : Hisilicon multimedia
  Created       : 2007/05/17
  Description   :
  History       :
  1.Date        : 2007/07/07
  Author        : y39262
  Modification  : Created file
******************************************************************************/
#include "hi_config.h"
#include "hi_h264api.h"

//#define MAX_DEC_NUM  32
#define BYTE_LEN 0x8000

HI_S32 main(HI_S32 argc,HI_U8** argv)
{
    HI_S32 end = 0, input_param;
    HI_U8  buf[BYTE_LEN];
    H264_DEC_ATTR_S   dec_attrbute;
    H264_DEC_FRAME_S  dec_frame;
    H264_LIBINFO_S    lib_info;
    HI_HDL handle = NULL;
    LARGE_INTEGER lpFrequency;
    LARGE_INTEGER t1;
    LARGE_INTEGER t2;
    HI_U32 time;
    HI_U32 pic_cnt = 0;
    HI_U32 ImageEnhanceEnable = 0;
    HI_U32 StrenthCoeff = 40;
    HI_S32 WatermarkEnable = 0;
    HI_S32 DeinterlaceEnable = 0;
    HI_S32 DirectOutputEnable = 0;
    FILE *h264 = NULL;          //input h264 stream file
    FILE *yuv = NULL;           //file to save yuv


    if ( 0 == Hi264DecGetInfo(&lib_info) )
    {
        fprintf(stderr, "Version: %s\nCopyright: %s\n\n", lib_info.sVersion, lib_info.sCopyRight);
		fprintf(stderr, "Function 0x%x\n", lib_info.uFunctionSet);
    }

    if (argc < 2)
    {
        fprintf(stderr, "Comand format or no H.264 stream! The Example:\n");
        fprintf(stderr, "hi_h264dec_w.exe stream_file.264 [-o yuvfile] [-direct] [-enhance 40] [-deinterlace]\n\n");
        goto exitmain;
    }

	/* open input bitstream file(necessary) */
	h264 = fopen(argv[1], "rb");
    if (NULL == h264)
    {
        fprintf(stderr, "Unable to open a h264 stream file %s \n", argv[1]);
        goto exitmain;
    }
    printf("decoding file: %s...\n",argv[1]);

	/* parse optional parameters */
	for(input_param = 2; input_param<argc; input_param  )
	{
		if(strcmp("-enhance", argv[input_param]) == 0 && (input_param 1)<argc)
		{
			ImageEnhanceEnable = 1;
			StrenthCoeff = atoi(argv[  input_param]);
			continue;
		}
		else if(strcmp("-deinterlace", argv[input_param]) == 0)
		{
			DeinterlaceEnable = 1;
			continue;
		}
		else if(strcmp("-direct", argv[input_param]) == 0)
		{
			DirectOutputEnable = 1;
			continue;
		}
		else if(strcmp("-o", argv[input_param]) == 0 && (input_param 1)<argc)
		{
            /* open yuv file */
            yuv = fopen(argv[  input_param], "wb");
            if (NULL == yuv)
            {
                fprintf(stderr, "Unable to open the file to save yuv %s.\n", argv[input_param]);
                goto exitmain;
            }
            printf("save yuv file: %s...\n",argv[input_param]);
		}
	}

    /*init the config info for docoder*/
    dec_attrbute.uBufNum        = 16;     // reference frames number: 16
    dec_attrbute.uPicHeightInMB = 36;     // D1(720x576)
    dec_attrbute.uPicWidthInMB  = 45;
    dec_attrbute.uStreamInType  = 0x00;   // bitstream begin with "00 00 01" or "00 00 00 01"

	if(DirectOutputEnable)
	{
		/* bit0 = 1: H.264 normal output mode; bit0 = 0: direct output mode */
		dec_attrbute.uWorkMode = 0x00;
	}
	else
	{
		dec_attrbute.uWorkMode = 0x01;
	}

    if(DeinterlaceEnable)
	{
		/* bit4 = 1:  enable deinteralce;    bit4 = 0: disable deinterlace */
		dec_attrbute.uWorkMode |= 0x10;
	}


    /*create a decoder*/
    handle = Hi264DecCreate(&dec_attrbute);
    if(NULL ==  handle)
    {
        goto exitmain;
    }

    /* count decoding time: start */
    QueryPerformanceFrequency(&lpFrequency);
    QueryPerformanceCounter(&t1);

    /* dec the h264 stream file */
    while (!end)
    {
        /* read bitstream from "h264" */
        HI_U32  len = fread(buf,1,sizeof(buf),h264);
        HI_U32  flags = (len>0)?0:1;
        HI_S32 result = 0;

        result = Hi264DecFrame(handle, buf,  len, 0, &dec_frame,  flags);

        while(HI_H264DEC_NEED_MORE_BITS  !=  result)
        {
            if(HI_H264DEC_NO_PICTURE ==  result)   //flush over and all the remain picture are output
            {
                end = 1;
                break;
            }

            if(HI_H264DEC_OK == result)   //get a picture
            {
                if(ImageEnhanceEnable)    //image enhance
                {
                    Hi264DecImageEnhance(handle, &dec_frame, StrenthCoeff);
                }
                if(NULL !=  yuv )
                {
                    const HI_U8 *pY = dec_frame.pY;
                    const HI_U8 *pU = dec_frame.pU;
                    const HI_U8 *pV = dec_frame.pV;
                    HI_U32 width    = dec_frame.uWidth;
                    HI_U32 height   = dec_frame.uHeight;
                    HI_U32 yStride  = dec_frame.uYStride;
                    HI_U32 uvStride = dec_frame.uUVStride;

                    fwrite(pY, 1, height* yStride, yuv);
                    fwrite(pU, 1, height* uvStride/2, yuv);
                    fwrite(pV, 1, height* uvStride/2, yuv);
                }

                pic_cnt  ;
            }
            /* continue decoding the remaining bitstream */
            result = Hi264DecFrame(handle, NULL,  0, 0, &dec_frame,  flags);
        }
    }

    /* count decoding time: end */
    QueryPerformanceCounter(&t2);
    time = (HI_U32)((t2.QuadPart-t1.QuadPart)*1000/lpFrequency.QuadPart);
    printf("\ntime= %d ms\n", time);
    printf("%d frames\n",pic_cnt);
    printf("fps: %d\n", pic_cnt*1000/(time 1));
    /* destory decoder */
    Hi264DecDestroy(handle);

exitmain:

    if (NULL != h264)
    {
        fclose(h264);
    }

    if (NULL != yuv)
    {
        fclose(yuv);
    }

    return 0;
}


标签: h264

实例下载地址

C++ 解码h264

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警