[Ffmpeg-devel] [PATCH] TIFF encoder (Google SoC qualification task)
Kamil Nowosad
k.nowosad
Tue Apr 3 11:03:36 CEST 2007
On Tue, Apr 03, 2007 at 10:53:41AM +0200, Kamil Nowosad wrote:
> Hi,
>
> Hope everything's correct ;-)
oops, it had a small mistake. Here's the corrected version.
-------------- next part --------------
Index: Changelog
===================================================================
--- Changelog (wersja 8603)
+++ Changelog (kopia robocza)
@@ -77,6 +77,7 @@
- DNxHD decoder
- Gamecube movie (.THP) playback system
- Blackfin optimizations
+- TIFF encoder
version 0.4.9-pre1:
Index: libavcodec/utils.c
===================================================================
--- libavcodec/utils.c (wersja 8603)
+++ libavcodec/utils.c (kopia robocza)
@@ -633,9 +633,12 @@
{"color_table_id", NULL, OFFSET(color_table_id), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX},
{"internal_buffer_count", NULL, OFFSET(internal_buffer_count), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX},
{"global_quality", NULL, OFFSET(global_quality), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX},
-{"coder", NULL, OFFSET(coder_type), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX, V|E, "coder"},
+{"coder", NULL, OFFSET(coder_type), FF_OPT_TYPE_INT, FF_CODER_TYPE_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 8603)
+++ libavcodec/Makefile (kopia robocza)
@@ -144,6 +144,7 @@
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 tiff.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 8603)
+++ libavcodec/tiff.c (kopia robocza)
@@ -20,49 +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
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,13 +20,17 @@
* 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"
-/* abridged list of TIFF tags */
+/**
+ * TIFF image encoder/decoder
+ * @file tiff.h
+ * @author Konstantin Shishkov
+ * @author Kamil Nowosad
+ */
+#ifndef TIFF_H
+#define TIFF_H
+
+/** abridged list of TIFF tags */
enum TiffTags{
TIFF_WIDTH = 0x100,
TIFF_HEIGHT,
@@ -33,15 +38,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 +70,7 @@
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/allcodecs.c
===================================================================
--- libavcodec/allcodecs.c (wersja 8603)
+++ libavcodec/allcodecs.c (kopia robocza)
@@ -132,7 +132,7 @@
REGISTER_DECODER(THEORA, theora);
REGISTER_DECODER(THP, thp);
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 8603)
+++ libavcodec/avcodec.h (kopia robocza)
@@ -1566,8 +1566,12 @@
*/
int global_quality;
-#define FF_CODER_TYPE_VLC 0
-#define FF_CODER_TYPE_AC 1
+#define FF_CODER_TYPE_DEFAULT -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
+#define FF_CODER_TYPE_DEFLATE 4
/**
* coder type
* - encoding: set by user.
@@ -2222,6 +2226,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,414 @@
+/*
+ * 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;
+
+ 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 void 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;
+ const static uint8_t type_sizes[] = {0, 1, 100, 2, 4, 8};
+
+ size = type_sizes[type] * count;
+
+ assert(s->num_ifd_entries < TIFF_MAX_IFD_ENTRIES);
+
+ 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) {
+ /** check size; to simplify, s->buf is set to s->buf_end+1
+ * and the code breaks on the next size check */
+ if (s->buf_end - s->buf < size) {
+ s->buf = s->buf_end + 1;
+ return;
+ }
+ 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++;
+}
+
+/**
+ * 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_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;
+ uint32_t zlen;
+#endif
+
+ /** set the compression */
+ switch (avctx->coder_type) {
+ case FF_CODER_TYPE_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 {
+ /** =ceil(height/num_of_strips) */
+ s->lines_per_strip = (8192 + s->bytes_per_line - 1) / s->bytes_per_line;
+ 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;
+
+ 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;
+ }
+ 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++) {
+ bytestream_put_buffer(&s->buf, src, s->bytes_per_line);
+ src += p->linesize[0];
+ }
+ }
+ break;
+ case TIFF_PACKBITS:
+ 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++) {
+ ret = ff_rle_encode(s->buf, s->buf_end - s->buf, src, 1,
+ s->bytes_per_line, 2, 0xff, -1, 0);
+ if (ret == -1) {
+ av_log(avctx, AV_LOG_ERROR,
+ "the buffer given is too short\n");
+ return -1;
+ }
+ s->buf += ret;
+ src += p->linesize[0];
+ }
+ }
+ 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 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 32 zero bits
+ */
+static int tiff_encode_frame(AVCodecContext * avctx, unsigned char *buf,
+ int buf_size, void *data)
+{
+ TiffEncoderContext *s = avctx->priv_data;
+ const static uint8_t header[4] = {0x49, 0x49, 42, 0};
+ const static uint8_t trailer[4] = { 0, 0, 0, 0};
+ const static uint32_t xres[2] = {72, 1};
+ const static uint32_t yres[2] = {72, 1}; ///< image resolution set to 72 dpi
+ int i, ret = -1;
+
+ s->num_ifd_entries = 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;
+ 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");
+ goto cleanup;
+ }
+
+ 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 (avctx->pix_fmt == PIX_FMT_PAL8){
+ AVFrame *p = s->p;
+ uint32_t *src = p->data[1];
+
+ for (i = 0; i < 256; i++) {
+ uint32_t sample = *src++;
+ s->color_map[2*256 + i] = (sample & 255) * 257; ///< B
+ sample = sample >> 8;
+ s->color_map[256 + i] = (sample & 255) * 257; ///< G
+ sample = sample >> 8;
+ s->color_map[i] = (sample & 255) * 257; ///< R
+ }
+ }
+
+ /** write the image data */
+ if (tiff_write_image_data(avctx))
+ goto cleanup;
+
+ /** IFD entries */
+ s->num_ifd_entries = 0;
+
+ 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});
+ if (avctx->pix_fmt == PIX_FMT_PAL8)
+ tiff_add_ifd_entry(avctx, TIFF_PAL, TIFF_SHORT, 3 << s->bpp, s->color_map);
+
+ if (tiff_write_ifd(avctx))
+ goto cleanup;
+
+ bytestream_put_buffer(&s->buf, trailer, 4);
+
+ ret = CURR_OFFSET(s);
+cleanup:
+ av_free(s->strip_offset);
+ av_free(s->strip_size);
+
+ return ret;
+}
+
+static int tiff_init(AVCodecContext * avctx)
+{
+ TiffEncoderContext *s = avctx->priv_data;
+
+ if (avctx->pix_fmt == PIX_FMT_PAL8){
+ s->color_map = av_malloc(768 * sizeof(uint16_t));
+ if (s->color_map == NULL) {
+ av_log(avctx, AV_LOG_ERROR, "not enough memory\n");
+ return -1;
+ }
+ }
+ return 0;
+}
+
+static int tiff_end(AVCodecContext * avctx)
+{
+ TiffEncoderContext *s = avctx->priv_data;
+
+ av_free(s->color_map);
+ 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 8603)
+++ doc/ffmpeg-doc.texi (kopia robocza)
@@ -922,7 +922,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