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