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