Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * TIFF image encoder
  3.  * Copyright (c) 2007 Bartlomiej Wolowiec
  4.  *
  5.  * This file is part of FFmpeg.
  6.  *
  7.  * FFmpeg is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * FFmpeg is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with FFmpeg; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20.  */
  21.  
  22. /**
  23.  * @file
  24.  * TIFF image encoder
  25.  * @author Bartlomiej Wolowiec
  26.  */
  27.  
  28. #include "config.h"
  29. #if CONFIG_ZLIB
  30. #include <zlib.h>
  31. #endif
  32.  
  33. #include "libavutil/imgutils.h"
  34. #include "libavutil/log.h"
  35. #include "libavutil/opt.h"
  36. #include "libavutil/pixdesc.h"
  37. #include "avcodec.h"
  38. #include "bytestream.h"
  39. #include "internal.h"
  40. #include "lzw.h"
  41. #include "put_bits.h"
  42. #include "rle.h"
  43. #include "tiff.h"
  44.  
  45. #define TIFF_MAX_ENTRY 32
  46.  
  47. /** sizes of various TIFF field types (string size = 1)*/
  48. static const uint8_t type_sizes2[14] = {
  49.     0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8, 4
  50. };
  51.  
  52. typedef struct TiffEncoderContext {
  53.     AVClass *class;                         ///< for private options
  54.     AVCodecContext *avctx;
  55.  
  56.     int width;                              ///< picture width
  57.     int height;                             ///< picture height
  58.     unsigned int bpp;                       ///< bits per pixel
  59.     int compr;                              ///< compression level
  60.     int bpp_tab_size;                       ///< bpp_tab size
  61.     enum TiffPhotometric photometric_interpretation;  ///< photometric interpretation
  62.     int strips;                             ///< number of strips
  63.     uint32_t *strip_sizes;
  64.     unsigned int strip_sizes_size;
  65.     uint32_t *strip_offsets;
  66.     unsigned int strip_offsets_size;
  67.     uint8_t *yuv_line;
  68.     unsigned int yuv_line_size;
  69.     int rps;                                ///< row per strip
  70.     uint8_t entries[TIFF_MAX_ENTRY * 12];   ///< entries in header
  71.     int num_entries;                        ///< number of entries
  72.     uint8_t **buf;                          ///< actual position in buffer
  73.     uint8_t *buf_start;                     ///< pointer to first byte in buffer
  74.     int buf_size;                           ///< buffer size
  75.     uint16_t subsampling[2];                ///< YUV subsampling factors
  76.     struct LZWEncodeState *lzws;            ///< LZW encode state
  77.     uint32_t dpi;                           ///< image resolution in DPI
  78. } TiffEncoderContext;
  79.  
  80. /**
  81.  * Check free space in buffer.
  82.  *
  83.  * @param s Tiff context
  84.  * @param need Needed bytes
  85.  * @return 0 - ok, 1 - no free space
  86.  */
  87. static inline int check_size(TiffEncoderContext *s, uint64_t need)
  88. {
  89.     if (s->buf_size < *s->buf - s->buf_start + need) {
  90.         *s->buf = s->buf_start + s->buf_size + 1;
  91.         av_log(s->avctx, AV_LOG_ERROR, "Buffer is too small\n");
  92.         return 1;
  93.     }
  94.     return 0;
  95. }
  96.  
  97. /**
  98.  * Put n values to buffer.
  99.  *
  100.  * @param p pointer to pointer to output buffer
  101.  * @param n number of values
  102.  * @param val pointer to values
  103.  * @param type type of values
  104.  * @param flip = 0 - normal copy, >0 - flip
  105.  */
  106. static void tnput(uint8_t **p, int n, const uint8_t *val, enum TiffTypes type,
  107.                   int flip)
  108. {
  109.     int i;
  110. #if HAVE_BIGENDIAN
  111.     flip ^= ((int[]) { 0, 0, 0, 1, 3, 3 })[type];
  112. #endif
  113.     for (i = 0; i < n * type_sizes2[type]; i++)
  114.         *(*p)++ = val[i ^ flip];
  115. }
  116.  
  117. /**
  118.  * Add entry to directory in tiff header.
  119.  *
  120.  * @param s Tiff context
  121.  * @param tag tag that identifies the entry
  122.  * @param type entry type
  123.  * @param count the number of values
  124.  * @param ptr_val pointer to values
  125.  */
  126. static int add_entry(TiffEncoderContext *s, enum TiffTags tag,
  127.                      enum TiffTypes type, int count, const void *ptr_val)
  128. {
  129.     uint8_t *entries_ptr = s->entries + 12 * s->num_entries;
  130.  
  131.     av_assert0(s->num_entries < TIFF_MAX_ENTRY);
  132.  
  133.     bytestream_put_le16(&entries_ptr, tag);
  134.     bytestream_put_le16(&entries_ptr, type);
  135.     bytestream_put_le32(&entries_ptr, count);
  136.  
  137.     if (type_sizes[type] * (int64_t)count <= 4) {
  138.         tnput(&entries_ptr, count, ptr_val, type, 0);
  139.     } else {
  140.         bytestream_put_le32(&entries_ptr, *s->buf - s->buf_start);
  141.         if (check_size(s, count * (int64_t)type_sizes2[type]))
  142.             return AVERROR_INVALIDDATA;
  143.         tnput(s->buf, count, ptr_val, type, 0);
  144.     }
  145.  
  146.     s->num_entries++;
  147.     return 0;
  148. }
  149.  
  150. static int add_entry1(TiffEncoderContext *s,
  151.                       enum TiffTags tag, enum TiffTypes type, int val)
  152. {
  153.     uint16_t w  = val;
  154.     uint32_t dw = val;
  155.     return add_entry(s, tag, type, 1,
  156.                      type == TIFF_SHORT ? (void *)&w : (void *)&dw);
  157. }
  158.  
  159. /**
  160.  * Encode one strip in tiff file.
  161.  *
  162.  * @param s Tiff context
  163.  * @param src input buffer
  164.  * @param dst output buffer
  165.  * @param n size of input buffer
  166.  * @param compr compression method
  167.  * @return number of output bytes. If an output error is encountered, a negative
  168.  * value corresponding to an AVERROR error code is returned.
  169.  */
  170. static int encode_strip(TiffEncoderContext *s, const int8_t *src,
  171.                         uint8_t *dst, int n, int compr)
  172. {
  173.     switch (compr) {
  174. #if CONFIG_ZLIB
  175.     case TIFF_DEFLATE:
  176.     case TIFF_ADOBE_DEFLATE:
  177.     {
  178.         unsigned long zlen = s->buf_size - (*s->buf - s->buf_start);
  179.         if (compress(dst, &zlen, src, n) != Z_OK) {
  180.             av_log(s->avctx, AV_LOG_ERROR, "Compressing failed\n");
  181.             return AVERROR_EXTERNAL;
  182.         }
  183.         return zlen;
  184.     }
  185. #endif
  186.     case TIFF_RAW:
  187.         if (check_size(s, n))
  188.             return AVERROR(EINVAL);
  189.         memcpy(dst, src, n);
  190.         return n;
  191.     case TIFF_PACKBITS:
  192.         return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start),
  193.                              src, 1, n, 2, 0xff, -1, 0);
  194.     case TIFF_LZW:
  195.         return ff_lzw_encode(s->lzws, src, n);
  196.     default:
  197.         av_log(s->avctx, AV_LOG_ERROR, "Unsupported compression method: %d\n",
  198.                compr);
  199.         return AVERROR(EINVAL);
  200.     }
  201. }
  202.  
  203. static void pack_yuv(TiffEncoderContext *s, const AVFrame *p,
  204.                      uint8_t *dst, int lnum)
  205. {
  206.     int i, j, k;
  207.     int w       = (s->width - 1) / s->subsampling[0] + 1;
  208.     uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
  209.     uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
  210.     if (s->width % s->subsampling[0] || s->height % s->subsampling[1]) {
  211.         for (i = 0; i < w; i++) {
  212.             for (j = 0; j < s->subsampling[1]; j++)
  213.                 for (k = 0; k < s->subsampling[0]; k++)
  214.                     *dst++ = p->data[0][FFMIN(lnum + j, s->height-1) * p->linesize[0] +
  215.                                         FFMIN(i * s->subsampling[0] + k, s->width-1)];
  216.             *dst++ = *pu++;
  217.             *dst++ = *pv++;
  218.         }
  219.     }else{
  220.         for (i = 0; i < w; i++) {
  221.             for (j = 0; j < s->subsampling[1]; j++)
  222.                 for (k = 0; k < s->subsampling[0]; k++)
  223.                     *dst++ = p->data[0][(lnum + j) * p->linesize[0] +
  224.                                         i * s->subsampling[0] + k];
  225.             *dst++ = *pu++;
  226.             *dst++ = *pv++;
  227.         }
  228.     }
  229. }
  230.  
  231. #define ADD_ENTRY(s, tag, type, count, ptr_val)         \
  232.     do {                                                \
  233.         ret = add_entry(s, tag, type, count, ptr_val);  \
  234.         if (ret < 0)                                    \
  235.             goto fail;                                  \
  236.     } while (0)
  237.  
  238. #define ADD_ENTRY1(s, tag, type, val)           \
  239.     do {                                        \
  240.         ret = add_entry1(s, tag, type, val);    \
  241.         if (ret < 0)                            \
  242.             goto fail;                          \
  243.     } while (0)
  244.  
  245. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  246.                         const AVFrame *pict, int *got_packet)
  247. {
  248.     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  249.     TiffEncoderContext *s = avctx->priv_data;
  250.     const AVFrame *const p = pict;
  251.     int i;
  252.     uint8_t *ptr;
  253.     uint8_t *offset;
  254.     uint32_t strips;
  255.     int bytes_per_row;
  256.     uint32_t res[2] = { s->dpi, 1 };    // image resolution (72/1)
  257.     uint16_t bpp_tab[4];
  258.     int ret = 0;
  259.     int is_yuv = 0, alpha = 0;
  260.     int shift_h, shift_v;
  261.     int packet_size;
  262.  
  263.     s->width          = avctx->width;
  264.     s->height         = avctx->height;
  265.     s->subsampling[0] = 1;
  266.     s->subsampling[1] = 1;
  267.  
  268.     avctx->bits_per_coded_sample =
  269.     s->bpp          = av_get_bits_per_pixel(desc);
  270.     s->bpp_tab_size = desc->nb_components;
  271.  
  272.     switch (avctx->pix_fmt) {
  273.     case AV_PIX_FMT_RGBA64LE:
  274.     case AV_PIX_FMT_RGBA:
  275.         alpha = 1;
  276.     case AV_PIX_FMT_RGB48LE:
  277.     case AV_PIX_FMT_RGB24:
  278.         s->photometric_interpretation = TIFF_PHOTOMETRIC_RGB;
  279.         break;
  280.     case AV_PIX_FMT_GRAY8:
  281.         avctx->bits_per_coded_sample = 0x28;
  282.     case AV_PIX_FMT_GRAY8A:
  283.     case AV_PIX_FMT_YA16LE:
  284.         alpha = avctx->pix_fmt == AV_PIX_FMT_GRAY8A || avctx->pix_fmt == AV_PIX_FMT_YA16LE;
  285.     case AV_PIX_FMT_GRAY16LE:
  286.     case AV_PIX_FMT_MONOBLACK:
  287.         s->photometric_interpretation = TIFF_PHOTOMETRIC_BLACK_IS_ZERO;
  288.         break;
  289.     case AV_PIX_FMT_PAL8:
  290.         s->photometric_interpretation = TIFF_PHOTOMETRIC_PALETTE;
  291.         break;
  292.     case AV_PIX_FMT_MONOWHITE:
  293.         s->photometric_interpretation = TIFF_PHOTOMETRIC_WHITE_IS_ZERO;
  294.         break;
  295.     case AV_PIX_FMT_YUV420P:
  296.     case AV_PIX_FMT_YUV422P:
  297.     case AV_PIX_FMT_YUV440P:
  298.     case AV_PIX_FMT_YUV444P:
  299.     case AV_PIX_FMT_YUV410P:
  300.     case AV_PIX_FMT_YUV411P:
  301.         av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &shift_h, &shift_v);
  302.         s->photometric_interpretation = TIFF_PHOTOMETRIC_YCBCR;
  303.         s->subsampling[0]             = 1 << shift_h;
  304.         s->subsampling[1]             = 1 << shift_v;
  305.         is_yuv                        = 1;
  306.         break;
  307.     default:
  308.         av_log(s->avctx, AV_LOG_ERROR,
  309.                "This colors format is not supported\n");
  310.         return AVERROR(EINVAL);
  311.     }
  312.  
  313.     for (i = 0; i < s->bpp_tab_size; i++)
  314.         bpp_tab[i] = desc->comp[i].depth_minus1 + 1;
  315.  
  316.     if (s->compr == TIFF_DEFLATE       ||
  317.         s->compr == TIFF_ADOBE_DEFLATE ||
  318.         s->compr == TIFF_LZW)
  319.         // best choice for DEFLATE
  320.         s->rps = s->height;
  321.     else
  322.         // suggest size of strip
  323.         s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1);
  324.     // round rps up
  325.     s->rps = ((s->rps - 1) / s->subsampling[1] + 1) * s->subsampling[1];
  326.  
  327.     strips = (s->height - 1) / s->rps + 1;
  328.  
  329.     bytes_per_row = (((s->width - 1) / s->subsampling[0] + 1) * s->bpp *
  330.                      s->subsampling[0] * s->subsampling[1] + 7) >> 3;
  331.     packet_size = avctx->height * bytes_per_row * 2 +
  332.                   avctx->height * 4 + AV_INPUT_BUFFER_MIN_SIZE;
  333.  
  334.     if ((ret = ff_alloc_packet2(avctx, pkt, packet_size, 0)) < 0)
  335.         return ret;
  336.     ptr          = pkt->data;
  337.     s->buf_start = pkt->data;
  338.     s->buf       = &ptr;
  339.     s->buf_size  = pkt->size;
  340.  
  341.     if (check_size(s, 8)) {
  342.         ret = AVERROR(EINVAL);
  343.         goto fail;
  344.     }
  345.  
  346.     // write header
  347.     bytestream_put_le16(&ptr, 0x4949);
  348.     bytestream_put_le16(&ptr, 42);
  349.  
  350.     offset = ptr;
  351.     bytestream_put_le32(&ptr, 0);
  352.  
  353.     if (strips > INT_MAX / FFMAX(sizeof(s->strip_sizes[0]), sizeof(s->strip_offsets[0]))) {
  354.         ret = AVERROR(ENOMEM);
  355.         goto fail;
  356.     }
  357.     av_fast_padded_mallocz(&s->strip_sizes  , &s->strip_sizes_size  , sizeof(s->strip_sizes  [0]) * strips);
  358.     av_fast_padded_mallocz(&s->strip_offsets, &s->strip_offsets_size, sizeof(s->strip_offsets[0]) * strips);
  359.  
  360.     if (!s->strip_sizes || !s->strip_offsets) {
  361.         ret = AVERROR(ENOMEM);
  362.         goto fail;
  363.     }
  364.  
  365.     if (is_yuv) {
  366.         av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, bytes_per_row);
  367.         if (s->yuv_line == NULL) {
  368.             av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
  369.             ret = AVERROR(ENOMEM);
  370.             goto fail;
  371.         }
  372.     }
  373.  
  374. #if CONFIG_ZLIB
  375.     if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
  376.         uint8_t *zbuf;
  377.         int zlen, zn;
  378.         int j;
  379.  
  380.         zlen = bytes_per_row * s->rps;
  381.         zbuf = av_malloc(zlen);
  382.         if (!zbuf) {
  383.             ret = AVERROR(ENOMEM);
  384.             goto fail;
  385.         }
  386.         s->strip_offsets[0] = ptr - pkt->data;
  387.         zn               = 0;
  388.         for (j = 0; j < s->rps; j++) {
  389.             if (is_yuv) {
  390.                 pack_yuv(s, p, s->yuv_line, j);
  391.                 memcpy(zbuf + zn, s->yuv_line, bytes_per_row);
  392.                 j += s->subsampling[1] - 1;
  393.             } else
  394.                 memcpy(zbuf + j * bytes_per_row,
  395.                        p->data[0] + j * p->linesize[0], bytes_per_row);
  396.             zn += bytes_per_row;
  397.         }
  398.         ret = encode_strip(s, zbuf, ptr, zn, s->compr);
  399.         av_free(zbuf);
  400.         if (ret < 0) {
  401.             av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
  402.             goto fail;
  403.         }
  404.         ptr           += ret;
  405.         s->strip_sizes[0] = ptr - pkt->data - s->strip_offsets[0];
  406.     } else
  407. #endif
  408.     {
  409.     if (s->compr == TIFF_LZW) {
  410.         s->lzws = av_malloc(ff_lzw_encode_state_size);
  411.         if (!s->lzws) {
  412.             ret = AVERROR(ENOMEM);
  413.             goto fail;
  414.         }
  415.     }
  416.     for (i = 0; i < s->height; i++) {
  417.         if (s->strip_sizes[i / s->rps] == 0) {
  418.             if (s->compr == TIFF_LZW) {
  419.                 ff_lzw_encode_init(s->lzws, ptr,
  420.                                    s->buf_size - (*s->buf - s->buf_start),
  421.                                    12, FF_LZW_TIFF, put_bits);
  422.             }
  423.             s->strip_offsets[i / s->rps] = ptr - pkt->data;
  424.         }
  425.         if (is_yuv) {
  426.             pack_yuv(s, p, s->yuv_line, i);
  427.             ret = encode_strip(s, s->yuv_line, ptr, bytes_per_row, s->compr);
  428.             i  += s->subsampling[1] - 1;
  429.         } else
  430.             ret = encode_strip(s, p->data[0] + i * p->linesize[0],
  431.                                ptr, bytes_per_row, s->compr);
  432.         if (ret < 0) {
  433.             av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
  434.             goto fail;
  435.         }
  436.         s->strip_sizes[i / s->rps] += ret;
  437.         ptr                     += ret;
  438.         if (s->compr == TIFF_LZW &&
  439.             (i == s->height - 1 || i % s->rps == s->rps - 1)) {
  440.             ret = ff_lzw_encode_flush(s->lzws, flush_put_bits);
  441.             s->strip_sizes[(i / s->rps)] += ret;
  442.             ptr                          += ret;
  443.         }
  444.     }
  445.     if (s->compr == TIFF_LZW)
  446.         av_freep(&s->lzws);
  447.     }
  448.  
  449.     s->num_entries = 0;
  450.  
  451.     ADD_ENTRY1(s, TIFF_SUBFILE, TIFF_LONG, 0);
  452.     ADD_ENTRY1(s, TIFF_WIDTH,   TIFF_LONG, s->width);
  453.     ADD_ENTRY1(s, TIFF_HEIGHT,  TIFF_LONG, s->height);
  454.  
  455.     if (s->bpp_tab_size)
  456.         ADD_ENTRY(s, TIFF_BPP, TIFF_SHORT, s->bpp_tab_size, bpp_tab);
  457.  
  458.     ADD_ENTRY1(s, TIFF_COMPR,       TIFF_SHORT, s->compr);
  459.     ADD_ENTRY1(s, TIFF_PHOTOMETRIC, TIFF_SHORT, s->photometric_interpretation);
  460.     ADD_ENTRY(s,  TIFF_STRIP_OFFS,  TIFF_LONG,  strips, s->strip_offsets);
  461.  
  462.     if (s->bpp_tab_size)
  463.         ADD_ENTRY1(s, TIFF_SAMPLES_PER_PIXEL, TIFF_SHORT, s->bpp_tab_size);
  464.  
  465.     ADD_ENTRY1(s, TIFF_ROWSPERSTRIP, TIFF_LONG,     s->rps);
  466.     ADD_ENTRY(s,  TIFF_STRIP_SIZE,   TIFF_LONG,     strips, s->strip_sizes);
  467.     ADD_ENTRY(s,  TIFF_XRES,         TIFF_RATIONAL, 1,      res);
  468.     if (avctx->sample_aspect_ratio.num > 0 &&
  469.         avctx->sample_aspect_ratio.den > 0) {
  470.         AVRational y = av_mul_q(av_make_q(s->dpi, 1),
  471.                                 avctx->sample_aspect_ratio);
  472.         res[0] = y.num;
  473.         res[1] = y.den;
  474.     }
  475.     ADD_ENTRY(s,  TIFF_YRES,         TIFF_RATIONAL, 1,      res);
  476.     ADD_ENTRY1(s, TIFF_RES_UNIT,     TIFF_SHORT,    2);
  477.  
  478.     if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT))
  479.         ADD_ENTRY(s, TIFF_SOFTWARE_NAME, TIFF_STRING,
  480.                   strlen(LIBAVCODEC_IDENT) + 1, LIBAVCODEC_IDENT);
  481.  
  482.     if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  483.         uint16_t pal[256 * 3];
  484.         for (i = 0; i < 256; i++) {
  485.             uint32_t rgb = *(uint32_t *) (p->data[1] + i * 4);
  486.             pal[i]       = ((rgb >> 16) & 0xff) * 257;
  487.             pal[i + 256] = ((rgb >>  8) & 0xff) * 257;
  488.             pal[i + 512] =  (rgb        & 0xff) * 257;
  489.         }
  490.         ADD_ENTRY(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal);
  491.     }
  492.     if (alpha)
  493.         ADD_ENTRY1(s,TIFF_EXTRASAMPLES,      TIFF_SHORT,            2);
  494.     if (is_yuv) {
  495.         /** according to CCIR Recommendation 601.1 */
  496.         uint32_t refbw[12] = { 15, 1, 235, 1, 128, 1, 240, 1, 128, 1, 240, 1 };
  497.         ADD_ENTRY(s, TIFF_YCBCR_SUBSAMPLING, TIFF_SHORT,    2, s->subsampling);
  498.         if (avctx->chroma_sample_location == AVCHROMA_LOC_TOPLEFT)
  499.             ADD_ENTRY1(s, TIFF_YCBCR_POSITIONING, TIFF_SHORT, 2);
  500.         ADD_ENTRY(s, TIFF_REFERENCE_BW,      TIFF_RATIONAL, 6, refbw);
  501.     }
  502.     // write offset to dir
  503.     bytestream_put_le32(&offset, ptr - pkt->data);
  504.  
  505.     if (check_size(s, 6 + s->num_entries * 12)) {
  506.         ret = AVERROR(EINVAL);
  507.         goto fail;
  508.     }
  509.     bytestream_put_le16(&ptr, s->num_entries);  // write tag count
  510.     bytestream_put_buffer(&ptr, s->entries, s->num_entries * 12);
  511.     bytestream_put_le32(&ptr, 0);
  512.  
  513.     pkt->size   = ptr - pkt->data;
  514.     pkt->flags |= AV_PKT_FLAG_KEY;
  515.     *got_packet = 1;
  516.  
  517. fail:
  518.     return ret < 0 ? ret : 0;
  519. }
  520.  
  521. static av_cold int encode_init(AVCodecContext *avctx)
  522. {
  523.     TiffEncoderContext *s = avctx->priv_data;
  524. #if FF_API_CODED_FRAME
  525. FF_DISABLE_DEPRECATION_WARNINGS
  526.     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  527.     avctx->coded_frame->key_frame = 1;
  528. FF_ENABLE_DEPRECATION_WARNINGS
  529. #endif
  530.     s->avctx = avctx;
  531.  
  532.     return 0;
  533. }
  534.  
  535. static av_cold int encode_close(AVCodecContext *avctx)
  536. {
  537.     TiffEncoderContext *s = avctx->priv_data;
  538.  
  539.     av_freep(&s->strip_sizes);
  540.     av_freep(&s->strip_offsets);
  541.     av_freep(&s->yuv_line);
  542.  
  543.     return 0;
  544. }
  545.  
  546. #define OFFSET(x) offsetof(TiffEncoderContext, x)
  547. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  548. static const AVOption options[] = {
  549.     {"dpi", "set the image resolution (in dpi)", OFFSET(dpi), AV_OPT_TYPE_INT, {.i64 = 72}, 1, 0x10000, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_ENCODING_PARAM},
  550.     { "compression_algo", NULL, OFFSET(compr), AV_OPT_TYPE_INT,   { .i64 = TIFF_PACKBITS }, TIFF_RAW, TIFF_DEFLATE, VE, "compression_algo" },
  551.     { "packbits",         NULL, 0,             AV_OPT_TYPE_CONST, { .i64 = TIFF_PACKBITS }, 0,        0,            VE, "compression_algo" },
  552.     { "raw",              NULL, 0,             AV_OPT_TYPE_CONST, { .i64 = TIFF_RAW      }, 0,        0,            VE, "compression_algo" },
  553.     { "lzw",              NULL, 0,             AV_OPT_TYPE_CONST, { .i64 = TIFF_LZW      }, 0,        0,            VE, "compression_algo" },
  554. #if CONFIG_ZLIB
  555.     { "deflate",          NULL, 0,             AV_OPT_TYPE_CONST, { .i64 = TIFF_DEFLATE  }, 0,        0,            VE, "compression_algo" },
  556. #endif
  557.     { NULL },
  558. };
  559.  
  560. static const AVClass tiffenc_class = {
  561.     .class_name = "TIFF encoder",
  562.     .item_name  = av_default_item_name,
  563.     .option     = options,
  564.     .version    = LIBAVUTIL_VERSION_INT,
  565. };
  566.  
  567. AVCodec ff_tiff_encoder = {
  568.     .name           = "tiff",
  569.     .long_name      = NULL_IF_CONFIG_SMALL("TIFF image"),
  570.     .type           = AVMEDIA_TYPE_VIDEO,
  571.     .id             = AV_CODEC_ID_TIFF,
  572.     .priv_data_size = sizeof(TiffEncoderContext),
  573.     .init           = encode_init,
  574.     .close          = encode_close,
  575.     .capabilities   = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_INTRA_ONLY,
  576.     .encode2        = encode_frame,
  577.     .pix_fmts       = (const enum AVPixelFormat[]) {
  578.         AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB48LE, AV_PIX_FMT_PAL8,
  579.         AV_PIX_FMT_RGBA, AV_PIX_FMT_RGBA64LE,
  580.         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A, AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_YA16LE,
  581.         AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_MONOWHITE,
  582.         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  583.         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  584.         AV_PIX_FMT_NONE
  585.     },
  586.     .priv_class     = &tiffenc_class,
  587. };
  588.