neatvnc/src/zrle.c

465 lines
12 KiB
C
Raw Normal View History

2019-09-07 16:51:07 +00:00
/*
2022-04-14 16:56:41 +00:00
* Copyright (c) 2019 - 2022 Andri Yngvason
2019-09-07 16:51:07 +00:00
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include "rfb-proto.h"
2019-08-25 16:40:59 +00:00
#include "vec.h"
#include "neatvnc.h"
#include "pixels.h"
2019-10-07 17:39:54 +00:00
#include "fb.h"
2020-09-26 16:16:36 +00:00
#include "enc-util.h"
2021-12-09 22:48:31 +00:00
#include "encoder.h"
#include "rcbuf.h"
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
2019-09-07 18:42:55 +00:00
#include <stdbool.h>
#include <assert.h>
#include <pixman.h>
#include <zlib.h>
2021-12-09 22:48:31 +00:00
#include <aml.h>
#define TILE_LENGTH 64
2019-09-07 18:42:55 +00:00
#define UDIV_UP(a, b) (((a) + (b) - 1) / (b))
2019-08-17 20:49:11 +00:00
2021-12-09 22:48:31 +00:00
struct encoder* zrle_encoder_new(void);
struct zrle_encoder {
struct encoder encoder;
struct rfb_pixel_format output_format;
struct nvnc_fb* current_fb;
struct pixman_region16 current_damage;
struct rcbuf *current_result;
z_stream zs;
struct aml_work* work;
};
struct encoder_impl encoder_impl_zrle;
static inline struct zrle_encoder* zrle_encoder(struct encoder* encoder)
{
assert(encoder->impl == &encoder_impl_zrle);
return (struct zrle_encoder*)encoder;
}
2019-10-20 22:13:51 +00:00
static inline int find_colour_in_palette(uint32_t* palette, int len,
uint32_t colour)
2019-09-01 22:56:59 +00:00
{
for (int i = 0; i < len; ++i)
if (palette[i] == colour)
return i;
return -1;
}
static int zrle_get_tile_palette(uint32_t* palette, const uint32_t* src,
size_t length)
2019-09-01 22:56:59 +00:00
{
int n = 0;
/* TODO: Maybe ignore the alpha channel */
palette[n++] = src[0];
for (size_t i = 0; i < length; ++i) {
uint32_t colour = src[i];
2019-09-01 22:56:59 +00:00
if (find_colour_in_palette(palette, n, colour) < 0) {
if (n >= 16)
return -1;
2019-09-01 22:56:59 +00:00
palette[n++] = colour;
2019-09-01 22:56:59 +00:00
}
}
2019-09-01 22:56:59 +00:00
return n;
}
static void zrle_encode_unichrome_tile(struct vec* dst,
const struct rfb_pixel_format* dst_fmt,
uint32_t colour,
const struct rfb_pixel_format* src_fmt)
2019-09-01 22:56:59 +00:00
{
int bytes_per_cpixel = calc_bytes_per_cpixel(dst_fmt);
2019-09-01 22:56:59 +00:00
vec_fast_append_8(dst, 1);
pixel_to_cpixel(((uint8_t*)dst->data) + 1, dst_fmt, (uint8_t*)&colour, src_fmt,
2019-10-20 22:13:51 +00:00
bytes_per_cpixel, 1);
2019-09-01 22:56:59 +00:00
dst->len += bytes_per_cpixel;
}
static void encode_run_length(struct vec* dst, uint8_t index, int run_length)
2019-09-01 22:56:59 +00:00
{
if (run_length == 1) {
vec_fast_append_8(dst, index);
return;
}
vec_fast_append_8(dst, index | 128);
while (run_length > 255) {
vec_fast_append_8(dst, 255);
run_length -= 255;
}
vec_fast_append_8(dst, run_length - 1);
}
static void zrle_encode_packed_tile(struct vec* dst,
const struct rfb_pixel_format* dst_fmt,
const uint32_t* src,
const struct rfb_pixel_format* src_fmt,
size_t length, uint32_t* palette,
int palette_size)
2019-09-01 22:56:59 +00:00
{
int bytes_per_cpixel = calc_bytes_per_cpixel(dst_fmt);
2019-09-01 22:56:59 +00:00
uint8_t cpalette[16 * 3];
pixel_to_cpixel((uint8_t*)cpalette, dst_fmt, (uint8_t*)palette, src_fmt,
2019-10-20 22:13:51 +00:00
bytes_per_cpixel, palette_size);
2019-09-01 22:56:59 +00:00
vec_fast_append_8(dst, 128 | palette_size);
2019-09-01 22:56:59 +00:00
vec_append(dst, cpalette, palette_size * bytes_per_cpixel);
int index;
int run_length = 1;
2019-09-01 22:56:59 +00:00
for (size_t i = 1; i < length; ++i) {
if (src[i] == src[i - 1]) {
run_length++;
continue;
}
2019-09-01 22:56:59 +00:00
2019-10-20 22:13:51 +00:00
index = find_colour_in_palette(palette, palette_size, src[i - 1]);
encode_run_length(dst, index, run_length);
run_length = 1;
}
2019-09-01 22:56:59 +00:00
if (run_length > 0) {
index = find_colour_in_palette(palette, palette_size,
2019-10-20 22:13:51 +00:00
src[length - 1]);
2019-09-01 22:56:59 +00:00
encode_run_length(dst, index, run_length);
}
}
static void zrle_copy_tile(uint32_t* dst, const uint32_t* src, int stride,
int width, int height)
{
2019-10-08 17:58:35 +00:00
for (int y = 0; y < height; ++y)
memcpy(dst + y * width, src + y * stride, width * 4);
}
static void zrle_encode_tile(struct vec* dst,
const struct rfb_pixel_format* dst_fmt,
const uint32_t* src,
const struct rfb_pixel_format* src_fmt,
size_t length)
2019-08-17 20:49:11 +00:00
{
int bytes_per_cpixel = calc_bytes_per_cpixel(dst_fmt);
2019-08-17 20:49:11 +00:00
2019-08-27 20:28:12 +00:00
vec_clear(dst);
2019-09-01 22:56:59 +00:00
uint32_t palette[16];
int palette_size = zrle_get_tile_palette(palette, src, length);
2019-09-01 22:56:59 +00:00
if (palette_size == 1) {
zrle_encode_unichrome_tile(dst, dst_fmt, palette[0], src_fmt);
return;
}
if (palette_size > 1) {
zrle_encode_packed_tile(dst, dst_fmt, src, src_fmt, length,
2019-10-20 22:13:51 +00:00
palette, palette_size);
2019-09-01 22:56:59 +00:00
return;
}
2019-08-27 20:28:12 +00:00
vec_fast_append_8(dst, 0);
pixel_to_cpixel(((uint8_t*)dst->data) + 1, dst_fmt, (uint8_t*)src, src_fmt,
2019-10-20 22:13:51 +00:00
bytes_per_cpixel, length);
2019-08-27 20:28:12 +00:00
dst->len += bytes_per_cpixel * length;
2019-08-17 20:49:11 +00:00
}
static int zrle_deflate(struct vec* dst, const struct vec* src, z_stream* zs,
bool flush)
2019-08-27 21:04:20 +00:00
{
zs->next_in = src->data;
zs->avail_in = src->len;
do {
if (dst->len == dst->cap && vec_reserve(dst, dst->cap * 2) < 0)
return -1;
zs->next_out = ((Bytef*)dst->data) + dst->len;
zs->avail_out = dst->cap - dst->len;
2020-04-10 12:36:51 +00:00
int r = deflate(zs, flush ? Z_SYNC_FLUSH : Z_NO_FLUSH);
if (r == Z_STREAM_ERROR)
return -1;
2019-08-27 21:04:20 +00:00
dst->len = zs->next_out - (Bytef*)dst->data;
2019-08-27 21:04:20 +00:00
} while (zs->avail_out == 0);
assert(zs->avail_in == 0);
return 0;
}
2021-12-11 21:55:56 +00:00
static int zrle_encode_box(struct zrle_encoder* self, struct vec* out,
const struct rfb_pixel_format* dst_fmt,
const struct nvnc_fb* fb,
const struct rfb_pixel_format* src_fmt, int x, int y,
int stride, int width, int height, z_stream* zs)
{
2019-08-25 16:40:59 +00:00
int r = -1;
int bytes_per_cpixel = calc_bytes_per_cpixel(dst_fmt);
2019-08-27 20:28:12 +00:00
struct vec in;
2019-08-17 20:49:11 +00:00
2021-12-11 21:55:56 +00:00
uint16_t x_pos = self->encoder.x_pos;
uint16_t y_pos = self->encoder.y_pos;
2019-10-20 22:13:51 +00:00
uint32_t* tile = malloc(TILE_LENGTH * TILE_LENGTH * 4);
if (!tile)
goto failure;
if (vec_init(&in, 1 + bytes_per_cpixel * TILE_LENGTH * TILE_LENGTH) < 0)
2019-08-25 16:40:59 +00:00
goto failure;
2021-12-11 21:55:56 +00:00
r = encode_rect_head(out, RFB_ENCODING_ZRLE, x_pos + x, y_pos + y,
width, height);
2019-08-27 21:49:28 +00:00
if (r < 0)
goto failure;
2019-08-25 16:40:59 +00:00
/* Reserve space for size */
2019-08-27 21:49:28 +00:00
size_t size_index = out->len;
vec_append_zero(out, 4);
2019-08-25 16:40:59 +00:00
2019-10-20 22:13:51 +00:00
int n_tiles = UDIV_UP(width, TILE_LENGTH) * UDIV_UP(height, TILE_LENGTH);
for (int i = 0; i < n_tiles; ++i) {
int tile_x = (i % UDIV_UP(width, TILE_LENGTH)) * TILE_LENGTH;
int tile_y = (i / UDIV_UP(width, TILE_LENGTH)) * TILE_LENGTH;
2019-08-25 19:17:24 +00:00
2019-10-20 22:13:51 +00:00
int tile_width = width - tile_x >= TILE_LENGTH ? TILE_LENGTH
: width - tile_x;
int tile_height = height - tile_y >= TILE_LENGTH
? TILE_LENGTH
: height - tile_y;
2019-08-25 19:17:24 +00:00
2019-10-07 17:39:54 +00:00
int y_off = y + tile_y;
2019-09-08 23:24:01 +00:00
zrle_copy_tile(tile,
2019-10-20 22:13:51 +00:00
((uint32_t*)fb->addr) + x + tile_x + y_off * stride,
stride, tile_width, tile_height);
zrle_encode_tile(&in, dst_fmt, tile, src_fmt,
2019-10-20 22:13:51 +00:00
tile_width * tile_height);
2019-08-25 16:40:59 +00:00
r = zrle_deflate(out, &in, zs, i == n_tiles - 1);
2019-08-27 21:04:20 +00:00
if (r < 0)
goto failure;
2019-08-25 16:40:59 +00:00
}
2019-08-27 21:49:28 +00:00
uint32_t out_size = htonl(out->len - size_index - 4);
memcpy(((uint8_t*)out->data) + size_index, &out_size, sizeof(out_size));
2019-08-25 16:40:59 +00:00
failure:
2019-08-27 20:28:12 +00:00
vec_destroy(&in);
free(tile);
2019-08-25 16:40:59 +00:00
return r;
#undef CHUNK
}
2021-12-11 21:55:56 +00:00
static int zrle_encode_frame(struct zrle_encoder* self, z_stream* zs,
struct vec* dst, const struct rfb_pixel_format* dst_fmt,
struct nvnc_fb* src, const struct rfb_pixel_format* src_fmt,
struct pixman_region16* region)
2019-08-25 16:40:59 +00:00
{
int rc = -1;
2021-12-12 16:05:29 +00:00
self->encoder.n_rects = 0;
int n_rects = 0;
2019-10-20 22:13:51 +00:00
struct pixman_box16* box = pixman_region_rectangles(region, &n_rects);
if (n_rects > UINT16_MAX) {
box = pixman_region_extents(region);
n_rects = 1;
}
2019-08-25 16:40:59 +00:00
2021-09-12 18:01:58 +00:00
rc = nvnc_fb_map(src);
if (rc < 0)
return -1;
for (int i = 0; i < n_rects; ++i) {
int x = box[i].x1;
int y = box[i].y1;
2019-08-25 19:10:35 +00:00
int box_width = box[i].x2 - x;
int box_height = box[i].y2 - y;
2021-12-11 21:55:56 +00:00
rc = zrle_encode_box(self, dst, dst_fmt, src, src_fmt, x, y,
2021-09-04 23:53:30 +00:00
src->stride, box_width, box_height, zs);
if (rc < 0)
return -1;
2019-08-25 16:40:59 +00:00
}
2021-12-12 16:05:29 +00:00
self->encoder.n_rects = n_rects;
2019-08-25 16:40:59 +00:00
return 0;
}
2021-12-09 22:48:31 +00:00
static void zrle_encoder_do_work(void* obj)
{
struct zrle_encoder* self = aml_get_userdata(obj);
int rc;
struct nvnc_fb* fb = self->current_fb;
assert(fb);
// TODO: Calculate the ideal buffer size based on the size of the
// damaged area.
size_t buffer_size = nvnc_fb_get_stride(fb) * nvnc_fb_get_height(fb) *
nvnc_fb_get_pixel_size(fb);
struct vec dst;
rc = vec_init(&dst, buffer_size);
assert(rc == 0);
struct rfb_pixel_format src_fmt;
rc = rfb_pixfmt_from_fourcc(&src_fmt, nvnc_fb_get_fourcc_format(fb));
assert(rc == 0);
2021-12-11 21:55:56 +00:00
rc = zrle_encode_frame(self, &self->zs, &dst, &self->output_format, fb,
2021-12-09 22:48:31 +00:00
&src_fmt, &self->current_damage);
assert(rc == 0);
self->current_result = rcbuf_new(dst.data, dst.len);
assert(self->current_result);
}
static void zrle_encoder_on_done(void* obj)
{
struct zrle_encoder* self = aml_get_userdata(obj);
assert(self->current_result);
2022-04-14 16:56:41 +00:00
uint64_t pts = nvnc_fb_get_pts(self->current_fb);
2021-12-09 22:48:31 +00:00
nvnc_fb_unref(self->current_fb);
self->current_fb = NULL;
pixman_region_clear(&self->current_damage);
struct rcbuf* result = self->current_result;
self->current_result = NULL;
aml_unref(self->work);
self->work = NULL;
2022-06-05 15:50:43 +00:00
encoder_finish_frame(&self->encoder, result, pts);
2021-12-09 22:48:31 +00:00
rcbuf_unref(result);
encoder_unref(&self->encoder);
2021-12-09 22:48:31 +00:00
}
struct encoder* zrle_encoder_new(void)
{
struct zrle_encoder* self = calloc(1, sizeof(*self));
if (!self)
return NULL;
2022-10-30 10:41:20 +00:00
encoder_init(&self->encoder, &encoder_impl_zrle);
2021-12-09 22:48:31 +00:00
int rc = deflateInit2(&self->zs,
/* compression level: */ 1,
/* method: */ Z_DEFLATED,
/* window bits: */ 15,
/* mem level: */ 9,
/* strategy: */ Z_DEFAULT_STRATEGY);
if (rc != Z_OK)
goto deflate_failure;
pixman_region_init(&self->current_damage);
return (struct encoder*)self;
deflate_failure:
free(self);
return NULL;
}
static void zrle_encoder_destroy(struct encoder* encoder)
{
struct zrle_encoder* self = zrle_encoder(encoder);
pixman_region_fini(&self->current_damage);
deflateEnd(&self->zs);
if (self->work)
aml_unref(self->work);
2022-10-30 13:53:10 +00:00
if (self->current_result)
rcbuf_unref(self->current_result);
2021-12-09 22:48:31 +00:00
free(self);
}
static void zrle_encoder_set_output_format(struct encoder* encoder,
const struct rfb_pixel_format* pixfmt)
{
struct zrle_encoder* self = zrle_encoder(encoder);
memcpy(&self->output_format, pixfmt, sizeof(self->output_format));
}
static int zrle_encoder_encode(struct encoder* encoder, struct nvnc_fb* fb,
struct pixman_region16* damage)
{
struct zrle_encoder* self = zrle_encoder(encoder);
assert(!self->current_fb);
self->work = aml_work_new(zrle_encoder_do_work, zrle_encoder_on_done,
self, NULL);
if (!self->work)
return -1;
self->current_fb = fb;
nvnc_fb_ref(self->current_fb);
pixman_region_copy(&self->current_damage, damage);
encoder_ref(&self->encoder);
2021-12-09 22:48:31 +00:00
int rc = aml_start(aml_get_default(), self->work);
if (rc < 0) {
encoder_unref(&self->encoder);
2021-12-09 22:48:31 +00:00
aml_unref(self->work);
self->work = NULL;
pixman_region_clear(&self->current_damage);
nvnc_fb_unref(self->current_fb);
self->current_fb = NULL;
}
return rc;
}
struct encoder_impl encoder_impl_zrle = {
.destroy = zrle_encoder_destroy,
.set_output_format = zrle_encoder_set_output_format,
.encode = zrle_encoder_encode,
};