sd_ass: initialize structs for external tracks properly
[mplayer.git] / libmpdemux / video.c
blob1ea85190ea5edd8007385b7f005a3fb0af67c05d
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 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include "mp_msg.h"
30 #include "stream/stream.h"
31 #include "demuxer.h"
32 #ifdef DEMUX_TY_OSD
33 #include "demux_ty_osd.h"
34 #endif
35 #include "stheader.h"
36 #include "parse_es.h"
37 #include "mpeg_hdr.h"
39 /* sub_cc (closed captions)*/
40 #include "sub/sub_cc.h"
42 /* biCompression constant */
43 #define BI_RGB 0L
45 static mp_mpeg_header_t picture;
47 static int telecine=0;
48 static float telecine_cnt=-2.5;
50 typedef enum {
51 VIDEO_MPEG12,
52 VIDEO_MPEG4,
53 VIDEO_H264,
54 VIDEO_VC1,
55 VIDEO_OTHER
56 } video_codec_t;
58 static video_codec_t find_video_codec(sh_video_t *sh_video)
60 demux_stream_t *d_video=sh_video->ds;
61 int fmt = d_video->demuxer->file_format;
63 if(
64 (fmt == DEMUXER_TYPE_PVA) ||
65 (fmt == DEMUXER_TYPE_MPEG_ES) ||
66 (fmt == DEMUXER_TYPE_MPEG_GXF) ||
67 (fmt == DEMUXER_TYPE_MPEG_PES) ||
69 (fmt == DEMUXER_TYPE_MPEG_PS || fmt == DEMUXER_TYPE_MPEG_TS) &&
70 ((! sh_video->format) || (sh_video->format==0x10000001) || (sh_video->format==0x10000002))
71 ) ||
72 (fmt == DEMUXER_TYPE_MPEG_TY)
74 return VIDEO_MPEG12;
75 else if((fmt == DEMUXER_TYPE_MPEG4_ES) ||
76 ((fmt == DEMUXER_TYPE_MPEG_TS) && (sh_video->format==0x10000004)) ||
77 ((fmt == DEMUXER_TYPE_MPEG_PS) && (sh_video->format==0x10000004))
79 return VIDEO_MPEG4;
80 else if((fmt == DEMUXER_TYPE_H264_ES) ||
81 ((fmt == DEMUXER_TYPE_MPEG_TS) && (sh_video->format==0x10000005)) ||
82 ((fmt == DEMUXER_TYPE_MPEG_PS) && (sh_video->format==0x10000005))
84 return VIDEO_H264;
85 else if((fmt == DEMUXER_TYPE_MPEG_PS || fmt == DEMUXER_TYPE_MPEG_TS) &&
86 (sh_video->format==mmioFOURCC('W', 'V', 'C', '1')))
87 return VIDEO_VC1;
88 else if (fmt == DEMUXER_TYPE_ASF && sh_video->bih && sh_video->bih->biCompression == mmioFOURCC('D', 'V', 'R', ' '))
89 return VIDEO_MPEG12;
90 else
91 return VIDEO_OTHER;
94 int video_read_properties(sh_video_t *sh_video){
95 demux_stream_t *d_video=sh_video->ds;
96 video_codec_t video_codec = find_video_codec(sh_video);
97 // Determine image properties:
98 switch(video_codec){
99 case VIDEO_OTHER: {
100 if((d_video->demuxer->file_format == DEMUXER_TYPE_ASF) || (d_video->demuxer->file_format == DEMUXER_TYPE_AVI)) {
101 // display info:
102 // in case no strf chunk has been seen in avi, we have no bitmap header
103 if(!sh_video->bih) return 0;
104 sh_video->format=sh_video->bih->biCompression;
105 sh_video->disp_w=sh_video->bih->biWidth;
106 sh_video->disp_h=abs(sh_video->bih->biHeight);
108 break;
110 case VIDEO_MPEG4: {
111 int pos = 0, vop_cnt=0, units[3];
112 videobuf_len=0; videobuf_code_len=0;
113 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for Video Object Start code... ");
114 while(1){
115 int i=sync_video_packet(d_video);
116 if(i<=0x11F) break; // found it!
117 if(!i || !skip_video_packet(d_video)){
118 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
119 return 0;
122 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
123 if(!videobuffer) {
124 videobuffer = av_malloc(VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
125 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
126 else {
127 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"Cannot allocate shared memory.\n");
128 return 0;
131 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for Video Object Layer Start code... ");
132 while(1){
133 int i=sync_video_packet(d_video);
134 mp_msg(MSGT_DECVIDEO,MSGL_V,"M4V: 0x%X\n",i);
135 if(i>=0x120 && i<=0x12F) break; // found it!
136 if(!i || !read_video_packet(d_video)){
137 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
138 return 0;
141 pos = videobuf_len+4;
142 if(!read_video_packet(d_video)){
143 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read Video Object Layer Header\n");
144 return 0;
146 mp4_header_process_vol(&picture, &(videobuffer[pos]));
147 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK! FPS SEEMS TO BE %.3f\nSearching for Video Object Plane Start code... ", sh_video->fps);
148 mp4_init:
149 while(1){
150 int i=sync_video_packet(d_video);
151 if(i==0x1B6) break; // found it!
152 if(!i || !read_video_packet(d_video)){
153 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
154 return 0;
157 pos = videobuf_len+4;
158 if(!read_video_packet(d_video)){
159 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read Video Object Plane Header\n");
160 return 0;
162 mp4_header_process_vop(&picture, &(videobuffer[pos]));
163 sh_video->disp_w = picture.display_picture_width;
164 sh_video->disp_h = picture.display_picture_height;
165 units[vop_cnt] = picture.timeinc_unit;
166 vop_cnt++;
167 //mp_msg(MSGT_DECVIDEO,MSGL_V, "TYPE: %d, unit: %d\n", picture.picture_type, picture.timeinc_unit);
168 if(!picture.fps) {
169 int i, mn, md, mx, diff;
170 if(vop_cnt < 3)
171 goto mp4_init;
173 i=0;
174 mn = mx = units[0];
175 for(i=0; i<3; i++) {
176 if(units[i] < mn)
177 mn = units[i];
178 if(units[i] > mx)
179 mx = units[i];
181 md = mn;
182 for(i=0; i<3; i++) {
183 if((units[i] > mn) && (units[i] < mx))
184 md = units[i];
186 mp_msg(MSGT_DECVIDEO,MSGL_V, "MIN: %d, mid: %d, max: %d\n", mn, md, mx);
187 if(mx - md > md - mn)
188 diff = md - mn;
189 else
190 diff = mx - md;
191 if(diff > 0){
192 picture.fps = ((float)picture.timeinc_resolution) / diff;
193 mp_msg(MSGT_DECVIDEO,MSGL_V, "FPS seems to be: %f, resolution: %d, delta_units: %d\n", picture.fps, picture.timeinc_resolution, diff);
196 if(picture.fps) {
197 sh_video->fps=picture.fps;
198 sh_video->frametime=1.0/picture.fps;
199 mp_msg(MSGT_DECVIDEO,MSGL_INFO, "FPS seems to be: %f\n", picture.fps);
201 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
202 sh_video->format=0x10000004;
203 break;
205 case VIDEO_H264: {
206 int pos = 0;
207 videobuf_len=0; videobuf_code_len=0;
208 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for sequence parameter set... ");
209 while(1){
210 int i=sync_video_packet(d_video);
211 if((i&~0x60) == 0x107 && i != 0x107) break; // found it!
212 if(!i || !skip_video_packet(d_video)){
213 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
214 return 0;
217 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
218 if(!videobuffer) {
219 videobuffer = av_malloc(VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
220 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
221 else {
222 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"Cannot allocate shared memory.\n");
223 return 0;
226 pos = videobuf_len+4;
227 if(!read_video_packet(d_video)){
228 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read sequence parameter set\n");
229 return 0;
231 h264_parse_sps(&picture, &(videobuffer[pos]), videobuf_len - pos);
232 sh_video->disp_w=picture.display_picture_width;
233 sh_video->disp_h=picture.display_picture_height;
234 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for picture parameter set... ");
235 while(1){
236 int i=sync_video_packet(d_video);
237 mp_msg(MSGT_DECVIDEO,MSGL_V,"H264: 0x%X\n",i);
238 if((i&~0x60) == 0x108 && i != 0x108) break; // found it!
239 if(!i || !read_video_packet(d_video)){
240 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
241 return 0;
244 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\nSearching for Slice... ");
245 while(1){
246 int i=sync_video_packet(d_video);
247 if((i&~0x60) == 0x101 || (i&~0x60) == 0x102 || (i&~0x60) == 0x105) 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!\n");
254 sh_video->format=0x10000005;
255 if(picture.fps) {
256 sh_video->fps=picture.fps;
257 sh_video->frametime=1.0/picture.fps;
258 mp_msg(MSGT_DECVIDEO,MSGL_INFO, "FPS seems to be: %f\n", picture.fps);
260 break;
262 case VIDEO_MPEG12: {
263 if (d_video->demuxer->file_format == DEMUXER_TYPE_ASF) { // DVR-MS
264 if(!sh_video->bih) return 0;
265 sh_video->format=sh_video->bih->biCompression;
267 mpeg_header_parser:
268 // Find sequence_header first:
269 videobuf_len=0; videobuf_code_len=0;
270 telecine=0; telecine_cnt=-2.5;
271 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for sequence header... ");
272 while(1){
273 int i=sync_video_packet(d_video);
274 if(i==0x1B3) break; // found it!
275 if(!i || !skip_video_packet(d_video)){
276 if( mp_msg_test(MSGT_DECVIDEO,MSGL_V) ) mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
277 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"MPEG: FATAL: EOF while searching for sequence header.\n");
278 return 0;
281 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
282 // ========= Read & process sequence header & extension ============
283 if(!videobuffer) {
284 videobuffer = av_malloc(VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
285 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
286 else {
287 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"Cannot allocate shared memory.\n");
288 return 0;
292 if(!read_video_packet(d_video)){
293 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"FATAL: Cannot read sequence header.\n");
294 return 0;
296 if(mp_header_process_sequence_header (&picture, &videobuffer[4])) {
297 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"MPEG: bad sequence header\n");
298 goto mpeg_header_parser;
300 if(sync_video_packet(d_video)==0x1B5){ // next packet is seq. ext.
301 int pos=videobuf_len;
302 if(!read_video_packet(d_video)){
303 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"FATAL: Cannot read sequence header extension.\n");
304 return 0;
306 if(mp_header_process_extension (&picture, &videobuffer[pos+4])) {
307 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"MPEG: bad sequence header extension\n");
308 return 0;
312 // display info:
313 sh_video->format=picture.mpeg1?0x10000001:0x10000002; // mpeg video
314 sh_video->fps=picture.fps * picture.frame_rate_extension_n / picture.frame_rate_extension_d;
315 if(!sh_video->fps){
316 sh_video->frametime=0;
317 } else {
318 sh_video->frametime=1.0/sh_video->fps;
320 sh_video->disp_w=picture.display_picture_width;
321 sh_video->disp_h=picture.display_picture_height;
322 // bitrate:
323 if(picture.bitrate!=0x3FFFF) // unspecified/VBR ?
324 sh_video->i_bps=picture.bitrate * 400 / 8;
325 // info:
326 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2,"mpeg bitrate: %d (%X)\n",picture.bitrate,picture.bitrate);
327 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"VIDEO: %s %dx%d (aspect %d) %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
328 picture.mpeg1?"MPEG1":"MPEG2",
329 sh_video->disp_w,sh_video->disp_h,
330 picture.aspect_ratio_information,
331 sh_video->fps,
332 sh_video->i_bps * 8 / 1000.0,
333 sh_video->i_bps / 1000.0 );
334 break;
336 case VIDEO_VC1: {
337 // Find sequence_header:
338 videobuf_len=0;
339 videobuf_code_len=0;
340 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"Searching for VC1 sequence header... ");
341 while(1){
342 int i=sync_video_packet(d_video);
343 if(i==0x10F) break; // found it!
344 if(!i || !skip_video_packet(d_video)){
345 if( mp_msg_test(MSGT_DECVIDEO,MSGL_V) ) mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
346 mp_msg(MSGT_DECVIDEO,MSGL_ERR, "Couldn't find VC-1 sequence header\n");
347 return 0;
350 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"found\n");
351 if(!videobuffer) {
352 videobuffer = av_malloc(VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
353 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
354 else {
355 mp_tmsg(MSGT_DECVIDEO,MSGL_ERR,"Cannot allocate shared memory.\n");
356 return 0;
359 if(!read_video_packet(d_video)){
360 mp_msg(MSGT_DECVIDEO,MSGL_ERR, "Couldn't read VC-1 sequence header!\n");
361 return 0;
364 while(1) {
365 int i=sync_video_packet(d_video);
366 if(i==0x10E) break; // found it!
367 if(!i || !skip_video_packet(d_video)){
368 mp_msg(MSGT_DECVIDEO,MSGL_V,"Couldn't find VC-1 entry point sync-code:(\n");
369 return 0;
372 if(!read_video_packet(d_video)){
373 mp_msg(MSGT_DECVIDEO,MSGL_V,"Couldn't read VC-1 entry point sync-code:(\n");
374 return 0;
377 if(mp_vc1_decode_sequence_header(&picture, &videobuffer[4], videobuf_len-4)) {
378 sh_video->bih = calloc(1, sizeof(*sh_video->bih) + videobuf_len);
379 if(sh_video->bih == NULL) {
380 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Couldn't alloc %zu bytes for VC-1 extradata!\n", sizeof(*sh_video->bih) + videobuf_len);
381 return 0;
383 sh_video->bih->biSize= sizeof(*sh_video->bih) + videobuf_len;
384 memcpy(sh_video->bih + 1, videobuffer, videobuf_len);
385 sh_video->bih->biCompression = sh_video->format;
386 sh_video->bih->biWidth = sh_video->disp_w = picture.display_picture_width;
387 sh_video->bih->biHeight = sh_video->disp_h = picture.display_picture_height;
388 if(picture.fps > 0) {
389 sh_video->frametime=1.0/picture.fps;
390 sh_video->fps = picture.fps;
392 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"VIDEO: VC-1 %dx%d, %5.3f fps, header len: %d\n",
393 sh_video->disp_w, sh_video->disp_h, sh_video->fps, videobuf_len);
395 break;
397 } // switch(file_format)
399 return 1;
402 static void process_userdata(const unsigned char* buf,int len){
403 int i;
404 /* if the user data starts with "CC", assume it is a CC info packet */
405 if(len>2 && buf[0]=='C' && buf[1]=='C'){
406 // mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"video.c: process_userdata() detected Closed Captions!\n");
407 subcc_process_data(buf+2,len-2);
409 #ifdef DEMUX_TY_OSD
410 if( len > 2 && buf[ 0 ] == 'T' && buf[ 1 ] == 'Y' )
412 ty_processuserdata( buf + 2, len - 2 );
413 return;
415 #endif
416 if(verbose<2) return;
417 fprintf(stderr, "user_data: len=%3d %02X %02X %02X %02X '",
418 len, buf[0], buf[1], buf[2], buf[3]);
419 for(i=0;i<len;i++)
420 // if(buf[i]>=32 && buf[i]<127) fputc(buf[i], stderr);
421 if(buf[i]&0x60) fputc(buf[i]&0x7F, stderr);
422 fprintf(stderr, "'\n");
425 int video_read_frame(sh_video_t* sh_video,float* frame_time_ptr,unsigned char** start,int force_fps){
426 demux_stream_t *d_video=sh_video->ds;
427 demuxer_t *demuxer=d_video->demuxer;
428 float frame_time=1;
429 float pts1=d_video->pts;
430 float pts=0;
431 float fps;
432 int picture_coding_type=0;
433 int in_size=0;
434 video_codec_t video_codec = find_video_codec(sh_video);
436 *start=NULL;
438 if(video_codec == VIDEO_MPEG12){
439 int in_frame=0;
440 //float newfps;
441 //videobuf_len=0;
442 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
443 int i=sync_video_packet(d_video);
444 //void* buffer=&videobuffer[videobuf_len+4];
445 int start=videobuf_len+4;
446 if(in_frame){
447 if(i<0x101 || i>=0x1B0){ // not slice code -> end of frame
448 if(!i) return -1; // EOF
449 break;
451 } else {
452 if(i==0x100){
453 pts=d_video->pts;
454 d_video->pts=0;
456 if(i>=0x101 && i<0x1B0) in_frame=1; // picture startcode
457 else if(!i) return -1; // EOF
459 if(!read_video_packet(d_video)) return -1; // EOF
460 // process headers:
461 switch(i){
462 case 0x1B3: mp_header_process_sequence_header (&picture, &videobuffer[start]);break;
463 case 0x1B5: mp_header_process_extension (&picture, &videobuffer[start]);break;
464 case 0x1B2: process_userdata (&videobuffer[start], videobuf_len-start);break;
465 case 0x100: picture_coding_type=(videobuffer[start+1] >> 3) & 7;break;
468 fps = picture.fps * picture.frame_rate_extension_n / picture.frame_rate_extension_d;
470 *start=videobuffer; in_size=videobuf_len;
472 // get mpeg fps:
473 if(sh_video->fps!=fps) if(!force_fps && !telecine){
474 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);
475 sh_video->fps=fps;
476 sh_video->frametime=1.0/fps;
479 // fix mpeg2 frametime:
480 frame_time=(picture.display_time)*0.01f;
481 picture.display_time=100;
482 videobuf_len=0;
484 telecine_cnt*=0.9; // drift out error
485 telecine_cnt+=frame_time-5.0/4.0;
486 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"\r telecine = %3.1f %5.3f \n",frame_time,telecine_cnt);
488 if(telecine){
489 frame_time=1;
490 if(telecine_cnt<-1.5 || telecine_cnt>1.5){
491 mp_tmsg(MSGT_DECVIDEO,MSGL_INFO,"\ndemux_mpg: 30000/1001fps NTSC content detected, switching framerate.\n");
492 telecine=0;
494 } else
495 if(telecine_cnt>-0.5 && telecine_cnt<0.5 && !force_fps){
496 sh_video->fps=sh_video->fps*4/5;
497 sh_video->frametime=sh_video->frametime*5/4;
498 mp_tmsg(MSGT_DECVIDEO,MSGL_INFO,"\ndemux_mpg: 24000/1001fps progressive NTSC content detected, switching framerate.\n");
499 telecine=1;
501 } else if(video_codec == VIDEO_MPEG4){
502 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
503 int i=sync_video_packet(d_video);
504 if(!i) return -1;
505 if(!read_video_packet(d_video)) return -1; // EOF
506 if(i==0x1B6) break;
508 *start=videobuffer; in_size=videobuf_len;
509 videobuf_len=0;
510 } else if(video_codec == VIDEO_H264){
511 int in_picture = 0;
512 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
513 int i=sync_video_packet(d_video);
514 int pos = videobuf_len+4;
515 if(!i) return -1;
516 if(!read_video_packet(d_video)) return -1; // EOF
517 if((i&~0x60) == 0x107 && i != 0x107) {
518 h264_parse_sps(&picture, &(videobuffer[pos]), videobuf_len - pos);
519 if(picture.fps > 0) {
520 sh_video->fps=picture.fps;
521 sh_video->frametime=1.0/picture.fps;
523 i=sync_video_packet(d_video);
524 if(!i) return -1;
525 if(!read_video_packet(d_video)) return -1; // EOF
528 // here starts the access unit end detection code
529 // see the mail on MPlayer-dev-eng for details:
530 // Date: Sat, 17 Sep 2005 11:24:06 +0200
531 // Subject: Re: [MPlayer-dev-eng] [RFC] h264 ES parser problems
532 // Message-ID: <20050917092406.GA7699@rz.uni-karlsruhe.de>
533 if((i&~0x60) == 0x101 || (i&~0x60) == 0x102 || (i&~0x60) == 0x105)
534 // found VCL NAL with slice header i.e. start of current primary coded
535 // picture, so start scanning for the end now
536 in_picture = 1;
537 if (in_picture) {
538 i = sync_video_packet(d_video) & ~0x60; // code of next packet
539 if(i == 0x106 || i == 0x109) break; // SEI or access unit delim.
540 if(i == 0x101 || i == 0x102 || i == 0x105) {
541 // assuming arbitrary slice ordering is not allowed, the
542 // first_mb_in_slice (golomb encoded) value should be 0 then
543 // for the first VCL NAL in a picture
544 if (demux_peekc(d_video) & 0x80)
545 break;
549 *start=videobuffer; in_size=videobuf_len;
550 videobuf_len=0;
551 } else if(video_codec == VIDEO_VC1) {
552 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE) {
553 int i=sync_video_packet(d_video);
554 if(!i) return -1;
555 if(!read_video_packet(d_video)) return -1; // EOF
556 if(i==0x10D) break;
558 *start=videobuffer;
559 in_size=videobuf_len;
560 videobuf_len=0;
561 } else {
562 // frame-based file formats: (AVI,ASF,MOV)
563 in_size=ds_get_packet(d_video,start);
564 if(in_size<0) return -1; // EOF
568 //------------------------ frame decoded. --------------------
570 // Increase video timers:
571 sh_video->num_frames+=frame_time;
572 ++sh_video->num_frames_decoded;
574 frame_time*=sh_video->frametime;
576 // override frame_time for variable/unknown FPS formats:
577 if(!force_fps) switch(demuxer->file_format){
578 case DEMUXER_TYPE_GIF:
579 case DEMUXER_TYPE_MATROSKA:
580 case DEMUXER_TYPE_MNG:
581 if(d_video->pts>0 && pts1>0 && d_video->pts>pts1)
582 frame_time=d_video->pts-pts1;
583 break;
584 case DEMUXER_TYPE_TV:
585 case DEMUXER_TYPE_MOV:
586 case DEMUXER_TYPE_FILM:
587 case DEMUXER_TYPE_VIVO:
588 case DEMUXER_TYPE_OGG:
589 case DEMUXER_TYPE_ASF: {
590 double next_pts = ds_get_next_pts(d_video);
591 double d= (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts-pts1;
592 if(d>=0){
593 if(d>0){
594 if((int)sh_video->fps==1000)
595 mp_msg(MSGT_CPLAYER,MSGL_V,"\navg. framerate: %d fps \n",(int)(1.0f/d));
596 sh_video->frametime=d; // 1ms
597 sh_video->fps=1.0f/d;
599 frame_time = d;
600 } else {
601 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);
602 // frame_time = 1/25.0;
605 break;
606 case DEMUXER_TYPE_LAVF:
607 case DEMUXER_TYPE_LAVF_PREFERRED:
608 if((int)sh_video->fps==1000 || (int)sh_video->fps<=1){
609 double next_pts = ds_get_next_pts(d_video);
610 double d= (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts-pts1;
611 if(d>=0){
612 frame_time = d;
615 break;
616 case DEMUXER_TYPE_REAL:
618 double next_pts = ds_get_next_pts(d_video);
619 double d = (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts - pts1;
621 frame_time = (d >= 0 && pts1 > 0) ? d : 0.001;
623 break;
626 if(video_codec == VIDEO_MPEG12){
627 sh_video->pts+=frame_time;
628 if(picture_coding_type==1)
629 d_video->keyframe = true;
630 if(picture_coding_type<=2 && sh_video->i_pts){
631 sh_video->pts=sh_video->i_pts;
632 sh_video->i_pts=pts;
633 } else {
634 if(pts){
635 if(picture_coding_type<=2) sh_video->i_pts=pts;
636 else sh_video->pts=pts;
639 } else
640 sh_video->pts=d_video->pts;
642 if(frame_time_ptr) *frame_time_ptr=frame_time;
643 return in_size;