/* * markad-standalone.cpp: A program for the Video Disk Recorder * * See the README file for copyright information and how to reach the author. * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "markad-standalone.h" #include "version.h" extern int sys_ioprio_set(int which, int who, int ioprio); bool SYSLOG=false; cMarkAdStandalone *cmasta=NULL; int SysLogLevel=2; static inline int ioprio_set(int which, int who, int ioprio) { #if defined(__i386__) #define __NR_ioprio_set 289 #define __NR_ioprio_get 290 #elif defined(__ppc__) #define __NR_ioprio_set 273 #define __NR_ioprio_get 274 #elif defined(__x86_64__) #define __NR_ioprio_set 251 #define __NR_ioprio_get 252 #elif defined(__ia64__) #define __NR_ioprio_set 1274 #define __NR_ioprio_get 1275 #else #error "Unsupported arch" #endif return syscall(__NR_ioprio_set, which, who, ioprio); } void syslog_with_tid(int priority, const char *format, ...) { va_list ap; if (SYSLOG) { char fmt[255]; snprintf(fmt, sizeof(fmt), "[%d] %s", getpid(), format); va_start(ap, format); vsyslog(priority, fmt, ap); va_end(ap); } else { char fmt[255]; snprintf(fmt, sizeof(fmt), "markad: [%d] %s", getpid(), format); va_start(ap, format); vprintf(fmt,ap); va_end(ap); printf("\n"); fflush(stdout); } } cOSDMessage::cOSDMessage(const char *Host, int Port) { tid=0; msg=NULL; host=strdup(Host); port=Port; } cOSDMessage::~cOSDMessage() { if (tid) pthread_join(tid,NULL); if (msg) free(msg); if (host) free((void*) host); } bool cOSDMessage::readreply(int fd) { usleep(400000); char c=' '; do { struct pollfd fds; fds.fd=fd; fds.events=POLLIN; fds.revents=0; int ret=poll(&fds,1,600); if (ret<=0) return false; if (fds.revents!=POLLIN) return false; if (read(fd,&c,1)<0) return false; } while (c!='\n'); return true; } void *cOSDMessage::send(void *posd) { cOSDMessage *osd=(cOSDMessage *) posd; struct hostent *host=gethostbyname(osd->host); if (!host) { osd->tid=0; return NULL; } struct sockaddr_in name; name.sin_family = AF_INET; name.sin_port = htons(osd->port); memcpy(&name.sin_addr.s_addr,host->h_addr,sizeof(host->h_addr)); uint size = sizeof(name); int sock; sock=socket(PF_INET, SOCK_STREAM, 0); if (sock<0) return NULL; if (connect(sock, (struct sockaddr *)&name,size)!=0) { close(sock); return NULL; } if (!osd->readreply(sock)) { close(sock); return NULL; } ssize_t ret; ret=write(sock,"MESG ",5); ret=write(sock,osd->msg,strlen(osd->msg)); ret=write(sock,"\r\n",2); if (!osd->readreply(sock)) { close(sock); return NULL; } ret=write(sock,"QUIT\r\n",6); osd->readreply(sock); close(sock); return NULL; } int cOSDMessage::Send(const char *format, ...) { if (tid) pthread_join(tid,NULL); if (msg) free(msg); va_list ap; va_start(ap, format); if (vasprintf(&msg,format,ap)==-1) return -1; va_end(ap); if (pthread_create(&tid,NULL,(void *(*) (void *))&send, (void *) this)!=0) return -1; return 0; } void cMarkAdStandalone::CalculateStopPosition(int startframe, int delta) { if (!length) return; if (!macontext.Video.Info.FramesPerSecond) return; int len_in_frames=length*macontext.Video.Info.FramesPerSecond; if (startframe) { iStop=-(startframe+len_in_frames); chkLEFT=startframe+delta+macontext.Video.Info.FramesPerSecond; chkRIGHT=startframe+(len_in_frames*2/3); } else { chkLEFT=(int) (len_in_frames*2/5); chkRIGHT=(int) (len_in_frames*2/3); } } void cMarkAdStandalone::AddStartMark() { if (tStart<2) { char *buf; if (asprintf(&buf,"start of recording (0)")!=-1) { marks.Add(MT_COMMONSTART,0,buf); isyslog("%s",buf); free(buf); } } if (tStart) { iStart=-(tStart*macontext.Video.Info.FramesPerSecond); } CalculateStopPosition(-iStart,macontext.Video.Info.FramesPerSecond*MAXRANGE); if (tStart==1) { tStart=iStart=0; } } void cMarkAdStandalone::CheckStartStop(int frame, bool checkend) { MarkAdMark mark; char *buf; if ((iStartCheck>0) && (frame>=iStartCheck)) { // check if we have a start mark which is near iStart clMark *before_iStart=marks.GetPrev(iStart,MT_START,0xF); clMark *after_iStart=marks.GetNext(iStart,MT_START,0xF); int MAXMARKDIFF=(int) (macontext.Video.Info.FramesPerSecond*MAXRANGE); int type=0; int newpos=0; int delta_before=MAXMARKDIFF; int delta_after=MAXMARKDIFF; if (before_iStart) { delta_before=abs(iStart-before_iStart->position); if (delta_before>MAXMARKDIFF) delta_before=MAXMARKDIFF; } if (after_iStart) { delta_after=abs(after_iStart->position-iStart); if (delta_after>MAXMARKDIFF) delta_after=MAXMARKDIFF; } if (delta_before>delta_after) { // use after_iStart newpos=after_iStart->position; type=after_iStart->type; } if (delta_beforeposition; type=before_iStart->type; } if (newpos) { if (type==MT_BORDERSTART) { // check if we have an MT_ASPECTCHANGE in low distance clMark *aspectmark=marks.GetPrev(newpos,MT_ASPECTSTART); if (!aspectmark) aspectmark=marks.GetNext(newpos,MT_ASPECTSTART); if (aspectmark) { int MAXMARKDIFF=(int) (macontext.Video.Info.FramesPerSecond*20); if (abs(newpos-aspectmark->position)<=MAXMARKDIFF) { newpos=aspectmark->position; } } } isyslog("using this mark instead of assumed start mark (%i->%i)", newpos,iStart); marks.Del(iStart); marks.DelTill(newpos); CalculateStopPosition(newpos,frame-newpos); } iStart=0; iStartCheck=0; } if ((iStopCheck>0) && ((frame>=iStopCheck) || (checkend))) { // check if we have a stop mark which is near iStop clMark *before_iStop=marks.GetPrev(iStop,MT_STOP,0xF); clMark *after_iStop=marks.GetNext(iStop,MT_STOP,0xF); int MAXMARKDIFF=(int) (macontext.Video.Info.FramesPerSecond*MAXRANGE); int newpos=0; int delta_before=MAXMARKDIFF; int delta_after=MAXMARKDIFF; if (before_iStop) { delta_before=abs(iStop-before_iStop->position); if (delta_before>MAXMARKDIFF) delta_before=MAXMARKDIFF; } if (after_iStop) { delta_after=abs(after_iStop->position-iStop); if (delta_after>MAXMARKDIFF) delta_after=MAXMARKDIFF; } if (delta_before>delta_after) { // use after_iStop newpos=after_iStop->position; } if (delta_beforeposition; } if (newpos) { isyslog("using this mark instead of assumed stop mark (%i->%i)", newpos,iStop); marks.Del(iStop); marks.DelTill(newpos,false); } else { marks.DelTill(iStop,false); } bDecodeVideo=false; gotendmark=true; iStop=0; iStopCheck=0; } if ((iStart<0) && (frame>-iStart)) { iStart=frame; if (asprintf(&buf,"assumed start of broadcast (%i)",iStart)!=-1) { mark.Type=MT_ASSUMEDSTART; mark.Position=iStart; mark.Comment=buf; AddMark(&mark); free(buf); } int MARKDIFF=(int) (macontext.Video.Info.FramesPerSecond*MAXRANGE); #if 0 clMark *before_iStart=marks.GetPrev(iStart,MT_START,0xF); if (before_iStart) { int tmpdiff=abs(iStart-before_iStart->position); if (tmpdiff-iStop)) { iStop=frame; if (asprintf(&buf,"assumed stop of broadcast (%i)",iStop)!=-1) { mark.Type=MT_ASSUMEDSTOP; mark.Position=iStop; mark.Comment=buf; AddMark(&mark); free(buf); } int MARKDIFF=(int) (macontext.Video.Info.FramesPerSecond*MAXRANGE); clMark *before_iStop=marks.GetPrev(iStart,MT_STOP,0xF); if (before_iStop) { int tmpdiff=abs(iStop-before_iStop->position); if (tmpdifftype==MT_LOGOSTOP) && mark->Next() && mark->Next()->type==MT_LOGOSTART) { int MARKDIFF=(int) (macontext.Video.Info.FramesPerSecond*55); if (abs(mark->Next()->position-mark->position)<=MARKDIFF) { double distance=(mark->Next()->position-mark->position)/ macontext.Video.Info.FramesPerSecond; isyslog("logo distance too short (%.1fs), deleting (%i,%i)",distance, mark->position,mark->Next()->position); clMark *tmp=mark; mark=mark->Next()->Next(); marks.Del(tmp->Next()); marks.Del(tmp); continue; } } mark=mark->Next(); if ((last) && (mark==last)) return; } } void cMarkAdStandalone::CheckLastMark() { if (marks.Count()<=2) return; // just two marks -> do nothing clMark *last=marks.GetLast(); if (!last) return; clMark *nexttolast=last->Prev(); if (!nexttolast) return; if ((last->type & 0xF)==(nexttolast->type & 0xF)) { isyslog("removing double stop mark (%i)",last->position); marks.Del(last); return; } } void cMarkAdStandalone::CheckFirstMark() { if (marksAligned) return; clMark *first=marks.GetFirst(); if (!first) return; // Check the second mark clMark *second=first->Next(); if (!second) return; if ((marks.Count(MT_BORDERCHANGE,0xF0)>0) && (marks.Count(MT_ASPECTCHANGE,0xF0)>0)) { // wait till its clear, if we use ASPECT or BORDER return; } if ((second->type & 0xF)==MT_START) { bool delsec=false; clMark *third=second->Next(); if (third) { if ((third->type & 0xF)==MT_START) delsec=true; } if (delsec) { isyslog("removing double start marks (%i,%i)",first->position,second->position); marks.Del(second); } else { isyslog("removing double start mark (%i)",first->position); } marks.Del(first); marksAligned=true; first=marks.GetFirst(); if (first) { CalculateStopPosition(first->position,macontext.Video.Info.FramesPerSecond*MAXRANGE); } return; } if ((second->type & 0xF)==MT_STOP) { marksAligned=true; return; } // If we have an aspectchange, check the next aspectchange mark // and the difference between if ((second->type==MT_ASPECTCHANGE) && (length)) { clMark *next=marks.GetNext(second->position,MT_ASPECTCHANGE); if (next) { int maxlen=length*(13*60)/(90*60); // max 13 minutes ads on 90 minutes program if (maxlen>(13*60)) maxlen=(13*60); // maximum ad block = 13 minutes int MAXPOSDIFF=(int) (macontext.Video.Info.FramesPerSecond*maxlen); if ((next->position-second->position)>MAXPOSDIFF) { clMark *first=marks.GetFirst(); if (first) { isyslog("removing unaligned start mark (%i)",first->position); marks.Del(first); marksAligned=true; return; } } } } return; } void cMarkAdStandalone::CheckAspectRatio_and_AudioChannels() { if (aspectChecked) return; dsyslog("checking aspectratio and audio channels"); if (!macontext.Info.Channels) { macontext.Info.Channels=macontext.Audio.Info.Channels; if (macontext.Info.Channels==2) setAudio20=true; if (macontext.Info.Channels==6) { isyslog("DolbyDigital5.1 audio detected. logo/border detection disabled"); bDecodeVideo=false; setAudio20=false; setAudio51=true; reprocess=true; } } bool aSet=false; if (!macontext.Info.AspectRatio.Num) { isyslog("assuming aspectratio of %i:%i", macontext.Video.Info.AspectRatio.Num,macontext.Video.Info.AspectRatio.Den); aSet=true; } else { if (!bIgnoreVideoInfo) { if ((macontext.Info.AspectRatio.Num!=macontext.Video.Info.AspectRatio.Num) && (macontext.Info.AspectRatio.Den!=macontext.Video.Info.AspectRatio.Den)) { isyslog("aspectratio in info wrong %i:%i instead of %i:%i", macontext.Video.Info.AspectRatio.Num,macontext.Video.Info.AspectRatio.Den, macontext.Info.AspectRatio.Num,macontext.Info.AspectRatio.Den); aSet=true; } } } if (aSet) { macontext.Info.AspectRatio.Num=macontext.Video.Info.AspectRatio.Num; macontext.Info.AspectRatio.Den=macontext.Video.Info.AspectRatio.Den; if ((macontext.Info.AspectRatio.Num==16) && (macontext.Info.AspectRatio.Den==9)) { macontext.Video.Options.IgnoreAspectRatio=true; setVideo169=true; setVideo43=false; setVideo43LB=false; } if ((macontext.Info.AspectRatio.Num==4) && (macontext.Info.AspectRatio.Den==3)) { setVideo43=true; setVideo169=false; } reprocess=true; } aspectChecked=true; return; } void cMarkAdStandalone::AddMark(MarkAdMark *Mark) { if (gotendmark) return; if (!Mark) return; if (!Mark->Type) return; if (!macontext.Video.Info.FramesPerSecond) return; bool loggedAlready=false; clMark *old=marks.Get(Mark->Position); if ((old) && (((old->type & 0xF0)==MT_ASPECTCHANGE) || ((old->type & 0xF0)==MT_CHANNELCHANGE))) { // Aspect- / Channelchange wins over Logo/Border return; } if (Mark->Type==MT_ASPECTSTOP) { // check if last mark is an stop mark in short distance clMark *prev=marks.GetLast(); if (prev) { if ((prev->type & 0xF)==MT_STOP) { int MARKDIFF=(int) (macontext.Video.Info.FramesPerSecond*15); if ((Mark->Position-prev->position)Comment) isyslog("%s",Mark->Comment); isyslog("double stop mark in short distance, deleting this mark (%i)",prev->position); marks.Del(prev); loggedAlready=true; } } if (prev->type==MT_ASPECTSTART) { int MARKDIFF=(int) (macontext.Video.Info.FramesPerSecond*240); if ((Mark->Position-prev->position)Comment) isyslog("%s",Mark->Comment); double distance=(Mark->Position-prev->position)/macontext.Video.Info.FramesPerSecond; isyslog("aspect mark distance too short (%.1fs), deleting (%i,%i)",distance, prev->position,Mark->Position); marks.Del(prev); return; } } } } if (Mark->Type==MT_LOGOSTOP) { // check if last mark is an audiochannel stop clMark *prev=marks.GetLast(); if ((prev) && (prev->type==MT_CHANNELSTOP)) { int MARKDIFF=(int) (macontext.Video.Info.FramesPerSecond*15); if ((Mark->Position-prev->position)Comment) isyslog("%s",Mark->Comment); isyslog("audiochannel change in short distance, using this mark (%i->%i)",Mark->Position,prev->position); return; } } prev=marks.GetPrev(Mark->Position,MT_LOGOSTART); if (prev) { int MARKDIFF=(int) (macontext.Video.Info.FramesPerSecond*240); if ((Mark->Position-prev->position)Comment) isyslog("%s",Mark->Comment); double distance=(Mark->Position-prev->position)/macontext.Video.Info.FramesPerSecond; isyslog("logo distance too short (%.1fs), deleting (%i,%i)",distance, prev->position,Mark->Position); marks.Del(prev); return; } } } if (Mark->Type==MT_LOGOSTART) { // check if last mark is an aspectratio change clMark *prev=marks.GetLast(); if (prev) { if ((prev->type & 0xF0)==MT_ASPECTCHANGE) { int MARKDIFF=(int) (macontext.Video.Info.FramesPerSecond*5); if ((Mark->Position-prev->position)Comment) isyslog("%s",Mark->Comment); isyslog("aspectratio change in short distance, deleting this mark (%i)", Mark->Position); return; } } if (prev->type==MT_CHANNELSTART) { int MARKDIFF=(int) (macontext.Video.Info.FramesPerSecond*5); if ((Mark->Position-prev->position)Comment) isyslog("%s",Mark->Comment); isyslog("audiochannel change in short distance, deleting this mark (%i)", Mark->Position); return; } } } } if (length>0) { if ((Mark->Type==MT_BORDERSTART) && (Mark->Position>chkLEFT) && (Mark->PositionComment) { isyslog("%s",Mark->Comment); loggedAlready=true; } isyslog("border changes detected. logo detection disabled"); macontext.Video.Options.IgnoreLogoDetection=true; marks.Del((uchar) MT_LOGOSTART); marks.Del((uchar) MT_LOGOSTOP); } bool deleteLogoBorder=false; if (((Mark->Type & 0xF0)==MT_CHANNELCHANGE) && (Mark->Position>chkLEFT) && (Mark->PositionComment) { isyslog("%s",Mark->Comment); loggedAlready=true; } } isyslog("audio channel changes detected. logo/border detection disabled"); if (macontext.Info.VPid.Type==MARKAD_PIDTYPE_VIDEO_H262) { if (!macontext.Info.AspectRatio.Num) { isyslog("assuming broadcast aspectratio is 16:9"); macontext.Info.AspectRatio.Num=16; macontext.Info.AspectRatio.Den=9; macontext.Video.Options.IgnoreAspectRatio=true; } } macontext.Info.Channels=6; setAudio51=true; setAudio20=false; reprocess=true; deleteLogoBorder=true; } if (((Mark->Type & 0xF0)==MT_ASPECTCHANGE) && (Mark->Position>chkLEFT) && (Mark->PositionComment) { isyslog("%s",Mark->Comment); loggedAlready=true; } } isyslog("aspectratio changes detected. logo/border detection disabled"); if (!macontext.Info.AspectRatio.Num) { isyslog("assuming broadcast aspectratio is 4:3"); macontext.Info.AspectRatio.Num=4; macontext.Info.AspectRatio.Den=3; reprocess=true; setVideo43=true; } else { if ((macontext.Info.AspectRatio.Num==4) && (macontext.Info.AspectRatio.Den==3)) { if (marks.Count(MT_BORDERSTART)>0) { isyslog("assuming broadcast is in letterbox format"); setVideo43LB=true; setVideo43=false; } } } deleteLogoBorder=true; } if (deleteLogoBorder) { bDecodeVideo=false; macontext.Video.Options.IgnoreLogoDetection=true; macontext.Video.Data.Valid=false; marks.Del((uchar) MT_LOGOSTART); marks.Del((uchar) MT_LOGOSTOP); marks.Del((uchar) MT_BORDERSTART); marks.Del((uchar) MT_BORDERSTOP); } } if (Mark->Position>chkLEFT) CheckFirstMark(); if (marksAligned) CheckLogoMarks(marks.GetLast()); if ((Mark->Comment) && (!loggedAlready)) isyslog("%s",Mark->Comment); marks.Add(Mark->Type,Mark->Position,Mark->Comment); } void cMarkAdStandalone::SaveFrame(int frame) { if (!macontext.Video.Info.Width) return; if (!macontext.Video.Data.Valid) return; FILE *pFile; char szFilename[256]; // Open file sprintf(szFilename, "/tmp/frame%06d.pgm", frame); pFile=fopen(szFilename, "wb"); if (pFile==NULL) return; // Write header fprintf(pFile, "P5\n%d %d\n255\n", macontext.Video.Data.PlaneLinesize[0], macontext.Video.Info.Height); // Write pixel data if (fwrite(macontext.Video.Data.Plane[0],1, macontext.Video.Data.PlaneLinesize[0]*macontext.Video.Info.Height,pFile)) {}; // Close file fclose(pFile); } void cMarkAdStandalone::CheckBroadcastLength() { if (length) return; if (!macontext.Video.Info.FramesPerSecond) return; /* get broadcastlength from length of indexFile */ int tframecnt=1; int iIndexError; marks.CheckIndex(directory,isTS,&tframecnt,&iIndexError); if (iIndexError!=0) return; length=tframecnt/macontext.Video.Info.FramesPerSecond; isyslog("got broadcast length of %im from index",length/60); reprocess=true; } bool cMarkAdStandalone::CheckIndexGrowing() { // Here we check if the index is more // advanced than our framecounter. // If not we wait. If we wait too much, // we discard this check... #define WAITTIME 15 if (!indexFile) return false; if (sleepcnt>=2) return false; // we already slept too much bool notenough=true; do { struct stat statbuf; if (stat(indexFile,&statbuf)==-1) return false; int maxframes=statbuf.st_size/8; if (maxframes<(framecnt+200)) { if ((difftime(time(NULL),statbuf.st_mtime))>=WAITTIME) { if (length && startTime) { if (time(NULL)>(startTime+(time_t) length)) { // "old" recording return false; } else { sleepcnt=0; if (!iwaittime) esyslog("recording interrupted, waiting for continuation..."); iwaittime+=WAITTIME; } } else { // "old" recording return false; } } marks.Save(directory,macontext.Video.Info.FramesPerSecond,isTS); sleep(WAITTIME); // now we sleep and hopefully the index will grow waittime+=WAITTIME; if (errno==EINTR) return false; sleepcnt++; if (sleepcnt>=2) { esyslog("no new data after %is, skipping wait!", waittime); notenough=false; // something went wrong? } } else { if (iwaittime) { esyslog("resuming after %is of interrupted recording, marks can be wrong now!",iwaittime); } iwaittime=0; sleepcnt=0; notenough=false; } } while (notenough); if (!sleepcnt) return true; else return false; } void cMarkAdStandalone::ChangeMarks(clMark **Mark1, clMark **Mark2, MarkAdPos *NewPos) { if (!NewPos) return; if (!Mark1) return; if (!*Mark1) return; bool save=false; if ((*Mark1)->position!=NewPos->FrameNumberBefore) { marks.Del(*Mark1); *Mark1=marks.Add(MT_MOVED,NewPos->FrameNumberBefore,NewPos->CommentBefore); save=true; } if (NewPos->CommentBefore) isyslog("%s",NewPos->CommentBefore); if (Mark2 && (*Mark2) && (*Mark2)->position!=NewPos->FrameNumberAfter) { marks.Del(*Mark2); *Mark2=marks.Add(MT_MOVED,NewPos->FrameNumberAfter,NewPos->CommentAfter); if (NewPos->CommentAfter) isyslog("%s",NewPos->CommentAfter); save=true; } if (save) marks.Save(directory,macontext.Video.Info.FramesPerSecond,isTS,true); } bool cMarkAdStandalone::ProcessFile2ndPass(clMark **Mark1, clMark **Mark2,int Number, off_t Offset, int Frame, int Frames) { if (!directory) return false; if (!Number) return false; if (!Frames) return false; if (!decoder) return false; if (!Mark1) return false; if (!*Mark1) return false; int pn; // process number 1=start mark, 2=before mark, 3=after mark if (Mark1 && Mark2) { if (!(*Mark1) || !(*Mark2)) return false; if (*Mark1==*Mark2) pn=1; if (*Mark1!=*Mark2) pn=3; } else { pn=2; } if (!Reset(false)) { // reset all, but marks esyslog("failed resetting state"); return false; } iframe=Frame; int actframe=Frame; int framecounter=0; MarkAdPos *pos=NULL; while (framecounter0) { if (abort) break; if ((video_demux) && (video) && (decoder) && (streaminfo)) { uchar *tspkt = data; int tslen = dataread; while (tslen>0) { int len=video_demux->Process(macontext.Info.VPid,tspkt,tslen,&vpkt); if (len<0) { esyslog("error demuxing video"); abort=true; break; } else { if (vpkt.Data) { bool dRes=false; if (streaminfo->FindVideoInfos(&macontext,vpkt.Data,vpkt.Length)) { actframe++; framecnt2++; if (macontext.Video.Info.Pict_Type==MA_I_TYPE) { lastiframe=iframe; if (macontext.Info.VPid.Type==MARKAD_PIDTYPE_VIDEO_H264) { iframe=actframe; } else { iframe=actframe-1; } dRes=true; } } if (pn>1) dRes=decoder->DecodeVideo(&macontext,vpkt.Data,vpkt.Length); if (dRes) { if ((actframe-iframe)<=3) { if (pn>1) pos=video->Process2ndPass(lastiframe,Frames,(pn==2)); //SaveFrame(lastiframe); framecounter++; } if ((pos) && (pn==3)) { // found overlap ChangeMarks(Mark1,Mark2,pos); close(f); return true; } } } tspkt+=len; tslen-=len; if (!vpkt.Offcnt) { offset_add+=len; } else { offset+=offset_add; offset+=len; offset_add=0; } } } } #if 0 if ((mp2_demux) && (audio) && (pn!=3)) { uchar *tspkt = data; int tslen = dataread; while (tslen>0) { int len=mp2_demux->Process(macontext.Info.APid,tspkt,tslen,&apkt); if (len<0) { esyslog("error demuxing mp2-audio"); break; } else { if (apkt.Data) { if (apkt.Timestamp) audiotime=apkt.Timestamp; if (abs(audiotime-lastiframetime)DecodeMP2(&macontext,apkt.Data,apkt.length)) { pos=audio->Process2ndPass(iframe); if (pos) ChangeMarks(Mark1,NULL,pos); } } } tspkt+=len; tslen-=len; } } } #endif if (abort) { close(f); return false; } if (framecounter>Frames) { break; } } close(f); Number++; Offset=0; } return true; } void cMarkAdStandalone::Process2ndPass() { if (abort) return; if (duplicate) return; if (!decoder) return; if (!length) return; if (!startTime) return; if (time(NULL)<(startTime+(time_t) length)) return; if (!macontext.Video.Info.FramesPerSecond) { isyslog("WARNING: assuming fps of 25"); macontext.Video.Info.FramesPerSecond=25; } if (!marks.Count()) { marks.Load(directory,macontext.Video.Info.FramesPerSecond,isTS); } bool infoheader=false; clMark *p1=NULL,*p2=NULL; #if 0 p1=marks.GetFirst(); if (!p1) return; if (p1->position>0) { isyslog("2nd pass"); infoheader=true; off_t offset; int number,frame,iframes; int frange=macontext.Video.Info.FramesPerSecond*120; if (marks.ReadIndex(directory,isTS,p1->position-(frange/10),frange,&number,&offset,&frame,&iframes)) { ProcessFile2ndPass(&p1,&p1,number,offset,frame,iframes); } } #endif if (marks.Count()<4) return; // here we cannot do much p1=marks.GetFirst(); if (!p1) return; p1=p1->Next(); if (p1) p2=p1->Next(); while ((p1) && (p2)) { if (!infoheader) { isyslog("2nd pass"); infoheader=true; } off_t offset; int number,frame,iframes; int frange=macontext.Video.Info.FramesPerSecond*120; // 40s + 80s if (marks.ReadIndex(directory,isTS,p1->position-frange,frange,&number,&offset,&frame,&iframes)) { if (!ProcessFile2ndPass(&p1,NULL,number,offset,frame,iframes)) break; frange=macontext.Video.Info.FramesPerSecond*320; // 160s + 160s if (marks.ReadIndex(directory,isTS,p2->position,frange,&number,&offset,&frame,&iframes)) { if (!ProcessFile2ndPass(&p1,&p2,number,offset,frame,iframes)) break; } } p1=p2->Next(); if (p1) { p2=p1->Next(); } else { p2=NULL; } } } bool cMarkAdStandalone::ProcessFile(int Number) { if (!directory) return false; if (!Number) return false; if (!CheckIndexGrowing()) CheckBroadcastLength(); if (abort) return false; const int datalen=319976; uchar data[datalen]; char *fbuf; if (isTS) { if (asprintf(&fbuf,"%s/%05i.ts",directory,Number)==-1) return false; } else { if (asprintf(&fbuf,"%s/%03i.vdr",directory,Number)==-1) return false; } int f=open(fbuf,O_RDONLY); free(fbuf); if (f==-1) return false; int dataread; dsyslog("processing file %05i",Number); uint64_t offset=0; int offset_add=0; while ((dataread=read(f,data,datalen))>0) { if (abort) break; if ((video_demux) && (video) && (streaminfo)) { uchar *tspkt = data; int tslen = dataread; while (tslen>0) { int len=video_demux->Process(macontext.Info.VPid,tspkt,tslen,&vpkt); if (len<0) { esyslog("error demuxing video"); abort=true; break; } else { if (vpkt.Data) { bool dRes=false; if (streaminfo->FindVideoInfos(&macontext,vpkt.Data,vpkt.Length)) { if ((macontext.Video.Info.Height) && (!noticeHEADER)) { isyslog("%s %i%c%0.f",(macontext.Video.Info.Height>576) ? "HDTV" : "SDTV", macontext.Video.Info.Height, macontext.Video.Info.Interlaced ? 'i' : 'p', macontext.Video.Info.FramesPerSecond); noticeHEADER=true; } if (!framecnt) { AddStartMark(); } if (macontext.Config->GenIndex) { marks.WriteIndex(directory,isTS,offset,macontext.Video.Info.Pict_Type,Number); } framecnt++; if (macontext.Video.Info.Pict_Type==MA_I_TYPE) { lastiframe=iframe; CheckStartStop(lastiframe); if (lastiframe>chkLEFT) CheckAspectRatio_and_AudioChannels(); if (macontext.Info.VPid.Type==MARKAD_PIDTYPE_VIDEO_H264) { iframe=framecnt; } else { iframe=framecnt-1; } dRes=true; } } if (macontext.Video.Info.FramesPerSecond<0) { macontext.Video.Info.FramesPerSecond*=-1; if (!setFrameRate) isyslog("framerate wrong or unset, using %.f",macontext.Video.Info.FramesPerSecond); setFrameRate=true; } if ((decoder) && (bDecodeVideo)) dRes=decoder->DecodeVideo(&macontext,vpkt.Data,vpkt.Length); if (dRes) { if ((framecnt-iframe)<=3) { MarkAdMarks *vmarks; vmarks=video->Process(lastiframe,iframe); if (vmarks) { for (int i=0; iCount; i++) { AddMark(&vmarks->Number[i]); } } //SaveFrame(lastiframe); // TODO: JUST FOR DEBUGGING! } } } tspkt+=len; tslen-=len; if (!vpkt.Offcnt) { offset_add+=len; } else { offset+=offset_add; offset+=len; offset_add=0; } } } } if ((ac3_demux) && (streaminfo) && (audio)) { uchar *tspkt = data; int tslen = dataread; while (tslen>0) { int len=ac3_demux->Process(macontext.Info.DPid,tspkt,tslen,&apkt); if (len<0) { esyslog("error demuxing ac3-audio"); break; } else { if (apkt.Data) { if (streaminfo->FindAC3AudioInfos(&macontext,apkt.Data,apkt.Length)) { if ((!isTS) && (!noticeVDR_AC3)) { isyslog("found AC3%s",macontext.Config->AC3Always ? "*" : ""); noticeVDR_AC3=true; } MarkAdMark *amark; amark=audio->Process(lastiframe,iframe); if (amark) AddMark(amark); } } tspkt+=len; tslen-=len; } } } if (((gotendmark) && (!macontext.Config->GenIndex)) || (reprocess)) { if (f!=-1) close(f); return true; } if (!CheckIndexGrowing()) CheckBroadcastLength(); if (abort) { if (f!=-1) close(f); return false; } } close(f); return true; } bool cMarkAdStandalone::Reset(bool FirstPass) { bool ret=true; reprocess=false; if (FirstPass) framecnt=0; lastiframe=0; iframe=0; gotendmark=false; memset(&vpkt,0,sizeof(vpkt)); memset(&apkt,0,sizeof(apkt)); iStart=iStartCheck=iStop=iStopCheck=0; chkLEFT=INT_MAX; chkRIGHT=INT_MIN; if (FirstPass) { marksAligned=false; marks.DelAll(); marks.CloseIndex(directory,isTS); } macontext.Video.Info.Pict_Type=0; macontext.Video.Info.AspectRatio.Den=0; macontext.Video.Info.AspectRatio.Num=0; macontext.Audio.Info.Channels=0; if (decoder) { ret=decoder->Clear(); } if (streaminfo) streaminfo->Clear(); if (video_demux) video_demux->Clear(); if (ac3_demux) ac3_demux->Clear(); if (mp2_demux) mp2_demux->Clear(); if (video) video->Clear(); if (audio) audio->Clear(); return ret; } bool cMarkAdStandalone::CheckDolbyDigital51() { if (macontext.Info.Channels!=6) return false; if (abort) return false; // Assumption: last mark must be MT_CHANNELSTOP and the position must be // beyond the half of the broadcast length (or?) clMark *mark=marks.GetLast(); if (!mark) return false; // no last mark? there is a problem! mark=marks.GetPrev(mark->position,MT_CHANNELSTART); if (mark) { if (mark->position>chkLEFT) return false; } reprocess=true; bDecodeVideo=macontext.Config->DecodeVideo; setAudio20=true; setAudio51=false; macontext.Info.Channels=2; isyslog("%s DolbyDigital5.1 marks found", mark ? "not enough" : "no"); isyslog("restarting from scratch"); if ((ac3_demux) && (!macontext.Config->AC3Always)) { delete ac3_demux; ac3_demux=NULL; macontext.Info.DPid.Num=0; } Reset(); return true; } void cMarkAdStandalone::ProcessFile() { for (int i=1; i<=MaxFiles; i++) { if (abort) break; if (!ProcessFile(i)) break; if ((gotendmark) && (!macontext.Config->GenIndex)) break; if (reprocess) { isyslog("restarting from scratch"); i=0; Reset(); } } if (!abort) { if (lastiframe) { CheckStartStop(lastiframe,true); if ((!gotendmark) && ((iStop<0) || (!tStart))) { char *buf; MarkAdMark tempmark; tempmark.Type=MT_COMMONSTOP; tempmark.Position=lastiframe; if (asprintf(&buf,"stop of recording (%i)",lastiframe)!=-1) { tempmark.Comment=buf; AddMark(&tempmark); free(buf); } } } CheckLastMark(); CheckLogoMarks(); } } void cMarkAdStandalone::Process() { if (abort) return; if (macontext.Config->BackupMarks) marks.Backup(directory,isTS); ProcessFile(); if (CheckDolbyDigital51()) ProcessFile(); marks.CloseIndex(directory,isTS); if (!abort) { if (marks.Save(directory,macontext.Video.Info.FramesPerSecond,isTS)) { if (length && startTime) { if (time(NULL)>(startTime+(time_t) length)) { int iIndexError=false; int tframecnt=macontext.Config->GenIndex ? framecnt : 0; if (marks.CheckIndex(directory,isTS,&tframecnt,&iIndexError)) { if (iIndexError) { if (macontext.Config->GenIndex) { switch (iIndexError) { case IERR_NOTFOUND: isyslog("no index found"); break; case IERR_TOOSHORT: isyslog("index too short"); break; default: isyslog("index contains errors"); break; } if (RegenerateIndex()) { isyslog("recreated index"); } else { esyslog("failed to recreate index"); } } else { esyslog("index doesn't match marks%s", ((isTS) || ((macontext.Info.VPid.Type== MARKAD_PIDTYPE_VIDEO_H264) && (!isTS))) ? ", sorry you're lost" : ", please run genindex"); } } } if ((!bIgnoreAudioInfo) && (!bIgnoreVideoInfo)) SaveInfo(); } else { // this shouldn't be reached esyslog("ALERT: stopping before end of broadcast"); } } } } if (macontext.Config->GenIndex) marks.RemoveGeneratedIndex(directory,isTS); } bool cMarkAdStandalone::SaveInfo() { if ((!setVideo43) && (!setVideo169) && (!setAudio20) && (!setAudio51) && (!setVideo43LB) && (!setFrameRate)) return true; char *src,*dst; if (asprintf(&src,"%s/info%s",directory,isTS ? "" : ".vdr")==-1) return false; if (asprintf(&dst,"%s/info.bak",directory)==-1) { free(src); return false; } FILE *r,*w; r=fopen(src,"r"); w=fopen(dst,"w+"); if ((!r) || (!w)) { free(src); free(dst); return false; } char *line=NULL; char *lline=NULL; size_t length=0; bool setVideo43LB_done=false; bool setVideo43_done=false; bool setVideo169_done=false; bool setAudio20_done=false; bool setAudio51_done=false; bool setFrameRate_done=false; char lang[4]=""; bool err=false; while (getline(&line,&length,r)!=-1) { if ((line[0]=='F') && (setFrameRate)) { if (fprintf(w,"F %i\n",(int) macontext.Video.Info.FramesPerSecond)<=0) err=true; setFrameRate_done=true; } else if (line[0]=='X') { int stream=0,type=0; char descr[256]=""; int result=sscanf(line,"%*c %i %i %3c %250c",&stream,&type,(char *) &lang, (char *) &descr); if ((result!=0) && (result!=EOF)) { switch (stream) { case 1: if ( (((type==1) || (type==5)) && ((setVideo169) || (setVideo43LB))) || (((type==2) || (type==3) || (type==6) || (type==7)) && ((setVideo43) || (setVideo43LB)))) { if (setVideo43) { if (fprintf(w,"X 1 01 %s 4:3\n",lang)<=0) err=true; setVideo43_done=true; } if (setVideo43LB) { if (fprintf(w,"X 1 01 %s 4:3 LetterBox\n",lang)<=0) err=true; setVideo43LB_done=true; } if (setVideo169) { if (fprintf(w,"X 1 03 %s 16:9\n",lang)<=0) err=true; setVideo169_done=true; } } else { if (fprintf(w,"%s",line)<=0) err=true; } break; case 2: if ((type==5) && ((setAudio51) || (setAudio20))) { if (setAudio51) { if (fprintf(w,"X 2 05 %s Dolby Digital 5.1\n",lang)<=0) err=true; setAudio51_done=true; } if (setAudio20) { if (fprintf(w,"X 2 05 %s Dolby Digital 2.0\n",lang)<=0) err=true; setAudio20_done=true; } } else { if (fprintf(w,"%s",line)<=0) err=true; } break; default: if (fprintf(w,"%s",line)<=0) err=true; break; } } } else { if (line[0]!='@') { if (fprintf(w,"%s",line)<=0) err=true; } else { if (lline) { free(lline); err=true; esyslog("multiple @lines in info file, please report this!"); } lline=strdup(line); } } if (err) break; } if (line) free(line); line=lline; if (lang[0]==0) strcpy(lang,"und"); if ((setVideo43LB) && (!setVideo43LB_done) && (!err)) { if (fprintf(w,"X 1 01 %s 4:3 LetterBox\n",lang)<=0) err=true; } if ((setVideo43) && (!setVideo43_done) && (!err)) { if (fprintf(w,"X 1 01 %s 4:3\n",lang)<=0) err=true; } if ((setVideo169) && (!setVideo169_done) && (!err)) { if (fprintf(w,"X 1 03 %s 16:9\n",lang)<=0) err=true; } if ((setAudio20) && (!setAudio20_done) && (!err)) { if (fprintf(w,"X 2 05 %s Dolby Digital 2.0\n",lang)<=0) err=true; } if ((setAudio51) && (!setAudio51_done) && (!err)) { if (fprintf(w,"X 2 05 %s Dolby Digital 5.1\n",lang)<=0) err=true; } if ((setFrameRate) && (!setFrameRate_done) && (!err)) { if (fprintf(w,"F %i\n",(int) macontext.Video.Info.FramesPerSecond)<=0) err=true; } if (line) { if (fprintf(w,"%s",line)<=0) err=true; free(line); } fclose(w); struct stat statbuf_r; if (fstat(fileno(r),&statbuf_r)==-1) err=true; fclose(r); if (err) { unlink(dst); } else { if (rename(dst,src)==-1) { err=true; } else { // preserve timestamps from old file struct utimbuf oldtimes; oldtimes.actime=statbuf_r.st_atime; oldtimes.modtime=statbuf_r.st_mtime; if (utime(src,&oldtimes)) {}; } } if ((getuid()==0 || geteuid()!=0) && (!err)) { // if we are root, set fileowner to owner of 001.vdr/00001.ts file char *spath=NULL; if (asprintf(&spath,"%s/%s",directory,isTS ? "00001.ts" : "001.vdr")!=-1) { struct stat statbuf; if (!stat(spath,&statbuf)) { if (chown(src,statbuf.st_uid, statbuf.st_gid)) {}; } free(spath); } } free(src); free(dst); return (err==false); } time_t cMarkAdStandalone::GetBroadcastStartPES() { // get broadcast start from atime of directory (if the volume is mounted with noatime) // fallback to the time of the directory struct mntent *ent; FILE *mounts=setmntent(_PATH_MOUNTED,"r"); while ((ent=getmntent(mounts))!=NULL) { if (strstr(directory,ent->mnt_dir)) { if (strstr(ent->mnt_opts,"noatime")) { struct stat statbuf; if (stat(directory,&statbuf)!=-1) { endmntent(mounts); isyslog("getting broadcast start from directory atime"); return statbuf.st_atime; } } } } endmntent(mounts); const char *timestr=strrchr(directory,'/'); if (timestr) { timestr++; if (isdigit(*timestr)) { time_t now = time(NULL); struct tm tm_r; struct tm t = *localtime_r(&now, &tm_r); // init timezone if (sscanf(timestr, "%4d-%02d-%02d.%02d%*c%02d", &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, & t.tm_min)==5) { t.tm_year-=1900; t.tm_mon--; t.tm_sec=0; t.tm_isdst=-1; isyslog("getting broadcast start from timer"); return mktime(&t); } } } return (time_t) 0; } time_t cMarkAdStandalone::GetBroadcastStart(time_t start, int fd) { if (isTS) { } else { /* with PES we can get the exact starttime only from info.vdr (if its not changed afterwards) */ struct stat statbuf; if (fstat(fd,&statbuf)==-1) return (time_t) 0; if (fabs(difftime(start,statbuf.st_mtime))>14400) return (time_t) 0; return statbuf.st_mtime; } return (time_t) 0; } bool cMarkAdStandalone::LoadInfo() { char *buf; if (asprintf(&buf,"%s/info%s",directory,isTS ? "" : ".vdr")==-1) return false; FILE *f; f=fopen(buf,"r"); free(buf); if (!f) return false; char *line=NULL; size_t linelen; setFrameRate=true; while (getline(&line,&linelen,f)!=-1) { if (line[0]=='C') { char channelname[256]=""; int result=sscanf(line,"%*c %*s %250c",(char *) &channelname); if (result==1) { macontext.Info.ChannelName=strdup(channelname); char *lf=strchr(macontext.Info.ChannelName,10); if (lf) *lf=0; char *cr=strchr(macontext.Info.ChannelName,13); if (cr) *cr=0; for (int i=0; i<(int) strlen(macontext.Info.ChannelName); i++) { if (macontext.Info.ChannelName[i]==' ') macontext.Info.ChannelName[i]='_'; if (macontext.Info.ChannelName[i]=='.') macontext.Info.ChannelName[i]='_'; } } } if (line[0]=='E') { int result=sscanf(line,"%*c %*i %li %i %*i %*x",&startTime,&length); if (result!=2) { startTime=0; length=0; } } if (line[0]=='T') { int result=sscanf(line,"%*c %79c",title); if ((result==0) || (result==EOF)) { title[0]=0; } else { char *lf=strchr(title,10); if (lf) *lf=0; char *cr=strchr(title,13); if (cr) *cr=0; } } if (line[0]=='F') { int fps; int result=sscanf(line,"%*c %i",&fps); if ((result==0) || (result==EOF)) { macontext.Video.Info.FramesPerSecond=0; } else { macontext.Video.Info.FramesPerSecond=fps; setFrameRate=false; } } if (line[0]=='X') { int stream=0,type=0; char descr[256]=""; int result=sscanf(line,"%*c %i %i %250c",&stream,&type,(char *) &descr); if ((result!=0) && (result!=EOF)) { if ((stream==1) && (!bIgnoreVideoInfo)) { if ((type!=1) && (type!=5)) { isyslog("broadcast aspectratio 16:9 (from info)"); macontext.Info.AspectRatio.Num=16; macontext.Info.AspectRatio.Den=9; } else { isyslog("broadcast aspectratio 4:3 (from info)"); macontext.Info.AspectRatio.Num=4; macontext.Info.AspectRatio.Den=3; } } if ((stream==2) && (!bIgnoreAudioInfo)) { if (type==5) { // if we have DolbyDigital 2.0 disable AC3 if (strchr(descr,'2')) { isyslog("broadcast with DolbyDigital2.0%s",macontext.Config->AC3Always ? "" : ", disabling AC3 decoding"); if (!macontext.Config->AC3Always) { macontext.Info.DPid.Num=0; macontext.Info.Channels=2; } } else // if we have DolbyDigital 5.1 disable video decoding if (strchr(descr,'5')) { bDecodeVideo=false; macontext.Info.Channels=6; macontext.Video.Options.IgnoreAspectRatio=true; isyslog("broadcast with DolbyDigital5.1, disabling video decoding"); if (macontext.Info.VPid.Type==MARKAD_PIDTYPE_VIDEO_H262) { if ((macontext.Info.AspectRatio.Num==4) && (macontext.Info.AspectRatio.Den==3)) { isyslog("wrong aspectratio in info, changing to 16:9"); macontext.Info.AspectRatio.Num=16; macontext.Info.AspectRatio.Den=9; setVideo169=true; } } } else { setAudio20=true; } } } } } } if (line) free(line); if ((length) && (!bIgnoreTimerInfo) && (startTime)) { time_t rStart=GetBroadcastStart(startTime,fileno(f)); if (rStart) { if (rStart>startTime+(time_t) length) { rStart=GetBroadcastStartPES(); } tStart=(int) (startTime-rStart); if (tStart<0) { if (length+tStart>0) { isyslog("broadcast start truncated by %im, length will be corrected",-tStart/60); startTime=rStart; length+=tStart; tStart=1; } else { esyslog("cannot determine broadcast start, disabling start/stop detection"); tStart=0; } } } else { tStart=0; } } else { tStart=0; } fclose(f); if (!length) { esyslog("cannot read broadcast length from info, marks can be wrong!"); macontext.Info.AspectRatio.Num=0; macontext.Info.AspectRatio.Den=0; bIgnoreAudioInfo=true; bIgnoreVideoInfo=true; bDecodeVideo=macontext.Config->DecodeVideo; macontext.Video.Options.IgnoreAspectRatio=false; } if (!macontext.Info.ChannelName) { return false; } else { return true; } } bool cMarkAdStandalone::CheckTS() { MaxFiles=0; isTS=false; if (!directory) return false; char *buf; if (asprintf(&buf,"%s/00001.ts",directory)==-1) return false; struct stat statbuf; if (stat(buf,&statbuf)==-1) { if (errno!=ENOENT) { free(buf); return false; } free(buf); if (asprintf(&buf,"%s/001.vdr",directory)==-1) return false; if (stat(buf,&statbuf)==-1) { free(buf); return false; } free(buf); // .VDR detected isTS=false; MaxFiles=999; return true; } free(buf); // .TS detected isTS=true; MaxFiles=65535; return true; } bool cMarkAdStandalone::CheckVDRHD() { char *buf; if (asprintf(&buf,"%s/001.vdr",directory)==-1) return false; int fd=open(buf,O_RDONLY); free(buf); if (fd==-1) return false; uchar pes_buf[32]; if (read(fd,pes_buf,sizeof(pes_buf))!=sizeof(pes_buf)) { close(fd); return false; } close(fd); if ((pes_buf[0]==0) && (pes_buf[1]==0) && (pes_buf[2]==1) && ((pes_buf[3] & 0xF0)==0xE0)) { int payloadstart=9+pes_buf[8]; if (payloadstart>23) return false; uchar *start=&pes_buf[payloadstart]; if ((start[0]==0) && (start[1]==0) && (start[2]==1) && (start[5]==0) && (start[6]==0) && (start[7]==0) && (start[8]==1)) { return true; } } return false; } bool cMarkAdStandalone::CheckPATPMT() { char *buf; if (asprintf(&buf,"%s/00001.ts",directory)==-1) return false; int fd=open(buf,O_RDONLY); free(buf); if (fd==-1) return false; uchar patpmt_buf[564]; uchar *patpmt; if (read(fd,patpmt_buf,sizeof(patpmt_buf))!=sizeof(patpmt_buf)) { close(fd); return false; } close(fd); patpmt=patpmt_buf; if ((patpmt[0]==0x47) && ((patpmt[1] & 0x5F)==0x40) && (patpmt[2]==0x11) && ((patpmt[3] & 0x10)==0x10)) patpmt+=188; // skip SDT // some checks if ((patpmt[0]!=0x47) || (patpmt[188]!=0x47)) return false; // no TS-Sync if (((patpmt[1] & 0x5F)!=0x40) && (patpmt[2]!=0)) return false; // no PAT if ((patpmt[3] & 0x10)!=0x10) return false; // PAT not without AFC if ((patpmt[191] & 0x10)!=0x10) return false; // PMT not without AFC struct PAT *pat = (struct PAT *) &patpmt[5]; // more checks if (pat->reserved1!=3) return false; // is always 11 if (pat->reserved3!=7) return false; // is always 111 int pid=pat->pid_L+(pat->pid_H<<8); int pmtpid=((patpmt[189] & 0x1f)<<8)+patpmt[190]; if (pid!=pmtpid) return false; // pid in PAT differs from pid in PMT struct PMT *pmt = (struct PMT *) &patpmt[193]; // still more checks if (pmt->reserved1!=3) return false; // is always 11 if (pmt->reserved2!=3) return false; // is always 11 if (pmt->reserved3!=7) return false; // is always 111 if (pmt->reserved4!=15) return false; // is always 1111 if ((pmt->program_number_H!=pat->program_number_H) || (pmt->program_number_L!=pat->program_number_L)) return false; int desc_len=(pmt->program_info_length_H<<8)+pmt->program_info_length_L; if (desc_len>166) return false; // beyond patpmt buffer int section_end = 196+(pmt->section_length_H<<8)+pmt->section_length_L; section_end-=4; // we don't care about the CRC32 if (section_end>376) return false; //beyond patpmt buffer int i=205+desc_len; while (iES_info_length_H<<8)+si->ES_info_length_L; if (esinfo_len) { es = (struct ES_DESCRIPTOR *) &patpmt[i+sizeof(struct STREAMINFO)]; } // oh no -> more checks! if (si->reserved1!=7) return false; if (si->reserved2!=15) return false; int pid=(si->PID_H<<8)+si->PID_L; switch (si->stream_type) { case 0x1: case 0x2: macontext.Info.VPid.Type=MARKAD_PIDTYPE_VIDEO_H262; // just use the first pid if (!macontext.Info.VPid.Num) macontext.Info.VPid.Num=pid; break; case 0x3: case 0x4: // just use the first pid if (!macontext.Info.APid.Num) macontext.Info.APid.Num=pid; break; case 0x6: if (es) { if (es->Descriptor_Tag==0x6A) macontext.Info.DPid.Num=pid; } break; case 0x1b: macontext.Info.VPid.Type=MARKAD_PIDTYPE_VIDEO_H264; // just use the first pid if (!macontext.Info.VPid.Num) macontext.Info.VPid.Num=pid; break; } i+=(sizeof(struct STREAMINFO)+esinfo_len); } return true; } bool cMarkAdStandalone::RegenerateIndex() { if (!directory) return false; // rename index[.vdr].generated -> index[.vdr] char *oldpath,*newpath; if (asprintf(&oldpath,"%s/index%s.generated",directory, isTS ? "" : ".vdr")==-1) return false; if (asprintf(&newpath,"%s/index%s",directory,isTS ? "" : ".vdr")==-1) { free(oldpath); return false; } if (rename(oldpath,newpath)!=0) { if (errno!=ENOENT) { free(oldpath); free(newpath); return false; } } free(oldpath); free(newpath); return true; } bool cMarkAdStandalone::CreatePidfile() { char *buf=NULL; if (asprintf(&buf,"%s/markad.pid",directory)==-1) return false; // check for other running markad process FILE *oldpid=fopen(buf,"r"); if (oldpid) { // found old pidfile, check if it's still running int pid; if (fscanf(oldpid,"%i\n",&pid)==1) { char procname[256]=""; snprintf(procname,sizeof(procname),"/proc/%i",pid); struct stat statbuf; if (stat(procname,&statbuf)!=-1) { // found another, running markad isyslog("another instance is running on this recording"); abort=duplicate=true; } } fclose(oldpid); } if (duplicate) { free(buf); return false; } FILE *pidfile=fopen(buf,"w+"); if (getuid()==0 || geteuid()!=0) { // if we are root, set fileowner to owner of directory struct stat statbuf; if (!stat(directory,&statbuf)) { if (chown(buf,statbuf.st_uid, statbuf.st_gid)) {}; } } free(buf); if (!pidfile) return false; fprintf(pidfile,"%i\n",(int) getpid()); fflush(pidfile); fclose(pidfile); return true; } void cMarkAdStandalone::RemovePidfile() { if (!directory) return; if (duplicate) return; char *buf; if (asprintf(&buf,"%s/markad.pid",directory)!=-1) { unlink(buf); free(buf); } } const char cMarkAdStandalone::frametypes[8]={'?','I','P','B','D','S','s','b'}; cMarkAdStandalone::cMarkAdStandalone(const char *Directory, const MarkAdConfig *config) { setlocale(LC_MESSAGES, ""); directory=Directory; abort=false; gotendmark=false; reprocess=false; indexFile=NULL; streaminfo=NULL; video_demux=NULL; ac3_demux=NULL; mp2_demux=NULL; decoder=NULL; video=NULL; audio=NULL; osd=NULL; memset(&vpkt,0,sizeof(vpkt)); memset(&apkt,0,sizeof(apkt)); setAudio51=false; setAudio20=false; setVideo43=false; setVideo43LB=false; setVideo169=false; aspectChecked=false; noticeVDR_MP2=false; noticeVDR_AC3=false; noticeHEADER=false; sleepcnt=0; waittime=iwaittime=0; duplicate=false; marksAligned=false; title[0]=0; memset(&macontext,0,sizeof(macontext)); macontext.Config=config; bDecodeVideo=config->DecodeVideo; bDecodeAudio=config->DecodeAudio; tStart=iStart=iStop=iStartCheck=iStopCheck=0; if ((config->ignoreInfo & IGNORE_VIDEOINFO)==IGNORE_VIDEOINFO) { bIgnoreVideoInfo=true; } else { bIgnoreVideoInfo=false; } if ((config->ignoreInfo & IGNORE_AUDIOINFO)==IGNORE_AUDIOINFO) { bIgnoreAudioInfo=true; } else { bIgnoreAudioInfo=false; } if ((config->ignoreInfo & IGNORE_TIMERINFO)==IGNORE_TIMERINFO) { bIgnoreTimerInfo=true; } else { bIgnoreTimerInfo=false; } macontext.Info.DPid.Type=MARKAD_PIDTYPE_AUDIO_AC3; macontext.Info.APid.Type=MARKAD_PIDTYPE_AUDIO_MP2; isyslog("starting v%s",VERSION); isyslog("on %s",Directory); if (!bDecodeAudio) { isyslog("audio decoding disabled by user"); } if (!bDecodeVideo) { isyslog("video decoding disabled by user"); } if (bIgnoreAudioInfo) { isyslog("audio info usage disabled by user"); } if (bIgnoreVideoInfo) { isyslog("video info usage disabled by user"); } if (bIgnoreTimerInfo) { isyslog("timer info usage disabled by user"); } if (config->logoExtraction!=-1) { // just to be sure extraction works bDecodeVideo=true; bIgnoreAudioInfo=true; bIgnoreVideoInfo=true; } if (!config->NoPid) { CreatePidfile(); if (abort) return; } if (config->Before) sleep(10); if (!CheckTS()) return; if (isTS) { if (!CheckPATPMT()) { esyslog("no PAT/PMT found -> cannot process"); abort=true; return; } if (asprintf(&indexFile,"%s/index",Directory)==-1) indexFile=NULL; } else { macontext.Info.APid.Num=-1; macontext.Info.DPid.Num=-1; macontext.Info.VPid.Num=-1; if (CheckVDRHD()) { macontext.Info.VPid.Type=MARKAD_PIDTYPE_VIDEO_H264; } else { macontext.Info.VPid.Type=MARKAD_PIDTYPE_VIDEO_H262; } if (asprintf(&indexFile,"%s/index.vdr",Directory)==-1) indexFile=NULL; } if (!LoadInfo()) { if (bDecodeVideo) { esyslog("failed loading info - logo %s%sdisabled", (config->logoExtraction!=-1) ? "extraction" : "detection", bIgnoreTimerInfo ? " " : " and pre-/post-timer "); tStart=0; } } if (tStart>1) isyslog("pre-timer %im",tStart/60); if (length) isyslog("broadcast length %im",length/60); if (title[0]) { ptitle=title; } else { ptitle=(char *) Directory; } if (config->OSD) { osd= new cOSDMessage(config->svdrphost,config->svdrpport); if (osd) osd->Send("%s %s",tr("starting markad for"),ptitle); } else { osd=NULL; } if (config->markFileName[0]) marks.SetFileName(config->markFileName); if (macontext.Info.VPid.Num) { if (isTS) { dsyslog("using %s-video (0x%04x)", macontext.Info.VPid.Type==MARKAD_PIDTYPE_VIDEO_H264 ? "H264": "H262", macontext.Info.VPid.Num); } else { dsyslog("using %s-video", macontext.Info.VPid.Type==MARKAD_PIDTYPE_VIDEO_H264 ? "H264": "H262"); } video_demux = new cMarkAdDemux(); } else { video_demux=NULL; } if (macontext.Info.APid.Num) { if (macontext.Info.APid.Num!=-1) dsyslog("using MP2 (0x%04x)",macontext.Info.APid.Num); mp2_demux = new cMarkAdDemux(); } else { mp2_demux=NULL; } if (macontext.Info.DPid.Num) { if (macontext.Info.DPid.Num!=-1) dsyslog("using AC3 (0x%04x)%s",macontext.Info.DPid.Num,macontext.Config->AC3Always ? "*" : ""); ac3_demux = new cMarkAdDemux(); } else { ac3_demux=NULL; } if (!abort) { decoder = new cMarkAdDecoder(macontext.Info.VPid.Type==MARKAD_PIDTYPE_VIDEO_H264, macontext.Info.APid.Num!=0,macontext.Info.DPid.Num!=0,config->threads); video = new cMarkAdVideo(&macontext); audio = new cMarkAdAudio(&macontext); streaminfo = new cMarkAdStreamInfo; if (macontext.Info.ChannelName) dsyslog("channel %s",macontext.Info.ChannelName); } framecnt=0; framecnt2=0; lastiframe=0; iframe=0; chkLEFT=INT_MAX; chkRIGHT=INT_MIN; gettimeofday(&tv1,&tz); } cMarkAdStandalone::~cMarkAdStandalone() { if ((!abort) && (!duplicate)) { gettimeofday(&tv2,&tz); time_t sec; suseconds_t usec; sec=tv2.tv_sec-tv1.tv_sec; usec=tv2.tv_usec-tv1.tv_usec; if (usec<0) { usec+=1000000; sec--; } double etime,ftime=0,ptime=0; etime=sec+((double) usec/1000000)-waittime; if (etime>0) ftime=(framecnt+framecnt2)/etime; if (macontext.Video.Info.FramesPerSecond>0) ptime=ftime/macontext.Video.Info.FramesPerSecond; isyslog("processed time %.2fs, %i/%i frames, %.1f fps, %.1f pps", etime,framecnt,framecnt2,ftime,ptime); } if (osd) { if (abort) { osd->Send("%s %s",tr("markad aborted for"),ptitle); } else { osd->Send("%s %s",tr("markad finished for"),ptitle); } } if (macontext.Info.ChannelName) free(macontext.Info.ChannelName); if (indexFile) free(indexFile); if (video_demux) delete video_demux; if (ac3_demux) delete ac3_demux; if (mp2_demux) delete mp2_demux; if (decoder) delete decoder; if (video) delete video; if (audio) delete audio; if (streaminfo) delete streaminfo; if (osd) delete osd; RemovePidfile(); } bool isnumber(const char *s) { while (*s) { if (!isdigit(*s)) return false; s++; } return true; } int usage(int svdrpport) { // nothing done, give the user some help printf("Usage: markad [options] cmd \n" "options:\n" "-a --AC3\n" " always search in DolbyDigital channels, even if the\n" " broadcast isn't in DolbyDigital5.1\n" "-b --background\n" " markad runs as a background-process\n" " this will be automatically set if called with \"after\"\n" "-d --disable=