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