Coverage Report

Created: 2026-11-12 07:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/src/mux/anim_encode.c
Line
Count
Source
1
// Copyright 2014 Google Inc. All Rights Reserved.
2
//
3
// Use of this source code is governed by a BSD-style license
4
// that can be found in the COPYING file in the root of the source
5
// tree. An additional intellectual property rights grant can be found
6
// in the file PATENTS. All contributing project authors may
7
// be found in the AUTHORS file in the root of the source tree.
8
// -----------------------------------------------------------------------------
9
//
10
//  AnimEncoder implementation.
11
//
12
13
#include <assert.h>
14
#include <limits.h>
15
#include <math.h>    // for pow()
16
#include <stddef.h>  // for ptrdiff_t, size_t
17
#include <stdio.h>
18
#include <stdlib.h>  // for abs()
19
#include <string.h>
20
21
#include "src/mux/animi.h"
22
#include "src/utils/utils.h"
23
#include "src/webp/decode.h"
24
#include "src/webp/encode.h"
25
#include "src/webp/format_constants.h"
26
#include "src/webp/mux.h"
27
#include "src/webp/mux_types.h"
28
#include "src/webp/types.h"
29
30
#if defined(_MSC_VER) && _MSC_VER < 1900
31
#define snprintf _snprintf
32
#endif
33
34
0
#define ERROR_STR_MAX_LENGTH 100
35
36
//------------------------------------------------------------------------------
37
// Internal structs.
38
39
// Stores frame rectangle dimensions.
40
typedef struct {
41
  int x_offset, y_offset, width, height;
42
} FrameRectangle;
43
44
// Used to store two candidates of encoded data for an animation frame. One of
45
// the two will be chosen later.
46
typedef struct {
47
  WebPMuxFrameInfo sub_frame;  // Encoded frame rectangle.
48
  WebPMuxFrameInfo key_frame;  // Encoded frame if it is a keyframe.
49
  int is_key_frame;            // True if 'key_frame' has been chosen.
50
} EncodedFrame;
51
52
struct WebPAnimEncoder {
53
  const int canvas_width;                // Canvas width.
54
  const int canvas_height;               // Canvas height.
55
  const WebPAnimEncoderOptions options;  // Global encoding options.
56
57
  WebPConfig last_config;           // Cached in case a re-encode is needed.
58
  WebPConfig last_config_reversed;  // If 'last_config' uses lossless, then
59
                                    // this config uses lossy and vice versa;
60
                                    // only valid if 'options.allow_mixed'
61
                                    // is true.
62
63
  WebPPicture* curr_canvas;  // Only pointer; we don't own memory.
64
65
  // Canvas buffers.
66
  WebPPicture curr_canvas_copy;   // Possibly modified current canvas.
67
  int curr_canvas_copy_modified;  // True if pixels in 'curr_canvas_copy'
68
                                  // differ from those in 'curr_canvas'.
69
70
  // Previous canvas (original animation).
71
  // Also used temporarily to store canvas_carryover_disposed pixel values.
72
  WebPPicture prev_canvas;
73
  // canvas_carryover contains the previous original input frame's pixels
74
  // (prev_canvas) with some parts carried over from even earlier original input
75
  // frames, to approximate the current state of the canvas at decoding.
76
  // canvas_carryover is  compared to curr_canvas at encoding to see what parts
77
  // of the current frame are similar enough to not be explicitly encoded.
78
  WebPPicture canvas_carryover;
79
80
  // Buffer of the size of a subframe, with one boolean value per pixel.
81
  // Used when encoding a subframe to remember the pixels that may change when
82
  // decoding that frame (0 means the pixel is explicitly encoded, 1 means
83
  // carrying over the pixel value of the previous frame).
84
  uint8_t* candidate_carryover_mask;
85
  // True if at least one pixel is carried over by the best candidate subframe.
86
  int best_candidate_carries_over;
87
  // Same as candidate_carryover_mask but for the best candidate subframe.
88
  uint8_t* best_candidate_carryover_mask;
89
90
  // Encoded data.
91
  EncodedFrame* encoded_frames;  // Array of encoded frames.
92
  size_t size;                   // Number of allocated frames.
93
  size_t start;                  // Frame start index.
94
  size_t count;                  // Number of valid frames.
95
  size_t flush_count;            // If >0, 'flush_count' frames starting from
96
                                 // 'start' are ready to be added to mux.
97
98
  // keyframe related.
99
  int64_t best_delta;         // min(canvas size - frame size) over the frames.
100
                              // Can be negative in certain cases due to
101
                              // transparent pixels in a frame.
102
  int keyframe;               // Index of selected keyframe relative to 'start'.
103
  int count_since_key_frame;  // Frames seen since the last keyframe.
104
105
  int first_timestamp;           // Timestamp of the first frame.
106
  int prev_timestamp;            // Timestamp of the last added frame.
107
  int prev_candidate_undecided;  // True if it's not yet decided if previous
108
                                 // frame would be a subframe or a keyframe.
109
110
  FrameRectangle prev_rect;  // Previous WebP frame rectangle. Only valid if
111
                             // prev_candidate_undecided is true.
112
113
  // Misc.
114
  int is_first_frame;  // True if first frame is yet to be added/being added.
115
  int got_null_frame;  // True if WebPAnimEncoderAdd() has already been called
116
                       // with a NULL frame.
117
118
  size_t in_frame_count;   // Number of input frames processed so far.
119
  size_t out_frame_count;  // Number of frames added to mux so far. This may be
120
                           // different from 'in_frame_count' due to merging.
121
122
  WebPMux* mux;  // Muxer to assemble the WebP bitstream.
123
  char error_str[ERROR_STR_MAX_LENGTH];  // Error string. Empty if no error.
124
};
125
126
// -----------------------------------------------------------------------------
127
// Life of WebPAnimEncoder object.
128
129
0
#define DELTA_INFINITY (1ULL << 32)
130
0
#define KEYFRAME_NONE (-1)
131
132
// Reset the counters in the WebPAnimEncoder.
133
0
static void ResetCounters(WebPAnimEncoder* const enc) {
134
0
  enc->start = 0;
135
0
  enc->count = 0;
136
0
  enc->flush_count = 0;
137
0
  enc->best_delta = DELTA_INFINITY;
138
0
  enc->keyframe = KEYFRAME_NONE;
139
0
}
140
141
0
static void DisableKeyframes(WebPAnimEncoderOptions* const enc_options) {
142
0
  enc_options->kmax = INT_MAX;
143
0
  enc_options->kmin = enc_options->kmax - 1;
144
0
}
145
146
0
#define MAX_CACHED_FRAMES 30
147
148
0
static void SanitizeEncoderOptions(WebPAnimEncoderOptions* const enc_options) {
149
0
  int print_warning = enc_options->verbose;
150
151
0
  if (enc_options->minimize_size) {
152
0
    DisableKeyframes(enc_options);
153
0
  }
154
155
0
  if (enc_options->kmax == 1) {  // All frames will be keyframes.
156
0
    enc_options->kmin = 0;
157
0
    enc_options->kmax = 0;
158
0
    return;
159
0
  } else if (enc_options->kmax <= 0) {
160
0
    DisableKeyframes(enc_options);
161
0
    print_warning = 0;
162
0
  }
163
164
0
  if (enc_options->kmin >= enc_options->kmax) {
165
0
    enc_options->kmin = enc_options->kmax - 1;
166
0
    if (print_warning) {
167
0
      fprintf(stderr, "WARNING: Setting kmin = %d, so that kmin < kmax.\n",
168
0
              enc_options->kmin);
169
0
    }
170
0
  } else {
171
0
    const int kmin_limit = enc_options->kmax / 2 + 1;
172
0
    if (enc_options->kmin < kmin_limit && kmin_limit < enc_options->kmax) {
173
      // This ensures that enc.keyframe + kmin >= kmax is always true. So, we
174
      // can flush all the frames in the 'count_since_key_frame == kmax' case.
175
0
      enc_options->kmin = kmin_limit;
176
0
      if (print_warning) {
177
0
        fprintf(stderr,
178
0
                "WARNING: Setting kmin = %d, so that kmin >= kmax / 2 + 1.\n",
179
0
                enc_options->kmin);
180
0
      }
181
0
    }
182
0
  }
183
  // Limit the max number of frames that are allocated.
184
  // Note: kmax >= 2 here, so 'kmax - MAX_CACHED_FRAMES' cannot overflow (unlike
185
  // 'kmax - kmin' for a very negative user-provided kmin).
186
0
  if (enc_options->kmin < enc_options->kmax - MAX_CACHED_FRAMES) {
187
0
    enc_options->kmin = enc_options->kmax - MAX_CACHED_FRAMES;
188
0
    if (print_warning) {
189
0
      fprintf(stderr,
190
0
              "WARNING: Setting kmin = %d, so that kmax - kmin <= %d.\n",
191
0
              enc_options->kmin, MAX_CACHED_FRAMES);
192
0
    }
193
0
  }
194
0
  assert(enc_options->kmin < enc_options->kmax);
195
0
}
196
197
#undef MAX_CACHED_FRAMES
198
199
0
static void DefaultEncoderOptions(WebPAnimEncoderOptions* const enc_options) {
200
0
  enc_options->anim_params.loop_count = 0;
201
0
  enc_options->anim_params.bgcolor = 0xffffffff;  // White.
202
0
  enc_options->minimize_size = 0;
203
0
  DisableKeyframes(enc_options);
204
0
  enc_options->allow_mixed = 0;
205
0
  enc_options->verbose = 0;
206
0
}
207
208
int WebPAnimEncoderOptionsInitInternal(WebPAnimEncoderOptions* enc_options,
209
0
                                       int abi_version) {
210
0
  if (enc_options == NULL ||
211
0
      WEBP_ABI_IS_INCOMPATIBLE(abi_version, WEBP_MUX_ABI_VERSION)) {
212
0
    return 0;
213
0
  }
214
0
  DefaultEncoderOptions(enc_options);
215
0
  return 1;
216
0
}
217
218
// This value is used to match a later call to WebPReplaceTransparentPixels(),
219
// making it a no-op for lossless (see WebPEncode()).
220
0
#define TRANSPARENT_COLOR 0x00000000
221
222
static void ClearRectangle(WebPPicture* const picture, int left, int top,
223
0
                           int width, int height) {
224
0
  int j;
225
0
  for (j = top; j < top + height; ++j) {
226
0
    uint32_t* const dst = picture->argb + (ptrdiff_t)j * picture->argb_stride;
227
0
    int i;
228
0
    for (i = left; i < left + width; ++i) {
229
0
      dst[i] = TRANSPARENT_COLOR;
230
0
    }
231
0
  }
232
0
}
233
234
static void WebPUtilClearPic(WebPPicture* const picture,
235
0
                             const FrameRectangle* const rect) {
236
0
  if (rect != NULL) {
237
0
    ClearRectangle(picture, rect->x_offset, rect->y_offset, rect->width,
238
0
                   rect->height);
239
0
  } else {
240
0
    ClearRectangle(picture, 0, 0, picture->width, picture->height);
241
0
  }
242
0
}
243
244
0
static void MarkNoError(WebPAnimEncoder* const enc) {
245
0
  enc->error_str[0] = '\0';  // Empty string.
246
0
}
247
248
0
static void MarkError(WebPAnimEncoder* const enc, const char* str) {
249
0
  if (snprintf(enc->error_str, ERROR_STR_MAX_LENGTH, "%s.", str) < 0) {
250
0
    assert(0);  // FIX ME!
251
0
  }
252
0
}
253
254
static void MarkError2(WebPAnimEncoder* const enc, const char* str,
255
0
                       int error_code) {
256
0
  if (snprintf(enc->error_str, ERROR_STR_MAX_LENGTH, "%s: %d.", str,
257
0
               error_code) < 0) {
258
0
    assert(0);  // FIX ME!
259
0
  }
260
0
}
261
262
WebPAnimEncoder* WebPAnimEncoderNewInternal(
263
    int width, int height, const WebPAnimEncoderOptions* enc_options,
264
0
    int abi_version) {
265
0
  WebPAnimEncoder* enc;
266
267
0
  if (WEBP_ABI_IS_INCOMPATIBLE(abi_version, WEBP_MUX_ABI_VERSION)) {
268
0
    return NULL;
269
0
  }
270
0
  if (width <= 0 || height <= 0 ||
271
0
      (width * (uint64_t)height) >= MAX_IMAGE_AREA) {
272
0
    return NULL;
273
0
  }
274
275
0
  enc = (WebPAnimEncoder*)WebPSafeCalloc(1, sizeof(*enc));
276
0
  if (enc == NULL) return NULL;
277
0
  MarkNoError(enc);
278
279
  // Dimensions and options.
280
0
  *(int*)&enc->canvas_width = width;
281
0
  *(int*)&enc->canvas_height = height;
282
0
  if (enc_options != NULL) {
283
0
    *(WebPAnimEncoderOptions*)&enc->options = *enc_options;
284
0
    SanitizeEncoderOptions((WebPAnimEncoderOptions*)&enc->options);
285
0
  } else {
286
0
    DefaultEncoderOptions((WebPAnimEncoderOptions*)&enc->options);
287
0
  }
288
289
  // Canvas buffers.
290
0
  if (!WebPPictureInit(&enc->curr_canvas_copy) ||
291
0
      !WebPPictureInit(&enc->prev_canvas) ||
292
0
      !WebPPictureInit(&enc->canvas_carryover)) {
293
0
    goto Err;
294
0
  }
295
0
  enc->curr_canvas_copy.width = width;
296
0
  enc->curr_canvas_copy.height = height;
297
0
  enc->curr_canvas_copy.use_argb = 1;
298
0
  if (!WebPPictureAlloc(&enc->curr_canvas_copy) ||
299
0
      !WebPPictureCopy(&enc->curr_canvas_copy, &enc->prev_canvas) ||
300
0
      !WebPPictureCopy(&enc->curr_canvas_copy, &enc->canvas_carryover)) {
301
0
    goto Err;
302
0
  }
303
0
  WebPUtilClearPic(&enc->canvas_carryover, NULL);
304
0
  enc->curr_canvas_copy_modified = 1;
305
306
  // Allocate for the whole canvas so that it can be reused for any subframe.
307
0
  enc->candidate_carryover_mask = (uint8_t*)WebPSafeMalloc(
308
0
      width * (uint64_t)height, sizeof(*enc->candidate_carryover_mask));
309
0
  if (enc->candidate_carryover_mask == NULL) goto Err;
310
0
  enc->best_candidate_carryover_mask = (uint8_t*)WebPSafeMalloc(
311
0
      width * (uint64_t)height, sizeof(*enc->best_candidate_carryover_mask));
312
0
  if (enc->best_candidate_carryover_mask == NULL) goto Err;
313
314
  // Encoded frames.
315
0
  ResetCounters(enc);
316
  // Note: one extra storage is for the previous frame.
317
0
  enc->size = enc->options.kmax - enc->options.kmin + 1;
318
  // We need space for at least 2 frames. But when kmin, kmax are both zero,
319
  // enc->size will be 1. So we handle that special case below.
320
0
  if (enc->size < 2) enc->size = 2;
321
0
  enc->encoded_frames =
322
0
      (EncodedFrame*)WebPSafeCalloc(enc->size, sizeof(*enc->encoded_frames));
323
0
  if (enc->encoded_frames == NULL) goto Err;
324
325
0
  enc->mux = WebPMuxNew();
326
0
  if (enc->mux == NULL) goto Err;
327
328
0
  enc->count_since_key_frame = 0;
329
0
  enc->first_timestamp = 0;
330
0
  enc->prev_timestamp = 0;
331
0
  enc->prev_candidate_undecided = 0;
332
0
  enc->is_first_frame = 1;
333
0
  enc->got_null_frame = 0;
334
335
0
  return enc;  // All OK.
336
337
0
Err:
338
0
  WebPAnimEncoderDelete(enc);
339
0
  return NULL;
340
0
}
341
342
// Release the data contained by 'encoded_frame'.
343
0
static void FrameRelease(EncodedFrame* const encoded_frame) {
344
0
  if (encoded_frame != NULL) {
345
0
    WebPDataClear(&encoded_frame->sub_frame.bitstream);
346
0
    WebPDataClear(&encoded_frame->key_frame.bitstream);
347
0
    memset(encoded_frame, 0, sizeof(*encoded_frame));
348
0
  }
349
0
}
350
351
0
void WebPAnimEncoderDelete(WebPAnimEncoder* enc) {
352
0
  if (enc != NULL) {
353
0
    WebPPictureFree(&enc->curr_canvas_copy);
354
0
    WebPPictureFree(&enc->prev_canvas);
355
0
    WebPPictureFree(&enc->canvas_carryover);
356
0
    WebPSafeFree(enc->candidate_carryover_mask);
357
0
    WebPSafeFree(enc->best_candidate_carryover_mask);
358
0
    if (enc->encoded_frames != NULL) {
359
0
      size_t i;
360
0
      for (i = 0; i < enc->size; ++i) {
361
0
        FrameRelease(&enc->encoded_frames[i]);
362
0
      }
363
0
      WebPSafeFree(enc->encoded_frames);
364
0
    }
365
0
    WebPMuxDelete(enc->mux);
366
0
    WebPSafeFree(enc);
367
0
  }
368
0
}
369
370
// -----------------------------------------------------------------------------
371
// Frame addition.
372
373
// Returns cached frame at the given 'position'.
374
static EncodedFrame* GetFrame(const WebPAnimEncoder* const enc,
375
0
                              size_t position) {
376
0
  assert(enc->start + position < enc->size);
377
0
  return &enc->encoded_frames[enc->start + position];
378
0
}
379
380
typedef int (*ComparePixelsFunc)(const uint32_t*, int, const uint32_t*, int,
381
                                 int, int);
