LCOV - code coverage report
Current view: top level - libavcodec - tiff.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 300 916 32.8 %
Date: 2017-12-15 18:13:28 Functions: 11 23 47.8 %

          Line data    Source code
       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/intreadwrite.h"
      39             : #include "libavutil/imgutils.h"
      40             : #include "avcodec.h"
      41             : #include "bytestream.h"
      42             : #include "faxcompr.h"
      43             : #include "internal.h"
      44             : #include "lzw.h"
      45             : #include "mathops.h"
      46             : #include "tiff.h"
      47             : #include "tiff_data.h"
      48             : #include "thread.h"
      49             : 
      50             : typedef struct TiffContext {
      51             :     AVCodecContext *avctx;
      52             :     GetByteContext gb;
      53             : 
      54             :     int width, height;
      55             :     unsigned int bpp, bppcount;
      56             :     uint32_t palette[256];
      57             :     int palette_is_set;
      58             :     int le;
      59             :     enum TiffCompr compr;
      60             :     enum TiffPhotometric photometric;
      61             :     int planar;
      62             :     int subsampling[2];
      63             :     int fax_opts;
      64             :     int predictor;
      65             :     int fill_order;
      66             :     uint32_t res[4];
      67             : 
      68             :     int strips, rps, sstype;
      69             :     int sot;
      70             :     int stripsizesoff, stripsize, stripoff, strippos;
      71             :     LZWState *lzw;
      72             : 
      73             :     uint8_t *deinvert_buf;
      74             :     int deinvert_buf_size;
      75             :     uint8_t *yuv_line;
      76             :     unsigned int yuv_line_size;
      77             :     uint8_t *fax_buffer;
      78             :     unsigned int fax_buffer_size;
      79             : 
      80             :     int geotag_count;
      81             :     TiffGeoTag *geotags;
      82             : } TiffContext;
      83             : 
      84          30 : static void free_geotags(TiffContext *const s)
      85             : {
      86             :     int i;
      87          30 :     for (i = 0; i < s->geotag_count; i++) {
      88           0 :         if (s->geotags[i].val)
      89           0 :             av_freep(&s->geotags[i].val);
      90             :     }
      91          30 :     av_freep(&s->geotags);
      92          30 :     s->geotag_count = 0;
      93          30 : }
      94             : 
      95             : #define RET_GEOKEY(TYPE, array, element)\
      96             :     if (key >= TIFF_##TYPE##_KEY_ID_OFFSET &&\
      97             :         key - TIFF_##TYPE##_KEY_ID_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_name_type_map))\
      98             :         return ff_tiff_##array##_name_type_map[key - TIFF_##TYPE##_KEY_ID_OFFSET].element;
      99             : 
     100           0 : static const char *get_geokey_name(int key)
     101             : {
     102           0 :     RET_GEOKEY(VERT, vert, name);
     103           0 :     RET_GEOKEY(PROJ, proj, name);
     104           0 :     RET_GEOKEY(GEOG, geog, name);
     105           0 :     RET_GEOKEY(CONF, conf, name);
     106             : 
     107           0 :     return NULL;
     108             : }
     109             : 
     110           0 : static int get_geokey_type(int key)
     111             : {
     112           0 :     RET_GEOKEY(VERT, vert, type);
     113           0 :     RET_GEOKEY(PROJ, proj, type);
     114           0 :     RET_GEOKEY(GEOG, geog, type);
     115           0 :     RET_GEOKEY(CONF, conf, type);
     116             : 
     117           0 :     return AVERROR_INVALIDDATA;
     118             : }
     119             : 
     120           0 : static int cmp_id_key(const void *id, const void *k)
     121             : {
     122           0 :     return *(const int*)id - ((const TiffGeoTagKeyName*)k)->key;
     123             : }
     124             : 
     125           0 : static const char *search_keyval(const TiffGeoTagKeyName *keys, int n, int id)
     126             : {
     127           0 :     TiffGeoTagKeyName *r = bsearch(&id, keys, n, sizeof(keys[0]), cmp_id_key);
     128           0 :     if(r)
     129           0 :         return r->name;
     130             : 
     131           0 :     return NULL;
     132             : }
     133             : 
     134           0 : static char *get_geokey_val(int key, int val)
     135             : {
     136             :     char *ap;
     137             : 
     138           0 :     if (val == TIFF_GEO_KEY_UNDEFINED)
     139           0 :         return av_strdup("undefined");
     140           0 :     if (val == TIFF_GEO_KEY_USER_DEFINED)
     141           0 :         return av_strdup("User-Defined");
     142             : 
     143             : #define RET_GEOKEY_VAL(TYPE, array)\
     144             :     if (val >= TIFF_##TYPE##_OFFSET &&\
     145             :         val - TIFF_##TYPE##_OFFSET < FF_ARRAY_ELEMS(ff_tiff_##array##_codes))\
     146             :         return av_strdup(ff_tiff_##array##_codes[val - TIFF_##TYPE##_OFFSET]);
     147             : 
     148           0 :     switch (key) {
     149           0 :     case TIFF_GT_MODEL_TYPE_GEOKEY:
     150           0 :         RET_GEOKEY_VAL(GT_MODEL_TYPE, gt_model_type);
     151           0 :         break;
     152           0 :     case TIFF_GT_RASTER_TYPE_GEOKEY:
     153           0 :         RET_GEOKEY_VAL(GT_RASTER_TYPE, gt_raster_type);
     154           0 :         break;
     155           0 :     case TIFF_GEOG_LINEAR_UNITS_GEOKEY:
     156             :     case TIFF_PROJ_LINEAR_UNITS_GEOKEY:
     157             :     case TIFF_VERTICAL_UNITS_GEOKEY:
     158           0 :         RET_GEOKEY_VAL(LINEAR_UNIT, linear_unit);
     159           0 :         break;
     160           0 :     case TIFF_GEOG_ANGULAR_UNITS_GEOKEY:
     161             :     case TIFF_GEOG_AZIMUTH_UNITS_GEOKEY:
     162           0 :         RET_GEOKEY_VAL(ANGULAR_UNIT, angular_unit);
     163           0 :         break;
     164           0 :     case TIFF_GEOGRAPHIC_TYPE_GEOKEY:
     165           0 :         RET_GEOKEY_VAL(GCS_TYPE, gcs_type);
     166           0 :         RET_GEOKEY_VAL(GCSE_TYPE, gcse_type);
     167           0 :         break;
     168           0 :     case TIFF_GEOG_GEODETIC_DATUM_GEOKEY:
     169           0 :         RET_GEOKEY_VAL(GEODETIC_DATUM, geodetic_datum);
     170           0 :         RET_GEOKEY_VAL(GEODETIC_DATUM_E, geodetic_datum_e);
     171           0 :         break;
     172           0 :     case TIFF_GEOG_ELLIPSOID_GEOKEY:
     173           0 :         RET_GEOKEY_VAL(ELLIPSOID, ellipsoid);
     174           0 :         break;
     175           0 :     case TIFF_GEOG_PRIME_MERIDIAN_GEOKEY:
     176           0 :         RET_GEOKEY_VAL(PRIME_MERIDIAN, prime_meridian);
     177           0 :         break;
     178           0 :     case TIFF_PROJECTED_CS_TYPE_GEOKEY:
     179           0 :         ap = av_strdup(search_keyval(ff_tiff_proj_cs_type_codes, FF_ARRAY_ELEMS(ff_tiff_proj_cs_type_codes), val));
     180           0 :         if(ap) return ap;
     181           0 :         break;
     182           0 :     case TIFF_PROJECTION_GEOKEY:
     183           0 :         ap = av_strdup(search_keyval(ff_tiff_projection_codes, FF_ARRAY_ELEMS(ff_tiff_projection_codes), val));
     184           0 :         if(ap) return ap;
     185           0 :         break;
     186           0 :     case TIFF_PROJ_COORD_TRANS_GEOKEY:
     187           0 :         RET_GEOKEY_VAL(COORD_TRANS, coord_trans);
     188           0 :         break;
     189           0 :     case TIFF_VERTICAL_CS_TYPE_GEOKEY:
     190           0 :         RET_GEOKEY_VAL(VERT_CS, vert_cs);
     191           0 :         RET_GEOKEY_VAL(ORTHO_VERT_CS, ortho_vert_cs);
     192           0 :         break;
     193             : 
     194             :     }
     195             : 
     196           0 :     ap = av_malloc(14);
     197           0 :     if (ap)
     198           0 :         snprintf(ap, 14, "Unknown-%d", val);
     199           0 :     return ap;
     200             : }
     201             : 
     202           0 : static char *doubles2str(double *dp, int count, const char *sep)
     203             : {
     204             :     int i;
     205             :     char *ap, *ap0;
     206             :     uint64_t component_len;
     207           0 :     if (!sep) sep = ", ";
     208           0 :     component_len = 24LL + strlen(sep);
     209           0 :     if (count >= (INT_MAX - 1)/component_len)
     210           0 :         return NULL;
     211           0 :     ap = av_malloc(component_len * count + 1);
     212           0 :     if (!ap)
     213           0 :         return NULL;
     214           0 :     ap0   = ap;
     215           0 :     ap[0] = '\0';
     216           0 :     for (i = 0; i < count; i++) {
     217           0 :         unsigned l = snprintf(ap, component_len, "%.15g%s", dp[i], sep);
     218           0 :         if(l >= component_len) {
     219           0 :             av_free(ap0);
     220           0 :             return NULL;
     221             :         }
     222           0 :         ap += l;
     223             :     }
     224           0 :     ap0[strlen(ap0) - strlen(sep)] = '\0';
     225           0 :     return ap0;
     226             : }
     227             : 
     228          14 : static int add_metadata(int count, int type,
     229             :                         const char *name, const char *sep, TiffContext *s, AVFrame *frame)
     230             : {
     231          14 :     switch(type) {
     232           0 :     case TIFF_DOUBLE: return ff_tadd_doubles_metadata(count, name, sep, &s->gb, s->le, &frame->metadata);
     233           2 :     case TIFF_SHORT : return ff_tadd_shorts_metadata(count, name, sep, &s->gb, s->le, 0, &frame->metadata);
     234          12 :     case TIFF_STRING: return ff_tadd_string_metadata(count, name, &s->gb, s->le, &frame->metadata);
     235           0 :     default         : return AVERROR_INVALIDDATA;
     236             :     };
     237             : }
     238             : 
     239       42246 : static void av_always_inline horizontal_fill(unsigned int bpp, uint8_t* dst,
     240             :                                              int usePtr, const uint8_t *src,
     241             :                                              uint8_t c, int width, int offset)
     242             : {
     243       42246 :     switch (bpp) {
     244           0 :     case 1:
     245           0 :         while (--width >= 0) {
     246           0 :             dst[(width+offset)*8+7] = (usePtr ? src[width] : c)      & 0x1;
     247           0 :             dst[(width+offset)*8+6] = (usePtr ? src[width] : c) >> 1 & 0x1;
     248           0 :             dst[(width+offset)*8+5] = (usePtr ? src[width] : c) >> 2 & 0x1;
     249           0 :             dst[(width+offset)*8+4] = (usePtr ? src[width] : c) >> 3 & 0x1;
     250           0 :             dst[(width+offset)*8+3] = (usePtr ? src[width] : c) >> 4 & 0x1;
     251           0 :             dst[(width+offset)*8+2] = (usePtr ? src[width] : c) >> 5 & 0x1;
     252           0 :             dst[(width+offset)*8+1] = (usePtr ? src[width] : c) >> 6 & 0x1;
     253           0 :             dst[(width+offset)*8+0] = (usePtr ? src[width] : c) >> 7;
     254             :         }
     255           0 :         break;
     256           0 :     case 2:
     257           0 :         while (--width >= 0) {
     258           0 :             dst[(width+offset)*4+3] = (usePtr ? src[width] : c) & 0x3;
     259           0 :             dst[(width+offset)*4+2] = (usePtr ? src[width] : c) >> 2 & 0x3;
     260           0 :             dst[(width+offset)*4+1] = (usePtr ? src[width] : c) >> 4 & 0x3;
     261           0 :             dst[(width+offset)*4+0] = (usePtr ? src[width] : c) >> 6;
     262             :         }
     263           0 :         break;
     264           0 :     case 4:
     265           0 :         while (--width >= 0) {
     266           0 :             dst[(width+offset)*2+1] = (usePtr ? src[width] : c) & 0xF;
     267           0 :             dst[(width+offset)*2+0] = (usePtr ? src[width] : c) >> 4;
     268             :         }
     269           0 :         break;
     270       42246 :     default:
     271       42246 :         if (usePtr) {
     272       39747 :             memcpy(dst + offset, src, width);
     273             :         } else {
     274        2499 :             memset(dst + offset, c, width);
     275             :         }
     276             :     }
     277       42246 : }
     278             : 
     279           0 : static int deinvert_buffer(TiffContext *s, const uint8_t *src, int size)
     280             : {
     281             :     int i;
     282             : 
     283           0 :     av_fast_padded_malloc(&s->deinvert_buf, &s->deinvert_buf_size, size);
     284           0 :     if (!s->deinvert_buf)
     285           0 :         return AVERROR(ENOMEM);
     286           0 :     for (i = 0; i < size; i++)
     287           0 :         s->deinvert_buf[i] = ff_reverse[src[i]];
     288             : 
     289           0 :     return 0;
     290             : }
     291             : 
     292           0 : static void unpack_yuv(TiffContext *s, AVFrame *p,
     293             :                        const uint8_t *src, int lnum)
     294             : {
     295             :     int i, j, k;
     296           0 :     int w       = (s->width - 1) / s->subsampling[0] + 1;
     297           0 :     uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
     298           0 :     uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
     299           0 :     if (s->width % s->subsampling[0] || s->height % s->subsampling[1]) {
     300           0 :         for (i = 0; i < w; i++) {
     301           0 :             for (j = 0; j < s->subsampling[1]; j++)
     302           0 :                 for (k = 0; k < s->subsampling[0]; k++)
     303           0 :                     p->data[0][FFMIN(lnum + j, s->height-1) * p->linesize[0] +
     304           0 :                                FFMIN(i * s->subsampling[0] + k, s->width-1)] = *src++;
     305           0 :             *pu++ = *src++;
     306           0 :             *pv++ = *src++;
     307             :         }
     308             :     }else{
     309           0 :         for (i = 0; i < w; i++) {
     310           0 :             for (j = 0; j < s->subsampling[1]; j++)
     311           0 :                 for (k = 0; k < s->subsampling[0]; k++)
     312           0 :                     p->data[0][(lnum + j) * p->linesize[0] +
     313           0 :                                i * s->subsampling[0] + k] = *src++;
     314           0 :             *pu++ = *src++;
     315           0 :             *pv++ = *src++;
     316             :         }
     317             :     }
     318           0 : }
     319             : 
     320             : #if CONFIG_ZLIB
     321           0 : static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
     322             :                            int size)
     323             : {
     324           0 :     z_stream zstream = { 0 };
     325             :     int zret;
     326             : 
     327           0 :     zstream.next_in   = (uint8_t *)src;
     328           0 :     zstream.avail_in  = size;
     329           0 :     zstream.next_out  = dst;
     330           0 :     zstream.avail_out = *len;
     331           0 :     zret              = inflateInit(&zstream);
     332           0 :     if (zret != Z_OK) {
     333           0 :         av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
     334           0 :         return zret;
     335             :     }
     336           0 :     zret = inflate(&zstream, Z_SYNC_FLUSH);
     337           0 :     inflateEnd(&zstream);
     338           0 :     *len = zstream.total_out;
     339           0 :     return zret == Z_STREAM_END ? Z_OK : zret;
     340             : }
     341             : 
     342           0 : static int tiff_unpack_zlib(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
     343             :                             const uint8_t *src, int size, int width, int lines,
     344             :                             int strip_start, int is_yuv)
     345             : {
     346             :     uint8_t *zbuf;
     347             :     unsigned long outlen;
     348             :     int ret, line;
     349           0 :     outlen = width * lines;
     350           0 :     zbuf   = av_malloc(outlen);
     351           0 :     if (!zbuf)
     352           0 :         return AVERROR(ENOMEM);
     353           0 :     if (s->fill_order) {
     354           0 :         if ((ret = deinvert_buffer(s, src, size)) < 0) {
     355           0 :             av_free(zbuf);
     356           0 :             return ret;
     357             :         }
     358           0 :         src = s->deinvert_buf;
     359             :     }
     360           0 :     ret = tiff_uncompress(zbuf, &outlen, src, size);
     361           0 :     if (ret != Z_OK) {
     362           0 :         av_log(s->avctx, AV_LOG_ERROR,
     363             :                "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
     364           0 :                (unsigned long)width * lines, ret);
     365           0 :         av_free(zbuf);
     366           0 :         return AVERROR_UNKNOWN;
     367             :     }
     368           0 :     src = zbuf;
     369           0 :     for (line = 0; line < lines; line++) {
     370           0 :         if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
     371           0 :             horizontal_fill(s->bpp, dst, 1, src, 0, width, 0);
     372             :         } else {
     373           0 :             memcpy(dst, src, width);
     374             :         }
     375           0 :         if (is_yuv) {
     376           0 :             unpack_yuv(s, p, dst, strip_start + line);
     377           0 :             line += s->subsampling[1] - 1;
     378             :         }
     379           0 :         dst += stride;
     380           0 :         src += width;
     381             :     }
     382           0 :     av_free(zbuf);
     383           0 :     return 0;
     384             : }
     385             : #endif
     386             : 
     387             : #if CONFIG_LZMA
     388           0 : static int tiff_uncompress_lzma(uint8_t *dst, uint64_t *len, const uint8_t *src,
     389             :                                 int size)
     390             : {
     391           0 :     lzma_stream stream = LZMA_STREAM_INIT;
     392             :     lzma_ret ret;
     393             : 
     394           0 :     stream.next_in   = (uint8_t *)src;
     395           0 :     stream.avail_in  = size;
     396           0 :     stream.next_out  = dst;
     397           0 :     stream.avail_out = *len;
     398           0 :     ret              = lzma_stream_decoder(&stream, UINT64_MAX, 0);
     399           0 :     if (ret != LZMA_OK) {
     400           0 :         av_log(NULL, AV_LOG_ERROR, "LZMA init error: %d\n", ret);
     401           0 :         return ret;
     402             :     }
     403           0 :     ret = lzma_code(&stream, LZMA_RUN);
     404           0 :     lzma_end(&stream);
     405           0 :     *len = stream.total_out;
     406           0 :     return ret == LZMA_STREAM_END ? LZMA_OK : ret;
     407             : }
     408             : 
     409           0 : static int tiff_unpack_lzma(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
     410             :                             const uint8_t *src, int size, int width, int lines,
     411             :                             int strip_start, int is_yuv)
     412             : {
     413           0 :     uint64_t outlen = width * (uint64_t)lines;
     414             :     int ret, line;
     415           0 :     uint8_t *buf = av_malloc(outlen);
     416           0 :     if (!buf)
     417           0 :         return AVERROR(ENOMEM);
     418           0 :     if (s->fill_order) {
     419           0 :         if ((ret = deinvert_buffer(s, src, size)) < 0) {
     420           0 :             av_free(buf);
     421           0 :             return ret;
     422             :         }
     423           0 :         src = s->deinvert_buf;
     424             :     }
     425           0 :     ret = tiff_uncompress_lzma(buf, &outlen, src, size);
     426           0 :     if (ret != LZMA_OK) {
     427           0 :         av_log(s->avctx, AV_LOG_ERROR,
     428             :                "Uncompressing failed (%"PRIu64" of %"PRIu64") with error %d\n", outlen,
     429           0 :                (uint64_t)width * lines, ret);
     430           0 :         av_free(buf);
     431           0 :         return AVERROR_UNKNOWN;
     432             :     }
     433           0 :     src = buf;
     434           0 :     for (line = 0; line < lines; line++) {
     435           0 :         if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
     436           0 :             horizontal_fill(s->bpp, dst, 1, src, 0, width, 0);
     437             :         } else {
     438           0 :             memcpy(dst, src, width);
     439             :         }
     440           0 :         if (is_yuv) {
     441           0 :             unpack_yuv(s, p, dst, strip_start + line);
     442           0 :             line += s->subsampling[1] - 1;
     443             :         }
     444           0 :         dst += stride;
     445           0 :         src += width;
     446             :     }
     447           0 :     av_free(buf);
     448           0 :     return 0;
     449             : }
     450             : #endif
     451             : 
     452          36 : static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride,
     453             :                            const uint8_t *src, int size, int width, int lines)
     454             : {
     455          36 :     int i, ret = 0;
     456             :     int line;
     457             :     uint8_t *src2;
     458             : 
     459          36 :     av_fast_padded_malloc(&s->fax_buffer, &s->fax_buffer_size, size);
     460          36 :     src2 = s->fax_buffer;
     461             : 
     462          36 :     if (!src2) {
     463           0 :         av_log(s->avctx, AV_LOG_ERROR,
     464             :                "Error allocating temporary buffer\n");
     465           0 :         return AVERROR(ENOMEM);
     466             :     }
     467             : 
     468          36 :     if (!s->fill_order) {
     469           0 :         memcpy(src2, src, size);
     470             :     } else {
     471      502274 :         for (i = 0; i < size; i++)
     472      502238 :             src2[i] = ff_reverse[src[i]];
     473             :     }
     474          36 :     memset(src2 + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
     475          36 :     ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
     476             :                           s->compr, s->fax_opts);
     477          36 :     if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
     478           0 :         for (line = 0; line < lines; line++) {
     479           0 :             horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
     480           0 :             dst += stride;
     481             :         }
     482          36 :     return ret;
     483             : }
     484             : 
     485         684 : static int tiff_unpack_strip(TiffContext *s, AVFrame *p, uint8_t *dst, int stride,
     486             :                              const uint8_t *src, int size, int strip_start, int lines)
     487             : {
     488             :     PutByteContext pb;
     489             :     int c, line, pixels, code, ret;
     490         684 :     const uint8_t *ssrc = src;
     491         684 :     int width = ((s->width * s->bpp) + 7) >> 3;
     492         684 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(p->format);
     493        1404 :     int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
     494         684 :                  (desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
     495           0 :                  desc->nb_components >= 3;
     496             : 
     497         684 :     if (s->planar)
     498           0 :         width /= s->bppcount;
     499             : 
     500         684 :     if (size <= 0)
     501           0 :         return AVERROR_INVALIDDATA;
     502             : 
     503         684 :     if (is_yuv) {
     504           0 :         int bytes_per_row = (((s->width - 1) / s->subsampling[0] + 1) * s->bpp *
     505           0 :                             s->subsampling[0] * s->subsampling[1] + 7) >> 3;
     506           0 :         av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, bytes_per_row);
     507           0 :         if (s->yuv_line == NULL) {
     508           0 :             av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
     509           0 :             return AVERROR(ENOMEM);
     510             :         }
     511           0 :         dst = s->yuv_line;
     512           0 :         stride = 0;
     513             : 
     514           0 :         width = (s->width - 1) / s->subsampling[0] + 1;
     515           0 :         width = width * s->subsampling[0] * s->subsampling[1] + 2*width;
     516           0 :         av_assert0(width <= bytes_per_row);
     517           0 :         av_assert0(s->bpp == 24);
     518             :     }
     519             : 
     520         684 :     if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
     521             : #if CONFIG_ZLIB
     522           0 :         return tiff_unpack_zlib(s, p, dst, stride, src, size, width, lines,
     523             :                                 strip_start, is_yuv);
     524             : #else
     525             :         av_log(s->avctx, AV_LOG_ERROR,
     526             :                "zlib support not enabled, "
     527             :                "deflate compression not supported\n");
     528             :         return AVERROR(ENOSYS);
     529             : #endif
     530             :     }
     531         684 :     if (s->compr == TIFF_LZMA) {
     532             : #if CONFIG_LZMA
     533           0 :         return tiff_unpack_lzma(s, p, dst, stride, src, size, width, lines,
     534             :                                 strip_start, is_yuv);
     535             : #else
     536             :         av_log(s->avctx, AV_LOG_ERROR,
     537             :                "LZMA support not enabled\n");
     538             :         return AVERROR(ENOSYS);
     539             : #endif
     540             :     }
     541         684 :     if (s->compr == TIFF_LZW) {
     542           0 :         if (s->fill_order) {
     543           0 :             if ((ret = deinvert_buffer(s, src, size)) < 0)
     544           0 :                 return ret;
     545           0 :             ssrc = src = s->deinvert_buf;
     546             :         }
     547           0 :         if (size > 1 && !src[0] && (src[1]&1)) {
     548           0 :             av_log(s->avctx, AV_LOG_ERROR, "Old style LZW is unsupported\n");
     549             :         }
     550           0 :         if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
     551           0 :             av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
     552           0 :             return ret;
     553             :         }
     554           0 :         for (line = 0; line < lines; line++) {
     555           0 :             pixels = ff_lzw_decode(s->lzw, dst, width);
     556           0 :             if (pixels < width) {
     557           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
     558             :                        pixels, width);
     559           0 :                 return AVERROR_INVALIDDATA;
     560             :             }
     561           0 :             if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8)
     562           0 :                 horizontal_fill(s->bpp, dst, 1, dst, 0, width, 0);
     563           0 :             if (is_yuv) {
     564           0 :                 unpack_yuv(s, p, dst, strip_start + line);
     565           0 :                 line += s->subsampling[1] - 1;
     566             :             }
     567           0 :             dst += stride;
     568             :         }
     569           0 :         return 0;
     570             :     }
     571        1368 :     if (s->compr == TIFF_CCITT_RLE ||
     572        1332 :         s->compr == TIFF_G3        ||
     573         648 :         s->compr == TIFF_G4) {
     574          36 :         if (is_yuv)
     575           0 :             return AVERROR_INVALIDDATA;
     576             : 
     577          36 :         return tiff_unpack_fax(s, dst, stride, src, size, width, lines);
     578             :     }
     579             : 
     580         648 :     bytestream2_init(&s->gb, src, size);
     581         648 :     bytestream2_init_writer(&pb, dst, is_yuv ? s->yuv_line_size : (stride * lines));
     582             : 
     583        5192 :     for (line = 0; line < lines; line++) {
     584        4544 :         if (src - ssrc > size) {
     585           0 :             av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
     586           0 :             return AVERROR_INVALIDDATA;
     587             :         }
     588             : 
     589        4544 :         if (bytestream2_get_bytes_left(&s->gb) == 0 || bytestream2_get_eof(&pb))
     590             :             break;
     591        4544 :         bytestream2_seek_p(&pb, stride * line, SEEK_SET);
     592        4544 :         switch (s->compr) {
     593         224 :         case TIFF_RAW:
     594         224 :             if (ssrc + size - src < width)
     595           0 :                 return AVERROR_INVALIDDATA;
     596             : 
     597         224 :             if (!s->fill_order) {
     598         224 :                 horizontal_fill(s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8),
     599             :                                 dst, 1, src, 0, width, 0);
     600             :             } else {
     601             :                 int i;
     602           0 :                 for (i = 0; i < width; i++)
     603           0 :                     dst[i] = ff_reverse[src[i]];
     604             :             }
     605         224 :             src += width;
     606         224 :             break;
     607        4320 :         case TIFF_PACKBITS:
     608       50662 :             for (pixels = 0; pixels < width;) {
     609       42022 :                 if (ssrc + size - src < 2) {
     610           0 :                     av_log(s->avctx, AV_LOG_ERROR, "Read went out of bounds\n");
     611           0 :                     return AVERROR_INVALIDDATA;
     612             :                 }
     613       42022 :                 code = s->fill_order ? (int8_t) ff_reverse[*src++]: (int8_t) *src++;
     614       42022 :                 if (code >= 0) {
     615       39523 :                     code++;
     616       79046 :                     if (pixels + code > width ||
     617       39523 :                         ssrc + size - src < code) {
     618           0 :                         av_log(s->avctx, AV_LOG_ERROR,
     619             :                                "Copy went out of bounds\n");
     620           0 :                         return AVERROR_INVALIDDATA;
     621             :                     }
     622       39523 :                     horizontal_fill(s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8),
     623             :                                     dst, 1, src, 0, code, pixels);
     624       39523 :                     src    += code;
     625       39523 :                     pixels += code;
     626        2499 :                 } else if (code != -128) { // -127..-1
     627        2499 :                     code = (-code) + 1;
     628        2499 :                     if (pixels + code > width) {
     629           0 :                         av_log(s->avctx, AV_LOG_ERROR,
     630             :                                "Run went out of bounds\n");
     631           0 :                         return AVERROR_INVALIDDATA;
     632             :                     }
     633        2499 :                     c = *src++;
     634        2499 :                     horizontal_fill(s->bpp * (s->avctx->pix_fmt == AV_PIX_FMT_PAL8),
     635             :                                     dst, 0, NULL, c, code, pixels);
     636        2499 :                     pixels += code;
     637             :                 }
     638             :             }
     639        4320 :             if (s->fill_order) {
     640             :                 int i;
     641           0 :                 for (i = 0; i < width; i++)
     642           0 :                     dst[i] = ff_reverse[dst[i]];
     643             :             }
     644        4320 :             break;
     645             :         }
     646        4544 :         if (is_yuv) {
     647           0 :             unpack_yuv(s, p, dst, strip_start + line);
     648           0 :             line += s->subsampling[1] - 1;
     649             :         }
     650        4544 :         dst += stride;
     651             :     }
     652         648 :     return 0;
     653             : }
     654             : 
     655          21 : static int init_image(TiffContext *s, ThreadFrame *frame)
     656             : {
     657             :     int ret;
     658          21 :     int create_gray_palette = 0;
     659             : 
     660             :     // make sure there is no aliasing in the following switch
     661          21 :     if (s->bpp >= 100 || s->bppcount >= 10) {
     662           0 :         av_log(s->avctx, AV_LOG_ERROR,
     663             :                "Unsupported image parameters: bpp=%d, bppcount=%d\n",
     664             :                s->bpp, s->bppcount);
     665           0 :         return AVERROR_INVALIDDATA;
     666             :     }
     667             : 
     668          21 :     switch (s->planar * 1000 + s->bpp * 10 + s->bppcount) {
     669           4 :     case 11:
     670           4 :         if (!s->palette_is_set) {
     671           4 :             s->avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
     672           4 :             break;
     673             :         }
     674             :     case 21:
     675             :     case 41:
     676           0 :         s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
     677           0 :         if (!s->palette_is_set) {
     678           0 :             create_gray_palette = 1;
     679             :         }
     680           0 :         break;
     681           0 :     case 81:
     682           0 :         s->avctx->pix_fmt = s->palette_is_set ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
     683           0 :         break;
     684          17 :     case 243:
     685          17 :         if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
     686           0 :             if (s->subsampling[0] == 1 && s->subsampling[1] == 1) {
     687           0 :                 s->avctx->pix_fmt = AV_PIX_FMT_YUV444P;
     688           0 :             } else if (s->subsampling[0] == 2 && s->subsampling[1] == 1) {
     689           0 :                 s->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
     690           0 :             } else if (s->subsampling[0] == 4 && s->subsampling[1] == 1) {
     691           0 :                 s->avctx->pix_fmt = AV_PIX_FMT_YUV411P;
     692           0 :             } else if (s->subsampling[0] == 1 && s->subsampling[1] == 2) {
     693           0 :                 s->avctx->pix_fmt = AV_PIX_FMT_YUV440P;
     694           0 :             } else if (s->subsampling[0] == 2 && s->subsampling[1] == 2) {
     695           0 :                 s->avctx->pix_fmt = AV_PIX_FMT_YUV420P;
     696           0 :             } else if (s->subsampling[0] == 4 && s->subsampling[1] == 4) {
     697           0 :                 s->avctx->pix_fmt = AV_PIX_FMT_YUV410P;
     698             :             } else {
     699           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr subsampling\n");
     700           0 :                 return AVERROR_PATCHWELCOME;
     701             :             }
     702             :         } else
     703          17 :             s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
     704          17 :         break;
     705           0 :     case 161:
     706           0 :         s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GRAY16LE : AV_PIX_FMT_GRAY16BE;
     707           0 :         break;
     708           0 :     case 162:
     709           0 :         s->avctx->pix_fmt = AV_PIX_FMT_YA8;
     710           0 :         break;
     711           0 :     case 322:
     712           0 :         s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YA16LE : AV_PIX_FMT_YA16BE;
     713           0 :         break;
     714           0 :     case 324:
     715           0 :         s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
     716           0 :         break;
     717           0 :     case 483:
     718           0 :         s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE  : AV_PIX_FMT_RGB48BE;
     719           0 :         break;
     720           0 :     case 644:
     721           0 :         s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGBA64LE  : AV_PIX_FMT_RGBA64BE;
     722           0 :         break;
     723           0 :     case 1243:
     724           0 :         s->avctx->pix_fmt = AV_PIX_FMT_GBRP;
     725           0 :         break;
     726           0 :     case 1324:
     727           0 :         s->avctx->pix_fmt = AV_PIX_FMT_GBRAP;
     728           0 :         break;
     729           0 :     case 1483:
     730           0 :         s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRP16LE : AV_PIX_FMT_GBRP16BE;
     731           0 :         break;
     732           0 :     case 1644:
     733           0 :         s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GBRAP16LE : AV_PIX_FMT_GBRAP16BE;
     734           0 :         break;
     735           0 :     default:
     736           0 :         av_log(s->avctx, AV_LOG_ERROR,
     737             :                "This format is not supported (bpp=%d, bppcount=%d)\n",
     738             :                s->bpp, s->bppcount);
     739           0 :         return AVERROR_INVALIDDATA;
     740             :     }
     741             : 
     742          21 :     if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
     743           0 :         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
     744           0 :         if((desc->flags & AV_PIX_FMT_FLAG_RGB) ||
     745           0 :            !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) ||
     746           0 :            desc->nb_components < 3) {
     747           0 :             av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr variant\n");
     748           0 :             return AVERROR_INVALIDDATA;
     749             :         }
     750             :     }
     751             : 
     752          21 :     if (s->width != s->avctx->width || s->height != s->avctx->height) {
     753           5 :         ret = ff_set_dimensions(s->avctx, s->width, s->height);
     754           5 :         if (ret < 0)
     755           0 :             return ret;
     756             :     }
     757          21 :     if ((ret = ff_thread_get_buffer(s->avctx, frame, 0)) < 0)
     758           0 :         return ret;
     759          21 :     if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
     760           0 :         if (!create_gray_palette)
     761           0 :             memcpy(frame->f->data[1], s->palette, sizeof(s->palette));
     762             :         else {
     763             :             /* make default grayscale pal */
     764             :             int i;
     765           0 :             uint32_t *pal = (uint32_t *)frame->f->data[1];
     766           0 :             for (i = 0; i < 1<<s->bpp; i++)
     767           0 :                 pal[i] = 0xFFU << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101;
     768             :         }
     769             :     }
     770          21 :     return 0;
     771             : }
     772             : 
     773          42 : static void set_sar(TiffContext *s, unsigned tag, unsigned num, unsigned den)
     774             : {
     775          42 :     int offset = tag == TIFF_YRES ? 2 : 0;
     776          42 :     s->res[offset++] = num;
     777          42 :     s->res[offset]   = den;
     778          42 :     if (s->res[0] && s->res[1] && s->res[2] && s->res[3]) {
     779          33 :         uint64_t num = s->res[2] * (uint64_t)s->res[1];
     780          33 :         uint64_t den = s->res[0] * (uint64_t)s->res[3];
     781          33 :         if (num > INT64_MAX || den > INT64_MAX) {
     782           0 :             num = num >> 1;
     783           0 :             den = den >> 1;
     784             :         }
     785          33 :         av_reduce(&s->avctx->sample_aspect_ratio.num, &s->avctx->sample_aspect_ratio.den,
     786             :                   num, den, INT32_MAX);
     787          33 :         if (!s->avctx->sample_aspect_ratio.den)
     788           0 :             s->avctx->sample_aspect_ratio = (AVRational) {0, 1};
     789             :     }
     790          42 : }
     791             : 
     792         299 : static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
     793             : {
     794         299 :     unsigned tag, type, count, off, value = 0, value2 = 0;
     795             :     int i, start;
     796             :     int pos;
     797             :     int ret;
     798             :     double *dp;
     799             : 
     800         299 :     ret = ff_tread_tag(&s->gb, s->le, &tag, &type, &count, &start);
     801         299 :     if (ret < 0) {
     802           0 :         goto end;
     803             :     }
     804             : 
     805         299 :     off = bytestream2_tell(&s->gb);
     806         299 :     if (count == 1) {
     807         230 :         switch (type) {
     808         188 :         case TIFF_BYTE:
     809             :         case TIFF_SHORT:
     810             :         case TIFF_LONG:
     811         188 :             value = ff_tget(&s->gb, type, s->le);
     812         188 :             break;
     813          42 :         case TIFF_RATIONAL:
     814          42 :             value  = ff_tget(&s->gb, TIFF_LONG, s->le);
     815          42 :             value2 = ff_tget(&s->gb, TIFF_LONG, s->le);
     816          42 :             break;
     817           0 :         case TIFF_STRING:
     818           0 :             if (count <= 4) {
     819           0 :                 break;
     820             :             }
     821             :         default:
     822           0 :             value = UINT_MAX;
     823             :         }
     824          69 :     }
     825             : 
     826         299 :     switch (tag) {
     827          21 :     case TIFF_WIDTH:
     828          21 :         s->width = value;
     829          21 :         break;
     830          21 :     case TIFF_HEIGHT:
     831          21 :         s->height = value;
     832          21 :         break;
     833          21 :     case TIFF_BPP:
     834          21 :         if (count > 4U) {
     835           0 :             av_log(s->avctx, AV_LOG_ERROR,
     836             :                    "This format is not supported (bpp=%d, %d components)\n",
     837             :                    value, count);
     838           0 :             return AVERROR_INVALIDDATA;
     839             :         }
     840          21 :         s->bppcount = count;
     841          21 :         if (count == 1)
     842           4 :             s->bpp = value;
     843             :         else {
     844          17 :             switch (type) {
     845          17 :             case TIFF_BYTE:
     846             :             case TIFF_SHORT:
     847             :             case TIFF_LONG:
     848          17 :                 s->bpp = 0;
     849          17 :                 if (bytestream2_get_bytes_left(&s->gb) < type_sizes[type] * count)
     850           0 :                     return AVERROR_INVALIDDATA;
     851          68 :                 for (i = 0; i < count; i++)
     852          51 :                     s->bpp += ff_tget(&s->gb, type, s->le);
     853          17 :                 break;
     854           0 :             default:
     855           0 :                 s->bpp = -1;
     856             :             }
     857             :         }
     858          21 :         break;
     859          21 :     case TIFF_SAMPLES_PER_PIXEL:
     860          21 :         if (count != 1) {
     861           0 :             av_log(s->avctx, AV_LOG_ERROR,
     862             :                    "Samples per pixel requires a single value, many provided\n");
     863           0 :             return AVERROR_INVALIDDATA;
     864             :         }
     865          21 :         if (value > 4U) {
     866           0 :             av_log(s->avctx, AV_LOG_ERROR,
     867             :                    "Samples per pixel %d is too large\n", value);
     868           0 :             return AVERROR_INVALIDDATA;
     869             :         }
     870          21 :         if (s->bppcount == 1)
     871           4 :             s->bpp *= value;
     872          21 :         s->bppcount = value;
     873          21 :         break;
     874          21 :     case TIFF_COMPR:
     875          21 :         s->compr     = value;
     876          21 :         av_log(s->avctx, AV_LOG_DEBUG, "compression: %d\n", s->compr);
     877          21 :         s->predictor = 0;
     878          21 :         switch (s->compr) {
     879          17 :         case TIFF_RAW:
     880             :         case TIFF_PACKBITS:
     881             :         case TIFF_LZW:
     882             :         case TIFF_CCITT_RLE:
     883          17 :             break;
     884           4 :         case TIFF_G3:
     885             :         case TIFF_G4:
     886           4 :             s->fax_opts = 0;
     887           4 :             break;
     888           0 :         case TIFF_DEFLATE:
     889             :         case TIFF_ADOBE_DEFLATE:
     890             : #if CONFIG_ZLIB
     891           0 :             break;
     892             : #else
     893             :             av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
     894             :             return AVERROR(ENOSYS);
     895             : #endif
     896           0 :         case TIFF_JPEG:
     897             :         case TIFF_NEWJPEG:
     898           0 :             avpriv_report_missing_feature(s->avctx, "JPEG compression");
     899           0 :             return AVERROR_PATCHWELCOME;
     900           0 :         case TIFF_LZMA:
     901             : #if CONFIG_LZMA
     902           0 :             break;
     903             : #else
     904             :             av_log(s->avctx, AV_LOG_ERROR, "LZMA not compiled in\n");
     905             :             return AVERROR(ENOSYS);
     906             : #endif
     907           0 :         default:
     908           0 :             av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
     909           0 :                    s->compr);
     910           0 :             return AVERROR_INVALIDDATA;
     911             :         }
     912          21 :         break;
     913          21 :     case TIFF_ROWSPERSTRIP:
     914          21 :         if (!value || (type == TIFF_LONG && value == UINT_MAX))
     915           0 :             value = s->height;
     916          21 :         s->rps = FFMIN(value, s->height);
     917          21 :         break;
     918          21 :     case TIFF_STRIP_OFFS:
     919          21 :         if (count == 1) {
     920           2 :             if (value > INT_MAX) {
     921           0 :                 av_log(s->avctx, AV_LOG_ERROR,
     922             :                     "strippos %u too large\n", value);
     923           0 :                 return AVERROR_INVALIDDATA;
     924             :             }
     925           2 :             s->strippos = 0;
     926           2 :             s->stripoff = value;
     927             :         } else
     928          19 :             s->strippos = off;
     929          21 :         s->strips = count;
     930          21 :         if (s->strips == 1)
     931           2 :             s->rps = s->height;
     932          21 :         s->sot = type;
     933          21 :         break;
     934          21 :     case TIFF_STRIP_SIZE:
     935          21 :         if (count == 1) {
     936           2 :             if (value > INT_MAX) {
     937           0 :                 av_log(s->avctx, AV_LOG_ERROR,
     938             :                     "stripsize %u too large\n", value);
     939           0 :                 return AVERROR_INVALIDDATA;
     940             :             }
     941           2 :             s->stripsizesoff = 0;
     942           2 :             s->stripsize     = value;
     943           2 :             s->strips        = 1;
     944             :         } else {
     945          19 :             s->stripsizesoff = off;
     946             :         }
     947          21 :         s->strips = count;
     948          21 :         s->sstype = type;
     949          21 :         break;
     950          42 :     case TIFF_XRES:
     951             :     case TIFF_YRES:
     952          42 :         set_sar(s, tag, value, value2);
     953          42 :         break;
     954           0 :     case TIFF_TILE_BYTE_COUNTS:
     955             :     case TIFF_TILE_LENGTH:
     956             :     case TIFF_TILE_OFFSETS:
     957             :     case TIFF_TILE_WIDTH:
     958           0 :         av_log(s->avctx, AV_LOG_ERROR, "Tiled images are not supported\n");
     959           0 :         return AVERROR_PATCHWELCOME;
     960             :         break;
     961           0 :     case TIFF_PREDICTOR:
     962           0 :         s->predictor = value;
     963           0 :         break;
     964          21 :     case TIFF_PHOTOMETRIC:
     965          21 :         switch (value) {
     966          21 :         case TIFF_PHOTOMETRIC_WHITE_IS_ZERO:
     967             :         case TIFF_PHOTOMETRIC_BLACK_IS_ZERO:
     968             :         case TIFF_PHOTOMETRIC_RGB:
     969             :         case TIFF_PHOTOMETRIC_PALETTE:
     970             :         case TIFF_PHOTOMETRIC_YCBCR:
     971          21 :             s->photometric = value;
     972          21 :             break;
     973           0 :         case TIFF_PHOTOMETRIC_ALPHA_MASK:
     974             :         case TIFF_PHOTOMETRIC_SEPARATED:
     975             :         case TIFF_PHOTOMETRIC_CIE_LAB:
     976             :         case TIFF_PHOTOMETRIC_ICC_LAB:
     977             :         case TIFF_PHOTOMETRIC_ITU_LAB:
     978             :         case TIFF_PHOTOMETRIC_CFA:
     979             :         case TIFF_PHOTOMETRIC_LOG_L:
     980             :         case TIFF_PHOTOMETRIC_LOG_LUV:
     981             :         case TIFF_PHOTOMETRIC_LINEAR_RAW:
     982           0 :             avpriv_report_missing_feature(s->avctx,
     983             :                                           "PhotometricInterpretation 0x%04X",
     984             :                                           value);
     985           0 :             return AVERROR_PATCHWELCOME;
     986           0 :         default:
     987           0 :             av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
     988             :                    "unknown\n", value);
     989           0 :             return AVERROR_INVALIDDATA;
     990             :         }
     991          21 :         break;
     992           6 :     case TIFF_FILL_ORDER:
     993           6 :         if (value < 1 || value > 2) {
     994           0 :             av_log(s->avctx, AV_LOG_ERROR,
     995             :                    "Unknown FillOrder value %d, trying default one\n", value);
     996           0 :             value = 1;
     997             :         }
     998           6 :         s->fill_order = value - 1;
     999           6 :         break;
    1000           0 :     case TIFF_PAL: {
    1001             :         GetByteContext pal_gb[3];
    1002           0 :         off = type_sizes[type];
    1003           0 :         if (count / 3 > 256 ||
    1004           0 :             bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
    1005           0 :             return AVERROR_INVALIDDATA;
    1006             : 
    1007           0 :         pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
    1008           0 :         bytestream2_skip(&pal_gb[1], count / 3 * off);
    1009           0 :         bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
    1010             : 
    1011           0 :         off = (type_sizes[type] - 1) << 3;
    1012           0 :         if (off > 31U) {
    1013           0 :             av_log(s->avctx, AV_LOG_ERROR, "palette shift %d is out of range\n", off);
    1014           0 :             return AVERROR_INVALIDDATA;
    1015             :         }
    1016             : 
    1017           0 :         for (i = 0; i < count / 3; i++) {
    1018           0 :             uint32_t p = 0xFF000000;
    1019           0 :             p |= (ff_tget(&pal_gb[0], type, s->le) >> off) << 16;
    1020           0 :             p |= (ff_tget(&pal_gb[1], type, s->le) >> off) << 8;
    1021           0 :             p |=  ff_tget(&pal_gb[2], type, s->le) >> off;
    1022           0 :             s->palette[i] = p;
    1023             :         }
    1024           0 :         s->palette_is_set = 1;
    1025           0 :         break;
    1026             :     }
    1027           6 :     case TIFF_PLANAR:
    1028           6 :         s->planar = value == 2;
    1029           6 :         break;
    1030           0 :     case TIFF_YCBCR_SUBSAMPLING:
    1031           0 :         if (count != 2) {
    1032           0 :             av_log(s->avctx, AV_LOG_ERROR, "subsample count invalid\n");
    1033           0 :             return AVERROR_INVALIDDATA;
    1034             :         }
    1035           0 :         for (i = 0; i < count; i++) {
    1036           0 :             s->subsampling[i] = ff_tget(&s->gb, type, s->le);
    1037           0 :             if (s->subsampling[i] <= 0) {
    1038           0 :                 av_log(s->avctx, AV_LOG_ERROR, "subsampling %d is invalid\n", s->subsampling[i]);
    1039           0 :                 s->subsampling[i] = 1;
    1040           0 :                 return AVERROR_INVALIDDATA;
    1041             :             }
    1042             :         }
    1043           0 :         break;
    1044           0 :     case TIFF_T4OPTIONS:
    1045           0 :         if (s->compr == TIFF_G3)
    1046           0 :             s->fax_opts = value;
    1047           0 :         break;
    1048           0 :     case TIFF_T6OPTIONS:
    1049           0 :         if (s->compr == TIFF_G4)
    1050           0 :             s->fax_opts = value;
    1051           0 :         break;
    1052             : #define ADD_METADATA(count, name, sep)\
    1053             :     if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\
    1054             :         av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
    1055             :         goto end;\
    1056             :     }
    1057           0 :     case TIFF_MODEL_PIXEL_SCALE:
    1058           0 :         ADD_METADATA(count, "ModelPixelScaleTag", NULL);
    1059           0 :         break;
    1060           0 :     case TIFF_MODEL_TRANSFORMATION:
    1061           0 :         ADD_METADATA(count, "ModelTransformationTag", NULL);
    1062           0 :         break;
    1063           0 :     case TIFF_MODEL_TIEPOINT:
    1064           0 :         ADD_METADATA(count, "ModelTiepointTag", NULL);
    1065           0 :         break;
    1066           0 :     case TIFF_GEO_KEY_DIRECTORY:
    1067           0 :         if (s->geotag_count) {
    1068           0 :             avpriv_request_sample(s->avctx, "Multiple geo key directories\n");
    1069           0 :             return AVERROR_INVALIDDATA;
    1070             :         }
    1071           0 :         ADD_METADATA(1, "GeoTIFF_Version", NULL);
    1072           0 :         ADD_METADATA(2, "GeoTIFF_Key_Revision", ".");
    1073           0 :         s->geotag_count   = ff_tget_short(&s->gb, s->le);
    1074           0 :         if (s->geotag_count > count / 4 - 1) {
    1075           0 :             s->geotag_count = count / 4 - 1;
    1076           0 :             av_log(s->avctx, AV_LOG_WARNING, "GeoTIFF key directory buffer shorter than specified\n");
    1077             :         }
    1078           0 :         if (   bytestream2_get_bytes_left(&s->gb) < s->geotag_count * sizeof(int16_t) * 4
    1079           0 :             || s->geotag_count == 0) {
    1080           0 :             s->geotag_count = 0;
    1081           0 :             return -1;
    1082             :         }
    1083           0 :         s->geotags = av_mallocz_array(s->geotag_count, sizeof(TiffGeoTag));
    1084           0 :         if (!s->geotags) {
    1085           0 :             av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
    1086           0 :             s->geotag_count = 0;
    1087           0 :             goto end;
    1088             :         }
    1089           0 :         for (i = 0; i < s->geotag_count; i++) {
    1090           0 :             s->geotags[i].key    = ff_tget_short(&s->gb, s->le);
    1091           0 :             s->geotags[i].type   = ff_tget_short(&s->gb, s->le);
    1092           0 :             s->geotags[i].count  = ff_tget_short(&s->gb, s->le);
    1093             : 
    1094           0 :             if (!s->geotags[i].type)
    1095           0 :                 s->geotags[i].val  = get_geokey_val(s->geotags[i].key, ff_tget_short(&s->gb, s->le));
    1096             :             else
    1097           0 :                 s->geotags[i].offset = ff_tget_short(&s->gb, s->le);
    1098             :         }
    1099           0 :         break;
    1100           0 :     case TIFF_GEO_DOUBLE_PARAMS:
    1101           0 :         if (count >= INT_MAX / sizeof(int64_t))
    1102           0 :             return AVERROR_INVALIDDATA;
    1103           0 :         if (bytestream2_get_bytes_left(&s->gb) < count * sizeof(int64_t))
    1104           0 :             return AVERROR_INVALIDDATA;
    1105           0 :         dp = av_malloc_array(count, sizeof(double));
    1106           0 :         if (!dp) {
    1107           0 :             av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
    1108           0 :             goto end;
    1109             :         }
    1110           0 :         for (i = 0; i < count; i++)
    1111           0 :             dp[i] = ff_tget_double(&s->gb, s->le);
    1112           0 :         for (i = 0; i < s->geotag_count; i++) {
    1113           0 :             if (s->geotags[i].type == TIFF_GEO_DOUBLE_PARAMS) {
    1114           0 :                 if (s->geotags[i].count == 0
    1115           0 :                     || s->geotags[i].offset + s->geotags[i].count > count) {
    1116           0 :                     av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
    1117           0 :                 } else if (s->geotags[i].val) {
    1118           0 :                     av_log(s->avctx, AV_LOG_WARNING, "Duplicate GeoTIFF key %d\n", s->geotags[i].key);
    1119             :                 } else {
    1120           0 :                     char *ap = doubles2str(&dp[s->geotags[i].offset], s->geotags[i].count, ", ");
    1121           0 :                     if (!ap) {
    1122           0 :                         av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
    1123           0 :                         av_freep(&dp);
    1124           0 :                         return AVERROR(ENOMEM);
    1125             :                     }
    1126           0 :                     s->geotags[i].val = ap;
    1127             :                 }
    1128             :             }
    1129             :         }
    1130           0 :         av_freep(&dp);
    1131           0 :         break;
    1132           0 :     case TIFF_GEO_ASCII_PARAMS:
    1133           0 :         pos = bytestream2_tell(&s->gb);
    1134           0 :         for (i = 0; i < s->geotag_count; i++) {
    1135           0 :             if (s->geotags[i].type == TIFF_GEO_ASCII_PARAMS) {
    1136           0 :                 if (s->geotags[i].count == 0
    1137           0 :                     || s->geotags[i].offset +  s->geotags[i].count > count) {
    1138           0 :                     av_log(s->avctx, AV_LOG_WARNING, "Invalid GeoTIFF key %d\n", s->geotags[i].key);
    1139             :                 } else {
    1140             :                     char *ap;
    1141             : 
    1142           0 :                     bytestream2_seek(&s->gb, pos + s->geotags[i].offset, SEEK_SET);
    1143           0 :                     if (bytestream2_get_bytes_left(&s->gb) < s->geotags[i].count)
    1144           0 :                         return AVERROR_INVALIDDATA;
    1145           0 :                     if (s->geotags[i].val)
    1146           0 :                         return AVERROR_INVALIDDATA;
    1147           0 :                     ap = av_malloc(s->geotags[i].count);
    1148           0 :                     if (!ap) {
    1149           0 :                         av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
    1150           0 :                         return AVERROR(ENOMEM);
    1151             :                     }
    1152           0 :                     bytestream2_get_bufferu(&s->gb, ap, s->geotags[i].count);
    1153           0 :                     ap[s->geotags[i].count - 1] = '\0'; //replace the "|" delimiter with a 0 byte
    1154           0 :                     s->geotags[i].val = ap;
    1155             :                 }
    1156             :             }
    1157             :         }
    1158           0 :         break;
    1159           0 :     case TIFF_ARTIST:
    1160           0 :         ADD_METADATA(count, "artist", NULL);
    1161           0 :         break;
    1162           0 :     case TIFF_COPYRIGHT:
    1163           0 :         ADD_METADATA(count, "copyright", NULL);
    1164           0 :         break;
    1165           0 :     case TIFF_DATE:
    1166           0 :         ADD_METADATA(count, "date", NULL);
    1167           0 :         break;
    1168           6 :     case TIFF_DOCUMENT_NAME:
    1169           6 :         ADD_METADATA(count, "document_name", NULL);
    1170           6 :         break;
    1171           0 :     case TIFF_HOST_COMPUTER:
    1172           0 :         ADD_METADATA(count, "computer", NULL);
    1173           0 :         break;
    1174           4 :     case TIFF_IMAGE_DESCRIPTION:
    1175           4 :         ADD_METADATA(count, "description", NULL);
    1176           4 :         break;
    1177           0 :     case TIFF_MAKE:
    1178           0 :         ADD_METADATA(count, "make", NULL);
    1179           0 :         break;
    1180           0 :     case TIFF_MODEL:
    1181           0 :         ADD_METADATA(count, "model", NULL);
    1182           0 :         break;
    1183           0 :     case TIFF_PAGE_NAME:
    1184           0 :         ADD_METADATA(count, "page_name", NULL);
    1185           0 :         break;
    1186           2 :     case TIFF_PAGE_NUMBER:
    1187           2 :         ADD_METADATA(count, "page_number", " / ");
    1188           2 :         break;
    1189           2 :     case TIFF_SOFTWARE_NAME:
    1190           2 :         ADD_METADATA(count, "software", NULL);
    1191           2 :         break;
    1192          42 :     default:
    1193          42 :         if (s->avctx->err_recognition & AV_EF_EXPLODE) {
    1194           0 :             av_log(s->avctx, AV_LOG_ERROR,
    1195             :                    "Unknown or unsupported tag %d/0X%0X\n",
    1196             :                    tag, tag);
    1197           0 :             return AVERROR_INVALIDDATA;
    1198             :         }
    1199             :     }
    1200         341 : end:
    1201         299 :     if (s->bpp > 64U) {
    1202           0 :         av_log(s->avctx, AV_LOG_ERROR,
    1203             :                 "This format is not supported (bpp=%d, %d components)\n",
    1204             :                 s->bpp, count);
    1205           0 :         s->bpp = 0;
    1206           0 :         return AVERROR_INVALIDDATA;
    1207             :     }
    1208         299 :     bytestream2_seek(&s->gb, start, SEEK_SET);
    1209         299 :     return 0;
    1210             : }
    1211             : 
    1212          21 : static int decode_frame(AVCodecContext *avctx,
    1213             :                         void *data, int *got_frame, AVPacket *avpkt)
    1214             : {
    1215          21 :     TiffContext *const s = avctx->priv_data;
    1216          21 :     AVFrame *const p = data;
    1217          21 :     ThreadFrame frame = { .f = data };
    1218             :     unsigned off;
    1219             :     int le, ret, plane, planes;
    1220             :     int i, j, entries, stride;
    1221             :     unsigned soff, ssize;
    1222             :     uint8_t *dst;
    1223             :     GetByteContext stripsizes;
    1224             :     GetByteContext stripdata;
    1225             : 
    1226          21 :     bytestream2_init(&s->gb, avpkt->data, avpkt->size);
    1227             : 
    1228             :     // parse image header
    1229          21 :     if ((ret = ff_tdecode_header(&s->gb, &le, &off))) {
    1230           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid TIFF header\n");
    1231           0 :         return ret;
    1232          21 :     } else if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
    1233           0 :         av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
    1234           0 :         return AVERROR_INVALIDDATA;
    1235             :     }
    1236          21 :     s->le          = le;
    1237             :     // TIFF_BPP is not a required tag and defaults to 1
    1238          21 :     s->bppcount    = s->bpp = 1;
    1239          21 :     s->photometric = TIFF_PHOTOMETRIC_NONE;
    1240          21 :     s->compr       = TIFF_RAW;
    1241          21 :     s->fill_order  = 0;
    1242          21 :     free_geotags(s);
    1243             : 
    1244             :     // Reset these offsets so we can tell if they were set this frame
    1245          21 :     s->stripsizesoff = s->strippos = 0;
    1246             :     /* parse image file directory */
    1247          21 :     bytestream2_seek(&s->gb, off, SEEK_SET);
    1248          21 :     entries = ff_tget_short(&s->gb, le);
    1249          21 :     if (bytestream2_get_bytes_left(&s->gb) < entries * 12)
    1250           0 :         return AVERROR_INVALIDDATA;
    1251         320 :     for (i = 0; i < entries; i++) {
    1252         299 :         if ((ret = tiff_decode_tag(s, p)) < 0)
    1253           0 :             return ret;
    1254             :     }
    1255             : 
    1256          21 :     for (i = 0; i<s->geotag_count; i++) {
    1257           0 :         const char *keyname = get_geokey_name(s->geotags[i].key);
    1258           0 :         if (!keyname) {
    1259           0 :             av_log(avctx, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[i].key);
    1260           0 :             continue;
    1261             :         }
    1262           0 :         if (get_geokey_type(s->geotags[i].key) != s->geotags[i].type) {
    1263           0 :             av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key);
    1264           0 :             continue;
    1265             :         }
    1266           0 :         ret = av_dict_set(&p->metadata, keyname, s->geotags[i].val, 0);
    1267           0 :         if (ret<0) {
    1268           0 :             av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);
    1269           0 :             return ret;
    1270             :         }
    1271             :     }
    1272             : 
    1273          21 :     if (!s->strippos && !s->stripoff) {
    1274           0 :         av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
    1275           0 :         return AVERROR_INVALIDDATA;
    1276             :     }
    1277             :     /* now we have the data and may start decoding */
    1278          21 :     if ((ret = init_image(s, &frame)) < 0)
    1279           0 :         return ret;
    1280             : 
    1281          21 :     if (s->strips == 1 && !s->stripsize) {
    1282           0 :         av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
    1283           0 :         s->stripsize = avpkt->size - s->stripoff;
    1284             :     }
    1285             : 
    1286          21 :     if (s->stripsizesoff) {
    1287          19 :         if (s->stripsizesoff >= (unsigned)avpkt->size)
    1288           0 :             return AVERROR_INVALIDDATA;
    1289          19 :         bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
    1290          19 :                          avpkt->size - s->stripsizesoff);
    1291             :     }
    1292          21 :     if (s->strippos) {
    1293          19 :         if (s->strippos >= (unsigned)avpkt->size)
    1294           0 :             return AVERROR_INVALIDDATA;
    1295          19 :         bytestream2_init(&stripdata, avpkt->data + s->strippos,
    1296          19 :                          avpkt->size - s->strippos);
    1297             :     }
    1298             : 
    1299          21 :     if (s->rps <= 0 || s->rps % s->subsampling[1]) {
    1300           0 :         av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);
    1301           0 :         return AVERROR_INVALIDDATA;
    1302             :     }
    1303             : 
    1304          21 :     planes = s->planar ? s->bppcount : 1;
    1305          42 :     for (plane = 0; plane < planes; plane++) {
    1306          21 :         stride = p->linesize[plane];
    1307          21 :         dst = p->data[plane];
    1308         705 :         for (i = 0; i < s->height; i += s->rps) {
    1309         684 :             if (i)
    1310         663 :                 dst += s->rps * stride;
    1311         684 :             if (s->stripsizesoff)
    1312         682 :                 ssize = ff_tget(&stripsizes, s->sstype, le);
    1313             :             else
    1314           2 :                 ssize = s->stripsize;
    1315             : 
    1316         684 :             if (s->strippos)
    1317         682 :                 soff = ff_tget(&stripdata, s->sot, le);
    1318             :             else
    1319           2 :                 soff = s->stripoff;
    1320             : 
    1321         684 :             if (soff > avpkt->size || ssize > avpkt->size - soff) {
    1322           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
    1323           0 :                 return AVERROR_INVALIDDATA;
    1324             :             }
    1325         684 :             if ((ret = tiff_unpack_strip(s, p, dst, stride, avpkt->data + soff, ssize, i,
    1326         684 :                                          FFMIN(s->rps, s->height - i))) < 0) {
    1327           0 :                 if (avctx->err_recognition & AV_EF_EXPLODE)
    1328           0 :                     return ret;
    1329           0 :                 break;
    1330             :             }
    1331             :         }
    1332          21 :         if (s->predictor == 2) {
    1333           0 :             if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {
    1334           0 :                 av_log(s->avctx, AV_LOG_ERROR, "predictor == 2 with YUV is unsupported");
    1335           0 :                 return AVERROR_PATCHWELCOME;
    1336             :             }
    1337           0 :             dst   = p->data[plane];
    1338           0 :             soff  = s->bpp >> 3;
    1339           0 :             if (s->planar)
    1340           0 :                 soff  = FFMAX(soff / s->bppcount, 1);
    1341           0 :             ssize = s->width * soff;
    1342           0 :             if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE ||
    1343           0 :                 s->avctx->pix_fmt == AV_PIX_FMT_RGBA64LE ||
    1344           0 :                 s->avctx->pix_fmt == AV_PIX_FMT_GRAY16LE ||
    1345           0 :                 s->avctx->pix_fmt == AV_PIX_FMT_YA16LE ||
    1346           0 :                 s->avctx->pix_fmt == AV_PIX_FMT_GBRP16LE ||
    1347           0 :                 s->avctx->pix_fmt == AV_PIX_FMT_GBRAP16LE) {
    1348           0 :                 for (i = 0; i < s->height; i++) {
    1349           0 :                     for (j = soff; j < ssize; j += 2)
    1350           0 :                         AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
    1351           0 :                     dst += stride;
    1352             :                 }
    1353           0 :             } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE ||
    1354           0 :                        s->avctx->pix_fmt == AV_PIX_FMT_RGBA64BE ||
    1355           0 :                        s->avctx->pix_fmt == AV_PIX_FMT_GRAY16BE ||
    1356           0 :                        s->avctx->pix_fmt == AV_PIX_FMT_YA16BE ||
    1357           0 :                        s->avctx->pix_fmt == AV_PIX_FMT_GBRP16BE ||
    1358           0 :                        s->avctx->pix_fmt == AV_PIX_FMT_GBRAP16BE) {
    1359           0 :                 for (i = 0; i < s->height; i++) {
    1360           0 :                     for (j = soff; j < ssize; j += 2)
    1361           0 :                         AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
    1362           0 :                     dst += stride;
    1363             :                 }
    1364             :             } else {
    1365           0 :                 for (i = 0; i < s->height; i++) {
    1366           0 :                     for (j = soff; j < ssize; j++)
    1367           0 :                         dst[j] += dst[j - soff];
    1368           0 :                     dst += stride;
    1369             :                 }
    1370             :             }
    1371             :         }
    1372             : 
    1373          21 :         if (s->photometric == TIFF_PHOTOMETRIC_WHITE_IS_ZERO) {
    1374           4 :             int c = (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255);
    1375           4 :             dst = p->data[plane];
    1376       12996 :             for (i = 0; i < s->height; i++) {
    1377     4170432 :                 for (j = 0; j < stride; j++)
    1378     4157440 :                     dst[j] = c - dst[j];
    1379       12992 :                 dst += stride;
    1380             :             }
    1381             :         }
    1382             :     }
    1383             : 
    1384          21 :     if (s->planar && s->bppcount > 2) {
    1385           0 :         FFSWAP(uint8_t*, p->data[0],     p->data[2]);
    1386           0 :         FFSWAP(int,      p->linesize[0], p->linesize[2]);
    1387           0 :         FFSWAP(uint8_t*, p->data[0],     p->data[1]);
    1388           0 :         FFSWAP(int,      p->linesize[0], p->linesize[1]);
    1389             :     }
    1390             : 
    1391          21 :     *got_frame = 1;
    1392             : 
    1393          21 :     return avpkt->size;
    1394             : }
    1395             : 
    1396           9 : static av_cold int tiff_init(AVCodecContext *avctx)
    1397             : {
    1398           9 :     TiffContext *s = avctx->priv_data;
    1399             : 
    1400           9 :     s->width  = 0;
    1401           9 :     s->height = 0;
    1402           9 :     s->subsampling[0] =
    1403           9 :     s->subsampling[1] = 1;
    1404           9 :     s->avctx  = avctx;
    1405           9 :     ff_lzw_decode_open(&s->lzw);
    1406           9 :     ff_ccitt_unpack_init();
    1407             : 
    1408           9 :     return 0;
    1409             : }
    1410             : 
    1411           9 : static av_cold int tiff_end(AVCodecContext *avctx)
    1412             : {
    1413           9 :     TiffContext *const s = avctx->priv_data;
    1414             : 
    1415           9 :     free_geotags(s);
    1416             : 
    1417           9 :     ff_lzw_decode_close(&s->lzw);
    1418           9 :     av_freep(&s->deinvert_buf);
    1419           9 :     s->deinvert_buf_size = 0;
    1420           9 :     av_freep(&s->fax_buffer);
    1421           9 :     s->fax_buffer_size = 0;
    1422           9 :     return 0;
    1423             : }
    1424             : 
    1425             : AVCodec ff_tiff_decoder = {
    1426             :     .name           = "tiff",
    1427             :     .long_name      = NULL_IF_CONFIG_SMALL("TIFF image"),
    1428             :     .type           = AVMEDIA_TYPE_VIDEO,
    1429             :     .id             = AV_CODEC_ID_TIFF,
    1430             :     .priv_data_size = sizeof(TiffContext),
    1431             :     .init           = tiff_init,
    1432             :     .close          = tiff_end,
    1433             :     .decode         = decode_frame,
    1434             :     .init_thread_copy = ONLY_IF_THREADS_ENABLED(tiff_init),
    1435             :     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
    1436             : };

Generated by: LCOV version 1.13