• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • 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 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     GetByteContext g;
00035 } SgiState;
00036 
00045 static int expand_rle_row(SgiState *s, uint8_t *out_buf,
00046                           uint8_t *out_end, int pixelstride)
00047 {
00048     unsigned char pixel, count;
00049     unsigned char *orig = out_buf;
00050 
00051     while (1) {
00052         if (bytestream2_get_bytes_left(&s->g) < 1)
00053             return AVERROR_INVALIDDATA;
00054         pixel = bytestream2_get_byteu(&s->g);
00055         if (!(count = (pixel & 0x7f))) {
00056             return (out_buf - orig) / pixelstride;
00057         }
00058 
00059         /* Check for buffer overflow. */
00060         if(out_buf + pixelstride * count >= out_end) return -1;
00061 
00062         if (pixel & 0x80) {
00063             while (count--) {
00064                 *out_buf = bytestream2_get_byte(&s->g);
00065                 out_buf += pixelstride;
00066             }
00067         } else {
00068             pixel = bytestream2_get_byte(&s->g);
00069 
00070             while (count--) {
00071                 *out_buf = pixel;
00072                 out_buf += pixelstride;
00073             }
00074         }
00075     }
00076 }
00077 
00084 static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
00085 {
00086     uint8_t *dest_row;
00087     unsigned int len = s->height * s->depth * 4;
00088     GetByteContext g_table = s->g;
00089     unsigned int y, z;
00090     unsigned int start_offset;
00091 
00092     /* size of  RLE offset and length tables */
00093     if (len * 2  > bytestream2_get_bytes_left(&s->g)) {
00094         return AVERROR_INVALIDDATA;
00095     }
00096 
00097     for (z = 0; z < s->depth; z++) {
00098         dest_row = out_buf;
00099         for (y = 0; y < s->height; y++) {
00100             dest_row -= s->linesize;
00101             start_offset = bytestream2_get_be32(&g_table);
00102             bytestream2_seek(&s->g, start_offset, SEEK_SET);
00103             if (expand_rle_row(s, dest_row + z, dest_row + FFABS(s->linesize),
00104                                s->depth) != s->width) {
00105                 return AVERROR_INVALIDDATA;
00106             }
00107         }
00108     }
00109     return 0;
00110 }
00111 
00119 static int read_uncompressed_sgi(unsigned char* out_buf, uint8_t* out_end,
00120                                  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 
00126     /* Test buffer size. */
00127     if (offset * s->depth > bytestream2_get_bytes_left(&s->g))
00128         return AVERROR_INVALIDDATA;
00129 
00130     /* Create a reader for each plane */
00131     for (z = 0; z < s->depth; z++) {
00132         gp[z] = s->g;
00133         bytestream2_skip(&gp[z], z * offset);
00134     }
00135 
00136     for (y = s->height - 1; y >= 0; y--) {
00137         out_end = out_buf + (y * s->linesize);
00138         if (s->bytes_per_channel == 1) {
00139             for (x = s->width; x > 0; x--)
00140                 for (z = 0; z < s->depth; z++)
00141                     *out_end++ = bytestream2_get_byteu(&gp[z]);
00142         } else {
00143             uint16_t *out16 = (uint16_t *)out_end;
00144             for (x = s->width; x > 0; x--)
00145                 for (z = 0; z < s->depth; z++)
00146                     *out16++ = bytestream2_get_ne16u(&gp[z]);
00147         }
00148     }
00149     return 0;
00150 }
00151 
00152 static int decode_frame(AVCodecContext *avctx,
00153                         void *data, int *data_size,
00154                         AVPacket *avpkt)
00155 {
00156     SgiState *s = avctx->priv_data;
00157     AVFrame *picture = data;
00158     AVFrame *p = &s->picture;
00159     unsigned int dimension, rle;
00160     int ret = 0;
00161     uint8_t *out_buf, *out_end;
00162 
00163     bytestream2_init(&s->g, avpkt->data, avpkt->size);
00164     if (bytestream2_get_bytes_left(&s->g) < SGI_HEADER_SIZE) {
00165         av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
00166         return AVERROR_INVALIDDATA;
00167     }
00168 
00169     /* Test for SGI magic. */
00170     if (bytestream2_get_be16(&s->g) != SGI_MAGIC) {
00171         av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
00172         return AVERROR_INVALIDDATA;
00173     }
00174 
00175     rle                  = bytestream2_get_byte(&s->g);
00176     s->bytes_per_channel = bytestream2_get_byte(&s->g);
00177     dimension            = bytestream2_get_be16(&s->g);
00178     s->width             = bytestream2_get_be16(&s->g);
00179     s->height            = bytestream2_get_be16(&s->g);
00180     s->depth             = bytestream2_get_be16(&s->g);
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 (av_image_check_size(s->width, s->height, 0, avctx))
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 = AV_PICTURE_TYPE_I;
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     bytestream2_seek(&s->g, SGI_HEADER_SIZE, SEEK_SET);
00227     if (rle) {
00228         ret = read_rle_sgi(out_end, s);
00229     } else {
00230         ret = read_uncompressed_sgi(out_buf, out_end, s);
00231     }
00232 
00233     if (ret == 0) {
00234         *picture   = s->picture;
00235         *data_size = sizeof(AVPicture);
00236         return avpkt->size;
00237     } else {
00238         return ret;
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 ff_sgi_decoder = {
00262     .name           = "sgi",
00263     .type           = AVMEDIA_TYPE_VIDEO,
00264     .id             = CODEC_ID_SGI,
00265     .priv_data_size = sizeof(SgiState),
00266     .init           = sgi_init,
00267     .close          = sgi_end,
00268     .decode         = decode_frame,
00269     .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
00270 };
00271 
Generated on Sun Apr 22 2012 21:54:04 for Libav by doxygen 1.7.1