Browse code

avcodec: add a Sun Rasterfile encoder

Signed-off-by: Justin Ruggles <justin.ruggles@gmail.com>

Aneesh Dogra authored on 2012/02/17 06:08:51
Showing 7 changed files
... ...
@@ -8,6 +8,7 @@ version <next>:
8 8
 - ISMV (Smooth Streaming) muxer
9 9
 - CDXL demuxer and decoder
10 10
 - Apple ProRes encoder
11
+- Sun Rasterfile Encoder
11 12
 
12 13
 
13 14
 version 0.8:
... ...
@@ -375,7 +375,7 @@ following image formats are supported:
375 375
     @tab V.Flash PTX format
376 376
 @item SGI          @tab X @tab X
377 377
     @tab SGI RGB image format
378
-@item Sun Rasterfile  @tab   @tab X
378
+@item Sun Rasterfile  @tab X @tab X
379 379
     @tab Sun RAS image format
380 380
 @item TIFF         @tab X @tab X
381 381
     @tab YUV, JPEG and some extension is not supported yet.
... ...
@@ -356,6 +356,7 @@ OBJS-$(CONFIG_SOL_DPCM_DECODER)        += dpcm.o
356 356
 OBJS-$(CONFIG_SP5X_DECODER)            += sp5xdec.o mjpegdec.o mjpeg.o
357 357
 OBJS-$(CONFIG_SRT_DECODER)             += srtdec.o ass.o
358 358
 OBJS-$(CONFIG_SUNRAST_DECODER)         += sunrast.o
359
+OBJS-$(CONFIG_SUNRAST_ENCODER)         += sunrastenc.o
359 360
 OBJS-$(CONFIG_SVQ1_DECODER)            += svq1dec.o svq1.o h263.o \
360 361
                                           mpegvideo.o error_resilience.o
361 362
 OBJS-$(CONFIG_SVQ1_ENCODER)            += svq1enc.o svq1.o    \
... ...
@@ -189,7 +189,7 @@ void avcodec_register_all(void)
189 189
     REGISTER_DECODER (SMC, smc);
190 190
     REGISTER_ENCDEC  (SNOW, snow);
191 191
     REGISTER_DECODER (SP5X, sp5x);
192
-    REGISTER_DECODER (SUNRAST, sunrast);
192
+    REGISTER_ENCDEC  (SUNRAST, sunrast);
193 193
     REGISTER_ENCDEC  (SVQ1, svq1);
194 194
     REGISTER_DECODER (SVQ3, svq3);
195 195
     REGISTER_ENCDEC  (TARGA, targa);
... ...
@@ -24,6 +24,10 @@
24 24
 
25 25
 #define RAS_MAGIC 0x59a66a95
26 26
 
27
+#define RMT_NONE      0
28
+#define RMT_EQUAL_RGB 1
29
+#define RMT_RAW       2 ///< the data layout of this map type is unknown
30
+
27 31
 /* The Old and Standard format types indicate that the image data is
28 32
  * uncompressed. There is no difference between the two formats. */
29 33
 #define RT_OLD          0
... ...
@@ -32,6 +36,7 @@
32 32
 /* The Byte-Encoded format type indicates that the image data is compressed
33 33
  * using a run-length encoding scheme. */
34 34
 #define RT_BYTE_ENCODED 2
35
+#define RLE_TRIGGER 0x80
35 36
 
36 37
 /* The RGB format type indicates that the image is uncompressed with reverse
37 38
  * component order from Old and Standard (RGB vs BGR). */
