sgidec.c 7.4 KB
Newer Older
1 2 3 4
/*
 * SGI image decoder
 * Todd Kirby <doubleshot@pacbell.net>
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8 9 10 11
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
12
 * Libav is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
19 20 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22
#include "libavutil/imgutils.h"
23 24
#include "avcodec.h"
#include "bytestream.h"
25
#include "internal.h"
26 27 28 29 30 31
#include "sgi.h"

typedef struct SgiState {
    unsigned int width;
    unsigned int height;
    unsigned int depth;
32
    unsigned int bytes_per_channel;
33
    int linesize;
34
    GetByteContext g;
35 36 37 38
} SgiState;

/**
 * Expand an RLE row into a channel.
39
 * @param s the current image state
40 41 42 43 44
 * @param out_buf Points to one line after the output buffer.
 * @param out_end end of line in output buffer
 * @param pixelstride pixel stride of input buffer
 * @return size of output in bytes, -1 if buffer overflows
 */
45 46
static int expand_rle_row(SgiState *s, uint8_t *out_buf,
                          uint8_t *out_end, int pixelstride)
47 48 49 50 51
{
    unsigned char pixel, count;
    unsigned char *orig = out_buf;

    while (1) {
52 53 54
        if (bytestream2_get_bytes_left(&s->g) < 1)
            return AVERROR_INVALIDDATA;
        pixel = bytestream2_get_byteu(&s->g);
55 56 57 58 59 60 61 62 63
        if (!(count = (pixel & 0x7f))) {
            return (out_buf - orig) / pixelstride;
        }

        /* Check for buffer overflow. */
        if(out_buf + pixelstride * count >= out_end) return -1;

        if (pixel & 0x80) {
            while (count--) {
64
                *out_buf = bytestream2_get_byte(&s->g);
65 66 67
                out_buf += pixelstride;
            }
        } else {
68
            pixel = bytestream2_get_byte(&s->g);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

            while (count--) {
                *out_buf = pixel;
                out_buf += pixelstride;
            }
        }
    }
}

/**
 * Read a run length encoded SGI image.
 * @param out_buf output buffer
 * @param s the current image state
 * @return 0 if no error, else return error number.
 */
84
static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
85 86 87
{
    uint8_t *dest_row;
    unsigned int len = s->height * s->depth * 4;
88
    GetByteContext g_table = s->g;
89 90 91 92
    unsigned int y, z;
    unsigned int start_offset;

    /* size of  RLE offset and length tables */
93
    if (len * 2  > bytestream2_get_bytes_left(&s->g)) {
94 95 96 97 98 99 100
        return AVERROR_INVALIDDATA;
    }

    for (z = 0; z < s->depth; z++) {
        dest_row = out_buf;
        for (y = 0; y < s->height; y++) {
            dest_row -= s->linesize;
101 102 103 104
            start_offset = bytestream2_get_be32(&g_table);
            bytestream2_seek(&s->g, start_offset, SEEK_SET);
            if (expand_rle_row(s, dest_row + z, dest_row + FFABS(s->linesize),
                               s->depth) != s->width) {
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
                return AVERROR_INVALIDDATA;
            }
        }
    }
    return 0;
}

/**
 * Read an uncompressed SGI image.
 * @param out_buf output buffer
 * @param out_end end ofoutput buffer
 * @param s the current image state
 * @return 0 if read success, otherwise return -1.
 */
static int read_uncompressed_sgi(unsigned char* out_buf, uint8_t* out_end,
120
                                 SgiState *s)
121 122
{
    int x, y, z;
123
    unsigned int offset = s->height * s->width * s->bytes_per_channel;
124
    GetByteContext gp[4];
125 126

    /* Test buffer size. */
127 128 129 130 131 132 133
    if (offset * s->depth > bytestream2_get_bytes_left(&s->g))
        return AVERROR_INVALIDDATA;

    /* Create a reader for each plane */
    for (z = 0; z < s->depth; z++) {
        gp[z] = s->g;
        bytestream2_skip(&gp[z], z * offset);
134 135 136 137
    }

    for (y = s->height - 1; y >= 0; y--) {
        out_end = out_buf + (y * s->linesize);
138 139 140 141 142 143 144 145 146
        if (s->bytes_per_channel == 1) {
            for (x = s->width; x > 0; x--)
                for (z = 0; z < s->depth; z++)
                    *out_end++ = bytestream2_get_byteu(&gp[z]);
        } else {
            uint16_t *out16 = (uint16_t *)out_end;
            for (x = s->width; x > 0; x--)
                for (z = 0; z < s->depth; z++)
                    *out16++ = bytestream2_get_ne16u(&gp[z]);
147 148 149 150 151 152
        }
    }
    return 0;
}

