2
* Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image encoder
3
* Copyright (c) 2012 Aneesh Dogra (lionaneesh) <lionaneesh@gmail.com>
5
* This file is part of FFmpeg.
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.
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.
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
23
#include "bytestream.h"
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
37
static void sunrast_image_write_header(AVCodecContext *avctx)
39
SUNRASTContext *s = avctx->priv_data;
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);
51
static void sunrast_image_write_image(AVCodecContext *avctx,
52
const uint8_t *pixels,
53
const uint32_t *palette_data,
56
SUNRASTContext *s = avctx->priv_data;
60
if (s->maplength) { // palettized
61
PutByteContext pb_r, pb_g;
62
int len = s->maplength / 3;
65
bytestream2_skip_p(&s->p, len);
67
bytestream2_skip_p(&s->p, len);
69
for (x = 0; x < len; x++) {
70
uint32_t pixel = palette_data[x];
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);
78
len = (s->depth * avctx->width + 7) >> 3;
79
alen = len + (len & 1);
82
if (s->type == RT_BYTE_ENCODED) {
83
uint8_t value, value2;
88
#define GET_VALUE y >= avctx->height ? 0 : x >= len ? ptr[len-1] : ptr[x]
92
while (y < avctx->height) {
102
while (value2 == value && run < 256 && y < avctx->height) {
107
ptr += linesize, y++;
112
if (run > 2 || value == RLE_TRIGGER) {
113
bytestream2_put_byteu(&s->p, RLE_TRIGGER);
114
bytestream2_put_byteu(&s->p, run - 1);
116
bytestream2_put_byteu(&s->p, value);
117
} else if (run == 1) {
118
bytestream2_put_byteu(&s->p, value);
120
bytestream2_put_be16u(&s->p, (value << 8) | value);
123
// update data length for header
124
s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;
126
for (y = 0; y < avctx->height; y++) {
127
bytestream2_put_buffer(&s->p, ptr, len);
129
bytestream2_put_byteu(&s->p, 0);
135
static av_cold int sunrast_encode_init(AVCodecContext *avctx)
137
SUNRASTContext *s = avctx->priv_data;
139
switch (avctx->coder_type) {
140
case FF_CODER_TYPE_RLE:
141
s->type = RT_BYTE_ENCODED;
143
case FF_CODER_TYPE_RAW:
144
s->type = RT_STANDARD;
147
av_log(avctx, AV_LOG_ERROR, "invalid coder_type\n");
148
return AVERROR(EINVAL);
151
s->maptype = RMT_NONE;
154
switch (avctx->pix_fmt) {
155
case AV_PIX_FMT_MONOWHITE:
158
case AV_PIX_FMT_PAL8 :
159
s->maptype = RMT_EQUAL_RGB;
160
s->maplength = 3 * 256;
162
case AV_PIX_FMT_GRAY8:
165
case AV_PIX_FMT_BGR24:
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);
178
static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
179
const AVFrame *frame, int *got_packet_ptr)
181
SUNRASTContext *s = avctx->priv_data;
184
if ((ret = ff_alloc_packet2(avctx, avpkt, s->size)) < 0)
187
bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
188
sunrast_image_write_header(avctx);
189
sunrast_image_write_image(avctx, frame->data[0],
190
(const uint32_t *)frame->data[1],
192
// update data length in header after RLE
193
if (s->type == RT_BYTE_ENCODED)
194
AV_WB32(&avpkt->data[16], s->length);
197
avpkt->flags |= AV_PKT_FLAG_KEY;
198
avpkt->size = bytestream2_tell_p(&s->p);
202
static av_cold int sunrast_encode_close(AVCodecContext *avctx)
204
av_frame_free(&avctx->coded_frame);
208
static const AVCodecDefault sunrast_defaults[] = {
213
AVCodec ff_sunrast_encoder = {
215
.long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
216
.type = AVMEDIA_TYPE_VIDEO,
217
.id = AV_CODEC_ID_SUNRAST,
218
.priv_data_size = sizeof(SUNRASTContext),
219
.init = sunrast_encode_init,
220
.close = sunrast_encode_close,
221
.encode2 = sunrast_encode_frame,
222
.defaults = sunrast_defaults,
223
.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24,
226
AV_PIX_FMT_MONOWHITE,