]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/wma.c
lavc: Add option to encode MPEG-2 AAC with libfdk-aac
[ffmpeg] / libavcodec / wma.c
index 371aad574d3bb5a6df38dfe8bdfe192fe8bbb9a3..db276461a735dfaaf2608075006b9cd965981d46 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/attributes.h"
 #include "avcodec.h"
 #include "sinewin.h"
 #include "wma.h"
+#include "wma_common.h"
 #include "wmadata.h"
 
 #undef NDEBUG
@@ -29,9 +31,9 @@
 
 /* XXX: use same run/length optimization as mpeg decoders */
 //FIXME maybe split decode / encode or pass flag
-static void init_coef_vlc(VLC *vlc, uint16_t **prun_table,
-                          float **plevel_table, uint16_t **pint_table,
-                          const CoefVLCTable *vlc_table)
+static av_cold void init_coef_vlc(VLC *vlc, uint16_t **prun_table,
+                                  float **plevel_table, uint16_t **pint_table,
+                                  const CoefVLCTable *vlc_table)
 {
     int n = vlc_table->n;
     const uint8_t  *table_bits   = vlc_table->huffbits;
@@ -67,47 +69,7 @@ static void init_coef_vlc(VLC *vlc, uint16_t **prun_table,
     av_free(level_table);
 }
 
-/**
- *@brief Get the samples per frame for this stream.
- *@param sample_rate output sample_rate
- *@param version wma version
- *@param decode_flags codec compression features
- *@return log2 of the number of output samples per frame
- */
-int av_cold ff_wma_get_frame_len_bits(int sample_rate, int version,
-                                      unsigned int decode_flags)
-{
-
-    int frame_len_bits;
-
-    if (sample_rate <= 16000) {
-        frame_len_bits = 9;
-    } else if (sample_rate <= 22050 ||
-             (sample_rate <= 32000 && version == 1)) {
-        frame_len_bits = 10;
-    } else if (sample_rate <= 48000 || version < 3) {
-        frame_len_bits = 11;
-    } else if (sample_rate <= 96000) {
-        frame_len_bits = 12;
-    } else {
-        frame_len_bits = 13;
-    }
-
-    if (version == 3) {
-        int tmp = decode_flags & 0x6;
-        if (tmp == 0x2) {
-            ++frame_len_bits;
-        } else if (tmp == 0x4) {
-            --frame_len_bits;
-        } else if (tmp == 0x6) {
-            frame_len_bits -= 2;
-        }
-    }
-
-    return frame_len_bits;
-}
-
-int ff_wma_init(AVCodecContext *avctx, int flags2)
+av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
 {
     WMACodecContext *s = avctx->priv_data;
     int i;
@@ -117,26 +79,22 @@ int ff_wma_init(AVCodecContext *avctx, int flags2)
     int coef_vlc_table;
 
     if (   avctx->sample_rate <= 0 || avctx->sample_rate > 50000
-        || avctx->channels    <= 0 || avctx->channels    > 8
+        || avctx->channels    <= 0 || avctx->channels    > 2
         || avctx->bit_rate    <= 0)
         return -1;
 
-    s->sample_rate = avctx->sample_rate;
-    s->nb_channels = avctx->channels;
-    s->bit_rate    = avctx->bit_rate;
-    s->block_align = avctx->block_align;
-
-    ff_dsputil_init(&s->dsp, avctx);
     ff_fmt_convert_init(&s->fmt_conv, avctx);
+    avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
 
-    if (avctx->codec->id == CODEC_ID_WMAV1) {
+    if (avctx->codec->id == AV_CODEC_ID_WMAV1) {
         s->version = 1;
     } else {
         s->version = 2;
     }
 
     /* compute MDCT block size */
-    s->frame_len_bits = ff_wma_get_frame_len_bits(s->sample_rate, s->version, 0);
+    s->frame_len_bits = ff_wma_get_frame_len_bits(avctx->sample_rate,
+                                                  s->version, 0);
     s->next_block_len_bits = s->frame_len_bits;
     s->prev_block_len_bits = s->frame_len_bits;
     s->block_len_bits      = s->frame_len_bits;
@@ -145,7 +103,7 @@ int ff_wma_init(AVCodecContext *avctx, int flags2)
     if (s->use_variable_block_len) {
         int nb_max, nb;
         nb = ((flags2 >> 3) & 3) + 1;
-        if ((s->bit_rate / s->nb_channels) >= 32000)
+        if ((avctx->bit_rate / avctx->channels) >= 32000)
             nb += 2;
         nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
         if (nb > nb_max)
@@ -157,10 +115,10 @@ int ff_wma_init(AVCodecContext *avctx, int flags2)
 
     /* init rate dependent parameters */
     s->use_noise_coding = 1;
-    high_freq = s->sample_rate * 0.5;
+    high_freq = avctx->sample_rate * 0.5;
 
     /* if version 2, then the rates are normalized */
-    sample_rate1 = s->sample_rate;
+    sample_rate1 = avctx->sample_rate;
     if (s->version == 2) {
         if (sample_rate1 >= 44100) {
             sample_rate1 = 44100;
@@ -175,13 +133,13 @@ int ff_wma_init(AVCodecContext *avctx, int flags2)
         }
     }
 
-    bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate);
+    bps = (float)avctx->bit_rate / (float)(avctx->channels * avctx->sample_rate);
     s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0 + 0.5)) + 2;
 
     /* compute high frequency value and choose if noise coding should
        be activated */
     bps1 = bps;
-    if (s->nb_channels == 2)
+    if (avctx->channels == 2)
         bps1 = bps * 1.6;
     if (sample_rate1 == 44100) {
         if (bps1 >= 0.61) {
@@ -224,8 +182,8 @@ int ff_wma_init(AVCodecContext *avctx, int flags2)
     }
     av_dlog(s->avctx, "flags2=0x%x\n", flags2);
     av_dlog(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
-            s->version, s->nb_channels, s->sample_rate, s->bit_rate,
-            s->block_align);
+            s->version, avctx->channels, avctx->sample_rate, avctx->bit_rate,
+            avctx->block_align);
     av_dlog(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
             bps, bps1, high_freq, s->byte_offset_bits);
     av_dlog(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
@@ -248,7 +206,7 @@ int ff_wma_init(AVCodecContext *avctx, int flags2)
                 lpos = 0;
                 for (i = 0; i < 25; i++) {
                     a = ff_wma_critical_freqs[i];
-                    b = s->sample_rate;
+                    b = avctx->sample_rate;
                     pos = ((block_len * 2 * a) + (b >> 1)) / b;
                     if (pos > block_len)
                         pos = block_len;
@@ -265,11 +223,11 @@ int ff_wma_init(AVCodecContext *avctx, int flags2)
                 table = NULL;
                 a = s->frame_len_bits - BLOCK_MIN_BITS - k;
                 if (a < 3) {
-                    if (s->sample_rate >= 44100) {
+                    if (avctx->sample_rate >= 44100) {
                         table = exponent_band_44100[a];
-                    } else if (s->sample_rate >= 32000) {
+                    } else if (avctx->sample_rate >= 32000) {
                         table = exponent_band_32000[a];
-                    } else if (s->sample_rate >= 22050) {
+                    } else if (avctx->sample_rate >= 22050) {
                         table = exponent_band_22050[a];
                     }
                 }
@@ -283,7 +241,7 @@ int ff_wma_init(AVCodecContext *avctx, int flags2)
                     lpos = 0;
                     for (i = 0; i < 25; i++) {
                         a = ff_wma_critical_freqs[i];
-                        b = s->sample_rate;
+                        b = avctx->sample_rate;
                         pos = ((block_len * 2 * a) + (b << 1)) / (4 * b);
                         pos <<= 2;
                         if (pos > block_len)
@@ -302,7 +260,7 @@ int ff_wma_init(AVCodecContext *avctx, int flags2)
             s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
             /* high freq computation */
             s->high_band_start[k] = (int)((block_len * 2 * high_freq) /
-                                          s->sample_rate + 0.5);
+                                          avctx->sample_rate + 0.5);
             n = s->exponent_sizes[k];
             j = 0;
             pos = 0;
@@ -382,7 +340,7 @@ int ff_wma_init(AVCodecContext *avctx, int flags2)
 
     /* choose the VLC tables for the coefficients */
     coef_vlc_table = 2;
-    if (s->sample_rate >= 32000) {
+    if (avctx->sample_rate >= 32000) {
         if (bps1 < 0.72) {
             coef_vlc_table = 0;
         } else if (bps1 < 1.16) {