Program: 70566
Project: 70566
Buggy source:
public byte[] write(boolean isFirstPes,FrameDataType frameDataType,FrameData... frames){
  byte[] tsFileBuffer=null;
  byte[] patTsBuf=write_pat();
  tsFileBuffer=TsUtil.margeByteArray(tsFileBuffer,patTsBuf);
  byte[] pmtTsBuf=write_pmt(frameDataType);
  tsFileBuffer=TsUtil.margeByteArray(tsFileBuffer,pmtTsBuf);
  for (  FrameData frame : frames) {
    boolean isFristTs=true;
    boolean isAudio=frame.isAudio;
    long pts=frame.pts;
    long dts=frame.dts;
    byte[] frameBuf=frame.buf;
    int frameBufSize=frameBuf.length;
    int frameBufPtr=0;
    int pid=isAudio ? TS_AUDIO_PID : TS_VIDEO_PID;
    while (frameBufPtr < frameBufSize) {
      int frameBufRemaining=frameBufSize - frameBufPtr;
      boolean isAdaptationField=(isFristTs || (frameBufRemaining < TS_PAYLOAD_SIZE)) ? true : false;
      byte[] tsBuf=new byte[TS_PACKET_SIZE];
      int offset=0;
      tsBuf[offset++]=0x47;
      tsBuf[offset++]=(byte)((isFristTs ? 0x40 : 0x00) | ((pid >> 8) & 0x1f));
      tsBuf[offset++]=(byte)(pid & 0xff);
      tsBuf[offset++]=(byte)((isAdaptationField ? 0x30 : 0x10) | ((isAudio ? mAudioContinuityCounter++ : mVideoContinuityCounter++) & 0xF));
      if (isFristTs) {
        tsBuf[offset++]=0x07;
        tsBuf[offset++]|=isFirstPes ? 0x50 : (isAudio ? 0x05 : 0x10);
        long pcr=pts;
        tsBuf[offset++]=(byte)((pcr >> 25) & 0xFF);
        tsBuf[offset++]=(byte)((pcr >> 17) & 0xFF);
        tsBuf[offset++]=(byte)((pcr >> 9) & 0xFF);
        tsBuf[offset++]=(byte)((pcr >> 1) & 0xFF);
        tsBuf[offset++]=0x00;
        tsBuf[offset++]=0x00;
        tsBuf[offset++]=0x00;
        tsBuf[offset++]=0x00;
        tsBuf[offset++]=0x01;
        tsBuf[offset++]=isAudio ? (byte)0xc0 : (byte)0xe0;
        int header_size=5 + 5;
        if (isAudio) {
          int pes_size=frameBufSize + header_size + 3;
          tsBuf[offset++]=(byte)((pes_size >> 8) & 0xFF);
          tsBuf[offset++]=(byte)(pes_size & 0xFF);
        }
 else {
          tsBuf[offset++]=0x00;
          tsBuf[offset++]=0x00;
        }
        tsBuf[offset++]=(byte)0x80;
        tsBuf[offset++]=(byte)0xc0;
        tsBuf[offset++]=(byte)header_size;
        tsBuf[offset++]=(byte)(((pts >> 30) & 0xFE) | 0x31);
        tsBuf[offset++]=(byte)((pts >> 22) & 0xff);
        tsBuf[offset++]=(byte)(((pts >> 14) & 0xFE) | 0x01);
        tsBuf[offset++]=(byte)((pts >> 7) & 0xff);
        tsBuf[offset++]=(byte)((pts << 1) & 0xFE | 0x01);
        tsBuf[offset++]=(byte)(((dts >> 29) & 0xFE) | 0x11);
        tsBuf[offset++]=(byte)((dts >> 22) & 0xff);
        tsBuf[offset++]=(byte)(((dts >> 14) & 0xFE) | 0x01);
        tsBuf[offset++]=(byte)((dts >> 7) & 0xff);
        tsBuf[offset++]=(byte)((dts << 1) & 0xFE | 0x01);
        if (!isAudio && Bytes.indexOf(frameBuf,H264_NAL) == -1) {
          System.arraycopy(H264_NAL,0,tsBuf,offset++,H264_NAL.length);
          offset+=H264_NAL.length;
        }
      }
 else {
        if (isAdaptationField) {
          tsBuf[offset++]=1;
          tsBuf[offset++]=(byte)0x00;
        }
 else {
        }
      }
      int tsBufRemaining=TS_PACKET_SIZE - offset;
      if (frameBufRemaining >= tsBufRemaining) {
        System.arraycopy(frameBuf,frameBufPtr,tsBuf,offset,tsBufRemaining);
        offset+=tsBufRemaining;
        frameBufPtr+=tsBufRemaining;
      }
 else {
        int paddingSize=tsBufRemaining - frameBufRemaining;
        if (isAdaptationField) {
          int adaptationFieldLength=(tsBuf[4] & 0xFF);
          int start=TS_HEADER_SIZE + adaptationFieldLength + 1;
          int end=offset - 1;
          for (int i=end; i >= start; i--) {
            tsBuf[i + paddingSize]=tsBuf[i];
          }
          for (int i=0; i < paddingSize; i++) {
            tsBuf[start + i]=(byte)0xff;
          }
          tsBuf[4]+=paddingSize;
        }
 else {
          tsBuf[3]|=0x20;
          tsBuf[4]=(byte)paddingSize;
          tsBuf[5]=0;
          for (int i=0; i < paddingSize; i++) {
            tsBuf[6 + i]=(byte)0xFF;
          }
        }
        System.arraycopy(frameBuf,frameBufPtr,tsBuf,(int)(offset + paddingSize),frameBufRemaining);
        frameBufPtr+=frameBufRemaining;
      }
      isFristTs=false;
      tsFileBuffer=TsUtil.margeByteArray(tsFileBuffer,tsBuf);
    }
  }
  return tsFileBuffer;
}

