Libav 0.7.1
libavcodec/tiff.c
Go to the documentation of this file.
00001 /*
00002  * TIFF image decoder
00003  * Copyright (c) 2006 Konstantin Shishkov
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include "avcodec.h"
00028 #if CONFIG_ZLIB
00029 #include <zlib.h>
00030 #endif
00031 #include "lzw.h"
00032 #include "tiff.h"
00033 #include "faxcompr.h"
00034 #include "libavutil/common.h"
00035 #include "libavutil/intreadwrite.h"
00036 #include "libavutil/imgutils.h"
00037 
00038 typedef struct TiffContext {
00039     AVCodecContext *avctx;
00040     AVFrame picture;
00041 
00042     int width, height;
00043     unsigned int bpp, bppcount;
00044     uint32_t palette[256];
00045     int palette_is_set;
00046     int le;
00047     enum TiffCompr compr;
00048     int invert;
00049     int fax_opts;
00050     int predictor;
00051     int fill_order;
00052 
00053     int strips, rps, sstype;
00054     int sot;
00055     const uint8_t* stripdata;
00056     const uint8_t* stripsizes;
00057     int stripsize, stripoff;
00058     LZWState *lzw;
00059 } TiffContext;
00060 
00061 static unsigned tget_short(const uint8_t **p, int le) {
00062     unsigned v = le ? AV_RL16(*p) : AV_RB16(*p);
00063     *p += 2;
00064     return v;
00065 }
00066 
00067 static unsigned tget_long(const uint8_t **p, int le) {
00068     unsigned v = le ? AV_RL32(*p) : AV_RB32(*p);
00069     *p += 4;
00070     return v;
00071 }
00072 
00073 static unsigned tget(const uint8_t **p, int type, int le) {
00074     switch(type){
00075     case TIFF_BYTE : return *(*p)++;
00076     case TIFF_SHORT: return tget_short(p, le);
00077     case TIFF_LONG : return tget_long (p, le);
00078     default        : return UINT_MAX;
00079     }
00080 }
00081 
00082 #if CONFIG_ZLIB
00083 static int tiff_uncompress(uint8_t *dst, unsigned long *len, const uint8_t *src, int size)
00084 {
00085     z_stream zstream;
00086     int zret;
00087 
00088     memset(&zstream, 0, sizeof(zstream));
00089     zstream.next_in = src;
00090     zstream.avail_in = size;
00091     zstream.next_out = dst;
00092     zstream.avail_out = *len;
00093     zret = inflateInit(&zstream);
00094     if (zret != Z_OK) {
00095         av_log(NULL, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
00096         return zret;
00097     }
00098     zret = inflate(&zstream, Z_SYNC_FLUSH);
00099     inflateEnd(&zstream);
00100     *len = zstream.total_out;
00101     return zret == Z_STREAM_END ? Z_OK : zret;
00102 }
00103 #endif
00104 
00105 static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uint8_t *src, int size, int lines){
00106     int c, line, pixels, code;
00107     const uint8_t *ssrc = src;
00108     int width = ((s->width * s->bpp) + 7) >> 3;
00109 #if CONFIG_ZLIB
00110     uint8_t *zbuf; unsigned long outlen;
00111 
00112     if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
00113         int ret;
00114         outlen = width * lines;
00115         zbuf = av_malloc(outlen);
00116         ret = tiff_uncompress(zbuf, &outlen, src, size);
00117         if(ret != Z_OK){
00118             av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu) with error %d\n", outlen, (unsigned long)width * lines, ret);
00119             av_free(zbuf);
00120             return -1;
00121         }
00122         src = zbuf;
00123         for(line = 0; line < lines; line++){
00124             memcpy(dst, src, width);
00125             dst += stride;
00126             src += width;
00127         }
00128         av_free(zbuf);
00129         return 0;
00130     }
00131 #endif
00132     if(s->compr == TIFF_LZW){
00133         if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){
00134             av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
00135             return -1;
00136         }
00137     }
00138     if(s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3 || s->compr == TIFF_G4){
00139         int i, ret = 0;
00140         uint8_t *src2 = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
00141 
00142         if(!src2 || (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE < (unsigned)size){
00143             av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");
00144             return -1;
00145         }
00146         if(s->fax_opts & 2){
00147             av_log(s->avctx, AV_LOG_ERROR, "Uncompressed fax mode is not supported (yet)\n");
00148             av_free(src2);
00149             return -1;
00150         }
00151         if(!s->fill_order){
00152             memcpy(src2, src, size);
00153         }else{
00154             for(i = 0; i < size; i++)
00155                 src2[i] = av_reverse[src[i]];
00156         }
00157         memset(src2+size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00158         switch(s->compr){
00159         case TIFF_CCITT_RLE:
00160         case TIFF_G3:
00161         case TIFF_G4:
00162             ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride, s->compr, s->fax_opts);
00163             break;
00164         }
00165         av_free(src2);
00166         return ret;
00167     }
00168     for(line = 0; line < lines; line++){
00169         if(src - ssrc > size){
00170             av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
00171             return -1;
00172         }
00173         switch(s->compr){
00174         case TIFF_RAW:
00175             if (ssrc + size - src < width)
00176                 return AVERROR_INVALIDDATA;
00177             if (!s->fill_order) {
00178                 memcpy(dst, src, width);
00179             } else {
00180                 int i;
00181                 for (i = 0; i < width; i++)
00182                     dst[i] = av_reverse[src[i]];
00183             }
00184             src += width;
00185             break;
00186         case TIFF_PACKBITS:
00187             for(pixels = 0; pixels < width;){
00188                 code = (int8_t)*src++;
00189                 if(code >= 0){
00190                     code++;
00191                     if(pixels + code > width){
00192                         av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
00193                         return -1;
00194                     }
00195                     memcpy(dst + pixels, src, code);
00196                     src += code;
00197                     pixels += code;
00198                 }else if(code != -128){ // -127..-1
00199                     code = (-code) + 1;
00200                     if(pixels + code > width){
00201                         av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
00202                         return -1;
00203                     }
00204                     c = *src++;
00205                     memset(dst + pixels, c, code);
00206                     pixels += code;
00207                 }
00208             }
00209             break;
00210         case TIFF_LZW:
00211             pixels = ff_lzw_decode(s->lzw, dst, width);
00212             if(pixels < width){
00213                 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
00214                 return -1;
00215             }
00216             break;
00217         }
00218         dst += stride;
00219     }
00220     return 0;
00221 }
00222 
00223 static int init_image(TiffContext *s)
00224 {
00225     int i, ret;
00226     uint32_t *pal;
00227 
00228     switch (s->bpp * 10 + s->bppcount) {
00229     case 11:
00230         s->avctx->pix_fmt = PIX_FMT_MONOBLACK;
00231         break;
00232     case 81:
00233         s->avctx->pix_fmt = PIX_FMT_PAL8;
00234         break;
00235     case 243:
00236         s->avctx->pix_fmt = PIX_FMT_RGB24;
00237         break;
00238     case 161:
00239         s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
00240         break;
00241     case 324:
00242         s->avctx->pix_fmt = PIX_FMT_RGBA;
00243         break;
00244     case 483:
00245         s->avctx->pix_fmt = s->le ? PIX_FMT_RGB48LE : PIX_FMT_RGB48BE;
00246         break;
00247     default:
00248         av_log(s->avctx, AV_LOG_ERROR,
00249                "This format is not supported (bpp=%d, bppcount=%d)\n",
00250                s->bpp, s->bppcount);
00251         return AVERROR_INVALIDDATA;
00252     }
00253     if (s->width != s->avctx->width || s->height != s->avctx->height) {
00254         if ((ret = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
00255             return ret;
00256         avcodec_set_dimensions(s->avctx, s->width, s->height);
00257     }
00258     if (s->picture.data[0])
00259         s->avctx->release_buffer(s->avctx, &s->picture);
00260     if ((ret = s->avctx->get_buffer(s->avctx, &s->picture)) < 0) {
00261         av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00262         return ret;
00263     }
00264     if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
00265         if (s->palette_is_set) {
00266             memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
00267         } else {
00268             /* make default grayscale pal */
00269             pal = (uint32_t *) s->picture.data[1];
00270             for (i = 0; i < 256; i++)
00271                 pal[i] = i * 0x010101;
00272         }
00273     }
00274     return 0;
00275 }
00276 
00277 static int tiff_decode_tag(TiffContext *s, const uint8_t *start, const uint8_t *buf, const uint8_t *end_buf)
00278 {
00279     unsigned tag, type, count, off, value = 0;
00280     int i, j;
00281     uint32_t *pal;
00282     const uint8_t *rp, *gp, *bp;
00283 
00284     if (end_buf - buf < 12)
00285         return -1;
00286     tag = tget_short(&buf, s->le);
00287     type = tget_short(&buf, s->le);
00288     count = tget_long(&buf, s->le);
00289     off = tget_long(&buf, s->le);
00290 
00291     if (type == 0 || type >= FF_ARRAY_ELEMS(type_sizes)) {
00292         av_log(s->avctx, AV_LOG_DEBUG, "Unknown tiff type (%u) encountered\n", type);
00293         return 0;
00294     }
00295 
00296     if(count == 1){
00297         switch(type){
00298         case TIFF_BYTE:
00299         case TIFF_SHORT:
00300             buf -= 4;
00301             value = tget(&buf, type, s->le);
00302             buf = NULL;
00303             break;
00304         case TIFF_LONG:
00305             value = off;
00306             buf = NULL;
00307             break;
00308         case TIFF_STRING:
00309             if(count <= 4){
00310                 buf -= 4;
00311                 break;
00312             }
00313         default:
00314             value = UINT_MAX;
00315             buf = start + off;
00316         }
00317     } else {
00318         if (count <= 4 && type_sizes[type] * count <= 4) {
00319             buf -= 4;
00320         } else {
00321             buf = start + off;
00322         }
00323     }
00324 
00325     if(buf && (buf < start || buf > end_buf)){
00326         av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
00327         return -1;
00328     }
00329 
00330     switch(tag){
00331     case TIFF_WIDTH:
00332         s->width = value;
00333         break;
00334     case TIFF_HEIGHT:
00335         s->height = value;
00336         break;
00337     case TIFF_BPP:
00338         s->bppcount = count;
00339         if(count > 4){
00340             av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
00341             return -1;
00342         }
00343         if(count == 1) s->bpp = value;
00344         else{
00345             switch(type){
00346             case TIFF_BYTE:
00347                 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
00348                 break;
00349             case TIFF_SHORT:
00350             case TIFF_LONG:
00351                 s->bpp = 0;
00352                 for(i = 0; i < count && buf < end_buf; i++) s->bpp += tget(&buf, type, s->le);
00353                 break;
00354             default:
00355                 s->bpp = -1;
00356             }
00357         }
00358         break;
00359     case TIFF_SAMPLES_PER_PIXEL:
00360         if (count != 1) {
00361             av_log(s->avctx, AV_LOG_ERROR,
00362                    "Samples per pixel requires a single value, many provided\n");
00363             return AVERROR_INVALIDDATA;
00364         }
00365         if (s->bppcount == 1)
00366             s->bpp *= value;
00367         s->bppcount = value;
00368         break;
00369     case TIFF_COMPR:
00370         s->compr = value;
00371         s->predictor = 0;
00372         switch(s->compr){
00373         case TIFF_RAW:
00374         case TIFF_PACKBITS:
00375         case TIFF_LZW:
00376         case TIFF_CCITT_RLE:
00377             break;
00378         case TIFF_G3:
00379         case TIFF_G4:
00380             s->fax_opts = 0;
00381             break;
00382         case TIFF_DEFLATE:
00383         case TIFF_ADOBE_DEFLATE:
00384 #if CONFIG_ZLIB
00385             break;
00386 #else
00387             av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
00388             return -1;
00389 #endif
00390         case TIFF_JPEG:
00391         case TIFF_NEWJPEG:
00392             av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
00393             return -1;
00394         default:
00395             av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
00396             return -1;
00397         }
00398         break;
00399     case TIFF_ROWSPERSTRIP:
00400         if (type == TIFF_LONG && value == UINT_MAX)
00401             value = s->avctx->height;
00402         if(value < 1){
00403             av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
00404             return -1;
00405         }
00406         s->rps = value;
00407         break;
00408     case TIFF_STRIP_OFFS:
00409         if(count == 1){
00410             s->stripdata = NULL;
00411             s->stripoff = value;
00412         }else
00413             s->stripdata = start + off;
00414         s->strips = count;
00415         if(s->strips == 1) s->rps = s->height;
00416         s->sot = type;
00417         if(s->stripdata > end_buf){
00418             av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
00419             return -1;
00420         }
00421         break;
00422     case TIFF_STRIP_SIZE:
00423         if(count == 1){
00424             s->stripsizes = NULL;
00425             s->stripsize = value;
00426             s->strips = 1;
00427         }else{
00428             s->stripsizes = start + off;
00429         }
00430         s->strips = count;
00431         s->sstype = type;
00432         if(s->stripsizes > end_buf){
00433             av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
00434             return -1;
00435         }
00436         break;
00437     case TIFF_PREDICTOR:
00438         s->predictor = value;
00439         break;
00440     case TIFF_INVERT:
00441         switch(value){
00442         case 0:
00443             s->invert = 1;
00444             break;
00445         case 1:
00446             s->invert = 0;
00447             break;
00448         case 2:
00449         case 3:
00450             break;
00451         default:
00452             av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
00453             return -1;
00454         }
00455         break;
00456     case TIFF_FILL_ORDER:
00457         if(value < 1 || value > 2){
00458             av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value);
00459             value = 1;
00460         }
00461         s->fill_order = value - 1;
00462         break;
00463     case TIFF_PAL:
00464         pal = (uint32_t *) s->palette;
00465         off = type_sizes[type];
00466         if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
00467             return -1;
00468         rp = buf;
00469         gp = buf + count / 3 * off;
00470         bp = buf + count / 3 * off * 2;
00471         off = (type_sizes[type] - 1) << 3;
00472         for(i = 0; i < count / 3; i++){
00473             j = (tget(&rp, type, s->le) >> off) << 16;
00474             j |= (tget(&gp, type, s->le) >> off) << 8;
00475             j |= tget(&bp, type, s->le) >> off;
00476             pal[i] = j;
00477         }
00478         s->palette_is_set = 1;
00479         break;
00480     case TIFF_PLANAR:
00481         if(value == 2){
00482             av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
00483             return -1;
00484         }
00485         break;
00486     case TIFF_T4OPTIONS:
00487         if(s->compr == TIFF_G3)
00488             s->fax_opts = value;
00489         break;
00490     case TIFF_T6OPTIONS:
00491         if(s->compr == TIFF_G4)
00492             s->fax_opts = value;
00493         break;
00494     default:
00495         av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n", tag, tag);
00496     }
00497     return 0;
00498 }
00499 
00500 static int decode_frame(AVCodecContext *avctx,
00501                         void *data, int *data_size,
00502                         AVPacket *avpkt)
00503 {
00504     const uint8_t *buf = avpkt->data;
00505     int buf_size = avpkt->size;
00506     TiffContext * const s = avctx->priv_data;
00507     AVFrame *picture = data;
00508     AVFrame * const p= (AVFrame*)&s->picture;
00509     const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
00510     unsigned off;
00511     int id, le, ret;
00512     int i, j, entries;
00513     int stride;
00514     unsigned soff, ssize;
00515     uint8_t *dst;
00516 
00517     //parse image header
00518     if (end_buf - buf < 8)
00519         return AVERROR_INVALIDDATA;
00520     id = AV_RL16(buf); buf += 2;
00521     if(id == 0x4949) le = 1;
00522     else if(id == 0x4D4D) le = 0;
00523     else{
00524         av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
00525         return -1;
00526     }
00527     s->le = le;
00528     s->invert = 0;
00529     s->compr = TIFF_RAW;
00530     s->fill_order = 0;
00531     // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
00532     // that further identifies the file as a TIFF file"
00533     if(tget_short(&buf, le) != 42){
00534         av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
00535         return -1;
00536     }
00537     // Reset these pointers so we can tell if they were set this frame
00538     s->stripsizes = s->stripdata = NULL;
00539     /* parse image file directory */
00540     off = tget_long(&buf, le);
00541     if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
00542         av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
00543         return AVERROR_INVALIDDATA;
00544     }
00545     buf = orig_buf + off;
00546     entries = tget_short(&buf, le);
00547     for(i = 0; i < entries; i++){
00548         if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
00549             return -1;
00550         buf += 12;
00551     }
00552     if(!s->stripdata && !s->stripoff){
00553         av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
00554         return -1;
00555     }
00556     /* now we have the data and may start decoding */
00557     if ((ret = init_image(s)) < 0)
00558         return ret;
00559 
00560     if(s->strips == 1 && !s->stripsize){
00561         av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
00562         s->stripsize = buf_size - s->stripoff;
00563     }
00564     stride = p->linesize[0];
00565     dst = p->data[0];
00566     for(i = 0; i < s->height; i += s->rps){
00567         if(s->stripsizes) {
00568             if (s->stripsizes >= end_buf)
00569                 return AVERROR_INVALIDDATA;
00570             ssize = tget(&s->stripsizes, s->sstype, s->le);
00571         } else
00572             ssize = s->stripsize;
00573 
00574         if(s->stripdata){
00575             if (s->stripdata >= end_buf)
00576                 return AVERROR_INVALIDDATA;
00577             soff = tget(&s->stripdata, s->sot, s->le);
00578         }else
00579             soff = s->stripoff;
00580 
00581         if (soff > buf_size || ssize > buf_size - soff) {
00582             av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
00583             return -1;
00584         }
00585         if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
00586             break;
00587         dst += s->rps * stride;
00588     }
00589     if(s->predictor == 2){
00590         dst = p->data[0];
00591         soff = s->bpp >> 3;
00592         ssize = s->width * soff;
00593         for(i = 0; i < s->height; i++) {
00594             for(j = soff; j < ssize; j++)
00595                 dst[j] += dst[j - soff];
00596             dst += stride;
00597         }
00598     }
00599 
00600     if(s->invert){
00601         uint8_t *src;
00602         int j;
00603 
00604         src = s->picture.data[0];
00605         for(j = 0; j < s->height; j++){
00606             for(i = 0; i < s->picture.linesize[0]; i++)
00607                 src[i] = 255 - src[i];
00608             src += s->picture.linesize[0];
00609         }
00610     }
00611     *picture= *(AVFrame*)&s->picture;
00612     *data_size = sizeof(AVPicture);
00613 
00614     return buf_size;
00615 }
00616 
00617 static av_cold int tiff_init(AVCodecContext *avctx){
00618     TiffContext *s = avctx->priv_data;
00619 
00620     s->width = 0;
00621     s->height = 0;
00622     s->avctx = avctx;
00623     avcodec_get_frame_defaults((AVFrame*)&s->picture);
00624     avctx->coded_frame= (AVFrame*)&s->picture;
00625     ff_lzw_decode_open(&s->lzw);
00626     ff_ccitt_unpack_init();
00627 
00628     return 0;
00629 }
00630 
00631 static av_cold int tiff_end(AVCodecContext *avctx)
00632 {
00633     TiffContext * const s = avctx->priv_data;
00634 
00635     ff_lzw_decode_close(&s->lzw);
00636     if(s->picture.data[0])
00637         avctx->release_buffer(avctx, &s->picture);
00638     return 0;
00639 }
00640 
00641 AVCodec ff_tiff_decoder = {
00642     "tiff",
00643     AVMEDIA_TYPE_VIDEO,
00644     CODEC_ID_TIFF,
00645     sizeof(TiffContext),
00646     tiff_init,
00647     NULL,
00648     tiff_end,
00649     decode_frame,
00650     CODEC_CAP_DR1,
00651     NULL,
00652     .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
00653 };