Libav 0.7.1
|
00001 /* 00002 * SGI image decoder 00003 * Todd Kirby <doubleshot@pacbell.net> 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav 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 * Libav 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 Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #include "libavutil/imgutils.h" 00023 #include "avcodec.h" 00024 #include "bytestream.h" 00025 #include "sgi.h" 00026 00027 typedef struct SgiState { 00028 AVFrame picture; 00029 unsigned int width; 00030 unsigned int height; 00031 unsigned int depth; 00032 unsigned int bytes_per_channel; 00033 int linesize; 00034 } SgiState; 00035 00045 static int expand_rle_row(const uint8_t *in_buf, const uint8_t* in_end, 00046 unsigned char *out_buf, uint8_t* out_end, int pixelstride) 00047 { 00048 unsigned char pixel, count; 00049 unsigned char *orig = out_buf; 00050 00051 while (1) { 00052 if(in_buf + 1 > in_end) return -1; 00053 pixel = bytestream_get_byte(&in_buf); 00054 if (!(count = (pixel & 0x7f))) { 00055 return (out_buf - orig) / pixelstride; 00056 } 00057 00058 /* Check for buffer overflow. */ 00059 if(out_buf + pixelstride * count >= out_end) return -1; 00060 00061 if (pixel & 0x80) { 00062 while (count--) { 00063 *out_buf = bytestream_get_byte(&in_buf); 00064 out_buf += pixelstride; 00065 } 00066 } else { 00067 pixel = bytestream_get_byte(&in_buf); 00068 00069 while (count--) { 00070 *out_buf = pixel; 00071 out_buf += pixelstride; 00072 } 00073 } 00074 } 00075 } 00076 00085 static int read_rle_sgi(unsigned char* out_buf, const uint8_t *in_buf, 00086 const uint8_t *in_end, SgiState* s) 00087 { 00088 uint8_t *dest_row; 00089 unsigned int len = s->height * s->depth * 4; 00090 const uint8_t *start_table = in_buf; 00091 unsigned int y, z; 00092 unsigned int start_offset; 00093 00094 /* size of RLE offset and length tables */ 00095 if(len * 2 > in_end - in_buf) { 00096 return AVERROR_INVALIDDATA; 00097 } 00098 00099 in_buf -= SGI_HEADER_SIZE; 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 = bytestream_get_be32(&start_table); 00105 if(start_offset > in_end - in_buf) { 00106 return AVERROR_INVALIDDATA; 00107 } 00108 if (expand_rle_row(in_buf + start_offset, in_end, dest_row + z, 00109 dest_row + FFABS(s->linesize), s->depth) != s->width) 00110 return AVERROR_INVALIDDATA; 00111 } 00112 } 00113 return 0; 00114 } 00115 00125 static int read_uncompressed_sgi(unsigned char* out_buf, uint8_t* out_end, 00126 const uint8_t *in_buf, const uint8_t *in_end, SgiState* s) 00127 { 00128 int x, y, z; 00129 const uint8_t *ptr; 00130 unsigned int offset = s->height * s->width * s->bytes_per_channel; 00131 00132 /* Test buffer size. */ 00133 if (offset * s->depth > in_end - in_buf) { 00134 return -1; 00135 } 00136 00137 for (y = s->height - 1; y >= 0; y--) { 00138 out_end = out_buf + (y * s->linesize); 00139 for (x = s->width; x > 0; x--) { 00140 ptr = in_buf += s->bytes_per_channel; 00141 for(z = 0; z < s->depth; z ++) { 00142 memcpy(out_end, ptr, s->bytes_per_channel); 00143 out_end += s->bytes_per_channel; 00144 ptr += offset; 00145 } 00146 } 00147 } 00148 return 0; 00149 } 00150 00151 static int decode_frame(AVCodecContext *avctx, 00152 void *data, int *data_size, 00153 AVPacket *avpkt) 00154 { 00155 const uint8_t *in_buf = avpkt->data; 00156 int buf_size = avpkt->size; 00157 SgiState *s = avctx->priv_data; 00158 AVFrame *picture = data; 00159 AVFrame *p = &s->picture; 00160 const uint8_t *in_end = in_buf + buf_size; 00161 unsigned int dimension, rle; 00162 int ret = 0; 00163 uint8_t *out_buf, *out_end; 00164 00165 if (buf_size < SGI_HEADER_SIZE){ 00166 av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", buf_size); 00167 return -1; 00168 } 00169 00170 /* Test for SGI magic. */ 00171 if (bytestream_get_be16(&in_buf) != SGI_MAGIC) { 00172 av_log(avctx, AV_LOG_ERROR, "bad magic number\n"); 00173 return -1; 00174 } 00175 00176 rle = bytestream_get_byte(&in_buf); 00177 s->bytes_per_channel = bytestream_get_byte(&in_buf); 00178 dimension = bytestream_get_be16(&in_buf); 00179 s->width = bytestream_get_be16(&in_buf); 00180 s->height = bytestream_get_be16(&in_buf); 00181 s->depth = bytestream_get_be16(&in_buf); 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 /* Check for supported image dimensions. */ 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 && s->bytes_per_channel == 1) { 00199 avctx->pix_fmt = 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 /* Skip header. */ 00227 in_buf += SGI_HEADER_SIZE - 12; 00228 if (rle) { 00229 ret = read_rle_sgi(out_end, in_buf, in_end, s); 00230 } else { 00231 ret = read_uncompressed_sgi(out_buf, out_end, in_buf, in_end, s); 00232 } 00233 00234 if (ret == 0) { 00235 *picture = s->picture; 00236 *data_size = sizeof(AVPicture); 00237 return buf_size; 00238 } else { 00239 return -1; 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 "sgi", 00264 AVMEDIA_TYPE_VIDEO, 00265 CODEC_ID_SGI, 00266 sizeof(SgiState), 00267 sgi_init, 00268 NULL, 00269 sgi_end, 00270 decode_frame, 00271 .long_name = NULL_IF_CONFIG_SMALL("SGI image"), 00272 }; 00273