annotate sunrast.c @ 9759:5968a9f15535 libavcodec

Correct calculation of compressed input length.
author reimar
date Sun, 31 May 2009 10:01:26 +0000
parents 6cd9e789dd9e
children 6a3f4053d935
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6084
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
1 /*
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
2 * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image decoder
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
3 * Copyright (c) 2007, 2008 Ivo van Poorten
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
4 *
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
5 * This file is part of FFmpeg.
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
6 *
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
11 *
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
15 * Lesser General Public License for more details.
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
16 *
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
20 */
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
21
8573
2acf0ae7b041 Fix build: Add intreadwrite.h and bswap.h #includes where necessary.
diego
parents: 7040
diff changeset
22 #include "libavutil/intreadwrite.h"
6084
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
23 #include "avcodec.h"
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
24
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
25 #define RT_OLD 0
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
26 #define RT_STANDARD 1
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
27 #define RT_BYTE_ENCODED 2
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
28 #define RT_FORMAT_RGB 3
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
29 #define RT_FORMAT_TIFF 4
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
30 #define RT_FORMAT_IFF 5
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
31
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
32 typedef struct SUNRASTContext {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
33 AVFrame picture;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
34 } SUNRASTContext;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
35
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6254
diff changeset
36 static av_cold int sunrast_init(AVCodecContext *avctx) {
6084
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
37 SUNRASTContext *s = avctx->priv_data;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
38
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
39 avcodec_get_frame_defaults(&s->picture);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
40 avctx->coded_frame= &s->picture;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
41
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
42 return 0;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
43 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
44
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
45 static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
9355
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 8573
diff changeset
46 int *data_size, AVPacket *avpkt) {
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 8573
diff changeset
47 const uint8_t *buf = avpkt->data;
6084
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
48 SUNRASTContext * const s = avctx->priv_data;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
49 AVFrame *picture = data;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
50 AVFrame * const p = &s->picture;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
51 unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
6254
michael
parents: 6086
diff changeset
52 uint8_t *ptr;
michael
parents: 6086
diff changeset
53 const uint8_t *bufstart = buf;
6084
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
54
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
55 if (AV_RB32(buf) != 0x59a66a95) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
56 av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
57 return -1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
58 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
59
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
60 w = AV_RB32(buf+4);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
61 h = AV_RB32(buf+8);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
62 depth = AV_RB32(buf+12);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
63 type = AV_RB32(buf+20);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
64 maptype = AV_RB32(buf+24);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
65 maplength = AV_RB32(buf+28);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
66
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
67 if (type > RT_BYTE_ENCODED && type <= RT_FORMAT_IFF) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
68 av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
69 return -1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
70 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
71 if (type > RT_FORMAT_IFF) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
72 av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
73 return -1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
74 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
75 if (maptype & ~1) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
76 av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
77 return -1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
78 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
79
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
80 buf += 32;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
81
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
82 switch (depth) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
83 case 1:
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
84 avctx->pix_fmt = PIX_FMT_MONOWHITE;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
85 break;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
86 case 8:
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
87 avctx->pix_fmt = PIX_FMT_PAL8;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
88 break;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
89 case 24:
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
90 avctx->pix_fmt = PIX_FMT_BGR24;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
91 break;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
92 default:
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
93 av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
94 return -1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
95 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
96
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
97 if (p->data[0])
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
98 avctx->release_buffer(avctx, p);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
99
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
100 if (avcodec_check_dimensions(avctx, w, h))
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
101 return -1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
102 if (w != avctx->width || h != avctx->height)
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
103 avcodec_set_dimensions(avctx, w, h);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
104 if (avctx->get_buffer(avctx, p) < 0) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
105 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
106 return -1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
107 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
108
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
109 p->pict_type = FF_I_TYPE;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
110
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
111 if (depth != 8 && maplength) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
112 av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
113
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
114 } else if (depth == 8) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
115 unsigned int len = maplength / 3;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
116
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
117 if (!maplength) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
118 av_log(avctx, AV_LOG_ERROR, "colormap expected\n");
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
119 return -1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
120 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
121 if (maplength % 3 || maplength > 768) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
122 av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
123 return -1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
124 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
125
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
126 ptr = p->data[1];
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
127 for (x=0; x<len; x++, ptr+=4)
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
128 *(uint32_t *)ptr = (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
129 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
130
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
131 buf += maplength;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
132
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
133 ptr = p->data[0];
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
134 stride = p->linesize[0];
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
135
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
136 /* scanlines are aligned on 16 bit boundaries */
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
137 len = (depth * w + 7) >> 3;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
138 alen = len + (len&1);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
139
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
140 if (type == RT_BYTE_ENCODED) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
141 int value, run;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
142 uint8_t *end = ptr + h*stride;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
143
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
144 x = 0;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
145 while (ptr != end) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
146 run = 1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
147 if ((value = *buf++) == 0x80) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
148 run = *buf++ + 1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
149 if (run != 1)
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
150 value = *buf++;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
151 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
152 while (run--) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
153 if (x < len)
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
154 ptr[x] = value;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
155 if (++x >= alen) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
156 x = 0;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
157 ptr += stride;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
158 if (ptr == end)
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
159 break;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
160 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
161 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
162 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
163 } else {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
164 for (y=0; y<h; y++) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
165 memcpy(ptr, buf, len);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
166 ptr += stride;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
167 buf += alen;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
168 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
169 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
170
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
171 *picture = s->picture;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
172 *data_size = sizeof(AVFrame);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
173
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
174 return buf - bufstart;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
175 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
176
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6254
diff changeset
177 static av_cold int sunrast_end(AVCodecContext *avctx) {
6084
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
178 SUNRASTContext *s = avctx->priv_data;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
179
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
180 if(s->picture.data[0])
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
181 avctx->release_buffer(avctx, &s->picture);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
182
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
183 return 0;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
184 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
185
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
186 AVCodec sunrast_decoder = {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
187 "sunrast",
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
188 CODEC_TYPE_VIDEO,
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
189 CODEC_ID_SUNRAST,
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
190 sizeof(SUNRASTContext),
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
191 sunrast_init,
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
192 NULL,
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
193 sunrast_end,
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
194 sunrast_decode_frame,
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
195 0,
6722
6eeb19edcee3 Add long names to some AVCodec declarations.
diego
parents: 6517
diff changeset
196 NULL,
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6722
diff changeset
197 .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
6084
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
198 };