]> git.vomp.tv Git - vompclient-marten.git/blob - videovpeogl.cc
OpenGL ES does not need power of 2 texture, code removed
[vompclient-marten.git] / videovpeogl.cc
1 /*
2     Copyright 2004-2005 Chris Tallon, 2009 Marten Richter
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #include "videovpeogl.h"
22 #include "audiovpe.h"
23 #include "mtdraspberry.h"
24 #include "demuxer.h"
25 // temp
26 #include "log.h"
27
28
29
30 //taken from libsi
31 //taken and adapted from libdtv, (c) Rolf Hakenes
32 // CRC32 lookup table for polynomial 0x04c11db7
33 unsigned int crc_table[256] = {
34    0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b,
35    0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
36    0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 0x4c11db70, 0x48d0c6c7,
37    0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
38    0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3,
39    0x709f7b7a, 0x745e66cd, 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039,
40    0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 0xbe2b5b58, 0xbaea46ef,
41    0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
42    0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb,
43    0xceb42022, 0xca753d95, 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1,
44    0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, 0x34867077, 0x30476dc0,
45    0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
46    0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4,
47    0x0808d07d, 0x0cc9cdca, 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde,
48    0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02, 0x5e9f46bf, 0x5a5e5b08,
49    0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
50    0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc,
51    0xb6238b25, 0xb2e29692, 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6,
52    0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a, 0xe0b41de7, 0xe4750050,
53    0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
54    0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34,
55    0xdc3abded, 0xd8fba05a, 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637,
56    0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb, 0x4f040d56, 0x4bc510e1,
57    0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
58    0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5,
59    0x3f9b762c, 0x3b5a6b9b, 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff,
60    0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, 0xf12f560e, 0xf5ee4bb9,
61    0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
62    0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd,
63    0xcda1f604, 0xc960ebb3, 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7,
64    0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 0x9b3660c6, 0x9ff77d71,
65    0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
66    0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2,
67    0x470cdd2b, 0x43cdc09c, 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8,
68    0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24, 0x119b4be9, 0x155a565e,
69    0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
70    0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a,
71    0x2d15ebe3, 0x29d4f654, 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0,
72    0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c, 0xe3a1cbc1, 0xe760d676,
73    0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
74    0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662,
75    0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
76    0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4};
77
78 unsigned int crc32 (const char *d, int len, unsigned int crc)
79 {
80    register int i;
81    const unsigned char *u=(unsigned char*)d; // Saves '& 0xff'
82
83    for (i=0; i<len; i++)
84       crc = (crc << 8) ^ crc_table[((crc >> 24) ^ *u++)];
85
86    return crc;
87 }
88
89
90
91 VideoVPEOGL::VideoVPEOGL()
92 {
93   if (instance) return;
94   muxout=NULL;
95   muxnumber=0;
96   lastpacketnum=-1;
97   audioconti=videoconti=0;
98   pmtversion=patversion=0;
99   
100 }
101
102 VideoVPEOGL::~VideoVPEOGL()
103 {
104   instance = NULL;
105 }
106
107 int VideoVPEOGL::init(UCHAR tformat)
108 {
109   if (initted) return 0;
110   initted = 1;
111
112 //  if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
113
114   if (!setFormat(tformat))           { shutdown(); return 0; }
115   if (!setConnection(COMPOSITERGB))  { shutdown(); return 0; }
116   if (!setAspectRatio(ASPECT4X3))    { shutdown(); return 0; }
117   if (!setMode(NORMAL))              { shutdown(); return 0; }
118   if (!setSource())                  { shutdown(); return 0; }
119   if (!attachFrameBuffer())          { shutdown(); return 0; }
120
121   setTVsize(ASPECT4X3);
122
123 /*  if (format == PAL) setLetterboxBorder("38");
124   else setLetterboxBorder("31");*/
125
126   stop();
127
128
129   return 1;
130 }
131
132
133
134 int VideoVPEOGL::setTVsize(UCHAR ttvsize)
135 {
136 /*  tvsize = ttvsize;
137
138   // Override the aspect ratio usage, temporarily use to set the video chip mode
139   if (!setAspectRatio(tvsize))       { shutdown(); return 0; }
140   close(fdVideo);
141   if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
142   if (!setSource())                  { shutdown(); return 0; }
143   if (!attachFrameBuffer())          { shutdown(); return 0; }
144
145   // Reopening the fd causes the scart aspect line to go back to 4:3
146   // Set this again to the same as the tv screen size
147   if (!setAspectRatio(tvsize))       { shutdown(); return 0; }
148
149   // mode == LETTERBOX is invalid if the TV is widescreen
150   if (tvsize == ASPECT16X9) setMode(NORMAL);
151 */
152   return 1;
153 }
154
155 int VideoVPEOGL::setDefaultAspect()
156 {
157   return setAspectRatio(tvsize);
158 }
159
160 int VideoVPEOGL::shutdown()
161 {
162   if (!initted) return 0;
163   initted = 0;
164 //  close(fdVideo);
165   return 1;
166 }
167
168
169 int VideoVPEOGL::setFormat(UCHAR tformat)
170 {
171   if (!initted) return 0;
172   if ((tformat != PAL) && (tformat != NTSC)) return 0;
173   format = tformat;
174
175 //  if (ioctl(fdVideo, AV_SET_VID_DISP_FMT, format) != 0) return 0;
176
177   if (format == NTSC)
178   {
179     screenWidth = 720;
180     screenHeight = 480;
181   }
182   if (format == PAL)
183   {
184     screenWidth = 720;
185     screenHeight = 576;
186   }
187
188   return 1;
189 }
190
191 int VideoVPEOGL::setConnection(UCHAR tconnection)
192 {
193   if (!initted) return 0;
194   if ((tconnection != COMPOSITERGB) && (tconnection != SVIDEO)) return 0;
195   connection = tconnection;
196
197 //  if (ioctl(fdVideo, AV_SET_VID_OUTPUT, connection) != 0) return 0;
198   return 1;
199 }
200
201 int VideoVPEOGL::setAspectRatio(UCHAR taspectRatio)
202 {
203   if (!initted) return 0;
204   if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
205   aspectRatio = taspectRatio;
206
207   Log::getInstance()->log("Video", Log::DEBUG, "Setting aspect to %i", aspectRatio);
208
209 //  if (ioctl(fdVideo, AV_SET_VID_RATIO, aspectRatio) != 0) return 0;
210   return 1;
211 }
212
213 int VideoVPEOGL::setMode(UCHAR tmode)
214 {
215   if (!initted) return 0;
216
217   if ((tmode == LETTERBOX) && (tvsize == ASPECT16X9)) return 0; // invalid mode
218
219   if ((tmode != NORMAL) && (tmode != LETTERBOX) && (tmode != UNKNOWN2) && (tmode != QUARTER) && (tmode != EIGHTH)
220       && (tmode != ZOOM) && (tmode != UNKNOWN6)) return 0;
221   mode = tmode;
222
223 //  if (ioctl(fdVideo, AV_SET_VID_MODE, mode) != 0) return 0;
224   return 1;
225 }
226
227 int VideoVPEOGL::signalOff()
228 {
229 //  if (ioctl(fdVideo, AV_SET_VID_DENC, 0) != 0) return 0;
230   return 1;
231 }
232
233 int VideoVPEOGL::signalOn()
234 {
235 //  if (ioctl(fdVideo, AV_SET_VID_DENC, 1) != 0) return 0;
236   return 1;
237 }
238
239 int VideoVPEOGL::setSource()
240 {
241   if (!initted) return 0;
242
243   // What does this do...
244 //  if (ioctl(fdVideo, AV_SET_VID_SRC, 1) != 0) return 0;
245   return 1;
246 }
247
248 int VideoVPEOGL::setPosition(int x, int y)
249 {
250   if (!initted) return 0;
251
252 //  vid_pos_regs_t pos_d;
253 //  pos_d.x = x;
254 //  pos_d.y = y;
255
256 /*  vid_pos_regs_t pos_d;
257
258   memset(&pos_d, 0, sizeof(pos_d));
259
260   pos_d.dest.y = y;
261   pos_d.dest.x = x;
262 /*
263 typedef struct {
264   int w;
265   int h;
266   int scale;
267   int x1;
268   int y;
269   int x;
270   int y2;
271   int x3;
272   int y3;
273   int x4;
274   int y4;
275 } vid_pos_regs_t;
276 */
277
278 /*
279   pos_d.w = 100;
280   pos_d.h = 30;
281   pos_d.scale = 2;
282   pos_d.x1 = 0;
283   pos_d.y = 100;            // Top left X
284   pos_d.x = 50;            // Top left Y
285   pos_d.y2 = 30;
286   pos_d.x3 = 60;
287   pos_d.y3 = 90;
288   pos_d.x4 = 120;
289   pos_d.y4 = 150;
290 */
291
292 //  if (ioctl(fdVideo, AV_SET_VID_POSITION, &pos_d) != 0) return 0;
293   return 1;
294 }
295
296 int VideoVPEOGL::sync()
297 {
298   if (!initted) return 0;
299
300 //  if (ioctl(fdVideo, AV_SET_VID_SYNC, 2) != 0) return 0;
301   return 1;
302 }
303
304 void VideoVPEOGL::initMuxer(bool iframe)
305 {
306         char buffer[1024];
307         lastpacketnum=-1;
308         iframemode=iframe;
309   if (muxout) return;
310   sprintf(buffer,"/share/dev/muxout%d.ts",muxnumber);
311   filemutex.Lock(); 
312   audioconti=videoconti=0;
313   muxout=fopen(buffer,"wb");
314   filemutex.Unlock();
315   WriteOutPATPMT();
316   Log::getInstance()->log("Video", Log::DEBUG, 
317                "Create file %s with result %d",buffer,muxout);
318 }
319
320 void VideoVPEOGL::deinitMuxer()
321 {
322   if (muxout)
323   {
324           filemutex.Lock();
325           FILE*temp=muxout;
326           muxout=NULL;
327           audioconti=videoconti=0;
328           filemutex.Unlock();
329           Log::getInstance()->log("Video", Log::DEBUG, 
330                "Close mux");
331
332           fclose(temp);
333           muxnumber++;
334   }
335 }
336
337
338 int VideoVPEOGL::play()
339 {
340   if (!initted) return 0;
341
342   
343
344 //  if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
345   return 1;
346 }
347
348 int VideoVPEOGL::stop()
349 {
350   if (!initted) return 0;
351
352   
353
354 //  if (ioctl(fdVideo, AV_SET_VID_STOP, 0) != 0) return 0;
355   return 1;
356 }
357
358 int VideoVPEOGL::reset()
359 {
360   if (!initted) return 0;
361  
362 //  if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0;
363   return 1;
364 }
365
366 int VideoVPEOGL::pause()
367 {
368   if (!initted) return 0;
369
370 //  if (ioctl(fdVideo, AV_SET_VID_PAUSE, 0) != 0) return 0;
371   return 1;
372 }
373
374 int VideoVPEOGL::unPause() // FIXME get rid - same as play!!
375 {
376   if (!initted) return 0;
377 //  if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
378   return 1;
379 }
380
381 int VideoVPEOGL::fastForward()
382 {
383   if (!initted) return 0;
384
385 //  if (ioctl(fdVideo, AV_SET_VID_FFWD, 1) != 0) return 0;
386   return 1;
387 }
388
389 int VideoVPEOGL::unFastForward()
390 {
391   if (!initted) return 0;
392
393 //  if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0; // don't need this.
394
395  //// if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
396   return 1;
397 }
398
399 int VideoVPEOGL::attachFrameBuffer()
400 {
401   if (!initted) return 0;
402
403 //  if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
404   return 1;
405 }
406
407 int VideoVPEOGL::blank(void)
408 {
409 //  if (ioctl(fdVideo, AV_SET_VID_FB, 1) != 0) return 0;
410 //  if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
411   return 1;
412 }
413
414 ULLONG VideoVPEOGL::getCurrentTimestamp()
415 {
416 /*  sync_data_t timestamps;
417   if (ioctl(fdVideo, AV_GET_VID_TIMESTAMPS, &timestamps) == 0)
418   {
419     // FIXME are these the right way around?
420
421     timestamps.stc = (timestamps.stc >> 31 ) | (timestamps.stc & 1);
422     timestamps.pts = (timestamps.pts >> 31 ) | (timestamps.pts & 1);
423
424     return timestamps.stc;
425   }
426   else
427   {
428     return 0;
429   }*/
430   return 0;
431 }
432
433 ULONG VideoVPEOGL::timecodeToFrameNumber(ULLONG timecode)
434 {
435   if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
436   else               return (ULONG)(((double)timecode / (double)90000) * (double)30);
437 }
438
439 #ifdef DEV
440 int VideoVPEOGL::test()
441 {
442   return 0;
443
444 //  ULLONG stc = 0;
445 //  return ioctl(fdVideo, AV_SET_VID_STC, &stc);
446 /*
447  // reset();
448   return 1;
449 */
450 }
451
452 int VideoVPEOGL::test2()
453 {
454   return 0;
455 }
456 #endif
457
458 void VideoVPEOGL::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
459 {
460   mediapacket = mplist.front();
461 }
462
463 UINT VideoVPEOGL::DeliverMediaSample(UCHAR* buffer, UINT* samplepos)
464 {
465   if (!muxout) return 0; 
466   
467   AudioVPE *audio=((AudioVPE*)audio->getInstance());
468  // Log::getInstance()->log("Video", Log::DEBUG, "lastpacket %d lastbuddypacket %d currentpacket %d",lastpacketnum,audio->getLastPacketNum(),mediapacket.index);
469  
470   //format pes
471   switch (mediapacket.type) {
472           case MPTYPE_VIDEO_MPEG2:
473                   h264=false;
474                   buffer[mediapacket.pos_buffer + 3]=0xe0; break;
475           case MPTYPE_VIDEO_H264:
476                   h264=true;
477                   buffer[mediapacket.pos_buffer + 3]=0xe0; break;
478   };
479   
480   
481   if (lastpacketnum!=-1 && mediapacket.index-1!=lastpacketnum && mediapacket.index-1>audio->getLastPacketNum() && mediapacket.index>audio->getCurrentPacketNum()) {
482           Log::getInstance()->log("Video", Log::DEBUG, "Packet not in order, packet num %d, lastpacketnum %d, audio lastpacketnum %d",mediapacket.index,lastpacketnum,audio->getLastPacketNum());
483           return 0; //Not in order
484   }
485   
486
487 /*  filemutex.Lock();
488   int written=fwrite(buffer + mediapacket.pos_buffer,1,mediapacket.length,muxout);*/
489   if (mediapacket.synched) WriteOutPATPMT();
490
491   int written=WriteOutTS(buffer + mediapacket.pos_buffer,mediapacket.length, mediapacket.type);
492   lastpacketnum=mediapacket.index;
493   //filemutex.Unlock();
494   //Log::getInstance()->log("Video", Log::DEBUG, "wrote %d bytes to mux",written);
495   
496
497   if (written == (int)mediapacket.length) { *samplepos = 0; return 1;}
498   if (written <= 0) {
499           return 0;
500   }
501   *samplepos = written;
502   // Handle a partial write. Is this possible? Should we bother? No!
503  
504   return 1;
505 }
506 /*
507
508 int VideoVPEOGL::WriteOutPCR(ULLONG pts)
509 {
510         unsigned char ts_buffer[188];
511         unsigned char ts_crc[4];
512         unsigned char conti;
513         memset(ts_buffer,0xFF,188);
514
515         conti=videoconti;
516         ts_buffer[0]=0x47; //TS_SYNC
517         ts_buffer[1]=0x40; //Payload_start
518         ts_buffer[2]=100; 
519         ts_buffer[3]=0x20| conti;
520         ts_buffer[4]=183;
521         ts_buffer[5]=0x10; //PCR!
522
523                 
524         
525     int tocopy=length;
526         unsigned char *dest=buffer;
527     filemutex.Lock();
528     unsigned int outlength=184;
529         while (tocopy>=outlength) {
530                 
531                 ts_buffer[3]=0x10 | conti;
532                 fwrite(ts_buffer,1,4,muxout); //Header out
533
534                 fwrite(dest,1, outlength,muxout);
535         
536
537                 dest+=outlength;
538                 tocopy-=outlength;
539                 conti++;
540                 conti&=0x0F;
541                 ts_buffer[1]=0x00; //Payload_start
542         }
543         if (tocopy>0) {
544                 ts_buffer[3]=0x30 | conti;//we have an adpation field
545                 fwrite(ts_buffer,1,4,muxout); //Header out
546                 unsigned char adaptionfield[2]={outlength-tocopy-1,0x00};
547                 if (tocopy<183) fwrite(adaptionfield,1,2,muxout);
548                 else fwrite(adaptionfield,1,1,muxout);
549                 unsigned char paddy=0xFF;
550                 if (outlength>tocopy+2) {
551                         for (int i=0;i<(outlength-tocopy-2);i++) fwrite(&paddy,1,1,muxout);
552                 }
553                 fwrite(dest,1,tocopy,muxout);
554         }
555         
556         
557         conti++;
558         conti&=0x0F;
559         filemutex.Unlock();
560
561         
562                 videoconti=conti; 
563         
564 }*/
565
566 int VideoVPEOGL::WriteOutTS(const unsigned char *buffer,int length, int type)
567 {
568         unsigned char ts_buffer[4];
569         unsigned char ts_crc[4];
570         unsigned char conti;
571         ts_buffer[0]=0x47; //TS_SYNC
572         ts_buffer[1]=0x40; //Payload_start
573         switch (type) {
574          case MPTYPE_VIDEO_MPEG2:
575          case MPTYPE_VIDEO_H264:
576                 conti=videoconti;
577                 ts_buffer[2]=100; break;
578                 
579         case  MPTYPE_MPEG_AUDIO: 
580                 conti=audioconti;
581                 ts_buffer[2]=101; break;
582         case  MPTYPE_AC3:
583         case MPTYPE_AC3_PRE13:
584                 conti=audioconti;
585                 ts_buffer[2]=102; break;
586         default:
587                 return 0; break; //????
588         };
589     int tocopy=length;
590         const unsigned char *dest=buffer;
591     filemutex.Lock();
592     unsigned int outlength=184;
593         while (tocopy>=outlength) {
594                 
595                 ts_buffer[3]=0x10 | conti;
596                 fwrite(ts_buffer,1,4,muxout); //Header out
597
598                 fwrite(dest,1, outlength,muxout);
599         
600
601                 dest+=outlength;
602                 tocopy-=outlength;
603                 conti++;
604                 conti&=0x0F;
605                 ts_buffer[1]=0x00; //Payload_start
606         }
607         if (tocopy>0) {
608                 ts_buffer[3]=0x30 | conti;//we have an adpation field
609                 fwrite(ts_buffer,1,4,muxout); //Header out
610                 unsigned char adaptionfield[2]={outlength-tocopy-1,0x00};
611                 if (tocopy<183) fwrite(adaptionfield,1,2,muxout);
612                 else fwrite(adaptionfield,1,1,muxout);
613                 unsigned char paddy=0xFF;
614                 if (outlength>tocopy+2) {
615                         for (int i=0;i<(outlength-tocopy-2);i++) fwrite(&paddy,1,1,muxout);
616                 }
617                 fwrite(dest,1,tocopy,muxout);
618         }
619         
620         
621         conti++;
622         conti&=0x0F;
623         filemutex.Unlock();
624
625         switch (type) {
626          case MPTYPE_VIDEO_MPEG2:
627           case MPTYPE_VIDEO_H264:
628                 videoconti=conti; break;
629                 
630         case  MPTYPE_MPEG_AUDIO: 
631         case  MPTYPE_AC3: 
632         case MPTYPE_AC3_PRE13:
633                 audioconti=conti;
634                 break;
635         };
636         return length;
637         
638 }
639
640 void VideoVPEOGL::WriteOutPATPMT()
641 {
642         unsigned char ts_buffer[188];
643         //PAT
644         memset(ts_buffer,0xFF,188);
645         ts_buffer[0]=0x47; //TS_SYNC
646         ts_buffer[1]=0x40; //Payload_start
647         ts_buffer[2]=0x00; 
648         ts_buffer[3]=0x10;
649         ts_buffer[4]=0x00;
650         ts_buffer[5]=0x00;//table id
651         ts_buffer[6]=0xB0; //Section syntax
652         ts_buffer[7]=13; //Section length
653         ts_buffer[8]=0x80;//PSID
654         ts_buffer[9]=0x08;//PSID lo 
655         ts_buffer[10]=0xC1 | (patversion << 1);
656         ts_buffer[11]=0x00;//section numbrt
657         ts_buffer[12]=0x00;
658         ts_buffer[13]=0x00; //hi program number
659         ts_buffer[14]=98; 
660         ts_buffer[15]=0xe0; 
661         ts_buffer[16]=98;
662         int crc =crc32((const char *)ts_buffer+5, 12, 0xFFFFFFFF);
663         int i = 0;
664         ts_buffer[17] = crc >> 24;
665         ts_buffer[18] = crc >> 16;
666         ts_buffer[19] = crc >> 8;
667         ts_buffer[20] = crc;
668         patversion++;
669         patversion&=0x1F;
670         filemutex.Lock();
671         fwrite(ts_buffer,1,188,muxout); //Header out
672         filemutex.Unlock();
673
674         //PMT
675         memset(ts_buffer,0xFF,188);
676         ts_buffer[0]=0x47; //TS_SYNC
677         ts_buffer[1]=0x40; //Payload_start
678         ts_buffer[2]=98; 
679         ts_buffer[3]=0x10;
680         ts_buffer[4]=0x00;
681         ts_buffer[5]=0x02;//table id
682         ts_buffer[6]=0xB0; //Section syntax
683         ts_buffer[7]=23; //Section length
684         ts_buffer[8]=0x00;//PSID
685         ts_buffer[9]=98;//PSID lo 
686         ts_buffer[10]=0xC1 | (pmtversion << 1);
687         ts_buffer[11]=0x00;//section numbrt
688         ts_buffer[12]=0x00;
689         ts_buffer[13]=0xE0 | 0x1F;
690         ts_buffer[14]=0xFF;//Video is pcr 
691         ts_buffer[15]=0xF0;//dummy 
692         ts_buffer[16]=0x00; //program info
693     //Video
694
695         if (h264) 
696         {
697                 ts_buffer[17] = 0x1B; // stream type video
698         }
699         else
700         {
701                 ts_buffer[17] = 0x02; // stream type video
702         }
703     ts_buffer[18] = 0xE0; //
704         ts_buffer[19]=100; //Video pid 
705         ts_buffer[20]= 0xF0; // 
706     ts_buffer[21]= 0x00; //
707         // audio
708         int add_length=0;
709         unsigned char atype=((AudioVPE*)Audio::getInstance())->getLastAType();
710         switch (atype) {
711         default:
712         case  MPTYPE_MPEG_AUDIO: 
713                 ts_buffer[22] = 0x04; // stream type audio
714                 ts_buffer[23] = 0xE0; //
715                 ts_buffer[24]=101; //Audio pid 
716                 ts_buffer[25]= 0xF0; // 
717                 ts_buffer[26]= 0x00; //
718         break;
719         case  MPTYPE_AC3: 
720         case MPTYPE_AC3_PRE13:
721                 ts_buffer[22] = 0x06; // stream type private
722                 ts_buffer[23] = 0xE0; //
723                 ts_buffer[24]=102; //ac3
724                 ts_buffer[25]= 0xF0; // 
725                 ts_buffer[26]= 0x03; //
726                 ts_buffer[27] = 0x6A;
727                 ts_buffer[28] = 0x01; // length
728                 ts_buffer[29] = 0x00;
729                 ts_buffer[7]+=3;
730                 add_length=3;
731                 break;
732         };
733         crc =crc32((const char *)ts_buffer+5, 22+add_length, 0xFFFFFFFF);
734         ts_buffer[27+add_length] = crc >> 24;
735         ts_buffer[28+add_length] = crc >> 16;
736         ts_buffer[29+add_length] = crc >> 8;
737         ts_buffer[30+add_length] = crc;
738         pmtversion++;
739         pmtversion&=0x1F;
740         filemutex.Lock();
741         fwrite(ts_buffer,1,188,muxout); //Header out
742         filemutex.Unlock();
743
744
745         
746 }
747
748
749 void VideoVPEOGL::ResetTimeOffsets()
750 {
751 }
752
753 bool VideoVPEOGL::displayIFrame(const UCHAR* buffer, UINT length)
754 {
755   //write(fdVideo, buffer, length);
756         if (!iframemode) EnterIframePlayback();
757         WriteOutTS(buffer,length, h264?MPTYPE_VIDEO_H264 :MPTYPE_VIDEO_MPEG2 );
758  
759   lastpacketnum=-1;
760   return true;
761 }
762
763 int VideoVPEOGL::EnterIframePlayback()
764 {
765         deinitMuxer();
766         initMuxer(true);
767         return 0;
768 }
769