]> git.sesse.net Git - ffmpeg/blob - libavcodec/sunrastenc.c
Merge commit 'be00ec832c519427cd92218abac77dafdc1d5487'
[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 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 "libavutil/opt.h"
23
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27 #include "sunrast.h"
28
29 typedef struct SUNRASTContext {
30     PutByteContext p;
31     int depth;      ///< depth of pixel
32     int length;     ///< length (bytes) of image
33     int type;       ///< type of file
34     int maptype;    ///< type of colormap
35     int maplength;  ///< length (bytes) of colormap
36     int size;
37 } SUNRASTContext;
38
39 static void sunrast_image_write_header(AVCodecContext *avctx)
40 {
41     SUNRASTContext *s = avctx->priv_data;
42
43     bytestream2_put_be32u(&s->p, RAS_MAGIC);
44     bytestream2_put_be32u(&s->p, avctx->width);
45     bytestream2_put_be32u(&s->p, avctx->height);
46     bytestream2_put_be32u(&s->p, s->depth);
47     bytestream2_put_be32u(&s->p, s->length);
48     bytestream2_put_be32u(&s->p, s->type);
49     bytestream2_put_be32u(&s->p, s->maptype);
50     bytestream2_put_be32u(&s->p, s->maplength);
51 }
52
53 static void sunrast_image_write_image(AVCodecContext *avctx,
54                                       const uint8_t *pixels,
55                                       const uint32_t *palette_data,
56                                       int linesize)
57 {
58     SUNRASTContext *s = avctx->priv_data;
59     const uint8_t *ptr;
60     int len, alen, x, y;
61
62     if (s->maplength) {     // palettized
63         PutByteContext pb_r, pb_g;
64         int len = s->maplength / 3;
65
66         pb_r = s->p;
67         bytestream2_skip_p(&s->p, len);
68         pb_g = s->p;
69         bytestream2_skip_p(&s->p, len);
70
71         for (x = 0; x < len; x++) {
72             uint32_t pixel = palette_data[x];
73
74             bytestream2_put_byteu(&pb_r, (pixel >> 16) & 0xFF);
75             bytestream2_put_byteu(&pb_g, (pixel >> 8)  & 0xFF);
76             bytestream2_put_byteu(&s->p,  pixel        & 0xFF);
77         }
78     }
79
80     len  = (s->depth * avctx->width + 7) >> 3;
81     alen = len + (len & 1);
82     ptr  = pixels;
83
84      if (s->type == RT_BYTE_ENCODED) {
85         uint8_t value, value2;
86         int run;
87
88         ptr = pixels;
89
90 #define GET_VALUE y >= avctx->height ? 0 : x >= len ? ptr[len-1] : ptr[x]
91
92         x = 0, y = 0;
93         value2 = GET_VALUE;
94         while (y < avctx->height) {
95             run = 1;
96             value = value2;
97             x++;
98             if (x >= alen) {
99                 x = 0;
100                 ptr += linesize, y++;
101             }
102
103             value2 = GET_VALUE;
104             while (value2 == value && run < 256 && y < avctx->height) {
105                 x++;
106                 run++;
107                 if (x >= alen) {
108                     x = 0;
109                     ptr += linesize, y++;
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         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 #if FF_API_CODER_TYPE
142 FF_DISABLE_DEPRECATION_WARNINGS
143     switch (avctx->coder_type) {
144     case FF_CODER_TYPE_RLE:
145         s->type = RT_BYTE_ENCODED;
146         break;
147     case FF_CODER_TYPE_RAW:
148         s->type = RT_STANDARD;
149         break;
150     default:
151         av_log(avctx, AV_LOG_ERROR, "invalid coder_type\n");
152         return AVERROR(EINVAL);
153     }
154 FF_ENABLE_DEPRECATION_WARNINGS
155     if (s->type != RT_BYTE_ENCODED && s->type != RT_STANDARD)
156 #endif
157     // adjust boolean option to RT equivalent
158     s->type++;
159
160     s->maptype                    = RMT_NONE;
161     s->maplength                  = 0;
162
163     switch (avctx->pix_fmt) {
164     case AV_PIX_FMT_MONOWHITE:
165         s->depth = 1;
166         break;
167     case AV_PIX_FMT_PAL8 :
168         s->maptype   = RMT_EQUAL_RGB;
169         s->maplength = 3 * 256;
170         /* fall-through */
171     case AV_PIX_FMT_GRAY8:
172         s->depth = 8;
173         break;
174     case AV_PIX_FMT_BGR24:
175         s->depth = 24;
176         break;
177     default:
178         return AVERROR_BUG;
179     }
180     s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
181     s->size   = 32 + s->maplength + s->length * s->type;
182
183     return 0;
184 }
185
186 static int sunrast_encode_frame(AVCodecContext *avctx,  AVPacket *avpkt,
187                                 const AVFrame *frame, int *got_packet_ptr)
188 {
189     SUNRASTContext *s = avctx->priv_data;
190     int ret;
191
192     if ((ret = ff_alloc_packet2(avctx, avpkt, s->size, 0)) < 0)
193         return ret;
194
195     bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
196     sunrast_image_write_header(avctx);
197     sunrast_image_write_image(avctx, frame->data[0],
198                               (const uint32_t *)frame->data[1],
199                               frame->linesize[0]);
200     // update data length in header after RLE
201     if (s->type == RT_BYTE_ENCODED)
202         AV_WB32(&avpkt->data[16], s->length);
203
204     *got_packet_ptr = 1;
205     avpkt->flags |= AV_PKT_FLAG_KEY;
206     avpkt->size = bytestream2_tell_p(&s->p);
207     return 0;
208 }
209
210 #define OFFSET(x) offsetof(SUNRASTContext, x)
211 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
212 static const AVOption options[] = {
213     { "rle", "Use run-length compression", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
214
215     { NULL },
216 };
217
218 static const AVClass utvideo_class = {
219     .class_name = "sunrast",
220     .item_name  = av_default_item_name,
221     .option     = options,
222     .version    = LIBAVUTIL_VERSION_INT,
223 };
224
225 #if FF_API_CODER_TYPE
226 static const AVCodecDefault sunrast_defaults[] = {
227      { "coder", "rle" },
228      { NULL },
229 };
230 #endif
231
232 AVCodec ff_sunrast_encoder = {
233     .name           = "sunrast",
234     .long_name      = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
235     .type           = AVMEDIA_TYPE_VIDEO,
236     .id             = AV_CODEC_ID_SUNRAST,
237     .priv_data_size = sizeof(SUNRASTContext),
238     .init           = sunrast_encode_init,
239     .encode2        = sunrast_encode_frame,
240 #if FF_API_CODER_TYPE
241     .defaults       = sunrast_defaults,
242 #endif
243     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24,
244                                                   AV_PIX_FMT_PAL8,
245                                                   AV_PIX_FMT_GRAY8,
246                                                   AV_PIX_FMT_MONOWHITE,
247                                                   AV_PIX_FMT_NONE },
248 };