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