Mercurial > mplayer.hg
annotate libmpcodecs/vf_lavc.c @ 13477:8e026ebb082f
--host-cc option for crosscompiling
author | faust3 |
---|---|
date | Sun, 26 Sep 2004 14:40:59 +0000 |
parents | d43c2028c00a |
children | 58bd9c33864c |
rev | line source |
---|---|
5873 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 #include <inttypes.h> | |
5 | |
6 #include "../config.h" | |
7 #include "../mp_msg.h" | |
8 #include "../help_mp.h" | |
9 | |
10 #ifdef USE_LIBAVCODEC | |
11 | |
12 #include "img_format.h" | |
13 #include "mp_image.h" | |
14 #include "vf.h" | |
15 | |
16 //#include "../libvo/fastmemcpy.h" | |
17 | |
18 #ifdef USE_LIBAVCODEC_SO | |
7004 | 19 #include <ffmpeg/avcodec.h> |
5873 | 20 #else |
21 #include "libavcodec/avcodec.h" | |
22 #endif | |
23 | |
8339 | 24 #if LIBAVCODEC_BUILD < 4641 |
11000 | 25 #error we do not support libavcodec prior to build 4641, get the latest libavcodec CVS |
8339 | 26 #endif |
27 | |
8413 | 28 #if LIBAVCODEC_BUILD < 4645 |
29 #warning your version of libavcodec is old, u might want to get a newer one | |
30 #endif | |
31 | |
32 #if LIBAVCODEC_BUILD < 4645 | |
33 #define AVFrame AVVideoFrame | |
34 #define coded_frame coded_picture | |
35 #endif | |
36 | |
11419 | 37 #if LIBAVCODEC_BUILD < 4684 |
38 #define FF_QP2LAMBDA 1 | |
39 #endif | |
40 | |
5873 | 41 extern int avcodec_inited; |
42 | |
43 struct vf_priv_s { | |
44 unsigned char* outbuf; | |
45 int outbuf_size; | |
7852 | 46 AVCodecContext* context; |
8413 | 47 AVFrame* pic; |
7852 | 48 AVCodec* codec; |
5873 | 49 vo_mpegpes_t pes; |
50 }; | |
51 | |
7852 | 52 #define lavc_venc_context (*vf->priv->context) |
5873 | 53 |
54 //===========================================================================// | |
55 | |
56 static int config(struct vf_instance_s* vf, | |
57 int width, int height, int d_width, int d_height, | |
58 unsigned int flags, unsigned int outfmt){ | |
59 if(vf_next_query_format(vf,IMGFMT_MPEGPES)<=0) return 0; | |
60 | |
61 lavc_venc_context.width = width; | |
62 lavc_venc_context.height = height; | |
6019 | 63 |
64 if(!lavc_venc_context.frame_rate){ | |
65 // guess FPS: | |
66 switch(height){ | |
67 case 240: | |
68 case 480: | |
9577 | 69 #if LIBAVCODEC_BUILD >= 4662 |
70 lavc_venc_context.frame_rate = 30000; | |
71 lavc_venc_context.frame_rate_base= 1001; | |
72 #else | |
6019 | 73 lavc_venc_context.frame_rate=29.97*FRAME_RATE_BASE; // NTSC |
9577 | 74 #endif |
6019 | 75 break; |
76 case 576: | |
77 case 288: | |
78 default: | |
9577 | 79 #if LIBAVCODEC_BUILD >= 4662 |
80 lavc_venc_context.frame_rate = 25; | |
81 lavc_venc_context.frame_rate_base= 1; | |
82 #else | |
6019 | 83 lavc_venc_context.frame_rate=25*FRAME_RATE_BASE; // PAL |
9577 | 84 #endif |
6019 | 85 break; |
86 // lavc_venc_context.frame_rate=vo_fps*FRAME_RATE_BASE; // same as src | |
87 } | |
88 } | |
5873 | 89 |
90 if(vf->priv->outbuf) free(vf->priv->outbuf); | |
91 | |
92 vf->priv->outbuf_size=10000+width*height; // must be enough! | |
93 vf->priv->outbuf = malloc(vf->priv->outbuf_size); | |
94 | |
95 if (avcodec_open(&lavc_venc_context, vf->priv->codec) != 0) { | |
96 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantOpenCodec); | |
97 return 0; | |
98 } | |
99 | |
100 if (lavc_venc_context.codec->encode == NULL) { | |
101 mp_msg(MSGT_MENCODER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n"); | |
102 return 0; | |
103 } | |
104 | |
105 return vf_next_config(vf,width,height,d_width,d_height,flags,IMGFMT_MPEGPES); | |
106 } | |
107 | |
7368 | 108 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){ |
5873 | 109 mp_image_t* dmpi; |
110 int out_size; | |
8413 | 111 AVFrame *pic= vf->priv->pic; |
5873 | 112 |
8339 | 113 pic->data[0]=mpi->planes[0]; |
114 pic->data[1]=mpi->planes[1]; | |
115 pic->data[2]=mpi->planes[2]; | |
116 pic->linesize[0]=mpi->stride[0]; | |
117 pic->linesize[1]=mpi->stride[1]; | |
118 pic->linesize[2]=mpi->stride[2]; | |
5873 | 119 |
120 out_size = avcodec_encode_video(&lavc_venc_context, | |
8339 | 121 vf->priv->outbuf, vf->priv->outbuf_size, pic); |
5873 | 122 |
7368 | 123 if(out_size<=0) return 1; |
5873 | 124 |
125 dmpi=vf_get_image(vf->next,IMGFMT_MPEGPES, | |
126 MP_IMGTYPE_EXPORT, 0, | |
127 mpi->w, mpi->h); | |
128 | |
129 vf->priv->pes.data=vf->priv->outbuf; | |
130 vf->priv->pes.size=out_size; | |
131 vf->priv->pes.id=0x1E0; | |
132 vf->priv->pes.timestamp=-1; // dunno | |
133 | |
7127 | 134 dmpi->planes[0]=(unsigned char*)&vf->priv->pes; |
5873 | 135 |
7368 | 136 return vf_next_put_image(vf,dmpi); |
5873 | 137 } |
138 | |
139 //===========================================================================// | |
140 | |
141 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
142 switch(fmt){ | |
143 case IMGFMT_YV12: | |
144 case IMGFMT_I420: | |
145 case IMGFMT_IYUV: | |
5876 | 146 return (vf_next_query_format(vf,IMGFMT_MPEGPES) & (~(VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_ACCEPT_STRIDE))); |
5873 | 147 } |
148 return 0; | |
149 } | |
150 | |
151 static int open(vf_instance_t *vf, char* args){ | |
6019 | 152 int p_quality=0; |
153 float p_fps=0; | |
154 | |
5873 | 155 vf->config=config; |
156 vf->put_image=put_image; | |
157 vf->query_format=query_format; | |
158 vf->priv=malloc(sizeof(struct vf_priv_s)); | |
159 memset(vf->priv,0,sizeof(struct vf_priv_s)); | |
160 | |
161 if (!avcodec_inited){ | |
162 avcodec_init(); | |
163 avcodec_register_all(); | |
164 avcodec_inited=1; | |
165 } | |
166 | |
167 vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name("mpeg1video"); | |
168 if (!vf->priv->codec) { | |
169 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_MissingLAVCcodec, "mpeg1video"); | |
170 return 0; | |
171 } | |
7852 | 172 |
173 vf->priv->context=avcodec_alloc_context(); | |
8413 | 174 #if LIBAVCODEC_BUILD >= 4645 |
175 vf->priv->pic = avcodec_alloc_frame(); | |
176 #else | |
177 vf->priv->pic = avcodec_alloc_picture(); | |
178 #endif | |
5873 | 179 |
180 // TODO: parse args -> | |
6019 | 181 if(args) sscanf(args, "%d:%f", &p_quality, &p_fps); |
182 | |
183 if(p_quality<32){ | |
184 // fixed qscale | |
185 lavc_venc_context.flags = CODEC_FLAG_QSCALE; | |
11257
837bca3ae69f
constant qscale was broken due to libavcodec changes, fix taken from ve_lavc.c
ranma
parents:
11000
diff
changeset
|
186 #if LIBAVCODEC_BUILD >= 4668 |
837bca3ae69f
constant qscale was broken due to libavcodec changes, fix taken from ve_lavc.c
ranma
parents:
11000
diff
changeset
|
187 lavc_venc_context.global_quality = |
837bca3ae69f
constant qscale was broken due to libavcodec changes, fix taken from ve_lavc.c
ranma
parents:
11000
diff
changeset
|
188 #endif |
837bca3ae69f
constant qscale was broken due to libavcodec changes, fix taken from ve_lavc.c
ranma
parents:
11000
diff
changeset
|
189 vf->priv->pic->quality = (int)(FF_QP2LAMBDA * ((p_quality<1) ? 1 : p_quality) + 0.5); |
6019 | 190 } else { |
191 // fixed bitrate (in kbits) | |
192 lavc_venc_context.bit_rate = 1000*p_quality; | |
193 } | |
9577 | 194 #if LIBAVCODEC_BUILD >= 4662 |
195 lavc_venc_context.frame_rate_base = 1000*1001; | |
196 lavc_venc_context.frame_rate = (p_fps<1.0) ? 0 : (p_fps * lavc_venc_context.frame_rate_base); | |
197 #else | |
198 lavc_venc_context.frame_rate = (p_fps<1.0) ? 0 : (p_fps * FRAME_RATE_BASE); | |
199 #endif | |
5873 | 200 lavc_venc_context.gop_size = 0; // I-only |
201 | |
202 return 1; | |
203 } | |
204 | |
205 vf_info_t vf_info_lavc = { | |
206 "realtime mpeg1 encoding with libavcodec", | |
207 "lavc", | |
208 "A'rpi", | |
209 "", | |
9593
e9a2af584986
Add the new -vf option wich is the same as vop in reverse order.
albeu
parents:
9577
diff
changeset
|
210 open, |
e9a2af584986
Add the new -vf option wich is the same as vop in reverse order.
albeu
parents:
9577
diff
changeset
|
211 NULL |
5873 | 212 }; |
213 | |
214 //===========================================================================// | |
215 #endif |