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