2aff45ad7dcda362d2234686e58ccf3874d228be
[libav.git] / libavcodec / tiff.c
1 /*
2 * TIFF image decoder
3 * Copyright (c) 2006 Konstantin Shishkov
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; 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 decoder
25 * @author Konstantin Shishkov
26 */
27
28 #include "config.h"
29 #if CONFIG_ZLIB
30 #include <zlib.h>
31 #endif
32
33 #include "libavutil/attributes.h"
34 #include "libavutil/intreadwrite.h"
35 #include "libavutil/imgutils.h"
36 #include "avcodec.h"
37 #include "bytestream.h"
38 #include "faxcompr.h"
39 #include "internal.h"
40 #include "lzw.h"
41 #include "mathops.h"
42 #include "tiff.h"
43
44 typedef struct TiffContext {
45 AVCodecContext *avctx;
46 GetByteContext gb;
47
48 int width, height;
49 unsigned int bpp, bppcount;
50 uint32_t palette[256];
51 int palette_is_set;
52 int le;
53 enum TiffCompr compr;
54 enum TiffPhotometric photometric;
55 int fax_opts;
56 int predictor;
57 int fill_order;
58
59 int strips, rps, sstype;
60 int sot;
61 int stripsizesoff, stripsize, stripoff, strippos;
62 LZWState *lzw;
63 } TiffContext;
64
65 static unsigned tget_short(GetByteContext *gb, int le)
66 {
67 return le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
68 }
69
70 static unsigned tget_long(GetByteContext *gb, int le)
71 {
72 return le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
73 }
74
75 static unsigned tget(GetByteContext *gb, int type, int le)
76 {
77 switch (type) {
78 case TIFF_BYTE: return bytestream2_get_byte(gb);
79 case TIFF_SHORT: return tget_short(gb, le);
80 case TIFF_LONG: return tget_long(gb, le);
81 default: return UINT_MAX;
82 }
83 }
84
85 #if CONFIG_ZLIB
86 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src,
87 int size)
88 {
89 z_stream zstream = { 0 };
90 int zret;
91
92 zstream.next_in = src;
93 zstream.avail_in = size;
94 zstream.next_out = dst;
95 zstream.avail_out = *len;
96 zret = inflateInit(&zstream);
97 if (zret != Z_OK) {
98 av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
99 return zret;
100 }
101 zret = inflate(&zstream, Z_SYNC_FLUSH);
102 inflateEnd(&zstream);
103 *len = zstream.total_out;
104 return zret == Z_STREAM_END ? Z_OK : zret;
105 }
106
107 static int tiff_unpack_zlib(TiffContext *s, uint8_t *dst, int stride,
108 const uint8_t *src, int size,
109 int width, int lines)
110 {
111 uint8_t *zbuf;
112 unsigned long outlen;
113 int ret, line;
114 outlen = width * lines;
115 zbuf = av_malloc(outlen);
116 if (!zbuf)
117 return AVERROR(ENOMEM);
118 ret = tiff_uncompress(zbuf, &outlen, src, size);
119 if (ret != Z_OK) {
120 av_log(s->avctx, AV_LOG_ERROR,
121 "Uncompressing failed (%lu of %lu) with error %d\n", outlen,
122 (unsigned long)width * lines, ret);
123 av_free(zbuf);
124 return AVERROR_UNKNOWN;
125 }
126 src = zbuf;
127 for (line = 0; line < lines; line++) {
128 memcpy(dst, src, width);
129 dst += stride;
130 src += width;
131 }
132 av_free(zbuf);
133 return 0;
134 }
135 #endif
136
137
138 static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride,
139 const uint8_t *src, int size, int lines)
140 {
141 int i, ret = 0;
142 uint8_t *src2 = av_malloc((unsigned)size +
143 FF_INPUT_BUFFER_PADDING_SIZE);
144
145 if (!src2) {
146 av_log(s->avctx, AV_LOG_ERROR,
147 "Error allocating temporary buffer\n");
148 return AVERROR(ENOMEM);
149 }
150 if (s->fax_opts & 2) {
151 avpriv_request_sample(s->avctx, "Uncompressed fax mode");
152 av_free(src2);
153 return AVERROR_PATCHWELCOME;
154 }
155 if (!s->fill_order) {
156 memcpy(src2, src, size);
157 } else {
158 for (i = 0; i < size; i++)
159 src2[i] = ff_reverse[src[i]];
160 }
161 memset(src2 + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
162 ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride,
163 s->compr, s->fax_opts);
164 av_free(src2);
165 return ret;
166 }
167
168 static int tiff_unpack_strip(TiffContext *s, uint8_t *dst, int stride,
169 const uint8_t *src, int size, int lines)
170 {
171 PutByteContext pb;
172 int c, line, pixels, code, ret;
173 int width = ((s->width * s->bpp) + 7) >> 3;
174
175 if (size <= 0)
176 return AVERROR_INVALIDDATA;
177
178 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
179 #if CONFIG_ZLIB
180 return tiff_unpack_zlib(s, dst, stride, src, size, width, lines);
181 #else
182 av_log(s->avctx, AV_LOG_ERROR,
183 "zlib support not enabled, "
184 "deflate compression not supported\n");
185 return AVERROR(ENOSYS);
186 #endif
187 }
188 if (s->compr == TIFF_LZW) {
189 if ((ret = ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF)) < 0) {
190 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
191 return ret;
192 }
193 for (line = 0; line < lines; line++) {
194 pixels = ff_lzw_decode(s->lzw, dst, width);
195 if (pixels < width) {
196 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n",
197 pixels, width);
198 return AVERROR_INVALIDDATA;
199 }
200 dst += stride;
201 }
202 return 0;
203 }
204 if (s->compr == TIFF_CCITT_RLE ||
205 s->compr == TIFF_G3 ||
206 s->compr == TIFF_G4) {
207 return tiff_unpack_fax(s, dst, stride, src, size, lines);
208 }
209
210 bytestream2_init(&s->gb, src, size);
211 bytestream2_init_writer(&pb, dst, stride * lines);
212
213 for (line = 0; line < lines; line++) {
214 if (bytestream2_get_bytes_left(&s->gb) == 0 || bytestream2_get_eof(&pb))
215 break;
216 bytestream2_seek_p(&pb, stride * line, SEEK_SET);
217 switch (s->compr) {
218 case TIFF_RAW:
219 if (!s->fill_order) {
220 bytestream2_copy_buffer(&pb, &s->gb, width);
221 } else {
222 int i;
223 for (i = 0; i < width; i++)
224 bytestream2_put_byte(&pb, ff_reverse[bytestream2_get_byte(&s->gb)]);
225 }
226 break;
227 case TIFF_PACKBITS:
228 for (pixels = 0; pixels < width;) {
229 code = ff_u8_to_s8(bytestream2_get_byte(&s->gb));
230 if (code >= 0) {
231 code++;
232 bytestream2_copy_buffer(&pb, &s->gb, code);
233 pixels += code;
234 } else if (code != -128) { // -127..-1
235 code = (-code) + 1;
236 c = bytestream2_get_byte(&s->gb);
237 bytestream2_set_buffer(&pb, c, code);
238 pixels += code;
239 }
240 }
241 break;
242 }
243 }
244 return 0;
245 }
246
247 static int init_image(TiffContext *s, AVFrame *frame)
248 {
249 int i, ret;
250 uint32_t *pal;
251
252 switch (s->bpp * 10 + s->bppcount) {
253 case 11:
254 s->avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
255 break;
256 case 81:
257 s->avctx->pix_fmt = AV_PIX_FMT_PAL8;
258 break;
259 case 243:
260 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
261 break;
262 case 161:
263 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_GRAY16LE : AV_PIX_FMT_GRAY16BE;
264 break;
265 case 162:
266 s->avctx->pix_fmt = AV_PIX_FMT_YA8;
267 break;
268 case 322:
269 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YA16LE : AV_PIX_FMT_YA16BE;
270 break;
271 case 324:
272 s->avctx->pix_fmt = AV_PIX_FMT_RGBA;
273 break;
274 case 483:
275 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE;
276 break;
277 default:
278 av_log(s->avctx, AV_LOG_ERROR,
279 "This format is not supported (bpp=%d, bppcount=%d)\n",
280 s->bpp, s->bppcount);
281 return AVERROR_INVALIDDATA;
282 }
283 if (s->width != s->avctx->width || s->height != s->avctx->height) {
284 ret = ff_set_dimensions(s->avctx, s->width, s->height);
285 if (ret < 0)
286 return ret;
287 }
288 if ((ret = ff_get_buffer(s->avctx, frame, 0)) < 0) {
289 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
290 return ret;
291 }
292 if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
293 if (s->palette_is_set) {
294 memcpy(frame->data[1], s->palette, sizeof(s->palette));
295 } else {
296 /* make default grayscale pal */
297 pal = (uint32_t *) frame->data[1];
298 for (i = 0; i < 256; i++)
299 pal[i] = i * 0x010101;
300 }
301 }
302 return 0;
303 }
304
305 static int tiff_decode_tag(TiffContext *s)
306 {
307 unsigned tag, type, count, off, value = 0;
308 int i, start;
309
310 if (bytestream2_get_bytes_left(&s->gb) < 12)
311 return AVERROR_INVALIDDATA;
312 tag = tget_short(&s->gb, s->le);
313 type = tget_short(&s->gb, s->le);
314 count = tget_long(&s->gb, s->le);
315 off = tget_long(&s->gb, s->le);
316 start = bytestream2_tell(&s->gb);
317
318 if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
319 av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n",
320 type);
321 return 0;
322 }
323
324 if (count == 1) {
325 switch (type) {
326 case TIFF_BYTE:
327 case TIFF_SHORT:
328 bytestream2_seek(&s->gb, -4, SEEK_CUR);
329 value = tget(&s->gb, type, s->le);
330 break;
331 case TIFF_LONG:
332 value = off;
333 break;
334 case TIFF_STRING:
335 if (count <= 4) {
336 bytestream2_seek(&s->gb, -4, SEEK_CUR);
337 break;
338 }
339 default:
340 value = UINT_MAX;
341 bytestream2_seek(&s->gb, off, SEEK_SET);
342 }
343 } else {
344 if (count <= 4 && type_sizes[type] * count <= 4)
345 bytestream2_seek(&s->gb, -4, SEEK_CUR);
346 else
347 bytestream2_seek(&s->gb, off, SEEK_SET);
348 }
349
350 switch (tag) {
351 case TIFF_WIDTH:
352 s->width = value;
353 break;
354 case TIFF_HEIGHT:
355 s->height = value;
356 break;
357 case TIFF_BPP:
358 s->bppcount = count;
359 if (count > 4) {
360 av_log(s->avctx, AV_LOG_ERROR,
361 "This format is not supported (bpp=%d, %d components)\n",
362 s->bpp, count);
363 return AVERROR_INVALIDDATA;
364 }
365 if (count == 1)
366 s->bpp = value;
367 else {
368 switch (type) {
369 case TIFF_BYTE:
370 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) +
371 ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
372 break;
373 case TIFF_SHORT:
374 case TIFF_LONG:
375 s->bpp = 0;
376 for (i = 0; i < count; i++)
377 s->bpp += tget(&s->gb, type, s->le);
378 break;
379 default:
380 s->bpp = -1;
381 }
382 }
383 break;
384 case TIFF_SAMPLES_PER_PIXEL:
385 if (count != 1) {
386 av_log(s->avctx, AV_LOG_ERROR,
387 "Samples per pixel requires a single value, many provided\n");
388 return AVERROR_INVALIDDATA;
389 }
390 if (s->bppcount == 1)
391 s->bpp *= value;
392 s->bppcount = value;
393 break;
394 case TIFF_COMPR:
395 s->compr = value;
396 s->predictor = 0;
397 switch (s->compr) {
398 case TIFF_RAW:
399 case TIFF_PACKBITS:
400 case TIFF_LZW:
401 case TIFF_CCITT_RLE:
402 break;
403 case TIFF_G3:
404 case TIFF_G4:
405 s->fax_opts = 0;
406 break;
407 case TIFF_DEFLATE:
408 case TIFF_ADOBE_DEFLATE:
409 #if CONFIG_ZLIB
410 break;
411 #else
412 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
413 return AVERROR(ENOSYS);
414 #endif
415 case TIFF_JPEG:
416 case TIFF_NEWJPEG:
417 avpriv_report_missing_feature(s->avctx, "JPEG compression");
418 return AVERROR_PATCHWELCOME;
419 default:
420 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n",
421 s->compr);
422 return AVERROR_INVALIDDATA;
423 }
424 break;
425 case TIFF_ROWSPERSTRIP:
426 if (!value || (type == TIFF_LONG && value == UINT_MAX))
427 value = s->height;
428 s->rps = FFMIN(value, s->height);
429 break;
430 case TIFF_STRIP_OFFS:
431 if (count == 1) {
432 s->strippos = 0;
433 s->stripoff = value;
434 } else
435 s->strippos = off;
436 s->strips = count;
437 if (s->strips == 1)
438 s->rps = s->height;
439 s->sot = type;
440 break;
441 case TIFF_STRIP_SIZE:
442 if (count == 1) {
443 s->stripsizesoff = 0;
444 s->stripsize = value;
445 s->strips = 1;
446 } else {
447 s->stripsizesoff = off;
448 }
449 s->strips = count;
450 s->sstype = type;
451 break;
452 case TIFF_PREDICTOR:
453 s->predictor = value;
454 break;
455 case TIFF_PHOTOMETRIC:
456 switch (value) {
457 case TIFF_PHOTOMETRIC_WHITE_IS_ZERO:
458 case TIFF_PHOTOMETRIC_BLACK_IS_ZERO:
459 case TIFF_PHOTOMETRIC_RGB:
460 case TIFF_PHOTOMETRIC_PALETTE:
461 s->photometric = value;
462 break;
463 case TIFF_PHOTOMETRIC_ALPHA_MASK:
464 case TIFF_PHOTOMETRIC_SEPARATED:
465 case TIFF_PHOTOMETRIC_YCBCR:
466 case TIFF_PHOTOMETRIC_CIE_LAB:
467 case TIFF_PHOTOMETRIC_ICC_LAB:
468 case TIFF_PHOTOMETRIC_ITU_LAB:
469 case TIFF_PHOTOMETRIC_CFA:
470 case TIFF_PHOTOMETRIC_LOG_L:
471 case TIFF_PHOTOMETRIC_LOG_LUV:
472 case TIFF_PHOTOMETRIC_LINEAR_RAW:
473 avpriv_report_missing_feature(s->avctx,
474 "PhotometricInterpretation 0x%04X",
475 value);
476 return AVERROR_PATCHWELCOME;
477 default:
478 av_log(s->avctx, AV_LOG_ERROR, "PhotometricInterpretation %u is "
479 "unknown\n", value);
480 return AVERROR_INVALIDDATA;
481 }
482 break;
483 case TIFF_FILL_ORDER:
484 if (value < 1 || value > 2) {
485 av_log(s->avctx, AV_LOG_ERROR,
486 "Unknown FillOrder value %d, trying default one\n", value);
487 value = 1;
488 }
489 s->fill_order = value - 1;
490 break;
491 case TIFF_PAL: {
492 GetByteContext pal_gb[3];
493 off = type_sizes[type];
494 if (count / 3 > 256 ||
495 bytestream2_get_bytes_left(&s->gb) < count / 3 * off * 3)
496 return AVERROR_INVALIDDATA;
497 pal_gb[0] = pal_gb[1] = pal_gb[2] = s->gb;
498 bytestream2_skip(&pal_gb[1], count / 3 * off);
499 bytestream2_skip(&pal_gb[2], count / 3 * off * 2);
500 off = (type_sizes[type] - 1) << 3;
501 for (i = 0; i < count / 3; i++) {
502 uint32_t p = 0xFF000000;
503 p |= (tget(&pal_gb[0], type, s->le) >> off) << 16;
504 p |= (tget(&pal_gb[1], type, s->le) >> off) << 8;
505 p |= tget(&pal_gb[2], type, s->le) >> off;
506 s->palette[i] = p;
507 }
508 s->palette_is_set = 1;
509 break;
510 }
511 case TIFF_PLANAR:
512 if (value == 2) {
513 avpriv_report_missing_feature(s->avctx, "Planar format");
514 return AVERROR_PATCHWELCOME;
515 }
516 break;
517 case TIFF_T4OPTIONS:
518 if (s->compr == TIFF_G3)
519 s->fax_opts = value;
520 break;
521 case TIFF_T6OPTIONS:
522 if (s->compr == TIFF_G4)
523 s->fax_opts = value;
524 break;
525 default:
526 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
527 av_log(s->avctx, AV_LOG_ERROR,
528 "Unknown or unsupported tag %d/0X%0X\n",
529 tag, tag);
530 return AVERROR_INVALIDDATA;
531 }
532 }
533 bytestream2_seek(&s->gb, start, SEEK_SET);
534 return 0;
535 }
536
537 static int decode_frame(AVCodecContext *avctx,
538 void *data, int *got_frame, AVPacket *avpkt)
539 {
540 TiffContext *const s = avctx->priv_data;
541 AVFrame *const p = data;
542 unsigned off;
543 int id, le, ret;
544 int i, j, entries, stride;
545 unsigned soff, ssize;
546 uint8_t *dst;
547 GetByteContext stripsizes;
548 GetByteContext stripdata;
549
550 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
551
552 // parse image header
553 if (avpkt->size < 8)
554 return AVERROR_INVALIDDATA;
555 id = bytestream2_get_le16(&s->gb);
556 if (id == 0x4949)
557 le = 1;
558 else if (id == 0x4D4D)
559 le = 0;
560 else {
561 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
562 return AVERROR_INVALIDDATA;
563 }
564 s->le = le;
565 s->photometric = TIFF_PHOTOMETRIC_NONE;
566 s->compr = TIFF_RAW;
567 s->fill_order = 0;
568 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
569 // that further identifies the file as a TIFF file"
570 if (tget_short(&s->gb, le) != 42) {
571 av_log(avctx, AV_LOG_ERROR,
572 "The answer to life, universe and everything is not correct!\n");
573 return AVERROR_INVALIDDATA;
574 }
575 // Reset these offsets so we can tell if they were set this frame
576 s->stripsizesoff = s->strippos = 0;
577 /* parse image file directory */
578 off = tget_long(&s->gb, le);
579 if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
580 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
581 return AVERROR_INVALIDDATA;
582 }
583 bytestream2_seek(&s->gb, off, SEEK_SET);
584 entries = tget_short(&s->gb, le);
585 for (i = 0; i < entries; i++) {
586 if ((ret = tiff_decode_tag(s)) < 0)
587 return ret;
588 }
589 if (!s->strippos && !s->stripoff) {
590 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
591 return AVERROR_INVALIDDATA;
592 }
593 /* now we have the data and may start decoding */
594 if ((ret = init_image(s, p)) < 0)
595 return ret;
596
597 if (s->strips == 1 && !s->stripsize) {
598 av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
599 s->stripsize = avpkt->size - s->stripoff;
600 }
601 stride = p->linesize[0];
602 dst = p->data[0];
603
604 if (s->stripsizesoff) {
605 if (s->stripsizesoff >= avpkt->size)
606 return AVERROR_INVALIDDATA;
607 bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
608 avpkt->size - s->stripsizesoff);
609 }
610 if (s->strippos) {
611 if (s->strippos >= avpkt->size)
612 return AVERROR_INVALIDDATA;
613 bytestream2_init(&stripdata, avpkt->data + s->strippos,
614 avpkt->size - s->strippos);
615 }
616
617 for (i = 0; i < s->height; i += s->rps) {
618 if (s->stripsizesoff)
619 ssize = tget(&stripsizes, s->sstype, le);
620 else
621 ssize = s->stripsize;
622
623 if (s->strippos)
624 soff = tget(&stripdata, s->sot, le);
625 else
626 soff = s->stripoff;
627
628 if (soff > avpkt->size || ssize > avpkt->size - soff) {
629 av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
630 return AVERROR_INVALIDDATA;
631 }
632 if ((ret = tiff_unpack_strip(s, dst, stride, avpkt->data + soff, ssize,
633 FFMIN(s->rps, s->height - i))) < 0) {
634 if (avctx->err_recognition & AV_EF_EXPLODE)
635 return ret;
636 break;
637 }
638 dst += s->rps * stride;
639 }
640 if (s->predictor == 2) {
641 dst = p->data[0];
642 soff = s->bpp >> 3;
643 ssize = s->width * soff;
644 if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48LE) {
645 for (i = 0; i < s->height; i++) {
646 for (j = soff; j < ssize; j += 2)
647 AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));
648 dst += stride;
649 }
650 } else if (s->avctx->pix_fmt == AV_PIX_FMT_RGB48BE) {
651 for (i = 0; i < s->height; i++) {
652 for (j = soff; j < ssize; j += 2)
653 AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));
654 dst += stride;
655 }
656 } else {
657 for (i = 0; i < s->height; i++) {
658 for (j = soff; j < ssize; j++)
659 dst[j] += dst[j - soff];
660 dst += stride;
661 }
662 }
663 }
664
665 if (s->photometric == TIFF_PHOTOMETRIC_WHITE_IS_ZERO) {
666 dst = p->data[0];
667 for (i = 0; i < s->height; i++) {
668 for (j = 0; j < p->linesize[0]; j++)
669 dst[j] = 255 - dst[j];
670 dst += stride;
671 }
672 }
673 *got_frame = 1;
674
675 return avpkt->size;
676 }
677
678 static av_cold int tiff_init(AVCodecContext *avctx)
679 {
680 TiffContext *s = avctx->priv_data;
681
682 s->width = 0;
683 s->height = 0;
684 s->avctx = avctx;
685 ff_lzw_decode_open(&s->lzw);
686 ff_ccitt_unpack_init();
687
688 return 0;
689 }
690
691 static av_cold int tiff_end(AVCodecContext *avctx)
692 {
693 TiffContext *const s = avctx->priv_data;
694
695 ff_lzw_decode_close(&s->lzw);
696 return 0;
697 }
698
699 AVCodec ff_tiff_decoder = {
700 .name = "tiff",
701 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
702 .type = AVMEDIA_TYPE_VIDEO,
703 .id = AV_CODEC_ID_TIFF,
704 .priv_data_size = sizeof(TiffContext),
705 .init = tiff_init,
706 .close = tiff_end,
707 .decode = decode_frame,
708 .capabilities = CODEC_CAP_DR1,
709 };