Mercurial > mplayer.hg
annotate mplayer.c @ 388:ccb5ebfaaeac
-bpp changes
author | arpi_esp |
---|---|
date | Fri, 13 Apr 2001 13:47:36 +0000 |
parents | 3360c39a6bb7 |
children | 15fb291915fa |
rev | line source |
---|---|
1 | 1 // AVI & MPEG Player v0.11 (C) 2000-2001. by A'rpi/ESP-team |
2 | |
112 | 3 // Enable ALSA emulation (using 32kB audio buffer) - timer testing only |
1 | 4 //#define SIMULATE_ALSA |
5 | |
112 | 6 // Define, if you want to run libmpeg2 in a new process (using codec-ctrl) |
7 //#define HAVE_CODECCTRL | |
8 | |
1 | 9 #ifdef USE_XMMP_AUDIO |
10 #define OUTBURST 4096 | |
11 #else | |
12 //#define OUTBURST 1024 | |
13 #define OUTBURST 512 | |
14 #endif | |
15 | |
16 #include <stdio.h> | |
17 #include <stdlib.h> | |
109 | 18 #include <string.h> |
1 | 19 |
20 #include <signal.h> | |
21 | |
22 #include <sys/ioctl.h> | |
23 #include <unistd.h> | |
24 #include <sys/mman.h> | |
25 | |
26 #include <sys/types.h> | |
27 #include <sys/wait.h> | |
28 #include <sys/time.h> | |
29 #include <sys/stat.h> | |
30 #include <fcntl.h> | |
31 #include <sys/soundcard.h> | |
32 #include <linux/cdrom.h> | |
33 | |
34 #include "version.h" | |
35 #include "config.h" | |
36 | |
147 | 37 #include "cfgparser.h" |
151 | 38 #include "cfg-mplayer-def.h" |
147 | 39 |
258 | 40 #include "subreader.h" |
41 | |
36 | 42 #include "libvo/video_out.h" |
220 | 43 #include "libvo/sub.h" |
36 | 44 |
1 | 45 // CODECS: |
46 #include "mp3lib/mp3.h" | |
47 #include "libac3/ac3.h" | |
48 #include "libmpeg2/mpeg2.h" | |
49 #include "libmpeg2/mpeg2_internal.h" | |
50 | |
51 #include "loader.h" | |
52 #include "wine/avifmt.h" | |
53 | |
303 | 54 #include "codec-cfg.h" |
175 | 55 |
56 #ifdef USE_DIRECTSHOW | |
57 #include "DirectShow/DS_VideoDec.h" | |
190 | 58 #include "DirectShow/DS_AudioDec.h" |
175 | 59 #endif |
60 | |
1 | 61 #include "opendivx/decore.h" |
62 | |
63 | |
64 #ifdef USE_XMMP_AUDIO | |
65 #include "libxmm/xmmp.h" | |
66 #include "libxmm/libxmm.h" | |
67 XMM xmm; | |
68 XMM_PluginSound *pSound=NULL; | |
69 #endif | |
70 | |
33 | 71 extern int vo_screenwidth; |
1 | 72 |
73 extern char* win32_codec_name; // must be set before calling DrvOpen() !!! | |
74 | |
75 extern int errno; | |
76 | |
77 #include "linux/getch2.h" | |
78 #include "linux/keycodes.h" | |
79 #include "linux/timer.h" | |
80 #include "linux/shmem.h" | |
81 | |
82 #ifdef HAVE_LIRC | |
83 #include "lirc_mp.h" | |
84 #endif | |
85 | |
86 #include "help_mp.h" | |
87 | |
88 #define DEBUG if(0) | |
362 | 89 int verbose=0; |
1 | 90 |
258 | 91 //**************************************************************************// |
92 // .SUB | |
93 //**************************************************************************// | |
94 | |
95 static current_sub=0; | |
96 | |
97 static subtitle* subtitles=NULL; | |
98 | |
99 void find_sub(unsigned long key){ | |
100 int i,j; | |
101 if(current_sub<0 || current_sub>=sub_num) current_sub=0; | |
102 vo_sub=&subtitles[current_sub]; | |
103 if(key>=vo_sub->start && key<=vo_sub->end) return; // OK! | |
104 // use logarithmic search: | |
105 i=0;j=sub_num-1; | |
106 // printf("Searching %d in %d..%d\n",key,subtitles[i].start,subtitles[j].end); | |
107 while(j>=i){ | |
108 current_sub=(i+j+1)/2; | |
109 if(key<subtitles[current_sub].start) | |
110 j=current_sub-1; | |
111 else | |
112 if(key>subtitles[current_sub].end) | |
113 i=current_sub+1; | |
114 else break; // found! | |
115 } | |
116 vo_sub=&subtitles[current_sub]; | |
117 if(key>=vo_sub->start && key<=vo_sub->end) return; // OK! | |
118 vo_sub=NULL; // no sub here | |
119 } | |
120 | |
121 //**************************************************************************// | |
122 // Config file | |
123 //**************************************************************************// | |
124 | |
153 | 125 static int cfg_inc_verbose(struct config *conf){ |
126 ++verbose; | |
127 return 0; | |
128 } | |
129 | |
162 | 130 static int cfg_include(struct config *conf, char *filename){ |
131 return parse_config_file(conf, filename); | |
132 } | |
133 | |
178 | 134 char *get_path(char *filename){ |
135 char *homedir; | |
136 char *buff; | |
137 static char *config_dir = "/.mplayer"; | |
138 int len; | |
139 | |
140 if ((homedir = getenv("HOME")) == NULL) | |
141 return NULL; | |
142 len = strlen(homedir) + strlen(config_dir) + 1; | |
143 if (filename == NULL) { | |
144 if ((buff = (char *) malloc(len)) == NULL) | |
145 return NULL; | |
146 sprintf(buff, "%s%s", homedir, config_dir); | |
147 } else { | |
148 len += strlen(filename) + 1; | |
149 if ((buff = (char *) malloc(len)) == NULL) | |
150 return NULL; | |
151 sprintf(buff, "%s%s/%s", homedir, config_dir, filename); | |
152 } | |
153 return buff; | |
154 } | |
155 | |
1 | 156 static int max_framesize=0; |
157 | |
158 static int dbg_es_sent=0; | |
159 static int dbg_es_rcvd=0; | |
160 | |
161 //static int show_packets=0; | |
162 | |
163 //**************************************************************************// | |
164 | |
165 typedef struct { | |
166 // file: | |
167 MainAVIHeader avih; | |
168 unsigned int movi_start; | |
169 unsigned int movi_end; | |
170 // index: | |
171 AVIINDEXENTRY* idx; | |
172 int idx_size; | |
173 int idx_pos; | |
174 int idx_pos_a; | |
175 int idx_pos_v; | |
176 int idx_offset; // ennyit kell hozzaadni az index offset ertekekhez | |
177 // video: | |
291 | 178 unsigned int bitrate; |
1 | 179 } avi_header_t; |
180 | |
181 avi_header_t avi_header; | |
182 | |
183 #include "aviprint.c" | |
184 | |
185 extern picture_t *picture; | |
186 | |
187 char* encode_name=NULL; | |
188 char* encode_index_name=NULL; | |
189 int encode_bitrate=0; | |
190 | |
191 //**************************************************************************// | |
111 | 192 // Input media streaming & demultiplexer: |
193 //**************************************************************************// | |
1 | 194 |
195 #include "stream.c" | |
196 #include "demuxer.c" | |
291 | 197 |
198 #include "stheader.h" | |
199 | |
1 | 200 #include "demux_avi.c" |
201 #include "demux_mpg.c" | |
202 | |
203 demuxer_t *demuxer=NULL; | |
204 demux_stream_t *d_audio=NULL; | |
205 demux_stream_t *d_video=NULL; | |
206 | |
291 | 207 sh_audio_t sh_audio_i; // FIXME later! |
208 sh_video_t sh_video_i; | |
209 sh_audio_t *sh_audio=&sh_audio_i; | |
210 sh_video_t *sh_video=&sh_video_i; | |
211 | |
1 | 212 // MPEG video stream parser: |
213 #include "parse_es.c" | |
214 | |
112 | 215 static const int frameratecode2framerate[16] = { |
216 0, 24000*10000/1001, 24*10000,25*10000, 30000*10000/1001, 30*10000,50*10000,60000*10000/1001, | |
217 60*10000, 0,0,0,0,0,0,0 | |
218 }; | |
219 | |
111 | 220 //**************************************************************************// |
221 // Audio codecs: | |
222 //**************************************************************************// | |
223 | |
296 | 224 // MP3 decoder buffer callback: |
1 | 225 int mplayer_audio_read(char *buf,int size){ |
226 int len; | |
291 | 227 len=demux_read_data(sh_audio->ds,buf,size); |
1 | 228 return len; |
229 } | |
230 | |
296 | 231 // AC3 decoder buffer callback: |
1 | 232 static void ac3_fill_buffer(uint8_t **start,uint8_t **end){ |
291 | 233 int len=ds_get_packet(sh_audio->ds,(char**)start); |
1 | 234 //printf("<ac3:%d>\n",len); |
235 if(len<0) | |
236 *start = *end = NULL; | |
237 else | |
238 *end = *start + len; | |
239 } | |
240 | |
241 #include "alaw.c" | |
242 #include "xa/xa_gsm.h" | |
243 | |
291 | 244 #include "dec_audio.c" |
245 | |
111 | 246 //**************************************************************************// |
247 // The OpenDivX stuff: | |
248 //**************************************************************************// | |
249 | |
1 | 250 unsigned char *opendivx_src[3]; |
251 int opendivx_stride[3]; | |
252 | |
111 | 253 // callback, the opendivx decoder calls this for each frame: |
80 | 254 void convert_linux(unsigned char *puc_y, int stride_y, |
1 | 255 unsigned char *puc_u, unsigned char *puc_v, int stride_uv, |
256 unsigned char *bmp, int width_y, int height_y){ | |
257 | |
258 // printf("convert_yuv called %dx%d stride: %d,%d\n",width_y,height_y,stride_y,stride_uv); | |
259 | |
260 opendivx_src[0]=puc_y; | |
261 opendivx_src[1]=puc_u; | |
262 opendivx_src[2]=puc_v; | |
263 | |
264 opendivx_stride[0]=stride_y; | |
265 opendivx_stride[1]=stride_uv; | |
266 opendivx_stride[2]=stride_uv; | |
267 } | |
268 | |
269 //**************************************************************************// | |
270 | |
271 #ifdef SIMULATE_ALSA | |
272 // Simulate ALSA buffering on OSS device :) (for testing...) | |
273 | |
274 #define fake_ALSA_size 32768 | |
275 char fake_ALSA_buffer[fake_ALSA_size]; | |
276 int fake_ALSA_len=0; | |
277 | |
278 void fake_ALSA_write(int audio_fd,char* a_buffer,int len){ | |
279 while(len>0){ | |
280 int x=fake_ALSA_size-fake_ALSA_len; | |
281 if(x>0){ | |
282 if(x>len) x=len; | |
283 memcpy(&fake_ALSA_buffer[fake_ALSA_len],a_buffer,x); | |
284 fake_ALSA_len+=x;len-=x; | |
285 } | |
286 if(fake_ALSA_len>=fake_ALSA_size){ | |
287 write(audio_fd,fake_ALSA_buffer,fake_ALSA_len); | |
288 fake_ALSA_len=0; | |
289 } | |
290 } | |
291 } | |
292 #endif | |
293 //**************************************************************************// | |
294 | |
295 // AVI file header reader/parser/writer: | |
296 #include "aviheader.c" | |
297 #include "aviwrite.c" | |
298 | |
299 // ASF headers: | |
300 #include "asfheader.c" | |
301 #include "demux_asf.c" | |
302 | |
303 // DLL codecs init routines | |
304 #include "dll_init.c" | |
305 | |
112 | 306 // Common FIFO functions, and keyboard/event FIFO code |
307 #include "fifo.c" | |
308 | |
1 | 309 // MPEG video codec process controller: |
112 | 310 #ifdef HAVE_CODECCTRL |
1 | 311 #include "codecctrl.c" |
112 | 312 #endif |
1 | 313 |
314 //**************************************************************************// | |
315 | |
316 static vo_functions_t *video_out=NULL; | |
317 | |
318 static int play_in_bg=0; | |
319 | |
320 void exit_player(char* how){ | |
321 if(how) printf("\nExiting... (%s)\n",how); | |
340 | 322 if(verbose) printf("max framesize was %d bytes\n",max_framesize); |
1 | 323 // restore terminal: |
324 getch2_disable(); | |
112 | 325 #ifdef HAVE_CODECCTRL |
1 | 326 if(child_pid){ |
327 // MPEG | |
328 // printf("\n\n"); | |
329 //send_cmd(data_fifo,0);usleep(50000); // EOF request | |
330 DEBUG_SIG { printf("Sending TERM signal to CTRL...\n");DEBUG_SIGNALS_SLEEP} | |
331 kill(child_pid,SIGTERM); | |
332 usleep(10000); // kill & wait 10ms | |
333 DEBUG_SIG { printf("Closing PIPEs...\n");DEBUG_SIGNALS_SLEEP} | |
334 close(control_fifo); | |
335 close(data_fifo); | |
336 DEBUG_SIG { printf("Freeing shmem...\n");DEBUG_SIGNALS_SLEEP} | |
337 if(videobuffer) shmem_free(videobuffer); | |
338 DEBUG_SIG { printf("Exiting...\n");DEBUG_SIGNALS_SLEEP} | |
112 | 339 } else |
340 #endif | |
341 { | |
1 | 342 // AVI |
343 video_out->uninit(); | |
344 } | |
345 #ifdef USE_XMMP_AUDIO | |
346 if(verbose) printf("XMM: closing audio driver...\n"); | |
347 if(pSound){ | |
348 pSound->Exit( pSound ); | |
349 xmm_Exit( &xmm ); | |
350 } | |
351 #endif | |
352 if(encode_name) avi_fixate(); | |
353 #ifdef HAVE_LIRC | |
354 lirc_mp_cleanup(); | |
355 #endif | |
356 //if(play_in_bg) system("xsetroot -solid \\#000000"); | |
357 exit(1); | |
358 } | |
359 | |
360 static char* current_module=NULL; // for debugging | |
361 | |
362 void exit_sighandler(int x){ | |
363 static int sig_count=0; | |
364 ++sig_count; | |
365 if(sig_count==2) exit(1); | |
366 if(sig_count>2){ | |
367 // can't stop :( | |
368 kill(getpid(),SIGKILL); | |
369 } | |
370 printf("\nMPlayer interrupted by signal %d in module: %s \n",x, | |
371 current_module?current_module:"unknown" | |
372 ); | |
373 exit_player(NULL); | |
374 } | |
375 | |
376 int divx_quality=0; | |
377 | |
147 | 378 int main(int argc,char* argv[], char *envp[]){ |
1 | 379 char* filename=NULL; //"MI2-Trailer.avi"; |
380 int i; | |
381 int seek_to_sec=0; | |
382 int seek_to_byte=0; | |
383 int f; // filedes | |
384 int stream_type; | |
385 stream_t* stream=NULL; | |
386 int file_format=DEMUXER_TYPE_UNKNOWN; | |
190 | 387 int has_audio=1; // audio 0=no 1=mpeg 2=pcm 3=ac3 4=ACM 5=alaw 6=msgsm 7=DShow |
303 | 388 int has_video=1; // video 0=no 1=mpeg 2=win32/VfW 3=OpenDivX 4=w32/DShow |
1 | 389 // |
390 int audio_format=0; // override | |
391 #ifdef ALSA_TIMER | |
392 int alsa=1; | |
393 #else | |
394 int alsa=0; | |
395 #endif | |
396 int audio_buffer_size=-1; | |
397 int audio_id=-1; | |
398 int video_id=-1; | |
399 float default_max_pts_correction=0.01f; | |
400 int delay_corrected=1; | |
401 float force_fps=0; | |
402 float default_fps=25; | |
403 float audio_delay=0; | |
404 int vcd_track=0; | |
405 #ifdef VCD_CACHE | |
406 int vcd_cache_size=128; | |
407 #endif | |
408 int no_index=0; | |
409 #ifdef AVI_SYNC_BPS | |
410 int pts_from_bps=1; | |
411 #else | |
412 int pts_from_bps=0; | |
413 #endif | |
414 char* title="MPlayer"; | |
415 // screen info: | |
416 char* video_driver=NULL; //"mga"; // default | |
417 int fullscreen=0; | |
208
ae0f909ccc7c
Adds code to deal with vidmode selection. -- mgraffam
mgraffam
parents:
190
diff
changeset
|
418 int vidmode=0; |
337 | 419 int softzoom=0; |
420 int screen_size_x=0;//SCREEN_SIZE_X; | |
421 int screen_size_y=0;//SCREEN_SIZE_Y; | |
1 | 422 int screen_size_xy=0; |
423 // movie info: | |
424 int movie_size_x=0; | |
425 int movie_size_y=0; | |
426 int out_fmt=0; | |
427 char *dsp="/dev/dsp"; | |
428 int force_ni=0; | |
178 | 429 char *conffile; |
151 | 430 int conffile_fd; |
212 | 431 char *font_name=NULL; |
215 | 432 float font_factor=0.75; |
258 | 433 char *sub_name=NULL; |
434 float sub_delay=0; | |
435 float sub_fps=0; | |
388 | 436 //int user_bpp=0; |
147 | 437 #include "cfg-mplayer.h" |
1 | 438 |
439 printf("%s",banner_text); | |
440 | |
147 | 441 if (parse_config_file(conf, "/etc/mplayer.conf") < 0) |
442 exit(1); | |
178 | 443 if ((conffile = get_path("")) == NULL) { |
147 | 444 printf("Can't find HOME dir\n"); |
445 } else { | |
178 | 446 mkdir(conffile, 0777); |
447 free(conffile); | |
448 if ((conffile = get_path("config")) == NULL) { | |
449 printf("get_path(\"config\") sziiiivas\n"); | |
450 } else { | |
451 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) { | |
452 printf("Creating config file: %s\n", conffile); | |
453 write(conffile_fd, default_config, strlen(default_config)); | |
454 close(conffile_fd); | |
455 } | |
456 if (parse_config_file(conf, conffile) < 0) | |
457 exit(1); | |
458 free(conffile); | |
151 | 459 } |
1 | 460 } |
147 | 461 if (parse_command_line(conf, argc, argv, envp, &filename) < 0) |
462 exit(1); | |
1 | 463 |
464 // Many users forget to include command line in bugreports... | |
465 if(verbose){ | |
466 printf("CommandLine:"); | |
467 for(i=1;i<argc;i++)printf(" '%s'",argv[i]); | |
468 printf("\n"); | |
469 } | |
470 | |
471 if(video_driver && strcmp(video_driver,"help")==0){ | |
472 printf("Available video output drivers:\n"); | |
473 i=0; | |
474 while (video_out_drivers[i]) { | |
475 const vo_info_t *info = video_out_drivers[i++]->get_info (); | |
476 printf("\t%s\t%s\n", info->short_name, info->name); | |
477 } | |
478 printf("\n"); | |
479 exit(0); | |
480 } | |
481 | |
212 | 482 // check font |
483 if(font_name){ | |
337 | 484 vo_font=read_font_desc(font_name,font_factor,verbose>1); |
212 | 485 if(!vo_font) printf("Can't load font: %s\n",font_name); |
220 | 486 } else { |
487 // try default: | |
337 | 488 vo_font=read_font_desc(get_path("font/font.desc"),font_factor,verbose>1); |
212 | 489 } |
490 | |
258 | 491 // check .sub |
492 if(sub_name){ | |
493 subtitles=sub_read_file(sub_name); | |
494 if(!subtitles) printf("Can't load subtitles: %s\n",font_name); | |
495 } else { | |
496 // try default: | |
497 subtitles=sub_read_file(get_path("default.sub")); | |
498 } | |
499 | |
500 | |
1 | 501 // check video_out driver name: |
502 if(!video_driver) | |
503 video_out=video_out_drivers[0]; | |
504 else | |
505 for (i=0; video_out_drivers[i] != NULL; i++){ | |
506 const vo_info_t *info = video_out_drivers[i]->get_info (); | |
507 if(strcmp(info->short_name,video_driver) == 0){ | |
508 video_out = video_out_drivers[i];break; | |
509 } | |
510 } | |
511 if(!video_out){ | |
512 printf("Invalid video output driver name: %s\n",video_driver); | |
513 return 0; | |
514 } | |
515 | |
516 if(!filename){ | |
517 if(vcd_track) filename="/dev/cdrom"; | |
153 | 518 else { |
519 printf("%s",help_text); exit(0); | |
520 } | |
1 | 521 } |
522 | |
303 | 523 // check codec.conf |
524 if(!parse_codec_cfg(get_path("codecs.conf"))){ | |
304
662f876d77e9
missing codecs.conf is no more a WARNING, it's FATAL
arpi_esp
parents:
303
diff
changeset
|
525 printf("(copy/link DOCS/codecs.conf to ~/.mplayer/codecs.conf)\n"); |
662f876d77e9
missing codecs.conf is no more a WARNING, it's FATAL
arpi_esp
parents:
303
diff
changeset
|
526 exit(1); |
303 | 527 } |
528 | |
1 | 529 |
530 if(vcd_track){ | |
531 //============ Open VideoCD track ============== | |
532 f=open(filename,O_RDONLY); | |
533 if(f<0){ printf("CD-ROM Device '%s' not found!\n",filename);return 1; } | |
534 vcd_read_toc(f); | |
535 if(!vcd_seek_to_track(f,vcd_track)){ printf("Error selecting VCD track!\n");return 1;} | |
536 seek_to_byte+=VCD_SECTOR_DATA*vcd_get_msf(); | |
537 if(verbose) printf("VCD start byte position: 0x%X\n",seek_to_byte); | |
538 stream_type=STREAMTYPE_VCD; | |
539 #ifdef VCD_CACHE | |
540 vcd_cache_init(vcd_cache_size); | |
541 #endif | |
542 } else { | |
543 //============ Open plain FILE ============ | |
544 f=open(filename,O_RDONLY); | |
545 if(f<0){ printf("File not found: '%s'\n",filename);return 1; } | |
546 stream_type=STREAMTYPE_FILE; | |
547 } | |
548 | |
549 stream=new_stream(f,stream_type); | |
550 | |
551 //============ Open & Sync stream and detect file format =============== | |
552 | |
553 if(has_audio==0) audio_id=-2; // do NOT read audio packets... | |
554 | |
555 //=============== Try to open as AVI file: ================= | |
556 stream_reset(stream); | |
557 demuxer=new_demuxer(stream,DEMUXER_TYPE_AVI,audio_id,video_id); | |
558 stream_seek(demuxer->stream,seek_to_byte); | |
559 { //---- RIFF header: | |
560 int id=stream_read_dword_le(demuxer->stream); // "RIFF" | |
561 if(id==mmioFOURCC('R','I','F','F')){ | |
562 stream_read_dword_le(demuxer->stream); //filesize | |
563 id=stream_read_dword_le(demuxer->stream); // "AVI " | |
564 if(id==formtypeAVI){ | |
565 printf("Detected AVI file format!\n"); | |
566 file_format=DEMUXER_TYPE_AVI; | |
567 } | |
568 } | |
569 } | |
570 //=============== Try to open as ASF file: ================= | |
571 if(file_format==DEMUXER_TYPE_UNKNOWN){ | |
572 stream_reset(stream); | |
573 demuxer=new_demuxer(stream,DEMUXER_TYPE_ASF,audio_id,video_id); | |
574 stream_seek(demuxer->stream,seek_to_byte); | |
575 if(asf_check_header()){ | |
576 printf("Detected ASF file format!\n"); | |
577 file_format=DEMUXER_TYPE_ASF; | |
578 } | |
579 } | |
580 //=============== Try to open as MPEG-PS file: ================= | |
581 if(file_format==DEMUXER_TYPE_UNKNOWN){ | |
582 stream_reset(stream); | |
583 demuxer=new_demuxer(stream,DEMUXER_TYPE_MPEG_PS,audio_id,video_id); | |
584 stream_seek(demuxer->stream,seek_to_byte); | |
585 if(audio_format) demuxer->audio->type=audio_format; // override audio format | |
586 if(ds_fill_buffer(demuxer->video)){ | |
587 printf("Detected MPEG-PS file format!\n"); | |
588 file_format=DEMUXER_TYPE_MPEG_PS; | |
589 } else { | |
590 // some hack to get meaningfull error messages to our unhappy users: | |
591 // if(num_elementary_packets100>16 && | |
592 // abs(num_elementary_packets101-num_elementary_packets100)<8){ | |
593 if(num_elementary_packets100>=2 && num_elementary_packets101>=2 && | |
594 abs(num_elementary_packets101-num_elementary_packets100)<8){ | |
595 file_format=DEMUXER_TYPE_MPEG_ES; // <-- hack is here :) | |
596 } else { | |
597 if(demuxer->synced==2) | |
598 printf("Missing MPEG video stream!? contact the author, it may be a bug :(\n"); | |
599 else | |
600 printf("Not MPEG System Stream format... (maybe Transport Stream?)\n"); | |
601 } | |
602 } | |
603 } | |
604 //=============== Try to open as MPEG-ES file: ================= | |
605 if(file_format==DEMUXER_TYPE_MPEG_ES){ // little hack, see above! | |
606 stream_reset(stream); | |
607 demuxer=new_demuxer(stream,DEMUXER_TYPE_MPEG_ES,audio_id,video_id); | |
608 stream_seek(demuxer->stream,seek_to_byte); | |
609 if(!ds_fill_buffer(demuxer->video)){ | |
610 printf("Invalid MPEG-ES stream??? contact the author, it may be a bug :(\n"); | |
611 file_format=DEMUXER_TYPE_UNKNOWN; | |
612 } else { | |
613 printf("Detected MPEG-ES file format!\n"); | |
614 } | |
615 } | |
616 //=============== Unknown, exiting... =========================== | |
617 if(file_format==DEMUXER_TYPE_UNKNOWN){ | |
618 printf("============= Sorry, this file format not recognized/supported ===============\n"); | |
619 printf("=== If this file is an AVI, ASF or MPEG stream, please contact the author! ===\n"); | |
620 exit(1); | |
621 } | |
622 //====== File format recognized, set up these for compatibility: ========= | |
623 d_audio=demuxer->audio; | |
624 d_video=demuxer->video; | |
291 | 625 d_audio->sh=sh_audio; sh_audio->ds=d_audio; |
626 d_video->sh=sh_video; sh_video->ds=d_video; | |
1 | 627 |
628 switch(file_format){ | |
629 case DEMUXER_TYPE_AVI: { | |
630 //---- AVI header: | |
631 read_avi_header(no_index); | |
632 stream_reset(demuxer->stream); | |
633 stream_seek(demuxer->stream,avi_header.movi_start); | |
634 avi_header.idx_pos=0; | |
635 avi_header.idx_pos_a=0; | |
636 avi_header.idx_pos_v=0; | |
637 if(avi_header.idx_size>0){ | |
638 // decide index format: | |
639 if(avi_header.idx[0].dwChunkOffset<avi_header.movi_start) | |
640 avi_header.idx_offset=avi_header.movi_start-4; | |
641 else | |
642 avi_header.idx_offset=0; | |
643 if(verbose) printf("AVI index offset: %d\n",avi_header.idx_offset); | |
644 } | |
645 demuxer->endpos=avi_header.movi_end; | |
646 | |
647 if(avi_header.idx_size>0){ | |
648 // check that file is non-interleaved: | |
649 int i; | |
650 int a_pos=-1; | |
651 int v_pos=-1; | |
652 for(i=0;i<avi_header.idx_size;i++){ | |
653 AVIINDEXENTRY* idx=&avi_header.idx[i]; | |
654 demux_stream_t* ds=demux_avi_select_stream(demuxer,idx->ckid); | |
655 int pos=idx->dwChunkOffset+avi_header.idx_offset; | |
656 if(a_pos==-1 && ds==demuxer->audio){ | |
657 a_pos=pos; | |
658 if(v_pos!=-1) break; | |
659 } | |
660 if(v_pos==-1 && ds==demuxer->video){ | |
661 v_pos=pos; | |
662 if(a_pos!=-1) break; | |
663 } | |
664 } | |
665 if(v_pos==-1){ | |
666 printf("AVI_NI: missing video stream!? contact the author, it may be a bug :(\n"); | |
667 exit(1); | |
668 } | |
669 if(a_pos==-1){ | |
670 printf("AVI_NI: No audio stream found -> nosound\n"); | |
671 has_audio=0; | |
672 } else { | |
673 if(force_ni || abs(a_pos-v_pos)>0x100000){ // distance > 1MB | |
674 printf("Detected NON-INTERLEAVED AVI file-format!\n"); | |
675 demuxer->type=DEMUXER_TYPE_AVI_NI; // HACK!!!! | |
676 pts_from_bps=1; // force BPS sync! | |
677 } | |
678 } | |
679 } else { | |
680 // no index | |
681 if(force_ni){ | |
682 printf("Using NON-INTERLEAVED Broken AVI file-format!\n"); | |
683 demuxer->type=DEMUXER_TYPE_AVI_NINI; // HACK!!!! | |
684 avi_header.idx_pos_a= | |
685 avi_header.idx_pos_v=avi_header.movi_start; | |
686 pts_from_bps=1; // force BPS sync! | |
687 } | |
688 } | |
689 if(!ds_fill_buffer(d_video)){ | |
690 printf("AVI: missing video stream!? contact the author, it may be a bug :(\n"); | |
691 exit(1); | |
692 } | |
693 if(has_audio){ | |
375 | 694 if(verbose) printf("ASF: Searching for audio stream (id:%d)\n",d_audio->id); |
1 | 695 if(!ds_fill_buffer(d_audio)){ |
375 | 696 printf("ASF: No Audio stream found... ->nosound\n"); |
1 | 697 has_audio=0; |
375 | 698 } else sh_audio->format=sh_audio->wf.wFormatTag; |
1 | 699 } |
375 | 700 // calculating video bitrate: |
291 | 701 default_fps=(float)sh_video->video.dwRate/(float)sh_video->video.dwScale; |
375 | 702 avi_header.bitrate=avi_header.movi_end-avi_header.movi_start-avi_header.idx_size*8; |
703 if(sh_audio) avi_header.bitrate-=sh_audio->audio.dwLength; | |
704 if(verbose) printf("AVI video length=%d\n",avi_header.bitrate); | |
705 avi_header.bitrate=((float)avi_header.bitrate/(float)sh_video->video.dwLength)*default_fps; | |
706 printf("VIDEO: [%.4s] %dx%d %dbpp %4.2f fps %5.1f kbps (%4.1f kbyte/s)\n", | |
707 &sh_video->bih.biCompression, | |
708 sh_video->bih.biWidth, | |
709 sh_video->bih.biHeight, | |
710 sh_video->bih.biBitCount, | |
711 default_fps, | |
712 avi_header.bitrate*0.008f, | |
713 avi_header.bitrate/1024.0f ); | |
1 | 714 break; |
715 } | |
716 case DEMUXER_TYPE_ASF: { | |
717 //---- ASF header: | |
718 read_asf_header(); | |
719 stream_reset(demuxer->stream); | |
720 stream_seek(demuxer->stream,avi_header.movi_start); | |
721 avi_header.idx_pos=0; | |
722 demuxer->endpos=avi_header.movi_end; | |
723 if(!ds_fill_buffer(d_video)){ | |
724 printf("ASF: missing video stream!? contact the author, it may be a bug :(\n"); | |
725 exit(1); | |
726 } | |
727 if(has_audio){ | |
728 if(verbose) printf("ASF: Searching for audio stream (id:%d)\n",d_audio->id); | |
729 if(!ds_fill_buffer(d_audio)){ | |
730 printf("ASF: No Audio stream found... ->nosound\n"); | |
731 has_audio=0; | |
375 | 732 } else sh_audio->format=sh_audio->wf.wFormatTag; |
1 | 733 } |
375 | 734 printf("VIDEO: [%.4s] %dx%d %dbpp\n", |
735 &sh_video->bih.biCompression, | |
736 sh_video->bih.biWidth, | |
737 sh_video->bih.biHeight, | |
738 sh_video->bih.biBitCount); | |
1 | 739 break; |
740 } | |
741 case DEMUXER_TYPE_MPEG_ES: { | |
303 | 742 demuxer->audio->type=0; |
375 | 743 has_audio=0; // ES streams has no audio channel |
744 break; | |
745 } | |
746 case DEMUXER_TYPE_MPEG_PS: { | |
747 if(has_audio) | |
748 if(!ds_fill_buffer(d_audio)){ | |
749 printf("MPEG: No Audio stream found... ->nosound\n"); | |
750 has_audio=0; | |
751 } else { | |
382 | 752 if(verbose) printf("detected MPG-PS audio format: %d\n",d_audio->type); |
375 | 753 switch(d_audio->type){ |
754 case 1: sh_audio->format=0x50;break; // mpeg | |
755 case 2: sh_audio->format=0x2;break; // pcm | |
756 case 3: sh_audio->format=0x2000;break; // ac3 | |
757 default: has_audio=0; // unknown type | |
758 } | |
759 } | |
760 break; | |
761 } | |
762 } // switch(file_format) | |
763 | |
764 // Determine image properties: | |
765 switch(file_format){ | |
766 case DEMUXER_TYPE_AVI: | |
767 case DEMUXER_TYPE_ASF: { | |
768 // display info: | |
769 sh_video->format=sh_video->bih.biCompression; | |
770 movie_size_x=sh_video->bih.biWidth; | |
771 movie_size_y=abs(sh_video->bih.biHeight); | |
772 break; | |
773 } | |
774 case DEMUXER_TYPE_MPEG_ES: | |
775 case DEMUXER_TYPE_MPEG_PS: { | |
303 | 776 // Find sequence_header first: |
777 if(verbose) printf("Searching for sequence header... ");fflush(stdout); | |
778 while(1){ | |
779 int i=sync_video_packet(d_video); | |
780 if(i==0x1B3) break; // found it! | |
781 if(!i || !skip_video_packet(d_video)){ | |
782 if(verbose) printf("NONE :(\n"); | |
783 printf("MPEG: FATAL: EOF while searching for sequence header\n"); | |
784 exit(1); | |
785 } | |
786 } | |
787 if(verbose) printf("OK!\n"); | |
788 sh_video->format=1; // mpeg video | |
375 | 789 mpeg2_init(); |
790 // ========= Read & process sequence header & extension ============ | |
791 videobuffer=shmem_alloc(VIDEOBUFFER_SIZE); | |
792 if(!videobuffer){ printf("Cannot allocate shared memory\n");exit(0);} | |
793 videobuf_len=0; | |
794 if(!read_video_packet(d_video)){ printf("FATAL: Cannot read sequence header!\n");exit(1);} | |
795 if(header_process_sequence_header (picture, &videobuffer[4])) { | |
796 printf ("bad sequence header!\n"); exit(1); | |
797 } | |
798 if(sync_video_packet(d_video)==0x1B5){ // next packet is seq. ext. | |
799 videobuf_len=0; | |
800 if(!read_video_packet(d_video)){ printf("FATAL: Cannot read sequence header extension!\n");exit(1);} | |
801 if(header_process_extension (picture, &videobuffer[4])) { | |
802 printf ("bad sequence header extension!\n"); exit(1); | |
303 | 803 } |
804 } | |
375 | 805 // display info: |
806 default_fps=frameratecode2framerate[picture->frame_rate_code]*0.0001f; | |
807 movie_size_x=picture->display_picture_width; | |
808 movie_size_y=picture->display_picture_height; | |
809 // info: | |
810 if(verbose) printf("mpeg bitrate: %d (%X)\n",picture->bitrate,picture->bitrate); | |
811 printf("VIDEO: %s %dx%d (aspect %d) %4.2f fps %5.1f kbps (%4.1f kbyte/s)\n", | |
812 picture->mpeg1?"MPEG1":"MPEG2", | |
813 movie_size_x,movie_size_y, | |
814 picture->aspect_ratio_information, | |
815 default_fps, | |
816 picture->bitrate*0.5f, | |
817 picture->bitrate/16.0f ); | |
1 | 818 break; |
819 } | |
820 } // switch(file_format) | |
821 | |
822 if(verbose) printf("file successfully opened (has_audio=%d)\n",has_audio); | |
823 | |
824 fflush(stdout); | |
825 | |
303 | 826 //================== Init AUDIO (codec) ========================== |
827 if(has_audio){ | |
828 // Go through the codec.conf and find the best codec... | |
332 | 829 sh_audio->codec=find_codec(sh_audio->format,NULL,NULL,1); |
303 | 830 if(!sh_audio->codec){ |
831 printf("Can't find codec for audio format 0x%X !\n",sh_audio->format); | |
832 has_audio=0; | |
833 } else { | |
340 | 834 printf("Found audio codec: [%s] drv:%d (%s)\n",sh_audio->codec->name,sh_audio->codec->driver,sh_audio->codec->info); |
303 | 835 has_audio=sh_audio->codec->driver; |
836 } | |
1 | 837 } |
838 | |
303 | 839 if(has_audio){ |
840 if(verbose) printf("Initializing audio codec...\n"); | |
841 has_audio=init_audio(sh_audio); | |
842 if(!has_audio){ | |
843 printf("Couldn't initialize audio codec! -> nosound\n"); | |
844 } else { | |
340 | 845 printf("AUDIO: samplerate=%d channels=%d bps=%d\n",sh_audio->samplerate,sh_audio->channels,sh_audio->samplesize); |
303 | 846 } |
175 | 847 } |
848 | |
303 | 849 //================== Init VIDEO (codec & libvo) ========================== |
850 | |
851 // Go through the codec.conf and find the best codec... | |
332 | 852 sh_video->codec=find_codec(sh_video->format,(unsigned int*) &sh_video->bih.biCompression,NULL,0); |
303 | 853 if(!sh_video->codec){ |
854 printf("Can't find codec for video format 0x%X !\n",sh_video->format); | |
855 exit(1); | |
856 } | |
857 has_video=sh_video->codec->driver; | |
858 | |
340 | 859 printf("Found video codec: [%s] drv:%d (%s)\n",sh_video->codec->name,sh_video->codec->driver,sh_video->codec->info); |
303 | 860 |
388 | 861 //if(user_bpp)printf("Trying user defined depth of %dbpp\n", user_bpp); |
385
3360c39a6bb7
- added -bpp switch (only takes effect if IMGFMT_BGR) to select the desired depth
acki2
parents:
382
diff
changeset
|
862 |
303 | 863 for(i=0;i<CODECS_MAX_OUTFMT;i++){ |
864 out_fmt=sh_video->codec->outfmt[i]; | |
388 | 865 // if(user_bpp){ |
866 // if( ((out_fmt & IMGFMT_BGR_MASK) == IMGFMT_BGR) && ((out_fmt & 0xff) == user_bpp) || (out_fmt & IMGFMT_BGR_MASK) != IMGFMT_BGR){ | |
867 // if(video_out->query_format(out_fmt)) break; | |
868 // } | |
869 // }else{ | |
385
3360c39a6bb7
- added -bpp switch (only takes effect if IMGFMT_BGR) to select the desired depth
acki2
parents:
382
diff
changeset
|
870 if(video_out->query_format(out_fmt)) break; |
388 | 871 // } |
303 | 872 } |
873 if(i>=CODECS_MAX_OUTFMT){ | |
874 printf("Sorry, selected video_out device is incompatible with this codec.\n"); | |
875 exit(1); | |
876 } | |
877 sh_video->outfmtidx=i; | |
878 | |
175 | 879 switch(has_video){ |
880 case 2: { | |
1 | 881 if(!init_video_codec(out_fmt)) exit(1); |
882 if(verbose) printf("INFO: Win32 video codec init OK!\n"); | |
883 break; | |
884 } | |
175 | 885 case 4: { // Win32/DirectShow |
303 | 886 #ifndef USE_DIRECTSHOW |
887 printf("MPlayer was compiled WITHOUT directshow support!\n"); | |
888 exit(1); | |
889 #else | |
291 | 890 sh_video->our_out_buffer=NULL; |
303 | 891 if(DS_VideoDecoder_Open(sh_video->codec->dll,&sh_video->codec->guid, &sh_video->bih, 0, &sh_video->our_out_buffer)){ |
892 printf("ERROR: Couldn't open required DirectShow codec: %s\n",sh_video->codec->dll); | |
273 | 893 printf("Maybe you forget to upgrade your win32 codecs?? It's time to download the new\n"); |
894 printf("package from: ftp://thot.banki.hu/esp-team/linux/MPlayer/w32codec.zip !\n"); | |
895 printf("Or you should disable DShow support: make distclean;make -f Makefile.No-DS\n"); | |
896 exit(1); | |
897 } | |
175 | 898 |
899 if(out_fmt==IMGFMT_YUY2) | |
900 DS_VideoDecoder_SetDestFmt(16,mmioFOURCC('Y', 'U', 'Y', '2')); | |
901 // DS_VideoDecoder_SetDestFmt(16,mmioFOURCC('Y', 'V', '1', '2')); | |
902 else | |
903 DS_VideoDecoder_SetDestFmt(out_fmt&255,0); | |
904 | |
905 DS_VideoDecoder_Start(); | |
906 | |
907 printf("DivX setting result = %d\n", DS_SetAttr_DivX("Quality",divx_quality) ); | |
908 // printf("DivX setting result = %d\n", DS_SetValue_DivX("Brightness",60) ); | |
909 | |
910 if(verbose) printf("INFO: Win32/DShow video codec init OK!\n"); | |
911 break; | |
374 | 912 #endif |
175 | 913 } |
1 | 914 case 3: { // OpenDivX |
915 if(verbose) printf("OpenDivX video codec\n"); | |
916 { DEC_PARAM dec_param; | |
917 DEC_SET dec_set; | |
291 | 918 dec_param.x_dim = sh_video->bih.biWidth; |
919 dec_param.y_dim = sh_video->bih.biHeight; | |
1 | 920 dec_param.color_depth = 32; |
921 decore(0x123, DEC_OPT_INIT, &dec_param, NULL); | |
922 dec_set.postproc_level = divx_quality; | |
923 decore(0x123, DEC_OPT_SETPP, &dec_set, NULL); | |
924 } | |
925 if(verbose) printf("INFO: OpenDivX video codec init OK!\n"); | |
926 break; | |
927 } | |
928 case 1: { | |
929 // init libmpeg2: | |
41 | 930 #ifdef MPEG12_POSTPROC |
1 | 931 picture->pp_options=divx_quality; |
41 | 932 #else |
933 if(divx_quality){ | |
934 printf("WARNING! You requested image postprocessing for an MPEG 1/2 video,\n"); | |
935 printf(" but compiled MPlayer without MPEG 1/2 postprocessing support!\n"); | |
936 printf(" #define MPEG12_POSTPROC in config.h, and recompile libmpeg2!\n"); | |
937 } | |
938 #endif | |
375 | 939 mpeg2_allocate_image_buffers (picture); |
1 | 940 break; |
941 } | |
942 } | |
943 | |
944 // ================== Init output files for encoding =============== | |
945 if(encode_name){ | |
946 // encode file!!! | |
947 FILE *encode_file=fopen(encode_name,"rb"); | |
948 if(encode_file){ | |
949 fclose(encode_file); | |
950 printf("File already exists: %s (don't overwrite your favourite AVI!)\n",encode_name); | |
951 return 0; | |
952 } | |
953 encode_file=fopen(encode_name,"wb"); | |
954 if(!encode_file){ | |
955 printf("Cannot create file for encoding\n"); | |
956 return 0; | |
957 } | |
958 write_avi_header_1(encode_file,mmioFOURCC('d', 'i', 'v', 'x'),default_fps,movie_size_x,movie_size_y); | |
959 fclose(encode_file); | |
960 encode_index_name=malloc(strlen(encode_name)+8); | |
961 strcpy(encode_index_name,encode_name); | |
962 strcat(encode_index_name,".index"); | |
963 if((encode_file=fopen(encode_index_name,"wb"))) | |
964 fclose(encode_file); | |
965 else encode_index_name=NULL; | |
966 has_audio=0; // disable audio !!!!! | |
967 } | |
968 | |
969 // ========== Init keyboard FIFO (connection to libvo) ============ | |
970 | |
971 make_pipe(&keyb_fifo_get,&keyb_fifo_put); | |
972 | |
973 // ========== Init display (movie_size_x*movie_size_y/out_fmt) ============ | |
974 | |
975 #ifdef X11_FULLSCREEN | |
976 if(fullscreen){ | |
977 if(vo_init()){ | |
978 //if(verbose) printf("X11 running at %dx%d depth: %d\n",vo_screenwidth,vo_screenheight,vo_depthonscreen); | |
979 } | |
980 if(!screen_size_xy) screen_size_xy=vo_screenwidth; // scale with asp.ratio | |
981 } | |
982 #endif | |
983 | |
984 if(screen_size_xy>0){ | |
985 if(screen_size_xy<=8){ | |
986 screen_size_x=screen_size_xy*movie_size_x; | |
987 screen_size_y=screen_size_xy*movie_size_y; | |
988 } else { | |
989 screen_size_x=screen_size_xy; | |
990 screen_size_y=screen_size_xy*movie_size_y/movie_size_x; | |
991 } | |
337 | 992 } else if(!vidmode){ |
993 if(!screen_size_x) screen_size_x=SCREEN_SIZE_X; | |
994 if(!screen_size_y) screen_size_y=SCREEN_SIZE_Y; | |
1 | 995 if(screen_size_x<=8) screen_size_x*=movie_size_x; |
996 if(screen_size_y<=8) screen_size_y*=movie_size_y; | |
997 } | |
208
ae0f909ccc7c
Adds code to deal with vidmode selection. -- mgraffam
mgraffam
parents:
190
diff
changeset
|
998 |
340 | 999 { const vo_info_t *info = video_out->get_info(); |
1000 printf("VO: [%s] %dx%d => %dx%d %s%s%s ",info->short_name, | |
1001 movie_size_x,movie_size_y, | |
1002 screen_size_x,screen_size_y, | |
1003 fullscreen?"fs ":"", | |
1004 vidmode?"vm ":"", | |
1005 softzoom?"zoom ":"" | |
1006 // fullscreen|(vidmode<<1)|(softzoom<<2) | |
1007 ); | |
1008 if((out_fmt&IMGFMT_BGR_MASK)==IMGFMT_BGR) | |
1009 printf("BGR%d\n",out_fmt&255); else | |
1010 if((out_fmt&IMGFMT_BGR_MASK)==IMGFMT_RGB) | |
1011 printf("RGB%d\n",out_fmt&255); else | |
1012 if(out_fmt==IMGFMT_YUY2) printf("YUY2\n"); else | |
1013 if(out_fmt==IMGFMT_YV12) printf("YV12\n"); | |
1014 } | |
1015 | |
1016 // if(verbose) printf("Destination size: %d x %d out_fmt=%0X\n", | |
1017 // screen_size_x,screen_size_y,out_fmt); | |
1 | 1018 |
337 | 1019 if(verbose) printf("video_out->init(%dx%d->%dx%d,flags=%d,'%s',0x%X)\n", |
1 | 1020 movie_size_x,movie_size_y, |
1021 screen_size_x,screen_size_y, | |
337 | 1022 fullscreen|(vidmode<<1)|(softzoom<<2), |
1023 title,out_fmt); | |
1 | 1024 |
1025 if(video_out->init(movie_size_x,movie_size_y, | |
1026 screen_size_x,screen_size_y, | |
337 | 1027 fullscreen|(vidmode<<1)|(softzoom<<2), |
1028 title,out_fmt)){ | |
1 | 1029 printf("FATAL: Cannot initialize video driver!\n");exit(1); |
1030 } | |
1031 if(verbose) printf("INFO: Video OUT driver init OK!\n"); | |
1032 | |
1033 fflush(stdout); | |
1034 | |
1035 //================== MAIN: ========================== | |
1036 { | |
1037 int audio_fd=-1; | |
1038 float buffer_delay=0; | |
1039 float frame_correction=0; // A-V timestamp kulonbseg atlagolas | |
1040 int frame_corr_num=0; // | |
1041 float a_frame=0; // Audio | |
1042 float v_frame=0; // Video | |
1043 float time_frame=0; // Timer | |
1044 float a_pts=0; | |
1045 float v_pts=0; | |
1046 float c_total=0; | |
1047 float max_pts_correction=default_max_pts_correction; | |
1048 int eof=0; | |
1049 int force_redraw=0; | |
1050 float num_frames=0; // number of frames played | |
1051 //int real_num_frames=0; // number of frames readed | |
1052 double video_time_usage=0; | |
1053 double vout_time_usage=0; | |
1054 double audio_time_usage=0; | |
36 | 1055 int grab_frames=0; |
212 | 1056 char osd_text_buffer[64]; |
371 | 1057 int osd_level=2; |
220 | 1058 int osd_visible=100; |
1059 int osd_function=OSD_PLAY; | |
1 | 1060 |
1061 #ifdef HAVE_LIRC | |
1062 lirc_mp_setup(); | |
1063 #endif | |
1064 | |
1065 #ifdef USE_TERMCAP | |
1066 load_termcap(NULL); // load key-codes | |
1067 #endif | |
1068 getch2_enable(); | |
1069 | |
1070 //========= Catch terminate signals: ================ | |
1071 // terminate requests: | |
1072 signal(SIGTERM,exit_sighandler); // kill | |
1073 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed | |
1074 signal(SIGINT,exit_sighandler); // Interrupt from keyboard | |
1075 signal(SIGQUIT,exit_sighandler); // Quit from keyboard | |
1076 // fatal errors: | |
1077 signal(SIGBUS,exit_sighandler); // bus error | |
1078 signal(SIGSEGV,exit_sighandler); // segfault | |
1079 signal(SIGILL,exit_sighandler); // illegal instruction | |
1080 signal(SIGFPE,exit_sighandler); // floating point exc. | |
1081 signal(SIGABRT,exit_sighandler); // abort() | |
1082 | |
1083 //================ SETUP AUDIO ========================== | |
1084 current_module="setup_audio"; | |
1085 | |
1086 if(has_audio){ | |
1087 #ifdef USE_XMMP_AUDIO | |
1088 xmm_Init( &xmm ); | |
1089 xmm.cSound = (XMM_PluginSound *)xmm_PluginRegister( XMMP_AUDIO_DRIVER ); | |
1090 if( xmm.cSound ){ | |
1091 pSound = xmm.cSound->Init( &xmm ); | |
296 | 1092 if( pSound && pSound->Start( pSound, sh_audio->samplerate, sh_audio->channels, |
1093 ( sh_audio->samplesize == 2 ) ? XMM_SOUND_FMT_S16LE : XMM_SOUND_FMT_U8 )){ | |
1 | 1094 printf("XMM: audio setup ok\n"); |
1095 } else { | |
1096 has_audio=0; | |
1097 } | |
1098 } else has_audio=0; | |
1099 #else | |
1100 audio_fd=open(dsp, O_WRONLY); | |
1101 if(audio_fd<0){ | |
1102 printf("Can't open audio device %s -> nosound\n",dsp); | |
1103 has_audio=0; | |
1104 } | |
1105 #endif | |
1106 } | |
1107 | |
1108 if(has_audio){ | |
1109 #ifdef USE_XMMP_AUDIO | |
1110 if(audio_buffer_size==-1){ | |
1111 // Measuring buffer size: | |
1112 buffer_delay=pSound->QueryDelay(pSound, 0); | |
1113 } else { | |
1114 // -abs commandline option | |
296 | 1115 buffer_delay=audio_buffer_size/(float)(sh_audio->o_bps); |
1 | 1116 } |
1117 #else | |
1118 int r; | |
296 | 1119 r=(sh_audio->samplesize==2)?AFMT_S16_LE:AFMT_U8;ioctl (audio_fd, SNDCTL_DSP_SETFMT, &r); |
1120 r=sh_audio->channels-1; ioctl (audio_fd, SNDCTL_DSP_STEREO, &r); | |
1121 r=sh_audio->samplerate; if(ioctl (audio_fd, SNDCTL_DSP_SPEED, &r)==-1) | |
1 | 1122 printf("audio_setup: your card doesn't support %d Hz samplerate\n",r); |
1123 | |
103 | 1124 #if 0 |
1125 // r = (64 << 16) + 1024; | |
1126 r = (65536 << 16) + 512; | |
1127 if(ioctl (audio_fd, SNDCTL_DSP_SETFRAGMENT, &r)==-1) | |
1128 printf("audio_setup: your card doesn't support setting fragments\n",r); | |
1129 #endif | |
1130 | |
1 | 1131 if(audio_buffer_size==-1){ |
1132 // Measuring buffer size: | |
1133 audio_buffer_size=0; | |
1134 #ifdef HAVE_AUDIO_SELECT | |
1135 while(audio_buffer_size<0x40000){ | |
1136 fd_set rfds; | |
1137 struct timeval tv; | |
1138 FD_ZERO(&rfds); FD_SET(audio_fd,&rfds); | |
1139 tv.tv_sec=0; tv.tv_usec = 0; | |
1140 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) break; | |
296 | 1141 write(audio_fd,&sh_audio->a_buffer[sh_audio->a_buffer_len],OUTBURST); |
1 | 1142 audio_buffer_size+=OUTBURST; |
1143 } | |
1144 if(audio_buffer_size==0){ | |
1145 printf("\n *** Your audio driver DOES NOT support select() ***\n"); | |
1146 printf("Recompile mplayer with #undef HAVE_AUDIO_SELECT in config.h !\n\n"); | |
1147 exit_player("audio_init"); | |
1148 } | |
1149 #endif | |
1150 } | |
296 | 1151 buffer_delay=audio_buffer_size/(float)(sh_audio->o_bps); |
1 | 1152 #endif |
1153 a_frame=-(buffer_delay); | |
1154 printf("Audio buffer size: %d bytes, delay: %5.3fs\n",audio_buffer_size,buffer_delay); | |
1155 } | |
1156 | |
1157 | |
340 | 1158 if(!has_audio){ |
1 | 1159 printf("Audio: no sound\n"); |
1160 if(verbose) printf("Freeing %d unused audio chunks\n",d_audio->packs); | |
1161 ds_free_packs(d_audio); // free buffered chunks | |
1162 d_audio->id=-2; // do not read audio chunks | |
296 | 1163 if(sh_audio->a_buffer) free(sh_audio->a_buffer); |
1164 alsa=1; | |
1165 // fake, required for timer: | |
1166 sh_audio->samplerate=76800; | |
1167 sh_audio->samplesize=sh_audio->channels=2; | |
1168 sh_audio->o_bps=sh_audio->channels*sh_audio->samplerate*sh_audio->samplesize; | |
1 | 1169 } |
1170 | |
1171 current_module=NULL; | |
1172 | |
1173 //==================== START PLAYING ======================= | |
1174 | |
109 | 1175 if(file_format==DEMUXER_TYPE_AVI){ |
1 | 1176 a_pts=d_audio->pts-(buffer_delay+audio_delay); |
291 | 1177 audio_delay-=(float)(sh_audio->audio.dwInitialFrames-sh_video->video.dwInitialFrames)/default_fps; |
1178 // audio_delay-=(float)(sh_audio->audio.dwInitialFrames-sh_video->video.dwInitialFrames)/default_fps; | |
340 | 1179 if(verbose){ |
1180 printf("AVI Initial frame delay: %5.3f\n",(float)(sh_audio->audio.dwInitialFrames-sh_video->video.dwInitialFrames)/default_fps); | |
1181 printf("v: audio_delay=%5.3f buffer_delay=%5.3f a_pts=%5.3f a_frame=%5.3f\n", | |
1182 audio_delay,buffer_delay,a_pts,a_frame); | |
1183 printf("START: a_pts=%5.3f v_pts=%5.3f \n",d_audio->pts,d_video->pts); | |
1184 } | |
1 | 1185 delay_corrected=0; // has to correct PTS diffs |
1186 d_video->pts=0;d_audio->pts=0; // PTS is outdated now! | |
1187 } | |
1188 if(force_fps) default_fps=force_fps; | |
1189 | |
1190 printf("Start playing...\n");fflush(stdout); | |
1191 | |
1192 InitTimer(); | |
1193 | |
1194 while(!eof){ | |
1195 | |
1196 /*========================== PLAY AUDIO ============================*/ | |
1197 | |
1198 while(has_audio){ | |
291 | 1199 |
1200 // Update buffer if needed | |
1 | 1201 unsigned int t=GetTimer(); |
1202 current_module="decode_audio"; // Enter AUDIO decoder module | |
303 | 1203 sh_audio->codec->driver=has_audio; // FIXME! |
296 | 1204 while(sh_audio->a_buffer_len<OUTBURST && !d_audio->eof){ |
1205 int ret=decode_audio(sh_audio,&sh_audio->a_buffer[sh_audio->a_buffer_len],sh_audio->a_buffer_size-sh_audio->a_buffer_len); | |
1206 if(ret>0) sh_audio->a_buffer_len+=ret; else break; | |
1 | 1207 } |
1208 current_module=NULL; // Leave AUDIO decoder module | |
291 | 1209 t=GetTimer()-t;audio_time_usage+=t*0.000001; |
1210 | |
1 | 1211 |
1212 // Play sound from the buffer: | |
296 | 1213 if(sh_audio->a_buffer_len>=OUTBURST){ // if not EOF |
1 | 1214 #ifdef USE_XMMP_AUDIO |
296 | 1215 pSound->Write( pSound, sh_audio->a_buffer, OUTBURST ); |
1 | 1216 #else |
1217 #ifdef SIMULATE_ALSA | |
296 | 1218 fake_ALSA_write(audio_fd,sh_audio->a_buffer,OUTBURST); // for testing purposes |
1 | 1219 #else |
296 | 1220 write(audio_fd,sh_audio->a_buffer,OUTBURST); |
1 | 1221 #endif |
1222 #endif | |
296 | 1223 sh_audio->a_buffer_len-=OUTBURST; |
1224 memcpy(sh_audio->a_buffer,&sh_audio->a_buffer[OUTBURST],sh_audio->a_buffer_len); | |
1 | 1225 #ifndef USE_XMMP_AUDIO |
1226 #ifndef SIMULATE_ALSA | |
1227 // check buffer | |
1228 #ifdef HAVE_AUDIO_SELECT | |
1229 { fd_set rfds; | |
1230 struct timeval tv; | |
1231 FD_ZERO(&rfds); | |
1232 FD_SET(audio_fd, &rfds); | |
1233 tv.tv_sec = 0; | |
1234 tv.tv_usec = 0; | |
1235 if(select(audio_fd+1, NULL, &rfds, NULL, &tv)){ | |
296 | 1236 a_frame+=OUTBURST/(float)(sh_audio->o_bps); |
1237 a_pts+=OUTBURST/(float)(sh_audio->o_bps); | |
1 | 1238 // printf("Filling audio buffer...\n"); |
1239 continue; | |
1240 // } else { | |
1241 // printf("audio buffer full...\n"); | |
1242 } | |
1243 } | |
1244 #endif | |
1245 #endif | |
1246 #endif | |
1247 } | |
1248 | |
1249 break; | |
1250 } // if(has_audio) | |
1251 | |
1252 /*========================== UPDATE TIMERS ============================*/ | |
1253 | |
296 | 1254 a_frame+=OUTBURST/(float)(sh_audio->o_bps); |
1255 a_pts+=OUTBURST/(float)(sh_audio->o_bps); | |
1 | 1256 |
1257 if(alsa){ | |
1258 // Use system timer for sync, not audio card/driver | |
296 | 1259 time_frame+=OUTBURST/(float)(sh_audio->o_bps); |
1 | 1260 time_frame-=GetRelativeTime(); |
1261 // printf("time_frame=%5.3f\n",time_frame); | |
1262 if(time_frame<-0.1 || time_frame>0.1){ | |
1263 time_frame=0; | |
1264 } else { | |
1265 // if(time_frame>0.01) usleep(1000000*(time_frame-0.01)); // sleeping | |
103 | 1266 // if(time_frame>0.019) usleep(1000000*(time_frame-0.019)); // sleeping |
1267 // if(time_frame>0.001) usleep(1000000*(time_frame)); // sleeping | |
1268 // if(time_frame>0.02) usleep(1000000*(time_frame)); // sleeping if >20ms | |
1269 while(time_frame>0.007){ | |
1270 // printf("TIMER %8.3f -> ",time_frame*1000); | |
1271 // if(time_frame>0.021) | |
1272 // usleep(time_frame-0.12); | |
1273 // else | |
1274 usleep(1000); | |
1275 time_frame-=GetRelativeTime(); | |
1276 // printf("%8.3f \n",time_frame*1000); | |
1277 } | |
1278 } | |
1279 | |
1 | 1280 } |
1281 | |
1282 | |
1283 /*========================== PLAY VIDEO ============================*/ | |
1284 | |
1285 if(1) | |
1286 while(v_frame<a_frame || force_redraw){ | |
1287 | |
1288 current_module="decode_video"; | |
1289 | |
1290 //-------------------- Decode a frame: ----------------------- | |
1291 switch(has_video){ | |
1292 case 3: { | |
1293 // OpenDivX | |
1294 unsigned int t=GetTimer(); | |
1295 unsigned int t2; | |
1296 DEC_FRAME dec_frame; | |
1297 char* start=NULL; | |
1298 int in_size=ds_get_packet(d_video,&start); | |
1299 if(in_size<0){ eof=1;break;} | |
1300 if(in_size>max_framesize) max_framesize=in_size; | |
1301 // let's decode | |
1302 dec_frame.length = in_size; | |
1303 dec_frame.bitstream = start; | |
1304 //dec_frame.bmp = video_out; | |
1305 dec_frame.render_flag = 1; | |
1306 decore(0x123, 0, &dec_frame, NULL); | |
1307 t2=GetTimer();t=t2-t;video_time_usage+=t*0.000001f; | |
1308 | |
1309 if(opendivx_src[0]){ | |
1310 video_out->draw_slice(opendivx_src,opendivx_stride, | |
1311 movie_size_x,movie_size_y,0,0); | |
117 | 1312 // video_out->flip_page(); |
1 | 1313 opendivx_src[0]=NULL; |
1314 } | |
1315 | |
1316 t2=GetTimer()-t2;vout_time_usage+=t2*0.000001f; | |
1317 | |
1318 ++num_frames; | |
291 | 1319 v_frame+=1.0f/default_fps; //(float)sh_video->video.dwScale/(float)sh_video->video.dwRate; |
1 | 1320 |
1321 break; | |
1322 } | |
175 | 1323 #ifdef USE_DIRECTSHOW |
1324 case 4: { // W32/DirectShow | |
1325 char* start=NULL; | |
1326 unsigned int t=GetTimer(); | |
1327 unsigned int t2; | |
1328 float pts1=d_video->pts; | |
1329 int in_size=ds_get_packet(d_video,&start); | |
1330 float pts2=d_video->pts; | |
1331 if(in_size<0){ eof=1;break;} | |
1332 if(in_size>max_framesize) max_framesize=in_size; | |
1333 | |
1334 // printf("frame len = %5.4f\n",pts2-pts1); | |
1335 | |
1336 DS_VideoDecoder_DecodeFrame(start, in_size, 0, 1); | |
1337 | |
1338 t2=GetTimer();t=t2-t;video_time_usage+=t*0.000001f; | |
291 | 1339 video_out->draw_frame((uint8_t **)&sh_video->our_out_buffer); |
175 | 1340 // video_out->flip_page(); |
1341 t2=GetTimer()-t2;vout_time_usage+=t2*0.000001f; | |
1342 | |
1343 ++num_frames; | |
1344 | |
1345 if(file_format==DEMUXER_TYPE_ASF){ | |
1346 float d=pts2-pts1; | |
1347 if(d>0 && d<0.2) v_frame+=d; | |
1348 } else | |
291 | 1349 v_frame+=1.0f/default_fps; //(float)sh_video->video.dwScale/(float)sh_video->video.dwRate; |
1350 //v_pts+=1.0f/default_fps; //(float)sh_video->video.dwScale/(float)sh_video->video.dwRate; | |
175 | 1351 |
1352 break; | |
1353 } | |
1354 #endif | |
1 | 1355 case 2: { |
1356 HRESULT ret; | |
1357 char* start=NULL; | |
1358 unsigned int t=GetTimer(); | |
1359 unsigned int t2; | |
1360 float pts1=d_video->pts; | |
1361 int in_size=ds_get_packet(d_video,&start); | |
1362 float pts2=d_video->pts; | |
1363 if(in_size<0){ eof=1;break;} | |
1364 if(in_size>max_framesize) max_framesize=in_size; | |
1365 | |
1366 // printf("frame len = %5.4f\n",pts2-pts1); | |
1367 | |
1368 //if(in_size>0){ | |
291 | 1369 sh_video->bih.biSizeImage = in_size; |
1370 ret = ICDecompress(sh_video->hic, ICDECOMPRESS_NOTKEYFRAME, | |
1 | 1371 // ret = ICDecompress(avi_header.hic, ICDECOMPRESS_NOTKEYFRAME|(ICDECOMPRESS_HURRYUP|ICDECOMPRESS_PREROL), |
291 | 1372 &sh_video->bih, start, |
1373 &sh_video->o_bih, sh_video->our_out_buffer); | |
1 | 1374 if(ret){ printf("Error decompressing frame, err=%d\n",ret);break; } |
1375 //} | |
1376 | |
1377 t2=GetTimer();t=t2-t;video_time_usage+=t*0.000001f; | |
291 | 1378 video_out->draw_frame((uint8_t **)&sh_video->our_out_buffer); |
117 | 1379 // video_out->flip_page(); |
1 | 1380 t2=GetTimer()-t2;vout_time_usage+=t2*0.000001f; |
1381 | |
1382 ++num_frames; | |
1383 | |
1384 if(file_format==DEMUXER_TYPE_ASF){ | |
1385 float d=pts2-pts1; | |
1386 if(d>0 && d<0.2) v_frame+=d; | |
1387 } else | |
291 | 1388 v_frame+=1.0f/default_fps; //(float)sh_video->video.dwScale/(float)sh_video->video.dwRate; |
1389 //v_pts+=1.0f/default_fps; //(float)sh_video->video.dwScale/(float)sh_video->video.dwRate; | |
1 | 1390 |
1391 break; | |
1392 } | |
1393 case 1: { | |
111 | 1394 #ifndef HAVE_CODECCTRL |
1395 | |
1396 int in_frame=0; | |
1397 videobuf_len=0; | |
1398 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){ | |
1399 int i=sync_video_packet(d_video); | |
1400 if(in_frame){ | |
1401 if(i<0x101 || i>=0x1B0){ // not slice code -> end of frame | |
1402 // send END OF FRAME code: | |
1403 #if 1 | |
1404 videobuffer[videobuf_len+0]=0; | |
1405 videobuffer[videobuf_len+1]=0; | |
1406 videobuffer[videobuf_len+2]=1; | |
1407 videobuffer[videobuf_len+3]=0xFF; | |
1408 videobuf_len+=4; | |
1409 #endif | |
1410 if(!i) eof=1; // EOF | |
1411 break; | |
1412 } | |
1413 } else { | |
1414 //if(i==0x100) in_frame=1; // picture startcode | |
1415 if(i>=0x101 && i<0x1B0) in_frame=1; // picture startcode | |
1416 else if(!i){ eof=1; break;} // EOF | |
1417 } | |
1418 if(grab_frames==2 && (i==0x1B3 || i==0x1B8)) grab_frames=1; | |
1419 if(!read_video_packet(d_video)){ eof=1; break;} // EOF | |
1420 //printf("read packet 0x%X, len=%d\n",i,videobuf_len); | |
1421 } | |
1422 | |
1423 if(videobuf_len>max_framesize) max_framesize=videobuf_len; // debug | |
1424 //printf("--- SEND %d bytes\n",videobuf_len); | |
1425 if(grab_frames==1){ | |
1426 FILE *f=fopen("grab.mpg","ab"); | |
1427 fwrite(videobuffer,videobuf_len-4,1,f); | |
1428 fclose(f); | |
1429 } | |
1430 ++dbg_es_sent; | |
1431 //if(videobuf_len>4) | |
1432 //my_write(data_fifo,(char*) &videobuf_len,4); | |
1433 | |
1434 { int t=0; | |
1435 int x; | |
1436 float l; | |
1437 t-=GetTimer(); | |
1438 mpeg2_decode_data(video_out, videobuffer, videobuffer+videobuf_len); | |
1439 t+=GetTimer(); | |
1440 //*** CMD=0 : Frame completed *** | |
1441 //send_cmd(control_fifo2,0); // FRAME_COMPLETED command | |
1442 x=frameratecode2framerate[picture->frame_rate_code]; //fps | |
1443 ++dbg_es_rcvd; | |
1444 l=(100+picture->repeat_count)*0.01f; | |
1445 num_frames+=l; | |
1446 picture->repeat_count=0; | |
1447 video_time_usage+=t*0.000001; | |
1448 if(x && !force_fps) default_fps=x*0.0001f; | |
1449 if(!force_redraw){ | |
1450 // increase video timers: | |
1451 v_frame+=l/default_fps; | |
1452 v_pts+=l/default_fps; | |
1453 } | |
1454 } | |
1455 //if(eof) break; | |
1456 | |
1457 #else | |
1 | 1458 while(1){ |
1459 int x; | |
1460 while(1){ | |
1461 x=-1; // paranoia | |
1462 if(4==read(control_fifo,&x,4)) break; // status/command | |
1463 usleep(5000); // do not eat 100% CPU (waiting for codec restart) | |
1464 } | |
1465 if(x==0x3030303){ | |
1466 //*** CMD=3030303 : Video packet requested *** | |
1467 // read a single compressed frame: | |
1468 int in_frame=0; | |
1469 videobuf_len=0; | |
1470 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){ | |
1471 int i=sync_video_packet(d_video); | |
1472 if(in_frame){ | |
1473 if(i<0x101 || i>=0x1B0){ // not slice code -> end of frame | |
1474 // send END OF FRAME code: | |
1475 #if 1 | |
1476 videobuffer[videobuf_len+0]=0; | |
1477 videobuffer[videobuf_len+1]=0; | |
1478 videobuffer[videobuf_len+2]=1; | |
1479 videobuffer[videobuf_len+3]=0xFF; | |
1480 videobuf_len+=4; | |
1481 #endif | |
1482 if(!i) eof=1; // EOF | |
1483 break; | |
1484 } | |
1485 } else { | |
1486 //if(i==0x100) in_frame=1; // picture startcode | |
1487 if(i>=0x101 && i<0x1B0) in_frame=1; // picture startcode | |
1488 else if(!i){ eof=1; break;} // EOF | |
1489 } | |
36 | 1490 if(grab_frames==2 && (i==0x1B3 || i==0x1B8)) grab_frames=1; |
1 | 1491 if(!read_video_packet(d_video)){ eof=1; break;} // EOF |
1492 //printf("read packet 0x%X, len=%d\n",i,videobuf_len); | |
1493 } | |
1494 if(videobuf_len>max_framesize) max_framesize=videobuf_len; // debug | |
1495 //printf("--- SEND %d bytes\n",videobuf_len); | |
36 | 1496 if(grab_frames==1){ |
1497 FILE *f=fopen("grab.mpg","ab"); | |
1498 fwrite(videobuffer,videobuf_len-4,1,f); | |
1499 fclose(f); | |
1500 } | |
1 | 1501 ++dbg_es_sent; |
1502 //if(videobuf_len>4) | |
1503 my_write(data_fifo,(char*) &videobuf_len,4); | |
1504 if(eof) break; | |
1505 } else | |
1506 if(x==0){ | |
1507 //*** CMD=0 : Frame completed *** | |
1508 int l=100; | |
1509 int t=0; | |
1510 read(control_fifo,&x,4); // FPS | |
1511 read(control_fifo,&l,4); // Length*100 | |
1512 read(control_fifo,&t,4); // Time*1000000 | |
1513 //printf("+++ FRAME COMPLETED fps=%d len=%d time=%d\n",x,l,t); | |
1514 ++dbg_es_rcvd; | |
1515 num_frames+=l/100.0f; | |
1516 video_time_usage+=t*0.000001; | |
1517 if(x && !force_fps) default_fps=x*0.0001f; | |
1518 if(!force_redraw){ | |
1519 // increase video timers: | |
1520 v_frame+=l*0.01f/default_fps; | |
1521 v_pts+=l*0.01f/default_fps; | |
1522 } | |
1523 break; // frame OK. | |
1524 } else | |
1525 if(x==0x22222222){ | |
1526 //*** CMD=22222222 : codec reset/restart *** | |
1527 read(control_fifo,&codec_pid,4); // PID | |
1528 printf("\nVideo codec started... (pid %d)\n",codec_pid); | |
1529 send_cmd(data_fifo,0x22222222); // send response (syncword) | |
1530 dbg_es_sent=dbg_es_rcvd=0; | |
1531 //printf(" [ReSync-VIDEO] \n"); | |
1532 while(1){ | |
1533 int id=sync_video_packet(d_video); | |
1534 if(id==0x100 || id>=0x1B0) break; // header chunk | |
1535 if(!id || !skip_video_packet(d_video)){ eof=1; break;} // EOF | |
1536 } | |
1537 if(eof) break; | |
1538 max_pts_correction=0.2; | |
1539 } else | |
1540 printf("invalid cmd: 0x%X\n",x); | |
1541 } | |
111 | 1542 #endif |
1 | 1543 break; |
1544 } | |
1545 } // switch | |
1546 //------------------------ frame decoded. -------------------- | |
1547 | |
117 | 1548 current_module="flip_page"; |
1549 | |
1550 video_out->flip_page(); | |
1551 | |
1 | 1552 current_module=NULL; |
1553 | |
1554 if(eof) break; | |
220 | 1555 if(force_redraw){ |
1556 --force_redraw; | |
1557 if(!force_redraw) osd_function=OSD_PLAY; | |
1558 } | |
1 | 1559 |
1560 // 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,a_frame); | |
1561 // fflush(stdout); | |
1562 | |
1563 #if 1 | |
1564 /*================ A-V TIMESTAMP CORRECTION: =========================*/ | |
1565 if(has_audio){ | |
109 | 1566 if(pts_from_bps && (file_format==DEMUXER_TYPE_AVI)){ |
291 | 1567 // a_pts=(float)ds_tell(d_audio)/sh_audio->wf.nAvgBytesPerSec-(buffer_delay+audio_delay); |
1568 a_pts=(float)ds_tell(d_audio)/sh_audio->wf.nAvgBytesPerSec-(buffer_delay); | |
1 | 1569 delay_corrected=1; // hack |
1570 } else | |
1571 if(d_audio->pts){ | |
1572 // printf("\n=== APTS a_pts=%5.3f v_pts=%5.3f === \n",d_audio->pts,d_video->pts); | |
1573 #if 1 | |
1574 if(!delay_corrected){ | |
1575 float x=d_audio->pts-d_video->pts-(buffer_delay+audio_delay); | |
1576 float y=-(buffer_delay+audio_delay); | |
1577 printf("Initial PTS delay: %5.3f sec (calculated: %5.3f)\n",x,y); | |
1578 audio_delay+=x; | |
1579 //a_pts-=x; | |
1580 delay_corrected=1; | |
340 | 1581 if(verbose) |
1 | 1582 printf("v: audio_delay=%5.3f buffer_delay=%5.3f a.pts=%5.3f v.pts=%5.3f\n", |
1583 audio_delay,buffer_delay,d_audio->pts,d_video->pts); | |
1584 } | |
1585 #endif | |
1586 a_pts=d_audio->pts-(buffer_delay+audio_delay); | |
1587 d_audio->pts=0; | |
1588 } | |
1589 if(d_video->pts) v_pts=d_video->pts; | |
1590 if(frame_corr_num==5){ | |
1591 float x=(frame_correction/5.0f); | |
1592 if(!delay_corrected){ | |
1593 #if 0 | |
1594 printf("Initial PTS delay: %5.3f sec\n",x); | |
1595 delay_corrected=1; | |
1596 audio_delay+=x; | |
1597 a_pts-=x; | |
1598 #endif | |
1599 } else | |
1600 { | |
1601 printf("A:%6.1f V:%6.1f A-V:%7.3f",a_pts,v_pts,x); | |
1602 x*=0.5f; | |
1603 if(x<-max_pts_correction) x=-max_pts_correction; else | |
1604 if(x> max_pts_correction) x= max_pts_correction; | |
1605 max_pts_correction=default_max_pts_correction; | |
1606 a_frame+=x; c_total+=x; | |
1607 #if 0 | |
1608 printf(" ct:%7.3f a=%d v=%d \r",c_total, | |
1609 d_audio->pos,d_video->pos); | |
1610 #else | |
1611 printf(" ct:%7.3f %3d %2d%% %2d%% %3.1f%% %d \r",c_total, | |
1612 (int)num_frames, | |
1613 (v_frame>0.5)?(int)(100.0*video_time_usage/(double)v_frame):0, | |
1614 (v_frame>0.5)?(int)(100.0*vout_time_usage/(double)v_frame):0, | |
1615 (v_frame>0.5)?(100.0*audio_time_usage/(double)v_frame):0, | |
1616 dbg_es_sent-dbg_es_rcvd | |
1617 ); | |
1618 #endif | |
1619 fflush(stdout); | |
1620 // printf("\n"); | |
1621 } | |
1622 //force_fps+=1*force_fps*x; | |
1623 // printf(" ct:%7.3f fps:%5.2f nf:%2.1f/%d t:%d.%03d\r",c_total,default_fps,num_frames,real_num_frames,codec_time_usage_sec,codec_time_usage/1000);fflush(stdout); | |
1624 frame_corr_num=0; frame_correction=0; | |
1625 } | |
1626 if(frame_corr_num>=0) frame_correction+=a_pts-v_pts; | |
1627 } else { | |
1628 // No audio: | |
1629 if(d_video->pts) v_pts=d_video->pts; | |
1630 if(frame_corr_num==5){ | |
1631 // printf("A: --- V:%6.1f \r",v_pts); | |
1632 printf("V:%6.1f %3d %2d%% %2d%% %3.1f%% %d \r",v_pts, | |
1633 (int)num_frames, | |
1634 (v_frame>0.5)?(int)(100.0*video_time_usage/(double)v_frame):0, | |
1635 (v_frame>0.5)?(int)(100.0*vout_time_usage/(double)v_frame):0, | |
1636 (v_frame>0.5)?(100.0*audio_time_usage/(double)v_frame):0, | |
1637 dbg_es_sent-dbg_es_rcvd); | |
1638 | |
1639 fflush(stdout); | |
1640 frame_corr_num=0; | |
1641 } | |
1642 } | |
1643 ++frame_corr_num; | |
1644 #endif | |
1645 | |
220 | 1646 if(osd_visible){ |
1647 --osd_visible; | |
1648 if(!osd_visible) vo_osd_progbar_type=-1; // disable | |
1649 } | |
371 | 1650 |
1651 if(osd_function==OSD_PAUSE){ | |
1652 printf("\n------ PAUSED -------\r");fflush(stdout); | |
1653 while( | |
1654 #ifdef HAVE_LIRC | |
1655 lirc_mp_getinput()<=0 && | |
1656 #endif | |
1657 getch2(20)<=0 && mplayer_get_key()<=0){ | |
1658 video_out->check_events(); | |
1659 } | |
1660 osd_function=OSD_PLAY; | |
1661 } | |
1662 | |
1 | 1663 } // while(v_frame<a_frame || force_redraw) |
1664 | |
1665 | |
1666 //================= Keyboard events, SEEKing ==================== | |
1667 | |
1668 { int rel_seek_secs=0; | |
1669 int c; | |
1670 while( | |
1671 #ifdef HAVE_LIRC | |
1672 (c=lirc_mp_getinput())>0 || | |
1673 #endif | |
1674 (c=getch2(0))>0 || (c=mplayer_get_key())>0) switch(c){ | |
1675 // seek 10 sec | |
1676 case KEY_RIGHT: | |
220 | 1677 osd_function=OSD_FFW; |
1 | 1678 rel_seek_secs+=10;break; |
1679 case KEY_LEFT: | |
220 | 1680 osd_function=OSD_REW; |
1 | 1681 rel_seek_secs-=10;break; |
1682 // seek 1 min | |
1683 case KEY_UP: | |
220 | 1684 osd_function=OSD_FFW; |
1 | 1685 rel_seek_secs+=60;break; |
1686 case KEY_DOWN: | |
220 | 1687 osd_function=OSD_REW; |
1 | 1688 rel_seek_secs-=60;break; |
1689 // delay correction: | |
1690 case '+': | |
220 | 1691 buffer_delay+=0.1; // increase audio buffer delay |
1 | 1692 a_frame-=0.1; |
1693 break; | |
1694 case '-': | |
220 | 1695 buffer_delay-=0.1; // decrease audio buffer delay |
1 | 1696 a_frame+=0.1; |
1697 break; | |
1698 // quit | |
1699 case KEY_ESC: // ESC | |
1700 case KEY_ENTER: // ESC | |
1701 case 'q': exit_player("Quit"); | |
36 | 1702 case 'g': grab_frames=2;break; |
1 | 1703 // restart codec |
112 | 1704 #ifdef HAVE_CODECCTRL |
1 | 1705 case 'k': kill(codec_pid,SIGKILL);break; |
1706 // case 'k': kill(child_pid,SIGKILL);break; | |
112 | 1707 #endif |
1 | 1708 // pause |
1709 case 'p': | |
1710 case ' ': | |
220 | 1711 osd_function=OSD_PAUSE; |
371 | 1712 #if 0 |
1 | 1713 printf("\n------ PAUSED -------\r");fflush(stdout); |
1714 while( | |
1715 #ifdef HAVE_LIRC | |
1716 lirc_mp_getinput()<=0 && | |
1717 #endif | |
33 | 1718 getch2(20)<=0 && mplayer_get_key()<=0){ |
1719 video_out->check_events(); | |
1720 } | |
220 | 1721 osd_function=OSD_PLAY; |
371 | 1722 #endif |
1723 break; | |
1724 case 'o': // toggle OSD | |
1725 osd_level=(osd_level+1)%3; | |
1 | 1726 break; |
1727 } | |
1728 if(rel_seek_secs) | |
1729 if(file_format==DEMUXER_TYPE_AVI && avi_header.idx_size<=0){ | |
1730 printf("Can't seek in raw .AVI streams! (index required) \n"); | |
1731 } else { | |
1732 int skip_audio_bytes=0; | |
1733 float skip_audio_secs=0; | |
1734 | |
1735 // clear demux buffers: | |
1736 if(has_audio) ds_free_packs(d_audio); | |
1737 ds_free_packs(d_video); | |
1738 | |
296 | 1739 // printf("sh_audio->a_buffer_len=%d \n",sh_audio->a_buffer_len); |
1740 sh_audio->a_buffer_len=0; | |
1 | 1741 |
1742 switch(file_format){ | |
1743 | |
1744 case DEMUXER_TYPE_AVI: { | |
1745 //================= seek in AVI ========================== | |
1746 int rel_seek_frames=rel_seek_secs*default_fps; | |
1747 int curr_audio_pos=0; | |
1748 int audio_chunk_pos=-1; | |
1749 int video_chunk_pos=d_video->pos; | |
1750 | |
1751 skip_video_frames=0; | |
1752 | |
1753 // SEEK streams | |
1754 // if(d_video->pts) avi_video_pts=d_video->pts; | |
1755 avi_audio_pts=0; | |
1756 d_video->pts=0; | |
1757 d_audio->pts=0; | |
1758 | |
1759 // find video chunk pos: | |
1760 if(rel_seek_frames>0){ | |
1761 // seek forward | |
1762 while(video_chunk_pos<avi_header.idx_size){ | |
1763 int id=avi_header.idx[video_chunk_pos].ckid; | |
1764 // if(LOWORD(id)==aviTWOCC('0','0')){ // video frame | |
1765 if(avi_stream_id(id)==d_video->id){ // video frame | |
1766 if((--rel_seek_frames)<0 && avi_header.idx[video_chunk_pos].dwFlags&AVIIF_KEYFRAME) break; | |
291 | 1767 v_pts+=(float)sh_video->video.dwScale/(float)sh_video->video.dwRate; |
1 | 1768 ++skip_audio_bytes; |
1769 } | |
1770 ++video_chunk_pos; | |
1771 } | |
1772 } else { | |
1773 // seek backward | |
1774 while(video_chunk_pos>=0){ | |
1775 int id=avi_header.idx[video_chunk_pos].ckid; | |
1776 // if(LOWORD(id)==aviTWOCC('0','0')){ // video frame | |
1777 if(avi_stream_id(id)==d_video->id){ // video frame | |
1778 if((++rel_seek_frames)>0 && avi_header.idx[video_chunk_pos].dwFlags&AVIIF_KEYFRAME) break; | |
291 | 1779 v_pts-=(float)sh_video->video.dwScale/(float)sh_video->video.dwRate; |
1 | 1780 --skip_audio_bytes; |
1781 } | |
1782 --video_chunk_pos; | |
1783 } | |
1784 } | |
1785 avi_header.idx_pos_a=avi_header.idx_pos_v= | |
1786 avi_header.idx_pos=video_chunk_pos; | |
1787 // printf("%d frames skipped\n",skip_audio_bytes); | |
1788 | |
1789 #if 1 | |
1790 // re-calc video pts: | |
1791 avi_video_pts=0; | |
1792 for(i=0;i<video_chunk_pos;i++){ | |
1793 int id=avi_header.idx[i].ckid; | |
1794 // if(LOWORD(id)==aviTWOCC('0','0')){ // video frame | |
1795 if(avi_stream_id(id)==d_video->id){ // video frame | |
291 | 1796 avi_video_pts+=(float)sh_video->video.dwScale/(float)sh_video->video.dwRate; |
1 | 1797 } |
1798 } | |
1799 //printf("v-pts recalc! %5.3f -> %5.3f \n",v_pts,avi_video_pts); | |
1800 v_pts=avi_video_pts; | |
1801 #else | |
1802 avi_video_pts=v_pts; | |
1803 #endif | |
1804 a_pts=avi_video_pts-(buffer_delay); | |
1805 //a_pts=v_pts; //-(buffer_delay+audio_delay); | |
1806 | |
1807 if(has_audio){ | |
1808 int i; | |
1809 int apos=0; | |
1810 int last=0; | |
1811 int len=0; | |
1812 | |
1813 // calc new audio position in audio stream: (using avg.bps value) | |
291 | 1814 curr_audio_pos=(avi_video_pts) * sh_audio->wf.nAvgBytesPerSec; |
1 | 1815 if(curr_audio_pos<0)curr_audio_pos=0; |
1816 #if 1 | |
1817 curr_audio_pos&=~15; // requires for PCM formats!!! | |
1818 #else | |
291 | 1819 curr_audio_pos/=sh_audio->wf.nBlockAlign; |
1820 curr_audio_pos*=sh_audio->wf.nBlockAlign; | |
1 | 1821 avi_header.audio_seekable=1; |
1822 #endif | |
1823 | |
1824 // find audio chunk pos: | |
1825 for(i=0;i<video_chunk_pos;i++){ | |
1826 int id=avi_header.idx[i].ckid; | |
1827 //if(TWOCCFromFOURCC(id)==cktypeWAVEbytes){ | |
1828 if(avi_stream_id(id)==d_audio->id){ | |
1829 int aid=StreamFromFOURCC(id); | |
1830 if(d_audio->id==aid || d_audio->id==-1){ | |
1831 len=avi_header.idx[i].dwChunkLength; | |
1832 last=i; | |
1833 if(apos<=curr_audio_pos && curr_audio_pos<(apos+len)){ | |
1834 if(verbose)printf("break;\n"); | |
1835 break; | |
1836 } | |
1837 apos+=len; | |
1838 } | |
1839 } | |
1840 } | |
1841 if(verbose)printf("XXX i=%d last=%d apos=%d curr_audio_pos=%d \n", | |
1842 i,last,apos,curr_audio_pos); | |
1843 // audio_chunk_pos=last; // maybe wrong (if not break; ) | |
1844 audio_chunk_pos=i; // maybe wrong (if not break; ) | |
1845 skip_audio_bytes=curr_audio_pos-apos; | |
1846 | |
1847 // update stream position: | |
1848 d_audio->pos=audio_chunk_pos; | |
1849 d_audio->dpos=apos; | |
1850 avi_header.idx_pos_a=avi_header.idx_pos_v= | |
1851 avi_header.idx_pos=audio_chunk_pos; | |
1852 | |
303 | 1853 if(!(sh_audio->codec->flags&CODECS_FLAG_SEEKABLE)){ |
1 | 1854 #if 0 |
1855 // curr_audio_pos=apos; // selected audio codec can't seek in chunk | |
291 | 1856 skip_audio_secs=(float)skip_audio_bytes/(float)sh_audio->wf.nAvgBytesPerSec; |
1 | 1857 //printf("Seek_AUDIO: %d bytes --> %5.3f secs\n",skip_audio_bytes,skip_audio_secs); |
1858 skip_audio_bytes=0; | |
1859 #else | |
291 | 1860 int d=skip_audio_bytes % sh_audio->wf.nBlockAlign; |
1 | 1861 skip_audio_bytes-=d; |
1862 // curr_audio_pos-=d; | |
291 | 1863 skip_audio_secs=(float)d/(float)sh_audio->wf.nAvgBytesPerSec; |
1 | 1864 //printf("Seek_AUDIO: %d bytes --> %5.3f secs\n",d,skip_audio_secs); |
1865 #endif | |
1866 } | |
1867 // now: audio_chunk_pos=pos in index | |
1868 // skip_audio_bytes=bytes to skip from that chunk | |
1869 // skip_audio_secs=time to play audio before video (if can't skip) | |
1870 | |
1871 // calc skip_video_frames & adjust video pts counter: | |
1872 // i=last; | |
1873 i=avi_header.idx_pos; | |
1874 while(i<video_chunk_pos){ | |
1875 int id=avi_header.idx[i].ckid; | |
1876 // if(LOWORD(id)==aviTWOCC('0','0')){ // video frame | |
1877 if(avi_stream_id(id)==d_video->id){ // video frame | |
1878 ++skip_video_frames; | |
1879 // requires for correct audio pts calculation (demuxer): | |
291 | 1880 avi_video_pts-=(float)sh_video->video.dwScale/(float)sh_video->video.dwRate; |
1 | 1881 } |
1882 ++i; | |
1883 } | |
1884 | |
1885 } | |
1886 | |
1887 if(verbose) printf("SEEK: idx=%d (a:%d v:%d) v.skip=%d a.skip=%d/%4.3f \n", | |
1888 avi_header.idx_pos,audio_chunk_pos,video_chunk_pos, | |
1889 skip_video_frames,skip_audio_bytes,skip_audio_secs); | |
1890 | |
220 | 1891 // Set OSD: |
371 | 1892 if(osd_level){ |
1893 osd_visible=default_fps; | |
1894 vo_osd_progbar_type=0; | |
1895 vo_osd_progbar_value=(demuxer->filepos)/((avi_header.movi_end-avi_header.movi_start)>>8); | |
1896 //printf("avi filepos = %d \n",vo_osd_progbar_value); | |
1897 // printf("avi filepos = %d (len=%d) \n",demuxer->filepos,(avi_header.movi_end-avi_header.movi_start)); | |
1898 } | |
220 | 1899 |
1 | 1900 } |
1901 break; | |
1902 | |
1903 case DEMUXER_TYPE_ASF: { | |
1904 //================= seek in ASF ========================== | |
1905 float p_rate=10; // packets / sec | |
1906 int rel_seek_packs=rel_seek_secs*p_rate; | |
1907 int rel_seek_bytes=rel_seek_packs*fileh.packetsize; | |
1908 int newpos; | |
1909 //printf("ASF: packs: %d duration: %d \n",(int)fileh.packets,*((int*)&fileh.duration)); | |
1910 // printf("ASF_seek: %d secs -> %d packs -> %d bytes \n", | |
1911 // rel_seek_secs,rel_seek_packs,rel_seek_bytes); | |
1912 newpos=demuxer->filepos+rel_seek_bytes; | |
1913 if(newpos<0) newpos=0; | |
1914 stream_seek(demuxer->stream,newpos); | |
1915 } | |
1916 break; | |
1917 | |
1918 case DEMUXER_TYPE_MPEG_ES: | |
1919 case DEMUXER_TYPE_MPEG_PS: { | |
1920 //================= seek in MPEG ========================== | |
1921 int newpos; | |
1922 if(picture->bitrate==0x3FFFF) // unspecified? | |
1923 newpos=demuxer->filepos+2324*75*rel_seek_secs; // 174.3 kbyte/sec | |
1924 else | |
1925 newpos=demuxer->filepos+(picture->bitrate*1000/16)*rel_seek_secs; | |
1926 // picture->bitrate=2324*75*8; // standard VCD bitrate (75 sectors / sec) | |
1927 | |
1928 if(newpos<seek_to_byte) newpos=seek_to_byte; | |
1929 stream_seek(demuxer->stream,newpos); | |
1930 // re-sync video: | |
1931 videobuf_code_len=0; // reset ES stream buffer | |
1932 while(1){ | |
1933 int i=sync_video_packet(d_video); | |
1934 if(i==0x1B3 || i==0x1B8) break; // found it! | |
1935 if(!i || !skip_video_packet(d_video)){ eof=1; break;} // EOF | |
1936 } | |
1937 // re-sync audio: (must read to get actual audio PTS) | |
1938 // if(has_audio) ds_fill_buffer(d_audio); | |
1939 } | |
1940 break; | |
1941 | |
1942 } // switch(file_format) | |
1943 | |
1944 //====================== re-sync audio: ===================== | |
1945 if(has_audio){ | |
1946 | |
1947 if(skip_audio_bytes){ | |
1948 demux_read_data(d_audio,NULL,skip_audio_bytes); | |
1949 d_audio->pts=0; // PTS is outdated because of the raw data skipping | |
1950 } | |
1951 | |
1952 current_module="resync_audio"; | |
1953 | |
1954 switch(has_audio){ | |
1955 case 1: | |
1956 MP3_DecodeFrame(NULL,-2); // resync | |
1957 MP3_DecodeFrame(NULL,-2); // resync | |
1958 MP3_DecodeFrame(NULL,-2); // resync | |
1959 break; | |
1960 case 3: | |
1961 ac3_bitstream_reset(); // reset AC3 bitstream buffer | |
1962 // if(verbose){ printf("Resyncing AC3 audio...");fflush(stdout);} | |
291 | 1963 sh_audio->ac3_frame=ac3_decode_frame(); // resync |
1 | 1964 // if(verbose) printf(" OK!\n"); |
1965 break; | |
1966 case 4: | |
291 | 1967 case 7: |
1968 sh_audio->a_in_buffer_len=0; // reset ACM/DShow audio buffer | |
1 | 1969 break; |
1970 } | |
1971 | |
1972 // re-sync PTS (MPEG-PS only!!!) | |
1973 if(file_format==DEMUXER_TYPE_MPEG_PS) | |
1974 if(d_video->pts && d_audio->pts){ | |
1975 if (d_video->pts < d_audio->pts){ | |
1976 | |
1977 } else { | |
1978 while(d_video->pts > d_audio->pts){ | |
1979 switch(has_audio){ | |
1980 case 1: MP3_DecodeFrame(NULL,-2);break; // skip MPEG frame | |
291 | 1981 case 3: sh_audio->ac3_frame=ac3_decode_frame();break; // skip AC3 frame |
1 | 1982 default: ds_fill_buffer(d_audio); // skip PCM frame |
1983 } | |
1984 } | |
1985 } | |
1986 } | |
1987 | |
1988 current_module=NULL; | |
1989 | |
1990 c_total=0; // kell ez? | |
1991 printf("A:%6.1f V:%6.1f A-V:%7.3f",d_audio->pts,d_video->pts,0.0f); | |
1992 printf(" ct:%7.3f \r",c_total);fflush(stdout); | |
1993 } else { | |
1994 printf("A: --- V:%6.1f \r",d_video->pts);fflush(stdout); | |
1995 } | |
1996 | |
1997 max_pts_correction=0.1; | |
1998 frame_corr_num=-5; frame_correction=0; | |
1999 force_redraw=5; | |
2000 a_frame=-buffer_delay-skip_audio_secs; | |
2001 // a_frame=-audio_delay-buffer_delay-skip_audio_secs; | |
2002 v_frame=0; // !!!!!! | |
2003 audio_time_usage=0; video_time_usage=0; vout_time_usage=0; | |
2004 // num_frames=real_num_frames=0; | |
2005 } | |
2006 } // keyboard event handler | |
2007 | |
220 | 2008 //================= Update OSD ==================== |
2009 { int i; | |
371 | 2010 if(osd_level>=2){ |
2011 vo_osd_text=osd_text_buffer; | |
2012 sprintf(vo_osd_text,"%c %02d:%02d:%02d",osd_function,(int)v_pts/3600,((int)v_pts/60)%60,((int)v_pts)%60); | |
2013 } else { | |
2014 vo_osd_text=NULL; | |
2015 } | |
220 | 2016 // for(i=1;i<=11;i++) osd_text_buffer[10+i]=i;osd_text_buffer[10+i]=0; |
371 | 2017 // vo_osd_text=osd_text_buffer; |
258 | 2018 |
2019 // find sub | |
2020 if(subtitles){ | |
2021 if(sub_fps==0) sub_fps=default_fps; | |
2022 find_sub(sub_uses_time?(100*(v_pts+sub_delay)):((v_pts+sub_delay)*sub_fps)); // FIXME! frame counter... | |
2023 } | |
220 | 2024 } |
1 | 2025 |
2026 } // while(!eof) | |
2027 | |
2028 //printf("\nEnd of file.\n"); | |
2029 exit_player("End of file"); | |
109 | 2030 } |
2031 return 1; | |
2032 } |