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