2 #include <lib/dvb/tstools.h>
3 #include <lib/base/eerror.h>
9 eDVBTSTools::eDVBTSTools()
13 m_maxrange = 256*1024;
19 eDVBTSTools::~eDVBTSTools()
24 int eDVBTSTools::openFile(const char *filename)
27 m_fd = ::open(filename, O_RDONLY);
33 void eDVBTSTools::closeFile()
39 void eDVBTSTools::setSyncPID(int pid)
44 void eDVBTSTools::setSearchRange(int maxrange)
46 m_maxrange = maxrange;
49 int eDVBTSTools::getPTS(off_t &offset, pts_t &pts)
54 offset -= offset % 188;
56 // TODO: multiple files!
57 if (lseek(m_fd, offset, SEEK_SET) < 0)
60 int left = m_maxrange;
64 unsigned char block[188];
65 if (read(m_fd, block, 188) != 188)
79 offset = lseek(m_fd, i - 188, SEEK_CUR);
83 int pid = ((block[1] << 8) | block[2]) & 0x1FFF;
84 int pusi = !!(block[1] & 0x40);
86 // printf("PID %04x, PUSI %d\n", pid, pusi);
94 /* ok, now we have a PES header */
97 /* check for adaption field */
99 pes = block + block[4] + 4 + 1;
103 /* somehow not a startcode. (this is invalid, since pusi was set.) ignore it. */
104 if (pes[0] || pes[1] || (pes[2] != 1))
107 if (pes[7] & 0x80) /* PTS */
109 pts = ((unsigned long long)(pes[ 9]&0xE)) << 29;
110 pts |= ((unsigned long long)(pes[10]&0xFF)) << 22;
111 pts |= ((unsigned long long)(pes[11]&0xFE)) << 14;
112 pts |= ((unsigned long long)(pes[12]&0xFF)) << 7;
113 pts |= ((unsigned long long)(pes[13]&0xFE)) >> 1;
122 void eDVBTSTools::calcBegin()
130 if (!getPTS(m_offset_begin, m_pts_begin))
135 void eDVBTSTools::calcEnd()
140 m_offset_end = lseek(m_fd, 0, SEEK_END);
149 m_offset_end -= m_maxrange;
150 if (m_offset_end < 0)
152 if (!getPTS(m_offset_end, m_pts_end))
159 int eDVBTSTools::calcLen(pts_t &len)
161 calcBegin(); calcEnd();
162 if (!(m_begin_valid && m_end_valid))
164 len = m_pts_end - m_pts_begin;
168 int eDVBTSTools::calcBitrate()
170 calcBegin(); calcEnd();
171 if (!(m_begin_valid && m_end_valid))
174 pts_t len_in_pts = m_pts_end - m_pts_begin;
175 off_t len_in_bytes = m_offset_end - m_offset_begin;
180 unsigned long long bitrate = len_in_bytes * 90000 * 8 / len_in_pts;
181 if ((bitrate < 10000) || (bitrate > 100000000))