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.

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