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
22 #include "libavutil/opt.h"
25 #include "bytestream.h"
30 #define SGI_SINGLE_CHAN 2
31 #define SGI_MULTI_CHAN 3
33 typedef struct SgiContext {
39 static av_cold int encode_init(AVCodecContext *avctx)
41 if (avctx->width > 65535 || avctx->height > 65535) {
42 av_log(avctx, AV_LOG_ERROR, "Unsupported resolution %dx%d. "
43 "SGI does not support resolutions above 65535x65535\n",
44 avctx->width, avctx->height);
45 return AVERROR_INVALIDDATA;
51 static int sgi_rle_encode(PutByteContext *pbc, const uint8_t *src,
54 int val, count, x, start = bytestream2_tell_p(pbc);
55 void (*bytestream2_put)(PutByteContext *, unsigned int);
58 bytestream2_put = bytestream2_put_byte;
60 bytestream2_put = bytestream2_put_be16;
62 for (x = 0; x < w; x += count) {
63 /* see if we can encode the next set of pixels with RLE */
64 count = ff_rle_count_pixels(src, w - x, bpp, 1);
66 if (bytestream2_get_bytes_left_p(pbc) < bpp * 2)
67 return AVERROR_INVALIDDATA;
69 val = bpp == 1 ? *src : AV_RB16(src);
70 bytestream2_put(pbc, count);
71 bytestream2_put(pbc, val);
74 /* fall back on uncompressed */
75 count = ff_rle_count_pixels(src, w - x, bpp, 0);
76 if (bytestream2_get_bytes_left_p(pbc) < bpp * (count + 1))
77 return AVERROR_INVALIDDATA;
79 bytestream2_put(pbc, count + 0x80);
80 for (i = 0; i < count; i++) {
81 val = bpp == 1 ? src[i] : AV_RB16(src + i * bpp);
82 bytestream2_put(pbc, val);
89 return bytestream2_tell_p(pbc) - start;
92 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
93 const AVFrame *frame, int *got_packet)
95 SgiContext *s = avctx->priv_data;
96 const AVFrame * const p = frame;
98 uint8_t *in_buf, *encode_buf;
99 int x, y, z, length, tablesize, ret, i;
100 unsigned int width, height, depth, dimension;
101 unsigned int bytes_per_channel, pixmax, put_be;
103 width = avctx->width;
104 height = avctx->height;
105 bytes_per_channel = 1;
107 put_be = HAVE_BIGENDIAN;
109 switch (avctx->pix_fmt) {
110 case AV_PIX_FMT_GRAY8:
111 dimension = SGI_SINGLE_CHAN;
112 depth = SGI_GRAYSCALE;
114 case AV_PIX_FMT_RGB24:
115 dimension = SGI_MULTI_CHAN;
118 case AV_PIX_FMT_RGBA:
119 dimension = SGI_MULTI_CHAN;
122 case AV_PIX_FMT_GRAY16LE:
123 put_be = !HAVE_BIGENDIAN;
124 case AV_PIX_FMT_GRAY16BE:
125 bytes_per_channel = 2;
127 dimension = SGI_SINGLE_CHAN;
128 depth = SGI_GRAYSCALE;
130 case AV_PIX_FMT_RGB48LE:
131 put_be = !HAVE_BIGENDIAN;
132 case AV_PIX_FMT_RGB48BE:
133 bytes_per_channel = 2;
135 dimension = SGI_MULTI_CHAN;
138 case AV_PIX_FMT_RGBA64LE:
139 put_be = !HAVE_BIGENDIAN;
140 case AV_PIX_FMT_RGBA64BE:
141 bytes_per_channel = 2;
143 dimension = SGI_MULTI_CHAN;
147 return AVERROR_INVALIDDATA;
150 tablesize = depth * height * 4;
151 length = SGI_HEADER_SIZE;
153 length += depth * height * width;
154 else // assume sgi_rle_encode() produces at most 2x size of input
155 length += tablesize * 2 + depth * height * (2 * width + 1);
157 if ((ret = ff_alloc_packet2(avctx, pkt, bytes_per_channel * length, 0)) < 0)
160 bytestream2_init_writer(&pbc, pkt->data, pkt->size);
163 bytestream2_put_be16(&pbc, SGI_MAGIC);
164 bytestream2_put_byte(&pbc, s->rle); /* RLE 1 - VERBATIM 0 */
165 bytestream2_put_byte(&pbc, bytes_per_channel);
166 bytestream2_put_be16(&pbc, dimension);
167 bytestream2_put_be16(&pbc, width);
168 bytestream2_put_be16(&pbc, height);
169 bytestream2_put_be16(&pbc, depth);
171 bytestream2_put_be32(&pbc, 0L); /* pixmin */
172 bytestream2_put_be32(&pbc, pixmax);
173 bytestream2_put_be32(&pbc, 0L); /* dummy */
176 for (i = 0; i < 80; i++)
177 bytestream2_put_byte(&pbc, 0L);
180 bytestream2_put_be32(&pbc, 0L);
182 /* The rest of the 512 byte header is unused. */
183 for (i = 0; i < 404; i++)
184 bytestream2_put_byte(&pbc, 0L);
187 PutByteContext taboff_pcb, tablen_pcb;
189 /* Skip RLE offset table. */
190 bytestream2_init_writer(&taboff_pcb, pbc.buffer, tablesize);
191 bytestream2_skip_p(&pbc, tablesize);
193 /* Skip RLE length table. */
194 bytestream2_init_writer(&tablen_pcb, pbc.buffer, tablesize);
195 bytestream2_skip_p(&pbc, tablesize);
197 /* Make an intermediate consecutive buffer. */
198 if (!(encode_buf = av_malloc(width * bytes_per_channel)))
199 return AVERROR(ENOMEM);
201 for (z = 0; z < depth; z++) {
202 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
204 for (y = 0; y < height; y++) {
205 bytestream2_put_be32(&taboff_pcb, bytestream2_tell_p(&pbc));
207 for (x = 0; x < width * bytes_per_channel; x += bytes_per_channel)
208 if (bytes_per_channel == 1) {
209 encode_buf[x] = in_buf[depth * x];
210 } else if (HAVE_BIGENDIAN ^ put_be) {
211 encode_buf[x + 1] = in_buf[depth * x];
212 encode_buf[x] = in_buf[depth * x + 1];
214 encode_buf[x] = in_buf[depth * x];
215 encode_buf[x + 1] = in_buf[depth * x + 1];
218 length = sgi_rle_encode(&pbc, encode_buf, width,
222 return AVERROR_INVALIDDATA;
225 bytestream2_put_be32(&tablen_pcb, length);
226 in_buf -= p->linesize[0];
232 for (z = 0; z < depth; z++) {
233 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
235 for (y = 0; y < height; y++) {
236 for (x = 0; x < width * depth; x += depth)
237 if (bytes_per_channel == 1)
238 bytestream2_put_byte(&pbc, in_buf[x]);
241 bytestream2_put_be16(&pbc, ((uint16_t *)in_buf)[x]);
243 bytestream2_put_le16(&pbc, ((uint16_t *)in_buf)[x]);
245 in_buf -= p->linesize[0];
251 pkt->size = bytestream2_tell_p(&pbc);
252 pkt->flags |= AV_PKT_FLAG_KEY;
258 #define OFFSET(x) offsetof(SgiContext, x)
259 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
260 static const AVOption options[] = {
261 { "rle", "Use run-length compression", OFFSET(rle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
266 static const AVClass sgi_class = {
268 .item_name = av_default_item_name,
270 .version = LIBAVUTIL_VERSION_INT,
273 AVCodec ff_sgi_encoder = {
275 .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
276 .type = AVMEDIA_TYPE_VIDEO,
277 .id = AV_CODEC_ID_SGI,
278 .priv_data_size = sizeof(SgiContext),
279 .priv_class = &sgi_class,
281 .encode2 = encode_frame,
282 .pix_fmts = (const enum AVPixelFormat[]) {
283 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
284 AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE,
285 AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE,
286 AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY8,
289 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,