FFmpeg
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 
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 
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 
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  width = avctx->width;
104  height = avctx->height;
105  bytes_per_channel = 1;
106  pixmax = 0xFF;
107  put_be = HAVE_BIGENDIAN;
108 
109  switch (avctx->pix_fmt) {
110  case AV_PIX_FMT_GRAY8:
112  depth = SGI_GRAYSCALE;
113  break;
114  case AV_PIX_FMT_RGB24:
116  depth = SGI_RGB;
117  break;
118  case AV_PIX_FMT_RGBA:
120  depth = SGI_RGBA;
121  break;
122  case AV_PIX_FMT_GRAY16LE:
123  put_be = !HAVE_BIGENDIAN;
124  case AV_PIX_FMT_GRAY16BE:
125  bytes_per_channel = 2;
126  pixmax = 0xFFFF;
128  depth = SGI_GRAYSCALE;
129  break;
130  case AV_PIX_FMT_RGB48LE:
131  put_be = !HAVE_BIGENDIAN;
132  case AV_PIX_FMT_RGB48BE:
133  bytes_per_channel = 2;
134  pixmax = 0xFFFF;
136  depth = SGI_RGB;
137  break;
138  case AV_PIX_FMT_RGBA64LE:
139  put_be = !HAVE_BIGENDIAN;
140  case AV_PIX_FMT_RGBA64BE:
141  bytes_per_channel = 2;
142  pixmax = 0xFFFF;
144  depth = SGI_RGBA;
145  break;
146  default:
147  return AVERROR_INVALIDDATA;
148  }
149 
150  tablesize = depth * height * 4;
151  length = SGI_HEADER_SIZE;
152  if (!s->rle)
153  length += depth * height * width;
154  else // assume sgi_rle_encode() produces at most 2x size of input
155  length += tablesize * 2 + depth * height * (2 * width + 1);
156 
157  if ((ret = ff_alloc_packet2(avctx, pkt, bytes_per_channel * length, 0)) < 0)
158  return ret;
159 
161 
162  /* Encode header. */
163  bytestream2_put_be16(&pbc, SGI_MAGIC);
164  bytestream2_put_byte(&pbc, s->rle); /* RLE 1 - VERBATIM 0 */
165  bytestream2_put_byte(&pbc, bytes_per_channel);
166  bytestream2_put_be16(&pbc, dimension);
167  bytestream2_put_be16(&pbc, width);
168  bytestream2_put_be16(&pbc, height);
169  bytestream2_put_be16(&pbc, depth);
170 
171  bytestream2_put_be32(&pbc, 0L); /* pixmin */
172  bytestream2_put_be32(&pbc, pixmax);
173  bytestream2_put_be32(&pbc, 0L); /* dummy */
174 
175  /* name */
176  for (i = 0; i < 80; i++)
177  bytestream2_put_byte(&pbc, 0L);
178 
179  /* colormap */
180  bytestream2_put_be32(&pbc, 0L);
181 
182  /* The rest of the 512 byte header is unused. */
183  for (i = 0; i < 404; i++)
184  bytestream2_put_byte(&pbc, 0L);
185 
186  if (s->rle) {
187  PutByteContext taboff_pcb, tablen_pcb;
188 
189  /* Skip RLE offset table. */
190  bytestream2_init_writer(&taboff_pcb, pbc.buffer, tablesize);
191  bytestream2_skip_p(&pbc, tablesize);
192 
193  /* Skip RLE length table. */
194  bytestream2_init_writer(&tablen_pcb, pbc.buffer, tablesize);
195  bytestream2_skip_p(&pbc, tablesize);
196 
197  /* Make an intermediate consecutive buffer. */
198  if (!(encode_buf = av_malloc(width * bytes_per_channel)))
199  return AVERROR(ENOMEM);
200 
201  for (z = 0; z < depth; z++) {
202  in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
203 
204  for (y = 0; y < height; y++) {
205  bytestream2_put_be32(&taboff_pcb, bytestream2_tell_p(&pbc));
206 
207  for (x = 0; x < width * bytes_per_channel; x += bytes_per_channel)
208  if (bytes_per_channel == 1) {
209  encode_buf[x] = in_buf[depth * x];
210  } else if (HAVE_BIGENDIAN ^ put_be) {
211  encode_buf[x + 1] = in_buf[depth * x];
212  encode_buf[x] = in_buf[depth * x + 1];
213  } else {
214  encode_buf[x] = in_buf[depth * x];
215  encode_buf[x + 1] = in_buf[depth * x + 1];
216  }
217 
218  length = sgi_rle_encode(&pbc, encode_buf, width,
219  bytes_per_channel);
220  if (length < 1) {
221  av_free(encode_buf);
222  return AVERROR_INVALIDDATA;
223  }
224 
225  bytestream2_put_be32(&tablen_pcb, length);
226  in_buf -= p->linesize[0];
227  }
228  }
229 
230  av_free(encode_buf);
231  } else {
232  for (z = 0; z < depth; z++) {
233  in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
234 
235  for (y = 0; y < height; y++) {
236  for (x = 0; x < width * depth; x += depth)
237  if (bytes_per_channel == 1)
238  bytestream2_put_byte(&pbc, in_buf[x]);
239  else
240  if (put_be)
241  bytestream2_put_be16(&pbc, ((uint16_t *)in_buf)[x]);
242  else
243  bytestream2_put_le16(&pbc, ((uint16_t *)in_buf)[x]);
244 
245  in_buf -= p->linesize[0];
246  }
247  }
248  }
249 
250  /* total length */
251  pkt->size = bytestream2_tell_p(&pbc);
253  *got_packet = 1;
254 
255  return 0;
256 }
257 
258 #define OFFSET(x) offsetof(SgiContext, x)
259 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
260 static const AVOption options[] = {
261  { "rle", "Use run-length compression", OFFSET(rle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
262 
263  { NULL },
264 };
265 
266 static const AVClass sgi_class = {
267  .class_name = "sgi",
268  .item_name = av_default_item_name,
269  .option = options,
270  .version = LIBAVUTIL_VERSION_INT,
271 };
272 
274  .name = "sgi",
275  .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
276  .type = AVMEDIA_TYPE_VIDEO,
277  .id = AV_CODEC_ID_SGI,
278  .priv_data_size = sizeof(SgiContext),
279  .priv_class = &sgi_class,
280  .init = encode_init,
281  .encode2 = encode_frame,
282  .pix_fmts = (const enum AVPixelFormat[]) {
288  },
289  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
290 };
AVCodec
AVCodec.
Definition: codec.h:197
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:41
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
rle.h
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
AV_PIX_FMT_RGBA64BE
@ 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:195
w
uint8_t w
Definition: llviddspenc.c:39
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:365
AVOption
AVOption.
Definition: opt.h:248
bytestream2_tell_p
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
Definition: bytestream.h:197
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:396
rle
static int rle(uint8_t *dst, const uint8_t *src, int compressed_size, int uncompressed_size)
Definition: exr.c:215
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:317
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
OFFSET
#define OFFSET(x)
Definition: sgienc.c:258
AV_PIX_FMT_GRAY16BE
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
val
static double val(void *priv, double ch)
Definition: aeval.c:76
encode_init
static av_cold int encode_init(AVCodecContext *avctx)
Definition: sgienc.c:39
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:181
bytestream2_get_bytes_left_p
static av_always_inline int bytestream2_get_bytes_left_p(PutByteContext *p)
Definition: bytestream.h:163
av_cold
#define av_cold
Definition: attributes.h:90
bytestream2_init_writer
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:147
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:257
options
static const AVOption options[]
Definition: sgienc.c:260
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:289
sgi_class
static const AVClass sgi_class
Definition: sgienc.c:266
SGI_MAGIC
#define SGI_MAGIC
SGI image file signature.
Definition: sgi.h:28
SGI_SINGLE_CHAN
#define SGI_SINGLE_CHAN
Definition: sgienc.c:30
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
SGI_RGBA
#define SGI_RGBA
Definition: sgi.h:34
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
AV_PIX_FMT_RGB48LE
@ 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:103
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
AV_PIX_FMT_RGBA64LE
@ 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:196
src
#define src
Definition: vp8dsp.c:255
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
SgiContext
Definition: sgienc.c:33
PutByteContext
Definition: bytestream.h:37
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
AVPacket::size
int size
Definition: packet.h:366
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
PutByteContext::buffer
uint8_t * buffer
Definition: bytestream.h:38
SgiContext::rle
int rle
Definition: sgienc.c:36
height
#define height
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:371
encode_frame
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: sgienc.c:92
sgi.h
SGI_RGB
#define SGI_RGB
Definition: sgi.h:33
i
int i
Definition: input.c:407
AV_PIX_FMT_RGB48BE
@ 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:102
SGI_MULTI_CHAN
#define SGI_MULTI_CHAN
Definition: sgienc.c:31
ff_sgi_encoder
const AVCodec ff_sgi_encoder
Definition: sgienc.c:273
bytestream2_skip_p
static av_always_inline void bytestream2_skip_p(PutByteContext *p, unsigned int size)
Definition: bytestream.h:180
ff_rle_count_pixels
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:27
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:204
AVCodecContext::height
int height
Definition: avcodec.h:674
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:711
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
L
#define L(x)
Definition: vp56_arith.h:36
AVCodecContext
main external API structure.
Definition: avcodec.h:501
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_GRAY16LE
@ AV_PIX_FMT_GRAY16LE
Y , 16bpp, little-endian.
Definition: pixfmt.h:98
SGI_HEADER_SIZE
#define SGI_HEADER_SIZE
Definition: sgi.h:30
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:528
AVPacket
This structure stores compressed data.
Definition: packet.h:342
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:674
bytestream.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:334
sgi_rle_encode
static int sgi_rle_encode(PutByteContext *pbc, const uint8_t *src, int w, int bpp)
Definition: sgienc.c:51
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AV_CODEC_ID_SGI
@ AV_CODEC_ID_SGI
Definition: codec_id.h:150
dimension
The official guide to swscale for confused that consecutive non overlapping rectangles of dimension(0, slice_top) -(picture_width
SGI_GRAYSCALE
#define SGI_GRAYSCALE
Definition: sgi.h:32
ff_alloc_packet2
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:33
int
int
Definition: ffmpeg_filter.c:156
VE
#define VE
Definition: sgienc.c:259
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98