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.

416 lines
13KB

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