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