Fix vf_tcdump's compilation
[mplayer/kovensky.git] / libmpdemux / parse_es.c
blob4dc20b86fde87240bf5eab5c2cc5476b620284d7
1 /*
2 * MPEG-ES video parser
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 <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
25 #include "config.h"
26 #include "mp_msg.h"
28 #include "stream/stream.h"
29 #include "demuxer.h"
30 #include "parse_es.h"
32 //static unsigned char videobuffer[MAX_VIDEO_PACKET_SIZE];
33 unsigned char* videobuffer=NULL;
34 int videobuf_len=0;
35 int next_nal = -1;
36 ///! legacy variable, 4 if stream is synced, 0 if not
37 int videobuf_code_len=0;
39 #define MAX_SYNCLEN (10 * 1024 * 1024)
40 // sync video stream, and returns next packet code
41 int sync_video_packet(demux_stream_t *ds){
42 if (!videobuf_code_len) {
43 int skipped=0;
44 if (!demux_pattern_3(ds, NULL, MAX_SYNCLEN, &skipped, 0x100)) {
45 if (skipped == MAX_SYNCLEN)
46 mp_msg(MSGT_DEMUXER, MSGL_ERR, "parse_es: could not sync video stream!\n");
47 goto eof_out;
49 next_nal = demux_getc(ds);
50 if (next_nal < 0)
51 goto eof_out;
52 videobuf_code_len = 4;
53 if(skipped) mp_dbg(MSGT_PARSEES,MSGL_DBG2,"videobuf: %d bytes skipped (next: 0x1%02X)\n",skipped,next_nal);
55 return 0x100|next_nal;
57 eof_out:
58 next_nal = -1;
59 videobuf_code_len = 0;
60 return 0;
63 // return: packet length
64 int read_video_packet(demux_stream_t *ds){
65 int packet_start;
66 int res, read;
68 if (VIDEOBUFFER_SIZE - videobuf_len < 5)
69 return 0;
70 // SYNC STREAM
71 // if(!sync_video_packet(ds)) return 0; // cannot sync (EOF)
73 // COPY STARTCODE:
74 packet_start=videobuf_len;
75 videobuffer[videobuf_len+0]=0;
76 videobuffer[videobuf_len+1]=0;
77 videobuffer[videobuf_len+2]=1;
78 videobuffer[videobuf_len+3]=next_nal;
79 videobuf_len+=4;
81 // READ PACKET:
82 res = demux_pattern_3(ds, &videobuffer[videobuf_len],
83 VIDEOBUFFER_SIZE - videobuf_len, &read, 0x100);
84 videobuf_len += read;
85 if (!res)
86 goto eof_out;
88 videobuf_len-=3;
90 mp_dbg(MSGT_PARSEES,MSGL_DBG2,"videobuf: packet 0x1%02X len=%d (total=%d)\n",videobuffer[packet_start+3],videobuf_len-packet_start,videobuf_len);
92 // Save next packet code:
93 next_nal = demux_getc(ds);
94 if (next_nal < 0)
95 goto eof_out;
96 videobuf_code_len=4;
98 return videobuf_len-packet_start;
100 eof_out:
101 next_nal = -1;
102 videobuf_code_len = 0;
103 return videobuf_len - packet_start;
106 // return: next packet code
107 int skip_video_packet(demux_stream_t *ds){
109 // SYNC STREAM
110 // if(!sync_video_packet(ds)) return 0; // cannot sync (EOF)
112 videobuf_code_len=0; // force resync
114 // SYNC AGAIN:
115 return sync_video_packet(ds);