Skip to main content

threers_codec/decode/av1/
mod.rs

1//! AV1 decoder: a port of dav1d 1.5.1 (single-threaded C paths), the
2//! decoder the AV1 conformance suite and libaom agree with bit for bit.
3//!
4//! [`Av1Decoder::decode`] takes one temporal unit (an IVF/WebM/MP4 sample:
5//! a sequence of OBUs) and returns the pictures it shows, film grain
6//! applied.
7
8// The arithmetic mirrors dav1d's C expressions term for term.
9#![allow(clippy::precedence)]
10
11pub mod cdf;
12// Field names follow dav1d's `CdfContext` (`classN`, ...).
13#[allow(non_snake_case)]
14pub mod cdf_data;
15pub mod decode;
16pub mod env;
17pub mod fg;
18pub mod filter;
19pub mod frame;
20pub mod intra_edge;
21pub mod ipred;
22pub mod itx;
23pub mod itx_1d;
24pub mod levels;
25pub mod lf_mask;
26pub mod mc;
27pub mod msac;
28pub mod obu;
29pub mod picture;
30pub mod qm;
31pub mod recon;
32pub mod refmvs;
33mod sched;
34#[allow(dead_code)]
35pub mod tables;
36pub mod warpmv;
37pub mod wedge;
38
39use std::sync::Arc;
40
41use cdf_data::CdfContext;
42use frame::{Pending, RefSlot, SlotEntry, TileData};
43use obu::*;
44pub use picture::Picture;
45
46/// A shown picture.
47pub struct Av1Frame {
48    /// The reconstructed picture, film grain applied.
49    pub pic: Arc<Picture>,
50    /// The frame header it was decoded with.
51    pub hdr: Arc<FrameHeader>,
52    /// The sequence header in force.
53    pub seq: Arc<SeqHeader>,
54}
55
56impl Av1Frame {
57    /// As a [`DecodedFrame`](crate::decode::DecodedFrame): planes
58    /// tightly packed, the sequence header's `matrix_coefficients` as the
59    /// colour matrix (0 with identity = RGB, planes G, B, R).
60    pub fn to_decoded(&self) -> crate::decode::DecodedFrame {
61        use crate::decode::{ChromaFormat, ColorInfo, DecodedFrame};
62        let p = &self.pic;
63        let chroma = match p.layout {
64            Layout::I400 => ChromaFormat::Monochrome,
65            Layout::I420 => ChromaFormat::Yuv420,
66            Layout::I422 => ChromaFormat::Yuv422,
67            Layout::I444 => ChromaFormat::Yuv444,
68        };
69        let plane = |pl: usize| -> Vec<u16> {
70            if pl > 0 && p.layout == Layout::I400 {
71                return Vec::new();
72            }
73            let (sx, sy) = p.ss(pl);
74            let (w, h) = (((p.w + sx) >> sx) as usize, ((p.h + sy) >> sy) as usize);
75            let s = p.pstride(pl);
76            let mut v = Vec::with_capacity(w * h);
77            for y in 0..h {
78                v.extend_from_slice(&p.planes[pl][y * s..y * s + w]);
79            }
80            v
81        };
82        DecodedFrame {
83            width: p.w as u32,
84            height: p.h as u32,
85            bit_depth: p.bpc as u8,
86            chroma,
87            y: plane(0),
88            u: plane(1),
89            v: plane(2),
90            color: ColorInfo::from_vui(
91                Some(self.seq.mtrx as u32),
92                self.seq.color_range,
93                p.h as u32,
94            ),
95        }
96    }
97}
98
99/// A hardware back end for [`Av1Decoder`]: the decoder parses the OBUs and
100/// keeps the reference slots; the accelerator reconstructs each frame. Film
101/// grain stays with the decoder, applied to what the accelerator reads back,
102/// so the output is the native decoder's either way.
103pub trait Av1Accel {
104    /// Decode one frame.
105    fn frame(&mut self, f: &AccelFrame<'_>) -> Result<(), String>;
106    /// The samples of a picture it decoded (before film grain).
107    fn read(&mut self, pic: &Picture) -> Result<Picture, String>;
108    /// [`read`](Self::read) for a picture shown without film grain, whose
109    /// samples nothing here touches again: an accelerator that hands
110    /// pictures on its own way may keep this one and return an empty
111    /// picture. By default, `read`.
112    fn read_shown(&mut self, pic: &Picture) -> Result<Picture, String> {
113        self.read(pic)
114    }
115}
116
117/// Everything an [`Av1Accel`] is told about one frame.
118pub struct AccelFrame<'a> {
119    /// The sequence header in force.
120    pub seq: &'a SeqHeader,
121    /// The frame header.
122    pub hdr: &'a FrameHeader,
123    /// The tile groups: their tile data (tile sizes included) and tile range.
124    pub tiles: &'a [TileData],
125    /// The reference slots as the frame finds them.
126    pub refs: &'a [Option<RefSlot>; 8],
127    /// The picture being decoded (size and format; no samples).
128    pub cur: &'a Arc<Picture>,
129}
130
131/// An AV1 decoder (a port of dav1d 1.5.1): feed it temporal units with
132/// [`decode`](Self::decode) (Section 5 OBUs, as in IVF, MP4 and Matroska) or
133/// [`decode_annexb`](Self::decode_annexb), and it returns the frames they show.
134#[derive(Default)]
135pub struct Av1Decoder {
136    seq: Option<Arc<SeqHeader>>,
137    refs: [Option<SlotEntry>; 8],
138    frame_hdr: Option<FrameHeader>,
139    tiles: Vec<TileData>,
140    n_tiles: u32,
141    operating_point_idc: u16,
142    accel: Option<Box<dyn Av1Accel>>,
143    /// Lent to each frame for its tile-parallel decode (`Fd::tile_pool`).
144    tile_pool: Vec<decode::TileBufs>,
145    /// Takes frames during [`decode_each`](Self::decode_each), to be decoded
146    /// concurrently; `None` decodes each as it is parsed.
147    sched: Option<Arc<sched::Sched>>,
148}
149
150/// A frame parsed and waiting to be decoded: its tile data and the
151/// reference slots as they stood before it.
152pub(crate) struct FrameJob {
153    seq: Arc<SeqHeader>,
154    hdr: Arc<FrameHeader>,
155    tiles: Vec<TileData>,
156    refs: [Option<SlotEntry>; 8],
157    done: Arc<Pending>,
158}
159
160/// A frame whose tiles are decoded and loop filters are not yet run.
161struct Reconstructed {
162    fd: decode::Fd,
163    in_cdf: Arc<CdfContext>,
164    out_cdf: Option<CdfContext>,
165}
166
167impl FrameJob {
168    /// Which slots this frame reads: the seven it predicts from and the one
169    /// its CDFs and segment map come from.
170    fn needed_slots(&self) -> [bool; 8] {
171        let mut need = [false; 8];
172        if self.hdr.is_inter_or_switch() {
173            for &i in &self.hdr.refidx {
174                need[i as usize] = true;
175            }
176        }
177        if self.hdr.primary_ref_frame != PRIMARY_REF_NONE {
178            need[self.hdr.refidx[self.hdr.primary_ref_frame as usize] as usize] = true;
179        }
180        need
181    }
182
183    /// The frames that must finish before this one starts.
184    fn dependencies(&self) -> Vec<Arc<Pending>> {
185        let need = self.needed_slots();
186        (0..8)
187            .filter(|&i| need[i])
188            .filter_map(|i| self.refs[i].as_ref().map(|e| e.done.clone()))
189            .collect()
190    }
191
192    /// The slots this frame reads, each waited for.
193    fn resolve_refs(&self) -> Result<[Option<RefSlot>; 8], String> {
194        let need = self.needed_slots();
195        let mut out: [Option<RefSlot>; 8] = Default::default();
196        for i in 0..8 {
197            if let (true, Some(e)) = (need[i], &self.refs[i]) {
198                out[i] = Some(e.done.wait()?);
199            }
200        }
201        Ok(out)
202    }
203
204    /// Entropy decoding and reconstruction, with `pool` lent for the tiles.
205    fn reconstruct(&self, pool: &mut Vec<decode::TileBufs>) -> Result<Reconstructed, String> {
206        let refs = self.resolve_refs()?;
207        let (mut fd, in_cdf) = decode::Fd::new(self.seq.clone(), self.hdr.clone(), &refs)?;
208        fd.tile_pool = std::mem::take(pool);
209        let out_cdf = fd.decode_tiles(&in_cdf, &self.tiles);
210        *pool = std::mem::take(&mut fd.tile_pool);
211        Ok(Reconstructed {
212            fd,
213            in_cdf,
214            out_cdf: out_cdf?,
215        })
216    }
217}
218
219impl Reconstructed {
220    /// Loop filters, then what a reference slot keeps of the frame.
221    fn finish(mut self) -> RefSlot {
222        self.fd.apply_filters();
223        let fd = &mut self.fd;
224        let hdr = fd.hdr.clone();
225        let cdf = if hdr.refresh_context {
226            Arc::new(self.out_cdf.unwrap_or(*self.in_cdf))
227        } else {
228            self.in_cdf
229        };
230        let pic = Arc::new(match fd.sr_cur.take() {
231            Some(sr) => sr,
232            None => std::mem::replace(&mut fd.cur, Picture::new(1, 1, fd.layout, fd.bpc)),
233        });
234        let segmap = fd.cur_segmap.take().map(Arc::new);
235        let refmvs = if (hdr.is_inter_or_switch() || hdr.allow_intrabc) && !hdr.allow_intrabc {
236            fd.rf.take().map(|rf| Arc::new(rf.rp))
237        } else {
238            None
239        };
240        RefSlot {
241            pic,
242            hdr: hdr.clone(),
243            seq: fd.seq.clone(),
244            cdf,
245            segmap,
246            refmvs,
247            refpoc: fd.refpoc,
248            showable: hdr.showable_frame,
249        }
250    }
251}
252
253/// `f`, with a panic turned into an error. A frame thread must always fill
254/// in its frame's [`Pending`]: later frames are blocked on it, and a corrupt
255/// stream that panics the decoder would otherwise hang them for good.
256fn caught<T>(f: impl FnOnce() -> Result<T, String>) -> Result<T, String> {
257    std::panic::catch_unwind(std::panic::AssertUnwindSafe(f))
258        .unwrap_or_else(|_| Err("frame decode panicked".into()))
259}
260
261/// A header with its global motion models' shear parameters filled in, as
262/// `Fd::new` makes it; the slots keep this form for later frames to parse
263/// against.
264fn with_shear(hdr: &Arc<FrameHeader>) -> Arc<FrameHeader> {
265    let mut h = (**hdr).clone();
266    for g in h.gmv.iter_mut() {
267        let _ = warpmv::get_shear_params(g);
268    }
269    Arc::new(h)
270}
271
272const OBU_SEQ_HDR: u32 = 1;
273const OBU_TD: u32 = 2;
274const OBU_FRAME_HDR: u32 = 3;
275const OBU_TILE_GRP: u32 = 4;
276const OBU_FRAME: u32 = 6;
277const OBU_REDUNDANT_FRAME_HDR: u32 = 7;
278
279impl Av1Decoder {
280    /// A decoder waiting for a sequence header.
281    pub fn new() -> Self {
282        Self::default()
283    }
284
285    /// Hand reconstruction to `accel` (see [`Av1Accel`]).
286    pub fn set_accel(&mut self, accel: Box<dyn Av1Accel>) {
287        self.accel = Some(accel);
288    }
289
290    /// Shown pictures from the accelerator into memory, then film grain.
291    fn finish_output(&mut self, out: &mut [Av1Frame]) -> Result<(), String> {
292        for f in out.iter_mut() {
293            if let Some(a) = self.accel.as_mut() {
294                f.pic = Arc::new(if f.hdr.film_grain_present {
295                    a.read(&f.pic)?
296                } else {
297                    a.read_shown(&f.pic)?
298                });
299            }
300            if f.hdr.film_grain_present {
301                f.pic = Arc::new(fg::apply_grain(&f.pic, &f.hdr.film_grain, &f.seq));
302            }
303        }
304        Ok(())
305    }
306
307    fn ref_headers(&self) -> RefHeaders {
308        let mut r: RefHeaders = Default::default();
309        for (i, s) in self.refs.iter().enumerate() {
310            r[i] = s.as_ref().map(|s| s.hdr.clone());
311        }
312        r
313    }
314
315    /// Decode one temporal unit (low-overhead OBU format).
316    pub fn decode(&mut self, data: &[u8]) -> Result<Vec<Av1Frame>, String> {
317        let shown = self.parse_temporal_unit(data)?;
318        self.output(shown)
319    }
320
321    /// Every OBU of one temporal unit; returns the frames it shows.
322    fn parse_temporal_unit(&mut self, data: &[u8]) -> Result<Vec<Arc<Pending>>, String> {
323        let mut out = Vec::new();
324        let mut pos = 0;
325        while pos < data.len() {
326            pos += self.parse_obu(&data[pos..], &mut out)?;
327        }
328        Ok(out)
329    }
330
331    /// The pictures a temporal unit shows, once decoded: one per unit, the
332    /// last (highest spatial layer), as libaom with output_all_layers=0.
333    fn output(&mut self, mut shown: Vec<Arc<Pending>>) -> Result<Vec<Av1Frame>, String> {
334        if shown.len() > 1 {
335            shown.drain(..shown.len() - 1);
336        }
337        let mut out = Vec::with_capacity(shown.len());
338        for p in shown {
339            let s = p.wait()?;
340            out.push(Av1Frame {
341                pic: s.pic,
342                hdr: s.hdr,
343                seq: s.seq,
344            });
345        }
346        self.finish_output(&mut out)?;
347        Ok(out)
348    }
349
350    /// Decode many temporal units (low-overhead OBU format), up to `threads`
351    /// frames at once, and give each shown frame to `each` in order, with the
352    /// index of its unit. The frames are those calling
353    /// [`decode`](Self::decode) on each unit would return.
354    ///
355    /// A unit that will not decode is skipped, as a player would: real files
356    /// have damaged frames, and the rest are still worth having. Returns how
357    /// many units gave up a frame that way, or the first error if not one
358    /// frame could be shown. An error from `each` stops at once.
359    ///
360    /// Frames run on the `parallel` feature's thread pool as their references
361    /// finish, and only a window of units is in flight, so memory does not
362    /// grow with the stream. How much this helps depends on the stream: an
363    /// encoder's layered frame structure leaves frames that do not reference
364    /// each other, and a strict chain leaves none.
365    ///
366    /// This call blocks until frames are done, which a browser forbids on its
367    /// main thread: from a threaded wasm build, call it in a Worker. It
368    /// decodes one frame at a time where no pool can help: without `parallel`
369    /// or a second pool thread, from inside the pool, or with an accelerator.
370    pub fn decode_each(
371        &mut self,
372        units: &[&[u8]],
373        threads: usize,
374        mut each: impl FnMut(usize, Av1Frame) -> Result<(), String>,
375    ) -> Result<usize, String> {
376        let (mut shown, mut skipped, mut first_error) = (0usize, 0usize, None);
377        let mut failed = |e: String, first_error: &mut Option<String>| {
378            skipped += 1;
379            first_error.get_or_insert(e);
380        };
381        let concurrent = self.accel.is_none() && threads >= 2 && crate::pool_can_serve_caller();
382        if !concurrent {
383            for (i, u) in units.iter().enumerate() {
384                match self.decode(u) {
385                    Ok(frames) => {
386                        for f in frames {
387                            shown += 1;
388                            each(i, f)?;
389                        }
390                    }
391                    Err(e) => failed(e, &mut first_error),
392                }
393            }
394        } else {
395            let sched = sched::Sched::new(threads, std::mem::take(&mut self.tile_pool));
396            self.sched = Some(sched.clone());
397            // Far enough ahead to reach the frames that can run side by side:
398            // a unit's shown frame can need several hidden ones decoded first.
399            let window = threads * 4;
400            let mut waiting = std::collections::VecDeque::new();
401            let mut run = || -> Result<(), String> {
402                let mut deliver = |this: &mut Self,
403                                   (i, unit): (usize, Vec<Arc<Pending>>)|
404                 -> Result<(), String> {
405                    match this.output(unit) {
406                        Ok(frames) => {
407                            for f in frames {
408                                shown += 1;
409                                each(i, f)?;
410                            }
411                        }
412                        Err(e) => failed(e, &mut first_error),
413                    }
414                    Ok(())
415                };
416                for (i, u) in units.iter().enumerate() {
417                    match self.parse_temporal_unit(u) {
418                        Ok(s) => waiting.push_back((i, s)),
419                        Err(e) => deliver(self, (i, vec![Pending::ready(Err(e))]))?,
420                    }
421                    while waiting.len() > window {
422                        deliver(self, waiting.pop_front().unwrap())?;
423                    }
424                }
425                while let Some(unit) = waiting.pop_front() {
426                    deliver(self, unit)?;
427                }
428                Ok(())
429            };
430            let r = run();
431            self.sched = None;
432            if r.is_err() {
433                sched.cancel();
434            }
435            let (pool, hidden_error) = sched.finish();
436            self.tile_pool = pool;
437            r?;
438            if let Some(e) = hidden_error {
439                first_error.get_or_insert(e);
440            }
441        }
442        match first_error {
443            Some(e) if shown == 0 => Err(e),
444            _ => Ok(skipped),
445        }
446    }
447
448    /// [`decode_each`](Self::decode_each), collecting the frames.
449    pub fn decode_batch(
450        &mut self,
451        units: &[&[u8]],
452        threads: usize,
453    ) -> Result<Vec<Av1Frame>, String> {
454        let mut out = Vec::new();
455        self.decode_each(units, threads, |_, f| {
456            out.push(f);
457            Ok(())
458        })?;
459        Ok(out)
460    }
461
462    /// Decode one Annex B `temporal_unit()` payload (frame units of
463    /// length-prefixed OBUs).
464    pub fn decode_annexb(&mut self, tu: &[u8]) -> Result<Vec<Av1Frame>, String> {
465        let mut obus = Vec::new();
466        let mut p = 0;
467        while p < tu.len() {
468            let (fsz, n) = leb128(&tu[p..])?;
469            p += n;
470            let fu = tu.get(p..p + fsz).ok_or("frame unit overrun")?;
471            p += fsz;
472            let mut q = 0;
473            while q < fu.len() {
474                let (osz, n) = leb128(&fu[q..])?;
475                q += n;
476                obus.push(fu.get(q..q + osz).ok_or("OBU overrun")?);
477                q += osz;
478            }
479        }
480        let mut shown = Vec::new();
481        for o in obus {
482            self.parse_obu(o, &mut shown)?;
483        }
484        self.output(shown)
485    }
486
487    /// `dav1d_parse_obus` for one OBU; returns the bytes consumed.
488    fn parse_obu(&mut self, data: &[u8], out: &mut Vec<Arc<Pending>>) -> Result<usize, String> {
489        let mut gb = GetBits::new(data);
490        gb.bit();
491        let typ = gb.bits(4);
492        let has_extension = gb.flag();
493        let has_length_field = gb.flag();
494        gb.bit();
495        let (mut temporal_id, mut spatial_id) = (0, 0);
496        if has_extension {
497            temporal_id = gb.bits(3) as u8;
498            spatial_id = gb.bits(2) as u8;
499            gb.bits(3);
500        }
501        let start = gb.byte_pos();
502        let end = if has_length_field {
503            let len = gb.uleb128() as usize;
504            let s = gb.byte_pos();
505            if len > data.len() - s {
506                return Err("OBU length overrun".into());
507            }
508            s + len
509        } else {
510            data.len()
511        };
512        let _ = start;
513        let payload_start = gb.byte_pos();
514        let payload = &data[payload_start..end];
515
516        if typ != OBU_SEQ_HDR && typ != OBU_TD && has_extension && self.operating_point_idc != 0 {
517            let in_t = (self.operating_point_idc >> temporal_id) & 1;
518            let in_s = (self.operating_point_idc >> (spatial_id + 8)) & 1;
519            if in_t == 0 || in_s == 0 {
520                return Ok(end);
521            }
522        }
523
524        match typ {
525            OBU_SEQ_HDR => {
526                let mut g = GetBits::new(payload);
527                let seq = parse_seq_hdr(&mut g)?;
528                self.operating_point_idc = seq.operating_points[0].idc;
529                let same = self
530                    .seq
531                    .as_ref()
532                    .map(|s| s.same_sequence(&seq))
533                    .unwrap_or(false);
534                if !same {
535                    if self.seq.is_some() {
536                        self.refs = Default::default();
537                    }
538                    self.frame_hdr = None;
539                }
540                self.seq = Some(Arc::new(seq));
541            }
542            OBU_REDUNDANT_FRAME_HDR if self.frame_hdr.is_some() => {}
543            OBU_FRAME | OBU_FRAME_HDR | OBU_REDUNDANT_FRAME_HDR => {
544                let seq = self.seq.clone().ok_or("frame before sequence header")?;
545                let mut g = GetBits::new(payload);
546                let hdr =
547                    parse_frame_hdr(&seq, &self.ref_headers(), &mut g, temporal_id, spatial_id)?;
548                self.tiles.clear();
549                self.n_tiles = 0;
550                let show_existing = hdr.show_existing_frame;
551                self.frame_hdr = Some(hdr);
552                if typ == OBU_FRAME {
553                    if show_existing {
554                        return Err("OBU_FRAME with show_existing_frame".into());
555                    }
556                    g.byte_align();
557                    let rest = &payload[g.byte_pos()..];
558                    self.tile_group(rest)?;
559                }
560            }
561            OBU_TILE_GRP => {
562                if self.frame_hdr.is_none() {
563                    return Err("tile group without frame header".into());
564                }
565                self.tile_group(payload)?;
566            }
567            _ => {}
568        }
569
570        if let (Some(seq), Some(hdr)) = (self.seq.clone(), self.frame_hdr.as_ref()) {
571            if hdr.show_existing_frame {
572                let idx = hdr.existing_frame_idx as usize;
573                let entry = self.refs[idx]
574                    .clone()
575                    .ok_or("show_existing_frame of an empty slot")?;
576                out.push(entry.done.clone());
577                if entry.hdr.frame_type == FRAME_KEY {
578                    // Showing a key frame again refreshes every slot with it,
579                    // which needs its decoded data: wait for it here.
580                    let slot = entry.done.wait()?;
581                    let mut s = slot.clone();
582                    s.showable = false;
583                    for i in 0..8 {
584                        let mut c = s.clone();
585                        if i != idx {
586                            c.refmvs = None;
587                        } else {
588                            c.refmvs = slot.refmvs.clone();
589                        }
590                        self.refs[i] = Some(SlotEntry {
591                            hdr: entry.hdr.clone(),
592                            seq: entry.seq.clone(),
593                            done: Pending::ready(Ok(c)),
594                        });
595                    }
596                }
597                self.frame_hdr = None;
598            } else if self.n_tiles == hdr.tiling.cols as u32 * hdr.tiling.rows as u32 {
599                let hdr = Arc::new(self.frame_hdr.take().unwrap());
600                let tiles = std::mem::take(&mut self.tiles);
601                self.n_tiles = 0;
602                self.submit_frame(seq, hdr, tiles, out)?;
603            }
604        }
605        Ok(end)
606    }
607
608    fn tile_group(&mut self, data: &[u8]) -> Result<(), String> {
609        let hdr = self.frame_hdr.as_ref().unwrap();
610        let mut g = GetBits::new(data);
611        let (s, e) = parse_tile_hdr(hdr, &mut g);
612        g.byte_align();
613        if s > e || s != self.n_tiles {
614            return Err("tile groups out of order".into());
615        }
616        self.tiles.push(TileData {
617            data: data[g.byte_pos()..].to_vec(),
618            start: s,
619            end: e,
620        });
621        self.n_tiles += 1 + e - s;
622        Ok(())
623    }
624
625    fn submit_frame(
626        &mut self,
627        seq: Arc<SeqHeader>,
628        hdr: Arc<FrameHeader>,
629        tiles: Vec<TileData>,
630        out: &mut Vec<Arc<Pending>>,
631    ) -> Result<(), String> {
632        let done = Pending::new();
633        let refs = self.refs.clone();
634        let entry = SlotEntry {
635            hdr: with_shear(&hdr),
636            seq: seq.clone(),
637            done: done.clone(),
638        };
639        for i in 0..8 {
640            if hdr.refresh_frame_flags & (1 << i) != 0 {
641                self.refs[i] = Some(entry.clone());
642            }
643        }
644        if hdr.show_frame {
645            out.push(done.clone());
646        }
647        let job = FrameJob {
648            seq,
649            hdr,
650            tiles,
651            refs,
652            done,
653        };
654        if let Some(sched) = &self.sched {
655            sched.submit(job);
656            return Ok(());
657        }
658        let r = if let Some(accel) = self.accel.as_mut() {
659            let refs =
660                std::array::from_fn(|i| job.refs[i].as_ref().and_then(|e| e.done.wait().ok()));
661            let (seq, hdr) = (&job.seq, &job.hdr);
662            let pic = Arc::new(Picture {
663                planes: Default::default(),
664                stride: [0; 2],
665                w: hdr.width[1],
666                h: hdr.height,
667                layout: seq.layout,
668                bpc: 8 + 2 * seq.hbd as u32,
669            });
670            accel
671                .frame(&AccelFrame {
672                    seq,
673                    hdr,
674                    tiles: &job.tiles,
675                    refs: &refs,
676                    cur: &pic,
677                })
678                .map(|()| {
679                    let cdf = Arc::new(*CdfContext::default_for(hdr.quant.yac));
680                    RefSlot {
681                        pic,
682                        hdr: hdr.clone(),
683                        seq: seq.clone(),
684                        cdf,
685                        segmap: None,
686                        refmvs: None,
687                        refpoc: [0; 7],
688                        showable: hdr.showable_frame,
689                    }
690                })
691        } else {
692            let pool = &mut self.tile_pool;
693            caught(|| job.reconstruct(pool).map(Reconstructed::finish))
694        };
695        job.done.set(r.clone());
696        r.map(|_| ())
697    }
698}
699
700/// Split an Annex B stream into `temporal_unit()` payloads.
701pub fn split_annexb(data: &[u8]) -> Result<Vec<&[u8]>, String> {
702    let mut v = Vec::new();
703    let mut p = 0;
704    while p < data.len() {
705        let (sz, n) = leb128(&data[p..])?;
706        p += n;
707        v.push(data.get(p..p + sz).ok_or("temporal unit overrun")?);
708        p += sz;
709    }
710    Ok(v)
711}
712
713fn leb128(d: &[u8]) -> Result<(usize, usize), String> {
714    let mut v = 0usize;
715    for (i, &b) in d.iter().enumerate().take(8) {
716        v |= ((b & 0x7f) as usize) << (7 * i);
717        if b & 0x80 == 0 {
718            return Ok((v, i + 1));
719        }
720    }
721    Err("bad leb128".into())
722}