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.

120 lines
3.5KB

  1. /*
  2. * Header file for hardcoded PCM tables
  3. *
  4. * Copyright (c) 2010 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #ifndef AVCODEC_PCM_TABLEGEN_H
  23. #define AVCODEC_PCM_TABLEGEN_H
  24. #include <stdint.h>
  25. #include "libavutil/attributes.h"
  26. /* from g711.c by SUN microsystems (unrestricted use) */
  27. #define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
  28. #define QUANT_MASK (0xf) /* Quantization field mask. */
  29. #define NSEGS (8) /* Number of A-law segments. */
  30. #define SEG_SHIFT (4) /* Left shift for segment number. */
  31. #define SEG_MASK (0x70) /* Segment field mask. */
  32. #define BIAS (0x84) /* Bias for linear code. */
  33. /*
  34. * alaw2linear() - Convert an A-law value to 16-bit linear PCM
  35. *
  36. */
  37. static av_cold int alaw2linear(unsigned char a_val)
  38. {
  39. int t;
  40. int seg;
  41. a_val ^= 0x55;
  42. t = a_val & QUANT_MASK;
  43. seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
  44. if(seg) t= (t + t + 1 + 32) << (seg + 2);
  45. else t= (t + t + 1 ) << 3;
  46. return (a_val & SIGN_BIT) ? t : -t;
  47. }
  48. static av_cold int ulaw2linear(unsigned char u_val)
  49. {
  50. int t;
  51. /* Complement to obtain normal u-law value. */
  52. u_val = ~u_val;
  53. /*
  54. * Extract and bias the quantization bits. Then
  55. * shift up by the segment number and subtract out the bias.
  56. */
  57. t = ((u_val & QUANT_MASK) << 3) + BIAS;
  58. t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
  59. return (u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS);
  60. }
  61. #if CONFIG_HARDCODED_TABLES
  62. #define pcm_alaw_tableinit()
  63. #define pcm_ulaw_tableinit()
  64. #include "libavcodec/pcm_tables.h"
  65. #else
  66. /* 16384 entries per table */
  67. static uint8_t linear_to_alaw[16384];
  68. static uint8_t linear_to_ulaw[16384];
  69. static av_cold void build_xlaw_table(uint8_t *linear_to_xlaw,
  70. int (*xlaw2linear)(unsigned char),
  71. int mask)
  72. {
  73. int i, j, v, v1, v2;
  74. j = 0;
  75. for(i=0;i<128;i++) {
  76. if (i != 127) {
  77. v1 = xlaw2linear(i ^ mask);
  78. v2 = xlaw2linear((i + 1) ^ mask);
  79. v = (v1 + v2 + 4) >> 3;
  80. } else {
  81. v = 8192;
  82. }
  83. for(;j<v;j++) {
  84. linear_to_xlaw[8192 + j] = (i ^ mask);
  85. if (j > 0)
  86. linear_to_xlaw[8192 - j] = (i ^ (mask ^ 0x80));
  87. }
  88. }
  89. linear_to_xlaw[0] = linear_to_xlaw[1];
  90. }
  91. static void pcm_alaw_tableinit(void)
  92. {
  93. build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5);
  94. }
  95. static void pcm_ulaw_tableinit(void)
  96. {
  97. build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff);
  98. }
  99. #endif /* CONFIG_HARDCODED_TABLES */
  100. #endif /* AVCODEC_PCM_TABLEGEN_H */