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.

454 lines
15KB

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