2020-12-15 11:25:23

by Mirela Rabulea OSS

[permalink] [raw]
Subject: [PATCH v6 0/9] Add V4L2 driver for i.MX8 JPEG Encoder/Decoder

From: Mirela Rabulea <[email protected]>

This patch set adds the V4L2 driver for i.MX8QXP/QM JPEG encoder/decoder
and it's dependencies.
The driver was tested on i.MX8QXP, using a unit test application and
the v4l2-compliance tool, including the streaming tests for decoder & encoder.

The output of latest v4l2-compliance on i.MX8QXP, decoder & encoder:

root@imx8qxpmek:/unit_tests/JPEG# ./v4l2-compliance-master -d /dev/video0 -s
v4l2-compliance 1.21.0-4686, 64 bits, 64-bit time_t
v4l2-compliance SHA: e0e4114f9714 2020-12-10 13:23:07

Compliance test for mxc-jpeg decode device /dev/video0:

Driver Info:
Driver name : mxc-jpeg decode
Card type : mxc-jpeg decoder
Bus info : platform:58400000.jpegdec
Driver version : 5.10.0
Capabilities : 0x84204000
Video Memory-to-Memory Multiplanar
Streaming
Extended Pix Format
Device Capabilities
Device Caps : 0x04204000
Video Memory-to-Memory Multiplanar
Streaming
Extended Pix Format
Detected JPEG Decoder

Required ioctls:
test VIDIOC_QUERYCAP: OK

Allow for multiple opens:
test second /dev/video0 open: OK
test VIDIOC_QUERYCAP: OK
test VIDIOC_G/S_PRIORITY: OK
test for unlimited opens: OK

test invalid ioctls: OK
Debug ioctls:
test VIDIOC_DBG_G/S_REGISTER: OK (Not Supported)
test VIDIOC_LOG_STATUS: OK (Not Supported)

Input ioctls:
test VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: OK (Not Supported)
test VIDIOC_G/S_FREQUENCY: OK (Not Supported)
test VIDIOC_S_HW_FREQ_SEEK: OK (Not Supported)
test VIDIOC_ENUMAUDIO: OK (Not Supported)
test VIDIOC_G/S/ENUMINPUT: OK (Not Supported)
test VIDIOC_G/S_AUDIO: OK (Not Supported)
Inputs: 0 Audio Inputs: 0 Tuners: 0

Output ioctls:
test VIDIOC_G/S_MODULATOR: OK (Not Supported)
test VIDIOC_G/S_FREQUENCY: OK (Not Supported)
test VIDIOC_ENUMAUDOUT: OK (Not Supported)
test VIDIOC_G/S/ENUMOUTPUT: OK (Not Supported)
test VIDIOC_G/S_AUDOUT: OK (Not Supported)
Outputs: 0 Audio Outputs: 0 Modulators: 0

Input/Output configuration ioctls:
test VIDIOC_ENUM/G/S/QUERY_STD: OK (Not Supported)
test VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: OK (Not Supported)
test VIDIOC_DV_TIMINGS_CAP: OK (Not Supported)
test VIDIOC_G/S_EDID: OK (Not Supported)

Control ioctls:
test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: OK (Not Supported)
test VIDIOC_QUERYCTRL: OK (Not Supported)
test VIDIOC_G/S_CTRL: OK (Not Supported)
test VIDIOC_G/S/TRY_EXT_CTRLS: OK (Not Supported)
test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: OK (Not Supported)
test VIDIOC_G/S_JPEGCOMP: OK (Not Supported)
Standard Controls: 0 Private Controls: 0

Format ioctls:
test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK
test VIDIOC_G/S_PARM: OK (Not Supported)
test VIDIOC_G_FBUF: OK (Not Supported)
test VIDIOC_G_FMT: OK
test VIDIOC_TRY_FMT: OK
test VIDIOC_S_FMT: OK
test VIDIOC_G_SLICED_VBI_CAP: OK (Not Supported)
test Cropping: OK (Not Supported)
test Composing: OK (Not Supported)
test Scaling: OK

Codec ioctls:
test VIDIOC_(TRY_)ENCODER_CMD: OK
test VIDIOC_G_ENC_INDEX: OK (Not Supported)
test VIDIOC_(TRY_)DECODER_CMD: OK

Buffer ioctls:
test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: OK
test VIDIOC_EXPBUF: OK
test Requests: OK (Not Supported)

Test input 0:

Streaming ioctls:
test read/write: OK (Not Supported)
test blocking wait: OK
Video Capture Multiplanar: Captured 58 buffers
test MMAP (no poll): OK
Video Capture Multiplanar: Captured 58 buffers
test MMAP (select): OK
Video Capture Multiplanar: Captured 58 buffers
test MMAP (epoll): OK
test USERPTR (no poll): OK (Not Supported)
test USERPTR (select): OK (Not Supported)
test DMABUF: Cannot test, specify --expbuf-device

Total for mxc-jpeg decode device /dev/video0: 52, Succeeded: 52, Failed: 0, Warnings: 0

root@imx8qxpmek:/unit_tests/JPEG# ./v4l2-compliance-master -d /dev/video1 -s
v4l2-compliance 1.21.0-4686, 64 bits, 64-bit time_t
v4l2-compliance SHA: e0e4114f9714 2020-12-10 13:23:07

Compliance test for mxc-jpeg decode device /dev/video1:

Driver Info:
Driver name : mxc-jpeg decode
Card type : mxc-jpeg decoder
Bus info : platform:58450000.jpegenc
Driver version : 5.10.0
Capabilities : 0x84204000
Video Memory-to-Memory Multiplanar
Streaming
Extended Pix Format
Device Capabilities
Device Caps : 0x04204000
Video Memory-to-Memory Multiplanar
Streaming
Extended Pix Format
Detected JPEG Encoder

Required ioctls:
test VIDIOC_QUERYCAP: OK

Allow for multiple opens:
test second /dev/video1 open: OK
test VIDIOC_QUERYCAP: OK
test VIDIOC_G/S_PRIORITY: OK
test for unlimited opens: OK

test invalid ioctls: OK
Debug ioctls:
test VIDIOC_DBG_G/S_REGISTER: OK (Not Supported)
test VIDIOC_LOG_STATUS: OK (Not Supported)

Input ioctls:
test VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: OK (Not Supported)
test VIDIOC_G/S_FREQUENCY: OK (Not Supported)
test VIDIOC_S_HW_FREQ_SEEK: OK (Not Supported)
test VIDIOC_ENUMAUDIO: OK (Not Supported)
test VIDIOC_G/S/ENUMINPUT: OK (Not Supported)
test VIDIOC_G/S_AUDIO: OK (Not Supported)
Inputs: 0 Audio Inputs: 0 Tuners: 0

Output ioctls:
test VIDIOC_G/S_MODULATOR: OK (Not Supported)
test VIDIOC_G/S_FREQUENCY: OK (Not Supported)
test VIDIOC_ENUMAUDOUT: OK (Not Supported)
test VIDIOC_G/S/ENUMOUTPUT: OK (Not Supported)
test VIDIOC_G/S_AUDOUT: OK (Not Supported)
Outputs: 0 Audio Outputs: 0 Modulators: 0

