• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/tiff.c

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

Generated on Fri Feb 22 2013 07:24:29 for FFmpeg by  doxygen 1.7.1