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