Compare commits

...
Author SHA1 Message Date
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. 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
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 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
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 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
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
Dylan M. Taylor 0623b43032 Merge pull request #1985 from dotCooCoo/chore/craft-update-1b34eba41
Updates: take craft-update from craft-libs main at 1b34eba41
2026-11-08 22:55:36 -05:00
dotCooCoo 5ee82b4e45 Updates: take craft-update from craft-libs main at 1b34eba41 2026-11-08 19:43:26 -08:00
Dylan M. Taylor 61423ea35b Merge pull request #1980 from dotCooCoo/chore/craft-update-updater-fixes
Updates: the updater reports installing, ready, installed and failed, and installs at exit
2026-11-08 22:12:17 -05:00
aiongg c4674f24fb Merge pull request #1982 from Silvertree2010/fix/h13151-polynomial-trendline-fit-fails-f
Make polynomial trendline fits independent of x scale and offset
2026-11-08 15:46:38 -05:00
NetterSilverandClaude Opus 5.5 4ba9a1a5d9 Make polynomial trendline fits independent of x scale and offset
Found while testing crates/model/src/trendline.rs:~419 (solve, used by polyfit)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-08 21:33:22 +01:00
dotCooCoo 3504a9da42 xtask: pdf-deps expects craft-pdf and krilla from the moved craft-libs pin
The pdf-deps check compared craft-pdf and krilla against storytold/craft-libs
at c5a1cbfb, so the gate stopped at that step once the pin moved to
dotCooCoo/craft-libs c7c84230. The expected repository and revision now
match the pin in Cargo.toml.
2026-11-08 12:11:03 -08:00
dotCooCoo ccab980657 Updates: the updater reports installing, ready, installed and failed, and installs at exit
craft-media, craft-pdf, craft-ui, craft-update and krilla move from craft-libs
c5a1cbfb to c7c84230, which carries storytold/craft-libs#687 and #696.

The check result window now shows whenever the updater's status is not
idle: after Help > Check for Updates, and at the first start after an
update, to say that it installed or why it failed. Restart to Update in
that window asks the app to close once nothing is unsaved. on_exit starts
the helper of a ready update, since on macOS it ends the process before
the app is dropped.
2026-11-08 11:30:36 -08:00
aiongg 48e2203123 Merge pull request #1969 from Silvertree2010/fix/h11403-sentence-case-capitalizes-the-wo
Don't capitalize the word after a decimal point in sentence case
2026-11-08 03:35:42 -05:00
NetterSilverandClaude Haiku 5.5 76bd26cca0 Don't capitalize the word after a decimal point in sentence case
Found while testing crates/model/src/edit.rs:~505 (change_case_parts, sentence branch)

