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