Input/Output configuration ioctls:
test VIDIOC_ENUM/G/S/QUERY_STD: OK (Not Supported)
test VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: OK (Not Supported)
test VIDIOC_DV_TIMINGS_CAP: OK (Not Supported)
test VIDIOC_G/S_EDID: OK (Not Supported)

Control ioctls:
test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: OK (Not Supported)
test VIDIOC_QUERYCTRL: OK (Not Supported)
test VIDIOC_G/S_CTRL: OK (Not Supported)
test VIDIOC_G/S/TRY_EXT_CTRLS: OK (Not Supported)
test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: OK (Not Supported)
test VIDIOC_G/S_JPEGCOMP: OK (Not Supported)
Standard Controls: 0 Private Controls: 0

Format ioctls:
test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK
test VIDIOC_G/S_PARM: OK (Not Supported)
test VIDIOC_G_FBUF: OK (Not Supported)
test VIDIOC_G_FMT: OK
test VIDIOC_TRY_FMT: OK
test VIDIOC_S_FMT: OK
test VIDIOC_G_SLICED_VBI_CAP: OK (Not Supported)
test Cropping: OK (Not Supported)
test Composing: OK (Not Supported)
test Scaling: OK (Not Supported)

Codec ioctls:
test VIDIOC_(TRY_)ENCODER_CMD: OK
test VIDIOC_G_ENC_INDEX: OK (Not Supported)
test VIDIOC_(TRY_)DECODER_CMD: OK

Buffer ioctls:
test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: OK
test VIDIOC_EXPBUF: OK
test Requests: OK (Not Supported)

Test input 0:

Streaming ioctls:
test read/write: OK (Not Supported)
test blocking wait: OK
Video Capture Multiplanar: Captured 58 buffers
test MMAP (no poll): OK
Video Capture Multiplanar: Captured 58 buffers
test MMAP (select): OK
Video Capture Multiplanar: Captured 58 buffers
test MMAP (epoll): OK
test USERPTR (no poll): OK (Not Supported)
test USERPTR (select): OK (Not Supported)
test DMABUF: Cannot test, specify --expbuf-device

Total for mxc-jpeg decode device /dev/video1: 52, Succeeded: 52, Failed: 0, Warnings: 0

Mirela Rabulea (9):
media: v4l: Add packed YUV444 24bpp pixel format
media: dt-bindings: Add bindings for i.MX8QXP/QM JPEG driver
media: imx-jpeg: Add V4L2 driver for i.MX8 JPEG Encoder/Decoder
arm64: dts: imx8qxp: Add jpeg encoder/decoder nodes
Add maintainer for IMX jpeg v4l2 driver
media: Add parsing for APP14 data segment in jpeg helpers
media: Quit parsing stream if doesn't start with SOI
media: Avoid parsing quantization and huffman tables
media: imx-jpeg: Use v4l2 jpeg helpers in mxc-jpeg

.../bindings/media/nxp,imx8-jpeg.yaml | 84 +
.../media/v4l/pixfmt-packed-yuv.rst | 10 +
MAINTAINERS | 8 +
arch/arm64/boot/dts/freescale/imx8qxp.dtsi | 35 +
drivers/media/platform/Kconfig | 2 +
drivers/media/platform/Makefile | 1 +
drivers/media/platform/imx-jpeg/Kconfig | 11 +
drivers/media/platform/imx-jpeg/Makefile | 3 +
drivers/media/platform/imx-jpeg/mxc-jpeg-hw.c | 168 ++
drivers/media/platform/imx-jpeg/mxc-jpeg-hw.h | 140 ++
drivers/media/platform/imx-jpeg/mxc-jpeg.c | 2193 +++++++++++++++++
drivers/media/platform/imx-jpeg/mxc-jpeg.h | 180 ++
drivers/media/v4l2-core/v4l2-ioctl.c | 1 +
drivers/media/v4l2-core/v4l2-jpeg.c | 58 +-
include/media/v4l2-jpeg.h | 18 +
include/uapi/linux/videodev2.h | 1 +
16 files changed, 2906 insertions(+), 7 deletions(-)
create mode 100644 Documentation/devicetree/bindings/media/nxp,imx8-jpeg.yaml
create mode 100644 drivers/media/platform/imx-jpeg/Kconfig
create mode 100644 drivers/media/platform/imx-jpeg/Makefile
create mode 100644 drivers/media/platform/imx-jpeg/mxc-jpeg-hw.c
create mode 100644 drivers/media/platform/imx-jpeg/mxc-jpeg-hw.h
create mode 100644 drivers/media/platform/imx-jpeg/mxc-jpeg.c
create mode 100644 drivers/media/platform/imx-jpeg/mxc-jpeg.h

--
2.17.1


2020-12-15 11:25:31

by Mirela Rabulea OSS

[permalink] [raw]
Subject: [PATCH v6 2/9] media: dt-bindings: Add bindings for i.MX8QXP/QM JPEG driver

From: Mirela Rabulea <[email protected]>

Add bindings documentation for i.MX8QXP/QM JPEG decoder & encoder driver.

Signed-off-by: Mirela Rabulea <[email protected]>
Reviewed-by: Rob Herring <[email protected]>
---
Changes in v6:
Reviewed-by: Rob Herring <[email protected]>

.../bindings/media/nxp,imx8-jpeg.yaml | 84 +++++++++++++++++++
1 file changed, 84 insertions(+)
create mode 100644 Documentation/devicetree/bindings/media/nxp,imx8-jpeg.yaml

