]> git.sesse.net Git - ffmpeg/blob - libavcodec/sgienc.c
2a898f5a4ffaae671c2f4b55ecc93acb794c4667
[ffmpeg] / libavcodec / sgienc.c
1 /*
2  * SGI image encoder
3  * Todd Kirby <doubleshot@pacbell.net>
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "libavutil/opt.h"
23
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27 #include "sgi.h"
28 #include "rle.h"
29
30 #define SGI_SINGLE_CHAN 2
31 #define SGI_MULTI_CHAN 3
32
33 typedef struct SgiContext {
34     AVClass *class;
35
36     int rle;
37 } SgiContext;
38
39 static av_cold int encode_init(AVCodecContext *avctx)
40 {
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;
46     }
47
48     return 0;
49 }
50
51 static int sgi_rle_encode(PutByteContext *pbc, const uint8_t *src,
52                           int w, int bpp)
53 {
54     int val, count, x, start = bytestream2_tell_p(pbc);
55     void (*bytestream2_put)(PutByteContext *, unsigned int);
56
57     if (bpp == 1)
58         bytestream2_put = bytestream2_put_byte;
59     else
60         bytestream2_put = bytestream2_put_be16;
61
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);
65         if (count > 1) {
66             if (bytestream2_get_bytes_left_p(pbc) < bpp * 2)
67                 return AVERROR_INVALIDDATA;
68
69             val = bpp == 1 ? *src : AV_RB16(src);
70             bytestream2_put(pbc, count);
71             bytestream2_put(pbc, val);
72         } else {
73             int i;
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;
78
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);
83             }
84         }
85
86         src += count * bpp;
87     }
88
89     return bytestream2_tell_p(pbc) - start;
90 }
91
92 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
93                         const AVFrame *frame, int *got_packet)
94 {
95     SgiContext *s = avctx->priv_data;
96     const AVFrame * const p = frame;
97     PutByteContext pbc;
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;
102
103 #if FF_API_CODED_FRAME
104 FF_DISABLE_DEPRECATION_WARNINGS
105     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
106     avctx->coded_frame->key_frame = 1;
107 FF_ENABLE_DEPRECATION_WARNINGS
108 #endif
109
110     width  = avctx->width;
111     height = avctx->height;
112     bytes_per_channel = 1;
113     pixmax = 0xFF;
114     put_be = HAVE_BIGENDIAN;
115
116     switch (avctx->pix_fmt) {
117     case AV_PIX_FMT_GRAY8:
118         dimension = SGI_SINGLE_CHAN;
119         depth     = SGI_GRAYSCALE;
120         break;
121     case AV_PIX_FMT_RGB24:
122         dimension = SGI_MULTI_CHAN;
123         depth     = SGI_RGB;
124         break;
125     case AV_PIX_FMT_RGBA:
126         dimension = SGI_MULTI_CHAN;
127         depth     = SGI_RGBA;
128         break;
129     case AV_PIX_FMT_GRAY16LE:
130         put_be = !HAVE_BIGENDIAN;
131     case AV_PIX_FMT_GRAY16BE:
132         bytes_per_channel = 2;
133         pixmax = 0xFFFF;
134         dimension = SGI_SINGLE_CHAN;
135         depth     = SGI_GRAYSCALE;
136         break;
137     case AV_PIX_FMT_RGB48LE:
138         put_be = !HAVE_BIGENDIAN;
139     case AV_PIX_FMT_RGB48BE:
140         bytes_per_channel = 2;
141         pixmax = 0xFFFF;
142         dimension = SGI_MULTI_CHAN;
143         depth     = SGI_RGB;
144         break;
145     case AV_PIX_FMT_RGBA64LE:
146         put_be = !HAVE_BIGENDIAN;
147     case AV_PIX_FMT_RGBA64BE:
148         bytes_per_channel = 2;
149         pixmax = 0xFFFF;
150         dimension = SGI_MULTI_CHAN;
151         depth     = SGI_RGBA;
152         break;
153     default:
154         return AVERROR_INVALIDDATA;
155     }
156
157     tablesize = depth * height * 4;
158     length = SGI_HEADER_SIZE;
159     if (!s->rle)
160         length += depth * height * width;
161     else // assume sgi_rle_encode() produces at most 2x size of input
162         length += tablesize * 2 + depth * height * (2 * width + 1);
163
164     if ((ret = ff_alloc_packet2(avctx, pkt, bytes_per_channel * length, 0)) < 0)
165         return ret;
166
167     bytestream2_init_writer(&pbc, pkt->data, pkt->size);
168
169     /* Encode header. */
170     bytestream2_put_be16(&pbc, SGI_MAGIC);
171     bytestream2_put_byte(&pbc, s->rle); /* RLE 1 - VERBATIM 0 */
172     bytestream2_put_byte(&pbc, bytes_per_channel);
173     bytestream2_put_be16(&pbc, dimension);
174     bytestream2_put_be16(&pbc, width);
175     bytestream2_put_be16(&pbc, height);
176     bytestream2_put_be16(&pbc, depth);
177
178     bytestream2_put_be32(&pbc, 0L); /* pixmin */
179     bytestream2_put_be32(&pbc, pixmax);
180     bytestream2_put_be32(&pbc, 0L); /* dummy */
181
182     /* name */
183     for (i = 0; i < 80; i++)
184         bytestream2_put_byte(&pbc, 0L);
185
186     /* colormap */
187     bytestream2_put_be32(&pbc, 0L);
188
189     /* The rest of the 512 byte header is unused. */
190     for (i = 0; i < 404; i++)
191         bytestream2_put_byte(&pbc, 0L);
192
193     if (s->rle) {
194         PutByteContext taboff_pcb, tablen_pcb;
195
196         /* Skip RLE offset table. */
197         bytestream2_init_writer(&taboff_pcb, pbc.buffer, tablesize);
198         bytestream2_skip_p(&pbc, tablesize);
199
200         /* Skip RLE length table. */
201         bytestream2_init_writer(&tablen_pcb, pbc.buffer, tablesize);
202         bytestream2_skip_p(&pbc, tablesize);
203
204         /* Make an intermediate consecutive buffer. */
205         if (!(encode_buf = av_malloc(width * bytes_per_channel)))
206             return AVERROR(ENOMEM);
207
208         for (z = 0; z < depth; z++) {
209             in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
210
211             for (y = 0; y < height; y++) {
212                 bytestream2_put_be32(&taboff_pcb, bytestream2_tell_p(&pbc));
213
214                 for (x = 0; x < width * bytes_per_channel; x += bytes_per_channel)
215                     if (bytes_per_channel == 1) {
216                         encode_buf[x]     = in_buf[depth * x];
217                     } else if (HAVE_BIGENDIAN ^ put_be) {
218                         encode_buf[x + 1] = in_buf[depth * x];
219                         encode_buf[x]     = in_buf[depth * x + 1];
220                     } else {
221                         encode_buf[x]     = in_buf[depth * x];
222                         encode_buf[x + 1] = in_buf[depth * x + 1];
223                     }
224
225                 length = sgi_rle_encode(&pbc, encode_buf, width,
226                                         bytes_per_channel);
227                 if (length < 1) {
228                     av_free(encode_buf);
229                     return AVERROR_INVALIDDATA;
230                 }
231
232                 bytestream2_put_be32(&tablen_pcb, length);
233                 in_buf -= p->linesize[0];
234             }
235         }
236
237         av_free(encode_buf);
238     } else {
239         for (z = 0; z < depth; z++) {
240             in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
241
242             for (y = 0; y < height; y++) {
243                 for (x = 0; x < width * depth; x += depth)
244                     if (bytes_per_channel == 1)
245                         bytestream2_put_byte(&pbc, in_buf[x]);
246                     else
247                         if (put_be)
248                             bytestream2_put_be16(&pbc, ((uint16_t *)in_buf)[x]);
249                         else
250                             bytestream2_put_le16(&pbc, ((uint16_t *)in_buf)[x]);
251
252                 in_buf -= p->linesize[0];
253             }
254         }
255     }
256
257     /* total length */
258     pkt->size   = bytestream2_tell_p(&pbc);
259     pkt->flags |= AV_PKT_FLAG_KEY;
260     *got_packet = 1;
261
262     return 0;
263 }
264
265 #define OFFSET(x) offsetof(SgiContext, x)
266 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
267 static const AVOption options[] = {
268     { "rle", "Use run-length compression", OFFSET(rle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
269
270     { NULL },
271 };
272
273 static const AVClass sgi_class = {
274     .class_name = "sgi",
275     .item_name  = av_default_item_name,
276     .option     = options,
277     .version    = LIBAVUTIL_VERSION_INT,
278 };
279
280 AVCodec ff_sgi_encoder = {
281     .name      = "sgi",
282     .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
283     .type      = AVMEDIA_TYPE_VIDEO,
284     .id        = AV_CODEC_ID_SGI,
285     .priv_data_size = sizeof(SgiContext),
286     .priv_class = &sgi_class,
287     .init      = encode_init,
288     .encode2   = encode_frame,
289     .pix_fmts  = (const enum AVPixelFormat[]) {
290         AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
291         AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE,
292         AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE,
293         AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY8,
294         AV_PIX_FMT_NONE
295     },
296     .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
297 };