For a film boasting only marginally fewer producers than it does speaking roles, there’s something impressively unguarded about “Filth.” Sophomore helmer Jon S. Baird’s needfully messy adaptation of “Trainspotting” author Irvine Welsh’s 1998 bestseller stays true to the novel’s spirit of riotous despair, even as it cuts down on the tapeworm monologues. Powered by a vigorous, image-shedding lead turn from James McAvoy as a coked-up Edinburgh detective on the fast track to either promotion or self-implosion, this descent into Scotch-marinated madness begins as ugly comedy, segues almost imperceptibly into farcical tragedy, and inevitably — perhaps intentionally — loses control in the process. Released across Blighty in early October, this bracingly abrasive trip has shown surprising commercial legs for a film that, true to its title, leaves audiences craving a hot shower afterward; Yanks may be less inclined to savor the stink.

Welsh, who takes an executive-producer credit here, was a hot property when “Filth” was published,  warm in the afterglow from Danny Boyle’s landmark 1996 adaptation of “Trainspotting.” That the new book then took 15 years to reach the screen is indicative of the challenges it poses to anyone attempting a film treatment. “Trainspotting,” a junkie odyssey written entirely in stream-of-consciousness Scots vernacular, was tricky enough; “Filth” is an entirely more heightened creation, practically baroque in its nastiness, with a barely penetrable protagonist in Det. Bruce Robertson (McAvoy), a morally repugnant all-purpose addict whose hobbies include homophobic bullying, erotic asphyxiation and sex-pest prank calls. (The title, of course, is both a colloquial British term for police and an apt description of Robertson himself.)

Improving on his 2008 debut, the proficient soccer-hooligan bio “Cass,” Baird spares the audience’s sensibilities (and stomach fluids) to some extent in his adaptation, leaving out some of Welsh’s least pleasant details and digressions — no regular updates on Robertson’s diseased genitals, for starters, though at least one penis-related gag lands on target — and constructing a more sympathetic backstory for this baddest of bad lieutenants. Not that the material has been sanitized: Nicolas Cage and Harvey Keitel might balk at some of the stunts McAvoy’s bent cop is required to pull onscreen, none more gasp-inducing than blackmailing a 14-year-old witness into fellatio.

The film opens with the murder of a Japanese student by a pack of feral thugs, Robertson’s investigation into which ostensibly gives the kinky narrative its spine. Just as the detective’s narcotics-blurred mind mostly seems to be elsewhere, however, so the case recedes into the background for the bulk of the film, as his crazed antics bleed into a host of interconnected subplots.

At the office, he’s deviously competing against colleagues — including straight-arrow go-getter Drummond (Imogen Poots, excellent) and puppyish rookie Lennox (Jamie Bell) — for the newly vacated role of inspector. Extracurricular activities include the extreme humiliation of Freemason cohort Blades (Eddie Marsan), a milquetoast sap whose wife (Shirley Henderson) Robertson subjects to repeated sexual harassment. His own wife, Carole (Shauna Macdonald), meanwhile, narrates from the sidelines in suspiciously Vaseline-smeared sidebars. It’s swiftly apparent that she’s no longer in the picture, though the circumstances of her departure only emerge later, feeding into a ludicrous twist that tests even this film’s loose grip on emotional reality. It’s all set at Christmastime; the sight of a urinating Santa notwithstanding, there’s nothing here to threaten “It’s a Wonderful Life” for holiday-standard status.

The tone of the proceedings ranges from chaotic to frenetic to anarchic: Non-U.K. viewers may be additionally exhausted by the prevalence of rapid-fire dialogue in a range of custard-thick Scottish brogues, disguising many of the screenplay’s richest, raunchiest one-liners. Baird’s direction is of a similarly heedless, knockabout nature, filching from other filmmakers with magpie abandon: Kubrick in the sweaty, distortive use of closeup, Fassbinder in an arch, unexpectedly resolved strain of Germanic camp, and Gilliam in Robertson’s fantasy sessions with swollen-headed shrink Jim Broadbent — perhaps the film’s least successful flight of fancy. An animated closing-credits sequence that restages the story with farmyard animals would be a stylistic bridge too far in just about any other film.

