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