GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/tiff.c Lines: 369 1367 27.0 %
Date: 2020-09-25 23:16:12 Branches: 230 1108 20.8 %

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 "libavutil/attributes.h"
37
#include "libavutil/avstring.h"
38
#include "libavutil/error.h"
39
#include "libavutil/intreadwrite.h"
40
#include "libavutil/imgutils.h"
41
#include "libavutil/opt.h"
42
#include "avcodec.h"
43
#include "bytestream.h"
44
#include "faxcompr.h"
45
#include "internal.h"
46
#include "lzw.h"
47
#include "mathops.h"
48
#include "tiff.h"
49
#include "tiff_data.h"
50
#include "mjpegdec.h"
51
#include "thread.h"
52
#include "get_bits.h"
53
54
typedef struct TiffContext {
55
    AVClass *class;
56
    AVCodecContext *avctx;
57
    GetByteContext gb;
58
59
    /* JPEG decoding for DNG */
60
    AVCodecContext *avctx_mjpeg; // wrapper context for MJPEG
61
    AVFrame *jpgframe;           // decoded JPEG tile
62
63
    int get_subimage;
64
    uint16_t get_page;
65
    int get_thumbnail;
66
67
    enum TiffType tiff_type;
68
    int width, height;
69
    unsigned int bpp, bppcount;
70
    uint32_t palette[256];
71
    int palette_is_set;
72
    int le;
73
    enum TiffCompr compr;
74
    enum TiffPhotometric photometric;
75
    int planar;
76
    int subsampling[2];
77
    int fax_opts;
78
    int predictor;
79
    int fill_order;
80
    uint32_t res[4];
81
    int is_thumbnail;
82
    unsigned last_tag;
83
84
    int is_bayer;
85
    uint8_t pattern[4];
86
    unsigned black_level;
87
    unsigned white_level;
88
    uint16_t dng_lut[65536];
89
90
    uint32_t sub_ifd;
91
    uint16_t cur_page;
92
93
    int strips, rps, sstype;
94
    int sot;
95
    int stripsizesoff, stripsize, stripoff, strippos;
96
    LZWState *lzw;
97
98
    /* Tile support */
99
    int is_tiled;
100
    int tile_byte_counts_offset, tile_offsets_offset;
101
    int tile_width, tile_length;
102
    int tile_count;
103
104
    int is_jpeg;
105
106
    uint8_t *deinvert_buf;
107
    int deinvert_buf_size;
108
    uint8_t *yuv_line;
109
    unsigned int yuv_line_size;
110
    uint8_t *fax_buffer;
111
    unsigned int fax_buffer_size;
112
113
    int geotag_count;
114
    TiffGeoTag *geotags;
115
} TiffContext;
116
117
static void tiff_set_type(TiffContext *s, enum TiffType tiff_type) {
118
    if (s->tiff_type < tiff_type) // Prioritize higher-valued entries
119
        s->tiff_type = tiff_type;
120
}
121
122
30
static void free_geotags(TiffContext *const s)
123
{
124
    int i;
125
30
    for (i = 0; i < s->geotag_count; i++) {
126
        if (s->geotags[i].val)
127
            av_freep(&s->geotags[i].val);
128
    }
129
30
    av_freep(&s->geotags);
130
30
    s->geotag_count = 0;
131
30
}
132
133
#define RET_GEOKEY(TYPE, array, element)\
134
    if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\
135
        key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_name_type_map))\
136
        return ff_tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].element;
137
138
static const char *get_geokey_name(int key)
139
{
140
    RET_GEOKEY(VERT, vert, name);
141
    RET_GEOKEY(PROJ, proj, name);
142
    RET_GEOKEY(GEOG, geog, name);
143
    RET_GEOKEY(CONF, conf, name);
144
145
    return NULL;
146
}
147
148
static int get_geokey_type(int key)
149
{
150
    RET_GEOKEY(VERT, vert, type);
151
    RET_GEOKEY(PROJ, proj, type);
152
    RET_GEOKEY(GEOG, geog, type);
153
    RET_GEOKEY(CONF, conf, type);
154
155
    return AVERROR_INVALIDDATA;
156
}
157
158
static int cmp_id_key(const void *id, const void *k)
159
{
160
    return *(const int*)id - ((const TiffGeoTagKeyName*)k)->key;
161
}
162
163
static const char *search_keyval(const TiffGeoTagKeyName *keys, int n, int id)
164
{
165
    TiffGeoTagKeyName *r = bsearch(&id, keys, n, sizeof(keys[0]), cmp_id_key);
166
    if(r)
167
        return r->name;
168
169
    return NULL;
170
}
171
172
static char *get_geokey_val(int key, int val)
173
{
174
    char *ap;
175
176
    if (val == TIFF_GEO_KEY_UNDEFINED)
177
        return av_strdup("undefined");
178
    if (val == TIFF_GEO_KEY_USER_DEFINED)
179
        return av_strdup("User-Defined");
180
181
#define RET_GEOKEY_VAL(TYPE, array)\
182
    if (val >= TIFF_##TYPE##_OFFSET &&\
183
        val - TIFF_##TYPE##_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_codes))\
184
        return av_strdup(ff_tiff_##array##_codes[val - TIFF_##TYPE##_OFFSET]);
185
186
    switch (key) {
187
    case TIFF_GT_MODEL_TYPE_GEOKEY:
188
        RET_GEOKEY_VAL(GT_MODEL_TYPE, gt_model_type);
189
        break;
190
    case TIFF_GT_RASTER_TYPE_GEOKEY:
191
        RET_GEOKEY_VAL(GT_RASTER_TYPE, gt_raster_type);
192
        break;
193
    case TIFF_GEOG_LINEAR_UNITS_GEOKEY:
194
    case TIFF_PROJ_LINEAR_UNITS_GEOKEY:
195
    case TIFF_VERTICAL_UNITS_GEOKEY:
196
        RET_GEOKEY_VAL(LINEAR_UNIT, linear_unit);
197
        break;
198
    case TIFF_GEOG_ANGULAR_UNITS_GEOKEY:
199
    case TIFF_GEOG_AZIMUTH_UNITS_GEOKEY:
200
        RET_GEOKEY_VAL(ANGULAR_UNIT, angular_unit);
201
        break;
202
    case TIFF_GEOGRAPHIC_TYPE_GEOKEY:
203
        RET_GEOKEY_VAL(GCS_TYPE, gcs_type);
204
        RET_GEOKEY_VAL(GCSE_TYPE, gcse_type);
205
        break;
206
    case TIFF_GEOG_GEODETIC_DATUM_GEOKEY:
207
        RET_GEOKEY_VAL(GEODETIC_DATUM, geodetic_datum);
208
        RET_GEOKEY_VAL(GEODETIC_DATUM_E, geodetic_datum_e);
209
        break;
210
    case TIFF_GEOG_ELLIPSOID_GEOKEY:
211
        RET_GEOKEY_VAL(ELLIPSOID, ellipsoid);
212
        break;
213
    case TIFF_GEOG_PRIME_MERIDIAN_GEOKEY:
214
        RET_GEOKEY_VAL(PRIME_MERIDIAN, prime_meridian);
215
        break;
216
    case TIFF_PROJECTED_CS_TYPE_GEOKEY:
217
        ap = av_strdup(search_keyval(ff_tiff_proj_cs_type_codes, FF_ARRAY_ELEMS(ff_tiff_proj_cs_type_codes), val));
218
        if(ap) return ap;
219
        break;
220
    case TIFF_PROJECTION_GEOKEY:
221
        ap = av_strdup(search_keyval(ff_tiff_projection_codes, FF_ARRAY_ELEMS(ff_tiff_projection_codes), val));
222
        if(ap) return ap;
223
        break;
224
    case TIFF_PROJ_COORD_TRANS_GEOKEY:
225
        RET_GEOKEY_VAL(COORD_TRANS, coord_trans);
226
        break;
227
    case TIFF_VERTICAL_CS_TYPE_GEOKEY:
228
        RET_GEOKEY_VAL(VERT_CS, vert_cs);
229
        RET_GEOKEY_VAL(ORTHO_VERT_CS, ortho_vert_cs);
230
        break;
231
232
    }
233
234
    ap = av_malloc(14);
235
    if (ap)
236
        snprintf(ap, 14, "Unknown-%d", val);
237
    return ap;
238
}
239
240
static char *doubles2str(double *dp, int count, const char *sep)
241
{
242
    int i;
243
    char *ap, *ap0;
244
    uint64_t component_len;
245
    if (!sep) sep = ", ";
246
    component_len = 24LL + strlen(sep);
247
    if (count >= (INT_MAX - 1)/component_len)
248
        return NULL;
249
    ap = av_malloc(component_len * count + 1);
250
    if (!ap)
251
        return NULL;
252
    ap0   = ap;
253
    ap[0] = '\0';
254
    for (i = 0; i < count; i++) {
255
        unsigned l = snprintf(ap, component_len, "%.15g%s", dp[i], sep);
256
        if(l >= component_len) {
257
            av_free(ap0);
258
            return NULL;
259
        }
260
        ap += l;
261
    }
262
    ap0[strlen(ap0) - strlen(sep)] = '\0';
263
    return ap0;
264
}
265
266
14
static int add_metadata(int count, int type,
267
                        const char *name, const char *sep, TiffContext *s, AVFrame *frame)
268
{
269

14
    switch(type) {
270
    case TIFF_DOUBLE: return ff_tadd_doubles_metadata(count, name, sep, &s->gb, s->le, &frame->metadata);
271
2
    case TIFF_SHORT : return ff_tadd_shorts_metadata(count, name, sep, &s->gb, s->le, 0, &frame->metadata);
272
12
    case TIFF_STRING: return ff_tadd_string_metadata(count, name, &s->gb, s->le, &frame->metadata);
273
    default         : return AVERROR_INVALIDDATA;
274
    };
275
}
276
277
static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int dst_stride,
278
                                      const uint8_t *src, int src_stride, int width, int height,
279
                                      int is_single_comp, int is_u16);
280
281
42246
static void av_always_inline horizontal_fill(TiffContext *s,
282
                                             unsigned int bpp, uint8_t* dst,
283
                                             int usePtr, const uint8_t *src,
284
                                             uint8_t c, int width, int offset)
285
{
286

42246
    switch (bpp) {
287
    case 1:
288
        while (--width >= 0) {
289
            dst[(width+offset)*8+7] = (usePtr ? src[width] : c)      & 0x1;
290
            dst[(width+offset)*8+6] = (usePtr ? src[width] : c) >> 1 & 0x1;
291
            dst[(width+offset)*8+5] = (usePtr ? src[width] : c) >> 2 & 0x1;
292
            dst[(width+offset)*8+4] = (usePtr ? src[width] : c) >> 3 & 0x1;
293
            dst[(width+offset)*8+3] = (usePtr ? src[width] : c) >> 4 & 0x1;
294
            dst[(width+offset)*8+2] = (usePtr ? src[width] : c) >> 5 & 0x1;
295
            dst[(width+offset)*8+1] = (usePtr ? src[width] : c) >> 6 & 0x1;
296
            dst[(width+offset)*8+0] = (usePtr ? src[width] : c) >> 7;
297
        }
298
        break;
299
    case 2:
300
        while (--width >= 0) {
301
            dst[(width+offset)*4+3] = (usePtr ? src[width] : c) & 0x3;
302
            dst[(width+offset)*4+2] = (usePtr ? src[width] : c) >> 2 & 0x3;
303
            dst[(width+offset)*4+1] = (usePtr ? src[width] : c) >> 4 & 0x3;
304
            dst[(width+offset)*4+0] = (usePtr ? src[width] : c) >> 6;
305
        }
306
        break;
307
    case 4:
308
        while (--width >= 0) {
309
            dst[(width+offset)*2+1] = (usePtr ? src[width] : c) & 0xF;
310
            dst[(width+offset)*2+0] = (usePtr ? src[width] : c) >> 4;
311
        }
312
        break;
313
    case 10:
314
    case 12:
315
    case 14: {
316
            uint16_t *dst16 = (uint16_t *)dst;
317
            int is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG);
318
            uint8_t shift = is_dng ? 0 : 16 - bpp;
319
            GetBitContext gb;
320
321
            init_get_bits8(&gb, src, width);
322
            for (int i = 0; i < s->width; i++) {
323
                dst16[i] = get_bits(&gb, bpp) << shift;
324
            }
325
        }
326
        break;
327
42246
    default:
328
42246
        if (usePtr) {
329
39747
            memcpy(dst + offset, src, width);
330
        } else {
331
2499
            memset(dst + offset, c, width);
332
        }
333
    }
334
42246
}
335
336
static int deinvert_buffer(TiffContext *s, const uint8_t *src, int size)
337
{
338
    int i;
339
340
    av_fast_padded_malloc(&s->deinvert_buf, &s->deinvert_buf_size, size);
341
    if (!s->deinvert_buf)
342
        return AVERROR(ENOMEM);
343
    for (i = 0; i < size; i++)
344
        s->deinvert_buf[i] = ff_reverse[src[i]];
345
346
    return 0;
347
}
348
349
static void unpack_gray(TiffContext *s, AVFrame *p,
350
                       const uint8_t *src, int lnum, int width, int bpp)
