]> git.sesse.net Git - ffmpeg/blob - libavcodec/sunrastenc.c
avcodec: add a Sun Rasterfile encoder
[ffmpeg] / libavcodec / sunrastenc.c
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 {
28     AVFrame picture;
29     PutByteContext p;
30     int depth;      ///< depth of pixel
31     int length;     ///< length (bytes) of image
32     int type;       ///< type of file
33     int maptype;    ///< type of colormap
34     int maplength;  ///< length (bytes) of colormap
35     int size;
36 } SUNRASTContext;
37
38 static void sunrast_image_write_header(AVCodecContext *avctx)
39 {
40     SUNRASTContext *s = avctx->priv_data;
41
42     bytestream2_put_be32u(&s->p, RAS_MAGIC);
43     bytestream2_put_be32u(&s->p, avctx->width);
44     bytestream2_put_be32u(&s->p, avctx->height);
45     bytestream2_put_be32u(&s->p, s->depth);
46     bytestream2_put_be32u(&s->p, s->length);
47     bytestream2_put_be32u(&s->p, s->type);
48     bytestream2_put_be32u(&s->p, s->maptype);
49     bytestream2_put_be32u(&s->p, s->maplength);
50 }
51
52 static void sunrast_image_write_image(AVCodecContext *avctx,
53                                       const uint8_t *pixels,
54                                       const uint32_t *palette_data,
55                                       int linesize)
56 {
57     SUNRASTContext *s = avctx->priv_data;
58     const uint8_t *ptr;
59     int len, alen, x;
60
61     if (s->maplength) {     // palettized
62         PutByteContext pb_r, pb_g;
63         int len = s->maplength / 3;
64
65         pb_r = s->p;
66         bytestream2_skip_p(&s->p, len);
67         pb_g = s->p;
68         bytestream2_skip_p(&s->p, len);
69
70         for (x = 0; x < len; x++) {
71             uint32_t pixel = palette_data[x];
72
73             bytestream2_put_byteu(&pb_r, (pixel >> 16) & 0xFF);
74             bytestream2_put_byteu(&pb_g, (pixel >> 8)  & 0xFF);
75             bytestream2_put_byteu(&s->p,  pixel        & 0xFF);
76         }
77     }
78
79     len  = (s->depth * avctx->width + 7) >> 3;
80     alen = len + (len & 1);
81     ptr  = pixels;
82
83      if (s->type == RT_BYTE_ENCODED) {
84         uint8_t value, value2;
85         int run;
86         const uint8_t *end = pixels + avctx->height * linesize;
87
88         ptr = pixels;
89
90 #define GET_VALUE ptr >= end ? 0 : x >= len ? ptr[len-1] : ptr[x]
91
92         x = 0;
93         value2 = GET_VALUE;
94         while (ptr < end) {
95             run = 1;
96             value = value2;
97             x++;
98             if (x >= alen) {
99                 x = 0;
100                 ptr += linesize;
101             }
102
103             value2 = GET_VALUE;
104             while (value2 == value && run < 256 && ptr < end) {
105                 x++;
106                 run++;
107                 if (x >= alen) {
108                     x = 0;
109                     ptr += linesize;
110                 }
111                 value2 = GET_VALUE;
112             }
113
114             if (run > 2 || value == RLE_TRIGGER) {
115                 bytestream2_put_byteu(&s->p, RLE_TRIGGER);
116                 bytestream2_put_byteu(&s->p, run - 1);
117                 if (run > 1)
118                     bytestream2_put_byteu(&s->p, value);
119             } else if (run == 1) {
120                 bytestream2_put_byteu(&s->p, value);
121             } else
122                 bytestream2_put_be16u(&s->p, (value << 8) | value);
123         }
124
125         // update data length for header
126         s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;
127     } else {
128         int y;
129         for (y = 0; y < avctx->height; y++) {
130             bytestream2_put_buffer(&s->p, ptr, len);
131             if (len < alen)
132                 bytestream2_put_byteu(&s->p, 0);
133             ptr += linesize;
134         }
135     }
136 }
137
138 static av_cold int sunrast_encode_init(AVCodecContext *avctx)
139 {
140     SUNRASTContext *s = avctx->priv_data;
141
142     switch (avctx->coder_type) {
143     case FF_CODER_TYPE_RLE:
144         s->type = RT_BYTE_ENCODED;
145         break;
146     case FF_CODER_TYPE_RAW:
147         s->type = RT_STANDARD;
148         break;
149     default:
150         av_log(avctx, AV_LOG_ERROR, "invalid coder_type\n");
151         return AVERROR(EINVAL);
152     }
153
154     avctx->coded_frame            = &s->picture;
155     avctx->coded_frame->key_frame = 1;
156     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
157     s->maptype                    = RMT_NONE;
158     s->maplength                  = 0;
159
160     switch (avctx->pix_fmt) {
161     case PIX_FMT_MONOWHITE:
162         s->depth = 1;
163         break;
164     case PIX_FMT_PAL8 :
165         s->maptype   = RMT_EQUAL_RGB;
166         s->maplength = 3 * 256;
167     case PIX_FMT_GRAY8:
168         s->depth = 8;
169         break;
170     case PIX_FMT_BGR24:
171         s->depth = 24;
172         break;
173     default:
174         return AVERROR_BUG;
175     }
176     s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
177     s->size   = 32 + s->maplength +
178                 s->length * (s->type == RT_BYTE_ENCODED ? 2 : 1);
179
180     return 0;
181 }
182
183 static int sunrast_encode_frame(AVCodecContext *avctx,  AVPacket *avpkt,
184                                 const AVFrame *frame, int *got_packet_ptr)
185 {
186     SUNRASTContext *s = avctx->priv_data;
187     int ret;
188
189     if ((ret = ff_alloc_packet(avpkt, s->size)) < 0)
190         return ret;
191
192     bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
193     sunrast_image_write_header(avctx);
194     sunrast_image_write_image(avctx, frame->data[0],
195                               (const uint32_t *)frame->data[1],
196                               frame->linesize[0]);
197     // update data length in header after RLE
198     if (s->type == RT_BYTE_ENCODED)
199         AV_WB32(&avpkt->data[16], s->length);
200
201     *got_packet_ptr = 1;
202     avpkt->size = bytestream2_tell_p(&s->p);
203     return 0;
204 }
205
206 static const AVCodecDefault sunrast_defaults[] = {
207      { "coder", "rle" },
208      { NULL },
209 };
210
211 AVCodec ff_sunrast_encoder = {
212     .name           = "sunrast",
213     .type           = AVMEDIA_TYPE_VIDEO,
214     .id             = CODEC_ID_SUNRAST,
215     .priv_data_size = sizeof(SUNRASTContext),
216     .init           = sunrast_encode_init,
217     .encode2        = sunrast_encode_frame,
218     .defaults       = sunrast_defaults,
219     .pix_fmts       = (const enum PixelFormat[]){ PIX_FMT_BGR24,
220                                                   PIX_FMT_PAL8,
221                                                   PIX_FMT_GRAY8,
222                                                   PIX_FMT_MONOWHITE,
223                                                   PIX_FMT_NONE },
224     .long_name      = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
225 };