static int decode_frame(AVCodecContext *avctx,
153
                        void *data, int *got_frame,
154
                        AVPacket *avpkt)
155 156
{
    SgiState *s = avctx->priv_data;
157
    AVFrame *p = data;
158
    unsigned int dimension, rle;
159 160 161
    int ret = 0;
    uint8_t *out_buf, *out_end;

162 163 164 165
    bytestream2_init(&s->g, avpkt->data, avpkt->size);
    if (bytestream2_get_bytes_left(&s->g) < SGI_HEADER_SIZE) {
        av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
        return AVERROR_INVALIDDATA;
166 167 168
    }

    /* Test for SGI magic. */
169
    if (bytestream2_get_be16(&s->g) != SGI_MAGIC) {
170
        av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
171
        return AVERROR_INVALIDDATA;
172 173
    }

174 175 176 177 178 179
    rle                  = bytestream2_get_byte(&s->g);
    s->bytes_per_channel = bytestream2_get_byte(&s->g);
    dimension            = bytestream2_get_be16(&s->g);
    s->width             = bytestream2_get_be16(&s->g);
    s->height            = bytestream2_get_be16(&s->g);
    s->depth             = bytestream2_get_be16(&s->g);
180

181
    if (s->bytes_per_channel != 1 && (s->bytes_per_channel != 2 || rle)) {
182 183 184 185 186 187 188 189 190 191 192
        av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
        return -1;
    }

    /* Check for supported image dimensions. */
    if (dimension != 2 && dimension != 3) {
        av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
        return -1;
    }

    if (s->depth == SGI_GRAYSCALE) {
193
        avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_GRAY16BE : AV_PIX_FMT_GRAY8;
194
    } else if (s->depth == SGI_RGB) {
195
        avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_RGB48BE : AV_PIX_FMT_RGB24;
196
    } else if (s->depth == SGI_RGBA && s->bytes_per_channel == 1) {
197
        avctx->pix_fmt = AV_PIX_FMT_RGBA;
198 199 200 201 202
    } else {
        av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
        return -1;
    }

203
    if (av_image_check_size(s->width, s->height, 0, avctx))
204 205 206
        return -1;
    avcodec_set_dimensions(avctx, s->width, s->height);

207
    if (ff_get_buffer(avctx, p, 0) < 0) {
208 209 210 211
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed.\n");
        return -1;
    }

212
    p->pict_type = AV_PICTURE_TYPE_I;
213 214 215 216 217 218 219 220
    p->key_frame = 1;
    out_buf = p->data[0];

    out_end = out_buf + p->linesize[0] * s->height;

    s->linesize = p->linesize[0];

    /* Skip header. */
221
    bytestream2_seek(&s->g, SGI_HEADER_SIZE, SEEK_SET);
222
    if (rle) {
223
        ret = read_rle_sgi(out_end, s);
224
    } else {
225
        ret = read_uncompressed_sgi(out_buf, out_end, s);
226 227 228
    }

    if (ret == 0) {
229
        *got_frame = 1;
230
        return avpkt->size;
231
    } else {
232
        return ret;
233 234 235
    }
}

236
AVCodec ff_sgi_decoder = {
237 238
    .name           = "sgi",
    .type           = AVMEDIA_TYPE_VIDEO,
239
    .id             = AV_CODEC_ID_SGI,
240 241
    .priv_data_size = sizeof(SgiState),
    .decode         = decode_frame,
242
    .long_name      = NULL_IF_CONFIG_SMALL("SGI image"),
243
    .capabilities   = CODEC_CAP_DR1,
244
};