[PATCH v1] jpeg: optimize compression process

Frediano Ziglio freddy77 at gmail.com
Sun Mar 2 17:24:52 UTC 2025


On Fri, Feb 28, 2025 at 9:53 PM <liweishi at kylinos.cn> wrote:

> From: Weishi Li <liweishi at kylinos.cn>
>
> similiar to mjpeg, when defined JCS_EXTENSIONS, there
> is no need to convert BGR24/BGRX32 to RGB24.
>
> Signed-off-by: Weishi Li <liweishi at kylinos.cn>
> ---
>  server/jpeg-encoder.c | 42 +++++++++++++++++++++++++++++++++++++-----
>  1 file changed, 37 insertions(+), 5 deletions(-)
>
> diff --git a/server/jpeg-encoder.c b/server/jpeg-encoder.c
> index fee07105..fe1038f3 100644
> --- a/server/jpeg-encoder.c
> +++ b/server/jpeg-encoder.c
> @@ -27,6 +27,16 @@
>  #include "red-common.h"
>  #include "jpeg-encoder.h"
>
> +#ifdef JCS_EXTENSIONS
> +#  ifndef WORDS_BIGENDIAN
> +#    define JCS_EXT_LE_BGRX JCS_EXT_BGRX
> +#    define JCS_EXT_LE_BGR JCS_EXT_BGR
> +#  else
> +#    define JCS_EXT_LE_BGRX JCS_EXT_XRGB
> +#    define JCS_EXT_LE_BGR JCS_EXT_RGB
> +#  endif
> +#endif
> +
>  struct JpegEncoderContext {
>      JpegEncoderUsrContext *usr;
>
> @@ -130,6 +140,7 @@ static void convert_RGB16_to_RGB24(void *line, int
> width, uint8_t **out_line)
>     }
>  }
>
> +#ifndef JCS_EXTENSIONS
>  static void convert_BGR24_to_RGB24(void *in_line, int width, uint8_t
> **out_line)
>  {
>      int x;
> @@ -165,7 +176,7 @@ static void convert_BGRX32_to_RGB24(void *line, int
> width, uint8_t **out_line)
>          *out_pix++ = pixel & 0xff;
>      }
>  }
> -
> +#endif
>
>  #define FILL_LINES() {                                                  \
>      if (lines == lines_end) {                                           \
> @@ -177,7 +188,7 @@ static void convert_BGRX32_to_RGB24(void *line, int
> width, uint8_t **out_line)
>      }                                                                   \
>  }
>
> -static void do_jpeg_encode(JpegEncoder *jpeg, uint8_t *lines, unsigned
> int num_lines)
> +static void do_jpeg_encode(JpegEncoder *jpeg, uint8_t *lines, unsigned
> int num_lines, JpegEncoderImageType type)
>  {
>      uint8_t *lines_end;
>      uint8_t *RGB24_line;
> @@ -186,10 +197,21 @@ static void do_jpeg_encode(JpegEncoder *jpeg,
> uint8_t *lines, unsigned int num_l
>      width = jpeg->cur_image.width;
>      stride = jpeg->cur_image.stride;
>
> -    RGB24_line = g_new(uint8_t, width*3);
> -
>      lines_end = lines + (stride * num_lines);
>
> +#ifdef JCS_EXTENSIONS
> +    if (type == JPEG_IMAGE_TYPE_BGR24 || type == JPEG_IMAGE_TYPE_BGRX32) {
>

Why not check if jpeg->cur_image.convert_line_to_RGB24 is NULL instead ? It
would remove the need to pass the type and reduce the difference.


> +        for (;jpeg->cinfo.next_scanline < jpeg->cinfo.image_height; lines
> += stride) {
> +            FILL_LINES();
> +            row_pointer[0] = lines;
> +            jpeg_write_scanlines(&jpeg->cinfo, row_pointer, 1);
> +        }
> +        return;
> +    }
> +#endif
> +
> +    RGB24_line = g_new(uint8_t, width*3);
> +
>      for (;jpeg->cinfo.next_scanline < jpeg->cinfo.image_height; lines +=
> stride) {
>          FILL_LINES();
>          jpeg->cur_image.convert_line_to_RGB24(lines, width, &RGB24_line);
> @@ -215,10 +237,20 @@ int jpeg_encode(JpegEncoderContext *enc, int
> quality, JpegEncoderImageType type,
>          enc->cur_image.convert_line_to_RGB24 = convert_RGB16_to_RGB24;
>          break;
>      case JPEG_IMAGE_TYPE_BGR24:
> +#ifdef JCS_EXTENSIONS
> +        enc->cinfo.in_color_space   = JCS_EXT_LE_BGR;
> +        enc->cinfo.input_components = 3;
> +#else
>          enc->cur_image.convert_line_to_RGB24 = convert_BGR24_to_RGB24;
> +#endif
>          break;
>      case JPEG_IMAGE_TYPE_BGRX32:
> +#ifdef JCS_EXTENSIONS
> +        enc->cinfo.in_color_space = JCS_EXT_LE_BGRX;
> +        enc->cinfo.input_components = 4;
> +#else
>          enc->cur_image.convert_line_to_RGB24 = convert_BGRX32_to_RGB24;
> +#endif
>          break;
>      default:
>          spice_error("bad image type");
> @@ -236,7 +268,7 @@ int jpeg_encode(JpegEncoderContext *enc, int quality,
> JpegEncoderImageType type,
>
>      jpeg_start_compress(&enc->cinfo, TRUE);
>
> -    do_jpeg_encode(enc, lines, num_lines);
> +    do_jpeg_encode(enc, lines, num_lines, type);
>
>      jpeg_finish_compress(&enc->cinfo);
>      return enc->cur_image.out_size;
>

Frediano
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/spice-devel/attachments/20250302/2a15a66c/attachment.htm>


More information about the Spice-devel mailing list