Compare commits

...
117 Commits
Author SHA1 Message Date
FredApps 930e1694b2 Release: DeckCraft v0.19.0 (main b0c77ebd6a) 2026-11-14 21:44:54 +01:00
FredApps 28efbec23d Release: DeckCraft v0.19.0 2026-11-14 21:44:49 +01:00
autocraft-bot[bot] b0c77ebd6a Merge pull request #1961 from Naseq/fix-1954-wasm-dead-code
ui-egui: cfg `opening_generation` off wasm32 and lint the wasm set with -D warnings (#1954)
2026-11-14 09:33:39 +00:00
Naseq 68a4190f95 Leave the xtask wasm gate unchanged
The CI gate change (wasm step from check to clippy -D warnings) belongs in
its own CI task; this PR keeps only the opening_generation cfg fix (#1954).
2026-11-14 13:52:49 +04:30
FredAppsandFredApps 69379cf689 Naming: use Diagram and Decorative Text for own features (#1981)
* Naming: use Diagram and Decorative Text for own features

* test(naming): guard neutral display labels

* test(ui): handle escaped literals in feature-name guard

---------

Co-authored-by: FredApps <[email protected]>
2026-11-14 01:29:01 +01:00
Vincent Cloutierandcloutier 7a24b20c8e Document the PPTX slide-list import limit (#2103)
* M1951: document the PPTX slide-list import limit

* M1951: expose the slide-list limit in public API docs

---------

Co-authored-by: cloutier <[email protected]>
2026-11-13 16:28:51 +00:00
FredApps ef9ac9ac7a Merge pull request #2062 from cloutier/fix/2060-large-adjustment-guides
PPTX: preserve large connector adjustment guides
2026-11-13 10:41:19 +01:00
FredApps eebede8ee4 Merge pull request #2058 from cloutier/fix/empty-master-text-styles
PPTX: preserve empty imported master text styles
2026-11-13 10:41:00 +01:00
FredApps a024a0e86c Merge pull request #1978 from aiongg/autocraft/fix-1977-pptx-bar-chart-series-level-c
Fix pPTX bar chart: series-level `<c:dLbls><c:delete val="1"/></c:dLbls>`
2026-11-13 10:40:24 +01:00
FredApps 9850dc6088 Merge pull request #1973 from scks1/fix/ungroup-nested-group
engine: arrange.ungroup frees a group nested in another group
2026-11-13 10:40:04 +01:00
FredApps 3ce1886bea Merge pull request #1972 from scks1/fix/duplicate-group-member
engine: edit.duplicate copies a shape inside a group into its group
2026-11-13 10:39:35 +01:00
FredApps 0a75620a73 Merge pull request #1968 from scks1/fix/copy-group-member-slide-box
engine: a shape copied from a group pastes where it was drawn
2026-11-13 10:39:13 +01:00
FredApps 74a0468917 Merge pull request #1967 from scks1/fix/merge-refuses-group-members
engine: shape.merge refuses a shape inside a group
2026-11-13 10:38:20 +01:00
FredApps 6166cb0870 Merge pull request #1951 from cloutier/fix/1910-slide-import-integrity
PPTX: report missing slide relationships and refuse over-limit lists
2026-11-13 10:36:39 +01:00
FredApps d2ee3ca9fc Merge pull request #1941 from neilcawse/fix/pptx-paragraph-build-inference
pptx: infer paragraph builds when bldP does not disable them
2026-11-13 10:36:06 +01:00
FredApps 506db24b07 Merge pull request #1966 from Silvertree2010/fix/h11008-delete-key-removes-only-the-firs
Delete a whole grapheme cluster with the forward Delete key
2026-11-13 10:34:40 +01:00
FredApps 7f9acc1c27 Merge pull request #2055 from cloutier/fix/picture-inherited-group-fill
PPTX: keep inherited group backing on transparent pictures
2026-11-13 10:27:28 +01:00
FredApps 3a92bcc93c Merge pull request #1915 from adikshan11/fix/automation-roots-exact-names
Automation roots: compare names exactly and refuse COM¹ to LPT³
2026-11-13 10:26:08 +01:00
Dylan M. Taylor 18355eeca2 Merge pull request #2064 from storytold/switch-metafile-craft-metafile
Metafile: read WMF, EMF and EMF+ with craft-metafile
2026-11-13 02:47:12 -05:00
Dylan M. Taylor 15a75a2662 Update pinned shared PDF revision check 2026-11-13 07:33:26 +00:00
Dylan M. Taylor 2f8398d87b Pin shared metafile updates from craft-libs 2026-11-13 07:13:55 +00:00
Dylan M. Taylor 718b6da035 Fix metafile clip coverage sample 2026-11-13 07:05:21 +00:00
Dylan M. Taylor f78a805ac7 Cover metafile transform and pen paths 2026-11-13 07:01:52 +00:00
Dylan M. Taylor ec2937f298 Merge remote-tracking branch 'origin/main' into codex/nextest-cli-bin-path
# Conflicts:
#	crates/pptx/src/opc.rs
2026-11-13 06:29:55 +00:00
Dylan M. Taylor 5e771693fc Merge pull request #2096 from storytold/codex/sonar-font-pin
ci: provide fonts to Sonar coverage tests
2026-11-13 01:28:51 -05:00
Dylan M. Taylor 4bd8e2a25e test(cli): resolve executable from nextest environment 2026-11-13 06:08:50 +00:00
Dylan M. Taylor 17223d0919 fix(pptx): simplify sidecar part predicate 2026-11-13 05:37:45 +00:00
Dylan M. Taylor c7fb94b974 ci: provide fonts to Sonar coverage tests 2026-11-13 05:30:15 +00:00
Dylan M. Taylor 1d584737b1 test(cli): resolve executable from nextest environment 2026-11-13 05:07:40 +00:00
Dylan M. Taylor b8c19475d2 Merge main and pin shared metafile candidate 2026-11-13 02:44:18 +00:00
Adithya Shankaran 2d1e6ff348 Drop the repeated test comment and show one backslash in the docs
The case-sensitive Windows test already says what it checks. In the MCP docs the OUT example is a code span, so one backslash is what a reader sees.
2026-11-12 12:10:41 +00:00
Dylan M. Taylorandautocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com> 9b1fe3ff23 PPTX: evaluate a custom geometry's guide lists once per coordinate space
* PPTX: evaluate a custom geometry's guide lists once per coordinate space

* address review: exempt guide-free geometries from the space cap

* address review: budget guide-name bytes cached across coordinate spaces

* Apply review fixes

- crates/pptx/src/custgeom.rs

* fix(pptx): warn when custom geometry paths are left out for the guide-cache limits

---------

Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
2026-11-12 06:28:38 -05:00
Dylan M. Taylor 653f596958 Chart render: budget category-by-series cells
* Chart render: budget category-by-series cells before drawing

* fix(render): chart cell budget is Excel's 255 series x 32,000 points
2026-11-12 06:28:30 -05:00
Dylan M. Taylorandautocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com> 129efb16bd fix(pptx): bound the chartEx writer's caches and series
* fix(pptx): bound the chartEx writer's caches and series

* Apply review fixes

- crates/pptx/src/write/chart.rs

---------

Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
2026-11-12 06:28:24 -05:00
autocraft-bot[bot] b8983ae65c Merge pull request #2075 from Hamza0101/fix/legend-entry-text
Charts: legend entries keep their own text size, weight and colour (#2067)
2026-11-12 10:04:49 +00:00
Hamza 8f93cd9099 Charts: the legend entry test checks the size as written, not only as read back 2026-11-12 14:49:36 +05:00
Hamza 0e0d7f8cb2 Charts: a legend entry's paragraph text wins over its list style
Review fixes: an entry's size, weight and colour are read from its
paragraph (a:p) first and from a:lstStyle only where the paragraph
sets none, as DrawingML inherits them; the writer keeps sizes to the
reader's 1-400 pt, so a size reads back as written; and the legend
draws an entry at its own size up to 400 pt rather than up to 8 times
the legend's.
2026-11-12 14:48:11 +05:00
autocraft-bot[bot] b5b77eb27e Apply review fixes
- crates/engine/src/cmd/insert/chart.rs
2026-11-12 08:01:45 +00:00
autocraft-bot[bot] b73a53df3d Apply review fixes
- crates/engine/src/cmd/format/font.rs
2026-11-12 07:46:39 +00:00
Hamza 1d608bb464 Charts: legend entries keep their own text size, weight and colour
A c:legendEntry can give one legend entry its own text (c:txPr), as
decks do to make legend text white on a dark chart. It was never read,
so every entry drew in the legend's text. The chart model keeps each
entry's size, bold and colour (Chart::legend_entries); the reader
takes them from the entry's c:txPr, the legend draws each name, key
and row at its entry's size and in its weight and colour, and a chart
rebuilt after an edit writes them back before c:overlay, in schema
order. Entries that set none of the three are not kept.

Fixes #2067
2026-11-12 12:20:32 +05:00
Hamza Naveed 6849fa0149 Pictures in a placeholder show the layout placeholder's fill (#2072)
A slide picture with an empty p:spPr in a layout picture placeholder
that sets a fill drew only its pixels: the picture path drew a fill
only when the picture set its own, so the beige frame the layout gives
the placeholder never showed behind a transparent image. A picture
that sets neither a fill nor a style now draws the explicit fill its
layout or master placeholder sets, in the placeholder's geometry. Its
own fill (a:noFill included) and its own style decide as before, and a
style fill alone, inherited or not, stays undrawn.

Fixes #2068
2026-11-12 07:00:59 +00:00
Hamza Naveed 6eebec7df6 Decrease Font Size steps down one size at a time from any size (#2070)
Ctrl+[ (and Cmd+Shift+,) shrank any size above 66 straight to 66: the
step list stopped at 66, so the largest list size below 120 was 66.
The list now holds PowerPoint's sizes up to 96, as the Font Size box
already listed them, and above 96 shrink steps back 8 points at a
time, as grow steps up: 120 -> 112 -> 104 -> 96 -> 88 -> 80 -> 72 ->
66. The Font Size box lists the same SIZES instead of adding its own
tail.

Fixes #2059
2026-11-12 06:16:48 +00:00
Dylan M. Taylor a82e37dee2 Video: decode H.264, HEVC, VP9 and AV1 with craft-video (#2065)
* Video: decode H.264, HEVC, VP9 and AV1 with craft-video; delete crates/h264, hevc, vp9, av1 and bitstream

* Video: pin craft-libs main with the DeckCraft decoder fixes; architecture notes

* Video: pin shared decoders to current craft-libs main
2026-11-12 00:02:30 -05:00
Dylan M. Taylor ac3e46b748 Metafile: read WMF, EMF and EMF+ with craft-metafile; delete crates/metafile
The renderer now draws what the shared reader adds: pen caps, joins and
dashes, clip paths (SELECTCLIPPATH) and turned or skewed bitmaps.
2026-11-12 01:09:45 +00:00
Vincent Cloutier 6a3cbd18d2 PPTX #2060: preserve large adjustment guides 2026-11-11 20:06:47 -05:00
Vincent Cloutier 5914363930 DC002: refuse over-limit PPTX slide lists 2026-11-11 19:32:31 -05:00
Dylan M. Taylor 7cc73ab189 Merge tested main for v0.18.1
# Conflicts:
#	Cargo.lock
2026-11-11 18:40:26 -05:00
Vincent Cloutier e0b0817288 fix(pptx): preserve empty imported master styles (#2057) 2026-11-11 18:33:37 -05:00
autocraft-bot[bot] 63331b8ab6 Apply review fixes
- crates/pptx/src/read.rs
2026-11-11 23:14:10 +00:00
Vincent Cloutier 3975b8cc5e Merge official main to refresh PR1951 2026-11-11 18:02:42 -05:00
Vincent Cloutier db257178f5 fix(pptx): keep pictures inherited group backing (#2054) 2026-11-11 17:43:03 -05:00
FredApps 2127bd1bd8 CI: nightly release dry run (#2052)
release.yml also runs nightly on main with craft-common-actions' dry-run: every release build
except macOS, with nothing signed, pushed or drafted. Breakage that only the release build hits
(riscv64, Flatpak, FreeBSD, packaging) then shows up the next morning instead of on release
day, as it did on 11 Nov 2026.
2026-11-11 23:03:54 +01:00
Dylan M. Taylor eec4f59535 AV1: decode tiles serially when parallel tile metadata copies would be huge (#1841) 2026-11-11 17:00:12 -05:00
Dylan M. Taylorandaiongg 988a492097 PPTX: copy only used, bounded namespace declarations onto imported fragments (#1740)
* pptx: copy only used, bounded namespace declarations onto imported fragments

* address review: index a branch's namespace declarations once, look up each child's prefixes

* address review: oversized declarations keep shadowing; keep MustUnderstand prefixes

* address review: keep declarations for prefixes used in QName attribute values

* Apply review fixes

- crates/pptx/src/read.rs

* address review: keep 400-part cap inclusive for rels part

* address review: budget the namespace bytes copied onto kept fragments per part

* fix test initializer: Part gained ns_budget

---------

Co-authored-by: aiongg <[email protected]>
2026-11-11 16:15:23 -05:00
Dylan M. Taylorandautocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com> 2406ce46ec chore: bump version to 0.18.1 for the Flatpak update fix (#2048)
Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
2026-11-11 15:25:10 -05:00
Dylan M. Taylor 60f91f7806 Video decoders: budget repeated-frame and queued output pictures, drop a failed VP9 sample's outputs (#1778)
* Video decoders: budget repeated-frame and queued output pictures, drop a failed VP9 sample's outputs

* address review: size AV1 repeats by the reference format and after layer selection, budget HEVC queued frames at their own size

* address review: charge the larger of the output queue and the in-flight reservation, not both

* address review: count frames held by the old DPB at their own size in the budget check

* address review: charge queued frames outside the DPB and the in-flight set separately
2026-11-11 15:13:36 -05:00
Dylan M. Taylor 2ef5f31bc8 PPTX export: stop adding chart series once a group's caches pass a point budget (#1804)
* PPTX export: stop adding chart series once a group's caches pass a point budget

* address review: size chart exports from the caches each group writes

* address review: cut each chart data cache at a cap far above Excel's 32,000 points per series, so the first series is bounded without failing export
2026-11-11 15:13:23 -05:00
Dylan M. Taylorandautocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com> c6846f6a75 Photo album: apply the album limits when reopening an album (#1755)
* engine: apply the photo album limits when reopening an album

* address review: refuse an album Update when the editor seed omitted items, instead of deleting them

* Apply review fixes

- crates/engine/src/cmd/insert/album.rs

* address review: refuse an album replace when a slide past the item limit holds other content

* address review: notes and comments on a shapeless slide past the item limit block an album replace

* address review: slide background, transition, animations, colour map and tags count as slide content

---------

Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
2026-11-11 14:59:47 -05:00
Dylan M. Taylorandautocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com> a3f2f215b0 Metafile: stop shading on a full canvas, charge nested pictures and clip regions to the budgets (#1777)
* Metafile: stop shading on a full canvas, charge nested pictures and clip regions to the budgets

* address review: count shared tiles once, include text advances, check clip region before keeping an item

* address review: count EMF+ object bytes against the canvas byte budget

* Apply review fixes

- crates/metafile/src/canvas.rs
- crates/metafile/src/tests.rs

* Apply review fixes

- crates/metafile/src/canvas.rs

* address review: count pieces of a continued EMF+ object against the shared byte budget

* Apply review fixes

- crates/metafile/src/canvas.rs

* address review: count the current and saved clips against the shared byte budget

---------

Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
2026-11-11 14:59:32 -05:00
Dylan M. Taylor 35166b3841 Media: Opus, Ogg, MP4 and Matroska come from craft-libs (#2047)
DeckCraft kept its own Opus decoder (crates/opus) and Ogg demuxer
(crates/ogg), and two crates that only re-exported craft-media
(crates/isobmff, crates/matroska). craft-libs has all four: craft-audio's
opus module and craft-media's ogg, isobmff and matroska modules. The four
crates are removed and crates/media depends on craft-audio and craft-media
directly.

craft-media and craft-audio are pinned at 113f2360, the head of craft-libs
#810, which carries the two Ogg fixes DeckCraft made to its own copy
(51d19baa, 9d36b593). The pin moves to craft-libs main once #810 merges.
craft-pdf, craft-update and craft-ui keep their pins.

One API difference: craft-media's OpusTiming::of saturates on a hostile
granule position instead of returning an error. The end-trim product in
the Ogg Opus decode now saturates as well; a test with a three-channel
file and the largest granule panicked on it without that.

crates/bitstream stays: it also holds the video decoders' memory budget,
which craft-libs does not have.
2026-11-11 14:47:18 -05:00
Dylan M. Taylor b57b4ccfcf PPTX: charge repeated chart decodes and sidecar copies to presentation-wide budgets (#1790)
* PPTX: charge repeated chart decodes and sidecar copies to presentation-wide budgets

* address review: count all chart vectors and relationship strings; reject a chart that crosses the presentation limit

* address review: charge chart part names/content types, roll back rejected capture

* address review: bound chart decode work across rejected and repeated references, cache refused sidecar captures

* address review: keep a chart past the presentation chart limits as its original part and closure, so a save writes it back
2026-11-11 14:33:58 -05:00
Dylan M. Taylor 2fbdcfe5f5 UI: cap live video decoders and bound slide-show grid thumbnail height (#1801)
* UI: cap live video decoders and bound slide-show grid thumbnail height

* address review: retry video feed creation when playback resumes

* address review: reclaim the decoder of ended videos when the feed cap is reached

* test: cover the video decoder cap and its retry

* address review: seeking a video without a decoder retries the bounded decoder allocation
2026-11-11 14:33:44 -05:00
Dylan M. Taylorandautocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com> 98eac5590d Render: bound bevel edge work, shape-gradient flattening and colour-glyph fills (#1786)
* Render: bound bevel edge work, shape-gradient flattening and colour-glyph fills

* address review: skip shape gradient fill when the outline is over the point limit

* address review: keep every contour of a compound bevel or shape gradient at a coarser raster, bound cubic subdivision before flattening

* Apply review fixes

- crates/render/src/pathgrad.rs

* Apply review fixes

- crates/render/src/pathgrad.rs

---------

Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
2026-11-11 14:33:29 -05:00
Dylan M. Taylor 46712d0293 Fonts: honor the disabled system fallback for symbol and script family loads (#1840)
* Fonts: honor the disabled system fallback for symbol and script family loads

* address review: no catalog loading for loaded families when system fallback is off

* test: cascades load no installed family when system fallback is off
2026-11-11 14:33:15 -05:00
Dylan M. Taylor e03c22c445 Notes: place bullets in one pass over the galley rows (#1833)
* Notes: place bullets in one pass over the galley rows

* address review: keep zero-character leading rows in row_span_at
2026-11-11 14:33:01 -05:00
Dylan M. Taylorandaiongg 98208a9eda PDF: gather a paragraph's text once, keep note line widths, count handout rules (#1773)
* pdf: gather a paragraph's text once, keep note line widths, count handout rules

* pdf: spell the rule-count check without a negated comparison

* address review: use a narrow tall page in the bounded-work test

* address review: require the huge-paper export to succeed

* Apply review fixes

- crates/pdf/src/lib.rs

---------

Co-authored-by: aiongg <[email protected]>
2026-11-11 14:32:47 -05:00
Dylan M. Taylor de52b6b766 MCP: bound a batch's entries and the bytes of results it gathers (#1759)
* mcp: bound a batch's entries and the bytes of results it gathers

* address review: charge the escaped batch reply against the budget

* address review: charge batch error entries against the byte budget

* address review: truncate id in batch limit entry

* address review: count the pretty-printed batch reply against the byte cap
2026-11-11 14:32:33 -05:00
Dylan M. Taylor 7c83fa43ac Spelling: check the automation write roots before appending to the user dictionary (#1754)
* engine: check the automation write roots before appending to the user dictionary

* address review: check the original Path for the dictionary write; isolate the confinement test
2026-11-11 14:32:18 -05:00
Dylan M. Taylor e37df901ef Release 0.18.1: repair Flatpak update checks and self-updates (#2046)
* Release 0.18.1: fix Flatpak update permissions

* test: avoid unchecked option parsing in Flatpak regression
2026-11-11 14:27:18 -05:00
Dylan M. Taylor 7fe979aa24 Fix Flatpak permissions for update checks and self-updates (#2045)
* fix(flatpak): allow release checks and self-update helpers

* test: keep Flatpak permission parsing within strict Clippy rules
2026-11-11 14:18:59 -05:00
neilcawse 690171665a pptx: preserve SmartArt node text when dgm:cxnLst has no parent-child connections (#1944)
* pptx: preserve SmartArt node text when dgm:cxnLst has no parent-child connections

* fix(pptx): apply unconnected-point SmartArt outline fallback when type="doc" point is absent

* fix(pptx): restrict SmartArt pt_order fallback to empty parOf graphs and preserve cached Picture/Connector children

* fix(pptx): convert default rect geometry to line for connectors in diagram drawings

* docs(pptx): note flat ptLst order fallback on dgm::outline
2026-11-11 18:48:47 +00:00
Neil Cawse e8802ccfbd Merge remote-tracking branch 'upstream/main' into fix/pptx-paragraph-build-inference 2026-11-11 18:35:52 +00:00
Dylan M. Taylor 203fa79211 Merge pull request #2041 from storytold/chore/craft-update-6e03f65
Updates: take craft-update from craft-libs main at 6e03f65bf
2026-11-11 12:43:51 -05:00
Dylan M. Taylor 3e2e73aab7 Updates: take craft-update from craft-libs main at 6e03f65bf 2026-11-11 12:43:42 -05:00
Dylan M. Taylor cf0d7fbb14 ci: run Harden-Runner on every job (#2039)
Adds step-security/harden-runner (audit, pinned to v2.22.4 like
craft-common-actions) as the first step of each job that did not have it,
so every workflow in the org's public repos reports its egress to
StepSecurity. Audit, not block: block-mode allow lists come from each
job's observed traffic.
2026-11-11 12:10:27 -05:00
FredApps 0ce1a83c14 Merge main into release for DeckCraft v0.18.0 2026-11-11 16:35:50 +01:00
FredApps 121c59d521 Release: DeckCraft v0.18.0 (#2033) 2026-11-11 16:35:45 +01:00
Dylan M. Taylor a55457ee53 Spelling: stop an affix header's count at the end of the file, share aliased dictionaries (#1794)
* Spelling: stop an affix header's count at the end of the file, share aliased dictionaries

* address review: share the first inserted dictionary on a concurrent load
2026-11-11 04:16:05 -05:00
Dylan M. Taylor 7bd141c1c9 xtask: match attribution globs with a table, not backtracking (#1797) 2026-11-11 04:07:19 -05:00
Dylan M. Taylorandaiongg e3e218a8fc Shape edits: edit each target once and read its box without copying it (#1769)
* engine: edit each target shape once and read its box without copying it

* engine: check the fill colour field in the repeated-ids test

* Apply review fixes

- crates/engine/src/tests.rs

* address review: shape.lock skips targets inside a targeted group

* address review: dedupe shape.lock targets before walking groups

---------

Co-authored-by: aiongg <[email protected]>
2026-11-11 04:00:58 -05:00
Dylan M. Taylor c532ae749b Logging: escape control characters and mark continuation lines in log records (#1791) 2026-11-11 03:52:36 -05:00
Dylan M. Taylor e0f3639e94 Undo: count a slide's notes, comments and tags in the memory budget (#1768)
* engine: count a slide's notes, comments and tags in the undo memory budget

* engine: drive the undo-budget test through slide.rename

* address review: size text cost by the Paragraph and Run struct sizes
2026-11-11 03:44:11 -05:00
Dylan M. Taylor eb10e2b9b3 Metafile: bound clipping work across all subpaths of one item (#1738)
* metafile: bound clipping work across all subpaths of one drawn item

* address review: charge fill clipping as polygons grow, one budget per item

* address review: charge actual clip work instead of projected polygon growth

* address review: share one clip-work allowance between fill and stroke

* address review: share one clip-work budget across fill and stroke, give region intersection its own budget
2026-11-11 03:35:40 -05:00
Dylan M. Taylorandaiongg 92a0437ab6 Media: check an MP4's sample tables where Symphonia will start reading it (#1737)
* media: check an MP4 for oversized sample tables where symphonia will start reading it

* address review: run MP4 table guard only for MP4 or unknown containers

* Apply review fixes

- crates/media/src/probe.rs
- crates/media/src/mp4guard.rs
- crates/media/src/audio.rs

---------

Co-authored-by: aiongg <[email protected]>
2026-11-11 03:28:15 -05:00
Dylan M. Taylor 4e87f2b850 H.264: bound decoder work from overlapping slices, gaps and queued output (#1706)
* H.264: bound decoder work from overlapping slices, gaps and queued output

- A macroblock that an earlier slice of the same picture already decoded is
  refused, so overlapping slices cannot redo the picture's reconstruction
  once per slice.
- The memory budget check counts pictures waiting in the output queue that
  have left the DPB (one decode call may carry many access units) and the
  DPB's actual size, counting shared buffers once.
- frame_num gap concealment advances the POC state across the skipped run in
  constant time instead of once per missing frame_num.
- Storing a reference picture evicts until the DPB is under capacity, rather
  than evicting one picture and inserting regardless.

* address review: charge pictures already held at their own size in the decoder budget

* address review: keep the h264 output-queue byte total incrementally
2026-11-11 03:17:58 -05:00
Neil Cawse 730c6cde9c Merge remote-tracking branch 'origin/main' into fix/pptx-paragraph-build-inference 2026-11-11 02:18:40 +00:00
Dylan M. Taylorandaiongg 54846750d4 Ink: extend a stroke in place and cap a gesture's samples (#1770)
* engine: extend an ink stroke in place and cap a gesture's samples

* engine: move the ink extension out of drag

* address review: close scribbles at the release point; keep vertices when a capped press adds none

* Apply review fixes

- crates/engine/src/tools/pointer.rs

* raise ink gesture cap to 100000 points

---------

Co-authored-by: aiongg <[email protected]>
2026-11-10 18:42:24 -05:00
Dylan M. Taylorandaiongg 1e9c392f67 Spelling: remember failed compound tails (#1835)
* Spelling: remember failed compound tails

* Apply review fixes

- crates/engine/src/spell.rs

* Apply review fixes

- crates/engine/src/spell.rs

---------

Co-authored-by: aiongg <[email protected]>
2026-11-10 17:54:53 -05:00
Dylan M. Taylorandaiongg c3fb184e9d engine: index font names and embedded faces instead of rescanning (#1749)
Co-authored-by: aiongg <[email protected]>
2026-11-10 17:54:22 -05:00
Neil Cawse b7e5cf05ea style(pptx): format timing.rs with rustfmt 2026-11-09 22:52:57 +00:00
Neil Cawse ce2bd95414 fix(pptx): require matching parent offsets for click-started inferred paragraph builds 2026-11-09 22:41:38 +00:00
Neil Cawse 41dd2bd3bb Merge remote-tracking branch 'origin/main' into fix/pptx-paragraph-build-inference 2026-11-09 14:23:39 +00:00
autocraft-bot[bot] 1469419354 Apply review fixes
- ROADMAP.md
- docs/file-format-parity.md
- docs/gaps.md
2026-11-08 21:35:34 +00:00
Simon Koch-Sultan 70a0d7a2d8 engine: test ungrouping a top-level group with a group inside it
Ungrouping a top-level group and a group inside it in one call, in
either order, frees the shapes of both onto the slide in the top-level
group's place, drawn where they were, returns them once each and
selects them. The other ungroup tests leave the outermost group
unnamed, so none checked that the inner group is found at the slide's
top level after the same call frees the group around it.
2026-11-08 15:36:50 -05:00
aiongg ea97b18c70 Fix pPTX bar chart: series-level <c:dLbls><c:delete val="1"/></c:dLbls>
Fixes #1977.

- crates/pptx/src/read/chart.rs
- crates/pptx/tests/chart_data_labels.rs
- crates/pptx/tests/autocraft_regression_1977.rs
2026-11-08 17:03:31 +00:00
Simon Koch-Sultan 17db75b74a engine: test ungrouping nested groups named out of order
Ungrouping a middle group, the group around it and then the innermost
group, all inside a fourth group, returns the four freed rectangles
once each, selects them, and leaves them in the fourth group in order.
Before the previous commit, the innermost group was listed twice and
replaced once, so the call also returned that group's id after the
group was gone.
2026-11-08 10:22:34 -05:00
aiongg cdca5bd09d Apply review fixes
- crates/engine/src/cmd/arrange.rs
2026-11-08 14:56:11 +00:00
Simon Koch-Sultan 7ebfa82297 engine: group copy test compares site counts
The test compared the pasted shape's connection sites with the original's
through zip, which stops at the shorter list. It now checks that both have
the same number of sites first.
2026-11-08 09:01:24 -05:00
Simon Koch-Sultan de5398cfc5 engine: nested ungroup test compares site counts and checks the selection
The test compared each shape's connection sites before and after through
zip, which stops at the shorter list, so a shape that lost sites would still
pass. It now checks the counts first. The case with three nested groups also
checks that the freed shapes are selected.
2026-11-08 09:01:24 -05:00
Simon Koch-Sultan 2750e65096 engine: group duplicate test compares site counts
The test compared the copy's connection sites with the original's through
zip, which stops at the shorter list. It now checks that both have the same
number of sites first.
2026-11-08 09:01:23 -05:00
Neil Cawse c564070d9e fix(pptx): check click group and parent-derived start offsets before folding inferred paragraph builds 2026-11-08 13:40:42 +00:00
Neil Cawse 3def50c98a fix(pptx): gate inferred non-click paragraph builds on 500ms/0ms delay when bldP is absent 2026-11-08 13:36:49 +00:00
Neil Cawse a570031909 fix(pptx): drop timing gate in plays_as_build, merge duplicate bldP entries, and expand build tests 2026-11-08 13:36:49 +00:00
Neil Cawse 9d9610a893 pptx: infer paragraph builds when bldP does not disable them 2026-11-08 13:36:49 +00:00
aiongg 5a2cab9483 Apply review fixes
- crates/model/src/edit.rs
- crates/engine/src/tests.rs
2026-11-08 07:34:41 +00:00
NetterSilverandClaude Haiku 5.5 3e2dc9ca7f Delete a whole grapheme cluster with the forward Delete key
Found while testing crates/engine/src/cmd/text/caret.rs:60 (delete_dir, forward) with crates/model/src/edit.rs:423 (char_move)

Co-Authored-By: Claude Haiku 5.5 <[email protected]>
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-08 07:17:28 +00:00
aiongg 6b1e2d6aef Apply review fixes
- xtask/src/main.rs
2026-11-08 04:50:32 +00:00
Naseq 283a148420 ui-egui: cfg opening_generation off wasm32; lint the wasm set with -D warnings (#1954) 2026-11-08 09:07:05 +04:30
Simon Koch-Sultan d680914140 engine: a shape copied from a group pastes where it was drawn
edit.copy and edit.cut put a shape inside a group on the clipboard with
its box in the group's own space. Pasted, it landed where it would be
drawn without the group's move, stretch, turn or flip: a shape from a
group moved 200 pt to the right pasted 200 pt left of where it was
drawn, and one from a group stretched to twice its width pasted half as
wide.

The copied shape now carries the box it is drawn in on the slide, mapped
out through each group around it as Ungroup maps a freed shape. It
pastes at the slide's top level where it was drawn, offset as any paste
on the same slide is. Cut still deletes the shape from its group.
A turned shape in a group stretched more one way than the other is drawn
skewed, which a box cannot hold; it pastes with the unskewed box Ungroup
gives it. Copying with a text caret is unchanged.

Test: a_shape_copied_from_a_group_pastes_where_it_was_drawn.
2026-11-08 01:21:59 +00:00
Simon Koch-Sultan 1c8b14edad engine: arrange.ungroup frees a group nested in another group
arrange.ungroup looked only at the slide's top-level list, so a group
inside another group answered `{"ids": []}`, stayed grouped, and the
selection was emptied.

Such a group's children now take its place among the outer group's
children, with their boxes mapped into the outer group's space by
child_to_parent, as for a top-level group, so they are drawn where they
were. They are selected and returned. A shape freed from a named group
and then from a named group around it is listed once.

Test: ungrouping_a_nested_group_frees_its_shapes_into_the_outer_group.
2026-11-08 01:21:58 +00:00
Simon Koch-Sultan 09548fe55c engine: edit.duplicate copies a shape inside a group into its group
edit.duplicate took its copies from the slide's top-level list only, so
duplicating a shape inside a group answered `{"ids": []}` and changed
nothing.

A shape inside a group is now copied into that group, after its other
children, and the copy is selected and returned. The duplicate offset is
converted into the group's space, so the copy is drawn as far from the
original on the slide as a top-level copy is, however the groups around
it are moved, stretched or turned. Duplicating a moved copy again
repeats the move (#966), measured on the slide. A shape whose group is
duplicated in the same call is copied only with the group's copy.

Test: duplicating_a_shape_in_a_group_copies_it_into_the_group.
2026-11-08 01:21:57 +00:00
Simon Koch-Sultan ea809d93f5 engine: shape.merge refuses a shape inside a group
shape.merge worked on the slide's top-level list. Given shapes inside a
group, it merged them from their boxes in the group's own space, left
them in the group and added the result to the slide's top level, so in a
moved or resized group the merged shape was drawn away from the shapes
it came from, which stayed drawn as well.

A call whose shapes include one inside a group is now refused with the
shape's name and a request to ungroup it first, and the slide is left as
it was. Merge Shapes is unavailable while a shape inside a group is
selected; a call that names top-level shapes in `ids` still runs
whatever is selected, as it does when nothing is selected.

Test: a_shape_in_a_group_is_refused.
2026-11-08 01:21:55 +00:00
Vincent Cloutier 26c1ac83dc DC-001: report unresolved slide relationships as damage 2026-11-07 16:12:41 -05:00
Adithya Shankaran 23d0f67d17 Say what the touched tests check
Doc comments only, on the tests this change touches and the ones the docstring scan listed.
2026-11-06 15:45:24 +00:00
aiongg fb67888e25 Apply review fixes
- docs/mcp.md
2026-11-06 06:29:34 +00:00
Adithya Shankaran 9e6758e276 Credit my name in the About window 2026-11-06 00:14:47 +05:30
Adithya Shankaran 6c0886a68a Automation roots: compare names exactly and refuse COM¹ to LPT³
On Windows a folder can be set to tell case apart, and then IN beside the root in passed as inside
it, because names were compared with to_lowercase. Both paths have the spelling the file system
gives them by then, so another spelling of the root still matches with an exact compare. A missing
name keeps the spelling it was given, but it can only match a root that exists, and resolve has
already found that one in its real spelling.

is_device_name also missed COM¹, COM², COM³ and LPT¹, LPT², LPT³, which Windows reserves too.
It also cut the name only at a period, so COM1: and NUL:x were not seen as device names.
2026-11-06 00:14:47 +05:30
233 changed files with 3893 additions and 60555 deletions
+4
View File
@@ -26,6 +26,10 @@ jobs:
# Same craft-fonts commit as release.yml.
CRAFT_FONTS_REF: 37827adc41fd165f9a9234ac40f94dbad353aa69
steps:
- name: Harden runner
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- name: Check out craft-fonts
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
+4
View File
@@ -27,6 +27,10 @@ jobs:
name: Windows installation policy (no app build)
runs-on: windows-latest
steps:
- name: Harden runner
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
+5
View File
@@ -1,6 +1,10 @@
name: Release
on:
# A nightly dry run on main: every release build except macOS, nothing signed or released
# (craft-common-actions release.yml `dry-run`). Finds release-only breakage before release day.
schedule:
- cron: "16 1 * * *"
push:
branches: [release]
workflow_dispatch:
@@ -30,6 +34,7 @@ jobs:
# Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos.
uses: storytold/craft-common-actions/.github/workflows/release.yml@main
with:
dry-run: ${{ github.event_name == 'schedule' }}
app-name: deckcraft
app-title: DeckCraft
# Keep identical to ci.yml and freebsd.yml. The shared release workflow sets
+3
View File
@@ -18,4 +18,7 @@ jobs:
sonarcloud:
# Tracked at @main so org-wide CI improvements propagate without PR churn.
uses: storytold/craft-common-actions/.github/workflows/sonarcloud.yml@main
with:
# Keep coverage tests on the same reviewed, immutable font set as the main gate.
fonts-ref: 37827adc41fd165f9a9234ac40f94dbad353aa69
secrets: inherit
Generated
+61 -109
View File
@@ -844,15 +844,24 @@ dependencies = [
"libc",
]
[[package]]
name = "craft-audio"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
dependencies = [
"craft-media",
"thiserror 2.0.21",
]
[[package]]
name = "craft-fonts-build"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
[[package]]
name = "craft-i18n"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
dependencies = [
"fixed_decimal",
"icu_decimal",
@@ -875,15 +884,26 @@ dependencies = [
[[package]]
name = "craft-media"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-metafile"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
dependencies = [
"encoding_rs",
"image",
"kurbo",
"thiserror 2.0.21",
]
[[package]]
name = "craft-pdf"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
dependencies = [
"krilla",
"thiserror 2.0.21",
@@ -892,7 +912,7 @@ dependencies = [
[[package]]
name = "craft-ui"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
dependencies = [
"craft-fonts-build",
"craft-i18n",
@@ -912,7 +932,7 @@ dependencies = [
[[package]]
name = "craft-update"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
dependencies = [
"chrono",
"dunce",
@@ -922,6 +942,7 @@ dependencies = [
"image",
"log",
"plist",
"rustix 1.1.5",
"semver",
"serde",
"serde_json",
@@ -935,6 +956,16 @@ dependencies = [
"zip 9.1.0",
]
[[package]]
name = "craft-video"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
dependencies = [
"craft-media",
"rayon",
"thiserror 2.0.21",
]
[[package]]
name = "craft-web-api"
version = "0.1.0"
@@ -1028,7 +1059,7 @@ checksum = "0c87e182de0887fd5361989c677c4e8f5000cd9491d6d563161a8f3a5519fc7f"
[[package]]
name = "deckcraft"
version = "0.17.0"
version = "0.19.0"
dependencies = [
"arboard",
"chrono",
@@ -1050,32 +1081,16 @@ dependencies = [
[[package]]
name = "deckcraft-anim"
version = "0.17.0"
version = "0.19.0"
dependencies = [
"deckcraft-geom",
"deckcraft-model",
"serde",
]
[[package]]
name = "deckcraft-av1"
version = "0.17.0"
dependencies = [
"deckcraft-bitstream",
"rayon",
"thiserror 2.0.21",
]
[[package]]
name = "deckcraft-bitstream"
version = "0.17.0"
dependencies = [
"craft-media",
]
[[package]]
name = "deckcraft-cli"
version = "0.17.0"
version = "0.19.0"
dependencies = [
"deckcraft-engine",
"deckcraft-mcp",
@@ -1085,14 +1100,14 @@ dependencies = [
[[package]]
name = "deckcraft-color"
version = "0.17.0"
version = "0.19.0"
dependencies = [
"serde",
]
[[package]]
name = "deckcraft-engine"
version = "0.17.0"
version = "0.19.0"
dependencies = [
"chrono",
"deckcraft-anim",
@@ -1118,7 +1133,7 @@ dependencies = [
[[package]]
name = "deckcraft-fonts"
version = "0.17.0"
version = "0.19.0"
dependencies = [
"epaint_default_fonts",
"harfrust 0.13.3",
@@ -1130,7 +1145,7 @@ dependencies = [
[[package]]
name = "deckcraft-format"
version = "0.17.0"
version = "0.19.0"
dependencies = [
"deckcraft-model",
"serde",
@@ -1141,47 +1156,15 @@ dependencies = [
[[package]]
name = "deckcraft-geom"
version = "0.17.0"
version = "0.19.0"
dependencies = [
"kurbo",
"serde",
]
[[package]]
name = "deckcraft-h264"
version = "0.17.0"
dependencies = [
"deckcraft-bitstream",
"rayon",
"thiserror 2.0.21",
]
[[package]]
name = "deckcraft-hevc"
version = "0.17.0"
dependencies = [
"deckcraft-bitstream",
"rayon",
"thiserror 2.0.21",
]
[[package]]
name = "deckcraft-isobmff"
version = "0.17.0"
dependencies = [
"craft-media",
]
[[package]]
name = "deckcraft-matroska"
version = "0.17.0"
dependencies = [
"craft-media",
]
[[package]]
name = "deckcraft-mcp"
version = "0.17.0"
version = "0.19.0"
dependencies = [
"deckcraft-engine",
"deckcraft-render",
@@ -1191,34 +1174,20 @@ dependencies = [
[[package]]
name = "deckcraft-media"
version = "0.17.0"
version = "0.19.0"
dependencies = [
"deckcraft-av1",
"deckcraft-h264",
"deckcraft-hevc",
"deckcraft-isobmff",
"deckcraft-matroska",
"deckcraft-ogg",
"deckcraft-opus",
"deckcraft-vp9",
"craft-audio",
"craft-media",
"craft-video",
"image",
"log",
"symphonia",
"thiserror 2.0.21",
]
[[package]]
name = "deckcraft-metafile"
version = "0.17.0"
dependencies = [
"image",
"kurbo",
"thiserror 2.0.21",
]
[[package]]
name = "deckcraft-model"
version = "0.17.0"
version = "0.19.0"
dependencies = [
"deckcraft-color",
"deckcraft-geom",
@@ -1228,17 +1197,9 @@ dependencies = [
"unicode-segmentation",
]
[[package]]
name = "deckcraft-ogg"
version = "0.17.0"
[[package]]
name = "deckcraft-opus"
version = "0.17.0"
[[package]]
name = "deckcraft-pdf"
version = "0.17.0"
version = "0.19.0"
dependencies = [
"craft-pdf",
"deckcraft-fonts",
@@ -1256,7 +1217,7 @@ dependencies = [
[[package]]
name = "deckcraft-pptx"
version = "0.17.0"
version = "0.19.0"
dependencies = [
"deckcraft-color",
"deckcraft-engine",
@@ -1272,13 +1233,13 @@ dependencies = [
[[package]]
name = "deckcraft-render"
version = "0.17.0"
version = "0.19.0"
dependencies = [
"craft-metafile",
"deckcraft-anim",
"deckcraft-color",
"deckcraft-fonts",
"deckcraft-geom",
"deckcraft-metafile",
"deckcraft-model",
"deckcraft-text",
"image",
@@ -1291,7 +1252,7 @@ dependencies = [
[[package]]
name = "deckcraft-text"
version = "0.17.0"
version = "0.19.0"
dependencies = [
"chrono",
"deckcraft-color",
@@ -1305,7 +1266,7 @@ dependencies = [
[[package]]
name = "deckcraft-ui-egui"
version = "0.17.0"
version = "0.19.0"
dependencies = [
"craft-ui",
"craft-update",
@@ -1329,18 +1290,9 @@ dependencies = [
"web-time",
]
[[package]]
name = "deckcraft-vp9"
version = "0.17.0"
dependencies = [
"deckcraft-bitstream",
"rayon",
"thiserror 2.0.21",
]
[[package]]
name = "deckcraft-web"
version = "0.17.0"
version = "0.19.0"
dependencies = [
"craft-web-api",
"craft-web-host",
@@ -2791,7 +2743,7 @@ dependencies = [
[[package]]
name = "krilla"
version = "0.8.2"
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
dependencies = [
"base64 0.22.1",
"bumpalo",
@@ -3570,7 +3522,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967"
dependencies = [
"libc",
"windows-sys 0.61.2",
"windows-sys 0.45.0",
]
[[package]]
@@ -6325,7 +6277,7 @@ checksum = "9440ea3e5aeabb0ac63af70daf835274065238cdd0cec83418f417eae38bacee"
[[package]]
name = "xtask"
version = "0.17.0"
version = "0.19.0"
dependencies = [
"serde_json",
]
+12 -17
View File
@@ -4,7 +4,7 @@ members = ["crates/*", "apps/*", "xtask"]
default-members = ["crates/*", "apps/deckcraft", "apps/deckcraft-cli"]
[workspace.package]
version = "0.17.0"
version = "0.19.0"
edition = "2024"
license = "MIT OR Apache-2.0"
rust-version = "1.95"
@@ -14,7 +14,6 @@ repository = "https://github.com/storytold/deckcraft"
# internal
deckcraft-geom = { path = "crates/geom" }
deckcraft-color = { path = "crates/color" }
deckcraft-metafile = { path = "crates/metafile" }
deckcraft-model = { path = "crates/model" }
deckcraft-fonts = { path = "crates/fonts" }
deckcraft-text = { path = "crates/text" }
@@ -27,16 +26,6 @@ deckcraft-engine = { path = "crates/engine" }
deckcraft-mcp = { path = "crates/mcp" }
deckcraft-ui-egui = { path = "crates/ui-egui" }
deckcraft-media = { path = "crates/media" }
# codecs (ported from FilmCraft, same owner and licence)
deckcraft-bitstream = { path = "crates/bitstream" }
deckcraft-h264 = { path = "crates/h264" }
deckcraft-hevc = { path = "crates/hevc" }
deckcraft-vp9 = { path = "crates/vp9" }
deckcraft-av1 = { path = "crates/av1" }
deckcraft-isobmff = { path = "crates/isobmff" }
deckcraft-matroska = { path = "crates/matroska" }
deckcraft-ogg = { path = "crates/ogg" }
deckcraft-opus = { path = "crates/opus" }
# foundation
kurbo = { version = "0.13", features = ["serde"] }
@@ -59,11 +48,17 @@ harfrust = "0.13"
vello_cpu = "0.2"
vello_common = { version = "0.2", default-features = false }
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif", "tiff", "bmp"] }
craft-media = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d", version = "=0.1.0", default-features = false }
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d", version = "=0.1.0", default-features = false, features = ["write", "write-simple-text"] }
# craft-media, craft-audio and craft-video share types and stay on one rev.
craft-media = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false }
craft-audio = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false, features = ["opus-decode"] }
# H.264, HEVC, VP9 and AV1 decoders.
craft-video = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false, features = ["h264-decode", "hevc-decode", "vp9-decode", "av1-decode", "threads"] }
# WMF, EMF and EMF+ reader.
craft-metafile = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0" }
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false, features = ["write", "write-simple-text"] }
# craft-update: app-specific pretend versions apply to startup and manual checks.
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "1b34eba41e62138fc2438d2734523b302840b45d", features = ["egui"] }
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d", version = "=0.1.0" }
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "63196e66f3033a6623eb629af95a104d26986725", features = ["egui"] }
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0" }
i_overlay = "9"
quick-xml = "0.41"
zip = { version = "3", default-features = false, features = ["deflate"] }
@@ -109,7 +104,7 @@ codegen-units = 16
debug = 0
[patch.crates-io]
krilla = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d", version = "=0.8.2" }
krilla = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.8.2" }
ecolor = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
emath = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
epaint = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
+3 -2
View File
@@ -76,8 +76,9 @@ Code dependencies
- symphonia (audio decoding for MP3, AAC, ALAC, FLAC, Vorbis, WAV, AIFF, CAF) is used unmodified
under the Mozilla Public License 2.0; its source is available at
https://github.com/pdeljanov/Symphonia.
- The H.264, HEVC, VP9, AV1, Opus, MP4, Matroska and Ogg crates under crates/ are ported from
FilmCraft (same owner, MIT OR Apache-2.0).
- The H.264, HEVC, VP9 and AV1 crates under crates/ are ported from FilmCraft (same owner,
MIT OR Apache-2.0). The Opus decoder (craft-audio) and the MP4, Matroska and Ogg demuxers
(craft-media) come from craft-libs (same owner, MIT OR Apache-2.0).
- Krilla 0.8.2 (https://github.com/LaurenzV/krilla) is used through craft-pdf under
MIT OR Apache-2.0. craft-libs vendors that version with page/surface unwind recovery
+4 -1
View File
@@ -2,7 +2,7 @@
**Stage: alpha** · next: beta, ~20 points and ~120–210 h away
> **Last reviewed:** 2026-11-04 · **Last updated:** 2026-11-04 · **Change:** trivial (Ogg Opus recovery includes trailing packets without granules in its reported duration; parity scores unchanged) · **Target:** Microsoft PowerPoint for Mac 16.113 (Microsoft 365)
> **Last reviewed:** 2026-11-04 · **Last updated:** 2026-11-11 · **Change:** trivial (slide-list entries with missing relationships are reported as lost slides and open as repaired copies; the Opus decoder and the Ogg, MP4 and Matroska demuxers come from craft-libs; parity scores unchanged) · **Target:** Microsoft PowerPoint for Mac 16.113 (Microsoft 365)
> **Last reviewed:** 2026-11-02 · **Last updated:** 2026-11-03 · **Change:** trivial (progress log: web `?host=parent` bridge opens and saves through a same-origin framing page; scores unchanged) · **Target:** Microsoft PowerPoint for Mac 16.113 (Microsoft 365)
> **Last reviewed:** 2026-11-02 · **Last updated:** 2026-11-03 · **Change:** trivial (community PR wave 10: PPTX and native saves keep table cell centred anchoring, text overflow, compatible line spacing, normalised character height, Thai distributed alignment, RTL table direction, alternate run language and reflection transforms; imported effects placed by parent time-node delays; Reuse Slides fits the destination size and brings custom table styles; deleted slides leave custom shows; merged shapes drop their animations; Compress Pictures measures grouped pictures at displayed size; parity scores unchanged) · **Target:** Microsoft PowerPoint for Mac 16.113 (Microsoft 365)
@@ -154,6 +154,8 @@ app's JSON control channel (`deckcraft --control PORT`).
| Date | What landed |
|---|---|
| 2026-11-11 | Media: the Opus decoder and the Ogg, MP4/MOV and Matroska demuxers are craft-audio and craft-media from craft-libs; DeckCraft's own copies and re-export crates are removed |
| 2026-11-07 | PPTX slide-list entries with missing relationships are reported in lostSlides instead of silently omitted. The engine opens the partial deck as a repaired copy with no original save path. Slide lists over 10,000 entries are explicitly refused before a partial load; exactly 10,000 remain supported. Synthetic 13-slide regression; does not establish the cause of #1910's field report. |
| 2026-11-04 | Ogg Opus recovery keeps trailing packets without granules in the total duration, matching their recovered positions; valid end trimming stays covered |
| 2026-11-03 | Web: the `?host=parent` bridge (as in GridCraft, WordCraft and PdfCraft) lets a same-origin page that frames DeckCraft send a presentation and store its saves; Save writes an opened .pptx back as .pptx under its own name, as on the desktop |
| 2026-11-02 | Slide show on Linux and the BSDs: Presenter View stays off when winit lists exactly one display, as in PowerPoint; if it lists none, the setting is kept. On one display the presenter window covered the show, and the hidden window stalled every frame: slide changes took up to ~1.7 s under Hyprland (#1156) |
@@ -340,6 +342,7 @@ app's JSON control channel (`deckcraft --control PORT`).
| Date | Change | Summary |
|---|---|---|
| 2026-11-11 | trivial | Opus and container code moved to craft-libs crates; parity scores unchanged |
| 2026-11-04 | trivial | Ogg Opus recovery duration includes its recovered tail; parity scores unchanged |
| 2026-11-02 | trivial | Progress log: run alternate-language metadata round-trips through native and PPTX formats; product parity scores unchanged |
| 2026-11-03 | trivial | Reuse Slides brings referenced custom table styles along; scores unchanged |
+2 -2
View File
@@ -1,7 +1,7 @@
use std::process::{Command, Output};
fn cli(args: &[&str]) -> Output {
Command::new(env!("CARGO_BIN_EXE_deckcraft-cli")).args(args).output().unwrap()
Command::new(crate::deckcraft_cli()).args(args).output().unwrap()
}
fn stdout(o: &Output) -> String {
@@ -197,7 +197,7 @@ fn every_subcommand_answers_help_and_does_nothing_else() {
std::fs::create_dir_all(&dir).unwrap();
for sub in SUBCOMMANDS {
for args in [vec![sub, "--help"], vec![sub, "-h", "out.png"], vec!["help", sub]] {
let o = Command::new(env!("CARGO_BIN_EXE_deckcraft-cli")).current_dir(&dir).args(&args).output().unwrap();
let o = Command::new(crate::deckcraft_cli()).current_dir(&dir).args(&args).output().unwrap();
assert!(stdout(&o).starts_with(&format!("usage: deckcraft-cli {sub}")), "{args:?}");
}
}
+2 -2
View File
@@ -29,7 +29,7 @@ fn closed_stdout() -> Stdio {
}
fn run(dir: &Path, args: &[&str], stdout: Stdio) -> Output {
Command::new(env!("CARGO_BIN_EXE_deckcraft-cli")).current_dir(dir).args(args).stdout(stdout).stderr(Stdio::piped()).output().unwrap()
Command::new(crate::deckcraft_cli()).current_dir(dir).args(args).stdout(stdout).stderr(Stdio::piped()).output().unwrap()
}
#[test]
@@ -52,7 +52,7 @@ fn broken_pipe_does_not_skip_later_commands_or_save() {
assert!(out.status.success(), "exit {:?}: {}", out.status, String::from_utf8_lossy(&out.stderr));
let saved = dir.0.join("out.deckcraft");
assert!(saved.is_file(), "requested save was skipped");
let info = Command::new(env!("CARGO_BIN_EXE_deckcraft-cli")).arg("info").arg(saved).output().unwrap();
let info = Command::new(crate::deckcraft_cli()).arg("info").arg(saved).output().unwrap();
assert!(info.status.success(), "{}", String::from_utf8_lossy(&info.stderr));
let data: serde_json::Value = serde_json::from_slice(&info.stdout).unwrap();
let slides = data["slides"].as_array().unwrap();
+3 -3
View File
@@ -4,7 +4,7 @@ use std::path::{Path, PathBuf};
use std::process::{Command, Output};
fn cli(args: &[&Path]) -> Output {
Command::new(env!("CARGO_BIN_EXE_deckcraft-cli")).args(args).output().unwrap()
Command::new(crate::deckcraft_cli()).args(args).output().unwrap()
}
fn temp_dir(name: &str) -> PathBuf {
@@ -18,7 +18,7 @@ fn temp_dir(name: &str) -> PathBuf {
fn convert_rejects_output_formats_it_cannot_write() {
let dir = temp_dir("convert-unsupported");
let input = dir.join("in.pptx");
let out = Command::new(env!("CARGO_BIN_EXE_deckcraft-cli")).args(["run", "--sample", "--export"]).arg(&input).output().unwrap();
let out = Command::new(crate::deckcraft_cli()).args(["run", "--sample", "--export"]).arg(&input).output().unwrap();
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
for name in ["out.odp", "out.bmp", "out.svg", "out.outline", "out"] {
@@ -48,7 +48,7 @@ fn run_rejects_unsupported_save_and_export_paths_before_running() {
let dir = temp_dir("run-unsupported");
for flag in ["--save", "--export"] {
let target = dir.join("deck.odp");
let out = Command::new(env!("CARGO_BIN_EXE_deckcraft-cli")).args(["run", "--sample", flag]).arg(&target).output().unwrap();
let out = Command::new(crate::deckcraft_cli()).args(["run", "--sample", flag]).arg(&target).output().unwrap();
assert!(!out.status.success(), "{flag}: run should fail");
assert!(String::from_utf8_lossy(&out.stderr).contains("unsupported output format .odp"), "{flag}");
assert!(!target.exists(), "{flag}: nothing is written");
+1 -1
View File
@@ -5,7 +5,7 @@ use std::path::PathBuf;
use std::process::{Command, Output};
fn cli(args: &[&str]) -> Output {
Command::new(env!("CARGO_BIN_EXE_deckcraft-cli")).args(args).output().unwrap()
Command::new(crate::deckcraft_cli()).args(args).output().unwrap()
}
fn temp_dir(name: &str) -> PathBuf {
+6
View File
@@ -1,3 +1,5 @@
use std::ffi::OsString;
mod cli;
#[cfg(unix)]
mod closed_stdout;
@@ -6,3 +8,7 @@ mod damaged;
// Integration tests: unwrapping and panicking on failure is fine here, unlike in shipped code (AGENTS.md).
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod mcp_roots;
fn deckcraft_cli() -> OsString {
std::env::var_os("NEXTEST_BIN_EXE_deckcraft_cli").unwrap_or_else(|| env!("CARGO_BIN_EXE_deckcraft-cli").into())
}
+1 -1
View File
@@ -7,7 +7,7 @@ use std::process::{Command, Output, Stdio};
use serde_json::Value;
fn bin() -> Command {
Command::new(env!("CARGO_BIN_EXE_deckcraft-cli"))
Command::new(crate::deckcraft_cli())
}
fn run(args: &[&str]) -> Output {
+20 -3
View File
@@ -148,7 +148,19 @@ pub fn rotate(dir: &Path) -> std::io::Result<(PathBuf, File)> {
/// One formatted log line: `<timestamp> <LEVEL> [<thread>] <target>: <message>\n`.
pub fn format_line(ts: &str, level: log::Level, thread: &str, target: &str, message: &str) -> String {
let message = message.strip_suffix('\n').unwrap_or(message);
format!("{ts} {level:<5} [{thread}] {target}: {message}\n")
// Messages can embed document-controlled text (a part name, a relationship id): control
// characters other than tab are escaped and each continuation line is marked, so such text
// cannot start a line that looks like a record of its own.
let mut text = String::with_capacity(message.len());
for c in message.chars() {
match c {
'\n' => text.push_str("\n | "),
'\t' => text.push(c),
c if c.is_control() => text.extend(c.escape_default()),
c => text.push(c),
}
}
format!("{ts} {level:<5} [{thread}] {target}: {text}\n")
}
/// Where formatted lines go besides standard error: kept in memory until a file is attached,
@@ -446,8 +458,13 @@ mod tests {
),
"2026-10-08T07:59:17.728Z WARN [main] deckcraft_pptx::opc: pptx: skipping oversized part ppt/media/image1.png\n"
);
// A multi-line message keeps its lines; the record still ends in exactly one newline.
assert!(format_line("t", log::Level::Error, "w", "x", "a\nb\n").ends_with("x: a\nb\n"));
// A multi-line message keeps its lines, each continuation marked; the record still ends in
// exactly one newline.
assert!(format_line("t", log::Level::Error, "w", "x", "a\nb\n").ends_with("x: a\n | b\n"));
// Injected line breaks and escape sequences cannot forge a record.
let line = format_line("t", log::Level::Warn, "w", "x", "part a\r\n2026 ERROR [m] y: forged\x1b[2J");
assert_eq!(line.matches('\n').count(), 2);
assert!(!line.contains('\r') && !line.contains('\x1b'));
}
#[test]
+4
View File
@@ -73,3 +73,7 @@ sync_github_name = true
display_name = "Arshiya"
real_name = "Arshiya Rahgozar"
sync_github_name = false
[people.adikshan11]
real_name = "Adithya Shankaran"
sync_github_name = true
-19
View File
@@ -1,19 +0,0 @@
[package]
name = "deckcraft-av1"
description = "Clean-room AV1 decoder (AV1 Bitstream & Decoding Process Specification v1.0.0 with Errata 1)"
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
[lints]
workspace = true
[features]
default = ["threads"]
threads = ["dep:rayon"]
[dependencies]
deckcraft-bitstream = { workspace = true }
thiserror = { workspace = true }
rayon = { workspace = true, optional = true }
-124
View File
@@ -1,124 +0,0 @@
//! MSB-first bit reader for OBU headers and the uncompressed frame header (spec 4.10 descriptors).
use crate::{Error, Result};
#[derive(Clone)]
pub(crate) struct BitReader<'a> {
data: &'a [u8],
/// Position in bits.
pos: usize,
}
impl<'a> BitReader<'a> {
pub fn new(data: &'a [u8]) -> Self {
BitReader { data, pos: 0 }
}
/// `get_position()`: bits consumed.
pub fn position(&self) -> usize {
self.pos
}
pub fn byte_pos(&self) -> usize {
self.pos.div_ceil(8)
}
#[inline]
pub fn bit(&mut self) -> Result<u32> {
let byte = *self.data.get(self.pos >> 3).ok_or(Error::Truncated)?;
let b = (byte >> (7 - (self.pos & 7))) & 1;
self.pos += 1;
Ok(b as u32)
}
/// `f(n)`, n ≤ 32.
pub fn f(&mut self, n: u32) -> Result<u32> {
let mut x: u64 = 0;
for _ in 0..n {
x = (x << 1) | self.bit()? as u64;
}
Ok(x as u32)
}
pub fn flag(&mut self) -> Result<bool> {
Ok(self.bit()? == 1)
}
/// `uvlc()`
pub fn uvlc(&mut self) -> Result<u32> {
let mut leading = 0u32;
while self.bit()? == 0 {
leading += 1;
if leading > 40 {
return Err(Error::Invalid("uvlc"));
}
}
if leading >= 32 {
return Ok(u32::MAX);
}
let v = self.f(leading)?;
Ok(v + ((1u64 << leading) - 1) as u32)
}
/// `le(n)`
pub fn le(&mut self, n: u32) -> Result<u32> {
let mut t = 0u32;
for i in 0..n {
t |= self.f(8)? << (8 * i);
}
Ok(t)
}
/// `leb128()`
pub fn leb128(&mut self) -> Result<u64> {
let mut v = 0u64;
for i in 0..8 {
let b = self.f(8)? as u64;
v |= (b & 0x7f) << (7 * i);
if b & 0x80 == 0 {
break;
}
}
Ok(v)
}
/// `su(n)`
pub fn su(&mut self, n: u32) -> Result<i32> {
let v = self.f(n)? as i64;
let sign = 1i64 << (n - 1);
Ok(if v & sign != 0 { (v - 2 * sign) as i32 } else { v as i32 })
}
/// `ns(n)`
pub fn ns(&mut self, n: u32) -> Result<u32> {
if n <= 1 {
return Ok(0);
}
let w = 32 - n.leading_zeros(); // FloorLog2(n) + 1
let m = (1u32 << w) - n;
let v = self.f(w - 1)?;
if v < m {
return Ok(v);
}
let extra = self.bit()?;
Ok((v << 1) - m + extra)
}
/// `byte_alignment()`
pub fn byte_align(&mut self) {
self.pos = self.pos.div_ceil(8) * 8;
}
}
/// Read a leb128 from a byte slice; returns (value, bytes used).
pub(crate) fn leb128(data: &[u8]) -> Result<(u64, usize)> {
let mut v = 0u64;
for i in 0..8 {
let b = *data.get(i).ok_or(Error::Truncated)? as u64;
v |= (b & 0x7f) << (7 * i);
if b & 0x80 == 0 {
return Ok((v, i + 1));
}
}
Ok((v, 8))
}
-314
View File
@@ -1,314 +0,0 @@
//! CDEF (7.15).
use crate::frame::{FrameBuf, Plane};
use crate::spec_tables::{CDEF_DIRECTIONS, CDEF_PRI_TAPS, CDEF_SEC_TAPS, CDEF_UV_DIR, DIV_TABLE};
use crate::state::FrameState;
/// Run CDEF on `fs.cur`; returns the CdefFrame, or None when no block is filtered (CdefFrame
/// equals CurrFrame).
pub(crate) fn apply(fs: &FrameState) -> Option<FrameBuf> {
let fh = &fs.fh;
if fh.coded_lossless || fh.allow_intrabc || !fs.seq.enable_cdef {
return None;
}
let (mi_rows, mi_cols) = (fh.mi_rows as usize, fh.mi_cols as usize);
let mut out = fs.cur.clone();
let mut any = false;
let mut r = 0;
while r < mi_rows {
let mut c = 0;
while c < mi_cols {
let idx = fs.cdef_idx(r & !15, c & !15);
if idx >= 0 && cdef_block(fs, &mut out, r, c, idx as usize) {
any = true;
}
c += 2;
}
r += 2;
}
any.then_some(out)
}
fn cdef_block(fs: &FrameState, out: &mut FrameBuf, r: usize, c: usize, idx: usize) -> bool {
let mi = &fs.mi;
let sk = |rr: usize, cc: usize| -> bool { if rr < mi.rows && cc < mi.cols { mi.skip[mi.idx(rr, cc)] } else { true } };
let skip = sk(r, c) && sk(r + 1, c) && sk(r, c + 1) && sk(r + 1, c + 1);
if skip {
return false;
}
let coeff_shift = fs.bit_depth - 8;
let (y_dir, var) = direction(fs, r, c);
let cp = &fs.fh.cdef;
let mut pri = (cp.y_pri[idx] << coeff_shift) as i32;
let sec = (cp.y_sec[idx] << coeff_shift) as i32;
let dir = if pri == 0 { 0 } else { y_dir };
let var_str = if (var >> 6) != 0 { (31 - ((var >> 6) as u32).leading_zeros()).min(12) as i32 } else { 0 };
pri = if var != 0 { (pri * (4 + var_str) + 8) >> 4 } else { 0 };
let damping = cp.damping as i32 + coeff_shift as i32;
filter(fs, out, 0, (r, c), (pri, sec), damping, dir);
if fs.num_planes == 1 {
return true;
}
let pri = (cp.uv_pri[idx] << coeff_shift) as i32;
let sec = (cp.uv_sec[idx] << coeff_shift) as i32;
let dir = if pri == 0 { 0 } else { CDEF_UV_DIR[fs.ssx][fs.ssy][y_dir] as usize };
let damping = cp.damping as i32 + coeff_shift as i32 - 1;
filter(fs, out, 1, (r, c), (pri, sec), damping, dir);
filter(fs, out, 2, (r, c), (pri, sec), damping, dir);
true
}
fn direction(fs: &FrameState, r: usize, c: usize) -> (usize, i32) {
let mut cost = [0i32; 8];
let mut partial = [[0i32; 15]; 8];
let x0 = c << 2;
let y0 = r << 2;
let pl = &fs.cur.planes[0];
let sh = fs.bit_depth - 8;
for i in 0..8 {
let row = pl.row(y0 + i);
for j in 0..8 {
let x = (row[x0 + j] as i32 >> sh) - 128;
partial[0][i + j] += x;
partial[1][i + j / 2] += x;
partial[2][i] += x;
partial[3][3 + i - j / 2] += x;
partial[4][7 + i - j] += x;
partial[5][3 - i / 2 + j] += x;
partial[6][j] += x;
partial[7][i / 2 + j] += x;
}
}
for i in 0..8 {
cost[2] += partial[2][i] * partial[2][i];
cost[6] += partial[6][i] * partial[6][i];
}
cost[2] *= DIV_TABLE[8] as i32;
cost[6] *= DIV_TABLE[8] as i32;
for i in 0..7 {
cost[0] += (partial[0][i] * partial[0][i] + partial[0][14 - i] * partial[0][14 - i]) * DIV_TABLE[i + 1] as i32;
cost[4] += (partial[4][i] * partial[4][i] + partial[4][14 - i] * partial[4][14 - i]) * DIV_TABLE[i + 1] as i32;
}
cost[0] += partial[0][7] * partial[0][7] * DIV_TABLE[8] as i32;
cost[4] += partial[4][7] * partial[4][7] * DIV_TABLE[8] as i32;
let mut i = 1;
while i < 8 {
for j in 0..5 {
cost[i] += partial[i][3 + j] * partial[i][3 + j];
}
cost[i] *= DIV_TABLE[8] as i32;
for j in 0..3 {
cost[i] += (partial[i][j] * partial[i][j] + partial[i][10 - j] * partial[i][10 - j]) * DIV_TABLE[2 * j + 2] as i32;
}
i += 2;
}
let mut best = 0;
let mut y_dir = 0;
for (i, &c) in cost.iter().enumerate() {
if c > best {
best = c;
y_dir = i;
}
}
(y_dir, (best - cost[(y_dir + 4) & 7]) >> 10)
}
/// Marks a sample outside the frame (CdefAvailable = 0) in the padded block.
const CDEF_NA: i32 = i32::MIN;
/// Padded block: 8x8 plus 2 samples on every side, row stride 12.
const PAD_STRIDE: usize = 12;
#[inline(always)]
fn constrain(diff: i32, threshold: i32, adj: u32) -> i32 {
let mag = (threshold - (diff.abs() >> adj)).clamp(0, diff.abs());
if diff < 0 { -mag } else { mag }
}
/// Tap offsets, weights and strengths of one block.
struct Taps {
pri: i32,
sec: i32,
pri_adj: u32,
sec_adj: u32,
pri_off: [isize; 2],
sec_off: [[isize; 2]; 2],
pri_taps: [i32; 2],
sec_taps: [i32; 2],
}
impl Taps {
fn new(coeff_shift: u32, (pri, sec): (i32, i32), damping: i32, dir: usize) -> Taps {
let pri_tap = ((pri >> coeff_shift) & 1) as usize;
let off = |d: usize, k: usize| CDEF_DIRECTIONS[d][k][0] as isize * PAD_STRIDE as isize + CDEF_DIRECTIONS[d][k][1] as isize;
let pri_off = [off(dir, 0), off(dir, 1)];
let d_lo = (dir + 6) & 7;
let d_hi = (dir + 2) & 7;
let sec_off = [[off(d_lo, 0), off(d_hi, 0)], [off(d_lo, 1), off(d_hi, 1)]];
let pri_taps = [CDEF_PRI_TAPS[pri_tap][0] as i32, CDEF_PRI_TAPS[pri_tap][1] as i32];
let sec_taps = [CDEF_SEC_TAPS[pri_tap][0] as i32, CDEF_SEC_TAPS[pri_tap][1] as i32];
let adj = |t: i32| if t == 0 { 0 } else { (damping - (31 - (t as u32).leading_zeros()) as i32).max(0) as u32 };
Taps { pri, sec, pri_adj: adj(pri), sec_adj: adj(sec), pri_off, sec_off, pri_taps, sec_taps }
}
/// The 12 (offset, weight, strength, adjustment) taps in filtering order.
fn tap_list(&self) -> [(isize, i32, i32, u32); 12] {
let mut taps = [(0isize, 0i32, 0i32, 0u32); 12];
let mut n = 0;
for k in 0..2 {
for sign in [-1isize, 1] {
taps[n] = (sign * self.pri_off[k], self.pri_taps[k], self.pri, self.pri_adj);
taps[n + 1] = (sign * self.sec_off[k][0], self.sec_taps[k], self.sec, self.sec_adj);
taps[n + 2] = (sign * self.sec_off[k][1], self.sec_taps[k], self.sec, self.sec_adj);
n += 3;
}
}
taps
}
}
/// Position and size of a block within its plane.
#[derive(Clone, Copy)]
struct BlockRect {
x0: usize,
y0: usize,
w: usize,
h: usize,
}
/// The block with two samples of padding on every side, plus whether every sample is available
/// (CdefAvailable: its 4x4 block is inside the frame).
fn padded_block(fs: &FrameState, plane: usize, src: &Plane, rect: BlockRect) -> ([i32; PAD_STRIDE * PAD_STRIDE], bool) {
let (sub_x, sub_y) = if plane > 0 { (fs.ssx, fs.ssy) } else { (0, 0) };
let (mi_rows, mi_cols) = (fs.fh.mi_rows as isize, fs.fh.mi_cols as isize);
let mut buf = [CDEF_NA; PAD_STRIDE * PAD_STRIDE];
let mut all_avail = true;
for i in 0..rect.h + 4 {
let y = (rect.y0 + i) as isize - 2;
let cand_r = (y << sub_y) >> 2;
if y < 0 || cand_r >= mi_rows {
all_avail = false;
continue;
}
let row = src.row(y as usize);
for j in 0..rect.w + 4 {
let x = (rect.x0 + j) as isize - 2;
let cand_c = (x << sub_x) >> 2;
if x >= 0 && cand_c < mi_cols {
buf[i * PAD_STRIDE + j] = row[x as usize] as i32;
} else {
all_avail = false;
}
}
}
(buf, all_avail)
}
/// Filters one sample whose taps may be unavailable (CDEF_NA).
fn filter_sample(buf: &[i32; PAD_STRIDE * PAD_STRIDE], taps: &Taps, ci: isize, x: i32) -> u16 {
let mut sum = 0i32;
let mut max = x;
let mut min = x;
for k in 0..2 {
for sign in [-1isize, 1] {
let p = buf[(ci + sign * taps.pri_off[k]) as usize];
if p != CDEF_NA {
if taps.pri != 0 {
sum += taps.pri_taps[k] * constrain(p - x, taps.pri, taps.pri_adj);
}
max = max.max(p);
min = min.min(p);
}
for s in 0..2 {
let q = buf[(ci + sign * taps.sec_off[k][s]) as usize];
if q != CDEF_NA {
if taps.sec != 0 {
sum += taps.sec_taps[k] * constrain(q - x, taps.sec, taps.sec_adj);
}
max = max.max(q);
min = min.min(q);
}
}
}
}
(x + ((8 + sum - (sum < 0) as i32) >> 4)).clamp(min, max) as u16
}
fn filter(fs: &FrameState, out: &mut FrameBuf, plane: usize, (r, c): (usize, usize), strengths: (i32, i32), damping: i32, dir: usize) {
let (pri, sec) = strengths;
if pri == 0 && sec == 0 {
// Every tap contributes 0 and the clamp to [min, max] keeps x: CdefFrame = CurrFrame.
return;
}
let coeff_shift = fs.bit_depth - 8;
let (sub_x, sub_y) = if plane > 0 { (fs.ssx, fs.ssy) } else { (0, 0) };
let rect = BlockRect { x0: (c * 4) >> sub_x, y0: (r * 4) >> sub_y, w: 8 >> sub_x, h: 8 >> sub_y };
let src = &fs.cur.planes[plane];
let (buf, all_avail) = padded_block(fs, plane, src, rect);
let taps = Taps::new(coeff_shift, strengths, damping, dir);
let dst = &mut out.planes[plane];
if all_avail {
// Every tap is available: whole rows at a time (constrain() with threshold 0 is 0).
let list = taps.tap_list();
let rows = (rect.y0, rect.x0, rect.h);
match rect.w {
8 => filter_rows::<8>(&buf, &list, dst, rows),
_ => filter_rows::<4>(&buf, &list, dst, rows),
}
return;
}
for i in 0..rect.h {
// The centre sample is read even where CdefAvailable is 0 (8x8 blocks straddling an odd
// MiRows / MiCols edge).
let srow = &src.row(rect.y0 + i)[rect.x0..rect.x0 + rect.w];
let drow = &mut dst.row_mut(rect.y0 + i)[rect.x0..rect.x0 + rect.w];
for j in 0..rect.w {
let ci = ((i + 2) * PAD_STRIDE + j + 2) as isize;
drow[j] = filter_sample(&buf, &taps, ci, srow[j] as i32);
}
}
}
/// CDEF of a block whose taps are all available, `W` samples per row. Samples (at most 12
/// bits), differences and the weighted tap sum all fit in i16, which doubles the SIMD width.
#[inline(always)]
fn filter_rows<const W: usize>(
buf: &[i32; PAD_STRIDE * PAD_STRIDE],
taps: &[(isize, i32, i32, u32); 12],
dst: &mut Plane,
(y0, x0, h): (usize, usize, usize),
) {
let mut b16 = [0i16; PAD_STRIDE * PAD_STRIDE];
for (d, &s) in b16.iter_mut().zip(buf.iter()) {
*d = s as i16;
}
for i in 0..h {
let ci = ((i + 2) * PAD_STRIDE + 2) as isize;
let Some(&x) = b16.get(ci as usize..).and_then(<[_]>::first_chunk::<W>) else {
return;
};
let (mut sum, mut max, mut min) = ([0i16; W], x, x);
for &(off, weight, thr, adj) in taps {
let (weight, thr) = (weight as i16, thr as i16);
let start = (ci + off) as usize;
let Some(p) = b16.get(start..).and_then(<[_]>::first_chunk::<W>) else {
return;
};
for j in 0..W {
// constrain(): .max / .min instead of clamp (whose bound check panics), and
// the sign applied branch-free, so the loop vectorises.
let d = p[j] - x[j];
let ad = d.abs();
let mag = (thr - (ad >> adj)).max(0).min(ad);
let sign = d >> 15;
sum[j] += weight * ((mag ^ sign) - sign);
max[j] = max[j].max(p[j]);
min[j] = min[j].min(p[j]);
}
}
let drow = &mut dst.row_mut(y0 + i)[x0..x0 + W];
for j in 0..W {
let s = sum[j];
drow[j] = (x[j] + ((8 + s - (s < 0) as i16) >> 4)).max(min[j]).min(max[j]) as u16;
}
}
}
-379
View File
@@ -1,379 +0,0 @@
//! The adaptive CDF arrays (spec 6.8.2 init_non_coeff_cdfs / init_coeff_cdfs, load_cdfs).
use crate::spec_tables::{
DEFAULT_ANGLE_DELTA_CDF, DEFAULT_CFL_ALPHA_CDF, DEFAULT_CFL_SIGN_CDF, DEFAULT_COEFF_BASE_CDF, DEFAULT_COEFF_BASE_EOB_CDF, DEFAULT_COEFF_BR_CDF,
DEFAULT_COMP_BWD_REF_CDF, DEFAULT_COMP_GROUP_IDX_CDF, DEFAULT_COMP_MODE_CDF, DEFAULT_COMP_REF_CDF, DEFAULT_COMP_REF_TYPE_CDF,
DEFAULT_COMPOUND_IDX_CDF, DEFAULT_COMPOUND_MODE_CDF, DEFAULT_COMPOUND_TYPE_CDF, DEFAULT_DC_SIGN_CDF, DEFAULT_DELTA_LF_CDF, DEFAULT_DELTA_Q_CDF,
DEFAULT_DRL_MODE_CDF, DEFAULT_EOB_EXTRA_CDF, DEFAULT_EOB_PT_16_CDF, DEFAULT_EOB_PT_32_CDF, DEFAULT_EOB_PT_64_CDF, DEFAULT_EOB_PT_128_CDF,
DEFAULT_EOB_PT_256_CDF, DEFAULT_EOB_PT_512_CDF, DEFAULT_EOB_PT_1024_CDF, DEFAULT_FILTER_INTRA_CDF, DEFAULT_FILTER_INTRA_MODE_CDF,
DEFAULT_INTER_INTRA_CDF, DEFAULT_INTER_INTRA_MODE_CDF, DEFAULT_INTER_TX_TYPE_SET1_CDF, DEFAULT_INTER_TX_TYPE_SET2_CDF,
DEFAULT_INTER_TX_TYPE_SET3_CDF, DEFAULT_INTERP_FILTER_CDF, DEFAULT_INTRA_TX_TYPE_SET1_CDF, DEFAULT_INTRA_TX_TYPE_SET2_CDF, DEFAULT_INTRABC_CDF,
DEFAULT_IS_INTER_CDF, DEFAULT_MOTION_MODE_CDF, DEFAULT_MV_BIT_CDF, DEFAULT_MV_CLASS_CDF, DEFAULT_MV_CLASS0_BIT_CDF, DEFAULT_MV_CLASS0_FR_CDF,
DEFAULT_MV_CLASS0_HP_CDF, DEFAULT_MV_FR_CDF, DEFAULT_MV_HP_CDF, DEFAULT_MV_JOINT_CDF, DEFAULT_MV_SIGN_CDF, DEFAULT_NEW_MV_CDF,
DEFAULT_PALETTE_SIZE_2_UV_COLOR_CDF, DEFAULT_PALETTE_SIZE_2_Y_COLOR_CDF, DEFAULT_PALETTE_SIZE_3_UV_COLOR_CDF, DEFAULT_PALETTE_SIZE_3_Y_COLOR_CDF,
DEFAULT_PALETTE_SIZE_4_UV_COLOR_CDF, DEFAULT_PALETTE_SIZE_4_Y_COLOR_CDF, DEFAULT_PALETTE_SIZE_5_UV_COLOR_CDF, DEFAULT_PALETTE_SIZE_5_Y_COLOR_CDF,
DEFAULT_PALETTE_SIZE_6_UV_COLOR_CDF, DEFAULT_PALETTE_SIZE_6_Y_COLOR_CDF, DEFAULT_PALETTE_SIZE_7_UV_COLOR_CDF, DEFAULT_PALETTE_SIZE_7_Y_COLOR_CDF,
DEFAULT_PALETTE_SIZE_8_UV_COLOR_CDF, DEFAULT_PALETTE_SIZE_8_Y_COLOR_CDF, DEFAULT_PALETTE_UV_MODE_CDF, DEFAULT_PALETTE_UV_SIZE_CDF,
DEFAULT_PALETTE_Y_MODE_CDF, DEFAULT_PALETTE_Y_SIZE_CDF, DEFAULT_PARTITION_W8_CDF, DEFAULT_PARTITION_W16_CDF, DEFAULT_PARTITION_W32_CDF,
DEFAULT_PARTITION_W64_CDF, DEFAULT_PARTITION_W128_CDF, DEFAULT_REF_MV_CDF, DEFAULT_RESTORATION_TYPE_CDF, DEFAULT_SEGMENT_ID_CDF,
DEFAULT_SEGMENT_ID_PREDICTED_CDF, DEFAULT_SINGLE_REF_CDF, DEFAULT_SKIP_CDF, DEFAULT_SKIP_MODE_CDF, DEFAULT_TX_8X8_CDF, DEFAULT_TX_16X16_CDF,
DEFAULT_TX_32X32_CDF, DEFAULT_TX_64X64_CDF, DEFAULT_TXB_SKIP_CDF, DEFAULT_TXFM_SPLIT_CDF, DEFAULT_UNI_COMP_REF_CDF, DEFAULT_USE_OBMC_CDF,
DEFAULT_USE_SGRPROJ_CDF, DEFAULT_USE_WIENER_CDF, DEFAULT_UV_MODE_CFL_ALLOWED_CDF, DEFAULT_UV_MODE_CFL_NOT_ALLOWED_CDF, DEFAULT_WEDGE_INDEX_CDF,
DEFAULT_WEDGE_INTER_INTRA_CDF, DEFAULT_Y_MODE_CDF, DEFAULT_ZERO_MV_CDF, FRAME_LF_COUNT, MV_CONTEXTS,
};
/// Every CDF array listed by init_non_coeff_cdfs and init_coeff_cdfs. Each innermost array holds
/// N cumulative values (the last is 32768) plus the adaptation counter.
#[derive(Clone)]
pub(crate) struct Cdfs {
pub y_mode: [[u16; 14]; 4],
pub uv_mode_cfl_not_allowed: [[u16; 14]; 13],
pub uv_mode_cfl_allowed: [[u16; 15]; 13],
pub angle_delta: [[u16; 8]; 8],
pub intrabc: [u16; 3],
pub partition_w8: [[u16; 5]; 4],
pub partition_w16: [[u16; 11]; 4],
pub partition_w32: [[u16; 11]; 4],
pub partition_w64: [[u16; 11]; 4],
pub partition_w128: [[u16; 9]; 4],
pub segment_id: [[u16; 9]; 3],
pub segment_id_predicted: [[u16; 3]; 3],
pub tx_8x8: [[u16; 3]; 3],
pub tx_16x16: [[u16; 4]; 3],
pub tx_32x32: [[u16; 4]; 3],
pub tx_64x64: [[u16; 4]; 3],
pub txfm_split: [[u16; 3]; 21],
pub filter_intra_mode: [u16; 6],
pub filter_intra: [[u16; 3]; 22],
pub interp_filter: [[u16; 4]; 16],
pub motion_mode: [[u16; 4]; 22],
pub new_mv: [[u16; 3]; 6],
pub zero_mv: [[u16; 3]; 2],
pub ref_mv: [[u16; 3]; 6],
pub compound_mode: [[u16; 9]; 8],
pub drl_mode: [[u16; 3]; 3],
pub is_inter: [[u16; 3]; 4],
pub comp_mode: [[u16; 3]; 5],
pub skip_mode: [[u16; 3]; 3],
pub skip: [[u16; 3]; 3],
pub comp_ref: [[[u16; 3]; 3]; 3],
pub comp_bwd_ref: [[[u16; 3]; 2]; 3],
pub single_ref: [[[u16; 3]; 6]; 3],
pub mv_joint: [[u16; 5]; MV_CONTEXTS],
pub mv_class: [[[u16; 12]; 2]; MV_CONTEXTS],
pub mv_class0_bit: [[[u16; 3]; 2]; MV_CONTEXTS],
pub mv_fr: [[[u16; 5]; 2]; MV_CONTEXTS],
pub mv_class0_fr: [[[[u16; 5]; 2]; 2]; MV_CONTEXTS],
pub mv_class0_hp: [[[u16; 3]; 2]; MV_CONTEXTS],
pub mv_sign: [[[u16; 3]; 2]; MV_CONTEXTS],
pub mv_bit: [[[[u16; 3]; 10]; 2]; MV_CONTEXTS],
pub mv_hp: [[[u16; 3]; 2]; MV_CONTEXTS],
pub palette_y_mode: [[[u16; 3]; 3]; 7],
pub palette_uv_mode: [[u16; 3]; 2],
pub palette_y_size: [[u16; 8]; 7],
pub palette_uv_size: [[u16; 8]; 7],
pub palette_2_y_color: [[u16; 3]; 5],
pub palette_2_uv_color: [[u16; 3]; 5],
pub palette_3_y_color: [[u16; 4]; 5],
pub palette_3_uv_color: [[u16; 4]; 5],
pub palette_4_y_color: [[u16; 5]; 5],
pub palette_4_uv_color: [[u16; 5]; 5],
pub palette_5_y_color: [[u16; 6]; 5],
pub palette_5_uv_color: [[u16; 6]; 5],
pub palette_6_y_color: [[u16; 7]; 5],
pub palette_6_uv_color: [[u16; 7]; 5],
pub palette_7_y_color: [[u16; 8]; 5],
pub palette_7_uv_color: [[u16; 8]; 5],
pub palette_8_y_color: [[u16; 9]; 5],
pub palette_8_uv_color: [[u16; 9]; 5],
pub delta_q: [u16; 5],
pub delta_lf: [u16; 5],
pub delta_lf_multi: [[u16; 5]; FRAME_LF_COUNT],
pub intra_tx_type_set1: [[[u16; 8]; 13]; 2],
pub intra_tx_type_set2: [[[u16; 6]; 13]; 3],
pub inter_tx_type_set1: [[u16; 17]; 2],
pub inter_tx_type_set2: [u16; 13],
pub inter_tx_type_set3: [[u16; 3]; 4],
pub use_obmc: [[u16; 3]; 22],
pub inter_intra: [[u16; 3]; 3],
pub comp_ref_type: [[u16; 3]; 5],
pub cfl_sign: [u16; 9],
pub uni_comp_ref: [[[u16; 3]; 3]; 3],
pub wedge_inter_intra: [[u16; 3]; 22],
pub comp_group_idx: [[u16; 3]; 6],
pub compound_idx: [[u16; 3]; 6],
pub compound_type: [[u16; 3]; 22],
pub inter_intra_mode: [[u16; 5]; 3],
pub wedge_index: [[u16; 17]; 22],
pub cfl_alpha: [[u16; 17]; 6],
pub use_wiener: [u16; 3],
pub use_sgrproj: [u16; 3],
pub restoration_type: [u16; 4],
// coefficient CDFs
pub txb_skip: [[[u16; 3]; 13]; 5],
pub eob_pt_16: [[[u16; 6]; 2]; 2],
pub eob_pt_32: [[[u16; 7]; 2]; 2],
pub eob_pt_64: [[[u16; 8]; 2]; 2],
pub eob_pt_128: [[[u16; 9]; 2]; 2],
pub eob_pt_256: [[[u16; 10]; 2]; 2],
pub eob_pt_512: [[u16; 11]; 2],
pub eob_pt_1024: [[u16; 12]; 2],
pub eob_extra: [[[[u16; 3]; 9]; 2]; 5],
pub dc_sign: [[[u16; 3]; 3]; 2],
pub coeff_base_eob: [[[[u16; 4]; 4]; 2]; 5],
pub coeff_base: [[[[u16; 5]; 42]; 2]; 5],
pub coeff_br: [[[[u16; 5]; 21]; 2]; 5],
}
impl Cdfs {
/// init_non_coeff_cdfs( ) followed by init_coeff_cdfs( ) for `base_q_idx`.
pub fn new(base_q_idx: u32) -> Box<Cdfs> {
let mut c = Box::new(Cdfs {
y_mode: DEFAULT_Y_MODE_CDF,
uv_mode_cfl_not_allowed: DEFAULT_UV_MODE_CFL_NOT_ALLOWED_CDF,
uv_mode_cfl_allowed: DEFAULT_UV_MODE_CFL_ALLOWED_CDF,
angle_delta: DEFAULT_ANGLE_DELTA_CDF,
intrabc: DEFAULT_INTRABC_CDF,
partition_w8: DEFAULT_PARTITION_W8_CDF,
partition_w16: DEFAULT_PARTITION_W16_CDF,
partition_w32: DEFAULT_PARTITION_W32_CDF,
partition_w64: DEFAULT_PARTITION_W64_CDF,
partition_w128: DEFAULT_PARTITION_W128_CDF,
segment_id: DEFAULT_SEGMENT_ID_CDF,
segment_id_predicted: DEFAULT_SEGMENT_ID_PREDICTED_CDF,
tx_8x8: DEFAULT_TX_8X8_CDF,
tx_16x16: DEFAULT_TX_16X16_CDF,
tx_32x32: DEFAULT_TX_32X32_CDF,
tx_64x64: DEFAULT_TX_64X64_CDF,
txfm_split: DEFAULT_TXFM_SPLIT_CDF,
filter_intra_mode: DEFAULT_FILTER_INTRA_MODE_CDF,
filter_intra: DEFAULT_FILTER_INTRA_CDF,
interp_filter: DEFAULT_INTERP_FILTER_CDF,
motion_mode: DEFAULT_MOTION_MODE_CDF,
new_mv: DEFAULT_NEW_MV_CDF,
zero_mv: DEFAULT_ZERO_MV_CDF,
ref_mv: DEFAULT_REF_MV_CDF,
compound_mode: DEFAULT_COMPOUND_MODE_CDF,
drl_mode: DEFAULT_DRL_MODE_CDF,
is_inter: DEFAULT_IS_INTER_CDF,
comp_mode: DEFAULT_COMP_MODE_CDF,
skip_mode: DEFAULT_SKIP_MODE_CDF,
skip: DEFAULT_SKIP_CDF,
comp_ref: DEFAULT_COMP_REF_CDF,
comp_bwd_ref: DEFAULT_COMP_BWD_REF_CDF,
single_ref: DEFAULT_SINGLE_REF_CDF,
mv_joint: [DEFAULT_MV_JOINT_CDF; MV_CONTEXTS],
mv_class: [DEFAULT_MV_CLASS_CDF; MV_CONTEXTS],
mv_class0_bit: [[DEFAULT_MV_CLASS0_BIT_CDF; 2]; MV_CONTEXTS],
mv_fr: [DEFAULT_MV_FR_CDF; MV_CONTEXTS],
mv_class0_fr: [DEFAULT_MV_CLASS0_FR_CDF; MV_CONTEXTS],
mv_class0_hp: [[DEFAULT_MV_CLASS0_HP_CDF; 2]; MV_CONTEXTS],
mv_sign: [[DEFAULT_MV_SIGN_CDF; 2]; MV_CONTEXTS],
mv_bit: [[DEFAULT_MV_BIT_CDF; 2]; MV_CONTEXTS],
mv_hp: [[DEFAULT_MV_HP_CDF; 2]; MV_CONTEXTS],
palette_y_mode: DEFAULT_PALETTE_Y_MODE_CDF,
palette_uv_mode: DEFAULT_PALETTE_UV_MODE_CDF,
palette_y_size: DEFAULT_PALETTE_Y_SIZE_CDF,
palette_uv_size: DEFAULT_PALETTE_UV_SIZE_CDF,
palette_2_y_color: DEFAULT_PALETTE_SIZE_2_Y_COLOR_CDF,
palette_2_uv_color: DEFAULT_PALETTE_SIZE_2_UV_COLOR_CDF,
palette_3_y_color: DEFAULT_PALETTE_SIZE_3_Y_COLOR_CDF,
palette_3_uv_color: DEFAULT_PALETTE_SIZE_3_UV_COLOR_CDF,
palette_4_y_color: DEFAULT_PALETTE_SIZE_4_Y_COLOR_CDF,
palette_4_uv_color: DEFAULT_PALETTE_SIZE_4_UV_COLOR_CDF,
palette_5_y_color: DEFAULT_PALETTE_SIZE_5_Y_COLOR_CDF,
palette_5_uv_color: DEFAULT_PALETTE_SIZE_5_UV_COLOR_CDF,
palette_6_y_color: DEFAULT_PALETTE_SIZE_6_Y_COLOR_CDF,
palette_6_uv_color: DEFAULT_PALETTE_SIZE_6_UV_COLOR_CDF,
palette_7_y_color: DEFAULT_PALETTE_SIZE_7_Y_COLOR_CDF,
palette_7_uv_color: DEFAULT_PALETTE_SIZE_7_UV_COLOR_CDF,
palette_8_y_color: DEFAULT_PALETTE_SIZE_8_Y_COLOR_CDF,
palette_8_uv_color: DEFAULT_PALETTE_SIZE_8_UV_COLOR_CDF,
delta_q: DEFAULT_DELTA_Q_CDF,
delta_lf: DEFAULT_DELTA_LF_CDF,
delta_lf_multi: [DEFAULT_DELTA_LF_CDF; FRAME_LF_COUNT],
intra_tx_type_set1: DEFAULT_INTRA_TX_TYPE_SET1_CDF,
intra_tx_type_set2: DEFAULT_INTRA_TX_TYPE_SET2_CDF,
inter_tx_type_set1: DEFAULT_INTER_TX_TYPE_SET1_CDF,
inter_tx_type_set2: DEFAULT_INTER_TX_TYPE_SET2_CDF,
inter_tx_type_set3: DEFAULT_INTER_TX_TYPE_SET3_CDF,
use_obmc: DEFAULT_USE_OBMC_CDF,
inter_intra: DEFAULT_INTER_INTRA_CDF,
comp_ref_type: DEFAULT_COMP_REF_TYPE_CDF,
cfl_sign: DEFAULT_CFL_SIGN_CDF,
uni_comp_ref: DEFAULT_UNI_COMP_REF_CDF,
wedge_inter_intra: DEFAULT_WEDGE_INTER_INTRA_CDF,
comp_group_idx: DEFAULT_COMP_GROUP_IDX_CDF,
compound_idx: DEFAULT_COMPOUND_IDX_CDF,
compound_type: DEFAULT_COMPOUND_TYPE_CDF,
inter_intra_mode: DEFAULT_INTER_INTRA_MODE_CDF,
wedge_index: DEFAULT_WEDGE_INDEX_CDF,
cfl_alpha: DEFAULT_CFL_ALPHA_CDF,
use_wiener: DEFAULT_USE_WIENER_CDF,
use_sgrproj: DEFAULT_USE_SGRPROJ_CDF,
restoration_type: DEFAULT_RESTORATION_TYPE_CDF,
txb_skip: DEFAULT_TXB_SKIP_CDF[0],
eob_pt_16: DEFAULT_EOB_PT_16_CDF[0],
eob_pt_32: DEFAULT_EOB_PT_32_CDF[0],
eob_pt_64: DEFAULT_EOB_PT_64_CDF[0],
eob_pt_128: DEFAULT_EOB_PT_128_CDF[0],
eob_pt_256: DEFAULT_EOB_PT_256_CDF[0],
eob_pt_512: DEFAULT_EOB_PT_512_CDF[0],
eob_pt_1024: DEFAULT_EOB_PT_1024_CDF[0],
eob_extra: DEFAULT_EOB_EXTRA_CDF[0],
dc_sign: DEFAULT_DC_SIGN_CDF[0],
coeff_base_eob: DEFAULT_COEFF_BASE_EOB_CDF[0],
coeff_base: DEFAULT_COEFF_BASE_CDF[0],
coeff_br: DEFAULT_COEFF_BR_CDF[0],
});
c.init_coeff(base_q_idx);
c
}
/// init_coeff_cdfs( )
pub fn init_coeff(&mut self, base_q_idx: u32) {
let idx = match base_q_idx {
0..=20 => 0,
21..=60 => 1,
61..=120 => 2,
_ => 3,
};
self.txb_skip = DEFAULT_TXB_SKIP_CDF[idx];
self.eob_pt_16 = DEFAULT_EOB_PT_16_CDF[idx];
self.eob_pt_32 = DEFAULT_EOB_PT_32_CDF[idx];
self.eob_pt_64 = DEFAULT_EOB_PT_64_CDF[idx];
self.eob_pt_128 = DEFAULT_EOB_PT_128_CDF[idx];
self.eob_pt_256 = DEFAULT_EOB_PT_256_CDF[idx];
self.eob_pt_512 = DEFAULT_EOB_PT_512_CDF[idx];
self.eob_pt_1024 = DEFAULT_EOB_PT_1024_CDF[idx];
self.eob_extra = DEFAULT_EOB_EXTRA_CDF[idx];
self.dc_sign = DEFAULT_DC_SIGN_CDF[idx];
self.coeff_base_eob = DEFAULT_COEFF_BASE_EOB_CDF[idx];
self.coeff_base = DEFAULT_COEFF_BASE_CDF[idx];
self.coeff_br = DEFAULT_COEFF_BR_CDF[idx];
}
/// The counter reset of load_cdfs( ): the last entry of every CDF array is set to 0.
pub fn clear_counts(&mut self) {
macro_rules! clr {
($($f:ident),*) => { $( self.$f.clear_count(); )* };
}
clr!(
y_mode,
uv_mode_cfl_not_allowed,
uv_mode_cfl_allowed,
angle_delta,
intrabc,
partition_w8,
partition_w16,
partition_w32,
partition_w64,
partition_w128,
segment_id,
segment_id_predicted,
tx_8x8,
tx_16x16,
tx_32x32,
tx_64x64,
txfm_split,
filter_intra_mode,
filter_intra,
interp_filter,
motion_mode,
new_mv,
zero_mv,
ref_mv,
compound_mode,
drl_mode,
is_inter,
comp_mode,
skip_mode,
skip,
comp_ref,
comp_bwd_ref,
single_ref,
mv_joint,
mv_class,
mv_class0_bit,
mv_fr,
mv_class0_fr,
mv_class0_hp,
mv_sign,
mv_bit,
mv_hp,
palette_y_mode,
palette_uv_mode,
palette_y_size,
palette_uv_size,
palette_2_y_color,
palette_2_uv_color,
palette_3_y_color,
palette_3_uv_color,
palette_4_y_color,
palette_4_uv_color,
palette_5_y_color,
palette_5_uv_color,
palette_6_y_color,
palette_6_uv_color,
palette_7_y_color,
palette_7_uv_color,
palette_8_y_color,
palette_8_uv_color,
delta_q,
delta_lf,
delta_lf_multi,
intra_tx_type_set1,
intra_tx_type_set2,
inter_tx_type_set1,
inter_tx_type_set2,
inter_tx_type_set3,
use_obmc,
inter_intra,
comp_ref_type,
cfl_sign,
uni_comp_ref,
wedge_inter_intra,
comp_group_idx,
compound_idx,
compound_type,
inter_intra_mode,
wedge_index,
cfl_alpha,
use_wiener,
use_sgrproj,
restoration_type,
txb_skip,
eob_pt_16,
eob_pt_32,
eob_pt_64,
eob_pt_128,
eob_pt_256,
eob_pt_512,
eob_pt_1024,
eob_extra,
dc_sign,
coeff_base_eob,
coeff_base,
coeff_br
);
}
}
/// Zeroes the adaptation counter (last element) of every innermost CDF array.
trait ClearCount {
fn clear_count(&mut self);
}
impl<const N: usize> ClearCount for [u16; N] {
fn clear_count(&mut self) {
self[N - 1] = 0;
}
}
impl<T: ClearCount, const M: usize> ClearCount for [T; M] {
fn clear_count(&mut self) {
for e in self.iter_mut() {
e.clear_count();
}
}
}
File diff suppressed because it is too large Load Diff
-287
View File
@@ -1,287 +0,0 @@
//! Frame sample buffers and per-4x4 mode info storage.
//!
//! Tiles decode in parallel into private buffers that cover only the tile's area: a [`Plane`]
//! or [`MiInfo`] may hold a region of the frame (origin `ox`, `oy`), addressed with frame
//! coordinates throughout.
/// Recycled plane allocations: every frame allocates (and every evicted reference frees)
/// megabytes of planes, and fresh large allocations cost page faults and kernel calls. Buffers
/// are zeroed again on reuse, so contents never leak between frames.
static PLANE_POOL: std::sync::Mutex<Vec<Vec<u16>>> = std::sync::Mutex::new(Vec::new());
const POOL_MIN_LEN: usize = 1 << 16;
/// Samples kept at most (96 M samples = 192 MB, a few 4K frames).
const POOL_MAX_SAMPLES: usize = 96 << 20;
/// A zeroed buffer of `len` samples (recycled memory for large ones).
fn zeroed(len: usize) -> Vec<u16> {
if len >= POOL_MIN_LEN {
let found = {
let mut pool = PLANE_POOL.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
pool.iter().position(|v| v.capacity() >= len && v.capacity() <= 2 * len).map(|i| pool.swap_remove(i))
};
if let Some(mut v) = found {
v.clear();
v.resize(len, 0);
return v;
}
}
vec![0; len]
}
/// One plane of samples (u16 at every bit depth), or a rectangular region of one.
#[derive(Default)]
pub struct Plane {
pub data: Vec<u16>,
pub stride: usize,
/// Allocated rows.
pub rows: usize,
/// Frame coordinates of `data[0]` (0 for whole planes).
pub ox: usize,
pub oy: usize,
}
impl Clone for Plane {
fn clone(&self) -> Plane {
let mut data = zeroed(self.data.len());
data.copy_from_slice(&self.data);
Plane { data, stride: self.stride, rows: self.rows, ox: self.ox, oy: self.oy }
}
}
impl Drop for Plane {
fn drop(&mut self) {
if self.data.capacity() >= POOL_MIN_LEN {
let v = std::mem::take(&mut self.data);
let mut pool = PLANE_POOL.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
let kept: usize = pool.iter().map(Vec::capacity).sum();
if kept + v.capacity() <= POOL_MAX_SAMPLES {
pool.push(v);
}
}
}
}
impl Plane {
pub fn new(width: usize, height: usize) -> Plane {
Plane { data: zeroed(width * height), stride: width, rows: height, ox: 0, oy: 0 }
}
/// A `width` x `height` region with its top-left sample at frame position (ox, oy).
pub fn region(ox: usize, oy: usize, width: usize, height: usize) -> Plane {
Plane { data: zeroed(width * height), stride: width, rows: height, ox, oy }
}
#[inline(always)]
pub fn at(&self, x: usize, y: usize) -> u16 {
self.data[(y - self.oy) * self.stride + x - self.ox]
}
#[inline(always)]
pub fn set(&mut self, x: usize, y: usize, v: u16) {
self.data[(y - self.oy) * self.stride + x - self.ox] = v;
}
/// Row `y`; index 0 is column `ox` (whole planes: column 0).
#[inline(always)]
pub fn row(&self, y: usize) -> &[u16] {
let o = (y - self.oy) * self.stride;
&self.data[o..o + self.stride]
}
#[inline(always)]
pub fn row_mut(&mut self, y: usize) -> &mut [u16] {
let o = (y - self.oy) * self.stride;
&mut self.data[o..o + self.stride]
}
/// Row `y` from column `x` (frame coordinates) to the end of the stored row.
#[inline(always)]
pub fn row_from(&self, y: usize, x: usize) -> &[u16] {
let o = (y - self.oy) * self.stride;
&self.data[o + x - self.ox..o + self.stride]
}
#[inline(always)]
pub fn row_from_mut(&mut self, y: usize, x: usize) -> &mut [u16] {
let o = (y - self.oy) * self.stride;
&mut self.data[o + x - self.ox..o + self.stride]
}
/// Copy this region into the whole plane `dst` (same frame coordinates).
pub fn copy_into(&self, dst: &mut Plane) {
for y in 0..self.rows {
let s = &self.data[y * self.stride..(y + 1) * self.stride];
dst.row_from_mut(self.oy + y, self.ox)[..self.stride].copy_from_slice(s);
}
}
}
/// A frame's three planes, sized to the macroblock-aligned coded area plus margins.
#[derive(Clone, Default)]
pub struct FrameBuf {
pub planes: [Plane; 3],
pub num_planes: usize,
pub subsampling_x: usize,
pub subsampling_y: usize,
pub bit_depth: u8,
/// Frame dimensions (luma) the samples are valid for.
pub width: usize,
pub height: usize,
}
impl FrameBuf {
/// Allocate a frame for a coded area of `width` x `height` luma samples (rounded up to the
/// 128x128 superblock grid plus 160 samples of slack for blocks extending past the edge).
pub fn new(width: usize, height: usize, num_planes: usize, subsampling_x: usize, subsampling_y: usize, bit_depth: u8) -> FrameBuf {
let aw = width.div_ceil(128) * 128 + 160;
let ah = height.div_ceil(128) * 128 + 160;
let mut planes: [Plane; 3] = Default::default();
planes[0] = Plane::new(aw, ah);
for p in planes.iter_mut().take(num_planes).skip(1) {
*p = Plane::new(aw >> subsampling_x, ah >> subsampling_y);
}
FrameBuf { planes, num_planes, subsampling_x, subsampling_y, bit_depth, width, height }
}
/// The region of a frame of this geometry covering luma samples [x0, x1) x [y0, y1)
/// (chroma scaled down); the bounds are multiples of 8, or the allocated size.
pub fn region_like(&self, x0: usize, y0: usize, x1: usize, y1: usize) -> FrameBuf {
let mut planes: [Plane; 3] = Default::default();
for (p, pl) in planes.iter_mut().enumerate().take(self.num_planes) {
let (sx, sy) = if p == 0 { (0, 0) } else { (self.subsampling_x, self.subsampling_y) };
let full = &self.planes[p];
let (px0, py0) = (x0 >> sx, y0 >> sy);
let px1 = (x1 >> sx).min(full.stride);
let py1 = (y1 >> sy).min(full.rows);
*pl = Plane::region(px0, py0, px1 - px0, py1 - py0);
}
FrameBuf {
planes,
num_planes: self.num_planes,
subsampling_x: self.subsampling_x,
subsampling_y: self.subsampling_y,
bit_depth: self.bit_depth,
width: self.width,
height: self.height,
}
}
pub fn plane_width(&self, plane: usize) -> usize {
if plane == 0 { self.width } else { (self.width + self.subsampling_x) >> self.subsampling_x }
}
pub fn plane_height(&self, plane: usize) -> usize {
if plane == 0 { self.height } else { (self.height + self.subsampling_y) >> self.subsampling_y }
}
}
/// A motion vector (row, col) in 1/8 sample units.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Mv {
pub row: i16,
pub col: i16,
}
impl Mv {
pub const ZERO: Mv = Mv { row: 0, col: 0 };
pub fn new(row: i32, col: i32) -> Mv {
Mv { row: row as i16, col: col as i16 }
}
}
/// Mode info stored for every 4x4 luma position (the arrays the spec indexes by
/// `[ row ][ col ]` in mode-info units).
#[derive(Clone, Default)]
pub struct MiInfo {
/// Frame size in mode info units.
pub cols: usize,
pub rows: usize,
/// Stored region: origin and width (the whole frame unless a tile's private copy).
pub ox: usize,
pub oy: usize,
pub stride: usize,
pub region_rows: usize,
pub y_mode: Vec<u8>,
pub uv_mode: Vec<u8>,
pub ref_frame: Vec<[i8; 2]>,
pub mv: Vec<[Mv; 2]>,
pub is_inter: Vec<bool>,
pub skip_mode: Vec<bool>,
pub skip: Vec<bool>,
pub tx_size: Vec<u8>,
pub inter_tx_size: Vec<u8>,
pub mi_size: Vec<u8>,
pub segment_id: Vec<u8>,
pub palette_size: [Vec<u8>; 2],
pub palette_colors: [Vec<[u16; 8]>; 2],
pub delta_lf: Vec<[i8; 4]>,
pub comp_group_idx: Vec<u8>,
pub compound_idx: Vec<u8>,
pub interp_filter: Vec<[u8; 2]>,
pub motion_mode: Vec<u8>,
/// TxTypes[ row ][ col ] (luma 4x4 units).
pub tx_type: Vec<u8>,
/// RefFrames[ row ][ col ] has been written for this frame.
pub written: Vec<bool>,
}
impl MiInfo {
/// `palette`: whether palette mode can occur (allow_screen_content_tools); without it the
/// palette arrays (40 bytes per unit) stay empty.
pub fn new(cols: usize, rows: usize, palette: bool) -> MiInfo {
MiInfo::region(cols, rows, 0, 0, cols, rows, palette)
}
/// Storage for the w x h mode info units at (ox, oy) of a cols x rows frame.
pub fn region(cols: usize, rows: usize, ox: usize, oy: usize, w: usize, h: usize, palette: bool) -> MiInfo {
let n = w * h;
let np = if palette { n } else { 0 };
MiInfo {
cols,
rows,
ox,
oy,
stride: w,
region_rows: h,
y_mode: vec![0; n],
uv_mode: vec![0; n],
ref_frame: vec![[0, -1]; n],
mv: vec![[Mv::ZERO; 2]; n],
is_inter: vec![false; n],
skip_mode: vec![false; n],
skip: vec![false; n],
tx_size: vec![0; n],
inter_tx_size: vec![0; n],
mi_size: vec![0; n],
segment_id: vec![0; n],
palette_size: [vec![0; np], vec![0; np]],
palette_colors: [vec![[0; 8]; np], vec![[0; 8]; np]],
delta_lf: vec![[0; 4]; n],
comp_group_idx: vec![0; n],
compound_idx: vec![0; n],
interp_filter: vec![[0; 2]; n],
motion_mode: vec![0; n],
tx_type: vec![0; n],
written: vec![false; n],
}
}
#[inline(always)]
pub fn idx(&self, row: usize, col: usize) -> usize {
debug_assert!(col >= self.ox && col < self.ox + self.stride && row >= self.oy, "mode info ({row}, {col}) outside the stored region");
(row - self.oy) * self.stride + col - self.ox
}
/// Copy this region into the whole-frame `dst`.
pub fn copy_into(&self, dst: &mut MiInfo) {
let (w, h) = (self.stride, self.region_rows);
macro_rules! copy {
($($f:ident)*) => {$(
for y in 0..h {
let d = dst.idx(self.oy + y, self.ox);
dst.$f[d..d + w].copy_from_slice(&self.$f[y * w..(y + 1) * w]);
}
)*};
}
copy!(y_mode uv_mode ref_frame mv is_inter skip_mode skip tx_size inter_tx_size mi_size segment_id delta_lf comp_group_idx compound_idx interp_filter motion_mode tx_type written);
for k in (0..2).filter(|&k| !self.palette_size[k].is_empty()) {
for y in 0..h {
let d = dst.idx(self.oy + y, self.ox);
dst.palette_size[k][d..d + w].copy_from_slice(&self.palette_size[k][y * w..(y + 1) * w]);
dst.palette_colors[k][d..d + w].copy_from_slice(&self.palette_colors[k][y * w..(y + 1) * w]);
}
}
}
}
-455
View File
@@ -1,455 +0,0 @@
//! Film grain synthesis (7.18.3), applied to output pictures only (references stay clean).
use crate::Picture;
use crate::header::{FilmGrainParams, SequenceHeader};
use crate::spec_tables::{GAUSSIAN_SEQUENCE, MC_IDENTITY};
#[inline]
/// One plane's scaling lookup (7.18.3.4) from its `n` points. The header rejects points whose
/// values don't strictly increase; a pair that still doesn't (defence in depth) is skipped
/// instead of dividing by zero. No points: all zero.
fn scaling_lut(n: usize, xs: &[u8], ys: &[u8]) -> [i32; 256] {
let mut l = [0i32; 256];
let n = n.min(xs.len()).min(ys.len());
let (Some(&x0), Some(&y0)) = (xs.first(), ys.first()) else { return l };
if n == 0 {
return l;
}
for v in l.iter_mut().take(x0 as usize) {
*v = y0 as i32;
}
for i in 0..n - 1 {
let (xa, xb, ya, yb) = (xs[i] as i32, xs[i + 1] as i32, ys[i] as i32, ys[i + 1] as i32);
let dx = xb - xa;
if dx <= 0 {
continue;
}
let delta = (yb - ya) * ((65536 + (dx >> 1)) / dx);
for x in 0..dx {
if let Some(v) = l.get_mut((xa + x) as usize) {
*v = ya + ((x * delta + 32768) >> 16);
}
}
}
let (xl, yl) = (xs[n - 1], ys[n - 1]);
for v in l.iter_mut().skip(xl as usize) {
*v = yl as i32;
}
l
}
fn round2(x: i32, n: u32) -> i32 {
if n == 0 { x } else { (x + (1 << (n - 1))) >> n }
}
struct Rng(u32);
impl Rng {
/// get_random_number( bits ) (7.18.3.2)
fn next(&mut self, bits: u32) -> i32 {
let r = self.0;
let bit = (r ^ (r >> 1) ^ (r >> 3) ^ (r >> 12)) & 1;
let r = (r >> 1) | (bit << 15);
self.0 = r;
((r >> (16 - bits)) & ((1 << bits) - 1)) as i32
}
}
/// Picture-level parameters shared by the grain synthesis stages.
struct GrainCtx<'a> {
g: &'a FilmGrainParams,
bd: u32,
mono: bool,
sub_x: usize,
sub_y: usize,
w: usize,
h: usize,
grain_min: i32,
grain_max: i32,
}
impl GrainCtx<'_> {
fn num_planes(&self) -> usize {
if self.mono { 1 } else { 3 }
}
fn grain_shift(&self) -> u32 {
12 - self.bd + self.g.grain_scale_shift
}
fn ar_shift(&self) -> u32 {
self.g.ar_coeff_shift_minus_6 + 6
}
fn lag(&self) -> i32 {
self.g.ar_coeff_lag as i32
}
fn stripe_w(&self) -> usize {
self.w + 64
}
}
type GrainTemplate = Vec<[i32; 82]>;
/// The luma, Cb and Cr grain templates (7.18.3.3).
struct Templates {
luma: GrainTemplate,
cb: GrainTemplate,
cr: GrainTemplate,
}
impl Templates {
fn plane(&self, plane: usize) -> &GrainTemplate {
match plane {
0 => &self.luma,
1 => &self.cb,
_ => &self.cr,
}
}
}
fn luma_ar_sum(g: &FilmGrainParams, luma: &[[i32; 82]], lag: i32, y: usize, x: usize) -> i32 {
let mut s = 0;
let mut pos = 0;
'outer: for dr in -lag..=0 {
for dc in -lag..=lag {
if dr == 0 && dc == 0 {
break 'outer;
}
let c = g.ar_coeffs_y_plus_128[pos] as i32 - 128;
s += luma[(y as i32 + dr) as usize][(x as i32 + dc) as usize] * c;
pos += 1;
}
}
s
}
fn luma_grain(c: &GrainCtx, rng: &mut Rng) -> GrainTemplate {
let g = c.g;
let shift = c.grain_shift();
let mut luma = vec![[0i32; 82]; 73];
for row in luma.iter_mut() {
for v in row.iter_mut() {
let gv = if g.num_y_points > 0 { GAUSSIAN_SEQUENCE[rng.next(11) as usize] as i32 } else { 0 };
*v = round2(gv, shift);
}
}
let ar_shift = c.ar_shift();
let lag = c.lag();
for y in 3..73 {
for x in 3..82 - 3 {
let s = luma_ar_sum(g, &luma, lag, y, x);
luma[y][x] = (luma[y][x] + round2(s, ar_shift)).clamp(c.grain_min, c.grain_max);
}
}
luma
}
/// The luma contribution to the chroma autoregressive filter at chroma position (y, x).
fn chroma_luma_term(c: &GrainCtx, luma: &[[i32; 82]], y: usize, x: usize) -> i32 {
let mut l = 0;
let lx = ((x - 3) << c.sub_x) + 3;
let ly = ((y - 3) << c.sub_y) + 3;
for i in 0..=c.sub_y {
for j in 0..=c.sub_x {
l += luma[ly + i][lx + j];
}
}
round2(l, (c.sub_x + c.sub_y) as u32)
}
fn chroma_ar_sums(c: &GrainCtx, t: &Templates, y: usize, x: usize) -> (i32, i32) {
let g = c.g;
let lag = c.lag();
let mut s0 = 0;
let mut s1 = 0;
let mut pos = 0;
'outer: for dr in -lag..=0 {
for dc in -lag..=lag {
let c0 = g.ar_coeffs_cb_plus_128[pos] as i32 - 128;
let c1 = g.ar_coeffs_cr_plus_128[pos] as i32 - 128;
if dr == 0 && dc == 0 {
if g.num_y_points > 0 {
let l = chroma_luma_term(c, &t.luma, y, x);
s0 += l * c0;
s1 += l * c1;
}
break 'outer;
}
let (yy, xx) = ((y as i32 + dr) as usize, (x as i32 + dc) as usize);
s0 += t.cb[yy][xx] * c0;
s1 += t.cr[yy][xx] * c1;
pos += 1;
}
}
(s0, s1)
}
/// Chroma grain templates; zero for monochrome pictures.
fn chroma_grain(c: &GrainCtx, rng: &mut Rng, luma: GrainTemplate) -> Templates {
let g = c.g;
let mut t = Templates { luma, cb: vec![[0i32; 82]; 73], cr: vec![[0i32; 82]; 73] };
if c.mono {
return t;
}
let shift = c.grain_shift();
let ar_shift = c.ar_shift();
let chroma_w = if c.sub_x != 0 { 44 } else { 82 };
let chroma_h = if c.sub_y != 0 { 38 } else { 73 };
for (arr, seed, on) in [(&mut t.cb, 0xb524u32, g.num_cb_points > 0), (&mut t.cr, 0x49d8, g.num_cr_points > 0)] {
rng.0 = g.grain_seed ^ seed;
for row in arr.iter_mut().take(chroma_h) {
for v in row.iter_mut().take(chroma_w) {
let gv = if on || g.chroma_scaling_from_luma { GAUSSIAN_SEQUENCE[rng.next(11) as usize] as i32 } else { 0 };
*v = round2(gv, shift);
}
}
}
for y in 3..chroma_h {
for x in 3..chroma_w - 3 {
let (s0, s1) = chroma_ar_sums(c, &t, y, x);
t.cb[y][x] = (t.cb[y][x] + round2(s0, ar_shift)).clamp(c.grain_min, c.grain_max);
t.cr[y][x] = (t.cr[y][x] + round2(s1, ar_shift)).clamp(c.grain_min, c.grain_max);
}
}
t
}
/// Scaling lookup tables (7.18.3.4), one per plane.
fn scaling_luts(g: &FilmGrainParams, num_planes: usize) -> [[i32; 256]; 3] {
let mut lut = [[0i32; 256]; 3];
for (plane, l) in lut.iter_mut().enumerate().take(num_planes) {
let (n, xs, ys) = if plane == 0 || g.chroma_scaling_from_luma {
(g.num_y_points, &g.point_y_value, &g.point_y_scaling)
} else if plane == 1 {
(g.num_cb_points, &g.point_cb_value, &g.point_cb_scaling)
} else {
(g.num_cr_points, &g.point_cr_value, &g.point_cr_scaling)
};
*l = scaling_lut(n, xs, ys);
}
lut
}
fn scale_lookup(lut: &[[i32; 256]; 3], bd: u32, plane: usize, index: i32) -> i32 {
let sh = bd - 8;
let x = index >> sh;
let rem = index - (x << sh);
if bd == 8 || x == 255 {
lut[plane][x as usize]
} else {
let start = lut[plane][x as usize];
let end = lut[plane][x as usize + 1];
start + round2((end - start) * rem, sh)
}
}
/// Fills one plane of a noise stripe from the grain template at stripe column `x`.
fn fill_stripe_plane(c: &GrainCtx, src: &GrainTemplate, s: &mut [i32], plane: usize, offsets: (usize, usize), x: usize) {
let (off_x, off_y) = offsets;
let stripe_w = c.stripe_w();
let overlap = c.g.overlap_flag;
let psx = if plane > 0 { c.sub_x } else { 0 };
let psy = if plane > 0 { c.sub_y } else { 0 };
let pox = if psx != 0 { 6 + off_x } else { 9 + off_x * 2 };
let poy = if psy != 0 { 6 + off_y } else { 9 + off_y * 2 };
for i in 0..(34 >> psy) {
for j in 0..(34 >> psx) {
let mut gv = src[poy + i][pox + j];
let (idx, blend) = if psx == 0 { (i * stripe_w + x * 2 + j, j < 2) } else { (i * stripe_w + x + j, j == 0) };
if blend && overlap && x > 0 {
let old = s[idx];
gv = if psx != 0 {
old * 23 + gv * 22
} else if j == 0 {
old * 27 + gv * 17
} else {
old * 17 + gv * 27
};
gv = round2(gv, 5).clamp(c.grain_min, c.grain_max);
}
s[idx] = gv;
}
}
}
/// Noise stripes (7.18.3.5).
struct Stripes {
data: Vec<[Vec<i32>; 3]>,
stripe_w: usize,
overlap: bool,
sub_y: usize,
grain_min: i32,
grain_max: i32,
}
impl Stripes {
fn build(c: &GrainCtx, t: &Templates, rng: &mut Rng) -> Stripes {
let g = c.g;
let stripe_w = c.stripe_w();
let n_stripes = (c.h.div_ceil(2)).div_ceil(16).max(1);
let mut data: Vec<[Vec<i32>; 3]> = (0..n_stripes).map(|_| [vec![0; 34 * stripe_w], vec![0; 34 * stripe_w], vec![0; 34 * stripe_w]]).collect();
for (luma_num, stripe) in data.iter_mut().enumerate() {
let mut r = g.grain_seed;
r ^= (((luma_num * 37 + 178) & 255) << 8) as u32;
r ^= ((luma_num * 173 + 105) & 255) as u32;
rng.0 = r;
let mut x = 0;
while x < c.w.div_ceil(2) {
let rand = rng.next(8) as usize;
let offsets = (rand >> 4, rand & 15);
for (plane, s) in stripe.iter_mut().enumerate().take(c.num_planes()) {
fill_stripe_plane(c, t.plane(plane), s, plane, offsets, x);
}
x += 16;
}
}
Stripes { data, stripe_w, overlap: g.overlap_flag, sub_y: c.sub_y, grain_min: c.grain_min, grain_max: c.grain_max }
}
fn noise_at(&self, plane: usize, y: usize, x: usize) -> i32 {
let psy = if plane > 0 { self.sub_y } else { 0 };
let luma_num = y >> (5 - psy);
let i = y - (luma_num << (5 - psy));
let mut gv = self.data[luma_num][plane][i * self.stripe_w + x];
if psy == 0 {
if i < 2 && luma_num > 0 && self.overlap {
let old = self.data[luma_num - 1][plane][(i + 32) * self.stripe_w + x];
gv = if i == 0 { old * 27 + gv * 17 } else { old * 17 + gv * 27 };
gv = round2(gv, 5).clamp(self.grain_min, self.grain_max);
}
} else if i < 1 && luma_num > 0 && self.overlap {
let old = self.data[luma_num - 1][plane][(i + 16) * self.stripe_w + x];
gv = round2(old * 23 + gv * 22, 5).clamp(self.grain_min, self.grain_max);
}
gv
}
}
/// Output clipping range and scaling shift for the blend step (7.18.3.5).
struct BlendRange {
min_value: i32,
max_luma: i32,
max_chroma: i32,
pix_max: i32,
scaling_shift: u32,
}
impl BlendRange {
fn new(c: &GrainCtx, matrix_coefficients: usize) -> BlendRange {
let bd = c.bd;
let (min_value, max_luma, max_chroma) = if c.g.clip_to_restricted_range {
let ml = 235 << (bd - 8);
(16 << (bd - 8), ml, if matrix_coefficients == MC_IDENTITY { ml } else { 240 << (bd - 8) })
} else {
let m = (256 << (bd - 8)) - 1;
(0, m, m)
};
BlendRange { min_value, max_luma, max_chroma, pix_max: (1i32 << bd) - 1, scaling_shift: c.g.grain_scaling_minus_8 + 8 }
}
}
struct BlendEnv<'a> {
c: &'a GrainCtx<'a>,
lut: &'a [[i32; 256]; 3],
stripes: &'a Stripes,
range: &'a BlendRange,
}
impl BlendEnv<'_> {
fn blend_chroma(&self, py: &[u16], pu: &mut [u16], pv: &mut [u16]) {
let c = self.c;
let g = c.g;
let (w, h, sub_x, sub_y, bd) = (c.w, c.h, c.sub_x, c.sub_y, c.bd);
let cw = (w + sub_x) >> sub_x;
let ch = (h + sub_y) >> sub_y;
for y in 0..ch {
for x in 0..cw {
let lx = x << sub_x;
let ly = y << sub_y;
let lnx = (lx + 1).min(w - 1);
let avg = if sub_x != 0 { round2(py[ly * w + lx] as i32 + py[ly * w + lnx] as i32, 1) } else { py[ly * w + lx] as i32 };
for (plane, out, on, luma_mult, mult, offset) in [
(1usize, &mut *pu, g.num_cb_points > 0, g.cb_luma_mult, g.cb_mult, g.cb_offset),
(2, &mut *pv, g.num_cr_points > 0, g.cr_luma_mult, g.cr_mult, g.cr_offset),
] {
if !(on || g.chroma_scaling_from_luma) {
continue;
}
let orig = out[y * cw + x] as i32;
let merged = if g.chroma_scaling_from_luma {
avg
} else {
let combined = avg * (luma_mult as i32 - 128) + orig * (mult as i32 - 128);
((combined >> 6) + ((offset as i32 - 256) << (bd - 8))).clamp(0, self.range.pix_max)
};
let noise = round2(scale_lookup(self.lut, bd, plane, merged) * self.stripes.noise_at(plane, y, x), self.range.scaling_shift);
out[y * cw + x] = (orig + noise).clamp(self.range.min_value, self.range.max_chroma) as u16;
}
}
}
}
fn blend_luma(&self, py: &mut [u16]) {
let (w, h) = (self.c.w, self.c.h);
for y in 0..h {
for x in 0..w {
let orig = py[y * w + x] as i32;
let noise = round2(scale_lookup(self.lut, self.c.bd, 0, orig) * self.stripes.noise_at(0, y, x), self.range.scaling_shift);
py[y * w + x] = (orig + noise).clamp(self.range.min_value, self.range.max_luma) as u16;
}
}
}
}
pub(crate) fn apply(pic: &mut Picture, g: &FilmGrainParams, seq: &SequenceHeader) {
let _ = seq;
let bd = pic.bit_depth as u32;
let grain_center = 128i32 << (bd - 8);
let c = GrainCtx {
g,
bd,
mono: pic.mono_chrome,
sub_x: pic.subsampling_x as usize,
sub_y: pic.subsampling_y as usize,
w: pic.width as usize,
h: pic.height as usize,
grain_min: -grain_center,
grain_max: (256i32 << (bd - 8)) - 1 - grain_center,
};
// Generate grain (7.18.3.3).
let mut rng = Rng(g.grain_seed);
let luma = luma_grain(&c, &mut rng);
let templates = chroma_grain(&c, &mut rng, luma);
let lut = scaling_luts(g, c.num_planes());
let stripes = Stripes::build(&c, &templates, &mut rng);
// Blend (7.18.3.5).
let range = BlendRange::new(&c, pic.matrix_coefficients as usize);
let env = BlendEnv { c: &c, lut: &lut, stripes: &stripes, range: &range };
if !c.mono {
let [py, pu, pv] = &mut pic.planes;
env.blend_chroma(py, pu, pv);
}
if g.num_y_points > 0 {
env.blend_luma(&mut pic.planes[0]);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scaling_lut_interpolates_and_tolerates_duplicate_points() {
let l = scaling_lut(2, &[0, 255], &[0, 255]);
assert_eq!((l[0], l[128], l[255]), (0, 128, 255));
// Duplicate and descending x values don't divide by zero.
let l = scaling_lut(4, &[10, 10, 5, 200], &[20, 40, 60, 80]);
assert_eq!(l[0], 20);
assert_eq!(l[255], 80);
assert_eq!(scaling_lut(0, &[], &[]), [0; 256]);
}
}
File diff suppressed because it is too large Load Diff
-839
View File
@@ -1,839 +0,0 @@
//! Inter prediction (spec 7.11.3): motion vector scaling, block inter prediction, warped
//! motion, masks (wedge, difference weight, inter-intra), distance weights and OBMC.
use std::sync::OnceLock;
use crate::frame::Plane;
use crate::mvpred::round2_signed64;
use crate::spec_tables::{
BLOCK_8X8, BLOCK_SIZES, COMPOUND_AVERAGE, COMPOUND_DIFFWTD, COMPOUND_DISTANCE, COMPOUND_INTRA, COMPOUND_WEDGE, DIV_LUT, DIV_LUT_BITS,
DIV_LUT_PREC_BITS, EIGHTTAP, EIGHTTAP_SHARP, EIGHTTAP_SMOOTH, FILTER_BITS, GLOBAL_GLOBALMV, GLOBALMV, II_H_PRED, II_SMOOTH_PRED, II_V_PRED,
II_WEIGHTS_1D, INTRA_FRAME, LAST_FRAME, LOCALWARP, LS_MV_MAX, MASK_MASTER_SIZE, MAX_FRAME_DISTANCE, MAX_SB_SIZE, MI_HEIGHT_LOG2, MI_WIDTH_LOG2,
NUM_4X4_BLOCKS_HIGH, NUM_4X4_BLOCKS_WIDE, OBMC, OBMC_MASK_2, OBMC_MASK_4, OBMC_MASK_8, OBMC_MASK_16, OBMC_MASK_32, QUANT_DIST_LOOKUP,
QUANT_DIST_WEIGHT, REF_SCALE_SHIFT, SCALE_SUBPEL_BITS, SUBPEL_BITS, SUBPEL_FILTERS, SUBPEL_MASK, TRANSLATION, WARP_PARAM_REDUCE_BITS,
WARPED_FILTERS, WARPEDDIFF_PREC_BITS, WARPEDMODEL_NONDIAGAFFINE_CLAMP, WARPEDMODEL_PREC_BITS, WARPEDMODEL_TRANS_CLAMP, WARPEDPIXEL_PREC_SHIFTS,
WEDGE_BITS, WEDGE_CODEBOOK, WEDGE_HORIZONTAL, WEDGE_MASTER_OBLIQUE_EVEN, WEDGE_MASTER_OBLIQUE_ODD, WEDGE_MASTER_VERTICAL, WEDGE_OBLIQUE27,
WEDGE_OBLIQUE63, WEDGE_OBLIQUE117, WEDGE_OBLIQUE153, WEDGE_TYPES, WEDGE_VERTICAL,
};
use crate::tile::TileDecoder;
const PRED_STRIDE: usize = 128;
#[inline(always)]
fn round2(x: i32, n: u32) -> i32 {
if n == 0 { x } else { (x + (1 << (n - 1))) >> n }
}
/// Rounding variables (7.11.3.2): (InterRound0, InterRound1, InterPostRound).
pub(crate) fn rounding(is_compound: bool, bd: u32) -> (u32, u32, u32) {
let mut r0 = 3;
let mut r1 = if is_compound { 7 } else { 11 };
if bd == 12 {
r0 += 2;
if !is_compound {
r1 -= 2;
}
}
(r0, r1, 2 * FILTER_BITS as u32 - (r0 + r1))
}
/// Resolve divisor process (7.11.3.7): (divShift, divFactor).
fn resolve_divisor(d: i64) -> (i32, i64) {
let a = d.unsigned_abs();
let n = 63 - a.leading_zeros() as i32;
let e = a as i64 - (1i64 << n);
let f = if n > DIV_LUT_BITS as i32 { round2_signed64(e, (n - DIV_LUT_BITS as i32) as u32) } else { e << (DIV_LUT_BITS as i32 - n) };
let shift = n + DIV_LUT_PREC_BITS as i32;
let factor = DIV_LUT[f as usize] as i64;
(shift, if d < 0 { -factor } else { factor })
}
/// Setup shear process (7.11.3.6): (warpValid, alpha, beta, gamma, delta).
pub(crate) fn setup_shear(p: &[i32; 6]) -> (bool, i32, i32, i32, i32) {
let prec = 1i64 << WARPEDMODEL_PREC_BITS;
let alpha0 = (p[2] as i64 - prec).clamp(-32768, 32767);
let beta0 = (p[3] as i64).clamp(-32768, 32767);
let (div_shift, div_factor) = resolve_divisor(p[2] as i64);
let v = (p[4] as i64) << WARPEDMODEL_PREC_BITS;
let gamma0 = round2_signed_wide(v as i128 * div_factor as i128, div_shift as u32).clamp(-32768, 32767);
let w = p[3] as i64 * p[4] as i64;
let delta0 = (p[5] as i64 - round2_signed_wide(w as i128 * div_factor as i128, div_shift as u32) - prec).clamp(-32768, 32767);
let rb = WARP_PARAM_REDUCE_BITS as u32;
let red = |x: i64| (round2_signed64(x, rb) << rb) as i32;
let (alpha, beta, gamma, delta) = (red(alpha0), red(beta0), red(gamma0), red(delta0));
let valid =
!(4 * alpha.abs() + 7 * beta.abs() >= (1 << WARPEDMODEL_PREC_BITS) || 4 * gamma.abs() + 4 * delta.abs() >= (1 << WARPEDMODEL_PREC_BITS));
(valid, alpha, beta, gamma, delta)
}
fn round2_signed_wide(x: i128, n: u32) -> i64 {
if n == 0 {
return x as i64;
}
(if x >= 0 { (x + (1 << (n - 1))) >> n } else { -((-x + (1 << (n - 1))) >> n) }) as i64
}
/// WedgeMasks[ bsize ][ flipSign ][ wedge ] (7.11.3.11), stored as w*h bytes each.
fn wedge_masks() -> &'static Vec<Vec<[Vec<u8>; 2]>> {
static M: OnceLock<Vec<Vec<[Vec<u8>; 2]>>> = OnceLock::new();
M.get_or_init(|| {
let n = MASK_MASTER_SIZE;
let mut master = vec![[[0u8; 64]; 64]; 6];
for j in 0..n {
let mut shift = (n / 4) as i32;
let mut i = 0;
while i < n {
master[WEDGE_OBLIQUE63][i][j] = WEDGE_MASTER_OBLIQUE_EVEN[(j as i32 - shift).clamp(0, n as i32 - 1) as usize];
shift -= 1;
master[WEDGE_OBLIQUE63][i + 1][j] = WEDGE_MASTER_OBLIQUE_ODD[(j as i32 - shift).clamp(0, n as i32 - 1) as usize];
master[WEDGE_VERTICAL][i][j] = WEDGE_MASTER_VERTICAL[j];
master[WEDGE_VERTICAL][i + 1][j] = WEDGE_MASTER_VERTICAL[j];
i += 2;
}
}
for i in 0..n {
for j in 0..n {
let msk = master[WEDGE_OBLIQUE63][i][j];
master[WEDGE_OBLIQUE27][j][i] = msk;
master[WEDGE_OBLIQUE117][i][n - 1 - j] = 64 - msk;
master[WEDGE_OBLIQUE153][n - 1 - j][i] = 64 - msk;
master[WEDGE_HORIZONTAL][j][i] = master[WEDGE_VERTICAL][i][j];
}
}
let mut out = vec![Vec::new(); BLOCK_SIZES];
for bsize in BLOCK_8X8..BLOCK_SIZES {
if WEDGE_BITS[bsize] == 0 {
continue;
}
let w = 4 * NUM_4X4_BLOCKS_WIDE[bsize] as usize;
let h = 4 * NUM_4X4_BLOCKS_HIGH[bsize] as usize;
let w4 = NUM_4X4_BLOCKS_WIDE[bsize];
let h4 = NUM_4X4_BLOCKS_HIGH[bsize];
let shape = if h4 > w4 {
0
} else if h4 < w4 {
1
} else {
2
};
for wedge in 0..WEDGE_TYPES {
let cb = &WEDGE_CODEBOOK[shape][wedge];
let dir = cb[0] as usize;
let xoff = n / 2 - ((cb[1] as usize * w) >> 3);
let yoff = n / 2 - ((cb[2] as usize * h) >> 3);
let mut sum = 0usize;
for i in 0..w {
sum += master[dir][yoff][xoff + i] as usize;
}
for i in 1..h {
sum += master[dir][yoff + i][xoff] as usize;
}
let avg = (sum + (w + h - 1) / 2) / (w + h - 1);
let flip = (avg < 32) as usize;
let mut a = vec![0u8; w * h];
let mut b = vec![0u8; w * h];
for i in 0..h {
for j in 0..w {
let m = master[dir][yoff + i][xoff + j];
a[i * w + j] = m;
b[i * w + j] = 64 - m;
}
}
let pair = if flip == 0 { [a, b] } else { [b, a] };
out[bsize].push(pair);
}
}
out
})
}
impl TileDecoder<'_, '_> {
/// Reference plane and its (lastX, lastY) for refIdx (-1: the current frame for intra BC).
fn ref_plane_dims(&self, ref_idx: i32, plane: usize) -> (i32, i32) {
let (sx, sy) = if plane > 0 { (self.fs.ssx as i32, self.fs.ssy as i32) } else { (0, 0) };
let (uw, fh) = if ref_idx < 0 {
((self.fs.fh.mi_cols * 4) as i32, (self.fs.fh.mi_rows * 4) as i32)
} else {
let ri = &self.fs.ref_info[ref_idx as usize];
(ri.upscaled_width as i32, ri.frame_height as i32)
};
(((uw + sx) >> sx) - 1, ((fh + sy) >> sy) - 1)
}
/// Motion vector scaling process (7.11.3.3): (startX, startY, stepX, stepY).
fn mv_scaling(&self, plane: usize, ref_idx: i32, x: i32, y: i32, mv: [i32; 2]) -> (i32, i32, i32, i32) {
let fh = &self.fs.fh;
let (ref_uw, ref_h) = if ref_idx < 0 {
(fh.upscaled_width as i64, fh.frame_height as i64)
} else {
let ri = &self.fs.ref_info[ref_idx as usize];
(ri.upscaled_width as i64, ri.frame_height as i64)
};
let fw = fh.frame_width as i64;
let fhh = fh.frame_height as i64;
let rs = REF_SCALE_SHIFT as i64;
let x_scale = ((ref_uw << rs) + fw / 2) / fw;
let y_scale = ((ref_h << rs) + fhh / 2) / fhh;
let (sx, sy) = if plane > 0 { (self.fs.ssx as i32, self.fs.ssy as i32) } else { (0, 0) };
let half = 1i64 << (SUBPEL_BITS - 1);
let orig_x = ((x as i64) << SUBPEL_BITS) + ((2 * mv[1]) >> sx) as i64 + half;
let orig_y = ((y as i64) << SUBPEL_BITS) + ((2 * mv[0]) >> sy) as i64 + half;
let base_x = orig_x * x_scale - (half << rs);
let base_y = orig_y * y_scale - (half << rs);
let off = (1i64 << (SCALE_SUBPEL_BITS - SUBPEL_BITS)) / 2;
let sh = (REF_SCALE_SHIFT + SUBPEL_BITS - SCALE_SUBPEL_BITS) as u32;
let start_x = round2_signed64(base_x, sh) + off;
let start_y = round2_signed64(base_y, sh) + off;
let step_x = round2_signed64(x_scale, (REF_SCALE_SHIFT - SCALE_SUBPEL_BITS) as u32);
let step_y = round2_signed64(y_scale, (REF_SCALE_SHIFT - SCALE_SUBPEL_BITS) as u32);
(start_x as i32, start_y as i32, step_x as i32, step_y as i32)
}
fn ref_plane(&self, ref_idx: i32, plane: usize) -> &Plane {
// A missing reference (rejected earlier for valid streams) predicts from the current frame.
match usize::try_from(ref_idx).ok().and_then(|i| self.fs.refs.get(i)).and_then(|r| r.as_ref()) {
Some(r) => &r.buf.planes[plane],
None => &self.t.cur.planes[plane],
}
}
/// Block inter prediction process (7.11.3.4) into `pred` (stride PRED_STRIDE); `tmp` holds
/// the intermediate (horizontally filtered) rows.
#[allow(clippy::too_many_arguments)]
fn block_inter_prediction(
&self,
plane: usize,
ref_idx: i32,
x: i32,
y: i32,
x_step: i32,
y_step: i32,
w: usize,
h: usize,
filters: [u8; 2],
rnd: (u32, u32, u32),
pred: &mut [i32],
tmp: &mut [i32],
) {
let refp = self.ref_plane(ref_idx, plane);
let (last_x, last_y) = self.ref_plane_dims(ref_idx, plane);
let mut fh = filters[1] as usize;
if w <= 4 {
if fh == EIGHTTAP || fh == EIGHTTAP_SHARP {
fh = 4;
} else if fh == EIGHTTAP_SMOOTH {
fh = 5;
}
}
let mut fv = filters[0] as usize;
if h <= 4 {
if fv == EIGHTTAP || fv == EIGHTTAP_SHARP {
fv = 4;
} else if fv == EIGHTTAP_SMOOTH {
fv = 5;
}
}
let unit = 1 << SCALE_SUBPEL_BITS;
if x_step == unit && y_step == unit {
mc_unscaled(
refp,
last_x,
last_y,
x,
y,
w,
h,
&SUBPEL_FILTERS[fh][((x >> 6) & SUBPEL_MASK as i32) as usize],
&SUBPEL_FILTERS[fv][((y >> 6) & SUBPEL_MASK as i32) as usize],
rnd,
pred,
tmp,
);
return;
}
let inter_h = ((((h as i32 - 1) * y_step + (1 << SCALE_SUBPEL_BITS) - 1) >> SCALE_SUBPEL_BITS) + 8) as usize;
for r in 0..inter_h {
let ry = ((y >> 10) + r as i32 - 3).clamp(0, last_y) as usize;
let row = refp.row(ry);
for c in 0..w {
let p = x + x_step * c as i32;
let filt = &SUBPEL_FILTERS[fh][((p >> 6) & SUBPEL_MASK as i32) as usize];
let mut s = 0i32;
let bx = (p >> 10) - 3;
for t in 0..8 {
s += filt[t] as i32 * row[(bx + t as i32).clamp(0, last_x) as usize] as i32;
}
tmp[r * w + c] = round2(s, rnd.0);
}
}
for r in 0..h {
for c in 0..w {
let p = (y & 1023) + y_step * r as i32;
let filt = &SUBPEL_FILTERS[fv][((p >> 6) & SUBPEL_MASK as i32) as usize];
let base = (p >> 10) as usize;
let mut s = 0i32;
for t in 0..8 {
s += filt[t] as i32 * tmp[(base + t) * w + c];
}
pred[r * PRED_STRIDE + c] = round2(s, rnd.1);
}
}
}
/// Block warp process (7.11.3.5) for the 8x8 section (i8, j8).
#[allow(clippy::too_many_arguments)]
fn block_warp(
&self,
params: &[i32; 6],
plane: usize,
ref_idx: i32,
x: i32,
y: i32,
i8: usize,
j8: usize,
w: usize,
h: usize,
rnd: (u32, u32, u32),
pred: &mut [i32],
) {
let refp = self.ref_plane(ref_idx, plane);
let (last_x, last_y) = self.ref_plane_dims(ref_idx, plane);
let (sx, sy) = if plane > 0 { (self.fs.ssx as i32, self.fs.ssy as i32) } else { (0, 0) };
let src_x = (x + j8 as i32 * 8 + 4) << sx;
let src_y = (y + i8 as i32 * 8 + 4) << sy;
let dst_x = params[2] as i64 * src_x as i64 + params[3] as i64 * src_y as i64 + params[0] as i64;
let dst_y = params[4] as i64 * src_x as i64 + params[5] as i64 * src_y as i64 + params[1] as i64;
let (_, alpha, beta, gamma, delta) = setup_shear(params);
let x4 = dst_x >> sx;
let y4 = dst_y >> sy;
let ix4 = (x4 >> WARPEDMODEL_PREC_BITS) as i32;
let sx4 = (x4 & ((1 << WARPEDMODEL_PREC_BITS) - 1)) as i32;
let iy4 = (y4 >> WARPEDMODEL_PREC_BITS) as i32;
let sy4 = (y4 & ((1 << WARPEDMODEL_PREC_BITS) - 1)) as i32;
let mut inter = [[0i32; 8]; 15];
for i1 in -7..8i32 {
let row = refp.row((iy4 + i1).clamp(0, last_y) as usize);
for i2 in -4..4i32 {
let sxv = sx4 + alpha * i2 + beta * i1;
let offs = (round2(sxv, WARPEDDIFF_PREC_BITS as u32) + WARPEDPIXEL_PREC_SHIFTS as i32) as usize;
let mut s = 0i32;
for i3 in 0..8i32 {
s += WARPED_FILTERS[offs][i3 as usize] as i32 * row[(ix4 + i2 - 3 + i3).clamp(0, last_x) as usize] as i32;
}
inter[(i1 + 7) as usize][(i2 + 4) as usize] = round2(s, rnd.0);
}
}
let lim_i = 4.min(h as i32 - i8 as i32 * 8 - 4);
let lim_j = 4.min(w as i32 - j8 as i32 * 8 - 4);
for i1 in -4..lim_i {
for i2 in -4..lim_j {
let syv = sy4 + gamma * i2 + delta * i1;
let offs = (round2(syv, WARPEDDIFF_PREC_BITS as u32) + WARPEDPIXEL_PREC_SHIFTS as i32) as usize;
let mut s = 0i32;
for i3 in 0..8 {
s += WARPED_FILTERS[offs][i3] as i32 * inter[(i1 + i3 as i32 + 4) as usize][(i2 + 4) as usize];
}
pred[(i8 * 8 + (i1 + 4) as usize) * PRED_STRIDE + j8 * 8 + (i2 + 4) as usize] = round2(s, rnd.1);
}
}
}
/// Warp estimation process (7.11.3.8): sets LocalWarpParams / LocalValid.
pub(crate) fn warp_estimation(&mut self) {
let mut a = [[0i64; 2]; 2];
let mut bx = [0i64; 2];
let mut by = [0i64; 2];
let mid_y = self.b.mi_row as i64 * 4 + self.b.bh4 as i64 * 2 - 1;
let mid_x = self.b.mi_col as i64 * 4 + self.b.bw4 as i64 * 2 - 1;
let suy = mid_y * 8;
let sux = mid_x * 8;
let duy = suy + self.b.mv_i[0][0] as i64;
let dux = sux + self.b.mv_i[0][1] as i64;
let ls = |a: i64, b: i64| ((a * b) >> 2) + (a + b);
for i in 0..self.mvs.num_samples {
let c = self.mvs.cand_list[i];
let sy = c[0] as i64 - suy;
let sx = c[1] as i64 - sux;
let dy = c[2] as i64 - duy;
let dx = c[3] as i64 - dux;
if (sx - dx).abs() < LS_MV_MAX as i64 && (sy - dy).abs() < LS_MV_MAX as i64 {
a[0][0] += ls(sx, sx) + 8;
a[0][1] += ls(sx, sy) + 4;
a[1][1] += ls(sy, sy) + 8;
bx[0] += ls(sx, dx) + 8;
bx[1] += ls(sy, dx) + 4;
by[0] += ls(sx, dy) + 4;
by[1] += ls(sy, dy) + 8;
}
}
let det = a[0][0] * a[1][1] - a[0][1] * a[0][1];
self.b.local_valid = det != 0;
if det == 0 {
return;
}
let (mut div_shift, mut div_factor) = resolve_divisor(det);
div_shift -= WARPEDMODEL_PREC_BITS as i32;
if div_shift < 0 {
div_factor <<= -div_shift;
div_shift = 0;
}
let prec = 1i64 << WARPEDMODEL_PREC_BITS;
let cl = WARPEDMODEL_NONDIAGAFFINE_CLAMP as i64;
let nondiag = |v: i64| round2_signed_wide(v as i128 * div_factor as i128, div_shift as u32).clamp(-cl + 1, cl - 1);
let diag = |v: i64| round2_signed_wide(v as i128 * div_factor as i128, div_shift as u32).clamp(prec - cl + 1, prec + cl - 1);
let mut p = [0i64; 6];
p[2] = diag(a[1][1] * bx[0] - a[0][1] * bx[1]);
p[3] = nondiag(-a[0][1] * bx[0] + a[0][0] * bx[1]);
p[4] = nondiag(a[1][1] * by[0] - a[0][1] * by[1]);
p[5] = diag(-a[0][1] * by[0] + a[0][0] * by[1]);
let mvx = self.b.mv_i[0][1] as i64;
let mvy = self.b.mv_i[0][0] as i64;
let vx = mvx * (1 << (WARPEDMODEL_PREC_BITS - 3)) - (mid_x * (p[2] - prec) + mid_y * p[3]);
let vy = mvy * (1 << (WARPEDMODEL_PREC_BITS - 3)) - (mid_x * p[4] + mid_y * (p[5] - prec));
let tc = WARPEDMODEL_TRANS_CLAMP as i64;
p[0] = vx.clamp(-tc, tc - 1);
p[1] = vy.clamp(-tc, tc - 1);
for i in 0..6 {
self.b.local_warp_params[i] = p[i] as i32;
}
}
/// Inter prediction process (7.11.3.1) for a w x h region of `plane` at (x, y).
#[allow(clippy::too_many_arguments)]
pub(crate) fn predict_inter(&mut self, plane: usize, x: usize, y: usize, w: usize, h: usize, cand_row: usize, cand_col: usize) {
let bd = self.fs.bit_depth;
let mi_i = self.t.mi.idx(cand_row, cand_col);
let cand_refs = self.t.mi.ref_frame[mi_i];
let cand_mvs = self.t.mi.mv[mi_i];
let filters = self.t.mi.interp_filter[mi_i];
let is_compound = cand_refs[1] > INTRA_FRAME as i8;
let rnd = rounding(is_compound, bd);
if plane == 0 && self.b.motion_mode as usize == LOCALWARP {
self.warp_estimation();
if self.b.local_valid {
let p = self.b.local_warp_params;
self.b.local_valid = setup_shear(&p).0;
}
}
let mut preds = [std::mem::take(&mut self.pred_buf[0]), std::mem::take(&mut self.pred_buf[1])];
let mut tmp = std::mem::take(&mut self.mc_tmp);
let fh = &self.fs.fh;
for ref_list in 0..1 + is_compound as usize {
let ref_frame = cand_refs[ref_list];
let ref_idx: i32 = if self.b.use_intrabc { -1 } else { fh.ref_frame_idx[ref_frame as usize - LAST_FRAME] as i32 };
let use_warp = self.warp_mode(ref_frame, ref_idx, w, h);
let mv = [cand_mvs[ref_list].row as i32, cand_mvs[ref_list].col as i32];
if use_warp != 0 {
let params = if use_warp == 1 { self.b.local_warp_params } else { fh.gm_params[ref_frame as usize] };
for i8 in 0..=((h - 1) >> 3) {
for j8 in 0..=((w - 1) >> 3) {
self.block_warp(&params, plane, ref_idx, x as i32, y as i32, i8, j8, w, h, rnd, &mut preds[ref_list]);
}
}
} else {
let (sx, sy, stx, sty) = self.mv_scaling(plane, ref_idx, x as i32, y as i32, mv);
self.block_inter_prediction(plane, ref_idx, sx, sy, stx, sty, w, h, filters, rnd, &mut preds[ref_list], &mut tmp);
}
}
let ct = self.b.compound_type;
self.fill_blend_mask(plane, w, h, &preds, rnd.2);
let (fwd, bck) = if ct == COMPOUND_DISTANCE { self.distance_weights(cand_refs) } else { (0, 0) };
let max = (1i32 << bd) - 1;
let is_inter_intra = self.b.is_inter && self.b.ref_frame[1] == INTRA_FRAME as i8;
if !is_compound && !is_inter_intra {
let pl = &mut self.t.cur.planes[plane];
for i in 0..h {
let row = &mut pl.row_from_mut(y + i, x)[..w];
for j in 0..w {
row[j] = preds[0][i * PRED_STRIDE + j].clamp(0, max) as u16;
}
}
} else if ct == COMPOUND_AVERAGE {
let pl = &mut self.t.cur.planes[plane];
for i in 0..h {
let row = &mut pl.row_from_mut(y + i, x)[..w];
for j in 0..w {
let k = i * PRED_STRIDE + j;
row[j] = round2(preds[0][k] + preds[1][k], 1 + rnd.2).clamp(0, max) as u16;
}
}
} else if ct == COMPOUND_DISTANCE {
let pl = &mut self.t.cur.planes[plane];
for i in 0..h {
let row = &mut pl.row_from_mut(y + i, x)[..w];
for j in 0..w {
let k = i * PRED_STRIDE + j;
row[j] = round2(fwd * preds[0][k] + bck * preds[1][k], 4 + rnd.2).clamp(0, max) as u16;
}
}
} else {
self.mask_blend(&preds, plane, x, y, w, h, rnd.2, max);
}
self.pred_buf = preds;
self.mc_tmp = tmp;
if self.b.motion_mode as usize == OBMC {
self.overlapped_motion_compensation(plane, w, h);
}
}
/// Warp selection for one reference: 0 none, 1 local warp, 2 global warp.
fn warp_mode(&self, ref_frame: i8, ref_idx: i32, w: usize, h: usize) -> u8 {
let fh = &self.fs.fh;
let ym = self.b.y_mode;
let global =
(ym == GLOBALMV || ym == GLOBAL_GLOBALMV) && ref_frame > INTRA_FRAME as i8 && fh.gm_type[ref_frame as usize] as usize > TRANSLATION;
let global_valid = global && setup_shear(&fh.gm_params[ref_frame as usize]).0;
if w < 8 || h < 8 || fh.force_integer_mv {
0
} else if self.b.motion_mode as usize == LOCALWARP && self.b.local_valid {
1
} else if global && !self.is_scaled_ref(ref_idx) && global_valid {
2
} else {
0
}
}
/// Builds the blend mask (wedge, inter-intra or difference-weighted) for the current compound type.
fn fill_blend_mask(&mut self, plane: usize, w: usize, h: usize, preds: &[Vec<i32>; 2], round_post: u32) {
let bd = self.fs.bit_depth;
let ct = self.b.compound_type;
if ct == COMPOUND_WEDGE && plane == 0 {
let m = &wedge_masks()[self.b.mi_size][self.b.wedge_index][self.b.wedge_sign];
for i in 0..h {
for j in 0..w {
self.mask[i * PRED_STRIDE + j] = m[i * w + j] as i32;
}
}
} else if ct == COMPOUND_INTRA {
let size_scale = MAX_SB_SIZE / h.max(w);
let im = self.b.interintra_mode;
for i in 0..h {
for j in 0..w {
self.mask[i * PRED_STRIDE + j] = if im == II_V_PRED {
II_WEIGHTS_1D[i * size_scale] as i32
} else if im == II_H_PRED {
II_WEIGHTS_1D[j * size_scale] as i32
} else if im == II_SMOOTH_PRED {
II_WEIGHTS_1D[i.min(j) * size_scale] as i32
} else {
32
};
}
}
} else if ct == COMPOUND_DIFFWTD && plane == 0 {
for i in 0..h {
for j in 0..w {
let k = i * PRED_STRIDE + j;
let diff = (preds[0][k] - preds[1][k]).abs();
let diff = round2(diff, (bd - 8) + round_post);
let m = (38 + diff / 16).clamp(0, 64);
self.mask[k] = if self.b.mask_type == 1 { 64 - m } else { m };
}
}
}
}
fn is_scaled_ref(&self, ref_idx: i32) -> bool {
if ref_idx < 0 {
return false;
}
let fh = &self.fs.fh;
let ri = &self.fs.ref_info[ref_idx as usize];
let xs = ((ri.upscaled_width << REF_SCALE_SHIFT) + fh.frame_width / 2) / fh.frame_width;
let ys = ((ri.frame_height << REF_SCALE_SHIFT) + fh.frame_height / 2) / fh.frame_height;
xs != 1 << REF_SCALE_SHIFT || ys != 1 << REF_SCALE_SHIFT
}
#[allow(clippy::too_many_arguments)]
fn mask_blend(&mut self, preds: &[Vec<i32>; 2], plane: usize, dx: usize, dy: usize, w: usize, h: usize, post: u32, max: i32) {
let (sx, sy) = if plane > 0 { (self.fs.ssx, self.fs.ssy) } else { (0, 0) };
let interintra = self.b.interintra;
let wedge_ii = self.b.wedge_interintra;
for yy in 0..h {
for xx in 0..w {
let mk = |r: usize, c: usize| self.mask[r * PRED_STRIDE + c];
let m = if (sx == 0 && sy == 0) || (interintra && !wedge_ii) {
mk(yy, xx)
} else if sx == 1 && sy == 0 {
round2(mk(yy, 2 * xx) + mk(yy, 2 * xx + 1), 1)
} else {
round2(mk(2 * yy, 2 * xx) + mk(2 * yy, 2 * xx + 1) + mk(2 * yy + 1, 2 * xx) + mk(2 * yy + 1, 2 * xx + 1), 2)
};
let k = yy * PRED_STRIDE + xx;
let pl = &mut self.t.cur.planes[plane];
if interintra {
let p0 = round2(preds[0][k], post).clamp(0, max);
let p1 = pl.at(dx + xx, dy + yy) as i32;
pl.set(dx + xx, dy + yy, round2(m * p1 + (64 - m) * p0, 6) as u16);
} else {
let v = round2(m * preds[0][k] + (64 - m) * preds[1][k], 6 + post).clamp(0, max);
pl.set(dx + xx, dy + yy, v as u16);
}
}
}
}
fn distance_weights(&self, refs: [i8; 2]) -> (i32, i32) {
let fh = &self.fs.fh;
let mut dist = [0i32; 2];
for (rl, d) in dist.iter_mut().enumerate() {
let h = fh.order_hints[refs[rl] as usize];
*d = crate::header::relative_dist(&self.fs.seq, h, fh.order_hint).abs().clamp(0, MAX_FRAME_DISTANCE as i32);
}
let d0 = dist[1];
let d1 = dist[0];
let order = (d0 <= d1) as usize;
if d0 == 0 || d1 == 0 {
return (QUANT_DIST_LOOKUP[3][order] as i32, QUANT_DIST_LOOKUP[3][1 - order] as i32);
}
let mut i = 0;
while i < 3 {
let c0 = QUANT_DIST_WEIGHT[i][order] as i32;
let c1 = QUANT_DIST_WEIGHT[i][1 - order] as i32;
if order == 1 {
if d0 * c0 > d1 * c1 {
break;
}
} else if d0 * c0 < d1 * c1 {
break;
}
i += 1;
}
(QUANT_DIST_LOOKUP[i][order] as i32, QUANT_DIST_LOOKUP[i][1 - order] as i32)
}
/// Overlapped motion compensation (7.11.3.9).
fn overlapped_motion_compensation(&mut self, plane: usize, w: usize, h: usize) {
let (sx, sy) = if plane > 0 { (self.fs.ssx, self.fs.ssy) } else { (0, 0) };
let (mi_rows, mi_cols) = (self.fs.fh.mi_rows as usize, self.fs.fh.mi_cols as usize);
let bd = self.fs.bit_depth;
let max = (1i32 << bd) - 1;
let mut obmc = std::mem::take(&mut self.pred_buf[0]);
let mut tmp = std::mem::take(&mut self.mc_tmp);
let get_mask = |len: usize| -> &'static [u8] {
match len {
2 => &OBMC_MASK_2,
4 => &OBMC_MASK_4,
8 => &OBMC_MASK_8,
16 => &OBMC_MASK_16,
_ => &OBMC_MASK_32,
}
};
let mut jobs: Vec<(usize, usize, usize, usize, usize, usize, usize)> = Vec::new(); // (pass, cand_row, cand_col, x4, y4, predW, predH)
if self.b.avail_u && self.fs.plane_residual_size(self.b.mi_size, plane) >= BLOCK_8X8 {
let w4 = self.b.bw4;
let mut x4 = self.b.mi_col;
let y4 = self.b.mi_row;
let mut n = 0;
let limit = 4.min(MI_WIDTH_LOG2[self.b.mi_size] as usize);
while n < limit && x4 < mi_cols.min(self.b.mi_col + w4) {
let cr = self.b.mi_row - 1;
let cc = x4 | 1;
let mi = &self.t.mi;
let ci = mi.idx(cr, cc);
let step4 = (NUM_4X4_BLOCKS_WIDE[mi.mi_size[ci] as usize] as usize).clamp(2, 16);
if mi.ref_frame[ci][0] > INTRA_FRAME as i8 {
n += 1;
let pw = w.min((step4 * 4) >> sx);
let ph = (h >> 1).min(32 >> sy);
jobs.push((0, cr, cc, x4, y4, pw, ph));
}
x4 += step4;
}
}
if self.b.avail_l {
let h4 = self.b.bh4;
let x4 = self.b.mi_col;
let mut y4 = self.b.mi_row;
let mut n = 0;
let limit = 4.min(MI_HEIGHT_LOG2[self.b.mi_size] as usize);
while n < limit && y4 < mi_rows.min(self.b.mi_row + h4) {
let cc = self.b.mi_col - 1;
let cr = y4 | 1;
let mi = &self.t.mi;
let ci = mi.idx(cr, cc);
let step4 = (NUM_4X4_BLOCKS_HIGH[mi.mi_size[ci] as usize] as usize).clamp(2, 16);
if mi.ref_frame[ci][0] > INTRA_FRAME as i8 {
n += 1;
let pw = (w >> 1).min(32 >> sx);
let ph = h.min((step4 * 4) >> sy);
jobs.push((1, cr, cc, x4, y4, pw, ph));
}
y4 += step4;
}
}
// the above pass completes before the left pass reads samples; jobs keep that order
for (pass, cr, cc, x4, y4, pw, ph) in jobs {
let ci = self.t.mi.idx(cr, cc);
let m = self.t.mi.mv[ci][0];
let mv = [m.row as i32, m.col as i32];
let ref_idx = self.fs.fh.ref_frame_idx[self.t.mi.ref_frame[ci][0] as usize - LAST_FRAME] as i32;
let px = (x4 * 4) >> sx;
let py = (y4 * 4) >> sy;
let (stx, sty, sxs, sys) = self.mv_scaling(plane, ref_idx, px as i32, py as i32, mv);
let filters = self.t.mi.interp_filter[ci];
let rnd = rounding(false, bd);
self.block_inter_prediction(plane, ref_idx, stx, sty, sxs, sys, pw, ph, filters, rnd, &mut obmc, &mut tmp);
let mask = get_mask(if pass == 0 { ph } else { pw });
let pl = &mut self.t.cur.planes[plane];
for i in 0..ph {
for j in 0..pw {
let mm = if pass == 0 { mask[i] } else { mask[j] } as i32;
let o = obmc[i * PRED_STRIDE + j].clamp(0, max);
let c = pl.at(px + j, py + i) as i32;
pl.set(px + j, py + i, round2(mm * c + (64 - mm) * o, 6) as u16);
}
}
}
self.pred_buf[0] = obmc;
self.mc_tmp = tmp;
}
}
const IDENTITY_TAPS: [i16; 8] = [0, 0, 0, 128, 0, 0, 0, 0];
/// Horizontal pass of one row for a block `W` wide: taps outer, columns inner, fixed width so
/// the row stays in registers.
#[inline(always)]
fn hpass<const W: usize>(src: &[u16], hf: &[i16; 8], out: &mut [i32], half: i32, sh: u32) {
let mut acc = [half; W];
for t in 0..8 {
let f = hf[t] as i32;
let Some(p) = src.get(t..).and_then(<[_]>::first_chunk::<W>) else {
return;
};
for j in 0..W {
acc[j] += f * p[j] as i32;
}
}
let Some(o) = out.first_chunk_mut::<W>() else {
return;
};
for j in 0..W {
o[j] = acc[j] >> sh;
}
}
/// Vertical pass of one output row from 8 intermediate rows (`rows`, stride `W`).
#[inline(always)]
fn vpass<const W: usize>(rows: &[i32], vf: &[i16; 8], out: &mut [i32], half: i32, sh: u32) {
let mut acc = [half; W];
for t in 0..8 {
let f = vf[t] as i32;
let Some(p) = rows.get(t * W..).and_then(<[_]>::first_chunk::<W>) else {
return;
};
for j in 0..W {
acc[j] += f * p[j];
}
}
let Some(o) = out.first_chunk_mut::<W>() else {
return;
};
for j in 0..W {
o[j] = acc[j] >> sh;
}
}
/// Block inter prediction without reference scaling: every column / row uses the same filter
/// phase, so the filters run over whole rows (bit-identical to the per-sample process).
#[allow(clippy::too_many_arguments)]
fn mc_unscaled(
refp: &Plane,
last_x: i32,
last_y: i32,
x: i32,
y: i32,
w: usize,
h: usize,
hf: &[i16; 8],
vf: &[i16; 8],
rnd: (u32, u32, u32),
pred: &mut [i32],
tmp: &mut [i32],
) {
let x0 = (x >> 10) - 3;
let y0 = (y >> 10) - 3;
let h_copy = *hf == IDENTITY_TAPS;
let v_copy = *vf == IDENTITY_TAPS;
// With the identity vertical phase only intermediate rows 3 .. 3 + h have a non-zero tap.
let (r_lo, r_hi) = if v_copy { (3, 3 + h) } else { (0, h + 7) };
let inside = x0 >= 0 && x0 + w as i32 + 7 <= last_x + 1;
let mut edge = [0u16; 128 + 8];
let mut acc = [0i32; 128];
let (r0, r1) = (rnd.0, rnd.1);
let (half0, half1) = (1i32 << (r0 - 1), 1i32 << (r1 - 1));
for r in r_lo..r_hi {
let ry = (y0 + r as i32).clamp(0, last_y) as usize;
let row = refp.row(ry);
let src: &[u16] = if inside {
&row[x0 as usize..x0 as usize + w + 7]
} else {
for (k, e) in edge[..w + 7].iter_mut().enumerate() {
*e = row[(x0 + k as i32).clamp(0, last_x) as usize];
}
&edge[..w + 7]
};
let out = &mut tmp[r * w..r * w + w];
if h_copy {
// Round2(128 * v, InterRound0) is exact.
for (o, &v) in out.iter_mut().zip(&src[3..3 + w]) {
*o = (v as i32) << (7 - r0);
}
} else {
match w {
2 => hpass::<2>(src, hf, out, half0, r0),
4 => hpass::<4>(src, hf, out, half0, r0),
8 => hpass::<8>(src, hf, out, half0, r0),
16 => hpass::<16>(src, hf, out, half0, r0),
32 => hpass::<32>(src, hf, out, half0, r0),
64 => hpass::<64>(src, hf, out, half0, r0),
_ => {
let acc = &mut acc[..w];
acc.fill(0);
for t in 0..8 {
let f = hf[t] as i32;
for (a, &v) in acc.iter_mut().zip(&src[t..t + w]) {
*a += f * v as i32;
}
}
for (o, &a) in out.iter_mut().zip(acc.iter()) {
*o = (a + half0) >> r0;
}
}
}
}
}
for r in 0..h {
let out = &mut pred[r * PRED_STRIDE..r * PRED_STRIDE + w];
if v_copy {
for (o, &v) in out.iter_mut().zip(&tmp[(r + 3) * w..(r + 4) * w]) {
*o = (128 * v + half1) >> r1;
}
} else {
let rows = &tmp[r * w..(r + 8) * w];
match w {
2 => vpass::<2>(rows, vf, out, half1, r1),
4 => vpass::<4>(rows, vf, out, half1, r1),
8 => vpass::<8>(rows, vf, out, half1, r1),
16 => vpass::<16>(rows, vf, out, half1, r1),
32 => vpass::<32>(rows, vf, out, half1, r1),
64 => vpass::<64>(rows, vf, out, half1, r1),
_ => {
let acc = &mut acc[..w];
acc.fill(0);
for t in 0..8 {
let f = vf[t] as i32;
for (a, &v) in acc.iter_mut().zip(&tmp[(r + t) * w..(r + t + 1) * w]) {
*a += f * v;
}
}
for (o, &a) in out.iter_mut().zip(acc.iter()) {
*o = (a + half1) >> r1;
}
}
}
}
}
}
-777
View File
@@ -1,777 +0,0 @@
//! Inter frame mode info syntax (5.11.7 / 5.11.18 – 5.11.33) with its CDF selection (8.3.2).
use crate::Result;
use crate::header::{EIGHTTAP, SWITCHABLE};
use crate::mvpred::MvP;
use crate::spec_tables::{
ALTREF_FRAME, ALTREF2_FRAME, BLOCK_8X8, BLOCK_32X32, BLOCK_64X64, BLOCK_128X128, BWDREF_FRAME, CLASS0_SIZE, COMP_NEWMV_CTXS, COMPOUND_AVERAGE,
COMPOUND_DIFFWTD, COMPOUND_DISTANCE, COMPOUND_INTRA, COMPOUND_MODE_CTX_MAP, COMPOUND_REFERENCE, COMPOUND_WEDGE, GLOBAL_GLOBALMV, GLOBALMV,
GOLDEN_FRAME, INTRA_FRAME, INTRABC_DELAY_PIXELS, LAST_FRAME, LAST2_FRAME, LAST3_FRAME, LOCALWARP, MV_CLASS_0, MV_INTRABC_CONTEXT,
MV_JOINT_HNZVNZ, MV_JOINT_HNZVZ, MV_JOINT_HZVNZ, NEAR_NEARMV, NEAR_NEWMV, NEAREST_NEARESTMV, NEAREST_NEWMV, NEARESTMV, NEARMV, NEW_NEARESTMV,
NEW_NEARMV, NEW_NEWMV, NEWMV, NUM_4X4_BLOCKS_HIGH, NUM_4X4_BLOCKS_WIDE, OBMC, REF_SCALE_SHIFT, SEG_LVL_GLOBALMV, SEG_LVL_REF_FRAME, SEG_LVL_SKIP,
SIMPLE, SINGLE_REFERENCE, SIZE_GROUP, TRANSLATION, UNIDIR_COMP_REFERENCE, UV_CFL_PRED, WEDGE_BITS,
};
use crate::tile::TileDecoder;
fn block_width(bs: usize) -> usize {
4 * NUM_4X4_BLOCKS_WIDE[bs] as usize
}
fn block_height(bs: usize) -> usize {
4 * NUM_4X4_BLOCKS_HIGH[bs] as usize
}
/// Neighbour reference frame summary (LeftRefFrame / AboveRefFrame and the derived flags).
#[derive(Clone, Copy, Default)]
pub(crate) struct Neighbours {
pub left: [i8; 2],
pub above: [i8; 2],
pub left_intra: bool,
pub above_intra: bool,
pub left_single: bool,
pub above_single: bool,
}
impl TileDecoder<'_, '_> {
pub(crate) fn inter_frame_mode_info_impl(&mut self) -> Result<()> {
self.b.use_intrabc = false;
let (r, c) = (self.b.mi_row, self.b.mi_col);
let mi = &self.t.mi;
let mut n = Neighbours {
left: if self.b.avail_l { mi.ref_frame[mi.idx(r, c - 1)] } else { [INTRA_FRAME as i8, -1] },
above: if self.b.avail_u { mi.ref_frame[mi.idx(r - 1, c)] } else { [INTRA_FRAME as i8, -1] },
..Default::default()
};
if !self.b.avail_l {
n.left[1] = -1;
}
if !self.b.avail_u {
n.above[1] = -1;
}
n.left_intra = n.left[0] <= INTRA_FRAME as i8;
n.above_intra = n.above[0] <= INTRA_FRAME as i8;
n.left_single = n.left[1] <= INTRA_FRAME as i8;
n.above_single = n.above[1] <= INTRA_FRAME as i8;
self.nb = n;
self.b.skip = false;
self.inter_segment_id(true);
self.read_skip_mode();
if self.b.skip_mode {
self.b.skip = true;
} else {
self.read_skip();
}
if !self.fs.fh.seg.seg_id_pre_skip {
self.inter_segment_id(false);
}
self.b.lossless = self.fs.fh.lossless_array[self.b.segment_id];
self.read_cdef();
self.read_delta_qindex();
self.read_delta_lf();
self.read_deltas = false;
self.read_is_inter();
if self.b.is_inter { self.inter_block_mode_info() } else { self.intra_block_mode_info() }
}
fn get_segment_id(&self) -> usize {
let (mi_rows, mi_cols) = (self.fs.fh.mi_rows as usize, self.fs.fh.mi_cols as usize);
let x_mis = (mi_cols - self.b.mi_col).min(self.b.bw4);
let y_mis = (mi_rows - self.b.mi_row).min(self.b.bh4);
let mut seg = 7u8;
for y in 0..y_mis {
for x in 0..x_mis {
let i = (self.b.mi_row + y) * self.t.mi.cols + self.b.mi_col + x;
seg = seg.min(self.fs.prev_segment_ids[i]);
}
}
seg as usize
}
fn set_seg_pred_context(&mut self, v: u8) {
for i in 0..self.b.bw4 {
if self.b.mi_col + i < self.t.above_seg_pred.len() {
self.t.above_seg_pred[self.b.mi_col + i] = v;
}
}
for i in 0..self.b.bh4 {
if self.b.mi_row + i < self.t.left_seg_pred.len() {
self.t.left_seg_pred[self.b.mi_row + i] = v;
}
}
}
fn inter_segment_id(&mut self, pre_skip: bool) {
let seg = &self.fs.fh.seg;
if !seg.enabled {
self.b.segment_id = 0;
return;
}
let predicted = self.get_segment_id();
if seg.update_map {
if pre_skip && !seg.seg_id_pre_skip {
self.b.segment_id = 0;
return;
}
if !pre_skip && self.b.skip {
self.set_seg_pred_context(0);
self.read_segment_id();
return;
}
if seg.temporal_update {
let ctx = self.t.left_seg_pred[self.b.mi_row] as usize + self.t.above_seg_pred[self.b.mi_col] as usize;
let pred_flag = self.sd.read_symbol(&mut self.cdf.segment_id_predicted[ctx]);
if pred_flag == 1 {
self.b.segment_id = predicted;
} else {
self.read_segment_id();
}
self.set_seg_pred_context(pred_flag as u8);
} else {
self.read_segment_id();
}
} else {
self.b.segment_id = predicted;
}
}
fn read_skip_mode(&mut self) {
let ms = self.b.mi_size;
if self.seg_active(SEG_LVL_SKIP)
|| self.seg_active(SEG_LVL_REF_FRAME)
|| self.seg_active(SEG_LVL_GLOBALMV)
|| !self.fs.fh.skip_mode_present
|| block_width(ms) < 8
|| block_height(ms) < 8
{
self.b.skip_mode = false;
} else {
let (r, c) = (self.b.mi_row, self.b.mi_col);
let mi = &self.t.mi;
let mut ctx = 0;
if self.b.avail_u {
ctx += mi.skip_mode[mi.idx(r - 1, c)] as usize;
}
if self.b.avail_l {
ctx += mi.skip_mode[mi.idx(r, c - 1)] as usize;
}
self.b.skip_mode = self.sd.read_symbol(&mut self.cdf.skip_mode[ctx]) == 1;
}
}
pub(crate) fn seg_active(&self, feature: usize) -> bool {
self.fs.fh.seg.enabled && self.fs.fh.seg.features.enabled[self.b.segment_id][feature]
}
fn read_is_inter(&mut self) {
if self.b.skip_mode {
self.b.is_inter = true;
} else if self.seg_active(SEG_LVL_REF_FRAME) {
self.b.is_inter = self.fs.fh.seg.features.data[self.b.segment_id][SEG_LVL_REF_FRAME] != INTRA_FRAME as i32;
} else if self.seg_active(SEG_LVL_GLOBALMV) {
self.b.is_inter = true;
} else {
let n = self.nb;
let ctx = if self.b.avail_u && self.b.avail_l {
if n.left_intra && n.above_intra { 3 } else { (n.left_intra || n.above_intra) as usize }
} else if self.b.avail_u || self.b.avail_l {
2 * (if self.b.avail_u { n.above_intra } else { n.left_intra }) as usize
} else {
0
};
self.b.is_inter = self.sd.read_symbol(&mut self.cdf.is_inter[ctx]) == 1;
}
}
fn intra_block_mode_info(&mut self) -> Result<()> {
self.b.ref_frame = [INTRA_FRAME as i8, -1];
let ctx = SIZE_GROUP[self.b.mi_size] as usize;
self.b.y_mode = self.sd.read_symbol(&mut self.cdf.y_mode[ctx]);
self.intra_angle_info_y();
if self.b.has_chroma {
self.read_uv_mode();
if self.b.uv_mode == UV_CFL_PRED {
self.read_cfl_alphas();
}
self.intra_angle_info_uv();
}
self.b.palette_size_y = 0;
self.b.palette_size_uv = 0;
let ms = self.b.mi_size;
if ms >= BLOCK_8X8 && block_width(ms) <= 64 && block_height(ms) <= 64 && self.fs.fh.allow_screen_content_tools {
self.palette_mode_info();
}
self.filter_intra_mode_info();
Ok(())
}
fn count_refs(&self, frame_type: i8) -> usize {
let n = self.nb;
let mut c = 0;
if self.b.avail_u {
c += (n.above[0] == frame_type) as usize + (n.above[1] == frame_type) as usize;
}
if self.b.avail_l {
c += (n.left[0] == frame_type) as usize + (n.left[1] == frame_type) as usize;
}
c
}
fn ref_count_ctx(c0: usize, c1: usize) -> usize {
match c0.cmp(&c1) {
std::cmp::Ordering::Less => 0,
std::cmp::Ordering::Equal => 1,
std::cmp::Ordering::Greater => 2,
}
}
fn ctx_comp_ref(&self) -> usize {
Self::ref_count_ctx(
self.count_refs(LAST_FRAME as i8) + self.count_refs(LAST2_FRAME as i8),
self.count_refs(LAST3_FRAME as i8) + self.count_refs(GOLDEN_FRAME as i8),
)
}
fn ctx_comp_ref_p1(&self) -> usize {
Self::ref_count_ctx(self.count_refs(LAST_FRAME as i8), self.count_refs(LAST2_FRAME as i8))
}
fn ctx_comp_ref_p2(&self) -> usize {
Self::ref_count_ctx(self.count_refs(LAST3_FRAME as i8), self.count_refs(GOLDEN_FRAME as i8))
}
fn ctx_comp_bwdref(&self) -> usize {
Self::ref_count_ctx(self.count_refs(BWDREF_FRAME as i8) + self.count_refs(ALTREF2_FRAME as i8), self.count_refs(ALTREF_FRAME as i8))
}
fn ctx_comp_bwdref_p1(&self) -> usize {
Self::ref_count_ctx(self.count_refs(BWDREF_FRAME as i8), self.count_refs(ALTREF2_FRAME as i8))
}
fn ctx_single_ref_p1(&self) -> usize {
let fwd = self.count_refs(LAST_FRAME as i8)
+ self.count_refs(LAST2_FRAME as i8)
+ self.count_refs(LAST3_FRAME as i8)
+ self.count_refs(GOLDEN_FRAME as i8);
let bwd = self.count_refs(BWDREF_FRAME as i8) + self.count_refs(ALTREF2_FRAME as i8) + self.count_refs(ALTREF_FRAME as i8);
Self::ref_count_ctx(fwd, bwd)
}
fn read_ref_frames(&mut self) {
if self.b.skip_mode {
let f = self.fs.fh.skip_mode_frame;
self.b.ref_frame = [f[0] as i8, f[1] as i8];
} else if self.seg_active(SEG_LVL_REF_FRAME) {
self.b.ref_frame = [self.fs.fh.seg.features.data[self.b.segment_id][SEG_LVL_REF_FRAME] as i8, -1];
} else if self.seg_active(SEG_LVL_SKIP) || self.seg_active(SEG_LVL_GLOBALMV) {
self.b.ref_frame = [LAST_FRAME as i8, -1];
} else {
let comp_mode = if self.fs.fh.reference_select && self.b.bw4.min(self.b.bh4) >= 2 {
let ctx = self.comp_mode_ctx();
self.sd.read_symbol(&mut self.cdf.comp_mode[ctx])
} else {
SINGLE_REFERENCE
};
if comp_mode == COMPOUND_REFERENCE {
let ctx = self.comp_ref_type_ctx();
let comp_ref_type = self.sd.read_symbol(&mut self.cdf.comp_ref_type[ctx]);
if comp_ref_type == UNIDIR_COMP_REFERENCE {
let ctx = self.ctx_single_ref_p1();
if self.sd.read_symbol(&mut self.cdf.uni_comp_ref[ctx][0]) == 1 {
self.b.ref_frame = [BWDREF_FRAME as i8, ALTREF_FRAME as i8];
} else {
let ctx1 = Self::ref_count_ctx(
self.count_refs(LAST2_FRAME as i8),
self.count_refs(LAST3_FRAME as i8) + self.count_refs(GOLDEN_FRAME as i8),
);
if self.sd.read_symbol(&mut self.cdf.uni_comp_ref[ctx1][1]) == 1 {
let ctx2 = self.ctx_comp_ref_p2();
if self.sd.read_symbol(&mut self.cdf.uni_comp_ref[ctx2][2]) == 1 {
self.b.ref_frame = [LAST_FRAME as i8, GOLDEN_FRAME as i8];
} else {
self.b.ref_frame = [LAST_FRAME as i8, LAST3_FRAME as i8];
}
} else {
self.b.ref_frame = [LAST_FRAME as i8, LAST2_FRAME as i8];
}
}
} else {
let ctx = self.ctx_comp_ref();
if self.sd.read_symbol(&mut self.cdf.comp_ref[ctx][0]) == 0 {
let ctx = self.ctx_comp_ref_p1();
let p1 = self.sd.read_symbol(&mut self.cdf.comp_ref[ctx][1]);
self.b.ref_frame[0] = if p1 == 1 { LAST2_FRAME } else { LAST_FRAME } as i8;
} else {
let ctx = self.ctx_comp_ref_p2();
let p2 = self.sd.read_symbol(&mut self.cdf.comp_ref[ctx][2]);
self.b.ref_frame[0] = if p2 == 1 { GOLDEN_FRAME } else { LAST3_FRAME } as i8;
}
let ctx = self.ctx_comp_bwdref();
if self.sd.read_symbol(&mut self.cdf.comp_bwd_ref[ctx][0]) == 0 {
let ctx = self.ctx_comp_bwdref_p1();
let p1 = self.sd.read_symbol(&mut self.cdf.comp_bwd_ref[ctx][1]);
self.b.ref_frame[1] = if p1 == 1 { ALTREF2_FRAME } else { BWDREF_FRAME } as i8;
} else {
self.b.ref_frame[1] = ALTREF_FRAME as i8;
}
}
} else {
let ctx = self.ctx_single_ref_p1();
if self.sd.read_symbol(&mut self.cdf.single_ref[ctx][0]) == 1 {
let ctx = self.ctx_comp_bwdref();
if self.sd.read_symbol(&mut self.cdf.single_ref[ctx][1]) == 0 {
let ctx = self.ctx_comp_bwdref_p1();
let p6 = self.sd.read_symbol(&mut self.cdf.single_ref[ctx][5]);
self.b.ref_frame[0] = if p6 == 1 { ALTREF2_FRAME } else { BWDREF_FRAME } as i8;
} else {
self.b.ref_frame[0] = ALTREF_FRAME as i8;
}
} else {
let ctx = self.ctx_comp_ref();
if self.sd.read_symbol(&mut self.cdf.single_ref[ctx][2]) == 1 {
let ctx = self.ctx_comp_ref_p2();
let p5 = self.sd.read_symbol(&mut self.cdf.single_ref[ctx][4]);
self.b.ref_frame[0] = if p5 == 1 { GOLDEN_FRAME } else { LAST3_FRAME } as i8;
} else {
let ctx = self.ctx_comp_ref_p1();
let p4 = self.sd.read_symbol(&mut self.cdf.single_ref[ctx][3]);
self.b.ref_frame[0] = if p4 == 1 { LAST2_FRAME } else { LAST_FRAME } as i8;
}
}
self.b.ref_frame[1] = -1;
}
}
}
fn check_backward(r: i8) -> bool {
r >= BWDREF_FRAME as i8 && r <= ALTREF_FRAME as i8
}
fn comp_mode_ctx(&self) -> usize {
let n = self.nb;
let (au, al) = (self.b.avail_u, self.b.avail_l);
if au && al {
if n.above_single && n.left_single {
(Self::check_backward(n.above[0]) ^ Self::check_backward(n.left[0])) as usize
} else if n.above_single {
2 + (Self::check_backward(n.above[0]) || n.above_intra) as usize
} else if n.left_single {
2 + (Self::check_backward(n.left[0]) || n.left_intra) as usize
} else {
4
}
} else if au {
if n.above_single { Self::check_backward(n.above[0]) as usize } else { 3 }
} else if al {
if n.left_single { Self::check_backward(n.left[0]) as usize } else { 3 }
} else {
1
}
}
fn comp_ref_type_ctx(&self) -> usize {
let n = self.nb;
let (au, al) = (self.b.avail_u, self.b.avail_l);
let samedir = |a: i8, b: i8| (a >= BWDREF_FRAME as i8) == (b >= BWDREF_FRAME as i8);
let (above0, above1, left0, left1) = (n.above[0], n.above[1], n.left[0], n.left[1]);
let above_comp_inter = au && !n.above_intra && !n.above_single;
let left_comp_inter = al && !n.left_intra && !n.left_single;
let above_uni = above_comp_inter && samedir(above0, above1);
let left_uni = left_comp_inter && samedir(left0, left1);
if au && !n.above_intra && al && !n.left_intra {
let sd = samedir(above0, left0) as usize;
if !above_comp_inter && !left_comp_inter {
1 + 2 * sd
} else if !above_comp_inter {
if !left_uni { 1 } else { 3 + sd }
} else if !left_comp_inter {
if !above_uni { 1 } else { 3 + sd }
} else if !above_uni && !left_uni {
0
} else if !above_uni || !left_uni {
2
} else {
3 + ((above0 == BWDREF_FRAME as i8) == (left0 == BWDREF_FRAME as i8)) as usize
}
} else if au && al {
if above_comp_inter {
1 + 2 * above_uni as usize
} else if left_comp_inter {
1 + 2 * left_uni as usize
} else {
2
}
} else if above_comp_inter {
4 * above_uni as usize
} else if left_comp_inter {
4 * left_uni as usize
} else {
2
}
}
fn inter_block_mode_info(&mut self) -> Result<()> {
self.b.palette_size_y = 0;
self.b.palette_size_uv = 0;
self.read_ref_frames();
let is_compound = self.b.ref_frame[1] > INTRA_FRAME as i8;
self.find_mv_stack(is_compound);
if self.b.skip_mode {
self.b.y_mode = NEAREST_NEARESTMV;
} else if self.seg_active(SEG_LVL_SKIP) || self.seg_active(SEG_LVL_GLOBALMV) {
self.b.y_mode = GLOBALMV;
} else if is_compound {
let ctx = COMPOUND_MODE_CTX_MAP[self.mvs.ref_mv_context >> 1][self.mvs.new_mv_context.min(COMP_NEWMV_CTXS - 1)] as usize;
self.b.y_mode = NEAREST_NEARESTMV + self.sd.read_symbol(&mut self.cdf.compound_mode[ctx]);
} else {
let new_mv = self.sd.read_symbol(&mut self.cdf.new_mv[self.mvs.new_mv_context]);
if new_mv == 0 {
self.b.y_mode = NEWMV;
} else {
let zero_mv = self.sd.read_symbol(&mut self.cdf.zero_mv[self.mvs.zero_mv_context]);
if zero_mv == 0 {
self.b.y_mode = GLOBALMV;
} else {
let ref_mv = self.sd.read_symbol(&mut self.cdf.ref_mv[self.mvs.ref_mv_context]);
self.b.y_mode = if ref_mv == 0 { NEARESTMV } else { NEARMV };
}
}
}
self.b.ref_mv_idx = 0;
let ym = self.b.y_mode;
if ym == NEWMV || ym == NEW_NEWMV {
for idx in 0..2 {
if self.mvs.num_mv_found > idx + 1 {
let ctx = self.mvs.drl_ctx_stack[idx];
let drl = self.sd.read_symbol(&mut self.cdf.drl_mode[ctx]);
if drl == 0 {
self.b.ref_mv_idx = idx;
break;
}
self.b.ref_mv_idx = idx + 1;
}
}
} else if matches!(ym, NEARMV | NEAR_NEARMV | NEAR_NEWMV | NEW_NEARMV) {
self.b.ref_mv_idx = 1;
for idx in 1..3 {
if self.mvs.num_mv_found > idx + 1 {
let ctx = self.mvs.drl_ctx_stack[idx];
let drl = self.sd.read_symbol(&mut self.cdf.drl_mode[ctx]);
if drl == 0 {
self.b.ref_mv_idx = idx;
break;
}
self.b.ref_mv_idx = idx + 1;
}
}
}
self.assign_mv(is_compound)?;
self.read_interintra_mode(is_compound);
self.read_motion_mode(is_compound);
self.read_compound_type(is_compound);
if self.fs.fh.interpolation_filter == SWITCHABLE {
let dual = self.fs.seq.enable_dual_filter;
for dir in 0..if dual { 2 } else { 1 } {
if self.needs_interp_filter() {
let ctx = self.interp_filter_ctx(dir);
self.b.interp_filter[dir] = self.sd.read_symbol(&mut self.cdf.interp_filter[ctx]) as u8;
} else {
self.b.interp_filter[dir] = EIGHTTAP;
}
}
if !dual {
self.b.interp_filter[1] = self.b.interp_filter[0];
}
} else {
self.b.interp_filter = [self.fs.fh.interpolation_filter; 2];
}
Ok(())
}
fn needs_interp_filter(&self) -> bool {
let ms = self.b.mi_size;
let large = block_width(ms).min(block_height(ms)) >= 8;
let gm = &self.fs.fh.gm_type;
if self.b.skip_mode || self.b.motion_mode as usize == LOCALWARP {
false
} else if large && self.b.y_mode == GLOBALMV {
gm[self.b.ref_frame[0] as usize] as usize == TRANSLATION
} else if large && self.b.y_mode == GLOBAL_GLOBALMV {
gm[self.b.ref_frame[0] as usize] as usize == TRANSLATION || gm[self.b.ref_frame[1] as usize] as usize == TRANSLATION
} else {
true
}
}
fn interp_filter_ctx(&self, dir: usize) -> usize {
let mut ctx = ((dir & 1) * 2 + (self.b.ref_frame[1] > INTRA_FRAME as i8) as usize) * 4;
let mut left_type = 3u8;
let mut above_type = 3u8;
let (r, c) = (self.b.mi_row, self.b.mi_col);
let mi = &self.t.mi;
let rf0 = self.b.ref_frame[0];
if self.b.avail_l {
let i = mi.idx(r, c - 1);
if mi.ref_frame[i][0] == rf0 || mi.ref_frame[i][1] == rf0 {
left_type = mi.interp_filter[i][dir];
}
}
if self.b.avail_u {
let i = mi.idx(r - 1, c);
if mi.ref_frame[i][0] == rf0 || mi.ref_frame[i][1] == rf0 {
above_type = mi.interp_filter[i][dir];
}
}
ctx += if left_type == above_type {
left_type as usize
} else if left_type == 3 {
above_type as usize
} else if above_type == 3 {
left_type as usize
} else {
3
};
ctx
}
fn get_mode(&self, ref_list: usize) -> usize {
let ym = self.b.y_mode;
if ref_list == 0 {
if ym < NEAREST_NEARESTMV {
ym
} else if matches!(ym, NEW_NEWMV | NEW_NEARESTMV | NEW_NEARMV) {
NEWMV
} else if matches!(ym, NEAREST_NEARESTMV | NEAREST_NEWMV) {
NEARESTMV
} else if matches!(ym, NEAR_NEARMV | NEAR_NEWMV) {
NEARMV
} else {
GLOBALMV
}
} else if matches!(ym, NEW_NEWMV | NEAREST_NEWMV | NEAR_NEWMV) {
NEWMV
} else if matches!(ym, NEAREST_NEARESTMV | NEW_NEARESTMV) {
NEARESTMV
} else if matches!(ym, NEAR_NEARMV | NEW_NEARMV) {
NEARMV
} else {
GLOBALMV
}
}
pub(crate) fn assign_mv(&mut self, is_compound: bool) -> Result<()> {
for i in 0..1 + is_compound as usize {
let comp_mode = if self.b.use_intrabc { NEWMV } else { self.get_mode(i) };
let pred: MvP = if self.b.use_intrabc {
let mut p = self.mvs.ref_stack_mv[0][0];
if p == [0, 0] {
p = self.mvs.ref_stack_mv[1][0];
}
if p == [0, 0] {
let sb_size = if self.fs.seq.use_128x128_superblock { BLOCK_128X128 } else { BLOCK_64X64 };
let sb4 = NUM_4X4_BLOCKS_HIGH[sb_size] as i32;
if (self.b.mi_row as i32) - sb4 < self.mi_row_start as i32 {
p = [0, -(sb4 * 4 + INTRABC_DELAY_PIXELS as i32) * 8];
} else {
p = [-(sb4 * 4 * 8), 0];
}
}
p
} else if comp_mode == GLOBALMV {
self.mvs.global_mvs[i]
} else {
let mut pos = if comp_mode == NEARESTMV { 0 } else { self.b.ref_mv_idx };
if comp_mode == NEWMV && self.mvs.num_mv_found <= 1 {
pos = 0;
}
self.mvs.ref_stack_mv[pos][i]
};
self.b.pred_mv[i] = pred;
if comp_mode == NEWMV {
self.read_mv(i);
} else {
self.b.mv_i[i] = pred;
}
}
Ok(())
}
fn read_mv(&mut self, r: usize) {
let mut diff = [0i32; 2];
let ctx = if self.b.use_intrabc { MV_INTRABC_CONTEXT } else { 0 };
let joint = self.sd.read_symbol(&mut self.cdf.mv_joint[ctx]);
if joint == MV_JOINT_HZVNZ || joint == MV_JOINT_HNZVNZ {
diff[0] = self.read_mv_component(ctx, 0);
}
if joint == MV_JOINT_HNZVZ || joint == MV_JOINT_HNZVNZ {
diff[1] = self.read_mv_component(ctx, 1);
}
self.b.mv_i[r] = [self.b.pred_mv[r][0] + diff[0], self.b.pred_mv[r][1] + diff[1]];
}
fn read_mv_component(&mut self, ctx: usize, comp: usize) -> i32 {
let fh = &self.fs.fh;
let (fim, hp) = (fh.force_integer_mv, fh.allow_high_precision_mv);
let sign = self.sd.read_symbol(&mut self.cdf.mv_sign[ctx][comp]);
let class = self.sd.read_symbol(&mut self.cdf.mv_class[ctx][comp]);
let mag = if class == MV_CLASS_0 {
let b0 = self.sd.read_symbol(&mut self.cdf.mv_class0_bit[ctx][comp]) as i32;
let fr = if fim { 3 } else { self.sd.read_symbol(&mut self.cdf.mv_class0_fr[ctx][comp][b0 as usize]) as i32 };
let h = if hp { self.sd.read_symbol(&mut self.cdf.mv_class0_hp[ctx][comp]) as i32 } else { 1 };
((b0 << 3) | (fr << 1) | h) + 1
} else {
let mut d = 0i32;
for i in 0..class {
d |= (self.sd.read_symbol(&mut self.cdf.mv_bit[ctx][comp][i]) as i32) << i;
}
let mut mag = (CLASS0_SIZE as i32) << (class + 2);
let fr = if fim { 3 } else { self.sd.read_symbol(&mut self.cdf.mv_fr[ctx][comp]) as i32 };
let h = if hp { self.sd.read_symbol(&mut self.cdf.mv_hp[ctx][comp]) as i32 } else { 1 };
mag += ((d << 3) | (fr << 1) | h) + 1;
mag
};
if sign == 1 { -mag } else { mag }
}
fn read_interintra_mode(&mut self, is_compound: bool) {
let ms = self.b.mi_size;
self.b.interintra = false;
if !self.b.skip_mode && self.fs.seq.enable_interintra_compound && !is_compound && (BLOCK_8X8..=BLOCK_32X32).contains(&ms) {
let ctx = SIZE_GROUP[ms] as usize - 1;
self.b.interintra = self.sd.read_symbol(&mut self.cdf.inter_intra[ctx]) == 1;
if self.b.interintra {
self.b.interintra_mode = self.sd.read_symbol(&mut self.cdf.inter_intra_mode[ctx]);
self.b.ref_frame[1] = INTRA_FRAME as i8;
self.b.angle_delta_y = 0;
self.b.angle_delta_uv = 0;
self.b.use_filter_intra = false;
self.b.wedge_interintra = self.sd.read_symbol(&mut self.cdf.wedge_inter_intra[ms]) == 1;
if self.b.wedge_interintra {
self.b.wedge_index = self.sd.read_symbol(&mut self.cdf.wedge_index[ms]);
self.b.wedge_sign = 0;
}
}
}
}
fn is_scaled(&self, rf: i8) -> bool {
let fh = &self.fs.fh;
let ri = &self.fs.ref_info[fh.ref_frame_idx[rf as usize - LAST_FRAME]];
let fw = fh.frame_width;
let fhh = fh.frame_height;
let xs = ((ri.upscaled_width << REF_SCALE_SHIFT) + fw / 2) / fw;
let ys = ((ri.frame_height << REF_SCALE_SHIFT) + fhh / 2) / fhh;
let no = 1 << REF_SCALE_SHIFT;
xs != no || ys != no
}
fn read_motion_mode(&mut self, is_compound: bool) {
self.b.motion_mode = SIMPLE as u8;
let fh = &self.fs.fh;
if self.b.skip_mode || !fh.is_motion_mode_switchable {
return;
}
let ms = self.b.mi_size;
if block_width(ms).min(block_height(ms)) < 8 {
return;
}
if !fh.force_integer_mv
&& (self.b.y_mode == GLOBALMV || self.b.y_mode == GLOBAL_GLOBALMV)
&& fh.gm_type[self.b.ref_frame[0] as usize] as usize > TRANSLATION
{
return;
}
if is_compound || self.b.ref_frame[1] == INTRA_FRAME as i8 || !self.has_overlappable_candidates() {
return;
}
self.find_warp_samples();
let fh = &self.fs.fh;
if fh.force_integer_mv || self.mvs.num_samples == 0 || !fh.allow_warped_motion || self.is_scaled(self.b.ref_frame[0]) {
let use_obmc = self.sd.read_symbol(&mut self.cdf.use_obmc[ms]);
self.b.motion_mode = if use_obmc == 1 { OBMC as u8 } else { SIMPLE as u8 };
} else {
self.b.motion_mode = self.sd.read_symbol(&mut self.cdf.motion_mode[ms]) as u8;
}
}
fn read_compound_type(&mut self, is_compound: bool) {
self.b.comp_group_idx = 0;
self.b.compound_idx = 1;
if self.b.skip_mode {
self.b.compound_type = COMPOUND_AVERAGE;
return;
}
if is_compound {
let ms = self.b.mi_size;
let n = WEDGE_BITS[ms];
if self.fs.seq.enable_masked_compound {
let ctx = self.comp_group_idx_ctx();
self.b.comp_group_idx = self.sd.read_symbol(&mut self.cdf.comp_group_idx[ctx]) as u8;
}
if self.b.comp_group_idx == 0 {
if self.fs.seq.enable_jnt_comp {
let ctx = self.compound_idx_ctx();
self.b.compound_idx = self.sd.read_symbol(&mut self.cdf.compound_idx[ctx]) as u8;
self.b.compound_type = if self.b.compound_idx == 1 { COMPOUND_AVERAGE } else { COMPOUND_DISTANCE };
} else {
self.b.compound_type = COMPOUND_AVERAGE;
}
} else if n == 0 {
self.b.compound_type = COMPOUND_DIFFWTD;
} else {
self.b.compound_type = self.sd.read_symbol(&mut self.cdf.compound_type[ms]);
}
if self.b.compound_type == COMPOUND_WEDGE {
self.b.wedge_index = self.sd.read_symbol(&mut self.cdf.wedge_index[ms]);
self.b.wedge_sign = self.sd.read_literal(1) as usize;
} else if self.b.compound_type == COMPOUND_DIFFWTD {
self.b.mask_type = self.sd.read_literal(1) as usize;
}
} else if self.b.interintra {
self.b.compound_type = if self.b.wedge_interintra { COMPOUND_WEDGE } else { COMPOUND_INTRA };
} else {
self.b.compound_type = COMPOUND_AVERAGE;
}
}
fn comp_group_idx_ctx(&self) -> usize {
let n = self.nb;
let mi = &self.t.mi;
let (r, c) = (self.b.mi_row, self.b.mi_col);
let mut ctx = 0;
if self.b.avail_u {
if !n.above_single {
ctx += mi.comp_group_idx[mi.idx(r - 1, c)] as usize;
} else if n.above[0] == ALTREF_FRAME as i8 {
ctx += 3;
}
}
if self.b.avail_l {
if !n.left_single {
ctx += mi.comp_group_idx[mi.idx(r, c - 1)] as usize;
} else if n.left[0] == ALTREF_FRAME as i8 {
ctx += 3;
}
}
ctx.min(5)
}
fn compound_idx_ctx(&self) -> usize {
let fh = &self.fs.fh;
let seq = &self.fs.seq;
let fwd = crate::header::relative_dist(seq, fh.order_hints[self.b.ref_frame[0] as usize], fh.order_hint).abs();
let bck = crate::header::relative_dist(seq, fh.order_hints[self.b.ref_frame[1] as usize], fh.order_hint).abs();
let mut ctx = if fwd == bck { 3 } else { 0 };
let n = self.nb;
let mi = &self.t.mi;
let (r, c) = (self.b.mi_row, self.b.mi_col);
if self.b.avail_u {
if !n.above_single {
ctx += mi.compound_idx[mi.idx(r - 1, c)] as usize;
} else if n.above[0] == ALTREF_FRAME as i8 {
ctx += 1;
}
}
if self.b.avail_l {
if !n.left_single {
ctx += mi.compound_idx[mi.idx(r, c - 1)] as usize;
} else if n.left[0] == ALTREF_FRAME as i8 {
ctx += 1;
}
}
ctx
}
}
-407
View File
@@ -1,407 +0,0 @@
//! Intra prediction (spec 7.11.2), palette prediction (7.11.4) and chroma from luma (7.11.5).
use crate::frame::Plane;
use crate::spec_tables::{
ANGLE_STEP, D67_PRED, DC_PRED, DR_INTRA_DERIVATIVE, INTRA_EDGE_KERNEL, INTRA_EDGE_TAPS, INTRA_FILTER_SCALE_BITS, INTRA_FILTER_TAPS,
MODE_TO_ANGLE, SM_WEIGHTS_TX_4X4, SM_WEIGHTS_TX_8X8, SM_WEIGHTS_TX_16X16, SM_WEIGHTS_TX_32X32, SM_WEIGHTS_TX_64X64, SMOOTH_H_PRED, SMOOTH_PRED,
SMOOTH_V_PRED, V_PRED,
};
use crate::transform::round2;
/// Inputs of the intra prediction process that come from the block.
pub(crate) struct IntraParams {
pub plane: usize,
pub x: usize,
pub y: usize,
pub have_left: bool,
pub have_above: bool,
pub have_above_right: bool,
pub have_below_left: bool,
pub mode: usize,
pub log2w: u32,
pub log2h: u32,
/// Largest valid x / y in the plane: ((MiCols * MI_SIZE) >> subX) - 1.
pub max_x: i32,
pub max_y: i32,
pub bit_depth: u32,
pub angle_delta: i32,
pub use_filter_intra: bool,
pub filter_intra_mode: usize,
pub enable_intra_edge_filter: bool,
/// Result of the intra filter type process (7.11.2.8).
pub filter_type: bool,
}
const EDGE: usize = 32; // offset of index 0 in the edge buffers (they start at index -2 .. -1)
#[inline(always)]
fn clip1(v: i32, bd: u32) -> u16 {
v.clamp(0, (1 << bd) - 1) as u16
}
pub(crate) fn is_directional_mode(mode: usize) -> bool {
(V_PRED..=D67_PRED).contains(&mode)
}
/// Predict the w x h block at (x, y) of `pl` (writes into the plane).
pub(crate) fn predict_intra(pl: &mut Plane, p: &IntraParams) {
let w = 1usize << p.log2w;
let h = 1usize << p.log2h;
let bd = p.bit_depth;
let (x, y) = (p.x, p.y);
// edge arrays with room for index -2 .. 2*(w+h)
let mut above = [0i32; 2 * 128 + 2 * EDGE];
let mut left = [0i32; 2 * 128 + 2 * EDGE];
let n = w + h;
if !p.have_above && p.have_left {
let v = pl.at(x - 1, y) as i32;
above[EDGE..EDGE + n].iter_mut().for_each(|a| *a = v);
} else if !p.have_above && !p.have_left {
let v = (1 << (bd - 1)) - 1;
above[EDGE..EDGE + n].iter_mut().for_each(|a| *a = v);
} else {
let above_limit = (p.max_x).min(x as i32 + if p.have_above_right { 2 * w as i32 } else { w as i32 } - 1);
let row = pl.row_from(y - 1, x);
for i in 0..n {
above[EDGE + i] = row[(above_limit.min(x as i32 + i as i32)) as usize - x] as i32;
}
}
if !p.have_left && p.have_above {
let v = pl.at(x, y - 1) as i32;
left[EDGE..EDGE + n].iter_mut().for_each(|a| *a = v);
} else if !p.have_left && !p.have_above {
let v = (1 << (bd - 1)) + 1;
left[EDGE..EDGE + n].iter_mut().for_each(|a| *a = v);
} else {
let left_limit = (p.max_y).min(y as i32 + if p.have_below_left { 2 * h as i32 } else { h as i32 } - 1);
for i in 0..n {
left[EDGE + i] = pl.at(x - 1, left_limit.min(y as i32 + i as i32) as usize) as i32;
}
}
let corner = if p.have_above && p.have_left {
pl.at(x - 1, y - 1) as i32
} else if p.have_above {
pl.at(x, y - 1) as i32
} else if p.have_left {
pl.at(x - 1, y) as i32
} else {
1 << (bd - 1)
};
above[EDGE - 1] = corner;
left[EDGE - 1] = corner;
let mut pred = [0u16; 64 * 64];
if p.plane == 0 && p.use_filter_intra {
recursive_intra(&above, &left, w, h, p.filter_intra_mode, bd, &mut pred);
} else if is_directional_mode(p.mode) {
directional(&mut above, &mut left, p, w, h, &mut pred);
} else if p.mode == SMOOTH_PRED || p.mode == SMOOTH_V_PRED || p.mode == SMOOTH_H_PRED {
smooth(&above, &left, p.mode, p.log2w, p.log2h, w, h, &mut pred);
} else if p.mode == DC_PRED {
dc(&above, &left, p.have_left, p.have_above, p.log2w, p.log2h, w, h, bd, &mut pred);
} else {
// PAETH_PRED
let tl = above[EDGE - 1];
for i in 0..h {
for j in 0..w {
let a = above[EDGE + j];
let l = left[EDGE + i];
let base = a + l - tl;
let p_left = (base - l).abs();
let p_top = (base - a).abs();
let p_tl = (base - tl).abs();
pred[i * w + j] = if p_left <= p_top && p_left <= p_tl {
l as u16
} else if p_top <= p_tl {
a as u16
} else {
tl as u16
};
}
}
}
for i in 0..h {
pl.row_from_mut(y + i, x)[..w].copy_from_slice(&pred[i * w..i * w + w]);
}
}
fn recursive_intra(above: &[i32], left: &[i32], w: usize, h: usize, mode: usize, bd: u32, pred: &mut [u16]) {
let w4 = w >> 2;
let h2 = h >> 1;
for i2 in 0..h2 {
for j4 in 0..w4 {
let mut pv = [0i32; 7];
for (i, v) in pv.iter_mut().enumerate() {
*v = if i < 5 {
if i2 == 0 {
above[EDGE + (j4 << 2) + i - 1]
} else if j4 == 0 && i == 0 {
left[EDGE + (i2 << 1) - 1]
} else {
pred[((i2 << 1) - 1) * w + (j4 << 2) + i - 1] as i32
}
} else if j4 == 0 {
left[EDGE + (i2 << 1) + i - 5]
} else {
pred[((i2 << 1) + i - 5) * w + (j4 << 2) - 1] as i32
};
}
for i1 in 0..2 {
for j1 in 0..4 {
let mut pr = 0i32;
for i in 0..7 {
pr += INTRA_FILTER_TAPS[mode][(i1 << 2) + j1][i] as i32 * pv[i];
}
pred[((i2 << 1) + i1) * w + (j4 << 2) + j1] = clip1(round2_signed(pr, INTRA_FILTER_SCALE_BITS as u32), bd);
}
}
}
}
}
#[inline(always)]
pub(crate) fn round2_signed(x: i32, n: u32) -> i32 {
if x >= 0 { round2(x, n) } else { -round2(-x, n) }
}
fn sm_weights(log2: u32) -> &'static [u8] {
match log2 {
2 => &SM_WEIGHTS_TX_4X4,
3 => &SM_WEIGHTS_TX_8X8,
4 => &SM_WEIGHTS_TX_16X16,
5 => &SM_WEIGHTS_TX_32X32,
_ => &SM_WEIGHTS_TX_64X64,
}
}
#[allow(clippy::too_many_arguments)]
fn smooth(above: &[i32], left: &[i32], mode: usize, log2w: u32, log2h: u32, w: usize, h: usize, pred: &mut [u16]) {
let wx = sm_weights(log2w);
let wy = sm_weights(log2h);
for i in 0..h {
for j in 0..w {
let v = if mode == SMOOTH_PRED {
let s = wy[i] as i32 * above[EDGE + j]
+ (256 - wy[i] as i32) * left[EDGE + h - 1]
+ wx[j] as i32 * left[EDGE + i]
+ (256 - wx[j] as i32) * above[EDGE + w - 1];
round2(s, 9)
} else if mode == SMOOTH_V_PRED {
round2(wy[i] as i32 * above[EDGE + j] + (256 - wy[i] as i32) * left[EDGE + h - 1], 8)
} else {
round2(wx[j] as i32 * left[EDGE + i] + (256 - wx[j] as i32) * above[EDGE + w - 1], 8)
};
pred[i * w + j] = v as u16;
}
}
}
#[allow(clippy::too_many_arguments)]
fn dc(above: &[i32], left: &[i32], have_left: bool, have_above: bool, log2w: u32, log2h: u32, w: usize, h: usize, bd: u32, pred: &mut [u16]) {
let v = if have_left && have_above {
let mut sum: i32 = left[EDGE..EDGE + h].iter().sum::<i32>() + above[EDGE..EDGE + w].iter().sum::<i32>();
sum += ((w + h) >> 1) as i32;
sum / (w + h) as i32
} else if have_left {
let sum: i32 = left[EDGE..EDGE + h].iter().sum();
(sum + (h >> 1) as i32) >> log2h
} else if have_above {
let sum: i32 = above[EDGE..EDGE + w].iter().sum();
(sum + (w >> 1) as i32) >> log2w
} else {
1 << (bd - 1)
};
let v = clip1(v, bd);
pred[..w * h].iter_mut().for_each(|p| *p = v);
}
fn edge_filter_strength(w: usize, h: usize, filter_type: bool, delta: i32) -> u32 {
let d = delta.abs();
let blk_wh = w + h;
let mut strength = 0;
if !filter_type {
if blk_wh <= 8 {
if d >= 56 {
strength = 1;
}
} else if blk_wh <= 16 {
if d >= 40 {
strength = 1;
}
} else if blk_wh <= 24 {
if d >= 8 {
strength = 1;
}
if d >= 16 {
strength = 2;
}
if d >= 32 {
strength = 3;
}
} else if blk_wh <= 32 {
strength = 1;
if d >= 4 {
strength = 2;
}
if d >= 32 {
strength = 3;
}
} else {
strength = 3;
}
} else if blk_wh <= 8 {
if d >= 40 {
strength = 1;
}
if d >= 64 {
strength = 2;
}
} else if blk_wh <= 16 {
if d >= 20 {
strength = 1;
}
if d >= 48 {
strength = 2;
}
} else if blk_wh <= 24 {
if d >= 4 {
strength = 3;
}
} else {
strength = 3;
}
strength
}
fn use_upsample(w: usize, h: usize, filter_type: bool, delta: i32) -> bool {
let d = delta.abs();
let blk_wh = w + h;
if d <= 0 || d >= 40 {
false
} else if !filter_type {
blk_wh <= 16
} else {
blk_wh <= 8
}
}
/// Intra edge filter (7.11.2.12) on buf[-1 .. sz-2] (edge[i] = buf[i - 1]).
fn edge_filter(buf: &mut [i32], sz: usize, strength: u32) {
if strength == 0 {
return;
}
let mut edge = [0i32; 2 * 128 + 2 * EDGE];
for i in 0..sz {
edge[i] = buf[EDGE + i - 1];
}
for i in 1..sz {
let mut s = 0;
for j in 0..INTRA_EDGE_TAPS {
let k = (i as i32 - 2 + j as i32).clamp(0, sz as i32 - 1) as usize;
s += INTRA_EDGE_KERNEL[strength as usize - 1][j] as i32 * edge[k];
}
buf[EDGE + i - 1] = (s + 8) >> 4;
}
}
/// Intra edge upsample (7.11.2.11).
fn edge_upsample(buf: &mut [i32], num_px: usize, bd: u32) {
let mut dup = [0i32; 2 * 128 + 8];
dup[0] = buf[EDGE - 1];
for i in -1..num_px as i32 {
dup[(i + 2) as usize] = buf[(EDGE as i32 + i) as usize];
}
dup[num_px + 2] = buf[EDGE + num_px - 1];
buf[EDGE - 2] = dup[0];
for i in 0..num_px {
let s = -dup[i] + 9 * dup[i + 1] + 9 * dup[i + 2] - dup[i + 3];
let s = clip1(round2(s, 4), bd) as i32;
buf[EDGE + 2 * i - 1] = s;
buf[EDGE + 2 * i] = dup[i + 2];
}
}
fn directional(above: &mut [i32], left: &mut [i32], p: &IntraParams, w: usize, h: usize, pred: &mut [u16]) {
let p_angle = MODE_TO_ANGLE[p.mode] as i32 + p.angle_delta * ANGLE_STEP as i32;
let mut upsample_above = 0u32;
let mut upsample_left = 0u32;
if p.enable_intra_edge_filter {
if p_angle != 90 && p_angle != 180 {
if p_angle > 90 && p_angle < 180 && (w + h) >= 24 {
let s = left[EDGE] * 5 + above[EDGE - 1] * 6 + above[EDGE] * 5;
let c = round2(s, 4);
left[EDGE - 1] = c;
above[EDGE - 1] = c;
}
if p.have_above {
let strength = edge_filter_strength(w, h, p.filter_type, p_angle - 90);
let num_px = w.min((p.max_x - p.x as i32 + 1) as usize) + if p_angle < 90 { h } else { 0 } + 1;
edge_filter(above, num_px, strength);
}
if p.have_left {
let strength = edge_filter_strength(w, h, p.filter_type, p_angle - 180);
let num_px = h.min((p.max_y - p.y as i32 + 1) as usize) + if p_angle > 180 { w } else { 0 } + 1;
edge_filter(left, num_px, strength);
}
}
if use_upsample(w, h, p.filter_type, p_angle - 90) {
upsample_above = 1;
let num_px = w + if p_angle < 90 { h } else { 0 };
edge_upsample(above, num_px, p.bit_depth);
}
if use_upsample(w, h, p.filter_type, p_angle - 180) {
upsample_left = 1;
let num_px = h + if p_angle > 180 { w } else { 0 };
edge_upsample(left, num_px, p.bit_depth);
}
}
let dx = if p_angle < 90 {
DR_INTRA_DERIVATIVE[p_angle as usize] as i32
} else if p_angle > 90 && p_angle < 180 {
DR_INTRA_DERIVATIVE[(180 - p_angle) as usize] as i32
} else {
0
};
let dy = if p_angle > 90 && p_angle < 180 {
DR_INTRA_DERIVATIVE[(p_angle - 90) as usize] as i32
} else if p_angle > 180 {
DR_INTRA_DERIVATIVE[(270 - p_angle) as usize] as i32
} else {
0
};
let a = |i: i32| above[(EDGE as i32 + i) as usize];
let l = |i: i32| left[(EDGE as i32 + i) as usize];
for i in 0..h as i32 {
for j in 0..w as i32 {
let v = if p_angle < 90 {
let idx = (i + 1) * dx;
let base = (idx >> (6 - upsample_above)) + (j << upsample_above);
let shift = ((idx << upsample_above) >> 1) & 0x1f;
let max_base_x = (w as i32 + h as i32 - 1) << upsample_above;
if base < max_base_x { round2(a(base) * (32 - shift) + a(base + 1) * shift, 5) } else { a(max_base_x) }
} else if p_angle > 90 && p_angle < 180 {
let idx = (j << 6) - (i + 1) * dx;
let base = idx >> (6 - upsample_above);
if base >= -(1 << upsample_above) {
let shift = ((idx << upsample_above) >> 1) & 0x1f;
round2(a(base) * (32 - shift) + a(base + 1) * shift, 5)
} else {
let idx = (i << 6) - (j + 1) * dy;
let base = idx >> (6 - upsample_left);
let shift = ((idx << upsample_left) >> 1) & 0x1f;
round2(l(base) * (32 - shift) + l(base + 1) * shift, 5)
}
} else if p_angle > 180 {
let idx = (j + 1) * dy;
let base = (idx >> (6 - upsample_left)) + (i << upsample_left);
let shift = ((idx << upsample_left) >> 1) & 0x1f;
round2(l(base) * (32 - shift) + l(base + 1) * shift, 5)
} else if p_angle == 90 {
a(j)
} else {
l(i)
};
pred[(i as usize) * w + j as usize] = v as u16;
}
}
}
-123
View File
@@ -1,123 +0,0 @@
//! Clean-room AV1 decoder, implemented from the AV1 Bitstream & Decoding Process Specification
//! v1.0.0 with Errata 1 (AOMediaCodec/av1-spec).
//!
//! See the crate README for the conformance status of each decoding stage.
// Items used only by stages still being written (inter prediction, post filters).
// Spec-style indexed loops (FilmCraft allows this lint workspace-wide).
#![allow(clippy::needless_range_loop)]
#![allow(dead_code)]
#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))]
mod bits;
mod cdef;
mod cdf;
mod decoder;
mod frame;
mod grain;
pub mod header;
mod inter;
mod inter_info;
mod intra;
mod mvpred;
mod par;
mod postfilter;
mod restoration;
mod spec_tables;
mod state;
mod stats;
mod symbol;
mod tile;
mod transform;
pub use decoder::Decoder;
pub use header::{ColorConfig, FrameHeader, SequenceHeader};
pub use stats::{DecodeStats, Stage};
/// Decoder errors.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum Error {
#[error("truncated AV1 data")]
Truncated,
#[error("invalid AV1 data: {0}")]
Invalid(&'static str),
#[error("unsupported AV1 feature: {0}")]
Unsupported(&'static str),
}
pub type Result<T> = std::result::Result<T, Error>;
/// A decoded (shown) frame: planar samples without row padding, `bit_depth` significant bits in
/// `u16`. Chroma planes are `((width + ssx) >> ssx) x ((height + ssy) >> ssy)`; they are empty
/// for monochrome streams.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Picture {
pub width: u32,
pub height: u32,
pub bit_depth: u8,
pub subsampling_x: u8,
pub subsampling_y: u8,
pub mono_chrome: bool,
pub planes: [Vec<u16>; 3],
pub color_primaries: u8,
pub transfer_characteristics: u8,
pub matrix_coefficients: u8,
pub full_range: bool,
/// Timestamp of the temporal unit that showed the picture ([`Decoder::decode_pts`]).
pub pts: i64,
/// Decoded in draft mode without deblocking / CDEF / loop restoration
/// ([`Decoder::set_draft`]): approximate, for reduced-resolution playback only.
pub draft: bool,
}
impl Picture {
pub fn plane_width(&self, plane: usize) -> u32 {
if plane == 0 { self.width } else { (self.width + self.subsampling_x as u32) >> self.subsampling_x }
}
pub fn plane_height(&self, plane: usize) -> u32 {
if plane == 0 { self.height } else { (self.height + self.subsampling_y as u32) >> self.subsampling_y }
}
}
/// Whether a temporal unit starts a random access point: its first frame is a shown key frame
/// (used for keyframe detection: containers may flag samples as sync that are not).
pub fn is_key_frame_unit(data: &[u8]) -> bool {
let mut pos = 0;
let mut seq_seen = false;
while pos < data.len() {
let h = data[pos];
let obu_type = (h >> 3) & 0xf;
let ext = h & 4 != 0;
let has_size = h & 2 != 0;
let mut p = pos + 1 + ext as usize;
let size = if has_size {
match bits::leb128(data.get(p..).unwrap_or(&[])) {
Ok((v, n)) => {
p += n;
v as usize
}
Err(_) => return false,
}
} else {
data.len().saturating_sub(p)
};
if p > data.len() {
return false;
}
match obu_type {
1 => seq_seen = true,
3 | 6 => {
let b = data.get(p).copied().unwrap_or(0);
// show_existing_frame = 0, frame_type = KEY_FRAME (0), show_frame = 1
let show_existing = b >> 7;
let frame_type = (b >> 5) & 3;
let show_frame = (b >> 4) & 1;
let _ = seq_seen;
return show_existing == 0 && frame_type == 0 && show_frame == 1;
}
_ => {}
}
pos = p.saturating_add(size);
}
false
}
-737
View File
@@ -1,737 +0,0 @@
//! Motion vector prediction processes (spec 7.10): find_mv_stack, has_overlappable_candidates
//! and find_warp_samples.
use crate::spec_tables::{
GLOBAL_GLOBALMV, GLOBALMV, IDENTITY, INTRA_FRAME, LEAST_SQUARES_SAMPLES_MAX, MAX_REF_MV_STACK_SIZE, MV_BORDER, NEAR_NEWMV, NEAREST_NEWMV,
NEW_NEARESTMV, NEW_NEARMV, NEW_NEWMV, NEWMV, NUM_4X4_BLOCKS_HIGH, NUM_4X4_BLOCKS_WIDE, REF_CAT_LEVEL, TRANSLATION, WARPEDMODEL_PREC_BITS,
};
use crate::tile::TileDecoder;
pub(crate) type MvP = [i32; 2];
pub(crate) const INVALID_MV: i32 = -1 << 15;
/// Output of find_mv_stack plus its working state.
#[derive(Clone, Default)]
pub(crate) struct MvStack {
pub ref_stack_mv: [[MvP; 2]; MAX_REF_MV_STACK_SIZE + 1],
pub weight_stack: [u32; MAX_REF_MV_STACK_SIZE + 1],
pub num_mv_found: usize,
pub new_mv_count: usize,
pub global_mvs: [MvP; 2],
pub found_match: bool,
pub close_matches: usize,
pub total_matches: usize,
pub new_mv_context: usize,
pub ref_mv_context: usize,
pub zero_mv_context: usize,
pub drl_ctx_stack: [usize; MAX_REF_MV_STACK_SIZE + 1],
ref_id_count: [usize; 2],
ref_diff_count: [usize; 2],
ref_id_mvs: [[MvP; 2]; 2],
ref_diff_mvs: [[MvP; 2]; 2],
// warp samples
pub num_samples: usize,
pub num_samples_scanned: usize,
pub cand_list: [[i32; 4]; LEAST_SQUARES_SAMPLES_MAX],
}
fn block_width(bs: usize) -> i32 {
4 * NUM_4X4_BLOCKS_WIDE[bs] as i32
}
fn block_height(bs: usize) -> i32 {
4 * NUM_4X4_BLOCKS_HIGH[bs] as i32
}
#[inline]
pub(crate) fn round2_signed64(x: i64, n: u32) -> i64 {
if n == 0 {
return x;
}
if x >= 0 { (x + (1 << (n - 1))) >> n } else { -((-x + (1 << (n - 1))) >> n) }
}
pub(crate) fn has_newmv(mode: usize) -> bool {
matches!(mode, NEWMV | NEW_NEWMV | NEAR_NEWMV | NEW_NEARMV | NEAREST_NEWMV | NEW_NEARESTMV)
}
impl TileDecoder<'_, '_> {
/// lower_mv_precision( candMv ) (7.10.2.10)
pub(crate) fn lower_mv_precision(&self, mv: &mut MvP) {
let fh = &self.fs.fh;
if fh.allow_high_precision_mv {
return;
}
for c in mv.iter_mut() {
if fh.force_integer_mv {
let a = c.abs();
let a_int = (a + 3) >> 3;
*c = if *c > 0 { a_int << 3 } else { -(a_int << 3) };
} else if *c & 1 != 0 {
*c += if *c > 0 { -1 } else { 1 };
}
}
}
/// Setup global MV process (7.10.2.1).
pub(crate) fn setup_global_mv(&self, ref_list: usize) -> MvP {
let rf = self.b.ref_frame[ref_list];
let fh = &self.fs.fh;
let mut mv: MvP = [0, 0];
if rf > INTRA_FRAME as i8 {
let rf = rf as usize;
let typ = fh.gm_type[rf] as usize;
let gm = &fh.gm_params[rf];
if typ == TRANSLATION {
mv = [gm[0] >> (WARPEDMODEL_PREC_BITS - 3), gm[1] >> (WARPEDMODEL_PREC_BITS - 3)];
} else if typ != IDENTITY {
let bw = block_width(self.b.mi_size) as i64;
let bh = block_height(self.b.mi_size) as i64;
let x = self.b.mi_col as i64 * 4 + bw / 2 - 1;
let y = self.b.mi_row as i64 * 4 + bh / 2 - 1;
let p = 1i64 << WARPEDMODEL_PREC_BITS;
let xc = (gm[2] as i64 - p) * x + gm[3] as i64 * y + gm[0] as i64;
let yc = gm[4] as i64 * x + (gm[5] as i64 - p) * y + gm[1] as i64;
if fh.allow_high_precision_mv {
mv = [round2_signed64(yc, WARPEDMODEL_PREC_BITS as u32 - 3) as i32, round2_signed64(xc, WARPEDMODEL_PREC_BITS as u32 - 3) as i32];
} else {
mv = [
round2_signed64(yc, WARPEDMODEL_PREC_BITS as u32 - 2) as i32 * 2,
round2_signed64(xc, WARPEDMODEL_PREC_BITS as u32 - 2) as i32 * 2,
];
}
}
}
self.lower_mv_precision(&mut mv);
mv
}
/// Find MV stack process (7.10.2).
pub(crate) fn find_mv_stack(&mut self, is_compound: bool) {
let bw4 = self.b.bw4 as isize;
let bh4 = self.b.bh4 as isize;
let mut s = MvStack { global_mvs: [self.setup_global_mv(0), [0, 0]], ..Default::default() };
if is_compound {
s.global_mvs[1] = self.setup_global_mv(1);
}
self.mvs = s;
self.mvs.found_match = false;
self.scan_row(-1, is_compound);
let mut found_above = self.mvs.found_match;
self.mvs.found_match = false;
self.scan_col(-1, is_compound);
let mut found_left = self.mvs.found_match;
self.mvs.found_match = false;
if bw4.max(bh4) <= 16 {
self.scan_point(-1, bw4, is_compound);
}
if self.mvs.found_match {
found_above = true;
}
self.mvs.close_matches = found_above as usize + found_left as usize;
let num_nearest = self.mvs.num_mv_found;
let num_new = self.mvs.new_mv_count;
for idx in 0..num_nearest {
self.mvs.weight_stack[idx] += REF_CAT_LEVEL as u32;
}
self.mvs.zero_mv_context = 0;
if self.fs.fh.use_ref_frame_mvs {
self.temporal_scan(is_compound);
}
self.scan_point(-1, -1, is_compound);
if self.mvs.found_match {
found_above = true;
}
self.mvs.found_match = false;
self.scan_row(-3, is_compound);
if self.mvs.found_match {
found_above = true;
}
self.mvs.found_match = false;
self.scan_col(-3, is_compound);
if self.mvs.found_match {
found_left = true;
}
self.mvs.found_match = false;
if bh4 > 1 {
self.scan_row(-5, is_compound);
}
if self.mvs.found_match {
found_above = true;
}
self.mvs.found_match = false;
if bw4 > 1 {
self.scan_col(-5, is_compound);
}
if self.mvs.found_match {
found_left = true;
}
self.mvs.total_matches = found_above as usize + found_left as usize;
let n = self.mvs.num_mv_found;
self.sort_stack(0, num_nearest, is_compound);
self.sort_stack(num_nearest, n, is_compound);
if self.mvs.num_mv_found < 2 {
self.extra_search(is_compound);
}
self.context_and_clamping(is_compound, num_new);
}
fn scan_row(&mut self, delta_row: isize, is_compound: bool) {
let bw4 = self.b.bw4 as isize;
let (mi_row, mi_col) = (self.b.mi_row as isize, self.b.mi_col as isize);
let mi_cols = self.fs.fh.mi_cols as isize;
let end4 = bw4.min(mi_cols - mi_col).min(16);
let mut delta_col = 0;
let use_step16 = bw4 >= 16;
let mut delta_row = delta_row;
if delta_row.abs() > 1 {
delta_row += mi_row & 1;
delta_col = 1 - (mi_col & 1);
}
let mut i = 0;
while i < end4 {
let mv_row = mi_row + delta_row;
let mv_col = mi_col + delta_col + i;
if !self.inside(mv_row, mv_col) {
break;
}
let mi = &self.t.mi;
let mut len = bw4.min(NUM_4X4_BLOCKS_WIDE[mi.mi_size[mi.idx(mv_row as usize, mv_col as usize)] as usize] as isize);
if delta_row.abs() > 1 {
len = len.max(2);
}
if use_step16 {
len = len.max(4);
}
let weight = len as u32 * 2;
self.add_ref_mv_candidate(mv_row as usize, mv_col as usize, is_compound, weight);
i += len;
}
}
fn scan_col(&mut self, delta_col: isize, is_compound: bool) {
let bh4 = self.b.bh4 as isize;
let (mi_row, mi_col) = (self.b.mi_row as isize, self.b.mi_col as isize);
let mi_rows = self.fs.fh.mi_rows as isize;
let end4 = bh4.min(mi_rows - mi_row).min(16);
let mut delta_row = 0;
let use_step16 = bh4 >= 16;
let mut delta_col = delta_col;
if delta_col.abs() > 1 {
delta_row = 1 - (mi_row & 1);
delta_col += mi_col & 1;
}
let mut i = 0;
while i < end4 {
let mv_row = mi_row + delta_row + i;
let mv_col = mi_col + delta_col;
if !self.inside(mv_row, mv_col) {
break;
}
let mi = &self.t.mi;
let mut len = bh4.min(NUM_4X4_BLOCKS_HIGH[mi.mi_size[mi.idx(mv_row as usize, mv_col as usize)] as usize] as isize);
if delta_col.abs() > 1 {
len = len.max(2);
}
if use_step16 {
len = len.max(4);
}
let weight = len as u32 * 2;
self.add_ref_mv_candidate(mv_row as usize, mv_col as usize, is_compound, weight);
i += len;
}
}
fn scan_point(&mut self, delta_row: isize, delta_col: isize, is_compound: bool) {
let mv_row = self.b.mi_row as isize + delta_row;
let mv_col = self.b.mi_col as isize + delta_col;
if self.inside(mv_row, mv_col) && self.t.mi.written[self.t.mi.idx(mv_row as usize, mv_col as usize)] {
self.add_ref_mv_candidate(mv_row as usize, mv_col as usize, is_compound, 4);
}
}
fn temporal_scan(&mut self, is_compound: bool) {
let bw4 = self.b.bw4 as isize;
let bh4 = self.b.bh4 as isize;
let step_w4 = if bw4 >= 16 { 4 } else { 2 };
let step_h4 = if bh4 >= 16 { 4 } else { 2 };
let mut dr = 0;
while dr < bh4.min(16) {
let mut dc = 0;
while dc < bw4.min(16) {
self.add_tpl_ref_mv(dr, dc, is_compound);
dc += step_w4;
}
dr += step_h4;
}
let allow_extension = (2..16).contains(&bh4) && (2..16).contains(&bw4);
if allow_extension {
let pos = [(bh4, -2), (bh4, bw4), (bh4 - 2, bw4)];
for &(dr, dc) in &pos {
let row = (self.b.mi_row as isize & 15) + dr;
let col = (self.b.mi_col as isize & 15) + dc;
if (0..16).contains(&row) && (0..16).contains(&col) {
self.add_tpl_ref_mv(dr, dc, is_compound);
}
}
}
}
fn add_tpl_ref_mv(&mut self, delta_row: isize, delta_col: isize, is_compound: bool) {
let mv_row = (self.b.mi_row as isize + delta_row) | 1;
let mv_col = (self.b.mi_col as isize + delta_col) | 1;
if !self.inside(mv_row, mv_col) {
return;
}
let x8 = (mv_col >> 1) as usize;
let y8 = (mv_row >> 1) as usize;
let w8 = self.fs.fh.mi_cols as usize >> 1;
if delta_row == 0 && delta_col == 0 {
self.mvs.zero_mv_context = 1;
}
let rf0 = self.b.ref_frame[0] as usize;
if !is_compound {
let mut cand = self.fs.motion_field[rf0][y8 * w8 + x8];
if cand[0] == INVALID_MV {
return;
}
self.lower_mv_precision(&mut cand);
if delta_row == 0 && delta_col == 0 {
let g = self.mvs.global_mvs[0];
self.mvs.zero_mv_context = ((cand[0] - g[0]).abs() >= 16 || (cand[1] - g[1]).abs() >= 16) as usize;
}
let n = self.mvs.num_mv_found;
let idx = (0..n).find(|&i| self.mvs.ref_stack_mv[i][0] == cand);
if let Some(i) = idx {
self.mvs.weight_stack[i] += 2;
} else if n < MAX_REF_MV_STACK_SIZE {
self.mvs.ref_stack_mv[n][0] = cand;
self.mvs.weight_stack[n] = 2;
self.mvs.num_mv_found += 1;
}
} else {
let rf1 = self.b.ref_frame[1] as usize;
let mut c0 = self.fs.motion_field[rf0][y8 * w8 + x8];
if c0[0] == INVALID_MV {
return;
}
let mut c1 = self.fs.motion_field[rf1][y8 * w8 + x8];
if c1[0] == INVALID_MV {
return;
}
self.lower_mv_precision(&mut c0);
self.lower_mv_precision(&mut c1);
if delta_row == 0 && delta_col == 0 {
let g = self.mvs.global_mvs;
self.mvs.zero_mv_context = ((c0[0] - g[0][0]).abs() >= 16
|| (c0[1] - g[0][1]).abs() >= 16
|| (c1[0] - g[1][0]).abs() >= 16
|| (c1[1] - g[1][1]).abs() >= 16) as usize;
}
let n = self.mvs.num_mv_found;
let idx = (0..n).find(|&i| self.mvs.ref_stack_mv[i][0] == c0 && self.mvs.ref_stack_mv[i][1] == c1);
if let Some(i) = idx {
self.mvs.weight_stack[i] += 2;
} else if n < MAX_REF_MV_STACK_SIZE {
self.mvs.ref_stack_mv[n] = [c0, c1];
self.mvs.weight_stack[n] = 2;
self.mvs.num_mv_found += 1;
}
}
}
fn add_ref_mv_candidate(&mut self, mv_row: usize, mv_col: usize, is_compound: bool, weight: u32) {
let mi = &self.t.mi;
let i = mi.idx(mv_row, mv_col);
if !mi.is_inter[i] {
return;
}
let rf = mi.ref_frame[i];
if !is_compound {
for cand_list in 0..2 {
if rf[cand_list] == self.b.ref_frame[0] {
self.search_stack(mv_row, mv_col, cand_list, weight);
}
}
} else if rf[0] == self.b.ref_frame[0] && rf[1] == self.b.ref_frame[1] {
self.compound_search_stack(mv_row, mv_col, weight);
}
}
fn search_stack(&mut self, mv_row: usize, mv_col: usize, cand_list: usize, weight: u32) {
let mi = &self.t.mi;
let i = mi.idx(mv_row, mv_col);
let cand_mode = mi.y_mode[i] as usize;
let cand_size = mi.mi_size[i] as usize;
let large = block_width(cand_size).min(block_height(cand_size)) >= 8;
let gm0 = self.fs.fh.gm_type[self.b.ref_frame[0] as usize] as usize;
let mut cand = if (cand_mode == GLOBALMV || cand_mode == GLOBAL_GLOBALMV) && gm0 > TRANSLATION && large {
self.mvs.global_mvs[0]
} else {
let m = mi.mv[i][cand_list];
[m.row as i32, m.col as i32]
};
self.lower_mv_precision(&mut cand);
if has_newmv(cand_mode) {
self.mvs.new_mv_count += 1;
}
self.mvs.found_match = true;
let n = self.mvs.num_mv_found;
if let Some(idx) = (0..n).find(|&k| self.mvs.ref_stack_mv[k][0] == cand) {
self.mvs.weight_stack[idx] += weight;
} else if n < MAX_REF_MV_STACK_SIZE {
self.mvs.ref_stack_mv[n][0] = cand;
self.mvs.weight_stack[n] = weight;
self.mvs.num_mv_found += 1;
}
}
fn compound_search_stack(&mut self, mv_row: usize, mv_col: usize, weight: u32) {
let mi = &self.t.mi;
let i = mi.idx(mv_row, mv_col);
let mut cand = [[mi.mv[i][0].row as i32, mi.mv[i][0].col as i32], [mi.mv[i][1].row as i32, mi.mv[i][1].col as i32]];
let cand_mode = mi.y_mode[i] as usize;
if cand_mode == GLOBAL_GLOBALMV {
for (rl, c) in cand.iter_mut().enumerate() {
if self.fs.fh.gm_type[self.b.ref_frame[rl] as usize] as usize > TRANSLATION {
*c = self.mvs.global_mvs[rl];
}
}
}
for c in cand.iter_mut() {
self.lower_mv_precision(c);
}
self.mvs.found_match = true;
let n = self.mvs.num_mv_found;
if let Some(idx) = (0..n).find(|&k| self.mvs.ref_stack_mv[k][0] == cand[0] && self.mvs.ref_stack_mv[k][1] == cand[1]) {
self.mvs.weight_stack[idx] += weight;
} else if n < MAX_REF_MV_STACK_SIZE {
self.mvs.ref_stack_mv[n] = cand;
self.mvs.weight_stack[n] = weight;
self.mvs.num_mv_found += 1;
}
if has_newmv(cand_mode) {
self.mvs.new_mv_count += 1;
}
}
fn sort_stack(&mut self, start: usize, end: usize, is_compound: bool) {
let mut end = end;
let s = &mut self.mvs;
while end > start {
let mut new_end = start;
for idx in start + 1..end {
if s.weight_stack[idx - 1] < s.weight_stack[idx] {
s.weight_stack.swap(idx - 1, idx);
for list in 0..1 + is_compound as usize {
let t = s.ref_stack_mv[idx - 1][list];
s.ref_stack_mv[idx - 1][list] = s.ref_stack_mv[idx][list];
s.ref_stack_mv[idx][list] = t;
}
new_end = idx;
}
}
end = new_end;
}
}
fn extra_search(&mut self, is_compound: bool) {
self.mvs.ref_id_count = [0; 2];
self.mvs.ref_diff_count = [0; 2];
let (mi_row, mi_col) = (self.b.mi_row as isize, self.b.mi_col as isize);
let mut w4 = (self.b.bw4 as isize).min(16);
let mut h4 = (self.b.bh4 as isize).min(16);
w4 = w4.min(self.fs.fh.mi_cols as isize - mi_col);
h4 = h4.min(self.fs.fh.mi_rows as isize - mi_row);
let num4x4 = w4.min(h4);
for pass in 0..2 {
let mut idx = 0;
while idx < num4x4 && self.mvs.num_mv_found < 2 {
let (mv_row, mv_col) = if pass == 0 { (mi_row - 1, mi_col + idx) } else { (mi_row + idx, mi_col - 1) };
if !self.inside(mv_row, mv_col) {
break;
}
self.add_extra_mv_candidate(mv_row as usize, mv_col as usize, is_compound);
let mi = &self.t.mi;
let sz = mi.mi_size[mi.idx(mv_row as usize, mv_col as usize)] as usize;
idx += if pass == 0 { NUM_4X4_BLOCKS_WIDE[sz] } else { NUM_4X4_BLOCKS_HIGH[sz] } as isize;
}
}
if is_compound {
let mut combined = [[[0i32; 2]; 2]; 2];
for list in 0..2 {
let mut comp_count = 0;
for idx in 0..self.mvs.ref_id_count[list] {
combined[comp_count][list] = self.mvs.ref_id_mvs[list][idx];
comp_count += 1;
}
let mut idx = 0;
while idx < self.mvs.ref_diff_count[list] && comp_count < 2 {
combined[comp_count][list] = self.mvs.ref_diff_mvs[list][idx];
comp_count += 1;
idx += 1;
}
while comp_count < 2 {
combined[comp_count][list] = self.mvs.global_mvs[list];
comp_count += 1;
}
}
let n = self.mvs.num_mv_found;
if n == 1 {
if combined[0][0] == self.mvs.ref_stack_mv[0][0] && combined[0][1] == self.mvs.ref_stack_mv[0][1] {
self.mvs.ref_stack_mv[n] = combined[1];
} else {
self.mvs.ref_stack_mv[n] = combined[0];
}
self.mvs.weight_stack[n] = 2;
self.mvs.num_mv_found += 1;
} else {
for c in combined {
let n = self.mvs.num_mv_found;
self.mvs.ref_stack_mv[n] = c;
self.mvs.weight_stack[n] = 2;
self.mvs.num_mv_found += 1;
}
}
} else {
for idx in self.mvs.num_mv_found..2 {
self.mvs.ref_stack_mv[idx][0] = self.mvs.global_mvs[0];
}
}
}
fn add_extra_mv_candidate(&mut self, mv_row: usize, mv_col: usize, is_compound: bool) {
let mi = &self.t.mi;
let i = mi.idx(mv_row, mv_col);
let rfs = mi.ref_frame[i];
let mvs = mi.mv[i];
let bias = &self.fs.fh.ref_frame_sign_bias;
if is_compound {
for cand_list in 0..2 {
let cand_ref = rfs[cand_list];
if cand_ref > INTRA_FRAME as i8 {
for list in 0..2 {
let mut cand = [mvs[cand_list].row as i32, mvs[cand_list].col as i32];
if cand_ref == self.b.ref_frame[list] && self.mvs.ref_id_count[list] < 2 {
let k = self.mvs.ref_id_count[list];
self.mvs.ref_id_mvs[list][k] = cand;
self.mvs.ref_id_count[list] += 1;
} else if self.mvs.ref_diff_count[list] < 2 {
if bias[cand_ref as usize] != bias[self.b.ref_frame[list] as usize] {
cand = [-cand[0], -cand[1]];
}
let k = self.mvs.ref_diff_count[list];
self.mvs.ref_diff_mvs[list][k] = cand;
self.mvs.ref_diff_count[list] += 1;
}
}
}
}
} else {
for cand_list in 0..2 {
let cand_ref = rfs[cand_list];
if cand_ref > INTRA_FRAME as i8 {
let mut cand = [mvs[cand_list].row as i32, mvs[cand_list].col as i32];
if bias[cand_ref as usize] != bias[self.b.ref_frame[0] as usize] {
cand = [-cand[0], -cand[1]];
}
let n = self.mvs.num_mv_found;
if !(0..n).any(|k| self.mvs.ref_stack_mv[k][0] == cand) {
self.mvs.ref_stack_mv[n][0] = cand;
self.mvs.weight_stack[n] = 2;
self.mvs.num_mv_found += 1;
}
}
}
}
}
fn context_and_clamping(&mut self, is_compound: bool, num_new: usize) {
let bw = block_width(self.b.mi_size);
let bh = block_height(self.b.mi_size);
let n = self.mvs.num_mv_found;
for idx in 0..n {
let mut z = 0;
if idx + 1 < n {
let w0 = self.mvs.weight_stack[idx];
let w1 = self.mvs.weight_stack[idx + 1];
if w0 >= REF_CAT_LEVEL as u32 {
if w1 < REF_CAT_LEVEL as u32 {
z = 1;
}
} else {
z = 2;
}
}
self.mvs.drl_ctx_stack[idx] = z;
}
for list in 0..1 + is_compound as usize {
for idx in 0..n {
let mut mv = self.mvs.ref_stack_mv[idx][list];
mv[0] = self.clamp_mv_row(mv[0], MV_BORDER as i32 + bh * 8);
mv[1] = self.clamp_mv_col(mv[1], MV_BORDER as i32 + bw * 8);
self.mvs.ref_stack_mv[idx][list] = mv;
}
}
let (close, total) = (self.mvs.close_matches, self.mvs.total_matches);
if close == 0 {
self.mvs.new_mv_context = total.min(1);
self.mvs.ref_mv_context = total;
} else if close == 1 {
self.mvs.new_mv_context = 3 - num_new.min(1);
self.mvs.ref_mv_context = 2 + total;
} else {
self.mvs.new_mv_context = 5 - num_new.min(1);
self.mvs.ref_mv_context = 5;
}
}
pub(crate) fn clamp_mv_row(&self, mvec: i32, border: i32) -> i32 {
let bh4 = self.b.bh4 as i32;
let to_top = -((self.b.mi_row as i32 * 4) * 8);
let to_bottom = ((self.fs.fh.mi_rows as i32 - bh4 - self.b.mi_row as i32) * 4) * 8;
mvec.clamp(to_top - border, to_bottom + border)
}
pub(crate) fn clamp_mv_col(&self, mvec: i32, border: i32) -> i32 {
let bw4 = self.b.bw4 as i32;
let to_left = -((self.b.mi_col as i32 * 4) * 8);
let to_right = ((self.fs.fh.mi_cols as i32 - bw4 - self.b.mi_col as i32) * 4) * 8;
mvec.clamp(to_left - border, to_right + border)
}
/// has_overlappable_candidates( ) (7.10.3)
pub(crate) fn has_overlappable_candidates(&self) -> bool {
let mi = &self.t.mi;
let (mi_rows, mi_cols) = (self.fs.fh.mi_rows as usize, self.fs.fh.mi_cols as usize);
if self.b.avail_u {
let mut x4 = self.b.mi_col;
while x4 < mi_cols.min(self.b.mi_col + self.b.bw4) {
let c = (x4 | 1).min(mi_cols - 1);
if mi.ref_frame[mi.idx(self.b.mi_row - 1, c)][0] > INTRA_FRAME as i8 {
return true;
}
x4 += 2;
}
}
if self.b.avail_l {
let mut y4 = self.b.mi_row;
while y4 < mi_rows.min(self.b.mi_row + self.b.bh4) {
let r = (y4 | 1).min(mi_rows - 1);
if mi.ref_frame[mi.idx(r, self.b.mi_col - 1)][0] > INTRA_FRAME as i8 {
return true;
}
y4 += 2;
}
}
false
}
/// Find warp samples process (7.10.4).
pub(crate) fn find_warp_samples(&mut self) {
self.mvs.num_samples = 0;
self.mvs.num_samples_scanned = 0;
let w4 = self.b.bw4 as isize;
let h4 = self.b.bh4 as isize;
let (mi_row, mi_col) = (self.b.mi_row as isize, self.b.mi_col as isize);
let (mi_rows, mi_cols) = (self.fs.fh.mi_rows as isize, self.fs.fh.mi_cols as isize);
let mut do_top_left = true;
let mut do_top_right = true;
if self.b.avail_u {
let mi = &self.t.mi;
let src_w = NUM_4X4_BLOCKS_WIDE[mi.mi_size[mi.idx(mi_row as usize - 1, mi_col as usize)] as usize] as isize;
if w4 <= src_w {
let col_offset = -(mi_col & (src_w - 1));
if col_offset < 0 {
do_top_left = false;
}
if col_offset + src_w > w4 {
do_top_right = false;
}
self.add_sample(-1, 0);
} else {
let mut i = 0;
while i < w4.min(mi_cols - mi_col) {
let mi = &self.t.mi;
let src_w = NUM_4X4_BLOCKS_WIDE[mi.mi_size[mi.idx(mi_row as usize - 1, (mi_col + i) as usize)] as usize] as isize;
let step = w4.min(src_w);
self.add_sample(-1, i);
i += step;
}
}
}
if self.b.avail_l {
let mi = &self.t.mi;
let src_h = NUM_4X4_BLOCKS_HIGH[mi.mi_size[mi.idx(mi_row as usize, mi_col as usize - 1)] as usize] as isize;
if h4 <= src_h {
let row_offset = -(mi_row & (src_h - 1));
if row_offset < 0 {
do_top_left = false;
}
self.add_sample(0, -1);
} else {
let mut i = 0;
while i < h4.min(mi_rows - mi_row) {
let mi = &self.t.mi;
let src_h = NUM_4X4_BLOCKS_HIGH[mi.mi_size[mi.idx((mi_row + i) as usize, mi_col as usize - 1)] as usize] as isize;
let step = h4.min(src_h);
self.add_sample(i, -1);
i += step;
}
}
}
if do_top_left {
self.add_sample(-1, -1);
}
if do_top_right && w4.max(h4) <= 16 {
self.add_sample(-1, w4);
}
if self.mvs.num_samples == 0 && self.mvs.num_samples_scanned > 0 {
self.mvs.num_samples = 1;
}
}
fn add_sample(&mut self, delta_row: isize, delta_col: isize) {
if self.mvs.num_samples_scanned >= LEAST_SQUARES_SAMPLES_MAX {
return;
}
let mv_row = self.b.mi_row as isize + delta_row;
let mv_col = self.b.mi_col as isize + delta_col;
if !self.inside(mv_row, mv_col) {
return;
}
let mi = &self.t.mi;
let i = mi.idx(mv_row as usize, mv_col as usize);
if !mi.written[i] {
return;
}
if mi.ref_frame[i][0] != self.b.ref_frame[0] || mi.ref_frame[i][1] != -1 {
return;
}
let cand_sz = mi.mi_size[i] as usize;
let cand_w4 = NUM_4X4_BLOCKS_WIDE[cand_sz] as isize;
let cand_h4 = NUM_4X4_BLOCKS_HIGH[cand_sz] as isize;
let cand_row = mv_row & !(cand_h4 - 1);
let cand_col = mv_col & !(cand_w4 - 1);
let mid_y = cand_row * 4 + cand_h4 * 2 - 1;
let mid_x = cand_col * 4 + cand_w4 * 2 - 1;
let threshold = block_width(self.b.mi_size).max(block_height(self.b.mi_size)).clamp(16, 112);
let cm = mi.mv[mi.idx(cand_row as usize, cand_col as usize)][0];
let mv_diff_row = (cm.row as i32 - self.b.mv_i[0][0]).abs();
let mv_diff_col = (cm.col as i32 - self.b.mv_i[0][1]).abs();
let valid = mv_diff_row + mv_diff_col <= threshold;
let cand = [mid_y as i32 * 8, mid_x as i32 * 8, mid_y as i32 * 8 + cm.row as i32, mid_x as i32 * 8 + cm.col as i32];
self.mvs.num_samples_scanned += 1;
if !valid && self.mvs.num_samples_scanned > 1 {
return;
}
self.mvs.cand_list[self.mvs.num_samples] = cand;
if valid {
self.mvs.num_samples += 1;
}
}
#[inline]
pub(crate) fn inside(&self, r: isize, c: isize) -> bool {
c >= self.mi_col_start as isize && c < self.mi_col_end as isize && r >= self.mi_row_start as isize && r < self.mi_row_end as isize
}
}
-72
View File
@@ -1,72 +0,0 @@
//! Optional data parallelism (rayon behind the `threads` feature; sequential otherwise and on
//! wasm32).
#[cfg(feature = "threads")]
use std::sync::Arc;
/// A handle to the decoder's worker pool (or none: run on the calling thread).
#[derive(Clone, Default)]
pub(crate) struct Pool {
#[cfg(feature = "threads")]
pool: Option<Arc<rayon::ThreadPool>>,
}
impl Pool {
/// A pool of `threads` workers (sequential when `threads` <= 1, without the `threads`
/// feature, or on wasm32).
pub fn new(threads: usize) -> Pool {
#[cfg(feature = "threads")]
{
let pool = if threads > 1 && cfg!(not(target_arch = "wasm32")) {
rayon::ThreadPoolBuilder::new().num_threads(threads).thread_name(|i| format!("av1-{i}")).build().ok().map(Arc::new)
} else {
None
};
Pool { pool }
}
#[cfg(not(feature = "threads"))]
{
let _ = threads;
Pool {}
}
}
pub fn is_parallel(&self) -> bool {
#[cfg(feature = "threads")]
{
self.pool.is_some()
}
#[cfg(not(feature = "threads"))]
{
false
}
}
/// `f(i, &mut items[i])` for every item, in parallel when possible; results in order.
pub fn map_mut<T: Send, R: Send>(&self, items: &mut [T], f: impl Fn(usize, &mut T) -> R + Sync + Send) -> Vec<R> {
#[cfg(feature = "threads")]
if let Some(pool) = &self.pool
&& items.len() > 1
{
use rayon::prelude::*;
return pool.install(|| items.par_iter_mut().enumerate().map(|(i, t)| f(i, t)).collect());
}
items.iter_mut().enumerate().map(|(i, t)| f(i, t)).collect()
}
/// `f(i, chunk)` for the consecutive `chunk_len`-element chunks of `data`.
pub fn chunks<T: Send>(&self, data: &mut [T], chunk_len: usize, f: impl Fn(usize, &mut [T]) + Sync + Send) {
let chunk_len = chunk_len.max(1);
#[cfg(feature = "threads")]
if let Some(pool) = &self.pool
&& data.len() > chunk_len
{
use rayon::prelude::*;
pool.install(|| data.par_chunks_mut(chunk_len).enumerate().for_each(|(i, c)| f(i, c)));
return;
}
for (i, c) in data.chunks_mut(chunk_len).enumerate() {
f(i, c);
}
}
}
-269
View File
@@ -1,269 +0,0 @@
//! In-loop post filters run by decode_frame_wrapup: loop filter (7.14), CDEF (7.15),
//! super-resolution upscaling (7.16) and loop restoration (7.17).
use crate::spec_tables::{
GLOBAL_GLOBALMV, GLOBALMV, INTRA_FRAME, MAX_LOOP_FILTER, NEARESTMV, NUM_4X4_BLOCKS_HIGH, NUM_4X4_BLOCKS_WIDE, SEG_LVL_ALT_LF_Y_V, TX_HEIGHT,
TX_WIDTH,
};
use crate::state::FrameState;
use crate::stats::{DecodeStats, Stage, Timer};
/// The post-filters run on the frame worker; `_pool` is for row-parallel filtering (not yet used).
/// `draft`: only super-resolution (which sets the output size) runs.
pub(crate) fn apply(fs: &mut FrameState, stats: &mut DecodeStats, _pool: &crate::par::Pool, draft: bool) {
let mut t = Timer::start();
let lvl = fs.fh.lf.level;
if (lvl[0] != 0 || lvl[1] != 0) && !draft {
loop_filter(fs);
}
stats.add(Stage::LoopFilter, t.lap());
let cdef = if draft { None } else { crate::cdef::apply(fs) };
stats.add(Stage::Cdef, t.lap());
let (up_cur, up_cdef) = if fs.fh.use_superres {
let c = crate::restoration::upscale(fs, &fs.cur);
let d = cdef.as_ref().map(|f| crate::restoration::upscale(fs, f));
(c, d)
} else {
(std::mem::take(&mut fs.cur), cdef)
};
stats.add(Stage::Superres, t.lap());
fs.cur = if fs.fh.lr.uses_lr && !draft {
crate::restoration::loop_restoration(fs, &up_cur, up_cdef.as_ref().unwrap_or(&up_cur))
} else {
up_cdef.unwrap_or(up_cur)
};
stats.add(Stage::Restoration, t.lap());
}
// ---------------------------------------------------------------------------------------------
// Loop filter (7.14)
fn loop_filter(fs: &mut FrameState) {
let (mi_rows, mi_cols) = (fs.fh.mi_rows as usize, fs.fh.mi_cols as usize);
for plane in 0..fs.num_planes {
if plane == 0 || fs.fh.lf.level[1 + plane] != 0 {
for pass in 0..2 {
let row_step = if plane == 0 { 1 } else { 1 << fs.ssy };
let col_step = if plane == 0 { 1 } else { 1 << fs.ssx };
let mut row = 0;
while row < mi_rows {
let mut col = 0;
while col < mi_cols {
edge(fs, plane, pass, row, col);
col += col_step;
}
row += row_step;
}
}
}
}
}
fn edge(fs: &mut FrameState, plane: usize, pass: usize, row: usize, col: usize) {
let (sub_x, sub_y) = if plane == 0 { (0, 0) } else { (fs.ssx, fs.ssy) };
let (dx, dy) = if pass == 0 { (1i32, 0i32) } else { (0, 1) };
let x = col * 4;
let y = row * 4;
let row = row | sub_y;
let col = col | sub_x;
let on_screen = !(x >= fs.fh.frame_width as usize || y >= fs.fh.frame_height as usize || (pass == 0 && x == 0) || (pass == 1 && y == 0));
if !on_screen {
return;
}
let xp = x >> sub_x;
let yp = y >> sub_y;
let prev_row = row - ((dy as usize) << sub_y);
let prev_col = col - ((dx as usize) << sub_x);
let tx_sz = fs.lf_tx_size(plane, row >> sub_y, col >> sub_x);
let is_tx_edge = if pass == 0 { xp.is_multiple_of(TX_WIDTH[tx_sz] as usize) } else { yp.is_multiple_of(TX_HEIGHT[tx_sz] as usize) };
if !is_tx_edge {
return;
}
let mi = &fs.mi;
let i = mi.idx(row, col);
let mi_size = mi.mi_size[i] as usize;
let plane_size = fs.plane_residual_size(mi_size, plane);
let skip = mi.skip[i];
let is_intra = mi.ref_frame[i][0] <= INTRA_FRAME as i8;
let is_block_edge = if pass == 0 {
xp.is_multiple_of(4 * NUM_4X4_BLOCKS_WIDE[plane_size] as usize)
} else {
yp.is_multiple_of(4 * NUM_4X4_BLOCKS_HIGH[plane_size] as usize)
};
if !(is_block_edge || !skip || is_intra) {
return;
}
let prev_tx_sz = fs.lf_tx_size(plane, prev_row >> sub_y, prev_col >> sub_x);
// filter size (7.14.3)
let base_size = if pass == 0 { TX_WIDTH[prev_tx_sz].min(TX_WIDTH[tx_sz]) } else { TX_HEIGHT[prev_tx_sz].min(TX_HEIGHT[tx_sz]) } as usize;
let filter_size = if plane == 0 { 16.min(base_size) } else { 8.min(base_size) };
let (mut lvl, mut limit, mut blimit, mut thresh) = strength(fs, row, col, plane, pass);
if lvl == 0 {
(lvl, limit, blimit, thresh) = strength(fs, prev_row, prev_col, plane, pass);
}
if lvl == 0 {
return;
}
let t = LfThresh::new(limit, blimit, thresh, fs.bit_depth);
let pl = &mut fs.cur.planes[plane];
let stride = pl.stride;
let (step, along) = if pass == 0 { (1, stride) } else { (stride, 1) };
let base = yp * stride + xp;
let chroma = plane > 0;
for k in 0..4 {
sample_filter(&mut pl.data, base + k * along, step, chroma, filter_size, t);
}
}
/// Adaptive filter strength (7.14.4): (lvl, limit, blimit, thresh).
fn strength(fs: &FrameState, row: usize, col: usize, plane: usize, pass: usize) -> (i32, i32, i32, i32) {
let mi = &fs.mi;
let i = mi.idx(row, col);
let segment = mi.segment_id[i] as usize;
let rf = mi.ref_frame[i][0];
let mode = mi.y_mode[i] as usize;
let mode_type = (mode >= NEARESTMV && mode != GLOBALMV && mode != GLOBAL_GLOBALMV) as usize;
let delta_lf = if fs.fh.delta_lf_multi { mi.delta_lf[i][if plane == 0 { pass } else { plane + 1 }] } else { mi.delta_lf[i][0] } as i32;
// 7.14.5
let idx = if plane == 0 { pass } else { plane + 1 };
let base = (delta_lf + fs.fh.lf.level[idx] as i32).clamp(0, MAX_LOOP_FILTER as i32);
let mut lvl_seg = base;
let feature = SEG_LVL_ALT_LF_Y_V + idx;
if fs.fh.seg.enabled && fs.fh.seg.features.enabled[segment][feature] {
lvl_seg = (fs.fh.seg.features.data[segment][feature] + lvl_seg).clamp(0, MAX_LOOP_FILTER as i32);
}
if fs.fh.lf.delta_enabled {
let n_shift = lvl_seg >> 5;
let d = &fs.fh.lf.deltas;
if rf == INTRA_FRAME as i8 {
lvl_seg += d.ref_deltas[INTRA_FRAME] << n_shift;
} else if rf > INTRA_FRAME as i8 {
lvl_seg += (d.ref_deltas[rf as usize] << n_shift) + (d.mode_deltas[mode_type] << n_shift);
}
lvl_seg = lvl_seg.clamp(0, MAX_LOOP_FILTER as i32);
}
let lvl = lvl_seg;
let sharp = fs.fh.lf.sharpness as i32;
let shift = if sharp > 4 {
2
} else if sharp > 0 {
1
} else {
0
};
let limit = if sharp > 0 { (lvl >> shift).clamp(1, 9 - sharp) } else { (lvl >> shift).max(1) };
let blimit = 2 * (lvl + 2) + limit;
let thresh = lvl >> 4;
(lvl, limit, blimit, thresh)
}
/// Per-edge thresholds of the sample filter, scaled to the bit depth.
#[derive(Clone, Copy)]
struct LfThresh {
limit: i32,
blimit: i32,
thresh: i32,
/// Flatness threshold 1 << (BitDepth - 8).
flat: i32,
/// 0x80 << (BitDepth - 8) and 1 << (BitDepth - 1).
off: i32,
half: i32,
}
impl LfThresh {
fn new(limit: i32, blimit: i32, thresh: i32, bd: u32) -> LfThresh {
let sh = bd - 8;
LfThresh { limit: limit << sh, blimit: blimit << sh, thresh: thresh << sh, flat: 1 << sh, off: 0x80 << sh, half: 1 << (bd - 1) }
}
}
/// Sample filtering process (7.14.6) for one line across the edge: `base` indexes q0 and
/// `step` is the distance between samples across the edge.
#[allow(clippy::too_many_arguments)]
#[inline(always)]
fn sample_filter(d: &mut [u16], base: usize, step: usize, chroma: bool, filter_size: usize, t: LfThresh) {
let at = |d: &[u16], k: isize| -> i32 { d[(base as isize + k * step as isize) as usize] as i32 };
let (q0, q1, p0, p1) = (at(d, 0), at(d, 1), at(d, -1), at(d, -2));
let lim = t.limit;
// filter mask (7.14.6.2)
if (p1 - p0).abs() > lim || (q1 - q0).abs() > lim || (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 > t.blimit {
return;
}
let hev = (p1 - p0).abs() > t.thresh || (q1 - q0).abs() > t.thresh;
let mut flat = false;
if filter_size > 4 {
let (q2, p2) = (at(d, 2), at(d, -3));
if (p2 - p1).abs() > lim || (q2 - q1).abs() > lim {
return;
}
let tf = t.flat;
flat = (p1 - p0).abs() <= tf && (q1 - q0).abs() <= tf && (p2 - p0).abs() <= tf && (q2 - q0).abs() <= tf;
if !chroma {
// filterLen >= 8
let (q3, p3) = (at(d, 3), at(d, -4));
if (p3 - p2).abs() > lim || (q3 - q2).abs() > lim {
return;
}
flat = flat && (p3 - p0).abs() <= tf && (q3 - q0).abs() <= tf;
}
}
if !flat {
// narrow filter (7.14.6.3)
let c = |v: i32| v.clamp(-t.half, t.half - 1);
let off = t.off;
let (ps1, ps0, qs0, qs1) = (p1 - off, p0 - off, q0 - off, q1 - off);
let mut filter = if hev { c(ps1 - qs1) } else { 0 };
filter = c(filter + 3 * (qs0 - ps0));
let filter1 = c(filter + 4) >> 3;
let filter2 = c(filter + 3) >> 3;
d[base] = (c(qs0 - filter1) + off) as u16;
d[base - step] = (c(ps0 + filter2) + off) as u16;
if !hev {
let f = (filter1 + 1) >> 1;
d[base + step] = (c(qs1 - f) + off) as u16;
d[base - 2 * step] = (c(ps1 + f) + off) as u16;
}
return;
}
if chroma {
wide::<2, 1, 3>(d, base, step);
} else if filter_size == 8 {
wide::<3, 0, 3>(d, base, step);
} else {
let tf = t.flat;
let q0 = at(d, 0);
let p0 = at(d, -1);
let flat2 = (4..7).all(|k| (at(d, k) - q0).abs() <= tf && (at(d, -k - 1) - p0).abs() <= tf);
if flat2 { wide::<6, 1, 4>(d, base, step) } else { wide::<3, 0, 3>(d, base, step) }
}
}
/// Wide filter process (7.14.6.4) with n = N, the 2-weight taps |j| <= N2 and log2Size = LOG2.
#[inline(always)]
fn wide<const N: usize, const N2: usize, const LOG2: u32>(d: &mut [u16], base: usize, step: usize) {
// v[k] = sample at offset k - (N + 1) from q0, k in 0 .. 2N + 2
let mut v = [0i32; 14];
let first = base - (N + 1) * step;
for (k, x) in v[..2 * N + 2].iter_mut().enumerate() {
*x = d[first + k * step] as i32;
}
// e[m] = v[clamp(m - N, 0, 2N + 1)]: the clamped tap positions as one array, so
// F[i] = sum_{jj = 0}^{2N} e[i + 1 + jj] * (2 if |jj - N| <= N2 else 1), a sliding window.
let mut e = [0i32; 25];
for (m, x) in e[..4 * N + 1].iter_mut().enumerate() {
*x = v[m.saturating_sub(N).min(2 * N + 1)];
}
let mut f = [0i32; 12];
let mut win: i32 = e[1..2 * N + 2].iter().sum();
for i in 0..2 * N {
if i > 0 {
win += e[i + 1 + 2 * N] - e[i];
}
let centre: i32 = e[i + 1 + N - N2..=i + 1 + N + N2].iter().sum();
f[i] = (win + centre + (1 << (LOG2 - 1))) >> LOG2;
}
let out = base - N * step;
for i in 0..2 * N {
d[out + i * step] = f[i] as u16;
}
}
-298
View File
@@ -1,298 +0,0 @@
//! Super-resolution upscaling (7.16) and loop restoration (7.17).
use crate::frame::{FrameBuf, Plane};
use crate::spec_tables::{
FILTER_BITS, RESTORE_NONE, RESTORE_SGRPROJ, RESTORE_WIENER, SGR_PARAMS, SGRPROJ_MTABLE_BITS, SGRPROJ_PRJ_BITS, SGRPROJ_RECIP_BITS,
SGRPROJ_RST_BITS, SGRPROJ_SGR_BITS, SUPERRES_EXTRA_BITS, SUPERRES_FILTER_OFFSET, SUPERRES_FILTER_TAPS, SUPERRES_SCALE_BITS, SUPERRES_SCALE_MASK,
UPSCALE_FILTER,
};
use crate::state::FrameState;
/// Upscale a frame horizontally from FrameWidth to UpscaledWidth (7.16).
pub(crate) fn upscale(fs: &FrameState, input: &FrameBuf) -> FrameBuf {
let fh = &fs.fh;
let up_w = fh.upscaled_width as usize;
let mut out = FrameBuf::new(up_w, fh.frame_height as usize, fs.num_planes, fs.ssx, fs.ssy, fs.bit_depth as u8);
let max = (1i32 << fs.bit_depth) - 1;
for plane in 0..fs.num_planes {
let (sub_x, sub_y) = if plane > 0 { (fs.ssx, fs.ssy) } else { (0, 0) };
let down_w = ((fh.frame_width as usize) + sub_x) >> sub_x;
let up_pw = (up_w + sub_x) >> sub_x;
let plane_h = ((fh.frame_height as usize) + sub_y) >> sub_y;
let sb = SUPERRES_SCALE_BITS as i64;
let step_x = (((down_w as i64) << sb) + (up_pw as i64 / 2)) / up_pw as i64;
let err = up_pw as i64 * step_x - ((down_w as i64) << sb);
let mut initial = (-(((up_pw - down_w) as i64) << (sb - 1)) + up_pw as i64 / 2) / up_pw as i64 + (1 << (SUPERRES_EXTRA_BITS - 1)) - err / 2;
initial &= SUPERRES_SCALE_MASK as i64;
let mi_w = fh.mi_cols as usize >> sub_x;
let max_x = (mi_w * 4 - 1) as i64;
let src = &input.planes[plane];
let dst = &mut out.planes[plane];
for y in 0..plane_h {
let row = src.row(y);
for x in 0..up_pw {
let src_x = -(1i64 << sb) + initial + x as i64 * step_x;
let src_px = src_x >> sb;
let subpel = ((src_x & SUPERRES_SCALE_MASK as i64) >> SUPERRES_EXTRA_BITS) as usize;
let mut sum = 0i32;
for k in 0..SUPERRES_FILTER_TAPS {
let sx = (src_px + k as i64 - SUPERRES_FILTER_OFFSET as i64).clamp(0, max_x) as usize;
sum += row[sx] as i32 * UPSCALE_FILTER[subpel][k] as i32;
}
dst.set(x, y, ((sum + 64) >> FILTER_BITS).clamp(0, max) as u16);
}
}
}
out
}
struct LrCtx<'a> {
cur: &'a Plane,
cdef: &'a Plane,
plane_end_x: i32,
plane_end_y: i32,
stripe_start_y: i32,
stripe_end_y: i32,
}
impl LrCtx<'_> {
/// Get source sample process (7.17.6).
#[inline(always)]
fn sample(&self, x: i32, y: i32) -> i32 {
let x = x.min(self.plane_end_x).max(0) as usize;
let y = y.min(self.plane_end_y).max(0);
if y < self.stripe_start_y {
let y = (self.stripe_start_y - 2).max(y) as usize;
self.cur.at(x, y) as i32
} else if y > self.stripe_end_y {
let y = (self.stripe_end_y + 2).min(y) as usize;
self.cur.at(x, y) as i32
} else {
self.cdef.at(x, y as usize) as i32
}
}
}
/// Position and size of a (at most 4x4) restoration block within its plane.
#[derive(Clone, Copy)]
struct LrBlock {
x: i32,
y: i32,
w: i32,
h: i32,
}
/// Loop restoration (7.17): returns LrFrame given UpscaledCurrFrame and UpscaledCdefFrame.
pub(crate) fn loop_restoration(fs: &FrameState, cur: &FrameBuf, cdef: &FrameBuf) -> FrameBuf {
let mut lr = cdef.clone();
for plane in 0..fs.num_planes {
if fs.fh.lr.frame_restoration_type[plane] == RESTORE_NONE as u8 {
continue;
}
restore_plane(fs, cur, cdef, &mut lr.planes[plane], plane);
}
lr
}
fn restore_plane(fs: &FrameState, cur: &FrameBuf, cdef: &FrameBuf, out: &mut Plane, plane: usize) {
let fh = &fs.fh;
let up_w = fh.upscaled_width as i32;
let fr_h = fh.frame_height as i32;
let bd = fs.bit_depth;
let (sub_x, sub_y) = if plane > 0 { (fs.ssx as i32, fs.ssy as i32) } else { (0, 0) };
let unit_size = fh.lr.loop_restoration_size[plane] as i32;
let unit_rows = fs.lr_unit_rows[plane] as i32;
let unit_cols = fs.lr_unit_cols[plane] as i32;
let plane_end_x = ((up_w + sub_x) >> sub_x) - 1;
let plane_end_y = ((fr_h + sub_y) >> sub_y) - 1;
let mut row = 0;
while row * 4 < fr_h {
let luma_y = row * 4;
let stripe_num = (luma_y + 8) / 64;
let stripe_start_y = (-8 + stripe_num * 64) >> sub_y;
let stripe_end_y = stripe_start_y + (64 >> sub_y) - 1;
let ctx = LrCtx { cur: &cur.planes[plane], cdef: &cdef.planes[plane], plane_end_x, plane_end_y, stripe_start_y, stripe_end_y };
let unit_row = (unit_rows - 1).min(((row * 4 + 8) >> sub_y) / unit_size);
let mut col = 0;
while col * 4 < up_w {
let unit_col = (unit_cols - 1).min(((col * 4) >> sub_x) / unit_size);
let x = (col * 4) >> sub_x;
let y = (row * 4) >> sub_y;
let w = (4 >> sub_x).min(plane_end_x - x + 1);
let h = (4 >> sub_y).min(plane_end_y - y + 1);
if w > 0 && h > 0 {
let ui = (unit_row * unit_cols + unit_col) as usize;
let blk = LrBlock { x, y, w, h };
match fs.lr.lr_type[plane][ui] {
t if t == RESTORE_WIENER as u8 => wiener(fs, &ctx, out, (plane, ui), blk, bd),
t if t == RESTORE_SGRPROJ as u8 => self_guided(fs, &ctx, out, (plane, ui), blk, bd),
_ => {}
}
}
col += 1;
}
row += 1;
}
}
fn wiener(fs: &FrameState, ctx: &LrCtx, out: &mut Plane, (plane, ui): (usize, usize), blk: LrBlock, bd: u32) {
let LrBlock { x, y, w, h } = blk;
let round0: u32 = if bd == 12 { 5 } else { 3 };
let round1: u32 = if bd == 12 { 9 } else { 11 };
let coef = &fs.lr.lr_wiener[plane][ui];
let mk = |c: &[i8; 3]| -> [i32; 7] {
let mut f = [0i32; 7];
f[3] = 128;
for i in 0..3 {
f[i] = c[i] as i32;
f[6 - i] = c[i] as i32;
f[3] -= 2 * c[i] as i32;
}
f
};
let vfilter = mk(&coef[0]);
let hfilter = mk(&coef[1]);
let fb = FILTER_BITS as u32;
let offset = 1i32 << (bd + fb - round0 - 1);
let limit = (1i32 << (bd + 1 + fb - round0)) - 1;
let mut inter = [[0i32; 4]; 10];
for r in 0..(h + 6) as usize {
for c in 0..w as usize {
let mut s = 0;
for t in 0..7 {
s += hfilter[t] * ctx.sample(x + c as i32 + t as i32 - 3, y + r as i32 - 3);
}
let v = (s + (1 << (round0 - 1))) >> round0;
inter[r][c] = v.clamp(-offset, limit - offset);
}
}
let max = (1i32 << bd) - 1;
for r in 0..h as usize {
for c in 0..w as usize {
let mut s = 0;
for t in 0..7 {
s += vfilter[t] * inter[r + t][c];
}
let v = (s + (1 << (round1 - 1))) >> round1;
out.set(x as usize + c, y as usize + r, v.clamp(0, max) as u16);
}
}
}
fn self_guided(fs: &FrameState, ctx: &LrCtx, out: &mut Plane, (plane, ui): (usize, usize), blk: LrBlock, bd: u32) {
let LrBlock { x, y, w, h } = blk;
let set = fs.lr.lr_sgr_set[plane][ui] as usize;
let mut flt0 = [[0i32; 4]; 4];
let mut flt1 = [[0i32; 4]; 4];
box_filter(ctx, blk, (set, 0), bd, &mut flt0);
box_filter(ctx, blk, (set, 1), bd, &mut flt1);
let xqd = fs.lr.lr_sgr_xqd[plane][ui];
let w0 = xqd[0] as i32;
let w1 = xqd[1] as i32;
let w2 = (1 << SGRPROJ_PRJ_BITS) - w0 - w1;
let r0 = SGR_PARAMS[set][0];
let r1 = SGR_PARAMS[set][2];
let max = (1i32 << bd) - 1;
let sh = (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) as u32;
for i in 0..h as usize {
for j in 0..w as usize {
let u = (ctx.cdef.at(x as usize + j, y as usize + i) as i32) << SGRPROJ_RST_BITS;
let mut v = w1 * u;
v += if r0 != 0 { w0 * flt0[i][j] } else { w0 * u };
v += if r1 != 0 { w2 * flt1[i][j] } else { w2 * u };
let s = (v + (1 << (sh - 1))) >> sh;
out.set(x as usize + j, y as usize + i, s.clamp(0, max) as u16);
}
}
}
#[inline(always)]
fn round2_64(x: i64, n: u32) -> i64 {
if n == 0 { x } else { (x + (1i64 << (n - 1))) >> n }
}
/// Box sums of the samples within `r` of (x, y): (sum of squares, sum).
fn box_sums(ctx: &LrCtx, x: i32, y: i32, r: i32) -> (i64, i64) {
let mut a = 0i64;
let mut b = 0i64;
for dy in -r..=r {
for dx in -r..=r {
let c = ctx.sample(x + dx, y + dy) as i64;
a += c * c;
b += c;
}
}
(a, b)
}
/// Self-guided filter constants of one pass.
struct SgrConsts {
n: i64,
s: i64,
one_over_n: i64,
}
/// (A, B) coefficients of one sample from its box sums.
fn sgr_coefficients(a: i64, b: i64, k: &SgrConsts, bd: u32) -> (i64, i64) {
let a = round2_64(a, 2 * (bd - 8));
let d = round2_64(b, bd - 8);
let p = (a * k.n - d * d).max(0);
let z = round2_64(p * k.s, SGRPROJ_MTABLE_BITS as u32);
let a2 = if z >= 255 {
256
} else if z == 0 {
1
} else {
((z << SGRPROJ_SGR_BITS) + z / 2) / (z + 1)
};
let b2 = ((1i64 << SGRPROJ_SGR_BITS) - a2) * b * k.one_over_n;
(a2, round2_64(b2, SGRPROJ_RECIP_BITS as u32))
}
fn box_weight(pass: usize, i: i32, dx: i32, dy: i32) -> i64 {
if pass == 0 {
if ((i + dy) & 1) != 0 { if dx == 0 { 6 } else { 5 } } else { 0 }
} else if dx == 0 || dy == 0 {
4
} else {
3
}
}
fn box_filter(ctx: &LrCtx, blk: LrBlock, (set, pass): (usize, usize), bd: u32, f: &mut [[i32; 4]; 4]) {
let LrBlock { x, y, w, h } = blk;
let r = SGR_PARAMS[set][pass * 2] as i32;
if r == 0 {
return;
}
let eps = SGR_PARAMS[set][pass * 2 + 1] as i64;
let n = ((2 * r + 1) * (2 * r + 1)) as i64;
let n2e = n * n * eps;
let k = SgrConsts { n, s: ((1i64 << SGRPROJ_MTABLE_BITS) + n2e / 2) / n2e, one_over_n: ((1i64 << SGRPROJ_RECIP_BITS) + n / 2) / n };
let mut a_arr = [[0i64; 6]; 6];
let mut b_arr = [[0i64; 6]; 6];
for i in -1..h + 1 {
for j in -1..w + 1 {
let (a, b) = box_sums(ctx, x + j, y + i, r);
let (a2, b2) = sgr_coefficients(a, b, &k, bd);
a_arr[(i + 1) as usize][(j + 1) as usize] = a2;
b_arr[(i + 1) as usize][(j + 1) as usize] = b2;
}
}
for i in 0..h {
let shift = if pass == 0 && (i & 1) == 1 { 4 } else { 5 };
for j in 0..w {
let mut a = 0i64;
let mut b = 0i64;
for dy in -1..=1i32 {
for dx in -1..=1i32 {
let weight = box_weight(pass, i, dx, dy);
a += weight * a_arr[(i + dy + 1) as usize][(j + dx + 1) as usize];
b += weight * b_arr[(i + dy + 1) as usize][(j + dx + 1) as usize];
}
}
let v = a * ctx.cdef.at((x + j) as usize, (y + i) as usize) as i64 + b;
f[i as usize][j as usize] = round2_64(v, (SGRPROJ_SGR_BITS + shift - SGRPROJ_RST_BITS) as u32) as i32;
}
}
}
File diff suppressed because it is too large Load Diff
-294
View File
@@ -1,294 +0,0 @@
//! Per-frame decoding state shared by the tiles and the post-filters.
//!
//! [`FrameShared`] holds what tiles only read (headers, references, projected motion field);
//! [`TileState`] what a tile writes (reconstruction, mode info, per-block filter parameters,
//! entropy contexts). With one tile the frame's own buffers move into the tile state and back;
//! with several, each tile decodes into private buffers covering its area (tiles never read
//! outside themselves), which are then copied into the frame.
use crate::decoder::RefFrame;
use crate::frame::{FrameBuf, MiInfo};
use crate::header::{FrameHeader, RefInfo, SequenceHeader};
use crate::spec_tables::{DEFAULT_INTRA_FRAME_Y_MODE_CDF, NUM_REF_FRAMES, RESTORE_NONE, SUBSAMPLED_SIZE};
use std::ops::{Deref, DerefMut};
use std::sync::Arc;
/// Read-only (during tile decoding) frame state.
pub(crate) struct FrameShared {
pub seq: SequenceHeader,
pub fh: FrameHeader,
pub num_planes: usize,
pub ssx: usize,
pub ssy: usize,
pub bit_depth: u32,
pub lr_unit_rows: [usize; 3],
pub lr_unit_cols: [usize; 3],
pub cdef_cols: usize,
/// Reference slots (FrameStore and saved state) used by this frame.
pub refs: [Option<Arc<RefFrame>>; NUM_REF_FRAMES],
pub ref_info: [RefInfo; NUM_REF_FRAMES],
/// MotionFieldMvs[ ref ][ y8 * w8 + x8 ] (7.9); empty when unused.
pub motion_field: [Vec<[i32; 2]>; 8],
/// PrevSegmentIds (mi units).
pub prev_segment_ids: Vec<u8>,
}
impl FrameShared {
pub fn plane_residual_size(&self, subsize: usize, plane: usize) -> usize {
let sx = if plane > 0 { self.ssx } else { 0 };
let sy = if plane > 0 { self.ssy } else { 0 };
SUBSAMPLED_SIZE[subsize][sx][sy] as usize
}
}
/// A byte per 4x4 unit of a plane (LoopfilterTxSizes), possibly for a region only.
#[derive(Clone, Default)]
pub(crate) struct Grid8 {
pub data: Vec<u8>,
pub ox: usize,
pub oy: usize,
pub stride: usize,
pub rows: usize,
}
impl Grid8 {
fn new(ox: usize, oy: usize, stride: usize, rows: usize) -> Grid8 {
Grid8 { data: vec![0; stride * rows], ox, oy, stride, rows }
}
#[inline]
pub fn get(&self, row: usize, col: usize) -> u8 {
self.data[(row - self.oy) * self.stride + col - self.ox]
}
/// Stores outside the grid are dropped (blocks reaching past the allocated margin).
#[inline]
pub fn set(&mut self, row: usize, col: usize, v: u8) {
if col >= self.ox && col < self.ox + self.stride && row >= self.oy && row < self.oy + self.rows {
self.data[(row - self.oy) * self.stride + col - self.ox] = v;
}
}
fn copy_into(&self, dst: &mut Grid8) {
for y in 0..self.rows {
let d = (self.oy + y - dst.oy) * dst.stride + self.ox - dst.ox;
dst.data[d..d + self.stride].copy_from_slice(&self.data[y * self.stride..(y + 1) * self.stride]);
}
}
}
/// Loop restoration parameters per unit.
#[derive(Clone, Default)]
pub(crate) struct LrUnits {
pub lr_type: [Vec<u8>; 3],
pub lr_wiener: [Vec<[[i8; 3]; 2]>; 3],
pub lr_sgr_set: [Vec<u8>; 3],
pub lr_sgr_xqd: [Vec<[i8; 2]>; 3],
}
/// What tile decoding writes.
pub(crate) struct TileState {
pub cur: FrameBuf,
pub mi: MiInfo,
/// cdef_idx per 64x64 block (-1: not coded).
pub cdef: Vec<i8>,
/// LoopfilterTxSizes[ plane ][ row ][ col ] in 4x4 units of each plane.
pub lf_tx_size: [Grid8; 3],
pub lr: LrUnits,
/// LR units this tile coded (per plane), for merging.
pub lr_written: [Vec<usize>; 3],
pub above_level: [Vec<u8>; 3],
pub above_dc: [Vec<u8>; 3],
pub above_seg_pred: Vec<u8>,
pub left_level: [Vec<u8>; 3],
pub left_dc: [Vec<u8>; 3],
pub left_seg_pred: Vec<u8>,
/// TileIntraFrameYModeCdf (reset per tile).
pub intra_frame_y_mode_cdf: [[[u16; 14]; 5]; 5],
cdef_cols: usize,
}
impl TileState {
fn with(cur: FrameBuf, mi: MiInfo, cdef: Vec<i8>, lf_tx_size: [Grid8; 3], lr: LrUnits, cdef_cols: usize) -> TileState {
let (mi_cols, mi_rows) = (mi.cols, mi.rows);
TileState {
cur,
mi,
cdef,
lf_tx_size,
lr,
lr_written: Default::default(),
above_level: std::array::from_fn(|_| vec![0; mi_cols + 64]),
above_dc: std::array::from_fn(|_| vec![0; mi_cols + 64]),
above_seg_pred: vec![0; mi_cols + 64],
left_level: std::array::from_fn(|_| vec![0; mi_rows + 64]),
left_dc: std::array::from_fn(|_| vec![0; mi_rows + 64]),
left_seg_pred: vec![0; mi_rows + 64],
intra_frame_y_mode_cdf: DEFAULT_INTRA_FRAME_Y_MODE_CDF,
cdef_cols,
}
}
#[inline]
pub fn cdef_idx(&self, r: usize, c: usize) -> i8 {
self.cdef[(r >> 4) * self.cdef_cols + (c >> 4)]
}
#[inline]
pub fn set_cdef_idx(&mut self, r: usize, c: usize, v: i8) {
let i = (r >> 4) * self.cdef_cols + (c >> 4);
if i < self.cdef.len() {
self.cdef[i] = v;
}
}
#[inline]
pub fn set_lf_tx_size(&mut self, plane: usize, row: usize, col: usize, v: u8) {
self.lf_tx_size[plane].set(row, col, v);
}
}
pub(crate) struct FrameState {
pub sh: FrameShared,
pub cur: FrameBuf,
pub mi: MiInfo,
cdef: Vec<i8>,
lf_tx_size: [Grid8; 3],
pub lr: LrUnits,
}
impl Deref for FrameState {
type Target = FrameShared;
fn deref(&self) -> &FrameShared {
&self.sh
}
}
impl DerefMut for FrameState {
fn deref_mut(&mut self) -> &mut FrameShared {
&mut self.sh
}
}
fn count_units_in_frame(unit_size: usize, frame_size: usize) -> usize {
((frame_size + (unit_size >> 1)) / unit_size).max(1)
}
impl FrameState {
pub fn new(seq: &SequenceHeader, fh: &FrameHeader) -> FrameState {
let c = &seq.color;
let (ssx, ssy) = (c.subsampling_x as usize, c.subsampling_y as usize);
let mi_cols = fh.mi_cols as usize;
let mi_rows = fh.mi_rows as usize;
let cur = FrameBuf::new(fh.frame_width as usize, fh.frame_height as usize, c.num_planes, ssx, ssy, c.bit_depth);
let cdef_cols = mi_cols.div_ceil(16) + 2;
let cdef_rows = mi_rows.div_ceil(16) + 2;
let mut lr_unit_rows = [0; 3];
let mut lr_unit_cols = [0; 3];
let mut lr = LrUnits::default();
for plane in 0..c.num_planes {
if fh.lr.frame_restoration_type[plane] == RESTORE_NONE as u8 {
continue;
}
let sub_x = if plane == 0 { 0 } else { ssx };
let sub_y = if plane == 0 { 0 } else { ssy };
let unit = fh.lr.loop_restoration_size[plane] as usize;
lr_unit_rows[plane] = count_units_in_frame(unit, (fh.frame_height as usize + sub_y) >> sub_y);
lr_unit_cols[plane] = count_units_in_frame(unit, (fh.upscaled_width as usize + sub_x) >> sub_x);
let n = lr_unit_rows[plane] * lr_unit_cols[plane];
lr.lr_type[plane] = vec![RESTORE_NONE as u8; n];
lr.lr_wiener[plane] = vec![[[0; 3]; 2]; n];
lr.lr_sgr_set[plane] = vec![0; n];
lr.lr_sgr_xqd[plane] = vec![[0; 2]; n];
}
FrameState {
sh: FrameShared {
seq: seq.clone(),
fh: fh.clone(),
num_planes: c.num_planes,
ssx,
ssy,
bit_depth: c.bit_depth as u32,
lr_unit_rows,
lr_unit_cols,
cdef_cols,
refs: Default::default(),
ref_info: Default::default(),
motion_field: Default::default(),
prev_segment_ids: vec![0; mi_cols * mi_rows],
},
cur,
mi: MiInfo::new(mi_cols, mi_rows, fh.allow_screen_content_tools),
cdef: vec![-1; cdef_cols * cdef_rows],
lf_tx_size: std::array::from_fn(|_| Grid8::new(0, 0, mi_cols + 32, mi_rows + 32)),
lr,
}
}
#[inline]
pub fn cdef_idx(&self, r: usize, c: usize) -> i8 {
self.cdef[(r >> 4) * self.cdef_cols + (c >> 4)]
}
#[inline]
pub fn lf_tx_size(&self, plane: usize, row: usize, col: usize) -> usize {
self.lf_tx_size[plane].get(row, col) as usize
}
/// Move the frame's buffers into a tile state covering the whole frame.
pub fn take_tile_state(&mut self) -> TileState {
TileState::with(
std::mem::take(&mut self.cur),
std::mem::take(&mut self.mi),
std::mem::take(&mut self.cdef),
std::mem::take(&mut self.lf_tx_size),
std::mem::take(&mut self.lr),
self.cdef_cols,
)
}
/// Return the buffers taken by [`FrameState::take_tile_state`].
pub fn put_tile_state(&mut self, t: TileState) {
self.cur = t.cur;
self.mi = t.mi;
self.cdef = t.cdef;
self.lf_tx_size = t.lf_tx_size;
self.lr = t.lr;
}
/// Private buffers for the tile covering mode info rows [r0, r1) x columns [c0, c1); tiles
/// on the right / bottom frame edge also cover the allocation margins.
pub fn tile_state(&self, r0: usize, r1: usize, c0: usize, c1: usize) -> TileState {
let (mi_cols, mi_rows) = (self.mi.cols, self.mi.rows);
let x1 = if c1 >= mi_cols { usize::MAX } else { c1 * 4 };
let y1 = if r1 >= mi_rows { usize::MAX } else { r1 * 4 };
let cur = self.cur.region_like(c0 * 4, r0 * 4, x1, y1);
let mi = MiInfo::region(mi_cols, mi_rows, c0, r0, c1 - c0, r1 - r0, !self.mi.palette_size[0].is_empty());
let lf = std::array::from_fn(|p| {
let (sx, sy) = if p == 0 { (0, 0) } else { (self.ssx, self.ssy) };
let full = &self.lf_tx_size[p];
let (gx0, gy0) = (c0 >> sx, r0 >> sy);
let gx1 = if c1 >= mi_cols { full.stride } else { c1 >> sx };
let gy1 = if r1 >= mi_rows { full.rows } else { r1 >> sy };
Grid8::new(gx0, gy0, gx1 - gx0, gy1 - gy0)
});
TileState::with(cur, mi, vec![-1; self.cdef.len()], lf, self.lr.clone(), self.cdef_cols)
}
/// Copy a tile's private buffers into the frame.
pub fn merge_tile_state(&mut self, t: &TileState) {
for p in 0..self.num_planes {
t.cur.planes[p].copy_into(&mut self.cur.planes[p]);
t.lf_tx_size[p].copy_into(&mut self.lf_tx_size[p]);
for &u in &t.lr_written[p] {
self.lr.lr_type[p][u] = t.lr.lr_type[p][u];
self.lr.lr_wiener[p][u] = t.lr.lr_wiener[p][u];
self.lr.lr_sgr_set[p][u] = t.lr.lr_sgr_set[p][u];
self.lr.lr_sgr_xqd[p][u] = t.lr.lr_sgr_xqd[p][u];
}
}
t.mi.copy_into(&mut self.mi);
for (d, &s) in self.cdef.iter_mut().zip(&t.cdef) {
if s != -1 {
*d = s;
}
}
}
}
-114
View File
@@ -1,114 +0,0 @@
//! Decode statistics: frame / tile counts and the busy time of every decoding stage.
/// A decoding stage (see [`DecodeStats::stage_secs`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Stage {
/// Frame header follow-up: reference setup, motion field estimation, CDF initialisation.
Setup,
/// Tile decoding: mode info, coefficients, prediction and reconstruction.
Tiles,
/// Deblocking loop filter (7.14).
LoopFilter,
/// CDEF (7.15).
Cdef,
/// Super-resolution upscaling (7.16).
Superres,
/// Loop restoration (7.17).
Restoration,
/// Reference update, motion vector storage, output copy and film grain synthesis.
Output,
}
impl Stage {
pub const COUNT: usize = 7;
pub const ALL: [Stage; Stage::COUNT] =
[Stage::Setup, Stage::Tiles, Stage::LoopFilter, Stage::Cdef, Stage::Superres, Stage::Restoration, Stage::Output];
pub fn name(self) -> &'static str {
match self {
Stage::Setup => "setup",
Stage::Tiles => "tiles",
Stage::LoopFilter => "deblock",
Stage::Cdef => "cdef",
Stage::Superres => "superres",
Stage::Restoration => "restoration",
Stage::Output => "output",
}
}
}
/// Counters accumulated by a [`crate::Decoder`]. Stage times are busy time summed over every
/// thread that worked on the stage (wall-clock per piece of work), so with threads they can add
/// up to more than the elapsed time.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct DecodeStats {
/// Frames decoded (excluding show_existing_frame repeats).
pub frames: u64,
/// Frames decoded in draft mode without in-loop filters.
pub draft_frames: u64,
/// Tiles decoded.
pub tiles: u64,
/// Busy seconds per [`Stage`] (index with `stage as usize`).
pub stage_secs: [f64; Stage::COUNT],
}
impl DecodeStats {
pub fn secs(&self, stage: Stage) -> f64 {
self.stage_secs[stage as usize]
}
pub(crate) fn add(&mut self, stage: Stage, secs: f64) {
self.stage_secs[stage as usize] += secs;
}
pub(crate) fn merge(&mut self, o: &DecodeStats) {
self.frames += o.frames;
self.draft_frames += o.draft_frames;
self.tiles += o.tiles;
for (a, b) in self.stage_secs.iter_mut().zip(o.stage_secs.iter()) {
*a += b;
}
}
/// Sum of all stage times.
pub fn total_secs(&self) -> f64 {
self.stage_secs.iter().sum()
}
}
/// A wall-clock stopwatch (a no-op on wasm32, which has no clock in std).
pub(crate) struct Timer {
#[cfg(not(target_arch = "wasm32"))]
start: std::time::Instant,
}
impl Timer {
#[inline]
pub fn start() -> Timer {
Timer {
#[cfg(not(target_arch = "wasm32"))]
start: std::time::Instant::now(),
}
}
/// Seconds since `start` (0 on wasm32).
#[inline]
pub fn secs(&self) -> f64 {
#[cfg(not(target_arch = "wasm32"))]
{
self.start.elapsed().as_secs_f64()
}
#[cfg(target_arch = "wasm32")]
{
0.0
}
}
/// Seconds since `start`, restarting the stopwatch.
#[inline]
pub fn lap(&mut self) -> f64 {
let s = self.secs();
*self = Timer::start();
s
}
}
-138
View File
@@ -1,138 +0,0 @@
//! Symbol (arithmetic) decoder, spec 8.2.
//!
//! The state follows the specification literally: `SymbolValue` / `SymbolRange` are 15/16-bit
//! quantities renormalised by reading `bits` new bits at a time, and `SymbolMaxBits` counts the
//! bits still available (going negative while zero padding is consumed).
const EC_PROB_SHIFT: u32 = 6;
const EC_MIN_PROB: u32 = 4;
pub(crate) struct SymbolDecoder<'a> {
data: &'a [u8],
/// Next byte of `data` to load into the window.
next: usize,
/// SymbolValue followed by `k` look-ahead bits: the bitstream with every bit inverted
/// (the spec's renormalisation XORs the new bits with ones), zero bits past the end of
/// the data reading as ones. SymbolValue = win >> k.
win: u64,
k: u32,
range: u32,
pub disable_update: bool,
}
impl<'a> SymbolDecoder<'a> {
/// `init_symbol( sz )` over `data` (the tile's bytes).
pub fn new(data: &'a [u8], disable_update: bool) -> Self {
let mut d = SymbolDecoder { data, next: 0, win: 0, k: 0, range: 1 << 15, disable_update };
// SymbolValue = ((1 << 15) - 1) ^ (first 15 bits): 15 inverted bits.
let mut filled = 0;
while filled < 15 + 32 {
d.win = (d.win << 8) | d.inverted_byte() as u64;
filled += 8;
}
d.k = filled - 15;
d
}
#[inline(always)]
fn inverted_byte(&mut self) -> u8 {
let b = self.data.get(self.next).copied().unwrap_or(0);
self.next += 1;
!b
}
/// `read_symbol( cdf )`: `cdf` has N + 1 entries (the last is the adaptation counter).
#[inline]
pub fn read_symbol(&mut self, cdf: &mut [u16]) -> usize {
let n = cdf.len() - 1;
let symbol = self.decode(cdf, n);
if !self.disable_update {
update_cdf(cdf, n, symbol);
}
symbol
}
#[inline(always)]
fn decode(&mut self, cdf: &[u16], n: usize) -> usize {
let value = (self.win >> self.k) as u32;
let mut cur = self.range;
let mut symbol = 0usize;
let mut prev;
loop {
prev = cur;
let f = (1u32 << 15) - cdf[symbol] as u32;
cur = ((self.range >> 8) * (f >> EC_PROB_SHIFT)) >> (7 - EC_PROB_SHIFT);
cur += EC_MIN_PROB * (n - symbol - 1) as u32;
if value >= cur {
break;
}
symbol += 1;
}
self.range = prev - cur;
self.win -= (cur as u64) << self.k;
self.renormalize();
symbol
}
/// Renormalisation (8.2.6): SymbolValue takes `bits` more (inverted) bits, which are
/// already in the window.
#[inline(always)]
fn renormalize(&mut self) {
let bits = 15 - (31 - self.range.leading_zeros());
self.range <<= bits;
self.k -= bits;
if self.k < 16 {
// SymbolValue < 2^16, so 16 + k <= 64 bits stay in the window.
while self.k <= 40 {
self.win = (self.win << 8) | self.inverted_byte() as u64;
self.k += 8;
}
}
}
/// `read_bool()`
#[inline]
pub fn read_bool(&mut self) -> u32 {
let cdf = [1u16 << 14, 1 << 15, 0];
self.decode(&cdf, 2) as u32
}
/// `read_literal( n )` (L(n))
pub fn read_literal(&mut self, n: u32) -> u32 {
let mut x = 0;
for _ in 0..n {
x = (x << 1) | self.read_bool();
}
x
}
/// NS(n)
pub fn read_ns(&mut self, n: u32) -> u32 {
if n <= 1 {
return 0;
}
let w = 32 - n.leading_zeros();
let m = (1u32 << w) - n;
let v = self.read_literal(w - 1);
if v < m {
return v;
}
let extra = self.read_literal(1);
(v << 1) - m + extra
}
}
/// CDF adaptation (8.2.6).
#[inline(always)]
pub(crate) fn update_cdf(cdf: &mut [u16], n: usize, symbol: usize) {
let count = cdf[n];
let rate = 3 + (count > 15) as u32 + (count > 31) as u32 + (31 - (n as u32).leading_zeros()).min(2);
// Entries below `symbol` move towards 0, the others towards 1 << 15 (the specification's
// comparison with tmp, resolved per entry: c >= 0 and c <= 1 << 15), without branching on
// the decoded symbol.
for (i, e) in cdf[..n - 1].iter_mut().enumerate() {
let c = *e as u32;
*e = if i < symbol { c - (c >> rate) } else { c + (((1 << 15) - c) >> rate) } as u16;
}
cdf[n] += (count < 32) as u16;
}
File diff suppressed because it is too large Load Diff
-780
View File
@@ -1,780 +0,0 @@
//! Inverse transforms (spec 7.13), implemented step by step as specified.
use crate::spec_tables::{
ADST_ADST, ADST_DCT, ADST_FLIPADST, COS128_LOOKUP, DCT_ADST, DCT_DCT, DCT_FLIPADST, FLIPADST_ADST, FLIPADST_DCT, FLIPADST_FLIPADST, H_ADST,
H_DCT, H_FLIPADST, TRANSFORM_ROW_SHIFT, TX_HEIGHT_LOG2, TX_WIDTH_LOG2, V_ADST, V_DCT, V_FLIPADST,
};
#[inline(always)]
fn round2_64(x: i64, n: u32) -> i32 {
if n == 0 { x as i32 } else { ((x + (1i64 << (n - 1))) >> n) as i32 }
}
#[inline(always)]
pub(crate) fn round2(x: i32, n: u32) -> i32 {
if n == 0 { x } else { (x + (1 << (n - 1))) >> n }
}
#[inline(always)]
fn cos128(angle: i32) -> i64 {
let a = (angle & 255) as usize;
(match a {
0..=64 => COS128_LOOKUP[a] as i32,
65..=128 => -(COS128_LOOKUP[128 - a] as i32),
129..=192 => -(COS128_LOOKUP[a - 128] as i32),
_ => COS128_LOOKUP[256 - a] as i32,
}) as i64
}
#[inline(always)]
fn sin128(angle: i32) -> i64 {
cos128(angle - 64)
}
/// One value of the 1D transforms, or `L` independent columns / rows of them processed
/// together (lane-wise, so the steps vectorise).
pub(crate) trait Lane: Copy {
const ZERO: Self;
/// (Round2( a * cos - b * sin, 12 ), Round2( a * sin + b * cos, 12 ))
fn rotate(a: Self, b: Self, cos: i32, sin: i32) -> (Self, Self);
/// (Clamp( x + y ), Clamp( x - y )) to [lo, hi]
fn hadamard(x: Self, y: Self, lo: i32, hi: i32) -> (Self, Self);
fn neg(self) -> Self;
/// Round2( v * m, 12 )
fn mul_round12(self, m: i32) -> Self;
fn mul(self, k: i32) -> Self;
fn adst4(t: &mut [Self]);
}
impl Lane for i32 {
const ZERO: i32 = 0;
#[inline(always)]
fn rotate(a: i32, b: i32, cos: i32, sin: i32) -> (i32, i32) {
let (a, b, c, s) = (a as i64, b as i64, cos as i64, sin as i64);
(round2_64(a * c - b * s, 12), round2_64(a * s + b * c, 12))
}
#[inline(always)]
fn hadamard(x: i32, y: i32, lo: i32, hi: i32) -> (i32, i32) {
((x + y).clamp(lo, hi), (x - y).clamp(lo, hi))
}
#[inline(always)]
fn neg(self) -> i32 {
-self
}
#[inline(always)]
fn mul_round12(self, m: i32) -> i32 {
round2_64(self as i64 * m as i64, 12)
}
#[inline(always)]
fn mul(self, k: i32) -> i32 {
self * k
}
fn adst4(t: &mut [i32]) {
inverse_adst4_scalar(t);
}
}
/// Lanes in 32-bit arithmetic: exact for conforming streams up to 10 bits per sample (the
/// spec requires every intermediate value to fit in 8 + BitDepth <= 18 bits, so products with
/// the 12-bit cosines stay below 2^31). Wrapping operations keep corrupt streams from
/// panicking.
impl<const L: usize> Lane for [i32; L] {
const ZERO: Self = [0; L];
#[inline(always)]
fn rotate(a: Self, b: Self, cos: i32, sin: i32) -> (Self, Self) {
let mut x = [0; L];
let mut y = [0; L];
for l in 0..L {
let p = a[l].wrapping_mul(cos).wrapping_sub(b[l].wrapping_mul(sin));
let q = a[l].wrapping_mul(sin).wrapping_add(b[l].wrapping_mul(cos));
x[l] = p.wrapping_add(1 << 11) >> 12;
y[l] = q.wrapping_add(1 << 11) >> 12;
}
(x, y)
}
#[inline(always)]
fn hadamard(x: Self, y: Self, lo: i32, hi: i32) -> (Self, Self) {
let mut a = [0; L];
let mut b = [0; L];
for l in 0..L {
a[l] = x[l].wrapping_add(y[l]).max(lo).min(hi);
b[l] = x[l].wrapping_sub(y[l]).max(lo).min(hi);
}
(a, b)
}
#[inline(always)]
fn neg(self) -> Self {
self.map(i32::wrapping_neg)
}
#[inline(always)]
fn mul_round12(self, m: i32) -> Self {
self.map(|v| v.wrapping_mul(m).wrapping_add(1 << 11) >> 12)
}
#[inline(always)]
fn mul(self, k: i32) -> Self {
self.map(|v| v.wrapping_mul(k))
}
fn adst4(t: &mut [Self]) {
for l in 0..L {
let mut v = [t[0][l], t[1][l], t[2][l], t[3][l]];
inverse_adst4_scalar(&mut v);
for k in 0..4 {
t[k][l] = v[k];
}
}
}
}
fn brev(num_bits: u32, x: usize) -> usize {
let mut t = 0;
for i in 0..num_bits {
let bit = (x >> i) & 1;
t += bit << (num_bits - 1 - i);
}
t
}
/// Butterfly rotation B( a, b, angle, flip ).
#[inline(always)]
fn bf<T: Lane>(t: &mut [T], a: usize, b: usize, angle: i32, flip: bool) {
let (x, y) = T::rotate(t[a], t[b], cos128(angle) as i32, sin128(angle) as i32);
t[a] = x;
t[b] = y;
if flip {
t.swap(a, b);
}
}
/// Hadamard rotation H( a, b, flip, r ).
#[inline(always)]
fn hd<T: Lane>(t: &mut [T], a: usize, b: usize, flip: bool, r: u32) {
let (a, b) = if flip { (b, a) } else { (a, b) };
let lo = -(1i32 << (r - 1));
let hi = (1i32 << (r - 1)) - 1;
let (x, y) = T::hadamard(t[a], t[b], lo, hi);
t[a] = x;
t[b] = y;
}
/// Inverse DCT of length 2^n (7.13.2.3), in place.
pub(crate) fn inverse_dct<T: Lane>(t: &mut [T], n: u32, r: u32) {
// 7.13.2.2 permutation
let len = 1usize << n;
let mut copy = [T::ZERO; 64];
copy[..len].copy_from_slice(&t[..len]);
for i in 0..len {
t[i] = copy[brev(n, i)];
}
inverse_dct_stage1(t, n, r);
inverse_dct_stage2(t, n, r);
inverse_dct_stage3(t, n, r);
inverse_dct_stage4(t, n, r);
inverse_dct_stage5(t, n, r);
inverse_dct_stage6(t, n, r);
}
fn inverse_dct_stage1<T: Lane>(t: &mut [T], n: u32, r: u32) {
if n == 6 {
for i in 0..16 {
bf(t, 32 + i, 63 - i, 63 - 4 * brev(4, i) as i32, false);
}
}
if n >= 5 {
for i in 0..8 {
bf(t, 16 + i, 31 - i, 6 + ((brev(3, 7 - i) as i32) << 3), false);
}
}
if n == 6 {
for i in 0..16 {
hd(t, 32 + i * 2, 33 + i * 2, i & 1 == 1, r);
}
}
if n >= 4 {
for i in 0..4 {
bf(t, 8 + i, 15 - i, 12 + ((brev(2, 3 - i) as i32) << 4), false);
}
}
if n >= 5 {
for i in 0..8 {
hd(t, 16 + 2 * i, 17 + 2 * i, i & 1 == 1, r);
}
}
}
fn inverse_dct_stage2<T: Lane>(t: &mut [T], n: u32, r: u32) {
if n == 6 {
for i in 0..4 {
for j in 0..2 {
bf(t, 62 - i * 4 - j, 33 + i * 4 + j, 60 - 16 * brev(2, i) as i32 + 64 * j as i32, true);
}
}
}
if n >= 3 {
for i in 0..2 {
bf(t, 4 + i, 7 - i, 56 - 32 * i as i32, false);
}
}
if n >= 4 {
for i in 0..4 {
hd(t, 8 + 2 * i, 9 + 2 * i, i & 1 == 1, r);
}
}
if n >= 5 {
for i in 0..2 {
for j in 0..2 {
bf(t, 30 - 4 * i - j, 17 + 4 * i + j, 24 + ((j as i32) << 6) + (((1 - i) as i32) << 5), true);
}
}
}
if n == 6 {
for i in 0..8 {
for j in 0..2 {
hd(t, 32 + i * 4 + j, 35 + i * 4 - j, i & 1 == 1, r);
}
}
}
}
fn inverse_dct_stage3<T: Lane>(t: &mut [T], n: u32, r: u32) {
for i in 0..2 {
bf(t, 2 * i, 2 * i + 1, 32 + 16 * i as i32, i == 0);
}
if n >= 3 {
for i in 0..2 {
hd(t, 4 + 2 * i, 5 + 2 * i, i == 1, r);
}
}
if n >= 4 {
for i in 0..2 {
bf(t, 14 - i, 9 + i, 48 + 64 * i as i32, true);
}
}
if n >= 5 {
for i in 0..4 {
for j in 0..2 {
hd(t, 16 + 4 * i + j, 19 + 4 * i - j, i & 1 == 1, r);
}
}
}
if n == 6 {
for i in 0..2 {
for j in 0..4 {
bf(t, 61 - i * 8 - j, 34 + i * 8 + j, 56 - i as i32 * 32 + (j as i32 >> 1) * 64, true);
}
}
}
}
fn inverse_dct_stage4<T: Lane>(t: &mut [T], n: u32, r: u32) {
for i in 0..2 {
hd(t, i, 3 - i, false, r);
}
if n >= 3 {
bf(t, 6, 5, 32, true);
}
if n >= 4 {
for i in 0..2 {
for j in 0..2 {
hd(t, 8 + 4 * i + j, 11 + 4 * i - j, i == 1, r);
}
}
}
if n >= 5 {
for i in 0..4 {
bf(t, 29 - i, 18 + i, 48 + (i as i32 >> 1) * 64, true);
}
}
if n == 6 {
for i in 0..4 {
for j in 0..4 {
hd(t, 32 + 8 * i + j, 39 + 8 * i - j, i & 1 == 1, r);
}
}
}
}
fn inverse_dct_stage5<T: Lane>(t: &mut [T], n: u32, r: u32) {
if n >= 3 {
for i in 0..4 {
hd(t, i, 7 - i, false, r);
}
}
if n >= 4 {
for i in 0..2 {
bf(t, 13 - i, 10 + i, 32, true);
}
}
if n >= 5 {
for i in 0..2 {
for j in 0..4 {
hd(t, 16 + i * 8 + j, 23 + i * 8 - j, i == 1, r);
}
}
}
if n == 6 {
for i in 0..8 {
bf(t, 59 - i, 36 + i, if i < 4 { 48 } else { 112 }, true);
}
}
if n >= 4 {
for i in 0..8 {
hd(t, i, 15 - i, false, r);
}
}
}
fn inverse_dct_stage6<T: Lane>(t: &mut [T], n: u32, r: u32) {
if n >= 5 {
for i in 0..4 {
bf(t, 27 - i, 20 + i, 32, true);
}
}
if n == 6 {
for i in 0..8 {
hd(t, 32 + i, 47 - i, false, r);
hd(t, 48 + i, 63 - i, true, r);
}
}
if n >= 5 {
for i in 0..16 {
hd(t, i, 31 - i, false, r);
}
}
if n == 6 {
for i in 0..8 {
bf(t, 55 - i, 40 + i, 32, true);
}
}
if n == 6 {
for i in 0..32 {
hd(t, i, 63 - i, false, r);
}
}
}
const SINPI_1_9: i64 = 1321;
const SINPI_2_9: i64 = 2482;
const SINPI_3_9: i64 = 3344;
const SINPI_4_9: i64 = 3803;
fn inverse_adst4_scalar(t: &mut [i32]) {
let mut s = [0i64; 7];
let mut x = [0i64; 4];
let t0 = t[0] as i64;
let t1 = t[1] as i64;
let t2 = t[2] as i64;
let t3 = t[3] as i64;
s[0] = SINPI_1_9 * t0;
s[1] = SINPI_2_9 * t0;
s[2] = SINPI_3_9 * t1;
s[3] = SINPI_4_9 * t2;
s[4] = SINPI_1_9 * t2;
s[5] = SINPI_2_9 * t3;
s[6] = SINPI_4_9 * t3;
let a7 = t0 - t2;
let b7 = a7 + t3;
s[0] += s[3];
s[1] -= s[4];
s[3] = s[2];
s[2] = SINPI_3_9 * b7;
s[0] += s[5];
s[1] -= s[6];
x[0] = s[0] + s[3];
x[1] = s[1] + s[3];
x[2] = s[2];
x[3] = s[0] + s[1];
x[3] -= s[3];
for i in 0..4 {
t[i] = round2_64(x[i], 12);
}
}
fn adst_in_permute<T: Lane>(t: &mut [T], n: u32) {
let n0 = 1usize << n;
let mut copy = [T::ZERO; 16];
copy[..n0].copy_from_slice(&t[..n0]);
for i in 0..n0 {
let idx = if i & 1 == 1 { i - 1 } else { n0 - i - 1 };
t[i] = copy[idx];
}
}
fn adst_out_permute<T: Lane>(t: &mut [T], n: u32) {
let n0 = 1usize << n;
let mut copy = [T::ZERO; 16];
copy[..n0].copy_from_slice(&t[..n0]);
for i in 0..n0 {
let a = (i >> 3) & 1;
let b = ((i >> 2) & 1) ^ ((i >> 3) & 1);
let c = ((i >> 1) & 1) ^ ((i >> 2) & 1);
let d = (i & 1) ^ ((i >> 1) & 1);
let idx = ((d << 3) | (c << 2) | (b << 1) | a) >> (4 - n);
t[i] = if i & 1 == 1 { copy[idx].neg() } else { copy[idx] };
}
}
fn inverse_adst8<T: Lane>(t: &mut [T], r: u32) {
adst_in_permute(t, 3);
for i in 0..4 {
bf(t, 2 * i, 2 * i + 1, 60 - 16 * i as i32, true);
}
for i in 0..4 {
hd(t, i, 4 + i, false, r);
}
for i in 0..2 {
bf(t, 4 + 3 * i, 5 + i, 48 - 32 * i as i32, true);
}
for i in 0..2 {
for j in 0..2 {
hd(t, 4 * j + i, 2 + 4 * j + i, false, r);
}
}
for i in 0..2 {
bf(t, 2 + 4 * i, 3 + 4 * i, 32, true);
}
adst_out_permute(t, 3);
}
fn inverse_adst16<T: Lane>(t: &mut [T], r: u32) {
adst_in_permute(t, 4);
for i in 0..8 {
bf(t, 2 * i, 2 * i + 1, 62 - 8 * i as i32, true);
}
for i in 0..8 {
hd(t, i, 8 + i, false, r);
}
for i in 0..2 {
bf(t, 8 + 2 * i, 9 + 2 * i, 56 - 32 * i as i32, true);
bf(t, 13 + 2 * i, 12 + 2 * i, 8 + 32 * i as i32, true);
}
for i in 0..4 {
for j in 0..2 {
hd(t, 8 * j + i, 4 + 8 * j + i, false, r);
}
}
for i in 0..2 {
for j in 0..2 {
bf(t, 4 + 8 * j + 3 * i, 5 + 8 * j + i, 48 - 32 * i as i32, true);
}
}
for i in 0..2 {
for j in 0..4 {
hd(t, 4 * j + i, 2 + 4 * j + i, false, r);
}
}
for i in 0..4 {
bf(t, 2 + 4 * i, 3 + 4 * i, 32, true);
}
adst_out_permute(t, 4);
}
fn inverse_adst<T: Lane>(t: &mut [T], n: u32, r: u32) {
match n {
2 => T::adst4(t),
3 => inverse_adst8(t, r),
_ => inverse_adst16(t, r),
}
}
fn inverse_identity<T: Lane>(t: &mut [T], n: u32) {
match n {
2 => {
for v in t[..4].iter_mut() {
*v = v.mul_round12(5793);
}
}
3 => {
for v in t[..8].iter_mut() {
*v = v.mul(2);
}
}
4 => {
for v in t[..16].iter_mut() {
*v = v.mul_round12(11586);
}
}
_ => {
for v in t[..32].iter_mut() {
*v = v.mul(4);
}
}
}
}
fn inverse_wht(t: &mut [i32], shift: u32) {
let mut a = t[0] >> shift;
let mut c = t[1] >> shift;
let mut d = t[2] >> shift;
let mut b = t[3] >> shift;
a += c;
d -= b;
let e = (a - d) >> 1;
b = e - b;
c = e - c;
a -= b;
d += c;
t[0] = a;
t[1] = b;
t[2] = c;
t[3] = d;
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Kind1d {
Dct,
Adst,
Identity,
}
fn row_kind(tx_type: usize) -> Kind1d {
match tx_type {
DCT_DCT | ADST_DCT | FLIPADST_DCT | H_DCT => Kind1d::Dct,
DCT_ADST | ADST_ADST | DCT_FLIPADST | FLIPADST_FLIPADST | ADST_FLIPADST | FLIPADST_ADST | H_ADST | H_FLIPADST => Kind1d::Adst,
_ => Kind1d::Identity,
}
}
fn col_kind(tx_type: usize) -> Kind1d {
match tx_type {
DCT_DCT | DCT_ADST | DCT_FLIPADST | V_DCT => Kind1d::Dct,
ADST_DCT | ADST_ADST | FLIPADST_DCT | FLIPADST_FLIPADST | ADST_FLIPADST | FLIPADST_ADST | V_ADST | V_FLIPADST => Kind1d::Adst,
_ => Kind1d::Identity,
}
}
/// 2D inverse transform (7.13.3). `dequant` is 64x64 (row stride 64, only the top-left 32x32
/// may be non-zero); the residual is written to `residual` with row stride `w`.
pub(crate) fn inverse_transform_2d(dequant: &[i32], residual: &mut [i32], tx_sz: usize, tx_type: usize, lossless: bool, bit_depth: u32) {
let log2w = TX_WIDTH_LOG2[tx_sz] as u32;
let log2h = TX_HEIGHT_LOG2[tx_sz] as u32;
let w = 1usize << log2w;
let h = 1usize << log2h;
let row_shift = if lossless { 0 } else { TRANSFORM_ROW_SHIFT[tx_sz] as u32 };
let col_shift = if lossless { 0 } else { 4 };
let row_clamp = bit_depth + 8;
let col_clamp = (bit_depth + 6).max(16);
let rk = row_kind(tx_type);
let ck = col_kind(tx_type);
let rect2 = log2w.abs_diff(log2h) == 1;
let p = Tx2d { w, h, log2w, log2h, row_shift, col_shift, row_clamp, col_clamp, rk, ck, rect2 };
if !lossless && bit_depth <= 10 {
if w >= 8 && h >= 8 {
transform_lanes::<8>(dequant, residual, &p);
} else {
transform_lanes::<4>(dequant, residual, &p);
}
return;
}
scalar_row_pass(dequant, residual, &p, lossless);
scalar_col_pass(residual, &p, lossless);
}
fn scalar_row_pass(dequant: &[i32], residual: &mut [i32], p: &Tx2d, lossless: bool) {
let (w, h, log2w, row_shift, row_clamp, col_clamp, rk, rect2) = (p.w, p.h, p.log2w, p.row_shift, p.row_clamp, p.col_clamp, p.rk, p.rect2);
let mut t = [0i32; 64];
let in_w = w.min(32);
for i in 0..h {
// Every 1D transform maps an all-zero row to zeros (each step is linear with
// Round2( 0 ) = 0), so rows without coefficients (including rows 32..63) are zero.
if i >= 32 || dequant[i * 64..i * 64 + in_w].iter().all(|&v| v == 0) {
residual[i * w..i * w + w].fill(0);
continue;
}
t[..in_w].copy_from_slice(&dequant[i * 64..i * 64 + in_w]);
t[in_w..w].fill(0);
if rect2 {
for v in t[..w].iter_mut() {
*v = round2_64(*v as i64 * 2896, 12);
}
}
if lossless {
inverse_wht(&mut t, 2);
} else {
match rk {
Kind1d::Dct => inverse_dct(&mut t, log2w, row_clamp),
Kind1d::Adst => inverse_adst(&mut t, log2w, row_clamp),
Kind1d::Identity => inverse_identity(&mut t, log2w),
}
}
let lo = -(1i32 << (col_clamp - 1));
let hi = (1i32 << (col_clamp - 1)) - 1;
for j in 0..w {
residual[i * w + j] = round2(t[j], row_shift).clamp(lo, hi);
}
}
}
fn scalar_col_pass(residual: &mut [i32], p: &Tx2d, lossless: bool) {
let (w, h, log2h, col_shift, col_clamp, ck) = (p.w, p.h, p.log2h, p.col_shift, p.col_clamp, p.ck);
let mut t = [0i32; 64];
for j in 0..w {
for i in 0..h {
t[i] = residual[i * w + j];
}
if lossless {
inverse_wht(&mut t, 0);
} else {
match ck {
Kind1d::Dct => inverse_dct(&mut t, log2h, col_clamp),
Kind1d::Adst => inverse_adst(&mut t, log2h, col_clamp),
Kind1d::Identity => inverse_identity(&mut t, log2h),
}
}
for i in 0..h {
residual[i * w + j] = round2(t[i], col_shift);
}
}
}
struct Tx2d {
w: usize,
h: usize,
log2w: u32,
log2h: u32,
row_shift: u32,
col_shift: u32,
row_clamp: u32,
col_clamp: u32,
rk: Kind1d,
ck: Kind1d,
rect2: bool,
}
#[inline(always)]
fn transform_1d<T: Lane>(t: &mut [T], kind: Kind1d, n: u32, r: u32) {
match kind {
Kind1d::Dct => inverse_dct(t, n, r),
Kind1d::Adst => inverse_adst(t, n, r),
Kind1d::Identity => inverse_identity(t, n),
}
}
/// The 2D inverse transform with `L` rows (row pass) or columns (column pass) at a time;
/// bit-identical to the one-at-a-time process for bit depths up to 10.
fn transform_lanes<const L: usize>(dequant: &[i32], residual: &mut [i32], p: &Tx2d) {
let mut t = [[0i32; L]; 64];
lanes_row_pass(dequant, residual, p, &mut t);
lanes_col_pass(residual, p, &mut t);
}
fn lanes_row_pass<const L: usize>(dequant: &[i32], residual: &mut [i32], p: &Tx2d, t: &mut [[i32; L]; 64]) {
let (w, h) = (p.w, p.h);
let in_w = w.min(32);
let lo = -(1i32 << (p.col_clamp - 1));
let hi = (1i32 << (p.col_clamp - 1)) - 1;
let mut i0 = 0;
while i0 < h {
// Rows without coefficients (and rows 32..63) transform to zeros.
if i0 >= 32 || (i0..i0 + L).all(|i| dequant[i * 64..i * 64 + in_w].iter().all(|&v| v == 0)) {
residual[i0 * w..(i0 + L) * w].fill(0);
i0 += L;
continue;
}
for (j, tj) in t[..w].iter_mut().enumerate() {
for l in 0..L {
tj[l] = if j < in_w { dequant[(i0 + l) * 64 + j] } else { 0 };
}
}
if p.rect2 {
for v in t[..w].iter_mut() {
*v = v.mul_round12(2896);
}
}
transform_1d(&mut t[..], p.rk, p.log2w, p.row_clamp);
for l in 0..L {
let out = &mut residual[(i0 + l) * w..(i0 + l + 1) * w];
for j in 0..w {
out[j] = round2(t[j][l], p.row_shift).clamp(lo, hi);
}
}
i0 += L;
}
}
fn lanes_col_pass<const L: usize>(residual: &mut [i32], p: &Tx2d, t: &mut [[i32; L]; 64]) {
let (w, h) = (p.w, p.h);
let mut j0 = 0;
while j0 < w {
for i in 0..h {
t[i].copy_from_slice(&residual[i * w + j0..i * w + j0 + L]);
}
transform_1d(&mut t[..], p.ck, p.log2h, p.col_clamp);
for i in 0..h {
let out = &mut residual[i * w + j0..i * w + j0 + L];
for l in 0..L {
out[l] = round2(t[i][l], p.col_shift);
}
}
j0 += L;
}
}
#[cfg(test)]
mod tests {
use super::*;
/// The DCT step list must implement an orthogonal transform: a DC-only input gives a flat
/// output equal to DC * cos(pi/4) (in the 2^12 fixed-point scale).
#[test]
fn dct_dc_is_flat() {
for n in 2..=6 {
let len = 1 << n;
let mut t = [0i32; 64];
t[0] = 4096;
inverse_dct(&mut t, n, 24);
for v in &t[..len] {
assert_eq!(*v, 2896, "n={n}: {:?}", &t[..len]);
}
}
}
/// Compare each inverse DCT basis vector with the real-valued DCT-III.
#[test]
fn dct_matches_float() {
for n in 2..=6u32 {
let len = 1usize << n;
for k in 0..len {
let mut t = [0i32; 64];
t[k] = 1 << 12;
inverse_dct(&mut t, n, 24);
for x in 0..len {
let c = if k == 0 { std::f64::consts::FRAC_1_SQRT_2 } else { 1.0 };
let want = 4096.0 * c * ((2 * x + 1) as f64 * k as f64 * std::f64::consts::PI / (2 * len) as f64).cos();
assert!((t[x] as f64 - want).abs() < 4.0 + 0.5 * n as f64, "n={n} k={k} x={x}: {} vs {want}", t[x]);
}
}
}
}
/// ADST8/16 basis vectors follow sin(pi (2i+1)(2k+1) / 4N) (scaled like the DCT).
#[test]
fn adst_basis_matches_sine() {
for n in 3..=4u32 {
let len = 1usize << n;
for k in 0..len {
let mut t = [0i32; 64];
t[k] = 1 << 12;
inverse_adst(&mut t, n, 24);
for i in 0..len {
let want = 4096.0 * ((std::f64::consts::PI * (2 * i + 1) as f64 * (2 * k + 1) as f64) / (4 * len) as f64).sin();
assert!((t[i] as f64 - want).abs() < 8.0, "n={n} k={k} i={i}: {} vs {want:.1}", t[i]);
}
}
}
}
#[test]
fn adst_is_close_to_sine_transform() {
for n in 3..=4u32 {
let len = 1usize << n;
for k in 0..len {
let mut t = [0i32; 64];
t[k] = 1 << 12;
inverse_adst(&mut t, n, 24);
let energy: f64 = t[..len].iter().map(|&v| (v as f64 / 4096.0).powi(2)).sum();
// like the DCT, the AV1 ADST is scaled by sqrt(N / 2)
assert!((energy - len as f64 / 2.0).abs() < 0.02, "n={n} k={k} energy {energy}");
}
}
}
}
-12
View File
@@ -1,12 +0,0 @@
[package]
name = "deckcraft-bitstream"
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
[lints]
workspace = true
[dependencies]
craft-media = { workspace = true, features = ["bitstream"] }
-82
View File
@@ -1,82 +0,0 @@
//! Memory budget for decoded pictures, shared by the video decoders.
//!
//! A per-axis size cap does not bound memory: a decoder holds the current picture, every
//! reference picture and the pictures still being decoded on worker threads. Each decoder checks
//! the whole picture store against [`DECODE_MEMORY_BUDGET`] with checked arithmetic before it
//! allocates for a new picture size, so a hostile header is an error, not an out-of-memory abort.
/// Bytes the decoded pictures of one decoder may take: 4 GiB on 64-bit targets, 1 GiB on 32-bit
/// ones (wasm32 has a 4 GiB address space in all).
pub const DECODE_MEMORY_BUDGET: u64 = if cfg!(target_pointer_width = "64") { 4 << 30 } else { 1 << 30 };
/// The shape of one decoded picture as a decoder allocates it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PictureShape {
/// Allocated luma samples per row and rows (padding included).
pub width: u64,
pub height: u64,
/// Chroma subsampling shifts (0 or 1); ignored when `chroma_planes` is 0.
pub shift_x: u32,
pub shift_y: u32,
/// Chroma planes (0 for monochrome, 2 otherwise).
pub chroma_planes: u64,
/// Bytes per sample (1 for 8-bit buffers, 2 for 16-bit ones).
pub bytes_per_sample: u64,
}
impl PictureShape {
/// Bytes of one picture, `None` if it does not fit in a `u64`.
pub fn bytes(&self) -> Option<u64> {
let luma = self.width.checked_mul(self.height)?;
let cw = self.width.div_ceil(1 << self.shift_x.min(1));
let ch = self.height.div_ceil(1 << self.shift_y.min(1));
let chroma = cw.checked_mul(ch)?.checked_mul(self.chroma_planes)?;
luma.checked_add(chroma)?.checked_mul(self.bytes_per_sample)
}
/// Bytes of `pictures` such pictures, `None` on overflow.
pub fn store_bytes(&self, pictures: u64) -> Option<u64> {
self.bytes()?.checked_mul(pictures)
}
/// Whether `pictures` such pictures fit in `budget` bytes.
pub fn fits(&self, pictures: u64, budget: u64) -> bool {
self.store_bytes(pictures).is_some_and(|b| b <= budget)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn shape(w: u64, h: u64) -> PictureShape {
PictureShape { width: w, height: h, shift_x: 1, shift_y: 1, chroma_planes: 2, bytes_per_sample: 2 }
}
#[test]
fn bytes_count_luma_and_subsampled_chroma() {
assert_eq!(shape(4, 4).bytes(), Some((16 + 2 * 4) * 2));
// Odd sizes round chroma up.
assert_eq!(shape(3, 3).bytes(), Some((9 + 2 * 4) * 2));
let mono = PictureShape { chroma_planes: 0, bytes_per_sample: 1, ..shape(10, 10) };
assert_eq!(mono.bytes(), Some(100));
let full = PictureShape { shift_x: 0, shift_y: 0, ..shape(10, 10) };
assert_eq!(full.bytes(), Some(600));
}
#[test]
fn exact_budget_boundary() {
let s = shape(4, 4); // 48 bytes
assert!(s.fits(10, 480));
assert!(!s.fits(10, 479));
assert!(!s.fits(11, 480));
}
#[test]
fn overflow_is_never_within_budget() {
let huge = shape(u64::MAX / 2, 3);
assert_eq!(huge.bytes(), None);
assert!(!huge.fits(1, u64::MAX));
assert!(!shape(1 << 31, 1 << 31).fits(u64::MAX, u64::MAX));
}
}
-14
View File
@@ -1,14 +0,0 @@
//! Bit-level reading and writing for DeckCraft's codec crates: a re-export of
//! [`craft_media::bitstream`] from craft-libs, plus [`budget`].
//!
//! - [`BitReader`]: MSB-first reader with Exp-Golomb (`ue(v)`, `se(v)`), as used by H.264/HEVC/AV1 headers.
//! - [`BitWriter`]: the matching writer (for encoders and header rewriting).
//! - [`unescape_rbsp`] / [`escape_rbsp`]: remove/insert H.264/HEVC emulation-prevention bytes.
//! - [`annexb_nals`] / [`length_prefixed_nals`]: split Annex-B byte streams and AVCC/HVCC samples into NAL units.
//! - [`budget`]: the memory budget video decoders check their picture store against.
#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))]
pub use craft_media::bitstream::*;
pub mod budget;
+11 -7
View File
@@ -466,22 +466,26 @@ fn ctx_xfrm(doc: &deckcraft_model::Presentation, sel: &crate::Selection, shape:
/// Run `f` on each target shape (copy-on-write), materialising inherited boxes first.
pub(crate) fn edit_shapes(s: &mut Session, p: &Value, cmd: &str, f: impl Fn(&mut deckcraft_model::Shape) -> Result<()>) -> Result<Value> {
let ids = targets(s, p)?;
let mut ids = targets(s, p)?;
if ids.is_empty() {
return Err(bad(cmd, "no shapes selected"));
}
// A shape named twice is edited once.
let mut seen = std::collections::HashSet::new();
ids.retain(|id| seen.insert(*id));
s.edit(|doc, sel| {
let boxes: Vec<(ShapeId, Xfrm)> = {
let st_shapes: Vec<deckcraft_model::Shape> =
ids.iter().filter_map(|id| current_shapes(doc, sel).and_then(|v| deckcraft_model::find_shape(v, *id)).cloned()).collect();
st_shapes.iter().map(|sh| (sh.id, xfrm_of(doc, sel, sh))).collect()
};
// The boxes are read from the shapes in place: no copy of a shape (and its text) per id.
let boxes: std::collections::HashMap<ShapeId, Xfrm> = ids
.iter()
.filter_map(|id| current_shapes(doc, sel).and_then(|v| deckcraft_model::find_shape(v, *id)))
.map(|sh| (sh.id, xfrm_of(doc, sel, sh)))
.collect();
let shapes = crate::shapes_mut(doc, sel).ok_or_else(|| bad(cmd, "no slide"))?;
let mut n = 0;
for id in &ids {
if let Some(sh) = deckcraft_model::find_shape_mut(shapes, *id) {
if sh.xfrm.is_none() {
sh.xfrm = boxes.iter().find(|(i, _)| i == id).map(|(_, x)| *x);
sh.xfrm = boxes.get(id).copied();
}
f(sh)?;
n += 1;
+32
View File
@@ -365,6 +365,38 @@ fn ungroup(s: &mut Session, p: &Value) -> Result<Value> {
out.push(sh);
}
*list = out;
// A group inside a group: its children take its place in the outer group, mapped into the
// outer group's child space, where the inner group's own box is.
for id in &ids {
let Some(siblings) = deckcraft_model::sibling_list_mut(list, *id) else { continue };
let Some(at) = siblings.iter().position(|x| x.id == *id) else { continue };
let freed: Vec<Shape> = match siblings.get(at) {
Some(Shape { kind: ShapeKind::Group { children, child }, xfrm: Some(gx), .. }) => children
.iter()
.map(|c| {
let mut c = c.clone();
if let Some(cx) = c.xfrm {
c.xfrm = Some(child_to_parent(gx, child, &cx));
}
c
})
.collect(),
_ => continue,
};
let freed_ids: Vec<ShapeId> = freed.iter().map(|c| c.id).collect();
siblings.splice(at..=at, freed);
// Ungrouped along with an outer group, it was listed among that group's children (once per expanded ancestor).
match new_sel.iter().position(|x| x == id) {
Some(i) => {
new_sel.splice(i..=i, freed_ids);
new_sel.retain(|x| x != id);
}
None => new_sel.extend(freed_ids),
}
}
// A shape freed from a named group and then from a named group around it is listed once.
let mut listed = std::collections::HashSet::new();
new_sel.retain(|x| listed.insert(*x));
sel.shapes = new_sel.clone();
Ok(json!({"ids": new_sel}))
})
+91 -6
View File
@@ -120,6 +120,12 @@ fn copy_inner(s: &mut Session, p: &Value) -> Result<String> {
})
})
.collect();
// A shape in a group is copied with the box it is drawn in, so it pastes where it was seen.
for sh in &mut shapes {
if let Some(x) = sh.xfrm.and_then(|x| box_on_slide(&st.doc, &st.selection, st.shapes(), sh.id, &x)) {
sh.xfrm = Some(x);
}
}
// Copying is the last chance to read what a placeholder inherits: a cut removes it, and the
// paste can be in another deck. Slide Master view pastes the originals instead.
let placeholders: Vec<Shape> = shapes.iter().filter(|x| x.ph.is_some()).cloned().collect();
@@ -147,6 +153,39 @@ pub(crate) fn detach_on_slide(doc: &deckcraft_model::Presentation, sel: &crate::
}
}
/// Where shape `id` is drawn on the slide when it is inside a group: `x`, its box in its group's
/// space, mapped out through each group around it, as Ungroup places a freed shape. `None` for a
/// shape on the top level or not found. A turned shape in a group stretched more one way than the
/// other is drawn skewed, which a box cannot hold, so it gets the unskewed box Ungroup gives it.
fn box_on_slide(
doc: &deckcraft_model::Presentation,
sel: &crate::Selection,
shapes: &[Shape],
id: ShapeId,
x: &deckcraft_geom::Xfrm,
) -> Option<deckcraft_geom::Xfrm> {
fn rec(
doc: &deckcraft_model::Presentation,
sel: &crate::Selection,
shapes: &[Shape],
id: ShapeId,
x: &deckcraft_geom::Xfrm,
depth: usize,
) -> Option<deckcraft_geom::Xfrm> {
if depth > 64 {
return None;
}
shapes.iter().find_map(|sh| {
let deckcraft_model::ShapeKind::Group { children, child } = &sh.kind else {
return None;
};
let inner = if children.iter().any(|c| c.id == id) { *x } else { rec(doc, sel, children, id, x, depth + 1)? };
Some(super::arrange::child_to_parent(&xfrm_of(doc, sel, sh), child, &inner))
})
}
rec(doc, sel, shapes, id, x, 0)
}
fn copying_caret(s: &Session) -> bool {
s.active().is_some_and(|d| d.selection.text.as_ref().is_some_and(|t| !t.is_range()))
}
@@ -559,6 +598,16 @@ fn duplicate(s: &mut Session, p: &Value) -> Result<Value> {
let (new_ids, made) = s.edit(|doc, sel| {
let list = super::current_shapes(doc, sel).cloned().unwrap_or_default();
let mut copies: Vec<Shape> = list.iter().filter(|x| ids.contains(&x.id)).cloned().collect();
// A shape inside a group is copied into that group, with the map from the group's space onto
// the slide so that its copy is offset on the slide as a top-level copy is.
let top = copies.len();
let mut to_slide = vec![None; top];
for id in grouped_targets(&list, &ids) {
if let Some((m, a)) = crate::connect::locate(doc, sel, &list, id) {
copies.push(m.clone());
to_slide.push(Some(a));
}
}
let sources: Vec<ShapeId> = copies.iter().map(|c| c.id).collect();
let boxes: Vec<_> = copies.iter().map(|c| xfrm_of(doc, sel, c)).collect();
detach_on_slide(doc, sel, &mut copies);
@@ -570,10 +619,12 @@ fn duplicate(s: &mut Session, p: &Value) -> Result<Value> {
})
.and_then(|(_, _, pairs)| {
let (src, copy) = pairs.first()?;
let a = xfrm_of(doc, sel, list.iter().find(|x| x.id == *src)?);
let b = xfrm_of(doc, sel, list.iter().find(|x| x.id == *copy)?);
let d = (b.x - a.x, b.y - a.y);
(d.0.is_finite() && d.1.is_finite()).then_some(d)
// Measured on the slide, so the copy of a shape in a group repeats its drawn move.
let (a, to_a) = crate::connect::locate(doc, sel, &list, *src)?;
let (b, to_b) = crate::connect::locate(doc, sel, &list, *copy)?;
let (a, b) = (xfrm_of(doc, sel, a), xfrm_of(doc, sel, b));
let d = to_b * deckcraft_geom::Point::new(b.x, b.y) - to_a * deckcraft_geom::Point::new(a.x, a.y);
(d.x.is_finite() && d.y.is_finite()).then_some((d.x, d.y))
})
.unwrap_or((off, off));
reid("edit.duplicate", doc, &mut copies)?;
@@ -588,7 +639,8 @@ fn duplicate(s: &mut Session, p: &Value) -> Result<Value> {
rename_automatic(c, &mut taken, count);
count += 1;
});
for (c, b) in copies.iter_mut().zip(boxes) {
for ((c, b), to) in copies.iter_mut().zip(boxes).zip(&to_slide) {
let (dx, dy) = to.map_or((dx, dy), |a| offset_in(a, dx, dy));
let mut x = b;
x.x += dx;
x.y += dy;
@@ -599,7 +651,12 @@ fn duplicate(s: &mut Session, p: &Value) -> Result<Value> {
}
let new_ids: Vec<ShapeId> = copies.iter().map(|c| c.id).collect();
let shapes = crate::shapes_mut(doc, sel).ok_or_else(|| bad("edit.duplicate", "no slide"))?;
shapes.extend(copies);
let mut copies = copies.into_iter();
shapes.extend(copies.by_ref().take(top));
// A member's copy goes last among its group's children, as a top-level copy goes last on the slide.
for (c, src) in copies.zip(sources.iter().skip(top)) {
deckcraft_model::sibling_list_mut(shapes, *src).ok_or_else(|| bad("edit.duplicate", "no such shape"))?.push(c);
}
sel.shapes = new_ids.clone();
let made = (sel.slide, sel.target, sources.into_iter().zip(new_ids.iter().copied()).collect());
Ok((new_ids, made))
@@ -608,6 +665,34 @@ fn duplicate(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({"ids": new_ids}))
}
/// The shapes in `ids` that are inside groups, in drawing order, without those in a group that is
/// in `ids` too: they are copied along with it.
fn grouped_targets(list: &[Shape], ids: &[ShapeId]) -> Vec<ShapeId> {
fn rec(shapes: &[Shape], ids: &[ShapeId], nested: bool, out: &mut Vec<ShapeId>, depth: usize) {
if depth > 64 {
return;
}
for sh in shapes {
if !ids.contains(&sh.id) {
rec(sh.children(), ids, true, out, depth + 1);
} else if nested {
out.push(sh.id);
}
}
}
let mut out = vec![];
rec(list, ids, false, &mut out, 0);
out
}
/// The offset in the space `to_slide` maps onto the slide that moves a shape by (`dx`, `dy`) on
/// the slide. A group with no width or height draws nothing, and the offset stays as it is.
fn offset_in(to_slide: deckcraft_geom::Affine, dx: f64, dy: f64) -> (f64, f64) {
let back = to_slide.inverse();
let v = back * deckcraft_geom::Point::new(dx, dy) - back * deckcraft_geom::Point::ZERO;
if v.x.is_finite() && v.y.is_finite() { (v.x, v.y) } else { (dx, dy) }
}
fn delete(s: &mut Session, p: &Value) -> Result<Value> {
if s.doc()?.selection.text.is_some() && ids_param(p, "ids").is_none() {
return super::text::delete_dir(s, "forward");
+3 -3
View File
@@ -70,7 +70,7 @@ pub fn specs() -> Vec<CommandSpec> {
cmd!(
"format.textTransparency",
"Text Transparency",
["Shape Format", "WordArt Styles"],
["Shape Format", "Decorative Text Styles"],
None,
"{value: 0..1} — 0 opaque, 1 invisible; keeps the text's colour (inherited colours included)",
has_text_or_shapes,
@@ -128,7 +128,7 @@ pub fn specs() -> Vec<CommandSpec> {
cmd!(
"format.textOutline",
"Text Outline",
["Shape Format", "WordArt Styles"],
["Shape Format", "Decorative Text Styles"],
None,
"{color?, width?: pt} (no color = none)",
has_text_or_shapes,
@@ -137,7 +137,7 @@ pub fn specs() -> Vec<CommandSpec> {
cmd!(
"format.textWarp",
"Text Transform",
["Shape Format", "WordArt Styles"],
["Shape Format", "Decorative Text Styles"],
None,
"{preset: a prstTxWarp name or none, adj?: [finite raw guide values, at most 8]} — none clears the transform; omitted adj uses the preset's defaults",
has_text_or_shapes,
+9 -2
View File
@@ -55,12 +55,18 @@ pub(super) fn size(s: &mut Session, p: &Value) -> Result<Value> {
format_run(s, &move |r| r.size = Some(v))
}
/// PowerPoint's font size list (grow/shrink steps through it).
pub const SIZES: [f64; 20] = [8.0, 9.0, 10.0, 10.5, 11.0, 12.0, 14.0, 16.0, 18.0, 20.0, 24.0, 28.0, 32.0, 36.0, 40.0, 44.0, 48.0, 54.0, 60.0, 66.0];
/// PowerPoint's font size list, as the Font Size box lists it (grow/shrink steps through it).
pub const SIZES: [f64; 24] =
[8.0, 9.0, 10.0, 10.5, 11.0, 12.0, 14.0, 16.0, 18.0, 20.0, 24.0, 28.0, 32.0, 36.0, 40.0, 44.0, 48.0, 54.0, 60.0, 66.0, 72.0, 80.0, 88.0, 96.0];
/// Grow steps up the list, then 8 points at a time past its top; shrink steps back down the same
/// way. A size past the top used to shrink straight to 66 (#2059).
fn step(cur: f64, up: bool) -> f64 {
let top = SIZES[SIZES.len() - 1];
if up {
SIZES.iter().copied().find(|v| *v > cur + 0.01).unwrap_or((cur + 8.0).min(4000.0))
} else if cur - 0.01 > top {
(cur - 8.0).max(top)
} else {
SIZES.iter().rev().copied().find(|v| *v < cur - 0.01).unwrap_or((cur - 1.0).max(1.0))
}
@@ -108,6 +114,7 @@ pub(super) fn color(s: &mut Session, p: &Value) -> Result<Value> {
edit_shapes(s, &json!({"ids": charts}), "format.color", |sh| {
if let ShapeKind::Chart(ch) = &mut sh.kind {
ch.text_color = Some(tc.clone());
ch.legend_entries.iter_mut().for_each(|e| e.color = None);
ch.raw = None;
}
Ok(())
+2 -2
View File
@@ -91,7 +91,7 @@ pub fn specs() -> Vec<CommandSpec> {
),
cmd!("insert.audio", "Audio from File…", ["Insert", "Media", "Audio"], None, "{path? | data: base64, name?, rect?}", has_slide, audio),
cmd!("insert.video", "Video from File…", ["Insert", "Media", "Video"], None, "{path? | data: base64, name?, rect?}", has_slide, video),
cmd!("insert.wordArt", "WordArt", ["Insert", "Text"], None, "{text?, style?: 0..}", has_slide, word_art),
cmd!("insert.wordArt", "Decorative Text", ["Insert", "Text"], None, "{text?, style?: 0..}", has_slide, word_art),
cmd!("insert.slideNumber", "Slide Number", ["Insert", "Text"], None, "{}", has_slide, slide_number),
cmd!(
"insert.dateTime",
@@ -142,7 +142,7 @@ pub fn specs() -> Vec<CommandSpec> {
),
cmd!(
"insert.smartArt",
"SmartArt",
"Diagram",
["Insert", "Illustrations"],
None,
"{kind: list|process|cycle|hierarchy|pyramid|matrix, items?: [text] (leading tabs = sub-points), nodes?: [{text, level}]} → {id}",
+144 -6
View File
@@ -80,6 +80,12 @@ pub(super) fn photo_album_edit(s: &mut Session, p: &Value) -> Result<Value> {
let opts = album_opts(p)?;
let items = album_items(p)?;
let (id, ids) = album_target(s, p)?;
// The editor is seeded within the album limits; replacing the album from a trimmed seed would
// delete what it left out, so refuse rather than lose it.
let st = s.doc()?;
if items_from_slides(&st.doc, &album_slides(st, &ids)).1 {
return Err(bad("insert.photoAlbumEdit", "this album holds more than the editor can carry; editing it would delete the rest"));
}
commit_album(s, items, opts, Some((id, ids)))
}
@@ -545,6 +551,19 @@ fn album_target(s: &Session, p: &Value) -> Result<(u32, Vec<u64>)> {
Err(bad("insert.photoAlbumEdit", "this slide is not part of a photo album"))
}
/// Whether a slide holds anything besides album items: shapes, notes, comments, a background, a
/// transition, animations, a colour map or tags.
fn has_slide_content(s: &deckcraft_model::Slide) -> bool {
!s.shapes.is_empty()
|| !s.notes.text().trim().is_empty()
|| !s.comments.is_empty()
|| s.background.is_some()
|| s.transition.is_some()
|| !s.animations.is_empty()
|| s.color_map.is_some()
|| !s.tags.is_empty()
}
fn pic_count(slide: &deckcraft_model::Slide) -> usize {
slide.shapes.iter().filter(|sh| matches!(sh.kind, ShapeKind::Picture { .. })).count()
}
@@ -592,9 +611,16 @@ fn infer_album(slides: &[&deckcraft_model::Slide], theme_now: &str) -> (String,
(layout, frame.into(), captions, any && gray, theme_now.to_string())
}
fn items_from_slides(doc: &deckcraft_model::Presentation, slides: &[&deckcraft_model::Slide]) -> Vec<Value> {
/// The album's items for the dialog, within the limits `insert.photoAlbum` itself takes (items,
/// bytes per picture and in all), checked before a picture is encoded: an album slide set can
/// name one large picture many times.
///
/// The flag is true when something was left out for a limit.
fn items_from_slides(doc: &deckcraft_model::Presentation, slides: &[&deckcraft_model::Slide]) -> (Vec<Value>, bool) {
let mut items = Vec::new();
for slide in slides {
let mut total = 0usize;
let mut omitted = false;
'slides: for slide in slides {
let pics: Vec<&deckcraft_model::Shape> = slide.shapes.iter().filter(|sh| matches!(sh.kind, ShapeKind::Picture { .. })).collect();
if pics.is_empty() {
let text = slide
@@ -605,13 +631,27 @@ fn items_from_slides(doc: &deckcraft_model::Presentation, slides: &[&deckcraft_m
.find(|t| !t.trim().is_empty())
.unwrap_or_default();
if !text.trim().is_empty() {
if items.len() >= ALBUM_MAX_ITEMS {
omitted = true;
break 'slides;
}
items.push(json!({"text": text.trim()}));
} else if items.len() >= ALBUM_MAX_ITEMS && has_slide_content(slide) {
// Past the item limit, a slide with other content would be deleted by a replace.
omitted = true;
break 'slides;
}
continue;
}
for sh in pics {
let ShapeKind::Picture { fill } = &sh.kind else { continue };
let media = doc.media(fill.media);
let size = media.map_or(0, |m| m.data.len());
if items.len() >= ALBUM_MAX_ITEMS || size > ALBUM_MAX_BYTES || total.saturating_add(size) > ALBUM_MAX_TOTAL {
omitted = true;
break 'slides;
}
total += size;
let name = if !sh.descr.is_empty() {
sh.descr.clone()
} else {
@@ -626,14 +666,19 @@ fn items_from_slides(doc: &deckcraft_model::Presentation, slides: &[&deckcraft_m
}));
}
}
items
(items, omitted)
}
/// The slides of album `ids` as the editor would seed them.
fn album_slides<'a>(st: &'a crate::DocState, ids: &[u64]) -> Vec<&'a deckcraft_model::Slide> {
ids.iter().filter_map(|sid| u32::try_from(*sid).ok()).filter_map(|n| st.doc.slide(deckcraft_model::SlideId(n))).collect()
}
fn album_seed(s: &Session) -> Result<Value> {
let (id, ids) = album_target(s, &Value::Null)?;
let st = s.doc()?;
let slides: Vec<&deckcraft_model::Slide> =
ids.iter().filter_map(|sid| u32::try_from(*sid).ok()).filter_map(|n| st.doc.slide(deckcraft_model::SlideId(n))).collect();
let slides = album_slides(st, &ids);
let (items, _) = items_from_slides(&st.doc, &slides);
let theme_now = st.doc.masters.first().map(|m| m.theme.name.clone()).filter(|n| !n.is_empty()).unwrap_or_else(|| "DeckCraft".into());
let inferred = infer_album(&slides, &theme_now);
let mark = slides.first().and_then(|sl| sl.raw_ext.as_deref().and_then(parse_album_mark));
@@ -655,7 +700,7 @@ fn album_seed(s: &Session) -> Result<Value> {
"captions": captions,
"grayscale": grayscale,
"theme": theme,
"items": items_from_slides(&st.doc, &slides),
"items": items,
}))
}
@@ -722,3 +767,96 @@ fn insert_album(s: &mut Session, items: Vec<AlbumItem>, opts: &AlbumOpts, at: Op
s.execute("slide.go", &json!({"index": first}))?;
Ok(json!({"slides": slides, "first": first, "pictures": pictures, "album": album_id}))
}
#[cfg(test)]
mod seed_tests {
use std::sync::Arc;
use deckcraft_model::style::PictureFill;
use deckcraft_model::{Shape, ShapeId, defaults};
use super::*;
#[test]
fn reopening_an_album_seeds_at_most_the_items_an_album_takes() {
// One picture named by 500 shapes: the dialog gets 48 items, not 500 encoded copies.
let mut p = defaults::new_presentation(None);
let img = p.add_media("a.png", "image/png", vec![1; 64 * 1024]).unwrap();
let pic = |k: u32| Shape {
id: ShapeId(10_000 + k),
kind: ShapeKind::Picture { fill: PictureFill { media: img, ..Default::default() } },
..Default::default()
};
Arc::make_mut(&mut p.slides[0]).shapes = (0..500).map(pic).collect();
let slides: Vec<&deckcraft_model::Slide> = p.slides.iter().map(|s| s.as_ref()).collect();
let (items, omitted) = items_from_slides(&p, &slides);
assert_eq!(items.len(), ALBUM_MAX_ITEMS);
assert!(omitted);
let few: Vec<&deckcraft_model::Slide> = Vec::new();
assert!(!items_from_slides(&p, &few).1);
}
/// An album too big for the editor's seed cannot be replaced from that seed: the replace would
/// delete what the seed left out.
#[test]
fn editing_an_album_the_editor_cannot_hold_is_refused() {
let mut s = crate::Session::new();
s.execute("file.new", &json!({})).unwrap();
let texts: Vec<Value> = (0..ALBUM_MAX_ITEMS).map(|i| json!({"text": format!("t{i}")})).collect();
s.execute("insert.photoAlbum", &json!({"pictures": texts, "layout": "1"})).unwrap();
let st = s.doc_mut().unwrap();
let doc = Arc::make_mut(&mut st.doc);
let mut extra = doc.slides.last().unwrap().as_ref().clone();
extra.id = deckcraft_model::SlideId(doc.alloc_id().unwrap());
doc.slides.push(Arc::new(extra));
let n = doc.slides.len();
let e = s.execute("insert.photoAlbumEdit", &json!({"pictures": [{"text": "x"}], "layout": "1"})).unwrap_err().to_string();
assert!(e.contains("delete the rest"), "{e}");
assert_eq!(s.doc().unwrap().doc.slides.len(), n);
}
/// Past the item limit, a slide holding only a drawing counts as content the replace would delete.
#[test]
fn editing_an_album_with_a_drawing_slide_past_the_limit_is_refused() {
let mut s = crate::Session::new();
s.execute("file.new", &json!({})).unwrap();
let texts: Vec<Value> = (0..ALBUM_MAX_ITEMS).map(|i| json!({"text": format!("t{i}")})).collect();
s.execute("insert.photoAlbum", &json!({"pictures": texts, "layout": "1"})).unwrap();
let st = s.doc_mut().unwrap();
let doc = Arc::make_mut(&mut st.doc);
let mut extra = doc.slides.last().unwrap().as_ref().clone();
extra.id = deckcraft_model::SlideId(doc.alloc_id().unwrap());
extra.shapes = vec![Shape { id: ShapeId(90_000), ..Default::default() }];
doc.slides.push(Arc::new(extra));
let e = s.execute("insert.photoAlbumEdit", &json!({"pictures": [{"text": "x"}], "layout": "1"})).unwrap_err().to_string();
assert!(e.contains("delete the rest"), "{e}");
}
/// Notes or comments on a shapeless slide past the limit are content a replace would delete.
#[test]
fn editing_an_album_with_a_notes_only_slide_past_the_limit_is_refused() {
let mut s = crate::Session::new();
s.execute("file.new", &json!({})).unwrap();
let texts: Vec<Value> = (0..ALBUM_MAX_ITEMS).map(|i| json!({"text": format!("t{i}")})).collect();
s.execute("insert.photoAlbum", &json!({"pictures": texts, "layout": "1"})).unwrap();
let st = s.doc_mut().unwrap();
let doc = Arc::make_mut(&mut st.doc);
let mut extra = doc.slides.last().unwrap().as_ref().clone();
extra.id = deckcraft_model::SlideId(doc.alloc_id().unwrap());
extra.shapes.clear();
extra.notes = deckcraft_model::text::TextBody::from_text("speaker notes");
doc.slides.push(Arc::new(extra));
let e = s.execute("insert.photoAlbumEdit", &json!({"pictures": [{"text": "x"}], "layout": "1"})).unwrap_err().to_string();
assert!(e.contains("delete the rest"), "{e}");
}
/// A shapeless slide with a background, transition, colour map or tags counts as content; a plain empty one does not.
#[test]
fn slide_level_settings_count_as_slide_content() {
use deckcraft_model::Slide;
assert!(!has_slide_content(&Slide::default()));
assert!(has_slide_content(&Slide { tags: vec![("k".into(), "v".into())], ..Default::default() }));
assert!(has_slide_content(&Slide { color_map: Some(vec![("bg1".into(), "lt1".into())]), ..Default::default() }));
assert!(has_slide_content(&Slide { transition: Some(Default::default()), ..Default::default() }));
}
}
+1
View File
@@ -190,6 +190,7 @@ pub(super) fn chart_options(s: &mut Session, p: &Value) -> Result<Value> {
with_chart(s, p, "chart.options", |c| {
if let Some(t) = &text_color {
c.text_color = t.clone();
c.legend_entries.iter_mut().for_each(|e| e.color = None);
}
apply_bin_options(c, bin_count, bin_size);
apply_subtotals_and_bars(c, p, subtotals.as_deref());
+54 -2
View File
@@ -24,12 +24,24 @@ pub fn specs() -> Vec<CommandSpec> {
"Merge Shapes",
["Shape Format", "Insert Shapes", "Merge Shapes"],
None,
"{op: union|combine|fragment|intersect|subtract, ids?: shapes in selection order (the first one's formatting is kept)} → {ids}",
has_two,
"{op: union|combine|fragment|intersect|subtract, ids?: shapes in selection order (the first one's formatting is kept)} → {ids} (a shape inside a group is refused; ungroup it first)",
can_merge,
merge
)]
}
/// Why Merge Shapes is unavailable while a shape inside a group is selected.
pub(crate) const IN_GROUP: &str = "a shape in a group can't be merged; ungroup it first";
/// Two or more shapes selected, none of them inside a group: the result replaces its sources in
/// the slide's top-level list, so a member would be drawn outside its group's scale and offset.
fn can_merge(s: &Session) -> std::result::Result<(), String> {
has_two(s)?;
let in_group =
s.active().is_some_and(|st| st.selection.shapes.iter().any(|id| st.shape(*id).is_some() && !st.shapes().iter().any(|t| t.id == *id)));
if in_group { Err(IN_GROUP.into()) } else { Ok(()) }
}
/// The filled outline of `s` in slide points (curves flattened). A text box with no fill and no
/// outline is its letters, as in PowerPoint (merging text with a picture fills the letters).
fn area_of(doc: &deckcraft_model::Presentation, sel: &crate::Selection, s: &Shape) -> Area {
@@ -197,6 +209,10 @@ fn merge(s: &mut Session, p: &Value) -> Result<Value> {
let ids = targets(s, p)?;
let st = s.doc()?;
let shapes: Vec<Shape> = ids.iter().filter_map(|id| st.shape(*id).cloned()).collect();
if let Some(member) = shapes.iter().find(|x| !st.shapes().iter().any(|t| t.id == x.id)) {
let name = if member.name.is_empty() { format!("shape {}", member.id.0) } else { format!("`{}`", member.name) };
return Err(bad("shape.merge", format!("{name} is in a group; ungroup it first to merge it")));
}
let mergeable = |x: &Shape| matches!(x.kind, ShapeKind::Shape | ShapeKind::Picture { .. }) && !x.is_line();
if shapes.len() < 2 || !shapes.iter().all(mergeable) {
return Err(bad("shape.merge", "select two or more shapes (not lines, groups, tables or charts)"));
@@ -379,4 +395,40 @@ mod tests {
assert_eq!(animations[1].trigger.map(|id| id.0), Some(first));
}
}
/// Merge Shapes refuses a shape inside a group, naming it, and is unavailable while one is
/// selected. Before, the result went to the slide's top level with the member's box from the
/// group's own space, so the shapes of a moved or resized group were redrawn somewhere else.
#[test]
fn a_shape_in_a_group_is_refused() {
fn rect(s: &mut Session, x: f64) -> u64 {
s.execute("shape.insert", &json!({"preset": "rect", "rect": [x, 100, 100, 100]})).unwrap()["id"].as_u64().unwrap()
}
let mut s = Session::with_new();
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
let (a, b, c) = (rect(&mut s, 100.0), rect(&mut s, 150.0), rect(&mut s, 500.0));
let g = s.execute("arrange.group", &json!({"ids": [a, b]})).unwrap()["id"].as_u64().unwrap();
s.execute("shape.move", &json!({"ids": [g], "dx": 300})).unwrap();
let before = s.doc().unwrap().current_slide().unwrap().shapes.clone();
let name = s.doc().unwrap().shape(ShapeId(a as u32)).unwrap().name.clone();
for ids in [json!([a, b]), json!([c, a])] {
let err = s.execute("shape.merge", &json!({"op": "union", "ids": ids})).unwrap_err().to_string();
assert!(err.contains(&format!("`{name}` is in a group; ungroup it first")), "{ids}: {err}");
}
assert_eq!(s.doc().unwrap().current_slide().unwrap().shapes, before, "a refused merge leaves the slide as it was");
let spec = crate::cmd::find_command("shape.merge").unwrap();
s.execute("edit.select", &json!({"ids": [a, c]})).unwrap();
assert_eq!((spec.enabled)(&s), Err("a shape in a group can't be merged; ungroup it first".to_string()));
assert!(s.execute("shape.merge", &json!({"op": "union"})).is_err());
// Named top-level shapes don't depend on the selection.
let d = rect(&mut s, 550.0);
s.execute("edit.select", &json!({"ids": [a, b]})).unwrap();
assert_eq!(s.execute("shape.merge", &json!({"op": "union", "ids": [c, d]})).unwrap()["ids"].as_array().map(Vec::len), Some(1));
s.execute("arrange.ungroup", &json!({"ids": [g]})).unwrap();
s.execute("edit.select", &json!({"ids": [a, b]})).unwrap();
assert_eq!((spec.enabled)(&s), Ok(()));
assert_eq!(s.execute("shape.merge", &json!({"op": "union"})).unwrap()["ids"].as_array().map(Vec::len), Some(1));
}
}
+8
View File
@@ -135,6 +135,14 @@ fn eye_show_all_and_hide_all() {
assert!(!flags(&s, a).0 && flags(&s, g).0 && flags(&s, e).0);
}
#[test]
fn lock_group_with_its_child_targeted_leaves_child_locked() {
let (mut s, ids) = deck();
let (d, g) = (ids[3], ids[7]);
s.execute("shape.lock", &json!({"ids": [g, d, d]})).unwrap();
assert!(flags(&s, g).1 && flags(&s, d).1);
}
#[test]
fn lock_cascades_through_groups_and_undoes() {
let (mut s, ids) = deck();
+19
View File
@@ -374,6 +374,25 @@ fn visible(s: &mut Session, p: &Value) -> Result<Value> {
/// (a child can still be unlocked on its own afterwards), as PowerPoint does.
fn lock(s: &mut Session, p: &Value) -> Result<Value> {
let v = bool_param(p, "locked");
// A targeted shape inside another targeted group is reached through the group: editing it again
// would toggle it back.
let mut ids = super::targets(s, p)?;
if ids.len() > 1 {
let doc = s.doc()?;
// Walk each distinct group once, however often `ids` repeats it.
let mut once = std::collections::HashSet::new();
ids.retain(|id| once.insert(*id));
let mut inside = std::collections::HashSet::new();
for id in &ids {
if let Some(g) = doc.shape(*id) {
deckcraft_model::walk(g.children(), &mut |c, _| {
inside.insert(c.id);
});
}
}
ids.retain(|id| !inside.contains(id));
}
let p = &json!({"ids": ids, "locked": p.get("locked")});
edit_shapes(s, p, "shape.lock", |sh| {
let to = v.unwrap_or(!sh.locked);
set_locked(sh, to, 0);
+1 -1
View File
@@ -104,7 +104,7 @@ pub(super) fn freeform(s: &mut Session, p: &Value) -> Result<Value> {
a.iter()
.filter_map(|v| Some(deckcraft_geom::Point::new(v.get(0)?.as_f64()?, v.get(1)?.as_f64()?)))
.filter(|q| q.x.is_finite() && q.y.is_finite())
.take(20_000)
.take(crate::tools::MAX_GESTURE_POINTS)
.collect()
})
.unwrap_or_default();
+1 -1
View File
@@ -196,7 +196,7 @@ pub(super) fn flip(s: &mut Session, p: &Value) -> Result<Value> {
let mut graphic = false;
deckcraft_model::walk(std::slice::from_ref(&*sh), &mut |x, _| graphic |= x.diagram.is_some());
if graphic {
return Err(bad("shape.flip", "Flip is not available for a SmartArt graphic; use `smartArt.layout` to change how it is laid out"));
return Err(bad("shape.flip", "Flip is not available for a Diagram graphic; use `smartArt.layout` to change how it is laid out"));
}
if let Some(x) = sh.xfrm.as_mut() {
if v {
+19 -19
View File
@@ -162,7 +162,7 @@ pub fn preview(kind: &str, style: &str, colors: &str, nodes: Option<&[DiagramNod
}
Shape {
id: ShapeId(9_999),
name: "SmartArt preview".into(),
name: "Diagram preview".into(),
xfrm: Some(area),
kind: ShapeKind::Group { children, child: area },
..Default::default()
@@ -178,16 +178,16 @@ pub fn specs() -> Vec<CommandSpec> {
cmd!(
query "smartArt.nodes",
"Text Pane",
["SmartArt Design", "Create Graphic"],
["Diagram Design", "Create Graphic"],
None,
"{id?} → {id, kind, nodes: [{text, level}]} (id: the SmartArt group or one of its shapes; default the selection)",
"{id?} → {id, kind, nodes: [{text, level}]} (id: the Diagram group or one of its shapes; default the selection)",
has_selection,
nodes
),
cmd!(
"smartArt.setNodes",
"Edit SmartArt Text",
["SmartArt Design", "Create Graphic"],
"Edit Diagram Text",
["Diagram Design", "Create Graphic"],
None,
"{id?, nodes?: [{text, level}], lines?: [string] (leading tabs set the level), index?, text?, title?} (index + text edits one node; title: its Name and Title box)",
has_selection,
@@ -196,7 +196,7 @@ pub fn specs() -> Vec<CommandSpec> {
cmd!(
"smartArt.addNode",
"Add Shape",
["SmartArt Design", "Create Graphic"],
["Diagram Design", "Create Graphic"],
None,
"{id?, index?, position?: after|before|below|above|assistant, text?} → {index} (default: after the last node)",
has_selection,
@@ -205,18 +205,18 @@ pub fn specs() -> Vec<CommandSpec> {
cmd!(
"smartArt.removeNode",
"Remove Shape",
["SmartArt Design", "Create Graphic"],
["Diagram Design", "Create Graphic"],
None,
"{id?, index} (its sub-points move up one level)",
has_selection,
remove_node
),
cmd!("smartArt.promote", "Promote", ["SmartArt Design", "Create Graphic"], None, "{id?, index}", has_selection, promote),
cmd!("smartArt.demote", "Demote", ["SmartArt Design", "Create Graphic"], None, "{id?, index}", has_selection, demote),
cmd!("smartArt.promote", "Promote", ["Diagram Design", "Create Graphic"], None, "{id?, index}", has_selection, promote),
cmd!("smartArt.demote", "Demote", ["Diagram Design", "Create Graphic"], None, "{id?, index}", has_selection, demote),
cmd!(
"smartArt.layout",
"Change Layout",
["SmartArt Design", "Layouts"],
["Diagram Design", "Layouts"],
None,
"{id?, kind: list|blockList|hBulletList|process|chevron|verticalProcess|stepUp|cycle|blockCycle|radial|hierarchy|pyramid|invertedPyramid|matrix|titledMatrix|pictureList|pictureCaption|pictureProcess|pictureHList|timeline|venn|target|textCycle|continuousCycle|horizontalHierarchy|gridMatrix|gear|funnel|segmentedCycle|multidirectionalCycle|orgChart|halfCircleOrgChart|cycleMatrix|segmentedPyramid|pie|nameTitleOrgChart|verticalBoxList|trapezoidList|tableList|accentProcess|continuousBlockProcess|bendingProcess|linearVenn|stackedVenn|nestedTarget}",
has_selection,
@@ -224,17 +224,17 @@ pub fn specs() -> Vec<CommandSpec> {
),
cmd!(
"smartArt.fromText",
"Convert to SmartArt",
"Convert to Diagram",
["Home", "Paragraph"],
None,
"{id?, kind?} → {id} (a text box or placeholder becomes a SmartArt graphic in its place: one node per paragraph, indent levels kept)",
"{id?, kind?} → {id} (a text box or placeholder becomes a Diagram graphic in its place: one node per paragraph, indent levels kept)",
has_selection,
from_text
),
cmd!(
"smartArt.toText",
"Convert to Text",
["SmartArt Design", "Reset"],
["Diagram Design", "Reset"],
None,
"{id?} → {id} (the graphic becomes a text box of its outline, bulleted by level)",
has_selection,
@@ -243,7 +243,7 @@ pub fn specs() -> Vec<CommandSpec> {
cmd!(
"smartArt.toShapes",
"Convert to Shapes",
["SmartArt Design", "Reset"],
["Diagram Design", "Reset"],
None,
"{id?} (the graphic becomes a plain group of its shapes; the outline is dropped)",
has_selection,
@@ -251,8 +251,8 @@ pub fn specs() -> Vec<CommandSpec> {
),
cmd!(
"smartArt.picture",
"Insert Picture in SmartArt",
["SmartArt Design", "Create Graphic"],
"Insert Picture in Diagram",
["Diagram Design", "Create Graphic"],
None,
"{id?, index, data?: base64 | path?: string | reset?: bool} (the node's picture placeholder in picture layouts)",
has_selection,
@@ -260,8 +260,8 @@ pub fn specs() -> Vec<CommandSpec> {
),
cmd!(
"smartArt.style",
"SmartArt Styles",
["SmartArt Design", "SmartArt Styles"],
"Diagram Styles",
["Diagram Design", "Diagram Styles"],
None,
"{id?, style: flat|subtle|intense|outline|polished|inset|cartoon|powder|brick}",
has_selection,
@@ -270,7 +270,7 @@ pub fn specs() -> Vec<CommandSpec> {
cmd!(
"smartArt.colors",
"Change Colors",
["SmartArt Design", "SmartArt Styles"],
["Diagram Design", "Diagram Styles"],
None,
"{id?, colors: colorful|range23|range34|range45|range56|accent1|…|accent6|accent1Grad|…|accent6Grad}",
has_selection,
+6 -6
View File
@@ -38,7 +38,7 @@ fn target_group(s: &Session, p: &Value, cmd: &str) -> Result<ShapeId> {
let st = s.doc()?;
let shapes = current_shapes(&st.doc, &st.selection).ok_or_else(|| bad(cmd, "no slide"))?;
let ids = targets(s, p)?;
ids.iter().find_map(|id| diagram_group(shapes, *id)).map(|g| g.id).ok_or_else(|| bad(cmd, "select a SmartArt graphic"))
ids.iter().find_map(|id| diagram_group(shapes, *id)).map(|g| g.id).ok_or_else(|| bad(cmd, "select a Diagram graphic"))
}
/// Children with fresh ids for diagram `d` inside a group whose child space is `area`.
@@ -55,7 +55,7 @@ fn edit_diagram<T>(s: &mut Session, p: &Value, cmd: &str, f: impl FnOnce(&mut Di
let gid = target_group(s, p, cmd)?;
s.edit(|doc, sel| {
let g = current_shapes(doc, sel).and_then(|v| deckcraft_model::find_shape(v, gid)).cloned().ok_or_else(|| bad(cmd, "no such shape"))?;
let (ShapeKind::Group { children, child }, Some(mut d)) = (g.kind, g.diagram) else { return Err(bad(cmd, "not a SmartArt graphic")) };
let (ShapeKind::Group { children, child }, Some(mut d)) = (g.kind, g.diagram) else { return Err(bad(cmd, "not a Diagram graphic")) };
sync_text(&mut d, &children);
let out = f(&mut d)?;
normalize(&mut d.nodes);
@@ -90,7 +90,7 @@ pub fn insert(s: &mut Session, kind: &str, mut nodes: Vec<DiagramNode>) -> Resul
let id = ShapeId(doc.alloc_id().ok_or_else(|| bad("insert.smartArt", NO_ID_SPACE))?);
let g = Shape {
id,
name: format!("SmartArt ({kind})"),
name: format!("Diagram ({kind})"),
xfrm: Some(area),
kind: ShapeKind::Group { children, child: area },
diagram: Some(d),
@@ -201,7 +201,7 @@ pub(super) fn from_text(s: &mut Session, p: &Value) -> Result<Value> {
let gid = ShapeId(doc.alloc_id().ok_or_else(|| bad(CMD, NO_ID_SPACE))?);
let g = Shape {
id: gid,
name: format!("SmartArt ({kind})"),
name: format!("Diagram ({kind})"),
xfrm: Some(area),
kind: ShapeKind::Group { children, child: area },
diagram: Some(d),
@@ -222,12 +222,12 @@ pub(super) fn from_text(s: &mut Session, p: &Value) -> Result<Value> {
pub(super) fn to_text(s: &mut Session, p: &Value) -> Result<Value> {
const CMD: &str = "smartArt.toText";
let gid = target_group(s, p, CMD)?;
let (_, d) = selected_for(s, gid).ok_or_else(|| bad(CMD, "select a SmartArt graphic"))?;
let (_, d) = selected_for(s, gid).ok_or_else(|| bad(CMD, "select a Diagram graphic"))?;
// The sizes the graphic's text shows at: its titles' and its bullets' (shrunk to fit included).
let (title_pt, bullet_pt) = {
let st = s.doc()?;
let shapes = current_shapes(&st.doc, &st.selection).ok_or_else(|| bad(CMD, "no slide"))?;
let g = diagram_group(shapes, gid).ok_or_else(|| bad(CMD, "select a SmartArt graphic"))?;
let g = diagram_group(shapes, gid).ok_or_else(|| bad(CMD, "select a Diagram graphic"))?;
text_sizes(g.children())
};
let new = s.edit(|doc, sel| {
+4 -4
View File
@@ -166,21 +166,21 @@ pub(super) fn text_shape(preset: &str, x: Xfrm, lines: &[&DiagramNode], title: b
if !title {
body.body.anchor = Some(deckcraft_model::text::Anchor::Top);
}
Shape { name: "SmartArt shape".into(), xfrm: Some(x), geom: Geom::preset(preset), text: Some(body), ..Default::default() }
Shape { name: "Diagram shape".into(), xfrm: Some(x), geom: Geom::preset(preset), text: Some(body), ..Default::default() }
}
/// A plain shape with no text (arrows between steps).
pub(super) fn deco(preset: &str, x: Xfrm, rot: f64) -> Shape {
let mut x = x;
x.rot = rot;
Shape { name: "SmartArt arrow".into(), xfrm: Some(x), geom: Geom::preset(preset), ..Default::default() }
Shape { name: "Diagram arrow".into(), xfrm: Some(x), geom: Geom::preset(preset), ..Default::default() }
}
pub(super) fn line_shape(a: (f64, f64), b: (f64, f64)) -> Shape {
let mut x = Xfrm::new(a.0.min(b.0), a.1.min(b.1), (b.0 - a.0).abs(), (b.1 - a.1).abs());
x.flip_h = (b.0 < a.0) != (b.1 < a.1);
Shape {
name: "SmartArt connector".into(),
name: "Diagram connector".into(),
xfrm: Some(x),
geom: Geom::preset("line"),
fill: Some(Fill::None),
@@ -257,7 +257,7 @@ fn fit_words(sh: &mut Shape) {
/// A picture placeholder for node `node`: its picture when it has one, else a pale box.
pub(super) fn picture_shape(d: &Diagram, preset: &str, x: Xfrm, i: usize, node: usize) -> Shape {
let mut sh = Shape { name: "SmartArt picture".into(), xfrm: Some(x), geom: Geom::preset(preset), ..Default::default() };
let mut sh = Shape { name: "Diagram picture".into(), xfrm: Some(x), geom: Geom::preset(preset), ..Default::default() };
paint(d, &mut sh, i, true);
if let Some(m) = d.nodes.get(node).and_then(|n| n.picture) {
sh.fill = Some(Fill::Picture(deckcraft_model::style::PictureFill { media: m, ..Default::default() }));
+1 -1
View File
@@ -56,7 +56,7 @@ pub(crate) fn delete_dir(s: &mut Session, dir: &str) -> Result<Value> {
ed::delete(body, a, b)
} else {
let other = match dir.as_str() {
"forward" => ed::char_move(body, a, true),
"forward" => ed::cluster_end(body, a),
_ => {
// Backspace at the start of an indented/bulleted paragraph outdents first.
if a.1 == 0
+87 -37
View File
@@ -9,7 +9,7 @@
//! folder, the rest of the path being plain names (`..` below a missing folder is refused, and so
//! is a link that leads nowhere).
//! - The result must lie inside the root, compared by whole components (`/work2` isn't inside
//! `/work`), without regard to case on Windows.
//! `/work`), exactly: on Windows what exists is resolved to its real case first.
//! - Read and write access are separate: a root given alone grants only its own kind of access.
//!
//! Roots are in force on a thread inside [`confine`], and in the whole process after
@@ -85,14 +85,20 @@ impl AutomationRoots {
/// May the file or folder at `path` be read (or written)? `Err` says why not.
pub fn check(&self, path: &str, access: Access) -> Result<(), String> {
self.check_path(Path::new(path), access)
}
/// [`Self::check`] for a path that may not be UTF-8: the checked path is the one opened.
pub fn check_path(&self, path: &Path, access: Access) -> Result<(), String> {
let path_text = path.display();
let roots = match access {
Access::Read => &self.read,
Access::Write => &self.write,
};
if roots.is_empty() {
return match access {
Access::Read => Err(format!("automation filesystem access is not granted: read authority is absent: {path}")),
Access::Write => Err(format!("automation filesystem access is not granted: write authority is absent: {path}")),
Access::Read => Err(format!("automation filesystem access is not granted: read authority is absent: {path_text}")),
Access::Write => Err(format!("automation filesystem access is not granted: write authority is absent: {path_text}")),
};
}
let resolved = resolve(path)?;
@@ -101,8 +107,8 @@ impl AutomationRoots {
}
let roots = roots.iter().map(|root| root.shown.display().to_string()).collect::<Vec<_>>().join(", ");
match access {
Access::Read => Err(format!("automation path rejected: outside the read roots {roots}: {path}")),
Access::Write => Err(format!("automation path rejected: outside the write roots {roots}: {path}")),
Access::Read => Err(format!("automation path rejected: outside the read roots {roots}: {path_text}")),
Access::Write => Err(format!("automation path rejected: outside the write roots {roots}: {path_text}")),
}
}
}
@@ -191,17 +197,23 @@ pub fn check_write(path: &str) -> Result<(), String> {
current().map_or(Ok(()), |r| r.check(path, Access::Write))
}
/// May `path` be written here? Always, without roots in force. For a path that may not be UTF-8.
pub fn check_write_path(path: &Path) -> Result<(), String> {
current().map_or(Ok(()), |r| r.check_path(path, Access::Write))
}
/// `path` made absolute with its links followed; for a path that doesn't exist (yet), its nearest
/// existing folder's links followed and the rest appended.
fn resolve(path: &str) -> Result<PathBuf, String> {
if path.is_empty() {
fn resolve(path: &Path) -> Result<PathBuf, String> {
if path.as_os_str().is_empty() {
return Err("automation path rejected: path is empty".into());
}
let shown = path.display();
// Checked as given: making `…\NUL` absolute gives the device path `\\.\NUL` on Windows.
if cfg!(windows) && Path::new(path).components().any(|c| matches!(c, Component::Normal(n) if is_device_name(&n.to_string_lossy()))) {
return Err(format!("automation path rejected: reserved device names are not allowed: {path}"));
if cfg!(windows) && path.components().any(|c| matches!(c, Component::Normal(n) if is_device_name(&n.to_string_lossy()))) {
return Err(format!("automation path rejected: reserved device names are not allowed: {shown}"));
}
let absolute = std::path::absolute(path).map_err(|e| format!("automation path rejected: cannot resolve {path}: {e}"))?;
let absolute = std::path::absolute(path).map_err(|e| format!("automation path rejected: cannot resolve {shown}: {e}"))?;
// The names below the nearest existing folder, last first.
let mut missing: Vec<OsString> = vec![];
let mut at = absolute.as_path();
@@ -212,11 +224,11 @@ fn resolve(path: &str) -> Result<PathBuf, String> {
return Ok(found);
}
Err(e) => match std::fs::symlink_metadata(at) {
Ok(m) if m.file_type().is_symlink() => return Err(format!("automation path rejected: a link that leads nowhere: {path}")),
Ok(m) if m.file_type().is_symlink() => return Err(format!("automation path rejected: a link that leads nowhere: {shown}")),
// There, yet its real path can't be had (no permission): nothing to compare.
Ok(_) => return Err(format!("automation path rejected: cannot resolve {path}: {e}")),
Ok(_) => return Err(format!("automation path rejected: cannot resolve {shown}: {e}")),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => return Err(format!("automation path rejected: cannot resolve {path}: {e}")),
Err(e) => return Err(format!("automation path rejected: cannot resolve {shown}: {e}")),
},
}
let parent = at.parent();
@@ -226,41 +238,30 @@ fn resolve(path: &str) -> Result<PathBuf, String> {
at = parent;
}
(Some(Component::ParentDir), _) => {
return Err(format!("automation path rejected: parent traversal below a folder that doesn't exist: {path}"));
return Err(format!("automation path rejected: parent traversal below a folder that doesn't exist: {shown}"));
}
// Down to the root (or drive) and it doesn't exist either.
_ => {
let e = std::io::Error::from(std::io::ErrorKind::NotFound);
return Err(format!("automation path rejected: cannot resolve {path}: {e}"));
return Err(format!("automation path rejected: cannot resolve {shown}: {e}"));
}
}
}
}
/// Is `path` (resolved) `root` or inside it, component by component (without regard to case on
/// Windows, whose file systems ignore it)?
/// Is `path` (resolved) `root` or inside it, component by component? Exactly, on Windows too: what
/// exists has its real case from [`resolve`], and a folder that doesn't exist yet may be a different
/// one from the root where a parent folder is case sensitive.
fn inside(path: &Path, root: &Path) -> bool {
let mut path = path.components();
root.components().all(|r| path.next().is_some_and(|p| same_component(p, r)))
root.components().all(|r| path.next() == Some(r))
}
fn same_component(a: Component, b: Component) -> bool {
if a == b {
return true;
}
if !cfg!(windows) {
return false;
}
match (a.as_os_str().to_str(), b.as_os_str().to_str()) {
(Some(a), Some(b)) => a.to_lowercase() == b.to_lowercase(),
_ => false,
}
}
/// A Windows device name (`CON`, `NUL`, `COM1`… also with an extension), which names no file.
/// A Windows device name (`CON`, `NUL`, `COM1`, `COM¹`… also with an extension or a colon), which names no file.
fn is_device_name(component: &str) -> bool {
let stem = component.split('.').next().unwrap_or(component).trim_end_matches(' ').to_ascii_uppercase();
let numbered = |prefix: &str| stem.strip_prefix(prefix).is_some_and(|n| matches!(n, "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"));
let stem = component.split(['.', ':']).next().unwrap_or(component).trim_end_matches(' ').to_ascii_uppercase();
let numbered =
|prefix: &str| stem.strip_prefix(prefix).is_some_and(|n| matches!(n, "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "¹" | "²" | "³"));
matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL" | "CLOCK$" | "CONIN$" | "CONOUT$") || numbered("COM") || numbered("LPT")
}
@@ -365,6 +366,7 @@ mod tests {
let _ = std::fs::remove_dir_all(base);
}
/// A root and a path reached through links are compared after the links are followed.
#[test]
fn links_are_followed_before_comparing() {
let (base, roots) = folders("links");
@@ -401,20 +403,67 @@ mod tests {
let _ = std::fs::remove_dir_all(base);
}
/// A name that differs in case is another name until the file system says otherwise.
#[test]
fn names_are_compared_exactly() {
let root = Path::new("/work/In");
assert!(inside(Path::new("/work/In/new/a.deckcraft"), root));
assert!(!inside(Path::new("/work/IN/a.deckcraft"), root));
assert!(!inside(Path::new("/WORK/In/a.deckcraft"), root));
assert!(!inside(Path::new("/work/in\u{212A}/a.deckcraft"), Path::new("/work/ink")), "the Kelvin sign folds to k");
if cfg!(windows) {
let root = Path::new(r"C:\work\In");
assert!(inside(Path::new(r"c:\work\In\a.deckcraft"), root), "a drive letter is one drive in either case");
assert!(!inside(Path::new(r"C:\work\IN\a.deckcraft"), root));
}
}
/// In a case-sensitive folder, `IN` beside the root `in` is another folder, there or not yet.
#[cfg(windows)]
#[test]
fn windows_ignores_case_and_refuses_devices() {
fn a_case_sensitive_folder_keeps_a_differently_cased_name_out() {
let base = std::env::temp_dir().join(format!("dc-file-access-case-sensitive-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&base);
std::fs::create_dir_all(&base).unwrap();
let set = std::process::Command::new("fsutil").args(["file", "setCaseSensitiveInfo"]).arg(&base).arg("enable").output();
if !set.as_ref().is_ok_and(|o| o.status.success()) {
eprintln!("skipped: a case-sensitive folder can't be made here: {set:?}");
let _ = std::fs::remove_dir_all(base);
return;
}
std::fs::create_dir(base.join("in")).unwrap();
let root = [base.join("in")];
let roots = AutomationRoots::new(&root, &root).unwrap().unwrap();
assert_eq!(roots.check(&s(base.join("in/new.deckcraft")), Access::Write), Ok(()));
let e = roots.check(&s(base.join("IN/new.deckcraft")), Access::Write).unwrap_err();
assert!(e.contains("outside the write root"), "{e}");
std::fs::create_dir(base.join("IN")).unwrap();
std::fs::write(base.join("IN/secret.deckcraft"), "x").unwrap();
let e = roots.check(&s(base.join("IN/secret.deckcraft")), Access::Read).unwrap_err();
assert!(e.contains("outside the read root"), "{e}");
let _ = std::fs::remove_dir_all(base);
}
#[cfg(windows)]
/// On Windows a path finds its real case on disk, and device names are refused.
#[test]
fn windows_finds_the_real_case_and_refuses_devices() {
let (base, roots) = folders("case");
let upper = s(base.join("IN/a.deckcraft"));
assert_eq!(roots.check(&upper, Access::Read), Ok(()), "{upper}");
assert_eq!(roots.check(&s(base.join("In/Sub/New.deckcraft")), Access::Read), Ok(()));
assert_eq!(roots.check(&s(base.join("OUT/New.deckcraft")), Access::Write), Ok(()));
assert_eq!(roots.check(&s(base.join("OUT")), Access::Write), Ok(()));
let e = roots.check(&s(base.join("out/NUL")), Access::Write).unwrap_err();
assert!(e.contains("reserved device names"), "{e}");
assert!(roots.check(&s(base.join("out/com1.txt")), Access::Write).is_err());
let e = roots.check(&s(base.join("out/COM¹.txt")), Access::Write).unwrap_err();
assert!(e.contains("reserved device names"), "{e}");
assert_eq!(roots.check(&s(base.join("out/console.txt")), Access::Write), Ok(()));
let _ = std::fs::remove_dir_all(base);
}
/// Scopes nest, and a scope applies only on the thread that set it.
#[test]
fn scopes_nest_and_stay_on_their_thread() {
let (base, roots) = folders("scopes");
@@ -436,12 +485,13 @@ mod tests {
let _ = std::fs::remove_dir_all(base);
}
/// What is a device name, with superscript digits and a name before a colon, and what isn't.
#[test]
fn device_names() {
for d in ["CON", "nul", "Com1", "LPT9.txt", "aux.deckcraft", "NUL "] {
for d in ["CON", "nul", "Com1", "LPT9.txt", "aux.deckcraft", "NUL ", "COM¹", "Lpt²", "lpt³.txt", "COM1:", "nul:x", "COM¹:"] {
assert!(is_device_name(d), "{d}");
}
for f in ["console", "com10", "COM0", "lpt", "a.deckcraft", "nul-x"] {
for f in ["console", "com10", "COM0", "lpt", "a.deckcraft", "nul-x", "COM⁴", "com¹0", "lpt¹x", "console:x"] {
assert!(!is_device_name(f), "{f}");
}
}
+11 -7
View File
@@ -1,6 +1,8 @@
//! Fonts a presentation uses that this machine doesn't have, so opening it can say so (its text
//! draws in a stand-in; see `FontDb::document_face`).
use std::collections::HashSet;
use deckcraft_fonts::FontDb;
use deckcraft_model::resolve::{self, Ctx};
use deckcraft_model::text::{Bullet, TextBody};
@@ -12,8 +14,13 @@ use deckcraft_model::{Presentation, Shape, ShapeKind};
/// font to load and are left for the round trip.
pub fn install_embedded_fonts(p: &Presentation) {
let db = FontDb::global();
// Faces can name one media item many times; each is copied and enumerated once.
let mut seen = HashSet::new();
for font in &p.embedded_fonts {
for id in font.faces().into_iter().flatten() {
if !seen.insert(id) {
continue;
}
let Some(item) = p.media(id) else { continue };
let Some(sfnt) = deckcraft_fonts::embedded_sfnt(&item.data) else { continue };
db.add_font(sfnt.to_vec());
@@ -27,12 +34,8 @@ pub fn install_embedded_fonts(p: &Presentation) {
/// and can't be installed, so they aren't reported.
pub fn missing_fonts(p: &Presentation) -> Vec<String> {
let db = FontDb::global();
let mut out: Vec<String> = Vec::new();
for family in used_fonts(p) {
if !family.starts_with('.') && !db.has_family(&family) && !out.iter().any(|f| f.eq_ignore_ascii_case(&family)) {
out.push(family);
}
}
// used_fonts is already free of case-insensitive duplicates.
let mut out: Vec<String> = used_fonts(p).into_iter().filter(|f| !f.starts_with('.') && !db.has_family(f)).collect();
out.sort_by_key(|f| f.to_lowercase());
out
}
@@ -40,9 +43,10 @@ pub fn missing_fonts(p: &Presentation) -> Vec<String> {
/// The families the slides' text runs and bullets are drawn in.
pub fn used_fonts(p: &Presentation) -> Vec<String> {
let mut out: Vec<String> = Vec::new();
let mut seen: HashSet<String> = HashSet::new();
let mut add = |f: String| {
let f = f.trim().to_string();
if !f.is_empty() && !out.iter().any(|o| o.eq_ignore_ascii_case(&f)) {
if !f.is_empty() && seen.insert(f.to_ascii_lowercase()) {
out.push(f);
}
};
+28 -4
View File
@@ -646,7 +646,9 @@ impl Session {
self.settle_composition();
}
if let Err(e) = (spec.enabled)(self) {
let named = (e == cmd::NOTHING_SELECTED || e == cmd::SELECT_TWO) && (params.get("ids").is_some() || params.get("id").is_some());
// A command told which shapes to act on doesn't depend on what is selected.
let named = (e == cmd::NOTHING_SELECTED || e == cmd::SELECT_TWO || e == cmd::merge::IN_GROUP)
&& (params.get("ids").is_some() || params.get("id").is_some());
if !named {
return Err(EngineError::Disabled(id.into(), e));
}
@@ -824,10 +826,32 @@ fn clamp_selection(st: &mut DocState) {
/// Default for [`History::byte_limit`].
pub const UNDO_BYTE_BUDGET: usize = 256 << 20;
/// A rough size of a text body in memory: its paragraphs and runs, at their inline struct sizes.
fn text_cost(t: &deckcraft_model::text::TextBody) -> usize {
use deckcraft_model::text::{Paragraph, Run};
let (para, run) = (std::mem::size_of::<Paragraph>(), std::mem::size_of::<Run>());
t.paragraphs.iter().map(|p| para + p.runs.iter().map(|r| run + r.text.len()).sum::<usize>()).sum()
}
/// A rough size of comments in memory, replies included.
fn comments_cost(cs: &[deckcraft_model::Comment], depth: usize) -> usize {
let kids = |c: &deckcraft_model::Comment| if depth < 64 { comments_cost(&c.replies, depth + 1) } else { 0 };
cs.iter().map(|c| 256 + c.author.len() + c.initials.len() + c.text.len() + c.date.len() + kids(c)).sum()
}
/// A rough size of a slide's own data besides its shapes: notes, comments, tags and kept XML,
/// all copied with the slide when an edit changes it.
fn slide_extra_cost(s: &deckcraft_model::Slide) -> usize {
text_cost(&s.notes)
+ comments_cost(&s.comments, 0)
+ s.name.len()
+ s.tags.iter().map(|(k, v)| 48 + k.len() + v.len()).sum::<usize>()
+ s.raw_ext.as_ref().map_or(0, String::len)
}
/// A rough size of a shape in memory: the struct, its text and kept XML, its children.
fn shape_cost(sh: &deckcraft_model::Shape, depth: usize) -> usize {
let text: usize =
sh.text.as_ref().map_or(0, |t| t.paragraphs.iter().map(|p| 96 + p.runs.iter().map(|r| 128 + r.text.len()).sum::<usize>()).sum());
let text: usize = sh.text.as_ref().map_or(0, text_cost);
let kids: usize = if depth < 64 { sh.children().iter().map(|c| shape_cost(c, depth + 1)).sum() } else { 0 };
768 + text + sh.raw_ext.as_ref().map_or(0, String::len) + kids
}
@@ -845,7 +869,7 @@ pub fn undo_cost(doc: &Presentation, newer: &Presentation) -> usize {
let media: HashSet<*const Vec<u8>> = newer.media.iter().map(|m| Arc::as_ptr(&m.data)).collect();
let mut n = 64 * doc.slides.len();
for s in doc.slides.iter().filter(|s| !slides.contains(&Arc::as_ptr(s))) {
n += 512 + shapes_cost(&s.shapes);
n += 512 + shapes_cost(&s.shapes) + slide_extra_cost(s);
}
for m in doc.masters.iter().filter(|m| !masters.contains(&Arc::as_ptr(m))) {
n += 4096 + shapes_cost(&m.shapes) + m.layouts.iter().map(|l| 512 + shapes_cost(&l.shapes)).sum::<usize>();
+74 -8
View File
@@ -171,7 +171,9 @@ impl Dictionary {
["COMPOUNDWORDMAX", n, ..] => d.compound_max = n.parse().unwrap_or(0),
["REP", n] => {
for _ in 0..n.parse::<usize>().unwrap_or(0) {
let r: Vec<&str> = lines.next().unwrap_or("").split_whitespace().collect();
// The count is the file's claim: stop when the records run out.
let Some(line) = lines.next() else { break };
let r: Vec<&str> = line.split_whitespace().collect();
if let [_, a, b, ..] = r.as_slice() {
d.rep.push((a.replace('_', " "), b.replace('_', " ")));
}
@@ -182,7 +184,8 @@ impl Dictionary {
let Some(flag) = one_flag(Some(flag), mode) else { continue };
let cross = cross.eq_ignore_ascii_case("y");
for _ in 0..n.parse::<usize>().unwrap_or(0) {
let r: Vec<&str> = lines.next().unwrap_or("").split_whitespace().collect();
let Some(line) = lines.next() else { break };
let r: Vec<&str> = line.split_whitespace().collect();
let [_, _, strip, add, rest @ ..] = r.as_slice() else { continue };
let (add, cont) = match add.split_once('/') {
Some((a, c)) => (a, parse_flags(c, mode)),
@@ -304,7 +307,12 @@ impl Dictionary {
/// `w` made of dictionary parts allowed in compounds (`COMPOUNDFLAG`, or the begin, middle and
/// end flags), each at least `COMPOUNDMIN` characters long.
fn compounded(&self, w: &[char], index: usize) -> bool {
fn compounded(&self, w: &[char], index: usize, failed: &mut HashSet<(usize, usize)>) -> bool {
// `w` is always a tail of the checked word, so its length names it: a (tail, part index)
// that failed once fails again however the earlier parts were split.
if failed.contains(&(w.len(), index)) {
return false;
}
if self.compound.is_none() && self.compound_end.is_none() {
return false;
}
@@ -322,10 +330,11 @@ impl Dictionary {
if [self.compound, self.compound_end].into_iter().flatten().any(|f| self.compound_part(rest, f, true)) {
return true;
}
if self.compounded(rest, index + 1) {
if self.compounded(rest, index + 1, failed) {
return true;
}
}
failed.insert((w.len(), index));
false
}
@@ -352,7 +361,7 @@ impl Dictionary {
return true;
}
}
self.compounded(&w, 0)
self.compounded(&w, 0, &mut HashSet::new())
}
/// Is `word` spelt correctly? Capitalised and upper-case forms of a lower-case word pass.
@@ -576,6 +585,15 @@ fn cache() -> &'static Cache {
CACHE.get_or_init(Default::default)
}
/// Language strings remembered by [`dictionary`]; a string past it is resolved again on each call.
const MAX_CACHED_LANGS: usize = 256;
/// Parsed dictionaries by their affix file, so aliases of one language share it.
fn by_files() -> &'static Mutex<HashMap<PathBuf, Arc<Dictionary>>> {
static M: OnceLock<Mutex<HashMap<PathBuf, Arc<Dictionary>>>> = OnceLock::new();
M.get_or_init(Default::default)
}
/// The dictionary for `lang` (a BCP-47 tag such as `en-US`), loaded once per process.
pub fn dictionary(lang: &str) -> Option<Arc<Dictionary>> {
let cache = cache();
@@ -586,10 +604,23 @@ pub fn dictionary(lang: &str) -> Option<Arc<Dictionary>> {
return d.clone();
}
let loaded = find_in(&search_dirs(), lang).and_then(|(a, d)| {
let (a, d) = (std::fs::read(a).ok()?, std::fs::read(d).ok()?);
Some(Arc::new(Dictionary::parse(&a, &d)))
// Tags that resolve to the same files share one parsed dictionary.
if let Ok(m) = by_files().lock()
&& let Some(dict) = m.get(&a)
{
return Some(dict.clone());
}
let (bytes_a, bytes_d) = (std::fs::read(&a).ok()?, std::fs::read(d).ok()?);
let dict = Arc::new(Dictionary::parse(&bytes_a, &bytes_d));
// A concurrent loader of the same files may have inserted first: share its copy.
if let Ok(mut m) = by_files().lock() {
return Some(m.entry(a).or_insert(dict).clone());
}
Some(dict)
});
if let Ok(mut c) = cache.lock() {
if let Ok(mut c) = cache.lock()
&& c.len() < MAX_CACHED_LANGS
{
c.insert(key, loaded.clone());
}
loaded
@@ -668,6 +699,8 @@ pub fn add_user_word(word: &str) -> std::io::Result<()> {
return Ok(());
}
let path = user_dictionary_path().ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, "no folder for the user dictionary"))?;
// A confined automation client may write only inside its write roots, this file included.
crate::file_access::check_write_path(&path).map_err(|e| std::io::Error::new(std::io::ErrorKind::PermissionDenied, e))?;
if let Some(d) = path.parent() {
std::fs::create_dir_all(d)?;
}
@@ -897,6 +930,14 @@ SFX D Y 1\nSFX D 0 ed/S .\n";
assert!(d.suggest("workingslides", 5).contains(&"working slides".to_string()));
}
/// An affix header can claim billions of records; parsing stops where the file does.
#[test]
fn a_header_count_past_the_records_ends_with_them() {
let aff = b"SFX S Y 4000000000\nSFX S 0 s .\nREP 4000000000\nREP a b\n";
let d = Dictionary::parse(aff, b"1\ncat/S\n".as_slice());
assert!(d.check("cats"));
}
/// #1175: compound splitting is quadratic in the word's length, so a word longer than
/// `MAX_CHECK_CHARS` is not checked; shorter ones still are.
#[test]
@@ -931,6 +972,17 @@ SFX D Y 1\nSFX D 0 ed/S .\n";
}
}
#[test]
fn overlapping_compound_roots_with_a_failing_ending_stay_cheap() {
// Roots a..aaaaaaaa overlap, so many splits of "aaa…ab" reach the same (tail, part) state; unmemoized that is millions of searches.
let d = Dictionary::parse(
b"COMPOUNDFLAG X\nCOMPOUNDMIN 1\n".as_slice(),
b"8\na/X\naa/X\naaa/X\naaaa/X\naaaaa/X\naaaaaa/X\naaaaaaa/X\naaaaaaaa/X\n".as_slice(),
);
assert!(!d.check(&format!("{}b", "a".repeat(99))));
assert!(d.check(&"a".repeat(20)));
}
#[test]
fn runs_give_words_their_language() {
use deckcraft_model::{Paragraph, Run, RunProps};
@@ -970,6 +1022,20 @@ SFX D Y 1\nSFX D 0 ed/S .\n";
assert_eq!(w, vec!["check", "done"]);
}
#[test]
fn a_confined_client_cannot_write_the_user_dictionary_outside_its_roots() {
let root = std::env::temp_dir().join(format!("deckcraft-dict-root-{}", std::process::id()));
std::fs::create_dir_all(&root).unwrap();
let roots = crate::file_access::AutomationRoots::new(std::slice::from_ref(&root), std::slice::from_ref(&root)).unwrap();
// The user dictionary is a file of its own outside `root`, not whatever HOME points at.
let outside = std::env::temp_dir().join(format!("deckcraft-dict-outside-{}", std::process::id()));
set_user_dictionary_path(outside.join("user.dic"));
let r = crate::file_access::confine(roots.as_ref(), || add_user_word("zqxconfinedword"));
assert_eq!(r.map_err(|e| e.kind()), Err(std::io::ErrorKind::PermissionDenied));
assert!(!outside.exists(), "the refused write created nothing");
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn a_long_paragraph_with_a_huge_language_is_checked_in_linear_work() {
use deckcraft_model::{Paragraph, Run, RunProps};
+420 -5
View File
@@ -98,6 +98,33 @@ fn text_editing_flow() {
assert_eq!(s.doc().unwrap().selection.shapes.len(), 1);
}
/// Forward Delete removes a whole grapheme cluster, not just its first scalar.
#[test]
fn forward_delete_removes_whole_grapheme_cluster() {
for (input, rest) in [("\u{1F44D}\u{1F3FD}y", "y"), ("e\u{301}x", "x")] {
let mut s = session();
let title = s.doc().unwrap().current_slide().unwrap().shapes[0].id.0;
s.execute("text.edit", &json!({"id": title})).unwrap();
s.execute("text.selectAll", &json!({})).unwrap();
s.execute("text.insert", &json!({"text": input})).unwrap();
s.execute("text.move", &json!({"to": "start"})).unwrap();
s.execute("text.delete", &json!({"dir": "forward"})).unwrap();
let t = s.execute("text.get", &json!({"id": title})).unwrap();
assert_eq!(t["text"], rest);
}
// Inside a cluster, Delete stops at that cluster's end and leaves the next flag intact.
let mut s = session();
let title = s.doc().unwrap().current_slide().unwrap().shapes[0].id.0;
s.execute("text.edit", &json!({"id": title})).unwrap();
s.execute("text.selectAll", &json!({})).unwrap();
s.execute("text.insert", &json!({"text": "\u{1F1FA}\u{1F1F8}\u{1F1E8}\u{1F1E6}"})).unwrap();
s.execute("text.move", &json!({"to": "start"})).unwrap();
s.execute("text.move", &json!({"to": "right"})).unwrap();
s.execute("text.delete", &json!({"dir": "forward"})).unwrap();
let t = s.execute("text.get", &json!({"id": title})).unwrap();
assert_eq!(t["text"], "\u{1F1FA}\u{1F1E8}\u{1F1E6}");
}
/// `text.set` treats CRLF (or a lone CR) as one paragraph break and keeps no `\r` in the text.
#[test]
fn text_set_crlf_makes_clean_paragraphs() {
@@ -214,6 +241,71 @@ fn clipboard_copy_paste_shapes_and_slides() {
assert!(s.doc().unwrap().doc.validate().is_empty());
}
/// A shape copied or cut from inside a group pastes where it was drawn: on another slide in the
/// same place, on its own slide offset as any paste there is. Its box is mapped out through each
/// group around it, however the groups were moved, stretched, turned or flipped. Before, the
/// clipboard kept the box inside the group, so the shape pasted where it had been before the group
/// was moved or stretched.
#[test]
fn a_shape_copied_from_a_group_pastes_where_it_was_drawn() {
fn rect(s: &mut Session, x: f64, y: f64) -> u64 {
s.execute("shape.insert", &json!({"preset": "rect", "rect": [x, y, 100, 100]})).unwrap()["id"].as_u64().unwrap()
}
let sid = |id: u64| ShapeId(u32::try_from(id).unwrap());
let sites = |s: &Session, id: ShapeId| {
let st = s.doc().unwrap();
connect::sites_of(&st.doc, &st.selection, st.shapes(), id).unwrap()
};
type Edits<'a> = &'a [(&'a str, Value)];
let edit = |s: &mut Session, id: u64, edits: Edits<'_>| {
for (cmd, p) in edits {
let mut p = p.clone();
p["ids"] = json!([id]);
s.execute(cmd, &p).unwrap();
}
};
// Edits to the shape's group, then to a group around that one (none: no outer group).
let cases: [(Edits<'_>, Edits<'_>); 4] = [
(&[("shape.move", json!({"dx": 200}))], &[]),
(&[("shape.resize", json!({"w": 600}))], &[]),
(&[("shape.rotate", json!({"deg": 30})), ("shape.flip", json!({"axis": "vertical"})), ("shape.move", json!({"dx": 50, "dy": 20}))], &[]),
(&[("shape.move", json!({"dx": 200}))], &[("shape.resize", json!({"w": 450})), ("shape.move", json!({"dy": 60}))]),
];
let off = session().prefs.duplicate_offset;
for (n, (inner_edits, outer_edits)) in cases.into_iter().enumerate() {
for (cmd, same_slide) in [("edit.copy", false), ("edit.cut", false), ("edit.copy", true)] {
let mut s = session();
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
let (a, b) = (rect(&mut s, 100.0, 100.0), rect(&mut s, 300.0, 100.0));
let g = s.execute("arrange.group", &json!({"ids": [a, b]})).unwrap()["id"].as_u64().unwrap();
edit(&mut s, g, inner_edits);
if !outer_edits.is_empty() {
let c = rect(&mut s, 100.0, 300.0);
let outer = s.execute("arrange.group", &json!({"ids": [g, c]})).unwrap()["id"].as_u64().unwrap();
edit(&mut s, outer, outer_edits);
}
let was = sites(&s, sid(a));
s.doc_mut().unwrap().selection.shapes = vec![sid(a)];
s.execute(cmd, &json!({})).unwrap();
assert_eq!(s.doc().unwrap().shape(sid(a)).is_some(), cmd == "edit.copy", "case {n}: {cmd}");
if !same_slide {
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
}
let r = s.execute("edit.paste", &json!({})).unwrap();
let pasted = sid(r["ids"][0].as_u64().unwrap());
assert!(s.doc().unwrap().shapes().iter().any(|x| x.id == pasted), "case {n}: {cmd} pastes on the top level");
let d = if same_slide { off } else { 0.0 };
let now = sites(&s, pasted);
assert_eq!(was.len(), now.len(), "case {n}: {cmd}");
assert!(
was.iter().zip(&now).all(|(p, q)| (q.x - p.x - d).hypot(q.y - p.y - d) < 1e-6),
"case {n}: {cmd} on the same slide {same_slide}: drawn at {was:?}, pasted at {now:?}"
);
assert_eq!(s.doc().unwrap().doc.validate(), Vec::<String>::new());
}
}
}
/// The resolved look of every paragraph and run of shape `id` on the current slide: size, font,
/// colour, bold, bullet, indent, alignment, and the box's insets and anchor.
fn resolved_look(s: &Session, id: ShapeId) -> String {
@@ -885,6 +977,154 @@ fn group_ungroup_roundtrip_positions() {
assert!((v["box"]["x"].as_f64().unwrap() - 310.0).abs() < 1e-6, "{v}");
}
/// Ungrouping a group inside another group frees its shapes into the outer group, in its place
/// in the outer group's order, drawn where they were, and selects them. Before, arrange.ungroup
/// looked only at the slide's top level: it answered `{ids: []}` and changed nothing.
#[test]
fn ungrouping_a_nested_group_frees_its_shapes_into_the_outer_group() {
fn rect(s: &mut Session, x: f64, y: f64) -> u64 {
s.execute("shape.insert", &json!({"preset": "rect", "rect": [x, y, 100, 100]})).unwrap()["id"].as_u64().unwrap()
}
let sid = |id: u64| ShapeId(u32::try_from(id).unwrap());
let sites = |s: &Session, id: u64| {
let st = s.doc().unwrap();
connect::sites_of(&st.doc, &st.selection, st.shapes(), sid(id)).unwrap()
};
// The outer group moved and stretched; then an inner group turned and flipped, inside an outer
// group that is turned and moved.
type Edits<'a> = &'a [(&'a str, Value)];
let cases: [(Edits<'_>, Edits<'_>); 2] = [
(&[], &[("shape.move", json!({"dx": 200})), ("shape.resize", json!({"w": 450}))]),
(
&[("shape.rotate", json!({"deg": 30})), ("shape.flip", json!({"axis": "horizontal"}))],
&[("shape.rotate", json!({"deg": 45})), ("shape.move", json!({"dx": 120, "dy": 40}))],
),
];
let edit = |s: &mut Session, id: u64, edits: Edits<'_>| {
for (cmd, p) in edits {
let mut p = p.clone();
p["ids"] = json!([id]);
s.execute(cmd, &p).unwrap();
}
};
for (n, (inner_edits, outer_edits)) in cases.into_iter().enumerate() {
let mut s = session();
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
let (a, b, c) = (rect(&mut s, 100.0, 100.0), rect(&mut s, 300.0, 100.0), rect(&mut s, 100.0, 300.0));
let inner = s.execute("arrange.group", &json!({"ids": [a, b]})).unwrap()["id"].as_u64().unwrap();
edit(&mut s, inner, inner_edits);
let outer = s.execute("arrange.group", &json!({"ids": [inner, c]})).unwrap()["id"].as_u64().unwrap();
edit(&mut s, outer, outer_edits);
let before = [sites(&s, a), sites(&s, b), sites(&s, c)];
let r = s.execute("arrange.ungroup", &json!({"ids": [inner]})).unwrap();
assert_eq!(r["ids"], json!([a, b]), "case {n}");
assert_eq!(s.doc().unwrap().selection.shapes, [sid(a), sid(b)]);
let st = s.doc().unwrap();
let order: Vec<ShapeId> = deckcraft_model::find_shape(st.shapes(), sid(outer)).unwrap().children().iter().map(|x| x.id).collect();
assert_eq!(order, [sid(a), sid(b), sid(c)], "case {n}");
for (id, was) in [a, b, c].into_iter().zip(before) {
let now = sites(&s, id);
assert_eq!(was.len(), now.len(), "case {n}: shape {id}");
assert!(was.iter().zip(&now).all(|(p, q)| (*p - *q).hypot() < 1e-6), "case {n}: shape {id} moved from {was:?} to {now:?}");
}
}
// Two named groups, one inside the other, inside a third: the shapes of both go to the third,
// each listed once.
let mut s = session();
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
let (a, b, c, d) = (rect(&mut s, 100.0, 100.0), rect(&mut s, 300.0, 100.0), rect(&mut s, 100.0, 300.0), rect(&mut s, 300.0, 300.0));
let inner = s.execute("arrange.group", &json!({"ids": [a, b]})).unwrap()["id"].as_u64().unwrap();
let middle = s.execute("arrange.group", &json!({"ids": [inner, c]})).unwrap()["id"].as_u64().unwrap();
let outer = s.execute("arrange.group", &json!({"ids": [middle, d]})).unwrap()["id"].as_u64().unwrap();
s.execute("shape.resize", &json!({"ids": [outer], "w": 600})).unwrap();
let before = [a, b, c, d].map(|id| sites(&s, id));
let r = s.execute("arrange.ungroup", &json!({"ids": [inner, middle]})).unwrap();
assert_eq!(r["ids"], json!([a, b, c]));
assert_eq!(s.doc().unwrap().selection.shapes, [sid(a), sid(b), sid(c)]);
let st = s.doc().unwrap();
let order: Vec<ShapeId> = deckcraft_model::find_shape(st.shapes(), sid(outer)).unwrap().children().iter().map(|x| x.id).collect();
assert_eq!(order, [sid(a), sid(b), sid(c), sid(d)]);
for (id, was) in [a, b, c, d].into_iter().zip(before) {
let now = sites(&s, id);
assert_eq!(was.len(), now.len(), "shape {id}");
assert!(was.iter().zip(&now).all(|(p, q)| (*p - *q).hypot() < 1e-6), "shape {id} moved from {was:?} to {now:?}");
}
}
/// Ungrouping nested groups in one call returns each freed shape once, whatever order the groups
/// are named in. Before, a group freed into a group that the same call freed next was listed twice
/// and replaced once, so the call returned the id of a group that no longer existed.
#[test]
fn ungrouping_nested_groups_in_any_order_returns_only_freed_shapes() {
fn rect(s: &mut Session, x: f64, y: f64) -> u64 {
s.execute("shape.insert", &json!({"preset": "rect", "rect": [x, y, 100, 100]})).unwrap()["id"].as_u64().unwrap()
}
fn group(s: &mut Session, ids: [u64; 2]) -> u64 {
s.execute("arrange.group", &json!({"ids": ids})).unwrap()["id"].as_u64().unwrap()
}
let sid = |id: u64| ShapeId(u32::try_from(id).unwrap());
let mut s = session();
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
let [a, b, c, d, e] = [(100.0, 100.0), (300.0, 100.0), (100.0, 300.0), (300.0, 300.0), (500.0, 300.0)].map(|(x, y)| rect(&mut s, x, y));
let inner = group(&mut s, [a, b]);
let middle = group(&mut s, [inner, c]);
let upper = group(&mut s, [middle, d]);
let outer = group(&mut s, [upper, e]);
let r = s.execute("arrange.ungroup", &json!({"ids": [middle, upper, inner]})).unwrap();
assert_eq!(r["ids"], json!([a, b, c, d]));
assert_eq!(s.doc().unwrap().selection.shapes, [sid(a), sid(b), sid(c), sid(d)]);
let st = s.doc().unwrap();
let order: Vec<ShapeId> = deckcraft_model::find_shape(st.shapes(), sid(outer)).unwrap().children().iter().map(|x| x.id).collect();
assert_eq!(order, [sid(a), sid(b), sid(c), sid(d), sid(e)]);
}
/// Ungrouping a top-level group and a group inside it in one call frees the shapes of both onto
/// the slide, in the top-level group's place and drawn where they were, whichever is named first.
#[test]
fn ungrouping_a_top_level_group_with_a_group_inside_it_frees_both() {
fn rect(s: &mut Session, x: f64, y: f64) -> u64 {
s.execute("shape.insert", &json!({"preset": "rect", "rect": [x, y, 100, 100]})).unwrap()["id"].as_u64().unwrap()
}
fn group(s: &mut Session, ids: [u64; 2]) -> u64 {
s.execute("arrange.group", &json!({"ids": ids})).unwrap()["id"].as_u64().unwrap()
}
let sid = |id: u64| ShapeId(u32::try_from(id).unwrap());
let sites = |s: &Session, id: u64| {
let st = s.doc().unwrap();
connect::sites_of(&st.doc, &st.selection, st.shapes(), sid(id)).unwrap()
};
let roots = |s: &Session| -> Vec<ShapeId> { s.doc().unwrap().shapes().iter().map(|x| x.id).collect() };
for outer_first in [true, false] {
let mut s = session();
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
let [a, b, c, z] = [(100.0, 100.0), (300.0, 100.0), (100.0, 300.0), (500.0, 500.0)].map(|(x, y)| rect(&mut s, x, y));
let inner = group(&mut s, [a, b]);
let outer = group(&mut s, [inner, c]);
s.execute("shape.resize", &json!({"ids": [outer], "w": 600})).unwrap();
let before = [a, b, c].map(|id| sites(&s, id));
let freed = [sid(a), sid(b), sid(c)];
let mut expected = vec![];
for x in roots(&s) {
if x == sid(outer) {
expected.extend(freed);
} else {
expected.push(x);
}
}
assert!(expected.contains(&sid(z)));
let ids = if outer_first { [outer, inner] } else { [inner, outer] };
let r = s.execute("arrange.ungroup", &json!({"ids": ids})).unwrap();
assert_eq!(r["ids"], json!([a, b, c]), "{ids:?}");
assert_eq!(s.doc().unwrap().selection.shapes, freed, "{ids:?}");
assert_eq!(roots(&s), expected, "{ids:?}");
for (id, was) in [a, b, c].into_iter().zip(before) {
let now = sites(&s, id);
assert_eq!(was.len(), now.len(), "{ids:?}: shape {id}");
assert!(was.iter().zip(&now).all(|(p, q)| (*p - *q).hypot() < 1e-6), "{ids:?}: shape {id} moved from {was:?} to {now:?}");
}
}
}
/// Grouping a group again and again stops at `MAX_GROUP_DEPTH`: past about 39 levels a saved
/// .deckcraft file no longer opens, and past 64 commands such as format.languageAll miss the text.
#[test]
@@ -3745,6 +3985,25 @@ fn undo_history_is_capped_by_memory_as_well_as_steps() {
assert_eq!(s.doc().unwrap().history.undo.len(), 120);
}
/// A slide's notes count toward the undo budget: small edits to a slide with large notes keep a
/// copy of those notes per step, and must not slip past the budget as if the slide were empty.
#[test]
fn undo_history_counts_slide_notes() {
let mut s = Session::with_new();
let notes = "n".repeat(1 << 20);
s.edit(|doc, _| {
std::sync::Arc::make_mut(&mut doc.slides[0]).notes = deckcraft_model::text::TextBody::from_text(&notes);
Ok(())
})
.unwrap();
s.doc_mut().unwrap().history = History { limit: 500, byte_limit: 8 << 20, ..Default::default() };
for i in 0..40 {
s.execute("slide.rename", &json!({"name": format!("v{i}"), "index": 0})).unwrap();
}
let n = s.doc().unwrap().history.undo.len();
assert!((1..=8).contains(&n), "{n} steps of 1 MB notes kept under an 8 MB budget");
}
#[test]
fn picture_styles_replace_each_other_in_one_step() {
const PNG: &[u8] = &[
@@ -4141,10 +4400,10 @@ fn smartart_round_trips_through_pptx_as_a_diagram() {
}
let back = deckcraft_pptx::import(&bytes).unwrap();
assert!(back.validate().is_empty());
let g = back.slides[1].shapes.iter().find(|x| x.diagram.is_some()).expect("a SmartArt group");
let g = back.slides[1].shapes.iter().find(|x| x.diagram.is_some()).expect("a Diagram group");
let d = g.diagram.as_ref().unwrap();
assert_eq!(d.kind, "hierarchy");
assert_eq!((d.style.as_str(), d.colors.as_str()), ("intense", "accent3"), "SmartArt style and colours come back");
assert_eq!((d.style.as_str(), d.colors.as_str()), ("intense", "accent3"), "Diagram style and colours come back");
assert!(d.imported);
let outline: Vec<(&str, u8)> = d.nodes.iter().map(|n| (n.text.as_str(), n.level)).collect();
assert_eq!(outline, [("CEO", 0), ("CTO", 1), ("Dev", 2), ("CFO", 1)]);
@@ -4172,7 +4431,7 @@ fn smartart_pictures_3d_styles_and_colour_variants() {
let pics = |s: &Session| -> Vec<deckcraft_model::Shape> {
let st = s.doc().unwrap();
let g = st.doc.slides[1].shapes.iter().find(|x| x.diagram.is_some()).unwrap().clone();
g.children().iter().filter(|c| c.name == "SmartArt picture").cloned().collect()
g.children().iter().filter(|c| c.name == "Diagram picture").cloned().collect()
};
assert_eq!(pics(&s).len(), 2, "one picture placeholder per top-level node");
assert!(pics(&s).iter().all(|p| !matches!(p.fill, Some(deckcraft_model::Fill::Picture(_)))));
@@ -5433,6 +5692,38 @@ fn pen_pressure_is_kept_and_a_mouse_draws_at_full_width() {
assert_eq!(got, vec![vec![0.2, 1.0, 0.4], vec![1.0, 1.0]]);
}
#[test]
fn a_long_ink_stroke_is_extended_in_place_and_capped() {
let mut s = session();
s.set_tool(tools::ToolKind::Ink { mode: "pen".into(), color: deckcraft_model::Rgba::rgb(20, 20, 20), width: 4.0 });
let ev = |kind, x: f64| tools::PointerEvent { kind, x, y: 80.0, mods: Mods::default(), tol: 3.0, pressure: Some(0.5) };
s.pointer(ev(PointerKind::Down, 0.0)).unwrap();
let n = tools::MAX_GESTURE_POINTS + 5_000;
for i in 1..n {
s.pointer(ev(PointerKind::Drag, (i % 500) as f64)).unwrap();
}
assert_eq!(s.tool.ink_preview.len(), tools::MAX_GESTURE_POINTS);
s.pointer(ev(PointerKind::Up, 0.0)).unwrap();
let kept: usize = ink_pressures(&s).iter().map(Vec::len).sum();
assert!((2..=tools::MAX_GESTURE_POINTS).contains(&kept), "{kept}");
}
#[test]
fn double_clicking_a_capped_freeform_keeps_its_vertices() {
let mut s = session();
let ev = |kind, x: f64, y: f64| tools::PointerEvent { kind, x, y, mods: Default::default(), tol: 3.0, pressure: None };
s.set_tool(tools::ToolKind::Shape { preset: "freeform".into() });
s.pointer(ev(tools::PointerKind::Down, 100.0, 100.0)).unwrap();
// Densely clicked vertices, each within the double-click tolerance of the last.
for i in 0..tools::MAX_GESTURE_POINTS + 10 {
s.pointer(ev(tools::PointerKind::Down, 300.0 + i as f64 * 0.001, 100.0)).unwrap();
}
s.pointer(ev(tools::PointerKind::DoubleClick, 300.0, 100.0)).unwrap();
let st = s.doc().unwrap();
let deckcraft_model::Geom::Custom { paths } = &st.shapes().last().unwrap().geom else { panic!("no freeform") };
assert_eq!(paths[0].d.matches(" L ").count(), tools::MAX_GESTURE_POINTS - 1);
}
fn ink_pressures(s: &Session) -> Vec<Vec<f32>> {
let st = s.doc().unwrap();
let Some(sh) = st.shapes().iter().rev().find(|sh| matches!(sh.kind, deckcraft_model::ShapeKind::Ink { .. })) else {
@@ -6543,6 +6834,66 @@ fn duplicating_a_moved_copy_repeats_the_move() {
assert_eq!(s.doc().unwrap().doc.validate(), Vec::<String>::new());
}
/// Duplicating a shape inside a group puts the copy in that group, last among its children, and
/// selects it. The copy is offset on the slide as a top-level copy is, however the group is moved,
/// stretched or turned, and duplicating a moved copy repeats the move (#966). A shape whose group is
/// duplicated as well goes along with the group's copy. Before, Duplicate looked only at the slide's
/// top level: it answered `{ids: []}` and changed nothing.
#[test]
fn duplicating_a_shape_in_a_group_copies_it_into_the_group() {
fn rect(s: &mut Session, x: f64) -> u64 {
s.execute("shape.insert", &json!({"preset": "rect", "rect": [x, 100, 100, 100]})).unwrap()["id"].as_u64().unwrap()
}
let sid = |id: u64| ShapeId(u32::try_from(id).unwrap());
// How far shape `to` is drawn from shape `from` on the slide, the same for each of its sites.
let offset = |s: &Session, from: u64, to: u64| {
let st = s.doc().unwrap();
let sites = |id| connect::sites_of(&st.doc, &st.selection, st.shapes(), sid(id)).unwrap();
let (p, q) = (sites(from), sites(to));
assert_eq!(p.len(), q.len(), "{p:?} {q:?}");
let d = q[0] - p[0];
assert!(p.iter().zip(&q).all(|(p, q)| (*q - *p - d).hypot() < 1e-6), "{p:?} {q:?}");
(d.x, d.y)
};
let grouped = |edit: &str, p: Value| {
let mut s = session();
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
let (a, b) = (rect(&mut s, 100.0), rect(&mut s, 300.0));
let g = s.execute("arrange.group", &json!({"ids": [a, b]})).unwrap()["id"].as_u64().unwrap();
let mut p = p;
p["ids"] = json!([g]);
s.execute(edit, &p).unwrap();
(s, a, b, g)
};
for (edit, p) in [("shape.move", json!({"dx": 200})), ("shape.resize", json!({"w": 600})), ("shape.rotate", json!({"deg": 90}))] {
let (mut s, a, b, g) = grouped(edit, p);
let off = s.prefs.duplicate_offset;
let r = s.execute("edit.duplicate", &json!({"ids": [a]})).unwrap();
assert_eq!(r["ids"].as_array().map(Vec::len), Some(1), "{edit}: {r}");
let c = r["ids"][0].as_u64().unwrap();
assert_eq!(s.doc().unwrap().selection.shapes, [sid(c)]);
let st = s.doc().unwrap();
let order: Vec<ShapeId> = deckcraft_model::find_shape(st.shapes(), sid(g)).unwrap().children().iter().map(|x| x.id).collect();
assert_eq!(order, [sid(a), sid(b), sid(c)], "{edit}");
let (dx, dy) = offset(&s, a, c);
assert!((dx - off).abs() < 1e-6 && (dy - off).abs() < 1e-6, "{edit}: the copy is drawn ({dx}, {dy}) from the original");
// Moved, then duplicated again: the next copy is drawn as far from it again.
s.execute("shape.move", &json!({"ids": [c], "dx": 30})).unwrap();
let (mx, my) = offset(&s, a, c);
let d = s.execute("edit.duplicate", &json!({"ids": [c]})).unwrap()["ids"][0].as_u64().unwrap();
let (nx, ny) = offset(&s, c, d);
assert!((nx - mx).abs() < 1e-6 && (ny - my).abs() < 1e-6, "{edit}: moved ({mx}, {my}), then ({nx}, {ny})");
assert_eq!(s.doc().unwrap().doc.validate(), Vec::<String>::new());
}
let (mut s, a, _, g) = grouped("shape.move", json!({"dx": 200}));
let r = s.execute("edit.duplicate", &json!({"ids": [a, g]})).unwrap();
assert_eq!(r["ids"].as_array().map(Vec::len), Some(1), "{r}");
let st = s.doc().unwrap();
let count = |id: u64| deckcraft_model::find_shape(st.shapes(), sid(id)).unwrap().children().len();
assert_eq!((count(g), count(r["ids"][0].as_u64().unwrap())), (2, 2));
assert_eq!(st.shapes().len(), 2);
}
fn chart_of(s: &Session, id: u64) -> deckcraft_model::Chart {
match &s.doc().unwrap().shape(deckcraft_model::ShapeId(id as u32)).unwrap().kind {
deckcraft_model::ShapeKind::Chart(c) => (**c).clone(),
@@ -6870,6 +7221,70 @@ fn master_body_placeholder_format_keeps_per_level_formatting() {
assert_eq!(shown(&s), vec![Some(28.0), Some(24.0)]);
}
/// #2059: Decrease Font Size (Ctrl+[, Cmd+Shift+,) steps down one size at a time from any size:
/// PowerPoint's list up to 96, then 8 points at a time above it, as Increase Font Size steps up.
/// A size above 66 used to shrink straight to 66.
#[test]
fn shrinking_a_large_font_steps_down_one_size_at_a_time() {
let mut s = session();
let r = s.execute("shape.insert", &json!({"preset": "rect", "rect": [80.0, 80.0, 420.0, 160.0], "text": "Big"})).unwrap();
let id = deckcraft_model::ShapeId(u32::try_from(r["id"].as_u64().unwrap()).unwrap());
let size = |s: &Session| s.doc().unwrap().shape(id).unwrap().text.as_ref().unwrap().paragraphs[0].runs[0].props.size;
s.execute("format.size", &json!({"size": 120})).unwrap();
let mut down = vec![];
for _ in 0..9 {
s.execute("format.shrink", &json!({})).unwrap();
down.push(size(&s).unwrap());
}
assert_eq!(down, [112.0, 104.0, 96.0, 88.0, 80.0, 72.0, 66.0, 60.0, 54.0]);
let mut up = vec![];
for _ in 0..7 {
s.execute("format.grow", &json!({})).unwrap();
up.push(size(&s).unwrap());
}
assert_eq!(up, [60.0, 66.0, 72.0, 80.0, 88.0, 96.0, 104.0]);
// A size between list steps goes to the next one down, and one past the top steps 8 at most.
s.execute("format.size", &json!({"size": 99})).unwrap();
s.execute("format.shrink", &json!({})).unwrap();
assert_eq!(size(&s), Some(96.0));
s.execute("format.size", &json!({"size": 150})).unwrap();
s.execute("format.shrink", &json!({})).unwrap();
assert_eq!(size(&s), Some(142.0));
}
/// A shape named many times in one edit is edited once and not copied per mention.
#[test]
fn repeated_target_ids_edit_the_shape_once() {
let mut s = session();
let r = s.execute("shape.insert", &json!({"preset": "rect", "rect": [100, 100, 300, 100], "text": "x".repeat(100_000)})).unwrap();
let id = r["id"].as_u64().unwrap();
let edited = s.execute("shape.fill", &json!({"ids": vec![id; 50_000], "color": "#ff0000"})).unwrap();
assert_eq!(edited, json!(1));
let v = s.execute("shape.inspect", &json!({"id": id})).unwrap();
assert_eq!(v["shape"]["fill"]["color"]["base"]["rgb"], json!({"a": 255, "b": 0, "g": 0, "r": 255}));
}
#[test]
fn many_distinct_fonts_are_listed_once_each_without_a_quadratic_scan() {
// 30,000 runs in 15,000 families, each written twice in different case.
let mut p = deckcraft_model::Presentation::default();
let slide = std::sync::Arc::make_mut(&mut p.slides[0]);
let mut body = deckcraft_model::text::TextBody::from_text("x");
let run = body.paragraphs[0].runs[0].clone();
body.paragraphs[0].runs = (0..30_000)
.map(|i| {
let mut r = run.clone();
let name = format!("Qqq Font {}", i / 2);
r.props.font = Some(if i % 2 == 0 { name } else { name.to_uppercase() });
r
})
.collect();
slide.shapes[0].text = Some(body);
let used = crate::fonts::used_fonts(&p);
assert_eq!(used.iter().filter(|f| f.starts_with("Qqq Font ")).count(), 15_000);
assert_eq!(crate::fonts::missing_fonts(&p).iter().filter(|f| f.to_ascii_lowercase().starts_with("qqq font ")).count(), 15_000);
}
#[test]
fn slide_master_view_rejects_invalid_indices() {
let mut s = Session::with_new();
@@ -6970,13 +7385,13 @@ fn flip_is_refused_on_a_smartart_graphic_and_leaves_it_a_graphic() {
let gid = s.execute("smartArt.fromText", &json!({"kind": "blockList"})).unwrap()["id"].as_u64().unwrap();
for cmd in ["arrange.flipHorizontal", "arrange.flipVertical", "shape.flip"] {
let e = s.execute(cmd, &json!({"ids": [gid]})).unwrap_err().to_string();
assert!(e.contains("SmartArt") && e.contains("smartArt.layout"), "{cmd}: {e}");
assert!(e.contains("Diagram") && e.contains("smartArt.layout"), "{cmd}: {e}");
}
// Grouped with another shape it cannot be flipped either: ungrouping would hand it the flip.
let other = s.execute("insert.textBox", &json!({"text": "x"})).unwrap()["id"].as_u64().unwrap();
let group = s.execute("arrange.group", &json!({"ids": [gid, other]})).unwrap()["id"].as_u64().unwrap();
let e = s.execute("shape.flip", &json!({"ids": [group]})).unwrap_err().to_string();
assert!(e.contains("SmartArt"), "{e}");
assert!(e.contains("Diagram"), "{e}");
s.execute("arrange.ungroup", &json!({"ids": [group]})).unwrap();
// Still a SmartArt graphic: smartArt commands keep working.
s.execute("smartArt.layout", &json!({"id": gid, "kind": "cycle"})).unwrap();
+3
View File
@@ -17,6 +17,7 @@ mod geometry;
mod hit;
mod pointer;
pub(crate) use drag::MAX_GESTURE_POINTS;
pub use hit::{line_xfrm, resize_box, table_rows, text_pos};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
@@ -275,6 +276,8 @@ pub struct ToolState {
pub path_preview: Vec<Point>,
pub path_smooth: bool,
pub path_closing: bool,
/// Whether the last freeform press added a vertex (it adds none at the point cap).
pub press_added_vertex: bool,
/// The shape in Edit Points: its vertices are handles instead of the box's.
pub edit_points: Option<ShapeId>,
/// The vertex (path, index) whose control handles show in Edit Points.
+6 -2
View File
@@ -4,8 +4,12 @@ use super::{Drag, Hit, Point, Result, Session, ShapeId, ShapeKind, TextSel, Tool
impl Session {
pub(super) fn double_click(&mut self, p: Point, tol: f64) -> Result<Value> {
if let Some(Drag::Freeform { mode, mut points, .. }) = self.tool.drag.clone() {
// The double-click's own click added a duplicate vertex.
while points.len() > 2 && points.last().zip(points.get(points.len() - 2)).is_some_and(|(a, b)| (*a - *b).hypot() <= tol) {
// The double-click's own click added a duplicate vertex (none at the point cap, where
// the existing vertices are all kept).
while self.tool.press_added_vertex
&& points.len() > 2
&& points.last().zip(points.get(points.len() - 2)).is_some_and(|(a, b)| (*a - *b).hypot() <= tol)
{
points.pop();
}
self.tool.drag = Some(Drag::Freeform { mode, points, pressed: false });
+22 -6
View File
@@ -12,9 +12,16 @@ struct MoveDrag {
enter_text: Option<(ShapeId, Point)>,
}
/// Samples one ink or freeform gesture keeps; later pointer moves extend it no further.
pub(crate) const MAX_GESTURE_POINTS: usize = 100_000;
impl Session {
/// Update the active drag interaction using the pointer position and modifiers.
pub(super) fn drag(&mut self, p: Point, mods: Mods, tol: f64) -> Result<Value> {
if matches!(self.tool.drag, Some(Drag::Ink { .. })) {
self.extend_ink(p);
return Ok(Value::Null);
}
let Some(drag) = self.tool.drag.clone() else { return Ok(Value::Null) };
match drag {
Drag::Guide { index } => {
@@ -181,7 +188,7 @@ impl Session {
}
Drag::Freeform { mode, mut points, pressed } => {
// Dragging scribbles (except for curves, which only take clicked points).
if pressed && mode != "curve" && points.last().is_none_or(|l| (*l - p).hypot() >= 1.5) {
if pressed && mode != "curve" && points.len() < MAX_GESTURE_POINTS && points.last().is_none_or(|l| (*l - p).hypot() >= 1.5) {
points.push(p);
}
let mut v = points.clone();
@@ -191,15 +198,24 @@ impl Session {
self.tool.path_preview = v;
self.tool.drag = Some(Drag::Freeform { mode, points, pressed });
}
Drag::Ink { mut points } => {
points.push(self.ink_sample(p));
self.tool.ink_preview = points.clone();
self.tool.drag = Some(Drag::Ink { points });
}
// Handled above.
Drag::Ink { .. } => {}
}
Ok(Value::Null)
}
/// Ink grows by one sample per move: extend it (and its preview) in place rather than
/// copying the whole stroke twice per event, up to [`MAX_GESTURE_POINTS`].
fn extend_ink(&mut self, p: Point) {
let sample = self.ink_sample(p);
if let Some(Drag::Ink { points }) = self.tool.drag.as_mut()
&& points.len() < MAX_GESTURE_POINTS
{
points.push(sample);
self.tool.ink_preview.push(sample);
}
}
fn drag_move(&mut self, drag: MoveDrag, p: Point, mods: Mods, tol: f64) -> Result<Value> {
let MoveDrag { ids, start, origs, moved, duplicate, enter_text } = drag;
let mut d = p - start;
+5 -2
View File
@@ -228,7 +228,10 @@ impl Session {
self.tool.drag = Some(Drag::Freeform { mode, points, pressed: false });
return self.finish_freeform(true);
}
points.push(p);
self.tool.press_added_vertex = points.len() < super::MAX_GESTURE_POINTS;
if self.tool.press_added_vertex {
points.push(p);
}
self.tool.path_preview = points.clone();
self.tool.drag = Some(Drag::Freeform { mode, points, pressed: true });
return Ok(Value::Null);
@@ -350,7 +353,7 @@ impl Session {
bp.warp.map(|n| (n, bp.warp_adj))
});
if let Some((name, base)) = found {
self.begin("Adjust WordArt");
self.begin("Adjust Decorative Text");
self.tool.drag = Some(Drag::Warp { id, index, name, base, orig });
}
}
+25 -2
View File
@@ -169,8 +169,14 @@ impl FontDb {
if bundled.contains(fam) {
continue;
}
if self.is_loaded(fam) || self.has_family(fam) {
let f = self.face(fam, style);
if self.is_loaded(fam) || (self.system_fallback_enabled() && self.has_family(fam)) {
// With system fallback off, only a face that is already loaded: `face` could load more catalog files.
let f = if self.system_fallback_enabled() {
self.face(fam, style)
} else {
let Some(f) = self.find(fam, style) else { continue };
f
};
if f.family.eq_ignore_ascii_case(fam) && covers_cluster(&f, cluster) {
return f;
}
@@ -311,4 +317,21 @@ mod tests {
assert!(c.windows(2).all(|w| w[0] <= w[1]));
}
}
/// With system fallback off, the automatic cascades (script families and symbol faces) load no
/// installed family: the result comes from what is already loaded.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn cascades_load_no_installed_family_when_system_fallback_is_off() {
let db = FontDb::with_font_dirs(Vec::new());
db.set_system_fallback(false);
let base = db.face(crate::DEFAULT_FAMILY, "Regular");
let loaded = |db: &FontDb| [b"Arab", b"Hebr", b"Thai", b"Deva"].into_iter().flat_map(|s| os_families(*s)).filter(|f| db.is_loaded(f)).count();
let before = loaded(&db);
for c in ['\u{0628}', '\u{05D0}', '\u{0E01}', '\u{0905}', '\u{2211}', '\u{2603}'] {
let f = db.cascade(&[c], std::slice::from_ref(&base), "Regular");
assert!(!f.family.is_empty());
}
assert_eq!(loaded(&db), before, "no installed family was loaded");
}
}
+14 -2
View File
@@ -976,6 +976,17 @@ impl FontDb {
let _ = on;
}
/// Whether automatic system-font lookups (fallback by character, symbol and script families)
/// may touch installed fonts. Lookups of a family the document or host names are not gated.
pub(crate) fn system_fallback_enabled(&self) -> bool {
#[cfg(not(target_arch = "wasm32"))]
{
self.sys.lock().unwrap_or_else(std::sync::PoisonError::into_inner).enabled
}
#[cfg(target_arch = "wasm32")]
false
}
/// Process-wide database: the bundled fonts, then the installed system fonts, cataloged the
/// first time a lookup by family name needs them (or ahead of time by
/// [`FontDb::scan_in_background`]).
@@ -1287,7 +1298,7 @@ impl FontDb {
Some(f)
}
fn find(&self, family: &str, style: &str) -> Option<Arc<FontFace>> {
pub(crate) fn find(&self, family: &str, style: &str) -> Option<Arc<FontFace>> {
if style.contains('{') {
{
let faces = self.read_faces();
@@ -1473,7 +1484,8 @@ impl FontDb {
};
#[cfg(not(target_arch = "wasm32"))]
{
let cataloged: Vec<String> = self.read_catalog().iter().map(|e| e.family.clone()).collect();
let cataloged: Vec<String> =
if self.system_fallback_enabled() { self.read_catalog().iter().map(|e| e.family.clone()).collect() } else { Vec::new() };
for fam in SYSTEM_FALLBACKS.iter().filter(|f| f.contains("Symbol")) {
if !self.is_loaded(fam) && cataloged.iter().any(|f| f.eq_ignore_ascii_case(fam)) {
self.load_cataloged(fam);
+8
View File
@@ -425,3 +425,11 @@ fn the_font_scan_skips_fifos_folders_and_link_loops() {
assert!(fifo || n >= 1);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn disabling_system_fallback_gates_automatic_family_loads() {
let db = FontDb::with_font_dirs(Vec::new());
assert!(cfg!(target_arch = "wasm32") || db.system_fallback_enabled());
db.set_system_fallback(false);
assert!(!db.system_fallback_enabled());
}
-19
View File
@@ -1,19 +0,0 @@
[package]
name = "deckcraft-h264"
description = "Clean-room pure-Rust H.264/AVC decoder (ITU-T H.264)"
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
[lints]
workspace = true
[features]
default = ["threads"]
threads = ["dep:rayon"]
[dependencies]
deckcraft-bitstream = { workspace = true }
thiserror = { workspace = true }
rayon = { workspace = true, optional = true }
-335
View File
@@ -1,335 +0,0 @@
//! CABAC arithmetic decoding engine (9.3.1.2, 9.3.3.2) and context initialisation (9.3.1.1).
use crate::cabac_tables::CABAC_INIT_MN;
use crate::error::{Result, ensure};
/// Table 9-44 rangeTabLPS[pStateIdx][qCodIRangeIdx].
#[rustfmt::skip]
pub(crate) static RANGE_TAB_LPS: [[u8; 4]; 64] = [
[128, 176, 208, 240], [128, 167, 197, 227], [128, 158, 187, 216], [123, 150, 178, 205],
[116, 142, 169, 195], [111, 135, 160, 185], [105, 128, 152, 175], [100, 122, 144, 166],
[95, 116, 137, 158], [90, 110, 130, 150], [85, 104, 123, 142], [81, 99, 117, 135],
[77, 94, 111, 128], [73, 89, 105, 122], [69, 85, 100, 116], [66, 80, 95, 110],
[62, 76, 90, 104], [59, 72, 86, 99], [56, 69, 81, 94], [53, 65, 77, 89],
[51, 62, 73, 85], [48, 59, 69, 80], [46, 56, 66, 76], [43, 53, 63, 72],
[41, 50, 59, 69], [39, 48, 56, 65], [37, 45, 54, 62], [35, 43, 51, 59],
[33, 41, 48, 56], [32, 39, 46, 53], [30, 37, 43, 50], [29, 35, 41, 48],
[27, 33, 39, 45], [26, 31, 37, 43], [24, 30, 35, 41], [23, 28, 33, 39],
[22, 27, 32, 37], [21, 26, 30, 35], [20, 24, 29, 33], [19, 23, 27, 31],
[18, 22, 26, 30], [17, 21, 25, 28], [16, 20, 23, 27], [15, 19, 22, 25],
[14, 18, 21, 24], [14, 17, 20, 23], [13, 16, 19, 22], [12, 15, 18, 21],
[12, 14, 17, 20], [11, 14, 16, 19], [11, 13, 15, 18], [10, 12, 15, 17],
[10, 12, 14, 16], [9, 11, 13, 15], [9, 11, 12, 14], [8, 10, 12, 14],
[8, 9, 11, 13], [7, 9, 11, 12], [7, 9, 10, 12], [7, 8, 10, 11],
[6, 8, 9, 11], [6, 7, 9, 10], [6, 7, 8, 9], [2, 2, 2, 2],
];
/// Table 9-45 transIdxLPS.
#[rustfmt::skip]
static TRANS_IDX_LPS: [u8; 64] = [
0, 0, 1, 2, 2, 4, 4, 5, 6, 7, 8, 9, 9, 11, 11, 12,
13, 13, 15, 15, 16, 16, 18, 18, 19, 19, 21, 21, 22, 22, 23, 24,
24, 25, 26, 26, 27, 27, 28, 29, 29, 30, 30, 30, 31, 32, 32, 33,
33, 33, 34, 34, 35, 35, 35, 36, 36, 36, 37, 37, 37, 38, 38, 63,
];
/// Table 9-45 transIdxMPS.
#[rustfmt::skip]
static TRANS_IDX_MPS: [u8; 64] = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 62, 63,
];
/// Combined state transition: index = (pStateIdx << 1 | valMPS) * 2 + bin_is_lps.
/// Each context is stored as `pStateIdx << 1 | valMPS`.
pub(crate) static NEXT_STATE: [[u8; 2]; 128] = {
let mut t = [[0u8; 2]; 128];
let mut s = 0;
while s < 128 {
let p = s >> 1;
let mps = s & 1;
// MPS path
t[s][0] = (TRANS_IDX_MPS[p] << 1) | mps as u8;
// LPS path
let new_mps = if p == 0 { 1 - mps } else { mps };
t[s][1] = (TRANS_IDX_LPS[p] << 1) | new_mps as u8;
s += 1;
}
t
};
/// Number of context variables used (0..=1023).
pub const NUM_CTX: usize = 1024;
pub struct Cabac<'a> {
data: &'a [u8],
/// Byte position of the next byte to load into `value`.
next_byte: usize,
range: u32,
/// codIOffset scaled by 2^bits: value = (codIOffset << bits) | (bits already loaded but not yet
/// consumed by the arithmetic decoder).
value: u64,
bits: u32,
pub ctx: [u8; NUM_CTX],
}
impl<'a> Cabac<'a> {
/// Create an engine over `data` (slice RBSP) starting at bit `pos` (must be byte aligned),
/// with contexts initialised for `slice_qp` and `init` (0..=2 cabac_init_idc, 3 = I slice).
pub fn new(data: &'a [u8], pos: usize, slice_qp: i32, init: usize) -> Result<Self> {
let mut c = Cabac { data, next_byte: pos / 8, range: 0, value: 0, bits: 0, ctx: [0; NUM_CTX] };
c.init_contexts(slice_qp, init);
c.init_engine()?;
Ok(c)
}
pub fn init_contexts(&mut self, slice_qp: i32, init: usize) {
let qp = slice_qp.clamp(0, 51);
let tab = &CABAC_INIT_MN[init];
for (c, &(m, n)) in self.ctx.iter_mut().zip(tab.iter()) {
let pre = (((m as i32) * qp) >> 4) + n as i32;
let pre = pre.clamp(1, 126);
*c = if pre <= 63 { ((63 - pre) << 1) as u8 } else { (((pre - 64) << 1) | 1) as u8 };
}
// ctxIdx 276 (end_of_slice / I_PCM) is handled by decode_terminate.
}
/// 9.3.1.2: codIRange = 510, codIOffset = read_bits(9) (from the current byte-aligned position).
pub fn init_engine(&mut self) -> Result<()> {
self.range = 510;
self.value = 0;
self.bits = 0;
self.refill();
// the top 9 loaded bits form codIOffset; the remaining ones stay buffered below it
self.bits -= 9;
ensure!((self.value >> self.bits) < 510, "invalid CABAC offset at init");
Ok(())
}
/// Load 32 more bits (zeros past the end of the data).
#[inline(always)]
fn refill(&mut self) {
let b = self.next_byte;
let mut w = [0u8; 4];
if let Some(src) = self.data.get(b..b + 4) {
w.copy_from_slice(src);
} else if b < self.data.len() {
let n = self.data.len() - b;
w[..n].copy_from_slice(&self.data[b..]);
}
self.next_byte += 4;
self.value = (self.value << 32) | u32::from_be_bytes(w) as u64;
self.bits += 32;
}
/// Bit position of the next bit not consumed by the arithmetic decoder (for I_PCM alignment
/// after decode_terminate returned 1).
pub fn bit_pos(&self) -> usize {
self.next_byte * 8 - self.bits as usize
}
/// Continue reading at byte-aligned bit position `pos` (call `init_engine` afterwards).
pub fn set_bit_pos(&mut self, pos: usize) {
self.next_byte = pos / 8;
self.value = 0;
self.bits = 0;
}
pub fn data(&self) -> &'a [u8] {
self.data
}
/// True when the reader has run past the end of the data (corrupt stream).
pub fn overrun(&self) -> bool {
self.next_byte > self.data.len() + 16
}
#[inline(always)]
fn renorm(&mut self) {
// codIRange < 512 always, so the shift is 0 when no renormalisation is needed: no branch on
// the (unpredictable) range, only on the rare refill.
let shift = self.range.leading_zeros() - 23;
self.range <<= shift;
if self.bits < shift {
self.refill();
}
self.bits -= shift;
}
/// 9.3.3.2.1, branch-free: the MPS / LPS outcome of a context-coded bin is close to random
/// at high bit rates, so it selects values instead of jumping.
#[inline(always)]
pub fn decode_decision(&mut self, ctx_idx: usize) -> u32 {
// Every ctxIdx is < 1024 and every state < 128: the masks only let the compiler drop the
// bounds checks from the per-bin path.
let ctx_idx = ctx_idx & (NUM_CTX - 1);
let s = (self.ctx[ctx_idx] & 127) as usize;
let q = ((self.range >> 6) & 3) as usize;
let lps = RANGE_TAB_LPS[s >> 1][q] as u32;
let mps_range = self.range - lps;
let scaled = (mps_range as u64) << self.bits;
let is_lps = self.value >= scaled;
self.value -= scaled & (is_lps as u64).wrapping_neg();
self.range = if is_lps { lps } else { mps_range };
self.ctx[ctx_idx] = NEXT_STATE[s][is_lps as usize];
self.renorm();
(s & 1) as u32 ^ is_lps as u32
}
#[inline(always)]
pub fn decode_bypass(&mut self) -> u32 {
if self.bits == 0 {
self.refill();
}
self.bits -= 1;
let scaled = (self.range as u64) << self.bits;
let one = self.value >= scaled;
self.value -= scaled & (one as u64).wrapping_neg();
one as u32
}
pub fn decode_terminate(&mut self) -> u32 {
self.range -= 2;
let scaled = (self.range as u64) << self.bits;
if self.value >= scaled {
1
} else {
self.renorm();
0
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Minimal CABAC encoder (9.3.4, informative) used to round-trip test the decoder.
struct Enc {
low: u32,
range: u32,
outstanding: u32,
first: bool,
bits: Vec<bool>,
ctx: Vec<u8>,
}
impl Enc {
fn new(ctx: &[u8]) -> Self {
Enc { low: 0, range: 510, outstanding: 0, first: true, bits: vec![], ctx: ctx.to_vec() }
}
fn put(&mut self, b: bool) {
if self.first {
self.first = false;
} else {
self.bits.push(b);
}
while self.outstanding > 0 {
self.bits.push(!b);
self.outstanding -= 1;
}
}
fn renorm(&mut self) {
while self.range < 256 {
if self.low < 256 {
self.put(false);
} else if self.low >= 512 {
self.low -= 512;
self.put(true);
} else {
self.low -= 256;
self.outstanding += 1;
}
self.range <<= 1;
self.low <<= 1;
}
}
fn encode(&mut self, ctx_idx: usize, bin: u32) {
let s = self.ctx[ctx_idx] as usize;
let p = s >> 1;
let mps = (s & 1) as u32;
let q = ((self.range >> 6) & 3) as usize;
let lps = RANGE_TAB_LPS[p][q] as u32;
self.range -= lps;
if bin != mps {
self.low += self.range;
self.range = lps;
self.ctx[ctx_idx] = NEXT_STATE[s][1];
} else {
self.ctx[ctx_idx] = NEXT_STATE[s][0];
}
self.renorm();
}
fn bypass(&mut self, bin: u32) {
self.low <<= 1;
if bin != 0 {
self.low += self.range;
}
if self.low >= 1024 {
self.put(true);
self.low -= 1024;
} else if self.low < 512 {
self.put(false);
} else {
self.low -= 512;
self.outstanding += 1;
}
}
fn terminate_flush(&mut self) -> Vec<u8> {
// encode terminate bin = 1 then flush (9.3.4.5)
self.range -= 2;
self.low += self.range;
self.range = 2;
self.renorm();
self.put((self.low >> 9) & 1 != 0);
self.bits.push((self.low >> 8) & 1 != 0);
self.bits.push(true); // rbsp_stop_one_bit
while !self.bits.len().is_multiple_of(8) {
self.bits.push(false);
}
self.bits.chunks(8).map(|c| c.iter().fold(0u8, |a, &b| (a << 1) | b as u8)).collect()
}
}
#[test]
fn context_init_formula() {
let c = Cabac::new(&[0, 0, 0, 0], 0, 26, 3).unwrap();
// ctxIdx 0: m=20, n=-15 at QP 26: ((20*26)>>4) - 15 = 32 - 15 = 17 -> pState 46, MPS 0
assert_eq!(c.ctx[0], 46 << 1);
// ctxIdx 60: m=0,n=41 -> pre 41 -> pState 22 MPS 0
assert_eq!(c.ctx[60], 22 << 1);
// ctxIdx 70 (I): m=0 n=11 -> pState 52 MPS 0
assert_eq!(c.ctx[70], 52 << 1);
}
#[test]
fn round_trip_random_bins() {
let mut seed = 12345u32;
let mut rnd = || {
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
seed
};
let dummy = Cabac::new(&[0, 0, 0, 0], 0, 30, 0).unwrap();
let mut enc = Enc::new(&dummy.ctx);
let mut ops = Vec::new();
for _ in 0..5000 {
let kind = rnd() % 4;
let ctx = (rnd() % 16) as usize;
// skewed bins so contexts adapt
let bin = if rnd() % 10 < 8 { (ctx & 1) as u32 } else { 1 - (ctx & 1) as u32 };
if kind == 0 {
enc.bypass(bin);
} else {
enc.encode(ctx, bin);
}
ops.push((kind, ctx, bin));
}
let bytes = enc.terminate_flush();
let mut dec = Cabac::new(&bytes, 0, 30, 0).unwrap();
for (i, &(kind, ctx, bin)) in ops.iter().enumerate() {
let got = if kind == 0 { dec.decode_bypass() } else { dec.decode_decision(ctx) };
assert_eq!(got, bin, "bin {i}");
}
assert_eq!(dec.decode_terminate(), 1);
}
}
-689
View File
@@ -1,689 +0,0 @@
//! CABAC slice data and macroblock layer parsing (7.3.4, 7.3.5 with 9.3 binarisations and context
//! index derivations).
use crate::cabac::Cabac;
use crate::error::{Result, ensure, invalid};
use crate::mbtypes::{MbTypeInfo, Part, SubMbInfo, b_mb_type, b_sub_mb_type, intra_mb_type, p_mb_type, p_sub_mb_type};
use crate::picture::MbKind;
use crate::slice::SliceType;
use crate::slicedec::{MbCur, SliceDecoder};
/// Table 9-43: significant_coeff_flag ctxIdxInc for 8x8 frame blocks.
#[rustfmt::skip]
const SIG8_FRAME: [u8; 63] = [
0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5, 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9, 10, 9, 8, 7,
7, 6, 11, 12, 13, 11, 6, 7, 8, 9, 14, 10, 9, 8, 6, 11, 12, 13, 11, 6, 9, 14, 10, 9, 11, 12, 13, 11, 14, 10, 12,
];
/// Table 9-43: last_significant_coeff_flag ctxIdxInc for 8x8 blocks.
#[rustfmt::skip]
const LAST8: [u8; 63] = [
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8,
];
/// ctxIdxInc = levelListIdx (ctxBlockCat 0, 1, 2, 4).
const IDENT_CTX: [u8; 64] = {
let mut t = [0u8; 64];
let mut i = 0;
while i < 64 {
t[i] = i as u8;
i += 1;
}
t
};
/// ctxIdxInc = Min(levelListIdx / NumC8x8, 2) for 4:2:0 chroma DC (ctxBlockCat 3).
const CHROMA_DC_CTX: [u8; 4] = [0, 1, 2, 2];
/// Table 9-40 ctxIdxBlockCatOffset for ctxBlockCat 0..=5: (coded_block_flag, significant/last, abs level).
const CAT_OFFSET: [(usize, usize, usize); 6] = [(0, 0, 0), (4, 15, 10), (8, 29, 20), (12, 44, 30), (16, 47, 39), (0, 0, 0)];
impl SliceDecoder<'_> {
/// Decode slice_data() with CABAC. `rbsp` is the whole slice RBSP.
pub fn decode_cabac(&mut self, rbsp: &[u8]) -> Result<()> {
let start = self.sh.header_bits.div_ceil(8) * 8;
let init = if self.sh.slice_type.is_intra() { 3 } else { self.sh.cabac_init_idc as usize };
let mut c = Cabac::new(rbsp, start, self.sh.qp(self.pps), init)?;
let total = self.mb_w * self.mb_h;
let mut addr = self.sh.first_mb_in_slice as usize;
let intra_slice = self.sh.slice_type.is_intra();
loop {
ensure!(addr < total, "macroblock address {addr} out of range");
self.start_mb(addr);
let skip = !intra_slice && self.decode_skip_flag(&mut c);
if skip {
self.decode_skip()?;
} else {
self.decode_mb_cabac(&mut c)?;
}
self.pic.mb_done(addr);
ensure!(!c.overrun(), "CABAC read past end of slice data");
if c.decode_terminate() == 1 {
break;
}
addr += 1;
}
Ok(())
}
fn decode_skip_flag(&mut self, c: &mut Cabac) -> bool {
let cond = |n: Option<usize>| n.map(|m| !self.pic.mbs[m].kind_is_skip()).unwrap_or(false) as usize;
let inc = cond(self.nb[0]) + cond(self.nb[1]);
let base = if self.sh.slice_type.is_b() { 24 } else { 11 };
c.decode_decision(base + inc) == 1
}
/// Intra mb_type suffix / I-slice mb_type (Table 9-36). `base` is 3 (I slice) or 17/32 (prefix
/// offsets in P/B slices). Returns the I-slice mb_type value 0..=25.
fn decode_mb_type_i(&mut self, c: &mut Cabac, base: usize) -> u32 {
let islice = base == 3;
let b0 = if islice {
let cond = |n: Option<usize>| n.map(|m| !matches!(self.pic.mbs[m].kind, MbKind::I4x4 | MbKind::I8x8)).unwrap_or(false) as usize;
let inc = cond(self.nb[0]) + cond(self.nb[1]);
c.decode_decision(base + inc)
} else {
c.decode_decision(base)
};
if b0 == 0 {
return 0;
}
if c.decode_terminate() == 1 {
return 25;
}
let (c_luma, c_chroma, c_chroma2, c_pred0, c_pred1) =
if islice { (base + 3, base + 4, base + 5, base + 6, base + 7) } else { (base + 1, base + 2, base + 2, base + 3, base + 3) };
let luma = c.decode_decision(c_luma);
let mut chroma = c.decode_decision(c_chroma);
if chroma != 0 {
chroma += c.decode_decision(c_chroma2);
}
let p0 = c.decode_decision(c_pred0);
let p1 = c.decode_decision(c_pred1);
1 + (p0 * 2 + p1) + 4 * chroma + 12 * luma
}
fn decode_mb_type(&mut self, c: &mut Cabac) -> Result<MbTypeInfo> {
let info = match self.sh.slice_type {
SliceType::I | SliceType::Si => intra_mb_type(self.decode_mb_type_i(c, 3)),
SliceType::P | SliceType::Sp => {
if c.decode_decision(14) == 1 {
intra_mb_type(self.decode_mb_type_i(c, 17))
} else if c.decode_decision(15) == 0 {
p_mb_type(if c.decode_decision(16) == 0 { 0 } else { 3 })
} else {
p_mb_type(if c.decode_decision(17) == 1 { 1 } else { 2 })
}
}
SliceType::B => {
let cond =
|n: Option<usize>| n.map(|m| !matches!(self.pic.mbs[m].kind, MbKind::BSkip | MbKind::BDirect16x16)).unwrap_or(false) as usize;
let inc = cond(self.nb[0]) + cond(self.nb[1]);
if c.decode_decision(27 + inc) == 0 {
b_mb_type(0)
} else if c.decode_decision(27 + 3) == 0 {
b_mb_type(1 + c.decode_decision(27 + 5))
} else {
let b2 = c.decode_decision(27 + 4);
let b3 = c.decode_decision(27 + 5);
let b4 = c.decode_decision(27 + 5);
let b5 = c.decode_decision(27 + 5);
let v = (b2 << 3) | (b3 << 2) | (b4 << 1) | b5;
match v {
0b1101 => intra_mb_type(self.decode_mb_type_i(c, 32)),
0b1111 => b_mb_type(22),
0b1110 => b_mb_type(11),
_ if b2 == 0 => b_mb_type(3 + v),
_ => {
let b6 = c.decode_decision(27 + 5);
b_mb_type(12 + (((v & 7) << 1) | b6))
}
}
}
}
};
match info {
Some(i) => Ok(i),
None => invalid("invalid CABAC mb_type"),
}
}
fn decode_sub_mb_type(&mut self, c: &mut Cabac) -> Result<SubMbInfo> {
let r = if self.sh.slice_type.is_b() {
if c.decode_decision(36) == 0 {
b_sub_mb_type(0)
} else if c.decode_decision(37) == 0 {
b_sub_mb_type(1 + c.decode_decision(39))
} else if c.decode_decision(38) == 1 {
if c.decode_decision(39) == 1 {
b_sub_mb_type(11 + c.decode_decision(39))
} else {
let b4 = c.decode_decision(39);
let b5 = c.decode_decision(39);
b_sub_mb_type(7 + (b4 << 1) + b5)
}
} else {
let b3 = c.decode_decision(39);
let b4 = c.decode_decision(39);
b_sub_mb_type(3 + (b3 << 1) + b4)
}
} else if c.decode_decision(21) == 1 {
p_sub_mb_type(0)
} else if c.decode_decision(22) == 0 {
p_sub_mb_type(1)
} else {
p_sub_mb_type(if c.decode_decision(23) == 1 { 2 } else { 3 })
};
match r {
Some(s) => Ok(s),
None => invalid("invalid CABAC sub_mb_type"),
}
}
/// ref_idx_lX for the partition whose top-left 4x4 block is (x, y) (9.3.3.1.1.6).
fn decode_ref_idx(&mut self, c: &mut Cabac, list: usize, x: usize, y: usize) -> u32 {
let cond = |xx: i32, yy: i32| -> usize {
let (mb, bx, by) = if xx < 0 {
(self.nb[0], 3usize, yy as usize)
} else if yy < 0 {
(self.nb[1], xx as usize, 3usize)
} else {
(Some(self.mb_addr), xx as usize, yy as usize)
};
let Some(m) = mb else { return 0 };
let st = &self.pic.mbs[m];
let b8 = (by >> 1) * 2 + (bx >> 1);
if st.kind.is_intra() || st.kind_is_skip() || st.direct8x8 & (1 << b8) != 0 {
return 0;
}
(st.ref_idx[list][b8] > 0) as usize
};
let inc = cond(x as i32 - 1, y as i32) + 2 * cond(x as i32, y as i32 - 1);
let mut v = 0u32;
if c.decode_decision(54 + inc) == 1 {
v = 1;
if c.decode_decision(54 + 4) == 1 {
v = 2;
while c.decode_decision(54 + 5) == 1 {
v += 1;
if v > 32 {
break;
}
}
}
}
v
}
/// One mvd component for the (sub-)partition with top-left 4x4 block (x, y) (9.3.3.1.1.7, UEG3).
fn decode_mvd(&mut self, c: &mut Cabac, list: usize, comp: usize, x: usize, y: usize) -> i32 {
let absn = |xx: i32, yy: i32| -> u32 {
let (mb, bx, by) = if xx < 0 {
(self.nb[0], 3usize, yy as usize)
} else if yy < 0 {
(self.nb[1], xx as usize, 3usize)
} else {
(Some(self.mb_addr), xx as usize, yy as usize)
};
match mb {
Some(m) => self.pic.mbs[m].mvd[list][by * 4 + bx][comp] as u32,
None => 0,
}
};
let sum = absn(x as i32 - 1, y as i32) + absn(x as i32, y as i32 - 1);
let inc = if sum > 32 {
2
} else if sum > 2 {
1
} else {
0
};
let base = if comp == 0 { 40 } else { 47 };
if c.decode_decision(base + inc) == 0 {
return 0;
}
let mut prefix = 1;
while prefix < 9 && c.decode_decision(base + (prefix + 2).min(6)) == 1 {
prefix += 1;
}
let mut val = prefix as i32;
if prefix >= 9 {
// EG3 suffix
let mut k = 3;
while c.decode_bypass() == 1 {
val += 1 << k;
k += 1;
if k > 24 {
break;
}
}
while k > 0 {
k -= 1;
val += (c.decode_bypass() as i32) << k;
}
}
if c.decode_bypass() == 1 { -val } else { val }
}
fn set_cur_ref(&mut self, list: usize, x: usize, y: usize, w: usize, h: usize, r: i8) {
let st = self.mb_mut();
for yy in (y..y + h).step_by(2) {
for xx in (x..x + w).step_by(2) {
st.ref_idx[list][(yy >> 1) * 2 + (xx >> 1)] = r;
}
}
}
fn set_cur_mvd(&mut self, list: usize, x: usize, y: usize, w: usize, h: usize, d: [i32; 2]) {
let a = [d[0].unsigned_abs().min(127) as u8, d[1].unsigned_abs().min(127) as u8];
let st = self.mb_mut();
for yy in y..y + h {
for xx in x..x + w {
st.mvd[list][yy * 4 + xx] = a;
}
}
}
fn decode_cbp(&mut self, c: &mut Cabac) -> u8 {
let mut luma = 0u8;
for b8 in 0..4usize {
let (bx, by) = ((b8 & 1) * 2, (b8 >> 1) * 2);
// A
let ca = if bx > 0 { ((luma >> (b8 - 1)) & 1 == 0) as usize } else { self.cbp_luma_cond(self.nb[0], b8 + 1) };
let cb = if by > 0 { ((luma >> (b8 - 2)) & 1 == 0) as usize } else { self.cbp_luma_cond(self.nb[1], b8 + 2) };
let _ = (bx, by);
let bin = c.decode_decision(73 + ca + 2 * cb);
luma |= (bin as u8) << b8;
}
let cond = |n: Option<usize>, bin: usize| -> usize {
let Some(m) = n else { return 0 };
let st = &self.pic.mbs[m];
if st.kind == MbKind::IPcm {
return 1;
}
if st.kind_is_skip() {
return 0;
}
let ch = st.cbp >> 4;
if bin == 0 { (ch != 0) as usize } else { (ch == 2) as usize }
};
let inc0 = cond(self.nb[0], 0) + 2 * cond(self.nb[1], 0);
let mut chroma = 0u8;
if c.decode_decision(77 + inc0) == 1 {
let inc1 = cond(self.nb[0], 1) + 2 * cond(self.nb[1], 1) + 4;
chroma = 1 + c.decode_decision(77 + inc1) as u8;
}
luma | (chroma << 4)
}
/// condTermFlagN for coded_block_pattern luma bin from a neighbouring MB; `b8n` is the neighbouring
/// 8x8 block index within that MB.
fn cbp_luma_cond(&self, n: Option<usize>, b8n: usize) -> usize {
let Some(m) = n else { return 0 };
let st = &self.pic.mbs[m];
if st.kind == MbKind::IPcm {
return 0;
}
if st.kind_is_skip() {
return 1;
}
((st.cbp >> (b8n & 3)) & 1 == 0) as usize
}
fn decode_qp_delta(&mut self, c: &mut Cabac) -> Result<i32> {
let inc = self.prev_qp_delta_nz as usize;
if c.decode_decision(60 + inc) == 0 {
return Ok(0);
}
let mut k = 1u32;
let mut ctx = 62;
while c.decode_decision(ctx) == 1 {
k += 1;
ctx = 63;
ensure!(k <= 128, "mb_qp_delta too large");
}
let v = k.div_ceil(2) as i32;
Ok(if k % 2 == 1 { v } else { -v })
}
/// coded_block_flag ctxIdxInc condition for a neighbouring luma 4x4 block (cat 0..2) or chroma.
fn cbf_luma_cond(&self, bx: i32, by: i32, intra_cur: bool) -> usize {
if bx >= 0 && by >= 0 {
return (self.mb().nnz[(by * 4 + bx) as usize] != 0) as usize;
}
let n = if bx < 0 { self.nb[0] } else { self.nb[1] };
let Some(m) = n else { return intra_cur as usize };
let st = &self.pic.mbs[m];
let (x, y) = if bx < 0 { (3, by as usize) } else { (bx as usize, 3) };
(st.nnz[y * 4 + x] != 0) as usize
}
fn cbf_dc_cond(&self, n: Option<usize>, bit: u8, intra_cur: bool) -> usize {
let Some(m) = n else { return intra_cur as usize };
let st = &self.pic.mbs[m];
if st.kind == MbKind::IPcm {
return 1;
}
(st.cbf_dc & (1 << bit) != 0) as usize
}
fn cbf_chroma_ac_cond(&self, comp: usize, bx: i32, by: i32, intra_cur: bool) -> usize {
if bx >= 0 && by >= 0 {
return (self.mb().nnz_c[comp][(by * 2 + bx) as usize] != 0) as usize;
}
let n = if bx < 0 { self.nb[0] } else { self.nb[1] };
let Some(m) = n else { return intra_cur as usize };
let st = &self.pic.mbs[m];
let (x, y) = if bx < 0 { (1, by as usize) } else { (bx as usize, 1) };
(st.nnz_c[comp][y * 2 + x] != 0) as usize
}
/// residual_block_cabac for ctxBlockCat `cat` (0..=5). Coefficients are written to `coeff`
/// in list order (indices 0..max_num). `cbf_inc` is the coded_block_flag ctxIdxInc (ignored for cat 5).
/// Returns the number of non-zero coefficients.
fn residual_block_cabac(&mut self, c: &mut Cabac, coeff: &mut [i32], cat: usize, max_num: usize, cbf_inc: usize) -> u8 {
if cat != 5 && c.decode_decision(85 + CAT_OFFSET[cat].0 + cbf_inc) == 0 {
return 0;
}
let (sig_base, last_base, abs_base) =
if cat == 5 { (402, 417, 426) } else { (105 + CAT_OFFSET[cat].1, 166 + CAT_OFFSET[cat].1, 227 + CAT_OFFSET[cat].2) };
let (sig_tab, last_tab): (&[u8], &[u8]) = match cat {
3 => (&CHROMA_DC_CTX, &CHROMA_DC_CTX),
5 => (&SIG8_FRAME, &LAST8),
_ => (&IDENT_CTX, &IDENT_CTX),
};
// significance map: positions of the significant coefficients in scan order
let mut pos = [0u8; 64];
let mut n = 0usize;
let last_i = max_num - 1;
let mut found_last = false;
for i in 0..last_i {
if c.decode_decision(sig_base + sig_tab[i] as usize) == 1 {
pos[n] = i as u8;
n += 1;
if c.decode_decision(last_base + last_tab[i] as usize) == 1 {
found_last = true;
break;
}
}
}
if !found_last {
pos[n] = last_i as u8;
n += 1;
}
let mut eq1 = 0usize;
let mut gt1 = 0usize;
let gt1_cap = if cat == 3 { 3 } else { 4 };
for &p in pos[..n].iter().rev() {
let inc0 = if gt1 != 0 { 0 } else { (1 + eq1).min(4) };
let mut abs = 1i32;
if c.decode_decision(abs_base + inc0) == 1 {
let ctx = abs_base + 5 + gt1.min(gt1_cap);
let mut prefix = 1;
while prefix < 14 && c.decode_decision(ctx) == 1 {
prefix += 1;
}
abs = prefix + 1;
if prefix == 14 {
let mut k = 0;
let mut suf = 0i32;
while c.decode_bypass() == 1 {
suf += 1 << k;
k += 1;
if k > 24 {
break;
}
}
while k > 0 {
k -= 1;
suf += (c.decode_bypass() as i32) << k;
}
abs += suf;
}
gt1 += 1;
} else {
eq1 += 1;
}
coeff[p as usize] = if c.decode_bypass() == 1 { -abs } else { abs };
}
n as u8
}
fn residual_cabac(&mut self, c: &mut Cabac, cbp: u8) -> Result<()> {
let kind = self.mb().kind;
let intra = kind.is_intra();
let t8 = self.mb().transform_8x8;
if kind == MbKind::I16x16 {
let inc = self.cbf_dc_cond(self.nb[0], 0, intra) + 2 * self.cbf_dc_cond(self.nb[1], 0, intra);
let mut lv = [0i32; 16];
if self.residual_block_cabac(c, &mut lv, 0, 16, inc) > 0 {
self.mb_mut().cbf_dc |= 1;
self.put_luma_dc(&lv);
}
}
for b8 in 0..4 {
let (bx8, by8) = ((b8 & 1) * 2, (b8 >> 1) * 2);
if cbp & (1 << b8) == 0 {
continue;
}
if t8 {
let mut lv = [0i32; 64];
self.residual_block_cabac(c, &mut lv, 5, 64, 0);
let st = self.mb_mut();
for r in [by8 * 4 + bx8, by8 * 4 + bx8 + 1, (by8 + 1) * 4 + bx8, (by8 + 1) * 4 + bx8 + 1] {
st.nnz[r] = 1;
}
self.put_luma8(b8, &lv);
} else {
for i4 in 0..4 {
let (bx, by) = (bx8 + (i4 & 1), by8 + (i4 >> 1));
let raster = by * 4 + bx;
let inc = self.cbf_luma_cond(bx as i32 - 1, by as i32, intra) + 2 * self.cbf_luma_cond(bx as i32, by as i32 - 1, intra);
let mut lv = [0i32; 16];
let n = if kind == MbKind::I16x16 {
self.residual_block_cabac(c, &mut lv[1..], 1, 15, inc)
} else {
self.residual_block_cabac(c, &mut lv, 2, 16, inc)
};
self.mb_mut().nnz[raster] = n;
if n > 0 {
self.put_luma4(raster, &lv, kind == MbKind::I16x16);
}
}
}
}
let cbp_c = cbp >> 4;
if cbp_c != 0 {
for comp in 0..2 {
let bit = 1 + comp as u8;
let inc = self.cbf_dc_cond(self.nb[0], bit, intra) + 2 * self.cbf_dc_cond(self.nb[1], bit, intra);
let mut lv = [0i32; 4];
if self.residual_block_cabac(c, &mut lv, 3, 4, inc) > 0 {
self.mb_mut().cbf_dc |= 1 << bit;
self.put_chroma_dc(comp, &lv);
}
}
}
if cbp_c == 2 {
for comp in 0..2 {
for b in 0..4 {
let (bx, by) = ((b & 1) as i32, (b >> 1) as i32);
let inc = self.cbf_chroma_ac_cond(comp, bx - 1, by, intra) + 2 * self.cbf_chroma_ac_cond(comp, bx, by - 1, intra);
let mut lv = [0i32; 16];
let n = self.residual_block_cabac(c, &mut lv[1..], 4, 15, inc);
self.mb_mut().nnz_c[comp][b] = n;
if n > 0 {
self.put_chroma_ac(comp, b, &lv);
}
}
}
}
Ok(())
}
/// Parse and reconstruct one macroblock_layer() with CABAC.
fn decode_mb_cabac(&mut self, c: &mut Cabac) -> Result<()> {
let mut info = self.decode_mb_type(c)?;
self.cur = MbCur { info, ..Default::default() };
if info.kind == MbKind::IPcm {
// pcm alignment + samples, then re-initialise the arithmetic decoder
let pos = c.bit_pos().div_ceil(8);
let data = c.data();
ensure!(data.len() >= pos + 384, "truncated I_PCM macroblock");
let Some(&samples) = data.get(pos..).and_then(|d| d.first_chunk::<384>()) else {
return invalid("truncated I_PCM macroblock");
};
c.set_bit_pos((pos + 384) * 8);
c.init_engine()?;
self.finish_pcm();
self.write_pcm(&samples);
self.prev_qp_delta_nz = false;
return Ok(());
}
let intra = info.kind.is_intra();
if info.kind == MbKind::I4x4 && self.pps.transform_8x8_mode && self.decode_t8x8_flag(c) {
info.kind = MbKind::I8x8;
self.cur.info.kind = MbKind::I8x8;
self.mb_mut().transform_8x8 = true;
}
self.mb_mut().kind = info.kind;
let n_ref = self.sh.num_ref_idx_active;
match info.kind {
MbKind::I4x4 | MbKind::I8x8 => {
let n = if info.kind == MbKind::I8x8 { 4 } else { 16 };
for i in 0..n {
self.cur.rem_mode[i] = if c.decode_decision(68) == 1 {
-1
} else {
let b0 = c.decode_decision(69);
let b1 = c.decode_decision(69);
let b2 = c.decode_decision(69);
(b0 | (b1 << 1) | (b2 << 2)) as i8
};
}
self.derive_intra_modes(info.kind == MbKind::I8x8);
let m = self.decode_chroma_mode(c);
self.mb_mut().intra_chroma_mode = m;
}
MbKind::I16x16 => {
let m = self.decode_chroma_mode(c);
self.mb_mut().intra_chroma_mode = m;
}
MbKind::BDirect16x16 => {
self.mb_mut().direct8x8 = 0xf;
}
_ => {
if info.part == Part::P8x8 {
for p in 0..4 {
self.cur.sub[p] = self.decode_sub_mb_type(c)?;
}
let direct_mask: u8 = (0..4).filter(|&p| self.cur.sub[p].direct).map(|p| 1u8 << p).sum();
self.mb_mut().direct8x8 = direct_mask;
for l in 0..2 {
for p in 0..4 {
let sub = self.cur.sub[p];
if sub.direct || sub.pred & (1 << l) == 0 {
continue;
}
let (x, y) = ((p & 1) * 2, (p >> 1) * 2);
let r = if n_ref[l] > 1 && !info.ref0 { self.decode_ref_idx(c, l, x, y) as i8 } else { 0 };
self.cur.ref_idx[l][p] = r;
self.set_cur_ref(l, x, y, 2, 2, r);
}
}
for l in 0..2 {
for p in 0..4 {
let sub = self.cur.sub[p];
if sub.direct || sub.pred & (1 << l) == 0 {
continue;
}
let (x8, y8) = ((p & 1) * 2, (p >> 1) * 2);
for j in 0..sub.shape.num_parts() {
let (sx, sy, w, h) = sub.shape.rect(j);
let (x, y) = (x8 + sx, y8 + sy);
let dx = self.decode_mvd(c, l, 0, x, y);
let dy = self.decode_mvd(c, l, 1, x, y);
self.set_cur_mvd(l, x, y, w, h, [dx, dy]);
self.cur.mvd[l][p * 4 + j] = [dx as i16, dy as i16];
}
}
}
} else {
let np = info.part.num_parts();
for l in 0..2 {
for p in 0..np {
if info.pred[p] & (1 << l) == 0 {
continue;
}
let (x, y, w, h) = info.part.rect(p);
let r = if n_ref[l] > 1 { self.decode_ref_idx(c, l, x, y) as i8 } else { 0 };
self.cur.ref_idx[l][p] = r;
self.set_cur_ref(l, x, y, w, h, r);
}
}
for l in 0..2 {
for p in 0..np {
if info.pred[p] & (1 << l) == 0 {
continue;
}
let (x, y, w, h) = info.part.rect(p);
let dx = self.decode_mvd(c, l, 0, x, y);
let dy = self.decode_mvd(c, l, 1, x, y);
self.set_cur_mvd(l, x, y, w, h, [dx, dy]);
self.cur.mvd[l][p * 4] = [dx as i16, dy as i16];
}
}
}
}
}
let cbp = if info.kind == MbKind::I16x16 { info.i16_cbp } else { self.decode_cbp(c) };
self.cur.cbp = cbp;
self.mb_mut().cbp = cbp;
if !intra && cbp & 15 != 0 && self.pps.transform_8x8_mode && self.no_sub_8x8_lt() {
let t = self.decode_t8x8_flag(c);
self.mb_mut().transform_8x8 = t;
}
if !intra {
// ref_idx written during parsing is replaced by the derived motion
self.derive_inter_motion()?;
}
if cbp != 0 || info.kind == MbKind::I16x16 {
let d = self.decode_qp_delta(c)?;
self.apply_qp_delta(d)?;
self.prev_qp_delta_nz = d != 0;
} else {
self.prev_qp_delta_nz = false;
}
self.store_qp();
self.residual_cabac(c, cbp)?;
if intra {
self.reconstruct_intra();
} else {
self.predict_inter();
self.reconstruct_inter_residual();
}
Ok(())
}
fn decode_t8x8_flag(&mut self, c: &mut Cabac) -> bool {
let cond = |n: Option<usize>| n.map(|m| self.pic.mbs[m].transform_8x8).unwrap_or(false) as usize;
let inc = cond(self.nb[0]) + cond(self.nb[1]);
c.decode_decision(399 + inc) == 1
}
fn decode_chroma_mode(&mut self, c: &mut Cabac) -> u8 {
let cond = |n: Option<usize>| {
n.map(|m| {
let st = &self.pic.mbs[m];
st.kind.is_intra() && st.kind != MbKind::IPcm && st.intra_chroma_mode != 0
})
.unwrap_or(false) as usize
};
let inc = cond(self.nb[0]) + cond(self.nb[1]);
if c.decode_decision(64 + inc) == 0 {
return 0;
}
if c.decode_decision(67) == 0 {
return 1;
}
if c.decode_decision(67) == 0 { 2 } else { 3 }
}
}
-525
View File
@@ -1,525 +0,0 @@
// Generated from ITU-T H.264 (06/2026) Tables 9-12..9-33 (m, n) by a table-extraction script.
// Index: [0..=2] = cabac_init_idc for P/SP/B slices, [3] = I/SI slices. (0, 0) marks "na".
#[rustfmt::skip]
pub(crate) static CABAC_INIT_MN: [[(i8, i8); 1024]; 4] = [
[
(20, -15), (2, 54), (3, 74), (20, -15), (2, 54), (3, 74), (-28, 127), (-23, 104),
(-6, 53), (-1, 54), (7, 51), (23, 33), (23, 2), (21, 0), (1, 9), (0, 49),
(-37, 118), (5, 57), (-13, 78), (-11, 65), (1, 62), (12, 49), (-4, 73), (17, 50),
(18, 64), (9, 43), (29, 0), (26, 67), (16, 90), (9, 104), (-46, 127), (-20, 104),
(1, 67), (-13, 78), (-11, 65), (1, 62), (-6, 86), (-17, 95), (-6, 61), (9, 45),
(-3, 69), (-6, 81), (-11, 96), (6, 55), (7, 67), (-5, 86), (2, 88), (0, 58),
(-3, 76), (-10, 94), (5, 54), (4, 69), (-3, 81), (0, 88), (-7, 67), (-5, 74),
(-4, 74), (-5, 80), (-7, 72), (1, 58), (0, 41), (0, 63), (0, 63), (0, 63),
(-9, 83), (4, 86), (0, 97), (-7, 72), (13, 41), (3, 62), (0, 45), (-4, 78),
(-3, 96), (-27, 126), (-28, 98), (-25, 101), (-23, 67), (-28, 82), (-20, 94), (-16, 83),
(-22, 110), (-21, 91), (-18, 102), (-13, 93), (-29, 127), (-7, 92), (-5, 89), (-7, 96),
(-13, 108), (-3, 46), (-1, 65), (-1, 57), (-9, 93), (-3, 74), (-9, 92), (-8, 87),
(-23, 126), (5, 54), (6, 60), (6, 59), (6, 69), (-1, 48), (0, 68), (-4, 69),
(-8, 88), (-2, 85), (-6, 78), (-1, 75), (-7, 77), (2, 54), (5, 50), (-3, 68),
(1, 50), (6, 42), (-4, 81), (1, 63), (-4, 70), (0, 67), (2, 57), (-2, 76),
(11, 35), (4, 64), (1, 61), (11, 35), (18, 25), (12, 24), (13, 29), (13, 36),
(-10, 93), (-7, 73), (-2, 73), (13, 46), (9, 49), (-7, 100), (9, 53), (2, 53),
(5, 53), (-2, 61), (0, 56), (0, 56), (-13, 63), (-5, 60), (-1, 62), (4, 57),
(-6, 69), (4, 57), (14, 39), (4, 51), (13, 68), (3, 64), (1, 61), (9, 63),
(7, 50), (16, 39), (5, 44), (4, 52), (11, 48), (-5, 60), (-1, 59), (0, 59),
(22, 33), (5, 44), (14, 43), (-1, 78), (0, 60), (9, 69), (11, 28), (2, 40),
(3, 44), (0, 49), (0, 46), (2, 44), (2, 51), (0, 47), (4, 39), (2, 62),
(6, 46), (0, 54), (3, 54), (2, 58), (4, 63), (6, 51), (6, 57), (7, 53),
(6, 52), (6, 55), (11, 45), (14, 36), (8, 53), (-1, 82), (7, 55), (-3, 78),
(15, 46), (22, 31), (-1, 84), (25, 7), (30, -7), (28, 3), (28, 4), (32, 0),
(34, -1), (30, 6), (30, 6), (32, 9), (31, 19), (26, 27), (26, 30), (37, 20),
(28, 34), (17, 70), (1, 67), (5, 59), (9, 67), (16, 30), (18, 32), (18, 35),
(22, 29), (24, 31), (23, 38), (18, 43), (20, 41), (11, 63), (9, 59), (9, 64),
(-1, 94), (-2, 89), (-9, 108), (-6, 76), (-2, 44), (0, 45), (0, 52), (-3, 64),
(-2, 59), (-4, 70), (-4, 75), (-8, 82), (-17, 102), (-9, 77), (3, 24), (0, 42),
(0, 48), (0, 55), (-6, 59), (-7, 71), (-12, 83), (-11, 87), (-30, 119), (1, 58),
(-3, 29), (-1, 36), (1, 38), (2, 43), (-6, 55), (0, 58), (0, 64), (-3, 74),
(-10, 90), (0, 70), (-4, 29), (5, 31), (7, 42), (1, 59), (-2, 58), (-3, 72),
(-3, 81), (-11, 97), (0, 58), (8, 5), (10, 14), (14, 18), (13, 27), (2, 40),
(0, 58), (-3, 70), (-6, 79), (-8, 85), (0, 0), (-13, 106), (-16, 106), (-10, 87),
(-21, 114), (-18, 110), (-14, 98), (-22, 110), (-21, 106), (-18, 103), (-21, 107), (-23, 108),
(-26, 112), (-10, 96), (-12, 95), (-5, 91), (-9, 93), (-22, 94), (-5, 86), (9, 67),
(-4, 80), (-10, 85), (-1, 70), (7, 60), (9, 58), (5, 61), (12, 50), (15, 50),
(18, 49), (17, 54), (10, 41), (7, 46), (-1, 51), (7, 49), (8, 52), (9, 41),
(6, 47), (2, 55), (13, 41), (10, 44), (6, 50), (5, 53), (13, 49), (4, 63),
(6, 64), (-2, 69), (-2, 59), (6, 70), (10, 44), (9, 31), (12, 43), (3, 53),
(14, 34), (10, 38), (-3, 52), (13, 40), (17, 32), (7, 44), (7, 38), (13, 50),
(10, 57), (26, 43), (14, 11), (11, 14), (9, 11), (18, 11), (21, 9), (23, -2),
(32, -15), (32, -15), (34, -21), (39, -23), (42, -33), (41, -31), (46, -28), (38, -12),
(21, 29), (45, -24), (53, -45), (48, -26), (65, -43), (43, -19), (39, -10), (30, 9),
(18, 26), (20, 27), (0, 57), (-14, 82), (-5, 75), (-19, 97), (-35, 125), (27, 0),
(28, 0), (31, -4), (27, 6), (34, 8), (30, 10), (24, 22), (33, 19), (22, 32),
(26, 31), (21, 41), (26, 44), (23, 47), (16, 65), (14, 71), (8, 60), (6, 63),
(17, 65), (21, 24), (23, 20), (26, 23), (27, 32), (28, 23), (28, 24), (23, 40),
(24, 32), (28, 29), (23, 42), (19, 57), (22, 53), (22, 61), (11, 86), (12, 40),
(11, 51), (14, 59), (-4, 79), (-7, 71), (-5, 69), (-9, 70), (-8, 66), (-10, 68),
(-19, 73), (-12, 69), (-16, 70), (-15, 67), (-20, 62), (-19, 70), (-16, 66), (-22, 65),
(-20, 63), (9, -2), (26, -9), (33, -9), (39, -7), (41, -2), (45, 3), (49, 9),
(45, 27), (36, 59), (-6, 66), (-7, 35), (-7, 42), (-8, 45), (-5, 48), (-12, 56),
(-6, 60), (-5, 62), (-8, 66), (-8, 76), (-5, 85), (-6, 81), (-10, 77), (-7, 81),
(-17, 80), (-18, 73), (-4, 74), (-10, 83), (-9, 71), (-9, 67), (-1, 61), (-8, 66),
(-14, 66), (0, 59), (2, 59), (21, -13), (33, -14), (39, -7), (46, -2), (51, 2),
(60, 6), (61, 17), (55, 34), (42, 62), (-7, 92), (-5, 89), (-7, 96), (-13, 108),
(-3, 46), (-1, 65), (-1, 57), (-9, 93), (-3, 74), (-9, 92), (-8, 87), (-23, 126),
(-7, 92), (-5, 89), (-7, 96), (-13, 108), (-3, 46), (-1, 65), (-1, 57), (-9, 93),
(-3, 74), (-9, 92), (-8, 87), (-23, 126), (-2, 85), (-6, 78), (-1, 75), (-7, 77),
(2, 54), (5, 50), (-3, 68), (1, 50), (6, 42), (-4, 81), (1, 63), (-4, 70),
(0, 67), (2, 57), (-2, 76), (11, 35), (4, 64), (1, 61), (11, 35), (18, 25),
(12, 24), (13, 29), (13, 36), (-10, 93), (-7, 73), (-2, 73), (13, 46), (9, 49),
(-7, 100), (9, 53), (2, 53), (5, 53), (-2, 61), (0, 56), (0, 56), (-13, 63),
(-5, 60), (-1, 62), (4, 57), (-6, 69), (4, 57), (14, 39), (4, 51), (13, 68),
(-2, 85), (-6, 78), (-1, 75), (-7, 77), (2, 54), (5, 50), (-3, 68), (1, 50),
(6, 42), (-4, 81), (1, 63), (-4, 70), (0, 67), (2, 57), (-2, 76), (11, 35),
(4, 64), (1, 61), (11, 35), (18, 25), (12, 24), (13, 29), (13, 36), (-10, 93),
(-7, 73), (-2, 73), (13, 46), (9, 49), (-7, 100), (9, 53), (2, 53), (5, 53),
(-2, 61), (0, 56), (0, 56), (-13, 63), (-5, 60), (-1, 62), (4, 57), (-6, 69),
(4, 57), (14, 39), (4, 51), (13, 68), (11, 28), (2, 40), (3, 44), (0, 49),
(0, 46), (2, 44), (2, 51), (0, 47), (4, 39), (2, 62), (6, 46), (0, 54),
(3, 54), (2, 58), (4, 63), (6, 51), (6, 57), (7, 53), (6, 52), (6, 55),
(11, 45), (14, 36), (8, 53), (-1, 82), (7, 55), (-3, 78), (15, 46), (22, 31),
(-1, 84), (25, 7), (30, -7), (28, 3), (28, 4), (32, 0), (34, -1), (30, 6),
(30, 6), (32, 9), (31, 19), (26, 27), (26, 30), (37, 20), (28, 34), (17, 70),
(11, 28), (2, 40), (3, 44), (0, 49), (0, 46), (2, 44), (2, 51), (0, 47),
(4, 39), (2, 62), (6, 46), (0, 54), (3, 54), (2, 58), (4, 63), (6, 51),
(6, 57), (7, 53), (6, 52), (6, 55), (11, 45), (14, 36), (8, 53), (-1, 82),
(7, 55), (-3, 78), (15, 46), (22, 31), (-1, 84), (25, 7), (30, -7), (28, 3),
(28, 4), (32, 0), (34, -1), (30, 6), (30, 6), (32, 9), (31, 19), (26, 27),
(26, 30), (37, 20), (28, 34), (17, 70), (-4, 79), (-7, 71), (-5, 69), (-9, 70),
(-8, 66), (-10, 68), (-19, 73), (-12, 69), (-16, 70), (-15, 67), (-20, 62), (-19, 70),
(-16, 66), (-22, 65), (-20, 63), (-5, 85), (-6, 81), (-10, 77), (-7, 81), (-17, 80),
(-18, 73), (-4, 74), (-10, 83), (-9, 71), (-9, 67), (-1, 61), (-8, 66), (-14, 66),
(0, 59), (2, 59), (9, -2), (26, -9), (33, -9), (39, -7), (41, -2), (45, 3),
(49, 9), (45, 27), (36, 59), (21, -13), (33, -14), (39, -7), (46, -2), (51, 2),
(60, 6), (61, 17), (55, 34), (42, 62), (-6, 66), (-7, 35), (-7, 42), (-8, 45),
(-5, 48), (-12, 56), (-6, 60), (-5, 62), (-8, 66), (-8, 76), (-4, 79), (-7, 71),
(-5, 69), (-9, 70), (-8, 66), (-10, 68), (-19, 73), (-12, 69), (-16, 70), (-15, 67),
(-20, 62), (-19, 70), (-16, 66), (-22, 65), (-20, 63), (-5, 85), (-6, 81), (-10, 77),
(-7, 81), (-17, 80), (-18, 73), (-4, 74), (-10, 83), (-9, 71), (-9, 67), (-1, 61),
(-8, 66), (-14, 66), (0, 59), (2, 59), (9, -2), (26, -9), (33, -9), (39, -7),
(41, -2), (45, 3), (49, 9), (45, 27), (36, 59), (21, -13), (33, -14), (39, -7),
(46, -2), (51, 2), (60, 6), (61, 17), (55, 34), (42, 62), (-6, 66), (-7, 35),
(-7, 42), (-8, 45), (-5, 48), (-12, 56), (-6, 60), (-5, 62), (-8, 66), (-8, 76),
(-13, 106), (-16, 106), (-10, 87), (-21, 114), (-18, 110), (-14, 98), (-22, 110), (-21, 106),
(-18, 103), (-21, 107), (-23, 108), (-26, 112), (-10, 96), (-12, 95), (-5, 91), (-9, 93),
(-22, 94), (-5, 86), (9, 67), (-4, 80), (-10, 85), (-1, 70), (7, 60), (9, 58),
(5, 61), (12, 50), (15, 50), (18, 49), (17, 54), (10, 41), (7, 46), (-1, 51),
(7, 49), (8, 52), (9, 41), (6, 47), (2, 55), (13, 41), (10, 44), (6, 50),
(5, 53), (13, 49), (4, 63), (6, 64), (-13, 106), (-16, 106), (-10, 87), (-21, 114),
(-18, 110), (-14, 98), (-22, 110), (-21, 106), (-18, 103), (-21, 107), (-23, 108), (-26, 112),
(-10, 96), (-12, 95), (-5, 91), (-9, 93), (-22, 94), (-5, 86), (9, 67), (-4, 80),
(-10, 85), (-1, 70), (7, 60), (9, 58), (5, 61), (12, 50), (15, 50), (18, 49),
(17, 54), (10, 41), (7, 46), (-1, 51), (7, 49), (8, 52), (9, 41), (6, 47),
(2, 55), (13, 41), (10, 44), (6, 50), (5, 53), (13, 49), (4, 63), (6, 64),
(14, 11), (11, 14), (9, 11), (18, 11), (21, 9), (23, -2), (32, -15), (32, -15),
(34, -21), (39, -23), (42, -33), (41, -31), (46, -28), (38, -12), (21, 29), (45, -24),
(53, -45), (48, -26), (65, -43), (43, -19), (39, -10), (30, 9), (18, 26), (20, 27),
(0, 57), (-14, 82), (-5, 75), (-19, 97), (-35, 125), (27, 0), (28, 0), (31, -4),
(27, 6), (34, 8), (30, 10), (24, 22), (33, 19), (22, 32), (26, 31), (21, 41),
(26, 44), (23, 47), (16, 65), (14, 71), (14, 11), (11, 14), (9, 11), (18, 11),
(21, 9), (23, -2), (32, -15), (32, -15), (34, -21), (39, -23), (42, -33), (41, -31),
(46, -28), (38, -12), (21, 29), (45, -24), (53, -45), (48, -26), (65, -43), (43, -19),
(39, -10), (30, 9), (18, 26), (20, 27), (0, 57), (-14, 82), (-5, 75), (-19, 97),
(-35, 125), (27, 0), (28, 0), (31, -4), (27, 6), (34, 8), (30, 10), (24, 22),
(33, 19), (22, 32), (26, 31), (21, 41), (26, 44), (23, 47), (16, 65), (14, 71),
(-6, 76), (-2, 44), (0, 45), (0, 52), (-3, 64), (-2, 59), (-4, 70), (-4, 75),
(-8, 82), (-17, 102), (-9, 77), (3, 24), (0, 42), (0, 48), (0, 55), (-6, 59),
(-7, 71), (-12, 83), (-11, 87), (-30, 119), (1, 58), (-3, 29), (-1, 36), (1, 38),
(2, 43), (-6, 55), (0, 58), (0, 64), (-3, 74), (-10, 90), (-6, 76), (-2, 44),
(0, 45), (0, 52), (-3, 64), (-2, 59), (-4, 70), (-4, 75), (-8, 82), (-17, 102),
(-9, 77), (3, 24), (0, 42), (0, 48), (0, 55), (-6, 59), (-7, 71), (-12, 83),
(-11, 87), (-30, 119), (1, 58), (-3, 29), (-1, 36), (1, 38), (2, 43), (-6, 55),
(0, 58), (0, 64), (-3, 74), (-10, 90), (-3, 74), (-9, 92), (-8, 87), (-23, 126),
(-3, 74), (-9, 92), (-8, 87), (-23, 126), (-3, 74), (-9, 92), (-8, 87), (-23, 126),
],
[
(20, -15), (2, 54), (3, 74), (20, -15), (2, 54), (3, 74), (-28, 127), (-23, 104),
(-6, 53), (-1, 54), (7, 51), (22, 25), (34, 0), (16, 0), (-2, 9), (4, 41),
(-29, 118), (2, 65), (-6, 71), (-13, 79), (5, 52), (9, 50), (-3, 70), (10, 54),
(26, 34), (19, 22), (40, 0), (57, 2), (41, 36), (26, 69), (-45, 127), (-15, 101),
(-4, 76), (-6, 71), (-13, 79), (5, 52), (6, 69), (-13, 90), (0, 52), (8, 43),
(-2, 69), (-5, 82), (-10, 96), (2, 59), (2, 75), (-3, 87), (-3, 100), (1, 56),
(-3, 74), (-6, 85), (0, 59), (-3, 81), (-7, 86), (-5, 95), (-1, 66), (-1, 77),
(1, 70), (-2, 86), (-5, 72), (0, 61), (0, 41), (0, 63), (0, 63), (0, 63),
(-9, 83), (4, 86), (0, 97), (-7, 72), (13, 41), (3, 62), (13, 15), (7, 51),
(2, 80), (-39, 127), (-18, 91), (-17, 96), (-26, 81), (-35, 98), (-24, 102), (-23, 97),
(-27, 119), (-24, 99), (-21, 110), (-18, 102), (-36, 127), (0, 80), (-5, 89), (-7, 94),
(-4, 92), (0, 39), (0, 65), (-15, 84), (-35, 127), (-2, 73), (-12, 104), (-9, 91),
(-31, 127), (3, 55), (7, 56), (7, 55), (8, 61), (-3, 53), (0, 68), (-7, 74),
(-9, 88), (-13, 103), (-13, 91), (-9, 89), (-14, 92), (-8, 76), (-12, 87), (-23, 110),
(-24, 105), (-10, 78), (-20, 112), (-17, 99), (-78, 127), (-70, 127), (-50, 127), (-46, 127),
(-4, 66), (-5, 78), (-4, 71), (-8, 72), (2, 59), (-1, 55), (-7, 70), (-6, 75),
(-8, 89), (-34, 119), (-3, 75), (32, 20), (30, 22), (-44, 127), (0, 54), (-5, 61),
(0, 58), (-1, 60), (-3, 61), (-8, 67), (-25, 84), (-14, 74), (-5, 65), (5, 52),
(2, 57), (0, 61), (-9, 69), (-11, 70), (18, 55), (-4, 71), (0, 58), (7, 61),
(9, 41), (18, 25), (9, 32), (5, 43), (9, 47), (0, 44), (0, 51), (2, 46),
(19, 38), (-4, 66), (15, 38), (12, 42), (9, 34), (0, 89), (4, 45), (10, 28),
(10, 31), (33, -11), (52, -43), (18, 15), (28, 0), (35, -22), (38, -25), (34, 0),
(39, -18), (32, -12), (102, -94), (0, 0), (56, -15), (33, -4), (29, 10), (37, -5),
(51, -29), (39, -9), (52, -34), (69, -58), (67, -63), (44, -5), (32, 7), (55, -29),
(32, 1), (0, 0), (27, 36), (33, -25), (34, -30), (36, -28), (38, -28), (38, -27),
(34, -18), (35, -16), (34, -14), (32, -8), (37, -6), (35, 0), (30, 10), (28, 18),
(26, 25), (29, 41), (0, 75), (2, 72), (8, 77), (14, 35), (18, 31), (17, 35),
(21, 30), (17, 45), (20, 42), (18, 45), (27, 26), (16, 54), (7, 66), (16, 56),
(11, 73), (10, 67), (-10, 116), (-23, 112), (-15, 71), (-7, 61), (0, 53), (-5, 66),
(-11, 77), (-9, 80), (-9, 84), (-10, 87), (-34, 127), (-21, 101), (-3, 39), (-5, 53),
(-7, 61), (-11, 75), (-15, 77), (-17, 91), (-25, 107), (-25, 111), (-28, 122), (-11, 76),
(-10, 44), (-10, 52), (-10, 57), (-9, 58), (-16, 72), (-7, 69), (-4, 69), (-5, 74),
(-9, 86), (2, 66), (-9, 34), (1, 32), (11, 31), (5, 52), (-2, 55), (-2, 67),
(0, 73), (-8, 89), (3, 52), (7, 4), (10, 8), (17, 8), (16, 19), (3, 37),
(-1, 61), (-5, 73), (-1, 70), (-4, 78), (0, 0), (-21, 126), (-23, 124), (-20, 110),
(-26, 126), (-25, 124), (-17, 105), (-27, 121), (-27, 117), (-17, 102), (-26, 117), (-27, 116),
(-33, 122), (-10, 95), (-14, 100), (-8, 95), (-17, 111), (-28, 114), (-6, 89), (-2, 80),
(-4, 82), (-9, 85), (-8, 81), (-1, 72), (5, 64), (1, 67), (9, 56), (0, 69),
(1, 69), (7, 69), (-7, 69), (-6, 67), (-16, 77), (-2, 64), (2, 61), (-6, 67),
(-3, 64), (2, 57), (-3, 65), (-3, 66), (0, 62), (9, 51), (-1, 66), (-2, 71),
(-2, 75), (-1, 70), (-9, 72), (14, 60), (16, 37), (0, 47), (18, 35), (11, 37),
(12, 41), (10, 41), (2, 48), (12, 41), (13, 41), (0, 59), (3, 50), (19, 40),
(3, 66), (18, 50), (19, -6), (18, -6), (14, 0), (26, -12), (31, -16), (33, -25),
(33, -22), (37, -28), (39, -30), (42, -30), (47, -42), (45, -36), (49, -34), (41, -17),
(32, 9), (69, -71), (63, -63), (66, -64), (77, -74), (54, -39), (52, -35), (41, -10),
(36, 0), (40, -1), (30, 14), (28, 26), (23, 37), (12, 55), (11, 65), (37, -33),
(39, -36), (40, -37), (38, -30), (46, -33), (42, -30), (40, -24), (49, -29), (38, -12),
(40, -10), (38, -3), (46, -5), (31, 20), (29, 30), (25, 44), (12, 48), (11, 49),
(26, 45), (22, 22), (23, 22), (27, 21), (33, 20), (26, 28), (30, 24), (27, 34),
(18, 42), (25, 39), (18, 50), (12, 70), (21, 54), (14, 71), (11, 83), (25, 32),
(21, 49), (21, 54), (-5, 85), (-6, 81), (-10, 77), (-7, 81), (-17, 80), (-18, 73),
(-4, 74), (-10, 83), (-9, 71), (-9, 67), (-1, 61), (-8, 66), (-14, 66), (0, 59),
(2, 59), (17, -10), (32, -13), (42, -9), (49, -5), (53, 0), (64, 3), (68, 10),
(66, 27), (47, 57), (-5, 71), (0, 24), (-1, 36), (-2, 42), (-2, 52), (-9, 57),
(-6, 63), (-4, 65), (-4, 67), (-7, 82), (-3, 81), (-3, 76), (-7, 72), (-6, 78),
(-12, 72), (-14, 68), (-3, 70), (-6, 76), (-5, 66), (-5, 62), (0, 57), (-4, 61),
(-9, 60), (1, 54), (2, 58), (17, -10), (32, -13), (42, -9), (49, -5), (53, 0),
(64, 3), (68, 10), (66, 27), (47, 57), (0, 80), (-5, 89), (-7, 94), (-4, 92),
(0, 39), (0, 65), (-15, 84), (-35, 127), (-2, 73), (-12, 104), (-9, 91), (-31, 127),
(0, 80), (-5, 89), (-7, 94), (-4, 92), (0, 39), (0, 65), (-15, 84), (-35, 127),
(-2, 73), (-12, 104), (-9, 91), (-31, 127), (-13, 103), (-13, 91), (-9, 89), (-14, 92),
(-8, 76), (-12, 87), (-23, 110), (-24, 105), (-10, 78), (-20, 112), (-17, 99), (-78, 127),
(-70, 127), (-50, 127), (-46, 127), (-4, 66), (-5, 78), (-4, 71), (-8, 72), (2, 59),
(-1, 55), (-7, 70), (-6, 75), (-8, 89), (-34, 119), (-3, 75), (32, 20), (30, 22),
(-44, 127), (0, 54), (-5, 61), (0, 58), (-1, 60), (-3, 61), (-8, 67), (-25, 84),
(-14, 74), (-5, 65), (5, 52), (2, 57), (0, 61), (-9, 69), (-11, 70), (18, 55),
(-13, 103), (-13, 91), (-9, 89), (-14, 92), (-8, 76), (-12, 87), (-23, 110), (-24, 105),
(-10, 78), (-20, 112), (-17, 99), (-78, 127), (-70, 127), (-50, 127), (-46, 127), (-4, 66),
(-5, 78), (-4, 71), (-8, 72), (2, 59), (-1, 55), (-7, 70), (-6, 75), (-8, 89),
(-34, 119), (-3, 75), (32, 20), (30, 22), (-44, 127), (0, 54), (-5, 61), (0, 58),
(-1, 60), (-3, 61), (-8, 67), (-25, 84), (-14, 74), (-5, 65), (5, 52), (2, 57),
(0, 61), (-9, 69), (-11, 70), (18, 55), (4, 45), (10, 28), (10, 31), (33, -11),
(52, -43), (18, 15), (28, 0), (35, -22), (38, -25), (34, 0), (39, -18), (32, -12),
(102, -94), (0, 0), (56, -15), (33, -4), (29, 10), (37, -5), (51, -29), (39, -9),
(52, -34), (69, -58), (67, -63), (44, -5), (32, 7), (55, -29), (32, 1), (0, 0),
(27, 36), (33, -25), (34, -30), (36, -28), (38, -28), (38, -27), (34, -18), (35, -16),
(34, -14), (32, -8), (37, -6), (35, 0), (30, 10), (28, 18), (26, 25), (29, 41),
(4, 45), (10, 28), (10, 31), (33, -11), (52, -43), (18, 15), (28, 0), (35, -22),
(38, -25), (34, 0), (39, -18), (32, -12), (102, -94), (0, 0), (56, -15), (33, -4),
(29, 10), (37, -5), (51, -29), (39, -9), (52, -34), (69, -58), (67, -63), (44, -5),
(32, 7), (55, -29), (32, 1), (0, 0), (27, 36), (33, -25), (34, -30), (36, -28),
(38, -28), (38, -27), (34, -18), (35, -16), (34, -14), (32, -8), (37, -6), (35, 0),
(30, 10), (28, 18), (26, 25), (29, 41), (-5, 85), (-6, 81), (-10, 77), (-7, 81),
(-17, 80), (-18, 73), (-4, 74), (-10, 83), (-9, 71), (-9, 67), (-1, 61), (-8, 66),
(-14, 66), (0, 59), (2, 59), (-3, 81), (-3, 76), (-7, 72), (-6, 78), (-12, 72),
(-14, 68), (-3, 70), (-6, 76), (-5, 66), (-5, 62), (0, 57), (-4, 61), (-9, 60),
(1, 54), (2, 58), (17, -10), (32, -13), (42, -9), (49, -5), (53, 0), (64, 3),
(68, 10), (66, 27), (47, 57), (17, -10), (32, -13), (42, -9), (49, -5), (53, 0),
(64, 3), (68, 10), (66, 27), (47, 57), (-5, 71), (0, 24), (-1, 36), (-2, 42),
(-2, 52), (-9, 57), (-6, 63), (-4, 65), (-4, 67), (-7, 82), (-5, 85), (-6, 81),
(-10, 77), (-7, 81), (-17, 80), (-18, 73), (-4, 74), (-10, 83), (-9, 71), (-9, 67),
(-1, 61), (-8, 66), (-14, 66), (0, 59), (2, 59), (-3, 81), (-3, 76), (-7, 72),
(-6, 78), (-12, 72), (-14, 68), (-3, 70), (-6, 76), (-5, 66), (-5, 62), (0, 57),
(-4, 61), (-9, 60), (1, 54), (2, 58), (17, -10), (32, -13), (42, -9), (49, -5),
(53, 0), (64, 3), (68, 10), (66, 27), (47, 57), (17, -10), (32, -13), (42, -9),
(49, -5), (53, 0), (64, 3), (68, 10), (66, 27), (47, 57), (-5, 71), (0, 24),
(-1, 36), (-2, 42), (-2, 52), (-9, 57), (-6, 63), (-4, 65), (-4, 67), (-7, 82),
(-21, 126), (-23, 124), (-20, 110), (-26, 126), (-25, 124), (-17, 105), (-27, 121), (-27, 117),
(-17, 102), (-26, 117), (-27, 116), (-33, 122), (-10, 95), (-14, 100), (-8, 95), (-17, 111),
(-28, 114), (-6, 89), (-2, 80), (-4, 82), (-9, 85), (-8, 81), (-1, 72), (5, 64),
(1, 67), (9, 56), (0, 69), (1, 69), (7, 69), (-7, 69), (-6, 67), (-16, 77),
(-2, 64), (2, 61), (-6, 67), (-3, 64), (2, 57), (-3, 65), (-3, 66), (0, 62),
(9, 51), (-1, 66), (-2, 71), (-2, 75), (-21, 126), (-23, 124), (-20, 110), (-26, 126),
(-25, 124), (-17, 105), (-27, 121), (-27, 117), (-17, 102), (-26, 117), (-27, 116), (-33, 122),
(-10, 95), (-14, 100), (-8, 95), (-17, 111), (-28, 114), (-6, 89), (-2, 80), (-4, 82),
(-9, 85), (-8, 81), (-1, 72), (5, 64), (1, 67), (9, 56), (0, 69), (1, 69),
(7, 69), (-7, 69), (-6, 67), (-16, 77), (-2, 64), (2, 61), (-6, 67), (-3, 64),
(2, 57), (-3, 65), (-3, 66), (0, 62), (9, 51), (-1, 66), (-2, 71), (-2, 75),
(19, -6), (18, -6), (14, 0), (26, -12), (31, -16), (33, -25), (33, -22), (37, -28),
(39, -30), (42, -30), (47, -42), (45, -36), (49, -34), (41, -17), (32, 9), (69, -71),
(63, -63), (66, -64), (77, -74), (54, -39), (52, -35), (41, -10), (36, 0), (40, -1),
(30, 14), (28, 26), (23, 37), (12, 55), (11, 65), (37, -33), (39, -36), (40, -37),
(38, -30), (46, -33), (42, -30), (40, -24), (49, -29), (38, -12), (40, -10), (38, -3),
(46, -5), (31, 20), (29, 30), (25, 44), (19, -6), (18, -6), (14, 0), (26, -12),
(31, -16), (33, -25), (33, -22), (37, -28), (39, -30), (42, -30), (47, -42), (45, -36),
(49, -34), (41, -17), (32, 9), (69, -71), (63, -63), (66, -64), (77, -74), (54, -39),
(52, -35), (41, -10), (36, 0), (40, -1), (30, 14), (28, 26), (23, 37), (12, 55),
(11, 65), (37, -33), (39, -36), (40, -37), (38, -30), (46, -33), (42, -30), (40, -24),
(49, -29), (38, -12), (40, -10), (38, -3), (46, -5), (31, 20), (29, 30), (25, 44),
(-23, 112), (-15, 71), (-7, 61), (0, 53), (-5, 66), (-11, 77), (-9, 80), (-9, 84),
(-10, 87), (-34, 127), (-21, 101), (-3, 39), (-5, 53), (-7, 61), (-11, 75), (-15, 77),
(-17, 91), (-25, 107), (-25, 111), (-28, 122), (-11, 76), (-10, 44), (-10, 52), (-10, 57),
(-9, 58), (-16, 72), (-7, 69), (-4, 69), (-5, 74), (-9, 86), (-23, 112), (-15, 71),
(-7, 61), (0, 53), (-5, 66), (-11, 77), (-9, 80), (-9, 84), (-10, 87), (-34, 127),
(-21, 101), (-3, 39), (-5, 53), (-7, 61), (-11, 75), (-15, 77), (-17, 91), (-25, 107),
(-25, 111), (-28, 122), (-11, 76), (-10, 44), (-10, 52), (-10, 57), (-9, 58), (-16, 72),
(-7, 69), (-4, 69), (-5, 74), (-9, 86), (-2, 73), (-12, 104), (-9, 91), (-31, 127),
(-2, 73), (-12, 104), (-9, 91), (-31, 127), (-2, 73), (-12, 104), (-9, 91), (-31, 127),
],
[
(20, -15), (2, 54), (3, 74), (20, -15), (2, 54), (3, 74), (-28, 127), (-23, 104),
(-6, 53), (-1, 54), (7, 51), (29, 16), (25, 0), (14, 0), (-10, 51), (-3, 62),
(-27, 99), (26, 16), (-4, 85), (-24, 102), (5, 57), (6, 57), (-17, 73), (14, 57),
(20, 40), (20, 10), (29, 0), (54, 0), (37, 42), (12, 97), (-32, 127), (-22, 117),
(-2, 74), (-4, 85), (-24, 102), (5, 57), (-6, 93), (-14, 88), (-6, 44), (4, 55),
(-11, 89), (-15, 103), (-21, 116), (19, 57), (20, 58), (4, 84), (6, 96), (1, 63),
(-5, 85), (-13, 106), (5, 63), (6, 75), (-3, 90), (-1, 101), (3, 55), (-4, 79),
(-2, 75), (-12, 97), (-7, 50), (1, 60), (0, 41), (0, 63), (0, 63), (0, 63),
(-9, 83), (4, 86), (0, 97), (-7, 72), (13, 41), (3, 62), (7, 34), (-9, 88),
(-20, 127), (-36, 127), (-17, 91), (-14, 95), (-25, 84), (-25, 86), (-12, 89), (-17, 91),
(-31, 127), (-14, 76), (-18, 103), (-13, 90), (-37, 127), (11, 80), (5, 76), (2, 84),
(5, 78), (-6, 55), (4, 61), (-14, 83), (-37, 127), (-5, 79), (-11, 104), (-11, 91),
(-30, 127), (0, 65), (-2, 79), (0, 72), (-4, 92), (-6, 56), (3, 68), (-8, 71),
(-13, 98), (-4, 86), (-12, 88), (-5, 82), (-3, 72), (-4, 67), (-8, 72), (-16, 89),
(-9, 69), (-1, 59), (5, 66), (4, 57), (-4, 71), (-2, 71), (2, 58), (-1, 74),
(-4, 44), (-1, 69), (0, 62), (-7, 51), (-4, 47), (-6, 42), (-3, 41), (-6, 53),
(8, 76), (-9, 78), (-11, 83), (9, 52), (0, 67), (-5, 90), (1, 67), (-15, 72),
(-5, 75), (-8, 80), (-21, 83), (-21, 64), (-13, 31), (-25, 64), (-29, 94), (9, 75),
(17, 63), (-8, 74), (-5, 35), (-2, 27), (13, 91), (3, 65), (-7, 69), (8, 77),
(-10, 66), (3, 62), (-3, 68), (-20, 81), (0, 30), (1, 7), (-3, 23), (-21, 74),
(16, 66), (-23, 124), (17, 37), (44, -18), (50, -34), (-22, 127), (4, 39), (0, 42),
(7, 34), (11, 29), (8, 31), (6, 37), (7, 42), (3, 40), (8, 33), (13, 43),
(13, 36), (4, 47), (3, 55), (2, 58), (6, 60), (8, 44), (11, 44), (14, 42),
(7, 48), (4, 56), (4, 52), (13, 37), (9, 49), (19, 58), (10, 48), (12, 45),
(0, 69), (20, 33), (8, 63), (35, -18), (33, -25), (28, -3), (24, 10), (27, 0),
(34, -14), (52, -44), (39, -24), (19, 17), (31, 25), (36, 29), (24, 33), (34, 15),
(30, 20), (22, 73), (20, 34), (19, 31), (27, 44), (19, 16), (15, 36), (15, 36),
(21, 28), (25, 21), (30, 20), (31, 12), (27, 16), (24, 42), (0, 93), (14, 56),
(15, 57), (26, 38), (-24, 127), (-24, 115), (-22, 82), (-9, 62), (0, 53), (0, 59),
(-14, 85), (-13, 89), (-13, 94), (-11, 92), (-29, 127), (-21, 100), (-14, 57), (-12, 67),
(-11, 71), (-10, 77), (-21, 85), (-16, 88), (-23, 104), (-15, 98), (-37, 127), (-10, 82),
(-8, 48), (-8, 61), (-8, 66), (-7, 70), (-14, 75), (-10, 79), (-9, 83), (-12, 92),
(-18, 108), (-4, 79), (-22, 69), (-16, 75), (-2, 58), (1, 58), (-13, 78), (-9, 83),
(-4, 81), (-13, 99), (-13, 81), (-6, 38), (-13, 62), (-6, 58), (-2, 59), (-16, 73),
(-10, 76), (-13, 86), (-9, 83), (-10, 87), (0, 0), (-22, 127), (-25, 127), (-25, 120),
(-27, 127), (-19, 114), (-23, 117), (-25, 118), (-26, 117), (-24, 113), (-28, 118), (-31, 120),
(-37, 124), (-10, 94), (-15, 102), (-10, 99), (-13, 106), (-50, 127), (-5, 92), (17, 57),
(-5, 86), (-13, 94), (-12, 91), (-2, 77), (0, 71), (-1, 73), (4, 64), (-7, 81),
(5, 64), (15, 57), (1, 67), (0, 68), (-10, 67), (1, 68), (0, 77), (2, 64),
(0, 68), (-5, 78), (7, 55), (5, 59), (2, 65), (14, 54), (15, 44), (5, 60),
(2, 70), (-2, 76), (-18, 86), (12, 70), (5, 64), (-12, 70), (11, 55), (5, 56),
(0, 69), (2, 65), (-6, 74), (5, 54), (7, 54), (-6, 76), (-11, 82), (-2, 77),
(-2, 77), (25, 42), (17, -13), (16, -9), (17, -12), (27, -21), (37, -30), (41, -40),
(42, -41), (48, -47), (39, -32), (46, -40), (52, -51), (46, -41), (52, -39), (43, -19),
(32, 11), (61, -55), (56, -46), (62, -50), (81, -67), (45, -20), (35, -2), (28, 15),
(34, 1), (39, 1), (30, 17), (20, 38), (18, 45), (15, 54), (0, 79), (36, -16),
(37, -14), (37, -17), (32, 1), (34, 15), (29, 15), (24, 25), (34, 22), (31, 16),
(35, 18), (31, 28), (33, 41), (36, 28), (27, 47), (21, 62), (18, 31), (19, 26),
(36, 24), (24, 23), (27, 16), (24, 30), (31, 29), (22, 41), (22, 42), (16, 60),
(15, 52), (14, 60), (3, 78), (-16, 123), (21, 53), (22, 56), (25, 61), (21, 33),
(19, 50), (17, 61), (-3, 78), (-8, 74), (-9, 72), (-10, 72), (-18, 75), (-12, 71),
(-11, 63), (-5, 70), (-17, 75), (-14, 72), (-16, 67), (-8, 53), (-14, 59), (-9, 52),
(-11, 68), (9, -2), (30, -10), (31, -4), (33, -1), (33, 7), (31, 12), (37, 23),
(31, 38), (20, 64), (-9, 71), (-7, 37), (-8, 44), (-11, 49), (-10, 56), (-12, 59),
(-8, 63), (-9, 67), (-6, 68), (-10, 79), (-3, 78), (-8, 74), (-9, 72), (-10, 72),
(-18, 75), (-12, 71), (-11, 63), (-5, 70), (-17, 75), (-14, 72), (-16, 67), (-8, 53),
(-14, 59), (-9, 52), (-11, 68), (9, -2), (30, -10), (31, -4), (33, -1), (33, 7),
(31, 12), (37, 23), (31, 38), (20, 64), (11, 80), (5, 76), (2, 84), (5, 78),
(-6, 55), (4, 61), (-14, 83), (-37, 127), (-5, 79), (-11, 104), (-11, 91), (-30, 127),
(11, 80), (5, 76), (2, 84), (5, 78), (-6, 55), (4, 61), (-14, 83), (-37, 127),
(-5, 79), (-11, 104), (-11, 91), (-30, 127), (-4, 86), (-12, 88), (-5, 82), (-3, 72),
(-4, 67), (-8, 72), (-16, 89), (-9, 69), (-1, 59), (5, 66), (4, 57), (-4, 71),
(-2, 71), (2, 58), (-1, 74), (-4, 44), (-1, 69), (0, 62), (-7, 51), (-4, 47),
(-6, 42), (-3, 41), (-6, 53), (8, 76), (-9, 78), (-11, 83), (9, 52), (0, 67),
(-5, 90), (1, 67), (-15, 72), (-5, 75), (-8, 80), (-21, 83), (-21, 64), (-13, 31),
(-25, 64), (-29, 94), (9, 75), (17, 63), (-8, 74), (-5, 35), (-2, 27), (13, 91),
(-4, 86), (-12, 88), (-5, 82), (-3, 72), (-4, 67), (-8, 72), (-16, 89), (-9, 69),
(-1, 59), (5, 66), (4, 57), (-4, 71), (-2, 71), (2, 58), (-1, 74), (-4, 44),
(-1, 69), (0, 62), (-7, 51), (-4, 47), (-6, 42), (-3, 41), (-6, 53), (8, 76),
(-9, 78), (-11, 83), (9, 52), (0, 67), (-5, 90), (1, 67), (-15, 72), (-5, 75),
(-8, 80), (-21, 83), (-21, 64), (-13, 31), (-25, 64), (-29, 94), (9, 75), (17, 63),
(-8, 74), (-5, 35), (-2, 27), (13, 91), (4, 39), (0, 42), (7, 34), (11, 29),
(8, 31), (6, 37), (7, 42), (3, 40), (8, 33), (13, 43), (13, 36), (4, 47),
(3, 55), (2, 58), (6, 60), (8, 44), (11, 44), (14, 42), (7, 48), (4, 56),
(4, 52), (13, 37), (9, 49), (19, 58), (10, 48), (12, 45), (0, 69), (20, 33),
(8, 63), (35, -18), (33, -25), (28, -3), (24, 10), (27, 0), (34, -14), (52, -44),
(39, -24), (19, 17), (31, 25), (36, 29), (24, 33), (34, 15), (30, 20), (22, 73),
(4, 39), (0, 42), (7, 34), (11, 29), (8, 31), (6, 37), (7, 42), (3, 40),
(8, 33), (13, 43), (13, 36), (4, 47), (3, 55), (2, 58), (6, 60), (8, 44),
(11, 44), (14, 42), (7, 48), (4, 56), (4, 52), (13, 37), (9, 49), (19, 58),
(10, 48), (12, 45), (0, 69), (20, 33), (8, 63), (35, -18), (33, -25), (28, -3),
(24, 10), (27, 0), (34, -14), (52, -44), (39, -24), (19, 17), (31, 25), (36, 29),
(24, 33), (34, 15), (30, 20), (22, 73), (-3, 78), (-8, 74), (-9, 72), (-10, 72),
(-18, 75), (-12, 71), (-11, 63), (-5, 70), (-17, 75), (-14, 72), (-16, 67), (-8, 53),
(-14, 59), (-9, 52), (-11, 68), (-3, 78), (-8, 74), (-9, 72), (-10, 72), (-18, 75),
(-12, 71), (-11, 63), (-5, 70), (-17, 75), (-14, 72), (-16, 67), (-8, 53), (-14, 59),
(-9, 52), (-11, 68), (9, -2), (30, -10), (31, -4), (33, -1), (33, 7), (31, 12),
(37, 23), (31, 38), (20, 64), (9, -2), (30, -10), (31, -4), (33, -1), (33, 7),
(31, 12), (37, 23), (31, 38), (20, 64), (-9, 71), (-7, 37), (-8, 44), (-11, 49),
(-10, 56), (-12, 59), (-8, 63), (-9, 67), (-6, 68), (-10, 79), (-3, 78), (-8, 74),
(-9, 72), (-10, 72), (-18, 75), (-12, 71), (-11, 63), (-5, 70), (-17, 75), (-14, 72),
(-16, 67), (-8, 53), (-14, 59), (-9, 52), (-11, 68), (-3, 78), (-8, 74), (-9, 72),
(-10, 72), (-18, 75), (-12, 71), (-11, 63), (-5, 70), (-17, 75), (-14, 72), (-16, 67),
(-8, 53), (-14, 59), (-9, 52), (-11, 68), (9, -2), (30, -10), (31, -4), (33, -1),
(33, 7), (31, 12), (37, 23), (31, 38), (20, 64), (9, -2), (30, -10), (31, -4),
(33, -1), (33, 7), (31, 12), (37, 23), (31, 38), (20, 64), (-9, 71), (-7, 37),
(-8, 44), (-11, 49), (-10, 56), (-12, 59), (-8, 63), (-9, 67), (-6, 68), (-10, 79),
(-22, 127), (-25, 127), (-25, 120), (-27, 127), (-19, 114), (-23, 117), (-25, 118), (-26, 117),
(-24, 113), (-28, 118), (-31, 120), (-37, 124), (-10, 94), (-15, 102), (-10, 99), (-13, 106),
(-50, 127), (-5, 92), (17, 57), (-5, 86), (-13, 94), (-12, 91), (-2, 77), (0, 71),
(-1, 73), (4, 64), (-7, 81), (5, 64), (15, 57), (1, 67), (0, 68), (-10, 67),
(1, 68), (0, 77), (2, 64), (0, 68), (-5, 78), (7, 55), (5, 59), (2, 65),
(14, 54), (15, 44), (5, 60), (2, 70), (-22, 127), (-25, 127), (-25, 120), (-27, 127),
(-19, 114), (-23, 117), (-25, 118), (-26, 117), (-24, 113), (-28, 118), (-31, 120), (-37, 124),
(-10, 94), (-15, 102), (-10, 99), (-13, 106), (-50, 127), (-5, 92), (17, 57), (-5, 86),
(-13, 94), (-12, 91), (-2, 77), (0, 71), (-1, 73), (4, 64), (-7, 81), (5, 64),
(15, 57), (1, 67), (0, 68), (-10, 67), (1, 68), (0, 77), (2, 64), (0, 68),
(-5, 78), (7, 55), (5, 59), (2, 65), (14, 54), (15, 44), (5, 60), (2, 70),
(17, -13), (16, -9), (17, -12), (27, -21), (37, -30), (41, -40), (42, -41), (48, -47),
(39, -32), (46, -40), (52, -51), (46, -41), (52, -39), (43, -19), (32, 11), (61, -55),
(56, -46), (62, -50), (81, -67), (45, -20), (35, -2), (28, 15), (34, 1), (39, 1),
(30, 17), (20, 38), (18, 45), (15, 54), (0, 79), (36, -16), (37, -14), (37, -17),
(32, 1), (34, 15), (29, 15), (24, 25), (34, 22), (31, 16), (35, 18), (31, 28),
(33, 41), (36, 28), (27, 47), (21, 62), (17, -13), (16, -9), (17, -12), (27, -21),
(37, -30), (41, -40), (42, -41), (48, -47), (39, -32), (46, -40), (52, -51), (46, -41),
(52, -39), (43, -19), (32, 11), (61, -55), (56, -46), (62, -50), (81, -67), (45, -20),
(35, -2), (28, 15), (34, 1), (39, 1), (30, 17), (20, 38), (18, 45), (15, 54),
(0, 79), (36, -16), (37, -14), (37, -17), (32, 1), (34, 15), (29, 15), (24, 25),
(34, 22), (31, 16), (35, 18), (31, 28), (33, 41), (36, 28), (27, 47), (21, 62),
(-24, 115), (-22, 82), (-9, 62), (0, 53), (0, 59), (-14, 85), (-13, 89), (-13, 94),
(-11, 92), (-29, 127), (-21, 100), (-14, 57), (-12, 67), (-11, 71), (-10, 77), (-21, 85),
(-16, 88), (-23, 104), (-15, 98), (-37, 127), (-10, 82), (-8, 48), (-8, 61), (-8, 66),
(-7, 70), (-14, 75), (-10, 79), (-9, 83), (-12, 92), (-18, 108), (-24, 115), (-22, 82),
(-9, 62), (0, 53), (0, 59), (-14, 85), (-13, 89), (-13, 94), (-11, 92), (-29, 127),
(-21, 100), (-14, 57), (-12, 67), (-11, 71), (-10, 77), (-21, 85), (-16, 88), (-23, 104),
(-15, 98), (-37, 127), (-10, 82), (-8, 48), (-8, 61), (-8, 66), (-7, 70), (-14, 75),
(-10, 79), (-9, 83), (-12, 92), (-18, 108), (-5, 79), (-11, 104), (-11, 91), (-30, 127),
(-5, 79), (-11, 104), (-11, 91), (-30, 127), (-5, 79), (-11, 104), (-11, 91), (-30, 127),
],
[
(20, -15), (2, 54), (3, 74), (20, -15), (2, 54), (3, 74), (-28, 127), (-23, 104),
(-6, 53), (-1, 54), (7, 51), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
(0, 0), (0, 0), (0, 0), (0, 0), (0, 41), (0, 63), (0, 63), (0, 63),
(-9, 83), (4, 86), (0, 97), (-7, 72), (13, 41), (3, 62), (0, 11), (1, 55),
(0, 69), (-17, 127), (-13, 102), (0, 82), (-7, 74), (-21, 107), (-27, 127), (-31, 127),
(-24, 127), (-18, 95), (-27, 127), (-21, 114), (-30, 127), (-17, 123), (-12, 115), (-16, 122),
(-11, 115), (-12, 63), (-2, 68), (-15, 84), (-13, 104), (-3, 70), (-8, 93), (-10, 90),
(-30, 127), (-1, 74), (-6, 97), (-7, 91), (-20, 127), (-4, 56), (-5, 82), (-7, 76),
(-22, 125), (-7, 93), (-11, 87), (-3, 77), (-5, 71), (-4, 63), (-4, 68), (-12, 84),
(-7, 62), (-7, 65), (8, 61), (5, 56), (-2, 66), (1, 64), (0, 61), (-2, 78),
(1, 50), (7, 52), (10, 35), (0, 44), (11, 38), (1, 45), (0, 46), (5, 44),
(31, 17), (1, 51), (7, 50), (28, 19), (16, 33), (14, 62), (-13, 108), (-15, 100),
(-13, 101), (-13, 91), (-12, 94), (-10, 88), (-16, 84), (-10, 86), (-7, 83), (-13, 87),
(-19, 94), (1, 70), (0, 72), (-5, 74), (18, 59), (-8, 102), (-15, 100), (0, 95),
(-4, 75), (2, 72), (-11, 75), (-3, 71), (15, 46), (-13, 69), (0, 62), (0, 65),
(21, 37), (-15, 72), (9, 57), (16, 54), (0, 62), (12, 72), (24, 0), (15, 9),
(8, 25), (13, 18), (15, 9), (13, 19), (10, 37), (12, 18), (6, 29), (20, 33),
(15, 30), (4, 45), (1, 58), (0, 62), (7, 61), (12, 38), (11, 45), (15, 39),
(11, 42), (13, 44), (16, 45), (12, 41), (10, 49), (30, 34), (18, 42), (10, 55),
(17, 51), (17, 46), (0, 89), (26, -19), (22, -17), (26, -17), (30, -25), (28, -20),
(33, -23), (37, -27), (33, -23), (40, -28), (38, -17), (33, -11), (40, -15), (41, -6),
(38, 1), (41, 17), (30, -6), (27, 3), (26, 22), (37, -16), (35, -4), (38, -8),
(38, -3), (37, 3), (38, 5), (42, 0), (35, 16), (39, 22), (14, 48), (27, 37),
(21, 60), (12, 68), (2, 97), (-3, 71), (-6, 42), (-5, 50), (-3, 54), (-2, 62),
(0, 58), (1, 63), (-2, 72), (-1, 74), (-9, 91), (-5, 67), (-5, 27), (-3, 39),
(-2, 44), (0, 46), (-16, 64), (-8, 68), (-10, 78), (-6, 77), (-10, 86), (-12, 92),
(-15, 55), (-10, 60), (-6, 62), (-4, 65), (-12, 73), (-8, 76), (-7, 80), (-9, 88),
(-17, 110), (-11, 97), (-20, 84), (-11, 79), (-6, 73), (-4, 74), (-13, 86), (-13, 96),
(-11, 97), (-19, 117), (-8, 78), (-5, 33), (-4, 48), (-2, 53), (-3, 62), (-13, 71),
(-10, 79), (-12, 86), (-13, 90), (-14, 97), (0, 0), (-6, 93), (-6, 84), (-8, 79),
(0, 66), (-1, 71), (0, 62), (-2, 60), (-2, 59), (-5, 75), (-3, 62), (-4, 58),
(-9, 66), (-1, 79), (0, 71), (3, 68), (10, 44), (-7, 62), (15, 36), (14, 40),
(16, 27), (12, 29), (1, 44), (20, 36), (18, 32), (5, 42), (1, 48), (10, 62),
(17, 46), (9, 64), (-12, 104), (-11, 97), (-16, 96), (-7, 88), (-8, 85), (-7, 85),
(-9, 85), (-13, 88), (4, 66), (-3, 77), (-3, 76), (-6, 76), (10, 58), (-1, 76),
(-1, 83), (-7, 99), (-14, 95), (2, 95), (0, 76), (-5, 74), (0, 70), (-11, 75),
(1, 68), (0, 65), (-14, 73), (3, 62), (4, 62), (-1, 68), (-13, 75), (11, 55),
(5, 64), (12, 70), (15, 6), (6, 19), (7, 16), (12, 14), (18, 13), (13, 11),
(13, 15), (15, 16), (12, 23), (13, 23), (15, 20), (14, 26), (14, 44), (17, 40),
(17, 47), (24, 17), (21, 21), (25, 22), (31, 27), (22, 29), (19, 35), (14, 50),
(10, 57), (7, 63), (-2, 77), (-4, 82), (-3, 94), (9, 69), (-12, 109), (36, -35),
(36, -34), (32, -26), (37, -30), (44, -32), (34, -18), (34, -15), (40, -15), (33, -7),
(35, -5), (33, 0), (38, 2), (33, 13), (23, 35), (13, 58), (29, -3), (26, 0),
(22, 30), (31, -7), (35, -15), (34, -3), (34, 3), (36, -1), (34, 5), (32, 11),
(35, 5), (34, 12), (39, 11), (30, 29), (34, 26), (29, 39), (19, 66), (31, 21),
(31, 31), (25, 50), (-17, 120), (-20, 112), (-18, 114), (-11, 85), (-15, 92), (-14, 89),
(-26, 71), (-15, 81), (-14, 80), (0, 68), (-14, 70), (-24, 56), (-23, 68), (-24, 50),
(-11, 74), (23, -13), (26, -13), (40, -15), (49, -14), (44, 3), (45, 6), (44, 34),
(33, 54), (19, 82), (-3, 75), (-1, 23), (1, 34), (1, 43), (0, 54), (-2, 55),
(0, 61), (1, 64), (0, 68), (-9, 92), (-14, 106), (-13, 97), (-15, 90), (-12, 90),
(-18, 88), (-10, 73), (-9, 79), (-14, 86), (-10, 73), (-10, 70), (-10, 69), (-5, 66),
(-9, 64), (-5, 58), (2, 59), (21, -10), (24, -11), (28, -8), (28, -1), (29, 3),
(29, 9), (35, 20), (29, 36), (14, 67), (-17, 123), (-12, 115), (-16, 122), (-11, 115),
(-12, 63), (-2, 68), (-15, 84), (-13, 104), (-3, 70), (-8, 93), (-10, 90), (-30, 127),
(-17, 123), (-12, 115), (-16, 122), (-11, 115), (-12, 63), (-2, 68), (-15, 84), (-13, 104),
(-3, 70), (-8, 93), (-10, 90), (-30, 127), (-7, 93), (-11, 87), (-3, 77), (-5, 71),
(-4, 63), (-4, 68), (-12, 84), (-7, 62), (-7, 65), (8, 61), (5, 56), (-2, 66),
(1, 64), (0, 61), (-2, 78), (1, 50), (7, 52), (10, 35), (0, 44), (11, 38),
(1, 45), (0, 46), (5, 44), (31, 17), (1, 51), (7, 50), (28, 19), (16, 33),
(14, 62), (-13, 108), (-15, 100), (-13, 101), (-13, 91), (-12, 94), (-10, 88), (-16, 84),
(-10, 86), (-7, 83), (-13, 87), (-19, 94), (1, 70), (0, 72), (-5, 74), (18, 59),
(-7, 93), (-11, 87), (-3, 77), (-5, 71), (-4, 63), (-4, 68), (-12, 84), (-7, 62),
(-7, 65), (8, 61), (5, 56), (-2, 66), (1, 64), (0, 61), (-2, 78), (1, 50),
(7, 52), (10, 35), (0, 44), (11, 38), (1, 45), (0, 46), (5, 44), (31, 17),
(1, 51), (7, 50), (28, 19), (16, 33), (14, 62), (-13, 108), (-15, 100), (-13, 101),
(-13, 91), (-12, 94), (-10, 88), (-16, 84), (-10, 86), (-7, 83), (-13, 87), (-19, 94),
(1, 70), (0, 72), (-5, 74), (18, 59), (24, 0), (15, 9), (8, 25), (13, 18),
(15, 9), (13, 19), (10, 37), (12, 18), (6, 29), (20, 33), (15, 30), (4, 45),
(1, 58), (0, 62), (7, 61), (12, 38), (11, 45), (15, 39), (11, 42), (13, 44),
(16, 45), (12, 41), (10, 49), (30, 34), (18, 42), (10, 55), (17, 51), (17, 46),
(0, 89), (26, -19), (22, -17), (26, -17), (30, -25), (28, -20), (33, -23), (37, -27),
(33, -23), (40, -28), (38, -17), (33, -11), (40, -15), (41, -6), (38, 1), (41, 17),
(24, 0), (15, 9), (8, 25), (13, 18), (15, 9), (13, 19), (10, 37), (12, 18),
(6, 29), (20, 33), (15, 30), (4, 45), (1, 58), (0, 62), (7, 61), (12, 38),
(11, 45), (15, 39), (11, 42), (13, 44), (16, 45), (12, 41), (10, 49), (30, 34),
(18, 42), (10, 55), (17, 51), (17, 46), (0, 89), (26, -19), (22, -17), (26, -17),
(30, -25), (28, -20), (33, -23), (37, -27), (33, -23), (40, -28), (38, -17), (33, -11),
(40, -15), (41, -6), (38, 1), (41, 17), (-17, 120), (-20, 112), (-18, 114), (-11, 85),
(-15, 92), (-14, 89), (-26, 71), (-15, 81), (-14, 80), (0, 68), (-14, 70), (-24, 56),
(-23, 68), (-24, 50), (-11, 74), (-14, 106), (-13, 97), (-15, 90), (-12, 90), (-18, 88),
(-10, 73), (-9, 79), (-14, 86), (-10, 73), (-10, 70), (-10, 69), (-5, 66), (-9, 64),
(-5, 58), (2, 59), (23, -13), (26, -13), (40, -15), (49, -14), (44, 3), (45, 6),
(44, 34), (33, 54), (19, 82), (21, -10), (24, -11), (28, -8), (28, -1), (29, 3),
(29, 9), (35, 20), (29, 36), (14, 67), (-3, 75), (-1, 23), (1, 34), (1, 43),
(0, 54), (-2, 55), (0, 61), (1, 64), (0, 68), (-9, 92), (-17, 120), (-20, 112),
(-18, 114), (-11, 85), (-15, 92), (-14, 89), (-26, 71), (-15, 81), (-14, 80), (0, 68),
(-14, 70), (-24, 56), (-23, 68), (-24, 50), (-11, 74), (-14, 106), (-13, 97), (-15, 90),
(-12, 90), (-18, 88), (-10, 73), (-9, 79), (-14, 86), (-10, 73), (-10, 70), (-10, 69),
(-5, 66), (-9, 64), (-5, 58), (2, 59), (23, -13), (26, -13), (40, -15), (49, -14),
(44, 3), (45, 6), (44, 34), (33, 54), (19, 82), (21, -10), (24, -11), (28, -8),
(28, -1), (29, 3), (29, 9), (35, 20), (29, 36), (14, 67), (-3, 75), (-1, 23),
(1, 34), (1, 43), (0, 54), (-2, 55), (0, 61), (1, 64), (0, 68), (-9, 92),
(-6, 93), (-6, 84), (-8, 79), (0, 66), (-1, 71), (0, 62), (-2, 60), (-2, 59),
(-5, 75), (-3, 62), (-4, 58), (-9, 66), (-1, 79), (0, 71), (3, 68), (10, 44),
(-7, 62), (15, 36), (14, 40), (16, 27), (12, 29), (1, 44), (20, 36), (18, 32),
(5, 42), (1, 48), (10, 62), (17, 46), (9, 64), (-12, 104), (-11, 97), (-16, 96),
(-7, 88), (-8, 85), (-7, 85), (-9, 85), (-13, 88), (4, 66), (-3, 77), (-3, 76),
(-6, 76), (10, 58), (-1, 76), (-1, 83), (-6, 93), (-6, 84), (-8, 79), (0, 66),
(-1, 71), (0, 62), (-2, 60), (-2, 59), (-5, 75), (-3, 62), (-4, 58), (-9, 66),
(-1, 79), (0, 71), (3, 68), (10, 44), (-7, 62), (15, 36), (14, 40), (16, 27),
(12, 29), (1, 44), (20, 36), (18, 32), (5, 42), (1, 48), (10, 62), (17, 46),
(9, 64), (-12, 104), (-11, 97), (-16, 96), (-7, 88), (-8, 85), (-7, 85), (-9, 85),
(-13, 88), (4, 66), (-3, 77), (-3, 76), (-6, 76), (10, 58), (-1, 76), (-1, 83),
(15, 6), (6, 19), (7, 16), (12, 14), (18, 13), (13, 11), (13, 15), (15, 16),
(12, 23), (13, 23), (15, 20), (14, 26), (14, 44), (17, 40), (17, 47), (24, 17),
(21, 21), (25, 22), (31, 27), (22, 29), (19, 35), (14, 50), (10, 57), (7, 63),
(-2, 77), (-4, 82), (-3, 94), (9, 69), (-12, 109), (36, -35), (36, -34), (32, -26),
(37, -30), (44, -32), (34, -18), (34, -15), (40, -15), (33, -7), (35, -5), (33, 0),
(38, 2), (33, 13), (23, 35), (13, 58), (15, 6), (6, 19), (7, 16), (12, 14),
(18, 13), (13, 11), (13, 15), (15, 16), (12, 23), (13, 23), (15, 20), (14, 26),
(14, 44), (17, 40), (17, 47), (24, 17), (21, 21), (25, 22), (31, 27), (22, 29),
(19, 35), (14, 50), (10, 57), (7, 63), (-2, 77), (-4, 82), (-3, 94), (9, 69),
(-12, 109), (36, -35), (36, -34), (32, -26), (37, -30), (44, -32), (34, -18), (34, -15),
(40, -15), (33, -7), (35, -5), (33, 0), (38, 2), (33, 13), (23, 35), (13, 58),
(-3, 71), (-6, 42), (-5, 50), (-3, 54), (-2, 62), (0, 58), (1, 63), (-2, 72),
(-1, 74), (-9, 91), (-5, 67), (-5, 27), (-3, 39), (-2, 44), (0, 46), (-16, 64),
(-8, 68), (-10, 78), (-6, 77), (-10, 86), (-12, 92), (-15, 55), (-10, 60), (-6, 62),
(-4, 65), (-12, 73), (-8, 76), (-7, 80), (-9, 88), (-17, 110), (-3, 71), (-6, 42),
(-5, 50), (-3, 54), (-2, 62), (0, 58), (1, 63), (-2, 72), (-1, 74), (-9, 91),
(-5, 67), (-5, 27), (-3, 39), (-2, 44), (0, 46), (-16, 64), (-8, 68), (-10, 78),
(-6, 77), (-10, 86), (-12, 92), (-15, 55), (-10, 60), (-6, 62), (-4, 65), (-12, 73),
(-8, 76), (-7, 80), (-9, 88), (-17, 110), (-3, 70), (-8, 93), (-10, 90), (-30, 127),
(-3, 70), (-8, 93), (-10, 90), (-30, 127), (-3, 70), (-8, 93), (-10, 90), (-30, 127),
],
];
-281
View File
@@ -1,281 +0,0 @@
//! CAVLC residual block parsing (9.2).
use crate::cavlc_tables::{COEFF_TOKEN, RUN_BEFORE, TOTAL_ZEROS_2X2, TOTAL_ZEROS_2X4, TOTAL_ZEROS_4X4};
use crate::error::{Result, ensure};
use deckcraft_bitstream::BitReader;
use std::sync::OnceLock;
/// Prefix-code lookup keyed by (number of leading zeros, next 5 bits after the first one bit).
pub(crate) struct VlcTable {
/// entry = (len << 8) | value, 0 = invalid.
entries: Box<[u16; 17 * 32]>,
}
impl VlcTable {
pub fn build(codes: &[(&str, u8)]) -> Self {
let mut entries = Box::new([0u16; 17 * 32]);
for &(code, value) in codes {
if code.is_empty() {
continue;
}
let len = code.len();
let lz = code.bytes().take_while(|&b| b == b'0').count();
let val = ((len as u16) << 8) | value as u16;
if lz == len {
// all-zero code: any continuation with at least `len` zeros
for l in lz..=16 {
for s in 0..32 {
entries[l * 32 + s] = val;
}
}
continue;
}
let suffix = &code[lz + 1..];
assert!(suffix.len() <= 5, "suffix too long in {code}");
let sbits = suffix.bytes().fold(0u32, |a, b| (a << 1) | u32::from(b == b'1'));
let free = 5 - suffix.len();
for ext in 0..(1u32 << free) {
let s = (sbits << free) | ext;
entries[lz * 32 + s as usize] = val;
}
}
Self { entries }
}
#[inline]
pub fn decode(&self, r: &mut BitReader) -> Result<u8> {
let bits = r.peek(32);
let lz = bits.leading_zeros().min(16);
let s = if lz >= 16 { 0 } else { ((bits << lz) << 1) >> 27 };
let e = self.entries[(lz * 32 + s) as usize];
ensure!(e != 0, "invalid VLC code");
r.skip((e >> 8) as usize)?;
Ok(e as u8)
}
}
struct Tables {
/// coeff_token per nC class; value = (TrailingOnes << 5) | TotalCoeff
coeff_token: [VlcTable; 6],
total_zeros_4x4: [VlcTable; 15],
total_zeros_2x2: [VlcTable; 3],
total_zeros_2x4: [VlcTable; 7],
run_before: [VlcTable; 7],
}
fn tables() -> &'static Tables {
static T: OnceLock<Tables> = OnceLock::new();
T.get_or_init(|| {
let coeff_token = std::array::from_fn(|col| {
let codes: Vec<(&str, u8)> = COEFF_TOKEN.iter().map(|&(t1, tc, ref c)| (c[col], (t1 << 5) | tc)).collect();
VlcTable::build(&codes)
});
fn simple<const N: usize>(row: &[&'static str; N]) -> VlcTable {
let codes: Vec<(&str, u8)> = row.iter().enumerate().map(|(i, &c)| (c, i as u8)).collect();
VlcTable::build(&codes)
}
Tables {
coeff_token,
total_zeros_4x4: std::array::from_fn(|i| simple(&TOTAL_ZEROS_4X4[i])),
total_zeros_2x2: std::array::from_fn(|i| simple(&TOTAL_ZEROS_2X2[i])),
total_zeros_2x4: std::array::from_fn(|i| simple(&TOTAL_ZEROS_2X4[i])),
run_before: std::array::from_fn(|i| simple(&RUN_BEFORE[i])),
}
})
}
/// Force table construction (so the first slice does not pay for it).
pub(crate) fn init_tables() {
let _ = tables();
}
/// Parse residual_block_cavlc(). Coefficients are written to `coeff[start_idx..=end_idx]` in scan order
/// (entries outside the coded range are left untouched; the caller zeroes them).
/// `nc` is the nC value (-1 for 4:2:0 chroma DC, -2 for 4:2:2 chroma DC). Returns TotalCoeff.
pub(crate) fn residual_block(r: &mut BitReader, coeff: &mut [i32], start_idx: usize, end_idx: usize, max_num_coeff: usize, nc: i32) -> Result<u8> {
let t = tables();
let col = match nc {
-1 => 4,
-2 => 5,
0..=1 => 0,
2..=3 => 1,
4..=7 => 2,
_ => 3,
};
let tok = t.coeff_token[col].decode(r)?;
let total_coeff = (tok & 31) as usize;
let trailing_ones = (tok >> 5) as usize;
if total_coeff == 0 {
return Ok(0);
}
ensure!(total_coeff <= max_num_coeff, "TotalCoeff exceeds maxNumCoeff");
let mut level = [0i32; 16];
let mut suffix_length: u32 = if total_coeff > 10 && trailing_ones < 3 { 1 } else { 0 };
for (i, lv) in level.iter_mut().enumerate().take(total_coeff) {
if i < trailing_ones {
*lv = if r.read_bit()? { -1 } else { 1 };
continue;
}
// level_prefix
let bits = r.peek(32);
let level_prefix = bits.leading_zeros();
ensure!(level_prefix <= 25, "level_prefix too large");
r.skip(level_prefix as usize + 1)?;
let level_suffix_size = if level_prefix == 14 && suffix_length == 0 {
4
} else if level_prefix >= 15 {
level_prefix - 3
} else {
suffix_length
};
let level_suffix = if level_suffix_size > 0 { r.read_bits(level_suffix_size)? as i32 } else { 0 };
let mut level_code = ((level_prefix.min(15) as i32) << suffix_length) + level_suffix;
if level_prefix >= 15 && suffix_length == 0 {
level_code += 15;
}
if level_prefix >= 16 {
level_code += (1 << (level_prefix - 3)) - 4096;
}
if i == trailing_ones && trailing_ones < 3 {
level_code += 2;
}
*lv = if level_code % 2 == 0 { (level_code + 2) >> 1 } else { (-level_code - 1) >> 1 };
if suffix_length == 0 {
suffix_length = 1;
}
if lv.abs() > (3 << (suffix_length - 1)) && suffix_length < 6 {
suffix_length += 1;
}
}
let mut zeros_left = if total_coeff < end_idx - start_idx + 1 {
let tz_idx = total_coeff - 1;
let tz = match max_num_coeff {
4 => t.total_zeros_2x2[tz_idx].decode(r)?,
8 => t.total_zeros_2x4[tz_idx].decode(r)?,
_ => t.total_zeros_4x4[tz_idx].decode(r)?,
} as usize;
ensure!(tz + total_coeff <= end_idx - start_idx + 1, "total_zeros out of range");
tz
} else {
0
};
// runs and placement: highest-frequency coefficient first
let mut pos = start_idx + total_coeff + zeros_left - 1;
for (i, &lv) in level.iter().enumerate().take(total_coeff) {
coeff[pos] = lv;
if i + 1 == total_coeff {
break;
}
let run = if zeros_left > 0 {
let run = t.run_before[zeros_left.min(7) - 1].decode(r)? as usize;
ensure!(run <= zeros_left, "run_before out of range");
zeros_left -= run;
run
} else {
0
};
pos -= 1 + run;
}
Ok(total_coeff as u8)
}
#[cfg(test)]
mod tests {
use super::*;
use deckcraft_bitstream::BitWriter;
fn kraft(codes: &[&str]) -> f64 {
codes.iter().filter(|c| !c.is_empty()).map(|c| 0.5f64.powi(c.len() as i32)).sum()
}
fn prefix_free(codes: &[&str]) -> bool {
let c: Vec<&&str> = codes.iter().filter(|c| !c.is_empty()).collect();
for (i, a) in c.iter().enumerate() {
for (j, b) in c.iter().enumerate() {
if i != j && b.starts_with(**a) {
return false;
}
}
}
true
}
#[test]
fn coeff_token_tables_are_prefix_codes() {
for col in 0..6 {
let codes: Vec<&str> = COEFF_TOKEN.iter().map(|e| e.2[col]).collect();
assert!(prefix_free(&codes), "column {col}");
let k = kraft(&codes);
assert!(k <= 1.0 + 1e-12, "column {col} kraft {k}");
}
// nC>=8 is a complete 6-bit fixed code except the unused 000011 pattern
let codes: Vec<&str> = COEFF_TOKEN.iter().map(|e| e.2[3]).collect();
assert!(codes.iter().all(|c| c.len() == 6));
}
#[test]
fn total_zeros_and_run_before_are_complete_prefix_codes() {
for (i, row) in TOTAL_ZEROS_4X4.iter().enumerate() {
assert!(prefix_free(row));
// tzVlcIndex 1 leaves the all-zero 9-bit code unused; all others are complete
let k = kraft(row);
if i == 0 {
assert!((k - (1.0 - 1.0 / 512.0)).abs() < 1e-12, "{row:?}");
} else {
assert!((k - 1.0).abs() < 1e-12, "{row:?}");
}
}
for row in TOTAL_ZEROS_2X2.iter() {
assert!(prefix_free(row));
assert!((kraft(row) - 1.0).abs() < 1e-12);
}
for row in TOTAL_ZEROS_2X4.iter() {
assert!(prefix_free(row));
assert!((kraft(row) - 1.0).abs() < 1e-12);
}
for row in RUN_BEFORE.iter() {
assert!(prefix_free(row));
}
}
fn write_code(w: &mut BitWriter, code: &str) {
for b in code.bytes() {
w.write_bit(b == b'1');
}
}
#[test]
fn decodes_spec_style_example() {
// Block: 0 3 -1 0 | 0 -1 1 0 | 1 0 0 0 | 0 0 0 0 (zig-zag order), nC = 0.
// TotalCoeff 5, TrailingOnes 3: coeff_token 0000 100.
// trailing ones (reverse order): +1, -1, -1 -> sign bits 0 1 1
// level -1? No: remaining levels (reverse): 1 (idx 4?) ... use the classic example:
// coefficients in scan order: [0, 3, -1, 0, 0, -1, 1, 0, 1, 0, ...]
// reverse order non-zero: 1 (pos 8), 1 (pos 6), -1 (pos 5), -1 (pos 2), 3 (pos 1)
// TotalCoeff 5, TrailingOnes 3 (1, 1, -1).
let mut w = BitWriter::new();
write_code(&mut w, "0000100"); // coeff_token TC=5 T1=3, nC 0..2
write_code(&mut w, "0"); // +1
write_code(&mut w, "0"); // +1
write_code(&mut w, "1"); // -1
// level -1 with suffixLength 0: levelCode = 1 (T1 == 3 so no +2) -> prefix 1 => "01"
write_code(&mut w, "01");
// level 3 with suffixLength 1: levelCode = 4 -> prefix 2, suffix 0 => "001" "0"
write_code(&mut w, "0010");
// total_zeros = 4 (positions 0, 3, 4, 7) with tzVlcIndex 5 -> "110"
write_code(&mut w, "110");
// run_before: zerosLeft 4 at pos 8: run 1 -> "10"; zerosLeft 3 at pos 6: run 0 -> "11";
// zerosLeft 3 at pos 5: run 2 -> "01"; zerosLeft 1 at pos 2: run 0 -> "1"
write_code(&mut w, "10");
write_code(&mut w, "11");
write_code(&mut w, "01");
write_code(&mut w, "1");
w.rbsp_trailing();
let b = w.finish();
let mut r = BitReader::new(&b);
let mut c = [0i32; 16];
let tc = residual_block(&mut r, &mut c, 0, 15, 16, 0).unwrap();
assert_eq!(tc, 5);
assert_eq!(&c[..10], &[0, 3, -1, 0, 0, -1, 1, 0, 1, 0]);
}
}
-117
View File
@@ -1,117 +0,0 @@
// Generated from ITU-T H.264 (06/2026) Tables 9-5, 9-7..9-10 by a table-extraction script.
// Codes are bit strings; "" marks an unused entry.
/// (TrailingOnes, TotalCoeff, [nC 0..2, 2..4, 4..8, 8.., -1, -2])
#[rustfmt::skip]
pub(crate) static COEFF_TOKEN: [(u8, u8, [&str; 6]); 62] = [
(0, 0, ["1", "11", "1111", "000011", "01", "1"]),
(0, 1, ["000101", "001011", "001111", "000000", "000111", "0001111"]),
(1, 1, ["01", "10", "1110", "000001", "1", "01"]),
(0, 2, ["00000111", "000111", "001011", "000100", "000100", "0001110"]),
(1, 2, ["000100", "00111", "01111", "000101", "000110", "0001101"]),
(2, 2, ["001", "011", "1101", "000110", "001", "001"]),
(0, 3, ["000000111", "0000111", "001000", "001000", "000011", "000000111"]),
(1, 3, ["00000110", "001010", "01100", "001001", "0000011", "0001100"]),
(2, 3, ["0000101", "001001", "01110", "001010", "0000010", "0001011"]),
(3, 3, ["00011", "0101", "1100", "001011", "000101", "00001"]),
(0, 4, ["0000000111", "00000111", "0001111", "001100", "000010", "000000110"]),
(1, 4, ["000000110", "000110", "01010", "001101", "00000011", "000000101"]),
(2, 4, ["00000101", "000101", "01011", "001110", "00000010", "0001010"]),
(3, 4, ["000011", "0100", "1011", "001111", "0000000", "000001"]),
(0, 5, ["00000000111", "00000100", "0001011", "010000", "", "0000000111"]),
(1, 5, ["0000000110", "0000110", "01000", "010001", "", "0000000110"]),
(2, 5, ["000000101", "0000101", "01001", "010010", "", "000000100"]),
(3, 5, ["0000100", "00110", "1010", "010011", "", "0001001"]),
(0, 6, ["0000000001111", "000000111", "0001001", "010100", "", "00000000111"]),
(1, 6, ["00000000110", "00000110", "001110", "010101", "", "00000000110"]),
(2, 6, ["0000000101", "00000101", "001101", "010110", "", "0000000101"]),
(3, 6, ["00000100", "001000", "1001", "010111", "", "0001000"]),
(0, 7, ["0000000001011", "00000001111", "0001000", "011000", "", "000000000111"]),
(1, 7, ["0000000001110", "000000110", "001010", "011001", "", "000000000110"]),
(2, 7, ["00000000101", "000000101", "001001", "011010", "", "00000000101"]),
(3, 7, ["000000100", "000100", "1000", "011011", "", "0000000100"]),
(0, 8, ["0000000001000", "00000001011", "00001111", "011100", "", "0000000000111"]),
(1, 8, ["0000000001010", "00000001110", "0001110", "011101", "", "000000000101"]),
(2, 8, ["0000000001101", "00000001101", "0001101", "011110", "", "000000000100"]),
(3, 8, ["0000000100", "0000100", "01101", "011111", "", "00000000100"]),
(0, 9, ["00000000001111", "000000001111", "00001011", "100000", "", ""]),
(1, 9, ["00000000001110", "00000001010", "00001110", "100001", "", ""]),
(2, 9, ["0000000001001", "00000001001", "0001010", "100010", "", ""]),
(3, 9, ["00000000100", "000000100", "001100", "100011", "", ""]),
(0, 10, ["00000000001011", "000000001011", "000001111", "100100", "", ""]),
(1, 10, ["00000000001010", "000000001110", "00001010", "100101", "", ""]),
(2, 10, ["00000000001101", "000000001101", "00001101", "100110", "", ""]),
(3, 10, ["0000000001100", "00000001100", "0001100", "100111", "", ""]),
(0, 11, ["000000000001111", "000000001000", "000001011", "101000", "", ""]),
(1, 11, ["000000000001110", "000000001010", "000001110", "101001", "", ""]),
(2, 11, ["00000000001001", "000000001001", "00001001", "101010", "", ""]),
(3, 11, ["00000000001100", "00000001000", "00001100", "101011", "", ""]),
(0, 12, ["000000000001011", "0000000001111", "000001000", "101100", "", ""]),
(1, 12, ["000000000001010", "0000000001110", "000001010", "101101", "", ""]),
(2, 12, ["000000000001101", "0000000001101", "000001101", "101110", "", ""]),
(3, 12, ["00000000001000", "000000001100", "00001000", "101111", "", ""]),
(0, 13, ["0000000000001111", "0000000001011", "0000001101", "110000", "", ""]),
(1, 13, ["000000000000001", "0000000001010", "000000111", "110001", "", ""]),
(2, 13, ["000000000001001", "0000000001001", "000001001", "110010", "", ""]),
(3, 13, ["000000000001100", "0000000001100", "000001100", "110011", "", ""]),
(0, 14, ["0000000000001011", "0000000000111", "0000001001", "110100", "", ""]),
(1, 14, ["0000000000001110", "00000000001011", "0000001100", "110101", "", ""]),
(2, 14, ["0000000000001101", "0000000000110", "0000001011", "110110", "", ""]),
(3, 14, ["000000000001000", "0000000001000", "0000001010", "110111", "", ""]),
(0, 15, ["0000000000000111", "00000000001001", "0000000101", "111000", "", ""]),
(1, 15, ["0000000000001010", "00000000001000", "0000001000", "111001", "", ""]),
(2, 15, ["0000000000001001", "00000000001010", "0000000111", "111010", "", ""]),
(3, 15, ["0000000000001100", "0000000000001", "0000000110", "111011", "", ""]),
(0, 16, ["0000000000000100", "00000000000111", "0000000001", "111100", "", ""]),
(1, 16, ["0000000000000110", "00000000000110", "0000000100", "111101", "", ""]),
(2, 16, ["0000000000000101", "00000000000101", "0000000011", "111110", "", ""]),
(3, 16, ["0000000000001000", "00000000000100", "0000000010", "111111", "", ""]),
];
/// total_zeros for 4x4 blocks: [tzVlcIndex-1][total_zeros]
#[rustfmt::skip]
pub(crate) static TOTAL_ZEROS_4X4: [[&str; 16]; 15] = [
["1", "011", "010", "0011", "0010", "00011", "00010", "000011", "000010", "0000011", "0000010", "00000011", "00000010", "000000011", "000000010", "000000001"],
["111", "110", "101", "100", "011", "0101", "0100", "0011", "0010", "00011", "00010", "000011", "000010", "000001", "000000", ""],
["0101", "111", "110", "101", "0100", "0011", "100", "011", "0010", "00011", "00010", "000001", "00001", "000000", "", ""],
["00011", "111", "0101", "0100", "110", "101", "100", "0011", "011", "0010", "00010", "00001", "00000", "", "", ""],
["0101", "0100", "0011", "111", "110", "101", "100", "011", "0010", "00001", "0001", "00000", "", "", "", ""],
["000001", "00001", "111", "110", "101", "100", "011", "010", "0001", "001", "000000", "", "", "", "", ""],
["000001", "00001", "101", "100", "011", "11", "010", "0001", "001", "000000", "", "", "", "", "", ""],
["000001", "0001", "00001", "011", "11", "10", "010", "001", "000000", "", "", "", "", "", "", ""],
["000001", "000000", "0001", "11", "10", "001", "01", "00001", "", "", "", "", "", "", "", ""],
["00001", "00000", "001", "11", "10", "01", "0001", "", "", "", "", "", "", "", "", ""],
["0000", "0001", "001", "010", "1", "011", "", "", "", "", "", "", "", "", "", ""],
["0000", "0001", "01", "1", "001", "", "", "", "", "", "", "", "", "", "", ""],
["000", "001", "1", "01", "", "", "", "", "", "", "", "", "", "", "", ""],
["00", "01", "1", "", "", "", "", "", "", "", "", "", "", "", "", ""],
["0", "1", "", "", "", "", "", "", "", "", "", "", "", "", "", ""],
];
/// total_zeros for chroma DC 2x2: [tzVlcIndex-1][total_zeros]
#[rustfmt::skip]
pub(crate) static TOTAL_ZEROS_2X2: [[&str; 4]; 3] = [
["1", "01", "001", "000"],
["1", "01", "00", ""],
["1", "0", "", ""],
];
/// total_zeros for chroma DC 2x4: [tzVlcIndex-1][total_zeros]
#[rustfmt::skip]
pub(crate) static TOTAL_ZEROS_2X4: [[&str; 8]; 7] = [
["1", "010", "011", "0010", "0011", "0001", "00001", "00000"],
["000", "01", "001", "100", "101", "110", "111", ""],
["000", "001", "01", "10", "110", "111", "", ""],
["110", "00", "01", "10", "111", "", "", ""],
["00", "01", "10", "11", "", "", "", ""],
["00", "01", "1", "", "", "", "", ""],
["0", "1", "", "", "", "", "", ""],
];
/// run_before: [min(zerosLeft,7)-1][run_before]
#[rustfmt::skip]
pub(crate) static RUN_BEFORE: [[&str; 15]; 7] = [
["1", "0", "", "", "", "", "", "", "", "", "", "", "", "", ""],
["1", "01", "00", "", "", "", "", "", "", "", "", "", "", "", ""],
["11", "10", "01", "00", "", "", "", "", "", "", "", "", "", "", ""],
["11", "10", "01", "001", "000", "", "", "", "", "", "", "", "", "", ""],
["11", "10", "011", "010", "001", "000", "", "", "", "", "", "", "", "", ""],
["11", "000", "001", "011", "010", "101", "100", "", "", "", "", "", "", "", ""],
["111", "110", "101", "100", "011", "010", "001", "0001", "00001", "000001", "0000001", "00000001", "000000001", "0000000001", "00000000001"],
];
-567
View File
@@ -1,567 +0,0 @@
//! Deblocking filter (8.7) for progressive frames, 4:2:0, 8-bit.
use crate::picture::{MbKind, MbState};
use crate::slicedec::{PicState, SliceInfo};
use crate::tables::{ALPHA, BETA, TC0};
/// Reference picture identity + mvs of one 4x4 block, for bS = 1 decisions.
#[derive(Clone, Copy)]
struct Motion {
ids: [u32; 2],
mv: [[i16; 2]; 2],
count: u8,
}
/// Referenced picture ids per 8x8 block and list (u32::MAX = list unused).
fn ref_ids8(st: &MbState, sl: &SliceInfo) -> [[u32; 2]; 4] {
std::array::from_fn(|b8| {
std::array::from_fn(|l| {
let r = st.ref_idx[l][b8];
if r >= 0 { sl.ref_ids[l].get(r as usize).copied().unwrap_or(u32::MAX - 1) } else { u32::MAX }
})
})
}
#[inline(always)]
fn block_motion(st: &MbState, ids8: &[[u32; 2]; 4], raster: usize) -> Motion {
let b8 = (raster >> 3) * 2 + ((raster & 3) >> 1);
let ids = ids8[b8];
let count = (ids[0] != u32::MAX) as u8 + (ids[1] != u32::MAX) as u8;
Motion { ids, mv: [st.mv[0][raster], st.mv[1][raster]], count }
}
#[inline(always)]
fn mv_far(a: [i16; 2], b: [i16; 2]) -> bool {
(a[0] as i32 - b[0] as i32).abs() >= 4 || (a[1] as i32 - b[1] as i32).abs() >= 4
}
#[inline]
fn motion_bs(p: &Motion, q: &Motion) -> u8 {
if p.ids == q.ids && p.mv == q.mv {
return 0;
}
if p.count != q.count {
return 1;
}
if p.count == 1 {
let (pi, pm) = if p.ids[0] != u32::MAX { (p.ids[0], p.mv[0]) } else { (p.ids[1], p.mv[1]) };
let (qi, qm) = if q.ids[0] != u32::MAX { (q.ids[0], q.mv[0]) } else { (q.ids[1], q.mv[1]) };
if pi != qi {
return 1;
}
return mv_far(pm, qm) as u8;
}
if p.count == 0 {
return 0;
}
// two motion vectors each
let same_set = (p.ids[0] == q.ids[0] && p.ids[1] == q.ids[1]) || (p.ids[0] == q.ids[1] && p.ids[1] == q.ids[0]);
if !same_set {
return 1;
}
if p.ids[0] != p.ids[1] {
// two different reference pictures: compare mvs referring to the same picture
if p.ids[0] == q.ids[0] {
(mv_far(p.mv[0], q.mv[0]) || mv_far(p.mv[1], q.mv[1])) as u8
} else {
(mv_far(p.mv[0], q.mv[1]) || mv_far(p.mv[1], q.mv[0])) as u8
}
} else {
// both mvs refer to the same picture
((mv_far(p.mv[0], q.mv[0]) || mv_far(p.mv[1], q.mv[1])) && (mv_far(p.mv[0], q.mv[1]) || mv_far(p.mv[1], q.mv[0]))) as u8
}
}
#[cfg(test)]
fn clip3(lo: i32, hi: i32, v: i32) -> i32 {
v.clamp(lo, hi)
}
/// Filter one set of eight samples p3 p2 p1 p0 | q0 q1 q2 q3 across an edge (8.7.2.3 / 8.7.2.4):
/// the per-line reference for [`filter_lanes`].
#[cfg(test)]
fn filter8(v: &mut [u8; 8], bs: u8, alpha: i32, beta: i32, tc0: i32, chroma: bool) {
let p0 = v[3] as i32;
let q0 = v[4] as i32;
let p1 = v[2] as i32;
let q1 = v[5] as i32;
if (p0 - q0).abs() >= alpha || (p1 - p0).abs() >= beta || (q1 - q0).abs() >= beta {
return;
}
if chroma {
if bs < 4 {
let tc = tc0 + 1;
let delta = clip3(-tc, tc, (((q0 - p0) << 2) + (p1 - q1) + 4) >> 3);
v[3] = (p0 + delta).clamp(0, 255) as u8;
v[4] = (q0 - delta).clamp(0, 255) as u8;
} else {
v[3] = ((2 * p1 + p0 + q1 + 2) >> 2) as u8;
v[4] = ((2 * q1 + q0 + p1 + 2) >> 2) as u8;
}
return;
}
let p2 = v[1] as i32;
let q2 = v[6] as i32;
let ap = (p2 - p0).abs();
let aq = (q2 - q0).abs();
if bs < 4 {
let tc = tc0 + (ap < beta) as i32 + (aq < beta) as i32;
let delta = clip3(-tc, tc, (((q0 - p0) << 2) + (p1 - q1) + 4) >> 3);
v[3] = (p0 + delta).clamp(0, 255) as u8;
v[4] = (q0 - delta).clamp(0, 255) as u8;
if ap < beta {
v[2] = (p1 + clip3(-tc0, tc0, (p2 + ((p0 + q0 + 1) >> 1) - (p1 << 1)) >> 1)) as u8;
}
if aq < beta {
v[5] = (q1 + clip3(-tc0, tc0, (q2 + ((p0 + q0 + 1) >> 1) - (q1 << 1)) >> 1)) as u8;
}
} else {
let strong = (p0 - q0).abs() < ((alpha >> 2) + 2);
if ap < beta && strong {
let p3 = v[0] as i32;
v[3] = ((p2 + 2 * p1 + 2 * p0 + 2 * q0 + q1 + 4) >> 3) as u8;
v[2] = ((p2 + p1 + p0 + q0 + 2) >> 2) as u8;
v[1] = ((2 * p3 + 3 * p2 + p1 + p0 + q0 + 4) >> 3) as u8;
} else {
v[3] = ((2 * p1 + p0 + q1 + 2) >> 2) as u8;
}
if aq < beta && strong {
let q3 = v[7] as i32;
v[4] = ((p1 + 2 * p0 + 2 * q0 + 2 * q1 + q2 + 4) >> 3) as u8;
v[5] = ((p0 + q0 + q1 + q2 + 2) >> 2) as u8;
v[6] = ((2 * q3 + 3 * q2 + q1 + q0 + p0 + 4) >> 3) as u8;
} else {
v[4] = ((2 * q1 + q0 + p1 + 2) >> 2) as u8;
}
}
}
/// Edge filter parameters for `n` lines; `bs[i]` / `tc0[i]` apply to lines `i * n / 4 .. (i + 1) * n / 4`.
struct EdgeParams {
bs: [u8; 4],
tc0: [i32; 4],
alpha: i32,
beta: i32,
}
/// Filter one edge for `N` lines at once (lane `i` = line `i` across the edge). `s[k][i]` holds
/// sample p3 p2 p1 p0 q0 q1 q2 q3 (k = 0..8) of line `i`. Branch-free per lane, so the loops
/// vectorise; bit-exact with [`filter8`] applied to every line (8.7.2.3 / 8.7.2.4).
#[inline(always)]
fn filter_lanes<const N: usize>(s: &mut [[i16; N]; 8], ep: &EdgeParams, chroma: bool) {
let per = N / 4;
let (alpha, beta) = (ep.alpha as i16, ep.beta as i16);
let mut bs_on = [false; N];
let mut tc0 = [0i16; N];
for k in 0..4 {
for i in k * per..(k + 1) * per {
bs_on[i] = ep.bs[k] != 0;
tc0[i] = ep.tc0[k] as i16;
}
}
let [p3, p2, p1, p0, q0, q1, q2, q3] = *s;
// Masks are 0 / -1 per lane, selects are bitwise, and clipping to per-lane bounds uses
// min / max (`clamp` would assert its bounds per lane), so every loop below vectorises.
let lt = |x: i16, y: i16| -((x < y) as i16);
let sel = |m: i16, a: i16, b: i16| (a & m) | (b & !m);
let mut on = [0i16; N];
let mut any = 0i16;
for i in 0..N {
on[i] = -(bs_on[i] as i16) & lt((p0[i] - q0[i]).abs(), alpha) & lt((p1[i] - p0[i]).abs(), beta) & lt((q1[i] - q0[i]).abs(), beta);
any |= on[i];
}
if any == 0 {
return;
}
// bS 4 is only ever assigned to all four segments of an edge
let strong_edge = ep.bs[0] >= 4;
if chroma {
for i in 0..N {
let (np0, nq0) = if strong_edge {
((2 * p1[i] + p0[i] + q1[i] + 2) >> 2, (2 * q1[i] + q0[i] + p1[i] + 2) >> 2)
} else {
let tc = tc0[i] + 1;
let delta = ((((q0[i] - p0[i]) << 2) + (p1[i] - q1[i]) + 4) >> 3).max(-tc).min(tc);
((p0[i] + delta).clamp(0, 255), (q0[i] - delta).clamp(0, 255))
};
s[3][i] = sel(on[i], np0, p0[i]);
s[4][i] = sel(on[i], nq0, q0[i]);
}
return;
}
if strong_edge {
let lim = (alpha >> 2) + 2;
for i in 0..N {
let (a, b, c, d, e, f, g, h) = (p3[i], p2[i], p1[i], p0[i], q0[i], q1[i], q2[i], q3[i]);
let strong = lt((d - e).abs(), lim);
let sp = on[i] & lt((b - d).abs(), beta) & strong;
let sq = on[i] & lt((g - e).abs(), beta) & strong;
let wp0 = (2 * c + d + f + 2) >> 2;
let wq0 = (2 * f + e + c + 2) >> 2;
let sp0 = (b + 2 * c + 2 * d + 2 * e + f + 4) >> 3;
let sq0 = (c + 2 * d + 2 * e + 2 * f + g + 4) >> 3;
s[3][i] = sel(sp, sp0, sel(on[i], wp0, d));
s[2][i] = sel(sp, (b + c + d + e + 2) >> 2, c);
s[1][i] = sel(sp, (2 * a + 3 * b + c + d + e + 4) >> 3, b);
s[4][i] = sel(sq, sq0, sel(on[i], wq0, e));
s[5][i] = sel(sq, (d + e + f + g + 2) >> 2, f);
s[6][i] = sel(sq, (2 * h + 3 * g + f + e + d + 4) >> 3, g);
}
} else {
for i in 0..N {
let ap = lt((p2[i] - p0[i]).abs(), beta);
let aq = lt((q2[i] - q0[i]).abs(), beta);
let t0 = tc0[i];
let tc = t0 - ap - aq;
let delta = ((((q0[i] - p0[i]) << 2) + (p1[i] - q1[i]) + 4) >> 3).max(-tc).min(tc);
let avg = (p0[i] + q0[i] + 1) >> 1;
let np1 = p1[i] + ((p2[i] + avg - (p1[i] << 1)) >> 1).max(-t0).min(t0);
let nq1 = q1[i] + ((q2[i] + avg - (q1[i] << 1)) >> 1).max(-t0).min(t0);
s[3][i] = sel(on[i], (p0[i] + delta).clamp(0, 255), p0[i]);
s[4][i] = sel(on[i], (q0[i] - delta).clamp(0, 255), q0[i]);
s[2][i] = sel(on[i] & ap, np1, p1[i]);
s[5][i] = sel(on[i] & aq, nq1, q1[i]);
}
}
}
/// Filter a vertical edge whose q0 column is at `x`, for `N` lines starting at row `y`.
#[inline(always)]
fn filter_vertical<const N: usize>(pix: &mut [u8], stride: usize, x: usize, y: usize, ep: &EdgeParams, chroma: bool) {
// Each line's eight samples as one little-endian word: byte k = sample k (p3 .. q3).
let mut w = [0u64; N];
for (i, w) in w.iter_mut().enumerate() {
let o = (y + i) * stride + x - 4;
let Some(&b) = pix.get(o..).and_then(<[_]>::first_chunk::<8>) else {
return;
};
*w = u64::from_le_bytes(b);
}
let mut s = [[0i16; N]; 8];
for (k, row) in s.iter_mut().enumerate() {
for i in 0..N {
row[i] = ((w[i] >> (8 * k)) & 0xff) as i16;
}
}
filter_lanes(&mut s, ep, chroma);
for (i, w) in w.iter_mut().enumerate() {
*w = (0..8).fold(0u64, |acc, k| acc | ((s[k][i] as u8 as u64) << (8 * k)));
}
for (i, w) in w.iter().enumerate() {
let o = (y + i) * stride + x - 4;
pix[o..o + 8].copy_from_slice(&w.to_le_bytes());
}
}
/// Filter a horizontal edge whose q0 row is `y`, for `N` columns starting at `x`.
#[inline(always)]
fn filter_horizontal<const N: usize>(pix: &mut [u8], stride: usize, x: usize, y: usize, ep: &EdgeParams, chroma: bool) {
let base = (y - 4) * stride + x;
let mut s = [[0i16; N]; 8];
for (k, row) in s.iter_mut().enumerate() {
let Some(src) = pix.get(base + k * stride..).and_then(<[_]>::first_chunk::<N>) else {
return;
};
for i in 0..N {
row[i] = src[i] as i16;
}
}
filter_lanes(&mut s, ep, chroma);
let (k0, k1) = if chroma { (3, 5) } else { (1, 7) };
for (k, row) in s.iter().enumerate().take(k1).skip(k0) {
let Some(dst) = pix.get_mut(base + k * stride..).and_then(<[_]>::first_chunk_mut::<N>) else {
return;
};
for i in 0..N {
dst[i] = row[i] as u8;
}
}
}
/// Deblock one macroblock (macroblocks must be processed in raster order).
#[allow(clippy::needless_range_loop)]
pub fn deblock_mb(pic: &mut PicState, addr: usize, mb_w: usize) {
let q = &pic.mbs[addr];
if q.slice_num == u32::MAX {
return;
}
let sq = &pic.slices[q.slice_num as usize];
if sq.disable_deblocking_filter_idc == 1 {
return;
}
let (mx, my) = (addr % mb_w, addr / mb_w);
let usable = |n: Option<usize>| -> Option<usize> {
let n = n?;
let st = &pic.mbs[n];
if st.slice_num == u32::MAX || (sq.disable_deblocking_filter_idc == 2 && st.slice_num != q.slice_num) {
return None;
}
Some(n)
};
let left = usable(if mx > 0 { Some(addr - 1) } else { None });
let top = usable(if my > 0 { Some(addr - mb_w) } else { None });
let alpha_off = sq.alpha_offset;
let beta_off = sq.beta_offset;
let bs = boundary_strengths(pic, q, sq, [left, top]);
let qp_of = |st: &MbState| if st.kind == MbKind::IPcm { 0 } else { st.qp as i32 };
let q_qp = qp_of(q);
let q_qpc = q.qpc;
let p_qp = [left.map(|n| (qp_of(&pic.mbs[n]), pic.mbs[n].qpc)), top.map(|n| (qp_of(&pic.mbs[n]), pic.mbs[n].qpc))];
let offsets = (alpha_off, beta_off);
let width = pic.planes.width;
let cwidth = pic.planes.cwidth;
// luma
for dir in 0..2 {
for e in 0..4 {
if bs[dir][e] == [0; 4] {
continue;
}
let qpp = if e == 0 { p_qp[dir].map(|p| p.0).unwrap_or(q_qp) } else { q_qp };
let ep = edge_params(offsets, qpp, q_qp, bs[dir][e]);
if ep.alpha == 0 || ep.beta == 0 {
// filterSamplesFlag needs |p0 - q0| < alpha and |p1 - p0| < beta (8.7.2.2): no
// sample of the edge changes (low QPs)
continue;
}
if dir == 0 {
filter_vertical::<16>(&mut pic.planes.y, width, mx * 16 + e * 4, my * 16, &ep, false);
} else {
filter_horizontal::<16>(&mut pic.planes.y, width, mx * 16, my * 16 + e * 4, &ep, false);
}
}
}
// chroma (4:2:0): edges at chroma samples 0 and 4 use the bS of luma edges 0 and 2
for c in 0..2 {
for dir in 0..2 {
for ce in 0..2 {
let e = ce * 2;
if bs[dir][e] == [0; 4] {
continue;
}
let qpp = if e == 0 { p_qp[dir].map(|p| p.1[c]).unwrap_or(q_qpc[c]) } else { q_qpc[c] } as i32;
let ep = edge_params(offsets, qpp, q_qpc[c] as i32, bs[dir][e]);
if ep.alpha == 0 || ep.beta == 0 {
continue;
}
let plane = if c == 0 { &mut pic.planes.cb } else { &mut pic.planes.cr };
if dir == 0 {
filter_vertical::<8>(plane, cwidth, mx * 8 + ce * 4, my * 8, &ep, true);
} else {
filter_horizontal::<8>(plane, cwidth, mx * 8, my * 8 + ce * 4, &ep, true);
}
}
}
}
}
/// Filter parameters of one edge from the neighbouring QPs and the slice's alpha/beta offsets.
fn edge_params((alpha_off, beta_off): (i32, i32), qpp: i32, qpq: i32, b: [u8; 4]) -> EdgeParams {
let qpav = (qpp + qpq + 1) >> 1;
let index_a = (qpav + alpha_off).clamp(0, 51) as usize;
let index_b = (qpav + beta_off).clamp(0, 51) as usize;
let tc = |b: u8| if (1..4).contains(&b) { TC0[index_a][b as usize - 1] as i32 } else { 0 };
EdgeParams { bs: b, tc0: [tc(b[0]), tc(b[1]), tc(b[2]), tc(b[3])], alpha: ALPHA[index_a] as i32, beta: BETA[index_b] as i32 }
}
/// bS[dir][edge][segment]; dir 0 = vertical edges (x), 1 = horizontal edges (y). `neighbors` is
/// `[left, top]`.
fn boundary_strengths(pic: &PicState, q: &MbState, sq: &SliceInfo, neighbors: [Option<usize>; 2]) -> [[[u8; 4]; 4]; 2] {
if q.kind.is_intra() { intra_strengths(q.transform_8x8, neighbors) } else { inter_strengths(pic, q, sq, neighbors) }
}
fn intra_strengths(t8: bool, neighbors: [Option<usize>; 2]) -> [[[u8; 4]; 4]; 2] {
let mut bs = [[[0u8; 4]; 4]; 2];
for dir in 0..2 {
if neighbors[dir].is_some() {
bs[dir][0] = [4; 4];
}
for e in 1..4 {
if !(t8 && e % 2 == 1) {
bs[dir][e] = [3; 4];
}
}
}
bs
}
fn inter_strengths(pic: &PicState, q: &MbState, sq: &SliceInfo, neighbors: [Option<usize>; 2]) -> [[[u8; 4]; 4]; 2] {
let mut bs = [[[0u8; 4]; 4]; 2];
let t8 = q.transform_8x8;
let qids = ref_ids8(q, sq);
let q_uniform = (0..2).all(|l| {
let r = q.ref_idx[l];
r[1] == r[0] && r[2] == r[0] && r[3] == r[0] && q.mv[l].iter().all(|m| *m == q.mv[l][0])
});
let qm: [Motion; 16] = std::array::from_fn(|r| block_motion(q, &qids, r));
for dir in 0..2 {
if let Some(n) = neighbors[dir] {
bs[dir][0] = edge_strengths_to_neighbor(&pic.mbs[n], &pic.slices[pic.mbs[n].slice_num as usize], q, &qm, dir);
}
if q_uniform && q.nz_mask == 0 {
// one motion for the whole MB and no coefficients: all internal edges have bS 0
continue;
}
for e in 1..4 {
if t8 && e % 2 == 1 {
continue;
}
for k in 0..4 {
let (rq, rp) = if dir == 0 { (k * 4 + e, k * 4 + e - 1) } else { (e * 4 + k, e * 4 + k - 4) };
bs[dir][e][k] = if (q.nz_mask >> rp) & 1 != 0 || (q.nz_mask >> rq) & 1 != 0 { 2 } else { motion_bs(&qm[rp], &qm[rq]) };
}
}
}
bs
}
/// bS of the macroblock edge shared with neighbour `p` (left for `dir` 0, top for `dir` 1).
fn edge_strengths_to_neighbor(p: &MbState, p_slice: &SliceInfo, q: &MbState, qm: &[Motion; 16], dir: usize) -> [u8; 4] {
if p.kind.is_intra() {
return [4; 4];
}
let pids = ref_ids8(p, p_slice);
std::array::from_fn(|k| {
let (rq, rp) = if dir == 0 { (k * 4, k * 4 + 3) } else { (k, 12 + k) };
if (p.nz_mask >> rp) & 1 != 0 || (q.nz_mask >> rq) & 1 != 0 { 2 } else { motion_bs(&block_motion(p, &pids, rp), &qm[rq]) }
})
}
#[cfg(test)]
mod tests {
use super::*;
/// The vectorised edge filter equals the per-line reference [`filter8`] on random edges
/// (small differences across the edge so that every filter branch is taken).
#[test]
fn lane_filter_matches_per_line_reference() {
let mut rng = 0x9e37_79b9_7f4a_7c15u64;
let mut next = move || {
rng ^= rng << 13;
rng ^= rng >> 7;
rng ^= rng << 17;
rng
};
for iter in 0..20_000 {
let chroma = iter % 3 == 0;
let strong = iter % 4 == 1;
let bs: [u8; 4] = if strong { [4; 4] } else { std::array::from_fn(|_| (next() % 4) as u8) };
let index_a = (next() % 52) as usize;
let index_b = (next() % 52) as usize;
let tc = |b: u8| if (1..4).contains(&b) { TC0[index_a][b as usize - 1] as i32 } else { 0 };
let ep = EdgeParams { bs, tc0: bs.map(tc), alpha: ALPHA[index_a] as i32, beta: BETA[index_b] as i32 };
let base = (next() % 256) as i32;
let spread = 1 + (next() % 24) as i32;
let mut lines = [[0u8; 8]; 16];
for l in lines.iter_mut() {
for v in l.iter_mut() {
*v = (base + (next() % (2 * spread as u64 + 1)) as i32 - spread).clamp(0, 255) as u8;
}
}
let n = if chroma { 8 } else { 16 };
let mut want = lines;
for (i, l) in want.iter_mut().enumerate().take(n) {
let k = i / (n / 4);
if bs[k] != 0 {
filter8(l, bs[k], ep.alpha, ep.beta, ep.tc0[k], chroma);
}
}
let got: Vec<[u8; 8]> = if chroma {
let mut s = [[0i16; 8]; 8];
for i in 0..8 {
for k in 0..8 {
s[k][i] = lines[i][k] as i16;
}
}
filter_lanes(&mut s, &ep, true);
(0..8).map(|i| std::array::from_fn(|k| s[k][i] as u8)).collect()
} else {
let mut s = [[0i16; 16]; 8];
for i in 0..16 {
for k in 0..8 {
s[k][i] = lines[i][k] as i16;
}
}
filter_lanes(&mut s, &ep, false);
(0..16).map(|i| std::array::from_fn(|k| s[k][i] as u8)).collect()
};
assert_eq!(&got[..], &want[..n], "iteration {iter}: bs {bs:?} indexA {index_a} indexB {index_b}");
}
}
}
#[cfg(test)]
mod speed {
use super::*;
/// Rough timing of the edge filters (`cargo test --release -p deckcraft-h264 --lib edge_filter_speed -- --ignored --nocapture`).
#[test]
#[ignore]
fn edge_filter_speed() {
let (w, h) = (1024usize, 256usize);
let mut pix: Vec<u8> = (0..w * h).map(|i| (128 + ((i * 7919) % 13) as i32 - 6) as u8).collect();
let eps = [
EdgeParams { bs: [4; 4], tc0: [0; 4], alpha: 40, beta: 9 },
EdgeParams { bs: [3; 4], tc0: [2; 4], alpha: 40, beta: 9 },
EdgeParams { bs: [2, 1, 0, 2], tc0: [1, 1, 0, 1], alpha: 40, beta: 9 },
];
let t0 = std::time::Instant::now();
let mut n = 0;
for _ in 0..20 {
for my in 1..h / 16 {
for mx in 1..w / 16 {
for (j, ep) in eps.iter().enumerate() {
filter_horizontal::<16>(&mut pix, w, mx * 16, my * 16 + j * 4, ep, false);
n += 1;
}
}
}
}
let dt = t0.elapsed();
let tv = std::time::Instant::now();
for _ in 0..20 {
for my in 1..h / 16 {
for mx in 1..w / 16 {
for (j, ep) in eps.iter().enumerate() {
filter_vertical::<16>(&mut pix, w, mx * 16 + j * 4, my * 16, ep, false);
}
}
}
}
println!("vertical: {:.1} ns/edge", tv.elapsed().as_nanos() as f64 / n as f64);
let t1 = std::time::Instant::now();
for _ in 0..20 {
for my in 1..h / 16 {
for mx in 1..w / 16 {
for ep in eps.iter() {
for (vert, x, y) in [(true, mx * 16, my * 16), (false, mx * 16, my * 16)] {
for i in 0..16 {
let b = ep.bs[i / 4];
if b == 0 {
continue;
}
let mut v = [0u8; 8];
for k in 0..8 {
v[k] = if vert { pix[(y + i) * w + x - 4 + k] } else { pix[(y - 4 + k) * w + x + i] };
}
filter8(&mut v, b, ep.alpha, ep.beta, ep.tc0[i / 4], false);
for k in 1..7 {
if vert {
pix[(y + i) * w + x - 4 + k] = v[k];
} else {
pix[(y - 4 + k) * w + x + i] = v[k];
}
}
}
}
}
}
}
}
println!("scalar reference (both): {:.1} ns/edge", t1.elapsed().as_nanos() as f64 / (2 * n) as f64);
println!(
"horizontal: {n} luma edges in {dt:?}: {:.1} ns/edge (checksum {})",
dt.as_nanos() as f64 / n as f64,
pix.iter().map(|&v| v as u64).sum::<u64>()
);
}
}
-689
View File
@@ -1,689 +0,0 @@
//! Top-level decoder: NAL dispatch, picture boundaries, POC, DPB and output.
//!
//! The calling thread parses headers and does all DPB / reference list bookkeeping (which only needs
//! header information). Macroblock decoding of each picture runs as a job — on a thread pool with the
//! `threads` feature — that publishes finished macroblock rows into the picture's shared [`Frame`];
//! jobs of later pictures block per row on the reference data they need, so several pictures decode
//! concurrently (frame-level parallelism).
use crate::dpb::{Dpb, Output, OutputMeta};
use crate::error::{Error, Result, ensure, invalid, unsupported};
use crate::params::{Pps, Sps};
use crate::picture::{Frame, FrameRef, MbKind, MbState, Planes, RefPic};
use crate::slice::{NalHeader, Poc, PocState, SliceHeader, SliceType, nal_type};
use crate::slicedec::{PicState, SliceDecoder};
use crate::transform::LevelScale;
use crate::{ColorInfo, Picture};
use deckcraft_bitstream::{BitReader, annexb_nals, length_prefixed_nals, unescape_rbsp};
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
/// One slice of a picture job.
struct SliceJob {
sh: SliceHeader,
pps: Arc<Pps>,
sps: Arc<Sps>,
rbsp: Vec<u8>,
refs: [Vec<RefPic>; 2],
ls: Arc<LevelScale>,
}
/// A picture whose slices are being collected.
struct PendingPic {
frame: FrameRef,
sps: Arc<Sps>,
first: SliceHeader,
poc: Poc,
pts: i64,
key: bool,
slices: Vec<SliceJob>,
}
/// Counters describing what the decoder has seen (useful to check test coverage).
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct DecodeStats {
pub pictures: u64,
pub slices_cavlc: u64,
pub slices_cabac: u64,
pub slices_i: u64,
pub slices_p: u64,
pub slices_b: u64,
pub mb_i4x4: u64,
pub mb_i8x8: u64,
pub mb_i16x16: u64,
pub mb_pcm: u64,
pub mb_p_skip: u64,
pub mb_b_skip: u64,
pub mb_b_direct16x16: u64,
pub mb_inter: u64,
/// Inter macroblocks using the 8x8 transform.
pub mb_inter_8x8_transform: u64,
pub mmco_ops: u64,
pub long_term_marks: u64,
pub frame_num_gaps: u64,
pub weighted_slices: u64,
pub temporal_direct_slices: u64,
/// Non-reference pictures decoded in draft mode (deblocking skipped).
pub draft_pictures: u64,
}
/// State shared with decoding jobs.
#[derive(Default)]
struct Shared {
error: Mutex<Option<Error>>,
stats: Mutex<DecodeStats>,
/// Recycled picture buffers.
pool: Mutex<Vec<PicState>>,
}
/// H.264 decoder.
pub struct Decoder {
spss: Vec<Option<Arc<Sps>>>,
ppss: Vec<Option<Arc<Pps>>>,
nal_length_size: Option<usize>,
dpb: Dpb,
poc_state: PocState,
prev_ref_frame_num: u32,
pending: Option<PendingPic>,
next_id: u32,
active_sps: Option<Arc<Sps>>,
ls_cache: Vec<(Arc<Pps>, Arc<LevelScale>)>,
/// Pictures leaving the DPB, converted once their decoding job has finished.
out_queue: VecDeque<Output>,
shared: Arc<Shared>,
#[cfg(feature = "threads")]
pool: Option<rayon::ThreadPool>,
in_flight: VecDeque<FrameRef>,
max_in_flight: usize,
/// Bytes the picture store may take ([`deckcraft_bitstream::budget`]).
memory_budget: u64,
threads: usize,
/// Draft mode: skip deblocking of non-reference pictures ([`Decoder::set_draft`]).
draft: bool,
}
impl Default for Decoder {
fn default() -> Self {
Self::new()
}
}
/// Is `sh` the first slice of a new primary coded picture relative to `prev` (7.4.1.2.4)?
fn is_new_picture(prev: &SliceHeader, sh: &SliceHeader, sps: &Sps) -> bool {
if sh.frame_num != prev.frame_num
|| sh.pps_id != prev.pps_id
|| sh.field_pic != prev.field_pic
|| sh.bottom_field != prev.bottom_field
|| (sh.nal_ref_idc == 0) != (prev.nal_ref_idc == 0)
|| sh.idr != prev.idr
|| (sh.idr && prev.idr && sh.idr_pic_id != prev.idr_pic_id)
{
return true;
}
if sps.pic_order_cnt_type == 0
&& (sh.pic_order_cnt_lsb != prev.pic_order_cnt_lsb || sh.delta_pic_order_cnt_bottom != prev.delta_pic_order_cnt_bottom)
{
return true;
}
if sps.pic_order_cnt_type == 1 && sh.delta_pic_order_cnt != prev.delta_pic_order_cnt {
return true;
}
false
}
/// The worker threads [`Decoder::new`] uses: the available cores (at most 16) with the `threads`
/// feature on native targets, 1 otherwise.
pub fn default_threads() -> usize {
#[cfg(all(feature = "threads", not(target_arch = "wasm32")))]
{
std::thread::available_parallelism().map(|n| n.get()).unwrap_or(1).min(16)
}
#[cfg(not(all(feature = "threads", not(target_arch = "wasm32"))))]
{
1
}
}
impl Decoder {
/// A decoder using all available cores (with the `threads` feature).
pub fn new() -> Self {
Self::with_threads(default_threads())
}
/// A decoder using up to `threads` worker threads (1 = decode on the calling thread).
pub fn with_threads(threads: usize) -> Self {
crate::cavlc::init_tables();
#[cfg(feature = "threads")]
let pool = if threads > 1 && cfg!(not(target_arch = "wasm32")) {
rayon::ThreadPoolBuilder::new().num_threads(threads).thread_name(|i| format!("h264-{i}")).build().ok()
} else {
None
};
#[cfg(feature = "threads")]
let workers = if pool.is_some() { threads } else { 1 };
#[cfg(not(feature = "threads"))]
let workers = 1;
Decoder {
spss: vec![None; 32],
ppss: vec![None; 256],
nal_length_size: None,
dpb: Dpb::new(),
poc_state: PocState::default(),
prev_ref_frame_num: 0,
pending: None,
next_id: 1,
active_sps: None,
ls_cache: Vec::new(),
out_queue: VecDeque::new(),
shared: Arc::new(Shared::default()),
#[cfg(feature = "threads")]
pool,
in_flight: VecDeque::new(),
max_in_flight: threads.max(1) + 2,
memory_budget: deckcraft_bitstream::budget::DECODE_MEMORY_BUDGET,
threads: workers,
draft: false,
}
}
/// Worker threads decoding pictures (1: on the calling thread).
pub fn threads(&self) -> usize {
self.threads
}
/// Draft mode (off by default) for reduced-resolution playback: pictures submitted from now
/// on that are non-reference pictures (`nal_ref_idc` 0) skip the deblocking filter (8.7).
/// Nothing predicts from a non-reference picture (and deblocking never changes the motion
/// data used for direct prediction), so every other picture stays bit-exact; the draft
/// pictures themselves are approximate and flagged [`Picture::draft`]. Never use it for
/// export or for a frame shown while paused.
pub fn set_draft(&mut self, on: bool) {
self.draft = on;
}
pub fn draft(&self) -> bool {
self.draft
}
/// Configure from an `avcC` (AVCDecoderConfigurationRecord) box payload.
pub fn from_avcc(avcc: &[u8]) -> Result<Self> {
let mut d = Decoder::new();
d.configure_avcc(avcc)?;
Ok(d)
}
/// Parse an `avcC` record: SPS/PPS and the NAL length size used by [`Decoder::decode`].
pub fn configure_avcc(&mut self, avcc: &[u8]) -> Result<()> {
ensure!(avcc.len() >= 7, "avcC too short");
ensure!(avcc[0] == 1, "unsupported avcC version {}", avcc[0]);
let mut pos = 5;
let mut sets = Vec::new();
let read_sets = |pos: &mut usize, count: usize, sets: &mut Vec<(usize, usize)>| -> Result<()> {
for _ in 0..count {
ensure!(*pos + 2 <= avcc.len(), "avcC truncated");
let len = u16::from_be_bytes([avcc[*pos], avcc[*pos + 1]]) as usize;
*pos += 2;
ensure!(*pos + len <= avcc.len(), "avcC truncated");
sets.push((*pos, len));
*pos += len;
}
Ok(())
};
let nsps = (avcc[pos] & 0x1f) as usize;
pos += 1;
read_sets(&mut pos, nsps, &mut sets)?;
ensure!(pos < avcc.len(), "avcC truncated");
let npps = avcc[pos] as usize;
pos += 1;
read_sets(&mut pos, npps, &mut sets)?;
for (i, (start, len)) in sets.iter().enumerate() {
let nal = &avcc[*start..*start + *len];
let header = nal.first().copied().ok_or_else(|| Error::Invalid("empty avcC parameter set".into()))?;
let expected = if i < nsps { nal_type::SPS } else { nal_type::PPS };
ensure!(header & 0x1f == expected, "avcC parameter set has NAL type {}, expected {expected}", header & 0x1f);
}
for (start, len) in sets {
self.handle_nal(&avcc[start..start + len], 0)?;
}
self.nal_length_size = Some((avcc[4] & 3) as usize + 1);
Ok(())
}
/// Length of NAL length prefixes (from avcC), or None for Annex-B input.
pub fn nal_length_size(&self) -> Option<usize> {
self.nal_length_size
}
/// Decode one access unit (all NAL units of one picture, Annex-B or length-prefixed per
/// configuration; a byte stream with several complete access units is accepted too). Returns
/// pictures that became ready for output, in output order; `pts` travels with the picture of
/// this access unit through reordering.
pub fn decode(&mut self, data: &[u8], pts: i64) -> Result<Vec<Picture>> {
let nals = match self.nal_length_size {
Some(n) => length_prefixed_nals(data, n)?,
None => annexb_nals(data),
};
let mut result = Ok(());
for nal in nals {
if let Err(e) = self.handle_nal(nal, pts) {
result = Err(e);
break;
}
}
// The access unit is complete: hand its picture to a decoding job.
self.submit_pending();
if let Some(e) = self.take_error() {
result = result.and(Err(e));
}
result?;
// Return finished pictures without blocking on frames still being decoded (bounded latency).
let max_queue = self.max_in_flight * 2;
let mut pics = Vec::new();
while let Some(o) = self.out_queue.front() {
if !o.frame.is_published(o.frame.mb_h() - 1) && self.out_queue.len() <= max_queue {
break;
}
let Some(o) = self.out_queue.pop_front() else { break };
pics.push(make_picture(&o));
}
Ok(pics)
}
/// Output all remaining pictures (end of stream).
pub fn flush(&mut self) -> Vec<Picture> {
self.submit_pending();
let mut outs = Vec::new();
self.dpb.flush(&mut outs);
self.emit(outs);
let pics = self.out_queue.drain(..).map(|o| make_picture(&o)).collect();
while let Some(f) = self.in_flight.pop_front() {
f.wait_complete();
}
pics
}
/// Statistics accumulated so far (macroblock counts include finished pictures only).
pub fn stats(&self) -> DecodeStats {
self.shared.stats.lock().map(|s| s.clone()).unwrap_or_default()
}
fn stat(&self, f: impl FnOnce(&mut DecodeStats)) {
if let Ok(mut s) = self.shared.stats.lock() {
f(&mut s);
}
}
/// First error reported by a decoding job since the last call (also returned by
/// [`Decoder::decode`]); useful after [`Decoder::flush`].
pub fn take_error(&mut self) -> Option<Error> {
self.shared.error.lock().ok().and_then(|mut e| e.take())
}
fn handle_nal(&mut self, nal: &[u8], pts: i64) -> Result<()> {
if nal.is_empty() {
return Ok(());
}
let hdr = NalHeader::parse(nal[0])?;
match hdr.nal_unit_type {
nal_type::SPS => {
let sps = Sps::parse(&unescape_rbsp(&nal[1..]))?;
let id = sps.id as usize;
self.spss[id] = Some(Arc::new(sps));
}
nal_type::PPS => {
let rbsp = unescape_rbsp(&nal[1..]);
let spss: Vec<Option<Sps>> = self.spss.iter().map(|s| s.as_ref().map(|s| (**s).clone())).collect();
let pps = Pps::parse(&rbsp, &spss)?;
let id = pps.id as usize;
self.ppss[id] = Some(Arc::new(pps));
}
nal_type::SLICE | nal_type::IDR => self.handle_slice(nal, hdr, pts)?,
nal_type::SLICE_DPA | nal_type::SLICE_DPB | nal_type::SLICE_DPC => {
return unsupported("data partitioning (Extended profile)");
}
nal_type::END_SEQ | nal_type::END_STREAM => self.submit_pending(),
// SEI, AUD, filler, SPS extension, prefix NAL, subset SPS, auxiliary and extension slices: skipped.
_ => {}
}
Ok(())
}
fn level_scale(&mut self, pps: &Arc<Pps>) -> Arc<LevelScale> {
if let Some((_, ls)) = self.ls_cache.iter().find(|(p, _)| Arc::ptr_eq(p, pps)) {
return ls.clone();
}
let ls: Arc<LevelScale> = Arc::from(LevelScale::new(&pps.scaling));
self.ls_cache.retain(|(p, _)| self.ppss.iter().flatten().any(|q| Arc::ptr_eq(p, q)));
self.ls_cache.push((pps.clone(), ls.clone()));
ls
}
fn handle_slice(&mut self, nal: &[u8], hdr: NalHeader, pts: i64) -> Result<()> {
let rbsp = unescape_rbsp(&nal[1..]);
let ppss = &self.ppss;
let spss = &self.spss;
let mut found: Option<(Arc<Pps>, Arc<Sps>)> = None;
let (sh, _, _) = SliceHeader::parse(&rbsp, hdr, |id| {
let pps = ppss[id as usize].as_ref().ok_or_else(|| Error::MissingParameterSet(format!("PPS {id}")))?;
let sps = spss[pps.sps_id as usize].as_ref().ok_or_else(|| Error::MissingParameterSet(format!("SPS {}", pps.sps_id)))?;
found = Some((pps.clone(), sps.clone()));
Ok((&**pps, &**sps))
})?;
let Some((pps, sps)) = found else {
return Err(Error::MissingParameterSet("PPS".into()));
};
sps.check_supported()?;
if pps.num_slice_groups > 1 {
return unsupported("slice groups (FMO)");
}
if sh.field_pic {
return unsupported("field pictures");
}
if matches!(sh.slice_type, SliceType::Sp | SliceType::Si) {
return unsupported("SP/SI slices");
}
if sh.redundant_pic_cnt > 0 {
return Ok(()); // redundant slices are ignored
}
let new_pic = match &self.pending {
None => true,
Some(p) => is_new_picture(&p.first, &sh, &sps) || sh.first_mb_in_slice == 0 || !Arc::ptr_eq(&p.sps, &sps),
};
if new_pic {
self.submit_pending();
if sh.first_mb_in_slice != 0 && self.active_sps.is_none() {
return invalid("stream does not start with the first slice of a picture");
}
self.start_picture(&sh, &sps, pts)?;
}
let ls = self.level_scale(&pps);
let Some(pending) = self.pending.as_mut() else {
return invalid("slice without a started picture");
};
let refs = self.dpb.build_ref_lists(&sh, pending.poc.frame(), sps.max_frame_num())?;
if !sh.slice_type.is_intra() {
ensure!(!refs[0].is_empty(), "no reference pictures available for inter slice");
if sh.slice_type.is_b() {
ensure!(!refs[1].is_empty(), "empty RefPicList1 in B slice");
}
}
let weighted = (pps.weighted_pred && sh.slice_type.is_p()) || (pps.weighted_bipred_idc != 0 && sh.slice_type.is_b());
let temporal = sh.slice_type.is_b() && !sh.direct_spatial_mv_pred;
let (cabac, st, mmcos, lt) = (
pps.entropy_coding_mode,
sh.slice_type,
sh.mmcos.len() as u64,
sh.long_term_reference as u64 + sh.mmcos.iter().filter(|m| m.op == 3 || m.op == 6).count() as u64,
);
pending.slices.push(SliceJob { sh, pps, sps, rbsp, refs, ls });
self.stat(|s| {
if cabac {
s.slices_cabac += 1;
} else {
s.slices_cavlc += 1;
}
match st {
SliceType::I | SliceType::Si => s.slices_i += 1,
SliceType::P | SliceType::Sp => s.slices_p += 1,
SliceType::B => s.slices_b += 1,
}
s.weighted_slices += weighted as u64;
s.temporal_direct_slices += temporal as u64;
s.mmco_ops += mmcos;
s.long_term_marks += lt;
});
Ok(())
}
/// Cap the bytes of decoded pictures (current, references, in flight) this decoder may hold.
pub fn set_memory_budget(&mut self, bytes: u64) {
self.memory_budget = bytes;
}
/// Refuse an SPS whose pictures would not fit the memory budget, before anything is allocated.
fn check_budget(&self, sps: &Sps) -> Result<()> {
let chroma = sps.chroma_array_type();
let shape = deckcraft_bitstream::budget::PictureShape {
width: u64::from(sps.width()),
height: u64::from(sps.height()),
shift_x: u32::from(chroma != 3),
shift_y: u32::from(chroma == 1),
chroma_planes: if chroma == 0 { 0 } else { 2 },
bytes_per_sample: 1,
};
let pictures = (sps.max_dpb_frames() + 1 + self.max_in_flight) as u64;
ensure!(
shape.fits(pictures, self.memory_budget),
"{}x{} pictures with {pictures} in the decoder exceed the memory budget",
sps.width(),
sps.height()
);
Ok(())
}
fn start_picture(&mut self, sh: &SliceHeader, sps: &Arc<Sps>, pts: i64) -> Result<()> {
self.check_budget(sps)?;
// Activate SPS; a resolution change flushes the DPB.
let changed = match &self.active_sps {
None => true,
Some(a) => a.width() != sps.width() || a.height() != sps.height() || a.max_dpb_frames() != sps.max_dpb_frames(),
};
if changed && self.active_sps.is_some() {
let mut outs = Vec::new();
self.dpb.flush(&mut outs);
self.emit(outs);
self.dpb.entries.clear();
}
self.active_sps = Some(sps.clone());
self.dpb.capacity = sps.max_dpb_frames();
self.dpb.max_reorder = sps.max_num_reorder_frames().min(self.dpb.capacity);
let mb_w = sps.pic_width_in_mbs as usize;
let mb_h = sps.frame_height_in_mbs() as usize;
if sh.idr {
let mut outs = Vec::new();
self.dpb.idr(sh.no_output_of_prior_pics, &mut outs);
self.emit(outs);
self.prev_ref_frame_num = 0;
} else {
let max = sps.max_frame_num();
if sh.frame_num != self.prev_ref_frame_num && sh.frame_num != (self.prev_ref_frame_num + 1) % max {
// A gap is only legal when the SPS allows it (7.4.2.1.1), but a cut or lossy stream has
// them anyway: those are concealed too rather than dropping the picture.
// frame_num gap (8.2.5.2): insert "non-existing" frames. Their samples are never used by
// conforming streams; copy the latest decoded frame for concealment.
let meta = Arc::new(output_meta(sps, pts, false));
let next_id = &mut self.next_id;
let poc_state = &mut self.poc_state;
let last = self.dpb.entries.iter().filter(|e| !e.non_existing).max_by_key(|e| e.frame.id).map(|e| e.frame.clone());
let mut make = |_fnum: u32| {
let id = *next_id;
*next_id += 1;
let planes = match &last {
Some(f) if f.mb_w == mb_w && f.mb_h() == mb_h => {
let mut p = Planes::new(mb_w * 16, mb_h * 16);
let (y, u, v) = f.copy_cropped((0, 0, mb_w * 16, mb_h * 16));
p.y = y;
p.cb = u;
p.cr = v;
p
}
_ => Planes::gray(mb_w * 16, mb_h * 16),
};
(Arc::new(Frame::from_planes(id, 0, &planes)), meta.clone())
};
let mut upd = |fnum: u32| poc_state.update_gap_frame(fnum, sps);
self.shared.stats.lock().map(|mut s| s.frame_num_gaps += 1).ok();
self.dpb.fill_frame_num_gap(self.prev_ref_frame_num, sh.frame_num, max, sps.max_num_ref_frames as usize, &mut make, &mut upd);
self.prev_ref_frame_num = (sh.frame_num + max - 1) % max;
}
}
let poc = self.poc_state.compute(sh, sps);
// MMCO5: the picture's POC becomes relative to itself (tempPicOrderCnt, 8.2.1).
let frame_poc = if sh.has_mmco5() { 0 } else { poc.frame() };
let id = self.next_id;
self.next_id += 1;
let frame = Arc::new(Frame::new(id, frame_poc, mb_w, mb_h));
self.pending = Some(PendingPic { frame, sps: sps.clone(), first: sh.clone(), poc, pts, key: sh.idr, slices: Vec::new() });
Ok(())
}
/// Start decoding the pending picture and do its reference marking / DPB insertion.
fn submit_pending(&mut self) {
let Some(p) = self.pending.take() else { return };
let PendingPic { frame, sps, first, poc, pts, key, slices } = p;
let draft = self.draft && first.nal_ref_idc == 0;
self.dispatch(frame.clone(), slices, draft);
self.poc_state.update(&first, &poc);
if first.nal_ref_idc != 0 {
self.prev_ref_frame_num = if first.has_mmco5() { 0 } else { first.frame_num };
}
let meta = Arc::new(OutputMeta { draft, ..output_meta(&sps, pts, key) });
let mut outs = Vec::new();
let fpoc = frame.poc;
self.dpb.store_picture(&first, frame, fpoc, sps.max_frame_num(), sps.max_num_ref_frames as usize, meta, &mut outs);
self.emit(outs);
}
fn dispatch(&mut self, frame: FrameRef, slices: Vec<SliceJob>, draft: bool) {
let shared = self.shared.clone();
#[cfg(feature = "threads")]
if let Some(pool) = &self.pool {
self.in_flight.retain(|f| !f.is_published(f.mb_h() - 1));
while self.in_flight.len() >= self.max_in_flight {
if let Some(f) = self.in_flight.pop_front() {
f.wait_complete();
}
}
self.in_flight.push_back(frame.clone());
pool.spawn_fifo(move || run_job(frame, slices, &shared, draft));
return;
}
run_job(frame, slices, &shared, draft);
}
fn emit(&mut self, outs: Vec<Output>) {
self.out_queue.extend(outs);
}
}
/// Decode all slices of one picture and publish its rows.
fn run_job(frame: FrameRef, slices: Vec<SliceJob>, shared: &Shared, draft: bool) {
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let reuse = shared.pool.lock().ok().and_then(|mut p| p.pop());
let mut pic = match reuse {
Some(mut p) if p.mb_w == frame.mb_w && p.mb_h == frame.mb_h() => {
p.reset(frame.clone());
p
}
_ => PicState::new(frame.clone()),
};
pic.skip_deblock = draft;
let mut first_err = None;
for s in &slices {
if let Err(e) = decode_slice(s, &mut pic) {
first_err.get_or_insert(e);
}
}
pic.flush_rows(true);
(pic, first_err)
}));
let (pic, err) = match result {
Ok((pic, err)) => (Some(pic), err),
Err(_) => {
// Make sure readers never block on this frame.
let fallback = Planes::gray(frame.width, frame.height);
let blank = vec![MbState::default(); frame.mb_w * frame.mb_h()];
for r in 0..frame.mb_h() {
if !frame.is_published(r) {
frame.publish(r, Frame::make_row(&fallback, r, &blank, &|_, _, _| u32::MAX));
}
}
(None, Some(Error::Invalid("internal error while decoding a picture".into())))
}
};
if let Ok(mut s) = shared.stats.lock() {
s.pictures += 1;
s.draft_pictures += draft as u64;
if let Some(pic) = &pic {
for st in &pic.mbs {
match st.kind {
MbKind::I4x4 => s.mb_i4x4 += 1,
MbKind::I8x8 => s.mb_i8x8 += 1,
MbKind::I16x16 => s.mb_i16x16 += 1,
MbKind::IPcm => s.mb_pcm += 1,
MbKind::PSkip => s.mb_p_skip += 1,
MbKind::BSkip => s.mb_b_skip += 1,
MbKind::BDirect16x16 => s.mb_b_direct16x16 += 1,
MbKind::Inter => s.mb_inter += 1,
MbKind::None => {}
}
if st.transform_8x8 && !st.kind.is_intra() {
s.mb_inter_8x8_transform += 1;
}
}
}
}
if let (Some(e), Ok(mut slot)) = (err, shared.error.lock()) {
slot.get_or_insert(e);
}
if let (Some(pic), Ok(mut p)) = (pic, shared.pool.lock())
&& p.len() < 4
{
p.push(pic);
}
}
fn decode_slice(s: &SliceJob, pic: &mut PicState) -> Result<()> {
let mut sd = SliceDecoder::new(&s.sh, &s.pps, &s.sps, pic, &s.refs, &s.ls)?;
if s.pps.entropy_coding_mode {
sd.decode_cabac(&s.rbsp)
} else {
let mut r = BitReader::new(&s.rbsp);
r.seek_bits(s.sh.header_bits);
sd.decode_cavlc(&mut r)
}
}
fn output_meta(sps: &Sps, pts: i64, key: bool) -> OutputMeta {
let vui = sps.vui.clone().unwrap_or_default();
OutputMeta {
pts,
key,
crop: sps.crop_rect(),
full_range: vui.full_range,
colour_primaries: vui.colour_primaries,
transfer_characteristics: vui.transfer_characteristics,
matrix_coefficients: vui.matrix_coefficients,
sar: vui.sar,
draft: false,
}
}
fn make_picture(o: &Output) -> Picture {
let (cx, cy, cw, ch) = o.meta.crop;
let (cx, cy, cw, ch) = (cx as usize, cy as usize, cw as usize, ch as usize);
let (y, u, v) = o.frame.copy_cropped((cx, cy, cw, ch));
let (ccw, cch) = (cw.div_ceil(2), ch.div_ceil(2));
Picture {
width: cw as u32,
height: ch as u32,
chroma_width: ccw as u32,
chroma_height: cch as u32,
y,
u,
v,
y_stride: cw,
uv_stride: ccw,
pts: o.meta.pts,
poc: o.poc,
key: o.meta.key,
color: ColorInfo {
full_range: o.meta.full_range,
primaries: o.meta.colour_primaries,
transfer: o.meta.transfer_characteristics,
matrix: o.meta.matrix_coefficients,
},
sar: o.meta.sar,
draft: o.meta.draft,
}
}
-597
View File
@@ -1,597 +0,0 @@
//! Decoded picture buffer: reference marking (8.2.5), reference list construction (8.2.4) and output
//! ordering / bumping (C.4).
use crate::error::{Result, ensure};
use crate::picture::{FrameRef, RefPic};
use crate::slice::{Mmco, RefPicListMod, SliceHeader, SliceType};
use std::sync::Arc;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RefMark {
Unused,
Short,
Long,
}
/// Output-related metadata attached to a decoded frame.
#[derive(Clone, Debug)]
pub struct OutputMeta {
pub pts: i64,
pub key: bool,
pub crop: (u32, u32, u32, u32),
pub full_range: bool,
pub colour_primaries: u8,
pub transfer_characteristics: u8,
pub matrix_coefficients: u8,
pub sar: (u16, u16),
/// Decoded without deblocking (draft mode, non-reference picture).
pub draft: bool,
}
pub struct DpbEntry {
pub frame: FrameRef,
pub poc: i32,
pub frame_num: u32,
pub long_term_frame_idx: u32,
pub mark: RefMark,
pub needed_for_output: bool,
pub non_existing: bool,
pub meta: Arc<OutputMeta>,
}
pub struct Dpb {
pub entries: Vec<DpbEntry>,
/// None = "no long-term frame indices".
pub max_long_term_frame_idx: Option<u32>,
pub capacity: usize,
pub max_reorder: usize,
}
/// A picture leaving the DPB for output.
pub struct Output {
pub frame: FrameRef,
pub poc: i32,
pub meta: Arc<OutputMeta>,
}
impl Dpb {
pub fn new() -> Self {
Dpb { entries: Vec::new(), max_long_term_frame_idx: None, capacity: 16, max_reorder: 16 }
}
fn frame_num_wrap(e: &DpbEntry, cur_frame_num: u32, max_frame_num: u32) -> i32 {
if e.frame_num > cur_frame_num { e.frame_num as i32 - max_frame_num as i32 } else { e.frame_num as i32 }
}
fn num_refs(&self) -> usize {
self.entries.iter().filter(|e| e.mark != RefMark::Unused).count()
}
/// Remove entries not needed for output and not used for reference.
fn prune(&mut self) {
self.entries.retain(|e| e.needed_for_output || e.mark != RefMark::Unused);
}
/// Output the picture with the smallest POC (C.4.5.3 "bumping"). Returns false if none waits.
fn bump(&mut self, out: &mut Vec<Output>) -> bool {
let Some(i) = self.entries.iter().enumerate().filter(|(_, e)| e.needed_for_output).min_by_key(|(_, e)| e.poc).map(|(i, _)| i) else {
return false;
};
let e = &mut self.entries[i];
e.needed_for_output = false;
out.push(Output { frame: e.frame.clone(), poc: e.poc, meta: e.meta.clone() });
self.prune();
true
}
/// Output everything in POC order and clear non-reference entries.
pub fn flush(&mut self, out: &mut Vec<Output>) {
while self.bump(out) {}
self.prune();
}
/// IDR: mark all references unused; output (or drop) prior pictures.
pub fn idr(&mut self, no_output_of_prior_pics: bool, out: &mut Vec<Output>) {
for e in &mut self.entries {
e.mark = RefMark::Unused;
}
if no_output_of_prior_pics {
self.entries.clear();
} else {
self.flush(out);
}
self.max_long_term_frame_idx = None;
}
/// Sliding window marking (8.2.5.3).
fn sliding_window(&mut self, cur_frame_num: u32, max_frame_num: u32, max_num_ref_frames: usize) {
let limit = max_num_ref_frames.max(1);
while self.num_refs() >= limit {
let victim = self
.entries
.iter()
.enumerate()
.filter(|(_, e)| e.mark == RefMark::Short)
.min_by_key(|(_, e)| Self::frame_num_wrap(e, cur_frame_num, max_frame_num))
.map(|(i, _)| i);
match victim {
Some(i) => self.entries[i].mark = RefMark::Unused,
None => break,
}
}
self.prune();
}
/// Insert "non-existing" frames for a frame_num gap (8.2.5.2).
#[allow(clippy::too_many_arguments)]
pub fn fill_frame_num_gap(
&mut self,
prev_ref_frame_num: u32,
frame_num: u32,
max_frame_num: u32,
max_num_ref_frames: usize,
make_frame: &mut dyn FnMut(u32) -> (FrameRef, Arc<OutputMeta>),
poc_state: &mut dyn FnMut(u32),
) {
if max_frame_num == 0 {
return;
}
let mut unused = (prev_ref_frame_num + 1) % max_frame_num;
// Only the last `max_num_ref_frames` "non-existing" frames can survive the sliding window, and
// inserting more than that evicts every earlier short-term reference. Skip straight to that tail
// (keeping the POC state walk for every missing frame_num) so a hostile gap of up to 65535
// frames costs at most `max_num_ref_frames` frame allocations instead of one per missing frame.
let gap = frame_num.wrapping_add(max_frame_num).wrapping_sub(unused) % max_frame_num;
let keep = u32::try_from(max_num_ref_frames.max(1)).unwrap_or(u32::MAX);
let skip = gap.saturating_sub(keep);
if skip > 0 {
for e in self.entries.iter_mut().filter(|e| e.mark == RefMark::Short) {
e.mark = RefMark::Unused;
}
self.prune();
for _ in 0..skip {
poc_state(unused);
unused = (unused + 1) % max_frame_num;
}
}
let mut guard = 0;
while unused != frame_num && guard < max_frame_num {
self.sliding_window(unused, max_frame_num, max_num_ref_frames);
let (frame, meta) = make_frame(unused);
poc_state(unused);
self.entries.push(DpbEntry {
poc: frame.poc,
frame,
frame_num: unused,
long_term_frame_idx: 0,
mark: RefMark::Short,
needed_for_output: false,
non_existing: true,
meta,
});
unused = (unused + 1) % max_frame_num;
guard += 1;
}
}
/// Initial reference picture lists (8.2.4.2) as DPB entries.
fn initial_entry_lists<'a>(
sh: &SliceHeader,
cur_poc: i32,
short: &[&'a DpbEntry],
long: &[&'a DpbEntry],
pic_num: impl Fn(&DpbEntry) -> i32,
) -> [Vec<&'a DpbEntry>; 2] {
let mut entry_lists: [Vec<&'a DpbEntry>; 2] = [Vec::new(), Vec::new()];
if sh.slice_type != SliceType::B {
let mut s = short.to_vec();
s.sort_by_key(|e| std::cmp::Reverse(pic_num(e)));
entry_lists[0] = s.into_iter().chain(long.iter().copied()).collect();
} else {
let mut before: Vec<&'a DpbEntry> = short.iter().copied().filter(|e| e.poc < cur_poc).collect();
let mut after: Vec<&'a DpbEntry> = short.iter().copied().filter(|e| e.poc > cur_poc).collect();
before.sort_by_key(|e| std::cmp::Reverse(e.poc));
after.sort_by_key(|e| e.poc);
entry_lists[0] = before.iter().chain(after.iter()).chain(long.iter()).copied().collect();
entry_lists[1] = after.iter().chain(before.iter()).chain(long.iter()).copied().collect();
if entry_lists[1].len() > 1 && entry_lists[0].len() == entry_lists[1].len() {
let same = entry_lists[0].iter().zip(entry_lists[1].iter()).all(|(a, b)| std::ptr::eq(*a, *b));
if same {
entry_lists[1].swap(0, 1);
}
}
}
entry_lists
}
/// picNumLXNoWrap (8-34, 8-35) of a short-term modification from the predictor `pred`, and the
/// picNum it selects.
fn short_term_pic_nums(pred: i32, idc: u32, value: u32, curr_pic_num: i32, max_pic_num: i32) -> (i32, i32) {
let d = value as i32 + 1;
let no_wrap = if idc == 0 {
if pred - d < 0 { pred - d + max_pic_num } else { pred - d }
} else if pred + d >= max_pic_num {
pred + d - max_pic_num
} else {
pred + d
};
let pn = if no_wrap > curr_pic_num { no_wrap - max_pic_num } else { no_wrap };
(no_wrap, pn)
}
/// Puts `target` at `ref_idx` and shifts the rest of the list up, then drops the later entries
/// that repeat it (8.2.4.3.1, 8.2.4.3.2) and keeps the list `n` long.
fn insert_ref<'a>(list: &mut Vec<Option<&'a DpbEntry>>, ref_idx: usize, n: usize, target: &'a DpbEntry, is_long: bool) {
for c in (ref_idx + 1..=n).rev() {
list[c] = list[c - 1];
}
list[ref_idx] = Some(target);
let mut nidx = ref_idx + 1;
for c in ref_idx + 1..=n {
let keep = match list[c] {
None => true,
Some(e) => {
if is_long {
!(e.mark == RefMark::Long && e.long_term_frame_idx == target.long_term_frame_idx)
} else {
!(e.mark == RefMark::Short && std::ptr::eq(e, target))
}
}
};
if keep {
list[nidx] = list[c];
nidx += 1;
}
}
list.truncate(n);
}
/// Reference picture list modification (8.2.4.3) of one list; `n` is num_ref_idx_active.
fn modify_ref_list<'a>(
list: &mut Vec<Option<&'a DpbEntry>>,
mods: &[RefPicListMod],
n: usize,
cur_fn: u32,
max_frame_num: u32,
short: &[&'a DpbEntry],
long: &[&'a DpbEntry],
) -> Result<()> {
let max_pic_num = max_frame_num as i32;
let curr_pic_num = cur_fn as i32;
let mut pred = curr_pic_num;
let mut ref_idx = 0usize;
for m in mods {
ensure!(ref_idx < n, "too many ref_pic_list_modification entries");
list.push(None); // temporarily one longer
let (target, is_long): (Option<&DpbEntry>, bool) = match m.idc {
0 | 1 => {
let (no_wrap, pn) = Self::short_term_pic_nums(pred, m.idc, m.value, curr_pic_num, max_pic_num);
pred = no_wrap;
(short.iter().copied().find(|e| Self::frame_num_wrap(e, cur_fn, max_frame_num) == pn), false)
}
2 => (long.iter().copied().find(|e| e.long_term_frame_idx == m.value), true),
_ => continue,
};
let Some(target) = target else {
// Missing picture: keep the list unchanged (error concealment).
list.pop();
ref_idx += 1;
continue;
};
Self::insert_ref(list, ref_idx, n, target, is_long);
ref_idx += 1;
}
Ok(())
}
/// Initial reference picture lists (8.2.4.2) + modification (8.2.4.3) for a frame slice.
pub fn build_ref_lists(&self, sh: &SliceHeader, cur_poc: i32, max_frame_num: u32) -> Result<[Vec<RefPic>; 2]> {
let mut lists: [Vec<RefPic>; 2] = [Vec::new(), Vec::new()];
if sh.slice_type.is_intra() {
return Ok(lists);
}
let cur_fn = sh.frame_num;
let pic_num = |e: &DpbEntry| Self::frame_num_wrap(e, cur_fn, max_frame_num);
let short: Vec<&DpbEntry> = self.entries.iter().filter(|e| e.mark == RefMark::Short).collect();
let mut long: Vec<&DpbEntry> = self.entries.iter().filter(|e| e.mark == RefMark::Long).collect();
long.sort_by_key(|e| e.long_term_frame_idx);
let to_ref = |e: &&DpbEntry| RefPic { frame: e.frame.clone(), long_term: e.mark == RefMark::Long };
let entry_lists = Self::initial_entry_lists(sh, cur_poc, &short, &long, pic_num);
let nlists = if sh.slice_type == SliceType::B { 2 } else { 1 };
for l in 0..nlists {
let n = sh.num_ref_idx_active[l] as usize;
let mut list: Vec<Option<&DpbEntry>> = entry_lists[l].iter().copied().map(Some).collect();
list.truncate(n);
list.resize(n, None);
if !sh.ref_pic_list_mod[l].is_empty() {
Self::modify_ref_list(&mut list, &sh.ref_pic_list_mod[l], n, cur_fn, max_frame_num, &short, &long)?;
}
// Fill missing entries (should not happen in conforming streams) with the first valid entry.
let fallback = list.iter().flatten().next().copied().or_else(|| self.entries.last());
let out: Vec<RefPic> = list.iter().filter_map(|e| e.or(fallback)).map(|e| to_ref(&e)).collect();
lists[l] = out;
}
Ok(lists)
}
/// Marks as unused every entry in state `mark` that `hit` selects.
fn unmark_where(&mut self, mark: RefMark, hit: impl Fn(&DpbEntry) -> bool) {
for e in &mut self.entries {
if e.mark == mark && hit(e) {
e.mark = RefMark::Unused;
}
}
}
/// Makes every short-term entry that `hit` selects a long-term one with index `idx`.
fn mark_long_where(&mut self, hit: impl Fn(&DpbEntry) -> bool, idx: u32) {
for e in &mut self.entries {
if e.mark == RefMark::Short && hit(e) {
e.mark = RefMark::Long;
e.long_term_frame_idx = idx;
}
}
}
/// One memory management control operation; returns true for MMCO 5.
fn apply_mmco(&mut self, m: &Mmco, cur_frame_num: u32, max_frame_num: u32, cur_long: &mut Option<u32>) -> bool {
let mut had5 = false;
let curr_pic_num = cur_frame_num as i32;
match m.op {
1 => {
let pn = curr_pic_num - (m.difference_of_pic_nums_minus1 as i32 + 1);
self.unmark_where(RefMark::Short, |e| Self::frame_num_wrap(e, cur_frame_num, max_frame_num) == pn);
}
2 => self.unmark_where(RefMark::Long, |e| e.long_term_frame_idx == m.long_term_pic_num),
3 => {
let pn = curr_pic_num - (m.difference_of_pic_nums_minus1 as i32 + 1);
self.unmark_where(RefMark::Long, |e| e.long_term_frame_idx == m.long_term_frame_idx);
self.mark_long_where(|e| Self::frame_num_wrap(e, cur_frame_num, max_frame_num) == pn, m.long_term_frame_idx);
}
4 => {
let max = m.max_long_term_frame_idx_plus1;
self.unmark_where(RefMark::Long, |e| max == 0 || e.long_term_frame_idx > max - 1);
self.max_long_term_frame_idx = if max == 0 { None } else { Some(max - 1) };
}
5 => {
for e in &mut self.entries {
e.mark = RefMark::Unused;
}
self.max_long_term_frame_idx = None;
had5 = true;
}
6 => {
// Only an index within the allowed range may be assigned (8.2.5.4.6).
let allowed = self.max_long_term_frame_idx.is_some_and(|max| m.long_term_frame_idx <= max);
if allowed {
self.unmark_where(RefMark::Long, |e| e.long_term_frame_idx == m.long_term_frame_idx);
*cur_long = Some(m.long_term_frame_idx);
}
}
_ => {}
}
had5
}
/// Adaptive memory control (8.2.5.4). Returns true if MMCO 5 was executed.
fn apply_mmcos(&mut self, mmcos: &[Mmco], cur_frame_num: u32, max_frame_num: u32, cur_long: &mut Option<u32>) -> bool {
let mut had5 = false;
for m in mmcos {
had5 |= self.apply_mmco(m, cur_frame_num, max_frame_num, cur_long);
}
self.prune();
had5
}
/// Reference marking of the current picture (8.2.5); returns (mark, long-term idx, MMCO 5 executed).
fn mark_current(&mut self, sh: &SliceHeader, max_frame_num: u32, max_num_ref_frames: usize) -> (RefMark, u32, bool) {
let mut mark = RefMark::Unused;
let mut long_idx = 0;
let mut had5 = false;
if sh.nal_ref_idc != 0 {
if sh.idr {
if sh.long_term_reference {
mark = RefMark::Long;
self.max_long_term_frame_idx = Some(0);
} else {
mark = RefMark::Short;
self.max_long_term_frame_idx = None;
}
} else {
let mut cur_long = None;
if sh.adaptive_ref_pic_marking {
had5 = self.apply_mmcos(&sh.mmcos, sh.frame_num, max_frame_num, &mut cur_long);
} else {
self.sliding_window(sh.frame_num, max_frame_num, max_num_ref_frames);
}
if let Some(i) = cur_long {
mark = RefMark::Long;
long_idx = i;
} else {
mark = RefMark::Short;
// make room if MMCOs left the DPB over-full (non-conforming streams)
if self.num_refs() >= max_num_ref_frames.max(1) && sh.adaptive_ref_pic_marking {
self.sliding_window(sh.frame_num, max_frame_num, max_num_ref_frames);
}
}
}
}
(mark, long_idx, had5)
}
/// Bumping until there is space (C.4.5.1 / C.4.5.2). Returns true when the (non-reference) current
/// picture has the smallest POC and is to be output directly instead of being stored.
fn bump_for_space(&mut self, mark: RefMark, poc: i32, out: &mut Vec<Output>) -> bool {
if mark == RefMark::Unused {
loop {
let waiting = self.entries.len();
if waiting < self.capacity {
break;
}
// non-reference picture with the smallest POC is output directly
let min_poc = self.entries.iter().filter(|e| e.needed_for_output).map(|e| e.poc).min();
if min_poc.is_none_or(|m| poc < m) {
return true;
}
if !self.bump(out) {
break;
}
}
} else {
while self.entries.len() >= self.capacity {
if !self.bump(out) {
if let Some(pos) = self.entries.iter().position(|e| !e.needed_for_output) {
self.entries.remove(pos);
} else {
self.entries.remove(0);
}
break;
}
}
}
false
}
/// Reference marking for the just-decoded picture and insertion into the DPB with output bumping.
/// Returns pictures ready for output.
#[allow(clippy::too_many_arguments)]
pub fn store_picture(
&mut self,
sh: &SliceHeader,
frame: FrameRef,
poc: i32,
max_frame_num: u32,
max_num_ref_frames: usize,
meta: Arc<OutputMeta>,
out: &mut Vec<Output>,
) {
let (mark, long_idx, had5) = self.mark_current(sh, max_frame_num, max_num_ref_frames);
if had5 {
// All earlier pictures are output before the current one (C.4.4).
self.flush(out);
}
let frame_num = if had5 { 0 } else { sh.frame_num };
if self.bump_for_space(mark, poc, out) {
out.push(Output { frame, poc, meta });
return;
}
self.entries.push(DpbEntry {
frame,
poc,
frame_num,
long_term_frame_idx: long_idx,
mark,
needed_for_output: true,
non_existing: false,
meta,
});
// Low-delay output: honour max_num_reorder_frames.
while self.entries.iter().filter(|e| e.needed_for_output).count() > self.max_reorder {
if !self.bump(out) {
break;
}
}
}
}
impl Default for Dpb {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::picture::Frame;
fn meta() -> Arc<OutputMeta> {
Arc::new(OutputMeta {
pts: 0,
key: false,
crop: (0, 0, 0, 0),
full_range: false,
colour_primaries: 2,
transfer_characteristics: 2,
matrix_coefficients: 2,
sar: (1, 1),
draft: false,
})
}
fn short(dpb: &mut Dpb, id: u32, frame_num: u32) {
dpb.entries.push(DpbEntry {
frame: Arc::new(Frame::new(id, 0, 1, 1)),
poc: 0,
frame_num,
long_term_frame_idx: 0,
mark: RefMark::Short,
needed_for_output: false,
non_existing: false,
meta: meta(),
});
}
/// Fill a gap and return (frames made, POC walk, reference frame_nums left in the DPB).
fn fill(dpb: &mut Dpb, prev: u32, cur: u32, max: u32, refs: usize) -> (u32, Vec<u32>, Vec<(u32, bool)>) {
let mut made = 0u32;
let mut walk = Vec::new();
let mut make = |_f: u32| {
made += 1;
(Arc::new(Frame::new(1000 + made, 0, 1, 1)), meta())
};
let mut poc = |f: u32| walk.push(f);
dpb.fill_frame_num_gap(prev, cur, max, refs, &mut make, &mut poc);
let left = dpb.entries.iter().filter(|e| e.mark != RefMark::Unused).map(|e| (e.frame_num, e.non_existing)).collect();
(made, walk, left)
}
#[test]
fn huge_gap_makes_only_the_frames_that_stay_referenced() {
let mut dpb = Dpb::new();
short(&mut dpb, 1, 0);
short(&mut dpb, 2, 1);
let (made, walk, left) = fill(&mut dpb, 1, 65535, 65536, 4);
assert_eq!(made, 4);
// The POC state still walks every missing frame_num, in order.
assert_eq!(walk.len(), 65533);
assert_eq!(walk.first(), Some(&2));
assert_eq!(walk.last(), Some(&65534));
assert_eq!(left, vec![(65531, true), (65532, true), (65533, true), (65534, true)]);
}
#[test]
fn huge_gap_across_wraparound_matches_the_frame_by_frame_result() {
let mut dpb = Dpb::new();
short(&mut dpb, 1, 65000);
let (made, walk, left) = fill(&mut dpb, 65000, 3, 65536, 2);
assert_eq!(made, 2);
assert_eq!(walk.len(), 538);
assert_eq!(walk.first(), Some(&65001));
assert_eq!(walk.last(), Some(&2));
assert_eq!(left, vec![(1, true), (2, true)]);
}
#[test]
fn small_gap_keeps_earlier_references() {
let mut dpb = Dpb::new();
short(&mut dpb, 1, 0);
short(&mut dpb, 2, 1);
let (made, walk, left) = fill(&mut dpb, 1, 4, 16, 4);
assert_eq!(made, 2);
assert_eq!(walk, vec![2, 3]);
assert_eq!(left, vec![(0, false), (1, false), (2, true), (3, true)]);
}
#[test]
fn long_term_references_survive_a_huge_gap() {
let mut dpb = Dpb::new();
short(&mut dpb, 1, 0);
if let Some(e) = dpb.entries.last_mut() {
e.mark = RefMark::Long;
}
let (made, _, left) = fill(&mut dpb, 0, 1000, 1024, 3);
assert!(made <= 3);
assert_eq!(left, vec![(0, false), (998, true), (999, true)]);
}
}
-38
View File
@@ -1,38 +0,0 @@
use deckcraft_bitstream::BitError;
/// Errors produced by the decoder.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum Error {
/// The bitstream ended early or contained an invalid Exp-Golomb code.
#[error("bitstream error: {0}")]
Bitstream(#[from] BitError),
/// A syntax element had a value outside its legal range.
#[error("invalid bitstream: {0}")]
Invalid(String),
/// A valid feature this decoder does not implement (yet).
#[error("unsupported: {0}")]
Unsupported(String),
/// A slice referenced a parameter set that has not been received.
#[error("missing parameter set: {0}")]
MissingParameterSet(String),
}
pub type Result<T> = std::result::Result<T, Error>;
pub(crate) fn invalid<T>(msg: impl Into<String>) -> Result<T> {
Err(Error::Invalid(msg.into()))
}
pub(crate) fn unsupported<T>(msg: impl Into<String>) -> Result<T> {
Err(Error::Unsupported(msg.into()))
}
/// `ensure!(cond, "message")` returns `Error::Invalid` when `cond` is false.
macro_rules! ensure {
($cond:expr, $($arg:tt)+) => {
if !$cond {
return Err($crate::error::Error::Invalid(format!($($arg)+)));
}
};
}
pub(crate) use ensure;
-513
View File
@@ -1,513 +0,0 @@
//! Inter prediction sample interpolation (8.4.2.2) and weighted sample prediction (8.4.2.3).
/// A reference plane (used by the plane-based helpers in tests).
#[cfg(test)]
#[derive(Clone, Copy)]
pub struct PlaneRef<'a> {
pub data: &'a [u8],
pub width: usize,
pub height: usize,
pub stride: usize,
}
const WIN: usize = 16 + 5;
/// Copy the (bw+5)x(bh+5) window whose top-left is (x-2, y-2) into `win` (stride WIN), clamping
/// coordinates to the picture.
#[cfg(test)]
#[inline]
fn fetch_window(p: PlaneRef, x: i32, y: i32, bw: usize, bh: usize, win: &mut [u8; WIN * WIN]) {
let x0 = x - 2;
let y0 = y - 2;
let ww = bw + 5;
let wh = bh + 5;
let maxx = p.width as i32 - 1;
let maxy = p.height as i32 - 1;
if x0 >= 0 && y0 >= 0 && x0 + ww as i32 - 1 <= maxx && y0 + wh as i32 - 1 <= maxy {
for r in 0..wh {
let src = (y0 as usize + r) * p.stride + x0 as usize;
win[r * WIN..r * WIN + ww].copy_from_slice(&p.data[src..src + ww]);
}
} else {
for r in 0..wh {
let yy = (y0 + r as i32).clamp(0, maxy) as usize;
let row = &p.data[yy * p.stride..yy * p.stride + p.width];
for c in 0..ww {
let xx = (x0 + c as i32).clamp(0, maxx) as usize;
win[r * WIN + c] = row[xx];
}
}
}
}
#[inline(always)]
fn tap6(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) -> i32 {
a - 5 * b + 20 * c + 20 * d - 5 * e + f
}
#[inline(always)]
fn clip1(v: i32) -> u8 {
v.clamp(0, 255) as u8
}
/// Copy `n` bytes with a fixed-size move for the common block widths.
#[inline(always)]
fn copy_n(dst: &mut [u8], src: &[u8], n: usize) {
fn fixed<const N: usize>(dst: &mut [u8], src: &[u8]) {
if let (Some(d), Some(s)) = (dst.first_chunk_mut::<N>(), src.first_chunk::<N>()) {
*d = *s;
}
}
match n {
16 => fixed::<16>(dst, src),
8 => fixed::<8>(dst, src),
4 => fixed::<4>(dst, src),
2 => fixed::<2>(dst, src),
_ => dst[..n].copy_from_slice(&src[..n]),
}
}
#[inline(always)]
fn avg(a: u8, b: u8) -> u8 {
((a as u16 + b as u16 + 1) >> 1) as u8
}
/// Horizontal 6-tap (unclipped) at every position of `row` for `n` outputs: out[c] uses row[c..c + 6].
#[inline(always)]
fn htap_row(row: &[u8], out: &mut [i32], n: usize) {
let row = &row[..n + 5];
for (o, w) in out[..n].iter_mut().zip(row.windows(6)) {
*o = tap6(w[0] as i32, w[1] as i32, w[2] as i32, w[3] as i32, w[4] as i32, w[5] as i32);
}
}
/// Vertical 6-tap (unclipped) over six rows at columns 0..n.
#[inline(always)]
fn vtap_row(rows: [&[u8]; 6], out: &mut [i32], n: usize) {
let [a, b, c, d, e, f] = rows.map(|r| &r[..n]);
for i in 0..n {
out[i] = tap6(a[i] as i32, b[i] as i32, c[i] as i32, d[i] as i32, e[i] as i32, f[i] as i32);
}
}
/// Luma sample interpolation for a bw x bh block (8.4.2.2.1). (x, y) is the integer sample position
/// (block position + (mv >> 2)); (fx, fy) the quarter-sample fraction. Output stride is `os`.
#[cfg(test)]
#[allow(clippy::too_many_arguments)]
pub fn mc_luma(p: PlaneRef, x: i32, y: i32, fx: u32, fy: u32, bw: usize, bh: usize, out: &mut [u8], os: usize) {
let mut win = [0u8; WIN * WIN];
let (x0, y0) = (x - 2, y - 2);
let inside = x0 >= 0 && y0 >= 0 && x0 as usize + bw + 5 <= p.width && y0 as usize + bh + 5 <= p.height;
let (src, ss): (&[u8], usize) = if inside {
(&p.data[y0 as usize * p.stride + x0 as usize..], p.stride)
} else {
fetch_window(p, x, y, bw, bh, &mut win);
(&win[..], WIN)
};
mc_luma_win(src, ss, fx, fy, bw, bh, out, os);
}
/// Luma interpolation from a prepared window: `src[0]` is the sample at (x - 2, y - 2) and `ss` the
/// window stride; the window covers (bw + 5) x (bh + 5) samples.
#[allow(clippy::too_many_arguments)]
pub fn mc_luma_win(src: &[u8], ss: usize, fx: u32, fy: u32, bw: usize, bh: usize, out: &mut [u8], os: usize) {
// window row r (0..bh+5) starts at src[r * ss]; sample G of block (r, c) is at window (r + 2, c + 2)
let wrow = |r: usize| &src[r * ss..r * ss + bw + 5];
let mut t = [0i32; 16];
match (fx, fy) {
(0, 0) => {
for r in 0..bh {
copy_n(&mut out[r * os..], &wrow(r + 2)[2..], bw);
}
}
(_, 0) => {
for r in 0..bh {
let row = wrow(r + 2);
htap_row(row, &mut t, bw);
let o = &mut out[r * os..r * os + bw];
match fx {
1 => {
for c in 0..bw {
o[c] = avg(row[c + 2], clip1((t[c] + 16) >> 5));
}
}
2 => {
for c in 0..bw {
o[c] = clip1((t[c] + 16) >> 5);
}
}
_ => {
for c in 0..bw {
o[c] = avg(row[c + 3], clip1((t[c] + 16) >> 5));
}
}
}
}
}
(0, _) => {
for r in 0..bh {
let rows = [wrow(r), wrow(r + 1), wrow(r + 2), wrow(r + 3), wrow(r + 4), wrow(r + 5)].map(|x| &x[2..]);
vtap_row(rows, &mut t, bw);
let g = if fy == 1 { rows[2] } else { rows[3] };
let o = &mut out[r * os..r * os + bw];
if fy == 2 {
for c in 0..bw {
o[c] = clip1((t[c] + 16) >> 5);
}
} else {
for c in 0..bw {
o[c] = avg(g[c], clip1((t[c] + 16) >> 5));
}
}
}
}
(2, _) | (_, 2) => {
// unclipped horizontal half samples b1 for window rows 0..bh+5
let mut b1 = [[0i32; 16]; WIN];
for (r, bt) in b1.iter_mut().enumerate().take(bh + 5) {
htap_row(wrow(r), bt, bw);
}
let mut v = [0i32; 16];
for r in 0..bh {
let o = &mut out[r * os..r * os + bw];
let (b0, b1r, b2, b3, b4, b5) = (&b1[r], &b1[r + 1], &b1[r + 2], &b1[r + 3], &b1[r + 4], &b1[r + 5]);
for c in 0..bw {
t[c] = tap6(b0[c], b1r[c], b2[c], b3[c], b4[c], b5[c]);
}
match (fx, fy) {
(2, 2) => {
for c in 0..bw {
o[c] = clip1((t[c] + 512) >> 10);
}
}
(2, 1) | (2, 3) => {
let bh_ = if fy == 1 { b2 } else { b3 };
for c in 0..bw {
o[c] = avg(clip1((bh_[c] + 16) >> 5), clip1((t[c] + 512) >> 10));
}
}
_ => {
let off = if fx == 1 { 2 } else { 3 };
let rows = [wrow(r), wrow(r + 1), wrow(r + 2), wrow(r + 3), wrow(r + 4), wrow(r + 5)].map(|x| &x[off..]);
vtap_row(rows, &mut v, bw);
for c in 0..bw {
o[c] = avg(clip1((v[c] + 16) >> 5), clip1((t[c] + 512) >> 10));
}
}
}
}
}
_ => {
// e, g, p, r: average of a horizontal half sample (row r or r + 1) and a vertical one (col c or c + 1)
let hoff = if fy == 1 { 2 } else { 3 };
let voff = if fx == 1 { 2 } else { 3 };
let mut v = [0i32; 16];
for r in 0..bh {
htap_row(wrow(r + hoff), &mut t, bw);
let rows = [wrow(r), wrow(r + 1), wrow(r + 2), wrow(r + 3), wrow(r + 4), wrow(r + 5)].map(|x| &x[voff..]);
vtap_row(rows, &mut v, bw);
let o = &mut out[r * os..r * os + bw];
for c in 0..bw {
o[c] = avg(clip1((t[c] + 16) >> 5), clip1((v[c] + 16) >> 5));
}
}
}
}
}
/// Chroma sample interpolation (8.4.2.2.2) for a bw x bh block at integer position (x, y) with
/// eighth-sample fraction (fx, fy).
#[cfg(test)]
#[allow(clippy::too_many_arguments)]
pub fn mc_chroma(p: PlaneRef, x: i32, y: i32, fx: u32, fy: u32, bw: usize, bh: usize, out: &mut [u8], os: usize) {
let maxx = p.width as i32 - 1;
let maxy = p.height as i32 - 1;
let inside = x >= 0 && y >= 0 && x + bw as i32 <= maxx && y + bh as i32 <= maxy;
// (bw+1) x (bh+1) source window; copied with edge clamping when it crosses the picture border
let mut win = [0u8; 9 * 9];
let (src, ss): (&[u8], usize) = if inside {
(&p.data[y as usize * p.stride + x as usize..], p.stride)
} else {
for r in 0..=bh {
let yy = (y + r as i32).clamp(0, maxy) as usize;
for c in 0..=bw {
win[r * 9 + c] = p.data[yy * p.stride + (x + c as i32).clamp(0, maxx) as usize];
}
}
(&win[..], 9)
};
mc_chroma_win(src, ss, fx, fy, bw, bh, out, os);
}
/// Chroma interpolation from a prepared (bw + 1) x (bh + 1) window whose first sample is the integer
/// position of the block.
#[allow(clippy::too_many_arguments)]
pub fn mc_chroma_win(src: &[u8], ss: usize, fx: u32, fy: u32, bw: usize, bh: usize, out: &mut [u8], os: usize) {
let (fx, fy) = (fx as u16, fy as u16);
let (w00, w10, w01, w11) = ((8 - fx) * (8 - fy), fx * (8 - fy), (8 - fx) * fy, fx * fy);
if fx == 0 && fy == 0 {
for r in 0..bh {
copy_n(&mut out[r * os..], &src[r * ss..], bw);
}
return;
}
for r in 0..bh {
let r0 = &src[r * ss..r * ss + bw + 1];
let r1 = &src[(r + 1) * ss..(r + 1) * ss + bw + 1];
let o = &mut out[r * os..r * os + bw];
for (c, o) in o.iter_mut().enumerate() {
let v = w00 * r0[c] as u16 + w10 * r0[c + 1] as u16 + w01 * r1[c] as u16 + w11 * r1[c + 1] as u16;
*o = ((v + 32) >> 6) as u8;
}
}
}
/// Weights for one prediction direction or pair.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Weight {
/// Default: copy (single list) or rounded average (bi).
Default,
/// Explicit/implicit weighting: log2 denominator, weights and offsets for L0 and L1.
Weighted { log_wd: i32, w0: i32, w1: i32, o0: i32, o1: i32 },
}
/// Combine prediction(s) into `dst` (stride `ds`). `p0`/`p1` have stride `ps`.
#[allow(clippy::too_many_arguments)]
pub fn weighted_store(dst: &mut [u8], ds: usize, p0: Option<&[u8]>, p1: Option<&[u8]>, ps: usize, bw: usize, bh: usize, w: Weight) {
match (p0, p1, w) {
(Some(a), Some(b), Weight::Default) => {
for r in 0..bh {
let d = &mut dst[r * ds..r * ds + bw];
let ra = &a[r * ps..r * ps + bw];
let rb = &b[r * ps..r * ps + bw];
for ((d, &x), &y) in d.iter_mut().zip(ra).zip(rb) {
*d = ((x as u32 + y as u32 + 1) >> 1) as u8;
}
}
}
(Some(a), Some(b), Weight::Weighted { log_wd, w0, w1, o0, o1 }) => {
let round = 1 << log_wd;
let off = (o0 + o1 + 1) >> 1;
let sh = log_wd + 1;
for r in 0..bh {
let d = &mut dst[r * ds..r * ds + bw];
let ra = &a[r * ps..r * ps + bw];
let rb = &b[r * ps..r * ps + bw];
for ((d, &x), &y) in d.iter_mut().zip(ra).zip(rb) {
*d = clip1(((x as i32 * w0 + y as i32 * w1 + round) >> sh) + off);
}
}
}
(Some(a), None, w) | (None, Some(a), w) => {
let (wt, o, log_wd) = match (w, p0.is_some()) {
(Weight::Default, _) => {
for r in 0..bh {
copy_n(&mut dst[r * ds..], &a[r * ps..], bw);
}
return;
}
(Weight::Weighted { w0, o0, log_wd, .. }, true) => (w0, o0, log_wd),
(Weight::Weighted { w1, o1, log_wd, .. }, false) => (w1, o1, log_wd),
};
let round = if log_wd >= 1 { 1 << (log_wd - 1) } else { 0 };
for r in 0..bh {
let d = &mut dst[r * ds..r * ds + bw];
let ra = &a[r * ps..r * ps + bw];
for (d, &x) in d.iter_mut().zip(ra) {
*d = clip1(((x as i32 * wt + round) >> log_wd) + o);
}
}
}
(None, None, _) => {}
}
}
#[cfg(test)]
mod reference {
use super::*;
/// Luma sample interpolation for a bw x bh block. (x, y) is the integer sample position
/// (block position + (mv >> 2)); (fx, fy) the quarter-sample fraction. Output stride is `os`.
#[allow(clippy::too_many_arguments)]
pub fn mc_luma_ref(p: PlaneRef, x: i32, y: i32, fx: u32, fy: u32, bw: usize, bh: usize, out: &mut [u8], os: usize) {
let mut win = [0u8; WIN * WIN];
fetch_window(p, x, y, bw, bh, &mut win);
// sample G at (r, c) in block coordinates is win[(r + 2) * WIN + c + 2]
let g = |r: usize, c: usize| win[(r + 2) * WIN + c + 2] as i32;
// unclipped horizontal half-sample b1 at block row r (-2..bh+3 via offset) and column c
let b1 = |r: isize, c: usize| {
let base = ((r + 2) as usize) * WIN + c;
tap6(win[base] as i32, win[base + 1] as i32, win[base + 2] as i32, win[base + 3] as i32, win[base + 4] as i32, win[base + 5] as i32)
};
let h1 = |r: usize, c: isize| {
let col = (c + 2) as usize;
tap6(
win[r * WIN + col] as i32,
win[(r + 1) * WIN + col] as i32,
win[(r + 2) * WIN + col] as i32,
win[(r + 3) * WIN + col] as i32,
win[(r + 4) * WIN + col] as i32,
win[(r + 5) * WIN + col] as i32,
)
};
match (fx, fy) {
(0, 0) => {
for r in 0..bh {
out[r * os..r * os + bw].copy_from_slice(&win[(r + 2) * WIN + 2..(r + 2) * WIN + 2 + bw]);
}
}
(_, 0) => {
// a, b, c
for r in 0..bh {
for c in 0..bw {
let b = clip1((b1(r as isize, c) + 16) >> 5) as i32;
out[r * os + c] = match fx {
1 => ((g(r, c) + b + 1) >> 1) as u8,
2 => b as u8,
_ => ((g(r, c + 1) + b + 1) >> 1) as u8,
};
}
}
}
(0, _) => {
// d, h, n
for r in 0..bh {
for c in 0..bw {
let h = clip1((h1(r, c as isize) + 16) >> 5) as i32;
out[r * os + c] = match fy {
1 => ((g(r, c) + h + 1) >> 1) as u8,
2 => h as u8,
_ => ((g(r + 1, c) + h + 1) >> 1) as u8,
};
}
}
}
(2, _) | (_, 2) => {
// j-based: j needs b1 for rows -2..bh+3
let mut bcol = [0i32; WIN * WIN];
for r in 0..bh + 5 {
for c in 0..bw {
bcol[r * WIN + c] = b1(r as isize - 2, c);
}
}
for r in 0..bh {
for c in 0..bw {
let j1 = tap6(
bcol[r * WIN + c],
bcol[(r + 1) * WIN + c],
bcol[(r + 2) * WIN + c],
bcol[(r + 3) * WIN + c],
bcol[(r + 4) * WIN + c],
bcol[(r + 5) * WIN + c],
);
let j = clip1((j1 + 512) >> 10) as i32;
let v = match (fx, fy) {
(2, 2) => j,
(2, 1) => (clip1((bcol[(r + 2) * WIN + c] + 16) >> 5) as i32 + j + 1) >> 1, // f
(2, 3) => (clip1((bcol[(r + 3) * WIN + c] + 16) >> 5) as i32 + j + 1) >> 1, // q
(1, 2) => (clip1((h1(r, c as isize) + 16) >> 5) as i32 + j + 1) >> 1, // i
_ => (clip1((h1(r, c as isize + 1) + 16) >> 5) as i32 + j + 1) >> 1, // k
};
out[r * os + c] = v as u8;
}
}
}
_ => {
// e, g, p, r: average of a horizontal half-sample (b or s) and a vertical one (h or m)
for r in 0..bh {
for c in 0..bw {
let hr = if fy == 1 { r as isize } else { r as isize + 1 };
let vc = if fx == 1 { c as isize } else { c as isize + 1 };
let bh_ = clip1((b1(hr, c) + 16) >> 5) as i32;
let vv = clip1((h1(r, vc) + 16) >> 5) as i32;
out[r * os + c] = ((bh_ + vv + 1) >> 1) as u8;
}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn plane(w: usize, h: usize, f: impl Fn(usize, usize) -> u8) -> Vec<u8> {
(0..w * h).map(|i| f(i % w, i / w)).collect()
}
#[test]
fn full_sample_copy_and_clamp() {
let d = plane(16, 16, |x, y| (x + 16 * y) as u8);
let p = PlaneRef { data: &d, width: 16, height: 16, stride: 16 };
let mut out = [0u8; 16];
mc_luma(p, 2, 3, 0, 0, 4, 4, &mut out, 4);
assert_eq!(&out[..4], &[50, 51, 52, 53]);
// clamped: far outside to the top-left gives sample (0,0)
mc_luma(p, -40, -40, 0, 0, 4, 4, &mut out, 4);
assert!(out.iter().all(|&v| v == 0));
}
#[test]
fn half_sample_constant_is_constant() {
let d = vec![100u8; 32 * 32];
let p = PlaneRef { data: &d, width: 32, height: 32, stride: 32 };
for fx in 0..4 {
for fy in 0..4 {
let mut out = [0u8; 64];
mc_luma(p, 8, 8, fx, fy, 8, 8, &mut out, 8);
assert!(out.iter().all(|&v| v == 100), "{fx},{fy}");
mc_chroma(p, 8, 8, fx * 2, fy * 2, 8, 8, &mut out, 8);
assert!(out.iter().all(|&v| v == 100));
}
}
}
#[test]
fn half_sample_horizontal_ramp() {
// linear ramp: 6-tap filter of a linear function is exact: b = (G + H) / 2 (rounded)
let d = plane(32, 32, |x, _| (x * 4) as u8);
let p = PlaneRef { data: &d, width: 32, height: 32, stride: 32 };
let mut out = [0u8; 16];
mc_luma(p, 8, 8, 2, 0, 4, 4, &mut out, 4);
assert_eq!(&out[..4], &[34, 38, 42, 46]);
mc_luma(p, 8, 8, 1, 0, 4, 4, &mut out, 4);
assert_eq!(&out[..4], &[33, 37, 41, 45]);
}
#[test]
fn fast_luma_matches_reference() {
let mut seed = 7u32;
let d: Vec<u8> = (0..40 * 36)
.map(|_| {
seed = seed.wrapping_mul(1664525).wrapping_add(1013904223);
(seed >> 24) as u8
})
.collect();
let p = PlaneRef { data: &d, width: 40, height: 36, stride: 40 };
for &(bw, bh) in &[(16, 16), (16, 8), (8, 16), (8, 8), (8, 4), (4, 8), (4, 4)] {
for fx in 0..4 {
for fy in 0..4 {
for &(x, y) in &[(10, 9), (-3, -5), (30, 30), (0, 0), (2, 2), (38, 1)] {
let mut a = [0u8; 256];
let mut b = [0u8; 256];
mc_luma(p, x, y, fx, fy, bw, bh, &mut a, 16);
reference::mc_luma_ref(p, x, y, fx, fy, bw, bh, &mut b, 16);
assert_eq!(a, b, "{bw}x{bh} frac {fx},{fy} at {x},{y}");
}
}
}
}
}
#[test]
fn weighting() {
let a = [100u8; 4];
let b = [50u8; 4];
let mut d = [0u8; 4];
weighted_store(&mut d, 4, Some(&a), Some(&b), 4, 4, 1, Weight::Default);
assert_eq!(d, [75; 4]);
weighted_store(&mut d, 4, Some(&a), None, 4, 4, 1, Weight::Weighted { log_wd: 5, w0: 16, w1: 0, o0: 3, o1: 0 });
assert_eq!(d, [53; 4]);
}
}
-438
View File
@@ -1,438 +0,0 @@
//! Intra prediction (8.3): Intra_4x4, Intra_8x8 (with reference filtering), Intra_16x16 and chroma.
/// Neighbouring sample availability for one block.
#[derive(Clone, Copy, Debug, Default)]
pub struct Avail {
pub left: bool,
pub top: bool,
pub top_left: bool,
pub top_right: bool,
}
/// Neighbouring samples of an NxN block: p[x,-1] for x = 0..2N-1, p[-1,y] for y = 0..N-1, p[-1,-1].
#[derive(Clone, Copy)]
struct Edge {
top: [i32; 16],
left: [i32; 8],
tl: i32,
}
impl Edge {
/// p[x, -1] for x >= -1
#[inline(always)]
fn t(&self, x: i32) -> i32 {
if x < 0 { self.tl } else { self.top[x as usize] }
}
/// p[-1, y] for y >= -1
#[inline(always)]
fn l(&self, y: i32) -> i32 {
if y < 0 { self.tl } else { self.left[y as usize] }
}
}
fn load_edge(plane: &[u8], stride: usize, x: usize, y: usize, n: usize, a: Avail) -> Edge {
let mut e = Edge { top: [128; 16], left: [128; 8], tl: 128 };
if a.top {
let row = &plane[(y - 1) * stride + x..];
for i in 0..n {
e.top[i] = row[i] as i32;
}
if a.top_right {
for i in n..2 * n {
e.top[i] = row[i] as i32;
}
} else {
let v = e.top[n - 1];
for t in &mut e.top[n..2 * n] {
*t = v;
}
}
}
if a.left {
for i in 0..n {
e.left[i] = plane[(y + i) * stride + x - 1] as i32;
}
}
if a.top_left {
e.tl = plane[(y - 1) * stride + x - 1] as i32;
}
e
}
/// Directional / DC prediction shared by Intra_4x4 and Intra_8x8 (8.3.1.2.x, 8.3.2.2.x).
fn pred_nxn(mode: u8, e: &Edge, n: i32, a: Avail, out: &mut [u8], stride: usize) {
let mut fill = |value: &dyn Fn(i32, i32) -> i32| {
for y in 0..n {
for x in 0..n {
out[y as usize * stride + x as usize] = value(x, y) as u8;
}
}
};
match mode {
0 => fill(&|x, _| e.t(x)),
1 => fill(&|_, y| e.l(y)),
2 => {
let dc = dc_value(e, n, a);
fill(&|_, _| dc);
}
3 => fill(&|x, y| diagonal_down_left(e, n, x, y)),
4 => fill(&|x, y| diagonal_down_right(e, x, y)),
5 => fill(&|x, y| vertical_right(e, x, y)),
6 => fill(&|x, y| horizontal_down(e, x, y)),
7 => fill(&|x, y| vertical_left(e, x, y)),
_ => fill(&|x, y| horizontal_up(e, n, x, y)),
}
}
fn dc_value(e: &Edge, n: i32, a: Avail) -> i32 {
let shift = if n == 4 { 2 } else { 3 };
let st: i32 = (0..n).map(|x| e.t(x)).sum();
let sl: i32 = (0..n).map(|y| e.l(y)).sum();
match (a.top, a.left) {
(true, true) => (st + sl + n) >> (shift + 1),
(false, true) => (sl + (n >> 1)) >> shift,
(true, false) => (st + (n >> 1)) >> shift,
_ => 128,
}
}
fn diagonal_down_left(e: &Edge, n: i32, x: i32, y: i32) -> i32 {
if x == n - 1 && y == n - 1 {
(e.t(2 * n - 2) + 3 * e.t(2 * n - 1) + 2) >> 2
} else {
(e.t(x + y) + 2 * e.t(x + y + 1) + e.t(x + y + 2) + 2) >> 2
}
}
fn diagonal_down_right(e: &Edge, x: i32, y: i32) -> i32 {
if x > y {
(e.t(x - y - 2) + 2 * e.t(x - y - 1) + e.t(x - y) + 2) >> 2
} else if x < y {
(e.l(y - x - 2) + 2 * e.l(y - x - 1) + e.l(y - x) + 2) >> 2
} else {
(e.t(0) + 2 * e.tl + e.l(0) + 2) >> 2
}
}
fn vertical_right(e: &Edge, x: i32, y: i32) -> i32 {
let z = 2 * x - y;
if z >= 0 && z % 2 == 0 {
(e.t(x - (y >> 1) - 1) + e.t(x - (y >> 1)) + 1) >> 1
} else if z > 0 {
(e.t(x - (y >> 1) - 2) + 2 * e.t(x - (y >> 1) - 1) + e.t(x - (y >> 1)) + 2) >> 2
} else if z == -1 {
(e.l(0) + 2 * e.tl + e.t(0) + 2) >> 2
} else {
(e.l(y - 2 * x - 1) + 2 * e.l(y - 2 * x - 2) + e.l(y - 2 * x - 3) + 2) >> 2
}
}
fn horizontal_down(e: &Edge, x: i32, y: i32) -> i32 {
let z = 2 * y - x;
if z >= 0 && z % 2 == 0 {
(e.l(y - (x >> 1) - 1) + e.l(y - (x >> 1)) + 1) >> 1
} else if z > 0 {
(e.l(y - (x >> 1) - 2) + 2 * e.l(y - (x >> 1) - 1) + e.l(y - (x >> 1)) + 2) >> 2
} else if z == -1 {
(e.l(0) + 2 * e.tl + e.t(0) + 2) >> 2
} else {
(e.t(x - 2 * y - 1) + 2 * e.t(x - 2 * y - 2) + e.t(x - 2 * y - 3) + 2) >> 2
}
}
fn vertical_left(e: &Edge, x: i32, y: i32) -> i32 {
let i = x + (y >> 1);
if y % 2 == 0 { (e.t(i) + e.t(i + 1) + 1) >> 1 } else { (e.t(i) + 2 * e.t(i + 1) + e.t(i + 2) + 2) >> 2 }
}
fn horizontal_up(e: &Edge, n: i32, x: i32, y: i32) -> i32 {
let zmax = 2 * n - 3;
let z = x + 2 * y;
let i = y + (x >> 1);
if z < zmax && z % 2 == 0 {
(e.l(i) + e.l(i + 1) + 1) >> 1
} else if z < zmax {
(e.l(i) + 2 * e.l(i + 1) + e.l(i + 2) + 2) >> 2
} else if z == zmax {
(e.l(n - 2) + 3 * e.l(n - 1) + 2) >> 2
} else {
e.l(n - 1)
}
}
/// Intra_4x4 prediction written into `plane` at (x, y).
pub fn pred4x4(plane: &mut [u8], stride: usize, x: usize, y: usize, mode: u8, a: Avail) {
let e = load_edge(plane, stride, x, y, 4, a);
pred_nxn(mode, &e, 4, a, &mut plane[y * stride + x..], stride);
}
/// Intra_8x8 prediction (with reference sample filtering) written into `plane` at (x, y).
pub fn pred8x8(plane: &mut [u8], stride: usize, x: usize, y: usize, mode: u8, a: Avail) {
let p = load_edge(plane, stride, x, y, 8, a);
let mut f = p;
if a.top {
f.top[0] = if a.top_left { (p.tl + 2 * p.top[0] + p.top[1] + 2) >> 2 } else { (3 * p.top[0] + p.top[1] + 2) >> 2 };
for i in 1..15 {
f.top[i] = (p.top[i - 1] + 2 * p.top[i] + p.top[i + 1] + 2) >> 2;
}
f.top[15] = (p.top[14] + 3 * p.top[15] + 2) >> 2;
}
if a.top_left {
f.tl = match (a.top, a.left) {
(true, true) => (p.top[0] + 2 * p.tl + p.left[0] + 2) >> 2,
(true, false) => (3 * p.tl + p.top[0] + 2) >> 2,
(false, true) => (3 * p.tl + p.left[0] + 2) >> 2,
_ => p.tl,
};
}
if a.left {
f.left[0] = if a.top_left { (p.tl + 2 * p.left[0] + p.left[1] + 2) >> 2 } else { (3 * p.left[0] + p.left[1] + 2) >> 2 };
for i in 1..7 {
f.left[i] = (p.left[i - 1] + 2 * p.left[i] + p.left[i + 1] + 2) >> 2;
}
f.left[7] = (p.left[6] + 3 * p.left[7] + 2) >> 2;
}
pred_nxn(mode, &f, 8, a, &mut plane[y * stride + x..], stride);
}
#[inline(always)]
fn clip(v: i32) -> u8 {
v.clamp(0, 255) as u8
}
/// Intra_16x16 prediction (8.3.3) for the macroblock at luma (x, y).
pub fn pred16x16(plane: &mut [u8], stride: usize, x: usize, y: usize, mode: u8, a: Avail) {
let mut top = [0i32; 16];
let mut left = [0i32; 16];
if a.top {
for i in 0..16 {
top[i] = plane[(y - 1) * stride + x + i] as i32;
}
}
if a.left {
for i in 0..16 {
left[i] = plane[(y + i) * stride + x - 1] as i32;
}
}
let base = y * stride + x;
match mode {
0 => {
for r in 0..16 {
for c in 0..16 {
plane[base + r * stride + c] = top[c] as u8;
}
}
}
1 => {
for r in 0..16 {
plane[base + r * stride..base + r * stride + 16].fill(left[r] as u8);
}
}
2 => {
let st: i32 = top.iter().sum();
let sl: i32 = left.iter().sum();
let dc = match (a.top, a.left) {
(true, true) => (st + sl + 16) >> 5,
(false, true) => (sl + 8) >> 4,
(true, false) => (st + 8) >> 4,
_ => 128,
} as u8;
for r in 0..16 {
plane[base + r * stride..base + r * stride + 16].fill(dc);
}
}
_ => {
let tl = if a.top_left { plane[(y - 1) * stride + x - 1] as i32 } else { 128 };
let t = |i: i32| if i < 0 { tl } else { top[i as usize] };
let l = |i: i32| if i < 0 { tl } else { left[i as usize] };
let mut h = 0;
let mut v = 0;
for k in 0..8 {
h += (k + 1) * (t(8 + k) - t(6 - k));
v += (k + 1) * (l(8 + k) - l(6 - k));
}
let aa = 16 * (left[15] + top[15]);
let b = (5 * h + 32) >> 6;
let c = (5 * v + 32) >> 6;
for r in 0..16 {
for cc in 0..16 {
plane[base + r * stride + cc] = clip((aa + b * (cc as i32 - 7) + c * (r as i32 - 7) + 16) >> 5);
}
}
}
}
}
/// Chroma intra prediction for a 4:2:0 macroblock (8x8 block at chroma (x, y)), 8.3.4.
/// Modes: 0 DC, 1 horizontal, 2 vertical, 3 plane.
pub fn pred_chroma(plane: &mut [u8], stride: usize, x: usize, y: usize, mode: u8, a: Avail) {
const W: usize = 8;
const H: usize = 8;
let mut top = [0i32; W];
let mut left = [0i32; H];
if a.top {
for i in 0..W {
top[i] = plane[(y - 1) * stride + x + i] as i32;
}
}
if a.left {
for i in 0..H {
left[i] = plane[(y + i) * stride + x - 1] as i32;
}
}
let base = y * stride + x;
match mode {
0 => {
for by in 0..H / 4 {
for bx in 0..W / 4 {
let st: i32 = top[bx * 4..bx * 4 + 4].iter().sum();
let sl: i32 = left[by * 4..by * 4 + 4].iter().sum();
let dc = if (bx == 0 && by == 0) || (bx > 0 && by > 0) {
match (a.top, a.left) {
(true, true) => (st + sl + 4) >> 3,
(false, true) => (sl + 2) >> 2,
(true, false) => (st + 2) >> 2,
_ => 128,
}
} else if bx > 0 {
if a.top {
(st + 2) >> 2
} else if a.left {
(sl + 2) >> 2
} else {
128
}
} else if a.left {
(sl + 2) >> 2
} else if a.top {
(st + 2) >> 2
} else {
128
};
for r in 0..4 {
let o = base + (by * 4 + r) * stride + bx * 4;
plane[o..o + 4].fill(dc as u8);
}
}
}
}
1 => {
for r in 0..H {
plane[base + r * stride..base + r * stride + W].fill(left[r] as u8);
}
}
2 => {
for r in 0..H {
for c in 0..W {
plane[base + r * stride + c] = top[c] as u8;
}
}
}
_ => {
let tl = if a.top_left { plane[(y - 1) * stride + x - 1] as i32 } else { 128 };
let t = |i: i32| if i < 0 { tl } else { top[i as usize] };
let l = |i: i32| if i < 0 { tl } else { left[i as usize] };
let mut h = 0;
let mut v = 0;
for k in 0..4 {
h += (k + 1) * (t(4 + k) - t(2 - k));
v += (k + 1) * (l(4 + k) - l(2 - k));
}
let aa = 16 * (left[H - 1] + top[W - 1]);
let b = (34 * h + 32) >> 6;
let c = (34 * v + 32) >> 6;
for r in 0..H {
for cc in 0..W {
plane[base + r * stride + cc] = clip((aa + b * (cc as i32 - 3) + c * (r as i32 - 3) + 16) >> 5);
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn setup() -> (Vec<u8>, usize) {
// 12x12 plane; block at (4,4); top row y=3 = 10,20,..; left column x=3 = 1,2,3,...
let stride = 16;
let mut p = vec![0u8; stride * 16];
for i in 0..12 {
p[3 * stride + 4 + i] = (10 * (i + 1)) as u8;
}
for i in 0..8 {
p[(4 + i) * stride + 3] = (i + 1) as u8;
}
p[3 * stride + 3] = 5;
(p, stride)
}
const ALL: Avail = Avail { left: true, top: true, top_left: true, top_right: true };
#[test]
fn i4_vertical_horizontal_dc() {
let (mut p, s) = setup();
pred4x4(&mut p, s, 4, 4, 0, ALL);
assert_eq!(&p[4 * s + 4..4 * s + 8], &[10, 20, 30, 40]);
assert_eq!(&p[7 * s + 4..7 * s + 8], &[10, 20, 30, 40]);
pred4x4(&mut p, s, 4, 4, 1, ALL);
assert_eq!(&p[5 * s + 4..5 * s + 8], &[2, 2, 2, 2]);
pred4x4(&mut p, s, 4, 4, 2, ALL);
// (10+20+30+40 + 1+2+3+4 + 4) >> 3 = 114 >> 3 = 14
assert_eq!(p[4 * s + 4], 14);
}
#[test]
fn i4_diag_down_left_hand_computed() {
let (mut p, s) = setup();
pred4x4(&mut p, s, 4, 4, 3, ALL);
// pred[0,0] = (t0 + 2 t1 + t2 + 2) >> 2 = (10 + 40 + 30 + 2) >> 2 = 20
assert_eq!(p[4 * s + 4], 20);
// pred[3,3] = (t6 + 3 t7 + 2) >> 2 = (70 + 240 + 2) >> 2 = 78
assert_eq!(p[7 * s + 7], 78);
}
#[test]
fn i4_diag_down_right_hand_computed() {
let (mut p, s) = setup();
pred4x4(&mut p, s, 4, 4, 4, ALL);
// x==y: (t0 + 2 tl + l0 + 2) >> 2 = (10 + 10 + 1 + 2) >> 2 = 5
assert_eq!(p[4 * s + 4], 5);
// x=1,y=0: (tl + 2 t0 + t1 + 2)>>2 = (5 + 20 + 20 + 2) >> 2 = 11
assert_eq!(p[4 * s + 5], 11);
// x=0,y=1: (tl + 2 l0 + l1 + 2) >> 2 = (5 + 2 + 2 + 2) >> 2 = 2
assert_eq!(p[5 * s + 4], 2);
}
#[test]
fn i4_top_right_substitution() {
let (mut p, s) = setup();
let a = Avail { top_right: false, ..ALL };
pred4x4(&mut p, s, 4, 4, 3, a);
// top-right replaced by t3 = 40: pred[3,3] = (40 + 120 + 2) >> 2 = 40
assert_eq!(p[7 * s + 7], 40);
}
#[test]
fn i16_plane_flat_is_flat() {
let s = 32;
let mut p = vec![77u8; s * 32];
pred16x16(&mut p, s, 8, 8, 3, ALL);
assert!(p[8 * s + 8..8 * s + 24].iter().all(|&v| v == 77));
}
#[test]
fn chroma_dc_rules() {
let s = 16;
let mut p = vec![0u8; s * 16];
for i in 0..8 {
p[3 * s + 4 + i] = if i < 4 { 40 } else { 80 };
p[(4 + i) * s + 3] = if i < 4 { 8 } else { 16 };
}
pred_chroma(&mut p, s, 4, 4, 0, ALL);
assert_eq!(p[4 * s + 4], (160 + 32 + 4) >> 3); // top-left block: both
assert_eq!(p[4 * s + 8], 80); // top-right block: top only
assert_eq!(p[8 * s + 4], 16); // bottom-left: left only
assert_eq!(p[8 * s + 8], ((320 + 64 + 4) >> 3) as u8); // bottom-right: both
}
}
-86
View File
@@ -1,86 +0,0 @@
//! Clean-room pure-Rust H.264 / AVC decoder, implemented from ITU-T Rec. H.264 (ISO/IEC 14496-10).
//!
//! Supported: progressive (frame) coding, 8-bit 4:2:0, Baseline / Main / High profiles — CAVLC and CABAC,
//! I/P/B slices, 8x8 transform, custom scaling matrices, weighted prediction, spatial/temporal direct,
//! multiple slices, long-term references and all MMCOs, deblocking.
//!
//! ```no_run
//! let mut dec = deckcraft_h264::Decoder::new();
//! let stream = std::fs::read("video.h264").unwrap();
//! for pic in dec.decode(&stream, 0).unwrap() {
//! println!("{}x{} poc {}", pic.width, pic.height, pic.poc);
//! }
//! for pic in dec.flush() {
//! let _ = pic;
//! }
//! ```
// Index loops over fixed-size blocks read more clearly than iterator chains in codec code.
#![allow(clippy::needless_range_loop, clippy::too_many_arguments)]
#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))]
mod cabac;
mod cabac_mb;
#[rustfmt::skip]
mod cabac_tables;
mod cavlc;
#[rustfmt::skip]
mod cavlc_tables;
mod deblock;
mod decoder;
mod dpb;
mod error;
mod inter;
mod intra;
mod mbtypes;
pub mod params;
mod picture;
pub use picture::set_plane_allocator;
pub mod slice;
mod slicedec;
#[cfg(test)]
mod synth_tests;
mod tables;
mod transform;
pub use decoder::{DecodeStats, Decoder, default_threads};
pub use error::{Error, Result};
/// Colour description from the VUI (ITU-T H.273 code points).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ColorInfo {
/// `video_full_range_flag`.
pub full_range: bool,
pub primaries: u8,
pub transfer: u8,
pub matrix: u8,
}
/// A decoded, cropped picture in 8-bit planar 4:2:0.
#[derive(Clone, Debug)]
pub struct Picture {
/// Cropped luma width.
pub width: u32,
/// Cropped luma height.
pub height: u32,
pub chroma_width: u32,
pub chroma_height: u32,
pub y: Vec<u8>,
pub u: Vec<u8>,
pub v: Vec<u8>,
pub y_stride: usize,
pub uv_stride: usize,
/// Presentation timestamp passed to [`Decoder::decode`] with the access unit of this picture.
pub pts: i64,
/// Picture order count.
pub poc: i32,
/// IDR picture.
pub key: bool,
pub color: ColorInfo,
/// Sample aspect ratio (0, 0 when unspecified).
pub sar: (u16, u16),
/// Decoded in draft mode ([`Decoder::set_draft`]): a non-reference picture whose deblocking
/// filter was skipped. Its samples are approximate (not the conforming output); no other
/// picture is affected.
pub draft: bool,
}
-235
View File
@@ -1,235 +0,0 @@
//! Macroblock / sub-macroblock type tables (7.4.5, Tables 7-11 .. 7-18).
use crate::picture::MbKind;
pub const PRED_L0: u8 = 1;
pub const PRED_L1: u8 = 2;
pub const PRED_BI: u8 = 3;
/// Macroblock partitioning for inter macroblocks.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum Part {
#[default]
P16x16,
P16x8,
P8x16,
P8x8,
}
impl Part {
pub fn num_parts(self) -> usize {
match self {
Part::P16x16 => 1,
Part::P16x8 | Part::P8x16 => 2,
Part::P8x8 => 4,
}
}
/// (x, y, w, h) of partition `i` in 4x4-block units.
pub fn rect(self, i: usize) -> (usize, usize, usize, usize) {
match self {
Part::P16x16 => (0, 0, 4, 4),
Part::P16x8 => (0, 2 * i, 4, 2),
Part::P8x16 => (2 * i, 0, 2, 4),
Part::P8x8 => ((i & 1) * 2, (i >> 1) * 2, 2, 2),
}
}
}
/// Sub-macroblock partitioning.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum SubPart {
#[default]
S8x8,
S8x4,
S4x8,
S4x4,
}
impl SubPart {
pub fn num_parts(self) -> usize {
match self {
SubPart::S8x8 => 1,
SubPart::S8x4 | SubPart::S4x8 => 2,
SubPart::S4x4 => 4,
}
}
/// (x, y, w, h) of sub-partition `j` relative to the 8x8 block, in 4x4 units.
pub fn rect(self, j: usize) -> (usize, usize, usize, usize) {
match self {
SubPart::S8x8 => (0, 0, 2, 2),
SubPart::S8x4 => (0, j, 2, 1),
SubPart::S4x8 => (j, 0, 1, 2),
SubPart::S4x4 => (j & 1, j >> 1, 1, 1),
}
}
}
/// Decoded meaning of mb_type.
#[derive(Clone, Copy, Debug, Default)]
pub struct MbTypeInfo {
pub kind: MbKind,
/// Intra16x16PredMode.
pub i16_mode: u8,
/// coded_block_pattern implied by an Intra_16x16 mb_type.
pub i16_cbp: u8,
pub part: Part,
/// Prediction list flags per macroblock partition.
pub pred: [u8; 2],
/// P_8x8ref0.
pub ref0: bool,
}
/// Intra mb_type (I-slice numbering 0..=25).
pub fn intra_mb_type(t: u32) -> Option<MbTypeInfo> {
let mut m = MbTypeInfo::default();
match t {
0 => m.kind = MbKind::I4x4, // or I8x8, decided by transform_size_8x8_flag
1..=24 => {
m.kind = MbKind::I16x16;
m.i16_mode = ((t - 1) % 4) as u8;
let chroma = ((t - 1) / 4) % 3;
let luma = if t >= 13 { 15 } else { 0 };
m.i16_cbp = (luma | (chroma << 4)) as u8;
}
25 => m.kind = MbKind::IPcm,
_ => return None,
}
Some(m)
}
/// P-slice mb_type 0..=4 (5.. are intra, handled by the caller).
pub fn p_mb_type(t: u32) -> Option<MbTypeInfo> {
let mut m = MbTypeInfo { kind: MbKind::Inter, pred: [PRED_L0; 2], ..Default::default() };
m.part = match t {
0 => Part::P16x16,
1 => Part::P16x8,
2 => Part::P8x16,
3 => Part::P8x8,
4 => {
m.ref0 = true;
Part::P8x8
}
_ => return None,
};
Some(m)
}
/// B-slice mb_type 0..=22 (23.. are intra).
pub fn b_mb_type(t: u32) -> Option<MbTypeInfo> {
let mut m = MbTypeInfo { kind: MbKind::Inter, ..Default::default() };
match t {
0 => {
m.kind = MbKind::BDirect16x16;
m.pred = [PRED_BI; 2];
}
1..=3 => {
m.part = Part::P16x16;
m.pred = [t as u8; 2];
}
4..=21 => {
const PAIRS: [(u8, u8); 9] = [
(PRED_L0, PRED_L0),
(PRED_L1, PRED_L1),
(PRED_L0, PRED_L1),
(PRED_L1, PRED_L0),
(PRED_L0, PRED_BI),
(PRED_L1, PRED_BI),
(PRED_BI, PRED_L0),
(PRED_BI, PRED_L1),
(PRED_BI, PRED_BI),
];
let (a, b) = PAIRS[((t - 4) / 2) as usize];
m.pred = [a, b];
m.part = if t.is_multiple_of(2) { Part::P16x8 } else { Part::P8x16 };
}
22 => m.part = Part::P8x8,
_ => return None,
}
Some(m)
}
/// Sub-macroblock type: (shape, prediction flags, direct).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct SubMbInfo {
pub shape: SubPart,
pub pred: u8,
pub direct: bool,
}
pub fn p_sub_mb_type(t: u32) -> Option<SubMbInfo> {
let shape = match t {
0 => SubPart::S8x8,
1 => SubPart::S8x4,
2 => SubPart::S4x8,
3 => SubPart::S4x4,
_ => return None,
};
Some(SubMbInfo { shape, pred: PRED_L0, direct: false })
}
pub fn b_sub_mb_type(t: u32) -> Option<SubMbInfo> {
let (shape, pred, direct) = match t {
0 => (SubPart::S8x8, PRED_BI, true),
1 => (SubPart::S8x8, PRED_L0, false),
2 => (SubPart::S8x8, PRED_L1, false),
3 => (SubPart::S8x8, PRED_BI, false),
4 => (SubPart::S8x4, PRED_L0, false),
5 => (SubPart::S4x8, PRED_L0, false),
6 => (SubPart::S8x4, PRED_L1, false),
7 => (SubPart::S4x8, PRED_L1, false),
8 => (SubPart::S8x4, PRED_BI, false),
9 => (SubPart::S4x8, PRED_BI, false),
10 => (SubPart::S4x4, PRED_L0, false),
11 => (SubPart::S4x4, PRED_L1, false),
12 => (SubPart::S4x4, PRED_BI, false),
_ => return None,
};
Some(SubMbInfo { shape, pred, direct })
}
/// Table 9-4 (a): codeNum -> coded_block_pattern for (Intra_4x4/Intra_8x8, Inter), ChromaArrayType 1 or 2.
#[rustfmt::skip]
pub const CBP_ME: [(u8, u8); 48] = [
(47, 0), (31, 16), (15, 1), (0, 2), (23, 4), (27, 8), (29, 32), (30, 3),
(7, 5), (11, 10), (13, 12), (14, 15), (39, 47), (43, 7), (45, 11), (46, 13),
(16, 14), (3, 6), (5, 9), (10, 31), (12, 35), (19, 37), (21, 42), (26, 44),
(28, 33), (35, 34), (37, 36), (42, 40), (44, 39), (1, 43), (2, 45), (4, 46),
(8, 17), (17, 18), (18, 20), (20, 24), (24, 19), (6, 21), (9, 26), (22, 28),
(25, 23), (32, 27), (33, 29), (34, 30), (36, 22), (40, 25), (38, 38), (41, 41),
];
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cbp_table_is_permutation() {
let mut a = [false; 48];
let mut b = [false; 48];
for &(i, p) in &CBP_ME {
a[i as usize] = true;
b[p as usize] = true;
}
assert!(a.iter().all(|&x| x) && b.iter().all(|&x| x));
}
#[test]
fn i16_types() {
let m = intra_mb_type(1).unwrap();
assert_eq!((m.kind, m.i16_mode, m.i16_cbp), (MbKind::I16x16, 0, 0));
let m = intra_mb_type(24).unwrap();
assert_eq!((m.i16_mode, m.i16_cbp), (3, 15 | (2 << 4)));
let m = intra_mb_type(13).unwrap();
assert_eq!((m.i16_mode, m.i16_cbp), (0, 15));
}
#[test]
fn b_types() {
let m = b_mb_type(12).unwrap();
assert_eq!((m.part, m.pred), (Part::P16x8, [PRED_L0, PRED_BI]));
let m = b_mb_type(21).unwrap();
assert_eq!((m.part, m.pred), (Part::P8x16, [PRED_BI, PRED_BI]));
let m = b_mb_type(11).unwrap();
assert_eq!((m.part, m.pred), (Part::P8x16, [PRED_L1, PRED_L0]));
}
}
-823
View File
@@ -1,823 +0,0 @@
//! Sequence and picture parameter sets (7.3.2.1, 7.3.2.2) including VUI and scaling lists.
use crate::error::{Result, ensure, unsupported};
use crate::tables::{DEFAULT_4X4_INTER, DEFAULT_4X4_INTRA, DEFAULT_8X8_INTER, DEFAULT_8X8_INTRA, ZIGZAG4, ZIGZAG8};
use deckcraft_bitstream::BitReader;
/// Scaling matrices after applying fall-back rules, stored in raster order (weightScale).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ScalingMatrices {
/// [Intra Y, Intra Cb, Intra Cr, Inter Y, Inter Cb, Inter Cr], raster order.
pub m4: [[u8; 16]; 6],
/// [Intra Y, Inter Y, Intra Cb, Inter Cb, Intra Cr, Inter Cr], raster order.
pub m8: [[u8; 64]; 6],
}
impl ScalingMatrices {
pub fn flat() -> Self {
Self { m4: [[16; 16]; 6], m8: [[16; 64]; 6] }
}
pub fn is_flat(&self) -> bool {
self.m4.iter().all(|m| m.iter().all(|&v| v == 16)) && self.m8.iter().all(|m| m.iter().all(|&v| v == 16))
}
}
/// Lists as parsed, in zig-zag order, before inverse scanning.
#[derive(Clone, Debug)]
struct ZzLists {
l4: [[u8; 16]; 6],
l8: [[u8; 64]; 6],
}
fn zz4_to_raster(l: &[u8; 16]) -> [u8; 16] {
let mut out = [0; 16];
for (k, &v) in l.iter().enumerate() {
out[ZIGZAG4[k] as usize] = v;
}
out
}
fn zz8_to_raster(l: &[u8; 64]) -> [u8; 64] {
let mut out = [0; 64];
for (k, &v) in l.iter().enumerate() {
out[ZIGZAG8[k] as usize] = v;
}
out
}
impl ZzLists {
fn to_matrices(&self) -> ScalingMatrices {
let mut m = ScalingMatrices::flat();
for i in 0..6 {
m.m4[i] = zz4_to_raster(&self.l4[i]);
m.m8[i] = zz8_to_raster(&self.l8[i]);
}
m
}
fn flat() -> Self {
Self { l4: [[16; 16]; 6], l8: [[16; 64]; 6] }
}
}
/// Result of parsing one scaling_list(): Some(list) or None meaning "use default".
fn parse_scaling_list<const N: usize>(r: &mut BitReader) -> Result<Option<[u8; N]>> {
let mut list = [0u8; N];
let mut last = 8i32;
let mut next = 8i32;
for (j, item) in list.iter_mut().enumerate() {
if next != 0 {
let delta = r.read_se()?;
ensure!((-128..=127).contains(&delta), "delta_scale out of range");
next = (last + delta + 256) % 256;
if j == 0 && next == 0 {
return Ok(None);
}
}
*item = if next == 0 { last as u8 } else { next as u8 };
last = *item as i32;
}
Ok(Some(list))
}
/// Parse scaling lists with a fall-back rule. `fallback` gives the lists used for rule B
/// (sequence-level) or None for rule A.
fn parse_scaling_lists(r: &mut BitReader, count: usize, fallback: Option<&ZzLists>) -> Result<ZzLists> {
let mut out = ZzLists::flat();
for i in 0..count.max(8) {
let present = if i < count { r.read_flag()? } else { false };
if i < 6 {
let parsed = if present { Some(parse_scaling_list::<16>(r)?) } else { None };
out.l4[i] = match parsed {
Some(Some(l)) => l,
Some(None) => {
if i < 3 {
DEFAULT_4X4_INTRA
} else {
DEFAULT_4X4_INTER
}
}
None => match (i, fallback) {
(0, None) => DEFAULT_4X4_INTRA,
(3, None) => DEFAULT_4X4_INTER,
(0 | 3, Some(f)) => f.l4[i],
_ => out.l4[i - 1],
},
};
} else {
let k = i - 6;
if k >= 6 {
break;
}
let parsed = if present { Some(parse_scaling_list::<64>(r)?) } else { None };
out.l8[k] = match parsed {
Some(Some(l)) => l,
Some(None) => {
if k % 2 == 0 {
DEFAULT_8X8_INTRA
} else {
DEFAULT_8X8_INTER
}
}
None => match (k, fallback) {
(0, None) => DEFAULT_8X8_INTRA,
(1, None) => DEFAULT_8X8_INTER,
(0 | 1, Some(f)) => f.l8[k],
_ => out.l8[k - 2],
},
};
}
}
// 8x8 chroma lists that were never signalled (count == 8) follow rule A/B chains as well.
if count <= 8 {
for k in 2..6 {
out.l8[k] = out.l8[k - 2];
}
}
Ok(out)
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct HrdParameters {
pub cpb_cnt: u32,
pub bit_rate_scale: u8,
pub cpb_size_scale: u8,
pub bit_rate_value_minus1: Vec<u32>,
pub cpb_size_value_minus1: Vec<u32>,
pub cbr_flag: Vec<bool>,
pub initial_cpb_removal_delay_length: u8,
pub cpb_removal_delay_length: u8,
pub dpb_output_delay_length: u8,
pub time_offset_length: u8,
}
fn parse_hrd(r: &mut BitReader) -> Result<HrdParameters> {
let cpb_cnt = r.read_ue_plus(1)?;
ensure!(cpb_cnt <= 32, "cpb_cnt_minus1 out of range");
let mut h = HrdParameters { cpb_cnt, bit_rate_scale: r.read_bits(4)? as u8, cpb_size_scale: r.read_bits(4)? as u8, ..Default::default() };
for _ in 0..cpb_cnt {
h.bit_rate_value_minus1.push(r.read_ue()?);
h.cpb_size_value_minus1.push(r.read_ue()?);
h.cbr_flag.push(r.read_flag()?);
}
h.initial_cpb_removal_delay_length = r.read_bits(5)? as u8 + 1;
h.cpb_removal_delay_length = r.read_bits(5)? as u8 + 1;
h.dpb_output_delay_length = r.read_bits(5)? as u8 + 1;
h.time_offset_length = r.read_bits(5)? as u8;
Ok(h)
}
/// Video usability information (Annex E).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Vui {
pub aspect_ratio_idc: u8,
pub sar: (u16, u16),
pub overscan_appropriate: Option<bool>,
pub video_format: u8,
pub full_range: bool,
pub colour_primaries: u8,
pub transfer_characteristics: u8,
pub matrix_coefficients: u8,
pub chroma_loc: Option<(u32, u32)>,
pub timing: Option<(u32, u32, bool)>,
pub nal_hrd: Option<HrdParameters>,
pub vcl_hrd: Option<HrdParameters>,
pub low_delay_hrd: bool,
pub pic_struct_present: bool,
pub bitstream_restriction: Option<BitstreamRestriction>,
}
impl Default for Vui {
fn default() -> Self {
Self {
aspect_ratio_idc: 0,
sar: (0, 0),
overscan_appropriate: None,
video_format: 5,
full_range: false,
colour_primaries: 2,
transfer_characteristics: 2,
matrix_coefficients: 2,
chroma_loc: None,
timing: None,
nal_hrd: None,
vcl_hrd: None,
low_delay_hrd: false,
pic_struct_present: false,
bitstream_restriction: None,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BitstreamRestriction {
pub motion_vectors_over_pic_boundaries: bool,
pub max_bytes_per_pic_denom: u32,
pub max_bits_per_mb_denom: u32,
pub log2_max_mv_length_horizontal: u32,
pub log2_max_mv_length_vertical: u32,
pub max_num_reorder_frames: u32,
pub max_dec_frame_buffering: u32,
}
/// Table E-1 sample aspect ratios.
const SAR_TABLE: [(u16, u16); 17] = [
(0, 0),
(1, 1),
(12, 11),
(10, 11),
(16, 11),
(40, 33),
(24, 11),
(20, 11),
(32, 11),
(80, 33),
(18, 11),
(15, 11),
(64, 33),
(160, 99),
(4, 3),
(3, 2),
(2, 1),
];
fn parse_vui(r: &mut BitReader) -> Result<Vui> {
let mut v = Vui::default();
if r.read_flag()? {
v.aspect_ratio_idc = r.read_bits(8)? as u8;
if v.aspect_ratio_idc == 255 {
v.sar = (r.read_bits(16)? as u16, r.read_bits(16)? as u16);
} else if (v.aspect_ratio_idc as usize) < SAR_TABLE.len() {
v.sar = SAR_TABLE[v.aspect_ratio_idc as usize];
}
}
if r.read_flag()? {
v.overscan_appropriate = Some(r.read_flag()?);
}
if r.read_flag()? {
v.video_format = r.read_bits(3)? as u8;
v.full_range = r.read_flag()?;
if r.read_flag()? {
v.colour_primaries = r.read_bits(8)? as u8;
v.transfer_characteristics = r.read_bits(8)? as u8;
v.matrix_coefficients = r.read_bits(8)? as u8;
}
}
if r.read_flag()? {
v.chroma_loc = Some((r.read_ue()?, r.read_ue()?));
}
if r.read_flag()? {
let n = r.read_bits(32)?;
let t = r.read_bits(32)?;
let fixed = r.read_flag()?;
v.timing = Some((n, t, fixed));
}
if r.read_flag()? {
v.nal_hrd = Some(parse_hrd(r)?);
}
if r.read_flag()? {
v.vcl_hrd = Some(parse_hrd(r)?);
}
if v.nal_hrd.is_some() || v.vcl_hrd.is_some() {
v.low_delay_hrd = r.read_flag()?;
}
v.pic_struct_present = r.read_flag()?;
// Some streams truncate the VUI after this point; treat EOF as "absent".
if r.bits_left() > 0 && r.read_flag()? {
let br = (|| -> Result<BitstreamRestriction> {
Ok(BitstreamRestriction {
motion_vectors_over_pic_boundaries: r.read_flag()?,
max_bytes_per_pic_denom: r.read_ue()?,
max_bits_per_mb_denom: r.read_ue()?,
log2_max_mv_length_horizontal: r.read_ue()?,
log2_max_mv_length_vertical: r.read_ue()?,
max_num_reorder_frames: r.read_ue()?,
max_dec_frame_buffering: r.read_ue()?,
})
})();
v.bitstream_restriction = br.ok();
}
Ok(v)
}
/// Sequence parameter set.
#[derive(Clone, Debug)]
pub struct Sps {
pub profile_idc: u8,
pub constraint_flags: u8,
pub level_idc: u8,
pub id: u32,
pub chroma_format_idc: u32,
pub separate_colour_plane: bool,
pub bit_depth_luma: u32,
pub bit_depth_chroma: u32,
pub qpprime_y_zero_transform_bypass: bool,
pub seq_scaling_matrix_present: bool,
scaling_lists: ZzLists,
pub scaling: ScalingMatrices,
pub log2_max_frame_num: u32,
pub pic_order_cnt_type: u32,
pub log2_max_poc_lsb: u32,
pub delta_pic_order_always_zero: bool,
pub offset_for_non_ref_pic: i32,
pub offset_for_top_to_bottom_field: i32,
pub offset_for_ref_frame: Vec<i32>,
pub max_num_ref_frames: u32,
pub gaps_in_frame_num_allowed: bool,
pub pic_width_in_mbs: u32,
pub pic_height_in_map_units: u32,
pub frame_mbs_only: bool,
pub mb_adaptive_frame_field: bool,
pub direct_8x8_inference: bool,
/// (left, right, top, bottom) in crop units as coded.
pub frame_crop: Option<(u32, u32, u32, u32)>,
pub vui: Option<Vui>,
}
impl Sps {
pub fn parse(rbsp: &[u8]) -> Result<Sps> {
let mut r = BitReader::new(rbsp);
let profile_idc = r.read_bits(8)? as u8;
let constraint_flags = r.read_bits(8)? as u8;
let level_idc = r.read_bits(8)? as u8;
let id = r.read_ue()?;
ensure!(id < 32, "seq_parameter_set_id {id} out of range");
let mut chroma_format_idc = 1;
let mut separate_colour_plane = false;
let mut bit_depth_luma = 8;
let mut bit_depth_chroma = 8;
let mut bypass = false;
let mut seq_scaling_matrix_present = false;
let mut scaling_lists = ZzLists::flat();
if matches!(profile_idc, 100 | 110 | 122 | 244 | 44 | 83 | 86 | 118 | 128 | 138 | 139 | 134 | 135) {
chroma_format_idc = r.read_ue()?;
ensure!(chroma_format_idc <= 3, "chroma_format_idc out of range");
if chroma_format_idc == 3 {
separate_colour_plane = r.read_flag()?;
}
bit_depth_luma = r.read_ue_plus(8)?;
bit_depth_chroma = r.read_ue_plus(8)?;
ensure!(bit_depth_luma <= 14 && bit_depth_chroma <= 14, "bit depth out of range");
bypass = r.read_flag()?;
seq_scaling_matrix_present = r.read_flag()?;
if seq_scaling_matrix_present {
let count = if chroma_format_idc != 3 { 8 } else { 12 };
scaling_lists = parse_scaling_lists(&mut r, count, None)?;
}
}
let log2_max_frame_num = r.read_ue_plus(4)?;
ensure!(log2_max_frame_num <= 16, "log2_max_frame_num out of range");
let pic_order_cnt_type = r.read_ue()?;
ensure!(pic_order_cnt_type <= 2, "pic_order_cnt_type out of range");
let mut log2_max_poc_lsb = 0;
let mut delta_pic_order_always_zero = false;
let mut offset_for_non_ref_pic = 0;
let mut offset_for_top_to_bottom_field = 0;
let mut offset_for_ref_frame = Vec::new();
if pic_order_cnt_type == 0 {
log2_max_poc_lsb = r.read_ue_plus(4)?;
ensure!(log2_max_poc_lsb <= 16, "log2_max_pic_order_cnt_lsb out of range");
} else if pic_order_cnt_type == 1 {
delta_pic_order_always_zero = r.read_flag()?;
offset_for_non_ref_pic = r.read_se()?;
offset_for_top_to_bottom_field = r.read_se()?;
let n = r.read_ue()?;
ensure!(n <= 255, "num_ref_frames_in_pic_order_cnt_cycle out of range");
for _ in 0..n {
offset_for_ref_frame.push(r.read_se()?);
}
}
let max_num_ref_frames = r.read_ue()?;
ensure!(max_num_ref_frames <= 16, "max_num_ref_frames out of range");
let gaps_in_frame_num_allowed = r.read_flag()?;
let pic_width_in_mbs = r.read_ue_plus(1)?;
let pic_height_in_map_units = r.read_ue_plus(1)?;
ensure!(pic_width_in_mbs <= 1024 && pic_height_in_map_units <= 1024, "picture too large");
let frame_mbs_only = r.read_flag()?;
let mb_adaptive_frame_field = if !frame_mbs_only { r.read_flag()? } else { false };
let direct_8x8_inference = r.read_flag()?;
let frame_crop = if r.read_flag()? { Some((r.read_ue()?, r.read_ue()?, r.read_ue()?, r.read_ue()?)) } else { None };
let vui = if r.read_flag()? { Some(parse_vui(&mut r)?) } else { None };
let scaling = if seq_scaling_matrix_present { scaling_lists.to_matrices() } else { ScalingMatrices::flat() };
let sps = Sps {
profile_idc,
constraint_flags,
level_idc,
id,
chroma_format_idc,
separate_colour_plane,
bit_depth_luma,
bit_depth_chroma,
qpprime_y_zero_transform_bypass: bypass,
seq_scaling_matrix_present,
scaling_lists,
scaling,
log2_max_frame_num,
pic_order_cnt_type,
log2_max_poc_lsb,
delta_pic_order_always_zero,
offset_for_non_ref_pic,
offset_for_top_to_bottom_field,
offset_for_ref_frame,
max_num_ref_frames,
gaps_in_frame_num_allowed,
pic_width_in_mbs,
pic_height_in_map_units,
frame_mbs_only,
mb_adaptive_frame_field,
direct_8x8_inference,
frame_crop,
vui,
};
if let Some((l, rr, t, b)) = sps.frame_crop {
let (cx, cy) = sps.crop_unit();
let crop = |a: u32, b: u32, unit: u32| (u64::from(a) + u64::from(b)) * u64::from(unit);
ensure!(crop(l, rr, cx) < u64::from(sps.width()) && crop(t, b, cy) < u64::from(sps.height()), "frame cropping exceeds picture size");
}
Ok(sps)
}
/// ChromaArrayType.
pub fn chroma_array_type(&self) -> u32 {
if self.separate_colour_plane { 0 } else { self.chroma_format_idc }
}
pub fn frame_height_in_mbs(&self) -> u32 {
(2 - self.frame_mbs_only as u32) * self.pic_height_in_map_units
}
/// Decoded (uncropped) luma width.
pub fn width(&self) -> u32 {
self.pic_width_in_mbs * 16
}
pub fn height(&self) -> u32 {
self.frame_height_in_mbs() * 16
}
pub fn max_frame_num(&self) -> u32 {
1 << self.log2_max_frame_num
}
/// (CropUnitX, CropUnitY).
pub fn crop_unit(&self) -> (u32, u32) {
let (sub_w, sub_h) = match self.chroma_array_type() {
1 => (2, 2),
2 => (2, 1),
_ => (1, 1),
};
if self.chroma_array_type() == 0 { (1, 2 - self.frame_mbs_only as u32) } else { (sub_w, sub_h * (2 - self.frame_mbs_only as u32)) }
}
/// Cropping rectangle in luma samples: (x, y, width, height).
pub fn crop_rect(&self) -> (u32, u32, u32, u32) {
match self.frame_crop {
None => (0, 0, self.width(), self.height()),
Some((l, r, t, b)) => {
let (cx, cy) = self.crop_unit();
(l * cx, t * cy, self.width() - (l + r) * cx, self.height() - (t + b) * cy)
}
}
}
/// MaxDpbFrames from Table A-1 (and the VUI max_dec_frame_buffering when present).
pub fn max_dpb_frames(&self) -> usize {
let max_dpb_mbs: u32 = match self.level_idc {
9 => 396,
10 => 396,
11 => {
if self.constraint_flags & 0x10 != 0 && !matches!(self.profile_idc, 100 | 110 | 122 | 244 | 44) {
396
} else {
900
}
}
12 | 13 | 20 => 2376,
21 => 4752,
22 | 30 => 8100,
31 => 18000,
32 => 20480,
40 | 41 => 32768,
42 => 34816,
50 => 110400,
51 | 52 => 184320,
60..=62 => 696320,
_ => 184320,
};
let frame_mbs = self.pic_width_in_mbs * self.frame_height_in_mbs();
let mut n = (max_dpb_mbs / frame_mbs.max(1)).clamp(1, 16) as usize;
if let Some(br) = self.vui.as_ref().and_then(|v| v.bitstream_restriction.as_ref()) {
n = (br.max_dec_frame_buffering as usize).min(16);
}
n.max(self.max_num_ref_frames as usize).max(1)
}
/// Number of frames that may precede a frame in decoding order and follow it in output order.
pub fn max_num_reorder_frames(&self) -> usize {
if let Some(br) = self.vui.as_ref().and_then(|v| v.bitstream_restriction.as_ref()) {
return br.max_num_reorder_frames as usize;
}
// Profiles that cannot reorder (no B slices) with constraint_set3 (intra) etc.
if matches!(self.profile_idc, 44 | 86 | 100 | 110 | 122 | 244) && self.constraint_flags & 0x10 != 0 {
return 0;
}
self.max_dpb_frames()
}
pub fn check_supported(&self) -> Result<()> {
if !self.frame_mbs_only {
return unsupported("interlaced (field / MBAFF) coding");
}
if self.chroma_array_type() != 1 {
return unsupported(format!("chroma_format_idc {} (only 4:2:0 is implemented)", self.chroma_format_idc));
}
if self.bit_depth_luma != 8 || self.bit_depth_chroma != 8 {
return unsupported(format!("bit depth luma {} chroma {} (only 8-bit is implemented)", self.bit_depth_luma, self.bit_depth_chroma));
}
if self.qpprime_y_zero_transform_bypass {
return unsupported("qpprime_y_zero_transform_bypass (lossless)");
}
Ok(())
}
}
/// Picture parameter set.
#[derive(Clone, Debug)]
pub struct Pps {
pub id: u32,
pub sps_id: u32,
pub entropy_coding_mode: bool,
pub bottom_field_pic_order_in_frame_present: bool,
pub num_slice_groups: u32,
pub slice_group_map_type: u32,
pub num_ref_idx_l0_default_active: u32,
pub num_ref_idx_l1_default_active: u32,
pub weighted_pred: bool,
pub weighted_bipred_idc: u32,
pub pic_init_qp: i32,
pub pic_init_qs: i32,
pub chroma_qp_index_offset: i32,
pub deblocking_filter_control_present: bool,
pub constrained_intra_pred: bool,
pub redundant_pic_cnt_present: bool,
pub transform_8x8_mode: bool,
pub pic_scaling_matrix_present: bool,
pub second_chroma_qp_index_offset: i32,
/// Effective scaling matrices (after PPS/SPS fall-back), raster order.
pub scaling: ScalingMatrices,
}
/// `26 + minus26`, after `minus26` is inside −(26 + QpBdOffset) ..= 25.
fn qp_plus_26(minus26: i32, qp_bd: i32, name: &str) -> Result<i32> {
ensure!((-(26 + qp_bd)..=25).contains(&minus26), "{name} out of range");
Ok(26 + minus26)
}
impl Pps {
/// Parse a PPS. Needs the SPS table because the tail depends on chroma_format_idc.
pub fn parse(rbsp: &[u8], spss: &[Option<Sps>]) -> Result<Pps> {
let mut r = BitReader::new(rbsp);
let id = r.read_ue()?;
ensure!(id < 256, "pic_parameter_set_id out of range");
let sps_id = r.read_ue()?;
ensure!(sps_id < 32, "seq_parameter_set_id out of range");
let sps = spss
.get(sps_id as usize)
.and_then(Option::as_ref)
.ok_or_else(|| crate::Error::MissingParameterSet(format!("SPS {sps_id} for PPS {id}")))?;
let entropy_coding_mode = r.read_flag()?;
let bottom_field_pic_order_in_frame_present = r.read_flag()?;
let num_slice_groups = r.read_ue_plus(1)?;
ensure!(num_slice_groups <= 8, "num_slice_groups out of range");
let mut slice_group_map_type = 0;
if num_slice_groups > 1 {
// Parsed for completeness; FMO decoding is unsupported.
slice_group_map_type = r.read_ue()?;
match slice_group_map_type {
0 => {
for _ in 0..num_slice_groups {
r.read_ue()?;
}
}
2 => {
for _ in 0..num_slice_groups - 1 {
r.read_ue()?;
r.read_ue()?;
}
}
3..=5 => {
r.read_flag()?;
r.read_ue()?;
}
6 => {
let n = r.read_ue_plus(1)?;
let bits = 32 - (num_slice_groups - 1).leading_zeros();
for _ in 0..n {
r.read_bits(bits)?;
}
}
_ => {}
}
}
let num_ref_idx_l0_default_active = r.read_ue_plus(1)?;
let num_ref_idx_l1_default_active = r.read_ue_plus(1)?;
ensure!(num_ref_idx_l0_default_active <= 32 && num_ref_idx_l1_default_active <= 32, "num_ref_idx_default_active out of range");
let weighted_pred = r.read_flag()?;
let weighted_bipred_idc = r.read_bits(2)?;
// pic_init_qp_minus26 is −(26 + QpBdOffsetY) ..= 25. Check that before adding 26: a hostile
// Exp-Golomb value overflows the addition in debug builds.
let qp_bd = 6 * sps.bit_depth_luma.saturating_sub(8) as i32;
let pic_init_qp = qp_plus_26(r.read_se()?, qp_bd, "pic_init_qp_minus26")?;
let pic_init_qs = qp_plus_26(r.read_se()?, qp_bd, "pic_init_qs_minus26")?;
let chroma_qp_index_offset = r.read_se()?;
ensure!((-12..=12).contains(&chroma_qp_index_offset), "chroma_qp_index_offset out of range");
let deblocking_filter_control_present = r.read_flag()?;
let constrained_intra_pred = r.read_flag()?;
let redundant_pic_cnt_present = r.read_flag()?;
let mut transform_8x8_mode = false;
let mut pic_scaling_matrix_present = false;
let mut second_chroma_qp_index_offset = chroma_qp_index_offset;
let mut scaling = sps.scaling.clone();
if r.more_rbsp_data() {
transform_8x8_mode = r.read_flag()?;
pic_scaling_matrix_present = r.read_flag()?;
if pic_scaling_matrix_present {
let count = 6 + if sps.chroma_format_idc != 3 { 2 } else { 6 } * transform_8x8_mode as usize;
// Fall-back rule B uses the sequence-level lists (or Flat when the SPS has none, but then
// rule A applies per 7.4.2.2).
let lists = if sps.seq_scaling_matrix_present {
parse_scaling_lists(&mut r, count, Some(&sps.scaling_lists))?
} else {
parse_scaling_lists(&mut r, count, None)?
};
scaling = lists.to_matrices();
}
second_chroma_qp_index_offset = r.read_se()?;
ensure!((-12..=12).contains(&second_chroma_qp_index_offset), "second_chroma_qp_index_offset out of range");
}
Ok(Pps {
id,
sps_id,
entropy_coding_mode,
bottom_field_pic_order_in_frame_present,
num_slice_groups,
slice_group_map_type,
num_ref_idx_l0_default_active,
num_ref_idx_l1_default_active,
weighted_pred,
weighted_bipred_idc,
pic_init_qp,
pic_init_qs,
chroma_qp_index_offset,
deblocking_filter_control_present,
constrained_intra_pred,
redundant_pic_cnt_present,
transform_8x8_mode,
pic_scaling_matrix_present,
second_chroma_qp_index_offset,
scaling,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use deckcraft_bitstream::BitWriter;
#[test]
fn scaling_list_default_signal() {
// delta_scale = -8 on the first entry => nextScale 0 => use default.
let mut w = BitWriter::new();
w.write_se(-8);
w.rbsp_trailing();
let b = w.finish();
let mut r = BitReader::new(&b);
assert_eq!(parse_scaling_list::<16>(&mut r).unwrap(), None);
}
#[test]
fn scaling_list_repeat_last() {
// 8 -> +2 = 10, then +0 = 10, then -10 => 0 => repeat 10 for the rest.
let mut w = BitWriter::new();
w.write_se(2);
w.write_se(0);
w.write_se(-10);
w.rbsp_trailing();
let b = w.finish();
let mut r = BitReader::new(&b);
let l = parse_scaling_list::<16>(&mut r).unwrap().unwrap();
assert_eq!(l, [10; 16]);
}
#[test]
fn parse_minimal_baseline_sps() {
// profile 66, level 30, sps_id 0, log2_max_frame_num_minus4 0, poc type 2, 1 ref, 11x9 MBs.
let mut w = BitWriter::new();
w.write_bits(66, 8);
w.write_bits(0xC0, 8);
w.write_bits(30, 8);
w.write_ue(0);
w.write_ue(0);
w.write_ue(2);
w.write_ue(1);
w.write_bit(false);
w.write_ue(10);
w.write_ue(8);
w.write_bit(true); // frame_mbs_only
w.write_bit(true); // direct_8x8
w.write_bit(false); // cropping
w.write_bit(false); // vui
w.rbsp_trailing();
let sps = Sps::parse(&w.finish()).unwrap();
assert_eq!((sps.width(), sps.height()), (176, 144));
assert_eq!(sps.pic_order_cnt_type, 2);
assert_eq!(sps.crop_rect(), (0, 0, 176, 144));
}
/// The baseline SPS of `parse_minimal_baseline_sps` with a chosen width and optional cropping.
fn sps_with(width_minus1: u32, crop: Option<[u32; 4]>) -> Vec<u8> {
let mut w = BitWriter::new();
w.write_bits(66, 8);
w.write_bits(0xC0, 8);
w.write_bits(30, 8);
w.write_ue(0);
w.write_ue(0);
w.write_ue(2);
w.write_ue(1);
w.write_bit(false);
w.write_ue(width_minus1);
w.write_ue(8);
w.write_bit(true); // frame_mbs_only
w.write_bit(true); // direct_8x8
w.write_bit(crop.is_some());
if let Some(c) = crop {
c.iter().for_each(|&v| w.write_ue(v));
}
w.write_bit(false); // vui
w.rbsp_trailing();
w.finish()
}
#[test]
fn hostile_sps_values_are_errors_not_overflows() {
// pic_width_in_mbs_minus1 = u32::MAX: `+ 1` used to overflow (wrapping to 0 in release).
assert!(Sps::parse(&sps_with(u32::MAX, None)).is_err());
// Crop offsets whose sum or product exceed u32.
assert!(Sps::parse(&sps_with(10, Some([u32::MAX - 1, u32::MAX - 1, 0, 0]))).is_err());
assert!(Sps::parse(&sps_with(10, Some([0x8000_0000, 0, 0, 0]))).is_err());
assert!(Sps::parse(&sps_with(10, Some([1, 1, 1, 1]))).is_ok());
}
fn pps_bits(qp_minus26: i32, qs_minus26: i32) -> Vec<u8> {
let mut w = BitWriter::new();
w.write_ue(0);
w.write_ue(0);
w.write_bit(false);
w.write_bit(false);
w.write_ue(0);
w.write_ue(0);
w.write_ue(0);
w.write_bit(false);
w.write_bits(0, 2);
w.write_se(qp_minus26);
w.write_se(qs_minus26);
w.write_se(0);
w.write_bit(false);
w.write_bit(false);
w.write_bit(false);
w.rbsp_trailing();
w.finish()
}
fn sps_table() -> Vec<Option<Sps>> {
vec![Sps::parse(&sps_with(10, None)).ok()]
}
#[test]
fn pic_init_qp_is_checked_before_adding_26() {
let table = sps_table();
assert_eq!(Pps::parse(&pps_bits(0, 0), &table).unwrap().pic_init_qp, 26);
assert_eq!(Pps::parse(&pps_bits(25, 0), &table).unwrap().pic_init_qp, 51);
assert_eq!(Pps::parse(&pps_bits(-26, 0), &table).unwrap().pic_init_qp, 0);
assert!(Pps::parse(&pps_bits(26, 0), &table).is_err());
assert!(Pps::parse(&pps_bits(-27, 0), &table).is_err());
assert!(Pps::parse(&pps_bits(i32::MAX, 0), &table).is_err());
assert!(Pps::parse(&pps_bits(0, i32::MAX), &table).is_err());
}
#[test]
fn slice_qp_addition_does_not_overflow() {
let table = sps_table();
let pps = Pps::parse(&pps_bits(0, 0), &table).unwrap();
let sps = table[0].clone().unwrap();
let nal = crate::slice::NalHeader { nal_ref_idc: 0, nal_unit_type: crate::slice::nal_type::SLICE };
let rbsp = |delta: i32| {
let mut w = BitWriter::new();
w.write_ue(0);
w.write_ue(2); // I
w.write_ue(0);
w.write_bits(0, sps.log2_max_frame_num);
w.write_se(delta);
w.rbsp_trailing();
w.finish()
};
let parse = |delta: i32| crate::slice::SliceHeader::parse(&rbsp(delta), nal, |_| Ok((&pps, table[0].as_ref().unwrap())));
let (sh, _, _) = parse(0).unwrap();
assert_eq!(sh.qp(&pps), 26);
assert!(parse(26).is_err());
assert!(parse(i32::MAX).is_err());
}
}
-343
View File
@@ -1,343 +0,0 @@
//! Picture buffers and per-macroblock state.
use std::sync::{Arc, OnceLock};
/// Planar 4:2:0 8-bit sample buffers (MB-aligned dimensions, stride == width).
#[derive(Clone)]
pub struct Planes {
pub y: Vec<u8>,
pub cb: Vec<u8>,
pub cr: Vec<u8>,
pub width: usize,
pub height: usize,
pub cwidth: usize,
}
impl Planes {
pub fn new(width: usize, height: usize) -> Self {
let (cw, ch) = (width / 2, height / 2);
Planes { y: vec![0; width * height], cb: vec![128; cw * ch], cr: vec![128; cw * ch], width, height, cwidth: cw }
}
pub fn gray(width: usize, height: usize) -> Self {
let mut p = Self::new(width, height);
p.y.fill(128);
p
}
}
/// Macroblock coding category.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum MbKind {
#[default]
None,
I4x4,
I8x8,
I16x16,
IPcm,
PSkip,
BSkip,
BDirect16x16,
/// Any other P or B inter macroblock.
Inter,
}
impl MbKind {
#[inline]
pub fn is_intra(self) -> bool {
matches!(self, MbKind::I4x4 | MbKind::I8x8 | MbKind::I16x16 | MbKind::IPcm)
}
}
/// Per-macroblock state kept for the whole picture (neighbour derivations, deblocking, co-located data).
#[derive(Clone, Copy)]
pub struct MbState {
/// Slice number within the picture; `u32::MAX` = not (yet) decoded.
pub slice_num: u32,
pub kind: MbKind,
pub transform_8x8: bool,
/// coded_block_pattern: bits 0..3 luma, bits 4..5 chroma.
pub cbp: u8,
pub qp: u8,
/// QPc for Cb / Cr (used by deblocking).
pub qpc: [u8; 2],
pub intra_chroma_mode: u8,
/// Intra4x4PredMode / Intra8x8PredMode per 4x4 block (raster order).
pub intra_modes: [u8; 16],
/// CAVLC: TotalCoeff per luma 4x4 block (raster). CABAC: coded_block_flag.
pub nnz: [u8; 16],
/// Same for chroma AC blocks [Cb, Cr][raster 2x2].
pub nnz_c: [[u8; 4]; 2],
/// CABAC coded_block_flag of DC blocks: bit0 luma (I16x16), bit1 Cb, bit2 Cr.
pub cbf_dc: u8,
/// Luma 4x4 blocks (raster bit) with non-zero coefficients, for deblocking bS = 2.
pub nz_mask: u16,
pub ref_idx: [[i8; 4]; 2],
/// Motion vectors per 4x4 block (raster order).
pub mv: [[[i16; 2]; 16]; 2],
/// |mvd| per 4x4 block (raster), saturated, for CABAC context selection.
pub mvd: [[[u8; 2]; 16]; 2],
/// Bit per 8x8 block predicted in direct mode.
pub direct8x8: u8,
}
impl MbState {
#[inline]
pub fn kind_is_skip(&self) -> bool {
matches!(self.kind, MbKind::PSkip | MbKind::BSkip)
}
}
impl Default for MbState {
fn default() -> Self {
MbState {
slice_num: u32::MAX,
kind: MbKind::None,
transform_8x8: false,
cbp: 0,
qp: 0,
qpc: [0; 2],
intra_chroma_mode: 0,
intra_modes: [2; 16],
nnz: [0; 16],
nnz_c: [[0; 4]; 2],
cbf_dc: 0,
nz_mask: 0,
ref_idx: [[-1; 4]; 2],
mv: [[[0; 2]; 16]; 2],
mvd: [[[0; 2]; 16]; 2],
direct8x8: 0,
}
}
}
/// One published macroblock row of a decoded frame: final (deblocked) samples plus the motion data
/// needed as co-located information for direct prediction.
pub struct FrameRow {
/// 16 luma lines.
pub y: Box<[u8]>,
/// 8 lines per chroma component.
pub cb: Box<[u8]>,
pub cr: Box<[u8]>,
/// Per 4x4 block: index mb_x * 16 + raster.
pub mv: [Box<[[i16; 2]]>; 2],
/// Per 8x8 block: index mb_x * 4 + b8.
pub ref_idx: [Box<[i8]>; 2],
/// Unique id of the referenced picture per 8x8 block (u32::MAX = none).
pub ref_id: [Box<[u32]>; 2],
pub intra: Box<[bool]>,
}
/// A decoded (or in-progress) frame as stored in the DPB and referenced by later pictures.
///
/// Rows are published in order by the thread decoding the picture; readers block (per row) until the
/// data they need is available, which lets several pictures decode concurrently.
pub struct Frame {
pub id: u32,
pub poc: i32,
pub width: usize,
pub height: usize,
pub cwidth: usize,
pub cheight: usize,
pub mb_w: usize,
rows: Box<[OnceLock<FrameRow>]>,
}
impl Frame {
pub fn new(id: u32, poc: i32, mb_w: usize, mb_h: usize) -> Self {
Frame {
id,
poc,
width: mb_w * 16,
height: mb_h * 16,
cwidth: mb_w * 8,
cheight: mb_h * 8,
mb_w,
rows: (0..mb_h).map(|_| OnceLock::new()).collect(),
}
}
pub fn mb_h(&self) -> usize {
self.rows.len()
}
/// Publish MB row `r` (ignored if already published).
pub fn publish(&self, r: usize, row: FrameRow) {
let _ = self.rows[r].set(row);
}
pub fn is_published(&self, r: usize) -> bool {
self.rows[r].get().is_some()
}
/// MB row `r`, waiting until it has been published.
#[inline]
pub fn row(&self, r: usize) -> &FrameRow {
#[cfg(feature = "threads")]
{
self.rows[r].wait()
}
#[cfg(not(feature = "threads"))]
{
self.rows[r].get().expect("reference row decoded before use")
}
}
/// Wait until the whole frame is available.
pub fn wait_complete(&self) {
for r in 0..self.rows.len() {
self.row(r);
}
}
/// Build a published row from a contiguous plane set (rows of MB row `r`) and motion data.
pub fn make_row(planes: &Planes, r: usize, mbs: &[MbState], ref_ids: &dyn Fn(&MbState, usize, i8) -> u32) -> FrameRow {
let w = planes.width;
let cw = planes.cwidth;
let mb_w = w / 16;
let y = planes.y[r * 16 * w..(r + 1) * 16 * w].into();
let cb = planes.cb[r * 8 * cw..(r + 1) * 8 * cw].into();
let cr = planes.cr[r * 8 * cw..(r + 1) * 8 * cw].into();
let row_mbs = &mbs[r * mb_w..(r + 1) * mb_w];
let mut mv = [vec![[0i16; 2]; mb_w * 16], vec![[0i16; 2]; mb_w * 16]];
let mut ref_idx = [vec![-1i8; mb_w * 4], vec![-1i8; mb_w * 4]];
let mut ref_id = [vec![u32::MAX; mb_w * 4], vec![u32::MAX; mb_w * 4]];
let mut intra = vec![true; mb_w];
for (x, st) in row_mbs.iter().enumerate() {
if st.slice_num == u32::MAX {
continue;
}
intra[x] = st.kind.is_intra();
for l in 0..2 {
mv[l][x * 16..x * 16 + 16].copy_from_slice(&st.mv[l]);
for b in 0..4 {
let ri = st.ref_idx[l][b];
ref_idx[l][x * 4 + b] = ri;
if ri >= 0 {
ref_id[l][x * 4 + b] = ref_ids(st, l, ri);
}
}
}
}
let [mv0, mv1] = mv;
let [ri0, ri1] = ref_idx;
let [id0, id1] = ref_id;
FrameRow { y, cb, cr, mv: [mv0.into(), mv1.into()], ref_idx: [ri0.into(), ri1.into()], ref_id: [id0.into(), id1.into()], intra: intra.into() }
}
/// A frame with every row published from `planes` and no motion (intra), e.g. "non-existing"
/// frames inferred for frame_num gaps.
pub fn from_planes(id: u32, poc: i32, planes: &Planes) -> Self {
let (mb_w, mb_h) = (planes.width / 16, planes.height / 16);
let f = Frame::new(id, poc, mb_w, mb_h);
let blank = vec![MbState::default(); mb_w * mb_h];
for r in 0..mb_h {
f.publish(r, Frame::make_row(planes, r, &blank, &|_, _, _| u32::MAX));
}
f
}
/// Copy the w x h luma window at (x0, y0) (clamped to the picture) into `out` (stride `os`).
#[inline]
pub fn luma_window(&self, x0: i32, y0: i32, w: usize, h: usize, out: &mut [u8], os: usize) {
let (width, height) = (self.width, self.height);
for r in 0..h {
let yy = (y0 + r as i32).clamp(0, height as i32 - 1) as usize;
let row = self.row(yy >> 4);
let line = &row.y[(yy & 15) * width..(yy & 15) * width + width];
copy_line::<24>(line, x0, &mut out[r * os..], w);
}
}
/// Copy the w x h window of chroma component `c` at (x0, y0) (clamped) into `out`.
#[inline]
pub fn chroma_window(&self, c: usize, (x0, y0): (i32, i32), (w, h): (usize, usize), out: &mut [u8], os: usize) {
let (cw, ch) = (self.cwidth, self.cheight);
for r in 0..h {
let yy = (y0 + r as i32).clamp(0, ch as i32 - 1) as usize;
let row = self.row(yy >> 3);
let plane = if c == 0 { &row.cb } else { &row.cr };
let line = &plane[(yy & 7) * cw..(yy & 7) * cw + cw];
copy_line::<16>(line, x0, &mut out[r * os..], w);
}
}
/// Crop and copy the frame into planar buffers (waits for completion).
pub fn copy_cropped(&self, crop: (usize, usize, usize, usize)) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
let (cx, cy, cw, ch) = crop;
let alloc = plane_allocator();
let mut y = alloc(cw * ch);
for r in cy..cy + ch {
let row = self.row(r >> 4);
y.extend_from_slice(&row.y[(r & 15) * self.width + cx..(r & 15) * self.width + cx + cw]);
}
let (ccx, ccy, ccw, cch) = (cx / 2, cy / 2, cw.div_ceil(2), ch.div_ceil(2));
let mut u = alloc(ccw * cch);
let mut v = alloc(ccw * cch);
for r in ccy..ccy + cch {
let row = self.row(r >> 3);
let o = (r & 7) * self.cwidth + ccx;
u.extend_from_slice(&row.cb[o..o + ccw]);
v.extend_from_slice(&row.cr[o..o + ccw]);
}
(y, u, v)
}
}
static PLANE_ALLOCATOR: std::sync::OnceLock<fn(usize) -> Vec<u8>> = std::sync::OnceLock::new();
/// Where output planes come from: an empty buffer with room for the given number of samples.
/// The host sets it once to recycle the planes of pictures it is done with; the first call wins.
pub fn set_plane_allocator(alloc: fn(usize) -> Vec<u8>) {
let _ = PLANE_ALLOCATOR.set(alloc);
}
fn plane_allocator() -> fn(usize) -> Vec<u8> {
PLANE_ALLOCATOR.get().copied().unwrap_or(Vec::with_capacity)
}
/// Copy `w` samples of `line` starting at x0 into `out`; when possible a fixed-size block of `N`
/// bytes is copied instead (cheaper than a variable-length copy; `out` must then have room for `N`).
#[inline(always)]
fn copy_line<const N: usize>(line: &[u8], x0: i32, out: &mut [u8], w: usize) {
if w <= N
&& x0 >= 0
&& let Some(src) = line.get(x0 as usize..).and_then(<[_]>::first_chunk::<N>)
&& let Some(dst) = out.first_chunk_mut::<N>()
{
*dst = *src;
} else {
copy_clamped(line, x0, &mut out[..w]);
}
}
/// Copy `out.len()` samples of `line` starting at x0, replicating edge samples outside the line.
#[inline(always)]
fn copy_clamped(line: &[u8], x0: i32, out: &mut [u8]) {
let w = out.len();
if x0 >= 0 && x0 as usize + w <= line.len() {
out.copy_from_slice(&line[x0 as usize..x0 as usize + w]);
} else {
let max = line.len() as i32 - 1;
for (c, o) in out.iter_mut().enumerate() {
*o = line[(x0 + c as i32).clamp(0, max) as usize];
}
}
}
pub type FrameRef = Arc<Frame>;
/// An entry of RefPicList0/1 for the current slice.
#[derive(Clone)]
pub struct RefPic {
pub frame: FrameRef,
pub long_term: bool,
}
impl RefPic {
pub fn poc(&self) -> i32 {
self.frame.poc
}
pub fn id(&self) -> u32 {
self.frame.id
}
}
-529
View File
@@ -1,529 +0,0 @@
//! NAL unit header and slice header parsing (7.3.1, 7.3.3).
use crate::error::{Result, ensure};
use crate::params::{Pps, Sps};
use deckcraft_bitstream::BitReader;
/// NAL unit types used by the decoder.
pub mod nal_type {
pub const SLICE: u8 = 1;
pub const SLICE_DPA: u8 = 2;
pub const SLICE_DPB: u8 = 3;
pub const SLICE_DPC: u8 = 4;
pub const IDR: u8 = 5;
pub const SEI: u8 = 6;
pub const SPS: u8 = 7;
pub const PPS: u8 = 8;
pub const AUD: u8 = 9;
pub const END_SEQ: u8 = 10;
pub const END_STREAM: u8 = 11;
pub const FILLER: u8 = 12;
pub const SPS_EXT: u8 = 13;
pub const PREFIX: u8 = 14;
pub const SUBSET_SPS: u8 = 15;
pub const AUX_SLICE: u8 = 19;
pub const SLICE_EXT: u8 = 20;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct NalHeader {
pub nal_ref_idc: u8,
pub nal_unit_type: u8,
}
impl NalHeader {
pub fn parse(b: u8) -> Result<Self> {
ensure!(b & 0x80 == 0, "forbidden_zero_bit set");
Ok(Self { nal_ref_idc: (b >> 5) & 3, nal_unit_type: b & 0x1f })
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SliceType {
P,
B,
I,
Sp,
Si,
}
impl SliceType {
fn from_raw(v: u32) -> Result<Self> {
Ok(match v % 5 {
0 => SliceType::P,
1 => SliceType::B,
2 => SliceType::I,
3 => SliceType::Sp,
_ => SliceType::Si,
})
}
pub fn is_intra(self) -> bool {
matches!(self, SliceType::I | SliceType::Si)
}
pub fn is_b(self) -> bool {
self == SliceType::B
}
pub fn is_p(self) -> bool {
matches!(self, SliceType::P | SliceType::Sp)
}
}
/// One entry of ref_pic_list_modification().
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RefPicListMod {
pub idc: u32,
/// abs_diff_pic_num_minus1 or long_term_pic_num.
pub value: u32,
}
/// memory_management_control_operation entry.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Mmco {
pub op: u32,
pub difference_of_pic_nums_minus1: u32,
pub long_term_pic_num: u32,
pub long_term_frame_idx: u32,
pub max_long_term_frame_idx_plus1: u32,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct WeightEntry {
pub luma_weight: i32,
pub luma_offset: i32,
pub luma_flag: bool,
pub chroma_weight: [i32; 2],
pub chroma_offset: [i32; 2],
pub chroma_flag: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PredWeightTable {
pub luma_log2_denom: u32,
pub chroma_log2_denom: u32,
pub l0: Vec<WeightEntry>,
pub l1: Vec<WeightEntry>,
}
#[derive(Clone, Debug)]
pub struct SliceHeader {
pub nal_ref_idc: u8,
pub nal_unit_type: u8,
pub idr: bool,
pub first_mb_in_slice: u32,
pub slice_type_raw: u32,
pub slice_type: SliceType,
pub pps_id: u32,
pub colour_plane_id: u32,
pub frame_num: u32,
pub field_pic: bool,
pub bottom_field: bool,
pub idr_pic_id: u32,
pub pic_order_cnt_lsb: u32,
pub delta_pic_order_cnt_bottom: i32,
pub delta_pic_order_cnt: [i32; 2],
pub redundant_pic_cnt: u32,
pub direct_spatial_mv_pred: bool,
pub num_ref_idx_active: [u32; 2],
pub ref_pic_list_mod: [Vec<RefPicListMod>; 2],
pub pred_weight_table: Option<PredWeightTable>,
pub no_output_of_prior_pics: bool,
pub long_term_reference: bool,
pub adaptive_ref_pic_marking: bool,
pub mmcos: Vec<Mmco>,
pub cabac_init_idc: u32,
pub slice_qp_delta: i32,
pub sp_for_switch: bool,
pub slice_qs_delta: i32,
pub disable_deblocking_filter_idc: u32,
pub slice_alpha_c0_offset_div2: i32,
pub slice_beta_offset_div2: i32,
pub slice_group_change_cycle: u32,
/// Bit offset of slice_data() within the RBSP.
pub header_bits: usize,
}
/// ref_pic_list_modification() for list 0 and list 1.
fn read_ref_pic_list_mods(r: &mut BitReader, slice_type: SliceType) -> Result<[Vec<RefPicListMod>; 2]> {
let mut ref_pic_list_mod = [Vec::new(), Vec::new()];
for (list, mods) in ref_pic_list_mod.iter_mut().enumerate() {
let present = if list == 0 { !slice_type.is_intra() } else { slice_type.is_b() };
if present && r.read_flag()? {
loop {
let idc = r.read_ue()?;
ensure!(idc <= 5, "modification_of_pic_nums_idc out of range");
if idc == 3 {
break;
}
let value = r.read_ue()?;
mods.push(RefPicListMod { idc, value });
ensure!(mods.len() <= 66, "too many ref_pic_list_modification entries");
}
}
}
Ok(ref_pic_list_mod)
}
/// pred_weight_table().
fn read_pred_weight_table(r: &mut BitReader, sps: &Sps, slice_type: SliceType, num_ref_idx_active: [u32; 2]) -> Result<PredWeightTable> {
let luma_log2_denom = r.read_ue()?;
ensure!(luma_log2_denom <= 7, "luma_log2_weight_denom out of range");
let has_chroma = sps.chroma_array_type() != 0;
let mut chroma_log2_denom = 0;
if has_chroma {
chroma_log2_denom = r.read_ue()?;
ensure!(chroma_log2_denom <= 7, "chroma_log2_weight_denom out of range");
}
let mut lists = [Vec::new(), Vec::new()];
let nlists = if slice_type.is_b() { 2 } else { 1 };
for (l, entries) in lists.iter_mut().enumerate().take(nlists) {
for _ in 0..num_ref_idx_active[l] {
entries.push(read_weight_entry(r, has_chroma, luma_log2_denom, chroma_log2_denom)?);
}
}
let [l0, l1] = lists;
Ok(PredWeightTable { luma_log2_denom, chroma_log2_denom, l0, l1 })
}
fn read_weight_entry(r: &mut BitReader, has_chroma: bool, luma_log2_denom: u32, chroma_log2_denom: u32) -> Result<WeightEntry> {
let mut e = WeightEntry { luma_weight: 1 << luma_log2_denom, chroma_weight: [1 << chroma_log2_denom; 2], ..Default::default() };
if r.read_flag()? {
e.luma_flag = true;
e.luma_weight = r.read_se()?;
e.luma_offset = r.read_se()?;
ensure!((-128..=127).contains(&e.luma_weight) && (-128..=127).contains(&e.luma_offset), "luma weight out of range");
}
if has_chroma && r.read_flag()? {
e.chroma_flag = true;
for j in 0..2 {
e.chroma_weight[j] = r.read_se()?;
e.chroma_offset[j] = r.read_se()?;
ensure!((-128..=127).contains(&e.chroma_weight[j]) && (-128..=127).contains(&e.chroma_offset[j]), "chroma weight out of range");
}
}
Ok(e)
}
/// The memory_management_control_operation list of dec_ref_pic_marking().
fn read_mmcos(r: &mut BitReader) -> Result<Vec<Mmco>> {
let mut mmcos = Vec::new();
loop {
let op = r.read_ue()?;
ensure!(op <= 6, "memory_management_control_operation out of range");
if op == 0 {
return Ok(mmcos);
}
let mut m = Mmco { op, difference_of_pic_nums_minus1: 0, long_term_pic_num: 0, long_term_frame_idx: 0, max_long_term_frame_idx_plus1: 0 };
if op == 1 || op == 3 {
m.difference_of_pic_nums_minus1 = r.read_ue()?;
}
if op == 2 {
m.long_term_pic_num = r.read_ue()?;
}
if op == 3 || op == 6 {
m.long_term_frame_idx = r.read_ue()?;
}
if op == 4 {
m.max_long_term_frame_idx_plus1 = r.read_ue()?;
}
mmcos.push(m);
ensure!(mmcos.len() <= 66, "too many MMCOs");
}
}
/// Deblocking filter control: (disable_deblocking_filter_idc, alpha_c0_offset_div2, beta_offset_div2).
fn read_deblocking_params(r: &mut BitReader) -> Result<(u32, i32, i32)> {
let disable_deblocking_filter_idc = r.read_ue()?;
ensure!(disable_deblocking_filter_idc <= 2, "disable_deblocking_filter_idc out of range");
if disable_deblocking_filter_idc == 1 {
return Ok((disable_deblocking_filter_idc, 0, 0));
}
let alpha = r.read_se()?;
let beta = r.read_se()?;
ensure!((-6..=6).contains(&alpha) && (-6..=6).contains(&beta), "deblocking offsets out of range");
Ok((disable_deblocking_filter_idc, alpha, beta))
}
impl SliceHeader {
pub fn qp(&self, pps: &Pps) -> i32 {
pps.pic_init_qp.saturating_add(self.slice_qp_delta)
}
pub fn has_mmco5(&self) -> bool {
self.mmcos.iter().any(|m| m.op == 5)
}
/// Parse the slice header. `pps_lookup` resolves pic_parameter_set_id to (PPS, SPS).
pub fn parse<'a>(
rbsp: &[u8],
nal: NalHeader,
pps_lookup: impl FnOnce(u32) -> Result<(&'a Pps, &'a Sps)>,
) -> Result<(SliceHeader, &'a Pps, &'a Sps)> {
let mut r = BitReader::new(rbsp);
let idr = nal.nal_unit_type == nal_type::IDR;
let first_mb_in_slice = r.read_ue()?;
let slice_type_raw = r.read_ue()?;
ensure!(slice_type_raw <= 9, "slice_type out of range");
let slice_type = SliceType::from_raw(slice_type_raw)?;
let pps_id = r.read_ue()?;
ensure!(pps_id < 256, "pic_parameter_set_id out of range");
let (pps, sps) = pps_lookup(pps_id)?;
let mut colour_plane_id = 0;
if sps.separate_colour_plane {
colour_plane_id = r.read_bits(2)?;
}
let frame_num = r.read_bits(sps.log2_max_frame_num)?;
let mut field_pic = false;
let mut bottom_field = false;
if !sps.frame_mbs_only {
field_pic = r.read_flag()?;
if field_pic {
bottom_field = r.read_flag()?;
}
}
let idr_pic_id = if idr { r.read_ue()? } else { 0 };
let mut pic_order_cnt_lsb = 0;
let mut delta_pic_order_cnt_bottom = 0;
let mut delta_pic_order_cnt = [0; 2];
if sps.pic_order_cnt_type == 0 {
pic_order_cnt_lsb = r.read_bits(sps.log2_max_poc_lsb)?;
if pps.bottom_field_pic_order_in_frame_present && !field_pic {
delta_pic_order_cnt_bottom = r.read_se()?;
}
}
if sps.pic_order_cnt_type == 1 && !sps.delta_pic_order_always_zero {
delta_pic_order_cnt[0] = r.read_se()?;
if pps.bottom_field_pic_order_in_frame_present && !field_pic {
delta_pic_order_cnt[1] = r.read_se()?;
}
}
let redundant_pic_cnt = if pps.redundant_pic_cnt_present { r.read_ue()? } else { 0 };
let direct_spatial_mv_pred = if slice_type.is_b() { r.read_flag()? } else { false };
let mut num_ref_idx_active = [pps.num_ref_idx_l0_default_active, pps.num_ref_idx_l1_default_active];
if !slice_type.is_intra() {
if r.read_flag()? {
num_ref_idx_active[0] = r.read_ue_plus(1)?;
if slice_type.is_b() {
num_ref_idx_active[1] = r.read_ue_plus(1)?;
}
}
let max = if field_pic { 32 } else { 16 };
ensure!(num_ref_idx_active[0] <= max && num_ref_idx_active[1] <= max, "num_ref_idx_active out of range");
}
if !slice_type.is_b() {
num_ref_idx_active[1] = 0;
}
if slice_type.is_intra() {
num_ref_idx_active[0] = 0;
}
ensure!(nal.nal_unit_type != 20 && nal.nal_unit_type != 21, "MVC slices are not supported");
let ref_pic_list_mod = read_ref_pic_list_mods(&mut r, slice_type)?;
let pred_weight_table = if (pps.weighted_pred && slice_type.is_p()) || (pps.weighted_bipred_idc == 1 && slice_type.is_b()) {
Some(read_pred_weight_table(&mut r, sps, slice_type, num_ref_idx_active)?)
} else {
None
};
let mut no_output_of_prior_pics = false;
let mut long_term_reference = false;
let mut adaptive_ref_pic_marking = false;
let mut mmcos = Vec::new();
if nal.nal_ref_idc != 0 {
if idr {
no_output_of_prior_pics = r.read_flag()?;
long_term_reference = r.read_flag()?;
} else {
adaptive_ref_pic_marking = r.read_flag()?;
if adaptive_ref_pic_marking {
mmcos = read_mmcos(&mut r)?;
}
}
}
let mut cabac_init_idc = 0;
if pps.entropy_coding_mode && !slice_type.is_intra() {
cabac_init_idc = r.read_ue()?;
ensure!(cabac_init_idc <= 2, "cabac_init_idc out of range");
}
let slice_qp_delta = r.read_se()?;
let qp = pps.pic_init_qp.checked_add(slice_qp_delta).ok_or_else(|| crate::Error::Invalid("slice QP out of range".into()))?;
ensure!((0..=51).contains(&qp), "slice QP {qp} out of range");
let mut sp_for_switch = false;
let mut slice_qs_delta = 0;
if matches!(slice_type, SliceType::Sp | SliceType::Si) {
if slice_type == SliceType::Sp {
sp_for_switch = r.read_flag()?;
}
slice_qs_delta = r.read_se()?;
}
let (disable_deblocking_filter_idc, slice_alpha_c0_offset_div2, slice_beta_offset_div2) =
if pps.deblocking_filter_control_present { read_deblocking_params(&mut r)? } else { (0, 0, 0) };
let mut slice_group_change_cycle = 0;
if pps.num_slice_groups > 1 && (3..=5).contains(&pps.slice_group_map_type) {
// Ceil(Log2(PicSizeInMapUnits / SliceGroupChangeRate + 1)); rate is not retained, so read
// conservatively using the picture size (FMO is unsupported for decoding anyway).
let pic_size = sps.pic_width_in_mbs * sps.pic_height_in_map_units;
let bits = 32 - pic_size.leading_zeros();
slice_group_change_cycle = r.read_bits(bits)?;
}
let header_bits = r.position();
Ok((
SliceHeader {
nal_ref_idc: nal.nal_ref_idc,
nal_unit_type: nal.nal_unit_type,
idr,
first_mb_in_slice,
slice_type_raw,
slice_type,
pps_id,
colour_plane_id,
frame_num,
field_pic,
bottom_field,
idr_pic_id,
pic_order_cnt_lsb,
delta_pic_order_cnt_bottom,
delta_pic_order_cnt,
redundant_pic_cnt,
direct_spatial_mv_pred,
num_ref_idx_active,
ref_pic_list_mod,
pred_weight_table,
no_output_of_prior_pics,
long_term_reference,
adaptive_ref_pic_marking,
mmcos,
cabac_init_idc,
slice_qp_delta,
sp_for_switch,
slice_qs_delta,
disable_deblocking_filter_idc,
slice_alpha_c0_offset_div2,
slice_beta_offset_div2,
slice_group_change_cycle,
header_bits,
},
pps,
sps,
))
}
}
/// POC decoding state carried between pictures (8.2.1).
#[derive(Clone, Debug, Default)]
pub struct PocState {
/// PicOrderCntMsb / pic_order_cnt_lsb of the previous reference picture (type 0).
prev_ref_msb: i32,
prev_ref_lsb: i32,
/// FrameNumOffset / frame_num of the previous picture (types 1 and 2).
prev_frame_num_offset: i32,
prev_frame_num: u32,
}
/// Output of POC computation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Poc {
pub top: i32,
pub bottom: i32,
msb: i32,
frame_num_offset: i32,
}
impl Poc {
pub fn frame(&self) -> i32 {
self.top.min(self.bottom)
}
}
impl PocState {
/// Compute TopFieldOrderCnt/BottomFieldOrderCnt of a frame (8.2.1.1-8.2.1.3).
pub fn compute(&self, sh: &SliceHeader, sps: &Sps) -> Poc {
match sps.pic_order_cnt_type {
0 => {
let (prev_msb, prev_lsb) = if sh.idr { (0, 0) } else { (self.prev_ref_msb, self.prev_ref_lsb) };
let max_lsb = 1i32 << sps.log2_max_poc_lsb;
let lsb = sh.pic_order_cnt_lsb as i32;
let msb = if lsb < prev_lsb && prev_lsb - lsb >= max_lsb / 2 {
prev_msb + max_lsb
} else if lsb > prev_lsb && lsb - prev_lsb > max_lsb / 2 {
prev_msb - max_lsb
} else {
prev_msb
};
let top = msb + lsb;
let bottom = top + sh.delta_pic_order_cnt_bottom;
Poc { top, bottom, msb, frame_num_offset: 0 }
}
1 => {
let frame_num_offset = self.frame_num_offset(sh.idr, sh.frame_num, sps);
let n = sps.offset_for_ref_frame.len() as i32;
let mut abs_frame_num = if n != 0 { frame_num_offset + sh.frame_num as i32 } else { 0 };
if sh.nal_ref_idc == 0 && abs_frame_num > 0 {
abs_frame_num -= 1;
}
let mut expected = 0i32;
if abs_frame_num > 0 {
let cycle_cnt = (abs_frame_num - 1) / n;
let in_cycle = (abs_frame_num - 1) % n;
let delta_cycle: i32 = sps.offset_for_ref_frame.iter().sum();
expected = cycle_cnt.wrapping_mul(delta_cycle);
for &o in &sps.offset_for_ref_frame[..=in_cycle as usize] {
expected += o;
}
}
if sh.nal_ref_idc == 0 {
expected += sps.offset_for_non_ref_pic;
}
let top = expected + sh.delta_pic_order_cnt[0];
let bottom = top + sps.offset_for_top_to_bottom_field + sh.delta_pic_order_cnt[1];
Poc { top, bottom, msb: 0, frame_num_offset }
}
_ => {
let frame_num_offset = self.frame_num_offset(sh.idr, sh.frame_num, sps);
let temp = if sh.idr {
0
} else if sh.nal_ref_idc == 0 {
2 * (frame_num_offset + sh.frame_num as i32) - 1
} else {
2 * (frame_num_offset + sh.frame_num as i32)
};
Poc { top: temp, bottom: temp, msb: 0, frame_num_offset }
}
}
}
fn frame_num_offset(&self, idr: bool, frame_num: u32, sps: &Sps) -> i32 {
if idr {
0
} else if self.prev_frame_num > frame_num {
self.prev_frame_num_offset + sps.max_frame_num() as i32
} else {
self.prev_frame_num_offset
}
}
/// Update after a picture has been decoded. `poc` is the value returned by [`PocState::compute`]
/// (before any MMCO5 adjustment).
pub fn update(&mut self, sh: &SliceHeader, poc: &Poc) {
let mmco5 = sh.has_mmco5();
if sh.nal_ref_idc != 0 {
if mmco5 {
self.prev_ref_msb = 0;
self.prev_ref_lsb = poc.top - poc.frame();
} else {
self.prev_ref_msb = poc.msb;
self.prev_ref_lsb = sh.pic_order_cnt_lsb as i32;
}
}
if mmco5 {
self.prev_frame_num_offset = 0;
self.prev_frame_num = 0;
} else {
self.prev_frame_num_offset = poc.frame_num_offset;
self.prev_frame_num = sh.frame_num;
}
}
/// Account for a "non-existing" frame inferred by the frame_num gap process (8.2.5.2).
pub fn update_gap_frame(&mut self, frame_num: u32, sps: &Sps) {
let off = self.frame_num_offset(false, frame_num, sps);
self.prev_frame_num_offset = off;
self.prev_frame_num = frame_num;
}
}
File diff suppressed because it is too large Load Diff
-662
View File
@@ -1,662 +0,0 @@
//! Hand-built bitstreams exercising features libx264 never emits: I_PCM (CAVLC and CABAC), long-term
//! references, MMCO operations, frame_num gaps and reference list modification. Every picture consists
//! of I_PCM and P_Skip macroblocks only, so the expected output is known exactly (I_PCM samples are
//! lossless, P_Skip copies the selected reference with a zero motion vector, and deblocking cannot
//! modify samples at these QPs).
use crate::Decoder;
use crate::cabac::{Cabac, NEXT_STATE, RANGE_TAB_LPS};
use deckcraft_bitstream::{BitWriter, escape_rbsp};
const MB_W: usize = 4;
const MB_H: usize = 3;
const NMB: usize = MB_W * MB_H;
fn nal(ref_idc: u8, t: u8, rbsp: &[u8]) -> Vec<u8> {
let mut v = vec![0, 0, 0, 1, (ref_idc << 5) | t];
v.extend(escape_rbsp(rbsp));
v
}
/// POC type used by the synthetic SPS: 0 (4-bit lsb), 1 (see below) or 2.
#[derive(Clone, Copy, PartialEq, Eq)]
enum PocType {
T0,
/// offset_for_non_ref_pic = -3, one reference frame per cycle with offset 4
T1,
T2,
}
fn sps(profile: u8, max_refs: u32, gaps: bool, poc: PocType) -> Vec<u8> {
let mut w = BitWriter::new();
w.write_bits(profile as u32, 8);
w.write_bits(0, 8);
w.write_bits(30, 8);
w.write_ue(0); // sps id
w.write_ue(0); // log2_max_frame_num - 4 -> 16
match poc {
PocType::T0 => {
w.write_ue(0);
w.write_ue(0); // log2_max_pic_order_cnt_lsb - 4 -> 4-bit lsb
}
PocType::T1 => {
w.write_ue(1);
w.write_bit(false); // delta_pic_order_always_zero_flag
w.write_se(-3); // offset_for_non_ref_pic
w.write_se(0); // offset_for_top_to_bottom_field
w.write_ue(1); // num_ref_frames_in_pic_order_cnt_cycle
w.write_se(4);
}
PocType::T2 => w.write_ue(2), // output order = decoding order
}
w.write_ue(max_refs);
w.write_bit(gaps);
w.write_ue(MB_W as u32 - 1);
w.write_ue(MB_H as u32 - 1);
w.write_bit(true); // frame_mbs_only
w.write_bit(true); // direct_8x8_inference
w.write_bit(false); // cropping
w.write_bit(false); // vui
w.rbsp_trailing();
nal(3, 7, &w.finish())
}
fn pps(cabac: bool, weighted: bool) -> Vec<u8> {
let mut w = BitWriter::new();
w.write_ue(0);
w.write_ue(0);
w.write_bit(cabac);
w.write_bit(false);
w.write_ue(0); // slice groups
w.write_ue(0); // num_ref_idx_l0_default - 1
w.write_ue(0);
w.write_bit(weighted); // weighted_pred
w.write_bits(if weighted { 1 } else { 0 }, 2); // explicit weighted bi-prediction
w.write_se(0); // pic_init_qp - 26
w.write_se(0);
w.write_se(0); // chroma_qp_index_offset
w.write_bit(true); // deblocking_filter_control_present
w.write_bit(false);
w.write_bit(false);
w.rbsp_trailing();
nal(3, 8, &w.finish())
}
#[test]
fn avcc_accepts_generated_sps_and_pps() {
let sps = sps(66, 4, false, PocType::T2);
let pps = pps(false, false);
let (sps, pps) = (&sps[4..], &pps[4..]);
let mut avcc = vec![1, 66, 0, 30, 0xff, 0xe1];
avcc.extend_from_slice(&(sps.len() as u16).to_be_bytes());
avcc.extend_from_slice(sps);
avcc.push(1);
avcc.extend_from_slice(&(pps.len() as u16).to_be_bytes());
avcc.extend_from_slice(pps);
assert!(Decoder::from_avcc(&avcc).is_ok());
}
#[test]
fn avcc_rejects_a_valid_picture_slice_without_starting_a_picture() {
let sequence = sps(66, 4, false, PocType::T0);
let picture = pps(false, false);
let slice = slice_nal(
&Pic {
idr: true,
frame_num: 0,
ref_idc: 3,
long_term_ref: false,
mods: vec![],
mmcos: vec![],
mbs: vec![Some(vec![128; 384]); NMB],
b: false,
poc_lsb: Some(0),
poc_delta: None,
wt: None,
},
false,
);
let mut avcc = vec![1, 66, 0, 30, 0xff, 0xe1];
let nal = &sequence[4..];
avcc.extend_from_slice(&(nal.len() as u16).to_be_bytes());
avcc.extend_from_slice(nal);
avcc.push(2);
avcc.extend_from_slice(&((picture.len() - 4) as u16).to_be_bytes());
avcc.extend_from_slice(&picture[4..]);
avcc.extend_from_slice(&((slice.len() - 4) as u16).to_be_bytes());
avcc.extend_from_slice(&slice[4..]);
let mut decoder = Decoder::with_threads(1);
assert!(decoder.configure_avcc(&avcc).is_err());
assert!(decoder.flush().is_empty());
assert_eq!(decoder.stats().pictures, 0);
}
#[derive(Default, Clone)]
struct Pic {
idr: bool,
frame_num: u32,
ref_idc: u8,
long_term_ref: bool,
/// ref_pic_list_modification entries (idc, value)
mods: Vec<(u32, u32)>,
/// MMCOs: (op, a, b) with a = difference_of_pic_nums_minus1 / long_term_pic_num /
/// max_long_term_frame_idx_plus1 and b = long_term_frame_idx
mmcos: Vec<(u32, u32, u32)>,
/// Per MB: Some(samples) for I_PCM, None for P_Skip / B_Skip.
mbs: Vec<Option<Vec<u8>>>,
/// B slice (all B_Skip).
b: bool,
poc_lsb: Option<u32>,
/// delta_pic_order_cnt[0] (POC type 1).
poc_delta: Option<i32>,
/// Explicit weights (luma, cb, cr) as (weight, offset) for refIdx 0 of list 0 and list 1;
/// log2 denominators are 5 (luma) and 3 (chroma).
wt: Option<[[(i32, i32); 3]; 2]>,
}
impl Pic {
fn is_p(&self) -> bool {
!self.idr && !self.b && self.mbs.iter().any(Option::is_none)
}
fn inter(&self) -> bool {
self.b || self.is_p()
}
}
fn slice_header(w: &mut BitWriter, p: &Pic, cabac: bool) {
w.write_ue(0); // first_mb
w.write_ue(if p.b {
6
} else if p.is_p() {
5
} else {
7
});
w.write_ue(0); // pps id
w.write_bits(p.frame_num, 4);
if p.idr {
w.write_ue(0);
}
if let Some(lsb) = p.poc_lsb {
w.write_bits(lsb, 4);
}
if let Some(d) = p.poc_delta {
w.write_se(d);
}
if p.b {
w.write_bit(true); // direct_spatial_mv_pred_flag
}
if p.inter() {
w.write_bit(false); // num_ref_idx_override
w.write_bit(!p.mods.is_empty());
if !p.mods.is_empty() {
for &(idc, v) in &p.mods {
w.write_ue(idc);
w.write_ue(v);
}
w.write_ue(3);
}
if p.b {
w.write_bit(false); // no list 1 modification
}
}
if let (Some(wt), true) = (p.wt, p.inter()) {
w.write_ue(5);
w.write_ue(3);
for l in 0..if p.b { 2 } else { 1 } {
w.write_bit(true);
w.write_se(wt[l][0].0);
w.write_se(wt[l][0].1);
w.write_bit(true);
for c in 1..3 {
w.write_se(wt[l][c].0);
w.write_se(wt[l][c].1);
}
}
}
if p.ref_idc != 0 {
if p.idr {
w.write_bit(false);
w.write_bit(p.long_term_ref);
} else {
w.write_bit(!p.mmcos.is_empty());
if !p.mmcos.is_empty() {
for &(op, a, b) in &p.mmcos {
w.write_ue(op);
match op {
1 => w.write_ue(a),
2 => w.write_ue(a),
3 => {
w.write_ue(a);
w.write_ue(b);
}
4 => w.write_ue(a),
6 => w.write_ue(b),
_ => {}
}
}
w.write_ue(0);
}
}
}
if cabac && p.inter() {
w.write_ue(0); // cabac_init_idc
}
w.write_se(0); // slice_qp_delta
w.write_ue(0); // disable_deblocking_filter_idc
w.write_se(0);
w.write_se(0);
}
/// Minimal CABAC encoder (9.3.4, informative) writing into a BitWriter.
struct Enc {
low: u32,
range: u32,
outstanding: u32,
first: bool,
ctx: [u8; 1024],
}
impl Enc {
fn new(qp: i32, init: usize) -> Self {
let c = Cabac::new(&[0, 0, 0, 0], 0, qp, init).unwrap();
Enc { low: 0, range: 510, outstanding: 0, first: true, ctx: c.ctx }
}
fn init(&mut self) {
self.low = 0;
self.range = 510;
self.outstanding = 0;
self.first = true;
}
fn put(&mut self, w: &mut BitWriter, b: bool) {
if self.first {
self.first = false;
} else {
w.write_bit(b);
}
while self.outstanding > 0 {
w.write_bit(!b);
self.outstanding -= 1;
}
}
fn renorm(&mut self, w: &mut BitWriter) {
while self.range < 256 {
if self.low < 256 {
self.put(w, false);
} else if self.low >= 512 {
self.low -= 512;
self.put(w, true);
} else {
self.low -= 256;
self.outstanding += 1;
}
self.range <<= 1;
self.low <<= 1;
}
}
fn encode(&mut self, w: &mut BitWriter, ctx_idx: usize, bin: u32) {
let s = self.ctx[ctx_idx] as usize;
let lps = RANGE_TAB_LPS[s >> 1][((self.range >> 6) & 3) as usize] as u32;
self.range -= lps;
if bin != (s & 1) as u32 {
self.low += self.range;
self.range = lps;
self.ctx[ctx_idx] = NEXT_STATE[s][1];
} else {
self.ctx[ctx_idx] = NEXT_STATE[s][0];
}
self.renorm(w);
}
fn terminate(&mut self, w: &mut BitWriter, bin: bool) {
self.range -= 2;
if bin {
self.low += self.range;
// EncodeFlush
self.range = 2;
self.renorm(w);
self.put(w, (self.low >> 9) & 1 != 0);
w.write_bits(((self.low >> 7) & 3) | 1, 2);
} else {
self.renorm(w);
}
}
}
fn write_pcm(w: &mut BitWriter, samples: &[u8]) {
w.align_zero();
w.write_bytes(samples);
}
fn slice_nal(p: &Pic, cabac: bool) -> Vec<u8> {
let mut w = BitWriter::new();
slice_header(&mut w, p, cabac);
if !cabac && p.b {
w.write_ue(NMB as u32);
w.rbsp_trailing();
} else if !cabac {
let mut run = 0;
for mb in &p.mbs {
match mb {
None => run += 1,
Some(s) => {
if p.is_p() {
w.write_ue(run);
run = 0;
w.write_ue(30); // I_PCM in a P slice
} else {
w.write_ue(25);
}
write_pcm(&mut w, s);
}
}
}
if run > 0 {
w.write_ue(run);
}
w.rbsp_trailing();
} else {
while !w.is_byte_aligned() {
w.write_bit(true); // cabac_alignment_one_bit
}
let mut e = Enc::new(26, if p.inter() { 0 } else { 3 });
let skip = |i: usize| p.mbs[i].is_none();
for (i, mb) in p.mbs.iter().enumerate() {
let (x, y) = (i % MB_W, i / MB_W);
let a = if x > 0 { Some(i - 1) } else { None };
let b = if y > 0 { Some(i - MB_W) } else { None };
if p.inter() {
let inc = a.map(|n| !skip(n) as usize).unwrap_or(0) + b.map(|n| !skip(n) as usize).unwrap_or(0);
e.encode(&mut w, if p.b { 24 } else { 11 } + inc, skip(i) as u32);
}
if let Some(s) = mb {
if p.is_p() {
e.encode(&mut w, 14, 1); // intra prefix
e.encode(&mut w, 17, 1); // not I_NxN
} else {
// neighbours are I_PCM (not I_NxN) -> condTerm 1
let inc = a.is_some() as usize + b.is_some() as usize;
e.encode(&mut w, 3 + inc, 1);
}
e.terminate(&mut w, true); // I_PCM
write_pcm(&mut w, s);
e.init();
}
e.terminate(&mut w, i + 1 == NMB); // end_of_slice_flag
}
w.align_zero();
}
let t = if p.idr { 5 } else { 1 };
nal(p.ref_idc, t, &w.finish())
}
fn random_mb(seed: &mut u32) -> Vec<u8> {
(0..384)
.map(|_| {
*seed = seed.wrapping_mul(1664525).wrapping_add(1013904223);
(*seed >> 24) as u8
})
.collect()
}
/// Expected cropped planes of a picture made of PCM blocks (`None` = copy from `reference`).
fn render(mbs: &[Option<Vec<u8>>], reference: Option<&(Vec<u8>, Vec<u8>, Vec<u8>)>) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
let (w, h) = (MB_W * 16, MB_H * 16);
let (mut y, mut u, mut v) = match reference {
Some(r) => r.clone(),
None => (vec![0; w * h], vec![0; w * h / 4], vec![0; w * h / 4]),
};
for (i, mb) in mbs.iter().enumerate() {
let Some(s) = mb else { continue };
let (x0, y0) = ((i % MB_W) * 16, (i / MB_W) * 16);
for r in 0..16 {
y[(y0 + r) * w + x0..(y0 + r) * w + x0 + 16].copy_from_slice(&s[r * 16..r * 16 + 16]);
}
for r in 0..8 {
let o = (y0 / 2 + r) * (w / 2) + x0 / 2;
u[o..o + 8].copy_from_slice(&s[256 + r * 8..256 + r * 8 + 8]);
v[o..o + 8].copy_from_slice(&s[320 + r * 8..320 + r * 8 + 8]);
}
}
(y, u, v)
}
fn build_and_check(cabac: bool, threads: usize) {
let mut seed = 99u32;
let pcm_pic = |seed: &mut u32| (0..NMB).map(|_| Some(random_mb(seed))).collect::<Vec<_>>();
let a = pcm_pic(&mut seed);
let b = pcm_pic(&mut seed);
let mixed: Vec<Option<Vec<u8>>> = (0..NMB).map(|i| if i % 3 == 1 { Some(random_mb(&mut seed)) } else { None }).collect();
let skip = vec![None; NMB];
let pics = vec![
// 0: IDR, all PCM, marked long-term (LongTermFrameIdx 0)
Pic { idr: true, frame_num: 0, ref_idc: 3, long_term_ref: true, mbs: a.clone(), ..Default::default() },
// 1: all PCM, short-term
Pic { frame_num: 1, ref_idc: 2, mbs: b.clone(), ..Default::default() },
// 2: P_Skip from the long-term picture via modification idc 2 (default list would use pic 1)
Pic { frame_num: 2, ref_idc: 2, mods: vec![(2, 0)], mbs: skip.clone(), ..Default::default() },
// 3: frame_num gap (3, 4 missing); P_Skip from pic 2 (PicNum 2) via idc 0: 5 - (2 + 1) = 2
Pic { frame_num: 5, ref_idc: 2, mods: vec![(0, 2)], mbs: skip.clone(), ..Default::default() },
// 4: mixed PCM / P_Skip from pic 1 (PicNum 1: 6 - 5); MMCO 4 raises MaxLongTermFrameIdx to 2,
// MMCO 2 drops long-term 0, MMCO 3 makes pic 1 (difference 6 - 1 - 1 = 4) long-term index 1
Pic { frame_num: 6, ref_idc: 2, mods: vec![(0, 4)], mmcos: vec![(4, 3, 0), (2, 0, 0), (3, 4, 1)], mbs: mixed.clone(), ..Default::default() },
// 5: non-reference P_Skip from long-term index 1 (= pic 1)
Pic { frame_num: 7, ref_idc: 0, mods: vec![(2, 1)], mbs: skip.clone(), ..Default::default() },
// 6: P_Skip with the default list: short-term by descending PicNum -> pic 4 first
Pic { frame_num: 7, ref_idc: 2, mmcos: vec![(1, 0, 0), (6, 0, 2)], mbs: skip.clone(), ..Default::default() },
// 7: MMCO 5 picture made of PCM, then 8: P_Skip referencing it (only reference left)
Pic { frame_num: 8, ref_idc: 2, mmcos: vec![(5, 0, 0)], mbs: pcm_pic(&mut seed), ..Default::default() },
];
let mut stream = sps(if cabac { 77 } else { 66 }, 8, true, PocType::T2);
stream.extend(pps(cabac, false));
for p in &pics {
stream.extend(slice_nal(p, cabac));
}
let after5 = Pic { frame_num: 1, ref_idc: 2, mbs: skip.clone(), ..Default::default() };
stream.extend(slice_nal(&after5, cabac));
// expected output
let pa = render(&a, None);
let pb = render(&b, None);
let p2 = pa.clone();
let p3 = p2.clone();
let p4 = render(&mixed, Some(&pb));
let p5 = pb.clone();
let p6 = p4.clone();
let p7 = render(&pics[7].mbs, None);
let p8 = p7.clone();
let expected = [pa, pb, p2, p3, p4, p5, p6, p7, p8];
if threads == 1 {
cross_check_ffmpeg(&stream, &expected, if cabac { "synth_cabac" } else { "synth_cavlc" });
}
let mut dec = Decoder::with_threads(threads);
let mut out = dec.decode(&stream, 0).unwrap();
out.extend(dec.flush());
assert!(dec.take_error().is_none());
assert_eq!(out.len(), expected.len(), "cabac={cabac}");
for (i, (p, e)) in out.iter().zip(expected.iter()).enumerate() {
assert_eq!((p.width as usize, p.height as usize), (MB_W * 16, MB_H * 16));
assert!(p.y == e.0 && p.u == e.1 && p.v == e.2, "picture {i} differs (cabac={cabac}, threads={threads})");
}
let s = dec.stats();
assert!(s.mb_pcm > 0 && s.frame_num_gaps == 1 && s.long_term_marks >= 3, "{s:?}");
}
type Planes3 = (Vec<u8>, Vec<u8>, Vec<u8>);
/// Validate the hand-built stream and its expected output with ffmpeg when available.
fn cross_check_ffmpeg(stream: &[u8], expected: &[Planes3], name: &str) {
// The ffmpeg cross-check lives in FilmCraft (test oracle); this copy checks the expected planes only.
let _ = (stream, expected, name);
}
#[test]
fn pcm_long_term_mmco_gaps_cavlc() {
build_and_check(false, 1);
build_and_check(false, 4);
}
#[test]
fn pcm_long_term_mmco_gaps_cabac() {
build_and_check(true, 1);
build_and_check(true, 4);
}
fn weighted_check(cabac: bool, threads: usize) {
let mut seed = 7u32;
let a: Vec<Option<Vec<u8>>> = (0..NMB).map(|_| Some(random_mb(&mut seed))).collect();
let b: Vec<Option<Vec<u8>>> = (0..NMB).map(|_| Some(random_mb(&mut seed))).collect();
let skip = vec![None; NMB];
let wp = [[(20, 3), (5, -1), (12, 2)], [(0, 0); 3]];
let wb = [[(40, -2), (4, 1), (9, -3)], [(30, 7), (6, 0), (2, 4)]];
// decoding order: I (POC 0), P (POC 8), B (POC 4, both refs), non-reference weighted P_Skip (POC 12)
let pics = [
Pic { idr: true, frame_num: 0, ref_idc: 3, poc_lsb: Some(0), mbs: a.clone(), ..Default::default() },
Pic { frame_num: 1, ref_idc: 2, poc_lsb: Some(8), mbs: b.clone(), wt: Some(wp), ..Default::default() },
Pic { frame_num: 2, ref_idc: 0, poc_lsb: Some(4), b: true, mbs: skip.clone(), wt: Some(wb), ..Default::default() },
Pic { frame_num: 2, ref_idc: 0, poc_lsb: Some(12), mbs: skip.clone(), wt: Some(wp), ..Default::default() },
];
let mut stream = sps(77, 2, false, PocType::T0);
stream.extend(pps(cabac, true));
for p in &pics {
stream.extend(slice_nal(p, cabac));
}
let pa = render(&a, None);
let pb = render(&b, None);
let clip = |v: i32| v.clamp(0, 255) as u8;
let mix = |x: &[u8], y: &[u8], c: usize| -> Vec<u8> {
let (w0, o0) = wb[0][c];
let (w1, o1) = wb[1][c];
let d = if c == 0 { 5 } else { 3 };
x.iter().zip(y).map(|(&p, &q)| clip(((p as i32 * w0 + q as i32 * w1 + (1 << d)) >> (d + 1)) + ((o0 + o1 + 1) >> 1))).collect()
};
let single = |x: &[u8], c: usize| -> Vec<u8> {
let (w, o) = wp[0][c];
let d = if c == 0 { 5 } else { 3 };
x.iter().map(|&p| clip(((p as i32 * w + (1 << (d - 1))) >> d) + o)).collect()
};
let pbi = (mix(&pa.0, &pb.0, 0), mix(&pa.1, &pb.1, 1), mix(&pa.2, &pb.2, 2));
let pp = (single(&pb.0, 0), single(&pb.1, 1), single(&pb.2, 2));
let expected = [pa, pbi, pb, pp];
if threads == 1 {
cross_check_ffmpeg(&stream, &expected, if cabac { "synth_wp_cabac" } else { "synth_wp_cavlc" });
}
let mut dec = Decoder::with_threads(threads);
let mut out = dec.decode(&stream, 0).unwrap();
out.extend(dec.flush());
assert!(dec.take_error().is_none());
assert_eq!(out.len(), 4);
for (i, (p, e)) in out.iter().zip(expected.iter()).enumerate() {
assert!(p.y == e.0 && p.u == e.1 && p.v == e.2, "picture {i} differs (cabac={cabac}, threads={threads})");
}
}
#[test]
fn explicit_weighted_prediction_p_and_b_skip() {
for cabac in [false, true] {
weighted_check(cabac, 1);
weighted_check(cabac, 3);
}
}
/// POC type 1: non-reference pictures are displayed before the preceding reference picture.
#[test]
fn poc_type1_reordering() {
for cabac in [false, true] {
let mut seed = 5u32;
let contents: Vec<Vec<Option<Vec<u8>>>> = (0..5).map(|_| (0..NMB).map(|_| Some(random_mb(&mut seed))).collect()).collect();
// (frame_num, ref_idc): POCs 0, 4, 1, 8, 5
let spec = [(0, 3), (1, 2), (2, 0), (2, 2), (3, 0)];
let mut stream = sps(77, 2, false, PocType::T1);
stream.extend(pps(cabac, false));
for (i, &(frame_num, ref_idc)) in spec.iter().enumerate() {
let p = Pic { idr: i == 0, frame_num, ref_idc, poc_delta: Some(0), mbs: contents[i].clone(), ..Default::default() };
stream.extend(slice_nal(&p, cabac));
}
let expected_order = [0usize, 2, 1, 4, 3];
let expected_poc = [0, 1, 4, 5, 8];
for threads in [1, 2] {
let mut dec = Decoder::with_threads(threads);
let mut out = dec.decode(&stream, 0).unwrap();
out.extend(dec.flush());
assert_eq!(out.len(), 5);
for (k, p) in out.iter().enumerate() {
let e = render(&contents[expected_order[k]], None);
assert_eq!(p.poc, expected_poc[k]);
assert!(p.y == e.0 && p.u == e.1 && p.v == e.2, "output {k} (cabac={cabac})");
}
}
}
}
/// One IDR picture of PCM macroblocks.
fn idr_stream(max_refs: u32) -> Vec<u8> {
let mut seed = 5u32;
let mbs: Vec<Option<Vec<u8>>> = (0..NMB).map(|_| Some(random_mb(&mut seed))).collect();
let mut stream = sps(66, max_refs, false, PocType::T2);
stream.extend(pps(false, false));
stream.extend(slice_nal(&Pic { idr: true, frame_num: 0, ref_idc: 3, mbs, ..Default::default() }, false));
stream
}
fn decodes_within(stream: &[u8], budget: u64) -> bool {
let mut dec = Decoder::with_threads(1);
dec.set_memory_budget(budget);
let Ok(mut out) = dec.decode(stream, 0) else { return false };
out.extend(dec.flush());
dec.take_error().is_none() && out.len() == 1
}
#[test]
fn picture_store_is_checked_against_the_memory_budget() {
let stream = idr_stream(4);
let sps_nal = sps(66, 4, false, PocType::T2);
let parsed = crate::params::Sps::parse(&deckcraft_bitstream::unescape_rbsp(&sps_nal[5..])).unwrap();
// 4:2:0, 8-bit: luma plus two quarter-size chroma planes per picture; the DPB, the current
// picture and threads + 2 in flight.
let one = (NMB * 256 + 2 * NMB * 64) as u64;
let pictures = (parsed.max_dpb_frames() + 1 + 3) as u64;
assert!(decodes_within(&stream, pictures * one));
assert!(!decodes_within(&stream, pictures * one - 1));
assert!(decodes_within(&stream, deckcraft_bitstream::budget::DECODE_MEMORY_BUDGET));
}
#[test]
fn largest_accepted_h264_size_is_over_the_default_budget() {
// 1024 x 1024 macroblocks (16384 x 16384) with 16 references and 3 in flight.
let shape =
deckcraft_bitstream::budget::PictureShape { width: 16384, height: 16384, shift_x: 1, shift_y: 1, chroma_planes: 2, bytes_per_sample: 1 };
assert!(!shape.fits(16 + 1 + 3, deckcraft_bitstream::budget::DECODE_MEMORY_BUDGET));
}
fn gap_stream(gaps: bool) -> Vec<u8> {
let mut seed = 7u32;
let a: Vec<Option<Vec<u8>>> = (0..NMB).map(|_| Some(random_mb(&mut seed))).collect();
let mut stream = sps(66, 2, gaps, PocType::T2);
stream.extend(pps(false, false));
stream.extend(slice_nal(&Pic { idr: true, frame_num: 0, ref_idc: 3, mbs: a, ..Default::default() }, false));
// frame_num 1..=9 are missing.
stream.extend(slice_nal(&Pic { frame_num: 10, ref_idc: 2, mbs: vec![None; NMB], ..Default::default() }, false));
stream
}
#[test]
fn frame_num_gap_is_concealed_even_when_the_sps_forbids_gaps() {
// A cut or lossy stream: the gap is filled (bounded) instead of dropping the picture.
let mut dec = Decoder::with_threads(1);
let mut out = dec.decode(&gap_stream(false), 0).unwrap();
out.extend(dec.flush());
assert_eq!(out.len(), 2);
assert_eq!(dec.stats().frame_num_gaps, 1);
}
#[test]
fn frame_num_gap_is_filled_when_the_sps_allows_gaps() {
let mut dec = Decoder::with_threads(1);
let mut out = dec.decode(&gap_stream(true), 0).unwrap();
out.extend(dec.flush());
assert!(dec.take_error().is_none());
assert_eq!(out.len(), 2);
assert_eq!(dec.stats().frame_num_gaps, 1);
}
-175
View File
@@ -1,175 +0,0 @@
//! Small constant tables from the H.264 specification.
/// 4x4 frame zig-zag scan: scan position -> raster index (row * 4 + col).
pub const ZIGZAG4: [u8; 16] = [0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15];
/// 8x8 frame zig-zag scan: scan position -> raster index (row * 8 + col).
pub const ZIGZAG8: [u8; 64] = zigzag8();
const fn zigzag8() -> [u8; 64] {
let mut out = [0u8; 64];
let mut k = 0;
let mut d: i32 = 0; // anti-diagonal index = row + col
while d < 15 {
// even diagonals go up-right (row decreasing), odd go down-left
let mut i: i32 = 0;
while i < 8 {
let (row, col) = if d % 2 == 0 { (d - i, i) } else { (i, d - i) };
if row >= 0 && row < 8 && col >= 0 && col < 8 {
out[k] = (row * 8 + col) as u8;
k += 1;
}
i += 1;
}
d += 1;
}
out
}
/// Table 7-3.
pub const DEFAULT_4X4_INTRA: [u8; 16] = [6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32, 32, 37, 37, 42];
pub const DEFAULT_4X4_INTER: [u8; 16] = [10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27, 27, 30, 30, 34];
/// Table 7-4.
#[rustfmt::skip]
pub const DEFAULT_8X8_INTRA: [u8; 64] = [
6, 10, 10, 13, 11, 13, 16, 16, 16, 16, 18, 18, 18, 18, 18, 23,
23, 23, 23, 23, 23, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27,
27, 27, 27, 27, 29, 29, 29, 29, 29, 29, 29, 31, 31, 31, 31, 31,
31, 33, 33, 33, 33, 33, 36, 36, 36, 36, 38, 38, 38, 40, 40, 42,
];
#[rustfmt::skip]
pub const DEFAULT_8X8_INTER: [u8; 64] = [
9, 13, 13, 15, 13, 15, 17, 17, 17, 17, 19, 19, 19, 19, 19, 21,
21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 24, 24, 24, 24,
24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27, 27,
27, 28, 28, 28, 28, 28, 30, 30, 30, 30, 32, 32, 32, 33, 33, 35,
];
/// Table 8-15: QPc as a function of qPI (0..=51).
pub const QPC_TABLE: [u8; 52] = {
let mut t = [0u8; 52];
let tail = [29u8, 30, 31, 32, 32, 33, 34, 34, 35, 35, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 39];
let mut i = 0;
while i < 52 {
t[i] = if i < 30 { i as u8 } else { tail[i - 30] };
i += 1;
}
t
};
/// (8-315) v matrix for normAdjust4x4.
pub const NORM_ADJUST4: [[u16; 3]; 6] = [[10, 16, 13], [11, 18, 14], [13, 20, 16], [14, 23, 18], [16, 25, 20], [18, 29, 23]];
/// (8-318) v matrix for normAdjust8x8.
pub const NORM_ADJUST8: [[u16; 6]; 6] = [
[20, 18, 32, 19, 25, 24],
[22, 19, 35, 21, 28, 26],
[26, 23, 42, 24, 33, 31],
[28, 25, 45, 26, 35, 33],
[32, 28, 51, 30, 40, 38],
[36, 32, 58, 34, 46, 43],
];
/// normAdjust4x4(m, i, j) for raster position `pos` (i = row, j = col).
pub const fn norm_adjust4(m: usize, pos: usize) -> u16 {
let (i, j) = (pos / 4, pos % 4);
if i % 2 == 0 && j % 2 == 0 {
NORM_ADJUST4[m][0]
} else if i % 2 == 1 && j % 2 == 1 {
NORM_ADJUST4[m][1]
} else {
NORM_ADJUST4[m][2]
}
}
/// normAdjust8x8(m, i, j) for raster position `pos` (i = row, j = col).
pub const fn norm_adjust8(m: usize, pos: usize) -> u16 {
let (i, j) = (pos / 8, pos % 8);
let k = if i % 4 == 0 && j % 4 == 0 {
0
} else if i % 2 == 1 && j % 2 == 1 {
1
} else if i % 4 == 2 && j % 4 == 2 {
2
} else if (i % 4 == 0 && j % 2 == 1) || (i % 2 == 1 && j % 4 == 0) {
3
} else if (i % 4 == 0 && j % 4 == 2) || (i % 4 == 2 && j % 4 == 0) {
4
} else {
5
};
NORM_ADJUST8[m][k]
}
/// Table 8-16 alpha' (indexA 0..=51).
#[rustfmt::skip]
pub const ALPHA: [u8; 52] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 5, 6, 7, 8, 9, 10, 12, 13,
15, 17, 20, 22, 25, 28, 32, 36, 40, 45, 50, 56, 63, 71, 80, 90, 101, 113, 127, 144, 162, 182, 203, 226, 255, 255,
];
/// Table 8-16 beta' (indexB 0..=51).
#[rustfmt::skip]
pub const BETA: [u8; 52] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,
6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
];
/// Table 8-17 tC0' for bS = 1, 2, 3 (indexA 0..=51).
#[rustfmt::skip]
pub const TC0: [[u8; 3]; 52] = {
let b1: [u8; 52] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 6, 6, 7, 8, 9, 10, 11, 13,
];
let b2: [u8; 52] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 7, 8, 8, 10, 11, 12, 13, 15, 17,
];
let b3: [u8; 52] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 6, 6, 7, 8, 9, 10, 11, 13, 14, 16, 18, 20, 23, 25,
];
let mut t = [[0u8; 3]; 52];
let mut i = 0;
while i < 52 {
t[i] = [b1[i], b2[i], b3[i]];
i += 1;
}
t
};
/// Luma 4x4 block index (decoding order, 6.4.3) -> (x, y) in 4-sample units.
pub const BLK4_XY: [(u8, u8); 16] =
[(0, 0), (1, 0), (0, 1), (1, 1), (2, 0), (3, 0), (2, 1), (3, 1), (0, 2), (1, 2), (0, 3), (1, 3), (2, 2), (3, 2), (2, 3), (3, 3)];
/// Raster 4x4 position (y * 4 + x) -> luma4x4BlkIdx.
pub const RASTER_TO_BLK4: [u8; 16] = [0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15];
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn zigzag8_known_prefix() {
assert_eq!(&ZIGZAG8[..16], &[0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5]);
assert_eq!(&ZIGZAG8[48..], &[58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63]);
let mut seen = [false; 64];
for &z in &ZIGZAG8 {
seen[z as usize] = true;
}
assert!(seen.iter().all(|&s| s));
}
#[test]
fn blk4_maps_consistent() {
for (b, &(x, y)) in BLK4_XY.iter().enumerate() {
let r = (y * 4 + x) as usize;
assert_eq!(RASTER_TO_BLK4[r] as usize, b);
}
}
#[test]
fn qpc() {
assert_eq!(QPC_TABLE[29], 29);
assert_eq!(QPC_TABLE[30], 29);
assert_eq!(QPC_TABLE[51], 39);
}
}
-346
View File
@@ -1,346 +0,0 @@
//! Scaling (dequantisation) and inverse transforms (8.5.10 - 8.5.13).
use crate::params::ScalingMatrices;
use crate::tables::{norm_adjust4, norm_adjust8};
/// LevelScale4x4 / LevelScale8x8 for all six scaling lists and qP % 6, raster order.
#[derive(Clone)]
pub struct LevelScale {
/// [list 0..6 (IntraY, IntraCb, IntraCr, InterY, InterCb, InterCr)][qp % 6][pos]
pub ls4: [[[i32; 16]; 6]; 6],
/// [list 0..6 (IntraY, InterY, IntraCb, InterCb, IntraCr, InterCr)][qp % 6][pos]
pub ls8: [[[i32; 64]; 6]; 6],
}
impl LevelScale {
pub fn new(m: &ScalingMatrices) -> Box<Self> {
let mut s = Box::new(LevelScale { ls4: [[[0; 16]; 6]; 6], ls8: [[[0; 64]; 6]; 6] });
for l in 0..6 {
for q in 0..6 {
for p in 0..16 {
s.ls4[l][q][p] = m.m4[l][p] as i32 * norm_adjust4(q, p) as i32;
}
for p in 0..64 {
s.ls8[l][q][p] = m.m8[l][p] as i32 * norm_adjust8(q, p) as i32;
}
}
}
s
}
}
/// Scale one 4x4 AC coefficient (8.5.12.1, not the DC of Intra16x16 / chroma blocks).
#[inline(always)]
pub fn scale4(c: i32, ls: i32, qp: i32) -> i32 {
if qp >= 24 { c.wrapping_mul(ls) << (qp / 6 - 4) } else { c.wrapping_mul(ls).wrapping_add(1 << (3 - qp / 6)) >> (4 - qp / 6) }
}
/// Scale one 8x8 coefficient (8.5.13.1).
#[inline(always)]
pub fn scale8(c: i32, ls: i32, qp: i32) -> i32 {
if qp >= 36 { c.wrapping_mul(ls) << (qp / 6 - 6) } else { c.wrapping_mul(ls).wrapping_add(1 << (5 - qp / 6)) >> (6 - qp / 6) }
}
/// Intra16x16 luma DC: inverse Hadamard + scaling (8.5.10). `c` is raster order (row*4+col);
/// output replaces `c` with dcY (raster: row i -> 4x4 block row, col j -> block column).
pub fn luma_dc_dequant(c: &mut [i32; 16], qp: i32, ls00: i32) {
let mut t = [0i32; 16];
// rows
for i in 0..4 {
let r = &c[i * 4..i * 4 + 4];
let s01 = r[0] + r[1];
let d01 = r[0] - r[1];
let s23 = r[2] + r[3];
let d23 = r[2] - r[3];
t[i * 4] = s01 + s23;
t[i * 4 + 1] = s01 - s23;
t[i * 4 + 2] = d01 - d23;
t[i * 4 + 3] = d01 + d23;
}
// columns
for j in 0..4 {
let s01 = t[j] + t[4 + j];
let d01 = t[j] - t[4 + j];
let s23 = t[8 + j] + t[12 + j];
let d23 = t[8 + j] - t[12 + j];
let f = [s01 + s23, s01 - s23, d01 - d23, d01 + d23];
for i in 0..4 {
c[i * 4 + j] = if qp >= 36 {
f[i].wrapping_mul(ls00) << (qp / 6 - 6)
} else {
f[i].wrapping_mul(ls00).wrapping_add(1 << (5 - qp / 6)) >> (6 - qp / 6)
};
}
}
}
/// 4:2:0 chroma DC: 2x2 transform + scaling (8.5.11). `c` = [c00, c01, c10, c11].
pub fn chroma_dc_dequant_420(c: &mut [i32; 4], qp: i32, ls00: i32) {
let s0 = c[0].wrapping_add(c[1]);
let d0 = c[0].wrapping_sub(c[1]);
let s1 = c[2].wrapping_add(c[3]);
let d1 = c[2].wrapping_sub(c[3]);
let f = [s0.wrapping_add(s1), d0.wrapping_add(d1), s0.wrapping_sub(s1), d0.wrapping_sub(d1)];
for k in 0..4 {
c[k] = (f[k].wrapping_mul(ls00) << (qp / 6)) >> 5;
}
}
#[inline(always)]
fn clip_u8(v: i32) -> u8 {
v.clamp(0, 255) as u8
}
/// A dequantised coefficient the inverse transforms are defined for: the dequantisation wraps
/// by design (see [`huge_levels_dequantise_without_overflow_panics`]), so their butterflies
/// clamp an r outside −2^15..2^15−1 to this range — where the spec leaves the output
/// unspecified anyway (8.5.12.1, 8.5.13.1) — instead of overflowing the plain i32 additions.
#[inline(always)]
fn r_clamp(v: i32) -> i32 {
v.clamp(-(1 << 15), (1 << 15) - 1)
}
/// Inverse 4x4 transform of raster-order `d` and add to `dst` (8.5.12.2, 8.5.14).
///
/// Rows first, then columns (the spec's order: the `>> 1` steps make it matter). The column
/// pass and the reconstruction run across all columns at once so they vectorise.
pub fn idct4_add(d: &[i32; 16], dst: &mut [u8], stride: usize) {
let d: [i32; 16] = std::array::from_fn(|i| r_clamp(d[i]));
let mut t = [[0i32; 4]; 4];
for (i, row) in t.iter_mut().enumerate() {
let r = &d[i * 4..i * 4 + 4];
let e = r[0] + r[2];
let f = r[0] - r[2];
let g = (r[1] >> 1) - r[3];
let h = r[1] + (r[3] >> 1);
*row = [e + h, f + g, f - g, e - h];
}
let mut o = [[0i32; 4]; 4];
for j in 0..4 {
let e = t[0][j] + t[2][j];
let f = t[0][j] - t[2][j];
let g = (t[1][j] >> 1) - t[3][j];
let h = t[1][j] + (t[3][j] >> 1);
o[0][j] = e + h;
o[1][j] = f + g;
o[2][j] = f - g;
o[3][j] = e - h;
}
for (i, row) in o.iter().enumerate() {
let Some(line) = dst.get_mut(i * stride..).and_then(<[_]>::first_chunk_mut::<4>) else {
return;
};
for (p, &v) in line.iter_mut().zip(row) {
*p = clip_u8(*p as i32 + ((v + 32) >> 6));
}
}
}
/// DC-only shortcut: all AC coefficients zero.
#[allow(dead_code)]
pub fn idct4_dc_add(dc: i32, dst: &mut [u8], stride: usize) {
let v = (dc + 32) >> 6;
for i in 0..4 {
for p in &mut dst[i * stride..i * stride + 4] {
*p = clip_u8(*p as i32 + v);
}
}
}
#[inline(always)]
fn idct8_1d(d: [i32; 8]) -> [i32; 8] {
let a0 = d[0] + d[4];
let a4 = d[0] - d[4];
let a2 = (d[2] >> 1) - d[6];
let a6 = d[2] + (d[6] >> 1);
let b0 = a0 + a6;
let b2 = a4 + a2;
let b4 = a4 - a2;
let b6 = a0 - a6;
let a1 = -d[3] + d[5] - d[7] - (d[7] >> 1);
let a3 = d[1] + d[7] - d[3] - (d[3] >> 1);
let a5 = -d[1] + d[7] + d[5] + (d[5] >> 1);
let a7 = d[3] + d[5] + d[1] + (d[1] >> 1);
let b1 = a1 + (a7 >> 2);
let b7 = a7 - (a1 >> 2);
let b3 = a3 + (a5 >> 2);
let b5 = (a3 >> 2) - a5;
[b0 + b7, b2 + b5, b4 + b3, b6 + b1, b6 - b1, b4 - b3, b2 - b5, b0 - b7]
}
/// Inverse 8x8 transform of raster-order `d` and add to `dst` (8.5.13.2): rows, then columns,
/// the column pass and reconstruction across all columns at once (they vectorise).
pub fn idct8_add(d: &[i32; 64], dst: &mut [u8], stride: usize) {
let d: [i32; 64] = std::array::from_fn(|i| r_clamp(d[i]));
let mut t = [[0i32; 8]; 8];
for (row, src) in t.iter_mut().zip(d.as_chunks::<8>().0) {
*row = idct8_1d(*src);
}
let mut o = [[0i32; 8]; 8];
for j in 0..8 {
let col = idct8_1d(std::array::from_fn(|i| t[i][j]));
for i in 0..8 {
o[i][j] = col[i];
}
}
for (i, row) in o.iter().enumerate() {
let Some(line) = dst.get_mut(i * stride..).and_then(<[_]>::first_chunk_mut::<8>) else {
return;
};
for (p, &v) in line.iter_mut().zip(row) {
*p = clip_u8(*p as i32 + ((v + 32) >> 6));
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// The straightforward per-column formulation (8.5.12.2 / 8.5.13.2) the vectorised
/// transforms must equal.
fn reference_add(d: &[i32], n: usize, dst: &mut [u8], stride: usize) {
let one = |v: &[i32]| -> Vec<i32> {
if n == 4 {
let (e, f, g, h) = (v[0] + v[2], v[0] - v[2], (v[1] >> 1) - v[3], v[1] + (v[3] >> 1));
vec![e + h, f + g, f - g, e - h]
} else {
idct8_1d(v.try_into().unwrap()).to_vec()
}
};
let rows: Vec<Vec<i32>> = (0..n).map(|i| one(&d[i * n..i * n + n])).collect();
for j in 0..n {
let col = one(&(0..n).map(|i| rows[i][j]).collect::<Vec<_>>());
for i in 0..n {
let p = &mut dst[i * stride + j];
*p = (*p as i32 + ((col[i] + 32) >> 6)).clamp(0, 255) as u8;
}
}
}
#[test]
fn vectorised_transforms_match_reference() {
let mut rng = 0x1234_5678_9abc_def1u64;
let mut next = move || {
rng ^= rng << 13;
rng ^= rng >> 7;
rng ^= rng << 17;
rng
};
for iter in 0..5000 {
let range = [64u64, 1024, 8192, 1 << 15][iter % 4];
let d4: [i32; 16] = std::array::from_fn(|_| (next() % (2 * range + 1)) as i32 - range as i32);
let d8: [i32; 64] = std::array::from_fn(|_| (next() % (2 * range + 1)) as i32 - range as i32);
let base: Vec<u8> = (0..24 * 8).map(|_| next() as u8).collect();
let (mut a, mut b) = (base.clone(), base.clone());
idct4_add(&d4, &mut a[3..], 24);
reference_add(&d4, 4, &mut b[3..], 24);
assert_eq!(a, b, "4x4 iteration {iter}");
let (mut a, mut b) = (base.clone(), base);
idct8_add(&d8, &mut a[5..], 24);
reference_add(&d8, 8, &mut b[5..], 24);
assert_eq!(a, b, "8x8 iteration {iter}");
}
}
#[test]
fn idct4_dc_only_matches_shortcut() {
let mut d = [0i32; 16];
d[0] = 640; // -> +10 everywhere
let mut a = [100u8; 16];
let mut b = [100u8; 16];
idct4_add(&d, &mut a, 4);
idct4_dc_add(640, &mut b, 4);
assert_eq!(a, b);
assert!(a.iter().all(|&v| v == 110));
}
#[test]
fn idct4_hand_example() {
// d = [[64, 64, 0, 0], 0...]: row0: e=64,f=64,g=32,h=64 -> [128, 96, 32, 0]; columns: each col only row0 set
// col j: e=f=t0j, g=0, h=0 -> all rows = t0j. Result (t+32)>>6 = [2, 2, 1, 0] for every row.
let mut d = [0i32; 16];
d[0] = 64;
d[1] = 64;
let mut out = [0u8; 16];
idct4_add(&d, &mut out, 4);
for i in 0..4 {
assert_eq!(&out[i * 4..i * 4 + 4], &[2, 2, 1, 0]);
}
}
#[test]
fn idct8_dc() {
let mut d = [0i32; 64];
d[0] = 64 * 5;
let mut out = [10u8; 64];
idct8_add(&d, &mut out, 8);
assert!(out.iter().all(|&v| v == 15));
}
#[test]
fn luma_dc_flat() {
// single DC coefficient spreads equally to all 16 blocks
let mut c = [0i32; 16];
c[0] = 4;
luma_dc_dequant(&mut c, 36, 16 * 16);
assert!(c.iter().all(|&v| v == 4 * 256));
}
#[test]
fn chroma_dc() {
let mut c = [1, 0, 0, 0];
chroma_dc_dequant_420(&mut c, 6, 16 * 16);
// f = [1,1,1,1] -> ((1*256) << 1) >> 5 = 16
assert_eq!(c, [16, 16, 16, 16]);
}
#[test]
fn scale_functions() {
assert_eq!(scale4(2, 160, 24), 2 * 160);
assert_eq!(scale4(2, 160, 12), (2 * 160 + 2) >> 2);
assert_eq!(scale8(1, 320, 36), 320);
assert_eq!(scale8(1, 320, 30), (320 + 1) >> 1);
}
/// Corrupt streams can carry huge coefficient levels: dequantisation wraps (as release
/// builds always did) instead of panicking with "attempt to multiply with overflow" in
/// debug builds (found by mutation fuzzing an H.264 transport stream).
#[test]
fn huge_levels_dequantise_without_overflow_panics() {
let mut c = [i32::MAX / 2, i32::MAX / 3, -i32::MAX / 2, 7];
chroma_dc_dequant_420(&mut c, 51, 16 * 25);
let _ = scale4(i32::MAX, 16 * 25, 51);
let _ = scale4(i32::MAX, 16 * 25, 0);
let _ = scale8(i32::MIN, 16 * 25, 51);
let _ = scale8(i32::MIN, 16 * 25, 0);
}
/// The same wrapping dequantisation hands the inverse transforms any i32 coefficient, and
/// their butterflies add in plain i32: `i32::MAX + i32::MAX` panicked a debug build. The
/// spec leaves the transforms' output unspecified when a dequantised value falls outside
/// −2^15..2^15−1 (8.5.12.1, 8.5.13.1), so those inputs are clamped to that range —
/// bit-exact for every stream inside it — instead of panicking.
#[test]
fn huge_coefficients_are_clamped_to_the_spec_r_range() {
assert_eq!(r_clamp(-32769), -32768);
assert_eq!(r_clamp(-32768), -32768);
assert_eq!(r_clamp(32767), 32767);
assert_eq!(r_clamp(32768), 32767);
let clamp = |v: i32| v.clamp(-(1 << 15), (1 << 15) - 1);
for d in [[i32::MAX; 16], [i32::MIN; 16], std::array::from_fn(|i| if i % 2 == 0 { i32::MIN } else { i32::MAX })] {
let clamped = std::array::from_fn(|i| clamp(d[i]));
let (mut a, mut b) = ([0u8; 64], [0u8; 64]);
idct4_add(&d, &mut a, 8);
idct4_add(&clamped, &mut b, 8);
assert_eq!(a, b, "idct4 of extremes equals idct4 of the spec range");
}
for d in [[i32::MIN; 64], [i32::MAX; 64]] {
let clamped = std::array::from_fn(|i| clamp(d[i]));
let (mut a, mut b) = ([0u8; 64], [0u8; 64]);
idct8_add(&d, &mut a, 8);
idct8_add(&clamped, &mut b, 8);
assert_eq!(a, b, "idct8 of extremes equals idct8 of the spec range");
}
}
}
-19
View File
@@ -1,19 +0,0 @@
[package]
name = "deckcraft-hevc"
description = "Clean-room pure-Rust H.265/HEVC decoder (ITU-T H.265)"
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
[lints]
workspace = true
[features]
default = ["threads"]
threads = ["dep:rayon"]
[dependencies]
deckcraft-bitstream = { workspace = true }
thiserror = { workspace = true }
rayon = { workspace = true, optional = true }
-352
View File
@@ -1,352 +0,0 @@
//! CABAC arithmetic decoding engine (9.3.4.3) and context initialisation (9.3.2.2).
use crate::error::{Result, ensure};
use crate::spec_tables::{CABAC_INIT, NUM_CTX};
/// Table 9-52 (same values as H.264) rangeTabLPS[pStateIdx][qCodIRangeIdx].
#[rustfmt::skip]
pub(crate) static RANGE_TAB_LPS: [[u8; 4]; 64] = [
[128, 176, 208, 240], [128, 167, 197, 227], [128, 158, 187, 216], [123, 150, 178, 205],
[116, 142, 169, 195], [111, 135, 160, 185], [105, 128, 152, 175], [100, 122, 144, 166],
[95, 116, 137, 158], [90, 110, 130, 150], [85, 104, 123, 142], [81, 99, 117, 135],
[77, 94, 111, 128], [73, 89, 105, 122], [69, 85, 100, 116], [66, 80, 95, 110],
[62, 76, 90, 104], [59, 72, 86, 99], [56, 69, 81, 94], [53, 65, 77, 89],
[51, 62, 73, 85], [48, 59, 69, 80], [46, 56, 66, 76], [43, 53, 63, 72],
[41, 50, 59, 69], [39, 48, 56, 65], [37, 45, 54, 62], [35, 43, 51, 59],
[33, 41, 48, 56], [32, 39, 46, 53], [30, 37, 43, 50], [29, 35, 41, 48],
[27, 33, 39, 45], [26, 31, 37, 43], [24, 30, 35, 41], [23, 28, 33, 39],
[22, 27, 32, 37], [21, 26, 30, 35], [20, 24, 29, 33], [19, 23, 27, 31],
[18, 22, 26, 30], [17, 21, 25, 28], [16, 20, 23, 27], [15, 19, 22, 25],
[14, 18, 21, 24], [14, 17, 20, 23], [13, 16, 19, 22], [12, 15, 18, 21],
[12, 14, 17, 20], [11, 14, 16, 19], [11, 13, 15, 18], [10, 12, 15, 17],
[10, 12, 14, 16], [9, 11, 13, 15], [9, 11, 12, 14], [8, 10, 12, 14],
[8, 9, 11, 13], [7, 9, 11, 12], [7, 9, 10, 12], [7, 8, 10, 11],
[6, 8, 9, 11], [6, 7, 9, 10], [6, 7, 8, 9], [2, 2, 2, 2],
];
/// Table 9-53 transIdxLPS.
#[rustfmt::skip]
static TRANS_IDX_LPS: [u8; 64] = [
0, 0, 1, 2, 2, 4, 4, 5, 6, 7, 8, 9, 9, 11, 11, 12,
13, 13, 15, 15, 16, 16, 18, 18, 19, 19, 21, 21, 22, 22, 23, 24,
24, 25, 26, 26, 27, 27, 28, 29, 29, 30, 30, 30, 31, 32, 32, 33,
33, 33, 34, 34, 35, 35, 35, 36, 36, 36, 37, 37, 37, 38, 38, 63,
];
/// Table 9-53 transIdxMPS.
#[rustfmt::skip]
static TRANS_IDX_MPS: [u8; 64] = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 62, 63,
];
/// Combined state transition: index = (pStateIdx << 1 | valMPS) * 2 + bin_is_lps.
/// Each context is stored as `pStateIdx << 1 | valMPS`.
pub(crate) static NEXT_STATE: [[u8; 2]; 128] = {
let mut t = [[0u8; 2]; 128];
let mut s = 0;
while s < 128 {
let p = s >> 1;
let mps = s & 1;
// MPS path
t[s][0] = (TRANS_IDX_MPS[p] << 1) | mps as u8;
// LPS path
let new_mps = if p == 0 { 1 - mps } else { mps };
t[s][1] = (TRANS_IDX_LPS[p] << 1) | new_mps as u8;
s += 1;
}
t
};
/// Context states, padded to a power of two so that the per-bin index needs no bounds check.
pub type Contexts = [u8; CTX_SLOTS];
const CTX_SLOTS: usize = NUM_CTX.next_power_of_two();
/// Initialise all context variables for `slice_qp` and `init_type` (9.3.2.2). Each context is stored as
/// `pStateIdx << 1 | valMps`.
pub fn init_contexts(ctx: &mut Contexts, slice_qp: i32, init_type: usize) {
let qp = slice_qp.clamp(0, 51);
for (c, &v) in ctx.iter_mut().zip(CABAC_INIT[init_type].iter()) {
let slope = (v >> 4) as i32;
let offset = (v & 15) as i32;
let m = slope * 5 - 45;
let n = (offset << 3) - 16;
let pre = (((m * qp) >> 4) + n).clamp(1, 126);
*c = if pre <= 63 { ((63 - pre) << 1) as u8 } else { (((pre - 64) << 1) | 1) as u8 };
}
}
pub struct Cabac<'a> {
data: &'a [u8],
/// Byte position of the next byte to load into `value`.
next_byte: usize,
range: u32,
/// ivlOffset scaled by 2^bits: value = (ivlOffset << bits) | (bits already loaded but not yet
/// consumed by the arithmetic decoder).
value: u64,
bits: u32,
pub ctx: Contexts,
}
impl<'a> Cabac<'a> {
/// Create an engine over `data` starting at byte `pos`, with contexts initialised for `slice_qp`
/// and `init_type`.
pub fn new(data: &'a [u8], pos: usize, slice_qp: i32, init_type: usize) -> Result<Self> {
let mut c = Cabac { data, next_byte: pos, range: 0, value: 0, bits: 0, ctx: [0; CTX_SLOTS] };
init_contexts(&mut c.ctx, slice_qp, init_type);
c.init_engine()?;
Ok(c)
}
/// 9.3.2.6: ivlCurrRange = 510, ivlOffset = read_bits(9) (from the current byte-aligned position).
pub fn init_engine(&mut self) -> Result<()> {
self.range = 510;
self.value = 0;
self.bits = 0;
self.refill();
self.bits -= 9;
ensure!((self.value >> self.bits) < 510, "invalid CABAC offset at init");
Ok(())
}
/// Load 32 more bits (zeros past the end of the data).
#[inline(always)]
fn refill(&mut self) {
let b = self.next_byte;
let mut w = [0u8; 4];
if let Some(src) = self.data.get(b..b + 4) {
w.copy_from_slice(src);
} else if b < self.data.len() {
let n = self.data.len() - b;
w[..n].copy_from_slice(&self.data[b..]);
}
self.next_byte += 4;
self.value = (self.value << 32) | u32::from_be_bytes(w) as u64;
self.bits += 32;
}
/// Bit position of the next bit not consumed by the arithmetic decoder (for PCM samples and
/// substream ends after decode_terminate returned 1).
pub fn bit_pos(&self) -> usize {
self.next_byte * 8 - self.bits as usize
}
/// Continue reading at byte `pos` (call `init_engine` afterwards).
pub fn set_byte_pos(&mut self, pos: usize) {
self.next_byte = pos;
self.value = 0;
self.bits = 0;
}
pub fn data(&self) -> &'a [u8] {
self.data
}
/// True when the reader has run past the end of the data (corrupt stream).
pub fn overrun(&self) -> bool {
self.next_byte > self.data.len() + 16
}
#[inline(always)]
fn renorm(&mut self) {
// ivlCurrRange < 512 always, so the shift is 0 when no renormalisation is needed: no
// branch on the (unpredictable) range, only on the rare refill.
let shift = self.range.leading_zeros() - 23;
self.range <<= shift;
if self.bits < shift {
self.refill();
}
self.bits -= shift;
}
/// 9.3.4.3.2, branch-free: the MPS / LPS outcome of a context-coded bin is close to random
/// at high bit rates, so values are selected instead of jumping.
#[inline(always)]
pub fn decode_decision(&mut self, ctx_idx: usize) -> u32 {
let ctx_idx = ctx_idx & (CTX_SLOTS - 1);
let s = (self.ctx[ctx_idx] & 127) as usize;
let q = ((self.range >> 6) & 3) as usize;
let lps = RANGE_TAB_LPS[s >> 1][q] as u32;
let mps_range = self.range - lps;
let scaled = (mps_range as u64) << self.bits;
let is_lps = self.value >= scaled;
self.value -= scaled & (is_lps as u64).wrapping_neg();
self.range = if is_lps { lps } else { mps_range };
self.ctx[ctx_idx] = NEXT_STATE[s][is_lps as usize];
self.renorm();
(s & 1) as u32 ^ is_lps as u32
}
#[inline(always)]
pub fn decode_bypass(&mut self) -> u32 {
if self.bits == 0 {
self.refill();
}
self.bits -= 1;
let scaled = (self.range as u64) << self.bits;
let one = self.value >= scaled;
self.value -= scaled & (one as u64).wrapping_neg();
one as u32
}
/// `n` (<= 16) bypass bins at once, most significant first. The bins of a run of bypass
/// decodes are the binary digits of floor(ivlOffset / ivlCurrRange) in the bit-scaled
/// representation (no renormalisation happens between them), so one division replaces `n`
/// compare-subtract steps.
#[inline(always)]
pub fn decode_bypass_bits(&mut self, n: u32) -> u32 {
debug_assert!(n <= 16);
if self.bits < n {
self.refill();
}
let s = self.bits - n;
let r = ((self.value >> s) / self.range as u64) as u32;
self.value -= ((r as u64) * self.range as u64) << s;
self.bits = s;
r
}
pub fn decode_terminate(&mut self) -> u32 {
self.range -= 2;
let scaled = (self.range as u64) << self.bits;
if self.value >= scaled {
1
} else {
self.renorm();
0
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Minimal CABAC encoder (9.3.4, informative) used to round-trip test the decoder.
struct Enc {
low: u32,
range: u32,
outstanding: u32,
first: bool,
bits: Vec<bool>,
ctx: Vec<u8>,
}
impl Enc {
fn new(ctx: &[u8]) -> Self {
Enc { low: 0, range: 510, outstanding: 0, first: true, bits: vec![], ctx: ctx.to_vec() }
}
fn put(&mut self, b: bool) {
if self.first {
self.first = false;
} else {
self.bits.push(b);
}
while self.outstanding > 0 {
self.bits.push(!b);
self.outstanding -= 1;
}
}
fn renorm(&mut self) {
while self.range < 256 {
if self.low < 256 {
self.put(false);
} else if self.low >= 512 {
self.low -= 512;
self.put(true);
} else {
self.low -= 256;
self.outstanding += 1;
}
self.range <<= 1;
self.low <<= 1;
}
}
fn encode(&mut self, ctx_idx: usize, bin: u32) {
let s = self.ctx[ctx_idx] as usize;
let p = s >> 1;
let mps = (s & 1) as u32;
let q = ((self.range >> 6) & 3) as usize;
let lps = RANGE_TAB_LPS[p][q] as u32;
self.range -= lps;
if bin != mps {
self.low += self.range;
self.range = lps;
self.ctx[ctx_idx] = NEXT_STATE[s][1];
} else {
self.ctx[ctx_idx] = NEXT_STATE[s][0];
}
self.renorm();
}
fn bypass(&mut self, bin: u32) {
self.low <<= 1;
if bin != 0 {
self.low += self.range;
}
if self.low >= 1024 {
self.put(true);
self.low -= 1024;
} else if self.low < 512 {
self.put(false);
} else {
self.low -= 512;
self.outstanding += 1;
}
}
fn terminate_flush(&mut self) -> Vec<u8> {
// encode terminate bin = 1 then flush (9.3.4.5)
self.range -= 2;
self.low += self.range;
self.range = 2;
self.renorm();
self.put((self.low >> 9) & 1 != 0);
self.bits.push((self.low >> 8) & 1 != 0);
self.bits.push(true); // rbsp_stop_one_bit
while !self.bits.len().is_multiple_of(8) {
self.bits.push(false);
}
self.bits.chunks(8).map(|c| c.iter().fold(0u8, |a, &b| (a << 1) | b as u8)).collect()
}
}
#[test]
fn context_init_formula() {
let mut ctx: Contexts = [0u8; CTX_SLOTS];
// initValue 154 is the "equiprobable" value: m = 0, n = 64 -> pStateIdx 0, valMps 1
init_contexts(&mut ctx, 30, 0);
assert_eq!(ctx[crate::spec_tables::CU_TRANSQUANT_BYPASS], 1);
// split_cu_flag ctx 0 (initValue 139): slope 8, offset 11 -> m = -5, n = 72; QP 30: (-150 >> 4) + 72 = 62
assert_eq!(ctx[crate::spec_tables::SPLIT_CU], (63 - 62) << 1);
}
#[test]
fn round_trip_random_bins() {
let mut seed = 12345u32;
let mut rnd = || {
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
seed
};
let dummy = Cabac::new(&[0, 0, 0, 0], 0, 30, 1).unwrap();
let mut enc = Enc::new(&dummy.ctx);
let mut ops = Vec::new();
for _ in 0..5000 {
let kind = rnd() % 4;
let ctx = (rnd() % 16) as usize;
// skewed bins so contexts adapt
let bin = if rnd() % 10 < 8 { (ctx & 1) as u32 } else { 1 - (ctx & 1) as u32 };
if kind == 0 {
enc.bypass(bin);
} else {
enc.encode(ctx, bin);
}
ops.push((kind, ctx, bin));
}
let bytes = enc.terminate_flush();
let mut dec = Cabac::new(&bytes, 0, 30, 1).unwrap();
for (i, &(kind, ctx, bin)) in ops.iter().enumerate() {
let got = if kind == 0 { dec.decode_bypass() } else { dec.decode_decision(ctx) };
assert_eq!(got, bin, "bin {i}");
}
assert_eq!(dec.decode_terminate(), 1);
}
}
-685
View File
@@ -1,685 +0,0 @@
//! Top-level decoder: NAL dispatch, picture boundaries, POC / RPS / DPB management and output.
//!
//! The calling thread parses parameter sets and slice headers and does all POC, RPS and DPB
//! bookkeeping. The CTU layer of each picture runs as a job (on a thread pool with the `threads`
//! feature) that publishes finished CTB rows into the picture's shared [`Frame`]; jobs of later pictures
//! block per row on the reference data they read, so several pictures decode concurrently.
use crate::dpb::{Dpb, Output, OutputMeta, PocState, RefPicSet, build_ref_lists};
use crate::error::{Error, Result, ensure, invalid};
use crate::params::{Layout, Pps, Sps, Vps};
use crate::picture::{Frame, FrameRef};
use crate::slice::{NalHeader, SliceHeader, nal_type};
use crate::slicedec::{F_INTRA, F_SKIP, PicState, SliceDecoder, SliceJob};
use crate::{ColorInfo, Picture, Plane};
use deckcraft_bitstream::{annexb_nals, length_prefixed_nals, unescape_rbsp};
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
/// A picture whose slices are being collected.
struct PendingPic {
frame: FrameRef,
sps: Arc<Sps>,
pps: Arc<Pps>,
layout: Arc<Layout>,
/// Header of the latest independent slice segment (for dependent slice segments).
last_sh: SliceHeader,
first_sh: SliceHeader,
rps: RefPicSet,
poc: i32,
output: bool,
meta: Arc<OutputMeta>,
slices: Vec<SliceJob>,
}
/// Counters describing what the decoder has seen (useful to check test coverage).
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct DecodeStats {
pub pictures: u64,
pub slices_i: u64,
pub slices_p: u64,
pub slices_b: u64,
pub dependent_slices: u64,
pub idr: u64,
pub cra: u64,
pub skipped_rasl: u64,
pub intra_4x4_blocks: u64,
pub skip_4x4_blocks: u64,
pub inter_4x4_blocks: u64,
pub weighted_slices: u64,
pub tmvp_slices: u64,
pub sao_slices: u64,
pub deblock_disabled_slices: u64,
pub tiles_pictures: u64,
pub wpp_pictures: u64,
pub scaling_list_pictures: u64,
pub transform_skip_pictures: u64,
pub long_term_slices: u64,
pub missing_refs: u64,
/// Pictures decoded in draft mode without in-loop filters ([`Decoder::set_draft`]).
pub draft_pictures: u64,
}
#[derive(Default)]
struct Shared {
error: Mutex<Option<Error>>,
stats: Mutex<DecodeStats>,
}
/// H.265 / HEVC decoder.
pub struct Decoder {
vpss: Vec<Option<Arc<Vps>>>,
spss: Vec<Option<Arc<Sps>>>,
ppss: Vec<Option<Arc<Pps>>>,
nal_length_size: Option<usize>,
dpb: Dpb,
poc_state: PocState,
pending: Option<PendingPic>,
next_id: u32,
active_sps: Option<Arc<Sps>>,
first_picture: bool,
after_eos: bool,
/// RASL pictures associated with the last IRAP are skipped (it had NoRaslOutputFlag = 1).
skip_rasl: bool,
layout_cache: Vec<(Arc<Pps>, Arc<Sps>, Arc<Layout>, Option<Arc<Vec<Vec<u8>>>>)>,
out_queue: VecDeque<Output>,
shared: Arc<Shared>,
#[cfg(feature = "threads")]
pool: Option<rayon::ThreadPool>,
in_flight: VecDeque<FrameRef>,
max_in_flight: usize,
/// Bytes the picture store may take ([`deckcraft_bitstream::budget`]).
memory_budget: u64,
draft: bool,
}
impl Default for Decoder {
fn default() -> Self {
Self::new()
}
}
fn default_threads() -> usize {
#[cfg(all(feature = "threads", not(target_arch = "wasm32")))]
{
std::thread::available_parallelism().map(|n| n.get()).unwrap_or(1).min(16)
}
#[cfg(not(all(feature = "threads", not(target_arch = "wasm32"))))]
{
1
}
}
impl Decoder {
/// A decoder using all available cores (with the `threads` feature).
pub fn new() -> Self {
Self::with_threads(default_threads())
}
/// A decoder using up to `threads` worker threads (1 = decode on the calling thread).
pub fn with_threads(threads: usize) -> Self {
#[cfg(feature = "threads")]
let pool = if threads > 1 && cfg!(not(target_arch = "wasm32")) {
rayon::ThreadPoolBuilder::new().num_threads(threads).thread_name(|i| format!("hevc-{i}")).build().ok()
} else {
None
};
Decoder {
vpss: vec![None; 16],
spss: vec![None; 16],
ppss: vec![None; 64],
nal_length_size: None,
dpb: Dpb::default(),
poc_state: PocState::default(),
pending: None,
next_id: 1,
active_sps: None,
first_picture: true,
after_eos: false,
skip_rasl: false,
layout_cache: Vec::new(),
out_queue: VecDeque::new(),
shared: Arc::new(Shared::default()),
#[cfg(feature = "threads")]
pool,
in_flight: VecDeque::new(),
max_in_flight: threads.max(1) + 2,
memory_budget: deckcraft_bitstream::budget::DECODE_MEMORY_BUDGET,
draft: false,
}
}
/// Configure from an `hvcC` (HEVCDecoderConfigurationRecord) box payload.
pub fn from_hvcc(hvcc: &[u8]) -> Result<Self> {
let mut d = Decoder::new();
d.configure_hvcc(hvcc)?;
Ok(d)
}
/// Parse an `hvcC` record: parameter-set arrays and the NAL length size used by [`Decoder::decode`].
pub fn configure_hvcc(&mut self, hvcc: &[u8]) -> Result<()> {
ensure!(hvcc.len() >= 23, "hvcC too short");
ensure!(hvcc[0] == 1, "unsupported hvcC version {}", hvcc[0]);
let num_arrays = hvcc[22] as usize;
for pass in 0..2 {
let mut pos = 23;
for _ in 0..num_arrays {
ensure!(pos <= hvcc.len() && hvcc.len() - pos >= 3, "hvcC truncated");
let array_type = hvcc[pos] & 0x3f;
let n = u16::from_be_bytes([hvcc[pos + 1], hvcc[pos + 2]]) as usize;
pos += 3;
for _ in 0..n {
ensure!(pos <= hvcc.len() && hvcc.len() - pos >= 2, "hvcC truncated");
let len = u16::from_be_bytes([hvcc[pos], hvcc[pos + 1]]) as usize;
pos += 2;
ensure!(len <= hvcc.len() - pos, "hvcC truncated");
let nal = &hvcc[pos..pos + len];
if pass == 0 {
let header = NalHeader::parse(nal)?;
ensure!(
[nal_type::VPS, nal_type::SPS, nal_type::PPS, nal_type::PREFIX_SEI, nal_type::SUFFIX_SEI].contains(&array_type),
"hvcC array has unsupported NAL type {array_type}"
);
ensure!(header.nal_type == array_type, "hvcC array type {array_type} disagrees with NAL header type {}", header.nal_type);
} else {
self.handle_nal(nal, 0)?;
}
pos += len;
}
}
}
self.nal_length_size = Some((hvcc[21] & 3) as usize + 1);
Ok(())
}
/// Length of NAL length prefixes (from hvcC), or None for Annex-B input.
pub fn nal_length_size(&self) -> Option<usize> {
self.nal_length_size
}
/// Decode one access unit (all NAL units of one picture, Annex-B or length-prefixed per
/// configuration; a byte stream with several complete access units is accepted too). Returns
/// pictures that became ready for output, in output order; `pts` travels with the picture of this
/// access unit through reordering.
pub fn decode(&mut self, data: &[u8], pts: i64) -> Result<Vec<Picture>> {
let nals = match self.nal_length_size {
Some(n) => length_prefixed_nals(data, n)?,
None => annexb_nals(data),
};
let mut result = Ok(());
for nal in nals {
if let Err(e) = self.handle_nal(nal, pts) {
result = Err(e);
break;
}
}
self.submit_pending();
if let Some(e) = self.take_error() {
result = result.and(Err(e));
}
result?;
let max_queue = self.max_in_flight * 2;
let mut pics = Vec::new();
while let Some(o) = self.out_queue.front() {
if !o.frame.is_complete() && self.out_queue.len() <= max_queue {
break;
}
let Some(o) = self.out_queue.pop_front() else { break };
pics.push(make_picture(&o));
}
Ok(pics)
}
/// Output all remaining pictures (end of stream).
pub fn flush(&mut self) -> Vec<Picture> {
self.submit_pending();
let mut outs = Vec::new();
self.dpb.flush(&mut outs);
self.out_queue.extend(outs);
let pics = self.out_queue.drain(..).map(|o| make_picture(&o)).collect();
while let Some(f) = self.in_flight.pop_front() {
f.wait_complete();
}
self.first_picture = true;
pics
}
/// Statistics accumulated so far.
pub fn stats(&self) -> DecodeStats {
self.shared.stats.lock().map(|s| s.clone()).unwrap_or_default()
}
fn stat(&self, f: impl FnOnce(&mut DecodeStats)) {
if let Ok(mut s) = self.shared.stats.lock() {
f(&mut s);
}
}
/// First error reported by a decoding job since the last call (also returned by
/// [`Decoder::decode`]); useful after [`Decoder::flush`].
/// Draft mode for reduced-resolution playback (off by default): sub-layer non-reference
/// pictures of the highest temporal sub-layer (pictures nothing predicts from) skip
/// deblocking and SAO and come out flagged [`Picture::draft`]; every other picture is
/// unchanged.
pub fn set_draft(&mut self, on: bool) {
self.draft = on;
}
pub fn take_error(&mut self) -> Option<Error> {
self.shared.error.lock().ok().and_then(|mut e| e.take())
}
fn handle_nal(&mut self, nal: &[u8], pts: i64) -> Result<()> {
if nal.len() < 2 {
return Ok(());
}
let hdr = NalHeader::parse(nal)?;
if hdr.layer_id != 0 {
return Ok(());
}
match hdr.nal_type {
nal_type::VPS => {
let v = Vps::parse(&unescape_rbsp(&nal[2..]))?;
let id = v.id as usize;
self.vpss[id] = Some(Arc::new(v));
}
nal_type::SPS => {
let s = Sps::parse(&unescape_rbsp(&nal[2..]))?;
let id = s.id as usize;
self.spss[id] = Some(Arc::new(s));
}
nal_type::PPS => {
let p = Pps::parse(&unescape_rbsp(&nal[2..]))?;
let id = p.id as usize;
self.ppss[id] = Some(Arc::new(p));
}
nal_type::EOS | nal_type::EOB => {
self.submit_pending();
self.after_eos = true;
}
0..=9 | 16..=21 => self.handle_slice(nal, hdr, pts)?,
// AUD, SEI, filler, reserved and unspecified NAL units are skipped
_ => {}
}
Ok(())
}
fn layout_for(&mut self, pps: &Arc<Pps>, sps: &Arc<Sps>) -> Result<(Arc<Layout>, Option<Arc<Vec<Vec<u8>>>>)> {
if let Some((_, _, l, s)) = self.layout_cache.iter().find(|(p, s, _, _)| Arc::ptr_eq(p, pps) && Arc::ptr_eq(s, sps)) {
return Ok((l.clone(), s.clone()));
}
let layout = Arc::new(Layout::new(sps, pps)?);
let scaling = if sps.scaling_list_enabled {
let list = pps.scaling_list.as_ref().or(sps.scaling_list.as_ref()).cloned().unwrap_or_else(crate::params::ScalingList::default_lists);
let mut v = Vec::with_capacity(24);
for size in 0..4 {
for m in 0..6 {
v.push(list.factor(size, m));
}
}
Some(Arc::new(v))
} else {
None
};
if self.layout_cache.len() > 8 {
self.layout_cache.remove(0);
}
self.layout_cache.push((pps.clone(), sps.clone(), layout.clone(), scaling.clone()));
Ok((layout, scaling))
}
fn handle_slice(&mut self, nal: &[u8], hdr: NalHeader, pts: i64) -> Result<()> {
let rbsp = unescape_rbsp(&nal[2..]);
// a first_slice_segment_in_pic_flag of 1 starts a new picture
let first = rbsp.first().is_some_and(|b| b & 0x80 != 0);
if first {
self.submit_pending();
}
if hdr.is_rasl() && (self.skip_rasl || self.first_picture) {
if first {
self.stat(|s| s.skipped_rasl += 1);
}
return Ok(());
}
let ppss = &self.ppss;
let spss = &self.spss;
let prev = self.pending.as_ref().map(|p| &p.last_sh);
let (sh, pps, sps) = SliceHeader::parse(
&rbsp,
hdr,
|id| {
let pps = ppss[id as usize].as_ref().ok_or_else(|| Error::MissingParameterSet(format!("PPS {id}")))?;
let sps = spss[pps.sps_id as usize].as_ref().ok_or_else(|| Error::MissingParameterSet(format!("SPS {}", pps.sps_id)))?;
Ok((pps.clone(), sps.clone()))
},
prev,
)?;
sps.check_supported()?;
pps.check_supported()?;
if first {
self.start_picture(&sh, &sps, &pps, pts)?;
} else {
let Some(p) = &self.pending else { return invalid("slice segment without the first slice of its picture") };
ensure!(Arc::ptr_eq(&p.pps, &pps), "PPS changed within a picture");
}
let Some(pending) = self.pending.as_mut() else {
return invalid("slice segment without the first slice of its picture");
};
let refs = build_ref_lists(&sh, &pending.rps)?;
if !sh.dependent {
pending.last_sh = sh.clone();
}
let (st, dependent, weighted, tmvp, sao, dbk_off, lt) =
(sh.slice_type, sh.dependent, sh.pwt.is_some(), sh.temporal_mvp, sh.sao_luma || sh.sao_chroma, sh.deblocking_disabled, !sh.lt.is_empty());
pending.slices.push(SliceJob { sh, pps, sps, rbsp, refs });
self.stat(|s| {
match st {
crate::slice::SliceType::I => s.slices_i += 1,
crate::slice::SliceType::P => s.slices_p += 1,
crate::slice::SliceType::B => s.slices_b += 1,
}
s.dependent_slices += dependent as u64;
s.weighted_slices += weighted as u64;
s.tmvp_slices += tmvp as u64;
s.sao_slices += sao as u64;
s.deblock_disabled_slices += dbk_off as u64;
s.long_term_slices += lt as u64;
});
Ok(())
}
/// Cap the bytes of decoded pictures (current, references, in flight) this decoder may hold.
pub fn set_memory_budget(&mut self, bytes: u64) {
self.memory_budget = bytes;
}
/// Refuse an SPS whose pictures would not fit the memory budget, before anything is allocated.
fn check_budget(&self, sps: &Sps) -> Result<()> {
let chroma = sps.chroma_array_type();
let shape = deckcraft_bitstream::budget::PictureShape {
width: u64::from(sps.width),
height: u64::from(sps.height),
shift_x: u32::from(sps.sub_width_c() == 2),
shift_y: u32::from(sps.sub_height_c() == 2),
chroma_planes: if chroma == 0 { 0 } else { 2 },
bytes_per_sample: 2,
};
let pictures = u64::from(sps.max_dec_pic_buffering) + 1 + self.max_in_flight as u64;
ensure!(
shape.fits(pictures, self.memory_budget),
"{}x{} pictures with {pictures} in the decoder exceed the memory budget",
sps.width,
sps.height
);
Ok(())
}
fn start_picture(&mut self, sh: &SliceHeader, sps: &Arc<Sps>, pps: &Arc<Pps>, pts: i64) -> Result<()> {
self.check_budget(sps)?;
ensure!(sh.first_slice_segment_in_pic && !sh.dependent, "invalid first slice segment");
let hdr = sh.nal;
let changed = match &self.active_sps {
None => true,
Some(a) => {
a.width != sps.width
|| a.height != sps.height
|| a.bit_depth_luma != sps.bit_depth_luma
|| a.bit_depth_chroma != sps.bit_depth_chroma
|| a.log2_ctb != sps.log2_ctb
}
};
let mut outs = Vec::new();
if changed && self.active_sps.is_some() {
self.dpb.flush(&mut outs);
}
self.active_sps = Some(sps.clone());
self.dpb.max_dec_pic_buffering = sps.max_dec_pic_buffering as usize;
self.dpb.max_num_reorder = sps.max_num_reorder as usize;
self.dpb.max_latency = if sps.max_latency_increase_plus1 != 0 { sps.max_num_reorder + sps.max_latency_increase_plus1 - 1 } else { 0 };
let irap_no_rasl = hdr.is_irap() && (hdr.is_idr() || hdr.is_bla() || self.first_picture || self.after_eos);
if hdr.is_irap() {
self.skip_rasl = irap_no_rasl;
self.stat(|s| {
if hdr.is_idr() {
s.idr += 1;
} else {
s.cra += 1;
}
});
}
let max_lsb = sps.max_poc_lsb();
let poc = self.poc_state.compute(sh, max_lsb, irap_no_rasl);
let (w, h) = (sps.width as usize, sps.height as usize);
let template = Frame::new(0, 0, w, h, sps.log2_ctb, sps.bit_depth_luma, sps.bit_depth_chroma);
let next_id = &mut self.next_id;
let mut missing = 0u64;
let max_missing = (sps.max_dec_pic_buffering as u64).clamp(1, 16);
let mut shared_gray: Option<FrameRef> = None;
let mut make_missing = |p: i32| -> FrameRef {
missing += 1;
if missing > max_missing
&& let Some(ref g) = shared_gray
{
return g.clone();
}
let id = *next_id;
*next_id += 1;
let f = Arc::new(Frame::gray(id, p, &template));
if shared_gray.is_none() {
shared_gray = Some(f.clone());
}
f
};
let rps = self.dpb.apply_rps(sh, poc, max_lsb, irap_no_rasl, &mut make_missing)?;
if missing > 0 {
self.stat(|s| s.missing_refs += missing);
}
if irap_no_rasl && !self.first_picture {
if sh.no_output_of_prior_pics && hdr.is_idr() {
self.dpb.clear();
} else {
self.dpb.flush(&mut outs);
}
} else {
self.dpb.bump_before_decode(&mut outs);
}
self.out_queue.extend(outs);
let (layout, scaling) = self.layout_for(pps, sps)?;
let _ = scaling;
let id = self.next_id;
self.next_id += 1;
let frame = Arc::new(Frame::new(id, poc, w, h, sps.log2_ctb, sps.bit_depth_luma, sps.bit_depth_chroma));
let output = sh.pic_output && !(hdr.is_rasl() && self.skip_rasl);
// Draft mode: a sub-layer non-reference picture of the highest sub-layer is never used
// for prediction, so its in-loop filters can be left out without changing anything else.
let draft = self.draft && hdr.is_sub_layer_non_ref() && hdr.temporal_id as u32 >= sps.max_sub_layers_minus1;
if draft {
self.stat(|s| s.draft_pictures += 1);
}
let meta = Arc::new(output_meta(sps, pts, hdr.is_irap(), draft));
self.pending = Some(PendingPic {
frame,
sps: sps.clone(),
pps: pps.clone(),
layout,
last_sh: sh.clone(),
first_sh: sh.clone(),
rps,
poc,
output,
meta,
slices: Vec::new(),
});
self.first_picture = false;
self.after_eos = false;
self.stat(|s| {
s.tiles_pictures += pps.tiles_enabled as u64;
s.wpp_pictures += pps.entropy_coding_sync as u64;
s.scaling_list_pictures += sps.scaling_list_enabled as u64;
s.transform_skip_pictures += pps.transform_skip as u64;
});
Ok(())
}
/// Start decoding the pending picture and insert it into the DPB.
fn submit_pending(&mut self) {
let Some(p) = self.pending.take() else { return };
let PendingPic { frame, sps, pps, layout, first_sh, poc, output, meta, slices, .. } = p;
let scaling =
self.layout_cache.iter().find(|(pp, ss, _, _)| Arc::ptr_eq(pp, &pps) && Arc::ptr_eq(ss, &sps)).and_then(|(_, _, _, s)| s.clone());
self.dispatch(frame.clone(), sps, pps, layout, scaling, slices, meta.draft);
self.poc_state.update(&first_sh, poc);
let mut outs = Vec::new();
self.dpb.insert(frame, poc, output, meta, &mut outs);
self.out_queue.extend(outs);
}
#[allow(clippy::too_many_arguments)]
fn dispatch(
&mut self,
frame: FrameRef,
sps: Arc<Sps>,
pps: Arc<Pps>,
layout: Arc<Layout>,
scaling: Option<Arc<Vec<Vec<u8>>>>,
slices: Vec<SliceJob>,
draft: bool,
) {
let shared = self.shared.clone();
#[cfg(feature = "threads")]
if let Some(pool) = &self.pool {
self.in_flight.retain(|f| !f.is_complete());
while self.in_flight.len() >= self.max_in_flight {
if let Some(f) = self.in_flight.pop_front() {
f.wait_complete();
}
}
self.in_flight.push_back(frame.clone());
pool.spawn_fifo(move || run_job(frame, sps, pps, layout, scaling, slices, draft, &shared));
return;
}
run_job(frame, sps, pps, layout, scaling, slices, draft, &shared);
}
}
/// Decode all slices of one picture and publish its rows.
#[allow(clippy::too_many_arguments)]
fn run_job(
frame: FrameRef,
sps: Arc<Sps>,
pps: Arc<Pps>,
layout: Arc<Layout>,
scaling: Option<Arc<Vec<Vec<u8>>>>,
slices: Vec<SliceJob>,
draft: bool,
shared: &Shared,
) {
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let mut pic = PicState::new(frame.clone(), sps, pps, layout, scaling);
pic.draft = draft;
let mut first_err = None;
for s in &slices {
if let Err(e) = SliceDecoder::decode(&mut pic, s) {
first_err.get_or_insert(e);
}
}
crate::filter::finish(&mut pic);
(pic, first_err)
}));
let (pic, err) = match result {
Ok((pic, err)) => (Some(pic), err),
Err(_) => {
// make sure readers never block on this frame
let gray = Frame::gray(0, frame.poc, &frame);
for r in 0..frame.num_rows() {
if !frame.is_published(r) {
let row = gray.row(r);
frame.publish(r, crate::picture::FrameRow { y: row.y.clone(), cb: row.cb.clone(), cr: row.cr.clone(), col: row.col.clone() });
}
}
(None, Some(Error::Invalid("internal error while decoding a picture".into())))
}
};
if let Ok(mut s) = shared.stats.lock() {
s.pictures += 1;
if let Some(pic) = &pic {
for b in &pic.blk {
if b.flags & F_INTRA != 0 {
s.intra_4x4_blocks += 1;
} else if b.flags & F_SKIP != 0 {
s.skip_4x4_blocks += 1;
} else if b.flags != 0 {
s.inter_4x4_blocks += 1;
}
}
}
}
if let (Some(e), Ok(mut slot)) = (err, shared.error.lock()) {
slot.get_or_insert(e);
}
}
fn output_meta(sps: &Sps, pts: i64, key: bool, draft: bool) -> OutputMeta {
let vui = sps.vui.clone().unwrap_or_default();
OutputMeta {
pts,
key,
crop: sps.crop_rect(),
full_range: vui.full_range,
colour_primaries: vui.colour_primaries,
transfer_characteristics: vui.transfer_characteristics,
matrix_coefficients: vui.matrix_coefficients,
sar: vui.sar,
bit_depth: sps.bit_depth_luma,
draft,
}
}
fn make_picture(o: &Output) -> Picture {
let (cx, cy, cw, ch) = o.meta.crop;
let (cx, cy, cw, ch) = (cx as usize, cy as usize, cw as usize, ch as usize);
let (y, u, v) = o.frame.copy_cropped((cx, cy, cw, ch));
let (ccw, cch) = (cw.div_ceil(2), ch.div_ceil(2));
let bd = o.meta.bit_depth;
let conv = |p: Vec<u16>| if bd <= 8 { Plane::U8(p.into_iter().map(|v| v as u8).collect()) } else { Plane::U16(p) };
Picture {
width: cw as u32,
height: ch as u32,
chroma_width: ccw as u32,
chroma_height: cch as u32,
bit_depth: bd,
y: conv(y),
u: conv(u),
v: conv(v),
y_stride: cw,
uv_stride: ccw,
pts: o.meta.pts,
poc: o.poc,
key: o.meta.key,
color: ColorInfo {
full_range: o.meta.full_range,
primaries: o.meta.colour_primaries,
transfer: o.meta.transfer_characteristics,
matrix: o.meta.matrix_coefficients,
},
sar: o.meta.sar,
draft: o.meta.draft,
}
}
#[cfg(test)]
mod config_tests {
use super::Decoder;
#[test]
fn hvcc_skips_prefix_and_suffix_sei_arrays() {
let mut config = vec![0; 23];
config[0] = 1;
config[22] = 2;
config.extend_from_slice(&[39, 0, 1, 0, 2, 0x4e, 0x01]);
config.extend_from_slice(&[40, 0, 1, 0, 2, 0x50, 0x01]);
let decoder = Decoder::from_hvcc(&config);
assert!(decoder.is_ok());
}
}
-296
View File
@@ -1,296 +0,0 @@
//! Decoded picture buffer: POC derivation (8.3.1), reference picture set (8.3.2), missing
//! reference generation (8.3.3), reference picture lists (8.3.4) and output / bumping (C.5.2).
use crate::error::{Result, ensure};
use crate::picture::{FrameRef, RefPic};
use crate::slice::SliceHeader;
use std::sync::Arc;
/// Output metadata travelling with a picture.
#[derive(Clone, Debug)]
pub struct OutputMeta {
pub pts: i64,
pub key: bool,
pub crop: (u32, u32, u32, u32),
pub full_range: bool,
pub colour_primaries: u8,
pub transfer_characteristics: u8,
pub matrix_coefficients: u8,
pub sar: (u16, u16),
pub bit_depth: u32,
/// Decoded in draft mode (no in-loop filters).
pub draft: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Marking {
Unused,
Short,
Long,
}
pub struct Entry {
pub frame: FrameRef,
pub poc: i32,
pub marking: Marking,
pub needed_for_output: bool,
pub latency: u32,
pub meta: Arc<OutputMeta>,
}
/// A picture leaving the DPB for output.
pub struct Output {
pub frame: FrameRef,
pub poc: i32,
pub meta: Arc<OutputMeta>,
}
/// Reference picture set of the current picture (8.3.2) as DPB indices / generated frames.
#[derive(Default)]
pub struct RefPicSet {
pub st_curr_before: Vec<RefPic>,
pub st_curr_after: Vec<RefPic>,
pub lt_curr: Vec<RefPic>,
}
#[derive(Default)]
pub struct Dpb {
pub entries: Vec<Entry>,
pub max_dec_pic_buffering: usize,
pub max_num_reorder: usize,
/// SpsMaxLatencyPictures (0 = no limit).
pub max_latency: u32,
}
impl Dpb {
fn bump(&mut self, outs: &mut Vec<Output>) -> bool {
let Some(i) = (0..self.entries.len()).filter(|&i| self.entries[i].needed_for_output).min_by_key(|&i| self.entries[i].poc) else {
return false;
};
let e = &mut self.entries[i];
e.needed_for_output = false;
outs.push(Output { frame: e.frame.clone(), poc: e.poc, meta: e.meta.clone() });
self.remove_unused();
true
}
fn remove_unused(&mut self) {
self.entries.retain(|e| e.needed_for_output || e.marking != Marking::Unused);
}
fn num_output(&self) -> usize {
self.entries.iter().filter(|e| e.needed_for_output).count()
}
fn latency_exceeded(&self) -> bool {
self.max_latency != 0 && self.entries.iter().any(|e| e.needed_for_output && e.latency >= self.max_latency)
}
/// Output everything (end of stream or IRAP without no_output_of_prior_pics).
pub fn flush(&mut self, outs: &mut Vec<Output>) {
while self.bump(outs) {}
for e in &mut self.entries {
e.marking = Marking::Unused;
}
self.entries.clear();
}
/// Discard everything without output.
pub fn clear(&mut self) {
self.entries.clear();
}
/// C.5.2.2: removal of pictures before decoding the current (non-IRAP-reset) picture, after the RPS
/// has been applied.
pub fn bump_before_decode(&mut self, outs: &mut Vec<Output>) {
self.remove_unused();
loop {
let full = self.entries.len() >= self.max_dec_pic_buffering.max(1);
if self.num_output() > self.max_num_reorder || self.latency_exceeded() || full {
if !self.bump(outs) {
// DPB full of reference pictures that are not needed for output: nothing to do
break;
}
} else {
break;
}
}
}
/// C.5.2.3: insert the current picture and apply "additional bumping".
pub fn insert(&mut self, frame: FrameRef, poc: i32, output: bool, meta: Arc<OutputMeta>, outs: &mut Vec<Output>) {
for e in &mut self.entries {
if e.needed_for_output {
e.latency += 1;
}
}
self.entries.push(Entry { frame, poc, marking: Marking::Short, needed_for_output: output, latency: 0, meta });
while self.num_output() > self.max_num_reorder || self.latency_exceeded() {
if !self.bump(outs) {
break;
}
}
}
/// Apply the RPS of the current picture (8.3.2): mark pictures, find (or generate) references.
/// `make_missing` creates a replacement frame for a missing reference picture with the given POC.
pub fn apply_rps(
&mut self,
sh: &SliceHeader,
poc: i32,
max_poc_lsb: i32,
irap_no_rasl: bool,
make_missing: &mut dyn FnMut(i32) -> FrameRef,
) -> Result<RefPicSet> {
let mut set = RefPicSet::default();
if sh.nal.is_irap() && irap_no_rasl {
for e in &mut self.entries {
e.marking = Marking::Unused;
}
}
if sh.nal.is_idr() {
for e in &mut self.entries {
e.marking = Marking::Unused;
}
return Ok(set);
}
// Long-term entries first (they may refer to any reference picture).
let mut keep = vec![false; self.entries.len()];
let mut lt_idx: Vec<(Option<usize>, i32, bool, bool)> = Vec::new(); // (entry, poc, used, msb_present)
for &(lsb, used, msb_present, cycle) in &sh.lt {
let mut p = lsb as i32;
if msb_present {
p += poc - (cycle as i32).wrapping_mul(max_poc_lsb) - (poc & (max_poc_lsb - 1));
}
let found = self
.entries
.iter()
.position(|e| e.marking != Marking::Unused && if msb_present { e.poc == p } else { (e.poc & (max_poc_lsb - 1)) == p });
lt_idx.push((found, p, used, msb_present));
}
// Short-term entries.
let mut st: Vec<(Option<usize>, i32, bool, bool)> = Vec::new(); // (entry, poc, used, before)
for &(d, used) in &sh.st_rps.s0 {
let p = poc + d;
let found = self.entries.iter().position(|e| e.marking == Marking::Short && e.poc == p);
st.push((found, p, used, true));
}
for &(d, used) in &sh.st_rps.s1 {
let p = poc + d;
let found = self.entries.iter().position(|e| e.marking == Marking::Short && e.poc == p);
st.push((found, p, used, false));
}
for &(f, _, _, _) in lt_idx.iter() {
if let Some(i) = f {
keep[i] = true;
}
}
for &(f, _, _, _) in st.iter() {
if let Some(i) = f {
keep[i] = true;
}
}
for (i, e) in self.entries.iter_mut().enumerate() {
if !keep[i] {
e.marking = Marking::Unused;
}
}
for &(f, _, _, _) in &lt_idx {
if let Some(i) = f {
self.entries[i].marking = Marking::Long;
}
}
let mut make = |found: Option<usize>, p: i32, lt: bool, entries: &mut Vec<Entry>| -> RefPic {
match found {
Some(i) => RefPic { frame: entries[i].frame.clone(), poc: entries[i].poc, long_term: lt },
None => {
// 8.3.3: generate an unavailable reference picture
let frame = make_missing(p);
RefPic { frame, poc: p, long_term: lt }
}
}
};
for &(f, p, used, before) in &st {
if !used {
continue;
}
let r = make(f, p, false, &mut self.entries);
if before {
set.st_curr_before.push(r);
} else {
set.st_curr_after.push(r);
}
}
for &(f, p, used, _) in &lt_idx {
if used {
let r = make(f, p, true, &mut self.entries);
set.lt_curr.push(r);
}
}
Ok(set)
}
}
/// Build RefPicList0/1 (8.3.4).
pub fn build_ref_lists(sh: &SliceHeader, rps: &RefPicSet) -> Result<[Vec<RefPic>; 2]> {
let mut lists: [Vec<RefPic>; 2] = [Vec::new(), Vec::new()];
if sh.is_intra() {
return Ok(lists);
}
let total = rps.st_curr_before.len() + rps.st_curr_after.len() + rps.lt_curr.len();
ensure!(total > 0, "no reference pictures for an inter slice");
for l in 0..if sh.is_b() { 2 } else { 1 } {
let n = (sh.num_ref_idx[l] as usize).max(total);
let mut temp = Vec::with_capacity(n);
let (first, second) = if l == 0 { (&rps.st_curr_before, &rps.st_curr_after) } else { (&rps.st_curr_after, &rps.st_curr_before) };
while temp.len() < n {
for r in first.iter().chain(second.iter()).chain(rps.lt_curr.iter()) {
if temp.len() < n {
temp.push(r.clone());
}
}
}
for i in 0..sh.num_ref_idx[l] as usize {
let idx = match &sh.list_entry[l] {
Some(e) => e[i] as usize,
None => i,
};
ensure!(idx < temp.len(), "list_entry out of range");
lists[l].push(temp[idx].clone());
}
}
Ok(lists)
}
/// POC state (8.3.1).
#[derive(Default, Clone, Copy)]
pub struct PocState {
pub prev_tid0_poc: i32,
}
impl PocState {
pub fn compute(&self, sh: &SliceHeader, max_poc_lsb: i32, irap_no_rasl: bool) -> i32 {
let lsb = sh.poc_lsb as i32;
let msb = if sh.nal.is_irap() && irap_no_rasl {
0
} else {
let prev_lsb = self.prev_tid0_poc & (max_poc_lsb - 1);
let prev_msb = self.prev_tid0_poc - prev_lsb;
if lsb < prev_lsb && prev_lsb - lsb >= max_poc_lsb / 2 {
prev_msb + max_poc_lsb
} else if lsb > prev_lsb && lsb - prev_lsb > max_poc_lsb / 2 {
prev_msb - max_poc_lsb
} else {
prev_msb
}
};
msb + lsb
}
/// Update after decoding a picture with the given header and POC.
pub fn update(&mut self, sh: &SliceHeader, poc: i32) {
if sh.nal.temporal_id == 0 && !sh.nal.is_rasl() && !sh.nal.is_radl() && !sh.nal.is_sub_layer_non_ref() {
self.prev_tid0_poc = poc;
}
}
}
-38
View File
@@ -1,38 +0,0 @@
use deckcraft_bitstream::BitError;
/// Errors produced by the decoder.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum Error {
/// The bitstream ended early or contained an invalid Exp-Golomb code.
#[error("bitstream error: {0}")]
Bitstream(#[from] BitError),
/// A syntax element had a value outside its legal range.
#[error("invalid bitstream: {0}")]
Invalid(String),
/// A valid feature this decoder does not implement (yet).
#[error("unsupported: {0}")]
Unsupported(String),
/// A slice referenced a parameter set that has not been received.
#[error("missing parameter set: {0}")]
MissingParameterSet(String),
}
pub type Result<T> = std::result::Result<T, Error>;
pub(crate) fn invalid<T>(msg: impl Into<String>) -> Result<T> {
Err(Error::Invalid(msg.into()))
}
pub(crate) fn unsupported<T>(msg: impl Into<String>) -> Result<T> {
Err(Error::Unsupported(msg.into()))
}
/// `ensure!(cond, "message")` returns `Error::Invalid` when `cond` is false.
macro_rules! ensure {
($cond:expr, $($arg:tt)+) => {
if !$cond {
return Err($crate::error::Error::Invalid(format!($($arg)+)));
}
};
}
pub(crate) use ensure;
-598
View File
@@ -1,598 +0,0 @@
//! In-loop filters and row publication: deblocking (8.7.2) and sample adaptive offset (8.7.3), run
//! row by row behind the CTU decoding so finished CTB rows can be published to later pictures early.
//!
//! Row r is deblocked once rows 0..=r+1 are decoded (row r+1 still needs the unfiltered bottom samples
//! of row r for intra prediction); SAO of row r runs once row r+1 is deblocked; then row r is final.
use crate::picture::{ColMv, FrameRow};
use crate::slicedec::{E_PU_H, E_PU_V, E_TU_H, E_TU_V, F_CBF, F_CODED, F_INTRA, F_NOFILTER, MvField, PicState};
use crate::spec_tables::{BETA_TABLE, TC_TABLE};
use crate::tables::qpc_420;
/// Called after each decoded CTB of CTB row `ry`.
pub fn ctb_decoded(pic: &mut PicState, ry: usize) {
pic.row_done[ry] += 1;
while pic.rows_complete < pic.hctb && pic.row_done[pic.rows_complete] as usize == pic.wctb {
pic.rows_complete += 1;
}
advance(pic, false);
}
/// Run the filters / publication as far as the decoded rows allow (`all`: end of picture).
pub fn advance(pic: &mut PicState, all: bool) {
let h = pic.hctb;
loop {
let r = pic.rows_deblocked;
if r < h && (all || pic.rows_complete >= (r + 2).min(h)) {
deblock_row(pic, r);
pic.rows_deblocked += 1;
continue;
}
let r = pic.rows_published;
if r < h && pic.rows_deblocked >= (r + 2).min(h) {
publish_row(pic, r);
pic.rows_published += 1;
continue;
}
break;
}
}
/// End of picture: filter and publish everything.
pub fn finish(pic: &mut PicState) {
pic.rows_complete = pic.hctb;
advance(pic, true);
}
#[inline]
fn tc_beta(q: i32, bs: i32, tc_off: i32, beta_off: i32, bd: u32) -> (i32, i32) {
let qb = (q + beta_off).clamp(0, 51);
let qt = (q + 2 * (bs - 1) + tc_off).clamp(0, 53);
((TC_TABLE[qt as usize] as i32) << (bd - 8), (BETA_TABLE[qb as usize] as i32) << (bd - 8))
}
/// Boundary strength (8.7.2.4) between 4x4 blocks p and q.
fn boundary_strength(pic: &PicState, pi: usize, qi: usize, tu_edge: bool) -> i32 {
let (p, q) = (&pic.blk[pi], &pic.blk[qi]);
if (p.flags | q.flags) & F_INTRA != 0 {
return 2;
}
if tu_edge && (p.flags | q.flags) & F_CBF != 0 {
return 1;
}
let (mp, mq) = (&pic.mvf[pi], &pic.mvf[qi]);
motion_bs(pic, pi, mp, qi, mq)
}
fn ref_id(pic: &PicState, blk: usize, f: &MvField, l: usize) -> u32 {
let (x, y) = ((blk % pic.w4) * 4, (blk / pic.w4) * 4);
let ctb = (y >> pic.log2_ctb) * pic.wctb + (x >> pic.log2_ctb);
let s = pic.ctb_slice[ctb];
if s == u32::MAX {
return u32::MAX;
}
pic.slices[s as usize].ref_ids[l].get(f.ref_idx[l] as usize).copied().unwrap_or(u32::MAX - 1)
}
fn motion_bs(pic: &PicState, pi: usize, p: &MvField, qi: usize, q: &MvField) -> i32 {
let np = p.pred(0) as u32 + p.pred(1) as u32;
let nq = q.pred(0) as u32 + q.pred(1) as u32;
if np != nq {
return 1;
}
let far = |a: [i16; 2], b: [i16; 2]| (a[0] as i32 - b[0] as i32).abs() >= 4 || (a[1] as i32 - b[1] as i32).abs() >= 4;
if np == 1 {
let lp = if p.pred(0) { 0 } else { 1 };
let lq = if q.pred(0) { 0 } else { 1 };
if ref_id(pic, pi, p, lp) != ref_id(pic, qi, q, lq) {
return 1;
}
return far(p.mv[lp], q.mv[lq]) as i32;
}
let (p0, p1) = (ref_id(pic, pi, p, 0), ref_id(pic, pi, p, 1));
let (q0, q1) = (ref_id(pic, qi, q, 0), ref_id(pic, qi, q, 1));
if !((p0 == q0 && p1 == q1) || (p0 == q1 && p1 == q0)) {
return 1;
}
if p0 != p1 {
if p0 == q0 { (far(p.mv[0], q.mv[0]) || far(p.mv[1], q.mv[1])) as i32 } else { (far(p.mv[0], q.mv[1]) || far(p.mv[1], q.mv[0])) as i32 }
} else {
((far(p.mv[0], q.mv[0]) || far(p.mv[1], q.mv[1])) && (far(p.mv[0], q.mv[1]) || far(p.mv[1], q.mv[0]))) as i32
}
}
/// Whether the edge between blocks p and q (q = current block, owning the edge) is filtered, and the
/// slice-level parameters (tc offset, beta offset) of q's slice.
fn edge_params(pic: &PicState, px: usize, py: usize, qx: usize, qy: usize) -> Option<(i32, i32)> {
let s = pic.log2_ctb;
let pc = (py >> s) * pic.wctb + (px >> s);
let qc = (qy >> s) * pic.wctb + (qx >> s);
let qs = pic.ctb_slice[qc];
let ps = pic.ctb_slice[pc];
if qs == u32::MAX || ps == u32::MAX {
return None;
}
let sq = &pic.slices[qs as usize];
if sq.deblock_disabled {
return None;
}
if pc != qc {
if pic.ctb_addr[pc] != pic.ctb_addr[qc] && !sq.lf_across {
return None;
}
if !pic.pps.loop_filter_across_tiles && pic.layout.tile_of_rs(pc as u32) != pic.layout.tile_of_rs(qc as u32) {
return None;
}
}
Some((sq.tc_offset, sq.beta_offset))
}
/// Deblock CTB row `r`: vertical edges of the row, then horizontal edges whose q side is in the row.
fn deblock_row(pic: &mut PicState, r: usize) {
if !pic.deblocking_enabled_anywhere || pic.draft {
return;
}
let ctb = 1usize << pic.log2_ctb;
let y0 = r * ctb;
let y1 = (y0 + ctb).min(pic.height);
let (w, w4) = (pic.width, pic.w4);
let (bd, bdc) = (pic.bd_y, pic.bd_c);
let (cb_off, cr_off) = (pic.pps.cb_qp_offset, pic.pps.cr_qp_offset);
// vertical edges
let mut bs_v = vec![0i8; w4 * ((y1 - y0) / 4)];
for by in y0 / 4..y1 / 4 {
for bx in (2..w4).step_by(2) {
let qi = by * w4 + bx;
let e = pic.blk[qi].edges;
if e & (E_TU_V | E_PU_V) == 0 || pic.blk[qi].flags & F_CODED == 0 {
continue;
}
let (x, y) = (bx * 4, by * 4);
let Some((tc_off, beta_off)) = edge_params(pic, x - 1, y, x, y) else { continue };
let bs = boundary_strength(pic, qi - 1, qi, e & E_TU_V != 0);
bs_v[(by - y0 / 4) * w4 + bx] = bs as i8;
if bs == 0 {
continue;
}
let (p, q) = (&pic.blk[qi - 1], &pic.blk[qi]);
let qpl = (p.qp as i32 + q.qp as i32 + 1) >> 1;
let (tc, beta) = tc_beta(qpl, bs, tc_off, beta_off, bd);
let (nfp, nfq) = (p.flags & F_NOFILTER != 0, q.flags & F_NOFILTER != 0);
filter_luma(&mut pic.planes[0], y * w + x, 1, w, tc, beta, nfp, nfq, bd);
}
}
// chroma vertical edges (8x8 chroma grid = 16 luma samples), bS == 2 only
let cw = pic.cwidth;
for by in (y0 / 4..y1 / 4).step_by(2) {
for bx in (4..w4).step_by(4) {
if bs_v[(by - y0 / 4) * w4 + bx] != 2 {
continue;
}
let (x, y) = (bx * 4, by * 4);
let qi = by * w4 + bx;
let Some((tc_off, _)) = edge_params(pic, x - 1, y, x, y) else { continue };
let (p, q) = (pic.blk[qi - 1], pic.blk[qi]);
let (nfp, nfq) = (p.flags & F_NOFILTER != 0, q.flags & F_NOFILTER != 0);
for (c, off) in [(1, cb_off), (2, cr_off)] {
let qpi = ((p.qp as i32 + q.qp as i32 + 1) >> 1) + off;
let qt = (qpc_420(qpi) + 2 + tc_off).clamp(0, 53);
let tc = (TC_TABLE[qt as usize] as i32) << (bdc - 8);
filter_chroma(&mut pic.planes[c], (y / 2) * cw + x / 2, 1, cw, tc, nfp, nfq, bdc);
}
}
}
// horizontal edges with the q side in this row
let mut bs_h = vec![0i8; w4 * ((y1 - y0) / 4)];
for by in (y0 / 4..y1 / 4).filter(|b| b % 2 == 0 && *b > 0) {
for bx in 0..w4 {
let qi = by * w4 + bx;
let e = pic.blk[qi].edges;
if e & (E_TU_H | E_PU_H) == 0 || pic.blk[qi].flags & F_CODED == 0 {
continue;
}
let (x, y) = (bx * 4, by * 4);
let Some((tc_off, beta_off)) = edge_params(pic, x, y - 1, x, y) else { continue };
let pi = qi - w4;
let bs = boundary_strength(pic, pi, qi, e & E_TU_H != 0);
bs_h[(by - y0 / 4) * w4 + bx] = bs as i8;
if bs == 0 {
continue;
}
let (p, q) = (&pic.blk[pi], &pic.blk[qi]);
let qpl = (p.qp as i32 + q.qp as i32 + 1) >> 1;
let (tc, beta) = tc_beta(qpl, bs, tc_off, beta_off, bd);
let (nfp, nfq) = (p.flags & F_NOFILTER != 0, q.flags & F_NOFILTER != 0);
filter_luma(&mut pic.planes[0], y * w + x, w, 1, tc, beta, nfp, nfq, bd);
}
}
for by in (y0 / 4..y1 / 4).filter(|b| b % 4 == 0 && *b > 0) {
for bx in (0..w4).step_by(2) {
if bs_h[(by - y0 / 4) * w4 + bx] != 2 {
continue;
}
let (x, y) = (bx * 4, by * 4);
let qi = by * w4 + bx;
let Some((tc_off, _)) = edge_params(pic, x, y - 1, x, y) else { continue };
let (p, q) = (pic.blk[qi - w4], pic.blk[qi]);
let (nfp, nfq) = (p.flags & F_NOFILTER != 0, q.flags & F_NOFILTER != 0);
for (c, off) in [(1, cb_off), (2, cr_off)] {
let qpi = ((p.qp as i32 + q.qp as i32 + 1) >> 1) + off;
let qt = (qpc_420(qpi) + 2 + tc_off).clamp(0, 53);
let tc = (TC_TABLE[qt as usize] as i32) << (bdc - 8);
filter_chroma(&mut pic.planes[c], (y / 2) * cw + x / 2, cw, 1, tc, nfp, nfq, bdc);
}
}
}
}
/// Filter one 4-sample luma edge segment. `o` = offset of q0 of the first line, `step` = distance
/// between p/q samples across the edge, `along` = distance between lines.
#[allow(clippy::too_many_arguments)]
fn filter_luma(s: &mut [u16], o: usize, step: usize, along: usize, tc: i32, beta: i32, nfp: bool, nfq: bool, bd: u32) {
let px = |s: &[u16], k: usize, i: usize| s[o + k * along - (i + 1) * step] as i32;
let qx = |s: &[u16], k: usize, i: usize| s[o + k * along + i * step] as i32;
let dp0 = (px(s, 0, 2) - 2 * px(s, 0, 1) + px(s, 0, 0)).abs();
let dp3 = (px(s, 3, 2) - 2 * px(s, 3, 1) + px(s, 3, 0)).abs();
let dq0 = (qx(s, 0, 2) - 2 * qx(s, 0, 1) + qx(s, 0, 0)).abs();
let dq3 = (qx(s, 3, 2) - 2 * qx(s, 3, 1) + qx(s, 3, 0)).abs();
let (dpq0, dpq3) = (dp0 + dq0, dp3 + dq3);
let (dp, dq) = (dp0 + dp3, dq0 + dq3);
let d = dpq0 + dpq3;
if d >= beta {
return;
}
let dsam = |s: &[u16], k: usize, dpq: i32| -> bool {
dpq < (beta >> 2)
&& (px(s, k, 3) - px(s, k, 0)).abs() + (qx(s, k, 0) - qx(s, k, 3)).abs() < (beta >> 3)
&& (px(s, k, 0) - qx(s, k, 0)).abs() < ((5 * tc + 1) >> 1)
};
let strong = dsam(s, 0, 2 * dpq0) && dsam(s, 3, 2 * dpq3);
let dep = dp < ((beta + (beta >> 1)) >> 3);
let deq = dq < ((beta + (beta >> 1)) >> 3);
let max = (1i32 << bd) - 1;
for k in 0..4 {
let base = o + k * along;
let p = |i: usize| s[base - (i + 1) * step] as i32;
let q = |i: usize| s[base + i * step] as i32;
let (p0, p1, p2, p3) = (p(0), p(1), p(2), p(3));
let (q0, q1, q2, q3) = (q(0), q(1), q(2), q(3));
if strong {
let t2 = 2 * tc;
if !nfp {
s[base - step] = ((p2 + 2 * p1 + 2 * p0 + 2 * q0 + q1 + 4) >> 3).clamp(p0 - t2, p0 + t2) as u16;
s[base - 2 * step] = ((p2 + p1 + p0 + q0 + 2) >> 2).clamp(p1 - t2, p1 + t2) as u16;
s[base - 3 * step] = ((2 * p3 + 3 * p2 + p1 + p0 + q0 + 4) >> 3).clamp(p2 - t2, p2 + t2) as u16;
}
if !nfq {
s[base] = ((p1 + 2 * p0 + 2 * q0 + 2 * q1 + q2 + 4) >> 3).clamp(q0 - t2, q0 + t2) as u16;
s[base + step] = ((p0 + q0 + q1 + q2 + 2) >> 2).clamp(q1 - t2, q1 + t2) as u16;
s[base + 2 * step] = ((p0 + q0 + q1 + 3 * q2 + 2 * q3 + 4) >> 3).clamp(q2 - t2, q2 + t2) as u16;
}
} else {
let mut delta = (9 * (q0 - p0) - 3 * (q1 - p1) + 8) >> 4;
if delta.abs() >= tc * 10 {
continue;
}
delta = delta.clamp(-tc, tc);
if !nfp {
s[base - step] = (p0 + delta).clamp(0, max) as u16;
}
if !nfq {
s[base] = (q0 - delta).clamp(0, max) as u16;
}
if dep && !nfp {
let dp = ((((p2 + p0 + 1) >> 1) - p1 + delta) >> 1).clamp(-(tc >> 1), tc >> 1);
s[base - 2 * step] = (p1 + dp).clamp(0, max) as u16;
}
if deq && !nfq {
let dq = ((((q2 + q0 + 1) >> 1) - q1 - delta) >> 1).clamp(-(tc >> 1), tc >> 1);
s[base + step] = (q1 + dq).clamp(0, max) as u16;
}
}
}
}
/// Filter one 4-sample chroma edge segment.
#[allow(clippy::too_many_arguments)]
fn filter_chroma(s: &mut [u16], o: usize, step: usize, along: usize, tc: i32, nfp: bool, nfq: bool, bd: u32) {
let max = (1i32 << bd) - 1;
for k in 0..4 {
let base = o + k * along;
let (p0, p1) = (s[base - step] as i32, s[base - 2 * step] as i32);
let (q0, q1) = (s[base] as i32, s[base + step] as i32);
let delta = ((((q0 - p0) << 2) + p1 - q1 + 4) >> 3).clamp(-tc, tc);
if !nfp {
s[base - step] = (p0 + delta).clamp(0, max) as u16;
}
if !nfq {
s[base] = (q0 - delta).clamp(0, max) as u16;
}
}
}
/// SAO of CTB row `r` from the deblocked planes into new row buffers (luma, Cb, Cr).
fn sao_row(pic: &PicState, r: usize) -> [Vec<u16>; 3] {
let ctb = 1usize << pic.log2_ctb;
let mut out: [Vec<u16>; 3] = [Vec::new(), Vec::new(), Vec::new()];
for c in 0..3 {
let (w, h, cs) = if c == 0 { (pic.width, pic.height, ctb) } else { (pic.cwidth, pic.cheight, ctb / 2) };
let y0 = r * cs;
let y1 = (y0 + cs).min(h);
out[c] = pic.planes[c][y0 * w..y1 * w].to_vec();
for rx in (0..pic.wctb).filter(|_| !pic.draft) {
let rs = r * pic.wctb + rx;
let si = pic.ctb_slice[rs];
if si == u32::MAX {
continue;
}
let sl = &pic.slices[si as usize];
if !(if c == 0 { sl.sao_luma } else { sl.sao_chroma }) {
continue;
}
let p = pic.sao[rs];
if p.type_idx[c] == 0 {
continue;
}
let x0 = rx * cs;
let x1 = (x0 + cs).min(w);
sao_ctb(pic, c, rs, x0, x1, y0, y1, w, h, &p, &mut out[c]);
}
}
out
}
#[allow(clippy::too_many_arguments)]
fn sao_ctb(
pic: &PicState,
c: usize,
rs: usize,
x0: usize,
x1: usize,
y0: usize,
y1: usize,
w: usize,
h: usize,
p: &crate::slicedec::SaoParams,
dst: &mut [u16],
) {
let bd = if c == 0 { pic.bd_y } else { pic.bd_c };
let max = (1i32 << bd) - 1;
let sc = if c == 0 { 0 } else { 1 };
let offs = p.offsets[c].map(|v| v as i32);
// samples of CUs that must not be modified
let nofilter = |pic: &PicState, x: usize, y: usize| pic.blk[((y << sc) >> 2) * pic.w4 + ((x << sc) >> 2)].flags & F_NOFILTER != 0;
let any_nofilter = {
let mut a = false;
for by in ((y0 << sc) >> 2)..(((y1 << sc) + 3) >> 2).min(pic.h4) {
for bx in ((x0 << sc) >> 2)..(((x1 << sc) + 3) >> 2).min(pic.w4) {
a |= pic.blk[by * pic.w4 + bx].flags & F_NOFILTER != 0;
}
}
a
};
if p.type_idx[c] == 1 {
let shift = bd - 5;
let mut table = [0i32; 32];
for k in 0..4 {
table[(k + p.band_pos[c] as usize) & 31] = offs[k];
}
for y in y0..y1 {
if !any_nofilter {
let band = (p.band_pos[c] as i32, offs);
sao_band_run(&pic.planes[c][y * w + x0..y * w + x1], &mut dst[(y - y0) * w + x0..(y - y0) * w + x1], shift, band, max);
continue;
}
for x in x0..x1 {
if nofilter(pic, x, y) {
continue;
}
let v = pic.planes[c][y * w + x] as i32;
dst[(y - y0) * w + x] = (v + table[(v >> shift) as usize]).clamp(0, max) as u16;
}
}
return;
}
let (hp, vp): ([i32; 2], [i32; 2]) = match p.eo_class[c] {
0 => ([-1, 1], [0, 0]),
1 => ([0, 0], [-1, 1]),
2 => ([-1, 1], [-1, 1]),
_ => ([1, -1], [-1, 1]),
};
// Neighbouring CTBs whose samples may not be used (other slice / tile without cross-boundary
// filtering), checked per sample only near CTB borders.
let s = pic.log2_ctb as usize - sc;
let cur_slice = pic.ctb_slice[rs];
let lf_cur = pic.slices[cur_slice as usize].lf_across;
let cur_ts = pic.layout.rs_to_ts[rs];
let cross_ok = |pic: &PicState, xn: usize, yn: usize| -> bool {
let nrs = (yn >> s) * pic.wctb + (xn >> s);
if nrs == rs {
return true;
}
let ns = pic.ctb_slice[nrs];
if ns == u32::MAX {
return false;
}
if pic.ctb_addr[nrs] != pic.ctb_addr[rs] {
// different slice: the later one (in decoding order) decides
let nts = pic.layout.rs_to_ts[nrs];
if nts < cur_ts && !lf_cur {
return false;
}
if cur_ts < nts && !pic.slices[ns as usize].lf_across {
return false;
}
}
if !pic.pps.loop_filter_across_tiles && pic.layout.tile_id[pic.layout.rs_to_ts[nrs] as usize] != pic.layout.tile_id[cur_ts as usize] {
return false;
}
true
};
let edge_map = [1usize, 2, 0, 3, 4];
// Offset per (2 + sum of neighbour signs); edgeIdx 0 adds nothing.
let by_sum = edge_map.map(|e| if e == 0 { 0 } else { offs[e - 1] });
// Any sample: neighbours outside the picture or in a CTB that may not be used leave it as is.
let general = |x: usize, y: usize| -> Option<u16> {
if any_nofilter && nofilter(pic, x, y) {
return None;
}
let mut sum = 0i32;
let v = pic.planes[c][y * w + x] as i32;
for k in 0..2 {
let xn = x as i32 + hp[k];
let yn = y as i32 + vp[k];
if xn < 0 || yn < 0 || xn >= w as i32 || yn >= h as i32 {
return None;
}
let (xn, yn) = (xn as usize, yn as usize);
if (xn < x0 || xn >= x1 || yn < y0 || yn >= y1) && !cross_ok(pic, xn, yn) {
return None;
}
sum += (v - pic.planes[c][yn * w + xn] as i32).signum();
}
Some((v + by_sum[(2 + sum) as usize]).clamp(0, max) as u16)
};
// Samples whose two neighbours lie inside this CTB need none of those checks: a tight loop
// (same arithmetic) for them, the general path for the CTB border.
let (mx, my) = ((hp[0] != 0) as usize, (vp[0] != 0) as usize);
let plane = &pic.planes[c];
for y in y0..y1 {
let inner_row = !any_nofilter && y >= y0 + my && y + my < y1 && x1 - x0 > 2 * mx;
if !inner_row {
for x in x0..x1 {
if let Some(v) = general(x, y) {
dst[(y - y0) * w + x] = v;
}
}
continue;
}
for x in (x0..x0 + mx).chain(x1 - mx..x1) {
if let Some(v) = general(x, y) {
dst[(y - y0) * w + x] = v;
}
}
let (ya, yb) = ((y as i32 + vp[0]) as usize, (y as i32 + vp[1]) as usize);
let (xa, xb) = (x0 as i32 + mx as i32 + hp[0], x0 as i32 + mx as i32 + hp[1]);
let n = x1 - x0 - 2 * mx;
let cur = &plane[y * w + x0 + mx..][..n];
let na = &plane[ya * w + xa as usize..][..n];
let nb = &plane[yb * w + xb as usize..][..n];
let out = &mut dst[(y - y0) * w + x0 + mx..][..n];
sao_edge_run(cur, na, nb, out, &by_sum, max);
}
}
/// Lanes of the SAO loops (16-bit arithmetic: samples are at most 12 bits, offsets 7 bits).
const SAO_LANES: usize = 16;
/// Edge offset of a run of samples whose two neighbours (`na`, `nb`) are all usable: per lane
/// the sum of the two neighbour signs selects the offset (no table lookup), so the loop
/// vectorises; bit-exact with the per-sample form.
#[inline(always)]
fn sao_edge_run(cur: &[u16], na: &[u16], nb: &[u16], out: &mut [u16], by_sum: &[i32; 5], max: i32) {
let o = by_sum.map(|v| v as i16);
let max = max as i16;
let lane = |v: i16, a: i16, b: i16| -> u16 {
let s = ((v > a) as i16 - (v < a) as i16) + ((v > b) as i16 - (v < b) as i16);
let m = |k: i16| -((s == k) as i16);
let off = (o[0] & m(-2)) | (o[1] & m(-1)) | (o[3] & m(1)) | (o[4] & m(2));
(v + off).max(0).min(max) as u16
};
let n = cur.len();
let full = n - n % SAO_LANES;
for i in (0..full).step_by(SAO_LANES) {
let (Some(c), Some(a), Some(b), Some(d)) = (lanes(cur, i), lanes(na, i), lanes(nb, i), lanes_mut(out, i)) else {
return;
};
for l in 0..SAO_LANES {
d[l] = lane(c[l] as i16, a[l] as i16, b[l] as i16);
}
}
for i in full..n {
out[i] = lane(cur[i] as i16, na[i] as i16, nb[i] as i16);
}
}
/// `SAO_LANES` samples from `i`, if all present.
#[inline(always)]
fn lanes(s: &[u16], i: usize) -> Option<&[u16; SAO_LANES]> {
s.get(i..)?.first_chunk()
}
#[inline(always)]
fn lanes_mut(s: &mut [u16], i: usize) -> Option<&mut [u16; SAO_LANES]> {
s.get_mut(i..)?.first_chunk_mut()
}
/// Band offset of a run of samples: the band index relative to `band_pos` selects one of the
/// four offsets per lane.
#[inline(always)]
fn sao_band_run(src: &[u16], out: &mut [u16], shift: u32, (band_pos, offs): (i32, [i32; 4]), max: i32) {
let o = offs.map(|v| v as i16);
let (bp, max) = (band_pos as i16, max as i16);
let lane = |v: i16| -> u16 {
let k = ((v >> shift) - bp) & 31;
let m = |j: i16| -((k == j) as i16);
let off = (o[0] & m(0)) | (o[1] & m(1)) | (o[2] & m(2)) | (o[3] & m(3));
(v + off).max(0).min(max) as u16
};
let n = src.len();
let full = n - n % SAO_LANES;
for i in (0..full).step_by(SAO_LANES) {
let (Some(c), Some(d)) = (lanes(src, i), lanes_mut(out, i)) else {
return;
};
for l in 0..SAO_LANES {
d[l] = lane(c[l] as i16);
}
}
for i in full..n {
out[i] = lane(src[i] as i16);
}
}
/// Publish CTB row `r` (final samples + compressed motion) into the frame.
fn publish_row(pic: &mut PicState, r: usize) {
let ctb = 1usize << pic.log2_ctb;
let (y0, y1) = (r * ctb, ((r + 1) * ctb).min(pic.height));
let (c0, c1) = (y0 / 2, y1.div_ceil(2));
let (w, cw) = (pic.width, pic.cwidth);
let w16 = pic.width.div_ceil(16);
let rows16 = (y1 - y0).div_ceil(16);
let mut col = vec![ColMv::default(); w16 * rows16];
for j in 0..rows16 {
for i in 0..w16 {
let (x, y) = (i * 16, y0 + j * 16);
let bi = (y >> 2) * pic.w4 + (x >> 2);
let b = &pic.blk[bi];
if b.flags & F_CODED == 0 || b.flags & F_INTRA != 0 {
continue;
}
let f = pic.mvf[bi];
let ctbi = (y >> pic.log2_ctb) * pic.wctb + (x >> pic.log2_ctb);
let si = pic.ctb_slice[ctbi];
if si == u32::MAX {
continue;
}
let sl = &pic.slices[si as usize];
let mut cm = ColMv { mv: f.mv, ref_poc: [0; 2], flags: 0 };
for l in 0..2 {
if f.pred(l) {
let ri = f.ref_idx[l] as usize;
if ri < sl.ref_pocs[l].len() {
cm.flags |= 1 << l;
cm.ref_poc[l] = sl.ref_pocs[l][ri];
if sl.ref_lt[l][ri] {
cm.flags |= 4 << l;
}
}
}
}
col[j * w16 + i] = cm;
}
}
let [oy, ocb, ocr] = sao_row(pic, r);
let _ = (w, cw, c0, c1);
let row = FrameRow { y: oy.into(), cb: ocb.into(), cr: ocr.into(), col: col.into() };
pic.frame.publish(r, row);
}
-190
View File
@@ -1,190 +0,0 @@
//! Fractional sample interpolation (8.5.3.3.3) and weighted sample prediction (8.5.3.3.4).
use crate::picture::Frame;
use crate::spec_tables::{CHROMA_FILTER, LUMA_FILTER};
/// Max prediction block size + filter margin.
const WIN: usize = 64 + 7;
/// Reference windows and first-pass rows, reused between blocks (no per-block zeroing).
pub struct McScratch {
win: Vec<i16>,
tmp: Vec<i16>,
}
impl Default for McScratch {
fn default() -> Self {
McScratch { win: vec![0; WIN * WIN], tmp: vec![0; WIN * WIN] }
}
}
/// `out[j] = (sum_k f[k] * src[j + k * step]) >> shift` for `W` outputs: taps outer, outputs
/// inner, so each tap is one vector multiply-add across the row (exact integer sums).
#[inline(always)]
fn filt_w<const T: usize, const W: usize>(src: &[i16], step: usize, f: &[i8; T], shift: u32, out: &mut [i16]) {
let mut acc = [0i32; W];
for k in 0..T {
let c = f[k] as i32;
let Some(p) = src.get(k * step..).and_then(<[_]>::first_chunk::<W>) else {
return;
};
for j in 0..W {
acc[j] += c * p[j] as i32;
}
}
let Some(o) = out.first_chunk_mut::<W>() else {
return;
};
for j in 0..W {
o[j] = (acc[j] >> shift) as i16;
}
}
/// [`filt_w`] for a run of `w` outputs (prediction block widths are fixed sizes).
#[inline(always)]
fn filt<const T: usize>(src: &[i16], step: usize, f: &[i8; T], shift: u32, out: &mut [i16], w: usize) {
match w {
2 => filt_w::<T, 2>(src, step, f, shift, out),
4 => filt_w::<T, 4>(src, step, f, shift, out),
6 => filt_w::<T, 6>(src, step, f, shift, out),
8 => filt_w::<T, 8>(src, step, f, shift, out),
12 => filt_w::<T, 12>(src, step, f, shift, out),
16 => filt_w::<T, 16>(src, step, f, shift, out),
24 => filt_w::<T, 24>(src, step, f, shift, out),
32 => filt_w::<T, 32>(src, step, f, shift, out),
48 => filt_w::<T, 48>(src, step, f, shift, out),
64 => filt_w::<T, 64>(src, step, f, shift, out),
_ => {
for (j, o) in out[..w].iter_mut().enumerate() {
let s: i32 = (0..T).map(|k| f[k] as i32 * src[j + k * step] as i32).sum();
*o = (s >> shift) as i16;
}
}
}
}
/// Separable sub-sample interpolation of a w x h block from `win` (stride WIN, the block's
/// top-left sample at (m, m) for a T-tap filter with m = T / 2 - 1).
#[allow(clippy::too_many_arguments)]
fn interpolate<const T: usize>(
win: &[i16],
tmp: &mut [i16],
fx: usize,
fy: usize,
hf: &[i8; T],
vf: &[i8; T],
w: usize,
h: usize,
shift1: u32,
shift3: u32,
out: &mut [i16],
) {
let m = T / 2 - 1;
if fx == 0 && fy == 0 {
for r in 0..h {
for (o, &s) in out[r * w..r * w + w].iter_mut().zip(&win[(r + m) * WIN + m..]) {
*o = s << shift3;
}
}
} else if fy == 0 {
for r in 0..h {
filt(&win[(r + m) * WIN..], 1, hf, shift1, &mut out[r * w..], w);
}
} else if fx == 0 {
for r in 0..h {
filt(&win[r * WIN + m..], WIN, vf, shift1, &mut out[r * w..], w);
}
} else {
for r in 0..h + T - 1 {
filt(&win[r * WIN..], 1, hf, shift1, &mut tmp[r * WIN..], w);
}
for r in 0..h {
filt(&tmp[r * WIN..], WIN, vf, 6, &mut out[r * w..], w);
}
}
}
/// Luma prediction samples (14-bit intermediate precision) of a w x h block at (x, y) displaced by the
/// quarter-sample motion vector `mv`, into `out` (stride w).
#[allow(clippy::too_many_arguments)]
pub fn mc_luma(f: &Frame, x: i32, y: i32, mv: [i16; 2], w: usize, h: usize, out: &mut [i16], sc: &mut McScratch) {
let bd = f.bit_depth;
let (fx, fy) = ((mv[0] & 3) as usize, (mv[1] & 3) as usize);
let xi = x + (mv[0] as i32 >> 2);
let yi = y + (mv[1] as i32 >> 2);
let shift1 = bd.min(12) - 8;
let shift3 = 14 - bd;
f.luma_window(xi - 3, yi - 3, w + 7, h + 7, &mut sc.win, WIN);
interpolate::<8>(&sc.win, &mut sc.tmp, fx, fy, &LUMA_FILTER[fx], &LUMA_FILTER[fy], w, h, shift1, shift3, out);
}
/// Chroma prediction samples of a w x h chroma block at chroma position (x, y) with a 1/8-sample
/// vector (4:2:0).
#[allow(clippy::too_many_arguments)]
pub fn mc_chroma(f: &Frame, c: usize, x: i32, y: i32, mv: [i16; 2], w: usize, h: usize, out: &mut [i16], sc: &mut McScratch) {
let bd = f.bit_depth_c;
let (fx, fy) = ((mv[0] & 7) as usize, (mv[1] & 7) as usize);
let xi = x + (mv[0] as i32 >> 3);
let yi = y + (mv[1] as i32 >> 3);
let shift1 = bd.min(12) - 8;
let shift3 = 14 - bd;
f.chroma_window(c, (xi - 1, yi - 1), (w + 3, h + 3), &mut sc.win, WIN);
interpolate::<4>(&sc.win, &mut sc.tmp, fx, fy, &CHROMA_FILTER[fx], &CHROMA_FILTER[fy], w, h, shift1, shift3, out);
}
/// Default weighted prediction, one list (8-262).
pub fn put_uni(p: &[i16], w: usize, h: usize, bd: u32, dst: &mut [u16], ds: usize) {
let shift = 14 - bd;
let off = if shift > 0 { 1 << (shift - 1) } else { 0 };
let max = (1i32 << bd) - 1;
// Rows as slices and min / max clipping (`clamp` asserts its bounds) so the loop vectorises.
for (d, s) in dst.chunks_mut(ds).zip(p[..w * h].chunks(w)) {
for (d, &s) in d[..w].iter_mut().zip(s) {
*d = ((s as i32 + off) >> shift).max(0).min(max) as u16;
}
}
}
/// Default weighted prediction, bi-prediction (8-263).
pub fn put_bi(p0: &[i16], p1: &[i16], w: usize, h: usize, bd: u32, dst: &mut [u16], ds: usize) {
let shift = 15 - bd;
let off = 1 << (shift - 1);
let max = (1i32 << bd) - 1;
for ((d, a), b) in dst.chunks_mut(ds).zip(p0[..w * h].chunks(w)).zip(p1[..w * h].chunks(w)) {
for ((d, &a), &b) in d[..w].iter_mut().zip(a).zip(b) {
*d = ((a as i32 + b as i32 + off) >> shift).max(0).min(max) as u16;
}
}
}
/// Explicit weighted prediction, one list (8-265). `o` is the offset already scaled to the bit depth.
pub fn put_weighted_uni(p: &[i16], (w, h): (usize, usize), bd: u32, (log2wd, wt, o): (u32, i32, i32), dst: &mut [u16], ds: usize) {
let max = (1i32 << bd) - 1;
for r in 0..h {
for c in 0..w {
let v = p[r * w + c] as i32 * wt;
let v = if log2wd >= 1 { ((v + (1 << (log2wd - 1))) >> log2wd) + o } else { v + o };
dst[r * ds + c] = v.max(0).min(max) as u16;
}
}
}
/// Explicit weighted bi-prediction (8-266).
pub fn put_weighted_bi(
p0: &[i16],
p1: &[i16],
(w, h): (usize, usize),
bd: u32,
(log2wd, w0, w1, o0, o1): (u32, i32, i32, i32, i32),
dst: &mut [u16],
ds: usize,
) {
let max = (1i32 << bd) - 1;
for r in 0..h {
for c in 0..w {
let i = r * w + c;
let v = (p0[i] as i32 * w0 + p1[i] as i32 * w1 + ((o0 + o1 + 1) << log2wd)) >> (log2wd + 1);
dst[r * ds + c] = v.max(0).min(max) as u16;
}
}
}
-253
View File
@@ -1,253 +0,0 @@
//! Intra sample prediction (8.4.4.2): reference substitution, filtering (incl. strong intra
//! smoothing), planar, DC and the 33 angular modes.
use crate::spec_tables::{INTRA_PRED_ANGLE, INV_ANGLE};
/// Reference samples of an nTbS block: `left[0]` = `top[0]` = p[-1][-1], `left[1 + y]` = p[-1][y],
/// `top[1 + x]` = p[x][-1] for x, y = 0..2*nTbS-1.
#[derive(Clone)]
pub struct Refs {
pub left: [u16; 129],
pub top: [u16; 129],
}
impl Default for Refs {
fn default() -> Self {
Refs { left: [0; 129], top: [0; 129] }
}
}
/// Substitution process (8.4.4.2.2). `avail_left[1 + y]` / `avail_top[1 + x]` (index 0 = corner) tell
/// which samples are available; unavailable ones are replaced.
pub fn substitute(r: &mut Refs, avail_left: &[bool; 129], avail_top: &[bool; 129], n: usize, bit_depth: u32) {
let n2 = 2 * n;
// Sequence order: p[-1][2n-1] .. p[-1][-1], p[0][-1] .. p[2n-1][-1]
let any = avail_left[..=n2].iter().any(|&a| a) || avail_top[1..=n2].iter().any(|&a| a);
if !any {
let v = 1u16 << (bit_depth - 1);
r.left[..=n2].fill(v);
r.top[..=n2].fill(v);
return;
}
if !avail_left[n2] {
// search upwards then along the top
let mut found = None;
for y in (0..n2).rev() {
if avail_left[y] {
found = Some(r.left[y]);
break;
}
}
if found.is_none() {
for x in 1..=n2 {
if avail_top[x] {
found = Some(r.top[x]);
break;
}
}
}
r.left[n2] = found.unwrap_or(0);
}
for y in (0..n2).rev() {
if !avail_left[y] {
r.left[y] = r.left[y + 1];
}
}
r.top[0] = r.left[0];
for x in 1..=n2 {
if !avail_top[x] {
r.top[x] = r.top[x - 1];
}
}
}
/// Filtering process of neighbouring samples (8.4.4.2.3) for luma (and 4:4:4 chroma).
pub fn filter(r: &mut Refs, mode: u32, n: usize, strong_enabled: bool, is_luma: bool, bit_depth: u32) {
if mode == 1 || n == 4 {
return;
}
let min_dist = (mode as i32 - 26).abs().min((mode as i32 - 10).abs());
let thres = match n {
8 => 7,
16 => 1,
_ => 0,
};
if min_dist <= thres {
return;
}
let n2 = 2 * n;
let c = r.left[0] as i32;
if strong_enabled && is_luma && n == 32 {
let (bl, tr) = (r.left[n2] as i32, r.top[n2] as i32);
let thr = 1 << (bit_depth - 5);
if (c + tr - 2 * r.top[n] as i32).abs() < thr && (c + bl - 2 * r.left[n] as i32).abs() < thr {
for i in 0..63 {
r.left[1 + i] = (((63 - i as i32) * c + (i as i32 + 1) * bl + 32) >> 6) as u16;
r.top[1 + i] = (((63 - i as i32) * c + (i as i32 + 1) * tr + 32) >> 6) as u16;
}
return;
}
}
let (l, t) = (r.left, r.top);
let corner = ((l[1] as u32 + 2 * c as u32 + t[1] as u32 + 2) >> 2) as u16;
for i in 1..n2 {
r.left[i] = ((l[i - 1] as u32 + 2 * l[i] as u32 + l[i + 1] as u32 + 2) >> 2) as u16;
r.top[i] = ((t[i - 1] as u32 + 2 * t[i] as u32 + t[i + 1] as u32 + 2) >> 2) as u16;
}
r.left[0] = corner;
r.top[0] = corner;
}
/// Predict an n x n block with `mode` into `out` (stride `os`). `edge_filters`: DC / horizontal /
/// vertical boundary smoothing (luma blocks smaller than 32x32).
pub fn predict(r: &Refs, mode: u32, n: usize, edge_filters: bool, bit_depth: u32, out: &mut [u16], os: usize) {
let max = (1i32 << bit_depth) - 1;
match mode {
0 => {
// planar
let log2 = n.trailing_zeros();
let tr = r.top[1 + n] as i32;
let bl = r.left[1 + n] as i32;
for y in 0..n {
let l = r.left[1 + y] as i32;
for x in 0..n {
let t = r.top[1 + x] as i32;
let v = ((n - 1 - x) as i32 * l + (x as i32 + 1) * tr + (n - 1 - y) as i32 * t + (y as i32 + 1) * bl + n as i32) >> (log2 + 1);
out[y * os + x] = v as u16;
}
}
}
1 => {
let log2 = n.trailing_zeros();
let sum: u32 = r.top[1..=n].iter().map(|&v| v as u32).sum::<u32>() + r.left[1..=n].iter().map(|&v| v as u32).sum::<u32>();
let dc = ((sum + n as u32) >> (log2 + 1)) as u16;
for y in 0..n {
out[y * os..y * os + n].fill(dc);
}
if edge_filters && n < 32 {
let d = dc as u32;
out[0] = ((r.left[1] as u32 + 2 * d + r.top[1] as u32 + 2) >> 2) as u16;
for x in 1..n {
out[x] = ((r.top[1 + x] as u32 + 3 * d + 2) >> 2) as u16;
}
for y in 1..n {
out[y * os] = ((r.left[1 + y] as u32 + 3 * d + 2) >> 2) as u16;
}
}
}
_ => {
let angle = INTRA_PRED_ANGLE[mode as usize] as i32;
// reference array ref[-n..=2n] stored at offset n
let mut refa = [0i32; 3 * 64 + 1];
let base = n as i32;
let (main, side) = if mode >= 18 { (&r.top, &r.left) } else { (&r.left, &r.top) };
for x in 0..=n {
refa[(base + x as i32) as usize] = main[x] as i32; // ref[x] = p[-1+x][-1]
}
if angle < 0 {
let lim = (n as i32 * angle) >> 5;
if lim < -1 {
let inv = INV_ANGLE[(mode - 11) as usize] as i32;
for x in lim..=-1 {
let idx = -1 + ((x * inv + 128) >> 8);
refa[(base + x) as usize] = side[(idx + 1) as usize] as i32;
}
}
} else {
for x in n + 1..=2 * n {
refa[(base + x as i32) as usize] = main[x] as i32;
}
}
let horizontal = mode < 18;
for j in 0..n {
let pos = (j as i32 + 1) * angle;
let idx = pos >> 5;
let fact = pos & 31;
for i in 0..n {
let k = (base + i as i32 + idx + 1) as usize;
let v = if fact != 0 { ((32 - fact) * refa[k] + fact * refa[k + 1] + 16) >> 5 } else { refa[k] };
if horizontal {
out[i * os + j] = v as u16;
} else {
out[j * os + i] = v as u16;
}
}
}
if edge_filters && n < 32 {
if mode == 26 {
for y in 0..n {
let v = r.top[1] as i32 + ((r.left[1 + y] as i32 - r.left[0] as i32) >> 1);
out[y * os] = v.clamp(0, max) as u16;
}
} else if mode == 10 {
for x in 0..n {
let v = r.left[1] as i32 + ((r.top[1 + x] as i32 - r.top[0] as i32) >> 1);
out[x] = v.clamp(0, max) as u16;
}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn refs(f: impl Fn(i32) -> u16) -> Refs {
let mut r = Refs::default();
r.left[0] = f(0);
r.top[0] = f(0);
for i in 1..129 {
r.left[i] = f(-(i as i32));
r.top[i] = f(i as i32);
}
r
}
#[test]
fn dc_and_planar_flat() {
let r = refs(|_| 100);
let mut out = [0u16; 64];
predict(&r, 1, 8, true, 8, &mut out, 8);
assert!(out.iter().all(|&v| v == 100));
predict(&r, 0, 8, true, 8, &mut out, 8);
assert!(out.iter().all(|&v| v == 100));
}
#[test]
fn pure_vertical_and_horizontal() {
let r = refs(|i| (128 + i) as u16);
let mut out = [0u16; 16];
predict(&r, 26, 4, false, 8, &mut out, 4);
for y in 0..4 {
for x in 0..4 {
assert_eq!(out[y * 4 + x], r.top[1 + x]);
}
}
predict(&r, 10, 4, false, 8, &mut out, 4);
for y in 0..4 {
for x in 0..4 {
assert_eq!(out[y * 4 + x], r.left[1 + y]);
}
}
// diagonal mode 34 reads top[x + y + 2]
predict(&r, 34, 4, false, 8, &mut out, 4);
assert_eq!(out[4 + 1], r.top[1 + 1 + 1 + 1]);
}
#[test]
fn substitution() {
let mut r = Refs::default();
let mut al = [false; 129];
let at = [false; 129];
r.left[3] = 77;
al[3] = true;
substitute(&mut r, &al, &at, 4, 8);
assert!(r.left[..=8].iter().all(|&v| v == 77));
assert!(r.top[..=8].iter().all(|&v| v == 77));
let mut r = Refs::default();
substitute(&mut r, &[false; 129], &[false; 129], 4, 10);
assert_eq!(r.top[5], 512);
}
}
-132
View File
@@ -1,132 +0,0 @@
//! Clean-room pure-Rust H.265 / HEVC decoder, implemented from ITU-T Rec. H.265 (ISO/IEC 23008-2).
//!
//! Supported: Main and Main 10 profiles (4:2:0, 8/10-bit; up to 12-bit 4:2:0 without range-extension
//! tools), all coding tools of those profiles: CABAC, coding / transform quadtrees, AMP, PCM,
//! transform skip, scaling lists, sign data hiding, 35 intra modes with strong intra smoothing,
//! merge / AMVP / TMVP, weighted prediction, deblocking, SAO, tiles, wavefront parallel processing
//! (parsing), dependent slice segments, short- and long-term reference picture sets.
//!
//! ```no_run
//! let mut dec = deckcraft_hevc::Decoder::new();
//! let stream = std::fs::read("video.h265").unwrap();
//! for pic in dec.decode(&stream, 0).unwrap() {
//! println!("{}x{} poc {}", pic.width, pic.height, pic.poc);
//! }
//! for pic in dec.flush() {
//! let _ = pic;
//! }
//! ```
// Index loops over fixed-size blocks read more clearly than iterator chains in codec code.
#![allow(clippy::needless_range_loop, clippy::too_many_arguments)]
#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))]
mod cabac;
mod decoder;
mod dpb;
mod error;
mod filter;
mod inter;
mod intra;
mod mvpred;
pub mod params;
mod picture;
pub mod slice;
mod slicedec;
#[rustfmt::skip]
#[allow(dead_code)]
mod spec_tables;
#[cfg(test)]
mod synth_tests;
mod tables;
mod transform;
pub use decoder::{DecodeStats, Decoder};
pub use error::{Error, Result};
/// Colour description from the VUI (ITU-T H.273 code points).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ColorInfo {
/// `video_full_range_flag`.
pub full_range: bool,
pub primaries: u8,
pub transfer: u8,
pub matrix: u8,
}
/// Samples of one plane: 8-bit streams produce `U8`, higher bit depths `U16` (value range
/// `0..(1 << bit_depth)`).
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Plane {
U8(Vec<u8>),
U16(Vec<u16>),
}
impl Plane {
pub fn len(&self) -> usize {
match self {
Plane::U8(v) => v.len(),
Plane::U16(v) => v.len(),
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn as_u8(&self) -> Option<&[u8]> {
match self {
Plane::U8(v) => Some(v),
Plane::U16(_) => None,
}
}
pub fn as_u16(&self) -> Option<&[u16]> {
match self {
Plane::U8(_) => None,
Plane::U16(v) => Some(v),
}
}
/// Sample `i` widened to u16.
pub fn get(&self, i: usize) -> u16 {
match self {
Plane::U8(v) => v[i] as u16,
Plane::U16(v) => v[i],
}
}
/// Raw bytes as written by ffmpeg `rawvideo` (8-bit, or 16-bit little endian).
pub fn to_le_bytes(&self) -> Vec<u8> {
match self {
Plane::U8(v) => v.clone(),
Plane::U16(v) => v.iter().flat_map(|s| s.to_le_bytes()).collect(),
}
}
}
/// A decoded, cropped picture in planar 4:2:0.
#[derive(Clone, Debug)]
pub struct Picture {
/// Cropped luma width.
pub width: u32,
/// Cropped luma height.
pub height: u32,
pub chroma_width: u32,
pub chroma_height: u32,
/// Luma bit depth (chroma uses the same storage type).
pub bit_depth: u32,
pub y: Plane,
pub u: Plane,
pub v: Plane,
/// Strides in samples.
pub y_stride: usize,
pub uv_stride: usize,
/// Presentation timestamp passed to [`Decoder::decode`] with the access unit of this picture.
pub pts: i64,
/// Picture order count.
pub poc: i32,
/// IRAP (IDR / CRA / BLA) picture.
pub key: bool,
pub color: ColorInfo,
/// Sample aspect ratio (0, 0 when unspecified).
pub sar: (u16, u16),
/// Decoded in draft mode without deblocking / SAO ([`Decoder::set_draft`]): approximate,
/// for reduced-resolution playback only. Never set without draft mode.
pub draft: bool,
}

Some files were not shown because too many files have changed in this diff Show More