00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "libavutil/imgutils.h"
00029 #include "libavutil/log.h"
00030 #include "libavutil/opt.h"
00031 #include "libavutil/pixdesc.h"
00032
00033 #include "avcodec.h"
00034 #include "config.h"
00035 #if CONFIG_ZLIB
00036 #include <zlib.h>
00037 #endif
00038 #include "bytestream.h"
00039 #include "internal.h"
00040 #include "tiff.h"
00041 #include "rle.h"
00042 #include "lzw.h"
00043 #include "put_bits.h"
00044
00045 #define TIFF_MAX_ENTRY 32
00046
00048 static const uint8_t type_sizes2[14] = {
00049 0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8, 4
00050 };
00051
00052 typedef struct TiffEncoderContext {
00053 AVClass *class;
00054 AVCodecContext *avctx;
00055 AVFrame picture;
00056
00057 int width;
00058 int height;
00059 unsigned int bpp;
00060 int compr;
00061 int bpp_tab_size;
00062 int photometric_interpretation;
00063 int strips;
00064 uint32_t *strip_sizes;
00065 unsigned int strip_sizes_size;
00066 uint32_t *strip_offsets;
00067 unsigned int strip_offsets_size;
00068 uint8_t *yuv_line;
00069 unsigned int yuv_line_size;
00070 int rps;
00071 uint8_t entries[TIFF_MAX_ENTRY*12];
00072 int num_entries;
00073 uint8_t **buf;
00074 uint8_t *buf_start;
00075 int buf_size;
00076 uint16_t subsampling[2];
00077 struct LZWEncodeState *lzws;
00078 uint32_t dpi;
00079 } TiffEncoderContext;
00080
00081
00089 static inline int check_size(TiffEncoderContext * s, uint64_t need)
00090 {
00091 if (s->buf_size < *s->buf - s->buf_start + need) {
00092 *s->buf = s->buf_start + s->buf_size + 1;
00093 av_log(s->avctx, AV_LOG_ERROR, "Buffer is too small\n");
00094 return 1;
00095 }
00096 return 0;
00097 }
00098
00108 static void tnput(uint8_t ** p, int n, const uint8_t * val, enum TiffTypes type,
00109 int flip)
00110 {
00111 int i;
00112 #if HAVE_BIGENDIAN
00113 flip ^= ((int[]) {0, 0, 0, 1, 3, 3})[type];
00114 #endif
00115 for (i = 0; i < n * type_sizes2[type]; i++)
00116 *(*p)++ = val[i ^ flip];
00117 }
00118
00128 static void add_entry(TiffEncoderContext * s,
00129 enum TiffTags tag, enum TiffTypes type, int count,
00130 const void *ptr_val)
00131 {
00132 uint8_t *entries_ptr = s->entries + 12 * s->num_entries;
00133
00134 av_assert0(s->num_entries < TIFF_MAX_ENTRY);
00135
00136 bytestream_put_le16(&entries_ptr, tag);
00137 bytestream_put_le16(&entries_ptr, type);
00138 bytestream_put_le32(&entries_ptr, count);
00139
00140 if (type_sizes[type] * (int64_t)count <= 4) {
00141 tnput(&entries_ptr, count, ptr_val, type, 0);
00142 } else {
00143 bytestream_put_le32(&entries_ptr, *s->buf - s->buf_start);
00144 check_size(s, count * (int64_t)type_sizes2[type]);
00145 tnput(s->buf, count, ptr_val, type, 0);
00146 }
00147
00148 s->num_entries++;
00149 }
00150
00151 static void add_entry1(TiffEncoderContext * s,
00152 enum TiffTags tag, enum TiffTypes type, int val){
00153 uint16_t w = val;
00154 uint32_t dw= val;
00155 add_entry(s, tag, type, 1, type == TIFF_SHORT ? (void *)&w : (void *)&dw);
00156 }
00157
00168 static int encode_strip(TiffEncoderContext * s, const int8_t * src,
00169 uint8_t * dst, int n, int compr)
00170 {
00171
00172 switch (compr) {
00173 #if CONFIG_ZLIB
00174 case TIFF_DEFLATE:
00175 case TIFF_ADOBE_DEFLATE:
00176 {
00177 unsigned long zlen = s->buf_size - (*s->buf - s->buf_start);
00178 if (compress(dst, &zlen, src, n) != Z_OK) {
00179 av_log(s->avctx, AV_LOG_ERROR, "Compressing failed\n");
00180 return -1;
00181 }
00182 return zlen;
00183 }
00184 #endif
00185 case TIFF_RAW:
00186 if (check_size(s, n))
00187 return -1;
00188 memcpy(dst, src, n);
00189 return n;
00190 case TIFF_PACKBITS:
00191 return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start), src, 1, n, 2, 0xff, -1, 0);
00192 case TIFF_LZW:
00193 return ff_lzw_encode(s->lzws, src, n);
00194 default:
00195 return -1;
00196 }
00197 }
00198
00199 static void pack_yuv(TiffEncoderContext * s, uint8_t * dst, int lnum)
00200 {
00201 AVFrame *p = &s->picture;
00202 int i, j, k;
00203 int w = (s->width - 1) / s->subsampling[0] + 1;
00204 uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
00205 uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
00206 if(s->width % s->subsampling[0] || s->height % s->subsampling[1]){
00207 for (i = 0; i < w; i++){
00208 for (j = 0; j < s->subsampling[1]; j++)
00209 for (k = 0; k < s->subsampling[0]; k++)
00210 *dst++ = p->data[0][FFMIN(lnum + j, s->height-1) * p->linesize[0] +
00211 FFMIN(i * s->subsampling[0] + k, s->width-1)];
00212 *dst++ = *pu++;
00213 *dst++ = *pv++;
00214 }
00215 }else{
00216 for (i = 0; i < w; i++){
00217 for (j = 0; j < s->subsampling[1]; j++)
00218 for (k = 0; k < s->subsampling[0]; k++)
00219 *dst++ = p->data[0][(lnum + j) * p->linesize[0] +
00220 i * s->subsampling[0] + k];
00221 *dst++ = *pu++;
00222 *dst++ = *pv++;
00223 }
00224 }
00225 }
00226
00227 static av_cold int encode_init(AVCodecContext *avctx)
00228 {
00229 TiffEncoderContext *s = avctx->priv_data;
00230
00231 avctx->coded_frame= &s->picture;
00232 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
00233 avctx->coded_frame->key_frame = 1;
00234 s->avctx = avctx;
00235
00236 return 0;
00237 }
00238
00239 static int encode_frame(AVCodecContext * avctx, AVPacket *pkt,
00240 const AVFrame *pict, int *got_packet)
00241 {
00242 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
00243 TiffEncoderContext *s = avctx->priv_data;
00244 AVFrame *const p = &s->picture;
00245 int i;
00246 uint8_t *ptr;
00247 uint8_t *offset;
00248 uint32_t strips;
00249 int bytes_per_row;
00250 uint32_t res[2] = { s->dpi, 1 };
00251 uint16_t bpp_tab[4];
00252 int ret = -1;
00253 int is_yuv = 0, alpha = 0;
00254 int shift_h, shift_v;
00255
00256 *p = *pict;
00257
00258 s->width = avctx->width;
00259 s->height = avctx->height;
00260 s->subsampling[0] = 1;
00261 s->subsampling[1] = 1;
00262
00263 avctx->bits_per_coded_sample =
00264 s->bpp = av_get_bits_per_pixel(desc);
00265 s->bpp_tab_size = desc->nb_components;
00266
00267 switch (avctx->pix_fmt) {
00268 case AV_PIX_FMT_RGBA64LE:
00269 case AV_PIX_FMT_RGBA:
00270 alpha = 1;
00271 case AV_PIX_FMT_RGB48LE:
00272 case AV_PIX_FMT_RGB24:
00273 s->photometric_interpretation = 2;
00274 break;
00275 case AV_PIX_FMT_GRAY8:
00276 avctx->bits_per_coded_sample = 0x28;
00277 case AV_PIX_FMT_GRAY8A:
00278 alpha = avctx->pix_fmt == AV_PIX_FMT_GRAY8A;
00279 case AV_PIX_FMT_GRAY16LE:
00280 case AV_PIX_FMT_MONOBLACK:
00281 s->photometric_interpretation = 1;
00282 break;
00283 case AV_PIX_FMT_PAL8:
00284 s->photometric_interpretation = 3;
00285 break;
00286 case AV_PIX_FMT_MONOWHITE:
00287 s->photometric_interpretation = 0;
00288 break;
00289 case AV_PIX_FMT_YUV420P:
00290 case AV_PIX_FMT_YUV422P:
00291 case AV_PIX_FMT_YUV440P:
00292 case AV_PIX_FMT_YUV444P:
00293 case AV_PIX_FMT_YUV410P:
00294 case AV_PIX_FMT_YUV411P:
00295 s->photometric_interpretation = 6;
00296 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &shift_h, &shift_v);
00297 s->subsampling[0] = 1 << shift_h;
00298 s->subsampling[1] = 1 << shift_v;
00299 is_yuv = 1;
00300 break;
00301 default:
00302 av_log(s->avctx, AV_LOG_ERROR,
00303 "This colors format is not supported\n");
00304 return -1;
00305 }
00306
00307 for (i = 0; i < s->bpp_tab_size; i++)
00308 bpp_tab[i] = desc->comp[i].depth_minus1 + 1;
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 ((ret = ff_alloc_packet2(avctx, pkt, avctx->width * avctx->height * s->bpp * 2 +
00320 avctx->height * 4 + FF_MIN_BUFFER_SIZE)) < 0)
00321 return ret;
00322 ptr = pkt->data;
00323 s->buf_start = pkt->data;
00324 s->buf = &ptr;
00325 s->buf_size = pkt->size;
00326
00327 if (check_size(s, 8))
00328 goto fail;
00329
00330
00331 bytestream_put_le16(&ptr, 0x4949);
00332 bytestream_put_le16(&ptr, 42);
00333
00334 offset = ptr;
00335 bytestream_put_le32(&ptr, 0);
00336
00337 av_fast_padded_mallocz(&s->strip_sizes, &s->strip_sizes_size, sizeof(s->strip_sizes[0]) * strips);
00338 av_fast_padded_mallocz(&s->strip_offsets, &s->strip_offsets_size, sizeof(s->strip_offsets[0]) * strips);
00339
00340 if (!s->strip_sizes || !s->strip_offsets) {
00341 ret = AVERROR(ENOMEM);
00342 goto fail;
00343 }
00344
00345 bytes_per_row = (((s->width - 1)/s->subsampling[0] + 1) * s->bpp
00346 * s->subsampling[0] * s->subsampling[1] + 7) >> 3;
00347 if (is_yuv){
00348 av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, bytes_per_row);
00349 if (s->yuv_line == NULL){
00350 av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
00351 ret = AVERROR(ENOMEM);
00352 goto fail;
00353 }
00354 }
00355
00356 #if CONFIG_ZLIB
00357 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
00358 uint8_t *zbuf;
00359 int zlen, zn;
00360 int j;
00361
00362 zlen = bytes_per_row * s->rps;
00363 zbuf = av_malloc(zlen);
00364 if (!zbuf) {
00365 ret = AVERROR(ENOMEM);
00366 goto fail;
00367 }
00368 s->strip_offsets[0] = ptr - pkt->data;
00369 zn = 0;
00370 for (j = 0; j < s->rps; j++) {
00371 if (is_yuv){
00372 pack_yuv(s, s->yuv_line, j);
00373 memcpy(zbuf + zn, s->yuv_line, bytes_per_row);
00374 j += s->subsampling[1] - 1;
00375 }
00376 else
00377 memcpy(zbuf + j * bytes_per_row,
00378 p->data[0] + j * p->linesize[0], bytes_per_row);
00379 zn += bytes_per_row;
00380 }
00381 ret = encode_strip(s, zbuf, ptr, zn, s->compr);
00382 av_free(zbuf);
00383 if (ret < 0) {
00384 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
00385 goto fail;
00386 }
00387 ptr += ret;
00388 s->strip_sizes[0] = ptr - pkt->data - s->strip_offsets[0];
00389 } else
00390 #endif
00391 {
00392 if (s->compr == TIFF_LZW) {
00393 s->lzws = av_malloc(ff_lzw_encode_state_size);
00394 if (!s->lzws) {
00395 ret = AVERROR(ENOMEM);
00396 goto fail;
00397 }
00398 }
00399 for (i = 0; i < s->height; i++) {
00400 if (s->strip_sizes[i / s->rps] == 0) {
00401 if(s->compr == TIFF_LZW){
00402 ff_lzw_encode_init(s->lzws, ptr, s->buf_size - (*s->buf - s->buf_start),
00403 12, FF_LZW_TIFF, put_bits);
00404 }
00405 s->strip_offsets[i / s->rps] = ptr - pkt->data;
00406 }
00407 if (is_yuv){
00408 pack_yuv(s, s->yuv_line, i);
00409 ret = encode_strip(s, s->yuv_line, ptr, bytes_per_row, s->compr);
00410 i += s->subsampling[1] - 1;
00411 }
00412 else
00413 ret = encode_strip(s, p->data[0] + i * p->linesize[0],
00414 ptr, bytes_per_row, s->compr);
00415 if (ret < 0) {
00416 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
00417 goto fail;
00418 }
00419 s->strip_sizes[i / s->rps] += ret;
00420 ptr += ret;
00421 if(s->compr == TIFF_LZW && (i==s->height-1 || i%s->rps == s->rps-1)){
00422 ret = ff_lzw_encode_flush(s->lzws, flush_put_bits);
00423 s->strip_sizes[(i / s->rps )] += ret ;
00424 ptr += ret;
00425 }
00426 }
00427 if(s->compr == TIFF_LZW)
00428 av_free(s->lzws);
00429 }
00430
00431 s->num_entries = 0;
00432
00433 add_entry1(s,TIFF_SUBFILE, TIFF_LONG, 0);
00434 add_entry1(s,TIFF_WIDTH, TIFF_LONG, s->width);
00435 add_entry1(s,TIFF_HEIGHT, TIFF_LONG, s->height);
00436
00437 if (s->bpp_tab_size)
00438 add_entry(s, TIFF_BPP, TIFF_SHORT, s->bpp_tab_size, bpp_tab);
00439
00440 add_entry1(s,TIFF_COMPR, TIFF_SHORT, s->compr);
00441 add_entry1(s,TIFF_INVERT, TIFF_SHORT, s->photometric_interpretation);
00442 add_entry(s, TIFF_STRIP_OFFS, TIFF_LONG, strips, s->strip_offsets);
00443
00444 if (s->bpp_tab_size)
00445 add_entry1(s,TIFF_SAMPLES_PER_PIXEL, TIFF_SHORT, s->bpp_tab_size);
00446
00447 add_entry1(s,TIFF_ROWSPERSTRIP, TIFF_LONG, s->rps);
00448 add_entry(s, TIFF_STRIP_SIZE, TIFF_LONG, strips, s->strip_sizes);
00449 add_entry(s, TIFF_XRES, TIFF_RATIONAL, 1, res);
00450 add_entry(s, TIFF_YRES, TIFF_RATIONAL, 1, res);
00451 add_entry1(s,TIFF_RES_UNIT, TIFF_SHORT, 2);
00452
00453 if(!(avctx->flags & CODEC_FLAG_BITEXACT))
00454 add_entry(s, TIFF_SOFTWARE_NAME, TIFF_STRING,
00455 strlen(LIBAVCODEC_IDENT) + 1, LIBAVCODEC_IDENT);
00456
00457 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
00458 uint16_t pal[256 * 3];
00459 for (i = 0; i < 256; i++) {
00460 uint32_t rgb = *(uint32_t *) (p->data[1] + i * 4);
00461 pal[i] = ((rgb >> 16) & 0xff) * 257;
00462 pal[i + 256] = ((rgb >> 8 ) & 0xff) * 257;
00463 pal[i + 512] = ( rgb & 0xff) * 257;
00464 }
00465 add_entry(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal);
00466 }
00467 if (alpha)
00468 add_entry1(s,TIFF_EXTRASAMPLES, TIFF_SHORT, 2);
00469 if (is_yuv){
00471 uint32_t refbw[12] = {15, 1, 235, 1, 128, 1, 240, 1, 128, 1, 240, 1};
00472 add_entry(s, TIFF_YCBCR_SUBSAMPLING, TIFF_SHORT, 2, s->subsampling);
00473 if (avctx->chroma_sample_location == AVCHROMA_LOC_TOPLEFT)
00474 add_entry1(s, TIFF_YCBCR_POSITIONING, TIFF_SHORT, 2);
00475 add_entry(s, TIFF_REFERENCE_BW, TIFF_RATIONAL, 6, refbw);
00476 }
00477 bytestream_put_le32(&offset, ptr - pkt->data);
00478
00479 if (check_size(s, 6 + s->num_entries * 12)) {
00480 ret = AVERROR(EINVAL);
00481 goto fail;
00482 }
00483 bytestream_put_le16(&ptr, s->num_entries);
00484 bytestream_put_buffer(&ptr, s->entries, s->num_entries * 12);
00485 bytestream_put_le32(&ptr, 0);
00486
00487 pkt->size = ptr - pkt->data;
00488 pkt->flags |= AV_PKT_FLAG_KEY;
00489 *got_packet = 1;
00490
00491 fail:
00492 return ret < 0 ? ret : 0;
00493 }
00494
00495 static av_cold int encode_close(AVCodecContext *avctx)
00496 {
00497 TiffEncoderContext *s = avctx->priv_data;
00498
00499 av_freep(&s->strip_sizes);
00500 av_freep(&s->strip_offsets);
00501 av_freep(&s->yuv_line);
00502
00503 return 0;
00504 }
00505
00506 #define OFFSET(x) offsetof(TiffEncoderContext, x)
00507 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
00508 static const AVOption options[] = {
00509 {"dpi", "set the image resolution (in dpi)", OFFSET(dpi), AV_OPT_TYPE_INT, {.i64 = 72}, 1, 0x10000, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_ENCODING_PARAM},
00510 { "compression_algo", NULL, OFFSET(compr), AV_OPT_TYPE_INT, {.i64 = TIFF_PACKBITS}, TIFF_RAW, TIFF_DEFLATE, VE, "compression_algo" },
00511 { "packbits", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = TIFF_PACKBITS}, 0, 0, VE, "compression_algo" },
00512 { "raw", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = TIFF_RAW}, 0, 0, VE, "compression_algo" },
00513 { "lzw", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = TIFF_LZW}, 0, 0, VE, "compression_algo" },
00514 #if CONFIG_ZLIB
00515 { "deflate", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = TIFF_DEFLATE}, 0, 0, VE, "compression_algo" },
00516 #endif
00517 { NULL },
00518 };
00519
00520 static const AVClass tiffenc_class = {
00521 .class_name = "TIFF encoder",
00522 .item_name = av_default_item_name,
00523 .option = options,
00524 .version = LIBAVUTIL_VERSION_INT,
00525 };
00526
00527 AVCodec ff_tiff_encoder = {
00528 .name = "tiff",
00529 .type = AVMEDIA_TYPE_VIDEO,
00530 .id = AV_CODEC_ID_TIFF,
00531 .priv_data_size = sizeof(TiffEncoderContext),
00532 .init = encode_init,
00533 .encode2 = encode_frame,
00534 .close = encode_close,
00535 .pix_fmts = (const enum AVPixelFormat[]) {
00536 AV_PIX_FMT_RGB24, AV_PIX_FMT_PAL8, AV_PIX_FMT_GRAY8,
00537 AV_PIX_FMT_GRAY8A, AV_PIX_FMT_GRAY16LE,
00538 AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_MONOWHITE,
00539 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
00540 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_RGB48LE,
00541 AV_PIX_FMT_RGBA, AV_PIX_FMT_RGBA64LE,
00542 AV_PIX_FMT_NONE
00543 },
00544 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
00545 .priv_class = &tiffenc_class,
00546 };