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 sgi_rle_encode(PutByteContext *pbc, const uint8_t *src,
46 int val, count, x, start = bytestream2_tell_p(pbc);
47 void (*bytestream2_put)(PutByteContext *, unsigned int);
50 bytestream2_put = bytestream2_put_byte;
52 bytestream2_put = bytestream2_put_be16;
54 for (x = 0; x < w; x += count) {
55 /* see if we can encode the next set of pixels with RLE */
56 count = ff_rle_count_pixels(src, w - x, bpp, 1);
58 if (bytestream2_get_bytes_left_p(pbc) < bpp * 2)
59 return AVERROR_INVALIDDATA;
61 val = bpp == 1 ? *src : AV_RB16(src);
62 bytestream2_put(pbc, count);
63 bytestream2_put(pbc, val);
66 /* fall back on uncompressed */
67 count = ff_rle_count_pixels(src, w - x, bpp, 0);
68 if (bytestream2_get_bytes_left_p(pbc) < bpp * (count + 1))
69 return AVERROR_INVALIDDATA;
71 bytestream2_put(pbc, count + 0x80);
72 for (i = 0; i < count; i++) {
73 val = bpp == 1 ? src[i] : AV_RB16(src + i * bpp);
74 bytestream2_put(pbc, val);
81 return bytestream2_tell_p(pbc) - start;
84 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
85 const AVFrame *frame, int *got_packet)
87 const AVFrame * const p = frame;
89 uint8_t *in_buf, *encode_buf;
90 int x, y, z, length, tablesize, ret;
91 unsigned int width, height, depth, dimension;
92 unsigned int bytes_per_channel, pixmax, put_be;
94 #if FF_API_CODED_FRAME
95 FF_DISABLE_DEPRECATION_WARNINGS
96 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
97 avctx->coded_frame->key_frame = 1;
98 FF_ENABLE_DEPRECATION_WARNINGS
101 width = avctx->width;
102 height = avctx->height;
103 bytes_per_channel = 1;
105 put_be = HAVE_BIGENDIAN;
107 switch (avctx->pix_fmt) {
108 case AV_PIX_FMT_GRAY8:
109 dimension = SGI_SINGLE_CHAN;
110 depth = SGI_GRAYSCALE;
112 case AV_PIX_FMT_RGB24:
113 dimension = SGI_MULTI_CHAN;
116 case AV_PIX_FMT_RGBA:
117 dimension = SGI_MULTI_CHAN;
120 case AV_PIX_FMT_GRAY16LE:
121 put_be = !HAVE_BIGENDIAN;
122 case AV_PIX_FMT_GRAY16BE:
123 bytes_per_channel = 2;
125 dimension = SGI_SINGLE_CHAN;
126 depth = SGI_GRAYSCALE;
128 case AV_PIX_FMT_RGB48LE:
129 put_be = !HAVE_BIGENDIAN;
130 case AV_PIX_FMT_RGB48BE:
131 bytes_per_channel = 2;
133 dimension = SGI_MULTI_CHAN;
136 case AV_PIX_FMT_RGBA64LE:
137 put_be = !HAVE_BIGENDIAN;
138 case AV_PIX_FMT_RGBA64BE:
139 bytes_per_channel = 2;
141 dimension = SGI_MULTI_CHAN;
145 return AVERROR_INVALIDDATA;
148 tablesize = depth * height * 4;
149 length = SGI_HEADER_SIZE;
150 if (avctx->coder_type == FF_CODER_TYPE_RAW)
151 length += depth * height * width;
152 else // assume sgi_rle_encode() produces at most 2x size of input
153 length += tablesize * 2 + depth * height * (2 * width + 1);
155 if ((ret = ff_alloc_packet2(avctx, pkt, bytes_per_channel * length, 0)) < 0)
158 bytestream2_init_writer(&pbc, pkt->data, pkt->size);
161 bytestream2_put_be16(&pbc, SGI_MAGIC);
162 bytestream2_put_byte(&pbc, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0 */
163 bytestream2_put_byte(&pbc, bytes_per_channel);
164 bytestream2_put_be16(&pbc, dimension);
165 bytestream2_put_be16(&pbc, width);
166 bytestream2_put_be16(&pbc, height);
167 bytestream2_put_be16(&pbc, depth);
169 bytestream2_put_be32(&pbc, 0L); /* pixmin */
170 bytestream2_put_be32(&pbc, pixmax);
171 bytestream2_put_be32(&pbc, 0L); /* dummy */
174 bytestream2_skip_p(&pbc, 80);
177 bytestream2_put_be32(&pbc, 0L);
179 /* The rest of the 512 byte header is unused. */
180 bytestream2_skip_p(&pbc, 404);
182 if (avctx->coder_type != FF_CODER_TYPE_RAW) {
183 PutByteContext taboff_pcb, tablen_pcb;
185 /* Skip RLE offset table. */
186 bytestream2_init_writer(&taboff_pcb, pbc.buffer, tablesize);
187 bytestream2_skip_p(&pbc, tablesize);
189 /* Skip RLE length table. */
190 bytestream2_init_writer(&tablen_pcb, pbc.buffer, tablesize);
191 bytestream2_skip_p(&pbc, tablesize);
193 /* Make an intermediate consecutive buffer. */
194 if (!(encode_buf = av_malloc(width * bytes_per_channel)))
195 return AVERROR(ENOMEM);
197 for (z = 0; z < depth; z++) {
198 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
200 for (y = 0; y < height; y++) {
201 bytestream2_put_be32(&taboff_pcb, bytestream2_tell_p(&pbc));
203 for (x = 0; x < width * bytes_per_channel; x += bytes_per_channel)
204 encode_buf[x] = in_buf[depth * x];
206 length = sgi_rle_encode(&pbc, encode_buf, width,
213 bytestream2_put_be32(&tablen_pcb, length);
214 in_buf -= p->linesize[0];
220 for (z = 0; z < depth; z++) {
221 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
223 for (y = 0; y < height; y++) {
224 for (x = 0; x < width * depth; x += depth)
225 if (bytes_per_channel == 1)
226 bytestream2_put_byte(&pbc, in_buf[x]);
229 bytestream2_put_be16(&pbc, ((uint16_t *)in_buf)[x]);
231 bytestream2_put_le16(&pbc, ((uint16_t *)in_buf)[x]);
233 in_buf -= p->linesize[0];
239 pkt->size = bytestream2_tell_p(&pbc);
240 pkt->flags |= AV_PKT_FLAG_KEY;
246 AVCodec ff_sgi_encoder = {
248 .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
249 .type = AVMEDIA_TYPE_VIDEO,
250 .id = AV_CODEC_ID_SGI,
252 .encode2 = encode_frame,
253 .pix_fmts = (const enum AVPixelFormat[]) {
254 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
255 AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE,
256 AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE,
257 AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY8,