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.

439 lines
14KB

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