| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * WebP (.webp) image decoder | ||
| 3 | * Copyright (c) 2013 Aneesh Dogra <aneesh@sugarlabs.org> | ||
| 4 | * Copyright (c) 2013 Justin Ruggles <justin.ruggles@gmail.com> | ||
| 5 | * Copyright (c) 2020 Pexeso Inc. | ||
| 6 | * | ||
| 7 | * This file is part of FFmpeg. | ||
| 8 | * | ||
| 9 | * FFmpeg is free software; you can redistribute it and/or | ||
| 10 | * modify it under the terms of the GNU Lesser General Public | ||
| 11 | * License as published by the Free Software Foundation; either | ||
| 12 | * version 2.1 of the License, or (at your option) any later version. | ||
| 13 | * | ||
| 14 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 17 | * Lesser General Public License for more details. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the GNU Lesser General Public | ||
| 20 | * License along with FFmpeg; if not, write to the Free Software | ||
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 22 | */ | ||
| 23 | |||
| 24 | /** | ||
| 25 | * @file | ||
| 26 | * WebP image decoder | ||
| 27 | * | ||
| 28 | * @author Aneesh Dogra <aneesh@sugarlabs.org> | ||
| 29 | * Container and Lossy decoding | ||
| 30 | * | ||
| 31 | * @author Justin Ruggles <justin.ruggles@gmail.com> | ||
| 32 | * Lossless decoder | ||
| 33 | * Compressed alpha for lossy | ||
| 34 | * | ||
| 35 | * @author James Almer <jamrial@gmail.com> | ||
| 36 | * Exif metadata | ||
| 37 | * ICC profile | ||
| 38 | * | ||
| 39 | * @author Thilo Borgmann <thilo.borgmann _at_ mail.de> | ||
| 40 | * XMP metadata | ||
| 41 | * | ||
| 42 | * @author Josef Zlomek, Pexeso Inc. <josef@pex.com> | ||
| 43 | * Animation | ||
| 44 | */ | ||
| 45 | |||
| 46 | #include "config_components.h" | ||
| 47 | |||
| 48 | #include "libavutil/colorspace.h" | ||
| 49 | #include "libavutil/imgutils.h" | ||
| 50 | #include "libavutil/mem.h" | ||
| 51 | |||
| 52 | #define BITSTREAM_READER_LE | ||
| 53 | #include "avcodec.h" | ||
| 54 | #include "bytestream.h" | ||
| 55 | #include "codec_internal.h" | ||
| 56 | #include "decode.h" | ||
| 57 | #include "exif_internal.h" | ||
| 58 | #include "get_bits.h" | ||
| 59 | #include "thread.h" | ||
| 60 | #include "tiff_common.h" | ||
| 61 | #include "vp8.h" | ||
| 62 | |||
| 63 | #define VP8X_FLAG_ANIMATION 0x02 | ||
| 64 | #define VP8X_FLAG_XMP_METADATA 0x04 | ||
| 65 | #define VP8X_FLAG_EXIF_METADATA 0x08 | ||
| 66 | #define VP8X_FLAG_ALPHA 0x10 | ||
| 67 | #define VP8X_FLAG_ICC 0x20 | ||
| 68 | |||
| 69 | #define MAX_PALETTE_SIZE 256 | ||
| 70 | #define MAX_CACHE_BITS 11 | ||
| 71 | #define NUM_CODE_LENGTH_CODES 19 | ||
| 72 | #define HUFFMAN_CODES_PER_META_CODE 5 | ||
| 73 | #define NUM_LITERAL_CODES 256 | ||
| 74 | #define NUM_LENGTH_CODES 24 | ||
| 75 | #define NUM_DISTANCE_CODES 40 | ||
| 76 | #define NUM_SHORT_DISTANCES 120 | ||
| 77 | #define MAX_HUFFMAN_CODE_LENGTH 15 | ||
| 78 | |||
| 79 | static const uint16_t alphabet_sizes[HUFFMAN_CODES_PER_META_CODE] = { | ||
| 80 | NUM_LITERAL_CODES + NUM_LENGTH_CODES, | ||
| 81 | NUM_LITERAL_CODES, NUM_LITERAL_CODES, NUM_LITERAL_CODES, | ||
| 82 | NUM_DISTANCE_CODES | ||
| 83 | }; | ||
| 84 | |||
| 85 | static const uint8_t code_length_code_order[NUM_CODE_LENGTH_CODES] = { | ||
| 86 | 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 | ||
| 87 | }; | ||
| 88 | |||
| 89 | static const int8_t lz77_distance_offsets[NUM_SHORT_DISTANCES][2] = { | ||
| 90 | { 0, 1 }, { 1, 0 }, { 1, 1 }, { -1, 1 }, { 0, 2 }, { 2, 0 }, { 1, 2 }, { -1, 2 }, | ||
| 91 | { 2, 1 }, { -2, 1 }, { 2, 2 }, { -2, 2 }, { 0, 3 }, { 3, 0 }, { 1, 3 }, { -1, 3 }, | ||
| 92 | { 3, 1 }, { -3, 1 }, { 2, 3 }, { -2, 3 }, { 3, 2 }, { -3, 2 }, { 0, 4 }, { 4, 0 }, | ||
| 93 | { 1, 4 }, { -1, 4 }, { 4, 1 }, { -4, 1 }, { 3, 3 }, { -3, 3 }, { 2, 4 }, { -2, 4 }, | ||
| 94 | { 4, 2 }, { -4, 2 }, { 0, 5 }, { 3, 4 }, { -3, 4 }, { 4, 3 }, { -4, 3 }, { 5, 0 }, | ||
| 95 | { 1, 5 }, { -1, 5 }, { 5, 1 }, { -5, 1 }, { 2, 5 }, { -2, 5 }, { 5, 2 }, { -5, 2 }, | ||
| 96 | { 4, 4 }, { -4, 4 }, { 3, 5 }, { -3, 5 }, { 5, 3 }, { -5, 3 }, { 0, 6 }, { 6, 0 }, | ||
| 97 | { 1, 6 }, { -1, 6 }, { 6, 1 }, { -6, 1 }, { 2, 6 }, { -2, 6 }, { 6, 2 }, { -6, 2 }, | ||
| 98 | { 4, 5 }, { -4, 5 }, { 5, 4 }, { -5, 4 }, { 3, 6 }, { -3, 6 }, { 6, 3 }, { -6, 3 }, | ||
| 99 | { 0, 7 }, { 7, 0 }, { 1, 7 }, { -1, 7 }, { 5, 5 }, { -5, 5 }, { 7, 1 }, { -7, 1 }, | ||
| 100 | { 4, 6 }, { -4, 6 }, { 6, 4 }, { -6, 4 }, { 2, 7 }, { -2, 7 }, { 7, 2 }, { -7, 2 }, | ||
| 101 | { 3, 7 }, { -3, 7 }, { 7, 3 }, { -7, 3 }, { 5, 6 }, { -5, 6 }, { 6, 5 }, { -6, 5 }, | ||
| 102 | { 8, 0 }, { 4, 7 }, { -4, 7 }, { 7, 4 }, { -7, 4 }, { 8, 1 }, { 8, 2 }, { 6, 6 }, | ||
| 103 | { -6, 6 }, { 8, 3 }, { 5, 7 }, { -5, 7 }, { 7, 5 }, { -7, 5 }, { 8, 4 }, { 6, 7 }, | ||
| 104 | { -6, 7 }, { 7, 6 }, { -7, 6 }, { 8, 5 }, { 7, 7 }, { -7, 7 }, { 8, 6 }, { 8, 7 } | ||
| 105 | }; | ||
| 106 | |||
| 107 | enum AlphaCompression { | ||
| 108 | ALPHA_COMPRESSION_NONE, | ||
| 109 | ALPHA_COMPRESSION_VP8L, | ||
| 110 | }; | ||
| 111 | |||
| 112 | enum AlphaFilter { | ||
| 113 | ALPHA_FILTER_NONE, | ||
| 114 | ALPHA_FILTER_HORIZONTAL, | ||
| 115 | ALPHA_FILTER_VERTICAL, | ||
| 116 | ALPHA_FILTER_GRADIENT, | ||
| 117 | }; | ||
| 118 | |||
| 119 | enum TransformType { | ||
| 120 | PREDICTOR_TRANSFORM = 0, | ||
| 121 | COLOR_TRANSFORM = 1, | ||
| 122 | SUBTRACT_GREEN = 2, | ||
| 123 | COLOR_INDEXING_TRANSFORM = 3, | ||
| 124 | }; | ||
| 125 | |||
| 126 | enum PredictionMode { | ||
| 127 | PRED_MODE_BLACK, | ||
| 128 | PRED_MODE_L, | ||
| 129 | PRED_MODE_T, | ||
| 130 | PRED_MODE_TR, | ||
| 131 | PRED_MODE_TL, | ||
| 132 | PRED_MODE_AVG_T_AVG_L_TR, | ||
| 133 | PRED_MODE_AVG_L_TL, | ||
| 134 | PRED_MODE_AVG_L_T, | ||
| 135 | PRED_MODE_AVG_TL_T, | ||
| 136 | PRED_MODE_AVG_T_TR, | ||
| 137 | PRED_MODE_AVG_AVG_L_TL_AVG_T_TR, | ||
| 138 | PRED_MODE_SELECT, | ||
| 139 | PRED_MODE_ADD_SUBTRACT_FULL, | ||
| 140 | PRED_MODE_ADD_SUBTRACT_HALF, | ||
| 141 | }; | ||
| 142 | |||
| 143 | enum HuffmanIndex { | ||
| 144 | HUFF_IDX_GREEN = 0, | ||
| 145 | HUFF_IDX_RED = 1, | ||
| 146 | HUFF_IDX_BLUE = 2, | ||
| 147 | HUFF_IDX_ALPHA = 3, | ||
| 148 | HUFF_IDX_DIST = 4 | ||
| 149 | }; | ||
| 150 | |||
| 151 | /* The structure of WebP lossless is an optional series of transformation data, | ||
| 152 | * followed by the primary image. The primary image also optionally contains | ||
| 153 | * an entropy group mapping if there are multiple entropy groups. There is a | ||
| 154 | * basic image type called an "entropy coded image" that is used for all of | ||
| 155 | * these. The type of each entropy coded image is referred to by the | ||
| 156 | * specification as its role. */ | ||
| 157 | enum ImageRole { | ||
| 158 | /* Primary Image: Stores the actual pixels of the image. */ | ||
| 159 | IMAGE_ROLE_ARGB, | ||
| 160 | |||
| 161 | /* Entropy Image: Defines which Huffman group to use for different areas of | ||
| 162 | * the primary image. */ | ||
| 163 | IMAGE_ROLE_ENTROPY, | ||
| 164 | |||
| 165 | /* Predictors: Defines which predictor type to use for different areas of | ||
| 166 | * the primary image. */ | ||
| 167 | IMAGE_ROLE_PREDICTOR, | ||
| 168 | |||
| 169 | /* Color Transform Data: Defines the color transformation for different | ||
| 170 | * areas of the primary image. */ | ||
| 171 | IMAGE_ROLE_COLOR_TRANSFORM, | ||
| 172 | |||
| 173 | /* Color Index: Stored as an image of height == 1. */ | ||
| 174 | IMAGE_ROLE_COLOR_INDEXING, | ||
| 175 | |||
| 176 | IMAGE_ROLE_NB, | ||
| 177 | }; | ||
| 178 | |||
| 179 | typedef struct HuffReader { | ||
| 180 | VLC vlc; /* Huffman decoder context */ | ||
| 181 | int simple; /* whether to use simple mode */ | ||
| 182 | int nb_symbols; /* number of coded symbols */ | ||
| 183 | uint16_t simple_symbols[2]; /* symbols for simple mode */ | ||
| 184 | } HuffReader; | ||
| 185 | |||
| 186 | typedef struct ImageContext { | ||
| 187 | enum ImageRole role; /* role of this image */ | ||
| 188 | AVFrame *frame; /* AVFrame for data */ | ||
| 189 | int color_cache_bits; /* color cache size, log2 */ | ||
| 190 | uint32_t *color_cache; /* color cache data */ | ||
| 191 | int nb_huffman_groups; /* number of huffman groups */ | ||
| 192 | HuffReader *huffman_groups; /* reader for each huffman group */ | ||
| 193 | /* relative size compared to primary image, log2. | ||
| 194 | * for IMAGE_ROLE_COLOR_INDEXING with <= 16 colors, this is log2 of the | ||
| 195 | * number of pixels per byte in the primary image (pixel packing) */ | ||
| 196 | int size_reduction; | ||
| 197 | int is_alpha_primary; | ||
| 198 | } ImageContext; | ||
| 199 | |||
| 200 | typedef struct WebPContext { | ||
| 201 | VP8Context v; /* VP8 Context used for lossy decoding */ | ||
| 202 | GetBitContext gb; /* bitstream reader for main image chunk */ | ||
| 203 | AVFrame *alpha_frame; /* AVFrame for alpha data decompressed from VP8L */ | ||
| 204 | AVPacket *pkt; /* AVPacket to be passed to the underlying VP8 decoder */ | ||
| 205 | AVCodecContext *avctx; /* parent AVCodecContext */ | ||
| 206 | int initialized; /* set once the VP8 context is initialized */ | ||
| 207 | int has_alpha; /* has a separate alpha chunk */ | ||
| 208 | enum AlphaCompression alpha_compression; /* compression type for alpha chunk */ | ||
| 209 | enum AlphaFilter alpha_filter; /* filtering method for alpha chunk */ | ||
| 210 | const uint8_t *alpha_data; /* alpha chunk data */ | ||
| 211 | int alpha_data_size; /* alpha chunk data size */ | ||
| 212 | int has_exif; /* set after an EXIF chunk has been processed */ | ||
| 213 | int has_iccp; /* set after an ICCP chunk has been processed */ | ||
| 214 | int has_xmp; /* set after an XMP chunk has been processed */ | ||
| 215 | int width; /* image width */ | ||
| 216 | int height; /* image height */ | ||
| 217 | |||
| 218 | int nb_transforms; /* number of transforms */ | ||
| 219 | enum TransformType transforms[4]; /* transformations used in the image, in order */ | ||
| 220 | /* reduced width when using a color indexing transform with <= 16 colors (pixel packing) | ||
| 221 | * before pixels are unpacked, or same as width otherwise. */ | ||
| 222 | int reduced_width; | ||
| 223 | int nb_huffman_groups; /* number of huffman groups in the primary image */ | ||
| 224 | ImageContext image[IMAGE_ROLE_NB]; /* image context for each role */ | ||
| 225 | } WebPContext; | ||
| 226 | |||
| 227 | #define GET_PIXEL(frame, x, y) \ | ||
| 228 | ((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x)) | ||
| 229 | |||
| 230 | #define GET_PIXEL_COMP(frame, x, y, c) \ | ||
| 231 | (*((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x) + c)) | ||
| 232 | |||
| 233 | 595 | static void image_ctx_free(ImageContext *img) | |
| 234 | { | ||
| 235 | int i, j; | ||
| 236 | |||
| 237 | 595 | av_free(img->color_cache); | |
| 238 |
3/4✓ Branch 0 taken 149 times.
✓ Branch 1 taken 446 times.
✓ Branch 2 taken 149 times.
✗ Branch 3 not taken.
|
595 | if (img->role != IMAGE_ROLE_ARGB && !img->is_alpha_primary) |
| 239 | 149 | av_frame_free(&img->frame); | |
| 240 |
2/2✓ Branch 0 taken 268 times.
✓ Branch 1 taken 327 times.
|
595 | if (img->huffman_groups) { |
| 241 |
2/2✓ Branch 0 taken 298 times.
✓ Branch 1 taken 268 times.
|
566 | for (i = 0; i < img->nb_huffman_groups; i++) { |
| 242 |
2/2✓ Branch 0 taken 1490 times.
✓ Branch 1 taken 298 times.
|
1788 | for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; j++) |
| 243 | 1490 | ff_vlc_free(&img->huffman_groups[i * HUFFMAN_CODES_PER_META_CODE + j].vlc); | |
| 244 | } | ||
| 245 | 268 | av_free(img->huffman_groups); | |
| 246 | } | ||
| 247 | 595 | memset(img, 0, sizeof(*img)); | |
| 248 | 595 | } | |
| 249 | |||
| 250 | 909961 | static int huff_reader_get_symbol(HuffReader *r, GetBitContext *gb) | |
| 251 | { | ||
| 252 |
2/2✓ Branch 0 taken 487590 times.
✓ Branch 1 taken 422371 times.
|
909961 | if (r->simple) { |
| 253 |
2/2✓ Branch 0 taken 467678 times.
✓ Branch 1 taken 19912 times.
|
487590 | if (r->nb_symbols == 1) |
| 254 | 467678 | return r->simple_symbols[0]; | |
| 255 | else | ||
| 256 | 19912 | return r->simple_symbols[get_bits1(gb)]; | |
| 257 | } else | ||
| 258 | 422371 | return get_vlc2(gb, r->vlc.table, 8, 2); | |
| 259 | } | ||
| 260 | |||
| 261 | 1106 | static int huff_reader_build_canonical(HuffReader *r, const uint8_t *code_lengths, | |
| 262 | uint16_t len_counts[MAX_HUFFMAN_CODE_LENGTH + 1], | ||
| 263 | uint8_t lens[], uint16_t syms[], | ||
| 264 | int alphabet_size, void *logctx) | ||
| 265 | { | ||
| 266 | 1106 | unsigned nb_codes = 0; | |
| 267 | int ret; | ||
| 268 | |||
| 269 | // Count the number of symbols of each length and transform len_counts | ||
| 270 | // into an array of offsets. | ||
| 271 |
2/2✓ Branch 0 taken 16590 times.
✓ Branch 1 taken 1106 times.
|
17696 | for (int len = 1; len <= MAX_HUFFMAN_CODE_LENGTH; ++len) { |
| 272 | 16590 | unsigned cnt = len_counts[len]; | |
| 273 | 16590 | len_counts[len] = nb_codes; | |
| 274 | 16590 | nb_codes += cnt; | |
| 275 | } | ||
| 276 | |||
| 277 |
2/2✓ Branch 0 taken 150106 times.
✓ Branch 1 taken 1106 times.
|
151212 | for (int sym = 0; sym < alphabet_size; ++sym) { |
| 278 |
2/2✓ Branch 0 taken 40151 times.
✓ Branch 1 taken 109955 times.
|
150106 | if (code_lengths[sym]) { |
| 279 | 40151 | unsigned idx = len_counts[code_lengths[sym]]++; | |
| 280 | 40151 | syms[idx] = sym; | |
| 281 | 40151 | lens[idx] = code_lengths[sym]; | |
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1106 times.
|
1106 | if (nb_codes == 0) { |
| 286 | // No symbols | ||
| 287 | ✗ | return AVERROR_INVALIDDATA; | |
| 288 | } | ||
| 289 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1106 times.
|
1106 | if (nb_codes == 1) { |
| 290 | // Special-case 1 symbol since the VLC reader cannot handle it | ||
| 291 | ✗ | r->nb_symbols = 1; | |
| 292 | ✗ | r->simple = 1; | |
| 293 | ✗ | r->simple_symbols[0] = syms[0]; | |
| 294 | ✗ | return 0; | |
| 295 | } | ||
| 296 | |||
| 297 | 1106 | ret = ff_vlc_init_from_lengths(&r->vlc, 8, nb_codes, lens, 1, | |
| 298 | syms, 2, 2, 0, VLC_INIT_OUTPUT_LE, logctx); | ||
| 299 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1106 times.
|
1106 | if (ret < 0) |
| 300 | ✗ | return ret; | |
| 301 | 1106 | r->simple = 0; | |
| 302 | |||
| 303 | 1106 | return 0; | |
| 304 | } | ||
| 305 | |||
| 306 | 937 | static void read_huffman_code_simple(WebPContext *s, HuffReader *hc) | |
| 307 | { | ||
| 308 | 937 | hc->nb_symbols = get_bits1(&s->gb) + 1; | |
| 309 | |||
| 310 |
2/2✓ Branch 1 taken 153 times.
✓ Branch 2 taken 784 times.
|
937 | if (get_bits1(&s->gb)) |
| 311 | 153 | hc->simple_symbols[0] = get_bits(&s->gb, 8); | |
| 312 | else | ||
| 313 | 784 | hc->simple_symbols[0] = get_bits1(&s->gb); | |
| 314 | |||
| 315 |
2/2✓ Branch 0 taken 222 times.
✓ Branch 1 taken 715 times.
|
937 | if (hc->nb_symbols == 2) |
| 316 | 222 | hc->simple_symbols[1] = get_bits(&s->gb, 8); | |
| 317 | |||
| 318 | 937 | hc->simple = 1; | |
| 319 | 937 | } | |
| 320 | |||
| 321 | 553 | static int read_huffman_code_normal(WebPContext *s, HuffReader *hc, | |
| 322 | int alphabet_size) | ||
| 323 | { | ||
| 324 | 553 | HuffReader code_len_hc = { { 0 }, 0, 0, { 0 } }; | |
| 325 | uint8_t *code_lengths; | ||
| 326 | 553 | uint8_t code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 }; | |
| 327 | uint8_t reordered_code_length_code_lengths[NUM_CODE_LENGTH_CODES]; | ||
| 328 | uint16_t reordered_code_length_syms[NUM_CODE_LENGTH_CODES]; | ||
| 329 | 553 | uint16_t len_counts[MAX_HUFFMAN_CODE_LENGTH + 1] = { 0 }; | |
| 330 | int symbol, max_symbol, prev_code_len, ret; | ||
| 331 | 553 | int num_codes = 4 + get_bits(&s->gb, 4); | |
| 332 | |||
| 333 | av_assert1(num_codes <= NUM_CODE_LENGTH_CODES); | ||
| 334 | |||
| 335 |
2/2✓ Branch 0 taken 6467 times.
✓ Branch 1 taken 553 times.
|
7020 | for (int i = 0; i < num_codes; i++) { |
| 336 | 6467 | unsigned len = get_bits(&s->gb, 3); | |
| 337 | 6467 | code_length_code_lengths[code_length_code_order[i]] = len; | |
| 338 | 6467 | len_counts[len]++; | |
| 339 | } | ||
| 340 | |||
| 341 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 2 taken 539 times.
|
553 | if (get_bits1(&s->gb)) { |
| 342 | 14 | int bits = 2 + 2 * get_bits(&s->gb, 3); | |
| 343 | 14 | max_symbol = 2 + get_bits(&s->gb, bits); | |
| 344 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (max_symbol > alphabet_size) { |
| 345 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "max symbol %d > alphabet size %d\n", | |
| 346 | max_symbol, alphabet_size); | ||
| 347 | ✗ | return AVERROR_INVALIDDATA; | |
| 348 | } | ||
| 349 | } else { | ||
| 350 | 539 | max_symbol = alphabet_size; | |
| 351 | } | ||
| 352 | |||
| 353 | 553 | ret = huff_reader_build_canonical(&code_len_hc, code_length_code_lengths, len_counts, | |
| 354 | reordered_code_length_code_lengths, | ||
| 355 | reordered_code_length_syms, | ||
| 356 | 553 | NUM_CODE_LENGTH_CODES, s->avctx); | |
| 357 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 553 times.
|
553 | if (ret < 0) |
| 358 | ✗ | return ret; | |
| 359 | |||
| 360 | 553 | code_lengths = av_malloc_array(alphabet_size, 2 * sizeof(uint8_t) + sizeof(uint16_t)); | |
| 361 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 553 times.
|
553 | if (!code_lengths) { |
| 362 | ✗ | ret = AVERROR(ENOMEM); | |
| 363 | ✗ | goto finish; | |
| 364 | } | ||
| 365 | |||
| 366 | 553 | prev_code_len = 8; | |
| 367 | 553 | symbol = 0; | |
| 368 | 553 | memset(len_counts, 0, sizeof(len_counts)); | |
| 369 |
2/2✓ Branch 0 taken 18532 times.
✓ Branch 1 taken 539 times.
|
19071 | while (symbol < alphabet_size) { |
| 370 | int code_len; | ||
| 371 | |||
| 372 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 18518 times.
|
18532 | if (!max_symbol--) |
| 373 | 14 | break; | |
| 374 | 18518 | code_len = huff_reader_get_symbol(&code_len_hc, &s->gb); | |
| 375 |
2/2✓ Branch 0 taken 11297 times.
✓ Branch 1 taken 7221 times.
|
18518 | if (code_len < 16U) { |
| 376 | /* Code length code [0..15] indicates literal code lengths. */ | ||
| 377 | 11297 | code_lengths[symbol++] = code_len; | |
| 378 | 11297 | len_counts[code_len]++; | |
| 379 |
2/2✓ Branch 0 taken 10836 times.
✓ Branch 1 taken 461 times.
|
11297 | if (code_len) |
| 380 | 10836 | prev_code_len = code_len; | |
| 381 | } else { | ||
| 382 | 7221 | int repeat = 0, length = 0; | |
| 383 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 4398 times.
✓ Branch 2 taken 1111 times.
✓ Branch 3 taken 1712 times.
|
7221 | switch (code_len) { |
| 384 | ✗ | default: | |
| 385 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 386 | ✗ | goto finish; | |
| 387 | 4398 | case 16: | |
| 388 | /* Code 16 repeats the previous non-zero value [3..6] times, | ||
| 389 | * i.e., 3 + ReadBits(2) times. If code 16 is used before a | ||
| 390 | * non-zero value has been emitted, a value of 8 is repeated. */ | ||
| 391 | 4398 | repeat = 3 + get_bits(&s->gb, 2); | |
| 392 | 4398 | length = prev_code_len; | |
| 393 | 4398 | len_counts[length] += repeat; | |
| 394 | 4398 | break; | |
| 395 | 1111 | case 17: | |
| 396 | /* Code 17 emits a streak of zeros [3..10], i.e., | ||
| 397 | * 3 + ReadBits(3) times. */ | ||
| 398 | 1111 | repeat = 3 + get_bits(&s->gb, 3); | |
| 399 | 1111 | break; | |
| 400 | 1712 | case 18: | |
| 401 | /* Code 18 emits a streak of zeros of length [11..138], i.e., | ||
| 402 | * 11 + ReadBits(7) times. */ | ||
| 403 | 1712 | repeat = 11 + get_bits(&s->gb, 7); | |
| 404 | 1712 | break; | |
| 405 | } | ||
| 406 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7221 times.
|
7221 | if (symbol + repeat > alphabet_size) { |
| 407 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
| 408 | "invalid symbol %d + repeat %d > alphabet size %d\n", | ||
| 409 | symbol, repeat, alphabet_size); | ||
| 410 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 411 | ✗ | goto finish; | |
| 412 | } | ||
| 413 |
2/2✓ Branch 0 taken 128302 times.
✓ Branch 1 taken 7221 times.
|
135523 | while (repeat-- > 0) |
| 414 | 128302 | code_lengths[symbol++] = length; | |
| 415 | } | ||
| 416 | } | ||
| 417 | |||
| 418 | 553 | ret = huff_reader_build_canonical(hc, code_lengths, len_counts, | |
| 419 | code_lengths + symbol, | ||
| 420 | 553 | (uint16_t*)(code_lengths + 2 * symbol), | |
| 421 | 553 | symbol, s->avctx); | |
| 422 | |||
| 423 | 553 | finish: | |
| 424 | 553 | ff_vlc_free(&code_len_hc.vlc); | |
| 425 | 553 | av_free(code_lengths); | |
| 426 | 553 | return ret; | |
| 427 | } | ||
| 428 | |||
| 429 | static int decode_entropy_coded_image(WebPContext *s, enum ImageRole role, | ||
| 430 | int w, int h); | ||
| 431 | |||
| 432 | #define PARSE_BLOCK_SIZE(w, h) do { \ | ||
| 433 | block_bits = get_bits(&s->gb, 3) + 2; \ | ||
| 434 | blocks_w = FFALIGN((w), 1 << block_bits) >> block_bits; \ | ||
| 435 | blocks_h = FFALIGN((h), 1 << block_bits) >> block_bits; \ | ||
| 436 | } while (0) | ||
| 437 | |||
| 438 | 25 | static int decode_entropy_image(WebPContext *s) | |
| 439 | { | ||
| 440 | ImageContext *img; | ||
| 441 | int ret, block_bits, blocks_w, blocks_h, x, y, max; | ||
| 442 | |||
| 443 | 25 | PARSE_BLOCK_SIZE(s->reduced_width, s->height); | |
| 444 | |||
| 445 | 25 | ret = decode_entropy_coded_image(s, IMAGE_ROLE_ENTROPY, blocks_w, blocks_h); | |
| 446 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | if (ret < 0) |
| 447 | ✗ | return ret; | |
| 448 | |||
| 449 | 25 | img = &s->image[IMAGE_ROLE_ENTROPY]; | |
| 450 | 25 | img->size_reduction = block_bits; | |
| 451 | |||
| 452 | /* the number of huffman groups is determined by the maximum group number | ||
| 453 | * coded in the entropy image */ | ||
| 454 | 25 | max = 0; | |
| 455 |
2/2✓ Branch 0 taken 193 times.
✓ Branch 1 taken 25 times.
|
218 | for (y = 0; y < img->frame->height; y++) { |
| 456 |
2/2✓ Branch 0 taken 3897 times.
✓ Branch 1 taken 193 times.
|
4090 | for (x = 0; x < img->frame->width; x++) { |
| 457 | 3897 | int p0 = GET_PIXEL_COMP(img->frame, x, y, 1); | |
| 458 | 3897 | int p1 = GET_PIXEL_COMP(img->frame, x, y, 2); | |
| 459 | 3897 | int p = p0 << 8 | p1; | |
| 460 | 3897 | max = FFMAX(max, p); | |
| 461 | } | ||
| 462 | } | ||
| 463 | 25 | s->nb_huffman_groups = max + 1; | |
| 464 | |||
| 465 | 25 | return 0; | |
| 466 | } | ||
| 467 | |||
| 468 | 6 | static int parse_transform_predictor(WebPContext *s) | |
| 469 | { | ||
| 470 | int block_bits, blocks_w, blocks_h, ret; | ||
| 471 | |||
| 472 | 6 | PARSE_BLOCK_SIZE(s->reduced_width, s->height); | |
| 473 | |||
| 474 | 6 | ret = decode_entropy_coded_image(s, IMAGE_ROLE_PREDICTOR, blocks_w, | |
| 475 | blocks_h); | ||
| 476 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (ret < 0) |
| 477 | ✗ | return ret; | |
| 478 | |||
| 479 | 6 | s->image[IMAGE_ROLE_PREDICTOR].size_reduction = block_bits; | |
| 480 | |||
| 481 | 6 | return 0; | |
| 482 | } | ||
| 483 | |||
| 484 | 4 | static int parse_transform_color(WebPContext *s) | |
| 485 | { | ||
| 486 | int block_bits, blocks_w, blocks_h, ret; | ||
| 487 | |||
| 488 | 4 | PARSE_BLOCK_SIZE(s->reduced_width, s->height); | |
| 489 | |||
| 490 | 4 | ret = decode_entropy_coded_image(s, IMAGE_ROLE_COLOR_TRANSFORM, blocks_w, | |
| 491 | blocks_h); | ||
| 492 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
| 493 | ✗ | return ret; | |
| 494 | |||
| 495 | 4 | s->image[IMAGE_ROLE_COLOR_TRANSFORM].size_reduction = block_bits; | |
| 496 | |||
| 497 | 4 | return 0; | |
| 498 | } | ||
| 499 | |||
| 500 | 114 | static int parse_transform_color_indexing(WebPContext *s) | |
| 501 | { | ||
| 502 | ImageContext *img; | ||
| 503 | int width_bits, index_size, ret, x; | ||
| 504 | uint8_t *ct; | ||
| 505 | |||
| 506 | 114 | index_size = get_bits(&s->gb, 8) + 1; | |
| 507 | |||
| 508 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 96 times.
|
114 | if (index_size <= 2) |
| 509 | 18 | width_bits = 3; | |
| 510 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 94 times.
|
96 | else if (index_size <= 4) |
| 511 | 2 | width_bits = 2; | |
| 512 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 93 times.
|
94 | else if (index_size <= 16) |
| 513 | 1 | width_bits = 1; | |
| 514 | else | ||
| 515 | 93 | width_bits = 0; | |
| 516 | |||
| 517 | 114 | ret = decode_entropy_coded_image(s, IMAGE_ROLE_COLOR_INDEXING, | |
| 518 | index_size, 1); | ||
| 519 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 114 times.
|
114 | if (ret < 0) |
| 520 | ✗ | return ret; | |
| 521 | |||
| 522 | 114 | img = &s->image[IMAGE_ROLE_COLOR_INDEXING]; | |
| 523 | 114 | img->size_reduction = width_bits; | |
| 524 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 93 times.
|
114 | if (width_bits > 0) |
| 525 | 21 | s->reduced_width = (s->width + ((1 << width_bits) - 1)) >> width_bits; | |
| 526 | |||
| 527 | /* color index values are delta-coded */ | ||
| 528 | 114 | ct = img->frame->data[0] + 4; | |
| 529 |
2/2✓ Branch 0 taken 28612 times.
✓ Branch 1 taken 114 times.
|
28726 | for (x = 4; x < img->frame->width * 4; x++, ct++) |
| 530 | 28612 | ct[0] += ct[-4]; | |
| 531 | |||
| 532 | 114 | return 0; | |
| 533 | } | ||
| 534 | |||
| 535 | 239514 | static HuffReader *get_huffman_group(WebPContext *s, ImageContext *img, | |
| 536 | int x, int y) | ||
| 537 | { | ||
| 538 | 239514 | ImageContext *gimg = &s->image[IMAGE_ROLE_ENTROPY]; | |
| 539 | 239514 | int group = 0; | |
| 540 | |||
| 541 |
2/2✓ Branch 0 taken 146268 times.
✓ Branch 1 taken 93246 times.
|
239514 | if (gimg->size_reduction > 0) { |
| 542 | 146268 | int group_x = x >> gimg->size_reduction; | |
| 543 | 146268 | int group_y = y >> gimg->size_reduction; | |
| 544 | 146268 | int g0 = GET_PIXEL_COMP(gimg->frame, group_x, group_y, 1); | |
| 545 | 146268 | int g1 = GET_PIXEL_COMP(gimg->frame, group_x, group_y, 2); | |
| 546 | 146268 | group = g0 << 8 | g1; | |
| 547 | } | ||
| 548 | |||
| 549 | 239514 | return &img->huffman_groups[group * HUFFMAN_CODES_PER_META_CODE]; | |
| 550 | } | ||
| 551 | |||
| 552 | 209778 | static av_always_inline void color_cache_put(ImageContext *img, uint32_t c) | |
| 553 | { | ||
| 554 | 209778 | uint32_t cache_idx = (0x1E35A7BD * c) >> (32 - img->color_cache_bits); | |
| 555 | 209778 | img->color_cache[cache_idx] = c; | |
| 556 | 209778 | } | |
| 557 | |||
| 558 | 268 | static int decode_entropy_coded_image(WebPContext *s, enum ImageRole role, | |
| 559 | int w, int h) | ||
| 560 | { | ||
| 561 | ImageContext *img; | ||
| 562 | HuffReader *hg; | ||
| 563 | int i, j, ret, x, y, width; | ||
| 564 | |||
| 565 | 268 | img = &s->image[role]; | |
| 566 | 268 | img->role = role; | |
| 567 | |||
| 568 |
2/2✓ Branch 0 taken 149 times.
✓ Branch 1 taken 119 times.
|
268 | if (!img->frame) { |
| 569 | 149 | img->frame = av_frame_alloc(); | |
| 570 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 149 times.
|
149 | if (!img->frame) |
| 571 | ✗ | return AVERROR(ENOMEM); | |
| 572 | } | ||
| 573 | |||
| 574 | 268 | img->frame->format = AV_PIX_FMT_ARGB; | |
| 575 | 268 | img->frame->width = w; | |
| 576 | 268 | img->frame->height = h; | |
| 577 | |||
| 578 |
4/4✓ Branch 0 taken 119 times.
✓ Branch 1 taken 149 times.
✓ Branch 2 taken 103 times.
✓ Branch 3 taken 16 times.
|
268 | if (role == IMAGE_ROLE_ARGB && !img->is_alpha_primary) { |
| 579 | 103 | ret = ff_thread_get_buffer(s->avctx, img->frame, 0); | |
| 580 | } else | ||
| 581 | 165 | ret = av_frame_get_buffer(img->frame, 1); | |
| 582 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 268 times.
|
268 | if (ret < 0) |
| 583 | ✗ | return ret; | |
| 584 | |||
| 585 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 264 times.
|
268 | if (get_bits1(&s->gb)) { |
| 586 | 4 | img->color_cache_bits = get_bits(&s->gb, 4); | |
| 587 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (img->color_cache_bits < 1 || img->color_cache_bits > 11) { |
| 588 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid color cache bits: %d\n", | |
| 589 | img->color_cache_bits); | ||
| 590 | ✗ | return AVERROR_INVALIDDATA; | |
| 591 | } | ||
| 592 | 4 | img->color_cache = av_calloc(1 << img->color_cache_bits, | |
| 593 | sizeof(*img->color_cache)); | ||
| 594 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!img->color_cache) |
| 595 | ✗ | return AVERROR(ENOMEM); | |
| 596 | } else { | ||
| 597 | 264 | img->color_cache_bits = 0; | |
| 598 | } | ||
| 599 | |||
| 600 | 268 | img->nb_huffman_groups = 1; | |
| 601 |
4/4✓ Branch 0 taken 119 times.
✓ Branch 1 taken 149 times.
✓ Branch 3 taken 25 times.
✓ Branch 4 taken 94 times.
|
268 | if (role == IMAGE_ROLE_ARGB && get_bits1(&s->gb)) { |
| 602 | 25 | ret = decode_entropy_image(s); | |
| 603 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | if (ret < 0) |
| 604 | ✗ | return ret; | |
| 605 | 25 | img->nb_huffman_groups = s->nb_huffman_groups; | |
| 606 | } | ||
| 607 | 268 | img->huffman_groups = av_calloc(img->nb_huffman_groups, | |
| 608 | HUFFMAN_CODES_PER_META_CODE * | ||
| 609 | sizeof(*img->huffman_groups)); | ||
| 610 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 268 times.
|
268 | if (!img->huffman_groups) |
| 611 | ✗ | return AVERROR(ENOMEM); | |
| 612 | |||
| 613 |
2/2✓ Branch 0 taken 298 times.
✓ Branch 1 taken 268 times.
|
566 | for (i = 0; i < img->nb_huffman_groups; i++) { |
| 614 | 298 | hg = &img->huffman_groups[i * HUFFMAN_CODES_PER_META_CODE]; | |
| 615 |
2/2✓ Branch 0 taken 1490 times.
✓ Branch 1 taken 298 times.
|
1788 | for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; j++) { |
| 616 | 1490 | int alphabet_size = alphabet_sizes[j]; | |
| 617 |
4/4✓ Branch 0 taken 298 times.
✓ Branch 1 taken 1192 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 289 times.
|
1490 | if (!j && img->color_cache_bits > 0) |
| 618 | 9 | alphabet_size += 1 << img->color_cache_bits; | |
| 619 | |||
| 620 |
2/2✓ Branch 1 taken 937 times.
✓ Branch 2 taken 553 times.
|
1490 | if (get_bits1(&s->gb)) { |
| 621 | 937 | read_huffman_code_simple(s, &hg[j]); | |
| 622 | } else { | ||
| 623 | 553 | ret = read_huffman_code_normal(s, &hg[j], alphabet_size); | |
| 624 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 553 times.
|
553 | if (ret < 0) |
| 625 | ✗ | return ret; | |
| 626 | } | ||
| 627 | } | ||
| 628 | } | ||
| 629 | |||
| 630 | 268 | width = img->frame->width; | |
| 631 |
2/2✓ Branch 0 taken 119 times.
✓ Branch 1 taken 149 times.
|
268 | if (role == IMAGE_ROLE_ARGB) |
| 632 | 119 | width = s->reduced_width; | |
| 633 | |||
| 634 | 268 | x = 0; y = 0; | |
| 635 |
2/2✓ Branch 0 taken 239514 times.
✓ Branch 1 taken 268 times.
|
239782 | while (y < img->frame->height) { |
| 636 | int v; | ||
| 637 | |||
| 638 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 239514 times.
|
239514 | if (get_bits_left(&s->gb) < 0) |
| 639 | ✗ | return AVERROR_INVALIDDATA; | |
| 640 | |||
| 641 | 239514 | hg = get_huffman_group(s, img, x, y); | |
| 642 | 239514 | v = huff_reader_get_symbol(&hg[HUFF_IDX_GREEN], &s->gb); | |
| 643 |
2/2✓ Branch 0 taken 207984 times.
✓ Branch 1 taken 31530 times.
|
239514 | if (v < NUM_LITERAL_CODES) { |
| 644 | /* literal pixel values */ | ||
| 645 | 207984 | uint8_t *p = GET_PIXEL(img->frame, x, y); | |
| 646 | 207984 | p[2] = v; | |
| 647 | 207984 | p[1] = huff_reader_get_symbol(&hg[HUFF_IDX_RED], &s->gb); | |
| 648 | 207984 | p[3] = huff_reader_get_symbol(&hg[HUFF_IDX_BLUE], &s->gb); | |
| 649 | 207984 | p[0] = huff_reader_get_symbol(&hg[HUFF_IDX_ALPHA], &s->gb); | |
| 650 |
2/2✓ Branch 0 taken 8244 times.
✓ Branch 1 taken 199740 times.
|
207984 | if (img->color_cache_bits) |
| 651 | 8244 | color_cache_put(img, AV_RB32(p)); | |
| 652 | 207984 | x++; | |
| 653 |
2/2✓ Branch 0 taken 1477 times.
✓ Branch 1 taken 206507 times.
|
207984 | if (x == width) { |
| 654 | 1477 | x = 0; | |
| 655 | 1477 | y++; | |
| 656 | } | ||
| 657 |
2/2✓ Branch 0 taken 27977 times.
✓ Branch 1 taken 3553 times.
|
31530 | } else if (v < NUM_LITERAL_CODES + NUM_LENGTH_CODES) { |
| 658 | /* LZ77 backwards mapping */ | ||
| 659 | int prefix_code, length, distance, ref_x, ref_y; | ||
| 660 | |||
| 661 | /* parse length and distance */ | ||
| 662 | 27977 | prefix_code = v - NUM_LITERAL_CODES; | |
| 663 |
2/2✓ Branch 0 taken 4044 times.
✓ Branch 1 taken 23933 times.
|
27977 | if (prefix_code < 4) { |
| 664 | 4044 | length = prefix_code + 1; | |
| 665 | } else { | ||
| 666 | 23933 | int extra_bits = (prefix_code - 2) >> 1; | |
| 667 | 23933 | int offset = 2 + (prefix_code & 1) << extra_bits; | |
| 668 | 23933 | length = offset + get_bits(&s->gb, extra_bits) + 1; | |
| 669 | } | ||
| 670 | 27977 | prefix_code = huff_reader_get_symbol(&hg[HUFF_IDX_DIST], &s->gb); | |
| 671 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27977 times.
|
27977 | if (prefix_code > 39U) { |
| 672 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
| 673 | "distance prefix code too large: %d\n", prefix_code); | ||
| 674 | ✗ | return AVERROR_INVALIDDATA; | |
| 675 | } | ||
| 676 |
2/2✓ Branch 0 taken 18110 times.
✓ Branch 1 taken 9867 times.
|
27977 | if (prefix_code < 4) { |
| 677 | 18110 | distance = prefix_code + 1; | |
| 678 | } else { | ||
| 679 | 9867 | int extra_bits = prefix_code - 2 >> 1; | |
| 680 | 9867 | int offset = 2 + (prefix_code & 1) << extra_bits; | |
| 681 | 9867 | distance = offset + get_bits(&s->gb, extra_bits) + 1; | |
| 682 | } | ||
| 683 | |||
| 684 | /* find reference location */ | ||
| 685 |
2/2✓ Branch 0 taken 22481 times.
✓ Branch 1 taken 5496 times.
|
27977 | if (distance <= NUM_SHORT_DISTANCES) { |
| 686 | 22481 | int xi = lz77_distance_offsets[distance - 1][0]; | |
| 687 | 22481 | int yi = lz77_distance_offsets[distance - 1][1]; | |
| 688 | 22481 | distance = FFMAX(1, xi + yi * width); | |
| 689 | } else { | ||
| 690 | 5496 | distance -= NUM_SHORT_DISTANCES; | |
| 691 | } | ||
| 692 | 27977 | ref_x = x; | |
| 693 | 27977 | ref_y = y; | |
| 694 |
2/2✓ Branch 0 taken 7319 times.
✓ Branch 1 taken 20658 times.
|
27977 | if (distance <= x) { |
| 695 | 7319 | ref_x -= distance; | |
| 696 | 7319 | distance = 0; | |
| 697 | } else { | ||
| 698 | 20658 | ref_x = 0; | |
| 699 | 20658 | distance -= x; | |
| 700 | } | ||
| 701 |
2/2✓ Branch 0 taken 70981 times.
✓ Branch 1 taken 27977 times.
|
98958 | while (distance >= width) { |
| 702 | 70981 | ref_y--; | |
| 703 | 70981 | distance -= width; | |
| 704 | } | ||
| 705 |
2/2✓ Branch 0 taken 20440 times.
✓ Branch 1 taken 7537 times.
|
27977 | if (distance > 0) { |
| 706 | 20440 | ref_x = width - distance; | |
| 707 | 20440 | ref_y--; | |
| 708 | } | ||
| 709 | 27977 | ref_x = FFMAX(0, ref_x); | |
| 710 | 27977 | ref_y = FFMAX(0, ref_y); | |
| 711 | |||
| 712 |
3/4✓ Branch 0 taken 7319 times.
✓ Branch 1 taken 20658 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7319 times.
|
27977 | if (ref_y == y && ref_x >= x) |
| 713 | ✗ | return AVERROR_INVALIDDATA; | |
| 714 | |||
| 715 | /* copy pixels | ||
| 716 | * source and dest regions can overlap and wrap lines, so just | ||
| 717 | * copy per-pixel */ | ||
| 718 |
2/2✓ Branch 0 taken 956688 times.
✓ Branch 1 taken 27877 times.
|
984565 | for (i = 0; i < length; i++) { |
| 719 | 956688 | uint8_t *p_ref = GET_PIXEL(img->frame, ref_x, ref_y); | |
| 720 | 956688 | uint8_t *p = GET_PIXEL(img->frame, x, y); | |
| 721 | |||
| 722 | 956688 | AV_COPY32(p, p_ref); | |
| 723 |
2/2✓ Branch 0 taken 201534 times.
✓ Branch 1 taken 755154 times.
|
956688 | if (img->color_cache_bits) |
| 724 | 201534 | color_cache_put(img, AV_RB32(p)); | |
| 725 | 956688 | x++; | |
| 726 | 956688 | ref_x++; | |
| 727 |
2/2✓ Branch 0 taken 8717 times.
✓ Branch 1 taken 947971 times.
|
956688 | if (x == width) { |
| 728 | 8717 | x = 0; | |
| 729 | 8717 | y++; | |
| 730 | } | ||
| 731 |
2/2✓ Branch 0 taken 8513 times.
✓ Branch 1 taken 948175 times.
|
956688 | if (ref_x == width) { |
| 732 | 8513 | ref_x = 0; | |
| 733 | 8513 | ref_y++; | |
| 734 | } | ||
| 735 |
3/4✓ Branch 0 taken 956588 times.
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 956588 times.
✗ Branch 3 not taken.
|
956688 | if (y == img->frame->height || ref_y == img->frame->height) |
| 736 | break; | ||
| 737 | } | ||
| 738 | } else { | ||
| 739 | /* read from color cache */ | ||
| 740 | 3553 | uint8_t *p = GET_PIXEL(img->frame, x, y); | |
| 741 | 3553 | int cache_idx = v - (NUM_LITERAL_CODES + NUM_LENGTH_CODES); | |
| 742 | |||
| 743 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3553 times.
|
3553 | if (!img->color_cache_bits) { |
| 744 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "color cache not found\n"); | |
| 745 | ✗ | return AVERROR_INVALIDDATA; | |
| 746 | } | ||
| 747 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3553 times.
|
3553 | if (cache_idx >= 1 << img->color_cache_bits) { |
| 748 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
| 749 | "color cache index out-of-bounds\n"); | ||
| 750 | ✗ | return AVERROR_INVALIDDATA; | |
| 751 | } | ||
| 752 | 3553 | AV_WB32(p, img->color_cache[cache_idx]); | |
| 753 | 3553 | x++; | |
| 754 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 3512 times.
|
3553 | if (x == width) { |
| 755 | 41 | x = 0; | |
| 756 | 41 | y++; | |
| 757 | } | ||
| 758 | } | ||
| 759 | } | ||
| 760 | |||
| 761 | 268 | return 0; | |
| 762 | } | ||
| 763 | |||
| 764 | /* PRED_MODE_BLACK */ | ||
| 765 | 6 | static void inv_predict_0(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 766 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 767 | { | ||
| 768 | 6 | AV_WB32(p, 0xFF000000); | |
| 769 | 6 | } | |
| 770 | |||
| 771 | /* PRED_MODE_L */ | ||
| 772 | 5300 | static void inv_predict_1(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 773 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 774 | { | ||
| 775 | 5300 | AV_COPY32(p, p_l); | |
| 776 | 5300 | } | |
| 777 | |||
| 778 | /* PRED_MODE_T */ | ||
| 779 | 21182 | static void inv_predict_2(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 780 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 781 | { | ||
| 782 | 21182 | AV_COPY32(p, p_t); | |
| 783 | 21182 | } | |
| 784 | |||
| 785 | /* PRED_MODE_TR */ | ||
| 786 | 6336 | static void inv_predict_3(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 787 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 788 | { | ||
| 789 | 6336 | AV_COPY32(p, p_tr); | |
| 790 | 6336 | } | |
| 791 | |||
| 792 | /* PRED_MODE_TL */ | ||
| 793 | 1248 | static void inv_predict_4(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 794 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 795 | { | ||
| 796 | 1248 | AV_COPY32(p, p_tl); | |
| 797 | 1248 | } | |
| 798 | |||
| 799 | /* PRED_MODE_AVG_T_AVG_L_TR */ | ||
| 800 | 4832 | static void inv_predict_5(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 801 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 802 | { | ||
| 803 | 4832 | p[0] = p_t[0] + (p_l[0] + p_tr[0] >> 1) >> 1; | |
| 804 | 4832 | p[1] = p_t[1] + (p_l[1] + p_tr[1] >> 1) >> 1; | |
| 805 | 4832 | p[2] = p_t[2] + (p_l[2] + p_tr[2] >> 1) >> 1; | |
| 806 | 4832 | p[3] = p_t[3] + (p_l[3] + p_tr[3] >> 1) >> 1; | |
| 807 | 4832 | } | |
| 808 | |||
| 809 | /* PRED_MODE_AVG_L_TL */ | ||
| 810 | 768 | static void inv_predict_6(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 811 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 812 | { | ||
| 813 | 768 | p[0] = p_l[0] + p_tl[0] >> 1; | |
| 814 | 768 | p[1] = p_l[1] + p_tl[1] >> 1; | |
| 815 | 768 | p[2] = p_l[2] + p_tl[2] >> 1; | |
| 816 | 768 | p[3] = p_l[3] + p_tl[3] >> 1; | |
| 817 | 768 | } | |
| 818 | |||
| 819 | /* PRED_MODE_AVG_L_T */ | ||
| 820 | 3072 | static void inv_predict_7(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 821 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 822 | { | ||
| 823 | 3072 | p[0] = p_l[0] + p_t[0] >> 1; | |
| 824 | 3072 | p[1] = p_l[1] + p_t[1] >> 1; | |
| 825 | 3072 | p[2] = p_l[2] + p_t[2] >> 1; | |
| 826 | 3072 | p[3] = p_l[3] + p_t[3] >> 1; | |
| 827 | 3072 | } | |
| 828 | |||
| 829 | /* PRED_MODE_AVG_TL_T */ | ||
| 830 | 4512 | static void inv_predict_8(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 831 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 832 | { | ||
| 833 | 4512 | p[0] = p_tl[0] + p_t[0] >> 1; | |
| 834 | 4512 | p[1] = p_tl[1] + p_t[1] >> 1; | |
| 835 | 4512 | p[2] = p_tl[2] + p_t[2] >> 1; | |
| 836 | 4512 | p[3] = p_tl[3] + p_t[3] >> 1; | |
| 837 | 4512 | } | |
| 838 | |||
| 839 | /* PRED_MODE_AVG_T_TR */ | ||
| 840 | 11488 | static void inv_predict_9(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 841 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 842 | { | ||
| 843 | 11488 | p[0] = p_t[0] + p_tr[0] >> 1; | |
| 844 | 11488 | p[1] = p_t[1] + p_tr[1] >> 1; | |
| 845 | 11488 | p[2] = p_t[2] + p_tr[2] >> 1; | |
| 846 | 11488 | p[3] = p_t[3] + p_tr[3] >> 1; | |
| 847 | 11488 | } | |
| 848 | |||
| 849 | /* PRED_MODE_AVG_AVG_L_TL_AVG_T_TR */ | ||
| 850 | 3236 | static void inv_predict_10(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 851 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 852 | { | ||
| 853 | 3236 | p[0] = (p_l[0] + p_tl[0] >> 1) + (p_t[0] + p_tr[0] >> 1) >> 1; | |
| 854 | 3236 | p[1] = (p_l[1] + p_tl[1] >> 1) + (p_t[1] + p_tr[1] >> 1) >> 1; | |
| 855 | 3236 | p[2] = (p_l[2] + p_tl[2] >> 1) + (p_t[2] + p_tr[2] >> 1) >> 1; | |
| 856 | 3236 | p[3] = (p_l[3] + p_tl[3] >> 1) + (p_t[3] + p_tr[3] >> 1) >> 1; | |
| 857 | 3236 | } | |
| 858 | |||
| 859 | /* PRED_MODE_SELECT */ | ||
| 860 | 3824 | static void inv_predict_11(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 861 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 862 | { | ||
| 863 | 3824 | int diff = (FFABS(p_l[0] - p_tl[0]) - FFABS(p_t[0] - p_tl[0])) + | |
| 864 | 3824 | (FFABS(p_l[1] - p_tl[1]) - FFABS(p_t[1] - p_tl[1])) + | |
| 865 | 3824 | (FFABS(p_l[2] - p_tl[2]) - FFABS(p_t[2] - p_tl[2])) + | |
| 866 | 3824 | (FFABS(p_l[3] - p_tl[3]) - FFABS(p_t[3] - p_tl[3])); | |
| 867 |
2/2✓ Branch 0 taken 2602 times.
✓ Branch 1 taken 1222 times.
|
3824 | if (diff <= 0) |
| 868 | 2602 | AV_COPY32(p, p_t); | |
| 869 | else | ||
| 870 | 1222 | AV_COPY32(p, p_l); | |
| 871 | 3824 | } | |
| 872 | |||
| 873 | /* PRED_MODE_ADD_SUBTRACT_FULL */ | ||
| 874 | ✗ | static void inv_predict_12(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 875 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 876 | { | ||
| 877 | ✗ | p[0] = av_clip_uint8(p_l[0] + p_t[0] - p_tl[0]); | |
| 878 | ✗ | p[1] = av_clip_uint8(p_l[1] + p_t[1] - p_tl[1]); | |
| 879 | ✗ | p[2] = av_clip_uint8(p_l[2] + p_t[2] - p_tl[2]); | |
| 880 | ✗ | p[3] = av_clip_uint8(p_l[3] + p_t[3] - p_tl[3]); | |
| 881 | ✗ | } | |
| 882 | |||
| 883 | 2048 | static av_always_inline uint8_t clamp_add_subtract_half(int a, int b, int c) | |
| 884 | { | ||
| 885 | 2048 | int d = a + b >> 1; | |
| 886 | 2048 | return av_clip_uint8(d + (d - c) / 2); | |
| 887 | } | ||
| 888 | |||
| 889 | /* PRED_MODE_ADD_SUBTRACT_HALF */ | ||
| 890 | 512 | static void inv_predict_13(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 891 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 892 | { | ||
| 893 | 512 | p[0] = clamp_add_subtract_half(p_l[0], p_t[0], p_tl[0]); | |
| 894 | 512 | p[1] = clamp_add_subtract_half(p_l[1], p_t[1], p_tl[1]); | |
| 895 | 512 | p[2] = clamp_add_subtract_half(p_l[2], p_t[2], p_tl[2]); | |
| 896 | 512 | p[3] = clamp_add_subtract_half(p_l[3], p_t[3], p_tl[3]); | |
| 897 | 512 | } | |
| 898 | |||
| 899 | typedef void (*inv_predict_func)(uint8_t *p, const uint8_t *p_l, | ||
| 900 | const uint8_t *p_tl, const uint8_t *p_t, | ||
| 901 | const uint8_t *p_tr); | ||
| 902 | |||
| 903 | static const inv_predict_func inverse_predict[14] = { | ||
| 904 | inv_predict_0, inv_predict_1, inv_predict_2, inv_predict_3, | ||
| 905 | inv_predict_4, inv_predict_5, inv_predict_6, inv_predict_7, | ||
| 906 | inv_predict_8, inv_predict_9, inv_predict_10, inv_predict_11, | ||
| 907 | inv_predict_12, inv_predict_13, | ||
| 908 | }; | ||
| 909 | |||
| 910 | 66316 | static void inverse_prediction(AVFrame *frame, enum PredictionMode m, int x, int y) | |
| 911 | { | ||
| 912 | uint8_t *dec, *p_l, *p_tl, *p_t, *p_tr; | ||
| 913 | uint8_t p[4]; | ||
| 914 | |||
| 915 | 66316 | dec = GET_PIXEL(frame, x, y); | |
| 916 | 66316 | p_l = GET_PIXEL(frame, x - 1, y); | |
| 917 | 66316 | p_tl = GET_PIXEL(frame, x - 1, y - 1); | |
| 918 | 66316 | p_t = GET_PIXEL(frame, x, y - 1); | |
| 919 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 65804 times.
|
66316 | if (x == frame->width - 1) |
| 920 | 512 | p_tr = GET_PIXEL(frame, 0, y); | |
| 921 | else | ||
| 922 | 65804 | p_tr = GET_PIXEL(frame, x + 1, y - 1); | |
| 923 | |||
| 924 | 66316 | inverse_predict[m](p, p_l, p_tl, p_t, p_tr); | |
| 925 | |||
| 926 | 66316 | dec[0] += p[0]; | |
| 927 | 66316 | dec[1] += p[1]; | |
| 928 | 66316 | dec[2] += p[2]; | |
| 929 | 66316 | dec[3] += p[3]; | |
| 930 | 66316 | } | |
| 931 | |||
| 932 | 6 | static int apply_predictor_transform(WebPContext *s) | |
| 933 | { | ||
| 934 | 6 | ImageContext *img = &s->image[IMAGE_ROLE_ARGB]; | |
| 935 | 6 | ImageContext *pimg = &s->image[IMAGE_ROLE_PREDICTOR]; | |
| 936 | int x, y; | ||
| 937 | |||
| 938 |
2/2✓ Branch 0 taken 572 times.
✓ Branch 1 taken 6 times.
|
578 | for (y = 0; y < img->frame->height; y++) { |
| 939 |
2/2✓ Branch 0 taken 66316 times.
✓ Branch 1 taken 572 times.
|
66888 | for (x = 0; x < s->reduced_width; x++) { |
| 940 | 66316 | int tx = x >> pimg->size_reduction; | |
| 941 | 66316 | int ty = y >> pimg->size_reduction; | |
| 942 | 66316 | enum PredictionMode m = GET_PIXEL_COMP(pimg->frame, tx, ty, 2); | |
| 943 | |||
| 944 |
2/2✓ Branch 0 taken 572 times.
✓ Branch 1 taken 65744 times.
|
66316 | if (x == 0) { |
| 945 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 566 times.
|
572 | if (y == 0) |
| 946 | 6 | m = PRED_MODE_BLACK; | |
| 947 | else | ||
| 948 | 566 | m = PRED_MODE_T; | |
| 949 |
2/2✓ Branch 0 taken 532 times.
✓ Branch 1 taken 65212 times.
|
65744 | } else if (y == 0) |
| 950 | 532 | m = PRED_MODE_L; | |
| 951 | |||
| 952 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66316 times.
|
66316 | if (m > 13) { |
| 953 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
| 954 | "invalid predictor mode: %d\n", m); | ||
| 955 | ✗ | return AVERROR_INVALIDDATA; | |
| 956 | } | ||
| 957 | 66316 | inverse_prediction(img->frame, m, x, y); | |
| 958 | } | ||
| 959 | } | ||
| 960 | 6 | return 0; | |
| 961 | } | ||
| 962 | |||
| 963 | 196608 | static av_always_inline uint8_t color_transform_delta(uint8_t color_pred, | |
| 964 | uint8_t color) | ||
| 965 | { | ||
| 966 | 196608 | return (int)ff_u8_to_s8(color_pred) * ff_u8_to_s8(color) >> 5; | |
| 967 | } | ||
| 968 | |||
| 969 | 4 | static int apply_color_transform(WebPContext *s) | |
| 970 | { | ||
| 971 | ImageContext *img, *cimg; | ||
| 972 | int x, y, cx, cy; | ||
| 973 | uint8_t *p, *cp; | ||
| 974 | |||
| 975 | 4 | img = &s->image[IMAGE_ROLE_ARGB]; | |
| 976 | 4 | cimg = &s->image[IMAGE_ROLE_COLOR_TRANSFORM]; | |
| 977 | |||
| 978 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 4 times.
|
516 | for (y = 0; y < img->frame->height; y++) { |
| 979 |
2/2✓ Branch 0 taken 65536 times.
✓ Branch 1 taken 512 times.
|
66048 | for (x = 0; x < s->reduced_width; x++) { |
| 980 | 65536 | cx = x >> cimg->size_reduction; | |
| 981 | 65536 | cy = y >> cimg->size_reduction; | |
| 982 | 65536 | cp = GET_PIXEL(cimg->frame, cx, cy); | |
| 983 | 65536 | p = GET_PIXEL(img->frame, x, y); | |
| 984 | |||
| 985 | 65536 | p[1] += color_transform_delta(cp[3], p[2]); | |
| 986 | 65536 | p[3] += color_transform_delta(cp[2], p[2]) + | |
| 987 | 65536 | color_transform_delta(cp[1], p[1]); | |
| 988 | } | ||
| 989 | } | ||
| 990 | 4 | return 0; | |
| 991 | } | ||
| 992 | |||
| 993 | 5 | static int apply_subtract_green_transform(WebPContext *s) | |
| 994 | { | ||
| 995 | int x, y; | ||
| 996 | 5 | ImageContext *img = &s->image[IMAGE_ROLE_ARGB]; | |
| 997 | |||
| 998 |
2/2✓ Branch 0 taken 859 times.
✓ Branch 1 taken 5 times.
|
864 | for (y = 0; y < img->frame->height; y++) { |
| 999 |
2/2✓ Branch 0 taken 206765 times.
✓ Branch 1 taken 859 times.
|
207624 | for (x = 0; x < s->reduced_width; x++) { |
| 1000 | 206765 | uint8_t *p = GET_PIXEL(img->frame, x, y); | |
| 1001 | 206765 | p[1] += p[2]; | |
| 1002 | 206765 | p[3] += p[2]; | |
| 1003 | } | ||
| 1004 | } | ||
| 1005 | 5 | return 0; | |
| 1006 | } | ||
| 1007 | |||
| 1008 | 114 | static int apply_color_indexing_transform(WebPContext *s) | |
| 1009 | { | ||
| 1010 | ImageContext *img; | ||
| 1011 | ImageContext *pal; | ||
| 1012 | int i, x, y; | ||
| 1013 | uint8_t *p; | ||
| 1014 | |||
| 1015 | 114 | img = &s->image[IMAGE_ROLE_ARGB]; | |
| 1016 | 114 | pal = &s->image[IMAGE_ROLE_COLOR_INDEXING]; | |
| 1017 | |||
| 1018 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 93 times.
|
114 | if (pal->size_reduction > 0) { // undo pixel packing |
| 1019 | GetBitContext gb_g; | ||
| 1020 | uint8_t *line; | ||
| 1021 | 21 | int pixel_bits = 8 >> pal->size_reduction; | |
| 1022 | |||
| 1023 | 21 | line = av_malloc(img->frame->linesize[0] + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 1024 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (!line) |
| 1025 | ✗ | return AVERROR(ENOMEM); | |
| 1026 | |||
| 1027 |
2/2✓ Branch 0 taken 1912 times.
✓ Branch 1 taken 21 times.
|
1933 | for (y = 0; y < img->frame->height; y++) { |
| 1028 | 1912 | p = GET_PIXEL(img->frame, 0, y); | |
| 1029 | 1912 | memcpy(line, p, img->frame->linesize[0]); | |
| 1030 | 1912 | init_get_bits(&gb_g, line, img->frame->linesize[0] * 8); | |
| 1031 | 1912 | skip_bits(&gb_g, 16); | |
| 1032 | 1912 | i = 0; | |
| 1033 |
2/2✓ Branch 0 taken 411466 times.
✓ Branch 1 taken 1912 times.
|
413378 | for (x = 0; x < img->frame->width; x++) { |
| 1034 | 411466 | p = GET_PIXEL(img->frame, x, y); | |
| 1035 | 411466 | p[2] = get_bits(&gb_g, pixel_bits); | |
| 1036 | 411466 | i++; | |
| 1037 |
2/2✓ Branch 0 taken 50969 times.
✓ Branch 1 taken 360497 times.
|
411466 | if (i == 1 << pal->size_reduction) { |
| 1038 | 50969 | skip_bits(&gb_g, 24); | |
| 1039 | 50969 | i = 0; | |
| 1040 | } | ||
| 1041 | } | ||
| 1042 | } | ||
| 1043 | 21 | av_free(line); | |
| 1044 | 21 | s->reduced_width = s->width; // we are back to full size | |
| 1045 | } | ||
| 1046 | |||
| 1047 | // switch to local palette if it's worth initializing it | ||
| 1048 |
2/2✓ Branch 0 taken 107 times.
✓ Branch 1 taken 7 times.
|
114 | if (img->frame->height * img->frame->width > 300) { |
| 1049 | uint8_t palette[256 * 4]; | ||
| 1050 | 107 | const int size = pal->frame->width * 4; | |
| 1051 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 107 times.
|
107 | av_assert0(size <= 1024U); |
| 1052 | 107 | memcpy(palette, GET_PIXEL(pal->frame, 0, 0), size); // copy palette | |
| 1053 | // set extra entries to transparent black | ||
| 1054 | 107 | memset(palette + size, 0, 256 * 4 - size); | |
| 1055 |
2/2✓ Branch 0 taken 8887 times.
✓ Branch 1 taken 107 times.
|
8994 | for (y = 0; y < img->frame->height; y++) { |
| 1056 |
2/2✓ Branch 0 taken 1306622 times.
✓ Branch 1 taken 8887 times.
|
1315509 | for (x = 0; x < img->frame->width; x++) { |
| 1057 | 1306622 | p = GET_PIXEL(img->frame, x, y); | |
| 1058 | 1306622 | i = p[2]; | |
| 1059 | 1306622 | AV_COPY32(p, &palette[i * 4]); | |
| 1060 | } | ||
| 1061 | } | ||
| 1062 | } else { | ||
| 1063 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 7 times.
|
57 | for (y = 0; y < img->frame->height; y++) { |
| 1064 |
2/2✓ Branch 0 taken 578 times.
✓ Branch 1 taken 50 times.
|
628 | for (x = 0; x < img->frame->width; x++) { |
| 1065 | 578 | p = GET_PIXEL(img->frame, x, y); | |
| 1066 | 578 | i = p[2]; | |
| 1067 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 578 times.
|
578 | if (i >= pal->frame->width) { |
| 1068 | ✗ | AV_WB32(p, 0x00000000); | |
| 1069 | } else { | ||
| 1070 | 578 | const uint8_t *pi = GET_PIXEL(pal->frame, i, 0); | |
| 1071 | 578 | AV_COPY32(p, pi); | |
| 1072 | } | ||
| 1073 | } | ||
| 1074 | } | ||
| 1075 | } | ||
| 1076 | |||
| 1077 | 114 | return 0; | |
| 1078 | } | ||
| 1079 | |||
| 1080 | 152 | static void update_canvas_size(AVCodecContext *avctx, int w, int h) | |
| 1081 | { | ||
| 1082 | 152 | WebPContext *s = avctx->priv_data; | |
| 1083 |
3/4✓ Branch 0 taken 140 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 140 times.
|
152 | if (s->width && s->width != w) { |
| 1084 | ✗ | av_log(avctx, AV_LOG_WARNING, "Width mismatch. %d != %d\n", | |
| 1085 | s->width, w); | ||
| 1086 | } | ||
| 1087 | 152 | s->width = w; | |
| 1088 |
3/4✓ Branch 0 taken 140 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 140 times.
|
152 | if (s->height && s->height != h) { |
| 1089 | ✗ | av_log(avctx, AV_LOG_WARNING, "Height mismatch. %d != %d\n", | |
| 1090 | s->height, h); | ||
| 1091 | } | ||
| 1092 | 152 | s->height = h; | |
| 1093 | 152 | } | |
| 1094 | |||
| 1095 | 119 | static int vp8_lossless_decode_frame(AVCodecContext *avctx, AVFrame *p, | |
| 1096 | int *got_frame, const uint8_t *data_start, | ||
| 1097 | unsigned int data_size, int is_alpha_chunk) | ||
| 1098 | { | ||
| 1099 | 119 | WebPContext *s = avctx->priv_data; | |
| 1100 | int w, h, ret, i, used; | ||
| 1101 | |||
| 1102 |
2/2✓ Branch 0 taken 103 times.
✓ Branch 1 taken 16 times.
|
119 | if (!is_alpha_chunk) |
| 1103 | 103 | avctx->pix_fmt = AV_PIX_FMT_ARGB; | |
| 1104 | |||
| 1105 | 119 | ret = init_get_bits8(&s->gb, data_start, data_size); | |
| 1106 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 119 times.
|
119 | if (ret < 0) |
| 1107 | ✗ | return ret; | |
| 1108 | |||
| 1109 |
2/2✓ Branch 0 taken 103 times.
✓ Branch 1 taken 16 times.
|
119 | if (!is_alpha_chunk) { |
| 1110 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 103 times.
|
103 | if (get_bits(&s->gb, 8) != 0x2F) { |
| 1111 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid WebP Lossless signature\n"); | |
| 1112 | ✗ | return AVERROR_INVALIDDATA; | |
| 1113 | } | ||
| 1114 | |||
| 1115 | 103 | w = get_bits(&s->gb, 14) + 1; | |
| 1116 | 103 | h = get_bits(&s->gb, 14) + 1; | |
| 1117 | |||
| 1118 | 103 | update_canvas_size(avctx, w, h); | |
| 1119 | |||
| 1120 | 103 | ret = ff_set_dimensions(avctx, s->width, s->height); | |
| 1121 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 103 times.
|
103 | if (ret < 0) |
| 1122 | ✗ | return ret; | |
| 1123 | |||
| 1124 | 103 | s->has_alpha = get_bits1(&s->gb); | |
| 1125 | |||
| 1126 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 103 times.
|
103 | if (get_bits(&s->gb, 3) != 0x0) { |
| 1127 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid WebP Lossless version\n"); | |
| 1128 | ✗ | return AVERROR_INVALIDDATA; | |
| 1129 | } | ||
| 1130 | } else { | ||
| 1131 |
2/4✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
|
16 | if (!s->width || !s->height) |
| 1132 | ✗ | return AVERROR_BUG; | |
| 1133 | 16 | w = s->width; | |
| 1134 | 16 | h = s->height; | |
| 1135 | } | ||
| 1136 | |||
| 1137 | /* parse transformations */ | ||
| 1138 | 119 | s->nb_transforms = 0; | |
| 1139 | 119 | s->reduced_width = s->width; | |
| 1140 | 119 | used = 0; | |
| 1141 |
2/2✓ Branch 1 taken 129 times.
✓ Branch 2 taken 119 times.
|
248 | while (get_bits1(&s->gb)) { |
| 1142 | 129 | enum TransformType transform = get_bits(&s->gb, 2); | |
| 1143 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 129 times.
|
129 | if (used & (1 << transform)) { |
| 1144 | ✗ | av_log(avctx, AV_LOG_ERROR, "Transform %d used more than once\n", | |
| 1145 | transform); | ||
| 1146 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 1147 | ✗ | goto free_and_return; | |
| 1148 | } | ||
| 1149 | 129 | used |= (1 << transform); | |
| 1150 | 129 | s->transforms[s->nb_transforms++] = transform; | |
| 1151 |
4/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 114 times.
✓ Branch 3 taken 5 times.
|
129 | switch (transform) { |
| 1152 | 6 | case PREDICTOR_TRANSFORM: | |
| 1153 | 6 | ret = parse_transform_predictor(s); | |
| 1154 | 6 | break; | |
| 1155 | 4 | case COLOR_TRANSFORM: | |
| 1156 | 4 | ret = parse_transform_color(s); | |
| 1157 | 4 | break; | |
| 1158 | 114 | case COLOR_INDEXING_TRANSFORM: | |
| 1159 | 114 | ret = parse_transform_color_indexing(s); | |
| 1160 | 114 | break; | |
| 1161 | } | ||
| 1162 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 129 times.
|
129 | if (ret < 0) |
| 1163 | ✗ | goto free_and_return; | |
| 1164 | } | ||
| 1165 | |||
| 1166 | /* decode primary image */ | ||
| 1167 | 119 | s->image[IMAGE_ROLE_ARGB].frame = p; | |
| 1168 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 103 times.
|
119 | if (is_alpha_chunk) |
| 1169 | 16 | s->image[IMAGE_ROLE_ARGB].is_alpha_primary = 1; | |
| 1170 | 119 | ret = decode_entropy_coded_image(s, IMAGE_ROLE_ARGB, w, h); | |
| 1171 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 119 times.
|
119 | if (ret < 0) |
| 1172 | ✗ | goto free_and_return; | |
| 1173 | |||
| 1174 | /* apply transformations */ | ||
| 1175 |
2/2✓ Branch 0 taken 129 times.
✓ Branch 1 taken 119 times.
|
248 | for (i = s->nb_transforms - 1; i >= 0; i--) { |
| 1176 |
4/5✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 114 times.
✗ Branch 4 not taken.
|
129 | switch (s->transforms[i]) { |
| 1177 | 6 | case PREDICTOR_TRANSFORM: | |
| 1178 | 6 | ret = apply_predictor_transform(s); | |
| 1179 | 6 | break; | |
| 1180 | 4 | case COLOR_TRANSFORM: | |
| 1181 | 4 | ret = apply_color_transform(s); | |
| 1182 | 4 | break; | |
| 1183 | 5 | case SUBTRACT_GREEN: | |
| 1184 | 5 | ret = apply_subtract_green_transform(s); | |
| 1185 | 5 | break; | |
| 1186 | 114 | case COLOR_INDEXING_TRANSFORM: | |
| 1187 | 114 | ret = apply_color_indexing_transform(s); | |
| 1188 | 114 | break; | |
| 1189 | } | ||
| 1190 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 129 times.
|
129 | if (ret < 0) |
| 1191 | ✗ | goto free_and_return; | |
| 1192 | } | ||
| 1193 | |||
| 1194 | 119 | *got_frame = 1; | |
| 1195 | 119 | p->pict_type = AV_PICTURE_TYPE_I; | |
| 1196 | 119 | p->flags |= AV_FRAME_FLAG_KEY; | |
| 1197 | 119 | p->flags |= AV_FRAME_FLAG_LOSSLESS; | |
| 1198 | 119 | ret = data_size; | |
| 1199 | |||
| 1200 | 119 | free_and_return: | |
| 1201 |
2/2✓ Branch 0 taken 595 times.
✓ Branch 1 taken 119 times.
|
714 | for (i = 0; i < IMAGE_ROLE_NB; i++) |
| 1202 | 595 | image_ctx_free(&s->image[i]); | |
| 1203 | |||
| 1204 | 119 | return ret; | |
| 1205 | } | ||
| 1206 | |||
| 1207 | ✗ | static void alpha_inverse_prediction(AVFrame *frame, enum AlphaFilter m) | |
| 1208 | { | ||
| 1209 | int x, y, ls; | ||
| 1210 | uint8_t *dec; | ||
| 1211 | |||
| 1212 | ✗ | ls = frame->linesize[3]; | |
| 1213 | |||
| 1214 | /* filter first row using horizontal filter */ | ||
| 1215 | ✗ | dec = frame->data[3] + 1; | |
| 1216 | ✗ | for (x = 1; x < frame->width; x++, dec++) | |
| 1217 | ✗ | *dec += *(dec - 1); | |
| 1218 | |||
| 1219 | /* filter first column using vertical filter */ | ||
| 1220 | ✗ | dec = frame->data[3] + ls; | |
| 1221 | ✗ | for (y = 1; y < frame->height; y++, dec += ls) | |
| 1222 | ✗ | *dec += *(dec - ls); | |
| 1223 | |||
| 1224 | /* filter the rest using the specified filter */ | ||
| 1225 | ✗ | switch (m) { | |
| 1226 | ✗ | case ALPHA_FILTER_HORIZONTAL: | |
| 1227 | ✗ | for (y = 1; y < frame->height; y++) { | |
| 1228 | ✗ | dec = frame->data[3] + y * ls + 1; | |
| 1229 | ✗ | for (x = 1; x < frame->width; x++, dec++) | |
| 1230 | ✗ | *dec += *(dec - 1); | |
| 1231 | } | ||
| 1232 | ✗ | break; | |
| 1233 | ✗ | case ALPHA_FILTER_VERTICAL: | |
| 1234 | ✗ | for (y = 1; y < frame->height; y++) { | |
| 1235 | ✗ | dec = frame->data[3] + y * ls + 1; | |
| 1236 | ✗ | for (x = 1; x < frame->width; x++, dec++) | |
| 1237 | ✗ | *dec += *(dec - ls); | |
| 1238 | } | ||
| 1239 | ✗ | break; | |
| 1240 | ✗ | case ALPHA_FILTER_GRADIENT: | |
| 1241 | ✗ | for (y = 1; y < frame->height; y++) { | |
| 1242 | ✗ | dec = frame->data[3] + y * ls + 1; | |
| 1243 | ✗ | for (x = 1; x < frame->width; x++, dec++) | |
| 1244 | ✗ | dec[0] += av_clip_uint8(*(dec - 1) + *(dec - ls) - *(dec - ls - 1)); | |
| 1245 | } | ||
| 1246 | ✗ | break; | |
| 1247 | } | ||
| 1248 | ✗ | } | |
| 1249 | |||
| 1250 | 16 | static int vp8_lossy_decode_alpha(AVCodecContext *avctx, AVFrame *p, | |
| 1251 | const uint8_t *data_start, | ||
| 1252 | unsigned int data_size) | ||
| 1253 | { | ||
| 1254 | 16 | WebPContext *s = avctx->priv_data; | |
| 1255 | int x, y, ret; | ||
| 1256 | |||
| 1257 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (s->alpha_compression == ALPHA_COMPRESSION_NONE) { |
| 1258 | GetByteContext gb; | ||
| 1259 | |||
| 1260 | ✗ | bytestream2_init(&gb, data_start, data_size); | |
| 1261 | ✗ | for (y = 0; y < s->height; y++) | |
| 1262 | ✗ | bytestream2_get_buffer(&gb, p->data[3] + p->linesize[3] * y, | |
| 1263 | ✗ | s->width); | |
| 1264 |
1/2✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 | } else if (s->alpha_compression == ALPHA_COMPRESSION_VP8L) { |
| 1265 | uint8_t *ap, *pp; | ||
| 1266 | 16 | int alpha_got_frame = 0; | |
| 1267 | |||
| 1268 | 16 | s->alpha_frame = av_frame_alloc(); | |
| 1269 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (!s->alpha_frame) |
| 1270 | ✗ | return AVERROR(ENOMEM); | |
| 1271 | |||
| 1272 | 16 | ret = vp8_lossless_decode_frame(avctx, s->alpha_frame, &alpha_got_frame, | |
| 1273 | data_start, data_size, 1); | ||
| 1274 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (ret < 0) { |
| 1275 | ✗ | av_frame_free(&s->alpha_frame); | |
| 1276 | ✗ | return ret; | |
| 1277 | } | ||
| 1278 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (!alpha_got_frame) { |
| 1279 | ✗ | av_frame_free(&s->alpha_frame); | |
| 1280 | ✗ | return AVERROR_INVALIDDATA; | |
| 1281 | } | ||
| 1282 | |||
| 1283 | /* copy green component of alpha image to alpha plane of primary image */ | ||
| 1284 |
2/2✓ Branch 0 taken 1727 times.
✓ Branch 1 taken 16 times.
|
1743 | for (y = 0; y < s->height; y++) { |
| 1285 | 1727 | ap = GET_PIXEL(s->alpha_frame, 0, y) + 2; | |
| 1286 | 1727 | pp = p->data[3] + p->linesize[3] * y; | |
| 1287 |
2/2✓ Branch 0 taken 397722 times.
✓ Branch 1 taken 1727 times.
|
399449 | for (x = 0; x < s->width; x++) { |
| 1288 | 397722 | *pp = *ap; | |
| 1289 | 397722 | pp++; | |
| 1290 | 397722 | ap += 4; | |
| 1291 | } | ||
| 1292 | } | ||
| 1293 | 16 | av_frame_free(&s->alpha_frame); | |
| 1294 | } | ||
| 1295 | |||
| 1296 | /* apply alpha filtering */ | ||
| 1297 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (s->alpha_filter) |
| 1298 | ✗ | alpha_inverse_prediction(p, s->alpha_filter); | |
| 1299 | |||
| 1300 | 16 | return 0; | |
| 1301 | } | ||
| 1302 | |||
| 1303 | 49 | static int vp8_lossy_decode_frame(AVCodecContext *avctx, AVFrame *p, | |
| 1304 | int *got_frame, uint8_t *data_start, | ||
| 1305 | unsigned int data_size) | ||
| 1306 | { | ||
| 1307 | 49 | WebPContext *s = avctx->priv_data; | |
| 1308 | int ret; | ||
| 1309 | |||
| 1310 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 41 times.
|
49 | if (!s->initialized) { |
| 1311 | 8 | ff_vp8_decode_init(avctx); | |
| 1312 | 8 | s->initialized = 1; | |
| 1313 | 8 | s->v.actually_webp = 1; | |
| 1314 | } | ||
| 1315 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 33 times.
|
49 | avctx->pix_fmt = s->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P; |
| 1316 | |||
| 1317 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
|
49 | if (data_size > INT_MAX) { |
| 1318 | ✗ | av_log(avctx, AV_LOG_ERROR, "unsupported chunk size\n"); | |
| 1319 | ✗ | return AVERROR_PATCHWELCOME; | |
| 1320 | } | ||
| 1321 | |||
| 1322 | 49 | av_packet_unref(s->pkt); | |
| 1323 | 49 | s->pkt->data = data_start; | |
| 1324 | 49 | s->pkt->size = data_size; | |
| 1325 | |||
| 1326 | 49 | ret = ff_vp8_decode_frame(avctx, p, got_frame, s->pkt); | |
| 1327 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
|
49 | if (ret < 0) |
| 1328 | ✗ | return ret; | |
| 1329 | |||
| 1330 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
|
49 | if (!*got_frame) |
| 1331 | ✗ | return AVERROR_INVALIDDATA; | |
| 1332 | |||
| 1333 | 49 | update_canvas_size(avctx, avctx->width, avctx->height); | |
| 1334 | |||
| 1335 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 33 times.
|
49 | if (s->has_alpha) { |
| 1336 | 16 | ret = vp8_lossy_decode_alpha(avctx, p, s->alpha_data, | |
| 1337 | 16 | s->alpha_data_size); | |
| 1338 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (ret < 0) |
| 1339 | ✗ | return ret; | |
| 1340 | } | ||
| 1341 | 49 | return ret; | |
| 1342 | } | ||
| 1343 | |||
| 1344 | 16 | static int webp_decode_frame(AVCodecContext *avctx, AVFrame *p, | |
| 1345 | int *got_frame, AVPacket *avpkt) | ||
| 1346 | { | ||
| 1347 | 16 | WebPContext *s = avctx->priv_data; | |
| 1348 | GetByteContext gb; | ||
| 1349 | int ret; | ||
| 1350 | uint32_t chunk_type, chunk_size; | ||
| 1351 | 16 | int vp8x_flags = 0; | |
| 1352 | |||
| 1353 | 16 | s->avctx = avctx; | |
| 1354 | 16 | s->width = 0; | |
| 1355 | 16 | s->height = 0; | |
| 1356 | 16 | *got_frame = 0; | |
| 1357 | 16 | s->has_alpha = 0; | |
| 1358 | 16 | s->has_exif = 0; | |
| 1359 | 16 | s->has_iccp = 0; | |
| 1360 | 16 | s->has_xmp = 0; | |
| 1361 | 16 | bytestream2_init(&gb, avpkt->data, avpkt->size); | |
| 1362 | |||
| 1363 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
|
16 | if (bytestream2_get_bytes_left(&gb) < 12) |
| 1364 | ✗ | return AVERROR_INVALIDDATA; | |
| 1365 | |||
| 1366 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
|
16 | if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) { |
| 1367 | ✗ | av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n"); | |
| 1368 | ✗ | return AVERROR_INVALIDDATA; | |
| 1369 | } | ||
| 1370 | |||
| 1371 | 16 | chunk_size = bytestream2_get_le32(&gb); | |
| 1372 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
|
16 | if (bytestream2_get_bytes_left(&gb) < chunk_size) |
| 1373 | ✗ | return AVERROR_INVALIDDATA; | |
| 1374 | |||
| 1375 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
|
16 | if (bytestream2_get_le32(&gb) != MKTAG('W', 'E', 'B', 'P')) { |
| 1376 | ✗ | av_log(avctx, AV_LOG_ERROR, "missing WEBP tag\n"); | |
| 1377 | ✗ | return AVERROR_INVALIDDATA; | |
| 1378 | } | ||
| 1379 | |||
| 1380 |
2/2✓ Branch 1 taken 24 times.
✓ Branch 2 taken 16 times.
|
40 | while (bytestream2_get_bytes_left(&gb) > 8) { |
| 1381 | 24 | chunk_type = bytestream2_get_le32(&gb); | |
| 1382 | 24 | chunk_size = bytestream2_get_le32(&gb); | |
| 1383 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (chunk_size == UINT32_MAX) |
| 1384 | ✗ | return AVERROR_INVALIDDATA; | |
| 1385 | 24 | chunk_size += chunk_size & 1; | |
| 1386 | |||
| 1387 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
|
24 | if (bytestream2_get_bytes_left(&gb) < chunk_size) { |
| 1388 | /* we seem to be running out of data, but it could also be that the | ||
| 1389 | bitstream has trailing junk leading to bogus chunk_size. */ | ||
| 1390 | ✗ | break; | |
| 1391 | } | ||
| 1392 | |||
| 1393 |
5/9✓ Branch 0 taken 6 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
24 | switch (chunk_type) { |
| 1394 | 6 | case MKTAG('V', 'P', '8', ' '): | |
| 1395 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (!*got_frame) { |
| 1396 | 6 | ret = vp8_lossy_decode_frame(avctx, p, got_frame, | |
| 1397 | 6 | avpkt->data + bytestream2_tell(&gb), | |
| 1398 | chunk_size); | ||
| 1399 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (ret < 0) |
| 1400 | ✗ | return ret; | |
| 1401 | } | ||
| 1402 | 6 | bytestream2_skip(&gb, chunk_size); | |
| 1403 | 24 | break; | |
| 1404 | 10 | case MKTAG('V', 'P', '8', 'L'): | |
| 1405 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (!*got_frame) { |
| 1406 | 10 | ret = vp8_lossless_decode_frame(avctx, p, got_frame, | |
| 1407 | 10 | avpkt->data + bytestream2_tell(&gb), | |
| 1408 | chunk_size, 0); | ||
| 1409 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (ret < 0) |
| 1410 | ✗ | return ret; | |
| 1411 | #if FF_API_CODEC_PROPS | ||
| 1412 | FF_DISABLE_DEPRECATION_WARNINGS | ||
| 1413 | 10 | avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS; | |
| 1414 | FF_ENABLE_DEPRECATION_WARNINGS | ||
| 1415 | #endif | ||
| 1416 | } | ||
| 1417 | 10 | bytestream2_skip(&gb, chunk_size); | |
| 1418 | 10 | break; | |
| 1419 | 4 | case MKTAG('V', 'P', '8', 'X'): | |
| 1420 |
3/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
|
4 | if (s->width || s->height || *got_frame) { |
| 1421 | ✗ | av_log(avctx, AV_LOG_ERROR, "Canvas dimensions are already set\n"); | |
| 1422 | ✗ | return AVERROR_INVALIDDATA; | |
| 1423 | } | ||
| 1424 | 4 | vp8x_flags = bytestream2_get_byte(&gb); | |
| 1425 | 4 | bytestream2_skip(&gb, 3); | |
| 1426 | 4 | s->width = bytestream2_get_le24(&gb) + 1; | |
| 1427 | 4 | s->height = bytestream2_get_le24(&gb) + 1; | |
| 1428 | 4 | ret = av_image_check_size(s->width, s->height, 0, avctx); | |
| 1429 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
| 1430 | ✗ | return ret; | |
| 1431 | 4 | break; | |
| 1432 | 2 | case MKTAG('A', 'L', 'P', 'H'): { | |
| 1433 | int alpha_header, filter_m, compression; | ||
| 1434 | |||
| 1435 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!(vp8x_flags & VP8X_FLAG_ALPHA)) { |
| 1436 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
| 1437 | "ALPHA chunk present, but alpha bit not set in the " | ||
| 1438 | "VP8X header\n"); | ||
| 1439 | } | ||
| 1440 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (chunk_size == 0) { |
| 1441 | ✗ | av_log(avctx, AV_LOG_ERROR, "invalid ALPHA chunk size\n"); | |
| 1442 | ✗ | return AVERROR_INVALIDDATA; | |
| 1443 | } | ||
| 1444 | 2 | alpha_header = bytestream2_get_byte(&gb); | |
| 1445 | 2 | s->alpha_data = avpkt->data + bytestream2_tell(&gb); | |
| 1446 | 2 | s->alpha_data_size = chunk_size - 1; | |
| 1447 | 2 | bytestream2_skip(&gb, s->alpha_data_size); | |
| 1448 | |||
| 1449 | 2 | filter_m = (alpha_header >> 2) & 0x03; | |
| 1450 | 2 | compression = alpha_header & 0x03; | |
| 1451 | |||
| 1452 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (compression > ALPHA_COMPRESSION_VP8L) { |
| 1453 | ✗ | av_log(avctx, AV_LOG_VERBOSE, | |
| 1454 | "skipping unsupported ALPHA chunk\n"); | ||
| 1455 | } else { | ||
| 1456 | 2 | s->has_alpha = 1; | |
| 1457 | 2 | s->alpha_compression = compression; | |
| 1458 | 2 | s->alpha_filter = filter_m; | |
| 1459 | } | ||
| 1460 | |||
| 1461 | 2 | break; | |
| 1462 | } | ||
| 1463 | 2 | case MKTAG('E', 'X', 'I', 'F'): { | |
| 1464 | 2 | AVBufferRef *exif_buf = NULL; | |
| 1465 | |||
| 1466 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (s->has_exif) { |
| 1467 | ✗ | av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra EXIF chunk\n"); | |
| 1468 | ✗ | goto exif_end; | |
| 1469 | } | ||
| 1470 | |||
| 1471 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!(vp8x_flags & VP8X_FLAG_EXIF_METADATA)) |
| 1472 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
| 1473 | "EXIF chunk present, but Exif bit not set in the " | ||
| 1474 | "VP8X header\n"); | ||
| 1475 | |||
| 1476 | 2 | exif_buf = av_buffer_alloc(chunk_size); | |
| 1477 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!exif_buf) { |
| 1478 | ✗ | av_log(avctx, AV_LOG_WARNING, "unable to allocate EXIF buffer\n"); | |
| 1479 | ✗ | goto exif_end; | |
| 1480 | } | ||
| 1481 | 2 | s->has_exif = 1; | |
| 1482 | 2 | memcpy(exif_buf->data, gb.buffer, chunk_size); | |
| 1483 | |||
| 1484 | 2 | ret = ff_decode_exif_attach_buffer(avctx, p, &exif_buf, AV_EXIF_TIFF_HEADER); | |
| 1485 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (ret < 0) |
| 1486 | ✗ | av_log(avctx, AV_LOG_WARNING, "unable to attach EXIF buffer\n"); | |
| 1487 | |||
| 1488 | 2 | exif_end: | |
| 1489 | 2 | bytestream2_skip(&gb, chunk_size); | |
| 1490 | 2 | break; | |
| 1491 | } | ||
| 1492 | ✗ | case MKTAG('I', 'C', 'C', 'P'): { | |
| 1493 | AVFrameSideData *sd; | ||
| 1494 | |||
| 1495 | ✗ | if (s->has_iccp) { | |
| 1496 | ✗ | av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra ICCP chunk\n"); | |
| 1497 | ✗ | bytestream2_skip(&gb, chunk_size); | |
| 1498 | ✗ | break; | |
| 1499 | } | ||
| 1500 | ✗ | if (!(vp8x_flags & VP8X_FLAG_ICC)) | |
| 1501 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
| 1502 | "ICCP chunk present, but ICC Profile bit not set in the " | ||
| 1503 | "VP8X header\n"); | ||
| 1504 | |||
| 1505 | ✗ | s->has_iccp = 1; | |
| 1506 | |||
| 1507 | ✗ | ret = ff_frame_new_side_data(avctx, p, AV_FRAME_DATA_ICC_PROFILE, chunk_size, &sd); | |
| 1508 | ✗ | if (ret < 0) | |
| 1509 | ✗ | return ret; | |
| 1510 | |||
| 1511 | ✗ | if (sd) { | |
| 1512 | ✗ | bytestream2_get_buffer(&gb, sd->data, chunk_size); | |
| 1513 | } else { | ||
| 1514 | ✗ | bytestream2_skip(&gb, chunk_size); | |
| 1515 | } | ||
| 1516 | ✗ | break; | |
| 1517 | } | ||
| 1518 | ✗ | case MKTAG('A', 'N', 'I', 'M'): | |
| 1519 | case MKTAG('A', 'N', 'M', 'F'): | ||
| 1520 | ✗ | av_log(avctx, AV_LOG_WARNING, "skipping unsupported chunk: %s\n", | |
| 1521 | ✗ | av_fourcc2str(chunk_type)); | |
| 1522 | ✗ | bytestream2_skip(&gb, chunk_size); | |
| 1523 | ✗ | break; | |
| 1524 | ✗ | case MKTAG('X', 'M', 'P', ' '): { | |
| 1525 | ✗ | if (s->has_xmp) { | |
| 1526 | ✗ | av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra XMP chunk\n"); | |
| 1527 | ✗ | bytestream2_skip(&gb, chunk_size); | |
| 1528 | ✗ | break; | |
| 1529 | } | ||
| 1530 | ✗ | if (!(vp8x_flags & VP8X_FLAG_XMP_METADATA)) | |
| 1531 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
| 1532 | "XMP chunk present, but XMP bit not set in the " | ||
| 1533 | "VP8X header\n"); | ||
| 1534 | |||
| 1535 | ✗ | s->has_xmp = 1; | |
| 1536 | |||
| 1537 | // there are at least chunk_size bytes left to read | ||
| 1538 | ✗ | uint8_t *buffer = av_malloc(chunk_size + 1); | |
| 1539 | ✗ | if (!buffer) | |
| 1540 | ✗ | return AVERROR(ENOMEM); | |
| 1541 | |||
| 1542 | ✗ | bytestream2_get_buffer(&gb, buffer, chunk_size); | |
| 1543 | ✗ | buffer[chunk_size] = '\0'; | |
| 1544 | |||
| 1545 | ✗ | av_dict_set(&p->metadata, "xmp", buffer, AV_DICT_DONT_STRDUP_VAL); | |
| 1546 | ✗ | break; | |
| 1547 | } | ||
| 1548 | ✗ | default: | |
| 1549 | ✗ | av_log(avctx, AV_LOG_VERBOSE, "skipping unknown chunk: %s\n", | |
| 1550 | ✗ | av_fourcc2str(chunk_type)); | |
| 1551 | ✗ | bytestream2_skip(&gb, chunk_size); | |
| 1552 | ✗ | break; | |
| 1553 | } | ||
| 1554 | } | ||
| 1555 | |||
| 1556 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (!*got_frame) { |
| 1557 | ✗ | av_log(avctx, AV_LOG_ERROR, "image data not found\n"); | |
| 1558 | ✗ | return AVERROR_INVALIDDATA; | |
| 1559 | } | ||
| 1560 | |||
| 1561 | 16 | return avpkt->size; | |
| 1562 | } | ||
| 1563 | |||
| 1564 | 22 | static av_cold int webp_decode_init(AVCodecContext *avctx) | |
| 1565 | { | ||
| 1566 | 22 | WebPContext *s = avctx->priv_data; | |
| 1567 | |||
| 1568 | 22 | s->pkt = av_packet_alloc(); | |
| 1569 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (!s->pkt) |
| 1570 | ✗ | return AVERROR(ENOMEM); | |
| 1571 | |||
| 1572 | 22 | return 0; | |
| 1573 | } | ||
| 1574 | |||
| 1575 | 22 | static av_cold int webp_decode_close(AVCodecContext *avctx) | |
| 1576 | { | ||
| 1577 | 22 | WebPContext *s = avctx->priv_data; | |
| 1578 | |||
| 1579 | 22 | av_packet_free(&s->pkt); | |
| 1580 | |||
| 1581 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 14 times.
|
22 | if (s->initialized) |
| 1582 | 8 | return ff_vp8_decode_free(avctx); | |
| 1583 | |||
| 1584 | 14 | return 0; | |
| 1585 | } | ||
| 1586 | |||
| 1587 | const FFCodec ff_webp_decoder = { | ||
| 1588 | .p.name = "webp", | ||
| 1589 | CODEC_LONG_NAME("WebP image"), | ||
| 1590 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 1591 | .p.id = AV_CODEC_ID_WEBP, | ||
| 1592 | .priv_data_size = sizeof(WebPContext), | ||
| 1593 | .init = webp_decode_init, | ||
| 1594 | FF_CODEC_DECODE_CB(webp_decode_frame), | ||
| 1595 | .close = webp_decode_close, | ||
| 1596 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, | ||
| 1597 | .caps_internal = FF_CODEC_CAP_ICC_PROFILES | | ||
| 1598 | FF_CODEC_CAP_USES_PROGRESSFRAMES, | ||
| 1599 | }; | ||
| 1600 | |||
| 1601 | #if CONFIG_WEBP_ANIM_DECODER | ||
| 1602 | |||
| 1603 | #define ANMF_FLAG_DISPOSE (1 << 0) | ||
| 1604 | #define ANMF_FLAG_NO_BLEND (1 << 1) | ||
| 1605 | |||
| 1606 | typedef struct AnimatedWebPContext { | ||
| 1607 | WebPContext w; | ||
| 1608 | |||
| 1609 | AVFrame *canvas; /* AVFrame for canvas */ | ||
| 1610 | AVFrame *subframe; /* AVFrame for subframe */ | ||
| 1611 | int canvas_width; /* canvas width */ | ||
| 1612 | int canvas_height; /* canvas height */ | ||
| 1613 | int anmf_flags; /* frame flags from ANMF chunk */ | ||
| 1614 | int pos_x; /* frame position X */ | ||
| 1615 | int pos_y; /* frame position Y */ | ||
| 1616 | int duration; /* frame duration */ | ||
| 1617 | int prev_anmf_flags; /* previous frame flags from ANMF chunk */ | ||
| 1618 | int prev_width; /* previous frame width */ | ||
| 1619 | int prev_height; /* previous frame height */ | ||
| 1620 | int prev_pos_x; /* previous frame position X */ | ||
| 1621 | int prev_pos_y; /* previous frame position Y */ | ||
| 1622 | uint8_t background_argb[4]; /* background color in ARGB format */ | ||
| 1623 | uint8_t background_yuva[4]; /* background color in YUVA format */ | ||
| 1624 | } AnimatedWebPContext; | ||
| 1625 | |||
| 1626 | /* | ||
| 1627 | * Blend src (foreground) into dst (background), in ARGB format. | ||
| 1628 | * pos_x, pos_y is the position in dst. | ||
| 1629 | */ | ||
| 1630 | 92 | static void blend_alpha_argb(AVFrame *dst, AVFrame *src, int pos_x, int pos_y) | |
| 1631 | { | ||
| 1632 |
2/2✓ Branch 0 taken 7118 times.
✓ Branch 1 taken 92 times.
|
7210 | for (int y = 0; y < src->height; y++) { |
| 1633 | 7118 | const uint8_t *src_argb = src->data[0] + y * src->linesize[0]; | |
| 1634 | 7118 | uint8_t *dst_argb = dst->data[0] + (pos_y + y) * dst->linesize[0] + pos_x * sizeof(uint32_t); | |
| 1635 |
2/2✓ Branch 0 taken 903094 times.
✓ Branch 1 taken 7118 times.
|
910212 | for (int x = 0; x < src->width; x++) { |
| 1636 | 903094 | int src_alpha = src_argb[0]; | |
| 1637 | 903094 | int dst_alpha = dst_argb[0]; | |
| 1638 | |||
| 1639 |
2/2✓ Branch 0 taken 179241 times.
✓ Branch 1 taken 723853 times.
|
903094 | if (src_alpha == 255) { |
| 1640 | 179241 | memcpy(dst_argb, src_argb, sizeof(uint32_t)); | |
| 1641 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 723853 times.
|
723853 | } else if (src_alpha == 0) { |
| 1642 | // no-op | ||
| 1643 | } else { | ||
| 1644 | ✗ | int tmp_alpha = (dst_alpha * (256 - src_alpha)) >> 8; | |
| 1645 | ✗ | int blend_alpha = src_alpha + tmp_alpha; | |
| 1646 | ✗ | int scale = (1UL << 24) / blend_alpha; | |
| 1647 | |||
| 1648 | ✗ | dst_argb[0] = blend_alpha; | |
| 1649 | ✗ | dst_argb[1] = (((uint32_t) (src_argb[1] * src_alpha + dst_argb[1] * tmp_alpha)) * scale) >> 24; | |
| 1650 | ✗ | dst_argb[2] = (((uint32_t) (src_argb[2] * src_alpha + dst_argb[2] * tmp_alpha)) * scale) >> 24; | |
| 1651 | ✗ | dst_argb[3] = (((uint32_t) (src_argb[3] * src_alpha + dst_argb[3] * tmp_alpha)) * scale) >> 24; | |
| 1652 | } | ||
| 1653 | 903094 | src_argb += sizeof(uint32_t); | |
| 1654 | 903094 | dst_argb += sizeof(uint32_t); | |
| 1655 | } | ||
| 1656 | } | ||
| 1657 | 92 | } | |
| 1658 | |||
| 1659 | /* | ||
| 1660 | * Blend src (foreground) into dst (background), in YUVA format. | ||
| 1661 | * pos_x, pos_y is the position in dst. | ||
| 1662 | */ | ||
| 1663 | 1 | static void blend_alpha_yuva(AVFrame *dst, AVFrame *src, int pos_x, int pos_y) | |
| 1664 | { | ||
| 1665 | 1 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(src->format); | |
| 1666 | |||
| 1667 | 1 | int plane_y = desc->comp[0].plane; | |
| 1668 | 1 | int plane_u = desc->comp[1].plane; | |
| 1669 | 1 | int plane_v = desc->comp[2].plane; | |
| 1670 | 1 | int plane_a = desc->comp[3].plane; | |
| 1671 | |||
| 1672 | // blend U & V planes first, because the later step may modify alpha plane | ||
| 1673 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 1 times.
|
61 | for (int y = 0; y < AV_CEIL_RSHIFT(src->height, 1); y++) { |
| 1674 | 60 | int tile_h = FFMIN(src->height - y * 2, 2); | |
| 1675 | 60 | const uint8_t *src_u = src->data[plane_u] + y * src->linesize[plane_u]; | |
| 1676 | 60 | const uint8_t *src_v = src->data[plane_v] + y * src->linesize[plane_v]; | |
| 1677 | 60 | uint8_t *dst_u = dst->data[plane_u] + ((pos_y >> 1) + y) * dst->linesize[plane_u] + (pos_x >> 1); | |
| 1678 | 60 | uint8_t *dst_v = dst->data[plane_v] + ((pos_y >> 1) + y) * dst->linesize[plane_v] + (pos_x >> 1); | |
| 1679 |
2/2✓ Branch 0 taken 5880 times.
✓ Branch 1 taken 60 times.
|
5940 | for (int x = 0; x < AV_CEIL_RSHIFT(src->width, 1); x++) { |
| 1680 | 5880 | int tile_w = FFMIN(src->width - x * 2, 2); | |
| 1681 | // calculate the average alpha of the tile | ||
| 1682 | 5880 | int src_alpha = 0; | |
| 1683 | 5880 | int dst_alpha = 0; | |
| 1684 |
2/2✓ Branch 0 taken 11662 times.
✓ Branch 1 taken 5880 times.
|
17542 | for (int yy = 0; yy < tile_h; yy++) { |
| 1685 |
2/2✓ Branch 0 taken 23324 times.
✓ Branch 1 taken 11662 times.
|
34986 | for (int xx = 0; xx < tile_w; xx++) { |
| 1686 | 23324 | src_alpha += src->data[plane_a][(y * 2 + yy) * src->linesize[plane_a] + | |
| 1687 | 23324 | (x * 2 + xx)]; | |
| 1688 | 23324 | dst_alpha += dst->data[plane_a][(((pos_y >> 1) + y) * 2 + yy) * dst->linesize[plane_a] + | |
| 1689 | 23324 | (((pos_x >> 1) + x) * 2 + xx)]; | |
| 1690 | } | ||
| 1691 | } | ||
| 1692 | 5880 | int shift = (tile_h == 2) + (tile_w == 2); | |
| 1693 | 5880 | src_alpha = AV_CEIL_RSHIFT(src_alpha, shift); | |
| 1694 | 5880 | dst_alpha = AV_CEIL_RSHIFT(dst_alpha, shift); | |
| 1695 | |||
| 1696 |
2/2✓ Branch 0 taken 3592 times.
✓ Branch 1 taken 2288 times.
|
5880 | if (src_alpha == 255) { |
| 1697 | 3592 | *dst_u = *src_u; | |
| 1698 | 3592 | *dst_v = *src_v; | |
| 1699 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2288 times.
|
2288 | } else if (src_alpha == 0) { |
| 1700 | // no-op | ||
| 1701 | } else { | ||
| 1702 | ✗ | int tmp_alpha = (dst_alpha * (256 - src_alpha)) >> 8; | |
| 1703 | ✗ | int blend_alpha = src_alpha + tmp_alpha; | |
| 1704 | ✗ | int scale = (1UL << 24) / blend_alpha; | |
| 1705 | ✗ | *dst_u = (((uint32_t) (*src_u * src_alpha + *dst_u * tmp_alpha)) * scale) >> 24; | |
| 1706 | ✗ | *dst_v = (((uint32_t) (*src_v * src_alpha + *dst_v * tmp_alpha)) * scale) >> 24; | |
| 1707 | } | ||
| 1708 | 5880 | src_u += 1; | |
| 1709 | 5880 | src_v += 1; | |
| 1710 | 5880 | dst_u += 1; | |
| 1711 | 5880 | dst_v += 1; | |
| 1712 | } | ||
| 1713 | } | ||
| 1714 | |||
| 1715 | // blend Y & A planes | ||
| 1716 |
2/2✓ Branch 0 taken 119 times.
✓ Branch 1 taken 1 times.
|
120 | for (int y = 0; y < src->height; y++) { |
| 1717 | 119 | const uint8_t *src_y = src->data[plane_y] + y * src->linesize[plane_y]; | |
| 1718 | 119 | const uint8_t *src_a = src->data[plane_a] + y * src->linesize[plane_a]; | |
| 1719 | 119 | uint8_t *dst_y = dst->data[plane_y] + (pos_y + y) * dst->linesize[plane_y] + pos_x; | |
| 1720 | 119 | uint8_t *dst_a = dst->data[plane_a] + (pos_y + y) * dst->linesize[plane_a] + pos_x; | |
| 1721 |
2/2✓ Branch 0 taken 23324 times.
✓ Branch 1 taken 119 times.
|
23443 | for (int x = 0; x < src->width; x++) { |
| 1722 | 23324 | int src_alpha = *src_a; | |
| 1723 | 23324 | int dst_alpha = *dst_a; | |
| 1724 | |||
| 1725 |
2/2✓ Branch 0 taken 14172 times.
✓ Branch 1 taken 9152 times.
|
23324 | if (src_alpha == 255) { |
| 1726 | 14172 | *dst_y = *src_y; | |
| 1727 | 14172 | *dst_a = 255; | |
| 1728 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9152 times.
|
9152 | } else if (src_alpha == 0) { |
| 1729 | // no-op | ||
| 1730 | } else { | ||
| 1731 | ✗ | int tmp_alpha = (dst_alpha * (256 - src_alpha)) >> 8; | |
| 1732 | ✗ | int blend_alpha = src_alpha + tmp_alpha; | |
| 1733 | ✗ | int scale = (1UL << 24) / blend_alpha; | |
| 1734 | ✗ | *dst_y = (((uint32_t) (*src_y * src_alpha + *dst_y * tmp_alpha)) * scale) >> 24; | |
| 1735 | ✗ | *dst_a = blend_alpha; | |
| 1736 | } | ||
| 1737 | 23324 | src_y += 1; | |
| 1738 | 23324 | src_a += 1; | |
| 1739 | 23324 | dst_y += 1; | |
| 1740 | 23324 | dst_a += 1; | |
| 1741 | } | ||
| 1742 | } | ||
| 1743 | 1 | } | |
| 1744 | |||
| 1745 | 2203726 | static av_always_inline void webp_yuva2argb(uint8_t *out, int Y, int U, int V, int A) | |
| 1746 | { | ||
| 1747 | // variables used in macros | ||
| 1748 | 2203726 | const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP; | |
| 1749 | uint8_t r, g, b; | ||
| 1750 | int y, cb, cr; | ||
| 1751 | int r_add, g_add, b_add; | ||
| 1752 | |||
| 1753 | 2203726 | YUV_TO_RGB1_CCIR(U, V); | |
| 1754 | 2203726 | YUV_TO_RGB2_CCIR(r, g, b, Y); | |
| 1755 | |||
| 1756 | 2203726 | out[0] = av_clip_uint8(A); | |
| 1757 | 2203726 | out[1] = av_clip_uint8(r); | |
| 1758 | 2203726 | out[2] = av_clip_uint8(g); | |
| 1759 | 2203726 | out[3] = av_clip_uint8(b); | |
| 1760 | 2203726 | } | |
| 1761 | |||
| 1762 | 28 | static void copy_yuva2argb(AVFrame *dst, AVFrame *src, int pos_x, int pos_y) | |
| 1763 | { | ||
| 1764 | 28 | const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src->format); | |
| 1765 | |||
| 1766 | 28 | int alpha = src_desc->nb_components > 3; | |
| 1767 | 28 | int plane_y = src_desc->comp[0].plane; | |
| 1768 | 28 | int plane_u = src_desc->comp[1].plane; | |
| 1769 | 28 | int plane_v = src_desc->comp[2].plane; | |
| 1770 | 28 | int plane_a = src_desc->comp[3].plane; | |
| 1771 | |||
| 1772 |
2/2✓ Branch 0 taken 6192 times.
✓ Branch 1 taken 28 times.
|
6220 | for (int y = 0; y < src->height; y++) { |
| 1773 | 6192 | const uint8_t *src_y = src->data[plane_y] + y * src->linesize[plane_y]; | |
| 1774 | 6192 | const uint8_t *src_u = src->data[plane_u] + (y >> 1) * src->linesize[plane_u]; | |
| 1775 | 6192 | const uint8_t *src_v = src->data[plane_v] + (y >> 1) * src->linesize[plane_v]; | |
| 1776 | 6192 | const uint8_t *src_a = NULL; | |
| 1777 | 6192 | uint8_t *dst_argb = dst->data[0] + (pos_y + y) * dst->linesize[0] + pos_x * 4; | |
| 1778 |
2/2✓ Branch 0 taken 4800 times.
✓ Branch 1 taken 1392 times.
|
6192 | if (alpha) |
| 1779 | 4800 | src_a = src->data[plane_a] + y * src->linesize[plane_a]; | |
| 1780 | |||
| 1781 |
2/2✓ Branch 0 taken 1938576 times.
✓ Branch 1 taken 6192 times.
|
1944768 | for (int x = 0; x < src->width; x++) { |
| 1782 |
2/2✓ Branch 0 taken 1536000 times.
✓ Branch 1 taken 402576 times.
|
1938576 | webp_yuva2argb(dst_argb, *src_y, *src_u, *src_v, (alpha ? *src_a : 255)); |
| 1783 | 1938576 | src_y += 1; | |
| 1784 | 1938576 | src_u += x & 1; | |
| 1785 | 1938576 | src_v += x & 1; | |
| 1786 |
2/2✓ Branch 0 taken 1536000 times.
✓ Branch 1 taken 402576 times.
|
1938576 | if (alpha) |
| 1787 | 1536000 | src_a += 1; | |
| 1788 | 1938576 | dst_argb += sizeof(uint32_t); | |
| 1789 | } | ||
| 1790 | } | ||
| 1791 | 28 | } | |
| 1792 | |||
| 1793 | 13 | static void blend_yuva2argb(AVFrame *dst, AVFrame *src, int pos_x, int pos_y) | |
| 1794 | { | ||
| 1795 | 13 | const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src->format); | |
| 1796 | |||
| 1797 | 13 | int plane_y = src_desc->comp[0].plane; | |
| 1798 | 13 | int plane_u = src_desc->comp[1].plane; | |
| 1799 | 13 | int plane_v = src_desc->comp[2].plane; | |
| 1800 | 13 | int plane_a = src_desc->comp[3].plane; | |
| 1801 | |||
| 1802 |
2/2✓ Branch 0 taken 1592 times.
✓ Branch 1 taken 13 times.
|
1605 | for (int y = 0; y < src->height; y++) { |
| 1803 | 1592 | const uint8_t *src_y = src->data[plane_y] + y * src->linesize[plane_y]; | |
| 1804 | 1592 | const uint8_t *src_u = src->data[plane_u] + (y >> 1) * src->linesize[plane_u]; | |
| 1805 | 1592 | const uint8_t *src_v = src->data[plane_v] + (y >> 1) * src->linesize[plane_v]; | |
| 1806 | 1592 | const uint8_t *src_a = src->data[plane_a] + y * src->linesize[plane_a]; | |
| 1807 | 1592 | uint8_t *dst_argb = dst->data[0] + (pos_y + y) * dst->linesize[0] + pos_x * 4; | |
| 1808 | |||
| 1809 |
2/2✓ Branch 0 taken 374206 times.
✓ Branch 1 taken 1592 times.
|
375798 | for (int x = 0; x < src->width; x++) { |
| 1810 | 374206 | int src_alpha = *src_a; | |
| 1811 | 374206 | int dst_alpha = dst_argb[0]; | |
| 1812 | |||
| 1813 |
2/2✓ Branch 0 taken 265150 times.
✓ Branch 1 taken 109056 times.
|
374206 | if (src_alpha == 255) { |
| 1814 | 265150 | webp_yuva2argb(dst_argb, *src_y, *src_u, *src_v, src_alpha); | |
| 1815 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 109056 times.
|
109056 | } else if (src_alpha == 0) { |
| 1816 | // no-op | ||
| 1817 | } else { | ||
| 1818 | uint8_t tmp[4]; | ||
| 1819 | ✗ | int tmp_alpha = (dst_alpha * (256 - src_alpha)) >> 8; | |
| 1820 | ✗ | int blend_alpha = src_alpha + tmp_alpha; | |
| 1821 | ✗ | int scale = (1UL << 24) / blend_alpha; | |
| 1822 | |||
| 1823 | ✗ | webp_yuva2argb(tmp, *src_y, *src_u, *src_v, src_alpha); | |
| 1824 | |||
| 1825 | ✗ | dst_argb[0] = blend_alpha; | |
| 1826 | ✗ | dst_argb[1] = (((uint32_t) (tmp[1] * src_alpha + dst_argb[1] * tmp_alpha)) * scale) >> 24; | |
| 1827 | ✗ | dst_argb[2] = (((uint32_t) (tmp[2] * src_alpha + dst_argb[2] * tmp_alpha)) * scale) >> 24; | |
| 1828 | ✗ | dst_argb[3] = (((uint32_t) (tmp[3] * src_alpha + dst_argb[3] * tmp_alpha)) * scale) >> 24; | |
| 1829 | } | ||
| 1830 | |||
| 1831 | 374206 | src_y += 1; | |
| 1832 | 374206 | src_u += x & 1; | |
| 1833 | 374206 | src_v += x & 1; | |
| 1834 | 374206 | src_a += 1; | |
| 1835 | 374206 | dst_argb += sizeof(uint32_t); | |
| 1836 | } | ||
| 1837 | } | ||
| 1838 | 13 | } | |
| 1839 | |||
| 1840 | 136 | static int blend_subframe_into_canvas(AnimatedWebPContext *s) | |
| 1841 | { | ||
| 1842 | 136 | AVFrame *canvas = s->canvas; | |
| 1843 | 136 | AVFrame *frame = s->subframe; | |
| 1844 | |||
| 1845 |
2/2✓ Branch 0 taken 114 times.
✓ Branch 1 taken 22 times.
|
136 | if ((s->anmf_flags & ANMF_FLAG_NO_BLEND) |
| 1846 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 106 times.
|
114 | || frame->format == AV_PIX_FMT_YUV420P) { |
| 1847 | // do not blend, overwrite | ||
| 1848 | |||
| 1849 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 21 times.
|
30 | if (canvas->format == AV_PIX_FMT_ARGB) { |
| 1850 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
|
9 | if (canvas->format == frame->format) { |
| 1851 | 1 | const uint8_t *src = frame->data[0]; | |
| 1852 | 1 | uint8_t *dst = canvas->data[0] + | |
| 1853 | 1 | s->pos_y * canvas->linesize[0] + | |
| 1854 | 1 | s->pos_x * sizeof(uint32_t); | |
| 1855 |
2/2✓ Branch 0 taken 347 times.
✓ Branch 1 taken 1 times.
|
348 | for (int y = 0; y < s->w.height; y++) { |
| 1856 | 347 | memcpy(dst, src, s->w.width * sizeof(uint32_t)); | |
| 1857 | 347 | src += frame->linesize[0]; | |
| 1858 | 347 | dst += canvas->linesize[0]; | |
| 1859 | } | ||
| 1860 | } else { | ||
| 1861 | 8 | copy_yuva2argb(canvas, frame, s->pos_x, s->pos_y); | |
| 1862 | } | ||
| 1863 | } else /* if (canvas->format == AV_PIX_FMT_YUVA420P) */ { | ||
| 1864 | 21 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format); | |
| 1865 | |||
| 1866 |
2/2✓ Branch 0 taken 63 times.
✓ Branch 1 taken 21 times.
|
84 | for (int comp = 0; comp < desc->nb_components; comp++) { |
| 1867 | 63 | int plane = desc->comp[comp].plane; | |
| 1868 |
4/4✓ Branch 0 taken 42 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 21 times.
|
63 | int shift = (comp == 1 || comp == 2) ? 1 : 0; |
| 1869 | 63 | const uint8_t *src = frame->data[plane]; | |
| 1870 | 63 | uint8_t *dst = canvas->data[plane] + | |
| 1871 | 63 | (s->pos_y >> shift) * canvas->linesize[plane] + | |
| 1872 | 63 | (s->pos_x >> shift); | |
| 1873 |
2/2✓ Branch 0 taken 10080 times.
✓ Branch 1 taken 63 times.
|
10143 | for (int y = 0; y < AV_CEIL_RSHIFT(s->w.height, shift); y++) { |
| 1874 | 10080 | memcpy(dst, src, AV_CEIL_RSHIFT(s->w.width, shift)); | |
| 1875 | 10080 | src += frame->linesize[plane]; | |
| 1876 | 10080 | dst += canvas->linesize[plane]; | |
| 1877 | } | ||
| 1878 | } | ||
| 1879 | |||
| 1880 |
2/4✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
|
21 | if (canvas->format == AV_PIX_FMT_YUVA420P && desc->nb_components < 4) { |
| 1881 | // frame does not have alpha, set alpha to 255 | ||
| 1882 | 21 | const AVPixFmtDescriptor *canvas_desc = av_pix_fmt_desc_get(canvas->format); | |
| 1883 | 21 | int plane = canvas_desc->comp[3].plane; | |
| 1884 | 21 | uint8_t *dst = canvas->data[plane] + s->pos_y * canvas->linesize[plane] + s->pos_x; | |
| 1885 |
2/2✓ Branch 0 taken 5040 times.
✓ Branch 1 taken 21 times.
|
5061 | for (int y = 0; y < s->w.height; y++) { |
| 1886 | 5040 | memset(dst, 255, s->w.width); | |
| 1887 | 5040 | dst += canvas->linesize[plane]; | |
| 1888 | } | ||
| 1889 | } | ||
| 1890 | } | ||
| 1891 | } else { | ||
| 1892 | // alpha blending | ||
| 1893 | |||
| 1894 |
2/2✓ Branch 0 taken 105 times.
✓ Branch 1 taken 1 times.
|
106 | if (canvas->format == AV_PIX_FMT_ARGB) { |
| 1895 |
2/2✓ Branch 0 taken 92 times.
✓ Branch 1 taken 13 times.
|
105 | if (canvas->format == frame->format) { |
| 1896 | 92 | blend_alpha_argb(canvas, frame, s->pos_x, s->pos_y); | |
| 1897 | } else { | ||
| 1898 | 13 | blend_yuva2argb(canvas, frame, s->pos_x, s->pos_y); | |
| 1899 | } | ||
| 1900 | } else /* if (canvas->format == AV_PIX_FMT_YUVA420P) */ { | ||
| 1901 | 1 | blend_alpha_yuva(canvas, frame, s->pos_x, s->pos_y); | |
| 1902 | } | ||
| 1903 | } | ||
| 1904 | |||
| 1905 | 136 | return 0; | |
| 1906 | } | ||
| 1907 | |||
| 1908 | /** | ||
| 1909 | * Fill a rectangle on the canvas with the background color (transparent black | ||
| 1910 | * by default, or the color from the ANIM chunk if provided by the demuxer). | ||
| 1911 | */ | ||
| 1912 | 46 | static void fill_canvas_rect(AnimatedWebPContext *s, int pos_x, int pos_y, int width, int height) | |
| 1913 | { | ||
| 1914 | 46 | AVFrame *canvas = s->canvas; | |
| 1915 | |||
| 1916 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 21 times.
|
46 | if (canvas->format == AV_PIX_FMT_ARGB) { |
| 1917 | 25 | uint32_t bg_color = AV_RN32(s->background_argb); | |
| 1918 | 25 | int is_repeatable = (bg_color == ((bg_color & 0xff) * 0x01010101)); | |
| 1919 |
2/2✓ Branch 0 taken 3000 times.
✓ Branch 1 taken 25 times.
|
3025 | for (int y = 0; y < height; y++) { |
| 1920 | 3000 | uint32_t *dst = (uint32_t *) (canvas->data[0] + (pos_y + y) * canvas->linesize[0]) + pos_x; | |
| 1921 |
1/2✓ Branch 0 taken 3000 times.
✗ Branch 1 not taken.
|
3000 | if (is_repeatable) { |
| 1922 | 3000 | memset(dst, bg_color, width * sizeof(uint32_t)); | |
| 1923 | } else { | ||
| 1924 | ✗ | for (int x = 0; x < width; x++) | |
| 1925 | ✗ | dst[x] = bg_color; | |
| 1926 | } | ||
| 1927 | } | ||
| 1928 | } else /* if (canvas->format == AV_PIX_FMT_YUVA420P) */ { | ||
| 1929 | 21 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(canvas->format); | |
| 1930 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 21 times.
|
105 | for (int comp = 0; comp < desc->nb_components; comp++) { |
| 1931 |
4/4✓ Branch 0 taken 63 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 42 times.
|
84 | int shift = (comp == 1 || comp == 2) ? 1 : 0; |
| 1932 | 84 | int plane = desc->comp[comp].plane; | |
| 1933 | 84 | uint8_t *dst = canvas->data[plane] + (pos_y >> shift) * canvas->linesize[plane] + (pos_x >> shift); | |
| 1934 |
2/2✓ Branch 0 taken 15120 times.
✓ Branch 1 taken 84 times.
|
15204 | for (int y = 0; y < AV_CEIL_RSHIFT(height, shift); y++) { |
| 1935 | 15120 | memset(dst, s->background_yuva[plane], AV_CEIL_RSHIFT(width, shift)); | |
| 1936 | 15120 | dst += canvas->linesize[plane]; | |
| 1937 | } | ||
| 1938 | } | ||
| 1939 | } | ||
| 1940 | 46 | } | |
| 1941 | |||
| 1942 | 43 | static int allocate_canvas(AnimatedWebPContext *s, int format) | |
| 1943 | { | ||
| 1944 | 43 | s->w.avctx->pix_fmt = format; | |
| 1945 | 43 | int ret = ff_set_dimensions(s->w.avctx, s->canvas_width, s->canvas_height); | |
| 1946 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
|
43 | if (ret < 0) |
| 1947 | ✗ | return ret; | |
| 1948 | 43 | return ff_reget_buffer(s->w.avctx, s->canvas, 0); | |
| 1949 | } | ||
| 1950 | |||
| 1951 | 136 | static int prepare_canvas(AnimatedWebPContext *s, int key_frame, int format) | |
| 1952 | { | ||
| 1953 | int ret; | ||
| 1954 | |||
| 1955 | /** | ||
| 1956 | * Clear the canvas on keyframes and frames that overwrite the entire | ||
| 1957 | * canvas. | ||
| 1958 | */ | ||
| 1959 |
2/2✓ Branch 0 taken 133 times.
✓ Branch 1 taken 3 times.
|
136 | if (key_frame || |
| 1960 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 113 times.
|
133 | ((s->anmf_flags & ANMF_FLAG_NO_BLEND) && |
| 1961 |
2/4✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
|
20 | (s->pos_x == 0) && (s->pos_x + s->w.width == s->canvas_width) && |
| 1962 |
2/4✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
|
20 | (s->pos_y == 0) && (s->pos_y + s->w.height == s->canvas_height))) |
| 1963 | 23 | av_frame_unref(s->canvas); | |
| 1964 | |||
| 1965 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 113 times.
|
136 | if (!s->canvas->buf[0]) { |
| 1966 | /* Allocate new canvas frame */ | ||
| 1967 | 23 | ret = allocate_canvas(s, format); | |
| 1968 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (ret < 0) |
| 1969 | ✗ | return ret; | |
| 1970 | /* ... and initialize it. */ | ||
| 1971 | 23 | fill_canvas_rect(s, 0, 0, s->canvas->width, s->canvas->height); | |
| 1972 | } else { | ||
| 1973 |
4/4✓ Branch 0 taken 91 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 71 times.
|
133 | if (format == AV_PIX_FMT_ARGB && s->canvas->format == AV_PIX_FMT_YUVA420P) { |
| 1974 | /** | ||
| 1975 | * If we have a lossless frame following a lossy frame, we upgrade | ||
| 1976 | * the canvas to ARGB, but we don't convert the canvas back to YUVA | ||
| 1977 | * if there is a lossy frame following a lossless frame. | ||
| 1978 | */ | ||
| 1979 | 20 | AVFrame *yuva_canvas = av_frame_clone(s->canvas); | |
| 1980 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (!yuva_canvas) |
| 1981 | ✗ | return AVERROR(ENOMEM); | |
| 1982 | 20 | av_frame_unref(s->canvas); | |
| 1983 | 20 | ret = allocate_canvas(s, AV_PIX_FMT_ARGB); | |
| 1984 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (ret < 0) { |
| 1985 | ✗ | av_frame_free(&yuva_canvas); | |
| 1986 | ✗ | return ret; | |
| 1987 | } | ||
| 1988 | 20 | copy_yuva2argb(s->canvas, yuva_canvas, 0, 0); | |
| 1989 | 20 | av_frame_free(&yuva_canvas); | |
| 1990 | } else { | ||
| 1991 | /** | ||
| 1992 | * The decode frame function returns a reference to the canvas, | ||
| 1993 | * therefore we have to ensure it is writable before using it | ||
| 1994 | * for a new frame. | ||
| 1995 | */ | ||
| 1996 | 93 | ret = av_frame_make_writable(s->canvas); | |
| 1997 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
|
93 | if (ret < 0) |
| 1998 | ✗ | return ret; | |
| 1999 | } | ||
| 2000 | /* Dispose of previous frame if needed. */ | ||
| 2001 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 90 times.
|
113 | if (s->prev_anmf_flags & ANMF_FLAG_DISPOSE) |
| 2002 | 23 | fill_canvas_rect(s, s->prev_pos_x, s->prev_pos_y, s->prev_width, s->prev_height); | |
| 2003 | } | ||
| 2004 | |||
| 2005 | 136 | return 0; | |
| 2006 | } | ||
| 2007 | |||
| 2008 | 136 | static int webp_anim_decode_frame(AVCodecContext *avctx, AVFrame *p, | |
| 2009 | int *got_frame, AVPacket *avpkt) | ||
| 2010 | { | ||
| 2011 | 136 | AnimatedWebPContext *s = avctx->priv_data; | |
| 2012 | 136 | int key_frame = (avpkt->flags & AV_PKT_FLAG_KEY); | |
| 2013 | int ret; | ||
| 2014 | |||
| 2015 | GetByteContext gb; | ||
| 2016 | 136 | bytestream2_init(&gb, avpkt->data, avpkt->size); | |
| 2017 | |||
| 2018 | /* Parse ANMF header. */ | ||
| 2019 | 136 | s->pos_x = bytestream2_get_le24(&gb) * 2; | |
| 2020 | 136 | s->pos_y = bytestream2_get_le24(&gb) * 2; | |
| 2021 | 136 | s->w.width = bytestream2_get_le24(&gb) + 1; | |
| 2022 | 136 | s->w.height = bytestream2_get_le24(&gb) + 1; | |
| 2023 | 136 | s->duration = bytestream2_get_le24(&gb); | |
| 2024 | 136 | s->anmf_flags = bytestream2_get_byte(&gb); | |
| 2025 | |||
| 2026 | 136 | av_log(avctx, AV_LOG_DEBUG, | |
| 2027 | "ANMF frame pos: %dx%d size: %dx%d duration: %d\n", | ||
| 2028 | s->pos_x, s->pos_y, s->w.width, s->w.height, s->duration); | ||
| 2029 | |||
| 2030 | /* Reset alpha field from previous frame. */ | ||
| 2031 | 136 | s->w.has_alpha = 0; | |
| 2032 | |||
| 2033 | /* Parse ANMF subchunks. */ | ||
| 2034 |
2/2✓ Branch 1 taken 150 times.
✓ Branch 2 taken 136 times.
|
286 | while (bytestream2_get_bytes_left(&gb) > 8) { |
| 2035 | 150 | uint32_t chunk_type = bytestream2_get_le32(&gb); | |
| 2036 | 150 | uint32_t chunk_size = bytestream2_get_le32(&gb); | |
| 2037 | |||
| 2038 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
|
150 | if (chunk_size == UINT32_MAX) { |
| 2039 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 2040 | ✗ | goto end; | |
| 2041 | } | ||
| 2042 | 150 | chunk_size += chunk_size & 1; | |
| 2043 | |||
| 2044 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 150 times.
|
150 | if (bytestream2_get_bytes_left(&gb) < chunk_size) { |
| 2045 | /* we seem to be running out of data, but it could also be that the | ||
| 2046 | * bitstream has trailing junk leading to bogus chunk_size. */ | ||
| 2047 | ✗ | break; | |
| 2048 | } | ||
| 2049 | |||
| 2050 |
3/4✓ Branch 0 taken 14 times.
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 93 times.
✗ Branch 3 not taken.
|
150 | switch (chunk_type) { |
| 2051 | 14 | case MKTAG('A', 'L', 'P', 'H'): { | |
| 2052 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (chunk_size == 0) { |
| 2053 | ✗ | av_log(avctx, AV_LOG_ERROR, "invalid ALPHA chunk size\n"); | |
| 2054 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 2055 | ✗ | goto end; | |
| 2056 | } | ||
| 2057 | 14 | int alpha_header = bytestream2_get_byte(&gb); | |
| 2058 | 14 | s->w.alpha_data = avpkt->data + bytestream2_tell(&gb); | |
| 2059 | 14 | s->w.alpha_data_size = chunk_size - 1; | |
| 2060 | 14 | bytestream2_skip(&gb, s->w.alpha_data_size); | |
| 2061 | |||
| 2062 | 14 | int filter_m = (alpha_header >> 2) & 0x03; | |
| 2063 | 14 | int compression = alpha_header & 0x03; | |
| 2064 | |||
| 2065 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (compression > ALPHA_COMPRESSION_VP8L) { |
| 2066 | ✗ | av_log(avctx, AV_LOG_VERBOSE, | |
| 2067 | "skipping unsupported ALPHA chunk\n"); | ||
| 2068 | } else { | ||
| 2069 | 14 | s->w.has_alpha = 1; | |
| 2070 | 14 | s->w.alpha_compression = compression; | |
| 2071 | 14 | s->w.alpha_filter = filter_m; | |
| 2072 | } | ||
| 2073 | |||
| 2074 | 150 | break; | |
| 2075 | } | ||
| 2076 | 43 | case MKTAG('V', 'P', '8', ' '): | |
| 2077 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
|
43 | if (*got_frame) { |
| 2078 | ✗ | av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra VP8 chunk\n"); | |
| 2079 | ✗ | bytestream2_skip(&gb, chunk_size); | |
| 2080 | ✗ | break; | |
| 2081 | } | ||
| 2082 | 43 | ret = vp8_lossy_decode_frame(avctx, s->subframe, got_frame, | |
| 2083 | 43 | avpkt->data + bytestream2_tell(&gb), | |
| 2084 | chunk_size); | ||
| 2085 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
|
43 | if (ret < 0) |
| 2086 | ✗ | goto end; | |
| 2087 | 43 | ret = prepare_canvas(s, key_frame, AV_PIX_FMT_YUVA420P); | |
| 2088 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
|
43 | if (ret < 0) |
| 2089 | ✗ | goto end; | |
| 2090 | 43 | bytestream2_skip(&gb, chunk_size); | |
| 2091 | 43 | break; | |
| 2092 | 93 | case MKTAG('V', 'P', '8', 'L'): | |
| 2093 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
|
93 | if (*got_frame) { |
| 2094 | ✗ | av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra VP8L chunk\n"); | |
| 2095 | ✗ | bytestream2_skip(&gb, chunk_size); | |
| 2096 | ✗ | break; | |
| 2097 | } | ||
| 2098 | 93 | ret = vp8_lossless_decode_frame(avctx, s->subframe, got_frame, | |
| 2099 | 93 | avpkt->data + bytestream2_tell(&gb), | |
| 2100 | chunk_size, 0); | ||
| 2101 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
|
93 | if (ret < 0) |
| 2102 | ✗ | goto end; | |
| 2103 | 93 | ret = prepare_canvas(s, key_frame, AV_PIX_FMT_ARGB); | |
| 2104 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
|
93 | if (ret < 0) |
| 2105 | ✗ | goto end; | |
| 2106 | #if FF_API_CODEC_PROPS | ||
| 2107 | FF_DISABLE_DEPRECATION_WARNINGS | ||
| 2108 | 93 | avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS; | |
| 2109 | FF_ENABLE_DEPRECATION_WARNINGS | ||
| 2110 | #endif | ||
| 2111 | 93 | bytestream2_skip(&gb, chunk_size); | |
| 2112 | 93 | break; | |
| 2113 | ✗ | default: | |
| 2114 | ✗ | av_log(avctx, AV_LOG_VERBOSE, "skipping unknown chunk: %s\n", | |
| 2115 | ✗ | av_fourcc2str(chunk_type)); | |
| 2116 | ✗ | bytestream2_skip(&gb, chunk_size); | |
| 2117 | ✗ | break; | |
| 2118 | } | ||
| 2119 | } | ||
| 2120 | |||
| 2121 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
|
136 | if (!*got_frame) { |
| 2122 | ✗ | av_log(avctx, AV_LOG_ERROR, "image data not found\n"); | |
| 2123 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 2124 | ✗ | goto end; | |
| 2125 | } | ||
| 2126 | |||
| 2127 | /* The subframe dimensions may have been modified by update_canvas_size() */ | ||
| 2128 |
1/2✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
|
136 | if (s->pos_x + s->w.width > s->canvas_width || |
| 2129 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
|
136 | s->pos_y + s->w.height > s->canvas_height) { |
| 2130 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 2131 | "Frame (%dx%d at pos %dx%d) does not fit into canvas (%dx%d)\n", | ||
| 2132 | s->w.width, s->w.height, s->pos_x, s->pos_y, | ||
| 2133 | s->canvas_width, s->canvas_height); | ||
| 2134 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 2135 | ✗ | goto end; | |
| 2136 | } | ||
| 2137 | |||
| 2138 | 136 | ret = blend_subframe_into_canvas(s); | |
| 2139 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
|
136 | if (ret < 0) |
| 2140 | ✗ | goto end; | |
| 2141 | |||
| 2142 | 136 | ret = av_frame_ref(p, s->canvas); | |
| 2143 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
|
136 | if (ret < 0) |
| 2144 | ✗ | goto end; | |
| 2145 | |||
| 2146 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 133 times.
|
136 | p->pict_type = key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P; |
| 2147 | 136 | p->pts = avpkt->pts; | |
| 2148 | 136 | p->duration = s->duration; | |
| 2149 | |||
| 2150 | 136 | s->prev_anmf_flags = s->anmf_flags; | |
| 2151 | 136 | s->prev_width = s->w.width; | |
| 2152 | 136 | s->prev_height = s->w.height; | |
| 2153 | 136 | s->prev_pos_x = s->pos_x; | |
| 2154 | 136 | s->prev_pos_y = s->pos_y; | |
| 2155 | |||
| 2156 | 136 | ret = avpkt->size; | |
| 2157 | |||
| 2158 | 136 | end: | |
| 2159 | 136 | av_frame_unref(s->subframe); | |
| 2160 | 136 | return ret; | |
| 2161 | } | ||
| 2162 | |||
| 2163 | 6 | static av_cold int webp_anim_decode_init(AVCodecContext *avctx) | |
| 2164 | { | ||
| 2165 | 6 | AnimatedWebPContext *s = avctx->priv_data; | |
| 2166 | |||
| 2167 | 6 | s->w.avctx = avctx; | |
| 2168 | 6 | s->canvas_width = avctx->width; | |
| 2169 | 6 | s->canvas_height = avctx->height; | |
| 2170 | |||
| 2171 | 6 | s->canvas = av_frame_alloc(); | |
| 2172 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!s->canvas) |
| 2173 | ✗ | return AVERROR(ENOMEM); | |
| 2174 | |||
| 2175 | 6 | s->subframe = av_frame_alloc(); | |
| 2176 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!s->subframe) |
| 2177 | ✗ | return AVERROR(ENOMEM); | |
| 2178 | |||
| 2179 | /** | ||
| 2180 | * Use background color if it was provided by the demuxer. Otherwise, the | ||
| 2181 | * background color will be 0x00000000 (transparent black). | ||
| 2182 | */ | ||
| 2183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (avctx->extradata_size >= 4) { |
| 2184 | ✗ | s->background_argb[0] = avctx->extradata[3]; | |
| 2185 | ✗ | s->background_argb[1] = avctx->extradata[2]; | |
| 2186 | ✗ | s->background_argb[2] = avctx->extradata[1]; | |
| 2187 | ✗ | s->background_argb[3] = avctx->extradata[0]; | |
| 2188 | } | ||
| 2189 | |||
| 2190 | /* Convert background color to YUVA. */ | ||
| 2191 | 6 | const uint8_t *argb = s->background_argb; | |
| 2192 | 6 | s->background_yuva[0] = RGB_TO_Y_CCIR(argb[1], argb[2], argb[3]); | |
| 2193 | 6 | s->background_yuva[1] = RGB_TO_U_CCIR(argb[1], argb[2], argb[3], 0); | |
| 2194 | 6 | s->background_yuva[2] = RGB_TO_V_CCIR(argb[1], argb[2], argb[3], 0); | |
| 2195 | 6 | s->background_yuva[3] = argb[0]; | |
| 2196 | |||
| 2197 | 6 | return webp_decode_init(avctx); | |
| 2198 | } | ||
| 2199 | |||
| 2200 | 6 | static av_cold int webp_anim_decode_close(AVCodecContext *avctx) | |
| 2201 | { | ||
| 2202 | 6 | AnimatedWebPContext *s = avctx->priv_data; | |
| 2203 | |||
| 2204 | 6 | av_frame_free(&s->canvas); | |
| 2205 | 6 | av_frame_free(&s->subframe); | |
| 2206 | |||
| 2207 | 6 | return webp_decode_close(avctx); | |
| 2208 | } | ||
| 2209 | |||
| 2210 | const FFCodec ff_webp_anim_decoder = { | ||
| 2211 | .p.name = "webp_anim", | ||
| 2212 | CODEC_LONG_NAME("Animated WebP image"), | ||
| 2213 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 2214 | .p.id = AV_CODEC_ID_WEBP_ANIM, | ||
| 2215 | .priv_data_size = sizeof(AnimatedWebPContext), | ||
| 2216 | .init = webp_anim_decode_init, | ||
| 2217 | FF_CODEC_DECODE_CB(webp_anim_decode_frame), | ||
| 2218 | .close = webp_anim_decode_close, | ||
| 2219 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS, | ||
| 2220 | .caps_internal = FF_CODEC_CAP_USES_PROGRESSFRAMES, | ||
| 2221 | }; | ||
| 2222 | #endif /* CONFIG_WEBP_ANIM_DECODER */ | ||
| 2223 |