382
383
// Returns true if 'length' number of pixels in 'src' and 'dst' are equal,
384
// assuming the given step sizes between pixels.
385
// 'max_allowed_diff' is unused and only there to allow function pointer use.
386
static WEBP_INLINE int ComparePixelsLossless(const uint32_t* src, int src_step,
387
                                             const uint32_t* dst, int dst_step,
388
0
                                             int length, int max_allowed_diff) {
389
0
  (void)max_allowed_diff;
390
0
  assert(length > 0);
391
0
  while (length-- > 0) {
392
0
    if (*src != *dst) {
393
0
      return 0;
394
0
    }
395
0
    src += src_step;
396
0
    dst += dst_step;
397
0
  }
398
0
  return 1;
399
0
}
400
401
// Helper to check if each channel in 'src' and 'dst' is at most off by
402
// 'max_allowed_diff'.
403
static WEBP_INLINE int PixelsAreSimilar(uint32_t src, uint32_t dst,
404
0
                                        int max_allowed_diff) {
405
0
  const int src_a = (src >> 24) & 0xff;
406
0
  const int src_r = (src >> 16) & 0xff;
407
0
  const int src_g = (src >> 8) & 0xff;
408
0
  const int src_b = (src >> 0) & 0xff;
409
0
  const int dst_a = (dst >> 24) & 0xff;
410
0
  const int dst_r = (dst >> 16) & 0xff;
411
0
  const int dst_g = (dst >> 8) & 0xff;
412
0
  const int dst_b = (dst >> 0) & 0xff;
413
414
0
  return (src_a == dst_a) &&
415
0
         (abs(src_r - dst_r) * dst_a <= (max_allowed_diff * 255)) &&
416
0
         (abs(src_g - dst_g) * dst_a <= (max_allowed_diff * 255)) &&
417
0
         (abs(src_b - dst_b) * dst_a <= (max_allowed_diff * 255));
418
0
}
419
420
// Returns true if 'length' number of pixels in 'src' and 'dst' are within an
421
// error bound, assuming the given step sizes between pixels.
422
static WEBP_INLINE int ComparePixelsLossy(const uint32_t* src, int src_step,
423
                                          const uint32_t* dst, int dst_step,
424
0
                                          int length, int max_allowed_diff) {
425
0
  assert(length > 0);
426
0
  while (length-- > 0) {
427
0
    if (!PixelsAreSimilar(*src, *dst, max_allowed_diff)) {
428
0
      return 0;
429
0
    }
430
0
    src += src_step;
431
0
    dst += dst_step;
432
0
  }
433
0
  return 1;
434
0
}
435
436
0
static int IsEmptyRect(const FrameRectangle* const rect) {
437
0
  return (rect->width == 0) || (rect->height == 0);
438
0
}
439
440
0
static int QualityToMaxDiff(float quality) {
441
0
  const double val = pow(quality / 100., 0.5);
442
0
  const double max_diff = 31 * (1 - val) + 1 * val;
443
0
  return (int)(max_diff + 0.5);
444
0
}
445
446
// Assumes that an initial valid guess of change rectangle 'rect' is passed.
447
static void MinimizeChangeRectangle(const WebPPicture* const src,
448
                                    const WebPPicture* const dst,
449
                                    FrameRectangle* const rect, int is_lossless,
450
0
                                    float quality) {
451
0
  int i, j;
452
0
  const ComparePixelsFunc compare_pixels =
453
0
      is_lossless ? ComparePixelsLossless : ComparePixelsLossy;
454
0
  const int max_allowed_diff_lossy = QualityToMaxDiff(quality);
455
0
  const int max_allowed_diff = is_lossless ? 0 : max_allowed_diff_lossy;
456
457
  // Assumption/correctness checks.
458
0
  assert(src->width == dst->width && src->height == dst->height);
459
0
  assert(rect->x_offset + rect->width <= dst->width);
460
0
  assert(rect->y_offset + rect->height <= dst->height);
461
0
  if (IsEmptyRect(rect)) goto NoChange;
462
463
  // Left boundary.
464
0
  for (i = rect->x_offset; i < rect->x_offset + rect->width; ++i) {
465
0
    const uint32_t* const src_argb =
466
0
        &src->argb[(ptrdiff_t)rect->y_offset * src->argb_stride + i];
467
0
    const uint32_t* const dst_argb =
468
0
        &dst->argb[(ptrdiff_t)rect->y_offset * dst->argb_stride + i];
469
0
    if (compare_pixels(src_argb, src->argb_stride, dst_argb, dst->argb_stride,
470
0
                       rect->height, max_allowed_diff)) {
471
0
      --rect->width;  // Redundant column.
472
0
      ++rect->x_offset;
473
0
    } else {
474
0
      break;
475
0
    }
476
0
  }
477
0
  if (rect->width == 0) goto NoChange;
478
479
  // Right boundary.
480
0
  for (i = rect->x_offset + rect->width - 1; i >= rect->x_offset; --i) {
481
0
    const uint32_t* const src_argb =
482
0
        &src->argb[(ptrdiff_t)rect->y_offset * src->argb_stride + i];
483
0
    const uint32_t* const dst_argb =
484
0
        &dst->argb[(ptrdiff_t)rect->y_offset * dst->argb_stride + i];
485
0
    if (compare_pixels(src_argb, src->argb_stride, dst_argb, dst->argb_stride,
486
0
                       rect->height, max_allowed_diff)) {
487
0
      --rect->width;  // Redundant column.
488
0
    } else {
489
0
      break;
490
0
    }
491
0
  }
492
0
  if (rect->width == 0) goto NoChange;
493
494
  // Top boundary.
495
0
  for (j = rect->y_offset; j < rect->y_offset + rect->height; ++j) {
496
0
    const uint32_t* const src_argb =
497
0
        &src->argb[(ptrdiff_t)j * src->argb_stride + rect->x_offset];
498
0
    const uint32_t* const dst_argb =
499
0
        &dst->argb[(ptrdiff_t)j * dst->argb_stride + rect->x_offset];
500
0
    if (compare_pixels(src_argb, 1, dst_argb, 1, rect->width,
501
0
                       max_allowed_diff)) {
502
0
      --rect->height;  // Redundant row.
503
0
      ++rect->y_offset;
504
0
    } else {
505
0
      break;
506
0
    }
507
0
  }
508
0
  if (rect->height == 0) goto NoChange;
509
510
  // Bottom boundary.
511
0
  for (j = rect->y_offset + rect->height - 1; j >= rect->y_offset; --j) {
512
0
    const uint32_t* const src_argb =
513
0
        &src->argb[(ptrdiff_t)j * src->argb_stride + rect->x_offset];
514
0
    const uint32_t* const dst_argb =
515
0
        &dst->argb[(ptrdiff_t)j * dst->argb_stride + rect->x_offset];
516
0
    if (compare_pixels(src_argb, 1, dst_argb, 1, rect->width,
517
0
                       max_allowed_diff)) {
518
0
      --rect->height;  // Redundant row.
519
0
    } else {
520
0
      break;
521
0
    }
522
0
  }
523
0
  if (rect->height == 0) goto NoChange;
524
525
0
  if (IsEmptyRect(rect)) {
526
0
  NoChange:
527
0
    rect->x_offset = 0;
528
0
    rect->y_offset = 0;
529
0
    rect->width = 0;
530
0
    rect->height = 0;
531
0
  }
532
0
}
533
534
// Snap rectangle to even offsets (and adjust dimensions if needed).
535
0
static WEBP_INLINE void SnapToEvenOffsets(FrameRectangle* const rect) {
536
0
  rect->width += (rect->x_offset & 1);
537
0
  rect->height += (rect->y_offset & 1);
538
0
  rect->x_offset &= ~1;
539
0
  rect->y_offset &= ~1;
540
0
}
541
542
typedef struct {
543
  int should_try;               // Should try this set of parameters.
544
  int empty_rect_allowed;       // Frame with empty rectangle can be skipped.
545
  FrameRectangle rect_ll;       // Frame rectangle for lossless compression.
546
  WebPPicture sub_frame_ll;     // subframe pic for lossless compression.
547
  FrameRectangle rect_lossy;    // Frame rectangle for lossy compression.
548
                                // Could be smaller than 'rect_ll' as pixels
549
                                // with small diffs can be ignored.
550
  WebPPicture sub_frame_lossy;  // subframe pic for lossy compression.
551
} SubFrameParams;
552
553
WEBP_NODISCARD static int SubFrameParamsInit(SubFrameParams* const params,
554
                                             int should_try,
555
0
                                             int empty_rect_allowed) {
556
0
  params->should_try = should_try;
557
0
  params->empty_rect_allowed = empty_rect_allowed;
558
0
  if (!WebPPictureInit(&params->sub_frame_ll) ||
559
0
      !WebPPictureInit(&params->sub_frame_lossy)) {
560
0
    return 0;
561
0
  }
562
0
  return 1;
563
0
}
564
565
0
static void SubFrameParamsFree(SubFrameParams* const params) {
566
0
  WebPPictureFree(&params->sub_frame_ll);
567
0
  WebPPictureFree(&params->sub_frame_lossy);
568
0
}
569
570
// Given previous and current canvas, picks the optimal rectangle for the
571
// current frame based on 'is_lossless' and other parameters. Assumes that the
572
// initial guess 'rect' is valid.
573
WEBP_NODISCARD static int GetSubRect(const WebPPicture* const prev_canvas,
574
                                     const WebPPicture* const curr_canvas,
575
                                     int is_key_frame, int is_first_frame,
576
                                     int empty_rect_allowed, int is_lossless,
577
                                     float quality, FrameRectangle* const rect,
578
0
                                     WebPPicture* const sub_frame) {
579
0
  if (!is_key_frame || is_first_frame) {  // Optimize frame rectangle.
580
    // Note: This behaves as expected for first frame, as 'prev_canvas' is
581
    // initialized to a fully transparent canvas in the beginning.
582
0
    MinimizeChangeRectangle(prev_canvas, curr_canvas, rect, is_lossless,
583
0
                            quality);
584
0
  }
585
586
0
  if (IsEmptyRect(rect)) {
587
0
    if (empty_rect_allowed) {  // No need to get 'sub_frame'.
588
0
      return 1;
589
0
    } else {  // Force a 1x1 rectangle.
590
0
      rect->width = 1;
591
0
      rect->height = 1;
592
0
      assert(rect->x_offset == 0);
593
0
      assert(rect->y_offset == 0);
594
0
    }
595
0
  }
596
597
0
  SnapToEvenOffsets(rect);
598
0
  return WebPPictureView(curr_canvas, rect->x_offset, rect->y_offset,
599
0
                         rect->width, rect->height, sub_frame);
600
0
}
601
602
// Picks optimal frame rectangle for both lossless and lossy compression. The
603
// initial guess for frame rectangles will be the full canvas.
604
WEBP_NODISCARD static int GetSubRects(const WebPPicture* const prev_canvas,
605
                                      const WebPPicture* const curr_canvas,
606
                                      int is_key_frame, int is_first_frame,
607
                                      float quality,
608
0
                                      SubFrameParams* const params) {
609
  // Lossless frame rectangle.
610
0
  params->rect_ll.x_offset = 0;
611
0
  params->rect_ll.y_offset = 0;
612
0
  params->rect_ll.width = curr_canvas->width;
613
0
  params->rect_ll.height = curr_canvas->height;
614
0
  if (!GetSubRect(prev_canvas, curr_canvas, is_key_frame, is_first_frame,
615
0
                  params->empty_rect_allowed, 1, quality, &params->rect_ll,
616
0
                  &params->sub_frame_ll)) {
617
0
    return 0;
618
0
  }
619
  // Lossy frame rectangle.
620
0
  params->rect_lossy = params->rect_ll;  // seed with lossless rect.
621
0
  return GetSubRect(prev_canvas, curr_canvas, is_key_frame, is_first_frame,
622
0
                    params->empty_rect_allowed, 0, quality, &params->rect_lossy,
623
0
                    &params->sub_frame_lossy);
624
0
}
625
626
0
static WEBP_INLINE int clip(int64_t v, int min_v, int max_v) {
627
0
  return (v < min_v) ? min_v : (v > max_v) ? max_v : (int)v;
628
0
}
629
630
// Validates that 'picture' has valid dimensions, colorspace, strides, and
631
// allocated plane buffers.
632
WEBP_NODISCARD static int ValidateAllocatedPicture(
633
0
    const WebPPicture* const picture) {
634
0
  WebPPicture view;
635
0
  if (picture == NULL) return 0;
636
0
  return WebPPictureView(picture, 0, 0, picture->width, picture->height, &view);
637
0
}
638
639
int WebPAnimEncoderRefineRect(const WebPPicture* const prev_canvas,
640
                              const WebPPicture* const curr_canvas,
641
                              int is_lossless, float quality,
642
                              int* const x_offset, int* const y_offset,
643
0
                              int* const width, int* const height) {
644
0
  FrameRectangle rect;
645
0
  int right, left, bottom, top;
646
0
  if (!ValidateAllocatedPicture(prev_canvas) ||
647
0
      !ValidateAllocatedPicture(curr_canvas) ||
648
0
      prev_canvas->width != curr_canvas->width ||
649
0
      prev_canvas->height != curr_canvas->height || !prev_canvas->use_argb ||
650
0
      !curr_canvas->use_argb || x_offset == NULL || y_offset == NULL ||
651
0
      width == NULL || height == NULL || *width <= 0 || *height <= 0 ||
652
0
      !(quality >= 0.f && quality <= 100.f)) {
653
0
    return 0;
654
0
  }
655
0
  right = clip((int64_t)*x_offset + *width, 0, curr_canvas->width);
656
0
  left = clip(*x_offset, 0, curr_canvas->width);
657
0
  bottom = clip((int64_t)*y_offset + *height, 0, curr_canvas->height);
658
0
  top = clip(*y_offset, 0, curr_canvas->height);
659
0
  rect.x_offset = left;
660
0
  rect.y_offset = top;
661
0
  rect.width = clip(right - left, 0, curr_canvas->width - rect.x_offset);
662
0
  rect.height = clip(bottom - top, 0, curr_canvas->height - rect.y_offset);
663
0
  MinimizeChangeRectangle(prev_canvas, curr_canvas, &rect, is_lossless,
664
0
                          quality);
665
0
  SnapToEvenOffsets(&rect);
666
0
  *x_offset = rect.x_offset;
667
0
  *y_offset = rect.y_offset;
668
0
  *width = rect.width;
669
0
  *height = rect.height;
670
0
  return 1;
671
0
}
672
673
static void DisposeFrameRectangle(int dispose_method,
674
                                  const FrameRectangle* const rect,
675
0
                                  WebPPicture* const curr_canvas) {
676
0
  assert(rect != NULL);
677
0
  if (dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) {
678
0
    WebPUtilClearPic(curr_canvas, rect);
679
0
  }
680
0
}
681
682
0
static size_t RectArea(const FrameRectangle* const rect) {
683
0
  return (size_t)rect->width * rect->height;
684
0
}
685
686
WEBP_NODISCARD static int IsLosslessBlendingPossible(
687
    const WebPPicture* const src, const WebPPicture* const dst,
688
0
    const FrameRectangle* const rect) {
689
0
  int i, j;
690
0
  assert(src->width == dst->width && src->height == dst->height);
691
0
  assert(rect->x_offset + rect->width <= dst->width);
692
0
  assert(rect->y_offset + rect->height <= dst->height);
693
0
  for (j = rect->y_offset; j < rect->y_offset + rect->height; ++j) {
694
0
    for (i = rect->x_offset; i < rect->x_offset + rect->width; ++i) {
695
0
      const uint32_t src_pixel = src->argb[(ptrdiff_t)j * src->argb_stride + i];
696
0
      const uint32_t dst_pixel = dst->argb[(ptrdiff_t)j * dst->argb_stride + i];
697
0
      const uint32_t dst_alpha = dst_pixel >> 24;
698
0
      if (dst_alpha != 0xff && src_pixel != dst_pixel) {
699
        // In this case, if we use blending, we can't attain the desired
700
        // 'dst_pixel' value for this pixel. So, blending is not possible.
701
0
        return 0;
702
0
      }
703
0
    }
704
0
  }
705
0
  return 1;
706
0
}
707
708
WEBP_NODISCARD static int IsLossyBlendingPossible(
709
    const WebPPicture* const src, const WebPPicture* const dst,
710
0
    const FrameRectangle* const rect, float quality) {
711
0
  const int max_allowed_diff_lossy = QualityToMaxDiff(quality);
712
0
  int i, j;
713
0
  assert(src->width == dst->width && src->height == dst->height);
714
0
  assert(rect->x_offset + rect->width <= dst->width);
715
0
  assert(rect->y_offset + rect->height <= dst->height);
716
0
  for (j = rect->y_offset; j < rect->y_offset + rect->height; ++j) {
717
0
    for (i = rect->x_offset; i < rect->x_offset + rect->width; ++i) {
718
0
      const uint32_t src_pixel = src->argb[(ptrdiff_t)j * src->argb_stride + i];
719
0
      const uint32_t dst_pixel = dst->argb[(ptrdiff_t)j * dst->argb_stride + i];
720
0
      const uint32_t dst_alpha = dst_pixel >> 24;
721
0
      if (dst_alpha != 0xff &&
722
0
          !PixelsAreSimilar(src_pixel, dst_pixel, max_allowed_diff_lossy)) {
723
        // In this case, if we use blending, we can't attain the desired
724
        // 'dst_pixel' value for this pixel. So, blending is not possible.
725
0
        return 0;
726
0
      }
727
0
    }
728
0
  }
729
0
  return 1;
730
0
}
731
732
// For pixels in 'rect', replace those pixels in 'dst' that are same as 'src' by
733
// transparent pixels.
734
// Returns true if at least one pixel gets modified.
735
// Remember the modified pixel locations as 1s in carryover_mask.
736
WEBP_NODISCARD static int IncreaseTransparency(const WebPPicture* const src,
737
                                               const FrameRectangle* const rect,
738
                                               WebPPicture* const dst,
739
0
                                               uint8_t* const carryover_mask) {
740
0
  int i, j;
741
0
  int modified = 0;
742
  // carryover_mask spans over the rect part of the canvas.
743
0
  uint8_t* carryover_row = carryover_mask;
744
0
  assert(src != NULL && dst != NULL && rect != NULL);
745
0
  assert(src->width == dst->width && src->height == dst->height);
746
0
  for (j = rect->y_offset; j < rect->y_offset + rect->height; ++j) {
747
0
    const uint32_t* const psrc = src->argb + (ptrdiff_t)j * src->argb_stride;
748
0
    uint32_t* const pdst = dst->argb + (ptrdiff_t)j * dst->argb_stride;
749
0
    for (i = rect->x_offset; i < rect->x_offset + rect->width; ++i) {
750
0
      if (psrc[i] == pdst[i] && pdst[i] != TRANSPARENT_COLOR) {
751
0
        pdst[i] = TRANSPARENT_COLOR;
752
0
        carryover_row[i - rect->x_offset] = 1;
753
0
        modified = 1;
754
0
      }
755
0
    }
756
0
    carryover_row += rect->width;
757
0
  }
758
0
  return modified;
759
0
}
760
761
#undef TRANSPARENT_COLOR
762
763
// Replace similar blocks of pixels by a 'see-through' transparent block
764
// with uniform average color.
765
// Assumes lossy compression is being used.
766
// Returns true if at least one pixel gets modified.
767
// Remember the modified pixel locations as 1s in carryover_mask.
768
WEBP_NODISCARD static int FlattenSimilarBlocks(const WebPPicture* const src,
769
                                               const FrameRectangle* const rect,
770
                                               WebPPicture* const dst,
771
                                               float quality,
772
0
                                               uint8_t* const carryover_mask) {
773
0
  const int max_allowed_diff_lossy = QualityToMaxDiff(quality);
774
0
  int i, j;
775
0
  int modified = 0;
776
0
  const int block_size = 8;
777
0
  const int y_start = (rect->y_offset + block_size) & ~(block_size - 1);
778
0
  const int y_end = (rect->y_offset + rect->height) & ~(block_size - 1);
779
0
  const int x_start = (rect->x_offset + block_size) & ~(block_size - 1);
780
0
  const int x_end = (rect->x_offset + rect->width) & ~(block_size - 1);
781
  // carryover_mask spans over the rect part of the canvas.
782
0
  uint8_t* carryover_mask_row =
783
0
      carryover_mask + (ptrdiff_t)(y_start - rect->y_offset) * rect->width +
784
0
      (x_start - rect->x_offset);
785
0
  assert(src != NULL && dst != NULL && rect != NULL);
786
0
  assert(src->width == dst->width && src->height == dst->height);
787
0
  assert((block_size & (block_size - 1)) == 0);  // must be a power of 2
788
  // Iterate over each block and count similar pixels.
789
0
  for (j = y_start; j < y_end; j += block_size) {
790
0
    uint8_t* carryover_mask_block = carryover_mask_row;
791
0
    for (i = x_start; i < x_end; i += block_size) {
792
0
      int cnt = 0;
793
0
      int avg_r = 0, avg_g = 0, avg_b = 0;
794
0
      int x, y;
795
0
      const uint32_t* const psrc =
796
0
          src->argb + (ptrdiff_t)j * src->argb_stride + i;
797
0
      uint32_t* const pdst = dst->argb + (ptrdiff_t)j * dst->argb_stride + i;
798
0
      for (y = 0; y < block_size; ++y) {
799
0
        for (x = 0; x < block_size; ++x) {
800
0
          const uint32_t src_pixel = psrc[x + (ptrdiff_t)y * src->argb_stride];
801
0
          const int alpha = src_pixel >> 24;
802
0
          if (alpha == 0xff &&
803
0
              PixelsAreSimilar(src_pixel,
804
0
                               pdst[x + (ptrdiff_t)y * dst->argb_stride],
805
0
                               max_allowed_diff_lossy)) {
806
0
            ++cnt;
807
0
            avg_r += (src_pixel >> 16) & 0xff;
808
0
            avg_g += (src_pixel >> 8) & 0xff;
809
0
            avg_b += (src_pixel >> 0) & 0xff;
810
0
          }
811
0
        }
812
0
      }
813
      // If we have a fully similar block, we replace it with an
814
      // average transparent block. This compresses better in lossy mode.
815
0
      if (cnt == block_size * block_size) {
816
0
        const uint32_t color = (0x00 << 24) | ((avg_r / cnt) << 16) |
817
0
                               ((avg_g / cnt) << 8) | ((avg_b / cnt) << 0);
818
0
        for (y = 0; y < block_size; ++y) {
819
0
          for (x = 0; x < block_size; ++x) {
820
0
            pdst[x + (ptrdiff_t)y * dst->argb_stride] = color;
821
0
            carryover_mask_block[(ptrdiff_t)y * rect->width + x] = 1;
822
0
          }
823
0
        }
824
0
        modified = 1;
825
0
      }
826
0
      carryover_mask_block += block_size;
827
0
    }
828
0
    carryover_mask_row += (ptrdiff_t)block_size * rect->width;
829
0
  }
830
0
  return modified;
831
0
}
832
833
WEBP_NODISCARD static int EncodeFrame(const WebPConfig* const config,
834
                                      WebPPicture* const pic,
835
0
                                      WebPMemoryWriter* const memory) {
836
0
  pic->use_argb = 1;
837
0
  pic->writer = WebPMemoryWrite;
838
0
  pic->custom_ptr = memory;
839
0
  if (!WebPEncode(config, pic)) {
840
0
    return 0;
841
0
  }
842
0
  return 1;
843
0
}
844
845
// Struct representing a candidate encoded frame including its metadata.
846
typedef struct {
847
  WebPMemoryWriter mem;  // Encoded bytes.
848
  WebPMuxFrameInfo info;
849
  FrameRectangle rect;  // Coordinates and dimensions of this candidate.
850
  int carries_over;  // True if at least one pixel in rect is carried over from
851
                     // the previous frame, meaning at least one pixel was set
852
                     // to fully transparent and this frame is blended.
853
                     // If this is true, such pixels are marked as 1s in
854
                     // WebPAnimEncoder::candidate_carryover_mask.
855
} Candidate;
856
857
// Generates a candidate encoded frame given a picture and metadata.
858
static WebPEncodingError EncodeCandidate(WebPPicture* const sub_frame,
859
                                         const FrameRectangle* const rect,
860
                                         const WebPConfig* const encoder_config,
861
                                         int use_blending,
862
0
                                         Candidate* const candidate) {
863
0
  WebPConfig config = *encoder_config;
864
0
  WebPEncodingError error_code = VP8_ENC_OK;
865
0
  assert(candidate != NULL);
866
0
  memset(candidate, 0, sizeof(*candidate));
867
868
  // Set frame rect and info.
869
0
  candidate->rect = *rect;
870
0
  candidate->info.id = WEBP_CHUNK_ANMF;
871
0
  candidate->info.x_offset = rect->x_offset;
872
0
  candidate->info.y_offset = rect->y_offset;
873
0
  candidate->info.dispose_method = WEBP_MUX_DISPOSE_NONE;  // Set later.
874
0
  candidate->info.blend_method =
875
0
      use_blending ? WEBP_MUX_BLEND : WEBP_MUX_NO_BLEND;
876
0
  candidate->info.duration = 0;  // Set in next call to WebPAnimEncoderAdd().
877
878
  // Encode picture.
879
0
  WebPMemoryWriterInit(&candidate->mem);
880
881
0
  if (!config.lossless && use_blending) {
882
    // Disable filtering to avoid blockiness in reconstructed frames at the
883
    // time of decoding.
884
0
    config.autofilter = 0;
885
0
    config.filter_strength = 0;
886
0
  }
887
0
  if (!EncodeFrame(&config, sub_frame, &candidate->mem)) {
888
0
    error_code = sub_frame->error_code;
889
0
    goto Err;
890
0
  }
891
892
0
  return error_code;
893
894
0
Err:
895
0
  WebPMemoryWriterClear(&candidate->mem);
896
0
  return error_code;
897
0
}
898
899
0
static void CopyCurrentCanvas(WebPAnimEncoder* const enc) {
900
0
  if (enc->curr_canvas_copy_modified) {
901
0
    WebPCopyPixels(enc->curr_canvas, &enc->curr_canvas_copy);
902
0
    enc->curr_canvas_copy.progress_hook = enc->curr_canvas->progress_hook;
903
0
    enc->curr_canvas_copy.user_data = enc->curr_canvas->user_data;
904
0
    enc->curr_canvas_copy_modified = 0;
905
0
  }
906
0
}
907
908
enum {
909
  LL_DISP_NONE = 0,
910
  LL_DISP_BG,
911
  LOSSY_DISP_NONE,
912
  LOSSY_DISP_BG,
913
  CANDIDATE_COUNT
914
};
915
916
0
#define MIN_COLORS_LOSSY 31      // Don't try lossy below this threshold.
917
0
#define MAX_COLORS_LOSSLESS 194  // Don't try lossless above this threshold.
918
919
static void GetEncodedData(const WebPMemoryWriter* const memory,
920
0
                           WebPData* const encoded_data) {
921
0
  encoded_data->bytes = memory->mem;
922
0
  encoded_data->size = memory->size;
923
0
}
924
925
// Opposite of SetPreviousDisposeMethod().
926
0
static WebPMuxAnimDispose GetPreviousDisposeMethod(WebPAnimEncoder* const enc) {
927
0
  const size_t position = enc->count - 2;
928
0
  EncodedFrame* const prev_enc_frame = GetFrame(enc, position);
929
0
  assert(enc->count >= 2);  // As current and previous frames are in enc.
930
0
  return prev_enc_frame->is_key_frame
931
0
             ? prev_enc_frame->key_frame.dispose_method
932
0
             : prev_enc_frame->sub_frame.dispose_method;
933
0
}
934
935
// Sets dispose method of the previous frame to be 'dispose_method'.
936
static void SetPreviousDisposeMethod(WebPAnimEncoder* const enc,
937
0
                                     WebPMuxAnimDispose dispose_method) {
938
0
  const size_t position = enc->count - 2;
939
0
  EncodedFrame* const prev_enc_frame = GetFrame(enc, position);
940
0
  assert(enc->count >= 2);  // As current and previous frames are in enc.
941
942
0
  if (enc->prev_candidate_undecided) {
943
0
    assert(dispose_method == WEBP_MUX_DISPOSE_NONE);
944
0
    prev_enc_frame->sub_frame.dispose_method = dispose_method;
945
0
    prev_enc_frame->key_frame.dispose_method = dispose_method;
946
0
  } else {
947
0
    WebPMuxFrameInfo* const prev_info = prev_enc_frame->is_key_frame
948
0
                                            ? &prev_enc_frame->key_frame
949
0
                                            : &prev_enc_frame->sub_frame;
950
0
    prev_info->dispose_method = dispose_method;
951
0
  }
952
0
}
953
954
// Pick the candidate encoded frame with smallest size and release other
955
// candidates.
956
// TODO(later): Perhaps a rough SSIM/PSNR produced by the encoder should
957
// also be a criteria, in addition to sizes.
958
static void PickBestCandidate(WebPAnimEncoder* const enc,
959
                              Candidate* const candidate,
960
                              WebPMuxAnimDispose dispose_method,
961
                              int is_key_frame,
962
                              Candidate** const best_candidate,
963
0
                              EncodedFrame* const encoded_frame) {
964
0
  if (*best_candidate == NULL ||
965
0
      candidate->mem.size < (*best_candidate)->mem.size) {
966
0
    WebPMuxFrameInfo* const dst =
967
0
        is_key_frame ? &encoded_frame->key_frame : &encoded_frame->sub_frame;
968
0
    *dst = candidate->info;
969
0
    GetEncodedData(&candidate->mem, &dst->bitstream);
970
0
    if (!is_key_frame) {
971
      // Note: Previous dispose method only matters for non-keyframes.
972
      // Also, we don't want to modify previous dispose method that was
973
      // selected when a non keyframe was assumed.
974
0
      SetPreviousDisposeMethod(enc, dispose_method);
975
976
0
      enc->best_candidate_carries_over = candidate->carries_over;
977
0
      if (candidate->carries_over) {
978
        // Save the best_candidate_carryover_mask to be able to generate the
979
        // canvas_carryover of the next frame later in case this candidate stays
980
        // the best one.
981
        // Note: The canvas_carryover could contain the pixel values with loss
982
        //       due to quantization as if they were decoded, to even better
983
        //       estimate which areas of the canvas should be explicitly encoded
984
        //       at each frame. Setting WebPConfig::show_compressed=1 is a way
985
        //       to approximate this. That works poorly: in lossy mode most
986
        //       areas of the decoded canvas change because of quantization loss
987
        //       in a frame, resulting in the encoder trying to explicitly
988
        //       encode most of the following frame because most of the areas
989
        //       differ significantly between the previous decoded canvas and
990
        //       the current original canvas. This repeats for all frames,
991
        //       resulting in a very large encoded file for no visible benefit.
992
        //       The canvas carryover approach seems to fix
993
        //       https://issues.webmproject.org/42340478 while still staying
994
        //       close enough to the old behavior (only looking at the previous
995
        //       and current original input frames) to not break everything.
996
        // Save candidate_carryover_mask as best_candidate_carryover_mask by
997
        // swapping the two buffers.
998
0
        uint8_t* const tmp_carryover_mask = enc->best_candidate_carryover_mask;
999
0
        enc->best_candidate_carryover_mask = enc->candidate_carryover_mask;
1000
0
        enc->candidate_carryover_mask = tmp_carryover_mask;
1001
0
      }
1002
0
    }
1003
1004
    // Release the memory of the previous best candidate if any.
1005
0
    if (*best_candidate != NULL) {
1006
0
      WebPMemoryWriterClear(&(*best_candidate)->mem);
1007
0
    }
1008
0
    *best_candidate = candidate;
1009
0
  } else {
1010
    // Release the memory of the current candidate which is not the best one.
1011
0
    WebPMemoryWriterClear(&candidate->mem);
1012
0
  }
1013
0
}
1014
1015
// Generates candidates for a given dispose method given pre-filled subframe
1016
// 'params'.
1017
static WebPEncodingError GenerateCandidates(
1018
    WebPAnimEncoder* const enc, Candidate candidates[CANDIDATE_COUNT],
1019
    WebPMuxAnimDispose dispose_method,
1020
    const WebPPicture* const canvas_carryover_disposed, int is_lossless,
1021
    int is_key_frame, SubFrameParams* const params,
1022
    const WebPConfig* const config_ll, const WebPConfig* const config_lossy,
1023
0
    Candidate** const best_candidate, EncodedFrame* const encoded_frame) {
1024
0
  WebPEncodingError error_code = VP8_ENC_OK;
1025
0
  const int is_dispose_none = (dispose_method == WEBP_MUX_DISPOSE_NONE);
1026
0
  Candidate* const candidate_ll =
1027
0
      is_dispose_none ? &candidates[LL_DISP_NONE] : &candidates[LL_DISP_BG];
1028
0
  Candidate* const candidate_lossy = is_dispose_none
1029
0
                                         ? &candidates[LOSSY_DISP_NONE]
1030
0
                                         : &candidates[LOSSY_DISP_BG];
1031
0
  WebPPicture* const curr_canvas = &enc->curr_canvas_copy;
1032
0
  const WebPPicture* const canvas_carryover =
1033
0
      is_dispose_none ? &enc->canvas_carryover : canvas_carryover_disposed;
1034
0
  int use_blending_ll, use_blending_lossy;
1035
0
  int evaluate_ll, evaluate_lossy;
1036
1037
0
  CopyCurrentCanvas(enc);
1038
0
  use_blending_ll =
1039
0
      !is_key_frame && IsLosslessBlendingPossible(canvas_carryover, curr_canvas,
1040
0
                                                  &params->rect_ll);
1041
0
  use_blending_lossy =
1042
0
      !is_key_frame &&
1043
0
      IsLossyBlendingPossible(canvas_carryover, curr_canvas,
1044
0
                              &params->rect_lossy, config_lossy->quality);
1045
1046
  // Pick candidates to be tried.
1047
0
  if (!enc->options.allow_mixed) {
1048
0
    evaluate_ll = is_lossless;
1049
0
    evaluate_lossy = !is_lossless;
1050
0
  } else if (enc->options.minimize_size) {
1051
0
    evaluate_ll = 1;
1052
0
    evaluate_lossy = 1;
1053
0
  } else {  // Use a heuristic for trying lossless and/or lossy compression.
1054
0
    const int num_colors = WebPGetColorPalette(&params->sub_frame_ll, NULL);
1055
0
    evaluate_ll = (num_colors < MAX_COLORS_LOSSLESS);
1056
0
    evaluate_lossy = (num_colors >= MIN_COLORS_LOSSY);
1057
0
  }
1058
1059
  // Generate candidates.
1060
0
  if (evaluate_ll) {
1061
0
    CopyCurrentCanvas(enc);
1062
0
    if (use_blending_ll) {
1063
      // Reset the whole carryover mask to "all pixels are explicitly encoded in
1064
      // this current frame".
1065
0
      memset(enc->candidate_carryover_mask, 0, RectArea(&params->rect_ll));
1066
0
      enc->curr_canvas_copy_modified =
1067
0
          IncreaseTransparency(canvas_carryover, &params->rect_ll, curr_canvas,
1068
0
                               enc->candidate_carryover_mask);
1069
0
    }
1070
0
    error_code = EncodeCandidate(&params->sub_frame_ll, &params->rect_ll,
1071
0
                                 config_ll, use_blending_ll, candidate_ll);
1072
0
    if (error_code != VP8_ENC_OK) return error_code;
1073
0
    candidate_ll->carries_over = enc->curr_canvas_copy_modified;
1074
    // WebPEncode() clears the RGB bits of fully transparent pixels in
1075
    // lossless mode when exact=0, modifying curr_canvas_copy in-place.
1076
0
    enc->curr_canvas_copy_modified = 1;
1077
0
    PickBestCandidate(enc, candidate_ll, dispose_method, is_key_frame,
1078
0
                      best_candidate, encoded_frame);
1079
0
  }
1080
0
  if (evaluate_lossy) {
1081
0
    CopyCurrentCanvas(enc);
1082
0
    if (use_blending_lossy) {
1083
      // Reset the whole carryover mask to "all pixels are explicitly encoded in
1084
      // this current frame".
1085
0
      memset(enc->candidate_carryover_mask, 0, RectArea(&params->rect_lossy));
1086
0
      enc->curr_canvas_copy_modified = FlattenSimilarBlocks(
1087
0
          canvas_carryover, &params->rect_lossy, curr_canvas,
1088
0
          config_lossy->quality, enc->candidate_carryover_mask);
1089
0
    }
1090
0
    error_code =
1091
0
        EncodeCandidate(&params->sub_frame_lossy, &params->rect_lossy,
1092
0
                        config_lossy, use_blending_lossy, candidate_lossy);
1093
0
    if (error_code != VP8_ENC_OK) return error_code;
1094
0
    candidate_lossy->carries_over = enc->curr_canvas_copy_modified;
1095
0
    enc->curr_canvas_copy_modified = 1;
1096
0
    PickBestCandidate(enc, candidate_lossy, dispose_method, is_key_frame,
1097
0
                      best_candidate, encoded_frame);
1098
0
  }
1099
0
  return error_code;
1100
0
}
1101
1102
#undef MIN_COLORS_LOSSY
1103
#undef MAX_COLORS_LOSSLESS
1104
1105
WEBP_NODISCARD static int IncreasePreviousDuration(WebPAnimEncoder* const enc,
1106
0
                                                   int duration) {
1107
0
  const size_t position = enc->count - 1;
1108
0
  EncodedFrame* const prev_enc_frame = GetFrame(enc, position);
1109
0
  int new_duration;
1110
1111
0
  assert(enc->count >= 1);
1112
0
  assert(!prev_enc_frame->is_key_frame ||
1113
0
         prev_enc_frame->sub_frame.duration ==
1114
0
             prev_enc_frame->key_frame.duration);
1115
0
  assert(prev_enc_frame->sub_frame.duration ==
1116
0
         (prev_enc_frame->sub_frame.duration & (MAX_DURATION - 1)));
1117
0
  assert(duration == (duration & (MAX_DURATION - 1)));
1118
1119
0
  new_duration = prev_enc_frame->sub_frame.duration + duration;
1120
0
  if (new_duration >= MAX_DURATION) {  // Special case.
1121
    // Separate out previous frame from earlier merged frames to avoid overflow.
1122
    // We add a 1x1 transparent frame for the previous frame, with blending on.
1123
0
    const FrameRectangle rect = {0, 0, 1, 1};
1124
0
    const uint8_t lossless_1x1_bytes[] = {
1125
0
        0x52, 0x49, 0x46, 0x46, 0x14, 0x00, 0x00, 0x00, 0x57, 0x45,
1126
0
        0x42, 0x50, 0x56, 0x50, 0x38, 0x4c, 0x08, 0x00, 0x00, 0x00,
1127
0
        0x2f, 0x00, 0x00, 0x00, 0x10, 0x88, 0x88, 0x08};
1128
0
    const WebPData lossless_1x1 = {lossless_1x1_bytes,
1129
0
                                   sizeof(lossless_1x1_bytes)};
1130
0
    const uint8_t lossy_1x1_bytes[] = {
1131
0
        0x52, 0x49, 0x46, 0x46, 0x40, 0x00, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50,
1132
0
        0x56, 0x50, 0x38, 0x58, 0x0a, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
1133
0
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x4c, 0x50, 0x48, 0x02, 0x00,
1134
0
        0x00, 0x00, 0x00, 0x00, 0x56, 0x50, 0x38, 0x20, 0x18, 0x00, 0x00, 0x00,
1135
0
        0x30, 0x01, 0x00, 0x9d, 0x01, 0x2a, 0x01, 0x00, 0x01, 0x00, 0x02, 0x00,
1136
0
        0x34, 0x25, 0xa4, 0x00, 0x03, 0x70, 0x00, 0xfe, 0xfb, 0xfd, 0x50, 0x00};
1137
0
    const WebPData lossy_1x1 = {lossy_1x1_bytes, sizeof(lossy_1x1_bytes)};
1138
0
    const int can_use_lossless =
1139
0
        (enc->last_config.lossless || enc->options.allow_mixed);
1140
0
    EncodedFrame* const curr_enc_frame = GetFrame(enc, enc->count);
1141
0
    curr_enc_frame->is_key_frame = 0;
1142
0
    curr_enc_frame->sub_frame.id = WEBP_CHUNK_ANMF;
1143
0
    curr_enc_frame->sub_frame.x_offset = 0;
1144
0
    curr_enc_frame->sub_frame.y_offset = 0;
1145
0
    curr_enc_frame->sub_frame.dispose_method = WEBP_MUX_DISPOSE_NONE;
1146
0
    curr_enc_frame->sub_frame.blend_method = WEBP_MUX_BLEND;
1147
0
    curr_enc_frame->sub_frame.duration = duration;
1148
0
    if (!WebPDataCopy(can_use_lossless ? &lossless_1x1 : &lossy_1x1,
1149
0
                      &curr_enc_frame->sub_frame.bitstream)) {
1150
0
      return 0;
1151
0
    }
1152
0
    ++enc->count;
1153
0
    ++enc->count_since_key_frame;
1154
0
    if (enc->keyframe != KEYFRAME_NONE) {
1155
      // Freeze the already chosen keyframe before flushing all previous frames.
1156
0
      enc->count_since_key_frame = (int)enc->count - 1 - enc->keyframe;
1157
0
      enc->best_delta = DELTA_INFINITY;
1158
0
      enc->keyframe = KEYFRAME_NONE;
1159
0
    }
1160
0
    enc->flush_count = enc->count - 1;
1161
0
    enc->prev_candidate_undecided = 0;
1162
0
    enc->prev_rect = rect;
1163
0
  } else {  // Regular case.
1164
    // Increase duration of the previous frame by 'duration'.
1165
0
    prev_enc_frame->sub_frame.duration = new_duration;
1166
0
    prev_enc_frame->key_frame.duration = new_duration;
1167
0
  }
1168
0
  return 1;
1169
0
}
1170
1171
// Copies the pixels that are identical in 'a' and 'b' to 'dst'.
1172
static void CopyIdenticalPixels(const WebPPicture* const a,
1173
                                const WebPPicture* const b,
1174
0
                                WebPPicture* const dst) {
1175
0
  int y, x;
1176
0
  const uint32_t* row_a = a->argb;
1177
0
  const uint32_t* row_b = b->argb;
1178
0
  uint32_t* row_dst = dst->argb;
1179
0
  assert(a->width == b->width && a->height == b->height);
1180
0
  assert(a->width == dst->width && a->height == dst->height);
1181
0
  assert(a->use_argb && b->use_argb && dst->use_argb);
1182
1183
0
  for (y = 0; y < a->height; ++y) {
1184
0
    for (x = 0; x < a->width; ++x) {
1185
0
      if (row_a[x] == row_b[x]) {
1186
0
        row_dst[x] = row_a[x];
1187
0
      }
1188
0
    }
1189
0
    row_a += a->argb_stride;
1190
0
    row_b += b->argb_stride;
1191
0
    row_dst += dst->argb_stride;
1192
0
  }
1193
0
}
1194
1195
// Copies the pixels where 'mask' is 0 from 'src' to 'dst'.
1196
static void CopyMaskedPixels(const WebPPicture* const src,
1197
                             const uint8_t* const mask,
1198
0
                             WebPPicture* const dst) {
1199
0
  int y, x;
1200
0
  const uint32_t* row_src = src->argb;
1201
0
  const uint8_t* row_mask = mask;
1202
0
  uint32_t* row_dst = dst->argb;
1203
0
  assert(src->width == dst->width && src->height == dst->height);
1204
0
  assert(src->use_argb && dst->use_argb);
1205
1206
0
  for (y = 0; y < src->height; ++y) {
1207
0
    for (x = 0; x < src->width; ++x) {
1208
0
      if (row_mask[x] == 0) {
1209
0
        row_dst[x] = row_src[x];
1210
0
      }
1211
0
    }
1212
0
    row_src += src->argb_stride;
1213
0
    row_mask += src->width;
1214
0
    row_dst += dst->argb_stride;
1215
0
  }
1216
0
}
1217
1218
// Depending on the configuration, tries different compressions
1219
// (lossy/lossless), dispose methods, blending methods etc to encode the current
1220
// frame and outputs the best one in 'encoded_frame'.
1221
// 'frame_skipped' will be set to true if this frame should actually be skipped.
1222
static WebPEncodingError SetFrame(WebPAnimEncoder* const enc,
1223
                                  const WebPConfig* const config,
1224
                                  int is_key_frame,
1225
                                  FrameRectangle* const best_candidate_rect,
1226
                                  EncodedFrame* const encoded_frame,
1227
0
                                  int* const frame_skipped) {
1228
0
  WebPEncodingError error_code = VP8_ENC_OK;
1229
0
  const WebPPicture* const curr_canvas = &enc->curr_canvas_copy;
1230
0
  const WebPPicture* const canvas_carryover = &enc->canvas_carryover;
1231
  // canvas_carryover with the area corresponding to the previous frame disposed
1232
  // to background color.
1233
0
  WebPPicture canvas_carryover_disposed;
1234
0
  Candidate candidates[CANDIDATE_COUNT];
1235
0
  Candidate* best_candidate = NULL;
1236
0
  const int is_lossless = config->lossless;
1237
0
  const int consider_lossless = is_lossless || enc->options.allow_mixed;
1238
0
  const int consider_lossy = !is_lossless || enc->options.allow_mixed;
1239
0
  const int is_first_frame = enc->is_first_frame;
1240
1241
  // First frame cannot be skipped as there is no 'previous frame' to merge it
1242
  // to. So, empty rectangle is not allowed for the first frame.
1243
0
  const int empty_rect_allowed_none = !is_first_frame;
1244
1245
  // Even if there is exact pixel match between 'disposed previous canvas' and
1246
  // 'current canvas', we can't skip current frame, as there may not be exact
1247
  // pixel match between 'previous canvas' and 'current canvas'. So, we don't
1248
  // allow empty rectangle in this case.
1249
0
  const int empty_rect_allowed_bg = 0;
1250
1251
  // If current frame is a keyframe, dispose method of previous frame doesn't
1252
  // matter, so we don't try dispose to background.
1253
  // Also, if keyframe insertion is on, and previous frame could be picked as
1254
  // either a subframe or a keyframe, then we can't be sure about what frame
1255
  // rectangle would be disposed. In that case too, we don't try dispose to
1256
  // background.
1257
0
  const int dispose_bg_possible =
1258
0
      !is_key_frame && !enc->prev_candidate_undecided;
1259
1260
0
  SubFrameParams dispose_none_params;
1261
0
  SubFrameParams dispose_bg_params;
1262
1263
0
  WebPConfig config_ll = *config;
1264
0
  WebPConfig config_lossy = *config;
1265
0
  config_ll.lossless = 1;
1266
0
  config_lossy.lossless = 0;
1267
0
  enc->last_config = *config;
1268
0
  enc->last_config_reversed = config->lossless ? config_lossy : config_ll;
1269
0
  *frame_skipped = 0;
1270
1271
0
  if (!WebPPictureInit(&canvas_carryover_disposed)) {
1272
0
    return VP8_ENC_ERROR_INVALID_CONFIGURATION;
1273
0
  }
1274
0
  if (!SubFrameParamsInit(&dispose_none_params, 1, empty_rect_allowed_none) ||
1275
0
      !SubFrameParamsInit(&dispose_bg_params, 0, empty_rect_allowed_bg)) {
1276
0
    return VP8_ENC_ERROR_INVALID_CONFIGURATION;
1277
0
  }
1278
1279
0
  memset(candidates, 0, sizeof(candidates));
1280
1281
  // Change-rectangle assuming previous frame was DISPOSE_NONE.
1282
0
  if (!GetSubRects(canvas_carryover, curr_canvas, is_key_frame, is_first_frame,
1283
0
                   config_lossy.quality, &dispose_none_params)) {
1284
0
    error_code = VP8_ENC_ERROR_INVALID_CONFIGURATION;
1285
0
    goto Err;
1286
0
  }
1287
1288
0
  if ((consider_lossless && IsEmptyRect(&dispose_none_params.rect_ll)) ||
1289
0
      (consider_lossy && IsEmptyRect(&dispose_none_params.rect_lossy))) {
1290
    // Don't encode the frame at all. Instead, the duration of the previous
1291
    // frame will be increased later.
1292
0
    assert(empty_rect_allowed_none);
1293
0
    *frame_skipped = 1;
1294
0
    goto End;
1295
0
  }
1296
1297
0
  if (dispose_bg_possible) {
1298
    // Change-rectangle assuming previous frame was DISPOSE_BACKGROUND.
1299
0
    if (!WebPPictureCopy(canvas_carryover, &canvas_carryover_disposed)) {
1300
0
      error_code = VP8_ENC_ERROR_OUT_OF_MEMORY;
1301
0
      goto Err;
1302
0
    }
1303
0
    DisposeFrameRectangle(WEBP_MUX_DISPOSE_BACKGROUND, &enc->prev_rect,
1304
0
                          &canvas_carryover_disposed);
1305
1306
0
    if (!GetSubRects(&canvas_carryover_disposed, curr_canvas, is_key_frame,
1307
0
                     is_first_frame, config_lossy.quality,
1308
0
                     &dispose_bg_params)) {
1309
0
      error_code = VP8_ENC_ERROR_INVALID_CONFIGURATION;
1310
0
      goto Err;
1311
0
    }
1312
0
    assert(!IsEmptyRect(&dispose_bg_params.rect_ll));
1313
0
    assert(!IsEmptyRect(&dispose_bg_params.rect_lossy));
1314
1315
0
    if (enc->options.minimize_size) {  // Try both dispose methods.
1316
0
      dispose_bg_params.should_try = 1;
1317
0
      dispose_none_params.should_try = 1;
1318
0
    } else if ((is_lossless && RectArea(&dispose_bg_params.rect_ll) <
1319
0
                                   RectArea(&dispose_none_params.rect_ll)) ||
1320
0
               (!is_lossless &&
1321
0
                RectArea(&dispose_bg_params.rect_lossy) <
1322
0
                    RectArea(&dispose_none_params.rect_lossy))) {
1323
0
      dispose_bg_params.should_try = 1;  // Pick DISPOSE_BACKGROUND.
1324
0
      dispose_none_params.should_try = 0;
1325
0
    }
1326
0
  }
1327
1328
0
  if (dispose_none_params.should_try) {
1329
0
    error_code =
1330
0
        GenerateCandidates(enc, candidates, WEBP_MUX_DISPOSE_NONE,
1331
0
                           /*canvas_carryover_disposed=*/NULL, is_lossless,
1332
0
                           is_key_frame, &dispose_none_params, &config_ll,
1333
0
                           &config_lossy, &best_candidate, encoded_frame);
1334
0
    if (error_code != VP8_ENC_OK) goto Err;
1335
0
  }
1336
1337
0
  if (dispose_bg_params.should_try) {
1338
0
    assert(!enc->is_first_frame);
1339
0
    assert(dispose_bg_possible);
1340
0
    error_code =
1341
0
        GenerateCandidates(enc, candidates, WEBP_MUX_DISPOSE_BACKGROUND,
1342
0
                           &canvas_carryover_disposed, is_lossless,
1343
0
                           is_key_frame, &dispose_bg_params, &config_ll,
1344
0
                           &config_lossy, &best_candidate, encoded_frame);
1345
0
    if (error_code != VP8_ENC_OK) goto Err;
1346
0
  }
1347
1348
0
  assert(best_candidate != NULL);
1349
0
  *best_candidate_rect = best_candidate->rect;
1350
0
  goto End;
1351
1352
0
Err:
1353
0
End:
1354
0
  WebPPictureFree(&canvas_carryover_disposed);
1355
0
  SubFrameParamsFree(&dispose_none_params);
1356
0
  SubFrameParamsFree(&dispose_bg_params);
1357
0
  return error_code;
1358
0
}
1359
1360
// Calculate the penalty incurred if we encode given frame as a keyframe
1361
// instead of a subframe.
1362
0
static int64_t KeyFramePenalty(const EncodedFrame* const encoded_frame) {
1363
0
  return ((int64_t)encoded_frame->key_frame.bitstream.size -
1364
0
          (int64_t)encoded_frame->sub_frame.bitstream.size);
1365
0
}
1366
1367
WEBP_NODISCARD static int CacheFrame(WebPAnimEncoder* const enc,
1368
0
                                     const WebPConfig* const config) {
1369
0
  int ok = 0;
1370
0
  int frame_skipped = 0;
1371
0
  WebPEncodingError error_code = VP8_ENC_OK;
1372
0
  const size_t position = enc->count;
1373
0
  EncodedFrame* const encoded_frame = GetFrame(enc, position);
1374
0
  FrameRectangle best_key_candidate_rect, best_sub_candidate_rect;
1375
0
  int candidate_undecided;
1376
0
  const WebPConfig orig_last_config = enc->last_config;
1377
0
  const WebPConfig orig_last_config_reversed = enc->last_config_reversed;
1378
0
  WebPMuxAnimDispose orig_prev_dispose = WEBP_MUX_DISPOSE_NONE;
1379
1380
0
  ++enc->count;
1381
0
  if (!enc->is_first_frame) {
1382
0
    orig_prev_dispose = GetPreviousDisposeMethod(enc);
1383
0
  }
1384
1385
0
  if (enc->is_first_frame) {  // Add this as a keyframe.
1386
0
    error_code = SetFrame(enc, config, 1, &best_key_candidate_rect,
1387
0
                          encoded_frame, &frame_skipped);
1388
0
    if (error_code != VP8_ENC_OK) goto End;
1389
0
    assert(frame_skipped == 0);  // First frame can't be skipped, even if empty.
1390
0
    assert(position == 0 && enc->count == 1);
1391
0
    encoded_frame->is_key_frame = 1;
1392
0
    enc->flush_count = 0;
1393
0
    enc->count_since_key_frame = 0;
1394
0
    candidate_undecided = 0;
1395
0
  } else {
1396
0
    ++enc->count_since_key_frame;
1397
1398
    // When some parts of the current original input frame (curr_canvas) did not
1399
    // change since the previous original input frame (prev_canvas), consider
1400
    // the encoder already did its best job at encoding these parts, and there
1401
    // is no need to explicitly encode these parts again. To avoid that, copy
1402
    // these pixels from curr_canvas (or prev_canvas) to canvas_carryover so
1403
    // that they are detected as unchanged in the SetFrame() implementation
1404
    // below. If all parts are identical, the whole frame may be skipped.
1405
    // TODO: Only allocate and use canvas_carryover for lossy and near-lossless.
1406
0
    CopyIdenticalPixels(&enc->prev_canvas, enc->curr_canvas,
1407
0
                        &enc->canvas_carryover);
1408
1409
0
    if (enc->count_since_key_frame <= enc->options.kmin) {
1410
      // Add this as a frame rectangle.
1411
0
      error_code = SetFrame(enc, config, 0, &best_sub_candidate_rect,
1412
0
                            encoded_frame, &frame_skipped);
1413
0
      if (error_code != VP8_ENC_OK) goto End;
1414
0
      if (frame_skipped) goto Skip;
1415
0
      encoded_frame->is_key_frame = 0;
1416
0
      enc->flush_count = enc->count - 1;
1417
0
      candidate_undecided = 0;
1418
0
    } else {
1419
0
      int64_t curr_delta;
1420
1421
      // Add this as a frame rectangle to enc.
1422
      // TODO: Only try to encode a subframe when it can be used (for example
1423
      //       only when enc->count_since_key_frame < enc->options.kmax ||
1424
      //       enc->best_delta < DELTA_INFINITY).
1425
      //       frame_skipped should still be tested to keep exact same behavior.
1426
0
      error_code = SetFrame(enc, config, 0, &best_sub_candidate_rect,
1427
0
                            encoded_frame, &frame_skipped);
1428
0
      if (error_code != VP8_ENC_OK) goto End;
1429
0
      if (frame_skipped) goto Skip;
1430
1431
      // Add this as a keyframe to enc, too.
1432
0
      error_code = SetFrame(enc, config, 1, &best_key_candidate_rect,
1433
0
                            encoded_frame, &frame_skipped);
1434
0
      if (error_code != VP8_ENC_OK) goto End;
1435
0
      assert(frame_skipped == 0);  // keyframe cannot be an empty rectangle.
1436
1437
      // Analyze size difference of the two variants.
1438
0
      curr_delta = KeyFramePenalty(encoded_frame);
1439
0
      if (curr_delta <= enc->best_delta) {  // Pick this as the keyframe.
1440
0
        if (enc->keyframe != KEYFRAME_NONE) {
1441
0
          EncodedFrame* const old_keyframe = GetFrame(enc, enc->keyframe);
1442
0
          assert(old_keyframe->is_key_frame);
1443
0
          old_keyframe->is_key_frame = 0;
1444
0
        }
1445
0
        encoded_frame->is_key_frame = 1;
1446
0
        candidate_undecided = 1;
1447
0
        enc->keyframe = (int)position;
1448
0
        enc->best_delta = curr_delta;
1449
0
        enc->flush_count = enc->count - 1;  // We can flush previous frames.
1450
0
      } else {
1451
0
        encoded_frame->is_key_frame = 0;
1452
0
        candidate_undecided = 0;
1453
0
      }
1454
      // Note: We need '>=' below because when kmin and kmax are both zero,
1455
      // count_since_key_frame will always be > kmax.
1456
0
      if (enc->count_since_key_frame >= enc->options.kmax) {
1457
        // Start a new sequence of kmin subframes, followed by (kmax-kmin)
1458
        // candidate frames. Exactly one of these candidate frames will end up
1459
        // as a keyframe in the output encoded animation.
1460
0
        enc->count_since_key_frame = 0;
1461
0
        enc->best_delta = DELTA_INFINITY;
1462
        // Freeze the previous candidate, whether it is a keyframe or not.
1463
0
        candidate_undecided = 0;
1464
0
        enc->keyframe = KEYFRAME_NONE;
1465
        // Flush all previous frames.
1466
0
        enc->flush_count = enc->count - 1;
1467
0
      }
1468
0
    }
1469
0
  }
1470
1471
0
  if (encoded_frame->is_key_frame) {
1472
    // A keyframe does not carry any pixels over from previous frames.
1473
0
    WebPCopyPixels(enc->curr_canvas, &enc->canvas_carryover);
1474
0
  } else {
1475
0
    const FrameRectangle* const curr_rect = &best_sub_candidate_rect;
1476
0
    WebPPicture curr_canvas_in_curr_rect;
1477
0
    WebPPicture canvas_carryover_in_curr_rect;
1478
1479
    // There is no carried over pixel in the disposed rectangle, if any.
1480
    // Note that this could not have been done earlier because the decision to
1481
    // dispose a frame is taken when encoding a next frame's candidate.
1482
0
    const WebPMuxAnimDispose prev_dispose_method =
1483
0
        GetPreviousDisposeMethod(enc);
1484
0
    assert(prev_dispose_method == WEBP_MUX_DISPOSE_NONE ||
1485
0
           !enc->prev_candidate_undecided);
1486
0
    DisposeFrameRectangle(prev_dispose_method, &enc->prev_rect,
1487
0
                          &enc->canvas_carryover);
1488
1489
    // The pixels outside the current frame rectangle are not carried over.
1490
    // Focus on the current frame rectangle.
1491
0
    if (!WebPPictureView(enc->curr_canvas, curr_rect->x_offset,
1492
0
                         curr_rect->y_offset, curr_rect->width,
1493
0
                         curr_rect->height, &curr_canvas_in_curr_rect) ||
1494
0
        !WebPPictureView(&enc->canvas_carryover, curr_rect->x_offset,
1495
0
                         curr_rect->y_offset, curr_rect->width,
1496
0
                         curr_rect->height, &canvas_carryover_in_curr_rect)) {
1497
0
      error_code = VP8_ENC_ERROR_INVALID_CONFIGURATION;
1498
0
      goto End;
1499
0
    }
1500
1501
0
    if (enc->best_candidate_carries_over) {
1502
      // Carry over the pixels that were set to fully transparent in the current
1503
      // frame (meaning they are left untouched in canvas_carryover). Copy the
1504
      // other pixels (the explicitly encoded ones) from the original input
1505
      // canvas (curr_canvas) to next frame's canvas_carryover.
1506
0
      CopyMaskedPixels(&curr_canvas_in_curr_rect,
1507
0
                       enc->best_candidate_carryover_mask,
1508
0
                       &canvas_carryover_in_curr_rect);
1509
0
    } else {
1510
      // No pixel is carried over from previous frames, either because the
1511
      // current subframe is not blended, or because no pixel was set to
1512
      // TRANSPARENT_COLOR.
1513
0
      WebPCopyPixels(&curr_canvas_in_curr_rect, &canvas_carryover_in_curr_rect);
1514
0
    }
1515
0
  }
1516
1517
  // Save the current frame environment as the previous frame environment for
1518
  // the next call to this function.
1519
0
  WebPCopyPixels(enc->curr_canvas, &enc->prev_canvas);
1520
0
  enc->prev_candidate_undecided = candidate_undecided;
1521
0
  if (candidate_undecided) {
1522
    // The previous frame rectangle is not known for sure. Do not save it.
1523
0
  } else {
1524
0
    enc->prev_rect = encoded_frame->is_key_frame ? best_key_candidate_rect
1525
0
                                                 : best_sub_candidate_rect;
1526
0
  }
1527
1528
0
  enc->is_first_frame = 0;
1529
1530
0
Skip:
1531
0
  ok = 1;
1532
0
  ++enc->in_frame_count;
1533
1534
0
End:
1535
0
  if (!ok || frame_skipped) {
1536
0
    FrameRelease(encoded_frame);
1537
0
    if (!ok) {
1538
0
      enc->last_config = orig_last_config;
1539
0
      enc->last_config_reversed = orig_last_config_reversed;
1540
0
      if (!enc->is_first_frame) {
1541
0
        SetPreviousDisposeMethod(enc, orig_prev_dispose);
1542
0
      }
1543
0
      MarkError2(enc, "ERROR adding frame. WebPEncodingError", error_code);
1544
0
    }
1545
    // We reset some counters, as the frame addition failed/was skipped.
1546
0
    --enc->count;
1547
0
    if (!enc->is_first_frame) --enc->count_since_key_frame;
1548
0
  }
1549
0
  enc->curr_canvas->error_code = error_code;  // report error_code
1550
0
  assert(ok || error_code != VP8_ENC_OK);
1551
0
  return ok;
1552
0
}
1553
1554
0
WEBP_NODISCARD static int FlushFrames(WebPAnimEncoder* const enc) {
1555
0
  while (enc->flush_count > 0) {
1556
0
    WebPMuxError err;
1557
0
    EncodedFrame* const curr = GetFrame(enc, 0);
1558
0
    const WebPMuxFrameInfo* const info =
1559
0
        curr->is_key_frame ? &curr->key_frame : &curr->sub_frame;
1560
0
    assert(enc->mux != NULL);
1561
0
    err = WebPMuxPushFrame(enc->mux, info, 1);
1562
0
    if (err != WEBP_MUX_OK) {
1563
0
      MarkError2(enc, "ERROR adding frame. WebPMuxError", err);
1564
0
      return 0;
1565
0
    }
1566
0
    if (enc->options.verbose) {
1567
0
      fprintf(stderr, "INFO: Added frame. offset:%d,%d dispose:%d blend:%d\n",
1568
0
              info->x_offset, info->y_offset, info->dispose_method,
1569
0
              info->blend_method);
1570
0
    }
1571
0
    ++enc->out_frame_count;
1572
0
    FrameRelease(curr);
1573
0
    ++enc->start;
1574
0
    --enc->flush_count;
1575
0
    --enc->count;
1576
0
    if (enc->keyframe != KEYFRAME_NONE) --enc->keyframe;
1577
0
  }
1578
1579
0
  if (enc->count > 0 && enc->start != 0) {
1580
    // Move remaining frames starting at enc->start to index 0.
1581
0
    size_t i;
1582
0
    for (i = 0; i < enc->count; ++i) {
1583
0
      const size_t old_idx = enc->start + i;
1584
0
      const EncodedFrame temp = enc->encoded_frames[i];
1585
0
      enc->encoded_frames[i] = enc->encoded_frames[old_idx];
1586
0
      enc->encoded_frames[old_idx] = temp;
1587
0
      FrameRelease(&enc->encoded_frames[old_idx]);
1588
0
    }
1589
0
    enc->start = 0;
1590
0
  } else if (enc->count == 0) {
1591
0
    enc->start = 0;
1592
0
  }
1593
0
  return 1;
1594
0
}
1595
1596
#undef DELTA_INFINITY
1597
#undef KEYFRAME_NONE
1598
1599
int WebPAnimEncoderAdd(WebPAnimEncoder* enc, WebPPicture* frame, int timestamp,
1600
0
                       const WebPConfig* encoder_config) {
1601
0
  WebPConfig config;
1602
0
  int ok;
1603
0
  uint32_t prev_frame_duration = 0;
1604
0
  int prev_duration = 0;
1605
0
  int will_split = 0;
1606
1607
0
  if (enc == NULL) {
1608
0
    return 0;
1609
0
  }
1610
0
  MarkNoError(enc);
1611
1612
0
  if (!enc->is_first_frame && enc->count == 0) {
1613
    // WebPAnimEncoderAssemble() flushed all frames, including the previous one.
1614
0
    if (frame != NULL) {
1615
0
      frame->error_code = VP8_ENC_ERROR_INVALID_CONFIGURATION;
1616
0
    }
1617
0
    MarkError(enc, "ERROR adding frame: animation already assembled");
1618
0
    return 0;
1619
0
  }
1620
1621
0
  if (!enc->is_first_frame) {
1622
    // Make sure timestamps are non-decreasing (integer wrap-around is OK).
1623
0
    prev_frame_duration = (uint32_t)timestamp - enc->prev_timestamp;
1624
0
    if (prev_frame_duration >= MAX_DURATION) {
1625
0
      if (frame != NULL) {
1626
0
        frame->error_code = VP8_ENC_ERROR_INVALID_CONFIGURATION;
1627
0
      }
1628
0
      MarkError(enc, "ERROR adding frame: timestamps must be non-decreasing");
1629
0
      return 0;
1630
0
    }
1631
0
  }
1632
1633
0
  if (frame != NULL) {
1634
0
    frame->error_code = VP8_ENC_OK;
1635
0
    if (frame->width != enc->canvas_width ||
1636
0
        frame->height != enc->canvas_height) {
1637
0
      frame->error_code = VP8_ENC_ERROR_INVALID_CONFIGURATION;
1638
0
      MarkError(enc, "ERROR adding frame: Invalid frame dimensions");
1639
0
      return 0;
1640
0
    }
1641
1642
0
    if (!ValidateAllocatedPicture(frame)) {
1643
0
      MarkError2(enc, "ERROR adding frame. WebPEncodingError",
1644
0
                 frame->error_code);
1645
0
      return 0;
1646
0
    }
1647
1648
0
    if (!frame->use_argb) {  // Convert frame from YUV(A) to ARGB.
1649
0
      if (enc->options.verbose) {
1650
0
        fprintf(stderr,
1651
0
                "WARNING: Converting frame from YUV(A) to ARGB format; "
1652
0
                "this incurs a small loss.\n");
1653
0
      }
1654
0
      if (!WebPPictureYUVAToARGB(frame)) {
1655
0
        MarkError(enc, "ERROR converting frame from YUV(A) to ARGB");
1656
0
        return 0;
1657
0
      }
1658
0
    }
1659
1660
0
    if (encoder_config != NULL) {
1661
0
      if (!WebPValidateConfig(encoder_config)) {
1662
0
        frame->error_code = VP8_ENC_ERROR_INVALID_CONFIGURATION;
1663
0
        MarkError(enc, "ERROR adding frame: Invalid WebPConfig");
1664
0
        return 0;
1665
0
      }
1666
0
      config = *encoder_config;
1667
0
    } else {
1668
0
      if (!WebPConfigInit(&config)) {
1669
0
        frame->error_code = VP8_ENC_ERROR_INVALID_CONFIGURATION;
1670
0
        MarkError(enc, "Cannot Init config");
1671
0
        return 0;
1672
0
      }
1673
0
      config.lossless = 1;
1674
0
    }
1675
0
  }
1676
1677
0
  if (!enc->is_first_frame) {
1678
0
    EncodedFrame* const prev_enc_frame = GetFrame(enc, enc->count - 1);
1679
0
    prev_duration = prev_enc_frame->sub_frame.duration;
1680
0
    will_split = (prev_duration + (int)prev_frame_duration >= MAX_DURATION);
1681
0
    if (!IncreasePreviousDuration(enc, (int)prev_frame_duration)) {
1682
0
      if (frame != NULL) {
1683
0
        frame->error_code = VP8_ENC_ERROR_OUT_OF_MEMORY;
1684
0
      }
1685
0
      MarkError(enc, "ERROR adding frame: Out of memory");
1686
0
      return 0;
1687
0
    }
1688
    // IncreasePreviousDuration() may add a frame to avoid exceeding
1689
    // MAX_DURATION which could cause CacheFrame() to over read 'encoded_frames'
1690
    // before the next flush.
1691
0
    if ((enc->flush_count > 0 || enc->count == enc->size) &&
1692
0
        !FlushFrames(enc)) {
1693
0
      if (frame != NULL) {
1694
0
        frame->error_code = VP8_ENC_ERROR_OUT_OF_MEMORY;
1695
0
      }
1696
0
      return 0;
1697
0
    }
1698
0
  } else {
1699
0
    enc->first_timestamp = timestamp;
1700
0
  }
1701
1702
0
  if (frame == NULL) {  // Special: last call.
1703
0
    enc->got_null_frame = 1;
1704
0
    enc->prev_timestamp = timestamp;
1705
0
    return 1;
1706
0
  }
1707
1708
0
  assert(enc->curr_canvas == NULL);
1709
0
  enc->curr_canvas = frame;  // Store reference.
1710
0
  assert(enc->curr_canvas_copy_modified == 1);
1711
0
  CopyCurrentCanvas(enc);
1712
1713
0
  ok = CacheFrame(enc, &config) && FlushFrames(enc);
1714
0
  if (!ok && frame->error_code == VP8_ENC_OK) {
1715
0
    frame->error_code = VP8_ENC_ERROR_OUT_OF_MEMORY;
1716
0
  }
1717
1718
0
  enc->curr_canvas = NULL;
1719
0
  enc->curr_canvas_copy.progress_hook = NULL;
1720
0
  enc->curr_canvas_copy.user_data = NULL;
1721
0
  enc->curr_canvas_copy.use_argb = 1;
1722
0
  enc->curr_canvas_copy_modified = 1;
1723
0
  if (ok) {
1724
0
    enc->prev_timestamp = timestamp;
1725
0
  } else if (!enc->is_first_frame) {
1726
0
    if (will_split) {
1727
0
      enc->prev_timestamp = timestamp;
1728
0
    } else {
1729
0
      EncodedFrame* const prev_enc_frame = GetFrame(enc, enc->count - 1);
1730
0
      prev_enc_frame->sub_frame.duration = prev_duration;
1731
0
      prev_enc_frame->key_frame.duration = prev_duration;
1732
0
    }
1733
0
  }
1734
0
  return ok;
1735
0
}
1736
1737
// -----------------------------------------------------------------------------
1738
// Bitstream assembly.
1739
1740
WEBP_NODISCARD static int DecodeFrameOntoCanvas(
1741
0
    const WebPMuxFrameInfo* const frame, WebPPicture* const canvas) {
1742
0
  const WebPData* const image = &frame->bitstream;
1743
0
  WebPPicture sub_image;
1744
0
  WebPDecoderConfig config;
1745
0
  if (!WebPInitDecoderConfig(&config)) {
1746
0
    return 0;
1747
0
  }
1748
0
  WebPUtilClearPic(canvas, NULL);
1749
0
  if (WebPGetFeatures(image->bytes, image->size, &config.input) !=
1750
0
      VP8_STATUS_OK) {
1751
0
    return 0;
1752
0
  }
1753
0
  if (!WebPPictureView(canvas, frame->x_offset, frame->y_offset,
1754
0
                       config.input.width, config.input.height, &sub_image)) {
1755
0
    return 0;
1756
0
  }
1757
0
  config.output.is_external_memory = 1;
1758
#ifdef WORDS_BIGENDIAN
1759
  config.output.colorspace = MODE_ARGB;
1760
#else
1761
0
  config.output.colorspace = MODE_BGRA;
1762
0
#endif
1763
0
  config.output.u.RGBA.rgba = (uint8_t*)sub_image.argb;
1764
0
  config.output.u.RGBA.stride = sub_image.argb_stride * 4;
1765
0
  config.output.u.RGBA.size =
1766
0
      (size_t)config.output.u.RGBA.stride * sub_image.height;
1767
1768
0
  if (WebPDecode(image->bytes, image->size, &config) != VP8_STATUS_OK) {
1769
0
    return 0;
1770
0
  }
1771
0
  return 1;
1772
0
}
1773
1774
WEBP_NODISCARD static int FrameToFullCanvas(WebPAnimEncoder* const enc,
1775
                                            const WebPMuxFrameInfo* const frame,
1776
0
                                            WebPData* const full_image) {
1777
0
  WebPPicture* const canvas_buf = &enc->curr_canvas_copy;
1778
0
  WebPMemoryWriter mem1, mem2;
1779
0
  WebPMemoryWriterInit(&mem1);
1780
0
  WebPMemoryWriterInit(&mem2);
1781
1782
0
  if (!DecodeFrameOntoCanvas(frame, canvas_buf)) goto Err;
1783
0
  if (!EncodeFrame(&enc->last_config, canvas_buf, &mem1)) goto Err;
1784
1785
0
  if (enc->options.allow_mixed) {
1786
0
    if (!EncodeFrame(&enc->last_config_reversed, canvas_buf, &mem2)) goto Err;
1787
0
    if (mem2.size < mem1.size) {
1788
0
      GetEncodedData(&mem2, full_image);
1789
0
      WebPMemoryWriterClear(&mem1);
1790
0
    } else {
1791
0
      GetEncodedData(&mem1, full_image);
1792
0
      WebPMemoryWriterClear(&mem2);
1793
0
    }
1794
0
  } else {
1795
0
    GetEncodedData(&mem1, full_image);
1796
0
  }
1797
0
  canvas_buf->use_argb = 1;
1798
0
  return 1;
1799
1800
0
Err:
1801
0
  canvas_buf->use_argb = 1;
1802
0
  WebPDataInit(full_image);
1803
0
  WebPMemoryWriterClear(&mem1);
1804
0
  WebPMemoryWriterClear(&mem2);
1805
0
  return 0;
1806
0
}
1807
1808
// Convert a single-frame animation to a non-animated image if appropriate.
1809
// TODO(urvang): Can we pick one of the two heuristically (based on frame
1810
// rectangle and/or presence of alpha)?
1811
static WebPMuxError OptimizeSingleFrame(WebPAnimEncoder* const enc,
1812
0
                                        WebPData* const webp_data) {
1813
0
  WebPMuxError err = WEBP_MUX_OK;
1814
0
  int canvas_width, canvas_height;
1815
0
  WebPMuxFrameInfo frame;
1816
0
  WebPData full_image;
1817
0
  WebPData webp_data2;
1818
0
  WebPMux* const mux = WebPMuxCreate(webp_data, 0);
1819
0
  if (mux == NULL) return WEBP_MUX_BAD_DATA;
1820
0
  assert(enc->out_frame_count == 1);
1821
0
  WebPDataInit(&frame.bitstream);
1822
0
  WebPDataInit(&full_image);
1823
0
  WebPDataInit(&webp_data2);
1824
1825
0
  err = WebPMuxGetFrame(mux, 1, &frame);
1826
0
  if (err != WEBP_MUX_OK) goto End;
1827
0
  if (frame.id != WEBP_CHUNK_ANMF) goto End;  // Non-animation: nothing to do.
1828
0
  err = WebPMuxGetCanvasSize(mux, &canvas_width, &canvas_height);
1829
0
  if (err != WEBP_MUX_OK) goto End;
1830
0
  if (!FrameToFullCanvas(enc, &frame, &full_image)) {
1831
0
    err = WEBP_MUX_BAD_DATA;
1832
0
    goto End;
1833
0
  }
1834
0
  err = WebPMuxSetImage(mux, &full_image, 1);
1835
0
  if (err != WEBP_MUX_OK) goto End;
1836
0
  err = WebPMuxAssemble(mux, &webp_data2);
1837
0
  if (err != WEBP_MUX_OK) goto End;
1838
1839
0
  if (webp_data2.size < webp_data->size) {  // Pick 'webp_data2' if smaller.
1840
0
    WebPDataClear(webp_data);
1841
0
    *webp_data = webp_data2;
1842
0
    WebPDataInit(&webp_data2);
1843
0
  }
1844
1845
0
End:
1846
0
  WebPDataClear(&frame.bitstream);
1847
0
  WebPDataClear(&full_image);
1848
0
  WebPMuxDelete(mux);
1849
0
  WebPDataClear(&webp_data2);
1850
0
  return err;
1851
0
}
1852
1853
0
int WebPAnimEncoderAssemble(WebPAnimEncoder* enc, WebPData* webp_data) {
1854
0
  WebPMux* mux;
1855
0
  WebPMuxError err;
1856
1857
0
  if (enc == NULL) {
1858
0
    return 0;
1859
0
  }
1860
0
  MarkNoError(enc);
1861
1862
0
  if (webp_data == NULL) {
1863
0
    MarkError(enc, "ERROR assembling: NULL input");
1864
0
    return 0;
1865
0
  }
1866
0
  WebPDataInit(webp_data);
1867
1868
0
  if (enc->in_frame_count == 0) {
1869
0
    MarkError(enc, "ERROR: No frames to assemble");
1870
0
    return 0;
1871
0
  }
1872
1873
0
  if (!enc->got_null_frame && enc->in_frame_count > 1 && enc->count > 0) {
1874
    // set duration of the last frame to be avg of durations of previous frames.
1875
0
    const double delta_time =
1876
0
        (uint32_t)enc->prev_timestamp - enc->first_timestamp;
1877
0
    const int average_duration = (int)(delta_time / (enc->in_frame_count - 1));
1878
0
    if (!IncreasePreviousDuration(enc, average_duration)) {
1879
0
      MarkError(enc, "ERROR assembling: Out of memory");
1880
0
      return 0;
1881
0
    }
1882
0
  }
1883
1884
  // Flush any remaining frames.
1885
0
  enc->flush_count = enc->count;
1886
0
  if (!FlushFrames(enc)) {
1887
0
    return 0;
1888
0
  }
1889
1890
  // Set definitive canvas size.
1891
0
  mux = enc->mux;
1892
0
  err = WebPMuxSetCanvasSize(mux, enc->canvas_width, enc->canvas_height);
1893
0
  if (err != WEBP_MUX_OK) goto Err;
1894
1895
0
  err = WebPMuxSetAnimationParams(mux, &enc->options.anim_params);
1896
0
  if (err != WEBP_MUX_OK) goto Err;
1897
1898
  // Assemble into a WebP bitstream.
1899
0
  err = WebPMuxAssemble(mux, webp_data);
1900
0
  if (err != WEBP_MUX_OK) goto Err;
1901
1902
0
  if (enc->out_frame_count == 1) {
1903
0
    err = OptimizeSingleFrame(enc, webp_data);
1904
0
    if (err != WEBP_MUX_OK) goto Err;
1905
0
  }
1906
0
  return 1;
1907
1908
0
Err:
1909
0
  WebPDataClear(webp_data);
1910
0
  MarkError2(enc, "ERROR assembling WebP", err);
1911
0
  return 0;
1912
0
}
1913
1914
0
const char* WebPAnimEncoderGetError(WebPAnimEncoder* enc) {
1915
0
  if (enc == NULL) return NULL;
1916
0
  return enc->error_str;
1917
0
}
1918
1919
WebPMuxError WebPAnimEncoderSetChunk(WebPAnimEncoder* enc, const char fourcc[4],
1920
                                     const WebPData* chunk_data,
1921
0
                                     int copy_data) {
1922
0
  if (enc == NULL) return WEBP_MUX_INVALID_ARGUMENT;
1923
0
  return WebPMuxSetChunk(enc->mux, fourcc, chunk_data, copy_data);
1924
0
}
1925
1926
WebPMuxError WebPAnimEncoderGetChunk(const WebPAnimEncoder* enc,
1927
                                     const char fourcc[4],
1928
0
                                     WebPData* chunk_data) {
1929
0
  if (enc == NULL) return WEBP_MUX_INVALID_ARGUMENT;
1930
0
  return WebPMuxGetChunk(enc->mux, fourcc, chunk_data);
1931
0
}
1932
1933
WebPMuxError WebPAnimEncoderDeleteChunk(WebPAnimEncoder* enc,
1934
0
                                        const char fourcc[4]) {
1935
0
  if (enc == NULL) return WEBP_MUX_INVALID_ARGUMENT;
1936
0
  return WebPMuxDeleteChunk(enc->mux, fourcc);
1937
0
}
1938
1939
// -----------------------------------------------------------------------------