demux_real.c: Remove unused hexdump() forward declaration
[mplayer/greg.git] / libmpdemux / video.c
blob1f249ff8047ceb8efae439cd642577e437a47aa6
1 /*
2 * video frame reading
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "config.h"
23 #include <stdio.h>
24 #if HAVE_MALLOC_H
25 #include <malloc.h>
26 #endif
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "mp_msg.h"
33 #include "stream/stream.h"
34 #include "demuxer.h"
35 #include "demux_ty_osd.h"
36 #include "stheader.h"
37 #include "parse_es.h"
38 #include "mpeg_hdr.h"
40 /* sub_cc (closed captions)*/
41 #include "sub_cc.h"
43 /* biCompression constant */
44 #define BI_RGB 0L
46 #ifdef CONFIG_LIVE555
47 #include "demux_rtp.h"
48 #endif
50 static mp_mpeg_header_t picture;
52 static int telecine=0;
53 static float telecine_cnt=-2.5;
55 typedef enum {
56 VIDEO_MPEG12,
57 VIDEO_MPEG4,
58 VIDEO_H264,
59 VIDEO_VC1,
60 VIDEO_OTHER
61 } video_codec_t;
63 static video_codec_t find_video_codec(sh_video_t *sh_video)
65 demux_stream_t *d_video=sh_video->ds;
66 int fmt = d_video->demuxer->file_format;
68 if(
69 (fmt == DEMUXER_TYPE_PVA) ||
70 (fmt == DEMUXER_TYPE_MPEG_ES) ||
71 (fmt == DEMUXER_TYPE_MPEG_GXF) ||
72 (fmt == DEMUXER_TYPE_MPEG_PES) ||
74 (fmt == DEMUXER_TYPE_MPEG_PS || fmt == DEMUXER_TYPE_MPEG_TS) &&
75 ((! sh_video->format) || (sh_video->format==0x10000001) || (sh_video->format==0x10000002))
76 ) ||
77 (fmt == DEMUXER_TYPE_MPEG_TY)
78 #ifdef CONFIG_LIVE555
79 || ((fmt == DEMUXER_TYPE_RTP) && demux_is_mpeg_rtp_stream(d_video->demuxer))
80 #endif
82 return VIDEO_MPEG12;
83 else if((fmt == DEMUXER_TYPE_MPEG4_ES) ||
84 ((fmt == DEMUXER_TYPE_MPEG_TS) && (sh_video->format==0x10000004)) ||
85 ((fmt == DEMUXER_TYPE_MPEG_PS) && (sh_video->format==0x10000004))
87 return VIDEO_MPEG4;
88 else if((fmt == DEMUXER_TYPE_H264_ES) ||
89 ((fmt == DEMUXER_TYPE_MPEG_TS) && (sh_video->format==0x10000005)) ||
90 ((fmt == DEMUXER_TYPE_MPEG_PS) && (sh_video->format==0x10000005))
92 return VIDEO_H264;
93 else if((fmt == DEMUXER_TYPE_MPEG_PS || fmt == DEMUXER_TYPE_MPEG_TS) &&
94 (sh_video->format==mmioFOURCC('W', 'V', 'C', '1')))
95 return VIDEO_VC1;
96 else if (fmt == DEMUXER_TYPE_ASF && sh_video->bih && sh_video->bih->biCompression == mmioFOURCC('D', 'V', 'R', ' '))
97 return VIDEO_MPEG12;
98 else
99 return VIDEO_OTHER;
102 int video_read_properties(sh_video_t *sh_video){
103 demux_stream_t *d_video=sh_video->ds;
104 video_codec_t video_codec = find_video_codec(sh_video);
105 // Determine image properties:
106 switch(video_codec){
107 case VIDEO_OTHER: {
108 if((d_video->demuxer->file_format == DEMUXER_TYPE_ASF) || (d_video->demuxer->file_format == DEMUXER_TYPE_AVI)) {
109 // display info:
110 // in case no strf chunk has been seen in avi, we have no bitmap header
111 if(!sh_video->bih) return 0;
112 sh_video->format=sh_video->bih->biCompression;
113 sh_video->disp_w=sh_video->bih->biWidth;
114 sh_video->disp_h=abs(sh_video->bih->biHeight);
116 break;
118 case VIDEO_MPEG4: {
119 int pos = 0, vop_cnt=0, units[3];
120 videobuf_len=0; videobuf_code_len=0;
121 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for Video Object Start code... ");
122 while(1){
123 int i=sync_video_packet(d_video);
124 if(i<=0x11F) break; // found it!
125 if(!i || !skip_video_packet(d_video)){
126 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
127 return 0;
130 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
131 if(!videobuffer) {
132 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
133 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
134 else {
135 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"Cannot allocate shared memory.\n");
136 return 0;
139 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for Video Object Layer Start code... ");
140 while(1){
141 int i=sync_video_packet(d_video);
142 mp_msg(MSGT_DECVIDEO,MSGL_V,"M4V: 0x%X\n",i);
143 if(i>=0x120 && i<=0x12F) break; // found it!
144 if(!i || !read_video_packet(d_video)){
145 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
146 return 0;
149 pos = videobuf_len+4;
150 if(!read_video_packet(d_video)){
151 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read Video Object Layer Header\n");
152 return 0;
154 mp4_header_process_vol(&picture, &(videobuffer[pos]));
155 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK! FPS SEEMS TO BE %.3f\nSearching for Video Object Plane Start code... ", sh_video->fps);
156 mp4_init:
157 while(1){
158 int i=sync_video_packet(d_video);
159 if(i==0x1B6) break; // found it!
160 if(!i || !read_video_packet(d_video)){
161 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
162 return 0;
165 pos = videobuf_len+4;
166 if(!read_video_packet(d_video)){
167 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read Video Object Plane Header\n");
168 return 0;
170 mp4_header_process_vop(&picture, &(videobuffer[pos]));
171 sh_video->disp_w = picture.display_picture_width;
172 sh_video->disp_h = picture.display_picture_height;
173 units[vop_cnt] = picture.timeinc_unit;
174 vop_cnt++;
175 //mp_msg(MSGT_DECVIDEO,MSGL_V, "TYPE: %d, unit: %d\n", picture.picture_type, picture.timeinc_unit);
176 if(!picture.fps) {
177 int i, mn, md, mx, diff;
178 if(vop_cnt < 3)
179 goto mp4_init;
181 i=0;
182 mn = mx = units[0];
183 for(i=0; i<3; i++) {
184 if(units[i] < mn)
185 mn = units[i];
186 if(units[i] > mx)
187 mx = units[i];
189 md = mn;
190 for(i=0; i<3; i++) {
191 if((units[i] > mn) && (units[i] < mx))
192 md = units[i];
194 mp_msg(MSGT_DECVIDEO,MSGL_V, "MIN: %d, mid: %d, max: %d\n", mn, md, mx);
195 if(mx - md > md - mn)
196 diff = md - mn;
197 else
198 diff = mx - md;
199 if(diff > 0){
200 picture.fps = ((float)picture.timeinc_resolution) / diff;
201 mp_msg(MSGT_DECVIDEO,MSGL_V, "FPS seems to be: %f, resolution: %d, delta_units: %d\n", picture.fps, picture.timeinc_resolution, diff);
204 if(picture.fps) {
205 sh_video->fps=picture.fps;
206 sh_video->frametime=1.0/picture.fps;
207 mp_msg(MSGT_DECVIDEO,MSGL_INFO, "FPS seems to be: %f\n", picture.fps);
209 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
210 sh_video->format=0x10000004;
211 break;
213 case VIDEO_H264: {
214 int pos = 0;
215 videobuf_len=0; videobuf_code_len=0;
216 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for sequence parameter set... ");
217 while(1){
218 int i=sync_video_packet(d_video);
219 if((i&~0x60) == 0x107 && i != 0x107) break; // found it!
220 if(!i || !skip_video_packet(d_video)){
221 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
222 return 0;
225 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
226 if(!videobuffer) {
227 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
228 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
229 else {
230 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"Cannot allocate shared memory.\n");
231 return 0;
234 pos = videobuf_len+4;
235 if(!read_video_packet(d_video)){
236 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read sequence parameter set\n");
237 return 0;
239 h264_parse_sps(&picture, &(videobuffer[pos]), videobuf_len - pos);
240 sh_video->disp_w=picture.display_picture_width;
241 sh_video->disp_h=picture.display_picture_height;
242 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for picture parameter set... ");
243 while(1){
244 int i=sync_video_packet(d_video);
245 mp_msg(MSGT_DECVIDEO,MSGL_V,"H264: 0x%X\n",i);
246 if((i&~0x60) == 0x108 && i != 0x108) break; // found it!
247 if(!i || !read_video_packet(d_video)){
248 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
249 return 0;
252 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\nSearching for Slice... ");
253 while(1){
254 int i=sync_video_packet(d_video);
255 if((i&~0x60) == 0x101 || (i&~0x60) == 0x102 || (i&~0x60) == 0x105) break; // found it!
256 if(!i || !read_video_packet(d_video)){
257 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
258 return 0;
261 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
262 sh_video->format=0x10000005;
263 if(picture.fps) {
264 sh_video->fps=picture.fps;
265 sh_video->frametime=1.0/picture.fps;
266 mp_msg(MSGT_DECVIDEO,MSGL_INFO, "FPS seems to be: %f\n", picture.fps);
268 break;
270 case VIDEO_MPEG12: {
271 if (d_video->demuxer->file_format == DEMUXER_TYPE_ASF) { // DVR-MS
272 if(!sh_video->bih) return 0;
273 sh_video->format=sh_video->bih->biCompression;
275 mpeg_header_parser:
276 // Find sequence_header first:
277 videobuf_len=0; videobuf_code_len=0;
278 telecine=0; telecine_cnt=-2.5;
279 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for sequence header... ");
280 while(1){
281 int i=sync_video_packet(d_video);
282 if(i==0x1B3) break; // found it!
283 if(!i || !skip_video_packet(d_video)){
284 if( mp_msg_test(MSGT_DECVIDEO,MSGL_V) ) mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
285 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"MPEG: FATAL: EOF while searching for sequence header.\n");
286 return 0;
289 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
290 // ========= Read & process sequence header & extension ============
291 if(!videobuffer) {
292 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
293 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
294 else {
295 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"Cannot allocate shared memory.\n");
296 return 0;
300 if(!read_video_packet(d_video)){
301 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"FATAL: Cannot read sequence header.\n");
302 return 0;
304 if(mp_header_process_sequence_header (&picture, &videobuffer[4])) {
305 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"MPEG: bad sequence header\n");
306 goto mpeg_header_parser;
308 if(sync_video_packet(d_video)==0x1B5){ // next packet is seq. ext.
309 int pos=videobuf_len;
310 if(!read_video_packet(d_video)){
311 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"FATAL: Cannot read sequence header extension.\n");
312 return 0;
314 if(mp_header_process_extension (&picture, &videobuffer[pos+4])) {
315 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"MPEG: bad sequence header extension\n");
316 return 0;
320 // display info:
321 sh_video->format=picture.mpeg1?0x10000001:0x10000002; // mpeg video
322 sh_video->fps=picture.fps * picture.frame_rate_extension_n / picture.frame_rate_extension_d;
323 if(!sh_video->fps){
324 sh_video->frametime=0;
325 } else {
326 sh_video->frametime=1.0/sh_video->fps;
328 sh_video->disp_w=picture.display_picture_width;
329 sh_video->disp_h=picture.display_picture_height;
330 // bitrate:
331 if(picture.bitrate!=0x3FFFF) // unspecified/VBR ?
332 sh_video->i_bps=picture.bitrate * 400 / 8;
333 // info:
334 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2,"mpeg bitrate: %d (%X)\n",picture.bitrate,picture.bitrate);
335 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"VIDEO: %s %dx%d (aspect %d) %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
336 picture.mpeg1?"MPEG1":"MPEG2",
337 sh_video->disp_w,sh_video->disp_h,
338 picture.aspect_ratio_information,
339 sh_video->fps,
340 sh_video->i_bps * 8 / 1000.0,
341 sh_video->i_bps / 1000.0 );
342 break;
344 case VIDEO_VC1: {
345 // Find sequence_header:
346 videobuf_len=0;
347 videobuf_code_len=0;
348 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"Searching for VC1 sequence header... ");
349 while(1){
350 int i=sync_video_packet(d_video);
351 if(i==0x10F) break; // found it!
352 if(!i || !skip_video_packet(d_video)){
353 if( mp_msg_test(MSGT_DECVIDEO,MSGL_V) ) mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
354 mp_msg(MSGT_DECVIDEO,MSGL_ERR, "Couldn't find VC-1 sequence header\n");
355 return 0;
358 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"found\n");
359 if(!videobuffer) {
360 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
361 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
362 else {
363 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"Cannot allocate shared memory.\n");
364 return 0;
367 if(!read_video_packet(d_video)){
368 mp_msg(MSGT_DECVIDEO,MSGL_ERR, "Couldn't read VC-1 sequence header!\n");
369 return 0;
372 while(1) {
373 int i=sync_video_packet(d_video);
374 if(i==0x10E) break; // found it!
375 if(!i || !skip_video_packet(d_video)){
376 mp_msg(MSGT_DECVIDEO,MSGL_V,"Couldn't find VC-1 entry point sync-code:(\n");
377 return 0;
380 if(!read_video_packet(d_video)){
381 mp_msg(MSGT_DECVIDEO,MSGL_V,"Couldn't read VC-1 entry point sync-code:(\n");
382 return 0;
385 if(mp_vc1_decode_sequence_header(&picture, &videobuffer[4], videobuf_len-4)) {
386 sh_video->bih = calloc(1, sizeof(*sh_video->bih) + videobuf_len);
387 if(sh_video->bih == NULL) {
388 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Couldn't alloc %d bytes for VC-1 extradata!\n", sizeof(*sh_video->bih) + videobuf_len);
389 return 0;
391 sh_video->bih->biSize= sizeof(*sh_video->bih) + videobuf_len;
392 memcpy(sh_video->bih + 1, videobuffer, videobuf_len);
393 sh_video->bih->biCompression = sh_video->format;
394 sh_video->bih->biWidth = sh_video->disp_w = picture.display_picture_width;
395 sh_video->bih->biHeight = sh_video->disp_h = picture.display_picture_height;
396 if(picture.fps > 0) {
397 sh_video->frametime=1.0/picture.fps;
398 sh_video->fps = picture.fps;
400 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"VIDEO: VC-1 %dx%d, %5.3f fps, header len: %d\n",
401 sh_video->disp_w, sh_video->disp_h, sh_video->fps, videobuf_len);
403 break;
405 } // switch(file_format)
407 return 1;
410 static void process_userdata(unsigned char* buf,int len){
411 int i;
412 /* if the user data starts with "CC", assume it is a CC info packet */
413 if(len>2 && buf[0]=='C' && buf[1]=='C'){
414 // mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"video.c: process_userdata() detected Closed Captions!\n");
415 subcc_process_data(buf+2,len-2);
417 if( len > 2 && buf[ 0 ] == 'T' && buf[ 1 ] == 'Y' )
419 ty_processuserdata( buf + 2, len - 2 );
420 return;
422 if(verbose<2) return;
423 fprintf(stderr, "user_data: len=%3d %02X %02X %02X %02X '",
424 len, buf[0], buf[1], buf[2], buf[3]);
425 for(i=0;i<len;i++)
426 // if(buf[i]>=32 && buf[i]<127) fputc(buf[i], stderr);
427 if(buf[i]&0x60) fputc(buf[i]&0x7F, stderr);
428 fprintf(stderr, "'\n");
431 int video_read_frame(sh_video_t* sh_video,float* frame_time_ptr,unsigned char** start,int force_fps){
432 demux_stream_t *d_video=sh_video->ds;
433 demuxer_t *demuxer=d_video->demuxer;
434 float frame_time=1;
435 float pts1=d_video->pts;
436 float pts=0;
437 float fps;
438 int picture_coding_type=0;
439 int in_size=0;
440 video_codec_t video_codec = find_video_codec(sh_video);
442 *start=NULL;
444 if(video_codec == VIDEO_MPEG12){
445 int in_frame=0;
446 //float newfps;
447 //videobuf_len=0;
448 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
449 int i=sync_video_packet(d_video);
450 //void* buffer=&videobuffer[videobuf_len+4];
451 int start=videobuf_len+4;
452 if(in_frame){
453 if(i<0x101 || i>=0x1B0){ // not slice code -> end of frame
454 if(!i) return -1; // EOF
455 break;
457 } else {
458 if(i==0x100){
459 pts=d_video->pts;
460 d_video->pts=0;
462 if(i>=0x101 && i<0x1B0) in_frame=1; // picture startcode
463 else if(!i) return -1; // EOF
465 if(!read_video_packet(d_video)) return -1; // EOF
466 // process headers:
467 switch(i){
468 case 0x1B3: mp_header_process_sequence_header (&picture, &videobuffer[start]);break;
469 case 0x1B5: mp_header_process_extension (&picture, &videobuffer[start]);break;
470 case 0x1B2: process_userdata (&videobuffer[start], videobuf_len-start);break;
471 case 0x100: picture_coding_type=(videobuffer[start+1] >> 3) & 7;break;
474 fps = picture.fps * picture.frame_rate_extension_n / picture.frame_rate_extension_d;
476 *start=videobuffer; in_size=videobuf_len;
478 // get mpeg fps:
479 if(sh_video->fps!=fps) if(!force_fps && !telecine){
480 mp_msg(MSGT_CPLAYER,MSGL_WARN,"Warning! FPS changed %5.3f -> %5.3f (%f) [%d] \n",sh_video->fps,fps,sh_video->fps-fps,picture.frame_rate_code);
481 sh_video->fps=fps;
482 sh_video->frametime=1.0/fps;
485 // fix mpeg2 frametime:
486 frame_time=(picture.display_time)*0.01f;
487 picture.display_time=100;
488 videobuf_len=0;
490 telecine_cnt*=0.9; // drift out error
491 telecine_cnt+=frame_time-5.0/4.0;
492 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"\r telecine = %3.1f %5.3f \n",frame_time,telecine_cnt);
494 if(telecine){
495 frame_time=1;
496 if(telecine_cnt<-1.5 || telecine_cnt>1.5){
497 mp_tmsg(MSGT_DECVIDEO,MSGL_INFO,"\ndemux_mpg: 30000/1001fps NTSC content detected, switching framerate.\n");
498 telecine=0;
500 } else
501 if(telecine_cnt>-0.5 && telecine_cnt<0.5 && !force_fps){
502 sh_video->fps=sh_video->fps*4/5;
503 sh_video->frametime=sh_video->frametime*5/4;
504 mp_tmsg(MSGT_DECVIDEO,MSGL_INFO,"\ndemux_mpg: 24000/1001fps progressive NTSC content detected, switching framerate.\n");
505 telecine=1;
507 } else if(video_codec == VIDEO_MPEG4){
508 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
509 int i=sync_video_packet(d_video);
510 if(!i) return -1;
511 if(!read_video_packet(d_video)) return -1; // EOF
512 if(i==0x1B6) break;
514 *start=videobuffer; in_size=videobuf_len;
515 videobuf_len=0;
516 } else if(video_codec == VIDEO_H264){
517 int in_picture = 0;
518 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
519 int i=sync_video_packet(d_video);
520 int pos = videobuf_len+4;
521 if(!i) return -1;
522 if(!read_video_packet(d_video)) return -1; // EOF
523 if((i&~0x60) == 0x107 && i != 0x107) {
524 h264_parse_sps(&picture, &(videobuffer[pos]), videobuf_len - pos);
525 if(picture.fps > 0) {
526 sh_video->fps=picture.fps;
527 sh_video->frametime=1.0/picture.fps;
529 i=sync_video_packet(d_video);
530 if(!i) return -1;
531 if(!read_video_packet(d_video)) return -1; // EOF
534 // here starts the access unit end detection code
535 // see the mail on MPlayer-dev-eng for details:
536 // Date: Sat, 17 Sep 2005 11:24:06 +0200
537 // Subject: Re: [MPlayer-dev-eng] [RFC] h264 ES parser problems
538 // Message-ID: <20050917092406.GA7699@rz.uni-karlsruhe.de>
539 if((i&~0x60) == 0x101 || (i&~0x60) == 0x102 || (i&~0x60) == 0x105)
540 // found VCL NAL with slice header i.e. start of current primary coded
541 // picture, so start scanning for the end now
542 in_picture = 1;
543 if (in_picture) {
544 i = sync_video_packet(d_video) & ~0x60; // code of next packet
545 if(i == 0x106 || i == 0x109) break; // SEI or access unit delim.
546 if(i == 0x101 || i == 0x102 || i == 0x105) {
547 // assuming arbitrary slice ordering is not allowed, the
548 // first_mb_in_slice (golomb encoded) value should be 0 then
549 // for the first VCL NAL in a picture
550 if (demux_peekc(d_video) & 0x80)
551 break;
555 *start=videobuffer; in_size=videobuf_len;
556 videobuf_len=0;
557 } else if(video_codec == VIDEO_VC1) {
558 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE) {
559 int i=sync_video_packet(d_video);
560 if(!i) return -1;
561 if(!read_video_packet(d_video)) return -1; // EOF
562 if(i==0x10D) break;
564 *start=videobuffer;
565 in_size=videobuf_len;
566 videobuf_len=0;
567 } else {
568 // frame-based file formats: (AVI,ASF,MOV)
569 in_size=ds_get_packet(d_video,start);
570 if(in_size<0) return -1; // EOF
574 //------------------------ frame decoded. --------------------
576 // Increase video timers:
577 sh_video->num_frames+=frame_time;
578 ++sh_video->num_frames_decoded;
580 frame_time*=sh_video->frametime;
582 // override frame_time for variable/unknown FPS formats:
583 if(!force_fps) switch(demuxer->file_format){
584 case DEMUXER_TYPE_GIF:
585 case DEMUXER_TYPE_MATROSKA:
586 case DEMUXER_TYPE_MNG:
587 if(d_video->pts>0 && pts1>0 && d_video->pts>pts1)
588 frame_time=d_video->pts-pts1;
589 break;
590 case DEMUXER_TYPE_TV:
591 case DEMUXER_TYPE_MOV:
592 case DEMUXER_TYPE_FILM:
593 case DEMUXER_TYPE_VIVO:
594 case DEMUXER_TYPE_OGG:
595 case DEMUXER_TYPE_ASF: {
596 double next_pts = ds_get_next_pts(d_video);
597 double d= (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts-pts1;
598 if(d>=0){
599 if(d>0){
600 if((int)sh_video->fps==1000)
601 mp_msg(MSGT_CPLAYER,MSGL_V,"\navg. framerate: %d fps \n",(int)(1.0f/d));
602 sh_video->frametime=d; // 1ms
603 sh_video->fps=1.0f/d;
605 frame_time = d;
606 } else {
607 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);
608 // frame_time = 1/25.0;
611 break;
612 case DEMUXER_TYPE_LAVF:
613 case DEMUXER_TYPE_LAVF_PREFERRED:
614 if((int)sh_video->fps==1000 || (int)sh_video->fps<=1){
615 double next_pts = ds_get_next_pts(d_video);
616 double d= (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts-pts1;
617 if(d>=0){
618 frame_time = d;
621 break;
622 case DEMUXER_TYPE_REAL:
624 double next_pts = ds_get_next_pts(d_video);
625 double d = (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts - pts1;
627 frame_time = (d >= 0 && pts1 > 0) ? d : 0.001;
629 break;
632 if(video_codec == VIDEO_MPEG12){
633 sh_video->pts+=frame_time;
634 if(picture_coding_type==1)
635 d_video->flags |= 1;
636 if(picture_coding_type<=2 && sh_video->i_pts){
637 sh_video->pts=sh_video->i_pts;
638 sh_video->i_pts=pts;
639 } else {
640 if(pts){
641 if(picture_coding_type<=2) sh_video->i_pts=pts;
642 else sh_video->pts=pts;
645 } else
646 sh_video->pts=d_video->pts;
648 if(frame_time_ptr) *frame_time_ptr=frame_time;
649 return in_size;