Mercurial > mplayer.hg
annotate mencoder.c @ 3491:c9aca79b7527
x11control, direct rendering and some minor changes
author | iive |
---|---|
date | Fri, 14 Dec 2001 18:14:27 +0000 |
parents | 9943dbacb48e |
children | 21fc87d76300 |
rev | line source |
---|---|
3384 | 1 #define VCODEC_COPY 0 |
3361
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
2 #define VCODEC_FRAMENO 1 |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
3 #define VCODEC_DIVX4 2 |
3480 | 4 #define VCODEC_RAW 3 |
3361
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
5 |
3385 | 6 #define ACODEC_COPY 0 |
2583 | 7 #define ACODEC_PCM 1 |
8 #define ACODEC_VBRMP3 2 | |
2531 | 9 |
10 #include <stdio.h> | |
11 #include <stdlib.h> | |
12 #include <string.h> | |
13 #include <signal.h> | |
14 | |
2591 | 15 #include "config.h" |
2531 | 16 #include "mp_msg.h" |
2978 | 17 #include "version.h" |
2531 | 18 #include "help_mp.h" |
19 | |
2978 | 20 static char* banner_text= |
21 "\n\n" | |
22 "MEncoder " VERSION "(C) 2000-2001 Arpad Gereoffy (see DOCS!)\n" | |
23 "\n"; | |
24 | |
3323 | 25 #include "cpudetect.h" |
26 | |
27 | |
2531 | 28 #include "codec-cfg.h" |
29 | |
30 #include "stream.h" | |
31 #include "demuxer.h" | |
32 #include "stheader.h" | |
33 | |
34 #include "aviwrite.h" | |
35 | |
2897 | 36 #ifdef USE_LIBVO2 |
37 #include "libvo2/libvo2.h" | |
38 #else | |
2531 | 39 #include "libvo/video_out.h" |
2897 | 40 #endif |
2531 | 41 |
2574 | 42 #include "dec_audio.h" |
43 #include "dec_video.h" | |
44 | |
2531 | 45 #include <encore2.h> |
2643 | 46 #include "divx4_vbr.h" |
2531 | 47 |
3357 | 48 #ifdef HAVE_MP3LAME |
2591 | 49 #include <lame/lame.h> |
3357 | 50 #endif |
2583 | 51 |
3236 | 52 #include <inttypes.h> |
53 #include "../postproc/swscale.h" | |
54 | |
3385 | 55 #include "fastmemcpy.h" |
56 | |
2583 | 57 //-------------------------- |
58 | |
2531 | 59 // cache2: |
2618 | 60 static int stream_cache_size=0; |
2531 | 61 #ifdef USE_STREAM_CACHE |
62 extern int cache_fill_status; | |
63 #else | |
64 #define cache_fill_status 0 | |
65 #endif | |
66 | |
2618 | 67 int vcd_track=0; |
68 int audio_id=-1; | |
69 int video_id=-1; | |
70 int dvdsub_id=-1; | |
71 | |
2531 | 72 char *audio_codec=NULL; // override audio codec |
73 char *video_codec=NULL; // override video codec | |
74 int audio_family=-1; // override audio codec family | |
75 int video_family=-1; // override video codec family | |
76 | |
3357 | 77 #ifdef HAVE_MP3LAME |
2661 | 78 int out_audio_codec=ACODEC_VBRMP3; |
3357 | 79 #else |
80 int out_audio_codec=ACODEC_PCM; | |
81 #endif | |
82 | |
2661 | 83 int out_video_codec=VCODEC_DIVX4; |
84 | |
2531 | 85 // audio stream skip/resync functions requires only for seeking. |
86 // (they should be implemented in the audio codec layer) | |
87 //void skip_audio_frame(sh_audio_t *sh_audio){} | |
88 //void resync_audio_stream(sh_audio_t *sh_audio){} | |
89 | |
2618 | 90 int verbose=0; // must be global! |
2531 | 91 |
92 double video_time_usage=0; | |
93 double vout_time_usage=0; | |
94 static double audio_time_usage=0; | |
95 static int total_time_usage_start=0; | |
96 static int benchmark=0; | |
97 | |
2605 | 98 // A-V sync: |
99 int delay_corrected=1; | |
100 static float default_max_pts_correction=-1;//0.01f; | |
101 static float max_pts_correction=0;//default_max_pts_correction; | |
102 static float c_total=0; | |
103 | |
2613 | 104 float force_fps=0; |
105 float force_ofps=0; // set to 24 for inverse telecine | |
2531 | 106 |
2618 | 107 int force_srate=0; |
108 | |
2626 | 109 char* out_filename="test.avi"; |
110 char* mp3_filename=NULL; | |
111 char* ac3_filename=NULL; | |
112 | |
2643 | 113 static int pass=0; |
114 static char* passtmpfile="divx2pass.log"; | |
3377
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
115 int pass_working=0; |
2643 | 116 |
117 static int play_n_frames=-1; | |
118 | |
2661 | 119 //char *out_audio_codec=NULL; // override audio codec |
120 //char *out_video_codec=NULL; // override video codec | |
2626 | 121 |
2591 | 122 //#include "libmpeg2/mpeg2.h" |
123 //#include "libmpeg2/mpeg2_internal.h" | |
124 | |
2626 | 125 ENC_PARAM divx4_param; |
2643 | 126 int divx4_crispness=100; |
2626 | 127 |
3357 | 128 #ifdef HAVE_MP3LAME |
2626 | 129 int lame_param_quality=0; // best |
130 int lame_param_vbr=vbr_default; | |
131 int lame_param_mode=-1; // unset | |
132 int lame_param_padding=-1; // unset | |
133 int lame_param_br=-1; // unset | |
134 int lame_param_ratio=-1; // unset | |
3357 | 135 #endif |
2626 | 136 |
3236 | 137 static int scale_srcW=0; |
138 static int scale_srcH=0; | |
139 static int vo_w=0, vo_h=0; | |
2618 | 140 //-------------------------- config stuff: |
141 | |
142 #include "cfgparser.h" | |
143 | |
144 static int cfg_inc_verbose(struct config *conf){ ++verbose; return 0;} | |
145 | |
146 static int cfg_include(struct config *conf, char *filename){ | |
147 return parse_config_file(conf, filename); | |
148 } | |
149 | |
150 #include "get_path.c" | |
151 | |
152 #include "cfg-mplayer-def.h" | |
153 #include "cfg-mencoder.h" | |
154 | |
2591 | 155 //--------------------------------------------------------------------------- |
156 | |
2627 | 157 // dummy datas for gui :( |
158 | |
159 #ifdef HAVE_NEW_GUI | |
160 float rel_seek_secs=0; | |
161 int abs_seek_pos=0; | |
162 int use_gui=0; | |
163 | |
164 void exit_player(char* how){ | |
165 } | |
166 void vo_x11_putkey(int key){ | |
167 } | |
168 void vo_setwindow( int w,int g ) { | |
169 } | |
170 void vo_setwindowsize( int w,int h ) { | |
171 } | |
172 | |
173 int vo_resize = 0; | |
174 int vo_expose = 0; | |
175 | |
176 #endif | |
177 // --- | |
178 | |
2591 | 179 // mini dummy libvo: |
2531 | 180 |
181 static unsigned char* vo_image=NULL; | |
182 static unsigned char* vo_image_ptr=NULL; | |
183 | |
184 static uint32_t draw_slice(uint8_t *src[], int stride[], int w,int h, int x0,int y0){ | |
185 int y; | |
3236 | 186 // printf("draw_slice %dx%d %d;%d\n",w,h,x0,y0); |
187 if(scale_srcW) | |
188 { | |
189 uint8_t* dstPtr[3]= { | |
190 vo_image, | |
191 vo_image + vo_w*vo_h*5/4, | |
192 vo_image + vo_w*vo_h}; | |
193 SwScale_YV12slice(src, stride, y0, h, dstPtr, vo_w, 12, scale_srcW, scale_srcH, vo_w, vo_h); | |
194 } | |
195 else | |
196 { | |
2531 | 197 // copy Y: |
198 for(y=0;y<h;y++){ | |
2639 | 199 unsigned char* s=src[0]+stride[0]*y; |
2531 | 200 unsigned char* d=vo_image+vo_w*(y0+y)+x0; |
201 memcpy(d,s,w); | |
202 } | |
203 x0>>=1;y0>>=1; | |
204 w>>=1;h>>=1; | |
205 // copy U: | |
206 for(y=0;y<h;y++){ | |
2639 | 207 unsigned char* s=src[2]+stride[2]*y; |
2531 | 208 unsigned char* d=vo_image+vo_w*vo_h+(vo_w>>1)*(y0+y)+x0; |
209 memcpy(d,s,w); | |
210 } | |
211 // copy V: | |
212 for(y=0;y<h;y++){ | |
2639 | 213 unsigned char* s=src[1]+stride[1]*y; |
2531 | 214 unsigned char* d=vo_image+vo_w*vo_h+vo_w*vo_h/4+(vo_w>>1)*(y0+y)+x0; |
215 memcpy(d,s,w); | |
216 } | |
3236 | 217 } // !swscaler |
2531 | 218 } |
219 | |
220 static uint32_t draw_frame(uint8_t *src[]){ | |
221 // printf("This function shouldn't be called - report bug!\n"); | |
222 // later: add YUY2->YV12 conversion here! | |
223 vo_image_ptr=src[0]; | |
224 } | |
225 | |
226 vo_functions_t video_out; | |
227 | |
2591 | 228 //--------------------------------------------------------------------------- |
229 | |
230 int dec_audio(sh_audio_t *sh_audio,unsigned char* buffer,int total){ | |
231 int size=0; | |
232 int eof=0; | |
233 while(size<total && !eof){ | |
234 int len=total-size; | |
235 if(len>MAX_OUTBURST) len=MAX_OUTBURST; | |
236 if(len>sh_audio->a_buffer_size) len=sh_audio->a_buffer_size; | |
237 if(len>sh_audio->a_buffer_len){ | |
238 int ret=decode_audio(sh_audio, | |
239 &sh_audio->a_buffer[sh_audio->a_buffer_len], | |
240 len-sh_audio->a_buffer_len, | |
241 sh_audio->a_buffer_size-sh_audio->a_buffer_len); | |
242 if(ret>0) sh_audio->a_buffer_len+=ret; else eof=1; | |
243 } | |
244 if(len>sh_audio->a_buffer_len) len=sh_audio->a_buffer_len; | |
245 memcpy(buffer+size,sh_audio->a_buffer,len); | |
246 sh_audio->a_buffer_len-=len; size+=len; | |
247 if(sh_audio->a_buffer_len>0) | |
248 memcpy(sh_audio->a_buffer,&sh_audio->a_buffer[len],sh_audio->a_buffer_len); | |
249 } | |
250 return size; | |
251 } | |
252 | |
253 //--------------------------------------------------------------------------- | |
2531 | 254 |
255 static int eof=0; | |
3320
ac8b70dd5e45
use return 1; if interrupted - patch by Artur Skawina <skawina@geocities.com>
arpi
parents:
3240
diff
changeset
|
256 static int interrupted=0; |
2531 | 257 |
258 static void exit_sighandler(int x){ | |
259 eof=1; | |
3320
ac8b70dd5e45
use return 1; if interrupted - patch by Artur Skawina <skawina@geocities.com>
arpi
parents:
3240
diff
changeset
|
260 interrupted=1; |
2531 | 261 } |
262 | |
2618 | 263 int main(int argc,char* argv[], char *envp[]){ |
2531 | 264 |
265 stream_t* stream=NULL; | |
266 demuxer_t* demuxer=NULL; | |
267 demux_stream_t *d_audio=NULL; | |
268 demux_stream_t *d_video=NULL; | |
269 demux_stream_t *d_dvdsub=NULL; | |
270 sh_audio_t *sh_audio=NULL; | |
271 sh_video_t *sh_video=NULL; | |
272 int file_format=DEMUXER_TYPE_UNKNOWN; | |
273 int i; | |
274 unsigned int out_fmt; | |
275 | |
276 aviwrite_t* muxer=NULL; | |
277 aviwrite_stream_t* mux_a=NULL; | |
278 aviwrite_stream_t* mux_v=NULL; | |
279 FILE* muxer_f=NULL; | |
280 | |
281 ENC_FRAME enc_frame; | |
282 ENC_RESULT enc_result; | |
283 void* enc_handle=NULL; | |
284 | |
3357 | 285 #ifdef HAVE_MP3LAME |
2591 | 286 lame_global_flags *lame; |
3357 | 287 #endif |
2583 | 288 |
2653 | 289 float audio_preload=0.5; |
2581 | 290 |
2613 | 291 double v_pts_corr=0; |
292 double v_timer_corr=0; | |
2605 | 293 |
2618 | 294 char** filenames=NULL; |
295 char* filename=NULL; | |
296 int num_filenames; | |
297 | |
3361
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
298 int decoded_frameno=0; |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
299 |
2531 | 300 //int out_buffer_size=0x200000; |
301 //unsigned char* out_buffer=malloc(out_buffer_size); | |
302 | |
2622 | 303 mp_msg_init(MSGL_STATUS); |
2978 | 304 mp_msg(MSGT_CPLAYER,MSGL_INFO,"%s",banner_text); |
2622 | 305 |
306 // check codec.conf | |
307 if(!parse_codec_cfg(get_path("codecs.conf"))){ | |
308 if(!parse_codec_cfg(DATADIR"/codecs.conf")){ | |
309 mp_msg(MSGT_MENCODER,MSGL_HINT,MSGTR_CopyCodecsConf); | |
310 exit(0); | |
311 } | |
312 } | |
2531 | 313 |
3323 | 314 /* Test for cpu capabilities (and corresponding OS support) for optimizing */ |
315 #ifdef ARCH_X86 | |
316 GetCpuCaps(&gCpuCaps); | |
317 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: Type: %d MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n", | |
318 gCpuCaps.cpuType,gCpuCaps.hasMMX,gCpuCaps.hasMMX2, | |
319 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt, | |
320 gCpuCaps.hasSSE, gCpuCaps.hasSSE2); | |
321 #endif | |
322 | |
323 | |
2643 | 324 // set some defaults, before parsing configfile/commandline: |
325 divx4_param.min_quantizer = 2; | |
326 divx4_param.max_quantizer = 31; | |
327 divx4_param.rc_period = 2000; | |
328 divx4_param.rc_reaction_period = 10; | |
329 divx4_param.rc_reaction_ratio = 20; | |
330 | |
331 | |
2618 | 332 num_filenames=parse_command_line(conf, argc, argv, envp, &filenames); |
333 if(num_filenames<0) exit(1); // error parsing cmdline | |
3378 | 334 if(!num_filenames && !vcd_track && !dvd_title && !tv_param_on){ |
2618 | 335 printf("\nMissing filename!\n\n"); |
336 exit(1); | |
337 } | |
338 | |
2622 | 339 mp_msg_init(verbose+MSGL_STATUS); |
2600 | 340 |
2618 | 341 filename=(num_filenames>0)?filenames[0]:NULL; |
342 stream=open_stream(filename,vcd_track,&file_format); | |
2531 | 343 |
344 if(!stream){ | |
345 printf("Cannot open file/device\n"); | |
346 exit(1); | |
347 } | |
348 | |
349 printf("success: format: %d data: 0x%X - 0x%X\n",file_format, (int)(stream->start_pos),(int)(stream->end_pos)); | |
350 | |
2618 | 351 if(stream_cache_size) stream_enable_cache(stream,stream_cache_size*1024); |
2531 | 352 |
2882 | 353 //demuxer=demux_open(stream,file_format,video_id,audio_id,dvdsub_id); |
354 demuxer=demux_open(stream,file_format,audio_id,video_id,dvdsub_id); | |
2531 | 355 if(!demuxer){ |
356 printf("Cannot open demuxer\n"); | |
357 exit(1); | |
358 } | |
359 | |
360 d_audio=demuxer->audio; | |
361 d_video=demuxer->video; | |
362 d_dvdsub=demuxer->sub; | |
363 sh_audio=d_audio->sh; | |
364 sh_video=d_video->sh; | |
365 | |
366 if(!video_read_properties(sh_video)){ | |
367 printf("Couldn't read video properties\n"); | |
368 exit(1); | |
369 } | |
370 | |
2622 | 371 mp_msg(MSGT_MENCODER,MSGL_INFO,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.2f ftime:=%6.4f\n", |
2531 | 372 demuxer->file_format,sh_video->format, sh_video->disp_w,sh_video->disp_h, |
373 sh_video->fps,sh_video->frametime | |
374 ); | |
375 | |
376 | |
377 sh_video->codec=NULL; | |
3361
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
378 if(out_video_codec>1){ |
2884 | 379 |
2622 | 380 if(video_family!=-1) mp_msg(MSGT_MENCODER,MSGL_INFO,MSGTR_TryForceVideoFmt,video_family); |
2531 | 381 while(1){ |
382 sh_video->codec=find_codec(sh_video->format, | |
383 sh_video->bih?((unsigned int*) &sh_video->bih->biCompression):NULL,sh_video->codec,0); | |
384 if(!sh_video->codec){ | |
385 if(video_family!=-1) { | |
386 sh_video->codec=NULL; /* re-search */ | |
2622 | 387 mp_msg(MSGT_MENCODER,MSGL_WARN,MSGTR_CantFindVfmtFallback); |
2531 | 388 video_family=-1; |
389 continue; | |
390 } | |
2622 | 391 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantFindVideoCodec,sh_video->format); |
392 mp_msg(MSGT_MENCODER,MSGL_HINT, MSGTR_TryUpgradeCodecsConfOrRTFM,get_path("codecs.conf")); | |
2531 | 393 exit(1); |
394 } | |
395 if(video_codec && strcmp(sh_video->codec->name,video_codec)) continue; | |
396 else if(video_family!=-1 && sh_video->codec->driver!=video_family) continue; | |
397 break; | |
398 } | |
399 | |
2622 | 400 mp_msg(MSGT_MENCODER,MSGL_INFO,"%s video codec: [%s] drv:%d (%s)\n",video_codec?"Forcing":"Detected",sh_video->codec->name,sh_video->codec->driver,sh_video->codec->info); |
2531 | 401 |
402 for(i=0;i<CODECS_MAX_OUTFMT;i++){ | |
403 out_fmt=sh_video->codec->outfmt[i]; | |
404 if(out_fmt==0xFFFFFFFF) continue; | |
405 if(out_fmt==IMGFMT_YV12) break; | |
406 if(out_fmt==IMGFMT_I420) break; | |
407 if(out_fmt==IMGFMT_IYUV) break; | |
408 if(out_fmt==IMGFMT_YUY2) break; | |
2825 | 409 if(out_fmt==IMGFMT_UYVY) break; |
2531 | 410 } |
411 if(i>=CODECS_MAX_OUTFMT){ | |
2622 | 412 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_VOincompCodec); |
2531 | 413 exit(1); // exit_player(MSGTR_Exit_error); |
414 } | |
415 sh_video->outfmtidx=i; | |
416 | |
3240 | 417 if(out_fmt==IMGFMT_YV12 && (vo_w!=0 || vo_h!=0)) |
3236 | 418 { |
419 scale_srcW= sh_video->disp_w; | |
420 scale_srcH= sh_video->disp_h; | |
3240 | 421 if(!vo_w) vo_w=sh_video->disp_w; |
422 if(!vo_h) vo_h=sh_video->disp_h; | |
3236 | 423 } |
424 else | |
425 { | |
426 vo_w=sh_video->disp_w; | |
427 vo_h=sh_video->disp_h; | |
428 } | |
429 | |
2531 | 430 if(out_fmt==IMGFMT_YV12 || out_fmt==IMGFMT_I420 || out_fmt==IMGFMT_IYUV){ |
431 vo_image=malloc(vo_w*vo_h*3/2); | |
432 vo_image_ptr=vo_image; | |
433 } | |
434 | |
435 if(!init_video(sh_video)){ | |
2622 | 436 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_CouldntInitVideoCodec); |
2531 | 437 exit(1); |
438 } | |
439 | |
2884 | 440 } // if(out_video_codec) |
2581 | 441 |
442 if(sh_audio){ | |
443 // Go through the codec.conf and find the best codec... | |
444 sh_audio->codec=NULL; | |
2622 | 445 if(audio_family!=-1) mp_msg(MSGT_MENCODER,MSGL_INFO,MSGTR_TryForceAudioFmt,audio_family); |
2581 | 446 while(1){ |
447 sh_audio->codec=find_codec(sh_audio->format,NULL,sh_audio->codec,1); | |
448 if(!sh_audio->codec){ | |
449 if(audio_family!=-1) { | |
450 sh_audio->codec=NULL; /* re-search */ | |
2622 | 451 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantFindAfmtFallback); |
2581 | 452 audio_family=-1; |
453 continue; | |
454 } | |
2622 | 455 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantFindAudioCodec,sh_audio->format); |
456 mp_msg(MSGT_MENCODER,MSGL_HINT, MSGTR_TryUpgradeCodecsConfOrRTFM,get_path("codecs.conf")); | |
2581 | 457 sh_audio=d_audio->sh=NULL; |
458 break; | |
459 } | |
460 if(audio_codec && strcmp(sh_audio->codec->name,audio_codec)) continue; | |
461 else if(audio_family!=-1 && sh_audio->codec->driver!=audio_family) continue; | |
2622 | 462 mp_msg(MSGT_MENCODER,MSGL_INFO,"%s audio codec: [%s] drv:%d (%s)\n",audio_codec?"Forcing":"Detected",sh_audio->codec->name,sh_audio->codec->driver,sh_audio->codec->info); |
2581 | 463 break; |
464 } | |
465 } | |
466 | |
467 if(sh_audio){ | |
2622 | 468 mp_msg(MSGT_MENCODER,MSGL_V,"Initializing audio codec...\n"); |
2581 | 469 if(!init_audio(sh_audio)){ |
2622 | 470 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CouldntInitAudioCodec); |
2581 | 471 sh_audio=d_audio->sh=NULL; |
472 } else { | |
2622 | 473 mp_msg(MSGT_MENCODER,MSGL_INFO,"AUDIO: srate=%d chans=%d bps=%d sfmt=0x%X ratio: %d->%d\n",sh_audio->samplerate,sh_audio->channels,sh_audio->samplesize, |
2581 | 474 sh_audio->sample_format,sh_audio->i_bps,sh_audio->o_bps); |
475 } | |
476 } | |
477 | |
478 | |
479 | |
2531 | 480 // set up video encoder: |
3236 | 481 SwScale_Init(); |
2531 | 482 video_out.draw_slice=draw_slice; |
483 video_out.draw_frame=draw_frame; | |
484 | |
485 // set up output file: | |
2626 | 486 muxer_f=fopen(out_filename,"wb"); |
2887
bc648c6a464a
fixes a segfault if file specified in -o can't be accessed
pl
parents:
2884
diff
changeset
|
487 if(!muxer_f) { |
bc648c6a464a
fixes a segfault if file specified in -o can't be accessed
pl
parents:
2884
diff
changeset
|
488 printf("Cannot open output file '%s'\n", out_filename); |
bc648c6a464a
fixes a segfault if file specified in -o can't be accessed
pl
parents:
2884
diff
changeset
|
489 exit(1); |
bc648c6a464a
fixes a segfault if file specified in -o can't be accessed
pl
parents:
2884
diff
changeset
|
490 } |
bc648c6a464a
fixes a segfault if file specified in -o can't be accessed
pl
parents:
2884
diff
changeset
|
491 |
2531 | 492 muxer=aviwrite_new_muxer(); |
2581 | 493 |
494 // ============= VIDEO =============== | |
495 | |
2531 | 496 mux_v=aviwrite_new_stream(muxer,AVIWRITE_TYPE_VIDEO); |
497 | |
498 mux_v->buffer_size=0x200000; | |
499 mux_v->buffer=malloc(mux_v->buffer_size); | |
500 | |
501 mux_v->source=sh_video; | |
502 | |
503 mux_v->h.dwSampleSize=0; // VBR | |
504 mux_v->h.dwScale=10000; | |
2613 | 505 mux_v->h.dwRate=mux_v->h.dwScale*(force_ofps?force_ofps:sh_video->fps); |
2531 | 506 |
2661 | 507 mux_v->codec=out_video_codec; |
2574 | 508 |
2531 | 509 switch(mux_v->codec){ |
3384 | 510 case VCODEC_COPY: |
511 printf("sh_video->bih: %x\n", sh_video->bih); | |
512 if (sh_video->bih) | |
513 mux_v->bih=sh_video->bih; | |
514 else | |
515 { | |
516 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)); | |
517 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER); | |
518 mux_v->bih->biWidth=sh_video->disp_w; | |
519 mux_v->bih->biHeight=sh_video->disp_h; | |
520 mux_v->bih->biCompression=sh_video->format; | |
521 mux_v->bih->biPlanes=1; | |
522 mux_v->bih->biBitCount=24; // FIXME!!! | |
523 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8); | |
524 } | |
525 printf("videocodec: framecopy (%dx%d %dbpp fourcc=%x)\n", | |
526 mux_v->bih->biWidth, mux_v->bih->biHeight, | |
527 mux_v->bih->biBitCount, mux_v->bih->biCompression); | |
2531 | 528 break; |
3480 | 529 case VCODEC_RAW: |
530 printf("sh_video->bih: %x\n", sh_video->bih); | |
531 if (sh_video->bih) | |
532 mux_v->bih=sh_video->bih; | |
533 else | |
534 { | |
535 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)); | |
536 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER); | |
537 mux_v->bih->biWidth=sh_video->disp_w; | |
538 mux_v->bih->biHeight=sh_video->disp_h; | |
539 mux_v->bih->biCompression=0; | |
540 mux_v->bih->biPlanes=1; | |
541 mux_v->bih->biBitCount=24; // FIXME!!! | |
542 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8); | |
543 } | |
544 mux_v->bih->biCompression=0; | |
545 printf("videocodec: raw (%dx%d %dbpp fourcc=%x)\n", | |
546 mux_v->bih->biWidth, mux_v->bih->biHeight, | |
547 mux_v->bih->biBitCount, mux_v->bih->biCompression); | |
548 break; | |
3361
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
549 case VCODEC_FRAMENO: |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
550 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)); |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
551 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER); |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
552 mux_v->bih->biWidth=vo_w; |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
553 mux_v->bih->biHeight=vo_h; |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
554 mux_v->bih->biPlanes=1; |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
555 mux_v->bih->biBitCount=24; |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
556 mux_v->bih->biCompression=mmioFOURCC('F','r','N','o'); |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
557 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8); |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
558 break; |
2531 | 559 case VCODEC_DIVX4: |
560 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)); | |
561 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER); | |
3236 | 562 mux_v->bih->biWidth=vo_w; |
563 mux_v->bih->biHeight=vo_h; | |
2635
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2627
diff
changeset
|
564 mux_v->bih->biPlanes=1; |
2531 | 565 mux_v->bih->biBitCount=24; |
566 mux_v->bih->biCompression=mmioFOURCC('d','i','v','x'); | |
567 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8); | |
3377
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
568 |
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
569 if (pass) |
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
570 printf("Divx: 2-pass logfile: %s\n", passtmpfile); |
2531 | 571 break; |
572 } | |
573 | |
2581 | 574 // ============= AUDIO =============== |
575 if(sh_audio){ | |
576 | |
577 mux_a=aviwrite_new_stream(muxer,AVIWRITE_TYPE_AUDIO); | |
578 | |
579 mux_a->buffer_size=0x100000; //16384; | |
580 mux_a->buffer=malloc(mux_a->buffer_size); | |
581 | |
582 mux_a->source=sh_audio; | |
583 | |
2661 | 584 mux_a->codec=out_audio_codec; |
2581 | 585 |
586 switch(mux_a->codec){ | |
3385 | 587 case ACODEC_COPY: |
588 printf("sh_audio->wf: %x\n", sh_audio->wf); | |
2581 | 589 mux_a->h.dwSampleSize=sh_audio->audio.dwSampleSize; |
590 mux_a->h.dwScale=sh_audio->audio.dwScale; | |
591 mux_a->h.dwRate=sh_audio->audio.dwRate; | |
3385 | 592 if (sh_audio->wf) |
593 mux_a->wf=sh_audio->wf; | |
594 else | |
595 { | |
596 mux_a->wf = malloc(sizeof(WAVEFORMATEX)); | |
597 mux_a->wf->nBlockAlign = mux_a->h.dwSampleSize; | |
598 mux_a->wf->wFormatTag = sh_audio->sample_format; | |
599 mux_a->wf->nChannels = sh_audio->channels; | |
600 mux_a->wf->nSamplesPerSec = sh_audio->samplerate; | |
601 mux_a->wf->nAvgBytesPerSec=mux_a->h.dwSampleSize*mux_a->wf->nSamplesPerSec; | |
3480 | 602 mux_a->wf->wBitsPerSample = 16; // FIXME |
3385 | 603 mux_a->wf->cbSize=0; // FIXME for l3codeca.acm |
604 } | |
605 printf("audiocodec: framecopy (format=%x chans=%d rate=%d bits=%d)\n", | |
606 mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec, | |
607 mux_a->wf->wBitsPerSample); | |
2581 | 608 break; |
2583 | 609 case ACODEC_PCM: |
610 printf("CBR PCM audio selected\n"); | |
611 mux_a->h.dwSampleSize=2*sh_audio->channels; | |
612 mux_a->h.dwScale=1; | |
613 mux_a->h.dwRate=sh_audio->samplerate; | |
614 mux_a->wf=malloc(sizeof(WAVEFORMATEX)); | |
615 mux_a->wf->nBlockAlign=mux_a->h.dwSampleSize; | |
616 mux_a->wf->wFormatTag=0x1; // PCM | |
617 mux_a->wf->nChannels=sh_audio->channels; | |
618 mux_a->wf->nSamplesPerSec=sh_audio->samplerate; | |
619 mux_a->wf->nAvgBytesPerSec=mux_a->h.dwSampleSize*mux_a->wf->nSamplesPerSec; | |
620 mux_a->wf->wBitsPerSample=16; | |
621 mux_a->wf->cbSize=0; // FIXME for l3codeca.acm | |
622 break; | |
2581 | 623 case ACODEC_VBRMP3: |
3385 | 624 printf("MP3 audio selected\n"); |
2581 | 625 mux_a->h.dwSampleSize=0; // VBR |
2653 | 626 mux_a->h.dwScale=1152; // samples/frame |
2581 | 627 mux_a->h.dwRate=sh_audio->samplerate; |
2635
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2627
diff
changeset
|
628 if(sizeof(MPEGLAYER3WAVEFORMAT)!=30) mp_msg(MSGT_MENCODER,MSGL_WARN,"sizeof(MPEGLAYER3WAVEFORMAT)==%d!=30, maybe broken C compiler?\n",sizeof(MPEGLAYER3WAVEFORMAT)); |
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2627
diff
changeset
|
629 mux_a->wf=malloc(sizeof(MPEGLAYER3WAVEFORMAT)); // should be 30 |
2581 | 630 mux_a->wf->wFormatTag=0x55; // MP3 |
631 mux_a->wf->nChannels=sh_audio->channels; | |
2639 | 632 mux_a->wf->nSamplesPerSec=force_srate?force_srate:sh_audio->samplerate; |
2635
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2627
diff
changeset
|
633 mux_a->wf->nAvgBytesPerSec=192000/8; // FIXME! |
2653 | 634 mux_a->wf->nBlockAlign=1152; // requires for l3codeca.acm + WMP 6.4 |
2635
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2627
diff
changeset
|
635 mux_a->wf->wBitsPerSample=0; //16; |
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2627
diff
changeset
|
636 // from NaNdub: (requires for l3codeca.acm) |
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2627
diff
changeset
|
637 mux_a->wf->cbSize=12; |
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2627
diff
changeset
|
638 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->wID=1; |
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2627
diff
changeset
|
639 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->fdwFlags=2; |
2653 | 640 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->nBlockSize=1152; // ??? |
2635
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2627
diff
changeset
|
641 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->nFramesPerBlock=1; |
c1e24e01601b
fixed AVI header creation - now should be compatible with NaNdub
arpi
parents:
2627
diff
changeset
|
642 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->nCodecDelay=0; |
2581 | 643 break; |
644 } | |
645 } | |
646 | |
2840
808fe0767cf8
fix typos - patch by Colin Marquardt <colin@marquardt-home.de>
pl
parents:
2825
diff
changeset
|
647 printf("Writing AVI header...\n"); |
2531 | 648 aviwrite_write_header(muxer,muxer_f); |
649 | |
650 switch(mux_v->codec){ | |
3384 | 651 case VCODEC_COPY: |
3480 | 652 case VCODEC_RAW: |
2531 | 653 break; |
3361
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
654 case VCODEC_FRAMENO: |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
655 decoded_frameno=0; |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
656 break; |
2531 | 657 case VCODEC_DIVX4: |
658 // init divx4linux: | |
3236 | 659 divx4_param.x_dim=vo_w; |
660 divx4_param.y_dim=vo_h; | |
2626 | 661 divx4_param.framerate=(float)mux_v->h.dwRate/mux_v->h.dwScale; |
662 if(!divx4_param.bitrate) divx4_param.bitrate=800000; | |
663 else if(divx4_param.bitrate<=16000) divx4_param.bitrate*=1000; | |
664 if(!divx4_param.quality) divx4_param.quality=5; // the quality of compression ( 1 - fastest, 5 - best ) | |
665 divx4_param.handle=NULL; | |
666 encore(NULL,ENC_OPT_INIT,&divx4_param,NULL); | |
667 enc_handle=divx4_param.handle; | |
2643 | 668 switch(out_fmt){ |
669 case IMGFMT_YV12: enc_frame.colorspace=ENC_CSP_YV12; break; | |
670 case IMGFMT_IYUV: | |
671 case IMGFMT_I420: enc_frame.colorspace=ENC_CSP_I420; break; | |
672 case IMGFMT_YUY2: enc_frame.colorspace=ENC_CSP_YUY2; break; | |
673 case IMGFMT_UYVY: enc_frame.colorspace=ENC_CSP_UYVY; break; | |
674 case IMGFMT_RGB24: | |
675 case IMGFMT_BGR24: | |
676 enc_frame.colorspace=ENC_CSP_RGB24; break; | |
677 default: | |
678 mp_msg(MSGT_MENCODER,MSGL_ERR,"divx4: unsupported out_fmt!\n"); | |
679 } | |
680 switch(pass){ | |
681 case 1: | |
3377
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
682 if (VbrControl_init_2pass_vbr_analysis(passtmpfile, divx4_param.quality) == -1) |
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
683 { |
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
684 printf("2pass failed: filename=%s\n", passtmpfile); |
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
685 pass_working = 0; |
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
686 } |
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
687 else |
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
688 pass_working = 1; |
2643 | 689 break; |
690 case 2: | |
3377
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
691 if (VbrControl_init_2pass_vbr_encoding(passtmpfile, |
2643 | 692 divx4_param.bitrate, |
693 divx4_param.framerate, | |
694 divx4_crispness, | |
3377
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
695 divx4_param.quality) == -1) |
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
696 { |
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
697 printf("2pass failed: filename=%s\n", passtmpfile); |
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
698 pass_working = 0; |
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
699 } |
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
700 else |
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
701 pass_working = 1; |
2643 | 702 break; |
703 } | |
2531 | 704 break; |
705 } | |
706 | |
2600 | 707 if(sh_audio) |
2583 | 708 switch(mux_a->codec){ |
3357 | 709 #ifdef HAVE_MP3LAME |
2583 | 710 case ACODEC_VBRMP3: |
711 | |
712 lame=lame_init(); | |
2591 | 713 lame_set_bWriteVbrTag(lame,0); |
2583 | 714 lame_set_in_samplerate(lame,sh_audio->samplerate); |
715 lame_set_num_channels(lame,mux_a->wf->nChannels); | |
2639 | 716 lame_set_out_samplerate(lame,mux_a->wf->nSamplesPerSec); |
2626 | 717 if(lame_param_vbr){ // VBR: |
718 lame_set_VBR(lame,lame_param_vbr); // vbr mode | |
719 lame_set_VBR_q(lame,lame_param_quality+1); // 1 = best vbr q 6=~128k | |
720 if(lame_param_br>0) lame_set_VBR_mean_bitrate_kbps(lame,lame_param_br); | |
721 } else { // CBR: | |
722 lame_set_quality(lame,lame_param_quality); // 0 = best q | |
723 if(lame_param_br>0) lame_set_brate(lame,lame_param_br); | |
724 } | |
725 if(lame_param_mode>=0) lame_set_mode(lame,lame_param_mode); // j-st | |
726 if(lame_param_ratio>0) lame_set_compression_ratio(lame,lame_param_ratio); | |
2583 | 727 lame_init_params(lame); |
2622 | 728 if(verbose){ |
2626 | 729 lame_print_config(lame); |
730 lame_print_internals(lame); | |
2622 | 731 } |
3357 | 732 break; |
733 #endif | |
2583 | 734 |
735 } | |
736 | |
2531 | 737 signal(SIGINT,exit_sighandler); // Interrupt from keyboard |
738 signal(SIGQUIT,exit_sighandler); // Quit from keyboard | |
739 signal(SIGTERM,exit_sighandler); // kill | |
740 | |
741 while(!eof){ | |
742 | |
2571 | 743 float frame_time=0; |
2531 | 744 int blit_frame=0; |
745 float a_pts=0; | |
746 float v_pts=0; | |
2574 | 747 unsigned char* start=NULL; |
748 int in_size; | |
2613 | 749 int skip_flag=0; // 1=skip -1=duplicate |
2531 | 750 |
2643 | 751 if(play_n_frames>=0){ |
752 --play_n_frames; | |
753 if(play_n_frames<0) break; | |
754 } | |
755 | |
2581 | 756 if(sh_audio){ |
757 // get audio: | |
2583 | 758 while(mux_a->timer-audio_preload<mux_v->timer){ |
2653 | 759 int len=0; |
2581 | 760 if(mux_a->h.dwSampleSize){ |
2605 | 761 // CBR - copy 0.5 sec of audio |
2583 | 762 switch(mux_a->codec){ |
3385 | 763 case ACODEC_COPY: // copy |
2583 | 764 len=sh_audio->i_bps/2; |
765 len/=mux_a->h.dwSampleSize;if(len<1) len=1; | |
766 len*=mux_a->h.dwSampleSize; | |
767 len=demux_read_data(sh_audio->ds,mux_a->buffer,len); | |
768 break; | |
769 case ACODEC_PCM: | |
770 len=mux_a->h.dwSampleSize*(mux_a->h.dwRate/2); | |
2591 | 771 len=dec_audio(sh_audio,mux_a->buffer,len); |
2583 | 772 break; |
773 } | |
2581 | 774 } else { |
2605 | 775 // VBR - encode/copy an audio frame |
776 switch(mux_a->codec){ | |
3385 | 777 case ACODEC_COPY: // copy |
778 printf("VBR audio framecopy not yet implemented!\n"); | |
2605 | 779 break; |
3357 | 780 #ifdef HAVE_MP3LAME |
2605 | 781 case ACODEC_VBRMP3: |
2591 | 782 while(mux_a->buffer_len<4){ |
783 unsigned char tmp[2304]; | |
784 int len=dec_audio(sh_audio,tmp,2304); | |
785 if(len<=0) break; // eof | |
786 len=lame_encode_buffer_interleaved(lame, | |
787 tmp,len/4, | |
788 mux_a->buffer+mux_a->buffer_len,mux_a->buffer_size-mux_a->buffer_len); | |
789 if(len<0) break; // error | |
790 mux_a->buffer_len+=len; | |
791 } | |
792 if(mux_a->buffer_len<4) break; | |
793 len=mp_decode_mp3_header(mux_a->buffer); | |
2639 | 794 //printf("%d\n",len); |
2591 | 795 if(len<=0) break; // bad frame! |
796 while(mux_a->buffer_len<len){ | |
797 unsigned char tmp[2304]; | |
798 int len=dec_audio(sh_audio,tmp,2304); | |
799 if(len<=0) break; // eof | |
800 len=lame_encode_buffer_interleaved(lame, | |
801 tmp,len/4, | |
802 mux_a->buffer+mux_a->buffer_len,mux_a->buffer_size-mux_a->buffer_len); | |
803 if(len<0) break; // error | |
804 mux_a->buffer_len+=len; | |
805 } | |
2605 | 806 break; |
3357 | 807 #endif |
2605 | 808 } |
2581 | 809 } |
2583 | 810 if(len<=0) break; // EOF? |
811 aviwrite_write_chunk(muxer,mux_a,muxer_f,len,0); | |
2655 | 812 if(!mux_a->h.dwSampleSize && mux_a->timer>0) |
3354 | 813 mux_a->wf->nAvgBytesPerSec=0.5f+(double)mux_a->size/mux_a->timer; // avg bps (VBR) |
2591 | 814 if(mux_a->buffer_len>=len){ |
815 mux_a->buffer_len-=len; | |
816 memcpy(mux_a->buffer,mux_a->buffer+len,mux_a->buffer_len); | |
817 } | |
2581 | 818 } |
819 } | |
820 | |
821 // get video frame! | |
822 in_size=video_read_frame(sh_video,&frame_time,&start,force_fps); | |
823 if(in_size<0){ eof=1; break; } | |
3361
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
824 sh_video->timer+=frame_time; ++decoded_frameno; |
2613 | 825 |
826 v_timer_corr-=frame_time-(float)mux_v->h.dwScale/mux_v->h.dwRate; | |
2531 | 827 |
2613 | 828 // check frame duplicate/drop: |
829 | |
830 if(v_timer_corr>=(float)mux_v->h.dwScale/mux_v->h.dwRate){ | |
831 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate; | |
832 ++skip_flag; // skip | |
833 } else | |
834 while(v_timer_corr<=-(float)mux_v->h.dwScale/mux_v->h.dwRate){ | |
835 v_timer_corr+=(float)mux_v->h.dwScale/mux_v->h.dwRate; | |
836 --skip_flag; // dup | |
837 } | |
2531 | 838 |
2613 | 839 while( (v_pts_corr<=-(float)mux_v->h.dwScale/mux_v->h.dwRate && skip_flag>0) |
840 || (v_pts_corr<=-2*(float)mux_v->h.dwScale/mux_v->h.dwRate) ){ | |
841 v_pts_corr+=(float)mux_v->h.dwScale/mux_v->h.dwRate; | |
842 --skip_flag; // dup | |
843 } | |
844 if( (v_pts_corr>=(float)mux_v->h.dwScale/mux_v->h.dwRate && skip_flag<0) | |
845 || (v_pts_corr>=2*(float)mux_v->h.dwScale/mux_v->h.dwRate) ) | |
846 if(skip_flag<=0){ // we can't skip more than 1 frame now | |
847 v_pts_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate; | |
848 ++skip_flag; // skip | |
849 } | |
850 | |
851 | |
2531 | 852 switch(mux_v->codec){ |
3384 | 853 case VCODEC_COPY: |
2574 | 854 mux_v->buffer=start; |
2639 | 855 if(skip_flag<=0) aviwrite_write_chunk(muxer,mux_v,muxer_f,in_size,(sh_video->ds->flags&1)?0x10:0); |
2574 | 856 break; |
3480 | 857 case VCODEC_RAW: |
858 blit_frame=decode_video(&video_out,sh_video,start,in_size,0); | |
859 if(skip_flag>0) break; | |
860 if(!blit_frame){ | |
861 // empty. | |
862 aviwrite_write_chunk(muxer,mux_v,muxer_f,0,0); | |
863 break; | |
864 } | |
865 mux_v->buffer = vo_image_ptr; | |
866 aviwrite_write_chunk(muxer,mux_v,muxer_f,mux_v->buffer_size,0x10); | |
867 break; | |
3361
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
868 case VCODEC_FRAMENO: |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
869 mux_v->buffer=&decoded_frameno; // tricky |
3363
1459912caea5
set all frames to keyframes for -ovc frameno - allow seeking in resulting audio-only avi
arpi
parents:
3362
diff
changeset
|
870 if(skip_flag<=0) aviwrite_write_chunk(muxer,mux_v,muxer_f,sizeof(int),0x10); |
3361
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
871 break; |
2531 | 872 case VCODEC_DIVX4: |
2574 | 873 blit_frame=decode_video(&video_out,sh_video,start,in_size,0); |
2639 | 874 if(skip_flag>0) break; |
2574 | 875 if(!blit_frame){ |
876 // empty. | |
877 aviwrite_write_chunk(muxer,mux_v,muxer_f,0,0); | |
878 break; | |
879 } | |
2531 | 880 enc_frame.image=vo_image_ptr; |
881 enc_frame.bitstream=mux_v->buffer; | |
882 enc_frame.length=mux_v->buffer_size; | |
2643 | 883 enc_frame.mvs=NULL; |
2531 | 884 enc_frame.quant=0; |
885 enc_frame.intra=0; | |
3377
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
886 if(pass==2 && pass_working){ // handle 2-pass: |
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
887 enc_frame.quant = VbrControl_get_quant(); |
2643 | 888 enc_frame.intra = VbrControl_get_intra(); |
889 encore(enc_handle,ENC_OPT_ENCODE_VBR,&enc_frame,&enc_result); | |
890 VbrControl_update_2pass_vbr_encoding(enc_result.motion_bits, | |
891 enc_result.texture_bits, | |
892 enc_result.total_bits); | |
893 } else { | |
894 encore(enc_handle,ENC_OPT_ENCODE,&enc_frame,&enc_result); | |
3377
4723f6fd750a
do not fault if 2pass VbrControl can't open the logfile
alex
parents:
3363
diff
changeset
|
895 if(pass==1 && pass_working){ |
2643 | 896 VbrControl_update_2pass_vbr_analysis(enc_result.is_key_frame, |
897 enc_result.motion_bits, | |
898 enc_result.texture_bits, | |
899 enc_result.total_bits, | |
900 enc_result.quantizer); | |
901 } | |
902 } | |
903 | |
2531 | 904 // printf("encoding...\n"); |
905 // printf(" len=%d key:%d qualt:%d \n",enc_frame.length,enc_result.is_key_frame,enc_result.quantizer); | |
906 aviwrite_write_chunk(muxer,mux_v,muxer_f,enc_frame.length,enc_result.is_key_frame?0x10:0); | |
907 break; | |
908 } | |
2613 | 909 |
910 if(skip_flag<0){ | |
2605 | 911 // duplicate frame |
2613 | 912 printf("\nduplicate %d frame(s)!!! \n",-skip_flag); |
913 while(skip_flag<0){ | |
914 aviwrite_write_chunk(muxer,mux_v,muxer_f,0,0); | |
915 ++skip_flag; | |
916 } | |
2639 | 917 } else |
918 if(skip_flag>0){ | |
2605 | 919 // skip frame |
920 printf("\nskip frame!!! \n"); | |
2613 | 921 --skip_flag; |
2605 | 922 } |
923 | |
924 if(sh_audio){ | |
925 float AV_delay,x; | |
926 // A-V sync! | |
927 if(pts_from_bps){ | |
928 unsigned int samples=(sh_audio->audio.dwSampleSize)? | |
929 ((ds_tell(d_audio)-sh_audio->a_in_buffer_len)/sh_audio->audio.dwSampleSize) : | |
930 (d_audio->pack_no); // <- used for VBR audio | |
931 a_pts=samples*(float)sh_audio->audio.dwScale/(float)sh_audio->audio.dwRate; | |
932 delay_corrected=1; | |
933 } else { | |
934 // PTS = (last timestamp) + (bytes after last timestamp)/(bytes per sec) | |
935 a_pts=d_audio->pts; | |
936 if(!delay_corrected) if(a_pts) delay_corrected=1; | |
937 //printf("*** %5.3f ***\n",a_pts); | |
938 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps; | |
939 } | |
940 v_pts=d_video->pts; | |
941 // av = compensated (with out buffering delay) A-V diff | |
2613 | 942 AV_delay=(a_pts-v_pts); AV_delay-=mux_a->timer-(mux_v->timer-(v_timer_corr+v_pts_corr)); |
2605 | 943 // compensate input video timer by av: |
944 x=AV_delay*0.1f; | |
945 if(x<-max_pts_correction) x=-max_pts_correction; else | |
946 if(x> max_pts_correction) x= max_pts_correction; | |
947 if(default_max_pts_correction>=0) | |
948 max_pts_correction=default_max_pts_correction; | |
949 else | |
950 max_pts_correction=sh_video->frametime*0.10; // +-10% of time | |
951 // sh_video->timer-=x; | |
952 c_total+=x; | |
2613 | 953 v_pts_corr+=x; |
2605 | 954 |
2613 | 955 printf("A:%6.1f V:%6.1f A-V:%7.3f oAV:%7.3f diff:%7.3f ct:%7.3f vpc:%7.3f \r", |
2605 | 956 a_pts,v_pts,a_pts-v_pts, |
957 (float)(mux_a->timer-mux_v->timer), | |
2613 | 958 AV_delay, c_total, v_pts_corr ); |
2605 | 959 |
960 } | |
961 | |
962 #if 0 | |
963 mp_msg(MSGT_AVSYNC,MSGL_STATUS,"A:%6.1f V:%6.1f A-V:%7.3f ct:%7.3f %3d/%3d %2d%% %2d%% %4.1f%% %d%%\r", | |
964 a_pts,v_pts,a_pts-v_pts,c_total, | |
965 (int)sh_video->num_frames,(int)sh_video->num_frames_decoded, | |
966 (sh_video->timer>0.5)?(int)(100.0*video_time_usage/(double)sh_video->timer):0, | |
967 (sh_video->timer>0.5)?(int)(100.0*vout_time_usage/(double)sh_video->timer):0, | |
968 (sh_video->timer>0.5)?(100.0*audio_time_usage/(double)sh_video->timer):0 | |
969 ,cache_fill_status | |
970 ); | |
971 #endif | |
972 | |
973 fflush(stdout); | |
974 | |
2531 | 975 |
976 | |
977 } // while(!eof) | |
978 | |
3357 | 979 #ifdef HAVE_MP3LAME |
980 // fixup CBR mp3 audio header: | |
3354 | 981 if(sh_audio && mux_a->codec==ACODEC_VBRMP3 && !lame_param_vbr){ |
982 mux_a->h.dwSampleSize=1; | |
983 mux_a->h.dwRate=mux_a->wf->nAvgBytesPerSec; | |
984 mux_a->h.dwScale=1; | |
985 printf("\n\nCBR audio effective bitrate: %8.3f kbit/s (%d bytes/sec)\n", | |
986 mux_a->h.dwRate*8.0f/1000.0f,mux_a->h.dwRate); | |
987 } | |
3357 | 988 #endif |
3354 | 989 |
2840
808fe0767cf8
fix typos - patch by Colin Marquardt <colin@marquardt-home.de>
pl
parents:
2825
diff
changeset
|
990 printf("\nWriting AVI index...\n"); |
2531 | 991 aviwrite_write_index(muxer,muxer_f); |
2622 | 992 printf("Fixup AVI header...\n"); |
2531 | 993 fseek(muxer_f,0,SEEK_SET); |
994 aviwrite_write_header(muxer,muxer_f); // update header | |
995 fclose(muxer_f); | |
996 | |
3361
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
997 printf("\nVideo stream: %8.3f kbit/s (%d bps) size: %d bytes %5.3f secs\n", |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
998 (float)(mux_v->size/mux_v->timer*8.0f/1000.0f), (int)(mux_v->size/mux_v->timer), mux_v->size, (float)mux_v->timer); |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
999 if(sh_audio) |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
1000 printf("\nAudio stream: %8.3f kbit/s (%d bps) size: %d bytes %5.3f secs\n", |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
1001 (float)(mux_a->size/mux_a->timer*8.0f/1000.0f), (int)(mux_a->size/mux_a->timer), mux_a->size, (float)mux_a->timer); |
5d70491f438c
new video codec: frameno (just the number of frame - for 3-pass encoding)
arpi
parents:
3357
diff
changeset
|
1002 |
2618 | 1003 if(stream) free_stream(stream); // kill cache thread |
1004 | |
3320
ac8b70dd5e45
use return 1; if interrupted - patch by Artur Skawina <skawina@geocities.com>
arpi
parents:
3240
diff
changeset
|
1005 return interrupted; |
2531 | 1006 } |