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