351
{
352
    GetBitContext gb;
353
    uint16_t *dst = (uint16_t *)(p->data[0] + lnum * p->linesize[0]);
354
355
    init_get_bits8(&gb, src, width);
356
357
    for (int i = 0; i < s->width; i++) {
358
        dst[i] = get_bits(&gb, bpp);
359
    }
360
}
361
362
static void unpack_yuv(TiffContext *s, AVFrame *p,
363
                       const uint8_t *src, int lnum)
364
{
365
    int i, j, k;
366
    int w       = (s->width - 1) / s->subsampling[0] + 1;
367
    uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
368
    uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
369
    if (s->width % s->subsampling[0] || s->height % s->subsampling[1]) {
370
        for (i = 0; i < w; i++) {
371
            for (j = 0; j < s->subsampling[1]; j++)
372
                for (k = 0; k < s->subsampling[0]; k++)
373
                    p->data[0][FFMIN(lnum + j, s->height-1) * p->linesize[0] +
374
                               FFMIN(i * s->subsampling[0] + k, s->width-1)] = *src++;
375
            *pu++ = *src++;
376
            *pv++ = *src++;
377
        }
378
    }else{
379
        for (i = 0; i < w; i++) {
380
            for (j = 0; j < s->subsampling[1]; j++)
381
                for (k = 0; k < s->subsampling[0]; k++)
382
                    p->data[0][(lnum + j) * p->linesize[0] +
383
                               i * s->subsampling[0] + k] = *src++;
384
            *pu++ = *src++;
385
            *pv++ = *src++;
386
        }
387
    }
388
}
389
390
#if CONFIG_ZLIB
391
static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
392
                           int size)
393
{
394
    z_stream zstream = { 0 };
395
    int zret;
396
397
    zstream.next_in   = src;
398
    zstream.avail_in  = size;
399
    zstream.next_out  = dst;
400
    zstream.avail_out = *len;
401
    zret              = inflateInit(&zstream);
402
    if (zret != Z_OK) {
403
        av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
404
        return zret;
405
    }
406
    zret = inflate(&zstream, Z_SYNC_FLUSH);
407
    inflateEnd(&zstream);
408
    *len = zstream.total_out;
409
    return zret == Z_STREAM_END ? Z_OK : zret;
410
}
411
412
static int tiff_unpack_zlib(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
413
                            const uint8_t *src, int size, int width, int lines,
414
                            int strip_start, int is_yuv)
415
{
416
    uint8_t *zbuf;
417
    unsigned long outlen;
418
    int ret, line;
419
    outlen = width * lines;
420
    zbuf   = av_malloc(outlen);
421
    if (!zbuf)
422
        return AVERROR(ENOMEM);
423
    if (s->fill_order) {
424
        if ((ret = deinvert_buffer(s, src, size)) < 0) {
425
            av_free(zbuf);
426
            return ret;
427
        }
428
        src = s->deinvert_buf;
429
    }
430
    ret = tiff_uncompress(zbuf, &outlen, src, size);
431
    if (ret != Z_OK) {
432
        av_log(s->avctx, AV_LOG_ERROR,
433
               "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
434
               (unsigned long)width * lines, ret);
435
        av_free(zbuf);
436
        return AVERROR_UNKNOWN;
437
    }
438
    src = zbuf;
439
    for (line = 0; line < lines; line++) {
440
        if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
441
            horizontal_fill(s, s->bpp, dst, 1, src, 0, width, 0);
442
        } else {
443
            memcpy(dst, src, width);
444
        }
445
        if (is_yuv) {
446
            unpack_yuv(s, p, dst, strip_start + line);
447
            line += s->subsampling[1] - 1;
448
        }
449
        dst += stride;
450
        src += width;
451
    }
452
    av_free(zbuf);
453
    return 0;
454
}
455
#endif
456
457
#if CONFIG_LZMA
458
static int tiff_uncompress_lzma(uint8_t *dst, uint64_t *len, const uint8_t *src,
459
                                int size)
460
{
461
    lzma_stream stream = LZMA_STREAM_INIT;
462
    lzma_ret ret;
463
464
    stream.next_in   = (uint8_t *)src;
465
    stream.avail_in  = size;
466
    stream.next_out  = dst;
467
    stream.avail_out = *len;
468
    ret              = lzma_stream_decoder(&stream, UINT64_MAX, 0);
469
    if (ret != LZMA_OK) {
470
        av_log(NULL, AV_LOG_ERROR, "LZMA init error: %d\n", ret);
471
        return ret;
472
    }
473
    ret = lzma_code(&stream, LZMA_RUN);
474
    lzma_end(&stream);
475
    *len = stream.total_out;
476
    return ret == LZMA_STREAM_END ? LZMA_OK : ret;
477
}
478
479
static int tiff_unpack_lzma(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
480
                            const uint8_t *src, int size, int width, int lines,
481
                            int strip_start, int is_yuv)
482
{
483
    uint64_t outlen = width * (uint64_t)lines;
484
    int ret, line;
485
    uint8_t *buf = av_malloc(outlen);
486
    if (!buf)
487
        return AVERROR(ENOMEM);
488
    if (s->fill_order) {
489
        if ((ret = deinvert_buffer(s, src, size)) < 0) {
490
            av_free(buf);
491
            return ret;
492
        }
493
        src = s->deinvert_buf;
494
    }
495
    ret = tiff_uncompress_lzma(buf, &outlen, src, size);
496
    if (ret != LZMA_OK) {
497
        av_log(s->avctx, AV_LOG_ERROR,
498
               "Uncompressing failed (%"PRIu64" of %"PRIu64") with error %d\n", outlen,
499
               (uint64_t)width * lines, ret);
500
        av_free(buf);
501
        return AVERROR_UNKNOWN;
502
    }
503
    src = buf;
504
    for (line = 0; line < lines; line++) {
505
        if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
506
            horizontal_fill(s, s->bpp, dst, 1, src, 0, width, 0);
507
        } else {
508
            memcpy(dst, src, width);
509
        }
510
        if (is_yuv) {
511
            unpack_yuv(s, p, dst, strip_start + line);
512
            line += s->subsampling[1] - 1;
513
        }
514
        dst += stride;
515
        src += width;
516
    }
517
    av_free(buf);
518
    return 0;
519
}
520
#endif
521
522
36
static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride,
523
                           const uint8_t *src, int size, int width, int lines)
524
{
525
36
    int i, ret = 0;
526
    int line;
527
    uint8_t *src2;
528
529
36
    av_fast_padded_malloc(&s->fax_buffer, &s->fax_buffer_size, size);
530
36
    src2 = s->fax_buffer;
531
532
36
    if (!src2) {
533
        av_log(s->avctx, AV_LOG_ERROR,
534
               "Error allocating temporary buffer\n");
535
        return AVERROR(ENOMEM);
536
    }
537
538
36
    if (!s->fill_order) {
539
        memcpy(src2, src, size);
540
    } else {
541
502274
        for (i = 0; i < size; i++)
542
502238
            src2[i] = ff_reverse[src[i]];
543
    }
544
36
    memset(src2 + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
545
36
    ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
546
                          s->compr, s->fax_opts);
547

36
    if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
548
        for (line = 0; line < lines; line++) {
549
            horizontal_fill(s, s->bpp, dst, 1, dst, 0, width, 0);
550
            dst += stride;
551
        }
552
36
    return ret;
553
}
554
555
static int dng_decode_strip(AVCodecContext *avctx, AVFrame *frame);
556
557
684
static int tiff_unpack_strip(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
558
                             const uint8_t *src, int size, int strip_start, int lines)
559
{
560
    PutByteContext pb;
561
    int c, line, pixels, code, ret;
562
684
    const uint8_t *ssrc = src;
563
684
    int width = ((s->width * s->bpp) + 7) >> 3;
564
684
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(p->format);
565
1404
    int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
566

684
                 (desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
567
                 desc->nb_components >= 3;
568
    int is_dng;
569
570
684
    if (s->planar)
571
        width /= s->bppcount;
572
573
684
    if (size <= 0)
574
        return AVERROR_INVALIDDATA;
575
576
684
    if (is_yuv) {
577
        int bytes_per_row = (((s->width - 1) / s->subsampling[0] + 1) * s->bpp *
578
                            s->subsampling[0] * s->subsampling[1] + 7) >> 3;
579
        av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, bytes_per_row);
580
        if (s->yuv_line == NULL) {
581
            av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
582
            return AVERROR(ENOMEM);
583
        }
584
        dst = s->yuv_line;
585
        stride = 0;
586
587
        width = (s->width - 1) / s->subsampling[0] + 1;
588
        width = width * s->subsampling[0] * s->subsampling[1] + 2*width;
589
        av_assert0(width <= bytes_per_row);
590
        av_assert0(s->bpp == 24);
591
    }
592
684
    if (s->is_bayer) {
593
        av_assert0(width == (s->bpp * s->width + 7) >> 3);
594
    }
595
684
    if (p->format == AV_PIX_FMT_GRAY12) {
596
        av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, width);
597
        if (s->yuv_line == NULL) {
598
            av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
599
            return AVERROR(ENOMEM);
600
        }
601
        dst = s->yuv_line;
602
        stride = 0;
603
    }
604
605

684
    if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
606
#if CONFIG_ZLIB
607
        return tiff_unpack_zlib(s, p, dst, stride, src, size, width, lines,
608
                                strip_start, is_yuv);
609
#else
610
        av_log(s->avctx, AV_LOG_ERROR,
611
               "zlib support not enabled, "
612
               "deflate compression not supported\n");
613
        return AVERROR(ENOSYS);
614
#endif
615
    }
616
684
    if (s->compr == TIFF_LZMA) {
617
#if CONFIG_LZMA
618
        return tiff_unpack_lzma(s, p, dst, stride, src, size, width, lines,
619
                                strip_start, is_yuv);
620
#else
621
        av_log(s->avctx, AV_LOG_ERROR,
622
               "LZMA support not enabled\n");
623
        return AVERROR(ENOSYS);
624
#endif
625
    }
