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 "avcodec.h"
23 #include "bytestream.h"
24 #include "internal.h"
25 #include "sgi.h"
26 #include "rle.h"
27 
28 #define SGI_SINGLE_CHAN 2
29 #define SGI_MULTI_CHAN 3
30 
31 typedef struct SgiContext {
33 } SgiContext;
34 
36 {
37  SgiContext *s = avctx->priv_data;
38 
39  if (avctx->width > 65535 || avctx->height > 65535) {
40  av_log(avctx, AV_LOG_ERROR, "SGI does not support resolutions above 65535x65535\n");
41  return -1;
42  }
43 
45  avctx->coded_frame = &s->picture;
46 
47  return 0;
48 }
49 
51  const AVFrame *frame, int *got_packet)
52 {
53  SgiContext *s = avctx->priv_data;
54  AVFrame * const p = &s->picture;
55  uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf, *buf;
56  int x, y, z, length, tablesize, ret;
57  unsigned int width, height, depth, dimension, bytes_per_channel, pixmax, put_be;
58  unsigned char *end_buf;
59 
60  *p = *frame;
62  p->key_frame = 1;
63 
64  width = avctx->width;
65  height = avctx->height;
66  bytes_per_channel = 1;
67  pixmax = 0xFF;
68  put_be = HAVE_BIGENDIAN;
69 
70  switch (avctx->pix_fmt) {
71  case AV_PIX_FMT_GRAY8:
72  dimension = SGI_SINGLE_CHAN;
73  depth = SGI_GRAYSCALE;
74  break;
75  case AV_PIX_FMT_RGB24:
76  dimension = SGI_MULTI_CHAN;
77  depth = SGI_RGB;
78  break;
79  case AV_PIX_FMT_RGBA:
80  dimension = SGI_MULTI_CHAN;
81  depth = SGI_RGBA;
82  break;
84  put_be = !HAVE_BIGENDIAN;
87  bytes_per_channel = 2;
88  pixmax = 0xFFFF;
89  dimension = SGI_SINGLE_CHAN;
90  depth = SGI_GRAYSCALE;
91  break;
92  case AV_PIX_FMT_RGB48LE:
93  put_be = !HAVE_BIGENDIAN;
94  case AV_PIX_FMT_RGB48BE:
96  bytes_per_channel = 2;
97  pixmax = 0xFFFF;
98  dimension = SGI_MULTI_CHAN;
99  depth = SGI_RGB;
100  break;
101  case AV_PIX_FMT_RGBA64LE:
102  put_be = !HAVE_BIGENDIAN;
103  case AV_PIX_FMT_RGBA64BE:
104  avctx->coder_type = FF_CODER_TYPE_RAW;
105  bytes_per_channel = 2;
106  pixmax = 0xFFFF;
107  dimension = SGI_MULTI_CHAN;
108  depth = SGI_RGBA;
109  break;
110  default:
111  return AVERROR_INVALIDDATA;
112  }
113 
114  tablesize = depth * height * 4;
115  length = SGI_HEADER_SIZE;
116  if (avctx->coder_type == FF_CODER_TYPE_RAW)
117  length += depth * height * width;
118  else // assume ff_rl_encode() produces at most 2x size of input
119  length += tablesize * 2 + depth * height * (2 * width + 1);
120 
121  if ((ret = ff_alloc_packet2(avctx, pkt, bytes_per_channel * length)) < 0)
122  return ret;
123  buf = pkt->data;
124  end_buf = pkt->data + pkt->size;
125 
126  /* Encode header. */
127  bytestream_put_be16(&buf, SGI_MAGIC);
128  bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/
129  bytestream_put_byte(&buf, bytes_per_channel);
130  bytestream_put_be16(&buf, dimension);
131  bytestream_put_be16(&buf, width);
132  bytestream_put_be16(&buf, height);
133  bytestream_put_be16(&buf, depth);
134 
135  bytestream_put_be32(&buf, 0L); /* pixmin */
136  bytestream_put_be32(&buf, pixmax);
137  bytestream_put_be32(&buf, 0L); /* dummy */
138 
139  /* name */
140  memset(buf, 0, SGI_HEADER_SIZE);
141  buf += 80;
142 
143  /* colormap */
144  bytestream_put_be32(&buf, 0L);
145 
146  /* The rest of the 512 byte header is unused. */
147  buf += 404;
148  offsettab = buf;
149 
150  if (avctx->coder_type != FF_CODER_TYPE_RAW) {
151  /* Skip RLE offset table. */
152  buf += tablesize;
153  lengthtab = buf;
154 
155  /* Skip RLE length table. */
156  buf += tablesize;
157 
158  /* Make an intermediate consecutive buffer. */
159  if (!(encode_buf = av_malloc(width)))
160  return -1;
161 
162  for (z = 0; z < depth; z++) {
163  in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
164 
165  for (y = 0; y < height; y++) {
166  bytestream_put_be32(&offsettab, buf - pkt->data);
167 
168  for (x = 0; x < width; x++)
169  encode_buf[x] = in_buf[depth * x];
170 
171  if ((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) {
172  av_free(encode_buf);
173  return -1;
174  }
175 
176  buf += length;
177  bytestream_put_byte(&buf, 0);
178  bytestream_put_be32(&lengthtab, length + 1);
179  in_buf -= p->linesize[0];
180  }
181  }
182 
183  av_free(encode_buf);
184  } else {
185  for (z = 0; z < depth; z++) {
186  in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
187 
188  for (y = 0; y < height; y++) {
189  for (x = 0; x < width * depth; x += depth)
190  if (bytes_per_channel == 1) {
191  bytestream_put_byte(&buf, in_buf[x]);
192  } else {
193  if (put_be) {
194  bytestream_put_be16(&buf, ((uint16_t *)in_buf)[x]);
195  } else {
196  bytestream_put_le16(&buf, ((uint16_t *)in_buf)[x]);
197  }
198  }
199 
200  in_buf -= p->linesize[0];
201  }
202  }
203  }
204 
205  /* total length */
206  pkt->size = buf - pkt->data;
207  pkt->flags |= AV_PKT_FLAG_KEY;
208  *got_packet = 1;
209 
210  return 0;
211 }
212 
214  .name = "sgi",
215  .type = AVMEDIA_TYPE_VIDEO,
216  .id = AV_CODEC_ID_SGI,
217  .priv_data_size = sizeof(SgiContext),
218  .init = encode_init,
219  .encode2 = encode_frame,
220  .pix_fmts = (const enum AVPixelFormat[]){
226  },
227  .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
228 };
const char * s
Definition: avisynth_c.h:668
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:70
AVFrame * coded_frame
the picture in the bitstream
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define FF_CODER_TYPE_RAW
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
#define SGI_MULTI_CHAN
Definition: sgienc.c:29
uint8_t
#define av_cold
Definition: attributes.h:78
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:112
static AVPacket pkt
Definition: demuxing.c:56
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:209
int coder_type
coder type
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: sgienc.c:50
uint8_t * data
struct SgiContext SgiContext
The official guide to swscale for confused that consecutive non overlapping rectangles of dimension(0, slice_top)-(picture_width
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
frame
Definition: stft.m:14
Discrete Time axis x
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
external API header
int depth
Definition: v4l.c:62
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:97
int flags
A combination of AV_PKT_FLAG values.
#define SGI_RGBA
Definition: sgi.h:34
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:144
ret
Definition: avfilter.c:821
int width
picture width / height.
AVCodec ff_sgi_encoder
Definition: sgienc.c:213
static av_cold int encode_init(AVCodecContext *avctx)
Definition: sgienc.c:35
#define L(x)
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
static int width
Definition: tests/utils.c:158
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
main external API structure.
#define SGI_HEADER_SIZE
Definition: sgi.h:30
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Y , 16bpp, big-endian.
Definition: pixfmt.h:101
#define SGI_GRAYSCALE
Definition: sgi.h:32
#define SGI_RGB
Definition: sgi.h:33
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
#define SGI_SINGLE_CHAN
Definition: sgienc.c:28
#define SGI_MAGIC
SGI image file signature.
Definition: sgi.h:28
Y , 8bpp.
Definition: pixfmt.h:76
common internal api header.
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:111
function y
Definition: D.m:1
Y , 16bpp, little-endian.
Definition: pixfmt.h:102
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:139
AVFrame picture
Definition: sgienc.c:32
int ff_rle_encode(uint8_t *outbuf, int out_size, const uint8_t *ptr, int bpp, int w, int add_rep, int xor_rep, int add_raw, int xor_raw)
RLE compress the row, with maximum size of out_size.
Definition: rle.c:58
const char int length
Definition: avisynth_c.h:668
#define HAVE_BIGENDIAN
Definition: config.h:116
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
This structure stores compressed data.
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:210