mlpdsp_init.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * MLP DSP functions x86-optimized
  3. * Copyright (c) 2009 Ramiro Polla
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/attributes.h"
  22. #include "libavutil/cpu.h"
  23. #include "libavutil/x86/asm.h"
  24. #include "libavutil/x86/cpu.h"
  25. #include "libavcodec/mlpdsp.h"
  26. #include "libavcodec/mlp.h"
  27. #define REMATRIX_CHANNEL_FUNC(opt) \
  28. void ff_mlp_rematrix_channel_##opt(int32_t *samples, \
  29. const int32_t *coeffs, \
  30. const uint8_t *bypassed_lsbs, \
  31. const int8_t *noise_buffer, \
  32. int index, \
  33. unsigned int dest_ch, \
  34. uint16_t blockpos, \
  35. unsigned int maxchan, \
  36. int matrix_noise_shift, \
  37. int access_unit_size_pow2, \
  38. int32_t mask);
  39. REMATRIX_CHANNEL_FUNC(sse4)
  40. REMATRIX_CHANNEL_FUNC(avx2_bmi2)
  41. #if HAVE_7REGS && HAVE_INLINE_ASM && HAVE_INLINE_ASM_NONLOCAL_LABELS
  42. extern char ff_mlp_firorder_8;
  43. extern char ff_mlp_firorder_7;
  44. extern char ff_mlp_firorder_6;
  45. extern char ff_mlp_firorder_5;
  46. extern char ff_mlp_firorder_4;
  47. extern char ff_mlp_firorder_3;
  48. extern char ff_mlp_firorder_2;
  49. extern char ff_mlp_firorder_1;
  50. extern char ff_mlp_firorder_0;
  51. extern char ff_mlp_iirorder_4;
  52. extern char ff_mlp_iirorder_3;
  53. extern char ff_mlp_iirorder_2;
  54. extern char ff_mlp_iirorder_1;
  55. extern char ff_mlp_iirorder_0;
  56. static const void * const firtable[9] = { &ff_mlp_firorder_0, &ff_mlp_firorder_1,
  57. &ff_mlp_firorder_2, &ff_mlp_firorder_3,
  58. &ff_mlp_firorder_4, &ff_mlp_firorder_5,
  59. &ff_mlp_firorder_6, &ff_mlp_firorder_7,
  60. &ff_mlp_firorder_8 };
  61. static const void * const iirtable[5] = { &ff_mlp_iirorder_0, &ff_mlp_iirorder_1,
  62. &ff_mlp_iirorder_2, &ff_mlp_iirorder_3,
  63. &ff_mlp_iirorder_4 };
  64. #if ARCH_X86_64
  65. #define MLPMUL(label, offset, offs, offc) \
  66. LABEL_MANGLE(label)": \n\t" \
  67. "movslq "offset"+"offs"(%0), %%rax\n\t" \
  68. "movslq "offset"+"offc"(%1), %%rdx\n\t" \
  69. "imul %%rdx, %%rax\n\t" \
  70. "add %%rax, %%rsi\n\t"
  71. #define FIRMULREG(label, offset, firc)\
  72. LABEL_MANGLE(label)": \n\t" \
  73. "movslq "#offset"(%0), %%rax\n\t" \
  74. "imul %"#firc", %%rax\n\t" \
  75. "add %%rax, %%rsi\n\t"
  76. #define CLEAR_ACCUM \
  77. "xor %%rsi, %%rsi\n\t"
  78. #define SHIFT_ACCUM \
  79. "shr %%cl, %%rsi\n\t"
  80. #define ACCUM "%%rdx"
  81. #define RESULT "%%rsi"
  82. #define RESULT32 "%%esi"
  83. #else /* if ARCH_X86_32 */
  84. #define MLPMUL(label, offset, offs, offc) \
  85. LABEL_MANGLE(label)": \n\t" \
  86. "mov "offset"+"offs"(%0), %%eax\n\t" \
  87. "imull "offset"+"offc"(%1) \n\t" \
  88. "add %%eax , %%esi\n\t" \
  89. "adc %%edx , %%ecx\n\t"
  90. #define FIRMULREG(label, offset, firc) \
  91. MLPMUL(label, #offset, "0", "0")
  92. #define CLEAR_ACCUM \
  93. "xor %%esi, %%esi\n\t" \
  94. "xor %%ecx, %%ecx\n\t"
  95. #define SHIFT_ACCUM \
  96. "mov %%ecx, %%edx\n\t" \
  97. "mov %%esi, %%eax\n\t" \
  98. "movzbl %7 , %%ecx\n\t" \
  99. "shrd %%cl, %%edx, %%eax\n\t" \
  100. #define ACCUM "%%edx"
  101. #define RESULT "%%eax"
  102. #define RESULT32 "%%eax"
  103. #endif /* !ARCH_X86_64 */
  104. #define BINC AV_STRINGIFY(4* MAX_CHANNELS)
  105. #define IOFFS AV_STRINGIFY(4*(MAX_FIR_ORDER + MAX_BLOCKSIZE))
  106. #define IOFFC AV_STRINGIFY(4* MAX_FIR_ORDER)
  107. #define FIRMUL(label, offset) MLPMUL(label, #offset, "0", "0")
  108. #define IIRMUL(label, offset) MLPMUL(label, #offset, IOFFS, IOFFC)
  109. static void mlp_filter_channel_x86(int32_t *state, const int32_t *coeff,
  110. int firorder, int iirorder,
  111. unsigned int filter_shift, int32_t mask,
  112. int blocksize, int32_t *sample_buffer)
  113. {
  114. const void *firjump = firtable[firorder];
  115. const void *iirjump = iirtable[iirorder];
  116. blocksize = -blocksize;
  117. __asm__ volatile(
  118. "1: \n\t"
  119. CLEAR_ACCUM
  120. "jmp *%5 \n\t"
  121. FIRMUL (ff_mlp_firorder_8, 0x1c )
  122. FIRMUL (ff_mlp_firorder_7, 0x18 )
  123. FIRMUL (ff_mlp_firorder_6, 0x14 )
  124. FIRMUL (ff_mlp_firorder_5, 0x10 )
  125. FIRMUL (ff_mlp_firorder_4, 0x0c )
  126. FIRMUL (ff_mlp_firorder_3, 0x08 )
  127. FIRMUL (ff_mlp_firorder_2, 0x04 )
  128. FIRMULREG(ff_mlp_firorder_1, 0x00, 8)
  129. LABEL_MANGLE(ff_mlp_firorder_0)":\n\t"
  130. "jmp *%6 \n\t"
  131. IIRMUL (ff_mlp_iirorder_4, 0x0c )
  132. IIRMUL (ff_mlp_iirorder_3, 0x08 )
  133. IIRMUL (ff_mlp_iirorder_2, 0x04 )
  134. IIRMUL (ff_mlp_iirorder_1, 0x00 )
  135. LABEL_MANGLE(ff_mlp_iirorder_0)":\n\t"
  136. SHIFT_ACCUM
  137. "mov "RESULT" ,"ACCUM" \n\t"
  138. "add (%2) ,"RESULT" \n\t"
  139. "and %4 ,"RESULT" \n\t"
  140. "sub $4 , %0 \n\t"
  141. "mov "RESULT32", (%0) \n\t"
  142. "mov "RESULT32", (%2) \n\t"
  143. "add $"BINC" , %2 \n\t"
  144. "sub "ACCUM" ,"RESULT" \n\t"
  145. "mov "RESULT32","IOFFS"(%0) \n\t"
  146. "incl %3 \n\t"
  147. "js 1b \n\t"
  148. : /* 0*/"+r"(state),
  149. /* 1*/"+r"(coeff),
  150. /* 2*/"+r"(sample_buffer),
  151. #if ARCH_X86_64
  152. /* 3*/"+r"(blocksize)
  153. : /* 4*/"r"((x86_reg)mask), /* 5*/"r"(firjump),
  154. /* 6*/"r"(iirjump) , /* 7*/"c"(filter_shift)
  155. , /* 8*/"r"((int64_t)coeff[0])
  156. : "rax", "rdx", "rsi"
  157. #else /* ARCH_X86_32 */
  158. /* 3*/"+m"(blocksize)
  159. : /* 4*/"m"( mask), /* 5*/"m"(firjump),
  160. /* 6*/"m"(iirjump) , /* 7*/"m"(filter_shift)
  161. : "eax", "edx", "esi", "ecx"
  162. #endif /* !ARCH_X86_64 */
  163. );
  164. }
  165. #endif /* HAVE_7REGS && HAVE_INLINE_ASM */
  166. av_cold void ff_mlpdsp_init_x86(MLPDSPContext *c)
  167. {
  168. int cpu_flags = av_get_cpu_flags();
  169. #if HAVE_7REGS && HAVE_INLINE_ASM && HAVE_INLINE_ASM_NONLOCAL_LABELS
  170. if (INLINE_MMX(cpu_flags))
  171. c->mlp_filter_channel = mlp_filter_channel_x86;
  172. #endif
  173. if (ARCH_X86_64 && EXTERNAL_SSE4(cpu_flags))
  174. c->mlp_rematrix_channel = ff_mlp_rematrix_channel_sse4;
  175. if (ARCH_X86_64 && EXTERNAL_AVX2_FAST(cpu_flags) && cpu_flags & AV_CPU_FLAG_BMI2)
  176. c->mlp_rematrix_channel = ff_mlp_rematrix_channel_avx2_bmi2;
  177. }