626
684
    if (s->compr == TIFF_LZW) {
627
        if (s->fill_order) {
628
            if ((ret = deinvert_buffer(s, src, size)) < 0)
629
                return ret;
630
            ssrc = src = s->deinvert_buf;
631
        }
632
        if (size > 1 && !src[0] && (src[1]&1)) {
633
            av_log(s->avctx, AV_LOG_ERROR, "Old style LZW is unsupported\n");
634
        }
635
        if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
636
            av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
637
            return ret;
638
        }
639
        for (line = 0; line < lines; line++) {
640
            pixels = ff_lzw_decode(s->lzw, dst, width);
641
            if (pixels < width) {
642
                av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
643
                       pixels, width);
644
                return AVERROR_INVALIDDATA;
645
            }
646
            if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
647
                horizontal_fill(s, s->bpp, dst, 1, dst, 0, width, 0);
648
            if (is_yuv) {
649
                unpack_yuv(s, p, dst, strip_start + line);
650
                line += s->subsampling[1] - 1;
651
            } else if (p->format == AV_PIX_FMT_GRAY12) {
652
                unpack_gray(s, p, dst, strip_start + line, width, s->bpp);
653
            }
654
            dst += stride;
655
        }
656
        return 0;
657
    }
658
684
    if (s->compr == TIFF_CCITT_RLE ||
659
684
        s->compr == TIFF_G3        ||
660
648
        s->compr == TIFF_G4) {
661

36
        if (is_yuv || p->format == AV_PIX_FMT_GRAY12)
662
            return AVERROR_INVALIDDATA;
663
664
36
        return tiff_unpack_fax(s, dst, stride, src, size, width, lines);
665
    }
666
667
648
    bytestream2_init(&s->gb, src, size);
668
648
    bytestream2_init_writer(&pb, dst, is_yuv ? s->yuv_line_size : (stride * lines));
669
670

648
    is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG);
671
672
    /* Decode JPEG-encoded DNGs with strips */
673

648
    if (s->compr == TIFF_NEWJPEG && is_dng) {
674
        if (s->strips > 1) {
675
            av_log(s->avctx, AV_LOG_ERROR, "More than one DNG JPEG strips unsupported\n");
676
            return AVERROR_PATCHWELCOME;
677
        }
678
        if ((ret = dng_decode_strip(s->avctx, p)) < 0)
679
            return ret;
680
        return 0;
681
    }
682
683

648
    if (is_dng && stride == 0)
684
        return AVERROR_INVALIDDATA;
685
686
5192
    for (line = 0; line < lines; line++) {
687
4544
        if (src - ssrc > size) {
688
            av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
689
            return AVERROR_INVALIDDATA;
690
        }
691
692

4544
        if (bytestream2_get_bytes_left(&s->gb) == 0 || bytestream2_get_eof(&pb))
693
            break;
694
4544
        bytestream2_seek_p(&pb, stride * line, SEEK_SET);
695
4544
        switch (s->compr) {
696
224
        case TIFF_RAW:
697
224
            if (ssrc + size - src < width)
698
                return AVERROR_INVALIDDATA;
699
700
224
            if (!s->fill_order) {
701

224
                horizontal_fill(s, s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 || s->is_bayer),
702
                                dst, 1, src, 0, width, 0);
703
            } else {
704
                int i;
705
                for (i = 0; i < width; i++)
706
                    dst[i] = ff_reverse[src[i]];
707
            }
708
709
            /* Color processing for DNG images with uncompressed strips (non-tiled) */
710
224
            if (is_dng) {
711
                int is_u16, pixel_size_bytes, pixel_size_bits, elements;
712
713
                is_u16 = (s->bpp / s->bppcount > 8);
714
                pixel_size_bits = (is_u16 ? 16 : 8);
715
                pixel_size_bytes = (is_u16 ? sizeof(uint16_t) : sizeof(uint8_t));
716
717
                elements = width / pixel_size_bytes * pixel_size_bits / s->bpp * s->bppcount; // need to account for [1, 16] bpp
718
                av_assert0 (elements * pixel_size_bytes <= FFABS(stride));
719
                dng_blit(s,
720
                         dst,
721
                         0, // no stride, only 1 line
722
                         dst,
723
                         0, // no stride, only 1 line
724
                         elements,
725
                         1,
726
                         0, // single-component variation is only preset in JPEG-encoded DNGs
727
                         is_u16);
728
            }
729
730
224
            src += width;
731
224
            break;
732
4320
        case TIFF_PACKBITS:
733
46342
            for (pixels = 0; pixels < width;) {
734
42022
                if (ssrc + size - src < 2) {
735
                    av_log(s->avctx, AV_LOG_ERROR, "Read went out of bounds\n");
736
                    return AVERROR_INVALIDDATA;
737
                }
738
42022
                code = s->fill_order ? (int8_t) ff_reverse[*src++]: (int8_t) *src++;
739
42022
                if (code >= 0) {
740
39523
                    code++;
741
39523
                    if (pixels + code > width ||
742
39523
                        ssrc + size - src < code) {
743
                        av_log(s->avctx, AV_LOG_ERROR,
744
                               "Copy went out of bounds\n");
745
                        return AVERROR_INVALIDDATA;
746
                    }
747
39523
                    horizontal_fill(s, s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8),
748
                                    dst, 1, src, 0, code, pixels);
749
39523
                    src    += code;
750
39523
                    pixels += code;
751
2499
                } else if (code != -128) { // -127..-1
752
2499
                    code = (-code) + 1;
753
2499
                    if (pixels + code > width) {
754
                        av_log(s->avctx, AV_LOG_ERROR,
755
                               "Run went out of bounds\n");
756
                        return AVERROR_INVALIDDATA;
757
                    }
758
2499
                    c = *src++;
759
2499
                    horizontal_fill(s, s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8),
760
                                    dst, 0, NULL, c, code, pixels);
761
2499
                    pixels += code;
762
                }
763
            }
764
4320
            if (s->fill_order) {
765
                int i;
766
                for (i = 0; i < width; i++)
767
                    dst[i] = ff_reverse[dst[i]];
768
            }
769
4320
            break;
770
        }
771
4544
        if (is_yuv) {
772
            unpack_yuv(s, p, dst, strip_start + line);
773
            line += s->subsampling[1] - 1;
774
4544
        } else if (p->format == AV_PIX_FMT_GRAY12) {
775
            unpack_gray(s, p, dst, strip_start + line, width, s->bpp);
776
        }
777
4544
        dst += stride;
778
    }
779
648
    return 0;
780
}
781
782
/**
783
 * Map stored raw sensor values into linear reference values (see: DNG Specification - Chapter 5)
784
 */
785
static uint16_t av_always_inline dng_process_color16(uint16_t value,
786
                                                     const uint16_t *lut,
787
                                                     uint16_t black_level,
788
                                                     float scale_factor) {
789
    float value_norm;
790
791
    // Lookup table lookup
792
    if (lut)
793
        value = lut[value];
794
795
    // Black level subtraction
796
    value = av_clip_uint16_c((unsigned)value - black_level);
797
798
    // Color scaling
799
    value_norm = (float)value * scale_factor;
800
801
    value = av_clip_uint16_c(value_norm * 65535);
802
803
    return value;
804
}
805
806
static uint16_t av_always_inline dng_process_color8(uint16_t value,
807
                                                    const uint16_t *lut,
808
                                                    uint16_t black_level,
809
                                                    float scale_factor) {
810
    return dng_process_color16(value, lut, black_level, scale_factor) >> 8;
811
}
812
813
static void dng_blit(TiffContext *s, uint8_t *dst, int dst_stride,
814
                     const uint8_t *src, int src_stride,
815
                     int width, int height, int is_single_comp, int is_u16)
816
{
817
    int line, col;
818
    float scale_factor;
819
820
    scale_factor = 1.0f / (s->white_level - s->black_level);
821
822
    if (is_single_comp) {
823
        if (!is_u16)
824
            return; /* <= 8bpp unsupported */
825
826
        /* Image is double the width and half the height we need, each row comprises 2 rows of the output
827
           (split vertically in the middle). */
828
        for (line = 0; line < height / 2; line++) {
829
            uint16_t *dst_u16 = (uint16_t *)dst;
830
            uint16_t *src_u16 = (uint16_t *)src;
831
832
            /* Blit first half of input row row to initial row of output */
833
            for (col = 0; col < width; col++)
834
                *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level, scale_factor);
835
836
            /* Advance the destination pointer by a row (source pointer remains in the same place) */
837
            dst += dst_stride * sizeof(uint16_t);
838
            dst_u16 = (uint16_t *)dst;
839
840
            /* Blit second half of input row row to next row of output */
841
            for (col = 0; col < width; col++)
842
                *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level, scale_factor);
843
844
            dst += dst_stride * sizeof(uint16_t);
845
            src += src_stride * sizeof(uint16_t);
846
        }
847
    } else {
848
        /* Input and output image are the same size and the MJpeg decoder has done per-component
849
           deinterleaving, so blitting here is straightforward. */
850
        if (is_u16) {
851
            for (line = 0; line < height; line++) {
852
                uint16_t *dst_u16 = (uint16_t *)dst;
853
                uint16_t *src_u16 = (uint16_t *)src;
854
855
                for (col = 0; col < width; col++)
856
                    *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, s->black_level, scale_factor);
857
858
                dst += dst_stride * sizeof(uint16_t);
859
                src += src_stride * sizeof(uint16_t);
860
            }
861
        } else {
862
            for (line = 0; line < height; line++) {
863
                uint8_t *dst_u8 = dst;
864
                const uint8_t *src_u8 = src;
865
866
                for (col = 0; col < width; col++)
867
                    *dst_u8++ = dng_process_color8(*src_u8++, s->dng_lut, s->black_level, scale_factor);
868
869
                dst += dst_stride;
870
                src += src_stride;
871
            }
872
        }
873
    }
874
}
875
876
static int dng_decode_jpeg(AVCodecContext *avctx, AVFrame *frame,
877
                           int tile_byte_count, int dst_x, int dst_y, int w, int h)
