Latest 9.1 ATI drivers finally fixed PBOs, thus do not need ati-hack and are much
[mplayer/glamo.git] / libmpcodecs / vf_screenshot.c
blob9d6b48d5bf95c038c2669c1e73ccd2e0ffa1e5ef
1 #include "config.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #ifdef HAVE_MALLOC_H
6 #include <malloc.h>
7 #endif
8 #include <string.h>
9 #include <inttypes.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <unistd.h>
15 #include "mp_msg.h"
17 #include "img_format.h"
18 #include "mp_image.h"
19 #include "vf.h"
20 #include "vf_scale.h"
22 #include "libswscale/swscale.h"
23 #include "libavcodec/avcodec.h"
25 struct vf_priv_s {
26 int frameno;
27 char fname[102];
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;
33 int dw, dh, stride;
34 uint8_t *buffer;
35 struct SwsContext *ctx;
36 AVCodecContext *avctx;
37 uint8_t *outbuffer;
38 int outbuffer_size;
41 //===========================================================================//
43 static int config(struct vf_instance_s* 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;
69 FILE * fp;
70 AVFrame pic;
71 int size;
73 fp = fopen (fname, "wb");
74 if (fp == NULL) {
75 mp_msg(MSGT_VFILTER,MSGL_ERR,"\nPNG Error opening %s for writing!\n", fname);
76 return;
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);
82 if (size > 0)
83 fwrite(priv->outbuffer, size, 1, fp);
85 fclose (fp);
88 static int fexists(char *fname)
90 struct stat dummy;
91 if (stat(fname, &dummy) == 0) return 1;
92 else return 0;
95 static void gen_fname(struct vf_priv_s* priv)
97 do {
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';
102 return;
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[3];
112 int dst_stride[3];
114 dst_stride[0] = priv->stride;
115 dst_stride[1] = dst_stride[2] = 0;
116 if (!priv->buffer)
117 priv->buffer = memalign(16, dst_stride[0]*priv->dh);
119 dst[0] = priv->buffer;
120 dst[1] = dst[2] = 0;
121 sws_scale_ordered(priv->ctx, mpi->planes, mpi->stride, 0, priv->dh, dst, dst_stride);
124 static void start_slice(struct vf_instance_s* vf, mp_image_t *mpi)
126 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
127 mpi->type, mpi->flags, mpi->width, mpi->height);
128 if (vf->priv->shot) {
129 vf->priv->store_slices = 1;
130 if (!vf->priv->buffer)
131 vf->priv->buffer = memalign(16, vf->priv->stride*vf->priv->dh);
136 static void draw_slice(struct vf_instance_s* vf, unsigned char** src,
137 int* stride, int w,int h, int x, int y)
139 if (vf->priv->store_slices) {
140 uint8_t *dst[3];
141 int dst_stride[3];
142 dst_stride[0] = vf->priv->stride;
143 dst_stride[1] = dst_stride[2] = 0;
144 dst[0] = vf->priv->buffer;
145 dst[1] = dst[2] = 0;
146 sws_scale_ordered(vf->priv->ctx, src, stride, y, h, dst, dst_stride);
148 vf_next_draw_slice(vf,src,stride,w,h,x,y);
151 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi)
153 // FIXME: should vf.c really call get_image when using slices??
154 if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
155 return;
156 vf->dmpi= vf_get_image(vf->next, mpi->imgfmt,
157 mpi->type, mpi->flags/* | MP_IMGFLAG_READABLE*/, mpi->width, mpi->height);
159 mpi->planes[0]=vf->dmpi->planes[0];
160 mpi->stride[0]=vf->dmpi->stride[0];
161 if(mpi->flags&MP_IMGFLAG_PLANAR){
162 mpi->planes[1]=vf->dmpi->planes[1];
163 mpi->planes[2]=vf->dmpi->planes[2];
164 mpi->stride[1]=vf->dmpi->stride[1];
165 mpi->stride[2]=vf->dmpi->stride[2];
167 mpi->width=vf->dmpi->width;
169 mpi->flags|=MP_IMGFLAG_DIRECT;
171 mpi->priv=(void*)vf->dmpi;
174 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts)
176 mp_image_t *dmpi = (mp_image_t *)mpi->priv;
178 if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
179 dmpi = vf->dmpi;
180 else
181 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
182 dmpi=vf_get_image(vf->next,mpi->imgfmt,
183 MP_IMGTYPE_EXPORT, 0,
184 mpi->width, mpi->height);
185 vf_clone_mpi_attributes(dmpi, mpi);
186 dmpi->planes[0]=mpi->planes[0];
187 dmpi->planes[1]=mpi->planes[1];
188 dmpi->planes[2]=mpi->planes[2];
189 dmpi->stride[0]=mpi->stride[0];
190 dmpi->stride[1]=mpi->stride[1];
191 dmpi->stride[2]=mpi->stride[2];
192 dmpi->width=mpi->width;
193 dmpi->height=mpi->height;
196 if(vf->priv->shot) {
197 if (vf->priv->shot==1)
198 vf->priv->shot=0;
199 gen_fname(vf->priv);
200 if (vf->priv->fname[0]) {
201 if (!vf->priv->store_slices)
202 scale_image(vf->priv, dmpi);
203 write_png(vf->priv);
205 vf->priv->store_slices = 0;
208 return vf_next_put_image(vf, dmpi, pts);
211 int control (vf_instance_t *vf, int request, void *data)
213 /** data contains an integer argument
214 * 0: take screenshot with the next frame
215 * 1: take screenshots with each frame until the same command is given once again
217 if(request==VFCTRL_SCREENSHOT) {
218 if (data && *(int*)data) { // repeated screenshot mode
219 if (vf->priv->shot==2)
220 vf->priv->shot=0;
221 else
222 vf->priv->shot=2;
223 } else { // single screenshot
224 if (!vf->priv->shot)
225 vf->priv->shot=1;
227 return CONTROL_TRUE;
229 return vf_next_control (vf, request, data);
233 //===========================================================================//
235 static int query_format(struct vf_instance_s* vf, unsigned int fmt)
237 switch(fmt){
238 case IMGFMT_YV12:
239 case IMGFMT_I420:
240 case IMGFMT_IYUV:
241 case IMGFMT_UYVY:
242 case IMGFMT_YUY2:
243 case IMGFMT_BGR32:
244 case IMGFMT_BGR24:
245 case IMGFMT_BGR16:
246 case IMGFMT_BGR15:
247 case IMGFMT_RGB32:
248 case IMGFMT_RGB24:
249 case IMGFMT_Y800:
250 case IMGFMT_Y8:
251 case IMGFMT_YVU9:
252 case IMGFMT_IF09:
253 case IMGFMT_444P:
254 case IMGFMT_422P:
255 case IMGFMT_411P:
256 return vf_next_query_format(vf, fmt);
258 return 0;
261 static void uninit(vf_instance_t *vf)
263 av_freep(&vf->priv->avctx);
264 if(vf->priv->ctx) sws_freeContext(vf->priv->ctx);
265 if (vf->priv->buffer) free(vf->priv->buffer);
266 free(vf->priv->outbuffer);
267 free(vf->priv);
270 // open conflicts with stdio.h at least under MinGW
271 static int screenshot_open(vf_instance_t *vf, char* args)
273 vf->config=config;
274 vf->control=control;
275 vf->put_image=put_image;
276 vf->query_format=query_format;
277 vf->start_slice=start_slice;
278 vf->draw_slice=draw_slice;
279 vf->get_image=get_image;
280 vf->uninit=uninit;
281 vf->priv=malloc(sizeof(struct vf_priv_s));
282 vf->priv->frameno=0;
283 vf->priv->shot=0;
284 vf->priv->store_slices=0;
285 vf->priv->buffer=0;
286 vf->priv->outbuffer=0;
287 vf->priv->ctx=0;
288 vf->priv->avctx = avcodec_alloc_context();
289 avcodec_register_all();
290 if (avcodec_open(vf->priv->avctx, avcodec_find_encoder(CODEC_ID_PNG))) {
291 mp_msg(MSGT_VFILTER, MSGL_FATAL, "Could not open libavcodec PNG encoder\n");
292 return 0;
294 return 1;
298 const vf_info_t vf_info_screenshot = {
299 "screenshot to file",
300 "screenshot",
301 "A'rpi, Jindrich Makovicka",
303 screenshot_open,
304 NULL
307 //===========================================================================//