af_aconvert_rematrix.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2011 Mina Nagy Zaki
  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. /**
  21. * @file
  22. * audio rematrixing functions, based on functions from libavcodec/resample.c
  23. */
  24. #if defined(FLOATING)
  25. # define DIV2 /2
  26. #else
  27. # define DIV2 >>1
  28. #endif
  29. REMATRIX_FUNC_SIG(stereo_to_mono_packed)
  30. {
  31. while (nb_samples >= 4) {
  32. outp[0][0] = (inp[0][0] + inp[0][1]) DIV2;
  33. outp[0][1] = (inp[0][2] + inp[0][3]) DIV2;
  34. outp[0][2] = (inp[0][4] + inp[0][5]) DIV2;
  35. outp[0][3] = (inp[0][6] + inp[0][7]) DIV2;
  36. outp[0] += 4;
  37. inp[0] += 8;
  38. nb_samples -= 4;
  39. }
  40. while (nb_samples--) {
  41. outp[0][0] = (inp[0][0] + inp[0][1]) DIV2;
  42. outp[0]++;
  43. inp[0] += 2;
  44. }
  45. }
  46. REMATRIX_FUNC_SIG(stereo_downmix_packed)
  47. {
  48. while (nb_samples--) {
  49. *outp[0]++ = inp[0][0];
  50. *outp[0]++ = inp[0][1];
  51. inp[0] += aconvert->in_nb_channels;
  52. }
  53. }
  54. REMATRIX_FUNC_SIG(mono_to_stereo_packed)
  55. {
  56. while (nb_samples >= 4) {
  57. outp[0][0] = outp[0][1] = inp[0][0];
  58. outp[0][2] = outp[0][3] = inp[0][1];
  59. outp[0][4] = outp[0][5] = inp[0][2];
  60. outp[0][6] = outp[0][7] = inp[0][3];
  61. outp[0] += 8;
  62. inp[0] += 4;
  63. nb_samples -= 4;
  64. }
  65. while (nb_samples--) {
  66. outp[0][0] = outp[0][1] = inp[0][0];
  67. outp[0] += 2;
  68. inp[0] += 1;
  69. }
  70. }
  71. /**
  72. * This is for when we have more than 2 input channels, need to downmix to mono
  73. * and do not have a conversion formula available. We just use first two input
  74. * channels - left and right. This is a placeholder until more conversion
  75. * functions are written.
  76. */
  77. REMATRIX_FUNC_SIG(mono_downmix_packed)
  78. {
  79. while (nb_samples--) {
  80. outp[0][0] = (inp[0][0] + inp[0][1]) DIV2;
  81. inp[0] += aconvert->in_nb_channels;
  82. outp[0]++;
  83. }
  84. }
  85. REMATRIX_FUNC_SIG(mono_downmix_planar)
  86. {
  87. FMT_TYPE *out = outp[0];
  88. while (nb_samples >= 4) {
  89. out[0] = (inp[0][0] + inp[1][0]) DIV2;
  90. out[1] = (inp[0][1] + inp[1][1]) DIV2;
  91. out[2] = (inp[0][2] + inp[1][2]) DIV2;
  92. out[3] = (inp[0][3] + inp[1][3]) DIV2;
  93. out += 4;
  94. inp[0] += 4;
  95. inp[1] += 4;
  96. nb_samples -= 4;
  97. }
  98. while (nb_samples--) {
  99. out[0] = (inp[0][0] + inp[1][0]) DIV2;
  100. out++;
  101. inp[0]++;
  102. inp[1]++;
  103. }
  104. }
  105. /* Stereo to 5.1 output */
  106. REMATRIX_FUNC_SIG(stereo_to_surround_5p1_packed)
  107. {
  108. while (nb_samples--) {
  109. outp[0][0] = inp[0][0]; /* left */
  110. outp[0][1] = inp[0][1]; /* right */
  111. outp[0][2] = (inp[0][0] + inp[0][1]) DIV2; /* center */
  112. outp[0][3] = 0; /* low freq */
  113. outp[0][4] = 0; /* FIXME: left surround: -3dB or -6dB or -9dB of stereo left */
  114. outp[0][5] = 0; /* FIXME: right surroud: -3dB or -6dB or -9dB of stereo right */
  115. inp[0] += 2;
  116. outp[0] += 6;
  117. }
  118. }
  119. REMATRIX_FUNC_SIG(stereo_to_surround_5p1_planar)
  120. {
  121. while (nb_samples--) {
  122. *outp[0]++ = *inp[0]; /* left */
  123. *outp[1]++ = *inp[1]; /* right */
  124. *outp[2]++ = (*inp[0] + *inp[1]) DIV2; /* center */
  125. *outp[3]++ = 0; /* low freq */
  126. *outp[4]++ = 0; /* FIXME: left surround: -3dB or -6dB or -9dB of stereo left */
  127. *outp[5]++ = 0; /* FIXME: right surroud: -3dB or -6dB or -9dB of stereo right */
  128. inp[0]++; inp[1]++;
  129. }
  130. }
  131. /*
  132. 5.1 to stereo input: [fl, fr, c, lfe, rl, rr]
  133. - Left = front_left + rear_gain * rear_left + center_gain * center
  134. - Right = front_right + rear_gain * rear_right + center_gain * center
  135. Where rear_gain is usually around 0.5-1.0 and
  136. center_gain is almost always 0.7 (-3 dB)
  137. */
  138. REMATRIX_FUNC_SIG(surround_5p1_to_stereo_packed)
  139. {
  140. while (nb_samples--) {
  141. *outp[0]++ = inp[0][0] + (0.5 * inp[0][4]) + (0.7 * inp[0][2]); //FIXME CLIPPING!
  142. *outp[0]++ = inp[0][1] + (0.5 * inp[0][5]) + (0.7 * inp[0][2]); //FIXME CLIPPING!
  143. inp[0] += 6;
  144. }
  145. }
  146. REMATRIX_FUNC_SIG(surround_5p1_to_stereo_planar)
  147. {
  148. while (nb_samples--) {
  149. *outp[0]++ = *inp[0] + (0.5 * *inp[4]) + (0.7 * *inp[2]); //FIXME CLIPPING!
  150. *outp[1]++ = *inp[1] + (0.5 * *inp[5]) + (0.7 * *inp[2]); //FIXME CLIPPING!
  151. inp[0]++; inp[1]++; inp[2]++; inp[3]++; inp[4]++; inp[5]++;
  152. }
  153. }
  154. #undef DIV2
  155. #undef REMATRIX_FUNC_NAME
  156. #undef FMT_TYPE