878
{
879
    TiffContext *s = avctx->priv_data;
880
    AVPacket jpkt;
881
    uint8_t *dst_data, *src_data;
882
    uint32_t dst_offset; /* offset from dst buffer in pixels */
883
    int is_single_comp, is_u16, pixel_size;
884
    int ret;
885
886
    if (tile_byte_count < 0 || tile_byte_count > bytestream2_get_bytes_left(&s->gb))
887
        return AVERROR_INVALIDDATA;
888
889
    /* Prepare a packet and send to the MJPEG decoder */
890
    av_init_packet(&jpkt);
891
    jpkt.data = (uint8_t*)s->gb.buffer;
892
    jpkt.size = tile_byte_count;
893
894
    if (s->is_bayer) {
895
        MJpegDecodeContext *mjpegdecctx = s->avctx_mjpeg->priv_data;
896
        /* We have to set this information here, there is no way to know if a given JPEG is a DNG-embedded
897
           image or not from its own data (and we need that information when decoding it). */
898
        mjpegdecctx->bayer = 1;
899
    }
900
901
    ret = avcodec_send_packet(s->avctx_mjpeg, &jpkt);
902
    if (ret < 0) {
903
        av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
904
        return ret;
905
    }
906
907
    ret = avcodec_receive_frame(s->avctx_mjpeg, s->jpgframe);
908
    if (ret < 0) {
909
        av_log(avctx, AV_LOG_ERROR, "JPEG decoding error: %s.\n", av_err2str(ret));
910
911
        /* Normally skip, error if explode */
912
        if (avctx->err_recognition & AV_EF_EXPLODE)
913
            return AVERROR_INVALIDDATA;
914
        else
915
            return 0;
916
    }
917
918
    is_u16 = (s->bpp > 8);
919
920
    /* Copy the outputted tile's pixels from 'jpgframe' to 'frame' (final buffer) */
921
922
    if (s->jpgframe->width  != s->avctx_mjpeg->width  ||
923
        s->jpgframe->height != s->avctx_mjpeg->height ||
924
        s->jpgframe->format != s->avctx_mjpeg->pix_fmt)
925
        return AVERROR_INVALIDDATA;
926
927
    /* See dng_blit for explanation */
928
    if (s->avctx_mjpeg->width  == w * 2 &&
929
        s->avctx_mjpeg->height == h / 2 &&
930
        s->avctx_mjpeg->pix_fmt == AV_PIX_FMT_GRAY16LE) {
931
        is_single_comp = 1;
932
    } else if (s->avctx_mjpeg->width  == w &&
933
               s->avctx_mjpeg->height == h &&
934
               s->avctx_mjpeg->pix_fmt == (is_u16 ? AV_PIX_FMT_GRAY16 : AV_PIX_FMT_GRAY8)
935
              ) {
936
        is_single_comp = 0;
937
    } else
938
        return AVERROR_INVALIDDATA;
939
940
    pixel_size = (is_u16 ? sizeof(uint16_t) : sizeof(uint8_t));
941
942
    if (is_single_comp && !is_u16) {
943
        av_log(s->avctx, AV_LOG_ERROR, "DNGs with bpp <= 8 and 1 component are unsupported\n");
944
        av_frame_unref(s->jpgframe);
945
        return AVERROR_PATCHWELCOME;
946
    }
947
948
    dst_offset = dst_x + frame->linesize[0] * dst_y / pixel_size;
949
    dst_data = frame->data[0] + dst_offset * pixel_size;
950
    src_data = s->jpgframe->data[0];
951
952
    dng_blit(s,
953
             dst_data,
954
             frame->linesize[0] / pixel_size,
955
             src_data,
956
             s->jpgframe->linesize[0] / pixel_size,
957
             w,
958
             h,
959
             is_single_comp,
960
             is_u16);
961
962
    av_frame_unref(s->jpgframe);
963
964
    return 0;
965
}
966
967
static int dng_decode_tiles(AVCodecContext *avctx, AVFrame *frame, AVPacket *avpkt)
968
{
969
    TiffContext *s = avctx->priv_data;
970
    int tile_idx;
971
    int tile_offset_offset, tile_offset;
972
    int tile_byte_count_offset, tile_byte_count;
973
    int tile_count_x, tile_count_y;
974
    int tile_width, tile_length;
975
    int has_width_leftover, has_height_leftover;
976
    int tile_x = 0, tile_y = 0;
977
    int pos_x = 0, pos_y = 0;
978
    int ret;
979
980
    s->jpgframe->width  = s->tile_width;
981
    s->jpgframe->height = s->tile_length;
982
983
    s->avctx_mjpeg->width = s->tile_width;
984
    s->avctx_mjpeg->height = s->tile_length;
985
986
    has_width_leftover = (s->width % s->tile_width != 0);
987
    has_height_leftover = (s->height % s->tile_length != 0);
988
989
    /* Calculate tile counts (round up) */
990
    tile_count_x = (s->width + s->tile_width - 1) / s->tile_width;
991
    tile_count_y = (s->height + s->tile_length - 1) / s->tile_length;
992
993
    /* Iterate over the number of tiles */
994
    for (tile_idx = 0; tile_idx < s->tile_count; tile_idx++) {
995
        tile_x = tile_idx % tile_count_x;
996
        tile_y = tile_idx / tile_count_x;
997
998
        if (has_width_leftover && tile_x == tile_count_x - 1) // If on the right-most tile
999
            tile_width = s->width % s->tile_width;
1000
        else
1001
            tile_width = s->tile_width;
1002
1003
        if (has_height_leftover && tile_y == tile_count_y - 1) // If on the bottom-most tile
1004
            tile_length = s->height % s->tile_length;
1005
        else
1006
            tile_length = s->tile_length;
1007
1008
        /* Read tile offset */
1009
        tile_offset_offset = s->tile_offsets_offset + tile_idx * sizeof(int);
1010
        bytestream2_seek(&s->gb, tile_offset_offset, SEEK_SET);
1011
        tile_offset = ff_tget_long(&s->gb, s->le);
1012
1013
        /* Read tile byte size */
1014
        tile_byte_count_offset = s->tile_byte_counts_offset + tile_idx * sizeof(int);
1015
        bytestream2_seek(&s->gb, tile_byte_count_offset, SEEK_SET);
1016
        tile_byte_count = ff_tget_long(&s->gb, s->le);
1017
1018
        /* Seek to tile data */
1019
        bytestream2_seek(&s->gb, tile_offset, SEEK_SET);
1020
1021
        /* Decode JPEG tile and copy it in the reference frame */
1022
        ret = dng_decode_jpeg(avctx, frame, tile_byte_count, pos_x, pos_y, tile_width, tile_length);
1023
1024
        if (ret < 0)
1025
            return ret;
1026
1027
        /* Advance current positions */
1028
        pos_x += tile_width;
1029
        if (tile_x == tile_count_x - 1) { // If on the right edge
1030
            pos_x = 0;
1031
            pos_y += tile_length;
1032
        }
1033
    }
1034
1035
    /* Frame is ready to be output */
1036
    frame->pict_type = AV_PICTURE_TYPE_I;
1037
    frame->key_frame = 1;
1038
1039
    return avpkt->size;
1040
}
1041
1042
static int dng_decode_strip(AVCodecContext *avctx, AVFrame *frame)
1043
{
1044
    TiffContext *s = avctx->priv_data;
1045
1046
    s->jpgframe->width  = s->width;
1047
    s->jpgframe->height = s->height;
1048
1049
    s->avctx_mjpeg->width = s->width;
1050
    s->avctx_mjpeg->height = s->height;
1051
1052
    return dng_decode_jpeg(avctx, frame, s->stripsize, 0, 0, s->width, s->height);
1053
}
1054
1055
21
static int init_image(TiffContext *s, ThreadFrame *frame)
1056
{
1057
    int ret;
1058
21
    int create_gray_palette = 0;
1059
1060
    // make sure there is no aliasing in the following switch
1061

21
    if (s->bpp >= 100 || s->bppcount >= 10) {
1062
        av_log(s->avctx, AV_LOG_ERROR,
1063
               "Unsupported image parameters: bpp=%d, bppcount=%d\n",
1064
               s->bpp, s->bppcount);
1065
        return AVERROR_INVALIDDATA;
1066
    }
1067
1068




21
    switch (s->planar * 1000 + s->bpp * 10 + s->bppcount + s->is_bayer * 10000) {
1069
4
    case 11:
1070
4
        if (!s->palette_is_set) {
1071
4
            s->avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
1072
4
            break;
1073
        }
1074
    case 21:
1075
    case 41:
1076
        s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
1077
        if (!s->palette_is_set) {
1078
            create_gray_palette = 1;
1079
        }
1080
        break;
1081
    case 81:
1082
        s->avctx->pix_fmt = s->palette_is_set ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
1083
        break;
1084
    case 121:
1085
        s->avctx->pix_fmt = AV_PIX_FMT_GRAY12;
1086
        break;
1087
    case 10081:
1088
        switch (AV_RL32(s->pattern)) {
1089
        case 0x02010100:
1090
            s->avctx->pix_fmt = AV_PIX_FMT_BAYER_RGGB8;
1091
            break;
1092
        case 0x00010102:
1093
            s->avctx->pix_fmt = AV_PIX_FMT_BAYER_BGGR8;
1094
            break;
1095
        case 0x01000201:
1096
            s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GBRG8;
1097
            break;
1098
        case 0x01020001:
1099
            s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GRBG8;
1100
            break;
1101
        default:
1102
            av_log(s->avctx, AV_LOG_ERROR, "Unsupported Bayer pattern: 0x%X\n",
1103
                   AV_RL32(s->pattern));
1104
            return AVERROR_PATCHWELCOME;
1105
        }
1106
        break;
1107
    case 10101:
1108
    case 10121:
1109
    case 10141:
1110
    case 10161:
1111
        switch (AV_RL32(s->pattern)) {
1112
        case 0x02010100:
1113
            s->avctx->pix_fmt = AV_PIX_FMT_BAYER_RGGB16;
1114
            break;
1115
        case 0x00010102:
1116
            s->avctx->pix_fmt = AV_PIX_FMT_BAYER_BGGR16;
1117
            break;
1118
        case 0x01000201:
1119
            s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GBRG16;
1120
            break;
1121
        case 0x01020001:
1122
            s->avctx->pix_fmt = AV_PIX_FMT_BAYER_GRBG16;
1123
            break;
1124
        default:
1125
            av_log(s->avctx, AV_LOG_ERROR, "Unsupported Bayer pattern: 0x%X\n",
1126
                   AV_RL32(s->pattern));
1127
            return AVERROR_PATCHWELCOME;
1128
        }
1129
        break;
1130
17
    case 243:
1131
17
        if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
1132
            if (s->subsampling[0] == 1 && s->subsampling[1] == 1) {
1133
                s->avctx->pix_fmt = AV_PIX_FMT_YUV444P;
1134
            } else if (s->subsampling[0] == 2 && s->subsampling[1] == 1) {
1135
                s->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
1136
            } else if (s->subsampling[0] == 4 && s->subsampling[1] == 1) {
1137
                s->avctx->pix_fmt = AV_PIX_FMT_YUV411P;
1138
            } else if (s->subsampling[0] == 1 && s->subsampling[1] == 2) {
1139
                s->avctx->pix_fmt = AV_PIX_FMT_YUV440P;
1140
            } else if (s->subsampling[0] == 2 && s->subsampling[1] == 2) {
1141
                s->avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1142
            } else if (s->subsampling[0] == 4 && s->subsampling[1] == 4) {
1143
                s->avctx->pix_fmt = AV_PIX_FMT_YUV410P;
1144
            } else {
1145
                av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr subsampling\n");
1146
                return AVERROR_PATCHWELCOME;
1147
            }
1148
        } else
1149
17
            s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
1150
17
        break;
1151
    case 161:
1152
        s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GRAY16LE : AV_PIX_FMT_GRAY16BE;
1153
        break;
1154
    case 162:
1155
        s->avctx->pix_fmt = AV_PIX_FMT_YA8;
1156
        break;
1157
    case 322:
1158
        s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YA16LE : AV_PIX_FMT_YA16BE;
1159
        break;
1160
    case 324:
1161
        s->avctx->pix_fmt = s->photometric == TIFF_PHOTOMETRIC_SEPARATED ? AV_PIX_FMT_RGB0 : AV_PIX_FMT_RGBA;
1162
        break;
1163
    case 405:
1164
        if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED)
1165
            s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
1166
        else {
1167
            av_log(s->avctx, AV_LOG_ERROR,
1168
                "bpp=40 without PHOTOMETRIC_SEPARATED is unsupported\n");
1169
            return AVERROR_PATCHWELCOME;
1170
        }
1171
        break;
1172
    case 483:
1173
        s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE  : AV_PIX_FMT_RGB48BE;
1174
        break;
1175
    case 644:
1176
        s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGBA64LE  : AV_PIX_FMT_RGBA64BE;
1177
        break;
1178
    case 1243:
1179
        s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
1180
        break;
1181
    case 1324:
1182
        s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
1183
        break;
1184
    case 1483:
1185
        s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRP16LE : AV_PIX_FMT_GBRP16BE;
1186
        break;
1187
    case 1644:
1188
        s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRAP16LE : AV_PIX_FMT_GBRAP16BE;
