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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 
40  avctx->coded_frame = &s->picture;
41 
42  return 0;
43 }
44 
45 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
46  const AVFrame *frame, int *got_packet)
47 {
48  SgiContext *s = avctx->priv_data;
49  AVFrame * const p = &s->picture;
50  uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf, *buf;
51  int x, y, z, length, tablesize, ret;
52  unsigned int width, height, depth, dimension;
53  unsigned char *end_buf;
54 
55  *p = *frame;
57  p->key_frame = 1;
58 
59  width = avctx->width;
60  height = avctx->height;
61 
62  switch (avctx->pix_fmt) {
63  case AV_PIX_FMT_GRAY8:
64  dimension = SGI_SINGLE_CHAN;
65  depth = SGI_GRAYSCALE;
66  break;
67  case AV_PIX_FMT_RGB24:
68  dimension = SGI_MULTI_CHAN;
69  depth = SGI_RGB;
70  break;
71  case AV_PIX_FMT_RGBA:
72  dimension = SGI_MULTI_CHAN;
73  depth = SGI_RGBA;
74  break;
75  default:
76  return AVERROR_INVALIDDATA;
77  }
78 
79  tablesize = depth * height * 4;
80  length = SGI_HEADER_SIZE;
81  if (avctx->coder_type == FF_CODER_TYPE_RAW)
82  length += depth * height * width;
83  else // assume ff_rl_encode() produces at most 2x size of input
84  length += tablesize * 2 + depth * height * (2 * width + 1);
85 
86  if ((ret = ff_alloc_packet(pkt, length)) < 0) {
87  av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", length);
88  return ret;
89  }
90  buf = pkt->data;
91  end_buf = pkt->data + pkt->size;
92 
93  /* Encode header. */
94  bytestream_put_be16(&buf, SGI_MAGIC);
95  bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/
96  bytestream_put_byte(&buf, 1); /* bytes_per_channel */
97  bytestream_put_be16(&buf, dimension);
98  bytestream_put_be16(&buf, width);
99  bytestream_put_be16(&buf, height);
100  bytestream_put_be16(&buf, depth);
101 
102  /* The rest are constant in this implementation. */
103  bytestream_put_be32(&buf, 0L); /* pixmin */
104  bytestream_put_be32(&buf, 255L); /* pixmax */
105  bytestream_put_be32(&buf, 0L); /* dummy */
106 
107  /* name */
108  memset(buf, 0, SGI_HEADER_SIZE);
109  buf += 80;
110 
111  /* colormap */
112  bytestream_put_be32(&buf, 0L);
113 
114  /* The rest of the 512 byte header is unused. */
115  buf += 404;
116  offsettab = buf;
117 
118  if (avctx->coder_type != FF_CODER_TYPE_RAW) {
119  /* Skip RLE offset table. */
120  buf += tablesize;
121  lengthtab = buf;
122 
123  /* Skip RLE length table. */
124  buf += tablesize;
125 
126  /* Make an intermediate consecutive buffer. */
127  if (!(encode_buf = av_malloc(width)))
128  return -1;
129 
130  for (z = 0; z < depth; z++) {
131  in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
132 
133  for (y = 0; y < height; y++) {
134  bytestream_put_be32(&offsettab, buf - pkt->data);
135 
136  for (x = 0; x < width; x++)
137  encode_buf[x] = in_buf[depth * x];
138 
139  if ((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) {
140  av_free(encode_buf);
141  return -1;
142  }
143 
144  buf += length;
145  bytestream_put_byte(&buf, 0);
146  bytestream_put_be32(&lengthtab, length + 1);
147  in_buf -= p->linesize[0];
148  }
149  }
150 
151  av_free(encode_buf);
152  } else {
153  for (z = 0; z < depth; z++) {
154  in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
155 
156  for (y = 0; y < height; y++) {
157  for (x = 0; x < width * depth; x += depth)
158  bytestream_put_byte(&buf, in_buf[x]);
159 
160  in_buf -= p->linesize[0];
161  }
162  }
163  }
164 
165  /* total length */
166  pkt->size = buf - pkt->data;
167  pkt->flags |= AV_PKT_FLAG_KEY;
168  *got_packet = 1;
169 
170  return 0;
171 }
172 
174  .name = "sgi",
175  .type = AVMEDIA_TYPE_VIDEO,
176  .id = AV_CODEC_ID_SGI,
177  .priv_data_size = sizeof(SgiContext),
178  .init = encode_init,
179  .encode2 = encode_frame,
180  .pix_fmts = (const enum AVPixelFormat[]){
182  },
183  .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
184 };
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:61
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
int size
Definition: avcodec.h:916
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
AVCodec.
Definition: avcodec.h:2960
#define SGI_MULTI_CHAN
Definition: sgienc.c:29
uint8_t
int coder_type
coder type
Definition: avcodec.h:2388
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: sgienc.c:45
uint8_t * data
Definition: avcodec.h:915
struct SgiContext SgiContext
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:921
#define SGI_RGBA
Definition: sgi.h:34
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
int width
picture width / height.
Definition: avcodec.h:1508
AVCodec ff_sgi_encoder
Definition: sgienc.c:173
static av_cold int encode_init(AVCodecContext *avctx)
Definition: sgienc.c:35
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:878
#define L(x)
Definition: vp56_arith.h:36
static int width
Definition: utils.c:156
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1339
#define SGI_HEADER_SIZE
Definition: sgi.h:30
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:602
#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: avcodec.h:997
#define SGI_SINGLE_CHAN
Definition: sgienc.c:28
int height
Definition: gxfenc.c:72
#define SGI_MAGIC
SGI image file signature.
Definition: sgi.h:28
Y , 8bpp.
Definition: pixfmt.h:73
common internal api header.
void * priv_data
Definition: avcodec.h:1382
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
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
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:898