It’d be easy to get swamped in filmmaking this busy, but McAvoy’s go-for-broke performance is its own hurricane within the storm. His usual boyish charms submerged beneath a sallow complexion and pot-scourer beard, the actor accepts that making Robertson even roguishly likable is a lost cause; far more important is that he remain a galvanizing figure to the end. Despite starting the film in full cry — pushing repeatedly against the fourth wall — McAvoy carefully modulates the excesses of his performance. There’s a hint of play-acting to Robertson’s sociopathic swagger at the beginning, but the actor sheds that self-awareness as the extent of the character’s bipolarity is revealed.

As an individual performance, it’s startling; as an exercise in opening out McAvoy’s amiable screen persona, it’s far more successful than his ill-fitting tough-guy roles in “Trance” and “Welcome to the Punch” earlier this year. Casting across the board is playfully savvy: It’s particularly amusing to see Bell and his “Billy Elliot” dad, Gary Lewis, reunited as equally dim-witted colleagues.

In effective contrast to the bleak goings-on, the technical contributions are positively zesty. Matthew Jensen’s bright, restless lensing opts only for exaggerated graininess during a grimy lads’ weekend in Hamburg; Clint Mansell’s typically ambient scoring often cedes the spotlight to knowingly tacky pop selections on the soundtrack. Costume designer Guy Speranza evidently had fun kitting McAvoy out in a peacockish range of clashing shirt-and-tie combinations — like much else in “Filth,” all in the best bad taste.

Film Review: ‘Filth’

<p dir="ltr">Reviewed at Vue Islington, London, Oct. 29, 2013. Running time: <strong>97 MIN.</strong></p>

  • Production: <p dir="ltr">(U.K.-Belgium-Germany-Sweden) A Lionsgate (in U.K.) release of a Logie Pictures, Steel Mill Pictures production in association with Maven Pictures, Film House Germany, Altitude Film Entertainment, Lionsgate U.K., Mollywood, Creative Scotland. (International sales: Creative Artists Agency, Los Angeles.) Produced by Ken Marshall, Jon S. Baird, Trudie Styler, Jens Meurer, Celine Rattray, Will Clarke, James McAvoy, Christian Angermayer, Mark Amin, Stephen Mao. Executive producers, Mark Hansell, Charles E. Bush, Jr., Irvine Welsh, M.H. Dastmaltchi, Pierre Lorinet, Benjamin Melkman, Yasin Qureshi, Jean-Pierre Valentini, Jon Harris, Zygi Kamasa, Guy Avshalom, Nicolas Meyer, Marc Schaberg, Matt Petzny, Jane L. Bruce, Stefan Haller, Robin Houcken, Ralph Dietrich, Karin Dietrich, Stephan Giger. Co-producers, Berry van Zwieten, Sean Wheelan, Jessica Ask, Benoit Roland.</p>
  • Crew: <p>Directed, written by Jon S. Baird, based on the novel by Irvine Welsh. Camera (color, widescreen), Matthew Jensen; editor, Mark Eckersley; music, Clint Mansell; music supervisor, Matt Biffa; production designer, Mike Gunn; art director, Moley Campbell; set decorator, Clare Pidgeon; costume designer, Guy Speranza; sound (Dolby Digital), Dirk Bombey; supervising sound editor, Joakim Sundstrom; re-recording mixer, Per Bostrom; visual effects supervisor, Hakan Blomdahl; visual effects, Filmgate; stunt coordinator, Marco Maas; line producer, Alex O'Neal; associate producer, Rachel Dargavel; assistant director, David Gilchrist; casting, Kahleen Crawford, Danny Jackson, Janet Hirshenson, Jane Jenkins.</p>
  • With: <p dir="ltr">James McAvoy, Eddie Marsan, Jamie Bell, Joanne Froggatt, Shirley Henderson, Imogen Poots, Emun Elliott, Martin Compston, Gary Lewis, Shauna Macdonald, John Sessions, Katie Dickie, Jim Broadbent, Brian McCardle, Iain De Caestecker, Joy McAvoy, Jordan Young, Pollyanna McIntosh, Bobby Rainsbury.</p>