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