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.

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