Mercurial > mplayer.hg
annotate libmpcodecs/vf_screenshot.c @ 36371:bbf289ad03f2
vo_vdpau: Initialize variables before, not after setup code.
Fixes possible crashes when playing multiple videos without
-fixed-vo.
author | reimar |
---|---|
date | Tue, 15 Oct 2013 18:58:55 +0000 |
parents | 583523331410 |
children | 8a1186284090 |
rev | line source |
---|---|
30421
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
30150
diff
changeset
|
1 /* |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
30150
diff
changeset
|
2 * This file is part of MPlayer. |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
30150
diff
changeset
|
3 * |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
30150
diff
changeset
|
4 * MPlayer is free software; you can redistribute it and/or modify |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
30150
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
30150
diff
changeset
|
6 * the Free Software Foundation; either version 2 of the License, or |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
30150
diff
changeset
|
7 * (at your option) any later version. |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
30150
diff
changeset
|
8 * |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
30150
diff
changeset
|
9 * MPlayer is distributed in the hope that it will be useful, |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
30150
diff
changeset
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
30150
diff
changeset
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
30150
diff
changeset
|
12 * GNU General Public License for more details. |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
30150
diff
changeset
|
13 * |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
30150
diff
changeset
|
14 * You should have received a copy of the GNU General Public License along |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
30150
diff
changeset
|
15 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
30150
diff
changeset
|
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
30150
diff
changeset
|
17 */ |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
30150
diff
changeset
|
18 |
17012 | 19 #include "config.h" |
16446 | 20 |
21 #include <stdio.h> | |
22 #include <stdlib.h> | |
28594
df67d03dde3b
Convert HAVE_MALLOC_H into a 0/1 definition, fixes the warning:
diego
parents:
27949
diff
changeset
|
23 #if HAVE_MALLOC_H |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
24 #include <malloc.h> |
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
25 #endif |
16446 | 26 #include <string.h> |
27 #include <inttypes.h> | |
28 | |
29 #include <sys/types.h> | |
30 #include <sys/stat.h> | |
31 #include <unistd.h> | |
32 | |
17012 | 33 #include "mp_msg.h" |
16446 | 34 |
35 #include "img_format.h" | |
36 #include "mp_image.h" | |
37 #include "vf.h" | |
38 #include "vf_scale.h" | |
39 | |
35712
d206960484fe
Add a number of missing libavutil header #includes.
diego
parents:
35114
diff
changeset
|
40 #include "libavutil/mem.h" |
18861 | 41 #include "libswscale/swscale.h" |
24731 | 42 #include "libavcodec/avcodec.h" |
43 | |
16446 | 44 struct vf_priv_s { |
45 int frameno; | |
16450 | 46 char fname[102]; |
19833 | 47 /// shot stores current screenshot mode: |
48 /// 0: don't take screenshots | |
49 /// 1: take single screenshot, reset to 0 afterwards | |
50 /// 2: take screenshots of each frame | |
16450 | 51 int shot, store_slices; |
52 int dw, dh, stride; | |
16446 | 53 uint8_t *buffer; |
54 struct SwsContext *ctx; | |
24731 | 55 AVCodecContext *avctx; |
56 uint8_t *outbuffer; | |
57 int outbuffer_size; | |
16446 | 58 }; |
59 | |
60 //===========================================================================// | |
61 | |
36309 | 62 static void draw_slice(struct vf_instance *vf, unsigned char** src, |
63 int* stride, int w,int h, int x, int y) | |
64 { | |
65 if (vf->priv->store_slices) { | |
66 uint8_t *dst[MP_MAX_PLANES] = {NULL}; | |
67 int dst_stride[MP_MAX_PLANES] = {0}; | |
68 dst_stride[0] = vf->priv->stride; | |
69 dst[0] = vf->priv->buffer; | |
70 sws_scale(vf->priv->ctx, src, stride, y, h, dst, dst_stride); | |
71 } | |
72 vf_next_draw_slice(vf,src,stride,w,h,x,y); | |
73 } | |
74 | |
30642
a972c1a4a012
cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents:
30638
diff
changeset
|
75 static int config(struct vf_instance *vf, |
27947 | 76 int width, int height, int d_width, int d_height, |
77 unsigned int flags, unsigned int outfmt) | |
16446 | 78 { |
36309 | 79 int res; |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
80 vf->priv->ctx=sws_getContextFromCmdLine(width, height, outfmt, |
27947 | 81 d_width, d_height, IMGFMT_RGB24); |
16446 | 82 |
24731 | 83 vf->priv->outbuffer_size = d_width * d_height * 3 * 2; |
84 vf->priv->outbuffer = realloc(vf->priv->outbuffer, vf->priv->outbuffer_size); | |
85 vf->priv->avctx->width = d_width; | |
86 vf->priv->avctx->height = d_height; | |
87 vf->priv->avctx->compression_level = 0; | |
16446 | 88 vf->priv->dw = d_width; |
89 vf->priv->dh = d_height; | |
16450 | 90 vf->priv->stride = (3*vf->priv->dw+15)&~15; |
16446 | 91 |
32537
8fa2f43cb760
Remove most of the NULL pointer check before free all over the code
cboesch
parents:
31082
diff
changeset
|
92 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
|
93 vf->priv->buffer = NULL; |
16446 | 94 |
36309 | 95 res = vf_next_config(vf,width,height,d_width,d_height,flags,outfmt); |
96 // Our draw_slice only works properly if the | |
97 // following filter can do slices. | |
98 vf->draw_slice=vf->next->draw_slice ? draw_slice : NULL; | |
99 return res; | |
16446 | 100 } |
101 | |
24731 | 102 static void write_png(struct vf_priv_s *priv) |
16446 | 103 { |
24731 | 104 char *fname = priv->fname; |
16446 | 105 FILE * fp; |
24731 | 106 AVFrame pic; |
107 int size; | |
16446 | 108 |
109 fp = fopen (fname, "wb"); | |
110 if (fp == NULL) { | |
27947 | 111 mp_msg(MSGT_VFILTER,MSGL_ERR,"\nPNG Error opening %s for writing!\n", fname); |
112 return; | |
16446 | 113 } |
27947 | 114 |
24731 | 115 pic.data[0] = priv->buffer; |
116 pic.linesize[0] = priv->stride; | |
117 size = avcodec_encode_video(priv->avctx, priv->outbuffer, priv->outbuffer_size, &pic); | |
118 if (size > 0) | |
119 fwrite(priv->outbuffer, size, 1, fp); | |
16446 | 120 |
121 fclose (fp); | |
122 } | |
123 | |
124 static int fexists(char *fname) | |
125 { | |
126 struct stat dummy; | |
127 if (stat(fname, &dummy) == 0) return 1; | |
128 else return 0; | |
129 } | |
130 | |
16450 | 131 static void gen_fname(struct vf_priv_s* priv) |
16446 | 132 { |
16450 | 133 do { |
27947 | 134 snprintf (priv->fname, 100, "shot%04d.png", ++priv->frameno); |
16450 | 135 } while (fexists(priv->fname) && priv->frameno < 100000); |
16457 | 136 if (fexists(priv->fname)) { |
27947 | 137 priv->fname[0] = '\0'; |
138 return; | |
16457 | 139 } |
16450 | 140 |
141 mp_msg(MSGT_VFILTER,MSGL_INFO,"*** screenshot '%s' ***\n",priv->fname); | |
142 | |
143 } | |
144 | |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
145 static void scale_image(struct vf_priv_s* priv, mp_image_t *mpi) |
16450 | 146 { |
29064
67c256364220
Consistently use MP_MAX_PLANES as size for plane pointer/stride arrays in libmpcodecs.
reimar
parents:
28594
diff
changeset
|
147 uint8_t *dst[MP_MAX_PLANES] = {NULL}; |
67c256364220
Consistently use MP_MAX_PLANES as size for plane pointer/stride arrays in libmpcodecs.
reimar
parents:
28594
diff
changeset
|
148 int dst_stride[MP_MAX_PLANES] = {0}; |
27947 | 149 |
16450 | 150 dst_stride[0] = priv->stride; |
16446 | 151 if (!priv->buffer) |
31003
00825525514e
Replace memalign(x) (x > 8) by av_malloc() to prevent crashes on systems
zuxy
parents:
30642
diff
changeset
|
152 priv->buffer = av_malloc(dst_stride[0]*priv->dh); |
16446 | 153 |
154 dst[0] = priv->buffer; | |
30150
16c71b965952
Replace deprecated sws_scale_ordered usages by sws_scale (which does the same).
reimar
parents:
30143
diff
changeset
|
155 sws_scale(priv->ctx, mpi->planes, mpi->stride, 0, priv->dh, dst, dst_stride); |
16450 | 156 } |
16446 | 157 |
30642
a972c1a4a012
cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents:
30638
diff
changeset
|
158 static void start_slice(struct vf_instance *vf, mp_image_t *mpi) |
27948 | 159 { |
35114
bec9e755fb9b
Fix handling of out-of-order slice rendered frames.
reimar
parents:
34659
diff
changeset
|
160 mpi->priv= |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
161 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt, |
27947 | 162 mpi->type, mpi->flags, mpi->width, mpi->height); |
16450 | 163 if (vf->priv->shot) { |
27947 | 164 vf->priv->store_slices = 1; |
165 if (!vf->priv->buffer) | |
31003
00825525514e
Replace memalign(x) (x > 8) by av_malloc() to prevent crashes on systems
zuxy
parents:
30642
diff
changeset
|
166 vf->priv->buffer = av_malloc(vf->priv->stride*vf->priv->dh); |
16450 | 167 } |
27947 | 168 |
16450 | 169 } |
170 | |
30642
a972c1a4a012
cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents:
30638
diff
changeset
|
171 static void get_image(struct vf_instance *vf, mp_image_t *mpi) |
27948 | 172 { |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
173 // 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
|
174 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
|
175 return; |
27947 | 176 vf->dmpi= vf_get_image(vf->next, mpi->imgfmt, |
177 mpi->type, mpi->flags/* | MP_IMGFLAG_READABLE*/, mpi->width, mpi->height); | |
16456 | 178 |
16450 | 179 mpi->planes[0]=vf->dmpi->planes[0]; |
180 mpi->stride[0]=vf->dmpi->stride[0]; | |
181 if(mpi->flags&MP_IMGFLAG_PLANAR){ | |
27947 | 182 mpi->planes[1]=vf->dmpi->planes[1]; |
183 mpi->planes[2]=vf->dmpi->planes[2]; | |
184 mpi->stride[1]=vf->dmpi->stride[1]; | |
185 mpi->stride[2]=vf->dmpi->stride[2]; | |
16450 | 186 } |
187 mpi->width=vf->dmpi->width; | |
188 | |
189 mpi->flags|=MP_IMGFLAG_DIRECT; | |
27947 | 190 |
16450 | 191 mpi->priv=(void*)vf->dmpi; |
16446 | 192 } |
193 | |
30642
a972c1a4a012
cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents:
30638
diff
changeset
|
194 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts) |
16446 | 195 { |
16464
0a828abf1f0f
Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents:
16457
diff
changeset
|
196 mp_image_t *dmpi = (mp_image_t *)mpi->priv; |
27947 | 197 |
35114
bec9e755fb9b
Fix handling of out-of-order slice rendered frames.
reimar
parents:
34659
diff
changeset
|
198 if(!(mpi->flags&(MP_IMGFLAG_DIRECT|MP_IMGFLAG_DRAW_CALLBACK))){ |
27947 | 199 dmpi=vf_get_image(vf->next,mpi->imgfmt, |
200 MP_IMGTYPE_EXPORT, 0, | |
201 mpi->width, mpi->height); | |
202 vf_clone_mpi_attributes(dmpi, mpi); | |
203 dmpi->planes[0]=mpi->planes[0]; | |
204 dmpi->planes[1]=mpi->planes[1]; | |
205 dmpi->planes[2]=mpi->planes[2]; | |
206 dmpi->stride[0]=mpi->stride[0]; | |
207 dmpi->stride[1]=mpi->stride[1]; | |
208 dmpi->stride[2]=mpi->stride[2]; | |
209 dmpi->width=mpi->width; | |
210 dmpi->height=mpi->height; | |
16450 | 211 } |
16446 | 212 |
213 if(vf->priv->shot) { | |
27947 | 214 if (vf->priv->shot==1) |
215 vf->priv->shot=0; | |
216 gen_fname(vf->priv); | |
217 if (vf->priv->fname[0]) { | |
218 if (!vf->priv->store_slices) | |
219 scale_image(vf->priv, dmpi); | |
220 write_png(vf->priv); | |
221 } | |
222 vf->priv->store_slices = 0; | |
16446 | 223 } |
224 | |
17906
20aca9baf5d8
passing pts through the filter layer (lets see if pts or cola comes out at the end)
michael
parents:
17012
diff
changeset
|
225 return vf_next_put_image(vf, dmpi, pts); |
16446 | 226 } |
227 | |
29702
28fb967d78d8
Mark some functions that do not need external visibility as static.
diego
parents:
29064
diff
changeset
|
228 static int control (vf_instance_t *vf, int request, void *data) |
16446 | 229 { |
19833 | 230 /** data contains an integer argument |
231 * 0: take screenshot with the next frame | |
232 * 1: take screenshots with each frame until the same command is given once again | |
233 **/ | |
16446 | 234 if(request==VFCTRL_SCREENSHOT) { |
27947 | 235 if (data && *(int*)data) { // repeated screenshot mode |
236 if (vf->priv->shot==2) | |
237 vf->priv->shot=0; | |
238 else | |
239 vf->priv->shot=2; | |
240 } else { // single screenshot | |
241 if (!vf->priv->shot) | |
242 vf->priv->shot=1; | |
243 } | |
16446 | 244 return CONTROL_TRUE; |
245 } | |
246 return vf_next_control (vf, request, data); | |
247 } | |
248 | |
249 | |
250 //===========================================================================// | |
251 | |
30642
a972c1a4a012
cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents:
30638
diff
changeset
|
252 static int query_format(struct vf_instance *vf, unsigned int fmt) |
16446 | 253 { |
254 switch(fmt){ | |
255 case IMGFMT_YV12: | |
256 case IMGFMT_I420: | |
257 case IMGFMT_IYUV: | |
258 case IMGFMT_UYVY: | |
259 case IMGFMT_YUY2: | |
260 case IMGFMT_BGR32: | |
261 case IMGFMT_BGR24: | |
262 case IMGFMT_BGR16: | |
263 case IMGFMT_BGR15: | |
31082
92f88bb315c5
Add support for 12-bit color mode on framebuffer devices.
cehoyos
parents:
31003
diff
changeset
|
264 case IMGFMT_BGR12: |
16446 | 265 case IMGFMT_RGB32: |
266 case IMGFMT_RGB24: | |
27947 | 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); | |
16446 | 275 } |
276 return 0; | |
277 } | |
278 | |
27949
88d793e52bd9
cosmetics: Move up uninit() to avoid a forward declaration.
diego
parents:
27948
diff
changeset
|
279 static void uninit(vf_instance_t *vf) |
88d793e52bd9
cosmetics: Move up uninit() to avoid a forward declaration.
diego
parents:
27948
diff
changeset
|
280 { |
30143 | 281 avcodec_close(vf->priv->avctx); |
27949
88d793e52bd9
cosmetics: Move up uninit() to avoid a forward declaration.
diego
parents:
27948
diff
changeset
|
282 av_freep(&vf->priv->avctx); |
88d793e52bd9
cosmetics: Move up uninit() to avoid a forward declaration.
diego
parents:
27948
diff
changeset
|
283 if(vf->priv->ctx) sws_freeContext(vf->priv->ctx); |
32537
8fa2f43cb760
Remove most of the NULL pointer check before free all over the code
cboesch
parents:
31082
diff
changeset
|
284 av_free(vf->priv->buffer); |
27949
88d793e52bd9
cosmetics: Move up uninit() to avoid a forward declaration.
diego
parents:
27948
diff
changeset
|
285 free(vf->priv->outbuffer); |
88d793e52bd9
cosmetics: Move up uninit() to avoid a forward declaration.
diego
parents:
27948
diff
changeset
|
286 free(vf->priv); |
88d793e52bd9
cosmetics: Move up uninit() to avoid a forward declaration.
diego
parents:
27948
diff
changeset
|
287 } |
88d793e52bd9
cosmetics: Move up uninit() to avoid a forward declaration.
diego
parents:
27948
diff
changeset
|
288 |
30638
a7b908875c14
Rename open() vf initialization function to vf_open().
diego
parents:
30421
diff
changeset
|
289 static int vf_open(vf_instance_t *vf, char *args) |
16446 | 290 { |
291 vf->config=config; | |
292 vf->control=control; | |
293 vf->put_image=put_image; | |
294 vf->query_format=query_format; | |
16450 | 295 vf->start_slice=start_slice; |
296 vf->draw_slice=draw_slice; | |
297 vf->get_image=get_image; | |
16874 | 298 vf->uninit=uninit; |
16446 | 299 vf->priv=malloc(sizeof(struct vf_priv_s)); |
300 vf->priv->frameno=0; | |
301 vf->priv->shot=0; | |
16450 | 302 vf->priv->store_slices=0; |
16446 | 303 vf->priv->buffer=0; |
24731 | 304 vf->priv->outbuffer=0; |
16446 | 305 vf->priv->ctx=0; |
34566
f3d53cd55376
Update deprecated avcodec_alloc_context()/avcodec_open() API calls
siretart
parents:
32537
diff
changeset
|
306 vf->priv->avctx = avcodec_alloc_context3(NULL); |
34659 | 307 vf->priv->avctx->pix_fmt = PIX_FMT_RGB24; |
24731 | 308 avcodec_register_all(); |
35715
8517826b0dbd
Replace CODEC_IDs their modern AV_-prefixed counterparts.
diego
parents:
35712
diff
changeset
|
309 if (avcodec_open2(vf->priv->avctx, avcodec_find_encoder(AV_CODEC_ID_PNG), NULL)) { |
24731 | 310 mp_msg(MSGT_VFILTER, MSGL_FATAL, "Could not open libavcodec PNG encoder\n"); |
311 return 0; | |
312 } | |
16446 | 313 return 1; |
314 } | |
315 | |
316 | |
25221 | 317 const vf_info_t vf_info_screenshot = { |
16446 | 318 "screenshot to file", |
319 "screenshot", | |
320 "A'rpi, Jindrich Makovicka", | |
321 "", | |
30638
a7b908875c14
Rename open() vf initialization function to vf_open().
diego
parents:
30421
diff
changeset
|
322 vf_open, |
16446 | 323 NULL |
324 }; | |
325 | |
326 //===========================================================================// |