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.

346 lines
11KB

  1. /*
  2. * Musepack SV7 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
  23. * MPEG Audio Layer 1/2 -like codec with frames of 1152 samples
  24. * divided into 32 subbands.
  25. */
  26. #include "libavutil/lfg.h"
  27. #include "avcodec.h"
  28. #include "get_bits.h"
  29. #include "dsputil.h"
  30. #include "mpegaudiodsp.h"
  31. #include "libavutil/audioconvert.h"
  32. #include "mpc.h"
  33. #include "mpc7data.h"
  34. static VLC scfi_vlc, dscf_vlc, hdr_vlc, quant_vlc[MPC7_QUANT_VLC_TABLES][2];
  35. static const uint16_t quant_offsets[MPC7_QUANT_VLC_TABLES*2 + 1] =
  36. {
  37. 0, 512, 1024, 1536, 2052, 2564, 3076, 3588, 4100, 4612, 5124,
  38. 5636, 6164, 6676, 7224
  39. };
  40. static av_cold int mpc7_decode_init(AVCodecContext * avctx)
  41. {
  42. int i, j;
  43. MPCContext *c = avctx->priv_data;
  44. GetBitContext gb;
  45. LOCAL_ALIGNED_16(uint8_t, buf, [16]);
  46. static int vlc_initialized = 0;
  47. static VLC_TYPE scfi_table[1 << MPC7_SCFI_BITS][2];
  48. static VLC_TYPE dscf_table[1 << MPC7_DSCF_BITS][2];
  49. static VLC_TYPE hdr_table[1 << MPC7_HDR_BITS][2];
  50. static VLC_TYPE quant_tables[7224][2];
  51. /* Musepack SV7 is always stereo */
  52. if (avctx->channels != 2) {
  53. av_log_ask_for_sample(avctx, "Unsupported number of channels: %d\n",
  54. avctx->channels);
  55. return AVERROR_PATCHWELCOME;
  56. }
  57. if(avctx->extradata_size < 16){
  58. av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size);
  59. return -1;
  60. }
  61. memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
  62. av_lfg_init(&c->rnd, 0xDEADBEEF);
  63. ff_dsputil_init(&c->dsp, avctx);
  64. ff_mpadsp_init(&c->mpadsp);
  65. c->dsp.bswap_buf((uint32_t*)buf, (const uint32_t*)avctx->extradata, 4);
  66. ff_mpc_init();
  67. init_get_bits(&gb, buf, 128);
  68. c->IS = get_bits1(&gb);
  69. c->MSS = get_bits1(&gb);
  70. c->maxbands = get_bits(&gb, 6);
  71. if(c->maxbands >= BANDS){
  72. av_log(avctx, AV_LOG_ERROR, "Too many bands: %i\n", c->maxbands);
  73. return -1;
  74. }
  75. skip_bits_long(&gb, 88);
  76. c->gapless = get_bits1(&gb);
  77. c->lastframelen = get_bits(&gb, 11);
  78. av_log(avctx, AV_LOG_DEBUG, "IS: %d, MSS: %d, TG: %d, LFL: %d, bands: %d\n",
  79. c->IS, c->MSS, c->gapless, c->lastframelen, c->maxbands);
  80. c->frames_to_skip = 0;
  81. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  82. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  83. avcodec_get_frame_defaults(&c->frame);
  84. avctx->coded_frame = &c->frame;
  85. if(vlc_initialized) return 0;
  86. av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
  87. scfi_vlc.table = scfi_table;
  88. scfi_vlc.table_allocated = 1 << MPC7_SCFI_BITS;
  89. if(init_vlc(&scfi_vlc, MPC7_SCFI_BITS, MPC7_SCFI_SIZE,
  90. &mpc7_scfi[1], 2, 1,
  91. &mpc7_scfi[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){
  92. av_log(avctx, AV_LOG_ERROR, "Cannot init SCFI VLC\n");
  93. return -1;
  94. }
  95. dscf_vlc.table = dscf_table;
  96. dscf_vlc.table_allocated = 1 << MPC7_DSCF_BITS;
  97. if(init_vlc(&dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE,
  98. &mpc7_dscf[1], 2, 1,
  99. &mpc7_dscf[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){
  100. av_log(avctx, AV_LOG_ERROR, "Cannot init DSCF VLC\n");
  101. return -1;
  102. }
  103. hdr_vlc.table = hdr_table;
  104. hdr_vlc.table_allocated = 1 << MPC7_HDR_BITS;
  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_NEW_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. quant_vlc[i][j].table = &quant_tables[quant_offsets[i*2 + j]];
  114. quant_vlc[i][j].table_allocated = quant_offsets[i*2 + j + 1] - quant_offsets[i*2 + j];
  115. if(init_vlc(&quant_vlc[i][j], 9, mpc7_quant_vlc_sizes[i],
  116. &mpc7_quant_vlc[i][j][1], 4, 2,
  117. &mpc7_quant_vlc[i][j][0], 4, 2, INIT_VLC_USE_NEW_STATIC)){
  118. av_log(avctx, AV_LOG_ERROR, "Cannot init QUANT VLC %i,%i\n",i,j);
  119. return -1;
  120. }
  121. }
  122. }
  123. vlc_initialized = 1;
  124. return 0;
  125. }
  126. /**
  127. * Fill samples for given subband
  128. */
  129. static inline void idx_to_quant(MPCContext *c, GetBitContext *gb, int idx, int *dst)
  130. {
  131. int i, i1, t;
  132. switch(idx){
  133. case -1:
  134. for(i = 0; i < SAMPLES_PER_BAND; i++){
  135. *dst++ = (av_lfg_get(&c->rnd) & 0x3FC) - 510;
  136. }
  137. break;
  138. case 1:
  139. i1 = get_bits1(gb);
  140. for(i = 0; i < SAMPLES_PER_BAND/3; i++){
  141. t = get_vlc2(gb, quant_vlc[0][i1].table, 9, 2);
  142. *dst++ = mpc7_idx30[t];
  143. *dst++ = mpc7_idx31[t];
  144. *dst++ = mpc7_idx32[t];
  145. }
  146. break;
  147. case 2:
  148. i1 = get_bits1(gb);
  149. for(i = 0; i < SAMPLES_PER_BAND/2; i++){
  150. t = get_vlc2(gb, quant_vlc[1][i1].table, 9, 2);
  151. *dst++ = mpc7_idx50[t];
  152. *dst++ = mpc7_idx51[t];
  153. }
  154. break;
  155. case 3: case 4: case 5: case 6: case 7:
  156. i1 = get_bits1(gb);
  157. for(i = 0; i < SAMPLES_PER_BAND; i++)
  158. *dst++ = get_vlc2(gb, quant_vlc[idx-1][i1].table, 9, 2) - mpc7_quant_vlc_off[idx-1];
  159. break;
  160. case 8: case 9: case 10: case 11: case 12:
  161. case 13: case 14: case 15: case 16: case 17:
  162. t = (1 << (idx - 2)) - 1;
  163. for(i = 0; i < SAMPLES_PER_BAND; i++)
  164. *dst++ = get_bits(gb, idx - 1) - t;
  165. break;
  166. default: // case 0 and -2..-17
  167. return;
  168. }
  169. }
  170. static int get_scale_idx(GetBitContext *gb, int ref)
  171. {
  172. int t = get_vlc2(gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  173. if (t == 8)
  174. return get_bits(gb, 6);
  175. return ref + t;
  176. }
  177. static int mpc7_decode_frame(AVCodecContext * avctx, void *data,
  178. int *got_frame_ptr, AVPacket *avpkt)
  179. {
  180. const uint8_t *buf = avpkt->data;
  181. int buf_size;
  182. MPCContext *c = avctx->priv_data;
  183. GetBitContext gb;
  184. int i, ch;
  185. int mb = -1;
  186. Band *bands = c->bands;
  187. int off, ret, last_frame, skip;
  188. int bits_used, bits_avail;
  189. memset(bands, 0, sizeof(*bands) * (c->maxbands + 1));
  190. buf_size = avpkt->size & ~3;
  191. if (buf_size <= 0) {
  192. av_log(avctx, AV_LOG_ERROR, "packet size is too small (%i bytes)\n",
  193. avpkt->size);
  194. return AVERROR_INVALIDDATA;
  195. }
  196. if (buf_size != avpkt->size) {
  197. av_log(avctx, AV_LOG_WARNING, "packet size is not a multiple of 4. "
  198. "extra bytes at the end will be skipped.\n");
  199. }
  200. skip = buf[0];
  201. last_frame = buf[1];
  202. buf += 4;
  203. buf_size -= 4;
  204. /* get output buffer */
  205. c->frame.nb_samples = MPC_FRAME_SIZE;
  206. if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
  207. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  208. return ret;
  209. }
  210. av_fast_padded_malloc(&c->bits, &c->buf_size, buf_size);
  211. if (!c->bits)
  212. return AVERROR(ENOMEM);
  213. c->dsp.bswap_buf((uint32_t *)c->bits, (const uint32_t *)buf, buf_size >> 2);
  214. init_get_bits(&gb, c->bits, buf_size * 8);
  215. skip_bits_long(&gb, skip);
  216. /* read subband indexes */
  217. for(i = 0; i <= c->maxbands; i++){
  218. for(ch = 0; ch < 2; ch++){
  219. int t = 4;
  220. if(i) t = get_vlc2(&gb, hdr_vlc.table, MPC7_HDR_BITS, 1) - 5;
  221. if(t == 4) bands[i].res[ch] = get_bits(&gb, 4);
  222. else bands[i].res[ch] = bands[i-1].res[ch] + t;
  223. if (bands[i].res[ch] < -1 || bands[i].res[ch] > 17) {
  224. av_log(avctx, AV_LOG_ERROR, "subband index invalid\n");
  225. return AVERROR_INVALIDDATA;
  226. }
  227. }
  228. if(bands[i].res[0] || bands[i].res[1]){
  229. mb = i;
  230. if(c->MSS) bands[i].msf = get_bits1(&gb);
  231. }
  232. }
  233. /* get scale indexes coding method */
  234. for(i = 0; i <= mb; i++)
  235. for(ch = 0; ch < 2; ch++)
  236. if(bands[i].res[ch]) bands[i].scfi[ch] = get_vlc2(&gb, scfi_vlc.table, MPC7_SCFI_BITS, 1);
  237. /* get scale indexes */
  238. for(i = 0; i <= mb; i++){
  239. for(ch = 0; ch < 2; ch++){
  240. if(bands[i].res[ch]){
  241. bands[i].scf_idx[ch][2] = c->oldDSCF[ch][i];
  242. bands[i].scf_idx[ch][0] = get_scale_idx(&gb, bands[i].scf_idx[ch][2]);
  243. switch(bands[i].scfi[ch]){
  244. case 0:
  245. bands[i].scf_idx[ch][1] = get_scale_idx(&gb, bands[i].scf_idx[ch][0]);
  246. bands[i].scf_idx[ch][2] = get_scale_idx(&gb, bands[i].scf_idx[ch][1]);
  247. break;
  248. case 1:
  249. bands[i].scf_idx[ch][1] = get_scale_idx(&gb, bands[i].scf_idx[ch][0]);
  250. bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1];
  251. break;
  252. case 2:
  253. bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
  254. bands[i].scf_idx[ch][2] = get_scale_idx(&gb, bands[i].scf_idx[ch][1]);
  255. break;
  256. case 3:
  257. bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
  258. break;
  259. }
  260. c->oldDSCF[ch][i] = bands[i].scf_idx[ch][2];
  261. }
  262. }
  263. }
  264. /* get quantizers */
  265. memset(c->Q, 0, sizeof(c->Q));
  266. off = 0;
  267. for(i = 0; i < BANDS; i++, off += SAMPLES_PER_BAND)
  268. for(ch = 0; ch < 2; ch++)
  269. idx_to_quant(c, &gb, bands[i].res[ch], c->Q[ch] + off);
  270. ff_mpc_dequantize_and_synth(c, mb, c->frame.data[0], 2);
  271. if(last_frame)
  272. c->frame.nb_samples = c->lastframelen;
  273. bits_used = get_bits_count(&gb);
  274. bits_avail = buf_size * 8;
  275. if (!last_frame && ((bits_avail < bits_used) || (bits_used + 32 <= bits_avail))) {
  276. av_log(avctx, AV_LOG_ERROR, "Error decoding frame: used %i of %i bits\n", bits_used, bits_avail);
  277. return -1;
  278. }
  279. if(c->frames_to_skip){
  280. c->frames_to_skip--;
  281. *got_frame_ptr = 0;
  282. return avpkt->size;
  283. }
  284. *got_frame_ptr = 1;
  285. *(AVFrame *)data = c->frame;
  286. return avpkt->size;
  287. }
  288. static void mpc7_decode_flush(AVCodecContext *avctx)
  289. {
  290. MPCContext *c = avctx->priv_data;
  291. memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
  292. c->frames_to_skip = 32;
  293. }
  294. static av_cold int mpc7_decode_close(AVCodecContext *avctx)
  295. {
  296. MPCContext *c = avctx->priv_data;
  297. av_freep(&c->bits);
  298. c->buf_size = 0;
  299. return 0;
  300. }
  301. AVCodec ff_mpc7_decoder = {
  302. .name = "mpc7",
  303. .type = AVMEDIA_TYPE_AUDIO,
  304. .id = AV_CODEC_ID_MUSEPACK7,
  305. .priv_data_size = sizeof(MPCContext),
  306. .init = mpc7_decode_init,
  307. .close = mpc7_decode_close,
  308. .decode = mpc7_decode_frame,
  309. .flush = mpc7_decode_flush,
  310. .capabilities = CODEC_CAP_DR1,
  311. .long_name = NULL_IF_CONFIG_SMALL("Musepack SV7"),
  312. };