1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
/*
* Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image encoder
* Copyright (c) 2012 Aneesh Dogra (lionaneesh) <lionaneesh@gmail.com>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/opt.h"
#include "avcodec.h"
#include "bytestream.h"
#include "codec_internal.h"
#include "encode.h"
#include "sunrast.h"
typedef struct SUNRASTContext {
AVClass *class;
PutByteContext p;
int depth; ///< depth of pixel
int length; ///< length (bytes) of image
int type; ///< type of file
int maptype; ///< type of colormap
int maplength; ///< length (bytes) of colormap
int size;
} SUNRASTContext;
static void sunrast_image_write_header(AVCodecContext *avctx)
{
SUNRASTContext *s = avctx->priv_data;
bytestream2_put_be32u(&s->p, RAS_MAGIC);
bytestream2_put_be32u(&s->p, avctx->width);
bytestream2_put_be32u(&s->p, avctx->height);
bytestream2_put_be32u(&s->p, s->depth);
bytestream2_put_be32u(&s->p, s->length);
bytestream2_put_be32u(&s->p, s->type);
bytestream2_put_be32u(&s->p, s->maptype);
bytestream2_put_be32u(&s->p, s->maplength);
}
static void sunrast_image_write_image(AVCodecContext *avctx,
const uint8_t *pixels,
const uint32_t *palette_data,
int linesize)
{
SUNRASTContext *s = avctx->priv_data;
const uint8_t *ptr;
int len, alen, x, y;
if (s->maplength) { // palettized
PutByteContext pb_r, pb_g;
int len = s->maplength / 3;
pb_r = s->p;
bytestream2_skip_p(&s->p, len);
pb_g = s->p;
bytestream2_skip_p(&s->p, len);
for (x = 0; x < len; x++) {
uint32_t pixel = palette_data[x];
bytestream2_put_byteu(&pb_r, (pixel >> 16) & 0xFF);
bytestream2_put_byteu(&pb_g, (pixel >> 8) & 0xFF);
bytestream2_put_byteu(&s->p, pixel & 0xFF);
}
}
len = (s->depth * avctx->width + 7) >> 3;
alen = len + (len & 1);
ptr = pixels;
if (s->type == RT_BYTE_ENCODED) {
uint8_t value, value2;
int run;
ptr = pixels;
#define GET_VALUE y >= avctx->height ? 0 : x >= len ? ptr[len-1] : ptr[x]
x = 0, y = 0;
value2 = GET_VALUE;
while (y < avctx->height) {
run = 1;
value = value2;
x++;
if (x >= alen) {
x = 0;
ptr += linesize, y++;
}
value2 = GET_VALUE;
while (value2 == value && run < 256 && y < avctx->height) {
x++;
run++;
if (x >= alen) {
x = 0;
ptr += linesize, y++;
}
value2 = GET_VALUE;
}
if (run > 2 || value == RLE_TRIGGER) {
bytestream2_put_byteu(&s->p, RLE_TRIGGER);
bytestream2_put_byteu(&s->p, run - 1);
if (run > 1)
bytestream2_put_byteu(&s->p, value);
} else if (run == 1) {
bytestream2_put_byteu(&s->p, value);
} else
bytestream2_put_be16u(&s->p, (value << 8) | value);
}
// update data length for header
s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;
} else {
for (y = 0; y < avctx->height; y++) {
bytestream2_put_buffer(&s->p, ptr, len);
if (len < alen)
bytestream2_put_byteu(&s->p, 0);
ptr += linesize;
}
}
}
static av_cold int sunrast_encode_init(AVCodecContext *avctx)
{
SUNRASTContext *s = avctx->priv_data;
// adjust boolean option to RT equivalent
s->type++;
s->maptype = RMT_NONE;
s->maplength = 0;
switch (avctx->pix_fmt) {
case AV_PIX_FMT_MONOWHITE:
s->depth = 1;
break;
case AV_PIX_FMT_PAL8 :
s->maptype = RMT_EQUAL_RGB;
s->maplength = 3 * 256;
/* fall-through */
case AV_PIX_FMT_GRAY8:
s->depth = 8;
break;
case AV_PIX_FMT_BGR24:
s->depth = 24;
break;
default:
return AVERROR_BUG;
}
s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
s->size = 32 + s->maplength + s->length * s->type;
return 0;
}
static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
const AVFrame *frame, int *got_packet_ptr)
{
SUNRASTContext *s = avctx->priv_data;
int ret;
if ((ret = ff_alloc_packet(avctx, avpkt, s->size)) < 0)
return ret;
bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
sunrast_image_write_header(avctx);
sunrast_image_write_image(avctx, frame->data[0],
(const uint32_t *)frame->data[1],
frame->linesize[0]);
// update data length in header after RLE
if (s->type == RT_BYTE_ENCODED)
AV_WB32(&avpkt->data[16], s->length);
*got_packet_ptr = 1;
avpkt->size = bytestream2_tell_p(&s->p);
return 0;
}
#define OFFSET(x) offsetof(SUNRASTContext, x)
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
static const AVOption options[] = {
{ "rle", "Use run-length compression", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
{ NULL },
};
static const AVClass sunrast_class = {
.class_name = "sunrast",
.option = options,
.version = LIBAVUTIL_VERSION_INT,
};
const FFCodec ff_sunrast_encoder = {
.p.name = "sunrast",
CODEC_LONG_NAME("Sun Rasterfile image"),
.p.type = AVMEDIA_TYPE_VIDEO,
.p.id = AV_CODEC_ID_SUNRAST,
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
.priv_data_size = sizeof(SUNRASTContext),
.init = sunrast_encode_init,
FF_CODEC_ENCODE_CB(sunrast_encode_frame),
.p.priv_class = &sunrast_class,
.p.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24,
AV_PIX_FMT_PAL8,
AV_PIX_FMT_GRAY8,
AV_PIX_FMT_MONOWHITE,
AV_PIX_FMT_NONE },
};
|