Mercurial > mplayer.hg
annotate libmpdemux/video.c @ 5449:c13dba5b224e
added vfw and acm uninit
author | alex |
---|---|
date | Mon, 01 Apr 2002 13:25:34 +0000 |
parents | 1a87310b5ca9 |
children | 8cd761968f35 |
rev | line source |
---|---|
2567 | 1 // read video frame |
2 | |
2775 | 3 #include "config.h" |
4 | |
2567 | 5 #include <stdio.h> |
2775 | 6 #ifdef HAVE_MALLOC_H |
7 #include <malloc.h> | |
8 #endif | |
2567 | 9 #include <stdlib.h> |
3726 | 10 #include <string.h> |
2567 | 11 #include <unistd.h> |
12 | |
13 #include "mp_msg.h" | |
14 #include "help_mp.h" | |
15 | |
16 #include "stream.h" | |
17 #include "demuxer.h" | |
18 #include "stheader.h" | |
19 #include "parse_es.h" | |
20 #include "mpeg_hdr.h" | |
21 | |
4774
fc2f76964606
Patch: Improved raw encoding support in mencoder by Fredrik Kuivinen
atmos4
parents:
4705
diff
changeset
|
22 /* biCompression constant */ |
fc2f76964606
Patch: Improved raw encoding support in mencoder by Fredrik Kuivinen
atmos4
parents:
4705
diff
changeset
|
23 #define BI_RGB 0L |
fc2f76964606
Patch: Improved raw encoding support in mencoder by Fredrik Kuivinen
atmos4
parents:
4705
diff
changeset
|
24 |
2567 | 25 static mp_mpeg_header_t picture; |
26 | |
27 int video_read_properties(sh_video_t *sh_video){ | |
28 demux_stream_t *d_video=sh_video->ds; | |
29 | |
30 // Determine image properties: | |
31 switch(d_video->demuxer->file_format){ | |
32 case DEMUXER_TYPE_AVI: | |
33 case DEMUXER_TYPE_ASF: { | |
4774
fc2f76964606
Patch: Improved raw encoding support in mencoder by Fredrik Kuivinen
atmos4
parents:
4705
diff
changeset
|
34 // display info: |
fc2f76964606
Patch: Improved raw encoding support in mencoder by Fredrik Kuivinen
atmos4
parents:
4705
diff
changeset
|
35 |
fc2f76964606
Patch: Improved raw encoding support in mencoder by Fredrik Kuivinen
atmos4
parents:
4705
diff
changeset
|
36 if(sh_video->bih->biCompression == BI_RGB && |
fc2f76964606
Patch: Improved raw encoding support in mencoder by Fredrik Kuivinen
atmos4
parents:
4705
diff
changeset
|
37 (sh_video->video.fccHandler == mmioFOURCC('D', 'I', 'B', ' ') || |
fc2f76964606
Patch: Improved raw encoding support in mencoder by Fredrik Kuivinen
atmos4
parents:
4705
diff
changeset
|
38 sh_video->video.fccHandler == mmioFOURCC('R', 'G', 'B', ' ') || |
fc2f76964606
Patch: Improved raw encoding support in mencoder by Fredrik Kuivinen
atmos4
parents:
4705
diff
changeset
|
39 sh_video->video.fccHandler == mmioFOURCC('R', 'A', 'W', ' ') || |
fc2f76964606
Patch: Improved raw encoding support in mencoder by Fredrik Kuivinen
atmos4
parents:
4705
diff
changeset
|
40 sh_video->video.fccHandler == 0)) { |
fc2f76964606
Patch: Improved raw encoding support in mencoder by Fredrik Kuivinen
atmos4
parents:
4705
diff
changeset
|
41 sh_video->format = mmioFOURCC(0, 'R', 'G', 'B') | sh_video->bih->biBitCount; |
fc2f76964606
Patch: Improved raw encoding support in mencoder by Fredrik Kuivinen
atmos4
parents:
4705
diff
changeset
|
42 } |
fc2f76964606
Patch: Improved raw encoding support in mencoder by Fredrik Kuivinen
atmos4
parents:
4705
diff
changeset
|
43 else |
fc2f76964606
Patch: Improved raw encoding support in mencoder by Fredrik Kuivinen
atmos4
parents:
4705
diff
changeset
|
44 sh_video->format=sh_video->bih->biCompression; |
fc2f76964606
Patch: Improved raw encoding support in mencoder by Fredrik Kuivinen
atmos4
parents:
4705
diff
changeset
|
45 |
2567 | 46 sh_video->disp_w=sh_video->bih->biWidth; |
47 sh_video->disp_h=abs(sh_video->bih->biHeight); | |
3980
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
48 |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
49 #if 1 |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
50 /* hack to support decoding of mpeg1 chunks in AVI's with libmpeg2 -- 2002 alex */ |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
51 if ((sh_video->format == 0x10000001) || |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
52 (sh_video->format == 0x10000002) || |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
53 (sh_video->format == mmioFOURCC('m','p','g','1')) || |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
54 (sh_video->format == mmioFOURCC('M','P','G','1')) || |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
55 (sh_video->format == mmioFOURCC('m','p','g','2')) || |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
56 (sh_video->format == mmioFOURCC('M','P','G','2')) || |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
57 (sh_video->format == mmioFOURCC('m','p','e','g')) || |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
58 (sh_video->format == mmioFOURCC('m','p','e','g'))) |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
59 { |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
60 int saved_pos, saved_type; |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
61 |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
62 /* demuxer pos saving is required for libavcodec mpeg decoder as it's |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
63 reading the mpeg header self! */ |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
64 |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
65 saved_pos = d_video->buffer_pos; |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
66 saved_type = d_video->demuxer->file_format; |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
67 |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
68 d_video->demuxer->file_format = DEMUXER_TYPE_MPEG_ES; |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
69 video_read_properties(sh_video); |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
70 d_video->demuxer->file_format = saved_type; |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
71 d_video->buffer_pos = saved_pos; |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
72 // goto mpeg_header_parser; |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
73 } |
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
74 #endif |
2567 | 75 break; |
76 } | |
77 case DEMUXER_TYPE_MPEG_ES: | |
78 case DEMUXER_TYPE_MPEG_PS: { | |
3980
cdd55ab40363
libmpeg2 is now able to decode framecopied (with mencoder) mpeg files
alex
parents:
3726
diff
changeset
|
79 //mpeg_header_parser: |
2567 | 80 // Find sequence_header first: |
81 videobuf_len=0; videobuf_code_len=0; | |
82 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for sequence header... ");fflush(stdout); | |
83 while(1){ | |
84 int i=sync_video_packet(d_video); | |
85 if(i==0x1B3) break; // found it! | |
86 if(!i || !skip_video_packet(d_video)){ | |
87 if(verbose) mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n"); | |
88 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_MpegNoSequHdr); | |
89 return 0; | |
90 } | |
91 } | |
92 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n"); | |
93 // sh_video=d_video->sh;sh_video->ds=d_video; | |
94 // mpeg2_init(); | |
95 // ========= Read & process sequence header & extension ============ | |
96 if(!videobuffer) videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE); | |
97 if(!videobuffer){ | |
98 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail); | |
99 return 0; | |
100 } | |
101 | |
102 if(!read_video_packet(d_video)){ | |
103 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_CannotReadMpegSequHdr); | |
104 return 0; | |
105 } | |
106 if(mp_header_process_sequence_header (&picture, &videobuffer[4])) { | |
107 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_BadMpegSequHdr); | |
108 return 0; | |
109 } | |
110 if(sync_video_packet(d_video)==0x1B5){ // next packet is seq. ext. | |
111 // videobuf_len=0; | |
112 int pos=videobuf_len; | |
113 if(!read_video_packet(d_video)){ | |
114 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_CannotReadMpegSequHdrEx); | |
115 return 0; | |
116 } | |
117 if(mp_header_process_extension (&picture, &videobuffer[pos+4])) { | |
118 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_BadMpegSequHdrEx); | |
119 return 0; | |
120 } | |
121 } | |
122 | |
123 // printf("picture.fps=%d\n",picture.fps); | |
124 | |
125 // fill aspect info: | |
126 switch(picture.aspect_ratio_information){ | |
127 case 2: // PAL/NTSC SVCD/DVD 4:3 | |
3379
84ed0593187c
Add aspect 4 to known aspect codes, probably SECAM 4:3, at least sample file was 4:3.
atmos4
parents:
2775
diff
changeset
|
128 case 4: // SECAM 4:3? - XXX check with more files! |
2567 | 129 case 8: // PAL VCD 4:3 |
130 case 12: // NTSC VCD 4:3 | |
131 sh_video->aspect=4.0/3.0; | |
132 break; | |
133 case 3: // PAL/NTSC Widescreen SVCD/DVD 16:9 | |
3666 | 134 case 6: // (PAL?)/NTSC Widescreen SVCD 16:9 |
2567 | 135 sh_video->aspect=16.0/9.0; |
136 break; | |
4336 | 137 case 9: // Movie Type ??? / 640x480 |
138 sh_video->aspect=0.0; | |
139 break; | |
2567 | 140 default: |
141 fprintf(stderr,"Detected unknown aspect_ratio_information in mpeg sequence header.\n" | |
142 "Please report the aspect value (%i) along with the movie type (VGA,PAL,NTSC," | |
143 "SECAM) and the movie resolution (720x576,352x240,480x480,...) to the MPlayer" | |
144 " developers, so that we can add support for it!\nAssuming 1:1 aspect for now.\n", | |
145 picture.aspect_ratio_information); | |
146 case 1: // VGA 1:1 - do not prescale | |
147 sh_video->aspect=0.0; | |
148 break; | |
149 } | |
150 // display info: | |
151 sh_video->format=picture.mpeg1?0x10000001:0x10000002; // mpeg video | |
152 sh_video->fps=picture.fps*0.0001f; | |
153 if(!sh_video->fps){ | |
154 // if(!force_fps){ | |
155 // fprintf(stderr,"FPS not specified (or invalid) in the header! Use the -fps option!\n"); | |
156 // return 0; | |
157 // } | |
158 sh_video->frametime=0; | |
159 } else { | |
160 sh_video->frametime=10000.0f/(float)picture.fps; | |
161 } | |
162 sh_video->disp_w=picture.display_picture_width; | |
163 sh_video->disp_h=picture.display_picture_height; | |
164 // bitrate: | |
165 if(picture.bitrate!=0x3FFFF) // unspecified/VBR ? | |
166 sh_video->i_bps=1000*picture.bitrate/16; | |
167 // info: | |
168 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2,"mpeg bitrate: %d (%X)\n",picture.bitrate,picture.bitrate); | |
169 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"VIDEO: %s %dx%d (aspect %d) %4.2f fps %5.1f kbps (%4.1f kbyte/s)\n", | |
170 picture.mpeg1?"MPEG1":"MPEG2", | |
171 sh_video->disp_w,sh_video->disp_h, | |
172 picture.aspect_ratio_information, | |
173 sh_video->fps, | |
174 picture.bitrate*0.5f, | |
175 picture.bitrate/16.0f ); | |
176 break; | |
177 } | |
178 } // switch(file_format) | |
179 | |
180 return 1; | |
181 } | |
182 | |
4074 | 183 static void process_userdata(unsigned char* buf,int len){ |
184 int i; | |
185 printf( "user_data: len=%3d %02X %02X %02X %02X '", | |
186 len, buf[0], buf[1], buf[2], buf[3]); | |
187 for(i=0;i<len;i++) | |
188 if(buf[i]>=32 && buf[i]<127) putchar(buf[i]); | |
189 printf("'\n"); | |
190 } | |
191 | |
2567 | 192 int video_read_frame(sh_video_t* sh_video,float* frame_time_ptr,unsigned char** start,int force_fps){ |
193 demux_stream_t *d_video=sh_video->ds; | |
194 demuxer_t *demuxer=d_video->demuxer; | |
195 float frame_time=1; | |
196 float pts1=d_video->pts; | |
197 // unsigned char* start=NULL; | |
198 int in_size=0; | |
199 | |
200 *start=NULL; | |
201 | |
202 if(demuxer->file_format==DEMUXER_TYPE_MPEG_ES || demuxer->file_format==DEMUXER_TYPE_MPEG_PS){ | |
203 int in_frame=0; | |
204 //float newfps; | |
205 //videobuf_len=0; | |
206 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){ | |
207 int i=sync_video_packet(d_video); | |
4074 | 208 //void* buffer=&videobuffer[videobuf_len+4]; |
209 int start=videobuf_len+4; | |
2567 | 210 if(in_frame){ |
211 if(i<0x101 || i>=0x1B0){ // not slice code -> end of frame | |
212 #if 1 | |
213 // send END OF FRAME code: | |
214 videobuffer[videobuf_len+0]=0; | |
215 videobuffer[videobuf_len+1]=0; | |
216 videobuffer[videobuf_len+2]=1; | |
217 videobuffer[videobuf_len+3]=0xFF; | |
218 videobuf_len+=4; | |
219 #endif | |
220 if(!i) return -1; // EOF | |
221 break; | |
222 } | |
223 } else { | |
224 //if(i==0x100) in_frame=1; // picture startcode | |
225 if(i>=0x101 && i<0x1B0) in_frame=1; // picture startcode | |
226 else if(!i) return -1; // EOF | |
227 } | |
228 //if(grab_frames==2 && (i==0x1B3 || i==0x1B8)) grab_frames=1; | |
229 if(!read_video_packet(d_video)) return -1; // EOF | |
230 //printf("read packet 0x%X, len=%d\n",i,videobuf_len); | |
231 // process headers: | |
232 switch(i){ | |
4074 | 233 case 0x1B3: mp_header_process_sequence_header (&picture, &videobuffer[start]);break; |
234 case 0x1B5: mp_header_process_extension (&picture, &videobuffer[start]);break; | |
235 case 0x1B2: if(verbose) process_userdata (&videobuffer[start], videobuf_len-start);break; | |
2567 | 236 } |
237 } | |
238 | |
239 // if(videobuf_len>max_framesize) max_framesize=videobuf_len; // debug | |
240 //printf("--- SEND %d bytes\n",videobuf_len); | |
241 // if(grab_frames==1){ | |
242 // FILE *f=fopen("grab.mpg","ab"); | |
243 // fwrite(videobuffer,videobuf_len-4,1,f); | |
244 // fclose(f); | |
245 // } | |
246 | |
247 *start=videobuffer; in_size=videobuf_len; | |
248 //blit_frame=decode_video(video_out,sh_video,videobuffer,videobuf_len,drop_frame); | |
249 | |
250 #if 1 | |
251 // get mpeg fps: | |
252 //newfps=frameratecode2framerate[picture->frame_rate_code]*0.0001f; | |
253 if((int)(sh_video->fps*10000+0.5)!=picture.fps) if(!force_fps){ | |
254 mp_msg(MSGT_CPLAYER,MSGL_WARN,"Warning! FPS changed %5.3f -> %5.3f (%f) [%d] \n",sh_video->fps,picture.fps*0.0001,sh_video->fps-picture.fps*0.0001,picture.frame_rate_code); | |
255 sh_video->fps=picture.fps*0.0001; | |
256 sh_video->frametime=10000.0f/(float)picture.fps; | |
257 } | |
258 #endif | |
259 | |
260 // fix mpeg2 frametime: | |
261 frame_time=(picture.display_time)*0.01f; | |
262 picture.display_time=100; | |
263 videobuf_len=0; | |
264 | |
265 } else { | |
266 // frame-based file formats: (AVI,ASF,MOV) | |
267 in_size=ds_get_packet(d_video,start); | |
268 if(in_size<0) return -1; // EOF | |
269 // if(in_size>max_framesize) max_framesize=in_size; | |
270 // blit_frame=decode_video(video_out,sh_video,start,in_size,drop_frame); | |
271 } | |
272 | |
273 // vdecode_time=video_time_usage-vdecode_time; | |
274 | |
275 //------------------------ frame decoded. -------------------- | |
276 | |
277 // Increase video timers: | |
278 sh_video->num_frames+=frame_time; | |
279 ++sh_video->num_frames_decoded; | |
280 | |
281 frame_time*=sh_video->frametime; | |
282 if(demuxer->file_format==DEMUXER_TYPE_ASF && !force_fps){ | |
283 // .ASF files has no fixed FPS - just frame durations! | |
284 float d=d_video->pts-pts1; | |
285 if(d>=0 && d<5) frame_time=d; | |
286 if(d>0){ | |
287 if(verbose) | |
288 if((int)sh_video->fps==1000) | |
289 mp_msg(MSGT_CPLAYER,MSGL_STATUS,"\rASF framerate: %d fps \n",(int)(1.0f/d)); | |
290 sh_video->frametime=d; // 1ms | |
291 sh_video->fps=1.0f/d; | |
292 } | |
293 } else | |
294 if(demuxer->file_format==DEMUXER_TYPE_MOV && !force_fps){ | |
295 // .MOV files has no fixed FPS - just frame durations! | |
296 frame_time=d_video->pts-pts1; | |
3526 | 297 } else |
5143 | 298 if(demuxer->file_format==DEMUXER_TYPE_FILM && !force_fps){ |
299 // FILM (CPK) files have no fixed FPS - just frame durations! | |
300 frame_time=d_video->pts-pts1; | |
301 } else | |
3526 | 302 if(demuxer->file_format==DEMUXER_TYPE_VIVO && !force_fps){ |
303 // .VIVO files has no fixed FPS - just frame durations! | |
304 if(d_video->pts-pts1>0) | |
305 frame_time=d_video->pts-pts1; | |
306 mp_msg(MSGT_CPLAYER,MSGL_V,"vivo frame_time=%5.3f \n",frame_time); | |
2567 | 307 } |
308 | |
4705 | 309 if(demuxer->file_format==DEMUXER_TYPE_MPEG_PS || |
310 demuxer->file_format==DEMUXER_TYPE_MPEG_ES) d_video->pts+=frame_time; | |
2567 | 311 |
312 if(frame_time_ptr) *frame_time_ptr=frame_time; | |
313 return in_size; | |
314 | |
315 } | |
316 | |
317 |