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
00032 #include "avcodec.h"
00033 #include "config.h"
00034 #if CONFIG_ZLIB
00035 #include <zlib.h>
00036 #endif
00037 #include "bytestream.h"
00038 #include "internal.h"
00039 #include "tiff.h"
00040 #include "rle.h"
00041 #include "lzw.h"
00042 #include "put_bits.h"
00043
00044 #define TIFF_MAX_ENTRY 32
00045
00047 static const uint8_t type_sizes2[14] = {
00048 0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8, 4
00049 };
00050
00051 typedef struct TiffEncoderContext {
00052 AVClass *class;
00053 AVCodecContext *avctx;
00054 AVFrame picture;
00055
00056 int width;
00057 int height;
00058 unsigned int bpp;
00059 int compr;
00060 int bpp_tab_size;
00061 int photometric_interpretation;
00062 int strips;
00063 uint32_t *strip_sizes;
00064 unsigned int strip_sizes_size;
00065 uint32_t *strip_offsets;
00066 unsigned int strip_offsets_size;
00067 uint8_t *yuv_line;
00068 unsigned int yuv_line_size;
00069 int rps;
00070 uint8_t entries[TIFF_MAX_ENTRY*12];
00071 int num_entries;
00072 uint8_t **buf;
00073 uint8_t *buf_start;
00074 int buf_size;
00075 uint16_t subsampling[2];
00076 struct LZWEncodeState *lzws;
00077 uint32_t dpi;
00078 } TiffEncoderContext;
00079
00080
00088 static inline int check_size(TiffEncoderContext * s, uint64_t need)
00089 {
00090 if (s->buf_size < *s->buf - s->buf_start + need) {
00091 *s->buf = s->buf_start + s->buf_size + 1;
00092 av_log(s->avctx, AV_LOG_ERROR, "Buffer is too small\n");
00093 return 1;
00094 }
00095 return 0;
00096 }
00097
00107 static void tnput(uint8_t ** p, int n, const uint8_t * val, enum TiffTypes type,
00108 int flip)
00109 {
00110 int i;
00111 #if HAVE_BIGENDIAN
00112 flip ^= ((int[]) {0, 0, 0, 1, 3, 3})[type];
00113 #endif
00114 for (i = 0; i < n * type_sizes2[type]; i++)
00115 *(*p)++ = val[i ^ flip];
00116 }
00117
00127 static void add_entry(TiffEncoderContext * s,
00128 enum TiffTags tag, enum TiffTypes type, int count,
00129 const void *ptr_val)
00130 {
00131 uint8_t *entries_ptr = s->entries + 12 * s->num_entries;
00132
00133 av_assert0(s->num_entries < TIFF_MAX_ENTRY);
00134
00135 bytestream_put_le16(&entries_ptr, tag);
00136 bytestream_put_le16(&entries_ptr, type);
00137 bytestream_put_le32(&entries_ptr, count);
00138
00139 if (type_sizes[type] * count <= 4) {
00140 tnput(&entries_ptr, count, ptr_val, type, 0);
00141 } else {
00142 bytestream_put_le32(&entries_ptr, *s->buf - s->buf_start);
00143 check_size(s, count * type_sizes2[type]);
00144 tnput(s->buf, count, ptr_val, type, 0);
00145 }
00146
00147 s->num_entries++;
00148 }
00149
00150 static void add_entry1(TiffEncoderContext * s,
00151 enum TiffTags tag, enum TiffTypes type, int val){
00152 uint16_t w = val;
00153 uint32_t dw= val;
00154 add_entry(s, tag, type, 1, type == TIFF_SHORT ? (void *)&w : (void *)&dw);
00155 }
00156
00167 static int encode_strip(TiffEncoderContext * s, const int8_t * src,
00168 uint8_t * dst, int n, int compr)
00169 {
00170
00171 switch (compr) {
00172 #if CONFIG_ZLIB
00173 case TIFF_DEFLATE:
00174 case TIFF_ADOBE_DEFLATE:
00175 {
00176 unsigned long zlen = s->buf_size - (*s->buf - s->buf_start);
00177 if (compress(dst, &zlen, src, n) != Z_OK) {
00178 av_log(s->avctx, AV_LOG_ERROR, "Compressing failed\n");
00179 return -1;
00180 }
00181 return zlen;
00182 }
00183 #endif
00184 case TIFF_RAW:
00185 if (check_size(s, n))
00186 return -1;
00187 memcpy(dst, src, n);
00188 return n;
00189 case TIFF_PACKBITS:
00190 return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start), src, 1, n, 2, 0xff, -1, 0);
00191 case TIFF_LZW:
00192 return ff_lzw_encode(s->lzws, src, n);
00193 default:
00194 return -1;
00195 }
00196 }
00197
00198 static void pack_yuv(TiffEncoderContext * s, uint8_t * dst, int lnum)
00199 {
00200 AVFrame *p = &s->picture;
00201 int i, j, k;
00202 int w = (s->width - 1) / s->subsampling[0] + 1;
00203 uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
00204 uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
00205 if(s->width % s->subsampling[0] || s->height % s->subsampling[1]){
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][FFMIN(lnum + j, s->height-1) * p->linesize[0] +
00210 FFMIN(i * s->subsampling[0] + k, s->width-1)];
00211 *dst++ = *pu++;
00212 *dst++ = *pv++;
00213 }
00214 }else{
00215 for (i = 0; i < w; i++){
00216 for (j = 0; j < s->subsampling[1]; j++)
00217 for (k = 0; k < s->subsampling[0]; k++)
00218 *dst++ = p->data[0][(lnum + j) * p->linesize[0] +
00219 i * s->subsampling[0] + k];
00220 *dst++ = *pu++;
00221 *dst++ = *pv++;
00222 }
00223 }
00224 }
00225
00226 static av_cold int encode_init(AVCodecContext *avctx)
00227 {
00228 TiffEncoderContext *s = avctx->priv_data;
00229
00230 avctx->coded_frame= &s->picture;
00231 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
00232 avctx->coded_frame->key_frame = 1;
00233 s->avctx = avctx;
00234
00235 return 0;
00236 }
00237
00238 static int encode_frame(AVCodecContext * avctx, AVPacket *pkt,
00239 const AVFrame *pict, int *got_packet)
00240 {
00241 TiffEncoderContext *s = avctx->priv_data;
00242 AVFrame *const p = &s->picture;
00243 int i;
00244 uint8_t *ptr;
00245 uint8_t *offset;
00246 uint32_t strips;
00247 int bytes_per_row;
00248 uint32_t res[2] = { s->dpi, 1 };
00249 uint16_t bpp_tab[4];
00250 int ret = -1;
00251 int is_yuv = 0, alpha = 0;
00252 int shift_h, shift_v;
00253
00254 *p = *pict;
00255
00256 s->width = avctx->width;
00257 s->height = avctx->height;
00258 s->subsampling[0] = 1;
00259 s->subsampling[1] = 1;
00260
00261 avctx->bits_per_coded_sample =
00262 s->bpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[avctx->pix_fmt]);
00263
00264 switch (avctx->pix_fmt) {
00265 case PIX_FMT_RGBA64LE:
00266 case PIX_FMT_RGBA:
00267 alpha = 1;
00268 case PIX_FMT_RGB48LE:
00269 case PIX_FMT_RGB24:
00270 s->photometric_interpretation = 2;
00271 break;
00272 case PIX_FMT_GRAY8:
00273 avctx->bits_per_coded_sample = 0x28;
00274 case PIX_FMT_GRAY8A:
00275 alpha = avctx->pix_fmt == PIX_FMT_GRAY8A;
00276 case PIX_FMT_GRAY16LE:
00277 case PIX_FMT_MONOBLACK:
00278 s->photometric_interpretation = 1;
00279 break;
00280 case PIX_FMT_PAL8:
00281 s->photometric_interpretation = 3;
00282 break;
00283 case PIX_FMT_MONOWHITE:
00284 s->photometric_interpretation = 0;
00285 break;
00286 case PIX_FMT_YUV420P:
00287 case PIX_FMT_YUV422P:
00288 case PIX_FMT_YUV440P:
00289 case PIX_FMT_YUV444P:
00290 case PIX_FMT_YUV410P:
00291 case PIX_FMT_YUV411P:
00292 s->photometric_interpretation = 6;
00293 avcodec_get_chroma_sub_sample(avctx->pix_fmt,
00294 &shift_h, &shift_v);
00295 s->subsampling[0] = 1 << shift_h;
00296 s->subsampling[1] = 1 << shift_v;
00297 is_yuv = 1;
00298 break;
00299 default:
00300 av_log(s->avctx, AV_LOG_ERROR,
00301 "This colors format is not supported\n");
00302 return -1;
00303 }
00304
00305 s->bpp_tab_size = av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
00306 for (i = 0; i < s->bpp_tab_size; i++)
00307 bpp_tab[i] = av_pix_fmt_descriptors[avctx->pix_fmt].comp[i].depth_minus1 + 1;
00308
00309 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE || s->compr == TIFF_LZW)
00310
00311 s->rps = s->height;
00312 else
00313 s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1);
00314 s->rps = ((s->rps - 1) / s->subsampling[1] + 1) * s->subsampling[1];
00315
00316 strips = (s->height - 1) / s->rps + 1;
00317
00318 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width * avctx->height * s->bpp * 2 +
00319 avctx->height * 4 + FF_MIN_BUFFER_SIZE)) < 0)
00320 return ret;
00321 ptr = pkt->data;
00322 s->buf_start = pkt->data;
00323 s->buf = &ptr;
00324 s->buf_size = pkt->size;
00325
00326 if (check_size(s, 8))
00327 goto fail;
00328
00329
00330 bytestream_put_le16(&ptr, 0x4949);
00331 bytestream_put_le16(&ptr, 42);
00332
00333 offset = ptr;
00334 bytestream_put_le32(&ptr, 0);
00335
00336 av_fast_padded_mallocz(&s->strip_sizes, &s->strip_sizes_size, sizeof(s->strip_sizes[0]) * strips);
00337 av_fast_padded_mallocz(&s->strip_offsets, &s->strip_offsets_size, sizeof(s->strip_offsets[0]) * strips);
00338
00339 if (!s->strip_sizes || !s->strip_offsets) {
00340 ret = AVERROR(ENOMEM);
00341 goto fail;
00342 }
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 av_fast_padded_malloc(&s->yuv_line, &s->yuv_line_size, bytes_per_row);
00348 if (s->yuv_line == NULL){
00349 ret = AVERROR(ENOMEM);
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 s->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, s->yuv_line, j);
00367 memcpy(zbuf + zn, s->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 s->strip_sizes[0] = ptr - pkt->data - s->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 (s->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 s->strip_offsets[i / s->rps] = ptr - pkt->data;
00395 }
00396 if (is_yuv){
00397 pack_yuv(s, s->yuv_line, i);
00398 ret = encode_strip(s, 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 s->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 s->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, s->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, s->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 (alpha)
00457 add_entry1(s,TIFF_EXTRASAMPLES, TIFF_SHORT, 2);
00458 if (is_yuv){
00460 uint32_t refbw[12] = {15, 1, 235, 1, 128, 1, 240, 1, 128, 1, 240, 1};
00461 add_entry(s, TIFF_YCBCR_SUBSAMPLING, TIFF_SHORT, 2, s->subsampling);
00462 add_entry(s, TIFF_REFERENCE_BW, TIFF_RATIONAL, 6, refbw);
00463 }
00464 bytestream_put_le32(&offset, ptr - pkt->data);
00465
00466 if (check_size(s, 6 + s->num_entries * 12)) {
00467 ret = AVERROR(EINVAL);
00468 goto fail;
00469 }
00470 bytestream_put_le16(&ptr, s->num_entries);
00471 bytestream_put_buffer(&ptr, s->entries, s->num_entries * 12);
00472 bytestream_put_le32(&ptr, 0);
00473
00474 pkt->size = ptr - pkt->data;
00475 pkt->flags |= AV_PKT_FLAG_KEY;
00476 *got_packet = 1;
00477
00478 fail:
00479 return ret < 0 ? ret : 0;
00480 }
00481
00482 static av_cold int encode_close(AVCodecContext *avctx)
00483 {
00484 TiffEncoderContext *s = avctx->priv_data;
00485
00486 av_freep(&s->strip_sizes);
00487 av_freep(&s->strip_offsets);
00488 av_freep(&s->yuv_line);
00489
00490 return 0;
00491 }
00492
00493 #define OFFSET(x) offsetof(TiffEncoderContext, x)
00494 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
00495 static const AVOption options[] = {
00496 {"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},
00497 { "compression_algo", NULL, OFFSET(compr), AV_OPT_TYPE_INT, {.i64 = TIFF_PACKBITS}, TIFF_RAW, TIFF_DEFLATE, VE, "compression_algo" },
00498 { "packbits", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = TIFF_PACKBITS}, 0, 0, VE, "compression_algo" },
00499 { "raw", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = TIFF_RAW}, 0, 0, VE, "compression_algo" },
00500 { "lzw", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = TIFF_LZW}, 0, 0, VE, "compression_algo" },
00501 #if CONFIG_ZLIB
00502 { "deflate", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = TIFF_DEFLATE}, 0, 0, VE, "compression_algo" },
00503 #endif
00504 { NULL },
00505 };
00506
00507 static const AVClass tiffenc_class = {
00508 .class_name = "TIFF encoder",
00509 .item_name = av_default_item_name,
00510 .option = options,
00511 .version = LIBAVUTIL_VERSION_INT,
00512 };
00513
00514 AVCodec ff_tiff_encoder = {
00515 .name = "tiff",
00516 .type = AVMEDIA_TYPE_VIDEO,
00517 .id = AV_CODEC_ID_TIFF,
00518 .priv_data_size = sizeof(TiffEncoderContext),
00519 .init = encode_init,
00520 .encode2 = encode_frame,
00521 .close = encode_close,
00522 .pix_fmts = (const enum PixelFormat[]) {
00523 PIX_FMT_RGB24, PIX_FMT_PAL8, PIX_FMT_GRAY8,
00524 PIX_FMT_GRAY8A, PIX_FMT_GRAY16LE,
00525 PIX_FMT_MONOBLACK, PIX_FMT_MONOWHITE,
00526 PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV440P, PIX_FMT_YUV444P,
00527 PIX_FMT_YUV410P, PIX_FMT_YUV411P, PIX_FMT_RGB48LE,
00528 PIX_FMT_RGBA, PIX_FMT_RGBA64LE,
00529 PIX_FMT_NONE
00530 },
00531 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
00532 .priv_class = &tiffenc_class,
00533 };