1189
        break;
1190
    default:
1191
        av_log(s->avctx, AV_LOG_ERROR,
1192
               "This format is not supported (bpp=%d, bppcount=%d)\n",
1193
               s->bpp, s->bppcount);
1194
        return AVERROR_INVALIDDATA;
1195
    }
1196
1197
21
    if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
1198
        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1199
        if((desc->flags & AV_PIX_FMT_FLAG_RGB) ||
1200
           !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) ||
1201
           desc->nb_components < 3) {
1202
            av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr variant\n");
1203
            return AVERROR_INVALIDDATA;
1204
        }
1205
    }
1206
1207

21
    if (s->width != s->avctx->width || s->height != s->avctx->height) {
1208
5
        ret = ff_set_dimensions(s->avctx, s->width, s->height);
1209
5
        if (ret < 0)
1210
            return ret;
1211
    }
1212
21
    if ((ret = ff_thread_get_buffer(s->avctx, frame, 0)) < 0)
1213
        return ret;
1214
21
    if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1215
        if (!create_gray_palette)
1216
            memcpy(frame->f->data[1], s->palette, sizeof(s->palette));
1217
        else {
1218
            /* make default grayscale pal */
1219
            int i;
1220
            uint32_t *pal = (uint32_t *)frame->f->data[1];
1221
            for (i = 0; i < 1<<s->bpp; i++)
1222
                pal[i] = 0xFFU << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101;
1223
        }
1224
    }
1225
21
    return 0;
1226
}
1227
1228
42
static void set_sar(TiffContext *s, unsigned tag, unsigned num, unsigned den)
1229
{
1230
42
    int offset = tag == TIFF_YRES ? 2 : 0;
1231
42
    s->res[offset++] = num;
1232
42
    s->res[offset]   = den;
1233


42
    if (s->res[0] && s->res[1] && s->res[2] && s->res[3]) {
1234
33
        uint64_t num = s->res[2] * (uint64_t)s->res[1];
1235
33
        uint64_t den = s->res[0] * (uint64_t)s->res[3];
1236

33
        if (num > INT64_MAX || den > INT64_MAX) {
1237
            num = num >> 1;
1238
            den = den >> 1;
1239
        }
1240
33
        av_reduce(&s->avctx->sample_aspect_ratio.num, &s->avctx->sample_aspect_ratio.den,
1241
                  num, den, INT32_MAX);
1242
33
        if (!s->avctx->sample_aspect_ratio.den)
1243
            s->avctx->sample_aspect_ratio = (AVRational) {0, 1};
1244
    }
1245
42
}
1246
1247
299
static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
1248
{
1249
    AVFrameSideData *sd;
1250
    GetByteContext gb_temp;
1251
299
    unsigned tag, type, count, off, value = 0, value2 = 1; // value2 is a denominator so init. to 1
1252
    int i, start;
1253
    int pos;
1254
    int ret;
1255
    double *dp;
1256
1257
299
    ret = ff_tread_tag(&s->gb, s->le, &tag, &type, &count, &start);
1258
299
    if (ret < 0) {
1259
        goto end;
1260
    }
1261
299
    if (tag <= s->last_tag)
1262
        return AVERROR_INVALIDDATA;
1263
1264
    // We ignore TIFF_STRIP_SIZE as it is sometimes in the logic but wrong order around TIFF_STRIP_OFFS
1265
299
    if (tag != TIFF_STRIP_SIZE)
1266
278
        s->last_tag = tag;
1267
1268
299
    off = bytestream2_tell(&s->gb);
1269
299
    if (count == 1) {
1270

230
        switch (type) {
1271
188
        case TIFF_BYTE:
1272
        case TIFF_SHORT:
1273
        case TIFF_LONG:
1274
188
            value = ff_tget(&s->gb, type, s->le);
1275
188
            break;
1276
42
        case TIFF_RATIONAL:
1277
42
            value  = ff_tget(&s->gb, TIFF_LONG, s->le);
1278
42
            value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
1279
42
            if (!value2) {
1280
                av_log(s->avctx, AV_LOG_ERROR, "Invalid denominator in rational\n");
1281
                return AVERROR_INVALIDDATA;
1282
            }
1283
1284
42
            break;
1285
        case TIFF_STRING:
1286
            if (count <= 4) {
1287
                break;
1288
            }
1289
        default:
1290
            value = UINT_MAX;
1291
        }
1292
69
    }
1293
1294












299
    switch (tag) {
1295
15
    case TIFF_SUBFILE:
1296
15
        s->is_thumbnail = (value != 0);
1297
15
        break;
1298
21
    case TIFF_WIDTH:
1299
21
        s->width = value;
1300
21
        break;
1301
21
    case TIFF_HEIGHT:
1302
21
        s->height = value;
1303
21
        break;
1304
21
    case TIFF_BPP:
1305

21
        if (count > 5 || count <= 0) {
1306
            av_log(s->avctx, AV_LOG_ERROR,
1307
                   "This format is not supported (bpp=%d, %d components)\n",
1308
                   value, count);
1309
            return AVERROR_INVALIDDATA;
1310
        }
1311
21
        s->bppcount = count;
1312
21
        if (count == 1)
1313
4
            s->bpp = value;
1314
        else {
1315
17
            switch (type) {
1316
17
            case TIFF_BYTE:
1317
            case TIFF_SHORT:
1318
            case TIFF_LONG:
1319
17
                s->bpp = 0;
1320
17
                if (bytestream2_get_bytes_left(&s->gb) < type_sizes[type] * count)
1321
                    return AVERROR_INVALIDDATA;
1322
68
                for (i = 0; i < count; i++)
1323
51
                    s->bpp += ff_tget(&s->gb, type, s->le);
1324
17
                break;
1325
            default:
1326
                s->bpp = -1;
1327
            }
1328
        }
1329
21
        break;
1330
21
    case TIFF_SAMPLES_PER_PIXEL:
1331
21
        if (count != 1) {
1332
            av_log(s->avctx, AV_LOG_ERROR,
1333
                   "Samples per pixel requires a single value, many provided\n");
1334
            return AVERROR_INVALIDDATA;
1335
        }
1336

21
        if (value > 5 || value <= 0) {
1337
            av_log(s->avctx, AV_LOG_ERROR,
1338
                   "Invalid samples per pixel %d\n", value);
1339
            return AVERROR_INVALIDDATA;
1340
        }
1341
21
        if (s->bppcount == 1)
1342
4
            s->bpp *= value;
1343
21
        s->bppcount = value;
1344
21
        break;
1345
21
    case TIFF_COMPR:
1346
21
        s->compr     = value;
1347
21
        av_log(s->avctx, AV_LOG_DEBUG, "compression: %d\n", s->compr);
1348
21
        s->predictor = 0;
1349

21
        switch (s->compr) {
1350
17
        case TIFF_RAW:
1351
        case TIFF_PACKBITS:
1352
        case TIFF_LZW:
1353
        case TIFF_CCITT_RLE:
1354
17
            break;
1355
4
        case TIFF_G3:
1356
        case TIFF_G4:
1357
4
            s->fax_opts = 0;
1358
4
            break;
1359
        case TIFF_DEFLATE:
1360
        case TIFF_ADOBE_DEFLATE:
1361
#if CONFIG_ZLIB
1362
            break;
1363
#else
1364
            av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
1365
            return AVERROR(ENOSYS);
1366
#endif
1367
        case TIFF_JPEG:
1368
        case TIFF_NEWJPEG:
1369
            s->is_jpeg = 1;
1370
            break;
1371
        case TIFF_LZMA:
1372
#if CONFIG_LZMA
1373
            break;
1374
#else
1375
            av_log(s->avctx, AV_LOG_ERROR, "LZMA not compiled in\n");
1376
            return AVERROR(ENOSYS);
1377
#endif
1378
        default:
1379
            av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
1380
                   s->compr);
1381
            return AVERROR_INVALIDDATA;
1382
        }
1383
21
        break;
1384
21
    case TIFF_ROWSPERSTRIP:
1385

21
        if (!value || (type == TIFF_LONG && value == UINT_MAX))
1386
            value = s->height;
1387
21
        s->rps = FFMIN(value, s->height);
1388
21
        break;
1389
21
    case TIFF_STRIP_OFFS:
1390
21
        if (count == 1) {
1391
2
            if (value > INT_MAX) {
1392
                av_log(s->avctx, AV_LOG_ERROR,
1393
                    "strippos %u too large\n", value);
1394
                return AVERROR_INVALIDDATA;
1395
            }
1396
2
            s->strippos = 0;
1397
2
            s->stripoff = value;
1398
        } else
1399
19
            s->strippos = off;
1400
21
        s->strips = count;
1401
21
        if (s->strips == 1)
1402
2
            s->rps = s->height;
1403
21
        s->sot = type;
1404
21
        break;
1405
21
    case TIFF_STRIP_SIZE:
1406
21
        if (count == 1) {
1407
2
            if (value > INT_MAX) {
1408
                av_log(s->avctx, AV_LOG_ERROR,
1409
                    "stripsize %u too large\n", value);
1410
                return AVERROR_INVALIDDATA;
1411
            }
1412
2
            s->stripsizesoff = 0;
1413
2
            s->stripsize     = value;
1414
2
            s->strips        = 1;
1415
        } else {
1416
19
            s->stripsizesoff = off;
1417
        }
1418
21
        s->strips = count;
1419
21
        s->sstype = type;
1420
21
        break;
1421
42
    case TIFF_XRES:
1422
    case TIFF_YRES:
1423
42
        set_sar(s, tag, value, value2);
1424
42
        break;
1425
    case TIFF_TILE_OFFSETS:
1426
        s->tile_offsets_offset = off;
1427
        s->tile_count = count;
1428
        s->is_tiled = 1;
1429
        break;
1430
    case TIFF_TILE_BYTE_COUNTS:
1431
        s->tile_byte_counts_offset = off;
1432
        break;
1433
    case TIFF_TILE_LENGTH:
1434
        s->tile_length = value;
1435
        break;
1436
    case TIFF_TILE_WIDTH:
1437
        s->tile_width = value;
1438
        break;
1439
    case TIFF_PREDICTOR:
1440
        s->predictor = value;
1441
        break;
1442
    case TIFF_SUB_IFDS:
1443
        if (count == 1)
1444
            s->sub_ifd = value;
1445
        else if (count > 1)
1446
            s->sub_ifd = ff_tget(&s->gb, TIFF_LONG, s->le); /** Only get the first SubIFD */
1447
        break;
1448
    case DNG_LINEARIZATION_TABLE:
1449
        if (count > FF_ARRAY_ELEMS(s->dng_lut))
1450
            return AVERROR_INVALIDDATA;
1451
        for (int i = 0; i < count; i++)
1452
            s->dng_lut[i] = ff_tget(&s->gb, type, s->le);
1453
        break;
1454
    case DNG_BLACK_LEVEL:
1455
        if (count > 1) {    /* Use the first value in the pattern (assume they're all the same) */
1456
            if (type == TIFF_RATIONAL) {
1457
                value  = ff_tget(&s->gb, TIFF_LONG, s->le);
1458
                value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
1459
                if (!value2) {
1460
                    av_log(s->avctx, AV_LOG_ERROR, "Invalid black level denominator\n");
1461
                    return AVERROR_INVALIDDATA;
1462
                }
1463
1464
                s->black_level = value / value2;
1465
            } else
1466
                s->black_level = ff_tget(&s->gb, type, s->le);
1467
            av_log(s->avctx, AV_LOG_WARNING, "Assuming black level pattern values are identical\n");
1468
        } else {
1469
            s->black_level = value / value2;
1470
        }
1471
        break;
1472
    case DNG_WHITE_LEVEL:
1473
        s->white_level = value;
1474
        break;
1475
    case TIFF_CFA_PATTERN_DIM:
1476
        if (count != 2 || (ff_tget(&s->gb, type, s->le) != 2 &&
1477
                           ff_tget(&s->gb, type, s->le) != 2)) {
1478
            av_log(s->avctx, AV_LOG_ERROR, "CFA Pattern dimensions are not 2x2\n");
1479
            return AVERROR_INVALIDDATA;
1480
        }
1481
        break;
1482
    case TIFF_CFA_PATTERN:
1483
        s->is_bayer = 1;
1484
        s->pattern[0] = ff_tget(&s->gb, type, s->le);
1485
        s->pattern[1] = ff_tget(&s->gb, type, s->le);
1486
        s->pattern[2] = ff_tget(&s->gb, type, s->le);
1487
        s->pattern[3] = ff_tget(&s->gb, type, s->le);
1488
        break;
1489
21
    case TIFF_PHOTOMETRIC:
1490
        switch (value) {
1491
21
        case TIFF_PHOTOMETRIC_WHITE_IS_ZERO:
1492
        case TIFF_PHOTOMETRIC_BLACK_IS_ZERO:
1493
        case TIFF_PHOTOMETRIC_RGB:
1494
        case TIFF_PHOTOMETRIC_PALETTE:
1495
        case TIFF_PHOTOMETRIC_SEPARATED:
1496
        case TIFF_PHOTOMETRIC_YCBCR:
1497
        case TIFF_PHOTOMETRIC_CFA:
1498
        case TIFF_PHOTOMETRIC_LINEAR_RAW: // Used by DNG images
1499
21
            s->photometric = value;
1500
21
            break;
1501
        case TIFF_PHOTOMETRIC_ALPHA_MASK:
1502
        case TIFF_PHOTOMETRIC_CIE_LAB:
1503
        case TIFF_PHOTOMETRIC_ICC_LAB:
1504
        case TIFF_PHOTOMETRIC_ITU_LAB:
1505
        case TIFF_PHOTOMETRIC_LOG_L:
1506
        case TIFF_PHOTOMETRIC_LOG_LUV:
1507
            avpriv_report_missing_feature(s->avctx,
1508
                                          "PhotometricInterpretation 0x%04X",
1509
                                          value);
1510
            return AVERROR_PATCHWELCOME;
1511
        default:
1512
            av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
1513
                   "unknown\n", value);
1514
            return AVERROR_INVALIDDATA;
1515
        }
