Libav 0.7.1
|
00001 /* 00002 * SGI image encoder 00003 * Todd Kirby <doubleshot@pacbell.net> 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #include "avcodec.h" 00023 #include "bytestream.h" 00024 #include "sgi.h" 00025 #include "rle.h" 00026 00027 #define SGI_SINGLE_CHAN 2 00028 #define SGI_MULTI_CHAN 3 00029 00030 typedef struct SgiContext { 00031 AVFrame picture; 00032 } SgiContext; 00033 00034 static av_cold int encode_init(AVCodecContext *avctx) 00035 { 00036 SgiContext *s = avctx->priv_data; 00037 00038 avcodec_get_frame_defaults(&s->picture); 00039 avctx->coded_frame = &s->picture; 00040 00041 return 0; 00042 } 00043 00044 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, 00045 int buf_size, void *data) 00046 { 00047 SgiContext *s = avctx->priv_data; 00048 AVFrame * const p = &s->picture; 00049 uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf; 00050 int x, y, z, length, tablesize; 00051 unsigned int width, height, depth, dimension; 00052 unsigned char *orig_buf = buf, *end_buf = buf + buf_size; 00053 00054 *p = *(AVFrame*)data; 00055 p->pict_type = AV_PICTURE_TYPE_I; 00056 p->key_frame = 1; 00057 00058 width = avctx->width; 00059 height = avctx->height; 00060 00061 switch (avctx->pix_fmt) { 00062 case PIX_FMT_GRAY8: 00063 dimension = SGI_SINGLE_CHAN; 00064 depth = SGI_GRAYSCALE; 00065 break; 00066 case PIX_FMT_RGB24: 00067 dimension = SGI_MULTI_CHAN; 00068 depth = SGI_RGB; 00069 break; 00070 case PIX_FMT_RGBA: 00071 dimension = SGI_MULTI_CHAN; 00072 depth = SGI_RGBA; 00073 break; 00074 default: 00075 return AVERROR_INVALIDDATA; 00076 } 00077 00078 tablesize = depth * height * 4; 00079 length = tablesize * 2 + SGI_HEADER_SIZE; 00080 00081 if (buf_size < length) { 00082 av_log(avctx, AV_LOG_ERROR, "buf_size too small(need %d, got %d)\n", length, buf_size); 00083 return -1; 00084 } 00085 00086 /* Encode header. */ 00087 bytestream_put_be16(&buf, SGI_MAGIC); 00088 bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/ 00089 bytestream_put_byte(&buf, 1); /* bytes_per_channel */ 00090 bytestream_put_be16(&buf, dimension); 00091 bytestream_put_be16(&buf, width); 00092 bytestream_put_be16(&buf, height); 00093 bytestream_put_be16(&buf, depth); 00094 00095 /* The rest are constant in this implementation. */ 00096 bytestream_put_be32(&buf, 0L); /* pixmin */ 00097 bytestream_put_be32(&buf, 255L); /* pixmax */ 00098 bytestream_put_be32(&buf, 0L); /* dummy */ 00099 00100 /* name */ 00101 memset(buf, 0, SGI_HEADER_SIZE); 00102 buf += 80; 00103 00104 /* colormap */ 00105 bytestream_put_be32(&buf, 0L); 00106 00107 /* The rest of the 512 byte header is unused. */ 00108 buf += 404; 00109 offsettab = buf; 00110 00111 if (avctx->coder_type != FF_CODER_TYPE_RAW) { 00112 /* Skip RLE offset table. */ 00113 buf += tablesize; 00114 lengthtab = buf; 00115 00116 /* Skip RLE length table. */ 00117 buf += tablesize; 00118 00119 /* Make an intermediate consecutive buffer. */ 00120 if (!(encode_buf = av_malloc(width))) 00121 return -1; 00122 00123 for (z = 0; z < depth; z++) { 00124 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z; 00125 00126 for (y = 0; y < height; y++) { 00127 bytestream_put_be32(&offsettab, buf - orig_buf); 00128 00129 for (x = 0; x < width; x++) 00130 encode_buf[x] = in_buf[depth * x]; 00131 00132 if ((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) { 00133 av_free(encode_buf); 00134 return -1; 00135 } 00136 00137 buf += length; 00138 bytestream_put_byte(&buf, 0); 00139 bytestream_put_be32(&lengthtab, length + 1); 00140 in_buf -= p->linesize[0]; 00141 } 00142 } 00143 00144 av_free(encode_buf); 00145 } else { 00146 for (z = 0; z < depth; z++) { 00147 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z; 00148 00149 for (y = 0; y < height; y++) { 00150 for (x = 0; x < width * depth; x += depth) 00151 bytestream_put_byte(&buf, in_buf[x]); 00152 00153 in_buf -= p->linesize[0]; 00154 } 00155 } 00156 } 00157 00158 /* total length */ 00159 return buf - orig_buf; 00160 } 00161 00162 AVCodec ff_sgi_encoder = { 00163 "sgi", 00164 AVMEDIA_TYPE_VIDEO, 00165 CODEC_ID_SGI, 00166 sizeof(SgiContext), 00167 encode_init, 00168 encode_frame, 00169 NULL, 00170 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA, PIX_FMT_GRAY8, PIX_FMT_NONE}, 00171 .long_name= NULL_IF_CONFIG_SMALL("SGI image"), 00172 };