You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
3.3 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#pragma once
#include "stdafx.h"
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <stdint.h>
#include <tchar.h>
#include <string>
#include <direct.h>
#include <iostream>
#include <algorithm>
//BOOST
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include "boost/shared_ptr.hpp"
//#include <boost/thread.hpp>
#include <boost/format.hpp>
//////////////////////////////////////////////////////////////////////////
//Definitions
//////////////////////////////////////////////////////////////////////////
typedef int (*pinit_h264ToBMP)(uint32_t width, uint32_t height, uint32_t pps);
typedef BOOL (*ppost_BMP_RGB)(const uint8_t *h264_data, uint32_t h264_data_size,unsigned char *decodebuf);
typedef void (*pFree_all)();
struct H264Decode
{
pinit_h264ToBMP m_DLL_init_h264ToBMP_without_encoder;
ppost_BMP_RGB m_DLL_post_BMP_RGB;
pFree_all m_Dll_pFree_all;
HINSTANCE m_h264Dll;
unsigned char* m_tmpBuf;
bool Init(int decodeId)
{
char dllname[255];
sprintf(dllname,"h264ToBMP_%d.dll",decodeId);
std::string destfilename = boost::str(boost::format("h264ToBMP_%d.dll") % decodeId);
boost::filesystem::path destPath(destfilename);
boost::filesystem::path basePath("h264ToBMP.dll");
if (!boost::filesystem::is_regular_file(destPath))
{
//winXP下如果已存文件再copy一下会出现问题如果catch住就可以避免其发生
try
{
boost::filesystem::copy_file(basePath, destPath);
}catch(boost::filesystem::filesystem_error)
{
}
}
m_h264Dll = LoadLibrary(destfilename.c_str());
if (nullptr == m_h264Dll)
{
return false;
}
m_DLL_init_h264ToBMP_without_encoder = (pinit_h264ToBMP) GetProcAddress(m_h264Dll, "init_h264ToBMP_without_encoder");
m_DLL_post_BMP_RGB = (ppost_BMP_RGB) GetProcAddress(m_h264Dll, "post_BMP_RGB");
m_Dll_pFree_all = (pFree_all) GetProcAddress(m_h264Dll, "free_all");
m_tmpBuf = new unsigned char[2500*1500];
return true;
}
int InitDecoder(int width, int height, int pps)
{
if (m_h264Dll != nullptr)
{
int ret = m_DLL_init_h264ToBMP_without_encoder(width, height, pps);
return ret;
}
else
{
return 0;
}
}
int DecodeVideo(const char* data,int msgLen,unsigned char* imgbuf)
{
try
{
if (m_h264Dll != nullptr)
{
static bool firstTime = true;
int length = msgLen;
if (true == firstTime)
{
firstTime = false;
uint8_t sps_frame[16] = { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0x89, 0x95, 0x41, 0x68, 0x22, 0xD0, 0x04 };//GAODE NEW
memcpy(m_tmpBuf, sps_frame, 15);
memcpy(m_tmpBuf + 15, data, msgLen);
length = msgLen + 15;
}
else
{
memcpy(m_tmpBuf, data, msgLen);
}
if (m_tmpBuf != nullptr && length > 0 && imgbuf != nullptr)
{
// 此处容易在测控状态不好的情况下报错。
try
{
int got_bmp = m_DLL_post_BMP_RGB(m_tmpBuf, length, imgbuf);
return got_bmp;
}
catch (...)
{
}
}
else
{
return 0;
}
}
else
{
return 0;
}
}
catch (CMemoryException* e)
{
e;
return 0;
}
catch (CFileException* e)
{
e;
return 0;
}
catch (CException* e)
{
e;
return 0;
}
return 1;
}
void DeInit()
{
delete m_tmpBuf;
m_tmpBuf = nullptr;
m_h264Dll = nullptr;
m_Dll_pFree_all();
}
};