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