sgienc.c 6.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * SGI image encoder
 * Todd Kirby <doubleshot@pacbell.net>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * 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.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * 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
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "avcodec.h"
#include "bytestream.h"
24
#include "internal.h"
25 26 27 28 29 30 31 32 33 34
#include "sgi.h"
#include "rle.h"

#define SGI_SINGLE_CHAN 2
#define SGI_MULTI_CHAN 3

typedef struct SgiContext {
    AVFrame picture;
} SgiContext;

35 36
static av_cold int encode_init(AVCodecContext *avctx)
{
37 38
    SgiContext *s = avctx->priv_data;

39 40 41 42 43
    if (avctx->width > 65535 || avctx->height > 65535) {
        av_log(avctx, AV_LOG_ERROR, "SGI does not support resolutions above 65535x65535\n");
        return -1;
    }

44 45 46 47 48 49
    avcodec_get_frame_defaults(&s->picture);
    avctx->coded_frame = &s->picture;

    return 0;
}

50 51
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                        const AVFrame *frame, int *got_packet)
52
{
53 54
    SgiContext *s = avctx->priv_data;
    AVFrame * const p = &s->picture;
55 56
    uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf, *buf;
    int x, y, z, length, tablesize, ret;
57
    unsigned int width, height, depth, dimension, bytes_per_channel, pixmax, put_be;
58
    unsigned char *end_buf;
59

60
    *p = *frame;
61
    p->pict_type = AV_PICTURE_TYPE_I;
62 63
    p->key_frame = 1;

64
    width  = avctx->width;
65
    height = avctx->height;
66 67 68
    bytes_per_channel = 1;
    pixmax = 0xFF;
    put_be = HAVE_BIGENDIAN;
69 70

    switch (avctx->pix_fmt) {
71 72 73 74 75 76 77 78 79 80 81 82
    case PIX_FMT_GRAY8:
        dimension = SGI_SINGLE_CHAN;
        depth     = SGI_GRAYSCALE;
        break;
    case PIX_FMT_RGB24:
        dimension = SGI_MULTI_CHAN;
        depth     = SGI_RGB;
        break;
    case PIX_FMT_RGBA:
        dimension = SGI_MULTI_CHAN;
        depth     = SGI_RGBA;
        break;
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    case PIX_FMT_GRAY16LE:
        put_be = !HAVE_BIGENDIAN;
    case PIX_FMT_GRAY16BE:
        avctx->coder_type = FF_CODER_TYPE_RAW;
        bytes_per_channel = 2;
        pixmax = 0xFFFF;
        dimension = SGI_SINGLE_CHAN;
        depth     = SGI_GRAYSCALE;
        break;
    case PIX_FMT_RGB48LE:
        put_be = !HAVE_BIGENDIAN;
    case PIX_FMT_RGB48BE:
        avctx->coder_type = FF_CODER_TYPE_RAW;
        bytes_per_channel = 2;
        pixmax = 0xFFFF;
        dimension = SGI_MULTI_CHAN;
        depth     = SGI_RGB;
        break;
    case PIX_FMT_RGBA64LE:
        put_be = !HAVE_BIGENDIAN;
    case PIX_FMT_RGBA64BE:
        avctx->coder_type = FF_CODER_TYPE_RAW;
        bytes_per_channel = 2;
        pixmax = 0xFFFF;
        dimension = SGI_MULTI_CHAN;
        depth     = SGI_RGBA;
        break;
110 111
    default:
        return AVERROR_INVALIDDATA;
112 113 114
    }

    tablesize = depth * height * 4;
115 116 117 118 119 120
    length = SGI_HEADER_SIZE;
    if (avctx->coder_type == FF_CODER_TYPE_RAW)
        length += depth * height * width;
    else // assume ff_rl_encode() produces at most 2x size of input
        length += tablesize * 2 + depth * height * (2 * width + 1);

121
    if ((ret = ff_alloc_packet2(avctx, pkt, bytes_per_channel * length)) < 0)
122 123 124
        return ret;
    buf     = pkt->data;
    end_buf = pkt->data + pkt->size;
125 126 127

    /* Encode header. */
    bytestream_put_be16(&buf, SGI_MAGIC);
128
    bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/
129
    bytestream_put_byte(&buf, bytes_per_channel);
130 131 132 133 134 135
    bytestream_put_be16(&buf, dimension);
    bytestream_put_be16(&buf, width);
    bytestream_put_be16(&buf, height);
    bytestream_put_be16(&buf, depth);

    bytestream_put_be32(&buf, 0L); /* pixmin */
136
    bytestream_put_be32(&buf, pixmax);
137 138 139 140 141 142 143 144 145 146 147 148 149
    bytestream_put_be32(&buf, 0L); /* dummy */

    /* name */
    memset(buf, 0, SGI_HEADER_SIZE);
    buf += 80;

     /* colormap */
    bytestream_put_be32(&buf, 0L);

    /* The rest of the 512 byte header is unused. */
    buf += 404;
    offsettab = buf;

150
    if (avctx->coder_type  != FF_CODER_TYPE_RAW) {
151 152 153
        /* Skip RLE offset table. */
        buf += tablesize;
        lengthtab = buf;
154

155 156
        /* Skip RLE length table. */
        buf += tablesize;
157

158
        /* Make an intermediate consecutive buffer. */
159
        if (!(encode_buf = av_malloc(width)))
160
            return -1;
161

162 163
        for (z = 0; z < depth; z++) {
            in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
164

165
            for (y = 0; y < height; y++) {
166
                bytestream_put_be32(&offsettab, buf - pkt->data);
167

168 169
                for (x = 0; x < width; x++)
                    encode_buf[x] = in_buf[depth * x];
170

171 172 173 174
                if ((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) {
                    av_free(encode_buf);
                    return -1;
                }
175

176 177 178 179 180
                buf += length;
                bytestream_put_byte(&buf, 0);
                bytestream_put_be32(&lengthtab, length + 1);
                in_buf -= p->linesize[0];
            }
181 182
        }

183
        av_free(encode_buf);
184 185
    } else {
        for (z = 0; z < depth; z++) {
186
            in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
187

188 189
            for (y = 0; y < height; y++) {
                for (x = 0; x < width * depth; x += depth)
190
                    if (bytes_per_channel == 1) {
191
                    bytestream_put_byte(&buf, in_buf[x]);
192 193 194 195 196 197 198
                    } else {
                        if (put_be) {
                            bytestream_put_be16(&buf, ((uint16_t *)in_buf)[x]);
                        } else {
                            bytestream_put_le16(&buf, ((uint16_t *)in_buf)[x]);
                        }
                    }
199

200
                in_buf -= p->linesize[0];
201 202
            }
        }
203
    }
204

205
    /* total length */
206 207 208 209 210
    pkt->size = buf - pkt->data;
    pkt->flags |= AV_PKT_FLAG_KEY;
    *got_packet = 1;

    return 0;
211 212
}

213
AVCodec ff_sgi_encoder = {
214 215 216 217 218
    .name           = "sgi",
    .type           = AVMEDIA_TYPE_VIDEO,
    .id             = CODEC_ID_SGI,
    .priv_data_size = sizeof(SgiContext),
    .init           = encode_init,
219
    .encode2        = encode_frame,
220
    .pix_fmts       = (const enum PixelFormat[]){
221 222 223 224 225
        PIX_FMT_RGB24, PIX_FMT_RGBA,
        PIX_FMT_RGB48LE, PIX_FMT_RGB48BE,
        PIX_FMT_RGBA64LE, PIX_FMT_RGBA64BE,
        PIX_FMT_GRAY16LE, PIX_FMT_GRAY16BE,
        PIX_FMT_GRAY8, PIX_FMT_NONE
226 227
    },
    .long_name      = NULL_IF_CONFIG_SMALL("SGI image"),
228
};