00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "libavutil/log.h"
00029 #include "libavutil/opt.h"
00030
00031 #include "avcodec.h"
00032 #if CONFIG_ZLIB
00033 #include <zlib.h>
00034 #endif
00035 #include "libavutil/opt.h"
00036 #include "bytestream.h"
00037 #include "tiff.h"
00038 #include "rle.h"
00039 #include "lzw.h"
00040 #include "put_bits.h"
00041
00042 #define TIFF_MAX_ENTRY 32
00043
00045 static const uint8_t type_sizes2[6] = {
00046 0, 1, 1, 2, 4, 8
00047 };
00048
00049 typedef struct TiffEncoderContext {
00050 AVClass *class;
00051 AVCodecContext *avctx;
00052 AVFrame picture;
00053
00054 int width;
00055 int height;
00056 unsigned int bpp;
00057 int compr;
00058 int bpp_tab_size;
00059 int photometric_interpretation;
00060 int strips;
00061 int rps;
00062 uint8_t entries[TIFF_MAX_ENTRY*12];
00063 int num_entries;
00064 uint8_t **buf;
00065 uint8_t *buf_start;
00066 int buf_size;
00067 uint16_t subsampling[2];
00068 struct LZWEncodeState *lzws;
00069 uint32_t dpi;
00070 } TiffEncoderContext;
00071
00072
00079 inline static int check_size(TiffEncoderContext * s, uint64_t need)
00080 {
00081 if (s->buf_size < *s->buf - s->buf_start + need) {
00082 *s->buf = s->buf_start + s->buf_size + 1;
00083 av_log(s->avctx, AV_LOG_ERROR, "Buffer is too small\n");
00084 return 1;
00085 }
00086 return 0;
00087 }
00088
00098 static void tnput(uint8_t ** p, int n, const uint8_t * val, enum TiffTypes type,
00099 int flip)
00100 {
00101 int i;
00102 #if HAVE_BIGENDIAN
00103 flip ^= ((int[]) {0, 0, 0, 1, 3, 3})[type];
00104 #endif
00105 for (i = 0; i < n * type_sizes2[type]; i++)
00106 *(*p)++ = val[i ^ flip];
00107 }
00108
00117 static void add_entry(TiffEncoderContext * s,
00118 enum TiffTags tag, enum TiffTypes type, int count,
00119 const void *ptr_val)
00120 {
00121 uint8_t *entries_ptr = s->entries + 12 * s->num_entries;
00122
00123 assert(s->num_entries < TIFF_MAX_ENTRY);
00124
00125 bytestream_put_le16(&entries_ptr, tag);
00126 bytestream_put_le16(&entries_ptr, type);
00127 bytestream_put_le32(&entries_ptr, count);
00128
00129 if (type_sizes[type] * count <= 4) {
00130 tnput(&entries_ptr, count, ptr_val, type, 0);
00131 } else {
00132 bytestream_put_le32(&entries_ptr, *s->buf - s->buf_start);
00133 check_size(s, count * type_sizes2[type]);
00134 tnput(s->buf, count, ptr_val, type, 0);
00135 }
00136
00137 s->num_entries++;
00138 }
00139
00140 static void add_entry1(TiffEncoderContext * s,
00141 enum TiffTags tag, enum TiffTypes type, int val){
00142 uint16_t w = val;
00143 uint32_t dw= val;
00144 add_entry(s, tag, type, 1, type == TIFF_SHORT ? (void *)&w : (void *)&dw);
00145 }
00146
00157 static int encode_strip(TiffEncoderContext * s, const int8_t * src,
00158 uint8_t * dst, int n, int compr)
00159 {
00160
00161 switch (compr) {
00162 #if CONFIG_ZLIB
00163 case TIFF_DEFLATE:
00164 case TIFF_ADOBE_DEFLATE:
00165 {
00166 unsigned long zlen = s->buf_size - (*s->buf - s->buf_start);
00167 if (compress(dst, &zlen, src, n) != Z_OK) {
00168 av_log(s->avctx, AV_LOG_ERROR, "Compressing failed\n");
00169 return -1;
00170 }
00171 return zlen;
00172 }
00173 #endif
00174 case TIFF_RAW:
00175 if (check_size(s, n))
00176 return -1;
00177 memcpy(dst, src, n);
00178 return n;
00179 case TIFF_PACKBITS:
00180 return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start), src, 1, n, 2, 0xff, -1, 0);
00181 case TIFF_LZW:
00182 return ff_lzw_encode(s->lzws, src, n);
00183 default:
00184 return -1;
00185 }
00186 }
00187
00188 static void pack_yuv(TiffEncoderContext * s, uint8_t * dst, int lnum)
00189 {
00190 AVFrame *p = &s->picture;
00191 int i, j, k;
00192 int w = (s->width - 1) / s->subsampling[0] + 1;
00193 uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
00194 uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
00195 for (i = 0; i < w; i++){
00196 for (j = 0; j < s->subsampling[1]; j++)
00197 for (k = 0; k < s->subsampling[0]; k++)
00198 *dst++ = p->data[0][(lnum + j) * p->linesize[0] +
00199 i * s->subsampling[0] + k];
00200 *dst++ = *pu++;
00201 *dst++ = *pv++;
00202 }
00203 }
00204
00205 static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
00206 int buf_size, void *data)
00207 {
00208 TiffEncoderContext *s = avctx->priv_data;
00209 AVFrame *pict = data;
00210 AVFrame *const p = (AVFrame *) & s->picture;
00211 int i;
00212 int n;
00213 uint8_t *ptr = buf;
00214 uint8_t *offset;
00215 uint32_t strips;
00216 uint32_t *strip_sizes = NULL;
00217 uint32_t *strip_offsets = NULL;
00218 int bytes_per_row;
00219 uint32_t res[2] = { s->dpi, 1 };
00220 uint16_t bpp_tab[] = { 8, 8, 8, 8 };
00221 int ret = -1;
00222 int is_yuv = 0;
00223 uint8_t *yuv_line = NULL;
00224 int shift_h, shift_v;
00225
00226 s->avctx = avctx;
00227 s->buf_start = buf;
00228 s->buf = &ptr;
00229 s->buf_size = buf_size;
00230
00231 *p = *pict;
00232 p->pict_type = AV_PICTURE_TYPE_I;
00233 p->key_frame = 1;
00234 avctx->coded_frame= &s->picture;
00235
00236 #if FF_API_TIFFENC_COMPLEVEL
00237 if (avctx->compression_level != FF_COMPRESSION_DEFAULT)
00238 av_log(avctx, AV_LOG_WARNING, "Using compression_level to set compression "
00239 "algorithm is deprecated. Please use the compression_algo private "
00240 "option instead.\n");
00241 if (avctx->compression_level == 0) {
00242 s->compr = TIFF_RAW;
00243 } else if(avctx->compression_level == 2) {
00244 s->compr = TIFF_LZW;
00245 #if CONFIG_ZLIB
00246 } else if ((avctx->compression_level >= 3)) {
00247 s->compr = TIFF_DEFLATE;
00248 #endif
00249 }
00250 #endif
00251
00252 s->width = avctx->width;
00253 s->height = avctx->height;
00254 s->subsampling[0] = 1;
00255 s->subsampling[1] = 1;
00256
00257 switch (avctx->pix_fmt) {
00258 case PIX_FMT_RGB48LE:
00259 s->bpp = 48;
00260 s->photometric_interpretation = 2;
00261 bpp_tab[0] = 16;
00262 bpp_tab[1] = 16;
00263 bpp_tab[2] = 16;
00264 bpp_tab[3] = 16;
00265 break;
00266 case PIX_FMT_RGBA:
00267 s->bpp = 32;
00268 s->photometric_interpretation = 2;
00269 break;
00270 case PIX_FMT_RGB24:
00271 s->bpp = 24;
00272 s->photometric_interpretation = 2;
00273 break;
00274 case PIX_FMT_GRAY8:
00275 s->bpp = 8;
00276 s->photometric_interpretation = 1;
00277 break;
00278 case PIX_FMT_PAL8:
00279 s->bpp = 8;
00280 s->photometric_interpretation = 3;
00281 break;
00282 case PIX_FMT_MONOBLACK:
00283 case PIX_FMT_MONOWHITE:
00284 s->bpp = 1;
00285 s->photometric_interpretation = avctx->pix_fmt == PIX_FMT_MONOBLACK;
00286 bpp_tab[0] = 1;
00287 break;
00288 case PIX_FMT_YUV420P:
00289 case PIX_FMT_YUV422P:
00290 case PIX_FMT_YUV444P:
00291 case PIX_FMT_YUV410P:
00292 case PIX_FMT_YUV411P:
00293 s->photometric_interpretation = 6;
00294 avcodec_get_chroma_sub_sample(avctx->pix_fmt,
00295 &shift_h, &shift_v);
00296 s->bpp = 8 + (16 >> (shift_h + shift_v));
00297 s->subsampling[0] = 1 << shift_h;
00298 s->subsampling[1] = 1 << shift_v;
00299 s->bpp_tab_size = 3;
00300 is_yuv = 1;
00301 break;
00302 default:
00303 av_log(s->avctx, AV_LOG_ERROR,
00304 "This colors format is not supported\n");
00305 return -1;
00306 }
00307 if (!is_yuv)
00308 s->bpp_tab_size = (s->bpp >= 48) ? ((s->bpp + 7) >> 4):((s->bpp + 7) >> 3);
00309
00310 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE || s->compr == TIFF_LZW)
00311
00312 s->rps = s->height;
00313 else
00314 s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1);
00315 s->rps = ((s->rps - 1) / s->subsampling[1] + 1) * s->subsampling[1];
00316
00317 strips = (s->height - 1) / s->rps + 1;
00318
00319 if (check_size(s, 8))
00320 goto fail;
00321
00322
00323 bytestream_put_le16(&ptr, 0x4949);
00324 bytestream_put_le16(&ptr, 42);
00325
00326 offset = ptr;
00327 bytestream_put_le32(&ptr, 0);
00328
00329 strip_sizes = av_mallocz(sizeof(*strip_sizes) * strips);
00330 strip_offsets = av_mallocz(sizeof(*strip_offsets) * strips);
00331
00332 bytes_per_row = (((s->width - 1)/s->subsampling[0] + 1) * s->bpp
00333 * s->subsampling[0] * s->subsampling[1] + 7) >> 3;
00334 if (is_yuv){
00335 yuv_line = av_malloc(bytes_per_row);
00336 if (yuv_line == NULL){
00337 av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
00338 goto fail;
00339 }
00340 }
00341
00342 #if CONFIG_ZLIB
00343 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
00344 uint8_t *zbuf;
00345 int zlen, zn;
00346 int j;
00347
00348 zlen = bytes_per_row * s->rps;
00349 zbuf = av_malloc(zlen);
00350 strip_offsets[0] = ptr - buf;
00351 zn = 0;
00352 for (j = 0; j < s->rps; j++) {
00353 if (is_yuv){
00354 pack_yuv(s, yuv_line, j);
00355 memcpy(zbuf + zn, yuv_line, bytes_per_row);
00356 j += s->subsampling[1] - 1;
00357 }
00358 else
00359 memcpy(zbuf + j * bytes_per_row,
00360 p->data[0] + j * p->linesize[0], bytes_per_row);
00361 zn += bytes_per_row;
00362 }
00363 n = encode_strip(s, zbuf, ptr, zn, s->compr);
00364 av_free(zbuf);
00365 if (n<0) {
00366 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
00367 goto fail;
00368 }
00369 ptr += n;
00370 strip_sizes[0] = ptr - buf - strip_offsets[0];
00371 } else
00372 #endif
00373 {
00374 if(s->compr == TIFF_LZW)
00375 s->lzws = av_malloc(ff_lzw_encode_state_size);
00376 for (i = 0; i < s->height; i++) {
00377 if (strip_sizes[i / s->rps] == 0) {
00378 if(s->compr == TIFF_LZW){
00379 ff_lzw_encode_init(s->lzws, ptr, s->buf_size - (*s->buf - s->buf_start),
00380 12, FF_LZW_TIFF, put_bits);
00381 }
00382 strip_offsets[i / s->rps] = ptr - buf;
00383 }
00384 if (is_yuv){
00385 pack_yuv(s, yuv_line, i);
00386 n = encode_strip(s, yuv_line, ptr, bytes_per_row, s->compr);
00387 i += s->subsampling[1] - 1;
00388 }
00389 else
00390 n = encode_strip(s, p->data[0] + i * p->linesize[0],
00391 ptr, bytes_per_row, s->compr);
00392 if (n < 0) {
00393 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
00394 goto fail;
00395 }
00396 strip_sizes[i / s->rps] += n;
00397 ptr += n;
00398 if(s->compr == TIFF_LZW && (i==s->height-1 || i%s->rps == s->rps-1)){
00399 int ret;
00400 ret = ff_lzw_encode_flush(s->lzws, flush_put_bits);
00401 strip_sizes[(i / s->rps )] += ret ;
00402 ptr += ret;
00403 }
00404 }
00405 if(s->compr == TIFF_LZW)
00406 av_free(s->lzws);
00407 }
00408
00409 s->num_entries = 0;
00410
00411 add_entry1(s,TIFF_SUBFILE, TIFF_LONG, 0);
00412 add_entry1(s,TIFF_WIDTH, TIFF_LONG, s->width);
00413 add_entry1(s,TIFF_HEIGHT, TIFF_LONG, s->height);
00414
00415 if (s->bpp_tab_size)
00416 add_entry(s, TIFF_BPP, TIFF_SHORT, s->bpp_tab_size, bpp_tab);
00417
00418 add_entry1(s,TIFF_COMPR, TIFF_SHORT, s->compr);
00419 add_entry1(s,TIFF_INVERT, TIFF_SHORT, s->photometric_interpretation);
00420 add_entry(s, TIFF_STRIP_OFFS, TIFF_LONG, strips, strip_offsets);
00421
00422 if (s->bpp_tab_size)
00423 add_entry1(s,TIFF_SAMPLES_PER_PIXEL, TIFF_SHORT, s->bpp_tab_size);
00424
00425 add_entry1(s,TIFF_ROWSPERSTRIP, TIFF_LONG, s->rps);
00426 add_entry(s, TIFF_STRIP_SIZE, TIFF_LONG, strips, strip_sizes);
00427 add_entry(s, TIFF_XRES, TIFF_RATIONAL, 1, res);
00428 add_entry(s, TIFF_YRES, TIFF_RATIONAL, 1, res);
00429 add_entry1(s,TIFF_RES_UNIT, TIFF_SHORT, 2);
00430
00431 if(!(avctx->flags & CODEC_FLAG_BITEXACT))
00432 add_entry(s, TIFF_SOFTWARE_NAME, TIFF_STRING,
00433 strlen(LIBAVCODEC_IDENT) + 1, LIBAVCODEC_IDENT);
00434
00435 if (avctx->pix_fmt == PIX_FMT_PAL8) {
00436 uint16_t pal[256 * 3];
00437 for (i = 0; i < 256; i++) {
00438 uint32_t rgb = *(uint32_t *) (p->data[1] + i * 4);
00439 pal[i] = ((rgb >> 16) & 0xff) * 257;
00440 pal[i + 256] = ((rgb >> 8 ) & 0xff) * 257;
00441 pal[i + 512] = ( rgb & 0xff) * 257;
00442 }
00443 add_entry(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal);
00444 }
00445 if (is_yuv){
00447 uint32_t refbw[12] = {15, 1, 235, 1, 128, 1, 240, 1, 128, 1, 240, 1};
00448 add_entry(s, TIFF_YCBCR_SUBSAMPLING, TIFF_SHORT, 2, s->subsampling);
00449 add_entry(s, TIFF_REFERENCE_BW, TIFF_RATIONAL, 6, refbw);
00450 }
00451 bytestream_put_le32(&offset, ptr - buf);
00452
00453 if (check_size(s, 6 + s->num_entries * 12))
00454 goto fail;
00455 bytestream_put_le16(&ptr, s->num_entries);
00456 bytestream_put_buffer(&ptr, s->entries, s->num_entries * 12);
00457 bytestream_put_le32(&ptr, 0);
00458
00459 ret = ptr - buf;
00460
00461 fail:
00462 av_free(strip_sizes);
00463 av_free(strip_offsets);
00464 av_free(yuv_line);
00465 return ret;
00466 }
00467
00468 #define OFFSET(x) offsetof(TiffEncoderContext, x)
00469 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
00470 static const AVOption options[] = {
00471 {"dpi", "set the image resolution (in dpi)", OFFSET(dpi), AV_OPT_TYPE_INT, {.dbl = 72}, 1, 0x10000, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_ENCODING_PARAM},
00472 { "compression_algo", NULL, OFFSET(compr), AV_OPT_TYPE_INT, {TIFF_PACKBITS}, TIFF_RAW, TIFF_DEFLATE, VE, "compression_algo" },
00473 { "packbits", NULL, 0, AV_OPT_TYPE_CONST, {TIFF_PACKBITS}, 0, 0, VE, "compression_algo" },
00474 { "raw", NULL, 0, AV_OPT_TYPE_CONST, {TIFF_RAW}, 0, 0, VE, "compression_algo" },
00475 { "lzw", NULL, 0, AV_OPT_TYPE_CONST, {TIFF_LZW}, 0, 0, VE, "compression_algo" },
00476 #if CONFIG_ZLIB
00477 { "deflate", NULL, 0, AV_OPT_TYPE_CONST, {TIFF_DEFLATE}, 0, 0, VE, "compression_algo" },
00478 #endif
00479 { NULL },
00480 };
00481
00482 static const AVClass tiffenc_class = {
00483 .class_name = "TIFF encoder",
00484 .item_name = av_default_item_name,
00485 .option = options,
00486 .version = LIBAVUTIL_VERSION_INT,
00487 };
00488
00489 AVCodec ff_tiff_encoder = {
00490 .name = "tiff",
00491 .type = AVMEDIA_TYPE_VIDEO,
00492 .id = CODEC_ID_TIFF,
00493 .priv_data_size = sizeof(TiffEncoderContext),
00494 .encode = encode_frame,
00495 .pix_fmts =
00496 (const enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_PAL8, PIX_FMT_GRAY8,
00497 PIX_FMT_MONOBLACK, PIX_FMT_MONOWHITE,
00498 PIX_FMT_YUV420P, PIX_FMT_YUV422P,
00499 PIX_FMT_YUV444P, PIX_FMT_YUV410P,
00500 PIX_FMT_YUV411P, PIX_FMT_RGB48LE,
00501 PIX_FMT_RGBA, PIX_FMT_NONE},
00502 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
00503 .priv_class = &tiffenc_class,
00504 };