Rows per strip may be >= height
[libav.git] / libavcodec / tiff.c
CommitLineData
a991b1fe
KS
1/*
2 * TIFF image decoder
3 * Copyright (c) 2006 Konstantin Shishkov
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#include "avcodec.h"
23#ifdef CONFIG_ZLIB
24#include <zlib.h>
25#endif
ffc5430b 26#include "lzw.h"
a991b1fe
KS
27
28/* abridged list of TIFF tags */
29enum TiffTags{
30 TIFF_WIDTH = 0x100,
31 TIFF_HEIGHT,
32 TIFF_BPP,
33 TIFF_COMPR,
34 TIFF_INVERT = 0x106,
35 TIFF_STRIP_OFFS = 0x111,
36 TIFF_ROWSPERSTRIP = 0x116,
37 TIFF_STRIP_SIZE,
38 TIFF_XPOS = 0x11E,
39 TIFF_YPOS = 0x11F,
40 TIFF_PREDICTOR = 0x13D
41};
42
43enum TiffCompr{
44 TIFF_RAW = 1,
45 TIFF_CCITT_RLE,
46 TIFF_G3,
47 TIFF_G4,
48 TIFF_LZW,
49 TIFF_JPEG,
50 TIFF_NEWJPEG,
51 TIFF_ADOBE_DEFLATE,
52 TIFF_PACKBITS = 0x8005,
53 TIFF_DEFLATE = 0x80B2
54};
55
56enum TiffTypes{
57 TIFF_BYTE = 1,
58 TIFF_STRING,
59 TIFF_SHORT,
60 TIFF_LONG,
61 TIFF_LONGLONG
62};
63
64typedef struct TiffContext {
65 AVCodecContext *avctx;
66 AVFrame picture;
67
68 int width, height;
69 unsigned int bpp;
70 int le;
71 int compr;
72
73 int strips, rps;
74 int sot;
75 uint8_t* stripdata;
76 uint8_t* stripsizes;
77 int stripsize, stripoff;
ffc5430b 78 LZWState *lzw;
a991b1fe
KS
79} TiffContext;
80
81static int tget_short(uint8_t **p, int le){
82 int v = le ? LE_16(*p) : BE_16(*p);
83 *p += 2;
84 return v;
85}
86
87static int tget_long(uint8_t **p, int le){
88 int v = le ? LE_32(*p) : BE_32(*p);
89 *p += 4;
90 return v;
91}
92
93static int tget(uint8_t **p, int type, int le){
94 switch(type){
95 case TIFF_BYTE : return *(*p)++;
96 case TIFF_SHORT: return tget_short(p, le);
97 case TIFF_LONG : return tget_long (p, le);
98 default : return -1;
99 }
100}
101
102static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, uint8_t *src, int size, int lines){
103 int c, line, pixels, code;
104 uint8_t *ssrc = src;
105 int width = s->width * (s->bpp / 8);
106#ifdef CONFIG_ZLIB
107 uint8_t *zbuf; unsigned long outlen;
108
109 if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
110 outlen = width * lines;
111 if(lines != s->height){
112 av_log(s->avctx, AV_LOG_ERROR, "This decoder won't decode ZLib-packed TIFF with %i lines per strip\n", lines);
113 return -1;
114 }
115 zbuf = av_malloc(outlen);
116 if(uncompress(zbuf, &outlen, src, size) != Z_OK){
117 av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu)\n", outlen, (unsigned long)width * lines);
118 av_free(zbuf);
119 return -1;
120 }
121 src = zbuf;
122 for(line = 0; line < lines; line++){
123 memcpy(dst, src, width);
124 dst += stride;
125 src += width;
126 }
127 av_free(zbuf);
128 return 0;
129 }
130#endif
ffc5430b
KS
131 if(s->compr == TIFF_LZW){
132 if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){
133 av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
134 return -1;
135 }
136 }
a991b1fe
KS
137 for(line = 0; line < lines; line++){
138 if(src - ssrc > size){
139 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
140 return -1;
141 }
142 switch(s->compr){
143 case TIFF_RAW:
144 memcpy(dst, src, s->width * (s->bpp / 8));
145 src += s->width * (s->bpp / 8);
146 break;
147 case TIFF_PACKBITS:
148 for(pixels = 0; pixels < width;){
149 code = (int8_t)*src++;
150 if(code >= 0){
151 code++;
152 if(pixels + code > width){
153 av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
154 return -1;
155 }
156 memcpy(dst + pixels, src, code);
157 src += code;
158 pixels += code;
159 }else if(code != -128){ // -127..-1
160 code = (-code) + 1;
161 if(pixels + code > width){
162 av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
163 return -1;
164 }
165 c = *src++;
166 memset(dst + pixels, c, code);
167 pixels += code;
168 }
169 }
170 break;
ffc5430b
KS
171 case TIFF_LZW:
172 pixels = ff_lzw_decode(s->lzw, dst, width);
173 if(pixels < width){
174 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
175 return -1;
176 }
177 break;
a991b1fe
KS
178 }
179 dst += stride;
180 }
181 return 0;
182}
183
184
185static int tiff_decode_tag(TiffContext *s, uint8_t *start, uint8_t *buf, uint8_t *end_buf, AVFrame *pic)
186{
187 int tag, type, count, off, value = 0;
188 uint8_t *src, *dst;
189 int i, j, ssize, soff, stride;
190
191 tag = tget_short(&buf, s->le);
192 type = tget_short(&buf, s->le);
193 count = tget_long(&buf, s->le);
194 off = tget_long(&buf, s->le);
195
196 if(count == 1){
197 switch(type){
198 case TIFF_BYTE:
199 case TIFF_SHORT:
200 buf -= 4;
201 value = tget(&buf, type, s->le);
202 buf = NULL;
203 break;
204 case TIFF_LONG:
205 value = off;
206 buf = NULL;
207 break;
208 default:
209 value = -1;
210 buf = start + off;
211 }
212 }else{
213 buf = start + off;
214 }
215
216 if(buf && (buf < start || buf > end_buf)){
217 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
218 return -1;
219 }
220
221 switch(tag){
222 case TIFF_WIDTH:
223 s->width = value;
224 break;
225 case TIFF_HEIGHT:
226 s->height = value;
227 s->avctx->pix_fmt = PIX_FMT_RGB24;
228 if(s->width != s->avctx->width || s->height != s->avctx->height){
229 if(avcodec_check_dimensions(s->avctx, s->width, s->height))
230 return -1;
231 avcodec_set_dimensions(s->avctx, s->width, s->height);
232 }
233 if(s->picture.data[0])
234 s->avctx->release_buffer(s->avctx, &s->picture);
235 if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
236 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
237 return -1;
238 }
239 break;
240 case TIFF_BPP:
241 if(count == 1) s->bpp = value;
242 else{
243 switch(type){
244 case TIFF_BYTE:
ec8a152b 245 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
a991b1fe
KS
246 break;
247 case TIFF_SHORT:
248 case TIFF_LONG:
ec8a152b
KS
249 s->bpp = 0;
250 for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le);
a991b1fe
KS
251 break;
252 default:
253 s->bpp = -1;
254 }
255 }
256 if(s->bpp != 24){
257 av_log(s->avctx, AV_LOG_ERROR, "Only RGB24 is supported\n");
258 return -1;
259 }
260 break;
261 case TIFF_COMPR:
262 s->compr = value;
263 switch(s->compr){
264 case TIFF_RAW:
265 case TIFF_PACKBITS:
ffc5430b 266 case TIFF_LZW:
a991b1fe
KS
267 break;
268 case TIFF_DEFLATE:
269 case TIFF_ADOBE_DEFLATE:
270#ifdef CONFIG_ZLIB
271 break;
272#else
273 av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
274 return -1;
275#endif
a991b1fe
KS
276 case TIFF_G3:
277 av_log(s->avctx, AV_LOG_ERROR, "CCITT G3 compression is not supported\n");
278 return -1;
279 case TIFF_G4:
280 av_log(s->avctx, AV_LOG_ERROR, "CCITT G4 compression is not supported\n");
281 return -1;
282 case TIFF_CCITT_RLE:
283 av_log(s->avctx, AV_LOG_ERROR, "CCITT RLE compression is not supported\n");
284 return -1;
27c18939
KS
285 case TIFF_JPEG:
286 case TIFF_NEWJPEG:
287 av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
288 return -1;
a991b1fe
KS
289 default:
290 av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
291 return -1;
292 }
293 break;
294 case TIFF_ROWSPERSTRIP:
380fd08f 295 if(value < 1){
a991b1fe
KS
296 av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
297 return -1;
298 }
299 s->rps = value;
300 break;
301 case TIFF_STRIP_OFFS:
302 if(count == 1){
303 s->stripdata = NULL;
304 s->stripoff = value;
305 }else
306 s->stripdata = start + off;
307 s->strips = count;
308 s->sot = type;
309 if(s->stripdata > end_buf){
310 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
311 return -1;
312 }
313 break;
314 case TIFF_STRIP_SIZE:
315 if(count == 1){
316 s->stripsizes = NULL;
317 s->stripsize = value;
318 s->strips = 1;
319 }else{
320 s->stripsizes = start + off;
321 }
322 s->strips = count;
323 if(s->stripsizes > end_buf){
324 av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
325 return -1;
326 }
327 if(!pic->data[0]){
328 av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
329 return -1;
330 }
331 /* now we have the data and may start decoding */
332 stride = pic->linesize[0];
333 dst = pic->data[0];
334 for(i = 0; i < s->height; i += s->rps){
335 if(s->stripsizes)
336 ssize = tget(&s->stripsizes, type, s->le);
337 else
338 ssize = s->stripsize;
339
340 if(s->stripdata){
341 soff = tget(&s->stripdata, s->sot, s->le);
342 }else
343 soff = s->stripoff;
344 src = start + soff;
345 if(tiff_unpack_strip(s, dst, stride, src, ssize, FFMIN(s->rps, s->height - i)) < 0)
346 break;
347 dst += s->rps * stride;
348 }
349 break;
350 case TIFF_PREDICTOR:
351 if(!pic->data[0]){
352 av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
353 return -1;
354 }
355 if(value == 2){
b0c32a70 356 src = pic->data[0];
a991b1fe
KS
357 stride = pic->linesize[0];
358 soff = s->bpp >> 3;
359 ssize = s->width * soff;
360 for(i = 0; i < s->height; i++) {
361 for(j = soff; j < ssize; j++)
362 src[j] += src[j - soff];
363 src += stride;
364 }
365 }
366 break;
367 }
368 return 0;
369}
370
371static int decode_frame(AVCodecContext *avctx,
372 void *data, int *data_size,
373 uint8_t *buf, int buf_size)
374{
375 TiffContext * const s = avctx->priv_data;
376 AVFrame *picture = data;
377 AVFrame * const p= (AVFrame*)&s->picture;
378 uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
379 int id, le, off;
380 int i, entries;
381
382 //parse image header
383 id = LE_16(buf); buf += 2;
384 if(id == 0x4949) le = 1;
385 else if(id == 0x4D4D) le = 0;
386 else{
387 av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
388 return -1;
389 }
390 s->le = le;
391 // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
392 // that further identifies the file as a TIFF file"
393 if(tget_short(&buf, le) != 42){
394 av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
395 return -1;
396 }
397 /* parse image file directory */
398 off = tget_long(&buf, le);
399 if(orig_buf + off + 14 >= end_buf){
400 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
401 return -1;
402 }
403 buf = orig_buf + off;
404 entries = tget_short(&buf, le);
405 for(i = 0; i < entries; i++){
406 if(tiff_decode_tag(s, orig_buf, buf, end_buf, p) < 0)
407 return -1;
408 buf += 12;
409 }
410
411 *picture= *(AVFrame*)&s->picture;
412 *data_size = sizeof(AVPicture);
413
414 return buf_size;
415}
416
417static int tiff_init(AVCodecContext *avctx){
418 TiffContext *s = avctx->priv_data;
419
420 s->width = 0;
421 s->height = 0;
422 s->avctx = avctx;
423 avcodec_get_frame_defaults((AVFrame*)&s->picture);
424 avctx->coded_frame= (AVFrame*)&s->picture;
425 s->picture.data[0] = NULL;
ffc5430b 426 ff_lzw_decode_open(&s->lzw);
a991b1fe
KS
427
428 return 0;
429}
430
431static int tiff_end(AVCodecContext *avctx)
432{
433 TiffContext * const s = avctx->priv_data;
434
ffc5430b 435 ff_lzw_decode_close(&s->lzw);
a991b1fe
KS
436 if(s->picture.data[0])
437 avctx->release_buffer(avctx, &s->picture);
438 return 0;
439}
440
441AVCodec tiff_decoder = {
442 "tiff",
443 CODEC_TYPE_VIDEO,
444 CODEC_ID_TIFF,
445 sizeof(TiffContext),
446 tiff_init,
447 NULL,
448 tiff_end,
449 decode_frame,
450 0,
451 NULL
452};