void DecoderParseSyntaxTest::TestSpecificBs() { int32_t iRet = ERR_NONE; m_sDecParam.bParseOnly = true; m_sDecParam.eEcActiveIdc = ERROR_CON_DISABLE; iRet = m_pDec->Initialize (&m_sDecParam); ASSERT_EQ (iRet, ERR_NONE); ASSERT_TRUE (ParseBs ("res/jm_1080p_allslice.264", CorrectParseOnly)); m_pDec->Uninitialize(); }
void DecoderParseSyntaxTest::DecodeBs (const char* sFileName) { uint8_t* pBuf = NULL; int32_t iBufPos = 0; int32_t iFileSize; int32_t i = 0; int32_t iSliceSize; int32_t iSliceIndex = 0; int32_t iEndOfStreamFlag = 0; FILE* pH264File; uint8_t uiStartCode[4] = {0, 0, 0, 1}; #if defined(ANDROID_NDK) std::string filename = std::string ("/sdcard/") + sFileName; ASSERT_TRUE ((pH264File = fopen (filename.c_str(), "rb")) != NULL); #else ASSERT_TRUE ((pH264File = fopen (sFileName, "rb")) != NULL); #endif fseek (pH264File, 0L, SEEK_END); iFileSize = (int32_t) ftell (pH264File); fseek (pH264File, 0L, SEEK_SET); pBuf = new uint8_t[iFileSize + 4]; ASSERT_EQ (fread (pBuf, 1, iFileSize, pH264File), (unsigned int) iFileSize); memcpy (pBuf + iFileSize, &uiStartCode[0], 4); //confirmed_safe_unsafe_usage while (true) { if (iBufPos >= iFileSize) { iEndOfStreamFlag = true; if (iEndOfStreamFlag) m_pDec->SetOption (DECODER_OPTION_END_OF_STREAM, (void*)&iEndOfStreamFlag); break; } for (i = 0; i < iFileSize; i++) { if ((pBuf[iBufPos + i] == 0 && pBuf[iBufPos + i + 1] == 0 && pBuf[iBufPos + i + 2] == 0 && pBuf[iBufPos + i + 3] == 1 && i > 0)) { break; } } iSliceSize = i; DecodeFrame (pBuf + iBufPos, iSliceSize, m_pData, &m_sBufferInfo, m_pCtx); iBufPos += iSliceSize; ++ iSliceIndex; if (iSliceIndex == 4) break; } fclose (pH264File); if (pBuf) { delete[] pBuf; pBuf = NULL; } }
void H264Decoder::initVideo(int frameWidth, int frameHeight) { mWidth = frameWidth; mHeight = frameHeight; SDecodingParam s = {0}; s.eOutputColorFormat = videoFormatI420; s.uiTargetDqLayer = (uint8_t) - 1; s.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_SVC; int ret = mDecoder->Initialize(&s); // zero is success //printf("decoder->Initialize returns %d\n", ret); }
H264Frame H264Decoder::input(int offset, int time, int timecode, int duration, bool keyframe, bool discontinuity, std::string buffer) { unsigned const char *pSrc = (unsigned const char *)buffer.data(); int iSrcLen = buffer.length(); //printf("input src %ld len %d\n", pSrc, iSrcLen); unsigned char *pDst[3] = {0}; SBufferInfo sDstInfo = {0}; int ret = mDecoder->DecodeFrame2(pSrc, iSrcLen, &pDst[0], &sDstInfo); // zero is success //printf("decoder->DecodeFrame2 returns %d; status %d\n", ret, sDstInfo.iBufferStatus); // @todo are the crop params needed? H264Plane y(pDst[0], sDstInfo.UsrData.sSystemBuffer.iStride[0], sDstInfo.UsrData.sSystemBuffer.iHeight, sDstInfo.UsrData.sSystemBuffer.iWidth, 0, 0); H264Plane cb(pDst[1], sDstInfo.UsrData.sSystemBuffer.iStride[1], sDstInfo.UsrData.sSystemBuffer.iHeight / 2, sDstInfo.UsrData.sSystemBuffer.iWidth / 2, 0, 0); H264Plane cr(pDst[2], sDstInfo.UsrData.sSystemBuffer.iStride[1], sDstInfo.UsrData.sSystemBuffer.iHeight / 2, sDstInfo.UsrData.sSystemBuffer.iWidth / 2, 0, 0); H264Frame frame(mWidth, mHeight, y, cb, cr); return frame; }
bool DecoderParseSyntaxTest::ParseBs (const char* sFileName, EDecCase eDecCase) { uint8_t* pBuf = NULL; int32_t iBufPos = 0; int32_t iFileSize; int32_t i = 0; int32_t iSliceSize; int32_t iSliceIndex = 0; int32_t iEndOfStreamFlag = 0; FILE* pH264File; uint8_t uiStartCode[4] = { 0, 0, 0, 1 }; int iRet = 0; #if defined(ANDROID_NDK) std::string filename = std::string ("/sdcard/") + sFileName; if ((pH264File = fopen (filename.c_str(), "rb")) == NULL) return false; #else if ((pH264File = fopen (sFileName, "rb")) == NULL) return false; #endif fseek (pH264File, 0L, SEEK_END); iFileSize = (int32_t)ftell (pH264File); fseek (pH264File, 0L, SEEK_SET); pBuf = new uint8_t[iFileSize + 4]; if (pBuf == NULL) { fclose (pH264File); return false; } if (fread (pBuf, 1, iFileSize, pH264File) != (unsigned int)iFileSize) { fclose (pH264File); if (pBuf) { delete[] pBuf; pBuf = NULL; } return false; } memcpy (pBuf + iFileSize, &uiStartCode[0], 4); //confirmed_safe_unsafe_usage while (true) { if (iBufPos >= iFileSize) { iEndOfStreamFlag = true; if (iEndOfStreamFlag) m_pDec->SetOption (DECODER_OPTION_END_OF_STREAM, (void*)&iEndOfStreamFlag); break; } for (i = 0; i < iFileSize; i++) { if ((pBuf[iBufPos + i] == 0 && pBuf[iBufPos + i + 1] == 0 && pBuf[iBufPos + i + 2] == 0 && pBuf[iBufPos + i + 3] == 1 && i > 0)) { break; } } iSliceSize = i; memset (&m_sParserBsInfo, 0, sizeof (SParserBsInfo)); iRet |= m_pDec->DecodeParser (pBuf + iBufPos, iSliceSize, &m_sParserBsInfo); iRet |= m_pDec->DecodeParser (NULL, 0, &m_sParserBsInfo); if (eDecCase == CorrectParseOnly) EXPECT_TRUE (iRet == dsErrorFree || iRet == dsFramePending); iBufPos += iSliceSize; ++iSliceIndex; if (iSliceIndex == 4) break; } if (eDecCase == ErrorDec) EXPECT_TRUE ((iRet & (dsBitstreamError | dsRefLost | dsDataErrorConcealed)) != 0) << iRet; fclose (pH264File); if (pBuf) { delete[] pBuf; pBuf = NULL; } return true; }
int32_t main (int32_t iArgC, char* pArgV[]) { ISVCDecoder* pDecoder = NULL; SDecodingParam sDecParam = {0}; string strInputFile (""), strOutputFile (""), strOptionFile (""); sDecParam.sVideoProperty.size = sizeof (sDecParam.sVideoProperty); if (iArgC < 2) { printf ("usage 1: h264dec.exe welsdec.cfg\n"); printf ("usage 2: h264dec.exe welsdec.264 out.yuv\n"); printf ("usage 3: h264dec.exe welsdec.264\n"); return 1; } else if (iArgC == 2) { if (strstr (pArgV[1], ".cfg")) { // read config file //confirmed_safe_unsafe_usage CReadConfig cReadCfg (pArgV[1]); string strTag[4]; string strReconFile (""); if (!cReadCfg.ExistFile()) { printf ("Specified file: %s not exist, maybe invalid path or parameter settting.\n", cReadCfg.GetFileName().c_str()); return 1; } while (!cReadCfg.EndOfFile()) { long nRd = cReadCfg.ReadLine (&strTag[0]); if (nRd > 0) { if (strTag[0].compare ("InputFile") == 0) { strInputFile = strTag[1]; } else if (strTag[0].compare ("OutputFile") == 0) { strOutputFile = strTag[1]; } else if (strTag[0].compare ("RestructionFile") == 0) { strReconFile = strTag[1]; int32_t iLen = strReconFile.length(); sDecParam.pFileNameRestructed = new char[iLen + 1]; if (sDecParam.pFileNameRestructed != NULL) { sDecParam.pFileNameRestructed[iLen] = 0; } strncpy (sDecParam.pFileNameRestructed, strReconFile.c_str(), iLen); //confirmed_safe_unsafe_usage } else if (strTag[0].compare ("TargetDQID") == 0) { sDecParam.uiTargetDqLayer = (uint8_t)atol (strTag[1].c_str()); } else if (strTag[0].compare ("OutColorFormat") == 0) { sDecParam.iOutputColorFormat = atol (strTag[1].c_str()); } else if (strTag[0].compare ("ErrorConcealmentFlag") == 0) { sDecParam.uiEcActiveFlag = (uint8_t)atol (strTag[1].c_str()); } else if (strTag[0].compare ("CPULoad") == 0) { sDecParam.uiCpuLoad = (uint32_t)atol (strTag[1].c_str()); } else if (strTag[0].compare ("VideoBitstreamType") == 0) { sDecParam.sVideoProperty.eVideoBsType = (VIDEO_BITSTREAM_TYPE)atol (strTag[1].c_str()); } } } if (strOutputFile.empty()) { printf ("No output file specified in configuration file.\n"); return 1; } } else if (strstr (pArgV[1], ".264")) { // no output dump yuv file, just try to render the decoded pictures //confirmed_safe_unsafe_usage strInputFile = pArgV[1]; sDecParam.iOutputColorFormat = videoFormatI420; sDecParam.uiTargetDqLayer = (uint8_t) - 1; sDecParam.uiEcActiveFlag = 1; sDecParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT; } } else { //iArgC > 2 strInputFile = pArgV[1]; strOutputFile = pArgV[2]; sDecParam.iOutputColorFormat = videoFormatI420; sDecParam.uiTargetDqLayer = (uint8_t) - 1; sDecParam.uiEcActiveFlag = 1; sDecParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT; if (iArgC > 3) { for (int i = 3; i < iArgC; i++) { char* cmd = pArgV[i]; if (!strcmp (cmd, "-options")) { if (i + 1 < iArgC) strOptionFile = pArgV[i++]; else { printf ("options file not specified.\n"); return 1; } } else if (!strcmp (cmd, "-trace")) { if (i + 1 < iArgC) WelsStderrSetTraceLevel (atoi (pArgV[i++])); else { printf ("trace level not specified.\n"); return 1; } } } } if (strOutputFile.empty()) { printf ("No output file specified in configuration file.\n"); return 1; } } if (strInputFile.empty()) { printf ("No input file specified in configuration file.\n"); return 1; } #if defined(_MSC_VER) HMODULE hModule = LoadLibraryA (".\\welsdec.dll"); PCreateDecoderFunc pCreateDecoderFunc = NULL; PDestroyDecoderFunc pDestroyDecoderFunc = NULL; pCreateDecoderFunc = (PCreateDecoderFunc)::GetProcAddress (hModule, "CreateDecoder"); pDestroyDecoderFunc = (PDestroyDecoderFunc)::GetProcAddress (hModule, "DestroyDecoder"); if ((hModule != NULL) && (pCreateDecoderFunc != NULL) && (pDestroyDecoderFunc != NULL)) { printf ("load library sw function successfully\n"); if (pCreateDecoderFunc (&pDecoder) || (NULL == pDecoder)) { printf ("Create Decoder failed.\n"); return 1; } } else { printf ("load library sw function failed\n"); return 1; } #else if (CreateDecoder (&pDecoder) || (NULL == pDecoder)) { printf ("Create Decoder failed.\n"); return 1; } #endif if (pDecoder->Initialize (&sDecParam, INIT_TYPE_PARAMETER_BASED)) { printf ("Decoder initialization failed.\n"); return 1; } int32_t iWidth = 0; int32_t iHeight = 0; H264DecodeInstance (pDecoder, strInputFile.c_str(), strOutputFile.c_str(), iWidth, iHeight, (!strOptionFile.empty() ? (void_t*) (const_cast<char*> (strOptionFile.c_str())) : NULL)); if (sDecParam.pFileNameRestructed != NULL) { delete []sDecParam.pFileNameRestructed; sDecParam.pFileNameRestructed = NULL; } if (pDecoder) { pDecoder->Uninitialize(); #if defined(_MSC_VER) pDestroyDecoderFunc (pDecoder); #else DestroyDecoder (pDecoder); #endif } return 0; }