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