diff --git a/Documentation/devicetree/bindings/media/nxp,imx8-jpeg.yaml b/Documentation/devicetree/bindings/media/nxp,imx8-jpeg.yaml
new file mode 100644
index 000000000000..5d13cbb5251b
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/nxp,imx8-jpeg.yaml
@@ -0,0 +1,84 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/media/nxp,imx8-jpeg.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: i.MX8QXP/QM JPEG decoder/encoder Device Tree Bindings
+
+maintainers:
+ - Mirela Rabulea <[email protected]>
+
+description: |-
+ The JPEG decoder/encoder present in iMX8QXP and iMX8QM SoCs is an
+ ISO/IEC 10918-1 JPEG standard compliant decoder/encoder, for Baseline
+ and Extended Sequential DCT modes.
+
+properties:
+ compatible:
+ items:
+ - enum:
+ # JPEG decoder
+ - nxp,imx8qxp-jpgdec
+ # JPEG encoder
+ - nxp,imx8qxp-jpgenc
+
+ reg:
+ maxItems: 1
+
+ interrupts:
+ description: |
+ There are 4 slots available in the IP, which the driver may use
+ If a certain slot is used, it should have an associated interrupt
+ The interrupt with index i is assumed to be for slot i
+ minItems: 1 # At least one slot is needed by the driver
+ maxItems: 4 # The IP has 4 slots available for use
+
+ power-domains:
+ description:
+ List of phandle and PM domain specifier as documented in
+ Documentation/devicetree/bindings/power/power_domain.txt
+ minItems: 2 # Wrapper and 1 slot
+ maxItems: 5 # Wrapper and 4 slots
+
+required:
+ - compatible
+ - reg
+ - interrupts
+ - power-domains
+
+additionalProperties: false
+
+examples:
+ - |
+ #include <dt-bindings/interrupt-controller/arm-gic.h>
+ #include <dt-bindings/firmware/imx/rsrc.h>
+
+ jpegdec: jpegdec@58400000 {
+ compatible = "nxp,imx8qxp-jpgdec";
+ reg = <0x58400000 0x00050000 >;
+ interrupts = <GIC_SPI 309 IRQ_TYPE_LEVEL_HIGH>,
+ <GIC_SPI 310 IRQ_TYPE_LEVEL_HIGH>,
+ <GIC_SPI 311 IRQ_TYPE_LEVEL_HIGH>,
+ <GIC_SPI 312 IRQ_TYPE_LEVEL_HIGH>;
+ power-domains = <&pd IMX_SC_R_MJPEG_DEC_MP>,
+ <&pd IMX_SC_R_MJPEG_DEC_S0>,
+ <&pd IMX_SC_R_MJPEG_DEC_S1>,
+ <&pd IMX_SC_R_MJPEG_DEC_S2>,
+ <&pd IMX_SC_R_MJPEG_DEC_S3>;
+ };
+
+ jpegenc: jpegenc@58450000 {
+ compatible = "nxp,imx8qxp-jpgenc";
+ reg = <0x58450000 0x00050000 >;
+ interrupts = <GIC_SPI 305 IRQ_TYPE_LEVEL_HIGH>,
+ <GIC_SPI 306 IRQ_TYPE_LEVEL_HIGH>,
+ <GIC_SPI 307 IRQ_TYPE_LEVEL_HIGH>,
+ <GIC_SPI 308 IRQ_TYPE_LEVEL_HIGH>;
+ power-domains = <&pd IMX_SC_R_MJPEG_ENC_MP>,
+ <&pd IMX_SC_R_MJPEG_ENC_S0>,
+ <&pd IMX_SC_R_MJPEG_ENC_S1>,
+ <&pd IMX_SC_R_MJPEG_ENC_S2>,
+ <&pd IMX_SC_R_MJPEG_ENC_S3>;
+ };
+...
--
2.17.1

2020-12-15 11:25:54

by Mirela Rabulea OSS

[permalink] [raw]
Subject: [PATCH v6 7/9] media: Quit parsing stream if doesn't start with SOI

From: Mirela Rabulea <[email protected]>

In the case we get an invalid stream, such as from v4l2-compliance
streaming test, jpeg_next_marker will end up parsing the entire
stream. The standard describes the high level syntax of a jpeg
as starting with SOI, ending with EOI, so return error if the very
first 2 bytes are not SOI.

Signed-off-by: Mirela Rabulea <[email protected]>
Reviewed-by: Philipp Zabel <[email protected]>
---
Changes in v6:
Reviewed-by: Philipp Zabel <[email protected]>

drivers/media/v4l2-core/v4l2-jpeg.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/drivers/media/v4l2-core/v4l2-jpeg.c b/drivers/media/v4l2-core/v4l2-jpeg.c
index d1483e7a775c..dc9def4c2648 100644
--- a/drivers/media/v4l2-core/v4l2-jpeg.c
+++ b/drivers/media/v4l2-core/v4l2-jpeg.c
@@ -503,11 +503,8 @@ int v4l2_jpeg_parse_header(void *buf, size_t len, struct v4l2_jpeg_header *out)
out->num_dht = 0;
out->num_dqt = 0;

- /* the first marker must be SOI */
- marker = jpeg_next_marker(&stream);
- if (marker < 0)
- return marker;
- if (marker != SOI)
+ /* the first bytes must be SOI, B.2.1 High-level syntax */
+ if (jpeg_get_word_be(&stream) != SOI)
return -EINVAL;

/* init value to signal if this marker is not present */
--
2.17.1

2020-12-15 11:25:57

by Mirela Rabulea OSS

[permalink] [raw]
Subject: [PATCH v6 8/9] media: Avoid parsing quantization and huffman tables

From: Mirela Rabulea <[email protected]>

These are optional in struct v4l2_jpeg_header, so skip DHT/DQT segment
parsing if huffman_tables/quantization_tables were not requested by user,
to save time.
However, do count them (num_dht/num_dqt).

Signed-off-by: Mirela Rabulea <[email protected]>
---
Changes in v6:
Call jpeg_skip_segment() instead of jpeg_parse_quantization_table()/jpeg_parse_huffman_tables(),
when quantization/huffman tables are not requested by user.
Remove the NULL pointer check in the lower-level function.
Thanks Philipp & Hans for feedback

drivers/media/v4l2-core/v4l2-jpeg.c | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/drivers/media/v4l2-core/v4l2-jpeg.c b/drivers/media/v4l2-core/v4l2-jpeg.c
index dc9def4c2648..f3d03d39defb 100644
--- a/drivers/media/v4l2-core/v4l2-jpeg.c
+++ b/drivers/media/v4l2-core/v4l2-jpeg.c
@@ -537,6 +537,10 @@ int v4l2_jpeg_parse_header(void *buf, size_t len, struct v4l2_jpeg_header *out)
&out->dht[out->num_dht++ % 4]);
if (ret < 0)
return ret;
+ if (!out->huffman_tables) {
+ ret = jpeg_skip_segment(&stream);
+ break;
+ }
ret = jpeg_parse_huffman_tables(&stream,
out->huffman_tables);
break;
@@ -545,6 +549,10 @@ int v4l2_jpeg_parse_header(void *buf, size_t len, struct v4l2_jpeg_header *out)
&out->dqt[out->num_dqt++ % 4]);
if (ret < 0)
return ret;
+ if (!out->quantization_tables) {
+ ret = jpeg_skip_segment(&stream);
+ break;
+ }
ret = jpeg_parse_quantization_tables(&stream,
out->frame.precision,
out->quantization_tables);
--
2.17.1

2020-12-15 11:26:03

by Mirela Rabulea OSS

[permalink] [raw]
Subject: [PATCH v6 9/9] media: imx-jpeg: Use v4l2 jpeg helpers in mxc-jpeg

From: Mirela Rabulea <[email protected]>

Use v4l2_jpeg_parse_header in mxc_jpeg_parse, remove the old
parsing way, which was duplicated in other jpeg drivers.

Signed-off-by: Mirela Rabulea <[email protected]>
---
Changes in v6:
Use the app14 transform flag enum

drivers/media/platform/imx-jpeg/Kconfig | 1 +
drivers/media/platform/imx-jpeg/mxc-jpeg.c | 267 ++++++---------------
drivers/media/platform/imx-jpeg/mxc-jpeg.h | 26 +-
3 files changed, 80 insertions(+), 214 deletions(-)

