2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <sys/types.h>
35 #include "img_format.h"
40 #include "libswscale/swscale.h"
41 #include "libavcodec/avcodec.h"
46 /// shot stores current screenshot mode:
47 /// 0: don't take screenshots
48 /// 1: take single screenshot, reset to 0 afterwards
49 /// 2: take screenshots of each frame
50 int shot
, store_slices
;
53 struct SwsContext
*ctx
;
54 AVCodecContext
*avctx
;
59 //===========================================================================//
61 static int config(struct vf_instance
*vf
,
62 int width
, int height
, int d_width
, int d_height
,
63 unsigned int flags
, unsigned int outfmt
)
65 vf
->priv
->ctx
=sws_getContextFromCmdLine(width
, height
, outfmt
,
66 d_width
, d_height
, IMGFMT_RGB24
);
68 vf
->priv
->outbuffer_size
= d_width
* d_height
* 3 * 2;
69 vf
->priv
->outbuffer
= realloc(vf
->priv
->outbuffer
, vf
->priv
->outbuffer_size
);
70 vf
->priv
->avctx
->width
= d_width
;
71 vf
->priv
->avctx
->height
= d_height
;
72 vf
->priv
->avctx
->pix_fmt
= PIX_FMT_RGB24
;
73 vf
->priv
->avctx
->compression_level
= 0;
74 vf
->priv
->dw
= d_width
;
75 vf
->priv
->dh
= d_height
;
76 vf
->priv
->stride
= (3*vf
->priv
->dw
+15)&~15;
78 free(vf
->priv
->buffer
); // probably reconfigured
79 vf
->priv
->buffer
= NULL
;
81 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
84 static void write_png(struct vf_priv_s
*priv
)
86 char *fname
= priv
->fname
;
91 fp
= fopen (fname
, "wb");
93 mp_msg(MSGT_VFILTER
,MSGL_ERR
,"\nPNG Error opening %s for writing!\n", fname
);
97 pic
.data
[0] = priv
->buffer
;
98 pic
.linesize
[0] = priv
->stride
;
99 size
= avcodec_encode_video(priv
->avctx
, priv
->outbuffer
, priv
->outbuffer_size
, &pic
);
101 fwrite(priv
->outbuffer
, size
, 1, fp
);
106 static int fexists(char *fname
)
109 if (stat(fname
, &dummy
) == 0) return 1;
113 static void gen_fname(struct vf_priv_s
* priv
)
116 snprintf (priv
->fname
, 100, "shot%04d.png", ++priv
->frameno
);
117 } while (fexists(priv
->fname
) && priv
->frameno
< 100000);
118 if (fexists(priv
->fname
)) {
119 priv
->fname
[0] = '\0';
123 mp_msg(MSGT_VFILTER
,MSGL_INFO
,"*** screenshot '%s' ***\n",priv
->fname
);
127 static void scale_image(struct vf_priv_s
* priv
, mp_image_t
*mpi
)
129 uint8_t *dst
[MP_MAX_PLANES
] = {NULL
};
130 int dst_stride
[MP_MAX_PLANES
] = {0};
132 dst_stride
[0] = priv
->stride
;
134 priv
->buffer
= av_malloc(dst_stride
[0]*priv
->dh
);
136 dst
[0] = priv
->buffer
;
137 sws_scale(priv
->ctx
, mpi
->planes
, mpi
->stride
, 0, priv
->dh
, dst
, dst_stride
);
140 static void start_slice(struct vf_instance
*vf
, mp_image_t
*mpi
)
142 vf
->dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
143 mpi
->type
, mpi
->flags
, mpi
->width
, mpi
->height
);
144 if (vf
->priv
->shot
) {
145 vf
->priv
->store_slices
= 1;
146 if (!vf
->priv
->buffer
)
147 vf
->priv
->buffer
= av_malloc(vf
->priv
->stride
*vf
->priv
->dh
);
152 static void draw_slice(struct vf_instance
*vf
, unsigned char** src
,
153 int* stride
, int w
,int h
, int x
, int y
)
155 if (vf
->priv
->store_slices
) {
156 uint8_t *dst
[MP_MAX_PLANES
] = {NULL
};
157 int dst_stride
[MP_MAX_PLANES
] = {0};
158 dst_stride
[0] = vf
->priv
->stride
;
159 dst
[0] = vf
->priv
->buffer
;
160 sws_scale(vf
->priv
->ctx
, src
, stride
, y
, h
, dst
, dst_stride
);
162 vf_next_draw_slice(vf
,src
,stride
,w
,h
,x
,y
);
165 static void get_image(struct vf_instance
*vf
, mp_image_t
*mpi
)
167 // FIXME: should vf.c really call get_image when using slices??
168 if (mpi
->flags
& MP_IMGFLAG_DRAW_CALLBACK
)
170 vf
->dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
,
171 mpi
->type
, mpi
->flags
/* | MP_IMGFLAG_READABLE*/, mpi
->width
, mpi
->height
);
173 mpi
->planes
[0]=vf
->dmpi
->planes
[0];
174 mpi
->stride
[0]=vf
->dmpi
->stride
[0];
175 if(mpi
->flags
&MP_IMGFLAG_PLANAR
){
176 mpi
->planes
[1]=vf
->dmpi
->planes
[1];
177 mpi
->planes
[2]=vf
->dmpi
->planes
[2];
178 mpi
->stride
[1]=vf
->dmpi
->stride
[1];
179 mpi
->stride
[2]=vf
->dmpi
->stride
[2];
181 mpi
->width
=vf
->dmpi
->width
;
183 mpi
->flags
|=MP_IMGFLAG_DIRECT
;
185 mpi
->priv
=(void*)vf
->dmpi
;
188 static int put_image(struct vf_instance
*vf
, mp_image_t
*mpi
, double pts
)
190 mp_image_t
*dmpi
= (mp_image_t
*)mpi
->priv
;
192 if (mpi
->flags
& MP_IMGFLAG_DRAW_CALLBACK
)
195 if(!(mpi
->flags
&MP_IMGFLAG_DIRECT
)){
196 dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
197 MP_IMGTYPE_EXPORT
, 0,
198 mpi
->width
, mpi
->height
);
199 vf_clone_mpi_attributes(dmpi
, mpi
);
200 dmpi
->planes
[0]=mpi
->planes
[0];
201 dmpi
->planes
[1]=mpi
->planes
[1];
202 dmpi
->planes
[2]=mpi
->planes
[2];
203 dmpi
->stride
[0]=mpi
->stride
[0];
204 dmpi
->stride
[1]=mpi
->stride
[1];
205 dmpi
->stride
[2]=mpi
->stride
[2];
206 dmpi
->width
=mpi
->width
;
207 dmpi
->height
=mpi
->height
;
211 if (vf
->priv
->shot
==1)
214 if (vf
->priv
->fname
[0]) {
215 if (!vf
->priv
->store_slices
)
216 scale_image(vf
->priv
, dmpi
);
219 vf
->priv
->store_slices
= 0;
222 return vf_next_put_image(vf
, dmpi
, pts
);
225 static int control (vf_instance_t
*vf
, int request
, void *data
)
227 /** data contains an integer argument
228 * 0: take screenshot with the next frame
229 * 1: take screenshots with each frame until the same command is given once again
231 if(request
==VFCTRL_SCREENSHOT
) {
232 if (data
&& *(int*)data
) { // repeated screenshot mode
233 if (vf
->priv
->shot
==2)
237 } else { // single screenshot
243 return vf_next_control (vf
, request
, data
);
247 //===========================================================================//
249 static int query_format(struct vf_instance
*vf
, unsigned int fmt
)
271 return vf_next_query_format(vf
, fmt
);
276 static void uninit(vf_instance_t
*vf
)
278 avcodec_close(vf
->priv
->avctx
);
279 av_freep(&vf
->priv
->avctx
);
280 if(vf
->priv
->ctx
) sws_freeContext(vf
->priv
->ctx
);
281 av_free(vf
->priv
->buffer
);
282 free(vf
->priv
->outbuffer
);
286 static int vf_open(vf_instance_t
*vf
, char *args
)
290 vf
->put_image
=put_image
;
291 vf
->query_format
=query_format
;
292 vf
->start_slice
=start_slice
;
293 vf
->draw_slice
=draw_slice
;
294 vf
->get_image
=get_image
;
296 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
299 vf
->priv
->store_slices
=0;
301 vf
->priv
->outbuffer
=0;
303 vf
->priv
->avctx
= avcodec_alloc_context();
304 avcodec_register_all();
305 if (avcodec_open(vf
->priv
->avctx
, avcodec_find_encoder(CODEC_ID_PNG
))) {
306 mp_msg(MSGT_VFILTER
, MSGL_FATAL
, "Could not open libavcodec PNG encoder\n");
313 const vf_info_t vf_info_screenshot
= {
314 "screenshot to file",
316 "A'rpi, Jindrich Makovicka",
322 //===========================================================================//