[Libav-user] PCR value for TS stream

Li Zhang lizhang at utelisys.com
Wed May 9 11:38:30 CEST 2012


I used the TS container to encapsulate my H264 and AAC. I want to use the PCR value to control UDP sending. However, I found that the PCR value I got from ffmpeg is very strange.

Firstly, some of the pcr value is super big and abnormal, such as


Secondly, I found the difference between two PCR value is 7200 or 3600, which is 80ms or 40ms. And there are twenty-four 80ms and only two 40ms every 26 differences. According the standard, TS stream should have a pcr value at least 40ms. So I do not know why.

Here is the code I used to parse PCR value, I hope it is correct.

        int i=0;        //position pointer iterator
        int buf_size = 0; //the length of data packet
        uint8_t *buf = NULL;    //data packet pointer
        uint8_t *position = NULL;       //starting point for each 188B
        uint8_t adaption_field = 0;     //adaption field existing flag
        unsigned int pcr_flag;  //pcr existing flag
        uint8_t *pcr_field = NULL;      //pcr field pointer
        uint64_t pcr_base;      //base part of pcr
        unsigned int pcr_ext;   //extentions part of pcr

                for(i=0;i<buf_size;) {

                        position = &buf[i];
                        adaption_field = (position[3] & 0x30)>>4;

                        if(adaption_field > 1) {
                                pcr_flag = (position[5] & 0x10)>>4;

                                if(pcr_flag == 1) {
                                        pcr_field = position + 6;

                                        pcr_base = (pcr_field[0] * 0x2000000) + (pcr_field[1] * 0x20000) + (pcr_field[2] * 0x200) + (pcr_field[3] * 0x02) + ((pcr_field[4] & 0x80) >> 7);
                                        pcr_ext = ((pcr_field[4] & 0x01) * 0x100) + pcr_field[5];

                                        printf("------------------pcr_base:%llu \n",pcr_base);
                         i += 188;

So does anyone can help me with that?


Best regards,


More information about the Libav-user mailing list