FFmpeg  4.4
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 #if FF_API_CODED_FRAME
106  avctx->coded_frame->key_frame = 1;
108 #endif
109 
110 #if FF_API_CODER_TYPE
112  if (avctx->coder_type == FF_CODER_TYPE_RAW)
113  s->rle = 0;
115 #endif
116 
117  width = avctx->width;
118  height = avctx->height;
119  bytes_per_channel = 1;
120  pixmax = 0xFF;
121  put_be = HAVE_BIGENDIAN;
122 
123  switch (avctx->pix_fmt) {
124  case AV_PIX_FMT_GRAY8:
125  dimension = SGI_SINGLE_CHAN;
126  depth = SGI_GRAYSCALE;
127  break;
128  case AV_PIX_FMT_RGB24:
129  dimension = SGI_MULTI_CHAN;
130  depth = SGI_RGB;
131  break;
132  case AV_PIX_FMT_RGBA:
133  dimension = SGI_MULTI_CHAN;
134  depth = SGI_RGBA;
135  break;
136  case AV_PIX_FMT_GRAY16LE:
137  put_be = !HAVE_BIGENDIAN;
138  case AV_PIX_FMT_GRAY16BE:
139  bytes_per_channel = 2;
140  pixmax = 0xFFFF;
141  dimension = SGI_SINGLE_CHAN;
142  depth = SGI_GRAYSCALE;
143  break;
144  case AV_PIX_FMT_RGB48LE:
145  put_be = !HAVE_BIGENDIAN;
146  case AV_PIX_FMT_RGB48BE:
147  bytes_per_channel = 2;
148  pixmax = 0xFFFF;
149  dimension = SGI_MULTI_CHAN;
150  depth = SGI_RGB;
151  break;
152  case AV_PIX_FMT_RGBA64LE:
153  put_be = !HAVE_BIGENDIAN;
154  case AV_PIX_FMT_RGBA64BE:
155  bytes_per_channel = 2;
156  pixmax = 0xFFFF;
157  dimension = SGI_MULTI_CHAN;
158  depth = SGI_RGBA;
159  break;
160  default:
161  return AVERROR_INVALIDDATA;
162  }
163 
164  tablesize = depth * height * 4;
165  length = SGI_HEADER_SIZE;
166  if (!s->rle)
167  length += depth * height * width;
168  else // assume sgi_rle_encode() produces at most 2x size of input
169  length += tablesize * 2 + depth * height * (2 * width + 1);
170 
171  if ((ret = ff_alloc_packet2(avctx, pkt, bytes_per_channel * length, 0)) < 0)
172  return ret;
173 
175 
176  /* Encode header. */
177  bytestream2_put_be16(&pbc, SGI_MAGIC);
178  bytestream2_put_byte(&pbc, s->rle); /* RLE 1 - VERBATIM 0 */
179  bytestream2_put_byte(&pbc, bytes_per_channel);
180  bytestream2_put_be16(&pbc, dimension);
181  bytestream2_put_be16(&pbc, width);
182  bytestream2_put_be16(&pbc, height);
183  bytestream2_put_be16(&pbc, depth);
184 
185  bytestream2_put_be32(&pbc, 0L); /* pixmin */
186  bytestream2_put_be32(&pbc, pixmax);
187  bytestream2_put_be32(&pbc, 0L); /* dummy */
188 
189  /* name */
190  for (i = 0; i < 80; i++)
191  bytestream2_put_byte(&pbc, 0L);
192 
193  /* colormap */
194  bytestream2_put_be32(&pbc, 0L);
195 
196  /* The rest of the 512 byte header is unused. */
197  for (i = 0; i < 404; i++)
198  bytestream2_put_byte(&pbc, 0L);
199 
200  if (s->rle) {
201  PutByteContext taboff_pcb, tablen_pcb;
202 
203  /* Skip RLE offset table. */
204  bytestream2_init_writer(&taboff_pcb, pbc.buffer, tablesize);
205  bytestream2_skip_p(&pbc, tablesize);
206 
207  /* Skip RLE length table. */
208  bytestream2_init_writer(&tablen_pcb, pbc.buffer, tablesize);
209  bytestream2_skip_p(&pbc, tablesize);
210 
211  /* Make an intermediate consecutive buffer. */
212  if (!(encode_buf = av_malloc(width * bytes_per_channel)))
213  return AVERROR(ENOMEM);
214 
215  for (z = 0; z < depth; z++) {
216  in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
217 
218  for (y = 0; y < height; y++) {
219  bytestream2_put_be32(&taboff_pcb, bytestream2_tell_p(&pbc));
220 
221  for (x = 0; x < width * bytes_per_channel; x += bytes_per_channel)
222  if (bytes_per_channel == 1) {
223  encode_buf[x] = in_buf[depth * x];
224  } else if (HAVE_BIGENDIAN ^ put_be) {
225  encode_buf[x + 1] = in_buf[depth * x];
226  encode_buf[x] = in_buf[depth * x + 1];
227  } else {
228  encode_buf[x] = in_buf[depth * x];
229  encode_buf[x + 1] = in_buf[depth * x + 1];
230  }
231 
232  length = sgi_rle_encode(&pbc, encode_buf, width,
233  bytes_per_channel);
234  if (length < 1) {
235  av_free(encode_buf);
236  return AVERROR_INVALIDDATA;
237  }
238 
239  bytestream2_put_be32(&tablen_pcb, length);
240  in_buf -= p->linesize[0];
241  }
242  }
243 
244  av_free(encode_buf);
245  } else {
246  for (z = 0; z < depth; z++) {
247  in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
248 
249  for (y = 0; y < height; y++) {
250  for (x = 0; x < width * depth; x += depth)
251  if (bytes_per_channel == 1)
252  bytestream2_put_byte(&pbc, in_buf[x]);
253  else
254  if (put_be)
255  bytestream2_put_be16(&pbc, ((uint16_t *)in_buf)[x]);
256  else
257  bytestream2_put_le16(&pbc, ((uint16_t *)in_buf)[x]);
258 
259  in_buf -= p->linesize[0];
260  }
261  }
262  }
263 
264  /* total length */
265  pkt->size = bytestream2_tell_p(&pbc);
267  *got_packet = 1;
268 
269  return 0;
270 }
271 
272 #define OFFSET(x) offsetof(SgiContext, x)
273 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
274 static const AVOption options[] = {
275  { "rle", "Use run-length compression", OFFSET(rle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
276 
277  { NULL },
278 };
279 
280 static const AVClass sgi_class = {
281  .class_name = "sgi",
282  .item_name = av_default_item_name,
283  .option = options,
284  .version = LIBAVUTIL_VERSION_INT,
285 };
286 
288  .name = "sgi",
289  .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
290  .type = AVMEDIA_TYPE_VIDEO,
291  .id = AV_CODEC_ID_SGI,
292  .priv_data_size = sizeof(SgiContext),
293  .priv_class = &sgi_class,
294  .init = encode_init,
295  .encode2 = encode_frame,
296  .pix_fmts = (const enum AVPixelFormat[]) {
302  },
303  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
304 };
static double val(void *priv, double ch)
Definition: aeval.c:76
#define L(x)
Definition: vp56_arith.h:36
#define av_cold
Definition: attributes.h:88
uint8_t
Libavcodec external API header.
#define FF_CODER_TYPE_RAW
Definition: avcodec.h:1449
#define AV_RB16
Definition: intreadwrite.h:53
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:147
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
Definition: bytestream.h:197
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(PutByteContext *p)
Definition: bytestream.h:163
#define s(width, name)
Definition: cbs_vp9.c:257
#define HAVE_BIGENDIAN
Definition: config.h:200
#define NULL
Definition: coverity.c:32
static AVFrame * frame
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
static int rle(uint8_t *dst, const uint8_t *src, int compressed_size, int uncompressed_size)
Definition: exr.c:215
int
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_CODEC_ID_SGI
Definition: codec_id.h:150
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int i
Definition: input.c:407
#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
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:303
uint8_t w
Definition: llviddspenc.c:39
AVOptions.
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ 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
@ 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:205
@ 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:206
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
@ 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_PIX_FMT_GRAY16LE
Y , 16bpp, little-endian.
Definition: pixfmt.h:98
typedef void(RENAME(mix_any_func_type))
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
#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:30
static const AVOption options[]
Definition: sgienc.c:274
#define VE
Definition: sgienc.c:273
static int sgi_rle_encode(PutByteContext *pbc, const uint8_t *src, int w, int bpp)
Definition: sgienc.c:51
static av_cold int encode_init(AVCodecContext *avctx)
Definition: sgienc.c:39
AVCodec ff_sgi_encoder
Definition: sgienc.c:287
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: sgienc.c:92
#define SGI_MULTI_CHAN
Definition: sgienc.c:31
#define OFFSET(x)
Definition: sgienc.c:272
static const AVClass sgi_class
Definition: sgienc.c:280
Describe the class of an AVClass context structure.
Definition: log.h:67
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
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int width
picture width / height.
Definition: avcodec.h:709
attribute_deprecated int coder_type
Definition: avcodec.h:1455
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:1764
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:396
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
AVOption.
Definition: opt.h:248
This structure stores compressed data.
Definition: packet.h:346
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
uint8_t * buffer
Definition: bytestream.h:38
int rle
Definition: sgienc.c:36
#define av_free(p)
#define av_malloc(s)
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
AVPacket * pkt
Definition: movenc.c:59
#define height
#define width