sunrastenc.c
Go to the documentation of this file.
1 /*
2  * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image encoder
3  * Copyright (c) 2012 Aneesh Dogra (lionaneesh) <lionaneesh@gmail.com>
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 "sunrast.h"
26 
27 typedef struct SUNRASTContext {
30  int depth;
31  int length;
32  int type;
33  int maptype;
34  int maplength;
35  int size;
37 
39 {
40  SUNRASTContext *s = avctx->priv_data;
41 
42  bytestream2_put_be32u(&s->p, RAS_MAGIC);
43  bytestream2_put_be32u(&s->p, avctx->width);
44  bytestream2_put_be32u(&s->p, avctx->height);
45  bytestream2_put_be32u(&s->p, s->depth);
46  bytestream2_put_be32u(&s->p, s->length);
47  bytestream2_put_be32u(&s->p, s->type);
48  bytestream2_put_be32u(&s->p, s->maptype);
49  bytestream2_put_be32u(&s->p, s->maplength);
50 }
51 
53  const uint8_t *pixels,
54  const uint32_t *palette_data,
55  int linesize)
56 {
57  SUNRASTContext *s = avctx->priv_data;
58  const uint8_t *ptr;
59  int len, alen, x;
60 
61  if (s->maplength) { // palettized
62  PutByteContext pb_r, pb_g;
63  int len = s->maplength / 3;
64 
65  pb_r = s->p;
66  bytestream2_skip_p(&s->p, len);
67  pb_g = s->p;
68  bytestream2_skip_p(&s->p, len);
69 
70  for (x = 0; x < len; x++) {
71  uint32_t pixel = palette_data[x];
72 
73  bytestream2_put_byteu(&pb_r, (pixel >> 16) & 0xFF);
74  bytestream2_put_byteu(&pb_g, (pixel >> 8) & 0xFF);
75  bytestream2_put_byteu(&s->p, pixel & 0xFF);
76  }
77  }
78 
79  len = (s->depth * avctx->width + 7) >> 3;
80  alen = len + (len & 1);
81  ptr = pixels;
82 
83  if (s->type == RT_BYTE_ENCODED) {
84  uint8_t value, value2;
85  int run;
86  const uint8_t *start = linesize < 0 ? pixels + (avctx->height - 1) * linesize
87  : pixels;
88  const uint8_t *end = linesize < 0 ? pixels - linesize
89  : pixels + avctx->height * linesize;
90 
91  ptr = pixels;
92 
93 #define GET_VALUE ptr >= end || ptr < start ? 0 : x >= len ? ptr[len-1] : ptr[x]
94 
95  x = 0;
96  value2 = GET_VALUE;
97  while (ptr < end && ptr >= start) {
98  run = 1;
99  value = value2;
100  x++;
101  if (x >= alen) {
102  x = 0;
103  ptr += linesize;
104  }
105 
106  value2 = GET_VALUE;
107  while (value2 == value && run < 256 && ptr < end && ptr >= start) {
108  x++;
109  run++;
110  if (x >= alen) {
111  x = 0;
112  ptr += linesize;
113  }
114  value2 = GET_VALUE;
115  }
116 
117  if (run > 2 || value == RLE_TRIGGER) {
118  bytestream2_put_byteu(&s->p, RLE_TRIGGER);
119  bytestream2_put_byteu(&s->p, run - 1);
120  if (run > 1)
121  bytestream2_put_byteu(&s->p, value);
122  } else if (run == 1) {
123  bytestream2_put_byteu(&s->p, value);
124  } else
125  bytestream2_put_be16u(&s->p, (value << 8) | value);
126  }
127 
128  // update data length for header
129  s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;
130  } else {
131  int y;
132  for (y = 0; y < avctx->height; y++) {
133  bytestream2_put_buffer(&s->p, ptr, len);
134  if (len < alen)
135  bytestream2_put_byteu(&s->p, 0);
136  ptr += linesize;
137  }
138  }
139 }
140 
142 {
143  SUNRASTContext *s = avctx->priv_data;
144 
145  switch (avctx->coder_type) {
146  case FF_CODER_TYPE_RLE:
147  s->type = RT_BYTE_ENCODED;
148  break;
149  case FF_CODER_TYPE_RAW:
150  s->type = RT_STANDARD;
151  break;
152  default:
153  av_log(avctx, AV_LOG_ERROR, "invalid coder_type\n");
154  return AVERROR(EINVAL);
155  }
156 
157  avctx->coded_frame = &s->picture;
158  avctx->coded_frame->key_frame = 1;
160  s->maptype = RMT_NONE;
161  s->maplength = 0;
162 
163  switch (avctx->pix_fmt) {
165  s->depth = 1;
166  break;
167  case AV_PIX_FMT_PAL8 :
168  s->maptype = RMT_EQUAL_RGB;
169  s->maplength = 3 * 256;
170  case AV_PIX_FMT_GRAY8:
171  s->depth = 8;
172  break;
173  case AV_PIX_FMT_BGR24:
174  s->depth = 24;
175  break;
176  default:
177  return AVERROR_BUG;
178  }
179  s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
180  s->size = 32 + s->maplength +
181  s->length * (s->type == RT_BYTE_ENCODED ? 2 : 1);
182 
183  return 0;
184 }
185 
186 static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
187  const AVFrame *frame, int *got_packet_ptr)
188 {
189  SUNRASTContext *s = avctx->priv_data;
190  int ret;
191 
192  if ((ret = ff_alloc_packet(avpkt, s->size)) < 0)
193  return ret;
194 
195  bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
197  sunrast_image_write_image(avctx, frame->data[0],
198  (const uint32_t *)frame->data[1],
199  frame->linesize[0]);
200  // update data length in header after RLE
201  if (s->type == RT_BYTE_ENCODED)
202  AV_WB32(&avpkt->data[16], s->length);
203 
204  *got_packet_ptr = 1;
205  avpkt->flags |= AV_PKT_FLAG_KEY;
206  avpkt->size = bytestream2_tell_p(&s->p);
207  return 0;
208 }
209 
211  { "coder", "rle" },
212  { NULL },
213 };
214 
216  .name = "sunrast",
217  .type = AVMEDIA_TYPE_VIDEO,
218  .id = AV_CODEC_ID_SUNRAST,
219  .priv_data_size = sizeof(SUNRASTContext),
221  .encode2 = sunrast_encode_frame,
222  .defaults = sunrast_defaults,
223  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24,
227  AV_PIX_FMT_NONE },
228  .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
229 };
#define RMT_EQUAL_RGB
Definition: sunrast.h:28
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
static const AVCodecDefault sunrast_defaults[]
Definition: sunrastenc.c:210
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
AVFrame picture
Definition: sunrast.c:30
int size
Definition: avcodec.h:916
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:139
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
int length
length (bytes) of image
Definition: sunrastenc.c:31
uint8_t run
Definition: svq3.c:132
AVCodec.
Definition: avcodec.h:2960
static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: sunrastenc.c:186
int depth
depth of pixel
Definition: sunrastenc.c:30
uint8_t
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
#define AV_WB32(p, d)
Definition: intreadwrite.h:239
PutByteContext p
Definition: sunrastenc.c:29
int coder_type
coder type
Definition: avcodec.h:2388
uint8_t * data
Definition: avcodec.h:915
#define RT_STANDARD
Definition: sunrast.h:34
int maplength
length (bytes) of colormap
Definition: sunrastenc.c:34
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
static const AVCodecDefault defaults[]
Definition: libspeexenc.c:358
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
int maptype
type of colormap
Definition: sunrastenc.c:33
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
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
Definition: bytestream.h:188
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:921
#define RLE_TRIGGER
Definition: sunrast.h:39
static av_always_inline void bytestream2_skip_p(PutByteContext *p, unsigned int size)
Definition: bytestream.h:171
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
int type
type of file
Definition: sunrastenc.c:32
#define RMT_NONE
Definition: sunrast.h:27
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:880
AVCodec ff_sunrast_encoder
Definition: sunrastenc.c:215
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
#define pixel
static av_always_inline unsigned int bytestream2_put_buffer(PutByteContext *p, const uint8_t *src, unsigned int size)
Definition: bytestream.h:277
NULL
Definition: eval.c:52
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
static void sunrast_image_write_image(AVCodecContext *avctx, const uint8_t *pixels, const uint32_t *palette_data, int linesize)
Definition: sunrastenc.c:52
static void sunrast_image_write_header(AVCodecContext *avctx)
Definition: sunrastenc.c:38
static av_cold int sunrast_encode_init(AVCodecContext *avctx)
Definition: sunrastenc.c:141
#define RT_BYTE_ENCODED
Definition: sunrast.h:38
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
Y , 8bpp.
Definition: pixfmt.h:73
common internal api header.
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:74
struct SUNRASTContext SUNRASTContext
void * priv_data
Definition: avcodec.h:1382
int len
#define RAS_MAGIC
Definition: sunrast.h:25
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
#define GET_VALUE
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:898