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 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 return AVERROR_INVALIDDATA;
42 static int sgi_rle_encode(PutByteContext *pbc, const uint8_t *src,
45 int val, count, x, start = bytestream2_tell_p(pbc);
46 void (*bytestream2_put)(PutByteContext *, unsigned int);
49 bytestream2_put = bytestream2_put_byte;
51 bytestream2_put = bytestream2_put_be16;
53 for (x = 0; x < w; x += count) {
54 /* see if we can encode the next set of pixels with RLE */
55 count = ff_rle_count_pixels(src, w - x, bpp, 1);
57 if (bytestream2_get_bytes_left_p(pbc) < bpp * 2)
58 return AVERROR_INVALIDDATA;
60 val = bpp == 1 ? *src : AV_RB16(src);
61 bytestream2_put(pbc, count);
62 bytestream2_put(pbc, val);
65 /* fall back on uncompressed */
66 count = ff_rle_count_pixels(src, w - x, bpp, 0);
67 if (bytestream2_get_bytes_left_p(pbc) < bpp * (count + 1))
68 return AVERROR_INVALIDDATA;
70 bytestream2_put(pbc, count + 0x80);
71 for (i = 0; i < count; i++) {
72 val = bpp == 1 ? src[i] : AV_RB16(src + i * bpp);
73 bytestream2_put(pbc, val);
80 return bytestream2_tell_p(pbc) - start;
83 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
84 const AVFrame *frame, int *got_packet)
86 const AVFrame * const p = frame;
88 uint8_t *in_buf, *encode_buf;
89 int x, y, z, length, tablesize, ret;
90 unsigned int width, height, depth, dimension;
91 unsigned int bytes_per_channel, pixmax, put_be;
93 #if FF_API_CODED_FRAME
94 FF_DISABLE_DEPRECATION_WARNINGS
95 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
96 avctx->coded_frame->key_frame = 1;
97 FF_ENABLE_DEPRECATION_WARNINGS
100 width = avctx->width;
101 height = avctx->height;
102 bytes_per_channel = 1;
104 put_be = HAVE_BIGENDIAN;
106 switch (avctx->pix_fmt) {
107 case AV_PIX_FMT_GRAY8:
108 dimension = SGI_SINGLE_CHAN;
109 depth = SGI_GRAYSCALE;
111 case AV_PIX_FMT_RGB24:
112 dimension = SGI_MULTI_CHAN;
115 case AV_PIX_FMT_RGBA:
116 dimension = SGI_MULTI_CHAN;
119 case AV_PIX_FMT_GRAY16LE:
120 put_be = !HAVE_BIGENDIAN;
121 case AV_PIX_FMT_GRAY16BE:
122 bytes_per_channel = 2;
124 dimension = SGI_SINGLE_CHAN;
125 depth = SGI_GRAYSCALE;
127 case AV_PIX_FMT_RGB48LE:
128 put_be = !HAVE_BIGENDIAN;
129 case AV_PIX_FMT_RGB48BE:
130 bytes_per_channel = 2;
132 dimension = SGI_MULTI_CHAN;
135 case AV_PIX_FMT_RGBA64LE:
136 put_be = !HAVE_BIGENDIAN;
137 case AV_PIX_FMT_RGBA64BE:
138 bytes_per_channel = 2;
140 dimension = SGI_MULTI_CHAN;
144 return AVERROR_INVALIDDATA;
147 tablesize = depth * height * 4;
148 length = SGI_HEADER_SIZE;
149 if (avctx->coder_type == FF_CODER_TYPE_RAW)
150 length += depth * height * width;
151 else // assume sgi_rle_encode() produces at most 2x size of input
152 length += tablesize * 2 + depth * height * (2 * width + 1);
154 if ((ret = ff_alloc_packet(pkt, bytes_per_channel * length)) < 0) {
155 av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", length);
159 bytestream2_init_writer(&pbc, pkt->data, pkt->size);
162 bytestream2_put_be16(&pbc, SGI_MAGIC);
163 bytestream2_put_byte(&pbc, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0 */
164 bytestream2_put_byte(&pbc, bytes_per_channel);
165 bytestream2_put_be16(&pbc, dimension);
166 bytestream2_put_be16(&pbc, width);
167 bytestream2_put_be16(&pbc, height);
168 bytestream2_put_be16(&pbc, depth);
170 bytestream2_put_be32(&pbc, 0L); /* pixmin */
171 bytestream2_put_be32(&pbc, pixmax);
172 bytestream2_put_be32(&pbc, 0L); /* dummy */
175 bytestream2_skip_p(&pbc, 80);
178 bytestream2_put_be32(&pbc, 0L);
180 /* The rest of the 512 byte header is unused. */
181 bytestream2_skip_p(&pbc, 404);
183 if (avctx->coder_type != FF_CODER_TYPE_RAW) {
184 PutByteContext taboff_pcb, tablen_pcb;
186 /* Skip RLE offset table. */
187 bytestream2_init_writer(&taboff_pcb, pbc.buffer, tablesize);
188 bytestream2_skip_p(&pbc, tablesize);
190 /* Skip RLE length table. */
191 bytestream2_init_writer(&tablen_pcb, pbc.buffer, tablesize);
192 bytestream2_skip_p(&pbc, tablesize);
194 /* Make an intermediate consecutive buffer. */
195 if (!(encode_buf = av_malloc(width * bytes_per_channel)))
196 return AVERROR(ENOMEM);
198 for (z = 0; z < depth; z++) {
199 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
201 for (y = 0; y < height; y++) {
202 bytestream2_put_be32(&taboff_pcb, bytestream2_tell_p(&pbc));
204 for (x = 0; x < width * bytes_per_channel; x += bytes_per_channel)
205 encode_buf[x] = in_buf[depth * x];
207 length = sgi_rle_encode(&pbc, encode_buf, width,
211 return AVERROR_INVALIDDATA;
214 bytestream2_put_be32(&tablen_pcb, length);
215 in_buf -= p->linesize[0];
221 for (z = 0; z < depth; z++) {
222 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
224 for (y = 0; y < height; y++) {
225 for (x = 0; x < width * depth; x += depth)
226 if (bytes_per_channel == 1)
227 bytestream2_put_byte(&pbc, in_buf[x]);
230 bytestream2_put_be16(&pbc, ((uint16_t *)in_buf)[x]);
232 bytestream2_put_le16(&pbc, ((uint16_t *)in_buf)[x]);
234 in_buf -= p->linesize[0];
240 pkt->size = bytestream2_tell_p(&pbc);
241 pkt->flags |= AV_PKT_FLAG_KEY;
247 AVCodec ff_sgi_encoder = {
249 .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
250 .type = AVMEDIA_TYPE_VIDEO,
251 .id = AV_CODEC_ID_SGI,
253 .encode2 = encode_frame,
254 .pix_fmts = (const enum AVPixelFormat[]) {
255 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
256 AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE,
257 AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE,
258 AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY8,