Use extra_cflags variable instead of CFLAGS to add system-specific CFLAGS.
[mplayer/glamo.git] / libmpdemux / video.c
blob58bcc6ad76513da1682f8abc26718349f0ac4d85
1 // read video frame
3 #include "config.h"
5 #include <stdio.h>
6 #if HAVE_MALLOC_H
7 #include <malloc.h>
8 #endif
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
13 #include "mp_msg.h"
14 #include "help_mp.h"
16 #include "stream/stream.h"
17 #include "demuxer.h"
18 #include "stheader.h"
19 #include "parse_es.h"
20 #include "mpeg_hdr.h"
22 /* sub_cc (closed captions)*/
23 #include "sub_cc.h"
25 /* biCompression constant */
26 #define BI_RGB 0L
28 #ifdef CONFIG_LIVE555
29 #include "demux_rtp.h"
30 #endif
32 static mp_mpeg_header_t picture;
34 static int telecine=0;
35 static float telecine_cnt=-2.5;
37 typedef enum {
38 VIDEO_MPEG12,
39 VIDEO_MPEG4,
40 VIDEO_H264,
41 VIDEO_VC1,
42 VIDEO_OTHER
43 } video_codec_t;
45 static video_codec_t find_video_codec(sh_video_t *sh_video)
47 demux_stream_t *d_video=sh_video->ds;
48 int fmt = d_video->demuxer->file_format;
50 if(
51 (fmt == DEMUXER_TYPE_PVA) ||
52 (fmt == DEMUXER_TYPE_MPEG_ES) ||
53 (fmt == DEMUXER_TYPE_MPEG_GXF) ||
54 (fmt == DEMUXER_TYPE_MPEG_PES) ||
56 (fmt == DEMUXER_TYPE_MPEG_PS || fmt == DEMUXER_TYPE_MPEG_TS) &&
57 ((! sh_video->format) || (sh_video->format==0x10000001) || (sh_video->format==0x10000002))
58 ) ||
59 (fmt == DEMUXER_TYPE_MPEG_TY)
60 #ifdef CONFIG_LIVE555
61 || ((fmt == DEMUXER_TYPE_RTP) && demux_is_mpeg_rtp_stream(d_video->demuxer))
62 #endif
64 return VIDEO_MPEG12;
65 else if((fmt == DEMUXER_TYPE_MPEG4_ES) ||
66 ((fmt == DEMUXER_TYPE_MPEG_TS) && (sh_video->format==0x10000004)) ||
67 ((fmt == DEMUXER_TYPE_MPEG_PS) && (sh_video->format==0x10000004))
69 return VIDEO_MPEG4;
70 else if((fmt == DEMUXER_TYPE_H264_ES) ||
71 ((fmt == DEMUXER_TYPE_MPEG_TS) && (sh_video->format==0x10000005)) ||
72 ((fmt == DEMUXER_TYPE_MPEG_PS) && (sh_video->format==0x10000005))
74 return VIDEO_H264;
75 else if((fmt == DEMUXER_TYPE_MPEG_PS || fmt == DEMUXER_TYPE_MPEG_TS) &&
76 (sh_video->format==mmioFOURCC('W', 'V', 'C', '1')))
77 return VIDEO_VC1;
78 else if (fmt == DEMUXER_TYPE_ASF && sh_video->bih && sh_video->bih->biCompression == mmioFOURCC('D', 'V', 'R', ' '))
79 return VIDEO_MPEG12;
80 else
81 return VIDEO_OTHER;
84 int video_read_properties(sh_video_t *sh_video){
85 demux_stream_t *d_video=sh_video->ds;
86 video_codec_t video_codec = find_video_codec(sh_video);
87 // Determine image properties:
88 switch(video_codec){
89 case VIDEO_OTHER: {
90 if((d_video->demuxer->file_format == DEMUXER_TYPE_ASF) || (d_video->demuxer->file_format == DEMUXER_TYPE_AVI)) {
91 // display info:
92 // in case no strf chunk has been seen in avi, we have no bitmap header
93 if(!sh_video->bih) return 0;
94 sh_video->format=sh_video->bih->biCompression;
95 sh_video->disp_w=sh_video->bih->biWidth;
96 sh_video->disp_h=abs(sh_video->bih->biHeight);
98 break;
100 case VIDEO_MPEG4: {
101 int pos = 0, vop_cnt=0, units[3];
102 videobuf_len=0; videobuf_code_len=0;
103 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for Video Object Start code... ");
104 while(1){
105 int i=sync_video_packet(d_video);
106 if(i<=0x11F) break; // found it!
107 if(!i || !skip_video_packet(d_video)){
108 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
109 return 0;
112 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
113 if(!videobuffer) {
114 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
115 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
116 else {
117 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail);
118 return 0;
121 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for Video Object Layer Start code... ");
122 while(1){
123 int i=sync_video_packet(d_video);
124 mp_msg(MSGT_DECVIDEO,MSGL_V,"M4V: 0x%X\n",i);
125 if(i>=0x120 && i<=0x12F) break; // found it!
126 if(!i || !read_video_packet(d_video)){
127 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
128 return 0;
131 pos = videobuf_len+4;
132 if(!read_video_packet(d_video)){
133 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read Video Object Layer Header\n");
134 return 0;
136 mp4_header_process_vol(&picture, &(videobuffer[pos]));
137 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK! FPS SEEMS TO BE %.3f\nSearching for Video Object Plane Start code... ", sh_video->fps);
138 mp4_init:
139 while(1){
140 int i=sync_video_packet(d_video);
141 if(i==0x1B6) break; // found it!
142 if(!i || !read_video_packet(d_video)){
143 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
144 return 0;
147 pos = videobuf_len+4;
148 if(!read_video_packet(d_video)){
149 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read Video Object Plane Header\n");
150 return 0;
152 mp4_header_process_vop(&picture, &(videobuffer[pos]));
153 units[vop_cnt] = picture.timeinc_unit;
154 vop_cnt++;
155 //mp_msg(MSGT_DECVIDEO,MSGL_V, "TYPE: %d, unit: %d\n", picture.picture_type, picture.timeinc_unit);
156 if(!picture.fps) {
157 int i, mn, md, mx, diff;
158 if(vop_cnt < 3)
159 goto mp4_init;
161 i=0;
162 mn = mx = units[0];
163 for(i=0; i<3; i++) {
164 if(units[i] < mn)
165 mn = units[i];
166 if(units[i] > mx)
167 mx = units[i];
169 md = mn;
170 for(i=0; i<3; i++) {
171 if((units[i] > mn) && (units[i] < mx))
172 md = units[i];
174 mp_msg(MSGT_DECVIDEO,MSGL_V, "MIN: %d, mid: %d, max: %d\n", mn, md, mx);
175 if(mx - md > md - mn)
176 diff = md - mn;
177 else
178 diff = mx - md;
179 if(diff > 0){
180 picture.fps = ((float)picture.timeinc_resolution) / diff;
181 mp_msg(MSGT_DECVIDEO,MSGL_V, "FPS seems to be: %f, resolution: %d, delta_units: %d\n", picture.fps, picture.timeinc_resolution, diff);
184 if(picture.fps) {
185 sh_video->fps=picture.fps;
186 sh_video->frametime=1.0/picture.fps;
187 mp_msg(MSGT_DECVIDEO,MSGL_INFO, "FPS seems to be: %f\n", picture.fps);
189 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
190 sh_video->format=0x10000004;
191 break;
193 case VIDEO_H264: {
194 int pos = 0;
195 videobuf_len=0; videobuf_code_len=0;
196 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for sequence parameter set... ");
197 while(1){
198 int i=sync_video_packet(d_video);
199 if((i&~0x60) == 0x107 && i != 0x107) break; // found it!
200 if(!i || !skip_video_packet(d_video)){
201 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
202 return 0;
205 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
206 if(!videobuffer) {
207 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
208 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
209 else {
210 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail);
211 return 0;
214 pos = videobuf_len+4;
215 if(!read_video_packet(d_video)){
216 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read sequence parameter set\n");
217 return 0;
219 h264_parse_sps(&picture, &(videobuffer[pos]), videobuf_len - pos);
220 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for picture parameter set... ");
221 while(1){
222 int i=sync_video_packet(d_video);
223 mp_msg(MSGT_DECVIDEO,MSGL_V,"H264: 0x%X\n",i);
224 if((i&~0x60) == 0x108 && i != 0x108) break; // found it!
225 if(!i || !read_video_packet(d_video)){
226 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
227 return 0;
230 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\nSearching for Slice... ");
231 while(1){
232 int i=sync_video_packet(d_video);
233 if((i&~0x60) == 0x101 || (i&~0x60) == 0x102 || (i&~0x60) == 0x105) break; // found it!
234 if(!i || !read_video_packet(d_video)){
235 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
236 return 0;
239 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
240 sh_video->format=0x10000005;
241 if(picture.fps) {
242 sh_video->fps=picture.fps;
243 sh_video->frametime=1.0/picture.fps;
244 mp_msg(MSGT_DECVIDEO,MSGL_INFO, "FPS seems to be: %f\n", picture.fps);
246 break;
248 case VIDEO_MPEG12: {
249 if (d_video->demuxer->file_format == DEMUXER_TYPE_ASF) { // DVR-MS
250 if(!sh_video->bih) return 0;
251 sh_video->format=sh_video->bih->biCompression;
253 mpeg_header_parser:
254 // Find sequence_header first:
255 videobuf_len=0; videobuf_code_len=0;
256 telecine=0; telecine_cnt=-2.5;
257 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for sequence header... ");
258 while(1){
259 int i=sync_video_packet(d_video);
260 if(i==0x1B3) break; // found it!
261 if(!i || !skip_video_packet(d_video)){
262 if( mp_msg_test(MSGT_DECVIDEO,MSGL_V) ) mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
263 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_MpegNoSequHdr);
264 return 0;
267 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
268 // ========= Read & process sequence header & extension ============
269 if(!videobuffer) {
270 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
271 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
272 else {
273 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail);
274 return 0;
278 if(!read_video_packet(d_video)){
279 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_CannotReadMpegSequHdr);
280 return 0;
282 if(mp_header_process_sequence_header (&picture, &videobuffer[4])) {
283 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_BadMpegSequHdr);
284 goto mpeg_header_parser;
286 if(sync_video_packet(d_video)==0x1B5){ // next packet is seq. ext.
287 int pos=videobuf_len;
288 if(!read_video_packet(d_video)){
289 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_CannotReadMpegSequHdrEx);
290 return 0;
292 if(mp_header_process_extension (&picture, &videobuffer[pos+4])) {
293 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_BadMpegSequHdrEx);
294 return 0;
298 // display info:
299 sh_video->format=picture.mpeg1?0x10000001:0x10000002; // mpeg video
300 sh_video->fps=picture.fps;
301 if(!sh_video->fps){
302 sh_video->frametime=0;
303 } else {
304 sh_video->frametime=1.0/picture.fps;
306 sh_video->disp_w=picture.display_picture_width;
307 sh_video->disp_h=picture.display_picture_height;
308 // bitrate:
309 if(picture.bitrate!=0x3FFFF) // unspecified/VBR ?
310 sh_video->i_bps=picture.bitrate * 400 / 8;
311 // info:
312 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2,"mpeg bitrate: %d (%X)\n",picture.bitrate,picture.bitrate);
313 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"VIDEO: %s %dx%d (aspect %d) %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
314 picture.mpeg1?"MPEG1":"MPEG2",
315 sh_video->disp_w,sh_video->disp_h,
316 picture.aspect_ratio_information,
317 sh_video->fps,
318 sh_video->i_bps * 8 / 1000.0,
319 sh_video->i_bps / 1000.0 );
320 break;
322 case VIDEO_VC1: {
323 // Find sequence_header:
324 videobuf_len=0;
325 videobuf_code_len=0;
326 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"Searching for VC1 sequence header... ");
327 while(1){
328 int i=sync_video_packet(d_video);
329 if(i==0x10F) break; // found it!
330 if(!i || !skip_video_packet(d_video)){
331 if( mp_msg_test(MSGT_DECVIDEO,MSGL_V) ) mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
332 mp_msg(MSGT_DECVIDEO,MSGL_ERR, "Couldn't find VC-1 sequence header\n");
333 return 0;
336 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"found\n");
337 if(!videobuffer) {
338 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
339 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
340 else {
341 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail);
342 return 0;
345 if(!read_video_packet(d_video)){
346 mp_msg(MSGT_DECVIDEO,MSGL_ERR, "Couldn't read VC-1 sequence header!\n");
347 return 0;
350 while(1) {
351 int i=sync_video_packet(d_video);
352 if(i==0x10E) break; // found it!
353 if(!i || !skip_video_packet(d_video)){
354 mp_msg(MSGT_DECVIDEO,MSGL_V,"Couldn't find VC-1 entry point sync-code:(\n");
355 return 0;
358 if(!read_video_packet(d_video)){
359 mp_msg(MSGT_DECVIDEO,MSGL_V,"Couldn't read VC-1 entry point sync-code:(\n");
360 return 0;
363 if(mp_vc1_decode_sequence_header(&picture, &videobuffer[4], videobuf_len-4)) {
364 sh_video->bih = (BITMAPINFOHEADER *) calloc(1, sizeof(BITMAPINFOHEADER) + videobuf_len);
365 if(sh_video->bih == NULL) {
366 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Couldn't alloc %d bytes for VC-1 extradata!\n", sizeof(BITMAPINFOHEADER) + videobuf_len);
367 return 0;
369 sh_video->bih->biSize= sizeof(BITMAPINFOHEADER) + videobuf_len;
370 memcpy(sh_video->bih + 1, videobuffer, videobuf_len);
371 sh_video->bih->biCompression = sh_video->format;
372 sh_video->bih->biWidth = sh_video->disp_w = picture.display_picture_width;
373 sh_video->bih->biHeight = sh_video->disp_h = picture.display_picture_height;
374 if(picture.fps > 0) {
375 sh_video->frametime=1.0/picture.fps;
376 sh_video->fps = picture.fps;
378 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"VIDEO: VC-1 %dx%d, %5.3f fps, header len: %d\n",
379 sh_video->disp_w, sh_video->disp_h, sh_video->fps, videobuf_len);
381 break;
383 } // switch(file_format)
385 return 1;
388 void ty_processuserdata( unsigned char* buf, int len );
390 static void process_userdata(unsigned char* buf,int len){
391 int i;
392 /* if the user data starts with "CC", assume it is a CC info packet */
393 if(len>2 && buf[0]=='C' && buf[1]=='C'){
394 // mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"video.c: process_userdata() detected Closed Captions!\n");
395 subcc_process_data(buf+2,len-2);
397 if( len > 2 && buf[ 0 ] == 'T' && buf[ 1 ] == 'Y' )
399 ty_processuserdata( buf + 2, len - 2 );
400 return;
402 if(verbose<2) return;
403 fprintf(stderr, "user_data: len=%3d %02X %02X %02X %02X '",
404 len, buf[0], buf[1], buf[2], buf[3]);
405 for(i=0;i<len;i++)
406 // if(buf[i]>=32 && buf[i]<127) fputc(buf[i], stderr);
407 if(buf[i]&0x60) fputc(buf[i]&0x7F, stderr);
408 fprintf(stderr, "'\n");
411 int video_read_frame(sh_video_t* sh_video,float* frame_time_ptr,unsigned char** start,int force_fps){
412 demux_stream_t *d_video=sh_video->ds;
413 demuxer_t *demuxer=d_video->demuxer;
414 float frame_time=1;
415 float pts1=d_video->pts;
416 float pts=0;
417 int picture_coding_type=0;
418 int in_size=0;
419 video_codec_t video_codec = find_video_codec(sh_video);
421 *start=NULL;
423 if(video_codec == VIDEO_MPEG12){
424 int in_frame=0;
425 //float newfps;
426 //videobuf_len=0;
427 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
428 int i=sync_video_packet(d_video);
429 //void* buffer=&videobuffer[videobuf_len+4];
430 int start=videobuf_len+4;
431 if(in_frame){
432 if(i<0x101 || i>=0x1B0){ // not slice code -> end of frame
433 if(!i) return -1; // EOF
434 break;
436 } else {
437 if(i==0x100){
438 pts=d_video->pts;
439 d_video->pts=0;
441 if(i>=0x101 && i<0x1B0) in_frame=1; // picture startcode
442 else if(!i) return -1; // EOF
444 if(!read_video_packet(d_video)) return -1; // EOF
445 // process headers:
446 switch(i){
447 case 0x1B3: mp_header_process_sequence_header (&picture, &videobuffer[start]);break;
448 case 0x1B5: mp_header_process_extension (&picture, &videobuffer[start]);break;
449 case 0x1B2: process_userdata (&videobuffer[start], videobuf_len-start);break;
450 case 0x100: picture_coding_type=(videobuffer[start+1] >> 3) & 7;break;
454 *start=videobuffer; in_size=videobuf_len;
456 // get mpeg fps:
457 if(sh_video->fps!=picture.fps) if(!force_fps && !telecine){
458 mp_msg(MSGT_CPLAYER,MSGL_WARN,"Warning! FPS changed %5.3f -> %5.3f (%f) [%d] \n",sh_video->fps,picture.fps,sh_video->fps-picture.fps,picture.frame_rate_code);
459 sh_video->fps=picture.fps;
460 sh_video->frametime=1.0/picture.fps;
463 // fix mpeg2 frametime:
464 frame_time=(picture.display_time)*0.01f;
465 picture.display_time=100;
466 videobuf_len=0;
468 telecine_cnt*=0.9; // drift out error
469 telecine_cnt+=frame_time-5.0/4.0;
470 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"\r telecine = %3.1f %5.3f \n",frame_time,telecine_cnt);
472 if(telecine){
473 frame_time=1;
474 if(telecine_cnt<-1.5 || telecine_cnt>1.5){
475 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_LeaveTelecineMode);
476 telecine=0;
478 } else
479 if(telecine_cnt>-0.5 && telecine_cnt<0.5 && !force_fps){
480 sh_video->fps=sh_video->fps*4/5;
481 sh_video->frametime=sh_video->frametime*5/4;
482 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_EnterTelecineMode);
483 telecine=1;
485 } else if(video_codec == VIDEO_MPEG4){
486 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
487 int i=sync_video_packet(d_video);
488 if(!i) return -1;
489 if(!read_video_packet(d_video)) return -1; // EOF
490 if(i==0x1B6) break;
492 *start=videobuffer; in_size=videobuf_len;
493 videobuf_len=0;
494 } else if(video_codec == VIDEO_H264){
495 int in_picture = 0;
496 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
497 int i=sync_video_packet(d_video);
498 int pos = videobuf_len+4;
499 if(!i) return -1;
500 if(!read_video_packet(d_video)) return -1; // EOF
501 if((i&~0x60) == 0x107 && i != 0x107) {
502 h264_parse_sps(&picture, &(videobuffer[pos]), videobuf_len - pos);
503 if(picture.fps > 0) {
504 sh_video->fps=picture.fps;
505 sh_video->frametime=1.0/picture.fps;
507 i=sync_video_packet(d_video);
508 if(!i) return -1;
509 if(!read_video_packet(d_video)) return -1; // EOF
512 // here starts the access unit end detection code
513 // see the mail on MPlayer-dev-eng for details:
514 // Date: Sat, 17 Sep 2005 11:24:06 +0200
515 // Subject: Re: [MPlayer-dev-eng] [RFC] h264 ES parser problems
516 // Message-ID: <20050917092406.GA7699@rz.uni-karlsruhe.de>
517 if((i&~0x60) == 0x101 || (i&~0x60) == 0x102 || (i&~0x60) == 0x105)
518 // found VCL NAL with slice header i.e. start of current primary coded
519 // picture, so start scanning for the end now
520 in_picture = 1;
521 if (in_picture) {
522 i = sync_video_packet(d_video) & ~0x60; // code of next packet
523 if(i == 0x106 || i == 0x109) break; // SEI or access unit delim.
524 if(i == 0x101 || i == 0x102 || i == 0x105) {
525 // assuming arbitrary slice ordering is not allowed, the
526 // first_mb_in_slice (golomb encoded) value should be 0 then
527 // for the first VCL NAL in a picture
528 if (demux_peekc(d_video) & 0x80)
529 break;
533 *start=videobuffer; in_size=videobuf_len;
534 videobuf_len=0;
535 } else if(video_codec == VIDEO_VC1) {
536 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE) {
537 int i=sync_video_packet(d_video);
538 if(!i) return -1;
539 if(!read_video_packet(d_video)) return -1; // EOF
540 if(i==0x10D) break;
542 *start=videobuffer;
543 in_size=videobuf_len;
544 videobuf_len=0;
545 } else {
546 // frame-based file formats: (AVI,ASF,MOV)
547 in_size=ds_get_packet(d_video,start);
548 if(in_size<0) return -1; // EOF
552 //------------------------ frame decoded. --------------------
554 // Increase video timers:
555 sh_video->num_frames+=frame_time;
556 ++sh_video->num_frames_decoded;
558 frame_time*=sh_video->frametime;
560 // override frame_time for variable/unknown FPS formats:
561 if(!force_fps) switch(demuxer->file_format){
562 case DEMUXER_TYPE_GIF:
563 case DEMUXER_TYPE_MATROSKA:
564 case DEMUXER_TYPE_MNG:
565 if(d_video->pts>0 && pts1>0 && d_video->pts>pts1)
566 frame_time=d_video->pts-pts1;
567 break;
568 case DEMUXER_TYPE_TV:
569 case DEMUXER_TYPE_MOV:
570 case DEMUXER_TYPE_FILM:
571 case DEMUXER_TYPE_VIVO:
572 case DEMUXER_TYPE_OGG:
573 case DEMUXER_TYPE_ASF: {
574 double next_pts = ds_get_next_pts(d_video);
575 double d= (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts-pts1;
576 if(d>=0){
577 if(d>0){
578 if((int)sh_video->fps==1000)
579 mp_msg(MSGT_CPLAYER,MSGL_V,"\navg. framerate: %d fps \n",(int)(1.0f/d));
580 sh_video->frametime=d; // 1ms
581 sh_video->fps=1.0f/d;
583 frame_time = d;
584 } else {
585 mp_msg(MSGT_CPLAYER,MSGL_WARN,"\nInvalid frame duration value (%5.3f/%5.3f => %5.3f). Defaulting to %5.3f sec.\n",d_video->pts,next_pts,d,frame_time);
586 // frame_time = 1/25.0;
589 break;
590 case DEMUXER_TYPE_LAVF:
591 case DEMUXER_TYPE_LAVF_PREFERRED:
592 if((int)sh_video->fps==1000 || (int)sh_video->fps<=1){
593 double next_pts = ds_get_next_pts(d_video);
594 double d= (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts-pts1;
595 if(d>=0){
596 frame_time = d;
599 break;
600 case DEMUXER_TYPE_REAL:
602 double next_pts = ds_get_next_pts(d_video);
603 double d = (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts - pts1;
605 frame_time = (d >= 0 && pts1 > 0) ? d : 0.001;
607 break;
610 if(video_codec == VIDEO_MPEG12){
611 sh_video->pts+=frame_time;
612 if(picture_coding_type==1)
613 d_video->flags |= 1;
614 if(picture_coding_type<=2 && sh_video->i_pts){
615 sh_video->pts=sh_video->i_pts;
616 sh_video->i_pts=pts;
617 } else {
618 if(pts){
619 if(picture_coding_type<=2) sh_video->i_pts=pts;
620 else sh_video->pts=pts;
623 } else
624 sh_video->pts=d_video->pts;
626 if(frame_time_ptr) *frame_time_ptr=frame_time;
627 return in_size;