]> git.vomp.tv Git - vompclient.git/blob - demuxer.h
Rename TCP class to TCPOld
[vompclient.git] / demuxer.h
1 /*
2     Copyright 2005-2008 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, see <https://www.gnu.org/licenses/>.
19 */
20
21 /*
22
23 Thanks go to Jon Gettler of the MVPMC project and Stephen Rice for
24 the demuxer in mvpmc. It was used in the creation of this demuxer;
25 however, no code was copied verbatim.
26
27 */
28
29 #ifndef DEMUXER_H
30 #define DEMUXER_H
31
32 #include "stream.h"
33 #include "defines.h"
34
35 class DVBSubtitles;
36 class Callback;
37 class DrainTarget;
38
39 struct PictCountInfo
40 {
41         bool hassps;
42         bool hasaccessunit;
43         int log2_max_frame_num;
44         int frame_mbs_only_flag;
45         int separate_color_plane_flag;
46 };
47
48 class PESPacket
49 {
50   public:
51     PESPacket();
52     PESPacket(const PESPacket& packet);
53     PESPacket& operator=(const PESPacket& packet);
54     ~PESPacket();
55     void init(UCHAR type, UCHAR sub = 0);
56     void truncate();
57     int  write(const UCHAR* buf, int len);
58
59     UCHAR operator[] (UINT index) const;
60                        // return data[index] if in bounds, else 0
61                        // so no proper error condition but never mind for now
62     const UCHAR* getData() const { return data; }
63     UINT getLength() const { return length; }
64     UINT getSize() const { return size; }
65     UCHAR getPacketType() const { return packetType; }
66     void setSubstream(UCHAR s) { substream = s; }
67     UCHAR getSubstream() const { return substream; }
68     ULLONG getPTS() const;
69     bool hasPTS() const { return (getPTS() != PTS_INVALID); }
70
71     UINT findPictureHeader(bool h264) const;
72     UINT findSeqHeader(bool h264) const;
73     UINT findSeqExtHeader(bool h264) const;
74     UINT countPictureHeaders(bool h264, struct PictCountInfo& pinfo) const;
75     static const ULLONG PTS_INVALID = (1LL << 33);
76
77
78
79   protected:
80     void copyFrom(const PESPacket& packet);
81     UCHAR * data;
82     UINT length, size;
83     UINT data_size;
84     UCHAR packetType;
85     UCHAR substream;
86     UINT mutable seq_header; // 0 = no, 1 = unknown, else = header offset
87 };
88
89 class Demuxer
90 {
91   public:
92     Demuxer();
93     virtual ~Demuxer();
94     static Demuxer* getInstance();
95      int init(Callback* tcallback, DrainTarget* audio, DrainTarget* video,
96          DrainTarget* teletext,
97          ULONG demuxMemoryV, ULONG demuxMemoryA, ULONG demuxMemoryT, double fps=25.,
98          DVBSubtitles* tsubtitles=NULL);
99     virtual void reset();
100     virtual void flush();
101     void flushAudio();
102     void seek();
103     void setVideoStream(int id);
104     //TODO HANS next virtual necessary?
105     //virtual void setAudioStream(int id);
106     void setAudioStream(int id);
107     void setTeletextStream(int id);
108     void setDVBSubtitleStream(int id);
109     bool writeAudio(bool * dataavail=NULL);
110     bool writeVideo(bool * dataavail=NULL);
111     bool writeTeletext(bool * dataavail=NULL);
112
113     virtual int scan(UCHAR* buf, int len) = 0;
114     virtual int findPTS(UCHAR* buf, int len, ULLONG* dest) = 0;
115     virtual int put(UCHAR* buf, int len) = 0;
116     virtual void setFrameNum(ULONG /* frame */) {}
117     virtual void setPacketNum(ULONG /* packet */) {}
118     virtual ULONG getFrameNumFromPTS(ULLONG /* pts */) { return 0; }
119     virtual ULONG getPacketNum() {return 0;}
120
121     bool* getmpAudioChannels(); //Maybe virtual ?
122     bool* getac3AudioChannels(); //Maybe virtual ?
123     bool* getSubtitleChannels();
124     int getselAudioChannel();
125     int getselSubtitleChannel();
126         bool ish264() {return h264;}
127     void seth264(bool newh264){h264=newh264;}
128
129     int getHorizontalSize() { return horizontal_size; }
130     int getVerticalSize() { return vertical_size; }
131     UCHAR getAspectRatio(int *tparx,int *tpary) {
132         *tparx=parx; *tpary=pary;return aspect_ratio; }
133     int getFrameRate() { return frame_rate; }
134     int getBitRate() { return bit_rate; }
135     bool getInterlaced() { return interlaced;}
136     ULLONG getVideoPTS() { return video_pts; }
137     ULLONG getAudioPTS() { return audio_pts; }
138
139     enum AspectRatio : UCHAR
140     {
141       ASPECT_1_1  = 1,
142       ASPECT_4_3  = 2,
143       ASPECT_16_9 = 3,
144       ASPECT_221  = 4,
145     };
146
147     // Remove all data from a buffer apart from video PES packets.
148     // Returns the length of the reduced data.
149     // removed *static function*, due to DemuxerTS
150     virtual UINT stripAudio(UCHAR* buf, UINT len);
151     void changeTimes(UCHAR* buf, UINT len,UINT playtime);
152
153     // Scan a buffer to see if video packets are present.
154     // Returns true if video exists; false if not.
155     // removed *static function* for h264 detection
156     static bool scanForVideo(UCHAR* buf, UINT len, bool &ish264);
157
158     enum PESTYPE
159     {
160       PESTYPE_PRIVATE_1 = 0xBD,
161
162       PESTYPE_AUD0 = 0xC0,
163       PESTYPE_AUD1,  PESTYPE_AUD2,  PESTYPE_AUD3,  PESTYPE_AUD4,
164       PESTYPE_AUD5,  PESTYPE_AUD6,  PESTYPE_AUD7,  PESTYPE_AUD8,
165       PESTYPE_AUD9,  PESTYPE_AUD10, PESTYPE_AUD11, PESTYPE_AUD12,
166       PESTYPE_AUD13, PESTYPE_AUD14, PESTYPE_AUD15, PESTYPE_AUD16,
167       PESTYPE_AUD17, PESTYPE_AUD18, PESTYPE_AUD19, PESTYPE_AUD20,
168       PESTYPE_AUD21, PESTYPE_AUD22, PESTYPE_AUD23, PESTYPE_AUD24,
169       PESTYPE_AUD25, PESTYPE_AUD26, PESTYPE_AUD27, PESTYPE_AUD28,
170       PESTYPE_AUD29, PESTYPE_AUD30, PESTYPE_AUD31,
171       PESTYPE_AUDMAX = PESTYPE_AUD31,
172
173       PESTYPE_VID0 = 0xE0,
174       PESTYPE_VID1,  PESTYPE_VID2,  PESTYPE_VID3,  PESTYPE_VID4,
175       PESTYPE_VID5,  PESTYPE_VID6,  PESTYPE_VID7,  PESTYPE_VID8,
176       PESTYPE_VID9,  PESTYPE_VID10, PESTYPE_VID11, PESTYPE_VID12,
177       PESTYPE_VID13, PESTYPE_VID14, PESTYPE_VID15,
178       PESTYPE_VIDMAX = PESTYPE_VID15
179     };
180     enum PESTYPE_SUBSTREAM
181     {
182       PESTYPE_SUBSTREAM_TELETEXT0 = 0x10,
183       PESTYPE_SUBSTREAM_TELETEXT1,PESTYPE_SUBSTREAM_TELETEXT2, PESTYPE_SUBSTREAM_TELETEXT3,
184       PESTYPE_SUBSTREAM_TELETEXT4,PESTYPE_SUBSTREAM_TELETEXT5,PESTYPE_SUBSTREAM_TELETEXT6,
185       PESTYPE_SUBSTREAM_TELETEXT7, PESTYPE_SUBSTREAM_TELETEXT8, PESTYPE_SUBSTREAM_TELETEXT9,
186       PESTYPE_SUBSTREAM_TELETEXT10,PESTYPE_SUBSTREAM_TELETEXT11,PESTYPE_SUBSTREAM_TELETEXT12,
187       PESTYPE_SUBSTREAM_TELETEXT13,PESTYPE_SUBSTREAM_TELETEXT14,PESTYPE_SUBSTREAM_TELETEXT15,
188       PESTYPE_SUBSTREAM_TELETEXTMAX=PESTYPE_SUBSTREAM_TELETEXT15,
189       PESTYPE_SUBSTREAM_DVBSUBTITLE0=0x20,
190       PESTYPE_SUBSTREAM_DVBSUBTITLE1,PESTYPE_SUBSTREAM_DVBSUBTITLE2,PESTYPE_SUBSTREAM_DVBSUBTITLE3,
191       PESTYPE_SUBSTREAM_DVBSUBTITLE4,PESTYPE_SUBSTREAM_DVBSUBTITLE5,PESTYPE_SUBSTREAM_DVBSUBTITLE6,
192       PESTYPE_SUBSTREAM_DVBSUBTITLE7,
193       PESTYPE_SUBSTREAM_DVBSUBTITLEMAX=PESTYPE_SUBSTREAM_DVBSUBTITLE7,
194       PESTYPE_SUBSTREAM_AC30 = 0x80,
195       PESTYPE_SUBSTREAM_AC31,PESTYPE_SUBSTREAM_AC32, PESTYPE_SUBSTREAM_AC33,
196       PESTYPE_SUBSTREAM_AC34,PESTYPE_SUBSTREAM_AC35,PESTYPE_SUBSTREAM_AC36,
197       PESTYPE_SUBSTREAM_AC37,
198       PESTYPE_SUBSTREAM_AC3MAX = PESTYPE_SUBSTREAM_AC37
199     };
200
201   protected:
202     // Operations on PES packets
203     bool submitPacket(PESPacket&);
204     void parsePacketDetails(PESPacket&);
205
206     // General demuxer objects and status indicators
207     static Demuxer* instance;
208     Stream videostream;
209     Stream audiostream;
210     DVBSubtitles* subtitles;
211     Stream teletextstream;
212     int shutdown();
213     bool initted;
214     bool vid_seeking;
215     bool aud_seeking;
216         bool h264;
217     int video_current, audio_current, teletext_current, subtitle_current;
218     int astreamtype;
219     bool livetv;
220
221     // Video stream information
222     void setAspectRatio(enum AspectRatio, int taspectx, int taspecty);
223     int parx;
224     int pary;
225     Callback* callback;
226
227     int horizontal_size;
228     int vertical_size;
229     bool interlaced;
230     int profile;
231     enum AspectRatio aspect_ratio;
232     int arcnt;
233     int frame_rate;
234     int bit_rate;
235     ULLONG video_pts;
236     ULLONG video_pts_seek;
237     ULLONG audio_pts;
238     ULLONG teletext_pts;
239     bool isteletextdecoded;
240
241
242         unsigned int packetnum;
243
244     // Constants
245     static const int FrameRates[9];
246
247         double fps;
248
249     bool ispre_1_3_19;
250     bool avail_mpaudchan[PESTYPE_AUDMAX-PESTYPE_AUD0+1];
251     bool avail_ac3audchan[PESTYPE_SUBSTREAM_AC3MAX-PESTYPE_SUBSTREAM_AC30+1];
252     bool avail_dvbsubtitlechan[PESTYPE_SUBSTREAM_DVBSUBTITLEMAX-PESTYPE_SUBSTREAM_DVBSUBTITLE0+1];
253 };
254
255 #endif