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