3 * Copyright (c) 2006 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
27 /* abridged list of TIFF tags */
34 TIFF_STRIP_OFFS = 0x111,
35 TIFF_ROWSPERSTRIP = 0x116,
39 TIFF_PREDICTOR = 0x13D
51 TIFF_PACKBITS = 0x8005,
63 typedef struct TiffContext {
64 AVCodecContext *avctx;
76 int stripsize, stripoff;
79 static int tget_short(uint8_t **p, int le){
80 int v = le ? LE_16(*p) : BE_16(*p);
85 static int tget_long(uint8_t **p, int le){
86 int v = le ? LE_32(*p) : BE_32(*p);
91 static int tget(uint8_t **p, int type, int le){
93 case TIFF_BYTE : return *(*p)++;
94 case TIFF_SHORT: return tget_short(p, le);
95 case TIFF_LONG : return tget_long (p, le);
100 static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, uint8_t *src, int size, int lines){
101 int c, line, pixels, code;
103 int width = s->width * (s->bpp / 8);
105 uint8_t *zbuf; unsigned long outlen;
107 if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
108 outlen = width * lines;
109 if(lines != s->height){
110 av_log(s->avctx, AV_LOG_ERROR, "This decoder won't decode ZLib-packed TIFF with %i lines per strip\n", lines);
113 zbuf = av_malloc(outlen);
114 if(uncompress(zbuf, &outlen, src, size) != Z_OK){
115 av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu)\n", outlen, (unsigned long)width * lines);
120 for(line = 0; line < lines; line++){
121 memcpy(dst, src, width);
129 for(line = 0; line < lines; line++){
130 if(src - ssrc > size){
131 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
136 memcpy(dst, src, s->width * (s->bpp / 8));
137 src += s->width * (s->bpp / 8);
140 for(pixels = 0; pixels < width;){
141 code = (int8_t)*src++;
144 if(pixels + code > width){
145 av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
148 memcpy(dst + pixels, src, code);
151 }else if(code != -128){ // -127..-1
153 if(pixels + code > width){
154 av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
158 memset(dst + pixels, c, code);
170 static int tiff_decode_tag(TiffContext *s, uint8_t *start, uint8_t *buf, uint8_t *end_buf, AVFrame *pic)
172 int tag, type, count, off, value = 0;
174 int i, j, ssize, soff, stride;
176 tag = tget_short(&buf, s->le);
177 type = tget_short(&buf, s->le);
178 count = tget_long(&buf, s->le);
179 off = tget_long(&buf, s->le);
186 value = tget(&buf, type, s->le);
201 if(buf && (buf < start || buf > end_buf)){
202 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
212 s->avctx->pix_fmt = PIX_FMT_RGB24;
213 if(s->width != s->avctx->width || s->height != s->avctx->height){
214 if(avcodec_check_dimensions(s->avctx, s->width, s->height))
216 avcodec_set_dimensions(s->avctx, s->width, s->height);
218 if(s->picture.data[0])
219 s->avctx->release_buffer(s->avctx, &s->picture);
220 if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
221 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
226 if(count == 1) s->bpp = value;
230 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF);
234 s->bpp = tget(&buf, type, s->le) + tget(&buf, type, s->le) + tget(&buf, type, s->le);
241 av_log(s->avctx, AV_LOG_ERROR, "Only RGB24 is supported\n");
252 case TIFF_ADOBE_DEFLATE:
256 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
260 av_log(s->avctx, AV_LOG_ERROR, "LZW: not implemented yet\n");
263 av_log(s->avctx, AV_LOG_ERROR, "CCITT G3 compression is not supported\n");
266 av_log(s->avctx, AV_LOG_ERROR, "CCITT G4 compression is not supported\n");
269 av_log(s->avctx, AV_LOG_ERROR, "CCITT RLE compression is not supported\n");
272 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
276 case TIFF_ROWSPERSTRIP:
277 if(value < 1 || value > s->height){
278 av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
283 case TIFF_STRIP_OFFS:
288 s->stripdata = start + off;
291 if(s->stripdata > end_buf){
292 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
296 case TIFF_STRIP_SIZE:
298 s->stripsizes = NULL;
299 s->stripsize = value;
302 s->stripsizes = start + off;
305 if(s->stripsizes > end_buf){
306 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
310 av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
313 /* now we have the data and may start decoding */
314 stride = pic->linesize[0];
316 for(i = 0; i < s->height; i += s->rps){
318 ssize = tget(&s->stripsizes, type, s->le);
320 ssize = s->stripsize;
323 soff = tget(&s->stripdata, s->sot, s->le);
327 if(tiff_unpack_strip(s, dst, stride, src, ssize, FFMIN(s->rps, s->height - i)) < 0)
329 dst += s->rps * stride;
334 av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
338 src = pic->data[0] + pic->linesize[0];
339 stride = pic->linesize[0];
341 ssize = s->width * soff;
342 for(i = 0; i < s->height; i++) {
343 for(j = soff; j < ssize; j++)
344 src[j] += src[j - soff];
353 static int decode_frame(AVCodecContext *avctx,
354 void *data, int *data_size,
355 uint8_t *buf, int buf_size)
357 TiffContext * const s = avctx->priv_data;
358 AVFrame *picture = data;
359 AVFrame * const p= (AVFrame*)&s->picture;
360 uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
365 id = LE_16(buf); buf += 2;
366 if(id == 0x4949) le = 1;
367 else if(id == 0x4D4D) le = 0;
369 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
373 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
374 // that further identifies the file as a TIFF file"
375 if(tget_short(&buf, le) != 42){
376 av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
379 /* parse image file directory */
380 off = tget_long(&buf, le);
381 if(orig_buf + off + 14 >= end_buf){
382 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
385 buf = orig_buf + off;
386 entries = tget_short(&buf, le);
387 for(i = 0; i < entries; i++){
388 if(tiff_decode_tag(s, orig_buf, buf, end_buf, p) < 0)
393 *picture= *(AVFrame*)&s->picture;
394 *data_size = sizeof(AVPicture);
399 static int tiff_init(AVCodecContext *avctx){
400 TiffContext *s = avctx->priv_data;
405 avcodec_get_frame_defaults((AVFrame*)&s->picture);
406 avctx->coded_frame= (AVFrame*)&s->picture;
407 s->picture.data[0] = NULL;
412 static int tiff_end(AVCodecContext *avctx)
414 TiffContext * const s = avctx->priv_data;
416 if(s->picture.data[0])
417 avctx->release_buffer(avctx, &s->picture);
421 AVCodec tiff_decoder = {