3 * Todd Kirby <doubleshot@pacbell.net>
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * 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 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * 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 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "bytestream.h"
28 #define SGI_SINGLE_CHAN 2
29 #define SGI_MULTI_CHAN 3
31 typedef struct SgiContext {
35 static av_cold int encode_init(AVCodecContext *avctx)
37 SgiContext *s = avctx->priv_data;
39 avcodec_get_frame_defaults(&s->picture);
40 avctx->coded_frame = &s->picture;
45 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
46 const AVFrame *frame, int *got_packet)
48 SgiContext *s = avctx->priv_data;
49 AVFrame * const p = &s->picture;
50 uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf, *buf;
51 int x, y, z, length, tablesize, ret;
52 unsigned int width, height, depth, dimension;
53 unsigned char *end_buf;
56 p->pict_type = AV_PICTURE_TYPE_I;
60 height = avctx->height;
62 switch (avctx->pix_fmt) {
64 dimension = SGI_SINGLE_CHAN;
65 depth = SGI_GRAYSCALE;
68 dimension = SGI_MULTI_CHAN;
72 dimension = SGI_MULTI_CHAN;
76 return AVERROR_INVALIDDATA;
79 tablesize = depth * height * 4;
80 length = SGI_HEADER_SIZE;
81 if (avctx->coder_type == FF_CODER_TYPE_RAW)
82 length += depth * height * width;
83 else // assume ff_rl_encode() produces at most 2x size of input
84 length += tablesize * 2 + depth * height * (2 * width + 1);
86 if ((ret = ff_alloc_packet(pkt, length)) < 0) {
87 av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", length);
91 end_buf = pkt->data + pkt->size;
94 bytestream_put_be16(&buf, SGI_MAGIC);
95 bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/
96 bytestream_put_byte(&buf, 1); /* bytes_per_channel */
97 bytestream_put_be16(&buf, dimension);
98 bytestream_put_be16(&buf, width);
99 bytestream_put_be16(&buf, height);
100 bytestream_put_be16(&buf, depth);
102 /* The rest are constant in this implementation. */
103 bytestream_put_be32(&buf, 0L); /* pixmin */
104 bytestream_put_be32(&buf, 255L); /* pixmax */
105 bytestream_put_be32(&buf, 0L); /* dummy */
108 memset(buf, 0, SGI_HEADER_SIZE);
112 bytestream_put_be32(&buf, 0L);
114 /* The rest of the 512 byte header is unused. */
118 if (avctx->coder_type != FF_CODER_TYPE_RAW) {
119 /* Skip RLE offset table. */
123 /* Skip RLE length table. */
126 /* Make an intermediate consecutive buffer. */
127 if (!(encode_buf = av_malloc(width)))
130 for (z = 0; z < depth; z++) {
131 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
133 for (y = 0; y < height; y++) {
134 bytestream_put_be32(&offsettab, buf - pkt->data);
136 for (x = 0; x < width; x++)
137 encode_buf[x] = in_buf[depth * x];
139 if ((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) {
145 bytestream_put_byte(&buf, 0);
146 bytestream_put_be32(&lengthtab, length + 1);
147 in_buf -= p->linesize[0];
153 for (z = 0; z < depth; z++) {
154 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
156 for (y = 0; y < height; y++) {
157 for (x = 0; x < width * depth; x += depth)
158 bytestream_put_byte(&buf, in_buf[x]);
160 in_buf -= p->linesize[0];
166 pkt->size = buf - pkt->data;
167 pkt->flags |= AV_PKT_FLAG_KEY;
173 AVCodec ff_sgi_encoder = {
175 .type = AVMEDIA_TYPE_VIDEO,
177 .priv_data_size = sizeof(SgiContext),
179 .encode2 = encode_frame,
180 .pix_fmts = (const enum PixelFormat[]){
181 PIX_FMT_RGB24, PIX_FMT_RGBA, PIX_FMT_GRAY8, PIX_FMT_NONE
183 .long_name = NULL_IF_CONFIG_SMALL("SGI image"),