FFmpeg
Loading...
Searching...
No Matches
sgienc.c
Go to the documentation of this file.
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
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
36typedef struct SgiContext {
37 AVClass *class;
38
39 int rle;
41
43{
44 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);
49 }
50
51 return 0;
52}
53
54static int sgi_rle_encode(PutByteContext *pbc, const uint8_t *src,
55 int w, int bpp)
56{
57 int val, count, x, start = bytestream2_tell_p(pbc);
58 void (*bytestream2_put)(PutByteContext *, unsigned int);
59
60 if (bpp == 1)
61 bytestream2_put = bytestream2_put_byte;
62 else
63 bytestream2_put = bytestream2_put_be16;
64
65 for (x = 0; x < w; x += count) {
66 /* see if we can encode the next set of pixels with RLE */
67 count = ff_rle_count_pixels(src, w - x, bpp, 1);
68 if (count > 1) {
69 if (bytestream2_get_bytes_left_p(pbc) < bpp * 2)
71
72 val = bpp == 1 ? *src : AV_RB16(src);
73 bytestream2_put(pbc, count);
74 bytestream2_put(pbc, val);
75 } else {
76 int i;
77 /* fall back on uncompressed */
78 count = ff_rle_count_pixels(src, w - x, bpp, 0);
79 if (bytestream2_get_bytes_left_p(pbc) < bpp * (count + 1))
81
82 bytestream2_put(pbc, count + 0x80);
83 for (i = 0; i < count; i++) {
84 val = bpp == 1 ? src[i] : AV_RB16(src + i * bpp);
85 bytestream2_put(pbc, val);
86 }
87 }
88
89 src += count * bpp;
90 }
91
92 return bytestream2_tell_p(pbc) - start;
93}
94
96 const AVFrame *frame, int *got_packet)
97{
98 SgiContext *s = avctx->priv_data;
99 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 width = avctx->width;
107 height = avctx->height;
108 bytes_per_channel = 1;
109 pixmax = 0xFF;
110 put_be = HAVE_BIGENDIAN;
111
112 switch (avctx->pix_fmt) {
113 case AV_PIX_FMT_GRAY8:
114 dimension = SGI_SINGLE_CHAN;
115 depth = SGI_GRAYSCALE;
116 break;
117 case AV_PIX_FMT_RGB24:
118 dimension = SGI_MULTI_CHAN;
119 depth = SGI_RGB;
120 break;
121 case AV_PIX_FMT_RGBA:
122 dimension = SGI_MULTI_CHAN;
123 depth = SGI_RGBA;
124 break;
126 put_be = !HAVE_BIGENDIAN;
129 bytes_per_channel = 2;
130 pixmax = 0xFFFF;
131 dimension = SGI_SINGLE_CHAN;
132 depth = SGI_GRAYSCALE;
133 break;
135 put_be = !HAVE_BIGENDIAN;
138 bytes_per_channel = 2;
139 pixmax = 0xFFFF;
140 dimension = SGI_MULTI_CHAN;
141 depth = SGI_RGB;
142 break;
144 put_be = !HAVE_BIGENDIAN;
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 tablesize = depth * height * 4;
157 length = SGI_HEADER_SIZE;
158 if (!s->rle)
159 length += depth * height * width;
160 else // assume sgi_rle_encode() produces at most 2x size of input
161 length += tablesize * 2 + depth * height * (2 * width + 1);
162
163 if ((ret = ff_alloc_packet(avctx, pkt, bytes_per_channel * length)) < 0)
164 return ret;
165
166 bytestream2_init_writer(&pbc, pkt->data, pkt->size);
167
168 /* Encode header. */
169 bytestream2_put_be16(&pbc, SGI_MAGIC);
170 bytestream2_put_byte(&pbc, s->rle); /* RLE 1 - VERBATIM 0 */
171 bytestream2_put_byte(&pbc, bytes_per_channel);
172 bytestream2_put_be16(&pbc, dimension);
173 bytestream2_put_be16(&pbc, width);
174 bytestream2_put_be16(&pbc, height);
175 bytestream2_put_be16(&pbc, depth);
176
177 bytestream2_put_be32(&pbc, 0L); /* pixmin */
178 bytestream2_put_be32(&pbc, pixmax);
179 bytestream2_put_be32(&pbc, 0L); /* dummy */
180
181 /* name */
182 for (i = 0; i < 80; i++)
183 bytestream2_put_byte(&pbc, 0L);
184
185 /* colormap */
186 bytestream2_put_be32(&pbc, 0L);
187
188 /* The rest of the 512 byte header is unused. */
189 for (i = 0; i < 404; i++)
190 bytestream2_put_byte(&pbc, 0L);
191
192 if (s->rle) {
193 PutByteContext taboff_pcb, tablen_pcb;
194
195 /* Skip RLE offset table. */
196 bytestream2_init_writer(&taboff_pcb, pbc.buffer, tablesize);
197 bytestream2_skip_p(&pbc, tablesize);
198
199 /* Skip RLE length table. */
200 bytestream2_init_writer(&tablen_pcb, pbc.buffer, tablesize);
201 bytestream2_skip_p(&pbc, tablesize);
202
203 /* Make an intermediate consecutive buffer. */
204 if (!(encode_buf = av_malloc(width * bytes_per_channel)))
205 return AVERROR(ENOMEM);
206
207 for (z = 0; z < depth; z++) {
208 const uint8_t *in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
209
210 for (y = 0; y < height; y++) {
211 bytestream2_put_be32(&taboff_pcb, bytestream2_tell_p(&pbc));
212
213 for (x = 0; x < width * bytes_per_channel; x += bytes_per_channel)
214 if (bytes_per_channel == 1) {
215 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 length = sgi_rle_encode(&pbc, encode_buf, width,
225 bytes_per_channel);
226 if (length < 1) {
227 av_free(encode_buf);
228 return AVERROR_INVALIDDATA;
229 }
230
231 bytestream2_put_be32(&tablen_pcb, length);
232 in_buf -= p->linesize[0];
233 }
234 }
235
236 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 pkt->size = bytestream2_tell_p(&pbc);
258 *got_packet = 1;
259
260 return 0;
261}
262
263#define OFFSET(x) offsetof(SgiContext, x)
264#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
265static 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
271static 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
static double val(void *priv, double ch)
Definition aeval.c:77
const FFCodec ff_sgi_encoder
Definition sgienc.c:278
#define VE
Definition amfenc_av1.c:30
#define L(x)
Definition vpx_arith.h:36
static av_cold int encode_init(AVCodecContext *avctx)
Definition asvenc.c:373
Libavcodec external API header.
static av_always_inline int bytestream2_tell_p(const PutByteContext *p)
Definition bytestream.h:197
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition bytestream.h:147
static av_always_inline void bytestream2_skip_p(PutByteContext *p, unsigned int size)
Definition bytestream.h:180
static av_always_inline int bytestream2_get_bytes_left_p(const PutByteContext *p)
Definition bytestream.h:163
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define NULL
Definition coverity.c:32
static AVPacket * pkt
static AVFrame * frame
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition encode.c:62
static int rle(uint8_t *dst, const uint8_t *src, int compressed_size, int uncompressed_size)
Definition exr.c:217
static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame, AVPacket *pkt)
Definition ffmpeg_enc.c:694
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_SGI
Definition codec_id.h:151
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define AV_RB16(p)
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
#define av_cold
Definition attributes.h:117
uint8_t w
Definition llvidencdsp.c:39
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition pixfmt.h:104
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_RGB48BE
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
Definition pixfmt.h:109
@ AV_PIX_FMT_RGBA64BE
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
Definition pixfmt.h:202
@ AV_PIX_FMT_RGBA64LE
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
Definition pixfmt.h:203
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
@ AV_PIX_FMT_RGB48LE
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as lit...
Definition pixfmt.h:110
@ AV_PIX_FMT_GRAY16LE
Y , 16bpp, little-endian.
Definition pixfmt.h:105
int ff_rle_count_pixels(const uint8_t *start, int len, int bpp, int same)
Count up to 127 consecutive pixels which are either all the same or all differ from the previous and ...
Definition rle.c:28
#define SGI_MAGIC
SGI image file signature.
Definition sgi.h:28
#define SGI_RGB
Definition sgi.h:33
#define SGI_GRAYSCALE
Definition sgi.h:32
#define SGI_HEADER_SIZE
Definition sgi.h:30
#define SGI_RGBA
Definition sgi.h:34
#define SGI_SINGLE_CHAN
Definition sgienc.c:33
static int sgi_rle_encode(PutByteContext *pbc, const uint8_t *src, int w, int bpp)
Definition sgienc.c:54
static av_cold int encode_init(AVCodecContext *avctx)
Definition sgienc.c:42
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition sgienc.c:95
#define SGI_MULTI_CHAN
Definition sgienc.c:34
#define OFFSET(x)
Definition sgienc.c:263
static const AVClass sgi_class
Definition sgienc.c:271
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
uint8_t * buffer
Definition bytestream.h:38
int rle
Definition sgienc.c:39
#define av_free(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89