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/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 
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);
48  return AVERROR_INVALIDDATA;
49  }
50 
51  return 0;
52 }
53 
54 static 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)
70  return AVERROR_INVALIDDATA;
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))
80  return AVERROR_INVALIDDATA;
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:
115  depth = SGI_GRAYSCALE;
116  break;
117  case AV_PIX_FMT_RGB24:
119  depth = SGI_RGB;
120  break;
121  case AV_PIX_FMT_RGBA:
123  depth = SGI_RGBA;
124  break;
125  case AV_PIX_FMT_GRAY16LE:
126  put_be = !HAVE_BIGENDIAN;
128  case AV_PIX_FMT_GRAY16BE:
129  bytes_per_channel = 2;
130  pixmax = 0xFFFF;
132  depth = SGI_GRAYSCALE;
133  break;
134  case AV_PIX_FMT_RGB48LE:
135  put_be = !HAVE_BIGENDIAN;
137  case AV_PIX_FMT_RGB48BE:
138  bytes_per_channel = 2;
139  pixmax = 0xFFFF;
141  depth = SGI_RGB;
142  break;
143  case AV_PIX_FMT_RGBA64LE:
144  put_be = !HAVE_BIGENDIAN;
146  case AV_PIX_FMT_RGBA64BE:
147  bytes_per_channel = 2;
148  pixmax = 0xFFFF;
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 
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
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 
279  .p.name = "sgi",
280  CODEC_LONG_NAME("SGI image"),
281  .p.type = AVMEDIA_TYPE_VIDEO,
282  .p.id = AV_CODEC_ID_SGI,
284  .priv_data_size = sizeof(SgiContext),
285  .p.priv_class = &sgi_class,
286  .init = encode_init,
292 };
CODEC_PIXFMTS
#define CODEC_PIXFMTS(...)
Definition: codec_internal.h:392
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:435
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:202
AVPacket::data
uint8_t * data
Definition: packet.h:595
AVOption
AVOption.
Definition: opt.h:429
encode.h
FFCodec
Definition: codec_internal.h:127
rle
static int rle(uint8_t *dst, const uint8_t *src, int compressed_size, int uncompressed_size)
Definition: exr.c:225
OFFSET
#define OFFSET(x)
Definition: sgienc.c:263
AV_PIX_FMT_GRAY16BE
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:104
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
val
static double val(void *priv, double ch)
Definition: aeval.c:77
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:359
encode_init
static av_cold int encode_init(AVCodecContext *avctx)
Definition: sgienc.c:42
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
av_cold
#define av_cold
Definition: attributes.h:119
bytestream2_init_writer
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:147
s
#define s(width, name)
Definition: cbs_vp9.c:198
bytestream2_tell_p
static av_always_inline int bytestream2_tell_p(const PutByteContext *p)
Definition: bytestream.h:197
options
static const AVOption options[]
Definition: sgienc.c:265
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#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:144
sgi_class
static const AVClass sgi_class
Definition: sgienc.c:271
SGI_MAGIC
#define SGI_MAGIC
SGI image file signature.
Definition: sgi.h:28
SGI_SINGLE_CHAN
#define SGI_SINGLE_CHAN
Definition: sgienc.c:33
av_fallthrough
#define av_fallthrough
Definition: attributes.h:67
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:332
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:100
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:76
bytestream2_get_bytes_left_p
static av_always_inline int bytestream2_get_bytes_left_p(const PutByteContext *p)
Definition: bytestream.h:163
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:110
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:242
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:203
options
Definition: swscale.c:45
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
SgiContext
Definition: sgienc.c:36
PutByteContext
Definition: bytestream.h:37
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:75
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:596
height
#define height
Definition: dsp.h:89
codec_internal.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
PutByteContext::buffer
uint8_t * buffer
Definition: bytestream.h:38
SgiContext::rle
int rle
Definition: sgienc.c:39
attributes.h
encode_frame
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: sgienc.c:95
sgi.h
SGI_RGB
#define SGI_RGB
Definition: sgi.h:33
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:109
av_malloc
#define av_malloc(s)
Definition: ops_asmgen.c:44
SGI_MULTI_CHAN
#define SGI_MULTI_CHAN
Definition: sgienc.c:34
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:28
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
AVCodecContext::height
int height
Definition: avcodec.h:600
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:639
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:81
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:265
AVCodecContext
main external API structure.
Definition: avcodec.h:439
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
L
#define L(x)
Definition: vpx_arith.h:36
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
AV_PIX_FMT_GRAY16LE
@ AV_PIX_FMT_GRAY16LE
Y , 16bpp, little-endian.
Definition: pixfmt.h:105
mem.h
w
uint8_t w
Definition: llvidencdsp.c:39
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:466
AVPacket
This structure stores compressed data.
Definition: packet.h:572
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:600
bytestream.h
sgi_rle_encode
static int sgi_rle_encode(PutByteContext *pbc, const uint8_t *src, int w, int bpp)
Definition: sgienc.c:54
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_CODEC_ID_SGI
@ AV_CODEC_ID_SGI
Definition: codec_id.h:153
ff_sgi_encoder
const FFCodec ff_sgi_encoder
Definition: sgienc.c:278
pkt
static AVPacket * pkt
Definition: demux_decode.c:55
dimension
The official guide to swscale for confused that consecutive non overlapping rectangles of dimension(0, slice_top) -(picture_width
width
#define width
Definition: dsp.h:89
SGI_GRAYSCALE
#define SGI_GRAYSCALE
Definition: sgi.h:32
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:61
src
#define src
Definition: vp8dsp.c:248
VE
#define VE
Definition: sgienc.c:264
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