diff options
Diffstat (limited to 'libavutil/tests')
49 files changed, 3384 insertions, 282 deletions
diff --git a/libavutil/tests/.gitignore b/libavutil/tests/.gitignore index c4d1d065fb..ae7a0eabb3 100644 --- a/libavutil/tests/.gitignore +++ b/libavutil/tests/.gitignore @@ -1,21 +1,48 @@ /adler32 /aes +/aes_ctr /atomic +/audio_fifo /avstring /base64 /blowfish +/bprint +/camellia +/cast5 +/color_utils /cpu +/cpu_init /crc /des +/dict +/display +/error /eval /fifo +/file /float_dsp +/hash /hmac +/imgutils /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 index 323690056c..511bf1e401 100644 --- a/libavutil/tests/adler32.c +++ b/libavutil/tests/adler32.c @@ -1,21 +1,22 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * 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" @@ -29,7 +30,7 @@ static volatile int checksum; int main(int argc, char **argv) { int i; - char data[LEN]; + uint8_t data[LEN]; av_log_set_level(AV_LOG_DEBUG); @@ -49,3 +50,4 @@ int main(int argc, char **argv) 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 index 49ca553e0a..1291ad6633 100644 --- a/libavutil/tests/aes.c +++ b/libavutil/tests/aes.c @@ -1,32 +1,33 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * 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/aes.c" - +// 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; - AVAES b; + struct AVAES *b; static const uint8_t rkey[2][16] = { { 0 }, { 0x10, 0xa5, 0x88, 0x69, 0xd7, 0x4b, 0xe5, 0xa3, @@ -43,14 +44,20 @@ int main(int argc, char **argv) { 0x6d, 0x25, 0x1e, 0x69, 0x44, 0xb0, 0x51, 0xe0, 0x4e, 0xaa, 0x6f, 0xb4, 0xdb, 0xf7, 0x84, 0x65 } }; - uint8_t pt[16], temp[16]; + 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); + 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", @@ -59,25 +66,42 @@ int main(int argc, char **argv) } } } + av_free(b); if (argc > 1 && !strcmp(argv[1], "-t")) { - AVAES ae, ad; + struct AVAES *ae, *ad; AVLFG prng; - av_aes_init(&ae, "PI=3.141592654..", 128, 0); - av_aes_init(&ad, "PI=3.141592654..", 128, 1); + 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 < 16; j++) + 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, 1, NULL, 0); + 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, 1, NULL, 1); + av_aes_crypt(ad, temp, temp, 2, NULL, 1); STOP_TIMER("aes"); } for (j = 0; j < 16; j++) { @@ -87,6 +111,9 @@ int main(int argc, char **argv) } } } + av_free(ae); + av_free(ad); } return err; } +// LCOV_EXCL_STOP diff --git a/libavutil/tests/aes_ctr.c b/libavutil/tests/aes_ctr.c new file mode 100644 index 0000000000..c5ebeda7ac --- /dev/null +++ b/libavutil/tests/aes_ctr.c @@ -0,0 +1,65 @@ +/* + * 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/mem.h" +#include "libavutil/aes_ctr.h" + +static const DECLARE_ALIGNED(8, uint8_t, plain)[] = { + 0x6d, 0x6f, 0x73, 0x74, 0x20, 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d +}; +static DECLARE_ALIGNED(8, uint8_t, tmp)[11]; + +int main (void) +{ + int ret = 1; + struct AVAESCTR *ae, *ad; + const uint8_t *iv; + + ae = av_aes_ctr_alloc(); + ad = av_aes_ctr_alloc(); + + if (!ae || !ad) + goto ERROR; + + if (av_aes_ctr_init(ae, (const uint8_t*)"0123456789abcdef") < 0) + goto ERROR; + + if (av_aes_ctr_init(ad, (const uint8_t*)"0123456789abcdef") < 0) + goto ERROR; + + av_aes_ctr_set_random_iv(ae); + iv = av_aes_ctr_get_iv(ae); + av_aes_ctr_set_iv(ad, iv); + + av_aes_ctr_crypt(ae, tmp, plain, sizeof(tmp)); + av_aes_ctr_crypt(ad, tmp, tmp, sizeof(tmp)); + + if (memcmp(tmp, plain, sizeof(tmp)) != 0){ + av_log(NULL, AV_LOG_ERROR, "test failed\n"); + goto ERROR; + } + + av_log(NULL, AV_LOG_INFO, "test passed\n"); + ret = 0; + +ERROR: + av_aes_ctr_free(ae); + av_aes_ctr_free(ad); + return ret; +} diff --git a/libavutil/tests/atomic.c b/libavutil/tests/atomic.c index 70636f42fb..e41bf5a2b9 100644 --- a/libavutil/tests/atomic.c +++ b/libavutil/tests/atomic.c @@ -1,35 +1,38 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include <assert.h> - #include "libavutil/atomic.h" +#include "libavutil/avassert.h" int main(void) { - volatile int val = 1; + volatile int val = 1; + void *tmp1 = (int *)&val; + void * volatile *tmp2 = &tmp1; int res; res = avpriv_atomic_int_add_and_fetch(&val, 1); - assert(res == 2); + av_assert0(res == 2); avpriv_atomic_int_set(&val, 3); res = avpriv_atomic_int_get(&val); - assert(res == 3); + av_assert0(res == 3); + avpriv_atomic_ptr_cas(tmp2, tmp1, &res); + av_assert0(*tmp2 == &res); return 0; } diff --git a/libavutil/tests/audio_fifo.c b/libavutil/tests/audio_fifo.c new file mode 100644 index 0000000000..e107166eb1 --- /dev/null +++ b/libavutil/tests/audio_fifo.c @@ -0,0 +1,200 @@ +/* + * 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 <stdlib.h> +#include <stdio.h> +#include <inttypes.h> +#include "libavutil/mem.h" +#include "libavutil/audio_fifo.c" + +#define MAX_CHANNELS 32 + + +typedef struct TestStruct { + const enum AVSampleFormat format; + const int nb_ch; + void const *data_planes[MAX_CHANNELS]; + const int nb_samples_pch; +} TestStruct; + +static const uint8_t data_U8 [] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; +static const int16_t data_S16[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; +static const float data_FLT[] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0}; + +static const TestStruct test_struct[] = { + {.format = AV_SAMPLE_FMT_U8 , .nb_ch = 1, .data_planes = {data_U8 , }, .nb_samples_pch = 12}, + {.format = AV_SAMPLE_FMT_U8P , .nb_ch = 2, .data_planes = {data_U8 , data_U8 +6, }, .nb_samples_pch = 6 }, + {.format = AV_SAMPLE_FMT_S16 , .nb_ch = 1, .data_planes = {data_S16, }, .nb_samples_pch = 12}, + {.format = AV_SAMPLE_FMT_S16P , .nb_ch = 2, .data_planes = {data_S16, data_S16+6, }, .nb_samples_pch = 6 }, + {.format = AV_SAMPLE_FMT_FLT , .nb_ch = 1, .data_planes = {data_FLT, }, .nb_samples_pch = 12}, + {.format = AV_SAMPLE_FMT_FLTP , .nb_ch = 2, .data_planes = {data_FLT, data_FLT+6, }, .nb_samples_pch = 6 } +}; + +static void free_data_planes(AVAudioFifo *afifo, void **output_data) +{ + int i; + for (i = 0; i < afifo->nb_buffers; ++i){ + av_freep(&output_data[i]); + } + av_freep(&output_data); +} + +static void ERROR(const char *str) +{ + fprintf(stderr, "%s\n", str); + exit(1); +} + +static void print_audio_bytes(const TestStruct *test_sample, void **data_planes, int nb_samples) +{ + int p, b, f; + int byte_offset = av_get_bytes_per_sample(test_sample->format); + int buffers = av_sample_fmt_is_planar(test_sample->format) + ? test_sample->nb_ch : 1; + int line_size = (buffers > 1) ? nb_samples * byte_offset + : nb_samples * byte_offset * test_sample->nb_ch; + for (p = 0; p < buffers; ++p){ + for(b = 0; b < line_size; b+=byte_offset){ + for (f = 0; f < byte_offset; f++){ + int order = !HAVE_BIGENDIAN ? (byte_offset - f - 1) : f; + printf("%02x", *((uint8_t*)data_planes[p] + b + order)); + } + putchar(' '); + } + putchar('\n'); + } +} + +static int read_samples_from_audio_fifo(AVAudioFifo* afifo, void ***output, int nb_samples) +{ + int i; + int samples = FFMIN(nb_samples, afifo->nb_samples); + int tot_elements = !av_sample_fmt_is_planar(afifo->sample_fmt) + ? samples : afifo->channels * samples; + void **data_planes = av_malloc_array(afifo->nb_buffers, sizeof(void*)); + if (!data_planes) + ERROR("failed to allocate memory!"); + if (*output) + free_data_planes(afifo, *output); + *output = data_planes; + + for (i = 0; i < afifo->nb_buffers; ++i){ + data_planes[i] = av_malloc_array(tot_elements, afifo->sample_size); + if (!data_planes[i]) + ERROR("failed to allocate memory!"); + } + + return av_audio_fifo_read(afifo, *output, nb_samples); +} + +static int write_samples_to_audio_fifo(AVAudioFifo* afifo, const TestStruct *test_sample, + int nb_samples, int offset) +{ + int offset_size, i; + void *data_planes[MAX_CHANNELS]; + + if(nb_samples > test_sample->nb_samples_pch - offset){ + return 0; + } + if(offset >= test_sample->nb_samples_pch){ + return 0; + } + offset_size = offset * afifo->sample_size; + + for (i = 0; i < afifo->nb_buffers ; ++i){ + data_planes[i] = (uint8_t*)test_sample->data_planes[i] + offset_size; + } + + return av_audio_fifo_write(afifo, data_planes, nb_samples); +} + +static void test_function(const TestStruct *test_sample) +{ + int ret, i; + void **output_data = NULL; + AVAudioFifo *afifo = av_audio_fifo_alloc(test_sample->format, test_sample->nb_ch, + test_sample->nb_samples_pch); + if (!afifo) { + ERROR("ERROR: av_audio_fifo_alloc returned NULL!"); + } + ret = write_samples_to_audio_fifo(afifo, test_sample, test_sample->nb_samples_pch, 0); + if (ret < 0){ + ERROR("ERROR: av_audio_fifo_write failed!"); + } + printf("written: %d\n", ret); + + ret = write_samples_to_audio_fifo(afifo, test_sample, test_sample->nb_samples_pch, 0); + if (ret < 0){ + ERROR("ERROR: av_audio_fifo_write failed!"); + } + printf("written: %d\n", ret); + printf("remaining samples in audio_fifo: %d\n\n", av_audio_fifo_size(afifo)); + + ret = read_samples_from_audio_fifo(afifo, &output_data, test_sample->nb_samples_pch); + if (ret < 0){ + ERROR("ERROR: av_audio_fifo_read failed!"); + } + printf("read: %d\n", ret); + print_audio_bytes(test_sample, output_data, ret); + printf("remaining samples in audio_fifo: %d\n\n", av_audio_fifo_size(afifo)); + + /* test av_audio_fifo_peek */ + ret = av_audio_fifo_peek(afifo, output_data, afifo->nb_samples); + if (ret < 0){ + ERROR("ERROR: av_audio_fifo_peek failed!"); + } + printf("peek:\n"); + print_audio_bytes(test_sample, output_data, ret); + printf("\n"); + + /* test av_audio_fifo_peek_at */ + printf("peek_at:\n"); + for (i = 0; i < afifo->nb_samples; ++i){ + ret = av_audio_fifo_peek_at(afifo, output_data, 1, i); + if (ret < 0){ + ERROR("ERROR: av_audio_fifo_peek_at failed!"); + } + printf("%d:\n", i); + print_audio_bytes(test_sample, output_data, ret); + } + printf("\n"); + + /* test av_audio_fifo_drain */ + ret = av_audio_fifo_drain(afifo, afifo->nb_samples); + if (ret < 0){ + ERROR("ERROR: av_audio_fifo_drain failed!"); + } + if (afifo->nb_samples){ + ERROR("drain failed to flush all samples in audio_fifo!"); + } + + /* deallocate */ + free_data_planes(afifo, output_data); + av_audio_fifo_free(afifo); +} + +int main(void) +{ + int t, tests = sizeof(test_struct)/sizeof(test_struct[0]); + + for (t = 0; t < tests; ++t){ + printf("\nTEST: %d\n\n", t+1); + test_function(&test_struct[t]); + } + return 0; +} diff --git a/libavutil/tests/avstring.c b/libavutil/tests/avstring.c index 2e5ee2d44a..14bc7ffcea 100644 --- a/libavutil/tests/avstring.c +++ b/libavutil/tests/avstring.c @@ -1,18 +1,18 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -25,7 +25,8 @@ int main(void) { int i; - static const char *strings[] = { + char *fullpath, *ptr; + static const char * const strings[] = { "''", "", ":", @@ -53,6 +54,8 @@ int main(void) "'\\fo\\o:': blahblah", "\\'fo\\o\\:': foo ' :blahblah" }; + const char *haystack = "Education consists mainly in what we have unlearned."; + const char * const needle[] = {"learned.", "unlearned.", "Unlearned"}; printf("Testing av_get_token()\n"); for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) { @@ -65,5 +68,42 @@ int main(void) 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"); + + /*Testing av_strnstr()*/ + #define TEST_STRNSTR(haystack, needle, hay_length, expected) \ + ptr = av_strnstr(haystack, needle, hay_length); \ + if (ptr != expected){ \ + printf("expected: %p, received %p\n", expected, ptr); \ + } + TEST_STRNSTR(haystack, needle [0], strlen(haystack), haystack+44); + TEST_STRNSTR(haystack, needle [1], strlen(haystack), haystack+42); + TEST_STRNSTR(haystack, needle [2], strlen(haystack), NULL ); + TEST_STRNSTR(haystack, strings[1], strlen(haystack), haystack ); + + /*Testing av_d2str()*/ + #define TEST_D2STR(value, expected) \ + if((ptr = av_d2str(value)) == NULL){ \ + printf("error, received null pointer!\n"); \ + } else { \ + if(strcmp(ptr, expected) != 0) \ + printf( "expected: %s, received: %s\n", expected, ptr); \ + av_free(ptr); \ + } + TEST_D2STR(0 , "0.000000"); + TEST_D2STR(-1.2333234, "-1.233323"); + TEST_D2STR(-1.2333237, "-1.233324"); return 0; } diff --git a/libavutil/tests/base64.c b/libavutil/tests/base64.c index 16ea399788..88fd55c220 100644 --- a/libavutil/tests/base64.c +++ b/libavutil/tests/base64.c @@ -1,26 +1,28 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * 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 @@ -42,21 +44,40 @@ static int test_encode_decode(const uint8_t *data, unsigned int data_size, return 1; } - if ((data2_size = av_base64_decode(data2, encoded, max_data2_size)) < 0) { + 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(void) +int main(int argc, char ** argv) { int i, error_count = 0; struct test { @@ -72,13 +93,36 @@ int main(void) { "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 index 301c616aa3..f4c9ced9b4 100644 --- a/libavutil/tests/blowfish.c +++ b/libavutil/tests/blowfish.c @@ -1,18 +1,18 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ 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 index 176b7eb030..f02a54cbbb 100644 --- a/libavutil/tests/cpu.c +++ b/libavutil/tests/cpu.c @@ -1,36 +1,35 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * 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> -#elif !HAVE_GETOPT +#endif +#if !HAVE_GETOPT #include "compat/getopt.c" #endif -#include <stdint.h> -#include <stdio.h> - -#include "libavutil/avstring.h" -#include "libavutil/common.h" -#include "libavutil/cpu.h" - static const struct { int flag; const char *name; @@ -47,6 +46,7 @@ static const struct { { 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 @@ -54,9 +54,9 @@ static const struct { { AV_CPU_FLAG_MMXEXT, "mmxext" }, { AV_CPU_FLAG_SSE, "sse" }, { AV_CPU_FLAG_SSE2, "sse2" }, - { AV_CPU_FLAG_SSE2SLOW, "sse2(slow)" }, + { AV_CPU_FLAG_SSE2SLOW, "sse2slow" }, { AV_CPU_FLAG_SSE3, "sse3" }, - { AV_CPU_FLAG_SSE3SLOW, "sse3(slow)" }, + { AV_CPU_FLAG_SSE3SLOW, "sse3slow" }, { AV_CPU_FLAG_SSSE3, "ssse3" }, { AV_CPU_FLAG_ATOM, "atom" }, { AV_CPU_FLAG_SSE4, "sse4.1" }, @@ -72,6 +72,7 @@ static const struct { { AV_CPU_FLAG_AVX2, "avx2" }, { AV_CPU_FLAG_BMI1, "bmi1" }, { AV_CPU_FLAG_BMI2, "bmi2" }, + { AV_CPU_FLAG_AESNI, "aesni" }, #endif { 0 } }; @@ -80,12 +81,12 @@ static void print_cpu_flags(int cpu_flags, const char *type) { int i; - fprintf(stderr, "cpu_flags(%s) = 0x%08X\n", type, cpu_flags); - fprintf(stderr, "cpu_flags_str(%s) =", type); + 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) - fprintf(stderr, " %s", cpu_flag_tab[i].name); - fprintf(stderr, "\n"); + printf(" %s", cpu_flag_tab[i].name); + printf("\n"); } @@ -95,6 +96,15 @@ int main(int argc, char **argv) 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; @@ -106,10 +116,11 @@ int main(int argc, char **argv) switch (c) { case 'c': { - int cpuflags = av_parse_cpu_flags(optarg); - if (cpuflags < 0) + unsigned flags = av_get_cpu_flags(); + if (av_parse_cpu_caps(&flags, optarg) < 0) return 2; - av_set_cpu_flags_mask(cpuflags); + + av_force_cpu_flags(flags); break; } case 't': @@ -130,7 +141,7 @@ int main(int argc, char **argv) print_cpu_flags(cpu_flags_raw, "raw"); print_cpu_flags(cpu_flags_eff, "effective"); - fprintf(stderr, "threads = %s (cpu_count = %d)\n", threads, cpu_count); + printf("threads = %s (cpu_count = %d)\n", threads, cpu_count); return 0; } diff --git a/libavutil/tests/cpu_init.c b/libavutil/tests/cpu_init.c new file mode 100644 index 0000000000..a379e47b13 --- /dev/null +++ b/libavutil/tests/cpu_init.c @@ -0,0 +1,65 @@ +/* + * 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 + */ + +/* + * This test program tests whether the one-time initialization in + * av_get_cpu_flags() has data races. + */ + +#include <stdio.h> +#include <string.h> + +#include "libavutil/cpu.h" +#include "libavutil/thread.h" + +static void *thread_main(void *arg) +{ + int *flags = arg; + + *flags = av_get_cpu_flags(); + return NULL; +} + +int main(void) +{ + int cpu_flags1; + int cpu_flags2; + int ret; + pthread_t thread1; + pthread_t thread2; + + if ((ret = pthread_create(&thread1, NULL, thread_main, &cpu_flags1))) { + fprintf(stderr, "pthread_create failed: %s.\n", strerror(ret)); + return 1; + } + if ((ret = pthread_create(&thread2, NULL, thread_main, &cpu_flags2))) { + fprintf(stderr, "pthread_create failed: %s.\n", strerror(ret)); + return 1; + } + pthread_join(thread1, NULL); + pthread_join(thread2, NULL); + + if (cpu_flags1 < 0) + return 2; + if (cpu_flags2 < 0) + return 2; + if (cpu_flags1 != cpu_flags2) + return 3; + + return 0; +} diff --git a/libavutil/tests/crc.c b/libavutil/tests/crc.c index c2c609fba8..9825d6bec9 100644 --- a/libavutil/tests/crc.c +++ b/libavutil/tests/crc.c @@ -1,18 +1,18 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -25,19 +25,20 @@ int main(void) { uint8_t buf[1999]; int i; - static const int p[5][3] = { + static const unsigned p[6][3] = { { AV_CRC_32_IEEE_LE, 0xEDB88320, 0x3D5CDD04 }, - { AV_CRC_32_IEEE, 0x04C11DB7, 0xC0F5BAE0 }, - { AV_CRC_16_ANSI_LE, 0xA001, 0xBFD8 }, - { AV_CRC_16_ANSI, 0x8005, 0x1FBB }, - { AV_CRC_8_ATM, 0x07, 0xE3 } + { 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 < 5; 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))); } diff --git a/libavutil/tests/des.c b/libavutil/tests/des.c index 1e7a2fa50a..ed04fad519 100644 --- a/libavutil/tests/des.c +++ b/libavutil/tests/des.c @@ -1,18 +1,18 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -98,10 +98,10 @@ int main(void) key[1] = rand64(); key[2] = rand64(); data = rand64(); - av_des_init(&d, key, 192, 0); - av_des_crypt(&d, &ct, &data, 1, NULL, 0); - av_des_init(&d, key, 192, 1); - av_des_crypt(&d, &ct, &ct, 1, NULL, 1); + 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; 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 index 818c59ccdb..2a1afcc4dc 100644 --- a/libavutil/tests/eval.c +++ b/libavutil/tests/eval.c @@ -1,18 +1,18 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -108,35 +108,73 @@ int main(int argc, char **argv) "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); - av_expr_parse_and_eval(&d, *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"); } - av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", + 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); - av_expr_parse_and_eval(&d, "80G/80Gi", + 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; - av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", + 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"); } } diff --git a/libavutil/tests/fifo.c b/libavutil/tests/fifo.c index 4c4b37a501..8a550e088b 100644 --- a/libavutil/tests/fifo.c +++ b/libavutil/tests/fifo.c @@ -1,30 +1,30 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * 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 <stdlib.h> #include "libavutil/fifo.h" int main(void) { /* create a FIFO buffer */ AVFifoBuffer *fifo = av_fifo_alloc(13 * sizeof(int)); - int i, j, n; + int i, j, n, *p; /* fill data */ for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++) @@ -38,6 +38,32 @@ int main(void) } 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"); + + /* generic peek at FIFO */ + + n = av_fifo_size(fifo); + p = malloc(n); + if (p == NULL) { + fprintf(stderr, "failed to allocate memory.\n"); + exit(1); + } + + (void) av_fifo_generic_peek(fifo, p, n, NULL); + + /* read data at p */ + n /= sizeof(int); + for(i = 0; i < n; ++i) + printf("%d: %d\n", i, p[i]); + + putchar('\n'); + /* read data */ for (i = 0; av_fifo_size(fifo) >= sizeof(int); i++) { av_fifo_generic_read(fifo, &j, sizeof(int), NULL); @@ -45,7 +71,39 @@ int main(void) } 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); + } + putchar('\n'); + + /* test fifo_grow */ + (void) av_fifo_grow(fifo, 15 * sizeof(int)); + + /* fill data */ + n = av_fifo_size(fifo) / sizeof(int); + for (i = n; 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); + free(p); 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 index ab6bf6a569..053324dd38 100644 --- a/libavutil/tests/float_dsp.c +++ b/libavutil/tests/float_dsp.c @@ -1,26 +1,36 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * 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" @@ -242,9 +252,9 @@ static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext * int main(int argc, char **argv) { - int ret = 0; + int ret = 0, seeded = 0; uint32_t seed; - AVFloatDSPContext fdsp, cdsp; + AVFloatDSPContext *fdsp, *cdsp; AVLFG lfg; LOCAL_ALIGNED(32, float, src0, [LEN]); @@ -253,12 +263,40 @@ int main(int argc, char **argv) LOCAL_ALIGNED(32, double, dbl_src0, [LEN]); LOCAL_ALIGNED(32, double, dbl_src1, [LEN]); - if (argc > 2 && !strcmp(argv[1], "-s")) - seed = strtoul(argv[2], NULL, 10); - else + 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: random seed %u\n", 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); @@ -269,28 +307,27 @@ int main(int argc, char **argv) fill_double_array(&lfg, dbl_src0, LEN); fill_double_array(&lfg, dbl_src1, LEN); - avpriv_float_dsp_init(&fdsp, 1); - av_set_cpu_flags_mask(0); - avpriv_float_dsp_init(&cdsp, 1); - - if (test_vector_fmul(&fdsp, &cdsp, src0, src1)) + if (test_vector_fmul(fdsp, cdsp, src0, src1)) ret -= 1 << 0; - if (test_vector_fmac_scalar(&fdsp, &cdsp, src2, src0, src1[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])) + if (test_vector_fmul_scalar(fdsp, cdsp, src0, src1[0])) ret -= 1 << 2; - if (test_vector_fmul_window(&fdsp, &cdsp, src0, src1, src2)) + if (test_vector_fmul_window(fdsp, cdsp, src0, src1, src2)) ret -= 1 << 3; - if (test_vector_fmul_add(&fdsp, &cdsp, src0, src1, src2)) + if (test_vector_fmul_add(fdsp, cdsp, src0, src1, src2)) ret -= 1 << 4; - if (test_vector_fmul_reverse(&fdsp, &cdsp, src0, src1)) + if (test_vector_fmul_reverse(fdsp, cdsp, src0, src1)) ret -= 1 << 5; - if (test_butterflies_float(&fdsp, &cdsp, src0, src1)) + if (test_butterflies_float(fdsp, cdsp, src0, src1)) ret -= 1 << 6; - if (test_scalarproduct_float(&fdsp, &cdsp, src0, src1)) + if (test_scalarproduct_float(fdsp, cdsp, src0, src1)) ret -= 1 << 7; - if (test_vector_dmul_scalar(&fdsp, &cdsp, dbl_src0, dbl_src1[0])) + 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 index 600240fe97..5eeb63ce05 100644 --- a/libavutil/tests/hmac.c +++ b/libavutil/tests/hmac.c @@ -1,18 +1,18 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -41,44 +41,52 @@ static void test(AVHMAC *hmac, const uint8_t *key, int keylen, int main(void) { uint8_t key1[20], key3[131], data3[50]; - enum AVHMACType i = AV_HMAC_SHA224; + 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."; - AVHMAC *hmac = av_hmac_alloc(AV_HMAC_MD5); - if (!hmac) - return 1; + "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)); - // RFC 2202 test vectors - test(hmac, key1, 16, data1, sizeof(data1)); - test(hmac, key2, sizeof(key2), data2, sizeof(data2)); - test(hmac, key3, 16, data3, sizeof(data3)); - test(hmac, key3, 80, data4, sizeof(data4)); - test(hmac, key3, 80, data5, sizeof(data5)); - av_hmac_free(hmac); - /* SHA-1 */ - hmac = av_hmac_alloc(AV_HMAC_SHA1); - if (!hmac) - return 1; - // RFC 2202 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, 80, data4, sizeof(data4)); - test(hmac, key3, 80, data5, sizeof(data5)); - av_hmac_free(hmac); + /* 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 */ - while (i <= AV_HMAC_SHA256) { + 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)); @@ -86,7 +94,6 @@ int main(void) test(hmac, key3, sizeof(key3), data4, sizeof(data4)); test(hmac, key3, sizeof(key3), data6, sizeof(data6)); av_hmac_free(hmac); - i++; } return 0; } diff --git a/libavutil/tests/imgutils.c b/libavutil/tests/imgutils.c new file mode 100644 index 0000000000..571045c857 --- /dev/null +++ b/libavutil/tests/imgutils.c @@ -0,0 +1,36 @@ +/* + * 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/imgutils.c" + +#undef printf + +int main(void) +{ + int64_t x, y; + + for (y = -1; y<UINT_MAX; y+= y/2 + 1) { + for (x = -1; x<UINT_MAX; x+= x/2 + 1) { + int ret = av_image_check_size(x, y, 0, NULL); + printf("%d", ret >= 0); + } + printf("\n"); + } + + return 0; +} diff --git a/libavutil/tests/lfg.c b/libavutil/tests/lfg.c index 9a0c47c4de..bf127e3031 100644 --- a/libavutil/tests/lfg.c +++ b/libavutil/tests/lfg.c @@ -1,18 +1,18 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -20,18 +20,96 @@ #include "libavutil/timer.h" #include "libavutil/lfg.h" +static const double Z_TABLE[31][10] = { + {0.5000, 0.5040, 0.5080, 0.5120, 0.5160, 0.5199, 0.5239, 0.5279, 0.5319, 0.5359}, + {0.5398, 0.5438, 0.5478, 0.5517, 0.5557, 0.5596, 0.5636, 0.5675, 0.5714, 0.5753}, + {0.5793, 0.5832, 0.5871, 0.5910, 0.5948, 0.5987, 0.6026, 0.6064, 0.6103, 0.6141}, + {0.6179, 0.6217, 0.6255, 0.6293, 0.6331, 0.6368, 0.6406, 0.6443, 0.6480, 0.6517}, + {0.6554, 0.6591, 0.6628, 0.6664, 0.6700, 0.6736, 0.6772, 0.6808, 0.6844, 0.6879}, + {0.6915, 0.6950, 0.6985, 0.7019, 0.7054, 0.7088, 0.7123, 0.7157, 0.7190, 0.7224}, + {0.7257, 0.7291, 0.7324, 0.7357, 0.7389, 0.7422, 0.7454, 0.7486, 0.7517, 0.7549}, + {0.7580, 0.7611, 0.7642, 0.7673, 0.7704, 0.7734, 0.7764, 0.7794, 0.7823, 0.7852}, + {0.7881, 0.7910, 0.7939, 0.7967, 0.7995, 0.8023, 0.8051, 0.8078, 0.8106, 0.8133}, + {0.8159, 0.8186, 0.8212, 0.8238, 0.8264, 0.8289, 0.8315, 0.8340, 0.8365, 0.8389}, + {0.8413, 0.8438, 0.8461, 0.8485, 0.8508, 0.8531, 0.8554, 0.8577, 0.8599, 0.8621}, + {0.8643, 0.8665, 0.8686, 0.8708, 0.8729, 0.8749, 0.8770, 0.8790, 0.8810, 0.8830}, + {0.8849, 0.8869, 0.8888, 0.8907, 0.8925, 0.8944, 0.8962, 0.8980, 0.8997, 0.9015}, + {0.9032, 0.9049, 0.9066, 0.9082, 0.9099, 0.9115, 0.9131, 0.9147, 0.9162, 0.9177}, + {0.9192, 0.9207, 0.9222, 0.9236, 0.9251, 0.9265, 0.9279, 0.9292, 0.9306, 0.9319}, + {0.9332, 0.9345, 0.9357, 0.9370, 0.9382, 0.9394, 0.9406, 0.9418, 0.9429, 0.9441}, + {0.9452, 0.9463, 0.9474, 0.9484, 0.9495, 0.9505, 0.9515, 0.9525, 0.9535, 0.9545}, + {0.9554, 0.9564, 0.9573, 0.9582, 0.9591, 0.9599, 0.9608, 0.9616, 0.9625, 0.9633}, + {0.9641, 0.9649, 0.9656, 0.9664, 0.9671, 0.9678, 0.9686, 0.9693, 0.9699, 0.9706}, + {0.9713, 0.9719, 0.9726, 0.9732, 0.9738, 0.9744, 0.9750, 0.9756, 0.9761, 0.9767}, + {0.9772, 0.9778, 0.9783, 0.9788, 0.9793, 0.9798, 0.9803, 0.9808, 0.9812, 0.9817}, + {0.9821, 0.9826, 0.9830, 0.9834, 0.9838, 0.9842, 0.9846, 0.9850, 0.9854, 0.9857}, + {0.9861, 0.9864, 0.9868, 0.9871, 0.9875, 0.9878, 0.9881, 0.9884, 0.9887, 0.9890}, + {0.9893, 0.9896, 0.9898, 0.9901, 0.9904, 0.9906, 0.9909, 0.9911, 0.9913, 0.9916}, + {0.9918, 0.9920, 0.9922, 0.9925, 0.9927, 0.9929, 0.9931, 0.9932, 0.9934, 0.9936}, + {0.9938, 0.9940, 0.9941, 0.9943, 0.9945, 0.9946, 0.9948, 0.9949, 0.9951, 0.9952}, + {0.9953, 0.9955, 0.9956, 0.9957, 0.9959, 0.9960, 0.9961, 0.9962, 0.9963, 0.9964}, + {0.9965, 0.9966, 0.9967, 0.9968, 0.9969, 0.9970, 0.9971, 0.9972, 0.9973, 0.9974}, + {0.9974, 0.9975, 0.9976, 0.9977, 0.9977, 0.9978, 0.9979, 0.9979, 0.9980, 0.9981}, + {0.9981, 0.9982, 0.9982, 0.9983, 0.9984, 0.9984, 0.9985, 0.9985, 0.9986, 0.9986}, + {0.9987, 0.9987, 0.9987, 0.9988, 0.9988, 0.9989, 0.9989, 0.9989, 0.9990, 0.9990} }; + +// Inverse cumulative distribution function +static double inv_cdf(double u) +{ + const double a[4] = { 2.50662823884, + -18.61500062529, + 41.39119773534, + -25.44106049637}; + + const double b[4] = {-8.47351093090, + 23.08336743743, + -21.06224101826, + 3.13082909833}; + + const double c[9] = {0.3374754822726147, + 0.9761690190917186, + 0.1607979714918209, + 0.0276438810333863, + 0.0038405729373609, + 0.0003951896511919, + 0.0000321767881768, + 0.0000002888167364, + 0.0000003960315187}; + + double r; + double x = u - 0.5; + + // Beasley-Springer + if (fabs(x) < 0.42) { + + double y = x * x; + r = x * (((a[3]*y+a[2])*y+a[1])*y+a[0]) / + ((((b[3]*y+b[2])*y+b[1])*y+b[0])*y+1.0); + } + else {// Moro + r = u; + if (x > 0.0) + r = 1.0 - u; + r = log(-log(r)); + r = c[0] + r*(c[1]+r*(c[2]+r*(c[3]+r*(c[4]+r*(c[5]+r*(c[6]+ + r*(c[7]+r*c[8]))))))); + if (x < 0.0) + r = -r; + } + + return r; +} 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++) + 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); @@ -39,18 +117,75 @@ int main(void) { double mean = 1000; double stddev = 53; + double samp_mean = 0.0, samp_stddev = 0.0, QH = 0; + double Z, p_value = -1, tot_samp = 1000; + double *PRN_arr = av_malloc_array(tot_samp, sizeof(double)); - av_lfg_init(&state, 42); + if (!PRN_arr) { + fprintf(stderr, "failed to allocate memory!\n"); + return 1; + } - for (i = 0; i < 1000; i += 2) { + av_lfg_init(&state, 42); + for (i = 0; i < tot_samp; i += 2) { double bmg_out[2]; av_bmg_get(&state, bmg_out); - av_log(NULL, AV_LOG_INFO, - "%f\n%f\n", - bmg_out[0] * stddev + mean, - bmg_out[1] * stddev + mean); + PRN_arr[i ] = bmg_out[0] * stddev + mean; + PRN_arr[i+1] = bmg_out[1] * stddev + mean; + samp_mean += PRN_arr[i] + PRN_arr[i+1]; + samp_stddev += PRN_arr[i] * PRN_arr[i] + PRN_arr[i+1] * PRN_arr[i+1]; + printf("PRN%d : %f\n" + "PRN%d : %f\n", + i, PRN_arr[i], i+1, PRN_arr[i+1]); + } + samp_mean /= tot_samp; + samp_stddev /= (tot_samp - 1); + samp_stddev -= (tot_samp * 1.0 / (tot_samp - 1))*samp_mean*samp_mean; + samp_stddev = sqrt(samp_stddev); + Z = (mean - samp_mean) / (stddev / sqrt(tot_samp)); + { + int x, y, a, b, flag = 0; + + if (Z < 0.0) { + flag = !flag; + Z = Z * -1.0; + } + + a = (int)(Z * 100); + b = ((int)Z * 100); + x = Z * 10; + y = (b > 0) ? a % b : a; + y = y % 10; + if (x > 30 || y > 9) { + av_log(NULL, AV_LOG_INFO, "error: out of bounds! tried to access" + "Z_TABLE[%d][%d]\n", x, y); + goto SKIP; + } + p_value = flag ? 1 - Z_TABLE[x][y] : Z_TABLE[x][y]; } - } +SKIP: for (i = 0; i < tot_samp; ++i) { + + if ( i < (tot_samp - 1)) { + double H_diff; + H_diff = inv_cdf((i + 2.0 - (3.0/8.0)) / (tot_samp + (1.0/4.0))); + H_diff -= inv_cdf((i + 1.0 - (3.0/8.0)) / (tot_samp + (1.0/4.0))); + + QH += ((PRN_arr[i + 1] - PRN_arr[i]) / H_diff); + } + } + QH = 1.0 - QH / ((tot_samp - 1.0) * samp_stddev); + + printf("sample mean : %f\n" + "true mean : %f\n" + "sample stddev: %f\n" + "true stddev : %f\n" + "z-score : %f\n" + "p-value : %f\n" + "QH[normality]: %f\n", + samp_mean, mean, samp_stddev, stddev, Z, p_value, QH); + + av_freep(&PRN_arr); + } return 0; } diff --git a/libavutil/tests/lls.c b/libavutil/tests/lls.c index 7866b38998..965b0f458d 100644 --- a/libavutil/tests/lls.c +++ b/libavutil/tests/lls.c @@ -1,18 +1,18 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ 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 index 9fb7006dd8..42e4538e0a 100644 --- a/libavutil/tests/md5.c +++ b/libavutil/tests/md5.c @@ -1,18 +1,18 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -33,7 +33,8 @@ int main(void) { uint8_t md5val[16]; int i; - uint8_t in[1000]; + 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; 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 index 350ce3d820..568eb45668 100644 --- a/libavutil/tests/opt.c +++ b/libavutil/tests/opt.c @@ -1,18 +1,18 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -20,11 +20,13 @@ #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; @@ -33,6 +35,26 @@ typedef struct TestContext { 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) @@ -41,15 +63,32 @@ typedef struct TestContext { #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 }, - { "toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1 }, - { "rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, 10 }, - { "string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, { 0 }, CHAR_MIN, CHAR_MAX }, - { "flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX, 0, "flags"}, - { "cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_COOL }, INT_MIN, INT_MAX, 0, "flags"}, - { "lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_LAME }, INT_MIN, INT_MAX, 0, "flags"}, - { "mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_MU }, INT_MIN, INT_MAX, 0, "flags"}, +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 }, }; @@ -64,46 +103,209 @@ static const AVClass test_class = { 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; - TestContext test_ctx = { .class = &test_class }; - static const char *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", - }; + + 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", + }; - av_opt_set_defaults(&test_ctx); - test_ctx.string = av_strdup("default"); - if (!test_ctx.string) - return AVERROR(ENOMEM); + test_ctx.class = &test_class; + av_opt_set_defaults(&test_ctx); - av_log_set_level(AV_LOG_DEBUG); + 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_set_options_string(&test_ctx, options[i], "=", ":") < 0) - av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]); - printf("\n"); + 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 index 805c01ff5a..180f624002 100644 --- a/libavutil/tests/parseutils.c +++ b/libavutil/tests/parseutils.c @@ -1,33 +1,41 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * 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" -#include "libavutil/parseutils.h" -int main(void) +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; - uint8_t rgba[4]; static const char *const rates[] = { "-inf", "inf", @@ -55,7 +63,23 @@ int main(void) " 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 ", @@ -92,25 +116,152 @@ int main(void) "red@-0.0", }; - printf("Testing av_parse_video_rate()\n"); + av_log_set_level(AV_LOG_DEBUG); - 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"); + 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]); } +} - printf("\nTesting av_parse_color()\n"); +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" }, + { " %Y - %b - %d %H : %M : %S", " 2012 - nOV - 21 20 : 12 : 21" }, + { " %Y - %B - %d %H : %M : %S", " 2012 - nOVemBeR - 21 20 : 12 : 21" }, + { " %Y - %B%d %H : %M : %S", " 2012 - may21 20 : 12 : 21" }, + { " %Y - %B%d %H : %M : %S", " 2012 - mby21 20 : 12 : 21" }, + { " %Y - %B - %d %H : %M : %S", " 2012 - JunE - 21 20 : 12 : 21" }, + { " %Y - %B - %d %H : %M : %S", " 2012 - Jane - 21 20 : 12 : 21" }, + { " %Y - %B - %d %H : %M : %S", " 2012 - January - 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"); + } + } +} - for (i = 0; i < FF_ARRAY_ELEMS(color_names); i++) { - if (av_parse_color(rgba, color_names[i], -1, NULL) >= 0) +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_names[i], rgba[0], rgba[1], rgba[2], rgba[3]); + 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..78067dbe41 --- /dev/null +++ b/libavutil/tests/random_seed.c @@ -0,0 +1,55 @@ +/* + * 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 +#define F 2 +#include <stdio.h> + +typedef uint32_t (*random_seed_ptr_t)(void); + +int main(void) +{ + int i, j, rsf, retry; + uint32_t seeds[N]; + random_seed_ptr_t random_seed[F] = {av_get_random_seed, get_generic_seed}; + + for (rsf=0; rsf<F; ++rsf){ + for (retry=0; retry<3; retry++){ + for (i=0; i<N; i++){ + seeds[i] = random_seed[rsf](); + for (j=0; j<i; j++) + if (seeds[j] == seeds[i]) + goto retry; + } + printf("seeds OK\n"); + break; + retry:; + } + if (retry >= 3) { + printf("rsf %d: FAIL at %d with %X\n", rsf, j, seeds[j]); + return 1; + } + } + return 0; +} 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 index 0f1d514b44..a95b3a001f 100644 --- a/libavutil/tests/sha.c +++ b/libavutil/tests/sha.c @@ -1,44 +1,49 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * 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/sha.c" - #include <stdio.h> +#include "libavutil/mem.h" +#include "libavutil/sha.h" + int main(void) { int i, j, k; - AVSHA ctx; + 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]); + av_sha_init(ctx, lengths[j]); if (k == 0) - av_sha_update(&ctx, "abc", 3); + av_sha_update(ctx, "abc", 3); else if (k == 1) - av_sha_update(&ctx, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56); + 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); + 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'); @@ -64,6 +69,7 @@ int main(void) 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 index fb19b27896..7503228970 100644 --- a/libavutil/tests/tree.c +++ b/libavutil/tests/tree.c @@ -1,18 +1,18 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -54,34 +54,32 @@ static void print(AVTreeNode *t, int depth) av_log(NULL, AV_LOG_ERROR, "NULL\n"); } -static int cmp(void *a, const void *b) +static int cmp(const void *a, const void *b) { - return (uint8_t *) a - (const uint8_t *) b; + return (const uint8_t *) a - (const uint8_t *) b; } -int main(void) +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++) { - AVTreeNode *node2 = NULL; intptr_t j = av_lfg_get(&prng) % 86294; - void *ret, *jj = (void *)(j + 1); - - while (ret = av_tree_find(root, jj, cmp, NULL)) { - j = av_lfg_get(&prng) % 86294; - jj = (void *)(j + 1); - } 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(); @@ -89,20 +87,20 @@ int main(void) av_log(NULL, AV_LOG_ERROR, "Memory allocation failure.\n"); return 1; } - av_tree_insert(&root, jj, cmp, &node); - - while (ret = av_tree_find(root, jj, cmp, NULL)) { - j = av_lfg_get(&prng) % 86294; - jj = (void *)(j + 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); } - - ret = av_tree_insert(&root, jj, cmp, &node2); - if (ret != jj) - av_tree_destroy(node2); - ret = av_tree_find(root, jj, cmp, NULL); - if (ret) - av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i); } + av_free(node); av_tree_destroy(root); 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 index 8fd76dd1e7..6a8c6475f3 100644 --- a/libavutil/tests/xtea.c +++ b/libavutil/tests/xtea.c @@ -1,18 +1,18 @@ /* - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -117,6 +117,7 @@ int main(void) 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"); av_free(ctx); |