Improve VDPAU noforce-mixer documentation.
[mplayer.git] / libmpcodecs / vf_lavc.c
blobe11d52a840042c1b87912498eded82fbf068e144
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <inttypes.h>
6 #include "config.h"
7 #include "mp_msg.h"
8 #include "help_mp.h"
10 #include "img_format.h"
11 #include "mp_image.h"
12 #include "vf.h"
13 #include "libavcodec/avcodec.h"
15 extern int avcodec_initialized;
17 struct vf_priv_s {
18 unsigned char* outbuf;
19 int outbuf_size;
20 AVCodecContext* context;
21 AVFrame* pic;
22 AVCodec* codec;
23 vo_mpegpes_t pes;
26 #define lavc_venc_context (*vf->priv->context)
28 //===========================================================================//
30 static int config(struct vf_instance_s* vf,
31 int width, int height, int d_width, int d_height,
32 unsigned int flags, unsigned int outfmt){
33 if(vf_next_query_format(vf,IMGFMT_MPEGPES)<=0) return 0;
35 lavc_venc_context.width = width;
36 lavc_venc_context.height = height;
38 if(!lavc_venc_context.time_base.num || !lavc_venc_context.time_base.den){
39 // guess FPS:
40 switch(height){
41 case 240:
42 case 480:
43 lavc_venc_context.time_base= (AVRational){1001,30000};
44 break;
45 case 576:
46 case 288:
47 default:
48 lavc_venc_context.time_base= (AVRational){1,25};
49 break;
50 // lavc_venc_context.frame_rate=vo_fps*FRAME_RATE_BASE; // same as src
54 if(vf->priv->outbuf) free(vf->priv->outbuf);
56 vf->priv->outbuf_size=10000+width*height; // must be enough!
57 vf->priv->outbuf = malloc(vf->priv->outbuf_size);
59 if (avcodec_open(&lavc_venc_context, vf->priv->codec) != 0) {
60 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantOpenCodec);
61 return 0;
64 if (lavc_venc_context.codec->encode == NULL) {
65 mp_msg(MSGT_MENCODER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n");
66 return 0;
69 return vf_next_config(vf,width,height,d_width,d_height,flags,IMGFMT_MPEGPES);
72 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
73 mp_image_t* dmpi;
74 int out_size;
75 AVFrame *pic= vf->priv->pic;
77 pic->data[0]=mpi->planes[0];
78 pic->data[1]=mpi->planes[1];
79 pic->data[2]=mpi->planes[2];
80 pic->linesize[0]=mpi->stride[0];
81 pic->linesize[1]=mpi->stride[1];
82 pic->linesize[2]=mpi->stride[2];
84 out_size = avcodec_encode_video(&lavc_venc_context,
85 vf->priv->outbuf, vf->priv->outbuf_size, pic);
87 if(out_size<=0) return 1;
89 dmpi=vf_get_image(vf->next,IMGFMT_MPEGPES,
90 MP_IMGTYPE_EXPORT, 0,
91 mpi->w, mpi->h);
93 vf->priv->pes.data=vf->priv->outbuf;
94 vf->priv->pes.size=out_size;
95 vf->priv->pes.id=0x1E0;
96 vf->priv->pes.timestamp=-1; // dunno
98 dmpi->planes[0]=(unsigned char*)&vf->priv->pes;
100 return vf_next_put_image(vf,dmpi, MP_NOPTS_VALUE);
103 //===========================================================================//
105 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
106 switch(fmt){
107 case IMGFMT_YV12:
108 case IMGFMT_I420:
109 case IMGFMT_IYUV:
110 return vf_next_query_format(vf, IMGFMT_MPEGPES) & (~(VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_ACCEPT_STRIDE));
112 return 0;
115 static int open(vf_instance_t *vf, char* args){
116 int p_quality=0;
117 float p_fps=0;
119 vf->config=config;
120 vf->put_image=put_image;
121 vf->query_format=query_format;
122 vf->priv=malloc(sizeof(struct vf_priv_s));
123 memset(vf->priv,0,sizeof(struct vf_priv_s));
125 if (!avcodec_initialized){
126 avcodec_init();
127 avcodec_register_all();
128 avcodec_initialized=1;
131 vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name("mpeg1video");
132 if (!vf->priv->codec) {
133 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_MissingLAVCcodec, "mpeg1video");
134 return 0;
137 vf->priv->context=avcodec_alloc_context();
138 vf->priv->pic = avcodec_alloc_frame();
140 // TODO: parse args ->
141 if(args) sscanf(args, "%d:%f", &p_quality, &p_fps);
143 if(p_quality<32){
144 // fixed qscale
145 lavc_venc_context.flags = CODEC_FLAG_QSCALE;
146 lavc_venc_context.global_quality =
147 vf->priv->pic->quality = (int)(FF_QP2LAMBDA * ((p_quality<1) ? 1 : p_quality) + 0.5);
148 } else {
149 // fixed bitrate (in kbits)
150 lavc_venc_context.bit_rate = 1000*p_quality;
152 lavc_venc_context.time_base.num = 1000*1001;
153 lavc_venc_context.time_base.den = (p_fps<1.0) ? 1000*1001*25 : (p_fps * lavc_venc_context.time_base.num);
154 lavc_venc_context.gop_size = 0; // I-only
155 lavc_venc_context.pix_fmt= PIX_FMT_YUV420P;
157 return 1;
160 const vf_info_t vf_info_lavc = {
161 "realtime mpeg1 encoding with libavcodec",
162 "lavc",
163 "A'rpi",
165 open,
166 NULL
169 //===========================================================================//