avbridge 2.12.0 → 2.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/CHANGELOG.md +177 -0
  2. package/README.md +33 -0
  3. package/dist/{avi-EQE6AR75.cjs → avi-32UABODO.cjs} +12 -4
  4. package/dist/avi-32UABODO.cjs.map +1 -0
  5. package/dist/{avi-Y3N325WZ.cjs → avi-5BPR6QUX.cjs} +12 -4
  6. package/dist/avi-5BPR6QUX.cjs.map +1 -0
  7. package/dist/{avi-NNHH4AAA.js → avi-BLIH7KKV.js} +12 -4
  8. package/dist/avi-BLIH7KKV.js.map +1 -0
  9. package/dist/{avi-S7EY54YA.js → avi-GX2H34IQ.js} +12 -4
  10. package/dist/avi-GX2H34IQ.js.map +1 -0
  11. package/dist/{chunk-2LNXMGT6.js → chunk-5CX7BVVV.js} +5 -5
  12. package/dist/{chunk-2LNXMGT6.js.map → chunk-5CX7BVVV.js.map} +1 -1
  13. package/dist/{chunk-5Y5BTB5D.js → chunk-B76QWPFM.js} +3 -3
  14. package/dist/{chunk-5Y5BTB5D.js.map → chunk-B76QWPFM.js.map} +1 -1
  15. package/dist/{chunk-GJBNLPGI.cjs → chunk-E5MAM2P4.cjs} +9 -9
  16. package/dist/{chunk-GJBNLPGI.cjs.map → chunk-E5MAM2P4.cjs.map} +1 -1
  17. package/dist/{chunk-7EF4VTUS.cjs → chunk-OFJYEITB.cjs} +489 -113
  18. package/dist/chunk-OFJYEITB.cjs.map +1 -0
  19. package/dist/{chunk-HBHSUGNI.cjs → chunk-VLI3Y6IJ.cjs} +5 -5
  20. package/dist/{chunk-HBHSUGNI.cjs.map → chunk-VLI3Y6IJ.cjs.map} +1 -1
  21. package/dist/{chunk-Z26PXRUY.js → chunk-VOC24LYF.js} +486 -110
  22. package/dist/chunk-VOC24LYF.js.map +1 -0
  23. package/dist/element-browser.js +492 -130
  24. package/dist/element-browser.js.map +1 -1
  25. package/dist/element.cjs +3 -3
  26. package/dist/element.js +2 -2
  27. package/dist/index.cjs +18 -18
  28. package/dist/index.js +6 -6
  29. package/dist/player.cjs +658 -170
  30. package/dist/player.cjs.map +1 -1
  31. package/dist/player.d.cts +36 -4
  32. package/dist/player.d.ts +36 -4
  33. package/dist/player.js +658 -170
  34. package/dist/player.js.map +1 -1
  35. package/dist/{remux-VPKCLHHM.cjs → remux-NSBJFMLG.cjs} +9 -9
  36. package/dist/{remux-VPKCLHHM.cjs.map → remux-NSBJFMLG.cjs.map} +1 -1
  37. package/dist/{remux-7TA4FKTY.js → remux-PHUHO3VV.js} +4 -4
  38. package/dist/{remux-7TA4FKTY.js.map → remux-PHUHO3VV.js.map} +1 -1
  39. package/package.json +1 -1
  40. package/src/element/avbridge-player.ts +223 -43
  41. package/src/probe/avi.ts +34 -2
  42. package/src/strategies/fallback/audio-output.ts +164 -35
  43. package/src/strategies/fallback/decoder.ts +467 -60
  44. package/src/strategies/fallback/video-renderer.ts +209 -29
  45. package/src/strategies/hybrid/decoder.ts +56 -28
  46. package/src/strategies/remux/pipeline.ts +12 -3
  47. package/vendor/libav/avbridge/libav-6.8.8.0-avbridge.wasm.mjs +1 -1
  48. package/vendor/libav/avbridge/libav-6.8.8.0-avbridge.wasm.wasm +0 -0
  49. package/vendor/libav/avbridge/libav-avbridge.mjs +1 -1
  50. package/dist/avi-EQE6AR75.cjs.map +0 -1
  51. package/dist/avi-NNHH4AAA.js.map +0 -1
  52. package/dist/avi-S7EY54YA.js.map +0 -1
  53. package/dist/avi-Y3N325WZ.cjs.map +0 -1
  54. package/dist/chunk-7EF4VTUS.cjs.map +0 -1
  55. package/dist/chunk-Z26PXRUY.js.map +0 -1
package/CHANGELOG.md CHANGED
@@ -4,6 +4,183 @@ All notable changes to **avbridge.js** are documented here. The format follows
4
4
  [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and this project
5
5
  adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [2.13.0]
8
+
9
+ Post-seek fast-forward fix on the fallback path, plus seekbar UX
10
+ improvements and a small new API for hosts embedding the player in
11
+ swipe-driven UIs.
12
+
13
+ ### Fixed
14
+
15
+ - **Post-seek "fast-forward" on MPEG-4 ASP + MP3 AVIs** (the headline
16
+ fix). After seeking deep into a long AVI, ~1–2 s of visibly
17
+ fast-forwarded video preceded normal playback. Root cause: the
18
+ fallback decoder's synthetic-PTS counter was reset to the user's
19
+ requested seek time, so the first NOPTS frame post-seek (typically
20
+ the keyframe libav landed on, ~4–8 s before target) got stamped
21
+ with the seek target's PTS. Every libav-pts frame after that was
22
+ REGRESSED-DROP'd because the synthetic anchor was ahead of real
23
+ content. The renderer painted the ~1-in-3 mis-labeled frames at
24
+ audio rate, producing 2.4–3× content rate on screen until the
25
+ synthetic counter caught up. Rewrote the content clock as
26
+ **sync-on-every-valid-pts / step-on-NOPTS** with no fallback to
27
+ seekTarget — labels now match real content within one frame at all
28
+ times. See `docs/dev/POSTMORTEMS.md` (2026-06-01) for the full
29
+ diagnostic arc, including the two epistemic traps that delayed
30
+ triage by hours.
31
+ - **Cold-start opening loses 1–2 frames.** First emitted keyframe at
32
+ `t=0` was being discarded as pre-anchor NOPTS, so the first paint
33
+ landed at content ~80 ms instead of 0. Special-cased
34
+ `seekTarget === 0`: anchor `lastContentUs = 0` on the
35
+ `f.key_frame === 1` frame directly. Strictly branched so it can't
36
+ touch the seek path.
37
+ - **Seekbar pointer events bubble to host gesture recognizers.**
38
+ Touches that start on the player chrome (seek bar, buttons,
39
+ settings menu, overlay play button) now stop propagation on
40
+ `pointerdown` / `pointermove` / `pointerup` / `pointercancel`, so
41
+ a host page that wraps the player in a TikTok-style vertical
42
+ pager won't latch its swipe handler on seek interactions.
43
+ - **Hybrid strategy audio uses packet PTS** instead of the synthetic
44
+ counter, matching the fallback strategy's audio path (see v2.12.2
45
+ refactor). `decodeAudioBatch` now captures `packetPtsSec` before
46
+ decode and routes per-frame; eliminates a class of post-seek
47
+ audio-scheduling drift that affected hybrid playback the same way
48
+ it affected fallback.
49
+ - **Remux pipeline drops stale writes after seek.** Each output's
50
+ `write` callback now captures the pump token at creation time and
51
+ drops any in-flight chunk whose token has been bumped by a
52
+ subsequent seek. Without this, fragments from the pre-seek output
53
+ could append to the SourceBuffer at their original timestamps,
54
+ the deferred seek would apply against the wrong buffered range,
55
+ and playback would snap to the end of the stale range.
56
+
57
+ ### Added
58
+
59
+ - **`AvbridgePlayerElement.isPlayerChromeEvent(event)`** static
60
+ helper. Returns `true` if a DOM event originated from the player's
61
+ interactive chrome (works across the shadow boundary via
62
+ `composedPath()`). Use this in **capture-phase** gesture
63
+ recognizers — capture-phase listeners run before the player's own
64
+ handlers can stop propagation, so they need to check the path
65
+ themselves. See README → `<avbridge-player>` → "Embedding inside a
66
+ swipe gesture".
67
+
68
+ ### Changed
69
+
70
+ - **Seekbar drag model** switches on `(pointer: coarse)` instead of
71
+ bar width. Touch devices (coarse) now get **relative-drag
72
+ scrubbing** — initial tap doesn't move the thumb, finger Δx maps to
73
+ Δt added to the time at pointerdown. Mouse/trackpad (fine) keep
74
+ **absolute-position** seeking — thumb jumps under the cursor on
75
+ tap, follows pointer on drag. Both modes commit live during drag
76
+ (throttled to ~4 Hz so the decoder pump isn't overwhelmed) and
77
+ once more on pointerup. Removes the 400 px `SCRUB_WIDTH_THRESHOLD`
78
+ — width was the wrong signal for distinguishing
79
+ precise-pointer-input vs imprecise-finger-input.
80
+ - **Diagnostic logging gated behind `globalThis.AVBRIDGE_DEBUG`.**
81
+ Per-packet (`[DIAG-PKT]`), per-frame (`[DIAG-FRAME]`), per-paint
82
+ (`[TRACE] PAINT`), and per-audio-chunk (`[TRACE-AUD]`) trace lines
83
+ are silent in normal use. Degraded-state warnings
84
+ (`first valid raw pts ≥ seek target`, `LEGACY (no PTS)`,
85
+ `REBASE anchor`) stay unconditional — those signal real problems.
86
+ - **Pre-roll re-enabled** with a structural safety guarantee. The
87
+ renderer paints at most ONE frame (the head of the queue, held
88
+ static) during the post-flush gate-wait window. Because the
89
+ decoder fix discards pre-target frames before they reach the
90
+ renderer queue, the pre-roll frame is now guaranteed to be a
91
+ near-target frame — the previous regression artifact (painting the
92
+ keyframe-to-target preroll stream) is structurally impossible.
93
+
94
+ ### Internal
95
+
96
+ - Replaced the fallback decoder's `sanitizeFrameTimestamp` call-site
97
+ with an inline anchor-and-step block that never lies about content
98
+ position. Synthetic counter no longer initialized to anything
99
+ derived from the user's click — unanchored on seek, established at
100
+ the first valid libav pts, extended by `frameStep` on NOPTS,
101
+ re-synced to truth on every valid pts.
102
+ - Removed dead debug scaffolding from `video-renderer.ts`:
103
+ `BREAK_AT_PAINT_AFTER_FLUSH` constant, `paintsSinceFlush` counter,
104
+ unused `paintHistory` ring-buffer field, the `debugger` statement
105
+ that would trap users on paint #10 post-seek when
106
+ `AVBRIDGE_DEBUG=true`.
107
+
108
+ ## [2.12.1]
109
+
110
+ DivX/Xvid AVI playback reliability — four latent bugs fixed. Content
111
+ that had been stuttering and freezing for months now plays smoothly
112
+ and seeks cleanly.
113
+
114
+ ### Added
115
+
116
+ - **Stats-for-Nerds overhaul** with live deltas: per-second decode
117
+ fps (% of realtime), paint fps, drops/sec, decode-ms breakdown
118
+ (avg/batch, % of wall, slowest), producer throttle %, queue span
119
+ + head/tail PTS, newest-decoded PTS, and explicit `PTS REGRESSIONS`
120
+ / `BSF MISSING` warning lines. Designed to answer "why is playback
121
+ stuttering" at a glance.
122
+ - **Decoder throughput instrumentation** in the fallback pipeline:
123
+ `videoDecodeMsTotal`, `videoDecodeBatches`, `audioDecodeMsTotal`,
124
+ `audioDecodeBatches`, `readMsTotal`, `pumpThrottleMsTotal`,
125
+ `slowestVideoBatchMs`, `newestVideoPtsMs`, `ptsRegressions`,
126
+ `worstPtsRegressionMs` all exposed in `getDiagnostics().runtime`.
127
+ - **`avbsf` fragment** in `scripts/build-libav.sh` + libav variant
128
+ rebuild. The BSF C code (`bsf-mpeg4_unpack_bframes`) was shipped
129
+ since 2.2.0 but the JS wrappers it needs
130
+ (`av_bsf_list_parse_str_js`, `av_bsf_init`, `av_bsf_send_packet`,
131
+ `av_bsf_receive_packet`) were never exported — the fixup was dead
132
+ at runtime. The BSF now actually runs, and its absence (if ever
133
+ rebuilt without `avbsf`) surfaces as `bsfMissing` in diagnostics
134
+ and a loud `console.error`.
135
+
136
+ ### Fixed
137
+
138
+ - **DivX/Xvid AVI stuttering** — large, clearly visible frame drops
139
+ (42 % of frames on a typical 25 fps 624×352 episode). Three
140
+ independent bugs stacked; all fixed now:
141
+ 1. *Synthetic PTS counter ignored valid neighbors.* When libav
142
+ emitted a frame with `AV_NOPTS_VALUE`, the fallback callback
143
+ assigned it a timestamp from a counter that started at 0 and
144
+ only advanced on invalid frames. At minute 5 of playback, one
145
+ bad frame would be tagged as PTS ≈ 0, jumping the renderer
146
+ wildly backwards. Now anchored to the last emitted frame's
147
+ PTS + one frame step, so invalid frames interpolate cleanly.
148
+ 2. *`mpeg4_unpack_bframes` BSF was compiled but unreachable from
149
+ JS* (see `avbsf` fragment addition above).
150
+ 3. *FPS probe gap for AVI.* `avg_frame_rate_num/_den` don't exist
151
+ as properties on libav.js stream records; they must be read
152
+ via `AVCodecParameters_framerate_num/_den`. Every AVI was
153
+ falling back to a 30 fps default, narrowing the renderer's
154
+ tolerance window and pushing 25 fps content off-cadence. Now
155
+ reads the real rate.
156
+ - **Seek freeze in the fallback strategy** — video stayed on the
157
+ pre-seek frame while audio continued from the new position. Root
158
+ cause: `hasFrames()` checked `framesPainted > 0`, which is
159
+ cumulative, so `waitForBuffer()` returned immediately after flush
160
+ even with an empty queue; audio started before any post-seek
161
+ frame had been decoded. Fixed with a `hasEverEnqueuedSinceFlush`
162
+ flag that resets on every `flush()`.
163
+ - **`DataCloneError` after seek** — the post-seek pump hammered the
164
+ console with `An ArrayBuffer is detached and could not be cloned`
165
+ on every decode batch. `flushBSF()` was sending a NULL packet as
166
+ its flush signal, but NULL is the EOF signal — it locked the BSF
167
+ into EOF state, so every subsequent `av_bsf_send_packet` failed
168
+ and `applyBSF` fell through to pushing the original input packet,
169
+ whose buffer had already been transferred to WASM by
170
+ `ff_copyin_packet`. Now uses `av_bsf_flush` (the actual flush
171
+ API) and defensively drops rejected packets instead of passing
172
+ their detached buffers through to the decoder.
173
+ - **Post-seek out-of-order frames from the mpeg4 decoder** —
174
+ `avcodec_flush_buffers` doesn't always clear the B-frame reorder
175
+ tail on mpeg4, so the first post-seek batch could contain frames
176
+ from before the seek mixed with frames from after, in arbitrary
177
+ order. The renderer's paint loop assumes monotonic queue and
178
+ breaks (head stuck, newer frames age out to late-drop) when that
179
+ invariant fails. The decoder now drops any frame whose PTS is
180
+ less than the previously emitted frame, with a console warning.
181
+ Counter resets at every seek so a legitimate large jump in PTS
182
+ after seek is always accepted.
183
+
7
184
  ## [2.12.0]
8
185
 
9
186
  Network playback performance + seek-bar polish.
package/README.md CHANGED
@@ -431,6 +431,39 @@ attribute mirroring the controls auto-hide state — useful if slotted
431
431
  buttons need to drive JS behavior (focus, announcements) in sync with
432
432
  the fade, not just CSS opacity.
433
433
 
434
+ #### Embedding inside a swipe gesture (TikTok-style pagers, etc.)
435
+
436
+ When `<avbridge-player>` is nested inside a host UI that recognizes
437
+ swipe gestures (vertical pager, drawer, carousel), pointer events
438
+ that start on the player chrome — seek bar, buttons, settings menu,
439
+ overlay play button — should NOT also latch the host's gesture
440
+ recognizer. The player handles this in two layers:
441
+
442
+ **Bubble-phase listeners** (the default) need no action on your
443
+ side. The player calls `stopPropagation()` on `pointerdown`,
444
+ `pointermove`, `pointerup`, and `pointercancel` for chrome
445
+ interactions, so they never bubble out to the host.
446
+
447
+ **Capture-phase listeners** (`{ capture: true }`) run *before* the
448
+ player's handlers, so `stopPropagation()` can't help. Check the
449
+ event against the static helper instead:
450
+
451
+ ```ts
452
+ import { AvbridgePlayerElement } from "avbridge/player-element";
453
+
454
+ document.addEventListener("pointerdown", (e) => {
455
+ if (AvbridgePlayerElement.isPlayerChromeEvent(e)) return;
456
+ startSwipeGesture(e);
457
+ }, { capture: true });
458
+ ```
459
+
460
+ `isPlayerChromeEvent(event)` returns `true` only for events whose
461
+ `composedPath()` includes the player's interactive chrome (works
462
+ across the shadow boundary). Events on the bare video surface return
463
+ `false` — the host page remains free to claim those for its own
464
+ gestures (e.g. swipe-to-next-video). Events that didn't hit a player
465
+ at all return `false`.
466
+
434
467
  This is a second tsup entry (`dist/element-browser.js`) that inlines
435
468
  mediabunny + libavjs-webcodecs-bridge into a single ~1.3 MB file with
436
469
  zero bare specifiers at runtime. Perfect for self-hosted tools or static
@@ -34,7 +34,7 @@ async function probeWithLibav(source, sniffed) {
34
34
  codec: ffmpegToAvbridgeVideo(codecName),
35
35
  width: codecpar?.width ?? 0,
36
36
  height: codecpar?.height ?? 0,
37
- fps: framerate(stream)
37
+ fps: await framerate(libav, stream)
38
38
  });
39
39
  } else if (stream.codec_type === libav.AVMEDIA_TYPE_AUDIO) {
40
40
  audioTracks.push({
@@ -62,7 +62,7 @@ async function probeWithLibav(source, sniffed) {
62
62
  duration
63
63
  };
64
64
  }
65
- function framerate(stream) {
65
+ async function framerate(libav, stream) {
66
66
  if (typeof stream.avg_frame_rate_num === "number" && stream.avg_frame_rate_den) {
67
67
  return stream.avg_frame_rate_num / stream.avg_frame_rate_den;
68
68
  }
@@ -70,6 +70,14 @@ function framerate(stream) {
70
70
  if (stream.avg_frame_rate.den === 0) return void 0;
71
71
  return stream.avg_frame_rate.num / stream.avg_frame_rate.den;
72
72
  }
73
+ try {
74
+ const num = await libav.AVCodecParameters_framerate_num?.(stream.codecpar);
75
+ const den = await libav.AVCodecParameters_framerate_den?.(stream.codecpar);
76
+ if (typeof num === "number" && typeof den === "number" && den > 0 && num > 0) {
77
+ return num / den;
78
+ }
79
+ } catch {
80
+ }
73
81
  return void 0;
74
82
  }
75
83
  async function safeDuration(libav, fmt_ctx) {
@@ -187,5 +195,5 @@ function ffmpegToAvbridgeAudio(name) {
187
195
  }
188
196
 
189
197
  exports.probeWithLibav = probeWithLibav;
190
- //# sourceMappingURL=avi-EQE6AR75.cjs.map
191
- //# sourceMappingURL=avi-EQE6AR75.cjs.map
198
+ //# sourceMappingURL=avi-32UABODO.cjs.map
199
+ //# sourceMappingURL=avi-32UABODO.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/probe/avi.ts"],"names":["loadLibav","prepareLibavInput"],"mappings":";;;;;;AAuBA,eAAsB,cAAA,CACpB,QACA,OAAA,EACuB;AAKvB,EAAA,MAAM,KAAA,GAAS,MAAMA,2BAAA,CAAU,UAAU,CAAA;AAEzC,EAAA,MAAM,WAAW,MAAA,CAAO,IAAA,IAAQ,SAAS,OAAA,KAAY,SAAA,GAAY,QAAQ,OAAO,CAAA,CAAA;AAIhF,EAAA,MAAM,MAAA,GAA2B,MAAMC,mCAAA,CAAkB,KAAA,EAA6D,UAAU,MAAM,CAAA;AAEtI,EAAA,IAAI,OAAA;AACJ,EAAA,IAAI,UAAyB,EAAC;AAC9B,EAAA,IAAI;AACF,IAAA,MAAM,MAAA,GAAS,MAAM,KAAA,CAAM,oBAAA,CAAqB,QAAQ,CAAA;AACxD,IAAA,OAAA,GAAU,OAAO,CAAC,CAAA;AAClB,IAAA,OAAA,GAAU,OAAO,CAAC,CAAA;AAAA,EACpB,SAAS,GAAA,EAAK;AACZ,IAAA,MAAM,MAAA,CAAO,MAAA,EAAO,CAAE,KAAA,CAAM,MAAM;AAAA,IAAC,CAAC,CAAA;AAKpC,IAAA,MAAM,KAAA,GACJ,GAAA,YAAe,KAAA,GACX,GAAA,CAAI,UACJ,OAAO,GAAA,KAAQ,QAAA,IAAY,GAAA,KAAQ,OACjC,IAAA,CAAK,SAAA,CAAU,GAAG,CAAA,GAClB,OAAO,GAAG,CAAA;AAElB,IAAA,OAAA,CAAQ,KAAA,CAAM,8CAA8C,GAAG,CAAA;AAC/D,IAAA,MAAM,IAAI,KAAA;AAAA,MACR,CAAA,yBAAA,EAA4B,QAAQ,CAAA,8HAAA,EAAiI,K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KAAK,MAAA;AAAU,MAAA,OAAO,MAAA;AAAA,IACtB,KAAK,KAAA;AAAU,MAAA,OAAO,KAAA;AAAA,IACtB,KAAK,MAAA;AAAU,MAAA,OAAO,MAAA;AAAA,IACtB,KAAK,OAAA;AAAA,IACL,KAAK,OAAA;AAAU,MAAA,OAAO,OAAA;AAAA,IACtB,KAAK,QAAA;AAAU,MAAA,OAAO,QAAA;AAAA,IACtB,KAAK,MAAA;AAAU,MAAA,OAAO,MAAA;AAAA,IACtB,KAAK,MAAA;AAAU,MAAA,OAAO,MAAA;AAAA,IACtB,KAAK,QAAA;AAAU,MAAA,OAAO,QAAA;AAAA,IACtB,KAAK,QAAA;AAAU,MAAA,OAAO,QAAA;AAAA,IACtB,KAAK,MAAA;AAAU,MAAA,OAAO,MAAA;AAAA,IACtB,KAAK,QAAA;AAAU,MAAA,OAAO,QAAA;AAAA,IACtB,KAAK,KAAA;AAAA,IACL,KAAK,KAAA;AAAU,MAAA,OAAO,KAAA;AAAA,IACtB,KAAK,QAAA;AAAA,IACL,KAAK,KAAA;AAAU,MAAA,OAAO,QAAA;AAAA,IACtB;AAAe,MAAA,OAAO,IAAA;AAAA;AAE1B","file":"avi-32UABODO.cjs","sourcesContent":["import type {\n AudioCodec,\n AudioTrackInfo,\n ContainerKind,\n MediaContext,\n VideoCodec,\n VideoTrackInfo,\n} from \"../types.js\";\nimport type { NormalizedSource } from \"../util/source.js\";\nimport { prepareLibavInput, type LibavInputHandle } from \"../util/libav-http-reader.js\";\nimport { loadLibav } from \"../strategies/fallback/libav-loader.js\";\n\n/**\n * Probe AVI/ASF/FLV (and any other format mediabunny doesn't speak) via\n * libav.js. This module is `import()`-ed only when sniffing identifies one of\n * those containers.\n *\n * Critical: codec identification goes through `libav.avcodec_get_name(id)`\n * which returns the FFmpeg codec name as a string (e.g. \"h264\", \"mpeg4\",\n * \"wmv3\"). The numeric AV_CODEC_ID_* enum is *not* exposed on the libav\n * instance (only AVMEDIA_TYPE_*, AV_PIX_FMT_*, AV_SAMPLE_FMT_* and a handful\n * of others are), so comparing codec_ids against constants does not work.\n */\nexport async function probeWithLibav(\n source: NormalizedSource,\n sniffed: ContainerKind,\n): Promise<MediaContext> {\n // AVI/ASF/FLV demuxers are not in any libav.js npm variant — they live in\n // the custom \"avbridge\" build produced by `scripts/build-libav.sh`. The loader\n // emits an actionable error if the build hasn't been run yet. Threading\n // is OFF by default in `loadLibav` (see the comment there for why).\n const libav = (await loadLibav(\"avbridge\")) as unknown as LibavInstance;\n\n const filename = source.name ?? `input.${sniffed === \"unknown\" ? \"bin\" : sniffed}`;\n // For Blob/File sources we use libav's in-memory readahead file. For URL\n // sources we attach an HTTP block reader so libav demuxes via Range\n // requests instead of buffering the whole file.\n const handle: LibavInputHandle = await prepareLibavInput(libav as unknown as Parameters<typeof prepareLibavInput>[0], filename, source);\n\n let fmt_ctx: number | undefined;\n let streams: LibavStream[] = [];\n try {\n const result = await libav.ff_init_demuxer_file(filename);\n fmt_ctx = result[0];\n streams = result[1];\n } catch (err) {\n await handle.detach().catch(() => {});\n // Errors thrown across the libav.js worker/pthread boundary aren't\n // always Error instances — they can be plain objects, numbers (errno\n // codes), or strings. Stringify defensively so the user-facing message\n // never has `(undefined)` in it.\n const inner =\n err instanceof Error\n ? err.message\n : typeof err === \"object\" && err !== null\n ? JSON.stringify(err)\n : String(err);\n // eslint-disable-next-line no-console\n console.error(\"[avbridge] ff_init_demuxer_file raw error:\", err);\n throw new Error(\n `libav.js could not demux ${filename}. The current libav variant likely lacks the required demuxer (e.g. AVI). See vendor/libav/README.md for build instructions. (${inner || \"no message — see console for raw error\"})`,\n );\n }\n\n const videoTracks: VideoTrackInfo[] = [];\n const audioTracks: AudioTrackInfo[] = [];\n\n for (const stream of streams) {\n const codecName = (await safe(() => libav.avcodec_get_name(stream.codec_id))) ?? `unknown(${stream.codec_id})`;\n // codecpar holds width/height/channels/sample_rate/profile/level/extradata\n // for the actual stream. We have to copy it out of WASM memory.\n const codecpar = await safe(() => libav.ff_copyout_codecpar(stream.codecpar));\n\n if (stream.codec_type === libav.AVMEDIA_TYPE_VIDEO) {\n videoTracks.push({\n id: stream.index,\n codec: ffmpegToAvbridgeVideo(codecName),\n width: codecpar?.width ?? 0,\n height: codecpar?.height ?? 0,\n fps: await framerate(libav, stream),\n });\n } else if (stream.codec_type === libav.AVMEDIA_TYPE_AUDIO) {\n audioTracks.push({\n id: stream.index,\n codec: ffmpegToAvbridgeAudio(codecName),\n channels: codecpar?.channels ?? codecpar?.ch_layout_nb_channels ?? 0,\n sampleRate: codecpar?.sample_rate ?? 0,\n });\n }\n }\n\n // We need this duration but cannot reliably get it from the streams alone\n // for AVI; libav.js exposes it via the AVFormatContext duration helper.\n const duration = await safeDuration(libav, fmt_ctx!);\n\n // Close the demuxer; the strategy will reopen it later if it ends up being\n // chosen. Probing should not pin native resources.\n await libav.avformat_close_input_js(fmt_ctx!).catch(() => {});\n await handle.detach().catch(() => {});\n\n return {\n source: source.original,\n name: source.name,\n byteLength: source.byteLength,\n container: sniffed === \"unknown\" ? \"unknown\" : sniffed,\n videoTracks,\n audioTracks,\n subtitleTracks: [],\n probedBy: \"libav\",\n duration,\n };\n}\n\n/**\n * Read frame rate from the stream's `AVCodecParameters.framerate`.\n *\n * `avg_frame_rate` / `r_frame_rate` live on the AVStream in C but libav.js\n * doesn't expose them as JS properties on the stream record — only via\n * dedicated accessor functions we don't ship. `codecpar.framerate` IS\n * accessible via `AVCodecParameters_framerate_num/_den` and is populated\n * for most containers (for AVI it's parsed from `dwRate`/`dwScale` in the\n * stream header).\n *\n * Returns undefined if unavailable so the caller can fall back to a\n * container-appropriate default (currently 30 fps, which is wrong for\n * 25 fps PAL and 23.976 fps film content — hence the importance of\n * reading this correctly).\n */\nasync function framerate(\n libav: LibavInstance,\n stream: LibavStream,\n): Promise<number | undefined> {\n // The stream record may still carry these (new libav.js versions) —\n // prefer them when present.\n if (typeof stream.avg_frame_rate_num === \"number\" && stream.avg_frame_rate_den) {\n return stream.avg_frame_rate_num / stream.avg_frame_rate_den;\n }\n if (stream.avg_frame_rate && typeof stream.avg_frame_rate === \"object\") {\n if (stream.avg_frame_rate.den === 0) return undefined;\n return stream.avg_frame_rate.num / stream.avg_frame_rate.den;\n }\n try {\n const num = await libav.AVCodecParameters_framerate_num?.(stream.codecpar);\n const den = await libav.AVCodecParameters_framerate_den?.(stream.codecpar);\n if (typeof num === \"number\" && typeof den === \"number\" && den > 0 && num > 0) {\n return num / den;\n }\n } catch {\n // Fall through.\n }\n return undefined;\n}\n\nasync function safeDuration(libav: LibavInstance, fmt_ctx: number): Promise<number | undefined> {\n try {\n // `AVFormatContext.duration` is an int64 in microseconds (AV_TIME_BASE).\n // libav.js exposes it as a split lo/hi pair the same way it does for\n // packet pts — `AVFormatContext_duration(ctx)` returns the low 32 bits,\n // `AVFormatContext_durationhi(ctx)` returns the high 32 bits. Reading\n // only the low half (the previous bug) gave garbage for any file whose\n // duration > ~35 minutes, and zero for shorter files where the value\n // happened to live in the high half.\n const lo = await libav.AVFormatContext_duration?.(fmt_ctx);\n const hi = await libav.AVFormatContext_durationhi?.(fmt_ctx);\n if (typeof lo !== \"number\" || typeof hi !== \"number\") return undefined;\n\n // AV_NOPTS_VALUE = -2^63 → ptshi = -2147483648, pts = 0. Means \"unknown\".\n if (hi === -2147483648 && lo === 0) return undefined;\n\n // Reconstruct the 64-bit value. Prefer libav's helper when available\n // because it correctly handles signed 32-bit two's complement.\n const us =\n typeof libav.i64tof64 === \"function\"\n ? libav.i64tof64(lo, hi)\n : hi * 0x100000000 + lo + (lo < 0 ? 0x100000000 : 0);\n\n if (!Number.isFinite(us) || us <= 0) return undefined;\n return us / 1_000_000;\n } catch {\n return undefined;\n }\n}\n\nasync function safe<T>(fn: () => Promise<T> | T): Promise<T | undefined> {\n try { return await fn(); } catch { return undefined; }\n}\n\n/** Map FFmpeg codec names to avbridge video codec identifiers. */\nfunction ffmpegToAvbridgeVideo(name: string): VideoCodec {\n switch (name) {\n case \"h264\": return \"h264\";\n case \"hevc\": return \"h265\";\n case \"vp8\": return \"vp8\";\n case \"vp9\": return \"vp9\";\n case \"av1\": return \"av1\";\n case \"mpeg4\": return \"mpeg4\"; // MPEG-4 Part 2 / DivX / Xvid\n case \"msmpeg4v1\":\n case \"msmpeg4v2\":\n case \"msmpeg4v3\": // a.k.a. DIV3\n return \"mpeg4\";\n case \"wmv1\":\n case \"wmv2\":\n case \"wmv3\":\n return \"wmv3\";\n case \"vc1\": return \"vc1\";\n case \"mpeg2video\": return \"mpeg2\";\n case \"mpeg1video\": return \"mpeg1\";\n case \"theora\": return \"theora\";\n case \"rv10\": return \"rv10\";\n case \"rv20\": return \"rv20\";\n case \"rv30\": return \"rv30\";\n case \"rv40\": return \"rv40\";\n case \"dvvideo\": return \"dv\"; // DV / DVCPRO (camcorder, MiniDV)\n case \"hq_hqa\": return \"hq_hqa\"; // Canopus HQ / HQA (Grass Valley)\n default: return name as VideoCodec;\n }\n}\n\nfunction ffmpegToAvbridgeAudio(name: string): AudioCodec {\n switch (name) {\n case \"aac\": return \"aac\";\n case \"mp3\":\n case \"mp3float\":\n return \"mp3\";\n case \"opus\": return \"opus\";\n case \"vorbis\": return \"vorbis\";\n case \"flac\": return \"flac\";\n case \"ac3\": return \"ac3\";\n case \"eac3\": return \"eac3\";\n case \"wmav1\":\n case \"wmav2\": return \"wmav2\";\n case \"wmapro\": return \"wmapro\";\n case \"alac\": return \"alac\";\n case \"cook\": return \"cook\";\n case \"ra_144\": return \"ra_144\";\n case \"ra_288\": return \"ra_288\";\n case \"sipr\": return \"sipr\";\n case \"atrac3\": return \"atrac3\";\n case \"dca\":\n case \"dts\": return \"dts\";\n case \"truehd\":\n case \"mlp\": return \"truehd\";\n default: return name as AudioCodec;\n }\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Minimal structural types for the slice of libav.js we touch.\n// ─────────────────────────────────────────────────────────────────────────────\n\ninterface LibavStream {\n index: number;\n codec_type: number;\n codec_id: number;\n codecpar: number;\n avg_frame_rate?: { num: number; den: number };\n avg_frame_rate_num?: number;\n avg_frame_rate_den?: number;\n}\n\ninterface LibavCodecpar {\n width?: number;\n height?: number;\n channels?: number;\n ch_layout_nb_channels?: number;\n sample_rate?: number;\n profile?: number;\n level?: number;\n}\n\ninterface LibavInstance {\n mkreadaheadfile(name: string, blob: Blob): Promise<void>;\n unlinkreadaheadfile(name: string): Promise<void>;\n ff_init_demuxer_file(name: string): Promise<[number, LibavStream[]]>;\n ff_copyout_codecpar(codecpar: number): Promise<LibavCodecpar>;\n avcodec_get_name(codec_id: number): Promise<string>;\n avformat_close_input_js(ctx: number): Promise<void>;\n AVFormatContext_duration?(ctx: number): Promise<number>;\n AVFormatContext_durationhi?(ctx: number): Promise<number>;\n i64tof64?(lo: number, hi: number): number;\n\n AVCodecParameters_framerate_num?(codecpar: number): Promise<number>;\n AVCodecParameters_framerate_den?(codecpar: number): Promise<number>;\n\n AVMEDIA_TYPE_VIDEO: number;\n AVMEDIA_TYPE_AUDIO: number;\n}\n"]}
@@ -34,7 +34,7 @@ async function probeWithLibav(source, sniffed) {
34
34
  codec: ffmpegToAvbridgeVideo(codecName),
35
35
  width: codecpar?.width ?? 0,
36
36
  height: codecpar?.height ?? 0,
37
- fps: framerate(stream)
37
+ fps: await framerate(libav, stream)
38
38
  });
39
39
  } else if (stream.codec_type === libav.AVMEDIA_TYPE_AUDIO) {
40
40
  audioTracks.push({
@@ -62,7 +62,7 @@ async function probeWithLibav(source, sniffed) {
62
62
  duration
63
63
  };
64
64
  }
65
- function framerate(stream) {
65
+ async function framerate(libav, stream) {
66
66
  if (typeof stream.avg_frame_rate_num === "number" && stream.avg_frame_rate_den) {
67
67
  return stream.avg_frame_rate_num / stream.avg_frame_rate_den;
68
68
  }
@@ -70,6 +70,14 @@ function framerate(stream) {
70
70
  if (stream.avg_frame_rate.den === 0) return void 0;
71
71
  return stream.avg_frame_rate.num / stream.avg_frame_rate.den;
72
72
  }
73
+ try {
74
+ const num = await libav.AVCodecParameters_framerate_num?.(stream.codecpar);
75
+ const den = await libav.AVCodecParameters_framerate_den?.(stream.codecpar);
76
+ if (typeof num === "number" && typeof den === "number" && den > 0 && num > 0) {
77
+ return num / den;
78
+ }
79
+ } catch {
80
+ }
73
81
  return void 0;
74
82
  }
75
83
  async function safeDuration(libav, fmt_ctx) {
@@ -187,5 +195,5 @@ function ffmpegToAvbridgeAudio(name) {
187
195
  }
188
196
 
189
197
  exports.probeWithLibav = probeWithLibav;
190
- //# sourceMappingURL=avi-Y3N325WZ.cjs.map
191
- //# sourceMappingURL=avi-Y3N325WZ.cjs.map
198
+ //# sourceMappingURL=avi-5BPR6QUX.cjs.map
199
+ //# sourceMappingURL=avi-5BPR6QUX.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/probe/avi.ts"],"names":["loadLibav","prepareLibavInput"],"mappings":";;;;;;AAuBA,eAAsB,cAAA,CACpB,QACA,OAAA,EACuB;AAKvB,EAAA,MAAM,KAAA,GAAS,MAAMA,2BAAA,CAAU,UAAU,CAAA;AAEzC,EAAA,MAAM,WAAW,MAAA,CAAO,IAAA,IAAQ,SAAS,OAAA,KAAY,SAAA,GAAY,QAAQ,OAAO,CAAA,CAAA;AAIhF,EAAA,MAAM,MAAA,GAA2B,MAAMC,mCAAA,CAAkB,KAAA,EAA6D,UAAU,MAAM,CAAA;AAEtI,EAAA,IAAI,OAAA;AACJ,EAAA,IAAI,UAAyB,EAAC;AAC9B,EAAA,IAAI;AACF,IAAA,MAAM,MAAA,GAAS,MAAM,KAAA,CAAM,oBAAA,CAAqB,QAAQ,CAAA;AACxD,IAAA,OAAA,GAAU,OAAO,CAAC,CAAA;AAClB,IAAA,OAAA,GAAU,OAAO,CAAC,CAAA;AAAA,EACpB,SAAS,GAAA,EAAK;AACZ,IAAA,MAAM,MAAA,CAAO,MAAA,EAAO,CAAE,KAAA,CAAM,MAAM;AAAA,IAAC,CAAC,CAAA;AAKpC,IAAA,MAAM,KAAA,GACJ,GAAA,YAAe,KAAA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AAEA,SAAS,sBAAsB,IAAA,EAA0B;AACvD,EAAA,QAAQ,IAAA;AAAM,IACZ,KAAK,KAAA;AAAU,MAAA,OAAO,KAAA;AAAA,IACtB,KAAK,KAAA;AAAA,IACL,KAAK,UAAA;AACH,MAAA,OAAO,KAAA;AAAA,IACT,KAAK,MAAA;AAAU,MAAA,OAAO,MAAA;AAAA,IACtB,KAAK,QAAA;AAAU,MAAA,OAAO,QAAA;AAAA,IACtB,KAAK,MAAA;AAAU,MAAA,OAAO,MAAA;AAAA,IACtB,KAAK,KAAA;AAAU,MAAA,OAAO,KAAA;AAAA,IACtB,KAAK,MAAA;AAAU,MAAA,OAAO,MAAA;AAAA,IACtB,KAAK,OAAA;AAAA,IACL,KAAK,OAAA;AAAU,MAAA,OAAO,OAAA;AAAA,IACtB,KAAK,QAAA;AAAU,MAAA,OAAO,QAAA;AAAA,IACtB,KAAK,MAAA;AAAU,MAAA,OAAO,MAAA;AAAA,IACtB,KAAK,MAAA;AAAU,MAAA,OAAO,MAAA;AAAA,IACtB,KAAK,QAAA;AAAU,MAAA,OAAO,QAAA;AAAA,IACtB,KAAK,QAAA;AAAU,MAAA,OAAO,QAAA;AAAA,IACtB,KAAK,MAAA;AAAU,MAAA,OAAO,MAAA;AAAA,IACtB,KAAK,QAAA;AAAU,MAAA,OAAO,QAAA;AAAA,IACtB,KAAK,KAAA;AAAA,IACL,KAAK,KAAA;AAAU,MAAA,OAAO,KAAA;AAAA,IACtB,KAAK,QAAA;AAAA,IACL,KAAK,KAAA;AAAU,MAAA,OAAO,QAAA;AAAA,IACtB;AAAe,MAAA,OAAO,IAAA;AAAA;AAE1B","file":"avi-5BPR6QUX.cjs","sourcesContent":["import type {\n AudioCodec,\n AudioTrackInfo,\n ContainerKind,\n MediaContext,\n VideoCodec,\n VideoTrackInfo,\n} from \"../types.js\";\nimport type { NormalizedSource } from \"../util/source.js\";\nimport { prepareLibavInput, type LibavInputHandle } from \"../util/libav-http-reader.js\";\nimport { loadLibav } from \"../strategies/fallback/libav-loader.js\";\n\n/**\n * Probe AVI/ASF/FLV (and any other format mediabunny doesn't speak) via\n * libav.js. This module is `import()`-ed only when sniffing identifies one of\n * those containers.\n *\n * Critical: codec identification goes through `libav.avcodec_get_name(id)`\n * which returns the FFmpeg codec name as a string (e.g. \"h264\", \"mpeg4\",\n * \"wmv3\"). The numeric AV_CODEC_ID_* enum is *not* exposed on the libav\n * instance (only AVMEDIA_TYPE_*, AV_PIX_FMT_*, AV_SAMPLE_FMT_* and a handful\n * of others are), so comparing codec_ids against constants does not work.\n */\nexport async function probeWithLibav(\n source: NormalizedSource,\n sniffed: ContainerKind,\n): Promise<MediaContext> {\n // AVI/ASF/FLV demuxers are not in any libav.js npm variant — they live in\n // the custom \"avbridge\" build produced by `scripts/build-libav.sh`. The loader\n // emits an actionable error if the build hasn't been run yet. Threading\n // is OFF by default in `loadLibav` (see the comment there for why).\n const libav = (await loadLibav(\"avbridge\")) as unknown as LibavInstance;\n\n const filename = source.name ?? `input.${sniffed === \"unknown\" ? \"bin\" : sniffed}`;\n // For Blob/File sources we use libav's in-memory readahead file. For URL\n // sources we attach an HTTP block reader so libav demuxes via Range\n // requests instead of buffering the whole file.\n const handle: LibavInputHandle = await prepareLibavInput(libav as unknown as Parameters<typeof prepareLibavInput>[0], filename, source);\n\n let fmt_ctx: number | undefined;\n let streams: LibavStream[] = [];\n try {\n const result = await libav.ff_init_demuxer_file(filename);\n fmt_ctx = result[0];\n streams = result[1];\n } catch (err) {\n await handle.detach().catch(() => {});\n // Errors thrown across the libav.js worker/pthread boundary aren't\n // always Error instances — they can be plain objects, numbers (errno\n // codes), or strings. Stringify defensively so the user-facing message\n // never has `(undefined)` in it.\n const inner =\n err instanceof Error\n ? err.message\n : typeof err === \"object\" && err !== null\n ? JSON.stringify(err)\n : String(err);\n // eslint-disable-next-line no-console\n console.error(\"[avbridge] ff_init_demuxer_file raw error:\", err);\n throw new Error(\n `libav.js could not demux ${filename}. The current libav variant likely lacks the required demuxer (e.g. AVI). See vendor/libav/README.md for build instructions. (${inner || \"no message — see console for raw error\"})`,\n );\n }\n\n const videoTracks: VideoTrackInfo[] = [];\n const audioTracks: AudioTrackInfo[] = [];\n\n for (const stream of streams) {\n const codecName = (await safe(() => libav.avcodec_get_name(stream.codec_id))) ?? `unknown(${stream.codec_id})`;\n // codecpar holds width/height/channels/sample_rate/profile/level/extradata\n // for the actual stream. We have to copy it out of WASM memory.\n const codecpar = await safe(() => libav.ff_copyout_codecpar(stream.codecpar));\n\n if (stream.codec_type === libav.AVMEDIA_TYPE_VIDEO) {\n videoTracks.push({\n id: stream.index,\n codec: ffmpegToAvbridgeVideo(codecName),\n width: codecpar?.width ?? 0,\n height: codecpar?.height ?? 0,\n fps: await framerate(libav, stream),\n });\n } else if (stream.codec_type === libav.AVMEDIA_TYPE_AUDIO) {\n audioTracks.push({\n id: stream.index,\n codec: ffmpegToAvbridgeAudio(codecName),\n channels: codecpar?.channels ?? codecpar?.ch_layout_nb_channels ?? 0,\n sampleRate: codecpar?.sample_rate ?? 0,\n });\n }\n }\n\n // We need this duration but cannot reliably get it from the streams alone\n // for AVI; libav.js exposes it via the AVFormatContext duration helper.\n const duration = await safeDuration(libav, fmt_ctx!);\n\n // Close the demuxer; the strategy will reopen it later if it ends up being\n // chosen. Probing should not pin native resources.\n await libav.avformat_close_input_js(fmt_ctx!).catch(() => {});\n await handle.detach().catch(() => {});\n\n return {\n source: source.original,\n name: source.name,\n byteLength: source.byteLength,\n container: sniffed === \"unknown\" ? \"unknown\" : sniffed,\n videoTracks,\n audioTracks,\n subtitleTracks: [],\n probedBy: \"libav\",\n duration,\n };\n}\n\n/**\n * Read frame rate from the stream's `AVCodecParameters.framerate`.\n *\n * `avg_frame_rate` / `r_frame_rate` live on the AVStream in C but libav.js\n * doesn't expose them as JS properties on the stream record — only via\n * dedicated accessor functions we don't ship. `codecpar.framerate` IS\n * accessible via `AVCodecParameters_framerate_num/_den` and is populated\n * for most containers (for AVI it's parsed from `dwRate`/`dwScale` in the\n * stream header).\n *\n * Returns undefined if unavailable so the caller can fall back to a\n * container-appropriate default (currently 30 fps, which is wrong for\n * 25 fps PAL and 23.976 fps film content — hence the importance of\n * reading this correctly).\n */\nasync function framerate(\n libav: LibavInstance,\n stream: LibavStream,\n): Promise<number | undefined> {\n // The stream record may still carry these (new libav.js versions) —\n // prefer them when present.\n if (typeof stream.avg_frame_rate_num === \"number\" && stream.avg_frame_rate_den) {\n return stream.avg_frame_rate_num / stream.avg_frame_rate_den;\n }\n if (stream.avg_frame_rate && typeof stream.avg_frame_rate === \"object\") {\n if (stream.avg_frame_rate.den === 0) return undefined;\n return stream.avg_frame_rate.num / stream.avg_frame_rate.den;\n }\n try {\n const num = await libav.AVCodecParameters_framerate_num?.(stream.codecpar);\n const den = await libav.AVCodecParameters_framerate_den?.(stream.codecpar);\n if (typeof num === \"number\" && typeof den === \"number\" && den > 0 && num > 0) {\n return num / den;\n }\n } catch {\n // Fall through.\n }\n return undefined;\n}\n\nasync function safeDuration(libav: LibavInstance, fmt_ctx: number): Promise<number | undefined> {\n try {\n // `AVFormatContext.duration` is an int64 in microseconds (AV_TIME_BASE).\n // libav.js exposes it as a split lo/hi pair the same way it does for\n // packet pts — `AVFormatContext_duration(ctx)` returns the low 32 bits,\n // `AVFormatContext_durationhi(ctx)` returns the high 32 bits. Reading\n // only the low half (the previous bug) gave garbage for any file whose\n // duration > ~35 minutes, and zero for shorter files where the value\n // happened to live in the high half.\n const lo = await libav.AVFormatContext_duration?.(fmt_ctx);\n const hi = await libav.AVFormatContext_durationhi?.(fmt_ctx);\n if (typeof lo !== \"number\" || typeof hi !== \"number\") return undefined;\n\n // AV_NOPTS_VALUE = -2^63 → ptshi = -2147483648, pts = 0. Means \"unknown\".\n if (hi === -2147483648 && lo === 0) return undefined;\n\n // Reconstruct the 64-bit value. Prefer libav's helper when available\n // because it correctly handles signed 32-bit two's complement.\n const us =\n typeof libav.i64tof64 === \"function\"\n ? libav.i64tof64(lo, hi)\n : hi * 0x100000000 + lo + (lo < 0 ? 0x100000000 : 0);\n\n if (!Number.isFinite(us) || us <= 0) return undefined;\n return us / 1_000_000;\n } catch {\n return undefined;\n }\n}\n\nasync function safe<T>(fn: () => Promise<T> | T): Promise<T | undefined> {\n try { return await fn(); } catch { return undefined; }\n}\n\n/** Map FFmpeg codec names to avbridge video codec identifiers. */\nfunction ffmpegToAvbridgeVideo(name: string): VideoCodec {\n switch (name) {\n case \"h264\": return \"h264\";\n case \"hevc\": return \"h265\";\n case \"vp8\": return \"vp8\";\n case \"vp9\": return \"vp9\";\n case \"av1\": return \"av1\";\n case \"mpeg4\": return \"mpeg4\"; // MPEG-4 Part 2 / DivX / Xvid\n case \"msmpeg4v1\":\n case \"msmpeg4v2\":\n case \"msmpeg4v3\": // a.k.a. DIV3\n return \"mpeg4\";\n case \"wmv1\":\n case \"wmv2\":\n case \"wmv3\":\n return \"wmv3\";\n case \"vc1\": return \"vc1\";\n case \"mpeg2video\": return \"mpeg2\";\n case \"mpeg1video\": return \"mpeg1\";\n case \"theora\": return \"theora\";\n case \"rv10\": return \"rv10\";\n case \"rv20\": return \"rv20\";\n case \"rv30\": return \"rv30\";\n case \"rv40\": return \"rv40\";\n case \"dvvideo\": return \"dv\"; // DV / DVCPRO (camcorder, MiniDV)\n case \"hq_hqa\": return \"hq_hqa\"; // Canopus HQ / HQA (Grass Valley)\n default: return name as VideoCodec;\n }\n}\n\nfunction ffmpegToAvbridgeAudio(name: string): AudioCodec {\n switch (name) {\n case \"aac\": return \"aac\";\n case \"mp3\":\n case \"mp3float\":\n return \"mp3\";\n case \"opus\": return \"opus\";\n case \"vorbis\": return \"vorbis\";\n case \"flac\": return \"flac\";\n case \"ac3\": return \"ac3\";\n case \"eac3\": return \"eac3\";\n case \"wmav1\":\n case \"wmav2\": return \"wmav2\";\n case \"wmapro\": return \"wmapro\";\n case \"alac\": return \"alac\";\n case \"cook\": return \"cook\";\n case \"ra_144\": return \"ra_144\";\n case \"ra_288\": return \"ra_288\";\n case \"sipr\": return \"sipr\";\n case \"atrac3\": return \"atrac3\";\n case \"dca\":\n case \"dts\": return \"dts\";\n case \"truehd\":\n case \"mlp\": return \"truehd\";\n default: return name as AudioCodec;\n }\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Minimal structural types for the slice of libav.js we touch.\n// ─────────────────────────────────────────────────────────────────────────────\n\ninterface LibavStream {\n index: number;\n codec_type: number;\n codec_id: number;\n codecpar: number;\n avg_frame_rate?: { num: number; den: number };\n avg_frame_rate_num?: number;\n avg_frame_rate_den?: number;\n}\n\ninterface LibavCodecpar {\n width?: number;\n height?: number;\n channels?: number;\n ch_layout_nb_channels?: number;\n sample_rate?: number;\n profile?: number;\n level?: number;\n}\n\ninterface LibavInstance {\n mkreadaheadfile(name: string, blob: Blob): Promise<void>;\n unlinkreadaheadfile(name: string): Promise<void>;\n ff_init_demuxer_file(name: string): Promise<[number, LibavStream[]]>;\n ff_copyout_codecpar(codecpar: number): Promise<LibavCodecpar>;\n avcodec_get_name(codec_id: number): Promise<string>;\n avformat_close_input_js(ctx: number): Promise<void>;\n AVFormatContext_duration?(ctx: number): Promise<number>;\n AVFormatContext_durationhi?(ctx: number): Promise<number>;\n i64tof64?(lo: number, hi: number): number;\n\n AVCodecParameters_framerate_num?(codecpar: number): Promise<number>;\n AVCodecParameters_framerate_den?(codecpar: number): Promise<number>;\n\n AVMEDIA_TYPE_VIDEO: number;\n AVMEDIA_TYPE_AUDIO: number;\n}\n"]}
@@ -32,7 +32,7 @@ async function probeWithLibav(source, sniffed) {
32
32
  codec: ffmpegToAvbridgeVideo(codecName),
33
33
  width: codecpar?.width ?? 0,
34
34
  height: codecpar?.height ?? 0,
35
- fps: framerate(stream)
35
+ fps: await framerate(libav, stream)
36
36
  });
37
37
  } else if (stream.codec_type === libav.AVMEDIA_TYPE_AUDIO) {
38
38
  audioTracks.push({
@@ -60,7 +60,7 @@ async function probeWithLibav(source, sniffed) {
60
60
  duration
61
61
  };
62
62
  }
63
- function framerate(stream) {
63
+ async function framerate(libav, stream) {
64
64
  if (typeof stream.avg_frame_rate_num === "number" && stream.avg_frame_rate_den) {
65
65
  return stream.avg_frame_rate_num / stream.avg_frame_rate_den;
66
66
  }
@@ -68,6 +68,14 @@ function framerate(stream) {
68
68
  if (stream.avg_frame_rate.den === 0) return void 0;
69
69
  return stream.avg_frame_rate.num / stream.avg_frame_rate.den;
70
70
  }
71
+ try {
72
+ const num = await libav.AVCodecParameters_framerate_num?.(stream.codecpar);
73
+ const den = await libav.AVCodecParameters_framerate_den?.(stream.codecpar);
74
+ if (typeof num === "number" && typeof den === "number" && den > 0 && num > 0) {
75
+ return num / den;
76
+ }
77
+ } catch {
78
+ }
71
79
  return void 0;
72
80
  }
73
81
  async function safeDuration(libav, fmt_ctx) {
@@ -185,5 +193,5 @@ function ffmpegToAvbridgeAudio(name) {
185
193
  }
186
194
 
187
195
  export { probeWithLibav };
188
- //# sourceMappingURL=avi-NNHH4AAA.js.map
189
- //# sourceMappingURL=avi-NNHH4AAA.js.map
196
+ //# sourceMappingURL=avi-BLIH7KKV.js.map
197
+ //# sourceMappingURL=avi-BLIH7KKV.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/probe/avi.ts"],"names":[],"mappings":";;;;AAuBA,eAAsB,cAAA,CACpB,QACA,OAAA,EACuB;AAKvB,EAAA,MAAM,KAAA,GAAS,MAAM,SAAA,CAAU,UAAU,CAAA;AAEzC,EAAA,MAAM,WAAW,MAAA,CAAO,IAAA,IAAQ,SAAS,OAAA,KAAY,SAAA,GAAY,QAAQ,OAAO,CAAA,CAAA;AAIhF,EAAA,MAAM,MAAA,GAA2B,MAAM,iBAAA,CAAkB,KAAA,EAA6D,UAAU,MAAM,CAAA;AAEtI,EAAA,IAAI,OAAA;AACJ,EAAA,IAAI,UAAyB,EAAC;AAC9B,EAAA,IAAI;AACF,IAAA,MAAM,MAAA,GAAS,MAAM,KAAA,CAAM,oBAAA,CAAqB,QAAQ,CAAA;AACxD,IAA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,OAAO,IAAA;AAAA;AAE1B","file":"avi-BLIH7KKV.js","sourcesContent":["import type {\n AudioCodec,\n AudioTrackInfo,\n ContainerKind,\n MediaContext,\n VideoCodec,\n VideoTrackInfo,\n} from \"../types.js\";\nimport type { NormalizedSource } from \"../util/source.js\";\nimport { prepareLibavInput, type LibavInputHandle } from \"../util/libav-http-reader.js\";\nimport { loadLibav } from \"../strategies/fallback/libav-loader.js\";\n\n/**\n * Probe AVI/ASF/FLV (and any other format mediabunny doesn't speak) via\n * libav.js. This module is `import()`-ed only when sniffing identifies one of\n * those containers.\n *\n * Critical: codec identification goes through `libav.avcodec_get_name(id)`\n * which returns the FFmpeg codec name as a string (e.g. \"h264\", \"mpeg4\",\n * \"wmv3\"). The numeric AV_CODEC_ID_* enum is *not* exposed on the libav\n * instance (only AVMEDIA_TYPE_*, AV_PIX_FMT_*, AV_SAMPLE_FMT_* and a handful\n * of others are), so comparing codec_ids against constants does not work.\n */\nexport async function probeWithLibav(\n source: NormalizedSource,\n sniffed: ContainerKind,\n): Promise<MediaContext> {\n // AVI/ASF/FLV demuxers are not in any libav.js npm variant — they live in\n // the custom \"avbridge\" build produced by `scripts/build-libav.sh`. The loader\n // emits an actionable error if the build hasn't been run yet. Threading\n // is OFF by default in `loadLibav` (see the comment there for why).\n const libav = (await loadLibav(\"avbridge\")) as unknown as LibavInstance;\n\n const filename = source.name ?? `input.${sniffed === \"unknown\" ? \"bin\" : sniffed}`;\n // For Blob/File sources we use libav's in-memory readahead file. For URL\n // sources we attach an HTTP block reader so libav demuxes via Range\n // requests instead of buffering the whole file.\n const handle: LibavInputHandle = await prepareLibavInput(libav as unknown as Parameters<typeof prepareLibavInput>[0], filename, source);\n\n let fmt_ctx: number | undefined;\n let streams: LibavStream[] = [];\n try {\n const result = await libav.ff_init_demuxer_file(filename);\n fmt_ctx = result[0];\n streams = result[1];\n } catch (err) {\n await handle.detach().catch(() => {});\n // Errors thrown across the libav.js worker/pthread boundary aren't\n // always Error instances — they can be plain objects, numbers (errno\n // codes), or strings. Stringify defensively so the user-facing message\n // never has `(undefined)` in it.\n const inner =\n err instanceof Error\n ? err.message\n : typeof err === \"object\" && err !== null\n ? JSON.stringify(err)\n : String(err);\n // eslint-disable-next-line no-console\n console.error(\"[avbridge] ff_init_demuxer_file raw error:\", err);\n throw new Error(\n `libav.js could not demux ${filename}. The current libav variant likely lacks the required demuxer (e.g. AVI). See vendor/libav/README.md for build instructions. (${inner || \"no message — see console for raw error\"})`,\n );\n }\n\n const videoTracks: VideoTrackInfo[] = [];\n const audioTracks: AudioTrackInfo[] = [];\n\n for (const stream of streams) {\n const codecName = (await safe(() => libav.avcodec_get_name(stream.codec_id))) ?? `unknown(${stream.codec_id})`;\n // codecpar holds width/height/channels/sample_rate/profile/level/extradata\n // for the actual stream. We have to copy it out of WASM memory.\n const codecpar = await safe(() => libav.ff_copyout_codecpar(stream.codecpar));\n\n if (stream.codec_type === libav.AVMEDIA_TYPE_VIDEO) {\n videoTracks.push({\n id: stream.index,\n codec: ffmpegToAvbridgeVideo(codecName),\n width: codecpar?.width ?? 0,\n height: codecpar?.height ?? 0,\n fps: await framerate(libav, stream),\n });\n } else if (stream.codec_type === libav.AVMEDIA_TYPE_AUDIO) {\n audioTracks.push({\n id: stream.index,\n codec: ffmpegToAvbridgeAudio(codecName),\n channels: codecpar?.channels ?? codecpar?.ch_layout_nb_channels ?? 0,\n sampleRate: codecpar?.sample_rate ?? 0,\n });\n }\n }\n\n // We need this duration but cannot reliably get it from the streams alone\n // for AVI; libav.js exposes it via the AVFormatContext duration helper.\n const duration = await safeDuration(libav, fmt_ctx!);\n\n // Close the demuxer; the strategy will reopen it later if it ends up being\n // chosen. Probing should not pin native resources.\n await libav.avformat_close_input_js(fmt_ctx!).catch(() => {});\n await handle.detach().catch(() => {});\n\n return {\n source: source.original,\n name: source.name,\n byteLength: source.byteLength,\n container: sniffed === \"unknown\" ? \"unknown\" : sniffed,\n videoTracks,\n audioTracks,\n subtitleTracks: [],\n probedBy: \"libav\",\n duration,\n };\n}\n\n/**\n * Read frame rate from the stream's `AVCodecParameters.framerate`.\n *\n * `avg_frame_rate` / `r_frame_rate` live on the AVStream in C but libav.js\n * doesn't expose them as JS properties on the stream record — only via\n * dedicated accessor functions we don't ship. `codecpar.framerate` IS\n * accessible via `AVCodecParameters_framerate_num/_den` and is populated\n * for most containers (for AVI it's parsed from `dwRate`/`dwScale` in the\n * stream header).\n *\n * Returns undefined if unavailable so the caller can fall back to a\n * container-appropriate default (currently 30 fps, which is wrong for\n * 25 fps PAL and 23.976 fps film content — hence the importance of\n * reading this correctly).\n */\nasync function framerate(\n libav: LibavInstance,\n stream: LibavStream,\n): Promise<number | undefined> {\n // The stream record may still carry these (new libav.js versions) —\n // prefer them when present.\n if (typeof stream.avg_frame_rate_num === \"number\" && stream.avg_frame_rate_den) {\n return stream.avg_frame_rate_num / stream.avg_frame_rate_den;\n }\n if (stream.avg_frame_rate && typeof stream.avg_frame_rate === \"object\") {\n if (stream.avg_frame_rate.den === 0) return undefined;\n return stream.avg_frame_rate.num / stream.avg_frame_rate.den;\n }\n try {\n const num = await libav.AVCodecParameters_framerate_num?.(stream.codecpar);\n const den = await libav.AVCodecParameters_framerate_den?.(stream.codecpar);\n if (typeof num === \"number\" && typeof den === \"number\" && den > 0 && num > 0) {\n return num / den;\n }\n } catch {\n // Fall through.\n }\n return undefined;\n}\n\nasync function safeDuration(libav: LibavInstance, fmt_ctx: number): Promise<number | undefined> {\n try {\n // `AVFormatContext.duration` is an int64 in microseconds (AV_TIME_BASE).\n // libav.js exposes it as a split lo/hi pair the same way it does for\n // packet pts — `AVFormatContext_duration(ctx)` returns the low 32 bits,\n // `AVFormatContext_durationhi(ctx)` returns the high 32 bits. Reading\n // only the low half (the previous bug) gave garbage for any file whose\n // duration > ~35 minutes, and zero for shorter files where the value\n // happened to live in the high half.\n const lo = await libav.AVFormatContext_duration?.(fmt_ctx);\n const hi = await libav.AVFormatContext_durationhi?.(fmt_ctx);\n if (typeof lo !== \"number\" || typeof hi !== \"number\") return undefined;\n\n // AV_NOPTS_VALUE = -2^63 → ptshi = -2147483648, pts = 0. Means \"unknown\".\n if (hi === -2147483648 && lo === 0) return undefined;\n\n // Reconstruct the 64-bit value. Prefer libav's helper when available\n // because it correctly handles signed 32-bit two's complement.\n const us =\n typeof libav.i64tof64 === \"function\"\n ? libav.i64tof64(lo, hi)\n : hi * 0x100000000 + lo + (lo < 0 ? 0x100000000 : 0);\n\n if (!Number.isFinite(us) || us <= 0) return undefined;\n return us / 1_000_000;\n } catch {\n return undefined;\n }\n}\n\nasync function safe<T>(fn: () => Promise<T> | T): Promise<T | undefined> {\n try { return await fn(); } catch { return undefined; }\n}\n\n/** Map FFmpeg codec names to avbridge video codec identifiers. */\nfunction ffmpegToAvbridgeVideo(name: string): VideoCodec {\n switch (name) {\n case \"h264\": return \"h264\";\n case \"hevc\": return \"h265\";\n case \"vp8\": return \"vp8\";\n case \"vp9\": return \"vp9\";\n case \"av1\": return \"av1\";\n case \"mpeg4\": return \"mpeg4\"; // MPEG-4 Part 2 / DivX / Xvid\n case \"msmpeg4v1\":\n case \"msmpeg4v2\":\n case \"msmpeg4v3\": // a.k.a. DIV3\n return \"mpeg4\";\n case \"wmv1\":\n case \"wmv2\":\n case \"wmv3\":\n return \"wmv3\";\n case \"vc1\": return \"vc1\";\n case \"mpeg2video\": return \"mpeg2\";\n case \"mpeg1video\": return \"mpeg1\";\n case \"theora\": return \"theora\";\n case \"rv10\": return \"rv10\";\n case \"rv20\": return \"rv20\";\n case \"rv30\": return \"rv30\";\n case \"rv40\": return \"rv40\";\n case \"dvvideo\": return \"dv\"; // DV / DVCPRO (camcorder, MiniDV)\n case \"hq_hqa\": return \"hq_hqa\"; // Canopus HQ / HQA (Grass Valley)\n default: return name as VideoCodec;\n }\n}\n\nfunction ffmpegToAvbridgeAudio(name: string): AudioCodec {\n switch (name) {\n case \"aac\": return \"aac\";\n case \"mp3\":\n case \"mp3float\":\n return \"mp3\";\n case \"opus\": return \"opus\";\n case \"vorbis\": return \"vorbis\";\n case \"flac\": return \"flac\";\n case \"ac3\": return \"ac3\";\n case \"eac3\": return \"eac3\";\n case \"wmav1\":\n case \"wmav2\": return \"wmav2\";\n case \"wmapro\": return \"wmapro\";\n case \"alac\": return \"alac\";\n case \"cook\": return \"cook\";\n case \"ra_144\": return \"ra_144\";\n case \"ra_288\": return \"ra_288\";\n case \"sipr\": return \"sipr\";\n case \"atrac3\": return \"atrac3\";\n case \"dca\":\n case \"dts\": return \"dts\";\n case \"truehd\":\n case \"mlp\": return \"truehd\";\n default: return name as AudioCodec;\n }\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Minimal structural types for the slice of libav.js we touch.\n// ─────────────────────────────────────────────────────────────────────────────\n\ninterface LibavStream {\n index: number;\n codec_type: number;\n codec_id: number;\n codecpar: number;\n avg_frame_rate?: { num: number; den: number };\n avg_frame_rate_num?: number;\n avg_frame_rate_den?: number;\n}\n\ninterface LibavCodecpar {\n width?: number;\n height?: number;\n channels?: number;\n ch_layout_nb_channels?: number;\n sample_rate?: number;\n profile?: number;\n level?: number;\n}\n\ninterface LibavInstance {\n mkreadaheadfile(name: string, blob: Blob): Promise<void>;\n unlinkreadaheadfile(name: string): Promise<void>;\n ff_init_demuxer_file(name: string): Promise<[number, LibavStream[]]>;\n ff_copyout_codecpar(codecpar: number): Promise<LibavCodecpar>;\n avcodec_get_name(codec_id: number): Promise<string>;\n avformat_close_input_js(ctx: number): Promise<void>;\n AVFormatContext_duration?(ctx: number): Promise<number>;\n AVFormatContext_durationhi?(ctx: number): Promise<number>;\n i64tof64?(lo: number, hi: number): number;\n\n AVCodecParameters_framerate_num?(codecpar: number): Promise<number>;\n AVCodecParameters_framerate_den?(codecpar: number): Promise<number>;\n\n AVMEDIA_TYPE_VIDEO: number;\n AVMEDIA_TYPE_AUDIO: number;\n}\n"]}
@@ -32,7 +32,7 @@ async function probeWithLibav(source, sniffed) {
32
32
  codec: ffmpegToAvbridgeVideo(codecName),
33
33
  width: codecpar?.width ?? 0,
34
34
  height: codecpar?.height ?? 0,
35
- fps: framerate(stream)
35
+ fps: await framerate(libav, stream)
36
36
  });
37
37
  } else if (stream.codec_type === libav.AVMEDIA_TYPE_AUDIO) {
38
38
  audioTracks.push({
@@ -60,7 +60,7 @@ async function probeWithLibav(source, sniffed) {
60
60
  duration
61
61
  };
62
62
  }
63
- function framerate(stream) {
63
+ async function framerate(libav, stream) {
64
64
  if (typeof stream.avg_frame_rate_num === "number" && stream.avg_frame_rate_den) {
65
65
  return stream.avg_frame_rate_num / stream.avg_frame_rate_den;
66
66
  }
@@ -68,6 +68,14 @@ function framerate(stream) {
68
68
  if (stream.avg_frame_rate.den === 0) return void 0;
69
69
  return stream.avg_frame_rate.num / stream.avg_frame_rate.den;
70
70
  }
71
+ try {
72
+ const num = await libav.AVCodecParameters_framerate_num?.(stream.codecpar);
73
+ const den = await libav.AVCodecParameters_framerate_den?.(stream.codecpar);
74
+ if (typeof num === "number" && typeof den === "number" && den > 0 && num > 0) {
75
+ return num / den;
76
+ }
77
+ } catch {
78
+ }
71
79
  return void 0;
72
80
  }
73
81
  async function safeDuration(libav, fmt_ctx) {
@@ -185,5 +193,5 @@ function ffmpegToAvbridgeAudio(name) {
185
193
  }
186
194
 
187
195
  export { probeWithLibav };
188
- //# sourceMappingURL=avi-S7EY54YA.js.map
189
- //# sourceMappingURL=avi-S7EY54YA.js.map
196
+ //# sourceMappingURL=avi-GX2H34IQ.js.map
197
+ //# sourceMappingURL=avi-GX2H34IQ.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/probe/avi.ts"],"names":[],"mappings":";;;;AAuBA,eAAsB,cAAA,CACpB,QACA,OAAA,EACuB;AAKvB,EAAA,MAAM,KAAA,GAAS,MAAM,SAAA,CAAU,UAAU,CAAA;AAEzC,EAAA,MAAM,WAAW,MAAA,CAAO,IAAA,IAAQ,SAAS,OAAA,KAAY,SAAA,GAAY,QAAQ,OAAO,CAAA,CAAA;AAIhF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;AAAU,MAAA,OAAO,MAAA;AAAA,IACtB,KAAK,QAAA;AAAU,MAAA,OAAO,QAAA;AAAA,IACtB,KAAK,KAAA;AAAA,IACL,KAAK,KAAA;AAAU,MAAA,OAAO,KAAA;AAAA,IACtB,KAAK,QAAA;AAAA,IACL,KAAK,KAAA;AAAU,MAAA,OAAO,QAAA;AAAA,IACtB;AAAe,MAAA,OAAO,IAAA;AAAA;AAE1B","file":"avi-GX2H34IQ.js","sourcesContent":["import type {\n AudioCodec,\n AudioTrackInfo,\n ContainerKind,\n MediaContext,\n VideoCodec,\n VideoTrackInfo,\n} from \"../types.js\";\nimport type { NormalizedSource } from \"../util/source.js\";\nimport { prepareLibavInput, type LibavInputHandle } from \"../util/libav-http-reader.js\";\nimport { loadLibav } from \"../strategies/fallback/libav-loader.js\";\n\n/**\n * Probe AVI/ASF/FLV (and any other format mediabunny doesn't speak) via\n * libav.js. This module is `import()`-ed only when sniffing identifies one of\n * those containers.\n *\n * Critical: codec identification goes through `libav.avcodec_get_name(id)`\n * which returns the FFmpeg codec name as a string (e.g. \"h264\", \"mpeg4\",\n * \"wmv3\"). The numeric AV_CODEC_ID_* enum is *not* exposed on the libav\n * instance (only AVMEDIA_TYPE_*, AV_PIX_FMT_*, AV_SAMPLE_FMT_* and a handful\n * of others are), so comparing codec_ids against constants does not work.\n */\nexport async function probeWithLibav(\n source: NormalizedSource,\n sniffed: ContainerKind,\n): Promise<MediaContext> {\n // AVI/ASF/FLV demuxers are not in any libav.js npm variant — they live in\n // the custom \"avbridge\" build produced by `scripts/build-libav.sh`. The loader\n // emits an actionable error if the build hasn't been run yet. Threading\n // is OFF by default in `loadLibav` (see the comment there for why).\n const libav = (await loadLibav(\"avbridge\")) as unknown as LibavInstance;\n\n const filename = source.name ?? `input.${sniffed === \"unknown\" ? \"bin\" : sniffed}`;\n // For Blob/File sources we use libav's in-memory readahead file. For URL\n // sources we attach an HTTP block reader so libav demuxes via Range\n // requests instead of buffering the whole file.\n const handle: LibavInputHandle = await prepareLibavInput(libav as unknown as Parameters<typeof prepareLibavInput>[0], filename, source);\n\n let fmt_ctx: number | undefined;\n let streams: LibavStream[] = [];\n try {\n const result = await libav.ff_init_demuxer_file(filename);\n fmt_ctx = result[0];\n streams = result[1];\n } catch (err) {\n await handle.detach().catch(() => {});\n // Errors thrown across the libav.js worker/pthread boundary aren't\n // always Error instances — they can be plain objects, numbers (errno\n // codes), or strings. Stringify defensively so the user-facing message\n // never has `(undefined)` in it.\n const inner =\n err instanceof Error\n ? err.message\n : typeof err === \"object\" && err !== null\n ? JSON.stringify(err)\n : String(err);\n // eslint-disable-next-line no-console\n console.error(\"[avbridge] ff_init_demuxer_file raw error:\", err);\n throw new Error(\n `libav.js could not demux ${filename}. The current libav variant likely lacks the required demuxer (e.g. AVI). See vendor/libav/README.md for build instructions. (${inner || \"no message — see console for raw error\"})`,\n );\n }\n\n const videoTracks: VideoTrackInfo[] = [];\n const audioTracks: AudioTrackInfo[] = [];\n\n for (const stream of streams) {\n const codecName = (await safe(() => libav.avcodec_get_name(stream.codec_id))) ?? `unknown(${stream.codec_id})`;\n // codecpar holds width/height/channels/sample_rate/profile/level/extradata\n // for the actual stream. We have to copy it out of WASM memory.\n const codecpar = await safe(() => libav.ff_copyout_codecpar(stream.codecpar));\n\n if (stream.codec_type === libav.AVMEDIA_TYPE_VIDEO) {\n videoTracks.push({\n id: stream.index,\n codec: ffmpegToAvbridgeVideo(codecName),\n width: codecpar?.width ?? 0,\n height: codecpar?.height ?? 0,\n fps: await framerate(libav, stream),\n });\n } else if (stream.codec_type === libav.AVMEDIA_TYPE_AUDIO) {\n audioTracks.push({\n id: stream.index,\n codec: ffmpegToAvbridgeAudio(codecName),\n channels: codecpar?.channels ?? codecpar?.ch_layout_nb_channels ?? 0,\n sampleRate: codecpar?.sample_rate ?? 0,\n });\n }\n }\n\n // We need this duration but cannot reliably get it from the streams alone\n // for AVI; libav.js exposes it via the AVFormatContext duration helper.\n const duration = await safeDuration(libav, fmt_ctx!);\n\n // Close the demuxer; the strategy will reopen it later if it ends up being\n // chosen. Probing should not pin native resources.\n await libav.avformat_close_input_js(fmt_ctx!).catch(() => {});\n await handle.detach().catch(() => {});\n\n return {\n source: source.original,\n name: source.name,\n byteLength: source.byteLength,\n container: sniffed === \"unknown\" ? \"unknown\" : sniffed,\n videoTracks,\n audioTracks,\n subtitleTracks: [],\n probedBy: \"libav\",\n duration,\n };\n}\n\n/**\n * Read frame rate from the stream's `AVCodecParameters.framerate`.\n *\n * `avg_frame_rate` / `r_frame_rate` live on the AVStream in C but libav.js\n * doesn't expose them as JS properties on the stream record — only via\n * dedicated accessor functions we don't ship. `codecpar.framerate` IS\n * accessible via `AVCodecParameters_framerate_num/_den` and is populated\n * for most containers (for AVI it's parsed from `dwRate`/`dwScale` in the\n * stream header).\n *\n * Returns undefined if unavailable so the caller can fall back to a\n * container-appropriate default (currently 30 fps, which is wrong for\n * 25 fps PAL and 23.976 fps film content — hence the importance of\n * reading this correctly).\n */\nasync function framerate(\n libav: LibavInstance,\n stream: LibavStream,\n): Promise<number | undefined> {\n // The stream record may still carry these (new libav.js versions) —\n // prefer them when present.\n if (typeof stream.avg_frame_rate_num === \"number\" && stream.avg_frame_rate_den) {\n return stream.avg_frame_rate_num / stream.avg_frame_rate_den;\n }\n if (stream.avg_frame_rate && typeof stream.avg_frame_rate === \"object\") {\n if (stream.avg_frame_rate.den === 0) return undefined;\n return stream.avg_frame_rate.num / stream.avg_frame_rate.den;\n }\n try {\n const num = await libav.AVCodecParameters_framerate_num?.(stream.codecpar);\n const den = await libav.AVCodecParameters_framerate_den?.(stream.codecpar);\n if (typeof num === \"number\" && typeof den === \"number\" && den > 0 && num > 0) {\n return num / den;\n }\n } catch {\n // Fall through.\n }\n return undefined;\n}\n\nasync function safeDuration(libav: LibavInstance, fmt_ctx: number): Promise<number | undefined> {\n try {\n // `AVFormatContext.duration` is an int64 in microseconds (AV_TIME_BASE).\n // libav.js exposes it as a split lo/hi pair the same way it does for\n // packet pts — `AVFormatContext_duration(ctx)` returns the low 32 bits,\n // `AVFormatContext_durationhi(ctx)` returns the high 32 bits. Reading\n // only the low half (the previous bug) gave garbage for any file whose\n // duration > ~35 minutes, and zero for shorter files where the value\n // happened to live in the high half.\n const lo = await libav.AVFormatContext_duration?.(fmt_ctx);\n const hi = await libav.AVFormatContext_durationhi?.(fmt_ctx);\n if (typeof lo !== \"number\" || typeof hi !== \"number\") return undefined;\n\n // AV_NOPTS_VALUE = -2^63 → ptshi = -2147483648, pts = 0. Means \"unknown\".\n if (hi === -2147483648 && lo === 0) return undefined;\n\n // Reconstruct the 64-bit value. Prefer libav's helper when available\n // because it correctly handles signed 32-bit two's complement.\n const us =\n typeof libav.i64tof64 === \"function\"\n ? libav.i64tof64(lo, hi)\n : hi * 0x100000000 + lo + (lo < 0 ? 0x100000000 : 0);\n\n if (!Number.isFinite(us) || us <= 0) return undefined;\n return us / 1_000_000;\n } catch {\n return undefined;\n }\n}\n\nasync function safe<T>(fn: () => Promise<T> | T): Promise<T | undefined> {\n try { return await fn(); } catch { return undefined; }\n}\n\n/** Map FFmpeg codec names to avbridge video codec identifiers. */\nfunction ffmpegToAvbridgeVideo(name: string): VideoCodec {\n switch (name) {\n case \"h264\": return \"h264\";\n case \"hevc\": return \"h265\";\n case \"vp8\": return \"vp8\";\n case \"vp9\": return \"vp9\";\n case \"av1\": return \"av1\";\n case \"mpeg4\": return \"mpeg4\"; // MPEG-4 Part 2 / DivX / Xvid\n case \"msmpeg4v1\":\n case \"msmpeg4v2\":\n case \"msmpeg4v3\": // a.k.a. DIV3\n return \"mpeg4\";\n case \"wmv1\":\n case \"wmv2\":\n case \"wmv3\":\n return \"wmv3\";\n case \"vc1\": return \"vc1\";\n case \"mpeg2video\": return \"mpeg2\";\n case \"mpeg1video\": return \"mpeg1\";\n case \"theora\": return \"theora\";\n case \"rv10\": return \"rv10\";\n case \"rv20\": return \"rv20\";\n case \"rv30\": return \"rv30\";\n case \"rv40\": return \"rv40\";\n case \"dvvideo\": return \"dv\"; // DV / DVCPRO (camcorder, MiniDV)\n case \"hq_hqa\": return \"hq_hqa\"; // Canopus HQ / HQA (Grass Valley)\n default: return name as VideoCodec;\n }\n}\n\nfunction ffmpegToAvbridgeAudio(name: string): AudioCodec {\n switch (name) {\n case \"aac\": return \"aac\";\n case \"mp3\":\n case \"mp3float\":\n return \"mp3\";\n case \"opus\": return \"opus\";\n case \"vorbis\": return \"vorbis\";\n case \"flac\": return \"flac\";\n case \"ac3\": return \"ac3\";\n case \"eac3\": return \"eac3\";\n case \"wmav1\":\n case \"wmav2\": return \"wmav2\";\n case \"wmapro\": return \"wmapro\";\n case \"alac\": return \"alac\";\n case \"cook\": return \"cook\";\n case \"ra_144\": return \"ra_144\";\n case \"ra_288\": return \"ra_288\";\n case \"sipr\": return \"sipr\";\n case \"atrac3\": return \"atrac3\";\n case \"dca\":\n case \"dts\": return \"dts\";\n case \"truehd\":\n case \"mlp\": return \"truehd\";\n default: return name as AudioCodec;\n }\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Minimal structural types for the slice of libav.js we touch.\n// ─────────────────────────────────────────────────────────────────────────────\n\ninterface LibavStream {\n index: number;\n codec_type: number;\n codec_id: number;\n codecpar: number;\n avg_frame_rate?: { num: number; den: number };\n avg_frame_rate_num?: number;\n avg_frame_rate_den?: number;\n}\n\ninterface LibavCodecpar {\n width?: number;\n height?: number;\n channels?: number;\n ch_layout_nb_channels?: number;\n sample_rate?: number;\n profile?: number;\n level?: number;\n}\n\ninterface LibavInstance {\n mkreadaheadfile(name: string, blob: Blob): Promise<void>;\n unlinkreadaheadfile(name: string): Promise<void>;\n ff_init_demuxer_file(name: string): Promise<[number, LibavStream[]]>;\n ff_copyout_codecpar(codecpar: number): Promise<LibavCodecpar>;\n avcodec_get_name(codec_id: number): Promise<string>;\n avformat_close_input_js(ctx: number): Promise<void>;\n AVFormatContext_duration?(ctx: number): Promise<number>;\n AVFormatContext_durationhi?(ctx: number): Promise<number>;\n i64tof64?(lo: number, hi: number): number;\n\n AVCodecParameters_framerate_num?(codecpar: number): Promise<number>;\n AVCodecParameters_framerate_den?(codecpar: number): Promise<number>;\n\n AVMEDIA_TYPE_VIDEO: number;\n AVMEDIA_TYPE_AUDIO: number;\n}\n"]}
@@ -185,7 +185,7 @@ async function probe(source, transport) {
185
185
  const hasUnknownCodec = result.videoTracks.some((t) => t.codec === "unknown") || result.audioTracks.some((t) => t.codec === "unknown");
186
186
  if (hasUnknownCodec) {
187
187
  try {
188
- const { probeWithLibav } = await import('./avi-NNHH4AAA.js');
188
+ const { probeWithLibav } = await import('./avi-BLIH7KKV.js');
189
189
  return await probeWithLibav(normalized, sniffed);
190
190
  } catch {
191
191
  return result;
@@ -198,7 +198,7 @@ async function probe(source, transport) {
198
198
  mediabunnyErr.message
199
199
  );
200
200
  try {
201
- const { probeWithLibav } = await import('./avi-NNHH4AAA.js');
201
+ const { probeWithLibav } = await import('./avi-BLIH7KKV.js');
202
202
  return await probeWithLibav(normalized, sniffed);
203
203
  } catch (libavErr) {
204
204
  const mbMsg = mediabunnyErr.message || String(mediabunnyErr);
@@ -212,7 +212,7 @@ async function probe(source, transport) {
212
212
  }
213
213
  }
214
214
  try {
215
- const { probeWithLibav } = await import('./avi-NNHH4AAA.js');
215
+ const { probeWithLibav } = await import('./avi-BLIH7KKV.js');
216
216
  return await probeWithLibav(normalized, sniffed);
217
217
  } catch (err) {
218
218
  const inner = err instanceof Error ? err.message : String(err);
@@ -233,5 +233,5 @@ async function probe(source, transport) {
233
233
  }
234
234
 
235
235
  export { avbridgeAudioToMediabunny, avbridgeVideoToMediabunny, buildMediabunnySourceFromInput, probe };
236
- //# sourceMappingURL=chunk-2LNXMGT6.js.map
237
- //# sourceMappingURL=chunk-2LNXMGT6.js.map
236
+ //# sourceMappingURL=chunk-5CX7BVVV.js.map
237
+ //# sourceMappingURL=chunk-5CX7BVVV.js.map