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.

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