File: jpeg-encoder.c

package info (click to toggle)
spice 0.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,460 kB
  • sloc: ansic: 43,305; cpp: 30,185; sh: 5,342; python: 3,040; makefile: 844
file content (275 lines) | stat: -rw-r--r-- 8,119 bytes parent folder | download
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
   Copyright (C) 2009 Red Hat, Inc.

   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/>.
*/
#include <config.h>

#ifdef _WIN32
/* Avoid conflicting types for INT32 */
#define QGLOBAL_H
#endif

#include <stdio.h>
#include <jpeglib.h>

#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;

    struct jpeg_destination_mgr dest_mgr;
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;

    struct {
        JpegEncoderImageType type;
        int width;
        int height;
        int stride;
        unsigned int out_size;
        void (*convert_line_to_RGB24) (void *line, int width, uint8_t **out_line);
    } cur_image;
};

typedef struct JpegEncoderContext JpegEncoder;

/* jpeg destination manager callbacks */

static void dest_mgr_init_destination(j_compress_ptr cinfo)
{
    JpegEncoder *enc = (JpegEncoder *)cinfo->client_data;
    if (enc->dest_mgr.free_in_buffer == 0) {
        enc->dest_mgr.free_in_buffer = enc->usr->more_space(enc->usr,
                                                            &enc->dest_mgr.next_output_byte);

        if (enc->dest_mgr.free_in_buffer == 0) {
            spice_error("not enough space");
        }
    }

    enc->cur_image.out_size = enc->dest_mgr.free_in_buffer;
}

static boolean dest_mgr_empty_output_buffer(j_compress_ptr cinfo)
{
    JpegEncoder *enc = (JpegEncoder *)cinfo->client_data;
    enc->dest_mgr.free_in_buffer = enc->usr->more_space(enc->usr,
                                                        &enc->dest_mgr.next_output_byte);

    if (enc->dest_mgr.free_in_buffer == 0) {
        spice_error("not enough space");
    }
    enc->cur_image.out_size += enc->dest_mgr.free_in_buffer;
    return TRUE;
}

static void dest_mgr_term_destination(j_compress_ptr cinfo)
{
    JpegEncoder *enc = (JpegEncoder *)cinfo->client_data;
    enc->cur_image.out_size -= enc->dest_mgr.free_in_buffer;
}

JpegEncoderContext* jpeg_encoder_create(JpegEncoderUsrContext *usr)
{
    JpegEncoder *enc;
    if (!usr->more_space || !usr->more_lines) {
        return NULL;
    }

    enc = g_new0(JpegEncoder, 1);

    enc->usr = usr;

    enc->dest_mgr.init_destination = dest_mgr_init_destination;
    enc->dest_mgr.empty_output_buffer = dest_mgr_empty_output_buffer;
    enc->dest_mgr.term_destination = dest_mgr_term_destination;

    enc->cinfo.err = jpeg_std_error(&enc->jerr);

    jpeg_create_compress(&enc->cinfo);
    enc->cinfo.client_data = enc;
    enc->cinfo.dest = &enc->dest_mgr;
    return enc;
}

void jpeg_encoder_destroy(JpegEncoderContext* encoder)
{
    jpeg_destroy_compress(&encoder->cinfo);
    g_free(encoder);
}

static void convert_RGB16_to_RGB24(void *line, int width, uint8_t **out_line)
{
    uint16_t *src_line = (uint16_t *) line;
    uint8_t *out_pix;
    int x;

    spice_assert(out_line && *out_line);

    out_pix = *out_line;

    for (x = 0; x < width; x++) {
       uint16_t pixel = *src_line++;
       pixel = GUINT16_FROM_LE(pixel);
       *out_pix++ = ((pixel >> 7) & 0xf8) | ((pixel >> 12) & 0x7);
       *out_pix++ = ((pixel >> 2) & 0xf8) | ((pixel >> 7) & 0x7);
       *out_pix++ = ((pixel << 3) & 0xf8) | ((pixel >> 2) & 0x7);
   }
}

#ifndef JCS_EXTENSIONS
static void convert_BGR24_to_RGB24(void *in_line, int width, uint8_t **out_line)
{
    int x;
    uint8_t *out_pix;
    uint8_t *line = (uint8_t *) in_line;
    spice_assert(out_line && *out_line);

    out_pix = *out_line;

    for (x = 0; x < width; x++) {
        *out_pix++ = line[2];
        *out_pix++ = line[1];
        *out_pix++ = line[0];
        line += 3;
    }
}

static void convert_BGRX32_to_RGB24(void *line, int width, uint8_t **out_line)
{
    uint32_t *src_line = (uint32_t *) line;
    uint8_t *out_pix;
    int x;

    spice_assert(out_line && *out_line);

    out_pix = *out_line;

    for (x = 0; x < width; x++) {
        uint32_t pixel = *src_line++;
        pixel = GUINT32_FROM_LE(pixel);
        *out_pix++ = (pixel >> 16) & 0xff;
        *out_pix++ = (pixel >> 8) & 0xff;
        *out_pix++ = pixel & 0xff;
    }
}
#endif

#define FILL_LINES() {                                                  \
    if (lines == lines_end) {                                           \
        int n = jpeg->usr->more_lines(jpeg->usr, &lines);               \
        if (n <= 0) {                                                   \
            spice_error("more lines failed");                           \
        }                                                               \
        lines_end = lines + n * stride;                                 \
    }                                                                   \
}

static void do_jpeg_encode(JpegEncoder *jpeg, uint8_t *lines, unsigned int num_lines)
{
    uint8_t *lines_end;
    uint8_t *RGB24_line;
    int stride, width;
    JSAMPROW row_pointer[1];
    width = jpeg->cur_image.width;
    stride = jpeg->cur_image.stride;

    lines_end = lines + (stride * num_lines);

#ifdef JCS_EXTENSIONS
    if (jpeg->cur_image.convert_line_to_RGB24 == NULL) {
        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);
        row_pointer[0] = RGB24_line;
        jpeg_write_scanlines(&jpeg->cinfo, row_pointer, 1);
    }

    g_free(RGB24_line);
}

int jpeg_encode(JpegEncoderContext *enc, int quality, JpegEncoderImageType type,
                int width, int height, uint8_t *lines, unsigned int num_lines, int stride,
                uint8_t *io_ptr, unsigned int num_io_bytes)
{
    enc->cur_image.type = type;
    enc->cur_image.width = width;
    enc->cur_image.height = height;
    enc->cur_image.stride = stride;
    enc->cur_image.out_size = 0;

    switch (type) {
    case JPEG_IMAGE_TYPE_RGB16:
        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");
    }

    enc->cinfo.image_width = width;
    enc->cinfo.image_height = height;
    enc->cinfo.input_components = 3;
    enc->cinfo.in_color_space = JCS_RGB;
    jpeg_set_defaults(&enc->cinfo);
    jpeg_set_quality(&enc->cinfo, quality, TRUE);

    enc->dest_mgr.next_output_byte = io_ptr;
    enc->dest_mgr.free_in_buffer = num_io_bytes;

    jpeg_start_compress(&enc->cinfo, TRUE);

    do_jpeg_encode(enc, lines, num_lines);

    jpeg_finish_compress(&enc->cinfo);
    return enc->cur_image.out_size;
}