diff options
author | Clément Bœsch <clement@stupeflix.com> | 2016-06-22 13:44:29 +0200 |
---|---|---|
committer | Clément Bœsch <clement@stupeflix.com> | 2016-06-22 13:44:34 +0200 |
commit | 5d48e4eafa6c4559683892b8638d10508125f3cf (patch) | |
tree | 0d5becbd08d6ae319497dd32cbe9b67f4670cc5e /libavutil/tests | |
parent | 989a614b707dcff8abdffe28dc24ec64a83b2837 (diff) | |
parent | a6a750c7ef240b72ce01e9653343a0ddf247d196 (diff) | |
download | ffmpeg-5d48e4eafa6c4559683892b8638d10508125f3cf.tar.gz |
Merge commit 'a6a750c7ef240b72ce01e9653343a0ddf247d196'
* commit 'a6a750c7ef240b72ce01e9653343a0ddf247d196':
tests: Move all test programs to a subdirectory
Merged-by: Clément Bœsch <clement@stupeflix.com>
Diffstat (limited to 'libavutil/tests')
45 files changed, 4658 insertions, 0 deletions
diff --git a/libavutil/tests/.gitignore b/libavutil/tests/.gitignore new file mode 100644 index 0000000000..c23c44b53b --- /dev/null +++ b/libavutil/tests/.gitignore @@ -0,0 +1,44 @@ +/adler32 +/aes +/atomic +/avstring +/base64 +/blowfish +/bprint +/camellia +/cast5 +/color_utils +/cpu +/crc +/des +/dict +/display +/error +/eval +/fifo +/file +/float_dsp +/hash +/hmac +/lfg +/lls +/log +/lzo +/md5 +/murmur3 +/opt +/parseutils +/pca +/pixdesc +/pixelutils +/random_seed +/rational +/ripemd +/sha +/sha512 +/softfloat +/tea +/tree +/twofish +/utf8 +/xtea diff --git a/libavutil/tests/adler32.c b/libavutil/tests/adler32.c new file mode 100644 index 0000000000..511bf1e401 --- /dev/null +++ b/libavutil/tests/adler32.c @@ -0,0 +1,53 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +// LCOV_EXCL_START +#include <string.h> + +#include "libavutil/log.h" +#include "libavutil/timer.h" +#include "libavutil/adler32.h" + +#define LEN 7001 + +static volatile int checksum; + +int main(int argc, char **argv) +{ + int i; + uint8_t data[LEN]; + + av_log_set_level(AV_LOG_DEBUG); + + for (i = 0; i < LEN; i++) + data[i] = ((i * i) >> 3) + 123 * i; + + if (argc > 1 && !strcmp(argv[1], "-t")) { + for (i = 0; i < 1000; i++) { + START_TIMER; + checksum = av_adler32_update(1, data, LEN); + STOP_TIMER("adler"); + } + } else { + checksum = av_adler32_update(1, data, LEN); + } + + av_log(NULL, AV_LOG_DEBUG, "%X (expected 50E6E508)\n", checksum); + return checksum == 0x50e6e508 ? 0 : 1; +} +// LCOV_EXCL_STOP diff --git a/libavutil/tests/aes.c b/libavutil/tests/aes.c new file mode 100644 index 0000000000..1291ad6633 --- /dev/null +++ b/libavutil/tests/aes.c @@ -0,0 +1,119 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +// LCOV_EXCL_START +#include <string.h> + +#include "libavutil/aes.h" +#include "libavutil/lfg.h" +#include "libavutil/log.h" +#include "libavutil/mem.h" + +int main(int argc, char **argv) +{ + int i, j; + struct AVAES *b; + static const uint8_t rkey[2][16] = { + { 0 }, + { 0x10, 0xa5, 0x88, 0x69, 0xd7, 0x4b, 0xe5, 0xa3, + 0x74, 0xcf, 0x86, 0x7c, 0xfb, 0x47, 0x38, 0x59 } + }; + static const uint8_t rpt[2][16] = { + { 0x6a, 0x84, 0x86, 0x7c, 0xd7, 0x7e, 0x12, 0xad, + 0x07, 0xea, 0x1b, 0xe8, 0x95, 0xc5, 0x3f, 0xa3 }, + { 0 } + }; + static const uint8_t rct[2][16] = { + { 0x73, 0x22, 0x81, 0xc0, 0xa0, 0xaa, 0xb8, 0xf7, + 0xa5, 0x4a, 0x0c, 0x67, 0xa0, 0xc4, 0x5e, 0xcf }, + { 0x6d, 0x25, 0x1e, 0x69, 0x44, 0xb0, 0x51, 0xe0, + 0x4e, 0xaa, 0x6f, 0xb4, 0xdb, 0xf7, 0x84, 0x65 } + }; + uint8_t pt[32]; + uint8_t temp[32]; + uint8_t iv[2][16]; + int err = 0; + + b = av_aes_alloc(); + if (!b) + return 1; + + av_log_set_level(AV_LOG_DEBUG); + + for (i = 0; i < 2; i++) { + av_aes_init(b, rkey[i], 128, 1); + av_aes_crypt(b, temp, rct[i], 1, NULL, 1); + for (j = 0; j < 16; j++) { + if (rpt[i][j] != temp[j]) { + av_log(NULL, AV_LOG_ERROR, "%d %02X %02X\n", + j, rpt[i][j], temp[j]); + err = 1; + } + } + } + av_free(b); + + if (argc > 1 && !strcmp(argv[1], "-t")) { + struct AVAES *ae, *ad; + AVLFG prng; + + ae = av_aes_alloc(); + ad = av_aes_alloc(); + + if (!ae || !ad) { + av_free(ae); + av_free(ad); + return 1; + } + + av_aes_init(ae, (const uint8_t*)"PI=3.141592654..", 128, 0); + av_aes_init(ad, (const uint8_t*)"PI=3.141592654..", 128, 1); + av_lfg_init(&prng, 1); + + for (i = 0; i < 10000; i++) { + for (j = 0; j < 32; j++) + pt[j] = av_lfg_get(&prng); + for (j = 0; j < 16; j++) + iv[0][j] = iv[1][j] = av_lfg_get(&prng); + { + START_TIMER; + av_aes_crypt(ae, temp, pt, 2, iv[0], 0); + if (!(i & (i - 1))) + av_log(NULL, AV_LOG_ERROR, "%02X %02X %02X %02X\n", + temp[0], temp[5], temp[10], temp[15]); + av_aes_crypt(ad, temp, temp, 2, iv[1], 1); + av_aes_crypt(ae, temp, pt, 2, NULL, 0); + if (!(i & (i - 1))) + av_log(NULL, AV_LOG_ERROR, "%02X %02X %02X %02X\n", + temp[0], temp[5], temp[10], temp[15]); + av_aes_crypt(ad, temp, temp, 2, NULL, 1); + STOP_TIMER("aes"); + } + for (j = 0; j < 16; j++) { + if (pt[j] != temp[j]) { + av_log(NULL, AV_LOG_ERROR, "%d %d %02X %02X\n", + i, j, pt[j], temp[j]); + } + } + } + av_free(ae); + av_free(ad); + } + return err; +} +// LCOV_EXCL_STOP diff --git a/libavutil/tests/atomic.c b/libavutil/tests/atomic.c new file mode 100644 index 0000000000..c92f220a7a --- /dev/null +++ b/libavutil/tests/atomic.c @@ -0,0 +1,34 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/atomic.h" +#include "libavutil/avassert.h" + +int main(void) +{ + volatile int val = 1; + int res; + + res = avpriv_atomic_int_add_and_fetch(&val, 1); + av_assert0(res == 2); + avpriv_atomic_int_set(&val, 3); + res = avpriv_atomic_int_get(&val); + av_assert0(res == 3); + + return 0; +} diff --git a/libavutil/tests/avstring.c b/libavutil/tests/avstring.c new file mode 100644 index 0000000000..1242b3fe8d --- /dev/null +++ b/libavutil/tests/avstring.c @@ -0,0 +1,83 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> + +#include "libavutil/common.h" +#include "libavutil/mem.h" +#include "libavutil/avstring.h" + +int main(void) +{ + int i; + char *fullpath; + static const char * const strings[] = { + "''", + "", + ":", + "\\", + "'", + " '' :", + " '' '' :", + "foo '' :", + "'foo'", + "foo ", + " ' foo ' ", + "foo\\", + "foo': blah:blah", + "foo\\: blah:blah", + "foo\'", + "'foo : ' :blahblah", + "\\ :blah", + " foo", + " foo ", + " foo \\ ", + "foo ':blah", + " foo bar : blahblah", + "\\f\\o\\o", + "'foo : \\ \\ ' : blahblah", + "'\\fo\\o:': blahblah", + "\\'fo\\o\\:': foo ' :blahblah" + }; + + printf("Testing av_get_token()\n"); + for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) { + const char *p = strings[i]; + char *q; + printf("|%s|", p); + q = av_get_token(&p, ":"); + printf(" -> |%s|", q); + printf(" + |%s|\n", p); + av_free(q); + } + + printf("Testing av_append_path_component()\n"); + #define TEST_APPEND_PATH_COMPONENT(path, component, expected) \ + fullpath = av_append_path_component((path), (component)); \ + printf("%s = %s\n", fullpath ? fullpath : "(null)", expected); \ + av_free(fullpath); + TEST_APPEND_PATH_COMPONENT(NULL, NULL, "(null)") + TEST_APPEND_PATH_COMPONENT("path", NULL, "path"); + TEST_APPEND_PATH_COMPONENT(NULL, "comp", "comp"); + TEST_APPEND_PATH_COMPONENT("path", "comp", "path/comp"); + TEST_APPEND_PATH_COMPONENT("path/", "comp", "path/comp"); + TEST_APPEND_PATH_COMPONENT("path", "/comp", "path/comp"); + TEST_APPEND_PATH_COMPONENT("path/", "/comp", "path/comp"); + TEST_APPEND_PATH_COMPONENT("path/path2/", "/comp/comp2", "path/path2/comp/comp2"); + return 0; +} diff --git a/libavutil/tests/base64.c b/libavutil/tests/base64.c new file mode 100644 index 0000000000..88fd55c220 --- /dev/null +++ b/libavutil/tests/base64.c @@ -0,0 +1,128 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +// LCOV_EXCL_START +#include <stdint.h> +#include <stdio.h> + +#include "libavutil/common.h" +#include "libavutil/base64.h" +#include "libavutil/timer.h" + +#define MAX_DATA_SIZE 1024 +#define MAX_ENCODED_SIZE 2048 + +static int test_encode_decode(const uint8_t *data, unsigned int data_size, + const char *encoded_ref) +{ + char encoded[MAX_ENCODED_SIZE]; + uint8_t data2[MAX_DATA_SIZE]; + int data2_size, max_data2_size = MAX_DATA_SIZE; + + if (!av_base64_encode(encoded, MAX_ENCODED_SIZE, data, data_size)) { + printf("Failed: cannot encode the input data\n"); + return 1; + } + if (encoded_ref && strcmp(encoded, encoded_ref)) { + printf("Failed: encoded string differs from reference\n" + "Encoded:\n%s\nReference:\n%s\n", encoded, encoded_ref); + return 1; + } + + if ((data2_size = av_base64_decode(data2, encoded, max_data2_size)) != data_size) { + printf("Failed: cannot decode the encoded string\n" + "Encoded:\n%s\n", encoded); + return 1; + } + if ((data2_size = av_base64_decode(data2, encoded, data_size)) != data_size) { + printf("Failed: cannot decode with minimal buffer\n" + "Encoded:\n%s\n", encoded); + return 1; + } + if (memcmp(data2, data, data_size)) { + printf("Failed: encoded/decoded data differs from original data\n"); + return 1; + } + if (av_base64_decode(NULL, encoded, 0) != 0) { + printf("Failed: decode to NULL buffer\n"); + return 1; + } + if (strlen(encoded)) { + char *end = strchr(encoded, '='); + if (!end) + end = encoded + strlen(encoded) - 1; + *end = '%'; + if (av_base64_decode(NULL, encoded, 0) >= 0) { + printf("Failed: error detection\n"); + return 1; + } + } + + printf("Passed!\n"); + return 0; +} + +int main(int argc, char ** argv) +{ + int i, error_count = 0; + struct test { + const uint8_t *data; + const char *encoded_ref; + } tests[] = { + { "", ""}, + { "1", "MQ=="}, + { "22", "MjI="}, + { "333", "MzMz"}, + { "4444", "NDQ0NA=="}, + { "55555", "NTU1NTU="}, + { "666666", "NjY2NjY2"}, + { "abc:def", "YWJjOmRlZg=="}, + }; + char in[1024], out[2048]; + + printf("Encoding/decoding tests\n"); + for (i = 0; i < FF_ARRAY_ELEMS(tests); i++) + error_count += test_encode_decode(tests[i].data, strlen(tests[i].data), tests[i].encoded_ref); + + if (argc>1 && !strcmp(argv[1], "-t")) { + memset(in, 123, sizeof(in)); + for(i=0; i<10000; i++){ + START_TIMER + av_base64_encode(out, sizeof(out), in, sizeof(in)); + STOP_TIMER("encode") + } + for(i=0; i<10000; i++){ + START_TIMER + av_base64_decode(in, out, sizeof(in)); + STOP_TIMER("decode") + } + + for(i=0; i<10000; i++){ + START_TIMER + av_base64_decode(NULL, out, 0); + STOP_TIMER("syntax check") + } + } + + if (error_count) + printf("Error Count: %d.\n", error_count); + + return !!error_count; +} + +// LCOV_EXCL_STOP diff --git a/libavutil/tests/blowfish.c b/libavutil/tests/blowfish.c new file mode 100644 index 0000000000..2c2e004830 --- /dev/null +++ b/libavutil/tests/blowfish.c @@ -0,0 +1,190 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "libavutil/blowfish.h" + +#define NUM_VARIABLE_KEY_TESTS 34 + +/* plaintext bytes -- left halves */ +static const uint32_t plaintext_l[NUM_VARIABLE_KEY_TESTS] = { + 0x00000000, 0xFFFFFFFF, 0x10000000, 0x11111111, 0x11111111, + 0x01234567, 0x00000000, 0x01234567, 0x01A1D6D0, 0x5CD54CA8, + 0x0248D438, 0x51454B58, 0x42FD4430, 0x059B5E08, 0x0756D8E0, + 0x762514B8, 0x3BDD1190, 0x26955F68, 0x164D5E40, 0x6B056E18, + 0x004BD6EF, 0x480D3900, 0x437540C8, 0x072D43A0, 0x02FE5577, + 0x1D9D5C50, 0x30553228, 0x01234567, 0x01234567, 0x01234567, + 0xFFFFFFFF, 0x00000000, 0x00000000, 0xFFFFFFFF +}; + +/* plaintext bytes -- right halves */ +static const uint32_t plaintext_r[NUM_VARIABLE_KEY_TESTS] = { + 0x00000000, 0xFFFFFFFF, 0x00000001, 0x11111111, 0x11111111, + 0x89ABCDEF, 0x00000000, 0x89ABCDEF, 0x39776742, 0x3DEF57DA, + 0x06F67172, 0x2DDF440A, 0x59577FA2, 0x51CF143A, 0x774761D2, + 0x29BF486A, 0x49372802, 0x35AF609A, 0x4F275232, 0x759F5CCA, + 0x09176062, 0x6EE762F2, 0x698F3CFA, 0x77075292, 0x8117F12A, + 0x18F728C2, 0x6D6F295A, 0x89ABCDEF, 0x89ABCDEF, 0x89ABCDEF, + 0xFFFFFFFF, 0x00000000, 0x00000000, 0xFFFFFFFF +}; + +/* key bytes for variable key tests */ +static const uint8_t variable_key[NUM_VARIABLE_KEY_TESTS][8] = { + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, + { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 }, + { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, + { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 }, + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }, + { 0x7C, 0xA1, 0x10, 0x45, 0x4A, 0x1A, 0x6E, 0x57 }, + { 0x01, 0x31, 0xD9, 0x61, 0x9D, 0xC1, 0x37, 0x6E }, + { 0x07, 0xA1, 0x13, 0x3E, 0x4A, 0x0B, 0x26, 0x86 }, + { 0x38, 0x49, 0x67, 0x4C, 0x26, 0x02, 0x31, 0x9E }, + { 0x04, 0xB9, 0x15, 0xBA, 0x43, 0xFE, 0xB5, 0xB6 }, + { 0x01, 0x13, 0xB9, 0x70, 0xFD, 0x34, 0xF2, 0xCE }, + { 0x01, 0x70, 0xF1, 0x75, 0x46, 0x8F, 0xB5, 0xE6 }, + { 0x43, 0x29, 0x7F, 0xAD, 0x38, 0xE3, 0x73, 0xFE }, + { 0x07, 0xA7, 0x13, 0x70, 0x45, 0xDA, 0x2A, 0x16 }, + { 0x04, 0x68, 0x91, 0x04, 0xC2, 0xFD, 0x3B, 0x2F }, + { 0x37, 0xD0, 0x6B, 0xB5, 0x16, 0xCB, 0x75, 0x46 }, + { 0x1F, 0x08, 0x26, 0x0D, 0x1A, 0xC2, 0x46, 0x5E }, + { 0x58, 0x40, 0x23, 0x64, 0x1A, 0xBA, 0x61, 0x76 }, + { 0x02, 0x58, 0x16, 0x16, 0x46, 0x29, 0xB0, 0x07 }, + { 0x49, 0x79, 0x3E, 0xBC, 0x79, 0xB3, 0x25, 0x8F }, + { 0x4F, 0xB0, 0x5E, 0x15, 0x15, 0xAB, 0x73, 0xA7 }, + { 0x49, 0xE9, 0x5D, 0x6D, 0x4C, 0xA2, 0x29, 0xBF }, + { 0x01, 0x83, 0x10, 0xDC, 0x40, 0x9B, 0x26, 0xD6 }, + { 0x1C, 0x58, 0x7F, 0x1C, 0x13, 0x92, 0x4F, 0xEF }, + { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }, + { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }, + { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE }, + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, + { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, + { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 } +}; + +/* ciphertext bytes -- left halves */ +static const uint32_t ciphertext_l[NUM_VARIABLE_KEY_TESTS] = { + 0x4EF99745, 0x51866FD5, 0x7D856F9A, 0x2466DD87, 0x61F9C380, + 0x7D0CC630, 0x4EF99745, 0x0ACEAB0F, 0x59C68245, 0xB1B8CC0B, + 0x1730E577, 0xA25E7856, 0x353882B1, 0x48F4D088, 0x432193B7, + 0x13F04154, 0x2EEDDA93, 0xD887E039, 0x5F99D04F, 0x4A057A3B, + 0x452031C1, 0x7555AE39, 0x53C55F9C, 0x7A8E7BFA, 0xCF9C5D7A, + 0xD1ABB290, 0x55CB3774, 0xFA34EC48, 0xA7907951, 0xC39E072D, + 0x014933E0, 0xF21E9A77, 0x24594688, 0x6B5C5A9C +}; + +/* ciphertext bytes -- right halves */ +static const uint32_t ciphertext_r[NUM_VARIABLE_KEY_TESTS] = { + 0x6198DD78, 0xB85ECB8A, 0x613063F2, 0x8B963C9D, 0x2281B096, + 0xAFDA1EC7, 0x6198DD78, 0xC6A0A28D, 0xEB05282B, 0x250F09A0, + 0x8BEA1DA4, 0xCF2651EB, 0x09CE8F1A, 0x4C379918, 0x8951FC98, + 0xD69D1AE5, 0xFFD39C79, 0x3C2DA6E3, 0x5B163969, 0x24D3977B, + 0xE4FADA8E, 0xF59B87BD, 0xB49FC019, 0x937E89A3, 0x4986ADB5, + 0x658BC778, 0xD13EF201, 0x47B268B2, 0x08EA3CAE, 0x9FAC631D, + 0xCDAFF6E4, 0xB71C49BC, 0x5754369A, 0x5D9E0A5A +}; + +/* plaintext bytes */ +static const uint8_t plaintext[8] = "BLOWFISH"; + +static const uint8_t plaintext2[16] = "BLOWFISHBLOWFISH"; + +/* ciphertext bytes */ +static const uint8_t ciphertext[8] = { + 0x32, 0x4E, 0xD0, 0xFE, 0xF4, 0x13, 0xA2, 0x03 +}; + +static const uint8_t ciphertext2[16] = { + 0x53, 0x00, 0x40, 0x06, 0x63, 0xf2, 0x1d, 0x99, + 0x3b, 0x9b, 0x27, 0x64, 0x46, 0xfd, 0x20, 0xc1, +}; + +#define IV "blowfish" + +static void test_blowfish(AVBlowfish *ctx, uint8_t *dst, const uint8_t *src, + const uint8_t *ref, int len, uint8_t *iv, int dir, + const char *test) +{ + av_blowfish_crypt(ctx, dst, src, len, iv, dir); + if (memcmp(dst, ref, 8*len)) { + int i; + printf("%s failed\ngot ", test); + for (i = 0; i < 8*len; i++) + printf("%02x ", dst[i]); + printf("\nexpected "); + for (i = 0; i < 8*len; i++) + printf("%02x ", ref[i]); + printf("\n"); + exit(1); + } +} + +int main(void) +{ + AVBlowfish ctx; + uint32_t tmptext_l[NUM_VARIABLE_KEY_TESTS]; + uint32_t tmptext_r[NUM_VARIABLE_KEY_TESTS]; + uint8_t tmp[16], iv[8]; + int i; + + av_blowfish_init(&ctx, "abcdefghijklmnopqrstuvwxyz", 26); + + test_blowfish(&ctx, tmp, plaintext, ciphertext, 1, NULL, 0, "encryption"); + test_blowfish(&ctx, tmp, ciphertext, plaintext, 1, NULL, 1, "decryption"); + test_blowfish(&ctx, tmp, tmp, ciphertext, 1, NULL, 0, "Inplace encryption"); + test_blowfish(&ctx, tmp, tmp, plaintext, 1, NULL, 1, "Inplace decryption"); + memcpy(iv, IV, 8); + test_blowfish(&ctx, tmp, plaintext2, ciphertext2, 2, iv, 0, "CBC encryption"); + memcpy(iv, IV, 8); + test_blowfish(&ctx, tmp, ciphertext2, plaintext2, 2, iv, 1, "CBC decryption"); + memcpy(iv, IV, 8); + test_blowfish(&ctx, tmp, tmp, ciphertext2, 2, iv, 0, "Inplace CBC encryption"); + memcpy(iv, IV, 8); + test_blowfish(&ctx, tmp, tmp, plaintext2, 2, iv, 1, "Inplace CBC decryption"); + + memcpy(tmptext_l, plaintext_l, sizeof(*plaintext_l) * NUM_VARIABLE_KEY_TESTS); + memcpy(tmptext_r, plaintext_r, sizeof(*plaintext_r) * NUM_VARIABLE_KEY_TESTS); + + for (i = 0; i < NUM_VARIABLE_KEY_TESTS; i++) { + av_blowfish_init(&ctx, variable_key[i], 8); + + av_blowfish_crypt_ecb(&ctx, &tmptext_l[i], &tmptext_r[i], 0); + if (tmptext_l[i] != ciphertext_l[i] || tmptext_r[i] != ciphertext_r[i]) { + printf("Test encryption failed.\n"); + return 1; + } + + av_blowfish_crypt_ecb(&ctx, &tmptext_l[i], &tmptext_r[i], 1); + if (tmptext_l[i] != plaintext_l[i] || tmptext_r[i] != plaintext_r[i]) { + printf("Test decryption failed.\n"); + return 1; + } + } + printf("Test encryption/decryption success.\n"); + + return 0; +} + diff --git a/libavutil/tests/bprint.c b/libavutil/tests/bprint.c new file mode 100644 index 0000000000..d7f381dd52 --- /dev/null +++ b/libavutil/tests/bprint.c @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2012 Nicolas George + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/bprint.c" + +#undef printf + +static void bprint_pascal(AVBPrint *b, unsigned size) +{ + unsigned i, j; + unsigned p[42]; + + av_assert0(size < FF_ARRAY_ELEMS(p)); + + p[0] = 1; + av_bprintf(b, "%8d\n", 1); + for (i = 1; i <= size; i++) { + p[i] = 1; + for (j = i - 1; j > 0; j--) + p[j] = p[j] + p[j - 1]; + for (j = 0; j <= i; j++) + av_bprintf(b, "%8d", p[j]); + av_bprintf(b, "\n"); + } +} + +int main(void) +{ + AVBPrint b; + char buf[256]; + struct tm testtime = { .tm_year = 100, .tm_mon = 11, .tm_mday = 20 }; + + av_bprint_init(&b, 0, -1); + bprint_pascal(&b, 5); + printf("Short text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len); + printf("%s\n", b.str); + av_bprint_finalize(&b, NULL); + + av_bprint_init(&b, 0, -1); + bprint_pascal(&b, 25); + printf("Long text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len); + av_bprint_finalize(&b, NULL); + + av_bprint_init(&b, 0, 2048); + bprint_pascal(&b, 25); + printf("Long text in limited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len); + av_bprint_finalize(&b, NULL); + + av_bprint_init(&b, 0, 1); + bprint_pascal(&b, 5); + printf("Short text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str), b.len); + + av_bprint_init(&b, 0, 1); + bprint_pascal(&b, 25); + printf("Long text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str)/8*8, b.len); + /* Note that the size of the automatic buffer is arch-dependent. */ + + av_bprint_init(&b, 0, 0); + bprint_pascal(&b, 25); + printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(b.str), b.len); + + av_bprint_init_for_buffer(&b, buf, sizeof(buf)); + bprint_pascal(&b, 25); + printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(buf), b.len); + + av_bprint_init(&b, 0, -1); + av_bprint_strftime(&b, "%Y-%m-%d", &testtime); + printf("strftime full: %u/%u \"%s\"\n", (unsigned)strlen(buf), b.len, b.str); + av_bprint_finalize(&b, NULL); + + av_bprint_init(&b, 0, 8); + av_bprint_strftime(&b, "%Y-%m-%d", &testtime); + printf("strftime truncated: %u/%u \"%s\"\n", (unsigned)strlen(buf), b.len, b.str); + + return 0; +} diff --git a/libavutil/tests/camellia.c b/libavutil/tests/camellia.c new file mode 100644 index 0000000000..1716b59a38 --- /dev/null +++ b/libavutil/tests/camellia.c @@ -0,0 +1,75 @@ +/* + * An implementation of the CAMELLIA algorithm as mentioned in RFC3713 + * Copyright (c) 2014 Supraja Meedinti + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/camellia.h" +#include "libavutil/log.h" + +int main(int argc, char *argv[]) +{ + const uint8_t Key[3][32] = { + {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}, + {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}, + {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff} + }; + const uint8_t rct[3][16] = { + {0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73, 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43}, + {0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9,0x96, 0xf8, 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9}, + {0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c, 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09} + }; + const uint8_t rpt[32] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}; + const int kbits[3] = {128, 192, 256}; + int i, j, err = 0; + uint8_t temp[32], iv[16]; + struct AVCAMELLIA *cs; + cs = av_camellia_alloc(); + if (!cs) + return 1; + for (j = 0; j < 3; j++) { + av_camellia_init(cs, Key[j], kbits[j]); + av_camellia_crypt(cs, temp, rpt, 1, NULL, 0); + for (i = 0; i < 16; i++) { + if (rct[j][i] != temp[i]) { + av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rct[j][i], temp[i]); + err = 1; + } + } + av_camellia_crypt(cs, temp, rct[j], 1, NULL, 1); + for (i = 0; i < 16; i++) { + if (rpt[i] != temp[i]) { + av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rpt[i], temp[i]); + err = 1; + } + } + } + av_camellia_init(cs, Key[0], 128); + memcpy(iv, "HALLO123HALLO123", 16); + av_camellia_crypt(cs, temp, rpt, 2, iv, 0); + memcpy(iv, "HALLO123HALLO123", 16); + av_camellia_crypt(cs, temp, temp, 2, iv, 1); + for (i = 0; i < 32; i++) { + if (rpt[i] != temp[i]) { + av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rpt[i], temp[i]); + err = 1; + } + } + av_free(cs); + return err; +} diff --git a/libavutil/tests/cast5.c b/libavutil/tests/cast5.c new file mode 100644 index 0000000000..ce3aa80b5b --- /dev/null +++ b/libavutil/tests/cast5.c @@ -0,0 +1,103 @@ +/* + * An implementation of the CAST128 algorithm as mentioned in RFC2144 + * Copyright (c) 2014 Supraja Meedinti + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/cast5.h" +#include "libavutil/log.h" + +int main(int argc, char** argv) +{ + + static const uint8_t Key[3][16] = { + {0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78, 0x23, 0x45, 0x67, 0x89, 0x34, 0x56, 0x78, 0x9a}, + {0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78, 0x23, 0x45}, + {0x01, 0x23, 0x45, 0x67, 0x12} + }; + static const uint8_t rpt[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}; + static const uint8_t rct[3][8] = { + {0x23, 0x8b, 0x4f, 0xe5, 0x84, 0x7e, 0x44, 0xb2}, + {0xeb, 0x6a, 0x71, 0x1a, 0x2c, 0x02, 0x27, 0x1b}, + {0x7a, 0xc8, 0x16, 0xd1, 0x6e, 0x9b, 0x30, 0x2e} + }; + static const uint8_t rct2[2][16] = { + {0xee, 0xa9, 0xd0, 0xa2, 0x49, 0xfd, 0x3b, 0xa6, 0xb3, 0x43, 0x6f, 0xb8, 0x9d, 0x6d, 0xca, 0x92}, + {0xb2, 0xc9, 0x5e, 0xb0, 0x0c, 0x31, 0xad, 0x71, 0x80, 0xac, 0x05, 0xb8, 0xe8, 0x3d, 0x69, 0x6e} + }; + static const uint8_t iv[8] = {0xee, 0xa9, 0xd0, 0xa2, 0x49, 0xfd, 0x3b, 0xa6}; + static uint8_t rpt2[2][16]; + int i, j, err = 0; + static const int key_bits[3] = {128, 80, 40}; + uint8_t temp[8]; + struct AVCAST5 *cs; + cs = av_cast5_alloc(); + if (!cs) + return 1; + for (j = 0; j < 3; j++){ + + av_cast5_init(cs, Key[j], key_bits[j]); + av_cast5_crypt(cs, temp, rpt, 1, 0); + for (i = 0;i < 8; i++){ + if (rct[j][i] != temp[i]){ + av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rct[j][i], temp[i]); + err = 1; + } + } + + av_cast5_crypt(cs, temp, rct[j], 1, 1); + for (i =0; i < 8; i++) { + if (rpt[i] != temp[i]) { + av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rpt[i], temp[i]); + err = 1; + } + } + } + memcpy(rpt2[0], Key[0], 16); + memcpy(rpt2[1], Key[0], 16); + for (i = 0; i < 1000000; i++){ + av_cast5_init(cs, rpt2[1], 128); + av_cast5_crypt(cs, rpt2[0], rpt2[0], 2, 0); + av_cast5_init(cs, rpt2[0], 128); + av_cast5_crypt(cs, rpt2[1], rpt2[1], 2, 0); + } + for (j = 0; j < 2; j++) { + for (i = 0; i < 16; i++) { + if (rct2[j][i] != rpt2[j][i]) { + av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rct2[j][i], rpt2[j][i]); + err = 1; + } + } + } + for (j = 0; j < 3; j++) { + + av_cast5_init(cs, Key[j], key_bits[j]); + memcpy(temp, iv, 8); + av_cast5_crypt2(cs, rpt2[0], rct2[0], 2, temp, 0); + memcpy(temp, iv, 8); + av_cast5_crypt2(cs, rpt2[0], rpt2[0], 2, temp, 1); + for (i = 0; i < 16; i++) { + if (rct2[0][i] != rpt2[0][i]) { + av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rct2[0][i], rpt2[0][i]); + err = 1; + } + } + } + av_free(cs); + return err; +} diff --git a/libavutil/tests/color_utils.c b/libavutil/tests/color_utils.c new file mode 100644 index 0000000000..ea0127158b --- /dev/null +++ b/libavutil/tests/color_utils.c @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2015 Kevin Wheatley <kevin.j.wheatley@gmail.com> + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/color_utils.c" + +int main(int argc, char *argv[]) +{ + int i, j; + static const double test_data[] = { + -0.1, -0.018053968510807, -0.01, -0.00449, 0.0, 0.00316227760, 0.005, + 0.009, 0.015, 0.1, 1.0, 52.37, 125.098765, 1999.11123, 6945.443, + 15123.4567, 19845.88923, 98678.4231, 99999.899998 + }; + + for(i = 0; i < AVCOL_TRC_NB; i++) { + avpriv_trc_function func = avpriv_get_trc_function_from_trc(i); + for(j = 0; j < FF_ARRAY_ELEMS(test_data); j++) { + if(func != NULL) { + double result = func(test_data[j]); + printf("AVColorTransferCharacteristic=%d calling func(%f) expected=%f\n", + i, test_data[j], result); + } + } + } + +} diff --git a/libavutil/tests/cpu.c b/libavutil/tests/cpu.c new file mode 100644 index 0000000000..f02a54cbbb --- /dev/null +++ b/libavutil/tests/cpu.c @@ -0,0 +1,147 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> + +#include "config.h" + +#include "libavutil/cpu.h" +#include "libavutil/avstring.h" + +#if HAVE_UNISTD_H +#include <unistd.h> +#endif +#if !HAVE_GETOPT +#include "compat/getopt.c" +#endif + +static const struct { + int flag; + const char *name; +} cpu_flag_tab[] = { +#if ARCH_AARCH64 + { AV_CPU_FLAG_ARMV8, "armv8" }, + { AV_CPU_FLAG_NEON, "neon" }, + { AV_CPU_FLAG_VFP, "vfp" }, +#elif ARCH_ARM + { AV_CPU_FLAG_ARMV5TE, "armv5te" }, + { AV_CPU_FLAG_ARMV6, "armv6" }, + { AV_CPU_FLAG_ARMV6T2, "armv6t2" }, + { AV_CPU_FLAG_VFP, "vfp" }, + { AV_CPU_FLAG_VFP_VM, "vfp_vm" }, + { AV_CPU_FLAG_VFPV3, "vfpv3" }, + { AV_CPU_FLAG_NEON, "neon" }, + { AV_CPU_FLAG_SETEND, "setend" }, +#elif ARCH_PPC + { AV_CPU_FLAG_ALTIVEC, "altivec" }, +#elif ARCH_X86 + { AV_CPU_FLAG_MMX, "mmx" }, + { AV_CPU_FLAG_MMXEXT, "mmxext" }, + { AV_CPU_FLAG_SSE, "sse" }, + { AV_CPU_FLAG_SSE2, "sse2" }, + { AV_CPU_FLAG_SSE2SLOW, "sse2slow" }, + { AV_CPU_FLAG_SSE3, "sse3" }, + { AV_CPU_FLAG_SSE3SLOW, "sse3slow" }, + { AV_CPU_FLAG_SSSE3, "ssse3" }, + { AV_CPU_FLAG_ATOM, "atom" }, + { AV_CPU_FLAG_SSE4, "sse4.1" }, + { AV_CPU_FLAG_SSE42, "sse4.2" }, + { AV_CPU_FLAG_AVX, "avx" }, + { AV_CPU_FLAG_AVXSLOW, "avxslow" }, + { AV_CPU_FLAG_XOP, "xop" }, + { AV_CPU_FLAG_FMA3, "fma3" }, + { AV_CPU_FLAG_FMA4, "fma4" }, + { AV_CPU_FLAG_3DNOW, "3dnow" }, + { AV_CPU_FLAG_3DNOWEXT, "3dnowext" }, + { AV_CPU_FLAG_CMOV, "cmov" }, + { AV_CPU_FLAG_AVX2, "avx2" }, + { AV_CPU_FLAG_BMI1, "bmi1" }, + { AV_CPU_FLAG_BMI2, "bmi2" }, + { AV_CPU_FLAG_AESNI, "aesni" }, +#endif + { 0 } +}; + +static void print_cpu_flags(int cpu_flags, const char *type) +{ + int i; + + printf("cpu_flags(%s) = 0x%08X\n", type, cpu_flags); + printf("cpu_flags_str(%s) =", type); + for (i = 0; cpu_flag_tab[i].flag; i++) + if (cpu_flags & cpu_flag_tab[i].flag) + printf(" %s", cpu_flag_tab[i].name); + printf("\n"); +} + + +int main(int argc, char **argv) +{ + int cpu_flags_raw = av_get_cpu_flags(); + int cpu_flags_eff; + int cpu_count = av_cpu_count(); + char threads[5] = "auto"; + int i; + + for(i = 0; cpu_flag_tab[i].flag; i++) { + unsigned tmp = 0; + if (av_parse_cpu_caps(&tmp, cpu_flag_tab[i].name) < 0) { + fprintf(stderr, "Table missing %s\n", cpu_flag_tab[i].name); + return 4; + } + } + + if (cpu_flags_raw < 0) + return 1; + + for (;;) { + int c = getopt(argc, argv, "c:t:"); + if (c == -1) + break; + switch (c) { + case 'c': + { + unsigned flags = av_get_cpu_flags(); + if (av_parse_cpu_caps(&flags, optarg) < 0) + return 2; + + av_force_cpu_flags(flags); + break; + } + case 't': + { + int len = av_strlcpy(threads, optarg, sizeof(threads)); + if (len >= sizeof(threads)) { + fprintf(stderr, "Invalid thread count '%s'\n", optarg); + return 2; + } + } + } + } + + cpu_flags_eff = av_get_cpu_flags(); + + if (cpu_flags_eff < 0) + return 3; + + print_cpu_flags(cpu_flags_raw, "raw"); + print_cpu_flags(cpu_flags_eff, "effective"); + printf("threads = %s (cpu_count = %d)\n", threads, cpu_count); + + return 0; +} diff --git a/libavutil/tests/crc.c b/libavutil/tests/crc.c new file mode 100644 index 0000000000..9825d6bec9 --- /dev/null +++ b/libavutil/tests/crc.c @@ -0,0 +1,46 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdint.h> +#include <stdio.h> + +#include "libavutil/crc.h" + +int main(void) +{ + uint8_t buf[1999]; + int i; + static const unsigned p[6][3] = { + { AV_CRC_32_IEEE_LE, 0xEDB88320, 0x3D5CDD04 }, + { AV_CRC_32_IEEE , 0x04C11DB7, 0xC0F5BAE0 }, + { AV_CRC_24_IEEE , 0x864CFB , 0xB704CE }, + { AV_CRC_16_ANSI_LE, 0xA001 , 0xBFD8 }, + { AV_CRC_16_ANSI , 0x8005 , 0x1FBB }, + { AV_CRC_8_ATM , 0x07 , 0xE3 } + }; + const AVCRC *ctx; + + for (i = 0; i < sizeof(buf); i++) + buf[i] = i + i * i; + + for (i = 0; i < 6; i++) { + ctx = av_crc_get_table(p[i][0]); + printf("crc %08X = %X\n", p[i][1], av_crc(ctx, 0, buf, sizeof(buf))); + } + return 0; +} diff --git a/libavutil/tests/des.c b/libavutil/tests/des.c new file mode 100644 index 0000000000..ed04fad519 --- /dev/null +++ b/libavutil/tests/des.c @@ -0,0 +1,128 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/des.c" + +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "libavutil/time.h" + +static uint64_t rand64(void) +{ + uint64_t r = rand(); + r = (r << 32) | rand(); + return r; +} + +static const uint8_t test_key[] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 }; +static const DECLARE_ALIGNED(8, uint8_t, plain)[] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }; +static const DECLARE_ALIGNED(8, uint8_t, crypt)[] = { 0x4a, 0xb6, 0x5b, 0x3d, 0x4b, 0x06, 0x15, 0x18 }; +static DECLARE_ALIGNED(8, uint8_t, tmp)[8]; +static DECLARE_ALIGNED(8, uint8_t, large_buffer)[10002][8]; +static const uint8_t cbc_key[] = { + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, + 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, + 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23 +}; + +static int run_test(int cbc, int decrypt) +{ + AVDES d; + int delay = cbc && !decrypt ? 2 : 1; + uint64_t res; + AV_WB64(large_buffer[0], 0x4e6f772069732074ULL); + AV_WB64(large_buffer[1], 0x1234567890abcdefULL); + AV_WB64(tmp, 0x1234567890abcdefULL); + av_des_init(&d, cbc_key, 192, decrypt); + av_des_crypt(&d, large_buffer[delay], large_buffer[0], 10000, cbc ? tmp : NULL, decrypt); + res = AV_RB64(large_buffer[9999 + delay]); + if (cbc) { + if (decrypt) + return res == 0xc5cecf63ecec514cULL; + else + return res == 0xcb191f85d1ed8439ULL; + } else { + if (decrypt) + return res == 0x8325397644091a0aULL; + else + return res == 0xdd17e8b8b437d232ULL; + } +} + +int main(void) +{ + AVDES d; + int i; + uint64_t key[3]; + uint64_t data; + uint64_t ct; + uint64_t roundkeys[16]; + srand(av_gettime()); + key[0] = AV_RB64(test_key); + data = AV_RB64(plain); + gen_roundkeys(roundkeys, key[0]); + if (des_encdec(data, roundkeys, 0) != AV_RB64(crypt)) { + printf("Test 1 failed\n"); + return 1; + } + av_des_init(&d, test_key, 64, 0); + av_des_crypt(&d, tmp, plain, 1, NULL, 0); + if (memcmp(tmp, crypt, sizeof(crypt))) { + printf("Public API decryption failed\n"); + return 1; + } + if (!run_test(0, 0) || !run_test(0, 1) || !run_test(1, 0) || !run_test(1, 1)) { + printf("Partial Monte-Carlo test failed\n"); + return 1; + } + for (i = 0; i < 1000; i++) { + key[0] = rand64(); + key[1] = rand64(); + key[2] = rand64(); + data = rand64(); + av_des_init(&d, (uint8_t *) key, 192, 0); + av_des_crypt(&d, (uint8_t *) &ct, (uint8_t *) &data, 1, NULL, 0); + av_des_init(&d, (uint8_t *) key, 192, 1); + av_des_crypt(&d, (uint8_t *) &ct, (uint8_t *) &ct, 1, NULL, 1); + if (ct != data) { + printf("Test 2 failed\n"); + return 1; + } + } +#ifdef GENTABLES + printf("static const uint32_t S_boxes_P_shuffle[8][64] = {\n"); + for (i = 0; i < 8; i++) { + int j; + printf(" {"); + for (j = 0; j < 64; j++) { + uint32_t v = S_boxes[i][j >> 1]; + v = j & 1 ? v >> 4 : v & 0xf; + v <<= 28 - 4 * i; + v = shuffle(v, P_shuffle, sizeof(P_shuffle)); + printf((j & 7) == 0 ? "\n " : " "); + printf("0x%08X,", v); + } + printf("\n },\n"); + } + printf("};\n"); +#endif + return 0; +} diff --git a/libavutil/tests/dict.c b/libavutil/tests/dict.c new file mode 100644 index 0000000000..0039ba5ff9 --- /dev/null +++ b/libavutil/tests/dict.c @@ -0,0 +1,133 @@ +/* + * copyright (c) 2009 Michael Niedermayer + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/dict.c" + +static void print_dict(const AVDictionary *m) +{ + AVDictionaryEntry *t = NULL; + while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) + printf("%s %s ", t->key, t->value); + printf("\n"); +} + +static void test_separators(const AVDictionary *m, const char pair, const char val) +{ + AVDictionary *dict = NULL; + char pairs[] = {pair , '\0'}; + char vals[] = {val, '\0'}; + + char *buffer = NULL; + av_dict_copy(&dict, m, 0); + print_dict(dict); + av_dict_get_string(dict, &buffer, val, pair); + printf("%s\n", buffer); + av_dict_free(&dict); + av_dict_parse_string(&dict, buffer, vals, pairs, 0); + av_freep(&buffer); + print_dict(dict); + av_dict_free(&dict); +} + +int main(void) +{ + AVDictionary *dict = NULL; + AVDictionaryEntry *e; + char *buffer = NULL; + + printf("Testing av_dict_get_string() and av_dict_parse_string()\n"); + av_dict_get_string(dict, &buffer, '=', ','); + printf("%s\n", buffer); + av_freep(&buffer); + av_dict_set(&dict, "aaa", "aaa", 0); + av_dict_set(&dict, "b,b", "bbb", 0); + av_dict_set(&dict, "c=c", "ccc", 0); + av_dict_set(&dict, "ddd", "d,d", 0); + av_dict_set(&dict, "eee", "e=e", 0); + av_dict_set(&dict, "f,f", "f=f", 0); + av_dict_set(&dict, "g=g", "g,g", 0); + test_separators(dict, ',', '='); + av_dict_free(&dict); + av_dict_set(&dict, "aaa", "aaa", 0); + av_dict_set(&dict, "bbb", "bbb", 0); + av_dict_set(&dict, "ccc", "ccc", 0); + av_dict_set(&dict, "\\,=\'\"", "\\,=\'\"", 0); + test_separators(dict, '"', '='); + test_separators(dict, '\'', '='); + test_separators(dict, ',', '"'); + test_separators(dict, ',', '\''); + test_separators(dict, '\'', '"'); + test_separators(dict, '"', '\''); + av_dict_free(&dict); + + printf("\nTesting av_dict_set()\n"); + av_dict_set(&dict, "a", "a", 0); + av_dict_set(&dict, "b", av_strdup("b"), AV_DICT_DONT_STRDUP_VAL); + av_dict_set(&dict, av_strdup("c"), "c", AV_DICT_DONT_STRDUP_KEY); + av_dict_set(&dict, av_strdup("d"), av_strdup("d"), AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL); + av_dict_set(&dict, "e", "e", AV_DICT_DONT_OVERWRITE); + av_dict_set(&dict, "e", "f", AV_DICT_DONT_OVERWRITE); + av_dict_set(&dict, "f", "f", 0); + av_dict_set(&dict, "f", NULL, 0); + av_dict_set(&dict, "ff", "f", 0); + av_dict_set(&dict, "ff", "f", AV_DICT_APPEND); + e = NULL; + while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))) + printf("%s %s\n", e->key, e->value); + av_dict_free(&dict); + + av_dict_set(&dict, NULL, "a", 0); + av_dict_set(&dict, NULL, "b", 0); + av_dict_get(dict, NULL, NULL, 0); + e = NULL; + while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))) + printf("'%s' '%s'\n", e->key, e->value); + av_dict_free(&dict); + + + //valgrind sensible test + printf("\nTesting av_dict_set_int()\n"); + av_dict_set_int(&dict, "1", 1, AV_DICT_DONT_STRDUP_VAL); + av_dict_set_int(&dict, av_strdup("2"), 2, AV_DICT_DONT_STRDUP_KEY); + av_dict_set_int(&dict, av_strdup("3"), 3, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL); + av_dict_set_int(&dict, "4", 4, 0); + av_dict_set_int(&dict, "5", 5, AV_DICT_DONT_OVERWRITE); + av_dict_set_int(&dict, "5", 6, AV_DICT_DONT_OVERWRITE); + av_dict_set_int(&dict, "12", 1, 0); + av_dict_set_int(&dict, "12", 2, AV_DICT_APPEND); + e = NULL; + while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))) + printf("%s %s\n", e->key, e->value); + av_dict_free(&dict); + + //valgrind sensible test + printf("\nTesting av_dict_set() with existing AVDictionaryEntry.key as key\n"); + av_dict_set(&dict, "key", "old", 0); + e = av_dict_get(dict, "key", NULL, 0); + av_dict_set(&dict, e->key, "new val OK", 0); + e = av_dict_get(dict, "key", NULL, 0); + printf("%s\n", e->value); + av_dict_set(&dict, e->key, e->value, 0); + e = av_dict_get(dict, "key", NULL, 0); + printf("%s\n", e->value); + av_dict_free(&dict); + + return 0; +} diff --git a/libavutil/tests/display.c b/libavutil/tests/display.c new file mode 100644 index 0000000000..893ebb5543 --- /dev/null +++ b/libavutil/tests/display.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2014 Vittorio Giovara <vittorio.giovara@gmail.com> + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/display.c" + +static void print_matrix(int32_t matrix[9]) +{ + int i, j; + + for (i = 0; i < 3; ++i) { + for (j = 0; j < 3 - 1; ++j) + printf("%d ", matrix[i*3 + j]); + + printf("%d\n", matrix[i*3 + j]); + } +} + +int main(void) +{ + int32_t matrix[9]; + + // Set the matrix to 90 degrees + av_display_rotation_set(matrix, 90); + print_matrix(matrix); + printf("degrees: %f\n", av_display_rotation_get(matrix)); + + // Set the matrix to -45 degrees + av_display_rotation_set(matrix, -45); + print_matrix(matrix); + printf("degrees: %f\n", av_display_rotation_get(matrix)); + + // flip horizontal + av_display_matrix_flip(matrix, 1, 0); + print_matrix(matrix); + printf("degrees: %f\n", av_display_rotation_get(matrix)); + + // flip vertical + av_display_matrix_flip(matrix, 0, 1); + print_matrix(matrix); + printf("degrees: %f\n", av_display_rotation_get(matrix)); + + return 0; + +} diff --git a/libavutil/tests/error.c b/libavutil/tests/error.c new file mode 100644 index 0000000000..16efc8ac45 --- /dev/null +++ b/libavutil/tests/error.c @@ -0,0 +1,37 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/error.c" + +#undef printf + +int main(void) +{ + int i; + + for (i = 0; i < FF_ARRAY_ELEMS(error_entries); i++) { + const struct error_entry *entry = &error_entries[i]; + printf("%d: %s [%s]\n", entry->num, av_err2str(entry->num), entry->tag); + } + + for (i = 0; i < 256; i++) { + printf("%d: %s\n", -i, av_err2str(-i)); + } + + return 0; +} diff --git a/libavutil/tests/eval.c b/libavutil/tests/eval.c new file mode 100644 index 0000000000..2a1afcc4dc --- /dev/null +++ b/libavutil/tests/eval.c @@ -0,0 +1,183 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <math.h> +#include <stdio.h> +#include <string.h> + +#include "libavutil/libm.h" +#include "libavutil/timer.h" +#include "libavutil/eval.h" + +static const double const_values[] = { + M_PI, + M_E, + 0 +}; + +static const char *const const_names[] = { + "PI", + "E", + 0 +}; + +int main(int argc, char **argv) +{ + int i; + double d; + const char *const *expr; + static const char *const exprs[] = { + "", + "1;2", + "-20", + "-PI", + "+PI", + "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", + "80G/80Gi", + "1k", + "1Gi", + "1gi", + "1GiFoo", + "1k+1k", + "1Gi*3foo", + "foo", + "foo(", + "foo()", + "foo)", + "sin", + "sin(", + "sin()", + "sin)", + "sin 10", + "sin(1,2,3)", + "sin(1 )", + "1", + "1foo", + "bar + PI + E + 100f*2 + foo", + "13k + 12f - foo(1, 2)", + "1gi", + "1Gi", + "st(0, 123)", + "st(1, 123); ld(1)", + "lte(0, 1)", + "lte(1, 1)", + "lte(1, 0)", + "lt(0, 1)", + "lt(1, 1)", + "gt(1, 0)", + "gt(2, 7)", + "gte(122, 122)", + /* compute 1+2+...+N */ + "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)", + /* compute Fib(N) */ + "st(1, 1); st(2, 2); st(0, 1); while(lte(ld(0),10), st(3, ld(1)+ld(2)); st(1, ld(2)); st(2, ld(3)); st(0, ld(0)+1)); ld(3)", + "while(0, 10)", + "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))", + "isnan(1)", + "isnan(NAN)", + "isnan(INF)", + "isinf(1)", + "isinf(NAN)", + "isinf(INF)", + "floor(NAN)", + "floor(123.123)", + "floor(-123.123)", + "trunc(123.123)", + "trunc(-123.123)", + "ceil(123.123)", + "ceil(-123.123)", + "sqrt(1764)", + "isnan(sqrt(-1))", + "not(1)", + "not(NAN)", + "not(0)", + "6.0206dB", + "-3.0103dB", + "pow(0,1.23)", + "pow(PI,1.23)", + "PI^1.23", + "pow(-1,1.23)", + "if(1, 2)", + "if(1, 1, 2)", + "if(0, 1, 2)", + "ifnot(0, 23)", + "ifnot(1, NaN) + if(0, 1)", + "ifnot(1, 1, 2)", + "ifnot(0, 1, 2)", + "taylor(1, 1)", + "taylor(eq(mod(ld(1),4),1)-eq(mod(ld(1),4),3), PI/2, 1)", + "root(sin(ld(0))-1, 2)", + "root(sin(ld(0))+6+sin(ld(0)/12)-log(ld(0)), 100)", + "7000000B*random(0)", + "squish(2)", + "gauss(0.1)", + "hypot(4,3)", + "gcd(30,55)*print(min(9,1))", + "bitor(42, 12)", + "bitand(42, 12)", + "bitand(NAN, 1)", + "between(10, -3, 10)", + "between(-4, -2, -1)", + "between(1,2)", + "clip(0, 2, 1)", + "clip(0/0, 1, 2)", + "clip(0, 0/0, 1)", + NULL + }; + int ret; + + for (expr = exprs; *expr; expr++) { + printf("Evaluating '%s'\n", *expr); + ret = av_expr_parse_and_eval(&d, *expr, + const_names, const_values, + NULL, NULL, NULL, NULL, NULL, 0, NULL); + if (isnan(d)) + printf("'%s' -> nan\n\n", *expr); + else + printf("'%s' -> %f\n\n", *expr, d); + if (ret < 0) + printf("av_expr_parse_and_eval failed\n"); + } + + ret = av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", + const_names, const_values, + NULL, NULL, NULL, NULL, NULL, 0, NULL); + printf("%f == 12.7\n", d); + if (ret < 0) + printf("av_expr_parse_and_eval failed\n"); + ret = av_expr_parse_and_eval(&d, "80G/80Gi", + const_names, const_values, + NULL, NULL, NULL, NULL, NULL, 0, NULL); + printf("%f == 0.931322575\n", d); + if (ret < 0) + printf("av_expr_parse_and_eval failed\n"); + + if (argc > 1 && !strcmp(argv[1], "-t")) { + for (i = 0; i < 1050; i++) { + START_TIMER; + ret = av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", + const_names, const_values, + NULL, NULL, NULL, NULL, NULL, 0, NULL); + if (ret < 0) + printf("av_expr_parse_and_eval failed\n"); + STOP_TIMER("av_expr_parse_and_eval"); + } + } + + return 0; +} diff --git a/libavutil/tests/fifo.c b/libavutil/tests/fifo.c new file mode 100644 index 0000000000..e4d7edf212 --- /dev/null +++ b/libavutil/tests/fifo.c @@ -0,0 +1,74 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> + +#include "libavutil/fifo.h" + +int main(void) +{ + /* create a FIFO buffer */ + AVFifoBuffer *fifo = av_fifo_alloc(13 * sizeof(int)); + int i, j, n; + + /* fill data */ + for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++) + av_fifo_generic_write(fifo, &i, sizeof(int), NULL); + + /* peek at FIFO */ + n = av_fifo_size(fifo) / sizeof(int); + for (i = -n + 1; i < n; i++) { + int *v = (int *)av_fifo_peek2(fifo, i * sizeof(int)); + printf("%d: %d\n", i, *v); + } + printf("\n"); + + /* peek_at at FIFO */ + n = av_fifo_size(fifo) / sizeof(int); + for (i = 0; i < n; i++) { + av_fifo_generic_peek_at(fifo, &j, i * sizeof(int), sizeof(j), NULL); + printf("%d: %d\n", i, j); + } + printf("\n"); + + /* read data */ + for (i = 0; av_fifo_size(fifo) >= sizeof(int); i++) { + av_fifo_generic_read(fifo, &j, sizeof(int), NULL); + printf("%d ", j); + } + printf("\n"); + + /* test *ndx overflow */ + av_fifo_reset(fifo); + fifo->rndx = fifo->wndx = ~(uint32_t)0 - 5; + + /* fill data */ + for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++) + av_fifo_generic_write(fifo, &i, sizeof(int), NULL); + + /* peek_at at FIFO */ + n = av_fifo_size(fifo) / sizeof(int); + for (i = 0; i < n; i++) { + av_fifo_generic_peek_at(fifo, &j, i * sizeof(int), sizeof(j), NULL); + printf("%d: %d\n", i, j); + } + + av_fifo_free(fifo); + + return 0; +} diff --git a/libavutil/tests/file.c b/libavutil/tests/file.c new file mode 100644 index 0000000000..3608bcccbe --- /dev/null +++ b/libavutil/tests/file.c @@ -0,0 +1,34 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/file.c" + +#undef printf + +int main(void) +{ + uint8_t *buf; + size_t size; + if (av_file_map("file.c", &buf, &size, 0, NULL) < 0) + return 1; + + buf[0] = 's'; + printf("%s", buf); + av_file_unmap(buf, size); + return 0; +} diff --git a/libavutil/tests/float_dsp.c b/libavutil/tests/float_dsp.c new file mode 100644 index 0000000000..053324dd38 --- /dev/null +++ b/libavutil/tests/float_dsp.c @@ -0,0 +1,333 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "config.h" + +#include <float.h> +#include <math.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#if HAVE_UNISTD_H +#include <unistd.h> /* for getopt */ +#endif +#if !HAVE_GETOPT +#include "compat/getopt.c" +#endif + +#include "libavutil/common.h" +#include "libavutil/cpu.h" +#include "libavutil/internal.h" +#include "libavutil/lfg.h" +#include "libavutil/log.h" +#include "libavutil/random_seed.h" +#include "libavutil/float_dsp.h" + +#define LEN 240 + +static void fill_float_array(AVLFG *lfg, float *a, int len) +{ + int i; + double bmg[2], stddev = 10.0, mean = 0.0; + + for (i = 0; i < len; i += 2) { + av_bmg_get(lfg, bmg); + a[i] = bmg[0] * stddev + mean; + a[i + 1] = bmg[1] * stddev + mean; + } +} +static int compare_floats(const float *a, const float *b, int len, + float max_diff) +{ + int i; + for (i = 0; i < len; i++) { + if (fabsf(a[i] - b[i]) > max_diff) { + av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n", + i, a[i], b[i], a[i] - b[i]); + return -1; + } + } + return 0; +} + +static void fill_double_array(AVLFG *lfg, double *a, int len) +{ + int i; + double bmg[2], stddev = 10.0, mean = 0.0; + + for (i = 0; i < len; i += 2) { + av_bmg_get(lfg, bmg); + a[i] = bmg[0] * stddev + mean; + a[i + 1] = bmg[1] * stddev + mean; + } +} + +static int compare_doubles(const double *a, const double *b, int len, + double max_diff) +{ + int i; + + for (i = 0; i < len; i++) { + if (fabs(a[i] - b[i]) > max_diff) { + av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n", + i, a[i], b[i], a[i] - b[i]); + return -1; + } + } + return 0; +} + +static int test_vector_fmul(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, + const float *v1, const float *v2) +{ + LOCAL_ALIGNED(32, float, cdst, [LEN]); + LOCAL_ALIGNED(32, float, odst, [LEN]); + int ret; + + cdsp->vector_fmul(cdst, v1, v2, LEN); + fdsp->vector_fmul(odst, v1, v2, LEN); + + if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON)) + av_log(NULL, AV_LOG_ERROR, "vector_fmul failed\n"); + + return ret; +} + +#define ARBITRARY_FMAC_SCALAR_CONST 0.005 +static int test_vector_fmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, + const float *v1, const float *src0, float scale) +{ + LOCAL_ALIGNED(32, float, cdst, [LEN]); + LOCAL_ALIGNED(32, float, odst, [LEN]); + int ret; + + memcpy(cdst, v1, LEN * sizeof(*v1)); + memcpy(odst, v1, LEN * sizeof(*v1)); + + cdsp->vector_fmac_scalar(cdst, src0, scale, LEN); + fdsp->vector_fmac_scalar(odst, src0, scale, LEN); + + if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMAC_SCALAR_CONST)) + av_log(NULL, AV_LOG_ERROR, "vector_fmac_scalar failed\n"); + + return ret; +} + +static int test_vector_fmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, + const float *v1, float scale) +{ + LOCAL_ALIGNED(32, float, cdst, [LEN]); + LOCAL_ALIGNED(32, float, odst, [LEN]); + int ret; + + cdsp->vector_fmul_scalar(cdst, v1, scale, LEN); + fdsp->vector_fmul_scalar(odst, v1, scale, LEN); + + if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON)) + av_log(NULL, AV_LOG_ERROR, "vector_fmul_scalar failed\n"); + + return ret; +} + +static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, + const double *v1, double scale) +{ + LOCAL_ALIGNED(32, double, cdst, [LEN]); + LOCAL_ALIGNED(32, double, odst, [LEN]); + int ret; + + cdsp->vector_dmul_scalar(cdst, v1, scale, LEN); + fdsp->vector_dmul_scalar(odst, v1, scale, LEN); + + if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON)) + av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n"); + + return ret; +} + +#define ARBITRARY_FMUL_WINDOW_CONST 0.008 +static int test_vector_fmul_window(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, + const float *v1, const float *v2, const float *v3) +{ + LOCAL_ALIGNED(32, float, cdst, [LEN]); + LOCAL_ALIGNED(32, float, odst, [LEN]); + int ret; + + cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2); + fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2); + + if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST)) + av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n"); + + return ret; +} + +#define ARBITRARY_FMUL_ADD_CONST 0.005 +static int test_vector_fmul_add(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, + const float *v1, const float *v2, const float *v3) +{ + LOCAL_ALIGNED(32, float, cdst, [LEN]); + LOCAL_ALIGNED(32, float, odst, [LEN]); + int ret; + + cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN); + fdsp->vector_fmul_add(odst, v1, v2, v3, LEN); + + if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST)) + av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n"); + + return ret; +} + +static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, + const float *v1, const float *v2) +{ + LOCAL_ALIGNED(32, float, cdst, [LEN]); + LOCAL_ALIGNED(32, float, odst, [LEN]); + int ret; + + cdsp->vector_fmul_reverse(cdst, v1, v2, LEN); + fdsp->vector_fmul_reverse(odst, v1, v2, LEN); + + if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON)) + av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n"); + + return ret; +} + +static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, + const float *v1, const float *v2) +{ + LOCAL_ALIGNED(32, float, cv1, [LEN]); + LOCAL_ALIGNED(32, float, cv2, [LEN]); + LOCAL_ALIGNED(32, float, ov1, [LEN]); + LOCAL_ALIGNED(32, float, ov2, [LEN]); + int ret; + + memcpy(cv1, v1, LEN * sizeof(*v1)); + memcpy(cv2, v2, LEN * sizeof(*v2)); + memcpy(ov1, v1, LEN * sizeof(*v1)); + memcpy(ov2, v2, LEN * sizeof(*v2)); + + cdsp->butterflies_float(cv1, cv2, LEN); + fdsp->butterflies_float(ov1, ov2, LEN); + + if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) || + (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON))) + av_log(NULL, AV_LOG_ERROR, "butterflies_float failed\n"); + + return ret; +} + +#define ARBITRARY_SCALARPRODUCT_CONST 0.2 +static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, + const float *v1, const float *v2) +{ + float cprod, oprod; + int ret; + + cprod = cdsp->scalarproduct_float(v1, v2, LEN); + oprod = fdsp->scalarproduct_float(v1, v2, LEN); + + if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST)) + av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n"); + + return ret; +} + +int main(int argc, char **argv) +{ + int ret = 0, seeded = 0; + uint32_t seed; + AVFloatDSPContext *fdsp, *cdsp; + AVLFG lfg; + + LOCAL_ALIGNED(32, float, src0, [LEN]); + LOCAL_ALIGNED(32, float, src1, [LEN]); + LOCAL_ALIGNED(32, float, src2, [LEN]); + LOCAL_ALIGNED(32, double, dbl_src0, [LEN]); + LOCAL_ALIGNED(32, double, dbl_src1, [LEN]); + + for (;;) { + int arg = getopt(argc, argv, "s:c:"); + if (arg == -1) + break; + switch (arg) { + case 's': + seed = strtoul(optarg, NULL, 10); + seeded = 1; + break; + case 'c': + { + int cpuflags = av_get_cpu_flags(); + + if (av_parse_cpu_caps(&cpuflags, optarg) < 0) + return 1; + + av_force_cpu_flags(cpuflags); + break; + } + } + } + if (!seeded) + seed = av_get_random_seed(); + + av_log(NULL, AV_LOG_INFO, "float_dsp-test: %s %u\n", seeded ? "seed" : "random seed", seed); + + fdsp = avpriv_float_dsp_alloc(1); + av_force_cpu_flags(0); + cdsp = avpriv_float_dsp_alloc(1); + + if (!fdsp || !cdsp) { + ret = 1; + goto end; + } + + av_lfg_init(&lfg, seed); + + fill_float_array(&lfg, src0, LEN); + fill_float_array(&lfg, src1, LEN); + fill_float_array(&lfg, src2, LEN); + + fill_double_array(&lfg, dbl_src0, LEN); + fill_double_array(&lfg, dbl_src1, LEN); + + if (test_vector_fmul(fdsp, cdsp, src0, src1)) + ret -= 1 << 0; + if (test_vector_fmac_scalar(fdsp, cdsp, src2, src0, src1[0])) + ret -= 1 << 1; + if (test_vector_fmul_scalar(fdsp, cdsp, src0, src1[0])) + ret -= 1 << 2; + if (test_vector_fmul_window(fdsp, cdsp, src0, src1, src2)) + ret -= 1 << 3; + if (test_vector_fmul_add(fdsp, cdsp, src0, src1, src2)) + ret -= 1 << 4; + if (test_vector_fmul_reverse(fdsp, cdsp, src0, src1)) + ret -= 1 << 5; + if (test_butterflies_float(fdsp, cdsp, src0, src1)) + ret -= 1 << 6; + if (test_scalarproduct_float(fdsp, cdsp, src0, src1)) + ret -= 1 << 7; + if (test_vector_dmul_scalar(fdsp, cdsp, dbl_src0, dbl_src1[0])) + ret -= 1 << 8; + +end: + av_freep(&fdsp); + av_freep(&cdsp); + return ret; +} diff --git a/libavutil/tests/hash.c b/libavutil/tests/hash.c new file mode 100644 index 0000000000..4f2ad52ad6 --- /dev/null +++ b/libavutil/tests/hash.c @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2013 Reimar Döffinger <Reimar.Doeffinger@gmx.de> + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> +#include <string.h> + +#include "libavutil/hash.h" + +#define SRC_BUF_SIZE 64 +#define DST_BUF_SIZE (AV_HASH_MAX_SIZE * 8) + +int main(void) +{ + struct AVHashContext *ctx = NULL; + int i, j, numhashes = 0; + static const uint8_t src[SRC_BUF_SIZE] = { 0 }; + uint8_t dst[DST_BUF_SIZE]; + + while (av_hash_names(numhashes)) + numhashes++; + + for (i = 0; i < numhashes; i++) { + if (av_hash_alloc(&ctx, av_hash_names(i)) < 0) + return 1; + + av_hash_init(ctx); + av_hash_update(ctx, src, SRC_BUF_SIZE); + memset(dst, 0, DST_BUF_SIZE); + av_hash_final_hex(ctx, dst, DST_BUF_SIZE); + printf("%s hex: %s\n", av_hash_get_name(ctx), dst); + + av_hash_init(ctx); + av_hash_update(ctx, src, SRC_BUF_SIZE); + av_hash_final_bin(ctx, dst, DST_BUF_SIZE); + printf("%s bin: ", av_hash_get_name(ctx)); + for (j = 0; j < av_hash_get_size(ctx); j++) { + printf("%#x ", dst[j]); + } + printf("\n"); + + av_hash_init(ctx); + av_hash_update(ctx, src, SRC_BUF_SIZE); + av_hash_final_b64(ctx, dst, DST_BUF_SIZE); + printf("%s b64: %s\n", av_hash_get_name(ctx), dst); + av_hash_freep(&ctx); + } + return 0; +} diff --git a/libavutil/tests/hmac.c b/libavutil/tests/hmac.c new file mode 100644 index 0000000000..5eeb63ce05 --- /dev/null +++ b/libavutil/tests/hmac.c @@ -0,0 +1,99 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/hmac.c" + +#include <stdio.h> +#include <string.h> + +static void test(AVHMAC *hmac, const uint8_t *key, int keylen, + const uint8_t *data, int datalen) +{ + uint8_t buf[MAX_HASHLEN]; + int out, i; + // Some of the test vectors are strings, where sizeof() includes the + // trailing null byte - remove that. + if (!key[keylen - 1]) + keylen--; + if (!data[datalen - 1]) + datalen--; + out = av_hmac_calc(hmac, data, datalen, key, keylen, buf, sizeof(buf)); + for (i = 0; i < out; i++) + printf("%02x", buf[i]); + printf("\n"); +} + +int main(void) +{ + uint8_t key1[20], key3[131], data3[50]; + AVHMAC *hmac; + enum AVHMACType i; + static const uint8_t key2[] = "Jefe"; + static const uint8_t data1[] = "Hi There"; + static const uint8_t data2[] = "what do ya want for nothing?"; + static const uint8_t data4[] = "Test Using Larger Than Block-Size Key - Hash Key First"; + static const uint8_t data5[] = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"; + static const uint8_t data6[] = "This is a test using a larger than block-size key and a larger " + "than block-size data. The key needs to be hashed before being used" + " by the HMAC algorithm."; + memset(key1, 0x0b, sizeof(key1)); + memset(key3, 0xaa, sizeof(key3)); + memset(data3, 0xdd, sizeof(data3)); + + /* MD5, SHA-1 */ + for (i = AV_HMAC_MD5; i <= AV_HMAC_SHA1; i++) { + hmac = av_hmac_alloc(i); + if (!hmac) + return 1; + // RFC 2202 test vectors + test(hmac, key1, hmac->hashlen, data1, sizeof(data1)); + test(hmac, key2, sizeof(key2), data2, sizeof(data2)); + test(hmac, key3, hmac->hashlen, data3, sizeof(data3)); + test(hmac, key3, 80, data4, sizeof(data4)); + test(hmac, key3, 80, data5, sizeof(data5)); + av_hmac_free(hmac); + } + + /* SHA-2 */ + for (i = AV_HMAC_SHA224; i <= AV_HMAC_SHA256; i++) { + hmac = av_hmac_alloc(i); + if (!hmac) + return 1; + // RFC 4231 test vectors + test(hmac, key1, sizeof(key1), data1, sizeof(data1)); + test(hmac, key2, sizeof(key2), data2, sizeof(data2)); + test(hmac, key3, 20, data3, sizeof(data3)); + test(hmac, key3, sizeof(key3), data4, sizeof(data4)); + test(hmac, key3, sizeof(key3), data6, sizeof(data6)); + av_hmac_free(hmac); + } + + for (i = AV_HMAC_SHA384; i <= AV_HMAC_SHA512; i++) { + hmac = av_hmac_alloc(i); + if (!hmac) + return 1; + // RFC 4231 test vectors + test(hmac, key1, sizeof(key1), data1, sizeof(data1)); + test(hmac, key2, sizeof(key2), data2, sizeof(data2)); + test(hmac, key3, 20, data3, sizeof(data3)); + test(hmac, key3, sizeof(key3), data4, sizeof(data4)); + test(hmac, key3, sizeof(key3), data6, sizeof(data6)); + av_hmac_free(hmac); + } + return 0; +} diff --git a/libavutil/tests/lfg.c b/libavutil/tests/lfg.c new file mode 100644 index 0000000000..1425e026b4 --- /dev/null +++ b/libavutil/tests/lfg.c @@ -0,0 +1,74 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/log.h" +#include "libavutil/timer.h" +#include "libavutil/lfg.h" + +int main(void) +{ + int x = 0; + int i, j; + AVLFG state; + + av_lfg_init(&state, 0xdeadbeef); + for (j = 0; j < 10000; j++) { + START_TIMER + for (i = 0; i < 624; i++) { + //av_log(NULL, AV_LOG_ERROR, "%X\n", av_lfg_get(&state)); + x += av_lfg_get(&state); + } + STOP_TIMER("624 calls of av_lfg_get"); + } + av_log(NULL, AV_LOG_ERROR, "final value:%X\n", x); + + /* BMG usage example */ + { + double mean = 1000; + double stddev = 53; + double samp_mean = 0.0, samp_stddev = 0.0; + double samp0, samp1; + + av_lfg_init(&state, 42); + + for (i = 0; i < 1000; i += 2) { + double bmg_out[2]; + av_bmg_get(&state, bmg_out); + samp0 = bmg_out[0] * stddev + mean; + samp1 = bmg_out[1] * stddev + mean; + samp_mean += samp0 + samp1; + samp_stddev += samp0 * samp0 + samp1 * samp1; + av_log(NULL, AV_LOG_INFO, + "%f\n%f\n", + samp0, + samp1); + } + /* TODO: add proper normality test */ + samp_mean /= 1000; + samp_stddev /= 999; + samp_stddev -= (1000.0/999.0)*samp_mean*samp_mean; + samp_stddev = sqrt(samp_stddev); + av_log(NULL, AV_LOG_INFO, "sample mean : %f\n" + "true mean : %f\n" + "sample stddev: %f\n" + "true stddev : %f\n", + samp_mean, mean, samp_stddev, stddev); + } + + return 0; +} diff --git a/libavutil/tests/lls.c b/libavutil/tests/lls.c new file mode 100644 index 0000000000..965b0f458d --- /dev/null +++ b/libavutil/tests/lls.c @@ -0,0 +1,54 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <limits.h> +#include <stdio.h> + +#include "libavutil/internal.h" +#include "libavutil/lfg.h" +#include "libavutil/lls.h" + +int main(void) +{ + LLSModel m; + int i, order; + AVLFG lfg; + + av_lfg_init(&lfg, 1); + avpriv_init_lls(&m, 3); + + for (i = 0; i < 100; i++) { + LOCAL_ALIGNED(32, double, var, [4]); + double eval; + + var[0] = (av_lfg_get(&lfg) / (double) UINT_MAX - 0.5) * 2; + var[1] = var[0] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5; + var[2] = var[1] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5; + var[3] = var[2] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5; + m.update_lls(&m, var); + avpriv_solve_lls(&m, 0.001, 0); + for (order = 0; order < 3; order++) { + eval = m.evaluate_lls(&m, var + 1, order); + printf("real:%9f order:%d pred:%9f var:%f coeffs:%f %9f %9f\n", + var[0], order, eval, sqrt(m.variance[order] / (i + 1)), + m.coeff[order][0], m.coeff[order][1], + m.coeff[order][2]); + } + } + return 0; +} diff --git a/libavutil/tests/log.c b/libavutil/tests/log.c new file mode 100644 index 0000000000..8fe9461c1b --- /dev/null +++ b/libavutil/tests/log.c @@ -0,0 +1,71 @@ +/* + * log functions + * Copyright (c) 2003 Michel Bardiaux + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/log.c" + +#include <string.h> + +static int call_log_format_line2(const char *fmt, char *buffer, int buffer_size, ...) +{ + va_list args; + int ret; + int print_prefix=1; + va_start(args, buffer_size); + ret = av_log_format_line2(NULL, AV_LOG_INFO, fmt, args, buffer, buffer_size, &print_prefix); + va_end(args); + return ret; +} + +int main(int argc, char **argv) +{ + int i; + av_log_set_level(AV_LOG_DEBUG); + for (use_color=0; use_color<=256; use_color = 255*use_color+1) { + av_log(NULL, AV_LOG_FATAL, "use_color: %d\n", use_color); + for (i = AV_LOG_DEBUG; i>=AV_LOG_QUIET; i-=8) { + av_log(NULL, i, " %d", i); + av_log(NULL, AV_LOG_INFO, "e "); + av_log(NULL, i + 256*123, "C%d", i); + av_log(NULL, AV_LOG_INFO, "e"); + } + av_log(NULL, AV_LOG_PANIC, "\n"); + } + { + int result; + char buffer[4]; + result = call_log_format_line2("foo", NULL, 0); + if(result != 3) { + printf("Test NULL buffer failed.\n"); + return 1; + } + result = call_log_format_line2("foo", buffer, 2); + if(result != 3 || strncmp(buffer, "f", 2)) { + printf("Test buffer too small failed.\n"); + return 1; + } + result = call_log_format_line2("foo", buffer, 4); + if(result != 3 || strncmp(buffer, "foo", 4)) { + printf("Test buffer sufficiently big failed.\n"); + return 1; + } + } + return 0; +} diff --git a/libavutil/tests/lzo.c b/libavutil/tests/lzo.c new file mode 100644 index 0000000000..0147ab5c1d --- /dev/null +++ b/libavutil/tests/lzo.c @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2006 Reimar Doeffinger + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> +#include <lzo/lzo1x.h> + +#include "libavutil/log.h" +#include "libavutil/lzo.h" +#include "libavutil/mem.h" + +#define MAXSZ (10*1024*1024) + +/* Define one of these to 1 if you wish to benchmark liblzo + * instead of our native implementation. */ +#define BENCHMARK_LIBLZO_SAFE 0 +#define BENCHMARK_LIBLZO_UNSAFE 0 + +int main(int argc, char *argv[]) { + FILE *in = fopen(argv[1], "rb"); + int comp_level = argc > 2 ? atoi(argv[2]) : 0; + uint8_t *orig = av_malloc(MAXSZ + 16); + uint8_t *comp = av_malloc(2*MAXSZ + 16); + uint8_t *decomp = av_malloc(MAXSZ + 16); + size_t s = fread(orig, 1, MAXSZ, in); + lzo_uint clen = 0; + long tmp[LZO1X_MEM_COMPRESS]; + int inlen, outlen; + int i; + av_log_set_level(AV_LOG_DEBUG); + if (comp_level == 0) { + lzo1x_1_compress(orig, s, comp, &clen, tmp); + } else if (comp_level == 11) { + lzo1x_1_11_compress(orig, s, comp, &clen, tmp); + } else if (comp_level == 12) { + lzo1x_1_12_compress(orig, s, comp, &clen, tmp); + } else if (comp_level == 15) { + lzo1x_1_15_compress(orig, s, comp, &clen, tmp); + } else + lzo1x_999_compress(orig, s, comp, &clen, tmp); + for (i = 0; i < 300; i++) { +START_TIMER + inlen = clen; outlen = MAXSZ; +#if BENCHMARK_LIBLZO_SAFE + if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL)) +#elif BENCHMARK_LIBLZO_UNSAFE + if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL)) +#else + if (av_lzo1x_decode(decomp, &outlen, comp, &inlen)) +#endif + av_log(NULL, AV_LOG_ERROR, "decompression error\n"); +STOP_TIMER("lzod") + } + if (memcmp(orig, decomp, s)) + av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n"); + else + av_log(NULL, AV_LOG_ERROR, "decompression OK\n"); + fclose(in); + av_free(orig); + av_free(comp); + av_free(decomp); + return 0; +} diff --git a/libavutil/tests/md5.c b/libavutil/tests/md5.c new file mode 100644 index 0000000000..42e4538e0a --- /dev/null +++ b/libavutil/tests/md5.c @@ -0,0 +1,55 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdint.h> +#include <stdio.h> + +#include "libavutil/md5.h" + +static void print_md5(uint8_t *md5) +{ + int i; + for (i = 0; i < 16; i++) + printf("%02x", md5[i]); + printf("\n"); +} + +int main(void) +{ + uint8_t md5val[16]; + int i; + volatile uint8_t in[1000]; // volatile to workaround http://llvm.org/bugs/show_bug.cgi?id=20849 + // FIXME remove volatile once it has been fixed and all fate clients are updated + + for (i = 0; i < 1000; i++) + in[i] = i * i; + av_md5_sum(md5val, in, 1000); + print_md5(md5val); + av_md5_sum(md5val, in, 63); + print_md5(md5val); + av_md5_sum(md5val, in, 64); + print_md5(md5val); + av_md5_sum(md5val, in, 65); + print_md5(md5val); + for (i = 0; i < 1000; i++) + in[i] = i % 127; + av_md5_sum(md5val, in, 999); + print_md5(md5val); + + return 0; +} diff --git a/libavutil/tests/murmur3.c b/libavutil/tests/murmur3.c new file mode 100644 index 0000000000..b8d6b1feee --- /dev/null +++ b/libavutil/tests/murmur3.c @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2013 Reimar Döffinger <Reimar.Doeffinger@gmx.de> + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/intreadwrite.h" +#include "libavutil/mem.h" +#include "libavutil/murmur3.h" + +int main(void) +{ + int i; + uint8_t hash_result[16] = {0}; + struct AVMurMur3 *ctx = av_murmur3_alloc(); +#if 1 + uint8_t in[256] = {0}; + uint8_t *hashes = av_mallocz(256 * 16); + for (i = 0; i < 256; i++) + { + in[i] = i; + av_murmur3_init_seeded(ctx, 256 - i); + // Note: this actually tests hashing 0 bytes + av_murmur3_update(ctx, in, i); + av_murmur3_final(ctx, hashes + 16 * i); + } + av_murmur3_init_seeded(ctx, 0); + av_murmur3_update(ctx, hashes, 256 * 16); + av_murmur3_final(ctx, hash_result); + av_free(hashes); + av_freep(&ctx); + printf("result: 0x%"PRIx64" 0x%"PRIx64"\n", AV_RL64(hash_result), AV_RL64(hash_result + 8)); + // official reference value is 32 bit + return AV_RL32(hash_result) != 0x6384ba69; +#else + uint8_t *in = av_mallocz(512*1024); + av_murmur3_init(ctx); + for (i = 0; i < 40*1024; i++) + av_murmur3_update(ctx, in, 512*1024); + av_murmur3_final(ctx, hash_result); + av_free(in); + return hash_result[0]; +#endif +} diff --git a/libavutil/tests/opt.c b/libavutil/tests/opt.c new file mode 100644 index 0000000000..568eb45668 --- /dev/null +++ b/libavutil/tests/opt.c @@ -0,0 +1,312 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <limits.h> +#include <stdio.h> + +#include "libavutil/common.h" +#include "libavutil/channel_layout.h" +#include "libavutil/error.h" +#include "libavutil/log.h" +#include "libavutil/mem.h" +#include "libavutil/rational.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" + +typedef struct TestContext { + const AVClass *class; + int num; + int toggle; + char *string; + int flags; + AVRational rational; + AVRational video_rate; + int w, h; + enum AVPixelFormat pix_fmt; + enum AVSampleFormat sample_fmt; + int64_t duration; + uint8_t color[4]; + int64_t channel_layout; + void *binary; + int binary_size; + void *binary1; + int binary_size1; + void *binary2; + int binary_size2; + int64_t num64; + float flt; + double dbl; + char *escape; + int bool1; + int bool2; + int bool3; +} TestContext; + +#define OFFSET(x) offsetof(TestContext, x) + +#define TEST_FLAG_COOL 01 +#define TEST_FLAG_LAME 02 +#define TEST_FLAG_MU 04 + +static const AVOption test_options[]= { + {"num", "set num", OFFSET(num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, 1 }, + {"toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, 1 }, + {"rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, 10, 1 }, + {"string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, { .str = "default" }, CHAR_MIN, CHAR_MAX, 1 }, + {"escape", "set escape str", OFFSET(escape), AV_OPT_TYPE_STRING, { .str = "\\=," }, CHAR_MIN, CHAR_MAX, 1 }, + {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, { .i64 = 1 }, 0, INT_MAX, 1, "flags" }, + {"cool", "set cool flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_COOL }, INT_MIN, INT_MAX, 1, "flags" }, + {"lame", "set lame flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_LAME }, INT_MIN, INT_MAX, 1, "flags" }, + {"mu", "set mu flag", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_MU }, INT_MIN, INT_MAX, 1, "flags" }, + {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, { .str="200x300" }, 0, 0, 1 }, + {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, { .i64 = AV_PIX_FMT_0BGR }, -1, INT_MAX, 1 }, + {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, { .i64 = AV_SAMPLE_FMT_S16 }, -1, INT_MAX, 1 }, + {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, INT_MAX, 1 }, + {"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, { .i64 = 1000 }, 0, INT64_MAX, 1 }, + {"color", "set color", OFFSET(color), AV_OPT_TYPE_COLOR, { .str = "pink" }, 0, 0, 1 }, + {"cl", "set channel layout", OFFSET(channel_layout), AV_OPT_TYPE_CHANNEL_LAYOUT, { .i64 = AV_CH_LAYOUT_HEXAGONAL }, 0, INT64_MAX, 1 }, + {"bin", "set binary value", OFFSET(binary), AV_OPT_TYPE_BINARY, { .str="62696e00" }, 0, 0, 1 }, + {"bin1", "set binary value", OFFSET(binary1), AV_OPT_TYPE_BINARY, { .str=NULL }, 0, 0, 1 }, + {"bin2", "set binary value", OFFSET(binary2), AV_OPT_TYPE_BINARY, { .str="" }, 0, 0, 1 }, + {"num64", "set num 64bit", OFFSET(num64), AV_OPT_TYPE_INT64, { .i64 = 1 }, 0, 100, 1 }, + {"flt", "set float", OFFSET(flt), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 / 3 }, 0, 100, 1 }, + {"dbl", "set double", OFFSET(dbl), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 / 3 }, 0, 100, 1 }, + {"bool1", "set boolean value", OFFSET(bool1), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, 1 }, + {"bool2", "set boolean value", OFFSET(bool2), AV_OPT_TYPE_BOOL, { .i64 = 1 }, -1, 1, 1 }, + {"bool3", "set boolean value", OFFSET(bool3), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, 1 }, + { NULL }, +}; + +static const char *test_get_name(void *ctx) +{ + return "test"; +} + +static const AVClass test_class = { + "TestContext", + test_get_name, + test_options +}; + +static void log_callback_help(void *ptr, int level, const char *fmt, va_list vl) +{ + vfprintf(stdout, fmt, vl); +} + +int main(void) +{ + int i; + + av_log_set_level(AV_LOG_DEBUG); + av_log_set_callback(log_callback_help); + + printf("Testing default values\n"); + { + TestContext test_ctx = { 0 }; + test_ctx.class = &test_class; + av_opt_set_defaults(&test_ctx); + + printf("num=%d\n", test_ctx.num); + printf("toggle=%d\n", test_ctx.toggle); + printf("string=%s\n", test_ctx.string); + printf("escape=%s\n", test_ctx.escape); + printf("flags=%d\n", test_ctx.flags); + printf("rational=%d/%d\n", test_ctx.rational.num, test_ctx.rational.den); + printf("video_rate=%d/%d\n", test_ctx.video_rate.num, test_ctx.video_rate.den); + printf("width=%d height=%d\n", test_ctx.w, test_ctx.h); + printf("pix_fmt=%s\n", av_get_pix_fmt_name(test_ctx.pix_fmt)); + printf("sample_fmt=%s\n", av_get_sample_fmt_name(test_ctx.sample_fmt)); + printf("duration=%"PRId64"\n", test_ctx.duration); + printf("color=%d %d %d %d\n", test_ctx.color[0], test_ctx.color[1], test_ctx.color[2], test_ctx.color[3]); + printf("channel_layout=%"PRId64"=%"PRId64"\n", test_ctx.channel_layout, (int64_t)AV_CH_LAYOUT_HEXAGONAL); + if (test_ctx.binary) + printf("binary=%x %x %x %x\n", ((uint8_t*)test_ctx.binary)[0], ((uint8_t*)test_ctx.binary)[1], ((uint8_t*)test_ctx.binary)[2], ((uint8_t*)test_ctx.binary)[3]); + printf("binary_size=%d\n", test_ctx.binary_size); + printf("num64=%"PRId64"\n", test_ctx.num64); + printf("flt=%.6f\n", test_ctx.flt); + printf("dbl=%.6f\n", test_ctx.dbl); + + av_opt_show2(&test_ctx, NULL, -1, 0); + + av_opt_free(&test_ctx); + } + + printf("\nTesting av_opt_is_set_to_default()\n"); + { + int ret; + TestContext test_ctx = { 0 }; + const AVOption *o = NULL; + test_ctx.class = &test_class; + + av_log_set_level(AV_LOG_QUIET); + + while (o = av_opt_next(&test_ctx, o)) { + ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0); + printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : ""); + } + av_opt_set_defaults(&test_ctx); + while (o = av_opt_next(&test_ctx, o)) { + ret = av_opt_is_set_to_default_by_name(&test_ctx, o->name, 0); + printf("name:%10s default:%d error:%s\n", o->name, !!ret, ret < 0 ? av_err2str(ret) : ""); + } + av_opt_free(&test_ctx); + } + + printf("\nTest av_opt_serialize()\n"); + { + TestContext test_ctx = { 0 }; + char *buf; + test_ctx.class = &test_class; + + av_log_set_level(AV_LOG_QUIET); + + av_opt_set_defaults(&test_ctx); + if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) { + printf("%s\n", buf); + av_opt_free(&test_ctx); + memset(&test_ctx, 0, sizeof(test_ctx)); + test_ctx.class = &test_class; + av_set_options_string(&test_ctx, buf, "=", ","); + av_free(buf); + if (av_opt_serialize(&test_ctx, 0, 0, &buf, '=', ',') >= 0) { + printf("%s\n", buf); + av_free(buf); + } + } + av_opt_free(&test_ctx); + } + + printf("\nTesting av_set_options_string()\n"); + { + TestContext test_ctx = { 0 }; + static const char * const options[] = { + "", + ":", + "=", + "foo=:", + ":=foo", + "=foo", + "foo=", + "foo", + "foo=val", + "foo==val", + "toggle=:", + "string=:", + "toggle=1 : foo", + "toggle=100", + "toggle==1", + "flags=+mu-lame : num=42: toggle=0", + "num=42 : string=blahblah", + "rational=0 : rational=1/2 : rational=1/-1", + "rational=-1/0", + "size=1024x768", + "size=pal", + "size=bogus", + "pix_fmt=yuv420p", + "pix_fmt=2", + "pix_fmt=bogus", + "sample_fmt=s16", + "sample_fmt=2", + "sample_fmt=bogus", + "video_rate=pal", + "video_rate=25", + "video_rate=30000/1001", + "video_rate=30/1.001", + "video_rate=bogus", + "duration=bogus", + "duration=123.45", + "duration=1\\:23\\:45.67", + "color=blue", + "color=0x223300", + "color=0x42FF07AA", + "cl=stereo+downmix", + "cl=foo", + "bin=boguss", + "bin=111", + "bin=ffff", + "num64=bogus", + "num64=44", + "num64=44.4", + "num64=-1", + "num64=101", + "flt=bogus", + "flt=2", + "flt=2.2", + "flt=-1", + "flt=101", + "dbl=bogus", + "dbl=2", + "dbl=2.2", + "dbl=-1", + "dbl=101", + "bool1=true", + "bool2=auto", + }; + + test_ctx.class = &test_class; + av_opt_set_defaults(&test_ctx); + + av_log_set_level(AV_LOG_QUIET); + + for (i=0; i < FF_ARRAY_ELEMS(options); i++) { + int silence_log = !strcmp(options[i], "rational=-1/0"); // inf formating differs between platforms + av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]); + if (silence_log) + av_log_set_callback(NULL); + if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0) + printf("Error '%s'\n", options[i]); + else + printf("OK '%s'\n", options[i]); + av_log_set_callback(log_callback_help); + } + av_opt_free(&test_ctx); + } + + printf("\nTesting av_opt_set_from_string()\n"); + { + TestContext test_ctx = { 0 }; + static const char * const options[] = { + "", + "5", + "5:hello", + "5:hello:size=pal", + "5:size=pal:hello", + ":", + "=", + " 5 : hello : size = pal ", + "a_very_long_option_name_that_will_need_to_be_ellipsized_around_here=42" + }; + static const char * const shorthand[] = { "num", "string", NULL }; + + test_ctx.class = &test_class; + av_opt_set_defaults(&test_ctx); + + av_log_set_level(AV_LOG_QUIET); + + for (i=0; i < FF_ARRAY_ELEMS(options); i++) { + av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]); + if (av_opt_set_from_string(&test_ctx, options[i], shorthand, "=", ":") < 0) + printf("Error '%s'\n", options[i]); + else + printf("OK '%s'\n", options[i]); + } + av_opt_free(&test_ctx); + } + + return 0; +} diff --git a/libavutil/tests/parseutils.c b/libavutil/tests/parseutils.c new file mode 100644 index 0000000000..682b3907cf --- /dev/null +++ b/libavutil/tests/parseutils.c @@ -0,0 +1,260 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#define TEST +#include "libavutil/parseutils.c" + +#include <stdint.h> +#include <stdio.h> + +#include "libavutil/common.h" +#include "libavutil/log.h" +#include "libavutil/rational.h" + +static uint32_t randomv = MKTAG('L','A','V','U'); + +static uint32_t av_get_random_seed_deterministic(void) +{ + return randomv = randomv * 1664525 + 1013904223; +} + +static void test_av_parse_video_rate(void) +{ + int i; + static const char *const rates[] = { + "-inf", + "inf", + "nan", + "123/0", + "-123 / 0", + "", + "/", + " 123 / 321", + "foo/foo", + "foo/1", + "1/foo", + "0/0", + "/0", + "1/", + "1", + "0", + "-123/123", + "-foo", + "123.23", + ".23", + "-.23", + "-0.234", + "-0.0000001", + " 21332.2324 ", + " -21332.2324 ", + }; + + for (i = 0; i < FF_ARRAY_ELEMS(rates); i++) { + int ret; + AVRational q = { 0, 0 }; + ret = av_parse_video_rate(&q, rates[i]); + printf("'%s' -> %d/%d %s\n", + rates[i], q.num, q.den, ret ? "ERROR" : "OK"); + } +} + +static void test_av_parse_color(void) +{ + int i; + uint8_t rgba[4]; + static const char *const color_names[] = { + "bikeshed", + "RaNdOm", + "foo", + "red", + "Red ", + "RED", + "Violet", + "Yellow", + "Red", + "0x000000", + "0x0000000", + "0xff000000", + "0x3e34ff", + "0x3e34ffaa", + "0xffXXee", + "0xfoobar", + "0xffffeeeeeeee", + "#ff0000", + "#ffXX00", + "ff0000", + "ffXX00", + "red@foo", + "random@10", + "0xff0000@1.0", + "red@", + "red@0xfff", + "red@0xf", + "red@2", + "red@0.1", + "red@-1", + "red@0.5", + "red@1.0", + "red@256", + "red@10foo", + "red@-1.0", + "red@-0.0", + }; + + av_log_set_level(AV_LOG_DEBUG); + + for (i = 0; i < FF_ARRAY_ELEMS(color_names); i++) { + if (av_parse_color(rgba, color_names[i], -1, NULL) >= 0) + printf("%s -> R(%d) G(%d) B(%d) A(%d)\n", + color_names[i], rgba[0], rgba[1], rgba[2], rgba[3]); + else + printf("%s -> error\n", color_names[i]); + } +} + +static void test_av_small_strptime(void) +{ + int i; + struct tm tm = { 0 }; + struct fmt_timespec_entry { + const char *fmt, *timespec; + } fmt_timespec_entries[] = { + { "%Y-%m-%d", "2012-12-21" }, + { "%Y - %m - %d", "2012-12-21" }, + { "%Y-%m-%d %H:%M:%S", "2012-12-21 20:12:21" }, + { " %Y - %m - %d %H : %M : %S", " 2012 - 12 - 21 20 : 12 : 21" }, + }; + + av_log_set_level(AV_LOG_DEBUG); + for (i = 0; i < FF_ARRAY_ELEMS(fmt_timespec_entries); i++) { + char *p; + struct fmt_timespec_entry *e = &fmt_timespec_entries[i]; + printf("fmt:'%s' spec:'%s' -> ", e->fmt, e->timespec); + p = av_small_strptime(e->timespec, e->fmt, &tm); + if (p) { + printf("%04d-%02d-%2d %02d:%02d:%02d\n", + 1900+tm.tm_year, tm.tm_mon+1, tm.tm_mday, + tm.tm_hour, tm.tm_min, tm.tm_sec); + } else { + printf("error\n"); + } + } +} + +static void test_av_parse_time(void) +{ + int i; + int64_t tv; + time_t tvi; + struct tm *tm; + static char tzstr[] = "TZ=CET-1"; + static const char * const time_string[] = { + "now", + "12:35:46", + "2000-12-20 0:02:47.5z", + "2012 - 02-22 17:44:07", + "2000-12-20T010247.6", + "2000-12-12 1:35:46+05:30", + "2002-12-12 22:30:40-02", + }; + static const char * const duration_string[] = { + "2:34:56.79", + "-1:23:45.67", + "42.1729", + "-1729.42", + "12:34", + }; + + av_log_set_level(AV_LOG_DEBUG); + putenv(tzstr); + printf("(now is 2012-03-17 09:14:13.2 +0100, local time is UTC+1)\n"); + for (i = 0; i < FF_ARRAY_ELEMS(time_string); i++) { + printf("%-24s -> ", time_string[i]); + if (av_parse_time(&tv, time_string[i], 0)) { + printf("error\n"); + } else { + tvi = tv / 1000000; + tm = gmtime(&tvi); + printf("%14"PRIi64".%06d = %04d-%02d-%02dT%02d:%02d:%02dZ\n", + tv / 1000000, (int)(tv % 1000000), + tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec); + } + } + for (i = 0; i < FF_ARRAY_ELEMS(duration_string); i++) { + printf("%-24s -> ", duration_string[i]); + if (av_parse_time(&tv, duration_string[i], 1)) { + printf("error\n"); + } else { + printf("%+21"PRIi64"\n", tv); + } + } +} + +static void test_av_get_known_color_name(void) +{ + int i; + const uint8_t *rgba; + const char *color; + + for (i = 0; i < FF_ARRAY_ELEMS(color_table); ++i) { + color = av_get_known_color_name(i, &rgba); + if (color) + printf("%s -> R(%d) G(%d) B(%d) A(%d)\n", + color, rgba[0], rgba[1], rgba[2], rgba[3]); + else + printf("Color ID: %d not found\n", i); + } +} + +static void test_av_find_info_tag(void) +{ + static const char args[] = "?tag1=val1&tag2=val2&tag3=val3&tag41=value 41&tag42=random1"; + static const char *tags[] = {"tag1", "tag2", "tag3", "tag4", "tag41", "41", "random1"}; + char buff[16]; + int i; + + for (i = 0; i < FF_ARRAY_ELEMS(tags); ++i) { + if (av_find_info_tag(buff, sizeof(buff), tags[i], args)) + printf("%d. %s found: %s\n", i, tags[i], buff); + else + printf("%d. %s not found\n", i, tags[i]); + } +} + +int main(void) +{ + printf("Testing av_parse_video_rate()\n"); + test_av_parse_video_rate(); + + printf("\nTesting av_parse_color()\n"); + test_av_parse_color(); + + printf("\nTesting av_small_strptime()\n"); + test_av_small_strptime(); + + printf("\nTesting av_parse_time()\n"); + test_av_parse_time(); + + printf("\nTesting av_get_known_color_name()\n"); + test_av_get_known_color_name(); + + printf("\nTesting av_find_info_tag()\n"); + test_av_find_info_tag(); + return 0; +} diff --git a/libavutil/tests/pca.c b/libavutil/tests/pca.c new file mode 100644 index 0000000000..2d9eb8f56a --- /dev/null +++ b/libavutil/tests/pca.c @@ -0,0 +1,102 @@ +/* + * principal component analysis (PCA) + * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/pca.c" +#include "libavutil/lfg.h" + +#undef printf +#include <stdio.h> +#include <stdlib.h> + +int main(void){ + PCA *pca; + int i, j, k; +#define LEN 8 + double eigenvector[LEN*LEN]; + double eigenvalue[LEN]; + AVLFG prng; + + av_lfg_init(&prng, 1); + + pca= ff_pca_init(LEN); + + for(i=0; i<9000000; i++){ + double v[2*LEN+100]; + double sum=0; + int pos = av_lfg_get(&prng) % LEN; + int v2 = av_lfg_get(&prng) % 101 - 50; + v[0] = av_lfg_get(&prng) % 101 - 50; + for(j=1; j<8; j++){ + if(j<=pos) v[j]= v[0]; + else v[j]= v2; + sum += v[j]; + } +/* for(j=0; j<LEN; j++){ + v[j] -= v[pos]; + }*/ +// sum += av_lfg_get(&prng) % 10; +/* for(j=0; j<LEN; j++){ + v[j] -= sum/LEN; + }*/ +// lbt1(v+100,v+100,LEN); + ff_pca_add(pca, v); + } + + + ff_pca(pca, eigenvector, eigenvalue); + for(i=0; i<LEN; i++){ + pca->count= 1; + pca->mean[i]= 0; + +// (0.5^|x|)^2 = 0.5^2|x| = 0.25^|x| + + +// pca.covariance[i + i*LEN]= pow(0.5, fabs + for(j=i; j<LEN; j++){ + printf("%f ", pca->covariance[i + j*LEN]); + } + printf("\n"); + } + + for(i=0; i<LEN; i++){ + double v[LEN]; + double error=0; + memset(v, 0, sizeof(v)); + for(j=0; j<LEN; j++){ + for(k=0; k<LEN; k++){ + v[j] += pca->covariance[FFMIN(k,j) + FFMAX(k,j)*LEN] * eigenvector[i + k*LEN]; + } + v[j] /= eigenvalue[i]; + error += fabs(v[j] - eigenvector[i + j*LEN]); + } + printf("%f ", error); + } + printf("\n"); + + for(i=0; i<LEN; i++){ + for(j=0; j<LEN; j++){ + printf("%9.6f ", eigenvector[i + j*LEN]); + } + printf(" %9.1f %f\n", eigenvalue[i], eigenvalue[i]/eigenvalue[0]); + } + + return 0; +} diff --git a/libavutil/tests/pixdesc.c b/libavutil/tests/pixdesc.c new file mode 100644 index 0000000000..7fbfeea96c --- /dev/null +++ b/libavutil/tests/pixdesc.c @@ -0,0 +1,46 @@ +/* + * pixel format descriptor + * Copyright (c) 2009 Michael Niedermayer <michaelni@gmx.at> + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/pixdesc.c" + +int main(void){ + int i; + int err=0; + int skip = 0; + + for (i=0; i<AV_PIX_FMT_NB*2; i++) { + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i); + if(!desc || !desc->name) { + skip ++; + continue; + } + if (skip) { + av_log(NULL, AV_LOG_INFO, "%3d unused pixel format values\n", skip); + skip = 0; + } + av_log(NULL, AV_LOG_INFO, "pix fmt %s avg_bpp:%d colortype:%d\n", desc->name, av_get_padded_bits_per_pixel(desc), get_color_type(desc)); + if ((!(desc->flags & AV_PIX_FMT_FLAG_ALPHA)) != (desc->nb_components != 2 && desc->nb_components != 4)) { + av_log(NULL, AV_LOG_ERROR, "Alpha flag mismatch\n"); + err = 1; + } + } + return err; +} diff --git a/libavutil/tests/pixelutils.c b/libavutil/tests/pixelutils.c new file mode 100644 index 0000000000..ec4dc8fe8f --- /dev/null +++ b/libavutil/tests/pixelutils.c @@ -0,0 +1,152 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/pixelutils.c" + +#define W1 320 +#define H1 240 +#define W2 640 +#define H2 480 + +static int run_single_test(const char *test, + const uint8_t *block1, ptrdiff_t stride1, + const uint8_t *block2, ptrdiff_t stride2, + int align, int n) +{ + int out, ref; + av_pixelutils_sad_fn f_ref = sad_c[n - 1]; + av_pixelutils_sad_fn f_out = av_pixelutils_get_sad_fn(n, n, align, NULL); + + switch (align) { + case 0: block1++; block2++; break; + case 1: block2++; break; + case 2: break; + } + + out = f_out(block1, stride1, block2, stride2); + ref = f_ref(block1, stride1, block2, stride2); + printf("[%s] [%c%c] SAD [%s] %dx%d=%d ref=%d\n", + out == ref ? "OK" : "FAIL", + align ? 'A' : 'U', align == 2 ? 'A' : 'U', + test, 1<<n, 1<<n, out, ref); + return out != ref; +} + +static int run_test(const char *test, + const uint8_t *b1, const uint8_t *b2) +{ + int i, a, ret = 0; + + for (a = 0; a < 3; a++) { + const uint8_t *block1 = b1; + const uint8_t *block2 = b2; + + switch (a) { + case 0: block1++; block2++; break; + case 1: block2++; break; + case 2: break; + } + for (i = 1; i <= FF_ARRAY_ELEMS(sad_c); i++) { + int r = run_single_test(test, b1, W1, b2, W2, a, i); + if (r) + ret = r; + } + } + return ret; +} + +int main(void) +{ + int i, align, ret; + uint8_t *buf1 = av_malloc(W1*H1); + uint8_t *buf2 = av_malloc(W2*H2); + uint32_t state = 0; + + if (!buf1 || !buf2) { + fprintf(stderr, "malloc failure\n"); + ret = 1; + goto end; + } + + ff_check_pixfmt_descriptors(); + +#define RANDOM_INIT(buf, size) do { \ + int k; \ + for (k = 0; k < size; k++) { \ + state = state * 1664525 + 1013904223; \ + buf[k] = state>>24; \ + } \ +} while (0) + + /* Normal test with different strides */ + RANDOM_INIT(buf1, W1*H1); + RANDOM_INIT(buf2, W2*H2); + ret = run_test("random", buf1, buf2); + if (ret < 0) + goto end; + + /* Check for maximum SAD */ + memset(buf1, 0xff, W1*H1); + memset(buf2, 0x00, W2*H2); + ret = run_test("max", buf1, buf2); + if (ret < 0) + goto end; + + /* Check for minimum SAD */ + memset(buf1, 0x90, W1*H1); + memset(buf2, 0x90, W2*H2); + ret = run_test("min", buf1, buf2); + if (ret < 0) + goto end; + + /* Exact buffer sizes, to check for overreads */ + for (i = 1; i <= 4; i++) { + for (align = 0; align < 3; align++) { + int size1, size2; + + av_freep(&buf1); + av_freep(&buf2); + + size1 = size2 = 1 << (i << 1); + + switch (align) { + case 0: size1++; size2++; break; + case 1: size2++; break; + case 2: break; + } + + buf1 = av_malloc(size1); + buf2 = av_malloc(size2); + if (!buf1 || !buf2) { + fprintf(stderr, "malloc failure\n"); + ret = 1; + goto end; + } + RANDOM_INIT(buf1, size1); + RANDOM_INIT(buf2, size2); + ret = run_single_test("small", buf1, 1<<i, buf2, 1<<i, align, i); + if (ret < 0) + goto end; + } + } + +end: + av_free(buf1); + av_free(buf2); + return ret; +} diff --git a/libavutil/tests/random_seed.c b/libavutil/tests/random_seed.c new file mode 100644 index 0000000000..ebe9b3e7dd --- /dev/null +++ b/libavutil/tests/random_seed.c @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2009 Baptiste Coudurier <baptiste.coudurier@gmail.com> + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#define TEST 1 +#include "libavutil/random_seed.c" + +#undef printf +#define N 256 +#include <stdio.h> + +int main(void) +{ + int i, j, retry; + uint32_t seeds[N]; + + for (retry=0; retry<3; retry++){ + for (i=0; i<N; i++){ + seeds[i] = av_get_random_seed(); + for (j=0; j<i; j++) + if (seeds[j] == seeds[i]) + goto retry; + } + printf("seeds OK\n"); + return 0; + retry:; + } + printf("FAIL at %d with %X\n", j, seeds[j]); + return 1; +} diff --git a/libavutil/tests/rational.c b/libavutil/tests/rational.c new file mode 100644 index 0000000000..caec07d7cb --- /dev/null +++ b/libavutil/tests/rational.c @@ -0,0 +1,134 @@ +/* + * rational numbers + * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at> + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/rational.c" +#include "libavutil/integer.h" + +int main(void) +{ + AVRational a,b,r; + int i,j,k; + static const int64_t numlist[] = { + INT64_MIN, INT64_MIN+1, INT64_MAX, INT32_MIN, INT32_MAX, 1,0,-1, + 123456789, INT32_MAX-1, INT32_MAX+1LL, UINT32_MAX-1, UINT32_MAX, UINT32_MAX+1LL + }; + + for (a.num = -2; a.num <= 2; a.num++) { + for (a.den = -2; a.den <= 2; a.den++) { + for (b.num = -2; b.num <= 2; b.num++) { + for (b.den = -2; b.den <= 2; b.den++) { + int c = av_cmp_q(a,b); + double d = av_q2d(a) == av_q2d(b) ? + 0 : (av_q2d(a) - av_q2d(b)); + if (d > 0) d = 1; + else if (d < 0) d = -1; + else if (d != d) d = INT_MIN; + if (c != d) + av_log(NULL, AV_LOG_ERROR, "%d/%d %d/%d, %d %f\n", a.num, + a.den, b.num, b.den, c,d); + r = av_sub_q(av_add_q(b,a), b); + if(b.den && (r.num*a.den != a.num*r.den || !r.num != !a.num || !r.den != !a.den)) + av_log(NULL, AV_LOG_ERROR, "%d/%d ", r.num, r.den); + } + } + } + } + + for (i = 0; i < FF_ARRAY_ELEMS(numlist); i++) { + int64_t a = numlist[i]; + + for (j = 0; j < FF_ARRAY_ELEMS(numlist); j++) { + int64_t b = numlist[j]; + if (b<=0) + continue; + for (k = 0; k < FF_ARRAY_ELEMS(numlist); k++) { + int64_t c = numlist[k]; + int64_t res; + AVInteger ai; + + if (c<=0) + continue; + res = av_rescale_rnd(a,b,c, AV_ROUND_ZERO); + + ai = av_mul_i(av_int2i(a), av_int2i(b)); + ai = av_div_i(ai, av_int2i(c)); + + if (av_cmp_i(ai, av_int2i(INT64_MAX)) > 0 && res == INT64_MIN) + continue; + if (av_cmp_i(ai, av_int2i(INT64_MIN)) < 0 && res == INT64_MIN) + continue; + if (av_cmp_i(ai, av_int2i(res)) == 0) + continue; + + // Special exception for INT64_MIN, remove this in case INT64_MIN is handled without off by 1 error + if (av_cmp_i(ai, av_int2i(res-1)) == 0 && a == INT64_MIN) + continue; + + av_log(NULL, AV_LOG_ERROR, "%"PRId64" * %"PRId64" / %"PRId64" = %"PRId64" or %"PRId64"\n", a,b,c, res, av_i2int(ai)); + } + } + } + + for (a.num = 1; a.num <= 10; a.num++) { + for (a.den = 1; a.den <= 10; a.den++) { + if (av_gcd(a.num, a.den) > 1) + continue; + for (b.num = 1; b.num <= 10; b.num++) { + for (b.den = 1; b.den <= 10; b.den++) { + int start; + if (av_gcd(b.num, b.den) > 1) + continue; + if (av_cmp_q(b, a) < 0) + continue; + for (start = 0; start < 10 ; start++) { + int acc= start; + int i; + + for (i = 0; i<100; i++) { + int exact = start + av_rescale_q(i+1, b, a); + acc = av_add_stable(a, acc, b, 1); + if (FFABS(acc - exact) > 2) { + av_log(NULL, AV_LOG_ERROR, "%d/%d %d/%d, %d %d\n", a.num, + a.den, b.num, b.den, acc, exact); + return 1; + } + } + } + } + } + } + } + + for (a.den = 1; a.den < 0x100000000U/3; a.den*=3) { + for (a.num = -1; a.num < (1<<27); a.num += 1 + a.num/100) { + float f = av_int2float(av_q2intfloat(a)); + float f2 = av_q2d(a); + if (fabs(f - f2) > fabs(f)/5000000) { + av_log(NULL, AV_LOG_ERROR, "%d/%d %f %f\n", a.num, + a.den, f, f2); + return 1; + } + + } + } + + return 0; +} diff --git a/libavutil/tests/ripemd.c b/libavutil/tests/ripemd.c new file mode 100644 index 0000000000..58e5fce344 --- /dev/null +++ b/libavutil/tests/ripemd.c @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2007 Michael Niedermayer <michaelni@gmx.at> + * Copyright (C) 2013 James Almer + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> + +#include "libavutil/mem.h" +#include "libavutil/ripemd.h" + +int main(void) +{ + int i, j, k; + struct AVRIPEMD *ctx; + unsigned char digest[40]; + static const int lengths[4] = { 128, 160, 256, 320 }; + + ctx = av_ripemd_alloc(); + if (!ctx) + return 1; + + for (j = 0; j < 4; j++) { + printf("Testing RIPEMD-%d\n", lengths[j]); + for (k = 0; k < 3; k++) { + av_ripemd_init(ctx, lengths[j]); + if (k == 0) + av_ripemd_update(ctx, "abc", 3); + else if (k == 1) + av_ripemd_update(ctx, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56); + else + for (i = 0; i < 1000*1000; i++) + av_ripemd_update(ctx, "a", 1); + av_ripemd_final(ctx, digest); + for (i = 0; i < lengths[j] >> 3; i++) + printf("%02X", digest[i]); + putchar('\n'); + } + switch (j) { //test vectors (from ISO:IEC 10118-3 (2004) and http://homes.esat.kuleuven.be/~bosselae/ripemd160.html) + case 0: + printf("c14a1219 9c66e4ba 84636b0f 69144c77\n" + "a1aa0689 d0fafa2d dc22e88b 49133a06\n" + "4a7f5723 f954eba1 216c9d8f 6320431f\n"); + break; + case 1: + printf("8eb208f7 e05d987a 9b044a8e 98c6b087 f15a0bfc\n" + "12a05338 4a9c0c88 e405a06c 27dcf49a da62eb2b\n" + "52783243 c1697bdb e16d37f9 7f68f083 25dc1528\n"); + break; + case 2: + printf("afbd6e22 8b9d8cbb cef5ca2d 03e6dba1 0ac0bc7d cbe4680e 1e42d2e9 75459b65\n" + "38430455 83aac6c8 c8d91285 73e7a980 9afb2a0f 34ccc36e a9e72f16 f6368e3f\n" + "ac953744 e10e3151 4c150d4d 8d7b6773 42e33399 788296e4 3ae4850c e4f97978\n"); + break; + case 3: + printf("de4c01b3 054f8930 a79d09ae 738e9230 1e5a1708 5beffdc1 b8d11671 3e74f82f a942d64c dbc4682d\n" + "d034a795 0cf72202 1ba4b84d f769a5de 2060e259 df4c9bb4 a4268c0e 935bbc74 70a969c9 d072a1ac\n" + "bdee37f4 371e2064 6b8b0d86 2dda1629 2ae36f40 965e8c85 09e63d1d bddecc50 3e2b63eb 9245bb66\n"); + break; + } + } + av_free(ctx); + + return 0; +} diff --git a/libavutil/tests/sha.c b/libavutil/tests/sha.c new file mode 100644 index 0000000000..a95b3a001f --- /dev/null +++ b/libavutil/tests/sha.c @@ -0,0 +1,75 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> + +#include "libavutil/mem.h" +#include "libavutil/sha.h" + +int main(void) +{ + int i, j, k; + struct AVSHA *ctx; + unsigned char digest[32]; + static const int lengths[3] = { 160, 224, 256 }; + + ctx = av_sha_alloc(); + if (!ctx) + return 1; + + for (j = 0; j < 3; j++) { + printf("Testing SHA-%d\n", lengths[j]); + for (k = 0; k < 3; k++) { + av_sha_init(ctx, lengths[j]); + if (k == 0) + av_sha_update(ctx, "abc", 3); + else if (k == 1) + av_sha_update(ctx, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56); + else + for (i = 0; i < 1000*1000; i++) + av_sha_update(ctx, "a", 1); + av_sha_final(ctx, digest); + for (i = 0; i < lengths[j] >> 3; i++) + printf("%02X", digest[i]); + putchar('\n'); + } + switch (j) { + case 0: + //test vectors (from FIPS PUB 180-1) + printf("A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D\n" + "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1\n" + "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F\n"); + break; + case 1: + //test vectors (from FIPS PUB 180-2 Appendix A) + printf("23097d22 3405d822 8642a477 bda255b3 2aadbce4 bda0b3f7 e36c9da7\n" + "75388b16 512776cc 5dba5da1 fd890150 b0c6455c b4f58b19 52522525\n" + "20794655 980c91d8 bbb4c1ea 97618a4b f03f4258 1948b2ee 4ee7ad67\n"); + break; + case 2: + //test vectors (from FIPS PUB 180-2) + printf("ba7816bf 8f01cfea 414140de 5dae2223 b00361a3 96177a9c b410ff61 f20015ad\n" + "248d6a61 d20638b8 e5c02693 0c3e6039 a33ce459 64ff2167 f6ecedd4 19db06c1\n" + "cdc76e5c 9914fb92 81a1c7e2 84d73e67 f1809a48 a497200e 046d39cc c7112cd0\n"); + break; + } + } + av_free(ctx); + + return 0; +} diff --git a/libavutil/tests/sha512.c b/libavutil/tests/sha512.c new file mode 100644 index 0000000000..f3b90fdf49 --- /dev/null +++ b/libavutil/tests/sha512.c @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2007 Michael Niedermayer <michaelni@gmx.at> + * Copyright (C) 2009 Konstantin Shishkov + * Copyright (C) 2013 James Almer + * based on BSD-licensed SHA-2 code by Aaron D. Gifford + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> + +#include "libavutil/mem.h" +#include "libavutil/sha512.h" + +int main(void) +{ + int i, j, k; + struct AVSHA512 *ctx; + unsigned char digest[64]; + static const int lengths[4] = { 224, 256, 384, 512 }; + + ctx = av_sha512_alloc(); + if (!ctx) + return 1; + + for (j = 0; j < 4; j++) { + if (j < 2) printf("Testing SHA-512/%d\n", lengths[j]); + else printf("Testing SHA-%d\n", lengths[j]); + for (k = 0; k < 3; k++) { + av_sha512_init(ctx, lengths[j]); + if (k == 0) + av_sha512_update(ctx, "abc", 3); + else if (k == 1) + av_sha512_update(ctx, "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 112); + else + for (i = 0; i < 1000*1000; i++) + av_sha512_update(ctx, "a", 1); + av_sha512_final(ctx, digest); + for (i = 0; i < lengths[j] >> 3; i++) + printf("%02X", digest[i]); + putchar('\n'); + } + switch (j) { //test vectors (from FIPS PUB 180-4 Apendix A) + case 0: + printf("4634270f 707b6a54 daae7530 460842e2 0e37ed26 5ceee9a4 3e8924aa\n" + "23fec5bb 94d60b23 30819264 0b0c4533 35d66473 4fe40e72 68674af9\n" + "37ab331d 76f0d36d e422bd0e deb22a28 accd487b 7a8453ae 965dd287\n"); + break; + case 1: + printf("53048e26 81941ef9 9b2e29b7 6b4c7dab e4c2d0c6 34fc6d46 e0e2f131 07e7af23\n" + "3928e184 fb8690f8 40da3988 121d31be 65cb9d3e f83ee614 6feac861 e19b563a\n" + "9a59a052 930187a9 7038cae6 92f30708 aa649192 3ef51943 94dc68d5 6c74fb21\n"); + break; + case 2: + printf("cb00753f 45a35e8b b5a03d69 9ac65007 272c32ab 0eded163 " + "1a8b605a 43ff5bed 8086072b a1e7cc23 58baeca1 34c825a7\n" + "09330c33 f71147e8 3d192fc7 82cd1b47 53111b17 3b3b05d2 " + "2fa08086 e3b0f712 fcc7c71a 557e2db9 66c3e9fa 91746039\n" + "9d0e1809 716474cb 086e834e 310a4a1c ed149e9c 00f24852 " + "7972cec5 704c2a5b 07b8b3dc 38ecc4eb ae97ddd8 7f3d8985\n"); + break; + case 3: + printf("ddaf35a1 93617aba cc417349 ae204131 12e6fa4e 89a97ea2 0a9eeee6 4b55d39a " + "2192992a 274fc1a8 36ba3c23 a3feebbd 454d4423 643ce80e 2a9ac94f a54ca49f\n" + "8e959b75 dae313da 8cf4f728 14fc143f 8f7779c6 eb9f7fa1 7299aead b6889018 " + "501d289e 4900f7e4 331b99de c4b5433a c7d329ee b6dd2654 5e96e55b 874be909\n" + "e718483d 0ce76964 4e2e42c7 bc15b463 8e1f98b1 3b204428 5632a803 afa973eb " + "de0ff244 877ea60a 4cb0432c e577c31b eb009c5c 2c49aa2e 4eadb217 ad8cc09b\n"); + break; + } + } + av_free(ctx); + + return 0; +} diff --git a/libavutil/tests/softfloat.c b/libavutil/tests/softfloat.c new file mode 100644 index 0000000000..16788d4da9 --- /dev/null +++ b/libavutil/tests/softfloat.c @@ -0,0 +1,156 @@ +/* + * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at> + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <inttypes.h> + +#include "libavutil/softfloat.h" +#include "libavutil/common.h" +#include "libavutil/log.h" + +#include <stdio.h> + +static const SoftFloat FLOAT_0_017776489257 = {0x1234, 12}; +static const SoftFloat FLOAT_1374_40625 = {0xabcd, 25}; +static const SoftFloat FLOAT_0_1249694824218 = {0xFFF, 15}; + + +int main(void){ + SoftFloat one= av_int2sf(1, 0); + SoftFloat sf1, sf2, sf3; + double d1, d2, d3; + int i, j; + av_log_set_level(AV_LOG_DEBUG); + + d1= 1; + for(i= 0; i<10; i++){ + d1= 1/(d1+1); + } + printf("test1 double=%d\n", (int)(d1 * (1<<24))); + + sf1= one; + for(i= 0; i<10; i++){ + sf1= av_div_sf(one, av_normalize_sf(av_add_sf(one, sf1))); + } + printf("test1 sf =%d\n", av_sf2int(sf1, 24)); + + + for(i= 0; i<100; i++){ + START_TIMER + d1= i; + d2= i/100.0; + for(j= 0; j<1000; j++){ + d1= (d1+1)*d2; + } + STOP_TIMER("float add mul") + } + printf("test2 double=%d\n", (int)(d1 * (1<<24))); + + for(i= 0; i<100; i++){ + START_TIMER + sf1= av_int2sf(i, 0); + sf2= av_div_sf(av_int2sf(i, 2), av_int2sf(200, 3)); + for(j= 0; j<1000; j++){ + sf1= av_mul_sf(av_add_sf(sf1, one),sf2); + } + STOP_TIMER("softfloat add mul") + } + printf("test2 sf =%d (%d %d)\n", av_sf2int(sf1, 24), sf1.exp, sf1.mant); + + d1 = 0.0177764893; + d2 = 1374.40625; + d3 = 0.1249694824; + d2 += d1; + d3 += d2; + printf("test3 double: %.10lf\n", d3); + + sf1 = FLOAT_0_017776489257; + sf2 = FLOAT_1374_40625; + sf3 = FLOAT_0_1249694824218; + sf2 = av_add_sf(sf1, sf2); + sf3 = av_add_sf(sf3, sf2); + printf("test3 softfloat: %.10lf (0x%08x %d)\n", (double)av_sf2double(sf3), sf3.mant, sf3.exp); + + sf1 = av_int2sf(0xFFFFFFF0, 0); + printf("test4 softfloat: %.10lf (0x%08x %d)\n", (double)av_sf2double(sf1), sf1.mant, sf1.exp); + sf1 = av_int2sf(0x00000010, 0); + printf("test4 softfloat: %.10lf (0x%08x %d)\n", (double)av_sf2double(sf1), sf1.mant, sf1.exp); + + sf1 = av_int2sf(0x1FFFFFFF, 0); + printf("test4 softfloat: %.10lf (0x%08x %d)\n", (double)av_sf2double(sf1), sf1.mant, sf1.exp); + sf1 = av_int2sf(0xE0000001, 0); + printf("test4 softfloat: %.10lf (0x%08x %d)\n", (double)av_sf2double(sf1), sf1.mant, sf1.exp); + + + sf1 = (SoftFloat){ 0x20000000, MIN_EXP }; + sf1 = av_mul_sf(sf1, sf1); + printf("test5 softfloat: %.10lf (0x%08x %d)\n", (double)av_sf2double(sf1), sf1.mant, sf1.exp); + + sf1 = (SoftFloat){ 0x20000000, MIN_EXP }; + sf2 = (SoftFloat){ 0x20000000, MAX_EXP }; + i = av_cmp_sf(sf1, sf2); + j = av_cmp_sf(sf2, sf1); + sf1 = av_div_sf(sf1, sf2); + printf("test6 softfloat: %.10lf (0x%08x %d) %d %d\n", (double)av_sf2double(sf1), sf1.mant, sf1.exp, i, j); + + for(i= -50; i<50; i++) { + sf1= av_int2sf(i, 0); + for(j= -50; j<50; j++) { + int c; + sf2= av_int2sf(j, 0); + c = av_cmp_sf(sf1, sf2); + if (FFDIFFSIGN(i,j) != c && (FFDIFFSIGN(i,j)^c)<0) { + printf("av_cmp_sf failed at %d %d as %X\n", i, j, c); + } + c = av_gt_sf(sf1, sf2); + if ((i>j) != c) { + printf("av_gt_sf failed at %d %d as %X\n", i, j, c); + } + } + sf1 = av_int2sf(1, i); + for(j = -50; j < 50; j++) { + int c; + sf2 = av_int2sf(1, j); + c = av_cmp_sf(sf2, sf1); + if (FFDIFFSIGN(i,j) != c && (FFDIFFSIGN(i,j)^c) < 0) { + printf("av_cmp_sf failed2 at %d %d as %X\n", i, j, c); + } + c = av_gt_sf(sf1, sf2); + if ((i<j) != c) { + printf("av_gt_sf failed2 at %d %d as %X\n", i, j, c); + } + } + } + + + for(i= 0; i<4*36; i++){ + int s, c; + double errs, errc; + + av_sincos_sf(i*(1ULL<<32)/36/4, &s, &c); + errs = (double)s/ (1<<30) - sin(i*M_PI/36); + errc = (double)c/ (1<<30) - cos(i*M_PI/36); + if (fabs(errs) > 0.00000002 || fabs(errc) >0.001) { + printf("sincos FAIL %d %f %f %f %f\n", i, (float)s/ (1<<30), (float)c/ (1<<30), sin(i*M_PI/36), cos(i*M_PI/36)); + } + + } + return 0; + +} diff --git a/libavutil/tests/tea.c b/libavutil/tests/tea.c new file mode 100644 index 0000000000..605bb524af --- /dev/null +++ b/libavutil/tests/tea.c @@ -0,0 +1,115 @@ +/* + * A 32-bit implementation of the TEA algorithm + * Copyright (c) 2015 Vesselin Bontchev + * + * Loosely based on the implementation of David Wheeler and Roger Needham, + * https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm#Reference_code + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> + +#include "libavutil/common.h" +#include "libavutil/tea.h" + +#define TEA_NUM_TESTS 4 + +// https://github.com/logandrews/TeaCrypt/blob/master/tea/tea_test.go +static const uint8_t tea_test_key[TEA_NUM_TESTS][16] = { + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }, + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }, + { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, + 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF + }, + { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, + 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF + } +}; + +static const uint8_t tea_test_pt[TEA_NUM_TESTS][8] = { + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }, + { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }, + { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF } +}; + +static const uint8_t tea_test_ct[TEA_NUM_TESTS][8] = { + { 0x41, 0xEA, 0x3A, 0x0A, 0x94, 0xBA, 0xA9, 0x40 }, + { 0x6A, 0x2F, 0x9C, 0xF3, 0xFC, 0xCF, 0x3C, 0x55 }, + { 0xDE, 0xB1, 0xC0, 0xA2, 0x7E, 0x74, 0x5D, 0xB3 }, + { 0x12, 0x6C, 0x6B, 0x92, 0xC0, 0x65, 0x3A, 0x3E } +}; + +static void test_tea(struct AVTEA *ctx, uint8_t *dst, const uint8_t *src, + const uint8_t *ref, int len, uint8_t *iv, int dir, + const char *test) +{ + av_tea_crypt(ctx, dst, src, len, iv, dir); + if (memcmp(dst, ref, 8*len)) { + int i; + printf("%s failed\ngot ", test); + for (i = 0; i < 8*len; i++) + printf("%02x ", dst[i]); + printf("\nexpected "); + for (i = 0; i < 8*len; i++) + printf("%02x ", ref[i]); + printf("\n"); + exit(1); + } +} + +int main(void) +{ + struct AVTEA *ctx; + uint8_t buf[8], iv[8]; + int i; + static const uint8_t src[32] = "HelloWorldHelloWorldHelloWorld"; + uint8_t ct[32]; + uint8_t pl[32]; + + ctx = av_tea_alloc(); + if (!ctx) + return 1; + + for (i = 0; i < TEA_NUM_TESTS; i++) { + av_tea_init(ctx, tea_test_key[i], 64); + + test_tea(ctx, buf, tea_test_pt[i], tea_test_ct[i], 1, NULL, 0, "encryption"); + test_tea(ctx, buf, tea_test_ct[i], tea_test_pt[i], 1, NULL, 1, "decryption"); + + /* encrypt */ + memcpy(iv, "HALLO123", 8); + av_tea_crypt(ctx, ct, src, 4, iv, 0); + + /* decrypt into pl */ + memcpy(iv, "HALLO123", 8); + test_tea(ctx, pl, ct, src, 4, iv, 1, "CBC decryption"); + + memcpy(iv, "HALLO123", 8); + test_tea(ctx, ct, ct, src, 4, iv, 1, "CBC inplace decryption"); + } + + printf("Test encryption/decryption success.\n"); + av_free(ctx); + + return 0; +} diff --git a/libavutil/tests/tree.c b/libavutil/tests/tree.c new file mode 100644 index 0000000000..7503228970 --- /dev/null +++ b/libavutil/tests/tree.c @@ -0,0 +1,108 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/tree.c" + +#include <stdint.h> + +#include "libavutil/common.h" +#include "libavutil/lfg.h" +#include "libavutil/log.h" + +static int check(AVTreeNode *t) +{ + if (t) { + int left = check(t->child[0]); + int right = check(t->child[1]); + + if (left > 999 || right > 999) + return 1000; + if (right - left != t->state) + return 1000; + if (t->state > 1 || t->state < -1) + return 1000; + return FFMAX(left, right) + 1; + } + return 0; +} + +static void print(AVTreeNode *t, int depth) +{ + int i; + for (i = 0; i < depth * 4; i++) + av_log(NULL, AV_LOG_ERROR, " "); + if (t) { + av_log(NULL, AV_LOG_ERROR, "Node %p %2d %p\n", t, t->state, t->elem); + print(t->child[0], depth + 1); + print(t->child[1], depth + 1); + } else + av_log(NULL, AV_LOG_ERROR, "NULL\n"); +} + +static int cmp(const void *a, const void *b) +{ + return (const uint8_t *) a - (const uint8_t *) b; +} + +int main(int argc, char **argv) +{ + int i; + void *k; + AVTreeNode *root = NULL, *node = NULL; + AVLFG prng; + int log_level = argc <= 1 ? AV_LOG_INFO : atoi(argv[1]); + + av_log_set_level(log_level); + + av_lfg_init(&prng, 1); + + for (i = 0; i < 10000; i++) { + intptr_t j = av_lfg_get(&prng) % 86294; + + if (check(root) > 999) { + av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i); + print(root, 0); + return 1; + } + av_log(NULL, AV_LOG_DEBUG, "inserting %4d\n", (int)j); + + if (!node) + node = av_tree_node_alloc(); + if (!node) { + av_log(NULL, AV_LOG_ERROR, "Memory allocation failure.\n"); + return 1; + } + av_tree_insert(&root, (void *)(j + 1), cmp, &node); + + j = av_lfg_get(&prng) % 86294; + { + AVTreeNode *node2 = NULL; + av_log(NULL, AV_LOG_DEBUG, "removing %4d\n", (int)j); + av_tree_insert(&root, (void *)(j + 1), cmp, &node2); + k = av_tree_find(root, (void *)(j + 1), cmp, NULL); + if (k) + av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i); + av_free(node2); + } + } + av_free(node); + + av_tree_destroy(root); + + return 0; +} diff --git a/libavutil/tests/twofish.c b/libavutil/tests/twofish.c new file mode 100644 index 0000000000..74e0926eaf --- /dev/null +++ b/libavutil/tests/twofish.c @@ -0,0 +1,94 @@ +/* + * An implementation of the TwoFish algorithm + * Copyright (c) 2015 Supraja Meedinti + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/log.h" +#include "libavutil/twofish.h" + +#include <stdio.h> +#include <stdlib.h> + +int main(int argc, char *argv[]) +{ + uint8_t Key[32] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff + }; + const uint8_t rct[6][16] = { + {0x9f, 0x58, 0x9f, 0x5c, 0xf6, 0x12, 0x2c, 0x32, 0xb6, 0xbf, 0xec, 0x2f, 0x2a, 0xe8, 0xc3, 0x5a}, + {0xcf, 0xd1, 0xd2, 0xe5, 0xa9, 0xbe, 0x9c, 0xdf, 0x50, 0x1f, 0x13, 0xb8, 0x92, 0xbd, 0x22, 0x48}, + {0x37, 0x52, 0x7b, 0xe0, 0x05, 0x23, 0x34, 0xb8, 0x9f, 0x0c, 0xfc, 0xca, 0xe8, 0x7c, 0xfa, 0x20}, + {0x5d, 0x9d, 0x4e, 0xef, 0xfa, 0x91, 0x51, 0x57, 0x55, 0x24, 0xf1, 0x15, 0x81, 0x5a, 0x12, 0xe0}, + {0xe7, 0x54, 0x49, 0x21, 0x2b, 0xee, 0xf9, 0xf4, 0xa3, 0x90, 0xbd, 0x86, 0x0a, 0x64, 0x09, 0x41}, + {0x37, 0xfe, 0x26, 0xff, 0x1c, 0xf6, 0x61, 0x75, 0xf5, 0xdd, 0xf4, 0xc3, 0x3b, 0x97, 0xa2, 0x05} + }; + uint8_t temp[32], iv[16], rpt[32] = {0}; + const int kbits[3] = {128, 192, 256}; + int i, j, err = 0; + struct AVTWOFISH *cs; + cs = av_twofish_alloc(); + if (!cs) + return 1; + for (j = 1; j < 3; j++) { + av_twofish_init(cs, Key, kbits[j]); + av_twofish_crypt(cs, temp, rpt, 1, NULL, 0); + for (i = 0; i < 16; i++) { + if (rct[j][i] != temp[i]) { + av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rct[j][i], temp[i]); + err = 1; + } + } + av_twofish_crypt(cs, temp, rct[j], 1, NULL, 1); + for (i = 0; i < 16; i++) { + if (rpt[i] != temp[i]) { + av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rpt[i], temp[i]); + err = 1; + } + } + } + for (j = 0; j < 3; j++) { + memset(Key, 0, sizeof(Key)); + memset(rpt, 0, sizeof(rpt)); + for (i = 1; i < 50; i++) { + av_twofish_init(cs, Key, kbits[j]); + av_twofish_crypt(cs, temp, rpt, 1, NULL, 0); + memcpy(Key+16,Key,(kbits[j]-128) >> 3); + memcpy(Key,rpt,16); + memcpy(rpt,temp,16); + } + for (i = 0; i < 16; i++) { + if (rct[3 + j][i] != temp[i]) { + av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rct[3 + j][i], temp[i]); + err = 1; + } + } + } + memset(rpt, 0, sizeof(rpt)); + memcpy(iv, "HALLO123HALLO123", 16); + av_twofish_crypt(cs, temp, rpt, 2, iv, 0); + memcpy(iv, "HALLO123HALLO123", 16); + av_twofish_crypt(cs, temp, temp, 2, iv, 1); + for (i = 0; i < 32; i++) { + if (rpt[i] != temp[i]) { + av_log(NULL, AV_LOG_ERROR, "%d %02x %02x\n", i, rpt[i], temp[i]); + err = 1; + } + } + av_free(cs); + return err; +} diff --git a/libavutil/tests/utf8.c b/libavutil/tests/utf8.c new file mode 100644 index 0000000000..37a2802b5f --- /dev/null +++ b/libavutil/tests/utf8.c @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2013 Stefano Sabatini + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> + +#include "libavutil/avstring.h" +#include "libavutil/file.h" + +static void print_sequence(const char *p, int l, int indent) +{ + int i; + for (i = 0; i < l; i++) + printf("%02X", (uint8_t)p[i]); + printf("%*s", indent-l*2, ""); +} + +int main(int argc, char **argv) +{ + int ret; + char *filename = argv[1]; + uint8_t *file_buf; + size_t file_buf_size; + uint32_t code; + const uint8_t *p, *endp; + + ret = av_file_map(filename, &file_buf, &file_buf_size, 0, NULL); + if (ret < 0) + return 1; + + p = file_buf; + endp = file_buf + file_buf_size; + while (p < endp) { + int l, r; + const uint8_t *p0 = p; + code = UINT32_MAX; + r = av_utf8_decode(&code, &p, endp, 0); + l = (int)(p-p0); + print_sequence(p0, l, 20); + if (code != UINT32_MAX) { + printf("%-10d 0x%-10X %-5d ", code, code, l); + if (r >= 0) { + if (*p0 == '\n') printf("\\n\n"); + else printf ("%.*s\n", l, p0); + } else { + printf("invalid code range\n"); + } + } else { + printf("invalid sequence\n"); + } + } + + av_file_unmap(file_buf, file_buf_size); + return 0; +} diff --git a/libavutil/tests/xtea.c b/libavutil/tests/xtea.c new file mode 100644 index 0000000000..a974600a25 --- /dev/null +++ b/libavutil/tests/xtea.c @@ -0,0 +1,121 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "libavutil/intreadwrite.h" +#include "libavutil/xtea.h" + +#define XTEA_NUM_TESTS 6 + +static const uint8_t xtea_test_key[XTEA_NUM_TESTS][16] = { + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } +}; + +static const uint8_t xtea_test_pt[XTEA_NUM_TESTS][8] = { + { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 }, + { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, + { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f }, + { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 }, + { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, + { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 } +}; + +static const uint8_t xtea_test_ct[XTEA_NUM_TESTS][8] = { + { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 }, + { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 }, + { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, + { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 }, + { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d }, + { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 } +}; + +static void test_xtea(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, + const uint8_t *ref, int len, uint8_t *iv, int dir, + const char *test, + void (*crypt)(AVXTEA *, uint8_t *, const uint8_t *, int, uint8_t *, int)) +{ + crypt(ctx, dst, src, len, iv, dir); + if (memcmp(dst, ref, 8*len)) { + int i; + printf("%s failed\ngot ", test); + for (i = 0; i < 8*len; i++) + printf("%02x ", dst[i]); + printf("\nexpected "); + for (i = 0; i < 8*len; i++) + printf("%02x ", ref[i]); + printf("\n"); + exit(1); + } +} + +int main(void) +{ + AVXTEA ctx; + uint8_t buf[16], iv[8]; + int i, j; + static const uint8_t src[32] = "HelloWorldHelloWorldHelloWorld"; + uint8_t ct[32]; + uint8_t pl[32]; + + for (i = 0; i < XTEA_NUM_TESTS; i++) { + av_xtea_init(&ctx, xtea_test_key[i]); + + test_xtea(&ctx, buf, xtea_test_pt[i], xtea_test_ct[i], 1, NULL, 0, "encryption", av_xtea_crypt); + test_xtea(&ctx, buf, xtea_test_ct[i], xtea_test_pt[i], 1, NULL, 1, "decryption", av_xtea_crypt); + + for (j = 0; j < 4; j++) + AV_WL32(&buf[4*j], AV_RB32(&xtea_test_key[i][4*j])); + av_xtea_le_init(&ctx, buf); + for (j = 0; j < 2; j++) { + AV_WL32(&ct[4*j], AV_RB32(&xtea_test_ct[i][4*j])); + AV_WL32(&pl[4*j], AV_RB32(&xtea_test_pt[i][4*j])); + } + test_xtea(&ctx, buf, pl, ct, 1, NULL, 0, "encryption", av_xtea_le_crypt); + test_xtea(&ctx, buf, ct, pl, 1, NULL, 1, "decryption", av_xtea_le_crypt); + + /* encrypt */ + memcpy(iv, "HALLO123", 8); + av_xtea_crypt(&ctx, ct, src, 4, iv, 0); + + /* decrypt into pl */ + memcpy(iv, "HALLO123", 8); + test_xtea(&ctx, pl, ct, src, 4, iv, 1, "CBC decryption", av_xtea_crypt); + + memcpy(iv, "HALLO123", 8); + test_xtea(&ctx, ct, ct, src, 4, iv, 1, "CBC inplace decryption", av_xtea_crypt); + } + + printf("Test encryption/decryption success.\n"); + + return 0; +} |