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

libavcodec/tiffenc.c

Go to the documentation of this file.
00001 /*
00002  * TIFF image encoder
00003  * Copyright (c) 2007 Bartlomiej Wolowiec
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "bytestream.h"
00032 #include "tiff.h"
00033 #include "rle.h"
00034 #include "lzw.h"
00035 
00036 #define TIFF_MAX_ENTRY 32
00037 
00039 static const uint8_t type_sizes2[6] = {
00040     0, 1, 1, 2, 4, 8
00041 };
00042 
00043 typedef struct TiffEncoderContext {
00044     AVCodecContext *avctx;
00045     AVFrame picture;
00046 
00047     int width;                          
00048     int height;                         
00049     unsigned int bpp;                   
00050     int compr;                          
00051     int bpp_tab_size;                   
00052     int photometric_interpretation;     
00053     int strips;                         
00054     int rps;                            
00055     uint8_t entries[TIFF_MAX_ENTRY*12]; 
00056     int num_entries;                    
00057     uint8_t **buf;                      
00058     uint8_t *buf_start;                 
00059     int buf_size;                       
00060     uint16_t subsampling[2];            
00061     struct LZWEncodeState *lzws;        
00062 } TiffEncoderContext;
00063 
00064 
00071 inline static int check_size(TiffEncoderContext * s, uint64_t need)
00072 {
00073     if (s->buf_size < *s->buf - s->buf_start + need) {
00074         *s->buf = s->buf_start + s->buf_size + 1;
00075         av_log(s->avctx, AV_LOG_ERROR, "Buffer is too small\n");
00076         return 1;
00077     }
00078     return 0;
00079 }
00080 
00090 static void tnput(uint8_t ** p, int n, const uint8_t * val, enum TiffTypes type,
00091                   int flip)
00092 {
00093     int i;
00094 #ifdef WORDS_BIGENDIAN
00095     flip ^= ((int[]) {0, 0, 0, 1, 3, 3})[type];
00096 #endif
00097     for (i = 0; i < n * type_sizes2[type]; i++)
00098         *(*p)++ = val[i ^ flip];
00099 }
00100 
00109 static void add_entry(TiffEncoderContext * s,
00110                       enum TiffTags tag, enum TiffTypes type, int count,
00111                       const void *ptr_val)
00112 {
00113     uint8_t *entries_ptr = s->entries + 12 * s->num_entries;
00114 
00115     assert(s->num_entries < TIFF_MAX_ENTRY);
00116 
00117     bytestream_put_le16(&entries_ptr, tag);
00118     bytestream_put_le16(&entries_ptr, type);
00119     bytestream_put_le32(&entries_ptr, count);
00120 
00121     if (type_sizes[type] * count <= 4) {
00122         tnput(&entries_ptr, count, ptr_val, type, 0);
00123     } else {
00124         bytestream_put_le32(&entries_ptr, *s->buf - s->buf_start);
00125         check_size(s, count * type_sizes2[type]);
00126         tnput(s->buf, count, ptr_val, type, 0);
00127     }
00128 
00129     s->num_entries++;
00130 }
00131 
00132 static void add_entry1(TiffEncoderContext * s,
00133                        enum TiffTags tag, enum TiffTypes type, int val){
00134     uint16_t w = val;
00135     uint32_t dw= val;
00136     add_entry(s, tag, type, 1, type == TIFF_SHORT ? (void *)&w : (void *)&dw);
00137 }
00138 
00149 static int encode_strip(TiffEncoderContext * s, const int8_t * src,
00150                         uint8_t * dst, int n, int compr)
00151 {
00152 
00153     switch (compr) {
00154 #if CONFIG_ZLIB
00155     case TIFF_DEFLATE:
00156     case TIFF_ADOBE_DEFLATE:
00157         {
00158             unsigned long zlen = s->buf_size - (*s->buf - s->buf_start);
00159             if (compress(dst, &zlen, src, n) != Z_OK) {
00160                 av_log(s->avctx, AV_LOG_ERROR, "Compressing failed\n");
00161                 return -1;
00162             }
00163             return zlen;
00164         }
00165 #endif
00166     case TIFF_RAW:
00167         if (check_size(s, n))
00168             return -1;
00169         memcpy(dst, src, n);
00170         return n;
00171     case TIFF_PACKBITS:
00172         return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start), src, 1, n, 2, 0xff, -1, 0);
00173     case TIFF_LZW:
00174         return ff_lzw_encode(s->lzws, src, n);
00175     default:
00176         return -1;
00177     }
00178 }
00179 
00180 static void pack_yuv(TiffEncoderContext * s, uint8_t * dst, int lnum)
00181 {
00182     AVFrame *p = &s->picture;
00183     int i, j, k;
00184     int w = (s->width - 1) / s->subsampling[0] + 1;
00185     uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
00186     uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
00187     for (i = 0; i < w; i++){
00188         for (j = 0; j < s->subsampling[1]; j++)
00189             for (k = 0; k < s->subsampling[0]; k++)
00190                 *dst++ = p->data[0][(lnum + j) * p->linesize[0] +
00191                                     i * s->subsampling[0] + k];
00192         *dst++ = *pu++;
00193         *dst++ = *pv++;
00194     }
00195 }
00196 
00197 static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
00198                         int buf_size, void *data)
00199 {
00200     TiffEncoderContext *s = avctx->priv_data;
00201     AVFrame *pict = data;
00202     AVFrame *const p = (AVFrame *) & s->picture;
00203     int i;
00204     int n;
00205     uint8_t *ptr = buf;
00206     uint8_t *offset;
00207     uint32_t strips;
00208     uint32_t *strip_sizes = NULL;
00209     uint32_t *strip_offsets = NULL;
00210     int bytes_per_row;
00211     uint32_t res[2] = { 72, 1 };        // image resolution (72/1)
00212     static const uint16_t bpp_tab[] = { 8, 8, 8, 8 };
00213     int ret = -1;
00214     int is_yuv = 0;
00215     uint8_t *yuv_line = NULL;
00216     int shift_h, shift_v;
00217 
00218     s->buf_start = buf;
00219     s->buf = &ptr;
00220     s->buf_size = buf_size;
00221 
00222     *p = *pict;
00223     p->pict_type = FF_I_TYPE;
00224     p->key_frame = 1;
00225     avctx->coded_frame= &s->picture;
00226 
00227     s->compr = TIFF_PACKBITS;
00228     if (avctx->compression_level == 0) {
00229         s->compr = TIFF_RAW;
00230     } else if(avctx->compression_level == 2) {
00231         s->compr = TIFF_LZW;
00232 #if CONFIG_ZLIB
00233     } else if ((avctx->compression_level >= 3)) {
00234         s->compr = TIFF_DEFLATE;
00235 #endif
00236     }
00237 
00238     s->width = avctx->width;
00239     s->height = avctx->height;
00240     s->subsampling[0] = 1;
00241     s->subsampling[1] = 1;
00242 
00243     switch (avctx->pix_fmt) {
00244     case PIX_FMT_RGB24:
00245         s->bpp = 24;
00246         s->photometric_interpretation = 2;
00247         break;
00248     case PIX_FMT_GRAY8:
00249         s->bpp = 8;
00250         s->photometric_interpretation = 1;
00251         break;
00252     case PIX_FMT_PAL8:
00253         s->bpp = 8;
00254         s->photometric_interpretation = 3;
00255         break;
00256     case PIX_FMT_MONOBLACK:
00257         s->bpp = 1;
00258         s->photometric_interpretation = 1;
00259         break;
00260     case PIX_FMT_MONOWHITE:
00261         s->bpp = 1;
00262         s->photometric_interpretation = 0;
00263         break;
00264     case PIX_FMT_YUV420P:
00265     case PIX_FMT_YUV422P:
00266     case PIX_FMT_YUV444P:
00267     case PIX_FMT_YUV410P:
00268     case PIX_FMT_YUV411P:
00269         s->photometric_interpretation = 6;
00270         avcodec_get_chroma_sub_sample(avctx->pix_fmt,
00271                 &shift_h, &shift_v);
00272         s->bpp = 8 + (16 >> (shift_h + shift_v));
00273         s->subsampling[0] = 1 << shift_h;
00274         s->subsampling[1] = 1 << shift_v;
00275         s->bpp_tab_size = 3;
00276         is_yuv = 1;
00277         break;
00278     default:
00279         av_log(s->avctx, AV_LOG_ERROR,
00280                "This colors format is not supported\n");
00281         return -1;
00282     }
00283     if (!is_yuv)
00284         s->bpp_tab_size = (s->bpp >> 3);
00285 
00286     if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE || s->compr == TIFF_LZW)
00287         //best choose for DEFLATE
00288         s->rps = s->height;
00289     else
00290         s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1);     // suggest size of strip
00291     s->rps = ((s->rps - 1) / s->subsampling[1] + 1) * s->subsampling[1]; // round rps up
00292 
00293     strips = (s->height - 1) / s->rps + 1;
00294 
00295     if (check_size(s, 8))
00296         goto fail;
00297 
00298     // write header
00299     bytestream_put_le16(&ptr, 0x4949);
00300     bytestream_put_le16(&ptr, 42);
00301 
00302     offset = ptr;
00303     bytestream_put_le32(&ptr, 0);
00304 
00305     strip_sizes = av_mallocz(sizeof(*strip_sizes) * strips);
00306     strip_offsets = av_mallocz(sizeof(*strip_offsets) * strips);
00307     if (!strip_sizes || !strip_offsets) {
00308         ret = AVERROR(ENOMEM);
00309         goto fail;
00310     }
00311 
00312     bytes_per_row = (((s->width - 1)/s->subsampling[0] + 1) * s->bpp
00313                     * s->subsampling[0] * s->subsampling[1] + 7) >> 3;
00314     if (is_yuv){
00315         yuv_line = av_malloc(bytes_per_row);
00316         if (yuv_line == NULL){
00317             av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
00318             ret = AVERROR(ENOMEM);
00319             goto fail;
00320         }
00321     }
00322 
00323 #if CONFIG_ZLIB
00324     if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
00325         uint8_t *zbuf;
00326         int zlen, zn;
00327         int j;
00328 
00329         zlen = bytes_per_row * s->rps;
00330         zbuf = av_malloc(zlen);
00331         if (!zbuf) {
00332             ret = AVERROR(ENOMEM);
00333             goto fail;
00334         }
00335         strip_offsets[0] = ptr - buf;
00336         zn = 0;
00337         for (j = 0; j < s->rps; j++) {
00338             if (is_yuv){
00339                 pack_yuv(s, yuv_line, j);
00340                 memcpy(zbuf + zn, yuv_line, bytes_per_row);
00341                 j += s->subsampling[1] - 1;
00342             }
00343             else
00344                 memcpy(zbuf + j * bytes_per_row,
00345                        p->data[0] + j * p->linesize[0], bytes_per_row);
00346             zn += bytes_per_row;
00347         }
00348         n = encode_strip(s, zbuf, ptr, zn, s->compr);
00349         av_free(zbuf);
00350         if (n<0) {
00351             av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
00352             goto fail;
00353         }
00354         ptr += n;
00355         strip_sizes[0] = ptr - buf - strip_offsets[0];
00356     } else
00357 #endif
00358     {
00359         if (s->compr == TIFF_LZW) {
00360             s->lzws = av_malloc(ff_lzw_encode_state_size);
00361             if (!s->lzws) {
00362                 ret = AVERROR(ENOMEM);
00363                 goto fail;
00364             }
00365         }
00366         for (i = 0; i < s->height; i++) {
00367             if (strip_sizes[i / s->rps] == 0) {
00368                 if(s->compr == TIFF_LZW){
00369                     ff_lzw_encode_init(s->lzws, ptr, s->buf_size - (*s->buf - s->buf_start), 12);
00370                 }
00371                 strip_offsets[i / s->rps] = ptr - buf;
00372             }
00373             if (is_yuv){
00374                  pack_yuv(s, yuv_line, i);
00375                  n = encode_strip(s, yuv_line, ptr, bytes_per_row, s->compr);
00376                  i += s->subsampling[1] - 1;
00377             }
00378             else
00379                 n = encode_strip(s, p->data[0] + i * p->linesize[0],
00380                         ptr, bytes_per_row, s->compr);
00381             if (n < 0) {
00382                 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
00383                 goto fail;
00384             }
00385             strip_sizes[i / s->rps] += n;
00386             ptr += n;
00387             if(s->compr == TIFF_LZW && (i==s->height-1 || i%s->rps == s->rps-1)){
00388                 int ret;
00389                 ret = ff_lzw_encode_flush(s->lzws);
00390                 strip_sizes[(i / s->rps )] += ret ;
00391                 ptr += ret;
00392             }
00393         }
00394         if(s->compr == TIFF_LZW)
00395             av_free(s->lzws);
00396     }
00397 
00398     s->num_entries = 0;
00399 
00400     add_entry1(s,TIFF_SUBFILE,           TIFF_LONG,             0);
00401     add_entry1(s,TIFF_WIDTH,             TIFF_LONG,             s->width);
00402     add_entry1(s,TIFF_HEIGHT,            TIFF_LONG,             s->height);
00403 
00404     if (s->bpp_tab_size)
00405     add_entry(s, TIFF_BPP,               TIFF_SHORT,    s->bpp_tab_size, bpp_tab);
00406 
00407     add_entry1(s,TIFF_COMPR,             TIFF_SHORT,            s->compr);
00408     add_entry1(s,TIFF_INVERT,            TIFF_SHORT,            s->photometric_interpretation);
00409     add_entry(s, TIFF_STRIP_OFFS,        TIFF_LONG,     strips, strip_offsets);
00410 
00411     if (s->bpp_tab_size)
00412     add_entry1(s,TIFF_SAMPLES_PER_PIXEL, TIFF_SHORT,            s->bpp_tab_size);
00413 
00414     add_entry1(s,TIFF_ROWSPERSTRIP,      TIFF_LONG,             s->rps);
00415     add_entry(s, TIFF_STRIP_SIZE,        TIFF_LONG,     strips, strip_sizes);
00416     add_entry(s, TIFF_XRES,              TIFF_RATIONAL, 1,      res);
00417     add_entry(s, TIFF_YRES,              TIFF_RATIONAL, 1,      res);
00418     add_entry1(s,TIFF_RES_UNIT,          TIFF_SHORT,            2);
00419 
00420     if(!(avctx->flags & CODEC_FLAG_BITEXACT))
00421     add_entry(s, TIFF_SOFTWARE_NAME,     TIFF_STRING,
00422               strlen(LIBAVCODEC_IDENT) + 1, LIBAVCODEC_IDENT);
00423 
00424     if (avctx->pix_fmt == PIX_FMT_PAL8) {
00425         uint16_t pal[256 * 3];
00426         for (i = 0; i < 256; i++) {
00427             uint32_t rgb = *(uint32_t *) (p->data[1] + i * 4);
00428             pal[i]       = ((rgb >> 16) & 0xff) * 257;
00429             pal[i + 256] = ((rgb >> 8 ) & 0xff) * 257;
00430             pal[i + 512] = ( rgb        & 0xff) * 257;
00431         }
00432         add_entry(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal);
00433     }
00434     if (is_yuv){
00436         uint32_t refbw[12] = {15, 1, 235, 1, 128, 1, 240, 1, 128, 1, 240, 1};
00437         add_entry(s, TIFF_YCBCR_SUBSAMPLING, TIFF_SHORT,    2, s->subsampling);
00438         add_entry(s, TIFF_REFERENCE_BW,      TIFF_RATIONAL, 6, refbw);
00439     }
00440     bytestream_put_le32(&offset, ptr - buf);    // write offset to dir
00441 
00442     if (check_size(s, 6 + s->num_entries * 12))
00443         goto fail;
00444     bytestream_put_le16(&ptr, s->num_entries);  // write tag count
00445     bytestream_put_buffer(&ptr, s->entries, s->num_entries * 12);
00446     bytestream_put_le32(&ptr, 0);
00447 
00448     ret = ptr - buf;
00449 
00450 fail:
00451     av_free(strip_sizes);
00452     av_free(strip_offsets);
00453     av_free(yuv_line);
00454     return ret;
00455 }
00456 
00457 AVCodec tiff_encoder = {
00458     "tiff",
00459     CODEC_TYPE_VIDEO,
00460     CODEC_ID_TIFF,
00461     sizeof(TiffEncoderContext),
00462     NULL,
00463     encode_frame,
00464     NULL,
00465     NULL,
00466     0,
00467     NULL,
00468     .pix_fmts =
00469         (enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_PAL8, PIX_FMT_GRAY8,
00470                               PIX_FMT_MONOBLACK, PIX_FMT_MONOWHITE,
00471                               PIX_FMT_YUV420P, PIX_FMT_YUV422P,
00472                               PIX_FMT_YUV444P, PIX_FMT_YUV410P,
00473                               PIX_FMT_YUV411P,
00474                               PIX_FMT_NONE},
00475     .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
00476 };

Generated on Sat Feb 16 2013 09:23:14 for ffmpeg by  doxygen 1.7.1