From 14d3d492cb0a60a0980ee2b2b938d41e806daded Mon Sep 17 00:00:00 2001 From: albertgr Date: Mon, 3 Apr 2023 23:05:07 +0200 Subject: [PATCH] Commit 1 concept fork --- source/Lib/EncoderLib/EncCu.cpp | 221 +++++++++++++++++++++----------- 1 file changed, 147 insertions(+), 74 deletions(-) diff --git a/source/Lib/EncoderLib/EncCu.cpp b/source/Lib/EncoderLib/EncCu.cpp index 8e45e42..4362f60 100644 --- a/source/Lib/EncoderLib/EncCu.cpp +++ b/source/Lib/EncoderLib/EncCu.cpp @@ -57,6 +57,16 @@ #include //END ALBERTO +//ALBERTO CONCEPT FORK +#include +#include +#include +#include +#include +#include +#include +//END ALBERTO CONCEPT FORK + //! \ingroup EncoderLib //! \{ @@ -65,6 +75,17 @@ int EncCu::iter = 0; //END ALBERTO +//ALBERTO CONCEPT FORK +struct vectorModeCostStruct { + int mode; + double cost; +}; +const int VECTOR_SIZE = 1 << 10; // 4^10 +const int VECTOR_BYTES = VECTOR_SIZE * sizeof(int); +sem_t* sem; +vectorModeCostStruct* vector; +//END ALBERTO CONCEPT FORK + // ==================================================================================================================== const MergeIdxPair EncCu::m_geoModeTest[GEO_MAX_NUM_CANDS] = { @@ -261,91 +282,115 @@ void EncCu::init( EncLib* pcEncLib, const SPS& sps ) void EncCu::compressCtu(CodingStructure &cs, const UnitArea &area, const unsigned ctuRsAddr, const EnumArray &prevQP, const EnumArray &currQP) { - m_modeCtrl->initCTUEncoding( *cs.slice ); - cs.treeType = TREE_D; - - cs.slice->m_mapPltCost[0].clear(); - cs.slice->m_mapPltCost[1].clear(); - // init the partitioning manager - QTBTPartitioner partitioner; - partitioner.initCtu(area, ChannelType::LUMA, *cs.slice); - if (m_pcEncCfg->getIBCMode()) - { - if (area.lx() == 0 && area.ly() == 0) - { - m_pcInterSearch->resetIbcSearch(); - } - m_pcInterSearch->resetCtuRecord(); - m_ctuIbcSearchRangeX = m_pcEncCfg->getIBCLocalSearchRangeX(); - m_ctuIbcSearchRangeY = m_pcEncCfg->getIBCLocalSearchRangeY(); - } - if (m_pcEncCfg->getIBCMode() && m_pcEncCfg->getIBCHashSearch() && (m_pcEncCfg->getIBCFastMethod() & IBC_FAST_METHOD_ADAPTIVE_SEARCHRANGE)) - { - const int hashHitRatio = m_ibcHashMap.getHashHitRatio(area.Y()); // in percent - if (hashHitRatio < 5) // 5% - { - m_ctuIbcSearchRangeX >>= 1; - m_ctuIbcSearchRangeY >>= 1; - } - if (cs.slice->getNumRefIdx(REF_PIC_LIST_0) > 0) - { - m_ctuIbcSearchRangeX >>= 1; - m_ctuIbcSearchRangeY >>= 1; - } - } - // init current context pointer - m_CurrCtx = m_ctxBuffer.data(); + //ALBERTO CONCEPT FORK + // Crear memoria compartida para el vector y el semáforo + int fd = shm_open("/myshm", O_CREAT | O_RDWR, 0666); + ftruncate(fd, VECTOR_BYTES + sizeof(sem_t)); + vector = (vectorModeCostStruct*) mmap(NULL, VECTOR_BYTES, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + sem = (sem_t*) (vector + VECTOR_SIZE); + sem_init(sem, 1, 1); + + for (int i = 0; i < VECTOR_SIZE; i++) { + pid_t pid = fork(); + if (pid == 0) { // proceso hijo + // acceder al vector en la posición i + sem_wait(sem); + vectorModeCostStruct structura; + vector[i] = structura; + sem_post(sem); + //END ALBERTO CONCEPT FORK + m_modeCtrl->initCTUEncoding( *cs.slice ); + cs.treeType = TREE_D; + + cs.slice->m_mapPltCost[0].clear(); + cs.slice->m_mapPltCost[1].clear(); + // init the partitioning manager + QTBTPartitioner partitioner; + partitioner.initCtu(area, ChannelType::LUMA, *cs.slice); + if (m_pcEncCfg->getIBCMode()) + { + if (area.lx() == 0 && area.ly() == 0) + { + m_pcInterSearch->resetIbcSearch(); + } + m_pcInterSearch->resetCtuRecord(); + m_ctuIbcSearchRangeX = m_pcEncCfg->getIBCLocalSearchRangeX(); + m_ctuIbcSearchRangeY = m_pcEncCfg->getIBCLocalSearchRangeY(); + } + if (m_pcEncCfg->getIBCMode() && m_pcEncCfg->getIBCHashSearch() && (m_pcEncCfg->getIBCFastMethod() & IBC_FAST_METHOD_ADAPTIVE_SEARCHRANGE)) + { + const int hashHitRatio = m_ibcHashMap.getHashHitRatio(area.Y()); // in percent + if (hashHitRatio < 5) // 5% + { + m_ctuIbcSearchRangeX >>= 1; + m_ctuIbcSearchRangeY >>= 1; + } + if (cs.slice->getNumRefIdx(REF_PIC_LIST_0) > 0) + { + m_ctuIbcSearchRangeX >>= 1; + m_ctuIbcSearchRangeY >>= 1; + } + } + // init current context pointer + m_CurrCtx = m_ctxBuffer.data(); - CodingStructure *tempCS = m_pTempCS[gp_sizeIdxInfo->idxFrom( area.lumaSize().width )][gp_sizeIdxInfo->idxFrom( area.lumaSize().height )]; - CodingStructure *bestCS = m_pBestCS[gp_sizeIdxInfo->idxFrom( area.lumaSize().width )][gp_sizeIdxInfo->idxFrom( area.lumaSize().height )]; + CodingStructure *tempCS = m_pTempCS[gp_sizeIdxInfo->idxFrom( area.lumaSize().width )][gp_sizeIdxInfo->idxFrom( area.lumaSize().height )]; + CodingStructure *bestCS = m_pBestCS[gp_sizeIdxInfo->idxFrom( area.lumaSize().width )][gp_sizeIdxInfo->idxFrom( area.lumaSize().height )]; - cs.initSubStructure(*tempCS, partitioner.chType, partitioner.currArea(), false); - cs.initSubStructure(*bestCS, partitioner.chType, partitioner.currArea(), false); - tempCS->currQP[ChannelType::LUMA] = bestCS->currQP[ChannelType::LUMA] = tempCS->baseQP = bestCS->baseQP = - currQP[ChannelType::LUMA]; - tempCS->prevQP[ChannelType::LUMA] = bestCS->prevQP[ChannelType::LUMA] = prevQP[ChannelType::LUMA]; + cs.initSubStructure(*tempCS, partitioner.chType, partitioner.currArea(), false); + cs.initSubStructure(*bestCS, partitioner.chType, partitioner.currArea(), false); + tempCS->currQP[ChannelType::LUMA] = bestCS->currQP[ChannelType::LUMA] = tempCS->baseQP = bestCS->baseQP = + currQP[ChannelType::LUMA]; + tempCS->prevQP[ChannelType::LUMA] = bestCS->prevQP[ChannelType::LUMA] = prevQP[ChannelType::LUMA]; - xCompressCU(tempCS, bestCS, partitioner); - cs.slice->m_mapPltCost[0].clear(); - cs.slice->m_mapPltCost[1].clear(); - // all signals were already copied during compression if the CTU was split - at this point only the structures are copied to the top level CS - const bool copyUnsplitCTUSignals = bestCS->cus.size() == 1; - cs.useSubStructure(*bestCS, partitioner.chType, CS::getArea(*bestCS, area, partitioner.chType), copyUnsplitCTUSignals, - false, false, copyUnsplitCTUSignals, true); + xCompressCU(tempCS, bestCS, partitioner); + cs.slice->m_mapPltCost[0].clear(); + cs.slice->m_mapPltCost[1].clear(); + // all signals were already copied during compression if the CTU was split - at this point only the structures are copied to the top level CS + const bool copyUnsplitCTUSignals = bestCS->cus.size() == 1; + cs.useSubStructure(*bestCS, partitioner.chType, CS::getArea(*bestCS, area, partitioner.chType), copyUnsplitCTUSignals, + false, false, copyUnsplitCTUSignals, true); - if (CS::isDualITree (cs) && isChromaEnabled (cs.pcv->chrFormat)) - { - m_CABACEstimator->getCtx() = m_CurrCtx->start; + if (CS::isDualITree (cs) && isChromaEnabled (cs.pcv->chrFormat)) + { + m_CABACEstimator->getCtx() = m_CurrCtx->start; - partitioner.initCtu(area, ChannelType::CHROMA, *cs.slice); + partitioner.initCtu(area, ChannelType::CHROMA, *cs.slice); - cs.initSubStructure(*tempCS, partitioner.chType, partitioner.currArea(), false); - cs.initSubStructure(*bestCS, partitioner.chType, partitioner.currArea(), false); - tempCS->currQP[ChannelType::CHROMA] = bestCS->currQP[ChannelType::CHROMA] = tempCS->baseQP = bestCS->baseQP = - currQP[ChannelType::CHROMA]; - tempCS->prevQP[ChannelType::CHROMA] = bestCS->prevQP[ChannelType::CHROMA] = prevQP[ChannelType::CHROMA]; + cs.initSubStructure(*tempCS, partitioner.chType, partitioner.currArea(), false); + cs.initSubStructure(*bestCS, partitioner.chType, partitioner.currArea(), false); + tempCS->currQP[ChannelType::CHROMA] = bestCS->currQP[ChannelType::CHROMA] = tempCS->baseQP = bestCS->baseQP = + currQP[ChannelType::CHROMA]; + tempCS->prevQP[ChannelType::CHROMA] = bestCS->prevQP[ChannelType::CHROMA] = prevQP[ChannelType::CHROMA]; - xCompressCU(tempCS, bestCS, partitioner); + xCompressCU(tempCS, bestCS, partitioner); - const bool copyUnsplitCTUSignals = bestCS->cus.size() == 1; - cs.useSubStructure(*bestCS, partitioner.chType, CS::getArea(*bestCS, area, partitioner.chType), - copyUnsplitCTUSignals, false, false, copyUnsplitCTUSignals, true); - } + const bool copyUnsplitCTUSignals = bestCS->cus.size() == 1; + cs.useSubStructure(*bestCS, partitioner.chType, CS::getArea(*bestCS, area, partitioner.chType), + copyUnsplitCTUSignals, false, false, copyUnsplitCTUSignals, true); + } - if (m_pcEncCfg->getUseRateCtrl()) - { - (m_pcRateCtrl->getRCPic()->getLCU(ctuRsAddr)).m_actualMSE = (double)bestCS->dist / (double)m_pcRateCtrl->getRCPic()->getLCU(ctuRsAddr).m_numberOfPixel; - } - // reset context states and uninit context pointer - m_CABACEstimator->getCtx() = m_CurrCtx->start; - m_CurrCtx = 0; + if (m_pcEncCfg->getUseRateCtrl()) + { + (m_pcRateCtrl->getRCPic()->getLCU(ctuRsAddr)).m_actualMSE = (double)bestCS->dist / (double)m_pcRateCtrl->getRCPic()->getLCU(ctuRsAddr).m_numberOfPixel; + } + // reset context states and uninit context pointer + m_CABACEstimator->getCtx() = m_CurrCtx->start; + m_CurrCtx = 0; - // Ensure that a coding was found - // Selected mode's RD-cost must be not MAX_DOUBLE. - CHECK( bestCS->cus.empty() , "No possible encoding found" ); - CHECK( bestCS->cus[0]->predMode == NUMBER_OF_PREDICTION_MODES, "No possible encoding found" ); - CHECK( bestCS->cost == MAX_DOUBLE , "No possible encoding found" ); + // Ensure that a coding was found + // Selected mode's RD-cost must be not MAX_DOUBLE. + CHECK( bestCS->cus.empty() , "No possible encoding found" ); + CHECK( bestCS->cus[0]->predMode == NUMBER_OF_PREDICTION_MODES, "No possible encoding found" ); + CHECK( bestCS->cost == MAX_DOUBLE , "No possible encoding found" ); + //ALBERTO CONCEPT FORK + exit(0); + } + } + //END ALBERTO CONCEPT FORK + munmap(vector, VECTOR_BYTES); + shm_unlink("/myshm"); } // ==================================================================================================================== @@ -723,6 +768,34 @@ void EncCu::xCompressCU( CodingStructure*& tempCS, CodingStructure*& bestCS, Par m_bestBcwCost.fill(std::numeric_limits::max()); m_bestBcwIdx.fill(BCW_NUM); } + + //ALBERTO CONCEPT FORK + std::cout << "-----------------------------------------------------" << std::endl; + int mode = 5; + int lastMode = m_modeCtrl->currTestMode().type; + /*Se avanza hasta el modo actual requerido por el proceso*/ + while (lastMode != mode) + { + std::cout << "Avanzando modo: " << lastMode << ", buscando modo: " << mode << std::endl; + if (lastMode == mode || !(m_modeCtrl->nextMode(*tempCS, partitioner))) //Si se encuntra mode o no hay mas modes salimos del bucle + { + std::cout << "No otros modos" << ", modo reciente: <" << lastMode << ">" << std::endl; + break; + } + lastMode = m_modeCtrl->currTestMode().type; + } + if (lastMode != mode) + { + std::cout << "El proceso que busca el modo " << mode << " NO ha resultado satisfactorio, no se puede aplicar ese modo para CU" << std::endl; + return; //NO SE PUEDE APLICAR MODO PARA ESTA CU + } + else + { + std::cout << "El proceso que busca el modo " << mode << " SI ha resultado satisfactorio" << std::endl; + } + std::cout << "-----------------------------------------------------" << std::endl; + //END ALBERTO CONCEPT FORK + do { for (int i = compBegin; i < (compBegin + numComp); i++) -- 2.24.1