You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.7KB

  1. /*
  2. * Copyright (c) 2020
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include "libavfilter/dnn/dnn_backend_native_layer_mathunary.h"
  24. #include "libavutil/avassert.h"
  25. #define EPS 0.00001
  26. static float get_expected(float f, DNNMathUnaryOperation op)
  27. {
  28. switch (op)
  29. {
  30. case DMUO_ABS:
  31. return (f >= 0) ? f : -f;
  32. case DMUO_SIN:
  33. return sin(f);
  34. case DMUO_COS:
  35. return cos(f);
  36. case DMUO_TAN:
  37. return tan(f);
  38. case DMUO_ASIN:
  39. return asin(f);
  40. case DMUO_ACOS:
  41. return acos(f);
  42. case DMUO_ATAN:
  43. return atan(f);
  44. default:
  45. av_assert0(!"not supported yet");
  46. return 0.f;
  47. }
  48. }
  49. static int test(DNNMathUnaryOperation op)
  50. {
  51. DnnLayerMathUnaryParams params;
  52. DnnOperand operands[2];
  53. int32_t input_indexes[1];
  54. float input[1*1*2*3] = {
  55. -3, 2.5, 2, -2.1, 7.8, 100};
  56. float *output;
  57. params.un_op = op;
  58. operands[0].data = input;
  59. operands[0].dims[0] = 1;
  60. operands[0].dims[1] = 1;
  61. operands[0].dims[2] = 2;
  62. operands[0].dims[3] = 3;
  63. operands[1].data = NULL;
  64. input_indexes[0] = 0;
  65. dnn_execute_layer_math_unary(operands, input_indexes, 1, &params);
  66. output = operands[1].data;
  67. for (int i = 0; i < sizeof(input) / sizeof(float); ++i) {
  68. float expected_output = get_expected(input[i], op);
  69. if(fabs(output[i] - expected_output) > EPS) {
  70. printf("at index %d, output: %f, expected_output: %f\n", i, output[i], expected_output);
  71. av_freep(&output);
  72. return 1;
  73. }
  74. }
  75. av_freep(&output);
  76. return 0;
  77. }
  78. int main(int agrc, char **argv)
  79. {
  80. if (test(DMUO_ABS))
  81. return 1;
  82. if (test(DMUO_SIN))
  83. return 1;
  84. if (test(DMUO_COS))
  85. return 1;
  86. if (test(DMUO_TAN))
  87. return 1;
  88. if (test(DMUO_ASIN))
  89. return 1;
  90. if (test(DMUO_ACOS))
  91. return 1;
  92. if (test(DMUO_ATAN))
  93. return 1;
  94. return 0;
  95. }