FFmpeg
Loading...
Searching...
No Matches
sunrast.c
Go to the documentation of this file.
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 FFmpeg.
6 *
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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/avassert.h"
23#include "libavutil/common.h"
25#include "libavutil/mem.h"
26#include "avcodec.h"
27#include "codec_internal.h"
28#include "decode.h"
29#include "sunrast.h"
30
32 int *got_frame, AVPacket *avpkt)
33{
34 const uint8_t *buf = avpkt->data;
35 const uint8_t *buf_end = avpkt->data + avpkt->size;
36 unsigned int w, h, depth, type, maptype, maplength, x, y, len, alen;
37 ptrdiff_t stride;
38 uint8_t *ptr, *ptr2 = NULL;
39 const uint8_t *bufstart = buf;
40 int ret;
41
42 if (avpkt->size < 32)
44
45 if (AV_RB32(buf) != RAS_MAGIC) {
46 av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
48 }
49
50 w = AV_RB32(buf + 4);
51 h = AV_RB32(buf + 8);
52 depth = AV_RB32(buf + 12);
53 type = AV_RB32(buf + 20);
54 maptype = AV_RB32(buf + 24);
55 maplength = AV_RB32(buf + 28);
56 buf += 32;
57
58 if (type == RT_EXPERIMENTAL) {
59 avpriv_request_sample(avctx, "TIFF/IFF/EXPERIMENTAL (compression) type");
61 }
62 if (type > RT_FORMAT_IFF) {
63 av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
65 }
66 if (maptype == RMT_RAW) {
67 avpriv_request_sample(avctx, "Unknown colormap type");
69 }
70 if (maptype > RMT_RAW) {
71 av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
73 }
74
76 av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
78 }
79
80 if (maplength > 768) {
81 av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
83 }
84
85 // This also checks depth to be valid
86 switch (depth) {
87 case 1:
88 avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_MONOWHITE;
89 break;
90 case 4:
91 avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_NONE;
92 break;
93 case 8:
94 avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
95 break;
96 case 24:
98 break;
99 case 32:
101 break;
102 default:
103 av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
104 return AVERROR_INVALIDDATA;
105 }
106
107 // This checks w and h to be valid in the sense that bytes of a padded bitmap are addressable with 32bit int
108 ret = ff_set_dimensions(avctx, w, h);
109 if (ret < 0)
110 return ret;
111
112 // ensured by ff_set_dimensions()
113 av_assert0(w <= (INT32_MAX - 7) / depth);
114
115 /* scanlines are aligned on 16 bit boundaries */
116 len = (depth * w + 7) >> 3;
117 alen = len + (len & 1);
118
119 // ensured by ff_set_dimensions()
120 av_assert0(h <= INT32_MAX / (3 * len));
121
122 // maplength is limited to 768 and the right term is limited to INT32_MAX / 256 so the add needs no check
123 if (buf_end - buf < (uint64_t)maplength + (len * h) * 3 / 256)
124 return AVERROR_INVALIDDATA;
125
126 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
127 return ret;
128
129 p->pict_type = AV_PICTURE_TYPE_I;
130
131 if (depth > 8 && maplength) {
132 av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
133
134 } else if (maplength) {
135 unsigned int len = maplength / 3;
136
137 if (maplength % 3) {
138 av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
139 return AVERROR_INVALIDDATA;
140 }
141
142 ptr = p->data[1];
143 for (x = 0; x < len; x++, ptr += 4)
144 *(uint32_t *)ptr = (0xFFU<<24) + (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
145 }
146
147 buf += maplength;
148
149 if (maplength && depth < 8) {
150 ptr = ptr2 = av_malloc_array((w + 15), h);
151 if (!ptr)
152 return AVERROR(ENOMEM);
153 stride = (w + 15 >> 3) * depth;
154 } else {
155 ptr = p->data[0];
156 stride = p->linesize[0];
157 }
158
159 if (type == RT_BYTE_ENCODED) {
160 int value, run;
161 uint8_t *end = ptr + (ptrdiff_t)h * stride;
162
163 x = 0;
164 while (ptr != end && buf < buf_end) {
165 run = 1;
166 if (buf_end - buf < 1) {
167 av_freep(&ptr2);
168 return AVERROR_INVALIDDATA;
169 }
170
171 if ((value = *buf++) == RLE_TRIGGER) {
172 run = *buf++ + 1;
173 if (run != 1)
174 value = *buf++;
175 }
176 while (run--) {
177 if (x < len)
178 ptr[x] = value;
179 if (++x >= alen) {
180 x = 0;
181 ptr += stride;
182 if (ptr == end)
183 break;
184 }
185 }
186 }
187 } else {
188 for (y = 0; y < h; y++) {
189 if (buf_end - buf < alen)
190 break;
191 memcpy(ptr, buf, len);
192 ptr += stride;
193 buf += alen;
194 }
195 }
196 if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && depth < 8) {
197 uint8_t *ptr_free = ptr2;
198 ptr = p->data[0];
199 for (y=0; y<h; y++) {
200 for (x = 0; x < (w + 7 >> 3) * depth; x++) {
201 if (depth == 1) {
202 ptr[8*x] = ptr2[x] >> 7;
203 ptr[8*x+1] = ptr2[x] >> 6 & 1;
204 ptr[8*x+2] = ptr2[x] >> 5 & 1;
205 ptr[8*x+3] = ptr2[x] >> 4 & 1;
206 ptr[8*x+4] = ptr2[x] >> 3 & 1;
207 ptr[8*x+5] = ptr2[x] >> 2 & 1;
208 ptr[8*x+6] = ptr2[x] >> 1 & 1;
209 ptr[8*x+7] = ptr2[x] & 1;
210 } else {
211 ptr[2*x] = ptr2[x] >> 4;
212 ptr[2*x+1] = ptr2[x] & 0xF;
213 }
214 }
215 ptr += p->linesize[0];
216 ptr2 += (w + 15 >> 3) * depth;
217 }
218 av_freep(&ptr_free);
219 }
220
221 *got_frame = 1;
222
223 return buf - bufstart;
224}
225
227 .p.name = "sunrast",
228 CODEC_LONG_NAME("Sun Rasterfile image"),
229 .p.type = AVMEDIA_TYPE_VIDEO,
230 .p.id = AV_CODEC_ID_SUNRAST,
231 .p.capabilities = AV_CODEC_CAP_DR1,
233};
const FFCodec ff_sunrast_decoder
Definition sunrast.c:226
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
common internal and external API header
#define NULL
Definition coverity.c:32
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
double value
Definition eval.c:102
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_SUNRAST
Definition codec_id.h:160
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
cl_device_type type
#define AV_RB32(p)
uint8_t w
Definition llvidencdsp.c:39
Memory handling functions.
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_0BGR
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
Definition pixfmt.h:264
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
@ AV_PIX_FMT_0RGB
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
Definition pixfmt.h:262
@ AV_PIX_FMT_MONOWHITE
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb.
Definition pixfmt.h:82
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
static int sunrast_decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition sunrast.c:31
#define RAS_MAGIC
Definition sunrast.h:25
#define RMT_RAW
the data layout of this map type is unknown
Definition sunrast.h:29
#define RT_FORMAT_RGB
Definition sunrast.h:43
#define RT_BYTE_ENCODED
Definition sunrast.h:38
#define RT_EXPERIMENTAL
Definition sunrast.h:54
#define RT_FORMAT_IFF
Definition sunrast.h:49
#define RT_FORMAT_TIFF
Definition sunrast.h:48
#define RLE_TRIGGER
Definition sunrast.h:39
uint8_t run
Definition svq3.c:207
#define stride
#define av_malloc_array(a, b)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
int len