2010-01-30 16:57:40 +00:00
|
|
|
/*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "config.h"
|
2005-09-11 06:42:32 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2009-02-17 11:16:19 +00:00
|
|
|
#if HAVE_MALLOC_H
|
2005-09-12 18:33:26 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
2005-09-11 06:42:32 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "mp_msg.h"
|
2005-09-11 06:42:32 +00:00
|
|
|
|
|
|
|
#include "img_format.h"
|
|
|
|
#include "mp_image.h"
|
|
|
|
#include "vf.h"
|
|
|
|
#include "vf_scale.h"
|
|
|
|
|
2006-06-30 12:00:31 +00:00
|
|
|
#include "libswscale/swscale.h"
|
2007-10-09 12:46:10 +00:00
|
|
|
#include "libavcodec/avcodec.h"
|
|
|
|
|
2005-09-11 06:42:32 +00:00
|
|
|
struct vf_priv_s {
|
|
|
|
int frameno;
|
2005-09-11 10:52:24 +00:00
|
|
|
char fname[102];
|
2006-09-15 13:27:59 +00:00
|
|
|
/// shot stores current screenshot mode:
|
|
|
|
/// 0: don't take screenshots
|
|
|
|
/// 1: take single screenshot, reset to 0 afterwards
|
|
|
|
/// 2: take screenshots of each frame
|
2005-09-11 10:52:24 +00:00
|
|
|
int shot, store_slices;
|
|
|
|
int dw, dh, stride;
|
2005-09-11 06:42:32 +00:00
|
|
|
uint8_t *buffer;
|
|
|
|
struct SwsContext *ctx;
|
2007-10-09 12:46:10 +00:00
|
|
|
AVCodecContext *avctx;
|
|
|
|
uint8_t *outbuffer;
|
|
|
|
int outbuffer_size;
|
2005-09-11 06:42:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//===========================================================================//
|
|
|
|
|
2010-02-21 15:48:03 +00:00
|
|
|
static int config(struct vf_instance *vf,
|
2008-11-23 11:58:11 +00:00
|
|
|
int width, int height, int d_width, int d_height,
|
|
|
|
unsigned int flags, unsigned int outfmt)
|
2005-09-11 06:42:32 +00:00
|
|
|
{
|
2005-09-12 18:33:26 +00:00
|
|
|
vf->priv->ctx=sws_getContextFromCmdLine(width, height, outfmt,
|
2008-11-23 11:58:11 +00:00
|
|
|
d_width, d_height, IMGFMT_RGB24);
|
2007-10-09 12:46:10 +00:00
|
|
|
|
|
|
|
vf->priv->outbuffer_size = d_width * d_height * 3 * 2;
|
|
|
|
vf->priv->outbuffer = realloc(vf->priv->outbuffer, vf->priv->outbuffer_size);
|
|
|
|
vf->priv->avctx->width = d_width;
|
|
|
|
vf->priv->avctx->height = d_height;
|
|
|
|
vf->priv->avctx->pix_fmt = PIX_FMT_RGB24;
|
|
|
|
vf->priv->avctx->compression_level = 0;
|
2005-09-11 06:42:32 +00:00
|
|
|
vf->priv->dw = d_width;
|
|
|
|
vf->priv->dh = d_height;
|
2005-09-11 10:52:24 +00:00
|
|
|
vf->priv->stride = (3*vf->priv->dw+15)&~15;
|
2005-09-11 06:42:32 +00:00
|
|
|
|
|
|
|
if (vf->priv->buffer) free(vf->priv->buffer); // probably reconfigured
|
2005-09-12 18:33:26 +00:00
|
|
|
vf->priv->buffer = NULL;
|
2005-09-11 06:42:32 +00:00
|
|
|
|
|
|
|
return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
|
|
|
|
}
|
|
|
|
|
2007-10-09 12:46:10 +00:00
|
|
|
static void write_png(struct vf_priv_s *priv)
|
2005-09-11 06:42:32 +00:00
|
|
|
{
|
2007-10-09 12:46:10 +00:00
|
|
|
char *fname = priv->fname;
|
2005-09-11 06:42:32 +00:00
|
|
|
FILE * fp;
|
2007-10-09 12:46:10 +00:00
|
|
|
AVFrame pic;
|
|
|
|
int size;
|
2005-09-11 06:42:32 +00:00
|
|
|
|
|
|
|
fp = fopen (fname, "wb");
|
|
|
|
if (fp == NULL) {
|
2008-11-23 11:58:11 +00:00
|
|
|
mp_msg(MSGT_VFILTER,MSGL_ERR,"\nPNG Error opening %s for writing!\n", fname);
|
|
|
|
return;
|
2005-09-11 06:42:32 +00:00
|
|
|
}
|
2008-11-23 11:58:11 +00:00
|
|
|
|
2007-10-09 12:46:10 +00:00
|
|
|
pic.data[0] = priv->buffer;
|
|
|
|
pic.linesize[0] = priv->stride;
|
|
|
|
size = avcodec_encode_video(priv->avctx, priv->outbuffer, priv->outbuffer_size, &pic);
|
|
|
|
if (size > 0)
|
|
|
|
fwrite(priv->outbuffer, size, 1, fp);
|
2005-09-11 06:42:32 +00:00
|
|
|
|
|
|
|
fclose (fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fexists(char *fname)
|
|
|
|
{
|
|
|
|
struct stat dummy;
|
|
|
|
if (stat(fname, &dummy) == 0) return 1;
|
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-11 10:52:24 +00:00
|
|
|
static void gen_fname(struct vf_priv_s* priv)
|
2005-09-11 06:42:32 +00:00
|
|
|
{
|
|
|
|
do {
|
2008-11-23 11:58:11 +00:00
|
|
|
snprintf (priv->fname, 100, "shot%04d.png", ++priv->frameno);
|
2005-09-11 10:52:24 +00:00
|
|
|
} while (fexists(priv->fname) && priv->frameno < 100000);
|
2005-09-11 15:41:08 +00:00
|
|
|
if (fexists(priv->fname)) {
|
2008-11-23 11:58:11 +00:00
|
|
|
priv->fname[0] = '\0';
|
|
|
|
return;
|
2005-09-11 15:41:08 +00:00
|
|
|
}
|
2005-09-11 06:42:32 +00:00
|
|
|
|
2005-09-11 10:52:24 +00:00
|
|
|
mp_msg(MSGT_VFILTER,MSGL_INFO,"*** screenshot '%s' ***\n",priv->fname);
|
|
|
|
|
|
|
|
}
|
2005-09-11 06:42:32 +00:00
|
|
|
|
2005-09-12 18:33:26 +00:00
|
|
|
static void scale_image(struct vf_priv_s* priv, mp_image_t *mpi)
|
2005-09-11 10:52:24 +00:00
|
|
|
{
|
2009-03-29 14:46:59 +00:00
|
|
|
uint8_t *dst[MP_MAX_PLANES] = {NULL};
|
|
|
|
int dst_stride[MP_MAX_PLANES] = {0};
|
2008-11-23 11:58:11 +00:00
|
|
|
|
2005-09-11 10:52:24 +00:00
|
|
|
dst_stride[0] = priv->stride;
|
2005-09-11 06:42:32 +00:00
|
|
|
if (!priv->buffer)
|
2008-11-23 11:58:11 +00:00
|
|
|
priv->buffer = memalign(16, dst_stride[0]*priv->dh);
|
2005-09-11 06:42:32 +00:00
|
|
|
|
|
|
|
dst[0] = priv->buffer;
|
2010-01-03 17:12:12 +00:00
|
|
|
sws_scale(priv->ctx, mpi->planes, mpi->stride, 0, priv->dh, dst, dst_stride);
|
2005-09-11 10:52:24 +00:00
|
|
|
}
|
|
|
|
|
2010-02-21 15:48:03 +00:00
|
|
|
static void start_slice(struct vf_instance *vf, mp_image_t *mpi)
|
2008-11-23 11:59:37 +00:00
|
|
|
{
|
2005-09-12 18:33:26 +00:00
|
|
|
vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
|
2008-11-23 11:58:11 +00:00
|
|
|
mpi->type, mpi->flags, mpi->width, mpi->height);
|
2005-09-11 10:52:24 +00:00
|
|
|
if (vf->priv->shot) {
|
2008-11-23 11:58:11 +00:00
|
|
|
vf->priv->store_slices = 1;
|
|
|
|
if (!vf->priv->buffer)
|
|
|
|
vf->priv->buffer = memalign(16, vf->priv->stride*vf->priv->dh);
|
2005-09-11 10:52:24 +00:00
|
|
|
}
|
2008-11-23 11:58:11 +00:00
|
|
|
|
2005-09-11 10:52:24 +00:00
|
|
|
}
|
2005-09-11 06:42:32 +00:00
|
|
|
|
2010-02-21 15:48:03 +00:00
|
|
|
static void draw_slice(struct vf_instance *vf, unsigned char** src,
|
2008-11-23 11:59:37 +00:00
|
|
|
int* stride, int w,int h, int x, int y)
|
|
|
|
{
|
2005-09-11 10:52:24 +00:00
|
|
|
if (vf->priv->store_slices) {
|
2009-03-29 14:46:59 +00:00
|
|
|
uint8_t *dst[MP_MAX_PLANES] = {NULL};
|
|
|
|
int dst_stride[MP_MAX_PLANES] = {0};
|
2008-11-23 11:58:11 +00:00
|
|
|
dst_stride[0] = vf->priv->stride;
|
|
|
|
dst[0] = vf->priv->buffer;
|
2010-01-03 17:12:12 +00:00
|
|
|
sws_scale(vf->priv->ctx, src, stride, y, h, dst, dst_stride);
|
2005-09-11 10:52:24 +00:00
|
|
|
}
|
|
|
|
vf_next_draw_slice(vf,src,stride,w,h,x,y);
|
|
|
|
}
|
|
|
|
|
2010-02-21 15:48:03 +00:00
|
|
|
static void get_image(struct vf_instance *vf, mp_image_t *mpi)
|
2008-11-23 11:59:37 +00:00
|
|
|
{
|
2005-09-12 18:33:26 +00:00
|
|
|
// FIXME: should vf.c really call get_image when using slices??
|
|
|
|
if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
|
|
|
|
return;
|
2008-11-23 11:58:11 +00:00
|
|
|
vf->dmpi= vf_get_image(vf->next, mpi->imgfmt,
|
|
|
|
mpi->type, mpi->flags/* | MP_IMGFLAG_READABLE*/, mpi->width, mpi->height);
|
2005-09-11 15:08:48 +00:00
|
|
|
|
2005-09-11 10:52:24 +00:00
|
|
|
mpi->planes[0]=vf->dmpi->planes[0];
|
|
|
|
mpi->stride[0]=vf->dmpi->stride[0];
|
|
|
|
if(mpi->flags&MP_IMGFLAG_PLANAR){
|
2008-11-23 11:58:11 +00:00
|
|
|
mpi->planes[1]=vf->dmpi->planes[1];
|
|
|
|
mpi->planes[2]=vf->dmpi->planes[2];
|
|
|
|
mpi->stride[1]=vf->dmpi->stride[1];
|
|
|
|
mpi->stride[2]=vf->dmpi->stride[2];
|
2005-09-11 10:52:24 +00:00
|
|
|
}
|
|
|
|
mpi->width=vf->dmpi->width;
|
|
|
|
|
|
|
|
mpi->flags|=MP_IMGFLAG_DIRECT;
|
2008-11-23 11:58:11 +00:00
|
|
|
|
2005-09-11 10:52:24 +00:00
|
|
|
mpi->priv=(void*)vf->dmpi;
|
2005-09-11 06:42:32 +00:00
|
|
|
}
|
|
|
|
|
2010-02-21 15:48:03 +00:00
|
|
|
static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
|
2005-09-11 06:42:32 +00:00
|
|
|
{
|
2005-09-12 18:33:26 +00:00
|
|
|
mp_image_t *dmpi = (mp_image_t *)mpi->priv;
|
2008-11-23 11:58:11 +00:00
|
|
|
|
2005-09-12 18:33:26 +00:00
|
|
|
if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
|
|
|
|
dmpi = vf->dmpi;
|
|
|
|
else
|
2005-09-11 10:52:24 +00:00
|
|
|
if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
|
2008-11-23 11:58:11 +00:00
|
|
|
dmpi=vf_get_image(vf->next,mpi->imgfmt,
|
|
|
|
MP_IMGTYPE_EXPORT, 0,
|
|
|
|
mpi->width, mpi->height);
|
|
|
|
vf_clone_mpi_attributes(dmpi, mpi);
|
|
|
|
dmpi->planes[0]=mpi->planes[0];
|
|
|
|
dmpi->planes[1]=mpi->planes[1];
|
|
|
|
dmpi->planes[2]=mpi->planes[2];
|
|
|
|
dmpi->stride[0]=mpi->stride[0];
|
|
|
|
dmpi->stride[1]=mpi->stride[1];
|
|
|
|
dmpi->stride[2]=mpi->stride[2];
|
|
|
|
dmpi->width=mpi->width;
|
|
|
|
dmpi->height=mpi->height;
|
2005-09-11 10:52:24 +00:00
|
|
|
}
|
2005-09-11 06:42:32 +00:00
|
|
|
|
|
|
|
if(vf->priv->shot) {
|
2008-11-23 11:58:11 +00:00
|
|
|
if (vf->priv->shot==1)
|
|
|
|
vf->priv->shot=0;
|
|
|
|
gen_fname(vf->priv);
|
|
|
|
if (vf->priv->fname[0]) {
|
|
|
|
if (!vf->priv->store_slices)
|
|
|
|
scale_image(vf->priv, dmpi);
|
|
|
|
write_png(vf->priv);
|
|
|
|
}
|
|
|
|
vf->priv->store_slices = 0;
|
2005-09-11 06:42:32 +00:00
|
|
|
}
|
|
|
|
|
2006-03-21 21:26:42 +00:00
|
|
|
return vf_next_put_image(vf, dmpi, pts);
|
2005-09-11 06:42:32 +00:00
|
|
|
}
|
|
|
|
|
2009-09-30 23:47:00 +00:00
|
|
|
static int control (vf_instance_t *vf, int request, void *data)
|
2005-09-11 06:42:32 +00:00
|
|
|
{
|
2006-09-15 13:27:59 +00:00
|
|
|
/** data contains an integer argument
|
|
|
|
* 0: take screenshot with the next frame
|
|
|
|
* 1: take screenshots with each frame until the same command is given once again
|
|
|
|
**/
|
2005-09-11 06:42:32 +00:00
|
|
|
if(request==VFCTRL_SCREENSHOT) {
|
2008-11-23 11:58:11 +00:00
|
|
|
if (data && *(int*)data) { // repeated screenshot mode
|
|
|
|
if (vf->priv->shot==2)
|
|
|
|
vf->priv->shot=0;
|
|
|
|
else
|
|
|
|
vf->priv->shot=2;
|
|
|
|
} else { // single screenshot
|
|
|
|
if (!vf->priv->shot)
|
|
|
|
vf->priv->shot=1;
|
|
|
|
}
|
2005-09-11 06:42:32 +00:00
|
|
|
return CONTROL_TRUE;
|
|
|
|
}
|
|
|
|
return vf_next_control (vf, request, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================//
|
|
|
|
|
2010-02-21 15:48:03 +00:00
|
|
|
static int query_format(struct vf_instance *vf, unsigned int fmt)
|
2005-09-11 06:42:32 +00:00
|
|
|
{
|
|
|
|
switch(fmt){
|
|
|
|
case IMGFMT_YV12:
|
|
|
|
case IMGFMT_I420:
|
|
|
|
case IMGFMT_IYUV:
|
|
|
|
case IMGFMT_UYVY:
|
|
|
|
case IMGFMT_YUY2:
|
|
|
|
case IMGFMT_BGR32:
|
|
|
|
case IMGFMT_BGR24:
|
|
|
|
case IMGFMT_BGR16:
|
|
|
|
case IMGFMT_BGR15:
|
|
|
|
case IMGFMT_RGB32:
|
|
|
|
case IMGFMT_RGB24:
|
2008-11-23 11:58:11 +00:00
|
|
|
case IMGFMT_Y800:
|
|
|
|
case IMGFMT_Y8:
|
|
|
|
case IMGFMT_YVU9:
|
|
|
|
case IMGFMT_IF09:
|
|
|
|
case IMGFMT_444P:
|
|
|
|
case IMGFMT_422P:
|
|
|
|
case IMGFMT_411P:
|
|
|
|
return vf_next_query_format(vf, fmt);
|
2005-09-11 06:42:32 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-23 12:01:48 +00:00
|
|
|
static void uninit(vf_instance_t *vf)
|
|
|
|
{
|
2010-01-03 15:56:33 +00:00
|
|
|
avcodec_close(vf->priv->avctx);
|
2008-11-23 12:01:48 +00:00
|
|
|
av_freep(&vf->priv->avctx);
|
|
|
|
if(vf->priv->ctx) sws_freeContext(vf->priv->ctx);
|
|
|
|
if (vf->priv->buffer) free(vf->priv->buffer);
|
|
|
|
free(vf->priv->outbuffer);
|
|
|
|
free(vf->priv);
|
|
|
|
}
|
|
|
|
|
2010-02-21 13:40:49 +00:00
|
|
|
static int vf_open(vf_instance_t *vf, char *args)
|
2005-09-11 06:42:32 +00:00
|
|
|
{
|
|
|
|
vf->config=config;
|
|
|
|
vf->control=control;
|
|
|
|
vf->put_image=put_image;
|
|
|
|
vf->query_format=query_format;
|
2005-09-11 10:52:24 +00:00
|
|
|
vf->start_slice=start_slice;
|
|
|
|
vf->draw_slice=draw_slice;
|
|
|
|
vf->get_image=get_image;
|
2005-10-30 08:45:24 +00:00
|
|
|
vf->uninit=uninit;
|
2005-09-11 06:42:32 +00:00
|
|
|
vf->priv=malloc(sizeof(struct vf_priv_s));
|
|
|
|
vf->priv->frameno=0;
|
|
|
|
vf->priv->shot=0;
|
2005-09-11 10:52:24 +00:00
|
|
|
vf->priv->store_slices=0;
|
2005-09-11 06:42:32 +00:00
|
|
|
vf->priv->buffer=0;
|
2007-10-09 12:46:10 +00:00
|
|
|
vf->priv->outbuffer=0;
|
2005-09-11 06:42:32 +00:00
|
|
|
vf->priv->ctx=0;
|
2007-10-09 12:46:10 +00:00
|
|
|
vf->priv->avctx = avcodec_alloc_context();
|
|
|
|
avcodec_register_all();
|
|
|
|
if (avcodec_open(vf->priv->avctx, avcodec_find_encoder(CODEC_ID_PNG))) {
|
|
|
|
mp_msg(MSGT_VFILTER, MSGL_FATAL, "Could not open libavcodec PNG encoder\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2005-09-11 06:42:32 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-02 14:57:15 +00:00
|
|
|
const vf_info_t vf_info_screenshot = {
|
2005-09-11 06:42:32 +00:00
|
|
|
"screenshot to file",
|
|
|
|
"screenshot",
|
|
|
|
"A'rpi, Jindrich Makovicka",
|
|
|
|
"",
|
2010-02-21 13:40:49 +00:00
|
|
|
vf_open,
|
2005-09-11 06:42:32 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
//===========================================================================//
|