1 .. SPDX-License-Identifier: GPL-2.0
3 .. include:: <isonum.txt>
5 ===============================================================
6 Intel Image Processing Unit 3 (IPU3) Imaging Unit (ImgU) driver
7 ===============================================================
9 Copyright |copy| 2018 Intel Corporation
14 This file documents the Intel IPU3 (3rd generation Image Processing Unit)
15 Imaging Unit drivers located under drivers/media/pci/intel/ipu3 (CIO2) as well
16 as under drivers/staging/media/ipu3 (ImgU).
18 The Intel IPU3 found in certain Kaby Lake (as well as certain Sky Lake)
19 platforms (U/Y processor lines) is made up of two parts namely the Imaging Unit
20 (ImgU) and the CIO2 device (MIPI CSI2 receiver).
22 The CIO2 device receives the raw Bayer data from the sensors and outputs the
23 frames in a format that is specific to the IPU3 (for consumption by the IPU3
24 ImgU). The CIO2 driver is available as drivers/media/pci/intel/ipu3/ipu3-cio2*
25 and is enabled through the CONFIG_VIDEO_IPU3_CIO2 config option.
27 The Imaging Unit (ImgU) is responsible for processing images captured
28 by the IPU3 CIO2 device. The ImgU driver sources can be found under
29 drivers/staging/media/ipu3 directory. The driver is enabled through the
30 CONFIG_VIDEO_IPU3_IMGU config option.
32 The two driver modules are named ipu3_csi2 and ipu3_imgu, respectively.
34 The drivers has been tested on Kaby Lake platforms (U/Y processor lines).
36 Both of the drivers implement V4L2, Media Controller and V4L2 sub-device
37 interfaces. The IPU3 CIO2 driver supports camera sensors connected to the CIO2
38 MIPI CSI-2 interfaces through V4L2 sub-device sensor drivers.
43 The CIO2 is represented as a single V4L2 subdev, which provides a V4L2 subdev
44 interface to the user space. There is a video node for each CSI-2 receiver,
45 with a single media controller interface for the entire device.
47 The CIO2 contains four independent capture channel, each with its own MIPI CSI-2
48 receiver and DMA engine. Each channel is modelled as a V4L2 sub-device exposed
49 to userspace as a V4L2 sub-device node and has two pads:
51 .. tabularcolumns:: |p{0.8cm}|p{4.0cm}|p{4.0cm}|
61 - MIPI CSI-2 input, connected to the sensor subdev
65 - Raw video capture, connected to the V4L2 video interface
67 The V4L2 video interfaces model the DMA engines. They are exposed to userspace
68 as V4L2 video device nodes.
70 Capturing frames in raw Bayer format
71 ------------------------------------
73 CIO2 MIPI CSI2 receiver is used to capture frames (in packed raw Bayer format)
74 from the raw sensors connected to the CSI2 ports. The captured frames are used
75 as input to the ImgU driver.
77 Image processing using IPU3 ImgU requires tools such as raw2pnm [#f1]_, and
78 yavta [#f2]_ due to the following unique requirements and / or features specific
81 -- The IPU3 CSI2 receiver outputs the captured frames from the sensor in packed
82 raw Bayer format that is specific to IPU3.
84 -- Multiple video nodes have to be operated simultaneously.
86 Let us take the example of ov5670 sensor connected to CSI2 port 0, for a
87 2592x1944 image capture.
89 Using the media contorller APIs, the ov5670 sensor is configured to send
90 frames in packed raw Bayer format to IPU3 CSI2 receiver.
92 # This example assumes /dev/media0 as the CIO2 media device
94 export MDEV=/dev/media0
96 # and that ov5670 sensor is connected to i2c bus 10 with address 0x36
98 export SDEV=$(media-ctl -d $MDEV -e "ov5670 10-0036")
100 # Establish the link for the media devices using media-ctl [#f3]_
101 media-ctl -d $MDEV -l "ov5670:0 -> ipu3-csi2 0:0[1]"
103 # Set the format for the media devices
104 media-ctl -d $MDEV -V "ov5670:0 [fmt:SGRBG10/2592x1944]"
106 media-ctl -d $MDEV -V "ipu3-csi2 0:0 [fmt:SGRBG10/2592x1944]"
108 media-ctl -d $MDEV -V "ipu3-csi2 0:1 [fmt:SGRBG10/2592x1944]"
110 Once the media pipeline is configured, desired sensor specific settings
111 (such as exposure and gain settings) can be set, using the yavta tool.
115 yavta -w 0x009e0903 444 $SDEV
117 yavta -w 0x009e0913 1024 $SDEV
119 yavta -w 0x009e0911 2046 $SDEV
121 Once the desired sensor settings are set, frame captures can be done as below.
125 yavta --data-prefix -u -c10 -n5 -I -s2592x1944 --file=/tmp/frame-#.bin \
126 -f IPU3_SGRBG10 $(media-ctl -d $MDEV -e "ipu3-cio2 0")
128 With the above command, 10 frames are captured at 2592x1944 resolution, with
129 sGRBG10 format and output as IPU3_SGRBG10 format.
131 The captured frames are available as /tmp/frame-#.bin files.
136 The ImgU is represented as two V4L2 subdevs, each of which provides a V4L2
137 subdev interface to the user space.
139 Each V4L2 subdev represents a pipe, which can support a maximum of 2 streams.
140 This helps to support advanced camera features like Continuous View Finder (CVF)
141 and Snapshot During Video(SDV).
143 The ImgU contains two independent pipes, each modelled as a V4L2 sub-device
144 exposed to userspace as a V4L2 sub-device node.
146 Each pipe has two sink pads and three source pads for the following purpose:
148 .. tabularcolumns:: |p{0.8cm}|p{4.0cm}|p{4.0cm}|
158 - Input raw video stream
162 - Processing parameters
166 - Output processed video stream
170 - Output viewfinder video stream
176 Each pad is connected to a corresponding V4L2 video interface, exposed to
177 userspace as a V4L2 video device node.
182 With ImgU, once the input video node ("ipu3-imgu 0/1":0, in
183 <entity>:<pad-number> format) is queued with buffer (in packed raw Bayer
184 format), ImgU starts processing the buffer and produces the video output in YUV
185 format and statistics output on respective output nodes. The driver is expected
186 to have buffers ready for all of parameter, output and statistics nodes, when
187 input video node is queued with buffer.
189 At a minimum, all of input, main output, 3A statistics and viewfinder
190 video nodes should be enabled for IPU3 to start image processing.
192 Each ImgU V4L2 subdev has the following set of video nodes.
194 input, output and viewfinder video nodes
195 ----------------------------------------
197 The frames (in packed raw Bayer format specific to the IPU3) received by the
198 input video node is processed by the IPU3 Imaging Unit and are output to 2 video
199 nodes, with each targeting a different purpose (main output and viewfinder
202 Details onand the Bayer format specific to the IPU3 can be found in
203 :ref:`v4l2-pix-fmt-ipu3-sbggr10`.
205 The driver supports V4L2 Video Capture Interface as defined at :ref:`devices`.
207 Only the multi-planar API is supported. More details can be found at
210 Parameters video node
211 ---------------------
213 The parameters video node receives the ImgU algorithm parameters that are used
214 to configure how the ImgU algorithms process the image.
216 Details on processing parameters specific to the IPU3 can be found in
217 :ref:`v4l2-meta-fmt-params`.
219 3A statistics video node
220 ------------------------
222 3A statistics video node is used by the ImgU driver to output the 3A (auto
223 focus, auto exposure and auto white balance) statistics for the frames that are
224 being processed by the ImgU to user space applications. User space applications
225 can use this statistics data to compute the desired algorithm parameters for
228 Configuring the Intel IPU3
229 ==========================
231 The IPU3 ImgU pipelines can be configured using the Media Controller, defined at
232 :ref:`media_controller`.
234 Firmware binary selection
235 -------------------------
237 The firmware binary is selected using the V4L2_CID_INTEL_IPU3_MODE, currently
238 defined in drivers/staging/media/ipu3/include/intel-ipu3.h . "VIDEO" and "STILL"
241 Processing the image in raw Bayer format
242 ----------------------------------------
244 Configuring ImgU V4L2 subdev for image processing
245 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
247 The ImgU V4L2 subdevs have to be configured with media controller APIs to have
248 all the video nodes setup correctly.
250 Let us take "ipu3-imgu 0" subdev as an example.
252 media-ctl -d $MDEV -r
254 media-ctl -d $MDEV -l "ipu3-imgu 0 input":0 -> "ipu3-imgu 0":0[1]
256 media-ctl -d $MDEV -l "ipu3-imgu 0":2 -> "ipu3-imgu 0 output":0[1]
258 media-ctl -d $MDEV -l "ipu3-imgu 0":3 -> "ipu3-imgu 0 viewfinder":0[1]
260 media-ctl -d $MDEV -l "ipu3-imgu 0":4 -> "ipu3-imgu 0 3a stat":0[1]
262 Also the pipe mode of the corresponding V4L2 subdev should be set as desired
263 (e.g 0 for video mode or 1 for still mode) through the control id 0x009819a1 as
266 yavta -w "0x009819A1 1" /dev/v4l-subdev7
268 Certain hardware blocks in ImgU pipeline can change the frame resolution by
269 cropping or scaling, these hardware blocks include Input Feeder(IF), Bayer Down
270 Scaler (BDS) and Geometric Distortion Correction (GDC).
271 There is also a block which can change the frame resolution - YUV Scaler, it is
272 only applicable to the secondary output.
274 RAW Bayer frames go through these ImgU pipeline hardware blocks and the final
275 processed image output to the DDR memory.
277 .. kernel-figure:: ipu3_rcb.svg
278 :alt: ipu3 resolution blocks image
280 IPU3 resolution change hardware blocks
284 Input Feeder gets the Bayer frame data from the sensor, it can enable cropping
285 of lines and columns from the frame and then store pixels into device's internal
286 pixel buffer which are ready to readout by following blocks.
288 **Bayer Down Scaler**
290 Bayer Down Scaler is capable of performing image scaling in Bayer domain, the
291 downscale factor can be configured from 1X to 1/4X in each axis with
292 configuration steps of 0.03125 (1/32).
294 **Geometric Distortion Correction**
296 Geometric Distortion Correction is used to performe correction of distortions
297 and image filtering. It needs some extra filter and envelop padding pixels to
298 work, so the input resolution of GDC should be larger than the output
303 YUV Scaler which similar with BDS, but it is mainly do image down scaling in
304 YUV domain, it can support up to 1/12X down scaling, but it can not be applied
307 The ImgU V4L2 subdev has to be configured with the supported resolutions in all
308 the above hardware blocks, for a given input resolution.
309 For a given supported resolution for an input frame, the Input Feeder, Bayer
310 Down Scaler and GDC blocks should be configured with the supported resolutions
311 as each hardware block has its own alignment requirement.
313 You must configure the output resolution of the hardware blocks smartly to meet
314 the hardware requirement along with keeping the maximum field of view.
315 The intermediate resolutions can be generated by specific tool and this
316 information can be obtained by looking at the following IPU3 ImgU configuration
319 https://chromium.googlesource.com/chromiumos/overlays/board-overlays/+/master
321 Under baseboard-poppy/media-libs/cros-camera-hal-configs-poppy/files/gcss
322 directory, graph_settings_ov5670.xml can be used as an example.
324 The following steps prepare the ImgU pipeline for the image processing.
326 1. The ImgU V4L2 subdev data format should be set by using the
327 VIDIOC_SUBDEV_S_FMT on pad 0, using the GDC width and height obtained above.
329 2. The ImgU V4L2 subdev cropping should be set by using the
330 VIDIOC_SUBDEV_S_SELECTION on pad 0, with V4L2_SEL_TGT_CROP as the target,
331 using the input feeder height and width.
333 3. The ImgU V4L2 subdev composing should be set by using the
334 VIDIOC_SUBDEV_S_SELECTION on pad 0, with V4L2_SEL_TGT_COMPOSE as the target,
335 using the BDS height and width.
337 For the ov5670 example, for an input frame with a resolution of 2592x1944
338 (which is input to the ImgU subdev pad 0), the corresponding resolutions
339 for input feeder, BDS and GDC are 2592x1944, 2592x1944 and 2560x1920
342 Once this is done, the received raw Bayer frames can be input to the ImgU
343 V4L2 subdev as below, using the open source application v4l2n [#f1]_.
345 For an image captured with 2592x1944 [#f4]_ resolution, with desired output
346 resolution as 2560x1920 and viewfinder resolution as 2560x1920, the following
347 v4l2n command can be used. This helps process the raw Bayer frames and produces
348 the desired results for the main output image and the viewfinder output, in NV12
351 v4l2n --pipe=4 --load=/tmp/frame-#.bin --open=/dev/video4
352 --fmt=type:VIDEO_OUTPUT_MPLANE,width=2592,height=1944,pixelformat=0X47337069
353 --reqbufs=type:VIDEO_OUTPUT_MPLANE,count:1 --pipe=1 --output=/tmp/frames.out
355 --fmt=type:VIDEO_CAPTURE_MPLANE,width=2560,height=1920,pixelformat=NV12
356 --reqbufs=type:VIDEO_CAPTURE_MPLANE,count:1 --pipe=2 --output=/tmp/frames.vf
358 --fmt=type:VIDEO_CAPTURE_MPLANE,width=2560,height=1920,pixelformat=NV12
359 --reqbufs=type:VIDEO_CAPTURE_MPLANE,count:1 --pipe=3 --open=/dev/video7
360 --output=/tmp/frames.3A --fmt=type:META_CAPTURE,?
361 --reqbufs=count:1,type:META_CAPTURE --pipe=1,2,3,4 --stream=5
363 where /dev/video4, /dev/video5, /dev/video6 and /dev/video7 devices point to
364 input, output, viewfinder and 3A statistics video nodes respectively.
366 Converting the raw Bayer image into YUV domain
367 ----------------------------------------------
369 The processed images after the above step, can be converted to YUV domain
375 raw2pnm -x2560 -y1920 -fNV12 /tmp/frames.out /tmp/frames.out.ppm
377 where 2560x1920 is output resolution, NV12 is the video format, followed
378 by input frame and output PNM file.
380 Viewfinder output frames
381 ~~~~~~~~~~~~~~~~~~~~~~~~
383 raw2pnm -x2560 -y1920 -fNV12 /tmp/frames.vf /tmp/frames.vf.ppm
385 where 2560x1920 is output resolution, NV12 is the video format, followed
386 by input frame and output PNM file.
388 Example user space code for IPU3
389 ================================
391 User space code that configures and uses IPU3 is available here.
393 https://chromium.googlesource.com/chromiumos/platform/arc-camera/+/master/
395 The source can be located under hal/intel directory.
397 Overview of IPU3 pipeline
398 =========================
400 IPU3 pipeline has a number of image processing stages, each of which takes a
401 set of parameters as input. The major stages of pipelines are shown here:
403 .. kernel-render:: DOT
404 :alt: IPU3 ImgU Pipeline
405 :caption: IPU3 ImgU Pipeline Diagram
407 digraph "IPU3 ImgU" {
412 a [label="Raw pixels"]
413 b [label="Bayer Downscaling"]
414 c [label="Optical Black Correction"]
415 d [label="Linearization"]
416 e [label="Lens Shading Correction"]
417 f [label="White Balance / Exposure / Focus Apply"]
418 g [label="Bayer Noise Reduction"]
420 i [label="Demosaicing"]
421 j [label="Color Correction Matrix"]
422 k [label="Gamma correction"]
423 l [label="Color Space Conversion"]
424 m [label="Chroma Down Scaling"]
425 n [label="Chromatic Noise Reduction"]
426 o [label="Total Color Correction"]
431 { rank=same; a -> b -> c -> d -> e -> f }
432 { rank=same; g -> h -> i -> j -> k -> l }
433 { rank=same; m -> n -> o -> p -> q -> r }
435 a -> g -> m [style=invis, weight=10]
441 The table below presents a description of the above algorithms.
443 ======================== =======================================================
445 ======================== =======================================================
446 Optical Black Correction Optical Black Correction block subtracts a pre-defined
447 value from the respective pixel values to obtain better
449 Defined in :c:type:`ipu3_uapi_obgrid_param`.
450 Linearization This algo block uses linearization parameters to
451 address non-linearity sensor effects. The Lookup table
453 :c:type:`ipu3_uapi_isp_lin_vmem_params`.
454 SHD Lens shading correction is used to correct spatial
455 non-uniformity of the pixel response due to optical
456 lens shading. This is done by applying a different gain
457 for each pixel. The gain, black level etc are
458 configured in :c:type:`ipu3_uapi_shd_config_static`.
459 BNR Bayer noise reduction block removes image noise by
460 applying a bilateral filter.
461 See :c:type:`ipu3_uapi_bnr_static_config` for details.
462 ANR Advanced Noise Reduction is a block based algorithm
463 that performs noise reduction in the Bayer domain. The
464 convolution matrix etc can be found in
465 :c:type:`ipu3_uapi_anr_config`.
466 DM Demosaicing converts raw sensor data in Bayer format
467 into RGB (Red, Green, Blue) presentation. Then add
468 outputs of estimation of Y channel for following stream
469 processing by Firmware. The struct is defined as
470 :c:type:`ipu3_uapi_dm_config`.
471 Color Correction Color Correction algo transforms sensor specific color
472 space to the standard "sRGB" color space. This is done
473 by applying 3x3 matrix defined in
474 :c:type:`ipu3_uapi_ccm_mat_config`.
475 Gamma correction Gamma correction :c:type:`ipu3_uapi_gamma_config` is a
476 basic non-linear tone mapping correction that is
477 applied per pixel for each pixel component.
478 CSC Color space conversion transforms each pixel from the
479 RGB primary presentation to YUV (Y: brightness,
480 UV: Luminance) presentation. This is done by applying
481 a 3x3 matrix defined in
482 :c:type:`ipu3_uapi_csc_mat_config`
483 CDS Chroma down sampling
484 After the CSC is performed, the Chroma Down Sampling
485 is applied for a UV plane down sampling by a factor
486 of 2 in each direction for YUV 4:2:0 using a 4x2
487 configurable filter :c:type:`ipu3_uapi_cds_params`.
488 CHNR Chroma noise reduction
489 This block processes only the chrominance pixels and
490 performs noise reduction by cleaning the high
492 See struct :c:type:`ipu3_uapi_yuvp1_chnr_config`.
493 TCC Total color correction as defined in struct
494 :c:type:`ipu3_uapi_yuvp2_tcc_static_config`.
495 XNR3 eXtreme Noise Reduction V3 is the third revision of
496 noise reduction algorithm used to improve image
497 quality. This removes the low frequency noise in the
498 captured image. Two related structs are being defined,
499 :c:type:`ipu3_uapi_isp_xnr3_params` for ISP data memory
500 and :c:type:`ipu3_uapi_isp_xnr3_vmem_params` for vector
502 TNR Temporal Noise Reduction block compares successive
503 frames in time to remove anomalies / noise in pixel
504 values. :c:type:`ipu3_uapi_isp_tnr3_vmem_params` and
505 :c:type:`ipu3_uapi_isp_tnr3_params` are defined for ISP
506 vector and data memory respectively.
507 ======================== =======================================================
509 Other often encountered acronyms not listed in above table:
514 Auto white balance filter response statistics
516 Bayer downscaler parameters
518 Color correction matrix coefficients
520 Image enhancement filter directed
522 Optical black level compensation
524 Output system configuration
532 A few stages of the pipeline will be executed by firmware running on the ISP
533 processor, while many others will use a set of fixed hardware blocks also
534 called accelerator cluster (ACC) to crunch pixel data and produce statistics.
536 ACC parameters of individual algorithms, as defined by
537 :c:type:`ipu3_uapi_acc_param`, can be chosen to be applied by the user
538 space through struct :c:type:`ipu3_uapi_flags` embedded in
539 :c:type:`ipu3_uapi_params` structure. For parameters that are configured as
540 not enabled by the user space, the corresponding structs are ignored by the
541 driver, in which case the existing configuration of the algorithm will be
547 .. [#f5] drivers/staging/media/ipu3/include/intel-ipu3.h
549 .. [#f1] https://github.com/intel/nvt
551 .. [#f2] http://git.ideasonboard.org/yavta.git
553 .. [#f3] http://git.ideasonboard.org/?p=media-ctl.git;a=summary
555 .. [#f4] ImgU limitation requires an additional 16x16 for all input resolutions