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.
|
|
|
|
*/
|
|
|
|
|
2002-04-27 23:34:44 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "mp_msg.h"
|
2002-04-27 23:34:44 +00:00
|
|
|
|
|
|
|
#include "img_format.h"
|
|
|
|
#include "mp_image.h"
|
|
|
|
#include "vf.h"
|
2010-09-04 08:11:31 +00:00
|
|
|
#include "vd_ffmpeg.h"
|
2002-04-27 23:34:44 +00:00
|
|
|
#include "libavcodec/avcodec.h"
|
|
|
|
|
|
|
|
struct vf_priv_s {
|
|
|
|
unsigned char* outbuf;
|
|
|
|
int outbuf_size;
|
2002-10-22 22:25:17 +00:00
|
|
|
AVCodecContext* context;
|
2002-12-09 12:36:04 +00:00
|
|
|
AVFrame* pic;
|
2002-10-22 22:25:17 +00:00
|
|
|
AVCodec* codec;
|
2002-04-27 23:34:44 +00:00
|
|
|
vo_mpegpes_t pes;
|
|
|
|
};
|
|
|
|
|
2002-10-22 22:25:17 +00:00
|
|
|
#define lavc_venc_context (*vf->priv->context)
|
2002-04-27 23:34:44 +00:00
|
|
|
|
|
|
|
//===========================================================================//
|
|
|
|
|
2010-05-29 14:15:55 +00:00
|
|
|
static int config(struct vf_instance *vf,
|
2002-04-27 23:34:44 +00:00
|
|
|
int width, int height, int d_width, int d_height,
|
|
|
|
unsigned int flags, unsigned int outfmt){
|
|
|
|
if(vf_next_query_format(vf,IMGFMT_MPEGPES)<=0) return 0;
|
|
|
|
|
|
|
|
lavc_venc_context.width = width;
|
|
|
|
lavc_venc_context.height = height;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2005-06-28 19:41:08 +00:00
|
|
|
if(!lavc_venc_context.time_base.num || !lavc_venc_context.time_base.den){
|
2002-05-08 22:11:14 +00:00
|
|
|
// guess FPS:
|
|
|
|
switch(height){
|
|
|
|
case 240:
|
|
|
|
case 480:
|
2005-04-30 22:41:41 +00:00
|
|
|
lavc_venc_context.time_base= (AVRational){1001,30000};
|
2002-05-08 22:11:14 +00:00
|
|
|
break;
|
|
|
|
case 576:
|
|
|
|
case 288:
|
|
|
|
default:
|
2005-04-30 22:41:41 +00:00
|
|
|
lavc_venc_context.time_base= (AVRational){1,25};
|
2002-05-08 22:11:14 +00:00
|
|
|
break;
|
|
|
|
// lavc_venc_context.frame_rate=vo_fps*FRAME_RATE_BASE; // same as src
|
|
|
|
}
|
|
|
|
}
|
2002-04-27 23:34:44 +00:00
|
|
|
|
2010-11-14 09:12:34 +00:00
|
|
|
free(vf->priv->outbuf);
|
2002-04-27 23:34:44 +00:00
|
|
|
|
|
|
|
vf->priv->outbuf_size=10000+width*height; // must be enough!
|
|
|
|
vf->priv->outbuf = malloc(vf->priv->outbuf_size);
|
|
|
|
|
|
|
|
if (avcodec_open(&lavc_venc_context, vf->priv->codec) != 0) {
|
2011-01-21 00:27:56 +00:00
|
|
|
mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Could not open codec.\n");
|
2002-04-27 23:34:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lavc_venc_context.codec->encode == NULL) {
|
2011-01-21 00:27:56 +00:00
|
|
|
mp_msg(MSGT_VFILTER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n");
|
2002-04-27 23:34:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vf_next_config(vf,width,height,d_width,d_height,flags,IMGFMT_MPEGPES);
|
|
|
|
}
|
|
|
|
|
2010-05-29 14:15:55 +00:00
|
|
|
static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
|
2002-04-27 23:34:44 +00:00
|
|
|
mp_image_t* dmpi;
|
|
|
|
int out_size;
|
2002-12-09 12:36:04 +00:00
|
|
|
AVFrame *pic= vf->priv->pic;
|
2002-04-27 23:34:44 +00:00
|
|
|
|
2002-12-04 10:49:03 +00:00
|
|
|
pic->data[0]=mpi->planes[0];
|
|
|
|
pic->data[1]=mpi->planes[1];
|
|
|
|
pic->data[2]=mpi->planes[2];
|
|
|
|
pic->linesize[0]=mpi->stride[0];
|
|
|
|
pic->linesize[1]=mpi->stride[1];
|
|
|
|
pic->linesize[2]=mpi->stride[2];
|
2002-04-27 23:34:44 +00:00
|
|
|
|
2009-07-06 23:26:13 +00:00
|
|
|
out_size = avcodec_encode_video(&lavc_venc_context,
|
2002-12-04 10:49:03 +00:00
|
|
|
vf->priv->outbuf, vf->priv->outbuf_size, pic);
|
2002-04-27 23:34:44 +00:00
|
|
|
|
2002-09-10 22:18:32 +00:00
|
|
|
if(out_size<=0) return 1;
|
2002-04-27 23:34:44 +00:00
|
|
|
|
|
|
|
dmpi=vf_get_image(vf->next,IMGFMT_MPEGPES,
|
|
|
|
MP_IMGTYPE_EXPORT, 0,
|
|
|
|
mpi->w, mpi->h);
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2002-04-27 23:34:44 +00:00
|
|
|
vf->priv->pes.data=vf->priv->outbuf;
|
|
|
|
vf->priv->pes.size=out_size;
|
|
|
|
vf->priv->pes.id=0x1E0;
|
|
|
|
vf->priv->pes.timestamp=-1; // dunno
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2002-08-28 22:45:48 +00:00
|
|
|
dmpi->planes[0]=(unsigned char*)&vf->priv->pes;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
vf_*: fix pts values passed to the next filter
Many video filters failed to calculate or even just pass through pts
values for their output frames. Fix this, and also make the two
remaining filters that called vf_next_put_image() twice for the same
input frame (vf_softpulldown, vf_telecine) use vf_queue_frame() so
that e.g. framestepping properly sees both frames.
Changed filters: vf_bmovl, vf_detc, vf_divtc, vf_filmdint, vf_ivtc,
vf_lavc, vf_phase, vf_pullup, vf_softpulldown, vf_telecine, vf_tile,
vf_tinterlace.
2011-04-23 16:56:47 +00:00
|
|
|
return vf_next_put_image(vf,dmpi, pts);
|
2002-04-27 23:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================//
|
|
|
|
|
2010-05-29 14:15:55 +00:00
|
|
|
static int query_format(struct vf_instance *vf, unsigned int fmt){
|
2002-04-27 23:34:44 +00:00
|
|
|
switch(fmt){
|
|
|
|
case IMGFMT_YV12:
|
|
|
|
case IMGFMT_I420:
|
|
|
|
case IMGFMT_IYUV:
|
2008-05-16 00:23:02 +00:00
|
|
|
return vf_next_query_format(vf, IMGFMT_MPEGPES) & (~(VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_ACCEPT_STRIDE));
|
2002-04-27 23:34:44 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-21 13:40:49 +00:00
|
|
|
static int vf_open(vf_instance_t *vf, char *args){
|
2002-05-08 22:11:14 +00:00
|
|
|
int p_quality=0;
|
|
|
|
float p_fps=0;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2002-04-27 23:34:44 +00:00
|
|
|
vf->config=config;
|
|
|
|
vf->put_image=put_image;
|
|
|
|
vf->query_format=query_format;
|
|
|
|
vf->priv=malloc(sizeof(struct vf_priv_s));
|
|
|
|
memset(vf->priv,0,sizeof(struct vf_priv_s));
|
|
|
|
|
2010-09-04 08:11:31 +00:00
|
|
|
init_avcodec();
|
2002-04-27 23:34:44 +00:00
|
|
|
|
|
|
|
vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name("mpeg1video");
|
|
|
|
if (!vf->priv->codec) {
|
2011-01-21 00:27:56 +00:00
|
|
|
mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Cannot find codec '%s' in libavcodec...\n", "mpeg1video");
|
2002-04-27 23:34:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2002-10-22 22:25:17 +00:00
|
|
|
vf->priv->context=avcodec_alloc_context();
|
2002-12-09 12:36:04 +00:00
|
|
|
vf->priv->pic = avcodec_alloc_frame();
|
2002-04-27 23:34:44 +00:00
|
|
|
|
|
|
|
// TODO: parse args ->
|
2002-05-08 22:11:14 +00:00
|
|
|
if(args) sscanf(args, "%d:%f", &p_quality, &p_fps);
|
|
|
|
|
|
|
|
if(p_quality<32){
|
|
|
|
// fixed qscale
|
|
|
|
lavc_venc_context.flags = CODEC_FLAG_QSCALE;
|
2003-10-24 11:34:48 +00:00
|
|
|
lavc_venc_context.global_quality =
|
|
|
|
vf->priv->pic->quality = (int)(FF_QP2LAMBDA * ((p_quality<1) ? 1 : p_quality) + 0.5);
|
2002-05-08 22:11:14 +00:00
|
|
|
} else {
|
|
|
|
// fixed bitrate (in kbits)
|
|
|
|
lavc_venc_context.bit_rate = 1000*p_quality;
|
|
|
|
}
|
2005-04-30 22:41:41 +00:00
|
|
|
lavc_venc_context.time_base.num = 1000*1001;
|
2006-03-21 21:26:42 +00:00
|
|
|
lavc_venc_context.time_base.den = (p_fps<1.0) ? 1000*1001*25 : (p_fps * lavc_venc_context.time_base.num);
|
2002-04-27 23:34:44 +00:00
|
|
|
lavc_venc_context.gop_size = 0; // I-only
|
2005-05-05 09:47:41 +00:00
|
|
|
lavc_venc_context.pix_fmt= PIX_FMT_YUV420P;
|
2002-04-27 23:34:44 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-12-02 14:57:15 +00:00
|
|
|
const vf_info_t vf_info_lavc = {
|
2002-04-27 23:34:44 +00:00
|
|
|
"realtime mpeg1 encoding with libavcodec",
|
|
|
|
"lavc",
|
|
|
|
"A'rpi",
|
|
|
|
"",
|
2010-02-21 13:40:49 +00:00
|
|
|
vf_open,
|
2003-03-15 18:01:02 +00:00
|
|
|
NULL
|
2002-04-27 23:34:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//===========================================================================//
|