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.

433 lines
15KB

  1. /*
  2. * Musepack SV8 decoder
  3. * Copyright (c) 2007 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
  23. * MPEG Audio Layer 1/2 -like codec with frames of 1152 samples
  24. * divided into 32 subbands.
  25. */
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/lfg.h"
  28. #include "avcodec.h"
  29. #include "get_bits.h"
  30. #include "internal.h"
  31. #include "mpegaudiodsp.h"
  32. #include "mpc.h"
  33. #include "mpc8data.h"
  34. #include "mpc8huff.h"
  35. static VLC band_vlc, scfi_vlc[2], dscf_vlc[2], res_vlc[2];
  36. static VLC q1_vlc, q2_vlc[2], q3_vlc[2], quant_vlc[4][2], q9up_vlc;
  37. static inline int mpc8_dec_base(GetBitContext *gb, int k, int n)
  38. {
  39. int len = mpc8_cnk_len[k-1][n-1] - 1;
  40. int code = len ? get_bits_long(gb, len) : 0;
  41. if (code >= mpc8_cnk_lost[k-1][n-1])
  42. code = ((code << 1) | get_bits1(gb)) - mpc8_cnk_lost[k-1][n-1];
  43. return code;
  44. }
  45. static inline int mpc8_dec_enum(GetBitContext *gb, int k, int n)
  46. {
  47. int bits = 0;
  48. const uint32_t * C = mpc8_cnk[k-1];
  49. int code = mpc8_dec_base(gb, k, n);
  50. do {
  51. n--;
  52. if (code >= C[n]) {
  53. bits |= 1U << n;
  54. code -= C[n];
  55. C -= 32;
  56. k--;
  57. }
  58. } while(k > 0);
  59. return bits;
  60. }
  61. static inline int mpc8_get_mod_golomb(GetBitContext *gb, int m)
  62. {
  63. if(mpc8_cnk_len[0][m] < 1) return 0;
  64. return mpc8_dec_base(gb, 1, m+1);
  65. }
  66. static int mpc8_get_mask(GetBitContext *gb, int size, int t)
  67. {
  68. int mask = 0;
  69. if(t && t != size)
  70. mask = mpc8_dec_enum(gb, FFMIN(t, size - t), size);
  71. if((t << 1) > size) mask = ~mask;
  72. return mask;
  73. }
  74. static const uint16_t vlc_offsets[13] = {
  75. 0, 640, 1184, 1748, 2298, 2426, 2554, 3066, 3578, 4106, 4618, 5196, 5708
  76. };
  77. static av_cold void build_vlc(VLC *vlc, int nb_bits,
  78. const uint8_t codes_counts[16],
  79. const uint8_t syms[], int offset)
  80. {
  81. uint8_t len[MPC8_MAX_VLC_SIZE];
  82. unsigned num = 0;
  83. for (int i = 16; i > 0; i--)
  84. for (unsigned tmp = num + codes_counts[i - 1]; num < tmp; num++)
  85. len[num] = i;
  86. ff_init_vlc_from_lengths(vlc, nb_bits, num, len, 1,
  87. syms, 1, 1, offset, INIT_VLC_USE_NEW_STATIC, NULL);
  88. }
  89. static av_cold int mpc8_decode_init(AVCodecContext * avctx)
  90. {
  91. int i;
  92. MPCContext *c = avctx->priv_data;
  93. GetBitContext gb;
  94. static int vlc_initialized = 0;
  95. int channels;
  96. static VLC_TYPE codes_table[5708][2];
  97. if(avctx->extradata_size < 2){
  98. av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size);
  99. return -1;
  100. }
  101. memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
  102. av_lfg_init(&c->rnd, 0xDEADBEEF);
  103. ff_mpadsp_init(&c->mpadsp);
  104. init_get_bits(&gb, avctx->extradata, 16);
  105. skip_bits(&gb, 3);//sample rate
  106. c->maxbands = get_bits(&gb, 5) + 1;
  107. if (c->maxbands >= BANDS) {
  108. av_log(avctx,AV_LOG_ERROR, "maxbands %d too high\n", c->maxbands);
  109. return AVERROR_INVALIDDATA;
  110. }
  111. channels = get_bits(&gb, 4) + 1;
  112. if (channels > 2) {
  113. avpriv_request_sample(avctx, "Multichannel MPC SV8");
  114. return AVERROR_PATCHWELCOME;
  115. }
  116. c->MSS = get_bits1(&gb);
  117. c->frames = 1 << (get_bits(&gb, 3) * 2);
  118. avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  119. avctx->channel_layout = (channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
  120. avctx->channels = channels;
  121. if(vlc_initialized) return 0;
  122. av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
  123. #define INIT_VLC(vlc, bits, len_counts, symbols, offset, static_size) \
  124. do { \
  125. static VLC_TYPE table[static_size][2]; \
  126. (vlc)->table = table; \
  127. (vlc)->table_allocated = static_size; \
  128. build_vlc(vlc, bits, len_counts, symbols, offset); \
  129. } while (0)
  130. INIT_VLC(&band_vlc, MPC8_BANDS_BITS,
  131. mpc8_bands_len_counts, mpc8_bands_syms, 0, 542);
  132. INIT_VLC(&q1_vlc, MPC8_Q1_BITS,
  133. mpc8_q1_len_counts, mpc8_q1_syms, 0, 520);
  134. INIT_VLC(&q9up_vlc, MPC8_Q9UP_BITS,
  135. mpc8_q9up_len_counts, mpc8_q9up_syms, 0, 524);
  136. INIT_VLC(&scfi_vlc[0], MPC8_SCFI0_BITS,
  137. mpc8_scfi_len_counts[0], mpc8_scfi0_syms, 0, 1 << MPC8_SCFI0_BITS);
  138. INIT_VLC(&scfi_vlc[1], MPC8_SCFI1_BITS,
  139. mpc8_scfi_len_counts[1], mpc8_scfi1_syms, 0, 1 << MPC8_SCFI1_BITS);
  140. INIT_VLC(&dscf_vlc[0], MPC8_DSCF0_BITS,
  141. mpc8_dscf_len_counts[0], mpc8_dscf0_syms, 0, 560);
  142. INIT_VLC(&dscf_vlc[1], MPC8_DSCF1_BITS,
  143. mpc8_dscf_len_counts[1], mpc8_dscf1_syms, 0, 598);
  144. INIT_VLC(&q3_vlc[0], MPC8_Q3_BITS,
  145. mpc8_q3_len_counts, mpc8_q3_syms, MPC8_Q3_OFFSET, 512);
  146. INIT_VLC(&q3_vlc[1], MPC8_Q4_BITS,
  147. mpc8_q4_len_counts, mpc8_q4_syms, MPC8_Q4_OFFSET, 516);
  148. for(i = 0; i < 2; i++){
  149. res_vlc[i].table = &codes_table[vlc_offsets[0+i]];
  150. res_vlc[i].table_allocated = vlc_offsets[1+i] - vlc_offsets[0+i];
  151. build_vlc(&res_vlc[i], MPC8_RES_BITS,
  152. mpc8_res_len_counts[i], mpc8_res_syms[i], 0);
  153. q2_vlc[i].table = &codes_table[vlc_offsets[2+i]];
  154. q2_vlc[i].table_allocated = vlc_offsets[3+i] - vlc_offsets[2+i];
  155. build_vlc(&q2_vlc[i], MPC8_Q2_BITS,
  156. mpc8_q2_len_counts[i], mpc8_q2_syms[i], 0);
  157. quant_vlc[0][i].table = &codes_table[vlc_offsets[4+i]];
  158. quant_vlc[0][i].table_allocated = vlc_offsets[5+i] - vlc_offsets[4+i];
  159. build_vlc(&quant_vlc[0][i], MPC8_Q5_BITS,
  160. mpc8_q5_len_counts[i], mpc8_q5_syms[i], MPC8_Q5_OFFSET);
  161. quant_vlc[1][i].table = &codes_table[vlc_offsets[6+i]];
  162. quant_vlc[1][i].table_allocated = vlc_offsets[7+i] - vlc_offsets[6+i];
  163. build_vlc(&quant_vlc[1][i], MPC8_Q6_BITS,
  164. mpc8_q6_len_counts[i], mpc8_q6_syms[i], MPC8_Q6_OFFSET);
  165. quant_vlc[2][i].table = &codes_table[vlc_offsets[8+i]];
  166. quant_vlc[2][i].table_allocated = vlc_offsets[9+i] - vlc_offsets[8+i];
  167. build_vlc(&quant_vlc[2][i], MPC8_Q7_BITS,
  168. mpc8_q7_len_counts[i], mpc8_q7_syms[i], MPC8_Q7_OFFSET);
  169. quant_vlc[3][i].table = &codes_table[vlc_offsets[10+i]];
  170. quant_vlc[3][i].table_allocated = vlc_offsets[11+i] - vlc_offsets[10+i];
  171. build_vlc(&quant_vlc[3][i], MPC8_Q8_BITS,
  172. mpc8_q8_len_counts[i], mpc8_q8_syms[i], MPC8_Q8_OFFSET);
  173. }
  174. vlc_initialized = 1;
  175. ff_mpa_synth_init_fixed();
  176. return 0;
  177. }
  178. static int mpc8_decode_frame(AVCodecContext * avctx, void *data,
  179. int *got_frame_ptr, AVPacket *avpkt)
  180. {
  181. AVFrame *frame = data;
  182. const uint8_t *buf = avpkt->data;
  183. int buf_size = avpkt->size;
  184. MPCContext *c = avctx->priv_data;
  185. GetBitContext gb2, *gb = &gb2;
  186. int i, j, k, ch, cnt, res, t;
  187. Band *bands = c->bands;
  188. int off;
  189. int maxband, keyframe;
  190. int last[2];
  191. keyframe = c->cur_frame == 0;
  192. if(keyframe){
  193. memset(c->Q, 0, sizeof(c->Q));
  194. c->last_bits_used = 0;
  195. }
  196. if ((res = init_get_bits8(gb, buf, buf_size)) < 0)
  197. return res;
  198. skip_bits(gb, c->last_bits_used & 7);
  199. if(keyframe)
  200. maxband = mpc8_get_mod_golomb(gb, c->maxbands + 1);
  201. else{
  202. maxband = c->last_max_band + get_vlc2(gb, band_vlc.table, MPC8_BANDS_BITS, 2);
  203. if(maxband > 32) maxband -= 33;
  204. }
  205. if (get_bits_left(gb) < 0) {
  206. *got_frame_ptr = 0;
  207. return buf_size;
  208. }
  209. if(maxband > c->maxbands + 1) {
  210. av_log(avctx, AV_LOG_ERROR, "maxband %d too large\n",maxband);
  211. return AVERROR_INVALIDDATA;
  212. }
  213. c->last_max_band = maxband;
  214. /* read subband indexes */
  215. if(maxband){
  216. last[0] = last[1] = 0;
  217. for(i = maxband - 1; i >= 0; i--){
  218. for(ch = 0; ch < 2; ch++){
  219. last[ch] = get_vlc2(gb, res_vlc[last[ch] > 2].table, MPC8_RES_BITS, 2) + last[ch];
  220. if(last[ch] > 15) last[ch] -= 17;
  221. bands[i].res[ch] = last[ch];
  222. }
  223. }
  224. if(c->MSS){
  225. int mask;
  226. cnt = 0;
  227. for(i = 0; i < maxband; i++)
  228. if(bands[i].res[0] || bands[i].res[1])
  229. cnt++;
  230. t = mpc8_get_mod_golomb(gb, cnt);
  231. mask = mpc8_get_mask(gb, cnt, t);
  232. for(i = maxband - 1; i >= 0; i--)
  233. if(bands[i].res[0] || bands[i].res[1]){
  234. bands[i].msf = mask & 1;
  235. mask >>= 1;
  236. }
  237. }
  238. }
  239. for(i = maxband; i < c->maxbands; i++)
  240. bands[i].res[0] = bands[i].res[1] = 0;
  241. if(keyframe){
  242. for(i = 0; i < 32; i++)
  243. c->oldDSCF[0][i] = c->oldDSCF[1][i] = 1;
  244. }
  245. for(i = 0; i < maxband; i++){
  246. if(bands[i].res[0] || bands[i].res[1]){
  247. cnt = !!bands[i].res[0] + !!bands[i].res[1] - 1;
  248. if(cnt >= 0){
  249. t = get_vlc2(gb, scfi_vlc[cnt].table, scfi_vlc[cnt].bits, 1);
  250. if(bands[i].res[0]) bands[i].scfi[0] = t >> (2 * cnt);
  251. if(bands[i].res[1]) bands[i].scfi[1] = t & 3;
  252. }
  253. }
  254. }
  255. for(i = 0; i < maxband; i++){
  256. for(ch = 0; ch < 2; ch++){
  257. if(!bands[i].res[ch]) continue;
  258. if(c->oldDSCF[ch][i]){
  259. bands[i].scf_idx[ch][0] = get_bits(gb, 7) - 6;
  260. c->oldDSCF[ch][i] = 0;
  261. }else{
  262. t = get_vlc2(gb, dscf_vlc[1].table, MPC8_DSCF1_BITS, 2);
  263. if(t == 64)
  264. t += get_bits(gb, 6);
  265. bands[i].scf_idx[ch][0] = ((bands[i].scf_idx[ch][2] + t - 25) & 0x7F) - 6;
  266. }
  267. for(j = 0; j < 2; j++){
  268. if((bands[i].scfi[ch] << j) & 2)
  269. bands[i].scf_idx[ch][j + 1] = bands[i].scf_idx[ch][j];
  270. else{
  271. t = get_vlc2(gb, dscf_vlc[0].table, MPC8_DSCF0_BITS, 2);
  272. if(t == 31)
  273. t = 64 + get_bits(gb, 6);
  274. bands[i].scf_idx[ch][j + 1] = ((bands[i].scf_idx[ch][j] + t - 25) & 0x7F) - 6;
  275. }
  276. }
  277. }
  278. }
  279. for(i = 0, off = 0; i < maxband; i++, off += SAMPLES_PER_BAND){
  280. for(ch = 0; ch < 2; ch++){
  281. res = bands[i].res[ch];
  282. switch(res){
  283. case -1:
  284. for(j = 0; j < SAMPLES_PER_BAND; j++)
  285. c->Q[ch][off + j] = (av_lfg_get(&c->rnd) & 0x3FC) - 510;
  286. break;
  287. case 0:
  288. break;
  289. case 1:
  290. for(j = 0; j < SAMPLES_PER_BAND; j += SAMPLES_PER_BAND / 2){
  291. cnt = get_vlc2(gb, q1_vlc.table, MPC8_Q1_BITS, 2);
  292. t = mpc8_get_mask(gb, 18, cnt);
  293. for(k = 0; k < SAMPLES_PER_BAND / 2; k++)
  294. c->Q[ch][off + j + k] = t & (1 << (SAMPLES_PER_BAND / 2 - k - 1))
  295. ? (get_bits1(gb) << 1) - 1 : 0;
  296. }
  297. break;
  298. case 2:
  299. cnt = 6;//2*mpc8_thres[res]
  300. for(j = 0; j < SAMPLES_PER_BAND; j += 3){
  301. t = get_vlc2(gb, q2_vlc[cnt > 3].table, MPC8_Q2_BITS, 2);
  302. c->Q[ch][off + j + 0] = mpc8_idx50[t];
  303. c->Q[ch][off + j + 1] = mpc8_idx51[t];
  304. c->Q[ch][off + j + 2] = mpc8_idx52[t];
  305. cnt = (cnt >> 1) + mpc8_huffq2[t];
  306. }
  307. break;
  308. case 3:
  309. case 4:
  310. for(j = 0; j < SAMPLES_PER_BAND; j += 2){
  311. t = get_vlc2(gb, q3_vlc[res - 3].table, MPC8_Q3_BITS, 2);
  312. c->Q[ch][off + j + 1] = t >> 4;
  313. c->Q[ch][off + j + 0] = sign_extend(t, 4);
  314. }
  315. break;
  316. case 5:
  317. case 6:
  318. case 7:
  319. case 8:
  320. cnt = 2 * mpc8_thres[res];
  321. for(j = 0; j < SAMPLES_PER_BAND; j++){
  322. const VLC *vlc = &quant_vlc[res - 5][cnt > mpc8_thres[res]];
  323. c->Q[ch][off + j] = get_vlc2(gb, vlc->table, vlc->bits, 2);
  324. cnt = (cnt >> 1) + FFABS(c->Q[ch][off + j]);
  325. }
  326. break;
  327. default:
  328. for(j = 0; j < SAMPLES_PER_BAND; j++){
  329. c->Q[ch][off + j] = get_vlc2(gb, q9up_vlc.table, MPC8_Q9UP_BITS, 2);
  330. if(res != 9){
  331. c->Q[ch][off + j] <<= res - 9;
  332. c->Q[ch][off + j] |= get_bits(gb, res - 9);
  333. }
  334. c->Q[ch][off + j] -= (1 << (res - 2)) - 1;
  335. }
  336. }
  337. }
  338. }
  339. frame->nb_samples = MPC_FRAME_SIZE;
  340. if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
  341. return res;
  342. ff_mpc_dequantize_and_synth(c, maxband - 1,
  343. (int16_t **)frame->extended_data,
  344. avctx->channels);
  345. c->cur_frame++;
  346. c->last_bits_used = get_bits_count(gb);
  347. if(c->cur_frame >= c->frames)
  348. c->cur_frame = 0;
  349. if (get_bits_left(gb) < 0) {
  350. av_log(avctx, AV_LOG_ERROR, "Overread %d\n", -get_bits_left(gb));
  351. c->last_bits_used = buf_size << 3;
  352. } else if (c->cur_frame == 0 && get_bits_left(gb) < 8) {// we have only padding left
  353. c->last_bits_used = buf_size << 3;
  354. }
  355. *got_frame_ptr = 1;
  356. return c->cur_frame ? c->last_bits_used >> 3 : buf_size;
  357. }
  358. static av_cold void mpc8_decode_flush(AVCodecContext *avctx)
  359. {
  360. MPCContext *c = avctx->priv_data;
  361. c->cur_frame = 0;
  362. }
  363. AVCodec ff_mpc8_decoder = {
  364. .name = "mpc8",
  365. .long_name = NULL_IF_CONFIG_SMALL("Musepack SV8"),
  366. .type = AVMEDIA_TYPE_AUDIO,
  367. .id = AV_CODEC_ID_MUSEPACK8,
  368. .priv_data_size = sizeof(MPCContext),
  369. .init = mpc8_decode_init,
  370. .decode = mpc8_decode_frame,
  371. .flush = mpc8_decode_flush,
  372. .capabilities = AV_CODEC_CAP_DR1,
  373. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
  374. AV_SAMPLE_FMT_NONE },
  375. };