hevc: clean up mvs(cherry picked from commit 955317c09b877a513d3fcfcd1615909b2f4f651c)
authorMickaël Raulet <mraulet@insa-rennes.fr>
Sun, 27 Oct 2013 15:50:20 +0000 (16:50 +0100)
committerMichael Niedermayer <michaelni@gmx.at>
Sun, 27 Oct 2013 22:48:43 +0000 (23:48 +0100)
Decreases the difference to Anton Khirnovs patch v5

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
libavcodec/hevc_mvs.c

index 6730685..c6e229f 100644 (file)
@@ -235,11 +235,10 @@ static int derive_temporal_colocated_mvs(HEVCContext *s, MvField temp_col,
 #define TAB_MVF_PU(v) \
     TAB_MVF(x##v##_pu, y##v##_pu)
 
-#define DERIVE_TEMPORAL_COLOCATED_MVS(v) \
+#define DERIVE_TEMPORAL_COLOCATED_MVS \
     derive_temporal_colocated_mvs(s, temp_col, \
                                   refIdxLx, mvLXCol, X, colPic, \
-                                  ff_hevc_get_ref_list(s, ref, \
-                                                       x##v, y##v))
+                                  ff_hevc_get_ref_list(s, ref, x, y))
 
 /*
  * 8.5.3.1.7  temporal luma motion vector prediction
@@ -250,13 +249,9 @@ static int temporal_luma_motion_vector(HEVCContext *s, int x0, int y0,
 {
     MvField *tab_mvf;
     MvField temp_col;
-    int xPRb, yPRb;
-    int xPRb_pu;
-    int yPRb_pu;
-    int xPCtr, yPCtr;
-    int xPCtr_pu;
-    int yPCtr_pu;
-    int min_pu_width = s->sps->width >> s->sps->log2_min_pu_size;
+    int x, y;
+    int x_pu, y_pu;
+    int min_pu_width = s->sps->min_pu_width;
     int availableFlagLXCol = 0;
     int colPic;
 
@@ -269,37 +264,33 @@ static int temporal_luma_motion_vector(HEVCContext *s, int x0, int y0,
     colPic  = ref->poc;
 
     //bottom right collocated motion vector
-    xPRb = x0 + nPbW;
-    yPRb = y0 + nPbH;
+    x = x0 + nPbW;
+    y = y0 + nPbH;
 
     if (s->threads_type == FF_THREAD_FRAME )
         ff_thread_await_progress(&ref->tf, INT_MAX, 0);
     if (tab_mvf &&
-        y0 >> s->sps->log2_ctb_size == yPRb >> s->sps->log2_ctb_size &&
-        yPRb < s->sps->height &&
-        xPRb < s->sps->width) {
-        xPRb = ((xPRb >> 4) << 4);
-        yPRb = ((yPRb >> 4) << 4);
-        xPRb_pu = xPRb >> s->sps->log2_min_pu_size;
-        yPRb_pu = yPRb >> s->sps->log2_min_pu_size;
-        temp_col = TAB_MVF_PU(PRb);
-        availableFlagLXCol = DERIVE_TEMPORAL_COLOCATED_MVS(PRb);
-    } else {
-        mvLXCol->x = 0;
-        mvLXCol->y = 0;
-        availableFlagLXCol = 0;
+        (y0 >> s->sps->log2_ctb_size) == (y >> s->sps->log2_ctb_size) &&
+        y < s->sps->height &&
+        x < s->sps->width) {
+        x = ((x >> 4) << 4);
+        y = ((y >> 4) << 4);
+        x_pu = x >> s->sps->log2_min_pu_size;
+        y_pu = y >> s->sps->log2_min_pu_size;
+        temp_col = TAB_MVF(x_pu, y_pu);
+        availableFlagLXCol = DERIVE_TEMPORAL_COLOCATED_MVS;
     }
 
     // derive center collocated motion vector
-    if (tab_mvf && availableFlagLXCol == 0) {
-        xPCtr = x0 + (nPbW >> 1);
-        yPCtr = y0 + (nPbH >> 1);
-        xPCtr = ((xPCtr >> 4) << 4);
-        yPCtr = ((yPCtr >> 4) << 4);
-        xPCtr_pu = xPCtr >> s->sps->log2_min_pu_size;
-        yPCtr_pu = yPCtr >> s->sps->log2_min_pu_size;
-        temp_col = TAB_MVF_PU(PCtr);
-        availableFlagLXCol = DERIVE_TEMPORAL_COLOCATED_MVS(PCtr);
+    if (tab_mvf && !availableFlagLXCol) {
+        x = x0 + (nPbW >> 1);
+        y = y0 + (nPbH >> 1);
+        x = ((x >> 4) << 4);
+        y = ((y >> 4) << 4);
+        x_pu = x >> s->sps->log2_min_pu_size;
+        y_pu = y >> s->sps->log2_min_pu_size;
+        temp_col = TAB_MVF(x_pu, y_pu);
+        availableFlagLXCol = DERIVE_TEMPORAL_COLOCATED_MVS;
     }
     return availableFlagLXCol;
 }
@@ -323,82 +314,62 @@ static void derive_spatial_merge_candidates(HEVCContext *s, int x0, int y0,
                                             int singleMCLFlag, int part_idx,
                                             struct MvField mergecandlist[])
 {
-    HEVCLocalContext *lc = s->HEVClc;
+    HEVCLocalContext *lc   = s->HEVClc;
     RefPicList *refPicList = s->ref->refPicList;
-    MvField *tab_mvf = s->ref->tab_mvf;
+    MvField *tab_mvf       = s->ref->tab_mvf;
+
+    const int min_pu_width     = s->sps->min_pu_width;
+
+    const int cand_bottom_left = lc->na.cand_bottom_left;
+    const int cand_left        = lc->na.cand_left;
+    const int cand_up_left     = lc->na.cand_up_left;
+    const int cand_up          = lc->na.cand_up;
+    const int cand_up_right    = lc->na.cand_up_right_sap;
+
+    const int xA1    = x0 - 1;
+    const int yA1    = y0 + nPbH - 1;
+    const int xA1_pu = xA1 >> s->sps->log2_min_pu_size;
+    const int yA1_pu = yA1 >> s->sps->log2_min_pu_size;
+
+    const int xB1    = x0 + nPbW - 1;
+    const int yB1    = y0 - 1;
+    const int xB1_pu = xB1 >> s->sps->log2_min_pu_size;
+    const int yB1_pu = yB1 >> s->sps->log2_min_pu_size;
+
+    const int xB0    = x0 + nPbW;
+    const int yB0    = y0 - 1;
+    const int xB0_pu = xB0 >> s->sps->log2_min_pu_size;
+    const int yB0_pu = yB0 >> s->sps->log2_min_pu_size;
+
+    const int xA0    = x0 - 1;
+    const int yA0    = y0 + nPbH;
+    const int xA0_pu = xA0 >> s->sps->log2_min_pu_size;
+    const int yA0_pu = yA0 >> s->sps->log2_min_pu_size;
+
+    const int xB2    = x0 - 1;
+    const int yB2    = y0 - 1;
+    const int xB2_pu = xB2 >> s->sps->log2_min_pu_size;
+    const int yB2_pu = yB2 >> s->sps->log2_min_pu_size;
+
+    const int nb_refs = (s->sh.slice_type == P_SLICE) ?
+                        s->sh.nb_refs[0] : FFMIN(s->sh.nb_refs[0], s->sh.nb_refs[1]);
+    int check_MER   = 1;
+    int check_MER_1 = 1;
 
-    int available_a1_flag = 0;
-    int available_b1_flag = 0;
-    int available_b0_flag = 0;
-    int available_a0_flag = 0;
-    int available_b2_flag = 0;
-    struct MvField spatialCMVS[MRG_MAX_NUM_CANDS];
-    struct MvField combCand = { { { 0 } } };
-    struct MvField TMVPCand = { { { 0 } } };
-    struct Mv mvL0Col = { 0 };
-    struct Mv mvL1Col = { 0 };
+    int zero_idx = 0;
 
-    //first left spatial merge candidate
-    int xA1 = x0 - 1;
-    int yA1 = y0 + nPbH - 1;
-    int is_available_a1;
-    int min_pu_width = s->sps->width >> s->sps->log2_min_pu_size;
+    int nb_merge_cand = 0;
+    int nb_orig_merge_cand = 0;
 
-    int check_MER = 1;
-    int check_MER_1 = 1;
-
-    int xB1, yB1;
+    int is_available_a0;
+    int is_available_a1;
+    int is_available_b0;
     int is_available_b1;
-    int xB1_pu;
-    int yB1_pu;
-
+    int is_available_b2;
     int check_B0;
-    int xB0, yB0;
-    int isAvailableB0;
-    int xB0_pu;
-    int yB0_pu;
-
     int check_A0;
-    int xA0, yA0;
-    int is_available_a0;
-    int xA0_pu;
-    int yA0_pu;
-
-    int xB2, yB2;
-    int isAvailableB2;
-    int xB2_pu;
-    int yB2_pu;
-    int mergearray_index = 0;
-
-    struct MvField zerovector;
-    int numRefIdx = 0;
-    int zeroIdx = 0;
-
-    int numMergeCand = 0;
-    int numOrigMergeCand = 0;
-    int sumcandidates = 0;
-    int combIdx = 0;
-    int combStop = 0;
-    int l0CandIdx = 0;
-    int l1CandIdx = 0;
-
-    int refIdxL0Col = 0;
-    int refIdxL1Col = 0;
-    int availableFlagLXCol = 0;
-
-    int cand_bottom_left = lc->na.cand_bottom_left;
-    int cand_left        = lc->na.cand_left;
-    int cand_up_left     = lc->na.cand_up_left;
-    int cand_up          = lc->na.cand_up;
-    int cand_up_right    = lc->na.cand_up_right_sap;
-
-
-    int xA1_pu = xA1 >> s->sps->log2_min_pu_size;
-    int yA1_pu = yA1 >> s->sps->log2_min_pu_size;
-
-    int availableFlagL0Col = 0;
-    int availableFlagL1Col = 0;
 
+    //first left spatial merge candidate
     is_available_a1 = AVAILABLE(cand_left, A1);
 
     if (!singleMCLFlag && part_idx == 1 &&
@@ -409,29 +380,10 @@ static void derive_spatial_merge_candidates(HEVCContext *s, int x0, int y0,
         is_available_a1 = 0;
     }
 
-    if (is_available_a1) {
-        available_a1_flag = 1;
-        spatialCMVS[0] = TAB_MVF_PU(A1);
-    } else {
-        available_a1_flag = 0;
-        spatialCMVS[0].ref_idx[0] = -1;
-        spatialCMVS[0].ref_idx[1] = -1;
-        spatialCMVS[0].mv[0].x = 0;
-        spatialCMVS[0].mv[0].y = 0;
-        spatialCMVS[0].mv[1].x = 0;
-        spatialCMVS[0].mv[1].y = 0;
-        spatialCMVS[0].pred_flag[0] = 0;
-        spatialCMVS[0].pred_flag[1] = 0;
-        spatialCMVS[0].is_intra = 0;
-    }
+    if (is_available_a1)
+        mergecandlist[nb_merge_cand++] = TAB_MVF_PU(A1);
 
     // above spatial merge candidate
-
-    xB1 = x0 + nPbW - 1;
-    yB1 = y0 - 1;
-    xB1_pu = xB1 >> s->sps->log2_min_pu_size;
-    yB1_pu = yB1 >> s->sps->log2_min_pu_size;
-
     is_available_b1 = AVAILABLE(cand_up, B1);
 
     if (!singleMCLFlag && part_idx == 1 &&
@@ -445,60 +397,26 @@ static void derive_spatial_merge_candidates(HEVCContext *s, int x0, int y0,
     if (is_available_a1 && is_available_b1)
         check_MER = !COMPARE_MV_REFIDX(B1, A1);
 
-    if (is_available_b1 && check_MER) {
-        available_b1_flag = 1;
-        spatialCMVS[1] = TAB_MVF_PU(B1);
-    } else {
-        available_b1_flag = 0;
-        spatialCMVS[1].ref_idx[0] = -1;
-        spatialCMVS[1].ref_idx[1] = -1;
-        spatialCMVS[1].mv[0].x = 0;
-        spatialCMVS[1].mv[0].y = 0;
-        spatialCMVS[1].mv[1].x = 0;
-        spatialCMVS[1].mv[1].y = 0;
-        spatialCMVS[1].pred_flag[0] = 0;
-        spatialCMVS[1].pred_flag[1] = 0;
-        spatialCMVS[1].is_intra = 0;
-    }
+    if (is_available_b1 && check_MER)
+        mergecandlist[nb_merge_cand++] = TAB_MVF_PU(B1);
 
     // above right spatial merge candidate
-    xB0 = x0 + nPbW;
-    yB0 = y0 - 1;
     check_MER = 1;
-    xB0_pu = xB0 >> s->sps->log2_min_pu_size;
-    yB0_pu = yB0 >> s->sps->log2_min_pu_size;
     check_B0 = PRED_BLOCK_AVAILABLE(B0);
 
-    isAvailableB0 = check_B0 && AVAILABLE(cand_up_right, B0);
+    is_available_b0 = check_B0 && AVAILABLE(cand_up_right, B0);
 
     if (isDiffMER(s, xB0, yB0, x0, y0))
-        isAvailableB0 = 0;
+        is_available_b0 = 0;
 
-    if (is_available_b1 && isAvailableB0)
+    if (is_available_b1 && is_available_b0)
         check_MER = !COMPARE_MV_REFIDX(B0, B1);
 
-    if (isAvailableB0 && check_MER) {
-        available_b0_flag = 1;
-        spatialCMVS[2] = TAB_MVF_PU(B0);
-    } else {
-        available_b0_flag = 0;
-        spatialCMVS[2].ref_idx[0] = -1;
-        spatialCMVS[2].ref_idx[1] = -1;
-        spatialCMVS[2].mv[0].x = 0;
-        spatialCMVS[2].mv[0].y = 0;
-        spatialCMVS[2].mv[1].x = 0;
-        spatialCMVS[2].mv[1].y = 0;
-        spatialCMVS[2].pred_flag[0] = 0;
-        spatialCMVS[2].pred_flag[1] = 0;
-        spatialCMVS[2].is_intra = 0;
-    }
+    if (is_available_b0 && check_MER)
+        mergecandlist[nb_merge_cand++] = TAB_MVF_PU(B0);
 
     // left bottom spatial merge candidate
-    xA0 = x0 - 1;
-    yA0 = y0 + nPbH;
     check_MER = 1;
-    xA0_pu = xA0 >> s->sps->log2_min_pu_size;
-    yA0_pu = yA0 >> s->sps->log2_min_pu_size;
     check_A0 = PRED_BLOCK_AVAILABLE(A0);
 
     is_available_a0 = check_A0 && AVAILABLE(cand_bottom_left, A0);
@@ -509,190 +427,99 @@ static void derive_spatial_merge_candidates(HEVCContext *s, int x0, int y0,
     if (is_available_a1 && is_available_a0)
         check_MER = !COMPARE_MV_REFIDX(A0, A1);
 
-    if (is_available_a0 && check_MER) {
-        available_a0_flag = 1;
-        spatialCMVS[3] = TAB_MVF_PU(A0);
-    } else {
-        available_a0_flag = 0;
-        spatialCMVS[3].ref_idx[0] = -1;
-        spatialCMVS[3].ref_idx[1] = -1;
-        spatialCMVS[3].mv[0].x = 0;
-        spatialCMVS[3].mv[0].y = 0;
-        spatialCMVS[3].mv[1].x = 0;
-        spatialCMVS[3].mv[1].y = 0;
-        spatialCMVS[3].pred_flag[0] = 0;
-        spatialCMVS[3].pred_flag[1] = 0;
-        spatialCMVS[3].is_intra = 0;
-    }
+    if (is_available_a0 && check_MER)
+        mergecandlist[nb_merge_cand++] = TAB_MVF_PU(A0);
 
     // above left spatial merge candidate
-    xB2 = x0 - 1;
-    yB2 = y0 - 1;
     check_MER = 1;
-    xB2_pu = xB2 >> s->sps->log2_min_pu_size;
-    yB2_pu = yB2 >> s->sps->log2_min_pu_size;
 
-    isAvailableB2 = AVAILABLE(cand_up_left, B2);
+    is_available_b2 = AVAILABLE(cand_up_left, B2);
 
     if (isDiffMER(s, xB2, yB2, x0, y0))
-        isAvailableB2 = 0;
+        is_available_b2 = 0;
 
-    if (is_available_a1 && isAvailableB2)
+    if (is_available_a1 && is_available_b2)
         check_MER = !COMPARE_MV_REFIDX(B2, A1);
 
-    if (is_available_b1 && isAvailableB2)
+    if (is_available_b1 && is_available_b2)
         check_MER_1 = !COMPARE_MV_REFIDX(B2, B1);
 
-    sumcandidates = available_a1_flag + available_b1_flag + available_b0_flag
-            + available_a0_flag;
-
-    if (isAvailableB2 && check_MER && check_MER_1 && sumcandidates != 4) {
-        available_b2_flag = 1;
-        spatialCMVS[4] = TAB_MVF_PU(B2);
-    } else {
-        available_b2_flag = 0;
-        spatialCMVS[4].ref_idx[0] = -1;
-        spatialCMVS[4].ref_idx[1] = -1;
-        spatialCMVS[4].mv[0].x = 0;
-        spatialCMVS[4].mv[0].y = 0;
-        spatialCMVS[4].mv[1].x = 0;
-        spatialCMVS[4].mv[1].y = 0;
-        spatialCMVS[4].pred_flag[0] = 0;
-        spatialCMVS[4].pred_flag[1] = 0;
-        spatialCMVS[4].is_intra = 0;
-    }
+    if (is_available_b2 && check_MER && check_MER_1 && nb_merge_cand != 4)
+        mergecandlist[nb_merge_cand++] = TAB_MVF_PU(B2);
 
     // temporal motion vector candidate
-    // one optimization is that do temporal checking only if the number of
-    // available candidates < MRG_MAX_NUM_CANDS
-    if (s->sh.slice_temporal_mvp_enabled_flag == 0) {
-        availableFlagLXCol = 0;
-    } else {
-        availableFlagL0Col = temporal_luma_motion_vector(s, x0, y0, nPbW, nPbH,
-                                                         refIdxL0Col, &mvL0Col, 0);
-        // one optimization is that l1 check can be done only when the current slice type is B_SLICE
-        if (s->sh.slice_type == B_SLICE) {
-            availableFlagL1Col = temporal_luma_motion_vector(s, x0, y0, nPbW,
-                                                             nPbH, refIdxL1Col, &mvL1Col, 1);
-        }
-        availableFlagLXCol = availableFlagL0Col || availableFlagL1Col;
-        if (availableFlagLXCol) {
-            TMVPCand.is_intra = 0;
-            TMVPCand.pred_flag[0] = availableFlagL0Col;
-            TMVPCand.pred_flag[1] = availableFlagL1Col;
-            if (TMVPCand.pred_flag[0]) {
-                TMVPCand.mv[0] = mvL0Col;
-                TMVPCand.ref_idx[0] = refIdxL0Col;
+    if (s->sh.slice_temporal_mvp_enabled_flag &&
+        nb_merge_cand < s->sh.max_num_merge_cand) {
+        Mv mv_l0_col, mv_l1_col;
+        int available_l0 = temporal_luma_motion_vector(s, x0, y0, nPbW, nPbH,
+                                                       0, &mv_l0_col, 0);
+        int available_l1 = (s->sh.slice_type == B_SLICE) ?
+                           temporal_luma_motion_vector(s, x0, y0, nPbW, nPbH,
+                                                       0, &mv_l1_col, 1) : 0;
+
+        if (available_l0 || available_l1) {
+            mergecandlist[nb_merge_cand].is_intra     = 0;
+            mergecandlist[nb_merge_cand].pred_flag[0] = available_l0;
+            mergecandlist[nb_merge_cand].pred_flag[1] = available_l1;
+            if (available_l0) {
+                mergecandlist[nb_merge_cand].mv[0]      = mv_l0_col;
+                mergecandlist[nb_merge_cand].ref_idx[0] = 0;
             }
-            if (TMVPCand.pred_flag[1]) {
-                TMVPCand.mv[1] = mvL1Col;
-                TMVPCand.ref_idx[1] = refIdxL1Col;
+            if (available_l1) {
+                mergecandlist[nb_merge_cand].mv[1]      = mv_l1_col;
+                mergecandlist[nb_merge_cand].ref_idx[1] = 0;
             }
+            nb_merge_cand++;
         }
     }
 
-    if (available_a1_flag) {
-        mergecandlist[mergearray_index] = spatialCMVS[0];
-        mergearray_index++;
-    }
-    if (available_b1_flag) {
-        mergecandlist[mergearray_index] = spatialCMVS[1];
-        mergearray_index++;
-    }
-    if (available_b0_flag) {
-        mergecandlist[mergearray_index] = spatialCMVS[2];
-        mergearray_index++;
-    }
-    if (available_a0_flag) {
-        mergecandlist[mergearray_index] = spatialCMVS[3];
-        mergearray_index++;
-    }
-    if (available_b2_flag) {
-        mergecandlist[mergearray_index] = spatialCMVS[4];
-        mergearray_index++;
-    }
-    if (availableFlagLXCol && mergearray_index < s->sh.max_num_merge_cand) {
-        mergecandlist[mergearray_index] = TMVPCand;
-        mergearray_index++;
-    }
-    numMergeCand = mergearray_index;
-    numOrigMergeCand = mergearray_index;
+    nb_orig_merge_cand = nb_merge_cand;
 
     // combined bi-predictive merge candidates  (applies for B slices)
-    if (s->sh.slice_type == B_SLICE) {
-        if (numOrigMergeCand > 1 &&
-            numOrigMergeCand < s->sh.max_num_merge_cand) {
-
-            combIdx = 0;
-            combStop = 0;
-            while (combStop != 1) {
-                MvField l0Cand;
-                MvField l1Cand;
-                l0CandIdx = l0_l1_cand_idx[combIdx][0];
-                l1CandIdx = l0_l1_cand_idx[combIdx][1];
-                l0Cand = mergecandlist[l0CandIdx];
-                l1Cand = mergecandlist[l1CandIdx];
-                if (l0Cand.pred_flag[0] == 1 &&
-                    l1Cand.pred_flag[1] == 1 &&
-                    (refPicList[0].list[l0Cand.ref_idx[0]] !=
-                     refPicList[1].list[l1Cand.ref_idx[1]] ||
-                     l0Cand.mv[0].x != l1Cand.mv[1].x ||
-                     l0Cand.mv[0].y != l1Cand.mv[1].y)) {
-                    combCand.ref_idx[0] = l0Cand.ref_idx[0];
-                    combCand.ref_idx[1] = l1Cand.ref_idx[1];
-                    combCand.pred_flag[0] = 1;
-                    combCand.pred_flag[1] = 1;
-                    combCand.mv[0].x = l0Cand.mv[0].x;
-                    combCand.mv[0].y = l0Cand.mv[0].y;
-                    combCand.mv[1].x = l1Cand.mv[1].x;
-                    combCand.mv[1].y = l1Cand.mv[1].y;
-                    combCand.is_intra = 0;
-                    mergecandlist[numMergeCand] = combCand;
-                    numMergeCand++;
-                }
-                combIdx++;
-                if (combIdx == numOrigMergeCand * (numOrigMergeCand - 1) ||
-                    numMergeCand == s->sh.max_num_merge_cand)
-                    combStop = 1;
+    if (s->sh.slice_type == B_SLICE && nb_orig_merge_cand > 1 &&
+        nb_orig_merge_cand < s->sh.max_num_merge_cand) {
+        int comb_idx = 0;
+
+        for (comb_idx = 0; nb_merge_cand < s->sh.max_num_merge_cand &&
+                           comb_idx < nb_orig_merge_cand * (nb_orig_merge_cand - 1); comb_idx++) {
+            int l0_cand_idx = l0_l1_cand_idx[comb_idx][0];
+            int l1_cand_idx = l0_l1_cand_idx[comb_idx][1];
+            MvField l0_cand = mergecandlist[l0_cand_idx];
+            MvField l1_cand = mergecandlist[l1_cand_idx];
+
+            if (l0_cand.pred_flag[0] && l1_cand.pred_flag[1] &&
+                (refPicList[0].list[l0_cand.ref_idx[0]] !=
+                 refPicList[1].list[l1_cand.ref_idx[1]] ||
+                 l0_cand.mv[0].x != l1_cand.mv[1].x ||
+                 l0_cand.mv[0].y != l1_cand.mv[1].y)) {
+                mergecandlist[nb_merge_cand].ref_idx[0]   = l0_cand.ref_idx[0];
+                mergecandlist[nb_merge_cand].ref_idx[1]   = l1_cand.ref_idx[1];
+                mergecandlist[nb_merge_cand].pred_flag[0] = 1;
+                mergecandlist[nb_merge_cand].pred_flag[1] = 1;
+                mergecandlist[nb_merge_cand].mv[0].x      = l0_cand.mv[0].x;
+                mergecandlist[nb_merge_cand].mv[0].y      = l0_cand.mv[0].y;
+                mergecandlist[nb_merge_cand].mv[1].x      = l1_cand.mv[1].x;
+                mergecandlist[nb_merge_cand].mv[1].y      = l1_cand.mv[1].y;
+                mergecandlist[nb_merge_cand].is_intra     = 0;
+                nb_merge_cand++;
             }
         }
     }
 
-    /*
-     * append Zero motion vector candidates
-     */
-    if (s->sh.slice_type == P_SLICE) {
-        numRefIdx = s->sh.nb_refs[0];
-    } else if (s->sh.slice_type == B_SLICE) {
-        numRefIdx = FFMIN(s->sh.nb_refs[0],
-                          s->sh.nb_refs[1]);
-    }
-    while (numMergeCand < s->sh.max_num_merge_cand) {
-        if (s->sh.slice_type == P_SLICE) {
-            zerovector.ref_idx[0] = (zeroIdx < numRefIdx) ? zeroIdx : 0;
-            zerovector.ref_idx[1] = -1;
-            zerovector.pred_flag[0] = 1;
-            zerovector.pred_flag[1] = 0;
-            zerovector.mv[0].x = 0;
-            zerovector.mv[0].y = 0;
-            zerovector.mv[1].x = 0;
-            zerovector.mv[1].y = 0;
-            zerovector.is_intra = 0;
-        } else {
-            zerovector.ref_idx[0] = (zeroIdx < numRefIdx) ? zeroIdx : 0;
-            zerovector.ref_idx[1] = (zeroIdx < numRefIdx) ? zeroIdx : 0;
-            zerovector.pred_flag[0] = 1;
-            zerovector.pred_flag[1] = 1;
-            zerovector.mv[0].x = 0;
-            zerovector.mv[0].y = 0;
-            zerovector.mv[1].x = 0;
-            zerovector.mv[1].y = 0;
-            zerovector.is_intra = 0;
-        }
+    // append Zero motion vector candidates
+    while (nb_merge_cand < s->sh.max_num_merge_cand) {
+        mergecandlist[nb_merge_cand].pred_flag[0] = 1;
+        mergecandlist[nb_merge_cand].pred_flag[1] = s->sh.slice_type == B_SLICE;
+        mergecandlist[nb_merge_cand].mv[0].x      = 0;
+        mergecandlist[nb_merge_cand].mv[0].y      = 0;
+        mergecandlist[nb_merge_cand].mv[1].x      = 0;
+        mergecandlist[nb_merge_cand].mv[1].y      = 0;
+        mergecandlist[nb_merge_cand].is_intra     = 0;
+        mergecandlist[nb_merge_cand].ref_idx[0]   = (zero_idx < nb_refs) ? zero_idx : 0;
+        mergecandlist[nb_merge_cand].ref_idx[1]   = (zero_idx < nb_refs) ? zero_idx : 0;
 
-        mergecandlist[numMergeCand] = zerovector;
-        numMergeCand++;
-        zeroIdx++;
+        nb_merge_cand++;
+        zero_idx++;
     }
 }
 
@@ -800,7 +627,6 @@ void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0, int nPbW,
     int isScaledFlag_L0 = 0;
     int availableFlagLXA0 = 0;
     int availableFlagLXB0 = 0;
-    int availableFlagLXCol = 0;
     int numMVPCandLX = 0;
     int min_pu_width = s->sps->min_pu_width;
 
@@ -826,7 +652,6 @@ void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0, int nPbW,
     Mv mvpcand_list[2] = { { 0 } };
     Mv mxA = { 0 };
     Mv mxB = { 0 };
-    Mv mvLXCol = { 0 };
     int ref_idx_curr = 0;
     int ref_idx = 0;
     int pred_flag_index_l0;
@@ -965,43 +790,24 @@ void ff_hevc_luma_mv_mvp_mode(HEVCContext *s, int x0, int y0, int nPbW,
         }
     }
 
-    if (availableFlagLXA0 && availableFlagLXB0 &&
-        (mxA.x != mxB.x || mxA.y != mxB.y)) {
-        availableFlagLXCol = 0;
-    } else {
-        //temporal motion vector prediction candidate
-        if (s->sh.slice_temporal_mvp_enabled_flag == 0) {
-            availableFlagLXCol = 0;
-        } else {
-            availableFlagLXCol = temporal_luma_motion_vector(s, x0, y0, nPbW,
-                    nPbH, ref_idx, &mvLXCol, LX);
-        }
-    }
-
-    if (availableFlagLXA0) {
-        mvpcand_list[numMVPCandLX] = mxA;
-        numMVPCandLX++;
-    }
-    if (availableFlagLXB0) {
-        mvpcand_list[numMVPCandLX] = mxB;
-        numMVPCandLX++;
-    }
+    if (availableFlagLXA0)
+        mvpcand_list[numMVPCandLX++] = mxA;
 
-    if (availableFlagLXA0 && availableFlagLXB0 &&
-        mxA.x == mxB.x && mxA.y == mxB.y) {
-        numMVPCandLX--;
-    }
+    if (availableFlagLXB0 && (!availableFlagLXA0 || mxA.x != mxB.x || mxA.y != mxB.y))
+        mvpcand_list[numMVPCandLX++] = mxB;
 
-    if (availableFlagLXCol && numMVPCandLX < 2) {
-        mvpcand_list[numMVPCandLX] = mvLXCol;
-        numMVPCandLX++;
+    //temporal motion vector prediction candidate
+    if (numMVPCandLX < 2 && s->sh.slice_temporal_mvp_enabled_flag) {
+        Mv mv_col;
+        int available_col = temporal_luma_motion_vector(s, x0, y0, nPbW,
+                                                        nPbH, ref_idx, &mv_col, LX);
+        if (available_col)
+            mvpcand_list[numMVPCandLX++] = mv_col;
     }
 
-    while (numMVPCandLX < 2) { // insert zero motion vectors when the number of available candidates are less than 2
-        mvpcand_list[numMVPCandLX].x = 0;
-        mvpcand_list[numMVPCandLX].y = 0;
-        numMVPCandLX++;
-    }
+    // insert zero motion vectors when the number of available candidates are less than 2
+    while (numMVPCandLX < 2)
+        mvpcand_list[numMVPCandLX++] = (Mv){ 0, 0 };
 
     mv->mv[LX].x = mvpcand_list[mvp_lx_flag].x;
     mv->mv[LX].y = mvpcand_list[mvp_lx_flag].y;