diff --git a/drivers/media/platform/imx-jpeg/Kconfig b/drivers/media/platform/imx-jpeg/Kconfig
index 7cc89e5eff90..d875f7c88cda 100644
--- a/drivers/media/platform/imx-jpeg/Kconfig
+++ b/drivers/media/platform/imx-jpeg/Kconfig
@@ -4,6 +4,7 @@ config VIDEO_IMX8_JPEG
depends on VIDEO_DEV && VIDEO_V4L2
select VIDEOBUF2_DMA_CONTIG
select V4L2_MEM2MEM_DEV
+ select V4L2_JPEG_HELPER
default m
help
This is a video4linux2 driver for the i.MX8 QXP/QM integrated
diff --git a/drivers/media/platform/imx-jpeg/mxc-jpeg.c b/drivers/media/platform/imx-jpeg/mxc-jpeg.c
index 8f297803f2c3..a33e6a071cc9 100644
--- a/drivers/media/platform/imx-jpeg/mxc-jpeg.c
+++ b/drivers/media/platform/imx-jpeg/mxc-jpeg.c
@@ -52,6 +52,7 @@
#include <linux/pm_domain.h>
#include <linux/string.h>

+#include <media/v4l2-jpeg.h>
#include <media/v4l2-mem2mem.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-common.h>
@@ -65,12 +66,16 @@ static struct mxc_jpeg_fmt mxc_formats[] = {
{
.name = "JPEG",
.fourcc = V4L2_PIX_FMT_JPEG,
+ .subsampling = -1,
+ .nc = -1,
.colplanes = 1,
.flags = MXC_JPEG_FMT_TYPE_ENC,
},
{
.name = "RGB", /*RGBRGB packed format*/
.fourcc = V4L2_PIX_FMT_RGB24,
+ .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
+ .nc = 3,
.depth = 24,
.colplanes = 1,
.h_align = 3,
@@ -80,6 +85,8 @@ static struct mxc_jpeg_fmt mxc_formats[] = {
{
.name = "ARGB", /* ARGBARGB packed format */
.fourcc = V4L2_PIX_FMT_ARGB32,
+ .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
+ .nc = 4,
.depth = 32,
.colplanes = 1,
.h_align = 3,
@@ -89,6 +96,8 @@ static struct mxc_jpeg_fmt mxc_formats[] = {
{
.name = "YUV420", /* 1st plane = Y, 2nd plane = UV */
.fourcc = V4L2_PIX_FMT_NV12,
+ .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420,
+ .nc = 3,
.depth = 12, /* 6 bytes (4Y + UV) for 4 pixels */
.colplanes = 2, /* 1 plane Y, 1 plane UV interleaved */
.h_align = 4,
@@ -98,6 +107,8 @@ static struct mxc_jpeg_fmt mxc_formats[] = {
{
.name = "YUV422", /* YUYV */
.fourcc = V4L2_PIX_FMT_YUYV,
+ .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422,
+ .nc = 3,
.depth = 16,
.colplanes = 1,
.h_align = 4,
@@ -107,6 +118,8 @@ static struct mxc_jpeg_fmt mxc_formats[] = {
{
.name = "YUV444", /* YUVYUV */
.fourcc = V4L2_PIX_FMT_YUV24,
+ .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_444,
+ .nc = 3,
.depth = 24,
.colplanes = 1,
.h_align = 3,
@@ -116,6 +129,8 @@ static struct mxc_jpeg_fmt mxc_formats[] = {
{
.name = "Gray", /* Gray (Y8/Y12) or Single Comp */
.fourcc = V4L2_PIX_FMT_GREY,
+ .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY,
+ .nc = 1,
.depth = 8,
.colplanes = 1,
.h_align = 3,
@@ -419,33 +434,6 @@ static enum mxc_jpeg_image_format mxc_jpeg_fourcc_to_imgfmt(u32 fourcc)
}
}

-static int mxc_jpeg_imgfmt_to_fourcc(enum mxc_jpeg_image_format imgfmt,
- u32 *fourcc)
-{
- switch (imgfmt) {
- case MXC_JPEG_GRAY:
- *fourcc = V4L2_PIX_FMT_GREY;
- return 0;
- case MXC_JPEG_YUV422:
- *fourcc = V4L2_PIX_FMT_YUYV;
- return 0;
- case MXC_JPEG_YUV420:
- *fourcc = V4L2_PIX_FMT_NV12;
- return 0;
- case MXC_JPEG_YUV444:
- *fourcc = V4L2_PIX_FMT_YUV24;
- return 0;
- case MXC_JPEG_RGB:
- *fourcc = V4L2_PIX_FMT_RGB24;
- return 0;
- case MXC_JPEG_ARGB:
- *fourcc = V4L2_PIX_FMT_ARGB32;
- return 0;
- default:
- return 1;
- }
-}
-
static struct mxc_jpeg_q_data *mxc_jpeg_get_q_data(struct mxc_jpeg_ctx *ctx,
enum v4l2_buf_type type)
{
@@ -1165,45 +1153,6 @@ static void mxc_jpeg_stop_streaming(struct vb2_queue *q)
release_active_buffers(q, VB2_BUF_STATE_ERROR);
}

-struct mxc_jpeg_stream {
- u8 *addr;
- u32 loc;
- u32 end;
-};
-
-static int get_byte(struct mxc_jpeg_stream *stream)
-{
- int ret;
-
- if (stream->loc >= stream->end)
- return -1;
- ret = stream->addr[stream->loc];
- stream->loc++;
- return ret;
-}
-
-static int get_sof(struct device *dev,
- struct mxc_jpeg_stream *stream,
- struct mxc_jpeg_sof *sof)
-{
- int i;
-
- if (stream->loc + sizeof(struct mxc_jpeg_sof) >= stream->end)
- return -1;
- memcpy(sof, &stream->addr[stream->loc], sizeof(struct mxc_jpeg_sof));
- _bswap16(&sof->length);
- _bswap16(&sof->height);
- _bswap16(&sof->width);
- dev_dbg(dev, "JPEG SOF: precision=%d\n", sof->precision);
- dev_dbg(dev, "JPEG SOF: height=%d, width=%d\n",
- sof->height, sof->width);
- for (i = 0; i < sof->components_no; i++) {
- dev_dbg(dev, "JPEG SOF: comp_id=%d, H=0x%x, V=0x%x\n",
- sof->comp[i].id, sof->comp[i].v, sof->comp[i].h);
- }
- return 0;
-}
-
static int mxc_jpeg_valid_comp_id(struct device *dev,
struct mxc_jpeg_sof *sof,
struct mxc_jpeg_sos *sos)
@@ -1233,45 +1182,18 @@ static int mxc_jpeg_valid_comp_id(struct device *dev,
return valid;
}

-static enum mxc_jpeg_image_format
-mxc_jpeg_get_image_format(struct device *dev, const struct mxc_jpeg_sof *sof)
+static u32 mxc_jpeg_get_image_format(struct device *dev,
+ const struct v4l2_jpeg_header header)
{
- if (sof->components_no == 1) {
- dev_dbg(dev, "IMAGE_FORMAT is: MXC_JPEG_GRAY\n");
- return MXC_JPEG_GRAY;
- }
- if (sof->components_no == 3) {
- if (sof->comp[0].h == 2 && sof->comp[0].v == 2 &&
- sof->comp[1].h == 1 && sof->comp[1].v == 1 &&
- sof->comp[2].h == 1 && sof->comp[2].v == 1){
- dev_dbg(dev, "IMAGE_FORMAT is: MXC_JPEG_YUV420\n");
- return MXC_JPEG_YUV420;
- }
- if (sof->comp[0].h == 2 && sof->comp[0].v == 1 &&
- sof->comp[1].h == 1 && sof->comp[1].v == 1 &&
- sof->comp[2].h == 1 && sof->comp[2].v == 1){
- dev_dbg(dev, "IMAGE_FORMAT is: MXC_JPEG_YUV422\n");
- return MXC_JPEG_YUV422;
- }
- if (sof->comp[0].h == 1 && sof->comp[0].v == 1 &&
- sof->comp[1].h == 1 && sof->comp[1].v == 1 &&
- sof->comp[2].h == 1 && sof->comp[2].v == 1){
- dev_dbg(dev, "IMAGE_FORMAT is: MXC_JPEG_YUV444\n");
- return MXC_JPEG_YUV444;
- }
- }
- if (sof->components_no == 4) {
- if (sof->comp[0].h == 1 && sof->comp[0].v == 1 &&
- sof->comp[1].h == 1 && sof->comp[1].v == 1 &&
- sof->comp[2].h == 1 && sof->comp[2].v == 1 &&
- sof->comp[3].h == 1 && sof->comp[3].v == 1){
- /* this is not tested */
- dev_dbg(dev, "IMAGE_FORMAT is: MXC_JPEG_ARGB\n");
- return MXC_JPEG_ARGB;
- }
- }
+ int i;
+
+ for (i = 0; i < MXC_JPEG_NUM_FORMATS; i++)
+ if (mxc_formats[i].subsampling == header.frame.subsampling &&
+ mxc_formats[i].nc == header.frame.num_components)
+ return mxc_formats[i].fourcc;
+
dev_err(dev, "Could not identify image format\n");
- return MXC_JPEG_INVALID;
+ return 0;
}

static void mxc_jpeg_bytesperline(struct mxc_jpeg_q_data *q,
@@ -1325,122 +1247,69 @@ static int mxc_jpeg_parse(struct mxc_jpeg_ctx *ctx,
struct device *dev = ctx->mxc_jpeg->dev;
struct mxc_jpeg_q_data *q_data_out, *q_data_cap;
enum v4l2_buf_type cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
- struct mxc_jpeg_stream stream;
- bool notfound = true;
- bool app14 = false;
bool src_chg = false;
- u8 app14_transform = 0;
- struct mxc_jpeg_sof sof, *psof = NULL;
- struct mxc_jpeg_sos *psos = NULL;
- int byte;
- u8 *next = NULL;
- enum mxc_jpeg_image_format img_fmt;
u32 fourcc;
+ struct v4l2_jpeg_header header;
+ struct mxc_jpeg_sof *psof = NULL;
+ struct mxc_jpeg_sos *psos = NULL;
+ int ret;

- memset(&sof, 0, sizeof(struct mxc_jpeg_sof));
- stream.addr = src_addr;
- stream.end = size;
- stream.loc = 0;
- *dht_needed = true;
+ memset(&header, 0, sizeof(header));
+ ret = v4l2_jpeg_parse_header((void *)src_addr, size, &header);
+ if (ret < 0) {
+ dev_err(dev, "Error parsing JPEG stream markers\n");
+ return ret;
+ }

- /* check stream starts with SOI */
- byte = get_byte(&stream);
- if (byte == -1 || byte != 0xFF)
- return -EINVAL;
- byte = get_byte(&stream);
- if (byte == -1 || byte != 0xD8)
- return -EINVAL;
+ /* if DHT marker present, no need to inject default one */
+ *dht_needed = (header.num_dht == 0);

- while (notfound) {
- byte = get_byte(&stream);
- if (byte == -1)
- return -EINVAL;
- if (byte != 0xff)
- continue;
- do {
- byte = get_byte(&stream);
- } while (byte == 0xff);
- if (byte == -1)
- return false;
- if (byte == 0)
- continue;
- switch (byte) {
- case DHT:
- /* DHT marker present, no need to inject default one */
- *dht_needed = false;
- break;
- case SOF2: /* Progressive DCF frame definition */
- dev_err(dev,
- "Progressive JPEG not supported by hardware");
- return -EINVAL;
- case SOF1: /* Extended sequential DCF frame definition */
- case SOF0: /* Baseline sequential DCF frame definition */
- if (get_sof(dev, &stream, &sof) == -1)
- break;
- next = stream.addr + stream.loc;
- psof = (struct mxc_jpeg_sof *)next;
- break;
- case SOS:
- next = stream.addr + stream.loc;
- psos = (struct mxc_jpeg_sos *)next;
- notfound = false;
- break;
- case APP14:
- app14 = true;
- /*
- * Application Data Syntax is:
- * 2 bytes(APPn:0xFF,0xEE), 2 bytes(Lp), Ap1...ApLp-2
- * The transform flag is in Ap12
- * stream.loc is now on APPn-0xEE byte
- */
- app14_transform = *(stream.addr + stream.loc + 12 + 1);
- break;
- default:
- notfound = true;
- }
- }
q_data_out = mxc_jpeg_get_q_data(ctx,
V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
if (q_data_out->w == 0 && q_data_out->h == 0) {
dev_warn(dev, "Invalid user resolution 0x0");
dev_warn(dev, "Keeping resolution from JPEG: %dx%d",
- sof.width, sof.height);
- q_data_out->w = sof.width;
- q_data_out->h = sof.height;
- } else if (sof.width != q_data_out->w || sof.height != q_data_out->h) {
+ header.frame.width, header.frame.height);
+ q_data_out->w = header.frame.width;
+ q_data_out->h = header.frame.height;
+ } else if (header.frame.width != q_data_out->w ||
+ header.frame.height != q_data_out->h) {
dev_err(dev,
"Resolution mismatch: %dx%d (JPEG) versus %dx%d(user)",
- sof.width, sof.height, q_data_out->w, q_data_out->h);
+ header.frame.width, header.frame.height,
+ q_data_out->w, q_data_out->h);
return -EINVAL;
}
- if (sof.width % 8 != 0 || sof.height % 8 != 0) {
+ if (header.frame.width % 8 != 0 || header.frame.height % 8 != 0) {
dev_err(dev, "JPEG width or height not multiple of 8: %dx%d\n",
- sof.width, sof.height);
+ header.frame.width, header.frame.height);
return -EINVAL;
}
- if (sof.width > MXC_JPEG_MAX_WIDTH ||
- sof.height > MXC_JPEG_MAX_HEIGHT) {
+ if (header.frame.width > MXC_JPEG_MAX_WIDTH ||
+ header.frame.height > MXC_JPEG_MAX_HEIGHT) {
dev_err(dev, "JPEG width or height should be <= 8192: %dx%d\n",
- sof.width, sof.height);
+ header.frame.width, header.frame.height);
return -EINVAL;
}
- if (sof.width < MXC_JPEG_MIN_WIDTH ||
- sof.height < MXC_JPEG_MIN_HEIGHT) {
+ if (header.frame.width < MXC_JPEG_MIN_WIDTH ||
+ header.frame.height < MXC_JPEG_MIN_HEIGHT) {
dev_err(dev, "JPEG width or height should be > 64: %dx%d\n",
- sof.width, sof.height);
+ header.frame.width, header.frame.height);
return -EINVAL;
}
- if (sof.components_no > MXC_JPEG_MAX_COMPONENTS) {
+ if (header.frame.num_components > V4L2_JPEG_MAX_COMPONENTS) {
dev_err(dev, "JPEG number of components should be <=%d",
- MXC_JPEG_MAX_COMPONENTS);
+ V4L2_JPEG_MAX_COMPONENTS);
return -EINVAL;
}
/* check and, if necessary, patch component IDs*/
+ psof = (struct mxc_jpeg_sof *)header.sof.start;
+ psos = (struct mxc_jpeg_sos *)header.sos.start;
if (!mxc_jpeg_valid_comp_id(dev, psof, psos))
dev_warn(dev, "JPEG component ids should be 0-3 or 1-4");

- img_fmt = mxc_jpeg_get_image_format(dev, &sof);
- if (img_fmt == MXC_JPEG_INVALID)
+ fourcc = mxc_jpeg_get_image_format(dev, header);
+ if (fourcc == 0)
return -EINVAL;

/*
@@ -1448,12 +1317,11 @@ static int mxc_jpeg_parse(struct mxc_jpeg_ctx *ctx,
* encoded with 3 components have RGB colorspace, see Recommendation
* ITU-T T.872 chapter 6.5.3 APP14 marker segment for colour encoding
*/
- if (img_fmt == MXC_JPEG_YUV444 && app14 && app14_transform == 0)
- img_fmt = MXC_JPEG_RGB;
-
- if (mxc_jpeg_imgfmt_to_fourcc(img_fmt, &fourcc)) {
- dev_err(dev, "Fourcc not found for %d", img_fmt);
- return -EINVAL;
+ if (fourcc == V4L2_PIX_FMT_YUV24 || fourcc == V4L2_PIX_FMT_RGB24) {
+ if (header.app14_tf == V4L2_JPEG_APP14_TF_CMYK_RGB)
+ fourcc = V4L2_PIX_FMT_RGB24;
+ else
+ fourcc = V4L2_PIX_FMT_YUV24;
}

/*
@@ -1461,10 +1329,11 @@ static int mxc_jpeg_parse(struct mxc_jpeg_ctx *ctx,
* detected from the jpeg output stream
*/
q_data_cap = mxc_jpeg_get_q_data(ctx, cap_type);
- if (q_data_cap->w != sof.width || q_data_cap->h != sof.height)
+ if (q_data_cap->w != header.frame.width ||
+ q_data_cap->h != header.frame.height)
src_chg = true;
- q_data_cap->w = sof.width;
- q_data_cap->h = sof.height;
+ q_data_cap->w = header.frame.width;
+ q_data_cap->h = header.frame.height;
q_data_cap->fmt = mxc_jpeg_find_format(ctx, fourcc);
q_data_cap->w_adjusted = q_data_cap->w;
q_data_cap->h_adjusted = q_data_cap->h;
@@ -1490,7 +1359,7 @@ static int mxc_jpeg_parse(struct mxc_jpeg_ctx *ctx,
(fourcc >> 24) & 0xff);

/* setup bytesperline/sizeimage for capture queue */
- mxc_jpeg_bytesperline(q_data_cap, sof.precision);
+ mxc_jpeg_bytesperline(q_data_cap, header.frame.precision);
mxc_jpeg_sizeimage(q_data_cap);

/*
diff --git a/drivers/media/platform/imx-jpeg/mxc-jpeg.h b/drivers/media/platform/imx-jpeg/mxc-jpeg.h
index ef1670dafeb4..313b09f831a4 100644
--- a/drivers/media/platform/imx-jpeg/mxc-jpeg.h
+++ b/drivers/media/platform/imx-jpeg/mxc-jpeg.h
@@ -42,6 +42,8 @@ enum mxc_jpeg_mode {
* struct jpeg_fmt - driver's internal color format data
* @name: format description
* @fourcc: fourcc code, 0 if not applicable
+ * @subsampling subsampling of jpeg components
+ * @nc: number of color components
* @depth: number of bits per pixel
* @colplanes: number of color planes (1 for packed formats)
* @h_align: horizontal alignment order (align to 2^h_align)
@@ -49,13 +51,15 @@ enum mxc_jpeg_mode {
* @flags: flags describing format applicability
*/
struct mxc_jpeg_fmt {
- char *name;
- u32 fourcc;
- int depth;
- int colplanes;
- int h_align;
- int v_align;
- u32 flags;
+ char *name;
+ u32 fourcc;
+ enum v4l2_jpeg_chroma_subsampling subsampling;
+ int nc;
+ int depth;
+ int colplanes;
+ int h_align;
+ int v_align;
+ u32 flags;
};

struct mxc_jpeg_desc {
@@ -117,14 +121,6 @@ struct mxc_jpeg_dev {
struct device_link **pd_link;
};

-/* JPEG marker identifiers */
-#define SOF0 0xC0
-#define SOF1 0xC1
-#define SOF2 0xC2
-#define SOS 0xDA
-#define DHT 0xC4
-#define APP14 0xEE
-
/**
* struct mxc_jpeg_sof_comp - JPEG Start Of Frame component fields
* @id: component id
--
2.17.1

2020-12-15 11:26:14

by Mirela Rabulea OSS

[permalink] [raw]
Subject: [PATCH v6 6/9] media: Add parsing for APP14 data segment in jpeg helpers

From: Mirela Rabulea <[email protected]>

According to Rec. ITU-T T.872 (06/2012) 6.5.3
APP14 segment is for color encoding, it contains a transform flag, which
may have values of 0, 1 and 2 and are interpreted as follows:
0 - CMYK for images that are encoded with four components
- RGB for images that are encoded with three components
1 - An image encoded with three components using YCbCr colour encoding.
2 - An image encoded with four components using YCCK colour encoding.

This is used in imx-jpeg decoder, to distinguish between
YUV444 and RGB24.

Signed-off-by: Mirela Rabulea <[email protected]>
---
Changes in v6:
Switch variable to lowercase Lp->lp
Check for "Adobe\0" in Ap1..6
Make the transform flag an enum
Removed a change in comment section, a leftover from a previous version
Thanks Philipp for feedback.

drivers/media/v4l2-core/v4l2-jpeg.c | 43 +++++++++++++++++++++++++++--
include/media/v4l2-jpeg.h | 18 ++++++++++++
2 files changed, 59 insertions(+), 2 deletions(-)

diff --git a/drivers/media/v4l2-core/v4l2-jpeg.c b/drivers/media/v4l2-core/v4l2-jpeg.c
index 8947fd95c6f1..d1483e7a775c 100644
--- a/drivers/media/v4l2-core/v4l2-jpeg.c
+++ b/drivers/media/v4l2-core/v4l2-jpeg.c
@@ -45,6 +45,7 @@ MODULE_LICENSE("GPL");
#define DHP 0xffde /* hierarchical progression */
#define EXP 0xffdf /* expand reference */
#define APP0 0xffe0 /* application data */
+#define APP14 0xffee /* application data for colour encoding */
#define APP15 0xffef
#define JPG0 0xfff0 /* extensions */
#define JPG13 0xfffd
@@ -444,8 +445,41 @@ static int jpeg_skip_segment(struct jpeg_stream *stream)
return jpeg_skip(stream, len - 2);
}

+/* Rec. ITU-T T.872 (06/2012) 6.5.3 */
+static int jpeg_parse_app14_data(struct jpeg_stream *stream)
+{
+ int ret;
+ int lp;
+ int skip;
+ int tf;
+
+ lp = jpeg_get_word_be(stream);
+ if (lp < 0)
+ return lp;
+
+ /* Check for "Adobe\0" in Ap1..6 */
+ if (strncmp(stream->curr, "Adobe\0", 6))
+ return -EINVAL;
+
+ /* get to Ap12 */
+ ret = jpeg_skip(stream, 11);
+ if (ret < 0)
+ return -EINVAL;
+
+ tf = jpeg_get_byte(stream);
+ if (tf < 0)
+ return tf;
+
+ skip = lp - 2 - 11;
+ ret = jpeg_skip(stream, skip);
+ if (ret < 0)
+ return -EINVAL;
+ else
+ return tf;
+}
+
/**
- * jpeg_parse_header - locate marker segments and optionally parse headers
+ * v4l2_jpeg_parse_header - locate marker segments and optionally parse headers
* @buf: address of the JPEG buffer, should start with a SOI marker
* @len: length of the JPEG buffer
* @out: returns marker segment positions and optionally parsed headers
@@ -476,6 +510,9 @@ int v4l2_jpeg_parse_header(void *buf, size_t len, struct v4l2_jpeg_header *out)
if (marker != SOI)
return -EINVAL;

+ /* init value to signal if this marker is not present */
+ out->app14_tf = -EINVAL;
+
/* loop through marker segments */
while ((marker = jpeg_next_marker(&stream)) >= 0) {
switch (marker) {
@@ -519,7 +556,9 @@ int v4l2_jpeg_parse_header(void *buf, size_t len, struct v4l2_jpeg_header *out)
ret = jpeg_parse_restart_interval(&stream,
&out->restart_interval);
break;
-
+ case APP14:
+ out->app14_tf = jpeg_parse_app14_data(&stream);
+ break;
case SOS:
ret = jpeg_reference_segment(&stream, &out->sos);
if (ret < 0)
diff --git a/include/media/v4l2-jpeg.h b/include/media/v4l2-jpeg.h
index ddba2a56c321..c1904aa3f7cc 100644
--- a/include/media/v4l2-jpeg.h
+++ b/include/media/v4l2-jpeg.h
@@ -87,6 +87,22 @@ struct v4l2_jpeg_scan_header {
/* Ss, Se, Ah, and Al are not used by any driver */
};

+/**
+ * enum v4l2_jpeg_app14_tf - APP14 transform flag
+ * According to Rec. ITU-T T.872 (06/2012) 6.5.3
+ * APP14 segment is for color encoding, it contains a transform flag,
+ * which may have values of 0, 1 and 2 and are interpreted as follows:
+ * @V4L2_JPEG_APP14_TF_CMYK_RGB: CMYK for images encoded with four components
+ * RGB for images encoded with three components
+ * @V4L2_JPEG_APP14_TF_YCBCR: an image encoded with three components using YCbCr
+ * @V4L2_JPEG_APP14_TF_YCCK: an image encoded with four components using YCCK
+ */
+enum v4l2_jpeg_app14_tf {
+ V4L2_JPEG_APP14_TF_CMYK_RGB = 0,
+ V4L2_JPEG_APP14_TF_YCBCR = 1,
+ V4L2_JPEG_APP14_TF_YCCK = 2,
+};
+
/**
* struct v4l2_jpeg_header - parsed JPEG header
* @sof: pointer to frame header and size
@@ -100,6 +116,7 @@ struct v4l2_jpeg_scan_header {
* order, optional
* @restart_interval: number of MCU per restart interval, Ri
* @ecs_offset: buffer offset in bytes to the entropy coded segment
+ * @app14_tf: transform flag from app14 data
*
* When this structure is passed to v4l2_jpeg_parse_header, the optional scan,
* quantization_tables, and huffman_tables pointers must be initialized to NULL
@@ -119,6 +136,7 @@ struct v4l2_jpeg_header {
struct v4l2_jpeg_reference *huffman_tables;
u16 restart_interval;
size_t ecs_offset;
+ enum v4l2_jpeg_app14_tf app14_tf;
};

int v4l2_jpeg_parse_header(void *buf, size_t len, struct v4l2_jpeg_header *out);
--
2.17.1

2020-12-15 11:26:46

by Mirela Rabulea OSS

[permalink] [raw]
Subject: [PATCH v6 5/9] Add maintainer for IMX jpeg v4l2 driver

From: Mirela Rabulea <[email protected]>

The driver is located in drivers/media/platform/imx-jpeg,
and it applies to the JPEG decoder from i.MX QXP and QM.

Signed-off-by: Mirela Rabulea <[email protected]>
---
MAINTAINERS | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index c9e221929961..09050a727d57 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -12818,6 +12818,14 @@ L: [email protected] (moderated for non-subscribers)
S: Supported
F: drivers/nfc/nxp-nci

+NXP i.MX 8QXP/8QM JPEG V4L2 DRIVER
+M: Mirela Rabulea <[email protected]>
+R: NXP Linux Team <[email protected]>
+L: [email protected]
+S: Maintained
+F: Documentation/devicetree/bindings/media/imx8-jpeg.yaml
+F: drivers/media/platform/imx-jpeg
+
OBJAGG
M: Jiri Pirko <[email protected]>
L: [email protected]
--
2.17.1

2021-01-04 17:03:17

by Philipp Zabel

[permalink] [raw]
Subject: Re: [PATCH v6 6/9] media: Add parsing for APP14 data segment in jpeg helpers

Hi Mirela,

thank you for the update. Just two issues below:

On Tue, 2020-12-15 at 13:18 +0200, Mirela Rabulea (OSS) wrote:
> From: Mirela Rabulea <[email protected]>
>
> According to Rec. ITU-T T.872 (06/2012) 6.5.3
> APP14 segment is for color encoding, it contains a transform flag, which
> may have values of 0, 1 and 2 and are interpreted as follows:
> 0 - CMYK for images that are encoded with four components
> - RGB for images that are encoded with three components
> 1 - An image encoded with three components using YCbCr colour encoding.
> 2 - An image encoded with four components using YCCK colour encoding.
>
> This is used in imx-jpeg decoder, to distinguish between
> YUV444 and RGB24.
>
> Signed-off-by: Mirela Rabulea <[email protected]>
> ---
> Changes in v6:
> Switch variable to lowercase Lp->lp
> Check for "Adobe\0" in Ap1..6
> Make the transform flag an enum
> Removed a change in comment section, a leftover from a previous version
> Thanks Philipp for feedback.
>
> drivers/media/v4l2-core/v4l2-jpeg.c | 43 +++++++++++++++++++++++++++--
> include/media/v4l2-jpeg.h | 18 ++++++++++++
> 2 files changed, 59 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/media/v4l2-core/v4l2-jpeg.c b/drivers/media/v4l2-core/v4l2-jpeg.c
> index 8947fd95c6f1..d1483e7a775c 100644
> --- a/drivers/media/v4l2-core/v4l2-jpeg.c
> +++ b/drivers/media/v4l2-core/v4l2-jpeg.c
> @@ -45,6 +45,7 @@ MODULE_LICENSE("GPL");
> #define DHP 0xffde /* hierarchical progression */
> #define EXP 0xffdf /* expand reference */
> #define APP0 0xffe0 /* application data */
> +#define APP14 0xffee /* application data for colour encoding */
> #define APP15 0xffef
> #define JPG0 0xfff0 /* extensions */
> #define JPG13 0xfffd
> @@ -444,8 +445,41 @@ static int jpeg_skip_segment(struct jpeg_stream *stream)
> return jpeg_skip(stream, len - 2);
> }
>
> +/* Rec. ITU-T T.872 (06/2012) 6.5.3 */
> +static int jpeg_parse_app14_data(struct jpeg_stream *stream)
> +{
> + int ret;
> + int lp;
> + int skip;
> + int tf;
> +
> + lp = jpeg_get_word_be(stream);
> + if (lp < 0)
> + return lp;

Here we should check that there are still 6 bytes available to compare:

if (stream->curr + 6 > stream->end)
return -EINVAL;

> + /* Check for "Adobe\0" in Ap1..6 */
> + if (strncmp(stream->curr, "Adobe\0", 6))
> + return -EINVAL;
> +
> + /* get to Ap12 */
> + ret = jpeg_skip(stream, 11);
> + if (ret < 0)
> + return -EINVAL;
> +
> + tf = jpeg_get_byte(stream);
> + if (tf < 0)
> + return tf;
> +
> + skip = lp - 2 - 11;
> + ret = jpeg_skip(stream, skip);
> + if (ret < 0)
> + return -EINVAL;
> + else
> + return tf;
> +}
> +
> /**
> - * jpeg_parse_header - locate marker segments and optionally parse headers
> + * v4l2_jpeg_parse_header - locate marker segments and optionally parse headers
> * @buf: address of the JPEG buffer, should start with a SOI marker
> * @len: length of the JPEG buffer
> * @out: returns marker segment positions and optionally parsed headers
> @@ -476,6 +510,9 @@ int v4l2_jpeg_parse_header(void *buf, size_t len, struct v4l2_jpeg_header *out)
> if (marker != SOI)
> return -EINVAL;
>
> + /* init value to signal if this marker is not present */
> + out->app14_tf = -EINVAL;
> +

Here we set app14_tf to a value that is not part of the enum.
You could define a value V4L2_JPEG_APP14_TF_UNKNOWN for the
uninitialized / error state.

> /* loop through marker segments */
> while ((marker = jpeg_next_marker(&stream)) >= 0) {
> switch (marker) {
> @@ -519,7 +556,9 @@ int v4l2_jpeg_parse_header(void *buf, size_t len, struct v4l2_jpeg_header *out)
> ret = jpeg_parse_restart_interval(&stream,
> &out->restart_interval);
> break;
> -
> + case APP14:
> + out->app14_tf = jpeg_parse_app14_data(&stream);

Same as above in case of -EINVAL return. Apart from this,

Reviewed-by: Philipp Zabel <[email protected]>

regards
Philipp

2021-01-04 17:03:31

by Philipp Zabel

[permalink] [raw]
Subject: Re: [PATCH v6 8/9] media: Avoid parsing quantization and huffman tables

On Tue, 2020-12-15 at 13:18 +0200, Mirela Rabulea (OSS) wrote:
> From: Mirela Rabulea <[email protected]>
>
> These are optional in struct v4l2_jpeg_header, so skip DHT/DQT segment
> parsing if huffman_tables/quantization_tables were not requested by user,
> to save time.
> However, do count them (num_dht/num_dqt).
>
> Signed-off-by: Mirela Rabulea <[email protected]>
> ---
> Changes in v6:
> Call jpeg_skip_segment() instead of jpeg_parse_quantization_table()/jpeg_parse_huffman_tables(),
> when quantization/huffman tables are not requested by user.
> Remove the NULL pointer check in the lower-level function.
> Thanks Philipp & Hans for feedback
>
> drivers/media/v4l2-core/v4l2-jpeg.c | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/drivers/media/v4l2-core/v4l2-jpeg.c b/drivers/media/v4l2-core/v4l2-jpeg.c
> index dc9def4c2648..f3d03d39defb 100644
> --- a/drivers/media/v4l2-core/v4l2-jpeg.c
> +++ b/drivers/media/v4l2-core/v4l2-jpeg.c
> @@ -537,6 +537,10 @@ int v4l2_jpeg_parse_header(void *buf, size_t len, struct v4l2_jpeg_header *out)
> &out->dht[out->num_dht++ % 4]);
> if (ret < 0)
> return ret;
> + if (!out->huffman_tables) {
> + ret = jpeg_skip_segment(&stream);
> + break;
> + }
> ret = jpeg_parse_huffman_tables(&stream,
> out->huffman_tables);
> break;
> @@ -545,6 +549,10 @@ int v4l2_jpeg_parse_header(void *buf, size_t len, struct v4l2_jpeg_header *out)
> &out->dqt[out->num_dqt++ % 4]);
> if (ret < 0)
> return ret;
> + if (!out->quantization_tables) {
> + ret = jpeg_skip_segment(&stream);
> + break;
> + }
> ret = jpeg_parse_quantization_tables(&stream,
> out->frame.precision,
> out->quantization_tables);

Reviewed-by: Philipp Zabel <[email protected]>

regards
Philipp