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 #include "put_bits.h"
00036
00037 #define TIFF_MAX_ENTRY 32
00038
00040 static const uint8_t type_sizes2[6] = {
00041 0, 1, 1, 2, 4, 8
00042 };
00043
00044 typedef struct TiffEncoderContext {
00045 AVCodecContext *avctx;
00046 AVFrame picture;
00047
00048 int width;
00049 int height;
00050 unsigned int bpp;
00051 int compr;
00052 int bpp_tab_size;
00053 int photometric_interpretation;
00054 int strips;
00055 int rps;
00056 uint8_t entries[TIFF_MAX_ENTRY*12];
00057 int num_entries;
00058 uint8_t **buf;
00059 uint8_t *buf_start;
00060 int buf_size;
00061 uint16_t subsampling[2];
00062 struct LZWEncodeState *lzws;
00063 } TiffEncoderContext;
00064
00065
00072 inline static int check_size(TiffEncoderContext * s, uint64_t need)
00073 {
00074 if (s->buf_size < *s->buf - s->buf_start + need) {
00075 *s->buf = s->buf_start + s->buf_size + 1;
00076 av_log(s->avctx, AV_LOG_ERROR, "Buffer is too small\n");
00077 return 1;
00078 }
00079 return 0;
00080 }
00081
00091 static void tnput(uint8_t ** p, int n, const uint8_t * val, enum TiffTypes type,
00092 int flip)
00093 {
00094 int i;
00095 #if HAVE_BIGENDIAN
00096 flip ^= ((int[]) {0, 0, 0, 1, 3, 3})[type];
00097 #endif
00098 for (i = 0; i < n * type_sizes2[type]; i++)
00099 *(*p)++ = val[i ^ flip];
00100 }
00101
00110 static void add_entry(TiffEncoderContext * s,
00111 enum TiffTags tag, enum TiffTypes type, int count,
00112 const void *ptr_val)
00113 {
00114 uint8_t *entries_ptr = s->entries + 12 * s->num_entries;
00115
00116 assert(s->num_entries < TIFF_MAX_ENTRY);
00117
00118 bytestream_put_le16(&entries_ptr, tag);
00119 bytestream_put_le16(&entries_ptr, type);
00120 bytestream_put_le32(&entries_ptr, count);
00121
00122 if (type_sizes[type] * count <= 4) {
00123 tnput(&entries_ptr, count, ptr_val, type, 0);
00124 } else {
00125 bytestream_put_le32(&entries_ptr, *s->buf - s->buf_start);
00126 check_size(s, count * type_sizes2[type]);
00127 tnput(s->buf, count, ptr_val, type, 0);
00128 }
00129
00130 s->num_entries++;
00131 }
00132
00133 static void add_entry1(TiffEncoderContext * s,
00134 enum TiffTags tag, enum TiffTypes type, int val){
00135 uint16_t w = val;
00136 uint32_t dw= val;
00137 add_entry(s, tag, type, 1, type == TIFF_SHORT ? (void *)&w : (void *)&dw);
00138 }
00139
00150 static int encode_strip(TiffEncoderContext * s, const int8_t * src,
00151 uint8_t * dst, int n, int compr)
00152 {
00153
00154 switch (compr) {
00155 #if CONFIG_ZLIB
00156 case TIFF_DEFLATE:
00157 case TIFF_ADOBE_DEFLATE:
00158 {
00159 unsigned long zlen = s->buf_size - (*s->buf - s->buf_start);
00160 if (compress(dst, &zlen, src, n) != Z_OK) {
00161 av_log(s->avctx, AV_LOG_ERROR, "Compressing failed\n");
00162 return -1;
00163 }
00164 return zlen;
00165 }
00166 #endif
00167 case TIFF_RAW:
00168 if (check_size(s, n))
00169 return -1;
00170 memcpy(dst, src, n);
00171 return n;
00172 case TIFF_PACKBITS:
00173 return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start), src, 1, n, 2, 0xff, -1, 0);
00174 case TIFF_LZW:
00175 return ff_lzw_encode(s->lzws, src, n);
00176 default:
00177 return -1;
00178 }
00179 }
00180
00181 static void pack_yuv(TiffEncoderContext * s, uint8_t * dst, int lnum)
00182 {
00183 AVFrame *p = &s->picture;
00184 int i, j, k;
00185 int w = (s->width - 1) / s->subsampling[0] + 1;
00186 uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
00187 uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
00188 for (i = 0; i < w; i++){
00189 for (j = 0; j < s->subsampling[1]; j++)
00190 for (k = 0; k < s->subsampling[0]; k++)
00191 *dst++ = p->data[0][(lnum + j) * p->linesize[0] +
00192 i * s->subsampling[0] + k];
00193 *dst++ = *pu++;
00194 *dst++ = *pv++;
00195 }
00196 }
00197
00198 static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
00199 int buf_size, void *data)
00200 {
00201 TiffEncoderContext *s = avctx->priv_data;
00202 AVFrame *pict = data;
00203 AVFrame *const p = (AVFrame *) & s->picture;
00204 int i;
00205 int n;
00206 uint8_t *ptr = buf;
00207 uint8_t *offset;
00208 uint32_t strips;
00209 uint32_t *strip_sizes = NULL;
00210 uint32_t *strip_offsets = NULL;
00211 int bytes_per_row;
00212 uint32_t res[2] = { 72, 1 };
00213 static const uint16_t bpp_tab[] = { 8, 8, 8, 8 };
00214 int ret = -1;
00215 int is_yuv = 0;
00216 uint8_t *yuv_line = NULL;
00217 int shift_h, shift_v;
00218
00219 s->buf_start = buf;
00220 s->buf = &ptr;
00221 s->buf_size = buf_size;
00222
00223 *p = *pict;
00224 p->pict_type = FF_I_TYPE;
00225 p->key_frame = 1;
00226 avctx->coded_frame= &s->picture;
00227
00228 s->compr = TIFF_PACKBITS;
00229 if (avctx->compression_level == 0) {
00230 s->compr = TIFF_RAW;
00231 } else if(avctx->compression_level == 2) {
00232 s->compr = TIFF_LZW;
00233 #if CONFIG_ZLIB
00234 } else if ((avctx->compression_level >= 3)) {
00235 s->compr = TIFF_DEFLATE;
00236 #endif
00237 }
00238
00239 s->width = avctx->width;
00240 s->height = avctx->height;
00241 s->subsampling[0] = 1;
00242 s->subsampling[1] = 1;
00243
00244 switch (avctx->pix_fmt) {
00245 case PIX_FMT_RGB24:
00246 s->bpp = 24;
00247 s->photometric_interpretation = 2;
00248 break;
00249 case PIX_FMT_GRAY8:
00250 s->bpp = 8;
00251 s->photometric_interpretation = 1;
00252 break;
00253 case PIX_FMT_PAL8:
00254 s->bpp = 8;
00255 s->photometric_interpretation = 3;
00256 break;
00257 case PIX_FMT_MONOBLACK:
00258 s->bpp = 1;
00259 s->photometric_interpretation = 1;
00260 break;
00261 case PIX_FMT_MONOWHITE:
00262 s->bpp = 1;
00263 s->photometric_interpretation = 0;
00264 break;
00265 case PIX_FMT_YUV420P:
00266 case PIX_FMT_YUV422P:
00267 case PIX_FMT_YUV444P:
00268 case PIX_FMT_YUV410P:
00269 case PIX_FMT_YUV411P:
00270 s->photometric_interpretation = 6;
00271 avcodec_get_chroma_sub_sample(avctx->pix_fmt,
00272 &shift_h, &shift_v);
00273 s->bpp = 8 + (16 >> (shift_h + shift_v));
00274 s->subsampling[0] = 1 << shift_h;
00275 s->subsampling[1] = 1 << shift_v;
00276 s->bpp_tab_size = 3;
00277 is_yuv = 1;
00278 break;
00279 default:
00280 av_log(s->avctx, AV_LOG_ERROR,
00281 "This colors format is not supported\n");
00282 return -1;
00283 }
00284 if (!is_yuv)
00285 s->bpp_tab_size = (s->bpp >> 3);
00286
00287 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE || s->compr == TIFF_LZW)
00288
00289 s->rps = s->height;
00290 else
00291 s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1);
00292 s->rps = ((s->rps - 1) / s->subsampling[1] + 1) * s->subsampling[1];
00293
00294 strips = (s->height - 1) / s->rps + 1;
00295
00296 if (check_size(s, 8))
00297 goto fail;
00298
00299
00300 bytestream_put_le16(&ptr, 0x4949);
00301 bytestream_put_le16(&ptr, 42);
00302
00303 offset = ptr;
00304 bytestream_put_le32(&ptr, 0);
00305
00306 strip_sizes = av_mallocz(sizeof(*strip_sizes) * strips);
00307 strip_offsets = av_mallocz(sizeof(*strip_offsets) * strips);
00308
00309 bytes_per_row = (((s->width - 1)/s->subsampling[0] + 1) * s->bpp
00310 * s->subsampling[0] * s->subsampling[1] + 7) >> 3;
00311 if (is_yuv){
00312 yuv_line = av_malloc(bytes_per_row);
00313 if (yuv_line == NULL){
00314 av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
00315 goto fail;
00316 }
00317 }
00318
00319 #if CONFIG_ZLIB
00320 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
00321 uint8_t *zbuf;
00322 int zlen, zn;
00323 int j;
00324
00325 zlen = bytes_per_row * s->rps;
00326 zbuf = av_malloc(zlen);
00327 strip_offsets[0] = ptr - buf;
00328 zn = 0;
00329 for (j = 0; j < s->rps; j++) {
00330 if (is_yuv){
00331 pack_yuv(s, yuv_line, j);
00332 memcpy(zbuf + zn, yuv_line, bytes_per_row);
00333 j += s->subsampling[1] - 1;
00334 }
00335 else
00336 memcpy(zbuf + j * bytes_per_row,
00337 p->data[0] + j * p->linesize[0], bytes_per_row);
00338 zn += bytes_per_row;
00339 }
00340 n = encode_strip(s, zbuf, ptr, zn, s->compr);
00341 av_free(zbuf);
00342 if (n<0) {
00343 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
00344 goto fail;
00345 }
00346 ptr += n;
00347 strip_sizes[0] = ptr - buf - strip_offsets[0];
00348 } else
00349 #endif
00350 {
00351 if(s->compr == TIFF_LZW)
00352 s->lzws = av_malloc(ff_lzw_encode_state_size);
00353 for (i = 0; i < s->height; i++) {
00354 if (strip_sizes[i / s->rps] == 0) {
00355 if(s->compr == TIFF_LZW){
00356 ff_lzw_encode_init(s->lzws, ptr, s->buf_size - (*s->buf - s->buf_start),
00357 12, FF_LZW_TIFF, put_bits);
00358 }
00359 strip_offsets[i / s->rps] = ptr - buf;
00360 }
00361 if (is_yuv){
00362 pack_yuv(s, yuv_line, i);
00363 n = encode_strip(s, yuv_line, ptr, bytes_per_row, s->compr);
00364 i += s->subsampling[1] - 1;
00365 }
00366 else
00367 n = encode_strip(s, p->data[0] + i * p->linesize[0],
00368 ptr, bytes_per_row, s->compr);
00369 if (n < 0) {
00370 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
00371 goto fail;
00372 }
00373 strip_sizes[i / s->rps] += n;
00374 ptr += n;
00375 if(s->compr == TIFF_LZW && (i==s->height-1 || i%s->rps == s->rps-1)){
00376 int ret;
00377 ret = ff_lzw_encode_flush(s->lzws, flush_put_bits);
00378 strip_sizes[(i / s->rps )] += ret ;
00379 ptr += ret;
00380 }
00381 }
00382 if(s->compr == TIFF_LZW)
00383 av_free(s->lzws);
00384 }
00385
00386 s->num_entries = 0;
00387
00388 add_entry1(s,TIFF_SUBFILE, TIFF_LONG, 0);
00389 add_entry1(s,TIFF_WIDTH, TIFF_LONG, s->width);
00390 add_entry1(s,TIFF_HEIGHT, TIFF_LONG, s->height);
00391
00392 if (s->bpp_tab_size)
00393 add_entry(s, TIFF_BPP, TIFF_SHORT, s->bpp_tab_size, bpp_tab);
00394
00395 add_entry1(s,TIFF_COMPR, TIFF_SHORT, s->compr);
00396 add_entry1(s,TIFF_INVERT, TIFF_SHORT, s->photometric_interpretation);
00397 add_entry(s, TIFF_STRIP_OFFS, TIFF_LONG, strips, strip_offsets);
00398
00399 if (s->bpp_tab_size)
00400 add_entry1(s,TIFF_SAMPLES_PER_PIXEL, TIFF_SHORT, s->bpp_tab_size);
00401
00402 add_entry1(s,TIFF_ROWSPERSTRIP, TIFF_LONG, s->rps);
00403 add_entry(s, TIFF_STRIP_SIZE, TIFF_LONG, strips, strip_sizes);
00404 add_entry(s, TIFF_XRES, TIFF_RATIONAL, 1, res);
00405 add_entry(s, TIFF_YRES, TIFF_RATIONAL, 1, res);
00406 add_entry1(s,TIFF_RES_UNIT, TIFF_SHORT, 2);
00407
00408 if(!(avctx->flags & CODEC_FLAG_BITEXACT))
00409 add_entry(s, TIFF_SOFTWARE_NAME, TIFF_STRING,
00410 strlen(LIBAVCODEC_IDENT) + 1, LIBAVCODEC_IDENT);
00411
00412 if (avctx->pix_fmt == PIX_FMT_PAL8) {
00413 uint16_t pal[256 * 3];
00414 for (i = 0; i < 256; i++) {
00415 uint32_t rgb = *(uint32_t *) (p->data[1] + i * 4);
00416 pal[i] = ((rgb >> 16) & 0xff) * 257;
00417 pal[i + 256] = ((rgb >> 8 ) & 0xff) * 257;
00418 pal[i + 512] = ( rgb & 0xff) * 257;
00419 }
00420 add_entry(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal);
00421 }
00422 if (is_yuv){
00424 uint32_t refbw[12] = {15, 1, 235, 1, 128, 1, 240, 1, 128, 1, 240, 1};
00425 add_entry(s, TIFF_YCBCR_SUBSAMPLING, TIFF_SHORT, 2, s->subsampling);
00426 add_entry(s, TIFF_REFERENCE_BW, TIFF_RATIONAL, 6, refbw);
00427 }
00428 bytestream_put_le32(&offset, ptr - buf);
00429
00430 if (check_size(s, 6 + s->num_entries * 12))
00431 goto fail;
00432 bytestream_put_le16(&ptr, s->num_entries);
00433 bytestream_put_buffer(&ptr, s->entries, s->num_entries * 12);
00434 bytestream_put_le32(&ptr, 0);
00435
00436 ret = ptr - buf;
00437
00438 fail:
00439 av_free(strip_sizes);
00440 av_free(strip_offsets);
00441 av_free(yuv_line);
00442 return ret;
00443 }
00444
00445 AVCodec tiff_encoder = {
00446 "tiff",
00447 AVMEDIA_TYPE_VIDEO,
00448 CODEC_ID_TIFF,
00449 sizeof(TiffEncoderContext),
00450 NULL,
00451 encode_frame,
00452 NULL,
00453 NULL,
00454 0,
00455 NULL,
00456 .pix_fmts =
00457 (const enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_PAL8, PIX_FMT_GRAY8,
00458 PIX_FMT_MONOBLACK, PIX_FMT_MONOWHITE,
00459 PIX_FMT_YUV420P, PIX_FMT_YUV422P,
00460 PIX_FMT_YUV444P, PIX_FMT_YUV410P,
00461 PIX_FMT_YUV411P,
00462 PIX_FMT_NONE},
00463 .long_name = NULL_IF_CONFIG_SMALL("TIFF image"),
00464 };