📄 video_decode.c
字号:
#include "hi_common_api.h"#include "stdio.h"#include <sys/types.h>#include <unistd.h>#include <pthread.h>#include <stdlib.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <errno.h>//#define DECODEFROMFILE#define DECODE_CHANNEL 4#define VO_CHN0 0#define VO_CHN1 1#define VO_CHN2 2#define VO_CHN3 3#define START_TASK 1#define STOP_TASK -1/*deode channel number*/#define DVS_DECODE_MAXNUM 4/*the display mode decode can support*/#define MODE_CIF 0#define MODE_HD1 1#define MODE_D1 2#define PAL 0#define NTSC 1/*record the work mode for TASK model*/int g_Mode = MODE_CIF;int g_Normal = NTSC; /*write the test programe*/char args[10][255];/*the switch to stop the decode for file*/int g_StopFlieFlag = HI_FALSE;/*debug info*/#define HI_DVS_DEC_DEBUG(info) { printf ("func: %s, line: %d "info"\n", __FUNCTION__, __LINE__);}int g_ChnStatus[DVS_DECODE_MAXNUM] = {-1, -1, -1, -1};extern int HI_DVS_DEC_Close(int chnId);extern int HI_DVS_DEC_Open(int chnId, int mode);extern int HI_DVS_DEC_GetStatus(int chnID, int*pMode);int HI_DVS_DEC_VoPubConfig (int normal, int mode){ HI_VO_Init(); VO_PUBLIC_ATTR_S struPubAttr; if ( HI_VO_GetPublicAttr (&struPubAttr) != HI_SUCCESS) { HI_DVS_DEC_DEBUG("HI_VO_GetPublicAttr error"); return HI_FAILURE; } struPubAttr.enable = HI_TRUE; /* TV mode */ struPubAttr.display_mode = VO_MODE_BT656; struPubAttr.tv_config.enMaster = VIDEO_CONTROL_MODE_MASTER; struPubAttr.tv_config.compose_mode = (VIDEO_NORM_E)normal; struPubAttr.tv_config.seriatim_mode = HI_TRUE; if (MODE_D1 == mode) { /*don't scale in order to increase the efficiency*/ struPubAttr.bOutputScale = HI_FALSE; } else { struPubAttr.bOutputScale = HI_TRUE; } /*the default color is blue */ struPubAttr.background_color = 0x00ff; struPubAttr.vo_chn_img_num = 4; struPubAttr.pip_buf_num = 4; /*config public attribute for VO*/ if ( HI_VO_SetPublicAttr (&struPubAttr) != HI_SUCCESS) { HI_DVS_DEC_DEBUG("set VO attribute failed !\n"); return HI_FAILURE; } return HI_SUCCESS;}int HI_DVS_DEC_VoChnConfigD1 (int normal){ VO_CHN_ATTR_S struChnAttr ; if ( HI_VO_GetChnAttr (VO_CHN0, &struChnAttr)!= HI_SUCCESS) { HI_DVS_DEC_DEBUG("vo get chn attr failed\n"); } struChnAttr.enable = HI_TRUE; struChnAttr.rectangle.x = 0; struChnAttr.rectangle.y = 0; struChnAttr.rectangle.width = 720; if(normal == PAL) struChnAttr.rectangle.height = 576; else struChnAttr.rectangle.height = 480; struChnAttr.zoom_enable = HI_TRUE; /*config channel attribute for VO channel*/ if ( HI_VO_SetChnAttr (VO_CHN0, &struChnAttr)!= HI_SUCCESS) { HI_DVS_DEC_DEBUG("Set VO_CHN0 failed!\n"); return HI_FAILURE; } return HI_SUCCESS;}int HI_DVS_DEC_VoChnConfigHD1 (int normal){ VO_CHN_ATTR_S struChnAttr ; if ( HI_VO_GetChnAttr (VO_CHN0, &struChnAttr)!= HI_SUCCESS) { HI_DVS_DEC_DEBUG("Get VO CHN0 attribute failed !" ); } struChnAttr.enable = HI_TRUE; if(normal == PAL) { struChnAttr.rectangle.x = 0; struChnAttr.rectangle.y = 0; struChnAttr.rectangle.width = 720; struChnAttr.rectangle.height = 288; } else { struChnAttr.rectangle.x = 0; struChnAttr.rectangle.y = 0; struChnAttr.rectangle.width = 720; struChnAttr.rectangle.height = 240; } struChnAttr.zoom_enable = HI_TRUE; /*steop 0: config channel attribute for VO*/ if ( HI_VO_SetChnAttr (VO_CHN0, &struChnAttr)!= HI_SUCCESS) { printf("Set VO_CHN0 failed!\n"); } if (normal == PAL) { struChnAttr.rectangle.x = 0; struChnAttr.rectangle.y = 288; struChnAttr.rectangle.width = 720; struChnAttr.rectangle.height = 288; } else { struChnAttr.rectangle.x = 0; struChnAttr.rectangle.y = 240; struChnAttr.rectangle.width = 720; struChnAttr.rectangle.height = 240; } if ( HI_VO_SetChnAttr (VO_CHN1, &struChnAttr)!= HI_SUCCESS) { printf("Set VO_CHN1 failed!\n"); } return HI_SUCCESS;}int HI_DVS_DEC_VoChnConfigCIF (int normal){ VO_CHN_ATTR_S struChnAttr ; if ( HI_VO_GetChnAttr (VO_CHN0, &struChnAttr)!= HI_SUCCESS) { printf("Set VO 0 ttribute failed !\n"); } struChnAttr.enable = HI_TRUE; struChnAttr.zoom_enable = HI_TRUE; if(normal == PAL) { struChnAttr.rectangle.x = 0; struChnAttr.rectangle.y = 0; struChnAttr.rectangle.width = 352; struChnAttr.rectangle.height = 288; } else { struChnAttr.rectangle.x = 0; struChnAttr.rectangle.y = 0; struChnAttr.rectangle.width = 352; struChnAttr.rectangle.height = 240; } /*steop 0: config channel attribute for VO*/ if ( HI_VO_SetChnAttr (VO_CHN0, &struChnAttr)!= HI_SUCCESS) { printf("Set VO_CHN0 failed!\n"); } if (normal == PAL) { struChnAttr.rectangle.x = 352; struChnAttr.rectangle.y = 0; struChnAttr.rectangle.width = 352; struChnAttr.rectangle.height = 288; } else { struChnAttr.rectangle.x = 352; struChnAttr.rectangle.y = 0; struChnAttr.rectangle.width = 352; struChnAttr.rectangle.height = 240; } if ( HI_VO_SetChnAttr (VO_CHN1, &struChnAttr)!= HI_SUCCESS) { printf("Set VO_CHN1 failed!\n"); } if(normal == PAL) { struChnAttr.rectangle.x = 0; struChnAttr.rectangle.y = 288; struChnAttr.rectangle.width = 352; struChnAttr.rectangle.height = 288; } else { struChnAttr.rectangle.x = 0; struChnAttr.rectangle.y = 240; struChnAttr.rectangle.width = 352; struChnAttr.rectangle.height = 240; } /*steop 0: config channel attribute for VO*/ if ( HI_VO_SetChnAttr (VO_CHN2, &struChnAttr)!= HI_SUCCESS) { printf("Set VO_CHN0 failed!\n"); } if (normal == PAL) { struChnAttr.rectangle.x = 352; struChnAttr.rectangle.y = 288; struChnAttr.rectangle.width = 352; struChnAttr.rectangle.height = 288; } else { struChnAttr.rectangle.x = 352; struChnAttr.rectangle.y = 240; struChnAttr.rectangle.width = 352; struChnAttr.rectangle.height = 240; } if ( HI_VO_SetChnAttr (VO_CHN3, &struChnAttr)!= HI_SUCCESS) { printf("Set VO_CHN1 failed!\n"); } return HI_SUCCESS;}int HI_DVS_DEC_DestroyAllVoCH(void){ int i; VO_CHN_ATTR_S l_chn_attr ; VO_PUBLIC_ATTR_S l_pub_attr ; /*disable all vo channel*/ for (i = 0; i <= VO_CHN3; i++) { if ( HI_VO_GetChnAttr (i, (VO_CHN_ATTR_S *)&l_chn_attr)!= HI_SUCCESS) { HI_DVS_DEC_DEBUG("HI_VO_GetChnAttr error\n\r "); } l_chn_attr.enable = HI_FALSE; if ( HI_VO_SetChnAttr (i, (VO_CHN_ATTR_S *)&l_chn_attr)!= HI_SUCCESS) { HI_DVS_DEC_DEBUG(" HI_VO_SetChnAttr error\n\r "); } } /*disable vo pub*/ if ( HI_VO_GetPublicAttr (&l_pub_attr) != HI_SUCCESS) { HI_DVS_DEC_DEBUG(" HI_VO_GetPublicAttr error\n\r "); } l_pub_attr.enable = HI_FALSE ; if ( HI_VO_SetPublicAttr (&l_pub_attr) != HI_SUCCESS) { HI_DVS_DEC_DEBUG(" HI_VO_SetPublicAttr error\n\r "); return HI_FAILURE; } return HI_SUCCESS;}/*can be called by init function, and mode switch*/int HI_DVS_DEC_CreateAllVoCH(int normal, int DisPlayMode){ int ret; ret = HI_DVS_DEC_VoPubConfig(normal, DisPlayMode); if (ret != HI_SUCCESS) { HI_DVS_DEC_DEBUG("VO config public attribute failed\n"); } switch (DisPlayMode) { case MODE_D1: { printf ("config vo d1\n"); ret = HI_DVS_DEC_VoChnConfigD1(normal); } break; case MODE_HD1: { ret = HI_DVS_DEC_VoChnConfigHD1(normal); } break; case MODE_CIF: { printf ("config vo cif\n"); ret = HI_DVS_DEC_VoChnConfigCIF(normal); } break; default: break; } if (ret != HI_SUCCESS) { HI_DVS_DEC_DEBUG("VO create all channel failed\n"); } return ret;}int HI_DVS_DEC_DestroyAllDecodeCH (void){ int ret; int i; for (i = 0; i < 4; i ++) { ret = HI_VDEC_DestroyCH(i);; if (HI_SUCCESS != ret) { HI_DVS_DEC_DEBUG("destroy decode channel error\n"); } } return HI_SUCCESS;}int HI_DVS_DEC_CreateAllDecodeCH (void){ int ret; int i; VDEC_ChannelPara_S config; VDEC_ATTR_TYPE_E type = VDEC_ATTR_ALL; ret = HI_VDEC_Open(); if (HI_SUCCESS != ret) { HI_DVS_DEC_DEBUG("creat decode channel error\n"); } /*set config value*/ config.type = VDEC_H264; config.framenumber = 4; config.picformat = VDEC_DEFAULT_PICTUREFORMAT; for (i = 0; i < 4; i ++) { ret = HI_VDEC_CreateCH(&i, type, &config); if (HI_SUCCESS != ret) { HI_DVS_DEC_DEBUG("creat decode channel error\n"); printf("Error code 0x%08x, %d\n", ret, i); return HI_FAILURE; } else { printf ("create decode channel: %d\n", i); } } return HI_SUCCESS;}#if 0void *HI_DVS_DEC_SendStream2Dec (void * args){ VDEC_STREAM_S stream; int length; char dataBuf[2048]; int ChanID; int pid; int ret; ChanID = *((int*)args); pid = getpid(); printf("vdec receive thread pid %d for channel: %d\n", pid, ChanID); while(STOP_TASK != g_ChnStatus[ChanID]) { length = HI_DVS_DEC_GetBuf(ChanID, dataBuf); if (length <= 0) { usleep(10000); continue; } stream.Datalen = length; stream.Dataaddr = (HI_U32)dataBuf; #if 0 { static FILE *pSliceFile = NULL; if( NULL == pSliceFile) { pSliceFile = fopen ("vdec_stream.264", "wb"); } if (NULL == pSliceFile) { printf("open file failed\n"); } fwrite (dataBuf, length, 1, pSliceFile); } #endif ret = HI_VDEC_SendStream(ChanID, &stream, HI_TRUE); while(HI_SUCCESS != ret) { ret = HI_VDEC_SendStream(ChanID, &stream, HI_TRUE); usleep(1); } } printf("vdec receive thread pid %d for channel: %d quit\n", pid, ChanID); pthread_exit(NULL); return NULL;}#endifvoid *HI_DVS_DEC_SendStream2VO(void * args){ HI_S32 ret; pid_t pid; int chnId; VDEC_FRAMEINFO_S frameinfo; chnId = (int)args; pid = getpid(); printf("vo receive thread pid %d for channel: %d\n", pid, chnId); while(STOP_TASK != g_ChnStatus[chnId]) { HI_VDEC_Receive(chnId, &frameinfo, NULL, HI_TRUE); if (frameinfo.value) { //printf("HI_DVS_DEC_SendStream2VO %d, %d\n", frameinfo.width, frameinfo.height); ret = HI_VO_SendData(chnId, &frameinfo);#ifdef DECODEFROMFILE while(HI_SUCCESS != ret) { /*if deocode the file, the decode data must be success*/ usleep(100); ret = HI_VO_SendData(chnId, &frameinfo); }#else if (ret != HI_SUCCESS) { /*if the data can't be send to vo ,then release it*/ ret = HI_VDEC_ReleaseVideoBuf(chnId, &frameinfo); while (HI_SUCCESS != ret) { usleep(100); /*release video buffer must be efficiency*/ ret = HI_VDEC_ReleaseVideoBuf(chnId, &frameinfo); } }#endif } usleep(100); } printf("vo receive thread pid %d for channel: %d quit\n", pid, chnId); pthread_exit(NULL); return NULL;}int HI_DVS_DEC_StartDataStream(int chnId){ pthread_t SendStream2Dec;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -