Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 108 additions & 9 deletions demo/pub/bbb.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Encode and verify the demo's pre-encoded SD rendition."""
"""Retime the demo's source, then encode and verify its pre-encoded SD rendition."""

import argparse
import json
import os
import subprocess
from fractions import Fraction
from pathlib import Path
Expand All @@ -17,7 +18,7 @@ def probe(path):
"-show_streams",
"-show_packets",
"-show_entries",
"stream=index,codec_type,codec_name,width,height,time_base,duration_ts:packet=stream_index,pts,duration,flags",
"stream=index,codec_type,codec_name,width,height,time_base,duration_ts:packet=stream_index,pts,dts,duration,flags",
"-of",
"json",
str(path),
Expand All @@ -26,11 +27,107 @@ def probe(path):
)


def packets(media, stream):
return [p for p in media["packets"] if p["stream_index"] == stream["index"]]


def video(media):
stream = next(s for s in media["streams"] if s["codec_type"] == "video")
# Fragmented BBB contains duplicate packets marked discard, not visible frames.
packets = [p for p in media["packets"] if p["stream_index"] == stream["index"] and "D" not in p["flags"]]
return stream, packets
return stream, packets(media, stream)


def strict(path, media):
# A muxer nudges a repeated timestamp by a tick, onto a clock too coarse for another muxer
# to tell apart, and a demuxer marks the repeat discard.
for stream in media["streams"]:
stream_packets = packets(media, stream)
assert all("D" not in p["flags"] for p in stream_packets), f"{path} stream {stream['index']} discards packets"
assert all(a["pts"] < b["pts"] for a, b in zip(stream_packets, stream_packets[1:])), (
f"{path} stream {stream['index']} timestamps do not strictly increase"
)


def retime(source, target):
"""Give each packet that repeats its predecessor's timestamp a slot of its own.

The first of the pair takes the frame slot missing just before it, if there is one;
otherwise the second moves halfway to the next packet, or to its stream's end.
"""
media = probe(source)
moves = {}
for stream in media["streams"]:
stream_packets = packets(media, stream)
assert all(p["pts"] == p["dts"] for p in stream_packets), "Retiming assumes no reordering"
pts = [p["pts"] for p in stream_packets]
moved = moves[stream["index"]] = {}
for index in range(1, len(pts)):
if pts[index] != pts[index - 1]:
continue
duration = stream_packets[index - 1]["duration"]
if index >= 2 and pts[index - 1] - pts[index - 2] == 2 * duration and index - 1 not in moved:
moved[index - 1] = (pts[index - 1] - duration, duration)
else:
end = pts[index + 1] if index + 1 < len(pts) else pts[index] + stream_packets[index]["duration"]
assert end - pts[index] >= 2, "No room to retime a repeated timestamp"
middle = pts[index] + (end - pts[index]) // 2
moved[index] = (middle, end - middle)
for index, (value, _) in moved.items():
print(f"stream {stream['index']} packet {index}: {pts[index]} -> {value} ({stream['time_base']})")
if not any(moves.values()):
print("Timestamps already strictly increase")
return

filters = []
for index, moved in moves.items():
ts, duration = "PTS", "DURATION"
for packet, (value, length) in sorted(moved.items(), reverse=True):
ts = f"if(eq(N\\,{packet})\\,{value}\\,{ts})"
duration = f"if(eq(N\\,{packet})\\,{length}\\,{duration})"
filters += [f"-bsf:{index}", f"setts=ts={ts}:duration={duration}"]
partial = target.with_suffix(".tmp.mp4")
Comment thread
pullfrog[bot] marked this conversation as resolved.
Resolved
# `just pub sync` uploads all of media/, so a rejected remux must not stay there.
try:
subprocess.run(
[
"ffmpeg",
"-hide_banner",
"-loglevel",
"warning",
"-nostdin",
"-y",
"-copyts",
"-i",
str(source),
"-map",
"0",
"-c",
"copy",
*filters,
"-f",
"mp4",
"-movflags",
"cmaf+separate_moof+delay_moov+skip_trailer",
"-frag_duration",
"1000",
str(partial),
],
check=True,
)
retimed = probe(partial)
strict(partial, retimed)
for stream in media["streams"]:
before = [p["pts"] for p in packets(media, stream)]
after = [p["pts"] for p in packets(retimed, stream)]
changed = {index: b for index, (a, b) in enumerate(zip(before, after)) if a != b}
expected = {index: value for index, (value, _) in moves[stream["index"]].items()}
assert len(before) == len(after) and changed == expected, "Retiming moved other packets"
assert [s["duration_ts"] for s in retimed["streams"]] == [s["duration_ts"] for s in media["streams"]], (
"Retiming changed a stream's duration"
)
os.replace(partial, target)
except BaseException:
partial.unlink(missing_ok=True)
raise


def check(source, target):
Expand All @@ -40,8 +137,8 @@ def check(source, target):
target_stream, target_packets = video(target_media)
assert len(target_media["streams"]) == 1, "SD must be video only"
assert (target_stream["codec_name"], target_stream["width"], target_stream["height"]) == ("h264", 640, 360)
for stream, packets in [(source_stream, source_packets), (target_stream, target_packets)]:
assert all(a["pts"] < b["pts"] for a, b in zip(packets, packets[1:])), "Non-increasing frame timestamps"
strict(source, source_media)
strict(target, target_media)
source_frames = [(p["pts"] * Fraction(source_stream["time_base"]), "K" in p["flags"]) for p in source_packets]
target_frames = [(p["pts"] * Fraction(target_stream["time_base"]), "K" in p["flags"]) for p in target_packets]
assert source_frames == target_frames, "SD frame timestamps/keyframes differ from source"
Expand Down Expand Up @@ -155,11 +252,13 @@ def encode(source, target):

if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("action", choices=["encode", "check"])
parser.add_argument("action", choices=["retime", "encode", "check"])
args = parser.parse_args()
source = Path("media/bbb.mp4")
target = Path("media/bbb-sd.mp4")
if args.action == "encode":
if args.action == "retime":
retime(source, source)
elif args.action == "encode":
encode(source, target)
else:
check(source, target)
7 changes: 6 additions & 1 deletion demo/pub/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ bbb url='http://localhost:4443' *args:
-map 0 -map 1:v -c copy -f mpegts -pes_payload_size 0 -muxdelay 0 - \
| cargo run --bin moq -- "${@:2}" --connect "{{ url }}" --broadcast bbb.hang import ts

# Retime the source's repeated timestamps in place, before encoding the SD rendition from it.
retime-bbb:
just download bbb
python3 bbb.py retime

# Encode the video-only SD rendition, preserving source timestamps and loop duration.
encode-bbb-sd:
just download bbb
python3 bbb.py encode

# Verify the hosted rendition's frames, keyframes, and three complete loops.
# Verify the hosted renditions' strictly increasing timestamps, frames, keyframes, and three complete loops.
check-bbb:
just download bbb
just download bbb-sd
Expand Down