comparison mplayer.c @ 1439:dabb5054353a

-autoq option...
author arpi
date Sat, 04 Aug 2001 13:52:41 +0000
parents 1728d249c783
children e795527de16a
comparison
equal deleted inserted replaced
1438:007b1ddd01e8 1439:dabb5054353a
227 #else 227 #else
228 #define GUI_MSG(x) 228 #define GUI_MSG(x)
229 #endif 229 #endif
230 230
231 // options: 231 // options:
232
233 int divx_quality=0;
234 int auto_quality=0;
235 int output_quality=0;
236
232 int osd_level=2; 237 int osd_level=2;
233 int divx_quality=0;
234 int auto_quality=-1;
235 char *seek_to_sec=NULL; 238 char *seek_to_sec=NULL;
236 off_t seek_to_byte=0; 239 off_t seek_to_byte=0;
237 int has_audio=1; 240 int has_audio=1;
238 char *audio_codec=NULL; // override audio codec 241 char *audio_codec=NULL; // override audio codec
239 char *video_codec=NULL; // override video codec 242 char *video_codec=NULL; // override video codec
395 #ifdef USE_LIBVO2 398 #ifdef USE_LIBVO2
396 extern int decode_video(vo2_handle_t *video_out,sh_video_t *sh_video,unsigned char *start,int in_size,int drop_frame); 399 extern int decode_video(vo2_handle_t *video_out,sh_video_t *sh_video,unsigned char *start,int in_size,int drop_frame);
397 #else 400 #else
398 extern int decode_video(vo_functions_t *video_out,sh_video_t *sh_video,unsigned char *start,int in_size,int drop_frame); 401 extern int decode_video(vo_functions_t *video_out,sh_video_t *sh_video,unsigned char *start,int in_size,int drop_frame);
399 #endif 402 #endif
403
404 extern int get_video_quality_max(sh_video_t *sh_video);
405 extern void set_video_quality(sh_video_t *sh_video,int quality);
400 406
401 #include "mixer.h" 407 #include "mixer.h"
402 #include "cfg-mplayer.h" 408 #include "cfg-mplayer.h"
403 409
404 void parse_cfgfiles( void ) 410 void parse_cfgfiles( void )
669 len=lseek(f,0,SEEK_END); lseek(f,0,SEEK_SET); 675 len=lseek(f,0,SEEK_END); lseek(f,0,SEEK_SET);
670 if (len == -1) 676 if (len == -1)
671 perror("Error: lseek failed to obtain video file size"); 677 perror("Error: lseek failed to obtain video file size");
672 else 678 else
673 #ifdef _LARGEFILE_SOURCE 679 #ifdef _LARGEFILE_SOURCE
674 fprintf(stderr, "File size is %lld bytes\n", (long long)len); 680 printf("File size is %lld bytes\n", (long long)len);
675 #else 681 #else
676 fprintf(stderr, "File size is %u bytes\n", (unsigned int)len); 682 printf("File size is %u bytes\n", (unsigned int)len);
677 #endif 683 #endif
678 stream=new_stream(f,STREAMTYPE_FILE); 684 stream=new_stream(f,STREAMTYPE_FILE);
679 stream->end_pos=len; 685 stream->end_pos=len;
680 #ifdef STREAMING 686 #ifdef STREAMING
681 } else { 687 } else {
894 fprintf(stderr,"FATAL: Couldn't initialize video codec :(\n"); 900 fprintf(stderr,"FATAL: Couldn't initialize video codec :(\n");
895 GUI_MSG( mplUnknowError ) 901 GUI_MSG( mplUnknowError )
896 exit(1); 902 exit(1);
897 } 903 }
898 904
905 if(auto_quality>0){
906 // Auto quality option enabled
907 output_quality=get_video_quality_max(sh_video);
908 if(auto_quality>output_quality) auto_quality=output_quality;
909 else output_quality=auto_quality;
910 printf("AutoQ: setting quality to %d\n",output_quality);
911 set_video_quality(sh_video,output_quality);
912 }
913
899 // ================== Init output files for encoding =============== 914 // ================== Init output files for encoding ===============
900 if(encode_name){ 915 if(encode_name){
901 // encode file!!! 916 // encode file!!!
902 FILE *encode_file=fopen(encode_name,"rb"); 917 FILE *encode_file=fopen(encode_name,"rb");
903 if(encode_file){ 918 if(encode_file){
1011 fflush(stdout); 1026 fflush(stdout);
1012 1027
1013 //================== MAIN: ========================== 1028 //================== MAIN: ==========================
1014 { 1029 {
1015 1030
1016 //float frame_correction=0; // average of A-V timestamp differences
1017 //int frame_corr_num=0; // 1031 //int frame_corr_num=0; //
1018 //float v_frame=0; // Video 1032 //float v_frame=0; // Video
1019 float time_frame=0; // Timer 1033 float time_frame=0; // Timer
1020 int eof=0; 1034 int eof=0;
1021 int force_redraw=0; 1035 int force_redraw=0;
1022 //float num_frames=0; // number of frames played 1036 //float num_frames=0; // number of frames played
1023 int grab_frames=0; 1037 int grab_frames=0;
1024 char osd_text_buffer[64]; 1038 char osd_text_buffer[64];
1025 int drop_frame=0; 1039 int drop_frame=0;
1026 int drop_frame_cnt=0; 1040 int drop_frame_cnt=0;
1041 // for auto-quality:
1042 float frame_correction=0; // average of A-V timestamp differences
1043 double cvideo_base_vtime;
1044 double cvideo_base_vframe;
1045 double vdecode_time;
1027 1046
1028 #ifdef HAVE_LIRC 1047 #ifdef HAVE_LIRC
1029 #ifdef HAVE_GUI 1048 #ifdef HAVE_GUI
1030 if ( nogui ) 1049 if ( nogui )
1031 #endif 1050 #endif
1143 InitTimer(); 1162 InitTimer();
1144 1163
1145 total_time_usage_start=GetTimer(); 1164 total_time_usage_start=GetTimer();
1146 1165
1147 while(!eof){ 1166 while(!eof){
1167 unsigned int aq_total_time=GetTimer();
1168 float aq_sleep_time=0;
1148 1169
1149 if(play_n_frames>=0){ 1170 if(play_n_frames>=0){
1150 --play_n_frames; 1171 --play_n_frames;
1151 if(play_n_frames<0) exit_player("Requested number of frames played"); 1172 if(play_n_frames<0) exit_player("Requested number of frames played");
1152 } 1173 }
1205 } 1226 }
1206 #endif 1227 #endif
1207 1228
1208 /*========================== PLAY VIDEO ============================*/ 1229 /*========================== PLAY VIDEO ============================*/
1209 1230
1231 cvideo_base_vframe=sh_video->timer;
1232 cvideo_base_vtime=video_time_usage;
1233
1210 if(1) 1234 if(1)
1211 while(1){ 1235 while(1){
1212 1236
1213 float frame_time=1; 1237 float frame_time=1;
1214 float pts1=d_video->pts; 1238 float pts1=d_video->pts;
1215 int blit_frame=0; 1239 int blit_frame=0;
1216 1240
1217 current_module="decode_video"; 1241 current_module="decode_video";
1218 1242
1219 //-------------------- Decode a frame: ----------------------- 1243 //-------------------- Decode a frame: -----------------------
1244
1245 vdecode_time=video_time_usage;
1220 1246
1221 if(file_format==DEMUXER_TYPE_MPEG_ES || file_format==DEMUXER_TYPE_MPEG_PS){ 1247 if(file_format==DEMUXER_TYPE_MPEG_ES || file_format==DEMUXER_TYPE_MPEG_PS){
1222 int in_frame=0; 1248 int in_frame=0;
1223 float newfps; 1249 float newfps;
1224 videobuf_len=0; 1250 videobuf_len=0;
1284 if(in_size<0){ eof=5;break;} 1310 if(in_size<0){ eof=5;break;}
1285 if(in_size>max_framesize) max_framesize=in_size; 1311 if(in_size>max_framesize) max_framesize=in_size;
1286 blit_frame=decode_video(video_out,sh_video,start,in_size,drop_frame); 1312 blit_frame=decode_video(video_out,sh_video,start,in_size,drop_frame);
1287 } 1313 }
1288 1314
1315 vdecode_time=video_time_usage-vdecode_time;
1316
1289 //------------------------ frame decoded. -------------------- 1317 //------------------------ frame decoded. --------------------
1290 1318
1291 // Increase video timers: 1319 // Increase video timers:
1292 sh_video->num_frames+=frame_time; 1320 sh_video->num_frames+=frame_time;
1293 frame_time*=sh_video->frametime; 1321 frame_time*=sh_video->frametime;
1349 time_frame=0; 1377 time_frame=0;
1350 1378
1351 } 1379 }
1352 1380
1353 // if(verbose>1)printf("sleep: %5.3f a:%6.3f v:%6.3f \n",time_frame,sh_audio->timer,sh_video->timer); 1381 // if(verbose>1)printf("sleep: %5.3f a:%6.3f v:%6.3f \n",time_frame,sh_audio->timer,sh_video->timer);
1382
1383 aq_sleep_time+=time_frame;
1354 1384
1355 while(time_frame>0.005){ 1385 while(time_frame>0.005){
1356 if(time_frame<=0.020) 1386 if(time_frame<=0.020)
1357 usec_sleep(10000); // sleeps 1 clock tick (10ms)! 1387 // usec_sleep(10000); // sleeps 1 clock tick (10ms)!
1388 usec_sleep(0); // sleeps 1 clock tick (10ms)!
1358 else 1389 else
1359 usec_sleep(1000000*(time_frame-0.002)); 1390 usec_sleep(1000000*(time_frame-0.002));
1360 time_frame-=GetRelativeTime(); 1391 time_frame-=GetRelativeTime();
1361 } 1392 }
1362 1393
1382 // printf("A:%6.1f V:%6.1f A-V:%7.3f frame=%5.2f \r",d_audio->pts,d_video->pts,d_audio->pts-d_video->pts,sh_audio->timer); 1413 // printf("A:%6.1f V:%6.1f A-V:%7.3f frame=%5.2f \r",d_audio->pts,d_video->pts,d_audio->pts-d_video->pts,sh_audio->timer);
1383 // fflush(stdout); 1414 // fflush(stdout);
1384 1415
1385 #if 1 1416 #if 1
1386 /*================ A-V TIMESTAMP CORRECTION: =========================*/ 1417 /*================ A-V TIMESTAMP CORRECTION: =========================*/
1418 frame_correction=0;
1387 if(sh_audio){ 1419 if(sh_audio){
1388 float a_pts=0; 1420 float a_pts=0;
1389 float v_pts=0; 1421 float v_pts=0;
1390 1422
1391 // unplayed bytes in our and soundcard/dma buffer: 1423 // unplayed bytes in our and soundcard/dma buffer:
1418 } 1450 }
1419 1451
1420 if(verbose>1)printf("### A:%8.3f (%8.3f) V:%8.3f A-V:%7.4f \n",a_pts,a_pts-audio_delay-delay,v_pts,(a_pts-delay-audio_delay)-v_pts); 1452 if(verbose>1)printf("### A:%8.3f (%8.3f) V:%8.3f A-V:%7.4f \n",a_pts,a_pts-audio_delay-delay,v_pts,(a_pts-delay-audio_delay)-v_pts);
1421 1453
1422 if(delay_corrected){ 1454 if(delay_corrected){
1423 float x=(a_pts-delay-audio_delay)-v_pts; 1455 float x=frame_correction=(a_pts-delay-audio_delay)-v_pts;
1424 // printf("A:%6.1f V:%6.1f A-V:%7.3f",a_pts-audio_delay-delay,v_pts,x); 1456 // printf("A:%6.1f V:%6.1f A-V:%7.3f",a_pts-audio_delay-delay,v_pts,x);
1425 printf("A:%6.1f (%6.1f) V:%6.1f A-V:%7.3f",a_pts,a_pts-audio_delay-delay,v_pts,x); 1457 printf("A:%6.1f (%6.1f) V:%6.1f A-V:%7.3f",a_pts,a_pts-audio_delay-delay,v_pts,x);
1426 x*=0.1f; 1458 x*=0.1f;
1427 if(x<-max_pts_correction) x=-max_pts_correction; else 1459 if(x<-max_pts_correction) x=-max_pts_correction; else
1428 if(x> max_pts_correction) x= max_pts_correction; 1460 if(x> max_pts_correction) x= max_pts_correction;
1429 if(default_max_pts_correction>=0) 1461 if(default_max_pts_correction>=0)
1430 max_pts_correction=default_max_pts_correction; 1462 max_pts_correction=default_max_pts_correction;
1431 else 1463 else
1432 max_pts_correction=sh_video->frametime*0.10; // +-10% of time 1464 max_pts_correction=sh_video->frametime*0.10; // +-10% of time
1433 sh_audio->timer+=x; c_total+=x; 1465 sh_audio->timer+=x; c_total+=x;
1434 printf(" ct:%7.3f %3d %2d%% %2d%% %4.1f%% %d\r",c_total, 1466 printf(" ct:%7.3f %3d %2d%% %2d%% %4.1f%% %d %d\r",c_total,
1435 (int)sh_video->num_frames, 1467 (int)sh_video->num_frames,
1436 (sh_video->timer>0.5)?(int)(100.0*video_time_usage/(double)sh_video->timer):0, 1468 (sh_video->timer>0.5)?(int)(100.0*video_time_usage/(double)sh_video->timer):0,
1437 (sh_video->timer>0.5)?(int)(100.0*vout_time_usage/(double)sh_video->timer):0, 1469 (sh_video->timer>0.5)?(int)(100.0*vout_time_usage/(double)sh_video->timer):0,
1438 (sh_video->timer>0.5)?(100.0*audio_time_usage/(double)sh_video->timer):0 1470 (sh_video->timer>0.5)?(100.0*audio_time_usage/(double)sh_video->timer):0
1439 ,drop_frame_cnt 1471 ,drop_frame_cnt
1472 ,output_quality
1440 ); 1473 );
1441 fflush(stdout); 1474 fflush(stdout);
1442 } 1475 }
1443 1476
1444 } else { 1477 } else {
1453 1486
1454 fflush(stdout); 1487 fflush(stdout);
1455 1488
1456 } 1489 }
1457 #endif 1490 #endif
1491
1492 /*Output quality adjustments:*/
1493 if(auto_quality>0){
1494 #if 0
1495 /*If we took a long time decoding this frame, downgrade the quality.*/
1496 if(output_quality>0&&
1497 (video_time_usage-cvideo_base_vtime)*sh_video->timer>=
1498 (0.95*sh_video->timer-(vout_time_usage+audio_time_usage))*
1499 (sh_video->timer-cvideo_base_vframe-frame_correction)){
1500 output_quality>>=1;
1501 printf("Downgrading quality to %i.\n",output_quality);
1502 set_video_quality(sh_video,output_quality);
1503 } else
1504 /*If we had plenty of extra time, upgrade the quality.*/
1505 if(output_quality<auto_quality&&
1506 vdecode_time<0.5*frame_time&&
1507 (video_time_usage-cvideo_base_vtime)*sh_video->timer<
1508 (0.67*sh_video->timer-(vout_time_usage+audio_time_usage))*
1509 (sh_video->timer-cvideo_base_vframe-frame_correction)){
1510 output_quality++;
1511 printf("Upgrading quality to %i.\n",output_quality);
1512 set_video_quality(sh_video,output_quality);
1513 }
1514 #else
1515 float total=0.000001f * (GetTimer()-aq_total_time);
1516 // if(output_quality<auto_quality && aq_sleep_time>0.05f*total)
1517 if(output_quality<auto_quality && aq_sleep_time>0)
1518 ++output_quality;
1519 else
1520 // if(output_quality>0 && aq_sleep_time<-0.05f*total)
1521 if(output_quality>1 && aq_sleep_time<0)
1522 --output_quality;
1523 else
1524 if(output_quality>0 && aq_sleep_time<-0.050f) // 50ms
1525 output_quality=0;
1526 // printf("total: %8.6f sleep: %8.6f q: %d\n",(0.000001f*aq_total_time),aq_sleep_time,output_quality);
1527 set_video_quality(sh_video,output_quality);
1528 #endif
1529 }
1458 1530
1459 #ifdef USE_OSD 1531 #ifdef USE_OSD
1460 if(osd_visible){ 1532 if(osd_visible){
1461 --osd_visible; 1533 --osd_visible;
1462 if(!osd_visible) vo_osd_progbar_type=-1; // disable 1534 if(!osd_visible) vo_osd_progbar_type=-1; // disable