layer1.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * layer1.c: Mpeg Layer-1 audio decoder
  3. *
  4. * Copyright (C) 1999-2010 The L.A.M.E. project
  5. *
  6. * Initially written by Michael Hipp, see also AUTHORS and README.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Library General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library 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. * Library General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Library General Public
  19. * License along with this library; if not, write to the
  20. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. * Boston, MA 02111-1307, USA.
  22. */
  23. /* $Id: layer1.c,v 1.31 2017/08/23 13:22:23 robert Exp $ */
  24. #ifdef HAVE_CONFIG_H
  25. # include <config.h>
  26. #endif
  27. #include <assert.h>
  28. #include "common.h"
  29. #include "decode_i386.h"
  30. #ifdef WITH_DMALLOC
  31. #include <dmalloc.h>
  32. #endif
  33. #include "layer1.h"
  34. static int gd_are_hip_tables_layer1_initialized = 0;
  35. void
  36. hip_init_tables_layer1(void)
  37. {
  38. if (gd_are_hip_tables_layer1_initialized) {
  39. return;
  40. }
  41. gd_are_hip_tables_layer1_initialized = 1;
  42. }
  43. typedef struct sideinfo_layer_I_struct
  44. {
  45. unsigned char allocation[SBLIMIT][2];
  46. unsigned char scalefactor[SBLIMIT][2];
  47. } sideinfo_layer_I;
  48. static int
  49. I_step_one(PMPSTR mp, sideinfo_layer_I* si)
  50. {
  51. struct frame *fr = &(mp->fr);
  52. int jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : 32;
  53. int i;
  54. int illegal_value_detected = 0;
  55. unsigned char const ba15 = 15; /* bit pattern not allowed, looks like sync(?) */
  56. memset(si, 0, sizeof(*si));
  57. assert(fr->stereo == 1 || fr->stereo == 2);
  58. if (fr->stereo == 2) {
  59. for (i = 0; i < jsbound; i++) {
  60. unsigned char b0 = get_leq_8_bits(mp, 4); /* values 0-15 */
  61. unsigned char b1 = get_leq_8_bits(mp, 4); /* values 0-15 */
  62. si->allocation[i][0] = b0;
  63. si->allocation[i][1] = b1;
  64. if (b0 == ba15 || b1 == ba15)
  65. illegal_value_detected = 1;
  66. }
  67. for (i = jsbound; i < SBLIMIT; i++) {
  68. unsigned char b = get_leq_8_bits(mp, 4); /* values 0-15 */
  69. si->allocation[i][0] = b;
  70. si->allocation[i][1] = b;
  71. if (b == ba15)
  72. illegal_value_detected = 1;
  73. }
  74. for (i = 0; i < SBLIMIT; i++) {
  75. unsigned char n0 = si->allocation[i][0];
  76. unsigned char n1 = si->allocation[i][1];
  77. unsigned char b0 = n0 ? get_leq_8_bits(mp, 6) : 0; /* values 0-63 */
  78. unsigned char b1 = n1 ? get_leq_8_bits(mp, 6) : 0; /* values 0-63 */
  79. si->scalefactor[i][0] = b0;
  80. si->scalefactor[i][1] = b1;
  81. }
  82. }
  83. else {
  84. for (i = 0; i < SBLIMIT; i++) {
  85. unsigned char b0 = get_leq_8_bits(mp, 4); /* values 0-15 */
  86. si->allocation[i][0] = b0;
  87. if (b0 == ba15)
  88. illegal_value_detected = 1;
  89. }
  90. for (i = 0; i < SBLIMIT; i++) {
  91. unsigned char n0 = si->allocation[i][0];
  92. unsigned char b0 = n0 ? get_leq_8_bits(mp, 6) : 0; /* values 0-63 */
  93. si->scalefactor[i][0] = b0;
  94. }
  95. }
  96. return illegal_value_detected;
  97. }
  98. static void
  99. I_step_two(PMPSTR mp, sideinfo_layer_I *si, real fraction[2][SBLIMIT])
  100. {
  101. double r0, r1;
  102. struct frame *fr = &(mp->fr);
  103. int ds_limit = fr->down_sample_sblimit;
  104. int i;
  105. assert(fr->stereo == 1 || fr->stereo == 2);
  106. if (fr->stereo == 2) {
  107. int jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : 32;
  108. for (i = 0; i < jsbound; i++) {
  109. unsigned char i0 = si->scalefactor[i][0];
  110. unsigned char i1 = si->scalefactor[i][1];
  111. unsigned char n0 = si->allocation[i][0];
  112. unsigned char n1 = si->allocation[i][1];
  113. assert( i0 < 64 );
  114. assert( i1 < 64 );
  115. assert( n0 < 16 );
  116. assert( n1 < 16 );
  117. if (n0 > 0) {
  118. unsigned short v = get_leq_16_bits(mp, n0 + 1); /* 0-65535 */
  119. r0 = (((-1) << n0) + v + 1) * muls[n0 + 1][i0];
  120. }
  121. else {
  122. r0 = 0;
  123. }
  124. if (n1 > 0) {
  125. unsigned short v = get_leq_16_bits(mp, n1 + 1); /* 0-65535 */
  126. r1 = (((-1) << n1) + v + 1) * muls[n1 + 1][i1];
  127. }
  128. else {
  129. r1 = 0;
  130. }
  131. fraction[0][i] = (real)r0;
  132. fraction[1][i] = (real)r1;
  133. }
  134. for (i = jsbound; i < SBLIMIT; i++) {
  135. unsigned char i0 = si->scalefactor[i][0];
  136. unsigned char i1 = si->scalefactor[i][1];
  137. unsigned char n = si->allocation[i][0];
  138. assert( i0 < 64 );
  139. assert( i1 < 64 );
  140. assert( n < 16 );
  141. if (n > 0) {
  142. unsigned short v = get_leq_16_bits(mp, n + 1); /* 0-65535 */
  143. unsigned int w = (((-1) << n) + v + 1);
  144. r0 = w * muls[n + 1][i0];
  145. r1 = w * muls[n + 1][i1];
  146. }
  147. else {
  148. r0 = r1 = 0;
  149. }
  150. fraction[0][i] = (real)r0;
  151. fraction[1][i] = (real)r1;
  152. }
  153. for (i = ds_limit; i < SBLIMIT; i++) {
  154. fraction[0][i] = 0.0;
  155. fraction[1][i] = 0.0;
  156. }
  157. }
  158. else {
  159. for (i = 0; i < SBLIMIT; i++) {
  160. unsigned char n = si->allocation[i][0];
  161. unsigned char j = si->scalefactor[i][0];
  162. assert( j < 64 );
  163. assert( n < 16 );
  164. if (n > 0) {
  165. unsigned short v = get_leq_16_bits(mp, n + 1);
  166. r0 = (((-1) << n) + v + 1) * muls[n + 1][j];
  167. }
  168. else {
  169. r0 = 0;
  170. }
  171. fraction[0][i] = (real)r0;
  172. }
  173. for (i = ds_limit; i < SBLIMIT; i++) {
  174. fraction[0][i] = 0.0;
  175. }
  176. }
  177. }
  178. int
  179. decode_layer1_sideinfo(PMPSTR mp)
  180. {
  181. (void) mp;
  182. /* FIXME: extract side information and check values */
  183. return 0;
  184. }
  185. int
  186. decode_layer1_frame(PMPSTR mp, unsigned char *pcm_sample, int *pcm_point)
  187. {
  188. real fraction[2][SBLIMIT]; /* FIXME: change real -> double ? */
  189. sideinfo_layer_I si;
  190. struct frame *fr = &(mp->fr);
  191. int single = fr->single;
  192. int i, clip = 0;
  193. if (I_step_one(mp, &si)) {
  194. lame_report_fnc(mp->report_err, "hip: Aborting layer 1 decode, illegal bit allocation value\n");
  195. return -1;
  196. }
  197. if (fr->stereo == 1 || single == 3)
  198. single = 0;
  199. if (single >= 0) {
  200. /* decoding one of possibly two channels */
  201. for (i = 0; i < SCALE_BLOCK; i++) {
  202. I_step_two(mp, &si, fraction);
  203. clip += synth_1to1_mono(mp, (real *) fraction[single], pcm_sample, pcm_point);
  204. }
  205. }
  206. else {
  207. for (i = 0; i < SCALE_BLOCK; i++) {
  208. int p1 = *pcm_point;
  209. I_step_two(mp, &si, fraction);
  210. clip += synth_1to1(mp, (real *) fraction[0], 0, pcm_sample, &p1);
  211. clip += synth_1to1(mp, (real *) fraction[1], 1, pcm_sample, pcm_point);
  212. }
  213. }
  214. return clip;
  215. }