00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00087 bytestream_put_be16(&buf, SGI_MAGIC);
00088 bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW);
00089 bytestream_put_byte(&buf, 1);
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
00096 bytestream_put_be32(&buf, 0L);
00097 bytestream_put_be32(&buf, 255L);
00098 bytestream_put_be32(&buf, 0L);
00099
00100
00101 memset(buf, 0, SGI_HEADER_SIZE);
00102 buf += 80;
00103
00104
00105 bytestream_put_be32(&buf, 0L);
00106
00107
00108 buf += 404;
00109 offsettab = buf;
00110
00111 if (avctx->coder_type != FF_CODER_TYPE_RAW) {
00112
00113 buf += tablesize;
00114 lengthtab = buf;
00115
00116
00117 buf += tablesize;
00118
00119
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
00159 return buf - orig_buf;
00160 }
00161
00162 AVCodec ff_sgi_encoder = {
00163 .name = "sgi",
00164 .type = AVMEDIA_TYPE_VIDEO,
00165 .id = CODEC_ID_SGI,
00166 .priv_data_size = sizeof(SgiContext),
00167 .init = encode_init,
00168 .encode = encode_frame,
00169 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA, PIX_FMT_GRAY8, PIX_FMT_NONE},
00170 .long_name= NULL_IF_CONFIG_SMALL("SGI image"),
00171 };