File size: 10,054 Bytes
f2f99a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86323af
 
f2f99a3
 
 
 
 
 
 
 
 
 
 
 
86323af
f2f99a3
86323af
f2f99a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86323af
 
f2f99a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { useState } from 'react';

interface CodeBlockProps {
  lang: string;
  children: string;
}

function CodeBlock({ lang, children }: CodeBlockProps) {
  const [copied, setCopied] = useState(false);
  const copy = () => {
    navigator.clipboard.writeText(children.trim());
    setCopied(true);
    setTimeout(() => setCopied(false), 1500);
  };
  return (
    <div className="group relative">
      <div className="flex items-center justify-between rounded-t-lg bg-zinc-800/80 px-3 py-1.5">
        <span className="text-[10px] font-medium uppercase tracking-wider text-zinc-500">{lang}</span>
        <button
          onClick={copy}
          className="text-[10px] text-zinc-500 transition-colors hover:text-zinc-300"
        >
          {copied ? 'Copied!' : 'Copy'}
        </button>
      </div>
      <pre className="overflow-x-auto rounded-b-lg bg-zinc-900/80 p-3 text-xs leading-relaxed text-zinc-300 ring-1 ring-zinc-800/50">
        <code>{children.trim()}</code>
      </pre>
    </div>
  );
}

function Section({ title, children }: { title: string; children: React.ReactNode }) {
  return (
    <div className="space-y-3">
      <h3 className="text-sm font-semibold text-zinc-200">{title}</h3>
      {children}
    </div>
  );
}

function Endpoint({ method, path, desc }: { method: string; path: string; desc: string }) {
  const color = method === 'POST' ? 'text-amber-400 bg-amber-400/10' : 'text-emerald-400 bg-emerald-400/10';
  return (
    <div className="flex items-start gap-2">
      <span className={`mt-0.5 shrink-0 rounded px-1.5 py-0.5 text-[10px] font-bold ${color}`}>{method}</span>
      <div>
        <code className="text-xs font-medium text-zinc-200">{path}</code>
        <p className="mt-0.5 text-xs text-zinc-500">{desc}</p>
      </div>
    </div>
  );
}

function ParamTable({ params }: { params: { name: string; type: string; desc: string; required?: boolean }[] }) {
  return (
    <div className="overflow-hidden rounded-lg ring-1 ring-zinc-800/50">
      <table className="w-full text-xs">
        <thead>
          <tr className="border-b border-zinc-800/50 bg-zinc-900/50">
            <th className="px-3 py-1.5 text-left font-medium text-zinc-400">Parameter</th>
            <th className="px-3 py-1.5 text-left font-medium text-zinc-400">Type</th>
            <th className="px-3 py-1.5 text-left font-medium text-zinc-400">Description</th>
          </tr>
        </thead>
        <tbody>
          {params.map((p) => (
            <tr key={p.name} className="border-b border-zinc-800/30 last:border-0">
              <td className="px-3 py-1.5">
                <code className="text-zinc-200">{p.name}</code>
                {p.required && <span className="ml-1 text-[9px] text-red-400">*</span>}
              </td>
              <td className="px-3 py-1.5 text-zinc-500">{p.type}</td>
              <td className="px-3 py-1.5 text-zinc-400">{p.desc}</td>
            </tr>
          ))}
        </tbody>
      </table>
    </div>
  );
}

