mpc7.c 9.5 KB

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