38 39
new file mode 100644
... ...
@@ -0,0 +1,225 @@
0
+/*
1
+ * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image encoder
2
+ * Copyright (c) 2012 Aneesh Dogra (lionaneesh) <lionaneesh@gmail.com>
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * Libav is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "avcodec.h"
22
+#include "bytestream.h"
23
+#include "internal.h"
24
+#include "sunrast.h"
25
+
26
+typedef struct SUNRASTContext {
27
+    AVFrame picture;
28
+    PutByteContext p;
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;
35
+} SUNRASTContext;
36
+
37
+static void sunrast_image_write_header(AVCodecContext *avctx)
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
+
51
+static void sunrast_image_write_image(AVCodecContext *avctx,
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;
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
+        const uint8_t *end = pixels + avctx->height * linesize;
86
+
87
+        ptr = pixels;
88
+
89
+#define GET_VALUE ptr >= end ? 0 : x >= len ? ptr[len-1] : ptr[x]
90
+
91
+        x = 0;
92
+        value2 = GET_VALUE;
93
+        while (ptr < end) {
94
+            run = 1;
95
+            value = value2;
96
+            x++;
97
+            if (x >= alen) {
98
+                x = 0;
99
+                ptr += linesize;
100
+            }
101
+
102
+            value2 = GET_VALUE;
103
+            while (value2 == value && run < 256 && ptr < end) {
104
+                x++;
105
+                run++;
106
+                if (x >= alen) {
107
+                    x = 0;
108
+                    ptr += linesize;
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
+        int y;
128
+        for (y = 0; y < avctx->height; y++) {
129
+            bytestream2_put_buffer(&s->p, ptr, len);
130
+            if (len < alen)
131
+                bytestream2_put_byteu(&s->p, 0);
132
+            ptr += linesize;
133
+        }
134
+    }
135
+}
136
+
137
+static av_cold int sunrast_encode_init(AVCodecContext *avctx)
138
+{
139
+    SUNRASTContext *s = avctx->priv_data;
140
+
141
+    switch (avctx->coder_type) {
142
+    case FF_CODER_TYPE_RLE:
143
+        s->type = RT_BYTE_ENCODED;
144
+        break;
145
+    case FF_CODER_TYPE_RAW:
146
+        s->type = RT_STANDARD;
147
+        break;
148
+    default:
149
+        av_log(avctx, AV_LOG_ERROR, "invalid coder_type\n");
150
+        return AVERROR(EINVAL);
151
+    }
152
+
153
+    avctx->coded_frame            = &s->picture;
154
+    avctx->coded_frame->key_frame = 1;
155
+    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
156
+    s->maptype                    = RMT_NONE;
157
+    s->maplength                  = 0;
158
+
159
+    switch (avctx->pix_fmt) {
160
+    case PIX_FMT_MONOWHITE:
161
+        s->depth = 1;
162
+        break;
163
+    case PIX_FMT_PAL8 :
164
+        s->maptype   = RMT_EQUAL_RGB;
165
+        s->maplength = 3 * 256;
166
+    case PIX_FMT_GRAY8:
167
+        s->depth = 8;
168
+        break;
169
+    case PIX_FMT_BGR24:
170
+        s->depth = 24;
171
+        break;
172
+    default:
173
+        return AVERROR_BUG;
174
+    }
175
+    s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
176
+    s->size   = 32 + s->maplength +
177
+                s->length * (s->type == RT_BYTE_ENCODED ? 2 : 1);
178
+
179
+    return 0;
180
+}
181
+
182
+static int sunrast_encode_frame(AVCodecContext *avctx,  AVPacket *avpkt,
183
+                                const AVFrame *frame, int *got_packet_ptr)
184
+{
185
+    SUNRASTContext *s = avctx->priv_data;
186
+    int ret;
187
+
188
+    if ((ret = ff_alloc_packet(avpkt, s->size)) < 0)
189
+        return ret;
190
+
191
+    bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
192
+    sunrast_image_write_header(avctx);
193
+    sunrast_image_write_image(avctx, frame->data[0],
194
+                              (const uint32_t *)frame->data[1],
195
+                              frame->linesize[0]);
196
+    // update data length in header after RLE
197
+    if (s->type == RT_BYTE_ENCODED)
198
+        AV_WB32(&avpkt->data[16], s->length);
199
+
200
+    *got_packet_ptr = 1;
201
+    avpkt->size = bytestream2_tell_p(&s->p);
202
+    return 0;
203
+}
204
+
205
+static const AVCodecDefault sunrast_defaults[] = {
206
+     { "coder", "rle" },
207
+     { NULL },
208
+};
209
+
210
+AVCodec ff_sunrast_encoder = {
211
+    .name           = "sunrast",
212
+    .type           = AVMEDIA_TYPE_VIDEO,
213
+    .id             = CODEC_ID_SUNRAST,
214
+    .priv_data_size = sizeof(SUNRASTContext),
215
+    .init           = sunrast_encode_init,
216
+    .encode2        = sunrast_encode_frame,
217
+    .defaults       = sunrast_defaults,
218
+    .pix_fmts       = (const enum PixelFormat[]){ PIX_FMT_BGR24,
219
+                                                  PIX_FMT_PAL8,
220
+                                                  PIX_FMT_GRAY8,
221
+                                                  PIX_FMT_MONOWHITE,
222
+                                                  PIX_FMT_NONE },
223
+    .long_name      = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
224
+};
... ...
@@ -491,7 +491,8 @@ AVOutputFormat ff_image2_muxer = {
491 491
     .name           = "image2",
492 492
     .long_name      = NULL_IF_CONFIG_SMALL("image2 sequence"),
493 493
     .extensions     = "bmp,dpx,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
494
-                      "ppm,sgi,tga,tif,tiff,jp2,xwd",
494
+                      "ppm,sgi,tga,tif,tiff,jp2,xwd,sun,ras,rs,im1,im8,im24,"
495
+                      "sunras",
495 496
     .priv_data_size = sizeof(VideoData),
496 497
     .video_codec    = CODEC_ID_MJPEG,
497 498
     .write_header   = write_header,