pcm_tablegen.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #define VIDC_SIGN_BIT (1)
  34. #define VIDC_QUANT_MASK (0x1E)
  35. #define VIDC_QUANT_SHIFT (1)
  36. #define VIDC_SEG_SHIFT (5)
  37. #define VIDC_SEG_MASK (0xE0)
  38. /* alaw2linear() - Convert an A-law value to 16-bit linear PCM */
  39. static av_cold int alaw2linear(unsigned char a_val)
  40. {
  41. int t;
  42. int seg;
  43. a_val ^= 0x55;
  44. t = a_val & QUANT_MASK;
  45. seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
  46. if(seg) t= (t + t + 1 + 32) << (seg + 2);
  47. else t= (t + t + 1 ) << 3;
  48. return (a_val & SIGN_BIT) ? t : -t;
  49. }
  50. static av_cold int ulaw2linear(unsigned char u_val)
  51. {
  52. int t;
  53. /* Complement to obtain normal u-law value. */
  54. u_val = ~u_val;
  55. /*
  56. * Extract and bias the quantization bits. Then
  57. * shift up by the segment number and subtract out the bias.
  58. */
  59. t = ((u_val & QUANT_MASK) << 3) + BIAS;
  60. t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
  61. return (u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS);
  62. }
  63. static av_cold int vidc2linear(unsigned char u_val)
  64. {
  65. int t;
  66. /*
  67. * Extract and bias the quantization bits. Then
  68. * shift up by the segment number and subtract out the bias.
  69. */
  70. t = (((u_val & VIDC_QUANT_MASK) >> VIDC_QUANT_SHIFT) << 3) + BIAS;
  71. t <<= ((unsigned)u_val & VIDC_SEG_MASK) >> VIDC_SEG_SHIFT;
  72. return (u_val & VIDC_SIGN_BIT) ? (BIAS - t) : (t - BIAS);
  73. }
  74. #if CONFIG_HARDCODED_TABLES
  75. #define pcm_alaw_tableinit()
  76. #define pcm_ulaw_tableinit()
  77. #define pcm_vidc_tableinit()
  78. #include "libavcodec/pcm_tables.h"
  79. #else
  80. /* 16384 entries per table */
  81. static uint8_t linear_to_alaw[16384];
  82. static uint8_t linear_to_ulaw[16384];
  83. static uint8_t linear_to_vidc[16384];
  84. static av_cold void build_xlaw_table(uint8_t *linear_to_xlaw,
  85. int (*xlaw2linear)(unsigned char),
  86. int mask)
  87. {
  88. int i, j, v, v1, v2;
  89. j = 1;
  90. linear_to_xlaw[8192] = mask;
  91. for(i=0;i<127;i++) {
  92. v1 = xlaw2linear(i ^ mask);
  93. v2 = xlaw2linear((i + 1) ^ mask);
  94. v = (v1 + v2 + 4) >> 3;
  95. for(;j<v;j+=1) {
  96. linear_to_xlaw[8192 - j] = (i ^ (mask ^ 0x80));
  97. linear_to_xlaw[8192 + j] = (i ^ mask);
  98. }
  99. }
  100. for(;j<8192;j++) {
  101. linear_to_xlaw[8192 - j] = (127 ^ (mask ^ 0x80));
  102. linear_to_xlaw[8192 + j] = (127 ^ mask);
  103. }
  104. linear_to_xlaw[0] = linear_to_xlaw[1];
  105. }
  106. static void pcm_alaw_tableinit(void)
  107. {
  108. build_xlaw_table(linear_to_alaw, alaw2linear, 0xd5);
  109. }
  110. static void pcm_ulaw_tableinit(void)
  111. {
  112. build_xlaw_table(linear_to_ulaw, ulaw2linear, 0xff);
  113. }
  114. static void pcm_vidc_tableinit(void)
  115. {
  116. build_xlaw_table(linear_to_vidc, vidc2linear, 0xff);
  117. }
  118. #endif /* CONFIG_HARDCODED_TABLES */
  119. #endif /* AVCODEC_PCM_TABLEGEN_H */