FFmpeg
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 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 "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; ///< depth of pixel
34  int length; ///< length (bytes) of image
35  int type; ///< type of file
36  int maptype; ///< type of colormap
37  int maplength; ///< length (bytes) of colormap
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, y;
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 
90  ptr = pixels;
91 
92 #define GET_VALUE y >= avctx->height ? 0 : x >= len ? ptr[len-1] : ptr[x]
93 
94  x = 0, y = 0;
95  value2 = GET_VALUE;
96  while (y < avctx->height) {
97  run = 1;
98  value = value2;
99  x++;
100  if (x >= alen) {
101  x = 0;
102  ptr += linesize, y++;
103  }
104 
105  value2 = GET_VALUE;
106  while (value2 == value && run < 256 && y < avctx->height) {
107  x++;
108  run++;
109  if (x >= alen) {
110  x = 0;
111  ptr += linesize, y++;
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  for (y = 0; y < avctx->height; y++) {
131  bytestream2_put_buffer(&s->p, ptr, len);
132  if (len < alen)
133  bytestream2_put_byteu(&s->p, 0);
134  ptr += linesize;
135  }
136  }
137 }
138 
140 {
141  SUNRASTContext *s = avctx->priv_data;
142 
143 #if FF_API_CODER_TYPE
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  }
157  if (s->type != RT_BYTE_ENCODED && s->type != RT_STANDARD)
158 #endif
159  // adjust boolean option to RT equivalent
160  s->type++;
161 
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  /* fall-through */
173  case AV_PIX_FMT_GRAY8:
174  s->depth = 8;
175  break;
176  case AV_PIX_FMT_BGR24:
177  s->depth = 24;
178  break;
179  default:
180  return AVERROR_BUG;
181  }
182  s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
183  s->size = 32 + s->maplength + s->length * s->type;
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_packet2(avctx, avpkt, s->size, 0)) < 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 
212 #define OFFSET(x) offsetof(SUNRASTContext, x)
213 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
214 static const AVOption options[] = {
215  { "rle", "Use run-length compression", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
216 
217  { NULL },
218 };
219 
220 static const AVClass sunrast_class = {
221  .class_name = "sunrast",
222  .item_name = av_default_item_name,
223  .option = options,
224  .version = LIBAVUTIL_VERSION_INT,
225 };
226 
227 #if FF_API_CODER_TYPE
229  { "coder", "rle" },
230  { NULL },
231 };
232 #endif
233 
235  .name = "sunrast",
236  .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
237  .type = AVMEDIA_TYPE_VIDEO,
238  .id = AV_CODEC_ID_SUNRAST,
239  .priv_data_size = sizeof(SUNRASTContext),
241  .encode2 = sunrast_encode_frame,
244 #endif
245  .priv_class = &sunrast_class,
246  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24,
250  AV_PIX_FMT_NONE },
251 };
AVCodec
AVCodec.
Definition: avcodec.h:3481
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
SUNRASTContext::p
PutByteContext p
Definition: sunrastenc.c:32
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
opt.h
RMT_EQUAL_RGB
#define RMT_EQUAL_RGB
Definition: sunrast.h:28
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
pixels
int pixels
Definition: avisynth_c.h:390
SUNRASTContext::length
int length
length (bytes) of image
Definition: sunrastenc.c:34
internal.h
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
AVOption
AVOption.
Definition: opt.h:246
bytestream2_tell_p
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
Definition: bytestream.h:193
AV_PIX_FMT_MONOWHITE
@ AV_PIX_FMT_MONOWHITE
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb.
Definition: pixfmt.h:75
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1509
sunrast_image_write_header
static void sunrast_image_write_header(AVCodecContext *avctx)
Definition: sunrastenc.c:41
options
static const AVOption options[]
Definition: sunrastenc.c:214
defaults
static const AVCodecDefault defaults[]
Definition: amfenc_h264.c:361
SUNRASTContext::type
int type
type of file
Definition: sunrastenc.c:35
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
ff_sunrast_encoder
AVCodec ff_sunrast_encoder
Definition: sunrastenc.c:234
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_cold
#define av_cold
Definition: attributes.h:84
sunrast_image_write_image
static void sunrast_image_write_image(AVCodecContext *avctx, const uint8_t *pixels, const uint32_t *palette_data, int linesize)
Definition: sunrastenc.c:55
bytestream2_init_writer
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:143
GET_VALUE
#define GET_VALUE
s
#define s(width, name)
Definition: cbs_vp9.c:257
SUNRASTContext
Definition: sunrastenc.c:29
bytestream2_put_buffer
static av_always_inline unsigned int bytestream2_put_buffer(PutByteContext *p, const uint8_t *src, unsigned int size)
Definition: bytestream.h:282
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
AVCodecDefault
Definition: internal.h:231
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
run
uint8_t run
Definition: svq3.c:206
RT_STANDARD
#define RT_STANDARD
Definition: sunrast.h:34
pixel
uint8_t pixel
Definition: tiny_ssim.c:42
SUNRASTContext::depth
int depth
depth of pixel
Definition: sunrastenc.c:33
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
sunrast.h
sunrast_encode_init
static av_cold int sunrast_encode_init(AVCodecContext *avctx)
Definition: sunrastenc.c:139
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
RMT_NONE
#define RMT_NONE
Definition: sunrast.h:27
OFFSET
#define OFFSET(x)
Definition: sunrastenc.c:212
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
PutByteContext
Definition: bytestream.h:37
VE
#define VE
Definition: sunrastenc.c:213
AVPacket::size
int size
Definition: avcodec.h:1478
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
height
#define height
AVCodecContext::coder_type
attribute_deprecated int coder_type
Definition: avcodec.h:2482
RLE_TRIGGER
#define RLE_TRIGGER
Definition: sunrast.h:39
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1483
sunrast_encode_frame
static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: sunrastenc.c:188
bytestream2_skip_p
static av_always_inline void bytestream2_skip_p(PutByteContext *p, unsigned int size)
Definition: bytestream.h:176
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
uint8_t
uint8_t
Definition: audio_convert.c:194
AVCodec::name
const char * name
Name of the codec implementation.
Definition: avcodec.h:3488
len
int len
Definition: vorbis_enc_data.h:452
FF_CODER_TYPE_RLE
#define FF_CODER_TYPE_RLE
Definition: avcodec.h:2477
AVCodecContext::height
int height
Definition: avcodec.h:1738
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1775
avcodec.h
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
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:72
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
FF_CODER_TYPE_RAW
#define FF_CODER_TYPE_RAW
Definition: avcodec.h:2476
sunrast_class
static const AVClass sunrast_class
Definition: sunrastenc.c:220
SUNRASTContext::maptype
int maptype
type of colormap
Definition: sunrastenc.c:36
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
SUNRASTContext::size
int size
Definition: sunrastenc.c:38
sunrast_defaults
static const AVCodecDefault sunrast_defaults[]
Definition: sunrastenc.c:228
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:223
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
RT_BYTE_ENCODED
#define RT_BYTE_ENCODED
Definition: sunrast.h:38
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:48
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:1592
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:1738
bytestream.h
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AV_CODEC_ID_SUNRAST
@ AV_CODEC_ID_SUNRAST
Definition: avcodec.h:328
RAS_MAGIC
#define RAS_MAGIC
Definition: sunrast.h:25
ff_alloc_packet2
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:32
SUNRASTContext::maplength
int maplength
length (bytes) of colormap
Definition: sunrastenc.c:37
FF_API_CODER_TYPE
#define FF_API_CODER_TYPE
Definition: version.h:88