1516
21
        break;
1517
6
    case TIFF_FILL_ORDER:
1518

6
        if (value < 1 || value > 2) {
1519
            av_log(s->avctx, AV_LOG_ERROR,
1520
                   "Unknown FillOrder value %d, trying default one\n", value);
1521
            value = 1;
1522
        }
1523
6
        s->fill_order = value - 1;
1524
6
        break;
1525
    case TIFF_PAL: {
1526
        GetByteContext pal_gb[3];
1527
        off = type_sizes[type];
1528
        if (count / 3 > 256 ||
1529
            bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
1530
            return AVERROR_INVALIDDATA;
1531
1532
        pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
1533
        bytestream2_skip(&pal_gb[1], count / 3 * off);
1534
        bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
1535
1536
        off = (type_sizes[type] - 1) << 3;
1537
        if (off > 31U) {
1538
            av_log(s->avctx, AV_LOG_ERROR, "palette shift %d is out of range\n", off);
1539
            return AVERROR_INVALIDDATA;
1540
        }
1541
1542
        for (i = 0; i < count / 3; i++) {
1543
            uint32_t p = 0xFF000000;
1544
            p |= (ff_tget(&pal_gb[0], type, s->le) >> off) << 16;
1545
            p |= (ff_tget(&pal_gb[1], type, s->le) >> off) << 8;
1546
            p |=  ff_tget(&pal_gb[2], type, s->le) >> off;
1547
            s->palette[i] = p;
1548
        }
1549
        s->palette_is_set = 1;
1550
        break;
1551
    }
1552
6
    case TIFF_PLANAR:
1553
6
        s->planar = value == 2;
1554
6
        break;
1555
    case TIFF_YCBCR_SUBSAMPLING:
1556
        if (count != 2) {
1557
            av_log(s->avctx, AV_LOG_ERROR, "subsample count invalid\n");
1558
            return AVERROR_INVALIDDATA;
1559
        }
1560
        for (i = 0; i < count; i++) {
1561
            s->subsampling[i] = ff_tget(&s->gb, type, s->le);
1562
            if (s->subsampling[i] <= 0) {
1563
                av_log(s->avctx, AV_LOG_ERROR, "subsampling %d is invalid\n", s->subsampling[i]);
1564
                s->subsampling[i] = 1;
1565
                return AVERROR_INVALIDDATA;
1566
            }
1567
        }
1568
        break;
1569
    case TIFF_T4OPTIONS:
1570
        if (s->compr == TIFF_G3)
1571
            s->fax_opts = value;
1572
        break;
1573
    case TIFF_T6OPTIONS:
1574
        if (s->compr == TIFF_G4)
1575
            s->fax_opts = value;
1576
        break;
1577
#define ADD_METADATA(count, name, sep)\
1578
    if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\
1579
        av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
1580
        goto end;\
1581
    }
1582
    case TIFF_MODEL_PIXEL_SCALE:
1583
        ADD_METADATA(count, "ModelPixelScaleTag", NULL);
1584
        break;
1585
    case TIFF_MODEL_TRANSFORMATION:
1586
        ADD_METADATA(count, "ModelTransformationTag", NULL);
1587
        break;
1588
    case TIFF_MODEL_TIEPOINT:
1589
        ADD_METADATA(count, "ModelTiepointTag", NULL);
1590
        break;
1591
    case TIFF_GEO_KEY_DIRECTORY:
1592
        if (s->geotag_count) {
1593
            avpriv_request_sample(s->avctx, "Multiple geo key directories\n");
1594
            return AVERROR_INVALIDDATA;
1595
        }
1596
        ADD_METADATA(1, "GeoTIFF_Version", NULL);
1597
        ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");
1598
        s->geotag_count   = ff_tget_short(&s->gb, s->le);
1599
        if (s->geotag_count > count / 4 - 1) {
1600
            s->geotag_count = count / 4 - 1;
1601
            av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");
1602
        }
1603
        if (   bytestream2_get_bytes_left(&s->gb) < s->geotag_count * sizeof(int16_t) * 4
1604
            || s->geotag_count == 0) {
1605
            s->geotag_count = 0;
1606
            return -1;
1607
        }
1608
        s->geotags = av_mallocz_array(s->geotag_count, sizeof(TiffGeoTag));
1609
        if (!s->geotags) {
1610
            av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1611
            s->geotag_count = 0;
1612
            goto end;
1613
        }
1614
        for (i = 0; i < s->geotag_count; i++) {
1615
            s->geotags[i].key    = ff_tget_short(&s->gb, s->le);
1616
            s->geotags[i].type   = ff_tget_short(&s->gb, s->le);
1617
            s->geotags[i].count  = ff_tget_short(&s->gb, s->le);
1618
1619
            if (!s->geotags[i].type)
1620
                s->geotags[i].val  = get_geokey_val(s->geotags[i].key, ff_tget_short(&s->gb, s->le));
1621
            else
1622
                s->geotags[i].offset = ff_tget_short(&s->gb, s->le);
1623
        }
1624
        break;
1625
    case TIFF_GEO_DOUBLE_PARAMS:
1626
        if (count >= INT_MAX / sizeof(int64_t))
1627
            return AVERROR_INVALIDDATA;
1628
        if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
1629
            return AVERROR_INVALIDDATA;
1630
        dp = av_malloc_array(count, sizeof(double));
1631
        if (!dp) {
1632
            av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1633
            goto end;
1634
        }
1635
        for (i = 0; i < count; i++)
1636
            dp[i] = ff_tget_double(&s->gb, s->le);
1637
        for (i = 0; i < s->geotag_count; i++) {
1638
            if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) {
1639
                if (s->geotags[i].count == 0
1640
                    || s->geotags[i].offset + s->geotags[i].count > count) {
1641
                    av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1642
                } else if (s->geotags[i].val) {
1643
                    av_log(s->avctx, AV_LOG_WARNING, "Duplicate GeoTIFF key %d\n", s->geotags[i].key);
1644
                } else {
1645
                    char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");
1646
                    if (!ap) {
1647
                        av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1648
                        av_freep(&dp);
1649
                        return AVERROR(ENOMEM);
1650
                    }
1651
                    s->geotags[i].val = ap;
1652
                }
1653
            }
1654
        }
1655
        av_freep(&dp);
1656
        break;
1657
    case TIFF_GEO_ASCII_PARAMS:
1658
        pos = bytestream2_tell(&s->gb);
1659
        for (i = 0; i < s->geotag_count; i++) {
1660
            if (s->geotags[i].type == TIFF_GEO_ASCII_PARAMS) {
1661
                if (s->geotags[i].count == 0
1662
                    || s->geotags[i].offset +  s->geotags[i].count > count) {
1663
                    av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
1664
                } else {
1665
                    char *ap;
1666
1667
                    bytestream2_seek(&s->gb, pos + s->geotags[i].offset, SEEK_SET);
1668
                    if (bytestream2_get_bytes_left(&s->gb) < s->geotags[i].count)
1669
                        return AVERROR_INVALIDDATA;
1670
                    if (s->geotags[i].val)
1671
                        return AVERROR_INVALIDDATA;
1672
                    ap = av_malloc(s->geotags[i].count);
1673
                    if (!ap) {
1674
                        av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
1675
                        return AVERROR(ENOMEM);
1676
                    }
1677
                    bytestream2_get_bufferu(&s->gb, ap, s->geotags[i].count);
1678
                    ap[s->geotags[i].count - 1] = '\0'; //replace the "|" delimiter with a 0 byte
1679
                    s->geotags[i].val = ap;
1680
                }
1681
            }
1682
        }
1683
        break;
1684
    case TIFF_ICC_PROFILE:
1685
        gb_temp = s->gb;
1686
        bytestream2_seek(&gb_temp, SEEK_SET, off);
1687
1688
        if (bytestream2_get_bytes_left(&gb_temp) < count)
1689
            return AVERROR_INVALIDDATA;
1690
1691
        sd = av_frame_new_side_data(frame, AV_FRAME_DATA_ICC_PROFILE, count);
1692
        if (!sd)
1693
            return AVERROR(ENOMEM);
1694
1695
        bytestream2_get_bufferu(&gb_temp, sd->data, count);
1696
        break;
1697
    case TIFF_ARTIST:
1698
        ADD_METADATA(count, "artist", NULL);
1699
        break;
1700
    case TIFF_COPYRIGHT:
1701
        ADD_METADATA(count, "copyright", NULL);
1702
        break;
1703
    case TIFF_DATE:
1704
        ADD_METADATA(count, "date", NULL);
1705
        break;
1706
6
    case TIFF_DOCUMENT_NAME:
1707
6
        ADD_METADATA(count, "document_name", NULL);
1708
6
        break;
1709
    case TIFF_HOST_COMPUTER:
1710
        ADD_METADATA(count, "computer", NULL);
1711
        break;
1712
4
    case TIFF_IMAGE_DESCRIPTION:
1713
4
        ADD_METADATA(count, "description", NULL);
1714
4
        break;
1715
    case TIFF_MAKE:
1716
        ADD_METADATA(count, "make", NULL);
1717
        break;
1718
    case TIFF_MODEL:
1719
        ADD_METADATA(count, "model", NULL);
1720
        break;
