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