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) ret = 0;
131 LOG(VB_GENERAL, LOG_INFO,
132 QString(
"PTSCMP: %1 %2 %3\n").arg(pts1).arg(pts2).arg(ret));
139 uint64_t
ptsadd(uint64_t pts1, uint64_t pts2)
153 if (
p->withbuf && !
p->buf){
154 p->buf =
static_cast<uchar*
>(malloc(
MAX_PLENGTH*
sizeof(uint8_t)));
156 }
else if (rb)
p->rbuf = rb;
159 memset(
p->pts, 0 , 5);
160 memset(
p->dts, 0 , 5);
166 unsigned short *pl =
nullptr;
169 std::array<uint8_t,3> headr { 0x00, 0x00, 0x01} ;
173 while (c < count && (!
p->mpeg ||
174 (
p->mpeg == 2 &&
p->found < 9))
175 && (
p->found < 5 || !
p->done)){
179 if (buf[c] == 0x00)
p->found++;
184 if (buf[c] == 0x01)
p->found++;
185 else if (buf[c] == 0){
202 [[clang::fallthrough]];
222 pl = (
unsigned short *) (buf+c);
223 p->plength = ntohs(*pl);
238 pl = (
unsigned short *)
p->plen;
239 p->plength = ntohs(*pl);
249 if ( (
p->flag1 & 0xC0) == 0x80 )
p->mpeg = 2;
251 LOG(VB_GENERAL, LOG_ERR,
252 "Error: THIS IS AN MPEG1 FILE");
259 if ( !
p->done &&
p->mpeg == 2){
267 if ( !
p->done &&
p->mpeg == 2){
283 if (
p->done || (
p->mpeg == 2 &&
p->found >= 9) ){
291 memcpy(
p->buf, headr.data(), 3);
293 memcpy(
p->buf+4,
p->plen,2);
295 memcpy(
p->hbuf, headr.data(), 3);
297 memcpy(
p->hbuf+4,
p->plen,2);
302 p->buf[6] =
p->flag1;
303 p->buf[7] =
p->flag2;
304 p->buf[8] =
p->hlength;
306 p->hbuf[6] =
p->flag1;
307 p->hbuf[7] =
p->flag2;
308 p->hbuf[8] =
p->hlength;
313 while (c < count && p->found < 14){
314 p->pts[
p->found-9] = buf[c];
316 p->buf[
p->found] = buf[c];
318 p->hbuf[
p->found] = buf[c];
322 if (c == count)
return;
325 if (((
p->flag2 &
PTS_DTS) == 0xC0) &&
p->found < 19){
326 while (c < count && p->found < 19){
327 p->dts[
p->found-14] = buf[c];
329 p->buf[
p->found] = buf[c];
331 p->hbuf[
p->found] = buf[c];
335 if (c == count)
return;
339 while (c < count && p->found < p->plength+6){
341 if (l+
p->found >
p->plength+6)
342 l =
p->plength+6-
p->found;
344 memcpy(
p->buf+
p->found, buf+c, l);
347 (
unsigned int)
p->hlength+9 ){
348 int rest =
p->hlength+9-
p->found;
349 memcpy(
p->hbuf+
p->found, buf+c, rest);
356 LOG(VB_GENERAL, LOG_ERR,
357 QString(
"ring buffer overflow %1")
358 .arg(
p->rbuf->size));
367 if(
p->found ==
p->plength+6){
374 if(
p->found + count - c < p->plength+6){
378 c +=
p->plength+6 -
p->found;
379 p->found =
p->plength+6;
383 if (
p->plength &&
p->found ==
p->plength+6) {
397 static uint32_t scr_base_ps(
const uint8_t *scr)
400 uint8_t *buf = (uint8_t *)&base;
402 buf[0] |= (uint8_t)((scr[0] & 0x18) << 3);
403 buf[0] |= (uint8_t)((scr[0] & 0x03) << 4);
404 buf[0] |= (uint8_t)((scr[1] & 0xF0) >> 4);
406 buf[1] |= (uint8_t)((scr[1] & 0x0F) << 4);
407 buf[1] |= (uint8_t)((scr[2] & 0xF0) >> 4);
409 buf[2] |= (uint8_t)((scr[2] & 0x08) << 4);
410 buf[2] |= (uint8_t)((scr[2] & 0x03) << 5);
411 buf[2] |= (uint8_t)((scr[3] & 0xF8) >> 3);
413 buf[3] |= (uint8_t)((scr[3] & 0x07) << 5);
414 buf[3] |= (uint8_t)((scr[4] & 0xF8) >> 3);
420 static uint16_t scr_ext_ps(
const uint8_t *scr)
424 ext = (short)(scr[5] >> 1);
425 ext += (short) (scr[4] & 0x03) * 128;
434 p->stuff_length=0xF8;
436 p->sheader_length = 0;
451 auto *ll = (
short *)
p->sheader_llength;
452 p->sheader_length = ntohs(*ll) - 6;
458 p->data = (uint8_t *) malloc(
p->sheader_length);
465 std::array<uint8_t,4> headr1 {0x00, 0x00, 0x01,
PACK_START };
466 std::array<uint8_t,4> headr2 {0x00, 0x00, 0x01,
SYS_START };
467 uint8_t buffy = 0xFF;
470 memcpy(buf,headr1.data(),4);
472 memcpy(buf+count,
p->scr,6);
474 memcpy(buf+count,
p->mux_rate,3);
476 memcpy(buf+count,&
p->stuff_length,1);
478 for (
long i=0; i< (
p->stuff_length & 3); i++){
479 memcpy(buf+count,&buffy,1);
483 if (
p->sheader_length){
484 memcpy(buf+count,headr2.data(),4);
486 memcpy(buf+count,
p->sheader_llength,2);
488 memcpy(buf+count,
p->rate_bound,3);
490 memcpy(buf+count,&
p->audio_bound,1);
492 memcpy(buf+count,&
p->video_bound,1);
494 memcpy(buf+count,&
p->reserved,1);
496 memcpy(buf+count,
p->data,
p->sheader_length);
497 count +=
p->sheader_length;
520 uint32_t lscr = htonl((uint32_t) ((SCR/300ULL) & 0x00000000FFFFFFFF));
521 auto *scr = (uint8_t *) 𝓁
522 auto scr_ext = (
uint16_t) ((SCR%300ULL) & 0x00000000000001FF);
532 p.scr[0] = 0x44 | ((scr[0] >> 3)&0x18) | ((scr[0] >> 4)&0x03);
533 p.scr[1] = 0x00 | ((scr[0] << 4)&0xF0) | ((scr[1] >> 4)&0x0F);
534 p.scr[2] = 0x04 | ((scr[1] << 4)&0xF0) | ((scr[2] >> 4)&0x08)
535 | ((scr[2] >> 5)&0x03);
536 p.scr[3] = 0x00 | ((scr[2] << 3)&0xF8) | ((scr[3] >> 5)&0x07);
537 p.scr[4] = 0x04 | ((scr[3] << 3)&0xF8) | ((scr_ext >> 7)&0x03);
538 p.scr[5] = 0x01 | ((scr_ext << 1)&0xFF);
542 p.mux_rate[0] = (uint8_t)(muxr >> 14);
543 p.mux_rate[1] = (uint8_t)(0xff & (muxr >> 6));
544 p.mux_rate[2] = (uint8_t)(0x03 | ((muxr & 0x3f) << 2));
546 p.stuff_length = 0xF8;
549 p.sheader_llength[0] = 0x00;
550 p.sheader_llength[1] = 0x12;
554 p.rate_bound[0] = (uint8_t)(0x80 | (muxr >>15));
555 p.rate_bound[1] = (uint8_t)(0xff & (muxr >> 7));
556 p.rate_bound[2] = (uint8_t)(0x01 | ((muxr & 0x7f)<<1));
559 p.audio_bound = (uint8_t)((audio_bound << 2)|(fixed << 1)|CSPS);
560 p.video_bound = (uint8_t)((audio_lock << 7)|
561 (video_lock << 6)|0x20|video_bound);
562 p.reserved = (uint8_t)(0xFF >> 1);
592 ((spts[0] & 0xC0) >>5);
593 pts[1] = ((spts[0] & 0x3F) << 2) |
594 ((spts[1] & 0xC0) >> 6);
595 pts[2] = 0x01 | ((spts[1] & 0x3F) << 2) |
596 ((spts[2] & 0x80) >> 6);
597 pts[3] = ((spts[2] & 0x7F) << 1) |
598 ((spts[3] & 0x80) >> 7);
599 pts[4] = 0x01 | ((spts[3] & 0x7F) << 1);
603 uint8_t *obuf,
int stuffing, uint8_t ptsdts)
605 std::array<uint8_t,2> le {};
606 std::array<uint8_t,3> dummy {};
609 std::array<uint8_t,3> headr {0x00, 0x00, 0x01};
611 uint32_t lpts = htonl((PTS/300ULL) & 0x00000000FFFFFFFFULL);
612 auto *
pts = (uint8_t *) &lpts;
614 if ((PTS/300ULL) & 0x0000000100000000ULL) ppts[0] |= 0x80;
616 uint32_t ldts = htonl((DTS/300ULL) & 0x00000000FFFFFFFFULL);
617 auto *dts = (uint8_t *) &ldts;
619 if ((DTS/300ULL) & 0x0000000100000000ULL) pdts[0] |= 0x80;
622 memcpy(obuf+c,headr.data(),3);
624 memcpy(obuf+c,&
id,1);
631 le[0] |= ((uint8_t)(length >> 8) & 0xFF);
632 le[1] |= ((uint8_t)(length) & 0xFF);
633 memcpy(obuf+c,le.data(),2);
637 memset(obuf+c,0xff,length);
658 memcpy(obuf+c,dummy.data(),3);
662 memcpy(obuf+c,ppts.data(),5);
664 }
else if ( ptsdts ==
PTS_DTS ){
665 memcpy(obuf+c,ppts.data(),5);
667 memcpy(obuf+c,pdts.data(),5);
671 memset(obuf+c,0xFF,stuffing);
678 uint64_t SCR, uint64_t muxr, uint8_t *buf)
690 uint64_t vdts, uint64_t SCR, uint64_t muxr,
691 uint8_t *buf,
int *vlength,
695 int length = *vlength;
697 if (! length)
return 0;
706 if ( length+
p >= pack_size){
709 if (pack_size - length -
p <=
PES_MIN){
710 stuff = pack_size - length-
p;
721 if (length-pos > *vlength){
722 LOG(VB_GENERAL, LOG_ERR,
723 QString(
"WHAT THE HELL %1 > %2").arg(length-pos).arg(*vlength));
726 int add =
ring_read( vrbuffer, buf+pos, length-pos);
728 if (add < 0)
return -1;
740 uint64_t SCR, uint32_t muxr, uint8_t *buf,
int *alength,
744 int length = *alength;
746 if (!length)
return 0;
753 if ( length+
p >= pack_size){
757 stuff = pack_size - length-
p;
766 int add =
ring_read( arbuffer, buf+pos, length-pos);
768 if (add < 0)
return -1;
776 if (pos != pack_size) {
777 LOG(VB_GENERAL, LOG_ERR, QString(
"apos: %1").arg(pos));
785 uint64_t
pts, uint64_t SCR,
786 uint32_t muxr, uint8_t *buf,
int *alength, uint8_t ptsdts,
787 int nframes,
int ac3_off,
ringbuffer *ac3rbuffer)
790 int length = *alength;
792 if (!length)
return 0;
799 if ( length+
p >= pack_size){
803 stuff = pack_size - length-
p;
812 buf+pos, stuff, ptsdts);
814 buf[pos+1] = nframes;
815 buf[pos+2] = (ac3_off >> 8)& 0xFF;
816 buf[pos+3] = (ac3_off)& 0xFF;
819 int add =
ring_read( ac3rbuffer, buf+pos, length-pos);
821 if (add < 0)
return -1;
829 if (pos != pack_size) {
830 LOG(VB_GENERAL, LOG_ERR, QString(
"apos: %1").arg(pos));
841 std::array<uint8_t,5> headr {0x00, 0x00, 0x01,
PRIVATE_STREAM2, 0x03 };
845 memcpy(buf+pos, headr.data(), 5);
848 memset(buf+pos, 0, 0x03d4);
851 memcpy(buf+pos, headr.data(), 5);
854 memset(buf+pos, 0, 0x03fA);