]> git.vomp.tv Git - vompclient-marten.git/blob - demuxer.h
Let vomp handle aspect ratio change, better aspect ratio control for HD and SD
[vompclient-marten.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(bool h264) const;
64     UINT findSeqHeader(bool h264) const;
65     UINT findSeqExtHeader(bool h264) const;
66     UINT countPictureHeaders(bool h264) const;
67     static const ULLONG PTS_INVALID = (1LL << 33);
68
69
70
71   protected:
72     void copyFrom(const PESPacket& packet);
73     UCHAR * data;
74     UINT length, size;
75     UINT data_size;
76     UCHAR packetType;
77     UCHAR substream;
78     UINT mutable seq_header; // 0 = no, 1 = unknown, else = header offset
79 };
80
81 class Demuxer
82 {
83   public:
84     Demuxer();
85     virtual ~Demuxer();
86     static Demuxer* getInstance();
87      int init(Callback* tcallback, DrainTarget* audio, DrainTarget* video,
88          DrainTarget* teletext,
89          ULONG demuxMemoryV, ULONG demuxMemoryA, ULONG demuxMemoryT, double fps=25.,
90          DVBSubtitles* tsubtitles=NULL);
91     virtual void reset();
92     virtual void flush();
93     void flushAudio();
94     void seek();
95     void setVideoStream(int id);
96     //TODO HANS next virtual necessary?
97     //virtual void setAudioStream(int id);
98     void setAudioStream(int id);
99     void setTeletextStream(int id);
100     void setDVBSubtitleStream(int id);
101     bool writeAudio(bool * dataavail=NULL);
102     bool writeVideo(bool * dataavail=NULL);
103     bool writeTeletext(bool * dataavail=NULL);
104
105     virtual int scan(UCHAR* buf, int len) = 0;
106     virtual int findPTS(UCHAR* buf, int len, ULLONG* dest) = 0;
107     virtual int put(UCHAR* buf, int len) = 0;
108     virtual void setFrameNum(ULONG frame) {}
109     virtual void setPacketNum(ULONG packet) {}
110     virtual ULONG getFrameNumFromPTS(ULLONG pts) {return 0;}
111     virtual ULONG getPacketNum() {return 0;}
112
113     bool* getmpAudioChannels(); //Maybe virtual ?
114     bool* getac3AudioChannels(); //Maybe virtual ?
115     bool* getSubtitleChannels();
116     int getselAudioChannel();
117     int getselSubtitleChannel();
118         bool ish264() {return h264;}
119     void seth264(bool newh264){h264=newh264;}
120
121     int getHorizontalSize() { return horizontal_size; }
122     int getVerticalSize() { return vertical_size; }
123     int getAspectRatio(int *tparx,int *tpary) {
124         *tparx=parx; *tpary=pary;return aspect_ratio; }
125     int getFrameRate() { return frame_rate; }
126     int getBitRate() { return bit_rate; }
127     bool getInterlaced() { return interlaced;}
128     ULLONG getVideoPTS() { return video_pts; }
129     ULLONG getAudioPTS() { return audio_pts; }
130
131     enum AspectRatio
132     {
133       ASPECT_1_1  = 1,
134       ASPECT_4_3  = 2,
135       ASPECT_16_9 = 3,
136       ASPECT_221  = 4,
137     };
138
139     // Remove all data from a buffer apart from video PES packets.
140     // Returns the length of the reduced data.
141     // removed *static function*, due to DemuxerTS
142     virtual UINT stripAudio(UCHAR* buf, UINT len);
143     void changeTimes(UCHAR* buf, UINT len,UINT playtime);
144
145     // Scan a buffer to see if video packets are present.
146     // Returns true if video exists; false if not.
147     // removed *static function* for h264 detection
148     static bool scanForVideo(UCHAR* buf, UINT len, bool &ish264);
149
150     enum PESTYPE
151     {
152       PESTYPE_PRIVATE_1 = 0xBD,
153
154       PESTYPE_AUD0 = 0xC0,
155       PESTYPE_AUD1,  PESTYPE_AUD2,  PESTYPE_AUD3,  PESTYPE_AUD4,
156       PESTYPE_AUD5,  PESTYPE_AUD6,  PESTYPE_AUD7,  PESTYPE_AUD8,
157       PESTYPE_AUD9,  PESTYPE_AUD10, PESTYPE_AUD11, PESTYPE_AUD12,
158       PESTYPE_AUD13, PESTYPE_AUD14, PESTYPE_AUD15, PESTYPE_AUD16,
159       PESTYPE_AUD17, PESTYPE_AUD18, PESTYPE_AUD19, PESTYPE_AUD20,
160       PESTYPE_AUD21, PESTYPE_AUD22, PESTYPE_AUD23, PESTYPE_AUD24,
161       PESTYPE_AUD25, PESTYPE_AUD26, PESTYPE_AUD27, PESTYPE_AUD28,
162       PESTYPE_AUD29, PESTYPE_AUD30, PESTYPE_AUD31,
163       PESTYPE_AUDMAX = PESTYPE_AUD31,
164
165       PESTYPE_VID0 = 0xE0,
166       PESTYPE_VID1,  PESTYPE_VID2,  PESTYPE_VID3,  PESTYPE_VID4,
167       PESTYPE_VID5,  PESTYPE_VID6,  PESTYPE_VID7,  PESTYPE_VID8,
168       PESTYPE_VID9,  PESTYPE_VID10, PESTYPE_VID11, PESTYPE_VID12,
169       PESTYPE_VID13, PESTYPE_VID14, PESTYPE_VID15,
170       PESTYPE_VIDMAX = PESTYPE_VID15
171     };
172     enum PESTYPE_SUBSTREAM
173     {
174       PESTYPE_SUBSTREAM_TELETEXT0 = 0x10,
175       PESTYPE_SUBSTREAM_TELETEXT1,PESTYPE_SUBSTREAM_TELETEXT2, PESTYPE_SUBSTREAM_TELETEXT3,
176       PESTYPE_SUBSTREAM_TELETEXT4,PESTYPE_SUBSTREAM_TELETEXT5,PESTYPE_SUBSTREAM_TELETEXT6,
177       PESTYPE_SUBSTREAM_TELETEXT7, PESTYPE_SUBSTREAM_TELETEXT8, PESTYPE_SUBSTREAM_TELETEXT9,
178       PESTYPE_SUBSTREAM_TELETEXT10,PESTYPE_SUBSTREAM_TELETEXT11,PESTYPE_SUBSTREAM_TELETEXT12,
179       PESTYPE_SUBSTREAM_TELETEXT13,PESTYPE_SUBSTREAM_TELETEXT14,PESTYPE_SUBSTREAM_TELETEXT15,
180       PESTYPE_SUBSTREAM_TELETEXTMAX=PESTYPE_SUBSTREAM_TELETEXT15,
181       PESTYPE_SUBSTREAM_DVBSUBTITLE0=0x20,
182       PESTYPE_SUBSTREAM_DVBSUBTITLE1,PESTYPE_SUBSTREAM_DVBSUBTITLE2,PESTYPE_SUBSTREAM_DVBSUBTITLE3,
183       PESTYPE_SUBSTREAM_DVBSUBTITLE4,PESTYPE_SUBSTREAM_DVBSUBTITLE5,PESTYPE_SUBSTREAM_DVBSUBTITLE6,
184       PESTYPE_SUBSTREAM_DVBSUBTITLE7,
185       PESTYPE_SUBSTREAM_DVBSUBTITLEMAX=PESTYPE_SUBSTREAM_DVBSUBTITLE7,
186       PESTYPE_SUBSTREAM_AC30 = 0x80,
187       PESTYPE_SUBSTREAM_AC31,PESTYPE_SUBSTREAM_AC32, PESTYPE_SUBSTREAM_AC33,
188       PESTYPE_SUBSTREAM_AC34,PESTYPE_SUBSTREAM_AC35,PESTYPE_SUBSTREAM_AC36,
189       PESTYPE_SUBSTREAM_AC37,
190       PESTYPE_SUBSTREAM_AC3MAX = PESTYPE_SUBSTREAM_AC37
191     };
192
193   protected:
194     // Operations on PES packets
195     bool submitPacket(PESPacket&);
196     void parsePacketDetails(PESPacket&);
197
198     // General demuxer objects and status indicators
199     static Demuxer* instance;
200     Stream videostream;
201     Stream audiostream;
202     DVBSubtitles* subtitles;
203     Stream teletextstream;
204     int shutdown();
205     bool initted;
206     bool vid_seeking;
207     bool aud_seeking;
208         bool h264;
209     int video_current, audio_current, teletext_current, subtitle_current;
210     int astreamtype;
211
212     // Video stream information
213     void setAspectRatio(enum AspectRatio, int taspectx, int taspecty);
214     int parx;
215     int pary;
216     Callback* callback;
217
218     int horizontal_size;
219     int vertical_size;
220     bool interlaced;
221     int profile;
222     enum AspectRatio aspect_ratio;
223     int arcnt;
224     int frame_rate;
225     int bit_rate;
226     ULLONG video_pts;
227     ULLONG video_pts_seek;
228     ULLONG audio_pts;
229     ULLONG teletext_pts;
230     bool isteletextdecoded;
231
232
233         unsigned int packetnum;
234
235     // Constants
236     static const int FrameRates[9];
237
238         double fps;
239
240     bool ispre_1_3_19;
241     bool avail_mpaudchan[PESTYPE_AUDMAX-PESTYPE_AUD0+1];
242     bool avail_ac3audchan[PESTYPE_SUBSTREAM_AC3MAX-PESTYPE_SUBSTREAM_AC30+1];
243     bool avail_dvbsubtitlechan[PESTYPE_SUBSTREAM_DVBSUBTITLEMAX-PESTYPE_SUBSTREAM_DVBSUBTITLE0+1];
244 };
245
246 #endif