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.

441 lines
14KB

  1. /*
  2. * WMA compatible encoder
  3. * Copyright (c) 2007 Michael Niedermayer
  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. #include "libavutil/attributes.h"
  22. #include "avcodec.h"
  23. #include "internal.h"
  24. #include "wma.h"
  25. #undef NDEBUG
  26. #include <assert.h>
  27. static av_cold int encode_init(AVCodecContext *avctx)
  28. {
  29. WMACodecContext *s = avctx->priv_data;
  30. int i, flags1, flags2, block_align;
  31. uint8_t *extradata;
  32. s->avctx = avctx;
  33. if (avctx->channels > MAX_CHANNELS) {
  34. av_log(avctx, AV_LOG_ERROR,
  35. "too many channels: got %i, need %i or fewer",
  36. avctx->channels, MAX_CHANNELS);
  37. return AVERROR(EINVAL);
  38. }
  39. if (avctx->sample_rate > 48000) {
  40. av_log(avctx, AV_LOG_ERROR, "sample rate is too high: %d > 48kHz",
  41. avctx->sample_rate);
  42. return AVERROR(EINVAL);
  43. }
  44. if (avctx->bit_rate < 24 * 1000) {
  45. av_log(avctx, AV_LOG_ERROR,
  46. "bitrate too low: got %i, need 24000 or higher\n",
  47. avctx->bit_rate);
  48. return AVERROR(EINVAL);
  49. }
  50. /* extract flag info */
  51. flags1 = 0;
  52. flags2 = 1;
  53. if (avctx->codec->id == AV_CODEC_ID_WMAV1) {
  54. extradata = av_malloc(4);
  55. if (!extradata)
  56. return AVERROR(ENOMEM);
  57. avctx->extradata_size = 4;
  58. AV_WL16(extradata, flags1);
  59. AV_WL16(extradata + 2, flags2);
  60. } else if (avctx->codec->id == AV_CODEC_ID_WMAV2) {
  61. extradata = av_mallocz(10);
  62. if (!extradata)
  63. return AVERROR(ENOMEM);
  64. avctx->extradata_size = 10;
  65. AV_WL32(extradata, flags1);
  66. AV_WL16(extradata + 4, flags2);
  67. } else {
  68. assert(0);
  69. }
  70. avctx->extradata = extradata;
  71. s->use_exp_vlc = flags2 & 0x0001;
  72. s->use_bit_reservoir = flags2 & 0x0002;
  73. s->use_variable_block_len = flags2 & 0x0004;
  74. if (avctx->channels == 2)
  75. s->ms_stereo = 1;
  76. ff_wma_init(avctx, flags2);
  77. /* init MDCT */
  78. for (i = 0; i < s->nb_block_sizes; i++)
  79. ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 0, 1.0);
  80. block_align = avctx->bit_rate * (int64_t) s->frame_len /
  81. (avctx->sample_rate * 8);
  82. block_align = FFMIN(block_align, MAX_CODED_SUPERFRAME_SIZE);
  83. avctx->block_align = block_align;
  84. avctx->bit_rate = avctx->block_align * 8LL * avctx->sample_rate /
  85. s->frame_len;
  86. avctx->frame_size = avctx->initial_padding = s->frame_len;
  87. return 0;
  88. }
  89. static void apply_window_and_mdct(AVCodecContext *avctx, const AVFrame *frame)
  90. {
  91. WMACodecContext *s = avctx->priv_data;
  92. float **audio = (float **) frame->extended_data;
  93. int len = frame->nb_samples;
  94. int window_index = s->frame_len_bits - s->block_len_bits;
  95. FFTContext *mdct = &s->mdct_ctx[window_index];
  96. int ch;
  97. const float *win = s->windows[window_index];
  98. int window_len = 1 << s->block_len_bits;
  99. float n = 2.0 * 32768.0 / window_len;
  100. for (ch = 0; ch < avctx->channels; ch++) {
  101. memcpy(s->output, s->frame_out[ch], window_len * sizeof(*s->output));
  102. s->fdsp.vector_fmul_scalar(s->frame_out[ch], audio[ch], n, len);
  103. s->fdsp.vector_fmul_reverse(&s->output[window_len], s->frame_out[ch],
  104. win, len);
  105. s->fdsp.vector_fmul(s->frame_out[ch], s->frame_out[ch], win, len);
  106. mdct->mdct_calc(mdct, s->coefs[ch], s->output);
  107. }
  108. }
  109. // FIXME use for decoding too
  110. static void init_exp(WMACodecContext *s, int ch, const int *exp_param)
  111. {
  112. int n;
  113. const uint16_t *ptr;
  114. float v, *q, max_scale, *q_end;
  115. ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
  116. q = s->exponents[ch];
  117. q_end = q + s->block_len;
  118. max_scale = 0;
  119. while (q < q_end) {
  120. /* XXX: use a table */
  121. v = pow(10, *exp_param++ *(1.0 / 16.0));
  122. max_scale = FFMAX(max_scale, v);
  123. n = *ptr++;
  124. do {
  125. *q++ = v;
  126. } while (--n);
  127. }
  128. s->max_exponent[ch] = max_scale;
  129. }
  130. static void encode_exp_vlc(WMACodecContext *s, int ch, const int *exp_param)
  131. {
  132. int last_exp;
  133. const uint16_t *ptr;
  134. float *q, *q_end;
  135. ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
  136. q = s->exponents[ch];
  137. q_end = q + s->block_len;
  138. if (s->version == 1) {
  139. last_exp = *exp_param++;
  140. assert(last_exp - 10 >= 0 && last_exp - 10 < 32);
  141. put_bits(&s->pb, 5, last_exp - 10);
  142. q += *ptr++;
  143. } else
  144. last_exp = 36;
  145. while (q < q_end) {
  146. int exp = *exp_param++;
  147. int code = exp - last_exp + 60;
  148. assert(code >= 0 && code < 120);
  149. put_bits(&s->pb, ff_aac_scalefactor_bits[code],
  150. ff_aac_scalefactor_code[code]);
  151. /* XXX: use a table */
  152. q += *ptr++;
  153. last_exp = exp;
  154. }
  155. }
  156. static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
  157. int total_gain)
  158. {
  159. int v, bsize, ch, coef_nb_bits, parse_exponents;
  160. float mdct_norm;
  161. int nb_coefs[MAX_CHANNELS];
  162. static const int fixed_exp[25] = {
  163. 20, 20, 20, 20, 20,
  164. 20, 20, 20, 20, 20,
  165. 20, 20, 20, 20, 20,
  166. 20, 20, 20, 20, 20,
  167. 20, 20, 20, 20, 20
  168. };
  169. // FIXME remove duplication relative to decoder
  170. if (s->use_variable_block_len) {
  171. assert(0); // FIXME not implemented
  172. } else {
  173. /* fixed block len */
  174. s->next_block_len_bits = s->frame_len_bits;
  175. s->prev_block_len_bits = s->frame_len_bits;
  176. s->block_len_bits = s->frame_len_bits;
  177. }
  178. s->block_len = 1 << s->block_len_bits;
  179. // assert((s->block_pos + s->block_len) <= s->frame_len);
  180. bsize = s->frame_len_bits - s->block_len_bits;
  181. // FIXME factor
  182. v = s->coefs_end[bsize] - s->coefs_start;
  183. for (ch = 0; ch < s->avctx->channels; ch++)
  184. nb_coefs[ch] = v;
  185. {
  186. int n4 = s->block_len / 2;
  187. mdct_norm = 1.0 / (float) n4;
  188. if (s->version == 1)
  189. mdct_norm *= sqrt(n4);
  190. }
  191. if (s->avctx->channels == 2)
  192. put_bits(&s->pb, 1, !!s->ms_stereo);
  193. for (ch = 0; ch < s->avctx->channels; ch++) {
  194. // FIXME only set channel_coded when needed, instead of always
  195. s->channel_coded[ch] = 1;
  196. if (s->channel_coded[ch])
  197. init_exp(s, ch, fixed_exp);
  198. }
  199. for (ch = 0; ch < s->avctx->channels; ch++) {
  200. if (s->channel_coded[ch]) {
  201. WMACoef *coefs1;
  202. float *coefs, *exponents, mult;
  203. int i, n;
  204. coefs1 = s->coefs1[ch];
  205. exponents = s->exponents[ch];
  206. mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
  207. mult *= mdct_norm;
  208. coefs = src_coefs[ch];
  209. if (s->use_noise_coding && 0) {
  210. assert(0); // FIXME not implemented
  211. } else {
  212. coefs += s->coefs_start;
  213. n = nb_coefs[ch];
  214. for (i = 0; i < n; i++) {
  215. double t = *coefs++ / (exponents[i] * mult);
  216. if (t < -32768 || t > 32767)
  217. return -1;
  218. coefs1[i] = lrint(t);
  219. }
  220. }
  221. }
  222. }
  223. v = 0;
  224. for (ch = 0; ch < s->avctx->channels; ch++) {
  225. int a = s->channel_coded[ch];
  226. put_bits(&s->pb, 1, a);
  227. v |= a;
  228. }
  229. if (!v)
  230. return 1;
  231. for (v = total_gain - 1; v >= 127; v -= 127)
  232. put_bits(&s->pb, 7, 127);
  233. put_bits(&s->pb, 7, v);
  234. coef_nb_bits = ff_wma_total_gain_to_bits(total_gain);
  235. if (s->use_noise_coding) {
  236. for (ch = 0; ch < s->avctx->channels; ch++) {
  237. if (s->channel_coded[ch]) {
  238. int i, n;
  239. n = s->exponent_high_sizes[bsize];
  240. for (i = 0; i < n; i++) {
  241. put_bits(&s->pb, 1, s->high_band_coded[ch][i] = 0);
  242. if (0)
  243. nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
  244. }
  245. }
  246. }
  247. }
  248. parse_exponents = 1;
  249. if (s->block_len_bits != s->frame_len_bits)
  250. put_bits(&s->pb, 1, parse_exponents);
  251. if (parse_exponents) {
  252. for (ch = 0; ch < s->avctx->channels; ch++) {
  253. if (s->channel_coded[ch]) {
  254. if (s->use_exp_vlc) {
  255. encode_exp_vlc(s, ch, fixed_exp);
  256. } else {
  257. assert(0); // FIXME not implemented
  258. // encode_exp_lsp(s, ch);
  259. }
  260. }
  261. }
  262. } else
  263. assert(0); // FIXME not implemented
  264. for (ch = 0; ch < s->avctx->channels; ch++) {
  265. if (s->channel_coded[ch]) {
  266. int run, tindex;
  267. WMACoef *ptr, *eptr;
  268. tindex = (ch == 1 && s->ms_stereo);
  269. ptr = &s->coefs1[ch][0];
  270. eptr = ptr + nb_coefs[ch];
  271. run = 0;
  272. for (; ptr < eptr; ptr++) {
  273. if (*ptr) {
  274. int level = *ptr;
  275. int abs_level = FFABS(level);
  276. int code = 0;
  277. if (abs_level <= s->coef_vlcs[tindex]->max_level)
  278. if (run < s->coef_vlcs[tindex]->levels[abs_level - 1])
  279. code = run + s->int_table[tindex][abs_level - 1];
  280. assert(code < s->coef_vlcs[tindex]->n);
  281. put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[code],
  282. s->coef_vlcs[tindex]->huffcodes[code]);
  283. if (code == 0) {
  284. if (1 << coef_nb_bits <= abs_level)
  285. return -1;
  286. put_bits(&s->pb, coef_nb_bits, abs_level);
  287. put_bits(&s->pb, s->frame_len_bits, run);
  288. }
  289. // FIXME the sign is flipped somewhere
  290. put_bits(&s->pb, 1, level < 0);
  291. run = 0;
  292. } else
  293. run++;
  294. }
  295. if (run)
  296. put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[1],
  297. s->coef_vlcs[tindex]->huffcodes[1]);
  298. }
  299. if (s->version == 1 && s->avctx->channels >= 2)
  300. avpriv_align_put_bits(&s->pb);
  301. }
  302. return 0;
  303. }
  304. static int encode_frame(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
  305. uint8_t *buf, int buf_size, int total_gain)
  306. {
  307. init_put_bits(&s->pb, buf, buf_size);
  308. if (s->use_bit_reservoir)
  309. assert(0); // FIXME not implemented
  310. else if (encode_block(s, src_coefs, total_gain) < 0)
  311. return INT_MAX;
  312. avpriv_align_put_bits(&s->pb);
  313. return put_bits_count(&s->pb) / 8 - s->avctx->block_align;
  314. }
  315. static int encode_superframe(AVCodecContext *avctx, AVPacket *avpkt,
  316. const AVFrame *frame, int *got_packet_ptr)
  317. {
  318. WMACodecContext *s = avctx->priv_data;
  319. int i, total_gain, ret;
  320. s->block_len_bits = s->frame_len_bits; // required by non variable block len
  321. s->block_len = 1 << s->block_len_bits;
  322. apply_window_and_mdct(avctx, frame);
  323. if (s->ms_stereo) {
  324. float a, b;
  325. int i;
  326. for (i = 0; i < s->block_len; i++) {
  327. a = s->coefs[0][i] * 0.5;
  328. b = s->coefs[1][i] * 0.5;
  329. s->coefs[0][i] = a + b;
  330. s->coefs[1][i] = a - b;
  331. }
  332. }
  333. if ((ret = ff_alloc_packet(avpkt, 2 * MAX_CODED_SUPERFRAME_SIZE))) {
  334. av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
  335. return ret;
  336. }
  337. total_gain = 128;
  338. for (i = 64; i; i >>= 1) {
  339. int error = encode_frame(s, s->coefs, avpkt->data, avpkt->size,
  340. total_gain - i);
  341. if (error < 0)
  342. total_gain -= i;
  343. }
  344. if ((i = encode_frame(s, s->coefs, avpkt->data, avpkt->size, total_gain)) >= 0) {
  345. av_log(avctx, AV_LOG_ERROR, "required frame size too large. please "
  346. "use a higher bit rate.\n");
  347. return AVERROR(EINVAL);
  348. }
  349. assert((put_bits_count(&s->pb) & 7) == 0);
  350. while (i++)
  351. put_bits(&s->pb, 8, 'N');
  352. flush_put_bits(&s->pb);
  353. if (frame->pts != AV_NOPTS_VALUE)
  354. avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
  355. avpkt->size = avctx->block_align;
  356. *got_packet_ptr = 1;
  357. return 0;
  358. }
  359. AVCodec ff_wmav1_encoder = {
  360. .name = "wmav1",
  361. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),
  362. .type = AVMEDIA_TYPE_AUDIO,
  363. .id = AV_CODEC_ID_WMAV1,
  364. .priv_data_size = sizeof(WMACodecContext),
  365. .init = encode_init,
  366. .encode2 = encode_superframe,
  367. .close = ff_wma_end,
  368. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
  369. AV_SAMPLE_FMT_NONE },
  370. };
  371. AVCodec ff_wmav2_encoder = {
  372. .name = "wmav2",
  373. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),
  374. .type = AVMEDIA_TYPE_AUDIO,
  375. .id = AV_CODEC_ID_WMAV2,
  376. .priv_data_size = sizeof(WMACodecContext),
  377. .init = encode_init,
  378. .encode2 = encode_superframe,
  379. .close = ff_wma_end,
  380. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
  381. AV_SAMPLE_FMT_NONE },
  382. };