annotate mplayer.c @ 13:36314c7bda8d

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