aboutsummaryrefslogtreecommitdiffhomepage
path: root/TOOLS/idet.sh
blob: 46bb835832ce0c053182305f8b744028aba7e438 (plain)
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
#!/bin/sh

: "${MPV:=mpv}"
: "${ILDETECT_MPV:=$MPV}"
: "${ILDETECT_MPVFLAGS:=--start=35% --length=35}"
: "${ILDETECT_DRY_RUN:=}"
: "${ILDETECT_QUIET:=}"
: "${ILDETECT_RUN_INTERLACED_ONLY:=}"
: "${ILDETECT_FORCE_RUN:=}"

# This script uses ffmpeg's "idet" filter for interlace detection. In the
# long run this should replace ildetect.sh+ildetect.so.

# exit status:
# 0 progressive
# 1 telecine
# 2 interlaced
# 8 unknown
# 16 detect fail
# 17+ mpv's status | 16

testfun()
{
    $ILDETECT_MPV "$@" \
        --vf-add=lavfi="[idet]" --msg-level ffmpeg=v \
        --o= --vo=null --no-audio --untimed \
        $ILDETECT_MPVFLAGS \
        | { if [ -n "$ILDETECT_QUIET" ]; then cat; else tee /dev/stderr; fi } \
        | grep "Parsed_idet_0: Multi frame detection: "
}

judge()
{
    tff=0
    bff=0
    progressive=0
    undetermined=0
    while read -r _ _ _ _ _ _ tff1 _ bff1 _ progressive1 _ undetermined1 _; do
        case "$tff1$bff1$progressive1$undetermined1" in
            *[!0-9]*)
                printf >&2 'ERROR: Unrecognized idet output: %s\n' "$out"
                exit 16
                ;;
        esac
        tff=$((tff + tff1))
        bff=$((bff + bff1))
        progressive=$((progressive + progressive1))
        undetermined=$((undetermined + undetermined1))
    done <<EOF
$(testfun "$@" | sed 's/:/: /g')
EOF

    interlaced=$((bff + tff))
    determined=$((interlaced + progressive))

    if [ "$undetermined" -gt "$determined" ] || [ "$determined" -lt 250 ]; then
        echo >&2 "ERROR: Less than 50% or 250 frames are determined."
        [ -n "$ILDETECT_FORCE_RUN" ] || exit 8
        echo >&2 "Assuming interlacing."
        if [ "$tff" -gt $((bff * 10)) ]; then
            verdict="interlaced-tff"
        elif [ "$bff" -gt $((tff * 10)) ]; then
            verdict="interlaced-bff"
        else
            verdict="interlaced"
        fi
    elif [ $((interlaced * 20)) -gt "$progressive" ]; then
        # At least 5% of the frames are interlaced!
        if [ "$tff" -gt $((bff * 10)) ]; then
            verdict="interlaced-tff"
        elif [ "$bff" -gt $((tff * 10)) ]; then
            verdict="interlaced-bff"
        else
            echo >&2 "ERROR: Content is interlaced, but can't determine field order."
            [ -n "$ILDETECT_FORCE_RUN" ] || exit 8
            echo >&2 "Assuming interlacing with default field order."
            verdict="interlaced"
        fi
    else
        # Likely progressive
        verdict="progressive"
    fi

    printf '%s\n' "$verdict"
}

judge "$@" --vf-clr
case "$verdict" in
    progressive)
        [ -n "$ILDETECT_DRY_RUN" ] || \
            [ -n "$ILDETECT_RUN_INTERLACED_ONLY" ] || \
            $ILDETECT_MPV "$@"
        r=$?
        [ $r -eq 0 ] || exit $((r | 16))
        exit 0
        ;;
    interlaced-tff)
        judge "$@" --vf-clr --vf-pre=lavfi=\[setfield=tff\],pullup
        case "$verdict" in
            progressive)
                [ -n "$ILDETECT_DRY_RUN" ] || \
                    $ILDETECT_MPV "$@" --vf-pre=lavfi=\[setfield=tff\],pullup
                r=$?
                [ $r -eq 0 ] || exit $((r | 16))
                exit 1
                ;;
            *)
                [ -n "$ILDETECT_DRY_RUN" ] || \
                    $ILDETECT_MPV "$@" --vf-pre=lavfi=\[setfield=tff\],yadif
                r=$?
                [ $r -eq 0 ] || exit $((r | 16))
                exit 2
                ;;
        esac
        ;;
    interlaced-bff)
        judge "$@" --vf-clr --vf-pre=lavfi=\[setfield=bff\],pullup
        case "$verdict" in
            progressive)
                [ -n "$ILDETECT_DRY_RUN" ] || \
                    $ILDETECT_MPV "$@" --vf-pre=lavfi=\[setfield=bff\],pullup
                r=$?
                [ $r -eq 0 ] || exit $((r | 16))
                exit 1
                ;;
            *)
                [ -n "$ILDETECT_DRY_RUN" ] || \
                    $ILDETECT_MPV "$@" --vf-pre=lavfi=\[setfield=bff\],yadif
                r=$?
                [ $r -eq 0 ] || exit $((r | 16))
                exit 2
                ;;
        esac
        ;;
    interlaced)
        judge "$@" --vf-clr --vf-pre=pullup
        case "$verdict" in
            progressive)
                [ -n "$ILDETECT_DRY_RUN" ] || \
                    $ILDETECT_MPV "$@" --vf-pre=pullup
                r=$?
                [ $r -eq 0 ] || exit $((r | 16))
                exit 1
                ;;
            *)
                [ -n "$ILDETECT_DRY_RUN" ] || \
                    $ILDETECT_MPV "$@" --vf-pre=yadif
                r=$?
                [ $r -eq 0 ] || exit $((r | 16))
                exit 2
                ;;
        esac
        ;;
    *)
        echo >&2 "ERROR: Internal error."
        exit 16
        ;;
esac