1721
    case TIFF_PAGE_NAME:
1722
        ADD_METADATA(count, "page_name", NULL);
1723
        break;
1724
2
    case TIFF_PAGE_NUMBER:
1725
2
        ADD_METADATA(count, "page_number", " / ");
1726
        // need to seek back to re-read the page number
1727
2
        bytestream2_seek(&s->gb, -count * sizeof(uint16_t), SEEK_CUR);
1728
        // read the page number
1729
2
        s->cur_page = ff_tget(&s->gb, TIFF_SHORT, s->le);
1730
        // get back to where we were before the previous seek
1731
2
        bytestream2_seek(&s->gb, count * sizeof(uint16_t) - sizeof(uint16_t), SEEK_CUR);
1732
2
        break;
1733
2
    case TIFF_SOFTWARE_NAME:
1734
2
        ADD_METADATA(count, "software", NULL);
1735
2
        break;
1736
    case DNG_VERSION:
1737
        if (count == 4) {
1738
            unsigned int ver[4];
1739
            ver[0] = ff_tget(&s->gb, type, s->le);
1740
            ver[1] = ff_tget(&s->gb, type, s->le);
1741
            ver[2] = ff_tget(&s->gb, type, s->le);
1742
            ver[3] = ff_tget(&s->gb, type, s->le);
1743
1744
            av_log(s->avctx, AV_LOG_DEBUG, "DNG file, version %u.%u.%u.%u\n",
1745
                ver[0], ver[1], ver[2], ver[3]);
1746
1747
            tiff_set_type(s, TIFF_TYPE_DNG);
1748
        }
1749
        break;
1750
    case CINEMADNG_TIME_CODES:
1751
    case CINEMADNG_FRAME_RATE:
1752
    case CINEMADNG_T_STOP:
1753
    case CINEMADNG_REEL_NAME:
1754
    case CINEMADNG_CAMERA_LABEL:
1755
        tiff_set_type(s, TIFF_TYPE_CINEMADNG);
1756
        break;
1757
27
    default:
1758
27
        if (s->avctx->err_recognition & AV_EF_EXPLODE) {
1759
            av_log(s->avctx, AV_LOG_ERROR,
1760
                   "Unknown or unsupported tag %d/0x%0X\n",
1761
                   tag, tag);
1762
            return AVERROR_INVALIDDATA;
1763
        }
1764
    }
1765
27
end:
1766
299
    if (s->bpp > 64U) {
1767
        av_log(s->avctx, AV_LOG_ERROR,
1768
                "This format is not supported (bpp=%d, %d components)\n",
1769
                s->bpp, count);
1770
        s->bpp = 0;
1771
        return AVERROR_INVALIDDATA;
1772
    }
1773
299
    bytestream2_seek(&s->gb, start, SEEK_SET);
1774
299
    return 0;
1775
}
1776
1777
21
static int decode_frame(AVCodecContext *avctx,
1778
                        void *data, int *got_frame, AVPacket *avpkt)
1779
{
1780
21
    TiffContext *const s = avctx->priv_data;
1781
21
    AVFrame *const p = data;
1782
21
    ThreadFrame frame = { .f = data };
1783
    unsigned off, last_off;
1784
    int le, ret, plane, planes;
1785
    int i, j, entries, stride;
1786
    unsigned soff, ssize;
1787
    uint8_t *dst;
1788
    GetByteContext stripsizes;
1789
    GetByteContext stripdata;
1790
    int retry_for_subifd, retry_for_page;
1791
    int is_dng;
1792
    int has_tile_bits, has_strip_bits;
1793
1794
21
    bytestream2_init(&s->gb, avpkt->data, avpkt->size);
1795
1796
    // parse image header
1797
21
    if ((ret = ff_tdecode_header(&s->gb, &le, &off))) {
1798
        av_log(avctx, AV_LOG_ERROR, "Invalid TIFF header\n");
1799
        return ret;
1800

21
    } else if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
1801
        av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
1802
        return AVERROR_INVALIDDATA;
1803
    }
1804
21
    s->le          = le;
1805
    // TIFF_BPP is not a required tag and defaults to 1
1806
1807
21
    s->tiff_type   = TIFF_TYPE_TIFF;
1808
21
again:
1809
21
    s->is_thumbnail = 0;
1810
21
    s->bppcount    = s->bpp = 1;
1811
21
    s->photometric = TIFF_PHOTOMETRIC_NONE;
1812
21
    s->compr       = TIFF_RAW;
1813
21
    s->fill_order  = 0;
1814
21
    s->white_level = 0;
1815
21
    s->is_bayer    = 0;
1816
21
    s->is_tiled    = 0;
1817
21
    s->is_jpeg     = 0;
1818
21
    s->cur_page    = 0;
1819
21
    s->last_tag    = 0;
1820
1821
1376277
    for (i = 0; i < 65536; i++)
1822
1376256
        s->dng_lut[i] = i;
1823
1824
21
    free_geotags(s);
1825
1826
    // Reset these offsets so we can tell if they were set this frame
1827
21
    s->stripsizesoff = s->strippos = 0;
1828
    /* parse image file directory */
1829
21
    bytestream2_seek(&s->gb, off, SEEK_SET);
1830
21
    entries = ff_tget_short(&s->gb, le);
1831
21
    if (bytestream2_get_bytes_left(&s->gb) < entries * 12)
1832
        return AVERROR_INVALIDDATA;
1833
320
    for (i = 0; i < entries; i++) {
1834
299
        if ((ret = tiff_decode_tag(s, p)) < 0)
1835
            return ret;
1836
    }
1837
1838

21
    if (s->get_thumbnail && !s->is_thumbnail) {
1839
        av_log(avctx, AV_LOG_INFO, "No embedded thumbnail present\n");
1840
        return AVERROR_EOF;
1841
    }
1842
1843
    /** whether we should process this IFD's SubIFD */
1844


21
    retry_for_subifd = s->sub_ifd && (s->get_subimage || (!s->get_thumbnail && s->is_thumbnail));
1845
    /** whether we should process this multi-page IFD's next page */
1846

21
    retry_for_page = s->get_page && s->cur_page + 1 < s->get_page;  // get_page is 1-indexed
1847
1848
21
    last_off = off;
1849
21
    if (retry_for_page) {
1850
        // set offset to the next IFD
1851
        off = ff_tget_long(&s->gb, le);
1852
21
    } else if (retry_for_subifd) {
1853
        // set offset to the SubIFD
1854
        off = s->sub_ifd;
1855
    }
1856
1857

21
    if (retry_for_subifd || retry_for_page) {
1858
        if (!off) {
1859
            av_log(avctx, AV_LOG_ERROR, "Requested entry not found\n");
1860
            return AVERROR_INVALIDDATA;
1861
        }
1862
        if (off <= last_off) {
1863
            avpriv_request_sample(s->avctx, "non increasing IFD offset\n");
1864
            return AVERROR_INVALIDDATA;
1865
        }
1866
        if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
1867
            av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
1868
            return AVERROR_INVALIDDATA;
1869
        }
1870
        s->sub_ifd = 0;
1871
        goto again;
1872
    }
1873
1874
    /* At this point we've decided on which (Sub)IFD to process */
1875
1876

21
    is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG);
1877
1878
21
    for (i = 0; i<s->geotag_count; i++) {
1879
        const char *keyname = get_geokey_name(s->geotags[i].key);
1880
        if (!keyname) {
1881
            av_log(avctx, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[i].key);
1882
            continue;
1883
        }
1884
        if (get_geokey_type(s->geotags[i].key) != s->geotags[i].type) {
1885
            av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key);
1886
            continue;
1887
        }
1888
        ret = av_dict_set(&p->metadata, keyname, s->geotags[i].val, 0);
1889
        if (ret<0) {
1890
            av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);
1891
            return ret;
1892
        }
1893
    }
1894
1895
21
    if (is_dng) {
1896
        int bps;
1897
1898
        if (s->bpp % s->bppcount)
1899
            return AVERROR_INVALIDDATA;
1900
        bps = s->bpp / s->bppcount;
1901
        if (bps < 8 || bps > 32)
1902
            return AVERROR_INVALIDDATA;
1903
1904
        if (s->white_level == 0)
1905
            s->white_level = (1LL << bps) - 1; /* Default value as per the spec */
1906
1907
        if (s->white_level <= s->black_level) {
1908
            av_log(avctx, AV_LOG_ERROR, "BlackLevel (%"PRId32") must be less than WhiteLevel (%"PRId32")\n",
1909
                s->black_level, s->white_level);
1910
            return AVERROR_INVALIDDATA;
1911
        }
1912
1913
        if (s->planar)
1914
            return AVERROR_PATCHWELCOME;
1915
    }
1916
1917

21
    if (!s->is_tiled && !s->strippos && !s->stripoff) {
1918
        av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
1919
        return AVERROR_INVALIDDATA;
1920
    }
1921
1922



21
    has_tile_bits  = s->is_tiled || s->tile_byte_counts_offset || s->tile_offsets_offset || s->tile_width || s->tile_length || s->tile_count;
1923




21
    has_strip_bits = s->strippos || s->strips || s->stripoff || s->rps || s->sot || s->sstype || s->stripsize || s->stripsizesoff;
1924
1925

21
    if (has_tile_bits && has_strip_bits) {
1926
        av_log(avctx, AV_LOG_ERROR, "Tiled TIFF is not allowed to strip\n");
1927
        return AVERROR_INVALIDDATA;
1928
    }
1929
1930
    /* now we have the data and may start decoding */
1931
21
    if ((ret = init_image(s, &frame)) < 0)
1932
        return ret;
1933
1934
21
    if (!s->is_tiled) {
1935

21
        if (s->strips == 1 && !s->stripsize) {
1936
            av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
1937
            s->stripsize = avpkt->size - s->stripoff;
1938
        }
1939
1940
21
        if (s->stripsizesoff) {
1941
19
            if (s->stripsizesoff >= (unsigned)avpkt->size)
1942
                return AVERROR_INVALIDDATA;
1943
19
            bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
1944
19
                            avpkt->size - s->stripsizesoff);
1945
        }
1946
21
        if (s->strippos) {
1947
19
            if (s->strippos >= (unsigned)avpkt->size)
1948
                return AVERROR_INVALIDDATA;
1949
19
            bytestream2_init(&stripdata, avpkt->data + s->strippos,
1950
19
                            avpkt->size - s->strippos);
1951
        }
1952
1953

21
        if (s->rps <= 0 || s->rps % s->subsampling[1]) {
1954
            av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);
1955
            return AVERROR_INVALIDDATA;
1956
        }
1957
    }
1958
1959
21
    if (s->photometric == TIFF_PHOTOMETRIC_LINEAR_RAW ||
1960
21
        s->photometric == TIFF_PHOTOMETRIC_CFA) {
1961
        p->color_trc = AVCOL_TRC_LINEAR;
1962
21
    } else if (s->photometric == TIFF_PHOTOMETRIC_BLACK_IS_ZERO) {
1963
        p->color_trc = AVCOL_TRC_GAMMA22;
1964
    }
1965
1966
    /* Handle DNG images with JPEG-compressed tiles */
1967
1968

21
    if (is_dng && s->is_tiled) {
1969
        if (!s->is_jpeg) {
1970
            avpriv_report_missing_feature(avctx, "DNG uncompressed tiled images");
1971
            return AVERROR_PATCHWELCOME;
1972
        } else if (!s->is_bayer) {
1973
            avpriv_report_missing_feature(avctx, "DNG JPG-compressed tiled non-bayer-encoded images");
1974
            return AVERROR_PATCHWELCOME;
1975
        } else {
1976
            if ((ret = dng_decode_tiles(avctx, (AVFrame*)data, avpkt)) > 0)
1977
                *got_frame = 1;
1978
            return ret;
1979
        }
1980
    }