Co-Authored-By: Claude Haiku 5.5 <[email protected]>
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-08 08:26:05 +00:00
Nasim 02fbb6a976 Notes: pin that Paste reaches the focused speaker-notes field, bare and inside the ⌘V batch (refs #1855) (#1856) 2026-11-08 01:59:22 -05:00
aiongg c16db2ef3e Merge pull request #1943 from neilcawse/fix/pptx-body-and-group-3d
pptx: preserve 3-D scene and extrusion XML on text bodies and groups
2026-11-08 01:18:16 -05:00
aiongg 809c44654d Apply review fixes
- crates/pptx/src/write/shapes.rs
2026-11-08 05:59:15 +00:00
aiongg 220b52dfdb Apply review fixes
- docs/file-format-parity.md
2026-11-08 05:43:41 +00:00
Neil Cawse 424f575ca5 fix(pptx): bound and validate body/group raw3d XML on read/write and expand 3-D tests 2026-11-08 05:26:33 +00:00
Neil Cawse aefd54d28e pptx: preserve 3-D scene and extrusion XML on text bodies and groups 2026-11-08 05:24:59 +00:00
Nasim 25809faf1a Accessibility: name ribbon drop-downs by purpose and expose the shown text as their value (#1956) (#1963) 2026-11-08 00:18:14 -05:00
aiongg fc35ad67c6 Merge pull request #1950 from Silvertree2010/fix/h9583-format-paragraph-silently-left-a
Fix format.paragraph silently left-aligning known align names
2026-11-07 15:10:55 -05:00
NetterSilverandClaude Haiku 5.5 57780f9e61 Fix format.paragraph silently left-aligning known align names
Found while testing crates/engine/src/cmd/format/paragraph.rs:123-129

Co-Authored-By: Claude Haiku 5.5 <[email protected]>
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-07 20:00:33 +00:00
FredApps 0627fb2589 Merge pull request #1932 from helenkwok/web/page-api
deckcraft-web: window.deckcraft, the page command API from craft-web-api
2026-11-07 15:37:54 +01:00
aiongg 170c5591b5 Merge pull request #1934 from Silvertree2010/fix/h7742-shape-adjust-with-an-invalid-neg
Reject invalid index in shape.adjust and shape.quickStyle
2026-11-07 03:34:42 -05:00
NetterSilverandClaude Haiku 5.5 a883d6b2d9 Reject invalid index in shape.adjust and shape.quickStyle
Found while testing crates/engine/src/cmd/shape/appearance.rs:8 (adjust, index via usize_param); same pattern in quick_style at :559

Co-Authored-By: Claude Haiku 5.5 <[email protected]>
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-07 08:14:47 +00:00
Helen Kwok 4b1e50a6b0 deckcraft-web: put each Cargo.toml comment above its own dependency, and say a call the app already took still finishes after a timeout 2026-11-07 10:28:04 +08:00
Helen Kwok c7919102ac deckcraft-web: window.deckcraft, the page command API from craft-web-api
The web shell queues the page's calls as control requests (the channel the desktop control server uses) and answers them on the next frame. The API is installed in every mode; opening, saving, the file name and the unsaved flag stay with the host messages.
2026-11-07 08:05:09 +08:00
FredApps 159ed07eb3 Merge pull request #1918 from storytold/house-rules
Copy the shared house rules into AGENTS.md
2026-11-06 15:00:00 +01:00
FredApps de9e79a445 docs: preserve local guidance in house-rules rollout 2026-11-06 12:36:46 +01:00
FredApps d7343553b3 Copy the shared house rules into AGENTS.md.
The blocks come from craft-contrib at dde09ea31e2e457f5b4653a6e76f7cdf55944bdf. CLAUDE.md imports AGENTS.md. The house-rules workflow checks the copy.
2026-11-06 11:50:01 +01:00
Luis 9a95c3a639 Release: refresh contributor credits for each release (#1889) (#1890)
About > Contributors still showed the snapshot taken for v0.4.0, because
nothing regenerated contributors/contributors.json before a release.
Opt in to the shared refresh from craft-common-actions: a read-only
credits job regenerates the snapshot for the release commit, and the
shared release workflow installs it before every source build. The
snapshot is not committed back.
2026-11-05 17:23:44 +01:00
FredApps daeedd5d19 Merge pull request #1895 from helenkwok/web/host-crate
deckcraft-web: host messages through craft-web-host, and page host mode
2026-11-05 14:11:06 +01:00
FredAppsandCursor a2c1633fe3 fix(web): refuse a host file of exactly 2 GiB
wasm32 cannot allocate a Vec of 2 GiB (isize::MAX is one byte less), so
the open check that only rejected a larger file trapped on that boundary.
Refuse 2 GiB as well, and say so in the failure the host receives.

Co-authored-by: Cursor <[email protected]>
2026-11-05 13:59:10 +01:00
aiongg bfdcc5cdfa Apply review fixes
- packaging/web/README.md
- apps/deckcraft-web/src/host.rs
2026-11-05 11:55:08 +00:00
Michael Müllerandaiongg 73de9900a7 Show: Presenter View only with a second display (#1156)
* Show: Presenter View only with a second display

With one display the presenter window opened on the same screen as the show. Under Hyprland's
focus-follows-mouse, whichever window the pointer was over took full screen, and the hidden one
stalled every frame: the show lagged, dropped key presses or froze.

One display now shows only the slides, as in PowerPoint. The host counts displays through winit's
monitor list when a show starts; hosts that can't tell keep Presenter View.

* Show: count displays by MonitorHandle

Wayland lists one output twice under the same wl_output id, so comparing handles counts it once, and two separate displays with the same name and geometry still count as two.

* Show: docs for Presenter View with a second display

slideshow-parity and hardware-parity note that on Linux one display shows only the slides, with status lines and revision history; ROADMAP progress log. display_count collects into a BTreeSet (MonitorHandle's Ord matches its Eq).

* Show: docs state the one-display rule exactly

Presenter View stays off when winit lists exactly one display; if it lists none, the setting is kept. The host callback covers Unix other than macOS, so the docs say Linux and the BSDs.

* Apply review fixes

- docs/slideshow-parity.md

---------

Co-authored-by: aiongg <[email protected]>
2026-11-05 12:51:16 +01:00
aiongg b225bb16a1 Apply review fixes
- apps/deckcraft-web/src/host.rs
2026-11-05 11:49:17 +00:00
Helen Kwok 9299113268 deckcraft-web: speak the host messages through craft-web-host, and add page host mode
The transports, the message types and the origin checks come from the shared crate (storytold/craft-libs#420). `data-host="page"` on the canvas now hosts DeckCraft in a page's own document, as in the other apps; the parent bridge keeps its behaviour.
2026-11-05 17:51:01 +08:00
FredApps 2b6d27d525 Merge pull request #1658 from SSchott/copy-the-fix
Fix Copying Images, Ctrl-C/Ctrl-V and within Deckcraft
2026-11-05 05:59:48 +01:00
FredApps 9998ee5e07 Merge pull request #1809 from jimprince/web-host-bridge
Web: open and save through a framing page (?host=parent)
2026-11-05 05:59:44 +01:00
FredApps 9e8aa81298 Merge remote-tracking branch 'origin/main' into copy-the-fix 2026-11-05 05:49:57 +01:00
FredApps 6b5faf6d51 Merge remote-tracking branch 'origin/main' into web-host-bridge 2026-11-05 05:49:57 +01:00
FredApps 0b5cd0f2d6 Merge pull request #1876 from zberry/connector-glue-in-groups
shape.connect glues to a shape inside a group, and the connector follows it
2026-11-05 05:43:49 +01:00
FredApps 4f171e8f78 Merge remote-tracking branch 'origin/main' into copy-the-fix 2026-11-05 05:41:44 +01:00
FredApps edeb99b00e Merge remote-tracking branch 'origin/main' into web-host-bridge 2026-11-05 05:41:44 +01:00
FredApps 29162e66ad Merge main into connector-glue-in-groups (keep both tests) 2026-11-05 05:32:34 +01:00
FredApps 8435cc301f Merge pull request #1886 from sbjwin/fix/thumbnail-grayscale
UI: follow Grayscale view mode in slide thumbnails
2026-11-05 05:31:42 +01:00
FredApps 75885b49de Merge pull request #1881 from zberry/smartart-no-flip
Flip is refused on a SmartArt graphic instead of turning it into a plain group
2026-11-05 05:31:38 +01:00
FredApps f7b728d0c2 Merge remote-tracking branch 'origin/main' into web-host-bridge 2026-11-05 05:25:08 +01:00
FredApps d82aaddab4 Merge remote-tracking branch 'origin/main' into copy-the-fix 2026-11-05 05:24:59 +01:00
FredApps 8f26681a1f Merge main into connector-glue-in-groups (keep both tests) 2026-11-05 05:21:16 +01:00
FredApps f2356fcf63 Merge main into flip-smartart (keep both tests) 2026-11-05 05:19:55 +01:00
FredApps 60132037b2 Merge branch 'main' into web-host-bridge
Keep both ROADMAP progress entries: Ogg Opus duration from main and the
web ?host=parent bridge from this branch.
2026-11-05 05:19:47 +01:00
FredApps ac09977c12 Merge pull request #1875 from zberry/table-cells-refused
Table cell commands refuse a malformed `cells` list instead of dropping entries
2026-11-05 05:19:11 +01:00
FredApps 480cbfd4ef Textures::thumb: document the stale thumbnail returned over budget 2026-11-05 05:18:38 +01:00
FredApps afc7f765c9 shape.connect: refuse a grouped non-line id as not a line 2026-11-05 05:18:29 +01:00
aiongg d9186c7ce1 Apply review fixes
- crates/engine/src/cmd/shape/size.rs
2026-11-05 04:04:02 +00:00
aiongg 75744dd67d Apply review fixes
- crates/engine/src/tools/drag.rs
2026-11-05 04:02:42 +00:00
sbjwin ebd306acc1 UI: request repaint on stale thumbnail return to finish grayscale refresh 2026-11-05 12:51:29 +09:00
aiongg 161af3b7d5 Merge main into smartart-no-flip 2026-11-05 03:49:15 +00:00
aiongg 4e4d98e69d Merge main into table-cells-refused 2026-11-05 03:49:09 +00:00
aiongg a52b2d348f Merge main into connector-glue-in-groups 2026-11-05 03:45:22 +00:00
aiongg acd19ff4ac Merge pull request #1879 from zberry/outline-title-and-body
Outlines leave footer, date and slide-number placeholders out: export, RTF and the outline view
2026-11-04 22:38:05 -05:00
sbjwin b06d60b8ed UI: follow Grayscale view mode in slide thumbnails
In PowerPoint, turning on Grayscale view mode turns both the active
slide canvas and the slide thumbnail pane to grayscale, while Print
retains the original full color for output.

Previously, DeckCraft only passed `app.ui.grayscale` to the main canvas
texture renderer (`Textures::slide`), while thumbnail rendering
(`Textures::thumb`) hardcoded `false` and lacked `grayscale` in its
cache key. As a result, when Grayscale mode was enabled, the canvas
rendered in grayscale but the thumbnail pane remained in full color.

- Update `Textures::thumb` to accept `grayscale: bool` and include it in
  the cache key (`extra: grayscale as u64`).
- Pass `app.ui.grayscale` to `Textures::thumb` in the thumbnail pane
  (`panels/thumbs.rs`), Slide Sorter & Notes Page (`sorter.rs`), and
  Notes Master view (`canvas.rs`).
- Keep Print preview (`backstage/print.rs`) in full color (`false`) to
  match the actual exported PDF print output.
- Add regression test `to_color_image_turns_colors_to_grayscale_when_flag_is_set`.
2026-11-05 12:35:59 +09:00
FredAppsandClaude Opus 5.5 a2d9e71b5e About: credits tables fit without side-scrolling and their columns resize (#1806)
* About: credits tables fit without side-scrolling and their columns resize

The Contributors table and the Models table were egui Grids inside a
two-way scroll area: they opened wider than the dialog, so they scrolled
sideways, and Company and Model sat almost touching. Both are now
egui_extras tables. Each column opens at the width of its header and widest
cell, the spare width goes to the text columns (Name; Company and Model),
and every column edge can be dragged. Widening a column grows the table
like a spreadsheet, and only then does it scroll sideways; the header stays
put while the rows scroll. Counts and dates are right-aligned and every
cell is vertically centred.

The sorted column stays highlighted and its direction shows on the
toolbar's sort-order button, so headers no longer reserve room for an
arrow. The opening widths come from fit_widths, a pure function with unit
tests that a fitted table fills its room exactly and that only the text
columns give way when room is short.

The About dialog opens 820 points wide, so every column fits at its natural width.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Let every credits column be dragged, and refit changed tables

The last column was egui's remainder column, which has no drag handle, so
it could only be resized from the edge before it. It is now an ordinary
column with its own handle.

A table keeps its column widths, so switching to a longer name left the
Name column at its old width and clipped the text. The table now goes
back to the opening fit whenever that fit changes: a longer widest cell
or a resized window. Dragged widths are dropped at that point.

fit_widths could overflow the room when one column hit the minimum,
because the other columns did not take up its share. Columns now give
way in proportion to their width above the minimum.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Refit credits tables on what they measure, not the fitted widths

Comparing the fitted widths missed the case it was meant for. Name is the
only growing column, so a longer name takes its extra width from the
spare room and the fit comes out the same: a dragged Name width stayed,
clipping the longer name.

It could also drop dragged widths by accident. The fit comes from the
available width inside the scroll area, which scrolling can nudge, so
scrolling a widened table could reset it.

The table now compares the natural widths and the clip rect's width,
which scrolling does not move.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-11-05 04:33:51 +01:00
Zach BerryandClaude Opus 5.5 fe35df948c Flip is refused on a SmartArt graphic instead of turning it into a plain group
PowerPoint greys Flip out for a SmartArt graphic (SmartArt Design ▸ Right to
Left is the way to mirror it), also inside a group, as ungrouping would hand
the flip to the graphic. Before, a flip turned it into a plain group that the
smartArt.* commands then refused. arrange.flipHorizontal and flipVertical
reach the check through shape.flip. Split out of the SmartArt PR at review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 20:09:40 -06:00
Zach BerryandClaude Opus 5.5 ede154d6be Outlines leave footer, date and slide-number placeholders out: export, RTF and the outline view
An agent run exporting a deck as an outline got the footer text and the
slide-number field (‹#›) as body lines under each title. PowerPoint's
outline view shows title and body placeholders only, so the text outline,
the RTF outline (Save As ▸ Outline/RTF) and the outline view now skip the
footer-row placeholders as they already skipped the title. Split out of the
frame export PR at review, which asked for all three together.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 20:07:59 -06:00
FredApps 6431460e80 Merge pull request #1872 from FredApps/fix/1866-web-presenter-all-slides
Show: All slides opens in the web Presenter View, as on desktop
2026-11-05 03:04:06 +01:00
Zach BerryandClaude Opus 5.5 af9f522c6a shape.connect glues to a shape inside a group, and the connector follows the group
An agent run found that connecting to a grouped shape answered ok but left
the connector's end unset: the sites of a group member were computed in the
group's child space, and rerouting only looked at the slide's top-level
shapes, so the glue was dropped at once. Both now locate the shape anywhere
in the tree with the affine onto the slide, as PowerPoint glues to grouped
shapes. shape.sites answers in the same slide space. A line inside a group is
refused as `id`: its ends would land in the group's space, and rerouting
does not follow grouped connectors.

Split out of the connector default sites PR at review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 19:48:16 -06:00
FredApps efd26de81d Show: clear the web presenter grid in hidden logic, let Enter and Space pick a focused thumbnail (#1866) 2026-11-05 02:48:01 +01:00
Zach BerryandClaude Opus 5.5 7c47d0a9ac Table cell commands refuse a malformed cells list instead of dropping entries
`cells` must be a list of exact [row, col] pairs inside the table; an entry
that is not one, a cell past the table, or more than 100000 entries is refused
and names the command, instead of being dropped or read as absent (which acted
on the selection or the whole table). Borders use checked arithmetic for the
neighbour cell. Split out of the 0-based index PR at review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 19:40:18 -06:00
FredApps 884e3d3353 Show: keep web All slides messages small, clear the grid when the popup closes, show it on the end screen (#1866) 2026-11-05 02:37:37 +01:00
FredApps f49ff09b8a Show: All slides opens in the web Presenter View, as on desktop (#1866) 2026-11-05 02:25:54 +01:00
aiongg 52f87aca13 Merge pull request #1474 from zberry/pptx-show-media-controls
PPTX: Show media controls survives a save and reopen
2026-11-04 19:17:57 -05:00
aiongg dfee4c1e88 Merge pull request #1870 from Silvertree2010/fix/1864-next-and-previous-not-working-in
Make Previous/Next work in the web presenter popup
2026-11-04 19:03:38 -05:00
NetterSilverandClaude Opus 5.5 f321416b34 Make Previous/Next work in the web presenter popup
Fixes #1864

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 00:52:05 +01:00
aiongg 41f07a3c4e Merge main into pptx-show-media-controls 2026-11-04 23:51:52 +00:00
.CooCoo 85de6ca5d7 Shape Fill: the gradient help says which kinds rotateWithShape keeps upright (#1843)
With rotateWithShape false, the renderer lays linear and radial gradients
out upright on the slide, while rectangular and path gradients still turn
with the shape. The shape.fill params doc now says so.
2026-11-04 18:11:29 -05:00
Harrison Booth fc18a78e65 comment: reject malformed navigation indices (#1547) 2026-11-04 18:09:50 -05:00
Dylan M. Taylor 31abd2d819 Pass the Linux signing key to the release workflow (#1868) 2026-11-04 17:45:40 -05:00
Stephan Schott-VerdugoandClaude Opus 5.5 070f12cc9c Keep Source Formatting resolves colours as the renderer does
Review follow-up on #1658 (CodeRabbit), both from storing the slide's
mapped scheme in 30ccc38e:

- Copying in Slide Master view used the current slide's colour map. Use
  `ctx_for`, so the theme is that of what is being edited.
- `ColorScheme` shares an entry between `dk1` and `tx1`, so a mapped scheme
  changed direct `dk1` colours too: a dark fill pasted white. `SourceTheme`
  keeps the theme's colours and the map apart, and `bake_theme` maps only
  `tx1`, `bg1`, `tx2` and `bg2`, as `Ctx::color` does.

The browser image test now checks that non-picture pastes keep their text.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 22:23:35 +01:00
Stephan Schott-VerdugoandClaude Opus 5.5 46a15a4684 Merge branch 'main' into copy-the-fix
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 22:19:46 +01:00
aiongg 144fd28f69 Merge pull request #1192 from MarekSpodBiedry/issue-1153
Use evaluated field values in the placed text so the PDF text layer shows the slide number (#1153)
2026-11-04 16:15:06 -05:00
Stephan Schott-VerdugoandClaude Opus 5.5 30ccc38e90 Keep Source Formatting uses the slide's colour map
Review follow-up on #1658 (CodeRabbit): `source_theme` stored the master's
scheme, so a slide or layout that overrides the colour map had its colours
fixed through the wrong map. Detaching made this reach a placeholder's
inherited colours: a title drawn white under an override pasted black.
Store the scheme the slide draws with. Paste as Picture reads the same
value.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 22:02:22 +01:00
aiongg 65ddd8ad51 Apply review fixes
- crates/render/src/placed.rs
2026-11-04 20:55:43 +00:00
Stephan Schott-VerdugoandClaude Opus 5.5 d6cc51e07a Alt+drag detaches placeholders; Slide Master view pastes them as placeholders
Review follow-up on #1658:

- The drag duplicate cleared `ph` without detaching, so a title or body
  dragged off with Alt lost its size and bullets. Copy, Duplicate and the
  drag now share `detach_on_slide`.
- Detaching at copy time turned a slide placeholder pasted in Slide Master
  view into a regular shape. The clipboard keeps the originals, and a paste
  there takes them, so it stays a placeholder as before this PR. Keep
  Source Formatting bakes them too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 21:43:33 +01:00
Stephan Schott-VerdugoandClaude Opus 5.5 077cd36e9f Merge branch 'main' into copy-the-fix
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 21:38:04 +01:00
Mex 9d36b5931c Ogg: include recovered tail packets in Opus duration (#1862)
* Ogg: include recovered tail packets in Opus duration

Keep the existing missing-granule recovery cursor in step with the packet ends it assigns. Add a regression for a partially and entirely unanchored stream; existing valid end-trimming coverage remains unchanged.

Validation: rustc --edition=2024 --test crates/ogg/src/lib.rs: all 12 tests pass. Scoped rustfmt check passes. Full cargo --offline xtask ci is blocked before build by the uncached pinned egui checkout.

* Ogg: record recovery duration correction
2026-11-04 15:15:59 -05:00
aiongg c252a681a0 Merge pull request #1454 from Silvertree2010/fix/h3020-rtf-outline-import-drops-the-res
Keep whole character after a backslash in RTF outline import
2026-11-04 14:43:02 -05:00
Dylan M. Taylor 80f784d210 Fonts: ignore variation tags the font has no axis for (#1795) 2026-11-04 14:36:13 -05:00
Dylan M. Taylor 07dc2210a1 Repair: bound the notes page size like a slide (#1832) 2026-11-04 14:09:46 -05:00
Dylan M. Taylor 51d19baa0c Ogg, xtask: check packet ranges against the source first; stats skips directory links (#1796) 2026-11-04 13:56:48 -05:00
MarekSpodBiedry 50187449c2 Render tests: import place_slide_with so the date test compiles (#1153) 2026-11-04 07:52:46 +01:00
MarekSpodBiedry b8de82e2cc Merge remote-tracking branch 'origin/main' into review-1192 2026-11-04 07:51:04 +01:00
bradandClaude Opus 5.5 6eaae2bd8c Web: only the page's first file replaces the start deck; close Save As after a same-name save
A file the page sent without a name opens untitled and unchanged, like the
empty start presentation, so the page's next file closed it. Host mode now
records the start presentation (start_for_host) and only the first file
that opens takes its place, as packaging/web/README.md says.

Save As > Save under the presentation's own name changed neither the path
nor the dirty flag, so the Save As page stayed open and a second click sent
the file again. The page now also compares the revision, which every save
bumps.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 23:26:50 -06:00
bradandClaude Opus 5.5 d5471bccf0 Web: open and save through a framing page (?host=parent)
A same-origin page that frames DeckCraft's web build with ?host=parent
sends a file with deckcraft:open and gets every save back as
deckcraft:save {name, saveAs, bytes} instead of a download, with
deckcraft:ready, deckcraft:dirty and deckcraft:failed, as GridCraft,
WordCraft and PdfCraft do. Without ?host=parent nothing changes.

Save follows the desktop app. Every file from the page opens through
file.adopt like File > Open on the desktop; a presentation sent with a
presentation name keeps that name as its file, so Save (Ctrl+S, the
File tab, the Close prompt or the command palette) writes it back under
that name and format with saveAs: false. Anything else (no name, an
outline, a damaged file opened as a repaired copy, a dropped file) is
untitled, and its first Save is a new <title>.deckcraft with saveAs:
true, as is Save As. Exports and Print are copies (saveAs: false).

Host mode starts with an empty presentation that gives way to the first
file while untouched, &author= signs comments, and the URL flags are
read as whole parameters so a free-text author can't set ?show.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 18:58:29 -06:00
Stephan Schott-Verdugo ba3d74dfb5 Merge branch 'main' into copy-the-fix 2026-11-03 09:10:31 +01:00
Zach BerryandClaude Opus 5.5 b9eaaeb23b Test: hidden media controls are written as PowerPoint's p14 extension
The round trip now also checks the written presProps.xml: inside p:showPr, the extension uri and
p14:showMediaCtrls with its namespace declared on the element; the default writes nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-02 17:03:02 -06:00
Stephan Schott-VerdugoandClaude Opus 5.5 1192e5ef30 Detached placeholders follow each property's style precedence
Review follow-up (CodeRabbit on #1658):

- Text levels are resolved before the copy's style changes. A master
  placeholder's font reference reached the copy's text (body font in an
  accent colour for a title) although the original ignores it: text only
  takes the shape's or the layout placeholder's font reference.
- Fill, line and effects come from where `resolve::fill`, `resolve::line`
  and `resolve::effects` take them: the first shape in the chain with an
  explicit value or a style. A layout's (or the slide's own) style no
  longer loses to an explicit fill, line or effect on the master. The
  copy gets one style whose references each come from the shape that
  supplied that property; unused references are neutral.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-01 23:06:09 +01:00
Stephan Schott-VerdugoandClaude Opus 5.5 f006b5d8ce Ctrl+V pastes a picture copied in a browser, not its link
A browser's Copy Image puts the picture on the clipboard together with
text that only names it (the address and alt text, or Firefox's UTF-16
text/x-moz-url read as UTF-8) and an <img> HTML fragment. Ctrl+V only
looked for a picture when the text was empty, so it pasted a text box
with the link. When the text or HTML only names a picture, paste the
picture; with no picture on the clipboard the link still pastes as text.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-01 22:38:05 +01:00
Stephan Schott-VerdugoandClaude Opus 5.5 e1cfe8516e Paste and duplicate keep a placeholder's inherited look
A placeholder copied or duplicated on a slide became a regular shape by
clearing `ph`, which also dropped everything it inherited from its layout
and master: the title's 44 pt heading font, the body's bullets and indent
levels, autofit and the box. Without `ph` the text fell back to the
presentation's default style (18 pt, no bullets), on Ctrl+V and the
context menu alike.

`resolve::detach_placeholder` makes the inherited box, fill, line,
effects, body properties and every level's text style the shape's own
before `ph` is cleared. Theme references stay references, so the default
paste still follows the destination theme. Copy (so Cut and pasting into
another deck work after the source is gone) and Duplicate use it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-01 22:29:43 +01:00
Stephan Schott-VerdugoandClaude Opus 5.5 cbb0c48445 Fix OS clipboard: enable eframe's clipboard feature dropped in the egui 0.37 migration
Since #1350 (v0.11.1) the desktop build listed eframe features without
`clipboard`, so egui-winit simulated a clipboard inside the app: Ctrl+V
never read text or pictures from other apps, and Ctrl+C of text (sent out
as HTML through arboard) never reached egui's clipboard, so Ctrl+V pasted
nothing or stale text. The context menu kept working because it only uses
the session clipboard.

Turn the feature back on. On native Wayland, egui-winit owns the clipboard
through winit's data device (text and PNG); leave the X11-only arboard
pasteboard (HTML, pictures) off there so copies are claimed on the Wayland
selection. Regression tests guard the feature list and the Wayland switch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-01 22:19:06 +01:00
MarekSpodBiedry 74d7b844fe Merge origin/main into issue-1153 2026-11-01 07:41:05 +01:00
Zach BerryandClaude Sonnet 5.5 6303166a18 [PP1] show.setup showMediaControls survives a pptx save and reopen
PowerPoint stores Show media controls as the p14:showMediaCtrls extension inside
p:showPr (uri {2FDB2607-...}); absent means shown. Write it when hidden, read it back.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-10-30 15:12:26 -05:00
NetterSilverandClaude Haiku 5.5 d8936f94af Keep whole character after a backslash in RTF outline import
Found while testing crates/format/src/rtf.rs:301-326 (symbol / text, reached via escape)

Co-Authored-By: Claude Haiku 5.5 <[email protected]>
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-10-27 10:58:38 +01:00
MarekSpodBiedry 813378cc20 Keep the PDF text layer aligned with line breaks, use one clock reading for raster and text layer, and test the page text (#1153) 2026-10-23 16:08:53 +02:00
MarekSpodBiedry cc6773c95f Use evaluated field values in the placed text so the PDF text layer shows the slide number (#1153) 2026-10-22 21:02:59 +02:00
258 changed files with 5913 additions and 53422 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:
+28
View File
@@ -0,0 +1,28 @@
name: House rules
on:
push:
branches: [main]
paths:
- AGENTS.md
- CLAUDE.md
- .github/workflows/house-rules.yml
pull_request:
paths:
- AGENTS.md
- CLAUDE.md
- .github/workflows/house-rules.yml
workflow_dispatch:
permissions:
contents: read
concurrency:
group: house-rules-${{ github.ref }}
cancel-in-progress: true
jobs:
house-rules:
uses: storytold/craft-common-actions/.github/workflows/house-rules.yml@main
with:
rust: true
+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
+14
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:
@@ -19,10 +23,18 @@ concurrency:
cancel-in-progress: false
jobs:
# Regenerates contributors/contributors.json for the release commit; every build bundles it.
credits:
permissions:
contents: read
pull-requests: read
uses: storytold/craft-common-actions/.github/workflows/refresh-contributors.yml@main
release:
needs: credits
# 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
@@ -30,6 +42,7 @@ jobs:
# manifest fails the release instead of silently skipping it.
craft-fonts-ref: 37827adc41fd165f9a9234ac40f94dbad353aa69
version: ${{ inputs.version }}
contributors-artifact: ${{ needs.credits.outputs.artifact-name }}
secrets:
APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }}
APPLE_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_CERTIFICATE_PASSWORD }}
@@ -45,3 +58,4 @@ jobs:
AZURE_SIGNING_ENDPOINT: ${{ secrets.AZURE_SIGNING_ENDPOINT }}
AZURE_SIGNING_ACCOUNT: ${{ secrets.AZURE_SIGNING_ACCOUNT }}
AZURE_CERT_PROFILE: ${{ secrets.AZURE_CERT_PROFILE }}
LINUX_SIGNING_KEY: ${{ secrets.LINUX_SIGNING_KEY }}
+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
+61 -10
View File
@@ -1,3 +1,56 @@
<!-- craft-house-rules:start core source=dde09ea31e2e457f5b4653a6e76f7cdf55944bdf sha256=540e6cea75d12d0710c3dca89658328e81e508ecef102446ca5bb95b7c0b92bb -->
## House rules
These apply in every Craft repository. A later rule may be stricter, not weaker. The source is `standards/house-rules-core.md` in `storytold/craft-contrib`. Propose changes there, not in a copy.
1. **A person is accountable.** Whoever runs an agent answers for what it opens and for its review comments.
2. **Check, then claim.** Before writing code, read the issue comments, search pull requests for `#N` and the keywords, and check recent commits on `main`. Comment on the issue first. Release a claim you will not finish.
3. **Verify, don't guess.** Open a definition before using it. Confirm the cause in the code, even when someone names a location.
4. **Keep fixes narrow and tested.** Fix what the issue describes, where the bug is. Add a test that fails without the fix.
5. **Review the diff before opening a pull request.** Identifiers exist as used, the test fails without the fix, nothing unrelated changed, and the description is true.
6. **Look for a better pull request.** If another fixes the same thing, close yours when it is better or equal, and say why.
7. **Report only what ran.** Say what you tested and what you could not run. Without a local build or test, say so and rely on CI before marking ready.
8. **`main` changes through a pull request.** Do not push to it. Branch protection enforces this. There is no exception.
9. **Stop and ask only about** licensing outside the allowlist, publishing, releases, a new remote, secrets, spending money, destructive git, and anything else hard to undo. Otherwise keep working. `plan/` is a gitignored notebook, not rules. Without it, do not ask for it and do not stop.
10. **Keep secrets and personal details out** of the repository, logs and requests.
11. **CI stays shared.** Workflows call `storytold/craft-common-actions`. Do not change CI in a feature or fix pull request. If CI cannot cover a change, open an issue there.
12. **Guards say why.** A check that looks removable says what it prevents. Read that before changing it.
13. **Instructions must be readable by everyone.** No notes about one machine, and no links to files a reader cannot open. `craftrules` is retired. Do not link to it.
14. **Keep pull requests moving.** Concurrent pull requests are fine; unanswered actionable feedback is not. Respond in 7 days, a reminder at 14, closure eligible at 30, from when an actionable request waits on the author. Suggestions and unconfirmed bot findings do not start the clock. A fix, explanation or agreed plan resets it; unrelated activity does not. Waiting on a reviewer or maintainer pauses it and does not count against the author. A maintainer decides closure; it is never automatic. Reuse search results, do not poll, and stop at a rate limit.
15. **No agent attribution.** A pull request does not say an agent wrote it. A repository rule may not require that.
<!-- craft-house-rules:end -->
<!-- craft-house-rules:start rust source=dde09ea31e2e457f5b4653a6e76f7cdf55944bdf sha256=387e0ccb4b49e567f60951d7d6fff7305054c89f43ad812b9993dc7c38528013 -->
## Rust Craft repositories
These apply to native Craft apps and to shared Rust libraries. Select them by repository type. A repository that merely contains some Rust does not take this block. Rules later in a repository's file may be stricter, but not weaker. The source is `standards/house-rules-rust.md` in [storytold/craft-contrib](https://github.com/storytold/craft-contrib).
1. **Never crash.** Non-test code does not panic and has no `unsafe`, except where the repository file names an exception. Return errors, treat input as hostile, and bound recursion and allocations. New production crate roots copy the `deny` attribute from an existing crate: `clippy::unwrap_used`, `clippy::expect_used`, `clippy::panic`, `clippy::unimplemented`, `clippy::todo`, `clippy::unreachable`. Every crash fix comes with a test that panicked before the fix.
2. **Clean room.** Learn from observed behaviour and public specifications. Do not copy another product's code, assets, strings or artwork. Check a dependency's licence before adding it.
3. **Assets and fonts.** Every asset is original or suitably licensed, and is recorded in `ATTRIBUTION.md` in the same change. Font files live in [storytold/craft-fonts](https://github.com/storytold/craft-fonts), not in the app repository.
4. **Rust only for native Craft apps.** No handwritten JavaScript or TypeScript, and no Tauri, Electron or webview shell, in those apps. Shared tooling follows its documented technical scope and maintainer-approved exceptions. Craft PR Review's existing Tauri and TypeScript UI is one such exception. It does not relax this rule for native apps. Preserve any existing web build.
5. **Time goes through `chrono`.** Do not hand-roll calendar conversion. For dates beyond what `chrono` can represent, follow [standards/time.md](https://github.com/storytold/craft-contrib/blob/main/standards/time.md).
6. **Contributor credits.** Add or edit only your own person's entry in `contributors/people.toml`, and only after asking them. Never guess a name or hand-edit `contributors.json`.
## Licence allowlist
Dependencies and assets use SPDX identifiers. An expression is allowed when every identifier is in this list. A repository may name a shorter list. It may not add an identifier here by editing its own copy of these rules.
- `ISC`
- `MIT`
- `BSD-2-Clause`
- `BSD-3-Clause`
- `Apache-2.0`
- `OFL-1.1`
- `Ubuntu-font-1.0`
- `Bitstream-Vera`
- `CC0-1.0`
- `CC-BY-4.0`
- `CC-BY-SA-4.0`
- `LicenseRef-Public-Domain`
`LicenseRef-Storyteller-Trademark` is not in this list. It covers Storyteller brand marks only, where a repository already says so.
<!-- craft-house-rules:end -->
# AGENTS.md — rules for every agent and contributor
DeckCraft is a clean-room, open-source, pure-Rust presentation application targeting Microsoft
@@ -7,8 +60,7 @@ PhotoCraft, VectorCraft, FilmCraft, LightCraft, PdfCraft, EffectCraft, DesignCra
(`../<app>`).
These rules bind every AI agent and every human contributor. They come before any task,
instruction or deadline. `CLAUDE.md` holds day-to-day working notes; if the two disagree, **this
file wins.**
instruction or deadline. Day-to-day working instructions are in this file; `CLAUDE.md` imports it.
---
@@ -125,14 +177,14 @@ People trust DeckCraft with their talks; a crash loses work. **This outranks fea
CLI, control channel and MCP do exactly the same thing.
- **The UI is thin**: panels read engine state and act through commands. Colours come from theme
tokens. **Never use Tauri**; egui only.
- **Time goes through `chrono`** (workspace dependency). Never hand-roll UTC/calendar conversion (days-to-date algorithms, leap-year math); a crate owns only its output format string. The one exception is range extension: past the years chrono can represent, shift the date by whole 400-year Gregorian cycles (146,097 days, after which the calendar repeats), let chrono compute the month and day, and add the cycles back to the year (splitting seconds into hours, minutes and seconds is plain arithmetic).
- **Never break wasm** (`cargo xtask wasm`).
## 5. Quality gates (before every commit)
`cargo xtask ci` = fmt check, clippy `-D warnings`, tests (nextest when installed) and doctests, layering check, asset check, wasm
check. Commit after each landed arc with a task id in the message (`M1.2: smart guides`) and push
to `main`.
check. Commit after each landed arc with a task id in the message (`M1.2: smart guides`).
New workspace crates include `workspace = true` under `[lints]` in `Cargo.toml`. Production crate roots
declare the crate-level lint attributes before modules; crate-level `//!` documentation may precede them.
## 6. Running and looking at the app
@@ -141,19 +193,18 @@ to `main`.
`{"id":1,"method":"engine.execute","params":{"command":"shape.insert","params":{"preset":"star5","rect":[100,100,200,200]}}}`
then `{"id":2,"method":"ui.screenshot","params":{"path":"/tmp/shot.png"}}`. Methods:
`crates/ui-egui/src/control.rs`, docs: `docs/control-protocol.md`.
- **For UI work, look at the result** (screenshot, read the PNG) and compare with the observations
in `plan/powerpoint/01-observed-ui.md`.
- **For UI work, look at the result** (screenshot, read the PNG). If available, compare with the observations
in `plan/powerpoint/01-observed-ui.md`. Otherwise verify the intended behaviour against the task and
committed documentation; the optional notebook is not a prerequisite.
- Headless: `deckcraft-cli render --sample --all --scale 1 out/`,
`deckcraft-cli run --sample --cmd 'slide.new={"title":"Hi"}' --export out.png`.
- MCP: `deckcraft-cli mcp` (headless) or `deckcraft-cli mcp --connect 7990` (drive the app).
See `docs/mcp.md`.
- Shell gotcha on the dev machine: `mv`/`cp` are aliased interactive; use `/bin/mv -f`, `/bin/cp -f`.
- Parallel agents: separate `CARGO_TARGET_DIR` per agent; edit only the crates you own.
## 7. Planning
`plan/` (gitignored) holds the local plan: `README.md`, `architecture.md`, `execution-plan.md`,
`STATUS.md` (start every session there) and `powerpoint/` observations. The committed progress
`plan/` is an optional gitignored notebook (status, execution notes, `powerpoint/` observations). Do not ask for it and do not stop when it is absent. The committed progress
docs:
| File | Holds |
+1 -25
View File
@@ -1,25 +1 @@
# DeckCraft — working notes for Claude
Read [AGENTS.md](AGENTS.md) first: its rules (asset policy, clean room, never crash) bind you and
win over anything here.
New workspace crates must include `[lints] workspace = true` in `Cargo.toml` to inherit
`[workspace.lints]`. Production crate roots must declare the required crate-level lint attributes
before modules or other crate items; crate-level `//!` documentation may precede them.
## Start every session
1. Read `plan/STATUS.md`, then the current milestone in `plan/execution-plan.md`.
2. Run `cargo xtask ci` before you change anything, so you know the tree is green.
## Habits
- Every user-visible action is a command with tests (`crates/engine/src/cmd/*`).
- Look at UI changes: `deckcraft --sample --control 7990` + `ui.screenshot`, or
`deckcraft-cli render`.
- Commit after each landed arc (`M<n>.<k>: what`) and push to `origin main`.
- Keep `ROADMAP.md`, the progress docs in `docs/` (AGENTS.md §7) and `plan/STATUS.md` current.
- Don't ask the user questions; record genuinely-theirs decisions in `plan/STATUS.md`.
## CI workflows
- Workflows in `.github/workflows/` call the shared workflows in [`storytold/craft-common-actions`](https://github.com/storytold/craft-common-actions) at `@main`, with their inputs and secrets. No other jobs: if no shared workflow does what's needed, it doesn't go here.
- Don't add or change CI in a feature or fix PR. If the existing workflows can't build or test a change, link an existing craft-common-actions issue or PR, or open a new issue, or a PR only for a small fix that is safe for every app and does not increase CI time.
- Some workflow files here still run their own jobs. Don't add to them. Moving a job into craft-common-actions is its own task, never part of another PR, and the job stays here until a shared workflow runs it.
@AGENTS.md
Generated
+203 -111
View File
@@ -565,18 +565,42 @@ dependencies = [
"thiserror 1.0.69",
]
[[package]]
name = "calloop"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43b32313e937c8721c9d17ee9bf60699a6402b4b7c2cb6ac1bee0ffcaa4b4e5e"
dependencies = [
"bitflags 2.13.2",
"polling",
"rustix 1.1.5",
"tracing",
]
[[package]]
name = "calloop-wayland-source"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95a66a987056935f7efce4ab5668920b5d0dac4a7c99991a67395f13702ddd20"
dependencies = [
"calloop",
"calloop 0.13.0",
"rustix 0.38.44",
"wayland-backend",
"wayland-client",
]
[[package]]
name = "calloop-wayland-source"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "138efcf0940a02ebf0cc8d1eff41a1682a46b431630f4c52450d6265876021fa"
dependencies = [
"calloop 0.14.5",
"rustix 1.1.5",
"wayland-backend",
"wayland-client",
]
[[package]]
name = "cast"
version = "0.3.0"
@@ -820,15 +844,24 @@ dependencies = [
"libc",
]
[[package]]
name = "craft-audio"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
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=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
[[package]]
name = "craft-i18n"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
dependencies = [
"fixed_decimal",
"icu_decimal",
@@ -851,7 +884,7 @@ dependencies = [
[[package]]
name = "craft-media"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7"
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
dependencies = [
"thiserror 2.0.21",
]
@@ -859,7 +892,7 @@ dependencies = [
[[package]]
name = "craft-pdf"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
dependencies = [
"krilla",
"thiserror 2.0.21",
@@ -868,7 +901,7 @@ dependencies = [
[[package]]
name = "craft-ui"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
dependencies = [
"craft-fonts-build",
"craft-i18n",
@@ -888,7 +921,7 @@ dependencies = [
[[package]]
name = "craft-update"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
dependencies = [
"chrono",
"dunce",
@@ -898,6 +931,7 @@ dependencies = [
"image",
"log",
"plist",
"rustix 1.1.5",
"semver",
"serde",
"serde_json",
@@ -911,6 +945,36 @@ dependencies = [
"zip 9.1.0",
]
[[package]]
name = "craft-video"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=09dcbc225b95232998498a40ca56e24716571d53#09dcbc225b95232998498a40ca56e24716571d53"
dependencies = [
"craft-media",
"rayon",
"thiserror 2.0.21",
]
[[package]]
name = "craft-web-api"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=fd5e2feff6ea25bed371a99cc732b3968d621505#fd5e2feff6ea25bed371a99cc732b3968d621505"
dependencies = [
"js-sys",
"serde_json",
"wasm-bindgen",
"wasm-bindgen-futures",
]
[[package]]
name = "craft-web-host"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=fd5e2feff6ea25bed371a99cc732b3968d621505#fd5e2feff6ea25bed371a99cc732b3968d621505"
dependencies = [
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "crc32fast"
version = "1.5.2"
@@ -984,7 +1048,7 @@ checksum = "0c87e182de0887fd5361989c677c4e8f5000cd9491d6d563161a8f3a5519fc7f"
[[package]]
name = "deckcraft"
version = "0.15.0"
version = "0.18.1"
dependencies = [
"arboard",
"chrono",
@@ -1006,32 +1070,16 @@ dependencies = [
[[package]]
name = "deckcraft-anim"
version = "0.15.0"
version = "0.18.1"
dependencies = [
"deckcraft-geom",
"deckcraft-model",
"serde",
]
[[package]]
name = "deckcraft-av1"
version = "0.15.0"
dependencies = [
"deckcraft-bitstream",
"rayon",
"thiserror 2.0.21",
]
[[package]]
name = "deckcraft-bitstream"
version = "0.15.0"
dependencies = [
"craft-media",
]
[[package]]
name = "deckcraft-cli"
version = "0.15.0"
version = "0.18.1"
dependencies = [
"deckcraft-engine",
"deckcraft-mcp",
@@ -1041,14 +1089,14 @@ dependencies = [
[[package]]
name = "deckcraft-color"
version = "0.15.0"
version = "0.18.1"
dependencies = [
"serde",
]
[[package]]
name = "deckcraft-engine"
version = "0.15.0"
version = "0.18.1"
dependencies = [
"chrono",
"deckcraft-anim",
@@ -1074,7 +1122,7 @@ dependencies = [
[[package]]
name = "deckcraft-fonts"
version = "0.15.0"
version = "0.18.1"
dependencies = [
"epaint_default_fonts",
"harfrust 0.13.3",
@@ -1086,7 +1134,7 @@ dependencies = [
[[package]]
name = "deckcraft-format"
version = "0.15.0"
version = "0.18.1"
dependencies = [
"deckcraft-model",
"serde",
@@ -1097,47 +1145,15 @@ dependencies = [
[[package]]
name = "deckcraft-geom"
version = "0.15.0"
version = "0.18.1"
dependencies = [
"kurbo",
"serde",
]
[[package]]
name = "deckcraft-h264"
version = "0.15.0"
dependencies = [
"deckcraft-bitstream",
"rayon",
"thiserror 2.0.21",
]
[[package]]
name = "deckcraft-hevc"
version = "0.15.0"
dependencies = [
"deckcraft-bitstream",
"rayon",
"thiserror 2.0.21",
]
[[package]]
name = "deckcraft-isobmff"
version = "0.15.0"
dependencies = [
"craft-media",
]
[[package]]
name = "deckcraft-matroska"
version = "0.15.0"
dependencies = [
"craft-media",
]
[[package]]
name = "deckcraft-mcp"
version = "0.15.0"
version = "0.18.1"
dependencies = [
"deckcraft-engine",
"deckcraft-render",
@@ -1147,16 +1163,11 @@ dependencies = [
[[package]]
name = "deckcraft-media"
version = "0.15.0"
version = "0.18.1"
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",
@@ -1165,7 +1176,7 @@ dependencies = [
[[package]]
name = "deckcraft-metafile"
version = "0.15.0"
version = "0.18.1"
dependencies = [
"image",
"kurbo",
@@ -1174,7 +1185,7 @@ dependencies = [
[[package]]
name = "deckcraft-model"
version = "0.15.0"
version = "0.18.1"
dependencies = [
"deckcraft-color",
"deckcraft-geom",
@@ -1184,17 +1195,9 @@ dependencies = [
"unicode-segmentation",
]
[[package]]
name = "deckcraft-ogg"
version = "0.15.0"
[[package]]
name = "deckcraft-opus"
version = "0.15.0"
[[package]]
name = "deckcraft-pdf"
version = "0.15.0"
version = "0.18.1"
dependencies = [
"craft-pdf",
"deckcraft-fonts",
@@ -1212,7 +1215,7 @@ dependencies = [
[[package]]
name = "deckcraft-pptx"
version = "0.15.0"
version = "0.18.1"
dependencies = [
"deckcraft-color",
"deckcraft-engine",
@@ -1228,7 +1231,7 @@ dependencies = [
[[package]]
name = "deckcraft-render"
version = "0.15.0"
version = "0.18.1"
dependencies = [
"deckcraft-anim",
"deckcraft-color",
@@ -1247,7 +1250,7 @@ dependencies = [
[[package]]
name = "deckcraft-text"
version = "0.15.0"
version = "0.18.1"
dependencies = [
"chrono",
"deckcraft-color",
@@ -1261,7 +1264,7 @@ dependencies = [
[[package]]
name = "deckcraft-ui-egui"
version = "0.15.0"
version = "0.18.1"
dependencies = [
"craft-ui",
"craft-update",
@@ -1275,6 +1278,7 @@ dependencies = [
"deckcraft-render",
"deckcraft-text",
"egui",
"egui_extras",
"egui_kittest",
"log",
"serde",
@@ -1284,19 +1288,12 @@ dependencies = [
"web-time",
]
[[package]]
name = "deckcraft-vp9"
version = "0.15.0"
dependencies = [
"deckcraft-bitstream",
"rayon",
"thiserror 2.0.21",
]
[[package]]
name = "deckcraft-web"
version = "0.15.0"
version = "0.18.1"
dependencies = [
"craft-web-api",
"craft-web-host",
"deckcraft-engine",
"deckcraft-ui-egui",
"eframe",
@@ -1507,19 +1504,35 @@ dependencies = [
"block2 0.6.2",
"bytemuck",
"egui",
"image",
"log",
"objc2 0.6.5",
"objc2-app-kit 0.3.2",
"objc2-foundation 0.3.2",
"objc2-ui-kit 0.3.2",
"parking_lot",
"profiling",
"raw-window-handle",
"smithay-clipboard",
"web-time",
"webbrowser",
"windows-sys 0.61.2",
"winit",
]
[[package]]
name = "egui_extras"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a"
dependencies = [
"ahash",
"egui",
"enum-map",
"itertools",
"log",
"profiling",
]
[[package]]
name = "egui_glow"
version = "0.37.0"
@@ -1587,6 +1600,26 @@ version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "66b7e2430c6dff6a955451e2cfc438f09cea1965a9d6f87f7e3b90decc014099"
[[package]]
name = "enum-map"
version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4532960034c15ba2f55e9eee631040ecc473c32e6c4b200828c759840f13a06"
dependencies = [
"enum-map-derive",
]
[[package]]
name = "enum-map-derive"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7127272f64b3e098f7d81f350d672081b879bff896d219c42370dc858b1af9ff"
dependencies = [
"proc-macro2",
"quote",
"syn 3.0.6",
]
[[package]]
name = "enumflags2"
version = "0.7.12"
@@ -2708,7 +2741,7 @@ dependencies = [
[[package]]
name = "krilla"
version = "0.8.2"
source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7"
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
dependencies = [
"base64 0.22.1",
"bumpalo",
@@ -3487,7 +3520,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967"
dependencies = [
"libc",
"windows-sys 0.45.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -4403,8 +4436,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3457dea1f0eb631b4034d61d4d8c32074caa6cd1ab2d59f2327bd8461e2c0016"
dependencies = [
"bitflags 2.13.2",
"calloop",
"calloop-wayland-source",
"calloop 0.13.0",
"calloop-wayland-source 0.3.0",
"cursor-icon",
"libc",
"log",
@@ -4421,6 +4454,44 @@ dependencies = [
"xkeysym",
]
[[package]]
name = "smithay-client-toolkit"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0512da38f5e2b31201a93524adb8d3136276fa4fe4aafab4e1f727a82b534cc0"
dependencies = [
"bitflags 2.13.2",
"calloop 0.14.5",
"calloop-wayland-source 0.4.1",
"cursor-icon",
"libc",
"log",
"memmap2",
"rustix 1.1.5",
"thiserror 2.0.21",
"wayland-backend",
"wayland-client",
"wayland-csd-frame",
"wayland-cursor",
"wayland-protocols",
"wayland-protocols-experimental",
"wayland-protocols-misc",
"wayland-protocols-wlr",
"wayland-scanner",
"xkeysym",
]
[[package]]
name = "smithay-clipboard"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71704c03f739f7745053bde45fa203a46c58d25bc5c4efba1d9a60e9dba81226"
dependencies = [
"libc",
"smithay-client-toolkit 0.20.0",
"wayland-backend",
]
[[package]]
name = "smol_str"
version = "0.2.2"
@@ -4928,6 +4999,7 @@ version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100"
dependencies = [
"log",
"pin-project-lite",
"tracing-attributes",
"tracing-core",
@@ -5370,6 +5442,32 @@ dependencies = [
"wayland-scanner",
]
[[package]]
name = "wayland-protocols-experimental"
version = "20250721.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40a1f863128dcaaec790d7b4b396cc9b9a7a079e878e18c47e6c2d2c5a8dcbb1"
dependencies = [
"bitflags 2.13.2",
"wayland-backend",
"wayland-client",
"wayland-protocols",
"wayland-scanner",
]
[[package]]
name = "wayland-protocols-misc"
version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e9567599ef23e09b8dad6e429e5738d4509dfc46b3b21f32841a304d16b29c8"
dependencies = [
"bitflags 2.13.2",
"wayland-backend",
"wayland-client",
"wayland-protocols",
"wayland-scanner",
]
[[package]]
name = "wayland-protocols-plasma"
version = "0.3.12"
@@ -5980,7 +6078,7 @@ dependencies = [
"bitflags 2.13.2",
"block2 0.5.1",
"bytemuck",
"calloop",
"calloop 0.13.0",
"cfg_aliases",
"concurrent-queue",
"core-foundation 0.9.4",
@@ -6001,7 +6099,7 @@ dependencies = [
"raw-window-handle",
"redox_syscall 0.4.1",
"rustix 0.38.44",
"smithay-client-toolkit",
"smithay-client-toolkit 0.19.2",
"smol_str",
"tracing",
"unicode-segmentation",
@@ -6177,7 +6275,7 @@ checksum = "9440ea3e5aeabb0ac63af70daf835274065238cdd0cec83418f417eae38bacee"
[[package]]
name = "xtask"
version = "0.15.0"
version = "0.18.1"
dependencies = [
"serde_json",
]
@@ -6423,7 +6521,6 @@ dependencies = [
"nix",
"typed-path",
"windows-sys 0.61.2",
"zopfli",
]
[[package]]
@@ -6511,11 +6608,6 @@ name = "egui_demo_lib"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a"
[[patch.unused]]
name = "egui_extras"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a"
[[patch.unused]]
name = "egui_inspection"
version = "0.37.0"
+10 -16
View File
@@ -4,7 +4,7 @@ members = ["crates/*", "apps/*", "xtask"]
default-members = ["crates/*", "apps/deckcraft", "apps/deckcraft-cli"]
[workspace.package]
version = "0.15.0"
version = "0.18.1"
edition = "2024"
license = "MIT OR Apache-2.0"
rust-version = "1.95"
@@ -27,16 +27,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 +49,15 @@ 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 = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", version = "=0.1.0", default-features = false }
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", 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 = "09dcbc225b95232998498a40ca56e24716571d53", version = "=0.1.0", default-features = false }
craft-audio = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53", 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 = "09dcbc225b95232998498a40ca56e24716571d53", version = "=0.1.0", default-features = false, features = ["h264-decode", "hevc-decode", "vp9-decode", "av1-decode", "threads"] }
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", 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 = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", features = ["egui"] }
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", version = "=0.1.0" }
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", version = "=0.1.0" }
i_overlay = "9"
quick-xml = "0.41"
zip = { version = "3", default-features = false, features = ["deflate"] }
@@ -109,7 +103,7 @@ codegen-units = 16
debug = 0
[patch.crates-io]
krilla = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", version = "=0.8.2" }
krilla = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", 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
+15
View File
@@ -2,8 +2,12 @@
**Stage: alpha** · next: beta, ~20 points and ~120–210 h away
> **Last reviewed:** 2026-11-04 · **Last updated:** 2026-11-11 · **Change:** trivial (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)
> **Last reviewed:** 2026-11-02 · **Last updated:** 2026-11-02 · **Change:** trivial (File › About replaces Account; product links, credits and appearance retained; parity scores unchanged) · **Target:** Microsoft PowerPoint for Mac 16.113 (Microsoft 365)
> **Last reviewed:** 2026-10-14 · **Last updated:** 2026-11-01 · **Change:** trivial (progress log: fix for a workflow-breaking regression: Ctrl+C/X/V use the system clipboard again, broken for all text and objects from 0.11.1 to 0.15.0; pasted and duplicated placeholders keep their look; stage and scores unchanged) · **Target:** Microsoft PowerPoint for Mac 16.113 (Microsoft 365)
> **Last reviewed:** 2026-10-14 · **Last updated:** 2026-11-01 · **Change:** trivial (locked No Fill and no-outline click-through regression coverage added; No Fill hit-testing follows resolved fill instead of the presence of a style (#1621); scores unchanged) · **Target:** Microsoft PowerPoint for Mac 16.113 (Microsoft 365)
> **Last reviewed:** 2026-10-14 · **Last updated:** 2026-10-30 · **Change:** trivial (progress log: read-only `slide.actions` lists shape and table text actions; parity scores unchanged) · **Target:** Microsoft PowerPoint for Mac 16.113 (Microsoft 365)
> **Last reviewed:** 2026-10-14 · **Last updated:** 2026-10-27 · **Change:** trivial (progress log: Set Up Slide Show ▸ Keep mouse cursor visible, #1445; stage and scores unchanged) · **Target:** Microsoft PowerPoint for Mac 16.113 (Microsoft 365)
@@ -150,6 +154,10 @@ 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-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) |
| 2026-11-02 | Native and PPTX saves retain optional table right-to-left direction, including explicit false and omission; two-save round-trip tests keep labeled columns in order |
| 2026-11-02 | Text runs preserve alternate-language metadata independently from the main language through native and PPTX saves |
| 2026-11-02 | Picture compression measures displayed dimensions through nested group transforms; checklist score unchanged |
@@ -159,6 +167,9 @@ app's JSON control channel (`deckcraft --control PORT`).
| 2026-11-02 | PPTX reflection effects keep their authored start position, affine scale/skew, direction and fade, alignment, and rotation flag through native and PPTX saves |
| 2026-11-03 | Reuse Slides brings along referenced custom table styles, including grouped tables, and remaps conflicting style ids without changing destination tables |
| 2026-11-02 | Tiled picture fills in groups: a tile (`a:blipFill` with `a:tile`) keeps its size, offset and anchor in slide points whatever the group's child-coordinate scale, uniform or stretched unevenly, nested or flipped (also a group's own tile fill passed to its members, and a picture shape's own tile, with its shadow and reflection), and tiles with `rotWithShape="0"` keep their size in points at any zoom, as PowerPoint draws them (measured on its own PNG export of a synthetic probe deck: every grouped case within ~3/255 of the ungrouped control's own difference from PowerPoint, which is ~13/255 from resampling). Before, a group whose child space was ~1800 times smaller than its box showed one flat texel of the texture (a Venn diagram with paper-textured circles and a hatched intersection) |
| 2026-11-01 | Ctrl+V of a picture copied in a browser (Copy Image) pastes the picture, not a text box with its link and alt text; the link still pastes as text when no picture came with it |
| 2026-11-01 | Copy, Cut, Paste, Duplicate and Alt+drag of a title, body or other placeholder on a slide keep its look: the copy is a regular shape that takes the size, font, bullets, indents, autofit and box its layout and master gave it, instead of 18 pt unbulleted text |
| 2026-11-01 | Clipboard: Ctrl+C, Ctrl+X and Ctrl+V use the system clipboard again, so text and pictures paste from other apps and in-app copies paste back. Since the egui 0.37 migration (#1350) the desktop build had a clipboard that only worked inside the app. On native Wayland, text goes through egui's Wayland clipboard; HTML stays on X11, macOS and Windows |
| 2026-10-31 | No Fill shape interiors allow selection and editing of underlying objects, even when a quick style remains; hit-testing uses the renderer’s fill resolution. Outlines, rotation, text targets and actual fills keep their hit behaviour (#1621) |
| 2026-10-30 | Print-preview tests use the URL emitted by egui and its exact file path; previews are explicitly removed before the macOS process exits |
| 2026-10-30 | `slide.actions` lists shape click and hover actions and text-run hyperlinks (including table cells and grouped shapes) for audit; listing never runs an action. The UI pane is a follow-up |
@@ -330,10 +341,14 @@ 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 |
| 2026-11-03 | trivial | Progress log: web `?host=parent` bridge to a framing page; scores unchanged |
| 2026-11-02 | trivial | File › About replaces Account; product links, credits and appearance retained |
| 2026-11-02 | trivial | Progress log: tiled picture fills keep their size in scaled groups. Scores and milestone names unchanged |
| 2026-11-01 | trivial | Workflow-breaking regression fixed: the desktop build uses the system clipboard again (eframe `clipboard` feature) |
| 2026-10-31 | trivial | No Fill hit-testing follows resolved fill instead of the presence of a style (#1621); scores unchanged |
| 2026-10-30 | trivial | Progress log: read-only slide action inspection; parity scores unchanged |
| 2026-10-29 | trivial | Progress log: going back in the slide show reverses the left slide's transition (#1531). Scores and milestone names 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 {
+5 -1
View File
@@ -16,6 +16,10 @@ path = "src/main.rs"
[dependencies]
deckcraft-engine = { workspace = true }
deckcraft-ui-egui = { workspace = true }
# The page command API (storytold/craft-libs#413): window.deckcraft over the control channel.
craft-web-api = { git = "https://github.com/storytold/craft-libs", rev = "fd5e2feff6ea25bed371a99cc732b3968d621505", version = "0.1.0" }
# The host messages (storytold/craft-libs): one contract and one bridge for the web builds.
craft-web-host = { git = "https://github.com/storytold/craft-libs", rev = "fd5e2feff6ea25bed371a99cc732b3968d621505", version = "0.1.0" }
egui = { workspace = true }
log = { workspace = true }
serde_json = { workspace = true }
@@ -27,4 +31,4 @@ rfd = { workspace = true }
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
js-sys = "0.3"
web-sys = { version = "0.3", features = ["Window", "Document", "Element", "HtmlElement", "HtmlCanvasElement", "HtmlAnchorElement", "Blob", "BlobPropertyBag", "Url", "Location", "CssStyleDeclaration", "MessageEvent", "Event", "EventTarget"] }
web-sys = { version = "0.3", features = ["Window", "Document", "Element", "HtmlElement", "HtmlCanvasElement", "HtmlAnchorElement", "Blob", "BlobPropertyBag", "Url", "Location", "CssStyleDeclaration", "MessageEvent", "Event", "EventTarget", "UrlSearchParams"] }
+151
View File
@@ -0,0 +1,151 @@
//! `window.deckcraft`: the page command API (`craft-web-api`, storytold/craft-libs#413) over the
//! app's own control channel, the one the desktop control server drives (`docs/control-protocol.md`).
//!
//! The crate owns the JavaScript side (`request`, `execute`, `commands`, `docInspect`, `uiInspect`,
//! `screenshot`, as promises with a timeout). This module is DeckCraft's part: a [`Request`] that
//! queues a [`ControlRequest`] for the app, which answers it on its next frame, and waits for the
//! reply. A call the page gave up on is released ([`Pending`]), so the app skips it if it has not started it, instead of
//! applying a command the page was told had failed.
use std::sync::Arc;
use std::sync::atomic::AtomicU8;
use std::sync::mpsc::{Receiver, TryRecvError};
use deckcraft_ui_egui::ControlRequest;
use serde_json::{Value, json};
/// How often a waiting call looks for the app's reply, in milliseconds (about a frame).
#[cfg(target_arch = "wasm32")]
const POLL_MS: i32 = 16;
/// A queued control request whose reply has not arrived. Dropping it before the reply (the page's
/// timeout fired, or the call was abandoned) releases the request: the app skips it if it has not started it (one it has taken still finishes).
struct Pending {
reply: Receiver<Value>,
state: Arc<AtomicU8>,
answered: bool,
}
impl Pending {
/// `Some` once the app has answered or gone away; `None` while the request is still waiting.
fn poll(&mut self) -> Option<Value> {
match self.reply.try_recv() {
Ok(reply) => {
self.answered = true;
Some(reply)
}
Err(TryRecvError::Empty) => None,
Err(TryRecvError::Disconnected) => {
self.answered = true;
Some(json!({"ok": false, "error": "the app dropped the request"}))
}
}
}
}
impl Drop for Pending {
fn drop(&mut self) {
if !self.answered {
ControlRequest::abandon(&self.state);
}
}
}
#[cfg(target_arch = "wasm32")]
pub use web::install;
#[cfg(target_arch = "wasm32")]
mod web {
use std::rc::Rc;
use std::sync::mpsc::channel;
use craft_web_api::methods::Methods;
use craft_web_api::page::{PageApi, Request};
use deckcraft_ui_egui::{ControlRequest, SlideApp};
use serde_json::json;
use wasm_bindgen_futures::JsFuture;
use super::{POLL_MS, Pending};
/// The app id in `window.<app>`.
const APP: &str = "deckcraft";
/// Give the app its control queue and put `window.deckcraft` on the page. An existing
/// `window.deckcraft` keeps the functions it already has.
pub fn install(app: &mut SlideApp, ctx: egui::Context) {
let (queue, requests) = channel::<ControlRequest>();
app.control_rx = Some(requests);
let request: Request = Rc::new(move |method, params| {
let (control, reply) = ControlRequest::new(method, params);
let mut pending = Pending { reply, state: control.state.clone(), answered: false };
if queue.send(control).is_err() {
pending.answered = true;
return Box::pin(async { json!({"ok": false, "error": "the app is not running"}) });
}
ctx.request_repaint();
Box::pin(async move {
loop {
if let Some(reply) = pending.poll() {
return reply;
}
sleep(POLL_MS).await;
}
})
});
if let Err(e) = Rc::new(PageApi::new(APP, Methods::default(), request)).install() {
log::warn!("couldn't install window.{APP}: {e:?}");
}
}
async fn sleep(ms: i32) {
let promise = js_sys::Promise::new(&mut |resolve, _| {
if let Some(window) = web_sys::window() {
let _ = window.set_timeout_with_callback_and_timeout_and_arguments_0(&resolve, ms);
}
});
let _ = JsFuture::from(promise).await;
}
}
#[cfg(test)]
mod tests {
use std::sync::atomic::Ordering;
use super::*;
/// `ControlRequest`'s `state` values (`deckcraft_ui_egui::control`): the app has started it, or its
/// sender gave up.
const TAKEN: u8 = 1;
const ABANDONED: u8 = 2;
fn pending() -> (Pending, ControlRequest) {
let (control, reply) = ControlRequest::new("document.inspect", json!({}));
(Pending { reply, state: control.state.clone(), answered: false }, control)
}
#[test]
fn a_call_the_page_gave_up_on_is_released_so_the_app_skips_it() {
let (waiting, control) = pending();
drop(waiting);
assert_eq!(control.state.load(Ordering::SeqCst), ABANDONED);
assert!(!control.take(), "the app must not run a request the page was told had failed");
}
#[test]
fn an_answered_call_is_not_released() {
let (mut waiting, control) = pending();
assert!(control.take());
control.reply.send(json!({"ok": true, "result": 1})).unwrap_or_default();
assert_eq!(waiting.poll(), Some(json!({"ok": true, "result": 1})));
drop(waiting);
assert_eq!(control.state.load(Ordering::SeqCst), TAKEN);
}
#[test]
fn a_request_the_app_never_runs_waits_until_it_is_dropped() {
let (mut waiting, control) = pending();
assert_eq!(waiting.poll(), None);
drop(control);
assert_eq!(waiting.poll().and_then(|r| r["ok"].as_bool()), Some(false), "the app went away");
}
}
+149
View File
@@ -0,0 +1,149 @@
//! The host bridge, for pages that embed DeckCraft and keep its files (a Nextcloud app, a personal
//! file server). The messages and the two transports are `craft-web-host`'s
//! (storytold/craft-libs), the same contract as the other Craft apps' bridges, with the
//! `deckcraft:` prefix. This module is DeckCraft's part of it.
//!
//! - `?host=parent` (an `<iframe>`) talks to the same-origin parent over `postMessage`;
//! `data-host="page"` on the canvas (the page's own document, no iframe) uses DOM
//! `CustomEvent`s on the canvas instead. `?host=parent` wins when both are present. With no
//! parent, or a parent on another origin, there is no bridge: the normal app with downloads.
//! - `deckcraft:ready` (to the host): sent once the app is running and listening; send a file now. DeckCraft
//! reads bytes, so it says `file: false` and a host sends `bytes`.
//! - `deckcraft:open` (from the host): `{ name, bytes }`, bytes an `ArrayBuffer` or
//! `Uint8Array`, name a bare file name. It opens like a file picked with File › Open on the
//! desktop: a presentation sent with its name (.pptx, .potx, .ppsx, .deckcraft) has that name
//! as its file, so Save goes back to it; an outline (.txt, .md, .rtf), a file without a name and
//! a damaged file open untitled.
//! - `deckcraft:save` (to the host): `{ name, saveAs, bytes }` (with the bytes as a transferred `ArrayBuffer` over `postMessage`) for
//! every file DeckCraft would otherwise download. The host stores it and reports failures
//! itself. `saveAs` says whether the file becomes the presentation's file:
//! - Save: `false`. It writes the opened file under its own name and format (a .pptx stays a
//! .pptx), as the desktop app does. A name the parent never sent (an untitled presentation, an
//! outline, a file sent without a name, a repaired copy of a damaged file, a file the user
//! picked or dropped) is a new file: `true`, as for Save As, named `<title>.deckcraft` as
//! Save suggests on the desktop.
//! - Save As (File › Save As as .pptx or .deckcraft, Ctrl+Shift+S): `true`. The presentation is
//! now that file; the next Save sends its name with `false`.
//! - Export (PDF, pictures, outline, GIF, handouts, a Save As type that isn't a presentation)
//! and Print: `false`. They are copies; the presentation's file stays the same.
//! - `deckcraft:dirty` whenever unsaved changes appear or go away, `deckcraft:failed` when the
//! app couldn't start or refused a file of 2 GiB or larger.
//!
//! In host mode the app starts with an empty presentation instead of the sample, which gives way to
//! the first file the host sends; the host guards leaving the page, and `?author=` names the user
//! for comments.
use std::cell::RefCell;
use std::rc::Rc;
use craft_web_host::web::{FileData, Host, Message};
use deckcraft_ui_egui::{Services, SlideApp};
/// The app id in the host messages (`deckcraft:ready`).
const APP: &str = "deckcraft";
/// The name `craft-web-host` gives an open request that carries none (it cleans a host's name and
/// falls back to this): no name is no file, so the presentation opens untitled.
const NAMELESS: &str = "document";
/// The largest file a host may send. File › Open on the desktop refuses anything over 2 GiB.
/// On wasm32 a `Vec` cannot hold 2 GiB (`isize::MAX` is one byte less), so a file of exactly
/// 2 GiB is refused too, before its bytes are copied.
const MAX_OPEN_BYTES: f64 = 2_147_483_648.0;
/// The longest user name taken from `?author=` (the preferences' own limit).
const MAX_AUTHOR_CHARS: usize = 256;
#[derive(Clone)]
pub struct Bridge {
host: Rc<Host>,
/// Files the host sent (the name, if it sent one), waiting for the next frame.
opened: Rc<RefCell<Vec<(Option<String>, Vec<u8>)>>>,
}
impl Bridge {
/// The bridge for this page, or `None` when DeckCraft isn't hosted: no `?host=parent` and no
/// `data-host="page"` on the canvas, an unframed `?host=parent` page (it stays the normal app
/// with downloads), or a parent on another origin.
pub fn detect(canvas: &web_sys::HtmlCanvasElement) -> Option<Self> {
let host = Host::connect(APP, canvas)?;
// An opaque origin (a file:// page in some browsers) can't be a postMessage target.
if host.mode() == craft_web_host::Mode::Parent && web_sys::window()?.location().origin().ok()? == "null" {
return None;
}
Some(Self { host: Rc::new(host), opened: Rc::default() })
}
/// Hand saved and exported files to the host instead of downloading them, and hear the files
/// it sends.
pub fn connect(&self, services: &mut Services, ctx: egui::Context) {
self.listen(ctx);
let bridge = self.clone();
services.host_save = Some(Box::new(move |name: &str, bytes: &[u8], save_as: bool| bridge.post_save(name, bytes, save_as)));
// Exports and Print are copies.
let bridge = self.clone();
services.download = Some(Box::new(move |name: &str, bytes: &[u8]| {
if let Err(e) = bridge.post_save(name, bytes, false) {
log::error!("couldn't hand {name} to the page: {e}");
}
}));
}
/// `?author=`: the host's name for the user signs new comments.
pub fn set_author(app: &mut SlideApp, author: &str) {
let author: String = author.trim().chars().take(MAX_AUTHOR_CHARS).collect();
if !author.is_empty() {
app.session.prefs.author = author;
}
}
pub fn post_ready(&self) {
let _ = self.host.post_ready(env!("CARGO_PKG_VERSION"), false);
}
/// The app couldn't start (no WebGPU or WebGL2, for example): let the host say so.
pub fn post_failed(&self, error: &str) {
let _ = self.host.post_failed(error, None);
}
/// Before each frame: open the files the host sent.
pub fn open_files(&self, app: &mut SlideApp) {
let opened = std::mem::take(&mut *self.opened.borrow_mut());
for (name, bytes) in opened {
app.open_from_host(name.as_deref(), &bytes);
}
}
/// After each frame: tell the host when unsaved changes appear or go away (the crate does not
/// repeat a state it already reported).
pub fn report_dirty(&self, app: &SlideApp) {
self.host.post_dirty(app.session.documents().iter().any(|d| d.is_dirty()));
}
fn post_save(&self, path: &str, bytes: &[u8], save_as: bool) -> Result<(), String> {
let name = std::path::Path::new(path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| path.to_string());
self.host.post_save(&name, bytes, None, save_as)
}
/// Hear `deckcraft:open` and queue the file for the next frame.
fn listen(&self, ctx: egui::Context) {
let opened = self.opened.clone();
let host = self.host.clone();
self.host.listen(move |message| {
let Message::Open { name, data } = message else { return };
if data.size() >= MAX_OPEN_BYTES {
log::warn!("host file {name} is 2 GiB or larger; not opened");
let _ = host.post_failed(&format!("{name} is 2 GiB or larger, which DeckCraft's web build cannot open"), None);
return;
}
let FileData::Bytes(array) = data else {
log::warn!("host file {name} came as a File; DeckCraft reads bytes only; not opened");
return;
};
// No name is no file: the presentation opens untitled, never under an invented name.
let name = (name != NAMELESS).then_some(name);
opened.borrow_mut().push((name, array.to_vec()));
ctx.request_repaint();
});
}
}
+8 -1
View File
@@ -9,15 +9,22 @@
//! - File → Open and Insert → Pictures/Audio/Video use the browser file picker; bytes arrive
//! asynchronously through `Services::inbox`, applied to the document, slide and operation
//! that started the pick (a dismissed picker or a closed document drops the file);
//! - Save and Export trigger a browser download;
//! - Save and Export trigger a browser download (or go to the framing page with `?host=parent`);
//! - dropped files are read asynchronously by `web::WebShell` and delivered through the inbox;
//! - preferences are not persisted.
//!
//! URL query flags: `?webgl` forces the WebGL2 backend instead of WebGPU; `?blank` starts with an
//! empty presentation instead of the sample deck; `?show` starts the slide show.
//! `?host=parent` (inside an `<iframe>`) talks to a same-origin parent page instead: it sends the
//! presentation to open and stores what DeckCraft saves (`host`), and `&author=` names the user
//! for comments.
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
#![forbid(unsafe_code)]
#[cfg(any(target_arch = "wasm32", test))]
mod api;
#[cfg(target_arch = "wasm32")]
mod host;
#[cfg(target_arch = "wasm32")]
mod presenter;
#[cfg(target_arch = "wasm32")]
+3
View File
@@ -251,6 +251,9 @@ impl Popup {
if value.get("nextChanged").and_then(|v| v.as_bool()) == Some(false) {
data.next = previous.next.clone();
}
if data.slides.is_none() {
data.slides = previous.slides.clone();
}
}
*target.borrow_mut() = Some(data);
*received.borrow_mut() = js_sys::Date::now();
+57 -7
View File
@@ -24,8 +24,18 @@ pub fn start() {
log::error!("missing <canvas id=\"{CANVAS_ID}\">");
return;
};
// Host modes (`craft-web-host`): `?host=parent`, a same-origin page framing DeckCraft,
// opens presentations in it and stores what it saves (`host`); `data-host="page"` on the
// canvas opts a page in that runs DeckCraft in its own document. The presenter popup is
// never the framed page.
let hosted = crate::presenter::token().is_none();
let bridge = if hosted { crate::host::Bridge::detect(&canvas) } else { None };
if hosted && bridge.is_none() && query_param("host").as_deref() == Some("parent") {
log::warn!("?host=parent needs a framing page on the same origin; saves download instead");
}
let app_bridge = bridge.clone();
let mut options = eframe::WebOptions::default();
if query().contains("webgl")
if query_has("webgl")
&& let eframe::egui_wgpu::WgpuSetup::CreateNew(create) = &mut options.wgpu_options.wgpu_setup
{
create.instance_descriptor.backends = eframe::wgpu::Backends::GL;
@@ -44,22 +54,43 @@ pub fn start() {
return Ok(Box::new(presenter) as Box<dyn eframe::App>);
}
let inbox: Inbox = Inbox::default();
let mut app = SlideApp::new(Session::new(), services(inbox.clone(), cc.egui_ctx.clone()));
let q = query();
if !q.contains("blank") {
let mut services = services(inbox.clone(), cc.egui_ctx.clone());
if let Some(bridge) = &app_bridge {
bridge.connect(&mut services, cc.egui_ctx.clone());
}
let mut app = SlideApp::new(Session::new(), services);
// In host mode the parent sends the presentation to edit; no sample meanwhile.
if app_bridge.is_some() {
if let Err(e) = app.start_for_host() {
log::error!("new presentation: {e}");
}
} else if !query_has("blank") {
if let Err(e) = deckcraft_engine::sample::open_sample(&mut app.session) {
log::error!("sample deck: {e}");
}
} else if let Err(e) = app.run("file.new", serde_json::json!({})) {
log::error!("new presentation: {e}");
}
if q.contains("show") {
if query_has("show") {
app.start_show(0, false);
}
Ok(Box::new(WebShell { app, inbox }))
if app_bridge.is_some()
&& let Some(author) = query_param("author")
{
crate::host::Bridge::set_author(&mut app, &author);
}
crate::api::install(&mut app, cc.egui_ctx.clone());
Ok(Box::new(WebShell { app, inbox, bridge: app_bridge }))
}),
)
.await;
// Ready only once the runner is up: starting can still fail after the app is made.
if let Some(bridge) = &bridge {
match &result {
Ok(()) => bridge.post_ready(),
Err(e) => bridge.post_failed(&format!("{e:?}")),
}
}
if let Some(el) = document.get_element_by_id(LOADING_ID) {
match result {
Ok(()) => el.remove(),
@@ -73,17 +104,36 @@ fn query() -> String {
web_sys::window().and_then(|w| w.location().search().ok()).unwrap_or_default()
}
fn query_param(key: &str) -> Option<String> {
web_sys::UrlSearchParams::new_with_str(&query()).ok()?.get(key)
}
/// Whether the URL has the flag `key` (`?show`, `&blank=1`). A whole parameter, never a substring:
/// `?author=` is free text.
fn query_has(key: &str) -> bool {
web_sys::UrlSearchParams::new_with_str(&query()).is_ok_and(|q| q.has(key))
}
/// Wraps the app to read dropped files asynchronously (browsers can't read them synchronously)
/// and feed them through the inbox.
struct WebShell {
app: SlideApp,
inbox: Inbox,
/// The parent page that keeps the files (`?host=parent`).
bridge: Option<crate::host::Bridge>,
}
impl eframe::App for WebShell {
/// Delegate one eframe logic pass to the wrapped application.
/// Delegate one eframe logic pass to the wrapped application, opening the files the parent
/// page sent first and telling it about unsaved changes after.
fn logic(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
if let Some(bridge) = &self.bridge {
bridge.open_files(&mut self.app);
}
self.app.logic(ctx);
if let Some(bridge) = &self.bridge {
bridge.report_dirty(&self.app);
}
if self.app.show.is_none()
&& let Some(host) = self.app.services.presenter.as_mut()
{
+3 -1
View File
@@ -14,7 +14,9 @@ workspace = true
deckcraft-engine = { workspace = true }
deckcraft-ui-egui = { workspace = true }
deckcraft-media = { workspace = true }
eframe = { workspace = true, features = ["default_fonts", "wgpu", "accesskit", "links", "wayland", "x11"] }
# `clipboard` is the OS clipboard (Ctrl+C/X/V with other apps). Without it egui simulates one inside
# the app; the egui 0.37 migration (#1350) dropped it from this list.
eframe = { workspace = true, features = ["default_fonts", "wgpu", "accesskit", "clipboard", "links", "wayland", "x11"] }
egui = { workspace = true }
serde_json = { workspace = true }
log = { workspace = true }
+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]
+52 -5
View File
@@ -51,6 +51,9 @@ impl eframe::App for App {
}
fn on_exit(&mut self) {
save_prefs(&self.0);
// The window is closing for good: a ready update's helper starts now and installs it once
// the process has exited.
self.0.start_pending_update();
// macOS (#279): end the process here, before eframe tears the window down. AccessKit's
// macOS adapter subclasses the window's content view and restores its class on teardown;
// AppKit's Touch Bar support observes that view (KVO), so in the next display cycle
@@ -103,7 +106,13 @@ fn save_prefs(app: &SlideApp) {
}
}
fn services() -> Services {
/// The desktop services. `native_wayland`: the window is a Wayland client (not Xwayland).
fn services(native_wayland: bool) -> Services {
// On native Wayland, egui-winit owns the clipboard through winit's data device, which carries
// text and PNG but no HTML. `pasteboard` (arboard, built for X11 only) would put a copy on the
// Xwayland selection instead and skip egui's Wayland claim, so Ctrl+V would read something
// else. There, text goes through egui and pictures arrive as `PasteImage` events.
let pasteboard = !native_wayland;
Services {
pick_open: Some(Box::new(|purpose: &str| {
let d = rfd::FileDialog::new();
@@ -126,9 +135,9 @@ fn services() -> Services {
pick_save: Some(Box::new(|name: &str| rfd::FileDialog::new().set_file_name(name).save_file().map(|p| p.to_string_lossy().to_string()))),
read: Some(Box::new(|p: &str| std::fs::read(p).map_err(|e| e.to_string()))),
write: Some(Box::new(|p: &str, b: &[u8]| std::fs::write(p, b).map_err(|e| e.to_string()))),
clipboard_image: Some(Box::new(pasteboard::image_png)),
clipboard_html: Some(Box::new(pasteboard::html)),
clipboard_set_html: Some(Box::new(pasteboard::set_html)),
clipboard_image: pasteboard.then(|| Box::new(pasteboard::image_png) as Box<dyn FnMut() -> Option<Vec<u8>>>),
clipboard_html: pasteboard.then(|| Box::new(pasteboard::html) as Box<dyn FnMut() -> Option<String>>),
clipboard_set_html: pasteboard.then(|| Box::new(pasteboard::set_html) as Box<dyn FnMut(&str, &str) -> bool>),
// Media is mixed and clocked by the output device; `DECKCRAFT_NO_AUDIO` plays silently.
audio_out: std::env::var_os("DECKCRAFT_NO_AUDIO")
.is_none()
@@ -137,6 +146,14 @@ fn services() -> Services {
}
}
/// Displays winit lists for `window`, counting an output Wayland lists twice once. `None` when it
/// lists none: Presenter View then stays as it was.
#[cfg(all(unix, not(target_os = "macos")))]
fn display_count(window: &winit::window::Window) -> Option<usize> {
let n = window.available_monitors().collect::<std::collections::BTreeSet<_>>().len();
(n > 0).then_some(n)
}
const APP_ID: &str = "ai.storyteller.deckcraft";
const USAGE: &str = "\
@@ -247,6 +264,11 @@ fn main() -> eframe::Result {
false
}
};
// `Default` lets winit choose, and winit takes Wayland whenever `WAYLAND_DISPLAY` is set.
#[cfg(all(unix, not(target_os = "macos")))]
let native_wayland = !x11 && std::env::var_os("WAYLAND_DISPLAY").is_some_and(|w| !w.is_empty());
#[cfg(not(all(unix, not(target_os = "macos"))))]
let native_wayland = false;
#[cfg(all(unix, not(target_os = "macos")))]
if x11 {
options.event_loop_builder = Some(Box::new(|b| {
@@ -268,7 +290,12 @@ fn main() -> eframe::Result {
options,
Box::new(move |cc| {
deckcraft_ui_egui::install_ime_pass_hooks(&cc.egui_ctx);
let mut app = SlideApp::new(Session::new(), services());
let mut app = SlideApp::new(Session::new(), services(native_wayland));
// Presenter View asks winit how many displays there are when a show starts.
#[cfg(all(unix, not(target_os = "macos")))]
if let Some(window) = cc.winit_window().cloned() {
app.services.displays = Some(Box::new(move || display_count(&window)));
}
load_prefs(&mut app);
app.enable_update_checks();
// AutoRecover: reopen what a previous run left unsaved, then keep it current.
@@ -335,6 +362,26 @@ mod tests {
assert_eq!(stdout.contains("on_exit returned"), !cfg!(target_os = "macos"), "{stdout}");
}
/// The egui 0.37 migration (#1350) left `clipboard` out of eframe's features. egui then
/// simulated a clipboard inside the app: nothing pasted from other apps, and Ctrl+C/Ctrl+V
/// lost text that went out as HTML. Keep the OS clipboard on.
#[test]
fn eframe_uses_the_os_clipboard() {
let manifest = include_str!("../Cargo.toml");
let eframe = manifest.lines().find(|l| l.starts_with("eframe = ")).unwrap_or_default();
assert!(eframe.contains("\"clipboard\""), "eframe must enable the OS clipboard: {eframe}");
}
/// On native Wayland, the X11-only pasteboard would bypass egui's Wayland clipboard; elsewhere
/// it carries HTML and pictures beside egui's text.
#[test]
fn the_pasteboard_stays_off_native_wayland() {
let s = services(true);
assert!(s.clipboard_html.is_none() && s.clipboard_set_html.is_none() && s.clipboard_image.is_none());
let s = services(false);
assert!(s.clipboard_html.is_some() && s.clipboard_set_html.is_some() && s.clipboard_image.is_some());
}
#[test]
fn help_exits_instead_of_opening_a_file() {
assert!(matches!(startup(None, ["--help".to_string()]), Startup::Help));
-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;
+45 -16
View File
@@ -107,7 +107,7 @@ fn copy_inner(s: &mut Session, p: &Value) -> Result<String> {
s.clipboard = crate::Clipboard { text: Some(TextBody::from_text(&plain)), plain: plain.clone(), ..Default::default() };
return Ok(plain);
}
let shapes: Vec<Shape> = st
let mut shapes: Vec<Shape> = st
.selection
.shapes
.iter()
@@ -120,14 +120,33 @@ fn copy_inner(s: &mut Session, p: &Value) -> Result<String> {
})
})
.collect();
// 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();
detach_on_slide(&st.doc, &st.selection, &mut shapes);
let mids = crate::media_refs::shapes_media(&shapes);
let media = st.doc.media.iter().filter(|m| mids.contains(&m.id)).cloned().collect();
let plain = shapes.iter().filter_map(|x| x.text.as_ref().map(|t| t.text())).collect::<Vec<_>>().join("\n");
let source_theme = st.current_slide().and_then(|sl| st.doc.master_for(sl)).map(|(m, _)| (m.scheme(), m.theme.fonts.clone()));
s.clipboard = crate::Clipboard { shapes, media, plain: plain.clone(), source_theme, ..Default::default() };
// The theme of what is being edited: a slide's or layout's colour map, else the master's.
let source_theme = super::text::ctx_for(&st.doc, &st.selection).map(|ctx| crate::SourceTheme {
colors: ctx.master.theme.colors.clone(),
color_map: ctx.color_map.clone(),
fonts: ctx.master.theme.fonts.clone(),
});
s.clipboard = crate::Clipboard { shapes, placeholders, media, plain: plain.clone(), source_theme, ..Default::default() };
Ok(plain)
}
/// On a slide, a copy of a placeholder is a regular shape: give `shapes` the look they inherit
/// there. In Slide Master view a copy stays a placeholder, so this changes nothing.
pub(crate) fn detach_on_slide(doc: &deckcraft_model::Presentation, sel: &crate::Selection, shapes: &mut [Shape]) {
if sel.target == crate::Target::Slides
&& let Some(ctx) = super::text::ctx_for(doc, sel)
{
shapes.iter_mut().for_each(|sh| deckcraft_model::resolve::detach_placeholder(&ctx, sh));
}
}
fn copying_caret(s: &Session) -> bool {
s.active().is_some_and(|d| d.selection.text.as_ref().is_some_and(|t| !t.is_range()))
}
@@ -319,6 +338,13 @@ fn paste(s: &mut Session, p: &Value) -> Result<Value> {
return s.edit(|doc, sel| {
let media = bring_media("edit.paste", doc, &clip.media)?;
let mut shapes = clip.shapes.clone();
if sel.target != crate::Target::Slides {
for sh in &mut shapes {
if let Some(original) = clip.placeholders.iter().find(|p| p.id == sh.id) {
sh.clone_from(original);
}
}
}
reid("edit.paste", doc, &mut shapes)?;
crate::media_refs::visit_mut::shapes(&mut shapes, &mut remap_media(&media));
let (mut taken, mut count) = super::current_names(doc, sel);
@@ -340,7 +366,8 @@ fn paste(s: &mut Session, p: &Value) -> Result<Value> {
x.x += off;
x.y += off;
}
// A pasted placeholder becomes a regular shape keeping its look.
// A placeholder pasted on a slide becomes a regular shape. One copied from a slide
// already has its look; see `copy_inner`.
if sh.ph.is_some() && sel.target == crate::Target::Slides {
sh.ph = None;
}
@@ -404,8 +431,9 @@ fn paste_special(s: &mut Session, p: &Value) -> Result<Value> {
"text" | "keepTextOnly" => paste_text(s, &json!({})),
"keepSource" | "keepSourceFormatting" => {
let saved = s.clipboard.clone();
if let Some((scheme, fonts)) = &saved.source_theme {
bake_theme(&mut s.clipboard.shapes, scheme, fonts);
if let Some(theme) = &saved.source_theme {
bake_theme(&mut s.clipboard.shapes, theme);
bake_theme(&mut s.clipboard.placeholders, theme);
}
let r = paste(s, &json!({}));
let pastes = s.clipboard.pastes;
@@ -420,8 +448,8 @@ fn paste_special(s: &mut Session, p: &Value) -> Result<Value> {
/// Replace theme colours and theme fonts with the values they have in `scheme` / `fonts`, so the
/// shapes keep their look under another theme.
pub(crate) fn bake_theme(shapes: &mut [Shape], scheme: &deckcraft_color::ColorScheme, fonts: &deckcraft_model::theme::FontScheme) {
fn rec(v: &mut Value, scheme: &deckcraft_color::ColorScheme, fonts: &deckcraft_model::theme::FontScheme, depth: usize) {
pub(crate) fn bake_theme(shapes: &mut [Shape], theme: &crate::SourceTheme) {
fn rec(v: &mut Value, theme: &crate::SourceTheme, depth: usize) {
if depth > 256 {
return;
}
@@ -430,7 +458,7 @@ pub(crate) fn bake_theme(shapes: &mut [Shape], scheme: &deckcraft_color::ColorSc
if o.get("kind").and_then(Value::as_str) == Some("scheme")
&& let Some(slot) = o.get("slot").and_then(|x| serde_json::from_value::<deckcraft_color::SchemeSlot>(x.clone()).ok())
&& slot != deckcraft_color::SchemeSlot::PhClr
&& let Ok(rgb) = serde_json::to_value(scheme.get(slot))
&& let Ok(rgb) = serde_json::to_value(theme.colors.get(deckcraft_model::map_slot(&theme.color_map, slot)))
{
o.clear();
o.insert("kind".into(), json!("rgb"));
@@ -438,14 +466,14 @@ pub(crate) fn bake_theme(shapes: &mut [Shape], scheme: &deckcraft_color::ColorSc
return;
}
for x in o.values_mut() {
rec(x, scheme, fonts, depth + 1);
rec(x, theme, depth + 1);
}
}
Value::Array(a) => a.iter_mut().for_each(|x| rec(x, scheme, fonts, depth + 1)),
Value::Array(a) => a.iter_mut().for_each(|x| rec(x, theme, depth + 1)),
Value::String(t) if t.starts_with('+') => {
let set = match t.get(1..3) {
Some("mj") => &fonts.major,
Some("mn") => &fonts.minor,
Some("mj") => &theme.fonts.major,
Some("mn") => &theme.fonts.minor,
_ => return,
};
let face = match t.get(3..) {
@@ -463,7 +491,7 @@ pub(crate) fn bake_theme(shapes: &mut [Shape], scheme: &deckcraft_color::ColorSc
}
for sh in shapes.iter_mut() {
let Ok(mut v) = serde_json::to_value(&*sh) else { continue };
rec(&mut v, scheme, fonts, 0);
rec(&mut v, theme, 0);
if let Ok(baked) = serde_json::from_value::<Shape>(v) {
*sh = baked;
}
@@ -476,8 +504,8 @@ fn paste_picture(s: &mut Session) -> Result<Value> {
if clip.shapes.is_empty() {
return Err(bad("edit.pasteSpecial", "copy shapes first to paste them as a picture"));
}
if let Some((scheme, fonts)) = &clip.source_theme {
bake_theme(&mut clip.shapes, scheme, fonts);
if let Some(theme) = &clip.source_theme {
bake_theme(&mut clip.shapes, theme);
}
let st = s.doc()?;
let mut doc = (*st.doc).clone();
@@ -533,6 +561,7 @@ fn duplicate(s: &mut Session, p: &Value) -> Result<Value> {
let mut copies: Vec<Shape> = list.iter().filter(|x| ids.contains(&x.id)).cloned().collect();
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);
// Duplicating the copies of the last Duplicate again repeats how far they were moved
// since (#966); anything else gets the usual offset.
let (dx, dy) = last
+13 -3
View File
@@ -253,8 +253,15 @@ pub struct LoadedFile {
#[cfg(not(target_arch = "wasm32"))]
pub fn read_presentation_file(path: &str) -> Result<LoadedFile> {
let bytes = crate::io::read_file(path).map_err(EngineError::Other)?;
let (doc, damaged) = open_checked(path, &bytes)?;
let native = deckcraft_format::sniff(&bytes) || deckcraft_pptx::sniff(&bytes);
loaded_file(path, &bytes)
}
/// Parse `bytes` as the presentation file `path`, as [`read_presentation_file`] does after
/// reading it. The web host bridge uses it for a file its page sent, which then saves back to
/// that name like a file opened on the desktop.
pub fn loaded_file(path: &str, bytes: &[u8]) -> Result<LoadedFile> {
let (doc, damaged) = open_checked(path, bytes)?;
let native = deckcraft_format::sniff(bytes) || deckcraft_pptx::sniff(bytes);
Ok(LoadedFile { path: path.to_string(), doc, native, damaged })
}
@@ -470,7 +477,10 @@ pub fn format_for_path(path: &str) -> &'static str {
}
}
fn mark_saved(s: &mut Session, path: Option<String>) -> Result<()> {
/// The active presentation is saved as it is now: no unsaved changes, and `path` (when given)
/// becomes its file. `file.save` calls it after writing; the web host bridge calls it once the
/// page has the bytes.
pub fn mark_saved(s: &mut Session, path: Option<String>) -> Result<()> {
let st = s.doc_mut()?;
st.saved_doc = st.doc.clone();
if path.is_some() {
+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(())
+3 -9
View File
@@ -120,13 +120,7 @@ pub(super) fn line_spacing(s: &mut Session, p: &Value) -> Result<Value> {
}
pub(super) fn paragraph(s: &mut Session, p: &Value) -> Result<Value> {
let a = str_param(p, "align").map(|a| match a {
"center" => Align::Center,
"right" => Align::Right,
"justify" => Align::Justify,
"distributed" => Align::Distributed,
_ => Align::Left,
});
let a = str_param(p, "align").map(|a| parse_align("format.paragraph", a)).transpose()?;
let ml = f64_param(p, "indentLeft");
let ind = f64_param(p, "indentFirst");
let sb = f64_param(p, "spaceBefore");
@@ -182,10 +176,10 @@ pub(super) fn clamp_pt(v: f64) -> f64 {
fn parse_align(cmd: &str, s: &str) -> Result<Align> {
Ok(match s {
"left" | "l" => Align::Left,
"center" | "ctr" => Align::Center,
"center" | "centre" | "ctr" => Align::Center,
"right" | "r" => Align::Right,
"justify" | "just" => Align::Justify,
"justLow" => Align::JustLow,
"justLow" | "justifyLow" => Align::JustLow,
"distributed" | "dist" => Align::Distributed,
"thaiDist" => Align::ThaiDistributed,
_ => return Err(bad(cmd, "`align` must be left, center, right, justify, justLow, distributed or thaiDist")),
+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());
+35 -1
View File
@@ -219,7 +219,7 @@ fn neighbour(s: &mut Session, p: &Value, forward: bool) -> Result<Value> {
let cmd = if forward { "comment.next" } else { "comment.prev" };
let (threads, slide, index) = {
let st = s.doc()?;
(comment_threads(&st.doc), usize_param(p, "slide").unwrap_or(st.selection.slide), usize_param(p, "index"))
(comment_threads(&st.doc), usize_param(p, "slide").unwrap_or(st.selection.slide), super::opt_usize_param(p, "index", cmd)?)
};
if threads.is_empty() {
return Err(bad(cmd, "no comments"));
@@ -862,6 +862,40 @@ mod tests {
assert_eq!(here["text"], "on first");
}
#[test]
fn malformed_comment_navigation_index_does_not_change_selection() {
let mut s = Session::with_new();
s.execute("comment.add", &json!({"text": "first"})).unwrap();
s.execute("shape.insert", &json!({"preset": "rect", "rect": [10.0, 10.0, 40.0, 40.0]})).unwrap();
s.execute("slide.new", &json!({})).unwrap();
s.execute("comment.add", &json!({"text": "second"})).unwrap();
s.execute("slide.go", &json!({"index": 0})).unwrap();
let shape = s.doc().unwrap().doc.slides.first().and_then(|slide| slide.shapes.first()).map(|shape| shape.id).unwrap();
s.doc_mut().unwrap().selection.shapes = vec![shape];
for (command, value) in [
("comment.next", json!(-1)),
("comment.next", json!(1.5)),
("comment.next", json!("bad")),
("comment.prev", json!(-1)),
("comment.prev", json!(1.5)),
("comment.prev", json!("bad")),
] {
let before = s.doc().unwrap().selection.clone();
let error = s.execute(command, &json!({"index": value})).unwrap_err();
assert!(error.to_string().contains("valid index"), "{command} {value}: {error}");
assert_eq!(s.doc().unwrap().selection, before, "{command} {value}");
}
let zero = s.execute("comment.next", &json!({"slide": 0, "index": 0})).unwrap();
assert_eq!(zero["slide"], 1);
assert_eq!(zero["index"], 0);
s.execute("slide.go", &json!({"index": 0})).unwrap();
let omitted = s.execute("comment.next", &json!({})).unwrap();
assert_eq!(omitted["slide"], 0);
assert_eq!(omitted["index"], 0);
}
#[test]
fn timestamps_are_iso_utc() {
assert_eq!(iso_utc(0), "1970-01-01T00:00:00Z");
+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();
+21 -2
View File
@@ -10,7 +10,7 @@ use serde_json::{Value, json};
use super::{
CommandSpec, NO_ID_SPACE, bad, bool_or, bool_param, cmd, color_param, color_value, edit_geometry, edit_shapes, f64_param, has_selection,
has_slide, id_param, ok, str_param, targets, usize_param, with_param, xfrm_of,
has_slide, id_param, ok, opt_usize_param, str_param, targets, usize_param, with_param, xfrm_of,
};
use crate::{Result, Session};
@@ -103,7 +103,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Shape Fill",
["Shape Format", "Shape Styles"],
None,
"{color? | none?: true | background?: true (the slide background shows through; shapes and text boxes only) | gradient?: {stops: [[pos 0..1, color]], kind?: linear|radial|rectangular|path, angle?: deg (linear), focus?: [l, t, r, b] fractions (radial, rectangular and path: where the first stop sits), rotateWithShape?: bool} | picture?: base64 | pattern?: {preset, fg, bg} | transparency?: 0..1 | reset?: true, ids?}; pictureProps?: {tile?: bool, offsetX?, offsetY?: pt, scaleX?, scaleY?: 0..10, align?: tl|t|tr|l|ctr|r|bl|b|br, mirror?: none|x|y|xy, stretch?: [l, t, r, b] fractions, rotateWithShape?: bool} adjusts a picture fill; a group also formats its members",
"{color? | none?: true | background?: true (the slide background shows through; shapes and text boxes only) | gradient?: {stops: [[pos 0..1, color]], kind?: linear|radial|rectangular|path, angle?: deg (linear), focus?: [l, t, r, b] fractions (radial, rectangular and path: where the first stop sits), rotateWithShape?: bool (false keeps linear and radial gradients upright on the slide; rectangular and path gradients turn with the shape when drawn)} | picture?: base64 | pattern?: {preset, fg, bg} | transparency?: 0..1 | reset?: true, ids?}; pictureProps?: {tile?: bool, offsetX?, offsetY?: pt, scaleX?, scaleY?: 0..10, align?: tl|t|tr|l|ctr|r|bl|b|br, mirror?: none|x|y|xy, stretch?: [l, t, r, b] fractions, rotateWithShape?: bool} adjusts a picture fill; a group also formats its members",
has_selection,
fill
),
@@ -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);
+5 -4
View File
@@ -1,11 +1,11 @@
use super::{
ColorRef, ColorTransform, Dash, Effects, Fill, Geom, Glow, Gradient, GradientShape, GradientStop, Line, LineEnd, PictureFill, PictureMode,
Reflection, Result, Rgba, SchemeSlot, Session, Shadow, ShapeStyle, Value, apply_fill, bad, bool_or, color_param, color_value, edit_geometry,
edit_shapes, f64_param, inherited_fills, json, requested_fill, str_param, targets, usize_param,
edit_shapes, f64_param, inherited_fills, json, opt_usize_param, requested_fill, str_param, targets,
};
pub(super) fn adjust(s: &mut Session, p: &Value) -> Result<Value> {
let i = usize_param(p, "index").unwrap_or(0).min(16);
let i = opt_usize_param(p, "index", "shape.adjust")?.unwrap_or(0).min(16);
let v = f64_param(p, "value").ok_or_else(|| bad("shape.adjust", "missing `value`"))?;
edit_geometry(s, p, "shape.adjust", |sh| {
if let Geom::Preset { name, adj } = &mut sh.geom {
@@ -74,7 +74,8 @@ pub(super) fn change(s: &mut Session, p: &Value) -> Result<Value> {
/// `{stops: [[pos 0–1, color], …], kind?: linear|radial|rectangular|path, angle?: deg (linear),
/// focus?: [l, t, r, b] fractions (radial/rectangular/path: where the first stop sits),
/// rotateWithShape?}`.
/// rotateWithShape?}`. The renderer keeps only linear and radial gradients upright when
/// `rotateWithShape` is false; rectangular and path gradients turn with the shape.
pub(crate) fn gradient_param(v: &Value) -> Option<Gradient> {
let stops: Vec<GradientStop> = v
.get("stops")?
@@ -555,7 +556,7 @@ pub fn quick_style_for(index: usize) -> (ShapeStyle, Option<Fill>, Option<Line>)
}
pub(super) fn quick_style(s: &mut Session, p: &Value) -> Result<Value> {
let i = usize_param(p, "index").unwrap_or(1).min(41);
let i = opt_usize_param(p, "index", "shape.quickStyle")?.unwrap_or(1).min(41);
let (st, f, l) = quick_style_for(i);
edit_shapes(s, p, "shape.quickStyle", |sh| {
sh.style = Some(st.clone());
+16 -5
View File
@@ -3,8 +3,9 @@ use super::{ColorRef, Fill, Geom, Line, NO_ID_SPACE, Result, Session, Value, bad
pub(super) fn sites(s: &mut Session, p: &Value) -> Result<Value> {
let id = id_param(p, "id", "shape.sites")?.ok_or_else(|| bad("shape.sites", "missing `id`"))?;
let st = s.doc()?;
let sh = st.shape(id).ok_or_else(|| bad("shape.sites", "no such shape"))?;
Ok(json!(crate::connect::sites(&st.doc, &st.selection, sh).iter().map(|q| [q.x, q.y]).collect::<Vec<_>>()))
// In slide space, as shape.connect uses them, for a shape inside a group too.
let v = crate::connect::sites_of(&st.doc, &st.selection, st.shapes(), id).ok_or_else(|| bad("shape.sites", "no such shape"))?;
Ok(json!(v.iter().map(|q| [q.x, q.y]).collect::<Vec<_>>()))
}
pub(super) fn connect(s: &mut Session, p: &Value) -> Result<Value> {
@@ -12,8 +13,13 @@ pub(super) fn connect(s: &mut Session, p: &Value) -> Result<Value> {
return Err(bad("shape.connect", "missing `from` / `to`"));
};
let st = s.doc()?;
let site_list = |id| st.shape(id).map(|sh| crate::connect::sites(&st.doc, &st.selection, sh)).unwrap_or_default();
let (a, b) = (site_list(from), site_list(to));
// Anywhere in the slide's tree: a shape inside a group takes a connector too (PowerPoint
// glues to grouped shapes), its sites where the group draws them.
let site_list = |id| {
crate::connect::sites_of(&st.doc, &st.selection, st.shapes(), id)
.ok_or_else(|| bad("shape.connect", format!("no shape {} on the current slide", id.0)))
};
let (a, b) = (site_list(from)?, site_list(to)?);
if a.is_empty() || b.is_empty() {
return Err(bad("shape.connect", "both shapes need connection sites (lines and groups have none)"));
}
@@ -35,6 +41,11 @@ pub(super) fn connect(s: &mut Session, p: &Value) -> Result<Value> {
};
let (p0, p1) = (a[ia], b[ib]);
let id = match id_param(p, "id", "shape.connect")? {
// The ends are in slide space and rerouting follows the slide's own connectors, so a line
// inside a group would be drawn in the group's space and never follow its shapes.
Some(id) if !st.shapes().iter().any(|sh| sh.id == id) && st.shape(id).is_some_and(|sh| sh.is_line()) => {
return Err(bad("shape.connect", "`id` names a line inside a group; ungroup it first, or leave `id` out for a new connector"));
}
Some(id) => id,
None => {
let preset = str_param(p, "preset").unwrap_or("straightConnector1");
@@ -93,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();
+7
View File
@@ -191,6 +191,13 @@ pub(super) fn rotate(s: &mut Session, p: &Value) -> Result<Value> {
pub(super) fn flip(s: &mut Session, p: &Value) -> Result<Value> {
let v = str_param(p, "axis").unwrap_or("horizontal").starts_with('v');
edit_geometry(s, p, "shape.flip", |sh| {
// PowerPoint greys Flip out for a SmartArt graphic (SmartArt Design > Right to Left mirrors it),
// also inside a group: ungrouping would hand the flip to the graphic.
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"));
}
if let Some(x) = sh.xfrm.as_mut() {
if v {
x.flip_v = !x.flip_v;
+43 -21
View File
@@ -253,9 +253,10 @@ fn cell_param(p: &Value, key: &str, cmd: &str) -> Result<Option<(usize, usize)>>
None | Some(Value::Null) => return Ok(None),
Some(v) => v,
};
let idx = |i: usize| v.as_array().and_then(|a| a.get(i)).and_then(Value::as_u64).and_then(|x| usize::try_from(x).ok());
match (idx(0), idx(1)) {
(Some(r), Some(c)) => Ok(Some((r, c))),
// Exactly [row, col]: an entry with more is as malformed as one with less.
let idx = |x: &Value| x.as_u64().and_then(|x| usize::try_from(x).ok());
match v.as_array().map(Vec::as_slice) {
Some([r, c]) if let (Some(r), Some(c)) = (idx(r), idx(c)) => Ok(Some((r, c))),
_ => Err(bad(cmd, format!("`{key}` is not a valid cell: {v}"))),
}
}
@@ -389,33 +390,54 @@ fn options(s: &mut Session, p: &Value) -> Result<Value> {
})
}
/// At most this many `cells` in one table cell command.
const MAX_CELLS: usize = 100_000;
/// The cells a command acts on: `cells`, else the selected cell range, else the cell holding the
/// caret, else every cell.
fn target_cells(s: &Session, p: &Value) -> Result<impl Fn(&Table) -> Vec<(usize, usize)> + use<>> {
let cells: Option<Vec<(usize, usize)>> = p
.get("cells")
.and_then(Value::as_array)
.map(|a| a.iter().take(100_000).filter_map(|c| Some((c.get(0)?.as_u64()? as usize, c.get(1)?.as_u64()? as usize))).collect());
fn target_cells(s: &Session, p: &Value, cmd: &str) -> Result<impl Fn(&Table) -> Result<Vec<(usize, usize)>> + use<>> {
// `cells` is a list of [row, col]: anything else, or an entry that is not one, is refused, never
// dropped or read as absent (which would act on the selection or the whole table).
let cells: Option<Vec<(usize, usize)>> = match p.get("cells") {
None | Some(Value::Null) => None,
Some(Value::Array(a)) if a.len() > MAX_CELLS => {
return Err(bad(cmd, format!("`cells` lists {} cells; at most {MAX_CELLS}", a.len())));
}
Some(Value::Array(a)) => Some(
a.iter()
.map(|c| {
cell_param(&json!({ "cell": c }), "cell", cmd)?.ok_or_else(|| bad(cmd, format!("`cells` entry {c} is not a [row, col] cell")))
})
.collect::<Result<_>>()?,
),
Some(v) => return Err(bad(cmd, format!("`cells` is a list of [row, col] cells, not {v}"))),
};
let st = s.doc()?;
// A selected range grows over the merged cells it touches; with only a caret, the caret's cell is used.
let sel = st.selection.cells.map(|(_, a, b)| (a, b, true)).or_else(|| st.selection.text.as_ref().and_then(|t| t.cell).map(|c| (c, c, false)));
let cmd = cmd.to_string();
Ok(move |t: &Table| match (&cells, sel) {
(Some(c), _) => c.clone(),
// Each listed cell must be in the table, so a command never reports success for a cell
// it could not edit.
(Some(c), _) => match c.iter().find(|(r, c)| *r >= t.n_rows() || *c >= t.n_cols()) {
Some((r, c)) => Err(bad(&cmd, format!("no cell [{r}, {c}]: the table has {} rows and {} columns", t.n_rows(), t.n_cols()))),
None => Ok(c.clone()),
},
(None, Some((a, b, range))) => {
let (a, b) = if range { cell_block(t, a, b) } else { (a, b) };
// The selection can name cells outside the table; only walk the ones that exist.
let (r1, c1) = (a.0.max(b.0).min(t.n_rows().saturating_sub(1)), a.1.max(b.1).min(t.n_cols().saturating_sub(1)));
(a.0.min(b.0)..=r1).flat_map(|r| (a.1.min(b.1)..=c1).map(move |c| (r, c))).collect()
Ok((a.0.min(b.0)..=r1).flat_map(|r| (a.1.min(b.1)..=c1).map(move |c| (r, c))).collect())
}
_ => (0..t.n_rows()).flat_map(|r| (0..t.n_cols()).map(move |c| (r, c))).collect(),
_ => Ok((0..t.n_rows()).flat_map(|r| (0..t.n_cols()).map(move |c| (r, c))).collect()),
})
}
fn cell_fill(s: &mut Session, p: &Value) -> Result<Value> {
let fill = if bool_or(p, "none", false) { Some(deckcraft_model::Fill::None) } else { color_param(p, "color").map(deckcraft_model::Fill::solid) };
let targets = target_cells(s, p)?;
let targets = target_cells(s, p, "table.cellFill")?;
with_table(s, p, "table.cellFill", move |t| {
for (r, c) in targets(t) {
for (r, c) in targets(t)? {
if let Some(cell) = t.cell_mut(r, c) {
cell.fill.clone_from(&fill);
}
@@ -447,9 +469,9 @@ fn borders(s: &mut Session, p: &Value) -> Result<Value> {
}
// "none" clears to no line, so the table style's own lines don't show through either.
let line = if which == "none" { Line::none() } else { line };
let targets = target_cells(s, p)?;
let targets = target_cells(s, p, "table.borders")?;
with_table(s, p, "table.borders", move |t| {
let list = targets(t);
let list = targets(t)?;
let (Some(r0), Some(r1), Some(c0), Some(c1)) =
(list.iter().map(|x| x.0).min(), list.iter().map(|x| x.0).max(), list.iter().map(|x| x.1).min(), list.iter().map(|x| x.1).max())
else {
@@ -463,9 +485,9 @@ fn borders(s: &mut Session, p: &Value) -> Result<Value> {
}
let n = match side {
LEFT => c.checked_sub(1).map(|c| (r, c, RIGHT)),
RIGHT => (c + 1 < cols).then_some((r, c + 1, LEFT)),
RIGHT => c.checked_add(1).filter(|c| *c < cols).map(|c| (r, c, LEFT)),
TOP => r.checked_sub(1).map(|r| (r, c, BOTTOM)),
_ => (r + 1 < rows).then_some((r + 1, c, TOP)),
_ => r.checked_add(1).filter(|r| *r < rows).map(|r| (r, c, TOP)),
};
if let Some((r, c, side)) = n
&& let Some(cell) = t.cell_mut(r, c)
@@ -523,9 +545,9 @@ fn cell_margins(s: &mut Session, p: &Value) -> Result<Value> {
};
let side = |k: &str, i: usize| f64_param(p, k).filter(|v| v.is_finite()).map_or(base[i], |v| v.clamp(0.0, 400.0));
let m = [side("left", 0), side("right", 1), side("top", 2), side("bottom", 3)];
let targets = target_cells(s, p)?;
let targets = target_cells(s, p, "table.cellMargins")?;
with_table(s, p, "table.cellMargins", move |t| {
for (r, c) in targets(t) {
for (r, c) in targets(t)? {
if let Some(cell) = t.cell_mut(r, c) {
cell.margins = Some(m);
}
@@ -560,9 +582,9 @@ fn cell_align(s: &mut Session, p: &Value) -> Result<Value> {
o => return Err(bad("table.cellAlign", format!("unknown anchor {o:?}; one of top, middle, bottom"))),
}),
};
let targets = target_cells(s, p)?;
let targets = target_cells(s, p, "table.cellAlign")?;
with_table(s, p, "table.cellAlign", move |t| {
for (r, c) in targets(t) {
for (r, c) in targets(t)? {
if let Some(cell) = t.cell_mut(r, c) {
if anchor.is_some() {
cell.anchor = anchor;
+44 -5
View File
@@ -1,14 +1,21 @@
//! Connectors glued to shapes: connection sites, snapping line ends to them, and rerouting attached
//! connectors whenever the shapes they connect move, resize or rotate.
use deckcraft_geom::{Point, Xfrm};
use deckcraft_geom::{Affine, Point, Rect, Xfrm};
use deckcraft_model::{Presentation, Shape, ShapeId, ShapeKind};
use crate::Selection;
use crate::cmd::xfrm_of;
/// Connection sites of `s` in slide coordinates (index = site index stored on connectors).
/// Connection sites of a top-level shape `s` in slide coordinates (index = site index stored on
/// connectors). For a shape inside a group use [`sites_of`] or [`sites_in`].
pub fn sites(doc: &Presentation, sel: &Selection, s: &Shape) -> Vec<Point> {
sites_in(doc, sel, s, Affine::IDENTITY)
}
/// Connection sites of `s` in slide coordinates, `to_abs` mapping the space its box lives in (the
/// slide, or the child space of the groups around it) onto the slide.
pub fn sites_in(doc: &Presentation, sel: &Selection, s: &Shape, to_abs: Affine) -> Vec<Point> {
if s.is_line() || matches!(s.kind, ShapeKind::Group { .. } | ShapeKind::Ink { .. }) {
return vec![];
}
@@ -17,10 +24,42 @@ pub fn sites(doc: &Presentation, sel: &Selection, s: &Shape) -> Vec<Point> {
if local.is_empty() {
local = vec![Point::new(x.w / 2.0, 0.0), Point::new(0.0, x.h / 2.0), Point::new(x.w / 2.0, x.h), Point::new(x.w, x.h / 2.0)];
}
let a = x.affine();
let a = to_abs * x.affine();
local.into_iter().map(|p| a * p).collect()
}
/// The shape `id` anywhere in `shapes` (a group's member included) with the affine that maps the
/// space its box lives in onto the slide: a connector glues to a grouped shape, as in PowerPoint,
/// and its sites are where the group draws them.
pub fn locate<'a>(doc: &Presentation, sel: &Selection, shapes: &'a [Shape], id: ShapeId) -> Option<(&'a Shape, Affine)> {
fn rec<'a>(doc: &Presentation, sel: &Selection, shapes: &'a [Shape], id: ShapeId, to_abs: Affine, depth: usize) -> Option<(&'a Shape, Affine)> {
if depth > 64 {
return None;
}
for s in shapes {
if s.id == id {
return Some((s, to_abs));
}
if let ShapeKind::Group { children, child } = &s.kind {
let x = xfrm_of(doc, sel, s);
let ch = Rect::new(child.x, child.y, child.x + child.w, child.y + child.h);
let nested = to_abs * deckcraft_geom::group_child_affine(&x, ch);
if let Some(f) = rec(doc, sel, children, id, nested, depth + 1) {
return Some(f);
}
}
}
None
}
rec(doc, sel, shapes, id, Affine::IDENTITY, 0)
}
/// Connection sites of shape `id` anywhere in `shapes`, in slide coordinates; `None` when there is
/// no such shape.
pub fn sites_of(doc: &Presentation, sel: &Selection, shapes: &[Shape], id: ShapeId) -> Option<Vec<Point>> {
locate(doc, sel, shapes, id).map(|(s, a)| sites_in(doc, sel, s, a))
}
/// The nearest connection site within `radius` of `p` on any visible shape except `exclude`.
pub fn nearest_site(
doc: &Presentation,
@@ -68,8 +107,8 @@ pub fn reroute(doc: &mut Presentation, sel: &Selection) {
let snapshot: Vec<Shape> = list.clone();
let view = doc.clone();
let site = |id: ShapeId, idx: u32| -> Option<Point> {
let s = snapshot.iter().find(|s| s.id == id)?;
let v = sites(&view, sel, s);
// Anywhere in the tree: a connector glued to a shape that is then grouped keeps following it.
let v = sites_of(&view, sel, &snapshot, id)?;
v.get(idx as usize).or(v.first()).copied()
};
let mut updates: Vec<(usize, Xfrm, bool, bool)> = vec![];
+26 -14
View File
@@ -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,12 +238,12 @@ 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}"));
}
}
}
+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);
}
};
+41 -6
View File
@@ -454,17 +454,30 @@ pub struct MediaStatus {
}
/// Clipboard contents copied within the app.
/// The theme a copy drew with. The colours and the map stay apart, as in
/// `deckcraft_model::resolve::Ctx::color`: the map renames only `tx1`, `bg1`, `tx2` and `bg2`, and
/// a direct `dk1` keeps the theme's colour.
#[derive(Clone, Debug)]
pub struct SourceTheme {
/// The theme's own colours, no colour map applied.
pub colors: deckcraft_color::ColorScheme,
pub color_map: Vec<(String, String)>,
pub fonts: deckcraft_model::theme::FontScheme,
}
#[derive(Clone, Debug, Default)]
pub struct Clipboard {
pub shapes: Vec<Shape>,
/// The copied placeholders as they were, by id. `shapes` holds a slide's placeholders as
/// regular shapes; a paste in Slide Master view takes these, which stay placeholders.
pub placeholders: Vec<Shape>,
pub slides: Vec<Slide>,
/// Media the copied shapes/slides reference.
pub media: Vec<deckcraft_model::MediaItem>,
pub text: Option<deckcraft_model::TextBody>,
pub plain: String,
/// The copied shapes' theme colours (colour map applied) and fonts, for Paste Special ›
/// Keep Source Formatting.
pub source_theme: Option<(deckcraft_color::ColorScheme, deckcraft_model::theme::FontScheme)>,
/// The theme the shapes were copied under, for Paste Special › Keep Source Formatting.
pub source_theme: Option<SourceTheme>,
/// How many times the shapes were pasted (each paste offsets further).
pub pastes: u32,
}
@@ -811,10 +824,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
}
@@ -832,7 +867,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};
+678
View File
@@ -214,6 +214,368 @@ fn clipboard_copy_paste_shapes_and_slides() {
assert!(s.doc().unwrap().doc.validate().is_empty());
}
/// 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 {
use deckcraft_model::resolve;
let st = s.doc().unwrap();
let slide = st.current_slide().unwrap();
let ctx = resolve::Ctx::for_slide(&st.doc, slide).unwrap();
let sh = st.shape(id).unwrap();
let t = sh.text.as_ref().unwrap();
let b = resolve::body(&ctx, sh);
let mut out = format!("body {:?} {:?} {:?}\n", b.anchor, b.inset_l, b.autofit);
for p in &t.paragraphs {
let pp = resolve::para(&ctx, sh, p);
out += &format!("para {:?} {:?} {:?} {:?}\n", pp.align, pp.margin_left, pp.indent, pp.bullet);
for r in &p.runs {
let rp = resolve::run(&ctx, sh, p, &r.props);
out += &format!(" run {:?} {:?} {:?} {:?}\n", rp.size, rp.font, rp.bold, rp.fill);
}
}
out
}
/// A placeholder pasted or duplicated on a slide becomes a regular shape, and keeps the look its
/// layout and master gave it: the title's size and font, the body's bullets and indents. Before,
/// the copy fell back to the master's other-text style (18 pt, no bullets).
#[test]
fn a_pasted_or_duplicated_placeholder_keeps_its_inherited_look() {
let mut s = session();
s.execute("slide.new", &json!({})).unwrap();
let phs: Vec<ShapeId> = s.doc().unwrap().current_slide().unwrap().shapes.iter().filter(|x| x.ph.is_some()).map(|x| x.id).collect();
assert!(phs.len() >= 2, "a title and a body placeholder");
for (k, id) in phs.iter().enumerate() {
s.execute("text.set", &json!({"id": id.0, "text": format!("Placeholder {k}\nSecond line")})).unwrap();
}
for id in phs {
let want = resolved_look(&s, id);
for cmd in ["paste", "duplicate"] {
s.execute("selection.select", &json!({"id": id.0})).unwrap();
if cmd == "paste" {
s.execute("edit.copy", &json!({})).unwrap();
s.execute("edit.paste", &json!({})).unwrap();
} else {
s.execute("edit.duplicate", &json!({})).unwrap();
}
let copy = s.doc().unwrap().selection.shapes[0];
assert_ne!(copy, id);
assert!(s.doc().unwrap().shape(copy).unwrap().ph.is_none(), "{cmd}: the copy is a regular shape");
assert_eq!(resolved_look(&s, copy), want, "{cmd} of placeholder {}", id.0);
}
}
assert!(s.doc().unwrap().doc.validate().is_empty());
}
/// Dragging a copy of a placeholder off it keeps the look too: the title's size and font, the
/// body's bullets. `edit.duplicate` is not the only way to duplicate.
#[test]
fn a_placeholder_duplicated_by_dragging_keeps_its_look() {
let mut s = session();
s.execute("slide.new", &json!({})).unwrap();
let phs: Vec<ShapeId> = s.doc().unwrap().current_slide().unwrap().shapes.iter().filter(|x| x.ph.is_some()).map(|x| x.id).collect();
assert!(phs.len() >= 2, "a title and a body placeholder");
for id in phs {
s.execute("text.set", &json!({"id": id.0, "text": "First\nSecond"})).unwrap();
let want = resolved_look(&s, id);
s.execute("selection.select", &json!({"id": id.0})).unwrap();
let st = s.doc().unwrap();
let x = crate::cmd::xfrm_of(&st.doc, &st.selection, st.shape(id).unwrap());
let (cx, cy) = (x.x + x.w / 2.0, x.y + x.h / 2.0);
let alt = |kind, x, y| PointerEvent { mods: Mods { alt: true, ..Default::default() }, ..tev(kind, x, y) };
s.pointer(alt(PointerKind::Down, cx, cy)).unwrap();
s.pointer(alt(PointerKind::Drag, cx + 30.0, cy + 30.0)).unwrap();
s.pointer(alt(PointerKind::Up, cx + 30.0, cy + 30.0)).unwrap();
let copy = s.doc().unwrap().selection.shapes[0];
assert_ne!(copy, id, "the drag made a copy");
assert!(s.doc().unwrap().shape(id).unwrap().ph.is_some(), "the original stays a placeholder");
assert!(s.doc().unwrap().shape(copy).unwrap().ph.is_none(), "the copy is a regular shape");
assert_eq!(resolved_look(&s, copy), want, "placeholder {}", id.0);
}
}
/// Keep Source Formatting fixes a copy's theme colours as the source drew them: through the colour
/// map of its slide or layout, not the master's. A title's inherited `tx1` stays the light colour
/// an override gave it.
#[test]
fn keep_source_formatting_uses_the_slides_colour_map() {
use deckcraft_model::resolve;
let first_run_colour = |s: &Session, id: ShapeId| {
let st = s.doc().unwrap();
let ctx = resolve::Ctx::for_slide(&st.doc, st.current_slide().unwrap()).unwrap();
let sh = st.shape(id).unwrap();
let p = &sh.text.as_ref().unwrap().paragraphs[0];
resolve::text_color(&ctx, &resolve::run(&ctx, sh, p, &p.runs[0].props))
};
for on_layout in [false, true] {
let mut s = session();
s.execute("slide.new", &json!({})).unwrap();
let title = s.doc().unwrap().current_slide().unwrap().shapes.iter().find(|x| x.ph.is_some()).unwrap().id;
s.execute("text.set", &json!({"id": title.0, "text": "Title"})).unwrap();
let plain = first_run_colour(&s, title);
let st = s.doc_mut().unwrap();
let at = st.selection.slide;
let doc = std::sync::Arc::make_mut(&mut st.doc);
if on_layout {
let layout = doc.slides[at].layout;
let m = std::sync::Arc::make_mut(&mut doc.masters[0]);
m.layouts.iter_mut().find(|l| l.id == layout).unwrap().color_map = swapped_colour_map();
} else {
std::sync::Arc::make_mut(&mut doc.slides[at]).color_map = swapped_colour_map();
}
let want = first_run_colour(&s, title);
assert_ne!(want, plain, "the override changes how the title draws");
s.execute("selection.select", &json!({"id": title.0})).unwrap();
s.execute("edit.copy", &json!({})).unwrap();
s.execute("edit.pasteSpecial", &json!({"as": "keepSource"})).unwrap();
let copy = s.doc().unwrap().selection.shapes[0];
assert_ne!(copy, title);
assert_eq!(first_run_colour(&s, copy), want, "override on the layout: {on_layout}");
}
}
/// A colour map that swaps text and background, as a dark slide or layout has.
fn swapped_colour_map() -> Option<Vec<(String, String)>> {
Some([("bg1", "dk1"), ("tx1", "lt1"), ("bg2", "dk2"), ("tx2", "lt2")].map(|(a, b)| (a.to_string(), b.to_string())).to_vec())
}
/// A new rectangle in the edited tree, filled with theme colour `slot`.
fn rect_filled_with(s: &mut Session, slot: deckcraft_color::SchemeSlot) -> ShapeId {
use deckcraft_model::{ColorRef, Fill};
let id = s.execute("shape.insert", &json!({"preset": "rect", "rect": [10, 10, 50, 50]})).unwrap()["id"].as_u64().unwrap();
let id = ShapeId(u32::try_from(id).unwrap());
s.edit(|doc, sel| {
let list = crate::shapes_mut(doc, sel).unwrap();
list.iter_mut().find(|x| x.id == id).unwrap().fill = Some(Fill::solid(ColorRef::scheme(slot)));
Ok(())
})
.unwrap();
id
}
/// How shape `id`'s fill draws in the edited tree.
fn drawn_fill(s: &Session, id: ShapeId) -> deckcraft_color::Rgba {
use deckcraft_model::{Fill, resolve};
let st = s.doc().unwrap();
let ctx = crate::cmd::text::ctx_for(&st.doc, &st.selection).unwrap();
match resolve::fill(&ctx, st.shape(id).unwrap()).0 {
Some(Fill::Solid { color }) => ctx.color(&color, None),
other => panic!("not a solid fill: {other:?}"),
}
}
/// Copy the selected shape and paste it with Keep Source Formatting; the copy's id.
fn keep_source_copy(s: &mut Session, id: ShapeId) -> ShapeId {
s.execute("selection.select", &json!({"id": id.0})).unwrap();
s.execute("edit.copy", &json!({})).unwrap();
s.execute("edit.pasteSpecial", &json!({"as": "keepSource"})).unwrap();
let copy = s.doc().unwrap().selection.shapes[0];
assert_ne!(copy, id);
copy
}
/// A colour map renames only `tx1`, `bg1`, `tx2` and `bg2`. Keep Source Formatting fixes every slot
/// as it draws: `tx1` follows the map, while a direct `dk1` stays the theme's dark colour.
#[test]
fn keep_source_formatting_maps_only_the_alias_colours() {
use deckcraft_color::SchemeSlot::{self, *};
let mut s = session();
let st = s.doc_mut().unwrap();
let at = st.selection.slide;
std::sync::Arc::make_mut(&mut std::sync::Arc::make_mut(&mut st.doc).slides[at]).color_map = swapped_colour_map();
let slots: [SchemeSlot; 9] = [Dk1, Lt1, Dk2, Lt2, Tx1, Bg1, Tx2, Bg2, Accent1];
let mut drawn = vec![];
for slot in slots {
let id = rect_filled_with(&mut s, slot);
let want = drawn_fill(&s, id);
let copy = keep_source_copy(&mut s, id);
assert_eq!(drawn_fill(&s, copy), want, "{slot:?}");
drawn.push(want);
}
assert_ne!(drawn[0], drawn[4], "the map sets `tx1` apart from `dk1`");
}
/// In Slide Master view a copy draws through the edited master's map, not an override on the slide
/// that happens to be current.
#[test]
fn keep_source_formatting_in_slide_master_view_ignores_the_slides_colour_map() {
use deckcraft_color::SchemeSlot::Tx1;
for master in [false, true] {
let mut s = session();
let st = s.doc_mut().unwrap();
let at = st.selection.slide;
std::sync::Arc::make_mut(&mut std::sync::Arc::make_mut(&mut st.doc).slides[at]).color_map = swapped_colour_map();
s.execute("view.slideMaster", &if master { json!({"master": 0}) } else { json!({}) }).unwrap();
let id = rect_filled_with(&mut s, Tx1);
let want = drawn_fill(&s, id);
let copy = keep_source_copy(&mut s, id);
assert_eq!(drawn_fill(&s, copy), want, "editing the master: {master}");
}
}
/// A slide's placeholder pasted in Slide Master view stays a placeholder that inherits from where
/// it lands; only a paste on a slide turns it into a regular shape with the look it had.
#[test]
fn a_slide_placeholder_pasted_in_slide_master_view_stays_a_placeholder() {
let mut s = session();
s.execute("slide.new", &json!({})).unwrap();
let title = s.doc().unwrap().current_slide().unwrap().shapes.iter().find(|x| x.ph.is_some()).unwrap().id;
s.execute("text.set", &json!({"id": title.0, "text": "Title"})).unwrap();
let want = resolved_look(&s, title);
let source = s.doc().unwrap().shape(title).unwrap().clone();
s.execute("selection.select", &json!({"id": title.0})).unwrap();
s.execute("edit.copy", &json!({})).unwrap();
s.execute("view.slideMaster", &json!({})).unwrap();
for paste in ["edit.paste", "edit.pasteSpecial"] {
s.execute(paste, &json!({"as": "keepSource"})).unwrap();
let st = s.doc().unwrap();
let copy = st.shape(st.selection.shapes[0]).unwrap();
assert_eq!(copy.ph.as_ref().map(|p| p.kind), source.ph.as_ref().map(|p| p.kind), "{paste} keeps the placeholder");
let list = copy.text.as_ref().map(|t| t.list_style.clone());
assert_eq!(list, source.text.as_ref().map(|t| t.list_style.clone()), "{paste} adds no baked styles");
}
s.execute("view.closeMaster", &json!({})).unwrap();
s.execute("edit.paste", &json!({})).unwrap();
let copy = s.doc().unwrap().selection.shapes[0];
assert!(s.doc().unwrap().shape(copy).unwrap().ph.is_none(), "on a slide the copy is a regular shape");
assert_eq!(resolved_look(&s, copy), want);
}
/// The resolved fill, line and effects of shape `id` on the current slide, with their style colours.
fn resolved_box(s: &Session, id: ShapeId) -> String {
use deckcraft_model::resolve;
let st = s.doc().unwrap();
let ctx = resolve::Ctx::for_slide(&st.doc, st.current_slide().unwrap()).unwrap();
let sh = st.shape(id).unwrap();
format!("{:?}\n{:?}\n{:?}", resolve::fill(&ctx, sh), resolve::line(&ctx, sh), resolve::effects(&ctx, sh))
}
/// Edit the title placeholders the current slide's title inherits from: its layout's and its
/// master's. Returns the slide title's id.
fn edit_title_parents(
s: &mut Session,
layout: impl FnOnce(&mut deckcraft_model::Shape),
master: impl FnOnce(&mut deckcraft_model::Shape),
) -> ShapeId {
let st = s.doc_mut().unwrap();
let slide = st.current_slide().unwrap().clone();
let title = slide.shapes.iter().find(|x| x.ph_type().is_some_and(|k| k.is_title())).unwrap().id;
let doc = std::sync::Arc::make_mut(&mut st.doc);
let m = std::sync::Arc::make_mut(&mut doc.masters[0]);
let lay = m.layouts.iter_mut().find(|l| l.id == slide.layout).unwrap();
layout(lay.shapes.iter_mut().find(|x| x.ph_type().is_some_and(|k| k.is_title())).unwrap());
master(m.shapes.iter_mut().find(|x| x.ph_type().is_some_and(|k| k.is_title())).unwrap());
title
}
/// A copied placeholder takes each of fill, line and effects from where the original does: a
/// style on the layout's placeholder (or on the slide's own) wins over an explicit value on the
/// master's, as `resolve::fill`, `resolve::line` and `resolve::effects` decide.
#[test]
fn a_pasted_placeholder_keeps_the_style_precedence_of_its_box() {
use deckcraft_color::SchemeSlot;
use deckcraft_model::{ColorRef, Effects, Fill, Line, ShapeStyle};
for style_on_slide in [false, true] {
let mut s = session();
s.execute("slide.new", &json!({})).unwrap();
let style = ShapeStyle {
line_ref: (1, ColorRef::scheme(SchemeSlot::Accent2)),
fill_ref: (1, ColorRef::scheme(SchemeSlot::Accent1)),
effect_ref: (0, ColorRef::scheme(SchemeSlot::Accent3)),
font_ref: ("minor".into(), None),
};
let layout_style = (!style_on_slide).then(|| style.clone());
let title = edit_title_parents(
&mut s,
|l| l.style = layout_style,
|m| {
m.fill = Some(Fill::solid(ColorRef::scheme(SchemeSlot::Accent4)));
m.line = Some(Line::solid(ColorRef::scheme(SchemeSlot::Accent5), 20.0));
m.effects = Some(Effects { soft_edge: Some(5.0), ..Default::default() });
},
);
if style_on_slide {
s.edit(|doc, sel| {
let list = crate::shapes_mut(doc, sel).unwrap();
list.iter_mut().find(|x| x.id == title).unwrap().style = Some(style.clone());
Ok(())
})
.unwrap();
}
s.execute("text.set", &json!({"id": title.0, "text": "Styled title"})).unwrap();
let want = (resolved_box(&s, title), resolved_look(&s, title));
s.execute("selection.select", &json!({"id": title.0})).unwrap();
s.execute("edit.copy", &json!({})).unwrap();
s.execute("edit.paste", &json!({})).unwrap();
let copy = s.doc().unwrap().selection.shapes[0];
assert_eq!((resolved_box(&s, copy), resolved_look(&s, copy)), want, "style on the slide: {style_on_slide}");
}
}
/// The master's placeholder style never reaches the copy's text: the original's text ignores it
/// (only the shape's or the layout's font reference counts), so the copy must too.
#[test]
fn a_pasted_placeholder_ignores_the_masters_font_reference() {
use deckcraft_color::SchemeSlot;
use deckcraft_model::{ColorRef, ShapeStyle};
let mut s = session();
s.execute("slide.new", &json!({})).unwrap();
let title = edit_title_parents(
&mut s,
|l| l.style = None,
|m| {
m.style = Some(ShapeStyle {
line_ref: (0, ColorRef::scheme(SchemeSlot::Accent2)),
fill_ref: (0, ColorRef::scheme(SchemeSlot::Accent1)),
effect_ref: (0, ColorRef::scheme(SchemeSlot::Accent3)),
font_ref: ("minor".into(), Some(ColorRef::scheme(SchemeSlot::Accent6))),
})
},
);
s.execute("text.set", &json!({"id": title.0, "text": "Title"})).unwrap();
let want = (resolved_box(&s, title), resolved_look(&s, title));
assert!(want.1.contains("+mj-lt"), "the title draws in the heading font: {}", want.1);
s.execute("selection.select", &json!({"id": title.0})).unwrap();
s.execute("edit.duplicate", &json!({})).unwrap();
let copy = s.doc().unwrap().selection.shapes[0];
assert_eq!((resolved_box(&s, copy), resolved_look(&s, copy)), want);
}
/// The same on every text placeholder of the sample deck (titles, bulleted bodies, subtitles).
#[test]
fn sample_placeholders_paste_with_their_look() {
let mut s = Session::new();
crate::sample::open_sample(&mut s).unwrap();
let slides = s.doc().unwrap().doc.slides.len();
let mut checked = 0;
for i in 0..slides {
s.execute("slide.go", &json!({"index": i})).unwrap();
let phs: Vec<ShapeId> = s
.doc()
.unwrap()
.current_slide()
.unwrap()
.shapes
.iter()
.filter(|x| x.ph.is_some() && x.text.as_ref().is_some_and(|t| !t.is_empty()))
.map(|x| x.id)
.collect();
for id in phs {
let want = resolved_look(&s, id);
s.execute("selection.select", &json!({"id": id.0})).unwrap();
s.execute("edit.copy", &json!({})).unwrap();
s.execute("edit.paste", &json!({})).unwrap();
let copy = s.doc().unwrap().selection.shapes[0];
assert_eq!(resolved_look(&s, copy), want, "slide {i}, placeholder {}", id.0);
checked += 1;
}
}
assert!(checked >= 3, "the sample has text placeholders ({checked})");
}
#[test]
fn pasted_shapes_get_the_next_automatic_name() {
let mut s = session();
@@ -1351,6 +1713,31 @@ fn tab_moves_between_cells_skipping_merged_ones() {
assert_eq!((cell(&s), table_of(&s, id).n_rows()), (Some((2, 0)), 3), "Tab in the last cell adds a row");
}
#[test]
fn format_paragraph_align_names_match_format_align() {
use deckcraft_model::text::Align;
let mut s = session();
let title = s.doc().unwrap().current_slide().unwrap().shapes[0].id.0;
s.execute("text.edit", &json!({"id": title})).unwrap();
let para = |s: &Session| s.doc().unwrap().current_slide().unwrap().shapes[0].text.as_ref().unwrap().paragraphs[0].props.align;
for (name, want) in [
("justLow", Align::JustLow),
("justifyLow", Align::JustLow),
("thaiDist", Align::ThaiDistributed),
("ctr", Align::Center),
("centre", Align::Center),
("r", Align::Right),
("just", Align::Justify),
("dist", Align::Distributed),
("distributed", Align::Distributed),
("left", Align::Left),
] {
s.execute("format.paragraph", &json!({"align": name})).unwrap();
assert_eq!(para(&s), Some(want), "{name}");
}
assert!(s.execute("format.paragraph", &json!({"align": "bogus"})).is_err());
}
#[test]
fn right_to_left_paragraphs() {
let mut s = session();
@@ -3358,6 +3745,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] = &[
@@ -4321,6 +4727,9 @@ fn shape_fill_documents_each_gradient_key_and_kind() {
let params = cmd::parse_params(find_command("shape.fill").unwrap().params).unwrap();
let about = params.iter().find(|p| p.name == "gradient").unwrap().about;
assert_eq!(cmd::param_keys(about).unwrap(), ["stops", "kind", "angle", "focus", "rotateWithShape"]);
// The renderer keeps only linear and radial gradients upright (`paint::fill_path`).
let rotate = cmd::parse_params(about).unwrap().into_iter().find(|p| p.name == "rotateWithShape").unwrap().about;
assert!(rotate.contains("rectangular and path gradients turn with the shape"), "{rotate}");
for kind in ["linear", "radial", "rectangular", "path"] {
assert!(about.contains(kind), "{kind}: {about}");
let g = cmd::shape::gradient_param(&json!({
@@ -5043,6 +5452,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 {
@@ -6480,6 +6921,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();
@@ -6506,3 +7011,176 @@ fn save_to_image_path_is_an_error_not_zip_bytes() {
}
let _ = std::fs::remove_dir_all(&dir);
}
/// `shape.connect` glues to a shape inside a group, as PowerPoint does: the connector's
/// end is set to the grouped shape and follows it when the group moves. The glue was dropped at
/// once (the end stayed `None`) because rerouting only looked at the slide's top-level shapes,
/// and the sites of a grouped shape were computed in the group's child space.
#[test]
fn shape_connect_glues_to_a_shape_inside_a_group() {
use deckcraft_model::{ShapeId, ShapeKind};
let mut s = session();
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
let a = s.execute("shape.insert", &json!({"preset": "rect", "rect": [10, 10, 100, 50]})).unwrap()["id"].as_u64().unwrap();
let b = s.execute("shape.insert", &json!({"preset": "rect", "rect": [10, 200, 100, 50]})).unwrap()["id"].as_u64().unwrap();
let g = s.execute("arrange.group", &json!({"ids": [a, b]})).unwrap()["id"].as_u64().unwrap();
let c = s.execute("shape.insert", &json!({"preset": "rect", "rect": [400, 10, 100, 50]})).unwrap()["id"].as_u64().unwrap();
let glue = |s: &Session, id: u64| {
let st = s.doc().unwrap();
match st.shape(ShapeId(id as u32)).unwrap().kind {
ShapeKind::Connector { start, end } => (start.map(|(i, _)| i.0 as u64), end.map(|(i, _)| i.0 as u64)),
_ => (None, None),
}
};
let near = |p: deckcraft_geom::Point, x: f64, y: f64| (p.x - x).abs() < 1e-6 && (p.y - y).abs() < 1e-6;
// A new connector into the group.
let r = s.execute("shape.connect", &json!({"from": c, "to": a})).unwrap();
let k = r["id"].as_u64().unwrap();
assert_eq!(glue(&s, k), (Some(c), Some(a)), "{r}");
let (p0, p1) = line_ends(&s, k);
assert!(near(p0, 400.0, 35.0), "from C's left site: {p0:?}");
assert!(near(p1, 110.0, 35.0), "to A's right site in slide space: {p1:?}");
s.execute("shape.move", &json!({"ids": [g], "dy": 100})).unwrap();
assert_eq!(glue(&s, k), (Some(c), Some(a)), "the glue survives the move");
let (_, p1) = line_ends(&s, k);
assert!(near(p1, 110.0, 135.0), "the connector follows the grouped shape: {p1:?}");
// Re-gluing an existing line with `id`.
let l = s.execute("shape.insert", &json!({"preset": "straightConnector1", "rect": [600, 400, 50, 50]})).unwrap()["id"].as_u64().unwrap();
let r = s.execute("shape.connect", &json!({"id": l, "from": c, "to": b})).unwrap();
assert_eq!(r["id"], l, "{r}");
assert_eq!(glue(&s, l), (Some(c), Some(b)), "{r}");
let (_, p1) = line_ends(&s, l);
assert!(near(p1, 110.0, 325.0), "to B's right site (C is mostly to its right) in slide space, B moved with the group: {p1:?}");
s.execute("shape.move", &json!({"ids": [g], "dx": 50})).unwrap();
assert_eq!(glue(&s, l), (Some(c), Some(b)));
let (_, p1) = line_ends(&s, l);
assert!(near(p1, 160.0, 325.0), "{p1:?}");
// shape.sites answers in slide space, as shape.connect uses them: A's right site, A moved
// with the group.
let v = s.execute("shape.sites", &json!({"id": a})).unwrap();
assert!(
v.as_array().unwrap().iter().any(|q| near(deckcraft_geom::Point::new(q[0].as_f64().unwrap(), q[1].as_f64().unwrap()), 160.0, 135.0)),
"{v}"
);
// A line inside a group is refused as `id`: its ends would be written in slide space into
// the group's space, and rerouting does not follow grouped connectors.
let m = s.execute("shape.insert", &json!({"preset": "line", "rect": [600, 300, 50, 0]})).unwrap()["id"].as_u64().unwrap();
let n = s.execute("shape.insert", &json!({"preset": "rect", "rect": [600, 450, 50, 50]})).unwrap()["id"].as_u64().unwrap();
s.execute("arrange.group", &json!({"ids": [m, n]})).unwrap();
let before = s.doc().unwrap().doc.clone();
let e = s.execute("shape.connect", &json!({"id": m, "from": c, "to": b})).unwrap_err().to_string();
assert!(e.contains("inside a group"), "{e}");
assert_eq!(s.doc().unwrap().doc, before, "nothing changed");
// A grouped shape that is not a line is refused as not a line, not as a grouped line.
let e = s.execute("shape.connect", &json!({"id": n, "from": c, "to": b})).unwrap_err().to_string();
assert!(e.contains("not a line"), "{e}");
assert_eq!(s.doc().unwrap().doc, before, "nothing changed");
}
#[test]
fn flip_is_refused_on_a_smartart_graphic_and_leaves_it_a_graphic() {
let mut s = session();
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
s.execute("insert.textBox", &json!({"text": "Goals\nRevenue"})).unwrap();
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}");
}
// 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}");
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();
// Ordinary shapes still flip.
let b = s.execute("insert.textBox", &json!({"text": "x"})).unwrap();
let id = b["id"].as_u64().unwrap();
assert_eq!(s.execute("arrange.flipHorizontal", &json!({"ids": [id]})).unwrap(), json!(1));
}
/// Table cell commands refuse a `cells` that is not a list of [row, col] cells in the table,
/// instead of dropping the entry or reading the list as absent (which acts on the selection).
#[test]
fn table_cell_commands_refuse_a_malformed_cells_list() {
let mut s = session();
let t = table_at(&mut s, &[100.0, 100.0], json!([["a", "b"], ["c", "d"]]));
let rev = s.doc().unwrap().revision;
for cells in [
json!([[-1, 0]]),
json!([[0]]),
json!([[0, 0], "x"]),
json!([[0, 0, "x"]]),
json!([[0, 2]]),
json!([[0, u64::MAX]]),
json!("0,0"),
json!({"0": 0}),
] {
for (cmd, extra) in [
("table.cellFill", json!({"color": "#FF0000"})),
("table.cellMargins", json!({"left": 4})),
("table.cellAlign", json!({"align": "center"})),
("table.borders", json!({"which": "right"})),
] {
let mut p = extra.clone();
p["id"] = json!(t);
p["cells"] = cells.clone();
let e = s.execute(cmd, &p).unwrap_err().to_string();
assert!(e.contains(cmd) && e.contains("cell"), "{cmd} {cells}: {e}");
}
}
assert_eq!(s.doc().unwrap().revision, rev, "nothing changed");
let many = json!(vec![[0, 0]; 100_001]);
assert!(s.execute("table.cellFill", &json!({"id": t, "color": "#FF0000", "cells": many})).unwrap_err().to_string().contains("at most"));
s.execute("table.cellFill", &json!({"id": t, "color": "#FF0000", "cells": [[1, 1]]})).unwrap();
assert!(table_of(&s, t).cell(1, 1).unwrap().fill.is_some() && table_of(&s, t).cell(0, 0).unwrap().fill.is_none());
}
/// The outline exports (PowerPoint's outline view) holds titles and body text only: footer,
/// date and slide-number placeholders are not body lines.
#[test]
fn outline_export_skips_footer_date_and_slide_number() {
let mut s = session();
s.execute("slide.new", &json!({"layout": "titleAndContent", "title": "Agenda", "body": "First point\nSecond point"})).unwrap();
s.execute(
"design.headerFooter",
&json!({"footer": true, "footerText": "Company Confidential", "slideNumber": true, "date": true, "dateText": "1 May 2026"}),
)
.unwrap();
let dir = std::env::temp_dir().join(format!("deckcraft-outline-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("deck.md");
s.execute("file.export", &json!({"path": path.to_string_lossy(), "format": "outline"})).unwrap();
let text = std::fs::read_to_string(&path).unwrap();
let _ = std::fs::remove_dir_all(&dir);
assert!(text.contains("Agenda") && text.contains("First point") && text.contains("Second point"), "{text:?}");
assert!(!text.contains("Company Confidential"), "footer text is not a body line: {text:?}");
assert!(!text.contains("‹#›"), "the slide-number field is not a body line: {text:?}");
assert!(!text.contains("1 May 2026"), "the date is not a body line: {text:?}");
// The RTF outline (Save As ▸ Outline/RTF) too.
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("deck.rtf");
s.execute("file.export", &json!({"path": path.to_string_lossy(), "format": "rtf"})).unwrap();
let rtf = std::fs::read_to_string(&path).unwrap();
let _ = std::fs::remove_dir_all(&dir);
assert!(rtf.contains("Agenda") && rtf.contains("First point"), "{rtf:?}");
assert!(!rtf.contains("Company Confidential") && !rtf.contains("1 May 2026"), "{rtf:?}");
}
#[test]
fn shape_adjust_and_quick_style_reject_invalid_index() {
let mut s = session();
let r = s.execute("shape.insert", &json!({"preset": "roundRect", "rect": [100, 100, 200, 100]})).unwrap();
let id = deckcraft_model::ShapeId(r["id"].as_u64().unwrap() as u32);
let before = s.doc().unwrap().shape(id).unwrap().clone();
for bad in [json!(-1), json!(1.5)] {
assert!(s.execute("shape.adjust", &json!({"index": bad, "value": 0.4})).is_err(), "adjust accepted {bad}");
assert!(s.execute("shape.quickStyle", &json!({"index": bad})).is_err(), "quickStyle accepted {bad}");
}
assert_eq!(s.doc().unwrap().shape(id).unwrap().geom, before.geom);
assert_eq!(s.doc().unwrap().shape(id).unwrap().style, before.style);
s.execute("shape.adjust", &json!({"value": 0.4})).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 });
+28 -7
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;
@@ -236,7 +252,12 @@ impl Session {
}
}
// A connector dragged away from shapes that stay put comes unglued at those ends.
let moved: Vec<ShapeId> = origs2.iter().map(|o| o.0).collect();
let mut moved: Vec<ShapeId> = vec![];
for (id, _) in &origs2 {
if let Some(sh) = deckcraft_model::find_shape(list, *id) {
deckcraft_model::walk(std::slice::from_ref(sh), &mut |s, _| moved.push(s.id));
}
}
for sh in list.iter_mut().filter(|s| moved.contains(&s.id)) {
if let ShapeKind::Connector { start, end } = &mut sh.kind {
for c in [start, end] {
+1
View File
@@ -101,6 +101,7 @@ impl Session {
let list = cmd::current_shapes(doc, sel).cloned().unwrap_or_default();
let mut copies: Vec<Shape> = list.iter().filter(|x| ids.contains(&x.id)).cloned().collect();
let boxes: Vec<Xfrm> = copies.iter().map(|c| xfrm_of(doc, sel, c)).collect();
cmd::edit::detach_on_slide(doc, sel, &mut copies);
cmd::edit::reid("edit.duplicate", doc, &mut copies)?;
for (c, b) in copies.iter_mut().zip(boxes) {
c.xfrm = Some(b);
+4 -1
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);
+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");
}
}
+21 -5
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`]).
@@ -1260,9 +1271,8 @@ impl FontDb {
fn instance(&self, family: &str, style: &str) -> Option<Arc<FontFace>> {
let open = style.find('{')?;
let base = self.find(family, style[..open].trim())?;
if base.skrifa().is_none_or(|f| f.axes().is_empty()) {
return Some(base);
}
let Some(font) = base.skrifa().filter(|f| !f.axes().is_empty()) else { return Some(base) };
let axis_tags: Vec<[u8; 4]> = font.axes().iter().map(|a| a.tag().to_be_bytes()).collect();
let mut coords = base.coords.clone();
for kv in style[open + 1..].trim_end_matches('}').split(',') {
let (k, v) = kv.split_once(':')?;
@@ -1272,6 +1282,11 @@ impl FontDb {
return None;
}
let tag = [k[0], k[1], k[2], k[3]];
// A tag the font has no axis for changes nothing; keeping it would grow the list with
// every distinct tag in the style text.
if !axis_tags.contains(&tag) {
continue;
}
match coords.iter_mut().find(|(t, _)| *t == tag) {
Some(c) => c.1 = v,
None => coords.push((tag, v)),
@@ -1283,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();
@@ -1469,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());
}
+8 -1
View File
@@ -247,6 +247,11 @@ pub fn repair(p: &mut Presentation) {
if !(p.notes_size.width.is_finite() && p.notes_size.width > 0.0 && p.notes_size.height.is_finite() && p.notes_size.height > 0.0) {
p.notes_size = Presentation::default().notes_size;
}
// A notes page is bounded like a slide: canvas and handout drawing scale with it.
p.notes_size = deckcraft_model::Size::new(
p.notes_size.width.clamp(*SLIDE_SIDE.start(), *SLIDE_SIDE.end()),
p.notes_size.height.clamp(*SLIDE_SIDE.start(), *SLIDE_SIDE.end()),
);
sane_numbers(p);
}
@@ -689,7 +694,9 @@ pub fn slides_to_outline(p: &Presentation) -> String {
out.push_str(&s.title().replace('\n', " "));
out.push('\n');
for sh in &s.shapes {
if sh.ph_type().is_some_and(|k| k.is_title()) {
// The title is the slide's line; footer, date and slide-number placeholders are not
// body text (PowerPoint's outline view shows title and body placeholders only).
if sh.ph_type().is_some_and(|k| k.is_title() || k.is_footer_kind()) {
continue;
}
if let Some(t) = &sh.text {
+10 -2
View File
@@ -71,7 +71,8 @@ pub fn slides_to_rtf(p: &Presentation) -> String {
}
out.push_str("\\b0\\par\n");
for sh in &s.shapes {
if sh.ph_type().is_some_and(|k| k.is_title()) {
// As in the text outline: footer, date and slide-number placeholders are not body text.
if sh.ph_type().is_some_and(|k| k.is_title() || k.is_footer_kind()) {
continue;
}
let Some(t) = &sh.text else { continue };
@@ -299,7 +300,8 @@ impl Outline<'_> {
/// A one-character control symbol (`\\`, `\{`, `\~`, ...).
fn symbol(&mut self, n: u8) {
self.i += 1;
let c = self.rtf.get(self.i..).and_then(|r| r.chars().next());
self.i = advance(self.rtf, self.i, 1);
if self.st.skip {
return;
}
@@ -308,6 +310,7 @@ impl Outline<'_> {
b'~' => self.line.push('\u{a0}'),
b'_' => self.line.push('\u{2011}'),
b'\n' | b'\r' => self.flush(),
_ if !n.is_ascii() => self.line.extend(c),
_ => {}
}
}
@@ -419,6 +422,11 @@ mod tests {
assert_eq!(rtf_to_outline(r"{\rtf1 x\u-70000?y\par}"), "x\u{ee90}y\n");
}
#[test]
fn multibyte_control_symbol_keeps_character_and_following_text() {
assert_eq!(rtf_to_outline("{\\rtf1 A\\éB\\par}"), "AéB\n");
}
/// `\\_` is RTF's non-breaking hyphen: a visible character, not markup.
#[test]
fn non_breaking_hyphen_is_kept() {
+7
View File
@@ -451,6 +451,13 @@ fn degenerate_slide_sizes_are_repaired() {
}
}
#[test]
fn repair_bounds_the_notes_page_size() {
let mut p = Presentation { notes_size: deckcraft_model::Size::new(1e12, 0.01), ..Default::default() };
repair(&mut p);
assert_eq!((p.notes_size.width, p.notes_size.height), (4032.0, 72.0));
}
/// A file whose slide size is null, a string or missing a side (written by hand or by
/// another tool; NaN is also saved as null) opens with the default size rather than failing
/// as damaged.
-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

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