ps->pts = -1.0;
static int mux_put_av_pack(PackStream *ps){  BitPack *bp = &ps->bit_pack;     uint32_t startcode;  int      i, header_len, header_data_len, sub_hdr_len;  int      size, padding_size, available_size, payload_size;  uint8_t  pts_dts_flag = 0x00;  uint8_t  pts_intro    = 0x00;  double   dts          = -1.0;  memset(ps->pack_start, 0, ps->pack_size);  put_init_bp(bp, ps->pack_start);   //-- put pack header --  size = put_bp_header(ps);      header_len = 3;  // mpeg2 id (1), flags (1), header data len (1)    //-- header data may contains at maximum:   --  //-- PTS (5) + DTS (5) + PTS-ext (3) = (13) --  //-- if timestamp(s) requested --  if ( ps->pts != -1.0 )  {    //-- video data (PTS + DTS) --    if ( ps->pkt_id == VIDEO_ID )    {      header_data_len = 10;      pts_dts_flag    = 0xC0;      pts_intro       = 0x03;      dts             = ps->pts - ps->pict_duration;       }    //-- audio data (PTS only) --    else    {      header_data_len = 5;      pts_intro    = 0x02;      pts_dts_flag = 0x80;    }    }  else  {    header_data_len = 0;  }    //-- calculate uncorrected payload size ... --  payload_size = ps->pack_size - (size + 6 + header_len + header_data_len);      //-- ... but possibly some corrections --     if (ps->pkt_id < AUDIO_ID_MP2)  {    startcode     = MX_PRIVATE_STREAM_1;    sub_hdr_len   = 4;    payload_size -= sub_hdr_len;  }  else   {    startcode   = 0x100 + ps->pkt_id;    sub_hdr_len = 0;  }      if (ps->pkt_len < payload_size)  {    available_size = ps->pkt_len;    padding_size   = payload_size - available_size;          if ( (padding_size < 7) && (padding_size > 0) )     {      header_data_len += padding_size;      padding_size     = 0;    }    else      padding_size -= 6;  }  else  {    available_size = payload_size;      padding_size   = 0;  }          //-- PES startcode (4) and PES packet len (2) --  put_qword(bp, startcode);   put_word(bp, available_size + header_len + header_data_len + sub_hdr_len);      //-- mpeg2 needs 3 byte header --   put_byte(bp, 0x80);            /* mpeg2 id */	  put_byte(bp, pts_dts_flag);    /* flags    */     put_byte(bp, header_data_len); /* header data len */      //-- PTS timestamp on (caller) demand --  if (ps->pts != -1.0)  {    int64_t timestamp = (int64_t)(ps->pts * 90);          //-- packet header data --    put_byte(bp,              (pts_intro << 4) |              (((timestamp >> 30) & 0x07) << 1) |              1);    put_word(bp, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));    put_word(bp, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));          header_data_len -= 5;  }    //-- DTS timestamp on (caller) demand --  if (dts != -1.0)  {    int64_t timestamp = (int64_t)(dts * 90);                //-- packet header data --    put_byte(bp,              (0x01 << 4) |              (((timestamp >> 30) & 0x07) << 1) |              1);    put_word(bp, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));    put_word(bp, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));          header_data_len -= 5;  }    ps->pts = -1.0;  // caller must set pts again to activate timestamping      //-- stuffing rest of header data len --  for(i=0;i<header_data_len;i++) put_byte(bp, 0xff);  if (startcode == MX_PRIVATE_STREAM_1)  {    uint16_t ofs_sw = mux_find_a52sw(ps->pkt_buf, available_size);    uint8_t  nb_sw  = (uint8_t)((available_size - ofs_sw)/ps->audio_encoded_fs);         if ( ((available_size-ofs_sw) % ps->audio_encoded_fs) > 0 ) nb_sw += 1;      //-- a AC3 specific header contains:                --    //-- audio ID                                   (8) --    //-- number of sync words contained in payload  (8) --    //-- offset to the first sync word in payload  (16) --    put_byte(bp, ps->pkt_id);    put_byte(bp, nb_sw);     put_word(bp, ofs_sw);  }  //-- put available data as payload --  put_mem(bp, ps->pkt_buf, available_size);  //-- padding rest of payload if needed --   if ( padding_size > 0 )  {        put_qword(bp, MX_PADDING_STREAM); 			    put_word(bp, padding_size);    for(i=0; i<padding_size;i++) put_byte(bp, 0xff);    }  size = bp->byte_ofs;    //-- TODO --  if (size != ps->pack_size)     fprintf(stderr, "\nERR: pack size (%d) mismatch\n", size);    ps->pack_cnt++;  ps->pack_start += size;  ps->buf_level  += size;    return available_size; // return really used payload}