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.

406 lines
12KB

  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. if ((s->channel_coded[ch]= 1)) { //FIXME only set channel_coded when needed, instead of always
  157. init_exp(s, ch, fixed_exp);
  158. }
  159. }
  160. for(ch = 0; ch < s->nb_channels; ch++) {
  161. if (s->channel_coded[ch]) {
  162. int16_t *coefs1;
  163. float *coefs, *exponents, mult;
  164. int i, n;
  165. coefs1 = s->coefs1[ch];
  166. exponents = s->exponents[ch];
  167. mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
  168. mult *= mdct_norm;
  169. coefs = src_coefs[ch];
  170. if (s->use_noise_coding && 0) {
  171. assert(0); //FIXME not implemented
  172. } else {
  173. coefs += s->coefs_start;
  174. n = nb_coefs[ch];
  175. for(i = 0;i < n; i++){
  176. double t= *coefs++ / (exponents[i] * mult);
  177. if(t<-32768 || t>32767)
  178. return -1;
  179. coefs1[i] = lrint(t);
  180. }
  181. }
  182. }
  183. }
  184. v = 0;
  185. for(ch = 0; ch < s->nb_channels; ch++) {
  186. int a = s->channel_coded[ch];
  187. put_bits(&s->pb, 1, a);
  188. v |= a;
  189. }
  190. if (!v)
  191. return 1;
  192. for(v= total_gain-1; v>=127; v-= 127)
  193. put_bits(&s->pb, 7, 127);
  194. put_bits(&s->pb, 7, v);
  195. coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);
  196. if (s->use_noise_coding) {
  197. for(ch = 0; ch < s->nb_channels; ch++) {
  198. if (s->channel_coded[ch]) {
  199. int i, n;
  200. n = s->exponent_high_sizes[bsize];
  201. for(i=0;i<n;i++) {
  202. put_bits(&s->pb, 1, s->high_band_coded[ch][i]= 0);
  203. if (0)
  204. nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
  205. }
  206. }
  207. }
  208. }
  209. parse_exponents = 1;
  210. if (s->block_len_bits != s->frame_len_bits) {
  211. put_bits(&s->pb, 1, parse_exponents);
  212. }
  213. if (parse_exponents) {
  214. for(ch = 0; ch < s->nb_channels; ch++) {
  215. if (s->channel_coded[ch]) {
  216. if (s->use_exp_vlc) {
  217. encode_exp_vlc(s, ch, fixed_exp);
  218. } else {
  219. assert(0); //FIXME not implemented
  220. // encode_exp_lsp(s, ch);
  221. }
  222. }
  223. }
  224. } else {
  225. assert(0); //FIXME not implemented
  226. }
  227. for(ch = 0; ch < s->nb_channels; ch++) {
  228. if (s->channel_coded[ch]) {
  229. int run, tindex;
  230. int16_t *ptr, *eptr;
  231. tindex = (ch == 1 && s->ms_stereo);
  232. ptr = &s->coefs1[ch][0];
  233. eptr = ptr + nb_coefs[ch];
  234. run=0;
  235. for(;ptr < eptr; ptr++){
  236. if(*ptr){
  237. int level= *ptr;
  238. int abs_level= FFABS(level);
  239. int code= 0;
  240. if(abs_level <= s->coef_vlcs[tindex]->max_level){
  241. if(run < s->coef_vlcs[tindex]->levels[abs_level-1])
  242. code= run + s->int_table[tindex][abs_level-1];
  243. }
  244. assert(code < s->coef_vlcs[tindex]->n);
  245. put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[code], s->coef_vlcs[tindex]->huffcodes[code]);
  246. if(code == 0){
  247. if(1<<coef_nb_bits <= abs_level)
  248. return -1;
  249. put_bits(&s->pb, coef_nb_bits, abs_level);
  250. put_bits(&s->pb, s->frame_len_bits, run);
  251. }
  252. put_bits(&s->pb, 1, level < 0); //FIXME the sign is fliped somewhere
  253. run=0;
  254. }else{
  255. run++;
  256. }
  257. }
  258. if(run)
  259. put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[1], s->coef_vlcs[tindex]->huffcodes[1]);
  260. }
  261. if (s->version == 1 && s->nb_channels >= 2) {
  262. align_put_bits(&s->pb);
  263. }
  264. }
  265. return 0;
  266. }
  267. static int encode_frame(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE], uint8_t *buf, int buf_size, int total_gain){
  268. init_put_bits(&s->pb, buf, buf_size);
  269. if (s->use_bit_reservoir) {
  270. assert(0);//FIXME not implemented
  271. }else{
  272. if(encode_block(s, src_coefs, total_gain) < 0)
  273. return INT_MAX;
  274. }
  275. align_put_bits(&s->pb);
  276. return put_bits_count(&s->pb)/8 - s->block_align;
  277. }
  278. static int encode_superframe(AVCodecContext *avctx,
  279. unsigned char *buf, int buf_size, void *data){
  280. WMACodecContext *s = avctx->priv_data;
  281. short *samples = data;
  282. int i, total_gain;
  283. s->block_len_bits= s->frame_len_bits; //required by non variable block len
  284. s->block_len = 1 << s->block_len_bits;
  285. apply_window_and_mdct(avctx, samples, avctx->frame_size);
  286. if (s->ms_stereo) {
  287. float a, b;
  288. int i;
  289. for(i = 0; i < s->block_len; i++) {
  290. a = s->coefs[0][i]*0.5;
  291. b = s->coefs[1][i]*0.5;
  292. s->coefs[0][i] = a + b;
  293. s->coefs[1][i] = a - b;
  294. }
  295. }
  296. #if 1
  297. total_gain= 128;
  298. for(i=64; i; i>>=1){
  299. int error= encode_frame(s, s->coefs, buf, buf_size, total_gain-i);
  300. if(error<0)
  301. total_gain-= i;
  302. }
  303. #else
  304. total_gain= 90;
  305. best= encode_frame(s, s->coefs, buf, buf_size, total_gain);
  306. for(i=32; i; i>>=1){
  307. int scoreL= encode_frame(s, s->coefs, buf, buf_size, total_gain-i);
  308. int scoreR= encode_frame(s, s->coefs, buf, buf_size, total_gain+i);
  309. av_log(NULL, AV_LOG_ERROR, "%d %d %d (%d)\n", scoreL, best, scoreR, total_gain);
  310. if(scoreL < FFMIN(best, scoreR)){
  311. best = scoreL;
  312. total_gain -= i;
  313. }else if(scoreR < best){
  314. best = scoreR;
  315. total_gain += i;
  316. }
  317. }
  318. #endif
  319. encode_frame(s, s->coefs, buf, buf_size, total_gain);
  320. assert((put_bits_count(&s->pb) & 7) == 0);
  321. i= s->block_align - (put_bits_count(&s->pb)+7)/8;
  322. assert(i>=0);
  323. while(i--)
  324. put_bits(&s->pb, 8, 'N');
  325. flush_put_bits(&s->pb);
  326. return pbBufPtr(&s->pb) - s->pb.buf;
  327. }
  328. AVCodec wmav1_encoder =
  329. {
  330. "wmav1",
  331. CODEC_TYPE_AUDIO,
  332. CODEC_ID_WMAV1,
  333. sizeof(WMACodecContext),
  334. encode_init,
  335. encode_superframe,
  336. ff_wma_end,
  337. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  338. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),
  339. };
  340. AVCodec wmav2_encoder =
  341. {
  342. "wmav2",
  343. CODEC_TYPE_AUDIO,
  344. CODEC_ID_WMAV2,
  345. sizeof(WMACodecContext),
  346. encode_init,
  347. encode_superframe,
  348. ff_wma_end,
  349. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  350. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),
  351. };