[Ffmpeg-devel] [PATCH] TIFF encoder (Google SoC qualification task)
Kamil Nowosad
k.nowosad
Sun Apr 1 17:20:57 CEST 2007
Hi
On Thu, Mar 29, 2007 at 04:51:25PM +0200, Michael Niedermayer wrote:
> i would recommand against adding LZW and JPEG compression simply because iam
> afraid that this could get tricky with googles deadlines, you can always
> submit these as seperate patches if you like after the tiff encoder passed
> review
> and additional features should be in seperate patches ideally anyway ...
Ok. I have simplified my patch.
> >
> > You have written that new files (like tiff.h) have to be diffed against
> > the parent file. Do you mean doing svn copy tiff.c tiff.h insted of svn
> > add tiff.h ?
>
> yes
done
> [...]
> > Index: libavcodec/utils.c
> > ===================================================================
> > --- libavcodec/utils.c (wersja 8542)
> > +++ libavcodec/utils.c (kopia robocza)
> > @@ -636,6 +636,9 @@
> > {"coder", NULL, OFFSET(coder_type), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX, V|E, "coder"},
> > {"vlc", "variable length coder / huffman coder", 0, FF_OPT_TYPE_CONST, FF_CODER_TYPE_VLC, INT_MIN, INT_MAX, V|E, "coder"},
> > {"ac", "arithmetic coder", 0, FF_OPT_TYPE_CONST, FF_CODER_TYPE_AC, INT_MIN, INT_MAX, V|E, "coder"},
> > +{"raw", "raw (no encoding)", 0, FF_OPT_TYPE_CONST, FF_CODER_TYPE_RAW, INT_MIN, INT_MAX, V|E, "coder"},
> > +{"rle", "run-lenghth coder", 0, FF_OPT_TYPE_CONST, FF_CODER_TYPE_RL, INT_MIN, INT_MAX, V|E, "coder"},
> > +{"def", "deflate", 0, FF_OPT_TYPE_CONST, FF_CODER_TYPE_DEFLATE, INT_MIN, INT_MAX, V|E, "coder"},
>
> i would prefer if this where called "deflate" becasue "def" could be
> missunderstood as "default"
done
> > {"context", "context model", OFFSET(context_model), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX, V|E},
> > {"slice_flags", NULL, OFFSET(slice_flags), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX},
> > {"xvmc_acceleration", NULL, OFFSET(xvmc_acceleration), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX},
> [...]
> > Index: libavcodec/avcodec.h
> > ===================================================================
> > --- libavcodec/avcodec.h (wersja 8542)
> > +++ libavcodec/avcodec.h (kopia robocza)
> > @@ -1565,8 +1565,11 @@
> > */
> > int global_quality;
> >
> > -#define FF_CODER_TYPE_VLC 0
> > -#define FF_CODER_TYPE_AC 1
> > +#define FF_CODER_TYPE_VLC 0
> > +#define FF_CODER_TYPE_AC 1
> > +#define FF_CODER_TYPE_RAW 2 // no coder
> > +#define FF_CODER_TYPE_RL 3 // run-length
>
> id prefer FF_CODER_TYPE_RLE as RLE is a well known term
done
> > +#ifdef CONFIG_ZLIB
> > +const enum TiffCompr tiff_default_compression = TIFF_DEFLATE;
> > +#else
> > +const enum TiffCompr tiff_default_compression = TIFF_PACKBITS;
> > +#endif
>
> a #define TIFF_DEFAULT might be a better idea
done
> > +
> > +static int value_size(uint16_t type, uint32_t count, uint8_t * value)
> > +{
> > + switch (type) {
> > + case TIFF_BYTE:
> > + return count;
> > + case TIFF_SHORT:
> > + return 2 * count;
> > + case TIFF_LONG:
> > + return 4 * count;
> > + case TIFF_LONGLONG:
> > + return 8 * count;
> > + case TIFF_STRING:
> > + return strlen(value) + 1;
> > + }
> > + return 0; // never reached
> > +}
done
> > +
> > +/**
> > + * adds an entry to the IFD (referenced by a pointer)
> > + */
> > +static int tiff_add_ifd_entryp(AVCodecContext * avctx, uint16_t tag,
> > + uint16_t type, uint32_t count,
> > + uint8_t * value)
> > +{
> > + TiffEncoderContext *s = avctx->priv_data;
> > + int size = value_size(type, count, value);
> > + TiffIfdEntry *entr;
> > +
> > + if (s->num_ifd_entries == s->max_ifd_entries) { // time to enlarge the buffer
> > + s->max_ifd_entries *= 2;
> > + s->ifd_entries =
> > + av_realloc(s->ifd_entries,
> > + s->max_ifd_entries * sizeof(TiffIfdEntry));
> > + if (s->ifd_entries == NULL) {
> > + av_log(avctx, AV_LOG_ERROR, "not enough memory\n");
> > + return -1;
> > + }
> > + }
>
> as the number of entries seems constant dynamic alocation and reallocation
> isnt needed
i have made a #define TIFF_MAX_IFD_ENTRIES 16 and now ifd_entries is a
static array
> > +
> > + entr = s->ifd_entries + s->num_ifd_entries;
> > + entr->tag = tag;
> > + entr->type = type;
> > + entr->count = count;
> > + if (size > 4) {
> > + if (s->buf + size >= s->buf_end){
> > + av_log(avctx, AV_LOG_ERROR, "the buffer given is too short\n");
> > + return -1;
> > + }
> > + entr->value = CURR_OFFSET(s);
> > + memcpy(s->buf, value, size);
> > + s->buf += size;
> > + } else {
> > + entr->value = 0;
> > + memcpy((uint8_t *) & entr->value, value, size);
>
> the cast is unneeded
fixed
> > + }
> > + s->num_ifd_entries++;
> > + return 0;
> > +}
>
> also why do you build the IFD in memory as TiffIfdEntry objects and then
> write it out, why not write each entry directly?
now I write each entry to buffer and after writing the whole data which
does not fit in the IFD, I copy the IFD to stream.
> > +/**
> > + * adds an entry to the IFD (referenced by a value)
> > + * [it is helpful when passing constants]
> > + */
> > +static int tiff_add_ifd_entryv(AVCodecContext * avctx, uint16_t tag,
> > + uint16_t type, uint32_t count,
> > + uint32_t value)
> > +{
> > + if (type == TIFF_BYTE) {
> > + uint8_t x = value;
> > + return tiff_add_ifd_entryp(avctx, tag, type, count, &x);
> > + } else if (type == TIFF_SHORT) {
> > + uint16_t x = value;
> > + return tiff_add_ifd_entryp(avctx, tag, type, count, &x);
> > + } else if (type == TIFF_LONG) {
> > + uint32_t x = value;
> > + return tiff_add_ifd_entryp(avctx, tag, type, count, &x);
> > + }
> > + return -1;
> > +}
>
> this function is unneeded
>
> a simple
> tiff_add_ifd_entryp(avctx, tag, type, count, (uint16_t[]){value});
> will work too
done
> [...]
> > +/**
> > + * packbits compression implementation
> > + */
> > +static int tiff_pack_bits(uint8_t * dst, int *dst_len, uint8_t * src,
> > + int src_len)
> > +{
> > + uint8_t *dst_begin = dst, *last_literal, special_case_ch;
> > + enum { START, RUN, LITERAL, SPECIAL } last;
> > + last = START;
> > + while (src_len > 0) {
> > + uint8_t *sp;
> > + int num = 1;
> > +
> > + for (sp = src + 1, src_len--; (*sp == *src) && (src_len > 0);
> > + src++, src_len--)
> > + num++;
> > +
> > + if (dst - dst_begin + (num + 127) / 128 + 2 >= *dst_len) // check if has enough space
> > + return -1;
> > +
> > +
> > +#define CHECK_AND_ADD(x, expr){ \
> > + if ((expr) || *last_literal == 127){\
> > + *dst = -1;\
> > + last_literal = dst++;\
> > + }\
> > + (*last_literal)++;\
> > + *dst++ = x;\
> > + }
> > + if (num == 1) {
> > + if (last == SPECIAL) {
> > + CHECK_AND_ADD(special_case_ch, 0);
> > + CHECK_AND_ADD(special_case_ch, 0);
> > + CHECK_AND_ADD(*sp, 0);
> > + } else
> > + CHECK_AND_ADD(*sp, last != LITERAL);
> > + last = LITERAL;
> > + } else if (num == 2) {
> > + switch (last) {
> > + case LITERAL:
> > + special_case_ch = *sp;
> > + last = SPECIAL;
> > + break;
> > + case SPECIAL:
> > + CHECK_AND_ADD(special_case_ch, 0);
> > + CHECK_AND_ADD(special_case_ch, 0);
> > + special_case_ch = *sp;
> > + default:
> > + *dst++ = -1;
> > + *dst++ = *sp;
> > + last = RUN;
> > + }
> > + } else {
> > + if (last == SPECIAL) {
> > + *dst++ = -1;
> > + *dst++ = special_case_ch;
> > + }
> > + while (num > 0) {
> > + *dst++ = (uint8_t) (-FFMIN(num, 128) + 1);
> > + *dst++ = *sp;
> > + num -= 128;
> > + }
> > + last = RUN;
> > + }
> > + }
>
> this code would encode 0 0 1 to -2 0 0 1
> which is wrong as that doesnt decode to 0 0 1
>
> furthermore its a duplicate of the rle encoder in targaenc.c
> please use the code from targaenc.c or if you prefer replace the code in
> targaenc.c with a better implementation and use that then in both targa and
> tiff (this of course would need benchmarks)
Huh, a stupid bug... ;-) fixed it, but now I have used the code from targaenc.c. However, IMHO it can be optimized
a bit. I will see what I can do later.
> [...]
> > +static int tiff_prepare_color_map(AVCodecContext * avctx)
> > +{
> > + TiffEncoderContext *s = avctx->priv_data;
> > + AVFrame *p = s->p;
> > + int i;
> > + uint32_t *src = p->data[1];
> > +
> > + s->color_map = av_malloc((1 << s->bpp) * 3 * sizeof(uint16_t));
> > + if (s->color_map == NULL) {
> > + av_log(avctx, AV_LOG_ERROR, "not enough memory\n");
> > + return -1;
> > + }
> > +
> > + for (i = 0; i < (1 << s->bpp); i++) {
> > + uint32_t sample = *src;
>
> *src++;
> avoids the seperate ++ later
fixed
> > + s->color_map[2 * (1 << s->bpp) + i] = (sample & 255) << 8; // B
>
> 2 * (1 << s->bpp) == 2<<s->bpp
fixed (this and other similiar expressions)
> > + sample = sample >> 8;
> > + s->color_map[(1 << s->bpp) + i] = (sample & 255) << 8; // G
> > + sample = sample >> 8;
> > + s->color_map[i] = (sample & 255) << 8; // R
>
> white is 65535 for short but 8bit white of 255 with the code above that
> reaches 65280 only
fixed. Now It does
s->color_map[...] = (sample & 255) * 257;
> [...]
> > + /* compute the number of strips */
> > + if (avctx->width == 0 || avctx->height == 0) {
> > + av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
> > + return -1;
>
> i dont think a encoder init function can be called with width or height
> 0 that is it will fail some checks before ...
removed
> > + } else if (s->compression == TIFF_DEFLATE) {
> > + s->lines_per_strip = avctx->height;
> > + s->num_of_strips = 1;
> > + } else {
> > + s->lines_per_strip = (8192 + s->bytes_per_line - 1) / (s->bytes_per_line); // =ceil(height/num_of_strips)
>
> superfluous ()
changed
> [...]
> > + case TIFF_RAW:
> > + if (s->buf + avctx->height * s->bytes_per_line >= s->buf_end){
> > + av_log(avctx, AV_LOG_ERROR, "the buffer given is too short\n");
> > + return -1;
> > + }
> > + for (y = 0; y < avctx->height; y += s->lines_per_strip) {
> > + uint8_t *src;
> > + int i;
> > +
> > + s->strip_offset[s->strip++] = CURR_OFFSET(s);
> > + for (i = 0;
> > + (i < s->lines_per_strip) && (y + i < avctx->height);
> > + i++) {
> > + src = p->data[0] + (y + i) * p->linesize[0];
>
> uint8_t *src= p->data[0] + y * p->linesize[0];
> src += p->linesize[0];
> moves some opertaions out of the loop
done
> > + memcpy(s->buf, src, s->bytes_per_line);
> > + s->buf += s->bytes_per_line;
>
> bytestream_put_buffer()
changed (here and in other places)
> > + }
> > + }
> > + break;
> > + case TIFF_PACKBITS:
> > + for (y = 0; y < avctx->height; y += s->lines_per_strip) {
> > + uint8_t *src;
> > + int i;
> > +
> > + s->strip_offset[s->strip++] = CURR_OFFSET(s);
> > + for (i = 0;
> > + (i < s->lines_per_strip) && (y + i < avctx->height);
> > + i++) {
> > + int plen = s->buf_end - s->buf;
> > + src = p->data[0] + (y + i) * p->linesize[0];
> > + if (tiff_pack_bits(s->buf, &plen, src, s->bytes_per_line)
> > + == -1) {
> > + av_log(avctx, AV_LOG_ERROR,
> > + "packbits compression error\n");
> > + return -1;
> > + }
> > + s->buf += plen;
> > + }
> > + }
>
> this and the TIFF_RAW case contain some duplicate code
I have made a macro, which contains common code for PACKBITS and RAW. I
don't find it very elegant, but it solves the problem of duplicate code, and
avoids the unneeded "if" for each line of the image - as Bartlomiej does.
> [...]
> > +/**
> > + * encoding scheme:
> > + * @li [if the client has not set it] TIFF signature
> > + * @li offset of the IFD
> > + * @li image data [divided into strips]
> > + * @li IFD data which doesn't fit inside the IFD
> > + * @li IFD, sorted by tag
> > + * @li [if the client will not set it] 32 zero bits
> > + */
> > +static int tiff_encode_frame(AVCodecContext * avctx, unsigned char *buf,
> > + int buf_size, void *data)
> > +{
> > + TiffEncoderContext *s = avctx->priv_data;
> > + const uint8_t header[4] = {0x49, 0x49, 42, 0},
> > + trailer[4] = {0, 0, 0, 0};
> > + uint32_t xres[2] = {72, 1},
> > + yres[2] = {72, 1};
> > + int ret;
> > +
> > + s->buf_start = s->buf = buf;
> > + s->buf_end = s->buf + buf_size;
> > + s->p = data;
> > +
> > + if (avctx->frame_number == 0) {
> > + /* do we have to write the header? (the TIFF muxer does it
> > + * for us - it is useful when creating TIFFs with multiple images in one file) */
> > + if (!avctx->extradata_size) { // i'm sure that there exists a nicer solution - maybe adding flags?
> > + memcpy(s->buf, header, 4);
> > + s->buf += 4;
> > + s->offset = 0;
> > + s->write_trailer = 1;
> > + } else
> > + s->offset = *(int *) avctx->extradata;
> > + }
>
> how is this supposed to work with stream copy? that is if i take a series
> of tiff images and stream copy them (no decoder, no encoder) into a
> multi image tiff file?
> how is this supposed to work if i take a multi image tiff file and stream
> copy that into individual tiff files (no decoder no encoder)?
> how is a decoder supposed to decode such individual tiff images, considering
> that a decoder sees exactly what the encoder outputs, that is a packet with
> offsets pointing relative to the filebegin which is not known
> or accessible to the decoder ...
>
> the easiest solution is to drop multi image support, implementing it cleanly
> could take more time than you think, the same is of course true for the
> other student working on tiff encoding
> you can always send multi tiff support as seperate patch later if you like
> and find a way how to cleanly implement it
I have dropped it for now.
> > +
> > + switch (avctx->pix_fmt) {
> > + case PIX_FMT_PAL8:
> > + s->bpp_info[0] = 8;
> > + s->nsamples = 1;
> > + s->bpp = 8;
> > + s->photometric_interpretation = 3;
> > + s->has_color_map = 1;
> > + break;
> > + case PIX_FMT_RGB24:
> > + s->bpp_info[0] = 8;
> > + s->bpp_info[1] = 8;
> > + s->bpp_info[2] = 8;
> > + s->nsamples = 3;
> > + s->bpp = 24;
> > + s->photometric_interpretation = 2;
> > + break;
> > + case PIX_FMT_GRAY8:
> > + s->bpp_info[0] = 8;
> > + s->nsamples = 1;
> > + s->bpp = 8;
> > + s->photometric_interpretation = 1;
> > + break;
> > + case PIX_FMT_MONOBLACK:
> > + s->bpp_info[0] = 1;
> > + s->nsamples = 1;
> > + s->bpp = 1;
> > + s->photometric_interpretation = 1;
> > + break;
> > + case PIX_FMT_MONOWHITE:
> > + s->bpp_info[0] = 1;
> > + s->nsamples = 1;
> > + s->bpp = 1;
> > + s->photometric_interpretation = 0;
> > + break;
> > + default:
> > + av_log(avctx, AV_LOG_ERROR, "this pix_fmt (%d) is not supported\n",
> > + avctx->pix_fmt);
> > + return -1;
> > + }
>
> this can be simplified
done
> > +
> > + s->bytes_per_line = (avctx->width * s->bpp + 7) / 8;
> > +
> > + s->entry_point = s->buf;
> > + s->buf += 4; // a place for next IFD offset [later updated]
> > +
> > + if (s->has_color_map)
> > + if (tiff_prepare_color_map(avctx) < 0) {
> > + return -1;
> > + }
> > +
> > + /* write the image data */
> > + if (tiff_write_image_data(avctx)) {
> > + if (s->strip_offset)
> > + av_free(s->strip_offset);
> > + if (s->strip_size)
> > + av_free(s->strip_size);
> > + if (s->has_color_map)
> > + av_free(s->color_map);
> > + return -1;
>
> the NULL checks before av_free are unneeded
> also this is duplicate code, the code at the end does the same
Oops, I didn't know, that I can pass NULL to av_free :-). Changed it.
--
Best regards,
Kamil Nowosad
-------------- next part --------------
Index: libavcodec/rle.c
===================================================================
--- libavcodec/rle.c (wersja 8553)
+++ libavcodec/rle.c (kopia robocza)
@@ -1,5 +1,5 @@
/*
- * Targa (.tga) image encoder
+ * Run-length compression
* Copyright (c) 2007 Bobby Bingham
*
* This file is part of FFmpeg.
@@ -19,7 +19,15 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
+
+/**
+ * Run-length compression
+ * @file rle.c
+ * @author Bobby Bingham
+ */
+
#include "avcodec.h"
+#include "rle.h"
/**
* Count up to 127 consecutive pixels which are either all the same or
@@ -55,32 +63,29 @@
}
/**
- * RLE compress the image, with maximum size of out_size
+ * RLE compress the data, with maximum size of out_size
* @param outbuf Output buffer
* @param out_size Maximum output size
- * @param pic Image to compress
+ * @param ptr data to compress
+ * @param w size of data to compress
* @param bpp Bytes per pixel
- * @param w Image width
- * @param h Image height
+ * @param style style of encoding @see rle.h
* @return Size of output in bytes, or -1 if larger than out_size
*/
-static int targa_encode_rle(uint8_t *outbuf, int out_size, AVFrame *pic,
- int bpp, int w, int h)
+int ff_encode_rle(uint8_t *outbuf, int out_size, uint8_t *ptr,
+ int w, int bpp, int style)
{
- int count, x, y;
- uint8_t *ptr, *line, *out;
+ int count, x;
+ uint8_t *out = outbuf;
- out = outbuf;
- line = pic->data[0];
-
- for(y = 0; y < h; y ++) {
- ptr = line;
-
for(x = 0; x < w; x += count) {
/* see if we can encode the next set of pixels with RLE */
if((count = count_pixels(ptr, w-x, bpp, 1)) > 1) {
if(out + bpp + 1 > outbuf + out_size) return -1;
- *out++ = 0x80 | (count - 1);
+ if (style == FF_RLE_SETBIT)
+ *out++ = 0x80 | (count - 1);
+ else
+ *out++ = -count + 1;
memcpy(out, ptr, bpp);
out += bpp;
} else {
@@ -94,106 +99,5 @@
}
ptr += count * bpp;
}
-
- line += pic->linesize[0];
- }
-
return out - outbuf;
}
-
-static int targa_encode_normal(uint8_t *outbuf, AVFrame *pic, int bpp, int w, int h)
-{
- int i, n = bpp * w;
- uint8_t *out = outbuf;
- uint8_t *ptr = pic->data[0];
-
- for(i=0; i < h; i++) {
- memcpy(out, ptr, n);
- out += n;
- ptr += pic->linesize[0];
- }
-
- return out - outbuf;
-}
-
-static int targa_encode_frame(AVCodecContext *avctx,
- unsigned char *outbuf,
- int buf_size, void *data){
- AVFrame *p = data;
- int bpp, picsize, datasize;
- uint8_t *out;
-
- if(avctx->width > 0xffff || avctx->height > 0xffff) {
- av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n");
- return -1;
- }
- picsize = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
- if(buf_size < picsize + 45) {
- av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
- return -1;
- }
-
- p->pict_type= FF_I_TYPE;
- p->key_frame= 1;
-
- /* zero out the header and only set applicable fields */
- memset(outbuf, 0, 11);
- AV_WL16(outbuf+12, avctx->width);
- AV_WL16(outbuf+14, avctx->height);
- outbuf[17] = 0x20; /* origin is top-left. no alpha */
-
- /* TODO: support alpha channel */
- switch(avctx->pix_fmt) {
- case PIX_FMT_GRAY8:
- outbuf[2] = 3; /* uncompressed grayscale image */
- outbuf[16] = 8; /* bpp */
- break;
- case PIX_FMT_RGB555:
- outbuf[2] = 2; /* uncompresses true-color image */
- outbuf[16] = 16; /* bpp */
- break;
- case PIX_FMT_BGR24:
- outbuf[2] = 2; /* uncompressed true-color image */
- outbuf[16] = 24; /* bpp */
- break;
- default:
- return -1;
- }
- bpp = outbuf[16] >> 3;
-
- out = outbuf + 18; /* skip past the header we just output */
-
- /* try RLE compression */
- datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, avctx->height);
-
- /* if that worked well, mark the picture as RLE compressed */
- if(datasize >= 0)
- outbuf[2] |= 8;
-
- /* if RLE didn't make it smaller, go back to no compression */
- else datasize = targa_encode_normal(out, p, bpp, avctx->width, avctx->height);
-
- out += datasize;
-
- /* The standard recommends including this section, even if we don't use
- * any of the features it affords. TODO: take advantage of the pixel
- * aspect ratio and encoder ID fields available? */
- memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
-
- return out + 26 - outbuf;
-}
-
-static int targa_encode_init(AVCodecContext *avctx)
-{
- return 0;
-}
-
-AVCodec targa_encoder = {
- .name = "targa",
- .type = CODEC_TYPE_VIDEO,
- .id = CODEC_ID_TARGA,
- .priv_data_size = 0,
- .init = targa_encode_init,
- .encode = targa_encode_frame,
- .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB555, PIX_FMT_GRAY8, -1},
-};
Index: libavcodec/rle.h
===================================================================
--- libavcodec/rle.h (wersja 0)
+++ libavcodec/rle.h (wersja 0)
@@ -0,0 +1,36 @@
+/*
+ * Run-length compression
+ * Copyright (c) 2007 Bobby Bingham
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifndef RLE_H
+#define RLE_H
+
+/** set 0x80 | (n-1) when decoder has to repeat next chunk n times
+ * (used by targa encoder) */
+#define FF_RLE_SETBIT 0
+/** set -n + 1 when decoder has to repeat next chunk n times
+ * (used by tiff encoder) */
+#define FF_RLE_NEG 1
+
+int ff_encode_rle(uint8_t *outbuf, int out_size, uint8_t *line,
+ int line_size, int bpp, int style);
+
+#endif /* RLE_H */
Index: libavcodec/utils.c
===================================================================
--- libavcodec/utils.c (wersja 8576)
+++ libavcodec/utils.c (kopia robocza)
@@ -636,6 +636,9 @@
{"coder", NULL, OFFSET(coder_type), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX, V|E, "coder"},
{"vlc", "variable length coder / huffman coder", 0, FF_OPT_TYPE_CONST, FF_CODER_TYPE_VLC, INT_MIN, INT_MAX, V|E, "coder"},
{"ac", "arithmetic coder", 0, FF_OPT_TYPE_CONST, FF_CODER_TYPE_AC, INT_MIN, INT_MAX, V|E, "coder"},
+{"raw", "raw (no encoding)", 0, FF_OPT_TYPE_CONST, FF_CODER_TYPE_RAW, INT_MIN, INT_MAX, V|E, "coder"},
+{"rle", "run-lenghth coder", 0, FF_OPT_TYPE_CONST, FF_CODER_TYPE_RLE, INT_MIN, INT_MAX, V|E, "coder"},
+{"deflate", "deflate-based coder", 0, FF_OPT_TYPE_CONST, FF_CODER_TYPE_DEFLATE, INT_MIN, INT_MAX, V|E, "coder"},
{"context", "context model", OFFSET(context_model), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX, V|E},
{"slice_flags", NULL, OFFSET(slice_flags), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX},
{"xvmc_acceleration", NULL, OFFSET(xvmc_acceleration), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX},
Index: libavcodec/Makefile
===================================================================
--- libavcodec/Makefile (wersja 8576)
+++ libavcodec/Makefile (kopia robocza)
@@ -140,10 +140,11 @@
OBJS-$(CONFIG_SVQ1_ENCODER) += svq1.o
OBJS-$(CONFIG_SVQ3_DECODER) += h264.o
OBJS-$(CONFIG_TARGA_DECODER) += targa.o
-OBJS-$(CONFIG_TARGA_ENCODER) += targaenc.o
+OBJS-$(CONFIG_TARGA_ENCODER) += targaenc.o rle.o
OBJS-$(CONFIG_THEORA_DECODER) += vp3.o xiph.o
OBJS-$(CONFIG_TIERTEXSEQVIDEO_DECODER) += tiertexseqv.o
OBJS-$(CONFIG_TIFF_DECODER) += tiff.o lzw.o
+OBJS-$(CONFIG_TIFF_ENCODER) += tiffenc.o rle.o
OBJS-$(CONFIG_TRUEMOTION1_DECODER) += truemotion1.o
OBJS-$(CONFIG_TRUEMOTION2_DECODER) += truemotion2.o
OBJS-$(CONFIG_TRUESPEECH_DECODER) += truespeech.o
Index: libavcodec/tiff.c
===================================================================
--- libavcodec/tiff.c (wersja 8576)
+++ libavcodec/tiff.c (kopia robocza)
@@ -20,54 +20,12 @@
*
*/
#include "avcodec.h"
+#include "tiff.h"
#ifdef CONFIG_ZLIB
#include <zlib.h>
#endif
#include "lzw.h"
-/* abridged list of TIFF tags */
-enum TiffTags{
- TIFF_WIDTH = 0x100,
- TIFF_HEIGHT,
- TIFF_BPP,
- TIFF_COMPR,
- TIFF_INVERT = 0x106,
- TIFF_STRIP_OFFS = 0x111,
- TIFF_ROWSPERSTRIP = 0x116,
- TIFF_STRIP_SIZE,
- TIFF_PLANAR = 0x11C,
- TIFF_XPOS = 0x11E,
- TIFF_YPOS = 0x11F,
- TIFF_PREDICTOR = 0x13D,
- TIFF_PAL = 0x140
-};
-
-enum TiffCompr{
- TIFF_RAW = 1,
- TIFF_CCITT_RLE,
- TIFF_G3,
- TIFF_G4,
- TIFF_LZW,
- TIFF_JPEG,
- TIFF_NEWJPEG,
- TIFF_ADOBE_DEFLATE,
- TIFF_PACKBITS = 0x8005,
- TIFF_DEFLATE = 0x80B2
-};
-
-enum TiffTypes{
- TIFF_BYTE = 1,
- TIFF_STRING,
- TIFF_SHORT,
- TIFF_LONG,
- TIFF_LONGLONG
-};
-
-/** sizes of various TIFF field types */
-static const int type_sizes[6] = {
- 0, 1, 100, 2, 4, 8
-};
-
typedef struct TiffContext {
AVCodecContext *avctx;
AVFrame picture;
Index: libavcodec/tiff.h
===================================================================
--- libavcodec/tiff.h (wersja 8542)
+++ libavcodec/tiff.h (kopia robocza)
@@ -1,6 +1,7 @@
/*
- * TIFF image decoder
+ * TIFF image encoder/decoder
* Copyright (c) 2006 Konstantin Shishkov
+ * Copyright (c) 2007 Kamil Nowosad
*
* This file is part of FFmpeg.
*
@@ -19,12 +20,10 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
-#include "avcodec.h"
-#ifdef CONFIG_ZLIB
-#include <zlib.h>
-#endif
-#include "lzw.h"
+#ifndef TIFF_H
+#define TIFF_H
+
/* abridged list of TIFF tags */
enum TiffTags{
TIFF_WIDTH = 0x100,
@@ -33,15 +32,20 @@
TIFF_COMPR,
TIFF_INVERT = 0x106,
TIFF_STRIP_OFFS = 0x111,
- TIFF_ROWSPERSTRIP = 0x116,
+ TIFF_SAMPLESPERPIX = 0x115,
+ TIFF_ROWSPERSTRIP,
TIFF_STRIP_SIZE,
TIFF_PLANAR = 0x11C,
+ TIFF_XRES = 0x11A,
+ TIFF_YRES = 0x11B,
TIFF_XPOS = 0x11E,
TIFF_YPOS = 0x11F,
+ TIFF_RES_UNIT = 0x128,
TIFF_PREDICTOR = 0x13D,
TIFF_PAL = 0x140
};
+/* list of TIFF compression types */
enum TiffCompr{
TIFF_RAW = 1,
TIFF_CCITT_RLE,
@@ -60,473 +64,11 @@
TIFF_STRING,
TIFF_SHORT,
TIFF_LONG,
- TIFF_LONGLONG
+ TIFF_RATIONAL
};
/** sizes of various TIFF field types */
static const int type_sizes[6] = {
0, 1, 100, 2, 4, 8
};
-
-typedef struct TiffContext {
- AVCodecContext *avctx;
- AVFrame picture;
-
- int width, height;
- unsigned int bpp;
- int le;
- int compr;
- int invert;
-
- int strips, rps;
- int sot;
- uint8_t* stripdata;
- uint8_t* stripsizes;
- int stripsize, stripoff;
- LZWState *lzw;
-} TiffContext;
-
-static int tget_short(uint8_t **p, int le){
- int v = le ? AV_RL16(*p) : AV_RB16(*p);
- *p += 2;
- return v;
-}
-
-static int tget_long(uint8_t **p, int le){
- int v = le ? AV_RL32(*p) : AV_RB32(*p);
- *p += 4;
- return v;
-}
-
-static int tget(uint8_t **p, int type, int le){
- switch(type){
- case TIFF_BYTE : return *(*p)++;
- case TIFF_SHORT: return tget_short(p, le);
- case TIFF_LONG : return tget_long (p, le);
- default : return -1;
- }
-}
-
-static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, uint8_t *src, int size, int lines){
- int c, line, pixels, code;
- uint8_t *ssrc = src;
- int width = s->width * (s->bpp / 8);
-#ifdef CONFIG_ZLIB
- uint8_t *zbuf; unsigned long outlen;
-
- if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){
- outlen = width * lines;
- zbuf = av_malloc(outlen);
- if(uncompress(zbuf, &outlen, src, size) != Z_OK){
- av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu)\n", outlen, (unsigned long)width * lines);
- av_free(zbuf);
- return -1;
- }
- src = zbuf;
- for(line = 0; line < lines; line++){
- memcpy(dst, src, width);
- dst += stride;
- src += width;
- }
- av_free(zbuf);
- return 0;
- }
-#endif
- if(s->compr == TIFF_LZW){
- if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){
- av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");
- return -1;
- }
- }
- for(line = 0; line < lines; line++){
- if(src - ssrc > size){
- av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
- return -1;
- }
- switch(s->compr){
- case TIFF_RAW:
- memcpy(dst, src, s->width * (s->bpp / 8));
- src += s->width * (s->bpp / 8);
- break;
- case TIFF_PACKBITS:
- for(pixels = 0; pixels < width;){
- code = (int8_t)*src++;
- if(code >= 0){
- code++;
- if(pixels + code > width){
- av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
- return -1;
- }
- memcpy(dst + pixels, src, code);
- src += code;
- pixels += code;
- }else if(code != -128){ // -127..-1
- code = (-code) + 1;
- if(pixels + code > width){
- av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
- return -1;
- }
- c = *src++;
- memset(dst + pixels, c, code);
- pixels += code;
- }
- }
- break;
- case TIFF_LZW:
- pixels = ff_lzw_decode(s->lzw, dst, width);
- if(pixels < width){
- av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
- return -1;
- }
- break;
- }
- dst += stride;
- }
- return 0;
-}
-
-
-static int tiff_decode_tag(TiffContext *s, uint8_t *start, uint8_t *buf, uint8_t *end_buf, AVFrame *pic)
-{
- int tag, type, count, off, value = 0;
- uint8_t *src, *dst;
- int i, j, ssize, soff, stride;
- int *pal, *rp, *gp, *bp;
-
- tag = tget_short(&buf, s->le);
- type = tget_short(&buf, s->le);
- count = tget_long(&buf, s->le);
- off = tget_long(&buf, s->le);
-
- if(count == 1){
- switch(type){
- case TIFF_BYTE:
- case TIFF_SHORT:
- buf -= 4;
- value = tget(&buf, type, s->le);
- buf = NULL;
- break;
- case TIFF_LONG:
- value = off;
- buf = NULL;
- break;
- default:
- value = -1;
- buf = start + off;
- }
- }else if(type_sizes[type] * count <= 4){
- buf -= 4;
- }else{
- buf = start + off;
- }
-
- if(buf && (buf < start || buf > end_buf)){
- av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
- return -1;
- }
-
- switch(tag){
- case TIFF_WIDTH:
- s->width = value;
- break;
- case TIFF_HEIGHT:
- s->height = value;
- break;
- case TIFF_BPP:
- if(count == 1) s->bpp = value;
- else{
- switch(type){
- case TIFF_BYTE:
- s->bpp = (off & 0xFF) + ((off >> 8) & 0xFF) + ((off >> 16) & 0xFF) + ((off >> 24) & 0xFF);
- break;
- case TIFF_SHORT:
- case TIFF_LONG:
- s->bpp = 0;
- for(i = 0; i < count; i++) s->bpp += tget(&buf, type, s->le);
- break;
- default:
- s->bpp = -1;
- }
- }
- switch(s->bpp){
- case 8:
- s->avctx->pix_fmt = PIX_FMT_PAL8;
- break;
- case 24:
- s->avctx->pix_fmt = PIX_FMT_RGB24;
- break;
- case 16:
- if(count == 1){
- s->avctx->pix_fmt = PIX_FMT_GRAY16BE;
- }else{
- av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%i)\n", s->bpp);
- return -1;
- }
- break;
- default:
- av_log(s->avctx, AV_LOG_ERROR, "This format is not supported (bpp=%i)\n", s->bpp);
- return -1;
- }
- if(s->width != s->avctx->width || s->height != s->avctx->height){
- if(avcodec_check_dimensions(s->avctx, s->width, s->height))
- return -1;
- avcodec_set_dimensions(s->avctx, s->width, s->height);
- }
- if(s->picture.data[0])
- s->avctx->release_buffer(s->avctx, &s->picture);
- if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
- av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
- return -1;
- }
- if(s->bpp == 8){
- /* make default grayscale pal */
- pal = s->picture.data[1];
- for(i = 0; i < 256; i++)
- pal[i] = i * 0x010101;
- }
- break;
- case TIFF_COMPR:
- s->compr = value;
- switch(s->compr){
- case TIFF_RAW:
- case TIFF_PACKBITS:
- case TIFF_LZW:
- break;
- case TIFF_DEFLATE:
- case TIFF_ADOBE_DEFLATE:
-#ifdef CONFIG_ZLIB
- break;
-#else
- av_log(s->avctx, AV_LOG_ERROR, "Deflate: ZLib not compiled in\n");
- return -1;
-#endif
- case TIFF_G3:
- av_log(s->avctx, AV_LOG_ERROR, "CCITT G3 compression is not supported\n");
- return -1;
- case TIFF_G4:
- av_log(s->avctx, AV_LOG_ERROR, "CCITT G4 compression is not supported\n");
- return -1;
- case TIFF_CCITT_RLE:
- av_log(s->avctx, AV_LOG_ERROR, "CCITT RLE compression is not supported\n");
- return -1;
- case TIFF_JPEG:
- case TIFF_NEWJPEG:
- av_log(s->avctx, AV_LOG_ERROR, "JPEG compression is not supported\n");
- return -1;
- default:
- av_log(s->avctx, AV_LOG_ERROR, "Unknown compression method %i\n", s->compr);
- return -1;
- }
- break;
- case TIFF_ROWSPERSTRIP:
- if(value < 1){
- av_log(s->avctx, AV_LOG_ERROR, "Incorrect value of rows per strip\n");
- return -1;
- }
- s->rps = value;
- break;
- case TIFF_STRIP_OFFS:
- if(count == 1){
- s->stripdata = NULL;
- s->stripoff = value;
- }else
- s->stripdata = start + off;
- s->strips = count;
- if(s->strips == 1) s->rps = s->height;
- s->sot = type;
- if(s->stripdata > end_buf){
- av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
- return -1;
- }
- break;
- case TIFF_STRIP_SIZE:
- if(count == 1){
- s->stripsizes = NULL;
- s->stripsize = value;
- s->strips = 1;
- }else{
- s->stripsizes = start + off;
- }
- s->strips = count;
- if(s->stripsizes > end_buf){
- av_log(s->avctx, AV_LOG_ERROR, "Tag referencing position outside the image\n");
- return -1;
- }
- if(!pic->data[0]){
- av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
- return -1;
- }
- /* now we have the data and may start decoding */
- stride = pic->linesize[0];
- dst = pic->data[0];
- for(i = 0; i < s->height; i += s->rps){
- if(s->stripsizes)
- ssize = tget(&s->stripsizes, type, s->le);
- else
- ssize = s->stripsize;
-
- if(s->stripdata){
- soff = tget(&s->stripdata, s->sot, s->le);
- }else
- soff = s->stripoff;
- src = start + soff;
- if(tiff_unpack_strip(s, dst, stride, src, ssize, FFMIN(s->rps, s->height - i)) < 0)
- break;
- dst += s->rps * stride;
- }
- break;
- case TIFF_PREDICTOR:
- if(!pic->data[0]){
- av_log(s->avctx, AV_LOG_ERROR, "Picture initialization missing\n");
- return -1;
- }
- if(value == 2){
- src = pic->data[0];
- stride = pic->linesize[0];
- soff = s->bpp >> 3;
- ssize = s->width * soff;
- for(i = 0; i < s->height; i++) {
- for(j = soff; j < ssize; j++)
- src[j] += src[j - soff];
- src += stride;
- }
- }
- break;
- case TIFF_INVERT:
- switch(value){
- case 0:
- s->invert = 1;
- break;
- case 1:
- s->invert = 0;
- break;
- case 2:
- case 3:
- break;
- default:
- av_log(s->avctx, AV_LOG_ERROR, "Color mode %d is not supported\n", value);
- return -1;
- }
- break;
- case TIFF_PAL:
- if(s->avctx->pix_fmt != PIX_FMT_PAL8){
- av_log(s->avctx, AV_LOG_ERROR, "Palette met but this is not palettized format\n");
- return -1;
- }
- pal = s->picture.data[1];
- off = type_sizes[type];
- rp = buf;
- gp = buf + count / 3 * off;
- bp = buf + count / 3 * off * 2;
- off = (type_sizes[type] - 1) << 3;
- for(i = 0; i < count / 3; i++){
- j = (tget(&rp, type, s->le) >> off) << 16;
- j |= (tget(&gp, type, s->le) >> off) << 8;
- j |= tget(&bp, type, s->le) >> off;
- pal[i] = j;
- }
- break;
- case TIFF_PLANAR:
- if(value == 2){
- av_log(s->avctx, AV_LOG_ERROR, "Planar format is not supported\n");
- return -1;
- }
- break;
- }
- return 0;
-}
-
-static int decode_frame(AVCodecContext *avctx,
- void *data, int *data_size,
- uint8_t *buf, int buf_size)
-{
- TiffContext * const s = avctx->priv_data;
- AVFrame *picture = data;
- AVFrame * const p= (AVFrame*)&s->picture;
- uint8_t *orig_buf = buf, *end_buf = buf + buf_size;
- int id, le, off;
- int i, entries;
-
- //parse image header
- id = AV_RL16(buf); buf += 2;
- if(id == 0x4949) le = 1;
- else if(id == 0x4D4D) le = 0;
- else{
- av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");
- return -1;
- }
- s->le = le;
- s->invert = 0;
- // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
- // that further identifies the file as a TIFF file"
- if(tget_short(&buf, le) != 42){
- av_log(avctx, AV_LOG_ERROR, "The answer to life, universe and everything is not correct!\n");
- return -1;
- }
- /* parse image file directory */
- off = tget_long(&buf, le);
- if(orig_buf + off + 14 >= end_buf){
- av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");
- return -1;
- }
- buf = orig_buf + off;
- entries = tget_short(&buf, le);
- for(i = 0; i < entries; i++){
- if(tiff_decode_tag(s, orig_buf, buf, end_buf, p) < 0)
- return -1;
- buf += 12;
- }
-
- if(s->invert){
- uint8_t *src;
- int j;
-
- src = s->picture.data[0];
- for(j = 0; j < s->height; j++){
- for(i = 0; i < s->picture.linesize[0]; i++)
- src[i] = 255 - src[i];
- src += s->picture.linesize[0];
- }
- }
- *picture= *(AVFrame*)&s->picture;
- *data_size = sizeof(AVPicture);
-
- return buf_size;
-}
-
-static int tiff_init(AVCodecContext *avctx){
- TiffContext *s = avctx->priv_data;
-
- s->width = 0;
- s->height = 0;
- s->avctx = avctx;
- avcodec_get_frame_defaults((AVFrame*)&s->picture);
- avctx->coded_frame= (AVFrame*)&s->picture;
- s->picture.data[0] = NULL;
- ff_lzw_decode_open(&s->lzw);
-
- return 0;
-}
-
-static int tiff_end(AVCodecContext *avctx)
-{
- TiffContext * const s = avctx->priv_data;
-
- ff_lzw_decode_close(&s->lzw);
- if(s->picture.data[0])
- avctx->release_buffer(avctx, &s->picture);
- return 0;
-}
-
-AVCodec tiff_decoder = {
- "tiff",
- CODEC_TYPE_VIDEO,
- CODEC_ID_TIFF,
- sizeof(TiffContext),
- tiff_init,
- NULL,
- tiff_end,
- decode_frame,
- 0,
- NULL
-};
+#endif // TIFF_H
Index: libavcodec/targaenc.c
===================================================================
--- libavcodec/targaenc.c (wersja 8576)
+++ libavcodec/targaenc.c (kopia robocza)
@@ -22,39 +22,6 @@
#include "avcodec.h"
/**
- * Count up to 127 consecutive pixels which are either all the same or
- * all differ from the previous and next pixels.
- * @param start Pointer to the first pixel
- * @param len Maximum number of pixels
- * @param bpp Bytes per pixel
- * @param same 1 if searching for identical pixel values. 0 for differing
- * @return Number of matching consecutive pixels found
- */
-static int count_pixels(uint8_t *start, int len, int bpp, int same)
-{
- uint8_t *pos;
- int count = 1;
-
- for(pos = start + bpp; count < FFMIN(128, len); pos += bpp, count ++) {
- if(same != !memcmp(pos-bpp, pos, bpp)) {
- if(!same) {
- /* if bpp == 1, then 0 1 1 0 is more efficiently encoded as a single
- * raw block of pixels. for larger bpp, RLE is as good or better */
- if(bpp == 1 && count + 1 < FFMIN(128, len) && *pos != *(pos+1))
- continue;
-
- /* if RLE can encode the next block better than as a raw block,
- * back up and leave _all_ the identical pixels for RLE */
- count --;
- }
- break;
- }
- }
-
- return count;
-}
-
-/**
* RLE compress the image, with maximum size of out_size
* @param outbuf Output buffer
* @param out_size Maximum output size
@@ -67,34 +34,18 @@
static int targa_encode_rle(uint8_t *outbuf, int out_size, AVFrame *pic,
int bpp, int w, int h)
{
- int count, x, y;
- uint8_t *ptr, *line, *out;
+ int y;
+ uint8_t *line, *out;
out = outbuf;
line = pic->data[0];
for(y = 0; y < h; y ++) {
- ptr = line;
-
- for(x = 0; x < w; x += count) {
- /* see if we can encode the next set of pixels with RLE */
- if((count = count_pixels(ptr, w-x, bpp, 1)) > 1) {
- if(out + bpp + 1 > outbuf + out_size) return -1;
- *out++ = 0x80 | (count - 1);
- memcpy(out, ptr, bpp);
- out += bpp;
- } else {
- /* fall back on uncompressed */
- count = count_pixels(ptr, w-x, bpp, 0);
- *out++ = count - 1;
-
- if(out + bpp*count > outbuf + out_size) return -1;
- memcpy(out, ptr, bpp * count);
- out += bpp * count;
- }
- ptr += count * bpp;
- }
-
+ int size = ff_encode_rle(out, &size, line, w, bpp);
+ if (size == -1)
+ return -1;
+ out_size -= size;
+ out += size;
line += pic->linesize[0];
}
Index: libavcodec/allcodecs.c
===================================================================
--- libavcodec/allcodecs.c (wersja 8576)
+++ libavcodec/allcodecs.c (kopia robocza)
@@ -131,7 +131,7 @@
REGISTER_ENCDEC (TARGA, targa);
REGISTER_DECODER(THEORA, theora);
REGISTER_DECODER(TIERTEXSEQVIDEO, tiertexseqvideo);
- REGISTER_DECODER(TIFF, tiff);
+ REGISTER_ENCDEC (TIFF, tiff);
REGISTER_DECODER(TRUEMOTION1, truemotion1);
REGISTER_DECODER(TRUEMOTION2, truemotion2);
REGISTER_DECODER(TSCC, tscc);
Index: libavcodec/avcodec.h
===================================================================
--- libavcodec/avcodec.h (wersja 8576)
+++ libavcodec/avcodec.h (kopia robocza)
@@ -1565,8 +1565,11 @@
*/
int global_quality;
-#define FF_CODER_TYPE_VLC 0
-#define FF_CODER_TYPE_AC 1
+#define FF_CODER_TYPE_VLC 0
+#define FF_CODER_TYPE_AC 1
+#define FF_CODER_TYPE_RAW 2 // no coder
+#define FF_CODER_TYPE_RLE 3 // run-length
+#define FF_CODER_TYPE_DEFLATE 4
/**
* coder type
* - encoding: set by user.
@@ -2221,6 +2224,7 @@
extern AVCodec sonic_ls_encoder;
extern AVCodec svq1_encoder;
extern AVCodec targa_encoder;
+extern AVCodec tiff_encoder;
extern AVCodec vcr1_encoder;
extern AVCodec vorbis_encoder;
extern AVCodec wmav1_encoder;
Index: libavcodec/tiffenc.c
===================================================================
--- libavcodec/tiffenc.c (wersja 0)
+++ libavcodec/tiffenc.c (wersja 0)
@@ -0,0 +1,417 @@
+/*
+ * TIFF image encoder
+ * Copyright (c) 2007 Kamil Nowosad
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+/**
+ * TIFF image encoder
+ * @file tiffenc.c
+ * @author Kamil Nowosad
+ */
+#include "avcodec.h"
+#include "bytestream.h"
+#include "rle.h"
+#include "tiff.h"
+#ifdef CONFIG_ZLIB
+#include <zlib.h>
+#endif
+
+#define CURR_OFFSET(s) (s->buf - s->buf_start)
+#define TIFF_MAX_IFD_ENTRIES 16
+#define TIFF_IFD_ENTRY_SIZE 12
+
+#ifdef CONFIG_ZLIB
+#define TIFF_DEFAULT TIFF_DEFLATE
+#else
+#define TIFF_DEFAULT TIFF_PACKBITS
+#endif
+
+typedef struct TiffEncoderContext {
+ enum TiffCompr compression;
+ uint8_t *buf, *buf_start, *buf_end, *entry_point;
+
+ int strip;
+ int num_of_strips;
+ int lines_per_strip;
+ uint32_t *strip_offset;
+ uint32_t *strip_size;
+
+ int has_color_map;
+ uint16_t *color_map;
+
+ AVFrame *p;
+
+ uint8_t ifd_entries[TIFF_IFD_ENTRY_SIZE * TIFF_MAX_IFD_ENTRIES];
+ int num_ifd_entries;
+
+ int bpp;
+ int bytes_per_line;
+ int nsamples;
+
+ uint16_t bpp_info[3];
+
+ int photometric_interpretation;
+} TiffEncoderContext;
+
+static void tiff_put(uint8_t ** dst, uint8_t * src, int size,
+ uint16_t type)
+{
+ int i, flip;
+#ifdef WORDS_BIGENDIAN
+ flip = ((int[]) {0, 0, 0, 1, 3, 3})[type];
+#else
+ flip = 0;
+#endif
+ for (i = 0; i < size; i++)
+ *(*dst)++ = src[i ^ flip];
+}
+
+/**
+ * adds an entry to the IFD
+ */
+static int tiff_add_ifd_entry(AVCodecContext * avctx, uint16_t tag,
+ uint16_t type, uint32_t count,
+ uint8_t * value)
+{
+ TiffEncoderContext *s = avctx->priv_data;
+ int size;
+ uint8_t *entr;
+
+ if (type == TIFF_STRING)
+ size = strlen(value) + 1;
+ else
+ size = type_sizes[type] * count;
+
+ entr = s->ifd_entries + s->num_ifd_entries * TIFF_IFD_ENTRY_SIZE;
+ bytestream_put_le16(&entr, tag);
+ bytestream_put_le16(&entr, type);
+ bytestream_put_le32(&entr, count);
+ if (size > 4) {
+ if (s->buf_end - s->buf < size) {
+ av_log(avctx, AV_LOG_ERROR, "the buffer given is too short\n");
+ return -1;
+ }
+ bytestream_put_le32(&entr, CURR_OFFSET(s));
+ tiff_put(&s->buf, value, size, type);
+ } else
+ tiff_put(&entr, value, size, type);
+ s->num_ifd_entries++;
+ return 0;
+}
+
+/**
+ * writes the whole ifd to stream
+ */
+static int tiff_write_ifd(AVCodecContext * avctx)
+{
+ TiffEncoderContext *s = avctx->priv_data;
+
+ /* image file directory starts here: */
+ bytestream_put_le32(&s->entry_point, CURR_OFFSET(s));
+
+ if (s->buf_end - s->buf < 2 + s->num_ifd_entries * TIFF_IFD_ENTRY_SIZE) {
+ av_log(avctx, AV_LOG_ERROR, "the buffer given is too short\n");
+ return -1;
+ }
+
+ /* the number of IFD entries */
+ bytestream_put_le16(&s->buf, s->num_ifd_entries);
+ bytestream_put_buffer(&s->buf, s->ifd_entries,
+ s->num_ifd_entries * TIFF_IFD_ENTRY_SIZE);
+ return 0;
+}
+
+static int tiff_prepare_color_map(AVCodecContext * avctx)
+{
+ TiffEncoderContext *s = avctx->priv_data;
+ AVFrame *p = s->p;
+ int i;
+ uint32_t *src = p->data[1];
+
+ s->color_map = av_malloc(3 * sizeof(uint16_t) << s->bpp);
+ if (s->color_map == NULL) {
+ av_log(avctx, AV_LOG_ERROR, "not enough memory\n");
+ return -1;
+ }
+
+ for (i = 0; i < (1 << s->bpp); i++) {
+ uint32_t sample = *src++;
+ s->color_map[(2 << s->bpp) + i] = (sample & 255) * 257; // B
+ sample = sample >> 8;
+ s->color_map[(1 << s->bpp) + i] = (sample & 255) * 257; // G
+ sample = sample >> 8;
+ s->color_map[i] = (sample & 255) * 257; // R
+ }
+ return 0;
+}
+
+static int tiff_write_image_data(AVCodecContext * avctx)
+{
+ TiffEncoderContext *s = avctx->priv_data;
+ AVFrame *p = s->p;
+ int i, y, ret;
+ uint8_t *src = p->data[0];
+#ifdef CONFIG_ZLIB
+ uint8_t *zbuf_start, *zbuf;
+ uLongf zlen;
+#endif
+
+ /* set the compression */
+ switch (avctx->coder_type) {
+ case 0: // XXX: when a user chose vlc it also assigns the default
+ s->compression = TIFF_DEFAULT;
+ break;
+ case FF_CODER_TYPE_RAW:
+ s->compression = TIFF_RAW;
+ break;
+ case FF_CODER_TYPE_RLE:
+ s->compression = TIFF_PACKBITS;
+ break;
+#ifdef CONFIG_ZLIB
+ case FF_CODER_TYPE_DEFLATE:
+ s->compression = TIFF_DEFLATE;
+ break;
+#endif
+ default:
+ av_log(avctx, AV_LOG_ERROR,
+ "Chosen compression not supported\n");
+ return -1;
+ }
+
+ /* compute the number of strips */
+ if (s->compression == TIFF_DEFLATE) {
+ s->lines_per_strip = avctx->height;
+ s->num_of_strips = 1;
+ } else {
+ s->lines_per_strip = (8192 + s->bytes_per_line - 1) / s->bytes_per_line; // =ceil(height/num_of_strips)
+ s->num_of_strips =
+ (avctx->height + s->lines_per_strip - 1) / s->lines_per_strip;
+ }
+
+ /* write the image data */
+ s->strip_offset = av_malloc((s->num_of_strips + 1) * sizeof(uint32_t));
+ s->strip_size = av_malloc((s->num_of_strips + 1) * sizeof(uint32_t));
+ if (s->strip_offset == NULL || s->strip_size == NULL) {
+ av_log(avctx, AV_LOG_ERROR, "not enough memory\n");
+ return -1;
+ }
+ s->strip = 0;
+
+#define COMPRESS(func) \
+ for (y = 0; y < avctx->height; y += s->lines_per_strip) {\
+ int i;\
+ s->strip_offset[s->strip++] = CURR_OFFSET(s);\
+ for (i = 0; (i < s->lines_per_strip) && (y + i < avctx->height); i++) {\
+ func;\
+ src += p->linesize[0];\
+ }\
+ }
+ switch (s->compression) {
+ case TIFF_RAW:
+ if (s->buf_end - s->buf < avctx->height * s->bytes_per_line) {
+ av_log(avctx, AV_LOG_ERROR, "the buffer given is too short\n");
+ return -1;
+ }
+ COMPRESS(bytestream_put_buffer(&s->buf, src, s->bytes_per_line));
+ break;
+
+ case TIFF_PACKBITS:
+ COMPRESS(ret =
+ ff_encode_rle(s->buf, s->buf_end - s->buf, src,
+ s->bytes_per_line, 1, FF_RLE_NEG);
+ if (ret == -1) {
+ av_log(avctx, AV_LOG_ERROR,
+ "the buffer given is too short\n");
+ return -1;
+ }
+ s->buf += ret;);
+ break;
+
+#ifdef CONFIG_ZLIB
+ case TIFF_DEFLATE:
+ zbuf_start = zbuf =
+ av_malloc(s->bytes_per_line * s->lines_per_strip);
+ if (zbuf == NULL) {
+ av_log(avctx, AV_LOG_ERROR, "not enough memory\n");
+ return -1;
+ }
+ for (y = 0; y < avctx->height; y++) {
+ bytestream_put_buffer(&zbuf, src, s->bytes_per_line);
+ src += p->linesize[0];
+ }
+ s->strip_offset[s->strip++] = CURR_OFFSET(s);
+ zlen = s->buf_end - s->buf;
+ ret = compress(s->buf, &zlen, zbuf_start, zbuf - zbuf_start);
+ av_free(zbuf_start);
+ if (ret != Z_OK) {
+ av_log(avctx, AV_LOG_ERROR, "ZLib compression error\n");
+ return -1;
+ }
+ s->buf += zlen;
+ break;
+#endif
+ }
+ s->strip_offset[s->strip] = CURR_OFFSET(s);
+#undef COMPRESS
+
+ /* compute strip sizes */
+ for (i = 0; i < s->strip; i++)
+ s->strip_size[i] = s->strip_offset[i + 1] - s->strip_offset[i];
+
+ return 0;
+}
+
+/**
+ * encoding scheme:
+ * @li [if the client has not set it] TIFF signature
+ * @li offset of the IFD
+ * @li image data [divided into strips]
+ * @li IFD data which doesn't fit inside the IFD
+ * @li IFD, sorted by tag
+ * @li [if the client will not set it] 32 zero bits
+ */
+static int tiff_encode_frame(AVCodecContext * avctx, unsigned char *buf,
+ int buf_size, void *data)
+{
+ TiffEncoderContext *s = avctx->priv_data;
+ const uint8_t header[4] = { 0x49, 0x49, 42, 0 };
+ const uint8_t trailer[4] = { 0, 0, 0, 0 };
+ uint32_t xres[2] = { 72, 1};
+ uint32_t yres[2] = { 72, 1}; // image resolution set to 72 dpi
+ int ret = -1, i;
+
+ s->num_ifd_entries = 0;
+ s->has_color_map = 0;
+ s->buf_start = s->buf = buf;
+ s->buf_end = s->buf + buf_size;
+ s->p = data;
+ bzero(s->bpp_info, 3);
+ switch (avctx->pix_fmt) {
+ case PIX_FMT_PAL8:
+ s->bpp_info[0] = 8;
+ s->photometric_interpretation = 3;
+ s->has_color_map = 1;
+ break;
+ case PIX_FMT_RGB24:
+ s->bpp_info[0] = s->bpp_info[1] = s->bpp_info[2] = 8;
+ s->photometric_interpretation = 2;
+ break;
+ case PIX_FMT_GRAY8:
+ s->bpp_info[0] = 8;
+ s->photometric_interpretation = 1;
+ break;
+ case PIX_FMT_MONOBLACK:
+ s->bpp_info[0] = 1;
+ s->photometric_interpretation = 1;
+ break;
+ case PIX_FMT_MONOWHITE:
+ s->bpp_info[0] = 1;
+ s->photometric_interpretation = 0;
+ break;
+ default:
+ av_log(avctx, AV_LOG_ERROR, "this pix_fmt is not supported\n");
+ return -1;
+ }
+
+ s->bpp = 0;
+ s->nsamples = 0;
+ for (i = 0; i < 3; i++) {
+ s->bpp += s->bpp_info[i];
+ s->nsamples += s->bpp_info[i] > 0;
+ }
+
+ s->bytes_per_line = (avctx->width * s->bpp + 7) / 8;
+
+ bytestream_put_buffer(&s->buf, header, 4);
+
+ s->entry_point = s->buf;
+ s->buf += 4; // a place for next IFD offset [later updated]
+
+ if (s->has_color_map && tiff_prepare_color_map(avctx))
+ goto cleanup;
+
+ /* write the image data */
+ if (tiff_write_image_data(avctx))
+ goto cleanup;
+
+ /* IFD entries */
+ s->num_ifd_entries = 0;
+
+ if (tiff_add_ifd_entry(avctx, TIFF_WIDTH, TIFF_LONG, 1, &avctx->width)||
+ tiff_add_ifd_entry(avctx, TIFF_HEIGHT, TIFF_LONG, 1, &avctx->height)||
+ tiff_add_ifd_entry(avctx, TIFF_BPP, TIFF_SHORT, s->nsamples, s->bpp_info)||
+ tiff_add_ifd_entry(avctx, TIFF_COMPR, TIFF_SHORT, 1, &s->compression)||
+ tiff_add_ifd_entry(avctx, TIFF_INVERT, TIFF_SHORT, 1, &s->photometric_interpretation)||
+ tiff_add_ifd_entry(avctx, TIFF_STRIP_OFFS, TIFF_LONG, s->strip, s->strip_offset)||
+ tiff_add_ifd_entry(avctx, TIFF_SAMPLESPERPIX, TIFF_SHORT, 1, &s->nsamples)||
+ tiff_add_ifd_entry(avctx, TIFF_ROWSPERSTRIP, TIFF_LONG, 1, &s->lines_per_strip)||
+ tiff_add_ifd_entry(avctx, TIFF_STRIP_SIZE, TIFF_LONG, s->strip, s->strip_size)||
+ tiff_add_ifd_entry(avctx, TIFF_XRES, TIFF_RATIONAL, 1, xres)||
+ tiff_add_ifd_entry(avctx, TIFF_YRES, TIFF_RATIONAL, 1, yres)||
+ tiff_add_ifd_entry(avctx, TIFF_RES_UNIT, TIFF_SHORT, 1, (uint16_t[]){2})||
+ ((s->has_color_map) &&
+ tiff_add_ifd_entry(avctx, TIFF_PAL, TIFF_SHORT, 3 << s->bpp, s->color_map))
+ )
+ goto cleanup;
+
+ if (tiff_write_ifd(avctx))
+ goto cleanup;
+
+ bytestream_put_buffer(&s->buf, trailer, 4);
+
+ /* end writing */
+ ret = CURR_OFFSET(s);
+ cleanup:
+ av_free(s->strip_offset);
+ av_free(s->strip_size);
+ av_free(s->color_map);
+
+ return ret;
+}
+
+static int tiff_init(AVCodecContext * avctx)
+{
+ return 0;
+}
+
+static int tiff_end(AVCodecContext * avctx)
+{
+ return 0;
+}
+
+AVCodec tiff_encoder = {
+ "tiff",
+ CODEC_TYPE_VIDEO,
+ CODEC_ID_TIFF,
+ sizeof(TiffEncoderContext),
+ tiff_init,
+ tiff_encode_frame,
+ tiff_end,
+ NULL,
+ 0,
+ NULL,
+ .pix_fmts = (enum PixelFormat[]) {
+ PIX_FMT_RGB24,
+ PIX_FMT_GRAY8,
+ PIX_FMT_MONOWHITE,
+ PIX_FMT_MONOBLACK,
+ PIX_FMT_PAL8,
+ -1},
+};
Index: doc/ffmpeg-doc.texi
===================================================================
--- doc/ffmpeg-doc.texi (wersja 8576)
+++ doc/ffmpeg-doc.texi (kopia robocza)
@@ -920,7 +920,7 @@
@item animated GIF @tab X @tab X @tab Only uncompressed GIFs are generated.
@item PNG @tab X @tab X @tab 2 bit and 4 bit/pixel not supported yet.
@item Targa @tab @tab X @tab Targa (.TGA) image format.
- at item TIFF @tab @tab X @tab Only 24 bit/pixel images are supported.
+ at item TIFF @tab X @tab X @tab
@item SGI @tab X @tab X @tab SGI RGB image format
@end multitable
More information about the ffmpeg-devel
mailing list