James McAvoy goes whole hog in “Filth,” an adaptation of a 1998 novel by Irvine Welsh, the Scottish author of “Trainspotting.” And by whole hog, I mean exactly that. His character, Bruce Robertson, who narrates the film, is an Edinburgh police detective sergeant on a catastrophic downward spiral. As he loses his sanity, he suggests a wounded wild boar on the rampage, dragging his entrails behind him as he charges around in circles. Compared with this maniac, the rotten policeman of two “Bad Lieutenant” films is a garden-variety bad boy.

Such is the electrical ferocity of Mr. McAvoy’s performance that you can never again look at him the same way. At times, this pig in a china shop suggests a descendant of Alex from “A Clockwork Orange,” and he is sometimes filmed as such, with distorting, wide-angle shots. A coke-crazed, booze-soaked sex fiend and sadomasochist obsessed with erotic asphyxiation, he is a ragingly homophobic, misogynistic bully, given to spewing torrents of profane abuse, sometimes accompanied by vomit. To help explain the character, the film’s director, Jon S. Baird, has made him clinically bipolar.

A major difference between Bruce and his wild-man forerunners is his raucous sense of humor, which lends him the dangerous charm of a boorish stand-up comedian you might enjoy from a back row of the theater, where the risk of being singled out is lowest. In one of the funniest scenes, he persuades his fellow detectives to photocopy images of their genitals and pass them around in a guessing game of which picture belongs to which officer. Because Bruce presses the enlarge button when his turn comes, a secretary who fancies him is eventually shocked to discover that he is smaller than advertised.

Mr. Baird’s visual imagination is more Terry Gilliam-goes-to-the-circus than the inside-the-toilet realism of Danny Boyle’s “Trainspotting.” The world as seen through Bruce’s bloodshot eyes is a surreal phantasmagoria that grows more grotesque as his sanity deteriorates. The movie stops short of including one of the book’s voices: a talking tapeworm inside Bruce’s body.

In the story, Bruce, told he is first in line for a promotion to chief inspector, sets out to destroy his rivals while halfheartedly trying to solve the murder shown in the movie’s opening scene. Hoping to win back his estranged wife, Carole (Shauna Macdonald), he foolishly stops taking the lithium prescribed by his psychiatrist, Dr. Rossi (Jim Broadbent). Without his medication, he begins to lose what remains of his stability and starts hallucinating about people with animal heads. He seduces Bunty (Shirley Henderson), the ravenously lustful wife of his loyal best friend, Bladesy (Eddie Marsan), a meek accountant, while harassing her with menacing prank phone calls. He blackmails a 14-year-old girl into performing oral sex.

The haranguing tone of “Filth,” whose title is Scottish slang for the police, causes the movie to lose its roughneck charm quickly. After a certain point, watching it is like listening to the ravings of an increasingly incoherent and abusive drunk. Further impeding enjoyment, the characters’ Scottish accents render swatches of the dialogue impenetrable.