vimc-scaler.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * vimc-scaler.c Virtual Media Controller Driver
  4. *
  5. * Copyright (C) 2015-2017 Helen Koike <[email protected]>
  6. */
  7. #include <linux/moduleparam.h>
  8. #include <linux/string.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/v4l2-mediabus.h>
  11. #include <media/v4l2-rect.h>
  12. #include <media/v4l2-subdev.h>
  13. #include "vimc-common.h"
  14. /* Pad identifier */
  15. enum vic_sca_pad {
  16. VIMC_SCALER_SINK = 0,
  17. VIMC_SCALER_SRC = 1,
  18. };
  19. #define VIMC_SCALER_FMT_WIDTH_DEFAULT 640
  20. #define VIMC_SCALER_FMT_HEIGHT_DEFAULT 480
  21. struct vimc_scaler_device {
  22. struct vimc_ent_device ved;
  23. struct v4l2_subdev sd;
  24. struct v4l2_rect crop_rect;
  25. /* Frame format for both sink and src pad */
  26. struct v4l2_mbus_framefmt fmt[2];
  27. /* Values calculated when the stream starts */
  28. u8 *src_frame;
  29. unsigned int bpp;
  30. struct media_pad pads[2];
  31. };
  32. static const struct v4l2_mbus_framefmt fmt_default = {
  33. .width = VIMC_SCALER_FMT_WIDTH_DEFAULT,
  34. .height = VIMC_SCALER_FMT_HEIGHT_DEFAULT,
  35. .code = MEDIA_BUS_FMT_RGB888_1X24,
  36. .field = V4L2_FIELD_NONE,
  37. .colorspace = V4L2_COLORSPACE_SRGB,
  38. };
  39. static const struct v4l2_rect crop_rect_default = {
  40. .width = VIMC_SCALER_FMT_WIDTH_DEFAULT,
  41. .height = VIMC_SCALER_FMT_HEIGHT_DEFAULT,
  42. .top = 0,
  43. .left = 0,
  44. };
  45. static const struct v4l2_rect crop_rect_min = {
  46. .width = VIMC_FRAME_MIN_WIDTH,
  47. .height = VIMC_FRAME_MIN_HEIGHT,
  48. .top = 0,
  49. .left = 0,
  50. };
  51. static struct v4l2_rect
  52. vimc_scaler_get_crop_bound_sink(const struct v4l2_mbus_framefmt *sink_fmt)
  53. {
  54. /* Get the crop bounds to clamp the crop rectangle correctly */
  55. struct v4l2_rect r = {
  56. .left = 0,
  57. .top = 0,
  58. .width = sink_fmt->width,
  59. .height = sink_fmt->height,
  60. };
  61. return r;
  62. }
  63. static int vimc_scaler_init_cfg(struct v4l2_subdev *sd,
  64. struct v4l2_subdev_state *sd_state)
  65. {
  66. struct v4l2_mbus_framefmt *mf;
  67. struct v4l2_rect *r;
  68. unsigned int i;
  69. for (i = 0; i < sd->entity.num_pads; i++) {
  70. mf = v4l2_subdev_get_try_format(sd, sd_state, i);
  71. *mf = fmt_default;
  72. }
  73. r = v4l2_subdev_get_try_crop(sd, sd_state, VIMC_SCALER_SINK);
  74. *r = crop_rect_default;
  75. return 0;
  76. }
  77. static int vimc_scaler_enum_mbus_code(struct v4l2_subdev *sd,
  78. struct v4l2_subdev_state *sd_state,
  79. struct v4l2_subdev_mbus_code_enum *code)
  80. {
  81. u32 mbus_code = vimc_mbus_code_by_index(code->index);
  82. const struct vimc_pix_map *vpix;
  83. if (!mbus_code)
  84. return -EINVAL;
  85. vpix = vimc_pix_map_by_code(mbus_code);
  86. /* We don't support bayer format */
  87. if (!vpix || vpix->bayer)
  88. return -EINVAL;
  89. code->code = mbus_code;
  90. return 0;
  91. }
  92. static int vimc_scaler_enum_frame_size(struct v4l2_subdev *sd,
  93. struct v4l2_subdev_state *sd_state,
  94. struct v4l2_subdev_frame_size_enum *fse)
  95. {
  96. const struct vimc_pix_map *vpix;
  97. if (fse->index)
  98. return -EINVAL;
  99. /* Only accept code in the pix map table in non bayer format */
  100. vpix = vimc_pix_map_by_code(fse->code);
  101. if (!vpix || vpix->bayer)
  102. return -EINVAL;
  103. fse->min_width = VIMC_FRAME_MIN_WIDTH;
  104. fse->min_height = VIMC_FRAME_MIN_HEIGHT;
  105. fse->max_width = VIMC_FRAME_MAX_WIDTH;
  106. fse->max_height = VIMC_FRAME_MAX_HEIGHT;
  107. return 0;
  108. }
  109. static struct v4l2_mbus_framefmt *
  110. vimc_scaler_pad_format(struct vimc_scaler_device *vscaler,
  111. struct v4l2_subdev_state *sd_state, u32 pad,
  112. enum v4l2_subdev_format_whence which)
  113. {
  114. if (which == V4L2_SUBDEV_FORMAT_TRY)
  115. return v4l2_subdev_get_try_format(&vscaler->sd, sd_state, pad);
  116. else
  117. return &vscaler->fmt[pad];
  118. }
  119. static struct v4l2_rect *
  120. vimc_scaler_pad_crop(struct vimc_scaler_device *vscaler,
  121. struct v4l2_subdev_state *sd_state,
  122. enum v4l2_subdev_format_whence which)
  123. {
  124. if (which == V4L2_SUBDEV_FORMAT_TRY)
  125. return v4l2_subdev_get_try_crop(&vscaler->sd, sd_state,
  126. VIMC_SCALER_SINK);
  127. else
  128. return &vscaler->crop_rect;
  129. }
  130. static int vimc_scaler_get_fmt(struct v4l2_subdev *sd,
  131. struct v4l2_subdev_state *sd_state,
  132. struct v4l2_subdev_format *format)
  133. {
  134. struct vimc_scaler_device *vscaler = v4l2_get_subdevdata(sd);
  135. format->format = *vimc_scaler_pad_format(vscaler, sd_state, format->pad,
  136. format->which);
  137. return 0;
  138. }
  139. static int vimc_scaler_set_fmt(struct v4l2_subdev *sd,
  140. struct v4l2_subdev_state *sd_state,
  141. struct v4l2_subdev_format *format)
  142. {
  143. struct vimc_scaler_device *vscaler = v4l2_get_subdevdata(sd);
  144. struct v4l2_mbus_framefmt *fmt;
  145. /* Do not change the active format while stream is on */
  146. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE && vscaler->src_frame)
  147. return -EBUSY;
  148. fmt = vimc_scaler_pad_format(vscaler, sd_state, format->pad, format->which);
  149. /*
  150. * The media bus code and colorspace can only be changed on the sink
  151. * pad, the source pad only follows.
  152. */
  153. if (format->pad == VIMC_SCALER_SINK) {
  154. const struct vimc_pix_map *vpix;
  155. /* Only accept code in the pix map table in non bayer format. */
  156. vpix = vimc_pix_map_by_code(format->format.code);
  157. if (vpix && !vpix->bayer)
  158. fmt->code = format->format.code;
  159. else
  160. fmt->code = fmt_default.code;
  161. /* Clamp the colorspace to valid values. */
  162. fmt->colorspace = format->format.colorspace;
  163. fmt->ycbcr_enc = format->format.ycbcr_enc;
  164. fmt->quantization = format->format.quantization;
  165. fmt->xfer_func = format->format.xfer_func;
  166. vimc_colorimetry_clamp(fmt);
  167. }
  168. /* Clamp and align the width and height */
  169. fmt->width = clamp_t(u32, format->format.width, VIMC_FRAME_MIN_WIDTH,
  170. VIMC_FRAME_MAX_WIDTH) & ~1;
  171. fmt->height = clamp_t(u32, format->format.height, VIMC_FRAME_MIN_HEIGHT,
  172. VIMC_FRAME_MAX_HEIGHT) & ~1;
  173. /*
  174. * Propagate the sink pad format to the crop rectangle and the source
  175. * pad.
  176. */
  177. if (format->pad == VIMC_SCALER_SINK) {
  178. struct v4l2_mbus_framefmt *src_fmt;
  179. struct v4l2_rect *crop;
  180. crop = vimc_scaler_pad_crop(vscaler, sd_state, format->which);
  181. crop->width = fmt->width;
  182. crop->height = fmt->height;
  183. crop->top = 0;
  184. crop->left = 0;
  185. src_fmt = vimc_scaler_pad_format(vscaler, sd_state, VIMC_SCALER_SRC,
  186. format->which);
  187. *src_fmt = *fmt;
  188. }
  189. format->format = *fmt;
  190. return 0;
  191. }
  192. static int vimc_scaler_get_selection(struct v4l2_subdev *sd,
  193. struct v4l2_subdev_state *sd_state,
  194. struct v4l2_subdev_selection *sel)
  195. {
  196. struct vimc_scaler_device *vscaler = v4l2_get_subdevdata(sd);
  197. struct v4l2_mbus_framefmt *sink_fmt;
  198. if (VIMC_IS_SRC(sel->pad))
  199. return -EINVAL;
  200. switch (sel->target) {
  201. case V4L2_SEL_TGT_CROP:
  202. sel->r = *vimc_scaler_pad_crop(vscaler, sd_state, sel->which);
  203. break;
  204. case V4L2_SEL_TGT_CROP_BOUNDS:
  205. sink_fmt = vimc_scaler_pad_format(vscaler, sd_state, VIMC_SCALER_SINK,
  206. sel->which);
  207. sel->r = vimc_scaler_get_crop_bound_sink(sink_fmt);
  208. break;
  209. default:
  210. return -EINVAL;
  211. }
  212. return 0;
  213. }
  214. static void vimc_scaler_adjust_sink_crop(struct v4l2_rect *r,
  215. const struct v4l2_mbus_framefmt *sink_fmt)
  216. {
  217. const struct v4l2_rect sink_rect =
  218. vimc_scaler_get_crop_bound_sink(sink_fmt);
  219. /* Disallow rectangles smaller than the minimal one. */
  220. v4l2_rect_set_min_size(r, &crop_rect_min);
  221. v4l2_rect_map_inside(r, &sink_rect);
  222. }
  223. static int vimc_scaler_set_selection(struct v4l2_subdev *sd,
  224. struct v4l2_subdev_state *sd_state,
  225. struct v4l2_subdev_selection *sel)
  226. {
  227. struct vimc_scaler_device *vscaler = v4l2_get_subdevdata(sd);
  228. struct v4l2_mbus_framefmt *sink_fmt;
  229. struct v4l2_rect *crop_rect;
  230. /* Only support setting the crop of the sink pad */
  231. if (VIMC_IS_SRC(sel->pad) || sel->target != V4L2_SEL_TGT_CROP)
  232. return -EINVAL;
  233. if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE && vscaler->src_frame)
  234. return -EBUSY;
  235. crop_rect = vimc_scaler_pad_crop(vscaler, sd_state, sel->which);
  236. sink_fmt = vimc_scaler_pad_format(vscaler, sd_state, VIMC_SCALER_SINK,
  237. sel->which);
  238. vimc_scaler_adjust_sink_crop(&sel->r, sink_fmt);
  239. *crop_rect = sel->r;
  240. return 0;
  241. }
  242. static const struct v4l2_subdev_pad_ops vimc_scaler_pad_ops = {
  243. .init_cfg = vimc_scaler_init_cfg,
  244. .enum_mbus_code = vimc_scaler_enum_mbus_code,
  245. .enum_frame_size = vimc_scaler_enum_frame_size,
  246. .get_fmt = vimc_scaler_get_fmt,
  247. .set_fmt = vimc_scaler_set_fmt,
  248. .get_selection = vimc_scaler_get_selection,
  249. .set_selection = vimc_scaler_set_selection,
  250. };
  251. static int vimc_scaler_s_stream(struct v4l2_subdev *sd, int enable)
  252. {
  253. struct vimc_scaler_device *vscaler = v4l2_get_subdevdata(sd);
  254. if (enable) {
  255. const struct vimc_pix_map *vpix;
  256. unsigned int frame_size;
  257. if (vscaler->src_frame)
  258. return 0;
  259. /* Save the bytes per pixel of the sink */
  260. vpix = vimc_pix_map_by_code(vscaler->fmt[VIMC_SCALER_SINK].code);
  261. vscaler->bpp = vpix->bpp;
  262. /* Calculate the frame size of the source pad */
  263. frame_size = vscaler->fmt[VIMC_SCALER_SRC].width
  264. * vscaler->fmt[VIMC_SCALER_SRC].height * vscaler->bpp;
  265. /* Allocate the frame buffer. Use vmalloc to be able to
  266. * allocate a large amount of memory
  267. */
  268. vscaler->src_frame = vmalloc(frame_size);
  269. if (!vscaler->src_frame)
  270. return -ENOMEM;
  271. } else {
  272. if (!vscaler->src_frame)
  273. return 0;
  274. vfree(vscaler->src_frame);
  275. vscaler->src_frame = NULL;
  276. }
  277. return 0;
  278. }
  279. static const struct v4l2_subdev_video_ops vimc_scaler_video_ops = {
  280. .s_stream = vimc_scaler_s_stream,
  281. };
  282. static const struct v4l2_subdev_ops vimc_scaler_ops = {
  283. .pad = &vimc_scaler_pad_ops,
  284. .video = &vimc_scaler_video_ops,
  285. };
  286. static void vimc_scaler_fill_src_frame(const struct vimc_scaler_device *const vscaler,
  287. const u8 *const sink_frame)
  288. {
  289. const struct v4l2_mbus_framefmt *src_fmt = &vscaler->fmt[VIMC_SCALER_SRC];
  290. const struct v4l2_rect *r = &vscaler->crop_rect;
  291. unsigned int snk_width = vscaler->fmt[VIMC_SCALER_SINK].width;
  292. unsigned int src_x, src_y;
  293. u8 *walker = vscaler->src_frame;
  294. /* Set each pixel at the src_frame to its sink_frame equivalent */
  295. for (src_y = 0; src_y < src_fmt->height; src_y++) {
  296. unsigned int snk_y, y_offset;
  297. snk_y = (src_y * r->height) / src_fmt->height + r->top;
  298. y_offset = snk_y * snk_width * vscaler->bpp;
  299. for (src_x = 0; src_x < src_fmt->width; src_x++) {
  300. unsigned int snk_x, x_offset, index;
  301. snk_x = (src_x * r->width) / src_fmt->width + r->left;
  302. x_offset = snk_x * vscaler->bpp;
  303. index = y_offset + x_offset;
  304. memcpy(walker, &sink_frame[index], vscaler->bpp);
  305. walker += vscaler->bpp;
  306. }
  307. }
  308. }
  309. static void *vimc_scaler_process_frame(struct vimc_ent_device *ved,
  310. const void *sink_frame)
  311. {
  312. struct vimc_scaler_device *vscaler = container_of(ved, struct vimc_scaler_device,
  313. ved);
  314. /* If the stream in this node is not active, just return */
  315. if (!vscaler->src_frame)
  316. return ERR_PTR(-EINVAL);
  317. vimc_scaler_fill_src_frame(vscaler, sink_frame);
  318. return vscaler->src_frame;
  319. };
  320. static void vimc_scaler_release(struct vimc_ent_device *ved)
  321. {
  322. struct vimc_scaler_device *vscaler =
  323. container_of(ved, struct vimc_scaler_device, ved);
  324. media_entity_cleanup(vscaler->ved.ent);
  325. kfree(vscaler);
  326. }
  327. static struct vimc_ent_device *vimc_scaler_add(struct vimc_device *vimc,
  328. const char *vcfg_name)
  329. {
  330. struct v4l2_device *v4l2_dev = &vimc->v4l2_dev;
  331. struct vimc_scaler_device *vscaler;
  332. int ret;
  333. /* Allocate the vscaler struct */
  334. vscaler = kzalloc(sizeof(*vscaler), GFP_KERNEL);
  335. if (!vscaler)
  336. return ERR_PTR(-ENOMEM);
  337. /* Initialize ved and sd */
  338. vscaler->pads[VIMC_SCALER_SINK].flags = MEDIA_PAD_FL_SINK;
  339. vscaler->pads[VIMC_SCALER_SRC].flags = MEDIA_PAD_FL_SOURCE;
  340. ret = vimc_ent_sd_register(&vscaler->ved, &vscaler->sd, v4l2_dev,
  341. vcfg_name,
  342. MEDIA_ENT_F_PROC_VIDEO_SCALER, 2,
  343. vscaler->pads, &vimc_scaler_ops);
  344. if (ret) {
  345. kfree(vscaler);
  346. return ERR_PTR(ret);
  347. }
  348. vscaler->ved.process_frame = vimc_scaler_process_frame;
  349. vscaler->ved.dev = vimc->mdev.dev;
  350. /* Initialize the frame format */
  351. vscaler->fmt[VIMC_SCALER_SINK] = fmt_default;
  352. vscaler->fmt[VIMC_SCALER_SRC] = fmt_default;
  353. /* Initialize the crop selection */
  354. vscaler->crop_rect = crop_rect_default;
  355. return &vscaler->ved;
  356. }
  357. struct vimc_ent_type vimc_scaler_type = {
  358. .add = vimc_scaler_add,
  359. .release = vimc_scaler_release
  360. };