FFmpeg  4.3
sgidec.c
Go to the documentation of this file.
1 /*
2  * SGI image decoder
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/imgutils.h"
23 #include "libavutil/avassert.h"
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27 #include "sgi.h"
28 
29 typedef struct SgiState {
31  unsigned int width;
32  unsigned int height;
33  unsigned int depth;
34  unsigned int bytes_per_channel;
35  int linesize;
37 } SgiState;
38 
39 /**
40  * Expand an RLE row into a channel.
41  * @param s the current image state
42  * @param out_buf Points to one line after the output buffer.
43  * @param len length of out_buf in bytes
44  * @param pixelstride pixel stride of input buffer
45  * @return size of output in bytes, else return error code.
46  */
47 static int expand_rle_row8(SgiState *s, uint8_t *out_buf,
48  int len, int pixelstride)
49 {
50  unsigned char pixel, count;
51  unsigned char *orig = out_buf;
52  uint8_t *out_end = out_buf + len;
53 
54  while (out_buf < out_end) {
55  if (bytestream2_get_bytes_left(&s->g) < 1)
56  return AVERROR_INVALIDDATA;
57  pixel = bytestream2_get_byteu(&s->g);
58  if (!(count = (pixel & 0x7f))) {
59  break;
60  }
61 
62  /* Check for buffer overflow. */
63  if (out_end - out_buf <= pixelstride * (count - 1)) {
64  av_log(s->avctx, AV_LOG_ERROR, "Invalid pixel count.\n");
65  return AVERROR_INVALIDDATA;
66  }
67 
68  if (pixel & 0x80) {
69  while (count--) {
70  *out_buf = bytestream2_get_byte(&s->g);
71  out_buf += pixelstride;
72  }
73  } else {
74  pixel = bytestream2_get_byte(&s->g);
75 
76  while (count--) {
77  *out_buf = pixel;
78  out_buf += pixelstride;
79  }
80  }
81  }
82  return (out_buf - orig) / pixelstride;
83 }
84 
85 static int expand_rle_row16(SgiState *s, uint16_t *out_buf,
86  int len, int pixelstride)
87 {
88  unsigned short pixel;
89  unsigned char count;
90  unsigned short *orig = out_buf;
91  uint16_t *out_end = out_buf + len;
92 
93  while (out_buf < out_end) {
94  if (bytestream2_get_bytes_left(&s->g) < 2)
95  return AVERROR_INVALIDDATA;
96  pixel = bytestream2_get_be16u(&s->g);
97  if (!(count = (pixel & 0x7f)))
98  break;
99 
100  /* Check for buffer overflow. */
101  if (out_end - out_buf <= pixelstride * (count - 1)) {
102  av_log(s->avctx, AV_LOG_ERROR, "Invalid pixel count.\n");
103  return AVERROR_INVALIDDATA;
104  }
105 
106  if (pixel & 0x80) {
107  while (count--) {
109  AV_WN16A(out_buf, pixel);
110  out_buf += pixelstride;
111  }
112  } else {
114 
115  while (count--) {
116  AV_WN16A(out_buf, pixel);
117  out_buf += pixelstride;
118  }
119  }
120  }
121  return (out_buf - orig) / pixelstride;
122 }
123 
124 
125 /**
126  * Read a run length encoded SGI image.
127  * @param out_buf output buffer
128  * @param s the current image state
129  * @return 0 if no error, else return error code.
130  */
131 static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
132 {
133  uint8_t *dest_row;
134  unsigned int len = s->height * s->depth * 4;
135  GetByteContext g_table = s->g;
136  unsigned int y, z;
137  unsigned int start_offset;
138  int linesize, ret;
139 
140  /* size of RLE offset and length tables */
141  if (len * 2 > bytestream2_get_bytes_left(&s->g)) {
142  return AVERROR_INVALIDDATA;
143  }
144 
145  for (z = 0; z < s->depth; z++) {
146  dest_row = out_buf;
147  for (y = 0; y < s->height; y++) {
148  linesize = s->width * s->depth;
149  dest_row -= s->linesize;
150  start_offset = bytestream2_get_be32(&g_table);
151  bytestream2_seek(&s->g, start_offset, SEEK_SET);
152  if (s->bytes_per_channel == 1)
153  ret = expand_rle_row8(s, dest_row + z, linesize, s->depth);
154  else
155  ret = expand_rle_row16(s, (uint16_t *)dest_row + z, linesize, s->depth);
156  if (ret != s->width)
157  return AVERROR_INVALIDDATA;
158  }
159  }
160  return 0;
161 }
162 
163 /**
164  * Read an uncompressed SGI image.
165  * @param out_buf output buffer
166  * @param s the current image state
167  * @return 0 if read success, else return error code.
168  */
169 static int read_uncompressed_sgi(unsigned char *out_buf, SgiState *s)
170 {
171  int x, y, z;
172  unsigned int offset = s->height * s->width * s->bytes_per_channel;
173  GetByteContext gp[4];
174  uint8_t *out_end;
175 
176  /* Test buffer size. */
177  if (offset * s->depth > bytestream2_get_bytes_left(&s->g))
178  return AVERROR_INVALIDDATA;
179 
180  /* Create a reader for each plane */
181  for (z = 0; z < s->depth; z++) {
182  gp[z] = s->g;
183  bytestream2_skip(&gp[z], z * offset);
184  }
185 
186  for (y = s->height - 1; y >= 0; y--) {
187  out_end = out_buf + (y * s->linesize);
188  if (s->bytes_per_channel == 1) {
189  for (x = s->width; x > 0; x--)
190  for (z = 0; z < s->depth; z++)
191  *out_end++ = bytestream2_get_byteu(&gp[z]);
192  } else {
193  uint16_t *out16 = (uint16_t *)out_end;
194  for (x = s->width; x > 0; x--)
195  for (z = 0; z < s->depth; z++)
196  *out16++ = bytestream2_get_ne16u(&gp[z]);
197  }
198  }
199  return 0;
200 }
201 
202 static int decode_frame(AVCodecContext *avctx,
203  void *data, int *got_frame,
204  AVPacket *avpkt)
205 {
206  SgiState *s = avctx->priv_data;
207  AVFrame *p = data;
208  unsigned int dimension, rle;
209  int ret = 0;
210  uint8_t *out_buf, *out_end;
211 
212  bytestream2_init(&s->g, avpkt->data, avpkt->size);
214  av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
215  return AVERROR_INVALIDDATA;
216  }
217 
218  /* Test for SGI magic. */
219  if (bytestream2_get_be16u(&s->g) != SGI_MAGIC) {
220  av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
221  return AVERROR_INVALIDDATA;
222  }
223 
224  rle = bytestream2_get_byteu(&s->g);
225  s->bytes_per_channel = bytestream2_get_byteu(&s->g);
226  dimension = bytestream2_get_be16u(&s->g);
227  s->width = bytestream2_get_be16u(&s->g);
228  s->height = bytestream2_get_be16u(&s->g);
229  s->depth = bytestream2_get_be16u(&s->g);
230 
231  if (s->bytes_per_channel != 1 && s->bytes_per_channel != 2) {
232  av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
233  return AVERROR_INVALIDDATA;
234  }
235 
236  /* Check for supported image dimensions. */
237  if (dimension != 2 && dimension != 3) {
238  av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
239  return AVERROR_INVALIDDATA;
240  }
241 
242  if (s->depth == SGI_GRAYSCALE) {
243  avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_GRAY16BE : AV_PIX_FMT_GRAY8;
244  } else if (s->depth == SGI_RGB) {
245  avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_RGB48BE : AV_PIX_FMT_RGB24;
246  } else if (s->depth == SGI_RGBA) {
247  avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_RGBA64BE : AV_PIX_FMT_RGBA;
248  } else {
249  av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
250  return AVERROR_INVALIDDATA;
251  }
252 
253  ret = ff_set_dimensions(avctx, s->width, s->height);
254  if (ret < 0)
255  return ret;
256 
257  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
258  return ret;
259 
261  p->key_frame = 1;
262  out_buf = p->data[0];
263 
264  out_end = out_buf + p->linesize[0] * s->height;
265 
266  s->linesize = p->linesize[0];
267 
268  /* Skip header. */
269  bytestream2_seek(&s->g, SGI_HEADER_SIZE, SEEK_SET);
270  if (rle) {
271  ret = read_rle_sgi(out_end, s);
272  } else {
273  ret = read_uncompressed_sgi(out_buf, s);
274  }
275  if (ret)
276  return ret;
277 
278  *got_frame = 1;
279  return avpkt->size;
280 }
281 
283 {
284  SgiState *s = avctx->priv_data;
285 
286  s->avctx = avctx;
287 
288  return 0;
289 }
290 
292  .name = "sgi",
293  .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
294  .type = AVMEDIA_TYPE_VIDEO,
295  .id = AV_CODEC_ID_SGI,
296  .priv_data_size = sizeof(SgiState),
297  .decode = decode_frame,
299  .capabilities = AV_CODEC_CAP_DR1,
300 };
AVCodec
AVCodec.
Definition: codec.h:190
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
SgiState::bytes_per_channel
unsigned int bytes_per_channel
Definition: sgidec.c:34
GetByteContext
Definition: bytestream.h:33
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
bytestream2_seek
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:208
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:205
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:355
data
const char data[16]
Definition: mxf.c:91
decode_frame
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: sgidec.c:202
expand_rle_row16
static int expand_rle_row16(SgiState *s, uint16_t *out_buf, int len, int pixelstride)
Definition: sgidec.c:85
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:314
AV_PIX_FMT_GRAY16BE
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition: pixfmt.h:97
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
x
FFmpeg Automated Testing Environment ************************************Introduction Using FATE from your FFmpeg source directory Submitting the results to the FFmpeg result aggregation server Uploading new samples to the fate suite FATE makefile targets and variables Makefile targets Makefile variables Examples Introduction **************FATE is an extended regression suite on the client side and a means for results aggregation and presentation on the server side The first part of this document explains how you can use FATE from your FFmpeg source directory to test your ffmpeg binary The second part describes how you can run FATE to submit the results to FFmpeg’s FATE server In any way you can have a look at the publicly viewable FATE results by visiting this as it can be seen if some test on some platform broke with their recent contribution This usually happens on the platforms the developers could not test on The second part of this document describes how you can run FATE to submit your results to FFmpeg’s FATE server If you want to submit your results be sure to check that your combination of OS and compiler is not already listed on the above mentioned website In the third part you can find a comprehensive listing of FATE makefile targets and variables Using FATE from your FFmpeg source directory **********************************************If you want to run FATE on your machine you need to have the samples in place You can get the samples via the build target fate rsync Use this command from the top level source this will cause FATE to fail NOTE To use a custom wrapper to run the pass ‘ target exec’ to ‘configure’ or set the TARGET_EXEC Make variable Submitting the results to the FFmpeg result aggregation server ****************************************************************To submit your results to the server you should run fate through the shell script ‘tests fate sh’ from the FFmpeg sources This script needs to be invoked with a configuration file as its first argument tests fate sh path to fate_config A configuration file template with comments describing the individual configuration variables can be found at ‘doc fate_config sh template’ Create a configuration that suits your based on the configuration template The ‘slot’ configuration variable can be any string that is not yet but it is suggested that you name it adhering to the following pattern ‘ARCH OS COMPILER COMPILER VERSION’ The configuration file itself will be sourced in a shell therefore all shell features may be used This enables you to setup the environment as you need it for your build For your first test runs the ‘fate_recv’ variable should be empty or commented out This will run everything as normal except that it will omit the submission of the results to the server The following files should be present in $workdir as specified in the configuration it may help to try out the ‘ssh’ command with one or more ‘ v’ options You should get detailed output concerning your SSH configuration and the authentication process The only thing left is to automate the execution of the fate sh script and the synchronisation of the samples directory Uploading new samples to the fate suite *****************************************If you need a sample uploaded send a mail to samples request This is for developers who have an account on the fate suite server If you upload new please make sure they are as small as space on each network bandwidth and so on benefit from smaller test cases Also keep in mind older checkouts use existing sample that means in practice generally do not remove or overwrite files as it likely would break older checkouts or releases Also all needed samples for a commit should be ideally before the push If you need an account for frequently uploading samples or you wish to help others by doing that send a mail to ffmpeg devel rsync vauL Duo x
Definition: fate.txt:150
AVFrame::key_frame
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:378
SgiState
Definition: sgidec.c:29
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
SgiState::g
GetByteContext g
Definition: sgidec.c:36
av_cold
#define av_cold
Definition: attributes.h:90
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
s
#define s(width, name)
Definition: cbs_vp9.c:257
SGI_MAGIC
#define SGI_MAGIC
SGI image file signature.
Definition: sgi.h:28
SgiState::linesize
int linesize
Definition: sgidec.c:35
AV_WN16A
#define AV_WN16A(p, v)
Definition: intreadwrite.h:534
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
pixel
uint8_t pixel
Definition: tiny_ssim.c:42
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
SgiState::height
unsigned int height
Definition: sgidec.c:32
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
ff_sgi_decoder
AVCodec ff_sgi_decoder
Definition: sgidec.c:291
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:383
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1854
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
AVPacket::size
int size
Definition: packet.h:356
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:186
gp
#define gp
Definition: regdef.h:62
bytestream2_get_ne16
#define bytestream2_get_ne16
Definition: bytestream.h:115
read_rle_sgi
static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
Read a run length encoded SGI image.
Definition: sgidec.c:131
read_uncompressed_sgi
static int read_uncompressed_sgi(unsigned char *out_buf, SgiState *s)
Read an uncompressed SGI image.
Definition: sgidec.c:169
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
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:102
SgiState::width
unsigned int width
Definition: sgidec.c:31
SgiState::depth
unsigned int depth
Definition: sgidec.c:33
uint8_t
uint8_t
Definition: audio_convert.c:194
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:197
len
int len
Definition: vorbis_enc_data.h:452
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:736
avcodec.h
ret
ret
Definition: filter_design.txt:187
expand_rle_row8
static int expand_rle_row8(SgiState *s, uint8_t *out_buf, int len, int pixelstride)
Expand an RLE row into a channel.
Definition: sgidec.c:47
AVCodecContext
main external API structure.
Definition: avcodec.h:526
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ff_set_dimensions
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:104
SGI_HEADER_SIZE
#define SGI_HEADER_SIZE
Definition: sgi.h:30
SgiState::avctx
AVCodecContext * avctx
Definition: sgidec.c:30
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:553
AVPacket
This structure stores compressed data.
Definition: packet.h:332
bytestream.h
imgutils.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:331
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
bytestream2_get_ne16u
#define bytestream2_get_ne16u
Definition: bytestream.h:119
sgi_decode_init
static av_cold int sgi_decode_init(AVCodecContext *avctx)
Definition: sgidec.c:282