wmaenc.c 12 KB

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