]> git.sesse.net Git - ffmpeg/blob - libavcodec/sunrast.c
dirac: add Comments and references to the standard
[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/common.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/imgutils.h"
25 #include "avcodec.h"
26 #include "sunrast.h"
27
28 typedef struct SUNRASTContext {
29     AVFrame picture;
30 } SUNRASTContext;
31
32 static av_cold int sunrast_init(AVCodecContext *avctx) {
33     SUNRASTContext *s = avctx->priv_data;
34
35     avcodec_get_frame_defaults(&s->picture);
36     avctx->coded_frame = &s->picture;
37
38     return 0;
39 }
40
41 static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
42                                 int *data_size, AVPacket *avpkt) {
43     const uint8_t *buf       = avpkt->data;
44     const uint8_t *buf_end   = avpkt->data + avpkt->size;
45     SUNRASTContext * const s = avctx->priv_data;
46     AVFrame *picture         = data;
47     AVFrame * const p        = &s->picture;
48     unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
49     uint8_t *ptr;
50     const uint8_t *bufstart = buf;
51     int ret;
52
53     if (avpkt->size < 32)
54         return AVERROR_INVALIDDATA;
55
56     if (AV_RB32(buf) != RAS_MAGIC) {
57         av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
58         return AVERROR_INVALIDDATA;
59     }
60
61     w         = AV_RB32(buf + 4);
62     h         = AV_RB32(buf + 8);
63     depth     = AV_RB32(buf + 12);
64     type      = AV_RB32(buf + 20);
65     maptype   = AV_RB32(buf + 24);
66     maplength = AV_RB32(buf + 28);
67     buf      += 32;
68
69     if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF || type == RT_EXPERIMENTAL) {
70         av_log_ask_for_sample(avctx, "unsupported (compression) type\n");
71         return AVERROR_PATCHWELCOME;
72     }
73     if (type > RT_FORMAT_IFF) {
74         av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
75         return AVERROR_INVALIDDATA;
76     }
77     if (av_image_check_size(w, h, 0, avctx)) {
78         av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
79         return AVERROR_INVALIDDATA;
80     }
81     if (maptype == RMT_RAW) {
82         av_log_ask_for_sample(avctx, "unsupported colormap type\n");
83         return AVERROR_PATCHWELCOME;
84     }
85     if (maptype > RMT_RAW) {
86         av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
87         return AVERROR_INVALIDDATA;
88     }
89
90
91     switch (depth) {
92         case 1:
93             avctx->pix_fmt = PIX_FMT_MONOWHITE;
94             break;
95         case 8:
96             avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_GRAY8;
97             break;
98         case 24:
99             avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
100             break;
101         default:
102             av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
103             return AVERROR_INVALIDDATA;
104     }
105
106     if (p->data[0])
107         avctx->release_buffer(avctx, p);
108
109     if (w != avctx->width || h != avctx->height)
110         avcodec_set_dimensions(avctx, w, h);
111     if ((ret = avctx->get_buffer(avctx, p)) < 0) {
112         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
113         return ret;
114     }
115
116     p->pict_type = AV_PICTURE_TYPE_I;
117
118     if (buf_end - buf < maplength)
119         return AVERROR_INVALIDDATA;
120
121     if (depth != 8 && maplength) {
122         av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
123
124     } else if (maplength) {
125         unsigned int len = maplength / 3;
126
127         if (maplength % 3 || maplength > 768) {
128             av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
129             return AVERROR_INVALIDDATA;
130         }
131
132         ptr = p->data[1];
133         for (x = 0; x < len; x++, ptr += 4)
134             *(uint32_t *)ptr = (buf[x] << 16) + (buf[len + x] << 8) + buf[len + len + x];
135     }
136
137     buf += maplength;
138
139     ptr    = p->data[0];
140     stride = p->linesize[0];
141
142     /* scanlines are aligned on 16 bit boundaries */
143     len  = (depth * w + 7) >> 3;
144     alen = len + (len & 1);
145
146     if (type == RT_BYTE_ENCODED) {
147         int value, run;
148         uint8_t *end = ptr + h * stride;
149
150         x = 0;
151         while (ptr != end && buf < buf_end) {
152             run = 1;
153             if (buf_end - buf < 1)
154                 return AVERROR_INVALIDDATA;
155
156             if ((value = *buf++) == RLE_TRIGGER) {
157                 run = *buf++ + 1;
158                 if (run != 1)
159                     value = *buf++;
160             }
161             while (run--) {
162                 if (x < len)
163                     ptr[x] = value;
164                 if (++x >= alen) {
165                     x = 0;
166                     ptr += stride;
167                     if (ptr == end)
168                         break;
169                 }
170             }
171         }
172     } else {
173         for (y = 0; y < h; y++) {
174             if (buf_end - buf < len)
175                 break;
176             memcpy(ptr, buf, len);
177             ptr += stride;
178             buf += alen;
179         }
180     }
181
182     *picture   = s->picture;
183     *data_size = sizeof(AVFrame);
184
185     return buf - bufstart;
186 }
187
188 static av_cold int sunrast_end(AVCodecContext *avctx) {
189     SUNRASTContext *s = avctx->priv_data;
190
191     if(s->picture.data[0])
192         avctx->release_buffer(avctx, &s->picture);
193
194     return 0;
195 }
196
197 AVCodec ff_sunrast_decoder = {
198     .name           = "sunrast",
199     .type           = AVMEDIA_TYPE_VIDEO,
200     .id             = AV_CODEC_ID_SUNRAST,
201     .priv_data_size = sizeof(SUNRASTContext),
202     .init           = sunrast_init,
203     .close          = sunrast_end,
204     .decode         = sunrast_decode_frame,
205     .capabilities   = CODEC_CAP_DR1,
206     .long_name      = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
207 };