FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "avcodec.h"
23 #include "bytestream.h"
24 #include "internal.h"
25 #include "sunrast.h"
26 
27 typedef struct SUNRASTContext {
29  int depth; ///< depth of pixel
30  int length; ///< length (bytes) of image
31  int type; ///< type of file
32  int maptype; ///< type of colormap
33  int maplength; ///< length (bytes) of colormap
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, y;
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 
86  ptr = pixels;
87 
88 #define GET_VALUE y >= avctx->height ? 0 : x >= len ? ptr[len-1] : ptr[x]
89 
90  x = 0, y = 0;
91  value2 = GET_VALUE;
92  while (y < avctx->height) {
93  run = 1;
94  value = value2;
95  x++;
96  if (x >= alen) {
97  x = 0;
98  ptr += linesize, y++;
99  }
100 
101  value2 = GET_VALUE;
102  while (value2 == value && run < 256 && y < avctx->height) {
103  x++;
104  run++;
105  if (x >= alen) {
106  x = 0;
107  ptr += linesize, y++;
108  }
109  value2 = GET_VALUE;
110  }
111 
112  if (run > 2 || value == RLE_TRIGGER) {
113  bytestream2_put_byteu(&s->p, RLE_TRIGGER);
114  bytestream2_put_byteu(&s->p, run - 1);
115  if (run > 1)
116  bytestream2_put_byteu(&s->p, value);
117  } else if (run == 1) {
118  bytestream2_put_byteu(&s->p, value);
119  } else
120  bytestream2_put_be16u(&s->p, (value << 8) | value);
121  }
122 
123  // update data length for header
124  s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;
125  } else {
126  for (y = 0; y < avctx->height; y++) {
127  bytestream2_put_buffer(&s->p, ptr, len);
128  if (len < alen)
129  bytestream2_put_byteu(&s->p, 0);
130  ptr += linesize;
131  }
132  }
133 }
134 
136 {
137  SUNRASTContext *s = avctx->priv_data;
138 
139  switch (avctx->coder_type) {
140  case FF_CODER_TYPE_RLE:
141  s->type = RT_BYTE_ENCODED;
142  break;
143  case FF_CODER_TYPE_RAW:
144  s->type = RT_STANDARD;
145  break;
146  default:
147  av_log(avctx, AV_LOG_ERROR, "invalid coder_type\n");
148  return AVERROR(EINVAL);
149  }
150 
151  s->maptype = RMT_NONE;
152  s->maplength = 0;
153 
154  switch (avctx->pix_fmt) {
156  s->depth = 1;
157  break;
158  case AV_PIX_FMT_PAL8 :
159  s->maptype = RMT_EQUAL_RGB;
160  s->maplength = 3 * 256;
161  /* fall-through */
162  case AV_PIX_FMT_GRAY8:
163  s->depth = 8;
164  break;
165  case AV_PIX_FMT_BGR24:
166  s->depth = 24;
167  break;
168  default:
169  return AVERROR_BUG;
170  }
171  s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
172  s->size = 32 + s->maplength +
173  s->length * (s->type == RT_BYTE_ENCODED ? 2 : 1);
174 
175  return 0;
176 }
177 
178 static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
179  const AVFrame *frame, int *got_packet_ptr)
180 {
181  SUNRASTContext *s = avctx->priv_data;
182  int ret;
183 
184  if ((ret = ff_alloc_packet2(avctx, avpkt, s->size, 0)) < 0)
185  return ret;
186 
187  bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
189  sunrast_image_write_image(avctx, frame->data[0],
190  (const uint32_t *)frame->data[1],
191  frame->linesize[0]);
192  // update data length in header after RLE
193  if (s->type == RT_BYTE_ENCODED)
194  AV_WB32(&avpkt->data[16], s->length);
195 
196  *got_packet_ptr = 1;
197  avpkt->flags |= AV_PKT_FLAG_KEY;
198  avpkt->size = bytestream2_tell_p(&s->p);
199  return 0;
200 }
201 
203  { "coder", "rle" },
204  { NULL },
205 };
206 
208  .name = "sunrast",
209  .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
210  .type = AVMEDIA_TYPE_VIDEO,
211  .id = AV_CODEC_ID_SUNRAST,
212  .priv_data_size = sizeof(SUNRASTContext),
214  .encode2 = sunrast_encode_frame,
215  .defaults = sunrast_defaults,
216  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24,
220  AV_PIX_FMT_NONE },
221 };
#define RMT_EQUAL_RGB
Definition: sunrast.h:28
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
static const AVCodecDefault sunrast_defaults[]
Definition: sunrastenc.c:202
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define FF_CODER_TYPE_RAW
Definition: avcodec.h:2647
int size
Definition: avcodec.h:1424
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:143
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1722
int length
length (bytes) of image
Definition: sunrastenc.c:30
uint8_t run
Definition: svq3.c:149
AVCodec.
Definition: avcodec.h:3472
static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: sunrastenc.c:178
int depth
depth of pixel
Definition: sunrastenc.c:29
#define FFALIGN(x, a)
Definition: common.h:86
uint8_t
#define av_cold
Definition: attributes.h:74
8 bit with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:74
PutByteContext p
Definition: sunrastenc.c:28
static AVFrame * frame
int coder_type
coder type
Definition: avcodec.h:2657
uint8_t * data
Definition: avcodec.h:1423
#define RT_STANDARD
Definition: sunrast.h:34
#define FF_CODER_TYPE_RLE
Definition: avcodec.h:2648
int maplength
length (bytes) of colormap
Definition: sunrastenc.c:33
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1469
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#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:175
int maptype
type of colormap
Definition: sunrastenc.c:32
const char * name
Name of the codec implementation.
Definition: avcodec.h:3479
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
Definition: bytestream.h:193
Libavcodec external API header.
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1429
#define RLE_TRIGGER
Definition: sunrast.h:39
static av_always_inline void bytestream2_skip_p(PutByteContext *p, unsigned int size)
Definition: bytestream.h:176
float y
int width
picture width / height.
Definition: avcodec.h:1681
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
int type
type of file
Definition: sunrastenc.c:31
#define RMT_NONE
Definition: sunrast.h:27
AVCodec ff_sunrast_encoder
Definition: sunrastenc.c:207
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:66
static av_always_inline unsigned int bytestream2_put_buffer(PutByteContext *p, const uint8_t *src, unsigned int size)
Definition: bytestream.h:282
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
main external API structure.
Definition: avcodec.h:1502
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
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
static void sunrast_image_write_header(AVCodecContext *avctx)
Definition: sunrastenc.c:37
static av_cold int sunrast_encode_init(AVCodecContext *avctx)
Definition: sunrastenc.c:135
#define RT_BYTE_ENCODED
Definition: sunrast.h:38
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1782
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:209
uint8_t pixel
Definition: tiny_ssim.c:42
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
Y , 8bpp.
Definition: pixfmt.h:71
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:72
void * priv_data
Definition: avcodec.h:1544
int pixels
Definition: avisynth_c.h:298
int len
#define RAS_MAGIC
Definition: sunrast.h:25
static const AVCodecDefault defaults[]
Definition: dcaenc.c:954
#define GET_VALUE
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
This structure stores compressed data.
Definition: avcodec.h:1400