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.

362 lines
12KB

  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. int frames_to_skip;
  47. /* for synthesis */
  48. DECLARE_ALIGNED_16(MPA_INT, synth_buf[MPA_MAX_CHANNELS][512*2]);
  49. int synth_buf_offset[MPA_MAX_CHANNELS];
  50. DECLARE_ALIGNED_16(int32_t, sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT]);
  51. } MPCContext;
  52. /** Subband structure - hold all variables for each subband */
  53. typedef struct {
  54. int msf; ///< mid-stereo flag
  55. int res[2];
  56. int scfi[2];
  57. int scf_idx[2][3];
  58. int Q[2];
  59. }Band;
  60. static int mpc7_decode_init(AVCodecContext * avctx)
  61. {
  62. int i, j;
  63. MPCContext *c = avctx->priv_data;
  64. GetBitContext gb;
  65. uint8_t buf[16];
  66. float f1=1.20050805774840750476 * 256;
  67. static int vlc_inited = 0;
  68. if(avctx->extradata_size < 16){
  69. av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size);
  70. return -1;
  71. }
  72. memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
  73. c->rnd = 0xDEADBEEF;
  74. dsputil_init(&c->dsp, avctx);
  75. c->dsp.bswap_buf(buf, avctx->extradata, 4);
  76. ff_mpa_synth_init(mpa_window);
  77. init_get_bits(&gb, buf, 128);
  78. c->IS = get_bits1(&gb);
  79. c->MSS = get_bits1(&gb);
  80. c->bands = get_bits(&gb, 6);
  81. if(c->bands >= BANDS){
  82. av_log(avctx, AV_LOG_ERROR, "Too many bands: %i\n", c->bands);
  83. return -1;
  84. }
  85. skip_bits(&gb, 88);
  86. c->gapless = get_bits1(&gb);
  87. c->lastframelen = get_bits(&gb, 11);
  88. av_log(avctx, AV_LOG_DEBUG, "IS: %d, MSS: %d, TG: %d, LFL: %d, bands: %d\n",
  89. c->IS, c->MSS, c->gapless, c->lastframelen, c->bands);
  90. c->frames_to_skip = 0;
  91. if(vlc_inited) return 0;
  92. av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
  93. if(init_vlc(&scfi_vlc, MPC7_SCFI_BITS, MPC7_SCFI_SIZE,
  94. &mpc7_scfi[1], 2, 1,
  95. &mpc7_scfi[0], 2, 1, INIT_VLC_USE_STATIC)){
  96. av_log(avctx, AV_LOG_ERROR, "Cannot init SCFI VLC\n");
  97. return -1;
  98. }
  99. if(init_vlc(&dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE,
  100. &mpc7_dscf[1], 2, 1,
  101. &mpc7_dscf[0], 2, 1, INIT_VLC_USE_STATIC)){
  102. av_log(avctx, AV_LOG_ERROR, "Cannot init DSCF VLC\n");
  103. return -1;
  104. }
  105. if(init_vlc(&hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE,
  106. &mpc7_hdr[1], 2, 1,
  107. &mpc7_hdr[0], 2, 1, INIT_VLC_USE_STATIC)){
  108. av_log(avctx, AV_LOG_ERROR, "Cannot init HDR VLC\n");
  109. return -1;
  110. }
  111. for(i = 0; i < MPC7_QUANT_VLC_TABLES; i++){
  112. for(j = 0; j < 2; j++){
  113. if(init_vlc(&quant_vlc[i][j], 9, mpc7_quant_vlc_sizes[i],
  114. &mpc7_quant_vlc[i][j][1], 4, 2,
  115. &mpc7_quant_vlc[i][j][0], 4, 2, INIT_VLC_USE_STATIC)){
  116. av_log(avctx, AV_LOG_ERROR, "Cannot init QUANT VLC %i,%i\n",i,j);
  117. return -1;
  118. }
  119. }
  120. }
  121. vlc_inited = 1;
  122. return 0;
  123. }
  124. // XXX replace with something better
  125. static int av_always_inline mpc_rnd(MPCContext *c)
  126. {
  127. c->rnd = c->rnd * 27 + 17;
  128. return c->rnd;
  129. }
  130. /**
  131. * Process decoded Musepack data and produce PCM
  132. * @todo make it available for MPC8 and MPC6
  133. */
  134. static void mpc_synth(MPCContext *c, int16_t *out)
  135. {
  136. int dither_state = 0;
  137. int i, ch;
  138. OUT_INT samples[MPA_MAX_CHANNELS * MPA_FRAME_SIZE], *samples_ptr;
  139. for(ch = 0; ch < 2; ch++){
  140. samples_ptr = samples + ch;
  141. for(i = 0; i < SAMPLES_PER_BAND; i++) {
  142. ff_mpa_synth_filter(c->synth_buf[ch], &(c->synth_buf_offset[ch]),
  143. mpa_window, &dither_state,
  144. samples_ptr, 2,
  145. c->sb_samples[ch][i]);
  146. samples_ptr += 64;
  147. }
  148. }
  149. for(i = 0; i < MPC_FRAME_SIZE*2; i++)
  150. *out++=samples[i];
  151. }
  152. /**
  153. * Fill samples for given subband
  154. */
  155. static void inline idx_to_quant(MPCContext *c, GetBitContext *gb, int idx, int *dst)
  156. {
  157. int i, i1, t;
  158. switch(idx){
  159. case -1:
  160. for(i = 0; i < SAMPLES_PER_BAND; i++){
  161. t = mpc_rnd(c);
  162. *dst++ = ((t>>24)& 0xFF) + ((t>>16) & 0xFF) + ((t>>8) & 0xFF) + (t & 0xFF) - 510;
  163. }
  164. case 1:
  165. i1 = get_bits1(gb);
  166. for(i = 0; i < SAMPLES_PER_BAND/3; i++){
  167. t = get_vlc2(gb, quant_vlc[0][i1].table, 9, 2);
  168. *dst++ = mpc_idx30[t];
  169. *dst++ = mpc_idx31[t];
  170. *dst++ = mpc_idx32[t];
  171. }
  172. break;
  173. case 2:
  174. i1 = get_bits1(gb);
  175. for(i = 0; i < SAMPLES_PER_BAND/2; i++){
  176. t = get_vlc2(gb, quant_vlc[1][i1].table, 9, 2);
  177. *dst++ = mpc_idx50[t];
  178. *dst++ = mpc_idx51[t];
  179. }
  180. break;
  181. case 3: case 4: case 5: case 6: case 7:
  182. i1 = get_bits1(gb);
  183. for(i = 0; i < SAMPLES_PER_BAND; i++)
  184. *dst++ = get_vlc2(gb, quant_vlc[idx-1][i1].table, 9, 2) - mpc7_quant_vlc_off[idx-1];
  185. break;
  186. case 8: case 9: case 10: case 11: case 12:
  187. case 13: case 14: case 15: case 16: case 17:
  188. t = (1 << (idx - 2)) - 1;
  189. for(i = 0; i < SAMPLES_PER_BAND; i++)
  190. *dst++ = get_bits(gb, idx - 1) - t;
  191. break;
  192. default: // case 0 and -2..-17
  193. return;
  194. }
  195. }
  196. static int mpc7_decode_frame(AVCodecContext * avctx,
  197. void *data, int *data_size,
  198. uint8_t * buf, int buf_size)
  199. {
  200. MPCContext *c = avctx->priv_data;
  201. GetBitContext gb;
  202. uint8_t *bits;
  203. int i, j, ch, t;
  204. int mb = -1;
  205. Band bands[BANDS];
  206. int Q[2][MPC_FRAME_SIZE];
  207. int off;
  208. float mul;
  209. int bits_used, bits_avail;
  210. memset(bands, 0, sizeof(bands));
  211. if(buf_size <= 4){
  212. av_log(avctx, AV_LOG_ERROR, "Too small buffer passed (%i bytes)\n", buf_size);
  213. }
  214. bits = av_malloc(((buf_size - 1) & ~3) + FF_INPUT_BUFFER_PADDING_SIZE);
  215. c->dsp.bswap_buf(bits, buf + 4, (buf_size - 4) >> 2);
  216. init_get_bits(&gb, bits, (buf_size - 4)* 8);
  217. skip_bits(&gb, buf[0]);
  218. /* read subband indexes */
  219. for(i = 0; i <= c->bands; i++){
  220. for(ch = 0; ch < 2; ch++){
  221. if(i) t = get_vlc2(&gb, hdr_vlc.table, MPC7_HDR_BITS, 1) - 5;
  222. if(!i || (t == 4)) bands[i].res[ch] = get_bits(&gb, 4);
  223. else bands[i].res[ch] = bands[i-1].res[ch] + t;
  224. }
  225. if(bands[i].res[0] || bands[i].res[1]){
  226. mb = i;
  227. if(c->MSS) bands[i].msf = get_bits1(&gb);
  228. }
  229. }
  230. /* get scale indexes coding method */
  231. for(i = 0; i <= mb; i++)
  232. for(ch = 0; ch < 2; ch++)
  233. if(bands[i].res[ch]) bands[i].scfi[ch] = get_vlc2(&gb, scfi_vlc.table, MPC7_SCFI_BITS, 1);
  234. /* get scale indexes */
  235. for(i = 0; i <= mb; i++){
  236. for(ch = 0; ch < 2; ch++){
  237. if(bands[i].res[ch]){
  238. bands[i].scf_idx[ch][2] = c->oldDSCF[ch][i];
  239. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  240. bands[i].scf_idx[ch][0] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][2] + t);
  241. switch(bands[i].scfi[ch]){
  242. case 0:
  243. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  244. bands[i].scf_idx[ch][1] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][0] + t);
  245. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  246. bands[i].scf_idx[ch][2] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][1] + t);
  247. break;
  248. case 1:
  249. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  250. bands[i].scf_idx[ch][1] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][0] + t);
  251. bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1];
  252. break;
  253. case 2:
  254. bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
  255. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  256. bands[i].scf_idx[ch][2] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][1] + t);
  257. break;
  258. case 3:
  259. bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
  260. break;
  261. }
  262. c->oldDSCF[ch][i] = bands[i].scf_idx[ch][2];
  263. }
  264. }
  265. }
  266. /* get quantizers */
  267. memset(Q, 0, sizeof(Q));
  268. off = 0;
  269. for(i = 0; i < BANDS; i++, off += SAMPLES_PER_BAND)
  270. for(ch = 0; ch < 2; ch++)
  271. idx_to_quant(c, &gb, bands[i].res[ch], Q[ch] + off);
  272. /* dequantize */
  273. memset(c->sb_samples, 0, sizeof(c->sb_samples));
  274. off = 0;
  275. for(i = 0; i <= mb; i++, off += SAMPLES_PER_BAND){
  276. for(ch = 0; ch < 2; ch++){
  277. if(bands[i].res[ch]){
  278. j = 0;
  279. mul = mpc_CC[bands[i].res[ch]] * mpc7_SCF[bands[i].scf_idx[ch][0]];
  280. for(; j < 12; j++)
  281. c->sb_samples[ch][j][i] = mul * Q[ch][j + off];
  282. mul = mpc_CC[bands[i].res[ch]] * mpc7_SCF[bands[i].scf_idx[ch][1]];
  283. for(; j < 24; j++)
  284. c->sb_samples[ch][j][i] = mul * Q[ch][j + off];
  285. mul = mpc_CC[bands[i].res[ch]] * mpc7_SCF[bands[i].scf_idx[ch][2]];
  286. for(; j < 36; j++)
  287. c->sb_samples[ch][j][i] = mul * Q[ch][j + off];
  288. }
  289. }
  290. if(bands[i].msf){
  291. int t1, t2;
  292. for(j = 0; j < SAMPLES_PER_BAND; j++){
  293. t1 = c->sb_samples[0][j][i];
  294. t2 = c->sb_samples[1][j][i];
  295. c->sb_samples[0][j][i] = t1 + t2;
  296. c->sb_samples[1][j][i] = t1 - t2;
  297. }
  298. }
  299. }
  300. mpc_synth(c, data);
  301. av_free(bits);
  302. bits_used = get_bits_count(&gb);
  303. bits_avail = (buf_size - 4) * 8;
  304. if(!buf[1] && ((bits_avail < bits_used) || (bits_used + 32 <= bits_avail))){
  305. av_log(NULL,0, "Error decoding frame: used %i of %i bits\n", bits_used, bits_avail);
  306. return -1;
  307. }
  308. if(c->frames_to_skip){
  309. c->frames_to_skip--;
  310. *data_size = 0;
  311. return buf_size;
  312. }
  313. *data_size = (buf[1] ? c->lastframelen : MPC_FRAME_SIZE) * 4;
  314. return buf_size;
  315. }
  316. static void mpc7_decode_flush(AVCodecContext *avctx)
  317. {
  318. MPCContext *c = avctx->priv_data;
  319. memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
  320. c->frames_to_skip = 32;
  321. }
  322. AVCodec mpc7_decoder = {
  323. "mpc sv7",
  324. CODEC_TYPE_AUDIO,
  325. CODEC_ID_MUSEPACK7,
  326. sizeof(MPCContext),
  327. mpc7_decode_init,
  328. NULL,
  329. NULL,
  330. mpc7_decode_frame,
  331. .flush = mpc7_decode_flush,
  332. };