FFmpeg  2.1.1
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 
32 {
33  if (avctx->width > 65535 || avctx->height > 65535) {
34  av_log(avctx, AV_LOG_ERROR, "SGI does not support resolutions above 65535x65535\n");
35  return -1;
36  }
37 
38  return 0;
39 }
40 
42  const AVFrame *frame, int *got_packet)
43 {
44  AVFrame * const p = (AVFrame *)frame;
45  uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf, *buf;
46  int x, y, z, length, tablesize, ret;
47  unsigned int width, height, depth, dimension, bytes_per_channel, pixmax, put_be;
48  unsigned char *end_buf;
49 
51  p->key_frame = 1;
52 
53  width = avctx->width;
54  height = avctx->height;
55  bytes_per_channel = 1;
56  pixmax = 0xFF;
57  put_be = HAVE_BIGENDIAN;
58 
59  switch (avctx->pix_fmt) {
60  case AV_PIX_FMT_GRAY8:
61  dimension = SGI_SINGLE_CHAN;
62  depth = SGI_GRAYSCALE;
63  break;
64  case AV_PIX_FMT_RGB24:
65  dimension = SGI_MULTI_CHAN;
66  depth = SGI_RGB;
67  break;
68  case AV_PIX_FMT_RGBA:
69  dimension = SGI_MULTI_CHAN;
70  depth = SGI_RGBA;
71  break;
73  put_be = !HAVE_BIGENDIAN;
76  bytes_per_channel = 2;
77  pixmax = 0xFFFF;
78  dimension = SGI_SINGLE_CHAN;
79  depth = SGI_GRAYSCALE;
80  break;
81  case AV_PIX_FMT_RGB48LE:
82  put_be = !HAVE_BIGENDIAN;
83  case AV_PIX_FMT_RGB48BE:
85  bytes_per_channel = 2;
86  pixmax = 0xFFFF;
87  dimension = SGI_MULTI_CHAN;
88  depth = SGI_RGB;
89  break;
91  put_be = !HAVE_BIGENDIAN;
94  bytes_per_channel = 2;
95  pixmax = 0xFFFF;
96  dimension = SGI_MULTI_CHAN;
97  depth = SGI_RGBA;
98  break;
99  default:
100  return AVERROR_INVALIDDATA;
101  }
102 
103  tablesize = depth * height * 4;
104  length = SGI_HEADER_SIZE;
105  if (avctx->coder_type == FF_CODER_TYPE_RAW)
106  length += depth * height * width;
107  else // assume ff_rl_encode() produces at most 2x size of input
108  length += tablesize * 2 + depth * height * (2 * width + 1);
109 
110  if ((ret = ff_alloc_packet2(avctx, pkt, bytes_per_channel * length)) < 0)
111  return ret;
112  buf = pkt->data;
113  end_buf = pkt->data + pkt->size;
114 
115  /* Encode header. */
116  bytestream_put_be16(&buf, SGI_MAGIC);
117  bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/
118  bytestream_put_byte(&buf, bytes_per_channel);
119  bytestream_put_be16(&buf, dimension);
120  bytestream_put_be16(&buf, width);
121  bytestream_put_be16(&buf, height);
122  bytestream_put_be16(&buf, depth);
123 
124  bytestream_put_be32(&buf, 0L); /* pixmin */
125  bytestream_put_be32(&buf, pixmax);
126  bytestream_put_be32(&buf, 0L); /* dummy */
127 
128  /* name */
129  memset(buf, 0, SGI_HEADER_SIZE);
130  buf += 80;
131 
132  /* colormap */
133  bytestream_put_be32(&buf, 0L);
134 
135  /* The rest of the 512 byte header is unused. */
136  buf += 404;
137  offsettab = buf;
138 
139  if (avctx->coder_type != FF_CODER_TYPE_RAW) {
140  /* Skip RLE offset table. */
141  buf += tablesize;
142  lengthtab = buf;
143 
144  /* Skip RLE length table. */
145  buf += tablesize;
146 
147  /* Make an intermediate consecutive buffer. */
148  if (!(encode_buf = av_malloc(width)))
149  return -1;
150 
151  for (z = 0; z < depth; z++) {
152  in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
153 
154  for (y = 0; y < height; y++) {
155  bytestream_put_be32(&offsettab, buf - pkt->data);
156 
157  for (x = 0; x < width; x++)
158  encode_buf[x] = in_buf[depth * x];
159 
160  if ((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) {
161  av_free(encode_buf);
162  return -1;
163  }
164 
165  buf += length;
166  bytestream_put_byte(&buf, 0);
167  bytestream_put_be32(&lengthtab, length + 1);
168  in_buf -= p->linesize[0];
169  }
170  }
171 
172  av_free(encode_buf);
173  } else {
174  for (z = 0; z < depth; z++) {
175  in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
176 
177  for (y = 0; y < height; y++) {
178  for (x = 0; x < width * depth; x += depth)
179  if (bytes_per_channel == 1) {
180  bytestream_put_byte(&buf, in_buf[x]);
181  } else {
182  if (put_be) {
183  bytestream_put_be16(&buf, ((uint16_t *)in_buf)[x]);
184  } else {
185  bytestream_put_le16(&buf, ((uint16_t *)in_buf)[x]);
186  }
187  }
188 
189  in_buf -= p->linesize[0];
190  }
191  }
192  }
193 
194  /* total length */
195  pkt->size = buf - pkt->data;
196  pkt->flags |= AV_PKT_FLAG_KEY;
197  *got_packet = 1;
198 
199  return 0;
200 }
201 
203  .name = "sgi",
204  .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
205  .type = AVMEDIA_TYPE_VIDEO,
206  .id = AV_CODEC_ID_SGI,
207  .init = encode_init,
208  .encode2 = encode_frame,
209  .pix_fmts = (const enum AVPixelFormat[]){
215  },
216 };
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1500
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: avcodec.h:4536
int size
Definition: avcodec.h:1064
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
Pixel format.
Definition: avcodec.h:4533
AVCodec.
Definition: avcodec.h:2922
#define av_cold
Definition: avcodec.h:653
Y , 8bpp.
Definition: avcodec.h:4542
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: avcodec.h:4685
#define SGI_MULTI_CHAN
Definition: sgienc.c:29
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
Definition: avcodec.h:4579
uint8_t
const char * name
Name of the codec implementation.
Definition: avcodec.h:2929
int coder_type
coder type
Definition: avcodec.h:2262
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1113
static AVFrame * frame
Definition: demuxing.c:51
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:219
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
Libavcodec external API header.
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
int depth
Definition: v4l.c:61
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1069
#define SGI_RGBA
Definition: sgi.h:34
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:167
float y
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
AVCodec ff_sgi_encoder
Definition: sgienc.c:202
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
static av_cold int encode_init(AVCodecContext *avctx)
Definition: sgienc.c:31
#define L(x)
Definition: vp56_arith.h:36
int AC3_NAME() encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
static int width
Definition: utils.c:158
main external API structure.
Definition: avcodec.h:1146
#define SGI_HEADER_SIZE
Definition: sgi.h:30
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: avcodec.h:4684
void * buf
Definition: avisynth_c.h:594
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
uint8_t * data
Definition: avcodec.h:1063
#define FF_CODER_TYPE_RAW
Definition: avcodec.h:2254
#define SGI_GRAYSCALE
Definition: sgi.h:32
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
#define SGI_RGB
Definition: sgi.h:33
#define SGI_SINGLE_CHAN
Definition: sgienc.c:28
Y , 16bpp, little-endian.
Definition: avcodec.h:4568
#define SGI_MAGIC
SGI image file signature.
Definition: sgi.h:28
common internal api header.
#define AVERROR_INVALIDDATA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: avcodec.h:4563
int key_frame
1 -&gt; keyframe, 0-&gt; not
Definition: frame.h:162
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
static AVPacket pkt
Definition: demuxing.c:52
const char int length
Definition: avisynth_c.h:668
#define HAVE_BIGENDIAN
Definition: config.h:122
This structure stores compressed data.
Definition: avcodec.h:1040
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
Y , 16bpp, big-endian.
Definition: avcodec.h:4567
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as lit...
Definition: avcodec.h:4580