ao_sun: Add support for AOPLAY_FINAL_CHUNK
[mplayer.git] / libmpcodecs / vf_lavc.c
blob2efccf093b72af1812196c8747b26dd9dc1358e9
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 "vd_ffmpeg.h"
31 #include "libavcodec/avcodec.h"
34 struct vf_priv_s {
35 unsigned char* outbuf;
36 int outbuf_size;
37 AVCodecContext* context;
38 AVFrame* pic;
39 AVCodec* codec;
40 vo_mpegpes_t pes;
43 #define lavc_venc_context (*vf->priv->context)
45 //===========================================================================//
47 static int config(struct vf_instance *vf,
48 int width, int height, int d_width, int d_height,
49 unsigned int flags, unsigned int outfmt){
50 if(vf_next_query_format(vf,IMGFMT_MPEGPES)<=0) return 0;
52 lavc_venc_context.width = width;
53 lavc_venc_context.height = height;
55 if(!lavc_venc_context.time_base.num || !lavc_venc_context.time_base.den){
56 // guess FPS:
57 switch(height){
58 case 240:
59 case 480:
60 lavc_venc_context.time_base= (AVRational){1001,30000};
61 break;
62 case 576:
63 case 288:
64 default:
65 lavc_venc_context.time_base= (AVRational){1,25};
66 break;
67 // lavc_venc_context.frame_rate=vo_fps*FRAME_RATE_BASE; // same as src
71 if(vf->priv->outbuf) free(vf->priv->outbuf);
73 vf->priv->outbuf_size=10000+width*height; // must be enough!
74 vf->priv->outbuf = malloc(vf->priv->outbuf_size);
76 if (avcodec_open(&lavc_venc_context, vf->priv->codec) != 0) {
77 mp_tmsg(MSGT_MENCODER,MSGL_ERR,"Could not open codec.\n");
78 return 0;
81 if (lavc_venc_context.codec->encode == NULL) {
82 mp_msg(MSGT_MENCODER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n");
83 return 0;
86 return vf_next_config(vf,width,height,d_width,d_height,flags,IMGFMT_MPEGPES);
89 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
90 mp_image_t* dmpi;
91 int out_size;
92 AVFrame *pic= vf->priv->pic;
94 pic->data[0]=mpi->planes[0];
95 pic->data[1]=mpi->planes[1];
96 pic->data[2]=mpi->planes[2];
97 pic->linesize[0]=mpi->stride[0];
98 pic->linesize[1]=mpi->stride[1];
99 pic->linesize[2]=mpi->stride[2];
101 out_size = avcodec_encode_video(&lavc_venc_context,
102 vf->priv->outbuf, vf->priv->outbuf_size, pic);
104 if(out_size<=0) return 1;
106 dmpi=vf_get_image(vf->next,IMGFMT_MPEGPES,
107 MP_IMGTYPE_EXPORT, 0,
108 mpi->w, mpi->h);
110 vf->priv->pes.data=vf->priv->outbuf;
111 vf->priv->pes.size=out_size;
112 vf->priv->pes.id=0x1E0;
113 vf->priv->pes.timestamp=-1; // dunno
115 dmpi->planes[0]=(unsigned char*)&vf->priv->pes;
117 return vf_next_put_image(vf,dmpi, MP_NOPTS_VALUE);
120 //===========================================================================//
122 static int query_format(struct vf_instance *vf, unsigned int fmt){
123 switch(fmt){
124 case IMGFMT_YV12:
125 case IMGFMT_I420:
126 case IMGFMT_IYUV:
127 return vf_next_query_format(vf, IMGFMT_MPEGPES) & (~(VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_ACCEPT_STRIDE));
129 return 0;
132 static int vf_open(vf_instance_t *vf, char *args){
133 int p_quality=0;
134 float p_fps=0;
136 vf->config=config;
137 vf->put_image=put_image;
138 vf->query_format=query_format;
139 vf->priv=malloc(sizeof(struct vf_priv_s));
140 memset(vf->priv,0,sizeof(struct vf_priv_s));
142 init_avcodec();
144 vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name("mpeg1video");
145 if (!vf->priv->codec) {
146 mp_tmsg(MSGT_MENCODER,MSGL_ERR,"Cannot find codec '%s' in libavcodec...\n", "mpeg1video");
147 return 0;
150 vf->priv->context=avcodec_alloc_context();
151 vf->priv->pic = avcodec_alloc_frame();
153 // TODO: parse args ->
154 if(args) sscanf(args, "%d:%f", &p_quality, &p_fps);
156 if(p_quality<32){
157 // fixed qscale
158 lavc_venc_context.flags = CODEC_FLAG_QSCALE;
159 lavc_venc_context.global_quality =
160 vf->priv->pic->quality = (int)(FF_QP2LAMBDA * ((p_quality<1) ? 1 : p_quality) + 0.5);
161 } else {
162 // fixed bitrate (in kbits)
163 lavc_venc_context.bit_rate = 1000*p_quality;
165 lavc_venc_context.time_base.num = 1000*1001;
166 lavc_venc_context.time_base.den = (p_fps<1.0) ? 1000*1001*25 : (p_fps * lavc_venc_context.time_base.num);
167 lavc_venc_context.gop_size = 0; // I-only
168 lavc_venc_context.pix_fmt= PIX_FMT_YUV420P;
170 return 1;
173 const vf_info_t vf_info_lavc = {
174 "realtime mpeg1 encoding with libavcodec",
175 "lavc",
176 "A'rpi",
178 vf_open,
179 NULL
182 //===========================================================================//