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