Libav
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 "libavutil/opt.h"
23 
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27 #include "sunrast.h"
28 
29 typedef struct SUNRASTContext {
30  AVClass *class;
31 
33  int depth;
34  int length;
35  int type;
36  int maptype;
37  int maplength;
38  int size;
40 
42 {
43  SUNRASTContext *s = avctx->priv_data;
44 
45  bytestream2_put_be32u(&s->p, RAS_MAGIC);
46  bytestream2_put_be32u(&s->p, avctx->width);
47  bytestream2_put_be32u(&s->p, avctx->height);
48  bytestream2_put_be32u(&s->p, s->depth);
49  bytestream2_put_be32u(&s->p, s->length);
50  bytestream2_put_be32u(&s->p, s->type);
51  bytestream2_put_be32u(&s->p, s->maptype);
52  bytestream2_put_be32u(&s->p, s->maplength);
53 }
54 
56  const uint8_t *pixels,
57  const uint32_t *palette_data,
58  int linesize)
59 {
60  SUNRASTContext *s = avctx->priv_data;
61  const uint8_t *ptr;
62  int len, alen, x;
63 
64  if (s->maplength) { // palettized
65  PutByteContext pb_r, pb_g;
66  int len = s->maplength / 3;
67 
68  pb_r = s->p;
69  bytestream2_skip_p(&s->p, len);
70  pb_g = s->p;
71  bytestream2_skip_p(&s->p, len);
72 
73  for (x = 0; x < len; x++) {
74  uint32_t pixel = palette_data[x];
75 
76  bytestream2_put_byteu(&pb_r, (pixel >> 16) & 0xFF);
77  bytestream2_put_byteu(&pb_g, (pixel >> 8) & 0xFF);
78  bytestream2_put_byteu(&s->p, pixel & 0xFF);
79  }
80  }
81 
82  len = (s->depth * avctx->width + 7) >> 3;
83  alen = len + (len & 1);
84  ptr = pixels;
85 
86  if (s->type == RT_BYTE_ENCODED) {
87  uint8_t value, value2;
88  int run;
89  const uint8_t *start = linesize < 0 ? pixels + (avctx->height - 1) * linesize
90  : pixels;
91  const uint8_t *end = linesize < 0 ? pixels - linesize
92  : pixels + avctx->height * linesize;
93 
94  ptr = pixels;
95 
96 #define GET_VALUE ptr >= end || ptr < start ? 0 : x >= len ? ptr[len-1] : ptr[x]
97 
98  x = 0;
99  value2 = GET_VALUE;
100  while (ptr < end && ptr >= start) {
101  run = 1;
102  value = value2;
103  x++;
104  if (x >= alen) {
105  x = 0;
106  ptr += linesize;
107  }
108 
109  value2 = GET_VALUE;
110  while (value2 == value && run < 256 && ptr < end && ptr >= start) {
111  x++;
112  run++;
113  if (x >= alen) {
114  x = 0;
115  ptr += linesize;
116  }
117  value2 = GET_VALUE;
118  }
119 
120  if (run > 2 || value == RLE_TRIGGER) {
121  bytestream2_put_byteu(&s->p, RLE_TRIGGER);
122  bytestream2_put_byteu(&s->p, run - 1);
123  if (run > 1)
124  bytestream2_put_byteu(&s->p, value);
125  } else if (run == 1) {
126  bytestream2_put_byteu(&s->p, value);
127  } else
128  bytestream2_put_be16u(&s->p, (value << 8) | value);
129  }
130 
131  // update data length for header
132  s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;
133  } else {
134  int y;
135  for (y = 0; y < avctx->height; y++) {
136  bytestream2_put_buffer(&s->p, ptr, len);
137  if (len < alen)
138  bytestream2_put_byteu(&s->p, 0);
139  ptr += linesize;
140  }
141  }
142 }
143 
145 {
146  SUNRASTContext *s = avctx->priv_data;
147 
148 #if FF_API_CODER_TYPE
150  switch (avctx->coder_type) {
151  case FF_CODER_TYPE_RLE:
152  s->type = RT_BYTE_ENCODED;
153  break;
154  case FF_CODER_TYPE_RAW:
155  s->type = RT_STANDARD;
156  break;
157  default:
158  av_log(avctx, AV_LOG_ERROR, "invalid coder_type\n");
159  return AVERROR(EINVAL);
160  }
162  if (s->type != RT_BYTE_ENCODED && s->type != RT_STANDARD)
163 #endif
164  // adjust boolean option to RT equivalent
165  s->type++;
166 
167 #if FF_API_CODED_FRAME
169  avctx->coded_frame->key_frame = 1;
172 #endif
173  s->maptype = RMT_NONE;
174  s->maplength = 0;
175 
176  switch (avctx->pix_fmt) {
178  s->depth = 1;
179  break;
180  case AV_PIX_FMT_PAL8 :
181  s->maptype = RMT_EQUAL_RGB;
182  s->maplength = 3 * 256;
183  /* fall-through */
184  case AV_PIX_FMT_GRAY8:
185  s->depth = 8;
186  break;
187  case AV_PIX_FMT_BGR24:
188  s->depth = 24;
189  break;
190  default:
191  return AVERROR_BUG;
192  }
193  s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
194  s->size = 32 + s->maplength + s->length * s->type;
195 
196  return 0;
197 }
198 
199 static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
200  const AVFrame *frame, int *got_packet_ptr)
201 {
202  SUNRASTContext *s = avctx->priv_data;
203  int ret;
204 
205  if ((ret = ff_alloc_packet(avpkt, s->size)) < 0)
206  return ret;
207 
208  bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
210  sunrast_image_write_image(avctx, frame->data[0],
211  (const uint32_t *)frame->data[1],
212  frame->linesize[0]);
213  // update data length in header after RLE
214  if (s->type == RT_BYTE_ENCODED)
215  AV_WB32(&avpkt->data[16], s->length);
216 
217  *got_packet_ptr = 1;
218  avpkt->flags |= AV_PKT_FLAG_KEY;
219  avpkt->size = bytestream2_tell_p(&s->p);
220  return 0;
221 }
222 
223 #define OFFSET(x) offsetof(SUNRASTContext, x)
224 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
225 static const AVOption options[] = {
226  { "rle", "Use run-length compression", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
227 
228  { NULL },
229 };
230 
231 static const AVClass sunrast_class = {
232  .class_name = "sunrast",
233  .item_name = av_default_item_name,
234  .option = options,
235  .version = LIBAVUTIL_VERSION_INT,
236 };
237 
238 #if FF_API_CODER_TYPE
240  { "coder", "rle" },
241  { NULL },
242 };
243 #endif
244 
246  .name = "sunrast",
247  .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
248  .type = AVMEDIA_TYPE_VIDEO,
249  .id = AV_CODEC_ID_SUNRAST,
250  .priv_data_size = sizeof(SUNRASTContext),
251  .priv_class = &sunrast_class,
253  .encode2 = sunrast_encode_frame,
255  .defaults = sunrast_defaults,
256 #endif
257  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24,
261  AV_PIX_FMT_NONE },
262 };
#define RMT_EQUAL_RGB
Definition: sunrast.h:28
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
AVOption.
Definition: opt.h:234
static const AVCodecDefault sunrast_defaults[]
Definition: sunrastenc.c:239
int size
Definition: avcodec.h:1347
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:141
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
#define VE
Definition: sunrastenc.c:224
#define FF_API_CODER_TYPE
Definition: version.h:189
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
int length
length (bytes) of image
Definition: sunrastenc.c:34
uint8_t run
Definition: svq3.c:203
AVCodec.
Definition: avcodec.h:3120
static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: sunrastenc.c:199
int depth
depth of pixel
Definition: sunrastenc.c:33
#define AV_WB32(p, val)
Definition: intreadwrite.h:246
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:39
uint8_t
#define av_cold
Definition: attributes.h:66
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:70
AVOptions.
PutByteContext p
Definition: sunrastenc.c:32
uint8_t * data
Definition: avcodec.h:1346
#define RT_STANDARD
Definition: sunrast.h:34
int maplength
length (bytes) of colormap
Definition: sunrastenc.c:37
#define FFALIGN(x, a)
Definition: macros.h:48
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
int maptype
type of colormap
Definition: sunrastenc.c:36
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
Definition: bytestream.h:190
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
#define RLE_TRIGGER
Definition: sunrast.h:39
static av_always_inline void bytestream2_skip_p(PutByteContext *p, unsigned int size)
Definition: bytestream.h:173
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:201
int width
picture width / height.
Definition: avcodec.h:1580
static const AVOption options[]
Definition: sunrastenc.c:225
int type
type of file
Definition: sunrastenc.c:35
#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:1211
AVCodec ff_sunrast_encoder
Definition: sunrastenc.c:245
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:62
#define pixel
static const AVCodecDefault defaults[]
Definition: libkvazaar.c:278
static av_always_inline unsigned int bytestream2_put_buffer(PutByteContext *p, const uint8_t *src, unsigned int size)
Definition: bytestream.h:279
attribute_deprecated int coder_type
Definition: avcodec.h:2440
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
NULL
Definition: eval.c:55
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
av_default_item_name
Definition: dnxhdenc.c:55
main external API structure.
Definition: avcodec.h:1409
static void sunrast_image_write_image(AVCodecContext *avctx, const uint8_t *pixels, const uint32_t *palette_data, int linesize)
Definition: sunrastenc.c:55
#define AVERROR_BUG
Bug detected, please report the issue.
Definition: error.h:60
Describe the class of an AVClass context structure.
Definition: log.h:34
static void sunrast_image_write_header(AVCodecContext *avctx)
Definition: sunrastenc.c:41
static av_cold int sunrast_encode_init(AVCodecContext *avctx)
Definition: sunrastenc.c:144
#define RT_BYTE_ENCODED
Definition: sunrast.h:38
#define OFFSET(x)
Definition: sunrastenc.c:223
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:257
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
Y , 8bpp.
Definition: pixfmt.h:67
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
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:68
#define FF_CODER_TYPE_RAW
Definition: avcodec.h:2431
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2797
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
void * priv_data
Definition: avcodec.h:1451
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:78
int len
#define FF_CODER_TYPE_RLE
Definition: avcodec.h:2432
static const AVClass sunrast_class
Definition: sunrastenc.c:231
#define RAS_MAGIC
Definition: sunrast.h:25
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:196
#define GET_VALUE
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
This structure stores compressed data.
Definition: avcodec.h:1323