16446
|
1 #include "../config.h"
|
|
2 #ifdef HAVE_PNG
|
|
3
|
|
4 #include <stdio.h>
|
|
5 #include <stdlib.h>
|
|
6 #include <string.h>
|
|
7 #include <inttypes.h>
|
|
8
|
|
9 #include <sys/types.h>
|
|
10 #include <sys/stat.h>
|
|
11 #include <unistd.h>
|
|
12
|
|
13 #include <png.h>
|
|
14
|
|
15 #include "../mp_msg.h"
|
|
16
|
|
17 #include "img_format.h"
|
|
18 #include "mp_image.h"
|
|
19 #include "vf.h"
|
|
20 #include "vf_scale.h"
|
|
21
|
|
22 #include "../libvo/fastmemcpy.h"
|
|
23 #include "../postproc/swscale.h"
|
|
24 #include "../postproc/rgb2rgb.h"
|
|
25
|
|
26 struct vf_priv_s {
|
|
27 int frameno;
|
16450
|
28 char fname[102];
|
|
29 int shot, store_slices;
|
|
30 int dw, dh, stride;
|
16446
|
31 uint8_t *buffer;
|
|
32 struct SwsContext *ctx;
|
|
33 mp_image_t *dmpi;
|
|
34 };
|
|
35
|
|
36 //===========================================================================//
|
|
37
|
|
38 static int config(struct vf_instance_s* vf,
|
|
39 int width, int height, int d_width, int d_height,
|
|
40 unsigned int flags, unsigned int outfmt)
|
|
41 {
|
|
42 int int_sws_flags=0;
|
|
43 SwsFilter *srcFilter, *dstFilter;
|
|
44
|
|
45 sws_getFlagsAndFilterFromCmdLine(&int_sws_flags, &srcFilter, &dstFilter);
|
|
46
|
|
47 vf->priv->ctx=sws_getContext(width, height, outfmt,
|
|
48 d_width, d_height, IMGFMT_BGR24,
|
|
49 int_sws_flags | get_sws_cpuflags(), srcFilter, dstFilter, NULL);
|
|
50
|
|
51 vf->priv->dw = d_width;
|
|
52 vf->priv->dh = d_height;
|
16450
|
53 vf->priv->stride = (3*vf->priv->dw+15)&~15;
|
16446
|
54
|
|
55 if (vf->priv->buffer) free(vf->priv->buffer); // probably reconfigured
|
|
56 vf->priv->dmpi = NULL;
|
|
57
|
|
58 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
|
|
59 }
|
|
60
|
|
61 static int write_png(char *fname, unsigned char *buffer, int width, int height, int stride)
|
|
62 {
|
|
63 FILE * fp;
|
|
64 png_structp png_ptr;
|
|
65 png_infop info_ptr;
|
|
66 png_byte **row_pointers;
|
|
67 int k;
|
|
68
|
|
69 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
|
70 info_ptr = png_create_info_struct(png_ptr);
|
|
71 fp = NULL;
|
|
72
|
|
73 if (setjmp(png_ptr->jmpbuf)) {
|
|
74 png_destroy_write_struct(&png_ptr, &info_ptr);
|
|
75 fclose(fp);
|
|
76 return 0;
|
|
77 }
|
|
78
|
|
79 fp = fopen (fname, "wb");
|
|
80 if (fp == NULL) {
|
|
81 mp_msg(MSGT_VFILTER,MSGL_ERR,"\nPNG Error opening %s for writing!\n", fname);
|
|
82 return 0;
|
|
83 }
|
|
84
|
|
85 png_init_io(png_ptr, fp);
|
|
86 png_set_compression_level(png_ptr, 0);
|
|
87
|
|
88 png_set_IHDR(png_ptr, info_ptr, width, height,
|
|
89 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
|
|
90 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
|
|
91
|
|
92 png_write_info(png_ptr, info_ptr);
|
|
93
|
|
94 png_set_bgr(png_ptr);
|
|
95
|
|
96 row_pointers = (png_byte**)malloc(height*sizeof(png_byte*));
|
16450
|
97 for (k = 0; k < height; k++) {
|
16446
|
98 unsigned char* s=buffer + stride*k;
|
|
99 row_pointers[k] = s;
|
|
100 }
|
|
101
|
|
102 png_write_image(png_ptr, row_pointers);
|
|
103 png_write_end(png_ptr, info_ptr);
|
|
104 png_destroy_write_struct(&png_ptr, &info_ptr);
|
|
105
|
|
106 free(row_pointers);
|
|
107
|
|
108 fclose (fp);
|
|
109 }
|
|
110
|
|
111 static int fexists(char *fname)
|
|
112 {
|
|
113 struct stat dummy;
|
|
114 if (stat(fname, &dummy) == 0) return 1;
|
|
115 else return 0;
|
|
116 }
|
|
117
|
16450
|
118 static void gen_fname(struct vf_priv_s* priv)
|
16446
|
119 {
|
16450
|
120 do {
|
|
121 snprintf (priv->fname, 100, "shot%04d.png", ++priv->frameno);
|
|
122 } while (fexists(priv->fname) && priv->frameno < 100000);
|
|
123 if (fexists(priv->fname)) return;
|
|
124
|
|
125 mp_msg(MSGT_VFILTER,MSGL_INFO,"*** screenshot '%s' ***\n",priv->fname);
|
|
126
|
|
127 }
|
|
128
|
|
129 static void scale_image(struct vf_priv_s* priv)
|
|
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;
|
|
141 sws_scale_ordered(priv->ctx, priv->dmpi->planes, priv->dmpi->stride, 0, priv->dmpi->height, dst, dst_stride);
|
16450
|
142 }
|
16446
|
143
|
16450
|
144 static void start_slice(struct vf_instance_s* vf, mp_image_t *mpi){
|
|
145 if(!vf->next->draw_slice) {
|
|
146 mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
|
|
147 return;
|
|
148 }
|
|
149 if(!(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)) return; // shouldn't happen
|
|
150 mpi->priv=vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
|
|
151 mpi->type, mpi->flags, mpi->width, mpi->height);
|
|
152 if (vf->priv->shot) {
|
|
153 vf->priv->store_slices = 1;
|
|
154 vf->priv->shot = 0;
|
|
155 if (!vf->priv->buffer)
|
|
156 vf->priv->buffer = (uint8_t*)memalign(16, vf->priv->stride*vf->priv->dh);
|
|
157 }
|
|
158
|
|
159 }
|
|
160
|
|
161 static void draw_slice(struct vf_instance_s* vf,
|
|
162 unsigned char** src, int* stride, int w,int h, int x, int y){
|
|
163 mp_image_t *dmpi=vf->dmpi;
|
|
164 if(!dmpi){
|
|
165 mp_msg(MSGT_VFILTER,MSGL_FATAL,"vf_screenshot: draw_slice() called with dmpi=NULL (no get_image??)\n");
|
|
166 return;
|
|
167 }
|
|
168 if (vf->priv->store_slices) {
|
|
169 uint8_t *dst[3];
|
|
170 int dst_stride[3];
|
|
171 dst_stride[0] = vf->priv->stride;
|
|
172 dst_stride[1] = dst_stride[2] = 0;
|
|
173 dst[0] = vf->priv->buffer;
|
|
174 dst[1] = dst[2] = 0;
|
|
175 sws_scale_ordered(vf->priv->ctx, src, stride, y, h, dst, dst_stride);
|
|
176 }
|
|
177 vf_next_draw_slice(vf,src,stride,w,h,x,y);
|
|
178 }
|
|
179
|
|
180 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){
|
|
181 vf->dmpi= vf_get_image(vf->next, mpi->imgfmt,
|
16452
|
182 mpi->type, mpi->flags | MP_IMGFLAG_READABLE, mpi->width, mpi->height);
|
16450
|
183
|
|
184 mpi->planes[0]=vf->dmpi->planes[0];
|
|
185 mpi->stride[0]=vf->dmpi->stride[0];
|
|
186 if(mpi->flags&MP_IMGFLAG_PLANAR){
|
|
187 mpi->planes[1]=vf->dmpi->planes[1];
|
|
188 mpi->planes[2]=vf->dmpi->planes[2];
|
|
189 mpi->stride[1]=vf->dmpi->stride[1];
|
|
190 mpi->stride[2]=vf->dmpi->stride[2];
|
|
191 }
|
|
192 mpi->width=vf->dmpi->width;
|
|
193
|
|
194 mpi->flags|=MP_IMGFLAG_DIRECT;
|
|
195 mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
|
|
196
|
|
197 mpi->priv=(void*)vf->dmpi;
|
16446
|
198 }
|
|
199
|
|
200 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi)
|
|
201 {
|
16450
|
202 if (mpi->flags&MP_IMGFLAG_DRAW_CALLBACK) {
|
|
203 if(vf->priv->store_slices) {
|
|
204 vf->priv->store_slices = 0;
|
|
205 gen_fname(vf->priv);
|
|
206 write_png(vf->priv->fname, vf->priv->buffer, vf->priv->dw, vf->priv->dh, vf->priv->stride);
|
|
207 }
|
|
208 return vf_next_put_image(vf,vf->dmpi);
|
|
209 }
|
|
210
|
|
211 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
|
|
212 vf->priv->dmpi=vf_get_image(vf->next,mpi->imgfmt,
|
|
213 MP_IMGTYPE_EXPORT, 0,
|
|
214 mpi->width, mpi->height);
|
|
215 vf_clone_mpi_attributes(vf->priv->dmpi, mpi);
|
|
216 } else {
|
|
217 vf->priv->dmpi=vf->dmpi;
|
|
218 }
|
|
219
|
16446
|
220 vf->priv->dmpi->planes[0]=mpi->planes[0];
|
|
221 vf->priv->dmpi->planes[1]=mpi->planes[1];
|
|
222 vf->priv->dmpi->planes[2]=mpi->planes[2];
|
|
223 vf->priv->dmpi->stride[0]=mpi->stride[0];
|
|
224 vf->priv->dmpi->stride[1]=mpi->stride[1];
|
|
225 vf->priv->dmpi->stride[2]=mpi->stride[2];
|
|
226 vf->priv->dmpi->width=mpi->width;
|
|
227 vf->priv->dmpi->height=mpi->height;
|
|
228
|
|
229 if(vf->priv->shot) {
|
16450
|
230 vf->priv->shot=0;
|
|
231 gen_fname(vf->priv);
|
|
232 scale_image(vf->priv);
|
|
233 write_png(vf->priv->fname, vf->priv->buffer, vf->priv->dw, vf->priv->dh, vf->priv->stride);
|
16446
|
234 }
|
|
235
|
|
236 return vf_next_put_image(vf, vf->priv->dmpi);
|
|
237 }
|
|
238
|
|
239 int control (vf_instance_t *vf, int request, void *data)
|
|
240 {
|
|
241 if(request==VFCTRL_SCREENSHOT) {
|
|
242 vf->priv->shot=1;
|
|
243 return CONTROL_TRUE;
|
|
244 }
|
|
245 return vf_next_control (vf, request, data);
|
|
246 }
|
|
247
|
|
248
|
|
249 //===========================================================================//
|
|
250
|
|
251 static int query_format(struct vf_instance_s* vf, unsigned int fmt)
|
|
252 {
|
|
253 switch(fmt){
|
|
254 case IMGFMT_YV12:
|
|
255 case IMGFMT_I420:
|
|
256 case IMGFMT_IYUV:
|
|
257 case IMGFMT_UYVY:
|
|
258 case IMGFMT_YUY2:
|
|
259 case IMGFMT_BGR32:
|
|
260 case IMGFMT_BGR24:
|
|
261 case IMGFMT_BGR16:
|
|
262 case IMGFMT_BGR15:
|
|
263 case IMGFMT_RGB32:
|
|
264 case IMGFMT_RGB24:
|
|
265 case IMGFMT_Y800:
|
|
266 case IMGFMT_Y8:
|
|
267 case IMGFMT_YVU9:
|
|
268 case IMGFMT_IF09:
|
|
269 case IMGFMT_444P:
|
|
270 case IMGFMT_422P:
|
|
271 case IMGFMT_411P:
|
|
272 return vf_next_query_format(vf, fmt);
|
|
273 }
|
|
274 return 0;
|
|
275 }
|
|
276
|
|
277 static int open(vf_instance_t *vf, char* args)
|
|
278 {
|
|
279 vf->config=config;
|
|
280 vf->control=control;
|
|
281 vf->put_image=put_image;
|
|
282 vf->query_format=query_format;
|
16450
|
283 vf->start_slice=start_slice;
|
|
284 vf->draw_slice=draw_slice;
|
|
285 vf->get_image=get_image;
|
16446
|
286 vf->priv=malloc(sizeof(struct vf_priv_s));
|
|
287 vf->priv->frameno=0;
|
|
288 vf->priv->shot=0;
|
16450
|
289 vf->priv->store_slices=0;
|
16446
|
290 vf->priv->buffer=0;
|
|
291 vf->priv->ctx=0;
|
|
292 return 1;
|
|
293 }
|
|
294
|
|
295 static void uninit(vf_instance_t *vf)
|
|
296 {
|
|
297 if(vf->priv->ctx) sws_freeContext(vf->priv->ctx);
|
|
298 if (vf->priv->buffer) free(vf->priv->buffer);
|
|
299 free(vf->priv);
|
|
300 }
|
|
301
|
|
302
|
|
303 vf_info_t vf_info_screenshot = {
|
|
304 "screenshot to file",
|
|
305 "screenshot",
|
|
306 "A'rpi, Jindrich Makovicka",
|
|
307 "",
|
|
308 open,
|
|
309 NULL
|
|
310 };
|
|
311
|
|
312 //===========================================================================//
|
|
313
|
|
314 #endif /* HAVE_PNG */
|