• 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 (s->bppcount == 1)
00364             s->bpp *= value;
00365         s->bppcount = value;
00366         break;
00367     case TIFF_COMPR:
00368         s->compr = value;
00369         s->predictor = 0;
00370         switch(s->compr){
00371         case TIFF_RAW:
00372         case TIFF_PACKBITS:
00373         case TIFF_LZW:
00374         case TIFF_CCITT_RLE:
00375             break;
00376         case TIFF_G3:
00377         case TIFF_G4:
00378             s->fax_opts = 0;
00379             break;
00380         case TIFF_DEFLATE:
00381         case TIFF_ADOBE_DEFLATE:
00382 #if CONFIG_ZLIB
00383             break;
00384 #else
00385             av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
00386             return -1;
00387 #endif
00388         case TIFF_JPEG:
00389         case TIFF_NEWJPEG:
00390             av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
00391             return -1;
00392         default:
00393             av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
00394             return -1;
00395         }
00396         break;
00397     case TIFF_ROWSPERSTRIP:
00398         if (type == TIFF_LONG && value == UINT_MAX)
00399             value = s->avctx->height;
00400         if(value < 1){
00401             av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
00402             return -1;
00403         }
00404         s->rps = value;
00405         break;
00406     case TIFF_STRIP_OFFS:
00407         if(count == 1){
00408             s->stripdata = NULL;
00409             s->stripoff = value;
00410         }else
00411             s->stripdata = start + off;
00412         s->strips = count;
00413         if(s->strips == 1) s->rps = s->height;
00414         s->sot = type;
00415         if(s->stripdata > end_buf){
00416             av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
00417             return -1;
00418         }
00419         break;
00420     case TIFF_STRIP_SIZE:
00421         if(count == 1){
00422             s->stripsizes = NULL;
00423             s->stripsize = value;
00424             s->strips = 1;
00425         }else{
00426             s->stripsizes = start + off;
00427         }
00428         s->strips = count;
00429         s->sstype = type;
00430         if(s->stripsizes > end_buf){
00431             av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
00432             return -1;
00433         }
00434         break;
00435     case TIFF_PREDICTOR:
00436         s->predictor = value;
00437         break;
00438     case TIFF_INVERT:
00439         switch(value){
00440         case 0:
00441             s->invert = 1;
00442             break;
00443         case 1:
00444             s->invert = 0;
00445             break;
00446         case 2:
00447         case 3:
00448             break;
00449         default:
00450             av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
00451             return -1;
00452         }
00453         break;
00454     case TIFF_FILL_ORDER:
00455         if(value < 1 || value > 2){
00456             av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value);
00457             value = 1;
00458         }
00459         s->fill_order = value - 1;
00460         break;
00461     case TIFF_PAL:
00462         pal = (uint32_t *) s->palette;
00463         off = type_sizes[type];
00464         if (count / 3 > 256 || end_buf - buf < count / 3 * off * 3)
00465             return -1;
00466         rp = buf;
00467         gp = buf + count / 3 * off;
00468         bp = buf + count / 3 * off * 2;
00469         off = (type_sizes[type] - 1) << 3;
00470         for(i = 0; i < count / 3; i++){
00471             j = 0xff << 24;
00472             j |= (tget(&rp, type, s->le) >> off) << 16;
00473             j |= (tget(&gp, type, s->le) >> off) << 8;
00474             j |= tget(&bp, type, s->le) >> off;
00475             pal[i] = j;
00476         }
00477         s->palette_is_set = 1;
00478         break;
00479     case TIFF_PLANAR:
00480         if(value == 2){
00481             av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
00482             return -1;
00483         }
00484         break;
00485     case TIFF_T4OPTIONS:
00486         if(s->compr == TIFF_G3)
00487             s->fax_opts = value;
00488         break;
00489     case TIFF_T6OPTIONS:
00490         if(s->compr == TIFF_G4)
00491             s->fax_opts = value;
00492         break;
00493     default:
00494         av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n", tag, tag);
00495     }
00496     return 0;
00497 }
00498 
00499 static int decode_frame(AVCodecContext *avctx,
00500                         void *data, int *data_size,
00501                         AVPacket *avpkt)
00502 {
00503     const uint8_t *buf = avpkt->data;
00504     int buf_size = avpkt->size;
00505     TiffContext * const s = avctx->priv_data;
00506     AVFrame *picture = data;
00507     AVFrame * const p= (AVFrame*)&s->picture;
00508     const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
00509     unsigned off;
00510     int id, le, ret;
00511     int i, j, entries;
00512     int stride;
00513     unsigned soff, ssize;
00514     uint8_t *dst;
00515 
00516     //parse image header
00517     if (end_buf - buf < 8)
00518         return AVERROR_INVALIDDATA;
00519     id = AV_RL16(buf); buf += 2;
00520     if(id == 0x4949) le = 1;
00521     else if(id == 0x4D4D) le = 0;
00522     else{
00523         av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
00524         return -1;
00525     }
00526     s->le = le;
00527     s->invert = 0;
00528     s->compr = TIFF_RAW;
00529     s->fill_order = 0;
00530     // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
00531     // that further identifies the file as a TIFF file"
00532     if(tget_short(&buf, le) != 42){
00533         av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
00534         return -1;
00535     }
00536     // Reset these pointers so we can tell if they were set this frame
00537     s->stripsizes = s->stripdata = NULL;
00538     /* parse image file directory */
00539     off = tget_long(&buf, le);
00540     if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {
00541         av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
00542         return AVERROR_INVALIDDATA;
00543     }
00544     buf = orig_buf + off;
00545     entries = tget_short(&buf, le);
00546     for(i = 0; i < entries; i++){
00547         if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
00548             return -1;
00549         buf += 12;
00550     }
00551     if(!s->stripdata && !s->stripoff){
00552         av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
00553         return -1;
00554     }
00555     /* now we have the data and may start decoding */
00556     if ((ret = init_image(s)) < 0)
00557         return ret;
00558 
00559     if(s->strips == 1 && !s->stripsize){
00560         av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
00561         s->stripsize = buf_size - s->stripoff;
00562     }
00563     stride = p->linesize[0];
00564     dst = p->data[0];
00565     for(i = 0; i < s->height; i += s->rps){
00566         if(s->stripsizes) {
00567             if (s->stripsizes >= end_buf)
00568                 return AVERROR_INVALIDDATA;
00569             ssize = tget(&s->stripsizes, s->sstype, s->le);
00570         } else
00571             ssize = s->stripsize;
00572 
00573         if(s->stripdata){
00574             if (s->stripdata >= end_buf)
00575                 return AVERROR_INVALIDDATA;
00576             soff = tget(&s->stripdata, s->sot, s->le);
00577         }else
00578             soff = s->stripoff;
00579 
00580         if (soff > buf_size || ssize > buf_size - soff) {
00581             av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");
00582             return -1;
00583         }
00584         if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
00585             break;
00586         dst += s->rps * stride;
00587     }
00588     if(s->predictor == 2){
00589         dst = p->data[0];
00590         soff = s->bpp >> 3;
00591         ssize = s->width * soff;
00592         for(i = 0; i < s->height; i++) {
00593             for(j = soff; j < ssize; j++)
00594                 dst[j] += dst[j - soff];
00595             dst += stride;
00596         }
00597     }
00598 
00599     if(s->invert){
00600         uint8_t *src;
00601         int j;
00602 
00603         src = s->picture.data[0];
00604         for(j = 0; j < s->height; j++){
00605             for(i = 0; i < s->picture.linesize[0]; i++)
00606                 src[i] = 255 - src[i];
00607             src += s->picture.linesize[0];
00608         }
00609     }
00610     *picture= *(AVFrame*)&s->picture;
00611     *data_size = sizeof(AVPicture);
00612 
00613     return buf_size;
00614 }
00615 
00616 static av_cold int tiff_init(AVCodecContext *avctx){
00617     TiffContext *s = avctx->priv_data;
00618 
00619     s->width = 0;
00620     s->height = 0;
00621     s->avctx = avctx;
00622     avcodec_get_frame_defaults((AVFrame*)&s->picture);
00623     avctx->coded_frame= (AVFrame*)&s->picture;
00624     ff_lzw_decode_open(&s->lzw);
00625     ff_ccitt_unpack_init();
00626 
00627     return 0;
00628 }
00629 
00630 static av_cold int tiff_end(AVCodecContext *avctx)
00631 {
00632     TiffContext * const s = avctx->priv_data;
00633 
00634     ff_lzw_decode_close(&s->lzw);
00635     if(s->picture.data[0])
00636         avctx->release_buffer(avctx, &s->picture);
00637     return 0;
00638 }
00639 
00640 AVCodec ff_tiff_decoder = {
00641     "tiff",
00642     AVMEDIA_TYPE_VIDEO,
00643     CODEC_ID_TIFF,
00644     sizeof(TiffContext),
00645     tiff_init,
00646     NULL,
00647     tiff_end,
00648     decode_frame,
00649     CODEC_CAP_DR1,
00650     NULL,
00651     .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
00652 };

Generated on Wed Apr 11 2012 07:31:35 for FFmpeg by  doxygen 1.7.1