-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomplete-decode
executable file
·473 lines (437 loc) · 10.2 KB
/
complete-decode
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#!/bin/bash -e
# Run through the entire decoding process.
# FIXME: Detect CX decoding automatically
# FIXME: Detect audio configuration automatically from the VBI
# FIXME: Do this with short PAL/NTSC CAV/CLV examples with -fsanitize={undefined,address}...
# FIXME: Clean up intermediate files (.map) once complete
# FIXME: Extract Teletext subtitles
# Catch runaway programs (especially ld-process-efm) using too much memory.
ulimit -d $((8192 * 1024))
usage () {
cat >&2 <<EOF
Usage: $0 [options] INPUT [OUTPUT]
$0 - Decode a LaserDisc image using ld-decode's tools
Options:
-p Disc has PAL video (default NTSC)
-A Disc has no analogue audio
-D Disc has no digital audio
-3 Disc has AC3 audio (and no right analogue channel)
-B Disc has bilingual analogue audio (default stereo)
-t Disc is missing CDDA timestamps (ld-process-efm -t)
-T Don't attempt to decode subtitles
-C Don't use ld-dropout-correct
-m Use ld-discmap
-V Do everything except final encoding step
-s FRAMES Skip initial frames (ld-decode -s FRAMES)
-f DECODER Select chroma decoder (ld-chroma-decoder -f DECODER)
-G GAIN Set chroma gain (ld-chroma-decoder --chroma-gain GAIN)
-P PHASE Set chroma phase (ld-chroma-decoder --chroma-phase PHASE)
-d Deinterlace before encoding (bwdif)
-5 Remove 3:2 pulldown before encoding (fieldmatch,decimate)
-q STRENGTH Denoise before encoding (hqdn3d=STRENGTH; typically 4)
-c CRF Set CRF for x264 (default 16)
-n Dry-run mode; show what will be done
INPUT may be an RF capture (.lds, .r8, .u8, .r16, .s16, .ldf, .raw.oga),
or an already-decoded TBC file (.tbc).
OUTPUT is the basename of the output files; if INPUT is an RF capture
"sample.lds", it will default to "out/sample", and if INPUT is a TBC
file it will default to its basename.
-p will be assumed if either filename contains _PAL_, -D will be assumed
if either filename contains _ANA_, and -3 will be assumed if either
filename contains _AC3_. As PAL discs can't have both analogue and
digital audio, PAL discs will be assumed to have digital audio unless -D
or _ANA_ is used.
EOF
exit 1
}
testsuitedir="$(dirname $(realpath $0))"
ac3=false
analogue=true
analogueL=false
analogueR=false
cc1=true
digital=true
notimestamps=false
dropouts=true
x264crf=16
deinterlace=false
pulldown=false
decoder=
chromagain=
chromaphase=
discmap=false
dryrun=false
standard=ntsc
denoise=
lddargs=()
noencode=false
while getopts "35ABCc:Ddf:G:mnpP:q:s:tTV" c; do
case "$c" in
3)
analogue=false
analogueL=true
analogueR=false
ac3=true
;;
5)
pulldown=true
;;
A)
analogue=false
;;
B)
analogue=false
analogueL=true
analogueR=true
;;
C)
dropouts=false
;;
c)
x264crf="$OPTARG"
;;
d)
deinterlace=true
;;
f)
decoder="$OPTARG"
;;
G)
chromagain="$OPTARG"
;;
D)
digital=false
;;
m)
discmap=true
;;
n)
dryrun=true
;;
P)
chromaphase="$OPTARG"
;;
p)
standard=pal
;;
q)
denoise="$OPTARG"
;;
s)
lddargs+=(-s "$OPTARG")
;;
t)
notimestamps=true
;;
T)
cc1=false
;;
V)
noencode=true
;;
\?)
usage
;;
esac
done
shift $(expr $OPTIND - 1)
# Detect input/output files
if [ "$#" -lt 1 -o "$#" -gt 2 ]; then
usage
fi
rffile=""
in=""
if [ "$#" -ge 1 ]; then
case "$1" in
*.lds|*.[ru]8|*.[rs]16|*.ldf|*.raw.oga)
rffile="$1"
out=$(dirname "$1")/out/$(echo "$1" | sed 's/\.\(lds\|[ru]8\|[rs]16\|ldf\|raw\.oga\)$//')
;;
*.tbc)
in=$(echo "$1" | sed 's/\.tbc$//')
out="$in"
;;
*)
usage
;;
esac
fi
if [ "$#" = 2 ]; then
out="$2"
fi
if [ -z "$in" ]; then
in="$out"
fi
# Detect audio modes based on options and filename
case "$rffile-$out" in
*_PAL_*)
standard=pal
;;
esac
case "$rffile-$out" in
*_ANA_*)
digital=false
;;
esac
case "$rffile-$out" in
*_AC3_*)
analogue=false
analogueL=true
analogueR=false
ac3=true
;;
esac
if $digital && [ "$standard" = pal ]; then
analogue=false
fi
if $ac3 && [ "$standard" = pal ]; then
echo >&2 "PAL can't have AC3"
exit 1
fi
# Check video options are sensible
if $deinterlace && $pulldown; then
echo >&2 "Deinterlace and 3:2 pulldown don't make sense together"
exit 1
fi
if $pulldown && [ "$standard" = pal ]; then
echo >&2 "3:2 pulldown doesn't make sense for 50 FPS video"
exit 1
fi
action () {
if $dryrun; then
echo >&2 "Would do:" "$@"
return
fi
echo >&2 ">>>" "$@"
case "$1" in
ld-decode)
"$@" 2>&1 | tee "$out".decode.log
;;
ld-process-efm)
"$@" 2>&1 | tee "$out".efm.log
;;
ld-discmap)
"$@" 2>&1 | tee "$out".discmap.log
;;
*)
"$@"
;;
esac
}
if [ -n "$rffile" -a ! -f "$in.tbc" ]; then
if ! ($analogue || $analogueL || $analogueR); then
lddargs+=(--daa)
fi
if ! $digital; then
lddargs+=(--noEFM)
fi
if $ac3; then
lddargs+=(--AC3)
fi
action ld-decode "--$standard" "${lddargs[@]}" "$rffile" "$in"
fi
# Parse audio parameters from the .json.
eval $(python3 -c '
import json
import sys
try:
with open(sys.argv[1]) as f:
j = json.load(f)
except FileNotFoundError:
print("analogue_params=\"-t unknown\"")
sys.exit(0)
analogue_params = [
"-t", "raw",
"-e", "signed" if j["pcmAudioParameters"]["isSigned"] else "unsigned",
"-L" if j["pcmAudioParameters"]["isLittleEndian"] else "-B",
"-b", str(j["pcmAudioParameters"]["bits"]),
"-r", str(j["pcmAudioParameters"]["sampleRate"]),
"-c", "2",
]
print("analogue_params=\"%s\"" % " ".join(analogue_params))
' "$in.tbc.json")
if [ ! -f "$out.vbi.json" ]; then
action ld-process-vbi -n --output-json "$out.vbi.json" "$in.tbc"
fi
if [ ! -f "$out.ffmetadata" ]; then
action ld-export-metadata --ffmetadata "$out.ffmetadata" "$out.vbi.json"
fi
# XXX Support CC2-4 (once ld-export-metadata does)
if [ ! -f "$out.cc1.scc" ]; then
action ld-export-metadata --closed-captions "$out.cc1.scc" "$out.vbi.json"
fi
# Only encode Closed Caption streams if there are any captions
if $cc1 && grep >/dev/null '^[0-9]' "$out.cc1.scc"; then
if [ ! -f "$out.cc1.srt" ]; then
action tt convert -i "$out.cc1.scc" -o "$out.cc1.srt"
fi
# We might have some CC data, but none that can be converted
if ! grep >/dev/null '^[0-9]' "$out.cc1.srt"; then
cc1=false
fi
else
cc1=false
fi
if $digital && [ ! -f "$out.digital.pcm" ]; then
efmcmd=(ld-process-efm)
if $notimestamps; then
efmcmd+=(-t)
fi
efmcmd+=("$in.efm" "$out.digital.pcm")
action "${efmcmd[@]}"
if grep >/dev/null 'Audio samples: 0$' "$out".efm.log; then
echo >&2 "EFM processing found no digital audio (may need -t?)"
exit 1
fi
fi
if $digital && [ ! -f "$out.digital.flac" ]; then
effects=()
# Assume the whole digital audio stream needs de-emphasis if
# ld-process-efm found more than a handful of preemphasised frames.
if [ -f "$out.efm.log" ]; then
preemp_frames=$( (grep 'Total Preemp Frames' "$out".efm.log || echo ':0') | sed -e 's/.*: *//' -e 'q')
if [ "$preemp_frames" -gt 10 ]; then
effects+=(deemph)
fi
fi
effects+=(stats)
action sox \
-t raw -e signed -b 16 -r 44100 -c 2 "$out".digital.pcm \
"$out".digital.flac \
"${effects[@]}"
fi
if $analogue && [ ! -f "$out.analogue.flac" ]; then
action sox \
$analogue_params "$in.pcm" \
"$out".analogue.flac \
stats
fi
if $analogueL && [ ! -f "$out.analogueL.flac" ]; then
action sox \
$analogue_params "$in.pcm" \
"$out".analogueL.flac \
remix 1 \
stats
fi
if $analogueR && [ ! -f "$out.analogueR.flac" ]; then
action sox \
$analogue_params "$in.pcm" \
"$out".analogueR.flac \
remix 2 \
stats
fi
# Look for a manually-added audio track (e.g. a separate audio capture, or an
# equivalent from a different release).
extraaudio=false
if [ -f "$out.extra.flac" ]; then
extraaudio=true
fi
prevtbc="$in.tbc"
if $discmap; then
if [ ! -f "$out.map" ]; then
action ld-discmap "$prevtbc" "$out".map
fi
prevtbc="$out".map
fi
if $noencode; then
exit 0
fi
if [ ! -f "$out.mkv" ]; then
if $dropouts; then
doccmd=(ld-dropout-correct --overcorrect)
doccmd+=(--output-json "$out".doc.json "$prevtbc" -)
else
doccmd=(cat "$prevtbc")
fi
chcmd=(ld-chroma-decoder)
if [ -n "$decoder" ]; then
chcmd+=(-f "$decoder")
elif [ "$standard" = pal ]; then
chcmd+=(-f transform3d)
else
chcmd+=(-f ntsc3d)
fi
if [ -n "$chromagain" ]; then
chcmd+=(--chroma-gain "$chromagain")
fi
if [ -n "$chromaphase" ]; then
chcmd+=(--chroma-phase "$chromaphase")
fi
chcmd+=(--chroma-nr 0 --luma-nr 0)
chcmd+=(--output-format y4m)
chcmd+=(--input-json "$prevtbc.json" - -)
ffcmd=(ffmpeg -f yuv4mpegpipe -i -)
streams=0
audiostreams=()
if $digital; then
ffcmd+=(-i "$out".digital.flac)
audiostreams+=($streams)
streams=$(($streams + 1))
fi
if $analogue; then
ffcmd+=(-i "$out".analogue.flac)
audiostreams+=($streams)
streams=$(($streams + 1))
fi
if $analogueL; then
ffcmd+=(-i "$out".analogueL.flac)
audiostreams+=($streams)
streams=$(($streams + 1))
fi
if $analogueR; then
ffcmd+=(-i "$out".analogueR.flac)
audiostreams+=($streams)
streams=$(($streams + 1))
fi
if $ac3; then
ffcmd+=(-i "$out".ac3)
audiostreams+=($streams)
streams=$(($streams + 1))
fi
if $extraaudio; then
ffcmd+=(-i "$out".extra.flac)
audiostreams+=($streams)
streams=$(($streams + 1))
fi
subtitlestreams=()
if $cc1; then
ffcmd+=(-i "$out".cc1.srt)
subtitlestreams+=($streams)
streams=$(($streams + 1))
fi
ffcmd+=(-i "$out".ffmetadata)
metadatastream=$streams
for i in $(seq 0 $streams); do
ffcmd+=(-map $i:0)
done
ffcmd+=(-map_metadata $metadatastream)
filters=""
if $deinterlace; then
filters="$filters,bwdif"
fi
if $pulldown; then
# XXX Using bwdif here produces 48 FPS output
filters="$filters,fieldmatch=combmatch=full,yadif=deint=interlaced,decimate"
fi
if [ -n "$denoise" ]; then
filters="$filters,hqdn3d=$denoise"
fi
if [ -n "$filters" ]; then
ffcmd+=(-filter:v "${filters#,}")
fi
# Use yuvj444p for full range and no chroma subsampling.
ffcmd+=(-pix_fmt yuvj444p -codec:v libx264 -crf "$x264crf")
if ! $deinterlace; then
ffcmd+=(-flags +ildct+ilme)
fi
for stream in "${audiostreams[@]}"; do
ffcmd+=(-codec:a:$stream copy)
done
for stream in "${subtitlestreams[@]}"; do
ffcmd+=(-codec:s:$stream copy)
done
ffcmd+=(-y "$out".mkv)
action "${doccmd[@]}" | \
action "${chcmd[@]}" | \
action "${ffcmd[@]}"
fi