Mercurial > mplayer.hg
annotate gui/util/bitmap.c @ 33135:36fdc061b4a8
Prevent segmentation faults.
Check alloc pointers. Besides, issue debug messages.
author | ib |
---|---|
date | Tue, 05 Apr 2011 13:12:36 +0000 |
parents | ffe524b8b8f3 |
children | 07be88814256 |
rev | line source |
---|---|
33046 | 1 /* |
2 * This file is part of MPlayer. | |
3 * | |
4 * MPlayer is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; either version 2 of the License, or | |
7 * (at your option) any later version. | |
8 * | |
9 * MPlayer is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License along | |
15 * with MPlayer; if not, write to the Free Software Foundation, Inc., | |
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 */ | |
18 | |
19 #include <stdio.h> | |
20 #include <stdlib.h> | |
21 #include <string.h> | |
33130 | 22 #include <unistd.h> |
33046 | 23 |
24 #include "bitmap.h" | |
25 | |
26 #include "help_mp.h" | |
27 #include "libavcodec/avcodec.h" | |
28 #include "libavutil/intreadwrite.h" | |
29 #include "libvo/fastmemcpy.h" | |
30 #include "mp_msg.h" | |
31 | |
32 static int pngRead(unsigned char *fname, txSample *bf) | |
33 { | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
34 FILE *file; |
33134 | 35 long len; |
33046 | 36 void *data; |
33135 | 37 int decode_ok, bpl; |
33046 | 38 AVCodecContext *avctx; |
39 AVFrame *frame; | |
40 AVPacket pkt; | |
41 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
42 file = fopen(fname, "rb"); |
33046 | 43 |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
44 if (!file) { |
33112 | 45 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] open error: %s\n", fname); |
33046 | 46 return 1; |
47 } | |
48 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
49 fseek(file, 0, SEEK_END); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
50 len = ftell(file); |
33046 | 51 |
52 if (len > 50 * 1024 * 1024) { | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
53 fclose(file); |
33112 | 54 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] file too big: %s\n", fname); |
33046 | 55 return 2; |
56 } | |
57 | |
58 data = av_malloc(len + FF_INPUT_BUFFER_PADDING_SIZE); | |
59 | |
33135 | 60 if (!data) { |
61 fclose(file); | |
62 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", len + FF_INPUT_BUFFER_PADDING_SIZE); | |
63 return 3; | |
64 } | |
65 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
66 fseek(file, 0, SEEK_SET); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
67 fread(data, len, 1, file); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
68 fclose(file); |
33046 | 69 |
70 avctx = avcodec_alloc_context(); | |
71 frame = avcodec_alloc_frame(); | |
72 avcodec_register_all(); | |
73 avcodec_open(avctx, avcodec_find_decoder(CODEC_ID_PNG)); | |
74 av_init_packet(&pkt); | |
75 pkt.data = data; | |
76 pkt.size = len; | |
77 // HACK: make PNGs decode normally instead of as CorePNG delta frames | |
78 pkt.flags = AV_PKT_FLAG_KEY; | |
79 avcodec_decode_video2(avctx, frame, &decode_ok, &pkt); | |
33134 | 80 |
33046 | 81 memset(bf, 0, sizeof(*bf)); |
82 | |
83 switch (avctx->pix_fmt) { | |
84 case PIX_FMT_GRAY8: | |
85 bf->BPP = 8; | |
86 break; | |
87 | |
88 case PIX_FMT_GRAY16BE: | |
89 bf->BPP = 16; | |
90 break; | |
91 | |
92 case PIX_FMT_RGB24: | |
93 bf->BPP = 24; | |
94 break; | |
95 | |
96 case PIX_FMT_BGRA: | |
97 case PIX_FMT_ARGB: | |
98 bf->BPP = 32; | |
99 break; | |
100 | |
101 default: | |
102 bf->BPP = 0; | |
103 break; | |
104 } | |
105 | |
106 if (decode_ok && bf->BPP) { | |
107 bf->Width = avctx->width; | |
108 bf->Height = avctx->height; | |
109 bpl = bf->Width * (bf->BPP / 8); | |
110 bf->ImageSize = bpl * bf->Height; | |
111 bf->Image = malloc(bf->ImageSize); | |
33135 | 112 |
113 if (bf->Image) | |
114 memcpy_pic(bf->Image, frame->data[0], bpl, bf->Height, bpl, frame->linesize[0]); | |
33046 | 115 } |
116 | |
117 avcodec_close(avctx); | |
118 av_freep(&frame); | |
119 av_freep(&avctx); | |
120 av_freep(&data); | |
121 | |
33135 | 122 if (decode_ok && bf->BPP && !bf->Image) { |
123 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", bf->ImageSize); | |
124 return 4; | |
125 } | |
126 | |
33112 | 127 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] file: %s\n", fname); |
128 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] size: %lux%lu, color depth: %u\n", bf->Width, bf->Height, bf->BPP); | |
129 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] image size: %lu\n", bf->ImageSize); | |
33046 | 130 |
131 return !(decode_ok && bf->BPP); | |
132 } | |
133 | |
33121 | 134 static int Convert24to32(txSample *bf) |
33046 | 135 { |
33132 | 136 char *orgImage; |
137 unsigned long i, c; | |
33046 | 138 |
139 if (bf->BPP == 24) { | |
33132 | 140 orgImage = bf->Image; |
141 | |
33046 | 142 bf->BPP = 32; |
143 bf->ImageSize = bf->Width * bf->Height * 4; | |
144 bf->Image = calloc(1, bf->ImageSize); | |
145 | |
146 if (!bf->Image) { | |
33132 | 147 free(orgImage); |
33112 | 148 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", bf->ImageSize); |
33121 | 149 return 0; |
33046 | 150 } |
151 | |
33112 | 152 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 32 bpp conversion size: %lu\n", bf->ImageSize); |
153 | |
33046 | 154 for (c = 0, i = 0; c < bf->ImageSize; c += 4, i += 3) |
33132 | 155 *(uint32_t *)&bf->Image[c] = AV_RB24(&orgImage[i]); |
33046 | 156 |
33132 | 157 free(orgImage); |
33046 | 158 } |
159 | |
33121 | 160 return 1; |
33046 | 161 } |
162 | |
163 static void Normalize(txSample *bf) | |
164 { | |
33131 | 165 unsigned long i; |
33046 | 166 |
33131 | 167 for (i = 0; i < bf->ImageSize; i += 4) |
168 #if HAVE_BIGENDIAN | |
169 bf->Image[i] = 0; | |
170 #else | |
33046 | 171 bf->Image[i + 3] = 0; |
172 #endif | |
173 } | |
174 | |
175 static unsigned char *fExist(unsigned char *fname) | |
176 { | |
33130 | 177 unsigned char *ext[] = { "png", "PNG" }; |
178 static unsigned char buf[512]; | |
179 unsigned int i; | |
33046 | 180 |
33130 | 181 if (access(fname, R_OK) == 0) |
33046 | 182 return fname; |
183 | |
33130 | 184 for (i = 0; i < FF_ARRAY_ELEMS(ext); i++) { |
185 snprintf(buf, sizeof(buf), "%s.%s", fname, ext[i]); | |
33046 | 186 |
33130 | 187 if (access(buf, R_OK) == 0) |
188 return buf; | |
33046 | 189 } |
190 | |
191 return NULL; | |
192 } | |
193 | |
194 int bpRead(char *fname, txSample *bf) | |
195 { | |
196 fname = fExist(fname); | |
197 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
198 if (!fname) |
33046 | 199 return -2; |
200 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
201 if (pngRead(fname, bf) != 0) { |
33112 | 202 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] read error: %s\n", fname); |
33046 | 203 return -5; |
204 } | |
205 | |
206 if (bf->BPP < 24) { | |
33112 | 207 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] bpp too low: %u\n", bf->BPP); |
33046 | 208 return -1; |
209 } | |
210 | |
33121 | 211 if (!Convert24to32(bf)) |
33046 | 212 return -8; |
213 | |
214 Normalize(bf); | |
33053 | 215 |
33046 | 216 return 0; |
217 } | |
218 | |
219 void bpFree(txSample *bf) | |
220 { | |
221 free(bf->Image); | |
222 memset(bf, 0, sizeof(*bf)); | |
223 } | |
224 | |
33129 | 225 int Convert32to1(txSample *in, txSample *out) |
33046 | 226 { |
33118 | 227 uint32_t *buf; |
228 unsigned long i; | |
229 int b = 0, c = 0; | |
230 unsigned char tmp = 0; | |
231 int shaped = 0; | |
232 | |
33046 | 233 out->Width = in->Width; |
234 out->Height = in->Height; | |
235 out->BPP = 1; | |
236 out->ImageSize = (out->Width * out->Height + 7) / 8; | |
33118 | 237 out->Image = calloc(1, out->ImageSize); |
33046 | 238 |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
239 if (!out->Image) { |
33114 | 240 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", out->ImageSize); |
241 return 0; | |
33125 | 242 } |
243 | |
33126 | 244 buf = (uint32_t *)in->Image; |
33046 | 245 |
33126 | 246 for (i = 0; i < out->Width * out->Height; i++) { |
247 tmp >>= 1; | |
33119 | 248 |
33129 | 249 if (buf[i] != TRANSPARENT) |
33126 | 250 tmp |= 0x80; |
33128 | 251 else { |
252 buf[i] = 0; | |
33126 | 253 shaped = 1; |
33128 | 254 } |
33046 | 255 |
33126 | 256 if (++b == 8) { |
257 out->Image[c++] = tmp; | |
258 tmp = b = 0; | |
259 } | |
260 } | |
33112 | 261 |
33126 | 262 if (b) |
263 out->Image[c] = tmp; | |
264 | |
265 if (!shaped) | |
266 bpFree(out); | |
33114 | 267 |
33133 | 268 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 1 bpp conversion size: %lu\n", out->ImageSize); |
269 | |
33114 | 270 return 1; |
33046 | 271 } |