]> git.sesse.net Git - ffmpeg/blob - libavcodec/sunrast.c
dnxhdenc: switch to encode2.
[ffmpeg] / libavcodec / sunrast.c
1 /*
2  * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image decoder
3  * Copyright (c) 2007, 2008 Ivo van Poorten
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/intreadwrite.h"
23 #include "libavutil/imgutils.h"
24 #include "avcodec.h"
25
26 #define RAS_MAGIC 0x59a66a95
27
28 /* The Old and Standard format types indicate that the image data is
29  * uncompressed. There is no difference between the two formats. */
30 #define RT_OLD          0
31 #define RT_STANDARD     1
32
33 /* The Byte-Encoded format type indicates that the image data is compressed
34  * using a run-length encoding scheme. */
35 #define RT_BYTE_ENCODED 2
36
37 /* The RGB format type indicates that the image is uncompressed with reverse
38  * component order from Old and Standard (RGB vs BGR). */
39 #define RT_FORMAT_RGB   3
40
41 /* The TIFF and IFF format types indicate that the raster file was originally
42  * converted from either of these file formats. We do not have any samples or
43  * documentation of the format details. */
44 #define RT_FORMAT_TIFF  4
45 #define RT_FORMAT_IFF   5
46
47 /* The Experimental format type is implementation-specific and is generally an
48  * indication that the image file does not conform to the Sun Raster file
49  * format specification. */
50 #define RT_EXPERIMENTAL 0xffff
51
52 typedef struct SUNRASTContext {
53     AVFrame picture;
54 } SUNRASTContext;
55
56 static av_cold int sunrast_init(AVCodecContext *avctx) {
57     SUNRASTContext *s = avctx->priv_data;
58
59     avcodec_get_frame_defaults(&s->picture);
60     avctx->coded_frame = &s->picture;
61
62     return 0;
63 }
64
65 static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
66                                 int *data_size, AVPacket *avpkt) {
67     const uint8_t *buf       = avpkt->data;
68     const uint8_t *buf_end   = avpkt->data + avpkt->size;
69     SUNRASTContext * const s = avctx->priv_data;
70     AVFrame *picture         = data;
71     AVFrame * const p        = &s->picture;
72     unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
73     uint8_t *ptr;
74     const uint8_t *bufstart = buf;
75     int ret;
76
77     if (avpkt->size < 32)
78         return AVERROR_INVALIDDATA;
79
80     if (AV_RB32(buf) != RAS_MAGIC) {
81         av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
82         return AVERROR_INVALIDDATA;
83     }
84
85     w         = AV_RB32(buf + 4);
86     h         = AV_RB32(buf + 8);
87     depth     = AV_RB32(buf + 12);
88     type      = AV_RB32(buf + 20);
89     maptype   = AV_RB32(buf + 24);
90     maplength = AV_RB32(buf + 28);
91     buf      += 32;
92
93     if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF || type == RT_EXPERIMENTAL) {
94         av_log_ask_for_sample(avctx, "unsupported (compression) type\n");
95         return AVERROR_PATCHWELCOME;
96     }
97     if (type > RT_FORMAT_IFF) {
98         av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
99         return AVERROR_INVALIDDATA;
100     }
101     if (av_image_check_size(w, h, 0, avctx)) {
102         av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
103         return AVERROR_INVALIDDATA;
104     }
105     if (maptype & ~1) {
106         av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
107         return AVERROR_INVALIDDATA;
108     }
109
110
111     switch (depth) {
112         case 1:
113             avctx->pix_fmt = PIX_FMT_MONOWHITE;
114             break;
115         case 8:
116             avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_GRAY8;
117             break;
118         case 24:
119             avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
120             break;
121         default:
122             av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
123             return AVERROR_INVALIDDATA;
124     }
125
126     if (p->data[0])
127         avctx->release_buffer(avctx, p);
128
129     if (w != avctx->width || h != avctx->height)
130         avcodec_set_dimensions(avctx, w, h);
131     if ((ret = avctx->get_buffer(avctx, p)) < 0) {
132         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
133         return ret;
134     }
135
136     p->pict_type = AV_PICTURE_TYPE_I;
137
138     if (buf_end - buf < maplength)
139         return AVERROR_INVALIDDATA;
140
141     if (depth != 8 && maplength) {
142         av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
143
144     } else if (maplength) {
145         unsigned int len = maplength / 3;
146
147         if (maplength % 3 || maplength > 768) {
148             av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
149             return AVERROR_INVALIDDATA;
150         }
151
152         ptr = p->data[1];
153         for (x = 0; x < len; x++, ptr += 4)
154             *(uint32_t *)ptr = (buf[x] << 16) + (buf[len + x] << 8) + buf[len + len + x];
155     }
156
157     buf += maplength;
158
159     ptr    = p->data[0];
160     stride = p->linesize[0];
161
162     /* scanlines are aligned on 16 bit boundaries */
163     len  = (depth * w + 7) >> 3;
164     alen = len + (len & 1);
165
166     if (type == RT_BYTE_ENCODED) {
167         int value, run;
168         uint8_t *end = ptr + h * stride;
169
170         x = 0;
171         while (ptr != end && buf < buf_end) {
172             run = 1;
173             if (buf_end - buf < 1)
174                 return AVERROR_INVALIDDATA;
175
176             if ((value = *buf++) == 0x80) {
177                 run = *buf++ + 1;
178                 if (run != 1)
179                     value = *buf++;
180             }
181             while (run--) {
182                 if (x < len)
183                     ptr[x] = value;
184                 if (++x >= alen) {
185                     x = 0;
186                     ptr += stride;
187                     if (ptr == end)
188                         break;
189                 }
190             }
191         }
192     } else {
193         for (y = 0; y < h; y++) {
194             if (buf_end - buf < len)
195                 break;
196             memcpy(ptr, buf, len);
197             ptr += stride;
198             buf += alen;
199         }
200     }
201
202     *picture   = s->picture;
203     *data_size = sizeof(AVFrame);
204
205     return buf - bufstart;
206 }
207
208 static av_cold int sunrast_end(AVCodecContext *avctx) {
209     SUNRASTContext *s = avctx->priv_data;
210
211     if(s->picture.data[0])
212         avctx->release_buffer(avctx, &s->picture);
213
214     return 0;
215 }
216
217 AVCodec ff_sunrast_decoder = {
218     .name           = "sunrast",
219     .type           = AVMEDIA_TYPE_VIDEO,
220     .id             = CODEC_ID_SUNRAST,
221     .priv_data_size = sizeof(SUNRASTContext),
222     .init           = sunrast_init,
223     .close          = sunrast_end,
224     .decode         = sunrast_decode_frame,
225     .capabilities   = CODEC_CAP_DR1,
226     .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
227 };