comparison mencoder.c @ 4367:c2be4fb65cee

3-pass encoding support (reads frame skip/dup control and audio stream from frameno.avi)
author arpi
date Sat, 26 Jan 2002 22:30:02 +0000
parents 9e20866c3250
children fae96d93c662
comparison
equal deleted inserted replaced
4366:1a1842ff1827 4367:c2be4fb65cee
301 301
302 int main(int argc,char* argv[], char *envp[]){ 302 int main(int argc,char* argv[], char *envp[]){
303 303
304 stream_t* stream=NULL; 304 stream_t* stream=NULL;
305 demuxer_t* demuxer=NULL; 305 demuxer_t* demuxer=NULL;
306 stream_t* stream2=NULL;
307 demuxer_t* demuxer2=NULL;
306 demux_stream_t *d_audio=NULL; 308 demux_stream_t *d_audio=NULL;
307 demux_stream_t *d_video=NULL; 309 demux_stream_t *d_video=NULL;
308 demux_stream_t *d_dvdsub=NULL; 310 demux_stream_t *d_dvdsub=NULL;
309 sh_audio_t *sh_audio=NULL; 311 sh_audio_t *sh_audio=NULL;
310 sh_video_t *sh_video=NULL; 312 sh_video_t *sh_video=NULL;
333 double v_timer_corr=0; 335 double v_timer_corr=0;
334 336
335 play_tree_t* playtree; 337 play_tree_t* playtree;
336 play_tree_iter_t* playtree_iter; 338 play_tree_iter_t* playtree_iter;
337 char* filename=NULL; 339 char* filename=NULL;
340 char* frameno_filename="frameno.avi";
338 341
339 int decoded_frameno=0; 342 int decoded_frameno=0;
343 int next_frameno=-1;
340 344
341 //int out_buffer_size=0x200000; 345 //int out_buffer_size=0x200000;
342 //unsigned char* out_buffer=malloc(out_buffer_size); 346 //unsigned char* out_buffer=malloc(out_buffer_size);
343 347
344 mp_msg_init(MSGL_STATUS); 348 mp_msg_init(MSGL_STATUS);
367 divx4_param.max_quantizer = 31; 371 divx4_param.max_quantizer = 31;
368 divx4_param.rc_period = 2000; 372 divx4_param.rc_period = 2000;
369 divx4_param.rc_reaction_period = 10; 373 divx4_param.rc_reaction_period = 10;
370 divx4_param.rc_reaction_ratio = 20; 374 divx4_param.rc_reaction_ratio = 20;
371 #endif 375 #endif
376
377 // FIXME: get rid of -dvd and other tricky options and config/playtree
378 stream2=open_stream(frameno_filename,0,&i);
379 if(stream2){
380 demuxer2=demux_open(stream2,DEMUXER_TYPE_AVI,-1,-1,-2);
381 if(demuxer2) printf("Using pass3 control file: %s\n",frameno_filename);
382 }
372 383
373 playtree = play_tree_new(); 384 playtree = play_tree_new();
374 mconfig = m_config_new(playtree); 385 mconfig = m_config_new(playtree);
375 m_config_register_options(mconfig,mencoder_opts); 386 m_config_register_options(mconfig,mencoder_opts);
376 387
422 } 433 }
423 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_DVDauthOk); 434 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_DVDauthOk);
424 } 435 }
425 #endif 436 #endif
426 437
427 if(!has_audio) audio_id=-2; /* do NOT read audio packets... */ 438 if(!has_audio || demuxer2) audio_id=-2; /* do NOT read audio packets... */
428 439
429 //demuxer=demux_open(stream,file_format,video_id,audio_id,dvdsub_id); 440 //demuxer=demux_open(stream,file_format,video_id,audio_id,dvdsub_id);
430 demuxer=demux_open(stream,file_format,audio_id,video_id,dvdsub_id); 441 demuxer=demux_open(stream,file_format,audio_id,video_id,dvdsub_id);
431 if(!demuxer){ 442 if(!demuxer){
432 printf("Cannot open demuxer\n"); 443 printf("Cannot open demuxer\n");
433 exit(1); 444 exit(1);
434 } 445 }
435 446
436 d_audio=demuxer->audio; 447 d_audio=demuxer2 ? demuxer2->audio : demuxer->audio;
437 d_video=demuxer->video; 448 d_video=demuxer->video;
438 d_dvdsub=demuxer->sub; 449 d_dvdsub=demuxer->sub;
439 sh_audio=d_audio->sh; 450 sh_audio=d_audio->sh;
440 sh_video=d_video->sh; 451 sh_video=d_video->sh;
441 452
540 exit(1); 551 exit(1);
541 } 552 }
542 553
543 } // if(out_video_codec) 554 } // if(out_video_codec)
544 555
545 if(sh_audio){ 556 if(sh_audio && out_audio_codec){
546 // Go through the codec.conf and find the best codec... 557 // Go through the codec.conf and find the best codec...
547 sh_audio->codec=NULL; 558 sh_audio->codec=NULL;
548 if(audio_family!=-1) mp_msg(MSGT_MENCODER,MSGL_INFO,MSGTR_TryForceAudioFmt,audio_family); 559 if(audio_family!=-1) mp_msg(MSGT_MENCODER,MSGL_INFO,MSGTR_TryForceAudioFmt,audio_family);
549 while(1){ 560 while(1){
550 sh_audio->codec=find_codec(sh_audio->format,NULL,sh_audio->codec,1); 561 sh_audio->codec=find_codec(sh_audio->format,NULL,sh_audio->codec,1);
565 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); 576 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);
566 break; 577 break;
567 } 578 }
568 } 579 }
569 580
570 if(sh_audio){ 581 if(sh_audio && out_audio_codec){
571 mp_msg(MSGT_MENCODER,MSGL_V,"Initializing audio codec...\n"); 582 mp_msg(MSGT_MENCODER,MSGL_V,"Initializing audio codec...\n");
572 if(!init_audio(sh_audio)){ 583 if(!init_audio(sh_audio)){
573 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CouldntInitAudioCodec); 584 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CouldntInitAudioCodec);
574 sh_audio=d_audio->sh=NULL; 585 sh_audio=d_audio->sh=NULL;
575 } else { 586 } else {
1123 } 1134 }
1124 } else { 1135 } else {
1125 // VBR - encode/copy an audio frame 1136 // VBR - encode/copy an audio frame
1126 switch(mux_a->codec){ 1137 switch(mux_a->codec){
1127 case ACODEC_COPY: // copy 1138 case ACODEC_COPY: // copy
1128 printf("VBR audio framecopy not yet implemented!\n"); 1139 len=ds_get_packet(sh_audio->ds,(unsigned char**) &mux_a->buffer);
1140 // printf("VBR audio framecopy not yet implemented!\n");
1129 break; 1141 break;
1130 #ifdef HAVE_MP3LAME 1142 #ifdef HAVE_MP3LAME
1131 case ACODEC_VBRMP3: 1143 case ACODEC_VBRMP3:
1132 while(mux_a->buffer_len<4){ 1144 while(mux_a->buffer_len<4){
1133 unsigned char tmp[2304]; 1145 unsigned char tmp[2304];
1173 if(in_size<0){ eof=1; break; } 1185 if(in_size<0){ eof=1; break; }
1174 sh_video->timer+=frame_time; ++decoded_frameno; 1186 sh_video->timer+=frame_time; ++decoded_frameno;
1175 1187
1176 v_timer_corr-=frame_time-(float)mux_v->h.dwScale/mux_v->h.dwRate; 1188 v_timer_corr-=frame_time-(float)mux_v->h.dwScale/mux_v->h.dwRate;
1177 1189
1190 if(demuxer2){
1191 // find our frame:
1192 while(next_frameno<decoded_frameno){
1193 int* start;
1194 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1195 if(len<0){ eof=1;break;}
1196 if(len==0) --skip_flag; else // duplicate
1197 if(len==4) next_frameno=start[0];
1198 }
1199 if(eof) break;
1200 if(skip_flag) printf("!!!!!!!!!!!!\n");
1201 skip_flag=next_frameno-decoded_frameno;
1202 // find next frame:
1203 while(next_frameno<=decoded_frameno){
1204 int* start;
1205 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1206 if(len<0){ eof=1;break;}
1207 if(len==0) --skip_flag; else // duplicate
1208 if(len==4) next_frameno=start[0];
1209 }
1210 // if(eof) break;
1211 // printf("Current fno=%d requested=%d skip=%d \n",decoded_frameno,fno,skip_flag);
1212 } else {
1213
1178 // check frame duplicate/drop: 1214 // check frame duplicate/drop:
1179 1215
1180 if(v_timer_corr>=(float)mux_v->h.dwScale/mux_v->h.dwRate){ 1216 if(v_timer_corr>=(float)mux_v->h.dwScale/mux_v->h.dwRate){
1181 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate; 1217 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1182 ++skip_flag; // skip 1218 ++skip_flag; // skip
1195 || (v_pts_corr>=2*(float)mux_v->h.dwScale/mux_v->h.dwRate) ) 1231 || (v_pts_corr>=2*(float)mux_v->h.dwScale/mux_v->h.dwRate) )
1196 if(skip_flag<=0){ // we can't skip more than 1 frame now 1232 if(skip_flag<=0){ // we can't skip more than 1 frame now
1197 v_pts_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate; 1233 v_pts_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1198 ++skip_flag; // skip 1234 ++skip_flag; // skip
1199 } 1235 }
1236
1237 } // demuxer2
1200 1238
1201 #ifdef USE_DVDREAD 1239 #ifdef USE_DVDREAD
1202 // DVD sub: 1240 // DVD sub:
1203 if(vo_spudec){ 1241 if(vo_spudec){
1204 unsigned char* packet=NULL; 1242 unsigned char* packet=NULL;
1225 break; 1263 break;
1226 } 1264 }
1227 mux_v->buffer = vo_image_ptr; 1265 mux_v->buffer = vo_image_ptr;
1228 aviwrite_write_chunk(muxer,mux_v,muxer_f,mux_v->buffer_size,0x10); 1266 aviwrite_write_chunk(muxer,mux_v,muxer_f,mux_v->buffer_size,0x10);
1229 break; 1267 break;
1230 case VCODEC_FRAMENO: 1268 case VCODEC_FRAMENO: {
1231 mux_v->buffer=&decoded_frameno; // tricky 1269 mux_v->buffer=&decoded_frameno; // tricky
1232 if(skip_flag<=0) aviwrite_write_chunk(muxer,mux_v,muxer_f,sizeof(int),0x10); 1270 if(skip_flag<=0) aviwrite_write_chunk(muxer,mux_v,muxer_f,sizeof(int),0x10);
1233 break; 1271 break; }
1234 case VCODEC_DIVX4: 1272 case VCODEC_DIVX4:
1235 #ifndef HAVE_DIVX4ENCORE 1273 #ifndef HAVE_DIVX4ENCORE
1236 printf("No support for Divx4 encore compiled in\n"); 1274 printf("No support for Divx4 encore compiled in\n");
1237 return 0; /* FIXME */ 1275 return 0; /* FIXME */
1238 #else 1276 #else
1307 // skip frame 1345 // skip frame
1308 printf("\nskip frame!!! \n"); 1346 printf("\nskip frame!!! \n");
1309 --skip_flag; 1347 --skip_flag;
1310 } 1348 }
1311 1349
1312 if(sh_audio){ 1350 if(sh_audio && !demuxer2){
1313 float AV_delay,x; 1351 float AV_delay,x;
1314 // A-V sync! 1352 // A-V sync!
1315 if(pts_from_bps){ 1353 if(pts_from_bps){
1316 unsigned int samples=(sh_audio->audio.dwSampleSize)? 1354 unsigned int samples=(sh_audio->audio.dwSampleSize)?
1317 ((ds_tell(d_audio)-sh_audio->a_in_buffer_len)/sh_audio->audio.dwSampleSize) : 1355 ((ds_tell(d_audio)-sh_audio->a_in_buffer_len)/sh_audio->audio.dwSampleSize) :