ao: fix crash after ao init failure (from recent 3a5fd15fa2)
[mplayer/greg.git] / libmpcodecs / vf_lavc.c
blobc31044ba211d8564e32013c18d5da4cb9a3176e1
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"
33 struct vf_priv_s {
34 unsigned char* outbuf;
35 int outbuf_size;
36 AVCodecContext* context;
37 AVFrame* pic;
38 AVCodec* codec;
39 vo_mpegpes_t pes;
42 #define lavc_venc_context (*vf->priv->context)
44 //===========================================================================//
46 static int config(struct vf_instance *vf,
47 int width, int height, int d_width, int d_height,
48 unsigned int flags, unsigned int outfmt){
49 if(vf_next_query_format(vf,IMGFMT_MPEGPES)<=0) return 0;
51 lavc_venc_context.width = width;
52 lavc_venc_context.height = height;
54 if(!lavc_venc_context.time_base.num || !lavc_venc_context.time_base.den){
55 // guess FPS:
56 switch(height){
57 case 240:
58 case 480:
59 lavc_venc_context.time_base= (AVRational){1001,30000};
60 break;
61 case 576:
62 case 288:
63 default:
64 lavc_venc_context.time_base= (AVRational){1,25};
65 break;
66 // lavc_venc_context.frame_rate=vo_fps*FRAME_RATE_BASE; // same as src
70 free(vf->priv->outbuf);
72 vf->priv->outbuf_size=10000+width*height; // must be enough!
73 vf->priv->outbuf = malloc(vf->priv->outbuf_size);
75 if (avcodec_open(&lavc_venc_context, vf->priv->codec) != 0) {
76 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Could not open codec.\n");
77 return 0;
80 if (lavc_venc_context.codec->encode == NULL) {
81 mp_msg(MSGT_VFILTER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n");
82 return 0;
85 return vf_next_config(vf,width,height,d_width,d_height,flags,IMGFMT_MPEGPES);
88 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
89 mp_image_t* dmpi;
90 int out_size;
91 AVFrame *pic= vf->priv->pic;
93 pic->data[0]=mpi->planes[0];
94 pic->data[1]=mpi->planes[1];
95 pic->data[2]=mpi->planes[2];
96 pic->linesize[0]=mpi->stride[0];
97 pic->linesize[1]=mpi->stride[1];
98 pic->linesize[2]=mpi->stride[2];
100 out_size = avcodec_encode_video(&lavc_venc_context,
101 vf->priv->outbuf, vf->priv->outbuf_size, pic);
103 if(out_size<=0) return 1;
105 dmpi=vf_get_image(vf->next,IMGFMT_MPEGPES,
106 MP_IMGTYPE_EXPORT, 0,
107 mpi->w, mpi->h);
109 vf->priv->pes.data=vf->priv->outbuf;
110 vf->priv->pes.size=out_size;
111 vf->priv->pes.id=0x1E0;
112 vf->priv->pes.timestamp=-1; // dunno
114 dmpi->planes[0]=(unsigned char*)&vf->priv->pes;
116 return vf_next_put_image(vf,dmpi, pts);
119 //===========================================================================//
121 static int query_format(struct vf_instance *vf, unsigned int fmt){
122 switch(fmt){
123 case IMGFMT_YV12:
124 case IMGFMT_I420:
125 case IMGFMT_IYUV:
126 return vf_next_query_format(vf, IMGFMT_MPEGPES) & (~(VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_ACCEPT_STRIDE));
128 return 0;
131 static int vf_open(vf_instance_t *vf, char *args){
132 int p_quality=0;
133 float p_fps=0;
135 vf->config=config;
136 vf->put_image=put_image;
137 vf->query_format=query_format;
138 vf->priv=malloc(sizeof(struct vf_priv_s));
139 memset(vf->priv,0,sizeof(struct vf_priv_s));
141 init_avcodec();
143 vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name("mpeg1video");
144 if (!vf->priv->codec) {
145 mp_tmsg(MSGT_VFILTER,MSGL_ERR,"Cannot find codec '%s' in libavcodec...\n", "mpeg1video");
146 return 0;
149 vf->priv->context=avcodec_alloc_context();
150 vf->priv->pic = avcodec_alloc_frame();
152 // TODO: parse args ->
153 if(args) sscanf(args, "%d:%f", &p_quality, &p_fps);
155 if(p_quality<32){
156 // fixed qscale
157 lavc_venc_context.flags = CODEC_FLAG_QSCALE;
158 lavc_venc_context.global_quality =
159 vf->priv->pic->quality = (int)(FF_QP2LAMBDA * ((p_quality<1) ? 1 : p_quality) + 0.5);
160 } else {
161 // fixed bitrate (in kbits)
162 lavc_venc_context.bit_rate = 1000*p_quality;
164 lavc_venc_context.time_base.num = 1000*1001;
165 lavc_venc_context.time_base.den = (p_fps<1.0) ? 1000*1001*25 : (p_fps * lavc_venc_context.time_base.num);
166 lavc_venc_context.gop_size = 0; // I-only
167 lavc_venc_context.pix_fmt= PIX_FMT_YUV420P;
169 return 1;
172 const vf_info_t vf_info_lavc = {
173 "realtime mpeg1 encoding with libavcodec",
174 "lavc",
175 "A'rpi",
177 vf_open,
178 NULL
181 //===========================================================================//