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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright 2015 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 "hal_m2vd_vdpu1"
 
#include <string.h>
 
#include "mpp_mem.h"
#include "mpp_common.h"
 
#include "hal_m2vd_base.h"
#include "hal_m2vd_vdpu1_reg.h"
#include "hal_m2vd_vpu1.h"
 
MPP_RET hal_m2vd_vdpu1_init(void *hal, MppHalCfg *cfg)
{
    MPP_RET ret = MPP_OK;
    M2vdHalCtx *ctx = (M2vdHalCtx *)hal;
    M2vdVdpu1Reg_t *regs = NULL;
 
    regs = mpp_calloc(M2vdVdpu1Reg_t, 1);
    if (NULL == regs) {
        mpp_err_f("failed to malloc register ret\n");
        ret = MPP_ERR_MALLOC;
        goto __ERR_RET;
    }
 
    ctx->reg_len = M2VD_VDPU1_REG_NUM;
 
    ret = mpp_dev_init(&ctx->dev, VPU_CLIENT_VDPU1);
    if (ret) {
        mpp_err_f("mpp_dev_init failed. ret: %d\n", ret);
        ret = MPP_ERR_UNKNOW;
        goto __ERR_RET;
    }
    if (ctx->group == NULL) {
        ret = mpp_buffer_group_get_internal(&ctx->group, MPP_BUFFER_TYPE_ION);
        if (ret) {
            mpp_err("m2v_hal mpp_buffer_group_get failed\n");
            goto __ERR_RET;
        }
    }
    ret = mpp_buffer_get(ctx->group, &ctx->qp_table, M2VD_BUF_SIZE_QPTAB);
    if (ret) {
        mpp_err("m2v_hal_qtable_base get buffer failed\n");
        goto __ERR_RET;
    }
 
    ctx->packet_slots   = cfg->packet_slots;
    ctx->frame_slots    = cfg->frame_slots;
    ctx->dec_cb         = cfg->dec_cb;
    ctx->regs           = (void*)regs;
    cfg->dev            = ctx->dev;
 
    return ret;
 
__ERR_RET:
    if (regs) {
        mpp_free(regs);
        regs = NULL;
    }
 
    if (ctx) {
        hal_m2vd_vdpu1_deinit(ctx);
    }
 
    return ret;
}
 
MPP_RET hal_m2vd_vdpu1_deinit(void *hal)
{
    MPP_RET ret = MPP_OK;
    M2vdHalCtx *p = (M2vdHalCtx *)hal;
 
    if (p->dev) {
        mpp_dev_deinit(p->dev);
        p->dev = NULL;
    }
 
    if (p->qp_table) {
        ret = mpp_buffer_put(p->qp_table);
        p->qp_table = NULL;
        if (MPP_OK !=  ret) {
            mpp_err("m2v_hal qp_table put buffer failed\n");
            return ret;
        }
    }
 
    if (p->group) {
        ret = mpp_buffer_group_put(p->group);
        p->group = NULL;
        if (ret) {
            mpp_err("m2v_hal group free buffer failed\n");
            return ret;
        }
    }
 
    return ret;
}
 
static MPP_RET hal_m2vd_vdpu1_init_hwcfg(M2vdHalCtx *ctx)
{
    M2vdVdpu1Reg_t *p_regs = (M2vdVdpu1Reg_t *)ctx->regs;
 
    memset(p_regs, 0, sizeof(M2vdVdpu1Reg_t));
    p_regs->sw02.dec_axi_rn_id = 0;
    p_regs->sw02.dec_timeout_e = 1;
    p_regs->sw02.dec_strswap32_e = 1;
    p_regs->sw02.dec_strendian_e = 1;
    p_regs->sw02.dec_inswap32_e = 1;
    p_regs->sw02.dec_outswap32_e = 1;
 
    p_regs->sw02.dec_clk_gate_e = 1;
    p_regs->sw02.dec_in_endian = 1;
    p_regs->sw02.dec_out_endian = 1;
 
    p_regs->sw02.dec_out_tiled_e = 0;
    p_regs->sw02.dec_max_burst = DEC_BUS_BURST_LENGTH_16;
    p_regs->sw02.dec_scmd_dis = 0;
    p_regs->sw02.dec_adv_pre_dis = 0;
    p_regs->sw55.apf_threshold = 8;
    p_regs->sw02.dec_latency = 0;
    p_regs->sw02.dec_data_disc_e = 0;
    p_regs->sw01.dec_irq = 0;
    p_regs->sw02.dec_axi_rn_id = 0;
    p_regs->sw03.dec_axi_wr_id = 0;
    p_regs->sw03.dec_mode = 8;
 
    return MPP_OK;
}
 
MPP_RET hal_m2vd_vdpu1_gen_regs(void *hal, HalTaskInfo *task)
{
    MPP_RET ret = MPP_OK;
 
    if (task->dec.valid) {
        void *q_table = NULL;
        MppBuffer streambuf = NULL;
        MppBuffer framebuf = NULL;
        M2vdHalCtx *ctx = (M2vdHalCtx *)hal;
        M2VDDxvaParam *dx = (M2VDDxvaParam *)task->dec.syntax.data;
        M2vdVdpu1Reg_t *p_regs = (M2vdVdpu1Reg_t*) ctx->regs;
 
        task->dec.valid = 0;
        q_table = mpp_buffer_get_ptr(ctx->qp_table);
        memcpy(q_table, dx->qp_tab, M2VD_BUF_SIZE_QPTAB);
 
        hal_m2vd_vdpu1_init_hwcfg(ctx);
 
        p_regs->sw18.mv_accuracy_fwd = 1;
        p_regs->sw18.mv_accuracy_bwd = 1;
        if (dx->seq_ext_head_dec_flag) {
            p_regs->sw03.dec_mode = 5;
            p_regs->sw18.fcode_fwd_hor = dx->pic.full_pel_forward_vector;
            p_regs->sw18.fcode_fwd_ver = dx->pic.forward_f_code;
            p_regs->sw18.fcode_bwd_hor = dx->pic.full_pel_backward_vector;
            p_regs->sw18.fcode_bwd_ver = dx->pic.backward_f_code;
        } else {
            p_regs->sw03.dec_mode = 6;
            p_regs->sw18.fcode_fwd_hor = dx->pic.forward_f_code;
            p_regs->sw18.fcode_fwd_ver = dx->pic.forward_f_code;
            p_regs->sw18.fcode_bwd_hor = dx->pic.backward_f_code;
            p_regs->sw18.fcode_bwd_ver = dx->pic.backward_f_code;
            if (dx->pic.full_pel_forward_vector)
                p_regs->sw18.mv_accuracy_fwd = 0;
            if (dx->pic.full_pel_backward_vector)
                p_regs->sw18.mv_accuracy_bwd = 0;
        }
 
        p_regs->sw04.pic_mb_width = (dx->seq.decode_width + 15) >> 4;
        p_regs->sw04.pic_mb_height_p = (dx->seq.decode_height + 15) >> 4;
        p_regs->sw03.pic_interlace_e = 1 - dx->seq_ext.progressive_sequence;
        if (dx->pic_code_ext.picture_structure == M2VD_PIC_STRUCT_FRAME)
            p_regs->sw03.pic_fieldmode_e = 0;
        else {
            p_regs->sw03.pic_fieldmode_e = 1;
            p_regs->sw03.pic_topfield_e = dx->pic_code_ext.picture_structure == 1;
        }
        if (dx->pic.picture_coding_type == M2VD_CODING_TYPE_B)
            p_regs->sw03.pic_b_e = 1;
        else
            p_regs->sw03.pic_b_e = 0;
        if (dx->pic.picture_coding_type == M2VD_CODING_TYPE_I)
            p_regs->sw03.pic_inter_e = 0;
        else
            p_regs->sw03.pic_inter_e = 1;
 
        p_regs->sw04.topfieldfirst_e = dx->pic_code_ext.top_field_first;
        p_regs->sw03.fwd_interlace_e = 0;
        p_regs->sw03.write_mvs_e = 0;
        p_regs->sw04.alt_scan_e = dx->pic_code_ext.alternate_scan;
        p_regs->sw18.alt_scan_flag_e = dx->pic_code_ext.alternate_scan;
 
        p_regs->sw05.qscale_type = dx->pic_code_ext.q_scale_type;
        p_regs->sw05.intra_dc_prec = dx->pic_code_ext.intra_dc_precision;
        p_regs->sw05.con_mv_e = dx->pic_code_ext.concealment_motion_vectors;
        p_regs->sw05.intra_vlc_tab = dx->pic_code_ext.intra_vlc_format;
        p_regs->sw05.frame_pred_dct = dx->pic_code_ext.frame_pred_frame_dct;
        p_regs->sw06.init_qp = 1;
 
        mpp_buf_slot_get_prop(ctx->packet_slots, task->dec.input, SLOT_BUFFER, &streambuf);
        p_regs->sw12.rlc_vlc_base = mpp_buffer_get_fd(streambuf);
        if (dx->bitstream_offset) {
            mpp_dev_set_reg_offset(ctx->dev, 12, dx->bitstream_offset);
        }
 
        mpp_buf_slot_get_prop(ctx->frame_slots, dx->CurrPic.Index7Bits, SLOT_BUFFER, &framebuf);
 
        if ((dx->pic_code_ext.picture_structure == M2VD_PIC_STRUCT_TOP_FIELD) ||
            (dx->pic_code_ext.picture_structure == M2VD_PIC_STRUCT_FRAME)) {
            p_regs->sw13.dec_out_base = mpp_buffer_get_fd(framebuf);
        } else {
            p_regs->sw13.dec_out_base = mpp_buffer_get_fd(framebuf);
            mpp_dev_set_reg_offset(ctx->dev, 13, MPP_ALIGN(dx->seq.decode_width, 16));
        }
 
        mpp_buf_slot_get_prop(ctx->frame_slots, dx->frame_refs[0].Index7Bits, SLOT_BUFFER, &framebuf);
        p_regs->sw14.refer0_base = mpp_buffer_get_fd(framebuf);
 
        mpp_buf_slot_get_prop(ctx->frame_slots, dx->frame_refs[1].Index7Bits, SLOT_BUFFER, &framebuf);
        p_regs->sw15.refer1_base = mpp_buffer_get_fd(framebuf);
 
        mpp_buf_slot_get_prop(ctx->frame_slots, dx->frame_refs[2].Index7Bits, SLOT_BUFFER, &framebuf);
        p_regs->sw16.refer2_base = mpp_buffer_get_fd(framebuf);
 
        mpp_buf_slot_get_prop(ctx->frame_slots, dx->frame_refs[3].Index7Bits, SLOT_BUFFER, &framebuf);
        p_regs->sw17.refer3_base = mpp_buffer_get_fd(framebuf);
 
        p_regs->sw40.qtable_base = mpp_buffer_get_fd(ctx->qp_table);
 
        p_regs->sw48.startmb_x = 0;
        p_regs->sw48.startmb_y = 0;
        p_regs->sw03.dec_out_dis = 0;
        p_regs->sw03.filtering_dis = 1;
        p_regs->sw06.stream_len = dx->bitstream_length;
        p_regs->sw05.stream_start_bit = dx->bitstream_start_bit;
        p_regs->sw01.dec_e = 1;
 
        task->dec.valid = 1;
        ctx->dec_frame_cnt++;
    }
 
    return ret;
}
 
