Mercurial > mplayer.hg
annotate gui/util/bitmap.c @ 34636:fde1a35cf043
Simplify codec id <-> tag mapping using avformat_get_riff_*_tags.
This also ensures that again only audio mappings will be used for
audio and video mappings for video.
This fixes bug #2038.
Based on patch by Andrew Wason [rectalogic rectalogic com].
author | reimar |
---|---|
date | Tue, 14 Feb 2012 19:22:22 +0000 |
parents | 03a6ae3bee1e |
children | b03481253518 |
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 | |
33984 | 19 /** |
20 * @file | |
21 * @brief Image loader and bitmap mask rendering | |
22 */ | |
23 | |
33046 | 24 #include <stdio.h> |
25 #include <stdlib.h> | |
26 #include <string.h> | |
33130 | 27 #include <unistd.h> |
33046 | 28 |
29 #include "bitmap.h" | |
30 | |
31 #include "help_mp.h" | |
32 #include "libavcodec/avcodec.h" | |
33690 | 33 #include "libavutil/common.h" |
33046 | 34 #include "libavutil/intreadwrite.h" |
35 #include "libvo/fastmemcpy.h" | |
36 #include "mp_msg.h" | |
37 | |
33984 | 38 /** |
39 * @brief Read and decode a PNG file into bitmap data. | |
40 * | |
41 * @param fname filename (with path) | |
42 * @param img pointer suitable to store the image data | |
43 * | |
44 * @return 0 (ok), 1 (decoding error), 2 (open error), 3 (file too big), | |
45 * 4 (out of memory), 5 (avcodec alloc error) | |
46 */ | |
33983 | 47 static int pngRead(const char *fname, guiImage *img) |
33046 | 48 { |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
49 FILE *file; |
33134 | 50 long len; |
33046 | 51 void *data; |
33135 | 52 int decode_ok, bpl; |
33046 | 53 AVCodecContext *avctx; |
54 AVFrame *frame; | |
55 AVPacket pkt; | |
56 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
57 file = fopen(fname, "rb"); |
33046 | 58 |
33137 | 59 if (!file) |
60 return 2; | |
33046 | 61 |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
62 fseek(file, 0, SEEK_END); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
63 len = ftell(file); |
33046 | 64 |
65 if (len > 50 * 1024 * 1024) { | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
66 fclose(file); |
33137 | 67 return 3; |
33046 | 68 } |
69 | |
70 data = av_malloc(len + FF_INPUT_BUFFER_PADDING_SIZE); | |
71 | |
33135 | 72 if (!data) { |
73 fclose(file); | |
33137 | 74 return 4; |
33135 | 75 } |
76 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
77 fseek(file, 0, SEEK_SET); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
78 fread(data, len, 1, file); |
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
79 fclose(file); |
33046 | 80 |
34468 | 81 avctx = avcodec_alloc_context3(NULL); |
33046 | 82 frame = avcodec_alloc_frame(); |
33139 | 83 |
84 if (!(avctx && frame)) { | |
85 av_free(frame); | |
86 av_free(avctx); | |
87 av_free(data); | |
88 return 5; | |
89 } | |
90 | |
33046 | 91 avcodec_register_all(); |
34468 | 92 avcodec_open2(avctx, avcodec_find_decoder(CODEC_ID_PNG), NULL); |
33139 | 93 |
33046 | 94 av_init_packet(&pkt); |
95 pkt.data = data; | |
96 pkt.size = len; | |
97 // HACK: make PNGs decode normally instead of as CorePNG delta frames | |
98 pkt.flags = AV_PKT_FLAG_KEY; | |
33139 | 99 |
33046 | 100 avcodec_decode_video2(avctx, frame, &decode_ok, &pkt); |
33134 | 101 |
33983 | 102 memset(img, 0, sizeof(*img)); |
33046 | 103 |
104 switch (avctx->pix_fmt) { | |
105 case PIX_FMT_GRAY8: | |
33983 | 106 img->Bpp = 8; |
33046 | 107 break; |
108 | |
109 case PIX_FMT_GRAY16BE: | |
33983 | 110 img->Bpp = 16; |
33046 | 111 break; |
112 | |
113 case PIX_FMT_RGB24: | |
33983 | 114 img->Bpp = 24; |
33046 | 115 break; |
116 | |
34515 | 117 case PIX_FMT_RGBA: |
33983 | 118 img->Bpp = 32; |
33046 | 119 break; |
120 | |
121 default: | |
33983 | 122 img->Bpp = 0; |
33046 | 123 break; |
124 } | |
125 | |
33983 | 126 if (decode_ok && img->Bpp) { |
127 img->Width = avctx->width; | |
128 img->Height = avctx->height; | |
129 bpl = img->Width * (img->Bpp / 8); | |
130 img->ImageSize = bpl * img->Height; | |
33136 | 131 |
33985 | 132 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] file: %s\n", fname); |
133 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] size: %lux%lu, color depth: %u\n", img->Width, img->Height, img->Bpp); | |
134 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] image size: %lu\n", img->ImageSize); | |
33136 | 135 |
33983 | 136 img->Image = malloc(img->ImageSize); |
33135 | 137 |
33983 | 138 if (img->Image) |
139 memcpy_pic(img->Image, frame->data[0], bpl, img->Height, bpl, frame->linesize[0]); | |
33137 | 140 else |
141 decode_ok = 0; | |
33046 | 142 } |
143 | |
144 avcodec_close(avctx); | |
33138 | 145 av_free(frame); |
146 av_free(avctx); | |
147 av_free(data); | |
33046 | 148 |
33983 | 149 return !(decode_ok && img->Bpp); |
33046 | 150 } |
151 | |
33984 | 152 /** |
34515 | 153 * @brief Convert a 24-bit RGB or 32-bit RGBA image into a 32-bit ARGB image. |
33984 | 154 * |
155 * @param img image to be converted | |
156 * | |
157 * @return 1 (ok) or 0 (error) | |
158 * | |
34515 | 159 * @note This is an in-place conversion, |
160 * new memory will be allocated for @a img if necessary. | |
33984 | 161 */ |
34515 | 162 static int convert_ARGB(guiImage *img) |
33046 | 163 { |
33132 | 164 char *orgImage; |
165 unsigned long i, c; | |
33046 | 166 |
33983 | 167 if (img->Bpp == 24) { |
168 orgImage = img->Image; | |
33132 | 169 |
33983 | 170 img->Bpp = 32; |
171 img->ImageSize = img->Width * img->Height * 4; | |
172 img->Image = calloc(1, img->ImageSize); | |
33046 | 173 |
33983 | 174 if (!img->Image) { |
33132 | 175 free(orgImage); |
33985 | 176 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", img->ImageSize); |
33121 | 177 return 0; |
33046 | 178 } |
179 | |
33985 | 180 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 32 bpp conversion size: %lu\n", img->ImageSize); |
33112 | 181 |
34517 | 182 for (i = 0, c = 0; i < img->ImageSize; i += 4, c += 3) |
183 *(uint32_t *)&img->Image[i] = ALPHA_OPAQUE | AV_RB24(&orgImage[c]); | |
33046 | 184 |
33132 | 185 free(orgImage); |
34515 | 186 } else if (img->Bpp == 32) { |
187 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 32 bpp ARGB conversion\n"); | |
188 | |
189 for (i = 0; i < img->ImageSize; i += 4) | |
190 *(uint32_t *)&img->Image[i] = (img->Image[i + 3] << 24) | AV_RB24(&img->Image[i]); | |
191 } else | |
192 return 0; | |
33046 | 193 |
33121 | 194 return 1; |
33046 | 195 } |
196 | |
33984 | 197 /** |
198 * @brief Check whether a (PNG) file exists. | |
199 * | |
200 * @param fname filename (with path, but may lack extension) | |
201 * | |
202 * @return path including extension (ok) or NULL (not accessible) | |
203 */ | |
33710 | 204 static const char *fExist(const char *fname) |
33046 | 205 { |
33141 | 206 static const char ext[][4] = { "png", "PNG" }; |
33710 | 207 static char buf[512]; |
33130 | 208 unsigned int i; |
33046 | 209 |
33130 | 210 if (access(fname, R_OK) == 0) |
33046 | 211 return fname; |
212 | |
33130 | 213 for (i = 0; i < FF_ARRAY_ELEMS(ext); i++) { |
214 snprintf(buf, sizeof(buf), "%s.%s", fname, ext[i]); | |
33046 | 215 |
33130 | 216 if (access(buf, R_OK) == 0) |
217 return buf; | |
33046 | 218 } |
219 | |
220 return NULL; | |
221 } | |
222 | |
33984 | 223 /** |
224 * @brief Read a PNG file. | |
225 * | |
226 * @param fname filename (with path, but may lack extension) | |
227 * @param img pointer suitable to store the image data | |
228 * | |
229 * @return 0 (ok), -1 (color depth too low), -2 (not accessible), | |
34515 | 230 * -5 (#pngRead() error) or -8 (#convert_ARGB() error) |
33984 | 231 */ |
33983 | 232 int bpRead(const char *fname, guiImage *img) |
33046 | 233 { |
33137 | 234 int r; |
235 | |
33046 | 236 fname = fExist(fname); |
237 | |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
238 if (!fname) |
33046 | 239 return -2; |
240 | |
33983 | 241 r = pngRead(fname, img); |
33137 | 242 |
243 if (r != 0) { | |
33985 | 244 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] read error #%d: %s\n", r, fname); |
33046 | 245 return -5; |
246 } | |
247 | |
33983 | 248 if (img->Bpp < 24) { |
33985 | 249 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] bpp too low: %u\n", img->Bpp); |
33046 | 250 return -1; |
251 } | |
252 | |
34515 | 253 if (!convert_ARGB(img)) |
33046 | 254 return -8; |
255 | |
256 return 0; | |
257 } | |
258 | |
33984 | 259 /** |
260 * @brief Free all memory allocated to an image and set all its pointers to NULL. | |
261 * | |
262 * @param img image to be freed | |
263 */ | |
33983 | 264 void bpFree(guiImage *img) |
33046 | 265 { |
33983 | 266 free(img->Image); |
267 memset(img, 0, sizeof(*img)); | |
33046 | 268 } |
269 | |
33984 | 270 /** |
271 * @brief Render a bitmap mask for an image. | |
272 * | |
273 * @param in image to render a bitmap mask from | |
274 * @param out bitmap mask | |
275 * | |
276 * @return 1 (ok) or 0 (error) | |
34132 | 277 * |
278 * @note As a side effect, transparent pixels of @a in will be rendered black. | |
33984 | 279 */ |
33710 | 280 int bpRenderMask(const guiImage *in, guiImage *out) |
33046 | 281 { |
33118 | 282 uint32_t *buf; |
34130 | 283 unsigned long x, y; |
284 unsigned long i = 0, c = 0; | |
285 unsigned char tmp = 0, b = 1; | |
33118 | 286 int shaped = 0; |
287 | |
33046 | 288 out->Width = in->Width; |
289 out->Height = in->Height; | |
33555 | 290 out->Bpp = 1; |
34130 | 291 out->ImageSize = ((out->Width + 7) / 8) * out->Height; |
33118 | 292 out->Image = calloc(1, out->ImageSize); |
33046 | 293 |
33122
1d81476c0d1e
Cosmetic: Rephrase some conditions and rename some variables.
ib
parents:
33121
diff
changeset
|
294 if (!out->Image) { |
33985 | 295 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory: %lu\n", out->ImageSize); |
33114 | 296 return 0; |
33125 | 297 } |
298 | |
33126 | 299 buf = (uint32_t *)in->Image; |
33046 | 300 |
34130 | 301 for (y = 0; y < in->Height; y++) { |
34131 | 302 for (x = 0; x < in->Width; x++) { |
303 if (!IS_TRANSPARENT(buf[i])) | |
304 tmp |= b; | |
305 else { | |
34132 | 306 buf[i] = 0; // pixel should be black (if transparency isn't supported) |
34131 | 307 shaped = 1; |
308 } | |
309 | |
310 i++; | |
311 b <<= 1; | |
312 | |
313 if (b == 0) { | |
314 out->Image[c++] = tmp; | |
315 tmp = 0; | |
316 b = 1; | |
317 } | |
33128 | 318 } |
33046 | 319 |
34131 | 320 if (b != 1) { |
33126 | 321 out->Image[c++] = tmp; |
34130 | 322 tmp = 0; |
323 b = 1; | |
33126 | 324 } |
325 } | |
33112 | 326 |
33126 | 327 if (!shaped) |
328 bpFree(out); | |
33114 | 329 |
33985 | 330 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] 1 bpp conversion size: %lu\n", out->ImageSize); |
33133 | 331 |
33114 | 332 return 1; |
33046 | 333 } |