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