comparison tiffenc.c @ 4799:812f759a7c59 libavcodec

TIFF-LZW encoding support by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
author michael
date Sat, 07 Apr 2007 15:15:45 +0000
parents d8b17a09a114
children 1a5c08c07cfb
comparison
equal deleted inserted replaced
4798:c68b9a261f79 4799:812f759a7c59
30 #include <zlib.h> 30 #include <zlib.h>
31 #endif 31 #endif
32 #include "bytestream.h" 32 #include "bytestream.h"
33 #include "tiff.h" 33 #include "tiff.h"
34 #include "rle.h" 34 #include "rle.h"
35 #include "lzw.h"
35 36
36 #define TIFF_MAX_ENTRY 32 37 #define TIFF_MAX_ENTRY 32
37 38
38 /** sizes of various TIFF field types (string size = 1)*/ 39 /** sizes of various TIFF field types (string size = 1)*/
39 static const uint8_t type_sizes2[6] = { 40 static const uint8_t type_sizes2[6] = {
56 int num_entries; ///< number of entires 57 int num_entries; ///< number of entires
57 uint8_t **buf; ///< actual position in buffer 58 uint8_t **buf; ///< actual position in buffer
58 uint8_t *buf_start; ///< pointer to first byte in buffer 59 uint8_t *buf_start; ///< pointer to first byte in buffer
59 int buf_size; ///< buffer size 60 int buf_size; ///< buffer size
60 uint16_t subsampling[2]; ///< YUV subsampling factors 61 uint16_t subsampling[2]; ///< YUV subsampling factors
62 struct LZWEncodeState *lzws; ///< LZW Encode state
61 } TiffEncoderContext; 63 } TiffEncoderContext;
62 64
63 65
64 /** 66 /**
65 * Check free space in buffer 67 * Check free space in buffer
167 return -1; 169 return -1;
168 memcpy(dst, src, n); 170 memcpy(dst, src, n);
169 return n; 171 return n;
170 case TIFF_PACKBITS: 172 case TIFF_PACKBITS:
171 return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start), src, 1, n, 2, 0xff, -1, 0); 173 return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start), src, 1, n, 2, 0xff, -1, 0);
174 case TIFF_LZW:
175 return ff_lzw_encode(s->lzws, src, n);
172 default: 176 default:
173 return -1; 177 return -1;
174 } 178 }
175 } 179 }
176 180
221 p->key_frame = 1; 225 p->key_frame = 1;
222 226
223 s->compr = TIFF_PACKBITS; 227 s->compr = TIFF_PACKBITS;
224 if (avctx->compression_level == 0) { 228 if (avctx->compression_level == 0) {
225 s->compr = TIFF_RAW; 229 s->compr = TIFF_RAW;
230 } else if(avctx->compression_level == 2) {
231 s->compr = TIFF_LZW;
226 #ifdef CONFIG_ZLIB 232 #ifdef CONFIG_ZLIB
227 } else if ((avctx->compression_level > 2)) { 233 } else if ((avctx->compression_level >= 3)) {
228 s->compr = TIFF_DEFLATE; 234 s->compr = TIFF_DEFLATE;
229 #endif 235 #endif
230 } 236 }
231 237
232 s->width = avctx->width; 238 s->width = avctx->width;
275 return -1; 281 return -1;
276 } 282 }
277 if (!is_yuv) 283 if (!is_yuv)
278 s->bpp_tab_size = (s->bpp >> 3); 284 s->bpp_tab_size = (s->bpp >> 3);
279 285
280 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) 286 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE || s->compr == TIFF_LZW)
281 //best choose for DEFLATE 287 //best choose for DEFLATE
282 s->rps = s->height; 288 s->rps = s->height;
283 else 289 else
284 s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1); // suggest size of strip 290 s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1); // suggest size of strip
285 s->rps = ((s->rps - 1) / s->subsampling[1] + 1) * s->subsampling[1]; // round rps up 291 s->rps = ((s->rps - 1) / s->subsampling[1] + 1) * s->subsampling[1]; // round rps up
339 ptr += n; 345 ptr += n;
340 strip_sizes[0] = ptr - buf - strip_offsets[0]; 346 strip_sizes[0] = ptr - buf - strip_offsets[0];
341 } else 347 } else
342 #endif 348 #endif
343 { 349 {
350 if(s->compr == TIFF_LZW)
351 s->lzws = av_malloc(ff_lzw_encode_state_size);
344 for (i = 0; i < s->height; i++) { 352 for (i = 0; i < s->height; i++) {
345 if (strip_sizes[i / s->rps] == 0) { 353 if (strip_sizes[i / s->rps] == 0) {
354 if(s->compr == TIFF_LZW){
355 ff_lzw_encode_init(s->lzws, ptr, s->buf_size - (*s->buf - s->buf_start), 12);
356 }
346 strip_offsets[i / s->rps] = ptr - buf; 357 strip_offsets[i / s->rps] = ptr - buf;
347 } 358 }
348 if (is_yuv){ 359 if (is_yuv){
349 pack_yuv(s, yuv_line, i); 360 pack_yuv(s, yuv_line, i);
350 n = encode_strip(s, yuv_line, ptr, bytes_per_row, s->compr); 361 n = encode_strip(s, yuv_line, ptr, bytes_per_row, s->compr);
357 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n"); 368 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
358 goto fail; 369 goto fail;
359 } 370 }
360 strip_sizes[i / s->rps] += n; 371 strip_sizes[i / s->rps] += n;
361 ptr += n; 372 ptr += n;
362 } 373 if(s->compr == TIFF_LZW && (i==s->height-1 || i%s->rps == s->rps-1)){
374 int ret;
375 ret = ff_lzw_encode_flush(s->lzws);
376 strip_sizes[(i / s->rps )] += ret ;
377 ptr += ret;
378 }
379 }
380 if(s->compr == TIFF_LZW)
381 av_free(s->lzws);
363 } 382 }
364 383
365 s->num_entries = 0; 384 s->num_entries = 0;
366 385
367 add_entry1(s,TIFF_SUBFILE, TIFF_LONG, 0); 386 add_entry1(s,TIFF_SUBFILE, TIFF_LONG, 0);