ac3dec_fixed.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. * Author: Stanislav Ocovaj (socovaj@mips.com)
  30. *
  31. * AC3 fixed-point decoder for MIPS platforms
  32. *
  33. * This file is part of FFmpeg.
  34. *
  35. * FFmpeg is free software; you can redistribute it and/or
  36. * modify it under the terms of the GNU Lesser General Public
  37. * License as published by the Free Software Foundation; either
  38. * version 2.1 of the License, or (at your option) any later version.
  39. *
  40. * FFmpeg is distributed in the hope that it will be useful,
  41. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  42. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  43. * Lesser General Public License for more details.
  44. *
  45. * You should have received a copy of the GNU Lesser General Public
  46. * License along with FFmpeg; if not, write to the Free Software
  47. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  48. */
  49. #define FFT_FLOAT 0
  50. #define USE_FIXED 1
  51. #define FFT_FIXED_32 1
  52. #include "ac3dec.h"
  53. static const int end_freq_inv_tab[8] =
  54. {
  55. 50529027, 44278013, 39403370, 32292987, 27356480, 23729101, 20951060, 18755316
  56. };
  57. static void scale_coefs (
  58. int32_t *dst,
  59. const int32_t *src,
  60. int dynrng,
  61. int len)
  62. {
  63. int i, shift;
  64. unsigned mul, round;
  65. int temp, temp1, temp2, temp3, temp4, temp5, temp6, temp7;
  66. mul = (dynrng & 0x1f) + 0x20;
  67. shift = 4 - (sign_extend(dynrng, 9) >> 5);
  68. if (shift > 0 ) {
  69. round = 1 << (shift-1);
  70. for (i=0; i<len; i+=8) {
  71. temp = src[i] * mul;
  72. temp1 = src[i+1] * mul;
  73. temp = temp + round;
  74. temp2 = src[i+2] * mul;
  75. temp1 = temp1 + round;
  76. dst[i] = temp >> shift;
  77. temp3 = src[i+3] * mul;
  78. temp2 = temp2 + round;
  79. dst[i+1] = temp1 >> shift;
  80. temp4 = src[i + 4] * mul;
  81. temp3 = temp3 + round;
  82. dst[i+2] = temp2 >> shift;
  83. temp5 = src[i+5] * mul;
  84. temp4 = temp4 + round;
  85. dst[i+3] = temp3 >> shift;
  86. temp6 = src[i+6] * mul;
  87. dst[i+4] = temp4 >> shift;
  88. temp5 = temp5 + round;
  89. temp7 = src[i+7] * mul;
  90. temp6 = temp6 + round;
  91. dst[i+5] = temp5 >> shift;
  92. temp7 = temp7 + round;
  93. dst[i+6] = temp6 >> shift;
  94. dst[i+7] = temp7 >> shift;
  95. }
  96. } else {
  97. shift = -shift;
  98. mul <<= shift;
  99. for (i=0; i<len; i+=8) {
  100. temp = src[i] * mul;
  101. temp1 = src[i+1] * mul;
  102. temp2 = src[i+2] * mul;
  103. dst[i] = temp;
  104. temp3 = src[i+3] * mul;
  105. dst[i+1] = temp1;
  106. temp4 = src[i + 4] * mul;
  107. dst[i+2] = temp2;
  108. temp5 = src[i+5] * mul;
  109. dst[i+3] = temp3;
  110. temp6 = src[i+6] * mul;
  111. dst[i+4] = temp4;
  112. temp7 = src[i+7] * mul;
  113. dst[i+5] = temp5;
  114. dst[i+6] = temp6;
  115. dst[i+7] = temp7;
  116. }
  117. }
  118. }
  119. /**
  120. * Downmix samples from original signal to stereo or mono (this is for 16-bit samples
  121. * and fixed point decoder - original (for 32-bit samples) is in ac3dsp.c).
  122. */
  123. static void ac3_downmix_c_fixed16(int16_t **samples, int16_t **matrix,
  124. int out_ch, int in_ch, int len)
  125. {
  126. int i, j;
  127. int v0, v1;
  128. if (out_ch == 2) {
  129. for (i = 0; i < len; i++) {
  130. v0 = v1 = 0;
  131. for (j = 0; j < in_ch; j++) {
  132. v0 += samples[j][i] * matrix[0][j];
  133. v1 += samples[j][i] * matrix[1][j];
  134. }
  135. samples[0][i] = (v0+2048)>>12;
  136. samples[1][i] = (v1+2048)>>12;
  137. }
  138. } else if (out_ch == 1) {
  139. for (i = 0; i < len; i++) {
  140. v0 = 0;
  141. for (j = 0; j < in_ch; j++)
  142. v0 += samples[j][i] * matrix[0][j];
  143. samples[0][i] = (v0+2048)>>12;
  144. }
  145. }
  146. }
  147. #include "eac3dec.c"
  148. #include "ac3dec.c"
  149. static const AVOption options[] = {
  150. { "cons_noisegen", "enable consistent noise generation", OFFSET(consistent_noise_generation), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, PAR },
  151. { "drc_scale", "percentage of dynamic range compression to apply", OFFSET(drc_scale), AV_OPT_TYPE_FLOAT, {.dbl = 1.0}, 0.0, 6.0, PAR },
  152. { "heavy_compr", "enable heavy dynamic range compression", OFFSET(heavy_compression), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, PAR },
  153. { NULL},
  154. };
  155. static const AVClass ac3_decoder_class = {
  156. .class_name = "Fixed-Point AC-3 Decoder",
  157. .item_name = av_default_item_name,
  158. .option = options,
  159. .version = LIBAVUTIL_VERSION_INT,
  160. };
  161. AVCodec ff_ac3_fixed_decoder = {
  162. .name = "ac3_fixed",
  163. .type = AVMEDIA_TYPE_AUDIO,
  164. .id = AV_CODEC_ID_AC3,
  165. .priv_data_size = sizeof (AC3DecodeContext),
  166. .init = ac3_decode_init,
  167. .close = ac3_decode_end,
  168. .decode = ac3_decode_frame,
  169. .capabilities = AV_CODEC_CAP_DR1,
  170. .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"),
  171. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
  172. AV_SAMPLE_FMT_NONE },
  173. .priv_class = &ac3_decoder_class,
  174. };