mixer: fix lowering hw volume while muted
[mplayer.git] / libmpdemux / video.c
blobdef9cac53c8f7894a13e7c56911dcf8fe4a6d1dd
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 #ifdef DEMUX_TY_OSD
36 #include "demux_ty_osd.h"
37 #endif
38 #include "stheader.h"
39 #include "parse_es.h"
40 #include "mpeg_hdr.h"
42 /* sub_cc (closed captions)*/
43 #include "sub/sub_cc.h"
45 /* biCompression constant */
46 #define BI_RGB 0L
48 static mp_mpeg_header_t picture;
50 static int telecine=0;
51 static float telecine_cnt=-2.5;
53 typedef enum {
54 VIDEO_MPEG12,
55 VIDEO_MPEG4,
56 VIDEO_H264,
57 VIDEO_VC1,
58 VIDEO_OTHER
59 } video_codec_t;
61 static video_codec_t find_video_codec(sh_video_t *sh_video)
63 demux_stream_t *d_video=sh_video->ds;
64 int fmt = d_video->demuxer->file_format;
66 if(
67 (fmt == DEMUXER_TYPE_PVA) ||
68 (fmt == DEMUXER_TYPE_MPEG_ES) ||
69 (fmt == DEMUXER_TYPE_MPEG_GXF) ||
70 (fmt == DEMUXER_TYPE_MPEG_PES) ||
72 (fmt == DEMUXER_TYPE_MPEG_PS || fmt == DEMUXER_TYPE_MPEG_TS) &&
73 ((! sh_video->format) || (sh_video->format==0x10000001) || (sh_video->format==0x10000002))
74 ) ||
75 (fmt == DEMUXER_TYPE_MPEG_TY)
77 return VIDEO_MPEG12;
78 else if((fmt == DEMUXER_TYPE_MPEG4_ES) ||
79 ((fmt == DEMUXER_TYPE_MPEG_TS) && (sh_video->format==0x10000004)) ||
80 ((fmt == DEMUXER_TYPE_MPEG_PS) && (sh_video->format==0x10000004))
82 return VIDEO_MPEG4;
83 else if((fmt == DEMUXER_TYPE_H264_ES) ||
84 ((fmt == DEMUXER_TYPE_MPEG_TS) && (sh_video->format==0x10000005)) ||
85 ((fmt == DEMUXER_TYPE_MPEG_PS) && (sh_video->format==0x10000005))
87 return VIDEO_H264;
88 else if((fmt == DEMUXER_TYPE_MPEG_PS || fmt == DEMUXER_TYPE_MPEG_TS) &&
89 (sh_video->format==mmioFOURCC('W', 'V', 'C', '1')))
90 return VIDEO_VC1;
91 else if (fmt == DEMUXER_TYPE_ASF && sh_video->bih && sh_video->bih->biCompression == mmioFOURCC('D', 'V', 'R', ' '))
92 return VIDEO_MPEG12;
93 else
94 return VIDEO_OTHER;
97 int video_read_properties(sh_video_t *sh_video){
98 demux_stream_t *d_video=sh_video->ds;
99 video_codec_t video_codec = find_video_codec(sh_video);
100 // Determine image properties:
101 switch(video_codec){
102 case VIDEO_OTHER: {
103 if((d_video->demuxer->file_format == DEMUXER_TYPE_ASF) || (d_video->demuxer->file_format == DEMUXER_TYPE_AVI)) {
104 // display info:
105 // in case no strf chunk has been seen in avi, we have no bitmap header
106 if(!sh_video->bih) return 0;
107 sh_video->format=sh_video->bih->biCompression;
108 sh_video->disp_w=sh_video->bih->biWidth;
109 sh_video->disp_h=abs(sh_video->bih->biHeight);
111 break;
113 case VIDEO_MPEG4: {
114 int pos = 0, vop_cnt=0, units[3];
115 videobuf_len=0; videobuf_code_len=0;
116 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for Video Object Start code... ");
117 while(1){
118 int i=sync_video_packet(d_video);
119 if(i<=0x11F) break; // found it!
120 if(!i || !skip_video_packet(d_video)){
121 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
122 return 0;
125 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
126 if(!videobuffer) {
127 videobuffer = memalign(8, VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
128 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
129 else {
130 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"Cannot allocate shared memory.\n");
131 return 0;
134 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for Video Object Layer Start code... ");
135 while(1){
136 int i=sync_video_packet(d_video);
137 mp_msg(MSGT_DECVIDEO,MSGL_V,"M4V: 0x%X\n",i);
138 if(i>=0x120 && i<=0x12F) break; // found it!
139 if(!i || !read_video_packet(d_video)){
140 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
141 return 0;
144 pos = videobuf_len+4;
145 if(!read_video_packet(d_video)){
146 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read Video Object Layer Header\n");
147 return 0;
149 mp4_header_process_vol(&picture, &(videobuffer[pos]));
150 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK! FPS SEEMS TO BE %.3f\nSearching for Video Object Plane Start code... ", sh_video->fps);
151 mp4_init:
152 while(1){
153 int i=sync_video_packet(d_video);
154 if(i==0x1B6) break; // found it!
155 if(!i || !read_video_packet(d_video)){
156 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
157 return 0;
160 pos = videobuf_len+4;
161 if(!read_video_packet(d_video)){
162 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read Video Object Plane Header\n");
163 return 0;
165 mp4_header_process_vop(&picture, &(videobuffer[pos]));
166 sh_video->disp_w = picture.display_picture_width;
167 sh_video->disp_h = picture.display_picture_height;
168 units[vop_cnt] = picture.timeinc_unit;
169 vop_cnt++;
170 //mp_msg(MSGT_DECVIDEO,MSGL_V, "TYPE: %d, unit: %d\n", picture.picture_type, picture.timeinc_unit);
171 if(!picture.fps) {
172 int i, mn, md, mx, diff;
173 if(vop_cnt < 3)
174 goto mp4_init;
176 i=0;
177 mn = mx = units[0];
178 for(i=0; i<3; i++) {
179 if(units[i] < mn)
180 mn = units[i];
181 if(units[i] > mx)
182 mx = units[i];
184 md = mn;
185 for(i=0; i<3; i++) {
186 if((units[i] > mn) && (units[i] < mx))
187 md = units[i];
189 mp_msg(MSGT_DECVIDEO,MSGL_V, "MIN: %d, mid: %d, max: %d\n", mn, md, mx);
190 if(mx - md > md - mn)
191 diff = md - mn;
192 else
193 diff = mx - md;
194 if(diff > 0){
195 picture.fps = ((float)picture.timeinc_resolution) / diff;
196 mp_msg(MSGT_DECVIDEO,MSGL_V, "FPS seems to be: %f, resolution: %d, delta_units: %d\n", picture.fps, picture.timeinc_resolution, diff);
199 if(picture.fps) {
200 sh_video->fps=picture.fps;
201 sh_video->frametime=1.0/picture.fps;
202 mp_msg(MSGT_DECVIDEO,MSGL_INFO, "FPS seems to be: %f\n", picture.fps);
204 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
205 sh_video->format=0x10000004;
206 break;
208 case VIDEO_H264: {
209 int pos = 0;
210 videobuf_len=0; videobuf_code_len=0;
211 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for sequence parameter set... ");
212 while(1){
213 int i=sync_video_packet(d_video);
214 if((i&~0x60) == 0x107 && i != 0x107) break; // found it!
215 if(!i || !skip_video_packet(d_video)){
216 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
217 return 0;
220 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
221 if(!videobuffer) {
222 videobuffer = memalign(8, VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
223 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
224 else {
225 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"Cannot allocate shared memory.\n");
226 return 0;
229 pos = videobuf_len+4;
230 if(!read_video_packet(d_video)){
231 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read sequence parameter set\n");
232 return 0;
234 h264_parse_sps(&picture, &(videobuffer[pos]), videobuf_len - pos);
235 sh_video->disp_w=picture.display_picture_width;
236 sh_video->disp_h=picture.display_picture_height;
237 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for picture parameter set... ");
238 while(1){
239 int i=sync_video_packet(d_video);
240 mp_msg(MSGT_DECVIDEO,MSGL_V,"H264: 0x%X\n",i);
241 if((i&~0x60) == 0x108 && i != 0x108) break; // found it!
242 if(!i || !read_video_packet(d_video)){
243 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
244 return 0;
247 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\nSearching for Slice... ");
248 while(1){
249 int i=sync_video_packet(d_video);
250 if((i&~0x60) == 0x101 || (i&~0x60) == 0x102 || (i&~0x60) == 0x105) break; // found it!
251 if(!i || !read_video_packet(d_video)){
252 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
253 return 0;
256 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
257 sh_video->format=0x10000005;
258 if(picture.fps) {
259 sh_video->fps=picture.fps;
260 sh_video->frametime=1.0/picture.fps;
261 mp_msg(MSGT_DECVIDEO,MSGL_INFO, "FPS seems to be: %f\n", picture.fps);
263 break;
265 case VIDEO_MPEG12: {
266 if (d_video->demuxer->file_format == DEMUXER_TYPE_ASF) { // DVR-MS
267 if(!sh_video->bih) return 0;
268 sh_video->format=sh_video->bih->biCompression;
270 mpeg_header_parser:
271 // Find sequence_header first:
272 videobuf_len=0; videobuf_code_len=0;
273 telecine=0; telecine_cnt=-2.5;
274 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for sequence header... ");
275 while(1){
276 int i=sync_video_packet(d_video);
277 if(i==0x1B3) break; // found it!
278 if(!i || !skip_video_packet(d_video)){
279 if( mp_msg_test(MSGT_DECVIDEO,MSGL_V) ) mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
280 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"MPEG: FATAL: EOF while searching for sequence header.\n");
281 return 0;
284 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
285 // ========= Read & process sequence header & extension ============
286 if(!videobuffer) {
287 videobuffer = memalign(8, VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
288 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
289 else {
290 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"Cannot allocate shared memory.\n");
291 return 0;
295 if(!read_video_packet(d_video)){
296 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"FATAL: Cannot read sequence header.\n");
297 return 0;
299 if(mp_header_process_sequence_header (&picture, &videobuffer[4])) {
300 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"MPEG: bad sequence header\n");
301 goto mpeg_header_parser;
303 if(sync_video_packet(d_video)==0x1B5){ // next packet is seq. ext.
304 int pos=videobuf_len;
305 if(!read_video_packet(d_video)){
306 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"FATAL: Cannot read sequence header extension.\n");
307 return 0;
309 if(mp_header_process_extension (&picture, &videobuffer[pos+4])) {
310 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"MPEG: bad sequence header extension\n");
311 return 0;
315 // display info:
316 sh_video->format=picture.mpeg1?0x10000001:0x10000002; // mpeg video
317 sh_video->fps=picture.fps * picture.frame_rate_extension_n / picture.frame_rate_extension_d;
318 if(!sh_video->fps){
319 sh_video->frametime=0;
320 } else {
321 sh_video->frametime=1.0/sh_video->fps;
323 sh_video->disp_w=picture.display_picture_width;
324 sh_video->disp_h=picture.display_picture_height;
325 // bitrate:
326 if(picture.bitrate!=0x3FFFF) // unspecified/VBR ?
327 sh_video->i_bps=picture.bitrate * 400 / 8;
328 // info:
329 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2,"mpeg bitrate: %d (%X)\n",picture.bitrate,picture.bitrate);
330 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"VIDEO: %s %dx%d (aspect %d) %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
331 picture.mpeg1?"MPEG1":"MPEG2",
332 sh_video->disp_w,sh_video->disp_h,
333 picture.aspect_ratio_information,
334 sh_video->fps,
335 sh_video->i_bps * 8 / 1000.0,
336 sh_video->i_bps / 1000.0 );
337 break;
339 case VIDEO_VC1: {
340 // Find sequence_header:
341 videobuf_len=0;
342 videobuf_code_len=0;
343 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"Searching for VC1 sequence header... ");
344 while(1){
345 int i=sync_video_packet(d_video);
346 if(i==0x10F) break; // found it!
347 if(!i || !skip_video_packet(d_video)){
348 if( mp_msg_test(MSGT_DECVIDEO,MSGL_V) ) mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
349 mp_msg(MSGT_DECVIDEO,MSGL_ERR, "Couldn't find VC-1 sequence header\n");
350 return 0;
353 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"found\n");
354 if(!videobuffer) {
355 videobuffer = memalign(8, VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
356 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
357 else {
358 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"Cannot allocate shared memory.\n");
359 return 0;
362 if(!read_video_packet(d_video)){
363 mp_msg(MSGT_DECVIDEO,MSGL_ERR, "Couldn't read VC-1 sequence header!\n");
364 return 0;
367 while(1) {
368 int i=sync_video_packet(d_video);
369 if(i==0x10E) break; // found it!
370 if(!i || !skip_video_packet(d_video)){
371 mp_msg(MSGT_DECVIDEO,MSGL_V,"Couldn't find VC-1 entry point sync-code:(\n");
372 return 0;
375 if(!read_video_packet(d_video)){
376 mp_msg(MSGT_DECVIDEO,MSGL_V,"Couldn't read VC-1 entry point sync-code:(\n");
377 return 0;
380 if(mp_vc1_decode_sequence_header(&picture, &videobuffer[4], videobuf_len-4)) {
381 sh_video->bih = calloc(1, sizeof(*sh_video->bih) + videobuf_len);
382 if(sh_video->bih == NULL) {
383 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Couldn't alloc %zu bytes for VC-1 extradata!\n", sizeof(*sh_video->bih) + videobuf_len);
384 return 0;
386 sh_video->bih->biSize= sizeof(*sh_video->bih) + videobuf_len;
387 memcpy(sh_video->bih + 1, videobuffer, videobuf_len);
388 sh_video->bih->biCompression = sh_video->format;
389 sh_video->bih->biWidth = sh_video->disp_w = picture.display_picture_width;
390 sh_video->bih->biHeight = sh_video->disp_h = picture.display_picture_height;
391 if(picture.fps > 0) {
392 sh_video->frametime=1.0/picture.fps;
393 sh_video->fps = picture.fps;
395 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"VIDEO: VC-1 %dx%d, %5.3f fps, header len: %d\n",
396 sh_video->disp_w, sh_video->disp_h, sh_video->fps, videobuf_len);
398 break;
400 } // switch(file_format)
402 return 1;
405 static void process_userdata(const unsigned char* buf,int len){
406 int i;
407 /* if the user data starts with "CC", assume it is a CC info packet */
408 if(len>2 && buf[0]=='C' && buf[1]=='C'){
409 // mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"video.c: process_userdata() detected Closed Captions!\n");
410 subcc_process_data(buf+2,len-2);
412 #ifdef DEMUX_TY_OSD
413 if( len > 2 && buf[ 0 ] == 'T' && buf[ 1 ] == 'Y' )
415 ty_processuserdata( buf + 2, len - 2 );
416 return;
418 #endif
419 if(verbose<2) return;
420 fprintf(stderr, "user_data: len=%3d %02X %02X %02X %02X '",
421 len, buf[0], buf[1], buf[2], buf[3]);
422 for(i=0;i<len;i++)
423 // if(buf[i]>=32 && buf[i]<127) fputc(buf[i], stderr);
424 if(buf[i]&0x60) fputc(buf[i]&0x7F, stderr);
425 fprintf(stderr, "'\n");
428 int video_read_frame(sh_video_t* sh_video,float* frame_time_ptr,unsigned char** start,int force_fps){
429 demux_stream_t *d_video=sh_video->ds;
430 demuxer_t *demuxer=d_video->demuxer;
431 float frame_time=1;
432 float pts1=d_video->pts;
433 float pts=0;
434 float fps;
435 int picture_coding_type=0;
436 int in_size=0;
437 video_codec_t video_codec = find_video_codec(sh_video);
439 *start=NULL;
441 if(video_codec == VIDEO_MPEG12){
442 int in_frame=0;
443 //float newfps;
444 //videobuf_len=0;
445 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
446 int i=sync_video_packet(d_video);
447 //void* buffer=&videobuffer[videobuf_len+4];
448 int start=videobuf_len+4;
449 if(in_frame){
450 if(i<0x101 || i>=0x1B0){ // not slice code -> end of frame
451 if(!i) return -1; // EOF
452 break;
454 } else {
455 if(i==0x100){
456 pts=d_video->pts;
457 d_video->pts=0;
459 if(i>=0x101 && i<0x1B0) in_frame=1; // picture startcode
460 else if(!i) return -1; // EOF
462 if(!read_video_packet(d_video)) return -1; // EOF
463 // process headers:
464 switch(i){
465 case 0x1B3: mp_header_process_sequence_header (&picture, &videobuffer[start]);break;
466 case 0x1B5: mp_header_process_extension (&picture, &videobuffer[start]);break;
467 case 0x1B2: process_userdata (&videobuffer[start], videobuf_len-start);break;
468 case 0x100: picture_coding_type=(videobuffer[start+1] >> 3) & 7;break;
471 fps = picture.fps * picture.frame_rate_extension_n / picture.frame_rate_extension_d;
473 *start=videobuffer; in_size=videobuf_len;
475 // get mpeg fps:
476 if(sh_video->fps!=fps) if(!force_fps && !telecine){
477 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);
478 sh_video->fps=fps;
479 sh_video->frametime=1.0/fps;
482 // fix mpeg2 frametime:
483 frame_time=(picture.display_time)*0.01f;
484 picture.display_time=100;
485 videobuf_len=0;
487 telecine_cnt*=0.9; // drift out error
488 telecine_cnt+=frame_time-5.0/4.0;
489 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"\r telecine = %3.1f %5.3f \n",frame_time,telecine_cnt);
491 if(telecine){
492 frame_time=1;
493 if(telecine_cnt<-1.5 || telecine_cnt>1.5){
494 mp_tmsg(MSGT_DECVIDEO,MSGL_INFO,"\ndemux_mpg: 30000/1001fps NTSC content detected, switching framerate.\n");
495 telecine=0;
497 } else
498 if(telecine_cnt>-0.5 && telecine_cnt<0.5 && !force_fps){
499 sh_video->fps=sh_video->fps*4/5;
500 sh_video->frametime=sh_video->frametime*5/4;
501 mp_tmsg(MSGT_DECVIDEO,MSGL_INFO,"\ndemux_mpg: 24000/1001fps progressive NTSC content detected, switching framerate.\n");
502 telecine=1;
504 } else if(video_codec == VIDEO_MPEG4){
505 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
506 int i=sync_video_packet(d_video);
507 if(!i) return -1;
508 if(!read_video_packet(d_video)) return -1; // EOF
509 if(i==0x1B6) break;
511 *start=videobuffer; in_size=videobuf_len;
512 videobuf_len=0;
513 } else if(video_codec == VIDEO_H264){
514 int in_picture = 0;
515 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
516 int i=sync_video_packet(d_video);
517 int pos = videobuf_len+4;
518 if(!i) return -1;
519 if(!read_video_packet(d_video)) return -1; // EOF
520 if((i&~0x60) == 0x107 && i != 0x107) {
521 h264_parse_sps(&picture, &(videobuffer[pos]), videobuf_len - pos);
522 if(picture.fps > 0) {
523 sh_video->fps=picture.fps;
524 sh_video->frametime=1.0/picture.fps;
526 i=sync_video_packet(d_video);
527 if(!i) return -1;
528 if(!read_video_packet(d_video)) return -1; // EOF
531 // here starts the access unit end detection code
532 // see the mail on MPlayer-dev-eng for details:
533 // Date: Sat, 17 Sep 2005 11:24:06 +0200
534 // Subject: Re: [MPlayer-dev-eng] [RFC] h264 ES parser problems
535 // Message-ID: <20050917092406.GA7699@rz.uni-karlsruhe.de>
536 if((i&~0x60) == 0x101 || (i&~0x60) == 0x102 || (i&~0x60) == 0x105)
537 // found VCL NAL with slice header i.e. start of current primary coded
538 // picture, so start scanning for the end now
539 in_picture = 1;
540 if (in_picture) {
541 i = sync_video_packet(d_video) & ~0x60; // code of next packet
542 if(i == 0x106 || i == 0x109) break; // SEI or access unit delim.
543 if(i == 0x101 || i == 0x102 || i == 0x105) {
544 // assuming arbitrary slice ordering is not allowed, the
545 // first_mb_in_slice (golomb encoded) value should be 0 then
546 // for the first VCL NAL in a picture
547 if (demux_peekc(d_video) & 0x80)
548 break;
552 *start=videobuffer; in_size=videobuf_len;
553 videobuf_len=0;
554 } else if(video_codec == VIDEO_VC1) {
555 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE) {
556 int i=sync_video_packet(d_video);
557 if(!i) return -1;
558 if(!read_video_packet(d_video)) return -1; // EOF
559 if(i==0x10D) break;
561 *start=videobuffer;
562 in_size=videobuf_len;
563 videobuf_len=0;
564 } else {
565 // frame-based file formats: (AVI,ASF,MOV)
566 in_size=ds_get_packet(d_video,start);
567 if(in_size<0) return -1; // EOF
571 //------------------------ frame decoded. --------------------
573 // Increase video timers:
574 sh_video->num_frames+=frame_time;
575 ++sh_video->num_frames_decoded;
577 frame_time*=sh_video->frametime;
579 // override frame_time for variable/unknown FPS formats:
580 if(!force_fps) switch(demuxer->file_format){
581 case DEMUXER_TYPE_GIF:
582 case DEMUXER_TYPE_MATROSKA:
583 case DEMUXER_TYPE_MNG:
584 if(d_video->pts>0 && pts1>0 && d_video->pts>pts1)
585 frame_time=d_video->pts-pts1;
586 break;
587 case DEMUXER_TYPE_TV:
588 case DEMUXER_TYPE_MOV:
589 case DEMUXER_TYPE_FILM:
590 case DEMUXER_TYPE_VIVO:
591 case DEMUXER_TYPE_OGG:
592 case DEMUXER_TYPE_ASF: {
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 if(d>0){
597 if((int)sh_video->fps==1000)
598 mp_msg(MSGT_CPLAYER,MSGL_V,"\navg. framerate: %d fps \n",(int)(1.0f/d));
599 sh_video->frametime=d; // 1ms
600 sh_video->fps=1.0f/d;
602 frame_time = d;
603 } else {
604 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);
605 // frame_time = 1/25.0;
608 break;
609 case DEMUXER_TYPE_LAVF:
610 case DEMUXER_TYPE_LAVF_PREFERRED:
611 if((int)sh_video->fps==1000 || (int)sh_video->fps<=1){
612 double next_pts = ds_get_next_pts(d_video);
613 double d= (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts-pts1;
614 if(d>=0){
615 frame_time = d;
618 break;
619 case DEMUXER_TYPE_REAL:
621 double next_pts = ds_get_next_pts(d_video);
622 double d = (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts - pts1;
624 frame_time = (d >= 0 && pts1 > 0) ? d : 0.001;
626 break;
629 if(video_codec == VIDEO_MPEG12){
630 sh_video->pts+=frame_time;
631 if(picture_coding_type==1)
632 d_video->keyframe = true;
633 if(picture_coding_type<=2 && sh_video->i_pts){
634 sh_video->pts=sh_video->i_pts;
635 sh_video->i_pts=pts;
636 } else {
637 if(pts){
638 if(picture_coding_type<=2) sh_video->i_pts=pts;
639 else sh_video->pts=pts;
642 } else
643 sh_video->pts=d_video->pts;
645 if(frame_time_ptr) *frame_time_ptr=frame_time;
646 return in_size;