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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2022 MediaTek Inc.
* Author: Ping-Hsun Wu <ping-hsun.wu@mediatek.com>
*/
#include <linux/math64.h>
#include <media/v4l2-common.h>
#include <media/videobuf2-v4l2.h>
#include <media/videobuf2-dma-contig.h>
#include "mtk-mdp3-core.h"
#include "mtk-mdp3-regs.h"
#include "mtk-mdp3-m2m.h"
static const struct mdp_format *mdp_find_fmt(const struct mtk_mdp_driver_data *mdp_data,
u32 pixelformat, u32 type)
{
u32 i, flag;
flag = V4L2_TYPE_IS_OUTPUT(type) ? MDP_FMT_FLAG_OUTPUT :
MDP_FMT_FLAG_CAPTURE;
for (i = 0; i < mdp_data->format_len; ++i) {
if (!(mdp_data->format[i].flags & flag))
continue;
if (mdp_data->format[i].pixelformat == pixelformat)
return &mdp_data->format[i];
}
return NULL;
}
static const struct mdp_format *mdp_find_fmt_by_index(const struct mtk_mdp_driver_data *mdp_data,
u32 index, u32 type)
{
u32 i, flag, num = 0;
flag = V4L2_TYPE_IS_OUTPUT(type) ? MDP_FMT_FLAG_OUTPUT :
MDP_FMT_FLAG_CAPTURE;
for (i = 0; i < mdp_data->format_len; ++i) {
if (!(mdp_data->format[i].flags & flag))
continue;
if (index == num)
return &mdp_data->format[i];
num++;
}
return NULL;
}
enum mdp_ycbcr_profile mdp_map_ycbcr_prof_mplane(struct v4l2_format *f,
u32 mdp_color)
{
struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
if (MDP_COLOR_IS_RGB(mdp_color))
return MDP_YCBCR_PROFILE_FULL_BT601;
switch (pix_mp->colorspace) {
case V4L2_COLORSPACE_JPEG:
return MDP_YCBCR_PROFILE_JPEG;
case V4L2_COLORSPACE_REC709:
case V4L2_COLORSPACE_DCI_P3:
if (pix_mp->quantization == V4L2_QUANTIZATION_FULL_RANGE)
return MDP_YCBCR_PROFILE_FULL_BT709;
return MDP_YCBCR_PROFILE_BT709;
case V4L2_COLORSPACE_BT2020:
if (pix_mp->quantization == V4L2_QUANTIZATION_FULL_RANGE)
return MDP_YCBCR_PROFILE_FULL_BT2020;
return MDP_YCBCR_PROFILE_BT2020;
default:
if (pix_mp->quantization == V4L2_QUANTIZATION_FULL_RANGE)
return MDP_YCBCR_PROFILE_FULL_BT601;
return MDP_YCBCR_PROFILE_BT601;
}
}
static void mdp_bound_align_image(u32 *w, u32 *h,
struct v4l2_frmsize_stepwise *s,
unsigned int salign)
{
unsigned int org_w, org_h;
org_w = *w;
org_h = *h;
v4l_bound_align_image(w, s->min_width, s->max_width, s->step_width,
h, s->min_height, s->max_height, s->step_height,
salign);
s->min_width = org_w;
s->min_height = org_h;
v4l2_apply_frmsize_constraints(w, h, s);
}
static int mdp_clamp_align(s32 *x, int min, int max, unsigned int align)
{
unsigned int mask;
if (min < 0 || max < 0)
return -ERANGE;
/* Bits that must be zero to be aligned */
mask = ~((1 << align) - 1);
min = 0 ? 0 : ((min + ~mask) & mask);
max = max & mask;
if ((unsigned int)min > (unsigned int)max)
return -ERANGE;
/* Clamp to aligned min and max */
*x = clamp(*x, min, max);
/* Round to nearest aligned value */
if (align)
*x = (*x + (1 << (align - 1))) & mask;
return 0;
}
int mdp_enum_fmt_mplane(struct mdp_dev *mdp, struct v4l2_fmtdesc *f)
{
const struct mdp_format *fmt;
fmt = mdp_find_fmt_by_index(mdp->mdp_data, f->index, f->type);
if (!fmt)
return -EINVAL;
f->pixelformat = fmt->pixelformat;
return 0;
}
const struct mdp_format *mdp_try_fmt_mplane(struct mdp_dev *mdp,
struct v4l2_format *f,
struct mdp_frameparam *param,
u32 ctx_id)
{
struct device *dev = ¶m->ctx->mdp_dev->pdev->dev;
struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
const struct mdp_format *fmt;
const struct mdp_pix_limit *pix_limit;
struct v4l2_frmsize_stepwise s;
u32 org_w, org_h;
unsigned int i;
fmt = mdp_find_fmt(mdp->mdp_data, pix_mp->pixelformat, f->type);
if (!fmt) {
fmt = mdp_find_fmt_by_index(mdp->mdp_data, 0, f->type);
if (!fmt) {
dev_dbg(dev, "%d: pixelformat %c%c%c%c invalid", ctx_id,
(pix_mp->pixelformat & 0xff),
(pix_mp->pixelformat >> 8) & 0xff,
(pix_mp->pixelformat >> 16) & 0xff,
(pix_mp->pixelformat >> 24) & 0xff);
return NULL;
}
}
pix_mp->field = V4L2_FIELD_NONE;
pix_mp->flags = 0;
pix_mp->pixelformat = fmt->pixelformat;
if (V4L2_TYPE_IS_CAPTURE(f->type)) {
pix_mp->colorspace = param->colorspace;
pix_mp->xfer_func = param->xfer_func;
pix_mp->ycbcr_enc = param->ycbcr_enc;
pix_mp->quantization = param->quant;
}
pix_limit = V4L2_TYPE_IS_OUTPUT(f->type) ? ¶m->limit->out_limit :
¶m->limit->cap_limit;
s.min_width = pix_limit->wmin;
s.max_width = pix_limit->wmax;
s.step_width = fmt->walign;
s.min_height = pix_limit->hmin;
s.max_height = pix_limit->hmax;
s.step_height = fmt->halign;
org_w = pix_mp->width;
org_h = pix_mp->height;
mdp_bound_align_image(&pix_mp->width, &pix_mp->height, &s, fmt->salign);
if (org_w != pix_mp->width || org_h != pix_mp->height)
dev_dbg(dev, "%d: size change: %ux%u to %ux%u", ctx_id,
org_w, org_h, pix_mp->width, pix_mp->height);
if (pix_mp->num_planes && pix_mp->num_planes != fmt->num_planes)
dev_dbg(dev, "%d num of planes change: %u to %u", ctx_id,
pix_mp->num_planes, fmt->num_planes);
pix_mp->num_planes = fmt->num_planes;
for (i = 0; i < pix_mp->num_planes; ++i) {
u32 min_bpl = (pix_mp->width * fmt->row_depth[i]) >> 3;
u32 max_bpl = (pix_limit->wmax * fmt->row_depth[i]) >> 3;
u32 bpl = pix_mp->plane_fmt[i].bytesperline;
u32 min_si, max_si;
u32 si = pix_mp->plane_fmt[i].sizeimage;
u64 di;
bpl = clamp(bpl, min_bpl, max_bpl);
pix_mp->plane_fmt[i].bytesperline = bpl;
di = (u64)bpl * pix_mp->height * fmt->depth[i];
min_si = (u32)div_u64(di, fmt->row_depth[i]);
di = (u64)bpl * s.max_height * fmt->depth[i];
max_si = (u32)div_u64(di, fmt->row_depth[i]);
si = clamp(si, min_si, max_si);
pix_mp->plane_fmt[i].sizeimage = si;
dev_dbg(dev, "%d: p%u, bpl:%u [%u, %u], sizeimage:%u [%u, %u]",
ctx_id, i, bpl, min_bpl, max_bpl, si, min_si, max_si);
}
return fmt;
}
static int mdp_clamp_start(s32 *x, int min, int max, unsigned int align,
u32 flags)
{
if (flags & V4L2_SEL_FLAG_GE)
max = *x;
if (flags & V4L2_SEL_FLAG_LE)
min = *x;
return mdp_clamp_align(x, min, max, align);
}
static int mdp_clamp_end(s32 *x, int min, int max, unsigned int align,
u32 flags)
{
if (flags & V4L2_SEL_FLAG_GE)
min = *x;
if (flags & V4L2_SEL_FLAG_LE)
max = *x;
return mdp_clamp_align(x, min, max, align);
}
int mdp_try_crop(struct mdp_m2m_ctx *ctx, struct v4l2_rect *r,
const struct v4l2_selection *s, struct mdp_frame *frame)
{
struct device *dev = &ctx->mdp_dev->pdev->dev;
s32 left, top, right, bottom;
u32 framew, frameh, walign, halign;
int ret;
dev_dbg(dev, "%d target:%d, set:(%d,%d) %ux%u", ctx->id,
s->target, s->r.left, s->r.top, s->r.width, s->r.height);
left = s->r.left;
top = s->r.top;
right = s->r.left + s->r.width;
bottom = s->r.top + s->r.height;
framew = frame->format.fmt.pix_mp.width;
frameh = frame->format.fmt.pix_mp.height;
if (mdp_target_is_crop(s->target)) {
walign = 1;
halign = 1;
} else {
walign = frame->mdp_fmt->walign;
halign = frame->mdp_fmt->halign;
}
dev_dbg(dev, "%d align:%u,%u, bound:%ux%u", ctx->id,
walign, halign, framew, frameh);
ret = mdp_clamp_start(&left, 0, right, walign, s->flags);
if (ret)
return ret;
ret = mdp_clamp_start(&top, 0, bottom, halign, s->flags);
if (ret)
return ret;
ret = mdp_clamp_end(&right, left, framew, walign, s->flags);
if (ret)
return ret;
ret = mdp_clamp_end(&bottom, top, frameh, halign, s->flags);
if (ret)
return ret;
r->left = left;
r->top = top;
r->width = right - left;
r->height = bottom - top;
dev_dbg(dev, "%d crop:(%d,%d) %ux%u", ctx->id,
r->left, r->top, r->width, r->height);
return 0;
}
int mdp_check_scaling_ratio(const struct v4l2_rect *crop,
const struct v4l2_rect *compose, s32 rotation,
const struct mdp_limit *limit)
{
u32 crop_w, crop_h, comp_w, comp_h;
crop_w = crop->width;
crop_h = crop->height;
if (90 == rotation || 270 == rotation) {
comp_w = compose->height;
comp_h = compose->width;
} else {
comp_w = compose->width;
comp_h = compose->height;
}
if ((crop_w / comp_w) > limit->h_scale_down_max ||
(crop_h / comp_h) > limit->v_scale_down_max ||
(comp_w / crop_w) > limit->h_scale_up_max ||
(comp_h / crop_h) > limit->v_scale_up_max)
return -ERANGE;
return 0;
}
bool mdp_check_pp_enable(struct mdp_dev *mdp, struct mdp_frame *frame)
{
u32 s, r1, r2;
if (!mdp || !frame)
return false;
if (!mdp->mdp_data->pp_criteria)
return false;
s = mdp->mdp_data->pp_criteria->width *
mdp->mdp_data->pp_criteria->height;
r1 = frame->crop.c.width * frame->crop.c.height;
r2 = frame->compose.width * frame->compose.height;
return (r1 >= s || r2 >= s);
}
/* Stride that is accepted by MDP HW */
static u32 mdp_fmt_get_stride(const struct mdp_format *fmt,
u32 bytesperline, unsigned int plane)
{
enum mdp_color c = fmt->mdp_color;
u32 stride;
stride = (bytesperline * MDP_COLOR_BITS_PER_PIXEL(c))
/ fmt->row_depth[0];
if (plane == 0)
return stride;
if (plane < MDP_COLOR_GET_PLANE_COUNT(c)) {
if (MDP_COLOR_IS_BLOCK_MODE(c))
stride = stride / 2;
return stride;
}
return 0;
}
/* Stride that is accepted by MDP HW of format with contiguous planes */
static u32 mdp_fmt_get_stride_contig(const struct mdp_format *fmt,
u32 pix_stride, unsigned int plane)
{
enum mdp_color c = fmt->mdp_color;
u32 stride = pix_stride;
if (plane == 0)
return stride;
if (plane < MDP_COLOR_GET_PLANE_COUNT(c)) {
stride = stride >> MDP_COLOR_GET_H_SUBSAMPLE(c);
if (MDP_COLOR_IS_UV_COPLANE(c) && !MDP_COLOR_IS_BLOCK_MODE(c))
stride = stride * 2;
return stride;
}
return 0;
}
/* Plane size that is accepted by MDP HW */
static u32 mdp_fmt_get_plane_size(const struct mdp_format *fmt,
u32 stride, u32 height, unsigned int plane)
{
enum mdp_color c = fmt->mdp_color;
u32 bytesperline;
bytesperline = (stride * fmt->row_depth[0])
/ MDP_COLOR_BITS_PER_PIXEL(c);
if (plane == 0)
return bytesperline * height;
if (plane < MDP_COLOR_GET_PLANE_COUNT(c)) {
height = height >> MDP_COLOR_GET_V_SUBSAMPLE(c);
if (MDP_COLOR_IS_BLOCK_MODE(c))
bytesperline = bytesperline * 2;
return bytesperline * height;
}
return 0;
}
static void mdp_prepare_buffer(struct img_image_buffer *b,
struct mdp_frame *frame, struct vb2_buffer *vb)
{
struct v4l2_pix_format_mplane *pix_mp = &frame->format.fmt.pix_mp;
unsigned int i;
b->format.colorformat = frame->mdp_fmt->mdp_color;
b->format.ycbcr_prof = frame->ycbcr_prof;
for (i = 0; i < pix_mp->num_planes; ++i) {
u32 stride = mdp_fmt_get_stride(frame->mdp_fmt,
pix_mp->plane_fmt[i].bytesperline, i);
b->format.plane_fmt[i].stride = stride;
b->format.plane_fmt[i].size =
mdp_fmt_get_plane_size(frame->mdp_fmt, stride,
pix_mp->height, i);
b->iova[i] = vb2_dma_contig_plane_dma_addr(vb, i);
}
for (; i < MDP_COLOR_GET_PLANE_COUNT(b->format.colorformat); ++i) {
u32 stride = mdp_fmt_get_stride_contig(frame->mdp_fmt,
b->format.plane_fmt[0].stride, i);
b->format.plane_fmt[i].stride = stride;
b->format.plane_fmt[i].size =
mdp_fmt_get_plane_size(frame->mdp_fmt, stride,
pix_mp->height, i);
b->iova[i] = b->iova[i - 1] + b->format.plane_fmt[i - 1].size;
}
b->usage = frame->usage;
}
void mdp_set_src_config(struct img_input *in,
struct mdp_frame *frame, struct vb2_buffer *vb)
{
in->buffer.format.width = frame->format.fmt.pix_mp.width;
in->buffer.format.height = frame->format.fmt.pix_mp.height;
mdp_prepare_buffer(&in->buffer, frame, vb);
}
static u32 mdp_to_fixed(u32 *r, struct v4l2_fract *f)
{
u32 q;
if (f->denominator == 0) {
*r = 0;
return 0;
}
q = f->numerator / f->denominator;
*r = div_u64(((u64)f->numerator - q * f->denominator) <<
IMG_SUBPIXEL_SHIFT, f->denominator);
return q;
}
static void mdp_set_src_crop(struct img_crop *c, struct mdp_crop *crop)
{
c->left = crop->c.left
+ mdp_to_fixed(&c->left_subpix, &crop->left_subpix);
c->top = crop->c.top
+ mdp_to_fixed(&c->top_subpix, &crop->top_subpix);
c->width = crop->c.width
+ mdp_to_fixed(&c->width_subpix, &crop->width_subpix);
c->height = crop->c.height
+ mdp_to_fixed(&c->height_subpix, &crop->height_subpix);
}
static void mdp_set_orientation(struct img_output *out,
s32 rotation, bool hflip, bool vflip)
{
u8 flip = 0;
if (hflip)
flip ^= 1;
if (vflip) {
/*
* A vertical flip is equivalent to
* a 180-degree rotation with a horizontal flip
*/
rotation += 180;
flip ^= 1;
}
out->rotation = rotation % 360;
if (flip != 0)
out->flags |= IMG_CTRL_FLAG_HFLIP;
else
out->flags &= ~IMG_CTRL_FLAG_HFLIP;
}
void mdp_set_dst_config(struct img_output *out,
struct mdp_frame *frame, struct vb2_buffer *vb)
{
out->buffer.format.width = frame->compose.width;
out->buffer.format.height = frame->compose.height;
mdp_prepare_buffer(&out->buffer, frame, vb);
mdp_set_src_crop(&out->crop, &frame->crop);
mdp_set_orientation(out, frame->rotation, frame->hflip, frame->vflip);
}
int mdp_frameparam_init(struct mdp_dev *mdp, struct mdp_frameparam *param)
{
struct mdp_frame *frame;
if (!param)
return -EINVAL;
INIT_LIST_HEAD(¶m->list);
param->limit = mdp->mdp_data->def_limit;
param->type = MDP_STREAM_TYPE_BITBLT;
frame = ¶m->output;
frame->format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
frame->mdp_fmt = mdp_try_fmt_mplane(mdp, &frame->format, param, 0);
frame->ycbcr_prof =
mdp_map_ycbcr_prof_mplane(&frame->format,
frame->mdp_fmt->mdp_color);
frame->usage = MDP_BUFFER_USAGE_HW_READ;
param->num_captures = 1;
frame = ¶m->captures[0];
frame->format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
frame->mdp_fmt = mdp_try_fmt_mplane(mdp, &frame->format, param, 0);
frame->ycbcr_prof =
mdp_map_ycbcr_prof_mplane(&frame->format,
frame->mdp_fmt->mdp_color);
frame->usage = MDP_BUFFER_USAGE_MDP;
frame->crop.c.width = param->output.format.fmt.pix_mp.width;
frame->crop.c.height = param->output.format.fmt.pix_mp.height;
frame->compose.width = frame->format.fmt.pix_mp.width;
frame->compose.height = frame->format.fmt.pix_mp.height;
return 0;
}
|