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