Mercurial > mplayer.hg
annotate mplayer.c @ 273:167356f35b8f
lamer_protection++
author | arpi_esp |
---|---|
date | Sun, 01 Apr 2001 23:01:37 +0000 |
parents | 3ad72b5ee016 |
children | da98e96499bb |
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 // int a_idx; | |
188 // int v_idx; | |
189 // video: | |
190 AVIStreamHeader video; | |
191 char *video_codec; | |
192 BITMAPINFOHEADER bih; // in format | |
193 BITMAPINFOHEADER o_bih; // out format | |
194 HIC hic; | |
175 | 195 char *our_out_buffer; |
1 | 196 unsigned int bitrate; |
197 // video format flags: (filled by codecs.c) | |
198 char yuv_supported; // 1 if codec support YUY2 output format | |
199 char yuv_hack_needed; // requires for divx & mpeg4 | |
200 char no_32bpp_support; // requires for INDEO 3.x, 4.x | |
201 char flipped; // image is upside-down | |
175 | 202 GUID* vids_guid; |
1 | 203 // audio: |
204 AVIStreamHeader audio; | |
205 char *audio_codec; | |
206 int audio_seekable; | |
175 | 207 GUID* auds_guid; |
1 | 208 char wf_ext[64]; // in format |
209 WAVEFORMATEX wf; // out format | |
210 HACMSTREAM srcstream; | |
211 int audio_in_minsize; | |
212 int audio_out_minsize; | |
213 } avi_header_t; | |
214 | |
215 avi_header_t avi_header; | |
216 | |
217 #include "aviprint.c" | |
218 #include "codecs.c" | |
219 | |
220 extern picture_t *picture; | |
221 | |
222 char* encode_name=NULL; | |
223 char* encode_index_name=NULL; | |
224 int encode_bitrate=0; | |
225 | |
226 //**************************************************************************// | |
111 | 227 // Input media streaming & demultiplexer: |
228 //**************************************************************************// | |
1 | 229 |
230 #include "stream.c" | |
231 #include "demuxer.c" | |
232 #include "demux_avi.c" | |
233 #include "demux_mpg.c" | |
234 | |
235 demuxer_t *demuxer=NULL; | |
236 demux_stream_t *d_audio=NULL; | |
237 demux_stream_t *d_video=NULL; | |
238 | |
239 // MPEG video stream parser: | |
240 #include "parse_es.c" | |
241 | |
112 | 242 static const int frameratecode2framerate[16] = { |
243 0, 24000*10000/1001, 24*10000,25*10000, 30000*10000/1001, 30*10000,50*10000,60000*10000/1001, | |
244 60*10000, 0,0,0,0,0,0,0 | |
245 }; | |
246 | |
111 | 247 //**************************************************************************// |
248 // Audio codecs: | |
249 //**************************************************************************// | |
250 | |
1 | 251 //int mp3_read(char *buf,int size){ |
252 int mplayer_audio_read(char *buf,int size){ | |
253 int len; | |
254 len=demux_read_data(d_audio,buf,size); | |
255 return len; | |
256 } | |
257 | |
258 static void ac3_fill_buffer(uint8_t **start,uint8_t **end){ | |
259 int len=ds_get_packet(d_audio,(char**)start); | |
260 //printf("<ac3:%d>\n",len); | |
261 if(len<0) | |
262 *start = *end = NULL; | |
263 else | |
264 *end = *start + len; | |
265 } | |
266 | |
267 #include "alaw.c" | |
268 | |
269 #include "xa/xa_gsm.h" | |
270 | |
111 | 271 //**************************************************************************// |
272 // The OpenDivX stuff: | |
273 //**************************************************************************// | |
274 | |
1 | 275 unsigned char *opendivx_src[3]; |
276 int opendivx_stride[3]; | |
277 | |
111 | 278 // callback, the opendivx decoder calls this for each frame: |
80 | 279 void convert_linux(unsigned char *puc_y, int stride_y, |
1 | 280 unsigned char *puc_u, unsigned char *puc_v, int stride_uv, |
281 unsigned char *bmp, int width_y, int height_y){ | |
282 | |
283 // printf("convert_yuv called %dx%d stride: %d,%d\n",width_y,height_y,stride_y,stride_uv); | |
284 | |
285 opendivx_src[0]=puc_y; | |
286 opendivx_src[1]=puc_u; | |
287 opendivx_src[2]=puc_v; | |
288 | |
289 opendivx_stride[0]=stride_y; | |
290 opendivx_stride[1]=stride_uv; | |
291 opendivx_stride[2]=stride_uv; | |
292 } | |
293 | |
294 //**************************************************************************// | |
295 | |
296 #ifdef SIMULATE_ALSA | |
297 // Simulate ALSA buffering on OSS device :) (for testing...) | |
298 | |
299 #define fake_ALSA_size 32768 | |
300 char fake_ALSA_buffer[fake_ALSA_size]; | |
301 int fake_ALSA_len=0; | |
302 | |
303 void fake_ALSA_write(int audio_fd,char* a_buffer,int len){ | |
304 while(len>0){ | |
305 int x=fake_ALSA_size-fake_ALSA_len; | |
306 if(x>0){ | |
307 if(x>len) x=len; | |
308 memcpy(&fake_ALSA_buffer[fake_ALSA_len],a_buffer,x); | |
309 fake_ALSA_len+=x;len-=x; | |
310 } | |
311 if(fake_ALSA_len>=fake_ALSA_size){ | |
312 write(audio_fd,fake_ALSA_buffer,fake_ALSA_len); | |
313 fake_ALSA_len=0; | |
314 } | |
315 } | |
316 } | |
317 #endif | |
318 //**************************************************************************// | |
319 | |
320 // AVI file header reader/parser/writer: | |
321 #include "aviheader.c" | |
322 #include "aviwrite.c" | |
323 | |
324 // ASF headers: | |
325 #include "asfheader.c" | |
326 #include "demux_asf.c" | |
327 | |
328 // DLL codecs init routines | |
329 #include "dll_init.c" | |
330 | |
112 | 331 // Common FIFO functions, and keyboard/event FIFO code |
332 #include "fifo.c" | |
333 | |
1 | 334 // MPEG video codec process controller: |
112 | 335 #ifdef HAVE_CODECCTRL |
1 | 336 #include "codecctrl.c" |
112 | 337 #endif |
1 | 338 |
339 //**************************************************************************// | |
340 | |
341 static vo_functions_t *video_out=NULL; | |
342 | |
343 static int play_in_bg=0; | |
344 | |
345 void exit_player(char* how){ | |
346 if(how) printf("\nExiting... (%s)\n",how); | |
347 printf("max framesize was %d bytes\n",max_framesize); | |
348 // restore terminal: | |
349 getch2_disable(); | |
112 | 350 #ifdef HAVE_CODECCTRL |
1 | 351 if(child_pid){ |
352 // MPEG | |
353 // printf("\n\n"); | |
354 //send_cmd(data_fifo,0);usleep(50000); // EOF request | |
355 DEBUG_SIG { printf("Sending TERM signal to CTRL...\n");DEBUG_SIGNALS_SLEEP} | |
356 kill(child_pid,SIGTERM); | |
357 usleep(10000); // kill & wait 10ms | |
358 DEBUG_SIG { printf("Closing PIPEs...\n");DEBUG_SIGNALS_SLEEP} | |
359 close(control_fifo); | |
360 close(data_fifo); | |
361 DEBUG_SIG { printf("Freeing shmem...\n");DEBUG_SIGNALS_SLEEP} | |
362 if(videobuffer) shmem_free(videobuffer); | |
363 DEBUG_SIG { printf("Exiting...\n");DEBUG_SIGNALS_SLEEP} | |
112 | 364 } else |
365 #endif | |
366 { | |
1 | 367 // AVI |
368 video_out->uninit(); | |
369 } | |
370 #ifdef USE_XMMP_AUDIO | |
371 if(verbose) printf("XMM: closing audio driver...\n"); | |
372 if(pSound){ | |
373 pSound->Exit( pSound ); | |
374 xmm_Exit( &xmm ); | |
375 } | |
376 #endif | |
377 if(encode_name) avi_fixate(); | |
378 #ifdef HAVE_LIRC | |
379 lirc_mp_cleanup(); | |
380 #endif | |
381 //if(play_in_bg) system("xsetroot -solid \\#000000"); | |
382 exit(1); | |
383 } | |
384 | |
385 static char* current_module=NULL; // for debugging | |
386 | |
387 void exit_sighandler(int x){ | |
388 static int sig_count=0; | |
389 ++sig_count; | |
390 if(sig_count==2) exit(1); | |
391 if(sig_count>2){ | |
392 // can't stop :( | |
393 kill(getpid(),SIGKILL); | |
394 } | |
395 printf("\nMPlayer interrupted by signal %d in module: %s \n",x, | |
396 current_module?current_module:"unknown" | |
397 ); | |
398 exit_player(NULL); | |
399 } | |
400 | |
401 int divx_quality=0; | |
402 | |
147 | 403 int main(int argc,char* argv[], char *envp[]){ |
1 | 404 char* filename=NULL; //"MI2-Trailer.avi"; |
405 int i; | |
406 int seek_to_sec=0; | |
407 int seek_to_byte=0; | |
408 int f; // filedes | |
409 int stream_type; | |
410 stream_t* stream=NULL; | |
411 int file_format=DEMUXER_TYPE_UNKNOWN; | |
190 | 412 int has_audio=1; // audio 0=no 1=mpeg 2=pcm 3=ac3 4=ACM 5=alaw 6=msgsm 7=DShow |
413 int has_video=0; // video 0=no 1=mpeg 2=win32/VfW 3=OpenDivX 4=w32/DShow | |
1 | 414 // |
415 int audio_format=0; // override | |
416 #ifdef ALSA_TIMER | |
417 int alsa=1; | |
418 #else | |
419 int alsa=0; | |
420 #endif | |
421 int audio_buffer_size=-1; | |
422 int audio_id=-1; | |
423 int video_id=-1; | |
424 float default_max_pts_correction=0.01f; | |
425 int delay_corrected=1; | |
426 float force_fps=0; | |
427 float default_fps=25; | |
428 float audio_delay=0; | |
429 int vcd_track=0; | |
430 #ifdef VCD_CACHE | |
431 int vcd_cache_size=128; | |
432 #endif | |
433 int no_index=0; | |
434 #ifdef AVI_SYNC_BPS | |
435 int pts_from_bps=1; | |
436 #else | |
437 int pts_from_bps=0; | |
438 #endif | |
439 char* title="MPlayer"; | |
440 // screen info: | |
441 char* video_driver=NULL; //"mga"; // default | |
442 int fullscreen=0; | |
208
ae0f909ccc7c
Adds code to deal with vidmode selection. -- mgraffam
mgraffam
parents:
190
diff
changeset
|
443 #ifdef HAVE_XF86VM |
ae0f909ccc7c
Adds code to deal with vidmode selection. -- mgraffam
mgraffam
parents:
190
diff
changeset
|
444 int vidmode=0; |
ae0f909ccc7c
Adds code to deal with vidmode selection. -- mgraffam
mgraffam
parents:
190
diff
changeset
|
445 #endif |
1 | 446 int screen_size_x=SCREEN_SIZE_X; |
447 int screen_size_y=SCREEN_SIZE_Y; | |
448 int screen_size_xy=0; | |
449 // movie info: | |
450 int movie_size_x=0; | |
451 int movie_size_y=0; | |
452 int out_fmt=0; | |
453 char *dsp="/dev/dsp"; | |
454 int force_ni=0; | |
178 | 455 char *conffile; |
151 | 456 int conffile_fd; |
212 | 457 char *font_name=NULL; |
215 | 458 float font_factor=0.75; |
258 | 459 char *sub_name=NULL; |
460 float sub_delay=0; | |
461 float sub_fps=0; | |
147 | 462 #include "cfg-mplayer.h" |
1 | 463 |
464 printf("%s",banner_text); | |
465 | |
147 | 466 if (parse_config_file(conf, "/etc/mplayer.conf") < 0) |
467 exit(1); | |
178 | 468 if ((conffile = get_path("")) == NULL) { |
147 | 469 printf("Can't find HOME dir\n"); |
470 } else { | |
178 | 471 mkdir(conffile, 0777); |
472 free(conffile); | |
473 if ((conffile = get_path("config")) == NULL) { | |
474 printf("get_path(\"config\") sziiiivas\n"); | |
475 } else { | |
476 if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) { | |
477 printf("Creating config file: %s\n", conffile); | |
478 write(conffile_fd, default_config, strlen(default_config)); | |
479 close(conffile_fd); | |
480 } | |
481 if (parse_config_file(conf, conffile) < 0) | |
482 exit(1); | |
483 free(conffile); | |
151 | 484 } |
1 | 485 } |
147 | 486 if (parse_command_line(conf, argc, argv, envp, &filename) < 0) |
487 exit(1); | |
1 | 488 |
489 // Many users forget to include command line in bugreports... | |
490 if(verbose){ | |
491 printf("CommandLine:"); | |
492 for(i=1;i<argc;i++)printf(" '%s'",argv[i]); | |
493 printf("\n"); | |
494 } | |
495 | |
496 if(video_driver && strcmp(video_driver,"help")==0){ | |
497 printf("Available video output drivers:\n"); | |
498 i=0; | |
499 while (video_out_drivers[i]) { | |
500 const vo_info_t *info = video_out_drivers[i++]->get_info (); | |
501 printf("\t%s\t%s\n", info->short_name, info->name); | |
502 } | |
503 printf("\n"); | |
504 exit(0); | |
505 } | |
506 | |
212 | 507 // check font |
508 if(font_name){ | |
215 | 509 vo_font=read_font_desc(font_name,font_factor); |
212 | 510 if(!vo_font) printf("Can't load font: %s\n",font_name); |
220 | 511 } else { |
512 // try default: | |
513 vo_font=read_font_desc(get_path("font/font.desc"),font_factor); | |
212 | 514 } |
515 | |
258 | 516 // check .sub |
517 if(sub_name){ | |
518 subtitles=sub_read_file(sub_name); | |
519 if(!subtitles) printf("Can't load subtitles: %s\n",font_name); | |
520 } else { | |
521 // try default: | |
522 subtitles=sub_read_file(get_path("default.sub")); | |
523 } | |
524 | |
525 | |
1 | 526 // check video_out driver name: |
527 if(!video_driver) | |
528 video_out=video_out_drivers[0]; | |
529 else | |
530 for (i=0; video_out_drivers[i] != NULL; i++){ | |
531 const vo_info_t *info = video_out_drivers[i]->get_info (); | |
532 if(strcmp(info->short_name,video_driver) == 0){ | |
533 video_out = video_out_drivers[i];break; | |
534 } | |
535 } | |
536 if(!video_out){ | |
537 printf("Invalid video output driver name: %s\n",video_driver); | |
538 return 0; | |
539 } | |
540 | |
208
ae0f909ccc7c
Adds code to deal with vidmode selection. -- mgraffam
mgraffam
parents:
190
diff
changeset
|
541 #ifdef HAVE_XF86VM |
ae0f909ccc7c
Adds code to deal with vidmode selection. -- mgraffam
mgraffam
parents:
190
diff
changeset
|
542 if (!video_driver) |
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 else if (strcmp(video_driver,"x11")) |
ae0f909ccc7c
Adds code to deal with vidmode selection. -- mgraffam
mgraffam
parents:
190
diff
changeset
|
545 vidmode=0; |
ae0f909ccc7c
Adds code to deal with vidmode selection. -- mgraffam
mgraffam
parents:
190
diff
changeset
|
546 #endif |
1 | 547 |
548 if(!filename){ | |
549 if(vcd_track) filename="/dev/cdrom"; | |
153 | 550 else { |
551 printf("%s",help_text); exit(0); | |
552 } | |
1 | 553 } |
554 | |
555 | |
556 if(vcd_track){ | |
557 //============ Open VideoCD track ============== | |
558 f=open(filename,O_RDONLY); | |
559 if(f<0){ printf("CD-ROM Device '%s' not found!\n",filename);return 1; } | |
560 vcd_read_toc(f); | |
561 if(!vcd_seek_to_track(f,vcd_track)){ printf("Error selecting VCD track!\n");return 1;} | |
562 seek_to_byte+=VCD_SECTOR_DATA*vcd_get_msf(); | |
563 if(verbose) printf("VCD start byte position: 0x%X\n",seek_to_byte); | |
564 stream_type=STREAMTYPE_VCD; | |
565 #ifdef VCD_CACHE | |
566 vcd_cache_init(vcd_cache_size); | |
567 #endif | |
568 } else { | |
569 //============ Open plain FILE ============ | |
570 f=open(filename,O_RDONLY); | |
571 if(f<0){ printf("File not found: '%s'\n",filename);return 1; } | |
572 stream_type=STREAMTYPE_FILE; | |
573 } | |
574 | |
575 stream=new_stream(f,stream_type); | |
576 | |
577 //============ Open & Sync stream and detect file format =============== | |
578 | |
579 if(has_audio==0) audio_id=-2; // do NOT read audio packets... | |
580 | |
581 //=============== Try to open as AVI file: ================= | |
582 stream_reset(stream); | |
583 demuxer=new_demuxer(stream,DEMUXER_TYPE_AVI,audio_id,video_id); | |
584 stream_seek(demuxer->stream,seek_to_byte); | |
585 { //---- RIFF header: | |
586 int id=stream_read_dword_le(demuxer->stream); // "RIFF" | |
587 if(id==mmioFOURCC('R','I','F','F')){ | |
588 stream_read_dword_le(demuxer->stream); //filesize | |
589 id=stream_read_dword_le(demuxer->stream); // "AVI " | |
590 if(id==formtypeAVI){ | |
591 printf("Detected AVI file format!\n"); | |
592 file_format=DEMUXER_TYPE_AVI; | |
593 } | |
594 } | |
595 } | |
596 //=============== Try to open as ASF file: ================= | |
597 if(file_format==DEMUXER_TYPE_UNKNOWN){ | |
598 stream_reset(stream); | |
599 demuxer=new_demuxer(stream,DEMUXER_TYPE_ASF,audio_id,video_id); | |
600 stream_seek(demuxer->stream,seek_to_byte); | |
601 if(asf_check_header()){ | |
602 printf("Detected ASF file format!\n"); | |
603 file_format=DEMUXER_TYPE_ASF; | |
604 // printf("!!! ASF files not (yet) supported !!!\n");exit(1); | |
605 } | |
606 } | |
607 //=============== Try to open as MPEG-PS file: ================= | |
608 if(file_format==DEMUXER_TYPE_UNKNOWN){ | |
609 stream_reset(stream); | |
610 demuxer=new_demuxer(stream,DEMUXER_TYPE_MPEG_PS,audio_id,video_id); | |
611 stream_seek(demuxer->stream,seek_to_byte); | |
612 if(audio_format) demuxer->audio->type=audio_format; // override audio format | |
613 if(ds_fill_buffer(demuxer->video)){ | |
614 printf("Detected MPEG-PS file format!\n"); | |
615 file_format=DEMUXER_TYPE_MPEG_PS; | |
616 } else { | |
617 // some hack to get meaningfull error messages to our unhappy users: | |
618 // if(num_elementary_packets100>16 && | |
619 // abs(num_elementary_packets101-num_elementary_packets100)<8){ | |
620 if(num_elementary_packets100>=2 && num_elementary_packets101>=2 && | |
621 abs(num_elementary_packets101-num_elementary_packets100)<8){ | |
622 file_format=DEMUXER_TYPE_MPEG_ES; // <-- hack is here :) | |
623 } else { | |
624 if(demuxer->synced==2) | |
625 printf("Missing MPEG video stream!? contact the author, it may be a bug :(\n"); | |
626 else | |
627 printf("Not MPEG System Stream format... (maybe Transport Stream?)\n"); | |
628 } | |
629 } | |
630 } | |
631 //=============== Try to open as MPEG-ES file: ================= | |
632 if(file_format==DEMUXER_TYPE_MPEG_ES){ // little hack, see above! | |
633 stream_reset(stream); | |
634 demuxer=new_demuxer(stream,DEMUXER_TYPE_MPEG_ES,audio_id,video_id); | |
635 stream_seek(demuxer->stream,seek_to_byte); | |
636 if(!ds_fill_buffer(demuxer->video)){ | |
637 printf("Invalid MPEG-ES stream??? contact the author, it may be a bug :(\n"); | |
638 file_format=DEMUXER_TYPE_UNKNOWN; | |
639 } else { | |
640 printf("Detected MPEG-ES file format!\n"); | |
641 } | |
642 } | |
643 //=============== Unknown, exiting... =========================== | |
644 if(file_format==DEMUXER_TYPE_UNKNOWN){ | |
645 printf("============= Sorry, this file format not recognized/supported ===============\n"); | |
646 printf("=== If this file is an AVI, ASF or MPEG stream, please contact the author! ===\n"); | |
647 exit(1); | |
648 } | |
649 //====== File format recognized, set up these for compatibility: ========= | |
650 d_audio=demuxer->audio; | |
651 d_video=demuxer->video; | |
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) | |
726 switch(((WAVEFORMATEX *)&avi_header.wf_ext)->wFormatTag){ | |
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){ | |
753 if(!avi_header.audio_codec) avi_header.audio_codec=get_auds_codec_name(); | |
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 } | |
765 default_fps=(float)avi_header.video.dwRate/(float)avi_header.video.dwScale; | |
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) | |
794 switch(((WAVEFORMATEX *)&avi_header.wf_ext)->wFormatTag){ | |
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){ | |
821 if(!avi_header.audio_codec) avi_header.audio_codec=get_auds_codec_name(); | |
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){ | |
862 if(avi_header.video.fccHandler==mmioFOURCC('d', 'v', 'x', '1')) has_video=3; | |
863 if(avi_header.video.fccHandler==mmioFOURCC('d', 'i', 'v', 'x')) has_video=3; | |
864 if(avi_header.bih.biCompression==mmioFOURCC('d', 'v', 'x', '1')) has_video=3; | |
865 if(avi_header.bih.biCompression==mmioFOURCC('d', 'i', 'v', 'x')) has_video=3; | |
58 | 866 // if(avi_header.bih.biCompression==mmioFOURCC('D', 'I', 'V', 'X')) has_video=3; // Gabucino |
1 | 867 } |
868 | |
175 | 869 if(has_video==2){ |
1 | 870 if(!avi_header.video_codec) avi_header.video_codec=get_vids_codec_name(); |
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; | |
908 if(avi_header.audio.fccType) avi_header.bitrate-=avi_header.audio.dwLength; | |
909 if(verbose) printf("AVI video length=%d\n",avi_header.bitrate); | |
910 avi_header.bitrate=((float)avi_header.bitrate/(float)avi_header.video.dwLength) | |
911 *((float)avi_header.video.dwRate/(float)avi_header.video.dwScale); | |
912 // default_fps=(float)avi_header.video.dwRate/(float)avi_header.video.dwScale; | |
913 printf("VIDEO: [%.4s] %dx%d %dbpp %4.2f fps %5.1f kbps (%4.1f kbyte/s)\n", | |
914 &avi_header.bih.biCompression, | |
915 avi_header.bih.biWidth, | |
916 avi_header.bih.biHeight, | |
917 avi_header.bih.biBitCount, | |
918 default_fps, | |
919 avi_header.bitrate*0.008f, | |
920 avi_header.bitrate/1024.0f ); | |
921 | |
922 // display info: | |
923 movie_size_x=avi_header.o_bih.biWidth; | |
924 movie_size_y=abs(avi_header.o_bih.biHeight); | |
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"); | |
942 avi_header.our_out_buffer=NULL; | |
273 | 943 if(DS_VideoDecoder_Open(avi_header.video_codec,avi_header.vids_guid, &avi_header.bih, 0, &avi_header.our_out_buffer)){ |
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; | |
966 if(avi_header.audio.fccType) avi_header.bitrate-=avi_header.audio.dwLength; | |
967 if(verbose) printf("AVI video length=%d\n",avi_header.bitrate); | |
968 avi_header.bitrate=((float)avi_header.bitrate/(float)avi_header.video.dwLength) | |
969 *((float)avi_header.video.dwRate/(float)avi_header.video.dwScale); | |
970 // default_fps=(float)avi_header.video.dwRate/(float)avi_header.video.dwScale; | |
971 printf("VIDEO: [%.4s] %dx%d %dbpp %4.2f fps %5.1f kbps (%4.1f kbyte/s)\n", | |
972 &avi_header.bih.biCompression, | |
973 avi_header.bih.biWidth, | |
974 avi_header.bih.biHeight, | |
975 avi_header.bih.biBitCount, | |
976 default_fps, | |
977 avi_header.bitrate*0.008f, | |
978 avi_header.bitrate/1024.0f ); | |
979 | |
980 // display info: | |
981 movie_size_x=avi_header.bih.biWidth; | |
982 movie_size_y=abs(avi_header.bih.biHeight); | |
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; | |
996 dec_param.x_dim = avi_header.bih.biWidth; | |
997 dec_param.y_dim = avi_header.bih.biHeight; | |
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; | |
1007 if(avi_header.audio.fccType) avi_header.bitrate-=avi_header.audio.dwLength; | |
1008 if(verbose) printf("AVI video length=%d\n",avi_header.bitrate); | |
1009 avi_header.bitrate=((float)avi_header.bitrate/(float)avi_header.video.dwLength) | |
1010 *((float)avi_header.video.dwRate/(float)avi_header.video.dwScale); | |
1011 // default_fps=(float)avi_header.video.dwRate/(float)avi_header.video.dwScale; | |
1012 printf("VIDEO: [%.4s] %dx%d %dbpp %4.2f fps %5.1f kbps (%4.1f kbyte/s)\n", | |
1013 &avi_header.bih.biCompression, | |
1014 avi_header.bih.biWidth, | |
1015 avi_header.bih.biHeight, | |
1016 avi_header.bih.biBitCount, | |
1017 default_fps, | |
1018 avi_header.bitrate*0.008f, | |
1019 avi_header.bitrate/1024.0f ); | |
1020 | |
1021 // display info: | |
1022 // movie_size_x=avi_header.bih.biWidth+(divx_quality?0:64); | |
1023 movie_size_x=avi_header.bih.biWidth; | |
1024 movie_size_y=abs(avi_header.bih.biHeight); | |
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 int pcm_bswap=0; | |
1189 float buffer_delay=0; | |
1190 float frame_correction=0; // A-V timestamp kulonbseg atlagolas | |
1191 int frame_corr_num=0; // | |
1192 float a_frame=0; // Audio | |
1193 float v_frame=0; // Video | |
1194 float time_frame=0; // Timer | |
1195 float a_pts=0; | |
1196 float v_pts=0; | |
1197 float c_total=0; | |
1198 float max_pts_correction=default_max_pts_correction; | |
1199 int eof=0; | |
1200 int force_redraw=0; | |
1201 ac3_frame_t *ac3_frame=NULL; | |
1202 float num_frames=0; // number of frames played | |
1203 //int real_num_frames=0; // number of frames readed | |
1204 double video_time_usage=0; | |
1205 double vout_time_usage=0; | |
1206 double audio_time_usage=0; | |
36 | 1207 int grab_frames=0; |
212 | 1208 char osd_text_buffer[64]; |
220 | 1209 int osd_visible=100; |
1210 int osd_function=OSD_PLAY; | |
1 | 1211 |
1212 #ifdef HAVE_LIRC | |
1213 lirc_mp_setup(); | |
1214 #endif | |
1215 | |
1216 #ifdef USE_TERMCAP | |
1217 load_termcap(NULL); // load key-codes | |
1218 #endif | |
1219 getch2_enable(); | |
1220 | |
1221 //========= Catch terminate signals: ================ | |
1222 // terminate requests: | |
1223 signal(SIGTERM,exit_sighandler); // kill | |
1224 signal(SIGHUP,exit_sighandler); // kill -HUP / xterm closed | |
1225 signal(SIGINT,exit_sighandler); // Interrupt from keyboard | |
1226 signal(SIGQUIT,exit_sighandler); // Quit from keyboard | |
1227 // fatal errors: | |
1228 signal(SIGBUS,exit_sighandler); // bus error | |
1229 signal(SIGSEGV,exit_sighandler); // segfault | |
1230 signal(SIGILL,exit_sighandler); // illegal instruction | |
1231 signal(SIGFPE,exit_sighandler); // floating point exc. | |
1232 signal(SIGABRT,exit_sighandler); // abort() | |
1233 | |
1234 //================ SETUP AUDIO ========================== | |
1235 current_module="setup_audio"; | |
1236 | |
1237 if(has_audio){ | |
1238 | |
1239 if(verbose) printf("Initializing audio codec...\n"); | |
1240 | |
1241 MP3_bps=2; | |
1242 pcm_bswap=0; | |
1243 a_buffer_size=16384; // default size, maybe not enough for Win32/ACM | |
1244 | |
1245 if(has_audio==4){ | |
1246 // Win32 ACM audio codec: | |
1247 if(init_audio_codec()){ | |
1248 MP3_channels=avi_header.wf.nChannels; | |
1249 MP3_samplerate=avi_header.wf.nSamplesPerSec; | |
1250 if(a_buffer_size<avi_header.audio_out_minsize+OUTBURST) | |
1251 a_buffer_size=avi_header.audio_out_minsize+OUTBURST; | |
1252 } else { | |
1253 printf("Could not load/initialize Win32/ACM AUDIO codec (missing DLL file?)\n"); | |
1254 if((((WAVEFORMATEX *)&avi_header.wf_ext)->wFormatTag)==0x55){ | |
1255 printf("Audio format is MP3 -> fallback to internal mp3lib/mpg123\n"); | |
1256 has_audio=1; // fallback to mp3lib | |
1257 } else | |
1258 has_audio=0; // nosound | |
1259 } | |
1260 } | |
1261 | |
190 | 1262 if(has_audio==7){ |
1263 #ifndef USE_DIRECTSHOW | |
1264 printf("Compiled without DirectShow support -> force nosound :(\n"); | |
1265 has_audio=0; | |
1266 #else | |
1267 // Win32 DShow audio codec: | |
1268 WAVEFORMATEX *in_fmt=(WAVEFORMATEX*)&avi_header.wf_ext; | |
1269 avi_header.wf.nChannels=in_fmt->nChannels; | |
1270 avi_header.wf.nSamplesPerSec=in_fmt->nSamplesPerSec; | |
1271 avi_header.wf.nAvgBytesPerSec=2*avi_header.wf.nSamplesPerSec*avi_header.wf.nChannels; | |
1272 avi_header.wf.wFormatTag=WAVE_FORMAT_PCM; | |
1273 avi_header.wf.nBlockAlign=2*in_fmt->nChannels; | |
1274 avi_header.wf.wBitsPerSample=16; | |
1275 avi_header.wf.cbSize=0; | |
1276 | |
1277 if(!DS_AudioDecoder_Open(avi_header.audio_codec,avi_header.auds_guid,in_fmt)){ | |
1278 MP3_channels=avi_header.wf.nChannels; | |
1279 MP3_samplerate=avi_header.wf.nSamplesPerSec; | |
237 | 1280 |
1281 avi_header.audio_in_minsize=2*avi_header.wf.nBlockAlign; | |
1282 if(avi_header.audio_in_minsize<8192) avi_header.audio_in_minsize=8192; | |
1283 a_in_buffer_size=avi_header.audio_in_minsize; | |
1284 a_in_buffer=malloc(a_in_buffer_size); | |
1285 a_in_buffer_len=0; | |
1286 | |
190 | 1287 } else { |
273 | 1288 printf("ERROR: Could not load/initialize Win32/DirctShow AUDIO codec: %s\n",avi_header.audio_codec); |
190 | 1289 if((in_fmt->wFormatTag)==0x55){ |
1290 printf("Audio format is MP3 -> fallback to internal mp3lib/mpg123\n"); | |
1291 has_audio=1; // fallback to mp3lib | |
1292 } else | |
273 | 1293 printf("Audio disabled! Try to upgrade your w32codec.zip package!!!\n"); |
190 | 1294 has_audio=0; // nosound |
1295 } | |
1296 #endif | |
1297 } | |
1298 | |
1299 | |
1 | 1300 // allocate audio out buffer: |
1301 a_buffer=malloc(a_buffer_size); | |
1302 memset(a_buffer,0,a_buffer_size); | |
1303 a_buffer_len=0; | |
1304 | |
92 | 1305 if(has_audio==4){ |
1306 int ret=acm_decode_audio(a_buffer,a_buffer_size); | |
1307 if(ret<0){ | |
1308 printf("ACM error %d -> switching to nosound...\n",ret); | |
1309 has_audio=0; | |
1310 } else { | |
1311 a_buffer_len=ret; | |
1312 printf("ACM decoding test: %d bytes\n",ret); | |
1313 } | |
1314 } | |
1315 | |
1 | 1316 if(has_audio==2){ |
109 | 1317 if(file_format==DEMUXER_TYPE_AVI){ |
1 | 1318 // AVI PCM Audio: |
1319 WAVEFORMATEX *h=(WAVEFORMATEX*)&avi_header.wf_ext; | |
1320 MP3_channels=h->nChannels; | |
1321 MP3_samplerate=h->nSamplesPerSec; | |
1322 MP3_bps=(h->wBitsPerSample+7)/8; | |
1323 } else { | |
1324 // DVD PCM audio: | |
1325 MP3_channels=2; | |
1326 MP3_samplerate=48000; | |
1327 pcm_bswap=1; | |
1328 } | |
1329 } else | |
1330 if(has_audio==3){ | |
1331 // Dolby AC3 audio: | |
1332 ac3_config.fill_buffer_callback = ac3_fill_buffer; | |
1333 ac3_config.num_output_ch = 2; | |
1334 ac3_config.flags = 0; | |
1335 #ifdef HAVE_MMX | |
1336 ac3_config.flags |= AC3_MMX_ENABLE; | |
1337 #endif | |
1338 #ifdef HAVE_3DNOW | |
1339 ac3_config.flags |= AC3_3DNOW_ENABLE; | |
1340 #endif | |
1341 ac3_init(); | |
1342 ac3_frame = ac3_decode_frame(); | |
1343 if(ac3_frame){ | |
1344 MP3_samplerate=ac3_frame->sampling_rate; | |
1345 MP3_channels=2; | |
1346 } else has_audio=0; // bad frame -> disable audio | |
1347 } else | |
1348 if(has_audio==5){ | |
1349 // aLaw audio codec: | |
1350 Gen_aLaw_2_Signed(); // init table | |
1351 MP3_channels=((WAVEFORMATEX*)&avi_header.wf_ext)->nChannels; | |
1352 MP3_samplerate=((WAVEFORMATEX*)&avi_header.wf_ext)->nSamplesPerSec; | |
1353 } else | |
1354 if(has_audio==6){ | |
1355 // MS-GSM audio codec: | |
1356 GSM_Init(); | |
1357 MP3_channels=((WAVEFORMATEX*)&avi_header.wf_ext)->nChannels; | |
1358 MP3_samplerate=((WAVEFORMATEX*)&avi_header.wf_ext)->nSamplesPerSec; | |
1359 } | |
1360 // must be here for Win32->mp3lib fallbacks | |
1361 if(has_audio==1){ | |
1362 // MPEG Audio: | |
1363 MP3_Init(); | |
1364 MP3_samplerate=MP3_channels=0; | |
1365 // printf("[\n"); | |
1366 a_buffer_len=MP3_DecodeFrame(a_buffer,-1); | |
1367 // printf("]\n"); | |
1368 MP3_channels=2; // hack | |
1369 } | |
1370 | |
1371 if(verbose) printf("Audio: type: %d samplerate=%d channels=%d bps=%d\n",has_audio,MP3_samplerate,MP3_channels,MP3_bps); | |
1372 | |
1373 if(!MP3_channels || !MP3_samplerate){ | |
1374 printf("Unknown/missing audio format, using nosound\n"); | |
1375 has_audio=0; | |
1376 } | |
1377 | |
1378 if(has_audio){ | |
1379 #ifdef USE_XMMP_AUDIO | |
1380 xmm_Init( &xmm ); | |
1381 xmm.cSound = (XMM_PluginSound *)xmm_PluginRegister( XMMP_AUDIO_DRIVER ); | |
1382 if( xmm.cSound ){ | |
1383 pSound = xmm.cSound->Init( &xmm ); | |
1384 if( pSound && pSound->Start( pSound, MP3_samplerate, MP3_channels, | |
1385 ( MP3_bps == 2 ) ? XMM_SOUND_FMT_S16LE : XMM_SOUND_FMT_U8 )){ | |
1386 printf("XMM: audio setup ok\n"); | |
1387 } else { | |
1388 has_audio=0; | |
1389 } | |
1390 } else has_audio=0; | |
1391 #else | |
1392 audio_fd=open(dsp, O_WRONLY); | |
1393 if(audio_fd<0){ | |
1394 printf("Can't open audio device %s -> nosound\n",dsp); | |
1395 has_audio=0; | |
1396 } | |
1397 #endif | |
1398 } | |
1399 | |
1400 if(has_audio){ | |
1401 #ifdef USE_XMMP_AUDIO | |
1402 if(audio_buffer_size==-1){ | |
1403 // Measuring buffer size: | |
1404 buffer_delay=pSound->QueryDelay(pSound, 0); | |
1405 } else { | |
1406 // -abs commandline option | |
1407 buffer_delay=audio_buffer_size/(float)(MP3_samplerate*MP3_channels*MP3_bps); | |
1408 } | |
1409 #else | |
1410 int r; | |
1411 r=(MP3_bps==2)?AFMT_S16_LE:AFMT_U8;ioctl (audio_fd, SNDCTL_DSP_SETFMT, &r); | |
1412 r=MP3_channels-1; ioctl (audio_fd, SNDCTL_DSP_STEREO, &r); | |
1413 r=MP3_samplerate; if(ioctl (audio_fd, SNDCTL_DSP_SPEED, &r)==-1) | |
1414 printf("audio_setup: your card doesn't support %d Hz samplerate\n",r); | |
1415 | |
103 | 1416 #if 0 |
1417 // r = (64 << 16) + 1024; | |
1418 r = (65536 << 16) + 512; | |
1419 if(ioctl (audio_fd, SNDCTL_DSP_SETFRAGMENT, &r)==-1) | |
1420 printf("audio_setup: your card doesn't support setting fragments\n",r); | |
1421 #endif | |
1422 | |
1 | 1423 if(audio_buffer_size==-1){ |
1424 // Measuring buffer size: | |
1425 audio_buffer_size=0; | |
1426 #ifdef HAVE_AUDIO_SELECT | |
1427 while(audio_buffer_size<0x40000){ | |
1428 fd_set rfds; | |
1429 struct timeval tv; | |
1430 FD_ZERO(&rfds); FD_SET(audio_fd,&rfds); | |
1431 tv.tv_sec=0; tv.tv_usec = 0; | |
1432 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) break; | |
1433 write(audio_fd,&a_buffer[a_buffer_len],OUTBURST); | |
1434 audio_buffer_size+=OUTBURST; | |
1435 } | |
1436 if(audio_buffer_size==0){ | |
1437 printf("\n *** Your audio driver DOES NOT support select() ***\n"); | |
1438 printf("Recompile mplayer with #undef HAVE_AUDIO_SELECT in config.h !\n\n"); | |
1439 exit_player("audio_init"); | |
1440 } | |
1441 #endif | |
1442 } | |
1443 buffer_delay=audio_buffer_size/(float)(MP3_samplerate*MP3_channels*MP3_bps); | |
1444 #endif | |
1445 a_frame=-(buffer_delay); | |
1446 printf("Audio buffer size: %d bytes, delay: %5.3fs\n",audio_buffer_size,buffer_delay); | |
1447 } | |
1448 | |
1449 } // has_audio | |
1450 | |
1451 if(has_audio){ | |
1452 printf("Audio: type: %d samplerate: %d channels: %d bps: %d\n",has_audio,MP3_samplerate,MP3_channels,MP3_bps); | |
1453 } else { | |
1454 printf("Audio: no sound\n"); | |
1455 if(verbose) printf("Freeing %d unused audio chunks\n",d_audio->packs); | |
1456 ds_free_packs(d_audio); // free buffered chunks | |
1457 d_audio->id=-2; // do not read audio chunks | |
1458 if(a_buffer) free(a_buffer); | |
103 | 1459 alsa=1; MP3_samplerate=76800;MP3_bps=MP3_channels=2; // fake, required for timer |
1 | 1460 } |
1461 | |
1462 current_module=NULL; | |
1463 | |
1464 //==================== START PLAYING ======================= | |
1465 | |
109 | 1466 if(file_format==DEMUXER_TYPE_AVI){ |
1 | 1467 a_pts=d_audio->pts-(buffer_delay+audio_delay); |
1468 audio_delay-=(float)(avi_header.audio.dwInitialFrames-avi_header.video.dwInitialFrames)/default_fps; | |
1469 // audio_delay-=(float)(avi_header.audio.dwInitialFrames-avi_header.video.dwInitialFrames)/default_fps; | |
1470 printf("AVI Initial frame delay: %5.3f\n",(float)(avi_header.audio.dwInitialFrames-avi_header.video.dwInitialFrames)/default_fps); | |
1471 printf("v: audio_delay=%5.3f buffer_delay=%5.3f a_pts=%5.3f a_frame=%5.3f\n", | |
1472 audio_delay,buffer_delay,a_pts,a_frame); | |
1473 printf("START: a_pts=%5.3f v_pts=%5.3f \n",d_audio->pts,d_video->pts); | |
1474 delay_corrected=0; // has to correct PTS diffs | |
1475 d_video->pts=0;d_audio->pts=0; // PTS is outdated now! | |
1476 } | |
1477 if(force_fps) default_fps=force_fps; | |
1478 | |
1479 printf("Start playing...\n");fflush(stdout); | |
1480 | |
92 | 1481 #if 0 |
1482 // ACM debug code | |
1 | 1483 { DWORD srcsize=0; |
1484 DWORD dstsize=16384*8; | |
1485 int ret=acmStreamSize(avi_header.srcstream,dstsize, &srcsize, ACM_STREAMSIZEF_DESTINATION); | |
1486 printf("acmStreamSize %d -> %d (err=%d)\n",dstsize,srcsize,ret); | |
1487 } | |
92 | 1488 #endif |
1 | 1489 |
1490 InitTimer(); | |
1491 | |
1492 while(!eof){ | |
1493 | |
1494 /*========================== PLAY AUDIO ============================*/ | |
1495 | |
1496 while(has_audio){ | |
1497 unsigned int t=GetTimer(); | |
1498 current_module="decode_audio"; // Enter AUDIO decoder module | |
1499 // Update buffer if needed | |
1500 while(a_buffer_len<OUTBURST && !d_audio->eof){ | |
1501 switch(has_audio){ | |
1502 case 1: // MPEG layer 2 or 3 | |
1503 a_buffer_len+=MP3_DecodeFrame(&a_buffer[a_buffer_len],-1); | |
1504 MP3_channels=2; // hack | |
1505 break; | |
1506 case 2: // PCM | |
1507 { int i=demux_read_data(d_audio,&a_buffer[a_buffer_len],OUTBURST); | |
1508 if(pcm_bswap){ | |
1509 int j; | |
1510 if(i&3){ printf("Warning! pcm_audio_size&3 !=0 (%d)\n",i);i&=~3; } | |
1511 for(j=0;j<i;j+=2){ | |
1512 char x=a_buffer[a_buffer_len+j]; | |
1513 a_buffer[a_buffer_len+j]=a_buffer[a_buffer_len+j+1]; | |
1514 a_buffer[a_buffer_len+j+1]=x; | |
1515 } | |
1516 } | |
1517 a_buffer_len+=i; | |
1518 break; | |
1519 } | |
1520 case 5: // aLaw decoder | |
1521 { int l=demux_read_data(d_audio,&a_buffer[a_buffer_len],OUTBURST/2); | |
1522 unsigned short *d=(unsigned short *) &a_buffer[a_buffer_len]; | |
1523 unsigned char *s=&a_buffer[a_buffer_len]; | |
1524 a_buffer_len+=2*l; | |
1525 while(l>0){ | |
1526 --l; | |
1527 d[l]=xa_alaw_2_sign[s[l]]; | |
1528 } | |
1529 break; | |
1530 } | |
1531 case 6: // MS-GSM decoder | |
1532 { unsigned char buf[65]; // 65 bytes / frame | |
92 | 1533 while(a_buffer_len<OUTBURST){ |
1534 if(demux_read_data(d_audio,buf,65)!=65) break; // EOF | |
1535 XA_MSGSM_Decoder(buf,(unsigned short *) &a_buffer[a_buffer_len]); // decodes 65 byte -> 320 short | |
1 | 1536 // XA_GSM_Decoder(buf,(unsigned short *) &a_buffer[a_buffer_len]); // decodes 33 byte -> 160 short |
92 | 1537 a_buffer_len+=2*320; |
1538 } | |
1 | 1539 break; |
1540 } | |
1541 case 3: // AC3 decoder | |
1542 //printf("{1:%d}",avi_header.idx_pos);fflush(stdout); | |
1543 if(!ac3_frame) ac3_frame=ac3_decode_frame(); | |
1544 //printf("{2:%d}",avi_header.idx_pos);fflush(stdout); | |
1545 if(ac3_frame){ | |
1546 memcpy(&a_buffer[a_buffer_len],ac3_frame->audio_data,256 * 6 *MP3_channels*MP3_bps); | |
1547 a_buffer_len+=256 * 6 *MP3_channels*MP3_bps; | |
1548 ac3_frame=NULL; | |
1549 } | |
1550 //printf("{3:%d}",avi_header.idx_pos);fflush(stdout); | |
1551 break; | |
1552 case 4: | |
92 | 1553 { int ret=acm_decode_audio(&a_buffer[a_buffer_len],a_buffer_size-a_buffer_len); |
1554 if(ret>0) a_buffer_len+=ret; | |
1 | 1555 break; |
1556 } | |
240 | 1557 #ifdef USE_DIRECTSHOW |
237 | 1558 case 7: // DirectShow |
1559 { int ret; | |
1560 int len=a_buffer_size-a_buffer_len; | |
1561 int size_in=0; | |
1562 int size_out=0; | |
1563 int srcsize=DS_AudioDecoder_GetSrcSize(len); | |
1564 if(verbose>2)printf("DShow says: srcsize=%d (buffsize=%d) out_size=%d\n",srcsize,a_in_buffer_size,len); | |
1565 if(srcsize>a_in_buffer_size) srcsize=a_in_buffer_size; // !!!!!! | |
1566 if(a_in_buffer_len<srcsize){ | |
1567 a_in_buffer_len+= | |
1568 demux_read_data(d_audio,&a_in_buffer[a_in_buffer_len], | |
1569 srcsize-a_in_buffer_len); | |
1570 } | |
1571 DS_AudioDecoder_Convert(a_in_buffer,a_in_buffer_len, | |
1572 &a_buffer[a_buffer_len],len, &size_in,&size_out); | |
1573 if(verbose>2)printf("DShow: audio %d -> %d converted (in_buf_len=%d of %d)\n",size_in,size_out,a_in_buffer_len,a_in_buffer_size); | |
1574 if(size_in>=a_in_buffer_len){ | |
1575 a_in_buffer_len=0; | |
1576 } else { | |
1577 a_in_buffer_len-=size_in; | |
1578 memcpy(a_in_buffer,&a_in_buffer[size_in],a_in_buffer_len); | |
1579 } | |
1580 a_buffer_len+=size_out; | |
1581 | |
1582 break; | |
1583 } | |
240 | 1584 #endif |
1 | 1585 } |
1586 } | |
1587 current_module=NULL; // Leave AUDIO decoder module | |
1588 t=GetTimer()-t; | |
1589 audio_time_usage+=t*0.000001; | |
1590 | |
1591 // Play sound from the buffer: | |
1592 | |
1593 if(a_buffer_len>=OUTBURST){ // if not EOF | |
1594 #ifdef USE_XMMP_AUDIO | |
1595 pSound->Write( pSound, a_buffer, OUTBURST ); | |
1596 #else | |
1597 #ifdef SIMULATE_ALSA | |
1598 fake_ALSA_write(audio_fd,a_buffer,OUTBURST); // for testing purposes | |
1599 #else | |
1600 write(audio_fd,a_buffer,OUTBURST); | |
1601 #endif | |
1602 #endif | |
1603 a_buffer_len-=OUTBURST; | |
1604 memcpy(a_buffer,&a_buffer[OUTBURST],a_buffer_len); | |
1605 #ifndef USE_XMMP_AUDIO | |
1606 #ifndef SIMULATE_ALSA | |
1607 // check buffer | |
1608 #ifdef HAVE_AUDIO_SELECT | |
1609 { fd_set rfds; | |
1610 struct timeval tv; | |
1611 FD_ZERO(&rfds); | |
1612 FD_SET(audio_fd, &rfds); | |
1613 tv.tv_sec = 0; | |
1614 tv.tv_usec = 0; | |
1615 if(select(audio_fd+1, NULL, &rfds, NULL, &tv)){ | |
1616 a_frame+=OUTBURST/(float)(MP3_samplerate*MP3_channels*MP3_bps); | |
1617 a_pts+=OUTBURST/(float)(MP3_samplerate*MP3_channels*MP3_bps); | |
1618 // printf("Filling audio buffer...\n"); | |
1619 continue; | |
1620 // } else { | |
1621 // printf("audio buffer full...\n"); | |
1622 } | |
1623 } | |
1624 #endif | |
1625 #endif | |
1626 #endif | |
1627 } | |
1628 | |
1629 break; | |
1630 } // if(has_audio) | |
1631 | |
1632 /*========================== UPDATE TIMERS ============================*/ | |
1633 | |
1634 a_frame+=OUTBURST/(float)(MP3_samplerate*MP3_channels*MP3_bps); | |
1635 a_pts+=OUTBURST/(float)(MP3_samplerate*MP3_channels*MP3_bps); | |
1636 | |
1637 if(alsa){ | |
1638 // Use system timer for sync, not audio card/driver | |
1639 time_frame+=OUTBURST/(float)(MP3_samplerate*MP3_channels*MP3_bps); | |
1640 time_frame-=GetRelativeTime(); | |
1641 // printf("time_frame=%5.3f\n",time_frame); | |
1642 if(time_frame<-0.1 || time_frame>0.1){ | |
1643 time_frame=0; | |
1644 } else { | |
1645 // if(time_frame>0.01) usleep(1000000*(time_frame-0.01)); // sleeping | |
103 | 1646 // if(time_frame>0.019) usleep(1000000*(time_frame-0.019)); // sleeping |
1647 // if(time_frame>0.001) usleep(1000000*(time_frame)); // sleeping | |
1648 // if(time_frame>0.02) usleep(1000000*(time_frame)); // sleeping if >20ms | |
1649 while(time_frame>0.007){ | |
1650 // printf("TIMER %8.3f -> ",time_frame*1000); | |
1651 // if(time_frame>0.021) | |
1652 // usleep(time_frame-0.12); | |
1653 // else | |
1654 usleep(1000); | |
1655 time_frame-=GetRelativeTime(); | |
1656 // printf("%8.3f \n",time_frame*1000); | |
1657 } | |
1658 } | |
1659 | |
1 | 1660 } |
1661 | |
1662 | |
1663 /*========================== PLAY VIDEO ============================*/ | |
1664 | |
1665 if(1) | |
1666 while(v_frame<a_frame || force_redraw){ | |
1667 | |
1668 current_module="decode_video"; | |
1669 | |
1670 //-------------------- Decode a frame: ----------------------- | |
1671 switch(has_video){ | |
1672 case 3: { | |
1673 // OpenDivX | |
1674 unsigned int t=GetTimer(); | |
1675 unsigned int t2; | |
1676 DEC_FRAME dec_frame; | |
1677 char* start=NULL; | |
1678 int in_size=ds_get_packet(d_video,&start); | |
1679 if(in_size<0){ eof=1;break;} | |
1680 if(in_size>max_framesize) max_framesize=in_size; | |
1681 // let's decode | |
1682 dec_frame.length = in_size; | |
1683 dec_frame.bitstream = start; | |
1684 //dec_frame.bmp = video_out; | |
1685 dec_frame.render_flag = 1; | |
1686 decore(0x123, 0, &dec_frame, NULL); | |
1687 t2=GetTimer();t=t2-t;video_time_usage+=t*0.000001f; | |
1688 | |
1689 if(opendivx_src[0]){ | |
1690 video_out->draw_slice(opendivx_src,opendivx_stride, | |
1691 movie_size_x,movie_size_y,0,0); | |
117 | 1692 // video_out->flip_page(); |
1 | 1693 opendivx_src[0]=NULL; |
1694 } | |
1695 | |
1696 t2=GetTimer()-t2;vout_time_usage+=t2*0.000001f; | |
1697 | |
1698 ++num_frames; | |
1699 v_frame+=1.0f/default_fps; //(float)avi_header.video.dwScale/(float)avi_header.video.dwRate; | |
1700 | |
1701 break; | |
1702 } | |
175 | 1703 #ifdef USE_DIRECTSHOW |
1704 case 4: { // W32/DirectShow | |
1705 char* start=NULL; | |
1706 unsigned int t=GetTimer(); | |
1707 unsigned int t2; | |
1708 float pts1=d_video->pts; | |
1709 int in_size=ds_get_packet(d_video,&start); | |
1710 float pts2=d_video->pts; | |
1711 if(in_size<0){ eof=1;break;} | |
1712 if(in_size>max_framesize) max_framesize=in_size; | |
1713 | |
1714 // printf("frame len = %5.4f\n",pts2-pts1); | |
1715 | |
1716 DS_VideoDecoder_DecodeFrame(start, in_size, 0, 1); | |
1717 | |
1718 t2=GetTimer();t=t2-t;video_time_usage+=t*0.000001f; | |
1719 video_out->draw_frame((uint8_t **)&avi_header.our_out_buffer); | |
1720 // video_out->flip_page(); | |
1721 t2=GetTimer()-t2;vout_time_usage+=t2*0.000001f; | |
1722 | |
1723 ++num_frames; | |
1724 | |
1725 if(file_format==DEMUXER_TYPE_ASF){ | |
1726 float d=pts2-pts1; | |
1727 if(d>0 && d<0.2) v_frame+=d; | |
1728 } else | |
1729 v_frame+=1.0f/default_fps; //(float)avi_header.video.dwScale/(float)avi_header.video.dwRate; | |
1730 //v_pts+=1.0f/default_fps; //(float)avi_header.video.dwScale/(float)avi_header.video.dwRate; | |
1731 | |
1732 break; | |
1733 } | |
1734 #endif | |
1 | 1735 case 2: { |
1736 HRESULT ret; | |
1737 char* start=NULL; | |
1738 unsigned int t=GetTimer(); | |
1739 unsigned int t2; | |
1740 float pts1=d_video->pts; | |
1741 int in_size=ds_get_packet(d_video,&start); | |
1742 float pts2=d_video->pts; | |
1743 if(in_size<0){ eof=1;break;} | |
1744 if(in_size>max_framesize) max_framesize=in_size; | |
1745 | |
1746 // printf("frame len = %5.4f\n",pts2-pts1); | |
1747 | |
1748 //if(in_size>0){ | |
1749 avi_header.bih.biSizeImage = in_size; | |
1750 ret = ICDecompress(avi_header.hic, ICDECOMPRESS_NOTKEYFRAME, | |
1751 // ret = ICDecompress(avi_header.hic, ICDECOMPRESS_NOTKEYFRAME|(ICDECOMPRESS_HURRYUP|ICDECOMPRESS_PREROL), | |
1752 &avi_header.bih, start, | |
1753 &avi_header.o_bih, avi_header.our_out_buffer); | |
1754 if(ret){ printf("Error decompressing frame, err=%d\n",ret);break; } | |
1755 //} | |
1756 | |
1757 t2=GetTimer();t=t2-t;video_time_usage+=t*0.000001f; | |
1758 video_out->draw_frame((uint8_t **)&avi_header.our_out_buffer); | |
117 | 1759 // video_out->flip_page(); |
1 | 1760 t2=GetTimer()-t2;vout_time_usage+=t2*0.000001f; |
1761 | |
1762 ++num_frames; | |
1763 | |
1764 if(file_format==DEMUXER_TYPE_ASF){ | |
1765 float d=pts2-pts1; | |
1766 if(d>0 && d<0.2) v_frame+=d; | |
1767 } else | |
1768 v_frame+=1.0f/default_fps; //(float)avi_header.video.dwScale/(float)avi_header.video.dwRate; | |
1769 //v_pts+=1.0f/default_fps; //(float)avi_header.video.dwScale/(float)avi_header.video.dwRate; | |
1770 | |
1771 break; | |
1772 } | |
1773 case 1: { | |
111 | 1774 #ifndef HAVE_CODECCTRL |
1775 | |
1776 int in_frame=0; | |
1777 videobuf_len=0; | |
1778 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){ | |
1779 int i=sync_video_packet(d_video); | |
1780 if(in_frame){ | |
1781 if(i<0x101 || i>=0x1B0){ // not slice code -> end of frame | |
1782 // send END OF FRAME code: | |
1783 #if 1 | |
1784 videobuffer[videobuf_len+0]=0; | |
1785 videobuffer[videobuf_len+1]=0; | |
1786 videobuffer[videobuf_len+2]=1; | |
1787 videobuffer[videobuf_len+3]=0xFF; | |
1788 videobuf_len+=4; | |
1789 #endif | |
1790 if(!i) eof=1; // EOF | |
1791 break; | |
1792 } | |
1793 } else { | |
1794 //if(i==0x100) in_frame=1; // picture startcode | |
1795 if(i>=0x101 && i<0x1B0) in_frame=1; // picture startcode | |
1796 else if(!i){ eof=1; break;} // EOF | |
1797 } | |
1798 if(grab_frames==2 && (i==0x1B3 || i==0x1B8)) grab_frames=1; | |
1799 if(!read_video_packet(d_video)){ eof=1; break;} // EOF | |
1800 //printf("read packet 0x%X, len=%d\n",i,videobuf_len); | |
1801 } | |
1802 | |
1803 if(videobuf_len>max_framesize) max_framesize=videobuf_len; // debug | |
1804 //printf("--- SEND %d bytes\n",videobuf_len); | |
1805 if(grab_frames==1){ | |
1806 FILE *f=fopen("grab.mpg","ab"); | |
1807 fwrite(videobuffer,videobuf_len-4,1,f); | |
1808 fclose(f); | |
1809 } | |
1810 ++dbg_es_sent; | |
1811 //if(videobuf_len>4) | |
1812 //my_write(data_fifo,(char*) &videobuf_len,4); | |
1813 | |
1814 { int t=0; | |
1815 int x; | |
1816 float l; | |
1817 t-=GetTimer(); | |
1818 mpeg2_decode_data(video_out, videobuffer, videobuffer+videobuf_len); | |
1819 t+=GetTimer(); | |
1820 //*** CMD=0 : Frame completed *** | |
1821 //send_cmd(control_fifo2,0); // FRAME_COMPLETED command | |
1822 x=frameratecode2framerate[picture->frame_rate_code]; //fps | |
1823 ++dbg_es_rcvd; | |
1824 l=(100+picture->repeat_count)*0.01f; | |
1825 num_frames+=l; | |
1826 picture->repeat_count=0; | |
1827 video_time_usage+=t*0.000001; | |
1828 if(x && !force_fps) default_fps=x*0.0001f; | |
1829 if(!force_redraw){ | |
1830 // increase video timers: | |
1831 v_frame+=l/default_fps; | |
1832 v_pts+=l/default_fps; | |
1833 } | |
1834 } | |
1835 //if(eof) break; | |
1836 | |
1837 #else | |
1 | 1838 while(1){ |
1839 int x; | |
1840 while(1){ | |
1841 x=-1; // paranoia | |
1842 if(4==read(control_fifo,&x,4)) break; // status/command | |
1843 usleep(5000); // do not eat 100% CPU (waiting for codec restart) | |
1844 } | |
1845 if(x==0x3030303){ | |
1846 //*** CMD=3030303 : Video packet requested *** | |
1847 // read a single compressed frame: | |
1848 int in_frame=0; | |
1849 videobuf_len=0; | |
1850 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){ | |
1851 int i=sync_video_packet(d_video); | |
1852 if(in_frame){ | |
1853 if(i<0x101 || i>=0x1B0){ // not slice code -> end of frame | |
1854 // send END OF FRAME code: | |
1855 #if 1 | |
1856 videobuffer[videobuf_len+0]=0; | |
1857 videobuffer[videobuf_len+1]=0; | |
1858 videobuffer[videobuf_len+2]=1; | |
1859 videobuffer[videobuf_len+3]=0xFF; | |
1860 videobuf_len+=4; | |
1861 #endif | |
1862 if(!i) eof=1; // EOF | |
1863 break; | |
1864 } | |
1865 } else { | |
1866 //if(i==0x100) in_frame=1; // picture startcode | |
1867 if(i>=0x101 && i<0x1B0) in_frame=1; // picture startcode | |
1868 else if(!i){ eof=1; break;} // EOF | |
1869 } | |
36 | 1870 if(grab_frames==2 && (i==0x1B3 || i==0x1B8)) grab_frames=1; |
1 | 1871 if(!read_video_packet(d_video)){ eof=1; break;} // EOF |
1872 //printf("read packet 0x%X, len=%d\n",i,videobuf_len); | |
1873 } | |
1874 if(videobuf_len>max_framesize) max_framesize=videobuf_len; // debug | |
1875 //printf("--- SEND %d bytes\n",videobuf_len); | |
36 | 1876 if(grab_frames==1){ |
1877 FILE *f=fopen("grab.mpg","ab"); | |
1878 fwrite(videobuffer,videobuf_len-4,1,f); | |
1879 fclose(f); | |
1880 } | |
1 | 1881 ++dbg_es_sent; |
1882 //if(videobuf_len>4) | |
1883 my_write(data_fifo,(char*) &videobuf_len,4); | |
1884 if(eof) break; | |
1885 } else | |
1886 if(x==0){ | |
1887 //*** CMD=0 : Frame completed *** | |
1888 int l=100; | |
1889 int t=0; | |
1890 read(control_fifo,&x,4); // FPS | |
1891 read(control_fifo,&l,4); // Length*100 | |
1892 read(control_fifo,&t,4); // Time*1000000 | |
1893 //printf("+++ FRAME COMPLETED fps=%d len=%d time=%d\n",x,l,t); | |
1894 ++dbg_es_rcvd; | |
1895 num_frames+=l/100.0f; | |
1896 video_time_usage+=t*0.000001; | |
1897 if(x && !force_fps) default_fps=x*0.0001f; | |
1898 if(!force_redraw){ | |
1899 // increase video timers: | |
1900 v_frame+=l*0.01f/default_fps; | |
1901 v_pts+=l*0.01f/default_fps; | |
1902 } | |
1903 break; // frame OK. | |
1904 } else | |
1905 if(x==0x22222222){ | |
1906 //*** CMD=22222222 : codec reset/restart *** | |
1907 read(control_fifo,&codec_pid,4); // PID | |
1908 printf("\nVideo codec started... (pid %d)\n",codec_pid); | |
1909 send_cmd(data_fifo,0x22222222); // send response (syncword) | |
1910 dbg_es_sent=dbg_es_rcvd=0; | |
1911 //printf(" [ReSync-VIDEO] \n"); | |
1912 while(1){ | |
1913 int id=sync_video_packet(d_video); | |
1914 if(id==0x100 || id>=0x1B0) break; // header chunk | |
1915 if(!id || !skip_video_packet(d_video)){ eof=1; break;} // EOF | |
1916 } | |
1917 if(eof) break; | |
1918 max_pts_correction=0.2; | |
1919 } else | |
1920 printf("invalid cmd: 0x%X\n",x); | |
1921 } | |
111 | 1922 #endif |
1 | 1923 break; |
1924 } | |
1925 } // switch | |
1926 //------------------------ frame decoded. -------------------- | |
1927 | |
117 | 1928 current_module="flip_page"; |
1929 | |
1930 video_out->flip_page(); | |
1931 | |
1 | 1932 current_module=NULL; |
1933 | |
1934 if(eof) break; | |
220 | 1935 if(force_redraw){ |
1936 --force_redraw; | |
1937 if(!force_redraw) osd_function=OSD_PLAY; | |
1938 } | |
1 | 1939 |
1940 // 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); | |
1941 // fflush(stdout); | |
1942 | |
1943 #if 1 | |
1944 /*================ A-V TIMESTAMP CORRECTION: =========================*/ | |
1945 if(has_audio){ | |
109 | 1946 if(pts_from_bps && (file_format==DEMUXER_TYPE_AVI)){ |
1 | 1947 // a_pts=(float)ds_tell(d_audio)/((WAVEFORMATEX*)avi_header.wf_ext)->nAvgBytesPerSec-(buffer_delay+audio_delay); |
1948 a_pts=(float)ds_tell(d_audio)/((WAVEFORMATEX*)avi_header.wf_ext)->nAvgBytesPerSec-(buffer_delay); | |
1949 delay_corrected=1; // hack | |
1950 } else | |
1951 if(d_audio->pts){ | |
1952 // printf("\n=== APTS a_pts=%5.3f v_pts=%5.3f === \n",d_audio->pts,d_video->pts); | |
1953 #if 1 | |
1954 if(!delay_corrected){ | |
1955 float x=d_audio->pts-d_video->pts-(buffer_delay+audio_delay); | |
1956 float y=-(buffer_delay+audio_delay); | |
1957 printf("Initial PTS delay: %5.3f sec (calculated: %5.3f)\n",x,y); | |
1958 audio_delay+=x; | |
1959 //a_pts-=x; | |
1960 delay_corrected=1; | |
1961 printf("v: audio_delay=%5.3f buffer_delay=%5.3f a.pts=%5.3f v.pts=%5.3f\n", | |
1962 audio_delay,buffer_delay,d_audio->pts,d_video->pts); | |
1963 } | |
1964 #endif | |
1965 a_pts=d_audio->pts-(buffer_delay+audio_delay); | |
1966 d_audio->pts=0; | |
1967 } | |
1968 if(d_video->pts) v_pts=d_video->pts; | |
1969 if(frame_corr_num==5){ | |
1970 float x=(frame_correction/5.0f); | |
1971 if(!delay_corrected){ | |
1972 #if 0 | |
1973 printf("Initial PTS delay: %5.3f sec\n",x); | |
1974 delay_corrected=1; | |
1975 audio_delay+=x; | |
1976 a_pts-=x; | |
1977 #endif | |
1978 } else | |
1979 { | |
1980 printf("A:%6.1f V:%6.1f A-V:%7.3f",a_pts,v_pts,x); | |
1981 x*=0.5f; | |
1982 if(x<-max_pts_correction) x=-max_pts_correction; else | |
1983 if(x> max_pts_correction) x= max_pts_correction; | |
1984 max_pts_correction=default_max_pts_correction; | |
1985 a_frame+=x; c_total+=x; | |
1986 #if 0 | |
1987 printf(" ct:%7.3f a=%d v=%d \r",c_total, | |
1988 d_audio->pos,d_video->pos); | |
1989 #else | |
1990 printf(" ct:%7.3f %3d %2d%% %2d%% %3.1f%% %d \r",c_total, | |
1991 (int)num_frames, | |
1992 (v_frame>0.5)?(int)(100.0*video_time_usage/(double)v_frame):0, | |
1993 (v_frame>0.5)?(int)(100.0*vout_time_usage/(double)v_frame):0, | |
1994 (v_frame>0.5)?(100.0*audio_time_usage/(double)v_frame):0, | |
1995 dbg_es_sent-dbg_es_rcvd | |
1996 ); | |
1997 #endif | |
1998 fflush(stdout); | |
1999 // printf("\n"); | |
2000 } | |
2001 //force_fps+=1*force_fps*x; | |
2002 // 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); | |
2003 frame_corr_num=0; frame_correction=0; | |
2004 } | |
2005 if(frame_corr_num>=0) frame_correction+=a_pts-v_pts; | |
2006 } else { | |
2007 // No audio: | |
2008 if(d_video->pts) v_pts=d_video->pts; | |
2009 if(frame_corr_num==5){ | |
2010 // printf("A: --- V:%6.1f \r",v_pts); | |
2011 printf("V:%6.1f %3d %2d%% %2d%% %3.1f%% %d \r",v_pts, | |
2012 (int)num_frames, | |
2013 (v_frame>0.5)?(int)(100.0*video_time_usage/(double)v_frame):0, | |
2014 (v_frame>0.5)?(int)(100.0*vout_time_usage/(double)v_frame):0, | |
2015 (v_frame>0.5)?(100.0*audio_time_usage/(double)v_frame):0, | |
2016 dbg_es_sent-dbg_es_rcvd); | |
2017 | |
2018 fflush(stdout); | |
2019 frame_corr_num=0; | |
2020 } | |
2021 } | |
2022 ++frame_corr_num; | |
2023 #endif | |
2024 | |
220 | 2025 if(osd_visible){ |
2026 --osd_visible; | |
2027 if(!osd_visible) vo_osd_progbar_type=-1; // disable | |
2028 } | |
1 | 2029 } // while(v_frame<a_frame || force_redraw) |
2030 | |
2031 | |
2032 //================= Keyboard events, SEEKing ==================== | |
2033 | |
2034 { int rel_seek_secs=0; | |
2035 int c; | |
2036 while( | |
2037 #ifdef HAVE_LIRC | |
2038 (c=lirc_mp_getinput())>0 || | |
2039 #endif | |
2040 (c=getch2(0))>0 || (c=mplayer_get_key())>0) switch(c){ | |
2041 // seek 10 sec | |
2042 case KEY_RIGHT: | |
220 | 2043 osd_function=OSD_FFW; |
1 | 2044 rel_seek_secs+=10;break; |
2045 case KEY_LEFT: | |
220 | 2046 osd_function=OSD_REW; |
1 | 2047 rel_seek_secs-=10;break; |
2048 // seek 1 min | |
2049 case KEY_UP: | |
220 | 2050 osd_function=OSD_FFW; |
1 | 2051 rel_seek_secs+=60;break; |
2052 case KEY_DOWN: | |
220 | 2053 osd_function=OSD_REW; |
1 | 2054 rel_seek_secs-=60;break; |
2055 // delay correction: | |
2056 case '+': | |
220 | 2057 buffer_delay+=0.1; // increase audio buffer delay |
1 | 2058 a_frame-=0.1; |
2059 break; | |
2060 case '-': | |
220 | 2061 buffer_delay-=0.1; // decrease audio buffer delay |
1 | 2062 a_frame+=0.1; |
2063 break; | |
2064 // quit | |
2065 case KEY_ESC: // ESC | |
2066 case KEY_ENTER: // ESC | |
2067 case 'q': exit_player("Quit"); | |
36 | 2068 case 'g': grab_frames=2;break; |
1 | 2069 // restart codec |
112 | 2070 #ifdef HAVE_CODECCTRL |
1 | 2071 case 'k': kill(codec_pid,SIGKILL);break; |
2072 // case 'k': kill(child_pid,SIGKILL);break; | |
112 | 2073 #endif |
1 | 2074 // pause |
2075 case 'p': | |
2076 case ' ': | |
220 | 2077 osd_function=OSD_PAUSE; |
1 | 2078 printf("\n------ PAUSED -------\r");fflush(stdout); |
2079 while( | |
2080 #ifdef HAVE_LIRC | |
2081 lirc_mp_getinput()<=0 && | |
2082 #endif | |
33 | 2083 getch2(20)<=0 && mplayer_get_key()<=0){ |
2084 video_out->check_events(); | |
2085 } | |
220 | 2086 osd_function=OSD_PLAY; |
1 | 2087 break; |
2088 } | |
2089 if(rel_seek_secs) | |
2090 if(file_format==DEMUXER_TYPE_AVI && avi_header.idx_size<=0){ | |
2091 printf("Can't seek in raw .AVI streams! (index required) \n"); | |
2092 } else { | |
2093 int skip_audio_bytes=0; | |
2094 float skip_audio_secs=0; | |
2095 | |
2096 // clear demux buffers: | |
2097 if(has_audio) ds_free_packs(d_audio); | |
2098 ds_free_packs(d_video); | |
2099 | |
2100 // printf("a_buffer_len=%d \n",a_buffer_len); | |
2101 a_buffer_len=0; | |
2102 | |
2103 switch(file_format){ | |
2104 | |
2105 case DEMUXER_TYPE_AVI: { | |
2106 //================= seek in AVI ========================== | |
2107 int rel_seek_frames=rel_seek_secs*default_fps; | |
2108 int curr_audio_pos=0; | |
2109 int audio_chunk_pos=-1; | |
2110 int video_chunk_pos=d_video->pos; | |
2111 | |
2112 skip_video_frames=0; | |
2113 | |
2114 // SEEK streams | |
2115 // if(d_video->pts) avi_video_pts=d_video->pts; | |
2116 avi_audio_pts=0; | |
2117 d_video->pts=0; | |
2118 d_audio->pts=0; | |
2119 | |
2120 // find video chunk pos: | |
2121 if(rel_seek_frames>0){ | |
2122 // seek forward | |
2123 while(video_chunk_pos<avi_header.idx_size){ | |
2124 int id=avi_header.idx[video_chunk_pos].ckid; | |
2125 // if(LOWORD(id)==aviTWOCC('0','0')){ // video frame | |
2126 if(avi_stream_id(id)==d_video->id){ // video frame | |
2127 if((--rel_seek_frames)<0 && avi_header.idx[video_chunk_pos].dwFlags&AVIIF_KEYFRAME) break; | |
2128 v_pts+=(float)avi_header.video.dwScale/(float)avi_header.video.dwRate; | |
2129 ++skip_audio_bytes; | |
2130 } | |
2131 ++video_chunk_pos; | |
2132 } | |
2133 } else { | |
2134 // seek backward | |
2135 while(video_chunk_pos>=0){ | |
2136 int id=avi_header.idx[video_chunk_pos].ckid; | |
2137 // if(LOWORD(id)==aviTWOCC('0','0')){ // video frame | |
2138 if(avi_stream_id(id)==d_video->id){ // video frame | |
2139 if((++rel_seek_frames)>0 && avi_header.idx[video_chunk_pos].dwFlags&AVIIF_KEYFRAME) break; | |
2140 v_pts-=(float)avi_header.video.dwScale/(float)avi_header.video.dwRate; | |
2141 --skip_audio_bytes; | |
2142 } | |
2143 --video_chunk_pos; | |
2144 } | |
2145 } | |
2146 avi_header.idx_pos_a=avi_header.idx_pos_v= | |
2147 avi_header.idx_pos=video_chunk_pos; | |
2148 // printf("%d frames skipped\n",skip_audio_bytes); | |
2149 | |
2150 #if 1 | |
2151 // re-calc video pts: | |
2152 avi_video_pts=0; | |
2153 for(i=0;i<video_chunk_pos;i++){ | |
2154 int id=avi_header.idx[i].ckid; | |
2155 // if(LOWORD(id)==aviTWOCC('0','0')){ // video frame | |
2156 if(avi_stream_id(id)==d_video->id){ // video frame | |
2157 avi_video_pts+=(float)avi_header.video.dwScale/(float)avi_header.video.dwRate; | |
2158 } | |
2159 } | |
2160 //printf("v-pts recalc! %5.3f -> %5.3f \n",v_pts,avi_video_pts); | |
2161 v_pts=avi_video_pts; | |
2162 #else | |
2163 avi_video_pts=v_pts; | |
2164 #endif | |
2165 a_pts=avi_video_pts-(buffer_delay); | |
2166 //a_pts=v_pts; //-(buffer_delay+audio_delay); | |
2167 | |
2168 if(has_audio){ | |
2169 int i; | |
2170 int apos=0; | |
2171 int last=0; | |
2172 int len=0; | |
2173 | |
2174 // calc new audio position in audio stream: (using avg.bps value) | |
2175 curr_audio_pos=(avi_video_pts) * ((WAVEFORMATEX*)avi_header.wf_ext)->nAvgBytesPerSec; | |
2176 if(curr_audio_pos<0)curr_audio_pos=0; | |
2177 #if 1 | |
2178 curr_audio_pos&=~15; // requires for PCM formats!!! | |
2179 #else | |
2180 curr_audio_pos/=((WAVEFORMATEX*)avi_header.wf_ext)->nBlockAlign; | |
2181 curr_audio_pos*=((WAVEFORMATEX*)avi_header.wf_ext)->nBlockAlign; | |
2182 avi_header.audio_seekable=1; | |
2183 #endif | |
2184 | |
2185 // find audio chunk pos: | |
2186 for(i=0;i<video_chunk_pos;i++){ | |
2187 int id=avi_header.idx[i].ckid; | |
2188 //if(TWOCCFromFOURCC(id)==cktypeWAVEbytes){ | |
2189 if(avi_stream_id(id)==d_audio->id){ | |
2190 int aid=StreamFromFOURCC(id); | |
2191 if(d_audio->id==aid || d_audio->id==-1){ | |
2192 len=avi_header.idx[i].dwChunkLength; | |
2193 last=i; | |
2194 if(apos<=curr_audio_pos && curr_audio_pos<(apos+len)){ | |
2195 if(verbose)printf("break;\n"); | |
2196 break; | |
2197 } | |
2198 apos+=len; | |
2199 } | |
2200 } | |
2201 } | |
2202 if(verbose)printf("XXX i=%d last=%d apos=%d curr_audio_pos=%d \n", | |
2203 i,last,apos,curr_audio_pos); | |
2204 // audio_chunk_pos=last; // maybe wrong (if not break; ) | |
2205 audio_chunk_pos=i; // maybe wrong (if not break; ) | |
2206 skip_audio_bytes=curr_audio_pos-apos; | |
2207 | |
2208 // update stream position: | |
2209 d_audio->pos=audio_chunk_pos; | |
2210 d_audio->dpos=apos; | |
2211 avi_header.idx_pos_a=avi_header.idx_pos_v= | |
2212 avi_header.idx_pos=audio_chunk_pos; | |
2213 | |
2214 if(!avi_header.audio_seekable){ | |
2215 #if 0 | |
2216 // curr_audio_pos=apos; // selected audio codec can't seek in chunk | |
2217 skip_audio_secs=(float)skip_audio_bytes/(float)((WAVEFORMATEX*)avi_header.wf_ext)->nAvgBytesPerSec; | |
2218 //printf("Seek_AUDIO: %d bytes --> %5.3f secs\n",skip_audio_bytes,skip_audio_secs); | |
2219 skip_audio_bytes=0; | |
2220 #else | |
2221 int d=skip_audio_bytes % ((WAVEFORMATEX*)avi_header.wf_ext)->nBlockAlign; | |
2222 skip_audio_bytes-=d; | |
2223 // curr_audio_pos-=d; | |
2224 skip_audio_secs=(float)d/(float)((WAVEFORMATEX*)avi_header.wf_ext)->nAvgBytesPerSec; | |
2225 //printf("Seek_AUDIO: %d bytes --> %5.3f secs\n",d,skip_audio_secs); | |
2226 #endif | |
2227 } | |
2228 // now: audio_chunk_pos=pos in index | |
2229 // skip_audio_bytes=bytes to skip from that chunk | |
2230 // skip_audio_secs=time to play audio before video (if can't skip) | |
2231 | |
2232 // calc skip_video_frames & adjust video pts counter: | |
2233 // i=last; | |
2234 i=avi_header.idx_pos; | |
2235 while(i<video_chunk_pos){ | |
2236 int id=avi_header.idx[i].ckid; | |
2237 // if(LOWORD(id)==aviTWOCC('0','0')){ // video frame | |
2238 if(avi_stream_id(id)==d_video->id){ // video frame | |
2239 ++skip_video_frames; | |
2240 // requires for correct audio pts calculation (demuxer): | |
2241 avi_video_pts-=(float)avi_header.video.dwScale/(float)avi_header.video.dwRate; | |
2242 } | |
2243 ++i; | |
2244 } | |
2245 | |
2246 } | |
2247 | |
2248 if(verbose) printf("SEEK: idx=%d (a:%d v:%d) v.skip=%d a.skip=%d/%4.3f \n", | |
2249 avi_header.idx_pos,audio_chunk_pos,video_chunk_pos, | |
2250 skip_video_frames,skip_audio_bytes,skip_audio_secs); | |
2251 | |
220 | 2252 // Set OSD: |
2253 osd_visible=default_fps; | |
2254 vo_osd_progbar_type=0; | |
2255 vo_osd_progbar_value=(demuxer->filepos)/((avi_header.movi_end-avi_header.movi_start)>>8); | |
2256 printf("avi filepos = %d \n",vo_osd_progbar_value); | |
2257 // printf("avi filepos = %d (len=%d) \n",demuxer->filepos,(avi_header.movi_end-avi_header.movi_start)); | |
2258 | |
1 | 2259 } |
2260 break; | |
2261 | |
2262 case DEMUXER_TYPE_ASF: { | |
2263 //================= seek in ASF ========================== | |
2264 float p_rate=10; // packets / sec | |
2265 int rel_seek_packs=rel_seek_secs*p_rate; | |
2266 int rel_seek_bytes=rel_seek_packs*fileh.packetsize; | |
2267 int newpos; | |
2268 //printf("ASF: packs: %d duration: %d \n",(int)fileh.packets,*((int*)&fileh.duration)); | |
2269 // printf("ASF_seek: %d secs -> %d packs -> %d bytes \n", | |
2270 // rel_seek_secs,rel_seek_packs,rel_seek_bytes); | |
2271 newpos=demuxer->filepos+rel_seek_bytes; | |
2272 if(newpos<0) newpos=0; | |
2273 stream_seek(demuxer->stream,newpos); | |
2274 } | |
2275 break; | |
2276 | |
2277 case DEMUXER_TYPE_MPEG_ES: | |
2278 case DEMUXER_TYPE_MPEG_PS: { | |
2279 //================= seek in MPEG ========================== | |
2280 int newpos; | |
2281 if(picture->bitrate==0x3FFFF) // unspecified? | |
2282 newpos=demuxer->filepos+2324*75*rel_seek_secs; // 174.3 kbyte/sec | |
2283 else | |
2284 newpos=demuxer->filepos+(picture->bitrate*1000/16)*rel_seek_secs; | |
2285 // picture->bitrate=2324*75*8; // standard VCD bitrate (75 sectors / sec) | |
2286 | |
2287 if(newpos<seek_to_byte) newpos=seek_to_byte; | |
2288 stream_seek(demuxer->stream,newpos); | |
2289 // re-sync video: | |
2290 videobuf_code_len=0; // reset ES stream buffer | |
2291 while(1){ | |
2292 int i=sync_video_packet(d_video); | |
2293 if(i==0x1B3 || i==0x1B8) break; // found it! | |
2294 if(!i || !skip_video_packet(d_video)){ eof=1; break;} // EOF | |
2295 } | |
2296 // re-sync audio: (must read to get actual audio PTS) | |
2297 // if(has_audio) ds_fill_buffer(d_audio); | |
2298 } | |
2299 break; | |
2300 | |
2301 } // switch(file_format) | |
2302 | |
2303 //====================== re-sync audio: ===================== | |
2304 if(has_audio){ | |
2305 | |
2306 if(skip_audio_bytes){ | |
2307 demux_read_data(d_audio,NULL,skip_audio_bytes); | |
2308 d_audio->pts=0; // PTS is outdated because of the raw data skipping | |
2309 } | |
2310 | |
2311 current_module="resync_audio"; | |
2312 | |
2313 switch(has_audio){ | |
2314 case 1: | |
2315 MP3_DecodeFrame(NULL,-2); // resync | |
2316 MP3_DecodeFrame(NULL,-2); // resync | |
2317 MP3_DecodeFrame(NULL,-2); // resync | |
2318 break; | |
2319 case 3: | |
2320 ac3_bitstream_reset(); // reset AC3 bitstream buffer | |
2321 // if(verbose){ printf("Resyncing AC3 audio...");fflush(stdout);} | |
2322 ac3_frame=ac3_decode_frame(); // resync | |
2323 // if(verbose) printf(" OK!\n"); | |
2324 break; | |
2325 case 4: | |
2326 a_in_buffer_len=0; // reset ACM audio buffer | |
2327 break; | |
2328 } | |
2329 | |
2330 // re-sync PTS (MPEG-PS only!!!) | |
2331 if(file_format==DEMUXER_TYPE_MPEG_PS) | |
2332 if(d_video->pts && d_audio->pts){ | |
2333 if (d_video->pts < d_audio->pts){ | |
2334 | |
2335 } else { | |
2336 while(d_video->pts > d_audio->pts){ | |
2337 switch(has_audio){ | |
2338 case 1: MP3_DecodeFrame(NULL,-2);break; // skip MPEG frame | |
2339 case 3: ac3_frame=ac3_decode_frame();break; // skip AC3 frame | |
2340 default: ds_fill_buffer(d_audio); // skip PCM frame | |
2341 } | |
2342 } | |
2343 } | |
2344 } | |
2345 | |
2346 current_module=NULL; | |
2347 | |
2348 c_total=0; // kell ez? | |
2349 printf("A:%6.1f V:%6.1f A-V:%7.3f",d_audio->pts,d_video->pts,0.0f); | |
2350 printf(" ct:%7.3f \r",c_total);fflush(stdout); | |
2351 } else { | |
2352 printf("A: --- V:%6.1f \r",d_video->pts);fflush(stdout); | |
2353 } | |
2354 | |
2355 max_pts_correction=0.1; | |
2356 frame_corr_num=-5; frame_correction=0; | |
2357 force_redraw=5; | |
2358 a_frame=-buffer_delay-skip_audio_secs; | |
2359 // a_frame=-audio_delay-buffer_delay-skip_audio_secs; | |
2360 v_frame=0; // !!!!!! | |
2361 audio_time_usage=0; video_time_usage=0; vout_time_usage=0; | |
2362 // num_frames=real_num_frames=0; | |
2363 } | |
2364 } // keyboard event handler | |
2365 | |
220 | 2366 //================= Update OSD ==================== |
2367 { int i; | |
2368 sprintf(osd_text_buffer,"%c %02d:%02d:%02d",osd_function,(int)v_pts/3600,((int)v_pts/60)%60,((int)v_pts)%60); | |
2369 // for(i=1;i<=11;i++) osd_text_buffer[10+i]=i;osd_text_buffer[10+i]=0; | |
2370 vo_osd_text=osd_text_buffer; | |
258 | 2371 |
2372 // find sub | |
2373 if(subtitles){ | |
2374 if(sub_fps==0) sub_fps=default_fps; | |
2375 find_sub(sub_uses_time?(100*(v_pts+sub_delay)):((v_pts+sub_delay)*sub_fps)); // FIXME! frame counter... | |
2376 } | |
220 | 2377 } |
1 | 2378 |
2379 } // while(!eof) | |
2380 | |
2381 //printf("\nEnd of file.\n"); | |
2382 exit_player("End of file"); | |
109 | 2383 } |
2384 return 1; | |
2385 } |