]> git.vomp.tv Git - vompclient.git/blob - demuxer.cc
New player, ffwd/fbwd, iframe navigation stuff
[vompclient.git] / demuxer.cc
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 #include "demuxer.h"
22 #ifndef WIN32
23 #include <endian.h>
24 #else
25 #define __LITTLE_ENDIAN 1234
26 #define __BIG_ENDIAN  4321
27 #define __BYTE_ORDER __LITTLE_ENDIAN
28 #endif
29
30 #if __BYTE_ORDER == __BIG_ENDIAN
31 #define DEMUXER_SEQ_HEAD 0x000001B3
32 #else
33 #define DEMUXER_SEQ_HEAD 0xB3010000
34 #endif
35
36 #define DEMUXER_PIC_HEAD 0x00000101
37
38 #define SEEK_THRESHOLD 150000 // About 1.5 seconds
39
40 const int Demuxer::FrameRates[9] = { 0, 23, 24, 25, 29, 30, 50, 59, 60 };
41 const ULLONG Demuxer::PESPacket::PTS_INVALID = (1LL << 33);
42
43 Demuxer* Demuxer::instance = NULL;
44
45 Demuxer::Demuxer()
46 {
47   if (instance) return;
48   instance = this;
49   initted = false;
50   callback = NULL;
51   arcnt = 0;
52   vid_seeking = aud_seeking = false;
53   video_pts = audio_pts = 0;
54 }
55
56 Demuxer::~Demuxer()
57 {
58   shutdown();
59   instance = NULL;
60 }
61
62 Demuxer* Demuxer::getInstance()
63 {
64   return instance;
65 }
66
67 int Demuxer::init(Callback* tcallback, DrainTarget* audio, DrainTarget* video)
68 {
69   if (!initted)
70   {
71     if ( !videostream.init(video, demuxMemoryV) ||
72          !audiostream.init(audio, demuxMemoryA))
73     {
74       Log::getInstance()->log("Demuxer", Log::CRIT,
75                               "Failed to initialize demuxer");
76       shutdown();
77       return 0;
78     }
79   }
80
81   reset();
82   initted = true;
83   callback = tcallback;
84   return 1;
85 }
86
87 void Demuxer::reset()
88 {
89   flush();
90   video_current = audio_current = -1;
91   horizontal_size = vertical_size = 0;
92   aspect_ratio = (enum AspectRatio) 0;
93   frame_rate = bit_rate = 0;
94 }
95
96 int Demuxer::shutdown()
97 {
98   videostream.shutdown();
99   audiostream.shutdown();
100   initted = false;
101   return 1;
102 }
103
104 void Demuxer::flush()
105 {
106   videostream.flush();
107   audiostream.flush();
108 }
109
110 void Demuxer::flushAudio()
111 {
112   audiostream.flush();
113 }
114
115 void Demuxer::seek()
116 {
117   vid_seeking = aud_seeking = true;
118   video_pts = audio_pts = 0;
119 }
120
121 void Demuxer::setAudioStream(int id)
122 {
123   audio_current = id;
124 }
125
126 void Demuxer::setVideoStream(int id)
127 {
128   video_current = id;
129 }
130
131 void Demuxer::setAspectRatio(enum AspectRatio ar)
132 {
133   if (aspect_ratio != ar)
134   {
135     Log::getInstance()->log("Demux", Log::DEBUG,
136                             "Aspect ratio difference signalled");
137     if (++arcnt > 3) // avoid changing aspect ratio if glitch in signal
138     {
139       arcnt = 0;
140       aspect_ratio = ar;
141       callback->call(this);
142     }
143   }
144   else
145     arcnt = 0;
146 }
147
148 bool Demuxer::writeAudio()
149 {
150   return audiostream.drain();
151 }
152
153 bool Demuxer::writeVideo()
154 {
155   return videostream.drain();
156 }
157
158 Demuxer::PESPacket::PESPacket()
159 {
160   data[0] = 0x00;
161   data[1] = 0x00;
162   data[2] = 0x01;
163   init(0);
164 }
165
166 void Demuxer::PESPacket::init(UCHAR type)
167 {
168   length = submitted = 0;
169   size = 6; closed = false;
170   data[3] = type;
171   data[4] = data[5] = 0;
172   packetType = type;
173   pts = PTS_INVALID;
174   seq_header = false;
175 }
176
177 int Demuxer::PESPacket::write(UCHAR *buf, int len)
178 {
179   if (closed) return 0;
180   if (length + len > 0xFFFA) return 0;
181   memcpy(data+length+6, buf, len);
182   length += len;
183   size += len;
184   data[4] = (length >> 8);
185   data[5] = (length & 0xFF);
186   return 1;
187 }
188
189 int Demuxer::PESPacket::submit()
190 {
191   if (submitted >= size) return 0;
192   if (!closed) parseDetails();
193
194   closed = true;
195   Demuxer* dx = Demuxer::getInstance();
196   int sent;
197   if (packetType >= PESTYPE_VID0 && packetType <= PESTYPE_VIDMAX)
198   {
199     if (dx->video_current == -1) dx->video_current = packetType;
200     if (dx->video_current == packetType && !dx->vid_seeking)
201       sent = dx->videostream.put(data+submitted, size-submitted);
202     else
203       sent = size-submitted;
204   }
205   else if (packetType >= PESTYPE_AUD0 && packetType <= PESTYPE_AUDMAX)
206   {
207     if (dx->audio_current == -1) dx->audio_current = packetType;
208     if (dx->audio_current == packetType && !dx->aud_seeking)
209       sent = dx->audiostream.put(data+submitted, size-submitted);
210     else
211       sent = size-submitted;
212   }
213   else
214   {
215     sent = size-submitted;
216   }
217
218   submitted += sent;
219   if (submitted < size) // Stream is full.
220     return 0;
221   else
222     return 1;
223 }
224
225 void Demuxer::PESPacket::parseDetails()
226 {
227   Demuxer* dx = Demuxer::getInstance();
228   if (packetType >= PESTYPE_AUD0 && packetType <= PESTYPE_AUDMAX)
229   {
230     // Extract audio PTS if it exists
231     if ( size >= 14 && (data[7] & 0x80) ) // PTS_DTS_flags indicate PTS
232     {
233       dx->audio_pts = pts = ( (ULLONG)(data[9] & 0x0E)  << 29 ) |
234                             ( (ULLONG)(data[10])        << 22 ) |
235                             ( (ULLONG)(data[11] & 0xFE) << 14 ) |
236                             ( (ULLONG)(data[12])        <<  7 ) |
237                             ( (ULLONG)(data[13] & 0xFE) >>  1 );
238
239       // We continue to seek on the audio if the video PTS that we
240       // are trying to match is ahead of the audio PTS by at most
241       // SEEK_THRESHOLD. We consider the possibility of PTS wrap.
242       if (dx->aud_seeking && !dx->vid_seeking &&
243           !( (dx->video_pts_seek > dx->audio_pts &&
244               dx->video_pts_seek - dx->audio_pts < SEEK_THRESHOLD)
245               ||
246              (dx->video_pts_seek < dx->audio_pts &&
247               dx->video_pts_seek + (1LL<<33) -
248                                    dx->audio_pts < SEEK_THRESHOLD) ))
249       {
250         dx->aud_seeking = 0;
251         Log::getInstance()->log("Demuxer", Log::DEBUG,
252             "Leaving  audio sync: Audio PTS = %llu", dx->audio_pts);
253       }
254     }
255   }
256   else if (packetType >= PESTYPE_VID0 && packetType <= PESTYPE_VIDMAX)
257   {
258     // Extract video PTS if it exists
259     if ( size >= 14 && (data[7] & 0x80) ) // PTS_DTS_flags indicate PTS
260     {
261       dx->video_pts = pts = ( (ULLONG)(data[9] & 0x0E)  << 29 ) |
262                             ( (ULLONG)(data[10])        << 22 ) |
263                             ( (ULLONG)(data[11] & 0xFE) << 14 ) |
264                             ( (ULLONG)(data[12])        <<  7 ) |
265                             ( (ULLONG)(data[13] & 0xFE) >>  1 );
266     }
267
268     // Now, scan for a sequence header and extract information
269     UINT pos = 9; // Start searching from byte 9
270     while (pos < size - 5)
271     {
272       UINT pattern = *(UINT*)(data+pos);
273       if (pattern == DEMUXER_SEQ_HEAD)
274       {
275         seq_header = true;
276         pos += 4;
277         if (pos+6 >= size) return;
278         dx->horizontal_size = ((int)data[pos] << 4) | ((int)data[pos+1] >> 4);
279         dx->vertical_size = (((int)data[pos+1] & 0xf) << 8) | (int)data[pos+2];
280         dx->setAspectRatio((enum AspectRatio)(data[pos+3] >> 4));
281         dx->frame_rate = data[pos+3] & 0x0f;
282         if (dx->frame_rate >= 1 && dx->frame_rate <= 8)
283           dx->frame_rate = FrameRates[dx->frame_rate];
284         else
285           dx->frame_rate = 0;
286         dx->bit_rate = ((int)data[pos+4] << 10) |
287                    ((int)data[pos+5] << 2) |
288                    ((int)data[pos+6] >> 6);
289         if (dx->vid_seeking)
290         {
291           dx->vid_seeking = 0;
292           dx->video_pts_seek = dx->video_pts;
293           Log::getInstance()->log("Demuxer", Log::DEBUG,
294               "Entering audio sync: Video PTS = %llu", dx->video_pts);
295           Log::getInstance()->log("Demuxer", Log::DEBUG,
296               "Entering audio sync: Audio PTS = %llu", dx->audio_pts);
297         }
298         return;
299       }
300       else pos++;
301     }
302   }
303 }
304
305 UINT Demuxer::PESPacket::findPictureHeader()
306 {
307   if (size < 12) return 0;
308   UINT pattern = *(UINT*)(data+8);
309   UINT pos = 11;
310   while (pattern != DEMUXER_PIC_HEAD)
311   {
312     if (++pos >= size) return 0;
313     pattern = (pattern << 8) | data[pos];
314   }
315   return pos-3;
316 }
317
318
319 // static function for stripping audio from a buffer containing an I Frame and its audio
320 UINT Demuxer::stripAudio(UCHAR* buf, UINT len)
321 {
322   UINT read_pos = 0, write_pos = 0;
323   UINT pattern, packet_length;
324   if (len < 4) return 0;
325   pattern = (buf[0] << 16) | (buf[1] << 8) | (buf[2]);
326   while (read_pos + 7 <= len)
327   {
328     pattern = ((pattern << 8) & 0xFFFFFFFF) | buf[read_pos+3];
329     if (pattern < 0x000001E0 || pattern > 0x000001EF)
330       read_pos++;
331     else
332     {
333       packet_length = ((buf[read_pos+4] << 8) | (buf[read_pos+5])) + 6;
334       if (read_pos + packet_length > len)
335         read_pos = len;
336       else
337       {
338         if (read_pos != write_pos)
339           memmove(buf+write_pos, buf+read_pos, packet_length);
340         read_pos += packet_length;
341         write_pos += packet_length;
342         pattern = (buf[read_pos] << 16) | (buf[read_pos+1] << 8)
343                                         | (buf[read_pos+2]);
344       }
345     }
346   }
347   return write_pos;
348 }