00001 /* 00002 * SGI image encoder 00003 * Todd Kirby <doubleshot@pacbell.net> 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 SgiContext *s = avctx->priv_data; 00036 00037 avcodec_get_frame_defaults(&s->picture); 00038 avctx->coded_frame = &s->picture; 00039 00040 return 0; 00041 } 00042 00043 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, 00044 int buf_size, void *data) { 00045 SgiContext *s = avctx->priv_data; 00046 AVFrame * const p = &s->picture; 00047 uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf; 00048 int x, y, z, length, tablesize; 00049 unsigned int width, height, depth, dimension; 00050 unsigned char *orig_buf = buf, *end_buf = buf + buf_size; 00051 00052 *p = *(AVFrame*)data; 00053 p->pict_type = FF_I_TYPE; 00054 p->key_frame = 1; 00055 00056 width = avctx->width; 00057 height = avctx->height; 00058 00059 switch (avctx->pix_fmt) { 00060 case PIX_FMT_GRAY8: 00061 dimension = SGI_SINGLE_CHAN; 00062 depth = SGI_GRAYSCALE; 00063 break; 00064 case PIX_FMT_RGB24: 00065 dimension = SGI_MULTI_CHAN; 00066 depth = SGI_RGB; 00067 break; 00068 case PIX_FMT_RGBA: 00069 dimension = SGI_MULTI_CHAN; 00070 depth = SGI_RGBA; 00071 break; 00072 default: 00073 return AVERROR_INVALIDDATA; 00074 } 00075 00076 tablesize = depth * height * 4; 00077 length = tablesize * 2 + SGI_HEADER_SIZE; 00078 00079 if (buf_size < length) { 00080 av_log(avctx, AV_LOG_ERROR, "buf_size too small(need %d, got %d)\n", length, buf_size); 00081 return -1; 00082 } 00083 00084 /* Encode header. */ 00085 bytestream_put_be16(&buf, SGI_MAGIC); 00086 bytestream_put_byte(&buf, 1); /* RLE */ 00087 bytestream_put_byte(&buf, 1); /* bytes_per_channel */ 00088 bytestream_put_be16(&buf, dimension); 00089 bytestream_put_be16(&buf, width); 00090 bytestream_put_be16(&buf, height); 00091 bytestream_put_be16(&buf, depth); 00092 00093 /* The rest are constant in this implementation. */ 00094 bytestream_put_be32(&buf, 0L); /* pixmin */ 00095 bytestream_put_be32(&buf, 255L); /* pixmax */ 00096 bytestream_put_be32(&buf, 0L); /* dummy */ 00097 00098 /* name */ 00099 memset(buf, 0, SGI_HEADER_SIZE); 00100 buf += 80; 00101 00102 /* colormap */ 00103 bytestream_put_be32(&buf, 0L); 00104 00105 /* The rest of the 512 byte header is unused. */ 00106 buf += 404; 00107 offsettab = buf; 00108 00109 /* Skip RLE offset table. */ 00110 buf += tablesize; 00111 lengthtab = buf; 00112 00113 /* Skip RLE length table. */ 00114 buf += tablesize; 00115 00116 /* Make an intermediate consecutive buffer. */ 00117 if ((encode_buf = av_malloc(width)) == NULL) 00118 return -1; 00119 00120 for (z = 0; z < depth; z++) { 00121 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z; 00122 00123 for (y = 0; y < height; y++) { 00124 bytestream_put_be32(&offsettab, buf - orig_buf); 00125 00126 for (x = 0; x < width; x++) 00127 encode_buf[x] = in_buf[depth * x]; 00128 00129 if((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) { 00130 av_free(encode_buf); 00131 return -1; 00132 } 00133 00134 buf += length; 00135 bytestream_put_byte(&buf, 0); 00136 bytestream_put_be32(&lengthtab, length + 1); 00137 in_buf -= p->linesize[0]; 00138 } 00139 } 00140 00141 av_free(encode_buf); 00142 /* total length */ 00143 return buf - orig_buf; 00144 } 00145 00146 AVCodec sgi_encoder = { 00147 "sgi", 00148 CODEC_TYPE_VIDEO, 00149 CODEC_ID_SGI, 00150 sizeof(SgiContext), 00151 encode_init, 00152 encode_frame, 00153 NULL, 00154 .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA, PIX_FMT_PAL8, PIX_FMT_GRAY8, PIX_FMT_NONE}, 00155 .long_name= NULL_IF_CONFIG_SMALL("SGI image"), 00156 }; 00157