comparison mplayer.c @ 1:3b5f5d1c5041

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