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