#include "config-host.h"
+/* This needs to be before jpeglib.h line because of conflict with
+ INT32 definitions between jmorecfg.h (included by jpeglib.h) and
+ Win32 basetsd.h (included by windows.h). */
+#include "qemu-common.h"
+
#ifdef CONFIG_VNC_PNG
+/* The following define is needed by pngconf.h. Otherwise it won't compile,
+ because setjmp.h was already included by qemu-common.h. */
+#define PNG_SKIP_SETJMP_CHECK
#include <png.h>
#endif
#ifdef CONFIG_VNC_JPEG
#include <jpeglib.h>
#endif
-#include "qemu-common.h"
-
#include "bswap.h"
#include "qint.h"
#include "vnc.h"
static int tight_send_framebuffer_update(VncState *vs, int x, int y,
int w, int h);
+#ifdef CONFIG_VNC_JPEG
+static const struct {
+ double jpeg_freq_min; /* Don't send JPEG if the freq is bellow */
+ double jpeg_freq_threshold; /* Always send JPEG if the freq is above */
+ int jpeg_idx; /* Allow indexed JPEG */
+ int jpeg_full; /* Allow full color JPEG */
+} tight_jpeg_conf[] = {
+ { 0, 8, 1, 1 },
+ { 0, 8, 1, 1 },
+ { 0, 8, 1, 1 },
+ { 0, 8, 1, 1 },
+ { 0, 10, 1, 1 },
+ { 0.1, 10, 1, 1 },
+ { 0.2, 10, 1, 1 },
+ { 0.3, 12, 0, 0 },
+ { 0.4, 14, 0, 0 },
+ { 0.5, 16, 0, 0 },
+};
+#endif
+
#ifdef CONFIG_VNC_PNG
static const struct {
int png_zlib_level, png_filters;
* compression (by applying "gradient" filter or JPEG coder).
*/
-static uint
+static unsigned int
tight_detect_smooth_image24(VncState *vs, int w, int h)
{
int off;
int x, y, d, dx;
- uint c;
- uint stats[256];
+ unsigned int c;
+ unsigned int stats[256];
int pixels = 0;
int pix, left[3];
- uint errors;
+ unsigned int errors;
unsigned char *buf = vs->tight.tight.buffer;
/*
#define DEFINE_DETECT_FUNCTION(bpp) \
\
- static uint \
+ static unsigned int \
tight_detect_smooth_image##bpp(VncState *vs, int w, int h) { \
bool endian; \
uint##bpp##_t pix; \
int max[3], shift[3]; \
int x, y, d, dx; \
- uint c; \
- uint stats[256]; \
+ unsigned int c; \
+ unsigned int stats[256]; \
int pixels = 0; \
int sample, sum, left[3]; \
- uint errors; \
+ unsigned int errors; \
unsigned char *buf = vs->tight.tight.buffer; \
\
endian = ((vs->clientds.flags & QEMU_BIG_ENDIAN_FLAG) != \
d < w - x - VNC_TIGHT_DETECT_SUBROW_WIDTH; d++) { \
pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d]; \
if (endian) { \
- pix = bswap_##bpp(pix); \
+ pix = bswap##bpp(pix); \
} \
for (c = 0; c < 3; c++) { \
left[c] = (int)(pix >> shift[c] & max[c]); \
dx++) { \
pix = ((uint##bpp##_t *)buf)[(y+d)*w+x+d+dx]; \
if (endian) { \
- pix = bswap_##bpp(pix); \
+ pix = bswap##bpp(pix); \
} \
sum = 0; \
for (c = 0; c < 3; c++) { \
static int
tight_detect_smooth_image(VncState *vs, int w, int h)
{
- uint errors;
+ unsigned int errors;
int compression = vs->tight.compression;
int quality = vs->tight.quality;
return 0;
}
- if (vs->tight.quality != -1) {
+ if (vs->tight.quality != (uint8_t)-1) {
if (w * h < VNC_TIGHT_JPEG_MIN_RECT_SIZE) {
return 0;
}
if (vs->clientds.pf.bytes_per_pixel == 4) {
if (vs->tight.pixel24) {
errors = tight_detect_smooth_image24(vs, w, h);
- if (vs->tight.quality != -1) {
+ if (vs->tight.quality != (uint8_t)-1) {
return (errors < tight_conf[quality].jpeg_threshold24);
}
return (errors < tight_conf[compression].gradient_threshold24);
* Should never happen, but don't break everything \
* if it does, use the first color instead \
*/ \
- if (idx == -1) { \
+ if (idx == (uint8_t)-1) { \
idx = 0; \
} \
while (rep >= 0) { \
for (x = 0; x < w; x++) { \
pix = *buf; \
if (endian) { \
- pix = bswap_##bpp(pix); \
+ pix = bswap##bpp(pix); \
} \
diff = 0; \
for (c = 0; c < 3; c++) { \
<< shift[c]; \
} \
if (endian) { \
- diff = bswap_##bpp(diff); \
+ diff = bswap##bpp(diff); \
} \
*buf++ = diff; \
} \
/*
* Check if a rectangle is all of the same color. If needSameColor is
* set to non-zero, then also check that its color equals to the
- * *colorPtr value. The result is 1 if the test is successfull, and in
+ * *colorPtr value. The result is 1 if the test is successful, and in
* that case new color will be stored in *colorPtr.
*/
zstream->avail_in = vs->tight.tight.offset;
zstream->next_out = vs->tight.zlib.buffer + vs->tight.zlib.offset;
zstream->avail_out = vs->tight.zlib.capacity - vs->tight.zlib.offset;
+ previous_out = zstream->avail_out;
zstream->data_type = Z_BINARY;
- previous_out = zstream->total_out;
/* start encoding */
if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
}
vs->tight.zlib.offset = vs->tight.zlib.capacity - zstream->avail_out;
- bytes = zstream->total_out - previous_out;
+ /* ...how much data has actually been produced by deflate() */
+ bytes = previous_out - zstream->avail_out;
tight_send_compact_size(vs, bytes);
vnc_write(vs, vs->tight.zlib.buffer, bytes);
static int send_full_color_rect(VncState *vs, int x, int y, int w, int h)
{
int stream = 0;
- size_t bytes;
+ ssize_t bytes;
#ifdef CONFIG_VNC_PNG
if (tight_can_send_png_rect(vs, w, h)) {
static int send_mono_rect(VncState *vs, int x, int y,
int w, int h, uint32_t bg, uint32_t fg)
{
- size_t bytes;
+ ssize_t bytes;
int stream = 1;
int level = tight_conf[vs->tight.compression].mono_zlib_level;
{
int stream = 3;
int level = tight_conf[vs->tight.compression].gradient_zlib_level;
- size_t bytes;
+ ssize_t bytes;
if (vs->clientds.pf.bytes_per_pixel == 1)
return send_full_color_rect(vs, x, y, w, h);
int stream = 2;
int level = tight_conf[vs->tight.compression].idx_zlib_level;
int colors;
- size_t bytes;
+ ssize_t bytes;
#ifdef CONFIG_VNC_PNG
if (tight_can_send_png_rect(vs, w, h)) {
jpeg_start_compress(&cinfo, true);
- buf = qemu_malloc(w * 3);
+ buf = g_malloc(w * 3);
row[0] = buf;
for (dy = 0; dy < h; dy++) {
rgb_prepare_row(vs, buf, x, y + dy, w);
jpeg_write_scanlines(&cinfo, row, 1);
}
- qemu_free(buf);
+ g_free(buf);
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
static void *vnc_png_malloc(png_structp png_ptr, png_size_t size)
{
- return qemu_malloc(size);
+ return g_malloc(size);
}
static void vnc_png_free(png_structp png_ptr, png_voidp ptr)
{
- qemu_free(ptr);
+ g_free(ptr);
}
static int send_png_rect(VncState *vs, int x, int y, int w, int h,
png_structp png_ptr;
png_infop info_ptr;
png_colorp png_palette = NULL;
- size_t offset;
int level = tight_png_conf[vs->tight.compression].png_zlib_level;
int filters = tight_png_conf[vs->tight.compression].png_filters;
uint8_t *buf;
png_set_PLTE(png_ptr, info_ptr, png_palette, palette_size(palette));
- offset = vs->tight.tight.offset;
if (vs->clientds.pf.bytes_per_pixel == 4) {
tight_encode_indexed_rect32(vs->tight.tight.buffer, w * h, palette);
} else {
png_write_info(png_ptr, info_ptr);
buffer_reserve(&vs->tight.png, 2048);
- buf = qemu_malloc(w * 3);
+ buf = g_malloc(w * 3);
for (dy = 0; dy < h; dy++)
{
if (color_type == PNG_COLOR_TYPE_PALETTE) {
}
png_write_row(png_ptr, buf);
}
- qemu_free(buf);
+ g_free(buf);
png_write_end(png_ptr, NULL);
ret = send_mono_rect(vs, x, y, w, h, bg, fg);
} else if (colors <= 256) {
ret = send_palette_rect(vs, x, y, w, h, palette);
+ } else {
+ ret = 0;
}
return ret;
}
#ifdef CONFIG_VNC_JPEG
static int send_sub_rect_jpeg(VncState *vs, int x, int y, int w, int h,
int bg, int fg, int colors,
- VncPalette *palette)
+ VncPalette *palette, bool force)
{
int ret;
if (colors == 0) {
- if (tight_detect_smooth_image(vs, w, h)) {
+ if (force || (tight_jpeg_conf[vs->tight.quality].jpeg_full &&
+ tight_detect_smooth_image(vs, w, h))) {
int quality = tight_conf[vs->tight.quality].jpeg_quality;
ret = send_jpeg_rect(vs, x, y, w, h, quality);
} else if (colors == 2) {
ret = send_mono_rect(vs, x, y, w, h, bg, fg);
} else if (colors <= 256) {
- if (colors > 96 &&
- tight_detect_smooth_image(vs, w, h)) {
+ if (force || (colors > 96 &&
+ tight_jpeg_conf[vs->tight.quality].jpeg_idx &&
+ tight_detect_smooth_image(vs, w, h))) {
int quality = tight_conf[vs->tight.quality].jpeg_quality;
ret = send_jpeg_rect(vs, x, y, w, h, quality);
} else {
ret = send_palette_rect(vs, x, y, w, h, palette);
}
+ } else {
+ ret = 0;
}
return ret;
}
uint32_t bg = 0, fg = 0;
int colors;
int ret = 0;
+#ifdef CONFIG_VNC_JPEG
+ bool force_jpeg = false;
+ bool allow_jpeg = true;
+#endif
vnc_framebuffer_update(vs, x, y, w, h, vs->tight.type);
vnc_raw_send_framebuffer_update(vs, x, y, w, h);
vnc_tight_stop(vs);
+#ifdef CONFIG_VNC_JPEG
+ if (!vs->vd->non_adaptive && vs->tight.quality != (uint8_t)-1) {
+ double freq = vnc_update_freq(vs, x, y, w, h);
+
+ if (freq < tight_jpeg_conf[vs->tight.quality].jpeg_freq_min) {
+ allow_jpeg = false;
+ }
+ if (freq >= tight_jpeg_conf[vs->tight.quality].jpeg_freq_threshold) {
+ force_jpeg = true;
+ vnc_sent_lossy_rect(vs, x, y, w, h);
+ }
+ }
+#endif
+
colors = tight_fill_palette(vs, x, y, w * h, &fg, &bg, &palette);
#ifdef CONFIG_VNC_JPEG
- if (vs->tight.quality != -1) {
- ret = send_sub_rect_jpeg(vs, x, y, w, h, bg, fg, colors, palette);
+ if (allow_jpeg && vs->tight.quality != (uint8_t)-1) {
+ ret = send_sub_rect_jpeg(vs, x, y, w, h, bg, fg, colors, palette,
+ force_jpeg);
} else {
ret = send_sub_rect_nojpeg(vs, x, y, w, h, bg, fg, colors, palette);
}
return send_solid_rect(vs);
}
-static int send_rect_simple(VncState *vs, int x, int y, int w, int h)
+static int send_rect_simple(VncState *vs, int x, int y, int w, int h,
+ bool split)
{
int max_size, max_width;
int max_sub_width, max_sub_height;
max_size = tight_conf[vs->tight.compression].max_rect_size;
max_width = tight_conf[vs->tight.compression].max_rect_width;
- if (w > max_width || w * h > max_size) {
+ if (split && (w > max_width || w * h > max_size)) {
max_sub_width = (w > max_width) ? max_width : w;
max_sub_height = max_size / max_sub_width;
/* If a rectangle becomes too large, send its upper part now. */
if (dy - y >= max_rows) {
- n += send_rect_simple(vs, x, y, w, max_rows);
+ n += send_rect_simple(vs, x, y, w, max_rows, true);
y += max_rows;
h -= max_rows;
}
/* Send rectangles at top and left to solid-color area. */
if (y_best != y) {
- n += send_rect_simple(vs, x, y, w, y_best-y);
+ n += send_rect_simple(vs, x, y, w, y_best-y, true);
}
if (x_best != x) {
n += tight_send_framebuffer_update(vs, x, y_best,
return n;
}
}
- return n + send_rect_simple(vs, x, y, w, h);
+ return n + send_rect_simple(vs, x, y, w, h, true);
}
static int tight_send_framebuffer_update(VncState *vs, int x, int y,
vs->tight.pixel24 = false;
}
- if (w * h < VNC_TIGHT_MIN_SPLIT_RECT_SIZE)
- return send_rect_simple(vs, x, y, w, h);
+#ifdef CONFIG_VNC_JPEG
+ if (vs->tight.quality != (uint8_t)-1) {
+ double freq = vnc_update_freq(vs, x, y, w, h);
+
+ if (freq > tight_jpeg_conf[vs->tight.quality].jpeg_freq_threshold) {
+ return send_rect_simple(vs, x, y, w, h, false);
+ }
+ }
+#endif
+
+ if (w * h < VNC_TIGHT_MIN_SPLIT_RECT_SIZE) {
+ return send_rect_simple(vs, x, y, w, h, true);
+ }
/* Calculate maximum number of rows in one non-solid rectangle. */