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 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
00085 bytestream_put_be16(&buf, SGI_MAGIC);
00086 bytestream_put_byte(&buf, 1);
00087 bytestream_put_byte(&buf, 1);
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
00094 bytestream_put_be32(&buf, 0L);
00095 bytestream_put_be32(&buf, 255L);
00096 bytestream_put_be32(&buf, 0L);
00097
00098
00099 memset(buf, 0, SGI_HEADER_SIZE);
00100 buf += 80;
00101
00102
00103 bytestream_put_be32(&buf, 0L);
00104
00105
00106 buf += 404;
00107 offsettab = buf;
00108
00109
00110 buf += tablesize;
00111 lengthtab = buf;
00112
00113
00114 buf += tablesize;
00115
00116
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
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