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