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 int tget_short(const uint8_t **p, int le){
00062     int v = le ? AV_RL16(*p) : AV_RB16(*p);
00063     *p += 2;
00064     return v;
00065 }
00066 
00067 static int tget_long(const uint8_t **p, int le){
00068     int v = le ? AV_RL32(*p) : AV_RB32(*p);
00069     *p += 4;
00070     return v;
00071 }
00072 
00073 static int 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 -1;
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 (!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     int tag, type, count, off, value = 0;
00278     int i, j;
00279     uint32_t *pal;
00280     const uint8_t *rp, *gp, *bp;
00281 
00282     tag = tget_short(&buf, s->le);
00283     type = tget_short(&buf, s->le);
00284     count = tget_long(&buf, s->le);
00285     off = tget_long(&buf, s->le);
00286 
00287     if(count == 1){
00288         switch(type){
00289         case TIFF_BYTE:
00290         case TIFF_SHORT:
00291             buf -= 4;
00292             value = tget(&buf, type, s->le);
00293             buf = NULL;
00294             break;
00295         case TIFF_LONG:
00296             value = off;
00297             buf = NULL;
00298             break;
00299         case TIFF_STRING:
00300             if(count <= 4){
00301                 buf -= 4;
00302                 break;
00303             }
00304         default:
00305             value = -1;
00306             buf = start + off;
00307         }
00308     }else if(type_sizes[type] * count <= 4){
00309         buf -= 4;
00310     }else{
00311         buf = start + off;
00312     }
00313 
00314     if(buf && (buf < start || buf > end_buf)){
00315         av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
00316         return -1;
00317     }
00318 
00319     switch(tag){
00320     case TIFF_WIDTH:
00321         s->width = value;
00322         break;
00323     case TIFF_HEIGHT:
00324         s->height = value;
00325         break;
00326     case TIFF_BPP:
00327         s->bppcount = count;
00328         if(count > 4){
00329             av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%d, %d components)\n", s->bpp, count);
00330             return -1;
00331         }
00332         if(count == 1) s->bpp = value;
00333         else{
00334             switch(type){
00335             case TIFF_BYTE:
00336                 s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
00337                 break;
00338             case TIFF_SHORT:
00339             case TIFF_LONG:
00340                 s->bpp = 0;
00341                 for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le);
00342                 break;
00343             default:
00344                 s->bpp = -1;
00345             }
00346         }
00347         break;
00348     case TIFF_SAMPLES_PER_PIXEL:
00349         if (count != 1) {
00350             av_log(s->avctx, AV_LOG_ERROR,
00351                    "Samples per pixel requires a single value, many provided\n");
00352             return AVERROR_INVALIDDATA;
00353         }
00354         if (s->bppcount == 1)
00355             s->bpp *= value;
00356         s->bppcount = value;
00357         break;
00358     case TIFF_COMPR:
00359         s->compr = value;
00360         s->predictor = 0;
00361         switch(s->compr){
00362         case TIFF_RAW:
00363         case TIFF_PACKBITS:
00364         case TIFF_LZW:
00365         case TIFF_CCITT_RLE:
00366             break;
00367         case TIFF_G3:
00368         case TIFF_G4:
00369             s->fax_opts = 0;
00370             break;
00371         case TIFF_DEFLATE:
00372         case TIFF_ADOBE_DEFLATE:
00373 #if CONFIG_ZLIB
00374             break;
00375 #else
00376             av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
00377             return -1;
00378 #endif
00379         case TIFF_JPEG:
00380         case TIFF_NEWJPEG:
00381             av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
00382             return -1;
00383         default:
00384             av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
00385             return -1;
00386         }
00387         break;
00388     case TIFF_ROWSPERSTRIP:
00389         if(type == TIFF_LONG && value == -1)
00390             value = s->avctx->height;
00391         if(value < 1){
00392             av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
00393             return -1;
00394         }
00395         s->rps = value;
00396         break;
00397     case TIFF_STRIP_OFFS:
00398         if(count == 1){
00399             s->stripdata = NULL;
00400             s->stripoff = value;
00401         }else
00402             s->stripdata = start + off;
00403         s->strips = count;
00404         if(s->strips == 1) s->rps = s->height;
00405         s->sot = type;
00406         if(s->stripdata > end_buf){
00407             av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
00408             return -1;
00409         }
00410         break;
00411     case TIFF_STRIP_SIZE:
00412         if(count == 1){
00413             s->stripsizes = NULL;
00414             s->stripsize = value;
00415             s->strips = 1;
00416         }else{
00417             s->stripsizes = start + off;
00418         }
00419         s->strips = count;
00420         s->sstype = type;
00421         if(s->stripsizes > end_buf){
00422             av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
00423             return -1;
00424         }
00425         break;
00426     case TIFF_PREDICTOR:
00427         s->predictor = value;
00428         break;
00429     case TIFF_INVERT:
00430         switch(value){
00431         case 0:
00432             s->invert = 1;
00433             break;
00434         case 1:
00435             s->invert = 0;
00436             break;
00437         case 2:
00438         case 3:
00439             break;
00440         default:
00441             av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
00442             return -1;
00443         }
00444         break;
00445     case TIFF_FILL_ORDER:
00446         if(value < 1 || value > 2){
00447             av_log(s->avctx, AV_LOG_ERROR, "Unknown FillOrder value %d, trying default one\n", value);
00448             value = 1;
00449         }
00450         s->fill_order = value - 1;
00451         break;
00452     case TIFF_PAL:
00453         pal = (uint32_t *) s->palette;
00454         off = type_sizes[type];
00455         rp = buf;
00456         gp = buf + count / 3 * off;
00457         bp = buf + count / 3 * off * 2;
00458         off = (type_sizes[type] - 1) << 3;
00459         for(i = 0; i < count / 3; i++){
00460             j = (tget(&rp, type, s->le) >> off) << 16;
00461             j |= (tget(&gp, type, s->le) >> off) << 8;
00462             j |= tget(&bp, type, s->le) >> off;
00463             pal[i] = j;
00464         }
00465         s->palette_is_set = 1;
00466         break;
00467     case TIFF_PLANAR:
00468         if(value == 2){
00469             av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
00470             return -1;
00471         }
00472         break;
00473     case TIFF_T4OPTIONS:
00474         if(s->compr == TIFF_G3)
00475             s->fax_opts = value;
00476         break;
00477     case TIFF_T6OPTIONS:
00478         if(s->compr == TIFF_G4)
00479             s->fax_opts = value;
00480         break;
00481     default:
00482         av_log(s->avctx, AV_LOG_DEBUG, "Unknown or unsupported tag %d/0X%0X\n", tag, tag);
00483     }
00484     return 0;
00485 }
00486 
00487 static int decode_frame(AVCodecContext *avctx,
00488                         void *data, int *data_size,
00489                         AVPacket *avpkt)
00490 {
00491     const uint8_t *buf = avpkt->data;
00492     int buf_size = avpkt->size;
00493     TiffContext * const s = avctx->priv_data;
00494     AVFrame *picture = data;
00495     AVFrame * const p= (AVFrame*)&s->picture;
00496     const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
00497     int id, le, off, ret;
00498     int i, j, entries;
00499     int stride, soff, ssize;
00500     uint8_t *dst;
00501 
00502     //parse image header
00503     id = AV_RL16(buf); buf += 2;
00504     if(id == 0x4949) le = 1;
00505     else if(id == 0x4D4D) le = 0;
00506     else{
00507         av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
00508         return -1;
00509     }
00510     s->le = le;
00511     s->invert = 0;
00512     s->compr = TIFF_RAW;
00513     s->fill_order = 0;
00514     // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
00515     // that further identifies the file as a TIFF file"
00516     if(tget_short(&buf, le) != 42){
00517         av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
00518         return -1;
00519     }
00520     /* parse image file directory */
00521     off = tget_long(&buf, le);
00522     if(orig_buf + off + 14 >= end_buf){
00523         av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
00524         return -1;
00525     }
00526     buf = orig_buf + off;
00527     entries = tget_short(&buf, le);
00528     for(i = 0; i < entries; i++){
00529         if(tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)
00530             return -1;
00531         buf += 12;
00532     }
00533     if(!s->stripdata && !s->stripoff){
00534         av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
00535         return -1;
00536     }
00537     /* now we have the data and may start decoding */
00538     if ((ret = init_image(s)) < 0)
00539         return ret;
00540 
00541     if(s->strips == 1 && !s->stripsize){
00542         av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
00543         s->stripsize = buf_size - s->stripoff;
00544     }
00545     stride = p->linesize[0];
00546     dst = p->data[0];
00547     for(i = 0; i < s->height; i += s->rps){
00548         if(s->stripsizes)
00549             ssize = tget(&s->stripsizes, s->sstype, s->le);
00550         else
00551             ssize = s->stripsize;
00552 
00553         if (ssize > buf_size) {
00554             av_log(avctx, AV_LOG_ERROR, "Buffer size is smaller than strip size\n");
00555             return -1;
00556         }
00557 
00558         if(s->stripdata){
00559             soff = tget(&s->stripdata, s->sot, s->le);
00560         }else
00561             soff = s->stripoff;
00562         if (soff < 0) {
00563             av_log(avctx, AV_LOG_ERROR, "Invalid stripoff: %d\n", soff);
00564             return AVERROR(EINVAL);
00565         }
00566         if(tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize, FFMIN(s->rps, s->height - i)) < 0)
00567             break;
00568         dst += s->rps * stride;
00569     }
00570     if(s->predictor == 2){
00571         dst = p->data[0];
00572         soff = s->bpp >> 3;
00573         ssize = s->width * soff;
00574         for(i = 0; i < s->height; i++) {
00575             for(j = soff; j < ssize; j++)
00576                 dst[j] += dst[j - soff];
00577             dst += stride;
00578         }
00579     }
00580 
00581     if(s->invert){
00582         uint8_t *src;
00583         int j;
00584 
00585         src = s->picture.data[0];
00586         for(j = 0; j < s->height; j++){
00587             for(i = 0; i < s->picture.linesize[0]; i++)
00588                 src[i] = 255 - src[i];
00589             src += s->picture.linesize[0];
00590         }
00591     }
00592     *picture= *(AVFrame*)&s->picture;
00593     *data_size = sizeof(AVPicture);
00594 
00595     return buf_size;
00596 }
00597 
00598 static av_cold int tiff_init(AVCodecContext *avctx){
00599     TiffContext *s = avctx->priv_data;
00600 
00601     s->width = 0;
00602     s->height = 0;
00603     s->avctx = avctx;
00604     avcodec_get_frame_defaults((AVFrame*)&s->picture);
00605     avctx->coded_frame= (AVFrame*)&s->picture;
00606     ff_lzw_decode_open(&s->lzw);
00607     ff_ccitt_unpack_init();
00608 
00609     return 0;
00610 }
00611 
00612 static av_cold int tiff_end(AVCodecContext *avctx)
00613 {
00614     TiffContext * const s = avctx->priv_data;
00615 
00616     ff_lzw_decode_close(&s->lzw);
00617     if(s->picture.data[0])
00618         avctx->release_buffer(avctx, &s->picture);
00619     return 0;
00620 }
00621 
00622 AVCodec ff_tiff_decoder = {
00623     "tiff",
00624     AVMEDIA_TYPE_VIDEO,
00625     CODEC_ID_TIFF,
00626     sizeof(TiffContext),
00627     tiff_init,
00628     NULL,
00629     tiff_end,
00630     decode_frame,
00631     CODEC_CAP_DR1,
00632     NULL,
00633     .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
00634 };