Merge svn changes up to r26979
[mplayer.git] / libmpdemux / video.c
blobb0f72c515f1ffcec26241cee4eb5ee844db3563a
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
26 #include "libavcodec/avcodec.h"
27 #else
28 #define FF_INPUT_BUFFER_PADDING_SIZE 8
29 #endif
31 /* biCompression constant */
32 #define BI_RGB 0L
34 #ifdef STREAMING_LIVE555
35 #include "demux_rtp.h"
36 #endif
38 static mp_mpeg_header_t picture;
40 static int telecine=0;
41 static float telecine_cnt=-2.5;
43 typedef enum {
44 VIDEO_MPEG12,
45 VIDEO_MPEG4,
46 VIDEO_H264,
47 VIDEO_VC1,
48 VIDEO_OTHER
49 } video_codec_t;
51 static video_codec_t find_video_codec(sh_video_t *sh_video)
53 demux_stream_t *d_video=sh_video->ds;
54 int fmt = d_video->demuxer->file_format;
56 if(
57 (fmt == DEMUXER_TYPE_PVA) ||
58 (fmt == DEMUXER_TYPE_MPEG_ES) ||
59 (fmt == DEMUXER_TYPE_MPEG_GXF) ||
60 (fmt == DEMUXER_TYPE_MPEG_PES) ||
62 (fmt == DEMUXER_TYPE_MPEG_PS || fmt == DEMUXER_TYPE_MPEG_TS) &&
63 ((! sh_video->format) || (sh_video->format==0x10000001) || (sh_video->format==0x10000002))
64 ) ||
65 (fmt == DEMUXER_TYPE_MPEG_TY)
66 #ifdef STREAMING_LIVE555
67 || ((fmt == DEMUXER_TYPE_RTP) && demux_is_mpeg_rtp_stream(d_video->demuxer))
68 #endif
70 return VIDEO_MPEG12;
71 else if((fmt == DEMUXER_TYPE_MPEG4_ES) ||
72 ((fmt == DEMUXER_TYPE_MPEG_TS) && (sh_video->format==0x10000004)) ||
73 ((fmt == DEMUXER_TYPE_MPEG_PS) && (sh_video->format==0x10000004))
75 return VIDEO_MPEG4;
76 else if((fmt == DEMUXER_TYPE_H264_ES) ||
77 ((fmt == DEMUXER_TYPE_MPEG_TS) && (sh_video->format==0x10000005)) ||
78 ((fmt == DEMUXER_TYPE_MPEG_PS) && (sh_video->format==0x10000005))
80 return VIDEO_H264;
81 else if((fmt == DEMUXER_TYPE_MPEG_PS || fmt == DEMUXER_TYPE_MPEG_TS) &&
82 (sh_video->format==mmioFOURCC('W', 'V', 'C', '1')))
83 return VIDEO_VC1;
84 else if (fmt == DEMUXER_TYPE_ASF && sh_video->bih && sh_video->bih->biCompression == mmioFOURCC('D', 'V', 'R', ' '))
85 return VIDEO_MPEG12;
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 if (d_video->demuxer->file_format == DEMUXER_TYPE_ASF) { // DVR-MS
256 if(!sh_video->bih) return 0;
257 sh_video->format=sh_video->bih->biCompression;
259 mpeg_header_parser:
260 // Find sequence_header first:
261 videobuf_len=0; videobuf_code_len=0;
262 telecine=0; telecine_cnt=-2.5;
263 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for sequence header... ");
264 while(1){
265 int i=sync_video_packet(d_video);
266 if(i==0x1B3) break; // found it!
267 if(!i || !skip_video_packet(d_video)){
268 if( mp_msg_test(MSGT_DECVIDEO,MSGL_V) ) mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
269 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_MpegNoSequHdr);
270 return 0;
273 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
274 // ========= Read & process sequence header & extension ============
275 if(!videobuffer) {
276 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
277 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
278 else {
279 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail);
280 return 0;
284 if(!read_video_packet(d_video)){
285 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_CannotReadMpegSequHdr);
286 return 0;
288 if(mp_header_process_sequence_header (&picture, &videobuffer[4])) {
289 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_BadMpegSequHdr);
290 goto mpeg_header_parser;
292 if(sync_video_packet(d_video)==0x1B5){ // next packet is seq. ext.
293 int pos=videobuf_len;
294 if(!read_video_packet(d_video)){
295 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_CannotReadMpegSequHdrEx);
296 return 0;
298 if(mp_header_process_extension (&picture, &videobuffer[pos+4])) {
299 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_BadMpegSequHdrEx);
300 return 0;
304 // display info:
305 sh_video->format=picture.mpeg1?0x10000001:0x10000002; // mpeg video
306 sh_video->fps=picture.fps;
307 if(!sh_video->fps){
308 sh_video->frametime=0;
309 } else {
310 sh_video->frametime=1.0/picture.fps;
312 sh_video->disp_w=picture.display_picture_width;
313 sh_video->disp_h=picture.display_picture_height;
314 // bitrate:
315 if(picture.bitrate!=0x3FFFF) // unspecified/VBR ?
316 sh_video->i_bps=picture.bitrate * 400 / 8;
317 // info:
318 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2,"mpeg bitrate: %d (%X)\n",picture.bitrate,picture.bitrate);
319 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"VIDEO: %s %dx%d (aspect %d) %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
320 picture.mpeg1?"MPEG1":"MPEG2",
321 sh_video->disp_w,sh_video->disp_h,
322 picture.aspect_ratio_information,
323 sh_video->fps,
324 sh_video->i_bps * 8 / 1000.0,
325 sh_video->i_bps / 1000.0 );
326 break;
328 case VIDEO_VC1: {
329 // Find sequence_header:
330 videobuf_len=0;
331 videobuf_code_len=0;
332 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"Searching for VC1 sequence header... ");
333 while(1){
334 int i=sync_video_packet(d_video);
335 if(i==0x10F) break; // found it!
336 if(!i || !skip_video_packet(d_video)){
337 if( mp_msg_test(MSGT_DECVIDEO,MSGL_V) ) mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
338 mp_msg(MSGT_DECVIDEO,MSGL_ERR, "Couldn't find VC-1 sequence header\n");
339 return 0;
342 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"found\n");
343 if(!videobuffer) {
344 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
345 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
346 else {
347 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail);
348 return 0;
351 if(!read_video_packet(d_video)){
352 mp_msg(MSGT_DECVIDEO,MSGL_ERR, "Couldn't read VC-1 sequence header!\n");
353 return 0;
356 while(1) {
357 int i=sync_video_packet(d_video);
358 if(i==0x10E) break; // found it!
359 if(!i || !skip_video_packet(d_video)){
360 mp_msg(MSGT_DECVIDEO,MSGL_V,"Couldn't find VC-1 entry point sync-code:(\n");
361 return 0;
364 if(!read_video_packet(d_video)){
365 mp_msg(MSGT_DECVIDEO,MSGL_V,"Couldn't read VC-1 entry point sync-code:(\n");
366 return 0;
369 if(mp_vc1_decode_sequence_header(&picture, &videobuffer[4], videobuf_len-4)) {
370 sh_video->bih = (BITMAPINFOHEADER *) calloc(1, sizeof(BITMAPINFOHEADER) + videobuf_len);
371 if(sh_video->bih == NULL) {
372 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Couldn't alloc %d bytes for VC-1 extradata!\n", sizeof(BITMAPINFOHEADER) + videobuf_len);
373 return 0;
375 sh_video->bih->biSize= sizeof(BITMAPINFOHEADER) + videobuf_len;
376 memcpy(sh_video->bih + 1, videobuffer, videobuf_len);
377 sh_video->bih->biCompression = sh_video->format;
378 sh_video->bih->biWidth = sh_video->disp_w = picture.display_picture_width;
379 sh_video->bih->biHeight = sh_video->disp_h = picture.display_picture_height;
380 if(picture.fps > 0) {
381 sh_video->frametime=1.0/picture.fps;
382 sh_video->fps = picture.fps;
384 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"VIDEO: VC-1 %dx%d, %5.3f fps, header len: %d\n",
385 sh_video->disp_w, sh_video->disp_h, sh_video->fps, videobuf_len);
387 break;
389 } // switch(file_format)
391 return 1;
394 void ty_processuserdata( unsigned char* buf, int len );
396 static void process_userdata(unsigned char* buf,int len){
397 int i;
398 /* if the user data starts with "CC", assume it is a CC info packet */
399 if(len>2 && buf[0]=='C' && buf[1]=='C'){
400 // mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"video.c: process_userdata() detected Closed Captions!\n");
401 subcc_process_data(buf+2,len-2);
403 if( len > 2 && buf[ 0 ] == 'T' && buf[ 1 ] == 'Y' )
405 ty_processuserdata( buf + 2, len - 2 );
406 return;
408 if(verbose<2) return;
409 fprintf(stderr, "user_data: len=%3d %02X %02X %02X %02X '",
410 len, buf[0], buf[1], buf[2], buf[3]);
411 for(i=0;i<len;i++)
412 // if(buf[i]>=32 && buf[i]<127) fputc(buf[i], stderr);
413 if(buf[i]&0x60) fputc(buf[i]&0x7F, stderr);
414 fprintf(stderr, "'\n");
417 int video_read_frame(sh_video_t* sh_video,float* frame_time_ptr,unsigned char** start,int force_fps){
418 demux_stream_t *d_video=sh_video->ds;
419 demuxer_t *demuxer=d_video->demuxer;
420 float frame_time=1;
421 float pts1=d_video->pts;
422 float pts=0;
423 int picture_coding_type=0;
424 int in_size=0;
425 video_codec_t video_codec = find_video_codec(sh_video);
427 *start=NULL;
429 if(video_codec == VIDEO_MPEG12){
430 int in_frame=0;
431 //float newfps;
432 //videobuf_len=0;
433 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
434 int i=sync_video_packet(d_video);
435 //void* buffer=&videobuffer[videobuf_len+4];
436 int start=videobuf_len+4;
437 if(in_frame){
438 if(i<0x101 || i>=0x1B0){ // not slice code -> end of frame
439 if(!i) return -1; // EOF
440 break;
442 } else {
443 if(i==0x100){
444 pts=d_video->pts;
445 d_video->pts=0;
447 if(i>=0x101 && i<0x1B0) in_frame=1; // picture startcode
448 else if(!i) return -1; // EOF
450 if(!read_video_packet(d_video)) return -1; // EOF
451 // process headers:
452 switch(i){
453 case 0x1B3: mp_header_process_sequence_header (&picture, &videobuffer[start]);break;
454 case 0x1B5: mp_header_process_extension (&picture, &videobuffer[start]);break;
455 case 0x1B2: process_userdata (&videobuffer[start], videobuf_len-start);break;
456 case 0x100: picture_coding_type=(videobuffer[start+1] >> 3) & 7;break;
460 *start=videobuffer; in_size=videobuf_len;
462 // get mpeg fps:
463 if(sh_video->fps!=picture.fps) if(!force_fps && !telecine){
464 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);
465 sh_video->fps=picture.fps;
466 sh_video->frametime=1.0/picture.fps;
469 // fix mpeg2 frametime:
470 frame_time=(picture.display_time)*0.01f;
471 picture.display_time=100;
472 videobuf_len=0;
474 telecine_cnt*=0.9; // drift out error
475 telecine_cnt+=frame_time-5.0/4.0;
476 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"\r telecine = %3.1f %5.3f \n",frame_time,telecine_cnt);
478 if(telecine){
479 frame_time=1;
480 if(telecine_cnt<-1.5 || telecine_cnt>1.5){
481 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_LeaveTelecineMode);
482 telecine=0;
484 } else
485 if(telecine_cnt>-0.5 && telecine_cnt<0.5 && !force_fps){
486 sh_video->fps=sh_video->fps*4/5;
487 sh_video->frametime=sh_video->frametime*5/4;
488 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_EnterTelecineMode);
489 telecine=1;
491 } else if(video_codec == VIDEO_MPEG4){
492 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
493 int i=sync_video_packet(d_video);
494 if(!i) return -1;
495 if(!read_video_packet(d_video)) return -1; // EOF
496 if(i==0x1B6) break;
498 *start=videobuffer; in_size=videobuf_len;
499 videobuf_len=0;
500 } else if(video_codec == VIDEO_H264){
501 int in_picture = 0;
502 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
503 int i=sync_video_packet(d_video);
504 int pos = videobuf_len+4;
505 if(!i) return -1;
506 if(!read_video_packet(d_video)) return -1; // EOF
507 if((i&~0x60) == 0x107 && i != 0x107) {
508 h264_parse_sps(&picture, &(videobuffer[pos]), videobuf_len - pos);
509 if(picture.fps > 0) {
510 sh_video->fps=picture.fps;
511 sh_video->frametime=1.0/picture.fps;
513 i=sync_video_packet(d_video);
514 if(!i) return -1;
515 if(!read_video_packet(d_video)) return -1; // EOF
518 // here starts the access unit end detection code
519 // see the mail on MPlayer-dev-eng for details:
520 // Date: Sat, 17 Sep 2005 11:24:06 +0200
521 // Subject: Re: [MPlayer-dev-eng] [RFC] h264 ES parser problems
522 // Message-ID: <20050917092406.GA7699@rz.uni-karlsruhe.de>
523 if((i&~0x60) == 0x101 || (i&~0x60) == 0x102 || (i&~0x60) == 0x105)
524 // found VCL NAL with slice header i.e. start of current primary coded
525 // picture, so start scanning for the end now
526 in_picture = 1;
527 if (in_picture) {
528 i = sync_video_packet(d_video) & ~0x60; // code of next packet
529 if(i == 0x106 || i == 0x109) break; // SEI or access unit delim.
530 if(i == 0x101 || i == 0x102 || i == 0x105) {
531 // assuming arbitrary slice ordering is not allowed, the
532 // first_mb_in_slice (golomb encoded) value should be 0 then
533 // for the first VCL NAL in a picture
534 if (demux_peekc(d_video) & 0x80)
535 break;
539 *start=videobuffer; in_size=videobuf_len;
540 videobuf_len=0;
541 } else if(video_codec == VIDEO_VC1) {
542 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE) {
543 int i=sync_video_packet(d_video);
544 if(!i) return -1;
545 if(!read_video_packet(d_video)) return -1; // EOF
546 if(i==0x10D) break;
548 *start=videobuffer;
549 in_size=videobuf_len;
550 videobuf_len=0;
551 } else {
552 // frame-based file formats: (AVI,ASF,MOV)
553 in_size=ds_get_packet(d_video,start);
554 if(in_size<0) return -1; // EOF
558 //------------------------ frame decoded. --------------------
560 // Increase video timers:
561 sh_video->num_frames+=frame_time;
562 ++sh_video->num_frames_decoded;
564 frame_time*=sh_video->frametime;
566 // override frame_time for variable/unknown FPS formats:
567 if(!force_fps) switch(demuxer->file_format){
568 case DEMUXER_TYPE_GIF:
569 case DEMUXER_TYPE_MATROSKA:
570 if(d_video->pts>0 && pts1>0 && d_video->pts>pts1)
571 frame_time=d_video->pts-pts1;
572 break;
573 case DEMUXER_TYPE_TV:
574 case DEMUXER_TYPE_MOV:
575 case DEMUXER_TYPE_FILM:
576 case DEMUXER_TYPE_VIVO:
577 case DEMUXER_TYPE_OGG:
578 case DEMUXER_TYPE_ASF: {
579 double next_pts = ds_get_next_pts(d_video);
580 double d= (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts-pts1;
581 if(d>=0){
582 if(d>0){
583 if((int)sh_video->fps==1000)
584 mp_msg(MSGT_CPLAYER,MSGL_V,"\navg. framerate: %d fps \n",(int)(1.0f/d));
585 sh_video->frametime=d; // 1ms
586 sh_video->fps=1.0f/d;
588 frame_time = d;
589 } else {
590 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);
591 // frame_time = 1/25.0;
594 break;
595 case DEMUXER_TYPE_LAVF:
596 if((int)sh_video->fps==1000 || (int)sh_video->fps<=1){
597 double next_pts = ds_get_next_pts(d_video);
598 double d= (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts-pts1;
599 if(d>=0){
600 frame_time = d;
603 break;
604 case DEMUXER_TYPE_REAL:
606 double next_pts = ds_get_next_pts(d_video);
607 double d = (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts - pts1;
609 frame_time = (d >= 0 && pts1 > 0) ? d : 0.001;
611 break;
614 if(video_codec == VIDEO_MPEG12){
615 sh_video->pts+=frame_time;
616 if(picture_coding_type==1)
617 d_video->flags |= 1;
618 if(picture_coding_type<=2 && sh_video->i_pts){
619 sh_video->pts=sh_video->i_pts;
620 sh_video->i_pts=pts;
621 } else {
622 if(pts){
623 if(picture_coding_type<=2) sh_video->i_pts=pts;
624 else sh_video->pts=pts;
627 } else
628 sh_video->pts=d_video->pts;
630 if(frame_time_ptr) *frame_time_ptr=frame_time;
631 return in_size;