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