summaryrefslogtreecommitdiff
path: root/server/lz4-encoder.c
blob: 7b6359f6588ffc22d0af61d437f58280db69a65e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
   Copyright (C) 2014 Flexible Software Solutions S.L.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef USE_LZ4

#define SPICE_LOG_DOMAIN "SpiceLz4Encoder"

#include <arpa/inet.h>
#include <lz4.h>
#include "red-common.h"
#include "lz4-encoder.h"

typedef struct Lz4Encoder {
    Lz4EncoderUsrContext *usr;
} Lz4Encoder;

Lz4EncoderContext* lz4_encoder_create(Lz4EncoderUsrContext *usr)
{
    Lz4Encoder *enc;
    if (!usr->more_space || !usr->more_lines) {
        return NULL;
    }

    enc = spice_new0(Lz4Encoder, 1);
    enc->usr = usr;

    return (Lz4EncoderContext*)enc;
}

void lz4_encoder_destroy(Lz4EncoderContext* encoder)
{
    free(encoder);
}

int lz4_encode(Lz4EncoderContext *lz4, int height, int stride, uint8_t *io_ptr,
               unsigned int num_io_bytes, int top_down, uint8_t format)
{
    Lz4Encoder *enc = (Lz4Encoder *)lz4;
    uint8_t *lines;
    int num_lines = 0;
    int total_lines = 0;
    int in_size, enc_size, out_size, already_copied;
    uint8_t *in_buf, *compressed_lines;
    uint8_t *out_buf = io_ptr;
    LZ4_stream_t *stream = LZ4_createStream();

    // Encode direction and format
    *(out_buf++) = top_down ? 1 : 0;
    *(out_buf++) = format;
    num_io_bytes -= 2;
    out_size = 2;

    do {
        num_lines = enc->usr->more_lines(enc->usr, &lines);
        if (num_lines <= 0) {
            spice_error("more lines failed");
            LZ4_freeStream(stream);
            return 0;
        }
        in_buf = lines;
        in_size = stride * num_lines;
        lines += in_size;
        compressed_lines = (uint8_t *) malloc(LZ4_compressBound(in_size) + 4);
        enc_size = LZ4_compress_continue(stream, (const char *) in_buf,
                                         (char *) compressed_lines + 4, in_size);
        if (enc_size <= 0) {
            spice_error("compress failed!");
            free(compressed_lines);
            LZ4_freeStream(stream);
            return 0;
        }
        *((uint32_t *)compressed_lines) = htonl(enc_size);

        out_size += enc_size += 4;
        already_copied = 0;
        while (num_io_bytes < enc_size) {
            memcpy(out_buf, compressed_lines + already_copied, num_io_bytes);
            already_copied += num_io_bytes;
            enc_size -= num_io_bytes;
            num_io_bytes = enc->usr->more_space(enc->usr, &io_ptr);
            if (num_io_bytes <= 0) {
                spice_error("more space failed");
                free(compressed_lines);
                LZ4_freeStream(stream);
                return 0;
            }
            out_buf = io_ptr;
        }
        memcpy(out_buf, compressed_lines + already_copied, enc_size);
        out_buf += enc_size;
        num_io_bytes -= enc_size;

        free(compressed_lines);
        total_lines += num_lines;
    } while (total_lines < height);

    LZ4_freeStream(stream);
    if (total_lines != height) {
        spice_error("too many lines\n");
        out_size = 0;
    }

    return out_size;
}

#endif // USE_LZ4