2567
|
1 // read video frame
|
|
2
|
|
3 #include <stdio.h>
|
|
4 #include <stdlib.h>
|
|
5 #include <unistd.h>
|
|
6
|
|
7 #include "config.h"
|
|
8 #include "mp_msg.h"
|
|
9 #include "help_mp.h"
|
|
10
|
|
11 #include "stream.h"
|
|
12 #include "demuxer.h"
|
|
13 #include "stheader.h"
|
|
14 #include "parse_es.h"
|
|
15 #include "mpeg_hdr.h"
|
|
16
|
|
17 static mp_mpeg_header_t picture;
|
|
18
|
|
19 int video_read_properties(sh_video_t *sh_video){
|
|
20 demux_stream_t *d_video=sh_video->ds;
|
|
21
|
|
22 // Determine image properties:
|
|
23 switch(d_video->demuxer->file_format){
|
|
24 case DEMUXER_TYPE_AVI:
|
|
25 case DEMUXER_TYPE_ASF: {
|
|
26 // display info:
|
|
27 sh_video->format=sh_video->bih->biCompression;
|
|
28 sh_video->disp_w=sh_video->bih->biWidth;
|
|
29 sh_video->disp_h=abs(sh_video->bih->biHeight);
|
|
30 break;
|
|
31 }
|
|
32 case DEMUXER_TYPE_MPEG_ES:
|
|
33 case DEMUXER_TYPE_MPEG_PS: {
|
|
34 // Find sequence_header first:
|
|
35 videobuf_len=0; videobuf_code_len=0;
|
|
36 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for sequence header... ");fflush(stdout);
|
|
37 while(1){
|
|
38 int i=sync_video_packet(d_video);
|
|
39 if(i==0x1B3) break; // found it!
|
|
40 if(!i || !skip_video_packet(d_video)){
|
|
41 if(verbose) mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
|
|
42 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_MpegNoSequHdr);
|
|
43 return 0;
|
|
44 }
|
|
45 }
|
|
46 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
|
|
47 // sh_video=d_video->sh;sh_video->ds=d_video;
|
|
48 // mpeg2_init();
|
|
49 // ========= Read & process sequence header & extension ============
|
|
50 if(!videobuffer) videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE);
|
|
51 if(!videobuffer){
|
|
52 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail);
|
|
53 return 0;
|
|
54 }
|
|
55
|
|
56 if(!read_video_packet(d_video)){
|
|
57 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_CannotReadMpegSequHdr);
|
|
58 return 0;
|
|
59 }
|
|
60 if(mp_header_process_sequence_header (&picture, &videobuffer[4])) {
|
|
61 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_BadMpegSequHdr);
|
|
62 return 0;
|
|
63 }
|
|
64 if(sync_video_packet(d_video)==0x1B5){ // next packet is seq. ext.
|
|
65 // videobuf_len=0;
|
|
66 int pos=videobuf_len;
|
|
67 if(!read_video_packet(d_video)){
|
|
68 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_CannotReadMpegSequHdrEx);
|
|
69 return 0;
|
|
70 }
|
|
71 if(mp_header_process_extension (&picture, &videobuffer[pos+4])) {
|
|
72 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_BadMpegSequHdrEx);
|
|
73 return 0;
|
|
74 }
|
|
75 }
|
|
76
|
|
77 // printf("picture.fps=%d\n",picture.fps);
|
|
78
|
|
79 // fill aspect info:
|
|
80 switch(picture.aspect_ratio_information){
|
|
81 case 2: // PAL/NTSC SVCD/DVD 4:3
|
|
82 case 8: // PAL VCD 4:3
|
|
83 case 12: // NTSC VCD 4:3
|
|
84 sh_video->aspect=4.0/3.0;
|
|
85 break;
|
|
86 case 3: // PAL/NTSC Widescreen SVCD/DVD 16:9
|
|
87 sh_video->aspect=16.0/9.0;
|
|
88 break;
|
|
89 default:
|
|
90 fprintf(stderr,"Detected unknown aspect_ratio_information in mpeg sequence header.\n"
|
|
91 "Please report the aspect value (%i) along with the movie type (VGA,PAL,NTSC,"
|
|
92 "SECAM) and the movie resolution (720x576,352x240,480x480,...) to the MPlayer"
|
|
93 " developers, so that we can add support for it!\nAssuming 1:1 aspect for now.\n",
|
|
94 picture.aspect_ratio_information);
|
|
95 case 1: // VGA 1:1 - do not prescale
|
|
96 sh_video->aspect=0.0;
|
|
97 break;
|
|
98 }
|
|
99 // display info:
|
|
100 sh_video->format=picture.mpeg1?0x10000001:0x10000002; // mpeg video
|
|
101 sh_video->fps=picture.fps*0.0001f;
|
|
102 if(!sh_video->fps){
|
|
103 // if(!force_fps){
|
|
104 // fprintf(stderr,"FPS not specified (or invalid) in the header! Use the -fps option!\n");
|
|
105 // return 0;
|
|
106 // }
|
|
107 sh_video->frametime=0;
|
|
108 } else {
|
|
109 sh_video->frametime=10000.0f/(float)picture.fps;
|
|
110 }
|
|
111 sh_video->disp_w=picture.display_picture_width;
|
|
112 sh_video->disp_h=picture.display_picture_height;
|
|
113 // bitrate:
|
|
114 if(picture.bitrate!=0x3FFFF) // unspecified/VBR ?
|
|
115 sh_video->i_bps=1000*picture.bitrate/16;
|
|
116 // info:
|
|
117 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2,"mpeg bitrate: %d (%X)\n",picture.bitrate,picture.bitrate);
|
|
118 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"VIDEO: %s %dx%d (aspect %d) %4.2f fps %5.1f kbps (%4.1f kbyte/s)\n",
|
|
119 picture.mpeg1?"MPEG1":"MPEG2",
|
|
120 sh_video->disp_w,sh_video->disp_h,
|
|
121 picture.aspect_ratio_information,
|
|
122 sh_video->fps,
|
|
123 picture.bitrate*0.5f,
|
|
124 picture.bitrate/16.0f );
|
|
125 break;
|
|
126 }
|
|
127 } // switch(file_format)
|
|
128
|
|
129 return 1;
|
|
130 }
|
|
131
|
|
132 int video_read_frame(sh_video_t* sh_video,float* frame_time_ptr,unsigned char** start,int force_fps){
|
|
133 demux_stream_t *d_video=sh_video->ds;
|
|
134 demuxer_t *demuxer=d_video->demuxer;
|
|
135 float frame_time=1;
|
|
136 float pts1=d_video->pts;
|
|
137 // unsigned char* start=NULL;
|
|
138 int in_size=0;
|
|
139
|
|
140 *start=NULL;
|
|
141
|
|
142 if(demuxer->file_format==DEMUXER_TYPE_MPEG_ES || demuxer->file_format==DEMUXER_TYPE_MPEG_PS){
|
|
143 int in_frame=0;
|
|
144 //float newfps;
|
|
145 //videobuf_len=0;
|
|
146 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
|
|
147 int i=sync_video_packet(d_video);
|
|
148 void* buffer=&videobuffer[videobuf_len+4];
|
|
149 if(in_frame){
|
|
150 if(i<0x101 || i>=0x1B0){ // not slice code -> end of frame
|
|
151 #if 1
|
|
152 // send END OF FRAME code:
|
|
153 videobuffer[videobuf_len+0]=0;
|
|
154 videobuffer[videobuf_len+1]=0;
|
|
155 videobuffer[videobuf_len+2]=1;
|
|
156 videobuffer[videobuf_len+3]=0xFF;
|
|
157 videobuf_len+=4;
|
|
158 #endif
|
|
159 if(!i) return -1; // EOF
|
|
160 break;
|
|
161 }
|
|
162 } else {
|
|
163 //if(i==0x100) in_frame=1; // picture startcode
|
|
164 if(i>=0x101 && i<0x1B0) in_frame=1; // picture startcode
|
|
165 else if(!i) return -1; // EOF
|
|
166 }
|
|
167 //if(grab_frames==2 && (i==0x1B3 || i==0x1B8)) grab_frames=1;
|
|
168 if(!read_video_packet(d_video)) return -1; // EOF
|
|
169 //printf("read packet 0x%X, len=%d\n",i,videobuf_len);
|
|
170 // process headers:
|
|
171 switch(i){
|
|
172 case 0x1B3: mp_header_process_sequence_header (&picture, buffer);break;
|
|
173 case 0x1B5: mp_header_process_extension (&picture, buffer);break;
|
|
174 }
|
|
175 }
|
|
176
|
|
177 // if(videobuf_len>max_framesize) max_framesize=videobuf_len; // debug
|
|
178 //printf("--- SEND %d bytes\n",videobuf_len);
|
|
179 // if(grab_frames==1){
|
|
180 // FILE *f=fopen("grab.mpg","ab");
|
|
181 // fwrite(videobuffer,videobuf_len-4,1,f);
|
|
182 // fclose(f);
|
|
183 // }
|
|
184
|
|
185 *start=videobuffer; in_size=videobuf_len;
|
|
186 //blit_frame=decode_video(video_out,sh_video,videobuffer,videobuf_len,drop_frame);
|
|
187
|
|
188 #if 1
|
|
189 // get mpeg fps:
|
|
190 //newfps=frameratecode2framerate[picture->frame_rate_code]*0.0001f;
|
|
191 if((int)(sh_video->fps*10000+0.5)!=picture.fps) if(!force_fps){
|
|
192 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);
|
|
193 sh_video->fps=picture.fps*0.0001;
|
|
194 sh_video->frametime=10000.0f/(float)picture.fps;
|
|
195 }
|
|
196 #endif
|
|
197
|
|
198 // fix mpeg2 frametime:
|
|
199 frame_time=(picture.display_time)*0.01f;
|
|
200 picture.display_time=100;
|
|
201 videobuf_len=0;
|
|
202
|
|
203 } else {
|
|
204 // frame-based file formats: (AVI,ASF,MOV)
|
|
205 in_size=ds_get_packet(d_video,start);
|
|
206 if(in_size<0) return -1; // EOF
|
|
207 // if(in_size>max_framesize) max_framesize=in_size;
|
|
208 // blit_frame=decode_video(video_out,sh_video,start,in_size,drop_frame);
|
|
209 }
|
|
210
|
|
211 // vdecode_time=video_time_usage-vdecode_time;
|
|
212
|
|
213 //------------------------ frame decoded. --------------------
|
|
214
|
|
215 // Increase video timers:
|
|
216 sh_video->num_frames+=frame_time;
|
|
217 ++sh_video->num_frames_decoded;
|
|
218
|
|
219 frame_time*=sh_video->frametime;
|
|
220 if(demuxer->file_format==DEMUXER_TYPE_ASF && !force_fps){
|
|
221 // .ASF files has no fixed FPS - just frame durations!
|
|
222 float d=d_video->pts-pts1;
|
|
223 if(d>=0 && d<5) frame_time=d;
|
|
224 if(d>0){
|
|
225 if(verbose)
|
|
226 if((int)sh_video->fps==1000)
|
|
227 mp_msg(MSGT_CPLAYER,MSGL_STATUS,"\rASF framerate: %d fps \n",(int)(1.0f/d));
|
|
228 sh_video->frametime=d; // 1ms
|
|
229 sh_video->fps=1.0f/d;
|
|
230 }
|
|
231 } else
|
|
232 if(demuxer->file_format==DEMUXER_TYPE_MOV && !force_fps){
|
|
233 // .MOV files has no fixed FPS - just frame durations!
|
|
234 frame_time=d_video->pts-pts1;
|
|
235 }
|
|
236
|
|
237 if(demuxer->file_format==DEMUXER_TYPE_MPEG_PS) d_video->pts+=frame_time;
|
|
238
|
|
239 if(frame_time_ptr) *frame_time_ptr=frame_time;
|
|
240 return in_size;
|
|
241
|
|
242 }
|
|
243
|
|
244
|