通過對碼流播放卡的分析,以及對公式的對照,對實際計算播放視頻文件所用帶寬的方法進行了分析(附件中,帶有c語言的實現)
分析如下:
關鍵是最後計算帶寬的公式:
PCRdelta = (uint64_t) ((uint64_t)PCR1 - (uint64_t)PCR0);
bitRate = (double) ( ((double)(40608000000ULL)*((double)mpegPackets))/(double)(PCRdelta));
PCRdelta : 是相鄰N個PCR的PCR差值
mpegPackets : 是相鄰N個PCR,所包含的packet的數量(MPEG packet)
我們可以理解爲: mpegPackets*188表示實際的數據
而PCRdelta表示播放這段實際數據,總需要播放的總數據量.
則: (mpegPackets*188)/PCRdelta : 單位實際播放的數據中所包含的有效(MPEG packet)數據量
而我們目前所使用的VP播放卡,要求了,DVB-ASI輸出帶寬必須是216Mb/s
所以就有 bitRate/216000000 = (mpegPackets*188)/PCRdelta
因此得到 bitRate = (double) ( ((double)(40608000000ULL)*((double)mpegPackets))/(double)(PCRdelta));
所以,按照目前這樣分析,播放同一個包含pcr的視頻文件,如果播放的DVB-ASI輸出總帶寬不同,則播放視頻文件所分配的帶寬也應該不同.
////////////////////////////////////////////////////////////////////////
//
// This routine extracts the bitrate from the content file.
// The content file must contain PCRs
//
////////////////////////////////////////////////////////////////////////
double getBitRate(FILE *fp)
{
// variables used to get bit rate from content file
uint64_t PCR0 = 0, PCR1 = 0;
uint64_t PCRdelta;
double bitRate = 0;
uint32_t mpegPackets = 0;
uint32_t gotPCR = 0;
unsigned char m2tTmp[256];
int length;
unsigned int PID = 0;
//used to control "." output
int sync = 0;
printf(" Scanning content file for bit rate ");
for (;;) {
length = fread(m2tTmp, sizeof(uint8_t), 188, fp);
if(length != 188) break;
mpegPackets++;
if (sync % 1200 == 0 ) {
//show user something while we loop
printf(".");
//force it out-may not otherwise appear in real time
fflush(stdout);
}
sync++;
// look for the sync byte
if(m2tTmp[0] != 0x47) {
printf("vpSendspts: data corruption in content file ");
fclose(fp);
exit(2);
}
// we are going to scan through the first 10 PCRs in this content.
// According to the specifications that will be on the order of
// every 40 milliseconds. We don't have to scan the whole file
// to get a good sense of its validity and bit rate.
// First see if the TS header indicates the presence of an
// adaptation field.
if(GET_DVBASI_ADAPT(m2tTmp) & 0x02) {
// Get the adaptation field and look for the presence of
// a PCR in the adaptation field
if(GET_DVBASI_ADAPTLENGTH(m2tTmp) == 0) {
printf("Zero length AF ");
continue;
}
if(GET_DVBASI_ADAPTFIELD(m2tTmp) & 0x10) {
// Get the initial PCR and reset the counter
if(gotPCR == 10) { // skip the first few, some times they are bad!
PID = GET_DVBASI_PID(m2tTmp);
GET_DVBASI_PCR(m2tTmp, PCR0);
mpegPackets = 0;
}
// Look for the 10th PCR in this content and
if((gotPCR > 20) && PID == GET_DVBASI_PID(m2tTmp)) {
GET_DVBASI_PCR(m2tTmp, PCR1);
PCRdelta = (uint64_t) ((uint64_t)PCR1 - (uint64_t)PCR0);
bitRate = (double) ( ((double)(40608000000ULL)*((double)mpegPackets))/(double)(PCRdelta));
break;
}
gotPCR++;
} // GET_DVBASI_ADAPTFIELD
} //GET_DVBASI_ADAPT
} // while read
//rewind file
rewind(fp);
fflush(fp);
return(bitRate);
}
//
// This routine extracts the bitrate from the content file.
// The content file must contain PCRs
//
////////////////////////////////////////////////////////////////////////
double getBitRate(FILE *fp)
{
// variables used to get bit rate from content file
uint64_t PCR0 = 0, PCR1 = 0;
uint64_t PCRdelta;
double bitRate = 0;
uint32_t mpegPackets = 0;
uint32_t gotPCR = 0;
unsigned char m2tTmp[256];
int length;
unsigned int PID = 0;
//used to control "." output
int sync = 0;
printf(" Scanning content file for bit rate ");
for (;;) {
length = fread(m2tTmp, sizeof(uint8_t), 188, fp);
if(length != 188) break;
mpegPackets++;
if (sync % 1200 == 0 ) {
//show user something while we loop
printf(".");
//force it out-may not otherwise appear in real time
fflush(stdout);
}
sync++;
// look for the sync byte
if(m2tTmp[0] != 0x47) {
printf("vpSendspts: data corruption in content file ");
fclose(fp);
exit(2);
}
// we are going to scan through the first 10 PCRs in this content.
// According to the specifications that will be on the order of
// every 40 milliseconds. We don't have to scan the whole file
// to get a good sense of its validity and bit rate.
// First see if the TS header indicates the presence of an
// adaptation field.
if(GET_DVBASI_ADAPT(m2tTmp) & 0x02) {
// Get the adaptation field and look for the presence of
// a PCR in the adaptation field
if(GET_DVBASI_ADAPTLENGTH(m2tTmp) == 0) {
printf("Zero length AF ");
continue;
}
if(GET_DVBASI_ADAPTFIELD(m2tTmp) & 0x10) {
// Get the initial PCR and reset the counter
if(gotPCR == 10) { // skip the first few, some times they are bad!
PID = GET_DVBASI_PID(m2tTmp);
GET_DVBASI_PCR(m2tTmp, PCR0);
mpegPackets = 0;
}
// Look for the 10th PCR in this content and
if((gotPCR > 20) && PID == GET_DVBASI_PID(m2tTmp)) {
GET_DVBASI_PCR(m2tTmp, PCR1);
PCRdelta = (uint64_t) ((uint64_t)PCR1 - (uint64_t)PCR0);
bitRate = (double) ( ((double)(40608000000ULL)*((double)mpegPackets))/(double)(PCRdelta));
break;
}
gotPCR++;
} // GET_DVBASI_ADAPTFIELD
} //GET_DVBASI_ADAPT
} // while read
//rewind file
rewind(fp);
fflush(fp);
return(bitRate);
}