Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[media] stk1160: Add frame scaling support

This commit implements frame decimation for stk1160, which allows
to support format changes instead of a static frame size.

The stk1160 supports independent row and column decimation, in two
different modes:
* set a number of rows/columns units to skip for each unit sent.
* set a number of rows/columns units to send for each unit skipped.

This effectively allows to achieve different frame scaling ratios.

The unit number can be set to either two row/columns sent/skipped,
or four row/columns sent/skipped. Since the video format (UYVY)
has 4-bytes, using a unit number of two row/columns results
in frame color 'shifting', so set to four row/columns sent/skipped.

Signed-off-by: Michael Stegemann <michael@stegemann.it>
Signed-off-by: Dale Hamel <dale.hamel@srvthe.net>
Signed-off-by: Ezequiel Garcia <ezequiel@vanguardiasur.com.ar>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>

authored by

Ezequiel Garcia and committed by
Mauro Carvalho Chehab
d3194520 890024ad

+211 -32
+34
drivers/media/usb/stk1160/stk1160-reg.h
··· 33 33 */ 34 34 #define STK1160_DCTRL 0x100 35 35 36 + /* 37 + * Decimation Control Register: 38 + * Byte 104: Horizontal Decimation Line Unit Count 39 + * Byte 105: Vertical Decimation Line Unit Count 40 + * Byte 106: Decimation Control 41 + * Bit 0 - Horizontal Decimation Control 42 + * 0 Horizontal decimation is disabled. 43 + * 1 Horizontal decimation is enabled. 44 + * Bit 1 - Decimates Half or More Column 45 + * 0 Decimates less than half from original column, 46 + * send count unit (0x105) before each unit skipped. 47 + * 1 Decimates half or more from original column, 48 + * skip count unit (0x105) before each unit sent. 49 + * Bit 2 - Vertical Decimation Control 50 + * 0 Vertical decimation is disabled. 51 + * 1 Vertical decimation is enabled. 52 + * Bit 3 - Vertical Greater or Equal to Half 53 + * 0 Decimates less than half from original row, 54 + * send count unit (0x105) before each unit skipped. 55 + * 1 Decimates half or more from original row, 56 + * skip count unit (0x105) before each unit sent. 57 + * Bit 4 - Decimation Unit 58 + * 0 Decimation will work with 2 rows or columns per unit. 59 + * 1 Decimation will work with 4 rows or columns per unit. 60 + */ 61 + #define STK1160_DMCTRL_H_UNITS 0x104 62 + #define STK1160_DMCTRL_V_UNITS 0x105 63 + #define STK1160_DMCTRL 0x106 64 + #define STK1160_H_DEC_EN BIT(0) 65 + #define STK1160_H_DEC_MODE BIT(1) 66 + #define STK1160_V_DEC_EN BIT(2) 67 + #define STK1160_V_DEC_MODE BIT(3) 68 + #define STK1160_DEC_UNIT_SIZE BIT(4) 69 + 36 70 /* Capture Frame Start Position */ 37 71 #define STK116_CFSPO 0x110 38 72 #define STK116_CFSPO_STX_L 0x110
+177 -32
drivers/media/usb/stk1160/stk1160-v4l.c
··· 42 42 module_param(keep_buffers, bool, 0644); 43 43 MODULE_PARM_DESC(keep_buffers, "don't release buffers upon stop streaming"); 44 44 45 + enum stk1160_decimate_mode { 46 + STK1160_DECIMATE_MORE_THAN_HALF, 47 + STK1160_DECIMATE_LESS_THAN_HALF, 48 + }; 49 + 50 + struct stk1160_decimate_ctrl { 51 + bool col_en, row_en; 52 + enum stk1160_decimate_mode col_mode, row_mode; 53 + unsigned int col_n, row_n; 54 + }; 55 + 45 56 /* supported video standards */ 46 57 static struct stk1160_fmt format[] = { 47 58 { ··· 61 50 .depth = 16, 62 51 } 63 52 }; 53 + 54 + /* 55 + * Helper to find the next divisor that results in modulo being zero. 56 + * This is required to guarantee valid decimation unit counts. 57 + */ 58 + static unsigned int 59 + div_round_integer(unsigned int x, unsigned int y) 60 + { 61 + for (;; y++) { 62 + if (x % y == 0) 63 + return x / y; 64 + } 65 + } 64 66 65 67 static void stk1160_set_std(struct stk1160 *dev) 66 68 { ··· 128 104 stk1160_write_reg(dev, std625[i].reg, std625[i].val); 129 105 } 130 106 107 + } 108 + 109 + static void stk1160_set_fmt(struct stk1160 *dev, 110 + struct stk1160_decimate_ctrl *ctrl) 111 + { 112 + u32 val = 0; 113 + 114 + if (ctrl) { 115 + /* 116 + * Since the format is UYVY, the device must skip or send 117 + * a number of rows/columns multiple of four. This way, the 118 + * colour format is preserved. The STK1160_DEC_UNIT_SIZE bit 119 + * does exactly this. 120 + */ 121 + val |= STK1160_DEC_UNIT_SIZE; 122 + val |= ctrl->col_en ? STK1160_H_DEC_EN : 0; 123 + val |= ctrl->row_en ? STK1160_V_DEC_EN : 0; 124 + val |= ctrl->col_mode == 125 + STK1160_DECIMATE_MORE_THAN_HALF ? 126 + STK1160_H_DEC_MODE : 0; 127 + val |= ctrl->row_mode == 128 + STK1160_DECIMATE_MORE_THAN_HALF ? 129 + STK1160_V_DEC_MODE : 0; 130 + 131 + /* Horizontal count units */ 132 + stk1160_write_reg(dev, STK1160_DMCTRL_H_UNITS, ctrl->col_n); 133 + /* Vertical count units */ 134 + stk1160_write_reg(dev, STK1160_DMCTRL_V_UNITS, ctrl->row_n); 135 + 136 + stk1160_dbg("decimate 0x%x, column units %d, row units %d\n", 137 + val, ctrl->col_n, ctrl->row_n); 138 + } 139 + 140 + /* Decimation control */ 141 + stk1160_write_reg(dev, STK1160_DMCTRL, val); 131 142 } 132 143 133 144 /* ··· 382 323 return 0; 383 324 } 384 325 326 + static int stk1160_try_fmt(struct stk1160 *dev, struct v4l2_format *f, 327 + struct stk1160_decimate_ctrl *ctrl) 328 + { 329 + unsigned int width, height; 330 + unsigned int base_width, base_height; 331 + unsigned int col_n, row_n; 332 + enum stk1160_decimate_mode col_mode, row_mode; 333 + bool col_en, row_en; 334 + 335 + base_width = 720; 336 + base_height = (dev->norm & V4L2_STD_525_60) ? 480 : 576; 337 + 338 + /* Minimum width and height is 5% the frame size */ 339 + width = clamp_t(unsigned int, f->fmt.pix.width, 340 + base_width / 20, base_width); 341 + height = clamp_t(unsigned int, f->fmt.pix.height, 342 + base_height / 20, base_height); 343 + 344 + /* Let's set default no decimation values */ 345 + col_n = 0; 346 + row_n = 0; 347 + col_en = false; 348 + row_en = false; 349 + f->fmt.pix.width = base_width; 350 + f->fmt.pix.height = base_height; 351 + row_mode = STK1160_DECIMATE_LESS_THAN_HALF; 352 + col_mode = STK1160_DECIMATE_LESS_THAN_HALF; 353 + 354 + if (width < base_width && width > base_width / 2) { 355 + /* 356 + * The device will send count units for each 357 + * unit skipped. This means count unit is: 358 + * 359 + * n = width / (frame width - width) 360 + * 361 + * And the width is: 362 + * 363 + * width = (n / n + 1) * frame width 364 + */ 365 + col_n = div_round_integer(width, base_width - width); 366 + if (col_n > 0 && col_n <= 255) { 367 + col_en = true; 368 + col_mode = STK1160_DECIMATE_LESS_THAN_HALF; 369 + f->fmt.pix.width = (base_width * col_n) / (col_n + 1); 370 + } 371 + 372 + } else if (width <= base_width / 2) { 373 + 374 + /* 375 + * The device will skip count units for each 376 + * unit sent. This means count is: 377 + * 378 + * n = (frame width / width) - 1 379 + * 380 + * And the width is: 381 + * 382 + * width = frame width / (n + 1) 383 + */ 384 + col_n = div_round_integer(base_width, width) - 1; 385 + if (col_n > 0 && col_n <= 255) { 386 + col_en = true; 387 + col_mode = STK1160_DECIMATE_MORE_THAN_HALF; 388 + f->fmt.pix.width = base_width / (col_n + 1); 389 + } 390 + } 391 + 392 + if (height < base_height && height > base_height / 2) { 393 + row_n = div_round_integer(height, base_height - height); 394 + if (row_n > 0 && row_n <= 255) { 395 + row_en = true; 396 + row_mode = STK1160_DECIMATE_LESS_THAN_HALF; 397 + f->fmt.pix.height = (base_height * row_n) / (row_n + 1); 398 + } 399 + 400 + } else if (height <= base_height / 2) { 401 + row_n = div_round_integer(base_height, height) - 1; 402 + if (row_n > 0 && row_n <= 255) { 403 + row_en = true; 404 + row_mode = STK1160_DECIMATE_MORE_THAN_HALF; 405 + f->fmt.pix.height = base_height / (row_n + 1); 406 + } 407 + } 408 + 409 + f->fmt.pix.pixelformat = dev->fmt->fourcc; 410 + f->fmt.pix.field = V4L2_FIELD_INTERLACED; 411 + f->fmt.pix.bytesperline = f->fmt.pix.width * 2; 412 + f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline; 413 + f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 414 + 415 + if (ctrl) { 416 + ctrl->col_en = col_en; 417 + ctrl->col_n = col_n; 418 + ctrl->col_mode = col_mode; 419 + ctrl->row_en = row_en; 420 + ctrl->row_n = row_n; 421 + ctrl->row_mode = row_mode; 422 + } 423 + 424 + stk1160_dbg("width %d, height %d\n", 425 + f->fmt.pix.width, f->fmt.pix.height); 426 + return 0; 427 + } 428 + 385 429 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, 386 - struct v4l2_format *f) 430 + struct v4l2_format *f) 387 431 { 388 432 struct stk1160 *dev = video_drvdata(file); 389 433 390 - /* 391 - * User can't choose size at his own will, 392 - * so we just return him the current size chosen 393 - * at standard selection. 394 - * TODO: Implement frame scaling? 395 - */ 396 - 397 - f->fmt.pix.pixelformat = dev->fmt->fourcc; 398 - f->fmt.pix.width = dev->width; 399 - f->fmt.pix.height = dev->height; 400 - f->fmt.pix.field = V4L2_FIELD_INTERLACED; 401 - f->fmt.pix.bytesperline = dev->width * 2; 402 - f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline; 403 - f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 404 - 405 - return 0; 434 + return stk1160_try_fmt(dev, f, NULL); 406 435 } 407 436 408 437 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, ··· 498 351 { 499 352 struct stk1160 *dev = video_drvdata(file); 500 353 struct vb2_queue *q = &dev->vb_vidq; 354 + struct stk1160_decimate_ctrl ctrl; 355 + int rc; 501 356 502 357 if (vb2_is_busy(q)) 503 358 return -EBUSY; 504 359 505 - vidioc_try_fmt_vid_cap(file, priv, f); 506 - 507 - /* We don't support any format changes */ 360 + rc = stk1160_try_fmt(dev, f, &ctrl); 361 + if (rc < 0) 362 + return rc; 363 + dev->width = f->fmt.pix.width; 364 + dev->height = f->fmt.pix.height; 365 + stk1160_set_fmt(dev, &ctrl); 508 366 509 367 return 0; 510 368 } ··· 545 393 return -ENODEV; 546 394 547 395 /* We need to set this now, before we call stk1160_set_std */ 396 + dev->width = 720; 397 + dev->height = (norm & V4L2_STD_525_60) ? 480 : 576; 548 398 dev->norm = norm; 549 399 550 - /* This is taken from saa7115 video decoder */ 551 - if (dev->norm & V4L2_STD_525_60) { 552 - dev->width = 720; 553 - dev->height = 480; 554 - } else if (dev->norm & V4L2_STD_625_50) { 555 - dev->width = 720; 556 - dev->height = 576; 557 - } else { 558 - stk1160_err("invalid standard\n"); 559 - return -EINVAL; 560 - } 561 - 562 400 stk1160_set_std(dev); 401 + 402 + /* Calling with NULL disables frame decimation */ 403 + stk1160_set_fmt(dev, NULL); 563 404 564 405 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, 565 406 dev->norm);