MPP_RET hal_m2vd_vdpu1_start(void *hal, HalTaskInfo *task)
{
    MPP_RET ret = MPP_OK;
    M2vdHalCtx *ctx = (M2vdHalCtx *)hal;
 
    do {
        MppDevRegWrCfg wr_cfg;
        MppDevRegRdCfg rd_cfg;
        RK_U32 *regs = (RK_U32 *)ctx->regs;
        RK_U32 reg_size = sizeof(M2vdVdpu1Reg_t);
 
        wr_cfg.reg = regs;
        wr_cfg.size = reg_size;
        wr_cfg.offset = 0;
 
        ret = mpp_dev_ioctl(ctx->dev, MPP_DEV_REG_WR, &wr_cfg);
        if (ret) {
            mpp_err_f("set register write failed %d\n", ret);
            break;
        }
 
        rd_cfg.reg = regs;
        rd_cfg.size = reg_size;
        rd_cfg.offset = 0;
 
        ret = mpp_dev_ioctl(ctx->dev, MPP_DEV_REG_RD, &rd_cfg);
        if (ret) {
            mpp_err_f("set register read failed %d\n", ret);
            break;
        }
 
        ret = mpp_dev_ioctl(ctx->dev, MPP_DEV_CMD_SEND, NULL);
        if (ret) {
            mpp_err_f("send cmd failed %d\n", ret);
            break;
        }
    } while (0);
 
    (void)task;
    return ret;
}
 
MPP_RET hal_m2vd_vdpu1_wait(void *hal, HalTaskInfo *task)
{
    MPP_RET ret = MPP_OK;
    M2vdHalCtx *ctx = (M2vdHalCtx *)hal;
    M2vdVdpu1Reg_t* reg_out = (M2vdVdpu1Reg_t * )ctx->regs;
 
    ret = mpp_dev_ioctl(ctx->dev, MPP_DEV_CMD_POLL, NULL);
    if (ret)
        mpp_err_f("poll cmd failed %d\n", ret);
 
    if (reg_out->sw01.dec_error_int | reg_out->sw01.dec_buffer_int) {
        if (ctx->dec_cb)
            mpp_callback(ctx->dec_cb, NULL);
    }
 
    (void)task;
 
    return ret;
}