11 #include <sys/types.h>
17 #include "img_format.h"
22 #include "libswscale/swscale.h"
23 #include "libavcodec/avcodec.h"
28 /// shot stores current screenshot mode:
29 /// 0: don't take screenshots
30 /// 1: take single screenshot, reset to 0 afterwards
31 /// 2: take screenshots of each frame
32 int shot
, store_slices
;
35 struct SwsContext
*ctx
;
36 AVCodecContext
*avctx
;
41 //===========================================================================//
43 static int config(struct vf_instance
* vf
,
44 int width
, int height
, int d_width
, int d_height
,
45 unsigned int flags
, unsigned int outfmt
)
47 vf
->priv
->ctx
=sws_getContextFromCmdLine(width
, height
, outfmt
,
48 d_width
, d_height
, IMGFMT_RGB24
);
50 vf
->priv
->outbuffer_size
= d_width
* d_height
* 3 * 2;
51 vf
->priv
->outbuffer
= realloc(vf
->priv
->outbuffer
, vf
->priv
->outbuffer_size
);
52 vf
->priv
->avctx
->width
= d_width
;
53 vf
->priv
->avctx
->height
= d_height
;
54 vf
->priv
->avctx
->pix_fmt
= PIX_FMT_RGB24
;
55 vf
->priv
->avctx
->compression_level
= 0;
56 vf
->priv
->dw
= d_width
;
57 vf
->priv
->dh
= d_height
;
58 vf
->priv
->stride
= (3*vf
->priv
->dw
+15)&~15;
60 if (vf
->priv
->buffer
) free(vf
->priv
->buffer
); // probably reconfigured
61 vf
->priv
->buffer
= NULL
;
63 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
66 static void write_png(struct vf_priv_s
*priv
)
68 char *fname
= priv
->fname
;
73 fp
= fopen (fname
, "wb");
75 mp_msg(MSGT_VFILTER
,MSGL_ERR
,"\nPNG Error opening %s for writing!\n", fname
);
79 pic
.data
[0] = priv
->buffer
;
80 pic
.linesize
[0] = priv
->stride
;
81 size
= avcodec_encode_video(priv
->avctx
, priv
->outbuffer
, priv
->outbuffer_size
, &pic
);
83 fwrite(priv
->outbuffer
, size
, 1, fp
);
88 static int fexists(char *fname
)
91 if (stat(fname
, &dummy
) == 0) return 1;
95 static void gen_fname(struct vf_priv_s
* priv
)
98 snprintf (priv
->fname
, 100, "shot%04d.png", ++priv
->frameno
);
99 } while (fexists(priv
->fname
) && priv
->frameno
< 100000);
100 if (fexists(priv
->fname
)) {
101 priv
->fname
[0] = '\0';
105 mp_msg(MSGT_VFILTER
,MSGL_INFO
,"*** screenshot '%s' ***\n",priv
->fname
);
109 static void scale_image(struct vf_priv_s
* priv
, mp_image_t
*mpi
)
111 uint8_t *dst
[MP_MAX_PLANES
] = {NULL
};
112 int dst_stride
[MP_MAX_PLANES
] = {0};
114 dst_stride
[0] = priv
->stride
;
116 priv
->buffer
= memalign(16, dst_stride
[0]*priv
->dh
);
118 dst
[0] = priv
->buffer
;
119 sws_scale_ordered(priv
->ctx
, mpi
->planes
, mpi
->stride
, 0, priv
->dh
, dst
, dst_stride
);
122 static void start_slice(struct vf_instance
* vf
, mp_image_t
*mpi
)
124 vf
->dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
125 mpi
->type
, mpi
->flags
, mpi
->width
, mpi
->height
);
126 if (vf
->priv
->shot
) {
127 vf
->priv
->store_slices
= 1;
128 if (!vf
->priv
->buffer
)
129 vf
->priv
->buffer
= memalign(16, vf
->priv
->stride
*vf
->priv
->dh
);
134 static void draw_slice(struct vf_instance
* vf
, unsigned char** src
,
135 int* stride
, int w
,int h
, int x
, int y
)
137 if (vf
->priv
->store_slices
) {
138 uint8_t *dst
[MP_MAX_PLANES
] = {NULL
};
139 int dst_stride
[MP_MAX_PLANES
] = {0};
140 dst_stride
[0] = vf
->priv
->stride
;
141 dst
[0] = vf
->priv
->buffer
;
142 sws_scale_ordered(vf
->priv
->ctx
, src
, stride
, y
, h
, dst
, dst_stride
);
144 vf_next_draw_slice(vf
,src
,stride
,w
,h
,x
,y
);
147 static void get_image(struct vf_instance
* vf
, mp_image_t
*mpi
)
149 // FIXME: should vf.c really call get_image when using slices??
150 if (mpi
->flags
& MP_IMGFLAG_DRAW_CALLBACK
)
152 vf
->dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
,
153 mpi
->type
, mpi
->flags
/* | MP_IMGFLAG_READABLE*/, mpi
->width
, mpi
->height
);
155 mpi
->planes
[0]=vf
->dmpi
->planes
[0];
156 mpi
->stride
[0]=vf
->dmpi
->stride
[0];
157 if(mpi
->flags
&MP_IMGFLAG_PLANAR
){
158 mpi
->planes
[1]=vf
->dmpi
->planes
[1];
159 mpi
->planes
[2]=vf
->dmpi
->planes
[2];
160 mpi
->stride
[1]=vf
->dmpi
->stride
[1];
161 mpi
->stride
[2]=vf
->dmpi
->stride
[2];
163 mpi
->width
=vf
->dmpi
->width
;
165 mpi
->flags
|=MP_IMGFLAG_DIRECT
;
167 mpi
->priv
=(void*)vf
->dmpi
;
170 static int put_image(struct vf_instance
* vf
, mp_image_t
*mpi
, double pts
)
172 mp_image_t
*dmpi
= (mp_image_t
*)mpi
->priv
;
174 if (mpi
->flags
& MP_IMGFLAG_DRAW_CALLBACK
)
177 if(!(mpi
->flags
&MP_IMGFLAG_DIRECT
)){
178 dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
179 MP_IMGTYPE_EXPORT
, 0,
180 mpi
->width
, mpi
->height
);
181 vf_clone_mpi_attributes(dmpi
, mpi
);
182 dmpi
->planes
[0]=mpi
->planes
[0];
183 dmpi
->planes
[1]=mpi
->planes
[1];
184 dmpi
->planes
[2]=mpi
->planes
[2];
185 dmpi
->stride
[0]=mpi
->stride
[0];
186 dmpi
->stride
[1]=mpi
->stride
[1];
187 dmpi
->stride
[2]=mpi
->stride
[2];
188 dmpi
->width
=mpi
->width
;
189 dmpi
->height
=mpi
->height
;
193 if (vf
->priv
->shot
==1)
196 if (vf
->priv
->fname
[0]) {
197 if (!vf
->priv
->store_slices
)
198 scale_image(vf
->priv
, dmpi
);
201 vf
->priv
->store_slices
= 0;
204 return vf_next_put_image(vf
, dmpi
, pts
);
207 static int control (vf_instance_t
*vf
, int request
, void *data
)
209 /** data contains an integer argument
210 * 0: take screenshot with the next frame
211 * 1: take screenshots with each frame until the same command is given once again
213 if(request
==VFCTRL_SCREENSHOT
) {
214 if (data
&& *(int*)data
) { // repeated screenshot mode
215 if (vf
->priv
->shot
==2)
219 } else { // single screenshot
225 return vf_next_control (vf
, request
, data
);
229 //===========================================================================//
231 static int query_format(struct vf_instance
* vf
, unsigned int fmt
)
252 return vf_next_query_format(vf
, fmt
);
257 static void uninit(vf_instance_t
*vf
)
259 av_freep(&vf
->priv
->avctx
);
260 if(vf
->priv
->ctx
) sws_freeContext(vf
->priv
->ctx
);
261 if (vf
->priv
->buffer
) free(vf
->priv
->buffer
);
262 free(vf
->priv
->outbuffer
);
266 // open conflicts with stdio.h at least under MinGW
267 static int screenshot_open(vf_instance_t
*vf
, char* args
)
271 vf
->put_image
=put_image
;
272 vf
->query_format
=query_format
;
273 vf
->start_slice
=start_slice
;
274 vf
->draw_slice
=draw_slice
;
275 vf
->get_image
=get_image
;
277 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
280 vf
->priv
->store_slices
=0;
282 vf
->priv
->outbuffer
=0;
284 vf
->priv
->avctx
= avcodec_alloc_context();
285 avcodec_register_all();
286 if (avcodec_open(vf
->priv
->avctx
, avcodec_find_encoder(CODEC_ID_PNG
))) {
287 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "Could not open libavcodec PNG encoder\n");
294 const vf_info_t vf_info_screenshot
= {
295 "screenshot to file",
297 "A'rpi, Jindrich Makovicka",
303 //===========================================================================//