File: encdec

package info (click to toggle)
ffmpeg 7:4.1.6-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 81,568 kB
  • sloc: ansic: 1,047,552; asm: 96,235; sh: 8,285; makefile: 4,057; cpp: 2,398; perl: 986; objc: 851; lisp: 834; awk: 53; ruby: 51; python: 49
file content (443 lines) | stat: -rwxr-xr-x 11,152 bytes parent folder | download
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
#!/bin/bash

set -e

# test encoders and decoders

SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
TEST_NAME="$1"
[ ! -z $TEST_NAME ] || TEST_NAME=${BASH_SOURCE[0]##*/}
ENCDECLIST="$SCRIPT_DIR/${TEST_NAME}"_list.txt
TEST_DIR="$AUTOPKGTEST_TMP/test"

encode_file() {
    test=$1
    dir=$2
    mux=${test%%;*}
    tenc=${test##*;}
    type=${tenc%%=*}
    enc=${tenc##*=}
    codec=${enc%%:*}
    encoder=${enc##*:}
    file_name="$dir/$encoder.$mux"
    opts=
    # some codecs require extra options
    if [ "$codec" == "avui" ]; then
        opts="-s 720x486"
    elif [ "$codec" == "dnxhd" ]; then
        opts="-s 1280x720 -b:v 90M -pix_fmt yuv422p"
    elif [ "$codec" == "dvvideo" ]; then
        opts="-s 720x480 -pix_fmt yuv411p"
    elif [ "$codec" == "h261" ]; then
        opts="-s 176x144"
    elif [ "$codec" == "h263" ]; then
        opts="-s 128x96"
    elif [ "$codec" == "hevc" ]; then
        opts="-s 64x64"
    elif [ "$codec" == "roq" ]; then
        opts="-s 128x128"
    elif [ "$codec" == "xface" ]; then
        opts="-s 48x48"
    elif [ "$codec" == "g723_1" ]; then
        opts="-ar 8000 -b:a 6300"
    elif [ "$codec" == "gsm" ]; then
        opts="-ar 8000 -b:a 13000"
    elif [ "$codec" == "gsm_ms" ]; then
        opts="-ar 8000 -b:a 13000"
    elif [ "$codec" == "roq_dpcm" ]; then
        opts="-ar 22050"
    elif [ "$codec" == "s302m" ]; then
        opts="-ac 2"
    elif [ "$codec" == "vorbis" ]; then
        opts="-ac 2"
    fi
    src=
    if [ "$type" = "v" ]; then
        src=testsrc=s=32x32:d=0.1
    elif [ "$type" = "a" ]; then
        src=sine=d=0.1
    else
        # unknown codec type
        return 10
    fi
    echo -e "trying muxer '$mux' with '$type' encoder '$encoder' for codec '$codec'\n"
    ret=0
    CMD="ffmpeg -f lavfi -i $src -strict -2 $opts -c:$type $encoder -f $mux $file_name -y -hide_banner -nostdin"
    echo $CMD
    $CMD 2>&1 || ret=$?
    return $ret
}

probe_file() {
    file_name=$1
    test=$2
    mux=${test%%;*}
    tenc=${test##*;}
    enc=${tenc##*=}
    codec=${enc%%:*}

    # determine the codec/format
    ret=0
    info=$(ffprobe -hide_banner -strict -2 "$file_name" 2>&1) || ret=$?
    if [ "$ret" != 0 ]; then
        echo "probing failed: $ret"
        return "$ret"
    fi
    # file formats can be a comma-separated list, e.g. matroska,webm
    file_formats=$(echo "$info" | grep "Input #" | sed "s/.*Input #[0-9]*, \([^ ]*\), from.*/\1/")
    file_formats=${file_formats//,/ }
    # there can be multiple streams, thus multiple codecs
    file_codecs=$(echo "$info" | grep "Stream #" | sed "s/.*Stream #.*:.*:.*: \([^ ,]*\).*/\1/")
    same_format=
    same_codec=

    # test if the file has the correct codec/format
    for file_format in $file_formats; do
        if [ "$mux" == "$file_format" ]; then
            same_format="$file_format"
        fi
    done
    if [ ! "$same_format" ]; then
        # wrong format
        echo "file has the wrong format: $file_formats"
        return 10
    fi
    for file_codec in $file_codecs; do
        if [ "$codec" == "$file_codec" ]; then
            same_codec="$file_codec"
        fi
    done
    if [ ! "$same_codec" ]; then
        # wrong codec
        echo "file has the wrong codec: $file_codecs"
        return 11
    fi
    return 0
}

decode_file() {
    file_name=$1
    ret=0
    # The option '-t 1' is necessary, because the comfortnoise decoder never stops producing noise.
    CMD="ffmpeg -strict -2 -i $file_name -t 1 -c:v rawvideo -c:a pcm_s32le -f nut /dev/null -y -hide_banner -nostdin"
    echo $CMD
    $CMD 2>&1 || ret=$?
    return $ret
}

streamcopy_file() {
    file_name=$1
    test=$2
    mux=${test%%;*}
    file_name_copy="$file_name.copy.$mux"
    ret=0
    # The option '-t 1' is necessary, because the comfortnoise decoder never stops producing noise.
    CMD="ffmpeg -strict -2 -i $file_name -t 1 -c copy -f $mux $file_name_copy -y -hide_banner -nostdin"
    echo $CMD
    $CMD 2>&1 || ret=$?
    return $ret
}

list_formats() {
    match="$1"
    old_IFS=$IFS
    # Set the field seperator to a newline
    IFS="
"
    # Get the raw list
    lines=$(ffmpeg -hide_banner -formats | grep "^ $match")
    for line in $lines; do
        item=$(echo "$line" | sed "s/^ [D ][E ] \([^ ]*\) .*/\1/")
        item=${item%%,*}
        echo "$item"
    done
    IFS=$old_IFS
}

list_codecs() {
    what=$1
    match="$2"
    type="$3"
    old_IFS=$IFS
    # Set the field seperator to a newline
    IFS="
"
    # Get the raw list
    lines=$(ffmpeg -hide_banner -codecs | grep "^ $match$type")
    for line in $lines; do
        items=$(echo "$line" | grep "($what:")
        codec=$(echo "$line" | sed "s/^ [^ ]* \([^ ]*\) .*/\1/")
        if [ -z "$items" ]; then
            echo "${type,,}=$codec"
        else
            # de/encoder(s) name(s) differ from codec name
            items=$(echo "$items" | sed "s/.*($what: \([^)]*\) ).*/\1/")
            IFS=" "
            for item in $items; do
                echo "${type,,}=$codec:$item"
            done
            IFS="
"
        fi
    done
    IFS=$old_IFS
}

list_muxers() {
    muxs=$(list_formats ".E [^=]")
    echo $muxs
}

list_demuxers() {
    dems=$(list_formats "D. [^=]")
    echo $dems
}

list_video_encoders() {
    vencs=$(list_codecs encoders ".E" "V")
    echo $vencs
}

list_video_decoders() {
    vdecs=$(list_codecs decoders "D." "V")
    echo $vdecs
}

list_audio_encoders() {
    aencs=$(list_codecs encoders ".E" "A")
    echo $aencs
}

list_audio_decoders() {
    adecs=$(list_codecs decoders "D." "A")
    echo $adecs
}

read_tests() {
    filename=$1
    old_IFS=$IFS
    # Set the field seperator to a newline
    IFS="
"
    # Get the raw list
    lines=$(cat "$filename")
    for line in $lines; do
        muxer=${line%%;*}
        tencs=${line##*;}
        if [ "$tencs" ]; then
            IFS=" "
            for tenc in $tencs; do
                echo "$muxer;$tenc"
            done
            IFS="
"
        fi
    done
    IFS=$old_IFS
}

echo "TEST: $TEST_NAME"
echo -e "ADTTMP directory: $AUTOPKGTEST_TMP\n\n"

mkdir -p "$TEST_DIR"

ffmpeg -version 2>&1

echo -e "\n"

# determine the supported muxers and encoders
echo "muxers:"
muxs=$(list_muxers)
echo "$muxs"
echo -e "\n"

vencs=$(list_video_encoders)
aencs=$(list_audio_encoders)
echo "encoders:"
encs="$vencs $aencs"
echo "$encs"
echo -e "\n"

for mux in $muxs; do
    mux_tests=
    for tenc in $encs; do
        mux_tests="$mux_tests $mux;$tenc "
    done
    possible_tests="$possible_tests$mux_tests"
done

if [ -f $ENCDECLIST ]; then
    tests=$(read_tests $ENCDECLIST)
else
    # create the list of working tests if it doesn't exist
    update="true"
    tests=$possible_tests
fi

num_tests=$(echo $tests | wc -w)
echo -e "$num_tests tests\n"

crashes=
failures=
skipped=

num_test=0
num_success=0

for test in $tests; do
    num_test=$((num_test + 1))
    echo "Test $num_test:"
    streamcopy=${test##*|}
    if [ "$update" ]; then
        streamcopy=1
    fi
    test=${test%%|*}
    # skip the test, if the muxer/encoder is not available
    available=$(echo "$possible_tests" | grep " $test ") || true
    if [ ! "$available" ]; then
        skipped="${skipped}${test}\n"
        echo -e "SKIPPED: $test\n\n"
        continue
    fi
    mux=${test%%;*}
    tenc=${test##*;}
    ret=0
    # try encoding a file
    # this also sets the file_name variable
    encode_file $test $TEST_DIR || ret=$?
    echo -e "\n"
    if [ "$ret" != "0" ]; then
        errmsg="$test; encoding return code: $ret"
        if [ "$ret" -gt 128 ]; then
            crashes="${crashes}${errmsg}\n"
        else
            failures="${failures}${errmsg}\n"
        fi
        echo -e "\nFAILED: $errmsg\n\n"
        continue
    fi
    ret=0
    # test if the file has the correct format/codec
    err=$(probe_file "$file_name" $test) || ret=$?
    if [ "$ret" != "0" ]; then
        errmsg="$test; $err"
        if [ "$ret" -gt 128 ]; then
            crashes="${crashes}${errmsg}\n"
        else
            failures="${failures}${errmsg}\n"
        fi
        echo -e "\nFAILED: $errmsg\n\n"
        continue
    fi
    ret=0
    # test decoding the file
    decode_file "$file_name" || ret=$?
    if [ "$ret" != "0" ]; then
        errmsg="$test; decoding return code: $ret"
        if [ "$ret" -gt 128 ]; then
            crashes="${crashes}${errmsg}\n"
        else
            failures="${failures}${errmsg}\n"
        fi
        echo -e "\nFAILED: $errmsg\n\n"
        continue
    fi
    streamcopy_orig="$streamcopy"
    if [ "$streamcopy" == "1" ]; then
        ret=0
        # test streamcopying the file
        # this also sets the file_name_copy variable
        echo -e "\n"
        streamcopy_file "$file_name" "$test" || ret=$?
        if [ "$ret" != "0" ]; then
            errmsg="$test; streamcopy return code: $ret"
            if [ "$ret" -gt 128 ]; then
                crashes="${crashes}${errmsg}\n"
            else
                failures="${failures}${errmsg}\n"
            fi
            echo -e "\nFAILED: $errmsg\n\n"
            streamcopy=0
        fi
    fi
    if [ "$streamcopy" == "1" ]; then
        ret=0
        # test if the file has the correct format/codec
        err=$(probe_file "$file_name_copy" $test) || ret=$?
        if [ "$ret" != "0" ]; then
            errmsg="$test; streamcopy: $err"
            if [ "$ret" -gt 128 ]; then
                crashes="${crashes}${errmsg}\n"
            else
                failures="${failures}${errmsg}\n"
            fi
            echo -e "\nFAILED: $errmsg\n\n"
            streamcopy=0
        fi
    fi
    works=$([ "$streamcopy" = "1" ] && echo works || echo fails)
    if [ "$streamcopy_orig" = "$streamcopy" ]; then
        echo -e "\nSUCCESS: correctly created file with format '$mux' and codec '$tenc'; streamcopying $works\n\n"
        num_success=$((num_success + 1))
    fi
    if [ "$update" ]; then
        if [ "x$last_mux" != "x$mux" ]; then
            if [ "$last_mux" ]; then
                # newline
                echo "" >> $ENCDECLIST
            fi
            echo -n "$mux;" >> $ENCDECLIST
        fi
        last_mux=$mux
        echo -n " $tenc|$streamcopy" >> $ENCDECLIST
    fi
done


echo "done!"


ret=0

num_crashes=0
num_failures=0
num_skipped=0

if [ "$crashes" ]; then
    num_crashes=$(echo -e "$crashes" | wc -l)
    num_crashes=$((num_crashes - 1))
fi
if [ "$failures" ]; then
    num_failures=$(echo -e "$failures" | wc -l)
    num_failures=$((num_failures - 1))
fi
if [ "$skipped" ]; then
    num_skipped=$(echo -e "$skipped" | wc -l)
    num_skipped=$((num_skipped - 1))
fi

if [ "$num_crashes" != "0" ]; then
    ret=1
    echo -e "\n\ncrashes: $num_crashes\n"
    echo -e "$crashes"
fi

if [ "$num_failures" != "0" ]; then
    ret=1
    echo -e "\n\nfailures: $num_failures\n"
    echo -e "$failures"
fi

if [ "$num_skipped" != "0" ]; then
    echo -e "\n\nskipped: $num_skipped\n"
    echo -e "$skipped"
fi

if [ "$num_success" != "0" ]; then
    echo -e "\n\nsuccess: $num_success\n"
else
    ret=1
    echo -e "\n\nno single success: something is definitely wrong\n"
fi

exit $ret