hc
2023-05-26 a23f51ed7a39e452c1037343a84d7db1ca2c5bd7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright 2020 Rockchip Electronics Co. LTD
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#define MODULE_TAG "iep2"
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
#include "mpp_common.h"
 
#include "iep2_api.h"
#include "iep2_gmv.h"
 
static void iep2_sort(uint32_t bin[], int map[], int size)
{
    int i, m, n;
    uint32_t *dat = malloc(size * sizeof(uint32_t));
 
    for (i = 0; i < size; ++i) {
        map[i] = i;
        dat[i] = bin[i];
    }
 
    for (m = 0; m < size; ++m) {
        int max = m;
        uint32_t temp;
        int p;
 
        for (n = m + 1; n < size; ++n) if (dat[n] > dat[max]) max = n;
        // Put found minimum element in its place
        temp = dat[m];
        p = map[m];
 
        map[m] = map[max];
        map[max] = p;
        dat[m] = dat[max];
        dat[max] = temp;
    }
 
    free(dat);
}
 
static int iep2_is_subt_mv(int mv, struct mv_list *mv_ls)
{
    int i;
 
    for (i = 0; i < mv_ls->idx; ++i) {
        if (RKABS(mv_ls->mv[i] - (mv * 4)) < 3)
            return 1;
    }
 
    return 0;
}
 
void iep2_update_gmv(struct iep2_api_ctx *ctx, struct mv_list *mv_ls)
{
    int rows = ctx->params.tile_rows;
    int cols = ctx->params.tile_cols;
    uint32_t *bin = ctx->output.mv_hist;
    int lbin = MPP_ARRAY_ELEMS(ctx->output.mv_hist);
    int i;
 
    int map[MPP_ARRAY_ELEMS(ctx->output.mv_hist)];
 
    uint32_t r = 6;
 
    // print mvc histogram of current motion estimation.
    for (i = 0; i < lbin; ++i) {
        if (bin[i] == 0)
            continue;
        iep_dbg_trace("mv(%d) %d\n", i - MVL, bin[i]);
    }
 
    bin[MVL] = 0; // disable 0 mv
 
    // update motion vector candidates
    iep2_sort(bin, map, lbin);
 
    iep_dbg_trace("sort map:\n");
    if (0) {
        for (i = 0; i < lbin; ++i) {
            fprintf(stderr, "%d ", map[i]);
        }
        fprintf(stderr, "\n");
    }
 
    memset(ctx->params.mv_tru_list, 0, sizeof(ctx->params.mv_tru_list));
    memset(ctx->params.mv_tru_vld, 0, sizeof(ctx->params.mv_tru_vld));
 
    // Get top 8 candidates of current motion estimation.
    for (i = 0; i < 8; ++i) {
        int8_t x = map[i] - MVL;
 
        if (bin[map[i]] > r * ((rows * cols) >> 7) ||
            iep2_is_subt_mv(x, mv_ls)) {
 
            // 1 bit at low endian for mv valid check
            ctx->params.mv_tru_list[i] = x;
            ctx->params.mv_tru_vld[i] = 1;
        } else {
            if (i == 0) {
                ctx->params.mv_tru_list[0] = 0;
                ctx->params.mv_tru_vld[0] = 1;
            }
            break;
        }
    }
 
    for (i = 0; i < 8; ++i)
        iep_dbg_trace("new mv candidates list[%d] (%d,%d)\n",
                      i, ctx->params.mv_tru_list[i], 0);
}