Hantro VPU JPEG encoder fixes
Subsystems (2)
cover Hantro VPU JPEG encoder fixes
This series addresses quality issues in encoded JPEG images.
The first patch actually restores the intention of the original submission of this driver: due to a typo the helper variables were unused and then have been removed in some cleanup done by Mauro.
The second patch aligns the driver's luma quantization table with the one in the ITU-T.81 standard.
The third patch changes the order in which quantization tables are written to the resulting file and to the hardware. The file expects a zig-zag order, while the hardware wants some special order, neither linear nor zig-zag. In other words, hardware-wise it rearranges which parts of quantization tables go into which 4-byte registers - in a hardware specific order rather than linear or zig-zag. It also affects rk3288 and hasn't been tested with it.
The fourth patch then rearranges the sequence of register writes. The whole luma quantization table must be written first, and then the chroma quantization is written. In other words, while patch 3/4 changes what goes into which register, this patch changes when each register is written to. It also affects rk3288 and hasn't been tested with it.
Andrzej Pietrasiewicz (4):
media: hantro: Read be32 words starting at every fourth byte
media: hantro: Use standard luma quantization table
media: hantro: Write the quantization tables in proper order
media: hantro: Write quantization table registers in increasing
addresses order
.../staging/media/hantro/hantro_h1_jpeg_enc.c | 19 ++++- drivers/staging/media/hantro/hantro_jpeg.c | 76 ++++++++++++++----- drivers/staging/media/hantro/hantro_jpeg.h | 2 +- .../media/hantro/rk3399_vpu_hw_jpeg_enc.c | 24 ++++-- 4 files changed, 89 insertions(+), 32 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/4 media: hantro: Read be32 words starting at every fourth byte
Since (luma/chroma)_qtable is an array of unsigned char, indexing it returns consecutive byte locations, but we are supposed to read the arrays in four-byte words. Consequently, we should be pointing get_unaligned_be32() at consecutive word locations instead.
--- drivers/staging/media/hantro/hantro_h1_jpeg_enc.c | 9 +++++++-- drivers/staging/media/hantro/rk3399_vpu_hw_jpeg_enc.c | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-)
drivers/staging/media/hantro/hantro_h1_jpeg_enc.c +7 -2
@@ -67,12 +67,17 @@ hantro_h1_jpeg_enc_set_qtable(struct hantro_dev *vpu, unsigned char *chroma_qtable) {u32 reg, i; +__be32 *luma_qtable_p; +__be32 *chroma_qtable_p; + + luma_qtable_p = (__be32 *)luma_qtable; + chroma_qtable_p = (__be32 *)chroma_qtable;for (i =0 ; i <H1_JPEG_QUANT_TABLE_COUNT ; i++) { - reg =get_unaligned_be32 (&luma_qtable[i]); + reg =get_unaligned_be32 (&luma_qtable_p[i]);vepu_write_relaxed (vpu, reg,H1_REG_JPEG_LUMA_QUAT (i)); - reg =get_unaligned_be32 (&chroma_qtable[i]); + reg =get_unaligned_be32 (&chroma_qtable_p[i]);vepu_write_relaxed (vpu, reg,H1_REG_JPEG_CHROMA_QUAT (i)); } }
drivers/staging/media/hantro/rk3399_vpu_hw_jpeg_enc.c +7 -2
@@ -98,12 +98,17 @@ rk3399_vpu_jpeg_enc_set_qtable(struct hantro_dev *vpu, unsigned char *chroma_qtable) {u32 reg, i; +__be32 *luma_qtable_p; +__be32 *chroma_qtable_p; + + luma_qtable_p = (__be32 *)luma_qtable; + chroma_qtable_p = (__be32 *)chroma_qtable;for (i =0 ; i <VEPU_JPEG_QUANT_TABLE_COUNT ; i++) { - reg =get_unaligned_be32 (&luma_qtable[i]); + reg =get_unaligned_be32 (&luma_qtable_p[i]);vepu_write_relaxed (vpu, reg,VEPU_REG_JPEG_LUMA_QUAT (i)); - reg =get_unaligned_be32 (&chroma_qtable[i]); + reg =get_unaligned_be32 (&chroma_qtable_p[i]);vepu_write_relaxed (vpu, reg,VEPU_REG_JPEG_CHROMA_QUAT (i)); } }
Signature · 1 line
2.17.1
Re: [PATCH 1/4] media: hantro: Read be32 words starting at every fourth byte
Quoted 5 lines · Andrzej Pietrasiewicz
Since (luma/chroma)_qtable is an array of unsigned char, indexing it returns consecutive byte locations, but we are supposed to read the arrays in four-byte words. Consequently, we should be pointing
get_unaligned_be32()at consecutive word locations instead.
Ouch!
Seems we were too fast on that cleanup. Please add:
Thanks,
Ezequiel
Quoted 58 lines
Signed-off-by Andrzej Pietrasiewicz <[email protected]>--- drivers/staging/media/hantro/hantro_h1_jpeg_enc.c | 9 +++++++-- drivers/staging/media/hantro/rk3399_vpu_hw_jpeg_enc.c | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-)drivers/staging/media/hantro/hantro_h1_jpeg_enc.c +7
-2@@ -67,12 +67,17 @@ hantro_h1_jpeg_enc_set_qtable(struct hantro_dev *vpu, unsigned char *chroma_qtable) {u32 reg, i; +__be32 *luma_qtable_p; +__be32 *chroma_qtable_p; + + luma_qtable_p = (__be32 *)luma_qtable; + chroma_qtable_p = (__be32 *)chroma_qtable;for (i =0 ; i <H1_JPEG_QUANT_TABLE_COUNT ; i++) { - reg =get_unaligned_be32 (&luma_qtable[i]); + reg =get_unaligned_be32 (&luma_qtable_p[i]);vepu_write_relaxed (vpu, reg,H1_REG_JPEG_LUMA_QUAT (i)); - reg =get_unaligned_be32 (&chroma_qtable[i]); + reg =get_unaligned_be32 (&chroma_qtable_p[i]);vepu_write_relaxed (vpu, reg,H1_REG_JPEG_CHROMA_QUAT (i)); } }drivers/staging/media/hantro/rk3399_vpu_hw_jpeg_enc.c +7
-2@@ -98,12 +98,17 @@ rk3399_vpu_jpeg_enc_set_qtable(struct hantro_dev *vpu, unsigned char *chroma_qtable) {u32 reg, i; +__be32 *luma_qtable_p; +__be32 *chroma_qtable_p; + + luma_qtable_p = (__be32 *)luma_qtable; + chroma_qtable_p = (__be32 *)chroma_qtable;for (i =0 ; i <VEPU_JPEG_QUANT_TABLE_COUNT ; i++) { - reg =get_unaligned_be32 (&luma_qtable[i]); + reg =get_unaligned_be32 (&luma_qtable_p[i]);vepu_write_relaxed (vpu, reg,VEPU_REG_JPEG_LUMA_QUAT (i)); - reg =get_unaligned_be32 (&chroma_qtable[i]); + reg =get_unaligned_be32 (&chroma_qtable_p[i]);vepu_write_relaxed (vpu, reg,VEPU_REG_JPEG_CHROMA_QUAT (i)); } }Signature · 1 line
2.17.1
PATCH 2/4 media: hantro: Use standard luma quantization table
In reply to Andrzej PietrasiewiczThe table is actually different in the document than in this file, so align this file with the document.
--- drivers/staging/media/hantro/hantro_jpeg.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/staging/media/hantro/hantro_jpeg.c +8 -8
@@ -23,17 +23,17 @@#define HUFF_CHROMA_AC_OFF 409/* Default tables from JPEG ITU-T.81 -* (ISO/IEC 10918-1) Annex K.3, I +* (ISO/IEC 10918-1) Annex K, tables K.1 and K.2 */ static const unsigned char luma_q_table[] = { -0x10 ,0x0b ,0x0a ,0x10 ,0x7c ,0x8c ,0x97 ,0xa1 , -0x0c ,0x0c ,0x0e ,0x13 ,0x7e ,0x9e ,0xa0 ,0x9b , -0x0e ,0x0d ,0x10 ,0x18 ,0x8c ,0x9d ,0xa9 ,0x9c , -0x0e ,0x11 ,0x16 ,0x1d ,0x97 ,0xbb ,0xb4 ,0xa2 , -0x12 ,0x16 ,0x25 ,0x38 ,0xa8 ,0x6d ,0x67 ,0xb1 , -0x18 ,0x23 ,0x37 ,0x40 ,0xb5 ,0x68 ,0x71 ,0xc0 , +0x10 ,0x0b ,0x0a ,0x10 ,0x18 ,0x28 ,0x33 ,0x3d , +0x0c ,0x0c ,0x0e ,0x13 ,0x1a ,0x3a ,0x3c ,0x37 , +0x0e ,0x0d ,0x10 ,0x18 ,0x28 ,0x39 ,0x45 ,0x38 , +0x0e ,0x11 ,0x16 ,0x1d ,0x33 ,0x57 ,0x50 ,0x3e , +0x12 ,0x16 ,0x25 ,0x38 ,0x44 ,0x6d ,0x67 ,0x4d , +0x18 ,0x23 ,0x37 ,0x40 ,0x51 ,0x68 ,0x71 ,0x5c ,0x31 ,0x40 ,0x4e ,0x57 ,0x67 ,0x79 ,0x78 ,0x65 , -0x48 ,0x5c ,0x5f ,0x62 ,0x70 ,0x64 ,0x67 ,0xc7 , +0x48 ,0x5c ,0x5f ,0x62 ,0x70 ,0x64 ,0x67 ,0x63 };static const unsigned char chroma_q_table[] = {
Signature · 1 line
2.17.1
Re: [PATCH 2/4] media: hantro: Use standard luma quantization table
Hi Andrzej,
Quoted 13 lines · Andrzej Pietrasiewicz
The table is actually different in the document than in this file, so align this file with the document.
Signed-off-by Andrzej Pietrasiewicz <[email protected]>--- drivers/staging/media/hantro/hantro_jpeg.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)drivers/staging/media/hantro/hantro_jpeg.c +0
-0
drivers/staging/media/hantro/hantro_jpeg.c line 27
Quoted 6 lines
drivers/staging/media/hantro/hantro_jpeg.c +1
-1@@ -23,17 +23,17 @@#define HUFF_CHROMA_AC_OFF 409/* Default tables from JPEG ITU-T.81 -* (ISO/IEC 10918-1) Annex K.3, I +* (ISO/IEC 10918-1) Annex K, tables K.1 and K.2 */
I wonder if we shouldn't just have these tables
in decimal instead of hexa, so they look exactly
like the ones in the spec.
Thanks,
Ezequiel
Quoted 23 lines
drivers/staging/media/hantro/hantro_jpeg.c +7
-7static const unsigned char luma_q_table[] = { -0x10 ,0x0b ,0x0a ,0x10 ,0x7c ,0x8c ,0x97 ,0xa1 , -0x0c ,0x0c ,0x0e ,0x13 ,0x7e ,0x9e ,0xa0 ,0x9b , -0x0e ,0x0d ,0x10 ,0x18 ,0x8c ,0x9d ,0xa9 ,0x9c , -0x0e ,0x11 ,0x16 ,0x1d ,0x97 ,0xbb ,0xb4 ,0xa2 , -0x12 ,0x16 ,0x25 ,0x38 ,0xa8 ,0x6d ,0x67 ,0xb1 , -0x18 ,0x23 ,0x37 ,0x40 ,0xb5 ,0x68 ,0x71 ,0xc0 , +0x10 ,0x0b ,0x0a ,0x10 ,0x18 ,0x28 ,0x33 ,0x3d , +0x0c ,0x0c ,0x0e ,0x13 ,0x1a ,0x3a ,0x3c ,0x37 , +0x0e ,0x0d ,0x10 ,0x18 ,0x28 ,0x39 ,0x45 ,0x38 , +0x0e ,0x11 ,0x16 ,0x1d ,0x33 ,0x57 ,0x50 ,0x3e , +0x12 ,0x16 ,0x25 ,0x38 ,0x44 ,0x6d ,0x67 ,0x4d , +0x18 ,0x23 ,0x37 ,0x40 ,0x51 ,0x68 ,0x71 ,0x5c ,0x31 ,0x40 ,0x4e ,0x57 ,0x67 ,0x79 ,0x78 ,0x65 , -0x48 ,0x5c ,0x5f ,0x62 ,0x70 ,0x64 ,0x67 ,0xc7 , +0x48 ,0x5c ,0x5f ,0x62 ,0x70 ,0x64 ,0x67 ,0x63 };static const unsigned char chroma_q_table[] = {Signature · 1 line
2.17.1
PATCH 3/4 media: hantro: Write the quantization tables in proper order
In reply to Andrzej PietrasiewiczThe quantization tables as defined in the file (luma_q_table, chroma_q_table) are in fact in linear order. The JPEG file header, which is not generated by the hardware, but must be programatically created with the CPU, expects the table in zigzag order. On the other hand, the hardware doesn't expect neither linear, nor zigzag order. Instead it expects the quantization tables in vertical groups of four quantization parameters, and the groups are organized in blocks of two vertically adjacent groups. On top of that the blocks must be provided to the hardware in this order:
leftmost top block, leftmost bottom block, second leftmost top block, second leftmost bottom block and so on. So, if this is the quantization table in linear order:
0x10, 0x0b, 0x0a, 0x10, 0x18, 0x28, 0x33, 0x3d,0x0c, 0x0c, 0x0e, 0x13, 0x1a, 0x3a, 0x3c, 0x37,0x0e, 0x0d, 0x10, 0x18, 0x28, 0x39, 0x45, 0x38,0x0e, 0x11, 0x16, 0x1d, 0x33, 0x57, 0x50, 0x3e,0x12, 0x16, 0x25, 0x38, 0x44, 0x6d, 0x67, 0x4d,0x18, 0x23, 0x37, 0x40, 0x51, 0x68, 0x71, 0x5c,0x31, 0x40, 0x4e, 0x57, 0x67, 0x79, 0x78, 0x65,0x48, 0x5c, 0x5f, 0x62, 0x70, 0x64, 0x67, 0x63
then the hardware expects this in its consecutive registers:
0x100c0e0e,0x0b0c0d11,0x12183148,0x1623405c,0x0a0e1016,0x1013181d,0x25374e5f,0x38405762,
and so on.
Consequently, the same area of memory cannot be used both for dumping it into the JPEG file header and writing its contents to the hardware registers. Instead, a separate pair of arrays is added for properly reordered quantization tables, to be read with get_unaligned_be32() and linearly written to the registers.
The "ctx" parameter is not needed any more for hantro_jpeg_get_qtable().
--- .../staging/media/hantro/hantro_h1_jpeg_enc.c | 4 +- drivers/staging/media/hantro/hantro_jpeg.c | 60 +++++++++++++++---- drivers/staging/media/hantro/hantro_jpeg.h | 2 +- .../media/hantro/rk3399_vpu_hw_jpeg_enc.c | 9 ++- 4 files changed, 55 insertions(+), 20 deletions(-)
drivers/staging/media/hantro/hantro_h1_jpeg_enc.c +2 -2
@@ -108,8 +108,8 @@ void hantro_h1_jpeg_enc_run(struct hantro_ctx *ctx)hantro_h1_set_src_img_ctrl (vpu, ctx);hantro_h1_jpeg_enc_set_buffers (vpu, ctx, &src_buf->vb2_buf);hantro_h1_jpeg_enc_set_qtable (vpu, -hantro_jpeg_get_qtable (&jpeg_ctx,0 ), -hantro_jpeg_get_qtable (&jpeg_ctx,1 )); +hantro_jpeg_get_qtable (0 ), +hantro_jpeg_get_qtable (1 )); reg =H1_REG_AXI_CTRL_OUTPUT_SWAP16 |H1_REG_AXI_CTRL_INPUT_SWAP16
drivers/staging/media/hantro/hantro_jpeg.c +48 -12
97 lines not on this page · raw message
drivers/staging/media/hantro/hantro_jpeg.h +1 -1
@@ -9,5 +9,5 @@ struct hantro_jpeg_ctx {unsigned char *buffer; }; -unsigned char *hantro_jpeg_get_qtable (struct hantro_jpeg_ctx *ctx,int index); +unsigned char *hantro_jpeg_get_qtable (int index);void hantro_jpeg_header_assemble (struct hantro_jpeg_ctx *ctx);
drivers/staging/media/hantro/rk3399_vpu_hw_jpeg_enc.c +4 -5
@@ -18,9 +18,8 @@* * Quantization luma table values are written to registers * VEPU_swreg_0-VEPU_swreg_15, and chroma table values to -* VEPU_swreg_16-VEPU_swreg_31. -* -* JPEG zigzag order is expected on the quantization tables. +* VEPU_swreg_16-VEPU_swreg_31. A special order is needed, neither +* zigzag, nor linear. */ #include <asm/unaligned.h>
@@ -139,8 +138,8 @@ void rk3399_vpu_jpeg_enc_run(struct hantro_ctx *ctx)rk3399_vpu_set_src_img_ctrl (vpu, ctx);rk3399_vpu_jpeg_enc_set_buffers (vpu, ctx, &src_buf->vb2_buf);rk3399_vpu_jpeg_enc_set_qtable (vpu, -hantro_jpeg_get_qtable (&jpeg_ctx,0 ), -hantro_jpeg_get_qtable (&jpeg_ctx,1 )); +hantro_jpeg_get_qtable (0 ), +hantro_jpeg_get_qtable (1 )); reg =VEPU_REG_OUTPUT_SWAP32 |VEPU_REG_OUTPUT_SWAP16
Signature · 1 line
2.17.1
PATCH 4/4 media: hantro: Write quantization table registers in increasing addresses order
In reply to Andrzej PietrasiewiczLuma and chroma qtables need to be written into two 16-register blocks, each table consisting of 64 bytes total. The blocks are contiguous and start at offset 0 for luma and at offset 0x40 for chroma.
The seemingly innocent optimization of writing the two blocks using one loop causes side effects which result in improper values of quantization tables being used by the hardware during encoding. Visually this results in macroblocking artifacts around contrasting edges in encoded images. The artifacts look like horizontally flipped shadows of the said edges. Changing the write operations to non-relaxed variant doesn't help.
This patch removes this premature optimization and after this change the macroblocking artifacts around contrasting edges are gone.
--- drivers/staging/media/hantro/hantro_h1_jpeg_enc.c | 6 ++++++ drivers/staging/media/hantro/rk3399_vpu_hw_jpeg_enc.c | 6 ++++++ 2 files changed, 12 insertions(+)
drivers/staging/media/hantro/hantro_h1_jpeg_enc.c +6 -0
@@ -73,10 +73,16 @@ hantro_h1_jpeg_enc_set_qtable(struct hantro_dev *vpu, luma_qtable_p = (__be32 *)luma_qtable; chroma_qtable_p = (__be32 *)chroma_qtable; +/* +* Quantization table registers must be written in contiguous blocks. +* DO NOT collapse the below two "for" loops into one. +*/ for (i =0 ; i <H1_JPEG_QUANT_TABLE_COUNT ; i++) { reg =get_unaligned_be32 (&luma_qtable_p[i]);vepu_write_relaxed (vpu, reg,H1_REG_JPEG_LUMA_QUAT (i)); + } +for (i =0 ; i <H1_JPEG_QUANT_TABLE_COUNT ; i++) { reg =get_unaligned_be32 (&chroma_qtable_p[i]);vepu_write_relaxed (vpu, reg,H1_REG_JPEG_CHROMA_QUAT (i)); }
drivers/staging/media/hantro/rk3399_vpu_hw_jpeg_enc.c +6 -0
@@ -103,10 +103,16 @@ rk3399_vpu_jpeg_enc_set_qtable(struct hantro_dev *vpu, luma_qtable_p = (__be32 *)luma_qtable; chroma_qtable_p = (__be32 *)chroma_qtable; +/* +* Quantization table registers must be written in contiguous blocks. +* DO NOT collapse the below two "for" loops into one. +*/ for (i =0 ; i <VEPU_JPEG_QUANT_TABLE_COUNT ; i++) { reg =get_unaligned_be32 (&luma_qtable_p[i]);vepu_write_relaxed (vpu, reg,VEPU_REG_JPEG_LUMA_QUAT (i)); + } +for (i =0 ; i <VEPU_JPEG_QUANT_TABLE_COUNT ; i++) { reg =get_unaligned_be32 (&chroma_qtable_p[i]);vepu_write_relaxed (vpu, reg,VEPU_REG_JPEG_CHROMA_QUAT (i)); }
Signature · 1 line
2.17.1
Hi Andrzej,
Thanks a lot for the fixes!
Quoted 17 lines · Andrzej Pietrasiewicz
This series addresses quality issues in encoded JPEG images.
The first patch actually restores the intention of the original submission of this driver: due to a typo the helper variables were unused and then have been removed in some cleanup done by Mauro.
The second patch aligns the driver's luma quantization table with the one in the ITU-T.81 standard.
The third patch changes the order in which quantization tables are written to the resulting file and to the hardware. The file expects a zig-zag order, while the hardware wants some special order, neither linear nor zig-zag. In other words, hardware-wise it rearranges which parts of quantization tables go into which 4-byte registers - in a hardware specific order rather than linear or zig-zag. It also affects rk3288 and hasn't been tested with it.
Quoted 7 lines
The fourth patch then rearranges the sequence of register writes. The whole luma quantization table must be written first, and then the chroma quantization is written. In other words, while patch 3/4 changes what goes into which register, this patch changes when each register is written to. It also affects rk3288 and hasn't been tested with it.
I've just tested RK3288, and this series is indeed fixing these issues. So for all patches:
Thanks,
Ezequiel
Quoted 17 lines
Andrzej Pietrasiewicz (4):
media: hantro: Read be32 words starting at every fourth byte
media: hantro: Use standard luma quantization table
media: hantro: Write the quantization tables in proper order
media: hantro: Write quantization table registers in increasing
addresses order.../staging/media/hantro/hantro_h1_jpeg_enc.c | 19 ++++- drivers/staging/media/hantro/hantro_jpeg.c | 76 ++++++++++++++----- drivers/staging/media/hantro/hantro_jpeg.h | 2 +- .../media/hantro/rk3399_vpu_hw_jpeg_enc.c | 24 ++++-- 4 files changed, 89 insertions(+), 32 deletions(-)Signature · 1 line
2.17.1
Hi All,
<snip>
Quoted 4 lines
I've just tested RK3288, and this series is indeed fixing these issues. So for all patches:
Tested-by Ezequiel Garcia <[email protected]>
A kind reminder.
The series fixes serious encoding quality problems in both rk3399 and rk3288, so it seems it should be included. A review is needed, though, at least for patches 2-4.
Thank you,
Andrzej