Libav
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 
32 {
33  avctx->coded_frame = av_frame_alloc();
34  if (!avctx->coded_frame)
35  return AVERROR(ENOMEM);
36 
37  return 0;
38 }
39 
40 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
41  const AVFrame *frame, int *got_packet)
42 {
43  const AVFrame * const p = frame;
44  uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf, *buf;
45  int x, y, z, length, tablesize, ret;
46  unsigned int width, height, depth, dimension;
47  unsigned char *end_buf;
48 
50  avctx->coded_frame->key_frame = 1;
51 
52  width = avctx->width;
53  height = avctx->height;
54 
55  switch (avctx->pix_fmt) {
56  case AV_PIX_FMT_GRAY8:
57  dimension = SGI_SINGLE_CHAN;
58  depth = SGI_GRAYSCALE;
59  break;
60  case AV_PIX_FMT_RGB24:
61  dimension = SGI_MULTI_CHAN;
62  depth = SGI_RGB;
63  break;
64  case AV_PIX_FMT_RGBA:
65  dimension = SGI_MULTI_CHAN;
66  depth = SGI_RGBA;
67  break;
68  default:
69  return AVERROR_INVALIDDATA;
70  }
71 
72  tablesize = depth * height * 4;
73  length = SGI_HEADER_SIZE;
74  if (avctx->coder_type == FF_CODER_TYPE_RAW)
75  length += depth * height * width;
76  else // assume ff_rl_encode() produces at most 2x size of input
77  length += tablesize * 2 + depth * height * (2 * width + 1);
78 
79  if ((ret = ff_alloc_packet(pkt, length)) < 0) {
80  av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", length);
81  return ret;
82  }
83  buf = pkt->data;
84  end_buf = pkt->data + pkt->size;
85 
86  /* Encode header. */
87  bytestream_put_be16(&buf, SGI_MAGIC);
88  bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/
89  bytestream_put_byte(&buf, 1); /* bytes_per_channel */
90  bytestream_put_be16(&buf, dimension);
91  bytestream_put_be16(&buf, width);
92  bytestream_put_be16(&buf, height);
93  bytestream_put_be16(&buf, depth);
94 
95  /* The rest are constant in this implementation. */
96  bytestream_put_be32(&buf, 0L); /* pixmin */
97  bytestream_put_be32(&buf, 255L); /* pixmax */
98  bytestream_put_be32(&buf, 0L); /* dummy */
99 
100  /* name */
101  memset(buf, 0, SGI_HEADER_SIZE);
102  buf += 80;
103 
104  /* colormap */
105  bytestream_put_be32(&buf, 0L);
106 
107  /* The rest of the 512 byte header is unused. */
108  buf += 404;
109  offsettab = buf;
110 
111  if (avctx->coder_type != FF_CODER_TYPE_RAW) {
112  /* Skip RLE offset table. */
113  buf += tablesize;
114  lengthtab = buf;
115 
116  /* Skip RLE length table. */
117  buf += tablesize;
118 
119  /* Make an intermediate consecutive buffer. */
120  if (!(encode_buf = av_malloc(width)))
121  return -1;
122 
123  for (z = 0; z < depth; z++) {
124  in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
125 
126  for (y = 0; y < height; y++) {
127  bytestream_put_be32(&offsettab, buf - pkt->data);
128 
129  for (x = 0; x < width; x++)
130  encode_buf[x] = in_buf[depth * x];
131 
132  if ((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) {
133  av_free(encode_buf);
134  return -1;
135  }
136 
137  buf += length;
138  bytestream_put_byte(&buf, 0);
139  bytestream_put_be32(&lengthtab, length + 1);
140  in_buf -= p->linesize[0];
141  }
142  }
143 
144  av_free(encode_buf);
145  } else {
146  for (z = 0; z < depth; z++) {
147  in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
148 
149  for (y = 0; y < height; y++) {
150  for (x = 0; x < width * depth; x += depth)
151  bytestream_put_byte(&buf, in_buf[x]);
152 
153  in_buf -= p->linesize[0];
154  }
155  }
156  }
157 
158  /* total length */
159  pkt->size = buf - pkt->data;
160  pkt->flags |= AV_PKT_FLAG_KEY;
161  *got_packet = 1;
162 
163  return 0;
164 }
165 
167 {
168  av_frame_free(&avctx->coded_frame);
169  return 0;
170 }
171 
173  .name = "sgi",
174  .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
175  .type = AVMEDIA_TYPE_VIDEO,
176  .id = AV_CODEC_ID_SGI,
177  .init = encode_init,
178  .encode2 = encode_frame,
179  .close = encode_close,
180  .pix_fmts = (const enum AVPixelFormat[]){
182  },
183 };
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:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:107
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2506
int size
Definition: avcodec.h:974
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1247
AVCodec.
Definition: avcodec.h:2755
#define SGI_MULTI_CHAN
Definition: sgienc.c:29
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:43
int coder_type
coder type
Definition: avcodec.h:2164
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: sgienc.c:40
uint8_t * data
Definition: avcodec.h:973
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1023
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:55
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:2762
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:96
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
#define SGI_RGBA
Definition: sgi.h:34
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:168
int width
picture width / height.
Definition: avcodec.h:1217
AVCodec ff_sgi_encoder
Definition: sgienc.c:172
static av_cold int encode_init(AVCodecContext *avctx)
Definition: sgienc.c:31
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1125
#define L(x)
Definition: vp56_arith.h:36
static int width
Definition: utils.c:156
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:125
main external API structure.
Definition: avcodec.h:1054
#define SGI_HEADER_SIZE
Definition: sgi.h:30
static av_cold int encode_close(AVCodecContext *avctx)
Definition: sgienc.c:166
#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:113
#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.
#define FF_CODER_TYPE_RAW
Definition: avcodec.h:2156
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:163
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:950