]> git.vomp.tv Git - vompclient.git/blob - demuxer.h
Sleep timer
[vompclient.git] / demuxer.h
1 /*
2     Copyright 2005-2007 Mark Calderbank
3     Copyright 2007 Marten Richter (AC3 support)
4
5     This file is part of VOMP.
6
7     VOMP is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     VOMP is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with VOMP; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 */
21
22 /*
23
24 Thanks go to Jon Gettler of the MVPMC project and Stephen Rice for
25 the demuxer in mvpmc. It was used in the creation of this demuxer;
26 however, no code was copied verbatim.
27
28 */
29
30 #ifndef DEMUXER_H
31 #define DEMUXER_H
32
33 #include "stream.h"
34 #include "defines.h"
35
36 class Callback;
37 class DrainTarget;
38
39 class PESPacket
40 {
41   public:
42     PESPacket();
43     void init(UCHAR type, UCHAR sub=0);
44     void truncate();
45     int  write(UCHAR* buf, int len);
46
47     UCHAR operator[] (UINT index); // return data[index] if in bounds, else 0
48                        // so no proper error condition but never mind for now
49     const UCHAR* getData() { return data; }
50     UINT getLength() { return length; }
51     UINT getSize() { return size; }
52     UCHAR getPacketType() { return packetType; }
53     void setSubstream(UCHAR s) { substream = s; }
54     UCHAR getSubstream() { return substream; }
55     ULLONG getPTS();
56     bool hasPTS() { return (getPTS() != PTS_INVALID); }
57
58     UINT findPictureHeader();
59     UINT findSeqHeader();
60     static const ULLONG PTS_INVALID;
61   protected:
62     UCHAR data[0x10000];
63     UINT length, size;
64     UCHAR packetType;
65     UCHAR substream;
66     ULLONG pts;
67     UINT seq_header; // 0 = no, 1 = unknown, else = header offset
68 };
69
70 class Demuxer
71 {
72   public:
73     Demuxer();
74     virtual ~Demuxer();
75     static Demuxer* getInstance();
76     int init(Callback* callback, DrainTarget* audio, DrainTarget* video, ULONG demuxMemoryV, ULONG demuxMemoryA);
77     virtual void reset();
78     virtual void flush();
79     void flushAudio();
80     void seek();
81     void setVideoStream(int id);
82     void setAudioStream(int id);
83     bool writeAudio();
84     bool writeVideo();
85
86     virtual int scan(UCHAR* buf, int len) = 0;
87     virtual int findPTS(UCHAR* buf, int len, ULLONG* dest) = 0;
88     virtual int put(UCHAR* buf, int len) = 0;
89     virtual void setFrameNum(ULONG frame) {}
90     virtual void setPacketNum(ULONG packet) {}
91     virtual ULONG getFrameNumFromPTS(ULLONG pts) {return 0;}
92     virtual ULONG getPacketNum() {return 0;}
93
94     bool* getmpAudioChannels(); //Maybe virtual ?
95     bool* getac3AudioChannels(); //Maybe virtual ?
96     int getselAudioChannel();
97     void setAudioChannel(int aud_channel);
98
99     int getHorizontalSize() { return horizontal_size; }
100     int getVerticalSize() { return vertical_size; }
101     int getAspectRatio() { return aspect_ratio; }
102     int getFrameRate() { return frame_rate; }
103     int getBitRate() { return bit_rate; }
104     ULLONG getVideoPTS() { return video_pts; }
105     ULLONG getAudioPTS() { return audio_pts; }
106
107     enum AspectRatio
108     {
109       ASPECT_4_3  = 2,
110       ASPECT_16_9 = 3
111     };
112
113     // Remove all data from a buffer apart from video PES packets.
114     // Returns the length of the reduced data.
115     // *static function*
116     static UINT stripAudio(UCHAR* buf, UINT len);
117
118     // Scan a buffer to see if video packets are present.
119     // Returns true if video exists; false if not.
120     // *static function*
121     static bool scanForVideo(UCHAR* buf, UINT len);
122
123     enum PESTYPE
124     {
125       PESTYPE_PRIVATE_1 = 0xBD,
126
127       PESTYPE_AUD0 = 0xC0,
128       PESTYPE_AUD1,  PESTYPE_AUD2,  PESTYPE_AUD3,  PESTYPE_AUD4,
129       PESTYPE_AUD5,  PESTYPE_AUD6,  PESTYPE_AUD7,  PESTYPE_AUD8,
130       PESTYPE_AUD9,  PESTYPE_AUD10, PESTYPE_AUD11, PESTYPE_AUD12,
131       PESTYPE_AUD13, PESTYPE_AUD14, PESTYPE_AUD15, PESTYPE_AUD16,
132       PESTYPE_AUD17, PESTYPE_AUD18, PESTYPE_AUD19, PESTYPE_AUD20,
133       PESTYPE_AUD21, PESTYPE_AUD22, PESTYPE_AUD23, PESTYPE_AUD24,
134       PESTYPE_AUD25, PESTYPE_AUD26, PESTYPE_AUD27, PESTYPE_AUD28,
135       PESTYPE_AUD29, PESTYPE_AUD30, PESTYPE_AUD31,
136       PESTYPE_AUDMAX = PESTYPE_AUD31,
137
138       PESTYPE_VID0 = 0xE0,
139       PESTYPE_VID1,  PESTYPE_VID2,  PESTYPE_VID3,  PESTYPE_VID4,
140       PESTYPE_VID5,  PESTYPE_VID6,  PESTYPE_VID7,  PESTYPE_VID8,
141       PESTYPE_VID9,  PESTYPE_VID10, PESTYPE_VID11, PESTYPE_VID12,
142       PESTYPE_VID13, PESTYPE_VID14, PESTYPE_VID15,
143       PESTYPE_VIDMAX = PESTYPE_VID15
144     };
145     enum PESTYPE_SUBSTREAM
146     {
147       PESTYPE_SUBSTREAM_AC30 = 0x80,
148       PESTYPE_SUBSTREAM_AC31,PESTYPE_SUBSTREAM_AC32, PESTYPE_SUBSTREAM_AC33,
149       PESTYPE_SUBSTREAM_AC34,PESTYPE_SUBSTREAM_AC35,PESTYPE_SUBSTREAM_AC36,
150       PESTYPE_SUBSTREAM_AC37,
151       PESTYPE_SUBSTREAM_AC3MAX = PESTYPE_SUBSTREAM_AC37
152     };
153
154   protected:
155     // Operations on PES packets
156     bool submitPacket(PESPacket&);
157     void parsePacketDetails(PESPacket&);
158
159     // General demuxer objects and status indicators
160     static Demuxer* instance;
161     Stream videostream;
162     Stream audiostream;
163     int shutdown();
164     bool initted;
165     bool vid_seeking;
166     bool aud_seeking;
167     int video_current, audio_current;
168
169     // Video stream information
170     void setAspectRatio(enum AspectRatio);
171     Callback* callback;
172
173     int horizontal_size;
174     int vertical_size;
175     enum AspectRatio aspect_ratio;
176     int arcnt;
177     int frame_rate;
178     int bit_rate;
179     ULLONG video_pts;
180     ULLONG video_pts_seek;
181     ULLONG audio_pts;
182
183     // Constants
184     static const int FrameRates[9];
185
186     bool ispre_1_3_19;
187     bool avail_mpaudchan[PESTYPE_AUDMAX-PESTYPE_AUD0+1];
188     bool avail_ac3audchan[PESTYPE_SUBSTREAM_AC3MAX-PESTYPE_SUBSTREAM_AC30+1];
189 };
190
191 #endif