Fix compilation after FFmpeg r23485.
[mplayer/glamo.git] / libmpdemux / video.c
blob4804519ff2a3392d8d63bf09368cd12f770a16b6
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"
32 #include "help_mp.h"
34 #include "stream/stream.h"
35 #include "demuxer.h"
36 #include "demux_ty_osd.h"
37 #include "stheader.h"
38 #include "parse_es.h"
39 #include "mpeg_hdr.h"
41 /* sub_cc (closed captions)*/
42 #include "sub_cc.h"
44 /* biCompression constant */
45 #define BI_RGB 0L
47 #ifdef CONFIG_LIVE555
48 #include "demux_rtp.h"
49 #endif
51 static mp_mpeg_header_t picture;
53 static int telecine=0;
54 static float telecine_cnt=-2.5;
56 typedef enum {
57 VIDEO_MPEG12,
58 VIDEO_MPEG4,
59 VIDEO_H264,
60 VIDEO_VC1,
61 VIDEO_OTHER
62 } video_codec_t;
64 static video_codec_t find_video_codec(sh_video_t *sh_video)
66 demux_stream_t *d_video=sh_video->ds;
67 int fmt = d_video->demuxer->file_format;
69 if(
70 (fmt == DEMUXER_TYPE_PVA) ||
71 (fmt == DEMUXER_TYPE_MPEG_ES) ||
72 (fmt == DEMUXER_TYPE_MPEG_GXF) ||
73 (fmt == DEMUXER_TYPE_MPEG_PES) ||
75 (fmt == DEMUXER_TYPE_MPEG_PS || fmt == DEMUXER_TYPE_MPEG_TS) &&
76 ((! sh_video->format) || (sh_video->format==0x10000001) || (sh_video->format==0x10000002))
77 ) ||
78 (fmt == DEMUXER_TYPE_MPEG_TY)
79 #ifdef CONFIG_LIVE555
80 || ((fmt == DEMUXER_TYPE_RTP) && demux_is_mpeg_rtp_stream(d_video->demuxer))
81 #endif
83 return VIDEO_MPEG12;
84 else if((fmt == DEMUXER_TYPE_MPEG4_ES) ||
85 ((fmt == DEMUXER_TYPE_MPEG_TS) && (sh_video->format==0x10000004)) ||
86 ((fmt == DEMUXER_TYPE_MPEG_PS) && (sh_video->format==0x10000004))
88 return VIDEO_MPEG4;
89 else if((fmt == DEMUXER_TYPE_H264_ES) ||
90 ((fmt == DEMUXER_TYPE_MPEG_TS) && (sh_video->format==0x10000005)) ||
91 ((fmt == DEMUXER_TYPE_MPEG_PS) && (sh_video->format==0x10000005))
93 return VIDEO_H264;
94 else if((fmt == DEMUXER_TYPE_MPEG_PS || fmt == DEMUXER_TYPE_MPEG_TS) &&
95 (sh_video->format==mmioFOURCC('W', 'V', 'C', '1')))
96 return VIDEO_VC1;
97 else if (fmt == DEMUXER_TYPE_ASF && sh_video->bih && sh_video->bih->biCompression == mmioFOURCC('D', 'V', 'R', ' '))
98 return VIDEO_MPEG12;
99 else
100 return VIDEO_OTHER;
103 int video_read_properties(sh_video_t *sh_video){
104 demux_stream_t *d_video=sh_video->ds;
105 video_codec_t video_codec = find_video_codec(sh_video);
106 // Determine image properties:
107 switch(video_codec){
108 case VIDEO_OTHER: {
109 if((d_video->demuxer->file_format == DEMUXER_TYPE_ASF) || (d_video->demuxer->file_format == DEMUXER_TYPE_AVI)) {
110 // display info:
111 // in case no strf chunk has been seen in avi, we have no bitmap header
112 if(!sh_video->bih) return 0;
113 sh_video->format=sh_video->bih->biCompression;
114 sh_video->disp_w=sh_video->bih->biWidth;
115 sh_video->disp_h=abs(sh_video->bih->biHeight);
117 break;
119 case VIDEO_MPEG4: {
120 int pos = 0, vop_cnt=0, units[3];
121 videobuf_len=0; videobuf_code_len=0;
122 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for Video Object Start code... ");
123 while(1){
124 int i=sync_video_packet(d_video);
125 if(i<=0x11F) break; // found it!
126 if(!i || !skip_video_packet(d_video)){
127 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
128 return 0;
131 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
132 if(!videobuffer) {
133 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
134 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
135 else {
136 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail);
137 return 0;
140 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for Video Object Layer Start code... ");
141 while(1){
142 int i=sync_video_packet(d_video);
143 mp_msg(MSGT_DECVIDEO,MSGL_V,"M4V: 0x%X\n",i);
144 if(i>=0x120 && i<=0x12F) break; // found it!
145 if(!i || !read_video_packet(d_video)){
146 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
147 return 0;
150 pos = videobuf_len+4;
151 if(!read_video_packet(d_video)){
152 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read Video Object Layer Header\n");
153 return 0;
155 mp4_header_process_vol(&picture, &(videobuffer[pos]));
156 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK! FPS SEEMS TO BE %.3f\nSearching for Video Object Plane Start code... ", sh_video->fps);
157 mp4_init:
158 while(1){
159 int i=sync_video_packet(d_video);
160 if(i==0x1B6) break; // found it!
161 if(!i || !read_video_packet(d_video)){
162 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
163 return 0;
166 pos = videobuf_len+4;
167 if(!read_video_packet(d_video)){
168 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read Video Object Plane Header\n");
169 return 0;
171 mp4_header_process_vop(&picture, &(videobuffer[pos]));
172 sh_video->disp_w = picture.display_picture_width;
173 sh_video->disp_h = picture.display_picture_height;
174 units[vop_cnt] = picture.timeinc_unit;
175 vop_cnt++;
176 //mp_msg(MSGT_DECVIDEO,MSGL_V, "TYPE: %d, unit: %d\n", picture.picture_type, picture.timeinc_unit);
177 if(!picture.fps) {
178 int i, mn, md, mx, diff;
179 if(vop_cnt < 3)
180 goto mp4_init;
182 i=0;
183 mn = mx = units[0];
184 for(i=0; i<3; i++) {
185 if(units[i] < mn)
186 mn = units[i];
187 if(units[i] > mx)
188 mx = units[i];
190 md = mn;
191 for(i=0; i<3; i++) {
192 if((units[i] > mn) && (units[i] < mx))
193 md = units[i];
195 mp_msg(MSGT_DECVIDEO,MSGL_V, "MIN: %d, mid: %d, max: %d\n", mn, md, mx);
196 if(mx - md > md - mn)
197 diff = md - mn;
198 else
199 diff = mx - md;
200 if(diff > 0){
201 picture.fps = ((float)picture.timeinc_resolution) / diff;
202 mp_msg(MSGT_DECVIDEO,MSGL_V, "FPS seems to be: %f, resolution: %d, delta_units: %d\n", picture.fps, picture.timeinc_resolution, diff);
205 if(picture.fps) {
206 sh_video->fps=picture.fps;
207 sh_video->frametime=1.0/picture.fps;
208 mp_msg(MSGT_DECVIDEO,MSGL_INFO, "FPS seems to be: %f\n", picture.fps);
210 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
211 sh_video->format=0x10000004;
212 break;
214 case VIDEO_H264: {
215 int pos = 0;
216 videobuf_len=0; videobuf_code_len=0;
217 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for sequence parameter set... ");
218 while(1){
219 int i=sync_video_packet(d_video);
220 if((i&~0x60) == 0x107 && i != 0x107) break; // found it!
221 if(!i || !skip_video_packet(d_video)){
222 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
223 return 0;
226 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
227 if(!videobuffer) {
228 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
229 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
230 else {
231 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail);
232 return 0;
235 pos = videobuf_len+4;
236 if(!read_video_packet(d_video)){
237 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read sequence parameter set\n");
238 return 0;
240 h264_parse_sps(&picture, &(videobuffer[pos]), videobuf_len - pos);
241 sh_video->disp_w=picture.display_picture_width;
242 sh_video->disp_h=picture.display_picture_height;
243 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for picture parameter set... ");
244 while(1){
245 int i=sync_video_packet(d_video);
246 mp_msg(MSGT_DECVIDEO,MSGL_V,"H264: 0x%X\n",i);
247 if((i&~0x60) == 0x108 && i != 0x108) break; // found it!
248 if(!i || !read_video_packet(d_video)){
249 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
250 return 0;
253 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\nSearching for Slice... ");
254 while(1){
255 int i=sync_video_packet(d_video);
256 if((i&~0x60) == 0x101 || (i&~0x60) == 0x102 || (i&~0x60) == 0x105) break; // found it!
257 if(!i || !read_video_packet(d_video)){
258 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
259 return 0;
262 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
263 sh_video->format=0x10000005;
264 if(picture.fps) {
265 sh_video->fps=picture.fps;
266 sh_video->frametime=1.0/picture.fps;
267 mp_msg(MSGT_DECVIDEO,MSGL_INFO, "FPS seems to be: %f\n", picture.fps);
269 break;
271 case VIDEO_MPEG12: {
272 if (d_video->demuxer->file_format == DEMUXER_TYPE_ASF) { // DVR-MS
273 if(!sh_video->bih) return 0;
274 sh_video->format=sh_video->bih->biCompression;
276 mpeg_header_parser:
277 // Find sequence_header first:
278 videobuf_len=0; videobuf_code_len=0;
279 telecine=0; telecine_cnt=-2.5;
280 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for sequence header... ");
281 while(1){
282 int i=sync_video_packet(d_video);
283 if(i==0x1B3) break; // found it!
284 if(!i || !skip_video_packet(d_video)){
285 if( mp_msg_test(MSGT_DECVIDEO,MSGL_V) ) mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
286 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_MpegNoSequHdr);
287 return 0;
290 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
291 // ========= Read & process sequence header & extension ============
292 if(!videobuffer) {
293 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
294 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
295 else {
296 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail);
297 return 0;
301 if(!read_video_packet(d_video)){
302 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_CannotReadMpegSequHdr);
303 return 0;
305 if(mp_header_process_sequence_header (&picture, &videobuffer[4])) {
306 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_BadMpegSequHdr);
307 goto mpeg_header_parser;
309 if(sync_video_packet(d_video)==0x1B5){ // next packet is seq. ext.
310 int pos=videobuf_len;
311 if(!read_video_packet(d_video)){
312 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_CannotReadMpegSequHdrEx);
313 return 0;
315 if(mp_header_process_extension (&picture, &videobuffer[pos+4])) {
316 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_BadMpegSequHdrEx);
317 return 0;
321 // display info:
322 sh_video->format=picture.mpeg1?0x10000001:0x10000002; // mpeg video
323 sh_video->fps=picture.fps * picture.frame_rate_extension_n / picture.frame_rate_extension_d;
324 if(!sh_video->fps){
325 sh_video->frametime=0;
326 } else {
327 sh_video->frametime=1.0/sh_video->fps;
329 sh_video->disp_w=picture.display_picture_width;
330 sh_video->disp_h=picture.display_picture_height;
331 // bitrate:
332 if(picture.bitrate!=0x3FFFF) // unspecified/VBR ?
333 sh_video->i_bps=picture.bitrate * 400 / 8;
334 // info:
335 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2,"mpeg bitrate: %d (%X)\n",picture.bitrate,picture.bitrate);
336 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"VIDEO: %s %dx%d (aspect %d) %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
337 picture.mpeg1?"MPEG1":"MPEG2",
338 sh_video->disp_w,sh_video->disp_h,
339 picture.aspect_ratio_information,
340 sh_video->fps,
341 sh_video->i_bps * 8 / 1000.0,
342 sh_video->i_bps / 1000.0 );
343 break;
345 case VIDEO_VC1: {
346 // Find sequence_header:
347 videobuf_len=0;
348 videobuf_code_len=0;
349 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"Searching for VC1 sequence header... ");
350 while(1){
351 int i=sync_video_packet(d_video);
352 if(i==0x10F) break; // found it!
353 if(!i || !skip_video_packet(d_video)){
354 if( mp_msg_test(MSGT_DECVIDEO,MSGL_V) ) mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
355 mp_msg(MSGT_DECVIDEO,MSGL_ERR, "Couldn't find VC-1 sequence header\n");
356 return 0;
359 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"found\n");
360 if(!videobuffer) {
361 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
362 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
363 else {
364 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail);
365 return 0;
368 if(!read_video_packet(d_video)){
369 mp_msg(MSGT_DECVIDEO,MSGL_ERR, "Couldn't read VC-1 sequence header!\n");
370 return 0;
373 while(1) {
374 int i=sync_video_packet(d_video);
375 if(i==0x10E) break; // found it!
376 if(!i || !skip_video_packet(d_video)){
377 mp_msg(MSGT_DECVIDEO,MSGL_V,"Couldn't find VC-1 entry point sync-code:(\n");
378 return 0;
381 if(!read_video_packet(d_video)){
382 mp_msg(MSGT_DECVIDEO,MSGL_V,"Couldn't read VC-1 entry point sync-code:(\n");
383 return 0;
386 if(mp_vc1_decode_sequence_header(&picture, &videobuffer[4], videobuf_len-4)) {
387 sh_video->bih = calloc(1, sizeof(BITMAPINFOHEADER) + videobuf_len);
388 if(sh_video->bih == NULL) {
389 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Couldn't alloc %d bytes for VC-1 extradata!\n", sizeof(BITMAPINFOHEADER) + videobuf_len);
390 return 0;
392 sh_video->bih->biSize= sizeof(BITMAPINFOHEADER) + videobuf_len;
393 memcpy(sh_video->bih + 1, videobuffer, videobuf_len);
394 sh_video->bih->biCompression = sh_video->format;
395 sh_video->bih->biWidth = sh_video->disp_w = picture.display_picture_width;
396 sh_video->bih->biHeight = sh_video->disp_h = picture.display_picture_height;
397 if(picture.fps > 0) {
398 sh_video->frametime=1.0/picture.fps;
399 sh_video->fps = picture.fps;
401 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"VIDEO: VC-1 %dx%d, %5.3f fps, header len: %d\n",
402 sh_video->disp_w, sh_video->disp_h, sh_video->fps, videobuf_len);
404 break;
406 } // switch(file_format)
408 return 1;
411 static void process_userdata(unsigned char* buf,int len){
412 int i;
413 /* if the user data starts with "CC", assume it is a CC info packet */
414 if(len>2 && buf[0]=='C' && buf[1]=='C'){
415 // mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"video.c: process_userdata() detected Closed Captions!\n");
416 subcc_process_data(buf+2,len-2);
418 if( len > 2 && buf[ 0 ] == 'T' && buf[ 1 ] == 'Y' )
420 ty_processuserdata( buf + 2, len - 2 );
421 return;
423 if(verbose<2) return;
424 fprintf(stderr, "user_data: len=%3d %02X %02X %02X %02X '",
425 len, buf[0], buf[1], buf[2], buf[3]);
426 for(i=0;i<len;i++)
427 // if(buf[i]>=32 && buf[i]<127) fputc(buf[i], stderr);
428 if(buf[i]&0x60) fputc(buf[i]&0x7F, stderr);
429 fprintf(stderr, "'\n");
432 int video_read_frame(sh_video_t* sh_video,float* frame_time_ptr,unsigned char** start,int force_fps){
433 demux_stream_t *d_video=sh_video->ds;
434 demuxer_t *demuxer=d_video->demuxer;
435 float frame_time=1;
436 float pts1=d_video->pts;
437 float pts=0;
438 float fps;
439 int picture_coding_type=0;
440 int in_size=0;
441 video_codec_t video_codec = find_video_codec(sh_video);
443 *start=NULL;
445 if(video_codec == VIDEO_MPEG12){
446 int in_frame=0;
447 //float newfps;
448 //videobuf_len=0;
449 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
450 int i=sync_video_packet(d_video);
451 //void* buffer=&videobuffer[videobuf_len+4];
452 int start=videobuf_len+4;
453 if(in_frame){
454 if(i<0x101 || i>=0x1B0){ // not slice code -> end of frame
455 if(!i) return -1; // EOF
456 break;
458 } else {
459 if(i==0x100){
460 pts=d_video->pts;
461 d_video->pts=0;
463 if(i>=0x101 && i<0x1B0) in_frame=1; // picture startcode
464 else if(!i) return -1; // EOF
466 if(!read_video_packet(d_video)) return -1; // EOF
467 // process headers:
468 switch(i){
469 case 0x1B3: mp_header_process_sequence_header (&picture, &videobuffer[start]);break;
470 case 0x1B5: mp_header_process_extension (&picture, &videobuffer[start]);break;
471 case 0x1B2: process_userdata (&videobuffer[start], videobuf_len-start);break;
472 case 0x100: picture_coding_type=(videobuffer[start+1] >> 3) & 7;break;
475 fps = picture.fps * picture.frame_rate_extension_n / picture.frame_rate_extension_d;
477 *start=videobuffer; in_size=videobuf_len;
479 // get mpeg fps:
480 if(sh_video->fps!=fps) if(!force_fps && !telecine){
481 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);
482 sh_video->fps=fps;
483 sh_video->frametime=1.0/fps;
486 // fix mpeg2 frametime:
487 frame_time=(picture.display_time)*0.01f;
488 picture.display_time=100;
489 videobuf_len=0;
491 telecine_cnt*=0.9; // drift out error
492 telecine_cnt+=frame_time-5.0/4.0;
493 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"\r telecine = %3.1f %5.3f \n",frame_time,telecine_cnt);
495 if(telecine){
496 frame_time=1;
497 if(telecine_cnt<-1.5 || telecine_cnt>1.5){
498 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_LeaveTelecineMode);
499 telecine=0;
501 } else
502 if(telecine_cnt>-0.5 && telecine_cnt<0.5 && !force_fps){
503 sh_video->fps=sh_video->fps*4/5;
504 sh_video->frametime=sh_video->frametime*5/4;
505 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_EnterTelecineMode);
506 telecine=1;
508 } else if(video_codec == VIDEO_MPEG4){
509 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
510 int i=sync_video_packet(d_video);
511 if(!i) return -1;
512 if(!read_video_packet(d_video)) return -1; // EOF
513 if(i==0x1B6) break;
515 *start=videobuffer; in_size=videobuf_len;
516 videobuf_len=0;
517 } else if(video_codec == VIDEO_H264){
518 int in_picture = 0;
519 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
520 int i=sync_video_packet(d_video);
521 int pos = videobuf_len+4;
522 if(!i) return -1;
523 if(!read_video_packet(d_video)) return -1; // EOF
524 if((i&~0x60) == 0x107 && i != 0x107) {
525 h264_parse_sps(&picture, &(videobuffer[pos]), videobuf_len - pos);
526 if(picture.fps > 0) {
527 sh_video->fps=picture.fps;
528 sh_video->frametime=1.0/picture.fps;
530 i=sync_video_packet(d_video);
531 if(!i) return -1;
532 if(!read_video_packet(d_video)) return -1; // EOF
535 // here starts the access unit end detection code
536 // see the mail on MPlayer-dev-eng for details:
537 // Date: Sat, 17 Sep 2005 11:24:06 +0200
538 // Subject: Re: [MPlayer-dev-eng] [RFC] h264 ES parser problems
539 // Message-ID: <20050917092406.GA7699@rz.uni-karlsruhe.de>
540 if((i&~0x60) == 0x101 || (i&~0x60) == 0x102 || (i&~0x60) == 0x105)
541 // found VCL NAL with slice header i.e. start of current primary coded
542 // picture, so start scanning for the end now
543 in_picture = 1;
544 if (in_picture) {
545 i = sync_video_packet(d_video) & ~0x60; // code of next packet
546 if(i == 0x106 || i == 0x109) break; // SEI or access unit delim.
547 if(i == 0x101 || i == 0x102 || i == 0x105) {
548 // assuming arbitrary slice ordering is not allowed, the
549 // first_mb_in_slice (golomb encoded) value should be 0 then
550 // for the first VCL NAL in a picture
551 if (demux_peekc(d_video) & 0x80)
552 break;
556 *start=videobuffer; in_size=videobuf_len;
557 videobuf_len=0;
558 } else if(video_codec == VIDEO_VC1) {
559 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE) {
560 int i=sync_video_packet(d_video);
561 if(!i) return -1;
562 if(!read_video_packet(d_video)) return -1; // EOF
563 if(i==0x10D) break;
565 *start=videobuffer;
566 in_size=videobuf_len;
567 videobuf_len=0;
568 } else {
569 // frame-based file formats: (AVI,ASF,MOV)
570 in_size=ds_get_packet(d_video,start);
571 if(in_size<0) return -1; // EOF
575 //------------------------ frame decoded. --------------------
577 // Increase video timers:
578 sh_video->num_frames+=frame_time;
579 ++sh_video->num_frames_decoded;
581 frame_time*=sh_video->frametime;
583 // override frame_time for variable/unknown FPS formats:
584 if(!force_fps) switch(demuxer->file_format){
585 case DEMUXER_TYPE_GIF:
586 case DEMUXER_TYPE_MATROSKA:
587 case DEMUXER_TYPE_MNG:
588 if(d_video->pts>0 && pts1>0 && d_video->pts>pts1)
589 frame_time=d_video->pts-pts1;
590 break;
591 case DEMUXER_TYPE_TV:
592 case DEMUXER_TYPE_MOV:
593 case DEMUXER_TYPE_FILM:
594 case DEMUXER_TYPE_VIVO:
595 case DEMUXER_TYPE_OGG:
596 case DEMUXER_TYPE_ASF: {
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 if(d>0){
601 if((int)sh_video->fps==1000)
602 mp_msg(MSGT_CPLAYER,MSGL_V,"\navg. framerate: %d fps \n",(int)(1.0f/d));
603 sh_video->frametime=d; // 1ms
604 sh_video->fps=1.0f/d;
606 frame_time = d;
607 } else {
608 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);
609 // frame_time = 1/25.0;
612 break;
613 case DEMUXER_TYPE_LAVF:
614 case DEMUXER_TYPE_LAVF_PREFERRED:
615 if((int)sh_video->fps==1000 || (int)sh_video->fps<=1){
616 double next_pts = ds_get_next_pts(d_video);
617 double d= (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts-pts1;
618 if(d>=0){
619 frame_time = d;
622 break;
623 case DEMUXER_TYPE_REAL:
625 double next_pts = ds_get_next_pts(d_video);
626 double d = (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts - pts1;
628 frame_time = (d >= 0 && pts1 > 0) ? d : 0.001;
630 break;
633 if(video_codec == VIDEO_MPEG12){
634 sh_video->pts+=frame_time;
635 if(picture_coding_type==1)
636 d_video->flags |= 1;
637 if(picture_coding_type<=2 && sh_video->i_pts){
638 sh_video->pts=sh_video->i_pts;
639 sh_video->i_pts=pts;
640 } else {
641 if(pts){
642 if(picture_coding_type<=2) sh_video->i_pts=pts;
643 else sh_video->pts=pts;
646 } else
647 sh_video->pts=d_video->pts;
649 if(frame_time_ptr) *frame_time_ptr=frame_time;
650 return in_size;