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