]> git.sesse.net Git - ffmpeg/blob - libavcodec/sunrastenc.c
lavc: Deprecate coder_type and its symbols
[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 "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;
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         const uint8_t *start = linesize < 0 ? pixels + (avctx->height - 1) * linesize
88                                             : pixels;
89         const uint8_t *end   = linesize < 0 ? pixels - linesize
90                                             : pixels + avctx->height * linesize;
91
92         ptr = pixels;
93
94 #define GET_VALUE ptr >= end || ptr < start ? 0 : x >= len ? ptr[len-1] : ptr[x]
95
96         x = 0;
97         value2 = GET_VALUE;
98         while (ptr < end && ptr >= start) {
99             run = 1;
100             value = value2;
101             x++;
102             if (x >= alen) {
103                 x = 0;
104                 ptr += linesize;
105             }
106
107             value2 = GET_VALUE;
108             while (value2 == value && run < 256 && ptr < end && ptr >= start) {
109                 x++;
110                 run++;
111                 if (x >= alen) {
112                     x = 0;
113                     ptr += linesize;
114                 }
115                 value2 = GET_VALUE;
116             }
117
118             if (run > 2 || value == RLE_TRIGGER) {
119                 bytestream2_put_byteu(&s->p, RLE_TRIGGER);
120                 bytestream2_put_byteu(&s->p, run - 1);
121                 if (run > 1)
122                     bytestream2_put_byteu(&s->p, value);
123             } else if (run == 1) {
124                 bytestream2_put_byteu(&s->p, value);
125             } else
126                 bytestream2_put_be16u(&s->p, (value << 8) | value);
127         }
128
129         // update data length for header
130         s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;
131     } else {
132         int y;
133         for (y = 0; y < avctx->height; y++) {
134             bytestream2_put_buffer(&s->p, ptr, len);
135             if (len < alen)
136                 bytestream2_put_byteu(&s->p, 0);
137             ptr += linesize;
138         }
139     }
140 }
141
142 static av_cold int sunrast_encode_init(AVCodecContext *avctx)
143 {
144     SUNRASTContext *s = avctx->priv_data;
145
146 #if FF_API_CODER_TYPE
147 FF_DISABLE_DEPRECATION_WARNINGS
148     switch (avctx->coder_type) {
149     case FF_CODER_TYPE_RLE:
150         s->type = RT_BYTE_ENCODED;
151         break;
152     case FF_CODER_TYPE_RAW:
153         s->type = RT_STANDARD;
154         break;
155     default:
156         av_log(avctx, AV_LOG_ERROR, "invalid coder_type\n");
157         return AVERROR(EINVAL);
158     }
159 FF_ENABLE_DEPRECATION_WARNINGS
160     if (s->type != RT_BYTE_ENCODED && s->type != RT_STANDARD)
161 #endif
162     // adjust boolean option to RT equivalent
163     s->type++;
164
165 #if FF_API_CODED_FRAME
166 FF_DISABLE_DEPRECATION_WARNINGS
167     avctx->coded_frame->key_frame = 1;
168     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
169 FF_ENABLE_DEPRECATION_WARNINGS
170 #endif
171     s->maptype                    = RMT_NONE;
172     s->maplength                  = 0;
173
174     switch (avctx->pix_fmt) {
175     case AV_PIX_FMT_MONOWHITE:
176         s->depth = 1;
177         break;
178     case AV_PIX_FMT_PAL8 :
179         s->maptype   = RMT_EQUAL_RGB;
180         s->maplength = 3 * 256;
181         /* fall-through */
182     case AV_PIX_FMT_GRAY8:
183         s->depth = 8;
184         break;
185     case AV_PIX_FMT_BGR24:
186         s->depth = 24;
187         break;
188     default:
189         return AVERROR_BUG;
190     }
191     s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
192     s->size   = 32 + s->maplength + s->length * s->type;
193
194     return 0;
195 }
196
197 static int sunrast_encode_frame(AVCodecContext *avctx,  AVPacket *avpkt,
198                                 const AVFrame *frame, int *got_packet_ptr)
199 {
200     SUNRASTContext *s = avctx->priv_data;
201     int ret;
202
203     if ((ret = ff_alloc_packet(avpkt, s->size)) < 0)
204         return ret;
205
206     bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
207     sunrast_image_write_header(avctx);
208     sunrast_image_write_image(avctx, frame->data[0],
209                               (const uint32_t *)frame->data[1],
210                               frame->linesize[0]);
211     // update data length in header after RLE
212     if (s->type == RT_BYTE_ENCODED)
213         AV_WB32(&avpkt->data[16], s->length);
214
215     *got_packet_ptr = 1;
216     avpkt->flags |= AV_PKT_FLAG_KEY;
217     avpkt->size = bytestream2_tell_p(&s->p);
218     return 0;
219 }
220
221 #define OFFSET(x) offsetof(SUNRASTContext, x)
222 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
223 static const AVOption options[] = {
224     { "rle", "Use run-length compression", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
225
226     { NULL },
227 };
228
229 static const AVClass utvideo_class = {
230     .class_name = "sunrast",
231     .item_name  = av_default_item_name,
232     .option     = options,
233     .version    = LIBAVUTIL_VERSION_INT,
234 };
235
236 #if FF_API_CODER_TYPE
237 static const AVCodecDefault sunrast_defaults[] = {
238      { "coder", "rle" },
239      { NULL },
240 };
241 #endif
242
243 AVCodec ff_sunrast_encoder = {
244     .name           = "sunrast",
245     .long_name      = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
246     .type           = AVMEDIA_TYPE_VIDEO,
247     .id             = AV_CODEC_ID_SUNRAST,
248     .priv_data_size = sizeof(SUNRASTContext),
249     .init           = sunrast_encode_init,
250     .encode2        = sunrast_encode_frame,
251 #if FF_API_CODER_TYPE
252     .defaults       = sunrast_defaults,
253 #endif
254     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24,
255                                                   AV_PIX_FMT_PAL8,
256                                                   AV_PIX_FMT_GRAY8,
257                                                   AV_PIX_FMT_MONOWHITE,
258                                                   AV_PIX_FMT_NONE },
259 };