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 static av_cold int encode_init(AVCodecContext *avctx)
33 if (avctx->width > 65535 || avctx->height > 65535) {
34 av_log(avctx, AV_LOG_ERROR,
35 "Unsupported resolution %dx%d.\n", avctx->width, avctx->height);
36 av_log(avctx, AV_LOG_ERROR, "SGI does not support resolutions above 65535x65535\n");
37 return AVERROR_INVALIDDATA;
43 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
44 const AVFrame *frame, int *got_packet)
46 const AVFrame * const p = frame;
47 uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf, *buf;
48 int x, y, z, length, tablesize, ret;
49 unsigned int width, height, depth, dimension;
50 unsigned int bytes_per_channel, pixmax, put_be;
51 unsigned char *end_buf;
53 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
54 avctx->coded_frame->key_frame = 1;
57 height = avctx->height;
58 bytes_per_channel = 1;
60 put_be = HAVE_BIGENDIAN;
62 switch (avctx->pix_fmt) {
63 case AV_PIX_FMT_GRAY8:
64 dimension = SGI_SINGLE_CHAN;
65 depth = SGI_GRAYSCALE;
67 case AV_PIX_FMT_RGB24:
68 dimension = SGI_MULTI_CHAN;
72 dimension = SGI_MULTI_CHAN;
75 case AV_PIX_FMT_GRAY16LE:
76 put_be = !HAVE_BIGENDIAN;
77 case AV_PIX_FMT_GRAY16BE:
78 avctx->coder_type = FF_CODER_TYPE_RAW;
79 bytes_per_channel = 2;
81 dimension = SGI_SINGLE_CHAN;
82 depth = SGI_GRAYSCALE;
84 case AV_PIX_FMT_RGB48LE:
85 put_be = !HAVE_BIGENDIAN;
86 case AV_PIX_FMT_RGB48BE:
87 avctx->coder_type = FF_CODER_TYPE_RAW;
88 bytes_per_channel = 2;
90 dimension = SGI_MULTI_CHAN;
93 case AV_PIX_FMT_RGBA64LE:
94 put_be = !HAVE_BIGENDIAN;
95 case AV_PIX_FMT_RGBA64BE:
96 avctx->coder_type = FF_CODER_TYPE_RAW;
97 bytes_per_channel = 2;
99 dimension = SGI_MULTI_CHAN;
103 return AVERROR_INVALIDDATA;
106 tablesize = depth * height * 4;
107 length = SGI_HEADER_SIZE;
108 if (avctx->coder_type == FF_CODER_TYPE_RAW)
109 length += depth * height * width;
110 else // assume ff_rl_encode() produces at most 2x size of input
111 length += tablesize * 2 + depth * height * (2 * width + 1);
113 if ((ret = ff_alloc_packet2(avctx, pkt, bytes_per_channel * length)) < 0)
116 end_buf = pkt->data + pkt->size;
119 bytestream_put_be16(&buf, SGI_MAGIC);
120 bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/
121 bytestream_put_byte(&buf, bytes_per_channel);
122 bytestream_put_be16(&buf, dimension);
123 bytestream_put_be16(&buf, width);
124 bytestream_put_be16(&buf, height);
125 bytestream_put_be16(&buf, depth);
127 bytestream_put_be32(&buf, 0L); /* pixmin */
128 bytestream_put_be32(&buf, pixmax);
129 bytestream_put_be32(&buf, 0L); /* dummy */
132 memset(buf, 0, SGI_HEADER_SIZE);
136 bytestream_put_be32(&buf, 0L);
138 /* The rest of the 512 byte header is unused. */
142 if (avctx->coder_type != FF_CODER_TYPE_RAW) {
143 /* Skip RLE offset table. */
147 /* Skip RLE length table. */
150 /* Make an intermediate consecutive buffer. */
151 if (!(encode_buf = av_malloc(width)))
152 return AVERROR(ENOMEM);
154 for (z = 0; z < depth; z++) {
155 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
157 for (y = 0; y < height; y++) {
158 bytestream_put_be32(&offsettab, buf - pkt->data);
160 for (x = 0; x < width; x++)
161 encode_buf[x] = in_buf[depth * x];
163 if ((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) {
169 bytestream_put_byte(&buf, 0);
170 bytestream_put_be32(&lengthtab, length + 1);
171 in_buf -= p->linesize[0];
177 for (z = 0; z < depth; z++) {
178 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
180 for (y = 0; y < height; y++) {
181 for (x = 0; x < width * depth; x += depth)
182 if (bytes_per_channel == 1) {
183 bytestream_put_byte(&buf, in_buf[x]);
186 bytestream_put_be16(&buf, ((uint16_t *)in_buf)[x]);
188 bytestream_put_le16(&buf, ((uint16_t *)in_buf)[x]);
192 in_buf -= p->linesize[0];
198 pkt->size = buf - pkt->data;
199 pkt->flags |= AV_PKT_FLAG_KEY;
205 AVCodec ff_sgi_encoder = {
207 .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
208 .type = AVMEDIA_TYPE_VIDEO,
209 .id = AV_CODEC_ID_SGI,
211 .encode2 = encode_frame,
212 .pix_fmts = (const enum AVPixelFormat[]) {
213 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
214 AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE,
215 AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE,
216 AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY8,