Mercurial > mplayer.hg
annotate gui/bitmap.c @ 33029:6b6320dcefdf
For a stream without pts values time video frames based on fps
value.
author | reimar |
---|---|
date | Sat, 26 Mar 2011 16:01:00 +0000 |
parents | 9949f3a123cf |
children |
rev | line source |
---|---|
26458 | 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 | |
23077 | 19 #include <stdio.h> |
20 #include <stdlib.h> | |
21 #include <string.h> | |
22 | |
32913 | 23 #include "bitmap.h" |
24 | |
23077 | 25 #include "help_mp.h" |
23193
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
26 #include "libavcodec/avcodec.h" |
23225
794aba782ebd
Fix Gui colors for 32 bit png images after switch to libavcodec decoding
reimar
parents:
23200
diff
changeset
|
27 #include "libavutil/intreadwrite.h" |
23193
7857af1ca50b
Remove libpng dependency for Gui, use libavcodec instead
reimar
parents:
23077
diff
changeset
|
28 #include "libvo/fastmemcpy.h" |
32913 | 29 #include "mp_msg.h" |
23077 | 30 |
32913 | 31 static int pngRead(unsigned char *fname, txSample *bf) |
23077 | 32 { |
32913 | 33 FILE *fp; |
34 int decode_ok; | |
35 void *data; | |
36 int len; | |
37 AVCodecContext *avctx; | |
38 AVFrame *frame; | |
39 AVPacket pkt; | |
40 | |
41 fp = fopen(fname, "rb"); | |
42 | |
43 if (!fp) { | |
44 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[png] file read error ( %s )\n", fname); | |
45 return 1; | |
46 } | |
47 | |
48 fseek(fp, 0, SEEK_END); | |
49 len = ftell(fp); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26458
diff
changeset
|
50 |
32913 | 51 if (len > 50 * 1024 * 1024) { |
52 fclose(fp); | |
53 return 2; | |
54 } | |
55 | |
56 data = av_malloc(len + FF_INPUT_BUFFER_PADDING_SIZE); | |
57 | |
58 fseek(fp, 0, SEEK_SET); | |
59 fread(data, len, 1, fp); | |
60 fclose(fp); | |
61 | |
62 avctx = avcodec_alloc_context(); | |
63 frame = avcodec_alloc_frame(); | |
64 avcodec_register_all(); | |
65 avcodec_open(avctx, avcodec_find_decoder(CODEC_ID_PNG)); | |
66 av_init_packet(&pkt); | |
67 pkt.data = data; | |
68 pkt.size = len; | |
69 // HACK: make PNGs decode normally instead of as CorePNG delta frames | |
70 pkt.flags = AV_PKT_FLAG_KEY; | |
71 avcodec_decode_video2(avctx, frame, &decode_ok, &pkt); | |
72 memset(bf, 0, sizeof(*bf)); | |
23077 | 73 |
32913 | 74 switch (avctx->pix_fmt) { |
75 case PIX_FMT_GRAY8: | |
76 bf->BPP = 8; | |
77 break; | |
78 | |
79 case PIX_FMT_GRAY16BE: | |
80 bf->BPP = 16; | |
81 break; | |
82 | |
83 case PIX_FMT_RGB24: | |
84 bf->BPP = 24; | |
85 break; | |
86 | |
87 case PIX_FMT_BGRA: | |
88 case PIX_FMT_ARGB: | |
89 bf->BPP = 32; | |
90 break; | |
23077 | 91 |
32913 | 92 default: |
93 bf->BPP = 0; | |
94 break; | |
95 } | |
96 | |
97 if (decode_ok && bf->BPP) { | |
98 int bpl; | |
99 | |
100 bf->Width = avctx->width; | |
101 bf->Height = avctx->height; | |
102 bpl = bf->Width * (bf->BPP / 8); | |
103 bf->ImageSize = bpl * bf->Height; | |
104 bf->Image = malloc(bf->ImageSize); | |
105 memcpy_pic(bf->Image, frame->data[0], bpl, bf->Height, bpl, frame->linesize[0]); | |
106 } | |
107 | |
108 avcodec_close(avctx); | |
109 av_freep(&frame); | |
110 av_freep(&avctx); | |
111 av_freep(&data); | |
112 | |
113 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[png] filename: %s.\n", fname); | |
32915
0cd5c0498962
Provide proper conversion specification for long and unsigned variables.
ib
parents:
32914
diff
changeset
|
114 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[png] size: %lux%lu bits: %u\n", bf->Width, bf->Height, bf->BPP); |
32913 | 115 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[png] imagesize: %lu\n", bf->ImageSize); |
116 | |
117 return !(decode_ok && bf->BPP); | |
23077 | 118 } |
119 | |
32913 | 120 static int conv24to32(txSample *bf) |
23077 | 121 { |
32913 | 122 unsigned char *tmpImage; |
32916 | 123 unsigned int i, c; |
32913 | 124 |
125 if (bf->BPP == 24) { | |
126 tmpImage = bf->Image; | |
127 bf->BPP = 32; | |
128 bf->ImageSize = bf->Width * bf->Height * 4; | |
129 bf->Image = calloc(1, bf->ImageSize); | |
23077 | 130 |
32913 | 131 if (!bf->Image) { |
132 free(tmpImage); | |
133 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] not enough memory for image\n"); | |
134 return 1; | |
135 } | |
136 | |
137 for (c = 0, i = 0; c < bf->ImageSize; c += 4, i += 3) | |
138 *(uint32_t *)&bf->Image[c] = AV_RB24(&tmpImage[i]); | |
139 | |
140 free(tmpImage); | |
23077 | 141 } |
32913 | 142 |
143 return 0; | |
23077 | 144 } |
145 | |
32913 | 146 static void Normalize(txSample *bf) |
23077 | 147 { |
32913 | 148 int i; |
149 | |
150 for (i = 0; i < (int)bf->ImageSize; i += 4) | |
29401
f01023c524c3
Replace WORDS_BIGENDIAN by HAVE_BIGENDIAN in all internal code.
diego
parents:
29263
diff
changeset
|
151 #if !HAVE_BIGENDIAN |
32913 | 152 bf->Image[i + 3] = 0; |
23077 | 153 #else |
32913 | 154 bf->Image[i] = 0; |
23077 | 155 #endif |
156 } | |
157 | |
32913 | 158 static unsigned char *fExist(unsigned char *fname) |
23077 | 159 { |
32914
692c174b6dc9
Move declaration of char array into scope of fExist() where it's used.
ib
parents:
32913
diff
changeset
|
160 static unsigned char tmp[512]; |
32913 | 161 FILE *fl; |
162 unsigned char ext[][6] = { ".png\0", ".PNG\0" }; | |
163 int i; | |
164 | |
165 fl = fopen(fname, "rb"); | |
23077 | 166 |
32913 | 167 if (fl != NULL) { |
168 fclose(fl); | |
169 return fname; | |
23077 | 170 } |
32913 | 171 |
172 for (i = 0; i < 2; i++) { | |
32914
692c174b6dc9
Move declaration of char array into scope of fExist() where it's used.
ib
parents:
32913
diff
changeset
|
173 snprintf(tmp, sizeof(tmp), "%s%s", fname, ext[i]); |
32913 | 174 fl = fopen(tmp, "rb"); |
175 | |
176 if (fl != NULL) { | |
177 fclose(fl); | |
178 return tmp; | |
179 } | |
180 } | |
181 | |
182 return NULL; | |
23077 | 183 } |
184 | |
32913 | 185 int bpRead(char *fname, txSample *bf) |
23077 | 186 { |
32913 | 187 fname = fExist(fname); |
188 | |
189 if (fname == NULL) | |
190 return -2; | |
191 | |
192 if (pngRead(fname, bf)) { | |
193 mp_dbg(MSGT_GPLAYER, MSGL_FATAL, "[bitmap] unknown file type ( %s )\n", fname); | |
194 return -5; | |
195 } | |
196 | |
197 if (bf->BPP < 24) { | |
198 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[bitmap] Sorry, only 24 and 32 bpp bitmaps are supported.\n"); | |
199 return -1; | |
200 } | |
201 | |
202 if (conv24to32(bf)) | |
203 return -8; | |
204 | |
205 Normalize(bf); | |
206 return 0; | |
23077 | 207 } |
208 | |
32917
9949f3a123cf
Add new function bpFree() to free txSamples (bitmaps).
ib
parents:
32916
diff
changeset
|
209 void bpFree(txSample *bf) |
9949f3a123cf
Add new function bpFree() to free txSamples (bitmaps).
ib
parents:
32916
diff
changeset
|
210 { |
9949f3a123cf
Add new function bpFree() to free txSamples (bitmaps).
ib
parents:
32916
diff
changeset
|
211 free(bf->Image); |
9949f3a123cf
Add new function bpFree() to free txSamples (bitmaps).
ib
parents:
32916
diff
changeset
|
212 memset(bf, 0, sizeof(*bf)); |
9949f3a123cf
Add new function bpFree() to free txSamples (bitmaps).
ib
parents:
32916
diff
changeset
|
213 } |
9949f3a123cf
Add new function bpFree() to free txSamples (bitmaps).
ib
parents:
32916
diff
changeset
|
214 |
32913 | 215 void Convert32to1(txSample *in, txSample *out, int adaptivlimit) |
23077 | 216 { |
32913 | 217 out->Width = in->Width; |
218 out->Height = in->Height; | |
219 out->BPP = 1; | |
220 out->ImageSize = (out->Width * out->Height + 7) / 8; | |
221 | |
32915
0cd5c0498962
Provide proper conversion specification for long and unsigned variables.
ib
parents:
32914
diff
changeset
|
222 mp_dbg(MSGT_GPLAYER, MSGL_DBG2, "[c32to1] imagesize: %lu\n", out->ImageSize); |
32913 | 223 |
224 out->Image = calloc(1, out->ImageSize); | |
225 | |
226 if (out->Image == NULL) | |
227 mp_msg(MSGT_GPLAYER, MSGL_WARN, MSGTR_NotEnoughMemoryC32To1); | |
228 { | |
229 int i, b, c = 0; | |
230 unsigned int *buf = NULL; | |
231 unsigned char tmp = 0; | |
232 int nothaveshape = 1; | |
233 | |
234 buf = (unsigned int *)in->Image; | |
235 | |
236 for (b = 0, i = 0; i < (int)(out->Width * out->Height); i++) { | |
237 if ((int)buf[i] != adaptivlimit) | |
238 tmp = (tmp >> 1) | 128; | |
239 else { | |
240 tmp = tmp >> 1; | |
241 buf[i] = nothaveshape = 0; | |
242 } | |
243 | |
244 if (b++ == 7) { | |
245 out->Image[c++] = tmp; | |
246 tmp = b = 0; | |
247 } | |
248 } | |
249 | |
250 if (b) | |
251 out->Image[c] = tmp; | |
252 | |
253 if (nothaveshape) { | |
254 free(out->Image); | |
255 out->Image = NULL; | |
256 } | |
257 } | |
23077 | 258 } |