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.

414 lines
22KB

  1. /*
  2. * Copyright (c) 2012
  3. * MIPS Technologies, Inc., California.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the MIPS Technologies, Inc., nor the names of its
  14. * contributors may be used to endorse or promote products derived from
  15. * this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE MIPS TECHNOLOGIES, INC. ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE MIPS TECHNOLOGIES, INC. BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * Authors: Branimir Vasic (bvasic@mips.com)
  30. * Nedeljko Babic (nbabic@mips.com)
  31. *
  32. * Various AC-3 DSP Utils optimized for MIPS
  33. *
  34. * This file is part of FFmpeg.
  35. *
  36. * FFmpeg is free software; you can redistribute it and/or
  37. * modify it under the terms of the GNU Lesser General Public
  38. * License as published by the Free Software Foundation; either
  39. * version 2.1 of the License, or (at your option) any later version.
  40. *
  41. * FFmpeg is distributed in the hope that it will be useful,
  42. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  43. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  44. * Lesser General Public License for more details.
  45. *
  46. * You should have received a copy of the GNU Lesser General Public
  47. * License along with FFmpeg; if not, write to the Free Software
  48. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  49. */
  50. /**
  51. * @file
  52. * Reference: libavcodec/ac3dsp.c
  53. */
  54. #include "config.h"
  55. #include "libavcodec/ac3dsp.h"
  56. #include "libavcodec/ac3.h"
  57. #include "libavutil/mips/asmdefs.h"
  58. #if HAVE_INLINE_ASM
  59. #if HAVE_MIPSDSP
  60. static void ac3_bit_alloc_calc_bap_mips(int16_t *mask, int16_t *psd,
  61. int start, int end,
  62. int snr_offset, int floor,
  63. const uint8_t *bap_tab, uint8_t *bap)
  64. {
  65. int band, band_end, cond;
  66. int m, address1, address2;
  67. int16_t *psd1, *psd_end;
  68. uint8_t *bap1;
  69. if (snr_offset == -960) {
  70. memset(bap, 0, AC3_MAX_COEFS);
  71. return;
  72. }
  73. psd1 = &psd[start];
  74. bap1 = &bap[start];
  75. band = ff_ac3_bin_to_band_tab[start];
  76. do {
  77. m = (FFMAX(mask[band] - snr_offset - floor, 0) & 0x1FE0) + floor;
  78. band_end = ff_ac3_band_start_tab[++band];
  79. band_end = FFMIN(band_end, end);
  80. psd_end = psd + band_end - 1;
  81. __asm__ volatile (
  82. "slt %[cond], %[psd1], %[psd_end] \n\t"
  83. "beqz %[cond], 1f \n\t"
  84. "2: \n\t"
  85. "lh %[address1], 0(%[psd1]) \n\t"
  86. "lh %[address2], 2(%[psd1]) \n\t"
  87. PTR_ADDIU " %[psd1], %[psd1], 4 \n\t"
  88. "subu %[address1], %[address1], %[m] \n\t"
  89. "sra %[address1], %[address1], 5 \n\t"
  90. "addiu %[address1], %[address1], -32 \n\t"
  91. "shll_s.w %[address1], %[address1], 26 \n\t"
  92. "subu %[address2], %[address2], %[m] \n\t"
  93. "sra %[address2], %[address2], 5 \n\t"
  94. "sra %[address1], %[address1], 26 \n\t"
  95. "addiu %[address1], %[address1], 32 \n\t"
  96. "lbux %[address1], %[address1](%[bap_tab]) \n\t"
  97. "addiu %[address2], %[address2], -32 \n\t"
  98. "shll_s.w %[address2], %[address2], 26 \n\t"
  99. "sb %[address1], 0(%[bap1]) \n\t"
  100. "slt %[cond], %[psd1], %[psd_end] \n\t"
  101. "sra %[address2], %[address2], 26 \n\t"
  102. "addiu %[address2], %[address2], 32 \n\t"
  103. "lbux %[address2], %[address2](%[bap_tab]) \n\t"
  104. "sb %[address2], 1(%[bap1]) \n\t"
  105. PTR_ADDIU " %[bap1], %[bap1], 2 \n\t"
  106. "bnez %[cond], 2b \n\t"
  107. PTR_ADDIU " %[psd_end], %[psd_end], 2 \n\t"
  108. "slt %[cond], %[psd1], %[psd_end] \n\t"
  109. "beqz %[cond], 3f \n\t"
  110. "1: \n\t"
  111. "lh %[address1], 0(%[psd1]) \n\t"
  112. PTR_ADDIU " %[psd1], %[psd1], 2 \n\t"
  113. "subu %[address1], %[address1], %[m] \n\t"
  114. "sra %[address1], %[address1], 5 \n\t"
  115. "addiu %[address1], %[address1], -32 \n\t"
  116. "shll_s.w %[address1], %[address1], 26 \n\t"
  117. "sra %[address1], %[address1], 26 \n\t"
  118. "addiu %[address1], %[address1], 32 \n\t"
  119. "lbux %[address1], %[address1](%[bap_tab]) \n\t"
  120. "sb %[address1], 0(%[bap1]) \n\t"
  121. PTR_ADDIU " %[bap1], %[bap1], 1 \n\t"
  122. "3: \n\t"
  123. : [address1]"=&r"(address1), [address2]"=&r"(address2),
  124. [cond]"=&r"(cond), [bap1]"+r"(bap1),
  125. [psd1]"+r"(psd1), [psd_end]"+r"(psd_end)
  126. : [m]"r"(m), [bap_tab]"r"(bap_tab)
  127. : "memory"
  128. );
  129. } while (end > band_end);
  130. }
  131. static void ac3_update_bap_counts_mips(uint16_t mant_cnt[16], uint8_t *bap,
  132. int len)
  133. {
  134. void *temp0, *temp2, *temp4, *temp5, *temp6, *temp7;
  135. int temp1, temp3;
  136. __asm__ volatile (
  137. "andi %[temp3], %[len], 3 \n\t"
  138. PTR_ADDU "%[temp2], %[bap], %[len] \n\t"
  139. PTR_ADDU "%[temp4], %[bap], %[temp3] \n\t"
  140. "beq %[temp2], %[temp4], 4f \n\t"
  141. "1: \n\t"
  142. "lbu %[temp0], -1(%[temp2]) \n\t"
  143. "lbu %[temp5], -2(%[temp2]) \n\t"
  144. "lbu %[temp6], -3(%[temp2]) \n\t"
  145. "sll %[temp0], %[temp0], 1 \n\t"
  146. PTR_ADDU "%[temp0], %[mant_cnt], %[temp0] \n\t"
  147. "sll %[temp5], %[temp5], 1 \n\t"
  148. PTR_ADDU "%[temp5], %[mant_cnt], %[temp5] \n\t"
  149. "lhu %[temp1], 0(%[temp0]) \n\t"
  150. "sll %[temp6], %[temp6], 1 \n\t"
  151. PTR_ADDU "%[temp6], %[mant_cnt], %[temp6] \n\t"
  152. "addiu %[temp1], %[temp1], 1 \n\t"
  153. "sh %[temp1], 0(%[temp0]) \n\t"
  154. "lhu %[temp1], 0(%[temp5]) \n\t"
  155. "lbu %[temp7], -4(%[temp2]) \n\t"
  156. PTR_ADDIU "%[temp2],%[temp2], -4 \n\t"
  157. "addiu %[temp1], %[temp1], 1 \n\t"
  158. "sh %[temp1], 0(%[temp5]) \n\t"
  159. "lhu %[temp1], 0(%[temp6]) \n\t"
  160. "sll %[temp7], %[temp7], 1 \n\t"
  161. PTR_ADDU "%[temp7], %[mant_cnt], %[temp7] \n\t"
  162. "addiu %[temp1], %[temp1],1 \n\t"
  163. "sh %[temp1], 0(%[temp6]) \n\t"
  164. "lhu %[temp1], 0(%[temp7]) \n\t"
  165. "addiu %[temp1], %[temp1], 1 \n\t"
  166. "sh %[temp1], 0(%[temp7]) \n\t"
  167. "bne %[temp2], %[temp4], 1b \n\t"
  168. "4: \n\t"
  169. "beqz %[temp3], 2f \n\t"
  170. "3: \n\t"
  171. "addiu %[temp3], %[temp3], -1 \n\t"
  172. "lbu %[temp0], -1(%[temp2]) \n\t"
  173. PTR_ADDIU "%[temp2],%[temp2], -1 \n\t"
  174. "sll %[temp0], %[temp0], 1 \n\t"
  175. PTR_ADDU "%[temp0], %[mant_cnt], %[temp0] \n\t"
  176. "lhu %[temp1], 0(%[temp0]) \n\t"
  177. "addiu %[temp1], %[temp1], 1 \n\t"
  178. "sh %[temp1], 0(%[temp0]) \n\t"
  179. "bgtz %[temp3], 3b \n\t"
  180. "2: \n\t"
  181. : [temp0] "=&r" (temp0), [temp1] "=&r" (temp1),
  182. [temp2] "=&r" (temp2), [temp3] "=&r" (temp3),
  183. [temp4] "=&r" (temp4), [temp5] "=&r" (temp5),
  184. [temp6] "=&r" (temp6), [temp7] "=&r" (temp7)
  185. : [len] "r" (len), [bap] "r" (bap),
  186. [mant_cnt] "r" (mant_cnt)
  187. : "memory"
  188. );
  189. }
  190. #endif
  191. #if HAVE_MIPSFPU
  192. static void float_to_fixed24_mips(int32_t *dst, const float *src, unsigned int len)
  193. {
  194. const float scale = 1 << 24;
  195. float src0, src1, src2, src3, src4, src5, src6, src7;
  196. int temp0, temp1, temp2, temp3, temp4, temp5, temp6, temp7;
  197. do {
  198. __asm__ volatile (
  199. "lwc1 %[src0], 0(%[src]) \n\t"
  200. "lwc1 %[src1], 4(%[src]) \n\t"
  201. "lwc1 %[src2], 8(%[src]) \n\t"
  202. "lwc1 %[src3], 12(%[src]) \n\t"
  203. "lwc1 %[src4], 16(%[src]) \n\t"
  204. "lwc1 %[src5], 20(%[src]) \n\t"
  205. "lwc1 %[src6], 24(%[src]) \n\t"
  206. "lwc1 %[src7], 28(%[src]) \n\t"
  207. "mul.s %[src0], %[src0], %[scale] \n\t"
  208. "mul.s %[src1], %[src1], %[scale] \n\t"
  209. "mul.s %[src2], %[src2], %[scale] \n\t"
  210. "mul.s %[src3], %[src3], %[scale] \n\t"
  211. "mul.s %[src4], %[src4], %[scale] \n\t"
  212. "mul.s %[src5], %[src5], %[scale] \n\t"
  213. "mul.s %[src6], %[src6], %[scale] \n\t"
  214. "mul.s %[src7], %[src7], %[scale] \n\t"
  215. "cvt.w.s %[src0], %[src0] \n\t"
  216. "cvt.w.s %[src1], %[src1] \n\t"
  217. "cvt.w.s %[src2], %[src2] \n\t"
  218. "cvt.w.s %[src3], %[src3] \n\t"
  219. "cvt.w.s %[src4], %[src4] \n\t"
  220. "cvt.w.s %[src5], %[src5] \n\t"
  221. "cvt.w.s %[src6], %[src6] \n\t"
  222. "cvt.w.s %[src7], %[src7] \n\t"
  223. "mfc1 %[temp0], %[src0] \n\t"
  224. "mfc1 %[temp1], %[src1] \n\t"
  225. "mfc1 %[temp2], %[src2] \n\t"
  226. "mfc1 %[temp3], %[src3] \n\t"
  227. "mfc1 %[temp4], %[src4] \n\t"
  228. "mfc1 %[temp5], %[src5] \n\t"
  229. "mfc1 %[temp6], %[src6] \n\t"
  230. "mfc1 %[temp7], %[src7] \n\t"
  231. "sw %[temp0], 0(%[dst]) \n\t"
  232. "sw %[temp1], 4(%[dst]) \n\t"
  233. "sw %[temp2], 8(%[dst]) \n\t"
  234. "sw %[temp3], 12(%[dst]) \n\t"
  235. "sw %[temp4], 16(%[dst]) \n\t"
  236. "sw %[temp5], 20(%[dst]) \n\t"
  237. "sw %[temp6], 24(%[dst]) \n\t"
  238. "sw %[temp7], 28(%[dst]) \n\t"
  239. : [dst] "+r" (dst), [src] "+r" (src),
  240. [src0] "=&f" (src0), [src1] "=&f" (src1),
  241. [src2] "=&f" (src2), [src3] "=&f" (src3),
  242. [src4] "=&f" (src4), [src5] "=&f" (src5),
  243. [src6] "=&f" (src6), [src7] "=&f" (src7),
  244. [temp0] "=r" (temp0), [temp1] "=r" (temp1),
  245. [temp2] "=r" (temp2), [temp3] "=r" (temp3),
  246. [temp4] "=r" (temp4), [temp5] "=r" (temp5),
  247. [temp6] "=r" (temp6), [temp7] "=r" (temp7)
  248. : [scale] "f" (scale)
  249. : "memory"
  250. );
  251. src = src + 8;
  252. dst = dst + 8;
  253. len -= 8;
  254. } while (len > 0);
  255. }
  256. static void ac3_downmix_mips(float **samples, float (*matrix)[2],
  257. int out_ch, int in_ch, int len)
  258. {
  259. int i, j, i1, i2, i3;
  260. float v0, v1, v2, v3;
  261. float v4, v5, v6, v7;
  262. float samples0, samples1, samples2, samples3, matrix_j, matrix_j2;
  263. float *samples_p, *samples_sw, *matrix_p, **samples_x, **samples_end;
  264. __asm__ volatile(
  265. ".set push \n\t"
  266. ".set noreorder \n\t"
  267. "li %[i1], 2 \n\t"
  268. "sll %[len], 2 \n\t"
  269. "move %[i], $zero \n\t"
  270. "sll %[j], %[in_ch], " PTRLOG " \n\t"
  271. "bne %[out_ch], %[i1], 3f \n\t" // if (out_ch == 2)
  272. " li %[i2], 1 \n\t"
  273. "2: \n\t" // start of the for loop (for (i = 0; i < len; i+=4))
  274. "move %[matrix_p], %[matrix] \n\t"
  275. "move %[samples_x], %[samples] \n\t"
  276. "mtc1 $zero, %[v0] \n\t"
  277. "mtc1 $zero, %[v1] \n\t"
  278. "mtc1 $zero, %[v2] \n\t"
  279. "mtc1 $zero, %[v3] \n\t"
  280. "mtc1 $zero, %[v4] \n\t"
  281. "mtc1 $zero, %[v5] \n\t"
  282. "mtc1 $zero, %[v6] \n\t"
  283. "mtc1 $zero, %[v7] \n\t"
  284. "addiu %[i1], %[i], 4 \n\t"
  285. "addiu %[i2], %[i], 8 \n\t"
  286. PTR_L " %[samples_p], 0(%[samples_x]) \n\t"
  287. "addiu %[i3], %[i], 12 \n\t"
  288. PTR_ADDU "%[samples_end],%[samples_x], %[j] \n\t"
  289. "move %[samples_sw], %[samples_p] \n\t"
  290. "1: \n\t" // start of the inner for loop (for (j = 0; j < in_ch; j++))
  291. "lwc1 %[matrix_j], 0(%[matrix_p]) \n\t"
  292. "lwc1 %[matrix_j2], 4(%[matrix_p]) \n\t"
  293. "lwxc1 %[samples0], %[i](%[samples_p]) \n\t"
  294. "lwxc1 %[samples1], %[i1](%[samples_p]) \n\t"
  295. "lwxc1 %[samples2], %[i2](%[samples_p]) \n\t"
  296. "lwxc1 %[samples3], %[i3](%[samples_p]) \n\t"
  297. PTR_ADDIU "%[matrix_p], 8 \n\t"
  298. PTR_ADDIU "%[samples_x]," PTRSIZE " \n\t"
  299. "madd.s %[v0], %[v0], %[samples0], %[matrix_j] \n\t"
  300. "madd.s %[v1], %[v1], %[samples1], %[matrix_j] \n\t"
  301. "madd.s %[v2], %[v2], %[samples2], %[matrix_j] \n\t"
  302. "madd.s %[v3], %[v3], %[samples3], %[matrix_j] \n\t"
  303. "madd.s %[v4], %[v4], %[samples0], %[matrix_j2]\n\t"
  304. "madd.s %[v5], %[v5], %[samples1], %[matrix_j2]\n\t"
  305. "madd.s %[v6], %[v6], %[samples2], %[matrix_j2]\n\t"
  306. "madd.s %[v7], %[v7], %[samples3], %[matrix_j2]\n\t"
  307. "bne %[samples_x], %[samples_end], 1b \n\t"
  308. PTR_L " %[samples_p], 0(%[samples_x]) \n\t"
  309. PTR_L " %[samples_p], " PTRSIZE "(%[samples]) \n\t"
  310. "swxc1 %[v0], %[i](%[samples_sw]) \n\t"
  311. "swxc1 %[v1], %[i1](%[samples_sw]) \n\t"
  312. "swxc1 %[v2], %[i2](%[samples_sw]) \n\t"
  313. "swxc1 %[v3], %[i3](%[samples_sw]) \n\t"
  314. "swxc1 %[v4], %[i](%[samples_p]) \n\t"
  315. "addiu %[i], 16 \n\t"
  316. "swxc1 %[v5], %[i1](%[samples_p]) \n\t"
  317. "swxc1 %[v6], %[i2](%[samples_p]) \n\t"
  318. "bne %[i], %[len], 2b \n\t"
  319. " swxc1 %[v7], %[i3](%[samples_p]) \n\t"
  320. "3: \n\t"
  321. "bne %[out_ch], %[i2], 6f \n\t" // if (out_ch == 1)
  322. " nop \n\t"
  323. "5: \n\t" // start of the outer for loop (for (i = 0; i < len; i+=4))
  324. "move %[matrix_p], %[matrix] \n\t"
  325. "move %[samples_x], %[samples] \n\t"
  326. "mtc1 $zero, %[v0] \n\t"
  327. "mtc1 $zero, %[v1] \n\t"
  328. "mtc1 $zero, %[v2] \n\t"
  329. "mtc1 $zero, %[v3] \n\t"
  330. "addiu %[i1], %[i], 4 \n\t"
  331. "addiu %[i2], %[i], 8 \n\t"
  332. PTR_L " %[samples_p], 0(%[samples_x]) \n\t"
  333. "addiu %[i3], %[i], 12 \n\t"
  334. PTR_ADDU "%[samples_end],%[samples_x], %[j] \n\t"
  335. "move %[samples_sw], %[samples_p] \n\t"
  336. "4: \n\t" // start of the inner for loop (for (j = 0; j < in_ch; j++))
  337. "lwc1 %[matrix_j], 0(%[matrix_p]) \n\t"
  338. "lwxc1 %[samples0], %[i](%[samples_p]) \n\t"
  339. "lwxc1 %[samples1], %[i1](%[samples_p]) \n\t"
  340. "lwxc1 %[samples2], %[i2](%[samples_p]) \n\t"
  341. "lwxc1 %[samples3], %[i3](%[samples_p]) \n\t"
  342. PTR_ADDIU "%[matrix_p], 8 \n\t"
  343. PTR_ADDIU "%[samples_x]," PTRSIZE " \n\t"
  344. "madd.s %[v0], %[v0], %[samples0], %[matrix_j] \n\t"
  345. "madd.s %[v1], %[v1], %[samples1], %[matrix_j] \n\t"
  346. "madd.s %[v2], %[v2], %[samples2], %[matrix_j] \n\t"
  347. "madd.s %[v3], %[v3], %[samples3], %[matrix_j] \n\t"
  348. "bne %[samples_x], %[samples_end], 4b \n\t"
  349. PTR_L " %[samples_p], 0(%[samples_x]) \n\t"
  350. "swxc1 %[v0], %[i](%[samples_sw]) \n\t"
  351. "addiu %[i], 16 \n\t"
  352. "swxc1 %[v1], %[i1](%[samples_sw]) \n\t"
  353. "swxc1 %[v2], %[i2](%[samples_sw]) \n\t"
  354. "bne %[i], %[len], 5b \n\t"
  355. " swxc1 %[v3], %[i3](%[samples_sw]) \n\t"
  356. "6: \n\t"
  357. ".set pop"
  358. :[samples_p]"=&r"(samples_p), [matrix_j]"=&f"(matrix_j), [matrix_j2]"=&f"(matrix_j2),
  359. [samples0]"=&f"(samples0), [samples1]"=&f"(samples1),
  360. [samples2]"=&f"(samples2), [samples3]"=&f"(samples3),
  361. [v0]"=&f"(v0), [v1]"=&f"(v1), [v2]"=&f"(v2), [v3]"=&f"(v3),
  362. [v4]"=&f"(v4), [v5]"=&f"(v5), [v6]"=&f"(v6), [v7]"=&f"(v7),
  363. [samples_x]"=&r"(samples_x), [matrix_p]"=&r"(matrix_p),
  364. [samples_end]"=&r"(samples_end), [samples_sw]"=&r"(samples_sw),
  365. [i1]"=&r"(i1), [i2]"=&r"(i2), [i3]"=&r"(i3), [i]"=&r"(i),
  366. [j]"=&r"(j), [len]"+r"(len)
  367. :[samples]"r"(samples), [matrix]"r"(matrix),
  368. [in_ch]"r"(in_ch), [out_ch]"r"(out_ch)
  369. :"memory"
  370. );
  371. }
  372. #endif
  373. #endif /* HAVE_INLINE_ASM */
  374. void ff_ac3dsp_init_mips(AC3DSPContext *c, int bit_exact) {
  375. #if HAVE_INLINE_ASM
  376. #if HAVE_MIPSDSP
  377. c->bit_alloc_calc_bap = ac3_bit_alloc_calc_bap_mips;
  378. c->update_bap_counts = ac3_update_bap_counts_mips;
  379. #endif
  380. #if HAVE_MIPSFPU
  381. c->float_to_fixed24 = float_to_fixed24_mips;
  382. c->downmix = ac3_downmix_mips;
  383. #endif
  384. #endif
  385. }