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