export default function ApiDocs() {
  return (
    <div className="space-y-8">
      {/* Core Library */}
      <Section title="Core Library (TypeScript)">
        <p className="text-xs text-zinc-400">
          The core engine is isomorphic β€” same code runs in the browser and on Node.js.
          It operates on raw Y-plane (luminance) buffers with no platform dependencies.
        </p>

        <div className="space-y-4">
          <div className="space-y-2">
            <h4 className="text-xs font-medium text-zinc-300">Embed a watermark</h4>
            <CodeBlock lang="typescript">{`
import { embedWatermark } from '@core/embedder';
import { getPreset } from '@core/presets';

const config = getPreset('moderate');
const payload = new Uint8Array([0xDE, 0xAD, 0xBE, 0xEF]);

const result = embedWatermark(yPlane, width, height, payload, secretKey, config);
// result.yPlane  β†’ Uint8Array (watermarked luminance)
// result.psnr    β†’ number (quality in dB, higher = less visible)
            `}</CodeBlock>
          </div>

          <div className="space-y-2">
            <h4 className="text-xs font-medium text-zinc-300">Detect a watermark</h4>
            <CodeBlock lang="typescript">{`
import { detectWatermarkMultiFrame } from '@core/detector';
import { getPreset } from '@core/presets';

const config = getPreset('moderate');
const result = detectWatermarkMultiFrame(yPlanes, width, height, secretKey, config);
// result.detected    β†’ boolean
// result.payload     β†’ Uint8Array | null (the 4-byte payload)
// result.confidence  β†’ number (0–1)
// result.tilesDecoded β†’ number
            `}</CodeBlock>
          </div>

          <div className="space-y-2">
            <h4 className="text-xs font-medium text-zinc-300">Auto-detect (tries all presets)</h4>
            <CodeBlock lang="typescript">{`
import { autoDetectMultiFrame } from '@core/detector';

const result = autoDetectMultiFrame(yPlanes, width, height, secretKey);
// result.presetUsed β†’ 'light' | 'moderate' | 'strong' | 'fortress' | null
            `}</CodeBlock>
          </div>
        </div>
      </Section>

      {/* HTTP API */}
      <Section title="HTTP API">
        <p className="text-xs text-zinc-400">
          Base URL: <code className="text-zinc-300">https://lightricks-ltmarx.hf.space</code> (HF Space) or <code className="text-zinc-300">http://localhost:7860</code> (local).
          For private Spaces, add <code className="text-zinc-300">Authorization: Bearer hf_YOUR_TOKEN</code> header.
        </p>

        <div className="space-y-4">
          <div className="space-y-2">
            <Endpoint method="POST" path="/api/embed" desc="Embed a watermark into a video" />
            <ParamTable params={[
              { name: 'videoBase64', type: 'string', desc: 'Base64-encoded video file', required: true },
              { name: 'key', type: 'string', desc: 'Secret key for embedding', required: true },
              { name: 'preset', type: 'string', desc: 'light | moderate | strong | fortress', required: true },
              { name: 'payload', type: 'string', desc: 'Hex string, up to 8 chars (32 bits)', required: true },
            ]} />
            <CodeBlock lang="bash">{`
curl -X POST https://lightricks-ltmarx.hf.space/api/embed \\
  -H "Content-Type: application/json" \\
  -H "Authorization: Bearer hf_YOUR_TOKEN" \\
  -d '{
    "videoBase64": "'$(base64 -i input.mp4)'",
    "key": "my-secret",
    "preset": "moderate",
    "payload": "DEADBEEF"
  }'
            `}</CodeBlock>
          </div>

          <div className="space-y-2">
            <Endpoint method="POST" path="/api/detect" desc="Detect and extract a watermark from a video" />
            <ParamTable params={[
              { name: 'videoBase64', type: 'string', desc: 'Base64-encoded video file', required: true },
              { name: 'key', type: 'string', desc: 'Secret key used during embedding', required: true },
              { name: 'preset', type: 'string', desc: 'Preset to try (omit to try all)', required: false },
              { name: 'frames', type: 'number', desc: 'Max frames to analyze (default: 10)', required: false },
            ]} />
            <CodeBlock lang="json">{`
{
  "detected": true,
  "payload": "DEADBEEF",
  "confidence": 0.97,
  "preset": "moderate",
  "tilesDecoded": 12,
  "tilesTotal": 16
}
            `}</CodeBlock>
          </div>

          <Endpoint method="GET" path="/api/health" desc="Health check β€” returns { status: 'ok' }" />
        </div>
      </Section>

      {/* CLI */}
      <Section title="CLI">
        <p className="text-xs text-zinc-400">
          Requires Node.js and FFmpeg installed locally.
        </p>

        <div className="space-y-2">
          <CodeBlock lang="bash">{`
# Embed a watermark
npx tsx server/cli.ts embed \\
  -i input.mp4 -o output.mp4 \\
  --key SECRET --preset moderate --payload DEADBEEF

# Detect a watermark (auto-tries all presets)
npx tsx server/cli.ts detect -i video.mp4 --key SECRET

# List available presets
npx tsx server/cli.ts presets
          `}</CodeBlock>
        </div>
      </Section>

      {/* Presets reference */}
      <Section title="Presets">
        <div className="overflow-hidden rounded-lg ring-1 ring-zinc-800/50">
          <table className="w-full text-xs">
            <thead>
              <tr className="border-b border-zinc-800/50 bg-zinc-900/50">
                <th className="px-3 py-1.5 text-left font-medium text-zinc-400">Preset</th>
                <th className="px-3 py-1.5 text-left font-medium text-zinc-400">Delta</th>
                <th className="px-3 py-1.5 text-left font-medium text-zinc-400">BCH</th>
                <th className="px-3 py-1.5 text-left font-medium text-zinc-400">Masking</th>
                <th className="px-3 py-1.5 text-left font-medium text-zinc-400">Use case</th>
              </tr>
            </thead>
            <tbody>
              {[
                { name: 'Light', delta: 50, bch: '(63,36,5)', mask: 'Yes', use: 'Near-invisible, mild compression' },
                { name: 'Moderate', delta: 62, bch: '(63,36,5)', mask: 'Yes', use: 'Near-invisible with perceptual masking' },
                { name: 'Strong', delta: 110, bch: '(63,36,5)', mask: 'Yes', use: 'More frequencies, handles rescaling' },
                { name: 'Fortress', delta: 150, bch: '(63,36,5)', mask: 'Yes', use: 'Maximum robustness' },
              ].map((p) => (
                <tr key={p.name} className="border-b border-zinc-800/30 last:border-0">
                  <td className="px-3 py-1.5 font-medium text-zinc-200">{p.name}</td>
                  <td className="px-3 py-1.5 tabular-nums text-zinc-400">{p.delta}</td>
                  <td className="px-3 py-1.5 font-mono text-zinc-400">{p.bch}</td>
                  <td className="px-3 py-1.5 text-zinc-400">{p.mask}</td>
                  <td className="px-3 py-1.5 text-zinc-400">{p.use}</td>
                </tr>
              ))}
            </tbody>
          </table>
        </div>
      </Section>
    </div>
  );
}