• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/sgidec.c

Go to the documentation of this file.
00001 /*
00002  * SGI image decoder
00003  * Todd Kirby <doubleshot@pacbell.net>
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "avcodec.h"
00023 #include "bytestream.h"
00024 #include "sgi.h"
00025 
00026 typedef struct SgiState {
00027     AVFrame picture;
00028     unsigned int width;
00029     unsigned int height;
00030     unsigned int depth;
00031     unsigned int bytes_per_channel;
00032     int linesize;
00033 } SgiState;
00034 
00044 static int expand_rle_row(const uint8_t *in_buf, const uint8_t* in_end,
00045             unsigned char *out_buf, uint8_t* out_end, int pixelstride)
00046 {
00047     unsigned char pixel, count;
00048     unsigned char *orig = out_buf;
00049 
00050     while (1) {
00051         if(in_buf + 1 > in_end) return -1;
00052         pixel = bytestream_get_byte(&in_buf);
00053         if (!(count = (pixel & 0x7f))) {
00054             return (out_buf - orig) / pixelstride;
00055         }
00056 
00057         /* Check for buffer overflow. */
00058         if(out_buf + pixelstride * count >= out_end) return -1;
00059 
00060         if (pixel & 0x80) {
00061             while (count--) {
00062                 *out_buf = bytestream_get_byte(&in_buf);
00063                 out_buf += pixelstride;
00064             }
00065         } else {
00066             pixel = bytestream_get_byte(&in_buf);
00067 
00068             while (count--) {
00069                 *out_buf = pixel;
00070                 out_buf += pixelstride;
00071             }
00072         }
00073     }
00074 }
00075 
00084 static int read_rle_sgi(unsigned char* out_buf, const uint8_t *in_buf,
00085                         const uint8_t *in_end, SgiState* s)
00086 {
00087     uint8_t *dest_row;
00088     unsigned int len = s->height * s->depth * 4;
00089     const uint8_t *start_table = in_buf;
00090     unsigned int y, z;
00091     unsigned int start_offset;
00092 
00093     /* size of  RLE offset and length tables */
00094     if(len * 2  > in_end - in_buf) {
00095         return AVERROR_INVALIDDATA;
00096     }
00097 
00098     in_buf -= SGI_HEADER_SIZE;
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 = bytestream_get_be32(&start_table);
00104             if(start_offset > in_end - in_buf) {
00105                 return AVERROR_INVALIDDATA;
00106             }
00107             if (expand_rle_row(in_buf + start_offset, in_end, dest_row + z,
00108                 dest_row + FFABS(s->linesize), s->depth) != s->width)
00109                 return AVERROR_INVALIDDATA;
00110         }
00111     }
00112     return 0;
00113 }
00114 
00124 static int read_uncompressed_sgi(unsigned char* out_buf, uint8_t* out_end,
00125                 const uint8_t *in_buf, const uint8_t *in_end, SgiState* s)
00126 {
00127     int x, y, z;
00128     const uint8_t *ptr;
00129     unsigned int offset = s->height * s->width * s->bytes_per_channel;
00130 
00131     /* Test buffer size. */
00132     if (offset * s->depth > in_end - in_buf) {
00133        return -1;
00134     }
00135 
00136     for (y = s->height - 1; y >= 0; y--) {
00137         out_end = out_buf + (y * s->linesize);
00138         for (x = s->width; x > 0; x--) {
00139             ptr = in_buf += s->bytes_per_channel;
00140             for(z = 0; z < s->depth; z ++) {
00141                 memcpy(out_end, ptr, s->bytes_per_channel);
00142                 out_end += s->bytes_per_channel;
00143                 ptr += offset;
00144             }
00145         }
00146     }
00147     return 0;
00148 }
00149 
00150 static int decode_frame(AVCodecContext *avctx,
00151                         void *data, int *data_size,
00152                         AVPacket *avpkt)
00153 {
00154     const uint8_t *in_buf = avpkt->data;
00155     int buf_size = avpkt->size;
00156     SgiState *s = avctx->priv_data;
00157     AVFrame *picture = data;
00158     AVFrame *p = &s->picture;
00159     const uint8_t *in_end = in_buf + buf_size;
00160     unsigned int dimension, rle;
00161     int ret = 0;
00162     uint8_t *out_buf, *out_end;
00163 
00164     if (buf_size < SGI_HEADER_SIZE){
00165         av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", buf_size);
00166         return -1;
00167     }
00168 
00169     /* Test for SGI magic. */
00170     if (bytestream_get_be16(&in_buf) != SGI_MAGIC) {
00171         av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
00172         return -1;
00173     }
00174 
00175     rle = bytestream_get_byte(&in_buf);
00176     s->bytes_per_channel = bytestream_get_byte(&in_buf);
00177     dimension = bytestream_get_be16(&in_buf);
00178     s->width  = bytestream_get_be16(&in_buf);
00179     s->height = bytestream_get_be16(&in_buf);
00180     s->depth  = bytestream_get_be16(&in_buf);
00181 
00182     if (s->bytes_per_channel != 1 && (s->bytes_per_channel != 2 || rle)) {
00183         av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
00184         return -1;
00185     }
00186 
00187     /* Check for supported image dimensions. */
00188     if (dimension != 2 && dimension != 3) {
00189         av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
00190         return -1;
00191     }
00192 
00193     if (s->depth == SGI_GRAYSCALE) {
00194         avctx->pix_fmt = s->bytes_per_channel == 2 ? PIX_FMT_GRAY16BE : PIX_FMT_GRAY8;
00195     } else if (s->depth == SGI_RGB) {
00196         avctx->pix_fmt = s->bytes_per_channel == 2 ? PIX_FMT_RGB48BE : PIX_FMT_RGB24;
00197     } else if (s->depth == SGI_RGBA && s->bytes_per_channel == 1) {
00198         avctx->pix_fmt = PIX_FMT_RGBA;
00199     } else {
00200         av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
00201         return -1;
00202     }
00203 
00204     if (avcodec_check_dimensions(avctx, s->width, s->height))
00205         return -1;
00206     avcodec_set_dimensions(avctx, s->width, s->height);
00207 
00208     if (p->data[0])
00209         avctx->release_buffer(avctx, p);
00210 
00211     p->reference = 0;
00212     if (avctx->get_buffer(avctx, p) < 0) {
00213         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed.\n");
00214         return -1;
00215     }
00216 
00217     p->pict_type = FF_I_TYPE;
00218     p->key_frame = 1;
00219     out_buf = p->data[0];
00220 
00221     out_end = out_buf + p->linesize[0] * s->height;
00222 
00223     s->linesize = p->linesize[0];
00224 
00225     /* Skip header. */
00226     in_buf += SGI_HEADER_SIZE - 12;
00227     if (rle) {
00228         ret = read_rle_sgi(out_end, in_buf, in_end, s);
00229     } else {
00230         ret = read_uncompressed_sgi(out_buf, out_end, in_buf, in_end, s);
00231     }
00232 
00233     if (ret == 0) {
00234         *picture   = s->picture;
00235         *data_size = sizeof(AVPicture);
00236         return buf_size;
00237     } else {
00238         return -1;
00239     }
00240 }
00241 
00242 static av_cold int sgi_init(AVCodecContext *avctx){
00243     SgiState *s = avctx->priv_data;
00244 
00245     avcodec_get_frame_defaults(&s->picture);
00246     avctx->coded_frame = &s->picture;
00247 
00248     return 0;
00249 }
00250 
00251 static av_cold int sgi_end(AVCodecContext *avctx)
00252 {
00253     SgiState * const s = avctx->priv_data;
00254 
00255     if (s->picture.data[0])
00256         avctx->release_buffer(avctx, &s->picture);
00257 
00258     return 0;
00259 }
00260 
00261 AVCodec sgi_decoder = {
00262     "sgi",
00263     AVMEDIA_TYPE_VIDEO,
00264     CODEC_ID_SGI,
00265     sizeof(SgiState),
00266     sgi_init,
00267     NULL,
00268     sgi_end,
00269     decode_frame,
00270     .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
00271 };
00272 

Generated on Fri Sep 16 2011 17:17:42 for FFmpeg by  doxygen 1.7.1