comparison mplayerHQ.c @ 118:7c67c52142ed

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