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.

355 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. #include "random.h"
  31. #ifdef CONFIG_MPEGAUDIO_HP
  32. #define USE_HIGHPRECISION
  33. #endif
  34. #include "mpegaudio.h"
  35. #include "mpcdata.h"
  36. #define BANDS 32
  37. #define SAMPLES_PER_BAND 36
  38. #define MPC_FRAME_SIZE (BANDS * SAMPLES_PER_BAND)
  39. static VLC scfi_vlc, dscf_vlc, hdr_vlc, quant_vlc[MPC7_QUANT_VLC_TABLES][2];
  40. static DECLARE_ALIGNED_16(MPA_INT, mpa_window[512]);
  41. typedef struct {
  42. DSPContext dsp;
  43. int IS, MSS, gapless;
  44. int lastframelen, bands;
  45. int oldDSCF[2][BANDS];
  46. AVRandomState rnd;
  47. int frames_to_skip;
  48. /* for synthesis */
  49. DECLARE_ALIGNED_16(MPA_INT, synth_buf[MPA_MAX_CHANNELS][512*2]);
  50. int synth_buf_offset[MPA_MAX_CHANNELS];
  51. DECLARE_ALIGNED_16(int32_t, sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT]);
  52. } MPCContext;
  53. /** Subband structure - hold all variables for each subband */
  54. typedef struct {
  55. int msf; ///< mid-stereo flag
  56. int res[2];
  57. int scfi[2];
  58. int scf_idx[2][3];
  59. int Q[2];
  60. }Band;
  61. static int mpc7_decode_init(AVCodecContext * avctx)
  62. {
  63. int i, j;
  64. MPCContext *c = avctx->priv_data;
  65. GetBitContext gb;
  66. uint8_t buf[16];
  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. av_init_random(0xDEADBEEF, &c->rnd);
  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. /**
  125. * Process decoded Musepack data and produce PCM
  126. * @todo make it available for MPC8 and MPC6
  127. */
  128. static void mpc_synth(MPCContext *c, int16_t *out)
  129. {
  130. int dither_state = 0;
  131. int i, ch;
  132. OUT_INT samples[MPA_MAX_CHANNELS * MPA_FRAME_SIZE], *samples_ptr;
  133. for(ch = 0; ch < 2; ch++){
  134. samples_ptr = samples + ch;
  135. for(i = 0; i < SAMPLES_PER_BAND; i++) {
  136. ff_mpa_synth_filter(c->synth_buf[ch], &(c->synth_buf_offset[ch]),
  137. mpa_window, &dither_state,
  138. samples_ptr, 2,
  139. c->sb_samples[ch][i]);
  140. samples_ptr += 64;
  141. }
  142. }
  143. for(i = 0; i < MPC_FRAME_SIZE*2; i++)
  144. *out++=samples[i];
  145. }
  146. /**
  147. * Fill samples for given subband
  148. */
  149. static inline void idx_to_quant(MPCContext *c, GetBitContext *gb, int idx, int *dst)
  150. {
  151. int i, i1, t;
  152. switch(idx){
  153. case -1:
  154. for(i = 0; i < SAMPLES_PER_BAND; i++){
  155. *dst++ = (av_random(&c->rnd) & 0x3FC) - 510;
  156. }
  157. break;
  158. case 1:
  159. i1 = get_bits1(gb);
  160. for(i = 0; i < SAMPLES_PER_BAND/3; i++){
  161. t = get_vlc2(gb, quant_vlc[0][i1].table, 9, 2);
  162. *dst++ = mpc_idx30[t];
  163. *dst++ = mpc_idx31[t];
  164. *dst++ = mpc_idx32[t];
  165. }
  166. break;
  167. case 2:
  168. i1 = get_bits1(gb);
  169. for(i = 0; i < SAMPLES_PER_BAND/2; i++){
  170. t = get_vlc2(gb, quant_vlc[1][i1].table, 9, 2);
  171. *dst++ = mpc_idx50[t];
  172. *dst++ = mpc_idx51[t];
  173. }
  174. break;
  175. case 3: case 4: case 5: case 6: case 7:
  176. i1 = get_bits1(gb);
  177. for(i = 0; i < SAMPLES_PER_BAND; i++)
  178. *dst++ = get_vlc2(gb, quant_vlc[idx-1][i1].table, 9, 2) - mpc7_quant_vlc_off[idx-1];
  179. break;
  180. case 8: case 9: case 10: case 11: case 12:
  181. case 13: case 14: case 15: case 16: case 17:
  182. t = (1 << (idx - 2)) - 1;
  183. for(i = 0; i < SAMPLES_PER_BAND; i++)
  184. *dst++ = get_bits(gb, idx - 1) - t;
  185. break;
  186. default: // case 0 and -2..-17
  187. return;
  188. }
  189. }
  190. static int mpc7_decode_frame(AVCodecContext * avctx,
  191. void *data, int *data_size,
  192. uint8_t * buf, int buf_size)
  193. {
  194. MPCContext *c = avctx->priv_data;
  195. GetBitContext gb;
  196. uint8_t *bits;
  197. int i, j, ch, t;
  198. int mb = -1;
  199. Band bands[BANDS];
  200. int Q[2][MPC_FRAME_SIZE];
  201. int off;
  202. float mul;
  203. int bits_used, bits_avail;
  204. memset(bands, 0, sizeof(bands));
  205. if(buf_size <= 4){
  206. av_log(avctx, AV_LOG_ERROR, "Too small buffer passed (%i bytes)\n", buf_size);
  207. }
  208. bits = av_malloc(((buf_size - 1) & ~3) + FF_INPUT_BUFFER_PADDING_SIZE);
  209. c->dsp.bswap_buf(bits, buf + 4, (buf_size - 4) >> 2);
  210. init_get_bits(&gb, bits, (buf_size - 4)* 8);
  211. skip_bits(&gb, buf[0]);
  212. /* read subband indexes */
  213. for(i = 0; i <= c->bands; i++){
  214. for(ch = 0; ch < 2; ch++){
  215. if(i) t = get_vlc2(&gb, hdr_vlc.table, MPC7_HDR_BITS, 1) - 5;
  216. if(!i || (t == 4)) bands[i].res[ch] = get_bits(&gb, 4);
  217. else bands[i].res[ch] = bands[i-1].res[ch] + t;
  218. }
  219. if(bands[i].res[0] || bands[i].res[1]){
  220. mb = i;
  221. if(c->MSS) bands[i].msf = get_bits1(&gb);
  222. }
  223. }
  224. /* get scale indexes coding method */
  225. for(i = 0; i <= mb; i++)
  226. for(ch = 0; ch < 2; ch++)
  227. if(bands[i].res[ch]) bands[i].scfi[ch] = get_vlc2(&gb, scfi_vlc.table, MPC7_SCFI_BITS, 1);
  228. /* get scale indexes */
  229. for(i = 0; i <= mb; i++){
  230. for(ch = 0; ch < 2; ch++){
  231. if(bands[i].res[ch]){
  232. bands[i].scf_idx[ch][2] = c->oldDSCF[ch][i];
  233. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  234. bands[i].scf_idx[ch][0] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][2] + t);
  235. switch(bands[i].scfi[ch]){
  236. case 0:
  237. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  238. bands[i].scf_idx[ch][1] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][0] + t);
  239. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  240. bands[i].scf_idx[ch][2] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][1] + t);
  241. break;
  242. case 1:
  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. bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1];
  246. break;
  247. case 2:
  248. bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
  249. t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  250. bands[i].scf_idx[ch][2] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][1] + t);
  251. break;
  252. case 3:
  253. bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
  254. break;
  255. }
  256. c->oldDSCF[ch][i] = bands[i].scf_idx[ch][2];
  257. }
  258. }
  259. }
  260. /* get quantizers */
  261. memset(Q, 0, sizeof(Q));
  262. off = 0;
  263. for(i = 0; i < BANDS; i++, off += SAMPLES_PER_BAND)
  264. for(ch = 0; ch < 2; ch++)
  265. idx_to_quant(c, &gb, bands[i].res[ch], Q[ch] + off);
  266. /* dequantize */
  267. memset(c->sb_samples, 0, sizeof(c->sb_samples));
  268. off = 0;
  269. for(i = 0; i <= mb; i++, off += SAMPLES_PER_BAND){
  270. for(ch = 0; ch < 2; ch++){
  271. if(bands[i].res[ch]){
  272. j = 0;
  273. mul = mpc_CC[bands[i].res[ch]] * mpc7_SCF[bands[i].scf_idx[ch][0]];
  274. for(; j < 12; j++)
  275. c->sb_samples[ch][j][i] = mul * Q[ch][j + off];
  276. mul = mpc_CC[bands[i].res[ch]] * mpc7_SCF[bands[i].scf_idx[ch][1]];
  277. for(; j < 24; j++)
  278. c->sb_samples[ch][j][i] = mul * Q[ch][j + off];
  279. mul = mpc_CC[bands[i].res[ch]] * mpc7_SCF[bands[i].scf_idx[ch][2]];
  280. for(; j < 36; j++)
  281. c->sb_samples[ch][j][i] = mul * Q[ch][j + off];
  282. }
  283. }
  284. if(bands[i].msf){
  285. int t1, t2;
  286. for(j = 0; j < SAMPLES_PER_BAND; j++){
  287. t1 = c->sb_samples[0][j][i];
  288. t2 = c->sb_samples[1][j][i];
  289. c->sb_samples[0][j][i] = t1 + t2;
  290. c->sb_samples[1][j][i] = t1 - t2;
  291. }
  292. }
  293. }
  294. mpc_synth(c, data);
  295. av_free(bits);
  296. bits_used = get_bits_count(&gb);
  297. bits_avail = (buf_size - 4) * 8;
  298. if(!buf[1] && ((bits_avail < bits_used) || (bits_used + 32 <= bits_avail))){
  299. av_log(NULL,0, "Error decoding frame: used %i of %i bits\n", bits_used, bits_avail);
  300. return -1;
  301. }
  302. if(c->frames_to_skip){
  303. c->frames_to_skip--;
  304. *data_size = 0;
  305. return buf_size;
  306. }
  307. *data_size = (buf[1] ? c->lastframelen : MPC_FRAME_SIZE) * 4;
  308. return buf_size;
  309. }
  310. static void mpc7_decode_flush(AVCodecContext *avctx)
  311. {
  312. MPCContext *c = avctx->priv_data;
  313. memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
  314. c->frames_to_skip = 32;
  315. }
  316. AVCodec mpc7_decoder = {
  317. "mpc sv7",
  318. CODEC_TYPE_AUDIO,
  319. CODEC_ID_MUSEPACK7,
  320. sizeof(MPCContext),
  321. mpc7_decode_init,
  322. NULL,
  323. NULL,
  324. mpc7_decode_frame,
  325. .flush = mpc7_decode_flush,
  326. };