1
2
3
4
5
6
7#include <linux/module.h>
8#include <linux/slab.h>
9
10#include "../vdec_drv_if.h"
11#include "../mtk_vcodec_util.h"
12#include "../mtk_vcodec_dec.h"
13#include "../mtk_vcodec_intr.h"
14#include "../vdec_vpu_if.h"
15#include "../vdec_drv_base.h"
16
17#define NAL_NON_IDR_SLICE 0x01
18#define NAL_IDR_SLICE 0x05
19#define NAL_H264_PPS 0x08
20#define NAL_TYPE(value) ((value) & 0x1F)
21
22#define BUF_PREDICTION_SZ (32 * 1024)
23
24#define MB_UNIT_LEN 16
25
26
27#define HW_MB_STORE_SZ 64
28
29#define H264_MAX_FB_NUM 17
30#define HDR_PARSING_BUF_SZ 1024
31
32#define DEC_ERR_RET(ret) ((ret) >> 16)
33#define H264_ERR_NOT_VALID 3
34
35
36
37
38
39
40
41
42
43struct h264_fb {
44 uint64_t vdec_fb_va;
45 uint64_t y_fb_dma;
46 uint64_t c_fb_dma;
47 int32_t poc;
48 uint32_t reserved;
49};
50
51
52
53
54
55
56
57
58
59struct h264_ring_fb_list {
60 struct h264_fb fb_list[H264_MAX_FB_NUM];
61 unsigned int read_idx;
62 unsigned int write_idx;
63 unsigned int count;
64 unsigned int reserved;
65};
66
67
68
69
70
71
72
73
74
75
76
77
78struct vdec_h264_dec_info {
79 uint32_t dpb_sz;
80 uint32_t resolution_changed;
81 uint32_t realloc_mv_buf;
82 uint32_t reserved;
83 uint64_t bs_dma;
84 uint64_t y_fb_dma;
85 uint64_t c_fb_dma;
86 uint64_t vdec_fb_va;
87};
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106struct vdec_h264_vsi {
107 unsigned char hdr_buf[HDR_PARSING_BUF_SZ];
108 uint64_t pred_buf_dma;
109 uint64_t mv_buf_dma[H264_MAX_FB_NUM];
110 struct h264_ring_fb_list list_free;
111 struct h264_ring_fb_list list_disp;
112 struct vdec_h264_dec_info dec;
113 struct vdec_pic_info pic;
114 struct v4l2_rect crop;
115};
116
117
118
119
120
121
122
123
124
125
126struct vdec_h264_inst {
127 unsigned int num_nalu;
128 struct mtk_vcodec_ctx *ctx;
129 struct mtk_vcodec_mem pred_buf;
130 struct mtk_vcodec_mem mv_buf[H264_MAX_FB_NUM];
131 struct vdec_vpu_inst vpu;
132 struct vdec_h264_vsi *vsi;
133};
134
135static unsigned int get_mv_buf_size(unsigned int width, unsigned int height)
136{
137 return HW_MB_STORE_SZ * (width/MB_UNIT_LEN) * (height/MB_UNIT_LEN);
138}
139
140static int allocate_predication_buf(struct vdec_h264_inst *inst)
141{
142 int err = 0;
143
144 inst->pred_buf.size = BUF_PREDICTION_SZ;
145 err = mtk_vcodec_mem_alloc(inst->ctx, &inst->pred_buf);
146 if (err) {
147 mtk_vcodec_err(inst, "failed to allocate ppl buf");
148 return err;
149 }
150
151 inst->vsi->pred_buf_dma = inst->pred_buf.dma_addr;
152 return 0;
153}
154
155static void free_predication_buf(struct vdec_h264_inst *inst)
156{
157 struct mtk_vcodec_mem *mem = NULL;
158
159 mtk_vcodec_debug_enter(inst);
160
161 inst->vsi->pred_buf_dma = 0;
162 mem = &inst->pred_buf;
163 if (mem->va)
164 mtk_vcodec_mem_free(inst->ctx, mem);
165}
166
167static int alloc_mv_buf(struct vdec_h264_inst *inst, struct vdec_pic_info *pic)
168{
169 int i;
170 int err;
171 struct mtk_vcodec_mem *mem = NULL;
172 unsigned int buf_sz = get_mv_buf_size(pic->buf_w, pic->buf_h);
173
174 for (i = 0; i < H264_MAX_FB_NUM; i++) {
175 mem = &inst->mv_buf[i];
176 if (mem->va)
177 mtk_vcodec_mem_free(inst->ctx, mem);
178 mem->size = buf_sz;
179 err = mtk_vcodec_mem_alloc(inst->ctx, mem);
180 if (err) {
181 mtk_vcodec_err(inst, "failed to allocate mv buf");
182 return err;
183 }
184 inst->vsi->mv_buf_dma[i] = mem->dma_addr;
185 }
186
187 return 0;
188}
189
190static void free_mv_buf(struct vdec_h264_inst *inst)
191{
192 int i;
193 struct mtk_vcodec_mem *mem = NULL;
194
195 for (i = 0; i < H264_MAX_FB_NUM; i++) {
196 inst->vsi->mv_buf_dma[i] = 0;
197 mem = &inst->mv_buf[i];
198 if (mem->va)
199 mtk_vcodec_mem_free(inst->ctx, mem);
200 }
201}
202
203static int check_list_validity(struct vdec_h264_inst *inst, bool disp_list)
204{
205 struct h264_ring_fb_list *list;
206
207 list = disp_list ? &inst->vsi->list_disp : &inst->vsi->list_free;
208
209 if (list->count > H264_MAX_FB_NUM ||
210 list->read_idx >= H264_MAX_FB_NUM ||
211 list->write_idx >= H264_MAX_FB_NUM) {
212 mtk_vcodec_err(inst, "%s list err: cnt=%d r_idx=%d w_idx=%d",
213 disp_list ? "disp" : "free", list->count,
214 list->read_idx, list->write_idx);
215 return -EINVAL;
216 }
217
218 return 0;
219}
220
221static void put_fb_to_free(struct vdec_h264_inst *inst, struct vdec_fb *fb)
222{
223 struct h264_ring_fb_list *list;
224
225 if (fb) {
226 if (check_list_validity(inst, false))
227 return;
228
229 list = &inst->vsi->list_free;
230 if (list->count == H264_MAX_FB_NUM) {
231 mtk_vcodec_err(inst, "[FB] put fb free_list full");
232 return;
233 }
234
235 mtk_vcodec_debug(inst, "[FB] put fb into free_list @(%p, %llx)",
236 fb->base_y.va, (u64)fb->base_y.dma_addr);
237
238 list->fb_list[list->write_idx].vdec_fb_va = (u64)(uintptr_t)fb;
239 list->write_idx = (list->write_idx == H264_MAX_FB_NUM - 1) ?
240 0 : list->write_idx + 1;
241 list->count++;
242 }
243}
244
245static void get_pic_info(struct vdec_h264_inst *inst,
246 struct vdec_pic_info *pic)
247{
248 *pic = inst->vsi->pic;
249 mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
250 pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
251 mtk_vcodec_debug(inst, "fb size: Y(%d), C(%d)",
252 pic->fb_sz[0], pic->fb_sz[1]);
253}
254
255static void get_crop_info(struct vdec_h264_inst *inst, struct v4l2_rect *cr)
256{
257 cr->left = inst->vsi->crop.left;
258 cr->top = inst->vsi->crop.top;
259 cr->width = inst->vsi->crop.width;
260 cr->height = inst->vsi->crop.height;
261
262 mtk_vcodec_debug(inst, "l=%d, t=%d, w=%d, h=%d",
263 cr->left, cr->top, cr->width, cr->height);
264}
265
266static void get_dpb_size(struct vdec_h264_inst *inst, unsigned int *dpb_sz)
267{
268 *dpb_sz = inst->vsi->dec.dpb_sz;
269 mtk_vcodec_debug(inst, "sz=%d", *dpb_sz);
270}
271
272static int vdec_h264_init(struct mtk_vcodec_ctx *ctx)
273{
274 struct vdec_h264_inst *inst = NULL;
275 int err;
276
277 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
278 if (!inst)
279 return -ENOMEM;
280
281 inst->ctx = ctx;
282
283 inst->vpu.id = IPI_VDEC_H264;
284 inst->vpu.dev = ctx->dev->vpu_plat_dev;
285 inst->vpu.ctx = ctx;
286 inst->vpu.handler = vpu_dec_ipi_handler;
287
288 err = vpu_dec_init(&inst->vpu);
289 if (err) {
290 mtk_vcodec_err(inst, "vdec_h264 init err=%d", err);
291 goto error_free_inst;
292 }
293
294 inst->vsi = (struct vdec_h264_vsi *)inst->vpu.vsi;
295 err = allocate_predication_buf(inst);
296 if (err)
297 goto error_deinit;
298
299 mtk_vcodec_debug(inst, "H264 Instance >> %p", inst);
300
301 ctx->drv_handle = inst;
302 return 0;
303
304error_deinit:
305 vpu_dec_deinit(&inst->vpu);
306
307error_free_inst:
308 kfree(inst);
309 return err;
310}
311
312static void vdec_h264_deinit(void *h_vdec)
313{
314 struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
315
316 mtk_vcodec_debug_enter(inst);
317
318 vpu_dec_deinit(&inst->vpu);
319 free_predication_buf(inst);
320 free_mv_buf(inst);
321
322 kfree(inst);
323}
324
325static int find_start_code(unsigned char *data, unsigned int data_sz)
326{
327 if (data_sz > 3 && data[0] == 0 && data[1] == 0 && data[2] == 1)
328 return 3;
329
330 if (data_sz > 4 && data[0] == 0 && data[1] == 0 && data[2] == 0 &&
331 data[3] == 1)
332 return 4;
333
334 return -1;
335}
336
337static int vdec_h264_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
338 struct vdec_fb *fb, bool *res_chg)
339{
340 struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
341 struct vdec_vpu_inst *vpu = &inst->vpu;
342 int nal_start_idx = 0;
343 int err = 0;
344 unsigned int nal_start;
345 unsigned int nal_type;
346 unsigned char *buf;
347 unsigned int buf_sz;
348 unsigned int data[2];
349 uint64_t vdec_fb_va = (u64)(uintptr_t)fb;
350 uint64_t y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
351 uint64_t c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
352
353 mtk_vcodec_debug(inst, "+ [%d] FB y_dma=%llx c_dma=%llx va=%p",
354 ++inst->num_nalu, y_fb_dma, c_fb_dma, fb);
355
356
357 if (bs == NULL)
358 return vpu_dec_reset(vpu);
359
360 buf = (unsigned char *)bs->va;
361 buf_sz = bs->size;
362 nal_start_idx = find_start_code(buf, buf_sz);
363 if (nal_start_idx < 0) {
364 mtk_vcodec_err(inst, "invalid nal start code");
365 err = -EIO;
366 goto err_free_fb_out;
367 }
368
369 nal_start = buf[nal_start_idx];
370 nal_type = NAL_TYPE(buf[nal_start_idx]);
371 mtk_vcodec_debug(inst, "\n + NALU[%d] type %d +\n", inst->num_nalu,
372 nal_type);
373
374 if (nal_type == NAL_H264_PPS) {
375 buf_sz -= nal_start_idx;
376 if (buf_sz > HDR_PARSING_BUF_SZ) {
377 err = -EILSEQ;
378 goto err_free_fb_out;
379 }
380 memcpy(inst->vsi->hdr_buf, buf + nal_start_idx, buf_sz);
381 }
382
383 inst->vsi->dec.bs_dma = (uint64_t)bs->dma_addr;
384 inst->vsi->dec.y_fb_dma = y_fb_dma;
385 inst->vsi->dec.c_fb_dma = c_fb_dma;
386 inst->vsi->dec.vdec_fb_va = vdec_fb_va;
387
388 data[0] = buf_sz;
389 data[1] = nal_start;
390 err = vpu_dec_start(vpu, data, 2);
391 if (err) {
392 if (err > 0 && (DEC_ERR_RET(err) == H264_ERR_NOT_VALID)) {
393 mtk_vcodec_err(inst, "- error bitstream - err = %d -",
394 err);
395 err = -EIO;
396 }
397 goto err_free_fb_out;
398 }
399
400 *res_chg = inst->vsi->dec.resolution_changed;
401 if (*res_chg) {
402 struct vdec_pic_info pic;
403
404 mtk_vcodec_debug(inst, "- resolution changed -");
405 get_pic_info(inst, &pic);
406
407 if (inst->vsi->dec.realloc_mv_buf) {
408 err = alloc_mv_buf(inst, &pic);
409 if (err)
410 goto err_free_fb_out;
411 }
412 }
413
414 if (nal_type == NAL_NON_IDR_SLICE || nal_type == NAL_IDR_SLICE) {
415
416 err = mtk_vcodec_wait_for_done_ctx(inst->ctx,
417 MTK_INST_IRQ_RECEIVED,
418 WAIT_INTR_TIMEOUT_MS);
419 if (err)
420 goto err_free_fb_out;
421
422 vpu_dec_end(vpu);
423 }
424
425 mtk_vcodec_debug(inst, "\n - NALU[%d] type=%d -\n", inst->num_nalu,
426 nal_type);
427 return 0;
428
429err_free_fb_out:
430 put_fb_to_free(inst, fb);
431 mtk_vcodec_err(inst, "\n - NALU[%d] err=%d -\n", inst->num_nalu, err);
432 return err;
433}
434
435static void vdec_h264_get_fb(struct vdec_h264_inst *inst,
436 struct h264_ring_fb_list *list,
437 bool disp_list, struct vdec_fb **out_fb)
438{
439 struct vdec_fb *fb;
440
441 if (check_list_validity(inst, disp_list))
442 return;
443
444 if (list->count == 0) {
445 mtk_vcodec_debug(inst, "[FB] there is no %s fb",
446 disp_list ? "disp" : "free");
447 *out_fb = NULL;
448 return;
449 }
450
451 fb = (struct vdec_fb *)
452 (uintptr_t)list->fb_list[list->read_idx].vdec_fb_va;
453 fb->status |= (disp_list ? FB_ST_DISPLAY : FB_ST_FREE);
454
455 *out_fb = fb;
456 mtk_vcodec_debug(inst, "[FB] get %s fb st=%d poc=%d %llx",
457 disp_list ? "disp" : "free",
458 fb->status, list->fb_list[list->read_idx].poc,
459 list->fb_list[list->read_idx].vdec_fb_va);
460
461 list->read_idx = (list->read_idx == H264_MAX_FB_NUM - 1) ?
462 0 : list->read_idx + 1;
463 list->count--;
464}
465
466static int vdec_h264_get_param(void *h_vdec, enum vdec_get_param_type type,
467 void *out)
468{
469 struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
470
471 switch (type) {
472 case GET_PARAM_DISP_FRAME_BUFFER:
473 vdec_h264_get_fb(inst, &inst->vsi->list_disp, true, out);
474 break;
475
476 case GET_PARAM_FREE_FRAME_BUFFER:
477 vdec_h264_get_fb(inst, &inst->vsi->list_free, false, out);
478 break;
479
480 case GET_PARAM_PIC_INFO:
481 get_pic_info(inst, out);
482 break;
483
484 case GET_PARAM_DPB_SIZE:
485 get_dpb_size(inst, out);
486 break;
487
488 case GET_PARAM_CROP_INFO:
489 get_crop_info(inst, out);
490 break;
491
492 default:
493 mtk_vcodec_err(inst, "invalid get parameter type=%d", type);
494 return -EINVAL;
495 }
496
497 return 0;
498}
499
500const struct vdec_common_if vdec_h264_if = {
501 .init = vdec_h264_init,
502 .decode = vdec_h264_decode,
503 .get_param = vdec_h264_get_param,
504 .deinit = vdec_h264_deinit,
505};
506