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.

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