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.

354 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. * @file mpc.c Musepack decoder
  23. * MPEG Audio Layer 1/2 -like codec with frames of 1152 samples
  24. * divided into 32 subbands.
  25. */
  26. #include "avcodec.h"
  27. #include "bitstream.h"
  28. #include "dsputil.h"
  29. #include "random.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. AVRandomState 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. 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. av_init_random(0xDEADBEEF, &c->rnd);
  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. c->frames_to_skip = 0;
  90. if(vlc_inited) return 0;
  91. av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
  92. if(init_vlc(&scfi_vlc, MPC7_SCFI_BITS, MPC7_SCFI_SIZE,
  93. &mpc7_scfi[1], 2, 1,
  94. &mpc7_scfi[0], 2, 1, INIT_VLC_USE_STATIC)){
  95. av_log(avctx, AV_LOG_ERROR, "Cannot init SCFI VLC\n");
  96. return -1;
  97. }
  98. if(init_vlc(&dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE,
  99. &mpc7_dscf[1], 2, 1,
  100. &mpc7_dscf[0], 2, 1, INIT_VLC_USE_STATIC)){
  101. av_log(avctx, AV_LOG_ERROR, "Cannot init DSCF VLC\n");
  102. return -1;
  103. }
  104. if(init_vlc(&hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE,
  105. &mpc7_hdr[1], 2, 1,
  106. &mpc7_hdr[0], 2, 1, INIT_VLC_USE_STATIC)){
  107. av_log(avctx, AV_LOG_ERROR, "Cannot init HDR VLC\n");
  108. return -1;
  109. }
  110. for(i = 0; i < MPC7_QUANT_VLC_TABLES; i++){
  111. for(j = 0; j < 2; j++){
  112. if(init_vlc(&quant_vlc[i][j], 9, mpc7_quant_vlc_sizes[i],
  113. &mpc7_quant_vlc[i][j][1], 4, 2,
  114. &mpc7_quant_vlc[i][j][0], 4, 2, INIT_VLC_USE_STATIC)){
  115. av_log(avctx, AV_LOG_ERROR, "Cannot init QUANT VLC %i,%i\n",i,j);
  116. return -1;
  117. }
  118. }
  119. }
  120. vlc_inited = 1;
  121. return 0;
  122. }
  123. /**
  124. * Process decoded Musepack data and produce PCM
  125. * @todo make it available for MPC8 and MPC6
  126. */
  127. static void mpc_synth(MPCContext *c, int16_t *out)
  128. {
  129. int dither_state = 0;
  130. int i, ch;
  131. OUT_INT samples[MPA_MAX_CHANNELS * MPA_FRAME_SIZE], *samples_ptr;
  132. for(ch = 0; ch < 2; ch++){
  133. samples_ptr = samples + ch;
  134. for(i = 0; i < SAMPLES_PER_BAND; i++) {
  135. ff_mpa_synth_filter(c->synth_buf[ch], &(c->synth_buf_offset[ch]),
  136. mpa_window, &dither_state,
  137. samples_ptr, 2,
  138. c->sb_samples[ch][i]);
  139. samples_ptr += 64;
  140. }
  141. }
  142. for(i = 0; i < MPC_FRAME_SIZE*2; i++)
  143. *out++=samples[i];
  144. }
  145. /**
  146. * Fill samples for given subband
  147. */
  148. static inline void idx_to_quant(MPCContext *c, GetBitContext *gb, int idx, int *dst)
  149. {
  150. int i, i1, t;
  151. switch(idx){
  152. case -1:
  153. for(i = 0; i < SAMPLES_PER_BAND; i++){
  154. *dst++ = (av_random(&c->rnd) & 0x3FC) - 510;
  155. }
  156. break;
  157. case 1:
  158. i1 = get_bits1(gb);
  159. for(i = 0; i < SAMPLES_PER_BAND/3; i++){
  160. t = get_vlc2(gb, quant_vlc[0][i1].table, 9, 2);
  161. *dst++ = mpc_idx30[t];
  162. *dst++ = mpc_idx31[t];
  163. *dst++ = mpc_idx32[t];
  164. }
  165. break;
  166. case 2:
  167. i1 = get_bits1(gb);
  168. for(i = 0; i < SAMPLES_PER_BAND/2; i++){
  169. t = get_vlc2(gb, quant_vlc[1][i1].table, 9, 2);
  170. *dst++ = mpc_idx50[t];
  171. *dst++ = mpc_idx51[t];
  172. }
  173. break;
  174. case 3: case 4: case 5: case 6: case 7:
  175. i1 = get_bits1(gb);
  176. for(i = 0; i < SAMPLES_PER_BAND; i++)
  177. *dst++ = get_vlc2(gb, quant_vlc[idx-1][i1].table, 9, 2) - mpc7_quant_vlc_off[idx-1];
  178. break;
  179. case 8: case 9: case 10: case 11: case 12:
  180. case 13: case 14: case 15: case 16: case 17:
  181. t = (1 << (idx - 2)) - 1;
  182. for(i = 0; i < SAMPLES_PER_BAND; i++)
  183. *dst++ = get_bits(gb, idx - 1) - t;
  184. break;
  185. default: // case 0 and -2..-17
  186. return;
  187. }
  188. }
  189. static int mpc7_decode_frame(AVCodecContext * avctx,
  190. void *data, int *data_size,
  191. uint8_t * buf, int buf_size)
  192. {
  193. MPCContext *c = avctx->priv_data;
  194. GetBitContext gb;
  195. uint8_t *bits;
  196. int i, j, ch, t;
  197. int mb = -1;
  198. Band bands[BANDS];
  199. int Q[2][MPC_FRAME_SIZE];
  200. int off;
  201. float mul;
  202. int bits_used, bits_avail;
  203. memset(bands, 0, sizeof(bands));
  204. if(buf_size <= 4){
  205. av_log(avctx, AV_LOG_ERROR, "Too small buffer passed (%i bytes)\n", buf_size);
  206. }
  207. bits = av_malloc(((buf_size - 1) & ~3) + FF_INPUT_BUFFER_PADDING_SIZE);
  208. c->dsp.bswap_buf(bits, buf + 4, (buf_size - 4) >> 2);
  209. init_get_bits(&gb, bits, (buf_size - 4)* 8);
  210. skip_bits(&gb, buf[0]);
  211. /* read subband indexes */
  212. for(i = 0; i <= c->bands; i++){
  213. for(ch = 0; ch < 2; ch++){
  214. if(i) t = get_vlc2(&gb, hdr_vlc.table, MPC7_HDR_BITS, 1) - 5;
  215. if(!i || (t == 4)) bands[i].res[ch] = get_bits(&gb, 4);
  216. else bands[i].res[ch] = bands[i-1].res[ch] + t;
  217. }
  218. if(bands[i].res[0] || bands[i].res[1]){
  219. mb = i;
  220. if(c->MSS) bands[i].msf = get_bits1(&gb);
  221. }
  222. }
  223. /* get scale indexes coding method */
  224. for(i = 0; i <= mb; i++)
  225. for(ch = 0; ch < 2; ch++)
  226. if(bands[i].res[ch]) bands[i].scfi[ch] = get_vlc2(&gb, scfi_vlc.table, MPC7_SCFI_BITS, 1);
  227. /* get scale indexes */
  228. for(i = 0; i <= mb; i++){
  229. for(ch = 0; ch < 2; ch++){
  230. if(bands[i].res[ch]){
  231. bands[i].scf_idx[ch][2] = c->oldDSCF[ch][i];
  232. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  233. bands[i].scf_idx[ch][0] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][2] + t);
  234. switch(bands[i].scfi[ch]){
  235. case 0:
  236. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  237. bands[i].scf_idx[ch][1] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][0] + t);
  238. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  239. bands[i].scf_idx[ch][2] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][1] + t);
  240. break;
  241. case 1:
  242. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  243. bands[i].scf_idx[ch][1] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][0] + t);
  244. bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1];
  245. break;
  246. case 2:
  247. bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
  248. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  249. bands[i].scf_idx[ch][2] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][1] + t);
  250. break;
  251. case 3:
  252. bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
  253. break;
  254. }
  255. c->oldDSCF[ch][i] = bands[i].scf_idx[ch][2];
  256. }
  257. }
  258. }
  259. /* get quantizers */
  260. memset(Q, 0, sizeof(Q));
  261. off = 0;
  262. for(i = 0; i < BANDS; i++, off += SAMPLES_PER_BAND)
  263. for(ch = 0; ch < 2; ch++)
  264. idx_to_quant(c, &gb, bands[i].res[ch], Q[ch] + off);
  265. /* dequantize */
  266. memset(c->sb_samples, 0, sizeof(c->sb_samples));
  267. off = 0;
  268. for(i = 0; i <= mb; i++, off += SAMPLES_PER_BAND){
  269. for(ch = 0; ch < 2; ch++){
  270. if(bands[i].res[ch]){
  271. j = 0;
  272. mul = mpc_CC[bands[i].res[ch]] * mpc7_SCF[bands[i].scf_idx[ch][0]];
  273. for(; j < 12; j++)
  274. c->sb_samples[ch][j][i] = mul * Q[ch][j + off];
  275. mul = mpc_CC[bands[i].res[ch]] * mpc7_SCF[bands[i].scf_idx[ch][1]];
  276. for(; j < 24; j++)
  277. c->sb_samples[ch][j][i] = mul * Q[ch][j + off];
  278. mul = mpc_CC[bands[i].res[ch]] * mpc7_SCF[bands[i].scf_idx[ch][2]];
  279. for(; j < 36; j++)
  280. c->sb_samples[ch][j][i] = mul * Q[ch][j + off];
  281. }
  282. }
  283. if(bands[i].msf){
  284. int t1, t2;
  285. for(j = 0; j < SAMPLES_PER_BAND; j++){
  286. t1 = c->sb_samples[0][j][i];
  287. t2 = c->sb_samples[1][j][i];
  288. c->sb_samples[0][j][i] = t1 + t2;
  289. c->sb_samples[1][j][i] = t1 - t2;
  290. }
  291. }
  292. }
  293. mpc_synth(c, data);
  294. av_free(bits);
  295. bits_used = get_bits_count(&gb);
  296. bits_avail = (buf_size - 4) * 8;
  297. if(!buf[1] && ((bits_avail < bits_used) || (bits_used + 32 <= bits_avail))){
  298. av_log(NULL,0, "Error decoding frame: used %i of %i bits\n", bits_used, bits_avail);
  299. return -1;
  300. }
  301. if(c->frames_to_skip){
  302. c->frames_to_skip--;
  303. *data_size = 0;
  304. return buf_size;
  305. }
  306. *data_size = (buf[1] ? c->lastframelen : MPC_FRAME_SIZE) * 4;
  307. return buf_size;
  308. }
  309. static void mpc7_decode_flush(AVCodecContext *avctx)
  310. {
  311. MPCContext *c = avctx->priv_data;
  312. memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
  313. c->frames_to_skip = 32;
  314. }
  315. AVCodec mpc7_decoder = {
  316. "mpc sv7",
  317. CODEC_TYPE_AUDIO,
  318. CODEC_ID_MUSEPACK7,
  319. sizeof(MPCContext),
  320. mpc7_decode_init,
  321. NULL,
  322. NULL,
  323. mpc7_decode_frame,
  324. .flush = mpc7_decode_flush,
  325. };