vimc-scaler.c 11 KB

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