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 "avcodec.h"
23 #include "bytestream.h"
24 #include "internal.h"
25 #include "sunrast.h"
26 
27 typedef struct SUNRASTContext {
29  int depth;
30  int length;
31  int type;
32  int maptype;
33  int maplength;
34  int size;
36 
38 {
39  SUNRASTContext *s = avctx->priv_data;
40 
41  bytestream2_put_be32u(&s->p, RAS_MAGIC);
42  bytestream2_put_be32u(&s->p, avctx->width);
43  bytestream2_put_be32u(&s->p, avctx->height);
44  bytestream2_put_be32u(&s->p, s->depth);
45  bytestream2_put_be32u(&s->p, s->length);
46  bytestream2_put_be32u(&s->p, s->type);
47  bytestream2_put_be32u(&s->p, s->maptype);
48  bytestream2_put_be32u(&s->p, s->maplength);
49 }
50 
52  const uint8_t *pixels,
53  const uint32_t *palette_data,
54  int linesize)
55 {
56  SUNRASTContext *s = avctx->priv_data;
57  const uint8_t *ptr;
58  int len, alen, x;
59 
60  if (s->maplength) { // palettized
61  PutByteContext pb_r, pb_g;
62  int len = s->maplength / 3;
63 
64  pb_r = s->p;
65  bytestream2_skip_p(&s->p, len);
66  pb_g = s->p;
67  bytestream2_skip_p(&s->p, len);
68 
69  for (x = 0; x < len; x++) {
70  uint32_t pixel = palette_data[x];
71 
72  bytestream2_put_byteu(&pb_r, (pixel >> 16) & 0xFF);
73  bytestream2_put_byteu(&pb_g, (pixel >> 8) & 0xFF);
74  bytestream2_put_byteu(&s->p, pixel & 0xFF);
75  }
76  }
77 
78  len = (s->depth * avctx->width + 7) >> 3;
79  alen = len + (len & 1);
80  ptr = pixels;
81 
82  if (s->type == RT_BYTE_ENCODED) {
83  uint8_t value, value2;
84  int run;
85  const uint8_t *start = linesize < 0 ? pixels + (avctx->height - 1) * linesize
86  : pixels;
87  const uint8_t *end = linesize < 0 ? pixels - linesize
88  : pixels + avctx->height * linesize;
89 
90  ptr = pixels;
91 
92 #define GET_VALUE ptr >= end || ptr < start ? 0 : x >= len ? ptr[len-1] : ptr[x]
93 
94  x = 0;
95  value2 = GET_VALUE;
96  while (ptr < end && ptr >= start) {
97  run = 1;
98  value = value2;
99  x++;
100  if (x >= alen) {
101  x = 0;
102  ptr += linesize;
103  }
104 
105  value2 = GET_VALUE;
106  while (value2 == value && run < 256 && ptr < end && ptr >= start) {
107  x++;
108  run++;
109  if (x >= alen) {
110  x = 0;
111  ptr += linesize;
112  }
113  value2 = GET_VALUE;
114  }
115 
116  if (run > 2 || value == RLE_TRIGGER) {
117  bytestream2_put_byteu(&s->p, RLE_TRIGGER);
118  bytestream2_put_byteu(&s->p, run - 1);
119  if (run > 1)
120  bytestream2_put_byteu(&s->p, value);
121  } else if (run == 1) {
122  bytestream2_put_byteu(&s->p, value);
123  } else
124  bytestream2_put_be16u(&s->p, (value << 8) | value);
125  }
126 
127  // update data length for header
128  s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;
129  } else {
130  int y;
131  for (y = 0; y < avctx->height; y++) {
132  bytestream2_put_buffer(&s->p, ptr, len);
133  if (len < alen)
134  bytestream2_put_byteu(&s->p, 0);
135  ptr += linesize;
136  }
137  }
138 }
139 
141 {
142  SUNRASTContext *s = avctx->priv_data;
143 
144  switch (avctx->coder_type) {
145  case FF_CODER_TYPE_RLE:
146  s->type = RT_BYTE_ENCODED;
147  break;
148  case FF_CODER_TYPE_RAW:
149  s->type = RT_STANDARD;
150  break;
151  default:
152  av_log(avctx, AV_LOG_ERROR, "invalid coder_type\n");
153  return AVERROR(EINVAL);
154  }
155 
156  avctx->coded_frame = av_frame_alloc();
157  if (!avctx->coded_frame)
158  return AVERROR(ENOMEM);
159 
160  avctx->coded_frame->key_frame = 1;
162  s->maptype = RMT_NONE;
163  s->maplength = 0;
164 
165  switch (avctx->pix_fmt) {
167  s->depth = 1;
168  break;
169  case AV_PIX_FMT_PAL8 :
170  s->maptype = RMT_EQUAL_RGB;
171  s->maplength = 3 * 256;
172  case AV_PIX_FMT_GRAY8:
173  s->depth = 8;
174  break;
175  case AV_PIX_FMT_BGR24:
176  s->depth = 24;
177  break;
178  default:
179  return AVERROR_BUG;
180  }
181  s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
182  s->size = 32 + s->maplength +
183  s->length * (s->type == RT_BYTE_ENCODED ? 2 : 1);
184 
185  return 0;
186 }
187 
188 static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
189  const AVFrame *frame, int *got_packet_ptr)
190 {
191  SUNRASTContext *s = avctx->priv_data;
192  int ret;
193 
194  if ((ret = ff_alloc_packet(avpkt, s->size)) < 0)
195  return ret;
196 
197  bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
199  sunrast_image_write_image(avctx, frame->data[0],
200  (const uint32_t *)frame->data[1],
201  frame->linesize[0]);
202  // update data length in header after RLE
203  if (s->type == RT_BYTE_ENCODED)
204  AV_WB32(&avpkt->data[16], s->length);
205 
206  *got_packet_ptr = 1;
207  avpkt->flags |= AV_PKT_FLAG_KEY;
208  avpkt->size = bytestream2_tell_p(&s->p);
209  return 0;
210 }
211 
213 {
214  av_frame_free(&avctx->coded_frame);
215  return 0;
216 }
217 
219  { "coder", "rle" },
220  { NULL },
221 };
222 
224  .name = "sunrast",
225  .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
226  .type = AVMEDIA_TYPE_VIDEO,
227  .id = AV_CODEC_ID_SUNRAST,
228  .priv_data_size = sizeof(SUNRASTContext),
231  .encode2 = sunrast_encode_frame,
232  .defaults = sunrast_defaults,
233  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24,
237  AV_PIX_FMT_NONE },
238 };
#define RMT_EQUAL_RGB
Definition: sunrast.h:28
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
static const AVCodecDefault sunrast_defaults[]
Definition: sunrastenc.c:218
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2548
int size
Definition: avcodec.h:974
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:141
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1270
int length
length (bytes) of image
Definition: sunrastenc.c:30
uint8_t run
Definition: svq3.c:146
AVCodec.
Definition: avcodec.h:2812
static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: sunrastenc.c:188
int depth
depth of pixel
Definition: sunrastenc.c:29
#define FFALIGN(x, a)
Definition: common.h:62
static av_cold int sunrast_encode_close(AVCodecContext *avctx)
Definition: sunrastenc.c:212
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:57
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:28
int coder_type
coder type
Definition: avcodec.h:2194
uint8_t * data
Definition: avcodec.h:973
#define RT_STANDARD
Definition: sunrast.h:34
int maplength
length (bytes) of colormap
Definition: sunrastenc.c:33
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
static const AVCodecDefault defaults[]
Definition: libspeexenc.c:345
#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:69
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
int maptype
type of colormap
Definition: sunrastenc.c:32
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
const char * name
Name of the codec implementation.
Definition: avcodec.h:2819
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:979
#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:196
int width
picture width / height.
Definition: avcodec.h:1229
int type
type of file
Definition: sunrastenc.c:31
#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:1245
AVCodec ff_sunrast_encoder
Definition: sunrastenc.c:223
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:279
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:153
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
static void sunrast_image_write_image(AVCodecContext *avctx, const uint8_t *pixels, const uint32_t *palette_data, int linesize)
Definition: sunrastenc.c:51
#define AVERROR_BUG
Bug detected, please report the issue.
Definition: error.h:60
static void sunrast_image_write_header(AVCodecContext *avctx)
Definition: sunrastenc.c:37
static av_cold int sunrast_encode_init(AVCodecContext *avctx)
Definition: sunrastenc.c:140
#define RT_BYTE_ENCODED
Definition: sunrast.h:38
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
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
#define FF_CODER_TYPE_RAW
Definition: avcodec.h:2184
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1092
int len
#define FF_CODER_TYPE_RLE
Definition: avcodec.h:2185
#define RAS_MAGIC
Definition: sunrast.h:25
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:191
#define GET_VALUE
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:950