gl_common: minor cleanup/refactor
[mplayer.git] / libmpcodecs / vf_lavc.c
blobb2c1dd756d9eb74f31767091917d09978770d781
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 if (lavc_venc_context.codec->encode == NULL) {
80 mp_msg(MSGT_VFILTER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n");
81 return 0;
84 return vf_next_config(vf,width,height,d_width,d_height,flags,IMGFMT_MPEGPES);
87 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
88 mp_image_t* dmpi;
89 int out_size;
90 AVFrame *pic= vf->priv->pic;
92 pic->data[0]=mpi->planes[0];
93 pic->data[1]=mpi->planes[1];
94 pic->data[2]=mpi->planes[2];
95 pic->linesize[0]=mpi->stride[0];
96 pic->linesize[1]=mpi->stride[1];
97 pic->linesize[2]=mpi->stride[2];
99 out_size = avcodec_encode_video(&lavc_venc_context,
100 vf->priv->outbuf, vf->priv->outbuf_size, pic);
102 if(out_size<=0) return 1;
104 dmpi=vf_get_image(vf->next,IMGFMT_MPEGPES,
105 MP_IMGTYPE_EXPORT, 0,
106 mpi->w, mpi->h);
108 vf->priv->pes.data=vf->priv->outbuf;
109 vf->priv->pes.size=out_size;
110 vf->priv->pes.id=0x1E0;
111 vf->priv->pes.timestamp=-1; // dunno
113 dmpi->planes[0]=(unsigned char*)&vf->priv->pes;
115 return vf_next_put_image(vf,dmpi, pts);
118 //===========================================================================//
120 static int query_format(struct vf_instance *vf, unsigned int fmt){
121 switch(fmt){
122 case IMGFMT_YV12:
123 case IMGFMT_I420:
124 case IMGFMT_IYUV:
125 return vf_next_query_format(vf, IMGFMT_MPEGPES) & (~(VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_ACCEPT_STRIDE));
127 return 0;
130 static int vf_open(vf_instance_t *vf, char *args){
131 int p_quality=0;
132 float p_fps=0;
134 vf->config=config;
135 vf->put_image=put_image;
136 vf->query_format=query_format;
137 vf->priv=malloc(sizeof(struct vf_priv_s));
138 memset(vf->priv,0,sizeof(struct vf_priv_s));
140 vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name("mpeg1video");
141 if (!vf->priv->codec) {
142 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Cannot find codec '%s' in libavcodec...\n", "mpeg1video");
143 return 0;
146 vf->priv->context=avcodec_alloc_context3(vf->priv->codec);
147 vf->priv->pic = avcodec_alloc_frame();
149 // TODO: parse args ->
150 if(args) sscanf(args, "%d:%f", &p_quality, &p_fps);
152 if(p_quality<32){
153 // fixed qscale
154 lavc_venc_context.flags = CODEC_FLAG_QSCALE;
155 lavc_venc_context.global_quality =
156 vf->priv->pic->quality = (int)(FF_QP2LAMBDA * ((p_quality<1) ? 1 : p_quality) + 0.5);
157 } else {
158 // fixed bitrate (in kbits)
159 lavc_venc_context.bit_rate = 1000*p_quality;
161 lavc_venc_context.time_base.num = 1000*1001;
162 lavc_venc_context.time_base.den = (p_fps<1.0) ? 1000*1001*25 : (p_fps * lavc_venc_context.time_base.num);
163 lavc_venc_context.gop_size = 0; // I-only
164 lavc_venc_context.pix_fmt= PIX_FMT_YUV420P;
166 return 1;
169 const vf_info_t vf_info_lavc = {
170 "realtime mpeg1 encoding with libavcodec",
171 "lavc",
172 "A'rpi",
174 vf_open,
175 NULL
178 //===========================================================================//