1981
1982
    /* Handle TIFF images and DNG images with uncompressed strips (non-tiled) */
1983
1984
21
    planes = s->planar ? s->bppcount : 1;
1985
42
    for (plane = 0; plane < planes; plane++) {
1986
21
        uint8_t *five_planes = NULL;
1987
21
        int remaining = avpkt->size;
1988
        int decoded_height;
1989
21
        stride = p->linesize[plane];
1990
21
        dst = p->data[plane];
1991
21
        if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED &&
1992
            s->avctx->pix_fmt == AV_PIX_FMT_RGBA) {
1993
            stride = stride * 5 / 4;
1994
            five_planes =
1995
            dst = av_malloc(stride * s->height);
1996
            if (!dst)
1997
                return AVERROR(ENOMEM);
1998
        }
1999
705
        for (i = 0; i < s->height; i += s->rps) {
2000
684
            if (i)
2001
663
                dst += s->rps * stride;
2002
684
            if (s->stripsizesoff)
2003
682
                ssize = ff_tget(&stripsizes, s->sstype, le);
2004
            else
2005
2
                ssize = s->stripsize;
2006
2007
684
            if (s->strippos)
2008
682
                soff = ff_tget(&stripdata, s->sot, le);
2009
            else
2010
2
                soff = s->stripoff;
2011
2012

684
            if (soff > avpkt->size || ssize > avpkt->size - soff || ssize > remaining) {
2013
                av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
2014
                av_freep(&five_planes);
2015
                return AVERROR_INVALIDDATA;
2016
            }
2017
684
            remaining -= ssize;
2018
684
            if ((ret = tiff_unpack_strip(s, p, dst, stride, avpkt->data + soff, ssize, i,
2019
684
                                         FFMIN(s->rps, s->height - i))) < 0) {
2020
                if (avctx->err_recognition & AV_EF_EXPLODE) {
2021
                    av_freep(&five_planes);
2022
                    return ret;
2023
                }
2024
                break;
2025
            }
2026
        }
2027
21
        decoded_height = FFMIN(i, s->height);
2028
2029
21
        if (s->predictor == 2) {
2030
            if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
2031
                av_log(s->avctx, AV_LOG_ERROR, "predictor == 2 with YUV is unsupported");
2032
                return AVERROR_PATCHWELCOME;
2033
            }
2034
            dst   = five_planes ? five_planes : p->data[plane];
2035
            soff  = s->bpp >> 3;
2036
            if (s->planar)
2037
                soff  = FFMAX(soff / s->bppcount, 1);
2038
            ssize = s->width * soff;
2039
            if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE ||
2040
                s->avctx->pix_fmt == AV_PIX_FMT_RGBA64LE ||
2041
                s->avctx->pix_fmt == AV_PIX_FMT_GRAY16LE ||
2042
                s->avctx->pix_fmt == AV_PIX_FMT_YA16LE ||
2043
                s->avctx->pix_fmt == AV_PIX_FMT_GBRP16LE ||
2044
                s->avctx->pix_fmt == AV_PIX_FMT_GBRAP16LE) {
2045
                for (i = 0; i < decoded_height; i++) {
2046
                    for (j = soff; j < ssize; j += 2)
2047
                        AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
2048
                    dst += stride;
2049
                }
2050
            } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
2051
                       s->avctx->pix_fmt == AV_PIX_FMT_RGBA64BE ||
2052
                       s->avctx->pix_fmt == AV_PIX_FMT_GRAY16BE ||
2053
                       s->avctx->pix_fmt == AV_PIX_FMT_YA16BE ||
2054
                       s->avctx->pix_fmt == AV_PIX_FMT_GBRP16BE ||
2055
                       s->avctx->pix_fmt == AV_PIX_FMT_GBRAP16BE) {
2056
                for (i = 0; i < decoded_height; i++) {
2057
                    for (j = soff; j < ssize; j += 2)
2058
                        AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
2059
                    dst += stride;
2060
                }
2061
            } else {
2062
                for (i = 0; i < decoded_height; i++) {
2063
                    for (j = soff; j < ssize; j++)
2064
                        dst[j] += dst[j - soff];
2065
                    dst += stride;
2066
                }
2067
            }
2068
        }
2069
2070
21
        if (s->photometric == TIFF_PHOTOMETRIC_WHITE_IS_ZERO) {
2071
4
            int c = (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255);
2072
4
            dst = p->data[plane];
2073
12996
            for (i = 0; i < s->height; i++) {
2074
4170432
                for (j = 0; j < stride; j++)
2075
4157440
                    dst[j] = c - dst[j];
2076
12992
                dst += stride;
2077
            }
2078
        }
2079
2080
21
        if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED &&
2081
            (s->avctx->pix_fmt == AV_PIX_FMT_RGB0 || s->avctx->pix_fmt == AV_PIX_FMT_RGBA)) {
2082
            int x = s->avctx->pix_fmt == AV_PIX_FMT_RGB0 ? 4 : 5;
2083
            uint8_t *src = five_planes ? five_planes : p->data[plane];
2084
            dst = p->data[plane];
2085
            for (i = 0; i < s->height; i++) {
2086
                for (j = 0; j < s->width; j++) {
2087
                    int k =  255 - src[x * j + 3];
2088
                    int r = (255 - src[x * j    ]) * k;
2089
                    int g = (255 - src[x * j + 1]) * k;
2090
                    int b = (255 - src[x * j + 2]) * k;
2091
                    dst[4 * j    ] = r * 257 >> 16;
2092
                    dst[4 * j + 1] = g * 257 >> 16;
2093
                    dst[4 * j + 2] = b * 257 >> 16;
2094
                    dst[4 * j + 3] = s->avctx->pix_fmt == AV_PIX_FMT_RGBA ? src[x * j + 4] : 255;
2095
                }
2096
                src += stride;
2097
                dst += p->linesize[plane];
2098
            }
2099
            av_freep(&five_planes);
2100
21
        } else if (s->photometric == TIFF_PHOTOMETRIC_SEPARATED &&
2101
            s->avctx->pix_fmt == AV_PIX_FMT_RGBA64BE) {
2102
            dst = p->data[plane];
2103
            for (i = 0; i < s->height; i++) {
2104
                for (j = 0; j < s->width; j++) {
2105
                    uint64_t k =  65535 - AV_RB16(dst + 8 * j + 6);
2106
                    uint64_t r = (65535 - AV_RB16(dst + 8 * j    )) * k;
2107
                    uint64_t g = (65535 - AV_RB16(dst + 8 * j + 2)) * k;
2108
                    uint64_t b = (65535 - AV_RB16(dst + 8 * j + 4)) * k;
2109
                    AV_WB16(dst + 8 * j    , r * 65537 >> 32);
2110
                    AV_WB16(dst + 8 * j + 2, g * 65537 >> 32);
2111
                    AV_WB16(dst + 8 * j + 4, b * 65537 >> 32);
2112
                    AV_WB16(dst + 8 * j + 6, 65535);
2113
                }
2114
                dst += p->linesize[plane];
2115
            }
2116
        }
2117
    }
2118
2119

21
    if (s->planar && s->bppcount > 2) {
2120
        FFSWAP(uint8_t*, p->data[0],     p->data[2]);
2121
        FFSWAP(int,      p->linesize[0], p->linesize[2]);
2122
        FFSWAP(uint8_t*, p->data[0],     p->data[1]);
2123
        FFSWAP(int,      p->linesize[0], p->linesize[1]);
2124
    }
2125
2126


21
    if (s->is_bayer && s->white_level && s->bpp == 16 && !is_dng) {
2127
        uint16_t *dst = (uint16_t *)p->data[0];
2128
        for (i = 0; i < s->height; i++) {
2129
            for (j = 0; j < s->width; j++)
2130
                dst[j] = FFMIN((dst[j] / (float)s->white_level) * 65535, 65535);
2131
            dst += stride / 2;
2132
        }
2133
    }
2134
2135
21
    *got_frame = 1;
2136
2137
21
    return avpkt->size;
2138
}
2139
2140
9
static av_cold int tiff_init(AVCodecContext *avctx)
2141
{
2142
9
    TiffContext *s = avctx->priv_data;
2143
    const AVCodec *codec;
2144
    int ret;
2145
2146
9
    s->width  = 0;
2147
9
    s->height = 0;
2148
9
    s->subsampling[0] =
2149
9
    s->subsampling[1] = 1;
2150
9
    s->avctx  = avctx;
2151
9
    ff_lzw_decode_open(&s->lzw);
2152
9
    if (!s->lzw)
2153
        return AVERROR(ENOMEM);
2154
9
    ff_ccitt_unpack_init();
2155
2156
    /* Allocate JPEG frame */
2157
9
    s->jpgframe = av_frame_alloc();
2158
9
    if (!s->jpgframe)
2159
        return AVERROR(ENOMEM);
2160
2161
    /* Prepare everything needed for JPEG decoding */
2162
9
    codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
2163
9
    if (!codec)
2164
        return AVERROR_BUG;
2165
9
    s->avctx_mjpeg = avcodec_alloc_context3(codec);
2166
9
    if (!s->avctx_mjpeg)
2167
        return AVERROR(ENOMEM);
2168
9
    s->avctx_mjpeg->flags = avctx->flags;
2169
9
    s->avctx_mjpeg->flags2 = avctx->flags2;
2170
9
    s->avctx_mjpeg->dct_algo = avctx->dct_algo;
2171
9
    s->avctx_mjpeg->idct_algo = avctx->idct_algo;
2172
9
    ret = ff_codec_open2_recursive(s->avctx_mjpeg, codec, NULL);
2173
9
    if (ret < 0) {
2174
        return ret;
2175
    }
2176
2177
9
    return 0;
2178
}
2179
2180
9
static av_cold int tiff_end(AVCodecContext *avctx)
2181
{
2182
9
    TiffContext *const s = avctx->priv_data;
2183
2184
9
    free_geotags(s);
2185
2186
9
    ff_lzw_decode_close(&s->lzw);
2187
9
    av_freep(&s->deinvert_buf);
2188
9
    s->deinvert_buf_size = 0;
2189
9
    av_freep(&s->yuv_line);
2190
9
    s->yuv_line_size = 0;
2191
9
    av_freep(&s->fax_buffer);
2192
9
    s->fax_buffer_size = 0;
2193
9
    av_frame_free(&s->jpgframe);
2194
9
    avcodec_free_context(&s->avctx_mjpeg);
2195
9
    return 0;
2196
}
2197
2198
#define OFFSET(x) offsetof(TiffContext, x)
2199
static const AVOption tiff_options[] = {
2200
    { "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 },
2201
    { "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 },
2202
    { "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 },
2203
    { NULL },
2204
};
2205
2206
static const AVClass tiff_decoder_class = {
2207
    .class_name = "TIFF decoder",
2208
    .item_name  = av_default_item_name,
2209
    .option     = tiff_options,
2210
    .version    = LIBAVUTIL_VERSION_INT,
2211
};
2212
2213
AVCodec ff_tiff_decoder = {
2214
    .name           = "tiff",
2215
    .long_name      = NULL_IF_CONFIG_SMALL("TIFF image"),
2216
    .type           = AVMEDIA_TYPE_VIDEO,
2217
    .id             = AV_CODEC_ID_TIFF,
2218
    .priv_data_size = sizeof(TiffContext),
2219
    .init           = tiff_init,
2220
    .close          = tiff_end,
2221
    .decode         = decode_frame,
2222
    .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
2223
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
2224
    .priv_class     = &tiff_decoder_class,
2225
};