]> git.vomp.tv Git - vompclient.git/blob - demuxer.h
Vogel Media Player 2008-11-28
[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, write to the Free Software Foundation, Inc.,
19     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 DVBSubtitles;
37 class Callback;
38 class DrainTarget;
39
40 class PESPacket
41 {
42   public:
43     PESPacket();
44     PESPacket(const PESPacket& packet);
45     PESPacket& operator=(const PESPacket& packet);
46     ~PESPacket();
47     void init(UCHAR type, UCHAR sub=0);
48     void truncate();
49     int  write(const UCHAR* buf, int len);
50
51     UCHAR operator[] (UINT index) const;
52                        // return data[index] if in bounds, else 0
53                        // so no proper error condition but never mind for now
54     const UCHAR* getData() const { return data; }
55     UINT getLength() const { return length; }
56     UINT getSize() const { return size; }
57     UCHAR getPacketType() const { return packetType; }
58     void setSubstream(UCHAR s) { substream = s; }
59     UCHAR getSubstream() const { return substream; }
60     ULLONG getPTS() const;
61     bool hasPTS() const { return (getPTS() != PTS_INVALID); }
62
63     UINT findPictureHeader() const;
64     UINT findSeqHeader() const;
65     static const ULLONG PTS_INVALID = (1LL << 33);
66   protected:
67     void copyFrom(const PESPacket& packet);
68     UCHAR * data;
69     UINT length, size;
70     UINT data_size;
71     UCHAR packetType;
72     UCHAR substream;
73     UINT mutable seq_header; // 0 = no, 1 = unknown, else = header offset
74 };
75
76 class Demuxer
77 {
78   public:
79     Demuxer();
80     virtual ~Demuxer();
81     static Demuxer* getInstance();
82     int init(Callback* callback, DrainTarget* audio, DrainTarget* video, ULONG demuxMemoryV, ULONG demuxMemoryA, DVBSubtitles* tsubtitles = NULL);
83     virtual void reset();
84     virtual void flush();
85     void flushAudio();
86     void seek();
87     void setVideoStream(int id);
88     void setAudioStream(int id);
89     bool writeAudio();
90     bool writeVideo();
91
92     virtual int scan(UCHAR* buf, int len) = 0;
93     virtual int findPTS(UCHAR* buf, int len, ULLONG* dest) = 0;
94     virtual int put(UCHAR* buf, int len) = 0;
95     virtual void setFrameNum(ULONG frame) {}
96     virtual void setPacketNum(ULONG packet) {}
97     virtual ULONG getFrameNumFromPTS(ULLONG pts) {return 0;}
98     virtual ULONG getPacketNum() {return 0;}
99
100     bool* getmpAudioChannels(); //Maybe virtual ?
101     bool* getac3AudioChannels(); //Maybe virtual ?
102     int getselAudioChannel();
103     void setAudioChannel(int aud_channel);
104
105     int getHorizontalSize() { return horizontal_size; }
106     int getVerticalSize() { return vertical_size; }
107     int getAspectRatio() { return aspect_ratio; }
108     int getFrameRate() { return frame_rate; }
109     int getBitRate() { return bit_rate; }
110     ULLONG getVideoPTS() { return video_pts; }
111     ULLONG getAudioPTS() { return audio_pts; }
112
113     enum AspectRatio
114     {
115       ASPECT_4_3  = 2,
116       ASPECT_16_9 = 3
117     };
118
119     // Remove all data from a buffer apart from video PES packets.
120     // Returns the length of the reduced data.
121     // *static function*
122     static UINT stripAudio(UCHAR* buf, UINT len);
123
124     // Scan a buffer to see if video packets are present.
125     // Returns true if video exists; false if not.
126     // *static function*
127     static bool scanForVideo(UCHAR* buf, UINT len);
128
129     enum PESTYPE
130     {
131       PESTYPE_PRIVATE_1 = 0xBD,
132
133       PESTYPE_AUD0 = 0xC0,
134       PESTYPE_AUD1,  PESTYPE_AUD2,  PESTYPE_AUD3,  PESTYPE_AUD4,
135       PESTYPE_AUD5,  PESTYPE_AUD6,  PESTYPE_AUD7,  PESTYPE_AUD8,
136       PESTYPE_AUD9,  PESTYPE_AUD10, PESTYPE_AUD11, PESTYPE_AUD12,
137       PESTYPE_AUD13, PESTYPE_AUD14, PESTYPE_AUD15, PESTYPE_AUD16,
138       PESTYPE_AUD17, PESTYPE_AUD18, PESTYPE_AUD19, PESTYPE_AUD20,
139       PESTYPE_AUD21, PESTYPE_AUD22, PESTYPE_AUD23, PESTYPE_AUD24,
140       PESTYPE_AUD25, PESTYPE_AUD26, PESTYPE_AUD27, PESTYPE_AUD28,
141       PESTYPE_AUD29, PESTYPE_AUD30, PESTYPE_AUD31,
142       PESTYPE_AUDMAX = PESTYPE_AUD31,
143
144       PESTYPE_VID0 = 0xE0,
145       PESTYPE_VID1,  PESTYPE_VID2,  PESTYPE_VID3,  PESTYPE_VID4,
146       PESTYPE_VID5,  PESTYPE_VID6,  PESTYPE_VID7,  PESTYPE_VID8,
147       PESTYPE_VID9,  PESTYPE_VID10, PESTYPE_VID11, PESTYPE_VID12,
148       PESTYPE_VID13, PESTYPE_VID14, PESTYPE_VID15,
149       PESTYPE_VIDMAX = PESTYPE_VID15
150     };
151     enum PESTYPE_SUBSTREAM
152     {
153       PESTYPE_SUBSTREAM_AC30 = 0x80,
154       PESTYPE_SUBSTREAM_AC31,PESTYPE_SUBSTREAM_AC32, PESTYPE_SUBSTREAM_AC33,
155       PESTYPE_SUBSTREAM_AC34,PESTYPE_SUBSTREAM_AC35,PESTYPE_SUBSTREAM_AC36,
156       PESTYPE_SUBSTREAM_AC37,
157       PESTYPE_SUBSTREAM_AC3MAX = PESTYPE_SUBSTREAM_AC37
158     };
159
160   protected:
161     // Operations on PES packets
162     bool submitPacket(PESPacket&);
163     void parsePacketDetails(PESPacket&);
164
165     // General demuxer objects and status indicators
166     static Demuxer* instance;
167     Stream videostream;
168     Stream audiostream;
169     DVBSubtitles* subtitles;
170     int shutdown();
171     bool initted;
172     bool vid_seeking;
173     bool aud_seeking;
174     int video_current, audio_current;
175
176     // Video stream information
177     void setAspectRatio(enum AspectRatio);
178     Callback* callback;
179
180     int horizontal_size;
181     int vertical_size;
182     enum AspectRatio aspect_ratio;
183     int arcnt;
184     int frame_rate;
185     int bit_rate;
186     ULLONG video_pts;
187     ULLONG video_pts_seek;
188     ULLONG audio_pts;
189
190     // Constants
191     static const int FrameRates[9];
192
193     bool ispre_1_3_19;
194     bool avail_mpaudchan[PESTYPE_AUDMAX-PESTYPE_AUD0+1];
195     bool avail_ac3audchan[PESTYPE_SUBSTREAM_AC3MAX-PESTYPE_SUBSTREAM_AC30+1];
196 };
197
198 #endif