cleanup: Silence compilation warnings on MinGW-w64
[mplayer.git] / screenshot.c
blob597a36afedadafbb110e852f26ec6307295f3021
1 /*
2 * This file is part of mplayer2.
4 * mplayer2 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 * mplayer2 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 mplayer2; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <inttypes.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
28 #include <libswscale/swscale.h>
29 #include <libavcodec/avcodec.h>
31 #include "config.h"
32 #include "talloc.h"
33 #include "screenshot.h"
34 #include "mp_core.h"
35 #include "mp_msg.h"
36 #include "libmpcodecs/img_format.h"
37 #include "libmpcodecs/mp_image.h"
38 #include "libmpcodecs/dec_video.h"
39 #include "libmpcodecs/vf.h"
40 #include "libvo/video_out.h"
42 #include "fmt-conversion.h"
44 //for sws_getContextFromCmdLine and mp_sws_set_colorspace
45 #include "libmpcodecs/vf_scale.h"
46 #include "libvo/csputils.h"
48 typedef struct screenshot_ctx {
49 int full_window;
50 int each_frame;
51 int using_vf_screenshot;
53 int frameno;
54 char fname[102];
55 } screenshot_ctx;
57 static screenshot_ctx *screenshot_get_ctx(MPContext *mpctx)
59 if (!mpctx->screenshot_ctx)
60 mpctx->screenshot_ctx = talloc_zero(mpctx, screenshot_ctx);
61 return mpctx->screenshot_ctx;
64 static int write_png(screenshot_ctx *ctx, struct mp_image *image)
66 char *fname = ctx->fname;
67 FILE *fp = NULL;
68 void *outbuffer = NULL;
69 int success = 0;
71 struct AVCodec *png_codec = avcodec_find_encoder(CODEC_ID_PNG);
72 AVCodecContext *avctx = NULL;
73 if (!png_codec)
74 goto print_open_fail;
75 avctx = avcodec_alloc_context3(png_codec);
76 if (!avctx)
77 goto print_open_fail;
79 avctx->time_base = AV_TIME_BASE_Q;
80 avctx->width = image->width;
81 avctx->height = image->height;
82 avctx->pix_fmt = PIX_FMT_RGB24;
83 avctx->compression_level = 0;
85 if (avcodec_open2(avctx, png_codec, NULL) < 0) {
86 print_open_fail:
87 mp_msg(MSGT_CPLAYER, MSGL_INFO, "Could not open libavcodec PNG encoder"
88 " for saving screenshot\n");
89 goto error_exit;
92 size_t outbuffer_size = image->width * image->height * 3 * 2;
93 outbuffer = malloc(outbuffer_size);
94 if (!outbuffer)
95 goto error_exit;
97 AVFrame pic;
98 avcodec_get_frame_defaults(&pic);
99 for (int n = 0; n < 4; n++) {
100 pic.data[n] = image->planes[n];
101 pic.linesize[n] = image->stride[n];
103 int size = avcodec_encode_video(avctx, outbuffer, outbuffer_size, &pic);
104 if (size < 1)
105 goto error_exit;
107 fp = fopen(fname, "wb");
108 if (fp == NULL) {
109 mp_msg(MSGT_CPLAYER, MSGL_ERR, "\nPNG Error opening %s for writing!\n",
110 fname);
111 goto error_exit;
114 fwrite(outbuffer, size, 1, fp);
115 fflush(fp);
117 if (ferror(fp))
118 goto error_exit;
120 success = 1;
121 error_exit:
122 if (avctx)
123 avcodec_close(avctx);
124 av_free(avctx);
125 if (fp)
126 fclose(fp);
127 free(outbuffer);
128 return success;
131 static int fexists(char *fname)
133 struct stat dummy;
134 if (stat(fname, &dummy) == 0)
135 return 1;
136 else
137 return 0;
140 static void gen_fname(screenshot_ctx *ctx)
142 do {
143 snprintf(ctx->fname, 100, "shot%04d.png", ++ctx->frameno);
144 } while (fexists(ctx->fname) && ctx->frameno < 100000);
145 if (fexists(ctx->fname)) {
146 ctx->fname[0] = '\0';
147 return;
150 mp_msg(MSGT_CPLAYER, MSGL_INFO, "*** screenshot '%s' ***\n", ctx->fname);
154 void screenshot_save(struct MPContext *mpctx, struct mp_image *image)
156 screenshot_ctx *ctx = screenshot_get_ctx(mpctx);
157 struct mp_image *dst = alloc_mpi(image->w, image->h, IMGFMT_RGB24);
159 struct SwsContext *sws = sws_getContextFromCmdLine(image->width,
160 image->height,
161 image->imgfmt,
162 dst->width,
163 dst->height,
164 dst->imgfmt);
166 struct mp_csp_details colorspace;
167 get_detected_video_colorspace(mpctx->sh_video, &colorspace);
168 // this is a property of the output device; images always use full-range RGB
169 colorspace.levels_out = MP_CSP_LEVELS_PC;
170 mp_sws_set_colorspace(sws, &colorspace);
172 sws_scale(sws, (const uint8_t **)image->planes, image->stride, 0,
173 image->height, dst->planes, dst->stride);
175 gen_fname(ctx);
176 write_png(ctx, dst);
178 sws_freeContext(sws);
179 free_mp_image(dst);
182 static void vf_screenshot_callback(void *pctx, struct mp_image *image)
184 struct MPContext *mpctx = (struct MPContext *)pctx;
185 screenshot_ctx *ctx = screenshot_get_ctx(mpctx);
186 screenshot_save(mpctx, image);
187 if (ctx->each_frame)
188 screenshot_request(mpctx, 0, ctx->full_window);
191 void screenshot_request(struct MPContext *mpctx, bool each_frame,
192 bool full_window)
194 if (mpctx->video_out && mpctx->video_out->config_ok) {
195 screenshot_ctx *ctx = screenshot_get_ctx(mpctx);
197 ctx->using_vf_screenshot = 0;
199 if (each_frame) {
200 ctx->each_frame = !ctx->each_frame;
201 ctx->full_window = full_window;
202 if (!ctx->each_frame)
203 return;
206 struct voctrl_screenshot_args args = { .full_window = full_window };
207 if (vo_control(mpctx->video_out, VOCTRL_SCREENSHOT, &args) == true) {
208 screenshot_save(mpctx, args.out_image);
209 free_mp_image(args.out_image);
210 } else {
211 mp_msg(MSGT_CPLAYER, MSGL_INFO, "No VO support for taking"
212 " screenshots, trying VFCTRL_SCREENSHOT!\n");
213 ctx->using_vf_screenshot = 1;
214 struct vf_ctrl_screenshot cmd = {
215 .image_callback = vf_screenshot_callback,
216 .image_callback_ctx = mpctx,
218 struct vf_instance *vfilter = mpctx->sh_video->vfilter;
219 if (vfilter->control(vfilter, VFCTRL_SCREENSHOT, &cmd) !=
220 CONTROL_OK)
221 mp_msg(MSGT_CPLAYER, MSGL_INFO,
222 "...failed (need --vf=screenshot?)\n");
227 void screenshot_flip(struct MPContext *mpctx)
229 screenshot_ctx *ctx = screenshot_get_ctx(mpctx);
231 if (!ctx->each_frame)
232 return;
234 // screenshot_flip is called when the VO presents a new frame. vf_screenshot
235 // can behave completely different (consider filters inserted between
236 // vf_screenshot and vf_vo, that add or remove frames), so handle this case
237 // somewhere else.
238 if (ctx->using_vf_screenshot)
239 return;
241 screenshot_request(mpctx, 0, ctx->full_window);