LCOV - code coverage report
Current view: top level - libavcodec - wma.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 223 262 85.1 %
Date: 2017-12-17 04:34:43 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :  * WMA compatible codec
       3             :  * Copyright (c) 2002-2007 The FFmpeg Project
       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             : 
      22             : #include "libavutil/attributes.h"
      23             : 
      24             : #include "avcodec.h"
      25             : #include "internal.h"
      26             : #include "sinewin.h"
      27             : #include "wma.h"
      28             : #include "wma_common.h"
      29             : #include "wma_freqs.h"
      30             : #include "wmadata.h"
      31             : 
      32             : /* XXX: use same run/length optimization as mpeg decoders */
      33             : // FIXME maybe split decode / encode or pass flag
      34          32 : static av_cold int init_coef_vlc(VLC *vlc, uint16_t **prun_table,
      35             :                                  float **plevel_table, uint16_t **pint_table,
      36             :                                  const CoefVLCTable *vlc_table)
      37             : {
      38          32 :     int n                        = vlc_table->n;
      39          32 :     const uint8_t  *table_bits   = vlc_table->huffbits;
      40          32 :     const uint32_t *table_codes  = vlc_table->huffcodes;
      41          32 :     const uint16_t *levels_table = vlc_table->levels;
      42             :     uint16_t *run_table, *level_table, *int_table;
      43             :     float *flevel_table;
      44             :     int i, l, j, k, level;
      45             : 
      46          32 :     init_vlc(vlc, VLCBITS, n, table_bits, 1, 1, table_codes, 4, 4, 0);
      47             : 
      48          32 :     run_table    = av_malloc_array(n, sizeof(uint16_t));
      49          32 :     level_table  = av_malloc_array(n, sizeof(uint16_t));
      50          32 :     flevel_table = av_malloc_array(n, sizeof(*flevel_table));
      51          32 :     int_table    = av_malloc_array(n, sizeof(uint16_t));
      52          32 :     if (!run_table || !level_table || !flevel_table || !int_table) {
      53           0 :         av_freep(&run_table);
      54           0 :         av_freep(&level_table);
      55           0 :         av_freep(&flevel_table);
      56           0 :         av_freep(&int_table);
      57           0 :         return AVERROR(ENOMEM);
      58             :     }
      59          32 :     i            = 2;
      60          32 :     level        = 1;
      61          32 :     k            = 0;
      62        2644 :     while (i < n) {
      63        2580 :         int_table[k] = i;
      64        2580 :         l            = levels_table[k++];
      65       20086 :         for (j = 0; j < l; j++) {
      66       17506 :             run_table[i]    = j;
      67       17506 :             level_table[i]  = level;
      68       17506 :             flevel_table[i] = level;
      69       17506 :             i++;
      70             :         }
      71        2580 :         level++;
      72             :     }
      73          32 :     *prun_table   = run_table;
      74          32 :     *plevel_table = flevel_table;
      75          32 :     *pint_table   = int_table;
      76          32 :     av_free(level_table);
      77             : 
      78          32 :     return 0;
      79             : }
      80             : 
      81          16 : av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
      82             : {
      83          16 :     WMACodecContext *s = avctx->priv_data;
      84             :     int i, ret;
      85             :     float bps1, high_freq;
      86             :     volatile float bps;
      87             :     int sample_rate1;
      88             :     int coef_vlc_table;
      89             : 
      90          32 :     if (avctx->sample_rate <= 0 || avctx->sample_rate > 50000 ||
      91          48 :         avctx->channels    <= 0 || avctx->channels    > 2     ||
      92          16 :         avctx->bit_rate    <= 0)
      93           0 :         return -1;
      94             : 
      95             : 
      96          16 :     if (avctx->codec->id == AV_CODEC_ID_WMAV1)
      97           3 :         s->version = 1;
      98             :     else
      99          13 :         s->version = 2;
     100             : 
     101             :     /* compute MDCT block size */
     102          16 :     s->frame_len_bits = ff_wma_get_frame_len_bits(avctx->sample_rate,
     103             :                                                   s->version, 0);
     104          16 :     s->next_block_len_bits = s->frame_len_bits;
     105          16 :     s->prev_block_len_bits = s->frame_len_bits;
     106          16 :     s->block_len_bits      = s->frame_len_bits;
     107             : 
     108          16 :     s->frame_len = 1 << s->frame_len_bits;
     109          16 :     if (s->use_variable_block_len) {
     110             :         int nb_max, nb;
     111           7 :         nb = ((flags2 >> 3) & 3) + 1;
     112           7 :         if ((avctx->bit_rate / avctx->channels) >= 32000)
     113           4 :             nb += 2;
     114           7 :         nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
     115           7 :         if (nb > nb_max)
     116           0 :             nb = nb_max;
     117           7 :         s->nb_block_sizes = nb + 1;
     118             :     } else
     119           9 :         s->nb_block_sizes = 1;
     120             : 
     121             :     /* init rate dependent parameters */
     122          16 :     s->use_noise_coding = 1;
     123          16 :     high_freq           = avctx->sample_rate * 0.5;
     124             : 
     125             :     /* if version 2, then the rates are normalized */
     126          16 :     sample_rate1 = avctx->sample_rate;
     127          16 :     if (s->version == 2) {
     128          13 :         if (sample_rate1 >= 44100)
     129          10 :             sample_rate1 = 44100;
     130           3 :         else if (sample_rate1 >= 22050)
     131           0 :             sample_rate1 = 22050;
     132           3 :         else if (sample_rate1 >= 16000)
     133           1 :             sample_rate1 = 16000;
     134           2 :         else if (sample_rate1 >= 11025)
     135           0 :             sample_rate1 = 11025;
     136           2 :         else if (sample_rate1 >= 8000)
     137           2 :             sample_rate1 = 8000;
     138             :     }
     139             : 
     140          32 :     bps                 = (float) avctx->bit_rate /
     141          16 :                           (float) (avctx->channels * avctx->sample_rate);
     142          16 :     s->byte_offset_bits = av_log2((int) (bps * s->frame_len / 8.0 + 0.5)) + 2;
     143          16 :     if (s->byte_offset_bits + 3 > MIN_CACHE_BITS) {
     144           0 :         av_log(avctx, AV_LOG_ERROR, "byte_offset_bits %d is too large\n", s->byte_offset_bits);
     145           0 :         return AVERROR_PATCHWELCOME;
     146             :     }
     147             : 
     148             :     /* compute high frequency value and choose if noise coding should
     149             :      * be activated */
     150          16 :     bps1 = bps;
     151          16 :     if (avctx->channels == 2)
     152          12 :         bps1 = bps * 1.6;
     153          16 :     if (sample_rate1 == 44100) {
     154          13 :         if (bps1 >= 0.61)
     155          13 :             s->use_noise_coding = 0;
     156             :         else
     157           0 :             high_freq = high_freq * 0.4;
     158           3 :     } else if (sample_rate1 == 22050) {
     159           0 :         if (bps1 >= 1.16)
     160           0 :             s->use_noise_coding = 0;
     161           0 :         else if (bps1 >= 0.72)
     162           0 :             high_freq = high_freq * 0.7;
     163             :         else
     164           0 :             high_freq = high_freq * 0.6;
     165           3 :     } else if (sample_rate1 == 16000) {
     166           1 :         if (bps > 0.5)
     167           1 :             high_freq = high_freq * 0.5;
     168             :         else
     169           0 :             high_freq = high_freq * 0.3;
     170           2 :     } else if (sample_rate1 == 11025)
     171           0 :         high_freq = high_freq * 0.7;
     172           2 :     else if (sample_rate1 == 8000) {
     173           2 :         if (bps <= 0.625)
     174           1 :             high_freq = high_freq * 0.5;
     175           1 :         else if (bps > 0.75)
     176           1 :             s->use_noise_coding = 0;
     177             :         else
     178           0 :             high_freq = high_freq * 0.65;
     179             :     } else {
     180           0 :         if (bps >= 0.8)
     181           0 :             high_freq = high_freq * 0.75;
     182           0 :         else if (bps >= 0.6)
     183           0 :             high_freq = high_freq * 0.6;
     184             :         else
     185           0 :             high_freq = high_freq * 0.5;
     186             :     }
     187             :     ff_dlog(s->avctx, "flags2=0x%x\n", flags2);
     188             :     ff_dlog(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%"PRId64" block_align=%d\n",
     189             :             s->version, avctx->channels, avctx->sample_rate, avctx->bit_rate,
     190             :             avctx->block_align);
     191             :     ff_dlog(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
     192             :             bps, bps1, high_freq, s->byte_offset_bits);
     193             :     ff_dlog(s->avctx, "use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
     194             :             s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
     195             : 
     196             :     /* compute the scale factor band sizes for each MDCT block size */
     197             :     {
     198             :         int a, b, pos, lpos, k, block_len, i, j, n;
     199             :         const uint8_t *table;
     200             : 
     201          16 :         if (s->version == 1)
     202           3 :             s->coefs_start = 3;
     203             :         else
     204          13 :             s->coefs_start = 0;
     205          55 :         for (k = 0; k < s->nb_block_sizes; k++) {
     206          39 :             block_len = s->frame_len >> k;
     207             : 
     208          39 :             if (s->version == 1) {
     209           3 :                 lpos = 0;
     210          75 :                 for (i = 0; i < 25; i++) {
     211          75 :                     a   = ff_wma_critical_freqs[i];
     212          75 :                     b   = avctx->sample_rate;
     213          75 :                     pos = ((block_len * 2 * a) + (b >> 1)) / b;
     214          75 :                     if (pos > block_len)
     215           3 :                         pos = block_len;
     216          75 :                     s->exponent_bands[0][i] = pos - lpos;
     217          75 :                     if (pos >= block_len) {
     218           3 :                         i++;
     219           3 :                         break;
     220             :                     }
     221          72 :                     lpos = pos;
     222             :                 }
     223           3 :                 s->exponent_sizes[0] = i;
     224             :             } else {
     225             :                 /* hardcoded tables */
     226          36 :                 table = NULL;
     227          36 :                 a     = s->frame_len_bits - BLOCK_MIN_BITS - k;
     228          36 :                 if (a < 3) {
     229          21 :                     if (avctx->sample_rate >= 44100)
     230          15 :                         table = exponent_band_44100[a];
     231           6 :                     else if (avctx->sample_rate >= 32000)
     232           0 :                         table = exponent_band_32000[a];
     233           6 :                     else if (avctx->sample_rate >= 22050)
     234           0 :                         table = exponent_band_22050[a];
     235             :                 }
     236          36 :                 if (table) {
     237          15 :                     n = *table++;
     238         235 :                     for (i = 0; i < n; i++)
     239         220 :                         s->exponent_bands[k][i] = table[i];
     240          15 :                     s->exponent_sizes[k] = n;
     241             :                 } else {
     242          21 :                     j    = 0;
     243          21 :                     lpos = 0;
     244         495 :                     for (i = 0; i < 25; i++) {
     245         495 :                         a     = ff_wma_critical_freqs[i];
     246         495 :                         b     = avctx->sample_rate;
     247         495 :                         pos   = ((block_len * 2 * a) + (b << 1)) / (4 * b);
     248         495 :                         pos <<= 2;
     249         495 :                         if (pos > block_len)
     250          21 :                             pos = block_len;
     251         495 :                         if (pos > lpos)
     252         489 :                             s->exponent_bands[k][j++] = pos - lpos;
     253         495 :                         if (pos >= block_len)
     254          21 :                             break;
     255         474 :                         lpos = pos;
     256             :                     }
     257          21 :                     s->exponent_sizes[k] = j;
     258             :                 }
     259             :             }
     260             : 
     261             :             /* max number of coefs */
     262          39 :             s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k;
     263             :             /* high freq computation */
     264         117 :             s->high_band_start[k] = (int) ((block_len * 2 * high_freq) /
     265          78 :                                            avctx->sample_rate + 0.5);
     266          39 :             n   = s->exponent_sizes[k];
     267          39 :             j   = 0;
     268          39 :             pos = 0;
     269         823 :             for (i = 0; i < n; i++) {
     270             :                 int start, end;
     271         784 :                 start = pos;
     272         784 :                 pos  += s->exponent_bands[k][i];
     273         784 :                 end   = pos;
     274         784 :                 if (start < s->high_band_start[k])
     275         767 :                     start = s->high_band_start[k];
     276         784 :                 if (end > s->coefs_end[k])
     277          45 :                     end = s->coefs_end[k];
     278         784 :                 if (end > start)
     279          16 :                     s->exponent_high_bands[k][j++] = end - start;
     280             :             }
     281          39 :             s->exponent_high_sizes[k] = j;
     282             :         }
     283             :     }
     284             : 
     285             : #ifdef TRACE
     286             :     {
     287             :         int i, j;
     288             :         for (i = 0; i < s->nb_block_sizes; i++) {
     289             :             ff_tlog(s->avctx, "%5d: n=%2d:",
     290             :                     s->frame_len >> i,
     291             :                     s->exponent_sizes[i]);
     292             :             for (j = 0; j < s->exponent_sizes[i]; j++)
     293             :                 ff_tlog(s->avctx, " %d", s->exponent_bands[i][j]);
     294             :             ff_tlog(s->avctx, "\n");
     295             :         }
     296             :     }
     297             : #endif /* TRACE */
     298             : 
     299             :     /* init MDCT windows : simple sine window */
     300          55 :     for (i = 0; i < s->nb_block_sizes; i++) {
     301          39 :         ff_init_ff_sine_windows(s->frame_len_bits - i);
     302          39 :         s->windows[i] = ff_sine_windows[s->frame_len_bits - i];
     303             :     }
     304             : 
     305          16 :     s->reset_block_lengths = 1;
     306             : 
     307          16 :     if (s->use_noise_coding) {
     308             :         /* init the noise generator */
     309           2 :         if (s->use_exp_vlc)
     310           0 :             s->noise_mult = 0.02;
     311             :         else
     312           2 :             s->noise_mult = 0.04;
     313             : 
     314             : #ifdef TRACE
     315             :         for (i = 0; i < NOISE_TAB_SIZE; i++)
     316             :             s->noise_table[i] = 1.0 * s->noise_mult;
     317             : #else
     318             :         {
     319             :             unsigned int seed;
     320             :             float norm;
     321           2 :             seed = 1;
     322           2 :             norm = (1.0 / (float) (1LL << 31)) * sqrt(3) * s->noise_mult;
     323       16386 :             for (i = 0; i < NOISE_TAB_SIZE; i++) {
     324       16384 :                 seed              = seed * 314159 + 1;
     325       16384 :                 s->noise_table[i] = (float) ((int) seed) * norm;
     326             :             }
     327             :         }
     328             : #endif /* TRACE */
     329             :     }
     330             : 
     331          16 :     s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
     332          16 :     if (!s->fdsp)
     333           0 :         return AVERROR(ENOMEM);
     334             : 
     335             :     /* choose the VLC tables for the coefficients */
     336          16 :     coef_vlc_table = 2;
     337          16 :     if (avctx->sample_rate >= 32000) {
     338          13 :         if (bps1 < 0.72)
     339           0 :             coef_vlc_table = 0;
     340          13 :         else if (bps1 < 1.16)
     341           2 :             coef_vlc_table = 1;
     342             :     }
     343          16 :     s->coef_vlcs[0] = &coef_vlcs[coef_vlc_table * 2];
     344          16 :     s->coef_vlcs[1] = &coef_vlcs[coef_vlc_table * 2 + 1];
     345          16 :     ret = init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0],
     346             :                         &s->int_table[0], s->coef_vlcs[0]);
     347          16 :     if (ret < 0)
     348           0 :         return ret;
     349             : 
     350          16 :     return init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1],
     351             :                          &s->int_table[1], s->coef_vlcs[1]);
     352             : }
     353             : 
     354        3650 : int ff_wma_total_gain_to_bits(int total_gain)
     355             : {
     356        3650 :     if (total_gain < 15)
     357           0 :         return 13;
     358        3650 :     else if (total_gain < 32)
     359          98 :         return 12;
     360        3552 :     else if (total_gain < 40)
     361         284 :         return 11;
     362        3268 :     else if (total_gain < 45)
     363          18 :         return 10;
     364             :     else
     365        3250 :         return  9;
     366             : }
     367             : 
     368          16 : int ff_wma_end(AVCodecContext *avctx)
     369             : {
     370          16 :     WMACodecContext *s = avctx->priv_data;
     371             :     int i;
     372             : 
     373          55 :     for (i = 0; i < s->nb_block_sizes; i++)
     374          39 :         ff_mdct_end(&s->mdct_ctx[i]);
     375             : 
     376          16 :     if (s->use_exp_vlc)
     377          13 :         ff_free_vlc(&s->exp_vlc);
     378          16 :     if (s->use_noise_coding)
     379           2 :         ff_free_vlc(&s->hgain_vlc);
     380          48 :     for (i = 0; i < 2; i++) {
     381          32 :         ff_free_vlc(&s->coef_vlc[i]);
     382          32 :         av_freep(&s->run_table[i]);
     383          32 :         av_freep(&s->level_table[i]);
     384          32 :         av_freep(&s->int_table[i]);
     385             :     }
     386          16 :     av_freep(&s->fdsp);
     387             : 
     388          16 :     return 0;
     389             : }
     390             : 
     391             : /**
     392             :  * Decode an uncompressed coefficient.
     393             :  * @param gb GetBitContext
     394             :  * @return the decoded coefficient
     395             :  */
     396        3915 : unsigned int ff_wma_get_large_val(GetBitContext *gb)
     397             : {
     398             :     /** consumes up to 34 bits */
     399        3915 :     int n_bits = 8;
     400             :     /** decode length */
     401        3915 :     if (get_bits1(gb)) {
     402         530 :         n_bits += 8;
     403         530 :         if (get_bits1(gb)) {
     404           0 :             n_bits += 8;
     405           0 :             if (get_bits1(gb))
     406           0 :                 n_bits += 7;
     407             :         }
     408             :     }
     409        3915 :     return get_bits_long(gb, n_bits);
     410             : }
     411             : 
     412             : /**
     413             :  * Decode run level compressed coefficients.
     414             :  * @param avctx codec context
     415             :  * @param gb bitstream reader context
     416             :  * @param vlc vlc table for get_vlc2
     417             :  * @param level_table level codes
     418             :  * @param run_table run codes
     419             :  * @param version 0 for wma1,2 1 for wmapro
     420             :  * @param ptr output buffer
     421             :  * @param offset offset in the output buffer
     422             :  * @param num_coefs number of input coefficients
     423             :  * @param block_len input buffer length (2^n)
     424             :  * @param frame_len_bits number of bits for escaped run codes
     425             :  * @param coef_nb_bits number of bits for escaped level codes
     426             :  * @return 0 on success, -1 otherwise
     427             :  */
     428        2174 : int ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb,
     429             :                             VLC *vlc, const float *level_table,
     430             :                             const uint16_t *run_table, int version,
     431             :                             WMACoef *ptr, int offset, int num_coefs,
     432             :                             int block_len, int frame_len_bits,
     433             :                             int coef_nb_bits)
     434             : {
     435             :     int code, level, sign;
     436        2174 :     const uint32_t *ilvl = (const uint32_t *) level_table;
     437        2174 :     uint32_t *iptr = (uint32_t *) ptr;
     438        2174 :     const unsigned int coef_mask = block_len - 1;
     439      592299 :     for (; offset < num_coefs; offset++) {
     440      592287 :         code = get_vlc2(gb, vlc->table, VLCBITS, VLCMAX);
     441      592287 :         if (code > 1) {
     442             :             /** normal code */
     443      586557 :             offset                  += run_table[code];
     444      586557 :             sign                     = get_bits1(gb) - 1;
     445      586557 :             iptr[offset & coef_mask] = ilvl[code] ^ (sign & 0x80000000);
     446        5730 :         } else if (code == 1) {
     447             :             /** EOB */
     448        2162 :             break;
     449             :         } else {
     450             :             /** escape */
     451        3568 :             if (!version) {
     452        2926 :                 level = get_bits(gb, coef_nb_bits);
     453             :                 /** NOTE: this is rather suboptimal. reading
     454             :                  *  block_len_bits would be better */
     455        2926 :                 offset += get_bits(gb, frame_len_bits);
     456             :             } else {
     457         642 :                 level = ff_wma_get_large_val(gb);
     458             :                 /** escape decode */
     459         642 :                 if (get_bits1(gb)) {
     460         321 :                     if (get_bits1(gb)) {
     461         263 :                         if (get_bits1(gb)) {
     462           0 :                             av_log(avctx, AV_LOG_ERROR,
     463             :                                    "broken escape sequence\n");
     464           0 :                             return -1;
     465             :                         } else
     466         263 :                             offset += get_bits(gb, frame_len_bits) + 4;
     467             :                     } else
     468          58 :                         offset += get_bits(gb, 2) + 1;
     469             :                 }
     470             :             }
     471        3568 :             sign                    = get_bits1(gb) - 1;
     472        3568 :             ptr[offset & coef_mask] = (level ^ sign) - sign;
     473             :         }
     474             :     }
     475             :     /** NOTE: EOB can be omitted */
     476        2174 :     if (offset > num_coefs) {
     477           0 :         av_log(avctx, AV_LOG_ERROR,
     478             :                "overflow (%d > %d) in spectral RLE, ignoring\n",
     479             :                offset,
     480             :                num_coefs
     481             :               );
     482           0 :         return -1;
     483             :     }
     484             : 
     485        2174 :     return 0;
     486             : }

Generated by: LCOV version 1.13