mpc.c 12 KB

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