Mercurial > mplayer.hg
annotate libmpcodecs/ve_lavc.c @ 5630:dfc219577da6
FPS calculation fixed
author | arpi |
---|---|
date | Mon, 15 Apr 2002 02:48:11 +0000 |
parents | d9af91d38449 |
children | 93c9df5225a7 |
rev | line source |
---|---|
5550 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 | |
5 #include "../config.h" | |
6 | |
7 #ifdef USE_LIBAVCODEC | |
8 | |
9 #include "../mp_msg.h" | |
10 #include "../help_mp.h" | |
11 | |
12 #include "codec-cfg.h" | |
13 #include "stream.h" | |
14 #include "demuxer.h" | |
15 #include "stheader.h" | |
16 | |
17 #include "aviwrite.h" | |
18 | |
5607 | 19 #include "img_format.h" |
20 #include "mp_image.h" | |
5550 | 21 #include "vf.h" |
22 | |
23 #include "divx4_vbr.h" | |
24 | |
25 extern int pass; | |
26 extern char* passtmpfile; | |
27 | |
28 //===========================================================================// | |
29 | |
30 #ifdef USE_LIBAVCODEC_SO | |
31 #include <libffmpeg/avcodec.h> | |
32 #else | |
33 #include "libavcodec/avcodec.h" | |
34 #endif | |
35 | |
36 extern int avcodec_inited; | |
37 | |
38 /* video options */ | |
39 static char *lavc_param_vcodec = NULL; | |
40 static int lavc_param_vbitrate = -1; | |
41 static int lavc_param_vrate_tolerance = 1024*8; | |
42 static int lavc_param_vhq = 0; /* default is realtime encoding */ | |
43 static int lavc_param_v4mv = 0; | |
5556 | 44 static int lavc_param_vme = 4; |
5550 | 45 static int lavc_param_vqscale = 0; |
46 static int lavc_param_vqmin = 3; | |
47 static int lavc_param_vqmax = 15; | |
48 static int lavc_param_vqdiff = 3; | |
49 static float lavc_param_vqcompress = 0.5; | |
50 static float lavc_param_vqblur = 0.5; | |
51 static int lavc_param_keyint = -1; | |
52 | |
53 #include "cfgparser.h" | |
54 | |
55 #ifdef USE_LIBAVCODEC | |
56 struct config lavcopts_conf[]={ | |
57 {"vcodec", &lavc_param_vcodec, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
58 {"vbitrate", &lavc_param_vbitrate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
59 {"vratetol", &lavc_param_vrate_tolerance, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
60 {"vhq", &lavc_param_vhq, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
61 {"v4mv", &lavc_param_v4mv, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
62 {"vme", &lavc_param_vme, CONF_TYPE_INT, CONF_RANGE, 0, 5, NULL}, | |
63 {"vqscale", &lavc_param_vqscale, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
64 {"vqmin", &lavc_param_vqmin, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
65 {"vqmax", &lavc_param_vqmax, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
66 {"vqdiff", &lavc_param_vqdiff, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
67 {"vqcomp", &lavc_param_vqcompress, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL}, | |
68 {"vqblur", &lavc_param_vqblur, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL}, | |
69 {"keyint", &lavc_param_keyint, CONF_TYPE_INT, 0, 0, 0, NULL}, | |
70 {NULL, NULL, 0, 0, 0, 0, NULL} | |
71 }; | |
72 #endif | |
73 | |
74 struct vf_priv_s { | |
75 aviwrite_stream_t* mux; | |
76 AVCodecContext context; | |
77 AVCodec *codec; | |
78 }; | |
79 | |
80 #define mux_v (vf->priv->mux) | |
81 #define lavc_venc_context (vf->priv->context) | |
82 | |
83 static int config(struct vf_instance_s* vf, | |
84 int width, int height, int d_width, int d_height, | |
85 unsigned int flags, unsigned int outfmt){ | |
86 | |
87 mux_v->bih->biWidth=width; | |
88 mux_v->bih->biHeight=height; | |
89 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8); | |
90 | |
91 memset(&lavc_venc_context, 0, sizeof(lavc_venc_context)); | |
92 | |
93 printf("videocodec: libavcodec (%dx%d fourcc=%x [%.4s])\n", | |
94 mux_v->bih->biWidth, mux_v->bih->biHeight, mux_v->bih->biCompression, | |
95 (char *)&mux_v->bih->biCompression); | |
96 | |
97 lavc_venc_context.width = width; | |
98 lavc_venc_context.height = height; | |
99 if (lavc_param_vbitrate >= 0) /* != -1 */ | |
100 lavc_venc_context.bit_rate = lavc_param_vbitrate*1000; | |
101 else | |
102 lavc_venc_context.bit_rate = 800000; /* default */ | |
103 lavc_venc_context.bit_rate_tolerance= lavc_param_vrate_tolerance*1000; | |
104 lavc_venc_context.frame_rate = (float)mux_v->h.dwRate/mux_v->h.dwScale * FRAME_RATE_BASE; | |
105 lavc_venc_context.qmin= lavc_param_vqmin; | |
106 lavc_venc_context.qmax= lavc_param_vqmax; | |
107 lavc_venc_context.max_qdiff= lavc_param_vqdiff; | |
108 lavc_venc_context.qcompress= lavc_param_vqcompress; | |
109 lavc_venc_context.qblur= lavc_param_vqblur; | |
110 /* keyframe interval */ | |
111 if (lavc_param_keyint >= 0) /* != -1 */ | |
112 lavc_venc_context.gop_size = lavc_param_keyint; | |
113 else | |
114 lavc_venc_context.gop_size = 250; /* default */ | |
115 | |
116 if (lavc_param_vhq) | |
117 { | |
118 printf("High quality encoding selected (non real time)!\n"); | |
119 lavc_venc_context.flags = CODEC_FLAG_HQ; | |
120 } | |
121 else | |
122 lavc_venc_context.flags = 0; | |
123 | |
124 lavc_venc_context.flags|= lavc_param_v4mv ? CODEC_FLAG_4MV : 0; | |
125 | |
5624 | 126 #ifdef ME_ZERO |
127 // workaround Juanjo's stupid incompatible change: | |
5550 | 128 motion_estimation_method = lavc_param_vme; |
5624 | 129 #else |
130 lavc_venc_context.me_method = ME_ZERO+lavc_param_vme; | |
131 #endif | |
5550 | 132 |
133 /* fixed qscale :p */ | |
134 if (lavc_param_vqscale) | |
135 { | |
136 printf("Using constant qscale = %d (VBR)\n", lavc_param_vqscale); | |
137 lavc_venc_context.flags |= CODEC_FLAG_QSCALE; | |
138 lavc_venc_context.quality = lavc_param_vqscale; | |
139 } | |
140 | |
141 switch(pass){ | |
142 case 1: | |
143 if (VbrControl_init_2pass_vbr_analysis(passtmpfile, 5) == -1){ | |
144 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
145 pass=0; | |
146 } | |
147 break; | |
148 case 2: | |
149 if (VbrControl_init_2pass_vbr_encoding(passtmpfile, | |
150 lavc_venc_context.bit_rate, | |
151 (float)mux_v->h.dwRate/mux_v->h.dwScale, | |
152 100, /* crispness */ | |
153 5) == -1){ | |
154 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
155 pass=0; | |
156 } | |
157 break; | |
158 } | |
159 | |
160 if (avcodec_open(&lavc_venc_context, vf->priv->codec) != 0) { | |
161 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantOpenCodec); | |
162 return 0; | |
163 } | |
164 | |
165 if (lavc_venc_context.codec->encode == NULL) { | |
166 mp_msg(MSGT_MENCODER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n"); | |
167 return 0; | |
168 } | |
169 | |
170 return 1; | |
171 } | |
172 | |
173 static int control(struct vf_instance_s* vf, int request, void* data){ | |
174 | |
175 return CONTROL_UNKNOWN; | |
176 } | |
177 | |
178 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
179 switch(fmt){ | |
180 case IMGFMT_YV12: | |
181 case IMGFMT_IYUV: | |
182 case IMGFMT_I420: | |
5565
0b301fec999a
capabilities support -> automatic insertion of scale, expand, pp
arpi
parents:
5556
diff
changeset
|
183 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE; |
5550 | 184 } |
185 return 0; | |
186 } | |
187 | |
188 static void put_image(struct vf_instance_s* vf, mp_image_t *mpi){ | |
189 int out_size; | |
190 AVPicture lavc_venc_picture; | |
191 | |
192 lavc_venc_picture.data[0]=mpi->planes[0]; | |
193 lavc_venc_picture.data[1]=mpi->planes[1]; | |
194 lavc_venc_picture.data[2]=mpi->planes[2]; | |
195 lavc_venc_picture.linesize[0]=mpi->stride[0]; | |
196 lavc_venc_picture.linesize[1]=mpi->stride[1]; | |
197 lavc_venc_picture.linesize[2]=mpi->stride[2]; | |
198 | |
199 if(pass==2){ // handle 2-pass: | |
200 lavc_venc_context.flags|=CODEC_FLAG_QSCALE; // enable VBR | |
201 lavc_venc_context.quality=VbrControl_get_quant(); | |
202 #ifdef CODEC_FLAG_TYPE | |
203 lavc_venc_context.flags|=CODEC_FLAG_TYPE; // force keyframes | |
204 lavc_venc_context.key_frame=VbrControl_get_intra(); | |
205 lavc_venc_context.gop_size=0x3fffffff; | |
206 #else | |
207 #error you should upgrade libavcodec... get latest CVS | |
208 #endif | |
209 out_size = avcodec_encode_video(&lavc_venc_context, mux_v->buffer, mux_v->buffer_size, | |
210 &lavc_venc_picture); | |
211 VbrControl_update_2pass_vbr_encoding(lavc_venc_context.mv_bits, | |
212 lavc_venc_context.i_tex_bits+lavc_venc_context.p_tex_bits, | |
213 8*out_size); | |
214 } else { | |
215 out_size = avcodec_encode_video(&lavc_venc_context, mux_v->buffer, mux_v->buffer_size, | |
216 &lavc_venc_picture); | |
217 | |
218 if(pass==1){ | |
219 VbrControl_update_2pass_vbr_analysis(lavc_venc_context.key_frame, | |
220 lavc_venc_context.mv_bits, | |
221 lavc_venc_context.i_tex_bits+lavc_venc_context.p_tex_bits, | |
222 8*out_size, lavc_venc_context.quality); | |
223 } | |
224 | |
225 } | |
226 | |
227 mencoder_write_chunk(mux_v,out_size,lavc_venc_context.key_frame?0x10:0); | |
228 } | |
229 | |
5551 | 230 static void uninit(struct vf_instance_s* vf){ |
231 avcodec_close(&lavc_venc_context); | |
232 } | |
233 | |
5550 | 234 //===========================================================================// |
235 | |
236 static int vf_open(vf_instance_t *vf, char* args){ | |
5551 | 237 vf->uninit=uninit; |
5550 | 238 vf->config=config; |
239 vf->control=control; | |
240 vf->query_format=query_format; | |
241 vf->put_image=put_image; | |
242 vf->priv=malloc(sizeof(struct vf_priv_s)); | |
243 memset(vf->priv,0,sizeof(struct vf_priv_s)); | |
244 vf->priv->mux=args; | |
245 | |
246 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)); | |
247 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER); | |
248 mux_v->bih->biWidth=0; | |
249 mux_v->bih->biHeight=0; | |
250 mux_v->bih->biPlanes=1; | |
251 mux_v->bih->biBitCount=24; | |
252 if (!lavc_param_vcodec) | |
253 { | |
254 printf("No libavcodec codec specified! It's requested!\n"); | |
255 return 0; | |
256 } | |
257 | |
258 if (!strcasecmp(lavc_param_vcodec, "mpeg1") || !strcasecmp(lavc_param_vcodec, "mpeg1video")) | |
259 mux_v->bih->biCompression = mmioFOURCC('m', 'p', 'g', '1'); | |
260 else if (!strcasecmp(lavc_param_vcodec, "h263") || !strcasecmp(lavc_param_vcodec, "h263p")) | |
261 mux_v->bih->biCompression = mmioFOURCC('h', '2', '6', '3'); | |
262 else if (!strcasecmp(lavc_param_vcodec, "rv10")) | |
263 mux_v->bih->biCompression = mmioFOURCC('R', 'V', '1', '0'); | |
264 else if (!strcasecmp(lavc_param_vcodec, "mjpeg")) | |
265 mux_v->bih->biCompression = mmioFOURCC('M', 'J', 'P', 'G'); | |
266 else if (!strcasecmp(lavc_param_vcodec, "mpeg4")) | |
267 mux_v->bih->biCompression = mmioFOURCC('D', 'I', 'V', 'X'); | |
268 else if (!strcasecmp(lavc_param_vcodec, "msmpeg4")) | |
269 mux_v->bih->biCompression = mmioFOURCC('d', 'i', 'v', '3'); | |
270 else | |
271 mux_v->bih->biCompression = mmioFOURCC(lavc_param_vcodec[0], | |
272 lavc_param_vcodec[1], lavc_param_vcodec[2], lavc_param_vcodec[3]); /* FIXME!!! */ | |
273 | |
274 if (!avcodec_inited){ | |
275 avcodec_init(); | |
276 avcodec_register_all(); | |
277 avcodec_inited=1; | |
278 } | |
279 | |
280 vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name(lavc_param_vcodec); | |
281 if (!vf->priv->codec) { | |
282 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_MissingLAVCcodec, lavc_param_vcodec); | |
283 return 0; | |
284 } | |
285 | |
286 return 1; | |
287 } | |
288 | |
289 vf_info_t ve_info_lavc = { | |
290 "libavcodec encoder", | |
291 "lavc", | |
292 "A'rpi", | |
293 "for internal use by mencoder", | |
294 vf_open | |
295 }; | |
296 | |
297 //===========================================================================// | |
298 #endif |