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