[x265] [PATCH x265] Use the data structure of analysis-save/load for multi-pass-opt-analysis/multi-pass-opt-distortion
Ashok Kumar Mishra
ashok at multicorewareinc.com
Mon Jun 11 10:53:40 CEST 2018
On Fri, Jun 8, 2018 at 5:44 PM, <aruna at multicorewareinc.com> wrote:
> # HG changeset patch
> # User Aruna Matheswaran <aruna at multicorewareinc.com>
> # Date 1526883919 -19800
> # Mon May 21 11:55:19 2018 +0530
> # Node ID 00eec4796d233e72d6344c7f4c9d5c69a9c55501
> # Parent ed853c4af6710a991d0cdf4bf68e00fe32edaacb
> Use the data structure of analysis-save/load for
> multi-pass-opt-analysis/multi-pass-opt-distortion.
>
> diff -r ed853c4af671 -r 00eec4796d23 source/common/frame.cpp
> --- a/source/common/frame.cpp Fri Apr 06 14:35:00 2018 +0530
> +++ b/source/common/frame.cpp Mon May 21 11:55:19 2018 +0530
> @@ -83,7 +83,7 @@
> m_analysisData.wt = NULL;
> m_analysisData.intraData = NULL;
> m_analysisData.interData = NULL;
> - m_analysis2Pass.analysisFramedata = NULL;
> + m_analysisData.distortionData = NULL;
> }
>
> if (param->bDynamicRefine)
> diff -r ed853c4af671 -r 00eec4796d23 source/common/frame.h
> --- a/source/common/frame.h Fri Apr 06 14:35:00 2018 +0530
> +++ b/source/common/frame.h Mon May 21 11:55:19 2018 +0530
> @@ -109,7 +109,6 @@
> Frame* m_prev;
> x265_param* m_param; // Points to the latest
> param set for the frame.
> x265_analysis_data m_analysisData;
> - x265_analysis_2Pass m_analysis2Pass;
> RcStats* m_rcData;
>
> Event m_copyMVType;
> diff -r ed853c4af671 -r 00eec4796d23 source/common/framedata.h
> --- a/source/common/framedata.h Fri Apr 06 14:35:00 2018 +0530
> +++ b/source/common/framedata.h Mon May 21 11:55:19 2018 +0530
> @@ -203,13 +203,8 @@
> int64_t* sadCost;
> };
>
> -struct analysis2PassFrameData
> +struct analysisDistortionData
> {
> - uint8_t* depth;
> - MV* m_mv[2];
> - int* mvpIdx[2];
> - int32_t* ref[2];
> - uint8_t* modes;
> sse_t* distortion;
> sse_t* ctuDistortion;
> double* scaledDistortion;
> diff -r ed853c4af671 -r 00eec4796d23 source/encoder/analysis.cpp
> --- a/source/encoder/analysis.cpp Fri Apr 06 14:35:00 2018 +0530
> +++ b/source/encoder/analysis.cpp Mon May 21 11:55:19 2018 +0530
> @@ -187,23 +187,20 @@
> for (uint32_t i = 0; i < cuGeom.numPartitions; i++)
> ctu.m_log2CUSize[i] = (uint8_t)m_param->maxLog2CUSize -
> ctu.m_cuDepth[i];
> }
> - if (m_param->analysisMultiPassRefine && m_param->rc.bStatRead)
> + if (m_param->analysisMultiPassRefine && m_param->rc.bStatRead &&
> (m_slice->m_sliceType != I_SLICE))
> {
> - m_multipassAnalysis = (analysis2PassFrameData*)m_
> frame->m_analysis2Pass.analysisFramedata;
> - m_multipassDepth = &m_multipassAnalysis->depth[ctu.m_cuAddr *
> ctu.m_numPartitions];
> - if (m_slice->m_sliceType != I_SLICE)
> + int numPredDir = m_slice->isInterP() ? 1 : 2;
> + m_reuseInterDataCTU = (analysis_inter_data*)m_frame-
> >m_analysisData.interData;
> + for (int dir = 0; dir < numPredDir; dir++)
> {
> - int numPredDir = m_slice->isInterP() ? 1 : 2;
> - for (int dir = 0; dir < numPredDir; dir++)
> - {
> - m_multipassMv[dir] = &m_multipassAnalysis->m_mv[dir][ctu.m_cuAddr
> * ctu.m_numPartitions];
> - m_multipassMvpIdx[dir] = &m_multipassAnalysis->mvpIdx[dir][ctu.m_cuAddr
> * ctu.m_numPartitions];
> - m_multipassRef[dir] = &m_multipassAnalysis->ref[dir][ctu.m_cuAddr
> * ctu.m_numPartitions];
> - }
> - m_multipassModes = &m_multipassAnalysis->modes[ctu.m_cuAddr
> * ctu.m_numPartitions];
> + m_reuseMv[dir] = &m_reuseInterDataCTU->mv[dir][ctu.m_cuAddr
> * ctu.m_numPartitions];
> + m_reuseMvpIdx[dir] = &m_reuseInterDataCTU->mvpIdx[dir][ctu.m_cuAddr
> * ctu.m_numPartitions];
> }
> + m_reuseRef = &m_reuseInterDataCTU->ref[ctu.m_cuAddr *
> ctu.m_numPartitions];
> + m_reuseModes = &m_reuseInterDataCTU->modes[ctu.m_cuAddr *
> ctu.m_numPartitions];
> + m_reuseDepth = &m_reuseInterDataCTU->depth[ctu.m_cuAddr *
> ctu.m_numPartitions];
> }
> -
> +
> if ((m_param->analysisSave || m_param->analysisLoad) &&
> m_slice->m_sliceType != I_SLICE && m_param->analysisReuseLevel > 1 &&
> m_param->analysisReuseLevel < 10)
> {
> int numPredDir = m_slice->isInterP() ? 1 : 2;
> @@ -1284,11 +1281,11 @@
> }
> }
> }
> - if (m_param->analysisMultiPassRefine && m_param->rc.bStatRead &&
> m_multipassAnalysis)
> + if (m_param->analysisMultiPassRefine && m_param->rc.bStatRead &&
> m_reuseInterDataCTU)
> {
> - if (mightNotSplit && depth == m_multipassDepth[cuGeom.
> absPartIdx])
> + if (mightNotSplit && depth == m_reuseDepth[cuGeom.
> absPartIdx])
> {
> - if (m_multipassModes[cuGeom.absPartIdx] == MODE_SKIP)
> + if (m_reuseModes[cuGeom.absPartIdx] == MODE_SKIP)
> {
> md.pred[PRED_MERGE].cu.initSubCU(parentCTU, cuGeom,
> qp);
> md.pred[PRED_SKIP].cu.initSubCU(parentCTU, cuGeom,
> qp);
> @@ -1977,11 +1974,11 @@
> }
> }
>
> - if (m_param->analysisMultiPassRefine && m_param->rc.bStatRead &&
> m_multipassAnalysis)
> + if (m_param->analysisMultiPassRefine && m_param->rc.bStatRead &&
> m_reuseInterDataCTU)
> {
> - if (mightNotSplit && depth == m_multipassDepth[cuGeom.
> absPartIdx])
> + if (mightNotSplit && depth == m_reuseDepth[cuGeom.
> absPartIdx])
> {
> - if (m_multipassModes[cuGeom.absPartIdx] == MODE_SKIP)
> + if (m_reuseModes[cuGeom.absPartIdx] == MODE_SKIP)
> {
> md.pred[PRED_MERGE].cu.initSubCU(parentCTU, cuGeom,
> qp);
> md.pred[PRED_SKIP].cu.initSubCU(parentCTU, cuGeom,
> qp);
> @@ -3015,7 +3012,7 @@
> }
> }
>
> - if (m_param->analysisMultiPassRefine && m_param->rc.bStatRead &&
> m_multipassAnalysis)
> + if (m_param->analysisMultiPassRefine && m_param->rc.bStatRead &&
> m_reuseInterDataCTU)
> {
> uint32_t numPU = interMode.cu.getNumPartInter(0);
> for (uint32_t part = 0; part < numPU; part++)
> @@ -3023,9 +3020,10 @@
> MotionData* bestME = interMode.bestME[part];
> for (int32_t i = 0; i < numPredDir; i++)
> {
> - bestME[i].ref = m_multipassRef[i][cuGeom.absPartIdx];
> - bestME[i].mv = m_multipassMv[i][cuGeom.absPartIdx];
> - bestME[i].mvpIdx = m_multipassMvpIdx[i][cuGeom.
> absPartIdx];
> + int* ref = &m_reuseRef[i * m_frame->m_analysisData.numPartitions
> * m_frame->m_analysisData.numCUsInFrame];
> + bestME[i].ref = ref[cuGeom.absPartIdx];
> + bestME[i].mv = m_reuseMv[i][cuGeom.absPartIdx];
> + bestME[i].mvpIdx = m_reuseMvpIdx[i][cuGeom.absPartIdx];
> }
> }
> }
> @@ -3079,7 +3077,7 @@
> }
> }
>
> - if (m_param->analysisMultiPassRefine && m_param->rc.bStatRead &&
> m_multipassAnalysis)
> + if (m_param->analysisMultiPassRefine && m_param->rc.bStatRead &&
> m_reuseInterDataCTU)
> {
> uint32_t numPU = interMode.cu.getNumPartInter(0);
> for (uint32_t part = 0; part < numPU; part++)
> @@ -3087,9 +3085,10 @@
> MotionData* bestME = interMode.bestME[part];
> for (int32_t i = 0; i < numPredDir; i++)
> {
> - bestME[i].ref = m_multipassRef[i][cuGeom.absPartIdx];
> - bestME[i].mv = m_multipassMv[i][cuGeom.absPartIdx];
> - bestME[i].mvpIdx = m_multipassMvpIdx[i][cuGeom.
> absPartIdx];
> + int* ref = &m_reuseRef[i * m_frame->m_analysisData.numPartitions
> * m_frame->m_analysisData.numCUsInFrame];
> + bestME[i].ref = ref[cuGeom.absPartIdx];
> + bestME[i].mv = m_reuseMv[i][cuGeom.absPartIdx];
> + bestME[i].mvpIdx = m_reuseMvpIdx[i][cuGeom.absPartIdx];
> }
> }
> }
> @@ -3563,10 +3562,10 @@
>
> if (m_param->analysisMultiPassDistortion && m_param->rc.bStatRead)
> {
> - m_multipassAnalysis = (analysis2PassFrameData*)m_
> frame->m_analysis2Pass.analysisFramedata;
> - if ((m_multipassAnalysis->threshold[ctu.m_cuAddr] < 0.9 ||
> m_multipassAnalysis->threshold[ctu.m_cuAddr] > 1.1)
> - && m_multipassAnalysis->highDistortionCtuCount &&
> m_multipassAnalysis->lowDistortionCtuCount)
> - qp += m_multipassAnalysis->offset[ctu.m_cuAddr];
> + analysisDistortionData* distortionData =
> (analysisDistortionData*)m_frame->m_analysisData.distortionData;
> + if ((distortionData->threshold[ctu.m_cuAddr] < 0.9 ||
> distortionData->threshold[ctu.m_cuAddr] > 1.1)
> + && distortionData->highDistortionCtuCount && distortionData->
> lowDistortionCtuCount)
> + qp += distortionData->offset[ctu.m_cuAddr];
> }
>
> int loopIncr;
> diff -r ed853c4af671 -r 00eec4796d23 source/encoder/analysis.h
> --- a/source/encoder/analysis.h Fri Apr 06 14:35:00 2018 +0530
> +++ b/source/encoder/analysis.h Mon May 21 11:55:19 2018 +0530
> @@ -129,18 +129,12 @@
> uint8_t* m_reuseModes;
> uint8_t* m_reusePartSize;
> uint8_t* m_reuseMergeFlag;
> + MV* m_reuseMv[2];
> + uint8_t* m_reuseMvpIdx[2];
>
> uint32_t m_splitRefIdx[4];
> uint64_t* cacheCost;
>
> -
> - analysis2PassFrameData* m_multipassAnalysis;
> - uint8_t* m_multipassDepth;
> - MV* m_multipassMv[2];
> - int* m_multipassMvpIdx[2];
> - int32_t* m_multipassRef[2];
> - uint8_t* m_multipassModes;
> -
> uint8_t m_evaluateInter;
> int32_t m_refineLevel;
>
> diff -r ed853c4af671 -r 00eec4796d23 source/encoder/api.cpp
> --- a/source/encoder/api.cpp Fri Apr 06 14:35:00 2018 +0530
> +++ b/source/encoder/api.cpp Mon May 21 11:55:19 2018 +0530
> @@ -278,7 +278,7 @@
> pic_in->analysisData.wt = NULL;
> pic_in->analysisData.intraData = NULL;
> pic_in->analysisData.interData = NULL;
> - pic_in->analysis2Pass.analysisFramedata = NULL;
> + pic_in->analysisData.distortionData = NULL;
> }
>
> if (pp_nal && numEncoded > 0 && encoder->m_outputCount >=
> encoder->m_latestParam->chunkStart)
> diff -r ed853c4af671 -r 00eec4796d23 source/encoder/encoder.cpp
> --- a/source/encoder/encoder.cpp Fri Apr 06 14:35:00 2018 +0530
> +++ b/source/encoder/encoder.cpp Mon May 21 11:55:19 2018 +0530
> @@ -1262,13 +1262,15 @@
> {
> if (pic_out)
> {
> - pic_out->analysis2Pass.poc = pic_out->poc;
> - pic_out->analysis2Pass.analysisFramedata =
> outFrame->m_analysis2Pass.analysisFramedata;
> + pic_out->analysisData.poc = pic_out->poc;
> + pic_out->analysisData.interData =
> outFrame->m_analysisData.interData;
> + pic_out->analysisData.intraData =
> outFrame->m_analysisData.intraData;
> + pic_out->analysisData.distortionData =
> outFrame->m_analysisData.distortionData;
> }
> - writeAnalysis2PassFile(&outFrame->m_analysis2Pass,
> *outFrame->m_encData, outFrame->m_lowres.sliceType);
> + writeAnalysisFile(&outFrame->m_analysisData,
> *outFrame->m_encData, outFrame->m_lowres.sliceType);
> }
> if (m_param->analysisMultiPassRefine || m_param->
> analysisMultiPassDistortion)
> - freeAnalysis2Pass(&outFrame->m_analysis2Pass,
> outFrame->m_lowres.sliceType);
> + freeAnalysis(&outFrame->m_analysisData,
> outFrame->m_lowres.sliceType);
> if (m_param->internalCsp == X265_CSP_I400)
> {
> if (slice->m_sliceType == P_SLICE)
> @@ -1373,10 +1375,14 @@
> {
> if (m_param->analysisMultiPassRefine || m_param->
> analysisMultiPassDistortion)
> {
> - allocAnalysis2Pass(&frameEnc->m_analysis2Pass,
> frameEnc->m_lowres.sliceType);
> - frameEnc->m_analysis2Pass.poc = frameEnc->m_poc;
> + uint32_t widthInCU = (m_param->sourceWidth +
> m_param->maxCUSize - 1) >> m_param->maxLog2CUSize;
> + uint32_t heightInCU = (m_param->sourceHeight +
> m_param->maxCUSize - 1) >> m_param->maxLog2CUSize;
> + frameEnc->m_analysisData.numCUsInFrame = widthInCU *
> heightInCU;
> + frameEnc->m_analysisData.numPartitions =
> m_param->num4x4Partitions;
> + allocAnalysis(&frameEnc->m_analysisData,
> frameEnc->m_lowres.sliceType);
> + frameEnc->m_analysisData.poc = frameEnc->m_poc;
> if (m_param->rc.bStatRead)
> - readAnalysis2PassFile(&frameEnc->m_analysis2Pass,
> frameEnc->m_poc, frameEnc->m_lowres.sliceType);
> + readAnalysisFile(&frameEnc->m_analysisData,
> frameEnc->m_poc, frameEnc->m_lowres.sliceType);
> }
>
> if (frameEnc->m_reconfigureRc && m_reconfigureRc)
> @@ -3290,68 +3296,84 @@
> }
> }
>
> -void Encoder::allocAnalysis2Pass(x265_analysis_2Pass* analysis, int
> sliceType)
> +void Encoder::allocAnalysis(x265_analysis_data* analysis, int sliceType)
> {
> - analysis->analysisFramedata = NULL;
> - analysis2PassFrameData *analysisFrameData = (analysis2PassFrameData*)
> analysis->analysisFramedata;
> - uint32_t widthInCU = (m_param->sourceWidth + m_param->maxCUSize - 1)
> >> m_param->maxLog2CUSize;
> - uint32_t heightInCU = (m_param->sourceHeight + m_param->maxCUSize -
> 1) >> m_param->maxLog2CUSize;
> -
> - uint32_t numCUsInFrame = widthInCU * heightInCU;
> - CHECKED_MALLOC_ZERO(analysisFrameData, analysis2PassFrameData, 1);
> - CHECKED_MALLOC_ZERO(analysisFrameData->depth, uint8_t,
> m_param->num4x4Partitions * numCUsInFrame);
> - CHECKED_MALLOC_ZERO(analysisFrameData->distortion, sse_t,
> m_param->num4x4Partitions * numCUsInFrame);
> + analysis->interData = analysis->intraData = analysis->distortionData
> = NULL;
> +
> + analysisDistortionData *distortionData = (analysisDistortionData*)
> analysis->distortionData;
> + CHECKED_MALLOC_ZERO(distortionData, analysisDistortionData, 1);
> + CHECKED_MALLOC_ZERO(distortionData->distortion, sse_t,
> analysis->numPartitions * analysis->numCUsInFrame);
> if (m_param->rc.bStatRead)
> {
> - CHECKED_MALLOC_ZERO(analysisFrameData->ctuDistortion, sse_t,
> numCUsInFrame);
> - CHECKED_MALLOC_ZERO(analysisFrameData->scaledDistortion, double,
> numCUsInFrame);
> - CHECKED_MALLOC_ZERO(analysisFrameData->offset, double,
> numCUsInFrame);
> - CHECKED_MALLOC_ZERO(analysisFrameData->threshold, double,
> numCUsInFrame);
> + CHECKED_MALLOC_ZERO(distortionData->ctuDistortion, sse_t,
> analysis->numCUsInFrame);
> + CHECKED_MALLOC_ZERO(distortionData->scaledDistortion, double,
> analysis->numCUsInFrame);
> + CHECKED_MALLOC_ZERO(distortionData->offset, double,
> analysis->numCUsInFrame);
> + CHECKED_MALLOC_ZERO(distortionData->threshold, double,
> analysis->numCUsInFrame);
> }
> - if (!IS_X265_TYPE_I(sliceType))
> + analysis->distortionData = distortionData;
> +
> + if (IS_X265_TYPE_I(sliceType))
> {
> - CHECKED_MALLOC_ZERO(analysisFrameData->m_mv[0], MV,
> m_param->num4x4Partitions * numCUsInFrame);
> - CHECKED_MALLOC_ZERO(analysisFrameData->m_mv[1], MV,
> m_param->num4x4Partitions * numCUsInFrame);
> - CHECKED_MALLOC_ZERO(analysisFrameData->mvpIdx[0], int,
> m_param->num4x4Partitions * numCUsInFrame);
> - CHECKED_MALLOC_ZERO(analysisFrameData->mvpIdx[1], int,
> m_param->num4x4Partitions * numCUsInFrame);
> - CHECKED_MALLOC_ZERO(analysisFrameData->ref[0], int32_t,
> m_param->num4x4Partitions * numCUsInFrame);
> - CHECKED_MALLOC_ZERO(analysisFrameData->ref[1], int32_t,
> m_param->num4x4Partitions * numCUsInFrame);
> - CHECKED_MALLOC(analysisFrameData->modes, uint8_t,
> m_param->num4x4Partitions * numCUsInFrame);
> + analysis_intra_data *intraData = (analysis_intra_data*)
> analysis->intraData;
> + CHECKED_MALLOC_ZERO(intraData, analysis_intra_data, 1);
> + CHECKED_MALLOC(intraData->depth, uint8_t,
> analysis->numPartitions * analysis->numCUsInFrame);
> + analysis->intraData = intraData;
> }
> -
> - analysis->analysisFramedata = analysisFrameData;
> -
> + else
> + {
> + analysis_inter_data *interData = (analysis_inter_data*)
> analysis->interData;
> + CHECKED_MALLOC_ZERO(interData, analysis_inter_data, 1);
> + CHECKED_MALLOC_ZERO(interData->depth, uint8_t,
> analysis->numPartitions * analysis->numCUsInFrame);
> + CHECKED_MALLOC(interData->modes, uint8_t,
> analysis->numPartitions * analysis->numCUsInFrame);
> + CHECKED_MALLOC_ZERO(interData->mvpIdx[0], uint8_t,
> analysis->numPartitions * analysis->numCUsInFrame);
> + CHECKED_MALLOC_ZERO(interData->mvpIdx[1], uint8_t,
> analysis->numPartitions * analysis->numCUsInFrame);
> + CHECKED_MALLOC_ZERO(interData->mv[0], MV,
> analysis->numPartitions * analysis->numCUsInFrame);
> + CHECKED_MALLOC_ZERO(interData->mv[1], MV,
> analysis->numPartitions * analysis->numCUsInFrame);
> + CHECKED_MALLOC_ZERO(interData->ref, int32_t, 2 *
> analysis->numPartitions * analysis->numCUsInFrame);
> + analysis->interData = interData;
> + }
> return;
>
> fail:
> - freeAnalysis2Pass(analysis, sliceType);
> + freeAnalysis(analysis, sliceType);
> m_aborted = true;
> }
>
> -void Encoder::freeAnalysis2Pass(x265_analysis_2Pass* analysis, int
> sliceType)
> +void Encoder::freeAnalysis(x265_analysis_data* analysis, int sliceType)
> {
> - if (analysis->analysisFramedata)
> + if (analysis->distortionData)
> {
> - X265_FREE(((analysis2PassFrameData*)
> analysis->analysisFramedata)->depth);
> - X265_FREE(((analysis2PassFrameData*)
> analysis->analysisFramedata)->distortion);
> + X265_FREE(((analysisDistortionData*)analysis->distortionData)->
> distortion);
> if (m_param->rc.bStatRead)
> {
> - X265_FREE(((analysis2PassFrameData*)
> analysis->analysisFramedata)->ctuDistortion);
> - X265_FREE(((analysis2PassFrameData*)
> analysis->analysisFramedata)->scaledDistortion);
> - X265_FREE(((analysis2PassFrameData*)
> analysis->analysisFramedata)->offset);
> - X265_FREE(((analysis2PassFrameData*)
> analysis->analysisFramedata)->threshold);
> + X265_FREE(((analysisDistortionData*)
> analysis->distortionData)->ctuDistortion);
> + X265_FREE(((analysisDistortionData*)
> analysis->distortionData)->scaledDistortion);
> + X265_FREE(((analysisDistortionData*)
> analysis->distortionData)->offset);
> + X265_FREE(((analysisDistortionData*)
> analysis->distortionData)->threshold);
> }
> - if (!IS_X265_TYPE_I(sliceType))
> + X265_FREE(analysis->distortionData);
> + }
> + if (IS_X265_TYPE_I(sliceType))
> + {
> + if (analysis->intraData)
> {
> - X265_FREE(((analysis2PassFrameData*)
> analysis->analysisFramedata)->m_mv[0]);
> - X265_FREE(((analysis2PassFrameData*)
> analysis->analysisFramedata)->m_mv[1]);
> - X265_FREE(((analysis2PassFrameData*)
> analysis->analysisFramedata)->mvpIdx[0]);
> - X265_FREE(((analysis2PassFrameData*)
> analysis->analysisFramedata)->mvpIdx[1]);
> - X265_FREE(((analysis2PassFrameData*)
> analysis->analysisFramedata)->ref[0]);
> - X265_FREE(((analysis2PassFrameData*)
> analysis->analysisFramedata)->ref[1]);
> - X265_FREE(((analysis2PassFrameData*)
> analysis->analysisFramedata)->modes);
> + X265_FREE(((analysis_intra_data*)analysis->intraData)->
> depth);
> + X265_FREE(analysis->intraData);
> }
> - X265_FREE(analysis->analysisFramedata);
> + }
> + else
> + {
> + if (analysis->interData)
> + {
> + X265_FREE(((analysis_inter_data*)analysis->interData)->
> mvpIdx[0]);
> + X265_FREE(((analysis_inter_data*)analysis->interData)->
> mvpIdx[1]);
> + X265_FREE(((analysis_inter_data*)analysis->interData)->
> mv[0]);
> + X265_FREE(((analysis_inter_data*)analysis->interData)->
> mv[1]);
> + X265_FREE(((analysis_inter_data*)analysis->interData)->ref);
> + X265_FREE(((analysis_inter_data*)analysis->interData)->
> depth);
> + X265_FREE(((analysis_inter_data*)analysis->interData)->
> modes);
> + X265_FREE(analysis->interData);
> + }
> }
> }
>
> @@ -4188,23 +4210,19 @@
> return SIZE_2Nx2N;
> }
>
> -void Encoder::readAnalysis2PassFile(x265_analysis_2Pass* analysis2Pass,
> int curPoc, int sliceType)
> +void Encoder::readAnalysisFile(x265_analysis_data* analysis, int curPoc,
> int sliceType)
> {
>
> #define X265_FREAD(val, size, readSize, fileOffset)\
> if (fread(val, size, readSize, fileOffset) != readSize)\
> {\
> x265_log(NULL, X265_LOG_ERROR, "Error reading analysis 2 pass
> data\n"); \
> - freeAnalysis2Pass(analysis2Pass, sliceType); \
> + freeAnalysis(analysis, sliceType); \
> m_aborted = true; \
> return; \
> }\
>
> uint32_t depthBytes = 0;
> - uint32_t widthInCU = (m_param->sourceWidth + m_param->maxCUSize - 1)
> >> m_param->maxLog2CUSize;
> - uint32_t heightInCU = (m_param->sourceHeight + m_param->maxCUSize -
> 1) >> m_param->maxLog2CUSize;
> - uint32_t numCUsInFrame = widthInCU * heightInCU;
> -
> int poc; uint32_t frameRecordSize;
> X265_FREAD(&frameRecordSize, sizeof(uint32_t), 1, m_analysisFileIn);
> X265_FREAD(&depthBytes, sizeof(uint32_t), 1, m_analysisFileIn);
> @@ -4213,11 +4231,11 @@
> if (poc != curPoc || feof(m_analysisFileIn))
> {
> x265_log(NULL, X265_LOG_WARNING, "Error reading analysis 2 pass
> data: Cannot find POC %d\n", curPoc);
> - freeAnalysis2Pass(analysis2Pass, sliceType);
> + freeAnalysis(analysis, sliceType);
> return;
> }
> /* Now arrived at the right frame, read the record */
> - analysis2Pass->frameRecordSize = frameRecordSize;
> + analysis->frameRecordSize = frameRecordSize;
> uint8_t* tempBuf = NULL, *depthBuf = NULL;
> sse_t *tempdistBuf = NULL, *distortionBuf = NULL;
> tempBuf = X265_MALLOC(uint8_t, depthBytes);
> @@ -4226,76 +4244,85 @@
> X265_FREAD(tempdistBuf, sizeof(sse_t), depthBytes, m_analysisFileIn);
> depthBuf = tempBuf;
> distortionBuf = tempdistBuf;
> - analysis2PassFrameData* analysisFrameData = (analysis2PassFrameData*)
> analysis2Pass->analysisFramedata;
> + x265_analysis_data *analysisData = (x265_analysis_data*)analysis;
> + analysis_intra_data *intraData = (analysis_intra_data
> *)analysisData->intraData;
> + analysis_inter_data *interData = (analysis_inter_data
> *)analysisData->interData;
> + analysisDistortionData *distortionData = (analysisDistortionData
> *)analysisData->distortionData;
> +
> size_t count = 0;
> uint32_t ctuCount = 0;
> double sum = 0, sqrSum = 0;
> for (uint32_t d = 0; d < depthBytes; d++)
> {
> - int bytes = m_param->num4x4Partitions >> (depthBuf[d] * 2);
> - memset(&analysisFrameData->depth[count], depthBuf[d], bytes);
> - analysisFrameData->distortion[count] = distortionBuf[d];
> - analysisFrameData->ctuDistortion[ctuCount] +=
> analysisFrameData->distortion[count];
> + int bytes = analysis->numPartitions >> (depthBuf[d] * 2);
> + if (IS_X265_TYPE_I(sliceType))
> + memset(&intraData->depth[count], depthBuf[d], bytes);
> + else
> + memset(&interData->depth[count], depthBuf[d], bytes);
> + distortionData->distortion[count] = distortionBuf[d];
> + distortionData->ctuDistortion[ctuCount] +=
> distortionData->distortion[count];
> count += bytes;
> - if ((count % (unsigned)m_param->num4x4Partitions) == 0)
> + if ((count % (unsigned)analysis->numPartitions) == 0)
> {
> - analysisFrameData->scaledDistortion[ctuCount] =
> X265_LOG2(X265_MAX(analysisFrameData->ctuDistortion[ctuCount], 1));
> - sum += analysisFrameData->scaledDistortion[ctuCount];
> - sqrSum += analysisFrameData->scaledDistortion[ctuCount] *
> analysisFrameData->scaledDistortion[ctuCount];
> + distortionData->scaledDistortion[ctuCount] =
> X265_LOG2(X265_MAX(distortionData->ctuDistortion[ctuCount], 1));
> + sum += distortionData->scaledDistortion[ctuCount];
> + sqrSum += distortionData->scaledDistortion[ctuCount] *
> distortionData->scaledDistortion[ctuCount];
> ctuCount++;
> }
> }
> - double avg = sum / numCUsInFrame;
> - analysisFrameData->sdDistortion = pow(((sqrSum / numCUsInFrame) -
> (avg * avg)), 0.5);
> - analysisFrameData->averageDistortion = avg;
> - analysisFrameData->highDistortionCtuCount = analysisFrameData->lowDistortionCtuCount
> = 0;
> - for (uint32_t i = 0; i < numCUsInFrame; ++i)
> + double avg = sum / analysis->numCUsInFrame;
> + distortionData->sdDistortion = pow(((sqrSum /
> analysis->numCUsInFrame) - (avg * avg)), 0.5);
> + distortionData->averageDistortion = avg;
> + distortionData->highDistortionCtuCount = distortionData->lowDistortionCtuCount
> = 0;
> + for (uint32_t i = 0; i < analysis->numCUsInFrame; ++i)
> {
> - analysisFrameData->threshold[i] = analysisFrameData->scaledDistortion[i]
> / analysisFrameData->averageDistortion;
> - analysisFrameData->offset[i] = (analysisFrameData->averageDistortion
> - analysisFrameData->scaledDistortion[i]) / analysisFrameData->
> sdDistortion;
> - if (analysisFrameData->threshold[i] < 0.9 &&
> analysisFrameData->offset[i] >= 1)
> - analysisFrameData->lowDistortionCtuCount++;
> - else if (analysisFrameData->threshold[i] > 1.1 &&
> analysisFrameData->offset[i] <= -1)
> - analysisFrameData->highDistortionCtuCount++;
> + distortionData->threshold[i] = distortionData->scaledDistortion[i]
> / distortionData->averageDistortion;
> + distortionData->offset[i] = (distortionData->averageDistortion -
> distortionData->scaledDistortion[i]) / distortionData->sdDistortion;
> + if (distortionData->threshold[i] < 0.9 &&
> distortionData->offset[i] >= 1)
> + distortionData->lowDistortionCtuCount++;
> + else if (distortionData->threshold[i] > 1.1 &&
> distortionData->offset[i] <= -1)
> + distortionData->highDistortionCtuCount++;
> }
> if (!IS_X265_TYPE_I(sliceType))
> {
> MV *tempMVBuf[2], *MVBuf[2];
> - int32_t *tempRefBuf[2], *refBuf[2];
> - int *tempMvpBuf[2], *mvpBuf[2];
> + int32_t *tempRefBuf, *refBuf;
> + uint8_t *tempMvpBuf[2], *mvpBuf[2];
> uint8_t* tempModeBuf = NULL, *modeBuf = NULL;
> -
> int numDir = sliceType == X265_TYPE_P ? 1 : 2;
> + tempRefBuf = X265_MALLOC(int32_t, numDir * depthBytes);
> +
> for (int i = 0; i < numDir; i++)
> {
> tempMVBuf[i] = X265_MALLOC(MV, depthBytes);
> X265_FREAD(tempMVBuf[i], sizeof(MV), depthBytes,
> m_analysisFileIn);
> MVBuf[i] = tempMVBuf[i];
> - tempMvpBuf[i] = X265_MALLOC(int, depthBytes);
> - X265_FREAD(tempMvpBuf[i], sizeof(int), depthBytes,
> m_analysisFileIn);
> + tempMvpBuf[i] = X265_MALLOC(uint8_t, depthBytes);
> + X265_FREAD(tempMvpBuf[i], sizeof(uint8_t), depthBytes,
> m_analysisFileIn);
> mvpBuf[i] = tempMvpBuf[i];
> - tempRefBuf[i] = X265_MALLOC(int32_t, depthBytes);
> - X265_FREAD(tempRefBuf[i], sizeof(int32_t), depthBytes,
> m_analysisFileIn);
> - refBuf[i] = tempRefBuf[i];
> + X265_FREAD(&tempRefBuf[i*depthBytes], sizeof(int32_t),
> depthBytes, m_analysisFileIn);
> }
> + refBuf = tempRefBuf;
> tempModeBuf = X265_MALLOC(uint8_t, depthBytes);
> X265_FREAD(tempModeBuf, sizeof(uint8_t), depthBytes,
> m_analysisFileIn);
> modeBuf = tempModeBuf;
> -
> +
> count = 0;
> +
> for (uint32_t d = 0; d < depthBytes; d++)
> {
> - size_t bytes = m_param->num4x4Partitions >> (depthBuf[d] * 2);
> + size_t bytes = analysis->numPartitions >> (depthBuf[d] * 2);
> for (int i = 0; i < numDir; i++)
> {
> + int32_t* ref = &((analysis_inter_data*)analysis->interData)->ref[i
> * analysis->numPartitions * analysis->numCUsInFrame];
> for (size_t j = count, k = 0; k < bytes; j++, k++)
> {
> - memcpy(&((analysis2PassFrameData*)analysis2Pass->
> analysisFramedata)->m_mv[i][j], MVBuf[i] + d, sizeof(MV));
> - memcpy(&((analysis2PassFrameData*)analysis2Pass->
> analysisFramedata)->mvpIdx[i][j], mvpBuf[i] + d, sizeof(int));
> - memcpy(&((analysis2PassFrameData*)analysis2Pass->analysisFramedata)->ref[i][j],
> refBuf[i] + d, sizeof(int32_t));
> + memcpy(&((analysis_inter_data*
> )analysis->interData)->mv[i][j], MVBuf[i] + d, sizeof(MV));
> + memcpy(&((analysis_inter_data*
> )analysis->interData)->mvpIdx[i][j], mvpBuf[i] + d, sizeof(uint8_t));
> + memcpy(&ref[j], refBuf + (i * depthBytes) + d,
> sizeof(int32_t));
> }
> }
> - memset(&((analysis2PassFrameData *)analysis2Pass->
> analysisFramedata)->modes[count], modeBuf[d], bytes);
> + memset(&((analysis_inter_data *)analysis->interData)->modes[count],
> modeBuf[d], bytes);
> count += bytes;
> }
>
> @@ -4303,8 +4330,8 @@
> {
> X265_FREE(tempMVBuf[i]);
> X265_FREE(tempMvpBuf[i]);
> - X265_FREE(tempRefBuf[i]);
> }
> + X265_FREE(tempRefBuf);
> X265_FREE(tempModeBuf);
> }
> X265_FREE(tempBuf);
> @@ -4507,24 +4534,25 @@
> #undef X265_FWRITE
> }
>
> -void Encoder::writeAnalysis2PassFile(x265_analysis_2Pass* analysis2Pass,
> FrameData &curEncData, int slicetype)
> +void Encoder::writeAnalysisFile(x265_analysis_data* analysis, FrameData
> &curEncData, int slicetype)
> {
> #define X265_FWRITE(val, size, writeSize, fileOffset)\
> if (fwrite(val, size, writeSize, fileOffset) < writeSize)\
> {\
> x265_log(NULL, X265_LOG_ERROR, "Error writing analysis 2 pass
> data\n"); \
> - freeAnalysis2Pass(analysis2Pass, slicetype); \
> + freeAnalysis(analysis, slicetype); \
> m_aborted = true; \
> return; \
> }\
>
> uint32_t depthBytes = 0;
> - uint32_t widthInCU = (m_param->sourceWidth + m_param->maxCUSize - 1)
> >> m_param->maxLog2CUSize;
> - uint32_t heightInCU = (m_param->sourceHeight + m_param->maxCUSize -
> 1) >> m_param->maxLog2CUSize;
> - uint32_t numCUsInFrame = widthInCU * heightInCU;
> - analysis2PassFrameData* analysisFrameData = (analysis2PassFrameData*)
> analysis2Pass->analysisFramedata;
> -
> - for (uint32_t cuAddr = 0; cuAddr < numCUsInFrame; cuAddr++)
> + x265_analysis_data *analysisData = (x265_analysis_data*)analysis;
> + analysis_intra_data *intraData = (analysis_intra_data
> *)analysisData->intraData;
> + analysis_inter_data *interData = (analysis_inter_data
> *)analysisData->interData;
> + analysisDistortionData *distortionData = (analysisDistortionData
> *)analysisData->distortionData;
> +
> +
> + for (uint32_t cuAddr = 0; cuAddr < analysis->numCUsInFrame; cuAddr++)
> {
> uint8_t depth = 0;
>
> @@ -4533,37 +4561,42 @@
> for (uint32_t absPartIdx = 0; absPartIdx < ctu->m_numPartitions;
> depthBytes++)
> {
> depth = ctu->m_cuDepth[absPartIdx];
> - analysisFrameData->depth[depthBytes] = depth;
> - analysisFrameData->distortion[depthBytes] =
> ctu->m_distortion[absPartIdx];
> + if (curEncData.m_slice->m_sliceType == I_SLICE)
> + intraData->depth[depthBytes] = depth;
> + else
> + interData->depth[depthBytes] = depth;
> + distortionData->distortion[depthBytes] =
> ctu->m_distortion[absPartIdx];
> absPartIdx += ctu->m_numPartitions >> (depth * 2);
> }
> }
>
> if (curEncData.m_slice->m_sliceType != I_SLICE)
> {
> + int32_t* ref[2];
> + ref[0] = ((analysis_inter_data*)analysis->interData)->ref;
> + ref[1] = &((analysis_inter_data*)analysis->interData)->ref[analysis->numPartitions
> * analysis->numCUsInFrame];
> depthBytes = 0;
> - for (uint32_t cuAddr = 0; cuAddr < numCUsInFrame; cuAddr++)
> + for (uint32_t cuAddr = 0; cuAddr < analysis->numCUsInFrame;
> cuAddr++)
> {
> uint8_t depth = 0;
> uint8_t predMode = 0;
>
> CUData* ctu = curEncData.getPicCTU(cuAddr);
> -
> for (uint32_t absPartIdx = 0; absPartIdx <
> ctu->m_numPartitions; depthBytes++)
> {
> depth = ctu->m_cuDepth[absPartIdx];
> - analysisFrameData->m_mv[0][depthBytes] =
> ctu->m_mv[0][absPartIdx];
> - analysisFrameData->mvpIdx[0][depthBytes] =
> ctu->m_mvpIdx[0][absPartIdx];
> - analysisFrameData->ref[0][depthBytes] =
> ctu->m_refIdx[0][absPartIdx];
> + interData->mv[0][depthBytes] = ctu->m_mv[0][absPartIdx];
> + interData->mvpIdx[0][depthBytes] =
> ctu->m_mvpIdx[0][absPartIdx];
> + ref[0][depthBytes] = ctu->m_refIdx[0][absPartIdx];
> predMode = ctu->m_predMode[absPartIdx];
> if (ctu->m_refIdx[1][absPartIdx] != -1)
> {
> - analysisFrameData->m_mv[1][depthBytes] =
> ctu->m_mv[1][absPartIdx];
> - analysisFrameData->mvpIdx[1][depthBytes] =
> ctu->m_mvpIdx[1][absPartIdx];
> - analysisFrameData->ref[1][depthBytes] =
> ctu->m_refIdx[1][absPartIdx];
> + interData->mv[1][depthBytes] =
> ctu->m_mv[1][absPartIdx];
> + interData->mvpIdx[1][depthBytes] =
> ctu->m_mvpIdx[1][absPartIdx];
> + ref[1][depthBytes] = ctu->m_refIdx[1][absPartIdx];
> predMode = 4; // used as indiacator if the block is
> coded as bidir
> }
> - analysisFrameData->modes[depthBytes] = predMode;
> + interData->modes[depthBytes] = predMode;
>
> absPartIdx += ctu->m_numPartitions >> (depth * 2);
> }
> @@ -4571,34 +4604,40 @@
> }
>
> /* calculate frameRecordSize */
> - analysis2Pass->frameRecordSize = sizeof(analysis2Pass->frameRecordSize)
> + sizeof(depthBytes) + sizeof(analysis2Pass->poc);
> -
> - analysis2Pass->frameRecordSize += depthBytes * sizeof(uint8_t);
> - analysis2Pass->frameRecordSize += depthBytes * sizeof(sse_t);
> + analysis->frameRecordSize = sizeof(analysis->frameRecordSize) +
> sizeof(depthBytes) + sizeof(analysis->poc);
> + analysis->frameRecordSize += depthBytes * sizeof(uint8_t);
> + analysis->frameRecordSize += depthBytes * sizeof(sse_t);
> if (curEncData.m_slice->m_sliceType != I_SLICE)
> {
> int numDir = (curEncData.m_slice->m_sliceType == P_SLICE) ? 1 :
> 2;
> - analysis2Pass->frameRecordSize += depthBytes * sizeof(MV) *
> numDir;
> - analysis2Pass->frameRecordSize += depthBytes * sizeof(int32_t) *
> numDir;
> - analysis2Pass->frameRecordSize += depthBytes * sizeof(int) *
> numDir;
> - analysis2Pass->frameRecordSize += depthBytes * sizeof(uint8_t);
> + analysis->frameRecordSize += depthBytes * sizeof(MV) * numDir;
> + analysis->frameRecordSize += depthBytes * sizeof(int32_t) *
> numDir;
> + analysis->frameRecordSize += depthBytes * sizeof(uint8_t) *
> numDir;
> + analysis->frameRecordSize += depthBytes * sizeof(uint8_t);
> }
> - X265_FWRITE(&analysis2Pass->frameRecordSize, sizeof(uint32_t), 1,
> m_analysisFileOut);
> + X265_FWRITE(&analysis->frameRecordSize, sizeof(uint32_t), 1,
> m_analysisFileOut);
> X265_FWRITE(&depthBytes, sizeof(uint32_t), 1, m_analysisFileOut);
> - X265_FWRITE(&analysis2Pass->poc, sizeof(uint32_t), 1,
> m_analysisFileOut);
> -
> - X265_FWRITE(analysisFrameData->depth, sizeof(uint8_t), depthBytes,
> m_analysisFileOut);
> - X265_FWRITE(analysisFrameData->distortion, sizeof(sse_t),
> depthBytes, m_analysisFileOut);
> + X265_FWRITE(&analysis->poc, sizeof(uint32_t), 1, m_analysisFileOut);
> + if (curEncData.m_slice->m_sliceType == I_SLICE)
> + {
> + X265_FWRITE(((analysis_intra_data*)analysis->intraData)->depth,
> sizeof(uint8_t), depthBytes, m_analysisFileOut);
> + }
> + else
> + {
> + X265_FWRITE(((analysis_inter_data*)analysis->interData)->depth,
> sizeof(uint8_t), depthBytes, m_analysisFileOut);
> + }
> + X265_FWRITE(distortionData->distortion, sizeof(sse_t), depthBytes,
> m_analysisFileOut);
> if (curEncData.m_slice->m_sliceType != I_SLICE)
> {
> int numDir = curEncData.m_slice->m_sliceType == P_SLICE ? 1 : 2;
> for (int i = 0; i < numDir; i++)
> {
> - X265_FWRITE(analysisFrameData->m_mv[i], sizeof(MV),
> depthBytes, m_analysisFileOut);
> - X265_FWRITE(analysisFrameData->mvpIdx[i], sizeof(int),
> depthBytes, m_analysisFileOut);
> - X265_FWRITE(analysisFrameData->ref[i], sizeof(int32_t),
> depthBytes, m_analysisFileOut);
> + int32_t* ref = &((analysis_inter_data*)analysis->interData)->ref[i
> * analysis->numPartitions * analysis->numCUsInFrame];
> + X265_FWRITE(interData->mv[i], sizeof(MV), depthBytes,
> m_analysisFileOut);
> + X265_FWRITE(interData->mvpIdx[i], sizeof(uint8_t),
> depthBytes, m_analysisFileOut);
> + X265_FWRITE(ref, sizeof(int32_t), depthBytes,
> m_analysisFileOut);
> }
> - X265_FWRITE(analysisFrameData->modes, sizeof(uint8_t),
> depthBytes, m_analysisFileOut);
> + X265_FWRITE(((analysis_inter_data*)analysis->interData)->modes,
> sizeof(uint8_t), depthBytes, m_analysisFileOut);
> }
> #undef X265_FWRITE
> }
> diff -r ed853c4af671 -r 00eec4796d23 source/encoder/encoder.h
> --- a/source/encoder/encoder.h Fri Apr 06 14:35:00 2018 +0530
> +++ b/source/encoder/encoder.h Mon May 21 11:55:19 2018 +0530
> @@ -275,11 +275,13 @@
>
> void allocAnalysis(x265_analysis_data* analysis);
>
> + void allocAnalysis(x265_analysis_data* analysis, int sliceType);
> +
> void freeAnalysis(x265_analysis_data* analysis);
>
> - void allocAnalysis2Pass(x265_analysis_2Pass* analysis, int
> sliceType);
> + void freeAnalysis(x265_analysis_data* analysis, int sliceType);
>
> - void freeAnalysis2Pass(x265_analysis_2Pass* analysis, int sliceType);
> + void readAnalysisFile(x265_analysis_data* analysis, int poc, int
> sliceType);
>
> void readAnalysisFile(x265_analysis_data* analysis, int poc, const
> x265_picture* picIn, int paramBytes);
>
> @@ -289,9 +291,10 @@
>
> int getPuShape(puOrientation* puOrient, int partSize, int numCTU);
>
> - void writeAnalysisFile(x265_analysis_data* pic, FrameData
> &curEncData);
> - void readAnalysis2PassFile(x265_analysis_2Pass* analysis2Pass, int
> poc, int sliceType);
> - void writeAnalysis2PassFile(x265_analysis_2Pass* analysis2Pass,
> FrameData &curEncData, int slicetype);
> + void writeAnalysisFile(x265_analysis_data* analysis, FrameData
> &curEncData);
> +
> + void writeAnalysisFile(x265_analysis_data* analysis, FrameData
> &curEncData, int slicetype);
> +
> void finishFrameStats(Frame* pic, FrameEncoder *curEncoder,
> x265_frame_stats* frameStats, int inPoc);
>
> int validateAnalysisData(x265_analysis_data* analysis, int
> readWriteFlag);
> diff -r ed853c4af671 -r 00eec4796d23 source/x265.h
> --- a/source/x265.h Fri Apr 06 14:35:00 2018 +0530
> +++ b/source/x265.h Mon May 21 11:55:19 2018 +0530
> @@ -143,6 +143,7 @@
> x265_lookahead_data lookahead;
> uint8_t* modeFlag[2];
> x265_analysis_validate saveParam;
> + void* distortionData;
> } x265_analysis_data;
>
> /* cu statistics */
> @@ -171,14 +172,6 @@
> /* All the above values will add up to 100%. */
> } x265_pu_stats;
>
> -
> -typedef struct x265_analysis_2Pass
> -{
> - uint32_t poc;
> - uint32_t frameRecordSize;
> - void* analysisFramedata;
> -}x265_analysis_2Pass;
> -
> /* Frame level statistics */
> typedef struct x265_frame_stats
> {
> @@ -383,8 +376,6 @@
>
> int height;
>
> - x265_analysis_2Pass analysis2Pass;
> -
> // pts is reordered in the order of encoding.
> int64_t reorderedPts;
> } x265_picture;
>
> _______________________________________________
> x265-devel mailing list
> x265-devel at videolan.org
> https://mailman.videolan.org/listinfo/x265-devel
>
>
Pushed.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/x265-devel/attachments/20180611/217e2387/attachment-0001.html>
More information about the x265-devel
mailing list