| 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 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with FFmpeg; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @file | ||
| 25 | * WebP image decoder | ||
| 26 | * | ||
| 27 | * @author Aneesh Dogra <aneesh@sugarlabs.org> | ||
| 28 | * Container and Lossy decoding | ||
| 29 | * | ||
| 30 | * @author Justin Ruggles <justin.ruggles@gmail.com> | ||
| 31 | * Lossless decoder | ||
| 32 | * Compressed alpha for lossy | ||
| 33 | * | ||
| 34 | * @author James Almer <jamrial@gmail.com> | ||
| 35 | * Exif metadata | ||
| 36 | * ICC profile | ||
| 37 | * | ||
| 38 | * Unimplemented: | ||
| 39 | * - Animation | ||
| 40 | * - XMP metadata | ||
| 41 | */ | ||
| 42 | |||
| 43 | #include "libavutil/imgutils.h" | ||
| 44 | #include "libavutil/mem.h" | ||
| 45 | |||
| 46 | #define BITSTREAM_READER_LE | ||
| 47 | #include "avcodec.h" | ||
| 48 | #include "bytestream.h" | ||
| 49 | #include "codec_internal.h" | ||
| 50 | #include "decode.h" | ||
| 51 | #include "exif_internal.h" | ||
| 52 | #include "get_bits.h" | ||
| 53 | #include "thread.h" | ||
| 54 | #include "tiff_common.h" | ||
| 55 | #include "vp8.h" | ||
| 56 | |||
| 57 | #define VP8X_FLAG_ANIMATION 0x02 | ||
| 58 | #define VP8X_FLAG_XMP_METADATA 0x04 | ||
| 59 | #define VP8X_FLAG_EXIF_METADATA 0x08 | ||
| 60 | #define VP8X_FLAG_ALPHA 0x10 | ||
| 61 | #define VP8X_FLAG_ICC 0x20 | ||
| 62 | |||
| 63 | #define MAX_PALETTE_SIZE 256 | ||
| 64 | #define MAX_CACHE_BITS 11 | ||
| 65 | #define NUM_CODE_LENGTH_CODES 19 | ||
| 66 | #define HUFFMAN_CODES_PER_META_CODE 5 | ||
| 67 | #define NUM_LITERAL_CODES 256 | ||
| 68 | #define NUM_LENGTH_CODES 24 | ||
| 69 | #define NUM_DISTANCE_CODES 40 | ||
| 70 | #define NUM_SHORT_DISTANCES 120 | ||
| 71 | #define MAX_HUFFMAN_CODE_LENGTH 15 | ||
| 72 | |||
| 73 | static const uint16_t alphabet_sizes[HUFFMAN_CODES_PER_META_CODE] = { | ||
| 74 | NUM_LITERAL_CODES + NUM_LENGTH_CODES, | ||
| 75 | NUM_LITERAL_CODES, NUM_LITERAL_CODES, NUM_LITERAL_CODES, | ||
| 76 | NUM_DISTANCE_CODES | ||
| 77 | }; | ||
| 78 | |||
| 79 | static const uint8_t code_length_code_order[NUM_CODE_LENGTH_CODES] = { | ||
| 80 | 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 | ||
| 81 | }; | ||
| 82 | |||
| 83 | static const int8_t lz77_distance_offsets[NUM_SHORT_DISTANCES][2] = { | ||
| 84 | { 0, 1 }, { 1, 0 }, { 1, 1 }, { -1, 1 }, { 0, 2 }, { 2, 0 }, { 1, 2 }, { -1, 2 }, | ||
| 85 | { 2, 1 }, { -2, 1 }, { 2, 2 }, { -2, 2 }, { 0, 3 }, { 3, 0 }, { 1, 3 }, { -1, 3 }, | ||
| 86 | { 3, 1 }, { -3, 1 }, { 2, 3 }, { -2, 3 }, { 3, 2 }, { -3, 2 }, { 0, 4 }, { 4, 0 }, | ||
| 87 | { 1, 4 }, { -1, 4 }, { 4, 1 }, { -4, 1 }, { 3, 3 }, { -3, 3 }, { 2, 4 }, { -2, 4 }, | ||
| 88 | { 4, 2 }, { -4, 2 }, { 0, 5 }, { 3, 4 }, { -3, 4 }, { 4, 3 }, { -4, 3 }, { 5, 0 }, | ||
| 89 | { 1, 5 }, { -1, 5 }, { 5, 1 }, { -5, 1 }, { 2, 5 }, { -2, 5 }, { 5, 2 }, { -5, 2 }, | ||
| 90 | { 4, 4 }, { -4, 4 }, { 3, 5 }, { -3, 5 }, { 5, 3 }, { -5, 3 }, { 0, 6 }, { 6, 0 }, | ||
| 91 | { 1, 6 }, { -1, 6 }, { 6, 1 }, { -6, 1 }, { 2, 6 }, { -2, 6 }, { 6, 2 }, { -6, 2 }, | ||
| 92 | { 4, 5 }, { -4, 5 }, { 5, 4 }, { -5, 4 }, { 3, 6 }, { -3, 6 }, { 6, 3 }, { -6, 3 }, | ||
| 93 | { 0, 7 }, { 7, 0 }, { 1, 7 }, { -1, 7 }, { 5, 5 }, { -5, 5 }, { 7, 1 }, { -7, 1 }, | ||
| 94 | { 4, 6 }, { -4, 6 }, { 6, 4 }, { -6, 4 }, { 2, 7 }, { -2, 7 }, { 7, 2 }, { -7, 2 }, | ||
| 95 | { 3, 7 }, { -3, 7 }, { 7, 3 }, { -7, 3 }, { 5, 6 }, { -5, 6 }, { 6, 5 }, { -6, 5 }, | ||
| 96 | { 8, 0 }, { 4, 7 }, { -4, 7 }, { 7, 4 }, { -7, 4 }, { 8, 1 }, { 8, 2 }, { 6, 6 }, | ||
| 97 | { -6, 6 }, { 8, 3 }, { 5, 7 }, { -5, 7 }, { 7, 5 }, { -7, 5 }, { 8, 4 }, { 6, 7 }, | ||
| 98 | { -6, 7 }, { 7, 6 }, { -7, 6 }, { 8, 5 }, { 7, 7 }, { -7, 7 }, { 8, 6 }, { 8, 7 } | ||
| 99 | }; | ||
| 100 | |||
| 101 | enum AlphaCompression { | ||
| 102 | ALPHA_COMPRESSION_NONE, | ||
| 103 | ALPHA_COMPRESSION_VP8L, | ||
| 104 | }; | ||
| 105 | |||
| 106 | enum AlphaFilter { | ||
| 107 | ALPHA_FILTER_NONE, | ||
| 108 | ALPHA_FILTER_HORIZONTAL, | ||
| 109 | ALPHA_FILTER_VERTICAL, | ||
| 110 | ALPHA_FILTER_GRADIENT, | ||
| 111 | }; | ||
| 112 | |||
| 113 | enum TransformType { | ||
| 114 | PREDICTOR_TRANSFORM = 0, | ||
| 115 | COLOR_TRANSFORM = 1, | ||
| 116 | SUBTRACT_GREEN = 2, | ||
| 117 | COLOR_INDEXING_TRANSFORM = 3, | ||
| 118 | }; | ||
| 119 | |||
| 120 | enum PredictionMode { | ||
| 121 | PRED_MODE_BLACK, | ||
| 122 | PRED_MODE_L, | ||
| 123 | PRED_MODE_T, | ||
| 124 | PRED_MODE_TR, | ||
| 125 | PRED_MODE_TL, | ||
| 126 | PRED_MODE_AVG_T_AVG_L_TR, | ||
| 127 | PRED_MODE_AVG_L_TL, | ||
| 128 | PRED_MODE_AVG_L_T, | ||
| 129 | PRED_MODE_AVG_TL_T, | ||
| 130 | PRED_MODE_AVG_T_TR, | ||
| 131 | PRED_MODE_AVG_AVG_L_TL_AVG_T_TR, | ||
| 132 | PRED_MODE_SELECT, | ||
| 133 | PRED_MODE_ADD_SUBTRACT_FULL, | ||
| 134 | PRED_MODE_ADD_SUBTRACT_HALF, | ||
| 135 | }; | ||
| 136 | |||
| 137 | enum HuffmanIndex { | ||
| 138 | HUFF_IDX_GREEN = 0, | ||
| 139 | HUFF_IDX_RED = 1, | ||
| 140 | HUFF_IDX_BLUE = 2, | ||
| 141 | HUFF_IDX_ALPHA = 3, | ||
| 142 | HUFF_IDX_DIST = 4 | ||
| 143 | }; | ||
| 144 | |||
| 145 | /* The structure of WebP lossless is an optional series of transformation data, | ||
| 146 | * followed by the primary image. The primary image also optionally contains | ||
| 147 | * an entropy group mapping if there are multiple entropy groups. There is a | ||
| 148 | * basic image type called an "entropy coded image" that is used for all of | ||
| 149 | * these. The type of each entropy coded image is referred to by the | ||
| 150 | * specification as its role. */ | ||
| 151 | enum ImageRole { | ||
| 152 | /* Primary Image: Stores the actual pixels of the image. */ | ||
| 153 | IMAGE_ROLE_ARGB, | ||
| 154 | |||
| 155 | /* Entropy Image: Defines which Huffman group to use for different areas of | ||
| 156 | * the primary image. */ | ||
| 157 | IMAGE_ROLE_ENTROPY, | ||
| 158 | |||
| 159 | /* Predictors: Defines which predictor type to use for different areas of | ||
| 160 | * the primary image. */ | ||
| 161 | IMAGE_ROLE_PREDICTOR, | ||
| 162 | |||
| 163 | /* Color Transform Data: Defines the color transformation for different | ||
| 164 | * areas of the primary image. */ | ||
| 165 | IMAGE_ROLE_COLOR_TRANSFORM, | ||
| 166 | |||
| 167 | /* Color Index: Stored as an image of height == 1. */ | ||
| 168 | IMAGE_ROLE_COLOR_INDEXING, | ||
| 169 | |||
| 170 | IMAGE_ROLE_NB, | ||
| 171 | }; | ||
| 172 | |||
| 173 | typedef struct HuffReader { | ||
| 174 | VLC vlc; /* Huffman decoder context */ | ||
| 175 | int simple; /* whether to use simple mode */ | ||
| 176 | int nb_symbols; /* number of coded symbols */ | ||
| 177 | uint16_t simple_symbols[2]; /* symbols for simple mode */ | ||
| 178 | } HuffReader; | ||
| 179 | |||
| 180 | typedef struct ImageContext { | ||
| 181 | enum ImageRole role; /* role of this image */ | ||
| 182 | AVFrame *frame; /* AVFrame for data */ | ||
| 183 | int color_cache_bits; /* color cache size, log2 */ | ||
| 184 | uint32_t *color_cache; /* color cache data */ | ||
| 185 | int nb_huffman_groups; /* number of huffman groups */ | ||
| 186 | HuffReader *huffman_groups; /* reader for each huffman group */ | ||
| 187 | /* relative size compared to primary image, log2. | ||
| 188 | * for IMAGE_ROLE_COLOR_INDEXING with <= 16 colors, this is log2 of the | ||
| 189 | * number of pixels per byte in the primary image (pixel packing) */ | ||
| 190 | int size_reduction; | ||
| 191 | int is_alpha_primary; | ||
| 192 | } ImageContext; | ||
| 193 | |||
| 194 | typedef struct WebPContext { | ||
| 195 | VP8Context v; /* VP8 Context used for lossy decoding */ | ||
| 196 | GetBitContext gb; /* bitstream reader for main image chunk */ | ||
| 197 | AVFrame *alpha_frame; /* AVFrame for alpha data decompressed from VP8L */ | ||
| 198 | AVPacket *pkt; /* AVPacket to be passed to the underlying VP8 decoder */ | ||
| 199 | AVCodecContext *avctx; /* parent AVCodecContext */ | ||
| 200 | int initialized; /* set once the VP8 context is initialized */ | ||
| 201 | int has_alpha; /* has a separate alpha chunk */ | ||
| 202 | enum AlphaCompression alpha_compression; /* compression type for alpha chunk */ | ||
| 203 | enum AlphaFilter alpha_filter; /* filtering method for alpha chunk */ | ||
| 204 | const uint8_t *alpha_data; /* alpha chunk data */ | ||
| 205 | int alpha_data_size; /* alpha chunk data size */ | ||
| 206 | int has_exif; /* set after an EXIF chunk has been processed */ | ||
| 207 | int has_iccp; /* set after an ICCP chunk has been processed */ | ||
| 208 | int width; /* image width */ | ||
| 209 | int height; /* image height */ | ||
| 210 | |||
| 211 | int nb_transforms; /* number of transforms */ | ||
| 212 | enum TransformType transforms[4]; /* transformations used in the image, in order */ | ||
| 213 | /* reduced width when using a color indexing transform with <= 16 colors (pixel packing) | ||
| 214 | * before pixels are unpacked, or same as width otherwise. */ | ||
| 215 | int reduced_width; | ||
| 216 | int nb_huffman_groups; /* number of huffman groups in the primary image */ | ||
| 217 | ImageContext image[IMAGE_ROLE_NB]; /* image context for each role */ | ||
| 218 | } WebPContext; | ||
| 219 | |||
| 220 | #define GET_PIXEL(frame, x, y) \ | ||
| 221 | ((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x)) | ||
| 222 | |||
| 223 | #define GET_PIXEL_COMP(frame, x, y, c) \ | ||
| 224 | (*((frame)->data[0] + (y) * frame->linesize[0] + 4 * (x) + c)) | ||
| 225 | |||
| 226 | 60 | static void image_ctx_free(ImageContext *img) | |
| 227 | { | ||
| 228 | int i, j; | ||
| 229 | |||
| 230 | 60 | av_free(img->color_cache); | |
| 231 |
3/4✓ Branch 0 taken 22 times.
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
|
60 | if (img->role != IMAGE_ROLE_ARGB && !img->is_alpha_primary) |
| 232 | 22 | av_frame_free(&img->frame); | |
| 233 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 26 times.
|
60 | if (img->huffman_groups) { |
| 234 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 34 times.
|
76 | for (i = 0; i < img->nb_huffman_groups; i++) { |
| 235 |
2/2✓ Branch 0 taken 210 times.
✓ Branch 1 taken 42 times.
|
252 | for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; j++) |
| 236 | 210 | ff_vlc_free(&img->huffman_groups[i * HUFFMAN_CODES_PER_META_CODE + j].vlc); | |
| 237 | } | ||
| 238 | 34 | av_free(img->huffman_groups); | |
| 239 | } | ||
| 240 | 60 | memset(img, 0, sizeof(*img)); | |
| 241 | 60 | } | |
| 242 | |||
| 243 | 280428 | static int huff_reader_get_symbol(HuffReader *r, GetBitContext *gb) | |
| 244 | { | ||
| 245 |
2/2✓ Branch 0 taken 73834 times.
✓ Branch 1 taken 206594 times.
|
280428 | if (r->simple) { |
| 246 |
2/2✓ Branch 0 taken 73514 times.
✓ Branch 1 taken 320 times.
|
73834 | if (r->nb_symbols == 1) |
| 247 | 73514 | return r->simple_symbols[0]; | |
| 248 | else | ||
| 249 | 320 | return r->simple_symbols[get_bits1(gb)]; | |
| 250 | } else | ||
| 251 | 206594 | return get_vlc2(gb, r->vlc.table, 8, 2); | |
| 252 | } | ||
| 253 | |||
| 254 | 160 | static int huff_reader_build_canonical(HuffReader *r, const uint8_t *code_lengths, | |
| 255 | uint16_t len_counts[MAX_HUFFMAN_CODE_LENGTH + 1], | ||
| 256 | uint8_t lens[], uint16_t syms[], | ||
| 257 | int alphabet_size, void *logctx) | ||
| 258 | { | ||
| 259 | 160 | unsigned nb_codes = 0; | |
| 260 | int ret; | ||
| 261 | |||
| 262 | // Count the number of symbols of each length and transform len_counts | ||
| 263 | // into an array of offsets. | ||
| 264 |
2/2✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 160 times.
|
2560 | for (int len = 1; len <= MAX_HUFFMAN_CODE_LENGTH; ++len) { |
| 265 | 2400 | unsigned cnt = len_counts[len]; | |
| 266 | 2400 | len_counts[len] = nb_codes; | |
| 267 | 2400 | nb_codes += cnt; | |
| 268 | } | ||
| 269 | |||
| 270 |
2/2✓ Branch 0 taken 22050 times.
✓ Branch 1 taken 160 times.
|
22210 | for (int sym = 0; sym < alphabet_size; ++sym) { |
| 271 |
2/2✓ Branch 0 taken 7476 times.
✓ Branch 1 taken 14574 times.
|
22050 | if (code_lengths[sym]) { |
| 272 | 7476 | unsigned idx = len_counts[code_lengths[sym]]++; | |
| 273 | 7476 | syms[idx] = sym; | |
| 274 | 7476 | lens[idx] = code_lengths[sym]; | |
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 160 times.
|
160 | if (nb_codes == 0) { |
| 279 | // No symbols | ||
| 280 | ✗ | return AVERROR_INVALIDDATA; | |
| 281 | } | ||
| 282 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 160 times.
|
160 | if (nb_codes == 1) { |
| 283 | // Special-case 1 symbol since the VLC reader cannot handle it | ||
| 284 | ✗ | r->nb_symbols = 1; | |
| 285 | ✗ | r->simple = 1; | |
| 286 | ✗ | r->simple_symbols[0] = syms[0]; | |
| 287 | ✗ | return 0; | |
| 288 | } | ||
| 289 | |||
| 290 | 160 | ret = ff_vlc_init_from_lengths(&r->vlc, 8, nb_codes, lens, 1, | |
| 291 | syms, 2, 2, 0, VLC_INIT_OUTPUT_LE, logctx); | ||
| 292 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 160 times.
|
160 | if (ret < 0) |
| 293 | ✗ | return ret; | |
| 294 | 160 | r->simple = 0; | |
| 295 | |||
| 296 | 160 | return 0; | |
| 297 | } | ||
| 298 | |||
| 299 | 130 | static void read_huffman_code_simple(WebPContext *s, HuffReader *hc) | |
| 300 | { | ||
| 301 | 130 | hc->nb_symbols = get_bits1(&s->gb) + 1; | |
| 302 | |||
| 303 |
2/2✓ Branch 1 taken 18 times.
✓ Branch 2 taken 112 times.
|
130 | if (get_bits1(&s->gb)) |
| 304 | 18 | hc->simple_symbols[0] = get_bits(&s->gb, 8); | |
| 305 | else | ||
| 306 | 112 | hc->simple_symbols[0] = get_bits1(&s->gb); | |
| 307 | |||
| 308 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 116 times.
|
130 | if (hc->nb_symbols == 2) |
| 309 | 14 | hc->simple_symbols[1] = get_bits(&s->gb, 8); | |
| 310 | |||
| 311 | 130 | hc->simple = 1; | |
| 312 | 130 | } | |
| 313 | |||
| 314 | 80 | static int read_huffman_code_normal(WebPContext *s, HuffReader *hc, | |
| 315 | int alphabet_size) | ||
| 316 | { | ||
| 317 | 80 | HuffReader code_len_hc = { { 0 }, 0, 0, { 0 } }; | |
| 318 | uint8_t *code_lengths; | ||
| 319 | 80 | uint8_t code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 }; | |
| 320 | uint8_t reordered_code_length_code_lengths[NUM_CODE_LENGTH_CODES]; | ||
| 321 | uint16_t reordered_code_length_syms[NUM_CODE_LENGTH_CODES]; | ||
| 322 | 80 | uint16_t len_counts[MAX_HUFFMAN_CODE_LENGTH + 1] = { 0 }; | |
| 323 | int symbol, max_symbol, prev_code_len, ret; | ||
| 324 | 80 | int num_codes = 4 + get_bits(&s->gb, 4); | |
| 325 | |||
| 326 | av_assert1(num_codes <= NUM_CODE_LENGTH_CODES); | ||
| 327 | |||
| 328 |
2/2✓ Branch 0 taken 1100 times.
✓ Branch 1 taken 80 times.
|
1180 | for (int i = 0; i < num_codes; i++) { |
| 329 | 1100 | unsigned len = get_bits(&s->gb, 3); | |
| 330 | 1100 | code_length_code_lengths[code_length_code_order[i]] = len; | |
| 331 | 1100 | len_counts[len]++; | |
| 332 | } | ||
| 333 | |||
| 334 |
2/2✓ Branch 1 taken 8 times.
✓ Branch 2 taken 72 times.
|
80 | if (get_bits1(&s->gb)) { |
| 335 | 8 | int bits = 2 + 2 * get_bits(&s->gb, 3); | |
| 336 | 8 | max_symbol = 2 + get_bits(&s->gb, bits); | |
| 337 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (max_symbol > alphabet_size) { |
| 338 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "max symbol %d > alphabet size %d\n", | |
| 339 | max_symbol, alphabet_size); | ||
| 340 | ✗ | return AVERROR_INVALIDDATA; | |
| 341 | } | ||
| 342 | } else { | ||
| 343 | 72 | max_symbol = alphabet_size; | |
| 344 | } | ||
| 345 | |||
| 346 | 80 | ret = huff_reader_build_canonical(&code_len_hc, code_length_code_lengths, len_counts, | |
| 347 | reordered_code_length_code_lengths, | ||
| 348 | reordered_code_length_syms, | ||
| 349 | 80 | NUM_CODE_LENGTH_CODES, s->avctx); | |
| 350 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | if (ret < 0) |
| 351 | ✗ | return ret; | |
| 352 | |||
| 353 | 80 | code_lengths = av_malloc_array(alphabet_size, 2 * sizeof(uint8_t) + sizeof(uint16_t)); | |
| 354 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | if (!code_lengths) { |
| 355 | ✗ | ret = AVERROR(ENOMEM); | |
| 356 | ✗ | goto finish; | |
| 357 | } | ||
| 358 | |||
| 359 | 80 | prev_code_len = 8; | |
| 360 | 80 | symbol = 0; | |
| 361 | 80 | memset(len_counts, 0, sizeof(len_counts)); | |
| 362 |
2/2✓ Branch 0 taken 3264 times.
✓ Branch 1 taken 72 times.
|
3336 | while (symbol < alphabet_size) { |
| 363 | int code_len; | ||
| 364 | |||
| 365 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 3256 times.
|
3264 | if (!max_symbol--) |
| 366 | 8 | break; | |
| 367 | 3256 | code_len = huff_reader_get_symbol(&code_len_hc, &s->gb); | |
| 368 |
2/2✓ Branch 0 taken 1938 times.
✓ Branch 1 taken 1318 times.
|
3256 | if (code_len < 16U) { |
| 369 | /* Code length code [0..15] indicates literal code lengths. */ | ||
| 370 | 1938 | code_lengths[symbol++] = code_len; | |
| 371 | 1938 | len_counts[code_len]++; | |
| 372 |
2/2✓ Branch 0 taken 1818 times.
✓ Branch 1 taken 120 times.
|
1938 | if (code_len) |
| 373 | 1818 | prev_code_len = code_len; | |
| 374 | } else { | ||
| 375 | 1318 | int repeat = 0, length = 0; | |
| 376 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 888 times.
✓ Branch 2 taken 160 times.
✓ Branch 3 taken 270 times.
|
1318 | switch (code_len) { |
| 377 | ✗ | default: | |
| 378 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 379 | ✗ | goto finish; | |
| 380 | 888 | case 16: | |
| 381 | /* Code 16 repeats the previous non-zero value [3..6] times, | ||
| 382 | * i.e., 3 + ReadBits(2) times. If code 16 is used before a | ||
| 383 | * non-zero value has been emitted, a value of 8 is repeated. */ | ||
| 384 | 888 | repeat = 3 + get_bits(&s->gb, 2); | |
| 385 | 888 | length = prev_code_len; | |
| 386 | 888 | len_counts[length] += repeat; | |
| 387 | 888 | break; | |
| 388 | 160 | case 17: | |
| 389 | /* Code 17 emits a streak of zeros [3..10], i.e., | ||
| 390 | * 3 + ReadBits(3) times. */ | ||
| 391 | 160 | repeat = 3 + get_bits(&s->gb, 3); | |
| 392 | 160 | break; | |
| 393 | 270 | case 18: | |
| 394 | /* Code 18 emits a streak of zeros of length [11..138], i.e., | ||
| 395 | * 11 + ReadBits(7) times. */ | ||
| 396 | 270 | repeat = 11 + get_bits(&s->gb, 7); | |
| 397 | 270 | break; | |
| 398 | } | ||
| 399 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1318 times.
|
1318 | if (symbol + repeat > alphabet_size) { |
| 400 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
| 401 | "invalid symbol %d + repeat %d > alphabet size %d\n", | ||
| 402 | symbol, repeat, alphabet_size); | ||
| 403 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 404 | ✗ | goto finish; | |
| 405 | } | ||
| 406 |
2/2✓ Branch 0 taken 18592 times.
✓ Branch 1 taken 1318 times.
|
19910 | while (repeat-- > 0) |
| 407 | 18592 | code_lengths[symbol++] = length; | |
| 408 | } | ||
| 409 | } | ||
| 410 | |||
| 411 | 80 | ret = huff_reader_build_canonical(hc, code_lengths, len_counts, | |
| 412 | code_lengths + symbol, | ||
| 413 | 80 | (uint16_t*)(code_lengths + 2 * symbol), | |
| 414 | 80 | symbol, s->avctx); | |
| 415 | |||
| 416 | 80 | finish: | |
| 417 | 80 | ff_vlc_free(&code_len_hc.vlc); | |
| 418 | 80 | av_free(code_lengths); | |
| 419 | 80 | return ret; | |
| 420 | } | ||
| 421 | |||
| 422 | static int decode_entropy_coded_image(WebPContext *s, enum ImageRole role, | ||
| 423 | int w, int h); | ||
| 424 | |||
| 425 | #define PARSE_BLOCK_SIZE(w, h) do { \ | ||
| 426 | block_bits = get_bits(&s->gb, 3) + 2; \ | ||
| 427 | blocks_w = FFALIGN((w), 1 << block_bits) >> block_bits; \ | ||
| 428 | blocks_h = FFALIGN((h), 1 << block_bits) >> block_bits; \ | ||
| 429 | } while (0) | ||
| 430 | |||
| 431 | 4 | static int decode_entropy_image(WebPContext *s) | |
| 432 | { | ||
| 433 | ImageContext *img; | ||
| 434 | int ret, block_bits, blocks_w, blocks_h, x, y, max; | ||
| 435 | |||
| 436 | 4 | PARSE_BLOCK_SIZE(s->reduced_width, s->height); | |
| 437 | |||
| 438 | 4 | ret = decode_entropy_coded_image(s, IMAGE_ROLE_ENTROPY, blocks_w, blocks_h); | |
| 439 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
| 440 | ✗ | return ret; | |
| 441 | |||
| 442 | 4 | img = &s->image[IMAGE_ROLE_ENTROPY]; | |
| 443 | 4 | img->size_reduction = block_bits; | |
| 444 | |||
| 445 | /* the number of huffman groups is determined by the maximum group number | ||
| 446 | * coded in the entropy image */ | ||
| 447 | 4 | max = 0; | |
| 448 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 4 times.
|
68 | for (y = 0; y < img->frame->height; y++) { |
| 449 |
2/2✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 64 times.
|
1088 | for (x = 0; x < img->frame->width; x++) { |
| 450 | 1024 | int p0 = GET_PIXEL_COMP(img->frame, x, y, 1); | |
| 451 | 1024 | int p1 = GET_PIXEL_COMP(img->frame, x, y, 2); | |
| 452 | 1024 | int p = p0 << 8 | p1; | |
| 453 | 1024 | max = FFMAX(max, p); | |
| 454 | } | ||
| 455 | } | ||
| 456 | 4 | s->nb_huffman_groups = max + 1; | |
| 457 | |||
| 458 | 4 | return 0; | |
| 459 | } | ||
| 460 | |||
| 461 | 6 | static int parse_transform_predictor(WebPContext *s) | |
| 462 | { | ||
| 463 | int block_bits, blocks_w, blocks_h, ret; | ||
| 464 | |||
| 465 | 6 | PARSE_BLOCK_SIZE(s->reduced_width, s->height); | |
| 466 | |||
| 467 | 6 | ret = decode_entropy_coded_image(s, IMAGE_ROLE_PREDICTOR, blocks_w, | |
| 468 | blocks_h); | ||
| 469 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (ret < 0) |
| 470 | ✗ | return ret; | |
| 471 | |||
| 472 | 6 | s->image[IMAGE_ROLE_PREDICTOR].size_reduction = block_bits; | |
| 473 | |||
| 474 | 6 | return 0; | |
| 475 | } | ||
| 476 | |||
| 477 | 4 | static int parse_transform_color(WebPContext *s) | |
| 478 | { | ||
| 479 | int block_bits, blocks_w, blocks_h, ret; | ||
| 480 | |||
| 481 | 4 | PARSE_BLOCK_SIZE(s->reduced_width, s->height); | |
| 482 | |||
| 483 | 4 | ret = decode_entropy_coded_image(s, IMAGE_ROLE_COLOR_TRANSFORM, blocks_w, | |
| 484 | blocks_h); | ||
| 485 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
| 486 | ✗ | return ret; | |
| 487 | |||
| 488 | 4 | s->image[IMAGE_ROLE_COLOR_TRANSFORM].size_reduction = block_bits; | |
| 489 | |||
| 490 | 4 | return 0; | |
| 491 | } | ||
| 492 | |||
| 493 | 8 | static int parse_transform_color_indexing(WebPContext *s) | |
| 494 | { | ||
| 495 | ImageContext *img; | ||
| 496 | int width_bits, index_size, ret, x; | ||
| 497 | uint8_t *ct; | ||
| 498 | |||
| 499 | 8 | index_size = get_bits(&s->gb, 8) + 1; | |
| 500 | |||
| 501 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
|
8 | if (index_size <= 2) |
| 502 | 2 | width_bits = 3; | |
| 503 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | else if (index_size <= 4) |
| 504 | 2 | width_bits = 2; | |
| 505 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | else if (index_size <= 16) |
| 506 | ✗ | width_bits = 1; | |
| 507 | else | ||
| 508 | 4 | width_bits = 0; | |
| 509 | |||
| 510 | 8 | ret = decode_entropy_coded_image(s, IMAGE_ROLE_COLOR_INDEXING, | |
| 511 | index_size, 1); | ||
| 512 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (ret < 0) |
| 513 | ✗ | return ret; | |
| 514 | |||
| 515 | 8 | img = &s->image[IMAGE_ROLE_COLOR_INDEXING]; | |
| 516 | 8 | img->size_reduction = width_bits; | |
| 517 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if (width_bits > 0) |
| 518 | 4 | s->reduced_width = (s->width + ((1 << width_bits) - 1)) >> width_bits; | |
| 519 | |||
| 520 | /* color index values are delta-coded */ | ||
| 521 | 8 | ct = img->frame->data[0] + 4; | |
| 522 |
2/2✓ Branch 0 taken 1424 times.
✓ Branch 1 taken 8 times.
|
1432 | for (x = 4; x < img->frame->width * 4; x++, ct++) |
| 523 | 1424 | ct[0] += ct[-4]; | |
| 524 | |||
| 525 | 8 | return 0; | |
| 526 | } | ||
| 527 | |||
| 528 | 69356 | static HuffReader *get_huffman_group(WebPContext *s, ImageContext *img, | |
| 529 | int x, int y) | ||
| 530 | { | ||
| 531 | 69356 | ImageContext *gimg = &s->image[IMAGE_ROLE_ENTROPY]; | |
| 532 | 69356 | int group = 0; | |
| 533 | |||
| 534 |
2/2✓ Branch 0 taken 65536 times.
✓ Branch 1 taken 3820 times.
|
69356 | if (gimg->size_reduction > 0) { |
| 535 | 65536 | int group_x = x >> gimg->size_reduction; | |
| 536 | 65536 | int group_y = y >> gimg->size_reduction; | |
| 537 | 65536 | int g0 = GET_PIXEL_COMP(gimg->frame, group_x, group_y, 1); | |
| 538 | 65536 | int g1 = GET_PIXEL_COMP(gimg->frame, group_x, group_y, 2); | |
| 539 | 65536 | group = g0 << 8 | g1; | |
| 540 | } | ||
| 541 | |||
| 542 | 69356 | return &img->huffman_groups[group * HUFFMAN_CODES_PER_META_CODE]; | |
| 543 | } | ||
| 544 | |||
| 545 | ✗ | static av_always_inline void color_cache_put(ImageContext *img, uint32_t c) | |
| 546 | { | ||
| 547 | ✗ | uint32_t cache_idx = (0x1E35A7BD * c) >> (32 - img->color_cache_bits); | |
| 548 | ✗ | img->color_cache[cache_idx] = c; | |
| 549 | ✗ | } | |
| 550 | |||
| 551 | 34 | static int decode_entropy_coded_image(WebPContext *s, enum ImageRole role, | |
| 552 | int w, int h) | ||
| 553 | { | ||
| 554 | ImageContext *img; | ||
| 555 | HuffReader *hg; | ||
| 556 | int i, j, ret, x, y, width; | ||
| 557 | |||
| 558 | 34 | img = &s->image[role]; | |
| 559 | 34 | img->role = role; | |
| 560 | |||
| 561 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 12 times.
|
34 | if (!img->frame) { |
| 562 | 22 | img->frame = av_frame_alloc(); | |
| 563 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (!img->frame) |
| 564 | ✗ | return AVERROR(ENOMEM); | |
| 565 | } | ||
| 566 | |||
| 567 | 34 | img->frame->format = AV_PIX_FMT_ARGB; | |
| 568 | 34 | img->frame->width = w; | |
| 569 | 34 | img->frame->height = h; | |
| 570 | |||
| 571 |
4/4✓ Branch 0 taken 12 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 2 times.
|
34 | if (role == IMAGE_ROLE_ARGB && !img->is_alpha_primary) { |
| 572 | 10 | ret = ff_thread_get_buffer(s->avctx, img->frame, 0); | |
| 573 | } else | ||
| 574 | 24 | ret = av_frame_get_buffer(img->frame, 1); | |
| 575 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
|
34 | if (ret < 0) |
| 576 | ✗ | return ret; | |
| 577 | |||
| 578 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
|
34 | if (get_bits1(&s->gb)) { |
| 579 | ✗ | img->color_cache_bits = get_bits(&s->gb, 4); | |
| 580 | ✗ | if (img->color_cache_bits < 1 || img->color_cache_bits > 11) { | |
| 581 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid color cache bits: %d\n", | |
| 582 | img->color_cache_bits); | ||
| 583 | ✗ | return AVERROR_INVALIDDATA; | |
| 584 | } | ||
| 585 | ✗ | img->color_cache = av_calloc(1 << img->color_cache_bits, | |
| 586 | sizeof(*img->color_cache)); | ||
| 587 | ✗ | if (!img->color_cache) | |
| 588 | ✗ | return AVERROR(ENOMEM); | |
| 589 | } else { | ||
| 590 | 34 | img->color_cache_bits = 0; | |
| 591 | } | ||
| 592 | |||
| 593 | 34 | img->nb_huffman_groups = 1; | |
| 594 |
4/4✓ Branch 0 taken 12 times.
✓ Branch 1 taken 22 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 8 times.
|
34 | if (role == IMAGE_ROLE_ARGB && get_bits1(&s->gb)) { |
| 595 | 4 | ret = decode_entropy_image(s); | |
| 596 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
| 597 | ✗ | return ret; | |
| 598 | 4 | img->nb_huffman_groups = s->nb_huffman_groups; | |
| 599 | } | ||
| 600 | 34 | img->huffman_groups = av_calloc(img->nb_huffman_groups, | |
| 601 | HUFFMAN_CODES_PER_META_CODE * | ||
| 602 | sizeof(*img->huffman_groups)); | ||
| 603 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
|
34 | if (!img->huffman_groups) |
| 604 | ✗ | return AVERROR(ENOMEM); | |
| 605 | |||
| 606 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 34 times.
|
76 | for (i = 0; i < img->nb_huffman_groups; i++) { |
| 607 | 42 | hg = &img->huffman_groups[i * HUFFMAN_CODES_PER_META_CODE]; | |
| 608 |
2/2✓ Branch 0 taken 210 times.
✓ Branch 1 taken 42 times.
|
252 | for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; j++) { |
| 609 | 210 | int alphabet_size = alphabet_sizes[j]; | |
| 610 |
3/4✓ Branch 0 taken 42 times.
✓ Branch 1 taken 168 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 42 times.
|
210 | if (!j && img->color_cache_bits > 0) |
| 611 | ✗ | alphabet_size += 1 << img->color_cache_bits; | |
| 612 | |||
| 613 |
2/2✓ Branch 1 taken 130 times.
✓ Branch 2 taken 80 times.
|
210 | if (get_bits1(&s->gb)) { |
| 614 | 130 | read_huffman_code_simple(s, &hg[j]); | |
| 615 | } else { | ||
| 616 | 80 | ret = read_huffman_code_normal(s, &hg[j], alphabet_size); | |
| 617 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | if (ret < 0) |
| 618 | ✗ | return ret; | |
| 619 | } | ||
| 620 | } | ||
| 621 | } | ||
| 622 | |||
| 623 | 34 | width = img->frame->width; | |
| 624 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 22 times.
|
34 | if (role == IMAGE_ROLE_ARGB) |
| 625 | 12 | width = s->reduced_width; | |
| 626 | |||
| 627 | 34 | x = 0; y = 0; | |
| 628 |
2/2✓ Branch 0 taken 69356 times.
✓ Branch 1 taken 34 times.
|
69390 | while (y < img->frame->height) { |
| 629 | int v; | ||
| 630 | |||
| 631 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 69356 times.
|
69356 | if (get_bits_left(&s->gb) < 0) |
| 632 | ✗ | return AVERROR_INVALIDDATA; | |
| 633 | |||
| 634 | 69356 | hg = get_huffman_group(s, img, x, y); | |
| 635 | 69356 | v = huff_reader_get_symbol(&hg[HUFF_IDX_GREEN], &s->gb); | |
| 636 |
2/2✓ Branch 0 taken 69230 times.
✓ Branch 1 taken 126 times.
|
69356 | if (v < NUM_LITERAL_CODES) { |
| 637 | /* literal pixel values */ | ||
| 638 | 69230 | uint8_t *p = GET_PIXEL(img->frame, x, y); | |
| 639 | 69230 | p[2] = v; | |
| 640 | 69230 | p[1] = huff_reader_get_symbol(&hg[HUFF_IDX_RED], &s->gb); | |
| 641 | 69230 | p[3] = huff_reader_get_symbol(&hg[HUFF_IDX_BLUE], &s->gb); | |
| 642 | 69230 | p[0] = huff_reader_get_symbol(&hg[HUFF_IDX_ALPHA], &s->gb); | |
| 643 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 69230 times.
|
69230 | if (img->color_cache_bits) |
| 644 | ✗ | color_cache_put(img, AV_RB32(p)); | |
| 645 | 69230 | x++; | |
| 646 |
2/2✓ Branch 0 taken 750 times.
✓ Branch 1 taken 68480 times.
|
69230 | if (x == width) { |
| 647 | 750 | x = 0; | |
| 648 | 750 | y++; | |
| 649 | } | ||
| 650 |
1/2✓ Branch 0 taken 126 times.
✗ Branch 1 not taken.
|
126 | } else if (v < NUM_LITERAL_CODES + NUM_LENGTH_CODES) { |
| 651 | /* LZ77 backwards mapping */ | ||
| 652 | int prefix_code, length, distance, ref_x, ref_y; | ||
| 653 | |||
| 654 | /* parse length and distance */ | ||
| 655 | 126 | prefix_code = v - NUM_LITERAL_CODES; | |
| 656 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 78 times.
|
126 | if (prefix_code < 4) { |
| 657 | 48 | length = prefix_code + 1; | |
| 658 | } else { | ||
| 659 | 78 | int extra_bits = (prefix_code - 2) >> 1; | |
| 660 | 78 | int offset = 2 + (prefix_code & 1) << extra_bits; | |
| 661 | 78 | length = offset + get_bits(&s->gb, extra_bits) + 1; | |
| 662 | } | ||
| 663 | 126 | prefix_code = huff_reader_get_symbol(&hg[HUFF_IDX_DIST], &s->gb); | |
| 664 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
|
126 | if (prefix_code > 39U) { |
| 665 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
| 666 | "distance prefix code too large: %d\n", prefix_code); | ||
| 667 | ✗ | return AVERROR_INVALIDDATA; | |
| 668 | } | ||
| 669 |
1/2✓ Branch 0 taken 126 times.
✗ Branch 1 not taken.
|
126 | if (prefix_code < 4) { |
| 670 | 126 | distance = prefix_code + 1; | |
| 671 | } else { | ||
| 672 | ✗ | int extra_bits = prefix_code - 2 >> 1; | |
| 673 | ✗ | int offset = 2 + (prefix_code & 1) << extra_bits; | |
| 674 | ✗ | distance = offset + get_bits(&s->gb, extra_bits) + 1; | |
| 675 | } | ||
| 676 | |||
| 677 | /* find reference location */ | ||
| 678 |
1/2✓ Branch 0 taken 126 times.
✗ Branch 1 not taken.
|
126 | if (distance <= NUM_SHORT_DISTANCES) { |
| 679 | 126 | int xi = lz77_distance_offsets[distance - 1][0]; | |
| 680 | 126 | int yi = lz77_distance_offsets[distance - 1][1]; | |
| 681 | 126 | distance = FFMAX(1, xi + yi * width); | |
| 682 | } else { | ||
| 683 | ✗ | distance -= NUM_SHORT_DISTANCES; | |
| 684 | } | ||
| 685 | 126 | ref_x = x; | |
| 686 | 126 | ref_y = y; | |
| 687 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 86 times.
|
126 | if (distance <= x) { |
| 688 | 40 | ref_x -= distance; | |
| 689 | 40 | distance = 0; | |
| 690 | } else { | ||
| 691 | 86 | ref_x = 0; | |
| 692 | 86 | distance -= x; | |
| 693 | } | ||
| 694 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 126 times.
|
148 | while (distance >= width) { |
| 695 | 22 | ref_y--; | |
| 696 | 22 | distance -= width; | |
| 697 | } | ||
| 698 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 62 times.
|
126 | if (distance > 0) { |
| 699 | 64 | ref_x = width - distance; | |
| 700 | 64 | ref_y--; | |
| 701 | } | ||
| 702 | 126 | ref_x = FFMAX(0, ref_x); | |
| 703 | 126 | ref_y = FFMAX(0, ref_y); | |
| 704 | |||
| 705 |
3/4✓ Branch 0 taken 40 times.
✓ Branch 1 taken 86 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
|
126 | if (ref_y == y && ref_x >= x) |
| 706 | ✗ | return AVERROR_INVALIDDATA; | |
| 707 | |||
| 708 | /* copy pixels | ||
| 709 | * source and dest regions can overlap and wrap lines, so just | ||
| 710 | * copy per-pixel */ | ||
| 711 |
2/2✓ Branch 0 taken 958 times.
✓ Branch 1 taken 118 times.
|
1076 | for (i = 0; i < length; i++) { |
| 712 | 958 | uint8_t *p_ref = GET_PIXEL(img->frame, ref_x, ref_y); | |
| 713 | 958 | uint8_t *p = GET_PIXEL(img->frame, x, y); | |
| 714 | |||
| 715 | 958 | AV_COPY32(p, p_ref); | |
| 716 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 958 times.
|
958 | if (img->color_cache_bits) |
| 717 | ✗ | color_cache_put(img, AV_RB32(p)); | |
| 718 | 958 | x++; | |
| 719 | 958 | ref_x++; | |
| 720 |
2/2✓ Branch 0 taken 74 times.
✓ Branch 1 taken 884 times.
|
958 | if (x == width) { |
| 721 | 74 | x = 0; | |
| 722 | 74 | y++; | |
| 723 | } | ||
| 724 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 886 times.
|
958 | if (ref_x == width) { |
| 725 | 72 | ref_x = 0; | |
| 726 | 72 | ref_y++; | |
| 727 | } | ||
| 728 |
3/4✓ Branch 0 taken 950 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 950 times.
✗ Branch 3 not taken.
|
958 | if (y == img->frame->height || ref_y == img->frame->height) |
| 729 | break; | ||
| 730 | } | ||
| 731 | } else { | ||
| 732 | /* read from color cache */ | ||
| 733 | ✗ | uint8_t *p = GET_PIXEL(img->frame, x, y); | |
| 734 | ✗ | int cache_idx = v - (NUM_LITERAL_CODES + NUM_LENGTH_CODES); | |
| 735 | |||
| 736 | ✗ | if (!img->color_cache_bits) { | |
| 737 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "color cache not found\n"); | |
| 738 | ✗ | return AVERROR_INVALIDDATA; | |
| 739 | } | ||
| 740 | ✗ | if (cache_idx >= 1 << img->color_cache_bits) { | |
| 741 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
| 742 | "color cache index out-of-bounds\n"); | ||
| 743 | ✗ | return AVERROR_INVALIDDATA; | |
| 744 | } | ||
| 745 | ✗ | AV_WB32(p, img->color_cache[cache_idx]); | |
| 746 | ✗ | x++; | |
| 747 | ✗ | if (x == width) { | |
| 748 | ✗ | x = 0; | |
| 749 | ✗ | y++; | |
| 750 | } | ||
| 751 | } | ||
| 752 | } | ||
| 753 | |||
| 754 | 34 | return 0; | |
| 755 | } | ||
| 756 | |||
| 757 | /* PRED_MODE_BLACK */ | ||
| 758 | 6 | static void inv_predict_0(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 759 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 760 | { | ||
| 761 | 6 | AV_WB32(p, 0xFF000000); | |
| 762 | 6 | } | |
| 763 | |||
| 764 | /* PRED_MODE_L */ | ||
| 765 | 5300 | static void inv_predict_1(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 | 5300 | AV_COPY32(p, p_l); | |
| 769 | 5300 | } | |
| 770 | |||
| 771 | /* PRED_MODE_T */ | ||
| 772 | 21182 | static void inv_predict_2(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 | 21182 | AV_COPY32(p, p_t); | |
| 776 | 21182 | } | |
| 777 | |||
| 778 | /* PRED_MODE_TR */ | ||
| 779 | 6336 | static void inv_predict_3(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 | 6336 | AV_COPY32(p, p_tr); | |
| 783 | 6336 | } | |
| 784 | |||
| 785 | /* PRED_MODE_TL */ | ||
| 786 | 1248 | static void inv_predict_4(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 | 1248 | AV_COPY32(p, p_tl); | |
| 790 | 1248 | } | |
| 791 | |||
| 792 | /* PRED_MODE_AVG_T_AVG_L_TR */ | ||
| 793 | 4832 | static void inv_predict_5(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 | 4832 | p[0] = p_t[0] + (p_l[0] + p_tr[0] >> 1) >> 1; | |
| 797 | 4832 | p[1] = p_t[1] + (p_l[1] + p_tr[1] >> 1) >> 1; | |
| 798 | 4832 | p[2] = p_t[2] + (p_l[2] + p_tr[2] >> 1) >> 1; | |
| 799 | 4832 | p[3] = p_t[3] + (p_l[3] + p_tr[3] >> 1) >> 1; | |
| 800 | 4832 | } | |
| 801 | |||
| 802 | /* PRED_MODE_AVG_L_TL */ | ||
| 803 | 768 | static void inv_predict_6(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 804 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 805 | { | ||
| 806 | 768 | p[0] = p_l[0] + p_tl[0] >> 1; | |
| 807 | 768 | p[1] = p_l[1] + p_tl[1] >> 1; | |
| 808 | 768 | p[2] = p_l[2] + p_tl[2] >> 1; | |
| 809 | 768 | p[3] = p_l[3] + p_tl[3] >> 1; | |
| 810 | 768 | } | |
| 811 | |||
| 812 | /* PRED_MODE_AVG_L_T */ | ||
| 813 | 3072 | static void inv_predict_7(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 814 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 815 | { | ||
| 816 | 3072 | p[0] = p_l[0] + p_t[0] >> 1; | |
| 817 | 3072 | p[1] = p_l[1] + p_t[1] >> 1; | |
| 818 | 3072 | p[2] = p_l[2] + p_t[2] >> 1; | |
| 819 | 3072 | p[3] = p_l[3] + p_t[3] >> 1; | |
| 820 | 3072 | } | |
| 821 | |||
| 822 | /* PRED_MODE_AVG_TL_T */ | ||
| 823 | 4512 | static void inv_predict_8(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 824 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 825 | { | ||
| 826 | 4512 | p[0] = p_tl[0] + p_t[0] >> 1; | |
| 827 | 4512 | p[1] = p_tl[1] + p_t[1] >> 1; | |
| 828 | 4512 | p[2] = p_tl[2] + p_t[2] >> 1; | |
| 829 | 4512 | p[3] = p_tl[3] + p_t[3] >> 1; | |
| 830 | 4512 | } | |
| 831 | |||
| 832 | /* PRED_MODE_AVG_T_TR */ | ||
| 833 | 11488 | static void inv_predict_9(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 834 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 835 | { | ||
| 836 | 11488 | p[0] = p_t[0] + p_tr[0] >> 1; | |
| 837 | 11488 | p[1] = p_t[1] + p_tr[1] >> 1; | |
| 838 | 11488 | p[2] = p_t[2] + p_tr[2] >> 1; | |
| 839 | 11488 | p[3] = p_t[3] + p_tr[3] >> 1; | |
| 840 | 11488 | } | |
| 841 | |||
| 842 | /* PRED_MODE_AVG_AVG_L_TL_AVG_T_TR */ | ||
| 843 | 3236 | static void inv_predict_10(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 844 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 845 | { | ||
| 846 | 3236 | p[0] = (p_l[0] + p_tl[0] >> 1) + (p_t[0] + p_tr[0] >> 1) >> 1; | |
| 847 | 3236 | p[1] = (p_l[1] + p_tl[1] >> 1) + (p_t[1] + p_tr[1] >> 1) >> 1; | |
| 848 | 3236 | p[2] = (p_l[2] + p_tl[2] >> 1) + (p_t[2] + p_tr[2] >> 1) >> 1; | |
| 849 | 3236 | p[3] = (p_l[3] + p_tl[3] >> 1) + (p_t[3] + p_tr[3] >> 1) >> 1; | |
| 850 | 3236 | } | |
| 851 | |||
| 852 | /* PRED_MODE_SELECT */ | ||
| 853 | 3824 | static void inv_predict_11(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 854 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 855 | { | ||
| 856 | 3824 | int diff = (FFABS(p_l[0] - p_tl[0]) - FFABS(p_t[0] - p_tl[0])) + | |
| 857 | 3824 | (FFABS(p_l[1] - p_tl[1]) - FFABS(p_t[1] - p_tl[1])) + | |
| 858 | 3824 | (FFABS(p_l[2] - p_tl[2]) - FFABS(p_t[2] - p_tl[2])) + | |
| 859 | 3824 | (FFABS(p_l[3] - p_tl[3]) - FFABS(p_t[3] - p_tl[3])); | |
| 860 |
2/2✓ Branch 0 taken 2602 times.
✓ Branch 1 taken 1222 times.
|
3824 | if (diff <= 0) |
| 861 | 2602 | AV_COPY32(p, p_t); | |
| 862 | else | ||
| 863 | 1222 | AV_COPY32(p, p_l); | |
| 864 | 3824 | } | |
| 865 | |||
| 866 | /* PRED_MODE_ADD_SUBTRACT_FULL */ | ||
| 867 | ✗ | static void inv_predict_12(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 868 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 869 | { | ||
| 870 | ✗ | p[0] = av_clip_uint8(p_l[0] + p_t[0] - p_tl[0]); | |
| 871 | ✗ | p[1] = av_clip_uint8(p_l[1] + p_t[1] - p_tl[1]); | |
| 872 | ✗ | p[2] = av_clip_uint8(p_l[2] + p_t[2] - p_tl[2]); | |
| 873 | ✗ | p[3] = av_clip_uint8(p_l[3] + p_t[3] - p_tl[3]); | |
| 874 | ✗ | } | |
| 875 | |||
| 876 | 2048 | static av_always_inline uint8_t clamp_add_subtract_half(int a, int b, int c) | |
| 877 | { | ||
| 878 | 2048 | int d = a + b >> 1; | |
| 879 | 2048 | return av_clip_uint8(d + (d - c) / 2); | |
| 880 | } | ||
| 881 | |||
| 882 | /* PRED_MODE_ADD_SUBTRACT_HALF */ | ||
| 883 | 512 | static void inv_predict_13(uint8_t *p, const uint8_t *p_l, const uint8_t *p_tl, | |
| 884 | const uint8_t *p_t, const uint8_t *p_tr) | ||
| 885 | { | ||
| 886 | 512 | p[0] = clamp_add_subtract_half(p_l[0], p_t[0], p_tl[0]); | |
| 887 | 512 | p[1] = clamp_add_subtract_half(p_l[1], p_t[1], p_tl[1]); | |
| 888 | 512 | p[2] = clamp_add_subtract_half(p_l[2], p_t[2], p_tl[2]); | |
| 889 | 512 | p[3] = clamp_add_subtract_half(p_l[3], p_t[3], p_tl[3]); | |
| 890 | 512 | } | |
| 891 | |||
| 892 | typedef void (*inv_predict_func)(uint8_t *p, const uint8_t *p_l, | ||
| 893 | const uint8_t *p_tl, const uint8_t *p_t, | ||
| 894 | const uint8_t *p_tr); | ||
| 895 | |||
| 896 | static const inv_predict_func inverse_predict[14] = { | ||
| 897 | inv_predict_0, inv_predict_1, inv_predict_2, inv_predict_3, | ||
| 898 | inv_predict_4, inv_predict_5, inv_predict_6, inv_predict_7, | ||
| 899 | inv_predict_8, inv_predict_9, inv_predict_10, inv_predict_11, | ||
| 900 | inv_predict_12, inv_predict_13, | ||
| 901 | }; | ||
| 902 | |||
| 903 | 66316 | static void inverse_prediction(AVFrame *frame, enum PredictionMode m, int x, int y) | |
| 904 | { | ||
| 905 | uint8_t *dec, *p_l, *p_tl, *p_t, *p_tr; | ||
| 906 | uint8_t p[4]; | ||
| 907 | |||
| 908 | 66316 | dec = GET_PIXEL(frame, x, y); | |
| 909 | 66316 | p_l = GET_PIXEL(frame, x - 1, y); | |
| 910 | 66316 | p_tl = GET_PIXEL(frame, x - 1, y - 1); | |
| 911 | 66316 | p_t = GET_PIXEL(frame, x, y - 1); | |
| 912 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 65804 times.
|
66316 | if (x == frame->width - 1) |
| 913 | 512 | p_tr = GET_PIXEL(frame, 0, y); | |
| 914 | else | ||
| 915 | 65804 | p_tr = GET_PIXEL(frame, x + 1, y - 1); | |
| 916 | |||
| 917 | 66316 | inverse_predict[m](p, p_l, p_tl, p_t, p_tr); | |
| 918 | |||
| 919 | 66316 | dec[0] += p[0]; | |
| 920 | 66316 | dec[1] += p[1]; | |
| 921 | 66316 | dec[2] += p[2]; | |
| 922 | 66316 | dec[3] += p[3]; | |
| 923 | 66316 | } | |
| 924 | |||
| 925 | 6 | static int apply_predictor_transform(WebPContext *s) | |
| 926 | { | ||
| 927 | 6 | ImageContext *img = &s->image[IMAGE_ROLE_ARGB]; | |
| 928 | 6 | ImageContext *pimg = &s->image[IMAGE_ROLE_PREDICTOR]; | |
| 929 | int x, y; | ||
| 930 | |||
| 931 |
2/2✓ Branch 0 taken 572 times.
✓ Branch 1 taken 6 times.
|
578 | for (y = 0; y < img->frame->height; y++) { |
| 932 |
2/2✓ Branch 0 taken 66316 times.
✓ Branch 1 taken 572 times.
|
66888 | for (x = 0; x < s->reduced_width; x++) { |
| 933 | 66316 | int tx = x >> pimg->size_reduction; | |
| 934 | 66316 | int ty = y >> pimg->size_reduction; | |
| 935 | 66316 | enum PredictionMode m = GET_PIXEL_COMP(pimg->frame, tx, ty, 2); | |
| 936 | |||
| 937 |
2/2✓ Branch 0 taken 572 times.
✓ Branch 1 taken 65744 times.
|
66316 | if (x == 0) { |
| 938 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 566 times.
|
572 | if (y == 0) |
| 939 | 6 | m = PRED_MODE_BLACK; | |
| 940 | else | ||
| 941 | 566 | m = PRED_MODE_T; | |
| 942 |
2/2✓ Branch 0 taken 532 times.
✓ Branch 1 taken 65212 times.
|
65744 | } else if (y == 0) |
| 943 | 532 | m = PRED_MODE_L; | |
| 944 | |||
| 945 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66316 times.
|
66316 | if (m > 13) { |
| 946 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
| 947 | "invalid predictor mode: %d\n", m); | ||
| 948 | ✗ | return AVERROR_INVALIDDATA; | |
| 949 | } | ||
| 950 | 66316 | inverse_prediction(img->frame, m, x, y); | |
| 951 | } | ||
| 952 | } | ||
| 953 | 6 | return 0; | |
| 954 | } | ||
| 955 | |||
| 956 | 196608 | static av_always_inline uint8_t color_transform_delta(uint8_t color_pred, | |
| 957 | uint8_t color) | ||
| 958 | { | ||
| 959 | 196608 | return (int)ff_u8_to_s8(color_pred) * ff_u8_to_s8(color) >> 5; | |
| 960 | } | ||
| 961 | |||
| 962 | 4 | static int apply_color_transform(WebPContext *s) | |
| 963 | { | ||
| 964 | ImageContext *img, *cimg; | ||
| 965 | int x, y, cx, cy; | ||
| 966 | uint8_t *p, *cp; | ||
| 967 | |||
| 968 | 4 | img = &s->image[IMAGE_ROLE_ARGB]; | |
| 969 | 4 | cimg = &s->image[IMAGE_ROLE_COLOR_TRANSFORM]; | |
| 970 | |||
| 971 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 4 times.
|
516 | for (y = 0; y < img->frame->height; y++) { |
| 972 |
2/2✓ Branch 0 taken 65536 times.
✓ Branch 1 taken 512 times.
|
66048 | for (x = 0; x < s->reduced_width; x++) { |
| 973 | 65536 | cx = x >> cimg->size_reduction; | |
| 974 | 65536 | cy = y >> cimg->size_reduction; | |
| 975 | 65536 | cp = GET_PIXEL(cimg->frame, cx, cy); | |
| 976 | 65536 | p = GET_PIXEL(img->frame, x, y); | |
| 977 | |||
| 978 | 65536 | p[1] += color_transform_delta(cp[3], p[2]); | |
| 979 | 65536 | p[3] += color_transform_delta(cp[2], p[2]) + | |
| 980 | 65536 | color_transform_delta(cp[1], p[1]); | |
| 981 | } | ||
| 982 | } | ||
| 983 | 4 | return 0; | |
| 984 | } | ||
| 985 | |||
| 986 | 4 | static int apply_subtract_green_transform(WebPContext *s) | |
| 987 | { | ||
| 988 | int x, y; | ||
| 989 | 4 | ImageContext *img = &s->image[IMAGE_ROLE_ARGB]; | |
| 990 | |||
| 991 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 4 times.
|
516 | for (y = 0; y < img->frame->height; y++) { |
| 992 |
2/2✓ Branch 0 taken 65536 times.
✓ Branch 1 taken 512 times.
|
66048 | for (x = 0; x < s->reduced_width; x++) { |
| 993 | 65536 | uint8_t *p = GET_PIXEL(img->frame, x, y); | |
| 994 | 65536 | p[1] += p[2]; | |
| 995 | 65536 | p[3] += p[2]; | |
| 996 | } | ||
| 997 | } | ||
| 998 | 4 | return 0; | |
| 999 | } | ||
| 1000 | |||
| 1001 | 8 | static int apply_color_indexing_transform(WebPContext *s) | |
| 1002 | { | ||
| 1003 | ImageContext *img; | ||
| 1004 | ImageContext *pal; | ||
| 1005 | int i, x, y; | ||
| 1006 | uint8_t *p; | ||
| 1007 | |||
| 1008 | 8 | img = &s->image[IMAGE_ROLE_ARGB]; | |
| 1009 | 8 | pal = &s->image[IMAGE_ROLE_COLOR_INDEXING]; | |
| 1010 | |||
| 1011 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if (pal->size_reduction > 0) { // undo pixel packing |
| 1012 | GetBitContext gb_g; | ||
| 1013 | uint8_t *line; | ||
| 1014 | 4 | int pixel_bits = 8 >> pal->size_reduction; | |
| 1015 | |||
| 1016 | 4 | line = av_malloc(img->frame->linesize[0] + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 1017 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!line) |
| 1018 | ✗ | return AVERROR(ENOMEM); | |
| 1019 | |||
| 1020 |
2/2✓ Branch 0 taken 76 times.
✓ Branch 1 taken 4 times.
|
80 | for (y = 0; y < img->frame->height; y++) { |
| 1021 | 76 | p = GET_PIXEL(img->frame, 0, y); | |
| 1022 | 76 | memcpy(line, p, img->frame->linesize[0]); | |
| 1023 | 76 | init_get_bits(&gb_g, line, img->frame->linesize[0] * 8); | |
| 1024 | 76 | skip_bits(&gb_g, 16); | |
| 1025 | 76 | i = 0; | |
| 1026 |
2/2✓ Branch 0 taken 6192 times.
✓ Branch 1 taken 76 times.
|
6268 | for (x = 0; x < img->frame->width; x++) { |
| 1027 | 6192 | p = GET_PIXEL(img->frame, x, y); | |
| 1028 | 6192 | p[2] = get_bits(&gb_g, pixel_bits); | |
| 1029 | 6192 | i++; | |
| 1030 |
2/2✓ Branch 0 taken 768 times.
✓ Branch 1 taken 5424 times.
|
6192 | if (i == 1 << pal->size_reduction) { |
| 1031 | 768 | skip_bits(&gb_g, 24); | |
| 1032 | 768 | i = 0; | |
| 1033 | } | ||
| 1034 | } | ||
| 1035 | } | ||
| 1036 | 4 | av_free(line); | |
| 1037 | 4 | s->reduced_width = s->width; // we are back to full size | |
| 1038 | } | ||
| 1039 | |||
| 1040 | // switch to local palette if it's worth initializing it | ||
| 1041 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
|
8 | if (img->frame->height * img->frame->width > 300) { |
| 1042 | uint8_t palette[256 * 4]; | ||
| 1043 | 2 | const int size = pal->frame->width * 4; | |
| 1044 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | av_assert0(size <= 1024U); |
| 1045 | 2 | memcpy(palette, GET_PIXEL(pal->frame, 0, 0), size); // copy palette | |
| 1046 | // set extra entries to transparent black | ||
| 1047 | 2 | memset(palette + size, 0, 256 * 4 - size); | |
| 1048 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 2 times.
|
62 | for (y = 0; y < img->frame->height; y++) { |
| 1049 |
2/2✓ Branch 0 taken 6000 times.
✓ Branch 1 taken 60 times.
|
6060 | for (x = 0; x < img->frame->width; x++) { |
| 1050 | 6000 | p = GET_PIXEL(img->frame, x, y); | |
| 1051 | 6000 | i = p[2]; | |
| 1052 | 6000 | AV_COPY32(p, &palette[i * 4]); | |
| 1053 | } | ||
| 1054 | } | ||
| 1055 | } else { | ||
| 1056 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 6 times.
|
54 | for (y = 0; y < img->frame->height; y++) { |
| 1057 |
2/2✓ Branch 0 taken 576 times.
✓ Branch 1 taken 48 times.
|
624 | for (x = 0; x < img->frame->width; x++) { |
| 1058 | 576 | p = GET_PIXEL(img->frame, x, y); | |
| 1059 | 576 | i = p[2]; | |
| 1060 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 576 times.
|
576 | if (i >= pal->frame->width) { |
| 1061 | ✗ | AV_WB32(p, 0x00000000); | |
| 1062 | } else { | ||
| 1063 | 576 | const uint8_t *pi = GET_PIXEL(pal->frame, i, 0); | |
| 1064 | 576 | AV_COPY32(p, pi); | |
| 1065 | } | ||
| 1066 | } | ||
| 1067 | } | ||
| 1068 | } | ||
| 1069 | |||
| 1070 | 8 | return 0; | |
| 1071 | } | ||
| 1072 | |||
| 1073 | 16 | static void update_canvas_size(AVCodecContext *avctx, int w, int h) | |
| 1074 | { | ||
| 1075 | 16 | WebPContext *s = avctx->priv_data; | |
| 1076 |
3/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
16 | if (s->width && s->width != w) { |
| 1077 | ✗ | av_log(avctx, AV_LOG_WARNING, "Width mismatch. %d != %d\n", | |
| 1078 | s->width, w); | ||
| 1079 | } | ||
| 1080 | 16 | s->width = w; | |
| 1081 |
3/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
16 | if (s->height && s->height != h) { |
| 1082 | ✗ | av_log(avctx, AV_LOG_WARNING, "Height mismatch. %d != %d\n", | |
| 1083 | s->height, h); | ||
| 1084 | } | ||
| 1085 | 16 | s->height = h; | |
| 1086 | 16 | } | |
| 1087 | |||
| 1088 | 12 | static int vp8_lossless_decode_frame(AVCodecContext *avctx, AVFrame *p, | |
| 1089 | int *got_frame, const uint8_t *data_start, | ||
| 1090 | unsigned int data_size, int is_alpha_chunk) | ||
| 1091 | { | ||
| 1092 | 12 | WebPContext *s = avctx->priv_data; | |
| 1093 | int w, h, ret, i, used; | ||
| 1094 | |||
| 1095 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
|
12 | if (!is_alpha_chunk) |
| 1096 | 10 | avctx->pix_fmt = AV_PIX_FMT_ARGB; | |
| 1097 | |||
| 1098 | 12 | ret = init_get_bits8(&s->gb, data_start, data_size); | |
| 1099 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (ret < 0) |
| 1100 | ✗ | return ret; | |
| 1101 | |||
| 1102 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
|
12 | if (!is_alpha_chunk) { |
| 1103 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
|
10 | if (get_bits(&s->gb, 8) != 0x2F) { |
| 1104 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid WebP Lossless signature\n"); | |
| 1105 | ✗ | return AVERROR_INVALIDDATA; | |
| 1106 | } | ||
| 1107 | |||
| 1108 | 10 | w = get_bits(&s->gb, 14) + 1; | |
| 1109 | 10 | h = get_bits(&s->gb, 14) + 1; | |
| 1110 | |||
| 1111 | 10 | update_canvas_size(avctx, w, h); | |
| 1112 | |||
| 1113 | 10 | ret = ff_set_dimensions(avctx, s->width, s->height); | |
| 1114 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (ret < 0) |
| 1115 | ✗ | return ret; | |
| 1116 | |||
| 1117 | 10 | s->has_alpha = get_bits1(&s->gb); | |
| 1118 | |||
| 1119 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
|
10 | if (get_bits(&s->gb, 3) != 0x0) { |
| 1120 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid WebP Lossless version\n"); | |
| 1121 | ✗ | return AVERROR_INVALIDDATA; | |
| 1122 | } | ||
| 1123 | } else { | ||
| 1124 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if (!s->width || !s->height) |
| 1125 | ✗ | return AVERROR_BUG; | |
| 1126 | 2 | w = s->width; | |
| 1127 | 2 | h = s->height; | |
| 1128 | } | ||
| 1129 | |||
| 1130 | /* parse transformations */ | ||
| 1131 | 12 | s->nb_transforms = 0; | |
| 1132 | 12 | s->reduced_width = s->width; | |
| 1133 | 12 | used = 0; | |
| 1134 |
2/2✓ Branch 1 taken 22 times.
✓ Branch 2 taken 12 times.
|
34 | while (get_bits1(&s->gb)) { |
| 1135 | 22 | enum TransformType transform = get_bits(&s->gb, 2); | |
| 1136 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (used & (1 << transform)) { |
| 1137 | ✗ | av_log(avctx, AV_LOG_ERROR, "Transform %d used more than once\n", | |
| 1138 | transform); | ||
| 1139 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 1140 | ✗ | goto free_and_return; | |
| 1141 | } | ||
| 1142 | 22 | used |= (1 << transform); | |
| 1143 | 22 | s->transforms[s->nb_transforms++] = transform; | |
| 1144 |
4/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 4 times.
|
22 | switch (transform) { |
| 1145 | 6 | case PREDICTOR_TRANSFORM: | |
| 1146 | 6 | ret = parse_transform_predictor(s); | |
| 1147 | 6 | break; | |
| 1148 | 4 | case COLOR_TRANSFORM: | |
| 1149 | 4 | ret = parse_transform_color(s); | |
| 1150 | 4 | break; | |
| 1151 | 8 | case COLOR_INDEXING_TRANSFORM: | |
| 1152 | 8 | ret = parse_transform_color_indexing(s); | |
| 1153 | 8 | break; | |
| 1154 | } | ||
| 1155 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (ret < 0) |
| 1156 | ✗ | goto free_and_return; | |
| 1157 | } | ||
| 1158 | |||
| 1159 | /* decode primary image */ | ||
| 1160 | 12 | s->image[IMAGE_ROLE_ARGB].frame = p; | |
| 1161 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
|
12 | if (is_alpha_chunk) |
| 1162 | 2 | s->image[IMAGE_ROLE_ARGB].is_alpha_primary = 1; | |
| 1163 | 12 | ret = decode_entropy_coded_image(s, IMAGE_ROLE_ARGB, w, h); | |
| 1164 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (ret < 0) |
| 1165 | ✗ | goto free_and_return; | |
| 1166 | |||
| 1167 | /* apply transformations */ | ||
| 1168 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 12 times.
|
34 | for (i = s->nb_transforms - 1; i >= 0; i--) { |
| 1169 |
4/5✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
|
22 | switch (s->transforms[i]) { |
| 1170 | 6 | case PREDICTOR_TRANSFORM: | |
| 1171 | 6 | ret = apply_predictor_transform(s); | |
| 1172 | 6 | break; | |
| 1173 | 4 | case COLOR_TRANSFORM: | |
| 1174 | 4 | ret = apply_color_transform(s); | |
| 1175 | 4 | break; | |
| 1176 | 4 | case SUBTRACT_GREEN: | |
| 1177 | 4 | ret = apply_subtract_green_transform(s); | |
| 1178 | 4 | break; | |
| 1179 | 8 | case COLOR_INDEXING_TRANSFORM: | |
| 1180 | 8 | ret = apply_color_indexing_transform(s); | |
| 1181 | 8 | break; | |
| 1182 | } | ||
| 1183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (ret < 0) |
| 1184 | ✗ | goto free_and_return; | |
| 1185 | } | ||
| 1186 | |||
| 1187 | 12 | *got_frame = 1; | |
| 1188 | 12 | p->pict_type = AV_PICTURE_TYPE_I; | |
| 1189 | 12 | p->flags |= AV_FRAME_FLAG_KEY; | |
| 1190 | 12 | p->flags |= AV_FRAME_FLAG_LOSSLESS; | |
| 1191 | 12 | ret = data_size; | |
| 1192 | |||
| 1193 | 12 | free_and_return: | |
| 1194 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 12 times.
|
72 | for (i = 0; i < IMAGE_ROLE_NB; i++) |
| 1195 | 60 | image_ctx_free(&s->image[i]); | |
| 1196 | |||
| 1197 | 12 | return ret; | |
| 1198 | } | ||
| 1199 | |||
| 1200 | ✗ | static void alpha_inverse_prediction(AVFrame *frame, enum AlphaFilter m) | |
| 1201 | { | ||
| 1202 | int x, y, ls; | ||
| 1203 | uint8_t *dec; | ||
| 1204 | |||
| 1205 | ✗ | ls = frame->linesize[3]; | |
| 1206 | |||
| 1207 | /* filter first row using horizontal filter */ | ||
| 1208 | ✗ | dec = frame->data[3] + 1; | |
| 1209 | ✗ | for (x = 1; x < frame->width; x++, dec++) | |
| 1210 | ✗ | *dec += *(dec - 1); | |
| 1211 | |||
| 1212 | /* filter first column using vertical filter */ | ||
| 1213 | ✗ | dec = frame->data[3] + ls; | |
| 1214 | ✗ | for (y = 1; y < frame->height; y++, dec += ls) | |
| 1215 | ✗ | *dec += *(dec - ls); | |
| 1216 | |||
| 1217 | /* filter the rest using the specified filter */ | ||
| 1218 | ✗ | switch (m) { | |
| 1219 | ✗ | case ALPHA_FILTER_HORIZONTAL: | |
| 1220 | ✗ | for (y = 1; y < frame->height; y++) { | |
| 1221 | ✗ | dec = frame->data[3] + y * ls + 1; | |
| 1222 | ✗ | for (x = 1; x < frame->width; x++, dec++) | |
| 1223 | ✗ | *dec += *(dec - 1); | |
| 1224 | } | ||
| 1225 | ✗ | break; | |
| 1226 | ✗ | case ALPHA_FILTER_VERTICAL: | |
| 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 - ls); | |
| 1231 | } | ||
| 1232 | ✗ | break; | |
| 1233 | ✗ | case ALPHA_FILTER_GRADIENT: | |
| 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[0] += av_clip_uint8(*(dec - 1) + *(dec - ls) - *(dec - ls - 1)); | |
| 1238 | } | ||
| 1239 | ✗ | break; | |
| 1240 | } | ||
| 1241 | ✗ | } | |
| 1242 | |||
| 1243 | 2 | static int vp8_lossy_decode_alpha(AVCodecContext *avctx, AVFrame *p, | |
| 1244 | const uint8_t *data_start, | ||
| 1245 | unsigned int data_size) | ||
| 1246 | { | ||
| 1247 | 2 | WebPContext *s = avctx->priv_data; | |
| 1248 | int x, y, ret; | ||
| 1249 | |||
| 1250 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (s->alpha_compression == ALPHA_COMPRESSION_NONE) { |
| 1251 | GetByteContext gb; | ||
| 1252 | |||
| 1253 | ✗ | bytestream2_init(&gb, data_start, data_size); | |
| 1254 | ✗ | for (y = 0; y < s->height; y++) | |
| 1255 | ✗ | bytestream2_get_buffer(&gb, p->data[3] + p->linesize[3] * y, | |
| 1256 | ✗ | s->width); | |
| 1257 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | } else if (s->alpha_compression == ALPHA_COMPRESSION_VP8L) { |
| 1258 | uint8_t *ap, *pp; | ||
| 1259 | 2 | int alpha_got_frame = 0; | |
| 1260 | |||
| 1261 | 2 | s->alpha_frame = av_frame_alloc(); | |
| 1262 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!s->alpha_frame) |
| 1263 | ✗ | return AVERROR(ENOMEM); | |
| 1264 | |||
| 1265 | 2 | ret = vp8_lossless_decode_frame(avctx, s->alpha_frame, &alpha_got_frame, | |
| 1266 | data_start, data_size, 1); | ||
| 1267 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) { |
| 1268 | ✗ | av_frame_free(&s->alpha_frame); | |
| 1269 | ✗ | return ret; | |
| 1270 | } | ||
| 1271 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!alpha_got_frame) { |
| 1272 | ✗ | av_frame_free(&s->alpha_frame); | |
| 1273 | ✗ | return AVERROR_INVALIDDATA; | |
| 1274 | } | ||
| 1275 | |||
| 1276 | /* copy green component of alpha image to alpha plane of primary image */ | ||
| 1277 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
|
18 | for (y = 0; y < s->height; y++) { |
| 1278 | 16 | ap = GET_PIXEL(s->alpha_frame, 0, y) + 2; | |
| 1279 | 16 | pp = p->data[3] + p->linesize[3] * y; | |
| 1280 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 16 times.
|
208 | for (x = 0; x < s->width; x++) { |
| 1281 | 192 | *pp = *ap; | |
| 1282 | 192 | pp++; | |
| 1283 | 192 | ap += 4; | |
| 1284 | } | ||
| 1285 | } | ||
| 1286 | 2 | av_frame_free(&s->alpha_frame); | |
| 1287 | } | ||
| 1288 | |||
| 1289 | /* apply alpha filtering */ | ||
| 1290 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (s->alpha_filter) |
| 1291 | ✗ | alpha_inverse_prediction(p, s->alpha_filter); | |
| 1292 | |||
| 1293 | 2 | return 0; | |
| 1294 | } | ||
| 1295 | |||
| 1296 | 6 | static int vp8_lossy_decode_frame(AVCodecContext *avctx, AVFrame *p, | |
| 1297 | int *got_frame, uint8_t *data_start, | ||
| 1298 | unsigned int data_size) | ||
| 1299 | { | ||
| 1300 | 6 | WebPContext *s = avctx->priv_data; | |
| 1301 | int ret; | ||
| 1302 | |||
| 1303 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (!s->initialized) { |
| 1304 | 6 | ff_vp8_decode_init(avctx); | |
| 1305 | 6 | s->initialized = 1; | |
| 1306 | 6 | s->v.actually_webp = 1; | |
| 1307 | } | ||
| 1308 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | avctx->pix_fmt = s->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P; |
| 1309 | |||
| 1310 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (data_size > INT_MAX) { |
| 1311 | ✗ | av_log(avctx, AV_LOG_ERROR, "unsupported chunk size\n"); | |
| 1312 | ✗ | return AVERROR_PATCHWELCOME; | |
| 1313 | } | ||
| 1314 | |||
| 1315 | 6 | av_packet_unref(s->pkt); | |
| 1316 | 6 | s->pkt->data = data_start; | |
| 1317 | 6 | s->pkt->size = data_size; | |
| 1318 | |||
| 1319 | 6 | ret = ff_vp8_decode_frame(avctx, p, got_frame, s->pkt); | |
| 1320 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (ret < 0) |
| 1321 | ✗ | return ret; | |
| 1322 | |||
| 1323 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!*got_frame) |
| 1324 | ✗ | return AVERROR_INVALIDDATA; | |
| 1325 | |||
| 1326 | 6 | update_canvas_size(avctx, avctx->width, avctx->height); | |
| 1327 | |||
| 1328 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | if (s->has_alpha) { |
| 1329 | 2 | ret = vp8_lossy_decode_alpha(avctx, p, s->alpha_data, | |
| 1330 | 2 | s->alpha_data_size); | |
| 1331 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
| 1332 | ✗ | return ret; | |
| 1333 | } | ||
| 1334 | 6 | return ret; | |
| 1335 | } | ||
| 1336 | |||
| 1337 | 16 | static int webp_decode_frame(AVCodecContext *avctx, AVFrame *p, | |
| 1338 | int *got_frame, AVPacket *avpkt) | ||
| 1339 | { | ||
| 1340 | 16 | WebPContext *s = avctx->priv_data; | |
| 1341 | GetByteContext gb; | ||
| 1342 | int ret; | ||
| 1343 | uint32_t chunk_type, chunk_size; | ||
| 1344 | 16 | int vp8x_flags = 0; | |
| 1345 | |||
| 1346 | 16 | s->avctx = avctx; | |
| 1347 | 16 | s->width = 0; | |
| 1348 | 16 | s->height = 0; | |
| 1349 | 16 | *got_frame = 0; | |
| 1350 | 16 | s->has_alpha = 0; | |
| 1351 | 16 | s->has_exif = 0; | |
| 1352 | 16 | s->has_iccp = 0; | |
| 1353 | 16 | bytestream2_init(&gb, avpkt->data, avpkt->size); | |
| 1354 | |||
| 1355 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
|
16 | if (bytestream2_get_bytes_left(&gb) < 12) |
| 1356 | ✗ | return AVERROR_INVALIDDATA; | |
| 1357 | |||
| 1358 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
|
16 | if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) { |
| 1359 | ✗ | av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n"); | |
| 1360 | ✗ | return AVERROR_INVALIDDATA; | |
| 1361 | } | ||
| 1362 | |||
| 1363 | 16 | chunk_size = bytestream2_get_le32(&gb); | |
| 1364 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
|
16 | if (bytestream2_get_bytes_left(&gb) < chunk_size) |
| 1365 | ✗ | return AVERROR_INVALIDDATA; | |
| 1366 | |||
| 1367 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
|
16 | if (bytestream2_get_le32(&gb) != MKTAG('W', 'E', 'B', 'P')) { |
| 1368 | ✗ | av_log(avctx, AV_LOG_ERROR, "missing WEBP tag\n"); | |
| 1369 | ✗ | return AVERROR_INVALIDDATA; | |
| 1370 | } | ||
| 1371 | |||
| 1372 |
2/2✓ Branch 1 taken 24 times.
✓ Branch 2 taken 16 times.
|
40 | while (bytestream2_get_bytes_left(&gb) > 8) { |
| 1373 | 24 | chunk_type = bytestream2_get_le32(&gb); | |
| 1374 | 24 | chunk_size = bytestream2_get_le32(&gb); | |
| 1375 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (chunk_size == UINT32_MAX) |
| 1376 | ✗ | return AVERROR_INVALIDDATA; | |
| 1377 | 24 | chunk_size += chunk_size & 1; | |
| 1378 | |||
| 1379 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
|
24 | if (bytestream2_get_bytes_left(&gb) < chunk_size) { |
| 1380 | /* we seem to be running out of data, but it could also be that the | ||
| 1381 | bitstream has trailing junk leading to bogus chunk_size. */ | ||
| 1382 | ✗ | break; | |
| 1383 | } | ||
| 1384 | |||
| 1385 |
5/8✓ 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.
|
24 | switch (chunk_type) { |
| 1386 | 6 | case MKTAG('V', 'P', '8', ' '): | |
| 1387 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (!*got_frame) { |
| 1388 | 6 | ret = vp8_lossy_decode_frame(avctx, p, got_frame, | |
| 1389 | 6 | avpkt->data + bytestream2_tell(&gb), | |
| 1390 | chunk_size); | ||
| 1391 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (ret < 0) |
| 1392 | ✗ | return ret; | |
| 1393 | } | ||
| 1394 | 6 | bytestream2_skip(&gb, chunk_size); | |
| 1395 | 24 | break; | |
| 1396 | 10 | case MKTAG('V', 'P', '8', 'L'): | |
| 1397 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (!*got_frame) { |
| 1398 | 10 | ret = vp8_lossless_decode_frame(avctx, p, got_frame, | |
| 1399 | 10 | avpkt->data + bytestream2_tell(&gb), | |
| 1400 | chunk_size, 0); | ||
| 1401 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (ret < 0) |
| 1402 | ✗ | return ret; | |
| 1403 | #if FF_API_CODEC_PROPS | ||
| 1404 | FF_DISABLE_DEPRECATION_WARNINGS | ||
| 1405 | 10 | avctx->properties |= FF_CODEC_PROPERTY_LOSSLESS; | |
| 1406 | FF_ENABLE_DEPRECATION_WARNINGS | ||
| 1407 | #endif | ||
| 1408 | } | ||
| 1409 | 10 | bytestream2_skip(&gb, chunk_size); | |
| 1410 | 10 | break; | |
| 1411 | 4 | case MKTAG('V', 'P', '8', 'X'): | |
| 1412 |
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) { |
| 1413 | ✗ | av_log(avctx, AV_LOG_ERROR, "Canvas dimensions are already set\n"); | |
| 1414 | ✗ | return AVERROR_INVALIDDATA; | |
| 1415 | } | ||
| 1416 | 4 | vp8x_flags = bytestream2_get_byte(&gb); | |
| 1417 | 4 | bytestream2_skip(&gb, 3); | |
| 1418 | 4 | s->width = bytestream2_get_le24(&gb) + 1; | |
| 1419 | 4 | s->height = bytestream2_get_le24(&gb) + 1; | |
| 1420 | 4 | ret = av_image_check_size(s->width, s->height, 0, avctx); | |
| 1421 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
| 1422 | ✗ | return ret; | |
| 1423 | 4 | break; | |
| 1424 | 2 | case MKTAG('A', 'L', 'P', 'H'): { | |
| 1425 | int alpha_header, filter_m, compression; | ||
| 1426 | |||
| 1427 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!(vp8x_flags & VP8X_FLAG_ALPHA)) { |
| 1428 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
| 1429 | "ALPHA chunk present, but alpha bit not set in the " | ||
| 1430 | "VP8X header\n"); | ||
| 1431 | } | ||
| 1432 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (chunk_size == 0) { |
| 1433 | ✗ | av_log(avctx, AV_LOG_ERROR, "invalid ALPHA chunk size\n"); | |
| 1434 | ✗ | return AVERROR_INVALIDDATA; | |
| 1435 | } | ||
| 1436 | 2 | alpha_header = bytestream2_get_byte(&gb); | |
| 1437 | 2 | s->alpha_data = avpkt->data + bytestream2_tell(&gb); | |
| 1438 | 2 | s->alpha_data_size = chunk_size - 1; | |
| 1439 | 2 | bytestream2_skip(&gb, s->alpha_data_size); | |
| 1440 | |||
| 1441 | 2 | filter_m = (alpha_header >> 2) & 0x03; | |
| 1442 | 2 | compression = alpha_header & 0x03; | |
| 1443 | |||
| 1444 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (compression > ALPHA_COMPRESSION_VP8L) { |
| 1445 | ✗ | av_log(avctx, AV_LOG_VERBOSE, | |
| 1446 | "skipping unsupported ALPHA chunk\n"); | ||
| 1447 | } else { | ||
| 1448 | 2 | s->has_alpha = 1; | |
| 1449 | 2 | s->alpha_compression = compression; | |
| 1450 | 2 | s->alpha_filter = filter_m; | |
| 1451 | } | ||
| 1452 | |||
| 1453 | 2 | break; | |
| 1454 | } | ||
| 1455 | 2 | case MKTAG('E', 'X', 'I', 'F'): { | |
| 1456 | 2 | AVBufferRef *exif_buf = NULL; | |
| 1457 | |||
| 1458 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (s->has_exif) { |
| 1459 | ✗ | av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra EXIF chunk\n"); | |
| 1460 | ✗ | goto exif_end; | |
| 1461 | } | ||
| 1462 | |||
| 1463 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!(vp8x_flags & VP8X_FLAG_EXIF_METADATA)) |
| 1464 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
| 1465 | "EXIF chunk present, but Exif bit not set in the " | ||
| 1466 | "VP8X header\n"); | ||
| 1467 | |||
| 1468 | 2 | exif_buf = av_buffer_alloc(chunk_size); | |
| 1469 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!exif_buf) { |
| 1470 | ✗ | av_log(avctx, AV_LOG_WARNING, "unable to allocate EXIF buffer\n"); | |
| 1471 | ✗ | goto exif_end; | |
| 1472 | } | ||
| 1473 | 2 | s->has_exif = 1; | |
| 1474 | 2 | memcpy(exif_buf->data, gb.buffer, chunk_size); | |
| 1475 | |||
| 1476 | 2 | ret = ff_decode_exif_attach_buffer(avctx, p, &exif_buf, AV_EXIF_TIFF_HEADER); | |
| 1477 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (ret < 0) |
| 1478 | ✗ | av_log(avctx, AV_LOG_WARNING, "unable to attach EXIF buffer\n"); | |
| 1479 | |||
| 1480 | 2 | exif_end: | |
| 1481 | 2 | bytestream2_skip(&gb, chunk_size); | |
| 1482 | 2 | break; | |
| 1483 | } | ||
| 1484 | ✗ | case MKTAG('I', 'C', 'C', 'P'): { | |
| 1485 | AVFrameSideData *sd; | ||
| 1486 | |||
| 1487 | ✗ | if (s->has_iccp) { | |
| 1488 | ✗ | av_log(avctx, AV_LOG_VERBOSE, "Ignoring extra ICCP chunk\n"); | |
| 1489 | ✗ | bytestream2_skip(&gb, chunk_size); | |
| 1490 | ✗ | break; | |
| 1491 | } | ||
| 1492 | ✗ | if (!(vp8x_flags & VP8X_FLAG_ICC)) | |
| 1493 | ✗ | av_log(avctx, AV_LOG_WARNING, | |
| 1494 | "ICCP chunk present, but ICC Profile bit not set in the " | ||
| 1495 | "VP8X header\n"); | ||
| 1496 | |||
| 1497 | ✗ | s->has_iccp = 1; | |
| 1498 | |||
| 1499 | ✗ | ret = ff_frame_new_side_data(avctx, p, AV_FRAME_DATA_ICC_PROFILE, chunk_size, &sd); | |
| 1500 | ✗ | if (ret < 0) | |
| 1501 | ✗ | return ret; | |
| 1502 | |||
| 1503 | ✗ | if (sd) { | |
| 1504 | ✗ | bytestream2_get_buffer(&gb, sd->data, chunk_size); | |
| 1505 | } else { | ||
| 1506 | ✗ | bytestream2_skip(&gb, chunk_size); | |
| 1507 | } | ||
| 1508 | ✗ | break; | |
| 1509 | } | ||
| 1510 | ✗ | case MKTAG('A', 'N', 'I', 'M'): | |
| 1511 | case MKTAG('A', 'N', 'M', 'F'): | ||
| 1512 | case MKTAG('X', 'M', 'P', ' '): | ||
| 1513 | ✗ | av_log(avctx, AV_LOG_WARNING, "skipping unsupported chunk: %s\n", | |
| 1514 | ✗ | av_fourcc2str(chunk_type)); | |
| 1515 | ✗ | bytestream2_skip(&gb, chunk_size); | |
| 1516 | ✗ | break; | |
| 1517 | ✗ | default: | |
| 1518 | ✗ | av_log(avctx, AV_LOG_VERBOSE, "skipping unknown chunk: %s\n", | |
| 1519 | ✗ | av_fourcc2str(chunk_type)); | |
| 1520 | ✗ | bytestream2_skip(&gb, chunk_size); | |
| 1521 | ✗ | break; | |
| 1522 | } | ||
| 1523 | } | ||
| 1524 | |||
| 1525 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (!*got_frame) { |
| 1526 | ✗ | av_log(avctx, AV_LOG_ERROR, "image data not found\n"); | |
| 1527 | ✗ | return AVERROR_INVALIDDATA; | |
| 1528 | } | ||
| 1529 | |||
| 1530 | 16 | return avpkt->size; | |
| 1531 | } | ||
| 1532 | |||
| 1533 | 16 | static av_cold int webp_decode_init(AVCodecContext *avctx) | |
| 1534 | { | ||
| 1535 | 16 | WebPContext *s = avctx->priv_data; | |
| 1536 | |||
| 1537 | 16 | s->pkt = av_packet_alloc(); | |
| 1538 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (!s->pkt) |
| 1539 | ✗ | return AVERROR(ENOMEM); | |
| 1540 | |||
| 1541 | 16 | return 0; | |
| 1542 | } | ||
| 1543 | |||
| 1544 | 16 | static av_cold int webp_decode_close(AVCodecContext *avctx) | |
| 1545 | { | ||
| 1546 | 16 | WebPContext *s = avctx->priv_data; | |
| 1547 | |||
| 1548 | 16 | av_packet_free(&s->pkt); | |
| 1549 | |||
| 1550 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 10 times.
|
16 | if (s->initialized) |
| 1551 | 6 | return ff_vp8_decode_free(avctx); | |
| 1552 | |||
| 1553 | 10 | return 0; | |
| 1554 | } | ||
| 1555 | |||
| 1556 | const FFCodec ff_webp_decoder = { | ||
| 1557 | .p.name = "webp", | ||
| 1558 | CODEC_LONG_NAME("WebP image"), | ||
| 1559 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 1560 | .p.id = AV_CODEC_ID_WEBP, | ||
| 1561 | .priv_data_size = sizeof(WebPContext), | ||
| 1562 | .init = webp_decode_init, | ||
| 1563 | FF_CODEC_DECODE_CB(webp_decode_frame), | ||
| 1564 | .close = webp_decode_close, | ||
| 1565 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, | ||
| 1566 | .caps_internal = FF_CODEC_CAP_ICC_PROFILES | | ||
| 1567 | FF_CODEC_CAP_USES_PROGRESSFRAMES, | ||
| 1568 | }; | ||
| 1569 |