FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/sgienc.c
Date: 2026-05-03 03:13:14
Exec Total Coverage
Lines: 77 136 56.6%
Functions: 3 3 100.0%
Branches: 30 64 46.9%

Line Branch Exec Source
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/attributes.h"
23 #include "libavutil/mem.h"
24 #include "libavutil/opt.h"
25
26 #include "avcodec.h"
27 #include "bytestream.h"
28 #include "codec_internal.h"
29 #include "encode.h"
30 #include "sgi.h"
31 #include "rle.h"
32
33 #define SGI_SINGLE_CHAN 2
34 #define SGI_MULTI_CHAN 3
35
36 typedef struct SgiContext {
37 AVClass *class;
38
39 int rle;
40 } SgiContext;
41
42 1 static av_cold int encode_init(AVCodecContext *avctx)
43 {
44
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (avctx->width > 65535 || avctx->height > 65535) {
45 av_log(avctx, AV_LOG_ERROR, "Unsupported resolution %dx%d. "
46 "SGI does not support resolutions above 65535x65535\n",
47 avctx->width, avctx->height);
48 return AVERROR_INVALIDDATA;
49 }
50
51 1 return 0;
52 }
53
54 11232 static int sgi_rle_encode(PutByteContext *pbc, const uint8_t *src,
55 int w, int bpp)
56 {
57 11232 int val, count, x, start = bytestream2_tell_p(pbc);
58 void (*bytestream2_put)(PutByteContext *, unsigned int);
59
60
1/2
✓ Branch 0 taken 11232 times.
✗ Branch 1 not taken.
11232 if (bpp == 1)
61 11232 bytestream2_put = bytestream2_put_byte;
62 else
63 bytestream2_put = bytestream2_put_be16;
64
65
2/2
✓ Branch 0 taken 86081 times.
✓ Branch 1 taken 11232 times.
97313 for (x = 0; x < w; x += count) {
66 /* see if we can encode the next set of pixels with RLE */
67 86081 count = ff_rle_count_pixels(src, w - x, bpp, 1);
68
2/2
✓ Branch 0 taken 34170 times.
✓ Branch 1 taken 51911 times.
86081 if (count > 1) {
69
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 34170 times.
34170 if (bytestream2_get_bytes_left_p(pbc) < bpp * 2)
70 return AVERROR_INVALIDDATA;
71
72
1/2
✓ Branch 0 taken 34170 times.
✗ Branch 1 not taken.
34170 val = bpp == 1 ? *src : AV_RB16(src);
73 34170 bytestream2_put(pbc, count);
74 34170 bytestream2_put(pbc, val);
75 } else {
76 int i;
77 /* fall back on uncompressed */
78 51911 count = ff_rle_count_pixels(src, w - x, bpp, 0);
79
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51911 times.
51911 if (bytestream2_get_bytes_left_p(pbc) < bpp * (count + 1))
80 return AVERROR_INVALIDDATA;
81
82 51911 bytestream2_put(pbc, count + 0x80);
83
2/2
✓ Branch 0 taken 3782869 times.
✓ Branch 1 taken 51911 times.
3834780 for (i = 0; i < count; i++) {
84
1/2
✓ Branch 0 taken 3782869 times.
✗ Branch 1 not taken.
3782869 val = bpp == 1 ? src[i] : AV_RB16(src + i * bpp);
85 3782869 bytestream2_put(pbc, val);
86 }
87 }
88
89 86081 src += count * bpp;
90 }
91
92 11232 return bytestream2_tell_p(pbc) - start;
93 }
94
95 13 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
96 const AVFrame *frame, int *got_packet)
97 {
98 13 SgiContext *s = avctx->priv_data;
99 13 const AVFrame * const p = frame;
100 PutByteContext pbc;
101 uint8_t *encode_buf;
102 int x, y, z, length, tablesize, ret, i;
103 unsigned int width, height, depth, dimension;
104 unsigned int bytes_per_channel, pixmax, put_be;
105
106 13 width = avctx->width;
107 13 height = avctx->height;
108 13 bytes_per_channel = 1;
109 13 pixmax = 0xFF;
110 13 put_be = HAVE_BIGENDIAN;
111
112
1/10
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
13 switch (avctx->pix_fmt) {
113 case AV_PIX_FMT_GRAY8:
114 dimension = SGI_SINGLE_CHAN;
115 depth = SGI_GRAYSCALE;
116 break;
117 13 case AV_PIX_FMT_RGB24:
118 13 dimension = SGI_MULTI_CHAN;
119 13 depth = SGI_RGB;
120 13 break;
121 case AV_PIX_FMT_RGBA:
122 dimension = SGI_MULTI_CHAN;
123 depth = SGI_RGBA;
124 break;
125 case AV_PIX_FMT_GRAY16LE:
126 put_be = !HAVE_BIGENDIAN;
127 av_fallthrough;
128 case AV_PIX_FMT_GRAY16BE:
129 bytes_per_channel = 2;
130 pixmax = 0xFFFF;
131 dimension = SGI_SINGLE_CHAN;
132 depth = SGI_GRAYSCALE;
133 break;
134 case AV_PIX_FMT_RGB48LE:
135 put_be = !HAVE_BIGENDIAN;
136 av_fallthrough;
137 case AV_PIX_FMT_RGB48BE:
138 bytes_per_channel = 2;
139 pixmax = 0xFFFF;
140 dimension = SGI_MULTI_CHAN;
141 depth = SGI_RGB;
142 break;
143 case AV_PIX_FMT_RGBA64LE:
144 put_be = !HAVE_BIGENDIAN;
145 av_fallthrough;
146 case AV_PIX_FMT_RGBA64BE:
147 bytes_per_channel = 2;
148 pixmax = 0xFFFF;
149 dimension = SGI_MULTI_CHAN;
150 depth = SGI_RGBA;
151 break;
152 default:
153 return AVERROR_INVALIDDATA;
154 }
155
156 13 tablesize = depth * height * 4;
157 13 length = SGI_HEADER_SIZE;
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (!s->rle)
159 length += depth * height * width;
160 else // assume sgi_rle_encode() produces at most 2x size of input
161 13 length += tablesize * 2 + depth * height * (2 * width + 1);
162
163
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
13 if ((ret = ff_alloc_packet(avctx, pkt, bytes_per_channel * length)) < 0)
164 return ret;
165
166 13 bytestream2_init_writer(&pbc, pkt->data, pkt->size);
167
168 /* Encode header. */
169 13 bytestream2_put_be16(&pbc, SGI_MAGIC);
170 13 bytestream2_put_byte(&pbc, s->rle); /* RLE 1 - VERBATIM 0 */
171 13 bytestream2_put_byte(&pbc, bytes_per_channel);
172 13 bytestream2_put_be16(&pbc, dimension);
173 13 bytestream2_put_be16(&pbc, width);
174 13 bytestream2_put_be16(&pbc, height);
175 13 bytestream2_put_be16(&pbc, depth);
176
177 13 bytestream2_put_be32(&pbc, 0L); /* pixmin */
178 13 bytestream2_put_be32(&pbc, pixmax);
179 13 bytestream2_put_be32(&pbc, 0L); /* dummy */
180
181 /* name */
182
2/2
✓ Branch 0 taken 1040 times.
✓ Branch 1 taken 13 times.
1053 for (i = 0; i < 80; i++)
183 1040 bytestream2_put_byte(&pbc, 0L);
184
185 /* colormap */
186 13 bytestream2_put_be32(&pbc, 0L);
187
188 /* The rest of the 512 byte header is unused. */
189
2/2
✓ Branch 0 taken 5252 times.
✓ Branch 1 taken 13 times.
5265 for (i = 0; i < 404; i++)
190 5252 bytestream2_put_byte(&pbc, 0L);
191
192
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (s->rle) {
193 PutByteContext taboff_pcb, tablen_pcb;
194
195 /* Skip RLE offset table. */
196 13 bytestream2_init_writer(&taboff_pcb, pbc.buffer, tablesize);
197 13 bytestream2_skip_p(&pbc, tablesize);
198
199 /* Skip RLE length table. */
200 13 bytestream2_init_writer(&tablen_pcb, pbc.buffer, tablesize);
201 13 bytestream2_skip_p(&pbc, tablesize);
202
203 /* Make an intermediate consecutive buffer. */
204
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
13 if (!(encode_buf = av_malloc(width * bytes_per_channel)))
205 return AVERROR(ENOMEM);
206
207
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 13 times.
52 for (z = 0; z < depth; z++) {
208 39 const uint8_t *in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
209
210
2/2
✓ Branch 0 taken 11232 times.
✓ Branch 1 taken 39 times.
11271 for (y = 0; y < height; y++) {
211 11232 bytestream2_put_be32(&taboff_pcb, bytestream2_tell_p(&pbc));
212
213
2/2
✓ Branch 0 taken 3953664 times.
✓ Branch 1 taken 11232 times.
3964896 for (x = 0; x < width * bytes_per_channel; x += bytes_per_channel)
214
1/2
✓ Branch 0 taken 3953664 times.
✗ Branch 1 not taken.
3953664 if (bytes_per_channel == 1) {
215 3953664 encode_buf[x] = in_buf[depth * x];
216 } else if (HAVE_BIGENDIAN ^ put_be) {
217 encode_buf[x + 1] = in_buf[depth * x];
218 encode_buf[x] = in_buf[depth * x + 1];
219 } else {
220 encode_buf[x] = in_buf[depth * x];
221 encode_buf[x + 1] = in_buf[depth * x + 1];
222 }
223
224 11232 length = sgi_rle_encode(&pbc, encode_buf, width,
225 bytes_per_channel);
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11232 times.
11232 if (length < 1) {
227 av_free(encode_buf);
228 return AVERROR_INVALIDDATA;
229 }
230
231 11232 bytestream2_put_be32(&tablen_pcb, length);
232 11232 in_buf -= p->linesize[0];
233 }
234 }
235
236 13 av_free(encode_buf);
237 } else {
238 for (z = 0; z < depth; z++) {
239 const uint8_t *in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
240
241 for (y = 0; y < height; y++) {
242 for (x = 0; x < width * depth; x += depth)
243 if (bytes_per_channel == 1)
244 bytestream2_put_byte(&pbc, in_buf[x]);
245 else
246 if (put_be)
247 bytestream2_put_be16(&pbc, ((uint16_t *)in_buf)[x]);
248 else
249 bytestream2_put_le16(&pbc, ((uint16_t *)in_buf)[x]);
250
251 in_buf -= p->linesize[0];
252 }
253 }
254 }
255
256 /* total length */
257 13 pkt->size = bytestream2_tell_p(&pbc);
258 13 *got_packet = 1;
259
260 13 return 0;
261 }
262
263 #define OFFSET(x) offsetof(SgiContext, x)
264 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
265 static const AVOption options[] = {
266 { "rle", "Use run-length compression", OFFSET(rle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
267
268 { NULL },
269 };
270
271 static const AVClass sgi_class = {
272 .class_name = "sgi",
273 .item_name = av_default_item_name,
274 .option = options,
275 .version = LIBAVUTIL_VERSION_INT,
276 };
277
278 const FFCodec ff_sgi_encoder = {
279 .p.name = "sgi",
280 CODEC_LONG_NAME("SGI image"),
281 .p.type = AVMEDIA_TYPE_VIDEO,
282 .p.id = AV_CODEC_ID_SGI,
283 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
284 .priv_data_size = sizeof(SgiContext),
285 .p.priv_class = &sgi_class,
286 .init = encode_init,
287 FF_CODEC_ENCODE_CB(encode_frame),
288 CODEC_PIXFMTS(AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
289 AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE,
290 AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE,
291 AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY8),
292 };
293