-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgio.c
290 lines (232 loc) · 8.06 KB
/
imgio.c
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <strings.h>
#include <arpa/inet.h>
#include "imgio.h"
unsigned char *clone_buf(const unsigned char *buf, size_t length) {
if (buf == NULL) return NULL;
unsigned char *ret = (unsigned char*) malloc(length);
assert(ret != NULL);
memcpy(ret, buf, length);
return ret;
}
Image *clone_image(const Image *image) {
Image *ret = (Image*) malloc(sizeof(Image));
memcpy(ret, image, sizeof(Image));
ret->ctx = NULL;
ret->surf = NULL;
ret->buf = clone_buf(ret->buf, 4 * ret->width * ret->height);
ret->jpeg_buf = clone_buf(ret->jpeg_buf, ret->jpeg_length);
ret->yuv_buf = clone_buf(ret->yuv_buf, ret->yuv_len);
return ret;
}
void free_image(Image *image) {
free(image->buf);
free(image->jpeg_buf);
free(image->yuv_buf);
if (image->ctx) cairo_destroy(image->ctx);
if (image->surf) cairo_surface_destroy(image->surf);
free(image);
}
Image *read_jpeg_frame(TJContext *ctx, FILE *fin) {
uint32_t length;
double timestamp;
size_t res;
res = fread(×tamp, 8, 1, fin);
if (res != 1) return NULL;
res = fread(&length, 4, 1, fin);
if (res != 1) return NULL;
length = ntohl(length);
//fprintf(stderr, "read length: %d\n", length);
unsigned char *buf = (unsigned char*) malloc(length);
if (buf == NULL) return NULL;
int remaining = length;
unsigned char *read_buf = buf;
while (remaining > 0) {
res = fread(read_buf, 1, remaining, fin);
if (res == 0) {
free(buf);
return NULL;
}
remaining -= res;
read_buf += res;
}
Image *image = (Image*) malloc(sizeof(Image));
if (image == NULL) {
free(buf);
return NULL;
}
bzero(image, sizeof(Image));
image->jpeg_buf = buf;
image->jpeg_length = length;
image->timestamp = timestamp;
return image;
}
Image *read_frame(TJContext *ctx, FILE *fin) {
Image *image = read_jpeg_frame(ctx, fin);
if (image == NULL) return NULL;
DecodeRes dres = decode_image(ctx, (char*) image->jpeg_buf, image->jpeg_length, TJPF_BGRX, TJFLAG_ACCURATEDCT);
image->width = dres.width;
image->height = dres.height;
image->buf = dres.buf;
image->subsamp = dres.subsamp;
// We clear away JPEG buffer in order to reclaim memory and avoid
// confusion
free(image->jpeg_buf);
image->jpeg_buf = NULL;
return image;
}
Image *read_frame_to_yuv(TJContext *ctx, FILE *fin) {
Image *image = read_jpeg_frame(ctx, fin);
if (image == NULL) return NULL;
DecodeYUVRes dres = decode_image_to_yuv(ctx, (char*) image->jpeg_buf, image->jpeg_length, TJFLAG_ACCURATEDCT);
image->width = dres.width;
image->height = dres.height;
image->yuv_buf = dres.buf;
image->yuv_len = dres.len;
image->subsamp = dres.subsamp;
// We clear away JPEG buffer in order to reclaim memory and avoid
// confusion
free(image->jpeg_buf);
image->jpeg_buf = NULL;
return image;
}
int write_jpeg_frame(TJContext *ctx, FILE *fout, Image *image) {
uint32_t length32 = htonl((uint32_t) image->jpeg_length);
size_t res;
res = fwrite(&image->timestamp, 8, 1, fout);
if (res != 1) return 0;
res = fwrite(&length32, 4, 1, fout);
if (res != 1) return 0;
int remaining = image->jpeg_length;
unsigned char *write_buf = image->jpeg_buf;
while (remaining > 0) {
res = fwrite(write_buf, 1, remaining, fout);
if (res == 0) {
return 0;
}
remaining -= res;
write_buf += res;
}
return 1;
}
int write_frame(TJContext *ctx, FILE *fout, Image *image) {
EncodeRes eres = encode_image(ctx, (unsigned long) image->buf, image->width, image->height, TJPF_BGRX, TJSAMP_420, 100, TJFLAG_ACCURATEDCT);
image->jpeg_length = eres.len;
image->jpeg_buf = eres.buf;
int res;
res = write_jpeg_frame(ctx, fout, image);
// We clear away JPEG buffer in order to reclaim memory and avoid
// confusion
free_encoded_image(image->jpeg_buf);
image->jpeg_buf = NULL;
return res;
}
void get_cairo_context(Image *image) {
image->surf = cairo_image_surface_create_for_data(image->buf, CAIRO_FORMAT_RGB24, image->width, image->height, 4 * image->width);
image->ctx = cairo_create(image->surf);
}
// Convert YUV images from the format generated by TurboJPEG (see
// documentation for tjEncodeYUV2()) to the one understood by SDL
// (planes fully unpacked and with specified stride, which is called
// pitch)
void copy_yuv_to_planes(const Image *image, Uint16 *pitches, Uint8 **pixels, int swap_chroma) {
// Detect which kind of subsampling we have
int hsub = (image->subsamp == TJSAMP_420 || image->subsamp == TJSAMP_422);
int vsub = (image->subsamp == TJSAMP_420 || image->subsamp == TJSAMP_440);
// Luma dimensions are padded to 2 on subsampled directions; then
// width is padded to 4 (???: but then what is the point of perhaps
// padding to 2 before?)
int luma_width = image->width;
int luma_height = image->height;
if (hsub) {
luma_width = (luma_width + 1) & -2;
}
if (vsub) {
luma_height = (luma_height + 1) & -2;
}
luma_width = (luma_width + 3) & -4;
// Chroma dimensions are just computed according to subsampling;
// moreover, width is padded to 4
int chroma_width = image->width;
int chroma_height = image->height;
if (hsub) {
chroma_width = (chroma_width + 1) / 2;
}
if (vsub) {
chroma_height = (chroma_height + 1) / 2;
}
int actual_chroma_width = chroma_width;
chroma_width = (chroma_width + 3) & -4;
int target_chroma_width = (image->width + 1) / 2;
int target_chroma_height = (image->height + 1) / 2;
// Copy luma plane
unsigned char *base = image->yuv_buf;
int i;
for (i = 0; i < image->height; i++) {
memcpy(&pixels[0][i * pitches[0]], base, image->width);
base += luma_width;
}
// If the original image is already subsampled on both directions,
// we can copy stuff around directly
if (hsub && vsub) {
// Copy first chroma plane
base = image->yuv_buf + luma_width * luma_height;
for (i = 0; i < chroma_height; i++) {
memcpy(&pixels[swap_chroma ? 2 : 1][i * pitches[swap_chroma ? 2 : 1]], base, actual_chroma_width);
base += chroma_width;
}
// Copy second chroma plane
base = image->yuv_buf + luma_width * luma_height + chroma_width * chroma_height;
for (i = 0; i < chroma_height; i++) {
memcpy(&pixels[swap_chroma ? 1 : 2][i * pitches[swap_chroma ? 1 : 2]], base, actual_chroma_width);
base += chroma_width;
}
}
// If not, we have to implement rescaling by hand (FIXME: although
// we should better call an accelerated function from some libav
// library; FIXME: duplicated code; FIXME: some cases are not
// supported)
else {
// Copy first chroma plane
if (image->width % 2 == 1 || image->height % 2 == 1) {
fprintf(stderr, "Images with odd size are not supported\n");
exit(1);
}
base = image->yuv_buf + luma_width * luma_height;
int j;
for (i = 0; i < target_chroma_height; i++) {
for (j = 0; j < target_chroma_width; j++) {
if (!hsub && !vsub) {
pixels[swap_chroma ? 2 : 1][i * pitches[swap_chroma ? 2 : 1] + j] =
(base[chroma_width * 2 * i + 2 * j] +
base[chroma_width * 2 * i + 2 * j + 1] +
base[chroma_width * (2 * i + 1) + 2 * j] +
base[chroma_width * (2 * i + 1) + 2 * j + 1]) / 4;
} else {
fprintf(stderr, "Only 444 and 420 subsampling are supported\n");
exit(1);
}
}
}
// Copy second chroma plane
base = image->yuv_buf + luma_width * luma_height + chroma_width * chroma_height;
for (i = 0; i < target_chroma_height; i++) {
for (j = 0; j < target_chroma_width; j++) {
if (!hsub && !vsub) {
pixels[swap_chroma ? 1 : 2][i * pitches[swap_chroma ? 1 : 2] + j] =
(base[chroma_width * 2 * i + 2 * j] +
base[chroma_width * 2 * i + 2 * j + 1] +
base[chroma_width * (2 * i + 1) + 2 * j] +
base[chroma_width * (2 * i + 1) + 2 * j + 1]) / 4;
} else {
fprintf(stderr, "Only 444 and 420 subsampling are supported\n");
exit(1);
}
}
}
}
// Whoo, we really hope everything went fine... Specs are definitely
// not clear!
}