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.

304 lines
9.9KB

  1. /*
  2. * Musepack SV7 decoder
  3. * Copyright (c) 2006 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "mpegaudio.h"
  31. #include "libavutil/audioconvert.h"
  32. #include "mpc.h"
  33. #include "mpc7data.h"
  34. #define BANDS 32
  35. #define SAMPLES_PER_BAND 36
  36. #define MPC_FRAME_SIZE (BANDS * SAMPLES_PER_BAND)
  37. static VLC scfi_vlc, dscf_vlc, hdr_vlc, quant_vlc[MPC7_QUANT_VLC_TABLES][2];
  38. static const uint16_t quant_offsets[MPC7_QUANT_VLC_TABLES*2 + 1] =
  39. {
  40. 0, 512, 1024, 1536, 2052, 2564, 3076, 3588, 4100, 4612, 5124,
  41. 5636, 6164, 6676, 7224
  42. };
  43. static av_cold int mpc7_decode_init(AVCodecContext * avctx)
  44. {
  45. int i, j;
  46. MPCContext *c = avctx->priv_data;
  47. GetBitContext gb;
  48. uint8_t buf[16];
  49. static int vlc_initialized = 0;
  50. static VLC_TYPE scfi_table[1 << MPC7_SCFI_BITS][2];
  51. static VLC_TYPE dscf_table[1 << MPC7_DSCF_BITS][2];
  52. static VLC_TYPE hdr_table[1 << MPC7_HDR_BITS][2];
  53. static VLC_TYPE quant_tables[7224][2];
  54. if(avctx->extradata_size < 16){
  55. av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size);
  56. return -1;
  57. }
  58. memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
  59. av_lfg_init(&c->rnd, 0xDEADBEEF);
  60. dsputil_init(&c->dsp, avctx);
  61. c->dsp.bswap_buf((uint32_t*)buf, (const uint32_t*)avctx->extradata, 4);
  62. ff_mpc_init();
  63. init_get_bits(&gb, buf, 128);
  64. c->IS = get_bits1(&gb);
  65. c->MSS = get_bits1(&gb);
  66. c->maxbands = get_bits(&gb, 6);
  67. if(c->maxbands >= BANDS){
  68. av_log(avctx, AV_LOG_ERROR, "Too many bands: %i\n", c->maxbands);
  69. return -1;
  70. }
  71. skip_bits_long(&gb, 88);
  72. c->gapless = get_bits1(&gb);
  73. c->lastframelen = get_bits(&gb, 11);
  74. av_log(avctx, AV_LOG_DEBUG, "IS: %d, MSS: %d, TG: %d, LFL: %d, bands: %d\n",
  75. c->IS, c->MSS, c->gapless, c->lastframelen, c->maxbands);
  76. c->frames_to_skip = 0;
  77. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  78. avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
  79. if(vlc_initialized) return 0;
  80. av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
  81. scfi_vlc.table = scfi_table;
  82. scfi_vlc.table_allocated = 1 << MPC7_SCFI_BITS;
  83. if(init_vlc(&scfi_vlc, MPC7_SCFI_BITS, MPC7_SCFI_SIZE,
  84. &mpc7_scfi[1], 2, 1,
  85. &mpc7_scfi[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){
  86. av_log(avctx, AV_LOG_ERROR, "Cannot init SCFI VLC\n");
  87. return -1;
  88. }
  89. dscf_vlc.table = dscf_table;
  90. dscf_vlc.table_allocated = 1 << MPC7_DSCF_BITS;
  91. if(init_vlc(&dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE,
  92. &mpc7_dscf[1], 2, 1,
  93. &mpc7_dscf[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){
  94. av_log(avctx, AV_LOG_ERROR, "Cannot init DSCF VLC\n");
  95. return -1;
  96. }
  97. hdr_vlc.table = hdr_table;
  98. hdr_vlc.table_allocated = 1 << MPC7_HDR_BITS;
  99. if(init_vlc(&hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE,
  100. &mpc7_hdr[1], 2, 1,
  101. &mpc7_hdr[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){
  102. av_log(avctx, AV_LOG_ERROR, "Cannot init HDR VLC\n");
  103. return -1;
  104. }
  105. for(i = 0; i < MPC7_QUANT_VLC_TABLES; i++){
  106. for(j = 0; j < 2; j++){
  107. quant_vlc[i][j].table = &quant_tables[quant_offsets[i*2 + j]];
  108. quant_vlc[i][j].table_allocated = quant_offsets[i*2 + j + 1] - quant_offsets[i*2 + j];
  109. if(init_vlc(&quant_vlc[i][j], 9, mpc7_quant_vlc_sizes[i],
  110. &mpc7_quant_vlc[i][j][1], 4, 2,
  111. &mpc7_quant_vlc[i][j][0], 4, 2, INIT_VLC_USE_NEW_STATIC)){
  112. av_log(avctx, AV_LOG_ERROR, "Cannot init QUANT VLC %i,%i\n",i,j);
  113. return -1;
  114. }
  115. }
  116. }
  117. vlc_initialized = 1;
  118. return 0;
  119. }
  120. /**
  121. * Fill samples for given subband
  122. */
  123. static inline void idx_to_quant(MPCContext *c, GetBitContext *gb, int idx, int *dst)
  124. {
  125. int i, i1, t;
  126. switch(idx){
  127. case -1:
  128. for(i = 0; i < SAMPLES_PER_BAND; i++){
  129. *dst++ = (av_lfg_get(&c->rnd) & 0x3FC) - 510;
  130. }
  131. break;
  132. case 1:
  133. i1 = get_bits1(gb);
  134. for(i = 0; i < SAMPLES_PER_BAND/3; i++){
  135. t = get_vlc2(gb, quant_vlc[0][i1].table, 9, 2);
  136. *dst++ = mpc7_idx30[t];
  137. *dst++ = mpc7_idx31[t];
  138. *dst++ = mpc7_idx32[t];
  139. }
  140. break;
  141. case 2:
  142. i1 = get_bits1(gb);
  143. for(i = 0; i < SAMPLES_PER_BAND/2; i++){
  144. t = get_vlc2(gb, quant_vlc[1][i1].table, 9, 2);
  145. *dst++ = mpc7_idx50[t];
  146. *dst++ = mpc7_idx51[t];
  147. }
  148. break;
  149. case 3: case 4: case 5: case 6: case 7:
  150. i1 = get_bits1(gb);
  151. for(i = 0; i < SAMPLES_PER_BAND; i++)
  152. *dst++ = get_vlc2(gb, quant_vlc[idx-1][i1].table, 9, 2) - mpc7_quant_vlc_off[idx-1];
  153. break;
  154. case 8: case 9: case 10: case 11: case 12:
  155. case 13: case 14: case 15: case 16: case 17:
  156. t = (1 << (idx - 2)) - 1;
  157. for(i = 0; i < SAMPLES_PER_BAND; i++)
  158. *dst++ = get_bits(gb, idx - 1) - t;
  159. break;
  160. default: // case 0 and -2..-17
  161. return;
  162. }
  163. }
  164. static int get_scale_idx(GetBitContext *gb, int ref)
  165. {
  166. int t = get_vlc2(gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
  167. if (t == 8)
  168. return get_bits(gb, 6);
  169. return ref + t;
  170. }
  171. static int mpc7_decode_frame(AVCodecContext * avctx,
  172. void *data, int *data_size,
  173. AVPacket *avpkt)
  174. {
  175. const uint8_t *buf = avpkt->data;
  176. int buf_size = avpkt->size;
  177. MPCContext *c = avctx->priv_data;
  178. GetBitContext gb;
  179. uint8_t *bits;
  180. int i, ch;
  181. int mb = -1;
  182. Band *bands = c->bands;
  183. int off;
  184. int bits_used, bits_avail;
  185. memset(bands, 0, sizeof(bands));
  186. if(buf_size <= 4){
  187. av_log(avctx, AV_LOG_ERROR, "Too small buffer passed (%i bytes)\n", buf_size);
  188. }
  189. bits = av_malloc(((buf_size - 1) & ~3) + FF_INPUT_BUFFER_PADDING_SIZE);
  190. c->dsp.bswap_buf((uint32_t*)bits, (const uint32_t*)(buf + 4), (buf_size - 4) >> 2);
  191. init_get_bits(&gb, bits, (buf_size - 4)* 8);
  192. skip_bits_long(&gb, buf[0]);
  193. /* read subband indexes */
  194. for(i = 0; i <= c->maxbands; i++){
  195. for(ch = 0; ch < 2; ch++){
  196. int t = 4;
  197. if(i) t = get_vlc2(&gb, hdr_vlc.table, MPC7_HDR_BITS, 1) - 5;
  198. if(t == 4) bands[i].res[ch] = get_bits(&gb, 4);
  199. else bands[i].res[ch] = bands[i-1].res[ch] + t;
  200. }
  201. if(bands[i].res[0] || bands[i].res[1]){
  202. mb = i;
  203. if(c->MSS) bands[i].msf = get_bits1(&gb);
  204. }
  205. }
  206. /* get scale indexes coding method */
  207. for(i = 0; i <= mb; i++)
  208. for(ch = 0; ch < 2; ch++)
  209. if(bands[i].res[ch]) bands[i].scfi[ch] = get_vlc2(&gb, scfi_vlc.table, MPC7_SCFI_BITS, 1);
  210. /* get scale indexes */
  211. for(i = 0; i <= mb; i++){
  212. for(ch = 0; ch < 2; ch++){
  213. if(bands[i].res[ch]){
  214. bands[i].scf_idx[ch][2] = c->oldDSCF[ch][i];
  215. bands[i].scf_idx[ch][0] = get_scale_idx(&gb, bands[i].scf_idx[ch][2]);
  216. switch(bands[i].scfi[ch]){
  217. case 0:
  218. bands[i].scf_idx[ch][1] = get_scale_idx(&gb, bands[i].scf_idx[ch][0]);
  219. bands[i].scf_idx[ch][2] = get_scale_idx(&gb, bands[i].scf_idx[ch][1]);
  220. break;
  221. case 1:
  222. bands[i].scf_idx[ch][1] = get_scale_idx(&gb, bands[i].scf_idx[ch][0]);
  223. bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1];
  224. break;
  225. case 2:
  226. bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
  227. bands[i].scf_idx[ch][2] = get_scale_idx(&gb, bands[i].scf_idx[ch][1]);
  228. break;
  229. case 3:
  230. bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
  231. break;
  232. }
  233. c->oldDSCF[ch][i] = bands[i].scf_idx[ch][2];
  234. }
  235. }
  236. }
  237. /* get quantizers */
  238. memset(c->Q, 0, sizeof(c->Q));
  239. off = 0;
  240. for(i = 0; i < BANDS; i++, off += SAMPLES_PER_BAND)
  241. for(ch = 0; ch < 2; ch++)
  242. idx_to_quant(c, &gb, bands[i].res[ch], c->Q[ch] + off);
  243. ff_mpc_dequantize_and_synth(c, mb, data, 2);
  244. av_free(bits);
  245. bits_used = get_bits_count(&gb);
  246. bits_avail = (buf_size - 4) * 8;
  247. if(!buf[1] && ((bits_avail < bits_used) || (bits_used + 32 <= bits_avail))){
  248. av_log(NULL,0, "Error decoding frame: used %i of %i bits\n", bits_used, bits_avail);
  249. return -1;
  250. }
  251. if(c->frames_to_skip){
  252. c->frames_to_skip--;
  253. *data_size = 0;
  254. return buf_size;
  255. }
  256. *data_size = (buf[1] ? c->lastframelen : MPC_FRAME_SIZE) * 4;
  257. return buf_size;
  258. }
  259. static void mpc7_decode_flush(AVCodecContext *avctx)
  260. {
  261. MPCContext *c = avctx->priv_data;
  262. memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
  263. c->frames_to_skip = 32;
  264. }
  265. AVCodec ff_mpc7_decoder = {
  266. "mpc7",
  267. AVMEDIA_TYPE_AUDIO,
  268. CODEC_ID_MUSEPACK7,
  269. sizeof(MPCContext),
  270. mpc7_decode_init,
  271. NULL,
  272. NULL,
  273. mpc7_decode_frame,
  274. .flush = mpc7_decode_flush,
  275. .long_name = NULL_IF_CONFIG_SMALL("Musepack SV7"),
  276. };