mp_msg: print messages to stdout, statusline to stderr
[mplayer.git] / libmpcodecs / vf_lavc.c
blob65e93a16cc9dd49fe780fcb5defbecf2208fd370
1 /*
2 * This file is part of MPlayer.
4 * MPlayer 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 * MPlayer 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 MPlayer; 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 "config.h"
25 #include "mp_msg.h"
27 #include "img_format.h"
28 #include "mp_image.h"
29 #include "vf.h"
30 #include "libavcodec/avcodec.h"
32 struct vf_priv_s {
33 unsigned char* outbuf;
34 int outbuf_size;
35 AVCodecContext* context;
36 AVFrame* pic;
37 AVCodec* codec;
38 vo_mpegpes_t pes;
41 #define lavc_venc_context (*vf->priv->context)
43 //===========================================================================//
45 static int config(struct vf_instance *vf,
46 int width, int height, int d_width, int d_height,
47 unsigned int flags, unsigned int outfmt){
48 if(vf_next_query_format(vf,IMGFMT_MPEGPES)<=0) return 0;
50 lavc_venc_context.width = width;
51 lavc_venc_context.height = height;
53 if(!lavc_venc_context.time_base.num || !lavc_venc_context.time_base.den){
54 // guess FPS:
55 switch(height){
56 case 240:
57 case 480:
58 lavc_venc_context.time_base= (AVRational){1001,30000};
59 break;
60 case 576:
61 case 288:
62 default:
63 lavc_venc_context.time_base= (AVRational){1,25};
64 break;
65 // lavc_venc_context.frame_rate=vo_fps*FRAME_RATE_BASE; // same as src
69 free(vf->priv->outbuf);
71 vf->priv->outbuf_size=10000+width*height; // must be enough!
72 vf->priv->outbuf = malloc(vf->priv->outbuf_size);
74 if (avcodec_open2(&lavc_venc_context, vf->priv->codec, NULL) != 0) {
75 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Could not open codec.\n");
76 return 0;
79 return vf_next_config(vf,width,height,d_width,d_height,flags,IMGFMT_MPEGPES);
82 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
83 mp_image_t* dmpi;
84 int out_size;
85 AVFrame *pic= vf->priv->pic;
87 pic->data[0]=mpi->planes[0];
88 pic->data[1]=mpi->planes[1];
89 pic->data[2]=mpi->planes[2];
90 pic->linesize[0]=mpi->stride[0];
91 pic->linesize[1]=mpi->stride[1];
92 pic->linesize[2]=mpi->stride[2];
94 out_size = avcodec_encode_video(&lavc_venc_context,
95 vf->priv->outbuf, vf->priv->outbuf_size, pic);
97 if(out_size<=0) return 1;
99 dmpi=vf_get_image(vf->next,IMGFMT_MPEGPES,
100 MP_IMGTYPE_EXPORT, 0,
101 mpi->w, mpi->h);
103 vf->priv->pes.data=vf->priv->outbuf;
104 vf->priv->pes.size=out_size;
105 vf->priv->pes.id=0x1E0;
106 vf->priv->pes.timestamp=-1; // dunno
108 dmpi->planes[0]=(unsigned char*)&vf->priv->pes;
110 return vf_next_put_image(vf,dmpi, pts);
113 //===========================================================================//
115 static int query_format(struct vf_instance *vf, unsigned int fmt){
116 switch(fmt){
117 case IMGFMT_YV12:
118 case IMGFMT_I420:
119 case IMGFMT_IYUV:
120 return vf_next_query_format(vf, IMGFMT_MPEGPES) & (~(VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_ACCEPT_STRIDE));
122 return 0;
125 static int vf_open(vf_instance_t *vf, char *args){
126 int p_quality=0;
127 float p_fps=0;
129 vf->config=config;
130 vf->put_image=put_image;
131 vf->query_format=query_format;
132 vf->priv=malloc(sizeof(struct vf_priv_s));
133 memset(vf->priv,0,sizeof(struct vf_priv_s));
135 vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name("mpeg1video");
136 if (!vf->priv->codec) {
137 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Cannot find codec '%s' in libavcodec...\n", "mpeg1video");
138 return 0;
141 vf->priv->context=avcodec_alloc_context3(vf->priv->codec);
142 vf->priv->pic = avcodec_alloc_frame();
144 // TODO: parse args ->
145 if(args) sscanf(args, "%d:%f", &p_quality, &p_fps);
147 if(p_quality<32){
148 // fixed qscale
149 lavc_venc_context.flags = CODEC_FLAG_QSCALE;
150 lavc_venc_context.global_quality =
151 vf->priv->pic->quality = (int)(FF_QP2LAMBDA * ((p_quality<1) ? 1 : p_quality) + 0.5);
152 } else {
153 // fixed bitrate (in kbits)
154 lavc_venc_context.bit_rate = 1000*p_quality;
156 lavc_venc_context.time_base.num = 1000*1001;
157 lavc_venc_context.time_base.den = (p_fps<1.0) ? 1000*1001*25 : (p_fps * lavc_venc_context.time_base.num);
158 lavc_venc_context.gop_size = 0; // I-only
159 lavc_venc_context.pix_fmt= PIX_FMT_YUV420P;
161 return 1;
164 const vf_info_t vf_info_lavc = {
165 "realtime mpeg1 encoding with libavcodec",
166 "lavc",
167 "A'rpi",
169 vf_open,
170 NULL
173 //===========================================================================//