annotate libmpcodecs/vf_screenshot.c @ 36757:18518cb8a99e

vf_screenshot: Fix memory leak on resolution/aspect changes.
author reimar
date Sun, 16 Feb 2014 14:47:36 +0000
parents 6e78b4f9569f
children 0fa1993a31c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16874
diff changeset
19 #include "config.h"
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
20
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
21 #include <stdio.h>
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
22 #include <stdlib.h>
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
23 #include <string.h>
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
24 #include <inttypes.h>
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
25
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
26 #include <sys/types.h>
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
27 #include <sys/stat.h>
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
28 #include <unistd.h>
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
29
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16874
diff changeset
30 #include "mp_msg.h"
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
31
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
32 #include "img_format.h"
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
33 #include "mp_image.h"
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
34 #include "vf.h"
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
35 #include "vf_scale.h"
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
36
35712
d206960484fe Add a number of missing libavutil header #includes.
diego
parents: 35114
diff changeset
37 #include "libavutil/mem.h"
18861
8579acff875e Move postproc ---> libswscale
lucabe
parents: 18771
diff changeset
38 #include "libswscale/swscale.h"
24731
1d9ea74f79a0 Make vf_screenshot use the libavcodec PNG encoder
reimar
parents: 23373
diff changeset
39 #include "libavcodec/avcodec.h"
1d9ea74f79a0 Make vf_screenshot use the libavcodec PNG encoder
reimar
parents: 23373
diff changeset
40
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
41 struct vf_priv_s {
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
42 int frameno;
36752
af146b0d33f0 Allow specifying custom path and name for screenshot files.
reimar
parents: 36751
diff changeset
43 char fname[PATH_MAX];
af146b0d33f0 Allow specifying custom path and name for screenshot files.
reimar
parents: 36751
diff changeset
44 char *prefix;
19833
10f36060390a Add repeated screenshot mode to vf_screenshot.
eugeni
parents: 18879
diff changeset
45 /// shot stores current screenshot mode:
10f36060390a Add repeated screenshot mode to vf_screenshot.
eugeni
parents: 18879
diff changeset
46 /// 0: don't take screenshots
10f36060390a Add repeated screenshot mode to vf_screenshot.
eugeni
parents: 18879
diff changeset
47 /// 1: take single screenshot, reset to 0 afterwards
10f36060390a Add repeated screenshot mode to vf_screenshot.
eugeni
parents: 18879
diff changeset
48 /// 2: take screenshots of each frame
16450
40222952440f DR and slice support
henry
parents: 16446
diff changeset
49 int shot, store_slices;
36753
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
50 int dw, dh;
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
51 AVFrame *pic;
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
52 struct SwsContext *ctx;
24731
1d9ea74f79a0 Make vf_screenshot use the libavcodec PNG encoder
reimar
parents: 23373
diff changeset
53 AVCodecContext *avctx;
1d9ea74f79a0 Make vf_screenshot use the libavcodec PNG encoder
reimar
parents: 23373
diff changeset
54 uint8_t *outbuffer;
1d9ea74f79a0 Make vf_screenshot use the libavcodec PNG encoder
reimar
parents: 23373
diff changeset
55 int outbuffer_size;
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
56 };
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
57
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
58 //===========================================================================//
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
59
36309
583523331410 vf_screenshot: slice handling fix.
reimar
parents: 35715
diff changeset
60 static void draw_slice(struct vf_instance *vf, unsigned char** src,
583523331410 vf_screenshot: slice handling fix.
reimar
parents: 35715
diff changeset
61 int* stride, int w,int h, int x, int y)
583523331410 vf_screenshot: slice handling fix.
reimar
parents: 35715
diff changeset
62 {
583523331410 vf_screenshot: slice handling fix.
reimar
parents: 35715
diff changeset
63 if (vf->priv->store_slices) {
36753
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
64 sws_scale(vf->priv->ctx, src, stride, y, h, vf->priv->pic->data, vf->priv->pic->linesize);
36309
583523331410 vf_screenshot: slice handling fix.
reimar
parents: 35715
diff changeset
65 }
583523331410 vf_screenshot: slice handling fix.
reimar
parents: 35715
diff changeset
66 vf_next_draw_slice(vf,src,stride,w,h,x,y);
583523331410 vf_screenshot: slice handling fix.
reimar
parents: 35715
diff changeset
67 }
583523331410 vf_screenshot: slice handling fix.
reimar
parents: 35715
diff changeset
68
30642
a972c1a4a012 cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents: 30638
diff changeset
69 static int config(struct vf_instance *vf,
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
70 int width, int height, int d_width, int d_height,
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
71 unsigned int flags, unsigned int outfmt)
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
72 {
36309
583523331410 vf_screenshot: slice handling fix.
reimar
parents: 35715
diff changeset
73 int res;
36757
18518cb8a99e vf_screenshot: Fix memory leak on resolution/aspect changes.
reimar
parents: 36756
diff changeset
74 if (vf->priv->ctx) sws_freeContext(vf->priv->ctx);
16464
0a828abf1f0f Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents: 16457
diff changeset
75 vf->priv->ctx=sws_getContextFromCmdLine(width, height, outfmt,
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
76 d_width, d_height, IMGFMT_RGB24);
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
77
36755
1644cfb31b2f vf_screenshot: use av_fast_malloc.
reimar
parents: 36754
diff changeset
78 av_fast_malloc(&vf->priv->outbuffer, &vf->priv->outbuffer_size, d_width * d_height * 3 * 2);
24731
1d9ea74f79a0 Make vf_screenshot use the libavcodec PNG encoder
reimar
parents: 23373
diff changeset
79 vf->priv->avctx->width = d_width;
1d9ea74f79a0 Make vf_screenshot use the libavcodec PNG encoder
reimar
parents: 23373
diff changeset
80 vf->priv->avctx->height = d_height;
1d9ea74f79a0 Make vf_screenshot use the libavcodec PNG encoder
reimar
parents: 23373
diff changeset
81 vf->priv->avctx->compression_level = 0;
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
82 vf->priv->dw = d_width;
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
83 vf->priv->dh = d_height;
36753
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
84 vf->priv->pic->linesize[0] = (3*vf->priv->dw+15)&~15;
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
85
36753
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
86 av_freep(&vf->priv->pic->data[0]); // probably reconfigured
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
87
36309
583523331410 vf_screenshot: slice handling fix.
reimar
parents: 35715
diff changeset
88 res = vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
583523331410 vf_screenshot: slice handling fix.
reimar
parents: 35715
diff changeset
89 // Our draw_slice only works properly if the
583523331410 vf_screenshot: slice handling fix.
reimar
parents: 35715
diff changeset
90 // following filter can do slices.
583523331410 vf_screenshot: slice handling fix.
reimar
parents: 35715
diff changeset
91 vf->draw_slice=vf->next->draw_slice ? draw_slice : NULL;
583523331410 vf_screenshot: slice handling fix.
reimar
parents: 35715
diff changeset
92 return res;
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
93 }
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
94
24731
1d9ea74f79a0 Make vf_screenshot use the libavcodec PNG encoder
reimar
parents: 23373
diff changeset
95 static void write_png(struct vf_priv_s *priv)
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
96 {
24731
1d9ea74f79a0 Make vf_screenshot use the libavcodec PNG encoder
reimar
parents: 23373
diff changeset
97 char *fname = priv->fname;
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
98 FILE * fp;
36753
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
99 AVPacket pkt;
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
100 int res, got_pkt;
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
101
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
102 fp = fopen (fname, "wb");
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
103 if (fp == NULL) {
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
104 mp_msg(MSGT_VFILTER,MSGL_ERR,"\nPNG Error opening %s for writing!\n", fname);
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
105 return;
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
106 }
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
107
36753
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
108 av_init_packet(&pkt);
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
109 pkt.data = priv->outbuffer;
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
110 pkt.size = priv->outbuffer_size;
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
111 res = avcodec_encode_video2(priv->avctx, &pkt, priv->pic, &got_pkt);
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
112 if (res >= 0 && got_pkt && pkt.size > 0)
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
113 fwrite(priv->outbuffer, pkt.size, 1, fp);
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
114
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
115 fclose (fp);
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
116 }
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
117
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
118 static int fexists(char *fname)
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
119 {
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
120 struct stat dummy;
36750
8a1186284090 Remove pointless "if".
reimar
parents: 36309
diff changeset
121 return stat(fname, &dummy) == 0;
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
122 }
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
123
16450
40222952440f DR and slice support
henry
parents: 16446
diff changeset
124 static void gen_fname(struct vf_priv_s* priv)
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
125 {
16450
40222952440f DR and slice support
henry
parents: 16446
diff changeset
126 do {
36752
af146b0d33f0 Allow specifying custom path and name for screenshot files.
reimar
parents: 36751
diff changeset
127 snprintf(priv->fname, sizeof(priv->fname), "%s%04d.png", priv->prefix, ++priv->frameno);
16450
40222952440f DR and slice support
henry
parents: 16446
diff changeset
128 } while (fexists(priv->fname) && priv->frameno < 100000);
16457
5ab14fa7a019 do nothing if no free filenames are available
henry
parents: 16456
diff changeset
129 if (fexists(priv->fname)) {
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
130 priv->fname[0] = '\0';
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
131 return;
16457
5ab14fa7a019 do nothing if no free filenames are available
henry
parents: 16456
diff changeset
132 }
16450
40222952440f DR and slice support
henry
parents: 16446
diff changeset
133
40222952440f DR and slice support
henry
parents: 16446
diff changeset
134 mp_msg(MSGT_VFILTER,MSGL_INFO,"*** screenshot '%s' ***\n",priv->fname);
40222952440f DR and slice support
henry
parents: 16446
diff changeset
135
40222952440f DR and slice support
henry
parents: 16446
diff changeset
136 }
40222952440f DR and slice support
henry
parents: 16446
diff changeset
137
16464
0a828abf1f0f Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents: 16457
diff changeset
138 static void scale_image(struct vf_priv_s* priv, mp_image_t *mpi)
16450
40222952440f DR and slice support
henry
parents: 16446
diff changeset
139 {
36753
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
140 if (!priv->pic->data[0])
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
141 priv->pic->data[0] = av_malloc(priv->pic->linesize[0]*priv->dh);
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
142
36753
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
143 sws_scale(priv->ctx, mpi->planes, mpi->stride, 0, priv->dh, priv->pic->data, priv->pic->linesize);
16450
40222952440f DR and slice support
henry
parents: 16446
diff changeset
144 }
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
145
30642
a972c1a4a012 cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents: 30638
diff changeset
146 static void start_slice(struct vf_instance *vf, mp_image_t *mpi)
27948
6e485d467cfe cosmetics: consistent function declarations.
diego
parents: 27947
diff changeset
147 {
35114
bec9e755fb9b Fix handling of out-of-order slice rendered frames.
reimar
parents: 34659
diff changeset
148 mpi->priv=
16464
0a828abf1f0f Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents: 16457
diff changeset
149 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
150 mpi->type, mpi->flags, mpi->width, mpi->height);
16450
40222952440f DR and slice support
henry
parents: 16446
diff changeset
151 if (vf->priv->shot) {
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
152 vf->priv->store_slices = 1;
36753
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
153 if (!vf->priv->pic->data[0])
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
154 vf->priv->pic->data[0] = av_malloc(vf->priv->pic->linesize[0]*vf->priv->dh);
16450
40222952440f DR and slice support
henry
parents: 16446
diff changeset
155 }
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
156
16450
40222952440f DR and slice support
henry
parents: 16446
diff changeset
157 }
40222952440f DR and slice support
henry
parents: 16446
diff changeset
158
30642
a972c1a4a012 cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents: 30638
diff changeset
159 static void get_image(struct vf_instance *vf, mp_image_t *mpi)
27948
6e485d467cfe cosmetics: consistent function declarations.
diego
parents: 27947
diff changeset
160 {
16464
0a828abf1f0f Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents: 16457
diff changeset
161 // 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
162 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
163 return;
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
164 vf->dmpi= vf_get_image(vf->next, mpi->imgfmt,
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
165 mpi->type, mpi->flags/* | MP_IMGFLAG_READABLE*/, mpi->width, mpi->height);
16456
34eb986b7e4a use slices if DR isn't available
henry
parents: 16452
diff changeset
166
16450
40222952440f DR and slice support
henry
parents: 16446
diff changeset
167 mpi->planes[0]=vf->dmpi->planes[0];
40222952440f DR and slice support
henry
parents: 16446
diff changeset
168 mpi->stride[0]=vf->dmpi->stride[0];
40222952440f DR and slice support
henry
parents: 16446
diff changeset
169 if(mpi->flags&MP_IMGFLAG_PLANAR){
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
170 mpi->planes[1]=vf->dmpi->planes[1];
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
171 mpi->planes[2]=vf->dmpi->planes[2];
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
172 mpi->stride[1]=vf->dmpi->stride[1];
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
173 mpi->stride[2]=vf->dmpi->stride[2];
16450
40222952440f DR and slice support
henry
parents: 16446
diff changeset
174 }
40222952440f DR and slice support
henry
parents: 16446
diff changeset
175 mpi->width=vf->dmpi->width;
40222952440f DR and slice support
henry
parents: 16446
diff changeset
176
40222952440f DR and slice support
henry
parents: 16446
diff changeset
177 mpi->flags|=MP_IMGFLAG_DIRECT;
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
178
16450
40222952440f DR and slice support
henry
parents: 16446
diff changeset
179 mpi->priv=(void*)vf->dmpi;
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
180 }
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
181
30642
a972c1a4a012 cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents: 30638
diff changeset
182 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
183 {
16464
0a828abf1f0f Fix multiple issues: No picture at all, broken pictures, only every second
reimar
parents: 16457
diff changeset
184 mp_image_t *dmpi = (mp_image_t *)mpi->priv;
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
185
35114
bec9e755fb9b Fix handling of out-of-order slice rendered frames.
reimar
parents: 34659
diff changeset
186 if(!(mpi->flags&(MP_IMGFLAG_DIRECT|MP_IMGFLAG_DRAW_CALLBACK))){
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
187 dmpi=vf_get_image(vf->next,mpi->imgfmt,
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
188 MP_IMGTYPE_EXPORT, 0,
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
189 mpi->width, mpi->height);
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
190 vf_clone_mpi_attributes(dmpi, mpi);
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
191 dmpi->planes[0]=mpi->planes[0];
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
192 dmpi->planes[1]=mpi->planes[1];
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
193 dmpi->planes[2]=mpi->planes[2];
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
194 dmpi->stride[0]=mpi->stride[0];
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
195 dmpi->stride[1]=mpi->stride[1];
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
196 dmpi->stride[2]=mpi->stride[2];
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
197 dmpi->width=mpi->width;
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
198 dmpi->height=mpi->height;
16450
40222952440f DR and slice support
henry
parents: 16446
diff changeset
199 }
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
200
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
201 if(vf->priv->shot) {
36756
6e78b4f9569f vf_screenshot: Simplify repeat screenshot handling logic.
reimar
parents: 36755
diff changeset
202 vf->priv->shot &= ~1;
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
203 gen_fname(vf->priv);
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
204 if (vf->priv->fname[0]) {
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
205 if (!vf->priv->store_slices)
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
206 scale_image(vf->priv, dmpi);
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
207 write_png(vf->priv);
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
208 }
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
209 vf->priv->store_slices = 0;
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
210 }
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
211
17906
20aca9baf5d8 passing pts through the filter layer (lets see if pts or cola comes out at the end)
michael
parents: 17012
diff changeset
212 return vf_next_put_image(vf, dmpi, pts);
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
213 }
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
214
29702
28fb967d78d8 Mark some functions that do not need external visibility as static.
diego
parents: 29064
diff changeset
215 static int control (vf_instance_t *vf, int request, void *data)
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
216 {
19833
10f36060390a Add repeated screenshot mode to vf_screenshot.
eugeni
parents: 18879
diff changeset
217 /** data contains an integer argument
10f36060390a Add repeated screenshot mode to vf_screenshot.
eugeni
parents: 18879
diff changeset
218 * 0: take screenshot with the next frame
10f36060390a Add repeated screenshot mode to vf_screenshot.
eugeni
parents: 18879
diff changeset
219 * 1: take screenshots with each frame until the same command is given once again
10f36060390a Add repeated screenshot mode to vf_screenshot.
eugeni
parents: 18879
diff changeset
220 **/
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
221 if(request==VFCTRL_SCREENSHOT) {
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
222 if (data && *(int*)data) { // repeated screenshot mode
36756
6e78b4f9569f vf_screenshot: Simplify repeat screenshot handling logic.
reimar
parents: 36755
diff changeset
223 vf->priv->shot ^= 2;
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
224 } else { // single screenshot
36756
6e78b4f9569f vf_screenshot: Simplify repeat screenshot handling logic.
reimar
parents: 36755
diff changeset
225 vf->priv->shot |= 1;
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
226 }
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
227 return CONTROL_TRUE;
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
228 }
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
229 return vf_next_control (vf, request, data);
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
230 }
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
231
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
232
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
233 //===========================================================================//
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
234
30642
a972c1a4a012 cosmetics: Rename struct vf_instance_s --> vf_instance.
diego
parents: 30638
diff changeset
235 static int query_format(struct vf_instance *vf, unsigned int fmt)
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
236 {
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
237 switch(fmt){
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
238 case IMGFMT_YV12:
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
239 case IMGFMT_I420:
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
240 case IMGFMT_IYUV:
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
241 case IMGFMT_UYVY:
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
242 case IMGFMT_YUY2:
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
243 case IMGFMT_BGR32:
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
244 case IMGFMT_BGR24:
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
245 case IMGFMT_BGR16:
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
246 case IMGFMT_BGR15:
31082
92f88bb315c5 Add support for 12-bit color mode on framebuffer devices.
cehoyos
parents: 31003
diff changeset
247 case IMGFMT_BGR12:
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
248 case IMGFMT_RGB32:
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
249 case IMGFMT_RGB24:
27947
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
250 case IMGFMT_Y800:
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
251 case IMGFMT_Y8:
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
252 case IMGFMT_YVU9:
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
253 case IMGFMT_IF09:
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
254 case IMGFMT_444P:
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
255 case IMGFMT_422P:
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
256 case IMGFMT_411P:
4681b6303443 cosmetics: Remove tabs and trailing whitespace.
diego
parents: 26069
diff changeset
257 return vf_next_query_format(vf, fmt);
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
258 }
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
259 return 0;
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
260 }
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
261
27949
88d793e52bd9 cosmetics: Move up uninit() to avoid a forward declaration.
diego
parents: 27948
diff changeset
262 static void uninit(vf_instance_t *vf)
88d793e52bd9 cosmetics: Move up uninit() to avoid a forward declaration.
diego
parents: 27948
diff changeset
263 {
30143
0a4585a1653d Add missing avcodec_close to vf_screenshot.
reimar
parents: 29702
diff changeset
264 avcodec_close(vf->priv->avctx);
27949
88d793e52bd9 cosmetics: Move up uninit() to avoid a forward declaration.
diego
parents: 27948
diff changeset
265 av_freep(&vf->priv->avctx);
88d793e52bd9 cosmetics: Move up uninit() to avoid a forward declaration.
diego
parents: 27948
diff changeset
266 if(vf->priv->ctx) sws_freeContext(vf->priv->ctx);
36753
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
267 av_freep(&vf->priv->pic->data[0]);
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
268 av_frame_free(&vf->priv->pic);
36755
1644cfb31b2f vf_screenshot: use av_fast_malloc.
reimar
parents: 36754
diff changeset
269 av_freep(&vf->priv->outbuffer);
36752
af146b0d33f0 Allow specifying custom path and name for screenshot files.
reimar
parents: 36751
diff changeset
270 free(vf->priv->prefix);
27949
88d793e52bd9 cosmetics: Move up uninit() to avoid a forward declaration.
diego
parents: 27948
diff changeset
271 free(vf->priv);
88d793e52bd9 cosmetics: Move up uninit() to avoid a forward declaration.
diego
parents: 27948
diff changeset
272 }
88d793e52bd9 cosmetics: Move up uninit() to avoid a forward declaration.
diego
parents: 27948
diff changeset
273
30638
a7b908875c14 Rename open() vf initialization function to vf_open().
diego
parents: 30421
diff changeset
274 static int vf_open(vf_instance_t *vf, char *args)
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
275 {
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
276 vf->config=config;
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
277 vf->control=control;
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
278 vf->put_image=put_image;
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
279 vf->query_format=query_format;
16450
40222952440f DR and slice support
henry
parents: 16446
diff changeset
280 vf->start_slice=start_slice;
40222952440f DR and slice support
henry
parents: 16446
diff changeset
281 vf->draw_slice=draw_slice;
40222952440f DR and slice support
henry
parents: 16446
diff changeset
282 vf->get_image=get_image;
16874
debd99a161e0 10l, uninit() was not used, fixes bug #401
reimar
parents: 16464
diff changeset
283 vf->uninit=uninit;
36754
122de2aa583b vf_screenshot: use calloc.
reimar
parents: 36753
diff changeset
284 vf->priv = calloc(1, sizeof(struct vf_priv_s));
36753
b3afe52119c4 vf_screenshot: Switch to avcodec_encode_video2 API.
reimar
parents: 36752
diff changeset
285 vf->priv->pic = av_frame_alloc();
36752
af146b0d33f0 Allow specifying custom path and name for screenshot files.
reimar
parents: 36751
diff changeset
286 vf->priv->prefix = strdup(args ? args : "shot");
34566
f3d53cd55376 Update deprecated avcodec_alloc_context()/avcodec_open() API calls
siretart
parents: 32537
diff changeset
287 vf->priv->avctx = avcodec_alloc_context3(NULL);
34659
c7db9db9105c vf screenshot: Fix breakage after lavc API changes
al
parents: 34566
diff changeset
288 vf->priv->avctx->pix_fmt = PIX_FMT_RGB24;
24731
1d9ea74f79a0 Make vf_screenshot use the libavcodec PNG encoder
reimar
parents: 23373
diff changeset
289 avcodec_register_all();
35715
8517826b0dbd Replace CODEC_IDs their modern AV_-prefixed counterparts.
diego
parents: 35712
diff changeset
290 if (avcodec_open2(vf->priv->avctx, avcodec_find_encoder(AV_CODEC_ID_PNG), NULL)) {
24731
1d9ea74f79a0 Make vf_screenshot use the libavcodec PNG encoder
reimar
parents: 23373
diff changeset
291 mp_msg(MSGT_VFILTER, MSGL_FATAL, "Could not open libavcodec PNG encoder\n");
1d9ea74f79a0 Make vf_screenshot use the libavcodec PNG encoder
reimar
parents: 23373
diff changeset
292 return 0;
1d9ea74f79a0 Make vf_screenshot use the libavcodec PNG encoder
reimar
parents: 23373
diff changeset
293 }
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
294 return 1;
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
295 }
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
296
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
297
25221
00fff9a3b735 Make all vf_info_t structs const
reimar
parents: 24731
diff changeset
298 const vf_info_t vf_info_screenshot = {
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
299 "screenshot to file",
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
300 "screenshot",
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
301 "A'rpi, Jindrich Makovicka",
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
302 "",
30638
a7b908875c14 Rename open() vf initialization function to vf_open().
diego
parents: 30421
diff changeset
303 vf_open,
16446
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
304 NULL
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
305 };
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
306
c82c0d407ec9 screenshot filter
henry
parents:
diff changeset
307 //===========================================================================//