3 * Todd Kirby <doubleshot@pacbell.net>
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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, bytes_per_channel, pixmax, put_be;
53 unsigned char *end_buf;
56 p->pict_type = AV_PICTURE_TYPE_I;
60 height = avctx->height;
61 bytes_per_channel = 1;
63 put_be = HAVE_BIGENDIAN;
65 switch (avctx->pix_fmt) {
67 dimension = SGI_SINGLE_CHAN;
68 depth = SGI_GRAYSCALE;
71 dimension = SGI_MULTI_CHAN;
75 dimension = SGI_MULTI_CHAN;
78 case PIX_FMT_GRAY16LE:
79 put_be = !HAVE_BIGENDIAN;
80 case PIX_FMT_GRAY16BE:
81 avctx->coder_type = FF_CODER_TYPE_RAW;
82 bytes_per_channel = 2;
84 dimension = SGI_SINGLE_CHAN;
85 depth = SGI_GRAYSCALE;
88 put_be = !HAVE_BIGENDIAN;
90 avctx->coder_type = FF_CODER_TYPE_RAW;
91 bytes_per_channel = 2;
93 dimension = SGI_MULTI_CHAN;
96 case PIX_FMT_RGBA64LE:
97 put_be = !HAVE_BIGENDIAN;
98 case PIX_FMT_RGBA64BE:
99 avctx->coder_type = FF_CODER_TYPE_RAW;
100 bytes_per_channel = 2;
102 dimension = SGI_MULTI_CHAN;
106 return AVERROR_INVALIDDATA;
109 tablesize = depth * height * 4;
110 length = SGI_HEADER_SIZE;
111 if (avctx->coder_type == FF_CODER_TYPE_RAW)
112 length += depth * height * width;
113 else // assume ff_rl_encode() produces at most 2x size of input
114 length += tablesize * 2 + depth * height * (2 * width + 1);
116 if ((ret = ff_alloc_packet(pkt, bytes_per_channel * length)) < 0) {
117 av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", length);
121 end_buf = pkt->data + pkt->size;
124 bytestream_put_be16(&buf, SGI_MAGIC);
125 bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/
126 bytestream_put_byte(&buf, bytes_per_channel);
127 bytestream_put_be16(&buf, dimension);
128 bytestream_put_be16(&buf, width);
129 bytestream_put_be16(&buf, height);
130 bytestream_put_be16(&buf, depth);
132 bytestream_put_be32(&buf, 0L); /* pixmin */
133 bytestream_put_be32(&buf, pixmax);
134 bytestream_put_be32(&buf, 0L); /* dummy */
137 memset(buf, 0, SGI_HEADER_SIZE);
141 bytestream_put_be32(&buf, 0L);
143 /* The rest of the 512 byte header is unused. */
147 if (avctx->coder_type != FF_CODER_TYPE_RAW) {
148 /* Skip RLE offset table. */
152 /* Skip RLE length table. */
155 /* Make an intermediate consecutive buffer. */
156 if (!(encode_buf = av_malloc(width)))
159 for (z = 0; z < depth; z++) {
160 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
162 for (y = 0; y < height; y++) {
163 bytestream_put_be32(&offsettab, buf - pkt->data);
165 for (x = 0; x < width; x++)
166 encode_buf[x] = in_buf[depth * x];
168 if ((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) {
174 bytestream_put_byte(&buf, 0);
175 bytestream_put_be32(&lengthtab, length + 1);
176 in_buf -= p->linesize[0];
182 for (z = 0; z < depth; z++) {
183 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
185 for (y = 0; y < height; y++) {
186 for (x = 0; x < width * depth; x += depth)
187 if (bytes_per_channel == 1) {
188 bytestream_put_byte(&buf, in_buf[x]);
191 bytestream_put_be16(&buf, ((uint16_t *)in_buf)[x]);
193 bytestream_put_le16(&buf, ((uint16_t *)in_buf)[x]);
197 in_buf -= p->linesize[0];
203 pkt->size = buf - pkt->data;
204 pkt->flags |= AV_PKT_FLAG_KEY;
210 AVCodec ff_sgi_encoder = {
212 .type = AVMEDIA_TYPE_VIDEO,
214 .priv_data_size = sizeof(SgiContext),
216 .encode2 = encode_frame,
217 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA,
218 PIX_FMT_RGB48LE, PIX_FMT_RGB48BE,
219 PIX_FMT_RGBA64LE, PIX_FMT_RGBA64BE,
220 PIX_FMT_GRAY16LE, PIX_FMT_GRAY16BE,
221 PIX_FMT_GRAY8, PIX_FMT_NONE},
222 .long_name= NULL_IF_CONFIG_SMALL("SGI image"),