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