00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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 };
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
00288 s->rps = s->height;
00289 else
00290 s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1);
00291 s->rps = ((s->rps - 1) / s->subsampling[1] + 1) * s->subsampling[1];
00292
00293 strips = (s->height - 1) / s->rps + 1;
00294
00295 if (check_size(s, 8))
00296 goto fail;
00297
00298
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
00308 bytes_per_row = (((s->width - 1)/s->subsampling[0] + 1) * s->bpp
00309 * s->subsampling[0] * s->subsampling[1] + 7) >> 3;
00310 if (is_yuv){
00311 yuv_line = av_malloc(bytes_per_row);
00312 if (yuv_line == NULL){
00313 av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
00314 goto fail;
00315 }
00316 }
00317
00318 #if CONFIG_ZLIB
00319 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
00320 uint8_t *zbuf;
00321 int zlen, zn;
00322 int j;
00323
00324 zlen = bytes_per_row * s->rps;
00325 zbuf = av_malloc(zlen);
00326 strip_offsets[0] = ptr - buf;
00327 zn = 0;
00328 for (j = 0; j < s->rps; j++) {
00329 if (is_yuv){
00330 pack_yuv(s, yuv_line, j);
00331 memcpy(zbuf + zn, yuv_line, bytes_per_row);
00332 j += s->subsampling[1] - 1;
00333 }
00334 else
00335 memcpy(zbuf + j * bytes_per_row,
00336 p->data[0] + j * p->linesize[0], bytes_per_row);
00337 zn += bytes_per_row;
00338 }
00339 n = encode_strip(s, zbuf, ptr, zn, s->compr);
00340 av_free(zbuf);
00341 if (n<0) {
00342 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
00343 goto fail;
00344 }
00345 ptr += n;
00346 strip_sizes[0] = ptr - buf - strip_offsets[0];
00347 } else
00348 #endif
00349 {
00350 if(s->compr == TIFF_LZW)
00351 s->lzws = av_malloc(ff_lzw_encode_state_size);
00352 for (i = 0; i < s->height; i++) {
00353 if (strip_sizes[i / s->rps] == 0) {
00354 if(s->compr == TIFF_LZW){
00355 ff_lzw_encode_init(s->lzws, ptr, s->buf_size - (*s->buf - s->buf_start), 12);
00356 }
00357 strip_offsets[i / s->rps] = ptr - buf;
00358 }
00359 if (is_yuv){
00360 pack_yuv(s, yuv_line, i);
00361 n = encode_strip(s, yuv_line, ptr, bytes_per_row, s->compr);
00362 i += s->subsampling[1] - 1;
00363 }
00364 else
00365 n = encode_strip(s, p->data[0] + i * p->linesize[0],
00366 ptr, bytes_per_row, s->compr);
00367 if (n < 0) {
00368 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
00369 goto fail;
00370 }
00371 strip_sizes[i / s->rps] += n;
00372 ptr += n;
00373 if(s->compr == TIFF_LZW && (i==s->height-1 || i%s->rps == s->rps-1)){
00374 int ret;
00375 ret = ff_lzw_encode_flush(s->lzws);
00376 strip_sizes[(i / s->rps )] += ret ;
00377 ptr += ret;
00378 }
00379 }
00380 if(s->compr == TIFF_LZW)
00381 av_free(s->lzws);
00382 }
00383
00384 s->num_entries = 0;
00385
00386 add_entry1(s,TIFF_SUBFILE, TIFF_LONG, 0);
00387 add_entry1(s,TIFF_WIDTH, TIFF_LONG, s->width);
00388 add_entry1(s,TIFF_HEIGHT, TIFF_LONG, s->height);
00389
00390 if (s->bpp_tab_size)
00391 add_entry(s, TIFF_BPP, TIFF_SHORT, s->bpp_tab_size, bpp_tab);
00392
00393 add_entry1(s,TIFF_COMPR, TIFF_SHORT, s->compr);
00394 add_entry1(s,TIFF_INVERT, TIFF_SHORT, s->photometric_interpretation);
00395 add_entry(s, TIFF_STRIP_OFFS, TIFF_LONG, strips, strip_offsets);
00396
00397 if (s->bpp_tab_size)
00398 add_entry1(s,TIFF_SAMPLES_PER_PIXEL, TIFF_SHORT, s->bpp_tab_size);
00399
00400 add_entry1(s,TIFF_ROWSPERSTRIP, TIFF_LONG, s->rps);
00401 add_entry(s, TIFF_STRIP_SIZE, TIFF_LONG, strips, strip_sizes);
00402 add_entry(s, TIFF_XRES, TIFF_RATIONAL, 1, res);
00403 add_entry(s, TIFF_YRES, TIFF_RATIONAL, 1, res);
00404 add_entry1(s,TIFF_RES_UNIT, TIFF_SHORT, 2);
00405
00406 if(!(avctx->flags & CODEC_FLAG_BITEXACT))
00407 add_entry(s, TIFF_SOFTWARE_NAME, TIFF_STRING,
00408 strlen(LIBAVCODEC_IDENT) + 1, LIBAVCODEC_IDENT);
00409
00410 if (avctx->pix_fmt == PIX_FMT_PAL8) {
00411 uint16_t pal[256 * 3];
00412 for (i = 0; i < 256; i++) {
00413 uint32_t rgb = *(uint32_t *) (p->data[1] + i * 4);
00414 pal[i] = ((rgb >> 16) & 0xff) * 257;
00415 pal[i + 256] = ((rgb >> 8 ) & 0xff) * 257;
00416 pal[i + 512] = ( rgb & 0xff) * 257;
00417 }
00418 add_entry(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal);
00419 }
00420 if (is_yuv){
00422 uint32_t refbw[12] = {15, 1, 235, 1, 128, 1, 240, 1, 128, 1, 240, 1};
00423 add_entry(s, TIFF_YCBCR_SUBSAMPLING, TIFF_SHORT, 2, s->subsampling);
00424 add_entry(s, TIFF_REFERENCE_BW, TIFF_RATIONAL, 6, refbw);
00425 }
00426 bytestream_put_le32(&offset, ptr - buf);
00427
00428 if (check_size(s, 6 + s->num_entries * 12))
00429 goto fail;
00430 bytestream_put_le16(&ptr, s->num_entries);
00431 bytestream_put_buffer(&ptr, s->entries, s->num_entries * 12);
00432 bytestream_put_le32(&ptr, 0);
00433
00434 ret = ptr - buf;
00435
00436 fail:
00437 av_free(strip_sizes);
00438 av_free(strip_offsets);
00439 av_free(yuv_line);
00440 return ret;
00441 }
00442
00443 AVCodec tiff_encoder = {
00444 "tiff",
00445 CODEC_TYPE_VIDEO,
00446 CODEC_ID_TIFF,
00447 sizeof(TiffEncoderContext),
00448 NULL,
00449 encode_frame,
00450 NULL,
00451 NULL,
00452 0,
00453 NULL,
00454 .pix_fmts =
00455 (enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_PAL8, PIX_FMT_GRAY8,
00456 PIX_FMT_MONOBLACK, PIX_FMT_MONOWHITE,
00457 PIX_FMT_YUV420P, PIX_FMT_YUV422P,
00458 PIX_FMT_YUV444P, PIX_FMT_YUV410P,
00459 PIX_FMT_YUV411P,
00460 PIX_FMT_NONE},
00461 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
00462 };