Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2006 Konstantin Shishkov | ||
3 | * | ||
4 | * This file is part of FFmpeg. | ||
5 | * | ||
6 | * FFmpeg is free software; you can redistribute it and/or | ||
7 | * modify it under the terms of the GNU Lesser General Public | ||
8 | * License as published by the Free Software Foundation; either | ||
9 | * version 2.1 of the License, or (at your option) any later version. | ||
10 | * | ||
11 | * FFmpeg is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | * Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with FFmpeg; if not, write to the Free Software | ||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file | ||
23 | * TIFF image decoder | ||
24 | * @author Konstantin Shishkov | ||
25 | */ | ||
26 | |||
27 | #include "config.h" | ||
28 | #if CONFIG_ZLIB | ||
29 | #include <zlib.h> | ||
30 | #endif | ||
31 | #if CONFIG_LZMA | ||
32 | #define LZMA_API_STATIC | ||
33 | #include <lzma.h> | ||
34 | #endif | ||
35 | |||
36 | #include <float.h> | ||
37 | |||
38 | #include "libavutil/attributes.h" | ||
39 | #include "libavutil/avstring.h" | ||
40 | #include "libavutil/error.h" | ||
41 | #include "libavutil/intreadwrite.h" | ||
42 | #include "libavutil/mem.h" | ||
43 | #include "libavutil/opt.h" | ||
44 | #include "libavutil/reverse.h" | ||
45 | #include "avcodec.h" | ||
46 | #include "bytestream.h" | ||
47 | #include "codec_internal.h" | ||
48 | #include "decode.h" | ||
49 | #include "faxcompr.h" | ||
50 | #include "lzw.h" | ||
51 | #include "tiff.h" | ||
52 | #include "tiff_common.h" | ||
53 | #include "tiff_data.h" | ||
54 | #include "mjpegdec.h" | ||
55 | #include "thread.h" | ||
56 | #include "get_bits.h" | ||
57 | |||
58 | typedef struct TiffContext { | ||
59 | AVClass *class; | ||
60 | AVCodecContext *avctx; | ||
61 | GetByteContext gb; | ||
62 | |||
63 | /* JPEG decoding for DNG */ | ||
64 | AVCodecContext *avctx_mjpeg; // wrapper context for MJPEG | ||
65 | AVPacket *jpkt; // encoded JPEG tile | ||
66 | AVFrame *jpgframe; // decoded JPEG tile | ||
67 | |||
68 | int get_subimage; | ||
69 | uint16_t get_page; | ||
70 | int get_thumbnail; | ||
71 | |||
72 | enum TiffType tiff_type; | ||
73 | int width, height; | ||
74 | unsigned int bpp, bppcount; | ||
75 | uint32_t palette[256]; | ||
76 | int palette_is_set; | ||
77 | int le; | ||
78 | enum TiffCompr compr; | ||
79 | enum TiffPhotometric photometric; | ||
80 | int planar; | ||
81 | int subsampling[2]; | ||
82 | int fax_opts; | ||
83 | int predictor; | ||
84 | int fill_order; | ||
85 | uint32_t res[4]; | ||
86 | int is_thumbnail; | ||
87 | unsigned last_tag; | ||
88 | |||
89 | int is_bayer; | ||
90 | int use_color_matrix; | ||
91 | uint8_t pattern[4]; | ||
92 | |||
93 | float analog_balance[4]; | ||
94 | float as_shot_neutral[4]; | ||
95 | float as_shot_white[4]; | ||
96 | float color_matrix[3][4]; | ||
97 | float camera_calibration[4][4]; | ||
98 | float premultiply[4]; | ||
99 | float black_level[4]; | ||
100 | |||
101 | unsigned white_level; | ||
102 | uint16_t dng_lut[65536]; | ||
103 | |||
104 | uint32_t sub_ifd; | ||
105 | uint16_t cur_page; | ||
106 | |||
107 | int strips, rps, sstype; | ||
108 | int sot; | ||
109 | int stripsizesoff, stripsize, stripoff, strippos; | ||
110 | LZWState *lzw; | ||
111 | |||
112 | /* Tile support */ | ||
113 | int is_tiled; | ||
114 | int tile_byte_counts_offset, tile_offsets_offset; | ||
115 | int tile_width, tile_length; | ||
116 | |||
117 | int is_jpeg; | ||
118 | |||
119 | uint8_t *deinvert_buf; | ||
120 | int deinvert_buf_size; | ||
121 | uint8_t *yuv_line; | ||
122 | unsigned int yuv_line_size; | ||
123 | |||
124 | int geotag_count; | ||
125 | TiffGeoTag *geotags; | ||
126 | } TiffContext; | ||
127 | |||
128 | static const float d65_white[3] = { 0.950456f, 1.f, 1.088754f }; | ||
129 | |||
130 | ✗ | static void tiff_set_type(TiffContext *s, enum TiffType tiff_type) { | |
131 | ✗ | if (s->tiff_type < tiff_type) // Prioritize higher-valued entries | |
132 | ✗ | s->tiff_type = tiff_type; | |
133 | ✗ | } | |
134 | |||
135 | 54 | static void free_geotags(TiffContext *const s) | |
136 | { | ||
137 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
|
54 | for (int i = 0; i < s->geotag_count; i++) |
138 | ✗ | av_freep(&s->geotags[i].val); | |
139 | 54 | av_freep(&s->geotags); | |
140 | 54 | s->geotag_count = 0; | |
141 | 54 | } | |
142 | |||
143 | ✗ | static const char *get_geokey_name(int key) | |
144 | { | ||
145 | #define RET_GEOKEY_STR(TYPE, array)\ | ||
146 | if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\ | ||
147 | key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(tiff_##array##_name_type_map))\ | ||
148 | return tiff_##array##_name_type_string + tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].offset; | ||
149 | |||
150 | ✗ | RET_GEOKEY_STR(VERT, vert); | |
151 | ✗ | RET_GEOKEY_STR(PROJ, proj); | |
152 | ✗ | RET_GEOKEY_STR(GEOG, geog); | |
153 | ✗ | RET_GEOKEY_STR(CONF, conf); | |
154 | |||
155 | ✗ | return NULL; | |
156 | } | ||
157 | |||
158 | ✗ | static int get_geokey_type(int key) | |
159 | { | ||
160 | #define RET_GEOKEY_TYPE(TYPE, array)\ | ||
161 | if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\ | ||
162 | key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(tiff_##array##_name_type_map))\ | ||
163 | return tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].type; | ||
164 | ✗ | RET_GEOKEY_TYPE(VERT, vert); | |
165 | ✗ | RET_GEOKEY_TYPE(PROJ, proj); | |
166 | ✗ | RET_GEOKEY_TYPE(GEOG, geog); | |
167 | ✗ | RET_GEOKEY_TYPE(CONF, conf); | |
168 | |||
169 | ✗ | return AVERROR_INVALIDDATA; | |
170 | } | ||
171 | |||
172 | ✗ | static int cmp_id_key(const void *id, const void *k) | |
173 | { | ||
174 | ✗ | return *(const int*)id - ((const TiffGeoTagKeyName*)k)->key; | |
175 | } | ||
176 | |||
177 | ✗ | static const char *search_keyval(const TiffGeoTagKeyName *keys, int n, int id) | |
178 | { | ||
179 | ✗ | const TiffGeoTagKeyName *r = bsearch(&id, keys, n, sizeof(keys[0]), cmp_id_key); | |
180 | ✗ | if(r) | |
181 | ✗ | return r->name; | |
182 | |||
183 | ✗ | return NULL; | |
184 | } | ||
185 | |||
186 | ✗ | static const char *get_geokey_val(int key, uint16_t val) | |
187 | { | ||
188 | ✗ | if (val == TIFF_GEO_KEY_UNDEFINED) | |
189 | ✗ | return "undefined"; | |
190 | ✗ | if (val == TIFF_GEO_KEY_USER_DEFINED) | |
191 | ✗ | return "User-Defined"; | |
192 | |||
193 | #define RET_GEOKEY_VAL(TYPE, array)\ | ||
194 | if (val >= TIFF_##TYPE##_OFFSET &&\ | ||
195 | val - TIFF_##TYPE##_OFFSET < FF_ARRAY_ELEMS(tiff_##array##_codes))\ | ||
196 | return tiff_##array##_codes[val - TIFF_##TYPE##_OFFSET]; | ||
197 | |||
198 | ✗ | switch (key) { | |
199 | ✗ | case TIFF_GT_MODEL_TYPE_GEOKEY: | |
200 | ✗ | RET_GEOKEY_VAL(GT_MODEL_TYPE, gt_model_type); | |
201 | ✗ | break; | |
202 | ✗ | case TIFF_GT_RASTER_TYPE_GEOKEY: | |
203 | ✗ | RET_GEOKEY_VAL(GT_RASTER_TYPE, gt_raster_type); | |
204 | ✗ | break; | |
205 | ✗ | case TIFF_GEOG_LINEAR_UNITS_GEOKEY: | |
206 | case TIFF_PROJ_LINEAR_UNITS_GEOKEY: | ||
207 | case TIFF_VERTICAL_UNITS_GEOKEY: | ||
208 | ✗ | RET_GEOKEY_VAL(LINEAR_UNIT, linear_unit); | |
209 | ✗ | break; | |
210 | ✗ | case TIFF_GEOG_ANGULAR_UNITS_GEOKEY: | |
211 | case TIFF_GEOG_AZIMUTH_UNITS_GEOKEY: | ||
212 | ✗ | RET_GEOKEY_VAL(ANGULAR_UNIT, angular_unit); | |
213 | ✗ | break; | |
214 | ✗ | case TIFF_GEOGRAPHIC_TYPE_GEOKEY: | |
215 | ✗ | RET_GEOKEY_VAL(GCS_TYPE, gcs_type); | |
216 | ✗ | RET_GEOKEY_VAL(GCSE_TYPE, gcse_type); | |
217 | ✗ | break; | |
218 | ✗ | case TIFF_GEOG_GEODETIC_DATUM_GEOKEY: | |
219 | ✗ | RET_GEOKEY_VAL(GEODETIC_DATUM, geodetic_datum); | |
220 | ✗ | RET_GEOKEY_VAL(GEODETIC_DATUM_E, geodetic_datum_e); | |
221 | ✗ | break; | |
222 | ✗ | case TIFF_GEOG_ELLIPSOID_GEOKEY: | |
223 | ✗ | RET_GEOKEY_VAL(ELLIPSOID, ellipsoid); | |
224 | ✗ | break; | |
225 | ✗ | case TIFF_GEOG_PRIME_MERIDIAN_GEOKEY: | |
226 | ✗ | RET_GEOKEY_VAL(PRIME_MERIDIAN, prime_meridian); | |
227 | ✗ | break; | |
228 | ✗ | case TIFF_PROJECTED_CS_TYPE_GEOKEY: | |
229 | ✗ | return search_keyval(tiff_proj_cs_type_codes, FF_ARRAY_ELEMS(tiff_proj_cs_type_codes), val); | |
230 | ✗ | case TIFF_PROJECTION_GEOKEY: | |
231 | ✗ | return search_keyval(tiff_projection_codes, FF_ARRAY_ELEMS(tiff_projection_codes), val); | |
232 | ✗ | case TIFF_PROJ_COORD_TRANS_GEOKEY: | |
233 | ✗ | RET_GEOKEY_VAL(COORD_TRANS, coord_trans); | |
234 | ✗ | break; | |
235 | ✗ | case TIFF_VERTICAL_CS_TYPE_GEOKEY: | |
236 | ✗ | RET_GEOKEY_VAL(VERT_CS, vert_cs); | |
237 | ✗ | RET_GEOKEY_VAL(ORTHO_VERT_CS, ortho_vert_cs); | |
238 | ✗ | break; | |
239 | |||
240 | } | ||
241 | |||
242 | ✗ | return NULL; | |
243 | } | ||
244 | |||
245 | ✗ | static char *doubles2str(double *dp, int count, const char *sep) | |
246 | { | ||
247 | int i; | ||
248 | char *ap, *ap0; | ||
249 | uint64_t component_len; | ||
250 | ✗ | if (!sep) sep = ", "; | |
251 | ✗ | component_len = 24LL + strlen(sep); | |
252 | ✗ | if (count >= (INT_MAX - 1)/component_len) | |
253 | ✗ | return NULL; | |
254 | ✗ | ap = av_malloc(component_len * count + 1); | |
255 | ✗ | if (!ap) | |
256 | ✗ | return NULL; | |
257 | ✗ | ap0 = ap; | |
258 | ✗ | ap[0] = '\0'; | |
259 | ✗ | for (i = 0; i < count; i++) { | |
260 | ✗ | unsigned l = snprintf(ap, component_len, "%.15g%s", dp[i], sep); | |
261 | ✗ | if(l >= component_len) { | |
262 | ✗ | av_free(ap0); | |
263 | ✗ | return NULL; | |
264 | } | ||
265 | ✗ | ap += l; | |
266 | } | ||
267 | ✗ | ap0[strlen(ap0) - strlen(sep)] = '\0'; | |
268 | ✗ | return ap0; | |
269 | } | ||
270 | |||
271 | 26 | static int add_metadata(int count, int type, | |
272 | const char *name, const char *sep, TiffContext *s, AVFrame *frame) | ||
273 | { | ||
274 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
|
26 | switch(type) { |
275 | ✗ | case TIFF_DOUBLE: return ff_tadd_doubles_metadata(count, name, sep, &s->gb, s->le, &frame->metadata); | |
276 | 12 | case TIFF_SHORT : return ff_tadd_shorts_metadata(count, name, sep, &s->gb, s->le, 0, &frame->metadata); | |
277 | 14 | case TIFF_STRING: return ff_tadd_string_metadata(count, name, &s->gb, s->le, &frame->metadata); | |
278 | ✗ | default : return AVERROR_INVALIDDATA; | |
279 | }; | ||
280 | } | ||
281 | |||
282 | /** | ||
283 | * Map stored raw sensor values into linear reference values (see: DNG Specification - Chapter 5) | ||
284 | */ | ||
285 | ✗ | static uint16_t av_always_inline dng_process_color16(uint16_t value, | |
286 | const uint16_t *lut, | ||
287 | float black_level, | ||
288 | float scale_factor) | ||
289 | { | ||
290 | float value_norm; | ||
291 | |||
292 | // Lookup table lookup | ||
293 | ✗ | value = lut[value]; | |
294 | |||
295 | // Black level subtraction | ||
296 | // Color scaling | ||
297 | ✗ | value_norm = ((float)value - black_level) * scale_factor; | |
298 | |||
299 | ✗ | value = av_clip_uint16(lrintf(value_norm)); | |
300 | |||
301 | ✗ | return value; | |
302 | } | ||
303 | |||
304 | ✗ | static uint16_t av_always_inline dng_process_color8(uint16_t value, | |
305 | const uint16_t *lut, | ||
306 | float black_level, | ||
307 | float scale_factor) | ||
308 | { | ||
309 | ✗ | return dng_process_color16(value, lut, black_level, scale_factor) >> 8; | |
310 | } | ||
311 | |||
312 | ✗ | static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int dst_stride, | |
313 | const uint8_t *src, int src_stride, int width, int height, | ||
314 | int is_single_comp, int is_u16, int odd_line) | ||
315 | { | ||
316 | float scale_factor[4]; | ||
317 | int line, col; | ||
318 | |||
319 | ✗ | if (s->is_bayer) { | |
320 | ✗ | for (int i = 0; i < 4; i++) | |
321 | ✗ | scale_factor[i] = s->premultiply[s->pattern[i]] * 65535.f / (s->white_level - s->black_level[i]); | |
322 | } else { | ||
323 | ✗ | for (int i = 0; i < 4; i++) | |
324 | ✗ | scale_factor[i] = s->premultiply[ i ] * 65535.f / (s->white_level - s->black_level[i]); | |
325 | } | ||
326 | |||
327 | ✗ | if (is_single_comp) { | |
328 | ✗ | if (!is_u16) | |
329 | ✗ | return; /* <= 8bpp unsupported */ | |
330 | |||
331 | /* Image is double the width and half the height we need, each row comprises 2 rows of the output | ||
332 | (split vertically in the middle). */ | ||
333 | ✗ | for (line = 0; line < height / 2; line++) { | |
334 | ✗ | uint16_t *dst_u16 = (uint16_t *)dst; | |
335 | ✗ | const uint16_t *src_u16 = (const uint16_t *)src; | |
336 | |||
337 | /* Blit first half of input row row to initial row of output */ | ||
338 | ✗ | for (col = 0; col < width; col++) | |
339 | ✗ | *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level[col&1], scale_factor[col&1]); | |
340 | |||
341 | /* Advance the destination pointer by a row (source pointer remains in the same place) */ | ||
342 | ✗ | dst += dst_stride * sizeof(uint16_t); | |
343 | ✗ | dst_u16 = (uint16_t *)dst; | |
344 | |||
345 | /* Blit second half of input row row to next row of output */ | ||
346 | ✗ | for (col = 0; col < width; col++) | |
347 | ✗ | *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level[(col&1) + 2], scale_factor[(col&1) + 2]); | |
348 | |||
349 | ✗ | dst += dst_stride * sizeof(uint16_t); | |
350 | ✗ | src += src_stride * sizeof(uint16_t); | |
351 | } | ||
352 | } else { | ||
353 | /* Input and output image are the same size and the MJpeg decoder has done per-component | ||
354 | deinterleaving, so blitting here is straightforward. */ | ||
355 | ✗ | if (is_u16) { | |
356 | ✗ | for (line = 0; line < height; line++) { | |
357 | ✗ | uint16_t *dst_u16 = (uint16_t *)dst; | |
358 | ✗ | const uint16_t *src_u16 = (const uint16_t *)src; | |
359 | |||
360 | ✗ | for (col = 0; col < width; col++) | |
361 | ✗ | *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, | |
362 | ✗ | s->black_level[(col&1) + 2 * ((line&1) + odd_line)], | |
363 | ✗ | scale_factor[(col&1) + 2 * ((line&1) + odd_line)]); | |
364 | |||
365 | ✗ | dst += dst_stride * sizeof(uint16_t); | |
366 | ✗ | src += src_stride * sizeof(uint16_t); | |
367 | } | ||
368 | } else { | ||
369 | ✗ | for (line = 0; line < height; line++) { | |
370 | ✗ | uint8_t *dst_u8 = dst; | |
371 | ✗ | const uint8_t *src_u8 = src; | |
372 | |||
373 | ✗ | for (col = 0; col < width; col++) | |
374 | ✗ | *dst_u8++ = dng_process_color8(*src_u8++, s->dng_lut, | |
375 | ✗ | s->black_level[(col&1) + 2 * ((line&1) + odd_line)], | |
376 | ✗ | scale_factor[(col&1) + 2 * ((line&1) + odd_line)]); | |
377 | |||
378 | ✗ | dst += dst_stride; | |
379 | ✗ | src += src_stride; | |
380 | } | ||
381 | } | ||
382 | } | ||
383 | } | ||
384 | |||
385 | 36484 | static void av_always_inline horizontal_fill(TiffContext *s, | |
386 | unsigned int bpp, uint8_t* dst, | ||
387 | int usePtr, const uint8_t *src, | ||
388 | uint8_t c, int width, int offset) | ||
389 | { | ||
390 |
1/5✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 36484 times.
|
36484 | switch (bpp) { |
391 | ✗ | case 1: | |
392 | ✗ | while (--width >= 0) { | |
393 | ✗ | dst[(width+offset)*8+7] = (usePtr ? src[width] : c) & 0x1; | |
394 | ✗ | dst[(width+offset)*8+6] = (usePtr ? src[width] : c) >> 1 & 0x1; | |
395 | ✗ | dst[(width+offset)*8+5] = (usePtr ? src[width] : c) >> 2 & 0x1; | |
396 | ✗ | dst[(width+offset)*8+4] = (usePtr ? src[width] : c) >> 3 & 0x1; | |
397 | ✗ | dst[(width+offset)*8+3] = (usePtr ? src[width] : c) >> 4 & 0x1; | |
398 | ✗ | dst[(width+offset)*8+2] = (usePtr ? src[width] : c) >> 5 & 0x1; | |
399 | ✗ | dst[(width+offset)*8+1] = (usePtr ? src[width] : c) >> 6 & 0x1; | |
400 | ✗ | dst[(width+offset)*8+0] = (usePtr ? src[width] : c) >> 7; | |
401 | } | ||
402 | ✗ | break; | |
403 | ✗ | case 2: | |
404 | ✗ | while (--width >= 0) { | |
405 | ✗ | dst[(width+offset)*4+3] = (usePtr ? src[width] : c) & 0x3; | |
406 | ✗ | dst[(width+offset)*4+2] = (usePtr ? src[width] : c) >> 2 & 0x3; | |
407 | ✗ | dst[(width+offset)*4+1] = (usePtr ? src[width] : c) >> 4 & 0x3; | |
408 | ✗ | dst[(width+offset)*4+0] = (usePtr ? src[width] : c) >> 6; | |
409 | } | ||
410 | ✗ | break; | |
411 | ✗ | case 4: | |
412 | ✗ | while (--width >= 0) { | |
413 | ✗ | dst[(width+offset)*2+1] = (usePtr ? src[width] : c) & 0xF; | |
414 | ✗ | dst[(width+offset)*2+0] = (usePtr ? src[width] : c) >> 4; | |
415 | } | ||
416 | ✗ | break; | |
417 | ✗ | case 10: | |
418 | case 12: | ||
419 | case 14: { | ||
420 | ✗ | uint16_t *dst16 = (uint16_t *)dst; | |
421 | ✗ | int is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG); | |
422 | ✗ | uint8_t shift = is_dng ? 0 : 16 - bpp; | |
423 | GetBitContext gb; | ||
424 | |||
425 | ✗ | av_unused int ret = init_get_bits8(&gb, src, width); | |
426 | av_assert1(ret >= 0); | ||
427 | ✗ | for (int i = 0; i < s->width; i++) { | |
428 | ✗ | dst16[i] = get_bits(&gb, bpp) << shift; | |
429 | } | ||
430 | } | ||
431 | ✗ | break; | |
432 | 36484 | default: | |
433 |
2/2✓ Branch 0 taken 34369 times.
✓ Branch 1 taken 2115 times.
|
36484 | if (usePtr) { |
434 | 34369 | memcpy(dst + offset, src, width); | |
435 | } else { | ||
436 | 2115 | memset(dst + offset, c, width); | |
437 | } | ||
438 | } | ||
439 | 36484 | } | |
440 | |||
441 | 18 | static int deinvert_buffer(TiffContext *s, const uint8_t *src, int size) | |
442 | { | ||
443 | int i; | ||
444 | |||
445 | 18 | av_fast_padded_malloc(&s->deinvert_buf, &s->deinvert_buf_size, size); | |
446 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if (!s->deinvert_buf) |
447 | ✗ | return AVERROR(ENOMEM); | |
448 |
2/2✓ Branch 0 taken 251119 times.
✓ Branch 1 taken 18 times.
|
251137 | for (i = 0; i < size; i++) |
449 | 251119 | s->deinvert_buf[i] = ff_reverse[src[i]]; | |
450 | |||
451 | 18 | return 0; | |
452 | } | ||
453 | |||
454 | ✗ | static void unpack_gray(TiffContext *s, AVFrame *p, | |
455 | const uint8_t *src, int lnum, int width, int bpp) | ||
456 | { | ||
457 | GetBitContext gb; | ||
458 | ✗ | uint16_t *dst = (uint16_t *)(p->data[0] + lnum * p->linesize[0]); | |
459 | |||
460 | ✗ | av_unused int ret = init_get_bits8(&gb, src, width); | |
461 | av_assert1(ret >= 0); | ||
462 | |||
463 | ✗ | for (int i = 0; i < s->width; i++) { | |
464 | ✗ | dst[i] = get_bits(&gb, bpp); | |
465 | } | ||
466 | ✗ | } | |
467 | |||
468 | ✗ | static void unpack_yuv(TiffContext *s, AVFrame *p, | |
469 | const uint8_t *src, int lnum) | ||
470 | { | ||
471 | int i, j, k; | ||
472 | ✗ | int w = (s->width - 1) / s->subsampling[0] + 1; | |
473 | ✗ | uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]]; | |
474 | ✗ | uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]]; | |
475 | ✗ | if (s->width % s->subsampling[0] || s->height % s->subsampling[1]) { | |
476 | ✗ | for (i = 0; i < w; i++) { | |
477 | ✗ | for (j = 0; j < s->subsampling[1]; j++) | |
478 | ✗ | for (k = 0; k < s->subsampling[0]; k++) | |
479 | ✗ | p->data[0][FFMIN(lnum + j, s->height-1) * p->linesize[0] + | |
480 | ✗ | FFMIN(i * s->subsampling[0] + k, s->width-1)] = *src++; | |
481 | ✗ | *pu++ = *src++; | |
482 | ✗ | *pv++ = *src++; | |
483 | } | ||
484 | }else{ | ||
485 | ✗ | for (i = 0; i < w; i++) { | |
486 | ✗ | for (j = 0; j < s->subsampling[1]; j++) | |
487 | ✗ | for (k = 0; k < s->subsampling[0]; k++) | |
488 | ✗ | p->data[0][(lnum + j) * p->linesize[0] + | |
489 | ✗ | i * s->subsampling[0] + k] = *src++; | |
490 | ✗ | *pu++ = *src++; | |
491 | ✗ | *pv++ = *src++; | |
492 | } | ||
493 | } | ||
494 | ✗ | } | |
495 | |||
496 | #if CONFIG_ZLIB | ||
497 | 2 | static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src, | |
498 | int size) | ||
499 | { | ||
500 | 2 | z_stream zstream = { 0 }; | |
501 | int zret; | ||
502 | |||
503 | 2 | zstream.next_in = src; | |
504 | 2 | zstream.avail_in = size; | |
505 | 2 | zstream.next_out = dst; | |
506 | 2 | zstream.avail_out = *len; | |
507 | 2 | zret = inflateInit(&zstream); | |
508 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (zret != Z_OK) { |
509 | ✗ | av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret); | |
510 | ✗ | return zret; | |
511 | } | ||
512 | 2 | zret = inflate(&zstream, Z_SYNC_FLUSH); | |
513 | 2 | inflateEnd(&zstream); | |
514 | 2 | *len = zstream.total_out; | |
515 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | return zret == Z_STREAM_END ? Z_OK : zret; |
516 | } | ||
517 | |||
518 | 2 | static int tiff_unpack_zlib(TiffContext *s, AVFrame *p, uint8_t *dst, int stride, | |
519 | const uint8_t *src, int size, int width, int lines, | ||
520 | int strip_start, int is_yuv) | ||
521 | { | ||
522 | uint8_t *zbuf; | ||
523 | unsigned long outlen; | ||
524 | int ret, line; | ||
525 | 2 | outlen = width * lines; | |
526 | 2 | zbuf = av_malloc(outlen); | |
527 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!zbuf) |
528 | ✗ | return AVERROR(ENOMEM); | |
529 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (s->fill_order) { |
530 | ✗ | if ((ret = deinvert_buffer(s, src, size)) < 0) { | |
531 | ✗ | av_free(zbuf); | |
532 | ✗ | return ret; | |
533 | } | ||
534 | ✗ | src = s->deinvert_buf; | |
535 | } | ||
536 | 2 | ret = tiff_uncompress(zbuf, &outlen, src, size); | |
537 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret != Z_OK) { |
538 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
539 | "Uncompressing failed (%lu of %lu) with error %d\n", outlen, | ||
540 | ✗ | (unsigned long)width * lines, ret); | |
541 | ✗ | av_free(zbuf); | |
542 | ✗ | return AVERROR_UNKNOWN; | |
543 | } | ||
544 | 2 | src = zbuf; | |
545 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
|
18 | for (line = 0; line < lines; line++) { |
546 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
16 | if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) { |
547 | ✗ | horizontal_fill(s, s->bpp, dst, 1, src, 0, width, 0); | |
548 | } else { | ||
549 | 16 | memcpy(dst, src, width); | |
550 | } | ||
551 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (is_yuv) { |
552 | ✗ | unpack_yuv(s, p, dst, strip_start + line); | |
553 | ✗ | line += s->subsampling[1] - 1; | |
554 | } | ||
555 | 16 | dst += stride; | |
556 | 16 | src += width; | |
557 | } | ||
558 | 2 | av_free(zbuf); | |
559 | 2 | return 0; | |
560 | } | ||
561 | #endif | ||
562 | |||
563 | #if CONFIG_LZMA | ||
564 | ✗ | static int tiff_uncompress_lzma(uint8_t *dst, uint64_t *len, const uint8_t *src, | |
565 | int size) | ||
566 | { | ||
567 | ✗ | lzma_stream stream = LZMA_STREAM_INIT; | |
568 | lzma_ret ret; | ||
569 | |||
570 | ✗ | stream.next_in = src; | |
571 | ✗ | stream.avail_in = size; | |
572 | ✗ | stream.next_out = dst; | |
573 | ✗ | stream.avail_out = *len; | |
574 | ✗ | ret = lzma_stream_decoder(&stream, UINT64_MAX, 0); | |
575 | ✗ | if (ret != LZMA_OK) { | |
576 | ✗ | av_log(NULL, AV_LOG_ERROR, "LZMA init error: %d\n", ret); | |
577 | ✗ | return ret; | |
578 | } | ||
579 | ✗ | ret = lzma_code(&stream, LZMA_RUN); | |
580 | ✗ | lzma_end(&stream); | |
581 | ✗ | *len = stream.total_out; | |
582 | ✗ | return ret == LZMA_STREAM_END ? LZMA_OK : ret; | |
583 | } | ||
584 | |||
585 | ✗ | static int tiff_unpack_lzma(TiffContext *s, AVFrame *p, uint8_t *dst, int stride, | |
586 | const uint8_t *src, int size, int width, int lines, | ||
587 | int strip_start, int is_yuv) | ||
588 | { | ||
589 | ✗ | uint64_t outlen = width * (uint64_t)lines; | |
590 | int ret, line; | ||
591 | ✗ | uint8_t *buf = av_malloc(outlen); | |
592 | ✗ | if (!buf) | |
593 | ✗ | return AVERROR(ENOMEM); | |
594 | ✗ | if (s->fill_order) { | |
595 | ✗ | if ((ret = deinvert_buffer(s, src, size)) < 0) { | |
596 | ✗ | av_free(buf); | |
597 | ✗ | return ret; | |
598 | } | ||
599 | ✗ | src = s->deinvert_buf; | |
600 | } | ||
601 | ✗ | ret = tiff_uncompress_lzma(buf, &outlen, src, size); | |
602 | ✗ | if (ret != LZMA_OK) { | |
603 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
604 | "Uncompressing failed (%"PRIu64" of %"PRIu64") with error %d\n", outlen, | ||
605 | ✗ | (uint64_t)width * lines, ret); | |
606 | ✗ | av_free(buf); | |
607 | ✗ | return AVERROR_UNKNOWN; | |
608 | } | ||
609 | ✗ | src = buf; | |
610 | ✗ | for (line = 0; line < lines; line++) { | |
611 | ✗ | if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) { | |
612 | ✗ | horizontal_fill(s, s->bpp, dst, 1, src, 0, width, 0); | |
613 | } else { | ||
614 | ✗ | memcpy(dst, src, width); | |
615 | } | ||
616 | ✗ | if (is_yuv) { | |
617 | ✗ | unpack_yuv(s, p, dst, strip_start + line); | |
618 | ✗ | line += s->subsampling[1] - 1; | |
619 | } | ||
620 | ✗ | dst += stride; | |
621 | ✗ | src += width; | |
622 | } | ||
623 | ✗ | av_free(buf); | |
624 | ✗ | return 0; | |
625 | } | ||
626 | #endif | ||
627 | |||
628 | 18 | static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride, | |
629 | const uint8_t *src, int size, int width, int lines) | ||
630 | { | ||
631 | int line; | ||
632 | int ret; | ||
633 | |||
634 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | if (s->fill_order) { |
635 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
|
18 | if ((ret = deinvert_buffer(s, src, size)) < 0) |
636 | ✗ | return ret; | |
637 | 18 | src = s->deinvert_buf; | |
638 | } | ||
639 | 18 | ret = ff_ccitt_unpack(s->avctx, src, size, dst, lines, stride, | |
640 | s->compr, s->fax_opts); | ||
641 |
2/4✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
|
18 | if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) |
642 | ✗ | for (line = 0; line < lines; line++) { | |
643 | ✗ | horizontal_fill(s, s->bpp, dst, 1, dst, 0, width, 0); | |
644 | ✗ | dst += stride; | |
645 | } | ||
646 | 18 | return ret; | |
647 | } | ||
648 | |||
649 | ✗ | static int dng_decode_jpeg(AVCodecContext *avctx, AVFrame *frame, | |
650 | int tile_byte_count, int dst_x, int dst_y, int w, int h) | ||
651 | { | ||
652 | ✗ | TiffContext *s = avctx->priv_data; | |
653 | uint8_t *dst_data, *src_data; | ||
654 | uint32_t dst_offset; /* offset from dst buffer in pixels */ | ||
655 | int is_single_comp, is_u16, pixel_size; | ||
656 | int ret; | ||
657 | |||
658 | ✗ | if (tile_byte_count < 0 || tile_byte_count > bytestream2_get_bytes_left(&s->gb)) | |
659 | ✗ | return AVERROR_INVALIDDATA; | |
660 | |||
661 | /* Prepare a packet and send to the MJPEG decoder */ | ||
662 | ✗ | av_packet_unref(s->jpkt); | |
663 | ✗ | s->jpkt->data = (uint8_t*)s->gb.buffer; | |
664 | ✗ | s->jpkt->size = tile_byte_count; | |
665 | |||
666 | ✗ | if (s->is_bayer) { | |
667 | ✗ | MJpegDecodeContext *mjpegdecctx = s->avctx_mjpeg->priv_data; | |
668 | /* We have to set this information here, there is no way to know if a given JPEG is a DNG-embedded | ||
669 | image or not from its own data (and we need that information when decoding it). */ | ||
670 | ✗ | mjpegdecctx->bayer = 1; | |
671 | } | ||
672 | |||
673 | ✗ | ret = avcodec_send_packet(s->avctx_mjpeg, s->jpkt); | |
674 | ✗ | if (ret < 0) { | |
675 | ✗ | av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n"); | |
676 | ✗ | return ret; | |
677 | } | ||
678 | |||
679 | ✗ | ret = avcodec_receive_frame(s->avctx_mjpeg, s->jpgframe); | |
680 | ✗ | if (ret < 0) { | |
681 | ✗ | av_log(avctx, AV_LOG_ERROR, "JPEG decoding error: %s.\n", av_err2str(ret)); | |
682 | |||
683 | /* Normally skip, error if explode */ | ||
684 | ✗ | if (avctx->err_recognition & AV_EF_EXPLODE) | |
685 | ✗ | return AVERROR_INVALIDDATA; | |
686 | else | ||
687 | ✗ | return 0; | |
688 | } | ||
689 | |||
690 | ✗ | is_u16 = (s->bpp > 8); | |
691 | |||
692 | /* Copy the outputted tile's pixels from 'jpgframe' to 'frame' (final buffer) */ | ||
693 | |||
694 | ✗ | if (s->jpgframe->width != s->avctx_mjpeg->width || | |
695 | ✗ | s->jpgframe->height != s->avctx_mjpeg->height || | |
696 | ✗ | s->jpgframe->format != s->avctx_mjpeg->pix_fmt) | |
697 | ✗ | return AVERROR_INVALIDDATA; | |
698 | |||
699 | /* See dng_blit for explanation */ | ||
700 | ✗ | if (s->avctx_mjpeg->width == w * 2 && | |
701 | ✗ | s->avctx_mjpeg->height == h / 2 && | |
702 | ✗ | s->avctx_mjpeg->pix_fmt == AV_PIX_FMT_GRAY16LE) { | |
703 | ✗ | is_single_comp = 1; | |
704 | ✗ | } else if (s->avctx_mjpeg->width >= w && | |
705 | ✗ | s->avctx_mjpeg->height >= h && | |
706 | ✗ | s->avctx_mjpeg->pix_fmt == (is_u16 ? AV_PIX_FMT_GRAY16 : AV_PIX_FMT_GRAY8) | |
707 | ) { | ||
708 | ✗ | is_single_comp = 0; | |
709 | } else | ||
710 | ✗ | return AVERROR_INVALIDDATA; | |
711 | |||
712 | ✗ | pixel_size = (is_u16 ? sizeof(uint16_t) : sizeof(uint8_t)); | |
713 | |||
714 | ✗ | if (is_single_comp && !is_u16) { | |
715 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "DNGs with bpp <= 8 and 1 component are unsupported\n"); | |
716 | ✗ | av_frame_unref(s->jpgframe); | |
717 | ✗ | return AVERROR_PATCHWELCOME; | |
718 | } | ||
719 | |||
720 | ✗ | dst_offset = dst_x + frame->linesize[0] * dst_y / pixel_size; | |
721 | ✗ | dst_data = frame->data[0] + dst_offset * pixel_size; | |
722 | ✗ | src_data = s->jpgframe->data[0]; | |
723 | |||
724 | ✗ | dng_blit(s, | |
725 | dst_data, | ||
726 | ✗ | frame->linesize[0] / pixel_size, | |
727 | src_data, | ||
728 | ✗ | s->jpgframe->linesize[0] / pixel_size, | |
729 | w, | ||
730 | h, | ||
731 | is_single_comp, | ||
732 | is_u16, 0); | ||
733 | |||
734 | ✗ | av_frame_unref(s->jpgframe); | |
735 | |||
736 | ✗ | return 0; | |
737 | } | ||
738 | |||
739 | 579 | static int tiff_unpack_strip(TiffContext *s, AVFrame *p, uint8_t *dst, int stride, | |
740 | const uint8_t *src, int size, int strip_start, int lines) | ||
741 | { | ||
742 | PutByteContext pb; | ||
743 | int c, line, pixels, code, ret; | ||
744 | 579 | const uint8_t *ssrc = src; | |
745 | 579 | int width = ((s->width * s->bpp) + 7) >> 3; | |
746 | 579 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(p->format); | |
747 | 1176 | int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) && | |
748 |
3/4✓ Branch 0 taken 18 times.
✓ Branch 1 taken 561 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
|
579 | (desc->flags & AV_PIX_FMT_FLAG_PLANAR) && |
749 | ✗ | desc->nb_components >= 3; | |
750 | int is_dng; | ||
751 | |||
752 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
|
579 | if (s->planar) |
753 | ✗ | width /= s->bppcount; | |
754 | |||
755 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
|
579 | if (size <= 0) |
756 | ✗ | return AVERROR_INVALIDDATA; | |
757 | |||
758 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
|
579 | if (is_yuv) { |
759 | ✗ | int bytes_per_row = (((s->width - 1) / s->subsampling[0] + 1) * s->bpp * | |
760 | ✗ | s->subsampling[0] * s->subsampling[1] + 7) >> 3; | |
761 | ✗ | av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, bytes_per_row); | |
762 | ✗ | if (s->yuv_line == NULL) { | |
763 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n"); | |
764 | ✗ | return AVERROR(ENOMEM); | |
765 | } | ||
766 | ✗ | dst = s->yuv_line; | |
767 | ✗ | stride = 0; | |
768 | |||
769 | ✗ | width = (s->width - 1) / s->subsampling[0] + 1; | |
770 | ✗ | width = width * s->subsampling[0] * s->subsampling[1] + 2*width; | |
771 | ✗ | av_assert0(width <= bytes_per_row); | |
772 | ✗ | av_assert0(s->bpp == 24); | |
773 | } | ||
774 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
|
579 | if (s->is_bayer) { |
775 | ✗ | av_assert0(width == (s->bpp * s->width + 7) >> 3); | |
776 | } | ||
777 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
579 | av_assert0(!(s->is_bayer && is_yuv)); |
778 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
|
579 | if (p->format == AV_PIX_FMT_GRAY12) { |
779 | ✗ | av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, width); | |
780 | ✗ | if (s->yuv_line == NULL) { | |
781 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n"); | |
782 | ✗ | return AVERROR(ENOMEM); | |
783 | } | ||
784 | ✗ | dst = s->yuv_line; | |
785 | ✗ | stride = 0; | |
786 | } | ||
787 | |||
788 |
3/4✓ Branch 0 taken 579 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 577 times.
|
579 | if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) { |
789 | #if CONFIG_ZLIB | ||
790 | 2 | return tiff_unpack_zlib(s, p, dst, stride, src, size, width, lines, | |
791 | strip_start, is_yuv); | ||
792 | #else | ||
793 | av_log(s->avctx, AV_LOG_ERROR, | ||
794 | "zlib support not enabled, " | ||
795 | "deflate compression not supported\n"); | ||
796 | return AVERROR(ENOSYS); | ||
797 | #endif | ||
798 | } | ||
799 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 577 times.
|
577 | if (s->compr == TIFF_LZMA) { |
800 | #if CONFIG_LZMA | ||
801 | ✗ | return tiff_unpack_lzma(s, p, dst, stride, src, size, width, lines, | |
802 | strip_start, is_yuv); | ||
803 | #else | ||
804 | av_log(s->avctx, AV_LOG_ERROR, | ||
805 | "LZMA support not enabled\n"); | ||
806 | return AVERROR(ENOSYS); | ||
807 | #endif | ||
808 | } | ||
809 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 575 times.
|
577 | if (s->compr == TIFF_LZW) { |
810 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (s->fill_order) { |
811 | ✗ | if ((ret = deinvert_buffer(s, src, size)) < 0) | |
812 | ✗ | return ret; | |
813 | ✗ | ssrc = src = s->deinvert_buf; | |
814 | } | ||
815 |
2/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
2 | if (size > 1 && !src[0] && (src[1]&1)) { |
816 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Old style LZW is unsupported\n"); | |
817 | } | ||
818 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) { |
819 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n"); | |
820 | ✗ | return ret; | |
821 | } | ||
822 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
|
18 | for (line = 0; line < lines; line++) { |
823 | 16 | pixels = ff_lzw_decode(s->lzw, dst, width); | |
824 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (pixels < width) { |
825 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", | |
826 | pixels, width); | ||
827 | ✗ | return AVERROR_INVALIDDATA; | |
828 | } | ||
829 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
16 | if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) |
830 | ✗ | horizontal_fill(s, s->bpp, dst, 1, dst, 0, width, 0); | |
831 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (is_yuv) { |
832 | ✗ | unpack_yuv(s, p, dst, strip_start + line); | |
833 | ✗ | line += s->subsampling[1] - 1; | |
834 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | } else if (p->format == AV_PIX_FMT_GRAY12) { |
835 | ✗ | unpack_gray(s, p, dst, strip_start + line, width, s->bpp); | |
836 | } | ||
837 | 16 | dst += stride; | |
838 | } | ||
839 | 2 | return 0; | |
840 | } | ||
841 |
1/2✓ Branch 0 taken 575 times.
✗ Branch 1 not taken.
|
575 | if (s->compr == TIFF_CCITT_RLE || |
842 |
2/2✓ Branch 0 taken 557 times.
✓ Branch 1 taken 18 times.
|
575 | s->compr == TIFF_G3 || |
843 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 557 times.
|
557 | s->compr == TIFF_G4) { |
844 |
2/4✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
|
18 | if (is_yuv || p->format == AV_PIX_FMT_GRAY12) |
845 | ✗ | return AVERROR_INVALIDDATA; | |
846 | |||
847 | 18 | return tiff_unpack_fax(s, dst, stride, src, size, width, lines); | |
848 | } | ||
849 | |||
850 | 557 | bytestream2_init(&s->gb, src, size); | |
851 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 557 times.
|
557 | bytestream2_init_writer(&pb, dst, is_yuv ? s->yuv_line_size : (stride * lines)); |
852 | |||
853 |
2/4✓ Branch 0 taken 557 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 557 times.
|
557 | is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG); |
854 | |||
855 | /* Decode JPEG-encoded DNGs with strips */ | ||
856 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 557 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
557 | if (s->compr == TIFF_NEWJPEG && is_dng) { |
857 | ✗ | if (s->strips > 1) { | |
858 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "More than one DNG JPEG strips unsupported\n"); | |
859 | ✗ | return AVERROR_PATCHWELCOME; | |
860 | } | ||
861 | ✗ | if (!s->is_bayer) | |
862 | ✗ | return AVERROR_PATCHWELCOME; | |
863 | ✗ | if ((ret = dng_decode_jpeg(s->avctx, p, s->stripsize, 0, 0, s->width, s->height)) < 0) | |
864 | ✗ | return ret; | |
865 | ✗ | return 0; | |
866 | } | ||
867 | |||
868 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 557 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
557 | if (is_dng && stride == 0) |
869 | ✗ | return AVERROR_INVALIDDATA; | |
870 | |||
871 |
2/2✓ Branch 0 taken 3872 times.
✓ Branch 1 taken 557 times.
|
4429 | for (line = 0; line < lines; line++) { |
872 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3872 times.
|
3872 | if (src - ssrc > size) { |
873 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n"); | |
874 | ✗ | return AVERROR_INVALIDDATA; | |
875 | } | ||
876 | |||
877 |
2/4✓ Branch 1 taken 3872 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3872 times.
✗ Branch 5 not taken.
|
3872 | if (bytestream2_get_bytes_left(&s->gb) == 0 || bytestream2_get_eof(&pb)) |
878 | break; | ||
879 | 3872 | bytestream2_seek_p(&pb, stride * line, SEEK_SET); | |
880 |
2/3✓ Branch 0 taken 128 times.
✓ Branch 1 taken 3744 times.
✗ Branch 2 not taken.
|
3872 | switch (s->compr) { |
881 | 128 | case TIFF_RAW: | |
882 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
128 | if (ssrc + size - src < width) |
883 | ✗ | return AVERROR_INVALIDDATA; | |
884 | |||
885 |
1/2✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
|
128 | if (!s->fill_order) { |
886 |
2/4✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 128 times.
|
128 | horizontal_fill(s, s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 || s->is_bayer), |
887 | dst, 1, src, 0, width, 0); | ||
888 | } else { | ||
889 | int i; | ||
890 | ✗ | for (i = 0; i < width; i++) | |
891 | ✗ | dst[i] = ff_reverse[src[i]]; | |
892 | } | ||
893 | |||
894 | /* Color processing for DNG images with uncompressed strips (non-tiled) */ | ||
895 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
128 | if (is_dng) { |
896 | int is_u16, pixel_size_bytes, pixel_size_bits, elements; | ||
897 | |||
898 | ✗ | is_u16 = (s->bpp / s->bppcount > 8); | |
899 | ✗ | pixel_size_bits = (is_u16 ? 16 : 8); | |
900 | ✗ | pixel_size_bytes = (is_u16 ? sizeof(uint16_t) : sizeof(uint8_t)); | |
901 | |||
902 | ✗ | elements = width / pixel_size_bytes * pixel_size_bits / s->bpp * s->bppcount; // need to account for [1, 16] bpp | |
903 | ✗ | av_assert0 (elements * pixel_size_bytes <= FFABS(stride)); | |
904 | ✗ | dng_blit(s, | |
905 | dst, | ||
906 | 0, // no stride, only 1 line | ||
907 | dst, | ||
908 | 0, // no stride, only 1 line | ||
909 | elements, | ||
910 | 1, | ||
911 | 0, // single-component variation is only preset in JPEG-encoded DNGs | ||
912 | is_u16, | ||
913 | ✗ | (line + strip_start)&1); | |
914 | } | ||
915 | |||
916 | 128 | src += width; | |
917 | 128 | break; | |
918 | 3744 | case TIFF_PACKBITS: | |
919 |
2/2✓ Branch 0 taken 36356 times.
✓ Branch 1 taken 3744 times.
|
40100 | for (pixels = 0; pixels < width;) { |
920 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36356 times.
|
36356 | if (ssrc + size - src < 2) { |
921 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Read went out of bounds\n"); | |
922 | ✗ | return AVERROR_INVALIDDATA; | |
923 | } | ||
924 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36356 times.
|
36356 | code = s->fill_order ? (int8_t) ff_reverse[*src++]: (int8_t) *src++; |
925 |
2/2✓ Branch 0 taken 34241 times.
✓ Branch 1 taken 2115 times.
|
36356 | if (code >= 0) { |
926 | 34241 | code++; | |
927 |
1/2✓ Branch 0 taken 34241 times.
✗ Branch 1 not taken.
|
34241 | if (pixels + code > width || |
928 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 34241 times.
|
34241 | ssrc + size - src < code) { |
929 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
930 | "Copy went out of bounds\n"); | ||
931 | ✗ | return AVERROR_INVALIDDATA; | |
932 | } | ||
933 | 34241 | horizontal_fill(s, s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8), | |
934 | dst, 1, src, 0, code, pixels); | ||
935 | 34241 | src += code; | |
936 | 34241 | pixels += code; | |
937 |
1/2✓ Branch 0 taken 2115 times.
✗ Branch 1 not taken.
|
2115 | } else if (code != -128) { // -127..-1 |
938 | 2115 | code = (-code) + 1; | |
939 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2115 times.
|
2115 | if (pixels + code > width) { |
940 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
941 | "Run went out of bounds\n"); | ||
942 | ✗ | return AVERROR_INVALIDDATA; | |
943 | } | ||
944 | 2115 | c = *src++; | |
945 | 2115 | horizontal_fill(s, s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8), | |
946 | dst, 0, NULL, c, code, pixels); | ||
947 | 2115 | pixels += code; | |
948 | } | ||
949 | } | ||
950 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3744 times.
|
3744 | if (s->fill_order) { |
951 | int i; | ||
952 | ✗ | for (i = 0; i < width; i++) | |
953 | ✗ | dst[i] = ff_reverse[dst[i]]; | |
954 | } | ||
955 | 3744 | break; | |
956 | } | ||
957 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3872 times.
|
3872 | if (is_yuv) { |
958 | ✗ | unpack_yuv(s, p, dst, strip_start + line); | |
959 | ✗ | line += s->subsampling[1] - 1; | |
960 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3872 times.
|
3872 | } else if (p->format == AV_PIX_FMT_GRAY12) { |
961 | ✗ | unpack_gray(s, p, dst, strip_start + line, width, s->bpp); | |
962 | } | ||
963 | 3872 | dst += stride; | |
964 | } | ||
965 | 557 | return 0; | |
966 | } | ||
967 | |||
968 | ✗ | static int dng_decode_tiles(AVCodecContext *avctx, AVFrame *frame, | |
969 | const AVPacket *avpkt) | ||
970 | { | ||
971 | ✗ | TiffContext *s = avctx->priv_data; | |
972 | int tile_idx; | ||
973 | int tile_offset_offset, tile_offset; | ||
974 | int tile_byte_count_offset, tile_byte_count; | ||
975 | int tile_count_x, tile_count_y; | ||
976 | int tile_width, tile_length; | ||
977 | int has_width_leftover, has_height_leftover; | ||
978 | ✗ | int tile_x = 0, tile_y = 0; | |
979 | ✗ | int pos_x = 0, pos_y = 0; | |
980 | int ret; | ||
981 | |||
982 | ✗ | if (s->tile_width <= 0 || s->tile_length <= 0) | |
983 | ✗ | return AVERROR_INVALIDDATA; | |
984 | |||
985 | ✗ | has_width_leftover = (s->width % s->tile_width != 0); | |
986 | ✗ | has_height_leftover = (s->height % s->tile_length != 0); | |
987 | |||
988 | /* Calculate tile counts (round up) */ | ||
989 | ✗ | tile_count_x = (s->width + s->tile_width - 1) / s->tile_width; | |
990 | ✗ | tile_count_y = (s->height + s->tile_length - 1) / s->tile_length; | |
991 | |||
992 | /* Iterate over the number of tiles */ | ||
993 | ✗ | for (tile_idx = 0; tile_idx < tile_count_x * tile_count_y; tile_idx++) { | |
994 | ✗ | tile_x = tile_idx % tile_count_x; | |
995 | ✗ | tile_y = tile_idx / tile_count_x; | |
996 | |||
997 | ✗ | if (has_width_leftover && tile_x == tile_count_x - 1) // If on the right-most tile | |
998 | ✗ | tile_width = s->width % s->tile_width; | |
999 | else | ||
1000 | ✗ | tile_width = s->tile_width; | |
1001 | |||
1002 | ✗ | if (has_height_leftover && tile_y == tile_count_y - 1) // If on the bottom-most tile | |
1003 | ✗ | tile_length = s->height % s->tile_length; | |
1004 | else | ||
1005 | ✗ | tile_length = s->tile_length; | |
1006 | |||
1007 | /* Read tile offset */ | ||
1008 | ✗ | tile_offset_offset = s->tile_offsets_offset + tile_idx * sizeof(int); | |
1009 | ✗ | bytestream2_seek(&s->gb, tile_offset_offset, SEEK_SET); | |
1010 | ✗ | tile_offset = ff_tget_long(&s->gb, s->le); | |
1011 | |||
1012 | /* Read tile byte size */ | ||
1013 | ✗ | tile_byte_count_offset = s->tile_byte_counts_offset + tile_idx * sizeof(int); | |
1014 | ✗ | bytestream2_seek(&s->gb, tile_byte_count_offset, SEEK_SET); | |
1015 | ✗ | tile_byte_count = ff_tget_long(&s->gb, s->le); | |
1016 | |||
1017 | /* Seek to tile data */ | ||
1018 | ✗ | bytestream2_seek(&s->gb, tile_offset, SEEK_SET); | |
1019 | |||
1020 | /* Decode JPEG tile and copy it in the reference frame */ | ||
1021 | ✗ | ret = dng_decode_jpeg(avctx, frame, tile_byte_count, pos_x, pos_y, tile_width, tile_length); | |
1022 | |||
1023 | ✗ | if (ret < 0) | |
1024 | ✗ | return ret; | |
1025 | |||
1026 | /* Advance current positions */ | ||
1027 | ✗ | pos_x += tile_width; | |
1028 | ✗ | if (tile_x == tile_count_x - 1) { // If on the right edge | |
1029 | ✗ | pos_x = 0; | |
1030 | ✗ | pos_y += tile_length; | |
1031 | } | ||
1032 | } | ||
1033 | |||
1034 | /* Frame is ready to be output */ | ||
1035 | ✗ | frame->pict_type = AV_PICTURE_TYPE_I; | |
1036 | ✗ | frame->flags |= AV_FRAME_FLAG_KEY; | |
1037 | |||
1038 | ✗ | return avpkt->size; | |
1039 | } | ||
1040 | |||
1041 | 33 | static int init_image(TiffContext *s, AVFrame *frame) | |
1042 | { | ||
1043 | int ret; | ||
1044 | 33 | int create_gray_palette = 0; | |
1045 | |||
1046 | // make sure there is no aliasing in the following switch | ||
1047 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
|
33 | if (s->bpp > 128 || s->bppcount >= 10) { |
1048 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
1049 | "Unsupported image parameters: bpp=%d, bppcount=%d\n", | ||
1050 | s->bpp, s->bppcount); | ||
1051 | ✗ | return AVERROR_INVALIDDATA; | |
1052 | } | ||
1053 | |||
1054 |
4/23✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 17 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 6 times.
✓ Branch 19 taken 6 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
|
33 | switch (s->planar * 10000 + s->bpp * 10 + s->bppcount + s->is_bayer * 100000) { |
1055 | 4 | case 11: | |
1056 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (!s->palette_is_set) { |
1057 | 4 | s->avctx->pix_fmt = AV_PIX_FMT_MONOBLACK; | |
1058 | 4 | break; | |
1059 | } | ||
1060 | case 21: | ||
1061 | case 41: | ||
1062 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_PAL8; | |
1063 | ✗ | if (!s->palette_is_set) { | |
1064 | ✗ | create_gray_palette = 1; | |
1065 | } | ||
1066 | ✗ | break; | |
1067 | ✗ | case 81: | |
1068 | ✗ | s->avctx->pix_fmt = s->palette_is_set ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8; | |
1069 | ✗ | break; | |
1070 | ✗ | case 121: | |
1071 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_GRAY12; | |
1072 | ✗ | break; | |
1073 | ✗ | case 100081: | |
1074 | ✗ | switch (AV_RL32(s->pattern)) { | |
1075 | ✗ | case 0x02010100: | |
1076 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_BAYER_RGGB8; | |
1077 | ✗ | break; | |
1078 | ✗ | case 0x00010102: | |
1079 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_BAYER_BGGR8; | |
1080 | ✗ | break; | |
1081 | ✗ | case 0x01000201: | |
1082 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GBRG8; | |
1083 | ✗ | break; | |
1084 | ✗ | case 0x01020001: | |
1085 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GRBG8; | |
1086 | ✗ | break; | |
1087 | ✗ | default: | |
1088 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Unsupported Bayer pattern: 0x%X\n", | |
1089 | ✗ | AV_RL32(s->pattern)); | |
1090 | ✗ | return AVERROR_PATCHWELCOME; | |
1091 | } | ||
1092 | ✗ | break; | |
1093 | ✗ | case 100101: | |
1094 | case 100121: | ||
1095 | case 100141: | ||
1096 | case 100161: | ||
1097 | ✗ | switch (AV_RL32(s->pattern)) { | |
1098 | ✗ | case 0x02010100: | |
1099 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_BAYER_RGGB16; | |
1100 | ✗ | break; | |
1101 | ✗ | case 0x00010102: | |
1102 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_BAYER_BGGR16; | |
1103 | ✗ | break; | |
1104 | ✗ | case 0x01000201: | |
1105 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GBRG16; | |
1106 | ✗ | break; | |
1107 | ✗ | case 0x01020001: | |
1108 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GRBG16; | |
1109 | ✗ | break; | |
1110 | ✗ | default: | |
1111 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Unsupported Bayer pattern: 0x%X\n", | |
1112 | ✗ | AV_RL32(s->pattern)); | |
1113 | ✗ | return AVERROR_PATCHWELCOME; | |
1114 | } | ||
1115 | ✗ | break; | |
1116 | 17 | case 243: | |
1117 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) { |
1118 | ✗ | if (s->subsampling[0] == 1 && s->subsampling[1] == 1) { | |
1119 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_YUV444P; | |
1120 | ✗ | } else if (s->subsampling[0] == 2 && s->subsampling[1] == 1) { | |
1121 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_YUV422P; | |
1122 | ✗ | } else if (s->subsampling[0] == 4 && s->subsampling[1] == 1) { | |
1123 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_YUV411P; | |
1124 | ✗ | } else if (s->subsampling[0] == 1 && s->subsampling[1] == 2) { | |
1125 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_YUV440P; | |
1126 | ✗ | } else if (s->subsampling[0] == 2 && s->subsampling[1] == 2) { | |
1127 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_YUV420P; | |
1128 | ✗ | } else if (s->subsampling[0] == 4 && s->subsampling[1] == 4) { | |
1129 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_YUV410P; | |
1130 | } else { | ||
1131 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr subsampling\n"); | |
1132 | ✗ | return AVERROR_PATCHWELCOME; | |
1133 | } | ||
1134 | } else | ||
1135 | 17 | s->avctx->pix_fmt = AV_PIX_FMT_RGB24; | |
1136 | 17 | break; | |
1137 | ✗ | case 161: | |
1138 | ✗ | s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GRAY16LE : AV_PIX_FMT_GRAY16BE; | |
1139 | ✗ | break; | |
1140 | ✗ | case 162: | |
1141 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_YA8; | |
1142 | ✗ | break; | |
1143 | ✗ | case 322: | |
1144 | ✗ | s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YA16LE : AV_PIX_FMT_YA16BE; | |
1145 | ✗ | break; | |
1146 | ✗ | case 324: | |
1147 | ✗ | s->avctx->pix_fmt = s->photometric == TIFF_PHOTOMETRIC_SEPARATED ? AV_PIX_FMT_RGB0 : AV_PIX_FMT_RGBA; | |
1148 | ✗ | break; | |
1149 | ✗ | case 405: | |
1150 | ✗ | if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED) | |
1151 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_RGBA; | |
1152 | else { | ||
1153 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
1154 | "bpp=40 without PHOTOMETRIC_SEPARATED is unsupported\n"); | ||
1155 | ✗ | return AVERROR_PATCHWELCOME; | |
1156 | } | ||
1157 | ✗ | break; | |
1158 | ✗ | case 483: | |
1159 | ✗ | s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE; | |
1160 | ✗ | break; | |
1161 | ✗ | case 644: | |
1162 | ✗ | s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGBA64LE : AV_PIX_FMT_RGBA64BE; | |
1163 | ✗ | break; | |
1164 | ✗ | case 10243: | |
1165 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_GBRP; | |
1166 | ✗ | break; | |
1167 | ✗ | case 10324: | |
1168 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_GBRAP; | |
1169 | ✗ | break; | |
1170 | ✗ | case 10483: | |
1171 | ✗ | s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRP16LE : AV_PIX_FMT_GBRP16BE; | |
1172 | ✗ | break; | |
1173 | ✗ | case 10644: | |
1174 | ✗ | s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRAP16LE : AV_PIX_FMT_GBRAP16BE; | |
1175 | ✗ | break; | |
1176 | 6 | case 963: | |
1177 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGBF32LE : AV_PIX_FMT_RGBF32BE; |
1178 | 6 | break; | |
1179 | 6 | case 1284: | |
1180 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGBAF32LE : AV_PIX_FMT_RGBAF32BE; |
1181 | 6 | break; | |
1182 | ✗ | case 10963: | |
1183 | ✗ | s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRPF32LE : AV_PIX_FMT_GBRPF32BE; | |
1184 | ✗ | break; | |
1185 | ✗ | case 11284: | |
1186 | ✗ | s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRAPF32LE : AV_PIX_FMT_GBRAPF32BE; | |
1187 | ✗ | break; | |
1188 | ✗ | default: | |
1189 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
1190 | "This format is not supported (bpp=%d, bppcount=%d)\n", | ||
1191 | s->bpp, s->bppcount); | ||
1192 | ✗ | return AVERROR_INVALIDDATA; | |
1193 | } | ||
1194 | |||
1195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) { |
1196 | ✗ | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt); | |
1197 | ✗ | if((desc->flags & AV_PIX_FMT_FLAG_RGB) || | |
1198 | ✗ | !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) || | |
1199 | ✗ | desc->nb_components < 3) { | |
1200 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr variant\n"); | |
1201 | ✗ | return AVERROR_INVALIDDATA; | |
1202 | } | ||
1203 | } | ||
1204 | |||
1205 |
3/4✓ Branch 0 taken 22 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
|
33 | if (s->width != s->avctx->width || s->height != s->avctx->height) { |
1206 | 11 | ret = ff_set_dimensions(s->avctx, s->width, s->height); | |
1207 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (ret < 0) |
1208 | ✗ | return ret; | |
1209 | } | ||
1210 | |||
1211 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 22 times.
|
33 | if (s->avctx->skip_frame >= AVDISCARD_ALL) |
1212 | 11 | return 0; | |
1213 | |||
1214 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
|
22 | if ((ret = ff_thread_get_buffer(s->avctx, frame, 0)) < 0) |
1215 | ✗ | return ret; | |
1216 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) { |
1217 | ✗ | if (!create_gray_palette) | |
1218 | ✗ | memcpy(frame->data[1], s->palette, sizeof(s->palette)); | |
1219 | else { | ||
1220 | /* make default grayscale pal */ | ||
1221 | int i; | ||
1222 | ✗ | uint32_t *pal = (uint32_t *)frame->data[1]; | |
1223 | ✗ | for (i = 0; i < 1<<s->bpp; i++) | |
1224 | ✗ | pal[i] = 0xFFU << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101; | |
1225 | } | ||
1226 | } | ||
1227 | 22 | return 1; | |
1228 | } | ||
1229 | |||
1230 | 42 | static void set_sar(TiffContext *s, unsigned tag, unsigned num, unsigned den) | |
1231 | { | ||
1232 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
|
42 | int offset = tag == TIFF_YRES ? 2 : 0; |
1233 | 42 | s->res[offset++] = num; | |
1234 | 42 | s->res[offset] = den; | |
1235 |
5/8✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✓ Branch 5 taken 9 times.
✓ Branch 6 taken 33 times.
✗ Branch 7 not taken.
|
42 | if (s->res[0] && s->res[1] && s->res[2] && s->res[3]) { |
1236 | 33 | uint64_t num = s->res[2] * (uint64_t)s->res[1]; | |
1237 | 33 | uint64_t den = s->res[0] * (uint64_t)s->res[3]; | |
1238 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
|
33 | if (num > INT64_MAX || den > INT64_MAX) { |
1239 | ✗ | num = num >> 1; | |
1240 | ✗ | den = den >> 1; | |
1241 | } | ||
1242 | 33 | av_reduce(&s->avctx->sample_aspect_ratio.num, &s->avctx->sample_aspect_ratio.den, | |
1243 | num, den, INT32_MAX); | ||
1244 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | if (!s->avctx->sample_aspect_ratio.den) |
1245 | ✗ | s->avctx->sample_aspect_ratio = (AVRational) {0, 1}; | |
1246 | } | ||
1247 | 42 | } | |
1248 | |||
1249 | 519 | static int tiff_decode_tag(TiffContext *s, AVFrame *frame) | |
1250 | { | ||
1251 | AVFrameSideData *sd; | ||
1252 | GetByteContext gb_temp; | ||
1253 | 519 | unsigned tag, type, count, off, value = 0, value2 = 1; // value2 is a denominator so init. to 1 | |
1254 | int i, start; | ||
1255 | int pos; | ||
1256 | int ret; | ||
1257 | double *dp; | ||
1258 | |||
1259 | 519 | ret = ff_tread_tag(&s->gb, s->le, &tag, &type, &count, &start); | |
1260 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 519 times.
|
519 | if (ret < 0) { |
1261 | ✗ | goto end; | |
1262 | } | ||
1263 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 519 times.
|
519 | if (tag <= s->last_tag) |
1264 | ✗ | return AVERROR_INVALIDDATA; | |
1265 | |||
1266 | // We ignore TIFF_STRIP_SIZE as it is sometimes in the logic but wrong order around TIFF_STRIP_OFFS | ||
1267 |
2/2✓ Branch 0 taken 486 times.
✓ Branch 1 taken 33 times.
|
519 | if (tag != TIFF_STRIP_SIZE) |
1268 | 486 | s->last_tag = tag; | |
1269 | |||
1270 | 519 | off = bytestream2_tell(&s->gb); | |
1271 |
2/2✓ Branch 0 taken 374 times.
✓ Branch 1 taken 145 times.
|
519 | if (count == 1) { |
1272 |
2/4✓ Branch 0 taken 332 times.
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
374 | switch (type) { |
1273 | 332 | case TIFF_BYTE: | |
1274 | case TIFF_SHORT: | ||
1275 | case TIFF_LONG: | ||
1276 | 332 | value = ff_tget(&s->gb, type, s->le); | |
1277 | 332 | break; | |
1278 | 42 | case TIFF_RATIONAL: | |
1279 | 42 | value = ff_tget_long(&s->gb, s->le); | |
1280 | 42 | value2 = ff_tget_long(&s->gb, s->le); | |
1281 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
|
42 | if (!value2) { |
1282 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator in rational\n"); | |
1283 | ✗ | value2 = 1; | |
1284 | } | ||
1285 | |||
1286 | 42 | break; | |
1287 | ✗ | case TIFF_STRING: | |
1288 | ✗ | if (count <= 4) { | |
1289 | ✗ | break; | |
1290 | } | ||
1291 | default: | ||
1292 | ✗ | value = UINT_MAX; | |
1293 | } | ||
1294 | } | ||
1295 | |||
1296 |
20/54✓ Branch 0 taken 15 times.
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 33 times.
✓ Branch 4 taken 33 times.
✓ Branch 5 taken 33 times.
✓ Branch 6 taken 33 times.
✓ Branch 7 taken 33 times.
✓ Branch 8 taken 33 times.
✓ Branch 9 taken 42 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 8 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 21 taken 33 times.
✓ Branch 22 taken 16 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 18 times.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✓ Branch 37 taken 2 times.
✓ Branch 38 taken 6 times.
✗ Branch 39 not taken.
✓ Branch 40 taken 4 times.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✓ Branch 44 taken 12 times.
✓ Branch 45 taken 2 times.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ Branch 48 not taken.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✓ Branch 53 taken 97 times.
|
519 | switch (tag) { |
1297 | 15 | case TIFF_SUBFILE: | |
1298 | 15 | s->is_thumbnail = (value != 0); | |
1299 | 15 | break; | |
1300 | 33 | case TIFF_WIDTH: | |
1301 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | if (value > INT_MAX) |
1302 | ✗ | return AVERROR_INVALIDDATA; | |
1303 | 33 | s->width = value; | |
1304 | 33 | break; | |
1305 | 33 | case TIFF_HEIGHT: | |
1306 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | if (value > INT_MAX) |
1307 | ✗ | return AVERROR_INVALIDDATA; | |
1308 | 33 | s->height = value; | |
1309 | 33 | break; | |
1310 | 33 | case TIFF_BPP: | |
1311 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
|
33 | if (count > 5 || count <= 0) { |
1312 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
1313 | "This format is not supported (bpp=%d, %d components)\n", | ||
1314 | value, count); | ||
1315 | ✗ | return AVERROR_INVALIDDATA; | |
1316 | } | ||
1317 | 33 | s->bppcount = count; | |
1318 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 29 times.
|
33 | if (count == 1) |
1319 | 4 | s->bpp = value; | |
1320 | else { | ||
1321 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | switch (type) { |
1322 | 29 | case TIFF_BYTE: | |
1323 | case TIFF_SHORT: | ||
1324 | case TIFF_LONG: | ||
1325 | 29 | s->bpp = 0; | |
1326 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
|
29 | if (bytestream2_get_bytes_left(&s->gb) < type_sizes[type] * count) |
1327 | ✗ | return AVERROR_INVALIDDATA; | |
1328 |
2/2✓ Branch 0 taken 93 times.
✓ Branch 1 taken 29 times.
|
122 | for (i = 0; i < count; i++) |
1329 | 93 | s->bpp += ff_tget(&s->gb, type, s->le); | |
1330 | 29 | break; | |
1331 | ✗ | default: | |
1332 | ✗ | s->bpp = -1; | |
1333 | } | ||
1334 | } | ||
1335 | 33 | break; | |
1336 | 33 | case TIFF_SAMPLES_PER_PIXEL: | |
1337 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | if (count != 1) { |
1338 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
1339 | "Samples per pixel requires a single value, many provided\n"); | ||
1340 | ✗ | return AVERROR_INVALIDDATA; | |
1341 | } | ||
1342 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
|
33 | if (value > 5 || value <= 0) { |
1343 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
1344 | "Invalid samples per pixel %d\n", value); | ||
1345 | ✗ | return AVERROR_INVALIDDATA; | |
1346 | } | ||
1347 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 29 times.
|
33 | if (s->bppcount == 1) |
1348 | 4 | s->bpp *= value; | |
1349 | 33 | s->bppcount = value; | |
1350 | 33 | break; | |
1351 | 33 | case TIFF_COMPR: | |
1352 | 33 | s->compr = value; | |
1353 | 33 | av_log(s->avctx, AV_LOG_DEBUG, "compression: %d\n", s->compr); | |
1354 | 33 | s->predictor = 0; | |
1355 |
3/6✓ Branch 0 taken 25 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
33 | switch (s->compr) { |
1356 | 25 | case TIFF_RAW: | |
1357 | case TIFF_PACKBITS: | ||
1358 | case TIFF_LZW: | ||
1359 | case TIFF_CCITT_RLE: | ||
1360 | 25 | break; | |
1361 | 4 | case TIFF_G3: | |
1362 | case TIFF_G4: | ||
1363 | 4 | s->fax_opts = 0; | |
1364 | 4 | break; | |
1365 | 4 | case TIFF_DEFLATE: | |
1366 | case TIFF_ADOBE_DEFLATE: | ||
1367 | #if CONFIG_ZLIB | ||
1368 | 4 | break; | |
1369 | #else | ||
1370 | av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n"); | ||
1371 | return AVERROR(ENOSYS); | ||
1372 | #endif | ||
1373 | ✗ | case TIFF_JPEG: | |
1374 | case TIFF_NEWJPEG: | ||
1375 | ✗ | s->is_jpeg = 1; | |
1376 | ✗ | break; | |
1377 | ✗ | case TIFF_LZMA: | |
1378 | #if CONFIG_LZMA | ||
1379 | ✗ | break; | |
1380 | #else | ||
1381 | av_log(s->avctx, AV_LOG_ERROR, "LZMA not compiled in\n"); | ||
1382 | return AVERROR(ENOSYS); | ||
1383 | #endif | ||
1384 | ✗ | default: | |
1385 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", | |
1386 | ✗ | s->compr); | |
1387 | ✗ | return AVERROR_INVALIDDATA; | |
1388 | } | ||
1389 | 33 | break; | |
1390 | 33 | case TIFF_ROWSPERSTRIP: | |
1391 |
4/6✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
|
33 | if (!value || (type == TIFF_LONG && value == UINT_MAX)) |
1392 | ✗ | value = s->height; | |
1393 | 33 | s->rps = FFMIN(value, s->height); | |
1394 | 33 | break; | |
1395 | 33 | case TIFF_STRIP_OFFS: | |
1396 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 19 times.
|
33 | if (count == 1) { |
1397 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (value > INT_MAX) { |
1398 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
1399 | "strippos %u too large\n", value); | ||
1400 | ✗ | return AVERROR_INVALIDDATA; | |
1401 | } | ||
1402 | 14 | s->strippos = 0; | |
1403 | 14 | s->stripoff = value; | |
1404 | } else | ||
1405 | 19 | s->strippos = off; | |
1406 | 33 | s->strips = count; | |
1407 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 31 times.
|
33 | if (s->strips == s->bppcount) |
1408 | 2 | s->rps = s->height; | |
1409 | 33 | s->sot = type; | |
1410 | 33 | break; | |
1411 | 33 | case TIFF_STRIP_SIZE: | |
1412 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 19 times.
|
33 | if (count == 1) { |
1413 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (value > INT_MAX) { |
1414 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
1415 | "stripsize %u too large\n", value); | ||
1416 | ✗ | return AVERROR_INVALIDDATA; | |
1417 | } | ||
1418 | 14 | s->stripsizesoff = 0; | |
1419 | 14 | s->stripsize = value; | |
1420 | 14 | s->strips = 1; | |
1421 | } else { | ||
1422 | 19 | s->stripsizesoff = off; | |
1423 | } | ||
1424 | 33 | s->strips = count; | |
1425 | 33 | s->sstype = type; | |
1426 | 33 | break; | |
1427 | 42 | case TIFF_XRES: | |
1428 | case TIFF_YRES: | ||
1429 | 42 | set_sar(s, tag, value, value2); | |
1430 | 42 | break; | |
1431 | ✗ | case TIFF_TILE_OFFSETS: | |
1432 | ✗ | s->tile_offsets_offset = off; | |
1433 | ✗ | s->is_tiled = 1; | |
1434 | ✗ | break; | |
1435 | ✗ | case TIFF_TILE_BYTE_COUNTS: | |
1436 | ✗ | s->tile_byte_counts_offset = off; | |
1437 | ✗ | break; | |
1438 | ✗ | case TIFF_TILE_LENGTH: | |
1439 | ✗ | if (value > INT_MAX) | |
1440 | ✗ | return AVERROR_INVALIDDATA; | |
1441 | ✗ | s->tile_length = value; | |
1442 | ✗ | break; | |
1443 | ✗ | case TIFF_TILE_WIDTH: | |
1444 | ✗ | if (value > INT_MAX) | |
1445 | ✗ | return AVERROR_INVALIDDATA; | |
1446 | ✗ | s->tile_width = value; | |
1447 | ✗ | break; | |
1448 | 8 | case TIFF_PREDICTOR: | |
1449 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (value > INT_MAX) |
1450 | ✗ | return AVERROR_INVALIDDATA; | |
1451 | 8 | s->predictor = value; | |
1452 | 8 | break; | |
1453 | ✗ | case TIFF_SUB_IFDS: | |
1454 | ✗ | if (count == 1) | |
1455 | ✗ | s->sub_ifd = value; | |
1456 | ✗ | else if (count > 1) | |
1457 | ✗ | s->sub_ifd = ff_tget_long(&s->gb, s->le); /** Only get the first SubIFD */ | |
1458 | ✗ | break; | |
1459 | ✗ | case TIFF_GRAY_RESPONSE_CURVE: | |
1460 | case DNG_LINEARIZATION_TABLE: | ||
1461 | ✗ | if (count < 1 || count > FF_ARRAY_ELEMS(s->dng_lut)) | |
1462 | ✗ | return AVERROR_INVALIDDATA; | |
1463 | ✗ | for (int i = 0; i < count; i++) | |
1464 | ✗ | s->dng_lut[i] = ff_tget(&s->gb, type, s->le); | |
1465 | ✗ | s->white_level = s->dng_lut[count-1]; | |
1466 | ✗ | break; | |
1467 | ✗ | case DNG_BLACK_LEVEL: | |
1468 | ✗ | if (count > FF_ARRAY_ELEMS(s->black_level)) | |
1469 | ✗ | return AVERROR_INVALIDDATA; | |
1470 | ✗ | s->black_level[0] = value / (float)value2; | |
1471 | ✗ | for (int i = 0; i < count && count > 1; i++) { | |
1472 | ✗ | if (type == TIFF_RATIONAL) { | |
1473 | ✗ | value = ff_tget_long(&s->gb, s->le); | |
1474 | ✗ | value2 = ff_tget_long(&s->gb, s->le); | |
1475 | ✗ | if (!value2) { | |
1476 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n"); | |
1477 | ✗ | value2 = 1; | |
1478 | } | ||
1479 | |||
1480 | ✗ | s->black_level[i] = value / (float)value2; | |
1481 | ✗ | } else if (type == TIFF_SRATIONAL) { | |
1482 | ✗ | int value = ff_tget_long(&s->gb, s->le); | |
1483 | ✗ | int value2 = ff_tget_long(&s->gb, s->le); | |
1484 | ✗ | if (!value2) { | |
1485 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n"); | |
1486 | ✗ | value2 = 1; | |
1487 | } | ||
1488 | |||
1489 | ✗ | s->black_level[i] = value / (float)value2; | |
1490 | } else { | ||
1491 | ✗ | s->black_level[i] = ff_tget(&s->gb, type, s->le); | |
1492 | } | ||
1493 | } | ||
1494 | ✗ | for (int i = count; i < 4 && count > 0; i++) | |
1495 | ✗ | s->black_level[i] = s->black_level[count - 1]; | |
1496 | ✗ | break; | |
1497 | ✗ | case DNG_WHITE_LEVEL: | |
1498 | ✗ | s->white_level = value; | |
1499 | ✗ | break; | |
1500 | ✗ | case TIFF_CFA_PATTERN_DIM: | |
1501 | ✗ | if (count != 2 || (ff_tget(&s->gb, type, s->le) != 2 && | |
1502 | ✗ | ff_tget(&s->gb, type, s->le) != 2)) { | |
1503 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "CFA Pattern dimensions are not 2x2\n"); | |
1504 | ✗ | return AVERROR_INVALIDDATA; | |
1505 | } | ||
1506 | ✗ | break; | |
1507 | ✗ | case TIFF_CFA_PATTERN: | |
1508 | ✗ | s->is_bayer = 1; | |
1509 | ✗ | s->pattern[0] = ff_tget(&s->gb, type, s->le); | |
1510 | ✗ | s->pattern[1] = ff_tget(&s->gb, type, s->le); | |
1511 | ✗ | s->pattern[2] = ff_tget(&s->gb, type, s->le); | |
1512 | ✗ | s->pattern[3] = ff_tget(&s->gb, type, s->le); | |
1513 | ✗ | break; | |
1514 | 33 | case TIFF_PHOTOMETRIC: | |
1515 |
1/3✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
33 | switch (value) { |
1516 | 33 | case TIFF_PHOTOMETRIC_WHITE_IS_ZERO: | |
1517 | case TIFF_PHOTOMETRIC_BLACK_IS_ZERO: | ||
1518 | case TIFF_PHOTOMETRIC_RGB: | ||
1519 | case TIFF_PHOTOMETRIC_PALETTE: | ||
1520 | case TIFF_PHOTOMETRIC_SEPARATED: | ||
1521 | case TIFF_PHOTOMETRIC_YCBCR: | ||
1522 | case TIFF_PHOTOMETRIC_CFA: | ||
1523 | case TIFF_PHOTOMETRIC_LINEAR_RAW: // Used by DNG images | ||
1524 | 33 | s->photometric = value; | |
1525 | 33 | break; | |
1526 | ✗ | case TIFF_PHOTOMETRIC_ALPHA_MASK: | |
1527 | case TIFF_PHOTOMETRIC_CIE_LAB: | ||
1528 | case TIFF_PHOTOMETRIC_ICC_LAB: | ||
1529 | case TIFF_PHOTOMETRIC_ITU_LAB: | ||
1530 | case TIFF_PHOTOMETRIC_LOG_L: | ||
1531 | case TIFF_PHOTOMETRIC_LOG_LUV: | ||
1532 | ✗ | avpriv_report_missing_feature(s->avctx, | |
1533 | "PhotometricInterpretation 0x%04X", | ||
1534 | value); | ||
1535 | ✗ | return AVERROR_PATCHWELCOME; | |
1536 | ✗ | default: | |
1537 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is " | |
1538 | "unknown\n", value); | ||
1539 | ✗ | return AVERROR_INVALIDDATA; | |
1540 | } | ||
1541 | 33 | break; | |
1542 | 16 | case TIFF_FILL_ORDER: | |
1543 |
2/4✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
|
16 | if (value < 1 || value > 2) { |
1544 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
1545 | "Unknown FillOrder value %d, trying default one\n", value); | ||
1546 | ✗ | value = 1; | |
1547 | } | ||
1548 | 16 | s->fill_order = value - 1; | |
1549 | 16 | break; | |
1550 | ✗ | case TIFF_PAL: { | |
1551 | GetByteContext pal_gb[3]; | ||
1552 | ✗ | off = type_sizes[type]; | |
1553 | ✗ | if (count / 3 > 256 || | |
1554 | ✗ | bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3) | |
1555 | ✗ | return AVERROR_INVALIDDATA; | |
1556 | |||
1557 | ✗ | pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb; | |
1558 | ✗ | bytestream2_skip(&pal_gb[1], count / 3 * off); | |
1559 | ✗ | bytestream2_skip(&pal_gb[2], count / 3 * off * 2); | |
1560 | |||
1561 | ✗ | off = (type_sizes[type] - 1) << 3; | |
1562 | ✗ | if (off > 31U) { | |
1563 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "palette shift %d is out of range\n", off); | |
1564 | ✗ | return AVERROR_INVALIDDATA; | |
1565 | } | ||
1566 | |||
1567 | ✗ | for (i = 0; i < count / 3; i++) { | |
1568 | ✗ | uint32_t p = 0xFF000000; | |
1569 | ✗ | p |= (ff_tget(&pal_gb[0], type, s->le) >> off) << 16; | |
1570 | ✗ | p |= (ff_tget(&pal_gb[1], type, s->le) >> off) << 8; | |
1571 | ✗ | p |= ff_tget(&pal_gb[2], type, s->le) >> off; | |
1572 | ✗ | s->palette[i] = p; | |
1573 | } | ||
1574 | ✗ | s->palette_is_set = 1; | |
1575 | ✗ | break; | |
1576 | } | ||
1577 | 18 | case TIFF_PLANAR: | |
1578 | 18 | s->planar = value == 2; | |
1579 | 18 | break; | |
1580 | ✗ | case TIFF_YCBCR_SUBSAMPLING: | |
1581 | ✗ | if (count != 2) { | |
1582 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "subsample count invalid\n"); | |
1583 | ✗ | return AVERROR_INVALIDDATA; | |
1584 | } | ||
1585 | ✗ | for (i = 0; i < count; i++) { | |
1586 | ✗ | s->subsampling[i] = ff_tget(&s->gb, type, s->le); | |
1587 | ✗ | if (s->subsampling[i] <= 0) { | |
1588 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "subsampling %d is invalid\n", s->subsampling[i]); | |
1589 | ✗ | s->subsampling[i] = 1; | |
1590 | ✗ | return AVERROR_INVALIDDATA; | |
1591 | } | ||
1592 | } | ||
1593 | ✗ | break; | |
1594 | ✗ | case TIFF_T4OPTIONS: | |
1595 | ✗ | if (s->compr == TIFF_G3) { | |
1596 | ✗ | if (value > INT_MAX) | |
1597 | ✗ | return AVERROR_INVALIDDATA; | |
1598 | ✗ | s->fax_opts = value; | |
1599 | } | ||
1600 | ✗ | break; | |
1601 | ✗ | case TIFF_T6OPTIONS: | |
1602 | ✗ | if (s->compr == TIFF_G4) { | |
1603 | ✗ | if (value > INT_MAX) | |
1604 | ✗ | return AVERROR_INVALIDDATA; | |
1605 | ✗ | s->fax_opts = value; | |
1606 | } | ||
1607 | ✗ | break; | |
1608 | #define ADD_METADATA(count, name, sep)\ | ||
1609 | if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\ | ||
1610 | av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\ | ||
1611 | goto end;\ | ||
1612 | } | ||
1613 | ✗ | case TIFF_MODEL_PIXEL_SCALE: | |
1614 | ✗ | ADD_METADATA(count, "ModelPixelScaleTag", NULL); | |
1615 | ✗ | break; | |
1616 | ✗ | case TIFF_MODEL_TRANSFORMATION: | |
1617 | ✗ | ADD_METADATA(count, "ModelTransformationTag", NULL); | |
1618 | ✗ | break; | |
1619 | ✗ | case TIFF_MODEL_TIEPOINT: | |
1620 | ✗ | ADD_METADATA(count, "ModelTiepointTag", NULL); | |
1621 | ✗ | break; | |
1622 | ✗ | case TIFF_GEO_KEY_DIRECTORY: | |
1623 | ✗ | if (s->geotag_count) { | |
1624 | ✗ | avpriv_request_sample(s->avctx, "Multiple geo key directories"); | |
1625 | ✗ | return AVERROR_INVALIDDATA; | |
1626 | } | ||
1627 | ✗ | ADD_METADATA(1, "GeoTIFF_Version", NULL); | |
1628 | ✗ | ADD_METADATA(2, "GeoTIFF_Key_Revision", "."); | |
1629 | ✗ | s->geotag_count = ff_tget_short(&s->gb, s->le); | |
1630 | ✗ | if (s->geotag_count > count / 4 - 1) { | |
1631 | ✗ | s->geotag_count = count / 4 - 1; | |
1632 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n"); | |
1633 | } | ||
1634 | ✗ | if ( bytestream2_get_bytes_left(&s->gb) < s->geotag_count * sizeof(int16_t) * 4 | |
1635 | ✗ | || s->geotag_count == 0) { | |
1636 | ✗ | s->geotag_count = 0; | |
1637 | ✗ | return -1; | |
1638 | } | ||
1639 | ✗ | s->geotags = av_calloc(s->geotag_count, sizeof(*s->geotags)); | |
1640 | ✗ | if (!s->geotags) { | |
1641 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n"); | |
1642 | ✗ | s->geotag_count = 0; | |
1643 | ✗ | goto end; | |
1644 | } | ||
1645 | ✗ | for (i = 0; i < s->geotag_count; i++) { | |
1646 | unsigned val; | ||
1647 | ✗ | s->geotags[i].key = ff_tget_short(&s->gb, s->le); | |
1648 | ✗ | s->geotags[i].type = ff_tget_short(&s->gb, s->le); | |
1649 | ✗ | s->geotags[i].count = ff_tget_short(&s->gb, s->le); | |
1650 | ✗ | val = ff_tget_short(&s->gb, s->le); | |
1651 | |||
1652 | ✗ | if (!s->geotags[i].type) { | |
1653 | ✗ | const char *str = get_geokey_val(s->geotags[i].key, val); | |
1654 | |||
1655 | ✗ | s->geotags[i].val = str ? av_strdup(str) : av_asprintf("Unknown-%u", val); | |
1656 | ✗ | if (!s->geotags[i].val) | |
1657 | ✗ | return AVERROR(ENOMEM); | |
1658 | } else | ||
1659 | ✗ | s->geotags[i].offset = val; | |
1660 | } | ||
1661 | ✗ | break; | |
1662 | ✗ | case TIFF_GEO_DOUBLE_PARAMS: | |
1663 | ✗ | if (count >= INT_MAX / sizeof(int64_t)) | |
1664 | ✗ | return AVERROR_INVALIDDATA; | |
1665 | ✗ | if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t)) | |
1666 | ✗ | return AVERROR_INVALIDDATA; | |
1667 | ✗ | dp = av_malloc_array(count, sizeof(double)); | |
1668 | ✗ | if (!dp) { | |
1669 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n"); | |
1670 | ✗ | goto end; | |
1671 | } | ||
1672 | ✗ | for (i = 0; i < count; i++) | |
1673 | ✗ | dp[i] = ff_tget_double(&s->gb, s->le); | |
1674 | ✗ | for (i = 0; i < s->geotag_count; i++) { | |
1675 | ✗ | if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) { | |
1676 | ✗ | if (s->geotags[i].count == 0 | |
1677 | ✗ | || s->geotags[i].offset + s->geotags[i].count > count) { | |
1678 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key); | |
1679 | ✗ | } else if (s->geotags[i].val) { | |
1680 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "Duplicate GeoTIFF key %d\n", s->geotags[i].key); | |
1681 | } else { | ||
1682 | ✗ | char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", "); | |
1683 | ✗ | if (!ap) { | |
1684 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n"); | |
1685 | ✗ | av_freep(&dp); | |
1686 | ✗ | return AVERROR(ENOMEM); | |
1687 | } | ||
1688 | ✗ | s->geotags[i].val = ap; | |
1689 | } | ||
1690 | } | ||
1691 | } | ||
1692 | ✗ | av_freep(&dp); | |
1693 | ✗ | break; | |
1694 | ✗ | case TIFF_GEO_ASCII_PARAMS: | |
1695 | ✗ | pos = bytestream2_tell(&s->gb); | |
1696 | ✗ | for (i = 0; i < s->geotag_count; i++) { | |
1697 | ✗ | if (s->geotags[i].type == TIFF_GEO_ASCII_PARAMS) { | |
1698 | ✗ | if (s->geotags[i].count == 0 | |
1699 | ✗ | || s->geotags[i].offset + s->geotags[i].count > count) { | |
1700 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key); | |
1701 | } else { | ||
1702 | char *ap; | ||
1703 | |||
1704 | ✗ | bytestream2_seek(&s->gb, pos + s->geotags[i].offset, SEEK_SET); | |
1705 | ✗ | if (bytestream2_get_bytes_left(&s->gb) < s->geotags[i].count) | |
1706 | ✗ | return AVERROR_INVALIDDATA; | |
1707 | ✗ | if (s->geotags[i].val) | |
1708 | ✗ | return AVERROR_INVALIDDATA; | |
1709 | ✗ | ap = av_malloc(s->geotags[i].count); | |
1710 | ✗ | if (!ap) { | |
1711 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n"); | |
1712 | ✗ | return AVERROR(ENOMEM); | |
1713 | } | ||
1714 | ✗ | bytestream2_get_bufferu(&s->gb, ap, s->geotags[i].count); | |
1715 | ✗ | ap[s->geotags[i].count - 1] = '\0'; //replace the "|" delimiter with a 0 byte | |
1716 | ✗ | s->geotags[i].val = ap; | |
1717 | } | ||
1718 | } | ||
1719 | } | ||
1720 | ✗ | break; | |
1721 | ✗ | case TIFF_ICC_PROFILE: | |
1722 | ✗ | gb_temp = s->gb; | |
1723 | ✗ | bytestream2_seek(&gb_temp, off, SEEK_SET); | |
1724 | |||
1725 | ✗ | if (bytestream2_get_bytes_left(&gb_temp) < count) | |
1726 | ✗ | return AVERROR_INVALIDDATA; | |
1727 | |||
1728 | ✗ | ret = ff_frame_new_side_data(s->avctx, frame, AV_FRAME_DATA_ICC_PROFILE, count, &sd); | |
1729 | ✗ | if (ret < 0) | |
1730 | ✗ | return ret; | |
1731 | ✗ | if (sd) | |
1732 | ✗ | bytestream2_get_bufferu(&gb_temp, sd->data, count); | |
1733 | ✗ | break; | |
1734 | ✗ | case TIFF_ARTIST: | |
1735 | ✗ | ADD_METADATA(count, "artist", NULL); | |
1736 | ✗ | break; | |
1737 | ✗ | case TIFF_COPYRIGHT: | |
1738 | ✗ | ADD_METADATA(count, "copyright", NULL); | |
1739 | ✗ | break; | |
1740 | 2 | case TIFF_DATE: | |
1741 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | ADD_METADATA(count, "date", NULL); |
1742 | 2 | break; | |
1743 | 6 | case TIFF_DOCUMENT_NAME: | |
1744 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
6 | ADD_METADATA(count, "document_name", NULL); |
1745 | 6 | break; | |
1746 | ✗ | case TIFF_HOST_COMPUTER: | |
1747 | ✗ | ADD_METADATA(count, "computer", NULL); | |
1748 | ✗ | break; | |
1749 | 4 | case TIFF_IMAGE_DESCRIPTION: | |
1750 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | ADD_METADATA(count, "description", NULL); |
1751 | 4 | break; | |
1752 | ✗ | case TIFF_MAKE: | |
1753 | ✗ | ADD_METADATA(count, "make", NULL); | |
1754 | ✗ | break; | |
1755 | ✗ | case TIFF_MODEL: | |
1756 | ✗ | ADD_METADATA(count, "model", NULL); | |
1757 | ✗ | break; | |
1758 | ✗ | case TIFF_PAGE_NAME: | |
1759 | ✗ | ADD_METADATA(count, "page_name", NULL); | |
1760 | ✗ | break; | |
1761 | 12 | case TIFF_PAGE_NUMBER: | |
1762 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | ADD_METADATA(count, "page_number", " / "); |
1763 | // need to seek back to re-read the page number | ||
1764 | 12 | bytestream2_seek(&s->gb, -count * sizeof(uint16_t), SEEK_CUR); | |
1765 | // read the page number | ||
1766 | 12 | s->cur_page = ff_tget_short(&s->gb, s->le); | |
1767 | // get back to where we were before the previous seek | ||
1768 | 12 | bytestream2_seek(&s->gb, count * sizeof(uint16_t) - sizeof(uint16_t), SEEK_CUR); | |
1769 | 12 | break; | |
1770 | 2 | case TIFF_SOFTWARE_NAME: | |
1771 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | ADD_METADATA(count, "software", NULL); |
1772 | 2 | break; | |
1773 | ✗ | case DNG_VERSION: | |
1774 | ✗ | if (count == 4) { | |
1775 | unsigned int ver[4]; | ||
1776 | ✗ | ver[0] = ff_tget(&s->gb, type, s->le); | |
1777 | ✗ | ver[1] = ff_tget(&s->gb, type, s->le); | |
1778 | ✗ | ver[2] = ff_tget(&s->gb, type, s->le); | |
1779 | ✗ | ver[3] = ff_tget(&s->gb, type, s->le); | |
1780 | |||
1781 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, "DNG file, version %u.%u.%u.%u\n", | |
1782 | ver[0], ver[1], ver[2], ver[3]); | ||
1783 | |||
1784 | ✗ | tiff_set_type(s, TIFF_TYPE_DNG); | |
1785 | } | ||
1786 | ✗ | break; | |
1787 | ✗ | case DNG_ANALOG_BALANCE: | |
1788 | ✗ | if (type != TIFF_RATIONAL) | |
1789 | ✗ | break; | |
1790 | |||
1791 | ✗ | for (int i = 0; i < 3; i++) { | |
1792 | ✗ | value = ff_tget_long(&s->gb, s->le); | |
1793 | ✗ | value2 = ff_tget_long(&s->gb, s->le); | |
1794 | ✗ | if (!value2) { | |
1795 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n"); | |
1796 | ✗ | value2 = 1; | |
1797 | } | ||
1798 | |||
1799 | ✗ | s->analog_balance[i] = value / (float)value2; | |
1800 | } | ||
1801 | ✗ | break; | |
1802 | ✗ | case DNG_AS_SHOT_NEUTRAL: | |
1803 | ✗ | if (type != TIFF_RATIONAL) | |
1804 | ✗ | break; | |
1805 | |||
1806 | ✗ | for (int i = 0; i < 3; i++) { | |
1807 | ✗ | value = ff_tget_long(&s->gb, s->le); | |
1808 | ✗ | value2 = ff_tget_long(&s->gb, s->le); | |
1809 | ✗ | if (!value2) { | |
1810 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n"); | |
1811 | ✗ | value2 = 1; | |
1812 | } | ||
1813 | |||
1814 | ✗ | s->as_shot_neutral[i] = value / (float)value2; | |
1815 | } | ||
1816 | ✗ | break; | |
1817 | ✗ | case DNG_AS_SHOT_WHITE_XY: | |
1818 | ✗ | if (type != TIFF_RATIONAL) | |
1819 | ✗ | break; | |
1820 | |||
1821 | ✗ | for (int i = 0; i < 2; i++) { | |
1822 | ✗ | value = ff_tget_long(&s->gb, s->le); | |
1823 | ✗ | value2 = ff_tget_long(&s->gb, s->le); | |
1824 | ✗ | if (!value2) { | |
1825 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n"); | |
1826 | ✗ | value2 = 1; | |
1827 | } | ||
1828 | |||
1829 | ✗ | s->as_shot_white[i] = value / (float)value2; | |
1830 | } | ||
1831 | ✗ | s->as_shot_white[2] = 1.f - s->as_shot_white[0] - s->as_shot_white[1]; | |
1832 | ✗ | for (int i = 0; i < 3; i++) { | |
1833 | ✗ | s->as_shot_white[i] /= d65_white[i]; | |
1834 | } | ||
1835 | ✗ | break; | |
1836 | ✗ | case DNG_COLOR_MATRIX1: | |
1837 | case DNG_COLOR_MATRIX2: | ||
1838 | ✗ | for (int i = 0; i < 3; i++) { | |
1839 | ✗ | for (int j = 0; j < 3; j++) { | |
1840 | ✗ | int value = ff_tget_long(&s->gb, s->le); | |
1841 | ✗ | int value2 = ff_tget_long(&s->gb, s->le); | |
1842 | ✗ | if (!value2) { | |
1843 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n"); | |
1844 | ✗ | value2 = 1; | |
1845 | } | ||
1846 | ✗ | s->color_matrix[i][j] = value / (float)value2; | |
1847 | } | ||
1848 | ✗ | s->use_color_matrix = 1; | |
1849 | } | ||
1850 | ✗ | break; | |
1851 | ✗ | case DNG_CAMERA_CALIBRATION1: | |
1852 | case DNG_CAMERA_CALIBRATION2: | ||
1853 | ✗ | for (int i = 0; i < 3; i++) { | |
1854 | ✗ | for (int j = 0; j < 3; j++) { | |
1855 | ✗ | int value = ff_tget_long(&s->gb, s->le); | |
1856 | ✗ | int value2 = ff_tget_long(&s->gb, s->le); | |
1857 | ✗ | if (!value2) { | |
1858 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "Invalid denominator\n"); | |
1859 | ✗ | value2 = 1; | |
1860 | } | ||
1861 | ✗ | s->camera_calibration[i][j] = value / (float)value2; | |
1862 | } | ||
1863 | } | ||
1864 | ✗ | break; | |
1865 | ✗ | case CINEMADNG_TIME_CODES: | |
1866 | case CINEMADNG_FRAME_RATE: | ||
1867 | case CINEMADNG_T_STOP: | ||
1868 | case CINEMADNG_REEL_NAME: | ||
1869 | case CINEMADNG_CAMERA_LABEL: | ||
1870 | ✗ | tiff_set_type(s, TIFF_TYPE_CINEMADNG); | |
1871 | ✗ | break; | |
1872 | 97 | default: | |
1873 |
1/2✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
|
97 | if (s->avctx->err_recognition & AV_EF_EXPLODE) { |
1874 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
1875 | "Unknown or unsupported tag %d/0x%0X\n", | ||
1876 | tag, tag); | ||
1877 | ✗ | return AVERROR_INVALIDDATA; | |
1878 | } | ||
1879 | } | ||
1880 | 97 | end: | |
1881 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 519 times.
|
519 | if (s->bpp > 128U) { |
1882 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
1883 | "This format is not supported (bpp=%d, %d components)\n", | ||
1884 | s->bpp, count); | ||
1885 | ✗ | s->bpp = 0; | |
1886 | ✗ | return AVERROR_INVALIDDATA; | |
1887 | } | ||
1888 | 519 | bytestream2_seek(&s->gb, start, SEEK_SET); | |
1889 | 519 | return 0; | |
1890 | } | ||
1891 | |||
1892 | static const float xyz2rgb[3][3] = { | ||
1893 | { 0.412453f, 0.357580f, 0.180423f }, | ||
1894 | { 0.212671f, 0.715160f, 0.072169f }, | ||
1895 | { 0.019334f, 0.119193f, 0.950227f }, | ||
1896 | }; | ||
1897 | |||
1898 | ✗ | static void camera_xyz_coeff(TiffContext *s, | |
1899 | float rgb2cam[3][4], | ||
1900 | double cam2xyz[4][3]) | ||
1901 | { | ||
1902 | double cam2rgb[4][3], num; | ||
1903 | int i, j, k; | ||
1904 | |||
1905 | ✗ | for (i = 0; i < 3; i++) { | |
1906 | ✗ | for (j = 0; j < 3; j++) { | |
1907 | ✗ | cam2rgb[i][j] = 0.; | |
1908 | ✗ | for (k = 0; k < 3; k++) | |
1909 | ✗ | cam2rgb[i][j] += cam2xyz[i][k] * xyz2rgb[k][j]; | |
1910 | } | ||
1911 | } | ||
1912 | |||
1913 | ✗ | for (i = 0; i < 3; i++) { | |
1914 | ✗ | for (num = j = 0; j < 3; j++) | |
1915 | ✗ | num += cam2rgb[i][j]; | |
1916 | ✗ | if (!num) | |
1917 | ✗ | num = 1; | |
1918 | ✗ | for (j = 0; j < 3; j++) | |
1919 | ✗ | cam2rgb[i][j] /= num; | |
1920 | ✗ | s->premultiply[i] = 1.f / num; | |
1921 | } | ||
1922 | ✗ | } | |
1923 | |||
1924 | 33 | static int decode_frame(AVCodecContext *avctx, AVFrame *p, | |
1925 | int *got_frame, AVPacket *avpkt) | ||
1926 | { | ||
1927 | 33 | TiffContext *const s = avctx->priv_data; | |
1928 | 33 | unsigned off, last_off = 0; | |
1929 | int le, ret, plane, planes; | ||
1930 | int i, j, entries, stride; | ||
1931 | unsigned soff, ssize; | ||
1932 | uint8_t *dst; | ||
1933 | GetByteContext stripsizes; | ||
1934 | GetByteContext stripdata; | ||
1935 | int retry_for_subifd, retry_for_page; | ||
1936 | int is_dng; | ||
1937 | int has_tile_bits, has_strip_bits; | ||
1938 | |||
1939 | 33 | bytestream2_init(&s->gb, avpkt->data, avpkt->size); | |
1940 | |||
1941 | // parse image header | ||
1942 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
|
33 | if ((ret = ff_tdecode_header(&s->gb, &le, &off))) { |
1943 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid TIFF header\n"); | |
1944 | ✗ | return ret; | |
1945 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
|
33 | } else if (off >= UINT_MAX - 14 || avpkt->size < off + 14) { |
1946 | ✗ | av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n"); | |
1947 | ✗ | return AVERROR_INVALIDDATA; | |
1948 | } | ||
1949 | 33 | s->le = le; | |
1950 | // TIFF_BPP is not a required tag and defaults to 1 | ||
1951 | |||
1952 | 33 | s->tiff_type = TIFF_TYPE_TIFF; | |
1953 | 33 | s->use_color_matrix = 0; | |
1954 | 33 | again: | |
1955 | 33 | s->is_thumbnail = 0; | |
1956 | 33 | s->bppcount = s->bpp = 1; | |
1957 | 33 | s->photometric = TIFF_PHOTOMETRIC_NONE; | |
1958 | 33 | s->compr = TIFF_RAW; | |
1959 | 33 | s->fill_order = 0; | |
1960 | 33 | s->white_level = 0; | |
1961 | 33 | s->is_bayer = 0; | |
1962 | 33 | s->is_tiled = 0; | |
1963 | 33 | s->is_jpeg = 0; | |
1964 | 33 | s->cur_page = 0; | |
1965 | 33 | s->last_tag = 0; | |
1966 | |||
1967 |
2/2✓ Branch 0 taken 2162688 times.
✓ Branch 1 taken 33 times.
|
2162721 | for (i = 0; i < 65536; i++) |
1968 | 2162688 | s->dng_lut[i] = i; | |
1969 | |||
1970 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 33 times.
|
165 | for (i = 0; i < FF_ARRAY_ELEMS(s->black_level); i++) |
1971 | 132 | s->black_level[i] = 0.f; | |
1972 | |||
1973 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 33 times.
|
165 | for (i = 0; i < FF_ARRAY_ELEMS(s->as_shot_neutral); i++) |
1974 | 132 | s->as_shot_neutral[i] = 0.f; | |
1975 | |||
1976 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 33 times.
|
165 | for (i = 0; i < FF_ARRAY_ELEMS(s->as_shot_white); i++) |
1977 | 132 | s->as_shot_white[i] = 1.f; | |
1978 | |||
1979 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 33 times.
|
165 | for (i = 0; i < FF_ARRAY_ELEMS(s->analog_balance); i++) |
1980 | 132 | s->analog_balance[i] = 1.f; | |
1981 | |||
1982 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 33 times.
|
165 | for (i = 0; i < FF_ARRAY_ELEMS(s->premultiply); i++) |
1983 | 132 | s->premultiply[i] = 1.f; | |
1984 | |||
1985 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 33 times.
|
165 | for (i = 0; i < 4; i++) |
1986 |
2/2✓ Branch 0 taken 528 times.
✓ Branch 1 taken 132 times.
|
660 | for (j = 0; j < 4; j++) |
1987 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 396 times.
|
528 | s->camera_calibration[i][j] = i == j; |
1988 | |||
1989 | 33 | free_geotags(s); | |
1990 | |||
1991 | // Reset these offsets so we can tell if they were set this frame | ||
1992 | 33 | s->stripsizesoff = s->strippos = 0; | |
1993 | /* parse image file directory */ | ||
1994 | 33 | bytestream2_seek(&s->gb, off, SEEK_SET); | |
1995 | 33 | entries = ff_tget_short(&s->gb, le); | |
1996 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
|
33 | if (bytestream2_get_bytes_left(&s->gb) < entries * 12) |
1997 | ✗ | return AVERROR_INVALIDDATA; | |
1998 |
2/2✓ Branch 0 taken 519 times.
✓ Branch 1 taken 33 times.
|
552 | for (i = 0; i < entries; i++) { |
1999 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 519 times.
|
519 | if ((ret = tiff_decode_tag(s, p)) < 0) |
2000 | ✗ | return ret; | |
2001 | } | ||
2002 | |||
2003 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
33 | if (s->get_thumbnail && !s->is_thumbnail) { |
2004 | ✗ | av_log(avctx, AV_LOG_INFO, "No embedded thumbnail present\n"); | |
2005 | ✗ | return AVERROR_EOF; | |
2006 | } | ||
2007 | |||
2008 | /** whether we should process this IFD's SubIFD */ | ||
2009 |
1/8✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
33 | retry_for_subifd = s->sub_ifd && (s->get_subimage || (!s->get_thumbnail && s->is_thumbnail)); |
2010 | /** whether we should process this multi-page IFD's next page */ | ||
2011 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
33 | retry_for_page = s->get_page && s->cur_page + 1 < s->get_page; // get_page is 1-indexed |
2012 | |||
2013 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | if (retry_for_page) { |
2014 | // set offset to the next IFD | ||
2015 | ✗ | off = ff_tget_long(&s->gb, le); | |
2016 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | } else if (retry_for_subifd) { |
2017 | // set offset to the SubIFD | ||
2018 | ✗ | off = s->sub_ifd; | |
2019 | } | ||
2020 | |||
2021 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
|
33 | if (retry_for_subifd || retry_for_page) { |
2022 | ✗ | if (!off) { | |
2023 | ✗ | av_log(avctx, AV_LOG_ERROR, "Requested entry not found\n"); | |
2024 | ✗ | return AVERROR_INVALIDDATA; | |
2025 | } | ||
2026 | ✗ | if (off <= last_off) { | |
2027 | ✗ | avpriv_request_sample(s->avctx, "non increasing IFD offset"); | |
2028 | ✗ | return AVERROR_INVALIDDATA; | |
2029 | } | ||
2030 | ✗ | last_off = off; | |
2031 | ✗ | if (off >= UINT_MAX - 14 || avpkt->size < off + 14) { | |
2032 | ✗ | av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n"); | |
2033 | ✗ | return AVERROR_INVALIDDATA; | |
2034 | } | ||
2035 | ✗ | s->sub_ifd = 0; | |
2036 | ✗ | goto again; | |
2037 | } | ||
2038 | |||
2039 | /* At this point we've decided on which (Sub)IFD to process */ | ||
2040 | |||
2041 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
|
33 | is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG); |
2042 | |||
2043 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | for (i = 0; i<s->geotag_count; i++) { |
2044 | ✗ | const char *keyname = get_geokey_name(s->geotags[i].key); | |
2045 | ✗ | if (!keyname) { | |
2046 | ✗ | av_log(avctx, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[i].key); | |
2047 | ✗ | continue; | |
2048 | } | ||
2049 | ✗ | if (get_geokey_type(s->geotags[i].key) != s->geotags[i].type) { | |
2050 | ✗ | av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key); | |
2051 | ✗ | continue; | |
2052 | } | ||
2053 | ✗ | ret = av_dict_set(&p->metadata, keyname, s->geotags[i].val, AV_DICT_DONT_STRDUP_VAL); | |
2054 | ✗ | s->geotags[i].val = NULL; | |
2055 | ✗ | if (ret<0) { | |
2056 | ✗ | av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname); | |
2057 | ✗ | return ret; | |
2058 | } | ||
2059 | } | ||
2060 | |||
2061 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | if (is_dng) { |
2062 | double cam2xyz[4][3]; | ||
2063 | float cmatrix[3][4]; | ||
2064 | ✗ | float pmin = FLT_MAX; | |
2065 | int bps; | ||
2066 | |||
2067 | ✗ | for (i = 0; i < 3; i++) { | |
2068 | ✗ | for (j = 0; j < 3; j++) | |
2069 | ✗ | s->camera_calibration[i][j] *= s->analog_balance[i]; | |
2070 | } | ||
2071 | |||
2072 | ✗ | if (!s->use_color_matrix) { | |
2073 | ✗ | for (i = 0; i < 3; i++) { | |
2074 | ✗ | if (s->camera_calibration[i][i]) | |
2075 | ✗ | s->premultiply[i] /= s->camera_calibration[i][i]; | |
2076 | } | ||
2077 | } else { | ||
2078 | ✗ | for (int c = 0; c < 3; c++) { | |
2079 | ✗ | for (i = 0; i < 3; i++) { | |
2080 | ✗ | cam2xyz[c][i] = 0.; | |
2081 | ✗ | for (j = 0; j < 3; j++) | |
2082 | ✗ | cam2xyz[c][i] += s->camera_calibration[c][j] * s->color_matrix[j][i] * s->as_shot_white[i]; | |
2083 | } | ||
2084 | } | ||
2085 | |||
2086 | ✗ | camera_xyz_coeff(s, cmatrix, cam2xyz); | |
2087 | } | ||
2088 | |||
2089 | ✗ | for (int c = 0; c < 3; c++) | |
2090 | ✗ | pmin = fminf(pmin, s->premultiply[c]); | |
2091 | |||
2092 | ✗ | for (int c = 0; c < 3; c++) | |
2093 | ✗ | s->premultiply[c] /= pmin; | |
2094 | |||
2095 | ✗ | if (s->bpp % s->bppcount) | |
2096 | ✗ | return AVERROR_INVALIDDATA; | |
2097 | ✗ | bps = s->bpp / s->bppcount; | |
2098 | ✗ | if (bps < 8 || bps > 32) | |
2099 | ✗ | return AVERROR_INVALIDDATA; | |
2100 | |||
2101 | ✗ | if (s->white_level == 0) | |
2102 | ✗ | s->white_level = (1LL << bps) - 1; /* Default value as per the spec */ | |
2103 | |||
2104 | ✗ | if (s->white_level <= s->black_level[0]) { | |
2105 | ✗ | av_log(avctx, AV_LOG_ERROR, "BlackLevel (%g) must be less than WhiteLevel (%"PRId32")\n", | |
2106 | ✗ | s->black_level[0], s->white_level); | |
2107 | ✗ | return AVERROR_INVALIDDATA; | |
2108 | } | ||
2109 | |||
2110 | ✗ | if (s->planar) | |
2111 | ✗ | return AVERROR_PATCHWELCOME; | |
2112 | } | ||
2113 | |||
2114 |
4/6✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 19 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 14 times.
|
33 | if (!s->is_tiled && !s->strippos && !s->stripoff) { |
2115 | ✗ | av_log(avctx, AV_LOG_ERROR, "Image data is missing\n"); | |
2116 | ✗ | return AVERROR_INVALIDDATA; | |
2117 | } | ||
2118 | |||
2119 |
5/10✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 33 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 33 times.
|
33 | has_tile_bits = s->is_tiled || s->tile_byte_counts_offset || s->tile_offsets_offset || s->tile_width || s->tile_length; |
2120 |
3/16✓ Branch 0 taken 14 times.
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
33 | has_strip_bits = s->strippos || s->strips || s->stripoff || s->rps || s->sot || s->sstype || s->stripsize || s->stripsizesoff; |
2121 | |||
2122 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
33 | if (has_tile_bits && has_strip_bits) { |
2123 | ✗ | int tiled_dng = s->is_tiled && is_dng; | |
2124 | ✗ | av_log(avctx, tiled_dng ? AV_LOG_WARNING : AV_LOG_ERROR, "Tiled TIFF is not allowed to strip\n"); | |
2125 | ✗ | if (!tiled_dng) | |
2126 | ✗ | return AVERROR_INVALIDDATA; | |
2127 | } | ||
2128 | |||
2129 | /* now we have the data and may start decoding */ | ||
2130 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 2 taken 22 times.
|
33 | if ((ret = init_image(s, p)) <= 0) |
2131 | 11 | return ret; | |
2132 | |||
2133 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
22 | if (!s->is_tiled || has_strip_bits) { |
2134 |
3/4✓ Branch 0 taken 7 times.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
|
22 | if (s->strips == 1 && !s->stripsize) { |
2135 | ✗ | av_log(avctx, AV_LOG_WARNING, "Image data size missing\n"); | |
2136 | ✗ | s->stripsize = avpkt->size - s->stripoff; | |
2137 | } | ||
2138 | |||
2139 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 7 times.
|
22 | if (s->stripsizesoff) { |
2140 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (s->stripsizesoff >= (unsigned)avpkt->size) |
2141 | ✗ | return AVERROR_INVALIDDATA; | |
2142 | 15 | bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff, | |
2143 | 15 | avpkt->size - s->stripsizesoff); | |
2144 | } | ||
2145 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 7 times.
|
22 | if (s->strippos) { |
2146 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (s->strippos >= (unsigned)avpkt->size) |
2147 | ✗ | return AVERROR_INVALIDDATA; | |
2148 | 15 | bytestream2_init(&stripdata, avpkt->data + s->strippos, | |
2149 | 15 | avpkt->size - s->strippos); | |
2150 | } | ||
2151 | |||
2152 |
2/4✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
|
22 | if (s->rps <= 0 || s->rps % s->subsampling[1]) { |
2153 | ✗ | av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps); | |
2154 | ✗ | return AVERROR_INVALIDDATA; | |
2155 | } | ||
2156 | } | ||
2157 | |||
2158 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | if (s->photometric == TIFF_PHOTOMETRIC_LINEAR_RAW || |
2159 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | s->photometric == TIFF_PHOTOMETRIC_CFA) { |
2160 | ✗ | p->color_trc = AVCOL_TRC_LINEAR; | |
2161 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | } else if (s->photometric == TIFF_PHOTOMETRIC_BLACK_IS_ZERO) { |
2162 | ✗ | p->color_trc = AVCOL_TRC_GAMMA22; | |
2163 | } | ||
2164 | |||
2165 | /* Handle DNG images with JPEG-compressed tiles */ | ||
2166 | |||
2167 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
22 | if (is_dng && s->is_tiled) { |
2168 | ✗ | if (!s->is_jpeg) { | |
2169 | ✗ | avpriv_report_missing_feature(avctx, "DNG uncompressed tiled images"); | |
2170 | ✗ | return AVERROR_PATCHWELCOME; | |
2171 | ✗ | } else if (!s->is_bayer) { | |
2172 | ✗ | avpriv_report_missing_feature(avctx, "DNG JPG-compressed tiled non-bayer-encoded images"); | |
2173 | ✗ | return AVERROR_PATCHWELCOME; | |
2174 | } else { | ||
2175 | ✗ | if ((ret = dng_decode_tiles(avctx, p, avpkt)) > 0) | |
2176 | ✗ | *got_frame = 1; | |
2177 | ✗ | return ret; | |
2178 | } | ||
2179 | } | ||
2180 | |||
2181 | /* Handle TIFF images and DNG images with uncompressed strips (non-tiled) */ | ||
2182 | |||
2183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | planes = s->planar ? s->bppcount : 1; |
2184 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 22 times.
|
44 | for (plane = 0; plane < planes; plane++) { |
2185 | 22 | uint8_t *five_planes = NULL; | |
2186 | 22 | int remaining = avpkt->size; | |
2187 | int decoded_height; | ||
2188 | 22 | stride = p->linesize[plane]; | |
2189 | 22 | dst = p->data[plane]; | |
2190 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED && |
2191 | ✗ | s->avctx->pix_fmt == AV_PIX_FMT_RGBA) { | |
2192 | ✗ | stride = stride * 5 / 4; | |
2193 | ✗ | five_planes = | |
2194 | ✗ | dst = av_malloc(stride * s->height); | |
2195 | ✗ | if (!dst) | |
2196 | ✗ | return AVERROR(ENOMEM); | |
2197 | } | ||
2198 |
2/2✓ Branch 0 taken 579 times.
✓ Branch 1 taken 22 times.
|
601 | for (i = 0; i < s->height; i += s->rps) { |
2199 |
2/2✓ Branch 0 taken 557 times.
✓ Branch 1 taken 22 times.
|
579 | if (i) |
2200 | 557 | dst += s->rps * stride; | |
2201 |
2/2✓ Branch 0 taken 572 times.
✓ Branch 1 taken 7 times.
|
579 | if (s->stripsizesoff) |
2202 | 572 | ssize = ff_tget(&stripsizes, s->sstype, le); | |
2203 | else | ||
2204 | 7 | ssize = s->stripsize; | |
2205 | |||
2206 |
2/2✓ Branch 0 taken 572 times.
✓ Branch 1 taken 7 times.
|
579 | if (s->strippos) |
2207 | 572 | soff = ff_tget(&stripdata, s->sot, le); | |
2208 | else | ||
2209 | 7 | soff = s->stripoff; | |
2210 | |||
2211 |
3/6✓ Branch 0 taken 579 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 579 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 579 times.
|
579 | if (soff > avpkt->size || ssize > avpkt->size - soff || ssize > remaining) { |
2212 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n"); | |
2213 | ✗ | av_freep(&five_planes); | |
2214 | ✗ | return AVERROR_INVALIDDATA; | |
2215 | } | ||
2216 | 579 | remaining -= ssize; | |
2217 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
|
579 | if ((ret = tiff_unpack_strip(s, p, dst, stride, avpkt->data + soff, ssize, i, |
2218 | 579 | FFMIN(s->rps, s->height - i))) < 0) { | |
2219 | ✗ | if (avctx->err_recognition & AV_EF_EXPLODE) { | |
2220 | ✗ | av_freep(&five_planes); | |
2221 | ✗ | return ret; | |
2222 | } | ||
2223 | ✗ | break; | |
2224 | } | ||
2225 | } | ||
2226 | 22 | decoded_height = FFMIN(i, s->height); | |
2227 | |||
2228 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (s->predictor == 2) { |
2229 | ✗ | if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) { | |
2230 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "predictor == 2 with YUV is unsupported"); | |
2231 | ✗ | return AVERROR_PATCHWELCOME; | |
2232 | } | ||
2233 | ✗ | dst = five_planes ? five_planes : p->data[plane]; | |
2234 | ✗ | soff = s->bpp >> 3; | |
2235 | ✗ | if (s->planar) | |
2236 | ✗ | soff = FFMAX(soff / s->bppcount, 1); | |
2237 | ✗ | ssize = s->width * soff; | |
2238 | ✗ | if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE || | |
2239 | ✗ | s->avctx->pix_fmt == AV_PIX_FMT_RGBA64LE || | |
2240 | ✗ | s->avctx->pix_fmt == AV_PIX_FMT_GRAY16LE || | |
2241 | ✗ | s->avctx->pix_fmt == AV_PIX_FMT_YA16LE || | |
2242 | ✗ | s->avctx->pix_fmt == AV_PIX_FMT_GBRP16LE || | |
2243 | ✗ | s->avctx->pix_fmt == AV_PIX_FMT_GBRAP16LE) { | |
2244 | ✗ | for (i = 0; i < decoded_height; i++) { | |
2245 | ✗ | for (j = soff; j < ssize; j += 2) | |
2246 | ✗ | AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff)); | |
2247 | ✗ | dst += stride; | |
2248 | } | ||
2249 | ✗ | } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE || | |
2250 | ✗ | s->avctx->pix_fmt == AV_PIX_FMT_RGBA64BE || | |
2251 | ✗ | s->avctx->pix_fmt == AV_PIX_FMT_GRAY16BE || | |
2252 | ✗ | s->avctx->pix_fmt == AV_PIX_FMT_YA16BE || | |
2253 | ✗ | s->avctx->pix_fmt == AV_PIX_FMT_GBRP16BE || | |
2254 | ✗ | s->avctx->pix_fmt == AV_PIX_FMT_GBRAP16BE) { | |
2255 | ✗ | for (i = 0; i < decoded_height; i++) { | |
2256 | ✗ | for (j = soff; j < ssize; j += 2) | |
2257 | ✗ | AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff)); | |
2258 | ✗ | dst += stride; | |
2259 | } | ||
2260 | } else { | ||
2261 | ✗ | for (i = 0; i < decoded_height; i++) { | |
2262 | ✗ | for (j = soff; j < ssize; j++) | |
2263 | ✗ | dst[j] += dst[j - soff]; | |
2264 | ✗ | dst += stride; | |
2265 | } | ||
2266 | } | ||
2267 | } | ||
2268 | |||
2269 | /* Floating point predictor | ||
2270 | TIFF Technical Note 3 http://chriscox.org/TIFFTN3d1.pdf */ | ||
2271 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 18 times.
|
22 | if (s->predictor == 3) { |
2272 | 4 | int channels = s->bppcount; | |
2273 | int group_size; | ||
2274 | uint8_t *tmpbuf; | ||
2275 | int bpc; | ||
2276 | |||
2277 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | dst = five_planes ? five_planes : p->data[plane]; |
2278 | 4 | soff = s->bpp >> 3; | |
2279 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (s->planar) { |
2280 | ✗ | soff = FFMAX(soff / s->bppcount, 1); | |
2281 | ✗ | channels = 1; | |
2282 | } | ||
2283 | 4 | ssize = s->width * soff; | |
2284 | 4 | bpc = FFMAX(soff / s->bppcount, 1); /* Bytes per component */ | |
2285 | 4 | group_size = s->width * channels; | |
2286 | |||
2287 | 4 | tmpbuf = av_malloc(ssize); | |
2288 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!tmpbuf) { |
2289 | ✗ | av_free(five_planes); | |
2290 | ✗ | return AVERROR(ENOMEM); | |
2291 | } | ||
2292 | |||
2293 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (s->avctx->pix_fmt == AV_PIX_FMT_RGBF32LE || |
2294 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | s->avctx->pix_fmt == AV_PIX_FMT_RGBAF32LE) { |
2295 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 4 times.
|
36 | for (i = 0; i < decoded_height; i++) { |
2296 | /* Copy first sample byte for each channel */ | ||
2297 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 32 times.
|
144 | for (j = 0; j < channels; j++) |
2298 | 112 | tmpbuf[j] = dst[j]; | |
2299 | |||
2300 | /* Decode horizontal differences */ | ||
2301 |
2/2✓ Branch 0 taken 3472 times.
✓ Branch 1 taken 32 times.
|
3504 | for (j = channels; j < ssize; j++) |
2302 | 3472 | tmpbuf[j] = dst[j] + tmpbuf[j-channels]; | |
2303 | |||
2304 | /* Combine shuffled bytes from their separate groups. Each | ||
2305 | byte of every floating point value in a row of pixels is | ||
2306 | split and combined into separate groups. A group of all | ||
2307 | the sign/exponents bytes in the row and groups for each | ||
2308 | of the upper, mid, and lower mantissa bytes in the row. */ | ||
2309 |
2/2✓ Branch 0 taken 896 times.
✓ Branch 1 taken 32 times.
|
928 | for (j = 0; j < group_size; j++) { |
2310 |
2/2✓ Branch 0 taken 3584 times.
✓ Branch 1 taken 896 times.
|
4480 | for (int k = 0; k < bpc; k++) { |
2311 | 3584 | dst[bpc * j + k] = tmpbuf[(bpc - k - 1) * group_size + j]; | |
2312 | } | ||
2313 | } | ||
2314 | 32 | dst += stride; | |
2315 | } | ||
2316 | ✗ | } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGBF32BE || | |
2317 | ✗ | s->avctx->pix_fmt == AV_PIX_FMT_RGBAF32BE) { | |
2318 | /* Same as LE only the shuffle at the end is reversed */ | ||
2319 | ✗ | for (i = 0; i < decoded_height; i++) { | |
2320 | ✗ | for (j = 0; j < channels; j++) | |
2321 | ✗ | tmpbuf[j] = dst[j]; | |
2322 | |||
2323 | ✗ | for (j = channels; j < ssize; j++) | |
2324 | ✗ | tmpbuf[j] = dst[j] + tmpbuf[j-channels]; | |
2325 | |||
2326 | ✗ | for (j = 0; j < group_size; j++) { | |
2327 | ✗ | for (int k = 0; k < bpc; k++) { | |
2328 | ✗ | dst[bpc * j + k] = tmpbuf[k * group_size + j]; | |
2329 | } | ||
2330 | } | ||
2331 | ✗ | dst += stride; | |
2332 | } | ||
2333 | } else { | ||
2334 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "unsupported floating point pixel format\n"); | |
2335 | } | ||
2336 | 4 | av_free(tmpbuf); | |
2337 | } | ||
2338 | |||
2339 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20 times.
|
22 | if (s->photometric == TIFF_PHOTOMETRIC_WHITE_IS_ZERO) { |
2340 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | int c = (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255); |
2341 | 2 | dst = p->data[plane]; | |
2342 |
2/2✓ Branch 0 taken 6496 times.
✓ Branch 1 taken 2 times.
|
6498 | for (i = 0; i < s->height; i++) { |
2343 |
2/2✓ Branch 0 taken 2078720 times.
✓ Branch 1 taken 6496 times.
|
2085216 | for (j = 0; j < stride; j++) |
2344 | 2078720 | dst[j] = c - dst[j]; | |
2345 | 6496 | dst += stride; | |
2346 | } | ||
2347 | } | ||
2348 | |||
2349 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED && |
2350 | ✗ | (s->avctx->pix_fmt == AV_PIX_FMT_RGB0 || s->avctx->pix_fmt == AV_PIX_FMT_RGBA)) { | |
2351 | ✗ | int x = s->avctx->pix_fmt == AV_PIX_FMT_RGB0 ? 4 : 5; | |
2352 | ✗ | uint8_t *src = five_planes ? five_planes : p->data[plane]; | |
2353 | ✗ | dst = p->data[plane]; | |
2354 | ✗ | for (i = 0; i < s->height; i++) { | |
2355 | ✗ | for (j = 0; j < s->width; j++) { | |
2356 | ✗ | int k = 255 - src[x * j + 3]; | |
2357 | ✗ | int r = (255 - src[x * j ]) * k; | |
2358 | ✗ | int g = (255 - src[x * j + 1]) * k; | |
2359 | ✗ | int b = (255 - src[x * j + 2]) * k; | |
2360 | ✗ | dst[4 * j ] = r * 257 >> 16; | |
2361 | ✗ | dst[4 * j + 1] = g * 257 >> 16; | |
2362 | ✗ | dst[4 * j + 2] = b * 257 >> 16; | |
2363 | ✗ | dst[4 * j + 3] = s->avctx->pix_fmt == AV_PIX_FMT_RGBA ? src[x * j + 4] : 255; | |
2364 | } | ||
2365 | ✗ | src += stride; | |
2366 | ✗ | dst += p->linesize[plane]; | |
2367 | } | ||
2368 | ✗ | av_freep(&five_planes); | |
2369 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | } else if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED && |
2370 | ✗ | s->avctx->pix_fmt == AV_PIX_FMT_RGBA64BE) { | |
2371 | ✗ | dst = p->data[plane]; | |
2372 | ✗ | for (i = 0; i < s->height; i++) { | |
2373 | ✗ | for (j = 0; j < s->width; j++) { | |
2374 | ✗ | uint64_t k = 65535 - AV_RB16(dst + 8 * j + 6); | |
2375 | ✗ | uint64_t r = (65535 - AV_RB16(dst + 8 * j )) * k; | |
2376 | ✗ | uint64_t g = (65535 - AV_RB16(dst + 8 * j + 2)) * k; | |
2377 | ✗ | uint64_t b = (65535 - AV_RB16(dst + 8 * j + 4)) * k; | |
2378 | ✗ | AV_WB16(dst + 8 * j , r * 65537 >> 32); | |
2379 | ✗ | AV_WB16(dst + 8 * j + 2, g * 65537 >> 32); | |
2380 | ✗ | AV_WB16(dst + 8 * j + 4, b * 65537 >> 32); | |
2381 | ✗ | AV_WB16(dst + 8 * j + 6, 65535); | |
2382 | } | ||
2383 | ✗ | dst += p->linesize[plane]; | |
2384 | } | ||
2385 | } | ||
2386 | } | ||
2387 | |||
2388 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
22 | if (s->planar && s->bppcount > 2) { |
2389 | ✗ | FFSWAP(uint8_t*, p->data[0], p->data[2]); | |
2390 | ✗ | FFSWAP(int, p->linesize[0], p->linesize[2]); | |
2391 | ✗ | FFSWAP(uint8_t*, p->data[0], p->data[1]); | |
2392 | ✗ | FFSWAP(int, p->linesize[0], p->linesize[1]); | |
2393 | } | ||
2394 | |||
2395 |
1/8✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
22 | if (s->is_bayer && s->white_level && s->bpp == 16 && !is_dng) { |
2396 | ✗ | uint16_t *dst = (uint16_t *)p->data[0]; | |
2397 | ✗ | for (i = 0; i < s->height; i++) { | |
2398 | ✗ | for (j = 0; j < s->width; j++) | |
2399 | ✗ | dst[j] = FFMIN((dst[j] / (float)s->white_level) * 65535, 65535); | |
2400 | ✗ | dst += stride / 2; | |
2401 | } | ||
2402 | } | ||
2403 | |||
2404 | 22 | *got_frame = 1; | |
2405 | |||
2406 | 22 | return avpkt->size; | |
2407 | } | ||
2408 | |||
2409 | 21 | static av_cold int tiff_init(AVCodecContext *avctx) | |
2410 | { | ||
2411 | 21 | TiffContext *s = avctx->priv_data; | |
2412 | const AVCodec *codec; | ||
2413 | int ret; | ||
2414 | |||
2415 | 21 | s->width = 0; | |
2416 | 21 | s->height = 0; | |
2417 | 21 | s->subsampling[0] = | |
2418 | 21 | s->subsampling[1] = 1; | |
2419 | 21 | s->avctx = avctx; | |
2420 | 21 | ff_lzw_decode_open(&s->lzw); | |
2421 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (!s->lzw) |
2422 | ✗ | return AVERROR(ENOMEM); | |
2423 | 21 | ff_ccitt_unpack_init(); | |
2424 | |||
2425 | /* Allocate JPEG frame */ | ||
2426 | 21 | s->jpgframe = av_frame_alloc(); | |
2427 | 21 | s->jpkt = av_packet_alloc(); | |
2428 |
2/4✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 21 times.
|
21 | if (!s->jpgframe || !s->jpkt) |
2429 | ✗ | return AVERROR(ENOMEM); | |
2430 | |||
2431 | /* Prepare everything needed for JPEG decoding */ | ||
2432 | 21 | codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); | |
2433 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (!codec) |
2434 | ✗ | return AVERROR_BUG; | |
2435 | 21 | s->avctx_mjpeg = avcodec_alloc_context3(codec); | |
2436 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (!s->avctx_mjpeg) |
2437 | ✗ | return AVERROR(ENOMEM); | |
2438 | 21 | s->avctx_mjpeg->flags = avctx->flags; | |
2439 | 21 | s->avctx_mjpeg->flags2 = avctx->flags2; | |
2440 | 21 | s->avctx_mjpeg->idct_algo = avctx->idct_algo; | |
2441 | 21 | s->avctx_mjpeg->max_pixels = avctx->max_pixels; | |
2442 | 21 | ret = avcodec_open2(s->avctx_mjpeg, codec, NULL); | |
2443 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (ret < 0) { |
2444 | ✗ | return ret; | |
2445 | } | ||
2446 | |||
2447 | 21 | return 0; | |
2448 | } | ||
2449 | |||
2450 | 21 | static av_cold int tiff_end(AVCodecContext *avctx) | |
2451 | { | ||
2452 | 21 | TiffContext *const s = avctx->priv_data; | |
2453 | |||
2454 | 21 | free_geotags(s); | |
2455 | |||
2456 | 21 | ff_lzw_decode_close(&s->lzw); | |
2457 | 21 | av_freep(&s->deinvert_buf); | |
2458 | 21 | s->deinvert_buf_size = 0; | |
2459 | 21 | av_freep(&s->yuv_line); | |
2460 | 21 | s->yuv_line_size = 0; | |
2461 | 21 | av_frame_free(&s->jpgframe); | |
2462 | 21 | av_packet_free(&s->jpkt); | |
2463 | 21 | avcodec_free_context(&s->avctx_mjpeg); | |
2464 | 21 | return 0; | |
2465 | } | ||
2466 | |||
2467 | #define OFFSET(x) offsetof(TiffContext, x) | ||
2468 | static const AVOption tiff_options[] = { | ||
2469 | { "subimage", "decode subimage instead if available", OFFSET(get_subimage), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM }, | ||
2470 | { "thumbnail", "decode embedded thumbnail subimage instead if available", OFFSET(get_thumbnail), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM }, | ||
2471 | { "page", "page number of multi-page image to decode (starting from 1)", OFFSET(get_page), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM }, | ||
2472 | { NULL }, | ||
2473 | }; | ||
2474 | |||
2475 | static const AVClass tiff_decoder_class = { | ||
2476 | .class_name = "TIFF decoder", | ||
2477 | .item_name = av_default_item_name, | ||
2478 | .option = tiff_options, | ||
2479 | .version = LIBAVUTIL_VERSION_INT, | ||
2480 | }; | ||
2481 | |||
2482 | const FFCodec ff_tiff_decoder = { | ||
2483 | .p.name = "tiff", | ||
2484 | CODEC_LONG_NAME("TIFF image"), | ||
2485 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
2486 | .p.id = AV_CODEC_ID_TIFF, | ||
2487 | .priv_data_size = sizeof(TiffContext), | ||
2488 | .init = tiff_init, | ||
2489 | .close = tiff_end, | ||
2490 | FF_CODEC_DECODE_CB(decode_frame), | ||
2491 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, | ||
2492 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_ICC_PROFILES | | ||
2493 | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, | ||
2494 | .p.priv_class = &tiff_decoder_class, | ||
2495 | }; | ||
2496 |