/media/libstagefright/codecs/mp3dec/src/pvmp3_decode_huff_cw.cpp

https://bitbucket.org/aways/android_frameworks_av · C++ · 758 lines · 489 code · 98 blank · 171 comment · 115 complexity · 947d2648e59bdaf9f9dd301e847950f9 MD5 · raw file

  1. /* ------------------------------------------------------------------
  2. * Copyright (C) 1998-2009 PacketVideo
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied.
  14. * See the License for the specific language governing permissions
  15. * and limitations under the License.
  16. * -------------------------------------------------------------------
  17. */
  18. /*
  19. ------------------------------------------------------------------------------
  20. PacketVideo Corp.
  21. MP3 Decoder Library
  22. Filename: pvmp3_decode_huff_cw.cpp
  23. Funtions:
  24. pvmp3_decode_huff_cw_tab0
  25. pvmp3_decode_huff_cw_tab1
  26. pvmp3_decode_huff_cw_tab2
  27. pvmp3_decode_huff_cw_tab3
  28. pvmp3_decode_huff_cw_tab5
  29. pvmp3_decode_huff_cw_tab6
  30. pvmp3_decode_huff_cw_tab7
  31. pvmp3_decode_huff_cw_tab8
  32. pvmp3_decode_huff_cw_tab9
  33. pvmp3_decode_huff_cw_tab10
  34. pvmp3_decode_huff_cw_tab11
  35. pvmp3_decode_huff_cw_tab12
  36. pvmp3_decode_huff_cw_tab13
  37. pvmp3_decode_huff_cw_tab15
  38. pvmp3_decode_huff_cw_tab16
  39. pvmp3_decode_huff_cw_tab24
  40. pvmp3_decode_huff_cw_tab32
  41. pvmp3_decode_huff_cw_tab33
  42. Date: 09/21/2007
  43. ------------------------------------------------------------------------------
  44. REVISION HISTORY
  45. Description:
  46. ------------------------------------------------------------------------------
  47. INPUT AND OUTPUT DEFINITIONS
  48. Inputs:
  49. BITS *pMainData = pointer to input mp3 Main data bit stream
  50. Outputs:
  51. cw = bit field extracted from a leaf entry of packed mp3 Huffman Tables
  52. ------------------------------------------------------------------------------
  53. FUNCTION DESCRIPTION
  54. These functions are used to decode huffman codewords from the input
  55. bitstream using combined binary search and look-up table approach.
  56. ------------------------------------------------------------------------------
  57. REQUIREMENTS
  58. ------------------------------------------------------------------------------
  59. REFERENCES
  60. [1] ISO MPEG Audio Subgroup Software Simulation Group (1996)
  61. ISO 13818-3 MPEG-2 Audio Decoder - Lower Sampling Frequency Extension
  62. [2] Introduction to Algorithms,
  63. Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest.
  64. The MIT press, 1990
  65. [3] "Selecting an Optimal Huffman Decoder for AAC",
  66. Vladimir Z. Mesarovic, et al.
  67. AES 111th Convention, September 21-24, 2001, New York, USA
  68. ------------------------------------------------------------------------------
  69. PSEUDO-CODE
  70. ------------------------------------------------------------------------------
  71. */
  72. /*----------------------------------------------------------------------------
  73. ; INCLUDES
  74. ----------------------------------------------------------------------------*/
  75. #include "pv_mp3dec_fxd_op.h"
  76. #include "pvmp3_tables.h"
  77. #include "pvmp3_getbits.h"
  78. #include "pvmp3_decode_huff_cw.h"
  79. /*----------------------------------------------------------------------------
  80. ; MACROS
  81. ; Define module specific macros here
  82. ----------------------------------------------------------------------------*/
  83. /*----------------------------------------------------------------------------
  84. ; DEFINES
  85. ; Include all pre-processor statements here. Include conditional
  86. ; compile variables also.
  87. ----------------------------------------------------------------------------*/
  88. /*----------------------------------------------------------------------------
  89. ; LOCAL FUNCTION DEFINITIONS
  90. ; Function Prototype declaration
  91. ----------------------------------------------------------------------------*/
  92. /*----------------------------------------------------------------------------
  93. ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
  94. ; Variable declaration - defined here and used outside this module
  95. ----------------------------------------------------------------------------*/
  96. /*----------------------------------------------------------------------------
  97. ; EXTERNAL FUNCTION REFERENCES
  98. ; Declare functions defined elsewhere and referenced in this module
  99. ----------------------------------------------------------------------------*/
  100. /*----------------------------------------------------------------------------
  101. ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
  102. ; Declare variables used in this module but defined elsewhere
  103. ----------------------------------------------------------------------------*/
  104. /*----------------------------------------------------------------------------
  105. ; FUNCTION CODE
  106. ----------------------------------------------------------------------------*/
  107. uint16 pvmp3_decode_huff_cw_tab0(tmp3Bits *pMainData)
  108. {
  109. OSCL_UNUSED_ARG(pMainData);
  110. return(0);
  111. }
  112. /*----------------------------------------------------------------------------
  113. ; FUNCTION CODE
  114. ----------------------------------------------------------------------------*/
  115. uint16 pvmp3_decode_huff_cw_tab1(tmp3Bits *pMainData)
  116. {
  117. uint32 tmp;
  118. uint16 cw;
  119. tmp = getUpTo9bits(pMainData, 3); /* hufftable1 */
  120. cw = *(huffTable_1 + tmp);
  121. pMainData->usedBits -= (3 - (cw & 0xFF));
  122. return(cw >> 8);
  123. }
  124. /*----------------------------------------------------------------------------
  125. ; FUNCTION CODE
  126. ----------------------------------------------------------------------------*/
  127. uint16 pvmp3_decode_huff_cw_tab2(tmp3Bits *pMainData)
  128. {
  129. uint32 tmp;
  130. uint16 cw;
  131. tmp = getUpTo9bits(pMainData, 6); /* huffTable_2,3 */
  132. if (tmp >> 3)
  133. {
  134. tmp = (tmp >> 3) - 1;
  135. }
  136. else
  137. {
  138. tmp = tmp + 7;
  139. }
  140. cw = *(huffTable_2 + tmp);
  141. pMainData->usedBits -= (6 - (cw & 0xFF));
  142. return(cw >> 8);
  143. }
  144. /*----------------------------------------------------------------------------
  145. ; FUNCTION CODE
  146. ----------------------------------------------------------------------------*/
  147. uint16 pvmp3_decode_huff_cw_tab3(tmp3Bits *pMainData)
  148. {
  149. uint32 tmp;
  150. uint16 cw;
  151. tmp = getUpTo9bits(pMainData, 6); /* huffTable_2,3 */
  152. if (tmp >> 3)
  153. {
  154. tmp = (tmp >> 3) - 1;
  155. }
  156. else
  157. {
  158. tmp = tmp + 7;
  159. }
  160. cw = *(huffTable_3 + tmp);
  161. pMainData->usedBits -= (6 - (cw & 0xFF));
  162. return(cw >> 8);
  163. }
  164. /*----------------------------------------------------------------------------
  165. ; FUNCTION CODE
  166. ----------------------------------------------------------------------------*/
  167. uint16 pvmp3_decode_huff_cw_tab5(tmp3Bits *pMainData)
  168. {
  169. uint32 tmp;
  170. uint16 cw;
  171. tmp = getUpTo9bits(pMainData, 8); /* huffTable_5 */
  172. if ((tmp >> 5))
  173. {
  174. tmp = (tmp >> 5) - 1;
  175. }
  176. else if ((tmp >> 1) >= 2)
  177. {
  178. tmp = (tmp >> 1) - 2 + 7;
  179. }
  180. else
  181. {
  182. tmp = (tmp & 3) + 21;
  183. }
  184. cw = *(huffTable_5 + tmp);
  185. pMainData->usedBits -= (8 - (cw & 0xFF));
  186. return(cw >> 8);
  187. }
  188. /*----------------------------------------------------------------------------
  189. ; FUNCTION CODE
  190. ----------------------------------------------------------------------------*/
  191. uint16 pvmp3_decode_huff_cw_tab6(tmp3Bits *pMainData)
  192. {
  193. uint32 tmp;
  194. uint16 cw;
  195. tmp = getUpTo9bits(pMainData, 7); /* huffTable_6 */
  196. if ((tmp >> 3) >= 3)
  197. {
  198. tmp = (tmp >> 3) - 3;
  199. }
  200. else if (tmp >> 1)
  201. {
  202. tmp = (tmp >> 1) - 1 + 13;
  203. }
  204. else
  205. {
  206. tmp = tmp + 24;
  207. }
  208. cw = *(huffTable_6 + tmp);
  209. pMainData->usedBits -= (7 - (cw & 0xFF));
  210. return(cw >> 8);
  211. }
  212. /*----------------------------------------------------------------------------
  213. ; FUNCTION CODE
  214. ----------------------------------------------------------------------------*/
  215. uint16 pvmp3_decode_huff_cw_tab7(tmp3Bits *pMainData)
  216. {
  217. uint32 tmp;
  218. uint16 cw;
  219. tmp = getUpTo17bits(pMainData, 10); /* huffTable_7 */
  220. if ((tmp >> 7) >= 2)
  221. {
  222. tmp = (tmp >> 7) - 2;
  223. }
  224. else if ((tmp >> 4) >= 7)
  225. {
  226. tmp = (tmp >> 4) - 7 + 6;
  227. }
  228. else if ((tmp >> 1) >= 2)
  229. {
  230. tmp = (tmp >> 1) - 2 + 15;
  231. }
  232. else
  233. {
  234. tmp = (tmp & 3) + 69;
  235. }
  236. cw = *(huffTable_7 + tmp);
  237. pMainData->usedBits -= (10 - (cw & 0xFF));
  238. return(cw >> 8);
  239. }
  240. /*----------------------------------------------------------------------------
  241. ; FUNCTION CODE
  242. ----------------------------------------------------------------------------*/
  243. uint16 pvmp3_decode_huff_cw_tab8(tmp3Bits *pMainData)
  244. {
  245. uint32 tmp;
  246. uint16 cw;
  247. tmp = getUpTo17bits(pMainData, 11); /* huffTable_8 */
  248. if ((tmp >> 7) >= 2)
  249. {
  250. tmp = (tmp >> 7) - 2;
  251. }
  252. else if ((tmp >> 5) >= 5)
  253. {
  254. tmp = (tmp >> 5) - 5 + 14;
  255. }
  256. else if ((tmp >> 2) >= 3)
  257. {
  258. tmp = (tmp >> 2) - 3 + 17;
  259. }
  260. else
  261. {
  262. tmp = (tmp) + 54;
  263. }
  264. cw = *(huffTable_8 + tmp);
  265. pMainData->usedBits -= (11 - (cw & 0xFF));
  266. return(cw >> 8);
  267. }
  268. /*----------------------------------------------------------------------------
  269. ; FUNCTION CODE
  270. ----------------------------------------------------------------------------*/
  271. uint16 pvmp3_decode_huff_cw_tab9(tmp3Bits *pMainData)
  272. {
  273. uint32 tmp;
  274. uint16 cw;
  275. tmp = getUpTo9bits(pMainData, 9); /* huffTable_9 */
  276. if ((tmp >> 5) >= 5)
  277. {
  278. tmp = (tmp >> 5) - 5;
  279. }
  280. else if ((tmp >> 3) >= 6)
  281. {
  282. tmp = (tmp >> 3) - 6 + 11;
  283. }
  284. else if ((tmp >> 1) >= 4)
  285. {
  286. tmp = (tmp >> 1) - 4 + 25;
  287. }
  288. else
  289. {
  290. tmp = tmp + 45;
  291. }
  292. cw = *(huffTable_9 + tmp);
  293. pMainData->usedBits -= (9 - (cw & 0xFF));
  294. return(cw >> 8);
  295. }
  296. /*----------------------------------------------------------------------------
  297. ; FUNCTION CODE
  298. ----------------------------------------------------------------------------*/
  299. uint16 pvmp3_decode_huff_cw_tab10(tmp3Bits *pMainData)
  300. {
  301. uint32 tmp;
  302. uint16 cw;
  303. tmp = getUpTo17bits(pMainData, 11); /* huffTable_10 */
  304. if (tmp >> 10)
  305. {
  306. tmp = (tmp >> 10) - 1;
  307. }
  308. else if ((tmp >> 7) >= 3)
  309. {
  310. tmp = (tmp >> 7) - 3 + 1;
  311. }
  312. else if ((tmp >> 5) >= 8)
  313. {
  314. tmp = (tmp >> 5) - 8 + 6;
  315. }
  316. else if ((tmp >> 3) >= 18)
  317. {
  318. tmp = (tmp >> 3) - 18 + 10;
  319. }
  320. else if ((tmp >> 2) >= 24)
  321. {
  322. tmp = (tmp >> 2) - 24 + 24;
  323. }
  324. else if ((tmp >> 1) >= 12)
  325. {
  326. tmp = (tmp >> 1) - 12 + 36;
  327. }
  328. else
  329. {
  330. tmp = (tmp) + 72;
  331. }
  332. cw = *(huffTable_10 + tmp);
  333. pMainData->usedBits -= (11 - (cw & 0xFF));
  334. return(cw >> 8);
  335. }
  336. /*----------------------------------------------------------------------------
  337. ; FUNCTION CODE
  338. ----------------------------------------------------------------------------*/
  339. uint16 pvmp3_decode_huff_cw_tab11(tmp3Bits *pMainData)
  340. {
  341. uint32 tmp;
  342. uint16 cw;
  343. tmp = getUpTo17bits(pMainData, 11); /* huffTable_11 */
  344. if ((tmp >> 8) >= 3)
  345. {
  346. tmp = (tmp >> 8) - 3;
  347. }
  348. else if ((tmp >> 6) >= 7)
  349. {
  350. tmp = (tmp >> 6) - 7 + 5;
  351. }
  352. else if ((tmp >> 3) >= 32)
  353. {
  354. tmp = (tmp >> 3) - 32 + 10;
  355. }
  356. else if ((tmp >> 2) >= 10)
  357. {
  358. tmp = (tmp >> 2) - 10 + 34;
  359. }
  360. else if ((tmp >> 1) >= 8)
  361. {
  362. tmp = (tmp >> 1) - 8 + 88;
  363. }
  364. else
  365. {
  366. tmp = (tmp & 0xFF) + 100;
  367. }
  368. cw = *(huffTable_11 + tmp);
  369. pMainData->usedBits -= (11 - (cw & 0xFF));
  370. return(cw >> 8);
  371. }
  372. /*----------------------------------------------------------------------------
  373. ; FUNCTION CODE
  374. ----------------------------------------------------------------------------*/
  375. uint16 pvmp3_decode_huff_cw_tab12(tmp3Bits *pMainData)
  376. {
  377. uint32 tmp;
  378. uint16 cw;
  379. tmp = getUpTo17bits(pMainData, 10); /* huffTable_12 */
  380. if ((tmp >> 7) >= 5)
  381. {
  382. tmp = (tmp >> 7) - 5;
  383. }
  384. else if ((tmp >> 5) >= 12)
  385. {
  386. tmp = (tmp >> 5) - 12 + 3;
  387. }
  388. else if ((tmp >> 4) >= 17)
  389. {
  390. tmp = (tmp >> 4) - 17 + 11;
  391. }
  392. else if ((tmp >> 2) >= 32)
  393. {
  394. tmp = (tmp >> 2) - 32 + 18;
  395. }
  396. else if ((tmp >> 1) >= 16)
  397. {
  398. tmp = (tmp >> 1) - 16 + 54;
  399. }
  400. else
  401. {
  402. tmp = (tmp & 0x1F) + 102;
  403. }
  404. cw = *(huffTable_12 + tmp);
  405. pMainData->usedBits -= (10 - (cw & 0xFF));
  406. return(cw >> 8);
  407. }
  408. /*----------------------------------------------------------------------------
  409. ; FUNCTION CODE
  410. ----------------------------------------------------------------------------*/
  411. uint16 pvmp3_decode_huff_cw_tab13(tmp3Bits *pMainData)
  412. {
  413. uint32 tmp;
  414. uint16 cw;
  415. tmp = getNbits(pMainData, 19); /* huffTable_13 */
  416. if (tmp >> 18)
  417. {
  418. tmp = 0;
  419. }
  420. else if ((tmp >> 15) >= 4)
  421. {
  422. tmp = (tmp >> 15) - 4 + 1;
  423. }
  424. else if ((tmp >> 11) >= 32)
  425. {
  426. tmp = (tmp >> 11) - 32 + 5;
  427. }
  428. else if ((tmp >> 9) >= 64)
  429. {
  430. tmp = (tmp >> 9) - 64 + 37;
  431. }
  432. else if ((tmp >> 8) >= 64)
  433. {
  434. tmp = (tmp >> 8) - 64 + 101;
  435. }
  436. else if ((tmp >> 7) >= 64)
  437. {
  438. tmp = (tmp >> 7) - 64 + 165;
  439. }
  440. else if ((tmp >> 6) >= 32)
  441. {
  442. tmp = (tmp >> 6) - 32 + 229;
  443. }
  444. else if ((tmp >> 5) >= 32)
  445. {
  446. tmp = (tmp >> 5) - 32 + 325;
  447. }
  448. else if ((tmp >> 4) >= 32)
  449. {
  450. tmp = (tmp >> 4) - 32 + 357;
  451. }
  452. else if ((tmp >> 3) >= 32)
  453. {
  454. tmp = (tmp >> 3) - 32 + 389;
  455. }
  456. else if ((tmp >> 2) >= 2)
  457. {
  458. tmp = (tmp >> 2) - 2 + 421;
  459. }
  460. else
  461. {
  462. tmp = (tmp & 0x7) + 483;
  463. }
  464. cw = *(huffTable_13 + tmp);
  465. pMainData->usedBits -= (19 - (cw & 0xFF));
  466. return(cw >> 8);
  467. }
  468. /*----------------------------------------------------------------------------
  469. ; FUNCTION CODE
  470. ----------------------------------------------------------------------------*/
  471. uint16 pvmp3_decode_huff_cw_tab15(tmp3Bits *pMainData)
  472. {
  473. uint32 tmp;
  474. uint16 cw;
  475. tmp = getUpTo17bits(pMainData, 13); /* huffTable_15 */
  476. if ((tmp >> 9) >= 10)
  477. {
  478. tmp = (tmp >> 9) - 10;
  479. }
  480. else if ((tmp >> 6) >= 39)
  481. {
  482. tmp = (tmp >> 6) - 39 + 6;
  483. }
  484. else if ((tmp >> 4) >= 62)
  485. {
  486. tmp = (tmp >> 4) - 62 + 47;
  487. }
  488. else if ((tmp >> 3) >= 60)
  489. {
  490. tmp = (tmp >> 3) - 60 + 141;
  491. }
  492. else if ((tmp >> 2) >= 64)
  493. {
  494. tmp = (tmp >> 2) - 64 + 205;
  495. }
  496. else if ((tmp >> 1) >= 32)
  497. {
  498. tmp = (tmp >> 1) - 32 + 261;
  499. }
  500. else
  501. {
  502. tmp = (tmp & 0x3f) + 357;
  503. }
  504. cw = *(huffTable_15 + tmp);
  505. pMainData->usedBits -= (13 - (cw & 0xFF));
  506. return(cw >> 8);
  507. }
  508. /*----------------------------------------------------------------------------
  509. ; FUNCTION CODE
  510. ----------------------------------------------------------------------------*/
  511. uint16 pvmp3_decode_huff_cw_tab16(tmp3Bits *pMainData)
  512. {
  513. uint32 tmp;
  514. uint16 cw;
  515. tmp = getUpTo17bits(pMainData, 17); /* huffTable_16 */
  516. if (tmp >> 16)
  517. {
  518. tmp = 0;
  519. }
  520. else if ((tmp >> 13) >= 4)
  521. {
  522. tmp = (tmp >> 13) - 4 + 1;
  523. }
  524. else if ((tmp >> 9) >= 38)
  525. {
  526. tmp = (tmp >> 9) - 38 + 5;
  527. }
  528. else if ((tmp >> 7) >= 94)
  529. {
  530. tmp = (tmp >> 7) - 94 + 31;
  531. }
  532. else if ((tmp >> 5) >= 214)
  533. {
  534. tmp = (tmp >> 5) - 214 + 89;
  535. }
  536. else if ((tmp >> 3) >= 704)
  537. {
  538. if ((tmp >> 4) >= 384)
  539. {
  540. tmp = (tmp >> 4) - 384 + 315;
  541. }
  542. else
  543. {
  544. tmp = (tmp >> 3) - 704 + 251;
  545. }
  546. }
  547. else if ((tmp >> 8) >= 14)
  548. {
  549. tmp = (tmp >> 8) - 14 + 359;
  550. }
  551. else if ((tmp) >= 3456)
  552. {
  553. if ((tmp >> 2) >= 868)
  554. {
  555. tmp = (tmp >> 2) - 868 + 383;
  556. }
  557. else
  558. {
  559. tmp = (tmp) - 3456 + 367;
  560. }
  561. }
  562. else
  563. {
  564. tmp = ((tmp >> 6) & 0x3f) + 411;
  565. }
  566. cw = *(huffTable_16 + tmp);
  567. pMainData->usedBits -= (17 - (cw & 0xFF));
  568. return(cw >> 8);
  569. }
  570. /*----------------------------------------------------------------------------
  571. ; FUNCTION CODE
  572. ----------------------------------------------------------------------------*/
  573. uint16 pvmp3_decode_huff_cw_tab24(tmp3Bits *pMainData)
  574. {
  575. uint32 tmp;
  576. uint16 cw;
  577. tmp = getUpTo17bits(pMainData, 12); /* huffTable_24 */
  578. if ((tmp >> 6) >= 41)
  579. {
  580. tmp = (tmp >> 6) - 41;
  581. }
  582. else if ((tmp >> 3) >= 218)
  583. {
  584. tmp = (tmp >> 3) - 218 + 23;
  585. }
  586. else if ((tmp >> 2) >= 336)
  587. {
  588. tmp = (tmp >> 2) - 336 + 133;
  589. }
  590. else if ((tmp >> 1) >= 520)
  591. {
  592. tmp = (tmp >> 1) - 520 + 233;
  593. }
  594. else if ((tmp) >= 1024)
  595. {
  596. tmp = (tmp) - 1024 + 385;
  597. }
  598. else if ((tmp >> 1) >= 352)
  599. {
  600. if ((tmp >> 8) == 3)
  601. {
  602. tmp = (tmp >> 8) - 3 + 433;
  603. }
  604. else
  605. {
  606. tmp = (tmp >> 1) - 352 + 401;
  607. }
  608. }
  609. else
  610. {
  611. tmp = ((tmp >> 4) & 0x3f) + 434;
  612. }
  613. cw = *(huffTable_24 + tmp);
  614. pMainData->usedBits -= (12 - (cw & 0xFF));
  615. return(cw >> 8);
  616. }
  617. /*----------------------------------------------------------------------------
  618. ; FUNCTION CODE
  619. ----------------------------------------------------------------------------*/
  620. uint16 pvmp3_decode_huff_cw_tab32(tmp3Bits *pMainData)
  621. {
  622. uint32 tmp = getUpTo9bits(pMainData, 6); /* huffTable_32 */
  623. if ((tmp >> 5))
  624. {
  625. pMainData->usedBits -= 5;
  626. return(0);
  627. }
  628. else
  629. {
  630. uint16 cw = *(huffTable_32 + (tmp & 0x1f));
  631. pMainData->usedBits -= (6 - (cw & 0xFF));
  632. return(cw >> 8);
  633. }
  634. }
  635. uint16 pvmp3_decode_huff_cw_tab33(tmp3Bits *pMainData)
  636. {
  637. uint16 tmp = getUpTo9bits(pMainData, 4); /* huffTable_33 */
  638. return((0x0f - tmp));
  639. }