| 1 |
// Copyright 2025 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
#include <stdlib.h> |
| 15 |
#include <string.h> |
| 16 |
#include <stdbool.h> |
| 17 |
#include <stdio.h> |
| 18 |
#include <webp/encode.h> |
| 19 |
#include <webp/decode.h> |
| 20 |
#include "webp/demux.h" |
| 21 |
#include <webp/mux.h> |
| 22 |
#include "../deps/parson/parson.h" |
| 23 |
|
| 24 |
void handle_commands(FILE *stream); |
| 25 |
|
| 26 |
int main() |
| 27 |
{ |
| 28 |
// This will read commands from stdin and write responses to stdout |
| 29 |
// and return 0 when stdin is closed. |
| 30 |
// Any errors gets reported in the RPC response messages. |
| 31 |
handle_commands(stdin); |
| 32 |
return 0; |
| 33 |
} |
| 34 |
|
| 35 |
// Translation table for WebPEncodingError to string. |
| 36 |
// This translation table is replicated in all webp bindings on the web. Great design. |
| 37 |
static const char *const kErrorMessages[VP8_ENC_ERROR_LAST] = { |
| 38 |
"OK", |
| 39 |
"OUT_OF_MEMORY: Out of memory allocating objects", |
| 40 |
"BITSTREAM_OUT_OF_MEMORY: Out of memory re-allocating byte buffer", |
| 41 |
"NULL_PARAMETER: NULL parameter passed to function", |
| 42 |
"INVALID_CONFIGURATION: configuration is invalid", |
| 43 |
"BAD_DIMENSION: Bad picture dimension. Maximum width and height " |
| 44 |
"allowed is 16383 pixels.", |
| 45 |
"PARTITION0_OVERFLOW: Partition #0 is too big to fit 512k.\n" |
| 46 |
"To reduce the size of this partition, try using less segments " |
| 47 |
"with the -segments option, and eventually reduce the number of " |
| 48 |
"header bits using -partition_limit. More details are available " |
| 49 |
"in the manual (`man cwebp`)", |
| 50 |
"PARTITION_OVERFLOW: Partition is too big to fit 16M", |
| 51 |
"BAD_WRITE: Picture writer returned an I/O error", |
| 52 |
"FILE_TOO_BIG: File would be too big to fit in 4G", |
| 53 |
"USER_ABORT: encoding abort requested by user"}; |
| 54 |
|
| 55 |
typedef struct |
| 56 |
{ |
| 57 |
int version; |
| 58 |
int id; |
| 59 |
char command[256]; |
| 60 |
char err[256]; |
| 61 |
} Header; |
| 62 |
|
| 63 |
typedef struct |
| 64 |
{ |
| 65 |
int width; |
| 66 |
int height; |
| 67 |
int stride; |
| 68 |
int loopCount; |
| 69 |
int frameCount; |
| 70 |
int *frameDurations; |
| 71 |
bool hasAlpha; |
| 72 |
|
| 73 |
} InputParams; |
| 74 |
|
| 75 |
typedef struct |
| 76 |
{ |
| 77 |
float quality; // between 1 and 100. |
| 78 |
char compression[32]; // "lossy" or "lossless" |
| 79 |
char hint[64]; // drawing, icon, photo, picture, or text. Default is photo. |
| 80 |
int preset; // preset to use; resolved from hint. |
| 81 |
|
| 82 |
bool useSharpYuv; // use sharp YUV for better quality. |
| 83 |
int method; // quality/speed trade-off (0=fast, 6=slower-better). Default is 2. |
| 84 |
|
| 85 |
} InputOptions; |
| 86 |
|
| 87 |
typedef struct |
| 88 |
{ |
| 89 |
InputOptions options; |
| 90 |
InputParams params; |
| 91 |
|
| 92 |
} InputData; |
| 93 |
|
| 94 |
typedef struct |
| 95 |
{ |
| 96 |
Header header; |
| 97 |
InputData data; |
| 98 |
|
| 99 |
} InputMessage; |
| 100 |
|
| 101 |
typedef struct |
| 102 |
{ |
| 103 |
Header header; |
| 104 |
InputData data; |
| 105 |
|
| 106 |
} OutputMessage; |
| 107 |
|
| 108 |
#define MAX_LINE_LENGTH 4096 |
| 109 |
|
| 110 |
static uint8_t *encodeNRGBA(WebPConfig *config, const uint8_t *rgba, int width, int height, int stride, size_t *output_size) |
| 111 |
{ |
| 112 |
WebPPicture pic; |
| 113 |
WebPMemoryWriter wrt; |
| 114 |
int ok; |
| 115 |
if (!WebPPictureInit(&pic)) |
| 116 |
{ |
| 117 |
fprintf(stderr, "WebPPictureInit failed\n"); |
| 118 |
return NULL; |
| 119 |
} |
| 120 |
|
| 121 |
pic.use_argb = 1; |
| 122 |
pic.width = width; |
| 123 |
pic.height = height; |
| 124 |
pic.writer = WebPMemoryWrite; |
| 125 |
pic.custom_ptr = &wrt; |
| 126 |
WebPMemoryWriterInit(&wrt); |
| 127 |
ok = WebPPictureImportRGBA(&pic, rgba, stride); |
| 128 |
if (ok) |
| 129 |
{ |
| 130 |
ok = WebPEncode(config, &pic); |
| 131 |
if (!ok) |
| 132 |
{ |
| 133 |
fprintf(stderr, "WebPEncode failed: %d (%s)\n", pic.error_code, kErrorMessages[pic.error_code]); |
| 134 |
} |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
fprintf(stderr, "WebPPictureImportRGBA failed: %d (%s)\n", pic.error_code, kErrorMessages[pic.error_code]); |
| 139 |
} |
| 140 |
WebPPictureFree(&pic); |
| 141 |
if (!ok) |
| 142 |
{ |
| 143 |
WebPMemoryWriterClear(&wrt); |
| 144 |
return NULL; |
| 145 |
} |
| 146 |
*output_size = wrt.size; |
| 147 |
return wrt.mem; |
| 148 |
} |
| 149 |
|
| 150 |
static uint8_t *encodeGray(WebPConfig *config, uint8_t *y, int width, int height, int stride, size_t *output_size) |
| 151 |
{ |
| 152 |
WebPPicture pic; |
| 153 |
WebPMemoryWriter wrt; |
| 154 |
|
| 155 |
int ok; |
| 156 |
if (!WebPPictureInit(&pic)) |
| 157 |
{ |
| 158 |
return NULL; |
| 159 |
} |
| 160 |
|
| 161 |
pic.use_argb = 0; |
| 162 |
pic.width = width; |
| 163 |
pic.height = height; |
| 164 |
pic.y_stride = stride; |
| 165 |
pic.writer = WebPMemoryWrite; |
| 166 |
pic.custom_ptr = &wrt; |
| 167 |
WebPMemoryWriterInit(&wrt); |
| 168 |
|
| 169 |
const int uvWidth = (int)(((int64_t)width + 1) >> 1); |
| 170 |
const int uvHeight = (int)(((int64_t)height + 1) >> 1); |
| 171 |
const int uvStride = uvWidth; |
| 172 |
const int uvSize = uvStride * uvHeight; |
| 173 |
const int gray = 128; |
| 174 |
uint8_t *chroma; |
| 175 |
|
| 176 |
chroma = malloc(uvSize); |
| 177 |
if (!chroma) |
| 178 |
{ |
| 179 |
return 0; |
| 180 |
} |
| 181 |
memset(chroma, gray, uvSize); |
| 182 |
|
| 183 |
pic.y = y; |
| 184 |
pic.u = chroma; |
| 185 |
pic.v = chroma; |
| 186 |
pic.uv_stride = uvStride; |
| 187 |
|
| 188 |
ok = WebPEncode(config, &pic); |
| 189 |
|
| 190 |
free(chroma); |
| 191 |
|
| 192 |
WebPPictureFree(&pic); |
| 193 |
if (!ok) |
| 194 |
{ |
| 195 |
WebPMemoryWriterClear(&wrt); |
| 196 |
return NULL; |
| 197 |
} |
| 198 |
*output_size = wrt.size; |
| 199 |
return wrt.mem; |
| 200 |
} |
| 201 |
|
| 202 |
static uint8_t *encodeNRGBAAnimated(WebPConfig *config, InputParams params, const uint8_t *all_frames_data, size_t *output_size) |
| 203 |
{ |
| 204 |
WebPAnimEncoderOptions anim_options; |
| 205 |
WebPAnimEncoderOptionsInit(&anim_options); |
| 206 |
anim_options.anim_params.loop_count = params.loopCount; |
| 207 |
|
| 208 |
WebPAnimEncoder *enc = WebPAnimEncoderNew(params.width, params.height, &anim_options); |
| 209 |
if (enc == NULL) |
| 210 |
{ |
| 211 |
fprintf(stderr, "Error creating WebPAnimEncoder\n"); |
| 212 |
return NULL; |
| 213 |
} |
| 214 |
|
| 215 |
int timestamp = 0; |
| 216 |
size_t frame_rgba_size = (size_t)params.stride * params.height; |
| 217 |
|
| 218 |
for (int i = 0; i < params.frameCount; i++) |
| 219 |
{ |
| 220 |
WebPPicture pic; |
| 221 |
if (!WebPPictureInit(&pic)) |
| 222 |
{ |
| 223 |
fprintf(stderr, "WebPPictureInit failed\n"); |
| 224 |
WebPAnimEncoderDelete(enc); |
| 225 |
return NULL; |
| 226 |
} |
| 227 |
pic.use_argb = 1; |
| 228 |
pic.width = params.width; |
| 229 |
pic.height = params.height; |
| 230 |
|
| 231 |
const uint8_t *frame_rgba = all_frames_data + i * frame_rgba_size; |
| 232 |
|
| 233 |
if (!WebPPictureImportRGBA(&pic, frame_rgba, params.stride)) |
| 234 |
{ |
| 235 |
fprintf(stderr, "WebPPictureImportRGBA failed\n"); |
| 236 |
WebPPictureFree(&pic); |
| 237 |
WebPAnimEncoderDelete(enc); |
| 238 |
return NULL; |
| 239 |
} |
| 240 |
|
| 241 |
if (!WebPAnimEncoderAdd(enc, &pic, timestamp, config)) |
| 242 |
{ |
| 243 |
fprintf(stderr, "WebPAnimEncoderAdd failed\n"); |
| 244 |
WebPPictureFree(&pic); |
| 245 |
WebPAnimEncoderDelete(enc); |
| 246 |
return NULL; |
| 247 |
} |
| 248 |
timestamp += params.frameDurations[i]; |
| 249 |
WebPPictureFree(&pic); |
| 250 |
} |
| 251 |
|
| 252 |
if (!WebPAnimEncoderAdd(enc, NULL, timestamp, config)) |
| 253 |
{ |
| 254 |
fprintf(stderr, "WebPAnimEncoderAdd failed for final frame\n"); |
| 255 |
WebPAnimEncoderDelete(enc); |
| 256 |
return NULL; |
| 257 |
} |
| 258 |
|
| 259 |
WebPData webp_data_out; |
| 260 |
WebPDataInit(&webp_data_out); |
| 261 |
if (!WebPAnimEncoderAssemble(enc, &webp_data_out)) |
| 262 |
{ |
| 263 |
fprintf(stderr, "WebPAnimEncoderAssemble failed\n"); |
| 264 |
WebPAnimEncoderDelete(enc); |
| 265 |
return NULL; |
| 266 |
} |
| 267 |
WebPAnimEncoderDelete(enc); |
| 268 |
|
| 269 |
*output_size = webp_data_out.size; |
| 270 |
uint8_t *webp_data = malloc(*output_size); |
| 271 |
if (webp_data == NULL) |
| 272 |
{ |
| 273 |
fprintf(stderr, "malloc failed for final webp data\n"); |
| 274 |
WebPDataClear(&webp_data_out); |
| 275 |
return NULL; |
| 276 |
} |
| 277 |
memcpy(webp_data, webp_data_out.bytes, *output_size); |
| 278 |
WebPDataClear(&webp_data_out); |
| 279 |
|
| 280 |
return webp_data; |
| 281 |
} |
| 282 |
|
| 283 |
static uint8_t initDecoderConfig(WebPDecoderConfig *config, WebPData data) |
| 284 |
{ |
| 285 |
if (!WebPInitDecoderConfig(config)) |
| 286 |
{ |
| 287 |
return 0; |
| 288 |
} |
| 289 |
if (WebPGetFeatures(data.bytes, data.size, &config->input) != VP8_STATUS_OK) |
| 290 |
{ |
| 291 |
return 0; |
| 292 |
} |
| 293 |
return 1; |
| 294 |
} |
| 295 |
|
| 296 |
static uint8_t initEncoderConfig(WebPConfig *config, InputOptions opts) |
| 297 |
{ |
| 298 |
if (!WebPConfigInit(config)) |
| 299 |
{ |
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
if (!WebPConfigPreset(config, opts.preset, opts.quality)) |
| 304 |
{ |
| 305 |
return 0; |
| 306 |
} |
| 307 |
|
| 308 |
if (strcmp(opts.compression, "lossless") == 0) |
| 309 |
{ |
| 310 |
// Activate the lossless compression mode with the desired efficiency level |
| 311 |
// between 0 (fastest, lowest compression) and 9 (slower, best compression). |
| 312 |
// A good default level is '6', providing a fair tradeoff between compression |
| 313 |
// speed and final compressed size. |
| 314 |
if (!WebPConfigLosslessPreset(config, 6)) |
| 315 |
{ |
| 316 |
return 1; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
config->use_sharp_yuv = opts.useSharpYuv ? 1 : 0; |
| 321 |
config->method = opts.method; |
| 322 |
|
| 323 |
return 1; |
| 324 |
} |
| 325 |
|
| 326 |
InputMessage parse_input_message(const char *line) |
| 327 |
{ |
| 328 |
InputMessage msg = {0}; |
| 329 |
|
| 330 |
JSON_Value *root_value = json_parse_string(line); |
| 331 |
if (root_value == NULL) |
| 332 |
{ |
| 333 |
fprintf(stderr, "Error parsing JSON line\n"); |
| 334 |
return msg; |
| 335 |
} |
| 336 |
|
| 337 |
if (json_value_get_type(root_value) != JSONObject) |
| 338 |
{ |
| 339 |
fprintf(stderr, "Error: Line did not parse to a valid JSON object\n"); |
| 340 |
json_value_free(root_value); |
| 341 |
return msg; |
| 342 |
} |
| 343 |
|
| 344 |
JSON_Object *root_object = json_value_get_object(root_value); |
| 345 |
|
| 346 |
JSON_Object *header_object = json_object_get_object(root_object, "header"); |
| 347 |
if (header_object != NULL) |
| 348 |
{ |
| 349 |
msg.header.version = (int)json_object_get_number(header_object, "version"); |
| 350 |
msg.header.id = (int)json_object_get_number(header_object, "id"); |
| 351 |
const char *command_str = json_object_get_string(header_object, "command"); |
| 352 |
if (command_str != NULL) |
| 353 |
{ |
| 354 |
strncpy(msg.header.command, command_str, sizeof(msg.header.command) - 1); |
| 355 |
msg.header.command[sizeof(msg.header.command) - 1] = '\0'; |
| 356 |
} |
| 357 |
const char *err_str = json_object_get_string(header_object, "err"); |
| 358 |
if (err_str != NULL) |
| 359 |
{ |
| 360 |
strncpy(msg.header.err, err_str, sizeof(msg.header.err) - 1); |
| 361 |
msg.header.err[sizeof(msg.header.err) - 1] = '\0'; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
JSON_Object *data_object = json_object_get_object(root_object, "data"); |
| 366 |
if (data_object != NULL) |
| 367 |
{ |
| 368 |
JSON_Object *params_object = json_object_get_object(data_object, "params"); |
| 369 |
if (params_object != NULL) |
| 370 |
{ |
| 371 |
msg.data.params.width = (int)json_object_get_number(params_object, "width"); |
| 372 |
msg.data.params.height = (int)json_object_get_number(params_object, "height"); |
| 373 |
msg.data.params.stride = (int)json_object_get_number(params_object, "stride"); |
| 374 |
msg.data.params.loopCount = (int)json_object_get_number(params_object, "loopCount"); |
| 375 |
JSON_Array *durations_array = json_object_get_array(params_object, "frameDurations"); |
| 376 |
if (durations_array != NULL) |
| 377 |
{ |
| 378 |
size_t count = json_array_get_count(durations_array); |
| 379 |
msg.data.params.frameCount = count; |
| 380 |
|
| 381 |
if (count > 0) |
| 382 |
{ |
| 383 |
msg.data.params.frameDurations = malloc(sizeof(int) * count); |
| 384 |
if (msg.data.params.frameDurations != NULL) |
| 385 |
{ |
| 386 |
for (size_t i = 0; i < count; i++) |
| 387 |
{ |
| 388 |
msg.data.params.frameDurations[i] = (int)json_array_get_number(durations_array, i); |
| 389 |
} |
| 390 |
} |
| 391 |
else |
| 392 |
{ |
| 393 |
// Malloc failed. |
| 394 |
msg.data.params.frameCount = 0; |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
JSON_Object *options_object = json_object_get_object(data_object, "options"); |
| 400 |
if (options_object != NULL) |
| 401 |
{ |
| 402 |
msg.data.options.quality = (int)json_object_get_number(options_object, "quality"); |
| 403 |
const char *compression_str = json_object_get_string(options_object, "compression"); |
| 404 |
if (compression_str != NULL) |
| 405 |
{ |
| 406 |
strncpy(msg.data.options.compression, compression_str, sizeof(msg.data.options.compression) - 1); |
| 407 |
msg.data.options.compression[sizeof(msg.data.options.compression) - 1] = '\0'; |
| 408 |
} |
| 409 |
const char *hint_str = json_object_get_string(options_object, "hint"); |
| 410 |
if (hint_str != NULL) |
| 411 |
{ |
| 412 |
strncpy(msg.data.options.hint, hint_str, sizeof(msg.data.options.hint) - 1); |
| 413 |
msg.data.options.hint[sizeof(msg.data.options.hint) - 1] = '\0'; |
| 414 |
} |
| 415 |
msg.data.options.useSharpYuv = json_object_get_number(options_object, "useSharpYuv") != 0; |
| 416 |
msg.data.options.method = (int)json_object_get_number(options_object, "method"); |
| 417 |
if (msg.data.options.method < 0 || msg.data.options.method > 6) |
| 418 |
{ |
| 419 |
msg.data.options.method = 4; // default |
| 420 |
} |
| 421 |
if (msg.data.options.quality < 0 || msg.data.options.quality > 100) |
| 422 |
{ |
| 423 |
msg.data.options.quality = 75; // default |
| 424 |
} |
| 425 |
if (strlen(msg.data.options.hint) == 0) |
| 426 |
{ |
| 427 |
strncpy(msg.data.options.hint, "photo", sizeof(msg.data.options.hint) - 1); |
| 428 |
} |
| 429 |
|
| 430 |
// Resolve preset from hint |
| 431 |
if (strcmp(msg.data.options.hint, "picture") == 0) |
| 432 |
{ |
| 433 |
msg.data.options.preset = WEBP_PRESET_PICTURE; |
| 434 |
} |
| 435 |
else if (strcmp(msg.data.options.hint, "photo") == 0) |
| 436 |
{ |
| 437 |
msg.data.options.preset = WEBP_PRESET_PHOTO; |
| 438 |
} |
| 439 |
else if (strcmp(msg.data.options.hint, "drawing") == 0) |
| 440 |
{ |
| 441 |
msg.data.options.preset = WEBP_PRESET_DRAWING; |
| 442 |
} |
| 443 |
else if (strcmp(msg.data.options.hint, "icon") == 0) |
| 444 |
{ |
| 445 |
msg.data.options.preset = WEBP_PRESET_ICON; |
| 446 |
} |
| 447 |
else if (strcmp(msg.data.options.hint, "text") == 0) |
| 448 |
{ |
| 449 |
msg.data.options.preset = WEBP_PRESET_TEXT; |
| 450 |
} |
| 451 |
else |
| 452 |
{ |
| 453 |
// I'm not sure what webp's default is, but photo seems like a good choice. |
| 454 |
msg.data.options.preset = WEBP_PRESET_PHOTO; |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
json_value_free(root_value); |
| 460 |
return msg; |
| 461 |
} |
| 462 |
|
| 463 |
static void write_blob(uint32_t id, const uint8_t *data, uint32_t size) |
| 464 |
{ |
| 465 |
uint8_t output_blob_header[16]; |
| 466 |
// See https://github.com/bep/textandbinarywriter |
| 467 |
const char magic[] = {'T', 'A', 'K', '3', '5', 'E', 'M', '1'}; |
| 468 |
memcpy(output_blob_header, magic, 8); |
| 469 |
*(uint32_t *)&output_blob_header[8] = id; |
| 470 |
*(uint32_t *)&output_blob_header[12] = size; |
| 471 |
|
| 472 |
fwrite(output_blob_header, 1, sizeof(output_blob_header), stdout); |
| 473 |
fwrite(data, 1, (size_t)size, stdout); |
| 474 |
fflush(stdout); |
| 475 |
} |
| 476 |
|
| 477 |
void write_output_message(const OutputMessage *msg) |
| 478 |
{ |
| 479 |
JSON_Value *root_value = json_value_init_object(); |
| 480 |
JSON_Object *root_object = json_value_get_object(root_value); |
| 481 |
|
| 482 |
// Header |
| 483 |
JSON_Value *header_value = json_value_init_object(); |
| 484 |
JSON_Object *header_object = json_value_get_object(header_value); |
| 485 |
json_object_set_value(root_object, "header", header_value); |
| 486 |
json_object_set_number(header_object, "version", msg->header.version); |
| 487 |
json_object_set_number(header_object, "id", msg->header.id); |
| 488 |
json_object_set_string(header_object, "err", msg->header.err); |
| 489 |
|
| 490 |
// Data |
| 491 |
if (msg->data.params.width > 0) |
| 492 |
{ |
| 493 |
JSON_Value *data_value = json_value_init_object(); |
| 494 |
JSON_Object *data_object = json_value_get_object(data_value); |
| 495 |
json_object_set_value(root_object, "data", data_value); |
| 496 |
|
| 497 |
JSON_Value *params_value = json_value_init_object(); |
| 498 |
JSON_Object *params_object = json_value_get_object(params_value); |
| 499 |
json_object_set_value(data_object, "params", params_value); |
| 500 |
|
| 501 |
json_object_set_number(params_object, "width", msg->data.params.width); |
| 502 |
json_object_set_number(params_object, "height", msg->data.params.height); |
| 503 |
json_object_set_number(params_object, "stride", msg->data.params.stride); |
| 504 |
json_object_set_boolean(params_object, "hasAlpha", msg->data.params.hasAlpha); |
| 505 |
if (msg->data.params.frameDurations != NULL) |
| 506 |
{ |
| 507 |
JSON_Value *durations_value = json_value_init_array(); |
| 508 |
JSON_Array *durations_array = json_value_get_array(durations_value); |
| 509 |
for (int i = 0; i < msg->data.params.frameCount; i++) |
| 510 |
{ |
| 511 |
json_array_append_number(durations_array, msg->data.params.frameDurations[i]); |
| 512 |
} |
| 513 |
|
| 514 |
json_object_set_value(params_object, "frameDurations", durations_value); |
| 515 |
json_object_set_number(params_object, "loopCount", msg->data.params.loopCount); |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
char *serialized_string = json_serialize_to_string(root_value); |
| 520 |
fprintf(stdout, "%s\n", serialized_string); |
| 521 |
fflush(stdout); |
| 522 |
|
| 523 |
json_free_serialized_string(serialized_string); |
| 524 |
json_value_free(root_value); |
| 525 |
} |
| 526 |
|
| 527 |
// drain_bytes discards n bytes from stream. Used to keep the protocol aligned |
| 528 |
// after an error that prevents the blob from being consumed normally. |
| 529 |
static void drain_bytes(FILE *stream, size_t n) |
| 530 |
{ |
| 531 |
uint8_t buf[4096]; |
| 532 |
while (n > 0) |
| 533 |
{ |
| 534 |
size_t want = n < sizeof(buf) ? n : sizeof(buf); |
| 535 |
size_t got = fread(buf, 1, want, stream); |
| 536 |
if (got == 0) |
| 537 |
{ |
| 538 |
break; |
| 539 |
} |
| 540 |
n -= got; |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
void handle_commands(FILE *stream) |
| 545 |
{ |
| 546 |
|
| 547 |
char line[MAX_LINE_LENGTH]; |
| 548 |
|
| 549 |
while (fgets(line, sizeof(line), stream) != NULL) |
| 550 |
{ |
| 551 |
InputMessage input = {0}; |
| 552 |
uint8_t *blob_data = NULL; |
| 553 |
uint32_t blob_size = 0; |
| 554 |
|
| 555 |
// Remove newline character if present |
| 556 |
line[strcspn(line, "\n")] = 0; |
| 557 |
|
| 558 |
if (strlen(line) == 0) |
| 559 |
{ |
| 560 |
continue; |
| 561 |
} |
| 562 |
|
| 563 |
input = parse_input_message(line); |
| 564 |
|
| 565 |
// Next in stream is a blob header defined in https://github.com/bep/textandbinaryreader |
| 566 |
// T', 'A', 'K', '3', '5', 'E', 'M', '1' id uint32, size uint32 |
| 567 |
uint8_t blob_header[16]; |
| 568 |
size_t read_bytes = fread(blob_header, 1, sizeof(blob_header), stream); |
| 569 |
if (read_bytes != sizeof(blob_header)) |
| 570 |
{ |
| 571 |
fprintf(stderr, "Error reading blob header\n"); |
| 572 |
goto cleanup; |
| 573 |
} |
| 574 |
uint32_t blob_id = *(uint32_t *)&blob_header[8]; |
| 575 |
blob_size = *(uint32_t *)&blob_header[12]; |
| 576 |
blob_data = malloc((size_t)blob_size); |
| 577 |
if (blob_data == NULL) |
| 578 |
{ |
| 579 |
// Out of memory. Drain the blob from the input stream so the next |
| 580 |
// command stays aligned, then report the error to the client instead |
| 581 |
// of leaving the stream corrupted with no response. |
| 582 |
drain_bytes(stream, (size_t)blob_size); |
| 583 |
OutputMessage err_output = {0}; |
| 584 |
err_output.header = input.header; |
| 585 |
snprintf(err_output.header.err, sizeof(err_output.header.err), |
| 586 |
"out of memory allocating %u bytes for blob data", blob_size); |
| 587 |
write_output_message(&err_output); |
| 588 |
goto cleanup; |
| 589 |
} |
| 590 |
read_bytes = fread(blob_data, 1, (size_t)blob_size, stream); |
| 591 |
if (read_bytes != (size_t)blob_size) |
| 592 |
{ |
| 593 |
fprintf(stderr, "[%d] Error reading blob data (size: %llu read: %zu) \n", blob_id, (unsigned long long)blob_size, read_bytes); |
| 594 |
goto cleanup; |
| 595 |
} |
| 596 |
|
| 597 |
OutputMessage output = {0}; |
| 598 |
output.header = input.header; |
| 599 |
|
| 600 |
if (strcmp(input.header.command, "decode") == 0) |
| 601 |
{ |
| 602 |
|
| 603 |
WebPData data; |
| 604 |
data.bytes = blob_data; |
| 605 |
data.size = (size_t)blob_size; |
| 606 |
|
| 607 |
WebPDecoderConfig config; |
| 608 |
if (!initDecoderConfig(&config, data)) |
| 609 |
{ |
| 610 |
strncpy(output.header.err, "Failed to initialize WebPDecoderConfig", sizeof(output.header.err) - 1); |
| 611 |
write_output_message(&output); |
| 612 |
goto cleanup; |
| 613 |
} |
| 614 |
|
| 615 |
output.data.params.width = config.input.width; |
| 616 |
output.data.params.height = config.input.height; |
| 617 |
output.data.params.stride = config.input.width * 4; |
| 618 |
output.data.params.frameDurations = NULL; |
| 619 |
|
| 620 |
if (config.input.has_animation) |
| 621 |
{ |
| 622 |
WebPAnimDecoderOptions dec_options; |
| 623 |
WebPAnimDecoderOptionsInit(&dec_options); |
| 624 |
dec_options.color_mode = MODE_RGBA; |
| 625 |
|
| 626 |
WebPAnimDecoder *dec = WebPAnimDecoderNew(&data, &dec_options); |
| 627 |
if (dec == NULL) |
| 628 |
{ |
| 629 |
strncpy(output.header.err, "Failed to create WebPAnimDecoder", sizeof(output.header.err) - 1); |
| 630 |
write_output_message(&output); |
| 631 |
goto cleanup; |
| 632 |
} |
| 633 |
|
| 634 |
WebPAnimInfo anim_info; |
| 635 |
if (!WebPAnimDecoderGetInfo(dec, &anim_info)) |
| 636 |
{ |
| 637 |
strncpy(output.header.err, "Failed to get animation info", sizeof(output.header.err) - 1); |
| 638 |
write_output_message(&output); |
| 639 |
WebPAnimDecoderDelete(dec); |
| 640 |
goto cleanup; |
| 641 |
} |
| 642 |
|
| 643 |
output.data.params.width = anim_info.canvas_width; |
| 644 |
output.data.params.height = anim_info.canvas_height; |
| 645 |
output.data.params.stride = anim_info.canvas_width * 4; |
| 646 |
output.data.params.frameCount = anim_info.frame_count; |
| 647 |
output.data.params.loopCount = anim_info.loop_count; |
| 648 |
output.data.params.hasAlpha = true; // Animated WebP always decoded as RGBA |
| 649 |
|
| 650 |
int *frameDurations = malloc(sizeof(int) * anim_info.frame_count); |
| 651 |
if (frameDurations == NULL) |
| 652 |
{ |
| 653 |
strncpy(output.header.err, "Failed to allocate memory for frame durations", sizeof(output.header.err) - 1); |
| 654 |
write_output_message(&output); |
| 655 |
WebPAnimDecoderDelete(dec); |
| 656 |
goto cleanup; |
| 657 |
} |
| 658 |
output.data.params.frameDurations = frameDurations; |
| 659 |
|
| 660 |
// Use a demuxer to get frame durations. |
| 661 |
WebPDemuxer *demux = WebPDemux(&data); |
| 662 |
if (demux != NULL) |
| 663 |
{ |
| 664 |
WebPIterator iter; |
| 665 |
for (uint32_t i = 0; i < anim_info.frame_count; ++i) |
| 666 |
{ |
| 667 |
if (WebPDemuxGetFrame(demux, i + 1, &iter)) |
| 668 |
{ |
| 669 |
frameDurations[i] = iter.duration; |
| 670 |
WebPDemuxReleaseIterator(&iter); |
| 671 |
} |
| 672 |
else |
| 673 |
{ |
| 674 |
frameDurations[i] = 0; |
| 675 |
} |
| 676 |
} |
| 677 |
WebPDemuxDelete(demux); |
| 678 |
} |
| 679 |
|
| 680 |
size_t frame_size = (size_t)anim_info.canvas_width * 4 * anim_info.canvas_height; |
| 681 |
size_t all_frames_size = frame_size * anim_info.frame_count; |
| 682 |
uint8_t *output_buffer = malloc(all_frames_size); |
| 683 |
|
| 684 |
if (output_buffer == NULL) |
| 685 |
{ |
| 686 |
strncpy(output.header.err, "Failed to allocate memory for frames", sizeof(output.header.err) - 1); |
| 687 |
write_output_message(&output); |
| 688 |
WebPAnimDecoderDelete(dec); |
| 689 |
free(frameDurations); |
| 690 |
output.data.params.frameDurations = NULL; |
| 691 |
goto cleanup; |
| 692 |
} |
| 693 |
|
| 694 |
int frame_index = 0; |
| 695 |
while (WebPAnimDecoderHasMoreFrames(dec)) |
| 696 |
{ |
| 697 |
uint8_t *frame_rgba; |
| 698 |
int timestamp; |
| 699 |
if (!WebPAnimDecoderGetNext(dec, &frame_rgba, ×tamp)) |
| 700 |
{ |
| 701 |
break; |
| 702 |
} |
| 703 |
memcpy(output_buffer + frame_index * frame_size, frame_rgba, frame_size); |
| 704 |
frame_index++; |
| 705 |
} |
| 706 |
|
| 707 |
write_output_message(&output); |
| 708 |
|
| 709 |
write_blob((uint32_t)input.header.id, output_buffer, (uint32_t)all_frames_size); |
| 710 |
|
| 711 |
WebPAnimDecoderDelete(dec); |
| 712 |
free(output_buffer); |
| 713 |
free(frameDurations); |
| 714 |
output.data.params.frameDurations = NULL; |
| 715 |
} |
| 716 |
else |
| 717 |
{ |
| 718 |
uint8_t *output_buffer; |
| 719 |
int bytesPerPixel; |
| 720 |
|
| 721 |
output.data.params.hasAlpha = config.input.has_alpha; |
| 722 |
|
| 723 |
if (config.input.has_alpha) |
| 724 |
{ |
| 725 |
output_buffer = WebPDecodeRGBA(blob_data, (size_t)blob_size, &output.data.params.width, &output.data.params.height); |
| 726 |
bytesPerPixel = 4; |
| 727 |
} |
| 728 |
else |
| 729 |
{ |
| 730 |
output_buffer = WebPDecodeRGB(blob_data, (size_t)blob_size, &output.data.params.width, &output.data.params.height); |
| 731 |
bytesPerPixel = 3; |
| 732 |
} |
| 733 |
|
| 734 |
if (output_buffer == NULL) |
| 735 |
{ |
| 736 |
strncpy(output.header.err, "Failed to decode WebP", sizeof(output.header.err) - 1); |
| 737 |
write_output_message(&output); |
| 738 |
goto cleanup; |
| 739 |
} |
| 740 |
|
| 741 |
output.data.params.stride = output.data.params.width * bytesPerPixel; |
| 742 |
|
| 743 |
write_output_message(&output); |
| 744 |
|
| 745 |
size_t output_size = (size_t)(output.data.params.stride * output.data.params.height); |
| 746 |
write_blob((uint32_t)input.header.id, output_buffer, (uint32_t)output_size); |
| 747 |
|
| 748 |
WebPFree(output_buffer); |
| 749 |
} |
| 750 |
} |
| 751 |
else if (strcmp(input.header.command, "config") == 0) |
| 752 |
{ |
| 753 |
int width, height; |
| 754 |
if (!WebPGetInfo(blob_data, (size_t)blob_size, &width, &height)) |
| 755 |
{ |
| 756 |
strncpy(output.header.err, "Failed to get WebP info", sizeof(output.header.err) - 1); |
| 757 |
write_output_message(&output); |
| 758 |
goto cleanup; |
| 759 |
} |
| 760 |
|
| 761 |
output.data.params.width = width; |
| 762 |
output.data.params.height = height; |
| 763 |
|
| 764 |
write_output_message(&output); |
| 765 |
} |
| 766 |
else if (strcmp(input.header.command, "encodeNRGBA") == 0) |
| 767 |
{ |
| 768 |
WebPConfig config; |
| 769 |
if (!initEncoderConfig(&config, input.data.options)) |
| 770 |
{ |
| 771 |
strncpy(output.header.err, "Error initializing WebPConfig", sizeof(output.header.err) - 1); |
| 772 |
write_output_message(&output); |
| 773 |
goto cleanup; |
| 774 |
} |
| 775 |
|
| 776 |
size_t output_size = 0; |
| 777 |
uint8_t *webp_data = NULL; |
| 778 |
|
| 779 |
if (input.data.params.frameDurations != NULL) |
| 780 |
{ |
| 781 |
webp_data = encodeNRGBAAnimated(&config, input.data.params, blob_data, &output_size); |
| 782 |
} |
| 783 |
else |
| 784 |
{ |
| 785 |
webp_data = encodeNRGBA(&config, blob_data, input.data.params.width, input.data.params.height, input.data.params.stride, &output_size); |
| 786 |
} |
| 787 |
|
| 788 |
if (webp_data == NULL) |
| 789 |
{ |
| 790 |
strncpy(output.header.err, "Error encoding NRGBA to WebP", sizeof(output.header.err) - 1); |
| 791 |
write_output_message(&output); |
| 792 |
goto cleanup; |
| 793 |
} |
| 794 |
|
| 795 |
write_output_message(&output); |
| 796 |
write_blob((uint32_t)input.header.id, webp_data, (uint32_t)output_size); |
| 797 |
free(webp_data); |
| 798 |
} |
| 799 |
else if (strcmp(input.header.command, "encodeGray") == 0) |
| 800 |
{ |
| 801 |
WebPConfig config; |
| 802 |
if (!initEncoderConfig(&config, input.data.options)) |
| 803 |
{ |
| 804 |
strncpy(output.header.err, "Error initializing WebPConfig", sizeof(output.header.err) - 1); |
| 805 |
write_output_message(&output); |
| 806 |
goto cleanup; |
| 807 |
} |
| 808 |
|
| 809 |
size_t output_size = 0; |
| 810 |
uint8_t *webp_data = encodeGray(&config, blob_data, input.data.params.width, input.data.params.height, input.data.params.stride, &output_size); |
| 811 |
if (webp_data == NULL) |
| 812 |
{ |
| 813 |
strncpy(output.header.err, "Error encoding Gray to WebP", sizeof(output.header.err) - 1); |
| 814 |
write_output_message(&output); |
| 815 |
goto cleanup; |
| 816 |
} |
| 817 |
|
| 818 |
write_output_message(&output); |
| 819 |
write_blob((uint32_t)input.header.id, webp_data, (uint32_t)output_size); |
| 820 |
|
| 821 |
free(webp_data); |
| 822 |
} |
| 823 |
else |
| 824 |
{ |
| 825 |
snprintf(output.header.err, sizeof(output.header.err), "Unknown command: %s", input.header.command); |
| 826 |
write_output_message(&output); |
| 827 |
} |
| 828 |
|
| 829 |
cleanup: |
| 830 |
if (blob_data != NULL) |
| 831 |
{ |
| 832 |
free(blob_data); |
| 833 |
} |
| 834 |
if (input.data.params.frameDurations != NULL) |
| 835 |
{ |
| 836 |
free(input.data.params.frameDurations); |
| 837 |
} |
| 838 |
} |
| 839 |
} |