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.

347 lines
11KB

  1. /*
  2. * Musepack decoder
  3. * Copyright (c) 2006 Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. /**
  23. * @file mpc.c Musepack decoder
  24. * MPEG Audio Layer 1/2 -like codec with frames of 1152 samples
  25. * divided into 32 subbands.
  26. */
  27. #include "avcodec.h"
  28. #include "bitstream.h"
  29. #include "dsputil.h"
  30. #ifdef CONFIG_MPEGAUDIO_HP
  31. #define USE_HIGHPRECISION
  32. #endif
  33. #include "mpegaudio.h"
  34. #include "mpcdata.h"
  35. #define BANDS 32
  36. #define SAMPLES_PER_BAND 36
  37. #define MPC_FRAME_SIZE (BANDS * SAMPLES_PER_BAND)
  38. static VLC scfi_vlc, dscf_vlc, hdr_vlc, quant_vlc[MPC7_QUANT_VLC_TABLES][2];
  39. static DECLARE_ALIGNED_16(MPA_INT, mpa_window[512]);
  40. typedef struct {
  41. DSPContext dsp;
  42. int IS, MSS, gapless;
  43. int lastframelen, bands;
  44. int oldDSCF[2][BANDS];
  45. int rnd;
  46. /* for synthesis */
  47. DECLARE_ALIGNED_16(MPA_INT, synth_buf[MPA_MAX_CHANNELS][512*2]);
  48. int synth_buf_offset[MPA_MAX_CHANNELS];
  49. DECLARE_ALIGNED_16(int32_t, sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT]);
  50. } MPCContext;
  51. /** Subband structure - hold all variables for each subband */
  52. typedef struct {
  53. int msf; ///< mid-stereo flag
  54. int res[2];
  55. int scfi[2];
  56. int scf_idx[2][3];
  57. int Q[2];
  58. }Band;
  59. static int mpc7_decode_init(AVCodecContext * avctx)
  60. {
  61. int i, j;
  62. MPCContext *c = avctx->priv_data;
  63. GetBitContext gb;
  64. uint8_t buf[16];
  65. float f1=1.20050805774840750476 * 256;
  66. static int vlc_inited = 0;
  67. if(avctx->extradata_size < 16){
  68. av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size);
  69. return -1;
  70. }
  71. memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
  72. c->rnd = 0xDEADBEEF;
  73. dsputil_init(&c->dsp, avctx);
  74. c->dsp.bswap_buf(buf, avctx->extradata, 4);
  75. ff_mpa_synth_init(mpa_window);
  76. init_get_bits(&gb, buf, 128);
  77. c->IS = get_bits1(&gb);
  78. c->MSS = get_bits1(&gb);
  79. c->bands = get_bits(&gb, 6);
  80. if(c->bands >= BANDS){
  81. av_log(avctx, AV_LOG_ERROR, "Too many bands: %i\n", c->bands);
  82. return -1;
  83. }
  84. skip_bits(&gb, 88);
  85. c->gapless = get_bits1(&gb);
  86. c->lastframelen = get_bits(&gb, 11);
  87. av_log(avctx, AV_LOG_DEBUG, "IS: %d, MSS: %d, TG: %d, LFL: %d, bands: %d\n",
  88. c->IS, c->MSS, c->gapless, c->lastframelen, c->bands);
  89. if(vlc_inited) return 0;
  90. av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
  91. if(init_vlc(&scfi_vlc, MPC7_SCFI_BITS, MPC7_SCFI_SIZE,
  92. &mpc7_scfi[1], 2, 1,
  93. &mpc7_scfi[0], 2, 1, INIT_VLC_USE_STATIC)){
  94. av_log(avctx, AV_LOG_ERROR, "Cannot init SCFI VLC\n");
  95. return -1;
  96. }
  97. if(init_vlc(&dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE,
  98. &mpc7_dscf[1], 2, 1,
  99. &mpc7_dscf[0], 2, 1, INIT_VLC_USE_STATIC)){
  100. av_log(avctx, AV_LOG_ERROR, "Cannot init DSCF VLC\n");
  101. return -1;
  102. }
  103. if(init_vlc(&hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE,
  104. &mpc7_hdr[1], 2, 1,
  105. &mpc7_hdr[0], 2, 1, INIT_VLC_USE_STATIC)){
  106. av_log(avctx, AV_LOG_ERROR, "Cannot init HDR VLC\n");
  107. return -1;
  108. }
  109. for(i = 0; i < MPC7_QUANT_VLC_TABLES; i++){
  110. for(j = 0; j < 2; j++){
  111. if(init_vlc(&quant_vlc[i][j], 9, mpc7_quant_vlc_sizes[i],
  112. &mpc7_quant_vlc[i][j][1], 4, 2,
  113. &mpc7_quant_vlc[i][j][0], 4, 2, INIT_VLC_USE_STATIC)){
  114. av_log(avctx, AV_LOG_ERROR, "Cannot init QUANT VLC %i,%i\n",i,j);
  115. return -1;
  116. }
  117. }
  118. }
  119. vlc_inited = 1;
  120. return 0;
  121. }
  122. // XXX replace with something better
  123. static int av_always_inline mpc_rnd(MPCContext *c)
  124. {
  125. c->rnd = c->rnd * 27 + 17;
  126. return c->rnd;
  127. }
  128. /**
  129. * Process decoded Musepack data and produce PCM
  130. * @todo make it available for MPC8 and MPC6
  131. */
  132. static void mpc_synth(MPCContext *c, int16_t *out)
  133. {
  134. int dither_state = 0;
  135. int i, ch;
  136. OUT_INT samples[MPA_MAX_CHANNELS * MPA_FRAME_SIZE], *samples_ptr;
  137. for(ch = 0; ch < 2; ch++){
  138. samples_ptr = samples + ch;
  139. for(i = 0; i < SAMPLES_PER_BAND; i++) {
  140. ff_mpa_synth_filter(c->synth_buf[ch], &(c->synth_buf_offset[ch]),
  141. mpa_window, &dither_state,
  142. samples_ptr, 2,
  143. c->sb_samples[ch][i]);
  144. samples_ptr += 64;
  145. }
  146. }
  147. for(i = 0; i < MPC_FRAME_SIZE*2; i++)
  148. *out++=samples[i];
  149. }
  150. /**
  151. * Fill samples for given subband
  152. */
  153. static void inline idx_to_quant(MPCContext *c, GetBitContext *gb, int idx, int *dst)
  154. {
  155. int i, i1, t;
  156. switch(idx){
  157. case -1:
  158. for(i = 0; i < SAMPLES_PER_BAND; i++){
  159. t = mpc_rnd(c);
  160. *dst++ = ((t>>24)& 0xFF) + ((t>>16) & 0xFF) + ((t>>8) & 0xFF) + (t & 0xFF) - 510;
  161. }
  162. case 1:
  163. i1 = get_bits1(gb);
  164. for(i = 0; i < SAMPLES_PER_BAND/3; i++){
  165. t = get_vlc2(gb, quant_vlc[0][i1].table, 9, 2);
  166. *dst++ = mpc_idx30[t];
  167. *dst++ = mpc_idx31[t];
  168. *dst++ = mpc_idx32[t];
  169. }
  170. break;
  171. case 2:
  172. i1 = get_bits1(gb);
  173. for(i = 0; i < SAMPLES_PER_BAND/2; i++){
  174. t = get_vlc2(gb, quant_vlc[1][i1].table, 9, 2);
  175. *dst++ = mpc_idx50[t];
  176. *dst++ = mpc_idx51[t];
  177. }
  178. break;
  179. case 3: case 4: case 5: case 6: case 7:
  180. i1 = get_bits1(gb);
  181. for(i = 0; i < SAMPLES_PER_BAND; i++)
  182. *dst++ = get_vlc2(gb, quant_vlc[idx-1][i1].table, 9, 2) - mpc7_quant_vlc_off[idx-1];
  183. break;
  184. case 8: case 9: case 10: case 11: case 12:
  185. case 13: case 14: case 15: case 16: case 17:
  186. t = (1 << (idx - 2)) - 1;
  187. for(i = 0; i < SAMPLES_PER_BAND; i++)
  188. *dst++ = get_bits(gb, idx - 1) - t;
  189. break;
  190. default: // case 0 and -2..-17
  191. return;
  192. }
  193. }
  194. static int mpc7_decode_frame(AVCodecContext * avctx,
  195. void *data, int *data_size,
  196. uint8_t * buf, int buf_size)
  197. {
  198. MPCContext *c = avctx->priv_data;
  199. GetBitContext gb;
  200. uint8_t *bits;
  201. int i, j, ch, t;
  202. int mb = -1;
  203. Band bands[BANDS];
  204. int Q[2][MPC_FRAME_SIZE];
  205. int off;
  206. float mul;
  207. int bits_used, bits_avail;
  208. memset(bands, 0, sizeof(bands));
  209. if(buf_size <= 4){
  210. av_log(avctx, AV_LOG_ERROR, "Too small buffer passed (%i bytes)\n", buf_size);
  211. }
  212. bits = av_malloc((buf_size - 1) & ~3);
  213. c->dsp.bswap_buf(bits, buf + 4, (buf_size - 4) >> 2);
  214. init_get_bits(&gb, bits, (buf_size - 4)* 8);
  215. skip_bits(&gb, buf[0]);
  216. /* read subband indexes */
  217. for(i = 0; i <= c->bands; i++){
  218. for(ch = 0; ch < 2; ch++){
  219. if(i) t = get_vlc2(&gb, hdr_vlc.table, MPC7_HDR_BITS, 1) - 5;
  220. if(!i || (t == 4)) bands[i].res[ch] = get_bits(&gb, 4);
  221. else bands[i].res[ch] = bands[i-1].res[ch] + t;
  222. }
  223. if(bands[i].res[0] || bands[i].res[1]){
  224. mb = i;
  225. if(c->MSS) bands[i].msf = get_bits1(&gb);
  226. }
  227. }
  228. /* get scale indexes coding method */
  229. for(i = 0; i <= mb; i++)
  230. for(ch = 0; ch < 2; ch++)
  231. if(bands[i].res[ch]) bands[i].scfi[ch] = get_vlc2(&gb, scfi_vlc.table, MPC7_SCFI_BITS, 1);
  232. /* get scale indexes */
  233. for(i = 0; i <= mb; i++){
  234. for(ch = 0; ch < 2; ch++){
  235. if(bands[i].res[ch]){
  236. bands[i].scf_idx[ch][2] = c->oldDSCF[ch][i];
  237. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  238. bands[i].scf_idx[ch][0] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][2] + t);
  239. switch(bands[i].scfi[ch]){
  240. case 0:
  241. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  242. bands[i].scf_idx[ch][1] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][0] + t);
  243. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  244. bands[i].scf_idx[ch][2] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][1] + t);
  245. break;
  246. case 1:
  247. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  248. bands[i].scf_idx[ch][1] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][0] + t);
  249. bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1];
  250. break;
  251. case 2:
  252. bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
  253. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  254. bands[i].scf_idx[ch][2] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][1] + t);
  255. break;
  256. case 3:
  257. bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
  258. break;
  259. }
  260. c->oldDSCF[ch][i] = bands[i].scf_idx[ch][2];
  261. }
  262. }
  263. }
  264. /* get quantizers */
  265. memset(Q, 0, sizeof(Q));
  266. off = 0;
  267. for(i = 0; i < BANDS; i++, off += SAMPLES_PER_BAND)
  268. for(ch = 0; ch < 2; ch++)
  269. idx_to_quant(c, &gb, bands[i].res[ch], Q[ch] + off);
  270. /* dequantize */
  271. memset(c->sb_samples, 0, sizeof(c->sb_samples));
  272. off = 0;
  273. for(i = 0; i <= mb; i++, off += SAMPLES_PER_BAND){
  274. for(ch = 0; ch < 2; ch++){
  275. if(bands[i].res[ch]){
  276. j = 0;
  277. mul = mpc_CC[bands[i].res[ch]] * mpc7_SCF[bands[i].scf_idx[ch][0]];
  278. for(; j < 12; j++)
  279. c->sb_samples[ch][j][i] = mul * Q[ch][j + off];
  280. mul = mpc_CC[bands[i].res[ch]] * mpc7_SCF[bands[i].scf_idx[ch][1]];
  281. for(; j < 24; j++)
  282. c->sb_samples[ch][j][i] = mul * Q[ch][j + off];
  283. mul = mpc_CC[bands[i].res[ch]] * mpc7_SCF[bands[i].scf_idx[ch][2]];
  284. for(; j < 36; j++)
  285. c->sb_samples[ch][j][i] = mul * Q[ch][j + off];
  286. }
  287. }
  288. if(bands[i].msf){
  289. int t1, t2;
  290. for(j = 0; j < SAMPLES_PER_BAND; j++){
  291. t1 = c->sb_samples[0][j][i];
  292. t2 = c->sb_samples[1][j][i];
  293. c->sb_samples[0][j][i] = t1 + t2;
  294. c->sb_samples[1][j][i] = t1 - t2;
  295. }
  296. }
  297. }
  298. mpc_synth(c, data);
  299. av_free(bits);
  300. bits_used = get_bits_count(&gb);
  301. bits_avail = (buf_size - 4) * 8;
  302. if(!buf[1] && ((bits_avail < bits_used) || (bits_used + 32 <= bits_avail))){
  303. av_log(NULL,0, "Error decoding frame: used %i of %i bits\n", bits_used, bits_avail);
  304. return -1;
  305. }
  306. *data_size = (buf[1] ? c->lastframelen : MPC_FRAME_SIZE) * 4;
  307. return buf_size;
  308. }
  309. AVCodec mpc7_decoder = {
  310. "mpc sv7",
  311. CODEC_TYPE_AUDIO,
  312. CODEC_ID_MUSEPACK7,
  313. sizeof(MPCContext),
  314. mpc7_decode_init,
  315. NULL,
  316. NULL,
  317. mpc7_decode_frame,
  318. };