Commit e622681d authored by Guennadi Liakhovetski's avatar Guennadi Liakhovetski Committed by Mauro Carvalho Chehab

V4L/DVB: sh_mobile_ceu_camera.c: preserve output window on VIDIOC_S_CROP

Current version of sh_mobile_ceu_camera.c interprets the V4L2 API specification
of the VIDIOC_S_CROP ioctl as "change input (for capture devices) area,
preserve scaling factors, therefore change output window," whereas a more
intuitive interpretation of the API is "change input area, preserve output
window." Switch sh_mobile_ceu_camera.c to use this interpretation.
Signed-off-by: default avatarGuennadi Liakhovetski <g.liakhovetski@gmx.de>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@redhat.com>
parent 730947bc
...@@ -115,9 +115,20 @@ struct sh_mobile_ceu_dev { ...@@ -115,9 +115,20 @@ struct sh_mobile_ceu_dev {
}; };
struct sh_mobile_ceu_cam { struct sh_mobile_ceu_cam {
struct v4l2_rect ceu_rect; /* CEU offsets within scaled by the CEU camera output */
unsigned int cam_width; unsigned int ceu_left;
unsigned int cam_height; unsigned int ceu_top;
/* Client output, as seen by the CEU */
unsigned int width;
unsigned int height;
/*
* User window from S_CROP / G_CROP, produced by client cropping and
* scaling, CEU scaling and CEU cropping, mapped back onto the client
* input window
*/
struct v4l2_rect subrect;
/* Camera cropping rectangle */
struct v4l2_rect rect;
const struct soc_mbus_pixelfmt *extra_fmt; const struct soc_mbus_pixelfmt *extra_fmt;
enum v4l2_mbus_pixelcode code; enum v4l2_mbus_pixelcode code;
}; };
...@@ -565,38 +576,36 @@ static u16 calc_scale(unsigned int src, unsigned int *dst) ...@@ -565,38 +576,36 @@ static u16 calc_scale(unsigned int src, unsigned int *dst)
} }
/* rect is guaranteed to not exceed the scaled camera rectangle */ /* rect is guaranteed to not exceed the scaled camera rectangle */
static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd, static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd)
unsigned int out_width,
unsigned int out_height)
{ {
struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent); struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
struct sh_mobile_ceu_cam *cam = icd->host_priv; struct sh_mobile_ceu_cam *cam = icd->host_priv;
struct v4l2_rect *rect = &cam->ceu_rect;
struct sh_mobile_ceu_dev *pcdev = ici->priv; struct sh_mobile_ceu_dev *pcdev = ici->priv;
unsigned int height, width, cdwdr_width, in_width, in_height; unsigned int height, width, cdwdr_width, in_width, in_height;
unsigned int left_offset, top_offset; unsigned int left_offset, top_offset;
u32 camor; u32 camor;
dev_dbg(icd->dev.parent, "Crop %ux%u@%u:%u\n", dev_geo(icd->dev.parent, "Crop %ux%u@%u:%u\n",
rect->width, rect->height, rect->left, rect->top); icd->user_width, icd->user_height, cam->ceu_left, cam->ceu_top);
left_offset = rect->left; left_offset = cam->ceu_left;
top_offset = rect->top; top_offset = cam->ceu_top;
/* CEU cropping (CFSZR) is applied _after_ the scaling filter (CFLCR) */
if (pcdev->image_mode) { if (pcdev->image_mode) {
in_width = rect->width; in_width = cam->width;
if (!pcdev->is_16bit) { if (!pcdev->is_16bit) {
in_width *= 2; in_width *= 2;
left_offset *= 2; left_offset *= 2;
} }
width = out_width; width = icd->user_width;
cdwdr_width = out_width; cdwdr_width = icd->user_width;
} else { } else {
int bytes_per_line = soc_mbus_bytes_per_line(out_width, int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
icd->current_fmt->host_fmt); icd->current_fmt->host_fmt);
unsigned int w_factor; unsigned int w_factor;
width = out_width; width = icd->user_width;
switch (icd->current_fmt->host_fmt->packing) { switch (icd->current_fmt->host_fmt->packing) {
case SOC_MBUS_PACKING_2X8_PADHI: case SOC_MBUS_PACKING_2X8_PADHI:
...@@ -606,17 +615,17 @@ static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd, ...@@ -606,17 +615,17 @@ static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd,
w_factor = 1; w_factor = 1;
} }
in_width = rect->width * w_factor; in_width = cam->width * w_factor;
left_offset = left_offset * w_factor; left_offset = left_offset * w_factor;
if (bytes_per_line < 0) if (bytes_per_line < 0)
cdwdr_width = out_width; cdwdr_width = icd->user_width;
else else
cdwdr_width = bytes_per_line; cdwdr_width = bytes_per_line;
} }
height = out_height; height = icd->user_height;
in_height = rect->height; in_height = cam->height;
if (V4L2_FIELD_NONE != pcdev->field) { if (V4L2_FIELD_NONE != pcdev->field) {
height /= 2; height /= 2;
in_height /= 2; in_height /= 2;
...@@ -775,9 +784,10 @@ static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd, ...@@ -775,9 +784,10 @@ static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
} }
ceu_write(pcdev, CAIFR, value); ceu_write(pcdev, CAIFR, value);
sh_mobile_ceu_set_rect(icd, icd->user_width, icd->user_height); sh_mobile_ceu_set_rect(icd);
mdelay(1); mdelay(1);
dev_geo(icd->dev.parent, "CFLCR 0x%x\n", pcdev->cflcr);
ceu_write(pcdev, CFLCR, pcdev->cflcr); ceu_write(pcdev, CFLCR, pcdev->cflcr);
/* /*
...@@ -866,6 +876,8 @@ static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt) ...@@ -866,6 +876,8 @@ static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt)
fmt->packing == SOC_MBUS_PACKING_EXTEND16); fmt->packing == SOC_MBUS_PACKING_EXTEND16);
} }
static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect);
static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx, static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
struct soc_camera_format_xlate *xlate) struct soc_camera_format_xlate *xlate)
{ {
...@@ -894,10 +906,55 @@ static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx, ...@@ -894,10 +906,55 @@ static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
return 0; return 0;
if (!icd->host_priv) { if (!icd->host_priv) {
struct v4l2_mbus_framefmt mf;
struct v4l2_rect rect;
struct device *dev = icd->dev.parent;
int shift = 0;
/* FIXME: subwindow is lost between close / open */
/* Cache current client geometry */
ret = client_g_rect(sd, &rect);
if (ret < 0)
return ret;
/* First time */
ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
if (ret < 0)
return ret;
while ((mf.width > 2560 || mf.height > 1920) && shift < 4) {
/* Try 2560x1920, 1280x960, 640x480, 320x240 */
mf.width = 2560 >> shift;
mf.height = 1920 >> shift;
ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
if (ret < 0)
return ret;
shift++;
}
if (shift == 4) {
dev_err(dev, "Failed to configure the client below %ux%x\n",
mf.width, mf.height);
return -EIO;
}
dev_geo(dev, "camera fmt %ux%u\n", mf.width, mf.height);
cam = kzalloc(sizeof(*cam), GFP_KERNEL); cam = kzalloc(sizeof(*cam), GFP_KERNEL);
if (!cam) if (!cam)
return -ENOMEM; return -ENOMEM;
/* We are called with current camera crop, initialise subrect with it */
cam->rect = rect;
cam->subrect = rect;
cam->width = mf.width;
cam->height = mf.height;
cam->width = mf.width;
cam->height = mf.height;
icd->host_priv = cam; icd->host_priv = cam;
} else { } else {
cam = icd->host_priv; cam = icd->host_priv;
...@@ -979,16 +1036,12 @@ static unsigned int scale_down(unsigned int size, unsigned int scale) ...@@ -979,16 +1036,12 @@ static unsigned int scale_down(unsigned int size, unsigned int scale)
return (size * 4096 + scale / 2) / scale; return (size * 4096 + scale / 2) / scale;
} }
static unsigned int scale_up(unsigned int size, unsigned int scale)
{
return (size * scale + 2048) / 4096;
}
static unsigned int calc_generic_scale(unsigned int input, unsigned int output) static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
{ {
return (input * 4096 + output / 2) / output; return (input * 4096 + output / 2) / output;
} }
/* Get and store current client crop */
static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect) static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
{ {
struct v4l2_crop crop; struct v4l2_crop crop;
...@@ -1007,25 +1060,51 @@ static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect) ...@@ -1007,25 +1060,51 @@ static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ret = v4l2_subdev_call(sd, video, cropcap, &cap); ret = v4l2_subdev_call(sd, video, cropcap, &cap);
if (ret < 0) if (!ret)
return ret;
*rect = cap.defrect; *rect = cap.defrect;
return ret; return ret;
} }
/* Client crop has changed, update our sub-rectangle to remain within the area */
static void update_subrect(struct sh_mobile_ceu_cam *cam)
{
struct v4l2_rect *rect = &cam->rect, *subrect = &cam->subrect;
if (rect->width < subrect->width)
subrect->width = rect->width;
if (rect->height < subrect->height)
subrect->height = rect->height;
if (rect->left > subrect->left)
subrect->left = rect->left;
else if (rect->left + rect->width >
subrect->left + subrect->width)
subrect->left = rect->left + rect->width -
subrect->width;
if (rect->top > subrect->top)
subrect->top = rect->top;
else if (rect->top + rect->height >
subrect->top + subrect->height)
subrect->top = rect->top + rect->height -
subrect->height;
}
/* /*
* The common for both scaling and cropping iterative approach is: * The common for both scaling and cropping iterative approach is:
* 1. try if the client can produce exactly what requested by the user * 1. try if the client can produce exactly what requested by the user
* 2. if (1) failed, try to double the client image until we get one big enough * 2. if (1) failed, try to double the client image until we get one big enough
* 3. if (2) failed, try to request the maximum image * 3. if (2) failed, try to request the maximum image
*/ */
static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop, static int client_s_crop(struct soc_camera_device *icd, struct v4l2_crop *crop,
struct v4l2_crop *cam_crop) struct v4l2_crop *cam_crop)
{ {
struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c; struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
struct device *dev = sd->v4l2_dev->dev; struct device *dev = sd->v4l2_dev->dev;
struct sh_mobile_ceu_cam *cam = icd->host_priv;
struct v4l2_cropcap cap; struct v4l2_cropcap cap;
int ret; int ret;
unsigned int width, height; unsigned int width, height;
...@@ -1043,6 +1122,7 @@ static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop, ...@@ -1043,6 +1122,7 @@ static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop,
/* Even if camera S_CROP failed, but camera rectangle matches */ /* Even if camera S_CROP failed, but camera rectangle matches */
dev_dbg(dev, "Camera S_CROP successful for %dx%d@%d:%d\n", dev_dbg(dev, "Camera S_CROP successful for %dx%d@%d:%d\n",
rect->width, rect->height, rect->left, rect->top); rect->width, rect->height, rect->left, rect->top);
cam->rect = *cam_rect;
return 0; return 0;
} }
...@@ -1057,6 +1137,7 @@ static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop, ...@@ -1057,6 +1137,7 @@ static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop,
if (ret < 0) if (ret < 0)
return ret; return ret;
/* Put user requested rectangle within sensor bounds */
soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2, soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
cap.bounds.width); cap.bounds.width);
soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4, soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
...@@ -1069,6 +1150,10 @@ static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop, ...@@ -1069,6 +1150,10 @@ static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop,
width = max(cam_rect->width, 2); width = max(cam_rect->width, 2);
height = max(cam_rect->height, 2); height = max(cam_rect->height, 2);
/*
* Loop as long as sensor is not covering the requested rectangle and
* is still within its bounds
*/
while (!ret && (is_smaller(cam_rect, rect) || while (!ret && (is_smaller(cam_rect, rect) ||
is_inside(cam_rect, rect)) && is_inside(cam_rect, rect)) &&
(cap.bounds.width > width || cap.bounds.height > height)) { (cap.bounds.width > width || cap.bounds.height > height)) {
...@@ -1086,6 +1171,7 @@ static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop, ...@@ -1086,6 +1171,7 @@ static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop,
* target left, set it to the middle point between the current * target left, set it to the middle point between the current
* left and minimum left. But that would add too much * left and minimum left. But that would add too much
* complexity: we would have to iterate each border separately. * complexity: we would have to iterate each border separately.
* Instead we just drop to the left and top bounds.
*/ */
if (cam_rect->left > rect->left) if (cam_rect->left > rect->left)
cam_rect->left = cap.bounds.left; cam_rect->left = cap.bounds.left;
...@@ -1122,83 +1208,34 @@ static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop, ...@@ -1122,83 +1208,34 @@ static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop,
cam_rect->left, cam_rect->top); cam_rect->left, cam_rect->top);
} }
return ret; if (!ret) {
} cam->rect = *cam_rect;
update_subrect(cam);
static int get_camera_scales(struct v4l2_subdev *sd, struct v4l2_rect *rect, }
unsigned int *scale_h, unsigned int *scale_v)
{
struct v4l2_mbus_framefmt mf;
int ret;
ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
if (ret < 0)
return ret; return ret;
*scale_h = calc_generic_scale(rect->width, mf.width);
*scale_v = calc_generic_scale(rect->height, mf.height);
return 0;
} }
static int get_camera_subwin(struct soc_camera_device *icd, /* Iterative s_mbus_fmt, also updates cached client crop on success */
struct v4l2_rect *cam_subrect, static int client_s_fmt(struct soc_camera_device *icd,
unsigned int cam_hscale, unsigned int cam_vscale) struct v4l2_mbus_framefmt *mf, bool ceu_can_scale)
{ {
struct sh_mobile_ceu_cam *cam = icd->host_priv; struct sh_mobile_ceu_cam *cam = icd->host_priv;
struct v4l2_rect *ceu_rect = &cam->ceu_rect;
if (!ceu_rect->width) {
struct v4l2_subdev *sd = soc_camera_to_subdev(icd); struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
struct device *dev = icd->dev.parent; struct device *dev = icd->dev.parent;
struct v4l2_mbus_framefmt mf; unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h;
unsigned int max_width, max_height;
struct v4l2_cropcap cap;
int ret; int ret;
/* First time */
ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf); ret = v4l2_subdev_call(sd, video, s_mbus_fmt, mf);
if (ret < 0) if (ret < 0)
return ret; return ret;
dev_geo(dev, "camera fmt %ux%u\n", mf.width, mf.height); dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height);
if (mf.width > 2560) {
ceu_rect->width = 2560;
ceu_rect->left = (mf.width - 2560) / 2;
} else {
ceu_rect->width = mf.width;
ceu_rect->left = 0;
}
if (mf.height > 1920) {
ceu_rect->height = 1920;
ceu_rect->top = (mf.height - 1920) / 2;
} else {
ceu_rect->height = mf.height;
ceu_rect->top = 0;
}
dev_geo(dev, "initialised CEU rect %ux%u@%u:%u\n",
ceu_rect->width, ceu_rect->height,
ceu_rect->left, ceu_rect->top);
}
cam_subrect->width = scale_up(ceu_rect->width, cam_hscale);
cam_subrect->left = scale_up(ceu_rect->left, cam_hscale);
cam_subrect->height = scale_up(ceu_rect->height, cam_vscale);
cam_subrect->top = scale_up(ceu_rect->top, cam_vscale);
return 0;
}
static int client_s_fmt(struct soc_camera_device *icd, if ((width == mf->width && height == mf->height) || !ceu_can_scale)
struct v4l2_mbus_framefmt *mf, bool ceu_can_scale) goto update_cache;
{
struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
struct device *dev = icd->dev.parent;
unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h;
unsigned int max_width, max_height;
struct v4l2_cropcap cap;
int ret;
cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
...@@ -1209,15 +1246,6 @@ static int client_s_fmt(struct soc_camera_device *icd, ...@@ -1209,15 +1246,6 @@ static int client_s_fmt(struct soc_camera_device *icd,
max_width = min(cap.bounds.width, 2560); max_width = min(cap.bounds.width, 2560);
max_height = min(cap.bounds.height, 1920); max_height = min(cap.bounds.height, 1920);
ret = v4l2_subdev_call(sd, video, s_mbus_fmt, mf);
if (ret < 0)
return ret;
dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height);
if ((width == mf->width && height == mf->height) || !ceu_can_scale)
return 0;
/* Camera set a format, but geometry is not precise, try to improve */ /* Camera set a format, but geometry is not precise, try to improve */
tmp_w = mf->width; tmp_w = mf->width;
tmp_h = mf->height; tmp_h = mf->height;
...@@ -1239,26 +1267,37 @@ static int client_s_fmt(struct soc_camera_device *icd, ...@@ -1239,26 +1267,37 @@ static int client_s_fmt(struct soc_camera_device *icd,
} }
} }
update_cache:
/* Update cache */
ret = client_g_rect(sd, &cam->rect);
if (ret < 0)
return ret;
update_subrect(cam);
return 0; return 0;
} }
/** /**
* @rect - camera cropped rectangle * @width - on output: user width, mapped back to input
* @sub_rect - CEU cropped rectangle, mapped back to camera input area * @height - on output: user height, mapped back to input
* @ceu_rect - on output calculated CEU crop rectangle * @mf - in- / output camera output window
*/ */
static int client_scale(struct soc_camera_device *icd, struct v4l2_rect *rect, static int client_scale(struct soc_camera_device *icd,
struct v4l2_rect *sub_rect, struct v4l2_rect *ceu_rect, struct v4l2_mbus_framefmt *mf,
struct v4l2_mbus_framefmt *mf, bool ceu_can_scale) unsigned int *width, unsigned int *height,
bool ceu_can_scale)
{ {
struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
struct sh_mobile_ceu_cam *cam = icd->host_priv; struct sh_mobile_ceu_cam *cam = icd->host_priv;
struct device *dev = icd->dev.parent; struct device *dev = icd->dev.parent;
struct v4l2_mbus_framefmt mf_tmp = *mf; struct v4l2_mbus_framefmt mf_tmp = *mf;
unsigned int scale_h, scale_v; unsigned int scale_h, scale_v;
int ret; int ret;
/* 5. Apply iterative camera S_FMT for camera user window. */ /*
* 5. Apply iterative camera S_FMT for camera user window (also updates
* client crop cache and the imaginary sub-rectangle).
*/
ret = client_s_fmt(icd, &mf_tmp, ceu_can_scale); ret = client_s_fmt(icd, &mf_tmp, ceu_can_scale);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -1270,60 +1309,22 @@ static int client_scale(struct soc_camera_device *icd, struct v4l2_rect *rect, ...@@ -1270,60 +1309,22 @@ static int client_scale(struct soc_camera_device *icd, struct v4l2_rect *rect,
/* unneeded - it is already in "mf_tmp" */ /* unneeded - it is already in "mf_tmp" */
/* 7. Calculate new camera scales. */ /* 7. Calculate new client scales. */
ret = get_camera_scales(sd, rect, &scale_h, &scale_v); scale_h = calc_generic_scale(cam->rect.width, mf_tmp.width);
if (ret < 0) scale_v = calc_generic_scale(cam->rect.height, mf_tmp.height);
return ret;
dev_geo(dev, "7: camera scales %u:%u\n", scale_h, scale_v);
cam->cam_width = mf_tmp.width;
cam->cam_height = mf_tmp.height;
mf->width = mf_tmp.width; mf->width = mf_tmp.width;
mf->height = mf_tmp.height; mf->height = mf_tmp.height;
mf->colorspace = mf_tmp.colorspace; mf->colorspace = mf_tmp.colorspace;
/* /*
* 8. Calculate new CEU crop - apply camera scales to previously * 8. Calculate new CEU crop - apply camera scales to previously
* calculated "effective" crop. * updated "effective" crop.
*/ */
ceu_rect->left = scale_down(sub_rect->left, scale_h); *width = scale_down(cam->subrect.width, scale_h);
ceu_rect->width = scale_down(sub_rect->width, scale_h); *height = scale_down(cam->subrect.height, scale_v);
ceu_rect->top = scale_down(sub_rect->top, scale_v);
ceu_rect->height = scale_down(sub_rect->height, scale_v);
dev_geo(dev, "8: new CEU rect %ux%u@%u:%u\n",
ceu_rect->width, ceu_rect->height,
ceu_rect->left, ceu_rect->top);
return 0;
}
/* Get combined scales */
static int get_scales(struct soc_camera_device *icd,
unsigned int *scale_h, unsigned int *scale_v)
{
struct sh_mobile_ceu_cam *cam = icd->host_priv;
struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
struct v4l2_crop cam_crop;
unsigned int width_in, height_in;
int ret;
cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; dev_geo(dev, "8: new client sub-window %ux%u\n", *width, *height);
ret = client_g_rect(sd, &cam_crop.c);
if (ret < 0)
return ret;
ret = get_camera_scales(sd, &cam_crop.c, scale_h, scale_v);
if (ret < 0)
return ret;
width_in = scale_up(cam->ceu_rect.width, *scale_h);
height_in = scale_up(cam->ceu_rect.height, *scale_v);
*scale_h = calc_generic_scale(width_in, icd->user_width);
*scale_v = calc_generic_scale(height_in, icd->user_height);
return 0; return 0;
} }
...@@ -1342,115 +1343,165 @@ static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd, ...@@ -1342,115 +1343,165 @@ static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
struct sh_mobile_ceu_dev *pcdev = ici->priv; struct sh_mobile_ceu_dev *pcdev = ici->priv;
struct v4l2_crop cam_crop; struct v4l2_crop cam_crop;
struct sh_mobile_ceu_cam *cam = icd->host_priv; struct sh_mobile_ceu_cam *cam = icd->host_priv;
struct v4l2_rect *cam_rect = &cam_crop.c, *ceu_rect = &cam->ceu_rect; struct v4l2_rect *cam_rect = &cam_crop.c;
struct v4l2_subdev *sd = soc_camera_to_subdev(icd); struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
struct device *dev = icd->dev.parent; struct device *dev = icd->dev.parent;
struct v4l2_mbus_framefmt mf; struct v4l2_mbus_framefmt mf;
unsigned int scale_comb_h, scale_comb_v, scale_ceu_h, scale_ceu_v, unsigned int scale_cam_h, scale_cam_v, scale_ceu_h, scale_ceu_v,
out_width, out_height; out_width, out_height, scale_h, scale_v;
int interm_width, interm_height;
u32 capsr, cflcr; u32 capsr, cflcr;
int ret; int ret;
/* 1. Calculate current combined scales. */ dev_geo(dev, "S_CROP(%ux%u@%u:%u)\n", rect->width, rect->height,
ret = get_scales(icd, &scale_comb_h, &scale_comb_v); rect->left, rect->top);
if (ret < 0)
return ret;
dev_geo(dev, "1: combined scales %u:%u\n", scale_comb_h, scale_comb_v); /* During camera cropping its output window can change too, stop CEU */
capsr = capture_save_reset(pcdev);
dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
/* 2. Apply iterative camera S_CROP for new input window. */ /* 1. - 2. Apply iterative camera S_CROP for new input window. */
ret = client_s_crop(sd, a, &cam_crop); ret = client_s_crop(icd, a, &cam_crop);
if (ret < 0) if (ret < 0)
return ret; return ret;
dev_geo(dev, "2: camera cropped to %ux%u@%u:%u\n", dev_geo(dev, "1-2: camera cropped to %ux%u@%u:%u\n",
cam_rect->width, cam_rect->height, cam_rect->width, cam_rect->height,
cam_rect->left, cam_rect->top); cam_rect->left, cam_rect->top);
/* On success cam_crop contains current camera crop */ /* On success cam_crop contains current camera crop */
/* /* 3. Retrieve camera output window */
* 3. If old combined scales applied to new crop produce an impossible ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
* user window, adjust scales to produce nearest possible window. if (ret < 0)
*/ return ret;
out_width = scale_down(rect->width, scale_comb_h);
out_height = scale_down(rect->height, scale_comb_v);
if (out_width > 2560)
out_width = 2560;
else if (out_width < 2)
out_width = 2;
if (out_height > 1920)
out_height = 1920;
else if (out_height < 4)
out_height = 4;
dev_geo(dev, "3: Adjusted output %ux%u\n", out_width, out_height);
/* 4. Use G_CROP to retrieve actual input window: already in cam_crop */
/*
* 5. Using actual input window and calculated combined scales calculate
* camera target output window.
*/
mf.width = scale_down(cam_rect->width, scale_comb_h);
mf.height = scale_down(cam_rect->height, scale_comb_v);
dev_geo(dev, "5: camera target %ux%u\n", mf.width, mf.height);
/* 6. - 9. */
mf.code = cam->code;
mf.field = pcdev->field;
capsr = capture_save_reset(pcdev);
dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
/* Make relative to camera rectangle */ if (mf.width > 2560 || mf.height > 1920)
rect->left -= cam_rect->left; return -EINVAL;
rect->top -= cam_rect->top;
ret = client_scale(icd, cam_rect, rect, ceu_rect, &mf, /* Cache camera output window */
pcdev->image_mode && cam->width = mf.width;
V4L2_FIELD_NONE == pcdev->field); cam->height = mf.height;
dev_geo(dev, "6-9: %d\n", ret); /* 4. Calculate camera scales */
scale_cam_h = calc_generic_scale(cam_rect->width, mf.width);
scale_cam_v = calc_generic_scale(cam_rect->height, mf.height);
/* 10. Use CEU cropping to crop to the new window. */ /* Calculate intermediate window */
sh_mobile_ceu_set_rect(icd, out_width, out_height); interm_width = scale_down(rect->width, scale_cam_h);
interm_height = scale_down(rect->height, scale_cam_v);
dev_geo(dev, "10: CEU cropped to %ux%u@%u:%u\n", if (pcdev->image_mode) {
ceu_rect->width, ceu_rect->height, out_width = min(interm_width, icd->user_width);
ceu_rect->left, ceu_rect->top); out_height = min(interm_height, icd->user_height);
} else {
out_width = interm_width;
out_height = interm_height;
}
/* /*
* 11. Calculate CEU scales from camera scales from results of (10) and * 5. Calculate CEU scales from camera scales from results of (5) and
* user window from (3) * the user window
*/ */
scale_ceu_h = calc_scale(ceu_rect->width, &out_width); scale_ceu_h = calc_scale(interm_width, &out_width);
scale_ceu_v = calc_scale(ceu_rect->height, &out_height); scale_ceu_v = calc_scale(interm_height, &out_height);
dev_geo(dev, "11: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v); /* Calculate camera scales */
scale_h = calc_generic_scale(cam_rect->width, out_width);
scale_v = calc_generic_scale(cam_rect->height, out_height);
/* 12. Apply CEU scales. */ dev_geo(dev, "5: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
/* Apply CEU scales. */
cflcr = scale_ceu_h | (scale_ceu_v << 16); cflcr = scale_ceu_h | (scale_ceu_v << 16);
if (cflcr != pcdev->cflcr) { if (cflcr != pcdev->cflcr) {
pcdev->cflcr = cflcr; pcdev->cflcr = cflcr;
ceu_write(pcdev, CFLCR, cflcr); ceu_write(pcdev, CFLCR, cflcr);
} }
icd->user_width = out_width;
icd->user_height = out_height;
cam->ceu_left = scale_down(rect->left - cam_rect->left, scale_h) & ~1;
cam->ceu_top = scale_down(rect->top - cam_rect->top, scale_v) & ~1;
/* 6. Use CEU cropping to crop to the new window. */
sh_mobile_ceu_set_rect(icd);
cam->subrect = *rect;
dev_geo(dev, "6: CEU cropped to %ux%u@%u:%u\n",
icd->user_width, icd->user_height,
cam->ceu_left, cam->ceu_top);
/* Restore capture */ /* Restore capture */
if (pcdev->active) if (pcdev->active)
capsr |= 1; capsr |= 1;
capture_restore(pcdev, capsr); capture_restore(pcdev, capsr);
icd->user_width = out_width;
icd->user_height = out_height;
/* Even if only camera cropping succeeded */ /* Even if only camera cropping succeeded */
return ret; return ret;
} }
static int sh_mobile_ceu_get_crop(struct soc_camera_device *icd,
struct v4l2_crop *a)
{
struct sh_mobile_ceu_cam *cam = icd->host_priv;
a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
a->c = cam->subrect;
return 0;
}
/*
* Calculate real client output window by applying new scales to the current
* client crop. New scales are calculated from the requested output format and
* CEU crop, mapped backed onto the client input (subrect).
*/
static void calculate_client_output(struct soc_camera_device *icd,
struct v4l2_pix_format *pix, struct v4l2_mbus_framefmt *mf)
{
struct sh_mobile_ceu_cam *cam = icd->host_priv;
struct device *dev = icd->dev.parent;
struct v4l2_rect *cam_subrect = &cam->subrect;
unsigned int scale_v, scale_h;
if (cam_subrect->width == cam->rect.width &&
cam_subrect->height == cam->rect.height) {
/* No sub-cropping */
mf->width = pix->width;
mf->height = pix->height;
return;
}
/* 1.-2. Current camera scales and subwin - cached. */
dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
cam_subrect->width, cam_subrect->height,
cam_subrect->left, cam_subrect->top);
/*
* 3. Calculate new combined scales from input sub-window to requested
* user window.
*/
/*
* TODO: CEU cannot scale images larger than VGA to smaller than SubQCIF
* (128x96) or larger than VGA
*/
scale_h = calc_generic_scale(cam_subrect->width, pix->width);
scale_v = calc_generic_scale(cam_subrect->height, pix->height);
dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
/*
* 4. Calculate client output window by applying combined scales to real
* input window.
*/
mf->width = scale_down(cam->rect.width, scale_h);
mf->height = scale_down(cam->rect.height, scale_v);
}
/* Similar to set_crop multistage iterative algorithm */ /* Similar to set_crop multistage iterative algorithm */
static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd, static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
struct v4l2_format *f) struct v4l2_format *f)
...@@ -1460,18 +1511,17 @@ static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd, ...@@ -1460,18 +1511,17 @@ static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
struct sh_mobile_ceu_cam *cam = icd->host_priv; struct sh_mobile_ceu_cam *cam = icd->host_priv;
struct v4l2_pix_format *pix = &f->fmt.pix; struct v4l2_pix_format *pix = &f->fmt.pix;
struct v4l2_mbus_framefmt mf; struct v4l2_mbus_framefmt mf;
struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
struct device *dev = icd->dev.parent; struct device *dev = icd->dev.parent;
__u32 pixfmt = pix->pixelformat; __u32 pixfmt = pix->pixelformat;
const struct soc_camera_format_xlate *xlate; const struct soc_camera_format_xlate *xlate;
struct v4l2_crop cam_crop; unsigned int ceu_sub_width, ceu_sub_height;
struct v4l2_rect *cam_rect = &cam_crop.c, cam_subrect, ceu_rect;
unsigned int scale_cam_h, scale_cam_v;
u16 scale_v, scale_h; u16 scale_v, scale_h;
int ret; int ret;
bool image_mode; bool image_mode;
enum v4l2_field field; enum v4l2_field field;
dev_geo(dev, "S_FMT(pix=0x%x, %ux%u)\n", pixfmt, pix->width, pix->height);
switch (pix->field) { switch (pix->field) {
default: default:
pix->field = V4L2_FIELD_NONE; pix->field = V4L2_FIELD_NONE;
...@@ -1492,46 +1542,8 @@ static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd, ...@@ -1492,46 +1542,8 @@ static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
return -EINVAL; return -EINVAL;
} }
/* 1. Calculate current camera scales. */ /* 1.-4. Calculate client output geometry */
cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; calculate_client_output(icd, &f->fmt.pix, &mf);
ret = client_g_rect(sd, cam_rect);
if (ret < 0)
return ret;
ret = get_camera_scales(sd, cam_rect, &scale_cam_h, &scale_cam_v);
if (ret < 0)
return ret;
dev_geo(dev, "1: camera scales %u:%u\n", scale_cam_h, scale_cam_v);
/*
* 2. Calculate "effective" input crop (sensor subwindow) - CEU crop
* scaled back at current camera scales onto input window.
*/
ret = get_camera_subwin(icd, &cam_subrect, scale_cam_h, scale_cam_v);
if (ret < 0)
return ret;
dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
cam_subrect.width, cam_subrect.height,
cam_subrect.left, cam_subrect.top);
/*
* 3. Calculate new combined scales from "effective" input window to
* requested user window.
*/
scale_h = calc_generic_scale(cam_subrect.width, pix->width);
scale_v = calc_generic_scale(cam_subrect.height, pix->height);
dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
/*
* 4. Calculate camera output window by applying combined scales to real
* input window.
*/
mf.width = scale_down(cam_rect->width, scale_h);
mf.height = scale_down(cam_rect->height, scale_v);
mf.field = pix->field; mf.field = pix->field;
mf.colorspace = pix->colorspace; mf.colorspace = pix->colorspace;
mf.code = xlate->code; mf.code = xlate->code;
...@@ -1547,17 +1559,17 @@ static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd, ...@@ -1547,17 +1559,17 @@ static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
image_mode = false; image_mode = false;
} }
dev_geo(dev, "4: camera output %ux%u\n", mf.width, mf.height); dev_geo(dev, "4: request camera output %ux%u\n", mf.width, mf.height);
/* 5. - 9. */ /* 5. - 9. */
ret = client_scale(icd, cam_rect, &cam_subrect, &ceu_rect, &mf, ret = client_scale(icd, &mf, &ceu_sub_width, &ceu_sub_height,
image_mode && V4L2_FIELD_NONE == field); image_mode && V4L2_FIELD_NONE == field);
dev_geo(dev, "5-9: client scale %d\n", ret); dev_geo(dev, "5-9: client scale return %d\n", ret);
/* Done with the camera. Now see if we can improve the result */ /* Done with the camera. Now see if we can improve the result */
dev_dbg(dev, "Camera %d fmt %ux%u, requested %ux%u\n", dev_geo(dev, "Camera %d fmt %ux%u, requested %ux%u\n",
ret, mf.width, mf.height, pix->width, pix->height); ret, mf.width, mf.height, pix->width, pix->height);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -1565,40 +1577,44 @@ static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd, ...@@ -1565,40 +1577,44 @@ static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
if (mf.code != xlate->code) if (mf.code != xlate->code)
return -EINVAL; return -EINVAL;
/* 9. Prepare CEU crop */
cam->width = mf.width;
cam->height = mf.height;
/* 10. Use CEU scaling to scale to the requested user window. */ /* 10. Use CEU scaling to scale to the requested user window. */
/* We cannot scale up */ /* We cannot scale up */
if (pix->width > mf.width) if (pix->width > ceu_sub_width)
pix->width = mf.width; ceu_sub_width = pix->width;
if (pix->width > ceu_rect.width)
pix->width = ceu_rect.width;
if (pix->height > mf.height) if (pix->height > ceu_sub_height)
pix->height = mf.height; ceu_sub_height = pix->height;
if (pix->height > ceu_rect.height)
pix->height = ceu_rect.height;
pix->colorspace = mf.colorspace; pix->colorspace = mf.colorspace;
if (image_mode) { if (image_mode) {
/* Scale pix->{width x height} down to width x height */ /* Scale pix->{width x height} down to width x height */
scale_h = calc_scale(ceu_rect.width, &pix->width); scale_h = calc_scale(ceu_sub_width, &pix->width);
scale_v = calc_scale(ceu_rect.height, &pix->height); scale_v = calc_scale(ceu_sub_height, &pix->height);
pcdev->cflcr = scale_h | (scale_v << 16);
} else { } else {
pix->width = ceu_rect.width; pix->width = ceu_sub_width;
pix->height = ceu_rect.height; pix->height = ceu_sub_height;
scale_h = scale_v = 0; scale_h = 0;
pcdev->cflcr = 0; scale_v = 0;
} }
pcdev->cflcr = scale_h | (scale_v << 16);
/*
* We have calculated CFLCR, the actual configuration will be performed
* in sh_mobile_ceu_set_bus_param()
*/
dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n", dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
ceu_rect.width, scale_h, pix->width, ceu_sub_width, scale_h, pix->width,
ceu_rect.height, scale_v, pix->height); ceu_sub_height, scale_v, pix->height);
cam->code = xlate->code; cam->code = xlate->code;
cam->ceu_rect = ceu_rect;
icd->current_fmt = xlate; icd->current_fmt = xlate;
pcdev->field = field; pcdev->field = field;
...@@ -1820,6 +1836,7 @@ static struct soc_camera_host_ops sh_mobile_ceu_host_ops = { ...@@ -1820,6 +1836,7 @@ static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
.remove = sh_mobile_ceu_remove_device, .remove = sh_mobile_ceu_remove_device,
.get_formats = sh_mobile_ceu_get_formats, .get_formats = sh_mobile_ceu_get_formats,
.put_formats = sh_mobile_ceu_put_formats, .put_formats = sh_mobile_ceu_put_formats,
.get_crop = sh_mobile_ceu_get_crop,
.set_crop = sh_mobile_ceu_set_crop, .set_crop = sh_mobile_ceu_set_crop,
.set_fmt = sh_mobile_ceu_set_fmt, .set_fmt = sh_mobile_ceu_set_fmt,
.try_fmt = sh_mobile_ceu_try_fmt, .try_fmt = sh_mobile_ceu_try_fmt,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment