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/mem.h"
23 #include "libavutil/opt.h"
24 
25 #include "avcodec.h"
26 #include "bytestream.h"
27 #include "codec_internal.h"
28 #include "encode.h"
29 #include "sgi.h"
30 #include "rle.h"
31 
32 #define SGI_SINGLE_CHAN 2
33 #define SGI_MULTI_CHAN 3
34 
35 typedef struct SgiContext {
36  AVClass *class;
37 
38  int rle;
39 } SgiContext;
40 
42 {
43  if (avctx->width > 65535 || avctx->height > 65535) {
44  av_log(avctx, AV_LOG_ERROR, "Unsupported resolution %dx%d. "
45  "SGI does not support resolutions above 65535x65535\n",
46  avctx->width, avctx->height);
47  return AVERROR_INVALIDDATA;
48  }
49 
50  return 0;
51 }
52 
53 static int sgi_rle_encode(PutByteContext *pbc, const uint8_t *src,
54  int w, int bpp)
55 {
56  int val, count, x, start = bytestream2_tell_p(pbc);
57  void (*bytestream2_put)(PutByteContext *, unsigned int);
58 
59  if (bpp == 1)
60  bytestream2_put = bytestream2_put_byte;
61  else
62  bytestream2_put = bytestream2_put_be16;
63 
64  for (x = 0; x < w; x += count) {
65  /* see if we can encode the next set of pixels with RLE */
66  count = ff_rle_count_pixels(src, w - x, bpp, 1);
67  if (count > 1) {
68  if (bytestream2_get_bytes_left_p(pbc) < bpp * 2)
69  return AVERROR_INVALIDDATA;
70 
71  val = bpp == 1 ? *src : AV_RB16(src);
72  bytestream2_put(pbc, count);
73  bytestream2_put(pbc, val);
74  } else {
75  int i;
76  /* fall back on uncompressed */
77  count = ff_rle_count_pixels(src, w - x, bpp, 0);
78  if (bytestream2_get_bytes_left_p(pbc) < bpp * (count + 1))
79  return AVERROR_INVALIDDATA;
80 
81  bytestream2_put(pbc, count + 0x80);
82  for (i = 0; i < count; i++) {
83  val = bpp == 1 ? src[i] : AV_RB16(src + i * bpp);
84  bytestream2_put(pbc, val);
85  }
86  }
87 
88  src += count * bpp;
89  }
90 
91  return bytestream2_tell_p(pbc) - start;
92 }
93 
95  const AVFrame *frame, int *got_packet)
96 {
97  SgiContext *s = avctx->priv_data;
98  const AVFrame * const p = frame;
99  PutByteContext pbc;
100  uint8_t *encode_buf;
101  int x, y, z, length, tablesize, ret, i;
102  unsigned int width, height, depth, dimension;
103  unsigned int bytes_per_channel, pixmax, put_be;
104 
105  width = avctx->width;
106  height = avctx->height;
107  bytes_per_channel = 1;
108  pixmax = 0xFF;
109  put_be = HAVE_BIGENDIAN;
110 
111  switch (avctx->pix_fmt) {
112  case AV_PIX_FMT_GRAY8:
114  depth = SGI_GRAYSCALE;
115  break;
116  case AV_PIX_FMT_RGB24:
118  depth = SGI_RGB;
119  break;
120  case AV_PIX_FMT_RGBA:
122  depth = SGI_RGBA;
123  break;
124  case AV_PIX_FMT_GRAY16LE:
125  put_be = !HAVE_BIGENDIAN;
126  case AV_PIX_FMT_GRAY16BE:
127  bytes_per_channel = 2;
128  pixmax = 0xFFFF;
130  depth = SGI_GRAYSCALE;
131  break;
132  case AV_PIX_FMT_RGB48LE:
133  put_be = !HAVE_BIGENDIAN;
134  case AV_PIX_FMT_RGB48BE:
135  bytes_per_channel = 2;
136  pixmax = 0xFFFF;
138  depth = SGI_RGB;
139  break;
140  case AV_PIX_FMT_RGBA64LE:
141  put_be = !HAVE_BIGENDIAN;
142  case AV_PIX_FMT_RGBA64BE:
143  bytes_per_channel = 2;
144  pixmax = 0xFFFF;
146  depth = SGI_RGBA;
147  break;
148  default:
149  return AVERROR_INVALIDDATA;
150  }
151 
152  tablesize = depth * height * 4;
153  length = SGI_HEADER_SIZE;
154  if (!s->rle)
155  length += depth * height * width;
156  else // assume sgi_rle_encode() produces at most 2x size of input
157  length += tablesize * 2 + depth * height * (2 * width + 1);
158 
159  if ((ret = ff_alloc_packet(avctx, pkt, bytes_per_channel * length)) < 0)
160  return ret;
161 
163 
164  /* Encode header. */
165  bytestream2_put_be16(&pbc, SGI_MAGIC);
166  bytestream2_put_byte(&pbc, s->rle); /* RLE 1 - VERBATIM 0 */
167  bytestream2_put_byte(&pbc, bytes_per_channel);
168  bytestream2_put_be16(&pbc, dimension);
169  bytestream2_put_be16(&pbc, width);
170  bytestream2_put_be16(&pbc, height);
171  bytestream2_put_be16(&pbc, depth);
172 
173  bytestream2_put_be32(&pbc, 0L); /* pixmin */
174  bytestream2_put_be32(&pbc, pixmax);
175  bytestream2_put_be32(&pbc, 0L); /* dummy */
176 
177  /* name */
178  for (i = 0; i < 80; i++)
179  bytestream2_put_byte(&pbc, 0L);
180 
181  /* colormap */
182  bytestream2_put_be32(&pbc, 0L);
183 
184  /* The rest of the 512 byte header is unused. */
185  for (i = 0; i < 404; i++)
186  bytestream2_put_byte(&pbc, 0L);
187 
188  if (s->rle) {
189  PutByteContext taboff_pcb, tablen_pcb;
190 
191  /* Skip RLE offset table. */
192  bytestream2_init_writer(&taboff_pcb, pbc.buffer, tablesize);
193  bytestream2_skip_p(&pbc, tablesize);
194 
195  /* Skip RLE length table. */
196  bytestream2_init_writer(&tablen_pcb, pbc.buffer, tablesize);
197  bytestream2_skip_p(&pbc, tablesize);
198 
199  /* Make an intermediate consecutive buffer. */
200  if (!(encode_buf = av_malloc(width * bytes_per_channel)))
201  return AVERROR(ENOMEM);
202 
203  for (z = 0; z < depth; z++) {
204  const uint8_t *in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
205 
206  for (y = 0; y < height; y++) {
207  bytestream2_put_be32(&taboff_pcb, bytestream2_tell_p(&pbc));
208 
209  for (x = 0; x < width * bytes_per_channel; x += bytes_per_channel)
210  if (bytes_per_channel == 1) {
211  encode_buf[x] = in_buf[depth * x];
212  } else if (HAVE_BIGENDIAN ^ put_be) {
213  encode_buf[x + 1] = in_buf[depth * x];
214  encode_buf[x] = in_buf[depth * x + 1];
215  } else {
216  encode_buf[x] = in_buf[depth * x];
217  encode_buf[x + 1] = in_buf[depth * x + 1];
218  }
219 
220  length = sgi_rle_encode(&pbc, encode_buf, width,
221  bytes_per_channel);
222  if (length < 1) {
223  av_free(encode_buf);
224  return AVERROR_INVALIDDATA;
225  }
226 
227  bytestream2_put_be32(&tablen_pcb, length);
228  in_buf -= p->linesize[0];
229  }
230  }
231 
232  av_free(encode_buf);
233  } else {
234  for (z = 0; z < depth; z++) {
235  const uint8_t *in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
236 
237  for (y = 0; y < height; y++) {
238  for (x = 0; x < width * depth; x += depth)
239  if (bytes_per_channel == 1)
240  bytestream2_put_byte(&pbc, in_buf[x]);
241  else
242  if (put_be)
243  bytestream2_put_be16(&pbc, ((uint16_t *)in_buf)[x]);
244  else
245  bytestream2_put_le16(&pbc, ((uint16_t *)in_buf)[x]);
246 
247  in_buf -= p->linesize[0];
248  }
249  }
250  }
251 
252  /* total length */
253  pkt->size = bytestream2_tell_p(&pbc);
254  *got_packet = 1;
255 
256  return 0;
257 }
258 
259 #define OFFSET(x) offsetof(SgiContext, x)
260 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
261 static const AVOption options[] = {
262  { "rle", "Use run-length compression", OFFSET(rle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
263 
264  { NULL },
265 };
266 
267 static const AVClass sgi_class = {
268  .class_name = "sgi",
269  .item_name = av_default_item_name,
270  .option = options,
271  .version = LIBAVUTIL_VERSION_INT,
272 };
273 
275  .p.name = "sgi",
276  CODEC_LONG_NAME("SGI image"),
277  .p.type = AVMEDIA_TYPE_VIDEO,
278  .p.id = AV_CODEC_ID_SGI,
280  .priv_data_size = sizeof(SgiContext),
281  .p.priv_class = &sgi_class,
282  .init = encode_init,
284  .p.pix_fmts = (const enum AVPixelFormat[]) {
290  },
291 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
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:389
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
w
uint8_t w
Definition: llviddspenc.c:38
AVPacket::data
uint8_t * data
Definition: packet.h:539
AVOption
AVOption.
Definition: opt.h:429
encode.h
bytestream2_tell_p
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
Definition: bytestream.h:197
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:217
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
OFFSET
#define OFFSET(x)
Definition: sgienc.c:259
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:320
encode_init
static av_cold int encode_init(AVCodecContext *avctx)
Definition: sgienc.c:41
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
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
s
#define s(width, name)
Definition: cbs_vp9.c:198
options
static const AVOption options[]
Definition: sgienc.c:261
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:159
sgi_class
static const AVClass sgi_class
Definition: sgienc.c:267
SGI_MAGIC
#define SGI_MAGIC
SGI image file signature.
Definition: sgi.h:28
SGI_SINGLE_CHAN
#define SGI_SINGLE_CHAN
Definition: sgienc.c:32
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
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:75
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:237
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
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
SgiContext
Definition: sgienc.c:35
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:540
height
#define height
Definition: dsp.h:85
codec_internal.h
PutByteContext::buffer
uint8_t * buffer
Definition: bytestream.h:38
SgiContext::rle
int rle
Definition: sgienc.c:38
encode_frame
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: sgienc.c:94
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
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
SGI_MULTI_CHAN
#define SGI_MULTI_CHAN
Definition: sgienc.c:33
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:194
AVCodecContext::height
int height
Definition: avcodec.h:624
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:663
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:80
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
AVCodecContext
main external API structure.
Definition: avcodec.h:451
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
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
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:105
mem.h
SGI_HEADER_SIZE
#define SGI_HEADER_SIZE
Definition: sgi.h:30
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:624
bytestream.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:434
sgi_rle_encode
static int sgi_rle_encode(PutByteContext *pbc, const uint8_t *src, int w, int bpp)
Definition: sgienc.c:53
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:274
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:85
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:62
src
#define src
Definition: vp8dsp.c:248
VE
#define VE
Definition: sgienc.c:260
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