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