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; ///< depth of pixel
31  int length; ///< length (bytes) of image
32  int type; ///< type of file
33  int maptype; ///< type of colormap
34  int maplength; ///< length (bytes) of colormap
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, y;
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 
87  ptr = pixels;
88 
89 #define GET_VALUE y >= avctx->height ? 0 : x >= len ? ptr[len-1] : ptr[x]
90 
91  x = 0, y = 0;
92  value2 = GET_VALUE;
93  while (y < avctx->height) {
94  run = 1;
95  value = value2;
96  x++;
97  if (x >= alen) {
98  x = 0;
99  ptr += linesize, y++;
100  }
101 
102  value2 = GET_VALUE;
103  while (value2 == value && run < 256 && y < avctx->height) {
104  x++;
105  run++;
106  if (x >= alen) {
107  x = 0;
108  ptr += linesize, y++;
109  }
110  value2 = GET_VALUE;
111  }
112 
113  if (run > 2 || value == RLE_TRIGGER) {
114  bytestream2_put_byteu(&s->p, RLE_TRIGGER);
115  bytestream2_put_byteu(&s->p, run - 1);
116  if (run > 1)
117  bytestream2_put_byteu(&s->p, value);
118  } else if (run == 1) {
119  bytestream2_put_byteu(&s->p, value);
120  } else
121  bytestream2_put_be16u(&s->p, (value << 8) | value);
122  }
123 
124  // update data length for header
125  s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;
126  } else {
127  for (y = 0; y < avctx->height; y++) {
128  bytestream2_put_buffer(&s->p, ptr, len);
129  if (len < alen)
130  bytestream2_put_byteu(&s->p, 0);
131  ptr += linesize;
132  }
133  }
134 }
135 
137 {
138  SUNRASTContext *s = avctx->priv_data;
139 
140  switch (avctx->coder_type) {
141  case FF_CODER_TYPE_RLE:
142  s->type = RT_BYTE_ENCODED;
143  break;
144  case FF_CODER_TYPE_RAW:
145  s->type = RT_STANDARD;
146  break;
147  default:
148  av_log(avctx, AV_LOG_ERROR, "invalid coder_type\n");
149  return AVERROR(EINVAL);
150  }
151 
152  avctx->coded_frame = &s->picture;
153  avctx->coded_frame->key_frame = 1;
155  s->maptype = RMT_NONE;
156  s->maplength = 0;
157 
158  switch (avctx->pix_fmt) {
160  s->depth = 1;
161  break;
162  case AV_PIX_FMT_PAL8 :
163  s->maptype = RMT_EQUAL_RGB;
164  s->maplength = 3 * 256;
165  case AV_PIX_FMT_GRAY8:
166  s->depth = 8;
167  break;
168  case AV_PIX_FMT_BGR24:
169  s->depth = 24;
170  break;
171  default:
172  return AVERROR_BUG;
173  }
174  s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
175  s->size = 32 + s->maplength +
176  s->length * (s->type == RT_BYTE_ENCODED ? 2 : 1);
177 
178  return 0;
179 }
180 
181 static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
182  const AVFrame *frame, int *got_packet_ptr)
183 {
184  SUNRASTContext *s = avctx->priv_data;
185  int ret;
186 
187  if ((ret = ff_alloc_packet2(avctx, avpkt, s->size)) < 0)
188  return ret;
189 
190  bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
192  sunrast_image_write_image(avctx, frame->data[0],
193  (const uint32_t *)frame->data[1],
194  frame->linesize[0]);
195  // update data length in header after RLE
196  if (s->type == RT_BYTE_ENCODED)
197  AV_WB32(&avpkt->data[16], s->length);
198 
199  *got_packet_ptr = 1;
200  avpkt->flags |= AV_PKT_FLAG_KEY;
201  avpkt->size = bytestream2_tell_p(&s->p);
202  return 0;
203 }
204 
206  { "coder", "rle" },
207  { NULL },
208 };
209 
211  .name = "sunrast",
212  .type = AVMEDIA_TYPE_VIDEO,
213  .id = AV_CODEC_ID_SUNRAST,
214  .priv_data_size = sizeof(SUNRASTContext),
216  .encode2 = sunrast_encode_frame,
217  .defaults = sunrast_defaults,
218  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24,
222  AV_PIX_FMT_NONE },
223  .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
224 };
#define RMT_EQUAL_RGB
Definition: sunrast.h:28
const char * s
Definition: avisynth_c.h:668
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
static const AVCodecDefault sunrast_defaults[]
Definition: sunrastenc.c:205
AVFrame * coded_frame
the picture in the bitstream
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
AVFrame picture
Definition: sunrastenc.c:28
#define FF_CODER_TYPE_RAW
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.
int length
length (bytes) of image
Definition: sunrastenc.c:31
uint8_t run
Definition: svq3.c:136
static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: sunrastenc.c:181
int depth
depth of pixel
Definition: sunrastenc.c:30
#define FFALIGN(x, a)
Definition: common.h:63
#define AV_WB32(p, darg)
Definition: intreadwrite.h:265
uint8_t
#define av_cold
Definition: attributes.h:78
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:79
PutByteContext p
Definition: sunrastenc.c:29
int coder_type
coder type
uint8_t * data
#define RT_STANDARD
Definition: sunrast.h:34
#define FF_CODER_TYPE_RLE
int maplength
length (bytes) of colormap
Definition: sunrastenc.c:34
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
frame
Definition: stft.m:14
Discrete Time axis x
static const AVCodecDefault defaults[]
Definition: libspeexenc.c:344
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
int maptype
type of colormap
Definition: sunrastenc.c:33
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
Definition: bytestream.h:188
external API header
int flags
A combination of AV_PKT_FLAG values.
#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.
Definition: frame.h:144
ret
Definition: avfilter.c:821
int width
picture width / height.
int type
type of file
Definition: sunrastenc.c:32
#define RMT_NONE
Definition: sunrast.h:27
AVCodec ff_sunrast_encoder
Definition: sunrastenc.c:210
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:71
static av_always_inline unsigned int bytestream2_put_buffer(PutByteContext *p, const uint8_t *src, unsigned int size)
Definition: bytestream.h:277
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
NULL
Definition: eval.c:55
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
main external API structure.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
static void sunrast_image_write_image(AVCodecContext *avctx, const uint8_t *pixels, const uint32_t *palette_data, int linesize)
Definition: sunrastenc.c:52
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
double value
Definition: eval.c:82
static void sunrast_image_write_header(AVCodecContext *avctx)
Definition: sunrastenc.c:38
static av_cold int sunrast_encode_init(AVCodecContext *avctx)
Definition: sunrastenc.c:136
#define RT_BYTE_ENCODED
Definition: sunrast.h:38
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
uint8_t pixel
Definition: tiny_ssim.c:40
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
Y , 8bpp.
Definition: pixfmt.h:76
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:77
struct SUNRASTContext SUNRASTContext
function y
Definition: D.m:1
int len
#define RAS_MAGIC
Definition: sunrast.h:25
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:139
#define GET_VALUE
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
This structure stores compressed data.