Mercurial > mplayer.hg
annotate libmpcodecs/vf_screenshot.c @ 24667:036c49377d02
Make sure there is no uninitialized data in BITMAPINFOHEADER created by fli demuxer
author | reimar |
---|---|
date | Tue, 02 Oct 2007 19:18:33 +0000 |
parents | f8d4f8eff72b |
children | 1d9ea74f79a0 |
rev | line source |
---|---|
17012 | 1 #include "config.h" |
16446 | 2 |
3 #include <stdio.h> | |
4 #include <stdlib.h> | |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
5 #ifdef HAVE_MALLOC_H |
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
6 #include <malloc.h> |
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
7 #endif |
16446 | 8 #include <string.h> |
9 #include <inttypes.h> | |
10 | |
11 #include <sys/types.h> | |
12 #include <sys/stat.h> | |
13 #include <unistd.h> | |
14 | |
15 #include <png.h> | |
16 | |
17012 | 17 #include "mp_msg.h" |
16446 | 18 |
19 #include "img_format.h" | |
20 #include "mp_image.h" | |
21 #include "vf.h" | |
22 #include "vf_scale.h" | |
23 | |
18861 | 24 #include "libswscale/swscale.h" |
16446 | 25 |
26 struct vf_priv_s { | |
27 int frameno; | |
16450 | 28 char fname[102]; |
19833 | 29 /// shot stores current screenshot mode: |
30 /// 0: don't take screenshots | |
31 /// 1: take single screenshot, reset to 0 afterwards | |
32 /// 2: take screenshots of each frame | |
16450 | 33 int shot, store_slices; |
34 int dw, dh, stride; | |
16446 | 35 uint8_t *buffer; |
36 struct SwsContext *ctx; | |
37 }; | |
38 | |
39 //===========================================================================// | |
40 | |
41 static int config(struct vf_instance_s* vf, | |
42 int width, int height, int d_width, int d_height, | |
43 unsigned int flags, unsigned int outfmt) | |
44 { | |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
45 vf->priv->ctx=sws_getContextFromCmdLine(width, height, outfmt, |
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
46 d_width, d_height, IMGFMT_BGR24); |
16446 | 47 |
48 vf->priv->dw = d_width; | |
49 vf->priv->dh = d_height; | |
16450 | 50 vf->priv->stride = (3*vf->priv->dw+15)&~15; |
16446 | 51 |
52 if (vf->priv->buffer) free(vf->priv->buffer); // probably reconfigured | |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
53 vf->priv->buffer = NULL; |
16446 | 54 |
55 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt); | |
56 } | |
57 | |
18484
28fd6f89e1b8
Make return type consistent with usage. Patch by Pierre Lombard.
rathann
parents:
17906
diff
changeset
|
58 static void write_png(char *fname, unsigned char *buffer, int width, int height, int stride) |
16446 | 59 { |
60 FILE * fp; | |
61 png_structp png_ptr; | |
62 png_infop info_ptr; | |
63 png_byte **row_pointers; | |
64 int k; | |
65 | |
66 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); | |
67 info_ptr = png_create_info_struct(png_ptr); | |
68 fp = NULL; | |
69 | |
70 if (setjmp(png_ptr->jmpbuf)) { | |
71 png_destroy_write_struct(&png_ptr, &info_ptr); | |
72 fclose(fp); | |
18484
28fd6f89e1b8
Make return type consistent with usage. Patch by Pierre Lombard.
rathann
parents:
17906
diff
changeset
|
73 return; |
16446 | 74 } |
75 | |
76 fp = fopen (fname, "wb"); | |
77 if (fp == NULL) { | |
78 mp_msg(MSGT_VFILTER,MSGL_ERR,"\nPNG Error opening %s for writing!\n", fname); | |
18484
28fd6f89e1b8
Make return type consistent with usage. Patch by Pierre Lombard.
rathann
parents:
17906
diff
changeset
|
79 return; |
16446 | 80 } |
81 | |
82 png_init_io(png_ptr, fp); | |
83 png_set_compression_level(png_ptr, 0); | |
84 | |
85 png_set_IHDR(png_ptr, info_ptr, width, height, | |
86 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, | |
87 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); | |
88 | |
89 png_write_info(png_ptr, info_ptr); | |
90 | |
91 png_set_bgr(png_ptr); | |
92 | |
18879 | 93 row_pointers = malloc(height*sizeof(png_byte*)); |
16450 | 94 for (k = 0; k < height; k++) { |
16446 | 95 unsigned char* s=buffer + stride*k; |
96 row_pointers[k] = s; | |
97 } | |
98 | |
99 png_write_image(png_ptr, row_pointers); | |
100 png_write_end(png_ptr, info_ptr); | |
101 png_destroy_write_struct(&png_ptr, &info_ptr); | |
102 | |
103 free(row_pointers); | |
104 | |
105 fclose (fp); | |
106 } | |
107 | |
108 static int fexists(char *fname) | |
109 { | |
110 struct stat dummy; | |
111 if (stat(fname, &dummy) == 0) return 1; | |
112 else return 0; | |
113 } | |
114 | |
16450 | 115 static void gen_fname(struct vf_priv_s* priv) |
16446 | 116 { |
16450 | 117 do { |
118 snprintf (priv->fname, 100, "shot%04d.png", ++priv->frameno); | |
119 } while (fexists(priv->fname) && priv->frameno < 100000); | |
16457 | 120 if (fexists(priv->fname)) { |
121 priv->fname[0] = '\0'; | |
122 return; | |
123 } | |
16450 | 124 |
125 mp_msg(MSGT_VFILTER,MSGL_INFO,"*** screenshot '%s' ***\n",priv->fname); | |
126 | |
127 } | |
128 | |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
129 static void scale_image(struct vf_priv_s* priv, mp_image_t *mpi) |
16450 | 130 { |
16446 | 131 uint8_t *dst[3]; |
132 int dst_stride[3]; | |
133 | |
16450 | 134 dst_stride[0] = priv->stride; |
16446 | 135 dst_stride[1] = dst_stride[2] = 0; |
136 if (!priv->buffer) | |
137 priv->buffer = (uint8_t*)memalign(16, dst_stride[0]*priv->dh); | |
138 | |
139 dst[0] = priv->buffer; | |
140 dst[1] = dst[2] = 0; | |
20930
29c478a2483f
Fix segfault with videos with unusual resolution, patch by Stanislav Maslovski % stanislav P maslovski A gmail P com %
gpoirier
parents:
19833
diff
changeset
|
141 sws_scale_ordered(priv->ctx, mpi->planes, mpi->stride, 0, priv->dh, dst, dst_stride); |
16450 | 142 } |
16446 | 143 |
16450 | 144 static void start_slice(struct vf_instance_s* vf, mp_image_t *mpi){ |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
145 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt, |
16450 | 146 mpi->type, mpi->flags, mpi->width, mpi->height); |
147 if (vf->priv->shot) { | |
148 vf->priv->store_slices = 1; | |
149 if (!vf->priv->buffer) | |
150 vf->priv->buffer = (uint8_t*)memalign(16, vf->priv->stride*vf->priv->dh); | |
151 } | |
152 | |
153 } | |
154 | |
155 static void draw_slice(struct vf_instance_s* vf, | |
156 unsigned char** src, int* stride, int w,int h, int x, int y){ | |
157 if (vf->priv->store_slices) { | |
158 uint8_t *dst[3]; | |
159 int dst_stride[3]; | |
160 dst_stride[0] = vf->priv->stride; | |
161 dst_stride[1] = dst_stride[2] = 0; | |
162 dst[0] = vf->priv->buffer; | |
163 dst[1] = dst[2] = 0; | |
164 sws_scale_ordered(vf->priv->ctx, src, stride, y, h, dst, dst_stride); | |
165 } | |
166 vf_next_draw_slice(vf,src,stride,w,h,x,y); | |
167 } | |
168 | |
169 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){ | |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
170 // FIXME: should vf.c really call get_image when using slices?? |
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
171 if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK) |
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
172 return; |
16450 | 173 vf->dmpi= vf_get_image(vf->next, mpi->imgfmt, |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
174 mpi->type, mpi->flags/* | MP_IMGFLAG_READABLE*/, mpi->width, mpi->height); |
16456 | 175 |
16450 | 176 mpi->planes[0]=vf->dmpi->planes[0]; |
177 mpi->stride[0]=vf->dmpi->stride[0]; | |
178 if(mpi->flags&MP_IMGFLAG_PLANAR){ | |
179 mpi->planes[1]=vf->dmpi->planes[1]; | |
180 mpi->planes[2]=vf->dmpi->planes[2]; | |
181 mpi->stride[1]=vf->dmpi->stride[1]; | |
182 mpi->stride[2]=vf->dmpi->stride[2]; | |
183 } | |
184 mpi->width=vf->dmpi->width; | |
185 | |
186 mpi->flags|=MP_IMGFLAG_DIRECT; | |
187 | |
188 mpi->priv=(void*)vf->dmpi; | |
16446 | 189 } |
190 | |
17906
20aca9baf5d8
passing pts through the filter layer (lets see if pts or cola comes out at the end)
michael
parents:
17012
diff
changeset
|
191 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts) |
16446 | 192 { |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
193 mp_image_t *dmpi = (mp_image_t *)mpi->priv; |
16450 | 194 |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
195 if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK) |
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
196 dmpi = vf->dmpi; |
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
197 else |
16450 | 198 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){ |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
199 dmpi=vf_get_image(vf->next,mpi->imgfmt, |
16450 | 200 MP_IMGTYPE_EXPORT, 0, |
201 mpi->width, mpi->height); | |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
202 vf_clone_mpi_attributes(dmpi, mpi); |
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
203 dmpi->planes[0]=mpi->planes[0]; |
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
204 dmpi->planes[1]=mpi->planes[1]; |
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
205 dmpi->planes[2]=mpi->planes[2]; |
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
206 dmpi->stride[0]=mpi->stride[0]; |
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
207 dmpi->stride[1]=mpi->stride[1]; |
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
208 dmpi->stride[2]=mpi->stride[2]; |
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
209 dmpi->width=mpi->width; |
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
210 dmpi->height=mpi->height; |
16450 | 211 } |
16446 | 212 |
213 if(vf->priv->shot) { | |
19833 | 214 if (vf->priv->shot==1) |
215 vf->priv->shot=0; | |
16450 | 216 gen_fname(vf->priv); |
16457 | 217 if (vf->priv->fname[0]) { |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
218 if (!vf->priv->store_slices) |
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
219 scale_image(vf->priv, dmpi); |
16457 | 220 write_png(vf->priv->fname, vf->priv->buffer, vf->priv->dw, vf->priv->dh, vf->priv->stride); |
221 } | |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
222 vf->priv->store_slices = 0; |
16446 | 223 } |
224 | |
17906
20aca9baf5d8
passing pts through the filter layer (lets see if pts or cola comes out at the end)
michael
parents:
17012
diff
changeset
|
225 return vf_next_put_image(vf, dmpi, pts); |
16446 | 226 } |
227 | |
228 int control (vf_instance_t *vf, int request, void *data) | |
229 { | |
19833 | 230 /** data contains an integer argument |
231 * 0: take screenshot with the next frame | |
232 * 1: take screenshots with each frame until the same command is given once again | |
233 **/ | |
16446 | 234 if(request==VFCTRL_SCREENSHOT) { |
19833 | 235 if (data && *(int*)data) { // repeated screenshot mode |
236 if (vf->priv->shot==2) | |
237 vf->priv->shot=0; | |
238 else | |
239 vf->priv->shot=2; | |
240 } else { // single screenshot | |
241 if (!vf->priv->shot) | |
242 vf->priv->shot=1; | |
243 } | |
16446 | 244 return CONTROL_TRUE; |
245 } | |
246 return vf_next_control (vf, request, data); | |
247 } | |
248 | |
249 | |
250 //===========================================================================// | |
251 | |
252 static int query_format(struct vf_instance_s* vf, unsigned int fmt) | |
253 { | |
254 switch(fmt){ | |
255 case IMGFMT_YV12: | |
256 case IMGFMT_I420: | |
257 case IMGFMT_IYUV: | |
258 case IMGFMT_UYVY: | |
259 case IMGFMT_YUY2: | |
260 case IMGFMT_BGR32: | |
261 case IMGFMT_BGR24: | |
262 case IMGFMT_BGR16: | |
263 case IMGFMT_BGR15: | |
264 case IMGFMT_RGB32: | |
265 case IMGFMT_RGB24: | |
266 case IMGFMT_Y800: | |
267 case IMGFMT_Y8: | |
268 case IMGFMT_YVU9: | |
269 case IMGFMT_IF09: | |
270 case IMGFMT_444P: | |
271 case IMGFMT_422P: | |
272 case IMGFMT_411P: | |
273 return vf_next_query_format(vf, fmt); | |
274 } | |
275 return 0; | |
276 } | |
277 | |
16874 | 278 static void uninit(vf_instance_t *vf); |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
279 // open conflicts with stdio.h at least under MinGW |
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
280 static int screenshot_open(vf_instance_t *vf, char* args) |
16446 | 281 { |
282 vf->config=config; | |
283 vf->control=control; | |
284 vf->put_image=put_image; | |
285 vf->query_format=query_format; | |
16450 | 286 vf->start_slice=start_slice; |
287 vf->draw_slice=draw_slice; | |
288 vf->get_image=get_image; | |
16874 | 289 vf->uninit=uninit; |
16446 | 290 vf->priv=malloc(sizeof(struct vf_priv_s)); |
291 vf->priv->frameno=0; | |
292 vf->priv->shot=0; | |
16450 | 293 vf->priv->store_slices=0; |
16446 | 294 vf->priv->buffer=0; |
295 vf->priv->ctx=0; | |
296 return 1; | |
297 } | |
298 | |
299 static void uninit(vf_instance_t *vf) | |
300 { | |
301 if(vf->priv->ctx) sws_freeContext(vf->priv->ctx); | |
302 if (vf->priv->buffer) free(vf->priv->buffer); | |
303 free(vf->priv); | |
304 } | |
305 | |
306 | |
307 vf_info_t vf_info_screenshot = { | |
308 "screenshot to file", | |
309 "screenshot", | |
310 "A'rpi, Jindrich Makovicka", | |
311 "", | |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
312 screenshot_open, |
16446 | 313 NULL |
314 }; | |
315 | |
316 //===========================================================================// |