Documentation: Add linebreak and try to be less ambiguous.
[mplayer/greg.git] / libmpdemux / video.c
bloba7b1ca1232e83766190d077bcea3a32562ac603f
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 units[vop_cnt] = picture.timeinc_unit;
173 vop_cnt++;
174 //mp_msg(MSGT_DECVIDEO,MSGL_V, "TYPE: %d, unit: %d\n", picture.picture_type, picture.timeinc_unit);
175 if(!picture.fps) {
176 int i, mn, md, mx, diff;
177 if(vop_cnt < 3)
178 goto mp4_init;
180 i=0;
181 mn = mx = units[0];
182 for(i=0; i<3; i++) {
183 if(units[i] < mn)
184 mn = units[i];
185 if(units[i] > mx)
186 mx = units[i];
188 md = mn;
189 for(i=0; i<3; i++) {
190 if((units[i] > mn) && (units[i] < mx))
191 md = units[i];
193 mp_msg(MSGT_DECVIDEO,MSGL_V, "MIN: %d, mid: %d, max: %d\n", mn, md, mx);
194 if(mx - md > md - mn)
195 diff = md - mn;
196 else
197 diff = mx - md;
198 if(diff > 0){
199 picture.fps = ((float)picture.timeinc_resolution) / diff;
200 mp_msg(MSGT_DECVIDEO,MSGL_V, "FPS seems to be: %f, resolution: %d, delta_units: %d\n", picture.fps, picture.timeinc_resolution, diff);
203 if(picture.fps) {
204 sh_video->fps=picture.fps;
205 sh_video->frametime=1.0/picture.fps;
206 mp_msg(MSGT_DECVIDEO,MSGL_INFO, "FPS seems to be: %f\n", picture.fps);
208 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
209 sh_video->format=0x10000004;
210 break;
212 case VIDEO_H264: {
213 int pos = 0;
214 videobuf_len=0; videobuf_code_len=0;
215 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for sequence parameter set... ");
216 while(1){
217 int i=sync_video_packet(d_video);
218 if((i&~0x60) == 0x107 && i != 0x107) break; // found it!
219 if(!i || !skip_video_packet(d_video)){
220 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
221 return 0;
224 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
225 if(!videobuffer) {
226 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
227 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
228 else {
229 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail);
230 return 0;
233 pos = videobuf_len+4;
234 if(!read_video_packet(d_video)){
235 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Can't read sequence parameter set\n");
236 return 0;
238 h264_parse_sps(&picture, &(videobuffer[pos]), videobuf_len - pos);
239 sh_video->disp_w=picture.display_picture_width;
240 sh_video->disp_h=picture.display_picture_height;
241 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for picture parameter set... ");
242 while(1){
243 int i=sync_video_packet(d_video);
244 mp_msg(MSGT_DECVIDEO,MSGL_V,"H264: 0x%X\n",i);
245 if((i&~0x60) == 0x108 && i != 0x108) break; // found it!
246 if(!i || !read_video_packet(d_video)){
247 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
248 return 0;
251 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\nSearching for Slice... ");
252 while(1){
253 int i=sync_video_packet(d_video);
254 if((i&~0x60) == 0x101 || (i&~0x60) == 0x102 || (i&~0x60) == 0x105) break; // found it!
255 if(!i || !read_video_packet(d_video)){
256 mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
257 return 0;
260 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
261 sh_video->format=0x10000005;
262 if(picture.fps) {
263 sh_video->fps=picture.fps;
264 sh_video->frametime=1.0/picture.fps;
265 mp_msg(MSGT_DECVIDEO,MSGL_INFO, "FPS seems to be: %f\n", picture.fps);
267 break;
269 case VIDEO_MPEG12: {
270 if (d_video->demuxer->file_format == DEMUXER_TYPE_ASF) { // DVR-MS
271 if(!sh_video->bih) return 0;
272 sh_video->format=sh_video->bih->biCompression;
274 mpeg_header_parser:
275 // Find sequence_header first:
276 videobuf_len=0; videobuf_code_len=0;
277 telecine=0; telecine_cnt=-2.5;
278 mp_msg(MSGT_DECVIDEO,MSGL_V,"Searching for sequence header... ");
279 while(1){
280 int i=sync_video_packet(d_video);
281 if(i==0x1B3) break; // found it!
282 if(!i || !skip_video_packet(d_video)){
283 if( mp_msg_test(MSGT_DECVIDEO,MSGL_V) ) mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
284 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_MpegNoSequHdr);
285 return 0;
288 mp_msg(MSGT_DECVIDEO,MSGL_V,"OK!\n");
289 // ========= Read & process sequence header & extension ============
290 if(!videobuffer) {
291 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
292 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
293 else {
294 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail);
295 return 0;
299 if(!read_video_packet(d_video)){
300 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_CannotReadMpegSequHdr);
301 return 0;
303 if(mp_header_process_sequence_header (&picture, &videobuffer[4])) {
304 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_BadMpegSequHdr);
305 goto mpeg_header_parser;
307 if(sync_video_packet(d_video)==0x1B5){ // next packet is seq. ext.
308 int pos=videobuf_len;
309 if(!read_video_packet(d_video)){
310 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_CannotReadMpegSequHdrEx);
311 return 0;
313 if(mp_header_process_extension (&picture, &videobuffer[pos+4])) {
314 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_BadMpegSequHdrEx);
315 return 0;
319 // display info:
320 sh_video->format=picture.mpeg1?0x10000001:0x10000002; // mpeg video
321 sh_video->fps=picture.fps * picture.frame_rate_extension_n / picture.frame_rate_extension_d;
322 if(!sh_video->fps){
323 sh_video->frametime=0;
324 } else {
325 sh_video->frametime=1.0/picture.fps;
327 sh_video->disp_w=picture.display_picture_width;
328 sh_video->disp_h=picture.display_picture_height;
329 // bitrate:
330 if(picture.bitrate!=0x3FFFF) // unspecified/VBR ?
331 sh_video->i_bps=picture.bitrate * 400 / 8;
332 // info:
333 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2,"mpeg bitrate: %d (%X)\n",picture.bitrate,picture.bitrate);
334 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"VIDEO: %s %dx%d (aspect %d) %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
335 picture.mpeg1?"MPEG1":"MPEG2",
336 sh_video->disp_w,sh_video->disp_h,
337 picture.aspect_ratio_information,
338 sh_video->fps,
339 sh_video->i_bps * 8 / 1000.0,
340 sh_video->i_bps / 1000.0 );
341 break;
343 case VIDEO_VC1: {
344 // Find sequence_header:
345 videobuf_len=0;
346 videobuf_code_len=0;
347 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"Searching for VC1 sequence header... ");
348 while(1){
349 int i=sync_video_packet(d_video);
350 if(i==0x10F) break; // found it!
351 if(!i || !skip_video_packet(d_video)){
352 if( mp_msg_test(MSGT_DECVIDEO,MSGL_V) ) mp_msg(MSGT_DECVIDEO,MSGL_V,"NONE :(\n");
353 mp_msg(MSGT_DECVIDEO,MSGL_ERR, "Couldn't find VC-1 sequence header\n");
354 return 0;
357 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"found\n");
358 if(!videobuffer) {
359 videobuffer=(char*)memalign(8,VIDEOBUFFER_SIZE + MP_INPUT_BUFFER_PADDING_SIZE);
360 if (videobuffer) memset(videobuffer+VIDEOBUFFER_SIZE, 0, MP_INPUT_BUFFER_PADDING_SIZE);
361 else {
362 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_ShMemAllocFail);
363 return 0;
366 if(!read_video_packet(d_video)){
367 mp_msg(MSGT_DECVIDEO,MSGL_ERR, "Couldn't read VC-1 sequence header!\n");
368 return 0;
371 while(1) {
372 int i=sync_video_packet(d_video);
373 if(i==0x10E) break; // found it!
374 if(!i || !skip_video_packet(d_video)){
375 mp_msg(MSGT_DECVIDEO,MSGL_V,"Couldn't find VC-1 entry point sync-code:(\n");
376 return 0;
379 if(!read_video_packet(d_video)){
380 mp_msg(MSGT_DECVIDEO,MSGL_V,"Couldn't read VC-1 entry point sync-code:(\n");
381 return 0;
384 if(mp_vc1_decode_sequence_header(&picture, &videobuffer[4], videobuf_len-4)) {
385 sh_video->bih = calloc(1, sizeof(BITMAPINFOHEADER) + videobuf_len);
386 if(sh_video->bih == NULL) {
387 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Couldn't alloc %d bytes for VC-1 extradata!\n", sizeof(BITMAPINFOHEADER) + videobuf_len);
388 return 0;
390 sh_video->bih->biSize= sizeof(BITMAPINFOHEADER) + videobuf_len;
391 memcpy(sh_video->bih + 1, videobuffer, videobuf_len);
392 sh_video->bih->biCompression = sh_video->format;
393 sh_video->bih->biWidth = sh_video->disp_w = picture.display_picture_width;
394 sh_video->bih->biHeight = sh_video->disp_h = picture.display_picture_height;
395 if(picture.fps > 0) {
396 sh_video->frametime=1.0/picture.fps;
397 sh_video->fps = picture.fps;
399 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"VIDEO: VC-1 %dx%d, %5.3f fps, header len: %d\n",
400 sh_video->disp_w, sh_video->disp_h, sh_video->fps, videobuf_len);
402 break;
404 } // switch(file_format)
406 return 1;
409 static void process_userdata(unsigned char* buf,int len){
410 int i;
411 /* if the user data starts with "CC", assume it is a CC info packet */
412 if(len>2 && buf[0]=='C' && buf[1]=='C'){
413 // mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"video.c: process_userdata() detected Closed Captions!\n");
414 subcc_process_data(buf+2,len-2);
416 if( len > 2 && buf[ 0 ] == 'T' && buf[ 1 ] == 'Y' )
418 ty_processuserdata( buf + 2, len - 2 );
419 return;
421 if(verbose<2) return;
422 fprintf(stderr, "user_data: len=%3d %02X %02X %02X %02X '",
423 len, buf[0], buf[1], buf[2], buf[3]);
424 for(i=0;i<len;i++)
425 // if(buf[i]>=32 && buf[i]<127) fputc(buf[i], stderr);
426 if(buf[i]&0x60) fputc(buf[i]&0x7F, stderr);
427 fprintf(stderr, "'\n");
430 int video_read_frame(sh_video_t* sh_video,float* frame_time_ptr,unsigned char** start,int force_fps){
431 demux_stream_t *d_video=sh_video->ds;
432 demuxer_t *demuxer=d_video->demuxer;
433 float frame_time=1;
434 float pts1=d_video->pts;
435 float pts=0;
436 float fps;
437 int picture_coding_type=0;
438 int in_size=0;
439 video_codec_t video_codec = find_video_codec(sh_video);
441 *start=NULL;
443 if(video_codec == VIDEO_MPEG12){
444 int in_frame=0;
445 //float newfps;
446 //videobuf_len=0;
447 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
448 int i=sync_video_packet(d_video);
449 //void* buffer=&videobuffer[videobuf_len+4];
450 int start=videobuf_len+4;
451 if(in_frame){
452 if(i<0x101 || i>=0x1B0){ // not slice code -> end of frame
453 if(!i) return -1; // EOF
454 break;
456 } else {
457 if(i==0x100){
458 pts=d_video->pts;
459 d_video->pts=0;
461 if(i>=0x101 && i<0x1B0) in_frame=1; // picture startcode
462 else if(!i) return -1; // EOF
464 if(!read_video_packet(d_video)) return -1; // EOF
465 // process headers:
466 switch(i){
467 case 0x1B3: mp_header_process_sequence_header (&picture, &videobuffer[start]);break;
468 case 0x1B5: mp_header_process_extension (&picture, &videobuffer[start]);break;
469 case 0x1B2: process_userdata (&videobuffer[start], videobuf_len-start);break;
470 case 0x100: picture_coding_type=(videobuffer[start+1] >> 3) & 7;break;
473 fps = picture.fps * picture.frame_rate_extension_n / picture.frame_rate_extension_d;
475 *start=videobuffer; in_size=videobuf_len;
477 // get mpeg fps:
478 if(sh_video->fps!=fps) if(!force_fps && !telecine){
479 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);
480 sh_video->fps=fps;
481 sh_video->frametime=1.0/fps;
484 // fix mpeg2 frametime:
485 frame_time=(picture.display_time)*0.01f;
486 picture.display_time=100;
487 videobuf_len=0;
489 telecine_cnt*=0.9; // drift out error
490 telecine_cnt+=frame_time-5.0/4.0;
491 mp_msg(MSGT_DECVIDEO,MSGL_DBG2,"\r telecine = %3.1f %5.3f \n",frame_time,telecine_cnt);
493 if(telecine){
494 frame_time=1;
495 if(telecine_cnt<-1.5 || telecine_cnt>1.5){
496 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_LeaveTelecineMode);
497 telecine=0;
499 } else
500 if(telecine_cnt>-0.5 && telecine_cnt<0.5 && !force_fps){
501 sh_video->fps=sh_video->fps*4/5;
502 sh_video->frametime=sh_video->frametime*5/4;
503 mp_msg(MSGT_DECVIDEO,MSGL_INFO,MSGTR_EnterTelecineMode);
504 telecine=1;
506 } else if(video_codec == VIDEO_MPEG4){
507 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
508 int i=sync_video_packet(d_video);
509 if(!i) return -1;
510 if(!read_video_packet(d_video)) return -1; // EOF
511 if(i==0x1B6) break;
513 *start=videobuffer; in_size=videobuf_len;
514 videobuf_len=0;
515 } else if(video_codec == VIDEO_H264){
516 int in_picture = 0;
517 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE){
518 int i=sync_video_packet(d_video);
519 int pos = videobuf_len+4;
520 if(!i) return -1;
521 if(!read_video_packet(d_video)) return -1; // EOF
522 if((i&~0x60) == 0x107 && i != 0x107) {
523 h264_parse_sps(&picture, &(videobuffer[pos]), videobuf_len - pos);
524 if(picture.fps > 0) {
525 sh_video->fps=picture.fps;
526 sh_video->frametime=1.0/picture.fps;
528 i=sync_video_packet(d_video);
529 if(!i) return -1;
530 if(!read_video_packet(d_video)) return -1; // EOF
533 // here starts the access unit end detection code
534 // see the mail on MPlayer-dev-eng for details:
535 // Date: Sat, 17 Sep 2005 11:24:06 +0200
536 // Subject: Re: [MPlayer-dev-eng] [RFC] h264 ES parser problems
537 // Message-ID: <20050917092406.GA7699@rz.uni-karlsruhe.de>
538 if((i&~0x60) == 0x101 || (i&~0x60) == 0x102 || (i&~0x60) == 0x105)
539 // found VCL NAL with slice header i.e. start of current primary coded
540 // picture, so start scanning for the end now
541 in_picture = 1;
542 if (in_picture) {
543 i = sync_video_packet(d_video) & ~0x60; // code of next packet
544 if(i == 0x106 || i == 0x109) break; // SEI or access unit delim.
545 if(i == 0x101 || i == 0x102 || i == 0x105) {
546 // assuming arbitrary slice ordering is not allowed, the
547 // first_mb_in_slice (golomb encoded) value should be 0 then
548 // for the first VCL NAL in a picture
549 if (demux_peekc(d_video) & 0x80)
550 break;
554 *start=videobuffer; in_size=videobuf_len;
555 videobuf_len=0;
556 } else if(video_codec == VIDEO_VC1) {
557 while(videobuf_len<VIDEOBUFFER_SIZE-MAX_VIDEO_PACKET_SIZE) {
558 int i=sync_video_packet(d_video);
559 if(!i) return -1;
560 if(!read_video_packet(d_video)) return -1; // EOF
561 if(i==0x10D) break;
563 *start=videobuffer;
564 in_size=videobuf_len;
565 videobuf_len=0;
566 } else {
567 // frame-based file formats: (AVI,ASF,MOV)
568 in_size=ds_get_packet(d_video,start);
569 if(in_size<0) return -1; // EOF
573 //------------------------ frame decoded. --------------------
575 // Increase video timers:
576 sh_video->num_frames+=frame_time;
577 ++sh_video->num_frames_decoded;
579 frame_time*=sh_video->frametime;
581 // override frame_time for variable/unknown FPS formats:
582 if(!force_fps) switch(demuxer->file_format){
583 case DEMUXER_TYPE_GIF:
584 case DEMUXER_TYPE_MATROSKA:
585 case DEMUXER_TYPE_MNG:
586 if(d_video->pts>0 && pts1>0 && d_video->pts>pts1)
587 frame_time=d_video->pts-pts1;
588 break;
589 case DEMUXER_TYPE_TV:
590 case DEMUXER_TYPE_MOV:
591 case DEMUXER_TYPE_FILM:
592 case DEMUXER_TYPE_VIVO:
593 case DEMUXER_TYPE_OGG:
594 case DEMUXER_TYPE_ASF: {
595 double next_pts = ds_get_next_pts(d_video);
596 double d= (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts-pts1;
597 if(d>=0){
598 if(d>0){
599 if((int)sh_video->fps==1000)
600 mp_msg(MSGT_CPLAYER,MSGL_V,"\navg. framerate: %d fps \n",(int)(1.0f/d));
601 sh_video->frametime=d; // 1ms
602 sh_video->fps=1.0f/d;
604 frame_time = d;
605 } else {
606 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);
607 // frame_time = 1/25.0;
610 break;
611 case DEMUXER_TYPE_LAVF:
612 case DEMUXER_TYPE_LAVF_PREFERRED:
613 if((int)sh_video->fps==1000 || (int)sh_video->fps<=1){
614 double next_pts = ds_get_next_pts(d_video);
615 double d= (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts-pts1;
616 if(d>=0){
617 frame_time = d;
620 break;
621 case DEMUXER_TYPE_REAL:
623 double next_pts = ds_get_next_pts(d_video);
624 double d = (next_pts != MP_NOPTS_VALUE) ? next_pts - d_video->pts : d_video->pts - pts1;
626 frame_time = (d >= 0 && pts1 > 0) ? d : 0.001;
628 break;
631 if(video_codec == VIDEO_MPEG12){
632 sh_video->pts+=frame_time;
633 if(picture_coding_type==1)
634 d_video->flags |= 1;
635 if(picture_coding_type<=2 && sh_video->i_pts){
636 sh_video->pts=sh_video->i_pts;
637 sh_video->i_pts=pts;
638 } else {
639 if(pts){
640 if(picture_coding_type<=2) sh_video->i_pts=pts;
641 else sh_video->pts=pts;
644 } else
645 sh_video->pts=d_video->pts;
647 if(frame_time_ptr) *frame_time_ptr=frame_time;
648 return in_size;