Fixed version (ground truth):
public byte[] write(boolean isFirstPes,FrameDataType frameDataType,FrameData... frames){
  byte[] tsFileBuffer=null;
  byte[] patTsBuf=write_pat();
  tsFileBuffer=TsUtil.margeByteArray(tsFileBuffer,patTsBuf);
  byte[] pmtTsBuf=write_pmt(frameDataType);
  tsFileBuffer=TsUtil.margeByteArray(tsFileBuffer,pmtTsBuf);
  for (  FrameData frame : frames) {
    boolean isFristTs=true;
    boolean isAudio=frame.isAudio;
    long pts=frame.pts;
    long dts=frame.dts;
    byte[] frameBuf=frame.buf;
    int frameBufSize=frameBuf.length;
    int frameBufPtr=0;
    int pid=isAudio ? TS_AUDIO_PID : TS_VIDEO_PID;
    while (frameBufPtr < frameBufSize) {
      int frameBufRemaining=frameBufSize - frameBufPtr;
      boolean isAdaptationField=(isFristTs || (frameBufRemaining < TS_PAYLOAD_SIZE)) ? true : false;
      byte[] tsBuf=new byte[TS_PACKET_SIZE];
      int offset=0;
      tsBuf[offset++]=0x47;
      tsBuf[offset++]=(byte)((isFristTs ? 0x40 : 0x00) | ((pid >> 8) & 0x1f));
      tsBuf[offset++]=(byte)(pid & 0xff);
      tsBuf[offset++]=(byte)((isAdaptationField ? 0x30 : 0x10) | ((isAudio ? mAudioContinuityCounter++ : mVideoContinuityCounter++) & 0xF));
      if (isFristTs) {
        tsBuf[offset++]=0x07;
        tsBuf[offset++]|=isFirstPes ? 0x50 : (isAudio ? 0x50 : 0x10);
        long pcr=pts;
        tsBuf[offset++]=(byte)((pcr >> 25) & 0xFF);
        tsBuf[offset++]=(byte)((pcr >> 17) & 0xFF);
        tsBuf[offset++]=(byte)((pcr >> 9) & 0xFF);
        tsBuf[offset++]=(byte)((pcr >> 1) & 0xFF);
        tsBuf[offset++]=0x00;
        tsBuf[offset++]=0x00;
        tsBuf[offset++]=0x00;
        tsBuf[offset++]=0x00;
        tsBuf[offset++]=0x01;
        tsBuf[offset++]=isAudio ? (byte)0xc0 : (byte)0xe0;
        int header_size=5 + 5;
        if (isAudio) {
          int pes_size=frameBufSize + header_size + 3;
          tsBuf[offset++]=(byte)((pes_size >> 8) & 0xFF);
          tsBuf[offset++]=(byte)(pes_size & 0xFF);
        }
 else {
          tsBuf[offset++]=0x00;
          tsBuf[offset++]=0x00;
        }
        tsBuf[offset++]=(byte)0x80;
        tsBuf[offset++]=(byte)0xc0;
        tsBuf[offset++]=(byte)header_size;
        tsBuf[offset++]=(byte)(((pts >> 30) & 0xFE) | 0x31);
        tsBuf[offset++]=(byte)((pts >> 22) & 0xff);
        tsBuf[offset++]=(byte)(((pts >> 14) & 0xFE) | 0x01);
        tsBuf[offset++]=(byte)((pts >> 7) & 0xff);
        tsBuf[offset++]=(byte)((pts << 1) & 0xFE | 0x01);
        tsBuf[offset++]=(byte)(((dts >> 29) & 0xFE) | 0x11);
        tsBuf[offset++]=(byte)((dts >> 22) & 0xff);
        tsBuf[offset++]=(byte)(((dts >> 14) & 0xFE) | 0x01);
        tsBuf[offset++]=(byte)((dts >> 7) & 0xff);
        tsBuf[offset++]=(byte)((dts << 1) & 0xFE | 0x01);
        if (!isAudio && Bytes.indexOf(frameBuf,H264_NAL) == -1) {
          System.arraycopy(H264_NAL,0,tsBuf,offset++,H264_NAL.length);
          offset+=H264_NAL.length;
        }
      }
 else {
        if (isAdaptationField) {
          tsBuf[offset++]=1;
          tsBuf[offset++]=(byte)0x00;
        }
 else {
        }
      }
      int tsBufRemaining=TS_PACKET_SIZE - offset;
      if (frameBufRemaining >= tsBufRemaining) {
        System.arraycopy(frameBuf,frameBufPtr,tsBuf,offset,tsBufRemaining);
        offset+=tsBufRemaining;
        frameBufPtr+=tsBufRemaining;
      }
 else {
        int paddingSize=tsBufRemaining - frameBufRemaining;
        if (isAdaptationField) {
          int adaptationFieldLength=(tsBuf[4] & 0xFF);
          int start=TS_HEADER_SIZE + adaptationFieldLength + 1;
          int end=offset - 1;
          for (int i=end; i >= start; i--) {
            tsBuf[i + paddingSize]=tsBuf[i];
          }
          for (int i=0; i < paddingSize; i++) {
            tsBuf[start + i]=(byte)0xff;
          }
          tsBuf[4]+=paddingSize;
        }
 else {
          tsBuf[3]|=0x20;
          tsBuf[4]=(byte)paddingSize;
          tsBuf[5]=0;
          for (int i=0; i < paddingSize; i++) {
            tsBuf[6 + i]=(byte)0xFF;
          }
        }
        System.arraycopy(frameBuf,frameBufPtr,tsBuf,(int)(offset + paddingSize),frameBufRemaining);
        frameBufPtr+=frameBufRemaining;
      }
      isFristTs=false;
      tsFileBuffer=TsUtil.margeByteArray(tsFileBuffer,tsBuf);
    }
  }
  return tsFileBuffer;
}