Add explanatory comments to the #endif part of multiple inclusion guards.
[mplayer/greg.git] / libmpdemux / video.c
blob311b72d9ed7dc3f5b469ae0d56e7cca0f0d81097
1 // read video frame
3 #include "config.h"
5 #include <stdio.h>
6 #ifdef 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 #ifdef USE_LIBAVCODEC_SO
26 #include <ffmpeg/avcodec.h>
27 #elif defined(USE_LIBAVCODEC)
28 #include "libavcodec/avcodec.h"
29 #else
30 #define FF_INPUT_BUFFER_PADDING_SIZE 8
31 #endif
33 /* biCompression constant */
34 #define BI_RGB 0L
36 #ifdef STREAMING_LIVE555
37 #include "demux_rtp.h"
38 #endif
40 static mp_mpeg_header_t picture;
42 static int telecine=0;
43 static float telecine_cnt=-2.5;
45 typedef enum {
46 VIDEO_MPEG12,
47 VIDEO_MPEG4,
48 VIDEO_H264,
49 VIDEO_VC1,
50 VIDEO_OTHER
51 } video_codec_t;
53 static video_codec_t find_video_codec(sh_video_t *sh_video)
55 demux_stream_t *d_video=sh_video->ds;
56 int fmt = d_video->demuxer->file_format;
58 if(
59 (fmt == DEMUXER_TYPE_PVA) ||
60 (fmt == DEMUXER_TYPE_MPEG_ES) ||
61 (fmt == DEMUXER_TYPE_MPEG_GXF) ||
62 (fmt == DEMUXER_TYPE_MPEG_PES) ||
64 (fmt == DEMUXER_TYPE_MPEG_PS || fmt == DEMUXER_TYPE_MPEG_TS) &&
65 ((! sh_video->format) || (sh_video->format==0x10000001) || (sh_video->format==0x10000002))
66 ) ||
67 (fmt == DEMUXER_TYPE_MPEG_TY)
68 #ifdef STREAMING_LIVE555
69 || ((fmt == DEMUXER_TYPE_RTP) && demux_is_mpeg_rtp_stream(d_video->demuxer))
70 #endif
72 return VIDEO_MPEG12;
73 else if((fmt == DEMUXER_TYPE_MPEG4_ES) ||
74 ((fmt == DEMUXER_TYPE_MPEG_TS) && (sh_video->format==0x10000004)) ||
75 ((fmt == DEMUXER_TYPE_MPEG_PS) && (sh_video->format==0x10000004))
77 return VIDEO_MPEG4;
78 else if((fmt == DEMUXER_TYPE_H264_ES) ||
79 ((fmt == DEMUXER_TYPE_MPEG_TS) && (sh_video->format==0x10000005)) ||
80 ((fmt == DEMUXER_TYPE_MPEG_PS) && (sh_video->format==0x10000005))
82 return VIDEO_H264;
83 else if((fmt == DEMUXER_TYPE_MPEG_PS || fmt == DEMUXER_TYPE_MPEG_TS) &&
84 (sh_video->format==mmioFOURCC('W', 'V', 'C', '1')))
85 return VIDEO_VC1;
86 else
87 return VIDEO_OTHER;
90 int video_read_properties(sh_video_t *sh_video){
91 demux_stream_t *d_video=sh_video->ds;
92 video_codec_t video_codec = find_video_codec(sh_video);
93 // Determine image properties:
94 switch(video_codec){
95 case VIDEO_OTHER: {
96 if((d_video->demuxer->file_format == DEMUXER_TYPE_ASF) || (d_video->demuxer->file_format == DEMUXER_TYPE_AVI)) {
97 // display info:
98 // in case no strf chunk has been seen in avi, we have no bitmap header
99 if(!sh_video->bih) return 0;
100 sh_video->format=sh_video->bih->biCompression;
101 sh_video->disp_w=sh_video->bih->biWidth;
102 sh_video->disp_h=abs(sh_video->bih->biHeight);
104 break;
106 case VIDEO_MPEG4: {
107 int pos = 0, vop_cnt=0, units[3];
108 videobuf_len=0; videobuf_code_len=0;
109 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for Video Object Start code... ");
110 while(1){
111 int i=sync_video_packet(d_video);
112 if(i<=0x11F) break; // found it!
113 if(!i || !skip_video_packet(d_video)){
114 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
115 return 0;
118 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
119 if(!videobuffer) {
120 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
121 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
122 else {
123 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail);
124 return 0;
127 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for Video Object Layer Start code... ");
128 while(1){
129 int i=sync_video_packet(d_video);
130 mp_msg(MSGT_DECVIDEO,MSGL_V,"M4V: 0x%X\n",i);
131 if(i>=0x120 && i<=0x12F) break; // found it!
132 if(!i || !read_video_packet(d_video)){
133 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
134 return 0;
137 pos = videobuf_len+4;
138 if(!read_video_packet(d_video)){
139 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read Video Object Layer Header\n");
140 return 0;
142 mp4_header_process_vol(&picture, &(videobuffer[pos]));
143 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK! FPS SEEMS TO BE %.3f\nSearching for Video Object Plane Start code... ", sh_video->fps);
144 mp4_init:
145 while(1){
146 int i=sync_video_packet(d_video);
147 if(i==0x1B6) break; // found it!
148 if(!i || !read_video_packet(d_video)){
149 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
150 return 0;
153 pos = videobuf_len+4;
154 if(!read_video_packet(d_video)){
155 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read Video Object Plane Header\n");
156 return 0;
158 mp4_header_process_vop(&picture, &(videobuffer[pos]));
159 units[vop_cnt] = picture.timeinc_unit;
160 vop_cnt++;
161 //mp_msg(MSGT_DECVIDEO,MSGL_V, "TYPE: %d, unit: %d\n", picture.picture_type, picture.timeinc_unit);
162 if(!picture.fps) {
163 int i, mn, md, mx, diff;
164 if(vop_cnt < 3)
165 goto mp4_init;
167 i=0;
168 mn = mx = units[0];
169 for(i=0; i<3; i++) {
170 if(units[i] < mn)
171 mn = units[i];
172 if(units[i] > mx)
173 mx = units[i];
175 md = mn;
176 for(i=0; i<3; i++) {
177 if((units[i] > mn) && (units[i] < mx))
178 md = units[i];
180 mp_msg(MSGT_DECVIDEO,MSGL_V, "MIN: %d, mid: %d, max: %d\n", mn, md, mx);
181 if(mx - md > md - mn)
182 diff = md - mn;
183 else
184 diff = mx - md;
185 if(diff > 0){
186 picture.fps = ((float)picture.timeinc_resolution) / diff;
187 mp_msg(MSGT_DECVIDEO,MSGL_V, "FPS seems to be: %f, resolution: %d, delta_units: %d\n", picture.fps, picture.timeinc_resolution, diff);
190 if(picture.fps) {
191 sh_video->fps=picture.fps;
192 sh_video->frametime=1.0/picture.fps;
193 mp_msg(MSGT_DECVIDEO,MSGL_INFO, "FPS seems to be: %f\n", picture.fps);
195 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
196 sh_video->format=0x10000004;
197 break;
199 case VIDEO_H264: {
200 int pos = 0;
201 videobuf_len=0; videobuf_code_len=0;
202 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for sequence parameter set... ");
203 while(1){
204 int i=sync_video_packet(d_video);
205 if((i&~0x60) == 0x107 && i != 0x107) break; // found it!
206 if(!i || !skip_video_packet(d_video)){
207 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
208 return 0;
211 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
212 if(!videobuffer) {
213 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
214 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
215 else {
216 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail);
217 return 0;
220 pos = videobuf_len+4;
221 if(!read_video_packet(d_video)){
222 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read sequence parameter set\n");
223 return 0;
225 h264_parse_sps(&picture, &(videobuffer[pos]), videobuf_len - pos);
226 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for picture parameter set... ");
227 while(1){
228 int i=sync_video_packet(d_video);
229 mp_msg(MSGT_DECVIDEO,MSGL_V,"H264: 0x%X\n",i);
230 if((i&~0x60) == 0x108 && i != 0x108) break; // found it!
231 if(!i || !read_video_packet(d_video)){
232 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
233 return 0;
236 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\nSearching for Slice... ");
237 while(1){
238 int i=sync_video_packet(d_video);
239 if((i&~0x60) == 0x101 || (i&~0x60) == 0x102 || (i&~0x60) == 0x105) break; // found it!
240 if(!i || !read_video_packet(d_video)){
241 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
242 return 0;
245 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
246 sh_video->format=0x10000005;
247 if(picture.fps) {
248 sh_video->fps=picture.fps;
249 sh_video->frametime=1.0/picture.fps;
250 mp_msg(MSGT_DECVIDEO,MSGL_INFO, "FPS seems to be: %f\n", picture.fps);
252 break;
254 case VIDEO_MPEG12: {
255 mpeg_header_parser:
256 // Find sequence_header first:
257 videobuf_len=0; videobuf_code_len=0;
258 telecine=0; telecine_cnt=-2.5;
259 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for sequence header... ");
260 while(1){
261 int i=sync_video_packet(d_video);
262 if(i==0x1B3) break; // found it!
263 if(!i || !skip_video_packet(d_video)){
264 if( mp_msg_test(MSGT_DECVIDEO,MSGL_V) ) mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
265 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_MpegNoSequHdr);
266 return 0;
269 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
270 // ========= Read & process sequence header & extension ============
271 if(!videobuffer) {
272 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
273 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
274 else {
275 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail);
276 return 0;
280 if(!read_video_packet(d_video)){
281 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_CannotReadMpegSequHdr);
282 return 0;
284 if(mp_header_process_sequence_header (&picture, &videobuffer[4])) {
285 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_BadMpegSequHdr);
286 goto mpeg_header_parser;
288 if(sync_video_packet(d_video)==0x1B5){ // next packet is seq. ext.
289 int pos=videobuf_len;
290 if(!read_video_packet(d_video)){
291 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_CannotReadMpegSequHdrEx);
292 return 0;
294 if(mp_header_process_extension (&picture, &videobuffer[pos+4])) {
295 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_BadMpegSequHdrEx);
296 return 0;
300 // display info:
301 sh_video->format=picture.mpeg1?0x10000001:0x10000002; // mpeg video
302 sh_video->fps=picture.fps;
303 if(!sh_video->fps){
304 sh_video->frametime=0;
305 } else {
306 sh_video->frametime=1.0/picture.fps;
308 sh_video->disp_w=picture.display_picture_width;
309 sh_video->disp_h=picture.display_picture_height;
310 // bitrate:
311 if(picture.bitrate!=0x3FFFF) // unspecified/VBR ?
312 sh_video->i_bps=picture.bitrate * 400 / 8;
313 // info:
314 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2,"mpeg bitrate: %d (%X)\n",picture.bitrate,picture.bitrate);
315 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"VIDEO: %s %dx%d (aspect %d) %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
316 picture.mpeg1?"MPEG1":"MPEG2",
317 sh_video->disp_w,sh_video->disp_h,
318 picture.aspect_ratio_information,
319 sh_video->fps,
320 sh_video->i_bps * 8 / 1000.0,
321 sh_video->i_bps / 1000.0 );
322 break;
324 case VIDEO_VC1: {
325 // Find sequence_header:
326 videobuf_len=0;
327 videobuf_code_len=0;
328 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"Searching for VC1 sequence header... ");
329 while(1){
330 int i=sync_video_packet(d_video);
331 if(i==0x10F) break; // found it!
332 if(!i || !skip_video_packet(d_video)){
333 if( mp_msg_test(MSGT_DECVIDEO,MSGL_V) ) mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
334 mp_msg(MSGT_DECVIDEO,MSGL_ERR, "Couldn't find VC-1 sequence header\n");
335 return 0;
338 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"found\n");
339 if(!videobuffer) {
340 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
341 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
342 else {
343 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail);
344 return 0;
347 if(!read_video_packet(d_video)){
348 mp_msg(MSGT_DECVIDEO,MSGL_ERR, "Couldn't read VC-1 sequence header!\n");
349 return 0;
352 while(1) {
353 int i=sync_video_packet(d_video);
354 if(i==0x10E) break; // found it!
355 if(!i || !skip_video_packet(d_video)){
356 mp_msg(MSGT_DECVIDEO,MSGL_V,"Couldn't find VC-1 entry point sync-code:(\n");
357 return 0;
360 if(!read_video_packet(d_video)){
361 mp_msg(MSGT_DECVIDEO,MSGL_V,"Couldn't read VC-1 entry point sync-code:(\n");
362 return 0;
365 if(mp_vc1_decode_sequence_header(&picture, &videobuffer[4], videobuf_len-4)) {
366 sh_video->bih = (BITMAPINFOHEADER *) calloc(1, sizeof(BITMAPINFOHEADER) + videobuf_len);
367 if(sh_video->bih == NULL) {
368 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Couldn't alloc %d bytes for VC-1 extradata!\n", sizeof(BITMAPINFOHEADER) + videobuf_len);
369 return 0;
371 sh_video->bih->biSize= sizeof(BITMAPINFOHEADER) + videobuf_len;
372 memcpy(sh_video->bih + 1, videobuffer, videobuf_len);
373 sh_video->bih->biCompression = sh_video->format;
374 sh_video->bih->biWidth = sh_video->disp_w = picture.display_picture_width;
375 sh_video->bih->biHeight = sh_video->disp_h = picture.display_picture_height;
376 if(picture.fps > 0) {
377 sh_video->frametime=1.0/picture.fps;
378 sh_video->fps = picture.fps;
380 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"VIDEO: VC-1 %dx%d, %5.3f fps, header len: %d\n",
381 sh_video->disp_w, sh_video->disp_h, sh_video->fps, videobuf_len);
383 break;
385 } // switch(file_format)
387 return 1;
390 void ty_processuserdata( unsigned char* buf, int len );
392 static void process_userdata(unsigned char* buf,int len){
393 int i;
394 /* if the user data starts with "CC", assume it is a CC info packet */
395 if(len>2 && buf[0]=='C' && buf[1]=='C'){
396 // mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"video.c: process_userdata() detected Closed Captions!\n");
397 subcc_process_data(buf+2,len-2);
399 if( len > 2 && buf[ 0 ] == 'T' && buf[ 1 ] == 'Y' )
401 ty_processuserdata( buf + 2, len - 2 );
402 return;
404 if(verbose<2) return;
405 fprintf(stderr, "user_data: len=%3d %02X %02X %02X %02X '",
406 len, buf[0], buf[1], buf[2], buf[3]);
407 for(i=0;i<len;i++)
408 // if(buf[i]>=32 && buf[i]<127) fputc(buf[i], stderr);
409 if(buf[i]&0x60) fputc(buf[i]&0x7F, stderr);
410 fprintf(stderr, "'\n");
413 int video_read_frame(sh_video_t* sh_video,float* frame_time_ptr,unsigned char** start,int force_fps){
414 demux_stream_t *d_video=sh_video->ds;
415 demuxer_t *demuxer=d_video->demuxer;
416 float frame_time=1;
417 float pts1=d_video->pts;
418 float pts=0;
419 int picture_coding_type=0;
420 int in_size=0;
421 video_codec_t video_codec = find_video_codec(sh_video);
423 *start=NULL;
425 if(video_codec == VIDEO_MPEG12){
426 int in_frame=0;
427 //float newfps;
428 //videobuf_len=0;
429 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
430 int i=sync_video_packet(d_video);
431 //void* buffer=&videobuffer[videobuf_len+4];
432 int start=videobuf_len+4;
433 if(in_frame){
434 if(i<0x101 || i>=0x1B0){ // not slice code -> end of frame
435 if(!i) return -1; // EOF
436 break;
438 } else {
439 if(i==0x100){
440 pts=d_video->pts;
441 d_video->pts=0;
443 if(i>=0x101 && i<0x1B0) in_frame=1; // picture startcode
444 else if(!i) return -1; // EOF
446 if(!read_video_packet(d_video)) return -1; // EOF
447 // process headers:
448 switch(i){
449 case 0x1B3: mp_header_process_sequence_header (&picture, &videobuffer[start]);break;
450 case 0x1B5: mp_header_process_extension (&picture, &videobuffer[start]);break;
451 case 0x1B2: process_userdata (&videobuffer[start], videobuf_len-start);break;
452 case 0x100: picture_coding_type=(videobuffer[start+1] >> 3) & 7;break;
456 *start=videobuffer; in_size=videobuf_len;
458 // get mpeg fps:
459 if(sh_video->fps!=picture.fps) if(!force_fps && !telecine){
460 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);
461 sh_video->fps=picture.fps;
462 sh_video->frametime=1.0/picture.fps;
465 // fix mpeg2 frametime:
466 frame_time=(picture.display_time)*0.01f;
467 picture.display_time=100;
468 videobuf_len=0;
470 telecine_cnt*=0.9; // drift out error
471 telecine_cnt+=frame_time-5.0/4.0;
472 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"\r telecine = %3.1f %5.3f \n",frame_time,telecine_cnt);
474 if(telecine){
475 frame_time=1;
476 if(telecine_cnt<-1.5 || telecine_cnt>1.5){
477 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_LeaveTelecineMode);
478 telecine=0;
480 } else
481 if(telecine_cnt>-0.5 && telecine_cnt<0.5 && !force_fps){
482 sh_video->fps=sh_video->fps*4/5;
483 sh_video->frametime=sh_video->frametime*5/4;
484 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_EnterTelecineMode);
485 telecine=1;
487 } else if(video_codec == VIDEO_MPEG4){
488 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
489 int i=sync_video_packet(d_video);
490 if(!i) return -1;
491 if(!read_video_packet(d_video)) return -1; // EOF
492 if(i==0x1B6) break;
494 *start=videobuffer; in_size=videobuf_len;
495 videobuf_len=0;
496 } else if(video_codec == VIDEO_H264){
497 int in_picture = 0;
498 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
499 int i=sync_video_packet(d_video);
500 int pos = videobuf_len+4;
501 if(!i) return -1;
502 if(!read_video_packet(d_video)) return -1; // EOF
503 if((i&~0x60) == 0x107 && i != 0x107) {
504 h264_parse_sps(&picture, &(videobuffer[pos]), videobuf_len - pos);
505 if(picture.fps > 0) {
506 sh_video->fps=picture.fps;
507 sh_video->frametime=1.0/picture.fps;
509 i=sync_video_packet(d_video);
510 if(!i) return -1;
511 if(!read_video_packet(d_video)) return -1; // EOF
514 // here starts the access unit end detection code
515 // see the mail on MPlayer-dev-eng for details:
516 // Date: Sat, 17 Sep 2005 11:24:06 +0200
517 // Subject: Re: [MPlayer-dev-eng] [RFC] h264 ES parser problems
518 // Message-ID: <20050917092406.GA7699@rz.uni-karlsruhe.de>
519 if((i&~0x60) == 0x101 || (i&~0x60) == 0x102 || (i&~0x60) == 0x105)
520 // found VCL NAL with slice header i.e. start of current primary coded
521 // picture, so start scanning for the end now
522 in_picture = 1;
523 if (in_picture) {
524 i = sync_video_packet(d_video) & ~0x60; // code of next packet
525 if(i == 0x106 || i == 0x109) break; // SEI or access unit delim.
526 if(i == 0x101 || i == 0x102 || i == 0x105) {
527 // assuming arbitrary slice ordering is not allowed, the
528 // first_mb_in_slice (golomb encoded) value should be 0 then
529 // for the first VCL NAL in a picture
530 if (demux_peekc(d_video) & 0x80)
531 break;
535 *start=videobuffer; in_size=videobuf_len;
536 videobuf_len=0;
537 } else if(video_codec == VIDEO_VC1) {
538 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE) {
539 int i=sync_video_packet(d_video);
540 if(!i) return -1;
541 if(!read_video_packet(d_video)) return -1; // EOF
542 if(i==0x10D) break;
544 *start=videobuffer;
545 in_size=videobuf_len;
546 videobuf_len=0;
547 } else {
548 // frame-based file formats: (AVI,ASF,MOV)
549 in_size=ds_get_packet(d_video,start);
550 if(in_size<0) return -1; // EOF
554 //------------------------ frame decoded. --------------------
556 // Increase video timers:
557 sh_video->num_frames+=frame_time;
558 ++sh_video->num_frames_decoded;
560 frame_time*=sh_video->frametime;
562 // override frame_time for variable/unknown FPS formats:
563 if(!force_fps) switch(demuxer->file_format){
564 case DEMUXER_TYPE_GIF:
565 case DEMUXER_TYPE_MATROSKA:
566 if(d_video->pts>0 && pts1>0 && d_video->pts>pts1)
567 frame_time=d_video->pts-pts1;
568 break;
569 case DEMUXER_TYPE_TV:
570 case DEMUXER_TYPE_MOV:
571 case DEMUXER_TYPE_FILM:
572 case DEMUXER_TYPE_VIVO:
573 case DEMUXER_TYPE_OGG:
574 case DEMUXER_TYPE_ASF: {
575 double next_pts = ds_get_next_pts(d_video);
576 double d= (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts-pts1;
577 if(d>=0){
578 if(d>0){
579 if((int)sh_video->fps==1000)
580 mp_msg(MSGT_CPLAYER,MSGL_V,"\navg. framerate: %d fps \n",(int)(1.0f/d));
581 sh_video->frametime=d; // 1ms
582 sh_video->fps=1.0f/d;
584 frame_time = d;
585 } else {
586 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);
587 // frame_time = 1/25.0;
590 break;
591 case DEMUXER_TYPE_LAVF:
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;