37 #include <netinet/in.h>
52 LOG(VB_GENERAL, LOG_INFO, QString(
"%1%2:%3:%4.%5")
53 .arg(negative ?
"-" :
"")
54 .arg((
unsigned int)(
pts/90000.0)/3600, 2,10,QChar(
'0'))
55 .arg(((
unsigned int)(
pts/90000.0)%3600)/60, 2,10,QChar(
'0'))
56 .arg(((
unsigned int)(
pts/90000.0)%3600)%60, 2,10,QChar(
'0'))
57 .arg((((
unsigned int)(
pts/9.0)%36000000)%600000)%10000, 4,10,QChar(
'0')));
69 LOG(VB_GENERAL, LOG_INFO, QString(
"%1%2:%3:%4.%5")
70 .arg(negative ?
"-" :
"")
71 .arg((
unsigned int)(
pts/90000.0)/3600, 2,10,QChar(
'0'))
72 .arg(((
unsigned int)(
pts/90000.0)%3600)/60, 2,10,QChar(
'0'))
73 .arg(((
unsigned int)(
pts/90000.0)%3600)%60, 2,10,QChar(
'0'))
74 .arg((((
unsigned int)(
pts/90.0)%3600000)%60000)%1000, 3,10,QChar(
'0')));
78 int64_t
ptsdiff(uint64_t pts1, uint64_t pts2)
80 switch (
ptscmp(pts1, pts2)){
106 int64_t diff = pts1 - pts2;
123 }
else if (pts1 == pts2) {
132 LOG(VB_GENERAL, LOG_INFO,
133 QString(
"PTSCMP: %1 %2 %3\n").arg(pts1).arg(pts2).arg(ret));
140 uint64_t
ptsadd(uint64_t pts1, uint64_t pts2)
154 if (
p->withbuf && !
p->buf){
155 p->buf =
static_cast<uchar*
>(malloc(
MAX_PLENGTH*
sizeof(uint8_t)));
162 memset(
p->pts, 0 , 5);
163 memset(
p->dts, 0 , 5);
169 unsigned short *pl =
nullptr;
172 std::array<uint8_t,3> headr { 0x00, 0x00, 0x01} ;
176 while (c < count && (!
p->mpeg ||
177 (
p->mpeg == 2 &&
p->found < 9))
178 && (
p->found < 5 || !
p->done)){
182 if (buf[c] == 0x00)
p->found++;
187 if (buf[c] == 0x01)
p->found++;
188 else if (buf[c] == 0){
227 pl = (
unsigned short *) (buf+c);
228 p->plength = ntohs(*pl);
243 pl = (
unsigned short *)
p->plen;
244 p->plength = ntohs(*pl);
254 if ( (
p->flag1 & 0xC0) == 0x80 )
p->mpeg = 2;
256 LOG(VB_GENERAL, LOG_ERR,
257 "Error: THIS IS AN MPEG1 FILE");
264 if ( !
p->done &&
p->mpeg == 2){
272 if ( !
p->done &&
p->mpeg == 2){
288 if (
p->done || (
p->mpeg == 2 &&
p->found >= 9) ){
296 memcpy(
p->buf, headr.data(), 3);
298 memcpy(
p->buf+4,
p->plen,2);
300 memcpy(
p->hbuf, headr.data(), 3);
302 memcpy(
p->hbuf+4,
p->plen,2);
307 p->buf[6] =
p->flag1;
308 p->buf[7] =
p->flag2;
309 p->buf[8] =
p->hlength;
311 p->hbuf[6] =
p->flag1;
312 p->hbuf[7] =
p->flag2;
313 p->hbuf[8] =
p->hlength;
318 while (c < count && p->found < 14){
319 p->pts[
p->found-9] = buf[c];
321 p->buf[
p->found] = buf[c];
323 p->hbuf[
p->found] = buf[c];
327 if (c == count)
return;
330 if (((
p->flag2 &
PTS_DTS) == 0xC0) &&
p->found < 19){
331 while (c < count && p->found < 19){
332 p->dts[
p->found-14] = buf[c];
334 p->buf[
p->found] = buf[c];
336 p->hbuf[
p->found] = buf[c];
340 if (c == count)
return;
344 while (c < count && p->found < p->plength+6){
346 if (l+
p->found >
p->plength+6)
347 l =
p->plength+6-
p->found;
349 memcpy(
p->buf+
p->found, buf+c, l);
352 (
unsigned int)
p->hlength+9 ){
353 int rest =
p->hlength+9-
p->found;
354 memcpy(
p->hbuf+
p->found, buf+c, rest);
361 LOG(VB_GENERAL, LOG_ERR,
362 QString(
"ring buffer overflow %1")
363 .arg(
p->rbuf->size));
372 if(
p->found ==
p->plength+6){
379 if(
p->found + count - c < p->plength+6){
383 c +=
p->plength+6 -
p->found;
384 p->found =
p->plength+6;
388 if (
p->plength &&
p->found ==
p->plength+6) {
402 static uint32_t scr_base_ps(
const uint8_t *scr)
405 uint8_t *buf = (uint8_t *)&base;
407 buf[0] |= (uint8_t)((scr[0] & 0x18) << 3);
408 buf[0] |= (uint8_t)((scr[0] & 0x03) << 4);
409 buf[0] |= (uint8_t)((scr[1] & 0xF0) >> 4);
411 buf[1] |= (uint8_t)((scr[1] & 0x0F) << 4);
412 buf[1] |= (uint8_t)((scr[2] & 0xF0) >> 4);
414 buf[2] |= (uint8_t)((scr[2] & 0x08) << 4);
415 buf[2] |= (uint8_t)((scr[2] & 0x03) << 5);
416 buf[2] |= (uint8_t)((scr[3] & 0xF8) >> 3);
418 buf[3] |= (uint8_t)((scr[3] & 0x07) << 5);
419 buf[3] |= (uint8_t)((scr[4] & 0xF8) >> 3);
425 static uint16_t scr_ext_ps(
const uint8_t *scr)
429 ext = (short)(scr[5] >> 1);
430 ext += (short) (scr[4] & 0x03) * 128;
439 p->stuff_length=0xF8;
441 p->sheader_length = 0;
456 auto *ll = (
short *)
p->sheader_llength;
457 p->sheader_length = ntohs(*ll) - 6;
463 p->data = (uint8_t *) malloc(
p->sheader_length);
470 std::array<uint8_t,4> headr1 {0x00, 0x00, 0x01,
PACK_START };
471 std::array<uint8_t,4> headr2 {0x00, 0x00, 0x01,
SYS_START };
472 uint8_t buffy = 0xFF;
475 memcpy(buf,headr1.data(),4);
477 memcpy(buf+count,
p->scr,6);
479 memcpy(buf+count,
p->mux_rate,3);
481 memcpy(buf+count,&
p->stuff_length,1);
483 for (
long i=0; i< (
p->stuff_length & 3); i++){
484 memcpy(buf+count,&buffy,1);
488 if (
p->sheader_length){
489 memcpy(buf+count,headr2.data(),4);
491 memcpy(buf+count,
p->sheader_llength,2);
493 memcpy(buf+count,
p->rate_bound,3);
495 memcpy(buf+count,&
p->audio_bound,1);
497 memcpy(buf+count,&
p->video_bound,1);
499 memcpy(buf+count,&
p->reserved,1);
501 memcpy(buf+count,
p->data,
p->sheader_length);
502 count +=
p->sheader_length;
525 uint32_t lscr = htonl((uint32_t) ((SCR/300ULL) & 0x00000000FFFFFFFF));
526 auto *scr = (uint8_t *) 𝓁
527 auto scr_ext = (
uint16_t) ((SCR%300ULL) & 0x00000000000001FF);
537 p.scr[0] = 0x44 | ((scr[0] >> 3)&0x18) | ((scr[0] >> 4)&0x03);
538 p.scr[1] = 0x00 | ((scr[0] << 4)&0xF0) | ((scr[1] >> 4)&0x0F);
539 p.scr[2] = 0x04 | ((scr[1] << 4)&0xF0) | ((scr[2] >> 4)&0x08)
540 | ((scr[2] >> 5)&0x03);
541 p.scr[3] = 0x00 | ((scr[2] << 3)&0xF8) | ((scr[3] >> 5)&0x07);
542 p.scr[4] = 0x04 | ((scr[3] << 3)&0xF8) | ((scr_ext >> 7)&0x03);
543 p.scr[5] = 0x01 | ((scr_ext << 1)&0xFF);
547 p.mux_rate[0] = (uint8_t)(muxr >> 14);
548 p.mux_rate[1] = (uint8_t)(0xff & (muxr >> 6));
549 p.mux_rate[2] = (uint8_t)(0x03 | ((muxr & 0x3f) << 2));
551 p.stuff_length = 0xF8;
554 p.sheader_llength[0] = 0x00;
555 p.sheader_llength[1] = 0x12;
559 p.rate_bound[0] = (uint8_t)(0x80 | (muxr >>15));
560 p.rate_bound[1] = (uint8_t)(0xff & (muxr >> 7));
561 p.rate_bound[2] = (uint8_t)(0x01 | ((muxr & 0x7f)<<1));
564 p.audio_bound = (uint8_t)((audio_bound << 2)|(fixed << 1)|CSPS);
565 p.video_bound = (uint8_t)((audio_lock << 7)|
566 (video_lock << 6)|0x20|video_bound);
567 p.reserved = (uint8_t)(0xFF >> 1);
597 ((spts[0] & 0xC0) >>5);
598 pts[1] = ((spts[0] & 0x3F) << 2) |
599 ((spts[1] & 0xC0) >> 6);
600 pts[2] = 0x01 | ((spts[1] & 0x3F) << 2) |
601 ((spts[2] & 0x80) >> 6);
602 pts[3] = ((spts[2] & 0x7F) << 1) |
603 ((spts[3] & 0x80) >> 7);
604 pts[4] = 0x01 | ((spts[3] & 0x7F) << 1);
608 uint8_t *obuf,
int stuffing, uint8_t ptsdts)
610 std::array<uint8_t,2> le {};
611 std::array<uint8_t,3> dummy {};
614 std::array<uint8_t,3> headr {0x00, 0x00, 0x01};
616 uint32_t lpts = htonl((PTS/300ULL) & 0x00000000FFFFFFFFULL);
617 auto *
pts = (uint8_t *) &lpts;
619 if ((PTS/300ULL) & 0x0000000100000000ULL) ppts[0] |= 0x80;
621 uint32_t ldts = htonl((DTS/300ULL) & 0x00000000FFFFFFFFULL);
622 auto *dts = (uint8_t *) &ldts;
624 if ((DTS/300ULL) & 0x0000000100000000ULL) pdts[0] |= 0x80;
627 memcpy(obuf+c,headr.data(),3);
629 memcpy(obuf+c,&
id,1);
636 le[0] |= ((uint8_t)(length >> 8) & 0xFF);
637 le[1] |= ((uint8_t)(length) & 0xFF);
638 memcpy(obuf+c,le.data(),2);
642 memset(obuf+c,0xff,length);
663 memcpy(obuf+c,dummy.data(),3);
667 memcpy(obuf+c,ppts.data(),5);
669 }
else if ( ptsdts ==
PTS_DTS ){
670 memcpy(obuf+c,ppts.data(),5);
672 memcpy(obuf+c,pdts.data(),5);
676 memset(obuf+c,0xFF,stuffing);
683 uint64_t SCR, uint64_t muxr, uint8_t *buf)
695 uint64_t vdts, uint64_t SCR, uint64_t muxr,
696 uint8_t *buf,
int *vlength,
700 int length = *vlength;
702 if (! length)
return 0;
711 if ( length+
p >= pack_size){
714 if (pack_size - length -
p <=
PES_MIN){
715 stuff = pack_size - length-
p;
727 if (length-pos > *vlength){
728 LOG(VB_GENERAL, LOG_ERR,
729 QString(
"WHAT THE HELL %1 > %2").arg(length-pos).arg(*vlength));
732 int add =
ring_read( vrbuffer, buf+pos, length-pos);
734 if (add < 0)
return -1;
746 uint64_t SCR, uint32_t muxr, uint8_t *buf,
int *alength,
750 int length = *alength;
752 if (!length)
return 0;
759 if ( length+
p >= pack_size){
763 stuff = pack_size - length-
p;
773 int add =
ring_read( arbuffer, buf+pos, length-pos);
775 if (add < 0)
return -1;
783 if (pos != pack_size) {
784 LOG(VB_GENERAL, LOG_ERR, QString(
"apos: %1").arg(pos));
792 uint64_t
pts, uint64_t SCR,
793 uint32_t muxr, uint8_t *buf,
int *alength, uint8_t ptsdts,
794 int nframes,
int ac3_off,
ringbuffer *ac3rbuffer)
797 int length = *alength;
799 if (!length)
return 0;
806 if ( length+
p >= pack_size){
810 stuff = pack_size - length-
p;
820 buf+pos, stuff, ptsdts);
822 buf[pos+1] = nframes;
823 buf[pos+2] = (ac3_off >> 8)& 0xFF;
824 buf[pos+3] = (ac3_off)& 0xFF;
827 int add =
ring_read( ac3rbuffer, buf+pos, length-pos);
829 if (add < 0)
return -1;
837 if (pos != pack_size) {
838 LOG(VB_GENERAL, LOG_ERR, QString(
"apos: %1").arg(pos));
849 std::array<uint8_t,5> headr {0x00, 0x00, 0x01,
PRIVATE_STREAM2, 0x03 };
853 memcpy(buf+pos, headr.data(), 5);
856 memset(buf+pos, 0, 0x03d4);
859 memcpy(buf+pos, headr.data(), 5);
862 memset(buf+pos, 0, 0x03fA);