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