]> git.vomp.tv Git - vompclient.git/blob - demuxer.h
Fix for possible crash when removing bar
[vompclient.git] / demuxer.h
1 /*
2     Copyright 2005-2006 Mark Calderbank
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 /*
22
23 Thanks goes 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 <stdio.h>
33 #include <memory.h>
34 #include "stream.h"
35 #include "log.h"
36 #include "defines.h"
37 #include "callback.h"
38 #include "draintarget.h"
39
40 #define PES_AUDIO_START 0xc0
41 #define PES_AUDIO_END 0xcf
42
43 class Demuxer
44 {
45 protected:
46   class PESPacket
47   {
48     public:
49       PESPacket();
50       void init(UCHAR type);
51       void truncate();
52       int  write(UCHAR* buf, int len);
53       int  submit();
54     protected:
55       UCHAR data[0x10000];
56       UINT length, size;
57       bool closed;
58       UCHAR packetType;
59       UCHAR substream; //for ac3
60       ULLONG pts;
61       bool seq_header;
62       UINT submitted;
63       virtual void parseDetails();
64       UINT findPictureHeader();
65
66
67       static const ULLONG PTS_INVALID;
68   };
69   friend class PESPacket;
70
71   public:
72     Demuxer();
73     virtual ~Demuxer();
74     static Demuxer* getInstance();
75     int init(Callback* callback, DrainTarget* audio, DrainTarget* video, ULONG demuxMemoryV, ULONG demuxMemoryA);
76     virtual void reset();
77     virtual void flush();
78     void flushAudio();
79     void seek();
80     void setVideoStream(int id);
81     void setAudioStream(int id);
82     bool writeAudio();
83     bool writeVideo();
84
85     virtual int scan(UCHAR* buf, int len) = 0;
86     virtual int findPTS(UCHAR* buf, int len, ULLONG* dest) = 0;
87     virtual int put(UCHAR* buf, int len) = 0;
88     virtual void setFrameNum(ULONG frame) {}
89     virtual void setPacketNum(ULONG packet) {}
90     virtual ULONG getFrameNumFromPTS(ULLONG pts) {return 0;}
91     virtual ULONG getPacketNum() {return 0;}
92
93     bool* getmpAudioChannels(); //Maybe virtual ?
94     bool* getac3AudioChannels(); //Maybe virtual ?
95     int getselAudioChannel();
96     void setAudioChannel(int aud_channel);
97
98     int getHorizontalSize() { return horizontal_size; }
99     int getVerticalSize() { return vertical_size; }
100     int getAspectRatio() { return aspect_ratio; }
101     int getFrameRate() { return frame_rate; }
102     int getBitRate() { return bit_rate; }
103     ULLONG getVideoPTS() { return video_pts; }
104     ULLONG getAudioPTS() { return audio_pts; }
105
106     enum AspectRatio
107     {
108       ASPECT_4_3  = 2,
109       ASPECT_16_9 = 3
110     };
111
112     // Remove all data from a buffer apart from video PES packets.
113     // Returns the length of the reduced data.
114     // *static function*
115     static UINT stripAudio(UCHAR* buf, UINT len);
116
117     // Scan a buffer to see if video packets are present.
118     // Returns true if video exists; false if not.
119     // *static function*
120     static bool scanForVideo(UCHAR* buf, UINT len);
121
122   protected:
123     // General demuxer objects and status indicators
124     static Demuxer* instance;
125     Stream videostream;
126     Stream audiostream;
127     int shutdown();
128     bool initted;
129     bool vid_seeking;
130     bool aud_seeking;
131     int video_current, audio_current;
132
133     // Video stream information
134     void setAspectRatio(enum AspectRatio);
135     Callback* callback;
136
137     int horizontal_size;
138     int vertical_size;
139     enum AspectRatio aspect_ratio;
140     int arcnt;
141     int frame_rate;
142     int bit_rate;
143     ULLONG video_pts;
144     ULLONG video_pts_seek;
145     ULLONG audio_pts;
146
147     // Constants
148     static const int FrameRates[9];
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_AC30 = 0x80,
175       PESTYPE_SUBSTREAM_AC31,PESTYPE_SUBSTREAM_AC32, PESTYPE_SUBSTREAM_AC33,
176       PESTYPE_SUBSTREAM_AC34,PESTYPE_SUBSTREAM_AC35,PESTYPE_SUBSTREAM_AC36,
177       PESTYPE_SUBSTREAM_AC37,
178       PESTYPE_SUBSTREAM_AC3MAX = PESTYPE_SUBSTREAM_AC37
179     };
180
181     bool ispre_1_3_19;
182     bool avail_mpaudchan[PESTYPE_AUDMAX-PESTYPE_AUD0+1];
183     bool avail_ac3audchan[PESTYPE_SUBSTREAM_AC3MAX-PESTYPE_SUBSTREAM_AC30+1];
184 };
185
186 #endif