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