00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/imgutils.h"
00023 #include "libavutil/avassert.h"
00024 #include "avcodec.h"
00025 #include "bytestream.h"
00026 #include "sgi.h"
00027
00028 typedef struct SgiState {
00029 AVFrame picture;
00030 unsigned int width;
00031 unsigned int height;
00032 unsigned int depth;
00033 unsigned int bytes_per_channel;
00034 int linesize;
00035 GetByteContext g;
00036 } SgiState;
00037
00046 static int expand_rle_row(SgiState *s, uint8_t *out_buf,
00047 uint8_t *out_end, int pixelstride)
00048 {
00049 unsigned char pixel, count;
00050 unsigned char *orig = out_buf;
00051
00052 while (out_buf < out_end) {
00053 if (bytestream2_get_bytes_left(&s->g) < 1)
00054 return AVERROR_INVALIDDATA;
00055 pixel = bytestream2_get_byteu(&s->g);
00056 if (!(count = (pixel & 0x7f))) {
00057 break;
00058 }
00059
00060
00061 if(out_buf + pixelstride * (count-1) >= out_end) return -1;
00062
00063 if (pixel & 0x80) {
00064 while (count--) {
00065 *out_buf = bytestream2_get_byte(&s->g);
00066 out_buf += pixelstride;
00067 }
00068 } else {
00069 pixel = bytestream2_get_byte(&s->g);
00070
00071 while (count--) {
00072 *out_buf = pixel;
00073 out_buf += pixelstride;
00074 }
00075 }
00076 }
00077 return (out_buf - orig) / pixelstride;
00078 }
00079
00086 static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
00087 {
00088 uint8_t *dest_row;
00089 unsigned int len = s->height * s->depth * 4;
00090 GetByteContext g_table = s->g;
00091 unsigned int y, z;
00092 unsigned int start_offset;
00093
00094
00095 if (len * 2 > bytestream2_get_bytes_left(&s->g)) {
00096 return AVERROR_INVALIDDATA;
00097 }
00098
00099 for (z = 0; z < s->depth; z++) {
00100 dest_row = out_buf;
00101 for (y = 0; y < s->height; y++) {
00102 dest_row -= s->linesize;
00103 start_offset = bytestream2_get_be32(&g_table);
00104 bytestream2_seek(&s->g, start_offset, SEEK_SET);
00105 if (expand_rle_row(s, dest_row + z, dest_row + s->width*s->depth,
00106 s->depth) != s->width) {
00107 return AVERROR_INVALIDDATA;
00108 }
00109 }
00110 }
00111 return 0;
00112 }
00113
00120 static int read_uncompressed_sgi(unsigned char* out_buf, SgiState *s)
00121 {
00122 int x, y, z;
00123 unsigned int offset = s->height * s->width * s->bytes_per_channel;
00124 GetByteContext gp[4];
00125 uint8_t *out_end;
00126
00127
00128 if (offset * s->depth > bytestream2_get_bytes_left(&s->g))
00129 return AVERROR_INVALIDDATA;
00130
00131
00132 for (z = 0; z < s->depth; z++) {
00133 gp[z] = s->g;
00134 bytestream2_skip(&gp[z], z * offset);
00135 }
00136
00137 for (y = s->height - 1; y >= 0; y--) {
00138 out_end = out_buf + (y * s->linesize);
00139 if (s->bytes_per_channel == 1) {
00140 for (x = s->width; x > 0; x--)
00141 for (z = 0; z < s->depth; z++)
00142 *out_end++ = bytestream2_get_byteu(&gp[z]);
00143 } else {
00144 uint16_t *out16 = (uint16_t *)out_end;
00145 for (x = s->width; x > 0; x--)
00146 for (z = 0; z < s->depth; z++)
00147 *out16++ = bytestream2_get_ne16u(&gp[z]);
00148 }
00149 }
00150 return 0;
00151 }
00152
00153 static int decode_frame(AVCodecContext *avctx,
00154 void *data, int *data_size,
00155 AVPacket *avpkt)
00156 {
00157 SgiState *s = avctx->priv_data;
00158 AVFrame *picture = data;
00159 AVFrame *p = &s->picture;
00160 unsigned int dimension, rle;
00161 int ret = 0;
00162 uint8_t *out_buf, *out_end;
00163
00164 bytestream2_init(&s->g, avpkt->data, avpkt->size);
00165 if (bytestream2_get_bytes_left(&s->g) < SGI_HEADER_SIZE) {
00166 av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
00167 return AVERROR_INVALIDDATA;
00168 }
00169
00170
00171 if (bytestream2_get_be16(&s->g) != SGI_MAGIC) {
00172 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
00173 return AVERROR_INVALIDDATA;
00174 }
00175
00176 rle = bytestream2_get_byte(&s->g);
00177 s->bytes_per_channel = bytestream2_get_byte(&s->g);
00178 dimension = bytestream2_get_be16(&s->g);
00179 s->width = bytestream2_get_be16(&s->g);
00180 s->height = bytestream2_get_be16(&s->g);
00181 s->depth = bytestream2_get_be16(&s->g);
00182
00183 if (s->bytes_per_channel != 1 && (s->bytes_per_channel != 2 || rle)) {
00184 av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
00185 return -1;
00186 }
00187
00188
00189 if (dimension != 2 && dimension != 3) {
00190 av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
00191 return -1;
00192 }
00193
00194 if (s->depth == SGI_GRAYSCALE) {
00195 avctx->pix_fmt = s->bytes_per_channel == 2 ? PIX_FMT_GRAY16BE : PIX_FMT_GRAY8;
00196 } else if (s->depth == SGI_RGB) {
00197 avctx->pix_fmt = s->bytes_per_channel == 2 ? PIX_FMT_RGB48BE : PIX_FMT_RGB24;
00198 } else if (s->depth == SGI_RGBA) {
00199 avctx->pix_fmt = s->bytes_per_channel == 2 ? PIX_FMT_RGBA64BE : PIX_FMT_RGBA;
00200 } else {
00201 av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
00202 return -1;
00203 }
00204
00205 if (av_image_check_size(s->width, s->height, 0, avctx))
00206 return -1;
00207 avcodec_set_dimensions(avctx, s->width, s->height);
00208
00209 if (p->data[0])
00210 avctx->release_buffer(avctx, p);
00211
00212 p->reference = 0;
00213 if (avctx->get_buffer(avctx, p) < 0) {
00214 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed.\n");
00215 return -1;
00216 }
00217
00218 p->pict_type = AV_PICTURE_TYPE_I;
00219 p->key_frame = 1;
00220 out_buf = p->data[0];
00221
00222 out_end = out_buf + p->linesize[0] * s->height;
00223
00224 s->linesize = p->linesize[0];
00225
00226
00227 bytestream2_seek(&s->g, SGI_HEADER_SIZE, SEEK_SET);
00228 if (rle) {
00229 ret = read_rle_sgi(out_end, s);
00230 } else {
00231 ret = read_uncompressed_sgi(out_buf, s);
00232 }
00233
00234 if (ret == 0) {
00235 *picture = s->picture;
00236 *data_size = sizeof(AVPicture);
00237 return avpkt->size;
00238 } else {
00239 return ret;
00240 }
00241 }
00242
00243 static av_cold int sgi_init(AVCodecContext *avctx){
00244 SgiState *s = avctx->priv_data;
00245
00246 avcodec_get_frame_defaults(&s->picture);
00247 avctx->coded_frame = &s->picture;
00248
00249 return 0;
00250 }
00251
00252 static av_cold int sgi_end(AVCodecContext *avctx)
00253 {
00254 SgiState * const s = avctx->priv_data;
00255
00256 if (s->picture.data[0])
00257 avctx->release_buffer(avctx, &s->picture);
00258
00259 return 0;
00260 }
00261
00262 AVCodec ff_sgi_decoder = {
00263 .name = "sgi",
00264 .type = AVMEDIA_TYPE_VIDEO,
00265 .id = AV_CODEC_ID_SGI,
00266 .priv_data_size = sizeof(SgiState),
00267 .init = sgi_init,
00268 .close = sgi_end,
00269 .decode = decode_frame,
00270 .capabilities = CODEC_CAP_DR1,
00271 .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
00272 };