FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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  avctx->coded_frame = av_frame_alloc();
38  if (!avctx->coded_frame)
39  return AVERROR(ENOMEM);
40 
41  return 0;
42 }
43 
45  const AVFrame *frame, int *got_packet)
46 {
47  const AVFrame * const p = frame;
48  uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf, *buf;
49  int x, y, z, length, tablesize, ret;
50  unsigned int width, height, depth, dimension, bytes_per_channel, pixmax, put_be;
51  unsigned char *end_buf;
52 
54  avctx->coded_frame->key_frame = 1;
55 
56  width = avctx->width;
57  height = avctx->height;
58  bytes_per_channel = 1;
59  pixmax = 0xFF;
60  put_be = HAVE_BIGENDIAN;
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;
76  put_be = !HAVE_BIGENDIAN;
79  bytes_per_channel = 2;
80  pixmax = 0xFFFF;
81  dimension = SGI_SINGLE_CHAN;
82  depth = SGI_GRAYSCALE;
83  break;
84  case AV_PIX_FMT_RGB48LE:
85  put_be = !HAVE_BIGENDIAN;
86  case AV_PIX_FMT_RGB48BE:
88  bytes_per_channel = 2;
89  pixmax = 0xFFFF;
90  dimension = SGI_MULTI_CHAN;
91  depth = SGI_RGB;
92  break;
94  put_be = !HAVE_BIGENDIAN;
97  bytes_per_channel = 2;
98  pixmax = 0xFFFF;
99  dimension = SGI_MULTI_CHAN;
100  depth = SGI_RGBA;
101  break;
102  default:
103  return AVERROR_INVALIDDATA;
104  }
105 
106  tablesize = depth * height * 4;
107  length = SGI_HEADER_SIZE;
108  if (avctx->coder_type == FF_CODER_TYPE_RAW)
109  length += depth * height * width;
110  else // assume ff_rl_encode() produces at most 2x size of input
111  length += tablesize * 2 + depth * height * (2 * width + 1);
112 
113  if ((ret = ff_alloc_packet2(avctx, pkt, bytes_per_channel * length)) < 0)
114  return ret;
115  buf = pkt->data;
116  end_buf = pkt->data + pkt->size;
117 
118  /* Encode header. */
119  bytestream_put_be16(&buf, SGI_MAGIC);
120  bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/
121  bytestream_put_byte(&buf, bytes_per_channel);
122  bytestream_put_be16(&buf, dimension);
123  bytestream_put_be16(&buf, width);
124  bytestream_put_be16(&buf, height);
125  bytestream_put_be16(&buf, depth);
126 
127  bytestream_put_be32(&buf, 0L); /* pixmin */
128  bytestream_put_be32(&buf, pixmax);
129  bytestream_put_be32(&buf, 0L); /* dummy */
130 
131  /* name */
132  memset(buf, 0, SGI_HEADER_SIZE);
133  buf += 80;
134 
135  /* colormap */
136  bytestream_put_be32(&buf, 0L);
137 
138  /* The rest of the 512 byte header is unused. */
139  buf += 404;
140  offsettab = buf;
141 
142  if (avctx->coder_type != FF_CODER_TYPE_RAW) {
143  /* Skip RLE offset table. */
144  buf += tablesize;
145  lengthtab = buf;
146 
147  /* Skip RLE length table. */
148  buf += tablesize;
149 
150  /* Make an intermediate consecutive buffer. */
151  if (!(encode_buf = av_malloc(width)))
152  return -1;
153 
154  for (z = 0; z < depth; z++) {
155  in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
156 
157  for (y = 0; y < height; y++) {
158  bytestream_put_be32(&offsettab, buf - pkt->data);
159 
160  for (x = 0; x < width; x++)
161  encode_buf[x] = in_buf[depth * x];
162 
163  if ((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) {
164  av_free(encode_buf);
165  return -1;
166  }
167 
168  buf += length;
169  bytestream_put_byte(&buf, 0);
170  bytestream_put_be32(&lengthtab, length + 1);
171  in_buf -= p->linesize[0];
172  }
173  }
174 
175  av_free(encode_buf);
176  } else {
177  for (z = 0; z < depth; z++) {
178  in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
179 
180  for (y = 0; y < height; y++) {
181  for (x = 0; x < width * depth; x += depth)
182  if (bytes_per_channel == 1) {
183  bytestream_put_byte(&buf, in_buf[x]);
184  } else {
185  if (put_be) {
186  bytestream_put_be16(&buf, ((uint16_t *)in_buf)[x]);
187  } else {
188  bytestream_put_le16(&buf, ((uint16_t *)in_buf)[x]);
189  }
190  }
191 
192  in_buf -= p->linesize[0];
193  }
194  }
195  }
196 
197  /* total length */
198  pkt->size = buf - pkt->data;
199  pkt->flags |= AV_PKT_FLAG_KEY;
200  *got_packet = 1;
201 
202  return 0;
203 }
204 
206 {
207  av_frame_free(&avctx->coded_frame);
208  return 0;
209 }
210 
212  .name = "sgi",
213  .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
214  .type = AVMEDIA_TYPE_VIDEO,
215  .id = AV_CODEC_ID_SGI,
216  .init = encode_init,
217  .encode2 = encode_frame,
218  .close = encode_close,
219  .pix_fmts = (const enum AVPixelFormat[]){
225  },
226 };