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 };